From nobody Sat May 4 18:05:09 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linux.ibm.com Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1542663626869556.5872249037083; Mon, 19 Nov 2018 13:40:26 -0800 (PST) Received: from localhost ([::1]:59103 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gOrHG-00012k-Iu for importer@patchew.org; Mon, 19 Nov 2018 16:40:18 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37339) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gOrEz-00089c-75 for qemu-devel@nongnu.org; Mon, 19 Nov 2018 16:37:58 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gOrEw-0004Sx-1T for qemu-devel@nongnu.org; Mon, 19 Nov 2018 16:37:57 -0500 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:45944) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gOrEv-0004SR-Pd for qemu-devel@nongnu.org; Mon, 19 Nov 2018 16:37:53 -0500 Received: from pps.filterd (m0098396.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id wAJLXd6L106651 for ; Mon, 19 Nov 2018 16:37:53 -0500 Received: from e33.co.us.ibm.com (e33.co.us.ibm.com [32.97.110.151]) by mx0a-001b2d01.pphosted.com with ESMTP id 2nv0pqbmtx-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 19 Nov 2018 16:37:52 -0500 Received: from localhost by e33.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 19 Nov 2018 21:37:52 -0000 Received: from b03cxnp08027.gho.boulder.ibm.com (9.17.130.19) by e33.co.us.ibm.com (192.168.1.133) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Mon, 19 Nov 2018 21:37:50 -0000 Received: from b03ledav005.gho.boulder.ibm.com (b03ledav005.gho.boulder.ibm.com [9.17.130.236]) by b03cxnp08027.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id wAJLbnnY29884540 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Mon, 19 Nov 2018 21:37:49 GMT Received: from b03ledav005.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 1F197BE053; Mon, 19 Nov 2018 21:37:49 +0000 (GMT) Received: from b03ledav005.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id F27CFBE051; Mon, 19 Nov 2018 21:37:47 +0000 (GMT) Received: from farosas.linux.ibm.com.ibmmodules.com (unknown [9.85.157.103]) by b03ledav005.gho.boulder.ibm.com (Postfix) with ESMTP; Mon, 19 Nov 2018 21:37:47 +0000 (GMT) From: Fabiano Rosas To: qemu-devel@nongnu.org Date: Mon, 19 Nov 2018 19:37:39 -0200 X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181119213739.773-1-farosas@linux.ibm.com> References: <20181119213739.773-1-farosas@linux.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 18111921-0036-0000-0000-00000A5D8094 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00010083; HX=3.00000242; KW=3.00000007; PH=3.00000004; SC=3.00000270; SDB=6.01119873; UDB=6.00581058; IPR=6.00899994; MB=3.00024237; MTD=3.00000008; XFM=3.00000015; UTC=2018-11-19 21:37:50 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18111921-0037-0000-0000-000049B09192 Message-Id: <20181119213739.773-2-farosas@linux.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2018-11-19_08:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1811190192 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy] X-Received-From: 148.163.156.1 Subject: [Qemu-devel] [RFC PATCH 1/1] target/ppc: support single stepping with KVM HV X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The hardware singlestep mechanism in POWER works via a Trace Interrupt (0xd00) that happens after any instruction executes, whenever MSR_SE =3D 1 (PowerISA Section 6.5.15 - Trace Interrupt). However, with kvm_hv, the Trace Interrupt happens inside the guest and KVM has no visibility of it. Therefore, when the gdbstub uses the KVM_SET_GUEST_DEBUG ioctl to enable singlestep, KVM simply ignores it. This patch takes advantage of the Trace Interrupt to perform the step inside the guest, but uses a breakpoint at the Trace Interrupt handler to return control to KVM. The exit is treated by KVM as a regular breakpoint and it returns to the host (and QEMU eventually). Before signalling GDB, QEMU sets the Next Instruction Pointer to the instruction following the one being stepped, effectively skipping the interrupt handler execution and hiding the trace interrupt breakpoint from GDB. This approach works with both of GDB's 'scheduler-locking' options (off, step). Signed-off-by: Fabiano Rosas --- accel/kvm/kvm-all.c | 10 +++++++ exec.c | 1 + include/sysemu/kvm.h | 4 +++ target/arm/kvm.c | 4 +++ target/i386/kvm.c | 4 +++ target/ppc/kvm.c | 65 +++++++++++++++++++++++++++++++++++++++++++- target/s390x/kvm.c | 4 +++ 7 files changed, 91 insertions(+), 1 deletion(-) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 4880a05399..4fb7199a15 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -2313,6 +2313,11 @@ int kvm_update_guest_debug(CPUState *cpu, unsigned l= ong reinject_trap) return data.err; } =20 +void kvm_set_singlestep(CPUState *cs, int enabled) +{ + kvm_arch_set_singlestep(cs, enabled); +} + int kvm_insert_breakpoint(CPUState *cpu, target_ulong addr, target_ulong len, int type) { @@ -2439,6 +2444,11 @@ int kvm_remove_breakpoint(CPUState *cpu, target_ulon= g addr, void kvm_remove_all_breakpoints(CPUState *cpu) { } + +void kvm_set_singlestep(CPUState *cs, int enabled) +{ +} + #endif /* !KVM_CAP_SET_GUEST_DEBUG */ =20 static int kvm_set_signal_mask(CPUState *cpu, const sigset_t *sigset) diff --git a/exec.c b/exec.c index bb6170dbff..55614822c3 100644 --- a/exec.c +++ b/exec.c @@ -1233,6 +1233,7 @@ void cpu_single_step(CPUState *cpu, int enabled) if (cpu->singlestep_enabled !=3D enabled) { cpu->singlestep_enabled =3D enabled; if (kvm_enabled()) { + kvm_set_singlestep(cpu, enabled); kvm_update_guest_debug(cpu, 0); } else { /* must flush all the translated code to avoid inconsistencies= */ diff --git a/include/sysemu/kvm.h b/include/sysemu/kvm.h index 97d8d9d0d5..a01a8d58dd 100644 --- a/include/sysemu/kvm.h +++ b/include/sysemu/kvm.h @@ -259,6 +259,8 @@ int kvm_remove_breakpoint(CPUState *cpu, target_ulong a= ddr, void kvm_remove_all_breakpoints(CPUState *cpu); int kvm_update_guest_debug(CPUState *cpu, unsigned long reinject_trap); =20 +void kvm_set_singlestep(CPUState *cpu, int enabled); + int kvm_on_sigbus_vcpu(CPUState *cpu, int code, void *addr); int kvm_on_sigbus(int code, void *addr); =20 @@ -431,6 +433,8 @@ void kvm_arch_remove_all_hw_breakpoints(void); =20 void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *db= g); =20 +void kvm_arch_set_singlestep(CPUState *cpu, int enabled); + bool kvm_arch_stop_on_emulation_error(CPUState *cpu); =20 int kvm_check_extension(KVMState *s, unsigned int extension); diff --git a/target/arm/kvm.c b/target/arm/kvm.c index 44dd0ce6ce..dd8e43ab7e 100644 --- a/target/arm/kvm.c +++ b/target/arm/kvm.c @@ -670,6 +670,10 @@ int kvm_arch_process_async_events(CPUState *cs) return 0; } =20 +void kvm_arch_set_singlestep(CPUState *cs, int enabled) +{ +} + /* The #ifdef protections are until 32bit headers are imported and can * be removed once both 32 and 64 bit reach feature parity. */ diff --git a/target/i386/kvm.c b/target/i386/kvm.c index f524e7d929..ba56f2ee1f 100644 --- a/target/i386/kvm.c +++ b/target/i386/kvm.c @@ -3521,6 +3521,10 @@ static int kvm_handle_debug(X86CPU *cpu, return ret; } =20 +void kvm_arch_set_singlestep(CPUState *cs, int enabled) +{ +} + void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *db= g) { const uint8_t type_code[] =3D { diff --git a/target/ppc/kvm.c b/target/ppc/kvm.c index f81327d6cd..3af5e91997 100644 --- a/target/ppc/kvm.c +++ b/target/ppc/kvm.c @@ -94,6 +94,7 @@ static int cap_ppc_safe_indirect_branch; static int cap_ppc_nested_kvm_hv; =20 static uint32_t debug_inst_opcode; +static target_ulong trace_handler_addr; =20 /* XXX We have a race condition where we actually have a level triggered * interrupt, but the infrastructure can't expose that yet, so the gue= st @@ -509,6 +510,9 @@ int kvm_arch_init_vcpu(CPUState *cs) kvm_get_one_reg(cs, KVM_REG_PPC_DEBUG_INST, &debug_inst_opcode); kvmppc_hw_debug_points_init(cenv); =20 + trace_handler_addr =3D (cenv->excp_vectors[POWERPC_EXCP_TRACE] | + 0xc000000000004000ull); + return ret; } =20 @@ -1551,6 +1555,28 @@ void kvm_arch_remove_all_hw_breakpoints(void) nb_hw_breakpoint =3D nb_hw_watchpoint =3D 0; } =20 +void kvm_arch_set_singlestep(CPUState *cs, int enabled) +{ + PowerPCCPU *cpu =3D POWERPC_CPU(cs); + CPUPPCState *env =3D &cpu->env; + + if (kvmppc_is_pr(kvm_state)) { + return; + } + + if (enabled) { + /* MSR_SE =3D 1 will cause a Trace Interrupt in the guest after + * the next instruction executes. */ + env->msr |=3D (1ULL << MSR_SE); + + /* We set a breakpoint at the interrupt handler address so + * that the singlestep will be seen by KVM (this is treated by + * KVM like an ordinary breakpoint) and control is returned to + * QEMU. */ + kvm_insert_breakpoint(cs, trace_handler_addr, 4, GDB_BREAKPOINT_SW= ); + } +} + void kvm_arch_update_guest_debug(CPUState *cs, struct kvm_guest_debug *dbg) { int n; @@ -1590,6 +1616,43 @@ void kvm_arch_update_guest_debug(CPUState *cs, struc= t kvm_guest_debug *dbg) } } =20 +static int kvm_handle_singlestep(CPUState *cs, + struct kvm_debug_exit_arch *arch_info) +{ + PowerPCCPU *cpu =3D POWERPC_CPU(cs); + CPUPPCState *env =3D &cpu->env; + target_ulong msr =3D env->msr; + uint32_t insn; + int ret =3D 1; + int reg; + + if (kvmppc_is_pr(kvm_state)) { + return ret; + } + + if (arch_info->address =3D=3D trace_handler_addr) { + cpu_synchronize_state(cs); + kvm_remove_breakpoint(cs, trace_handler_addr, 4, GDB_BREAKPOINT_SW= ); + + cpu_memory_rw_debug(cs, env->spr[SPR_SRR0] - 4, (uint8_t *)&insn, + sizeof(insn), 0); + + /* If the last instruction was a mfmsr, make sure that the + * MSR_SE bit is not set to avoid the guest kernel knowing + * that it is being single-stepped */ + if (((insn >> 26) & 0x3f) =3D=3D 31 && ((insn >> 1) & 0x3ff) =3D= =3D 83) { + reg =3D (insn >> 21) & 0x1f; + env->gpr[reg] &=3D ~(1ULL << MSR_SE); + } + + env->nip =3D env->spr[SPR_SRR0]; + env->msr =3D msr &=3D ~(1ULL << MSR_SE); + cpu_synchronize_state(cs); + } + + return ret; +} + static int kvm_handle_debug(PowerPCCPU *cpu, struct kvm_run *run) { CPUState *cs =3D CPU(cpu); @@ -1600,7 +1663,7 @@ static int kvm_handle_debug(PowerPCCPU *cpu, struct k= vm_run *run) int flag =3D 0; =20 if (cs->singlestep_enabled) { - handle =3D 1; + handle =3D kvm_handle_singlestep(cs, arch_info); } else if (arch_info->status) { if (nb_hw_breakpoint + nb_hw_watchpoint > 0) { if (arch_info->status & KVMPPC_DEBUG_BREAKPOINT) { diff --git a/target/s390x/kvm.c b/target/s390x/kvm.c index 2ebf26adfe..4bde183458 100644 --- a/target/s390x/kvm.c +++ b/target/s390x/kvm.c @@ -975,6 +975,10 @@ void kvm_arch_remove_all_hw_breakpoints(void) hw_breakpoints =3D NULL; } =20 +void kvm_arch_set_singlestep(CPUState *cs, int enabled) +{ +} + void kvm_arch_update_guest_debug(CPUState *cpu, struct kvm_guest_debug *db= g) { int i; --=20 2.17.1