xref: /qemu/target/i386/gdbstub.c (revision ac2fb86a)
1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth  * x86 gdb server stub
3fcf5ef2aSThomas Huth  *
4fcf5ef2aSThomas Huth  * Copyright (c) 2003-2005 Fabrice Bellard
5fcf5ef2aSThomas Huth  * Copyright (c) 2013 SUSE LINUX Products GmbH
6fcf5ef2aSThomas Huth  *
7fcf5ef2aSThomas Huth  * This library is free software; you can redistribute it and/or
8fcf5ef2aSThomas Huth  * modify it under the terms of the GNU Lesser General Public
9fcf5ef2aSThomas Huth  * License as published by the Free Software Foundation; either
10d9ff33adSChetan Pant  * version 2.1 of the License, or (at your option) any later version.
11fcf5ef2aSThomas Huth  *
12fcf5ef2aSThomas Huth  * This library is distributed in the hope that it will be useful,
13fcf5ef2aSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14fcf5ef2aSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15fcf5ef2aSThomas Huth  * Lesser General Public License for more details.
16fcf5ef2aSThomas Huth  *
17fcf5ef2aSThomas Huth  * You should have received a copy of the GNU Lesser General Public
18fcf5ef2aSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19fcf5ef2aSThomas Huth  */
20fcf5ef2aSThomas Huth #include "qemu/osdep.h"
21*ac2fb86aSIlya Leoshkevich #include "accel/tcg/vcpu-state.h"
22fcf5ef2aSThomas Huth #include "cpu.h"
23*ac2fb86aSIlya Leoshkevich #include "exec/gdbstub.h"
248b4d80bbSPhilippe Mathieu-Daudé #include "gdbstub/helpers.h"
25*ac2fb86aSIlya Leoshkevich #ifdef CONFIG_LINUX_USER
26*ac2fb86aSIlya Leoshkevich #include "linux-user/qemu.h"
27*ac2fb86aSIlya Leoshkevich #endif
28fcf5ef2aSThomas Huth 
29fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
30fcf5ef2aSThomas Huth static const int gpr_map[16] = {
31fcf5ef2aSThomas Huth     R_EAX, R_EBX, R_ECX, R_EDX, R_ESI, R_EDI, R_EBP, R_ESP,
32fcf5ef2aSThomas Huth     8, 9, 10, 11, 12, 13, 14, 15
33fcf5ef2aSThomas Huth };
34fcf5ef2aSThomas Huth #else
35fcf5ef2aSThomas Huth #define gpr_map gpr_map32
36fcf5ef2aSThomas Huth #endif
37fcf5ef2aSThomas Huth static const int gpr_map32[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
38fcf5ef2aSThomas Huth 
397b0f97baSDoug Gale /*
407b0f97baSDoug Gale  * Keep these in sync with assignment to
417b0f97baSDoug Gale  * gdb_num_core_regs in target/i386/cpu.c
427b0f97baSDoug Gale  * and with the machine description
437b0f97baSDoug Gale  */
447b0f97baSDoug Gale 
457b0f97baSDoug Gale /*
467b0f97baSDoug Gale  * SEG: 6 segments, plus fs_base, gs_base, kernel_gs_base
477b0f97baSDoug Gale  */
487b0f97baSDoug Gale 
497b0f97baSDoug Gale /*
507b0f97baSDoug Gale  * general regs ----->  8 or 16
517b0f97baSDoug Gale  */
527b0f97baSDoug Gale #define IDX_NB_IP       1
537b0f97baSDoug Gale #define IDX_NB_FLAGS    1
547b0f97baSDoug Gale #define IDX_NB_SEG      (6 + 3)
557b0f97baSDoug Gale #define IDX_NB_CTL      6
567b0f97baSDoug Gale #define IDX_NB_FP       16
577b0f97baSDoug Gale /*
587b0f97baSDoug Gale  * fpu regs ----------> 8 or 16
597b0f97baSDoug Gale  */
607b0f97baSDoug Gale #define IDX_NB_MXCSR    1
617b0f97baSDoug Gale /*
627b0f97baSDoug Gale  *          total ----> 8+1+1+9+6+16+8+1=50 or 16+1+1+9+6+16+16+1=66
637b0f97baSDoug Gale  */
647b0f97baSDoug Gale 
65fcf5ef2aSThomas Huth #define IDX_IP_REG      CPU_NB_REGS
667b0f97baSDoug Gale #define IDX_FLAGS_REG   (IDX_IP_REG + IDX_NB_IP)
677b0f97baSDoug Gale #define IDX_SEG_REGS    (IDX_FLAGS_REG + IDX_NB_FLAGS)
687b0f97baSDoug Gale #define IDX_CTL_REGS    (IDX_SEG_REGS + IDX_NB_SEG)
697b0f97baSDoug Gale #define IDX_FP_REGS     (IDX_CTL_REGS + IDX_NB_CTL)
707b0f97baSDoug Gale #define IDX_XMM_REGS    (IDX_FP_REGS + IDX_NB_FP)
71fcf5ef2aSThomas Huth #define IDX_MXCSR_REG   (IDX_XMM_REGS + CPU_NB_REGS)
72fcf5ef2aSThomas Huth 
737b0f97baSDoug Gale #define IDX_CTL_CR0_REG     (IDX_CTL_REGS + 0)
747b0f97baSDoug Gale #define IDX_CTL_CR2_REG     (IDX_CTL_REGS + 1)
757b0f97baSDoug Gale #define IDX_CTL_CR3_REG     (IDX_CTL_REGS + 2)
767b0f97baSDoug Gale #define IDX_CTL_CR4_REG     (IDX_CTL_REGS + 3)
777b0f97baSDoug Gale #define IDX_CTL_CR8_REG     (IDX_CTL_REGS + 4)
787b0f97baSDoug Gale #define IDX_CTL_EFER_REG    (IDX_CTL_REGS + 5)
797b0f97baSDoug Gale 
807b0f97baSDoug Gale #ifdef TARGET_X86_64
817b0f97baSDoug Gale #define GDB_FORCE_64 1
827b0f97baSDoug Gale #else
837b0f97baSDoug Gale #define GDB_FORCE_64 0
847b0f97baSDoug Gale #endif
857b0f97baSDoug Gale 
gdb_read_reg_cs64(uint32_t hflags,GByteArray * buf,target_ulong val)864d81e285SClaudio Fontana static int gdb_read_reg_cs64(uint32_t hflags, GByteArray *buf, target_ulong val)
874d81e285SClaudio Fontana {
884d81e285SClaudio Fontana     if ((hflags & HF_CS64_MASK) || GDB_FORCE_64) {
894d81e285SClaudio Fontana         return gdb_get_reg64(buf, val);
904d81e285SClaudio Fontana     }
914d81e285SClaudio Fontana     return gdb_get_reg32(buf, val);
924d81e285SClaudio Fontana }
934d81e285SClaudio Fontana 
gdb_write_reg_cs64(uint32_t hflags,uint8_t * buf,target_ulong * val)944d81e285SClaudio Fontana static int gdb_write_reg_cs64(uint32_t hflags, uint8_t *buf, target_ulong *val)
954d81e285SClaudio Fontana {
964d81e285SClaudio Fontana     if (hflags & HF_CS64_MASK) {
974d81e285SClaudio Fontana         *val = ldq_p(buf);
984d81e285SClaudio Fontana         return 8;
994d81e285SClaudio Fontana     }
1004d81e285SClaudio Fontana     *val = ldl_p(buf);
1014d81e285SClaudio Fontana     return 4;
1024d81e285SClaudio Fontana }
1037b0f97baSDoug Gale 
gdb_get_reg(CPUX86State * env,GByteArray * mem_buf,target_ulong val)104e7a4427aSIlya Leoshkevich static int gdb_get_reg(CPUX86State *env, GByteArray *mem_buf, target_ulong val)
105e7a4427aSIlya Leoshkevich {
106e7a4427aSIlya Leoshkevich     if (TARGET_LONG_BITS == 64) {
107e7a4427aSIlya Leoshkevich         if (env->hflags & HF_CS64_MASK) {
108e7a4427aSIlya Leoshkevich             return gdb_get_reg64(mem_buf, val);
109e7a4427aSIlya Leoshkevich         } else {
110e7a4427aSIlya Leoshkevich             return gdb_get_reg64(mem_buf, val & 0xffffffffUL);
111e7a4427aSIlya Leoshkevich         }
112e7a4427aSIlya Leoshkevich     } else {
113e7a4427aSIlya Leoshkevich         return gdb_get_reg32(mem_buf, val);
114e7a4427aSIlya Leoshkevich     }
115e7a4427aSIlya Leoshkevich }
116e7a4427aSIlya Leoshkevich 
x86_cpu_gdb_read_register(CPUState * cs,GByteArray * mem_buf,int n)117a010bdbeSAlex Bennée int x86_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
118fcf5ef2aSThomas Huth {
119fcf5ef2aSThomas Huth     X86CPU *cpu = X86_CPU(cs);
120fcf5ef2aSThomas Huth     CPUX86State *env = &cpu->env;
121fcf5ef2aSThomas Huth 
1227b0f97baSDoug Gale     uint64_t tpr;
1237b0f97baSDoug Gale 
124e3592bc9SDoug Evans     /* N.B. GDB can't deal with changes in registers or sizes in the middle
125e3592bc9SDoug Evans        of a session. So if we're in 32-bit mode on a 64-bit cpu, still act
126e3592bc9SDoug Evans        as if we're on a 64-bit cpu. */
127e3592bc9SDoug Evans 
128fcf5ef2aSThomas Huth     if (n < CPU_NB_REGS) {
129e3592bc9SDoug Evans         if (TARGET_LONG_BITS == 64) {
130e3592bc9SDoug Evans             if (env->hflags & HF_CS64_MASK) {
131fcf5ef2aSThomas Huth                 return gdb_get_reg64(mem_buf, env->regs[gpr_map[n]]);
132fcf5ef2aSThomas Huth             } else if (n < CPU_NB_REGS32) {
133e3592bc9SDoug Evans                 return gdb_get_reg64(mem_buf,
134e3592bc9SDoug Evans                                      env->regs[gpr_map[n]] & 0xffffffffUL);
135e3592bc9SDoug Evans             } else {
136b7b8756aSAlex Bennée                 return gdb_get_regl(mem_buf, 0);
137e3592bc9SDoug Evans             }
138e3592bc9SDoug Evans         } else {
139fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, env->regs[gpr_map32[n]]);
140fcf5ef2aSThomas Huth         }
141fcf5ef2aSThomas Huth     } else if (n >= IDX_FP_REGS && n < IDX_FP_REGS + 8) {
14249be78caSTaiseiIto         int st_index = n - IDX_FP_REGS;
14349be78caSTaiseiIto         int r_index = (st_index + env->fpstt) % 8;
14449be78caSTaiseiIto         floatx80 *fp = &env->fpregs[r_index].d;
145b7b8756aSAlex Bennée         int len = gdb_get_reg64(mem_buf, cpu_to_le64(fp->low));
146bbc40fefSPeter Xu         len += gdb_get_reg16(mem_buf, cpu_to_le16(fp->high));
147b7b8756aSAlex Bennée         return len;
148fcf5ef2aSThomas Huth     } else if (n >= IDX_XMM_REGS && n < IDX_XMM_REGS + CPU_NB_REGS) {
149fcf5ef2aSThomas Huth         n -= IDX_XMM_REGS;
150e3592bc9SDoug Evans         if (n < CPU_NB_REGS32 || TARGET_LONG_BITS == 64) {
151b7b8756aSAlex Bennée             return gdb_get_reg128(mem_buf,
152e618e1f9SAlex Bennée                                   env->xmm_regs[n].ZMM_Q(1),
153e618e1f9SAlex Bennée                                   env->xmm_regs[n].ZMM_Q(0));
154fcf5ef2aSThomas Huth         }
155fcf5ef2aSThomas Huth     } else {
156fcf5ef2aSThomas Huth         switch (n) {
157fcf5ef2aSThomas Huth         case IDX_IP_REG:
158e7a4427aSIlya Leoshkevich             return gdb_get_reg(env, mem_buf, env->eip);
159fcf5ef2aSThomas Huth         case IDX_FLAGS_REG:
160fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, env->eflags);
161fcf5ef2aSThomas Huth 
162fcf5ef2aSThomas Huth         case IDX_SEG_REGS:
163fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, env->segs[R_CS].selector);
164fcf5ef2aSThomas Huth         case IDX_SEG_REGS + 1:
165fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, env->segs[R_SS].selector);
166fcf5ef2aSThomas Huth         case IDX_SEG_REGS + 2:
167fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, env->segs[R_DS].selector);
168fcf5ef2aSThomas Huth         case IDX_SEG_REGS + 3:
169fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, env->segs[R_ES].selector);
170fcf5ef2aSThomas Huth         case IDX_SEG_REGS + 4:
171fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, env->segs[R_FS].selector);
172fcf5ef2aSThomas Huth         case IDX_SEG_REGS + 5:
173fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, env->segs[R_GS].selector);
1747b0f97baSDoug Gale         case IDX_SEG_REGS + 6:
1754d81e285SClaudio Fontana             return gdb_read_reg_cs64(env->hflags, mem_buf, env->segs[R_FS].base);
1767b0f97baSDoug Gale         case IDX_SEG_REGS + 7:
1774d81e285SClaudio Fontana             return gdb_read_reg_cs64(env->hflags, mem_buf, env->segs[R_GS].base);
1787b0f97baSDoug Gale 
1797b0f97baSDoug Gale         case IDX_SEG_REGS + 8:
1807b0f97baSDoug Gale #ifdef TARGET_X86_64
1814d81e285SClaudio Fontana             return gdb_read_reg_cs64(env->hflags, mem_buf, env->kernelgsbase);
1827b0f97baSDoug Gale #else
1837b0f97baSDoug Gale             return gdb_get_reg32(mem_buf, 0);
1847b0f97baSDoug Gale #endif
1857b0f97baSDoug Gale 
186fcf5ef2aSThomas Huth         case IDX_FP_REGS + 8:
187fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, env->fpuc);
188fcf5ef2aSThomas Huth         case IDX_FP_REGS + 9:
189fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, (env->fpus & ~0x3800) |
190fcf5ef2aSThomas Huth                                           (env->fpstt & 0x7) << 11);
191fcf5ef2aSThomas Huth         case IDX_FP_REGS + 10:
192fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, 0); /* ftag */
193fcf5ef2aSThomas Huth         case IDX_FP_REGS + 11:
194fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, 0); /* fiseg */
195fcf5ef2aSThomas Huth         case IDX_FP_REGS + 12:
196fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, 0); /* fioff */
197fcf5ef2aSThomas Huth         case IDX_FP_REGS + 13:
198fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, 0); /* foseg */
199fcf5ef2aSThomas Huth         case IDX_FP_REGS + 14:
200fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, 0); /* fooff */
201fcf5ef2aSThomas Huth         case IDX_FP_REGS + 15:
202fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, 0); /* fop */
203fcf5ef2aSThomas Huth 
204fcf5ef2aSThomas Huth         case IDX_MXCSR_REG:
205418b0f93SJoseph Myers             update_mxcsr_from_sse_status(env);
206fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, env->mxcsr);
2077b0f97baSDoug Gale 
2087b0f97baSDoug Gale         case IDX_CTL_CR0_REG:
2094d81e285SClaudio Fontana             return gdb_read_reg_cs64(env->hflags, mem_buf, env->cr[0]);
2107b0f97baSDoug Gale         case IDX_CTL_CR2_REG:
2114d81e285SClaudio Fontana             return gdb_read_reg_cs64(env->hflags, mem_buf, env->cr[2]);
2127b0f97baSDoug Gale         case IDX_CTL_CR3_REG:
2134d81e285SClaudio Fontana             return gdb_read_reg_cs64(env->hflags, mem_buf, env->cr[3]);
2147b0f97baSDoug Gale         case IDX_CTL_CR4_REG:
2154d81e285SClaudio Fontana             return gdb_read_reg_cs64(env->hflags, mem_buf, env->cr[4]);
2167b0f97baSDoug Gale         case IDX_CTL_CR8_REG:
2174d81e285SClaudio Fontana #ifndef CONFIG_USER_ONLY
2187b0f97baSDoug Gale             tpr = cpu_get_apic_tpr(cpu->apic_state);
2197b0f97baSDoug Gale #else
2207b0f97baSDoug Gale             tpr = 0;
2217b0f97baSDoug Gale #endif
2224d81e285SClaudio Fontana             return gdb_read_reg_cs64(env->hflags, mem_buf, tpr);
2237b0f97baSDoug Gale 
2247b0f97baSDoug Gale         case IDX_CTL_EFER_REG:
2254d81e285SClaudio Fontana             return gdb_read_reg_cs64(env->hflags, mem_buf, env->efer);
226fcf5ef2aSThomas Huth         }
227fcf5ef2aSThomas Huth     }
228fcf5ef2aSThomas Huth     return 0;
229fcf5ef2aSThomas Huth }
230fcf5ef2aSThomas Huth 
x86_cpu_gdb_load_seg(X86CPU * cpu,X86Seg sreg,uint8_t * mem_buf)231c117e5b1SPhilippe Mathieu-Daudé static int x86_cpu_gdb_load_seg(X86CPU *cpu, X86Seg sreg, uint8_t *mem_buf)
232fcf5ef2aSThomas Huth {
233fcf5ef2aSThomas Huth     CPUX86State *env = &cpu->env;
234fcf5ef2aSThomas Huth     uint16_t selector = ldl_p(mem_buf);
235fcf5ef2aSThomas Huth 
236fcf5ef2aSThomas Huth     if (selector != env->segs[sreg].selector) {
237fcf5ef2aSThomas Huth #if defined(CONFIG_USER_ONLY)
238fcf5ef2aSThomas Huth         cpu_x86_load_seg(env, sreg, selector);
239fcf5ef2aSThomas Huth #else
240fcf5ef2aSThomas Huth         unsigned int limit, flags;
241fcf5ef2aSThomas Huth         target_ulong base;
242fcf5ef2aSThomas Huth 
243fcf5ef2aSThomas Huth         if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) {
244fcf5ef2aSThomas Huth             int dpl = (env->eflags & VM_MASK) ? 3 : 0;
245fcf5ef2aSThomas Huth             base = selector << 4;
246fcf5ef2aSThomas Huth             limit = 0xffff;
247fcf5ef2aSThomas Huth             flags = DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
248fcf5ef2aSThomas Huth                     DESC_A_MASK | (dpl << DESC_DPL_SHIFT);
249fcf5ef2aSThomas Huth         } else {
250fcf5ef2aSThomas Huth             if (!cpu_x86_get_descr_debug(env, selector, &base, &limit,
251fcf5ef2aSThomas Huth                                          &flags)) {
252fcf5ef2aSThomas Huth                 return 4;
253fcf5ef2aSThomas Huth             }
254fcf5ef2aSThomas Huth         }
255fcf5ef2aSThomas Huth         cpu_x86_load_seg_cache(env, sreg, selector, base, limit, flags);
256fcf5ef2aSThomas Huth #endif
257fcf5ef2aSThomas Huth     }
258fcf5ef2aSThomas Huth     return 4;
259fcf5ef2aSThomas Huth }
260fcf5ef2aSThomas Huth 
gdb_write_reg(CPUX86State * env,uint8_t * mem_buf,target_ulong * val)261e7a4427aSIlya Leoshkevich static int gdb_write_reg(CPUX86State *env, uint8_t *mem_buf, target_ulong *val)
262e7a4427aSIlya Leoshkevich {
263e7a4427aSIlya Leoshkevich     if (TARGET_LONG_BITS == 64) {
264e7a4427aSIlya Leoshkevich         if (env->hflags & HF_CS64_MASK) {
265e7a4427aSIlya Leoshkevich             *val = ldq_p(mem_buf);
266e7a4427aSIlya Leoshkevich         } else {
267e7a4427aSIlya Leoshkevich             *val = ldq_p(mem_buf) & 0xffffffffUL;
268e7a4427aSIlya Leoshkevich         }
269e7a4427aSIlya Leoshkevich         return 8;
270e7a4427aSIlya Leoshkevich     } else {
271e7a4427aSIlya Leoshkevich         *val = (uint32_t)ldl_p(mem_buf);
272e7a4427aSIlya Leoshkevich         return 4;
273e7a4427aSIlya Leoshkevich     }
274e7a4427aSIlya Leoshkevich }
275e7a4427aSIlya Leoshkevich 
x86_cpu_gdb_write_register(CPUState * cs,uint8_t * mem_buf,int n)276fcf5ef2aSThomas Huth int x86_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
277fcf5ef2aSThomas Huth {
278fcf5ef2aSThomas Huth     X86CPU *cpu = X86_CPU(cs);
279fcf5ef2aSThomas Huth     CPUX86State *env = &cpu->env;
2804d81e285SClaudio Fontana     target_ulong tmp;
2814d81e285SClaudio Fontana     int len;
282fcf5ef2aSThomas Huth 
283e3592bc9SDoug Evans     /* N.B. GDB can't deal with changes in registers or sizes in the middle
284e3592bc9SDoug Evans        of a session. So if we're in 32-bit mode on a 64-bit cpu, still act
285e3592bc9SDoug Evans        as if we're on a 64-bit cpu. */
286e3592bc9SDoug Evans 
287fcf5ef2aSThomas Huth     if (n < CPU_NB_REGS) {
288e3592bc9SDoug Evans         if (TARGET_LONG_BITS == 64) {
289e3592bc9SDoug Evans             if (env->hflags & HF_CS64_MASK) {
290fcf5ef2aSThomas Huth                 env->regs[gpr_map[n]] = ldtul_p(mem_buf);
291e3592bc9SDoug Evans             } else if (n < CPU_NB_REGS32) {
292e3592bc9SDoug Evans                 env->regs[gpr_map[n]] = ldtul_p(mem_buf) & 0xffffffffUL;
293e3592bc9SDoug Evans             }
294fcf5ef2aSThomas Huth             return sizeof(target_ulong);
295fcf5ef2aSThomas Huth         } else if (n < CPU_NB_REGS32) {
296fcf5ef2aSThomas Huth             n = gpr_map32[n];
297fcf5ef2aSThomas Huth             env->regs[n] &= ~0xffffffffUL;
298fcf5ef2aSThomas Huth             env->regs[n] |= (uint32_t)ldl_p(mem_buf);
299fcf5ef2aSThomas Huth             return 4;
300fcf5ef2aSThomas Huth         }
301fcf5ef2aSThomas Huth     } else if (n >= IDX_FP_REGS && n < IDX_FP_REGS + 8) {
302b7b8756aSAlex Bennée         floatx80 *fp = (floatx80 *) &env->fpregs[n - IDX_FP_REGS];
303b7b8756aSAlex Bennée         fp->low = le64_to_cpu(* (uint64_t *) mem_buf);
304b7b8756aSAlex Bennée         fp->high = le16_to_cpu(* (uint16_t *) (mem_buf + 8));
305fcf5ef2aSThomas Huth         return 10;
306fcf5ef2aSThomas Huth     } else if (n >= IDX_XMM_REGS && n < IDX_XMM_REGS + CPU_NB_REGS) {
307fcf5ef2aSThomas Huth         n -= IDX_XMM_REGS;
308e3592bc9SDoug Evans         if (n < CPU_NB_REGS32 || TARGET_LONG_BITS == 64) {
309fcf5ef2aSThomas Huth             env->xmm_regs[n].ZMM_Q(0) = ldq_p(mem_buf);
310fcf5ef2aSThomas Huth             env->xmm_regs[n].ZMM_Q(1) = ldq_p(mem_buf + 8);
311fcf5ef2aSThomas Huth             return 16;
312fcf5ef2aSThomas Huth         }
313fcf5ef2aSThomas Huth     } else {
314fcf5ef2aSThomas Huth         switch (n) {
315fcf5ef2aSThomas Huth         case IDX_IP_REG:
316e7a4427aSIlya Leoshkevich             return gdb_write_reg(env, mem_buf, &env->eip);
317fcf5ef2aSThomas Huth         case IDX_FLAGS_REG:
318fcf5ef2aSThomas Huth             env->eflags = ldl_p(mem_buf);
319fcf5ef2aSThomas Huth             return 4;
320fcf5ef2aSThomas Huth 
321fcf5ef2aSThomas Huth         case IDX_SEG_REGS:
322fcf5ef2aSThomas Huth             return x86_cpu_gdb_load_seg(cpu, R_CS, mem_buf);
323fcf5ef2aSThomas Huth         case IDX_SEG_REGS + 1:
324fcf5ef2aSThomas Huth             return x86_cpu_gdb_load_seg(cpu, R_SS, mem_buf);
325fcf5ef2aSThomas Huth         case IDX_SEG_REGS + 2:
326fcf5ef2aSThomas Huth             return x86_cpu_gdb_load_seg(cpu, R_DS, mem_buf);
327fcf5ef2aSThomas Huth         case IDX_SEG_REGS + 3:
328fcf5ef2aSThomas Huth             return x86_cpu_gdb_load_seg(cpu, R_ES, mem_buf);
329fcf5ef2aSThomas Huth         case IDX_SEG_REGS + 4:
330fcf5ef2aSThomas Huth             return x86_cpu_gdb_load_seg(cpu, R_FS, mem_buf);
331fcf5ef2aSThomas Huth         case IDX_SEG_REGS + 5:
332fcf5ef2aSThomas Huth             return x86_cpu_gdb_load_seg(cpu, R_GS, mem_buf);
3337b0f97baSDoug Gale         case IDX_SEG_REGS + 6:
3344d81e285SClaudio Fontana             return gdb_write_reg_cs64(env->hflags, mem_buf, &env->segs[R_FS].base);
3357b0f97baSDoug Gale         case IDX_SEG_REGS + 7:
3364d81e285SClaudio Fontana             return gdb_write_reg_cs64(env->hflags, mem_buf, &env->segs[R_GS].base);
3377b0f97baSDoug Gale         case IDX_SEG_REGS + 8:
3385a07192aSmkdolata@us.ibm.com #ifdef TARGET_X86_64
3394d81e285SClaudio Fontana             return gdb_write_reg_cs64(env->hflags, mem_buf, &env->kernelgsbase);
3407b0f97baSDoug Gale #endif
3415a07192aSmkdolata@us.ibm.com             return 4;
3427b0f97baSDoug Gale 
343fcf5ef2aSThomas Huth         case IDX_FP_REGS + 8:
344fcf5ef2aSThomas Huth             cpu_set_fpuc(env, ldl_p(mem_buf));
345fcf5ef2aSThomas Huth             return 4;
346fcf5ef2aSThomas Huth         case IDX_FP_REGS + 9:
347fcf5ef2aSThomas Huth             tmp = ldl_p(mem_buf);
348fcf5ef2aSThomas Huth             env->fpstt = (tmp >> 11) & 7;
349fcf5ef2aSThomas Huth             env->fpus = tmp & ~0x3800;
350fcf5ef2aSThomas Huth             return 4;
351fcf5ef2aSThomas Huth         case IDX_FP_REGS + 10: /* ftag */
352fcf5ef2aSThomas Huth             return 4;
353fcf5ef2aSThomas Huth         case IDX_FP_REGS + 11: /* fiseg */
354fcf5ef2aSThomas Huth             return 4;
355fcf5ef2aSThomas Huth         case IDX_FP_REGS + 12: /* fioff */
356fcf5ef2aSThomas Huth             return 4;
357fcf5ef2aSThomas Huth         case IDX_FP_REGS + 13: /* foseg */
358fcf5ef2aSThomas Huth             return 4;
359fcf5ef2aSThomas Huth         case IDX_FP_REGS + 14: /* fooff */
360fcf5ef2aSThomas Huth             return 4;
361fcf5ef2aSThomas Huth         case IDX_FP_REGS + 15: /* fop */
362fcf5ef2aSThomas Huth             return 4;
363fcf5ef2aSThomas Huth 
364fcf5ef2aSThomas Huth         case IDX_MXCSR_REG:
365fcf5ef2aSThomas Huth             cpu_set_mxcsr(env, ldl_p(mem_buf));
366fcf5ef2aSThomas Huth             return 4;
3677b0f97baSDoug Gale 
3687b0f97baSDoug Gale         case IDX_CTL_CR0_REG:
3694d81e285SClaudio Fontana             len = gdb_write_reg_cs64(env->hflags, mem_buf, &tmp);
3701852f094SClaudio Fontana #ifndef CONFIG_USER_ONLY
3714d81e285SClaudio Fontana             cpu_x86_update_cr0(env, tmp);
3721852f094SClaudio Fontana #endif
3734d81e285SClaudio Fontana             return len;
3747b0f97baSDoug Gale 
3757b0f97baSDoug Gale         case IDX_CTL_CR2_REG:
3764d81e285SClaudio Fontana             len = gdb_write_reg_cs64(env->hflags, mem_buf, &tmp);
3771852f094SClaudio Fontana #ifndef CONFIG_USER_ONLY
3784d81e285SClaudio Fontana             env->cr[2] = tmp;
3791852f094SClaudio Fontana #endif
3804d81e285SClaudio Fontana             return len;
3817b0f97baSDoug Gale 
3827b0f97baSDoug Gale         case IDX_CTL_CR3_REG:
3834d81e285SClaudio Fontana             len = gdb_write_reg_cs64(env->hflags, mem_buf, &tmp);
3841852f094SClaudio Fontana #ifndef CONFIG_USER_ONLY
3854d81e285SClaudio Fontana             cpu_x86_update_cr3(env, tmp);
3861852f094SClaudio Fontana #endif
3874d81e285SClaudio Fontana             return len;
3887b0f97baSDoug Gale 
3897b0f97baSDoug Gale         case IDX_CTL_CR4_REG:
3904d81e285SClaudio Fontana             len = gdb_write_reg_cs64(env->hflags, mem_buf, &tmp);
3911852f094SClaudio Fontana #ifndef CONFIG_USER_ONLY
3924d81e285SClaudio Fontana             cpu_x86_update_cr4(env, tmp);
3931852f094SClaudio Fontana #endif
3944d81e285SClaudio Fontana             return len;
3957b0f97baSDoug Gale 
3967b0f97baSDoug Gale         case IDX_CTL_CR8_REG:
3974d81e285SClaudio Fontana             len = gdb_write_reg_cs64(env->hflags, mem_buf, &tmp);
3984d81e285SClaudio Fontana #ifndef CONFIG_USER_ONLY
3994d81e285SClaudio Fontana             cpu_set_apic_tpr(cpu->apic_state, tmp);
4007b0f97baSDoug Gale #endif
4014d81e285SClaudio Fontana             return len;
4027b0f97baSDoug Gale 
4037b0f97baSDoug Gale         case IDX_CTL_EFER_REG:
4044d81e285SClaudio Fontana             len = gdb_write_reg_cs64(env->hflags, mem_buf, &tmp);
4051852f094SClaudio Fontana #ifndef CONFIG_USER_ONLY
4064d81e285SClaudio Fontana             cpu_load_efer(env, tmp);
4071852f094SClaudio Fontana #endif
4084d81e285SClaudio Fontana             return len;
409fcf5ef2aSThomas Huth         }
410fcf5ef2aSThomas Huth     }
411fcf5ef2aSThomas Huth     /* Unrecognised register.  */
412fcf5ef2aSThomas Huth     return 0;
413fcf5ef2aSThomas Huth }
414*ac2fb86aSIlya Leoshkevich 
415*ac2fb86aSIlya Leoshkevich #ifdef CONFIG_LINUX_USER
416*ac2fb86aSIlya Leoshkevich 
417*ac2fb86aSIlya Leoshkevich #define IDX_ORIG_AX 0
418*ac2fb86aSIlya Leoshkevich 
x86_cpu_gdb_read_linux_register(CPUState * cs,GByteArray * mem_buf,int n)419*ac2fb86aSIlya Leoshkevich static int x86_cpu_gdb_read_linux_register(CPUState *cs, GByteArray *mem_buf,
420*ac2fb86aSIlya Leoshkevich                                            int n)
421*ac2fb86aSIlya Leoshkevich {
422*ac2fb86aSIlya Leoshkevich     X86CPU *cpu = X86_CPU(cs);
423*ac2fb86aSIlya Leoshkevich     CPUX86State *env = &cpu->env;
424*ac2fb86aSIlya Leoshkevich 
425*ac2fb86aSIlya Leoshkevich     switch (n) {
426*ac2fb86aSIlya Leoshkevich     case IDX_ORIG_AX:
427*ac2fb86aSIlya Leoshkevich         return gdb_get_reg(env, mem_buf, get_task_state(cs)->orig_ax);
428*ac2fb86aSIlya Leoshkevich     }
429*ac2fb86aSIlya Leoshkevich     return 0;
430*ac2fb86aSIlya Leoshkevich }
431*ac2fb86aSIlya Leoshkevich 
x86_cpu_gdb_write_linux_register(CPUState * cs,uint8_t * mem_buf,int n)432*ac2fb86aSIlya Leoshkevich static int x86_cpu_gdb_write_linux_register(CPUState *cs, uint8_t *mem_buf,
433*ac2fb86aSIlya Leoshkevich                                             int n)
434*ac2fb86aSIlya Leoshkevich {
435*ac2fb86aSIlya Leoshkevich     X86CPU *cpu = X86_CPU(cs);
436*ac2fb86aSIlya Leoshkevich     CPUX86State *env = &cpu->env;
437*ac2fb86aSIlya Leoshkevich 
438*ac2fb86aSIlya Leoshkevich     switch (n) {
439*ac2fb86aSIlya Leoshkevich     case IDX_ORIG_AX:
440*ac2fb86aSIlya Leoshkevich         return gdb_write_reg(env, mem_buf, &get_task_state(cs)->orig_ax);
441*ac2fb86aSIlya Leoshkevich     }
442*ac2fb86aSIlya Leoshkevich     return 0;
443*ac2fb86aSIlya Leoshkevich }
444*ac2fb86aSIlya Leoshkevich 
445*ac2fb86aSIlya Leoshkevich #endif
446*ac2fb86aSIlya Leoshkevich 
x86_cpu_gdb_init(CPUState * cs)447*ac2fb86aSIlya Leoshkevich void x86_cpu_gdb_init(CPUState *cs)
448*ac2fb86aSIlya Leoshkevich {
449*ac2fb86aSIlya Leoshkevich #ifdef CONFIG_LINUX_USER
450*ac2fb86aSIlya Leoshkevich     gdb_register_coprocessor(cs, x86_cpu_gdb_read_linux_register,
451*ac2fb86aSIlya Leoshkevich                              x86_cpu_gdb_write_linux_register,
452*ac2fb86aSIlya Leoshkevich #ifdef TARGET_X86_64
453*ac2fb86aSIlya Leoshkevich                              gdb_find_static_feature("i386-64bit-linux.xml"),
454*ac2fb86aSIlya Leoshkevich #else
455*ac2fb86aSIlya Leoshkevich                              gdb_find_static_feature("i386-32bit-linux.xml"),
456*ac2fb86aSIlya Leoshkevich #endif
457*ac2fb86aSIlya Leoshkevich                              0);
458*ac2fb86aSIlya Leoshkevich #endif
459*ac2fb86aSIlya Leoshkevich }
460