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"
21 #include "qemu-common.h"
23 #include "exec/gdbstub.h"
26 static const int gpr_map
[16] = {
27 R_EAX
, R_EBX
, R_ECX
, R_EDX
, R_ESI
, R_EDI
, R_EBP
, R_ESP
,
28 8, 9, 10, 11, 12, 13, 14, 15
31 #define gpr_map gpr_map32
33 static const int gpr_map32
[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
35 #define IDX_IP_REG CPU_NB_REGS
36 #define IDX_FLAGS_REG (IDX_IP_REG + 1)
37 #define IDX_SEG_REGS (IDX_FLAGS_REG + 1)
38 #define IDX_FP_REGS (IDX_SEG_REGS + 6)
39 #define IDX_XMM_REGS (IDX_FP_REGS + 16)
40 #define IDX_MXCSR_REG (IDX_XMM_REGS + CPU_NB_REGS)
42 int x86_cpu_gdb_read_register(CPUState
*cs
, uint8_t *mem_buf
, int n
)
44 X86CPU
*cpu
= X86_CPU(cs
);
45 CPUX86State
*env
= &cpu
->env
;
47 /* N.B. GDB can't deal with changes in registers or sizes in the middle
48 of a session. So if we're in 32-bit mode on a 64-bit cpu, still act
49 as if we're on a 64-bit cpu. */
51 if (n
< CPU_NB_REGS
) {
52 if (TARGET_LONG_BITS
== 64) {
53 if (env
->hflags
& HF_CS64_MASK
) {
54 return gdb_get_reg64(mem_buf
, env
->regs
[gpr_map
[n
]]);
55 } else if (n
< CPU_NB_REGS32
) {
56 return gdb_get_reg64(mem_buf
,
57 env
->regs
[gpr_map
[n
]] & 0xffffffffUL
);
59 memset(mem_buf
, 0, sizeof(target_ulong
));
60 return sizeof(target_ulong
);
63 return gdb_get_reg32(mem_buf
, env
->regs
[gpr_map32
[n
]]);
65 } else if (n
>= IDX_FP_REGS
&& n
< IDX_FP_REGS
+ 8) {
67 /* FIXME: byteswap float values - after fixing fpregs layout. */
68 memcpy(mem_buf
, &env
->fpregs
[n
- IDX_FP_REGS
], 10);
70 memset(mem_buf
, 0, 10);
73 } else if (n
>= IDX_XMM_REGS
&& n
< IDX_XMM_REGS
+ CPU_NB_REGS
) {
75 if (n
< CPU_NB_REGS32
|| TARGET_LONG_BITS
== 64) {
76 stq_p(mem_buf
, env
->xmm_regs
[n
].ZMM_Q(0));
77 stq_p(mem_buf
+ 8, env
->xmm_regs
[n
].ZMM_Q(1));
83 if (TARGET_LONG_BITS
== 64) {
84 if (env
->hflags
& HF_CS64_MASK
) {
85 return gdb_get_reg64(mem_buf
, env
->eip
);
87 return gdb_get_reg64(mem_buf
, env
->eip
& 0xffffffffUL
);
90 return gdb_get_reg32(mem_buf
, env
->eip
);
93 return gdb_get_reg32(mem_buf
, env
->eflags
);
96 return gdb_get_reg32(mem_buf
, env
->segs
[R_CS
].selector
);
97 case IDX_SEG_REGS
+ 1:
98 return gdb_get_reg32(mem_buf
, env
->segs
[R_SS
].selector
);
99 case IDX_SEG_REGS
+ 2:
100 return gdb_get_reg32(mem_buf
, env
->segs
[R_DS
].selector
);
101 case IDX_SEG_REGS
+ 3:
102 return gdb_get_reg32(mem_buf
, env
->segs
[R_ES
].selector
);
103 case IDX_SEG_REGS
+ 4:
104 return gdb_get_reg32(mem_buf
, env
->segs
[R_FS
].selector
);
105 case IDX_SEG_REGS
+ 5:
106 return gdb_get_reg32(mem_buf
, env
->segs
[R_GS
].selector
);
108 case IDX_FP_REGS
+ 8:
109 return gdb_get_reg32(mem_buf
, env
->fpuc
);
110 case IDX_FP_REGS
+ 9:
111 return gdb_get_reg32(mem_buf
, (env
->fpus
& ~0x3800) |
112 (env
->fpstt
& 0x7) << 11);
113 case IDX_FP_REGS
+ 10:
114 return gdb_get_reg32(mem_buf
, 0); /* ftag */
115 case IDX_FP_REGS
+ 11:
116 return gdb_get_reg32(mem_buf
, 0); /* fiseg */
117 case IDX_FP_REGS
+ 12:
118 return gdb_get_reg32(mem_buf
, 0); /* fioff */
119 case IDX_FP_REGS
+ 13:
120 return gdb_get_reg32(mem_buf
, 0); /* foseg */
121 case IDX_FP_REGS
+ 14:
122 return gdb_get_reg32(mem_buf
, 0); /* fooff */
123 case IDX_FP_REGS
+ 15:
124 return gdb_get_reg32(mem_buf
, 0); /* fop */
127 return gdb_get_reg32(mem_buf
, env
->mxcsr
);
133 static int x86_cpu_gdb_load_seg(X86CPU
*cpu
, int sreg
, uint8_t *mem_buf
)
135 CPUX86State
*env
= &cpu
->env
;
136 uint16_t selector
= ldl_p(mem_buf
);
138 if (selector
!= env
->segs
[sreg
].selector
) {
139 #if defined(CONFIG_USER_ONLY)
140 cpu_x86_load_seg(env
, sreg
, selector
);
142 unsigned int limit
, flags
;
145 if (!(env
->cr
[0] & CR0_PE_MASK
) || (env
->eflags
& VM_MASK
)) {
146 int dpl
= (env
->eflags
& VM_MASK
) ? 3 : 0;
147 base
= selector
<< 4;
149 flags
= DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
150 DESC_A_MASK
| (dpl
<< DESC_DPL_SHIFT
);
152 if (!cpu_x86_get_descr_debug(env
, selector
, &base
, &limit
,
157 cpu_x86_load_seg_cache(env
, sreg
, selector
, base
, limit
, flags
);
163 int x86_cpu_gdb_write_register(CPUState
*cs
, uint8_t *mem_buf
, int n
)
165 X86CPU
*cpu
= X86_CPU(cs
);
166 CPUX86State
*env
= &cpu
->env
;
169 /* N.B. GDB can't deal with changes in registers or sizes in the middle
170 of a session. So if we're in 32-bit mode on a 64-bit cpu, still act
171 as if we're on a 64-bit cpu. */
173 if (n
< CPU_NB_REGS
) {
174 if (TARGET_LONG_BITS
== 64) {
175 if (env
->hflags
& HF_CS64_MASK
) {
176 env
->regs
[gpr_map
[n
]] = ldtul_p(mem_buf
);
177 } else if (n
< CPU_NB_REGS32
) {
178 env
->regs
[gpr_map
[n
]] = ldtul_p(mem_buf
) & 0xffffffffUL
;
180 return sizeof(target_ulong
);
181 } else if (n
< CPU_NB_REGS32
) {
183 env
->regs
[n
] &= ~0xffffffffUL
;
184 env
->regs
[n
] |= (uint32_t)ldl_p(mem_buf
);
187 } else if (n
>= IDX_FP_REGS
&& n
< IDX_FP_REGS
+ 8) {
188 #ifdef USE_X86LDOUBLE
189 /* FIXME: byteswap float values - after fixing fpregs layout. */
190 memcpy(&env
->fpregs
[n
- IDX_FP_REGS
], mem_buf
, 10);
193 } else if (n
>= IDX_XMM_REGS
&& n
< IDX_XMM_REGS
+ CPU_NB_REGS
) {
195 if (n
< CPU_NB_REGS32
|| TARGET_LONG_BITS
== 64) {
196 env
->xmm_regs
[n
].ZMM_Q(0) = ldq_p(mem_buf
);
197 env
->xmm_regs
[n
].ZMM_Q(1) = ldq_p(mem_buf
+ 8);
203 if (TARGET_LONG_BITS
== 64) {
204 if (env
->hflags
& HF_CS64_MASK
) {
205 env
->eip
= ldq_p(mem_buf
);
207 env
->eip
= ldq_p(mem_buf
) & 0xffffffffUL
;
211 env
->eip
&= ~0xffffffffUL
;
212 env
->eip
|= (uint32_t)ldl_p(mem_buf
);
216 env
->eflags
= ldl_p(mem_buf
);
220 return x86_cpu_gdb_load_seg(cpu
, R_CS
, mem_buf
);
221 case IDX_SEG_REGS
+ 1:
222 return x86_cpu_gdb_load_seg(cpu
, R_SS
, mem_buf
);
223 case IDX_SEG_REGS
+ 2:
224 return x86_cpu_gdb_load_seg(cpu
, R_DS
, mem_buf
);
225 case IDX_SEG_REGS
+ 3:
226 return x86_cpu_gdb_load_seg(cpu
, R_ES
, mem_buf
);
227 case IDX_SEG_REGS
+ 4:
228 return x86_cpu_gdb_load_seg(cpu
, R_FS
, mem_buf
);
229 case IDX_SEG_REGS
+ 5:
230 return x86_cpu_gdb_load_seg(cpu
, R_GS
, mem_buf
);
232 case IDX_FP_REGS
+ 8:
233 cpu_set_fpuc(env
, ldl_p(mem_buf
));
235 case IDX_FP_REGS
+ 9:
236 tmp
= ldl_p(mem_buf
);
237 env
->fpstt
= (tmp
>> 11) & 7;
238 env
->fpus
= tmp
& ~0x3800;
240 case IDX_FP_REGS
+ 10: /* ftag */
242 case IDX_FP_REGS
+ 11: /* fiseg */
244 case IDX_FP_REGS
+ 12: /* fioff */
246 case IDX_FP_REGS
+ 13: /* foseg */
248 case IDX_FP_REGS
+ 14: /* fooff */
250 case IDX_FP_REGS
+ 15: /* fop */
254 cpu_set_mxcsr(env
, ldl_p(mem_buf
));
258 /* Unrecognised register. */