xref: /qemu/target/i386/gdbstub.c (revision 5a07192a)
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
10fcf5ef2aSThomas Huth  * version 2 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"
21fcf5ef2aSThomas Huth #include "cpu.h"
22fcf5ef2aSThomas Huth #include "exec/gdbstub.h"
23fcf5ef2aSThomas Huth 
24fcf5ef2aSThomas Huth #ifdef TARGET_X86_64
25fcf5ef2aSThomas Huth static const int gpr_map[16] = {
26fcf5ef2aSThomas Huth     R_EAX, R_EBX, R_ECX, R_EDX, R_ESI, R_EDI, R_EBP, R_ESP,
27fcf5ef2aSThomas Huth     8, 9, 10, 11, 12, 13, 14, 15
28fcf5ef2aSThomas Huth };
29fcf5ef2aSThomas Huth #else
30fcf5ef2aSThomas Huth #define gpr_map gpr_map32
31fcf5ef2aSThomas Huth #endif
32fcf5ef2aSThomas Huth static const int gpr_map32[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
33fcf5ef2aSThomas Huth 
347b0f97baSDoug Gale /*
357b0f97baSDoug Gale  * Keep these in sync with assignment to
367b0f97baSDoug Gale  * gdb_num_core_regs in target/i386/cpu.c
377b0f97baSDoug Gale  * and with the machine description
387b0f97baSDoug Gale  */
397b0f97baSDoug Gale 
407b0f97baSDoug Gale /*
417b0f97baSDoug Gale  * SEG: 6 segments, plus fs_base, gs_base, kernel_gs_base
427b0f97baSDoug Gale  */
437b0f97baSDoug Gale 
447b0f97baSDoug Gale /*
457b0f97baSDoug Gale  * general regs ----->  8 or 16
467b0f97baSDoug Gale  */
477b0f97baSDoug Gale #define IDX_NB_IP       1
487b0f97baSDoug Gale #define IDX_NB_FLAGS    1
497b0f97baSDoug Gale #define IDX_NB_SEG      (6 + 3)
507b0f97baSDoug Gale #define IDX_NB_CTL      6
517b0f97baSDoug Gale #define IDX_NB_FP       16
527b0f97baSDoug Gale /*
537b0f97baSDoug Gale  * fpu regs ----------> 8 or 16
547b0f97baSDoug Gale  */
557b0f97baSDoug Gale #define IDX_NB_MXCSR    1
567b0f97baSDoug Gale /*
577b0f97baSDoug Gale  *          total ----> 8+1+1+9+6+16+8+1=50 or 16+1+1+9+6+16+16+1=66
587b0f97baSDoug Gale  */
597b0f97baSDoug Gale 
60fcf5ef2aSThomas Huth #define IDX_IP_REG      CPU_NB_REGS
617b0f97baSDoug Gale #define IDX_FLAGS_REG   (IDX_IP_REG + IDX_NB_IP)
627b0f97baSDoug Gale #define IDX_SEG_REGS    (IDX_FLAGS_REG + IDX_NB_FLAGS)
637b0f97baSDoug Gale #define IDX_CTL_REGS    (IDX_SEG_REGS + IDX_NB_SEG)
647b0f97baSDoug Gale #define IDX_FP_REGS     (IDX_CTL_REGS + IDX_NB_CTL)
657b0f97baSDoug Gale #define IDX_XMM_REGS    (IDX_FP_REGS + IDX_NB_FP)
66fcf5ef2aSThomas Huth #define IDX_MXCSR_REG   (IDX_XMM_REGS + CPU_NB_REGS)
67fcf5ef2aSThomas Huth 
687b0f97baSDoug Gale #define IDX_CTL_CR0_REG     (IDX_CTL_REGS + 0)
697b0f97baSDoug Gale #define IDX_CTL_CR2_REG     (IDX_CTL_REGS + 1)
707b0f97baSDoug Gale #define IDX_CTL_CR3_REG     (IDX_CTL_REGS + 2)
717b0f97baSDoug Gale #define IDX_CTL_CR4_REG     (IDX_CTL_REGS + 3)
727b0f97baSDoug Gale #define IDX_CTL_CR8_REG     (IDX_CTL_REGS + 4)
737b0f97baSDoug Gale #define IDX_CTL_EFER_REG    (IDX_CTL_REGS + 5)
747b0f97baSDoug Gale 
757b0f97baSDoug Gale #ifdef TARGET_X86_64
767b0f97baSDoug Gale #define GDB_FORCE_64 1
777b0f97baSDoug Gale #else
787b0f97baSDoug Gale #define GDB_FORCE_64 0
797b0f97baSDoug Gale #endif
807b0f97baSDoug Gale 
817b0f97baSDoug Gale 
82fcf5ef2aSThomas Huth int x86_cpu_gdb_read_register(CPUState *cs, uint8_t *mem_buf, int n)
83fcf5ef2aSThomas Huth {
84fcf5ef2aSThomas Huth     X86CPU *cpu = X86_CPU(cs);
85fcf5ef2aSThomas Huth     CPUX86State *env = &cpu->env;
86fcf5ef2aSThomas Huth 
877b0f97baSDoug Gale     uint64_t tpr;
887b0f97baSDoug Gale 
89e3592bc9SDoug Evans     /* N.B. GDB can't deal with changes in registers or sizes in the middle
90e3592bc9SDoug Evans        of a session. So if we're in 32-bit mode on a 64-bit cpu, still act
91e3592bc9SDoug Evans        as if we're on a 64-bit cpu. */
92e3592bc9SDoug Evans 
93fcf5ef2aSThomas Huth     if (n < CPU_NB_REGS) {
94e3592bc9SDoug Evans         if (TARGET_LONG_BITS == 64) {
95e3592bc9SDoug Evans             if (env->hflags & HF_CS64_MASK) {
96fcf5ef2aSThomas Huth                 return gdb_get_reg64(mem_buf, env->regs[gpr_map[n]]);
97fcf5ef2aSThomas Huth             } else if (n < CPU_NB_REGS32) {
98e3592bc9SDoug Evans                 return gdb_get_reg64(mem_buf,
99e3592bc9SDoug Evans                                      env->regs[gpr_map[n]] & 0xffffffffUL);
100e3592bc9SDoug Evans             } else {
101e3592bc9SDoug Evans                 memset(mem_buf, 0, sizeof(target_ulong));
102e3592bc9SDoug Evans                 return sizeof(target_ulong);
103e3592bc9SDoug Evans             }
104e3592bc9SDoug Evans         } else {
105fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, env->regs[gpr_map32[n]]);
106fcf5ef2aSThomas Huth         }
107fcf5ef2aSThomas Huth     } else if (n >= IDX_FP_REGS && n < IDX_FP_REGS + 8) {
108fcf5ef2aSThomas Huth #ifdef USE_X86LDOUBLE
109fcf5ef2aSThomas Huth         /* FIXME: byteswap float values - after fixing fpregs layout. */
110fcf5ef2aSThomas Huth         memcpy(mem_buf, &env->fpregs[n - IDX_FP_REGS], 10);
111fcf5ef2aSThomas Huth #else
112fcf5ef2aSThomas Huth         memset(mem_buf, 0, 10);
113fcf5ef2aSThomas Huth #endif
114fcf5ef2aSThomas Huth         return 10;
115fcf5ef2aSThomas Huth     } else if (n >= IDX_XMM_REGS && n < IDX_XMM_REGS + CPU_NB_REGS) {
116fcf5ef2aSThomas Huth         n -= IDX_XMM_REGS;
117e3592bc9SDoug Evans         if (n < CPU_NB_REGS32 || TARGET_LONG_BITS == 64) {
118fcf5ef2aSThomas Huth             stq_p(mem_buf, env->xmm_regs[n].ZMM_Q(0));
119fcf5ef2aSThomas Huth             stq_p(mem_buf + 8, env->xmm_regs[n].ZMM_Q(1));
120fcf5ef2aSThomas Huth             return 16;
121fcf5ef2aSThomas Huth         }
122fcf5ef2aSThomas Huth     } else {
123fcf5ef2aSThomas Huth         switch (n) {
124fcf5ef2aSThomas Huth         case IDX_IP_REG:
125e3592bc9SDoug Evans             if (TARGET_LONG_BITS == 64) {
126e3592bc9SDoug Evans                 if (env->hflags & HF_CS64_MASK) {
127fcf5ef2aSThomas Huth                     return gdb_get_reg64(mem_buf, env->eip);
128fcf5ef2aSThomas Huth                 } else {
129e3592bc9SDoug Evans                     return gdb_get_reg64(mem_buf, env->eip & 0xffffffffUL);
130e3592bc9SDoug Evans                 }
131e3592bc9SDoug Evans             } else {
132fcf5ef2aSThomas Huth                 return gdb_get_reg32(mem_buf, env->eip);
133fcf5ef2aSThomas Huth             }
134fcf5ef2aSThomas Huth         case IDX_FLAGS_REG:
135fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, env->eflags);
136fcf5ef2aSThomas Huth 
137fcf5ef2aSThomas Huth         case IDX_SEG_REGS:
138fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, env->segs[R_CS].selector);
139fcf5ef2aSThomas Huth         case IDX_SEG_REGS + 1:
140fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, env->segs[R_SS].selector);
141fcf5ef2aSThomas Huth         case IDX_SEG_REGS + 2:
142fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, env->segs[R_DS].selector);
143fcf5ef2aSThomas Huth         case IDX_SEG_REGS + 3:
144fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, env->segs[R_ES].selector);
145fcf5ef2aSThomas Huth         case IDX_SEG_REGS + 4:
146fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, env->segs[R_FS].selector);
147fcf5ef2aSThomas Huth         case IDX_SEG_REGS + 5:
148fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, env->segs[R_GS].selector);
149fcf5ef2aSThomas Huth 
1507b0f97baSDoug Gale         case IDX_SEG_REGS + 6:
1517b0f97baSDoug Gale             if ((env->hflags & HF_CS64_MASK) || GDB_FORCE_64) {
1527b0f97baSDoug Gale                 return gdb_get_reg64(mem_buf, env->segs[R_FS].base);
1537b0f97baSDoug Gale             }
1547b0f97baSDoug Gale             return gdb_get_reg32(mem_buf, env->segs[R_FS].base);
1557b0f97baSDoug Gale 
1567b0f97baSDoug Gale         case IDX_SEG_REGS + 7:
1577b0f97baSDoug Gale             if ((env->hflags & HF_CS64_MASK) || GDB_FORCE_64) {
1587b0f97baSDoug Gale                 return gdb_get_reg64(mem_buf, env->segs[R_GS].base);
1597b0f97baSDoug Gale             }
1607b0f97baSDoug Gale             return gdb_get_reg32(mem_buf, env->segs[R_GS].base);
1617b0f97baSDoug Gale 
1627b0f97baSDoug Gale         case IDX_SEG_REGS + 8:
1637b0f97baSDoug Gale #ifdef TARGET_X86_64
1647b0f97baSDoug Gale             if ((env->hflags & HF_CS64_MASK) || GDB_FORCE_64) {
1657b0f97baSDoug Gale                 return gdb_get_reg64(mem_buf, env->kernelgsbase);
1667b0f97baSDoug Gale             }
1677b0f97baSDoug Gale             return gdb_get_reg32(mem_buf, env->kernelgsbase);
1687b0f97baSDoug Gale #else
1697b0f97baSDoug Gale             return gdb_get_reg32(mem_buf, 0);
1707b0f97baSDoug Gale #endif
1717b0f97baSDoug Gale 
172fcf5ef2aSThomas Huth         case IDX_FP_REGS + 8:
173fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, env->fpuc);
174fcf5ef2aSThomas Huth         case IDX_FP_REGS + 9:
175fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, (env->fpus & ~0x3800) |
176fcf5ef2aSThomas Huth                                           (env->fpstt & 0x7) << 11);
177fcf5ef2aSThomas Huth         case IDX_FP_REGS + 10:
178fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, 0); /* ftag */
179fcf5ef2aSThomas Huth         case IDX_FP_REGS + 11:
180fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, 0); /* fiseg */
181fcf5ef2aSThomas Huth         case IDX_FP_REGS + 12:
182fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, 0); /* fioff */
183fcf5ef2aSThomas Huth         case IDX_FP_REGS + 13:
184fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, 0); /* foseg */
185fcf5ef2aSThomas Huth         case IDX_FP_REGS + 14:
186fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, 0); /* fooff */
187fcf5ef2aSThomas Huth         case IDX_FP_REGS + 15:
188fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, 0); /* fop */
189fcf5ef2aSThomas Huth 
190fcf5ef2aSThomas Huth         case IDX_MXCSR_REG:
191fcf5ef2aSThomas Huth             return gdb_get_reg32(mem_buf, env->mxcsr);
1927b0f97baSDoug Gale 
1937b0f97baSDoug Gale         case IDX_CTL_CR0_REG:
1947b0f97baSDoug Gale             if ((env->hflags & HF_CS64_MASK) || GDB_FORCE_64) {
1957b0f97baSDoug Gale                 return gdb_get_reg64(mem_buf, env->cr[0]);
1967b0f97baSDoug Gale             }
1977b0f97baSDoug Gale             return gdb_get_reg32(mem_buf, env->cr[0]);
1987b0f97baSDoug Gale 
1997b0f97baSDoug Gale         case IDX_CTL_CR2_REG:
2007b0f97baSDoug Gale             if ((env->hflags & HF_CS64_MASK) || GDB_FORCE_64) {
2017b0f97baSDoug Gale                 return gdb_get_reg64(mem_buf, env->cr[2]);
2027b0f97baSDoug Gale             }
2037b0f97baSDoug Gale             return gdb_get_reg32(mem_buf, env->cr[2]);
2047b0f97baSDoug Gale 
2057b0f97baSDoug Gale         case IDX_CTL_CR3_REG:
2067b0f97baSDoug Gale             if ((env->hflags & HF_CS64_MASK) || GDB_FORCE_64) {
2077b0f97baSDoug Gale                 return gdb_get_reg64(mem_buf, env->cr[3]);
2087b0f97baSDoug Gale             }
2097b0f97baSDoug Gale             return gdb_get_reg32(mem_buf, env->cr[3]);
2107b0f97baSDoug Gale 
2117b0f97baSDoug Gale         case IDX_CTL_CR4_REG:
2127b0f97baSDoug Gale             if ((env->hflags & HF_CS64_MASK) || GDB_FORCE_64) {
2137b0f97baSDoug Gale                 return gdb_get_reg64(mem_buf, env->cr[4]);
2147b0f97baSDoug Gale             }
2157b0f97baSDoug Gale             return gdb_get_reg32(mem_buf, env->cr[4]);
2167b0f97baSDoug Gale 
2177b0f97baSDoug Gale         case IDX_CTL_CR8_REG:
2187b0f97baSDoug Gale #ifdef CONFIG_SOFTMMU
2197b0f97baSDoug Gale             tpr = cpu_get_apic_tpr(cpu->apic_state);
2207b0f97baSDoug Gale #else
2217b0f97baSDoug Gale             tpr = 0;
2227b0f97baSDoug Gale #endif
2237b0f97baSDoug Gale             if ((env->hflags & HF_CS64_MASK) || GDB_FORCE_64) {
2247b0f97baSDoug Gale                 return gdb_get_reg64(mem_buf, tpr);
2257b0f97baSDoug Gale             }
2267b0f97baSDoug Gale             return gdb_get_reg32(mem_buf, tpr);
2277b0f97baSDoug Gale 
2287b0f97baSDoug Gale         case IDX_CTL_EFER_REG:
2297b0f97baSDoug Gale             if ((env->hflags & HF_CS64_MASK) || GDB_FORCE_64) {
2307b0f97baSDoug Gale                 return gdb_get_reg64(mem_buf, env->efer);
2317b0f97baSDoug Gale             }
2327b0f97baSDoug Gale             return gdb_get_reg32(mem_buf, env->efer);
233fcf5ef2aSThomas Huth         }
234fcf5ef2aSThomas Huth     }
235fcf5ef2aSThomas Huth     return 0;
236fcf5ef2aSThomas Huth }
237fcf5ef2aSThomas Huth 
238fcf5ef2aSThomas Huth static int x86_cpu_gdb_load_seg(X86CPU *cpu, int sreg, uint8_t *mem_buf)
239fcf5ef2aSThomas Huth {
240fcf5ef2aSThomas Huth     CPUX86State *env = &cpu->env;
241fcf5ef2aSThomas Huth     uint16_t selector = ldl_p(mem_buf);
242fcf5ef2aSThomas Huth 
243fcf5ef2aSThomas Huth     if (selector != env->segs[sreg].selector) {
244fcf5ef2aSThomas Huth #if defined(CONFIG_USER_ONLY)
245fcf5ef2aSThomas Huth         cpu_x86_load_seg(env, sreg, selector);
246fcf5ef2aSThomas Huth #else
247fcf5ef2aSThomas Huth         unsigned int limit, flags;
248fcf5ef2aSThomas Huth         target_ulong base;
249fcf5ef2aSThomas Huth 
250fcf5ef2aSThomas Huth         if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) {
251fcf5ef2aSThomas Huth             int dpl = (env->eflags & VM_MASK) ? 3 : 0;
252fcf5ef2aSThomas Huth             base = selector << 4;
253fcf5ef2aSThomas Huth             limit = 0xffff;
254fcf5ef2aSThomas Huth             flags = DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
255fcf5ef2aSThomas Huth                     DESC_A_MASK | (dpl << DESC_DPL_SHIFT);
256fcf5ef2aSThomas Huth         } else {
257fcf5ef2aSThomas Huth             if (!cpu_x86_get_descr_debug(env, selector, &base, &limit,
258fcf5ef2aSThomas Huth                                          &flags)) {
259fcf5ef2aSThomas Huth                 return 4;
260fcf5ef2aSThomas Huth             }
261fcf5ef2aSThomas Huth         }
262fcf5ef2aSThomas Huth         cpu_x86_load_seg_cache(env, sreg, selector, base, limit, flags);
263fcf5ef2aSThomas Huth #endif
264fcf5ef2aSThomas Huth     }
265fcf5ef2aSThomas Huth     return 4;
266fcf5ef2aSThomas Huth }
267fcf5ef2aSThomas Huth 
268fcf5ef2aSThomas Huth int x86_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
269fcf5ef2aSThomas Huth {
270fcf5ef2aSThomas Huth     X86CPU *cpu = X86_CPU(cs);
271fcf5ef2aSThomas Huth     CPUX86State *env = &cpu->env;
272fcf5ef2aSThomas Huth     uint32_t tmp;
273fcf5ef2aSThomas Huth 
274e3592bc9SDoug Evans     /* N.B. GDB can't deal with changes in registers or sizes in the middle
275e3592bc9SDoug Evans        of a session. So if we're in 32-bit mode on a 64-bit cpu, still act
276e3592bc9SDoug Evans        as if we're on a 64-bit cpu. */
277e3592bc9SDoug Evans 
278fcf5ef2aSThomas Huth     if (n < CPU_NB_REGS) {
279e3592bc9SDoug Evans         if (TARGET_LONG_BITS == 64) {
280e3592bc9SDoug Evans             if (env->hflags & HF_CS64_MASK) {
281fcf5ef2aSThomas Huth                 env->regs[gpr_map[n]] = ldtul_p(mem_buf);
282e3592bc9SDoug Evans             } else if (n < CPU_NB_REGS32) {
283e3592bc9SDoug Evans                 env->regs[gpr_map[n]] = ldtul_p(mem_buf) & 0xffffffffUL;
284e3592bc9SDoug Evans             }
285fcf5ef2aSThomas Huth             return sizeof(target_ulong);
286fcf5ef2aSThomas Huth         } else if (n < CPU_NB_REGS32) {
287fcf5ef2aSThomas Huth             n = gpr_map32[n];
288fcf5ef2aSThomas Huth             env->regs[n] &= ~0xffffffffUL;
289fcf5ef2aSThomas Huth             env->regs[n] |= (uint32_t)ldl_p(mem_buf);
290fcf5ef2aSThomas Huth             return 4;
291fcf5ef2aSThomas Huth         }
292fcf5ef2aSThomas Huth     } else if (n >= IDX_FP_REGS && n < IDX_FP_REGS + 8) {
293fcf5ef2aSThomas Huth #ifdef USE_X86LDOUBLE
294fcf5ef2aSThomas Huth         /* FIXME: byteswap float values - after fixing fpregs layout. */
295fcf5ef2aSThomas Huth         memcpy(&env->fpregs[n - IDX_FP_REGS], mem_buf, 10);
296fcf5ef2aSThomas Huth #endif
297fcf5ef2aSThomas Huth         return 10;
298fcf5ef2aSThomas Huth     } else if (n >= IDX_XMM_REGS && n < IDX_XMM_REGS + CPU_NB_REGS) {
299fcf5ef2aSThomas Huth         n -= IDX_XMM_REGS;
300e3592bc9SDoug Evans         if (n < CPU_NB_REGS32 || TARGET_LONG_BITS == 64) {
301fcf5ef2aSThomas Huth             env->xmm_regs[n].ZMM_Q(0) = ldq_p(mem_buf);
302fcf5ef2aSThomas Huth             env->xmm_regs[n].ZMM_Q(1) = ldq_p(mem_buf + 8);
303fcf5ef2aSThomas Huth             return 16;
304fcf5ef2aSThomas Huth         }
305fcf5ef2aSThomas Huth     } else {
306fcf5ef2aSThomas Huth         switch (n) {
307fcf5ef2aSThomas Huth         case IDX_IP_REG:
308e3592bc9SDoug Evans             if (TARGET_LONG_BITS == 64) {
309e3592bc9SDoug Evans                 if (env->hflags & HF_CS64_MASK) {
310fcf5ef2aSThomas Huth                     env->eip = ldq_p(mem_buf);
311e3592bc9SDoug Evans                 } else {
312e3592bc9SDoug Evans                     env->eip = ldq_p(mem_buf) & 0xffffffffUL;
313e3592bc9SDoug Evans                 }
314fcf5ef2aSThomas Huth                 return 8;
315fcf5ef2aSThomas Huth             } else {
316fcf5ef2aSThomas Huth                 env->eip &= ~0xffffffffUL;
317fcf5ef2aSThomas Huth                 env->eip |= (uint32_t)ldl_p(mem_buf);
318fcf5ef2aSThomas Huth                 return 4;
319fcf5ef2aSThomas Huth             }
320fcf5ef2aSThomas Huth         case IDX_FLAGS_REG:
321fcf5ef2aSThomas Huth             env->eflags = ldl_p(mem_buf);
322fcf5ef2aSThomas Huth             return 4;
323fcf5ef2aSThomas Huth 
324fcf5ef2aSThomas Huth         case IDX_SEG_REGS:
325fcf5ef2aSThomas Huth             return x86_cpu_gdb_load_seg(cpu, R_CS, mem_buf);
326fcf5ef2aSThomas Huth         case IDX_SEG_REGS + 1:
327fcf5ef2aSThomas Huth             return x86_cpu_gdb_load_seg(cpu, R_SS, mem_buf);
328fcf5ef2aSThomas Huth         case IDX_SEG_REGS + 2:
329fcf5ef2aSThomas Huth             return x86_cpu_gdb_load_seg(cpu, R_DS, mem_buf);
330fcf5ef2aSThomas Huth         case IDX_SEG_REGS + 3:
331fcf5ef2aSThomas Huth             return x86_cpu_gdb_load_seg(cpu, R_ES, mem_buf);
332fcf5ef2aSThomas Huth         case IDX_SEG_REGS + 4:
333fcf5ef2aSThomas Huth             return x86_cpu_gdb_load_seg(cpu, R_FS, mem_buf);
334fcf5ef2aSThomas Huth         case IDX_SEG_REGS + 5:
335fcf5ef2aSThomas Huth             return x86_cpu_gdb_load_seg(cpu, R_GS, mem_buf);
336fcf5ef2aSThomas Huth 
3377b0f97baSDoug Gale         case IDX_SEG_REGS + 6:
3387b0f97baSDoug Gale             if (env->hflags & HF_CS64_MASK) {
3397b0f97baSDoug Gale                 env->segs[R_FS].base = ldq_p(mem_buf);
3407b0f97baSDoug Gale                 return 8;
3417b0f97baSDoug Gale             }
3427b0f97baSDoug Gale             env->segs[R_FS].base = ldl_p(mem_buf);
3437b0f97baSDoug Gale             return 4;
3447b0f97baSDoug Gale 
3457b0f97baSDoug Gale         case IDX_SEG_REGS + 7:
3467b0f97baSDoug Gale             if (env->hflags & HF_CS64_MASK) {
3477b0f97baSDoug Gale                 env->segs[R_GS].base = ldq_p(mem_buf);
3487b0f97baSDoug Gale                 return 8;
3497b0f97baSDoug Gale             }
3507b0f97baSDoug Gale             env->segs[R_GS].base = ldl_p(mem_buf);
3517b0f97baSDoug Gale             return 4;
3527b0f97baSDoug Gale 
3537b0f97baSDoug Gale         case IDX_SEG_REGS + 8:
354*5a07192aSmkdolata@us.ibm.com #ifdef TARGET_X86_64
3557b0f97baSDoug Gale             if (env->hflags & HF_CS64_MASK) {
3567b0f97baSDoug Gale                 env->kernelgsbase = ldq_p(mem_buf);
3577b0f97baSDoug Gale                 return 8;
3587b0f97baSDoug Gale             }
3597b0f97baSDoug Gale             env->kernelgsbase = ldl_p(mem_buf);
3607b0f97baSDoug Gale #endif
361*5a07192aSmkdolata@us.ibm.com             return 4;
3627b0f97baSDoug Gale 
363fcf5ef2aSThomas Huth         case IDX_FP_REGS + 8:
364fcf5ef2aSThomas Huth             cpu_set_fpuc(env, ldl_p(mem_buf));
365fcf5ef2aSThomas Huth             return 4;
366fcf5ef2aSThomas Huth         case IDX_FP_REGS + 9:
367fcf5ef2aSThomas Huth             tmp = ldl_p(mem_buf);
368fcf5ef2aSThomas Huth             env->fpstt = (tmp >> 11) & 7;
369fcf5ef2aSThomas Huth             env->fpus = tmp & ~0x3800;
370fcf5ef2aSThomas Huth             return 4;
371fcf5ef2aSThomas Huth         case IDX_FP_REGS + 10: /* ftag */
372fcf5ef2aSThomas Huth             return 4;
373fcf5ef2aSThomas Huth         case IDX_FP_REGS + 11: /* fiseg */
374fcf5ef2aSThomas Huth             return 4;
375fcf5ef2aSThomas Huth         case IDX_FP_REGS + 12: /* fioff */
376fcf5ef2aSThomas Huth             return 4;
377fcf5ef2aSThomas Huth         case IDX_FP_REGS + 13: /* foseg */
378fcf5ef2aSThomas Huth             return 4;
379fcf5ef2aSThomas Huth         case IDX_FP_REGS + 14: /* fooff */
380fcf5ef2aSThomas Huth             return 4;
381fcf5ef2aSThomas Huth         case IDX_FP_REGS + 15: /* fop */
382fcf5ef2aSThomas Huth             return 4;
383fcf5ef2aSThomas Huth 
384fcf5ef2aSThomas Huth         case IDX_MXCSR_REG:
385fcf5ef2aSThomas Huth             cpu_set_mxcsr(env, ldl_p(mem_buf));
386fcf5ef2aSThomas Huth             return 4;
3877b0f97baSDoug Gale 
3887b0f97baSDoug Gale         case IDX_CTL_CR0_REG:
3897b0f97baSDoug Gale             if (env->hflags & HF_CS64_MASK) {
3907b0f97baSDoug Gale                 cpu_x86_update_cr0(env, ldq_p(mem_buf));
3917b0f97baSDoug Gale                 return 8;
3927b0f97baSDoug Gale             }
3937b0f97baSDoug Gale             cpu_x86_update_cr0(env, ldl_p(mem_buf));
3947b0f97baSDoug Gale             return 4;
3957b0f97baSDoug Gale 
3967b0f97baSDoug Gale         case IDX_CTL_CR2_REG:
3977b0f97baSDoug Gale             if (env->hflags & HF_CS64_MASK) {
3987b0f97baSDoug Gale                 env->cr[2] = ldq_p(mem_buf);
3997b0f97baSDoug Gale                 return 8;
4007b0f97baSDoug Gale             }
4017b0f97baSDoug Gale             env->cr[2] = ldl_p(mem_buf);
4027b0f97baSDoug Gale             return 4;
4037b0f97baSDoug Gale 
4047b0f97baSDoug Gale         case IDX_CTL_CR3_REG:
4057b0f97baSDoug Gale             if (env->hflags & HF_CS64_MASK) {
4067b0f97baSDoug Gale                 cpu_x86_update_cr3(env, ldq_p(mem_buf));
4077b0f97baSDoug Gale                 return 8;
4087b0f97baSDoug Gale             }
4097b0f97baSDoug Gale             cpu_x86_update_cr3(env, ldl_p(mem_buf));
4107b0f97baSDoug Gale             return 4;
4117b0f97baSDoug Gale 
4127b0f97baSDoug Gale         case IDX_CTL_CR4_REG:
4137b0f97baSDoug Gale             if (env->hflags & HF_CS64_MASK) {
4147b0f97baSDoug Gale                 cpu_x86_update_cr4(env, ldq_p(mem_buf));
4157b0f97baSDoug Gale                 return 8;
4167b0f97baSDoug Gale             }
4177b0f97baSDoug Gale             cpu_x86_update_cr4(env, ldl_p(mem_buf));
4187b0f97baSDoug Gale             return 4;
4197b0f97baSDoug Gale 
4207b0f97baSDoug Gale         case IDX_CTL_CR8_REG:
4217b0f97baSDoug Gale             if (env->hflags & HF_CS64_MASK) {
4227b0f97baSDoug Gale #ifdef CONFIG_SOFTMMU
4237b0f97baSDoug Gale                 cpu_set_apic_tpr(cpu->apic_state, ldq_p(mem_buf));
4247b0f97baSDoug Gale #endif
4257b0f97baSDoug Gale                 return 8;
4267b0f97baSDoug Gale             }
4277b0f97baSDoug Gale #ifdef CONFIG_SOFTMMU
4287b0f97baSDoug Gale             cpu_set_apic_tpr(cpu->apic_state, ldl_p(mem_buf));
4297b0f97baSDoug Gale #endif
4307b0f97baSDoug Gale             return 4;
4317b0f97baSDoug Gale 
4327b0f97baSDoug Gale         case IDX_CTL_EFER_REG:
4337b0f97baSDoug Gale             if (env->hflags & HF_CS64_MASK) {
4347b0f97baSDoug Gale                 cpu_load_efer(env, ldq_p(mem_buf));
4357b0f97baSDoug Gale                 return 8;
4367b0f97baSDoug Gale             }
4377b0f97baSDoug Gale             cpu_load_efer(env, ldl_p(mem_buf));
4387b0f97baSDoug Gale             return 4;
4397b0f97baSDoug Gale 
440fcf5ef2aSThomas Huth         }
441fcf5ef2aSThomas Huth     }
442fcf5ef2aSThomas Huth     /* Unrecognised register.  */
443fcf5ef2aSThomas Huth     return 0;
444fcf5ef2aSThomas Huth }
445