Skip to content

Commit 18f3976

Browse files
Alexandru Eliseictmarinas
authored andcommitted
KVM: arm64: uapi: Add kvm_debug_exit_arch.hsr_high
When userspace is debugging a VM, the kvm_debug_exit_arch part of the kvm_run struct contains arm64 specific debug information: the ESR_EL2 value, encoded in the field "hsr", and the address of the instruction that caused the exception, encoded in the field "far". Linux has moved to treating ESR_EL2 as a 64-bit register, but unfortunately kvm_debug_exit_arch.hsr cannot be changed because that would change the memory layout of the struct on big endian machines: Current layout: | Layout with "hsr" extended to 64 bits: | offset 0: ESR_EL2[31:0] (hsr) | offset 0: ESR_EL2[61:32] (hsr[61:32]) offset 4: padding | offset 4: ESR_EL2[31:0] (hsr[31:0]) offset 8: FAR_EL2[61:0] (far) | offset 8: FAR_EL2[61:0] (far) which breaks existing code. The padding is inserted by the compiler because the "far" field must be aligned to 8 bytes (each field must be naturally aligned - aapcs64 [1], page 18), and the struct itself must be aligned to 8 bytes (the struct must be aligned to the maximum alignment of its fields - aapcs64, page 18), which means that "hsr" must be aligned to 8 bytes as it is the first field in the struct. To avoid changing the struct size and layout for the existing fields, add a new field, "hsr_high", which replaces the existing padding. "hsr_high" will be used to hold the ESR_EL2[61:32] bits of the register. The memory layout, both on big and little endian machine, becomes: offset 0: ESR_EL2[31:0] (hsr) offset 4: ESR_EL2[61:32] (hsr_high) offset 8: FAR_EL2[61:0] (far) The padding that the compiler inserts for the current struct layout is unitialized. To prevent an updated userspace running on an old kernel mistaking the padding for a valid "hsr_high" value, add a new flag, KVM_DEBUG_ARCH_HSR_HIGH_VALID, to kvm_run->flags to let userspace know that "hsr_high" holds a valid ESR_EL2[61:32] value. [1] https://github.com/ARM-software/abi-aa/releases/download/2021Q3/aapcs64.pdf Signed-off-by: Alexandru Elisei <alexandru.elisei@arm.com> Reviewed-by: Marc Zyngier <maz@kernel.org> Link: https://lore.kernel.org/r/20220425114444.368693-6-alexandru.elisei@arm.com Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
1 parent 0b12620 commit 18f3976

File tree

4 files changed

+7
-0
lines changed

4 files changed

+7
-0
lines changed

Documentation/virt/kvm/api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5713,6 +5713,8 @@ affect the device's behavior. Current defined flags::
57135713
#define KVM_RUN_X86_SMM (1 << 0)
57145714
/* x86, set if bus lock detected in VM */
57155715
#define KVM_RUN_BUS_LOCK (1 << 1)
5716+
/* arm64, set for KVM_EXIT_DEBUG */
5717+
#define KVM_DEBUG_ARCH_HSR_HIGH_VALID (1 << 0)
57165718

57175719
::
57185720

arch/arm64/include/uapi/asm/kvm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,10 @@ struct kvm_guest_debug_arch {
139139
__u64 dbg_wvr[KVM_ARM_MAX_DBG_REGS];
140140
};
141141

142+
#define KVM_DEBUG_ARCH_HSR_HIGH_VALID (1 << 0)
142143
struct kvm_debug_exit_arch {
143144
__u32 hsr;
145+
__u32 hsr_high; /* ESR_EL2[61:32] */
144146
__u64 far; /* used for watchpoints */
145147
};
146148

arch/arm64/kvm/arm.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
783783

784784
ret = 1;
785785
run->exit_reason = KVM_EXIT_UNKNOWN;
786+
run->flags = 0;
786787
while (ret > 0) {
787788
/*
788789
* Check conditions before entering the guest

arch/arm64/kvm/handle_exit.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ static int kvm_handle_guest_debug(struct kvm_vcpu *vcpu)
121121

122122
run->exit_reason = KVM_EXIT_DEBUG;
123123
run->debug.arch.hsr = lower_32_bits(esr);
124+
run->debug.arch.hsr_high = upper_32_bits(esr);
125+
run->flags = KVM_DEBUG_ARCH_HSR_HIGH_VALID;
124126

125127
if (ESR_ELx_EC(esr) == ESR_ELx_EC_WATCHPT_LOW)
126128
run->debug.arch.far = vcpu->arch.fault.far_el2;

0 commit comments

Comments
 (0)