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"
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 };
34 #define IDX_IP_REG CPU_NB_REGS
35 #define IDX_FLAGS_REG (IDX_IP_REG + 1)
36 #define IDX_SEG_REGS (IDX_FLAGS_REG + 1)
37 #define IDX_FP_REGS (IDX_SEG_REGS + 6)
38 #define IDX_XMM_REGS (IDX_FP_REGS + 16)
39 #define IDX_MXCSR_REG (IDX_XMM_REGS + CPU_NB_REGS)
41 int x86_cpu_gdb_read_register(CPUState
*cs
, uint8_t *mem_buf
, int n
)
43 X86CPU
*cpu
= X86_CPU(cs
);
44 CPUX86State
*env
= &cpu
->env
;
46 if (n
< CPU_NB_REGS
) {
47 if (TARGET_LONG_BITS
== 64 && env
->hflags
& HF_CS64_MASK
) {
48 return gdb_get_reg64(mem_buf
, env
->regs
[gpr_map
[n
]]);
49 } else if (n
< CPU_NB_REGS32
) {
50 return gdb_get_reg32(mem_buf
, env
->regs
[gpr_map32
[n
]]);
52 } else if (n
>= IDX_FP_REGS
&& n
< IDX_FP_REGS
+ 8) {
54 /* FIXME: byteswap float values - after fixing fpregs layout. */
55 memcpy(mem_buf
, &env
->fpregs
[n
- IDX_FP_REGS
], 10);
57 memset(mem_buf
, 0, 10);
60 } else if (n
>= IDX_XMM_REGS
&& n
< IDX_XMM_REGS
+ CPU_NB_REGS
) {
62 if (n
< CPU_NB_REGS32
||
63 (TARGET_LONG_BITS
== 64 && env
->hflags
& HF_CS64_MASK
)) {
64 stq_p(mem_buf
, env
->xmm_regs
[n
].ZMM_Q(0));
65 stq_p(mem_buf
+ 8, env
->xmm_regs
[n
].ZMM_Q(1));
71 if (TARGET_LONG_BITS
== 64 && env
->hflags
& HF_CS64_MASK
) {
72 return gdb_get_reg64(mem_buf
, env
->eip
);
74 return gdb_get_reg32(mem_buf
, env
->eip
);
77 return gdb_get_reg32(mem_buf
, env
->eflags
);
80 return gdb_get_reg32(mem_buf
, env
->segs
[R_CS
].selector
);
81 case IDX_SEG_REGS
+ 1:
82 return gdb_get_reg32(mem_buf
, env
->segs
[R_SS
].selector
);
83 case IDX_SEG_REGS
+ 2:
84 return gdb_get_reg32(mem_buf
, env
->segs
[R_DS
].selector
);
85 case IDX_SEG_REGS
+ 3:
86 return gdb_get_reg32(mem_buf
, env
->segs
[R_ES
].selector
);
87 case IDX_SEG_REGS
+ 4:
88 return gdb_get_reg32(mem_buf
, env
->segs
[R_FS
].selector
);
89 case IDX_SEG_REGS
+ 5:
90 return gdb_get_reg32(mem_buf
, env
->segs
[R_GS
].selector
);
93 return gdb_get_reg32(mem_buf
, env
->fpuc
);
95 return gdb_get_reg32(mem_buf
, (env
->fpus
& ~0x3800) |
96 (env
->fpstt
& 0x7) << 11);
97 case IDX_FP_REGS
+ 10:
98 return gdb_get_reg32(mem_buf
, 0); /* ftag */
99 case IDX_FP_REGS
+ 11:
100 return gdb_get_reg32(mem_buf
, 0); /* fiseg */
101 case IDX_FP_REGS
+ 12:
102 return gdb_get_reg32(mem_buf
, 0); /* fioff */
103 case IDX_FP_REGS
+ 13:
104 return gdb_get_reg32(mem_buf
, 0); /* foseg */
105 case IDX_FP_REGS
+ 14:
106 return gdb_get_reg32(mem_buf
, 0); /* fooff */
107 case IDX_FP_REGS
+ 15:
108 return gdb_get_reg32(mem_buf
, 0); /* fop */
111 return gdb_get_reg32(mem_buf
, env
->mxcsr
);
117 static int x86_cpu_gdb_load_seg(X86CPU
*cpu
, int sreg
, uint8_t *mem_buf
)
119 CPUX86State
*env
= &cpu
->env
;
120 uint16_t selector
= ldl_p(mem_buf
);
122 if (selector
!= env
->segs
[sreg
].selector
) {
123 #if defined(CONFIG_USER_ONLY)
124 cpu_x86_load_seg(env
, sreg
, selector
);
126 unsigned int limit
, flags
;
129 if (!(env
->cr
[0] & CR0_PE_MASK
) || (env
->eflags
& VM_MASK
)) {
130 int dpl
= (env
->eflags
& VM_MASK
) ? 3 : 0;
131 base
= selector
<< 4;
133 flags
= DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
134 DESC_A_MASK
| (dpl
<< DESC_DPL_SHIFT
);
136 if (!cpu_x86_get_descr_debug(env
, selector
, &base
, &limit
,
141 cpu_x86_load_seg_cache(env
, sreg
, selector
, base
, limit
, flags
);
147 int x86_cpu_gdb_write_register(CPUState
*cs
, uint8_t *mem_buf
, int n
)
149 X86CPU
*cpu
= X86_CPU(cs
);
150 CPUX86State
*env
= &cpu
->env
;
153 if (n
< CPU_NB_REGS
) {
154 if (TARGET_LONG_BITS
== 64 && env
->hflags
& HF_CS64_MASK
) {
155 env
->regs
[gpr_map
[n
]] = ldtul_p(mem_buf
);
156 return sizeof(target_ulong
);
157 } else if (n
< CPU_NB_REGS32
) {
159 env
->regs
[n
] &= ~0xffffffffUL
;
160 env
->regs
[n
] |= (uint32_t)ldl_p(mem_buf
);
163 } else if (n
>= IDX_FP_REGS
&& n
< IDX_FP_REGS
+ 8) {
164 #ifdef USE_X86LDOUBLE
165 /* FIXME: byteswap float values - after fixing fpregs layout. */
166 memcpy(&env
->fpregs
[n
- IDX_FP_REGS
], mem_buf
, 10);
169 } else if (n
>= IDX_XMM_REGS
&& n
< IDX_XMM_REGS
+ CPU_NB_REGS
) {
171 if (n
< CPU_NB_REGS32
||
172 (TARGET_LONG_BITS
== 64 && env
->hflags
& HF_CS64_MASK
)) {
173 env
->xmm_regs
[n
].ZMM_Q(0) = ldq_p(mem_buf
);
174 env
->xmm_regs
[n
].ZMM_Q(1) = ldq_p(mem_buf
+ 8);
180 if (TARGET_LONG_BITS
== 64 && env
->hflags
& HF_CS64_MASK
) {
181 env
->eip
= ldq_p(mem_buf
);
184 env
->eip
&= ~0xffffffffUL
;
185 env
->eip
|= (uint32_t)ldl_p(mem_buf
);
189 env
->eflags
= ldl_p(mem_buf
);
193 return x86_cpu_gdb_load_seg(cpu
, R_CS
, mem_buf
);
194 case IDX_SEG_REGS
+ 1:
195 return x86_cpu_gdb_load_seg(cpu
, R_SS
, mem_buf
);
196 case IDX_SEG_REGS
+ 2:
197 return x86_cpu_gdb_load_seg(cpu
, R_DS
, mem_buf
);
198 case IDX_SEG_REGS
+ 3:
199 return x86_cpu_gdb_load_seg(cpu
, R_ES
, mem_buf
);
200 case IDX_SEG_REGS
+ 4:
201 return x86_cpu_gdb_load_seg(cpu
, R_FS
, mem_buf
);
202 case IDX_SEG_REGS
+ 5:
203 return x86_cpu_gdb_load_seg(cpu
, R_GS
, mem_buf
);
205 case IDX_FP_REGS
+ 8:
206 cpu_set_fpuc(env
, ldl_p(mem_buf
));
208 case IDX_FP_REGS
+ 9:
209 tmp
= ldl_p(mem_buf
);
210 env
->fpstt
= (tmp
>> 11) & 7;
211 env
->fpus
= tmp
& ~0x3800;
213 case IDX_FP_REGS
+ 10: /* ftag */
215 case IDX_FP_REGS
+ 11: /* fiseg */
217 case IDX_FP_REGS
+ 12: /* fioff */
219 case IDX_FP_REGS
+ 13: /* foseg */
221 case IDX_FP_REGS
+ 14: /* fooff */
223 case IDX_FP_REGS
+ 15: /* fop */
227 cpu_set_mxcsr(env
, ldl_p(mem_buf
));
231 /* Unrecognised register. */