From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MYGYW-0003UZ-EH for qemu-devel@nongnu.org; Tue, 04 Aug 2009 05:43:40 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MYGYR-0003Rz-Vn for qemu-devel@nongnu.org; Tue, 04 Aug 2009 05:43:39 -0400 Received: from [199.232.76.173] (port=60385 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MYGYR-0003RY-Bl for qemu-devel@nongnu.org; Tue, 04 Aug 2009 05:43:35 -0400 Received: from az33egw02.freescale.net ([192.88.158.103]:36309) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1MYGYQ-00021A-M3 for qemu-devel@nongnu.org; Tue, 04 Aug 2009 05:43:35 -0400 Received: from de01smr01.freescale.net (de01smr01.freescale.net [10.208.0.31]) by az33egw02.freescale.net (8.14.3/az33egw02) with ESMTP id n749fbUX009569 for ; Tue, 4 Aug 2009 02:41:47 -0700 (MST) Received: from zch01exm26.fsl.freescale.net (zch01exm26.ap.freescale.net [10.192.129.221]) by de01smr01.freescale.net (8.13.1/8.13.0) with ESMTP id n749fvhC008366 for ; Tue, 4 Aug 2009 04:42:02 -0500 (CDT) From: Liu Yu Date: Tue, 4 Aug 2009 17:36:06 +0800 Message-Id: <1249378568-18008-4-git-send-email-yu.liu@freescale.com> In-Reply-To: <1249378568-18008-3-git-send-email-yu.liu@freescale.com> References: <1249378568-18008-1-git-send-email-yu.liu@freescale.com> <1249378568-18008-2-git-send-email-yu.liu@freescale.com> <1249378568-18008-3-git-send-email-yu.liu@freescale.com> Subject: [Qemu-devel] [PATCH 3/5] kvmppc: Guest debug support List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: jan.kiszka@siemens.com, froydnj@codesourcery.com, Liu Yu , kvm-ppc@vger.kernel.org, hollisb@us.ibm.com Signed-off-by: Liu Yu --- target-ppc/kvm.c | 203 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 203 insertions(+), 0 deletions(-) diff --git a/target-ppc/kvm.c b/target-ppc/kvm.c index b53d6e9..97a0737 100644 --- a/target-ppc/kvm.c +++ b/target-ppc/kvm.c @@ -8,6 +8,9 @@ * Christian Ehrhardt * Hollis Blanchard * + * Copyright (C) 2009 Freescale Semiconductor, Inc. All rights reserved. + * Yu Liu + * * This work is licensed under the terms of the GNU GPL, version 2 or later. * See the COPYING file in the top-level directory. * @@ -18,6 +21,7 @@ #include #include +#include #include "qemu-common.h" #include "qemu-timer.h" @@ -26,6 +30,7 @@ #include "kvm_ppc.h" #include "cpu.h" #include "device_tree.h" +#include "gdbstub.h" //#define DEBUG_KVM @@ -216,3 +221,201 @@ int kvm_arch_handle_exit(CPUState *env, struct kvm_run *run) return ret; } +#ifdef KVM_CAP_SET_GUEST_DEBUG +int kvm_arch_insert_sw_breakpoint(CPUState *env, struct kvm_sw_breakpoint *bp) +{ + uint32_t sc = tswap32(KVM_INST_GUESTGDB); + uint32_t tmp; + + if (cpu_memory_rw_debug(env, bp->pc, (uint8_t *)&bp->saved_insn, 4, 0) || + cpu_memory_rw_debug(env, bp->pc, (uint8_t *)&sc, 4, 1)) + return -EINVAL; + cpu_memory_rw_debug(env, bp->pc, (uint8_t *)&tmp, 4, 0); + return 0; +} + +int kvm_arch_remove_sw_breakpoint(CPUState *env, struct kvm_sw_breakpoint *bp) +{ + uint32_t sc; + + if (cpu_memory_rw_debug(env, bp->pc, (uint8_t *)&sc, 4, 0) || + sc != tswap32(KVM_INST_GUESTGDB) || + cpu_memory_rw_debug(env, bp->pc, (uint8_t *)&bp->saved_insn, 4, 1)) + return -EINVAL; + return 0; +} + +static struct { + target_ulong addr; + int type; +} hw_breakpoint[6]; + +static int nb_hw_breakpoint; +static int nb_hw_watchpoint; +static int max_hw_breakpoint; +static int max_hw_watchpoint; + +static void kvmppc_debug_init(int max_hw_bp, int max_hw_wp) +{ + max_hw_breakpoint = max_hw_bp > 4? 4 : max_hw_bp; + max_hw_watchpoint = max_hw_wp > 2? 2 : max_hw_wp; +} + +static int find_hw_breakpoint(target_ulong addr, int type) +{ + int n; + + for (n = 0; n < nb_hw_breakpoint + nb_hw_watchpoint; n++) + if (hw_breakpoint[n].addr == addr && hw_breakpoint[n].type == type) + return n; + return -1; +} + +int kvm_arch_insert_hw_breakpoint(target_ulong addr, + target_ulong len, int type) +{ + hw_breakpoint[nb_hw_breakpoint + nb_hw_watchpoint].addr = addr; + hw_breakpoint[nb_hw_breakpoint + nb_hw_watchpoint].type = type; + + switch (type) { + case GDB_BREAKPOINT_HW: + if (nb_hw_breakpoint >= max_hw_breakpoint) + return -ENOBUFS; + + if (find_hw_breakpoint(addr, type) >= 0) + return -EEXIST; + + nb_hw_breakpoint++; + break; + + case GDB_WATCHPOINT_WRITE: + case GDB_WATCHPOINT_READ: + case GDB_WATCHPOINT_ACCESS: + if (nb_hw_watchpoint >= max_hw_watchpoint) + return -ENOBUFS; + + if (find_hw_breakpoint(addr, type) >= 0) + return -EEXIST; + + nb_hw_watchpoint++; + break; + + default: + return -ENOSYS; + } + + return 0; +} + +int kvm_arch_remove_hw_breakpoint(target_ulong addr, + target_ulong len, int type) +{ + int n; + + n = find_hw_breakpoint(addr, type); + if (n < 0) + return -ENOENT; + + switch (type) { + case GDB_BREAKPOINT_HW: + nb_hw_breakpoint--; + break; + + case GDB_WATCHPOINT_WRITE: + case GDB_WATCHPOINT_READ: + case GDB_WATCHPOINT_ACCESS: + nb_hw_watchpoint--; + break; + + default: + return -ENOSYS; + } + hw_breakpoint[n] = hw_breakpoint[nb_hw_breakpoint + nb_hw_watchpoint]; + + return 0; +} + +void kvm_arch_remove_all_hw_breakpoints(void) +{ + nb_hw_breakpoint = nb_hw_watchpoint = 0; +} + +static CPUWatchpoint hw_watchpoint; + +int kvm_arch_debug(struct kvm_debug_exit_arch *arch_info) +{ + int handle = 0; + int n; + + if (cpu_single_env->singlestep_enabled) { + handle = 1; + + } else if (arch_info->status) { + if (arch_info->status & KVMPPC_DEBUG_BREAKPOINT) { + n = find_hw_breakpoint(arch_info->pc, GDB_BREAKPOINT_HW); + if (n >= 0) + handle = 1; + + } else if (arch_info->status & (KVMPPC_DEBUG_WATCH_READ | + KVMPPC_DEBUG_WATCH_WRITE)) { + if ((n = find_hw_breakpoint(arch_info->pc, GDB_WATCHPOINT_ACCESS)) >= 0) { + handle = 1; + cpu_single_env->watchpoint_hit = &hw_watchpoint; + hw_watchpoint.vaddr = hw_breakpoint[n].addr; + hw_watchpoint.flags = BP_MEM_ACCESS; + } else if ((n = find_hw_breakpoint(arch_info->pc, GDB_WATCHPOINT_WRITE) >= 0)) { + handle = 1; + cpu_single_env->watchpoint_hit = &hw_watchpoint; + hw_watchpoint.vaddr = hw_breakpoint[n].addr; + hw_watchpoint.flags = BP_MEM_WRITE; + } else if ((n = find_hw_breakpoint(arch_info->pc, GDB_WATCHPOINT_READ) >= 0)) { + handle = 1; + cpu_single_env->watchpoint_hit = &hw_watchpoint; + hw_watchpoint.vaddr = hw_breakpoint[n].addr; + hw_watchpoint.flags = BP_MEM_READ; + } + } + + } else if (kvm_find_sw_breakpoint(cpu_single_env, arch_info->pc)) + handle = 1; + + /* XXX inject guest debug exception */ + if (!handle) + printf("Unhandled debug exception!\n"); + + return handle; +} + +void kvm_arch_update_guest_debug(CPUState *env, struct kvm_guest_debug *dbg) +{ + if (kvm_sw_breakpoints_active(env)) + dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP; + + if (nb_hw_breakpoint + nb_hw_watchpoint > 0) { + int n; + + dbg->control |= KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP; + memset(dbg->arch.bp, 0, sizeof(dbg->arch.bp)); + for (n = 0; n < nb_hw_breakpoint + nb_hw_watchpoint; n++) { + switch (hw_breakpoint[n].type) { + case GDB_BREAKPOINT_HW: + dbg->arch.bp[n].type = KVMPPC_DEBUG_BREAKPOINT; + break; + case GDB_WATCHPOINT_WRITE: + dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_WRITE; + break; + case GDB_WATCHPOINT_READ: + dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_READ; + break; + case GDB_WATCHPOINT_ACCESS: + dbg->arch.bp[n].type = KVMPPC_DEBUG_WATCH_WRITE | KVMPPC_DEBUG_WATCH_READ; + break; + default: + printf("Unsupported breakpoint type\n"); + exit(-1); + } + dbg->arch.bp[n].addr = hw_breakpoint[n].addr; + } + } +} +#endif /* KVM_CAP_SET_GUEST_DEBUG */ -- 1.5.4