Skip to content

Commit 2776421

Browse files
danielhbavpatel
authored andcommitted
RISC-V: KVM: provide UAPI for host SATP mode
KVM userspaces need to be aware of the host SATP to allow them to advertise it back to the guest OS. Since this information is used to build the guest FDT we can't wait for the SATP reg to be readable. We just need to read the SATP mode, thus we can use the existing 'satp_mode' global that represents the SATP reg with MODE set and both ASID and PPN cleared. E.g. for a 32 bit host running with sv32 satp_mode is 0x80000000, for a 64 bit host running sv57 satp_mode is 0xa000000000000000, and so on. Add a new userspace virtual config register 'satp_mode' to allow userspace to read the current SATP mode the host is using with GET_ONE_REG API before spinning the vcpu. 'satp_mode' can't be changed via KVM, so SET_ONE_REG is allowed as long as userspace writes the existing 'satp_mode'. Signed-off-by: Daniel Henrique Barboza <dbarboza@ventanamicro.com> Reviewed-by: Andrew Jones <ajones@ventanamicro.com> Signed-off-by: Anup Patel <anup@brainfault.org>
1 parent d2064d4 commit 2776421

3 files changed

Lines changed: 10 additions & 0 deletions

File tree

arch/riscv/include/asm/csr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#ifndef CONFIG_64BIT
5555
#define SATP_PPN _AC(0x003FFFFF, UL)
5656
#define SATP_MODE_32 _AC(0x80000000, UL)
57+
#define SATP_MODE_SHIFT 31
5758
#define SATP_ASID_BITS 9
5859
#define SATP_ASID_SHIFT 22
5960
#define SATP_ASID_MASK _AC(0x1FF, UL)
@@ -62,6 +63,7 @@
6263
#define SATP_MODE_39 _AC(0x8000000000000000, UL)
6364
#define SATP_MODE_48 _AC(0x9000000000000000, UL)
6465
#define SATP_MODE_57 _AC(0xa000000000000000, UL)
66+
#define SATP_MODE_SHIFT 60
6567
#define SATP_ASID_BITS 16
6668
#define SATP_ASID_SHIFT 44
6769
#define SATP_ASID_MASK _AC(0xFFFF, UL)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ struct kvm_riscv_config {
5555
unsigned long marchid;
5656
unsigned long mimpid;
5757
unsigned long zicboz_block_size;
58+
unsigned long satp_mode;
5859
};
5960

6061
/* CORE registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */

arch/riscv/kvm/vcpu_onereg.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ static int kvm_riscv_vcpu_get_reg_config(struct kvm_vcpu *vcpu,
152152
case KVM_REG_RISCV_CONFIG_REG(mimpid):
153153
reg_val = vcpu->arch.mimpid;
154154
break;
155+
case KVM_REG_RISCV_CONFIG_REG(satp_mode):
156+
reg_val = satp_mode >> SATP_MODE_SHIFT;
157+
break;
155158
default:
156159
return -EINVAL;
157160
}
@@ -234,6 +237,10 @@ static int kvm_riscv_vcpu_set_reg_config(struct kvm_vcpu *vcpu,
234237
else
235238
return -EBUSY;
236239
break;
240+
case KVM_REG_RISCV_CONFIG_REG(satp_mode):
241+
if (reg_val != (satp_mode >> SATP_MODE_SHIFT))
242+
return -EINVAL;
243+
break;
237244
default:
238245
return -EINVAL;
239246
}

0 commit comments

Comments
 (0)