~ubuntu-branches/ubuntu/trusty/qemu/trusty

« back to all changes in this revision

Viewing changes to gdbstub.c

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2013-10-22 22:47:07 UTC
  • mfrom: (1.8.3) (10.1.42 sid)
  • Revision ID: package-import@ubuntu.com-20131022224707-1lya34fw3k3f24tv
Tags: 1.6.0+dfsg-2ubuntu1
* Merge 1.6.0~rc0+dfsg-2exp from debian experimental.  Remaining changes:
  - debian/control
    * update maintainer
    * remove libiscsi, usb-redir, vde, vnc-jpeg, and libssh2-1-dev
      from build-deps
    * enable rbd
    * add qemu-system and qemu-common B/R to qemu-keymaps
    * add D:udev, R:qemu, R:qemu-common and B:qemu-common to
      qemu-system-common
    * qemu-system-arm, qemu-system-ppc, qemu-system-sparc:
      - add qemu-kvm to Provides
      - add qemu-common, qemu-kvm, kvm to B/R
      - remove openbios-sparc from qemu-system-sparc D
      - drop openbios-ppc and openhackware Depends to Suggests (for now)
    * qemu-system-x86:
      - add qemu-common to Breaks/Replaces.
      - add cpu-checker to Recommends.
    * qemu-user: add B/R:qemu-kvm
    * qemu-kvm:
      - add armhf armel powerpc sparc to Architecture
      - C/R/P: qemu-kvm-spice
    * add qemu-common package
    * drop qemu-slof which is not packaged in ubuntu
  - add qemu-system-common.links for tap ifup/down scripts and OVMF link.
  - qemu-system-x86.links:
    * remove pxe rom links which are in kvm-ipxe
    * add symlink for kvm.1 manpage
  - debian/rules
    * add kvm-spice symlink to qemu-kvm
    * call dh_installmodules for qemu-system-x86
    * update dh_installinit to install upstart script
    * run dh_installman (Closes: #709241) (cherrypicked from 1.5.0+dfsg-2)
  - Add qemu-utils.links for kvm-* symlinks.
  - Add qemu-system-x86.qemu-kvm.upstart and .default
  - Add qemu-system-x86.modprobe to set nesting=1
  - Add qemu-system-common.preinst to add kvm group
  - qemu-system-common.postinst: remove bad group acl if there, then have
    udev relabel /dev/kvm.
  - New linaro patches from qemu-linaro rebasing branch
  - Dropped patches:
    * xen-simplify-xen_enabled.patch
    * sparc-linux-user-fix-missing-symbols-in-.rel-.rela.plt-sections.patch
    * main_loop-do-not-set-nonblocking-if-xen_enabled.patch
    * xen_machine_pv-do-not-create-a-dummy-CPU-in-machine-.patch
    * virtio-rng-fix-crash
  - Kept patches:
    * expose_vms_qemu64cpu.patch - updated
    * linaro arm patches from qemu-linaro rebasing branch
  - New patches:
    * fix-pci-add: change CONFIG variable in ifdef to make sure that
      pci_add is defined.
* Add linaro patches
* Add experimental mach-virt patches for arm virtualization.
* qemu-system-common.install: add debian/tmp/usr/lib to install the
  qemu-bridge-helper

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
#include "cpu.h"
41
41
#include "qemu/sockets.h"
42
42
#include "sysemu/kvm.h"
43
 
#include "qemu/bitops.h"
44
43
 
45
 
#ifndef TARGET_CPU_MEMORY_RW_DEBUG
46
 
static inline int target_memory_rw_debug(CPUArchState *env, target_ulong addr,
47
 
                                         uint8_t *buf, int len, int is_write)
 
44
static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
 
45
                                         uint8_t *buf, int len, bool is_write)
48
46
{
49
 
    return cpu_memory_rw_debug(env, addr, buf, len, is_write);
 
47
    CPUClass *cc = CPU_GET_CLASS(cpu);
 
48
 
 
49
    if (cc->memory_rw_debug) {
 
50
        return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
 
51
    }
 
52
    return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
50
53
}
51
 
#else
52
 
/* target_memory_rw_debug() defined in cpu.h */
53
 
#endif
54
54
 
55
55
enum {
56
56
    GDB_SIGNAL_0 = 0,
287
287
    RS_CHKSUM2,
288
288
};
289
289
typedef struct GDBState {
290
 
    CPUArchState *c_cpu; /* current CPU for step/continue ops */
291
 
    CPUArchState *g_cpu; /* current CPU for other ops */
292
 
    CPUArchState *query_cpu; /* for q{f|s}ThreadInfo */
 
290
    CPUState *c_cpu; /* current CPU for step/continue ops */
 
291
    CPUState *g_cpu; /* current CPU for other ops */
 
292
    CPUState *query_cpu; /* for q{f|s}ThreadInfo */
293
293
    enum RSState state; /* parsing state */
294
294
    char line_buf[MAX_PACKET_LENGTH];
295
295
    int line_buf_index;
315
315
 
316
316
static GDBState *gdbserver_state;
317
317
 
318
 
/* This is an ugly hack to cope with both new and old gdb.
319
 
   If gdb sends qXfer:features:read then assume we're talking to a newish
320
 
   gdb that understands target descriptions.  */
321
 
static int gdb_has_xml;
 
318
bool gdb_has_xml;
322
319
 
323
320
#ifdef CONFIG_USER_ONLY
324
321
/* XXX: This is not thread safe.  Do we care?  */
371
368
#ifdef CONFIG_USER_ONLY
372
369
    s->running_state = 1;
373
370
#else
374
 
    vm_start();
 
371
    if (runstate_check(RUN_STATE_GUEST_PANICKED)) {
 
372
        runstate_set(RUN_STATE_DEBUG);
 
373
    }
 
374
    if (!runstate_needs_reset()) {
 
375
        vm_start();
 
376
    }
375
377
#endif
376
378
}
377
379
 
483
485
    return put_packet_binary(s, buf, strlen(buf));
484
486
}
485
487
 
486
 
/* The GDB remote protocol transfers values in target byte order.  This means
487
 
   we can use the raw memory access routines to access the value buffer.
488
 
   Conveniently, these also handle the case where the buffer is mis-aligned.
489
 
 */
490
 
#define GET_REG8(val) do { \
491
 
    stb_p(mem_buf, val); \
492
 
    return 1; \
493
 
    } while(0)
494
 
#define GET_REG16(val) do { \
495
 
    stw_p(mem_buf, val); \
496
 
    return 2; \
497
 
    } while(0)
498
 
#define GET_REG32(val) do { \
499
 
    stl_p(mem_buf, val); \
500
 
    return 4; \
501
 
    } while(0)
502
 
#define GET_REG64(val) do { \
503
 
    stq_p(mem_buf, val); \
504
 
    return 8; \
505
 
    } while(0)
506
 
 
507
 
#if TARGET_LONG_BITS == 64
508
 
#define GET_REGL(val) GET_REG64(val)
509
 
#define ldtul_p(addr) ldq_p(addr)
510
 
#else
511
 
#define GET_REGL(val) GET_REG32(val)
512
 
#define ldtul_p(addr) ldl_p(addr)
513
 
#endif
514
 
 
515
 
#if defined(TARGET_I386)
516
 
 
517
 
#ifdef TARGET_X86_64
518
 
static const int gpr_map[16] = {
519
 
    R_EAX, R_EBX, R_ECX, R_EDX, R_ESI, R_EDI, R_EBP, R_ESP,
520
 
    8, 9, 10, 11, 12, 13, 14, 15
521
 
};
522
 
#else
523
 
#define gpr_map gpr_map32
524
 
#endif
525
 
static const int gpr_map32[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
526
 
 
527
 
#define NUM_CORE_REGS (CPU_NB_REGS * 2 + 25)
528
 
 
529
 
#define IDX_IP_REG      CPU_NB_REGS
530
 
#define IDX_FLAGS_REG   (IDX_IP_REG + 1)
531
 
#define IDX_SEG_REGS    (IDX_FLAGS_REG + 1)
532
 
#define IDX_FP_REGS     (IDX_SEG_REGS + 6)
533
 
#define IDX_XMM_REGS    (IDX_FP_REGS + 16)
534
 
#define IDX_MXCSR_REG   (IDX_XMM_REGS + CPU_NB_REGS)
535
 
 
536
 
static int cpu_gdb_read_register(CPUX86State *env, uint8_t *mem_buf, int n)
537
 
{
538
 
    if (n < CPU_NB_REGS) {
539
 
        if (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK) {
540
 
            GET_REG64(env->regs[gpr_map[n]]);
541
 
        } else if (n < CPU_NB_REGS32) {
542
 
            GET_REG32(env->regs[gpr_map32[n]]);
543
 
        }
544
 
    } else if (n >= IDX_FP_REGS && n < IDX_FP_REGS + 8) {
545
 
#ifdef USE_X86LDOUBLE
546
 
        /* FIXME: byteswap float values - after fixing fpregs layout. */
547
 
        memcpy(mem_buf, &env->fpregs[n - IDX_FP_REGS], 10);
548
 
#else
549
 
        memset(mem_buf, 0, 10);
550
 
#endif
551
 
        return 10;
552
 
    } else if (n >= IDX_XMM_REGS && n < IDX_XMM_REGS + CPU_NB_REGS) {
553
 
        n -= IDX_XMM_REGS;
554
 
        if (n < CPU_NB_REGS32 ||
555
 
            (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK)) {
556
 
            stq_p(mem_buf, env->xmm_regs[n].XMM_Q(0));
557
 
            stq_p(mem_buf + 8, env->xmm_regs[n].XMM_Q(1));
558
 
            return 16;
559
 
        }
560
 
    } else {
561
 
        switch (n) {
562
 
        case IDX_IP_REG:
563
 
            if (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK) {
564
 
                GET_REG64(env->eip);
565
 
            } else {
566
 
                GET_REG32(env->eip);
567
 
            }
568
 
        case IDX_FLAGS_REG: GET_REG32(env->eflags);
569
 
 
570
 
        case IDX_SEG_REGS:     GET_REG32(env->segs[R_CS].selector);
571
 
        case IDX_SEG_REGS + 1: GET_REG32(env->segs[R_SS].selector);
572
 
        case IDX_SEG_REGS + 2: GET_REG32(env->segs[R_DS].selector);
573
 
        case IDX_SEG_REGS + 3: GET_REG32(env->segs[R_ES].selector);
574
 
        case IDX_SEG_REGS + 4: GET_REG32(env->segs[R_FS].selector);
575
 
        case IDX_SEG_REGS + 5: GET_REG32(env->segs[R_GS].selector);
576
 
 
577
 
        case IDX_FP_REGS + 8:  GET_REG32(env->fpuc);
578
 
        case IDX_FP_REGS + 9:  GET_REG32((env->fpus & ~0x3800) |
579
 
                                         (env->fpstt & 0x7) << 11);
580
 
        case IDX_FP_REGS + 10: GET_REG32(0); /* ftag */
581
 
        case IDX_FP_REGS + 11: GET_REG32(0); /* fiseg */
582
 
        case IDX_FP_REGS + 12: GET_REG32(0); /* fioff */
583
 
        case IDX_FP_REGS + 13: GET_REG32(0); /* foseg */
584
 
        case IDX_FP_REGS + 14: GET_REG32(0); /* fooff */
585
 
        case IDX_FP_REGS + 15: GET_REG32(0); /* fop */
586
 
 
587
 
        case IDX_MXCSR_REG: GET_REG32(env->mxcsr);
588
 
        }
589
 
    }
590
 
    return 0;
591
 
}
592
 
 
593
 
static int cpu_x86_gdb_load_seg(CPUX86State *env, int sreg, uint8_t *mem_buf)
594
 
{
595
 
    uint16_t selector = ldl_p(mem_buf);
596
 
 
597
 
    if (selector != env->segs[sreg].selector) {
598
 
#if defined(CONFIG_USER_ONLY)
599
 
        cpu_x86_load_seg(env, sreg, selector);
600
 
#else
601
 
        unsigned int limit, flags;
602
 
        target_ulong base;
603
 
 
604
 
        if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) {
605
 
            base = selector << 4;
606
 
            limit = 0xffff;
607
 
            flags = 0;
608
 
        } else {
609
 
            if (!cpu_x86_get_descr_debug(env, selector, &base, &limit, &flags))
610
 
                return 4;
611
 
        }
612
 
        cpu_x86_load_seg_cache(env, sreg, selector, base, limit, flags);
613
 
#endif
614
 
    }
615
 
    return 4;
616
 
}
617
 
 
618
 
static int cpu_gdb_write_register(CPUX86State *env, uint8_t *mem_buf, int n)
619
 
{
620
 
    uint32_t tmp;
621
 
 
622
 
    if (n < CPU_NB_REGS) {
623
 
        if (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK) {
624
 
            env->regs[gpr_map[n]] = ldtul_p(mem_buf);
625
 
            return sizeof(target_ulong);
626
 
        } else if (n < CPU_NB_REGS32) {
627
 
            n = gpr_map32[n];
628
 
            env->regs[n] &= ~0xffffffffUL;
629
 
            env->regs[n] |= (uint32_t)ldl_p(mem_buf);
630
 
            return 4;
631
 
        }
632
 
    } else if (n >= IDX_FP_REGS && n < IDX_FP_REGS + 8) {
633
 
#ifdef USE_X86LDOUBLE
634
 
        /* FIXME: byteswap float values - after fixing fpregs layout. */
635
 
        memcpy(&env->fpregs[n - IDX_FP_REGS], mem_buf, 10);
636
 
#endif
637
 
        return 10;
638
 
    } else if (n >= IDX_XMM_REGS && n < IDX_XMM_REGS + CPU_NB_REGS) {
639
 
        n -= IDX_XMM_REGS;
640
 
        if (n < CPU_NB_REGS32 ||
641
 
            (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK)) {
642
 
            env->xmm_regs[n].XMM_Q(0) = ldq_p(mem_buf);
643
 
            env->xmm_regs[n].XMM_Q(1) = ldq_p(mem_buf + 8);
644
 
            return 16;
645
 
        }
646
 
    } else {
647
 
        switch (n) {
648
 
        case IDX_IP_REG:
649
 
            if (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK) {
650
 
                env->eip = ldq_p(mem_buf);
651
 
                return 8;
652
 
            } else {
653
 
                env->eip &= ~0xffffffffUL;
654
 
                env->eip |= (uint32_t)ldl_p(mem_buf);
655
 
                return 4;
656
 
            }
657
 
        case IDX_FLAGS_REG:
658
 
            env->eflags = ldl_p(mem_buf);
659
 
            return 4;
660
 
 
661
 
        case IDX_SEG_REGS:     return cpu_x86_gdb_load_seg(env, R_CS, mem_buf);
662
 
        case IDX_SEG_REGS + 1: return cpu_x86_gdb_load_seg(env, R_SS, mem_buf);
663
 
        case IDX_SEG_REGS + 2: return cpu_x86_gdb_load_seg(env, R_DS, mem_buf);
664
 
        case IDX_SEG_REGS + 3: return cpu_x86_gdb_load_seg(env, R_ES, mem_buf);
665
 
        case IDX_SEG_REGS + 4: return cpu_x86_gdb_load_seg(env, R_FS, mem_buf);
666
 
        case IDX_SEG_REGS + 5: return cpu_x86_gdb_load_seg(env, R_GS, mem_buf);
667
 
 
668
 
        case IDX_FP_REGS + 8:
669
 
            env->fpuc = ldl_p(mem_buf);
670
 
            return 4;
671
 
        case IDX_FP_REGS + 9:
672
 
            tmp = ldl_p(mem_buf);
673
 
            env->fpstt = (tmp >> 11) & 7;
674
 
            env->fpus = tmp & ~0x3800;
675
 
            return 4;
676
 
        case IDX_FP_REGS + 10: /* ftag */  return 4;
677
 
        case IDX_FP_REGS + 11: /* fiseg */ return 4;
678
 
        case IDX_FP_REGS + 12: /* fioff */ return 4;
679
 
        case IDX_FP_REGS + 13: /* foseg */ return 4;
680
 
        case IDX_FP_REGS + 14: /* fooff */ return 4;
681
 
        case IDX_FP_REGS + 15: /* fop */   return 4;
682
 
 
683
 
        case IDX_MXCSR_REG:
684
 
            env->mxcsr = ldl_p(mem_buf);
685
 
            return 4;
686
 
        }
687
 
    }
688
 
    /* Unrecognised register.  */
689
 
    return 0;
690
 
}
691
 
 
692
 
#elif defined (TARGET_PPC)
693
 
 
694
 
/* Old gdb always expects FP registers.  Newer (xml-aware) gdb only
695
 
   expects whatever the target description contains.  Due to a
696
 
   historical mishap the FP registers appear in between core integer
697
 
   regs and PC, MSR, CR, and so forth.  We hack round this by giving the
698
 
   FP regs zero size when talking to a newer gdb.  */
699
 
#define NUM_CORE_REGS 71
700
 
#if defined (TARGET_PPC64)
701
 
#define GDB_CORE_XML "power64-core.xml"
702
 
#else
703
 
#define GDB_CORE_XML "power-core.xml"
704
 
#endif
705
 
 
706
 
static int cpu_gdb_read_register(CPUPPCState *env, uint8_t *mem_buf, int n)
707
 
{
708
 
    if (n < 32) {
709
 
        /* gprs */
710
 
        GET_REGL(env->gpr[n]);
711
 
    } else if (n < 64) {
712
 
        /* fprs */
713
 
        if (gdb_has_xml)
714
 
            return 0;
715
 
        stfq_p(mem_buf, env->fpr[n-32]);
716
 
        return 8;
717
 
    } else {
718
 
        switch (n) {
719
 
        case 64: GET_REGL(env->nip);
720
 
        case 65: GET_REGL(env->msr);
721
 
        case 66:
722
 
            {
723
 
                uint32_t cr = 0;
724
 
                int i;
725
 
                for (i = 0; i < 8; i++)
726
 
                    cr |= env->crf[i] << (32 - ((i + 1) * 4));
727
 
                GET_REG32(cr);
728
 
            }
729
 
        case 67: GET_REGL(env->lr);
730
 
        case 68: GET_REGL(env->ctr);
731
 
        case 69: GET_REGL(env->xer);
732
 
        case 70:
733
 
            {
734
 
                if (gdb_has_xml)
735
 
                    return 0;
736
 
                GET_REG32(env->fpscr);
737
 
            }
738
 
        }
739
 
    }
740
 
    return 0;
741
 
}
742
 
 
743
 
static int cpu_gdb_write_register(CPUPPCState *env, uint8_t *mem_buf, int n)
744
 
{
745
 
    if (n < 32) {
746
 
        /* gprs */
747
 
        env->gpr[n] = ldtul_p(mem_buf);
748
 
        return sizeof(target_ulong);
749
 
    } else if (n < 64) {
750
 
        /* fprs */
751
 
        if (gdb_has_xml)
752
 
            return 0;
753
 
        env->fpr[n-32] = ldfq_p(mem_buf);
754
 
        return 8;
755
 
    } else {
756
 
        switch (n) {
757
 
        case 64:
758
 
            env->nip = ldtul_p(mem_buf);
759
 
            return sizeof(target_ulong);
760
 
        case 65:
761
 
            ppc_store_msr(env, ldtul_p(mem_buf));
762
 
            return sizeof(target_ulong);
763
 
        case 66:
764
 
            {
765
 
                uint32_t cr = ldl_p(mem_buf);
766
 
                int i;
767
 
                for (i = 0; i < 8; i++)
768
 
                    env->crf[i] = (cr >> (32 - ((i + 1) * 4))) & 0xF;
769
 
                return 4;
770
 
            }
771
 
        case 67:
772
 
            env->lr = ldtul_p(mem_buf);
773
 
            return sizeof(target_ulong);
774
 
        case 68:
775
 
            env->ctr = ldtul_p(mem_buf);
776
 
            return sizeof(target_ulong);
777
 
        case 69:
778
 
            env->xer = ldtul_p(mem_buf);
779
 
            return sizeof(target_ulong);
780
 
        case 70:
781
 
            /* fpscr */
782
 
            if (gdb_has_xml)
783
 
                return 0;
784
 
            store_fpscr(env, ldtul_p(mem_buf), 0xffffffff);
785
 
            return sizeof(target_ulong);
786
 
        }
787
 
    }
788
 
    return 0;
789
 
}
790
 
 
791
 
#elif defined (TARGET_SPARC)
792
 
 
793
 
#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
794
 
#define NUM_CORE_REGS 86
795
 
#else
796
 
#define NUM_CORE_REGS 72
797
 
#endif
798
 
 
799
 
#ifdef TARGET_ABI32
800
 
#define GET_REGA(val) GET_REG32(val)
801
 
#else
802
 
#define GET_REGA(val) GET_REGL(val)
803
 
#endif
804
 
 
805
 
static int cpu_gdb_read_register(CPUSPARCState *env, uint8_t *mem_buf, int n)
806
 
{
807
 
    if (n < 8) {
808
 
        /* g0..g7 */
809
 
        GET_REGA(env->gregs[n]);
810
 
    }
811
 
    if (n < 32) {
812
 
        /* register window */
813
 
        GET_REGA(env->regwptr[n - 8]);
814
 
    }
815
 
#if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
816
 
    if (n < 64) {
817
 
        /* fprs */
818
 
        if (n & 1) {
819
 
            GET_REG32(env->fpr[(n - 32) / 2].l.lower);
820
 
        } else {
821
 
            GET_REG32(env->fpr[(n - 32) / 2].l.upper);
822
 
        }
823
 
    }
824
 
    /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
825
 
    switch (n) {
826
 
    case 64: GET_REGA(env->y);
827
 
    case 65: GET_REGA(cpu_get_psr(env));
828
 
    case 66: GET_REGA(env->wim);
829
 
    case 67: GET_REGA(env->tbr);
830
 
    case 68: GET_REGA(env->pc);
831
 
    case 69: GET_REGA(env->npc);
832
 
    case 70: GET_REGA(env->fsr);
833
 
    case 71: GET_REGA(0); /* csr */
834
 
    default: GET_REGA(0);
835
 
    }
836
 
#else
837
 
    if (n < 64) {
838
 
        /* f0-f31 */
839
 
        if (n & 1) {
840
 
            GET_REG32(env->fpr[(n - 32) / 2].l.lower);
841
 
        } else {
842
 
            GET_REG32(env->fpr[(n - 32) / 2].l.upper);
843
 
        }
844
 
    }
845
 
    if (n < 80) {
846
 
        /* f32-f62 (double width, even numbers only) */
847
 
        GET_REG64(env->fpr[(n - 32) / 2].ll);
848
 
    }
849
 
    switch (n) {
850
 
    case 80: GET_REGL(env->pc);
851
 
    case 81: GET_REGL(env->npc);
852
 
    case 82: GET_REGL((cpu_get_ccr(env) << 32) |
853
 
                      ((env->asi & 0xff) << 24) |
854
 
                      ((env->pstate & 0xfff) << 8) |
855
 
                      cpu_get_cwp64(env));
856
 
    case 83: GET_REGL(env->fsr);
857
 
    case 84: GET_REGL(env->fprs);
858
 
    case 85: GET_REGL(env->y);
859
 
    }
860
 
#endif
861
 
    return 0;
862
 
}
863
 
 
864
 
static int cpu_gdb_write_register(CPUSPARCState *env, uint8_t *mem_buf, int n)
865
 
{
866
 
#if defined(TARGET_ABI32)
867
 
    abi_ulong tmp;
868
 
 
869
 
    tmp = ldl_p(mem_buf);
870
 
#else
871
 
    target_ulong tmp;
872
 
 
873
 
    tmp = ldtul_p(mem_buf);
874
 
#endif
875
 
 
876
 
    if (n < 8) {
877
 
        /* g0..g7 */
878
 
        env->gregs[n] = tmp;
879
 
    } else if (n < 32) {
880
 
        /* register window */
881
 
        env->regwptr[n - 8] = tmp;
882
 
    }
883
 
#if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
884
 
    else if (n < 64) {
885
 
        /* fprs */
886
 
        /* f0-f31 */
887
 
        if (n & 1) {
888
 
            env->fpr[(n - 32) / 2].l.lower = tmp;
889
 
        } else {
890
 
            env->fpr[(n - 32) / 2].l.upper = tmp;
891
 
        }
892
 
    } else {
893
 
        /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
894
 
        switch (n) {
895
 
        case 64: env->y = tmp; break;
896
 
        case 65: cpu_put_psr(env, tmp); break;
897
 
        case 66: env->wim = tmp; break;
898
 
        case 67: env->tbr = tmp; break;
899
 
        case 68: env->pc = tmp; break;
900
 
        case 69: env->npc = tmp; break;
901
 
        case 70: env->fsr = tmp; break;
902
 
        default: return 0;
903
 
        }
904
 
    }
905
 
    return 4;
906
 
#else
907
 
    else if (n < 64) {
908
 
        /* f0-f31 */
909
 
        tmp = ldl_p(mem_buf);
910
 
        if (n & 1) {
911
 
            env->fpr[(n - 32) / 2].l.lower = tmp;
912
 
        } else {
913
 
            env->fpr[(n - 32) / 2].l.upper = tmp;
914
 
        }
915
 
        return 4;
916
 
    } else if (n < 80) {
917
 
        /* f32-f62 (double width, even numbers only) */
918
 
        env->fpr[(n - 32) / 2].ll = tmp;
919
 
    } else {
920
 
        switch (n) {
921
 
        case 80: env->pc = tmp; break;
922
 
        case 81: env->npc = tmp; break;
923
 
        case 82:
924
 
            cpu_put_ccr(env, tmp >> 32);
925
 
            env->asi = (tmp >> 24) & 0xff;
926
 
            env->pstate = (tmp >> 8) & 0xfff;
927
 
            cpu_put_cwp64(env, tmp & 0xff);
928
 
            break;
929
 
        case 83: env->fsr = tmp; break;
930
 
        case 84: env->fprs = tmp; break;
931
 
        case 85: env->y = tmp; break;
932
 
        default: return 0;
933
 
        }
934
 
    }
935
 
    return 8;
936
 
#endif
937
 
}
938
 
#elif defined (TARGET_ARM)
939
 
 
940
 
/* Old gdb always expect FPA registers.  Newer (xml-aware) gdb only expect
941
 
   whatever the target description contains.  Due to a historical mishap
942
 
   the FPA registers appear in between core integer regs and the CPSR.
943
 
   We hack round this by giving the FPA regs zero size when talking to a
944
 
   newer gdb.  */
945
 
#define NUM_CORE_REGS 26
946
 
#define GDB_CORE_XML "arm-core.xml"
947
 
 
948
 
static int cpu_gdb_read_register(CPUARMState *env, uint8_t *mem_buf, int n)
949
 
{
950
 
    if (n < 16) {
951
 
        /* Core integer register.  */
952
 
        GET_REG32(env->regs[n]);
953
 
    }
954
 
    if (n < 24) {
955
 
        /* FPA registers.  */
956
 
        if (gdb_has_xml)
957
 
            return 0;
958
 
        memset(mem_buf, 0, 12);
959
 
        return 12;
960
 
    }
961
 
    switch (n) {
962
 
    case 24:
963
 
        /* FPA status register.  */
964
 
        if (gdb_has_xml)
965
 
            return 0;
966
 
        GET_REG32(0);
967
 
    case 25:
968
 
        /* CPSR */
969
 
        GET_REG32(cpsr_read(env));
970
 
    }
971
 
    /* Unknown register.  */
972
 
    return 0;
973
 
}
974
 
 
975
 
static int cpu_gdb_write_register(CPUARMState *env, uint8_t *mem_buf, int n)
976
 
{
977
 
    uint32_t tmp;
978
 
 
979
 
    tmp = ldl_p(mem_buf);
980
 
 
981
 
    /* Mask out low bit of PC to workaround gdb bugs.  This will probably
982
 
       cause problems if we ever implement the Jazelle DBX extensions.  */
983
 
    if (n == 15)
984
 
        tmp &= ~1;
985
 
 
986
 
    if (n < 16) {
987
 
        /* Core integer register.  */
988
 
        env->regs[n] = tmp;
989
 
        return 4;
990
 
    }
991
 
    if (n < 24) { /* 16-23 */
992
 
        /* FPA registers (ignored).  */
993
 
        if (gdb_has_xml)
994
 
            return 0;
995
 
        return 12;
996
 
    }
997
 
    switch (n) {
998
 
    case 24:
999
 
        /* FPA status register (ignored).  */
1000
 
        if (gdb_has_xml)
1001
 
            return 0;
1002
 
        return 4;
1003
 
    case 25:
1004
 
        /* CPSR */
1005
 
        cpsr_write (env, tmp, 0xffffffff);
1006
 
        return 4;
1007
 
    }
1008
 
    /* Unknown register.  */
1009
 
    return 0;
1010
 
}
1011
 
 
1012
 
#elif defined (TARGET_M68K)
1013
 
 
1014
 
#define NUM_CORE_REGS 18
1015
 
 
1016
 
#define GDB_CORE_XML "cf-core.xml"
1017
 
 
1018
 
static int cpu_gdb_read_register(CPUM68KState *env, uint8_t *mem_buf, int n)
1019
 
{
1020
 
    if (n < 8) {
1021
 
        /* D0-D7 */
1022
 
        GET_REG32(env->dregs[n]);
1023
 
    } else if (n < 16) {
1024
 
        /* A0-A7 */
1025
 
        GET_REG32(env->aregs[n - 8]);
1026
 
    } else {
1027
 
        switch (n) {
1028
 
        case 16: GET_REG32(env->sr);
1029
 
        case 17: GET_REG32(env->pc);
1030
 
        }
1031
 
    }
1032
 
    /* FP registers not included here because they vary between
1033
 
       ColdFire and m68k.  Use XML bits for these.  */
1034
 
    return 0;
1035
 
}
1036
 
 
1037
 
static int cpu_gdb_write_register(CPUM68KState *env, uint8_t *mem_buf, int n)
1038
 
{
1039
 
    uint32_t tmp;
1040
 
 
1041
 
    tmp = ldl_p(mem_buf);
1042
 
 
1043
 
    if (n < 8) {
1044
 
        /* D0-D7 */
1045
 
        env->dregs[n] = tmp;
1046
 
    } else if (n < 16) {
1047
 
        /* A0-A7 */
1048
 
        env->aregs[n - 8] = tmp;
1049
 
    } else {
1050
 
        switch (n) {
1051
 
        case 16: env->sr = tmp; break;
1052
 
        case 17: env->pc = tmp; break;
1053
 
        default: return 0;
1054
 
        }
1055
 
    }
1056
 
    return 4;
1057
 
}
1058
 
#elif defined (TARGET_MIPS)
1059
 
 
1060
 
#define NUM_CORE_REGS 73
1061
 
 
1062
 
static int cpu_gdb_read_register(CPUMIPSState *env, uint8_t *mem_buf, int n)
1063
 
{
1064
 
    if (n < 32) {
1065
 
        GET_REGL(env->active_tc.gpr[n]);
1066
 
    }
1067
 
    if (env->CP0_Config1 & (1 << CP0C1_FP)) {
1068
 
        if (n >= 38 && n < 70) {
1069
 
            if (env->CP0_Status & (1 << CP0St_FR))
1070
 
                GET_REGL(env->active_fpu.fpr[n - 38].d);
1071
 
            else
1072
 
                GET_REGL(env->active_fpu.fpr[n - 38].w[FP_ENDIAN_IDX]);
1073
 
        }
1074
 
        switch (n) {
1075
 
        case 70: GET_REGL((int32_t)env->active_fpu.fcr31);
1076
 
        case 71: GET_REGL((int32_t)env->active_fpu.fcr0);
1077
 
        }
1078
 
    }
1079
 
    switch (n) {
1080
 
    case 32: GET_REGL((int32_t)env->CP0_Status);
1081
 
    case 33: GET_REGL(env->active_tc.LO[0]);
1082
 
    case 34: GET_REGL(env->active_tc.HI[0]);
1083
 
    case 35: GET_REGL(env->CP0_BadVAddr);
1084
 
    case 36: GET_REGL((int32_t)env->CP0_Cause);
1085
 
    case 37: GET_REGL(env->active_tc.PC | !!(env->hflags & MIPS_HFLAG_M16));
1086
 
    case 72: GET_REGL(0); /* fp */
1087
 
    case 89: GET_REGL((int32_t)env->CP0_PRid);
1088
 
    }
1089
 
    if (n >= 73 && n <= 88) {
1090
 
        /* 16 embedded regs.  */
1091
 
        GET_REGL(0);
1092
 
    }
1093
 
 
1094
 
    return 0;
1095
 
}
1096
 
 
1097
 
/* convert MIPS rounding mode in FCR31 to IEEE library */
1098
 
static unsigned int ieee_rm[] =
1099
 
  {
1100
 
    float_round_nearest_even,
1101
 
    float_round_to_zero,
1102
 
    float_round_up,
1103
 
    float_round_down
1104
 
  };
1105
 
#define RESTORE_ROUNDING_MODE \
1106
 
    set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3], &env->active_fpu.fp_status)
1107
 
 
1108
 
static int cpu_gdb_write_register(CPUMIPSState *env, uint8_t *mem_buf, int n)
1109
 
{
1110
 
    target_ulong tmp;
1111
 
 
1112
 
    tmp = ldtul_p(mem_buf);
1113
 
 
1114
 
    if (n < 32) {
1115
 
        env->active_tc.gpr[n] = tmp;
1116
 
        return sizeof(target_ulong);
1117
 
    }
1118
 
    if (env->CP0_Config1 & (1 << CP0C1_FP)
1119
 
            && n >= 38 && n < 73) {
1120
 
        if (n < 70) {
1121
 
            if (env->CP0_Status & (1 << CP0St_FR))
1122
 
              env->active_fpu.fpr[n - 38].d = tmp;
1123
 
            else
1124
 
              env->active_fpu.fpr[n - 38].w[FP_ENDIAN_IDX] = tmp;
1125
 
        }
1126
 
        switch (n) {
1127
 
        case 70:
1128
 
            env->active_fpu.fcr31 = tmp & 0xFF83FFFF;
1129
 
            /* set rounding mode */
1130
 
            RESTORE_ROUNDING_MODE;
1131
 
            break;
1132
 
        case 71: env->active_fpu.fcr0 = tmp; break;
1133
 
        }
1134
 
        return sizeof(target_ulong);
1135
 
    }
1136
 
    switch (n) {
1137
 
    case 32: env->CP0_Status = tmp; break;
1138
 
    case 33: env->active_tc.LO[0] = tmp; break;
1139
 
    case 34: env->active_tc.HI[0] = tmp; break;
1140
 
    case 35: env->CP0_BadVAddr = tmp; break;
1141
 
    case 36: env->CP0_Cause = tmp; break;
1142
 
    case 37:
1143
 
        env->active_tc.PC = tmp & ~(target_ulong)1;
1144
 
        if (tmp & 1) {
1145
 
            env->hflags |= MIPS_HFLAG_M16;
1146
 
        } else {
1147
 
            env->hflags &= ~(MIPS_HFLAG_M16);
1148
 
        }
1149
 
        break;
1150
 
    case 72: /* fp, ignored */ break;
1151
 
    default: 
1152
 
        if (n > 89)
1153
 
            return 0;
1154
 
        /* Other registers are readonly.  Ignore writes.  */
1155
 
        break;
1156
 
    }
1157
 
 
1158
 
    return sizeof(target_ulong);
1159
 
}
1160
 
#elif defined(TARGET_OPENRISC)
1161
 
 
1162
 
#define NUM_CORE_REGS (32 + 3)
1163
 
 
1164
 
static int cpu_gdb_read_register(CPUOpenRISCState *env, uint8_t *mem_buf, int n)
1165
 
{
1166
 
    if (n < 32) {
1167
 
        GET_REG32(env->gpr[n]);
1168
 
    } else {
1169
 
        switch (n) {
1170
 
        case 32:    /* PPC */
1171
 
            GET_REG32(env->ppc);
1172
 
            break;
1173
 
 
1174
 
        case 33:    /* NPC */
1175
 
            GET_REG32(env->npc);
1176
 
            break;
1177
 
 
1178
 
        case 34:    /* SR */
1179
 
            GET_REG32(env->sr);
1180
 
            break;
1181
 
 
1182
 
        default:
1183
 
            break;
1184
 
        }
1185
 
    }
1186
 
    return 0;
1187
 
}
1188
 
 
1189
 
static int cpu_gdb_write_register(CPUOpenRISCState *env,
1190
 
                                  uint8_t *mem_buf, int n)
1191
 
{
1192
 
    uint32_t tmp;
1193
 
 
1194
 
    if (n > NUM_CORE_REGS) {
1195
 
        return 0;
1196
 
    }
1197
 
 
1198
 
    tmp = ldl_p(mem_buf);
1199
 
 
1200
 
    if (n < 32) {
1201
 
        env->gpr[n] = tmp;
1202
 
    } else {
1203
 
        switch (n) {
1204
 
        case 32: /* PPC */
1205
 
            env->ppc = tmp;
1206
 
            break;
1207
 
 
1208
 
        case 33: /* NPC */
1209
 
            env->npc = tmp;
1210
 
            break;
1211
 
 
1212
 
        case 34: /* SR */
1213
 
            env->sr = tmp;
1214
 
            break;
1215
 
 
1216
 
        default:
1217
 
            break;
1218
 
        }
1219
 
    }
1220
 
    return 4;
1221
 
}
1222
 
#elif defined (TARGET_SH4)
1223
 
 
1224
 
/* Hint: Use "set architecture sh4" in GDB to see fpu registers */
1225
 
/* FIXME: We should use XML for this.  */
1226
 
 
1227
 
#define NUM_CORE_REGS 59
1228
 
 
1229
 
static int cpu_gdb_read_register(CPUSH4State *env, uint8_t *mem_buf, int n)
1230
 
{
1231
 
    switch (n) {
1232
 
    case 0 ... 7:
1233
 
        if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
1234
 
            GET_REGL(env->gregs[n + 16]);
1235
 
        } else {
1236
 
            GET_REGL(env->gregs[n]);
1237
 
        }
1238
 
    case 8 ... 15:
1239
 
        GET_REGL(env->gregs[n]);
1240
 
    case 16:
1241
 
        GET_REGL(env->pc);
1242
 
    case 17:
1243
 
        GET_REGL(env->pr);
1244
 
    case 18:
1245
 
        GET_REGL(env->gbr);
1246
 
    case 19:
1247
 
        GET_REGL(env->vbr);
1248
 
    case 20:
1249
 
        GET_REGL(env->mach);
1250
 
    case 21:
1251
 
        GET_REGL(env->macl);
1252
 
    case 22:
1253
 
        GET_REGL(env->sr);
1254
 
    case 23:
1255
 
        GET_REGL(env->fpul);
1256
 
    case 24:
1257
 
        GET_REGL(env->fpscr);
1258
 
    case 25 ... 40:
1259
 
        if (env->fpscr & FPSCR_FR) {
1260
 
            stfl_p(mem_buf, env->fregs[n - 9]);
1261
 
        } else {
1262
 
            stfl_p(mem_buf, env->fregs[n - 25]);
1263
 
        }
1264
 
        return 4;
1265
 
    case 41:
1266
 
        GET_REGL(env->ssr);
1267
 
    case 42:
1268
 
        GET_REGL(env->spc);
1269
 
    case 43 ... 50:
1270
 
        GET_REGL(env->gregs[n - 43]);
1271
 
    case 51 ... 58:
1272
 
        GET_REGL(env->gregs[n - (51 - 16)]);
1273
 
    }
1274
 
 
1275
 
    return 0;
1276
 
}
1277
 
 
1278
 
static int cpu_gdb_write_register(CPUSH4State *env, uint8_t *mem_buf, int n)
1279
 
{
1280
 
    switch (n) {
1281
 
    case 0 ... 7:
1282
 
        if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
1283
 
            env->gregs[n + 16] = ldl_p(mem_buf);
1284
 
        } else {
1285
 
            env->gregs[n] = ldl_p(mem_buf);
1286
 
        }
1287
 
        break;
1288
 
    case 8 ... 15:
1289
 
        env->gregs[n] = ldl_p(mem_buf);
1290
 
        break;
1291
 
    case 16:
1292
 
        env->pc = ldl_p(mem_buf);
1293
 
        break;
1294
 
    case 17:
1295
 
        env->pr = ldl_p(mem_buf);
1296
 
        break;
1297
 
    case 18:
1298
 
        env->gbr = ldl_p(mem_buf);
1299
 
        break;
1300
 
    case 19:
1301
 
        env->vbr = ldl_p(mem_buf);
1302
 
        break;
1303
 
    case 20:
1304
 
        env->mach = ldl_p(mem_buf);
1305
 
        break;
1306
 
    case 21:
1307
 
        env->macl = ldl_p(mem_buf);
1308
 
        break;
1309
 
    case 22:
1310
 
        env->sr = ldl_p(mem_buf);
1311
 
        break;
1312
 
    case 23:
1313
 
        env->fpul = ldl_p(mem_buf);
1314
 
        break;
1315
 
    case 24:
1316
 
        env->fpscr = ldl_p(mem_buf);
1317
 
        break;
1318
 
    case 25 ... 40:
1319
 
        if (env->fpscr & FPSCR_FR) {
1320
 
            env->fregs[n - 9] = ldfl_p(mem_buf);
1321
 
        } else {
1322
 
            env->fregs[n - 25] = ldfl_p(mem_buf);
1323
 
        }
1324
 
        break;
1325
 
    case 41:
1326
 
        env->ssr = ldl_p(mem_buf);
1327
 
        break;
1328
 
    case 42:
1329
 
        env->spc = ldl_p(mem_buf);
1330
 
        break;
1331
 
    case 43 ... 50:
1332
 
        env->gregs[n - 43] = ldl_p(mem_buf);
1333
 
        break;
1334
 
    case 51 ... 58:
1335
 
        env->gregs[n - (51 - 16)] = ldl_p(mem_buf);
1336
 
        break;
1337
 
    default: return 0;
1338
 
    }
1339
 
 
1340
 
    return 4;
1341
 
}
1342
 
#elif defined (TARGET_MICROBLAZE)
1343
 
 
1344
 
#define NUM_CORE_REGS (32 + 5)
1345
 
 
1346
 
static int cpu_gdb_read_register(CPUMBState *env, uint8_t *mem_buf, int n)
1347
 
{
1348
 
    if (n < 32) {
1349
 
        GET_REG32(env->regs[n]);
1350
 
    } else {
1351
 
        GET_REG32(env->sregs[n - 32]);
1352
 
    }
1353
 
    return 0;
1354
 
}
1355
 
 
1356
 
static int cpu_gdb_write_register(CPUMBState *env, uint8_t *mem_buf, int n)
1357
 
{
1358
 
    uint32_t tmp;
1359
 
 
1360
 
    if (n > NUM_CORE_REGS)
1361
 
        return 0;
1362
 
 
1363
 
    tmp = ldl_p(mem_buf);
1364
 
 
1365
 
    if (n < 32) {
1366
 
        env->regs[n] = tmp;
1367
 
    } else {
1368
 
        env->sregs[n - 32] = tmp;
1369
 
    }
1370
 
    return 4;
1371
 
}
1372
 
#elif defined (TARGET_CRIS)
1373
 
 
1374
 
#define NUM_CORE_REGS 49
1375
 
 
1376
 
static int
1377
 
read_register_crisv10(CPUCRISState *env, uint8_t *mem_buf, int n)
1378
 
{
1379
 
    if (n < 15) {
1380
 
        GET_REG32(env->regs[n]);
1381
 
    }
1382
 
 
1383
 
    if (n == 15) {
1384
 
        GET_REG32(env->pc);
1385
 
    }
1386
 
 
1387
 
    if (n < 32) {
1388
 
        switch (n) {
1389
 
        case 16:
1390
 
            GET_REG8(env->pregs[n - 16]);
1391
 
            break;
1392
 
        case 17:
1393
 
            GET_REG8(env->pregs[n - 16]);
1394
 
            break;
1395
 
        case 20:
1396
 
        case 21:
1397
 
            GET_REG16(env->pregs[n - 16]);
1398
 
            break;
1399
 
        default:
1400
 
            if (n >= 23) {
1401
 
                GET_REG32(env->pregs[n - 16]);
1402
 
            }
1403
 
            break;
1404
 
        }
1405
 
    }
1406
 
    return 0;
1407
 
}
1408
 
 
1409
 
static int cpu_gdb_read_register(CPUCRISState *env, uint8_t *mem_buf, int n)
1410
 
{
1411
 
    uint8_t srs;
1412
 
 
1413
 
    if (env->pregs[PR_VR] < 32)
1414
 
        return read_register_crisv10(env, mem_buf, n);
1415
 
 
1416
 
    srs = env->pregs[PR_SRS];
1417
 
    if (n < 16) {
1418
 
        GET_REG32(env->regs[n]);
1419
 
    }
1420
 
 
1421
 
    if (n >= 21 && n < 32) {
1422
 
        GET_REG32(env->pregs[n - 16]);
1423
 
    }
1424
 
    if (n >= 33 && n < 49) {
1425
 
        GET_REG32(env->sregs[srs][n - 33]);
1426
 
    }
1427
 
    switch (n) {
1428
 
    case 16: GET_REG8(env->pregs[0]);
1429
 
    case 17: GET_REG8(env->pregs[1]);
1430
 
    case 18: GET_REG32(env->pregs[2]);
1431
 
    case 19: GET_REG8(srs);
1432
 
    case 20: GET_REG16(env->pregs[4]);
1433
 
    case 32: GET_REG32(env->pc);
1434
 
    }
1435
 
 
1436
 
    return 0;
1437
 
}
1438
 
 
1439
 
static int cpu_gdb_write_register(CPUCRISState *env, uint8_t *mem_buf, int n)
1440
 
{
1441
 
    uint32_t tmp;
1442
 
 
1443
 
    if (n > 49)
1444
 
        return 0;
1445
 
 
1446
 
    tmp = ldl_p(mem_buf);
1447
 
 
1448
 
    if (n < 16) {
1449
 
        env->regs[n] = tmp;
1450
 
    }
1451
 
 
1452
 
    if (n >= 21 && n < 32) {
1453
 
        env->pregs[n - 16] = tmp;
1454
 
    }
1455
 
 
1456
 
    /* FIXME: Should support function regs be writable?  */
1457
 
    switch (n) {
1458
 
    case 16: return 1;
1459
 
    case 17: return 1;
1460
 
    case 18: env->pregs[PR_PID] = tmp; break;
1461
 
    case 19: return 1;
1462
 
    case 20: return 2;
1463
 
    case 32: env->pc = tmp; break;
1464
 
    }
1465
 
 
1466
 
    return 4;
1467
 
}
1468
 
#elif defined (TARGET_ALPHA)
1469
 
 
1470
 
#define NUM_CORE_REGS 67
1471
 
 
1472
 
static int cpu_gdb_read_register(CPUAlphaState *env, uint8_t *mem_buf, int n)
1473
 
{
1474
 
    uint64_t val;
1475
 
    CPU_DoubleU d;
1476
 
 
1477
 
    switch (n) {
1478
 
    case 0 ... 30:
1479
 
        val = env->ir[n];
1480
 
        break;
1481
 
    case 32 ... 62:
1482
 
        d.d = env->fir[n - 32];
1483
 
        val = d.ll;
1484
 
        break;
1485
 
    case 63:
1486
 
        val = cpu_alpha_load_fpcr(env);
1487
 
        break;
1488
 
    case 64:
1489
 
        val = env->pc;
1490
 
        break;
1491
 
    case 66:
1492
 
        val = env->unique;
1493
 
        break;
1494
 
    case 31:
1495
 
    case 65:
1496
 
        /* 31 really is the zero register; 65 is unassigned in the
1497
 
           gdb protocol, but is still required to occupy 8 bytes. */
1498
 
        val = 0;
1499
 
        break;
1500
 
    default:
1501
 
        return 0;
1502
 
    }
1503
 
    GET_REGL(val);
1504
 
}
1505
 
 
1506
 
static int cpu_gdb_write_register(CPUAlphaState *env, uint8_t *mem_buf, int n)
1507
 
{
1508
 
    target_ulong tmp = ldtul_p(mem_buf);
1509
 
    CPU_DoubleU d;
1510
 
 
1511
 
    switch (n) {
1512
 
    case 0 ... 30:
1513
 
        env->ir[n] = tmp;
1514
 
        break;
1515
 
    case 32 ... 62:
1516
 
        d.ll = tmp;
1517
 
        env->fir[n - 32] = d.d;
1518
 
        break;
1519
 
    case 63:
1520
 
        cpu_alpha_store_fpcr(env, tmp);
1521
 
        break;
1522
 
    case 64:
1523
 
        env->pc = tmp;
1524
 
        break;
1525
 
    case 66:
1526
 
        env->unique = tmp;
1527
 
        break;
1528
 
    case 31:
1529
 
    case 65:
1530
 
        /* 31 really is the zero register; 65 is unassigned in the
1531
 
           gdb protocol, but is still required to occupy 8 bytes. */
1532
 
        break;
1533
 
    default:
1534
 
        return 0;
1535
 
    }
1536
 
    return 8;
1537
 
}
1538
 
#elif defined (TARGET_S390X)
1539
 
 
1540
 
#define NUM_CORE_REGS  S390_NUM_REGS
1541
 
 
1542
 
static int cpu_gdb_read_register(CPUS390XState *env, uint8_t *mem_buf, int n)
1543
 
{
1544
 
    uint64_t val;
1545
 
    int cc_op;
1546
 
 
1547
 
    switch (n) {
1548
 
    case S390_PSWM_REGNUM:
1549
 
        cc_op = calc_cc(env, env->cc_op, env->cc_src, env->cc_dst, env->cc_vr);
1550
 
        val = deposit64(env->psw.mask, 44, 2, cc_op);
1551
 
        GET_REGL(val);
1552
 
        break;
1553
 
    case S390_PSWA_REGNUM:
1554
 
        GET_REGL(env->psw.addr);
1555
 
        break;
1556
 
    case S390_R0_REGNUM ... S390_R15_REGNUM:
1557
 
        GET_REGL(env->regs[n-S390_R0_REGNUM]);
1558
 
        break;
1559
 
    case S390_A0_REGNUM ... S390_A15_REGNUM:
1560
 
        GET_REG32(env->aregs[n-S390_A0_REGNUM]);
1561
 
        break;
1562
 
    case S390_FPC_REGNUM:
1563
 
        GET_REG32(env->fpc);
1564
 
        break;
1565
 
    case S390_F0_REGNUM ... S390_F15_REGNUM:
1566
 
        GET_REG64(env->fregs[n-S390_F0_REGNUM].ll);
1567
 
        break;
1568
 
    }
1569
 
 
1570
 
    return 0;
1571
 
}
1572
 
 
1573
 
static int cpu_gdb_write_register(CPUS390XState *env, uint8_t *mem_buf, int n)
1574
 
{
1575
 
    target_ulong tmpl;
1576
 
    uint32_t tmp32;
1577
 
    int r = 8;
1578
 
    tmpl = ldtul_p(mem_buf);
1579
 
    tmp32 = ldl_p(mem_buf);
1580
 
 
1581
 
    switch (n) {
1582
 
    case S390_PSWM_REGNUM:
1583
 
        env->psw.mask = tmpl;
1584
 
        env->cc_op = extract64(tmpl, 44, 2);
1585
 
        break;
1586
 
    case S390_PSWA_REGNUM:
1587
 
        env->psw.addr = tmpl;
1588
 
        break;
1589
 
    case S390_R0_REGNUM ... S390_R15_REGNUM:
1590
 
        env->regs[n-S390_R0_REGNUM] = tmpl;
1591
 
        break;
1592
 
    case S390_A0_REGNUM ... S390_A15_REGNUM:
1593
 
        env->aregs[n-S390_A0_REGNUM] = tmp32;
1594
 
        r = 4;
1595
 
        break;
1596
 
    case S390_FPC_REGNUM:
1597
 
        env->fpc = tmp32;
1598
 
        r = 4;
1599
 
        break;
1600
 
    case S390_F0_REGNUM ... S390_F15_REGNUM:
1601
 
        env->fregs[n-S390_F0_REGNUM].ll = tmpl;
1602
 
        break;
1603
 
    default:
1604
 
        return 0;
1605
 
    }
1606
 
    return r;
1607
 
}
1608
 
#elif defined (TARGET_LM32)
1609
 
 
1610
 
#include "hw/lm32/lm32_pic.h"
1611
 
#define NUM_CORE_REGS (32 + 7)
1612
 
 
1613
 
static int cpu_gdb_read_register(CPULM32State *env, uint8_t *mem_buf, int n)
1614
 
{
1615
 
    if (n < 32) {
1616
 
        GET_REG32(env->regs[n]);
1617
 
    } else {
1618
 
        switch (n) {
1619
 
        case 32:
1620
 
            GET_REG32(env->pc);
1621
 
            break;
1622
 
        /* FIXME: put in right exception ID */
1623
 
        case 33:
1624
 
            GET_REG32(0);
1625
 
            break;
1626
 
        case 34:
1627
 
            GET_REG32(env->eba);
1628
 
            break;
1629
 
        case 35:
1630
 
            GET_REG32(env->deba);
1631
 
            break;
1632
 
        case 36:
1633
 
            GET_REG32(env->ie);
1634
 
            break;
1635
 
        case 37:
1636
 
            GET_REG32(lm32_pic_get_im(env->pic_state));
1637
 
            break;
1638
 
        case 38:
1639
 
            GET_REG32(lm32_pic_get_ip(env->pic_state));
1640
 
            break;
1641
 
        }
1642
 
    }
1643
 
    return 0;
1644
 
}
1645
 
 
1646
 
static int cpu_gdb_write_register(CPULM32State *env, uint8_t *mem_buf, int n)
1647
 
{
1648
 
    uint32_t tmp;
1649
 
 
1650
 
    if (n > NUM_CORE_REGS) {
1651
 
        return 0;
1652
 
    }
1653
 
 
1654
 
    tmp = ldl_p(mem_buf);
1655
 
 
1656
 
    if (n < 32) {
1657
 
        env->regs[n] = tmp;
1658
 
    } else {
1659
 
        switch (n) {
1660
 
        case 32:
1661
 
            env->pc = tmp;
1662
 
            break;
1663
 
        case 34:
1664
 
            env->eba = tmp;
1665
 
            break;
1666
 
        case 35:
1667
 
            env->deba = tmp;
1668
 
            break;
1669
 
        case 36:
1670
 
            env->ie = tmp;
1671
 
            break;
1672
 
        case 37:
1673
 
            lm32_pic_set_im(env->pic_state, tmp);
1674
 
            break;
1675
 
        case 38:
1676
 
            lm32_pic_set_ip(env->pic_state, tmp);
1677
 
            break;
1678
 
        }
1679
 
    }
1680
 
    return 4;
1681
 
}
1682
 
#elif defined(TARGET_XTENSA)
1683
 
 
1684
 
/* Use num_core_regs to see only non-privileged registers in an unmodified gdb.
1685
 
 * Use num_regs to see all registers. gdb modification is required for that:
1686
 
 * reset bit 0 in the 'flags' field of the registers definitions in the
1687
 
 * gdb/xtensa-config.c inside gdb source tree or inside gdb overlay.
1688
 
 */
1689
 
#define NUM_CORE_REGS (env->config->gdb_regmap.num_regs)
1690
 
#define num_g_regs NUM_CORE_REGS
1691
 
 
1692
 
static int cpu_gdb_read_register(CPUXtensaState *env, uint8_t *mem_buf, int n)
1693
 
{
1694
 
    const XtensaGdbReg *reg = env->config->gdb_regmap.reg + n;
1695
 
 
1696
 
    if (n < 0 || n >= env->config->gdb_regmap.num_regs) {
1697
 
        return 0;
1698
 
    }
1699
 
 
1700
 
    switch (reg->type) {
1701
 
    case 9: /*pc*/
1702
 
        GET_REG32(env->pc);
1703
 
        break;
1704
 
 
1705
 
    case 1: /*ar*/
1706
 
        xtensa_sync_phys_from_window(env);
1707
 
        GET_REG32(env->phys_regs[(reg->targno & 0xff) % env->config->nareg]);
1708
 
        break;
1709
 
 
1710
 
    case 2: /*SR*/
1711
 
        GET_REG32(env->sregs[reg->targno & 0xff]);
1712
 
        break;
1713
 
 
1714
 
    case 3: /*UR*/
1715
 
        GET_REG32(env->uregs[reg->targno & 0xff]);
1716
 
        break;
1717
 
 
1718
 
    case 4: /*f*/
1719
 
        GET_REG32(float32_val(env->fregs[reg->targno & 0x0f]));
1720
 
        break;
1721
 
 
1722
 
    case 8: /*a*/
1723
 
        GET_REG32(env->regs[reg->targno & 0x0f]);
1724
 
        break;
1725
 
 
1726
 
    default:
1727
 
        qemu_log("%s from reg %d of unsupported type %d\n",
1728
 
                __func__, n, reg->type);
1729
 
        return 0;
1730
 
    }
1731
 
}
1732
 
 
1733
 
static int cpu_gdb_write_register(CPUXtensaState *env, uint8_t *mem_buf, int n)
1734
 
{
1735
 
    uint32_t tmp;
1736
 
    const XtensaGdbReg *reg = env->config->gdb_regmap.reg + n;
1737
 
 
1738
 
    if (n < 0 || n >= env->config->gdb_regmap.num_regs) {
1739
 
        return 0;
1740
 
    }
1741
 
 
1742
 
    tmp = ldl_p(mem_buf);
1743
 
 
1744
 
    switch (reg->type) {
1745
 
    case 9: /*pc*/
1746
 
        env->pc = tmp;
1747
 
        break;
1748
 
 
1749
 
    case 1: /*ar*/
1750
 
        env->phys_regs[(reg->targno & 0xff) % env->config->nareg] = tmp;
1751
 
        xtensa_sync_window_from_phys(env);
1752
 
        break;
1753
 
 
1754
 
    case 2: /*SR*/
1755
 
        env->sregs[reg->targno & 0xff] = tmp;
1756
 
        break;
1757
 
 
1758
 
    case 3: /*UR*/
1759
 
        env->uregs[reg->targno & 0xff] = tmp;
1760
 
        break;
1761
 
 
1762
 
    case 4: /*f*/
1763
 
        env->fregs[reg->targno & 0x0f] = make_float32(tmp);
1764
 
        break;
1765
 
 
1766
 
    case 8: /*a*/
1767
 
        env->regs[reg->targno & 0x0f] = tmp;
1768
 
        break;
1769
 
 
1770
 
    default:
1771
 
        qemu_log("%s to reg %d of unsupported type %d\n",
1772
 
                __func__, n, reg->type);
1773
 
        return 0;
1774
 
    }
1775
 
 
1776
 
    return 4;
1777
 
}
1778
 
#else
1779
 
 
1780
 
#define NUM_CORE_REGS 0
1781
 
 
1782
 
static int cpu_gdb_read_register(CPUArchState *env, uint8_t *mem_buf, int n)
1783
 
{
1784
 
    return 0;
1785
 
}
1786
 
 
1787
 
static int cpu_gdb_write_register(CPUArchState *env, uint8_t *mem_buf, int n)
1788
 
{
1789
 
    return 0;
1790
 
}
1791
 
 
1792
 
#endif
1793
 
 
1794
 
#if !defined(TARGET_XTENSA)
1795
 
static int num_g_regs = NUM_CORE_REGS;
1796
 
#endif
1797
 
 
1798
 
#ifdef GDB_CORE_XML
1799
488
/* Encode data using the encoding for 'x' packets.  */
1800
489
static int memtox(char *buf, const char *mem, int len)
1801
490
{
1817
506
    return p - buf;
1818
507
}
1819
508
 
1820
 
static const char *get_feature_xml(const char *p, const char **newp)
 
509
static const char *get_feature_xml(const char *p, const char **newp,
 
510
                                   CPUClass *cc)
1821
511
{
1822
512
    size_t len;
1823
513
    int i;
1834
524
        /* Generate the XML description for this CPU.  */
1835
525
        if (!target_xml[0]) {
1836
526
            GDBRegisterState *r;
 
527
            CPUState *cpu = first_cpu;
1837
528
 
1838
529
            snprintf(target_xml, sizeof(target_xml),
1839
530
                     "<?xml version=\"1.0\"?>"
1840
531
                     "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
1841
532
                     "<target>"
1842
533
                     "<xi:include href=\"%s\"/>",
1843
 
                     GDB_CORE_XML);
 
534
                     cc->gdb_core_xml_file);
1844
535
 
1845
 
            for (r = first_cpu->gdb_regs; r; r = r->next) {
 
536
            for (r = cpu->gdb_regs; r; r = r->next) {
1846
537
                pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
1847
538
                pstrcat(target_xml, sizeof(target_xml), r->xml);
1848
539
                pstrcat(target_xml, sizeof(target_xml), "\"/>");
1858
549
    }
1859
550
    return name ? xml_builtin[i][1] : NULL;
1860
551
}
1861
 
#endif
1862
552
 
1863
 
static int gdb_read_register(CPUArchState *env, uint8_t *mem_buf, int reg)
 
553
static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
1864
554
{
 
555
    CPUClass *cc = CPU_GET_CLASS(cpu);
 
556
    CPUArchState *env = cpu->env_ptr;
1865
557
    GDBRegisterState *r;
1866
558
 
1867
 
    if (reg < NUM_CORE_REGS)
1868
 
        return cpu_gdb_read_register(env, mem_buf, reg);
 
559
    if (reg < cc->gdb_num_core_regs) {
 
560
        return cc->gdb_read_register(cpu, mem_buf, reg);
 
561
    }
1869
562
 
1870
 
    for (r = env->gdb_regs; r; r = r->next) {
 
563
    for (r = cpu->gdb_regs; r; r = r->next) {
1871
564
        if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
1872
565
            return r->get_reg(env, mem_buf, reg - r->base_reg);
1873
566
        }
1875
568
    return 0;
1876
569
}
1877
570
 
1878
 
static int gdb_write_register(CPUArchState *env, uint8_t *mem_buf, int reg)
 
571
static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
1879
572
{
 
573
    CPUClass *cc = CPU_GET_CLASS(cpu);
 
574
    CPUArchState *env = cpu->env_ptr;
1880
575
    GDBRegisterState *r;
1881
576
 
1882
 
    if (reg < NUM_CORE_REGS)
1883
 
        return cpu_gdb_write_register(env, mem_buf, reg);
 
577
    if (reg < cc->gdb_num_core_regs) {
 
578
        return cc->gdb_write_register(cpu, mem_buf, reg);
 
579
    }
1884
580
 
1885
 
    for (r = env->gdb_regs; r; r = r->next) {
 
581
    for (r = cpu->gdb_regs; r; r = r->next) {
1886
582
        if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
1887
583
            return r->set_reg(env, mem_buf, reg - r->base_reg);
1888
584
        }
1890
586
    return 0;
1891
587
}
1892
588
 
1893
 
#if !defined(TARGET_XTENSA)
1894
589
/* Register a supplemental set of CPU registers.  If g_pos is nonzero it
1895
590
   specifies the first register number and these registers are included in
1896
591
   a standard "g" packet.  Direction is relative to gdb, i.e. get_reg is
1897
592
   gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
1898
593
 */
1899
594
 
1900
 
void gdb_register_coprocessor(CPUArchState * env,
1901
 
                             gdb_reg_cb get_reg, gdb_reg_cb set_reg,
1902
 
                             int num_regs, const char *xml, int g_pos)
 
595
void gdb_register_coprocessor(CPUState *cpu,
 
596
                              gdb_reg_cb get_reg, gdb_reg_cb set_reg,
 
597
                              int num_regs, const char *xml, int g_pos)
1903
598
{
1904
599
    GDBRegisterState *s;
1905
600
    GDBRegisterState **p;
1906
 
    static int last_reg = NUM_CORE_REGS;
1907
601
 
1908
 
    p = &env->gdb_regs;
 
602
    p = &cpu->gdb_regs;
1909
603
    while (*p) {
1910
604
        /* Check for duplicates.  */
1911
605
        if (strcmp((*p)->xml, xml) == 0)
1914
608
    }
1915
609
 
1916
610
    s = g_new0(GDBRegisterState, 1);
1917
 
    s->base_reg = last_reg;
 
611
    s->base_reg = cpu->gdb_num_regs;
1918
612
    s->num_regs = num_regs;
1919
613
    s->get_reg = get_reg;
1920
614
    s->set_reg = set_reg;
1921
615
    s->xml = xml;
1922
616
 
1923
617
    /* Add to end of list.  */
1924
 
    last_reg += num_regs;
 
618
    cpu->gdb_num_regs += num_regs;
1925
619
    *p = s;
1926
620
    if (g_pos) {
1927
621
        if (g_pos != s->base_reg) {
1928
622
            fprintf(stderr, "Error: Bad gdb register numbering for '%s'\n"
1929
623
                    "Expected %d got %d\n", xml, g_pos, s->base_reg);
1930
624
        } else {
1931
 
            num_g_regs = last_reg;
 
625
            cpu->gdb_num_g_regs = cpu->gdb_num_regs;
1932
626
        }
1933
627
    }
1934
628
}
1935
 
#endif
1936
629
 
1937
630
#ifndef CONFIG_USER_ONLY
1938
631
static const int xlat_gdb_type[] = {
1944
637
 
1945
638
static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
1946
639
{
 
640
    CPUState *cpu;
1947
641
    CPUArchState *env;
1948
642
    int err = 0;
1949
643
 
1950
 
    if (kvm_enabled())
 
644
    if (kvm_enabled()) {
1951
645
        return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
 
646
    }
1952
647
 
1953
648
    switch (type) {
1954
649
    case GDB_BREAKPOINT_SW:
1955
650
    case GDB_BREAKPOINT_HW:
1956
 
        for (env = first_cpu; env != NULL; env = env->next_cpu) {
 
651
        for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
 
652
            env = cpu->env_ptr;
1957
653
            err = cpu_breakpoint_insert(env, addr, BP_GDB, NULL);
1958
654
            if (err)
1959
655
                break;
1963
659
    case GDB_WATCHPOINT_WRITE:
1964
660
    case GDB_WATCHPOINT_READ:
1965
661
    case GDB_WATCHPOINT_ACCESS:
1966
 
        for (env = first_cpu; env != NULL; env = env->next_cpu) {
 
662
        for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
 
663
            env = cpu->env_ptr;
1967
664
            err = cpu_watchpoint_insert(env, addr, len, xlat_gdb_type[type],
1968
665
                                        NULL);
1969
666
            if (err)
1978
675
 
1979
676
static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
1980
677
{
 
678
    CPUState *cpu;
1981
679
    CPUArchState *env;
1982
680
    int err = 0;
1983
681
 
1984
 
    if (kvm_enabled())
 
682
    if (kvm_enabled()) {
1985
683
        return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
 
684
    }
1986
685
 
1987
686
    switch (type) {
1988
687
    case GDB_BREAKPOINT_SW:
1989
688
    case GDB_BREAKPOINT_HW:
1990
 
        for (env = first_cpu; env != NULL; env = env->next_cpu) {
 
689
        for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
 
690
            env = cpu->env_ptr;
1991
691
            err = cpu_breakpoint_remove(env, addr, BP_GDB);
1992
692
            if (err)
1993
693
                break;
1997
697
    case GDB_WATCHPOINT_WRITE:
1998
698
    case GDB_WATCHPOINT_READ:
1999
699
    case GDB_WATCHPOINT_ACCESS:
2000
 
        for (env = first_cpu; env != NULL; env = env->next_cpu) {
 
700
        for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
 
701
            env = cpu->env_ptr;
2001
702
            err = cpu_watchpoint_remove(env, addr, len, xlat_gdb_type[type]);
2002
703
            if (err)
2003
704
                break;
2011
712
 
2012
713
static void gdb_breakpoint_remove_all(void)
2013
714
{
 
715
    CPUState *cpu;
2014
716
    CPUArchState *env;
2015
717
 
2016
718
    if (kvm_enabled()) {
2018
720
        return;
2019
721
    }
2020
722
 
2021
 
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
 
723
    for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
 
724
        env = cpu->env_ptr;
2022
725
        cpu_breakpoint_remove_all(env, BP_GDB);
2023
726
#ifndef CONFIG_USER_ONLY
2024
727
        cpu_watchpoint_remove_all(env, BP_GDB);
2028
731
 
2029
732
static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
2030
733
{
2031
 
    cpu_synchronize_state(s->c_cpu);
2032
 
#if defined(TARGET_I386)
2033
 
    s->c_cpu->eip = pc;
2034
 
#elif defined (TARGET_PPC)
2035
 
    s->c_cpu->nip = pc;
2036
 
#elif defined (TARGET_SPARC)
2037
 
    s->c_cpu->pc = pc;
2038
 
    s->c_cpu->npc = pc + 4;
2039
 
#elif defined (TARGET_ARM)
2040
 
    s->c_cpu->regs[15] = pc;
2041
 
#elif defined (TARGET_SH4)
2042
 
    s->c_cpu->pc = pc;
2043
 
#elif defined (TARGET_MIPS)
2044
 
    s->c_cpu->active_tc.PC = pc & ~(target_ulong)1;
2045
 
    if (pc & 1) {
2046
 
        s->c_cpu->hflags |= MIPS_HFLAG_M16;
2047
 
    } else {
2048
 
        s->c_cpu->hflags &= ~(MIPS_HFLAG_M16);
 
734
    CPUState *cpu = s->c_cpu;
 
735
    CPUClass *cc = CPU_GET_CLASS(cpu);
 
736
 
 
737
    cpu_synchronize_state(cpu);
 
738
    if (cc->set_pc) {
 
739
        cc->set_pc(cpu, pc);
2049
740
    }
2050
 
#elif defined (TARGET_MICROBLAZE)
2051
 
    s->c_cpu->sregs[SR_PC] = pc;
2052
 
#elif defined(TARGET_OPENRISC)
2053
 
    s->c_cpu->pc = pc;
2054
 
#elif defined (TARGET_CRIS)
2055
 
    s->c_cpu->pc = pc;
2056
 
#elif defined (TARGET_ALPHA)
2057
 
    s->c_cpu->pc = pc;
2058
 
#elif defined (TARGET_S390X)
2059
 
    s->c_cpu->psw.addr = pc;
2060
 
#elif defined (TARGET_LM32)
2061
 
    s->c_cpu->pc = pc;
2062
 
#elif defined(TARGET_XTENSA)
2063
 
    s->c_cpu->pc = pc;
2064
 
#endif
2065
741
}
2066
742
 
2067
 
static CPUArchState *find_cpu(uint32_t thread_id)
 
743
static CPUState *find_cpu(uint32_t thread_id)
2068
744
{
2069
 
    CPUArchState *env;
2070
745
    CPUState *cpu;
2071
746
 
2072
 
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
2073
 
        cpu = ENV_GET_CPU(env);
 
747
    for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
2074
748
        if (cpu_index(cpu) == thread_id) {
2075
 
            return env;
 
749
            return cpu;
2076
750
        }
2077
751
    }
2078
752
 
2081
755
 
2082
756
static int gdb_handle_packet(GDBState *s, const char *line_buf)
2083
757
{
2084
 
    CPUArchState *env;
 
758
    CPUState *cpu;
 
759
    CPUClass *cc;
2085
760
    const char *p;
2086
761
    uint32_t thread;
2087
762
    int ch, reg_size, type, res;
2099
774
    case '?':
2100
775
        /* TODO: Make this return the correct value for user-mode.  */
2101
776
        snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
2102
 
                 cpu_index(ENV_GET_CPU(s->c_cpu)));
 
777
                 cpu_index(s->c_cpu));
2103
778
        put_packet(s, buf);
2104
779
        /* Remove all the breakpoints when this query is issued,
2105
780
         * because gdb is doing and initial connect and the state
2161
836
            }
2162
837
            if (res) {
2163
838
                if (res_thread != -1 && res_thread != 0) {
2164
 
                    env = find_cpu(res_thread);
2165
 
                    if (env == NULL) {
 
839
                    cpu = find_cpu(res_thread);
 
840
                    if (cpu == NULL) {
2166
841
                        put_packet(s, "E22");
2167
842
                        break;
2168
843
                    }
2169
 
                    s->c_cpu = env;
 
844
                    s->c_cpu = cpu;
2170
845
                }
2171
846
                if (res == 's') {
2172
847
                    cpu_single_step(s->c_cpu, sstep_flags);
2228
903
        break;
2229
904
    case 'g':
2230
905
        cpu_synchronize_state(s->g_cpu);
2231
 
        env = s->g_cpu;
2232
906
        len = 0;
2233
 
        for (addr = 0; addr < num_g_regs; addr++) {
 
907
        for (addr = 0; addr < s->g_cpu->gdb_num_g_regs; addr++) {
2234
908
            reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
2235
909
            len += reg_size;
2236
910
        }
2239
913
        break;
2240
914
    case 'G':
2241
915
        cpu_synchronize_state(s->g_cpu);
2242
 
        env = s->g_cpu;
2243
916
        registers = mem_buf;
2244
917
        len = strlen(p) / 2;
2245
918
        hextomem((uint8_t *)registers, p, len);
2246
 
        for (addr = 0; addr < num_g_regs && len > 0; addr++) {
 
919
        for (addr = 0; addr < s->g_cpu->gdb_num_g_regs && len > 0; addr++) {
2247
920
            reg_size = gdb_write_register(s->g_cpu, registers, addr);
2248
921
            len -= reg_size;
2249
922
            registers += reg_size;
2255
928
        if (*p == ',')
2256
929
            p++;
2257
930
        len = strtoull(p, NULL, 16);
2258
 
        if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, 0) != 0) {
 
931
        if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
2259
932
            put_packet (s, "E14");
2260
933
        } else {
2261
934
            memtohex(buf, mem_buf, len);
2270
943
        if (*p == ':')
2271
944
            p++;
2272
945
        hextomem(mem_buf, p, len);
2273
 
        if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, 1) != 0) {
 
946
        if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
 
947
                                   true) != 0) {
2274
948
            put_packet(s, "E14");
2275
949
        } else {
2276
950
            put_packet(s, "OK");
2329
1003
            put_packet(s, "OK");
2330
1004
            break;
2331
1005
        }
2332
 
        env = find_cpu(thread);
2333
 
        if (env == NULL) {
 
1006
        cpu = find_cpu(thread);
 
1007
        if (cpu == NULL) {
2334
1008
            put_packet(s, "E22");
2335
1009
            break;
2336
1010
        }
2337
1011
        switch (type) {
2338
1012
        case 'c':
2339
 
            s->c_cpu = env;
 
1013
            s->c_cpu = cpu;
2340
1014
            put_packet(s, "OK");
2341
1015
            break;
2342
1016
        case 'g':
2343
 
            s->g_cpu = env;
 
1017
            s->g_cpu = cpu;
2344
1018
            put_packet(s, "OK");
2345
1019
            break;
2346
1020
        default:
2350
1024
        break;
2351
1025
    case 'T':
2352
1026
        thread = strtoull(p, (char **)&p, 16);
2353
 
        env = find_cpu(thread);
 
1027
        cpu = find_cpu(thread);
2354
1028
 
2355
 
        if (env != NULL) {
 
1029
        if (cpu != NULL) {
2356
1030
            put_packet(s, "OK");
2357
1031
        } else {
2358
1032
            put_packet(s, "E22");
2394
1068
        } else if (strcmp(p,"sThreadInfo") == 0) {
2395
1069
        report_cpuinfo:
2396
1070
            if (s->query_cpu) {
2397
 
                snprintf(buf, sizeof(buf), "m%x",
2398
 
                         cpu_index(ENV_GET_CPU(s->query_cpu)));
 
1071
                snprintf(buf, sizeof(buf), "m%x", cpu_index(s->query_cpu));
2399
1072
                put_packet(s, buf);
2400
1073
                s->query_cpu = s->query_cpu->next_cpu;
2401
1074
            } else
2403
1076
            break;
2404
1077
        } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
2405
1078
            thread = strtoull(p+16, (char **)&p, 16);
2406
 
            env = find_cpu(thread);
2407
 
            if (env != NULL) {
2408
 
                CPUState *cpu = ENV_GET_CPU(env);
2409
 
                cpu_synchronize_state(env);
 
1079
            cpu = find_cpu(thread);
 
1080
            if (cpu != NULL) {
 
1081
                cpu_synchronize_state(cpu);
2410
1082
                len = snprintf((char *)mem_buf, sizeof(mem_buf),
2411
1083
                               "CPU#%d [%s]", cpu->cpu_index,
2412
1084
                               cpu->halted ? "halted " : "running");
2417
1089
        }
2418
1090
#ifdef CONFIG_USER_ONLY
2419
1091
        else if (strncmp(p, "Offsets", 7) == 0) {
2420
 
            TaskState *ts = s->c_cpu->opaque;
 
1092
            CPUArchState *env = s->c_cpu->env_ptr;
 
1093
            TaskState *ts = env->opaque;
2421
1094
 
2422
1095
            snprintf(buf, sizeof(buf),
2423
1096
                     "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
2446
1119
#endif /* !CONFIG_USER_ONLY */
2447
1120
        if (strncmp(p, "Supported", 9) == 0) {
2448
1121
            snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
2449
 
#ifdef GDB_CORE_XML
2450
 
            pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
2451
 
#endif
 
1122
            cc = CPU_GET_CLASS(first_cpu);
 
1123
            if (cc->gdb_core_xml_file != NULL) {
 
1124
                pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
 
1125
            }
2452
1126
            put_packet(s, buf);
2453
1127
            break;
2454
1128
        }
2455
 
#ifdef GDB_CORE_XML
2456
1129
        if (strncmp(p, "Xfer:features:read:", 19) == 0) {
2457
1130
            const char *xml;
2458
1131
            target_ulong total_len;
2459
1132
 
2460
 
            gdb_has_xml = 1;
 
1133
            cc = CPU_GET_CLASS(first_cpu);
 
1134
            if (cc->gdb_core_xml_file == NULL) {
 
1135
                goto unknown_command;
 
1136
            }
 
1137
 
 
1138
            gdb_has_xml = true;
2461
1139
            p += 19;
2462
 
            xml = get_feature_xml(p, &p);
 
1140
            xml = get_feature_xml(p, &p, cc);
2463
1141
            if (!xml) {
2464
1142
                snprintf(buf, sizeof(buf), "E00");
2465
1143
                put_packet(s, buf);
2491
1169
            put_packet_binary(s, buf, len + 1);
2492
1170
            break;
2493
1171
        }
2494
 
#endif
2495
1172
        /* Unrecognised 'q' command.  */
2496
1173
        goto unknown_command;
2497
1174
 
2505
1182
    return RS_IDLE;
2506
1183
}
2507
1184
 
2508
 
void gdb_set_stop_cpu(CPUArchState *env)
 
1185
void gdb_set_stop_cpu(CPUState *cpu)
2509
1186
{
2510
 
    gdbserver_state->c_cpu = env;
2511
 
    gdbserver_state->g_cpu = env;
 
1187
    gdbserver_state->c_cpu = cpu;
 
1188
    gdbserver_state->g_cpu = cpu;
2512
1189
}
2513
1190
 
2514
1191
#ifndef CONFIG_USER_ONLY
2515
1192
static void gdb_vm_state_change(void *opaque, int running, RunState state)
2516
1193
{
2517
1194
    GDBState *s = gdbserver_state;
2518
 
    CPUArchState *env = s->c_cpu;
2519
 
    CPUState *cpu = ENV_GET_CPU(env);
 
1195
    CPUArchState *env = s->c_cpu->env_ptr;
 
1196
    CPUState *cpu = s->c_cpu;
2520
1197
    char buf[256];
2521
1198
    const char *type;
2522
1199
    int ret;
2584
1261
    put_packet(s, buf);
2585
1262
 
2586
1263
    /* disable single step if it was enabled */
2587
 
    cpu_single_step(env, 0);
 
1264
    cpu_single_step(cpu, 0);
2588
1265
}
2589
1266
#endif
2590
1267
 
2775
1452
}
2776
1453
 
2777
1454
int
2778
 
gdb_handlesig (CPUArchState *env, int sig)
 
1455
gdb_handlesig(CPUState *cpu, int sig)
2779
1456
{
2780
 
  GDBState *s;
2781
 
  char buf[256];
2782
 
  int n;
2783
 
 
2784
 
  s = gdbserver_state;
2785
 
  if (gdbserver_fd < 0 || s->fd < 0)
 
1457
    CPUArchState *env = cpu->env_ptr;
 
1458
    GDBState *s;
 
1459
    char buf[256];
 
1460
    int n;
 
1461
 
 
1462
    s = gdbserver_state;
 
1463
    if (gdbserver_fd < 0 || s->fd < 0) {
 
1464
        return sig;
 
1465
    }
 
1466
 
 
1467
    /* disable single step if it was enabled */
 
1468
    cpu_single_step(cpu, 0);
 
1469
    tb_flush(env);
 
1470
 
 
1471
    if (sig != 0) {
 
1472
        snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
 
1473
        put_packet(s, buf);
 
1474
    }
 
1475
    /* put_packet() might have detected that the peer terminated the
 
1476
       connection.  */
 
1477
    if (s->fd < 0) {
 
1478
        return sig;
 
1479
    }
 
1480
 
 
1481
    sig = 0;
 
1482
    s->state = RS_IDLE;
 
1483
    s->running_state = 0;
 
1484
    while (s->running_state == 0) {
 
1485
        n = read(s->fd, buf, 256);
 
1486
        if (n > 0) {
 
1487
            int i;
 
1488
 
 
1489
            for (i = 0; i < n; i++) {
 
1490
                gdb_read_byte(s, buf[i]);
 
1491
            }
 
1492
        } else if (n == 0 || errno != EAGAIN) {
 
1493
            /* XXX: Connection closed.  Should probably wait for another
 
1494
               connection before continuing.  */
 
1495
            return sig;
 
1496
        }
 
1497
    }
 
1498
    sig = s->signal;
 
1499
    s->signal = 0;
2786
1500
    return sig;
2787
 
 
2788
 
  /* disable single step if it was enabled */
2789
 
  cpu_single_step(env, 0);
2790
 
  tb_flush(env);
2791
 
 
2792
 
  if (sig != 0)
2793
 
    {
2794
 
      snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb (sig));
2795
 
      put_packet(s, buf);
2796
 
    }
2797
 
  /* put_packet() might have detected that the peer terminated the 
2798
 
     connection.  */
2799
 
  if (s->fd < 0)
2800
 
      return sig;
2801
 
 
2802
 
  sig = 0;
2803
 
  s->state = RS_IDLE;
2804
 
  s->running_state = 0;
2805
 
  while (s->running_state == 0) {
2806
 
      n = read (s->fd, buf, 256);
2807
 
      if (n > 0)
2808
 
        {
2809
 
          int i;
2810
 
 
2811
 
          for (i = 0; i < n; i++)
2812
 
            gdb_read_byte (s, buf[i]);
2813
 
        }
2814
 
      else if (n == 0 || errno != EAGAIN)
2815
 
        {
2816
 
          /* XXX: Connection closed.  Should probably wait for another
2817
 
             connection before continuing.  */
2818
 
          return sig;
2819
 
        }
2820
 
  }
2821
 
  sig = s->signal;
2822
 
  s->signal = 0;
2823
 
  return sig;
2824
1501
}
2825
1502
 
2826
1503
/* Tell the remote gdb that the process has exited due to SIG.  */
2827
1504
void gdb_signalled(CPUArchState *env, int sig)
2828
1505
{
2829
 
  GDBState *s;
2830
 
  char buf[4];
2831
 
 
2832
 
  s = gdbserver_state;
2833
 
  if (gdbserver_fd < 0 || s->fd < 0)
2834
 
    return;
2835
 
 
2836
 
  snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb (sig));
2837
 
  put_packet(s, buf);
 
1506
    GDBState *s;
 
1507
    char buf[4];
 
1508
 
 
1509
    s = gdbserver_state;
 
1510
    if (gdbserver_fd < 0 || s->fd < 0) {
 
1511
        return;
 
1512
    }
 
1513
 
 
1514
    snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
 
1515
    put_packet(s, buf);
2838
1516
}
2839
1517
 
2840
1518
static void gdb_accept(void)
2865
1543
    s->c_cpu = first_cpu;
2866
1544
    s->g_cpu = first_cpu;
2867
1545
    s->fd = fd;
2868
 
    gdb_has_xml = 0;
 
1546
    gdb_has_xml = false;
2869
1547
 
2870
1548
    gdbserver_state = s;
2871
1549
 
2951
1629
    switch (event) {
2952
1630
    case CHR_EVENT_OPENED:
2953
1631
        vm_stop(RUN_STATE_PAUSED);
2954
 
        gdb_has_xml = 0;
 
1632
        gdb_has_xml = false;
2955
1633
        break;
2956
1634
    default:
2957
1635
        break;