2013-07-07 12:07:54 +02:00
|
|
|
/*
|
|
|
|
* x86 gdb server stub
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003-2005 Fabrice Bellard
|
|
|
|
* Copyright (c) 2013 SUSE LINUX Products GmbH
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2016-01-26 19:17:03 +01:00
|
|
|
#include "qemu/osdep.h"
|
2013-06-29 04:18:45 +02:00
|
|
|
#include "qemu-common.h"
|
2016-03-15 16:58:45 +01:00
|
|
|
#include "cpu.h"
|
2013-06-29 04:18:45 +02:00
|
|
|
#include "exec/gdbstub.h"
|
2013-07-07 12:07:54 +02:00
|
|
|
|
|
|
|
#ifdef TARGET_X86_64
|
|
|
|
static const int gpr_map[16] = {
|
|
|
|
R_EAX, R_EBX, R_ECX, R_EDX, R_ESI, R_EDI, R_EBP, R_ESP,
|
|
|
|
8, 9, 10, 11, 12, 13, 14, 15
|
|
|
|
};
|
|
|
|
#else
|
|
|
|
#define gpr_map gpr_map32
|
|
|
|
#endif
|
|
|
|
static const int gpr_map32[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
|
|
|
|
|
|
|
|
#define IDX_IP_REG CPU_NB_REGS
|
|
|
|
#define IDX_FLAGS_REG (IDX_IP_REG + 1)
|
|
|
|
#define IDX_SEG_REGS (IDX_FLAGS_REG + 1)
|
|
|
|
#define IDX_FP_REGS (IDX_SEG_REGS + 6)
|
|
|
|
#define IDX_XMM_REGS (IDX_FP_REGS + 16)
|
|
|
|
#define IDX_MXCSR_REG (IDX_XMM_REGS + CPU_NB_REGS)
|
|
|
|
|
2013-06-29 04:18:45 +02:00
|
|
|
int x86_cpu_gdb_read_register(CPUState *cs, uint8_t *mem_buf, int n)
|
2013-07-07 12:07:54 +02:00
|
|
|
{
|
2013-06-29 04:18:45 +02:00
|
|
|
X86CPU *cpu = X86_CPU(cs);
|
|
|
|
CPUX86State *env = &cpu->env;
|
|
|
|
|
2016-11-04 00:35:32 +01:00
|
|
|
/* N.B. GDB can't deal with changes in registers or sizes in the middle
|
|
|
|
of a session. So if we're in 32-bit mode on a 64-bit cpu, still act
|
|
|
|
as if we're on a 64-bit cpu. */
|
|
|
|
|
2013-07-07 12:07:54 +02:00
|
|
|
if (n < CPU_NB_REGS) {
|
2016-11-04 00:35:32 +01:00
|
|
|
if (TARGET_LONG_BITS == 64) {
|
|
|
|
if (env->hflags & HF_CS64_MASK) {
|
|
|
|
return gdb_get_reg64(mem_buf, env->regs[gpr_map[n]]);
|
|
|
|
} else if (n < CPU_NB_REGS32) {
|
|
|
|
return gdb_get_reg64(mem_buf,
|
|
|
|
env->regs[gpr_map[n]] & 0xffffffffUL);
|
|
|
|
} else {
|
|
|
|
memset(mem_buf, 0, sizeof(target_ulong));
|
|
|
|
return sizeof(target_ulong);
|
|
|
|
}
|
|
|
|
} else {
|
2013-07-07 13:05:05 +02:00
|
|
|
return gdb_get_reg32(mem_buf, env->regs[gpr_map32[n]]);
|
2013-07-07 12:07:54 +02:00
|
|
|
}
|
|
|
|
} else if (n >= IDX_FP_REGS && n < IDX_FP_REGS + 8) {
|
|
|
|
#ifdef USE_X86LDOUBLE
|
|
|
|
/* FIXME: byteswap float values - after fixing fpregs layout. */
|
|
|
|
memcpy(mem_buf, &env->fpregs[n - IDX_FP_REGS], 10);
|
|
|
|
#else
|
|
|
|
memset(mem_buf, 0, 10);
|
|
|
|
#endif
|
|
|
|
return 10;
|
|
|
|
} else if (n >= IDX_XMM_REGS && n < IDX_XMM_REGS + CPU_NB_REGS) {
|
|
|
|
n -= IDX_XMM_REGS;
|
2016-11-04 00:35:32 +01:00
|
|
|
if (n < CPU_NB_REGS32 || TARGET_LONG_BITS == 64) {
|
2015-11-26 20:14:32 +01:00
|
|
|
stq_p(mem_buf, env->xmm_regs[n].ZMM_Q(0));
|
|
|
|
stq_p(mem_buf + 8, env->xmm_regs[n].ZMM_Q(1));
|
2013-07-07 12:07:54 +02:00
|
|
|
return 16;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (n) {
|
|
|
|
case IDX_IP_REG:
|
2016-11-04 00:35:32 +01:00
|
|
|
if (TARGET_LONG_BITS == 64) {
|
|
|
|
if (env->hflags & HF_CS64_MASK) {
|
|
|
|
return gdb_get_reg64(mem_buf, env->eip);
|
|
|
|
} else {
|
|
|
|
return gdb_get_reg64(mem_buf, env->eip & 0xffffffffUL);
|
|
|
|
}
|
2013-07-07 12:07:54 +02:00
|
|
|
} else {
|
2013-07-07 13:05:05 +02:00
|
|
|
return gdb_get_reg32(mem_buf, env->eip);
|
2013-07-07 12:07:54 +02:00
|
|
|
}
|
|
|
|
case IDX_FLAGS_REG:
|
2013-07-07 13:05:05 +02:00
|
|
|
return gdb_get_reg32(mem_buf, env->eflags);
|
2013-07-07 12:07:54 +02:00
|
|
|
|
|
|
|
case IDX_SEG_REGS:
|
2013-07-07 13:05:05 +02:00
|
|
|
return gdb_get_reg32(mem_buf, env->segs[R_CS].selector);
|
2013-07-07 12:07:54 +02:00
|
|
|
case IDX_SEG_REGS + 1:
|
2013-07-07 13:05:05 +02:00
|
|
|
return gdb_get_reg32(mem_buf, env->segs[R_SS].selector);
|
2013-07-07 12:07:54 +02:00
|
|
|
case IDX_SEG_REGS + 2:
|
2013-07-07 13:05:05 +02:00
|
|
|
return gdb_get_reg32(mem_buf, env->segs[R_DS].selector);
|
2013-07-07 12:07:54 +02:00
|
|
|
case IDX_SEG_REGS + 3:
|
2013-07-07 13:05:05 +02:00
|
|
|
return gdb_get_reg32(mem_buf, env->segs[R_ES].selector);
|
2013-07-07 12:07:54 +02:00
|
|
|
case IDX_SEG_REGS + 4:
|
2013-07-07 13:05:05 +02:00
|
|
|
return gdb_get_reg32(mem_buf, env->segs[R_FS].selector);
|
2013-07-07 12:07:54 +02:00
|
|
|
case IDX_SEG_REGS + 5:
|
2013-07-07 13:05:05 +02:00
|
|
|
return gdb_get_reg32(mem_buf, env->segs[R_GS].selector);
|
2013-07-07 12:07:54 +02:00
|
|
|
|
|
|
|
case IDX_FP_REGS + 8:
|
2013-07-07 13:05:05 +02:00
|
|
|
return gdb_get_reg32(mem_buf, env->fpuc);
|
2013-07-07 12:07:54 +02:00
|
|
|
case IDX_FP_REGS + 9:
|
2013-07-07 13:05:05 +02:00
|
|
|
return gdb_get_reg32(mem_buf, (env->fpus & ~0x3800) |
|
|
|
|
(env->fpstt & 0x7) << 11);
|
2013-07-07 12:07:54 +02:00
|
|
|
case IDX_FP_REGS + 10:
|
2013-07-07 13:05:05 +02:00
|
|
|
return gdb_get_reg32(mem_buf, 0); /* ftag */
|
2013-07-07 12:07:54 +02:00
|
|
|
case IDX_FP_REGS + 11:
|
2013-07-07 13:05:05 +02:00
|
|
|
return gdb_get_reg32(mem_buf, 0); /* fiseg */
|
2013-07-07 12:07:54 +02:00
|
|
|
case IDX_FP_REGS + 12:
|
2013-07-07 13:05:05 +02:00
|
|
|
return gdb_get_reg32(mem_buf, 0); /* fioff */
|
2013-07-07 12:07:54 +02:00
|
|
|
case IDX_FP_REGS + 13:
|
2013-07-07 13:05:05 +02:00
|
|
|
return gdb_get_reg32(mem_buf, 0); /* foseg */
|
2013-07-07 12:07:54 +02:00
|
|
|
case IDX_FP_REGS + 14:
|
2013-07-07 13:05:05 +02:00
|
|
|
return gdb_get_reg32(mem_buf, 0); /* fooff */
|
2013-07-07 12:07:54 +02:00
|
|
|
case IDX_FP_REGS + 15:
|
2013-07-07 13:05:05 +02:00
|
|
|
return gdb_get_reg32(mem_buf, 0); /* fop */
|
2013-07-07 12:07:54 +02:00
|
|
|
|
|
|
|
case IDX_MXCSR_REG:
|
2013-07-07 13:05:05 +02:00
|
|
|
return gdb_get_reg32(mem_buf, env->mxcsr);
|
2013-07-07 12:07:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-29 04:18:45 +02:00
|
|
|
static int x86_cpu_gdb_load_seg(X86CPU *cpu, int sreg, uint8_t *mem_buf)
|
2013-07-07 12:07:54 +02:00
|
|
|
{
|
2013-06-29 04:18:45 +02:00
|
|
|
CPUX86State *env = &cpu->env;
|
2013-07-07 12:07:54 +02:00
|
|
|
uint16_t selector = ldl_p(mem_buf);
|
|
|
|
|
|
|
|
if (selector != env->segs[sreg].selector) {
|
|
|
|
#if defined(CONFIG_USER_ONLY)
|
|
|
|
cpu_x86_load_seg(env, sreg, selector);
|
|
|
|
#else
|
|
|
|
unsigned int limit, flags;
|
|
|
|
target_ulong base;
|
|
|
|
|
|
|
|
if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) {
|
2014-05-15 16:07:04 +02:00
|
|
|
int dpl = (env->eflags & VM_MASK) ? 3 : 0;
|
2013-07-07 12:07:54 +02:00
|
|
|
base = selector << 4;
|
|
|
|
limit = 0xffff;
|
2014-05-15 16:07:04 +02:00
|
|
|
flags = DESC_P_MASK | DESC_S_MASK | DESC_W_MASK |
|
|
|
|
DESC_A_MASK | (dpl << DESC_DPL_SHIFT);
|
2013-07-07 12:07:54 +02:00
|
|
|
} else {
|
|
|
|
if (!cpu_x86_get_descr_debug(env, selector, &base, &limit,
|
|
|
|
&flags)) {
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cpu_x86_load_seg_cache(env, sreg, selector, base, limit, flags);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
2013-06-29 04:18:45 +02:00
|
|
|
int x86_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
|
2013-07-07 12:07:54 +02:00
|
|
|
{
|
2013-06-29 04:18:45 +02:00
|
|
|
X86CPU *cpu = X86_CPU(cs);
|
|
|
|
CPUX86State *env = &cpu->env;
|
2013-07-07 12:07:54 +02:00
|
|
|
uint32_t tmp;
|
|
|
|
|
2016-11-04 00:35:32 +01:00
|
|
|
/* N.B. GDB can't deal with changes in registers or sizes in the middle
|
|
|
|
of a session. So if we're in 32-bit mode on a 64-bit cpu, still act
|
|
|
|
as if we're on a 64-bit cpu. */
|
|
|
|
|
2013-07-07 12:07:54 +02:00
|
|
|
if (n < CPU_NB_REGS) {
|
2016-11-04 00:35:32 +01:00
|
|
|
if (TARGET_LONG_BITS == 64) {
|
|
|
|
if (env->hflags & HF_CS64_MASK) {
|
|
|
|
env->regs[gpr_map[n]] = ldtul_p(mem_buf);
|
|
|
|
} else if (n < CPU_NB_REGS32) {
|
|
|
|
env->regs[gpr_map[n]] = ldtul_p(mem_buf) & 0xffffffffUL;
|
|
|
|
}
|
2013-07-07 12:07:54 +02:00
|
|
|
return sizeof(target_ulong);
|
|
|
|
} else if (n < CPU_NB_REGS32) {
|
|
|
|
n = gpr_map32[n];
|
|
|
|
env->regs[n] &= ~0xffffffffUL;
|
|
|
|
env->regs[n] |= (uint32_t)ldl_p(mem_buf);
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
} else if (n >= IDX_FP_REGS && n < IDX_FP_REGS + 8) {
|
|
|
|
#ifdef USE_X86LDOUBLE
|
|
|
|
/* FIXME: byteswap float values - after fixing fpregs layout. */
|
|
|
|
memcpy(&env->fpregs[n - IDX_FP_REGS], mem_buf, 10);
|
|
|
|
#endif
|
|
|
|
return 10;
|
|
|
|
} else if (n >= IDX_XMM_REGS && n < IDX_XMM_REGS + CPU_NB_REGS) {
|
|
|
|
n -= IDX_XMM_REGS;
|
2016-11-04 00:35:32 +01:00
|
|
|
if (n < CPU_NB_REGS32 || TARGET_LONG_BITS == 64) {
|
2015-11-26 20:14:32 +01:00
|
|
|
env->xmm_regs[n].ZMM_Q(0) = ldq_p(mem_buf);
|
|
|
|
env->xmm_regs[n].ZMM_Q(1) = ldq_p(mem_buf + 8);
|
2013-07-07 12:07:54 +02:00
|
|
|
return 16;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (n) {
|
|
|
|
case IDX_IP_REG:
|
2016-11-04 00:35:32 +01:00
|
|
|
if (TARGET_LONG_BITS == 64) {
|
|
|
|
if (env->hflags & HF_CS64_MASK) {
|
|
|
|
env->eip = ldq_p(mem_buf);
|
|
|
|
} else {
|
|
|
|
env->eip = ldq_p(mem_buf) & 0xffffffffUL;
|
|
|
|
}
|
2013-07-07 12:07:54 +02:00
|
|
|
return 8;
|
|
|
|
} else {
|
|
|
|
env->eip &= ~0xffffffffUL;
|
|
|
|
env->eip |= (uint32_t)ldl_p(mem_buf);
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
case IDX_FLAGS_REG:
|
|
|
|
env->eflags = ldl_p(mem_buf);
|
|
|
|
return 4;
|
|
|
|
|
|
|
|
case IDX_SEG_REGS:
|
2013-06-29 04:18:45 +02:00
|
|
|
return x86_cpu_gdb_load_seg(cpu, R_CS, mem_buf);
|
2013-07-07 12:07:54 +02:00
|
|
|
case IDX_SEG_REGS + 1:
|
2013-06-29 04:18:45 +02:00
|
|
|
return x86_cpu_gdb_load_seg(cpu, R_SS, mem_buf);
|
2013-07-07 12:07:54 +02:00
|
|
|
case IDX_SEG_REGS + 2:
|
2013-06-29 04:18:45 +02:00
|
|
|
return x86_cpu_gdb_load_seg(cpu, R_DS, mem_buf);
|
2013-07-07 12:07:54 +02:00
|
|
|
case IDX_SEG_REGS + 3:
|
2013-06-29 04:18:45 +02:00
|
|
|
return x86_cpu_gdb_load_seg(cpu, R_ES, mem_buf);
|
2013-07-07 12:07:54 +02:00
|
|
|
case IDX_SEG_REGS + 4:
|
2013-06-29 04:18:45 +02:00
|
|
|
return x86_cpu_gdb_load_seg(cpu, R_FS, mem_buf);
|
2013-07-07 12:07:54 +02:00
|
|
|
case IDX_SEG_REGS + 5:
|
2013-06-29 04:18:45 +02:00
|
|
|
return x86_cpu_gdb_load_seg(cpu, R_GS, mem_buf);
|
2013-07-07 12:07:54 +02:00
|
|
|
|
|
|
|
case IDX_FP_REGS + 8:
|
2014-09-17 10:05:19 +02:00
|
|
|
cpu_set_fpuc(env, ldl_p(mem_buf));
|
2013-07-07 12:07:54 +02:00
|
|
|
return 4;
|
|
|
|
case IDX_FP_REGS + 9:
|
|
|
|
tmp = ldl_p(mem_buf);
|
|
|
|
env->fpstt = (tmp >> 11) & 7;
|
|
|
|
env->fpus = tmp & ~0x3800;
|
|
|
|
return 4;
|
|
|
|
case IDX_FP_REGS + 10: /* ftag */
|
|
|
|
return 4;
|
|
|
|
case IDX_FP_REGS + 11: /* fiseg */
|
|
|
|
return 4;
|
|
|
|
case IDX_FP_REGS + 12: /* fioff */
|
|
|
|
return 4;
|
|
|
|
case IDX_FP_REGS + 13: /* foseg */
|
|
|
|
return 4;
|
|
|
|
case IDX_FP_REGS + 14: /* fooff */
|
|
|
|
return 4;
|
|
|
|
case IDX_FP_REGS + 15: /* fop */
|
|
|
|
return 4;
|
|
|
|
|
|
|
|
case IDX_MXCSR_REG:
|
2014-02-24 23:59:54 +01:00
|
|
|
cpu_set_mxcsr(env, ldl_p(mem_buf));
|
2013-07-07 12:07:54 +02:00
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Unrecognised register. */
|
|
|
|
return 0;
|
|
|
|
}
|