RISC-V: KVM: Guest Debug Support - Software Breakpoint Part
Subsystems (4)
v2 cover RISC-V: KVM: Guest Debug Support - Software Breakpoint Part
This series implements the ?KVM Guset Debug? feature on RISC-V. This is an existing feature which is already supported by some other arches. It allows us to debug a RISC-V KVM guest from GDB in host side.
As the first stage, the software breakpoints (ebreak instruction) is implemented. HW breakpoints support will come later after a synthetically consideration with the SBI debug trigger extension.
A selftest case was added in this series. Manual test was done on QEMU RISC-V hypervisor emulator. (add '-s' to enable the gdbserver in QEMU)
This series is based on Linux 6.8-rc6 and also available at:
https://github.com/Du-Chao/kvm-riscv/tree/guest_debug_sw_v2
The matched QEMU is available at:
https://github.com/Du-Chao/qemu/tree/riscv_gd_sw
Please note that if Paolo's change
https://lore.kernel.org/kvm/20240131233056.10845-8-pbonzini at redhat.com/ is adopted, then we can keep 'arch/riscv/include/uapi/asm/kvm.h' untouched.
Changes from v1->v2:
- Rebased on Linux 6.8-rc6.
- Maintain a hedeleg in "struct kvm_vcpu_config" for each VCPU.
- Update the HEDELEG csr in kvm_arch_vcpu_load().
Changes from RFC->v1:
- Rebased on Linux 6.8-rc2.
- Merge PATCH1 and PATCH2 into one patch.
- kselftest case added.
v1 link:
https://lore.kernel.org/kvm/20240206074931.22930-1-duchao at eswincomputing.com RFC link:
https://lore.kernel.org/kvm/20231221095002.7404-1-duchao at eswincomputing.com
Chao Du (3):
RISC-V: KVM: Implement kvm_arch_vcpu_ioctl_set_guest_debug()
RISC-V: KVM: Handle breakpoint exits for VCPU
RISC-V: KVM: selftests: Add breakpoints test support
arch/riscv/include/asm/kvm_host.h | 17 +++++++ arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kvm/main.c | 18 +------ arch/riscv/kvm/vcpu.c | 15 +++++- arch/riscv/kvm/vcpu_exit.c | 4 ++ arch/riscv/kvm/vm.c | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/riscv/breakpoints.c | 49 +++++++++++++++++++ 8 files changed, 88 insertions(+), 18 deletions(-) create mode 100644 tools/testing/selftests/kvm/riscv/breakpoints.c
--
2.17.1
PATCH v2 1/3 RISC-V: KVM: Implement kvm_arch_vcpu_ioctl_set_guest_debug()
kvm_vm_ioctl_check_extension(): Return 1 if KVM_CAP_SET_GUEST_DEBUG is been checked.
kvm_arch_vcpu_ioctl_set_guest_debug(): Update the guest_debug flags from userspace accordingly. Route the breakpoint exceptions to HS mode if the VCPU is being debugged by userspace, by clearing the corresponding bit in hedeleg. Write the actual CSR in kvm_arch_vcpu_load().
--- arch/riscv/include/asm/kvm_host.h | 17 +++++++++++++++++ arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kvm/main.c | 18 ++---------------- arch/riscv/kvm/vcpu.c | 15 +++++++++++++-- arch/riscv/kvm/vm.c | 1 + 5 files changed, 34 insertions(+), 18 deletions(-)
arch/riscv/include/asm/kvm_host.h +17 -0
@@ -43,6 +43,22 @@ KVM_ARCH_REQ_FLAGS(5, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)#define KVM_REQ_STEAL_UPDATE KVM_ARCH_REQ(6) +#define KVM_HEDELEG_DEFAULT ((_AC(1, UL) << EXC_INST_MISALIGNED) | \ + (_AC(1, UL) << EXC_BREAKPOINT) | \ + (_AC(1, UL) << EXC_SYSCALL) | \ + (_AC(1, UL) << EXC_INST_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_LOAD_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_STORE_PAGE_FAULT)) +#define KVM_HEDELEG_GUEST_DEBUG ((_AC(1, UL) << EXC_INST_MISALIGNED) | \ + (_AC(1, UL) << EXC_SYSCALL) | \ + (_AC(1, UL) << EXC_INST_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_LOAD_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_STORE_PAGE_FAULT)) + +#define KVM_HIDELEG_DEFAULT ((_AC(1, UL) << IRQ_VS_SOFT) | \ + (_AC(1, UL) << IRQ_VS_TIMER) | \ + (_AC(1, UL) << IRQ_VS_EXT)) +enum kvm_riscv_hfence_type {KVM_RISCV_HFENCE_UNKNOWN =0 ,KVM_RISCV_HFENCE_GVMA_VMID_GPA ,
@@ -169,6 +185,7 @@ struct kvm_vcpu_csr {struct kvm_vcpu_config {u64 henvcfg;u64 hstateen0; +unsigned long hedeleg; };struct kvm_vcpu_smstateen_csr {
arch/riscv/include/uapi/asm/kvm.h +1 -0
@@ -17,6 +17,7 @@#define __KVM_HAVE_IRQ_LINE#define __KVM_HAVE_READONLY_MEM +#define __KVM_HAVE_GUEST_DEBUG#define KVM_COALESCED_MMIO_PAGE_OFFSET 1
arch/riscv/kvm/main.c +2 -16
@@ -22,22 +22,8 @@ long kvm_arch_dev_ioctl(struct file *filp,int kvm_arch_hardware_enable (void ) { -unsigned long hideleg, hedeleg; - - hedeleg =0 ; - hedeleg |= (1UL <<EXC_INST_MISALIGNED ); - hedeleg |= (1UL <<EXC_BREAKPOINT ); - hedeleg |= (1UL <<EXC_SYSCALL ); - hedeleg |= (1UL <<EXC_INST_PAGE_FAULT ); - hedeleg |= (1UL <<EXC_LOAD_PAGE_FAULT ); - hedeleg |= (1UL <<EXC_STORE_PAGE_FAULT ); -csr_write (CSR_HEDELEG , hedeleg); - - hideleg =0 ; - hideleg |= (1UL <<IRQ_VS_SOFT ); - hideleg |= (1UL <<IRQ_VS_TIMER ); - hideleg |= (1UL <<IRQ_VS_EXT ); -csr_write (CSR_HIDELEG , hideleg); +csr_write (CSR_HEDELEG ,KVM_HEDELEG_DEFAULT ); +csr_write (CSR_HIDELEG ,KVM_HIDELEG_DEFAULT );/* VS should access only the time counter directly. Everything else should trap */ csr_write (CSR_HCOUNTEREN ,0x02 );
arch/riscv/kvm/vcpu.c +13 -2
@@ -475,8 +475,15 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,int kvm_arch_vcpu_ioctl_set_guest_debug (struct kvm_vcpu *vcpu,struct kvm_guest_debug *dbg) { -/* TODO; To be implemented later. */ -return -EINVAL ; +if (dbg->control &KVM_GUESTDBG_ENABLE ) { + vcpu->guest_debug = dbg->control; + vcpu->arch.cfg.hedeleg =KVM_HEDELEG_GUEST_DEBUG ; + }else { + vcpu->guest_debug =0 ; + vcpu->arch.cfg.hedeleg =KVM_HEDELEG_DEFAULT ; + } + +return 0 ; }static void kvm_riscv_vcpu_setup_config (struct kvm_vcpu *vcpu)
@@ -505,6 +512,9 @@ static void kvm_riscv_vcpu_setup_config(struct kvm_vcpu *vcpu)if (riscv_isa_extension_available (isa,SMSTATEEN )) cfg->hstateen0 |=SMSTATEEN0_SSTATEEN0 ; } + +if (!vcpu->guest_debug) + cfg->hedeleg =KVM_HEDELEG_DEFAULT ; }void kvm_arch_vcpu_load (struct kvm_vcpu *vcpu,int cpu)
@@ -519,6 +529,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)csr_write (CSR_VSEPC , csr->vsepc);csr_write (CSR_VSCAUSE , csr->vscause);csr_write (CSR_VSTVAL , csr->vstval); +csr_write (CSR_HEDELEG , cfg->hedeleg);csr_write (CSR_HVIP , csr->hvip);csr_write (CSR_VSATP , csr->vsatp);csr_write (CSR_HENVCFG , cfg->henvcfg);
arch/riscv/kvm/vm.c +1 -0
@@ -186,6 +186,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_READONLY_MEM :case KVM_CAP_MP_STATE :case KVM_CAP_IMMEDIATE_EXIT : +case KVM_CAP_SET_GUEST_DEBUG : r =1 ;break ;case KVM_CAP_NR_VCPUS :
Signature · 1 line
2.17.1
PATCH v2 2/3 RISC-V: KVM: Handle breakpoint exits for VCPU
In reply to Chao DuExit to userspace for breakpoint traps. Set the exit_reason as KVM_EXIT_DEBUG before exit.
--- arch/riscv/kvm/vcpu_exit.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/riscv/kvm/vcpu_exit.c +4 -0
@@ -204,6 +204,10 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,if (vcpu->arch.guest_context.hstatus &HSTATUS_SPV ) ret =kvm_riscv_vcpu_sbi_ecall (vcpu, run);break ; +case EXC_BREAKPOINT : + run->exit_reason =KVM_EXIT_DEBUG ; + ret =0 ; +break ;default :break ; }
Signature · 1 line
2.17.1
PATCH v2 3/3 RISC-V: KVM: selftests: Add breakpoints test support
In reply to Chao DuInitial support for RISC-V KVM breakpoint test. Check the exit reason and the PC when guest debug is enabled.
--- tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/riscv/breakpoints.c | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 tools/testing/selftests/kvm/riscv/breakpoints.c
tools/testing/selftests/kvm/Makefile +1 -0
@@ -184,6 +184,7 @@ TEST_GEN_PROGS_s390x += rseq_test TEST_GEN_PROGS_s390x += set_memory_region_test TEST_GEN_PROGS_s390x += kvm_binary_stats_test +TEST_GEN_PROGS_riscv += riscv/breakpoints TEST_GEN_PROGS_riscv += demand_paging_test TEST_GEN_PROGS_riscv += dirty_log_test TEST_GEN_PROGS_riscv += get-reg-list
tools/testing/selftests/kvm/riscv/breakpoints.c new file +49 -0
49 lines not on this page · raw message
Signature · 1 line
2.17.1
Anup Patel
maintainerRe: [PATCH v2 1/3] RISC-V: KVM: Implement kvm_arch_vcpu_ioctl_set_guest_debug()
In reply to Chao DuQuoted 24 lines · On Fri, Mar 1, 2024 at 7:08?AM Chao Du <[email protected]> wrote:
kvm_vm_ioctl_check_extension(): Return 1 ifKVM_CAP_SET_GUEST_DEBUGis been checked.
kvm_arch_vcpu_ioctl_set_guest_debug(): Update theguest_debugflags from userspace accordingly. Route the breakpoint exceptions to HS mode if the VCPU is being debugged by userspace, by clearing the corresponding bit in hedeleg. Write the actual CSR inkvm_arch_vcpu_load().Signed-off-by Chao Du <[email protected]>--- arch/riscv/include/asm/kvm_host.h | 17 +++++++++++++++++ arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kvm/main.c | 18 ++---------------- arch/riscv/kvm/vcpu.c | 15 +++++++++++++-- arch/riscv/kvm/vm.c | 1 + 5 files changed, 34 insertions(+), 18 deletions(-)arch/riscv/include/asm/kvm_host.h +0
-0
arch/riscv/include/asm/kvm_host.h line 51
Quoted 9 lines
arch/riscv/include/asm/kvm_host.h +6
-0@@ -43,6 +43,22 @@ KVM_ARCH_REQ_FLAGS(5, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)#define KVM_REQ_STEAL_UPDATE KVM_ARCH_REQ(6) +#define KVM_HEDELEG_DEFAULT ((_AC(1, UL) << EXC_INST_MISALIGNED) | \ + (_AC(1, UL) << EXC_BREAKPOINT) | \ + (_AC(1, UL) << EXC_SYSCALL) | \ + (_AC(1, UL) << EXC_INST_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_LOAD_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_STORE_PAGE_FAULT))
Use BIT(xyz) here. For example: BIT(EXC_INST_MISALIGNED)
Also, BIT(EXC_BREAKPOINT) should not be part of KVM_HEDELEG_DEFAULT.
arch/riscv/include/asm/kvm_host.h line 56
Quoted 5 lines
arch/riscv/include/asm/kvm_host.h +5
-0+#define KVM_HEDELEG_GUEST_DEBUG ((_AC(1, UL) << EXC_INST_MISALIGNED) | \ + (_AC(1, UL) << EXC_SYSCALL) | \ + (_AC(1, UL) << EXC_INST_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_LOAD_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_STORE_PAGE_FAULT))
No need for KVM_HEDELEG_GUEST_DEBUG, see below.
arch/riscv/include/asm/kvm_host.h line 61
Quoted 5 lines
arch/riscv/include/asm/kvm_host.h +5
-0+ +#define KVM_HIDELEG_DEFAULT ((_AC(1, UL) << IRQ_VS_SOFT) | \ + (_AC(1, UL) << IRQ_VS_TIMER) | \ + (_AC(1, UL) << IRQ_VS_EXT)) +
Same as above, use BIT(xyz) here.
Quoted 57 lines
arch/riscv/include/asm/kvm_host.h +1
-0enum kvm_riscv_hfence_type {KVM_RISCV_HFENCE_UNKNOWN =0 ,KVM_RISCV_HFENCE_GVMA_VMID_GPA ,@@ -169,6 +185,7 @@ struct kvm_vcpu_csr {struct kvm_vcpu_config {u64 henvcfg;u64 hstateen0; +unsigned long hedeleg; };struct kvm_vcpu_smstateen_csr {arch/riscv/include/uapi/asm/kvm.h +1
-0@@ -17,6 +17,7 @@#define __KVM_HAVE_IRQ_LINE#define __KVM_HAVE_READONLY_MEM +#define __KVM_HAVE_GUEST_DEBUG#define KVM_COALESCED_MMIO_PAGE_OFFSET 1arch/riscv/kvm/main.c +2
-16@@ -22,22 +22,8 @@ long kvm_arch_dev_ioctl(struct file *filp,int kvm_arch_hardware_enable (void ) { -unsigned long hideleg, hedeleg; - - hedeleg =0 ; - hedeleg |= (1UL <<EXC_INST_MISALIGNED ); - hedeleg |= (1UL <<EXC_BREAKPOINT ); - hedeleg |= (1UL <<EXC_SYSCALL ); - hedeleg |= (1UL <<EXC_INST_PAGE_FAULT ); - hedeleg |= (1UL <<EXC_LOAD_PAGE_FAULT ); - hedeleg |= (1UL <<EXC_STORE_PAGE_FAULT ); -csr_write (CSR_HEDELEG , hedeleg); - - hideleg =0 ; - hideleg |= (1UL <<IRQ_VS_SOFT ); - hideleg |= (1UL <<IRQ_VS_TIMER ); - hideleg |= (1UL <<IRQ_VS_EXT ); -csr_write (CSR_HIDELEG , hideleg); +csr_write (CSR_HEDELEG ,KVM_HEDELEG_DEFAULT ); +csr_write (CSR_HIDELEG ,KVM_HIDELEG_DEFAULT );/* VS should access only the time counter directly. Everything else should trap */ csr_write (CSR_HCOUNTEREN ,0x02 );arch/riscv/kvm/vcpu.c +0
-0
arch/riscv/kvm/vcpu.c int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, · line 479
Quoted 5 lines
arch/riscv/kvm/vcpu.c +0
-2@@ -475,8 +475,15 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,int kvm_arch_vcpu_ioctl_set_guest_debug (struct kvm_vcpu *vcpu,struct kvm_guest_debug *dbg) { -/* TODO; To be implemented later. */ -return -EINVAL ;
if (vcpu->arch.ran_atleast_once)
return -EBUSY;
arch/riscv/kvm/vcpu.c int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, · line 484
Quoted 7 lines
arch/riscv/kvm/vcpu.c +7
-0+if (dbg->control &KVM_GUESTDBG_ENABLE ) { + vcpu->guest_debug = dbg->control; + vcpu->arch.cfg.hedeleg =KVM_HEDELEG_GUEST_DEBUG ; + }else { + vcpu->guest_debug =0 ; + vcpu->arch.cfg.hedeleg =KVM_HEDELEG_DEFAULT ; + }
Don't update vcpu->arch.cfg.hedeleg here since it should be only done in kvm_riscv_vcpu_setup_config().
arch/riscv/kvm/vcpu.c static void kvm_riscv_vcpu_setup_config(struct kvm_vcpu *vcpu) · line 517
Quoted 12 lines
arch/riscv/kvm/vcpu.c +5
-0+ +return 0 ; }static void kvm_riscv_vcpu_setup_config (struct kvm_vcpu *vcpu)@@ -505,6 +512,9 @@ static void kvm_riscv_vcpu_setup_config(struct kvm_vcpu *vcpu)if (riscv_isa_extension_available (isa,SMSTATEEN )) cfg->hstateen0 |=SMSTATEEN0_SSTATEEN0 ; } + +if (!vcpu->guest_debug) + cfg->hedeleg =KVM_HEDELEG_DEFAULT ;
This should be:
cfg->hedeleg = KVM_HEDELEG_DEFAULT;
if (vcpu->guest_debug)
cfg->hedeleg |= BIT(EXC_BREAKPOINT);
Quoted 24 lines
arch/riscv/kvm/vcpu.c +1
-0}void kvm_arch_vcpu_load (struct kvm_vcpu *vcpu,int cpu)@@ -519,6 +529,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)csr_write (CSR_VSEPC , csr->vsepc);csr_write (CSR_VSCAUSE , csr->vscause);csr_write (CSR_VSTVAL , csr->vstval); +csr_write (CSR_HEDELEG , cfg->hedeleg);csr_write (CSR_HVIP , csr->hvip);csr_write (CSR_VSATP , csr->vsatp);csr_write (CSR_HENVCFG , cfg->henvcfg);arch/riscv/kvm/vm.c +1
-1@@ -186,6 +186,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_READONLY_MEM :case KVM_CAP_MP_STATE :case KVM_CAP_IMMEDIATE_EXIT : +case KVM_CAP_SET_GUEST_DEBUG : r =1 ;break ;case KVM_CAP_NR_VCPUS : --
Quoted 2 lines
2.17.1
Regards,
Anup
Re: [PATCH v2 2/3] RISC-V: KVM: Handle breakpoint exits for VCPU
In reply to Chao DuQuoted 5 lines · On Fri, Mar 1, 2024 at 7:08?AM Chao Du <[email protected]> wrote:
Exit to userspace for breakpoint traps. Set the
exit_reasonasKVM_EXIT_DEBUGbefore exit.Signed-off-by Chao Du <[email protected]>
LGTM.
Thanks,
Anup
Quoted 22 lines
--- arch/riscv/kvm/vcpu_exit.c | 4 ++++ 1 file changed, 4 insertions(+)arch/riscv/kvm/vcpu_exit.c +4
-1@@ -204,6 +204,10 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,if (vcpu->arch.guest_context.hstatus &HSTATUS_SPV ) ret =kvm_riscv_vcpu_sbi_ecall (vcpu, run);break ; +case EXC_BREAKPOINT : + run->exit_reason =KVM_EXIT_DEBUG ; + ret =0 ; +break ;default :break ; } --2.17.1
Anup Patel
maintainerRe: [PATCH v2 3/3] RISC-V: KVM: selftests: Add breakpoints test support
In reply to Chao DuThis is actually a ebreak test so subject should be:
"RISC-V: KVM: selftests: Add ebreak test support"
Quoted 3 lines · On Fri, Mar 1, 2024 at 7:08?AM Chao Du <[email protected]> wrote:
Initial support for RISC-V KVM breakpoint test. Check the exit reason and the PC when guest debug is enabled.
s/breakpoint/ebreak/
Quoted 5 lines
Signed-off-by Chao Du <[email protected]>--- tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/riscv/breakpoints.c | 49 +++++++++++++++++++
Rename riscv/breakpoints.c to riscv/ebreak_test.c
Quoted 21 lines
2 files changed, 50 insertions(+) create mode 100644 tools/testing/selftests/kvm/riscv/breakpoints.ctools/testing/selftests/kvm/Makefile +1
-0@@ -184,6 +184,7 @@ TEST_GEN_PROGS_s390x += rseq_test TEST_GEN_PROGS_s390x += set_memory_region_test TEST_GEN_PROGS_s390x += kvm_binary_stats_test +TEST_GEN_PROGS_riscv += riscv/breakpoints TEST_GEN_PROGS_riscv += demand_paging_test TEST_GEN_PROGS_riscv += dirty_log_test TEST_GEN_PROGS_riscv += get-reg-listtools/testing/selftests/kvm/riscv/breakpoints.c new file +0
-0
tools/testing/selftests/kvm/riscv/breakpoints.c line 3
Quoted 3 lines
tools/testing/selftests/kvm/riscv/breakpoints.c new file +3
-0@@ -0,0 +1,49 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* RISC-V KVM breakpoint tests.
s/breakpoint tests/ebreak test/
Quoted 27 lines
tools/testing/selftests/kvm/riscv/breakpoints.c new file +27
-0+* +* Copyright 2024 Beijing ESWIN Computing Technology Co., Ltd. +* +*/ +#include "kvm_util.h" + +#define PC (v) ((uint64_t)&(v)) + +extern unsigned char sw_bp; + +static void guest_code (void ) +{ + asmvolatile ("sw_bp: ebreak" ); + asmvolatile ("nop" ); + asmvolatile ("nop" ); + asmvolatile ("nop" ); + +GUEST_DONE (); +} + +int main (void ) +{ +struct kvm_vm *vm; +struct kvm_vcpu *vcpu; +struct kvm_guest_debug debug; +uint64_t pc; +
tools/testing/selftests/kvm/riscv/breakpoints.c line 40
Quoted 10 lines
tools/testing/selftests/kvm/riscv/breakpoints.c new file +10
-0+TEST_REQUIRE (kvm_has_cap (KVM_CAP_SET_GUEST_DEBUG )); + + vm =vm_create_with_one_vcpu (&vcpu, guest_code); + +memset (&debug,0 ,sizeof (debug)); + debug.control =KVM_GUESTDBG_ENABLE ; +vcpu_guest_debug_set (vcpu, &debug); +vcpu_run (vcpu); + +TEST_ASSERT_KVM_EXIT_REASON (vcpu,KVM_EXIT_DEBUG );
This is only testing the case where KVM_GUESTDBG_ENABLE is set.
This test should be enhanced to:
1) First test "ebreak" executed by Guest when KVM_GUESTDBG_ENABLE is not set. Here, we would expect main() to receive KVM_EXIT_DEBUG.
2) Second test "ebreak" executed by Guest when KVM_GUESTDBG_ENABLE is set. Here, we would expect main() will not receive KVM_EXIT_DEBUG.
Quoted 10 lines
tools/testing/selftests/kvm/riscv/breakpoints.c new file +9
-1+ +vcpu_get_reg (vcpu,RISCV_CORE_REG (regs.pc), &pc); + +TEST_ASSERT_EQ (pc,PC (sw_bp)); + +kvm_vm_free (vm); + +return 0 ; +} --
Quoted 2 lines
2.17.1
Regards,
Anup
Re: [PATCH v2 1/3] RISC-V: KVM: Implement kvm_arch_vcpu_ioctl_set_guest_debug()
In reply to Anup PatelQuoted 29 lines · Anup Patel
Quoted 27 lines · On Fri, Mar 1, 2024 at 7:08?AM Chao Du <[email protected]> wrote:
kvm_vm_ioctl_check_extension(): Return 1 ifKVM_CAP_SET_GUEST_DEBUGis been checked.
kvm_arch_vcpu_ioctl_set_guest_debug(): Update theguest_debugflags from userspace accordingly. Route the breakpoint exceptions to HS mode if the VCPU is being debugged by userspace, by clearing the corresponding bit in hedeleg. Write the actual CSR inkvm_arch_vcpu_load().Signed-off-by Chao Du <[email protected]>--- arch/riscv/include/asm/kvm_host.h | 17 +++++++++++++++++ arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kvm/main.c | 18 ++---------------- arch/riscv/kvm/vcpu.c | 15 +++++++++++++-- arch/riscv/kvm/vm.c | 1 + 5 files changed, 34 insertions(+), 18 deletions(-)arch/riscv/include/asm/kvm_host.h +0
-0@@ -43,6 +43,22 @@ KVM_ARCH_REQ_FLAGS(5, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)#define KVM_REQ_STEAL_UPDATE KVM_ARCH_REQ(6)
Quoted 8 lines
arch/riscv/include/asm/kvm_host.h line 51
Quoted 6 lines
arch/riscv/include/asm/kvm_host.h +6
-0+#define KVM_HEDELEG_DEFAULT ((_AC(1, UL) << EXC_INST_MISALIGNED) | \ + (_AC(1, UL) << EXC_BREAKPOINT) | \ + (_AC(1, UL) << EXC_SYSCALL) | \ + (_AC(1, UL) << EXC_INST_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_LOAD_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_STORE_PAGE_FAULT))Use BIT(xyz) here. For example:
BIT(EXC_INST_MISALIGNED)
Thanks, I will use BIT() instead in next revision.
Quoted 3 lines
Also,
BIT(EXC_BREAKPOINT)should not be part ofKVM_HEDELEG_DEFAULT.
I think the bit EXC_BREAKPOINT should be set by default, like what you already did in kvm_arch_hardware_enable(). Then the VS could get the ebreak and handle it accordingly.
If the guest_debug is enabled, ebreak instructions are inserted by the userspace(QEMU). So KVM should 'intercept' the ebreak and exit to QEMU. Bit EXC_BREAKPOINT should be cleared in this case.
Quoted 81 lines
arch/riscv/include/asm/kvm_host.h line 56
Quoted 6 lines
arch/riscv/include/asm/kvm_host.h +5
-0+#define KVM_HEDELEG_GUEST_DEBUG ((_AC(1, UL) << EXC_INST_MISALIGNED) | \ + (_AC(1, UL) << EXC_SYSCALL) | \ + (_AC(1, UL) << EXC_INST_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_LOAD_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_STORE_PAGE_FAULT))No need for
KVM_HEDELEG_GUEST_DEBUG, see below.arch/riscv/include/asm/kvm_host.h line 61
Quoted 5 lines
arch/riscv/include/asm/kvm_host.h +5
-0+ +#define KVM_HIDELEG_DEFAULT ((_AC(1, UL) << IRQ_VS_SOFT) | \ + (_AC(1, UL) << IRQ_VS_TIMER) | \ + (_AC(1, UL) << IRQ_VS_EXT)) +Same as above, use BIT(xyz) here.
Quoted 57 lines
arch/riscv/include/asm/kvm_host.h +1
-0enum kvm_riscv_hfence_type {KVM_RISCV_HFENCE_UNKNOWN =0 ,KVM_RISCV_HFENCE_GVMA_VMID_GPA ,@@ -169,6 +185,7 @@ struct kvm_vcpu_csr {struct kvm_vcpu_config {u64 henvcfg;u64 hstateen0; +unsigned long hedeleg; };struct kvm_vcpu_smstateen_csr {arch/riscv/include/uapi/asm/kvm.h +1
-0@@ -17,6 +17,7 @@#define __KVM_HAVE_IRQ_LINE#define __KVM_HAVE_READONLY_MEM +#define __KVM_HAVE_GUEST_DEBUG#define KVM_COALESCED_MMIO_PAGE_OFFSET 1arch/riscv/kvm/main.c +2
-16@@ -22,22 +22,8 @@ long kvm_arch_dev_ioctl(struct file *filp,int kvm_arch_hardware_enable (void ) { -unsigned long hideleg, hedeleg; - - hedeleg =0 ; - hedeleg |= (1UL <<EXC_INST_MISALIGNED ); - hedeleg |= (1UL <<EXC_BREAKPOINT ); - hedeleg |= (1UL <<EXC_SYSCALL ); - hedeleg |= (1UL <<EXC_INST_PAGE_FAULT ); - hedeleg |= (1UL <<EXC_LOAD_PAGE_FAULT ); - hedeleg |= (1UL <<EXC_STORE_PAGE_FAULT ); -csr_write (CSR_HEDELEG , hedeleg); - - hideleg =0 ; - hideleg |= (1UL <<IRQ_VS_SOFT ); - hideleg |= (1UL <<IRQ_VS_TIMER ); - hideleg |= (1UL <<IRQ_VS_EXT ); -csr_write (CSR_HIDELEG , hideleg); +csr_write (CSR_HEDELEG ,KVM_HEDELEG_DEFAULT ); +csr_write (CSR_HIDELEG ,KVM_HIDELEG_DEFAULT );/* VS should access only the time counter directly. Everything else should trap */ csr_write (CSR_HCOUNTEREN ,0x02 );arch/riscv/kvm/vcpu.c +0
-0arch/riscv/kvm/vcpu.c int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, · line 479
Quoted 5 lines
arch/riscv/kvm/vcpu.c +0
-2@@ -475,8 +475,15 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,int kvm_arch_vcpu_ioctl_set_guest_debug (struct kvm_vcpu *vcpu,struct kvm_guest_debug *dbg) { -/* TODO; To be implemented later. */ -return -EINVAL ;if (
vcpu->arch.ran_atleast_once)
Quoted 1 line
return -EBUSY;
If we enabled the guest_debug in QEMU side, then the KVM_SET_GUEST_DEBUG ioctl will come before the first KVM_RUN. This will always cause an ERROR.
Quoted 28 lines
arch/riscv/kvm/vcpu.c int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, · line 484
Quoted 9 lines
arch/riscv/kvm/vcpu.c +7
-0+if (dbg->control &KVM_GUESTDBG_ENABLE ) { + vcpu->guest_debug = dbg->control; + vcpu->arch.cfg.hedeleg =KVM_HEDELEG_GUEST_DEBUG ; + }else { + vcpu->guest_debug =0 ; + vcpu->arch.cfg.hedeleg =KVM_HEDELEG_DEFAULT ; + }Don't update
vcpu->arch.cfg.hedeleghere since it should be only done inkvm_riscv_vcpu_setup_config().arch/riscv/kvm/vcpu.c static void kvm_riscv_vcpu_setup_config(struct kvm_vcpu *vcpu) · line 517
Quoted 12 lines
arch/riscv/kvm/vcpu.c +5
-0+ +return 0 ; }static void kvm_riscv_vcpu_setup_config (struct kvm_vcpu *vcpu)@@ -505,6 +512,9 @@ static void kvm_riscv_vcpu_setup_config(struct kvm_vcpu *vcpu)if (riscv_isa_extension_available (isa,SMSTATEEN )) cfg->hstateen0 |=SMSTATEEN0_SSTATEEN0 ; } + +if (!vcpu->guest_debug) + cfg->hedeleg =KVM_HEDELEG_DEFAULT ;This should be:
Quoted 3 lines
cfg->hedeleg=KVM_HEDELEG_DEFAULT;
if (vcpu->guest_debug)cfg->hedeleg |= BIT(EXC_BREAKPOINT);
Like above, here the logic should be:
cfg->hedeleg = KVM_HEDELEG_DEFAULT; // with BIT(EXC_BREAKPOINT) if (vcpu->guest_debug)
cfg->hedeleg &= ~BIT(EXC_BREAKPOINT);
Another approach is:
initialize the cfg->hedeleg as KVM_HEDELEG_DEFAULT during kvm_arch_vcpu_create(). Besides that, only update the cfg->hedeleg in kvm_arch_vcpu_ioctl_set_guest_debug().
Quoted 22 lines
Quoted 22 lines
arch/riscv/kvm/vcpu.c +1
-0}void kvm_arch_vcpu_load (struct kvm_vcpu *vcpu,int cpu)@@ -519,6 +529,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)csr_write (CSR_VSEPC , csr->vsepc);csr_write (CSR_VSCAUSE , csr->vscause);csr_write (CSR_VSTVAL , csr->vstval); +csr_write (CSR_HEDELEG , cfg->hedeleg);csr_write (CSR_HVIP , csr->hvip);csr_write (CSR_VSATP , csr->vsatp);csr_write (CSR_HENVCFG , cfg->henvcfg);arch/riscv/kvm/vm.c +1
-0@@ -186,6 +186,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_READONLY_MEM :case KVM_CAP_MP_STATE :case KVM_CAP_IMMEDIATE_EXIT : +case KVM_CAP_SET_GUEST_DEBUG : r =1 ;
Quoted 8 lines
Quoted 5 lines
arch/riscv/kvm/vm.c +0
-1break ;case KVM_CAP_NR_VCPUS : --2.17.1
Regards,
Anup
Thanks,
Chao
Anup Patel
maintainerRe: [PATCH v2 1/3] RISC-V: KVM: Implement kvm_arch_vcpu_ioctl_set_guest_debug()
Quoted 46 lines · On Fri, Mar 1, 2024 at 12:05?PM Chao Du <[email protected]> wrote:
Quoted 29 lines · Anup Patel
Quoted 27 lines · On Fri, Mar 1, 2024 at 7:08?AM Chao Du <[email protected]> wrote:
kvm_vm_ioctl_check_extension(): Return 1 ifKVM_CAP_SET_GUEST_DEBUGis been checked.
kvm_arch_vcpu_ioctl_set_guest_debug(): Update theguest_debugflags from userspace accordingly. Route the breakpoint exceptions to HS mode if the VCPU is being debugged by userspace, by clearing the corresponding bit in hedeleg. Write the actual CSR inkvm_arch_vcpu_load().Signed-off-by Chao Du <[email protected]>--- arch/riscv/include/asm/kvm_host.h | 17 +++++++++++++++++ arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kvm/main.c | 18 ++---------------- arch/riscv/kvm/vcpu.c | 15 +++++++++++++-- arch/riscv/kvm/vm.c | 1 + 5 files changed, 34 insertions(+), 18 deletions(-)arch/riscv/include/asm/kvm_host.h +0
-0@@ -43,6 +43,22 @@ KVM_ARCH_REQ_FLAGS(5, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)#define KVM_REQ_STEAL_UPDATE KVM_ARCH_REQ(6)Quoted 8 lines
arch/riscv/include/asm/kvm_host.h line 51
Quoted 6 lines
arch/riscv/include/asm/kvm_host.h +6
-0+#define KVM_HEDELEG_DEFAULT ((_AC(1, UL) << EXC_INST_MISALIGNED) | \ + (_AC(1, UL) << EXC_BREAKPOINT) | \ + (_AC(1, UL) << EXC_SYSCALL) | \ + (_AC(1, UL) << EXC_INST_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_LOAD_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_STORE_PAGE_FAULT))Use BIT(xyz) here. For example:
BIT(EXC_INST_MISALIGNED)Thanks, I will use
BIT()instead in next revision.Quoted 3 lines
Also,
BIT(EXC_BREAKPOINT)should not be part ofKVM_HEDELEG_DEFAULT.
Quoted 7 lines
I think the bit
EXC_BREAKPOINTshould be set by default, like what you already did inkvm_arch_hardware_enable(). Then the VS could get the ebreak and handle it accordingly.If the
guest_debugis enabled, ebreak instructions are inserted by the userspace(QEMU). So KVM should 'intercept' the ebreak and exit to QEMU. BitEXC_BREAKPOINTshould be cleared in this case.
If EXC_BREAKPOINT is delegated by default then it is not consistent with vcpu->guest_debug which is not enabled by default.
Quoted 84 lines
Quoted 82 lines
arch/riscv/include/asm/kvm_host.h line 56
Quoted 7 lines
arch/riscv/include/asm/kvm_host.h +5
-0+#define KVM_HEDELEG_GUEST_DEBUG ((_AC(1, UL) << EXC_INST_MISALIGNED) | \ + (_AC(1, UL) << EXC_SYSCALL) | \ + (_AC(1, UL) << EXC_INST_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_LOAD_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_STORE_PAGE_FAULT))No need for
KVM_HEDELEG_GUEST_DEBUG, see below.arch/riscv/include/asm/kvm_host.h line 61
Quoted 5 lines
arch/riscv/include/asm/kvm_host.h +5
-0+ +#define KVM_HIDELEG_DEFAULT ((_AC(1, UL) << IRQ_VS_SOFT) | \ + (_AC(1, UL) << IRQ_VS_TIMER) | \ + (_AC(1, UL) << IRQ_VS_EXT)) +Same as above, use BIT(xyz) here.
Quoted 57 lines
arch/riscv/include/asm/kvm_host.h +1
-0enum kvm_riscv_hfence_type {KVM_RISCV_HFENCE_UNKNOWN =0 ,KVM_RISCV_HFENCE_GVMA_VMID_GPA ,@@ -169,6 +185,7 @@ struct kvm_vcpu_csr {struct kvm_vcpu_config {u64 henvcfg;u64 hstateen0; +unsigned long hedeleg; };struct kvm_vcpu_smstateen_csr {arch/riscv/include/uapi/asm/kvm.h +1
-0@@ -17,6 +17,7 @@#define __KVM_HAVE_IRQ_LINE#define __KVM_HAVE_READONLY_MEM +#define __KVM_HAVE_GUEST_DEBUG#define KVM_COALESCED_MMIO_PAGE_OFFSET 1arch/riscv/kvm/main.c +2
-16@@ -22,22 +22,8 @@ long kvm_arch_dev_ioctl(struct file *filp,int kvm_arch_hardware_enable (void ) { -unsigned long hideleg, hedeleg; - - hedeleg =0 ; - hedeleg |= (1UL <<EXC_INST_MISALIGNED ); - hedeleg |= (1UL <<EXC_BREAKPOINT ); - hedeleg |= (1UL <<EXC_SYSCALL ); - hedeleg |= (1UL <<EXC_INST_PAGE_FAULT ); - hedeleg |= (1UL <<EXC_LOAD_PAGE_FAULT ); - hedeleg |= (1UL <<EXC_STORE_PAGE_FAULT ); -csr_write (CSR_HEDELEG , hedeleg); - - hideleg =0 ; - hideleg |= (1UL <<IRQ_VS_SOFT ); - hideleg |= (1UL <<IRQ_VS_TIMER ); - hideleg |= (1UL <<IRQ_VS_EXT ); -csr_write (CSR_HIDELEG , hideleg); +csr_write (CSR_HEDELEG ,KVM_HEDELEG_DEFAULT ); +csr_write (CSR_HIDELEG ,KVM_HIDELEG_DEFAULT );/* VS should access only the time counter directly. Everything else should trap */ csr_write (CSR_HCOUNTEREN ,0x02 );arch/riscv/kvm/vcpu.c +0
-0arch/riscv/kvm/vcpu.c int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, · line 479
Quoted 5 lines
arch/riscv/kvm/vcpu.c +0
-2@@ -475,8 +475,15 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,int kvm_arch_vcpu_ioctl_set_guest_debug (struct kvm_vcpu *vcpu,struct kvm_guest_debug *dbg) { -/* TODO; To be implemented later. */ -return -EINVAL ;if (
vcpu->arch.ran_atleast_once)Quoted 1 line
return -EBUSY;
Quoted 2 lines
If we enabled the
guest_debugin QEMU side, then theKVM_SET_GUEST_DEBUGioctl will come before the firstKVM_RUN. This will always cause an ERROR.
The check ensures that KVM user space can only enable/disable guest debug before the VCPU is run. I don't see why this would fail for QEMU.
Quoted 35 lines
Quoted 29 lines
arch/riscv/kvm/vcpu.c int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, · line 484
Quoted 10 lines
arch/riscv/kvm/vcpu.c +7
-0+if (dbg->control &KVM_GUESTDBG_ENABLE ) { + vcpu->guest_debug = dbg->control; + vcpu->arch.cfg.hedeleg =KVM_HEDELEG_GUEST_DEBUG ; + }else { + vcpu->guest_debug =0 ; + vcpu->arch.cfg.hedeleg =KVM_HEDELEG_DEFAULT ; + }Don't update
vcpu->arch.cfg.hedeleghere since it should be only done inkvm_riscv_vcpu_setup_config().arch/riscv/kvm/vcpu.c static void kvm_riscv_vcpu_setup_config(struct kvm_vcpu *vcpu) · line 517
Quoted 12 lines
arch/riscv/kvm/vcpu.c +5
-0+ +return 0 ; }static void kvm_riscv_vcpu_setup_config (struct kvm_vcpu *vcpu)@@ -505,6 +512,9 @@ static void kvm_riscv_vcpu_setup_config(struct kvm_vcpu *vcpu)if (riscv_isa_extension_available (isa,SMSTATEEN )) cfg->hstateen0 |=SMSTATEEN0_SSTATEEN0 ; } + +if (!vcpu->guest_debug) + cfg->hedeleg =KVM_HEDELEG_DEFAULT ;This should be:
Quoted 3 lines
cfg->hedeleg=KVM_HEDELEG_DEFAULT;
if (vcpu->guest_debug)cfg->hedeleg |= BIT(EXC_BREAKPOINT);Like above, here the logic should be:
Quoted 7 lines
cfg->hedeleg=KVM_HEDELEG_DEFAULT; // withBIT(EXC_BREAKPOINT)if (vcpu->guest_debug)cfg->hedeleg &= ~BIT(EXC_BREAKPOINT);Another approach is:
initialize thecfg->hedelegasKVM_HEDELEG_DEFAULTduringkvm_arch_vcpu_create(). Besides that, only update thecfg->hedeleginkvm_arch_vcpu_ioctl_set_guest_debug().
I disagree. We should handle hedeleg just like we handle henvcfg.
Quoted 32 lines
Quoted 23 lines
Quoted 23 lines
arch/riscv/kvm/vcpu.c +1
-0}void kvm_arch_vcpu_load (struct kvm_vcpu *vcpu,int cpu)@@ -519,6 +529,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)csr_write (CSR_VSEPC , csr->vsepc);csr_write (CSR_VSCAUSE , csr->vscause);csr_write (CSR_VSTVAL , csr->vstval); +csr_write (CSR_HEDELEG , cfg->hedeleg);csr_write (CSR_HVIP , csr->hvip);csr_write (CSR_VSATP , csr->vsatp);csr_write (CSR_HENVCFG , cfg->henvcfg);arch/riscv/kvm/vm.c +1
-0@@ -186,6 +186,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_READONLY_MEM :case KVM_CAP_MP_STATE :case KVM_CAP_IMMEDIATE_EXIT : +case KVM_CAP_SET_GUEST_DEBUG : r =1 ;Quoted 8 lines
Quoted 5 lines
arch/riscv/kvm/vm.c +0
-1break ;case KVM_CAP_NR_VCPUS : --2.17.1
Regards,
Anup
Quoted 2 lines
Thanks,
Chao
Regards,
Anup
Re: [PATCH v2 1/3] RISC-V: KVM: Implement kvm_arch_vcpu_ioctl_set_guest_debug()
Quoted 52 lines · Anup Patel
Quoted 50 lines · On Fri, Mar 1, 2024 at 12:05?PM Chao Du <[email protected]> wrote:
Quoted 29 lines · Anup Patel
Quoted 27 lines · On Fri, Mar 1, 2024 at 7:08?AM Chao Du <[email protected]> wrote:
kvm_vm_ioctl_check_extension(): Return 1 ifKVM_CAP_SET_GUEST_DEBUGis been checked.
kvm_arch_vcpu_ioctl_set_guest_debug(): Update theguest_debugflags from userspace accordingly. Route the breakpoint exceptions to HS mode if the VCPU is being debugged by userspace, by clearing the corresponding bit in hedeleg. Write the actual CSR inkvm_arch_vcpu_load().Signed-off-by Chao Du <[email protected]>--- arch/riscv/include/asm/kvm_host.h | 17 +++++++++++++++++ arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kvm/main.c | 18 ++---------------- arch/riscv/kvm/vcpu.c | 15 +++++++++++++-- arch/riscv/kvm/vm.c | 1 + 5 files changed, 34 insertions(+), 18 deletions(-)arch/riscv/include/asm/kvm_host.h +0
-0@@ -43,6 +43,22 @@ KVM_ARCH_REQ_FLAGS(5, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)#define KVM_REQ_STEAL_UPDATE KVM_ARCH_REQ(6)Quoted 8 lines
arch/riscv/include/asm/kvm_host.h line 51
Quoted 6 lines
arch/riscv/include/asm/kvm_host.h +6
-0+#define KVM_HEDELEG_DEFAULT ((_AC(1, UL) << EXC_INST_MISALIGNED) | \ + (_AC(1, UL) << EXC_BREAKPOINT) | \ + (_AC(1, UL) << EXC_SYSCALL) | \ + (_AC(1, UL) << EXC_INST_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_LOAD_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_STORE_PAGE_FAULT))Use BIT(xyz) here. For example:
BIT(EXC_INST_MISALIGNED)Thanks, I will use
BIT()instead in next revision.Quoted 3 lines
Also,
BIT(EXC_BREAKPOINT)should not be part ofKVM_HEDELEG_DEFAULT.I think the bit
EXC_BREAKPOINTshould be set by default, like what you already did inkvm_arch_hardware_enable(). Then the VS could get the ebreak and handle it accordingly.
Quoted 6 lines
Quoted 3 lines
If the
guest_debugis enabled, ebreak instructions are inserted by the userspace(QEMU). So KVM should 'intercept' the ebreak and exit to QEMU. BitEXC_BREAKPOINTshould be cleared in this case.If
EXC_BREAKPOINTis delegated by default then it is not consistent withvcpu->guest_debugwhich is not enabled by default.
To enable the guest_debug corresponding to NOT delegate the EXC_BREAKPOINT. They are somehow 'opposite'.
This 'kvm_guest_debug' feature is different from "debug in the guest". The later requires the delegation of EXC_BREAKPOINT.
The former does not.
Quoted 85 lines
Quoted 85 lines
Quoted 84 lines
arch/riscv/include/asm/kvm_host.h line 56
Quoted 8 lines
arch/riscv/include/asm/kvm_host.h +5
-0+#define KVM_HEDELEG_GUEST_DEBUG ((_AC(1, UL) << EXC_INST_MISALIGNED) | \ + (_AC(1, UL) << EXC_SYSCALL) | \ + (_AC(1, UL) << EXC_INST_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_LOAD_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_STORE_PAGE_FAULT))No need for
KVM_HEDELEG_GUEST_DEBUG, see below.arch/riscv/include/asm/kvm_host.h line 61
Quoted 5 lines
arch/riscv/include/asm/kvm_host.h +5
-0+ +#define KVM_HIDELEG_DEFAULT ((_AC(1, UL) << IRQ_VS_SOFT) | \ + (_AC(1, UL) << IRQ_VS_TIMER) | \ + (_AC(1, UL) << IRQ_VS_EXT)) +Same as above, use BIT(xyz) here.
Quoted 57 lines
arch/riscv/include/asm/kvm_host.h +1
-0enum kvm_riscv_hfence_type {KVM_RISCV_HFENCE_UNKNOWN =0 ,KVM_RISCV_HFENCE_GVMA_VMID_GPA ,@@ -169,6 +185,7 @@ struct kvm_vcpu_csr {struct kvm_vcpu_config {u64 henvcfg;u64 hstateen0; +unsigned long hedeleg; };struct kvm_vcpu_smstateen_csr {arch/riscv/include/uapi/asm/kvm.h +1
-0@@ -17,6 +17,7 @@#define __KVM_HAVE_IRQ_LINE#define __KVM_HAVE_READONLY_MEM +#define __KVM_HAVE_GUEST_DEBUG#define KVM_COALESCED_MMIO_PAGE_OFFSET 1arch/riscv/kvm/main.c +2
-16@@ -22,22 +22,8 @@ long kvm_arch_dev_ioctl(struct file *filp,int kvm_arch_hardware_enable (void ) { -unsigned long hideleg, hedeleg; - - hedeleg =0 ; - hedeleg |= (1UL <<EXC_INST_MISALIGNED ); - hedeleg |= (1UL <<EXC_BREAKPOINT ); - hedeleg |= (1UL <<EXC_SYSCALL ); - hedeleg |= (1UL <<EXC_INST_PAGE_FAULT ); - hedeleg |= (1UL <<EXC_LOAD_PAGE_FAULT ); - hedeleg |= (1UL <<EXC_STORE_PAGE_FAULT ); -csr_write (CSR_HEDELEG , hedeleg); - - hideleg =0 ; - hideleg |= (1UL <<IRQ_VS_SOFT ); - hideleg |= (1UL <<IRQ_VS_TIMER ); - hideleg |= (1UL <<IRQ_VS_EXT ); -csr_write (CSR_HIDELEG , hideleg); +csr_write (CSR_HEDELEG ,KVM_HEDELEG_DEFAULT ); +csr_write (CSR_HIDELEG ,KVM_HIDELEG_DEFAULT );/* VS should access only the time counter directly. Everything else should trap */ csr_write (CSR_HCOUNTEREN ,0x02 );arch/riscv/kvm/vcpu.c +0
-0arch/riscv/kvm/vcpu.c int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, · line 479
Quoted 5 lines
arch/riscv/kvm/vcpu.c +0
-2@@ -475,8 +475,15 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,int kvm_arch_vcpu_ioctl_set_guest_debug (struct kvm_vcpu *vcpu,struct kvm_guest_debug *dbg) { -/* TODO; To be implemented later. */ -return -EINVAL ;if (
vcpu->arch.ran_atleast_once)return -EBUSY;
Quoted 6 lines
Quoted 2 lines
If we enabled the
guest_debugin QEMU side, then theKVM_SET_GUEST_DEBUGioctl will come before the firstKVM_RUN. This will always cause an ERROR.The check ensures that KVM user space can only enable/disable guest debug before the VCPU is run. I don't see why this would fail for QEMU.
In the current implementation of GDB and QEMU, the userspace will enable/disable guest_debug frequently during the debugging (almost every step). The sequence should like:
KVM_SET_GUEST_DEBUG enableKVM_RUNKVM_SET_GUEST_DEBUG disableKVM_SET_GUEST_DEBUG enableKVM_RUNKVM_SET_GUEST_DEBUG disableKVM_SET_GUEST_DEBUG enableKVM_RUNKVM_SET_GUEST_DEBUG disable
...
Quoted 40 lines
Quoted 40 lines
Quoted 30 lines
arch/riscv/kvm/vcpu.c int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, · line 484
Quoted 11 lines
arch/riscv/kvm/vcpu.c +7
-0+if (dbg->control &KVM_GUESTDBG_ENABLE ) { + vcpu->guest_debug = dbg->control; + vcpu->arch.cfg.hedeleg =KVM_HEDELEG_GUEST_DEBUG ; + }else { + vcpu->guest_debug =0 ; + vcpu->arch.cfg.hedeleg =KVM_HEDELEG_DEFAULT ; + }Don't update
vcpu->arch.cfg.hedeleghere since it should be only done inkvm_riscv_vcpu_setup_config().arch/riscv/kvm/vcpu.c static void kvm_riscv_vcpu_setup_config(struct kvm_vcpu *vcpu) · line 517
Quoted 12 lines
arch/riscv/kvm/vcpu.c +5
-0+ +return 0 ; }static void kvm_riscv_vcpu_setup_config (struct kvm_vcpu *vcpu)@@ -505,6 +512,9 @@ static void kvm_riscv_vcpu_setup_config(struct kvm_vcpu *vcpu)if (riscv_isa_extension_available (isa,SMSTATEEN )) cfg->hstateen0 |=SMSTATEEN0_SSTATEEN0 ; } + +if (!vcpu->guest_debug) + cfg->hedeleg =KVM_HEDELEG_DEFAULT ;This should be:
Quoted 3 lines
cfg->hedeleg=KVM_HEDELEG_DEFAULT;
if (vcpu->guest_debug)cfg->hedeleg |= BIT(EXC_BREAKPOINT);Like above, here the logic should be:
cfg->hedeleg=KVM_HEDELEG_DEFAULT; // withBIT(EXC_BREAKPOINT)if (vcpu->guest_debug)cfg->hedeleg &= ~BIT(EXC_BREAKPOINT);
Quoted 5 lines
Quoted 3 lines
Another approach is:
initialize thecfg->hedelegasKVM_HEDELEG_DEFAULTduringkvm_arch_vcpu_create(). Besides that, only update thecfg->hedeleginkvm_arch_vcpu_ioctl_set_guest_debug().I disagree. We should handle hedeleg just like we handle henvcfg.
OK, let's only update the cfg->hedeleg in kvm_riscv_vcpu_setup_config().
Quoted 33 lines
Quoted 33 lines
Quoted 32 lines
Quoted 27 lines
arch/riscv/kvm/vcpu.c +1
-0}void kvm_arch_vcpu_load (struct kvm_vcpu *vcpu,int cpu)@@ -519,6 +529,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)csr_write (CSR_VSEPC , csr->vsepc);csr_write (CSR_VSCAUSE , csr->vscause);csr_write (CSR_VSTVAL , csr->vstval); +csr_write (CSR_HEDELEG , cfg->hedeleg);csr_write (CSR_HVIP , csr->hvip);csr_write (CSR_VSATP , csr->vsatp);csr_write (CSR_HENVCFG , cfg->henvcfg);arch/riscv/kvm/vm.c +1
-1@@ -186,6 +186,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_READONLY_MEM :case KVM_CAP_MP_STATE :case KVM_CAP_IMMEDIATE_EXIT : +case KVM_CAP_SET_GUEST_DEBUG : r =1 ;break ;case KVM_CAP_NR_VCPUS : --Quoted 2 lines
2.17.1
Regards,
Anup
Quoted 5 lines
Quoted 2 lines
Thanks,
ChaoRegards,
Anup
Thanks,
Chao
Re: [PATCH v2 1/3] RISC-V: KVM: Implement kvm_arch_vcpu_ioctl_set_guest_debug()
Quoted 61 lines · On Fri, Mar 1, 2024 at 12:57?PM Chao Du <[email protected]> wrote:
Quoted 52 lines · Anup Patel
Quoted 50 lines · On Fri, Mar 1, 2024 at 12:05?PM Chao Du <[email protected]> wrote:
Quoted 29 lines · Anup Patel
Quoted 27 lines · On Fri, Mar 1, 2024 at 7:08?AM Chao Du <[email protected]> wrote:
kvm_vm_ioctl_check_extension(): Return 1 ifKVM_CAP_SET_GUEST_DEBUGis been checked.
kvm_arch_vcpu_ioctl_set_guest_debug(): Update theguest_debugflags from userspace accordingly. Route the breakpoint exceptions to HS mode if the VCPU is being debugged by userspace, by clearing the corresponding bit in hedeleg. Write the actual CSR inkvm_arch_vcpu_load().Signed-off-by Chao Du <[email protected]>--- arch/riscv/include/asm/kvm_host.h | 17 +++++++++++++++++ arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kvm/main.c | 18 ++---------------- arch/riscv/kvm/vcpu.c | 15 +++++++++++++-- arch/riscv/kvm/vm.c | 1 + 5 files changed, 34 insertions(+), 18 deletions(-)arch/riscv/include/asm/kvm_host.h +0
-0@@ -43,6 +43,22 @@ KVM_ARCH_REQ_FLAGS(5, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)#define KVM_REQ_STEAL_UPDATE KVM_ARCH_REQ(6)Quoted 8 lines
arch/riscv/include/asm/kvm_host.h line 51
Quoted 6 lines
arch/riscv/include/asm/kvm_host.h +6
-0+#define KVM_HEDELEG_DEFAULT ((_AC(1, UL) << EXC_INST_MISALIGNED) | \ + (_AC(1, UL) << EXC_BREAKPOINT) | \ + (_AC(1, UL) << EXC_SYSCALL) | \ + (_AC(1, UL) << EXC_INST_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_LOAD_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_STORE_PAGE_FAULT))Use BIT(xyz) here. For example:
BIT(EXC_INST_MISALIGNED)Thanks, I will use
BIT()instead in next revision.Quoted 3 lines
Also,
BIT(EXC_BREAKPOINT)should not be part ofKVM_HEDELEG_DEFAULT.I think the bit
EXC_BREAKPOINTshould be set by default, like what you already did inkvm_arch_hardware_enable(). Then the VS could get the ebreak and handle it accordingly.Quoted 6 lines
Quoted 3 lines
If the
guest_debugis enabled, ebreak instructions are inserted by the userspace(QEMU). So KVM should 'intercept' the ebreak and exit to QEMU. BitEXC_BREAKPOINTshould be cleared in this case.If
EXC_BREAKPOINTis delegated by default then it is not consistent withvcpu->guest_debugwhich is not enabled by default.
Quoted 6 lines
To enable the
guest_debugcorresponding to NOT delegate theEXC_BREAKPOINT. They are somehow 'opposite'.This '
kvm_guest_debug' feature is different from "debug in the guest". The later requires the delegation ofEXC_BREAKPOINT.
The former does not.
In which case your below code is totally misleading.
+ if (dbg->control & KVM_GUESTDBG_ENABLE) {
+ vcpu->guest_debug = dbg->control;
+ vcpu->arch.cfg.hedeleg = KVM_HEDELEG_GUEST_DEBUG;
+ } else {
+ vcpu->guest_debug = 0;
+ vcpu->arch.cfg.hedeleg = KVM_HEDELEG_DEFAULT;
+ }
This should have been:
+ if (dbg->control & KVM_GUESTDBG_ENABLE) {
+ vcpu->guest_debug = dbg->control;
+ vcpu->arch.cfg.hedeleg &= ~BIT(EXC_BREAKPOINT);
+ } else {
+ vcpu->guest_debug = 0;
+ vcpu->arch.cfg.hedeleg |= BIT(EXC_BREAKPOINT);
+ }
Quoted 97 lines
Quoted 86 lines
Quoted 86 lines
Quoted 85 lines
arch/riscv/include/asm/kvm_host.h line 56
Quoted 9 lines
arch/riscv/include/asm/kvm_host.h +5
-0+#define KVM_HEDELEG_GUEST_DEBUG ((_AC(1, UL) << EXC_INST_MISALIGNED) | \ + (_AC(1, UL) << EXC_SYSCALL) | \ + (_AC(1, UL) << EXC_INST_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_LOAD_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_STORE_PAGE_FAULT))No need for
KVM_HEDELEG_GUEST_DEBUG, see below.arch/riscv/include/asm/kvm_host.h line 61
Quoted 5 lines
arch/riscv/include/asm/kvm_host.h +5
-0+ +#define KVM_HIDELEG_DEFAULT ((_AC(1, UL) << IRQ_VS_SOFT) | \ + (_AC(1, UL) << IRQ_VS_TIMER) | \ + (_AC(1, UL) << IRQ_VS_EXT)) +Same as above, use BIT(xyz) here.
Quoted 57 lines
arch/riscv/include/asm/kvm_host.h +1
-0enum kvm_riscv_hfence_type {KVM_RISCV_HFENCE_UNKNOWN =0 ,KVM_RISCV_HFENCE_GVMA_VMID_GPA ,@@ -169,6 +185,7 @@ struct kvm_vcpu_csr {struct kvm_vcpu_config {u64 henvcfg;u64 hstateen0; +unsigned long hedeleg; };struct kvm_vcpu_smstateen_csr {arch/riscv/include/uapi/asm/kvm.h +1
-0@@ -17,6 +17,7 @@#define __KVM_HAVE_IRQ_LINE#define __KVM_HAVE_READONLY_MEM +#define __KVM_HAVE_GUEST_DEBUG#define KVM_COALESCED_MMIO_PAGE_OFFSET 1arch/riscv/kvm/main.c +2
-16@@ -22,22 +22,8 @@ long kvm_arch_dev_ioctl(struct file *filp,int kvm_arch_hardware_enable (void ) { -unsigned long hideleg, hedeleg; - - hedeleg =0 ; - hedeleg |= (1UL <<EXC_INST_MISALIGNED ); - hedeleg |= (1UL <<EXC_BREAKPOINT ); - hedeleg |= (1UL <<EXC_SYSCALL ); - hedeleg |= (1UL <<EXC_INST_PAGE_FAULT ); - hedeleg |= (1UL <<EXC_LOAD_PAGE_FAULT ); - hedeleg |= (1UL <<EXC_STORE_PAGE_FAULT ); -csr_write (CSR_HEDELEG , hedeleg); - - hideleg =0 ; - hideleg |= (1UL <<IRQ_VS_SOFT ); - hideleg |= (1UL <<IRQ_VS_TIMER ); - hideleg |= (1UL <<IRQ_VS_EXT ); -csr_write (CSR_HIDELEG , hideleg); +csr_write (CSR_HEDELEG ,KVM_HEDELEG_DEFAULT ); +csr_write (CSR_HIDELEG ,KVM_HIDELEG_DEFAULT );/* VS should access only the time counter directly. Everything else should trap */ csr_write (CSR_HCOUNTEREN ,0x02 );arch/riscv/kvm/vcpu.c +0
-0arch/riscv/kvm/vcpu.c int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, · line 479
Quoted 5 lines
arch/riscv/kvm/vcpu.c +0
-2@@ -475,8 +475,15 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,int kvm_arch_vcpu_ioctl_set_guest_debug (struct kvm_vcpu *vcpu,struct kvm_guest_debug *dbg) { -/* TODO; To be implemented later. */ -return -EINVAL ;if (
vcpu->arch.ran_atleast_once)return -EBUSY;Quoted 6 lines
Quoted 2 lines
If we enabled the
guest_debugin QEMU side, then theKVM_SET_GUEST_DEBUGioctl will come before the firstKVM_RUN. This will always cause an ERROR.The check ensures that KVM user space can only enable/disable guest debug before the VCPU is run. I don't see why this would fail for QEMU.
In the current implementation of GDB and QEMU, the userspace will enable/disable
guest_debugfrequently during the debugging (almost every step). The sequence should like:
Quoted 10 lines
KVM_SET_GUEST_DEBUGenableKVM_RUNKVM_SET_GUEST_DEBUGdisableKVM_SET_GUEST_DEBUGenableKVM_RUNKVM_SET_GUEST_DEBUGdisableKVM_SET_GUEST_DEBUGenableKVM_RUNKVM_SET_GUEST_DEBUGdisable
...
Fair enough, no need to check "ran_atleast_once"
Quoted 88 lines
Quoted 41 lines
Quoted 41 lines
Quoted 31 lines
arch/riscv/kvm/vcpu.c int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, · line 484
Quoted 12 lines
arch/riscv/kvm/vcpu.c +7
-0+if (dbg->control &KVM_GUESTDBG_ENABLE ) { + vcpu->guest_debug = dbg->control; + vcpu->arch.cfg.hedeleg =KVM_HEDELEG_GUEST_DEBUG ; + }else { + vcpu->guest_debug =0 ; + vcpu->arch.cfg.hedeleg =KVM_HEDELEG_DEFAULT ; + }Don't update
vcpu->arch.cfg.hedeleghere since it should be only done inkvm_riscv_vcpu_setup_config().arch/riscv/kvm/vcpu.c static void kvm_riscv_vcpu_setup_config(struct kvm_vcpu *vcpu) · line 517
Quoted 12 lines
arch/riscv/kvm/vcpu.c +5
-0+ +return 0 ; }static void kvm_riscv_vcpu_setup_config (struct kvm_vcpu *vcpu)@@ -505,6 +512,9 @@ static void kvm_riscv_vcpu_setup_config(struct kvm_vcpu *vcpu)if (riscv_isa_extension_available (isa,SMSTATEEN )) cfg->hstateen0 |=SMSTATEEN0_SSTATEEN0 ; } + +if (!vcpu->guest_debug) + cfg->hedeleg =KVM_HEDELEG_DEFAULT ;This should be:
Quoted 3 lines
cfg->hedeleg=KVM_HEDELEG_DEFAULT;
if (vcpu->guest_debug)cfg->hedeleg |= BIT(EXC_BREAKPOINT);Like above, here the logic should be:
cfg->hedeleg=KVM_HEDELEG_DEFAULT; // withBIT(EXC_BREAKPOINT)if (vcpu->guest_debug)cfg->hedeleg &= ~BIT(EXC_BREAKPOINT);Quoted 5 lines
Quoted 3 lines
Another approach is:
initialize thecfg->hedelegasKVM_HEDELEG_DEFAULTduringkvm_arch_vcpu_create(). Besides that, only update thecfg->hedeleginkvm_arch_vcpu_ioctl_set_guest_debug().I disagree. We should handle hedeleg just like we handle henvcfg.
OK, let's only update the
cfg->hedeleginkvm_riscv_vcpu_setup_config().Quoted 33 lines
Quoted 33 lines
Quoted 32 lines
Quoted 27 lines
arch/riscv/kvm/vcpu.c +1
-0}void kvm_arch_vcpu_load (struct kvm_vcpu *vcpu,int cpu)@@ -519,6 +529,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)csr_write (CSR_VSEPC , csr->vsepc);csr_write (CSR_VSCAUSE , csr->vscause);csr_write (CSR_VSTVAL , csr->vstval); +csr_write (CSR_HEDELEG , cfg->hedeleg);csr_write (CSR_HVIP , csr->hvip);csr_write (CSR_VSATP , csr->vsatp);csr_write (CSR_HENVCFG , cfg->henvcfg);arch/riscv/kvm/vm.c +1
-1@@ -186,6 +186,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_READONLY_MEM :case KVM_CAP_MP_STATE :case KVM_CAP_IMMEDIATE_EXIT : +case KVM_CAP_SET_GUEST_DEBUG : r =1 ;break ;case KVM_CAP_NR_VCPUS : --Quoted 2 lines
2.17.1
Regards,
AnupQuoted 5 lines
Quoted 2 lines
Thanks,
ChaoRegards,
Anup
Quoted 6 lines
Thanks,
Chao
--
kvm-riscv mailing list
kvm-riscv at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv
Regards,
Anup
Re: [PATCH v2 1/3] RISC-V: KVM: Implement kvm_arch_vcpu_ioctl_set_guest_debug()
Thanks Anup.
Let me try to summarize the changes in next revision:
1. Use BIT().
2. In kvm_arch_vcpu_ioctl_set_guest_debug(), do below things:
if (dbg->control & KVM_GUESTDBG_ENABLE) {
vcpu->guest_debug = dbg->control;
} else {
vcpu->guest_debug = 0;
}
3. In kvm_riscv_vcpu_setup_config(), do below things:
cfg->hedeleg = KVM_HEDELEG_DEFAULT; // with BIT(EXC_BREAKPOINT)
if (vcpu->guest_debug)
cfg->hedeleg &= ~BIT(EXC_BREAKPOINT);Will prepare a PATCH v3 accordingly.
Regards,
Chao
Quoted 297 lines · Anup Patel
Quoted 61 lines · On Fri, Mar 1, 2024 at 12:57?PM Chao Du <[email protected]> wrote:
Quoted 52 lines · Anup Patel
Quoted 50 lines · On Fri, Mar 1, 2024 at 12:05?PM Chao Du <[email protected]> wrote:
Quoted 29 lines · Anup Patel
Quoted 27 lines · On Fri, Mar 1, 2024 at 7:08?AM Chao Du <[email protected]> wrote:
kvm_vm_ioctl_check_extension(): Return 1 ifKVM_CAP_SET_GUEST_DEBUGis been checked.
kvm_arch_vcpu_ioctl_set_guest_debug(): Update theguest_debugflags from userspace accordingly. Route the breakpoint exceptions to HS mode if the VCPU is being debugged by userspace, by clearing the corresponding bit in hedeleg. Write the actual CSR inkvm_arch_vcpu_load().Signed-off-by Chao Du <[email protected]>--- arch/riscv/include/asm/kvm_host.h | 17 +++++++++++++++++ arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kvm/main.c | 18 ++---------------- arch/riscv/kvm/vcpu.c | 15 +++++++++++++-- arch/riscv/kvm/vm.c | 1 + 5 files changed, 34 insertions(+), 18 deletions(-)arch/riscv/include/asm/kvm_host.h +0
-0@@ -43,6 +43,22 @@ KVM_ARCH_REQ_FLAGS(5, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)#define KVM_REQ_STEAL_UPDATE KVM_ARCH_REQ(6)Quoted 8 lines
arch/riscv/include/asm/kvm_host.h line 51
Quoted 6 lines
arch/riscv/include/asm/kvm_host.h +6
-0+#define KVM_HEDELEG_DEFAULT ((_AC(1, UL) << EXC_INST_MISALIGNED) | \ + (_AC(1, UL) << EXC_BREAKPOINT) | \ + (_AC(1, UL) << EXC_SYSCALL) | \ + (_AC(1, UL) << EXC_INST_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_LOAD_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_STORE_PAGE_FAULT))Use BIT(xyz) here. For example:
BIT(EXC_INST_MISALIGNED)Thanks, I will use
BIT()instead in next revision.Quoted 3 lines
Also,
BIT(EXC_BREAKPOINT)should not be part ofKVM_HEDELEG_DEFAULT.I think the bit
EXC_BREAKPOINTshould be set by default, like what you already did inkvm_arch_hardware_enable(). Then the VS could get the ebreak and handle it accordingly.Quoted 6 lines
Quoted 3 lines
If the
guest_debugis enabled, ebreak instructions are inserted by the userspace(QEMU). So KVM should 'intercept' the ebreak and exit to QEMU. BitEXC_BREAKPOINTshould be cleared in this case.If
EXC_BREAKPOINTis delegated by default then it is not consistent withvcpu->guest_debugwhich is not enabled by default.Quoted 6 lines
To enable the
guest_debugcorresponding to NOT delegate theEXC_BREAKPOINT. They are somehow 'opposite'.This '
kvm_guest_debug' feature is different from "debug in the guest". The later requires the delegation ofEXC_BREAKPOINT.
The former does not.In which case your below code is totally misleading.
arch/riscv/kvm/vcpu.c +7
-0+if (dbg->control &KVM_GUESTDBG_ENABLE ) { + vcpu->guest_debug = dbg->control; + vcpu->arch.cfg.hedeleg =KVM_HEDELEG_GUEST_DEBUG ; + }else { + vcpu->guest_debug =0 ; + vcpu->arch.cfg.hedeleg =KVM_HEDELEG_DEFAULT ; + }This should have been:
arch/riscv/kvm/vcpu.c +7
-0+if (dbg->control &KVM_GUESTDBG_ENABLE ) { + vcpu->guest_debug = dbg->control; + vcpu->arch.cfg.hedeleg &= ~BIT (EXC_BREAKPOINT ); + }else { + vcpu->guest_debug =0 ; + vcpu->arch.cfg.hedeleg |=BIT (EXC_BREAKPOINT ); + }Quoted 98 lines
Quoted 87 lines
Quoted 87 lines
Quoted 86 lines
arch/riscv/include/asm/kvm_host.h line 56
Quoted 10 lines
arch/riscv/include/asm/kvm_host.h +5
-0+#define KVM_HEDELEG_GUEST_DEBUG ((_AC(1, UL) << EXC_INST_MISALIGNED) | \ + (_AC(1, UL) << EXC_SYSCALL) | \ + (_AC(1, UL) << EXC_INST_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_LOAD_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_STORE_PAGE_FAULT))No need for
KVM_HEDELEG_GUEST_DEBUG, see below.arch/riscv/include/asm/kvm_host.h line 61
Quoted 5 lines
arch/riscv/include/asm/kvm_host.h +5
-0+ +#define KVM_HIDELEG_DEFAULT ((_AC(1, UL) << IRQ_VS_SOFT) | \ + (_AC(1, UL) << IRQ_VS_TIMER) | \ + (_AC(1, UL) << IRQ_VS_EXT)) +Same as above, use BIT(xyz) here.
Quoted 57 lines
arch/riscv/include/asm/kvm_host.h +1
-0enum kvm_riscv_hfence_type {KVM_RISCV_HFENCE_UNKNOWN =0 ,KVM_RISCV_HFENCE_GVMA_VMID_GPA ,@@ -169,6 +185,7 @@ struct kvm_vcpu_csr {struct kvm_vcpu_config {u64 henvcfg;u64 hstateen0; +unsigned long hedeleg; };struct kvm_vcpu_smstateen_csr {arch/riscv/include/uapi/asm/kvm.h +1
-0@@ -17,6 +17,7 @@#define __KVM_HAVE_IRQ_LINE#define __KVM_HAVE_READONLY_MEM +#define __KVM_HAVE_GUEST_DEBUG#define KVM_COALESCED_MMIO_PAGE_OFFSET 1arch/riscv/kvm/main.c +2
-16@@ -22,22 +22,8 @@ long kvm_arch_dev_ioctl(struct file *filp,int kvm_arch_hardware_enable (void ) { -unsigned long hideleg, hedeleg; - - hedeleg =0 ; - hedeleg |= (1UL <<EXC_INST_MISALIGNED ); - hedeleg |= (1UL <<EXC_BREAKPOINT ); - hedeleg |= (1UL <<EXC_SYSCALL ); - hedeleg |= (1UL <<EXC_INST_PAGE_FAULT ); - hedeleg |= (1UL <<EXC_LOAD_PAGE_FAULT ); - hedeleg |= (1UL <<EXC_STORE_PAGE_FAULT ); -csr_write (CSR_HEDELEG , hedeleg); - - hideleg =0 ; - hideleg |= (1UL <<IRQ_VS_SOFT ); - hideleg |= (1UL <<IRQ_VS_TIMER ); - hideleg |= (1UL <<IRQ_VS_EXT ); -csr_write (CSR_HIDELEG , hideleg); +csr_write (CSR_HEDELEG ,KVM_HEDELEG_DEFAULT ); +csr_write (CSR_HIDELEG ,KVM_HIDELEG_DEFAULT );/* VS should access only the time counter directly. Everything else should trap */ csr_write (CSR_HCOUNTEREN ,0x02 );arch/riscv/kvm/vcpu.c +0
-0arch/riscv/kvm/vcpu.c int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, · line 479
Quoted 5 lines
arch/riscv/kvm/vcpu.c +0
-2@@ -475,8 +475,15 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,int kvm_arch_vcpu_ioctl_set_guest_debug (struct kvm_vcpu *vcpu,struct kvm_guest_debug *dbg) { -/* TODO; To be implemented later. */ -return -EINVAL ;if (
vcpu->arch.ran_atleast_once)return -EBUSY;Quoted 6 lines
Quoted 2 lines
If we enabled the
guest_debugin QEMU side, then theKVM_SET_GUEST_DEBUGioctl will come before the firstKVM_RUN. This will always cause an ERROR.The check ensures that KVM user space can only enable/disable guest debug before the VCPU is run. I don't see why this would fail for QEMU.
In the current implementation of GDB and QEMU, the userspace will enable/disable
guest_debugfrequently during the debugging (almost every step). The sequence should like:Quoted 10 lines
KVM_SET_GUEST_DEBUGenableKVM_RUNKVM_SET_GUEST_DEBUGdisableKVM_SET_GUEST_DEBUGenableKVM_RUNKVM_SET_GUEST_DEBUGdisableKVM_SET_GUEST_DEBUGenableKVM_RUNKVM_SET_GUEST_DEBUGdisable
...Fair enough, no need to check "
ran_atleast_once"Quoted 88 lines
Quoted 41 lines
Quoted 41 lines
Quoted 31 lines
arch/riscv/kvm/vcpu.c int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, · line 484
Quoted 12 lines
arch/riscv/kvm/vcpu.c +7
-0+if (dbg->control &KVM_GUESTDBG_ENABLE ) { + vcpu->guest_debug = dbg->control; + vcpu->arch.cfg.hedeleg =KVM_HEDELEG_GUEST_DEBUG ; + }else { + vcpu->guest_debug =0 ; + vcpu->arch.cfg.hedeleg =KVM_HEDELEG_DEFAULT ; + }Don't update
vcpu->arch.cfg.hedeleghere since it should be only done inkvm_riscv_vcpu_setup_config().arch/riscv/kvm/vcpu.c static void kvm_riscv_vcpu_setup_config(struct kvm_vcpu *vcpu) · line 517
Quoted 12 lines
arch/riscv/kvm/vcpu.c +5
-0+ +return 0 ; }static void kvm_riscv_vcpu_setup_config (struct kvm_vcpu *vcpu)@@ -505,6 +512,9 @@ static void kvm_riscv_vcpu_setup_config(struct kvm_vcpu *vcpu)if (riscv_isa_extension_available (isa,SMSTATEEN )) cfg->hstateen0 |=SMSTATEEN0_SSTATEEN0 ; } + +if (!vcpu->guest_debug) + cfg->hedeleg =KVM_HEDELEG_DEFAULT ;This should be:
Quoted 3 lines
cfg->hedeleg=KVM_HEDELEG_DEFAULT;
if (vcpu->guest_debug)cfg->hedeleg |= BIT(EXC_BREAKPOINT);Like above, here the logic should be:
cfg->hedeleg=KVM_HEDELEG_DEFAULT; // withBIT(EXC_BREAKPOINT)if (vcpu->guest_debug)cfg->hedeleg &= ~BIT(EXC_BREAKPOINT);Quoted 5 lines
Quoted 3 lines
Another approach is:
initialize thecfg->hedelegasKVM_HEDELEG_DEFAULTduringkvm_arch_vcpu_create(). Besides that, only update thecfg->hedeleginkvm_arch_vcpu_ioctl_set_guest_debug().I disagree. We should handle hedeleg just like we handle henvcfg.
OK, let's only update the
cfg->hedeleginkvm_riscv_vcpu_setup_config().Quoted 33 lines
Quoted 33 lines
Quoted 32 lines
Quoted 27 lines
arch/riscv/kvm/vcpu.c +1
-0}void kvm_arch_vcpu_load (struct kvm_vcpu *vcpu,int cpu)@@ -519,6 +529,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)csr_write (CSR_VSEPC , csr->vsepc);csr_write (CSR_VSCAUSE , csr->vscause);csr_write (CSR_VSTVAL , csr->vstval); +csr_write (CSR_HEDELEG , cfg->hedeleg);csr_write (CSR_HVIP , csr->hvip);csr_write (CSR_VSATP , csr->vsatp);csr_write (CSR_HENVCFG , cfg->henvcfg);arch/riscv/kvm/vm.c +1
-1@@ -186,6 +186,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_READONLY_MEM :case KVM_CAP_MP_STATE :case KVM_CAP_IMMEDIATE_EXIT : +case KVM_CAP_SET_GUEST_DEBUG : r =1 ;break ;case KVM_CAP_NR_VCPUS : --Quoted 2 lines
2.17.1
Regards,
AnupQuoted 5 lines
Quoted 2 lines
Thanks,
ChaoRegards,
AnupQuoted 6 lines
Thanks,
Chao
--
kvm-riscv mailing list
kvm-riscv at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscvRegards,
Anup
Anup Patel
maintainerRe: [PATCH v2 1/3] RISC-V: KVM: Implement kvm_arch_vcpu_ioctl_set_guest_debug()
Quoted 11 lines · On Fri, Mar 1, 2024 at 1:46?PM Chao Du <[email protected]> wrote:
Thanks Anup.
Let me try to summarize the changes in next revision:1. Use
BIT().
2. Inkvm_arch_vcpu_ioctl_set_guest_debug(), do below things:if (dbg->control & KVM_GUESTDBG_ENABLE) { vcpu->guest_debug = dbg->control; } else { vcpu->guest_debug = 0; }
Since, kvm_arch_vcpu_ioctl_set_guest_debug() is called multiple times at runtime, this should be:
if (dbg->control & KVM_GUESTDBG_ENABLE) {
vcpu->guest_debug = dbg->control;
vcpu->arch.cfg.hedeleg &= ~BIT(EXC_BREAKPOINT);
} else {
vcpu->guest_debug = 0;
vcpu->arch.cfg.hedeleg |= BIT(EXC_BREAKPOINT);
}Quoted 6 lines
3. In
kvm_riscv_vcpu_setup_config(), do below things:cfg->hedeleg = KVM_HEDELEG_DEFAULT; // with BIT(EXC_BREAKPOINT) if (vcpu->guest_debug) cfg->hedeleg &= ~BIT(EXC_BREAKPOINT);Will prepare a PATCH v3 accordingly.
Yes, please send v3.
Thanks,
Anup
Quoted 298 lines · Anup Patel
Quoted 297 lines · On Fri, Mar 1, 2024 at 12:57?PM Chao Du <[email protected]> wrote:
Quoted 61 lines · Anup Patel
Quoted 52 lines · On Fri, Mar 1, 2024 at 12:05?PM Chao Du <[email protected]> wrote:
Quoted 50 lines · Anup Patel
Quoted 29 lines · On Fri, Mar 1, 2024 at 7:08?AM Chao Du <[email protected]> wrote:
Quoted 27 lines
kvm_vm_ioctl_check_extension(): Return 1 ifKVM_CAP_SET_GUEST_DEBUGis been checked.
kvm_arch_vcpu_ioctl_set_guest_debug(): Update theguest_debugflags from userspace accordingly. Route the breakpoint exceptions to HS mode if the VCPU is being debugged by userspace, by clearing the corresponding bit in hedeleg. Write the actual CSR inkvm_arch_vcpu_load().Signed-off-by Chao Du <[email protected]>--- arch/riscv/include/asm/kvm_host.h | 17 +++++++++++++++++ arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kvm/main.c | 18 ++---------------- arch/riscv/kvm/vcpu.c | 15 +++++++++++++-- arch/riscv/kvm/vm.c | 1 + 5 files changed, 34 insertions(+), 18 deletions(-)arch/riscv/include/asm/kvm_host.h +0
-0@@ -43,6 +43,22 @@ KVM_ARCH_REQ_FLAGS(5, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)#define KVM_REQ_STEAL_UPDATE KVM_ARCH_REQ(6)Quoted 8 lines
arch/riscv/include/asm/kvm_host.h line 51
Quoted 6 lines
arch/riscv/include/asm/kvm_host.h +6
-0+#define KVM_HEDELEG_DEFAULT ((_AC(1, UL) << EXC_INST_MISALIGNED) | \ + (_AC(1, UL) << EXC_BREAKPOINT) | \ + (_AC(1, UL) << EXC_SYSCALL) | \ + (_AC(1, UL) << EXC_INST_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_LOAD_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_STORE_PAGE_FAULT))Use BIT(xyz) here. For example:
BIT(EXC_INST_MISALIGNED)Thanks, I will use
BIT()instead in next revision.Quoted 3 lines
Also,
BIT(EXC_BREAKPOINT)should not be part ofKVM_HEDELEG_DEFAULT.I think the bit
EXC_BREAKPOINTshould be set by default, like what you already did inkvm_arch_hardware_enable(). Then the VS could get the ebreak and handle it accordingly.Quoted 6 lines
Quoted 3 lines
If the
guest_debugis enabled, ebreak instructions are inserted by the userspace(QEMU). So KVM should 'intercept' the ebreak and exit to QEMU. BitEXC_BREAKPOINTshould be cleared in this case.If
EXC_BREAKPOINTis delegated by default then it is not consistent withvcpu->guest_debugwhich is not enabled by default.Quoted 6 lines
To enable the
guest_debugcorresponding to NOT delegate theEXC_BREAKPOINT. They are somehow 'opposite'.This '
kvm_guest_debug' feature is different from "debug in the guest". The later requires the delegation ofEXC_BREAKPOINT.
The former does not.In which case your below code is totally misleading.
(quoted diff) +7
-0+ if (dbg->control & KVM_GUESTDBG_ENABLE) { + vcpu->guest_debug = dbg->control; + vcpu->arch.cfg.hedeleg = KVM_HEDELEG_GUEST_DEBUG; + } else { + vcpu->guest_debug = 0; + vcpu->arch.cfg.hedeleg = KVM_HEDELEG_DEFAULT; + }This should have been:
(quoted diff) +7
-0+ if (dbg->control & KVM_GUESTDBG_ENABLE) { + vcpu->guest_debug = dbg->control; + vcpu->arch.cfg.hedeleg &= ~BIT(EXC_BREAKPOINT); + } else { + vcpu->guest_debug = 0; + vcpu->arch.cfg.hedeleg |= BIT(EXC_BREAKPOINT); + }Quoted 98 lines
Quoted 87 lines
Quoted 87 lines
Quoted 86 lines
arch/riscv/include/asm/kvm_host.h line 56
Quoted 10 lines
arch/riscv/include/asm/kvm_host.h +5
-0+#define KVM_HEDELEG_GUEST_DEBUG ((_AC(1, UL) << EXC_INST_MISALIGNED) | \ + (_AC(1, UL) << EXC_SYSCALL) | \ + (_AC(1, UL) << EXC_INST_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_LOAD_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_STORE_PAGE_FAULT))No need for
KVM_HEDELEG_GUEST_DEBUG, see below.arch/riscv/include/asm/kvm_host.h line 61
Quoted 5 lines
arch/riscv/include/asm/kvm_host.h +5
-0+ +#define KVM_HIDELEG_DEFAULT ((_AC(1, UL) << IRQ_VS_SOFT) | \ + (_AC(1, UL) << IRQ_VS_TIMER) | \ + (_AC(1, UL) << IRQ_VS_EXT)) +Same as above, use BIT(xyz) here.
Quoted 57 lines
arch/riscv/include/asm/kvm_host.h +1
-0enum kvm_riscv_hfence_type {KVM_RISCV_HFENCE_UNKNOWN =0 ,KVM_RISCV_HFENCE_GVMA_VMID_GPA ,@@ -169,6 +185,7 @@ struct kvm_vcpu_csr {struct kvm_vcpu_config {u64 henvcfg;u64 hstateen0; +unsigned long hedeleg; };struct kvm_vcpu_smstateen_csr {arch/riscv/include/uapi/asm/kvm.h +1
-0@@ -17,6 +17,7 @@#define __KVM_HAVE_IRQ_LINE#define __KVM_HAVE_READONLY_MEM +#define __KVM_HAVE_GUEST_DEBUG#define KVM_COALESCED_MMIO_PAGE_OFFSET 1arch/riscv/kvm/main.c +2
-16@@ -22,22 +22,8 @@ long kvm_arch_dev_ioctl(struct file *filp,int kvm_arch_hardware_enable (void ) { -unsigned long hideleg, hedeleg; - - hedeleg =0 ; - hedeleg |= (1UL <<EXC_INST_MISALIGNED ); - hedeleg |= (1UL <<EXC_BREAKPOINT ); - hedeleg |= (1UL <<EXC_SYSCALL ); - hedeleg |= (1UL <<EXC_INST_PAGE_FAULT ); - hedeleg |= (1UL <<EXC_LOAD_PAGE_FAULT ); - hedeleg |= (1UL <<EXC_STORE_PAGE_FAULT ); -csr_write (CSR_HEDELEG , hedeleg); - - hideleg =0 ; - hideleg |= (1UL <<IRQ_VS_SOFT ); - hideleg |= (1UL <<IRQ_VS_TIMER ); - hideleg |= (1UL <<IRQ_VS_EXT ); -csr_write (CSR_HIDELEG , hideleg); +csr_write (CSR_HEDELEG ,KVM_HEDELEG_DEFAULT ); +csr_write (CSR_HIDELEG ,KVM_HIDELEG_DEFAULT );/* VS should access only the time counter directly. Everything else should trap */ csr_write (CSR_HCOUNTEREN ,0x02 );arch/riscv/kvm/vcpu.c +0
-0arch/riscv/kvm/vcpu.c int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, · line 479
Quoted 5 lines
arch/riscv/kvm/vcpu.c +0
-2@@ -475,8 +475,15 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,int kvm_arch_vcpu_ioctl_set_guest_debug (struct kvm_vcpu *vcpu,struct kvm_guest_debug *dbg) { -/* TODO; To be implemented later. */ -return -EINVAL ;if (
vcpu->arch.ran_atleast_once)return -EBUSY;Quoted 6 lines
Quoted 2 lines
If we enabled the
guest_debugin QEMU side, then theKVM_SET_GUEST_DEBUGioctl will come before the firstKVM_RUN. This will always cause an ERROR.The check ensures that KVM user space can only enable/disable guest debug before the VCPU is run. I don't see why this would fail for QEMU.
In the current implementation of GDB and QEMU, the userspace will enable/disable
guest_debugfrequently during the debugging (almost every step). The sequence should like:Quoted 10 lines
KVM_SET_GUEST_DEBUGenableKVM_RUNKVM_SET_GUEST_DEBUGdisableKVM_SET_GUEST_DEBUGenableKVM_RUNKVM_SET_GUEST_DEBUGdisableKVM_SET_GUEST_DEBUGenableKVM_RUNKVM_SET_GUEST_DEBUGdisable
...Fair enough, no need to check "
ran_atleast_once"Quoted 88 lines
Quoted 41 lines
Quoted 41 lines
Quoted 31 lines
arch/riscv/kvm/vcpu.c int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, · line 484
Quoted 12 lines
arch/riscv/kvm/vcpu.c +7
-0+if (dbg->control &KVM_GUESTDBG_ENABLE ) { + vcpu->guest_debug = dbg->control; + vcpu->arch.cfg.hedeleg =KVM_HEDELEG_GUEST_DEBUG ; + }else { + vcpu->guest_debug =0 ; + vcpu->arch.cfg.hedeleg =KVM_HEDELEG_DEFAULT ; + }Don't update
vcpu->arch.cfg.hedeleghere since it should be only done inkvm_riscv_vcpu_setup_config().arch/riscv/kvm/vcpu.c static void kvm_riscv_vcpu_setup_config(struct kvm_vcpu *vcpu) · line 517
Quoted 12 lines
arch/riscv/kvm/vcpu.c +5
-0+ +return 0 ; }static void kvm_riscv_vcpu_setup_config (struct kvm_vcpu *vcpu)@@ -505,6 +512,9 @@ static void kvm_riscv_vcpu_setup_config(struct kvm_vcpu *vcpu)if (riscv_isa_extension_available (isa,SMSTATEEN )) cfg->hstateen0 |=SMSTATEEN0_SSTATEEN0 ; } + +if (!vcpu->guest_debug) + cfg->hedeleg =KVM_HEDELEG_DEFAULT ;This should be:
Quoted 3 lines
cfg->hedeleg=KVM_HEDELEG_DEFAULT;
if (vcpu->guest_debug)cfg->hedeleg |= BIT(EXC_BREAKPOINT);Like above, here the logic should be:
cfg->hedeleg=KVM_HEDELEG_DEFAULT; // withBIT(EXC_BREAKPOINT)if (vcpu->guest_debug)cfg->hedeleg &= ~BIT(EXC_BREAKPOINT);Quoted 5 lines
Quoted 3 lines
Another approach is:
initialize thecfg->hedelegasKVM_HEDELEG_DEFAULTduringkvm_arch_vcpu_create(). Besides that, only update thecfg->hedeleginkvm_arch_vcpu_ioctl_set_guest_debug().I disagree. We should handle hedeleg just like we handle henvcfg.
OK, let's only update the
cfg->hedeleginkvm_riscv_vcpu_setup_config().Quoted 33 lines
Quoted 33 lines
Quoted 32 lines
Quoted 27 lines
arch/riscv/kvm/vcpu.c +1
-0}void kvm_arch_vcpu_load (struct kvm_vcpu *vcpu,int cpu)@@ -519,6 +529,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)csr_write (CSR_VSEPC , csr->vsepc);csr_write (CSR_VSCAUSE , csr->vscause);csr_write (CSR_VSTVAL , csr->vstval); +csr_write (CSR_HEDELEG , cfg->hedeleg);csr_write (CSR_HVIP , csr->hvip);csr_write (CSR_VSATP , csr->vsatp);csr_write (CSR_HENVCFG , cfg->henvcfg);arch/riscv/kvm/vm.c +1
-1@@ -186,6 +186,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_READONLY_MEM :case KVM_CAP_MP_STATE :case KVM_CAP_IMMEDIATE_EXIT : +case KVM_CAP_SET_GUEST_DEBUG : r =1 ;break ;case KVM_CAP_NR_VCPUS : --Quoted 2 lines
2.17.1
Regards,
AnupQuoted 5 lines
Quoted 2 lines
Thanks,
ChaoRegards,
AnupQuoted 6 lines
Thanks,
Chao
--
kvm-riscv mailing list
kvm-riscv at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscvRegards,
Anup
Re: [PATCH v2 1/3] RISC-V: KVM: Implement kvm_arch_vcpu_ioctl_set_guest_debug()
Quoted 17 lines · Anup Patel
Quoted 11 lines · On Fri, Mar 1, 2024 at 1:46?PM Chao Du <[email protected]> wrote:
Thanks Anup.
Let me try to summarize the changes in next revision:1. Use
BIT().
2. Inkvm_arch_vcpu_ioctl_set_guest_debug(), do below things:if (dbg->control & KVM_GUESTDBG_ENABLE) { vcpu->guest_debug = dbg->control; } else { vcpu->guest_debug = 0; }Since,
kvm_arch_vcpu_ioctl_set_guest_debug()is called multiple times at runtime, this should be:
Quoted 14 lines
if (dbg->control & KVM_GUESTDBG_ENABLE) { vcpu->guest_debug = dbg->control; vcpu->arch.cfg.hedeleg &= ~BIT(EXC_BREAKPOINT); } else { vcpu->guest_debug = 0; vcpu->arch.cfg.hedeleg |= BIT(EXC_BREAKPOINT); }Quoted 6 lines
3. In
kvm_riscv_vcpu_setup_config(), do below things:cfg->hedeleg = KVM_HEDELEG_DEFAULT; // with BIT(EXC_BREAKPOINT) if (vcpu->guest_debug) cfg->hedeleg &= ~BIT(EXC_BREAKPOINT);Will prepare a PATCH v3 accordingly.
I thought it over, maybe we should do this:
- In kvm_arch_vcpu_ioctl_set_guest_debug():
if (dbg->control & KVM_GUESTDBG_ENABLE) {
vcpu->guest_debug = dbg->control;
vcpu->arch.cfg.hedeleg = KVM_HEDELEG_DEFAULT & (~BIT(EXC_BREAKPOINT));
} else {
vcpu->guest_debug = 0;
vcpu->arch.cfg.hedeleg = KVM_HEDELEG_DEFAULT;
}
- In kvm_riscv_vcpu_setup_config():
if (!vcpu->guest_debug)
cfg->hedeleg = KVM_HEDELEG_DEFAULT;which is similar with my original changes, but clear the bit EXC_BREAKPOINT explicitly.
Could you please confirm ?
Quoted 304 lines
Yes, please send v3.
Thanks,
AnupQuoted 298 lines · Anup Patel
Quoted 297 lines · On Fri, Mar 1, 2024 at 12:57?PM Chao Du <[email protected]> wrote:
Quoted 61 lines · Anup Patel
Quoted 52 lines · On Fri, Mar 1, 2024 at 12:05?PM Chao Du <[email protected]> wrote:
Quoted 50 lines · Anup Patel
Quoted 29 lines · On Fri, Mar 1, 2024 at 7:08?AM Chao Du <[email protected]> wrote:
Quoted 27 lines
kvm_vm_ioctl_check_extension(): Return 1 ifKVM_CAP_SET_GUEST_DEBUGis been checked.
kvm_arch_vcpu_ioctl_set_guest_debug(): Update theguest_debugflags from userspace accordingly. Route the breakpoint exceptions to HS mode if the VCPU is being debugged by userspace, by clearing the corresponding bit in hedeleg. Write the actual CSR inkvm_arch_vcpu_load().Signed-off-by Chao Du <[email protected]>--- arch/riscv/include/asm/kvm_host.h | 17 +++++++++++++++++ arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kvm/main.c | 18 ++---------------- arch/riscv/kvm/vcpu.c | 15 +++++++++++++-- arch/riscv/kvm/vm.c | 1 + 5 files changed, 34 insertions(+), 18 deletions(-)arch/riscv/include/asm/kvm_host.h +0
-0@@ -43,6 +43,22 @@ KVM_ARCH_REQ_FLAGS(5, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)#define KVM_REQ_STEAL_UPDATE KVM_ARCH_REQ(6)Quoted 8 lines
arch/riscv/include/asm/kvm_host.h line 51
Quoted 6 lines
arch/riscv/include/asm/kvm_host.h +6
-0+#define KVM_HEDELEG_DEFAULT ((_AC(1, UL) << EXC_INST_MISALIGNED) | \ + (_AC(1, UL) << EXC_BREAKPOINT) | \ + (_AC(1, UL) << EXC_SYSCALL) | \ + (_AC(1, UL) << EXC_INST_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_LOAD_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_STORE_PAGE_FAULT))Use BIT(xyz) here. For example:
BIT(EXC_INST_MISALIGNED)Thanks, I will use
BIT()instead in next revision.Quoted 3 lines
Also,
BIT(EXC_BREAKPOINT)should not be part ofKVM_HEDELEG_DEFAULT.I think the bit
EXC_BREAKPOINTshould be set by default, like what you already did inkvm_arch_hardware_enable(). Then the VS could get the ebreak and handle it accordingly.Quoted 6 lines
Quoted 3 lines
If the
guest_debugis enabled, ebreak instructions are inserted by the userspace(QEMU). So KVM should 'intercept' the ebreak and exit to QEMU. BitEXC_BREAKPOINTshould be cleared in this case.If
EXC_BREAKPOINTis delegated by default then it is not consistent withvcpu->guest_debugwhich is not enabled by default.Quoted 6 lines
To enable the
guest_debugcorresponding to NOT delegate theEXC_BREAKPOINT. They are somehow 'opposite'.This '
kvm_guest_debug' feature is different from "debug in the guest". The later requires the delegation ofEXC_BREAKPOINT.
The former does not.In which case your below code is totally misleading.
(quoted diff) +7
-0+ if (dbg->control & KVM_GUESTDBG_ENABLE) { + vcpu->guest_debug = dbg->control; + vcpu->arch.cfg.hedeleg = KVM_HEDELEG_GUEST_DEBUG; + } else { + vcpu->guest_debug = 0; + vcpu->arch.cfg.hedeleg = KVM_HEDELEG_DEFAULT; + }This should have been:
(quoted diff) +7
-0+ if (dbg->control & KVM_GUESTDBG_ENABLE) { + vcpu->guest_debug = dbg->control; + vcpu->arch.cfg.hedeleg &= ~BIT(EXC_BREAKPOINT); + } else { + vcpu->guest_debug = 0; + vcpu->arch.cfg.hedeleg |= BIT(EXC_BREAKPOINT); + }Quoted 98 lines
Quoted 87 lines
Quoted 87 lines
Quoted 86 lines
arch/riscv/include/asm/kvm_host.h line 56
Quoted 10 lines
arch/riscv/include/asm/kvm_host.h +5
-0+#define KVM_HEDELEG_GUEST_DEBUG ((_AC(1, UL) << EXC_INST_MISALIGNED) | \ + (_AC(1, UL) << EXC_SYSCALL) | \ + (_AC(1, UL) << EXC_INST_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_LOAD_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_STORE_PAGE_FAULT))No need for
KVM_HEDELEG_GUEST_DEBUG, see below.arch/riscv/include/asm/kvm_host.h line 61
Quoted 5 lines
arch/riscv/include/asm/kvm_host.h +5
-0+ +#define KVM_HIDELEG_DEFAULT ((_AC(1, UL) << IRQ_VS_SOFT) | \ + (_AC(1, UL) << IRQ_VS_TIMER) | \ + (_AC(1, UL) << IRQ_VS_EXT)) +Same as above, use BIT(xyz) here.
Quoted 57 lines
arch/riscv/include/asm/kvm_host.h +1
-0enum kvm_riscv_hfence_type {KVM_RISCV_HFENCE_UNKNOWN =0 ,KVM_RISCV_HFENCE_GVMA_VMID_GPA ,@@ -169,6 +185,7 @@ struct kvm_vcpu_csr {struct kvm_vcpu_config {u64 henvcfg;u64 hstateen0; +unsigned long hedeleg; };struct kvm_vcpu_smstateen_csr {arch/riscv/include/uapi/asm/kvm.h +1
-0@@ -17,6 +17,7 @@#define __KVM_HAVE_IRQ_LINE#define __KVM_HAVE_READONLY_MEM +#define __KVM_HAVE_GUEST_DEBUG#define KVM_COALESCED_MMIO_PAGE_OFFSET 1arch/riscv/kvm/main.c +2
-16@@ -22,22 +22,8 @@ long kvm_arch_dev_ioctl(struct file *filp,int kvm_arch_hardware_enable (void ) { -unsigned long hideleg, hedeleg; - - hedeleg =0 ; - hedeleg |= (1UL <<EXC_INST_MISALIGNED ); - hedeleg |= (1UL <<EXC_BREAKPOINT ); - hedeleg |= (1UL <<EXC_SYSCALL ); - hedeleg |= (1UL <<EXC_INST_PAGE_FAULT ); - hedeleg |= (1UL <<EXC_LOAD_PAGE_FAULT ); - hedeleg |= (1UL <<EXC_STORE_PAGE_FAULT ); -csr_write (CSR_HEDELEG , hedeleg); - - hideleg =0 ; - hideleg |= (1UL <<IRQ_VS_SOFT ); - hideleg |= (1UL <<IRQ_VS_TIMER ); - hideleg |= (1UL <<IRQ_VS_EXT ); -csr_write (CSR_HIDELEG , hideleg); +csr_write (CSR_HEDELEG ,KVM_HEDELEG_DEFAULT ); +csr_write (CSR_HIDELEG ,KVM_HIDELEG_DEFAULT );/* VS should access only the time counter directly. Everything else should trap */ csr_write (CSR_HCOUNTEREN ,0x02 );arch/riscv/kvm/vcpu.c +0
-0arch/riscv/kvm/vcpu.c int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, · line 479
Quoted 5 lines
arch/riscv/kvm/vcpu.c +0
-2@@ -475,8 +475,15 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,int kvm_arch_vcpu_ioctl_set_guest_debug (struct kvm_vcpu *vcpu,struct kvm_guest_debug *dbg) { -/* TODO; To be implemented later. */ -return -EINVAL ;if (
vcpu->arch.ran_atleast_once)return -EBUSY;Quoted 6 lines
Quoted 2 lines
If we enabled the
guest_debugin QEMU side, then theKVM_SET_GUEST_DEBUGioctl will come before the firstKVM_RUN. This will always cause an ERROR.The check ensures that KVM user space can only enable/disable guest debug before the VCPU is run. I don't see why this would fail for QEMU.
In the current implementation of GDB and QEMU, the userspace will enable/disable
guest_debugfrequently during the debugging (almost every step). The sequence should like:Quoted 10 lines
KVM_SET_GUEST_DEBUGenableKVM_RUNKVM_SET_GUEST_DEBUGdisableKVM_SET_GUEST_DEBUGenableKVM_RUNKVM_SET_GUEST_DEBUGdisableKVM_SET_GUEST_DEBUGenableKVM_RUNKVM_SET_GUEST_DEBUGdisable
...Fair enough, no need to check "
ran_atleast_once"Quoted 88 lines
Quoted 41 lines
Quoted 41 lines
Quoted 31 lines
arch/riscv/kvm/vcpu.c int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, · line 484
Quoted 12 lines
arch/riscv/kvm/vcpu.c +7
-0+if (dbg->control &KVM_GUESTDBG_ENABLE ) { + vcpu->guest_debug = dbg->control; + vcpu->arch.cfg.hedeleg =KVM_HEDELEG_GUEST_DEBUG ; + }else { + vcpu->guest_debug =0 ; + vcpu->arch.cfg.hedeleg =KVM_HEDELEG_DEFAULT ; + }Don't update
vcpu->arch.cfg.hedeleghere since it should be only done inkvm_riscv_vcpu_setup_config().arch/riscv/kvm/vcpu.c static void kvm_riscv_vcpu_setup_config(struct kvm_vcpu *vcpu) · line 517
Quoted 12 lines
arch/riscv/kvm/vcpu.c +5
-0+ +return 0 ; }static void kvm_riscv_vcpu_setup_config (struct kvm_vcpu *vcpu)@@ -505,6 +512,9 @@ static void kvm_riscv_vcpu_setup_config(struct kvm_vcpu *vcpu)if (riscv_isa_extension_available (isa,SMSTATEEN )) cfg->hstateen0 |=SMSTATEEN0_SSTATEEN0 ; } + +if (!vcpu->guest_debug) + cfg->hedeleg =KVM_HEDELEG_DEFAULT ;This should be:
Quoted 3 lines
cfg->hedeleg=KVM_HEDELEG_DEFAULT;
if (vcpu->guest_debug)cfg->hedeleg |= BIT(EXC_BREAKPOINT);Like above, here the logic should be:
cfg->hedeleg=KVM_HEDELEG_DEFAULT; // withBIT(EXC_BREAKPOINT)if (vcpu->guest_debug)cfg->hedeleg &= ~BIT(EXC_BREAKPOINT);Quoted 5 lines
Quoted 3 lines
Another approach is:
initialize thecfg->hedelegasKVM_HEDELEG_DEFAULTduringkvm_arch_vcpu_create(). Besides that, only update thecfg->hedeleginkvm_arch_vcpu_ioctl_set_guest_debug().I disagree. We should handle hedeleg just like we handle henvcfg.
OK, let's only update the
cfg->hedeleginkvm_riscv_vcpu_setup_config().Quoted 33 lines
Quoted 33 lines
Quoted 32 lines
Quoted 27 lines
arch/riscv/kvm/vcpu.c +1
-0}void kvm_arch_vcpu_load (struct kvm_vcpu *vcpu,int cpu)@@ -519,6 +529,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)csr_write (CSR_VSEPC , csr->vsepc);csr_write (CSR_VSCAUSE , csr->vscause);csr_write (CSR_VSTVAL , csr->vstval); +csr_write (CSR_HEDELEG , cfg->hedeleg);csr_write (CSR_HVIP , csr->hvip);csr_write (CSR_VSATP , csr->vsatp);csr_write (CSR_HENVCFG , cfg->henvcfg);arch/riscv/kvm/vm.c +1
-1@@ -186,6 +186,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_READONLY_MEM :case KVM_CAP_MP_STATE :case KVM_CAP_IMMEDIATE_EXIT : +case KVM_CAP_SET_GUEST_DEBUG : r =1 ;break ;case KVM_CAP_NR_VCPUS : --Quoted 2 lines
2.17.1
Regards,
AnupQuoted 5 lines
Quoted 2 lines
Thanks,
ChaoRegards,
AnupQuoted 6 lines
Thanks,
Chao
--
kvm-riscv mailing list
kvm-riscv at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscvRegards,
Anup
Re: [PATCH v2 3/3] RISC-V: KVM: selftests: Add breakpoints test support
In reply to Chao DuQuoted 37 lines · Chao Du
Initial support for RISC-V KVM breakpoint test. Check the exit reason and the PC when guest debug is enabled.
Signed-off-by Chao Du <[email protected]>--- tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/riscv/breakpoints.c | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 tools/testing/selftests/kvm/riscv/breakpoints.ctools/testing/selftests/kvm/Makefile +1
-0@@ -184,6 +184,7 @@ TEST_GEN_PROGS_s390x += rseq_test TEST_GEN_PROGS_s390x += set_memory_region_test TEST_GEN_PROGS_s390x += kvm_binary_stats_test +TEST_GEN_PROGS_riscv += riscv/breakpoints TEST_GEN_PROGS_riscv += demand_paging_test TEST_GEN_PROGS_riscv += dirty_log_test TEST_GEN_PROGS_riscv += get-reg-listtools/testing/selftests/kvm/riscv/breakpoints.c new file +9
-0@@ -0,0 +1,49 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* RISC-V KVM breakpoint tests. +* +* Copyright 2024 Beijing ESWIN Computing Technology Co., Ltd. +* +*/ +#include "kvm_util.h" +
tools/testing/selftests/kvm/riscv/breakpoints.c line 19
Quoted 10 lines
tools/testing/selftests/kvm/riscv/breakpoints.c new file +10
-0+#define PC (v) ((uint64_t)&(v)) + +extern unsigned char sw_bp; + +static void guest_code (void ) +{ + asmvolatile ("sw_bp: ebreak" ); + asmvolatile ("nop" ); + asmvolatile ("nop" ); + asmvolatile ("nop" );
What are the nops for? And, since they're all in their own asm()'s the compiler could be inserting instructions between them and also the ebreak above. If we need three nops immediately following the ebreak then we need to put everything in one asm()
asm volatile(
"sw_bp: ebreak\n"
" nop\n"
" nop\n"
" nop\n");
Quoted 7 lines
tools/testing/selftests/kvm/riscv/breakpoints.c new file +7
-0+ +GUEST_DONE (); +} + +int main (void ) +{ +struct kvm_vm *vm;
tools/testing/selftests/kvm/riscv/breakpoints.c line 36
Quoted 10 lines
tools/testing/selftests/kvm/riscv/breakpoints.c new file +10
-0+struct kvm_vcpu *vcpu; +struct kvm_guest_debug debug; +uint64_t pc; + +TEST_REQUIRE (kvm_has_cap (KVM_CAP_SET_GUEST_DEBUG )); + + vm =vm_create_with_one_vcpu (&vcpu, guest_code); + +memset (&debug,0 ,sizeof (debug)); + debug.control =KVM_GUESTDBG_ENABLE ;
nit: The above two lines can be removed if we initialize debug as
struct kvm_guest_debug debug = {
.control = KVM_GUESTDBG_ENABLE,
};tools/testing/selftests/kvm/riscv/breakpoints.c line 40
Quoted 4 lines
tools/testing/selftests/kvm/riscv/breakpoints.c new file +4
-0+vcpu_guest_debug_set (vcpu, &debug); +vcpu_run (vcpu); + +TEST_ASSERT_KVM_EXIT_REASON (vcpu,KVM_EXIT_DEBUG );
As Anup pointed out, we need to also ensure that without making the KVM_SET_GUEST_DEBUG ioctl call we get the expected behavior. You can use GUEST_SYNC() in the guest code to prove that it was able to issue an ebreak without exiting to the VMM.
Quoted 12 lines
tools/testing/selftests/kvm/riscv/breakpoints.c new file +9
-0+ +vcpu_get_reg (vcpu,RISCV_CORE_REG (regs.pc), &pc); + +TEST_ASSERT_EQ (pc,PC (sw_bp)); + +kvm_vm_free (vm); + +return 0 ; +}Signature · 1 line
2.17.1
Thanks,
drew
Re: [PATCH v2 3/3] RISC-V: KVM: selftests: Add breakpoints test support
Quoted 55 lines · Andrew Jones
Quoted 37 lines · Chao Du
Initial support for RISC-V KVM breakpoint test. Check the exit reason and the PC when guest debug is enabled.
Signed-off-by Chao Du <[email protected]>--- tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/riscv/breakpoints.c | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 tools/testing/selftests/kvm/riscv/breakpoints.ctools/testing/selftests/kvm/Makefile +1
-0@@ -184,6 +184,7 @@ TEST_GEN_PROGS_s390x += rseq_test TEST_GEN_PROGS_s390x += set_memory_region_test TEST_GEN_PROGS_s390x += kvm_binary_stats_test +TEST_GEN_PROGS_riscv += riscv/breakpoints TEST_GEN_PROGS_riscv += demand_paging_test TEST_GEN_PROGS_riscv += dirty_log_test TEST_GEN_PROGS_riscv += get-reg-listtools/testing/selftests/kvm/riscv/breakpoints.c new file +9
-0@@ -0,0 +1,49 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* RISC-V KVM breakpoint tests. +* +* Copyright 2024 Beijing ESWIN Computing Technology Co., Ltd. +* +*/ +#include "kvm_util.h" +tools/testing/selftests/kvm/riscv/breakpoints.c line 19
Quoted 10 lines
tools/testing/selftests/kvm/riscv/breakpoints.c new file +10
-0+#define PC (v) ((uint64_t)&(v)) + +extern unsigned char sw_bp; + +static void guest_code (void ) +{ + asmvolatile ("sw_bp: ebreak" ); + asmvolatile ("nop" ); + asmvolatile ("nop" ); + asmvolatile ("nop" );What are the nops for? And, since they're all in their own asm()'s the compiler could be inserting instructions between them and also the ebreak above. If we need three nops immediately following the ebreak then we need to put everything in one
asm()
Quoted 6 lines
asm volatile(
"sw_bp: ebreak\n"
" nop\n"
" nop\n"
" nop\n");
Actually the nops have no special purpose, I will remove and just keep the ebreak here.
Quoted 20 lines
Quoted 7 lines
tools/testing/selftests/kvm/riscv/breakpoints.c new file +7
-0+ +GUEST_DONE (); +} + +int main (void ) +{ +struct kvm_vm *vm;tools/testing/selftests/kvm/riscv/breakpoints.c line 36
Quoted 10 lines
tools/testing/selftests/kvm/riscv/breakpoints.c new file +10
-0+struct kvm_vcpu *vcpu; +struct kvm_guest_debug debug; +uint64_t pc; + +TEST_REQUIRE (kvm_has_cap (KVM_CAP_SET_GUEST_DEBUG )); + + vm =vm_create_with_one_vcpu (&vcpu, guest_code); + +memset (&debug,0 ,sizeof (debug)); + debug.control =KVM_GUESTDBG_ENABLE ;nit: The above two lines can be removed if we initialize debug as
Quoted 4 lines
struct kvm_guest_debug debug = {
.control = KVM_GUESTDBG_ENABLE,
};Yeah, I will do so in the next patch.
Quoted 10 lines
tools/testing/selftests/kvm/riscv/breakpoints.c line 40
Quoted 4 lines
tools/testing/selftests/kvm/riscv/breakpoints.c new file +4
-0+vcpu_guest_debug_set (vcpu, &debug); +vcpu_run (vcpu); + +TEST_ASSERT_KVM_EXIT_REASON (vcpu,KVM_EXIT_DEBUG );As Anup pointed out, we need to also ensure that without making the
KVM_SET_GUEST_DEBUGioctl call we get the expected behavior. You can useGUEST_SYNC()in the guest code to prove that it was able to issue an ebreak without exiting to the VMM.
Sure, will cover that case also.
Quoted 15 lines
Quoted 12 lines
tools/testing/selftests/kvm/riscv/breakpoints.c new file +9
-0+ +vcpu_get_reg (vcpu,RISCV_CORE_REG (regs.pc), &pc); + +TEST_ASSERT_EQ (pc,PC (sw_bp)); + +kvm_vm_free (vm); + +return 0 ; +}Signature · 1 line
2.17.1Thanks,
drew
Re: [PATCH v2 1/3] RISC-V: KVM: Implement kvm_arch_vcpu_ioctl_set_guest_debug()
In reply to Chao DuQuoted 48 lines · On Fri, Mar 1, 2024 at 2:53?PM Chao Du <[email protected]> wrote:
Quoted 17 lines · Anup Patel
Quoted 11 lines · On Fri, Mar 1, 2024 at 1:46?PM Chao Du <[email protected]> wrote:
Thanks Anup.
Let me try to summarize the changes in next revision:1. Use
BIT().
2. Inkvm_arch_vcpu_ioctl_set_guest_debug(), do below things:if (dbg->control & KVM_GUESTDBG_ENABLE) { vcpu->guest_debug = dbg->control; } else { vcpu->guest_debug = 0; }Since,
kvm_arch_vcpu_ioctl_set_guest_debug()is called multiple times at runtime, this should be:Quoted 14 lines
if (dbg->control & KVM_GUESTDBG_ENABLE) { vcpu->guest_debug = dbg->control; vcpu->arch.cfg.hedeleg &= ~BIT(EXC_BREAKPOINT); } else { vcpu->guest_debug = 0; vcpu->arch.cfg.hedeleg |= BIT(EXC_BREAKPOINT); }Quoted 6 lines
3. In
kvm_riscv_vcpu_setup_config(), do below things:cfg->hedeleg = KVM_HEDELEG_DEFAULT; // with BIT(EXC_BREAKPOINT) if (vcpu->guest_debug) cfg->hedeleg &= ~BIT(EXC_BREAKPOINT);Will prepare a PATCH v3 accordingly.
I thought it over, maybe we should do this:
- In
kvm_arch_vcpu_ioctl_set_guest_debug():if (dbg->control & KVM_GUESTDBG_ENABLE) { vcpu->guest_debug = dbg->control; vcpu->arch.cfg.hedeleg = KVM_HEDELEG_DEFAULT & (~BIT(EXC_BREAKPOINT)); } else { vcpu->guest_debug = 0; vcpu->arch.cfg.hedeleg = KVM_HEDELEG_DEFAULT; } - In kvm_riscv_vcpu_setup_config(): if (!vcpu->guest_debug) cfg->hedeleg = KVM_HEDELEG_DEFAULT;
Quoted 3 lines
which is similar with my original changes, but clear the bit
EXC_BREAKPOINTexplicitly.Could you please confirm ?
I suggest going with my previous suggestion.
Regards,
Anup
Quoted 305 lines
Quoted 305 lines
Yes, please send v3.
Thanks,
AnupQuoted 298 lines · Anup Patel
Quoted 297 lines · On Fri, Mar 1, 2024 at 12:57?PM Chao Du <[email protected]> wrote:
Quoted 61 lines · Anup Patel
Quoted 52 lines · On Fri, Mar 1, 2024 at 12:05?PM Chao Du <[email protected]> wrote:
Quoted 50 lines · Anup Patel
Quoted 29 lines · On Fri, Mar 1, 2024 at 7:08?AM Chao Du <[email protected]> wrote:
Quoted 27 lines
kvm_vm_ioctl_check_extension(): Return 1 ifKVM_CAP_SET_GUEST_DEBUGis been checked.
kvm_arch_vcpu_ioctl_set_guest_debug(): Update theguest_debugflags from userspace accordingly. Route the breakpoint exceptions to HS mode if the VCPU is being debugged by userspace, by clearing the corresponding bit in hedeleg. Write the actual CSR inkvm_arch_vcpu_load().Signed-off-by Chao Du <[email protected]>--- arch/riscv/include/asm/kvm_host.h | 17 +++++++++++++++++ arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kvm/main.c | 18 ++---------------- arch/riscv/kvm/vcpu.c | 15 +++++++++++++-- arch/riscv/kvm/vm.c | 1 + 5 files changed, 34 insertions(+), 18 deletions(-)arch/riscv/include/asm/kvm_host.h +0
-0@@ -43,6 +43,22 @@ KVM_ARCH_REQ_FLAGS(5, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)#define KVM_REQ_STEAL_UPDATE KVM_ARCH_REQ(6)Quoted 8 lines
arch/riscv/include/asm/kvm_host.h line 51
Quoted 6 lines
arch/riscv/include/asm/kvm_host.h +6
-0+#define KVM_HEDELEG_DEFAULT ((_AC(1, UL) << EXC_INST_MISALIGNED) | \ + (_AC(1, UL) << EXC_BREAKPOINT) | \ + (_AC(1, UL) << EXC_SYSCALL) | \ + (_AC(1, UL) << EXC_INST_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_LOAD_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_STORE_PAGE_FAULT))Use BIT(xyz) here. For example:
BIT(EXC_INST_MISALIGNED)Thanks, I will use
BIT()instead in next revision.Quoted 3 lines
Also,
BIT(EXC_BREAKPOINT)should not be part ofKVM_HEDELEG_DEFAULT.I think the bit
EXC_BREAKPOINTshould be set by default, like what you already did inkvm_arch_hardware_enable(). Then the VS could get the ebreak and handle it accordingly.Quoted 6 lines
Quoted 3 lines
If the
guest_debugis enabled, ebreak instructions are inserted by the userspace(QEMU). So KVM should 'intercept' the ebreak and exit to QEMU. BitEXC_BREAKPOINTshould be cleared in this case.If
EXC_BREAKPOINTis delegated by default then it is not consistent withvcpu->guest_debugwhich is not enabled by default.Quoted 6 lines
To enable the
guest_debugcorresponding to NOT delegate theEXC_BREAKPOINT. They are somehow 'opposite'.This '
kvm_guest_debug' feature is different from "debug in the guest". The later requires the delegation ofEXC_BREAKPOINT.
The former does not.In which case your below code is totally misleading.
(quoted diff) +7
-0+ if (dbg->control & KVM_GUESTDBG_ENABLE) { + vcpu->guest_debug = dbg->control; + vcpu->arch.cfg.hedeleg = KVM_HEDELEG_GUEST_DEBUG; + } else { + vcpu->guest_debug = 0; + vcpu->arch.cfg.hedeleg = KVM_HEDELEG_DEFAULT; + }This should have been:
(quoted diff) +7
-0+ if (dbg->control & KVM_GUESTDBG_ENABLE) { + vcpu->guest_debug = dbg->control; + vcpu->arch.cfg.hedeleg &= ~BIT(EXC_BREAKPOINT); + } else { + vcpu->guest_debug = 0; + vcpu->arch.cfg.hedeleg |= BIT(EXC_BREAKPOINT); + }Quoted 98 lines
Quoted 87 lines
Quoted 87 lines
Quoted 86 lines
arch/riscv/include/asm/kvm_host.h line 56
Quoted 10 lines
arch/riscv/include/asm/kvm_host.h +5
-0+#define KVM_HEDELEG_GUEST_DEBUG ((_AC(1, UL) << EXC_INST_MISALIGNED) | \ + (_AC(1, UL) << EXC_SYSCALL) | \ + (_AC(1, UL) << EXC_INST_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_LOAD_PAGE_FAULT) | \ + (_AC(1, UL) << EXC_STORE_PAGE_FAULT))No need for
KVM_HEDELEG_GUEST_DEBUG, see below.arch/riscv/include/asm/kvm_host.h line 61
Quoted 5 lines
arch/riscv/include/asm/kvm_host.h +5
-0+ +#define KVM_HIDELEG_DEFAULT ((_AC(1, UL) << IRQ_VS_SOFT) | \ + (_AC(1, UL) << IRQ_VS_TIMER) | \ + (_AC(1, UL) << IRQ_VS_EXT)) +Same as above, use BIT(xyz) here.
Quoted 57 lines
arch/riscv/include/asm/kvm_host.h +1
-0enum kvm_riscv_hfence_type {KVM_RISCV_HFENCE_UNKNOWN =0 ,KVM_RISCV_HFENCE_GVMA_VMID_GPA ,@@ -169,6 +185,7 @@ struct kvm_vcpu_csr {struct kvm_vcpu_config {u64 henvcfg;u64 hstateen0; +unsigned long hedeleg; };struct kvm_vcpu_smstateen_csr {arch/riscv/include/uapi/asm/kvm.h +1
-0@@ -17,6 +17,7 @@#define __KVM_HAVE_IRQ_LINE#define __KVM_HAVE_READONLY_MEM +#define __KVM_HAVE_GUEST_DEBUG#define KVM_COALESCED_MMIO_PAGE_OFFSET 1arch/riscv/kvm/main.c +2
-16@@ -22,22 +22,8 @@ long kvm_arch_dev_ioctl(struct file *filp,int kvm_arch_hardware_enable (void ) { -unsigned long hideleg, hedeleg; - - hedeleg =0 ; - hedeleg |= (1UL <<EXC_INST_MISALIGNED ); - hedeleg |= (1UL <<EXC_BREAKPOINT ); - hedeleg |= (1UL <<EXC_SYSCALL ); - hedeleg |= (1UL <<EXC_INST_PAGE_FAULT ); - hedeleg |= (1UL <<EXC_LOAD_PAGE_FAULT ); - hedeleg |= (1UL <<EXC_STORE_PAGE_FAULT ); -csr_write (CSR_HEDELEG , hedeleg); - - hideleg =0 ; - hideleg |= (1UL <<IRQ_VS_SOFT ); - hideleg |= (1UL <<IRQ_VS_TIMER ); - hideleg |= (1UL <<IRQ_VS_EXT ); -csr_write (CSR_HIDELEG , hideleg); +csr_write (CSR_HEDELEG ,KVM_HEDELEG_DEFAULT ); +csr_write (CSR_HIDELEG ,KVM_HIDELEG_DEFAULT );/* VS should access only the time counter directly. Everything else should trap */ csr_write (CSR_HCOUNTEREN ,0x02 );arch/riscv/kvm/vcpu.c +0
-0arch/riscv/kvm/vcpu.c int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, · line 479
Quoted 5 lines
arch/riscv/kvm/vcpu.c +0
-2@@ -475,8 +475,15 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,int kvm_arch_vcpu_ioctl_set_guest_debug (struct kvm_vcpu *vcpu,struct kvm_guest_debug *dbg) { -/* TODO; To be implemented later. */ -return -EINVAL ;if (
vcpu->arch.ran_atleast_once)return -EBUSY;Quoted 6 lines
Quoted 2 lines
If we enabled the
guest_debugin QEMU side, then theKVM_SET_GUEST_DEBUGioctl will come before the firstKVM_RUN. This will always cause an ERROR.The check ensures that KVM user space can only enable/disable guest debug before the VCPU is run. I don't see why this would fail for QEMU.
In the current implementation of GDB and QEMU, the userspace will enable/disable
guest_debugfrequently during the debugging (almost every step). The sequence should like:Quoted 10 lines
KVM_SET_GUEST_DEBUGenableKVM_RUNKVM_SET_GUEST_DEBUGdisableKVM_SET_GUEST_DEBUGenableKVM_RUNKVM_SET_GUEST_DEBUGdisableKVM_SET_GUEST_DEBUGenableKVM_RUNKVM_SET_GUEST_DEBUGdisable
...Fair enough, no need to check "
ran_atleast_once"Quoted 88 lines
Quoted 41 lines
Quoted 41 lines
Quoted 31 lines
arch/riscv/kvm/vcpu.c int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, · line 484
Quoted 12 lines
arch/riscv/kvm/vcpu.c +7
-0+if (dbg->control &KVM_GUESTDBG_ENABLE ) { + vcpu->guest_debug = dbg->control; + vcpu->arch.cfg.hedeleg =KVM_HEDELEG_GUEST_DEBUG ; + }else { + vcpu->guest_debug =0 ; + vcpu->arch.cfg.hedeleg =KVM_HEDELEG_DEFAULT ; + }Don't update
vcpu->arch.cfg.hedeleghere since it should be only done inkvm_riscv_vcpu_setup_config().arch/riscv/kvm/vcpu.c static void kvm_riscv_vcpu_setup_config(struct kvm_vcpu *vcpu) · line 517
Quoted 12 lines
arch/riscv/kvm/vcpu.c +5
-0+ +return 0 ; }static void kvm_riscv_vcpu_setup_config (struct kvm_vcpu *vcpu)@@ -505,6 +512,9 @@ static void kvm_riscv_vcpu_setup_config(struct kvm_vcpu *vcpu)if (riscv_isa_extension_available (isa,SMSTATEEN )) cfg->hstateen0 |=SMSTATEEN0_SSTATEEN0 ; } + +if (!vcpu->guest_debug) + cfg->hedeleg =KVM_HEDELEG_DEFAULT ;This should be:
Quoted 3 lines
cfg->hedeleg=KVM_HEDELEG_DEFAULT;
if (vcpu->guest_debug)cfg->hedeleg |= BIT(EXC_BREAKPOINT);Like above, here the logic should be:
cfg->hedeleg=KVM_HEDELEG_DEFAULT; // withBIT(EXC_BREAKPOINT)if (vcpu->guest_debug)cfg->hedeleg &= ~BIT(EXC_BREAKPOINT);Quoted 5 lines
Quoted 3 lines
Another approach is:
initialize thecfg->hedelegasKVM_HEDELEG_DEFAULTduringkvm_arch_vcpu_create(). Besides that, only update thecfg->hedeleginkvm_arch_vcpu_ioctl_set_guest_debug().I disagree. We should handle hedeleg just like we handle henvcfg.
OK, let's only update the
cfg->hedeleginkvm_riscv_vcpu_setup_config().Quoted 33 lines
Quoted 33 lines
Quoted 32 lines
Quoted 27 lines
arch/riscv/kvm/vcpu.c +1
-0}void kvm_arch_vcpu_load (struct kvm_vcpu *vcpu,int cpu)@@ -519,6 +529,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)csr_write (CSR_VSEPC , csr->vsepc);csr_write (CSR_VSCAUSE , csr->vscause);csr_write (CSR_VSTVAL , csr->vstval); +csr_write (CSR_HEDELEG , cfg->hedeleg);csr_write (CSR_HVIP , csr->hvip);csr_write (CSR_VSATP , csr->vsatp);csr_write (CSR_HENVCFG , cfg->henvcfg);arch/riscv/kvm/vm.c +1
-1@@ -186,6 +186,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_READONLY_MEM :case KVM_CAP_MP_STATE :case KVM_CAP_IMMEDIATE_EXIT : +case KVM_CAP_SET_GUEST_DEBUG : r =1 ;break ;case KVM_CAP_NR_VCPUS : --Quoted 2 lines
2.17.1
Regards,
AnupQuoted 5 lines
Quoted 2 lines
Thanks,
ChaoRegards,
AnupQuoted 6 lines
Thanks,
Chao
--
kvm-riscv mailing list
kvm-riscv at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscvRegards,
Anup