4 * Copyright (c) 2003-2005 Fabrice Bellard
5 * Copyright (c) 2013 SUSE LINUX Products GmbH
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
22 #include "exec/gdbstub.h"
25 static const int gpr_map
[16] = {
26 R_EAX
, R_EBX
, R_ECX
, R_EDX
, R_ESI
, R_EDI
, R_EBP
, R_ESP
,
27 8, 9, 10, 11, 12, 13, 14, 15
30 #define gpr_map gpr_map32
32 static const int gpr_map32
[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
35 * Keep these in sync with assignment to
36 * gdb_num_core_regs in target/i386/cpu.c
37 * and with the machine description
41 * SEG: 6 segments, plus fs_base, gs_base, kernel_gs_base
45 * general regs -----> 8 or 16
48 #define IDX_NB_FLAGS 1
49 #define IDX_NB_SEG (6 + 3)
53 * fpu regs ----------> 8 or 16
55 #define IDX_NB_MXCSR 1
57 * total ----> 8+1+1+9+6+16+8+1=50 or 16+1+1+9+6+16+16+1=66
60 #define IDX_IP_REG CPU_NB_REGS
61 #define IDX_FLAGS_REG (IDX_IP_REG + IDX_NB_IP)
62 #define IDX_SEG_REGS (IDX_FLAGS_REG + IDX_NB_FLAGS)
63 #define IDX_CTL_REGS (IDX_SEG_REGS + IDX_NB_SEG)
64 #define IDX_FP_REGS (IDX_CTL_REGS + IDX_NB_CTL)
65 #define IDX_XMM_REGS (IDX_FP_REGS + IDX_NB_FP)
66 #define IDX_MXCSR_REG (IDX_XMM_REGS + CPU_NB_REGS)
68 #define IDX_CTL_CR0_REG (IDX_CTL_REGS + 0)
69 #define IDX_CTL_CR2_REG (IDX_CTL_REGS + 1)
70 #define IDX_CTL_CR3_REG (IDX_CTL_REGS + 2)
71 #define IDX_CTL_CR4_REG (IDX_CTL_REGS + 3)
72 #define IDX_CTL_CR8_REG (IDX_CTL_REGS + 4)
73 #define IDX_CTL_EFER_REG (IDX_CTL_REGS + 5)
76 #define GDB_FORCE_64 1
78 #define GDB_FORCE_64 0
82 int x86_cpu_gdb_read_register(CPUState
*cs
, uint8_t *mem_buf
, int n
)
84 X86CPU
*cpu
= X86_CPU(cs
);
85 CPUX86State
*env
= &cpu
->env
;
89 /* N.B. GDB can't deal with changes in registers or sizes in the middle
90 of a session. So if we're in 32-bit mode on a 64-bit cpu, still act
91 as if we're on a 64-bit cpu. */
93 if (n
< CPU_NB_REGS
) {
94 if (TARGET_LONG_BITS
== 64) {
95 if (env
->hflags
& HF_CS64_MASK
) {
96 return gdb_get_reg64(mem_buf
, env
->regs
[gpr_map
[n
]]);
97 } else if (n
< CPU_NB_REGS32
) {
98 return gdb_get_reg64(mem_buf
,
99 env
->regs
[gpr_map
[n
]] & 0xffffffffUL
);
101 memset(mem_buf
, 0, sizeof(target_ulong
));
102 return sizeof(target_ulong
);
105 return gdb_get_reg32(mem_buf
, env
->regs
[gpr_map32
[n
]]);
107 } else if (n
>= IDX_FP_REGS
&& n
< IDX_FP_REGS
+ 8) {
108 #ifdef USE_X86LDOUBLE
109 /* FIXME: byteswap float values - after fixing fpregs layout. */
110 memcpy(mem_buf
, &env
->fpregs
[n
- IDX_FP_REGS
], 10);
112 memset(mem_buf
, 0, 10);
115 } else if (n
>= IDX_XMM_REGS
&& n
< IDX_XMM_REGS
+ CPU_NB_REGS
) {
117 if (n
< CPU_NB_REGS32
|| TARGET_LONG_BITS
== 64) {
118 stq_p(mem_buf
, env
->xmm_regs
[n
].ZMM_Q(0));
119 stq_p(mem_buf
+ 8, env
->xmm_regs
[n
].ZMM_Q(1));
125 if (TARGET_LONG_BITS
== 64) {
126 if (env
->hflags
& HF_CS64_MASK
) {
127 return gdb_get_reg64(mem_buf
, env
->eip
);
129 return gdb_get_reg64(mem_buf
, env
->eip
& 0xffffffffUL
);
132 return gdb_get_reg32(mem_buf
, env
->eip
);
135 return gdb_get_reg32(mem_buf
, env
->eflags
);
138 return gdb_get_reg32(mem_buf
, env
->segs
[R_CS
].selector
);
139 case IDX_SEG_REGS
+ 1:
140 return gdb_get_reg32(mem_buf
, env
->segs
[R_SS
].selector
);
141 case IDX_SEG_REGS
+ 2:
142 return gdb_get_reg32(mem_buf
, env
->segs
[R_DS
].selector
);
143 case IDX_SEG_REGS
+ 3:
144 return gdb_get_reg32(mem_buf
, env
->segs
[R_ES
].selector
);
145 case IDX_SEG_REGS
+ 4:
146 return gdb_get_reg32(mem_buf
, env
->segs
[R_FS
].selector
);
147 case IDX_SEG_REGS
+ 5:
148 return gdb_get_reg32(mem_buf
, env
->segs
[R_GS
].selector
);
150 case IDX_SEG_REGS
+ 6:
151 if ((env
->hflags
& HF_CS64_MASK
) || GDB_FORCE_64
) {
152 return gdb_get_reg64(mem_buf
, env
->segs
[R_FS
].base
);
154 return gdb_get_reg32(mem_buf
, env
->segs
[R_FS
].base
);
156 case IDX_SEG_REGS
+ 7:
157 if ((env
->hflags
& HF_CS64_MASK
) || GDB_FORCE_64
) {
158 return gdb_get_reg64(mem_buf
, env
->segs
[R_GS
].base
);
160 return gdb_get_reg32(mem_buf
, env
->segs
[R_GS
].base
);
162 case IDX_SEG_REGS
+ 8:
164 if ((env
->hflags
& HF_CS64_MASK
) || GDB_FORCE_64
) {
165 return gdb_get_reg64(mem_buf
, env
->kernelgsbase
);
167 return gdb_get_reg32(mem_buf
, env
->kernelgsbase
);
169 return gdb_get_reg32(mem_buf
, 0);
172 case IDX_FP_REGS
+ 8:
173 return gdb_get_reg32(mem_buf
, env
->fpuc
);
174 case IDX_FP_REGS
+ 9:
175 return gdb_get_reg32(mem_buf
, (env
->fpus
& ~0x3800) |
176 (env
->fpstt
& 0x7) << 11);
177 case IDX_FP_REGS
+ 10:
178 return gdb_get_reg32(mem_buf
, 0); /* ftag */
179 case IDX_FP_REGS
+ 11:
180 return gdb_get_reg32(mem_buf
, 0); /* fiseg */
181 case IDX_FP_REGS
+ 12:
182 return gdb_get_reg32(mem_buf
, 0); /* fioff */
183 case IDX_FP_REGS
+ 13:
184 return gdb_get_reg32(mem_buf
, 0); /* foseg */
185 case IDX_FP_REGS
+ 14:
186 return gdb_get_reg32(mem_buf
, 0); /* fooff */
187 case IDX_FP_REGS
+ 15:
188 return gdb_get_reg32(mem_buf
, 0); /* fop */
191 return gdb_get_reg32(mem_buf
, env
->mxcsr
);
193 case IDX_CTL_CR0_REG
:
194 if ((env
->hflags
& HF_CS64_MASK
) || GDB_FORCE_64
) {
195 return gdb_get_reg64(mem_buf
, env
->cr
[0]);
197 return gdb_get_reg32(mem_buf
, env
->cr
[0]);
199 case IDX_CTL_CR2_REG
:
200 if ((env
->hflags
& HF_CS64_MASK
) || GDB_FORCE_64
) {
201 return gdb_get_reg64(mem_buf
, env
->cr
[2]);
203 return gdb_get_reg32(mem_buf
, env
->cr
[2]);
205 case IDX_CTL_CR3_REG
:
206 if ((env
->hflags
& HF_CS64_MASK
) || GDB_FORCE_64
) {
207 return gdb_get_reg64(mem_buf
, env
->cr
[3]);
209 return gdb_get_reg32(mem_buf
, env
->cr
[3]);
211 case IDX_CTL_CR4_REG
:
212 if ((env
->hflags
& HF_CS64_MASK
) || GDB_FORCE_64
) {
213 return gdb_get_reg64(mem_buf
, env
->cr
[4]);
215 return gdb_get_reg32(mem_buf
, env
->cr
[4]);
217 case IDX_CTL_CR8_REG
:
218 #ifdef CONFIG_SOFTMMU
219 tpr
= cpu_get_apic_tpr(cpu
->apic_state
);
223 if ((env
->hflags
& HF_CS64_MASK
) || GDB_FORCE_64
) {
224 return gdb_get_reg64(mem_buf
, tpr
);
226 return gdb_get_reg32(mem_buf
, tpr
);
228 case IDX_CTL_EFER_REG
:
229 if ((env
->hflags
& HF_CS64_MASK
) || GDB_FORCE_64
) {
230 return gdb_get_reg64(mem_buf
, env
->efer
);
232 return gdb_get_reg32(mem_buf
, env
->efer
);
238 static int x86_cpu_gdb_load_seg(X86CPU
*cpu
, int sreg
, uint8_t *mem_buf
)
240 CPUX86State
*env
= &cpu
->env
;
241 uint16_t selector
= ldl_p(mem_buf
);
243 if (selector
!= env
->segs
[sreg
].selector
) {
244 #if defined(CONFIG_USER_ONLY)
245 cpu_x86_load_seg(env
, sreg
, selector
);
247 unsigned int limit
, flags
;
250 if (!(env
->cr
[0] & CR0_PE_MASK
) || (env
->eflags
& VM_MASK
)) {
251 int dpl
= (env
->eflags
& VM_MASK
) ? 3 : 0;
252 base
= selector
<< 4;
254 flags
= DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
255 DESC_A_MASK
| (dpl
<< DESC_DPL_SHIFT
);
257 if (!cpu_x86_get_descr_debug(env
, selector
, &base
, &limit
,
262 cpu_x86_load_seg_cache(env
, sreg
, selector
, base
, limit
, flags
);
268 int x86_cpu_gdb_write_register(CPUState
*cs
, uint8_t *mem_buf
, int n
)
270 X86CPU
*cpu
= X86_CPU(cs
);
271 CPUX86State
*env
= &cpu
->env
;
274 /* N.B. GDB can't deal with changes in registers or sizes in the middle
275 of a session. So if we're in 32-bit mode on a 64-bit cpu, still act
276 as if we're on a 64-bit cpu. */
278 if (n
< CPU_NB_REGS
) {
279 if (TARGET_LONG_BITS
== 64) {
280 if (env
->hflags
& HF_CS64_MASK
) {
281 env
->regs
[gpr_map
[n
]] = ldtul_p(mem_buf
);
282 } else if (n
< CPU_NB_REGS32
) {
283 env
->regs
[gpr_map
[n
]] = ldtul_p(mem_buf
) & 0xffffffffUL
;
285 return sizeof(target_ulong
);
286 } else if (n
< CPU_NB_REGS32
) {
288 env
->regs
[n
] &= ~0xffffffffUL
;
289 env
->regs
[n
] |= (uint32_t)ldl_p(mem_buf
);
292 } else if (n
>= IDX_FP_REGS
&& n
< IDX_FP_REGS
+ 8) {
293 #ifdef USE_X86LDOUBLE
294 /* FIXME: byteswap float values - after fixing fpregs layout. */
295 memcpy(&env
->fpregs
[n
- IDX_FP_REGS
], mem_buf
, 10);
298 } else if (n
>= IDX_XMM_REGS
&& n
< IDX_XMM_REGS
+ CPU_NB_REGS
) {
300 if (n
< CPU_NB_REGS32
|| TARGET_LONG_BITS
== 64) {
301 env
->xmm_regs
[n
].ZMM_Q(0) = ldq_p(mem_buf
);
302 env
->xmm_regs
[n
].ZMM_Q(1) = ldq_p(mem_buf
+ 8);
308 if (TARGET_LONG_BITS
== 64) {
309 if (env
->hflags
& HF_CS64_MASK
) {
310 env
->eip
= ldq_p(mem_buf
);
312 env
->eip
= ldq_p(mem_buf
) & 0xffffffffUL
;
316 env
->eip
&= ~0xffffffffUL
;
317 env
->eip
|= (uint32_t)ldl_p(mem_buf
);
321 env
->eflags
= ldl_p(mem_buf
);
325 return x86_cpu_gdb_load_seg(cpu
, R_CS
, mem_buf
);
326 case IDX_SEG_REGS
+ 1:
327 return x86_cpu_gdb_load_seg(cpu
, R_SS
, mem_buf
);
328 case IDX_SEG_REGS
+ 2:
329 return x86_cpu_gdb_load_seg(cpu
, R_DS
, mem_buf
);
330 case IDX_SEG_REGS
+ 3:
331 return x86_cpu_gdb_load_seg(cpu
, R_ES
, mem_buf
);
332 case IDX_SEG_REGS
+ 4:
333 return x86_cpu_gdb_load_seg(cpu
, R_FS
, mem_buf
);
334 case IDX_SEG_REGS
+ 5:
335 return x86_cpu_gdb_load_seg(cpu
, R_GS
, mem_buf
);
337 case IDX_SEG_REGS
+ 6:
338 if (env
->hflags
& HF_CS64_MASK
) {
339 env
->segs
[R_FS
].base
= ldq_p(mem_buf
);
342 env
->segs
[R_FS
].base
= ldl_p(mem_buf
);
345 case IDX_SEG_REGS
+ 7:
346 if (env
->hflags
& HF_CS64_MASK
) {
347 env
->segs
[R_GS
].base
= ldq_p(mem_buf
);
350 env
->segs
[R_GS
].base
= ldl_p(mem_buf
);
354 case IDX_SEG_REGS
+ 8:
355 if (env
->hflags
& HF_CS64_MASK
) {
356 env
->kernelgsbase
= ldq_p(mem_buf
);
359 env
->kernelgsbase
= ldl_p(mem_buf
);
363 case IDX_FP_REGS
+ 8:
364 cpu_set_fpuc(env
, ldl_p(mem_buf
));
366 case IDX_FP_REGS
+ 9:
367 tmp
= ldl_p(mem_buf
);
368 env
->fpstt
= (tmp
>> 11) & 7;
369 env
->fpus
= tmp
& ~0x3800;
371 case IDX_FP_REGS
+ 10: /* ftag */
373 case IDX_FP_REGS
+ 11: /* fiseg */
375 case IDX_FP_REGS
+ 12: /* fioff */
377 case IDX_FP_REGS
+ 13: /* foseg */
379 case IDX_FP_REGS
+ 14: /* fooff */
381 case IDX_FP_REGS
+ 15: /* fop */
385 cpu_set_mxcsr(env
, ldl_p(mem_buf
));
388 case IDX_CTL_CR0_REG
:
389 if (env
->hflags
& HF_CS64_MASK
) {
390 cpu_x86_update_cr0(env
, ldq_p(mem_buf
));
393 cpu_x86_update_cr0(env
, ldl_p(mem_buf
));
396 case IDX_CTL_CR2_REG
:
397 if (env
->hflags
& HF_CS64_MASK
) {
398 env
->cr
[2] = ldq_p(mem_buf
);
401 env
->cr
[2] = ldl_p(mem_buf
);
404 case IDX_CTL_CR3_REG
:
405 if (env
->hflags
& HF_CS64_MASK
) {
406 cpu_x86_update_cr3(env
, ldq_p(mem_buf
));
409 cpu_x86_update_cr3(env
, ldl_p(mem_buf
));
412 case IDX_CTL_CR4_REG
:
413 if (env
->hflags
& HF_CS64_MASK
) {
414 cpu_x86_update_cr4(env
, ldq_p(mem_buf
));
417 cpu_x86_update_cr4(env
, ldl_p(mem_buf
));
420 case IDX_CTL_CR8_REG
:
421 if (env
->hflags
& HF_CS64_MASK
) {
422 #ifdef CONFIG_SOFTMMU
423 cpu_set_apic_tpr(cpu
->apic_state
, ldq_p(mem_buf
));
427 #ifdef CONFIG_SOFTMMU
428 cpu_set_apic_tpr(cpu
->apic_state
, ldl_p(mem_buf
));
432 case IDX_CTL_EFER_REG
:
433 if (env
->hflags
& HF_CS64_MASK
) {
434 cpu_load_efer(env
, ldq_p(mem_buf
));
437 cpu_load_efer(env
, ldl_p(mem_buf
));
442 /* Unrecognised register. */