RISC-V: Add steal-time support
Subsystems (7)
v3 cover RISC-V: Add steal-time support
One frequently touted benefit of virtualization is the ability to consolidate machines, increasing resource utilization. It may even be desirable to overcommit, at the risk of one or more VCPUs having to wait. Hypervisors which have interfaces for guests to retrieve the amount of time each VCPU had to wait give observers within the guests ways to account for less progress than would otherwise be expected. The SBI STA extension[1] provides a standard interface for guest VCPUs to retrieve the amount of time "stolen".
This series has three parts:
1) Patches 01-03 - Add paravirt support to RISC-V and implement steal-time accounting support using the SBI STA extension.
2) Patches 04-09 - Implement SBI STA in KVM so KVM guests, such as Linux guests which enable the paravirt steal-time support, can be enlightened about stolen time.
3) Patches 10-13 - Add RISC-V support to the KVM selftests steal_time test and the SBI extension regs in the get-reg-list test.
These patches are based on kvm-riscv/riscv_kvm_queue
[1] https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/src/ext-steal-time.adoc
This patches are also available here
https://github.com/jones-drew/linux/commits/kvm/steal-time-v3
Changes since v2:
- Always check sequence in case the SBI impl isn't writing steal atomically [Anup]
- Create a reset function for sta and also reset last_steal [Anup]
- Picked up Anup's r-b's
Changes since v1:
- sbi_sta_steal_time_set_shmem() now maps SBI errors to linux errors [Conor]
- Squashed v1 patch 4 into v1 patch 3 [Conor]
- Moved kvm_riscv_vcpu_set/get_reg_sbi() to arch/riscv/kvm/vcpu_sbi.c which seems like a more appropriate file for them
- Rebased on kvm-riscv/riscv_kvm_queue which has all the dependencies
Changes since RFC:
- Rebased on v6.7-rc3 (plus the patches mentioned above)
- Added defines for the -1's [Conor]
- Added a comment to the steal read sequence [Conor]
- Only do the steal read sequence for 32-bit and added READ_ONCE's
- Check for SBI v2.0 implementation
- Rename set_steal_time_shmem to steal_time_set_shmem
- Add get-reg-list support and test with get-reg-list kselftest
- A few other minor improvements
- Picked up one r-b from Conor
Thanks,
drew
Andrew Jones (13):
RISC-V: paravirt: Add skeleton for pv-time support
RISC-V: Add SBI STA extension definitions
RISC-V: paravirt: Implement steal-time support
RISC-V: KVM: Add SBI STA extension skeleton
RISC-V: KVM: Add steal-update vcpu request
RISC-V: KVM: Add SBI STA info to vcpu_arch
RISC-V: KVM: Add support for SBI extension registers
RISC-V: KVM: Add support for SBI STA registers
RISC-V: KVM: Implement SBI STA extension
RISC-V: KVM: selftests: Move sbi_ecall to processor.c
RISC-V: KVM: selftests: Add guest_sbi_probe_extension
RISC-V: KVM: selftests: Add steal_time test support
RISC-V: KVM: selftests: Add get-reg-list test for STA registers
.../admin-guide/kernel-parameters.txt | 6 +- arch/riscv/Kconfig | 19 ++ arch/riscv/include/asm/kvm_host.h | 10 + arch/riscv/include/asm/kvm_vcpu_sbi.h | 10 + arch/riscv/include/asm/paravirt.h | 28 +++ arch/riscv/include/asm/paravirt_api_clock.h | 1 + arch/riscv/include/asm/sbi.h | 17 ++ arch/riscv/include/uapi/asm/kvm.h | 13 ++ arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/paravirt.c | 134 +++++++++++ arch/riscv/kernel/time.c | 3 + arch/riscv/kvm/Kconfig | 1 + arch/riscv/kvm/Makefile | 1 + arch/riscv/kvm/vcpu.c | 7 + arch/riscv/kvm/vcpu_onereg.c | 50 ++++- arch/riscv/kvm/vcpu_sbi.c | 67 ++++++ arch/riscv/kvm/vcpu_sbi_sta.c | 208 ++++++++++++++++++ tools/testing/selftests/kvm/Makefile | 5 +- .../selftests/kvm/include/riscv/processor.h | 22 ++ .../selftests/kvm/lib/riscv/processor.c | 45 ++++ tools/testing/selftests/kvm/lib/riscv/ucall.c | 26 --- .../selftests/kvm/riscv/get-reg-list.c | 43 ++++ tools/testing/selftests/kvm/steal_time.c | 99 +++++++++ 23 files changed, 781 insertions(+), 35 deletions(-) create mode 100644 arch/riscv/include/asm/paravirt.h create mode 100644 arch/riscv/include/asm/paravirt_api_clock.h create mode 100644 arch/riscv/kernel/paravirt.c create mode 100644 arch/riscv/kvm/vcpu_sbi_sta.c
Signature · 1 line
2.43.0
PATCH v3 1/13 RISC-V: paravirt: Add skeleton for pv-time support
Add the files and functions needed to support paravirt time on RISC-V. Also include the common code needed for the first application of pv-time, which is steal-time. In the next patches we'll complete the functions to fully enable steal-time support.
--- .../admin-guide/kernel-parameters.txt | 6 +- arch/riscv/include/asm/paravirt.h | 28 +++++++ arch/riscv/include/asm/paravirt_api_clock.h | 1 + arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/paravirt.c | 77 +++++++++++++++++++ arch/riscv/kernel/time.c | 3 + 6 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 arch/riscv/include/asm/paravirt.h create mode 100644 arch/riscv/include/asm/paravirt_api_clock.h create mode 100644 arch/riscv/kernel/paravirt.c
Documentation/admin-guide/kernel-parameters.txt +3 -3
@@ -3985,9 +3985,9 @@ vulnerability. System may allow data leaks with this option. - no-steal-acc [X86,PV_OPS,ARM64,PPC/PSERIES] Disable paravirtualized - steal time accounting. steal time is computed, but - won't influence scheduler behaviour + no-steal-acc [X86,PV_OPS,ARM64,PPC/PSERIES,RISCV] Disable + paravirtualized steal time accounting. steal time is + computed, but won't influence scheduler behaviour nosync [HW,M68K] Disables sync negotiation for all devices.
arch/riscv/include/asm/paravirt.h new file +28 -0
@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_RISCV_PARAVIRT_H +#define _ASM_RISCV_PARAVIRT_H + +#ifdef CONFIG_PARAVIRT +#include <linux/static_call_types.h> + +struct static_key ; +extern struct static_key paravirt_steal_enabled; +extern struct static_key paravirt_steal_rq_enabled; + +u64 dummy_steal_clock (int cpu); + +DECLARE_STATIC_CALL (pv_steal_clock, dummy_steal_clock); + +static inline u64 paravirt_steal_clock (int cpu) +{ +return static_call (pv_steal_clock)(cpu); +} + +int __init pv_time_init (void ); + +#else + +#define pv_time_init () do {} while (0) + +#endif /* CONFIG_PARAVIRT */ +#endif /* _ASM_RISCV_PARAVIRT_H */
arch/riscv/include/asm/paravirt_api_clock.h new file +1 -0
@@ -0,0 +1 @@ +#include <asm/paravirt.h>
arch/riscv/kernel/Makefile +1 -0
@@ -85,6 +85,7 @@ obj-$(CONFIG_SMP) += sbi-ipi.o obj-$(CONFIG_SMP) += cpu_ops_sbi.o endif obj-$(CONFIG_HOTPLUG_CPU) += cpu-hotplug.o +obj-$(CONFIG_PARAVIRT) += paravirt.o obj-$(CONFIG_KGDB) += kgdb.o obj-$(CONFIG_KEXEC_CORE) += kexec_relocate.o crash_save_regs.o machine_kexec.o obj-$(CONFIG_KEXEC_FILE) += elf_kexec.o machine_kexec_file.o
arch/riscv/kernel/paravirt.c new file +77 -0
77 lines not on this page · raw message
arch/riscv/kernel/time.c +3 -0
@@ -12,6 +12,7 @@#include <asm/sbi.h> #include <asm/processor.h> #include <asm/timex.h> +#include <asm/paravirt.h> unsigned long riscv_timebase__ro_after_init ;EXPORT_SYMBOL_GPL (riscv_timebase);
@@ -45,4 +46,6 @@ void __init time_init(void)timer_probe ();tick_setup_hrtimer_broadcast (); + +pv_time_init (); }
Signature · 1 line
2.43.0
PATCH v3 2/13 RISC-V: Add SBI STA extension definitions
In reply to Andrew JonesThe SBI STA extension enables steal-time accounting. Add the definitions it specifies.
--- arch/riscv/include/asm/sbi.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
arch/riscv/include/asm/sbi.h +17 -0
@@ -31,6 +31,7 @@ enum sbi_ext_id {SBI_EXT_SRST =0x53525354 ,SBI_EXT_PMU =0x504D55 ,SBI_EXT_DBCN =0x4442434E , +SBI_EXT_STA =0x535441 ,/* Experimentals extensions must lie within this range */ SBI_EXT_EXPERIMENTAL_START = 0x08000000,
@@ -243,6 +244,22 @@ enum sbi_ext_dbcn_fid { SBI_EXT_DBCN_CONSOLE_WRITE_BYTE = 2, }; +/* SBI STA (steal-time accounting) extension */ +enum sbi_ext_sta_fid { +SBI_EXT_STA_STEAL_TIME_SET_SHMEM =0 , +}; + +struct sbi_sta_struct { +__le32 sequence; +__le32 flags; +__le64 steal; +u8 preempted; +u8 pad[47 ]; +} __packed; + +#define SBI_STA_SHMEM_DISABLE -1 + +/* SBI spec version fields */ #define SBI_SPEC_VERSION_DEFAULT 0x1#define SBI_SPEC_VERSION_MAJOR_SHIFT 24#define SBI_SPEC_VERSION_MAJOR_MASK 0x7f
Signature · 1 line
2.43.0
PATCH v3 3/13 RISC-V: paravirt: Implement steal-time support
In reply to Andrew JonesWhen the SBI STA extension exists we can use it to implement paravirt steal-time support. Fill in the empty pv-time functions with an SBI STA implementation and add the Kconfig knobs allowing it to be enabled.
--- arch/riscv/Kconfig | 19 +++++++++++ arch/riscv/kernel/paravirt.c | 63 ++++++++++++++++++++++++++++++++++-- 2 files changed, 79 insertions(+), 3 deletions(-)
arch/riscv/Kconfig +19 -0
@@ -724,6 +724,25 @@ config COMPAT If you want to execute 32-bit userspace applications, say Y. +config PARAVIRT + bool "Enable paravirtualization code" + depends on RISCV_SBI + help + This changes the kernel so it can modify itself when it is run + under a hypervisor, potentially improving performance significantly + over full virtualization. + +config PARAVIRT_TIME_ACCOUNTING + bool "Paravirtual steal time accounting" + depends on PARAVIRT + help + Select this option to enable fine granularity task steal time + accounting. Time spent executing other tasks in parallel with + the current vCPU is discounted from the vCPU power. To account for + that, there can be a small performance impact. + + If in doubt, say N here. + config RELOCATABLE bool "Build a relocatable kernel" depends on MMU && 64BIT && !XIP_KERNEL
arch/riscv/kernel/paravirt.c +60 -3
96 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v3 4/13 RISC-V: KVM: Add SBI STA extension skeleton
In reply to Andrew JonesAdd the files and functions needed to support the SBI STA (steal-time accounting) extension. In the next patches we'll complete the functions to fully enable SBI STA support.
--- arch/riscv/include/asm/kvm_vcpu_sbi.h | 1 + arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kvm/Makefile | 1 + arch/riscv/kvm/vcpu_sbi.c | 4 +++ arch/riscv/kvm/vcpu_sbi_sta.c | 47 +++++++++++++++++++++++++++ 5 files changed, 54 insertions(+) create mode 100644 arch/riscv/kvm/vcpu_sbi_sta.c
arch/riscv/include/asm/kvm_vcpu_sbi.h +1 -0
@@ -76,6 +76,7 @@ extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_rfence;extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_srst;extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_hsm;extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_dbcn; +extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_sta;extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_experimental;extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_vendor;
arch/riscv/include/uapi/asm/kvm.h +1 -0
@@ -157,6 +157,7 @@ enum KVM_RISCV_SBI_EXT_ID {KVM_RISCV_SBI_EXT_EXPERIMENTAL ,KVM_RISCV_SBI_EXT_VENDOR ,KVM_RISCV_SBI_EXT_DBCN , +KVM_RISCV_SBI_EXT_STA , KVM_RISCV_SBI_EXT_MAX, };
arch/riscv/kvm/Makefile +1 -0
@@ -26,6 +26,7 @@ kvm-$(CONFIG_RISCV_SBI_V01) += vcpu_sbi_v01.o kvm-y += vcpu_sbi_base.o kvm-y += vcpu_sbi_replace.o kvm-y += vcpu_sbi_hsm.o +kvm-y += vcpu_sbi_sta.o kvm-y += vcpu_timer.o kvm-$(CONFIG_RISCV_PMU_SBI) += vcpu_pmu.o vcpu_sbi_pmu.o kvm-y += aia.o
arch/riscv/kvm/vcpu_sbi.c +4 -0
@@ -70,6 +70,10 @@ static const struct kvm_riscv_sbi_extension_entry sbi_ext[] = { .ext_idx = KVM_RISCV_SBI_EXT_DBCN, .ext_ptr = &vcpu_sbi_ext_dbcn, }, + { + .ext_idx = KVM_RISCV_SBI_EXT_STA, + .ext_ptr = &vcpu_sbi_ext_sta, + }, { .ext_idx = KVM_RISCV_SBI_EXT_EXPERIMENTAL, .ext_ptr = &vcpu_sbi_ext_experimental,
arch/riscv/kvm/vcpu_sbi_sta.c new file +47 -0
47 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v3 5/13 RISC-V: KVM: Add steal-update vcpu request
In reply to Andrew JonesAdd a new vcpu request to inform a vcpu that it should record its steal-time information. The request is made each time it has been detected that the vcpu task was not assigned a cpu for some time, which is easy to do by making the request from vcpu-load. The record function is just a stub for now and will be filled in with the rest of the steal-time support functions in following patches.
--- arch/riscv/include/asm/kvm_host.h | 3 +++ arch/riscv/kvm/vcpu.c | 5 +++++ arch/riscv/kvm/vcpu_sbi_sta.c | 4 ++++ 3 files changed, 12 insertions(+)
arch/riscv/include/asm/kvm_host.h +3 -0
@@ -41,6 +41,7 @@ KVM_ARCH_REQ_FLAGS(4, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)#define KVM_REQ_HFENCE \ KVM_ARCH_REQ_FLAGS(5, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) +#define KVM_REQ_STEAL_UPDATE KVM_ARCH_REQ(6)enum kvm_riscv_hfence_type {KVM_RISCV_HFENCE_UNKNOWN =0 ,
@@ -372,4 +373,6 @@ bool kvm_riscv_vcpu_has_interrupts(struct kvm_vcpu *vcpu, u64 mask);void kvm_riscv_vcpu_power_off (struct kvm_vcpu *vcpu);void kvm_riscv_vcpu_power_on (struct kvm_vcpu *vcpu); +void kvm_riscv_vcpu_record_steal_time (struct kvm_vcpu *vcpu); +#endif /* __RISCV_KVM_HOST_H__ */
arch/riscv/kvm/vcpu.c +5 -0
@@ -541,6 +541,8 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)kvm_riscv_vcpu_aia_load (vcpu, cpu); +kvm_make_request (KVM_REQ_STEAL_UPDATE , vcpu); + vcpu->cpu = cpu; }
@@ -614,6 +616,9 @@ static void kvm_riscv_check_vcpu_requests(struct kvm_vcpu *vcpu)if (kvm_check_request (KVM_REQ_HFENCE , vcpu))kvm_riscv_hfence_process (vcpu); + +if (kvm_check_request (KVM_REQ_STEAL_UPDATE , vcpu)) +kvm_riscv_vcpu_record_steal_time (vcpu); } }
arch/riscv/kvm/vcpu_sbi_sta.c +4 -0
@@ -8,6 +8,10 @@#include <asm/kvm_vcpu_sbi.h> #include <asm/sbi.h> +void kvm_riscv_vcpu_record_steal_time (struct kvm_vcpu *vcpu) +{ +} +static int kvm_sbi_sta_steal_time_set_shmem (struct kvm_vcpu *vcpu) {return SBI_ERR_FAILURE ;
Signature · 1 line
2.43.0
PATCH v3 6/13 RISC-V: KVM: Add SBI STA info to vcpu_arch
In reply to Andrew JonesKVM's implementation of SBI STA needs to track the address of each VCPU's steal-time shared memory region as well as the amount of stolen time. Add a structure to vcpu_arch to contain this state and make sure that the address is always set to INVALID_GPA on vcpu reset. And, of course, ensure KVM won't try to update steal- time when the shared memory address is invalid.
--- arch/riscv/include/asm/kvm_host.h | 7 +++++++ arch/riscv/kvm/vcpu.c | 2 ++ arch/riscv/kvm/vcpu_sbi_sta.c | 10 ++++++++++ 3 files changed, 19 insertions(+)
arch/riscv/include/asm/kvm_host.h +7 -0
@@ -263,6 +263,12 @@ struct kvm_vcpu_arch {/* 'static' configurations which are set only once */ struct kvm_vcpu_config cfg; + +/* SBI steal-time accounting */ +struct { +gpa_t shmem; +u64 last_steal; + } sta; };static inline void kvm_arch_sync_events (struct kvm *kvm) {}
@@ -373,6 +379,7 @@ bool kvm_riscv_vcpu_has_interrupts(struct kvm_vcpu *vcpu, u64 mask);void kvm_riscv_vcpu_power_off (struct kvm_vcpu *vcpu);void kvm_riscv_vcpu_power_on (struct kvm_vcpu *vcpu); +void kvm_riscv_vcpu_sbi_sta_reset (struct kvm_vcpu *vcpu);void kvm_riscv_vcpu_record_steal_time (struct kvm_vcpu *vcpu);#endif /* __RISCV_KVM_HOST_H__ */
arch/riscv/kvm/vcpu.c +2 -0
@@ -83,6 +83,8 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu) vcpu->arch.hfence_tail =0 ;memset (vcpu->arch.hfence_queue,0 ,sizeof (vcpu->arch.hfence_queue)); +kvm_riscv_vcpu_sbi_sta_reset (vcpu); +/* Reset the guest CSRs for hotplug usecase */ if (loaded)kvm_arch_vcpu_load (vcpu,smp_processor_id ());
arch/riscv/kvm/vcpu_sbi_sta.c +10 -0
@@ -8,8 +8,18 @@#include <asm/kvm_vcpu_sbi.h> #include <asm/sbi.h> +void kvm_riscv_vcpu_sbi_sta_reset (struct kvm_vcpu *vcpu) +{ + vcpu->arch.sta.shmem =INVALID_GPA ; + vcpu->arch.sta.last_steal =0 ; +} +void kvm_riscv_vcpu_record_steal_time (struct kvm_vcpu *vcpu) { +gpa_t shmem = vcpu->arch.sta.shmem; + +if (shmem ==INVALID_GPA ) +return ; }static int kvm_sbi_sta_steal_time_set_shmem (struct kvm_vcpu *vcpu)
Signature · 1 line
2.43.0
PATCH v3 7/13 RISC-V: KVM: Add support for SBI extension registers
In reply to Andrew JonesSome SBI extensions have state that needs to be saved / restored when migrating the VM. Provide a get/set-one-reg register type for SBI extension registers. Each SBI extension that uses this type will have its own subtype. There are currently no subtypes defined. The next patch introduces the first one.
--- arch/riscv/include/asm/kvm_vcpu_sbi.h | 4 ++ arch/riscv/include/uapi/asm/kvm.h | 3 ++ arch/riscv/kvm/vcpu_onereg.c | 42 +++++++++++++++++-- arch/riscv/kvm/vcpu_sbi.c | 58 +++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 4 deletions(-)
arch/riscv/include/asm/kvm_vcpu_sbi.h +4 -0
@@ -60,6 +60,10 @@ int kvm_riscv_vcpu_set_reg_sbi_ext(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);int kvm_riscv_vcpu_get_reg_sbi_ext (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg); +int kvm_riscv_vcpu_set_reg_sbi (struct kvm_vcpu *vcpu, +const struct kvm_one_reg *reg); +int kvm_riscv_vcpu_get_reg_sbi (struct kvm_vcpu *vcpu, +const struct kvm_one_reg *reg);const struct kvm_vcpu_sbi_extension *kvm_vcpu_sbi_find_ext (struct kvm_vcpu *vcpu,unsigned long extid);bool riscv_vcpu_supports_sbi_ext (struct kvm_vcpu *vcpu,int idx);
arch/riscv/include/uapi/asm/kvm.h +3 -0
@@ -242,6 +242,9 @@ enum KVM_RISCV_SBI_EXT_ID {#define KVM_REG_RISCV_VECTOR_REG (n) \ ((n) + sizeof(struct __riscv_v_ext_state) / sizeof(unsigned long)) +/* Registers for specific SBI extensions are mapped as type 10 */ +#define KVM_REG_RISCV_SBI (0x0a << KVM_REG_RISCV_TYPE_SHIFT) +/* Device Control API: RISC-V AIA */ #define KVM_DEV_RISCV_APLIC_ALIGN 0x1000#define KVM_DEV_RISCV_APLIC_SIZE 0x4000
arch/riscv/kvm/vcpu_onereg.c +38 -4
80 lines not on this page · raw message
arch/riscv/kvm/vcpu_sbi.c +58 -0
64 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v3 8/13 RISC-V: KVM: Add support for SBI STA registers
In reply to Andrew JonesKVM userspace needs to be able to save and restore the steal-time shared memory address. Provide the address through the get/set-one-reg interface with two ulong-sized SBI STA extension registers (lo and hi). 64-bit KVM userspace must not set the hi register to anything other than zero and is allowed to completely neglect saving/restoring it.
--- arch/riscv/include/asm/kvm_vcpu_sbi.h | 5 +++ arch/riscv/include/uapi/asm/kvm.h | 9 +++++ arch/riscv/kvm/vcpu_onereg.c | 36 +++++++++++------- arch/riscv/kvm/vcpu_sbi.c | 5 +++ arch/riscv/kvm/vcpu_sbi_sta.c | 55 +++++++++++++++++++++++++++ 5 files changed, 96 insertions(+), 14 deletions(-)
arch/riscv/include/asm/kvm_vcpu_sbi.h +5 -0
@@ -70,6 +70,11 @@ bool riscv_vcpu_supports_sbi_ext(struct kvm_vcpu *vcpu, int idx);int kvm_riscv_vcpu_sbi_ecall (struct kvm_vcpu *vcpu,struct kvm_run *run);void kvm_riscv_vcpu_sbi_init (struct kvm_vcpu *vcpu); +int kvm_riscv_vcpu_get_reg_sbi_sta (struct kvm_vcpu *vcpu,unsigned long reg_num, +unsigned long *reg_val); +int kvm_riscv_vcpu_set_reg_sbi_sta (struct kvm_vcpu *vcpu,unsigned long reg_num, +unsigned long reg_val); +#ifdef CONFIG_RISCV_SBI_V01 extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_v01;#endif
arch/riscv/include/uapi/asm/kvm.h +9 -0
@@ -161,6 +161,12 @@ enum KVM_RISCV_SBI_EXT_ID { KVM_RISCV_SBI_EXT_MAX, }; +/* SBI STA extension registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */ +struct kvm_riscv_sbi_sta { +unsigned long shmem_lo; +unsigned long shmem_hi; +}; +/* Possible states for kvm_riscv_timer */ #define KVM_RISCV_TIMER_STATE_OFF 0#define KVM_RISCV_TIMER_STATE_ON 1
@@ -244,6 +250,9 @@ enum KVM_RISCV_SBI_EXT_ID {/* Registers for specific SBI extensions are mapped as type 10 */ #define KVM_REG_RISCV_SBI (0x0a << KVM_REG_RISCV_TYPE_SHIFT) +#define KVM_REG_RISCV_SBI_STA (0x0 << KVM_REG_RISCV_SUBTYPE_SHIFT) +#define KVM_REG_RISCV_SBI_STA_REG (name) \ + (offsetof(struct kvm_riscv_sbi_sta, name) / sizeof(unsigned long))/* Device Control API: RISC-V AIA */ #define KVM_DEV_RISCV_APLIC_ALIGN 0x1000
arch/riscv/kvm/vcpu_onereg.c +22 -14
@@ -961,27 +961,35 @@ static unsigned long num_sbi_ext_regs(struct kvm_vcpu *vcpu)return copy_sbi_ext_reg_indices (vcpu,NULL ); } -static inline unsigned long num_sbi_regs (struct kvm_vcpu *vcpu) -{ -return 0 ; -} -static int copy_sbi_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices) { -int n =num_sbi_regs (vcpu); +struct kvm_vcpu_sbi_context *scontext = &vcpu->arch.sbi_context; +int total =0 ; -for (int i =0 ; i < n; i++) { -u64 reg =KVM_REG_RISCV |KVM_REG_SIZE_U64 | -KVM_REG_RISCV_SBI | i; +if (scontext->ext_status[KVM_RISCV_SBI_EXT_STA ] ==KVM_RISCV_SBI_EXT_STATUS_ENABLED ) { +u64 size =IS_ENABLED (CONFIG_32BIT ) ?KVM_REG_SIZE_U32 :KVM_REG_SIZE_U64 ; +int n =sizeof (struct kvm_riscv_sbi_sta ) /sizeof (unsigned long ); -if (uindices) { -if (put_user (reg, uindices)) -return -EFAULT ; - uindices++; +for (int i =0 ; i < n; i++) { +u64 reg =KVM_REG_RISCV | size | +KVM_REG_RISCV_SBI |KVM_REG_RISCV_SBI_STA | i; + +if (uindices) { +if (put_user (reg, uindices)) +return -EFAULT ; + uindices++; + } } + + total += n; } -return n; +return total; +} + +static inline unsigned long num_sbi_regs (struct kvm_vcpu *vcpu) +{ +return copy_sbi_reg_indices (vcpu,NULL ); }static inline unsigned long num_vector_regs (const struct kvm_vcpu *vcpu)
arch/riscv/kvm/vcpu_sbi.c +5 -0
@@ -345,6 +345,8 @@ int kvm_riscv_vcpu_set_reg_sbi(struct kvm_vcpu *vcpu, reg_num &= ~KVM_REG_RISCV_SUBTYPE_MASK ;switch (reg_subtype) { +case KVM_REG_RISCV_SBI_STA : +return kvm_riscv_vcpu_set_reg_sbi_sta (vcpu, reg_num, reg_val);default :return -EINVAL ; }
@@ -370,6 +372,9 @@ int kvm_riscv_vcpu_get_reg_sbi(struct kvm_vcpu *vcpu, reg_num &= ~KVM_REG_RISCV_SUBTYPE_MASK ;switch (reg_subtype) { +case KVM_REG_RISCV_SBI_STA : + ret =kvm_riscv_vcpu_get_reg_sbi_sta (vcpu, reg_num, ®_val); +break ;default :return -EINVAL ; }
arch/riscv/kvm/vcpu_sbi_sta.c +55 -0
64 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v3 9/13 RISC-V: KVM: Implement SBI STA extension
In reply to Andrew JonesAdd a select SCHED_INFO to the KVM config in order to get run_delay info. Then implement SBI STA's set-steal-time-shmem function and kvm_riscv_vcpu_record_steal_time() to provide the steal-time info to guests.
--- arch/riscv/kvm/Kconfig | 1 + arch/riscv/kvm/vcpu_sbi_sta.c | 96 ++++++++++++++++++++++++++++++++++- 2 files changed, 95 insertions(+), 2 deletions(-)
arch/riscv/kvm/Kconfig +1 -0
@@ -32,6 +32,7 @@ config KVM select KVM_XFER_TO_GUEST_WORK select MMU_NOTIFIER select PREEMPT_NOTIFIERS + select SCHED_INFO help Support hosting virtualized guest machines.
arch/riscv/kvm/vcpu_sbi_sta.c +94 -2
124 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v3 10/13 RISC-V: KVM: selftests: Move sbi_ecall to processor.c
In reply to Andrew Jonessbi_ecall() isn't ucall specific and its prototype is already in processor.h. Move its implementation to processor.c.
--- .../selftests/kvm/lib/riscv/processor.c | 26 +++++++++++++++++++ tools/testing/selftests/kvm/lib/riscv/ucall.c | 26 ------------------- 2 files changed, 26 insertions(+), 26 deletions(-)
tools/testing/selftests/kvm/lib/riscv/processor.c +26 -0
@@ -367,3 +367,29 @@ void vcpu_args_set(struct kvm_vcpu *vcpu, unsigned int num, ...)void assert_on_unhandled_exception (struct kvm_vcpu *vcpu) { } + +struct sbiret sbi_ecall (int ext,int fid,unsigned long arg0, +unsigned long arg1,unsigned long arg2, +unsigned long arg3,unsigned long arg4, +unsigned long arg5) +{ + register uintptr_t a0 asm ("a0") = (uintptr_t)(arg0); + register uintptr_t a1 asm ("a1") = (uintptr_t)(arg1); + register uintptr_t a2 asm ("a2") = (uintptr_t)(arg2); + register uintptr_t a3 asm ("a3") = (uintptr_t)(arg3); + register uintptr_t a4 asm ("a4") = (uintptr_t)(arg4); + register uintptr_t a5 asm ("a5") = (uintptr_t)(arg5); + register uintptr_t a6 asm ("a6") = (uintptr_t)(fid); + register uintptr_t a7 asm ("a7") = (uintptr_t)(ext); +struct sbiret ret; + + asmvolatile ( +"ecall" + :"+r" (a0),"+r" (a1) + :"r" (a2),"r" (a3),"r" (a4),"r" (a5),"r" (a6),"r" (a7) + :"memory" ); + ret.error = a0; + ret.value = a1; + +return ret; +}
tools/testing/selftests/kvm/lib/riscv/ucall.c +0 -26
@@ -10,32 +10,6 @@#include "kvm_util.h" #include "processor.h" -struct sbiret sbi_ecall (int ext,int fid,unsigned long arg0, -unsigned long arg1,unsigned long arg2, -unsigned long arg3,unsigned long arg4, -unsigned long arg5) -{ - register uintptr_t a0 asm ("a0") = (uintptr_t)(arg0); - register uintptr_t a1 asm ("a1") = (uintptr_t)(arg1); - register uintptr_t a2 asm ("a2") = (uintptr_t)(arg2); - register uintptr_t a3 asm ("a3") = (uintptr_t)(arg3); - register uintptr_t a4 asm ("a4") = (uintptr_t)(arg4); - register uintptr_t a5 asm ("a5") = (uintptr_t)(arg5); - register uintptr_t a6 asm ("a6") = (uintptr_t)(fid); - register uintptr_t a7 asm ("a7") = (uintptr_t)(ext); -struct sbiret ret; - - asmvolatile ( -"ecall" - :"+r" (a0),"+r" (a1) - :"r" (a2),"r" (a3),"r" (a4),"r" (a5),"r" (a6),"r" (a7) - :"memory" ); - ret.error = a0; - ret.value = a1; - -return ret; -} -void *ucall_arch_get_ucall (struct kvm_vcpu *vcpu) {struct kvm_run *run = vcpu->run;
Signature · 1 line
2.43.0
PATCH v3 11/13 RISC-V: KVM: selftests: Add guest_sbi_probe_extension
In reply to Andrew JonesAdd guest_sbi_probe_extension(), allowing guest code to probe for SBI extensions. As guest_sbi_probe_extension() needs SBI_ERR_NOT_SUPPORTED, take the opportunity to bring in all SBI error codes. We don't bring in all current extension IDs or base extension function IDs though, even though we need one of each, because we'd prefer to bring those in as necessary.
--- .../selftests/kvm/include/riscv/processor.h | 21 +++++++++++++++++++ .../selftests/kvm/lib/riscv/processor.c | 19 +++++++++++++++++ 2 files changed, 40 insertions(+)
tools/testing/selftests/kvm/include/riscv/processor.h +21 -0
@@ -108,6 +108,17 @@ static inline uint64_t __kvm_reg_id(uint64_t type, uint64_t subtype,#define SATP_ASID_SHIFT 44#define SATP_ASID_MASK _AC(0xFFFF, UL) +/* SBI return error codes */ +#define SBI_SUCCESS 0 +#define SBI_ERR_FAILURE -1 +#define SBI_ERR_NOT_SUPPORTED -2 +#define SBI_ERR_INVALID_PARAM -3 +#define SBI_ERR_DENIED -4 +#define SBI_ERR_INVALID_ADDRESS -5 +#define SBI_ERR_ALREADY_AVAILABLE -6 +#define SBI_ERR_ALREADY_STARTED -7 +#define SBI_ERR_ALREADY_STOPPED -8 +#define SBI_EXT_EXPERIMENTAL_START 0x08000000#define SBI_EXT_EXPERIMENTAL_END 0x08FFFFFF
@@ -115,6 +126,14 @@ static inline uint64_t __kvm_reg_id(uint64_t type, uint64_t subtype,#define KVM_RISCV_SELFTESTS_SBI_UCALL 0#define KVM_RISCV_SELFTESTS_SBI_UNEXP 1 +enum sbi_ext_id { +SBI_EXT_BASE =0x10 , +}; + +enum sbi_ext_base_fid { +SBI_EXT_BASE_PROBE_EXT =3 , +}; +struct sbiret {long error;long value;
@@ -125,4 +144,6 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0, unsigned long arg3, unsigned long arg4, unsigned long arg5); +bool guest_sbi_probe_extension (int extid,long *out_val); +#endif /* SELFTEST_KVM_PROCESSOR_H */
tools/testing/selftests/kvm/lib/riscv/processor.c +19 -0
@@ -393,3 +393,22 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,return ret; } + +bool guest_sbi_probe_extension (int extid,long *out_val) +{ +struct sbiret ret; + + ret =sbi_ecall (SBI_EXT_BASE ,SBI_EXT_BASE_PROBE_EXT , extid, +0 ,0 ,0 ,0 ,0 ); + +__GUEST_ASSERT (!ret.error || ret.error ==SBI_ERR_NOT_SUPPORTED , +"ret.error=%ld, ret.value=%ld\n" , ret.error, ret.value); + +if (ret.error ==SBI_ERR_NOT_SUPPORTED ) +return false; + +if (out_val) + *out_val = ret.value; + +return true; +}
Signature · 1 line
2.43.0
PATCH v3 12/13 RISC-V: KVM: selftests: Add steal_time test support
In reply to Andrew JonesWith the introduction of steal-time accounting support for RISC-V KVM we can add RISC-V support to the steal_time test.
--- tools/testing/selftests/kvm/Makefile | 5 +- .../selftests/kvm/include/riscv/processor.h | 1 + tools/testing/selftests/kvm/steal_time.c | 99 +++++++++++++++++++ 3 files changed, 103 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/Makefile +3 -2
@@ -194,12 +194,13 @@ TEST_GEN_PROGS_s390x += kvm_binary_stats_test TEST_GEN_PROGS_riscv += demand_paging_test TEST_GEN_PROGS_riscv += dirty_log_test -TEST_GEN_PROGS_riscv += guest_print_test TEST_GEN_PROGS_riscv += get-reg-list +TEST_GEN_PROGS_riscv += guest_print_test +TEST_GEN_PROGS_riscv += kvm_binary_stats_test TEST_GEN_PROGS_riscv += kvm_create_max_vcpus TEST_GEN_PROGS_riscv += kvm_page_table_test TEST_GEN_PROGS_riscv += set_memory_region_test -TEST_GEN_PROGS_riscv += kvm_binary_stats_test +TEST_GEN_PROGS_riscv += steal_time SPLIT_TESTS += get-reg-list
tools/testing/selftests/kvm/include/riscv/processor.h +1 -0
@@ -128,6 +128,7 @@ static inline uint64_t __kvm_reg_id(uint64_t type, uint64_t subtype,enum sbi_ext_id {SBI_EXT_BASE =0x10 , +SBI_EXT_STA =0x535441 , };enum sbi_ext_base_fid {
tools/testing/selftests/kvm/steal_time.c +99 -0
112 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v3 13/13 RISC-V: KVM: selftests: Add get-reg-list test for STA registers
In reply to Andrew JonesAdd SBI STA and its two registers to the get-reg-list test.
--- .../selftests/kvm/riscv/get-reg-list.c | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+)
tools/testing/selftests/kvm/riscv/get-reg-list.c +43 -0
@@ -71,6 +71,7 @@ bool filter_reg(__u64 reg)case KVM_REG_RISCV_SBI_EXT |KVM_REG_RISCV_SBI_SINGLE |KVM_RISCV_SBI_EXT_HSM :case KVM_REG_RISCV_SBI_EXT |KVM_REG_RISCV_SBI_SINGLE |KVM_RISCV_SBI_EXT_PMU :case KVM_REG_RISCV_SBI_EXT |KVM_REG_RISCV_SBI_SINGLE |KVM_RISCV_SBI_EXT_DBCN : +case KVM_REG_RISCV_SBI_EXT |KVM_REG_RISCV_SBI_SINGLE |KVM_RISCV_SBI_EXT_STA :case KVM_REG_RISCV_SBI_EXT |KVM_REG_RISCV_SBI_SINGLE |KVM_RISCV_SBI_EXT_EXPERIMENTAL :case KVM_REG_RISCV_SBI_EXT |KVM_REG_RISCV_SBI_SINGLE |KVM_RISCV_SBI_EXT_VENDOR :return true;
@@ -461,6 +462,7 @@ static const char *sbi_ext_single_id_to_str(__u64 reg_off)KVM_SBI_EXT_ARR (KVM_RISCV_SBI_EXT_SRST ),KVM_SBI_EXT_ARR (KVM_RISCV_SBI_EXT_HSM ),KVM_SBI_EXT_ARR (KVM_RISCV_SBI_EXT_PMU ), +KVM_SBI_EXT_ARR (KVM_RISCV_SBI_EXT_STA ),KVM_SBI_EXT_ARR (KVM_RISCV_SBI_EXT_EXPERIMENTAL ),KVM_SBI_EXT_ARR (KVM_RISCV_SBI_EXT_VENDOR ), KVM_SBI_EXT_ARR(KVM_RISCV_SBI_EXT_DBCN),
@@ -509,6 +511,32 @@ static const char *sbi_ext_id_to_str(const char *prefix, __u64 id)return strdup_printf ("%lld | %lld /* UNKNOWN */" , reg_subtype, reg_off); } +static const char *sbi_sta_id_to_str (__u64 reg_off) +{ +switch (reg_off) { +case 0 :return "KVM_REG_RISCV_SBI_STA | KVM_REG_RISCV_SBI_STA_REG(shmem_lo)" ; +case 1 :return "KVM_REG_RISCV_SBI_STA | KVM_REG_RISCV_SBI_STA_REG(shmem_hi)" ; + } +return strdup_printf ("KVM_REG_RISCV_SBI_STA | %lld /* UNKNOWN */" , reg_off); +} + +static const char *sbi_id_to_str (const char *prefix,__u64 id) +{ +__u64 reg_off = id & ~(REG_MASK |KVM_REG_RISCV_SBI ); +__u64 reg_subtype = reg_off &KVM_REG_RISCV_SUBTYPE_MASK ; + +assert ((id &KVM_REG_RISCV_TYPE_MASK ) ==KVM_REG_RISCV_SBI ); + + reg_off &= ~KVM_REG_RISCV_SUBTYPE_MASK ; + +switch (reg_subtype) { +case KVM_REG_RISCV_SBI_STA : +return sbi_sta_id_to_str (reg_off); + } + +return strdup_printf ("%lld | %lld /* UNKNOWN */" , reg_subtype, reg_off); +} +void print_reg (const char *prefix,__u64 id) {const char *reg_size =NULL ;
@@ -565,6 +593,10 @@ void print_reg(const char *prefix, __u64 id)printf ("\tKVM_REG_RISCV | %s | KVM_REG_RISCV_SBI_EXT | %s,\n" , reg_size,sbi_ext_id_to_str (prefix, id));break ; +case KVM_REG_RISCV_SBI : +printf ("\tKVM_REG_RISCV | %s | KVM_REG_RISCV_SBI | %s,\n" , + reg_size,sbi_id_to_str (prefix, id)); +break ;default :printf ("\tKVM_REG_RISCV | %s | 0x%llx /* UNKNOWN */,\n" , reg_size, id & ~REG_MASK );
@@ -651,6 +683,12 @@ static __u64 sbi_base_regs[] = { KVM_REG_RISCV | KVM_REG_SIZE_ULONG | KVM_REG_RISCV_SBI_EXT | KVM_REG_RISCV_SBI_SINGLE | KVM_RISCV_SBI_EXT_VENDOR, }; +static __u64 sbi_sta_regs[] = { +KVM_REG_RISCV |KVM_REG_SIZE_ULONG |KVM_REG_RISCV_SBI_EXT |KVM_REG_RISCV_SBI_SINGLE |KVM_RISCV_SBI_EXT_STA , +KVM_REG_RISCV |KVM_REG_SIZE_ULONG |KVM_REG_RISCV_SBI |KVM_REG_RISCV_SBI_STA |KVM_REG_RISCV_SBI_STA_REG (shmem_lo), +KVM_REG_RISCV |KVM_REG_SIZE_ULONG |KVM_REG_RISCV_SBI |KVM_REG_RISCV_SBI_STA |KVM_REG_RISCV_SBI_STA_REG (shmem_hi), +}; +static __u64 zicbom_regs[] = {KVM_REG_RISCV |KVM_REG_SIZE_ULONG |KVM_REG_RISCV_CONFIG |KVM_REG_RISCV_CONFIG_REG (zicbom_block_size),KVM_REG_RISCV |KVM_REG_SIZE_ULONG |KVM_REG_RISCV_ISA_EXT |KVM_REG_RISCV_ISA_SINGLE |KVM_RISCV_ISA_EXT_ZICBOM ,
@@ -757,6 +795,9 @@ static __u64 fp_d_regs[] = {#define SUBLIST_SBI_BASE \ {"sbi-base", .feature_type = VCPU_FEATURE_SBI_EXT, .feature = KVM_RISCV_SBI_EXT_V01, \ .regs = sbi_base_regs, .regs_n = ARRAY_SIZE(sbi_base_regs),} +#define SUBLIST_SBI_STA \ + {"sbi-sta", .feature_type = VCPU_FEATURE_SBI_EXT, .feature = KVM_RISCV_SBI_EXT_STA, \ + .regs = sbi_sta_regs, .regs_n = ARRAY_SIZE(sbi_sta_regs),}#define SUBLIST_ZICBOM \ {"zicbom", .feature = KVM_RISCV_ISA_EXT_ZICBOM, .regs = zicbom_regs, .regs_n = ARRAY_SIZE(zicbom_regs),}#define SUBLIST_ZICBOZ \
@@ -832,6 +873,7 @@ static struct vcpu_reg_list config_sbi_##ext = { \/* Note: The below list is alphabetically sorted. */ KVM_SBI_EXT_SUBLIST_CONFIG (base,BASE ); +KVM_SBI_EXT_SUBLIST_CONFIG (sta,STA );KVM_SBI_EXT_SIMPLE_CONFIG (pmu,PMU );KVM_SBI_EXT_SIMPLE_CONFIG (dbcn,DBCN );
@@ -858,6 +900,7 @@ KVM_ISA_EXT_SIMPLE_CONFIG(zihpm, ZIHPM);struct vcpu_reg_list *vcpu_configs[] = { &config_sbi_base, + &config_sbi_sta, &config_sbi_pmu, &config_sbi_dbcn, &config_aia,
Signature · 1 line
2.43.0
PATCH v3 1/13 RISC-V: paravirt: Add skeleton for pv-time support
In reply to Andrew JonesQuoted 162 lines · On Sun, Dec 17, 2023 at 12:40?PM Andrew Jones <[email protected]> wrote:
Add the files and functions needed to support paravirt time on RISC-V. Also include the common code needed for the first application of pv-time, which is steal-time. In the next patches we'll complete the functions to fully enable steal-time support.
Reviewed-by Anup Patel <[email protected]>Signed-off-by Andrew Jones <[email protected]>--- .../admin-guide/kernel-parameters.txt | 6 +- arch/riscv/include/asm/paravirt.h | 28 +++++++ arch/riscv/include/asm/paravirt_api_clock.h | 1 + arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/paravirt.c | 77 +++++++++++++++++++ arch/riscv/kernel/time.c | 3 + 6 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 arch/riscv/include/asm/paravirt.h create mode 100644 arch/riscv/include/asm/paravirt_api_clock.h create mode 100644 arch/riscv/kernel/paravirt.cDocumentation/admin-guide/kernel-parameters.txt +3
-3@@ -3985,9 +3985,9 @@ vulnerability. System may allow data leaks with this option. - no-steal-acc [X86,PV_OPS,ARM64,PPC/PSERIES] Disable paravirtualized - steal time accounting. steal time is computed, but - won't influence scheduler behaviour + no-steal-acc [X86,PV_OPS,ARM64,PPC/PSERIES,RISCV] Disable + paravirtualized steal time accounting. steal time is + computed, but won't influence scheduler behaviour nosync [HW,M68K] Disables sync negotiation for all devices.arch/riscv/include/asm/paravirt.h new file +28
-0@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_RISCV_PARAVIRT_H +#define _ASM_RISCV_PARAVIRT_H + +#ifdef CONFIG_PARAVIRT +#include <linux/static_call_types.h> + +struct static_key ; +extern struct static_key paravirt_steal_enabled; +extern struct static_key paravirt_steal_rq_enabled; + +u64 dummy_steal_clock (int cpu); + +DECLARE_STATIC_CALL (pv_steal_clock, dummy_steal_clock); + +static inline u64 paravirt_steal_clock (int cpu) +{ +return static_call (pv_steal_clock)(cpu); +} + +int __init pv_time_init (void ); + +#else + +#define pv_time_init () do {} while (0) + +#endif /* CONFIG_PARAVIRT */ +#endif /* _ASM_RISCV_PARAVIRT_H */ arch/riscv/include/asm/paravirt_api_clock.h new file +1
-0@@ -0,0 +1 @@ +#include <asm/paravirt.h> arch/riscv/kernel/Makefile +1
-0@@ -85,6 +85,7 @@ obj-$(CONFIG_SMP) += sbi-ipi.o obj-$(CONFIG_SMP) += cpu_ops_sbi.o endif obj-$(CONFIG_HOTPLUG_CPU) += cpu-hotplug.o +obj-$(CONFIG_PARAVIRT) += paravirt.o obj-$(CONFIG_KGDB) += kgdb.o obj-$(CONFIG_KEXEC_CORE) += kexec_relocate.o crash_save_regs.o machine_kexec.o obj-$(CONFIG_KEXEC_FILE) += elf_kexec.o machine_kexec_file.oarch/riscv/kernel/paravirt.c new file +65
-0@@ -0,0 +1,77 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (c) 2023 Ventana Micro Systems Inc. +*/ + +#define pr_fmt (fmt) "riscv-pv: " fmt + +#include <linux/cpuhotplug.h> +#include <linux/init.h> +#include <linux/jump_label.h> +#include <linux/printk.h> +#include <linux/static_call.h> +#include <linux/types.h> + +struct static_key paravirt_steal_enabled; +struct static_key paravirt_steal_rq_enabled; + +static u64 native_steal_clock (int cpu) +{ +return 0 ; +} + +DEFINE_STATIC_CALL (pv_steal_clock, native_steal_clock); + +static bool steal_acc = true; +static int __init parse_no_stealacc (char *arg) +{ + steal_acc = false; +return 0 ; +} + +early_param ("no-steal-acc" , parse_no_stealacc); + +static bool __init has_pv_steal_clock (void ) +{ +return false; +} + +static int pv_time_cpu_online (unsigned int cpu) +{ +return 0 ; +} + +static int pv_time_cpu_down_prepare (unsigned int cpu) +{ +return 0 ; +} + +static u64 pv_time_steal_clock (int cpu) +{ +return 0 ; +} + +int __init pv_time_init (void ) +{ +int ret; + +if (!has_pv_steal_clock ()) +return 0 ; + + ret =cpuhp_setup_state (CPUHP_AP_ONLINE_DYN , +"riscv/pv_time:online" , + pv_time_cpu_online, + pv_time_cpu_down_prepare); +if (ret <0 )
arch/riscv/kernel/paravirt.c line 75
Quoted 10 lines
arch/riscv/kernel/paravirt.c new file +10
-0+return ret; + +static_call_update (pv_steal_clock, pv_time_steal_clock); + +static_key_slow_inc (¶virt_steal_enabled); +if (steal_acc) +static_key_slow_inc (¶virt_steal_rq_enabled); + +pr_info ("using paravirt steal-time\n" ); +
Nit comment:
If the scheduler doesn't take stolen time into consideration (when If no-steal-acc is specified in the command line),
the above log is a bit misleading ? ARM64 also seems to be doing the same thing.
Maybe the intention of the log is to steal time extension presence rather than actual usage in place ?
Quoted 22 lines
arch/riscv/kernel/paravirt.c new file +2
-0+return 0 ; +}arch/riscv/kernel/time.c +3
-1@@ -12,6 +12,7 @@#include <asm/sbi.h> #include <asm/processor.h> #include <asm/timex.h> +#include <asm/paravirt.h> unsigned long riscv_timebase__ro_after_init ;EXPORT_SYMBOL_GPL (riscv_timebase);@@ -45,4 +46,6 @@ void __init time_init(void)timer_probe ();tick_setup_hrtimer_broadcast (); + +pv_time_init (); } --
Quoted 2 lines
2.43.0
Other than that, lgtm.
Signature · 2 lines
Regards, Atish
PATCH v3 2/13 RISC-V: Add SBI STA extension definitions
In reply to Andrew JonesQuoted 47 lines · On Sun, Dec 17, 2023 at 12:40?PM Andrew Jones <[email protected]> wrote:
The SBI STA extension enables steal-time accounting. Add the definitions it specifies.
Reviewed-by Conor Dooley <[email protected]>Reviewed-by Anup Patel <[email protected]>Signed-off-by Andrew Jones <[email protected]>--- arch/riscv/include/asm/sbi.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)arch/riscv/include/asm/sbi.h +17
-1@@ -31,6 +31,7 @@ enum sbi_ext_id {SBI_EXT_SRST =0x53525354 ,SBI_EXT_PMU =0x504D55 ,SBI_EXT_DBCN =0x4442434E , +SBI_EXT_STA =0x535441 ,/* Experimentals extensions must lie within this range */ SBI_EXT_EXPERIMENTAL_START = 0x08000000,@@ -243,6 +244,22 @@ enum sbi_ext_dbcn_fid { SBI_EXT_DBCN_CONSOLE_WRITE_BYTE = 2, }; +/* SBI STA (steal-time accounting) extension */ +enum sbi_ext_sta_fid { +SBI_EXT_STA_STEAL_TIME_SET_SHMEM =0 , +}; + +struct sbi_sta_struct { +__le32 sequence; +__le32 flags; +__le64 steal; +u8 preempted; +u8 pad[47 ]; +} __packed; + +#define SBI_STA_SHMEM_DISABLE -1 + +/* SBI spec version fields */ #define SBI_SPEC_VERSION_DEFAULT 0x1#define SBI_SPEC_VERSION_MAJOR_SHIFT 24#define SBI_SPEC_VERSION_MAJOR_MASK 0x7f --
Quoted 2 lines
2.43.0
Signature · 2 lines
Regards, Atish
PATCH v3 3/13 RISC-V: paravirt: Implement steal-time support
In reply to Andrew JonesQuoted 145 lines · On Sun, Dec 17, 2023 at 12:40?PM Andrew Jones <[email protected]> wrote:
When the SBI STA extension exists we can use it to implement paravirt steal-time support. Fill in the empty pv-time functions with an SBI STA implementation and add the
Kconfigknobs allowing it to be enabled.Signed-off-by Andrew Jones <[email protected]>--- arch/riscv/Kconfig | 19 +++++++++++ arch/riscv/kernel/paravirt.c | 63 ++++++++++++++++++++++++++++++++++-- 2 files changed, 79 insertions(+), 3 deletions(-)arch/riscv/Kconfig +19
-0@@ -724,6 +724,25 @@ config COMPAT If you want to execute 32-bit userspace applications, say Y. +config PARAVIRT + bool "Enable paravirtualization code" + depends on RISCV_SBI + help + This changes the kernel so it can modify itself when it is run + under a hypervisor, potentially improving performance significantly + over full virtualization. + +config PARAVIRT_TIME_ACCOUNTING + bool "Paravirtual steal time accounting" + depends on PARAVIRT + help + Select this option to enable fine granularity task steal time + accounting. Time spent executing other tasks in parallel with + the current vCPU is discounted from the vCPU power. To account for + that, there can be a small performance impact. + + If in doubt, say N here. + config RELOCATABLE bool "Build a relocatable kernel" depends on MMU && 64BIT && !XIP_KERNELarch/riscv/kernel/paravirt.c +60
-4@@ -6,12 +6,21 @@#define pr_fmt (fmt) "riscv-pv: " fmt#include <linux/cpuhotplug.h> +#include <linux/compiler.h> +#include <linux/errno.h> #include <linux/init.h> #include <linux/jump_label.h> +#include <linux/kconfig.h> +#include <linux/kernel.h> +#include <linux/percpu-defs.h> #include <linux/printk.h> #include <linux/static_call.h> #include <linux/types.h> +#include <asm/barrier.h> +#include <asm/page.h> +#include <asm/sbi.h> +struct static_key paravirt_steal_enabled;struct static_key paravirt_steal_rq_enabled;@@ -31,24 +40,72 @@ static int __init parse_no_stealacc(char *arg)early_param ("no-steal-acc" , parse_no_stealacc); +DEFINE_PER_CPU(struct sbi_sta_struct, steal_time) __aligned(64); +static bool __init has_pv_steal_clock (void ) { +if (sbi_spec_version >=sbi_mk_version (2 ,0 ) && +sbi_probe_extension (SBI_EXT_STA ) >0 ) { +pr_info ("SBI STA extension detected\n" ); +return true; + } +return false; } -static int pv_time_cpu_online (unsigned int cpu) +static int sbi_sta_steal_time_set_shmem (unsigned long lo,unsigned long hi, +unsigned long flags) { +struct sbiret ret; + + ret =sbi_ecall (SBI_EXT_STA ,SBI_EXT_STA_STEAL_TIME_SET_SHMEM , + lo, hi, flags,0 ,0 ,0 ); +if (ret.error) { +if (lo ==SBI_STA_SHMEM_DISABLE && hi ==SBI_STA_SHMEM_DISABLE ) +pr_warn ("Failed to disable steal-time shmem" ); +else +pr_warn ("Failed to set steal-time shmem" ); +return sbi_err_map_linux_errno (ret.error); + } +return 0 ; } +static int pv_time_cpu_online (unsigned int cpu) +{ +struct sbi_sta_struct *st =this_cpu_ptr (&steal_time); +phys_addr_t pa =__pa (st); +unsigned long lo = (unsigned long )pa; +unsigned long hi =IS_ENABLED (CONFIG_32BIT ) ?upper_32_bits ((u64 )pa) :0 ; + +return sbi_sta_steal_time_set_shmem (lo, hi,0 ); +} +static int pv_time_cpu_down_prepare (unsigned int cpu) { -return 0 ; +return sbi_sta_steal_time_set_shmem (SBI_STA_SHMEM_DISABLE , +SBI_STA_SHMEM_DISABLE ,0 ); }static u64 pv_time_steal_clock (int cpu) { -return 0 ; +struct sbi_sta_struct *st =per_cpu_ptr (&steal_time, cpu); +u32 sequence; +u64 steal; + +/* +* Check the sequence field before and after reading the steal +* field. Repeat the read if it is different or odd. +*/ +do { + sequence =READ_ONCE (st->sequence); +virt_rmb (); + steal =READ_ONCE (st->steal); +virt_rmb (); + }while ((le32_to_cpu (sequence) &1 ) || + sequence !=READ_ONCE (st->sequence)); + +return le64_to_cpu (steal); }int __init pv_time_init (void ) --
Quoted 2 lines
2.43.0
Signature · 2 lines
Regards, Atish
PATCH v3 4/13 RISC-V: KVM: Add SBI STA extension skeleton
In reply to Andrew JonesQuoted 121 lines · On Sun, Dec 17, 2023 at 12:40?PM Andrew Jones <[email protected]> wrote:
Add the files and functions needed to support the SBI STA (steal-time accounting) extension. In the next patches we'll complete the functions to fully enable SBI STA support.
Reviewed-by Anup Patel <[email protected]>Signed-off-by Andrew Jones <[email protected]>--- arch/riscv/include/asm/kvm_vcpu_sbi.h | 1 + arch/riscv/include/uapi/asm/kvm.h | 1 + arch/riscv/kvm/Makefile | 1 + arch/riscv/kvm/vcpu_sbi.c | 4 +++ arch/riscv/kvm/vcpu_sbi_sta.c | 47 +++++++++++++++++++++++++++ 5 files changed, 54 insertions(+) create mode 100644 arch/riscv/kvm/vcpu_sbi_sta.carch/riscv/include/asm/kvm_vcpu_sbi.h +1
-0@@ -76,6 +76,7 @@ extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_rfence;extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_srst;extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_hsm;extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_dbcn; +extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_sta;extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_experimental;extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_vendor;arch/riscv/include/uapi/asm/kvm.h +1
-0@@ -157,6 +157,7 @@ enum KVM_RISCV_SBI_EXT_ID {KVM_RISCV_SBI_EXT_EXPERIMENTAL ,KVM_RISCV_SBI_EXT_VENDOR ,KVM_RISCV_SBI_EXT_DBCN , +KVM_RISCV_SBI_EXT_STA , KVM_RISCV_SBI_EXT_MAX, };arch/riscv/kvm/Makefile +1
-0@@ -26,6 +26,7 @@ kvm-$(CONFIG_RISCV_SBI_V01) += vcpu_sbi_v01.o kvm-y += vcpu_sbi_base.o kvm-y += vcpu_sbi_replace.o kvm-y += vcpu_sbi_hsm.o +kvm-y += vcpu_sbi_sta.o kvm-y += vcpu_timer.o kvm-$(CONFIG_RISCV_PMU_SBI) += vcpu_pmu.o vcpu_sbi_pmu.o kvm-y += aia.oarch/riscv/kvm/vcpu_sbi.c +4
-0@@ -70,6 +70,10 @@ static const struct kvm_riscv_sbi_extension_entry sbi_ext[] = { .ext_idx = KVM_RISCV_SBI_EXT_DBCN, .ext_ptr = &vcpu_sbi_ext_dbcn, }, + { + .ext_idx = KVM_RISCV_SBI_EXT_STA, + .ext_ptr = &vcpu_sbi_ext_sta, + }, { .ext_idx = KVM_RISCV_SBI_EXT_EXPERIMENTAL, .ext_ptr = &vcpu_sbi_ext_experimental,arch/riscv/kvm/vcpu_sbi_sta.c new file +47
-1@@ -0,0 +1,47 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (c) 2023 Ventana Micro Systems Inc. +*/ + +#include <linux/kvm_host.h> + +#include <asm/kvm_vcpu_sbi.h> +#include <asm/sbi.h> + +static int kvm_sbi_sta_steal_time_set_shmem (struct kvm_vcpu *vcpu) +{ +return SBI_ERR_FAILURE ; +} + +static int kvm_sbi_ext_sta_handler (struct kvm_vcpu *vcpu,struct kvm_run *run, +struct kvm_vcpu_sbi_return *retdata) +{ +struct kvm_cpu_context *cp = &vcpu->arch.guest_context; +unsigned long funcid = cp->a6; +int ret; + +switch (funcid) { +case SBI_EXT_STA_STEAL_TIME_SET_SHMEM : + ret =kvm_sbi_sta_steal_time_set_shmem (vcpu); +break ; +default : + ret =SBI_ERR_NOT_SUPPORTED ; +break ; + } + + retdata->err_val = ret; + +return 0 ; +} + +static unsigned long kvm_sbi_ext_sta_probe (struct kvm_vcpu *vcpu) +{ +return 0 ; +} + +const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_sta = { + .extid_start =SBI_EXT_STA , + .extid_end =SBI_EXT_STA , + .handler = kvm_sbi_ext_sta_handler, + .probe = kvm_sbi_ext_sta_probe, +}; --
Quoted 2 lines
2.43.0
Signature · 2 lines
Regards, Atish
PATCH v3 5/13 RISC-V: KVM: Add steal-update vcpu request
In reply to Andrew JonesQuoted 74 lines · On Sun, Dec 17, 2023 at 12:40?PM Andrew Jones <[email protected]> wrote:
Add a new vcpu request to inform a vcpu that it should record its steal-time information. The request is made each time it has been detected that the vcpu task was not assigned a cpu for some time, which is easy to do by making the request from vcpu-load. The record function is just a stub for now and will be filled in with the rest of the steal-time support functions in following patches.
Reviewed-by Anup Patel <[email protected]>Signed-off-by Andrew Jones <[email protected]>--- arch/riscv/include/asm/kvm_host.h | 3 +++ arch/riscv/kvm/vcpu.c | 5 +++++ arch/riscv/kvm/vcpu_sbi_sta.c | 4 ++++ 3 files changed, 12 insertions(+)arch/riscv/include/asm/kvm_host.h +3
-0@@ -41,6 +41,7 @@ KVM_ARCH_REQ_FLAGS(4, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)#define KVM_REQ_HFENCE \ KVM_ARCH_REQ_FLAGS(5, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) +#define KVM_REQ_STEAL_UPDATE KVM_ARCH_REQ(6)enum kvm_riscv_hfence_type {KVM_RISCV_HFENCE_UNKNOWN =0 ,@@ -372,4 +373,6 @@ bool kvm_riscv_vcpu_has_interrupts(struct kvm_vcpu *vcpu, u64 mask);void kvm_riscv_vcpu_power_off (struct kvm_vcpu *vcpu);void kvm_riscv_vcpu_power_on (struct kvm_vcpu *vcpu); +void kvm_riscv_vcpu_record_steal_time (struct kvm_vcpu *vcpu); +#endif /* __RISCV_KVM_HOST_H__ */ arch/riscv/kvm/vcpu.c +5
-0@@ -541,6 +541,8 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)kvm_riscv_vcpu_aia_load (vcpu, cpu); +kvm_make_request (KVM_REQ_STEAL_UPDATE , vcpu); + vcpu->cpu = cpu; }@@ -614,6 +616,9 @@ static void kvm_riscv_check_vcpu_requests(struct kvm_vcpu *vcpu)if (kvm_check_request (KVM_REQ_HFENCE , vcpu))kvm_riscv_hfence_process (vcpu); + +if (kvm_check_request (KVM_REQ_STEAL_UPDATE , vcpu)) +kvm_riscv_vcpu_record_steal_time (vcpu); } }arch/riscv/kvm/vcpu_sbi_sta.c +4
-1@@ -8,6 +8,10 @@#include <asm/kvm_vcpu_sbi.h> #include <asm/sbi.h> +void kvm_riscv_vcpu_record_steal_time (struct kvm_vcpu *vcpu) +{ +} +static int kvm_sbi_sta_steal_time_set_shmem (struct kvm_vcpu *vcpu) {return SBI_ERR_FAILURE ; --
Quoted 2 lines
2.43.0
Signature · 2 lines
Regards, Atish
PATCH v3 6/13 RISC-V: KVM: Add SBI STA info to vcpu_arch
In reply to Andrew JonesQuoted 78 lines · On Sun, Dec 17, 2023 at 12:40?PM Andrew Jones <[email protected]> wrote:
KVM's implementation of SBI STA needs to track the address of each VCPU's steal-time shared memory region as well as the amount of stolen time. Add a structure to
vcpu_archto contain this state and make sure that the address is always set toINVALID_GPAon vcpu reset. And, of course, ensure KVM won't try to update steal- time when the shared memory address is invalid.Reviewed-by Anup Patel <[email protected]>Signed-off-by Andrew Jones <[email protected]>--- arch/riscv/include/asm/kvm_host.h | 7 +++++++ arch/riscv/kvm/vcpu.c | 2 ++ arch/riscv/kvm/vcpu_sbi_sta.c | 10 ++++++++++ 3 files changed, 19 insertions(+)arch/riscv/include/asm/kvm_host.h +7
-0@@ -263,6 +263,12 @@ struct kvm_vcpu_arch {/* 'static' configurations which are set only once */ struct kvm_vcpu_config cfg; + +/* SBI steal-time accounting */ +struct { +gpa_t shmem; +u64 last_steal; + } sta; };static inline void kvm_arch_sync_events (struct kvm *kvm) {}@@ -373,6 +379,7 @@ bool kvm_riscv_vcpu_has_interrupts(struct kvm_vcpu *vcpu, u64 mask);void kvm_riscv_vcpu_power_off (struct kvm_vcpu *vcpu);void kvm_riscv_vcpu_power_on (struct kvm_vcpu *vcpu); +void kvm_riscv_vcpu_sbi_sta_reset (struct kvm_vcpu *vcpu);void kvm_riscv_vcpu_record_steal_time (struct kvm_vcpu *vcpu);#endif /* __RISCV_KVM_HOST_H__ */ arch/riscv/kvm/vcpu.c +2
-0@@ -83,6 +83,8 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu) vcpu->arch.hfence_tail =0 ;memset (vcpu->arch.hfence_queue,0 ,sizeof (vcpu->arch.hfence_queue)); +kvm_riscv_vcpu_sbi_sta_reset (vcpu); +/* Reset the guest CSRs for hotplug usecase */ if (loaded)kvm_arch_vcpu_load (vcpu,smp_processor_id ());arch/riscv/kvm/vcpu_sbi_sta.c +10
-1@@ -8,8 +8,18 @@#include <asm/kvm_vcpu_sbi.h> #include <asm/sbi.h> +void kvm_riscv_vcpu_sbi_sta_reset (struct kvm_vcpu *vcpu) +{ + vcpu->arch.sta.shmem =INVALID_GPA ; + vcpu->arch.sta.last_steal =0 ; +} +void kvm_riscv_vcpu_record_steal_time (struct kvm_vcpu *vcpu) { +gpa_t shmem = vcpu->arch.sta.shmem; + +if (shmem ==INVALID_GPA ) +return ; }static int kvm_sbi_sta_steal_time_set_shmem (struct kvm_vcpu *vcpu) --
Quoted 2 lines
2.43.0
Signature · 2 lines
Regards, Atish
PATCH v3 1/13 RISC-V: paravirt: Add skeleton for pv-time support
In reply to Atish PatraQuoted 1 line · Atish Patra
On Sun, Dec 17, 2023 at 12:40?PM Andrew Jones <[email protected]> wrote:
...
Quoted 29 lines
Quoted 12 lines
arch/riscv/kernel/paravirt.c new file +12
-0+int __init pv_time_init (void ) +{ +int ret; + +if (!has_pv_steal_clock ()) +return 0 ; + + ret =cpuhp_setup_state (CPUHP_AP_ONLINE_DYN , +"riscv/pv_time:online" , + pv_time_cpu_online, + pv_time_cpu_down_prepare); +if (ret <0 )arch/riscv/kernel/paravirt.c line 75
Quoted 10 lines
arch/riscv/kernel/paravirt.c new file +10
-0+return ret; + +static_call_update (pv_steal_clock, pv_time_steal_clock); + +static_key_slow_inc (¶virt_steal_enabled); +if (steal_acc) +static_key_slow_inc (¶virt_steal_rq_enabled); + +pr_info ("using paravirt steal-time\n" ); +Nit comment:
If the scheduler doesn't take stolen time into consideration (when If no-steal-acc is specified in the command line),
the above log is a bit misleading ? ARM64 also seems to be doing the same thing.
Quoted 2 lines
Maybe the intention of the log is to steal time extension presence rather than actual usage in place ?
You're right that the word "using" isn't great. "Computing" would be better as, even with no-steal-acc, steal time gets computed and shows up in /proc/stat.
Quoted 28 lines
Quoted 23 lines
(quoted diff) +2
-0+ return 0; +}arch/riscv/kernel/time.c +3
-1@@ -12,6 +12,7 @@#include <asm/sbi.h> #include <asm/processor.h> #include <asm/timex.h> +#include <asm/paravirt.h> unsigned long riscv_timebase__ro_after_init ;EXPORT_SYMBOL_GPL (riscv_timebase);@@ -45,4 +46,6 @@ void __init time_init(void)timer_probe ();tick_setup_hrtimer_broadcast (); + +pv_time_init (); } --Quoted 2 lines
2.43.0
Other than that, lgtm.
Quoted 1 line
Reviewed-by Atish Patra <[email protected]>
Thanks,
drew
PATCH v3 7/13 RISC-V: KVM: Add support for SBI extension registers
In reply to Andrew JonesQuoted 36 lines · On Sun, Dec 17, 2023 at 12:40?PM Andrew Jones <[email protected]> wrote:
Some SBI extensions have state that needs to be saved / restored when migrating the VM. Provide a get/set-one-reg register type for SBI extension registers. Each SBI extension that uses this type will have its own subtype. There are currently no subtypes defined. The next patch introduces the first one.
Reviewed-by Anup Patel <[email protected]>Signed-off-by Andrew Jones <[email protected]>--- arch/riscv/include/asm/kvm_vcpu_sbi.h | 4 ++ arch/riscv/include/uapi/asm/kvm.h | 3 ++ arch/riscv/kvm/vcpu_onereg.c | 42 +++++++++++++++++-- arch/riscv/kvm/vcpu_sbi.c | 58 +++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 4 deletions(-)arch/riscv/include/asm/kvm_vcpu_sbi.h +4
-0@@ -60,6 +60,10 @@ int kvm_riscv_vcpu_set_reg_sbi_ext(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);int kvm_riscv_vcpu_get_reg_sbi_ext (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg); +int kvm_riscv_vcpu_set_reg_sbi (struct kvm_vcpu *vcpu, +const struct kvm_one_reg *reg); +int kvm_riscv_vcpu_get_reg_sbi (struct kvm_vcpu *vcpu, +const struct kvm_one_reg *reg);const struct kvm_vcpu_sbi_extension *kvm_vcpu_sbi_find_ext (struct kvm_vcpu *vcpu,unsigned long extid);bool riscv_vcpu_supports_sbi_ext (struct kvm_vcpu *vcpu,int idx);arch/riscv/include/uapi/asm/kvm.h +0
-0
arch/riscv/include/uapi/asm/kvm.h enum KVM_RISCV_SBI_EXT_ID { · line 247
Quoted 6 lines
arch/riscv/include/uapi/asm/kvm.h +3
-0@@ -242,6 +242,9 @@ enum KVM_RISCV_SBI_EXT_ID {#define KVM_REG_RISCV_VECTOR_REG (n) \ ((n) + sizeof(struct __riscv_v_ext_state) / sizeof(unsigned long)) +/* Registers for specific SBI extensions are mapped as type 10 */ +#define KVM_REG_RISCV_SBI (0x0a << KVM_REG_RISCV_TYPE_SHIFT) +
nit comment: KVM_REG_RISCV_SBI looks bit odd when we already have KVM_REG_RISCV_SBI_EXT for
extension enabling/disabling.
How about renaming this to KVM_REG_RISCV_SBI_EXT_STATE or something similar indicate that this
for a specific extension state ?
Quoted 162 lines
arch/riscv/include/uapi/asm/kvm.h +0
-0/* Device Control API: RISC-V AIA */ #define KVM_DEV_RISCV_APLIC_ALIGN 0x1000#define KVM_DEV_RISCV_APLIC_SIZE 0x4000arch/riscv/kvm/vcpu_onereg.c +38
-4@@ -961,6 +961,29 @@ static unsigned long num_sbi_ext_regs(struct kvm_vcpu *vcpu)return copy_sbi_ext_reg_indices (vcpu,NULL ); } +static inline unsigned long num_sbi_regs (struct kvm_vcpu *vcpu) +{ +return 0 ; +} + +static int copy_sbi_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices) +{ +int n =num_sbi_regs (vcpu); + +for (int i =0 ; i < n; i++) { +u64 reg =KVM_REG_RISCV |KVM_REG_SIZE_U64 | +KVM_REG_RISCV_SBI | i; + +if (uindices) { +if (put_user (reg, uindices)) +return -EFAULT ; + uindices++; + } + } + +return n; +} +static inline unsigned long num_vector_regs (const struct kvm_vcpu *vcpu) {if (!riscv_isa_extension_available (vcpu->arch.isa, v))@@ -1028,6 +1051,7 @@ unsigned long kvm_riscv_vcpu_num_regs(struct kvm_vcpu *vcpu) res +=num_vector_regs (vcpu); res +=num_isa_ext_regs (vcpu); res +=num_sbi_ext_regs (vcpu); + res +=num_sbi_regs (vcpu);return res; }@@ -1083,6 +1107,12 @@ int kvm_riscv_vcpu_copy_reg_indices(struct kvm_vcpu *vcpu, ret =copy_sbi_ext_reg_indices (vcpu, uindices);if (ret <0 )return ret; + uindices += ret; + + ret =copy_sbi_reg_indices (vcpu, uindices); +if (ret <0 ) +return ret; + uindices += ret;return 0 ; }@@ -1105,12 +1135,14 @@ int kvm_riscv_vcpu_set_reg(struct kvm_vcpu *vcpu,case KVM_REG_RISCV_FP_D :return kvm_riscv_vcpu_set_reg_fp (vcpu, reg,KVM_REG_RISCV_FP_D ); +case KVM_REG_RISCV_VECTOR : +return kvm_riscv_vcpu_set_reg_vector (vcpu, reg);case KVM_REG_RISCV_ISA_EXT :return kvm_riscv_vcpu_set_reg_isa_ext (vcpu, reg);case KVM_REG_RISCV_SBI_EXT :return kvm_riscv_vcpu_set_reg_sbi_ext (vcpu, reg); -case KVM_REG_RISCV_VECTOR : -return kvm_riscv_vcpu_set_reg_vector (vcpu, reg); +case KVM_REG_RISCV_SBI : +return kvm_riscv_vcpu_set_reg_sbi (vcpu, reg);default :break ; }@@ -1136,12 +1168,14 @@ int kvm_riscv_vcpu_get_reg(struct kvm_vcpu *vcpu,case KVM_REG_RISCV_FP_D :return kvm_riscv_vcpu_get_reg_fp (vcpu, reg,KVM_REG_RISCV_FP_D ); +case KVM_REG_RISCV_VECTOR : +return kvm_riscv_vcpu_get_reg_vector (vcpu, reg);case KVM_REG_RISCV_ISA_EXT :return kvm_riscv_vcpu_get_reg_isa_ext (vcpu, reg);case KVM_REG_RISCV_SBI_EXT :return kvm_riscv_vcpu_get_reg_sbi_ext (vcpu, reg); -case KVM_REG_RISCV_VECTOR : -return kvm_riscv_vcpu_get_reg_vector (vcpu, reg); +case KVM_REG_RISCV_SBI : +return kvm_riscv_vcpu_get_reg_sbi (vcpu, reg);default :break ; }arch/riscv/kvm/vcpu_sbi.c +58
-1@@ -325,6 +325,64 @@ int kvm_riscv_vcpu_get_reg_sbi_ext(struct kvm_vcpu *vcpu,return 0 ; } +int kvm_riscv_vcpu_set_reg_sbi (struct kvm_vcpu *vcpu, +const struct kvm_one_reg *reg) +{ +unsigned long __user *uaddr = + (unsigned long __user *)(unsigned long )reg->addr; +unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | +KVM_REG_SIZE_MASK | +KVM_REG_RISCV_SBI ); +unsigned long reg_subtype, reg_val; + +if (KVM_REG_SIZE (reg->id) !=sizeof (unsigned long )) +return -EINVAL ; + +if (copy_from_user (®_val, uaddr,KVM_REG_SIZE (reg->id))) +return -EFAULT ; + + reg_subtype = reg_num &KVM_REG_RISCV_SUBTYPE_MASK ; + reg_num &= ~KVM_REG_RISCV_SUBTYPE_MASK ; + +switch (reg_subtype) { +default : +return -EINVAL ; + } + +return 0 ; +} + +int kvm_riscv_vcpu_get_reg_sbi (struct kvm_vcpu *vcpu, +const struct kvm_one_reg *reg) +{ +unsigned long __user *uaddr = + (unsigned long __user *)(unsigned long )reg->addr; +unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | +KVM_REG_SIZE_MASK | +KVM_REG_RISCV_SBI ); +unsigned long reg_subtype, reg_val; +int ret; + +if (KVM_REG_SIZE (reg->id) !=sizeof (unsigned long )) +return -EINVAL ; + + reg_subtype = reg_num &KVM_REG_RISCV_SUBTYPE_MASK ; + reg_num &= ~KVM_REG_RISCV_SUBTYPE_MASK ; + +switch (reg_subtype) { +default : +return -EINVAL ; + } + +if (ret) +return ret; + +if (copy_to_user (uaddr, ®_val,KVM_REG_SIZE (reg->id))) +return -EFAULT ; + +return 0 ; +} +const struct kvm_vcpu_sbi_extension *kvm_vcpu_sbi_find_ext (struct kvm_vcpu *vcpu,unsigned long extid) { --
Quoted 2 lines
2.43.0
Other than that, lgtm.
Signature · 2 lines
Regards, Atish
PATCH v3 9/13 RISC-V: KVM: Implement SBI STA extension
In reply to Andrew JonesQuoted 157 lines · On Sun, Dec 17, 2023 at 12:40?PM Andrew Jones <[email protected]> wrote:
Add a select
SCHED_INFOto the KVM config in order to getrun_delayinfo. Then implement SBI STA's set-steal-time-shmem function andkvm_riscv_vcpu_record_steal_time()to provide the steal-time info to guests.Reviewed-by Anup Patel <[email protected]>Signed-off-by Andrew Jones <[email protected]>--- arch/riscv/kvm/Kconfig | 1 + arch/riscv/kvm/vcpu_sbi_sta.c | 96 ++++++++++++++++++++++++++++++++++- 2 files changed, 95 insertions(+), 2 deletions(-)arch/riscv/kvm/Kconfig +1
-0@@ -32,6 +32,7 @@ config KVM select KVM_XFER_TO_GUEST_WORK select MMU_NOTIFIER select PREEMPT_NOTIFIERS + select SCHED_INFO help Support hosting virtualized guest machines.arch/riscv/kvm/vcpu_sbi_sta.c +94
-3@@ -6,9 +6,15 @@#include <linux/kconfig.h> #include <linux/kernel.h> #include <linux/kvm_host.h> +#include <linux/mm.h> +#include <linux/sizes.h> +#include <asm/bug.h> +#include <asm/current.h> #include <asm/kvm_vcpu_sbi.h> +#include <asm/page.h> #include <asm/sbi.h> +#include <asm/uaccess.h> void kvm_riscv_vcpu_sbi_sta_reset (struct kvm_vcpu *vcpu) {@@ -19,14 +25,100 @@ void kvm_riscv_vcpu_sbi_sta_reset(struct kvm_vcpu *vcpu)void kvm_riscv_vcpu_record_steal_time (struct kvm_vcpu *vcpu) {gpa_t shmem = vcpu->arch.sta.shmem; +u64 last_steal = vcpu->arch.sta.last_steal; +u32 *sequence_ptr, sequence; +u64 *steal_ptr, steal; +unsigned long hva; +gfn_t gfn;if (shmem ==INVALID_GPA )return ; + +/* +* shmem is 64-byte aligned (see the enforcement in +* kvm_sbi_sta_steal_time_set_shmem()) and the size of sbi_sta_struct +* is 64 bytes, so we know all its offsets are in the same page. +*/ + gfn = shmem >>PAGE_SHIFT ; + hva =kvm_vcpu_gfn_to_hva (vcpu, gfn); + +if (WARN_ON (kvm_is_error_hva (hva))) { + vcpu->arch.sta.shmem =INVALID_GPA ; +return ; + } + + sequence_ptr = (u32 *)(hva +offset_in_page (shmem) + + offsetof(struct sbi_sta_struct , sequence)); + steal_ptr = (u64 *)(hva +offset_in_page (shmem) + + offsetof(struct sbi_sta_struct , steal)); + +if (WARN_ON (get_user (sequence, sequence_ptr))) +return ; + + sequence =le32_to_cpu (sequence); + sequence +=1 ; + +if (WARN_ON (put_user (cpu_to_le32 (sequence), sequence_ptr))) +return ; + +if (!WARN_ON (get_user (steal, steal_ptr))) { + steal =le64_to_cpu (steal); + vcpu->arch.sta.last_steal =READ_ONCE (current->sched_info.run_delay); + steal += vcpu->arch.sta.last_steal - last_steal; +WARN_ON (put_user (cpu_to_le64 (steal), steal_ptr)); + } + + sequence +=1 ; +WARN_ON (put_user (cpu_to_le32 (sequence), sequence_ptr)); + +kvm_vcpu_mark_page_dirty (vcpu, gfn); }static int kvm_sbi_sta_steal_time_set_shmem (struct kvm_vcpu *vcpu) { -return SBI_ERR_FAILURE ; +struct kvm_cpu_context *cp = &vcpu->arch.guest_context; +unsigned long shmem_phys_lo = cp->a0; +unsigned long shmem_phys_hi = cp->a1; +u32 flags = cp->a2; +struct sbi_sta_struct zero_sta = {0 }; +unsigned long hva; +bool writable; +gpa_t shmem; +int ret; + +if (flags !=0 ) +return SBI_ERR_INVALID_PARAM ; + +if (shmem_phys_lo ==SBI_STA_SHMEM_DISABLE && + shmem_phys_hi ==SBI_STA_SHMEM_DISABLE ) { + vcpu->arch.sta.shmem =INVALID_GPA ; +return 0 ; + } + +if (shmem_phys_lo & (SZ_64 -1 )) +return SBI_ERR_INVALID_PARAM ; + + shmem = shmem_phys_lo; + +if (shmem_phys_hi !=0 ) { +if (IS_ENABLED (CONFIG_32BIT )) + shmem |= ((gpa_t )shmem_phys_hi <<32 ); +else +return SBI_ERR_INVALID_ADDRESS ; + } + + hva =kvm_vcpu_gfn_to_hva_prot (vcpu, shmem >>PAGE_SHIFT , &writable); +if (kvm_is_error_hva (hva) || !writable) +return SBI_ERR_INVALID_ADDRESS ; + + ret =kvm_vcpu_write_guest (vcpu, shmem, &zero_sta,sizeof (zero_sta)); +if (ret) +return SBI_ERR_FAILURE ; + + vcpu->arch.sta.shmem = shmem; + vcpu->arch.sta.last_steal = current->sched_info.run_delay; + +return 0 ; } static int kvm_sbi_ext_sta_handler(struct kvm_vcpu *vcpu, struct kvm_run *run,@@ -52,7 +144,7 @@ static int kvm_sbi_ext_sta_handler(struct kvm_vcpu *vcpu, struct kvm_run *run,static unsigned long kvm_sbi_ext_sta_probe (struct kvm_vcpu *vcpu) { -return 0 ; +return !!sched_info_on (); }const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_sta = { --
Quoted 2 lines
2.43.0
Signature · 2 lines
Regards, Atish
PATCH v3 11/13 RISC-V: KVM: selftests: Add guest_sbi_probe_extension
In reply to Andrew JonesQuoted 24 lines · On Sun, Dec 17, 2023 at 12:40?PM Andrew Jones <[email protected]> wrote:
Add
guest_sbi_probe_extension(), allowing guest code to probe for SBI extensions. Asguest_sbi_probe_extension()needsSBI_ERR_NOT_SUPPORTED, take the opportunity to bring in all SBI error codes. We don't bring in all current extension IDs or base extension function IDs though, even though we need one of each, because we'd prefer to bring those in as necessary.Reviewed-by Anup Patel <[email protected]>Signed-off-by Andrew Jones <[email protected]>--- .../selftests/kvm/include/riscv/processor.h | 21 +++++++++++++++++++ .../selftests/kvm/lib/riscv/processor.c | 19 +++++++++++++++++ 2 files changed, 40 insertions(+)tools/testing/selftests/kvm/include/riscv/processor.h +1
-0@@ -108,6 +108,17 @@ static inline uint64_t __kvm_reg_id(uint64_t type, uint64_t subtype,#define SATP_ASID_SHIFT 44#define SATP_ASID_MASK _AC(0xFFFF, UL) +/* SBI return error codes */
tools/testing/selftests/kvm/include/riscv/processor.h static inline uint64_t __kvm_reg_id(uint64_t type, uint64_t subtype, · line 121
Quoted 10 lines
tools/testing/selftests/kvm/include/riscv/processor.h +10
-0+#define SBI_SUCCESS 0 +#define SBI_ERR_FAILURE -1 +#define SBI_ERR_NOT_SUPPORTED -2 +#define SBI_ERR_INVALID_PARAM -3 +#define SBI_ERR_DENIED -4 +#define SBI_ERR_INVALID_ADDRESS -5 +#define SBI_ERR_ALREADY_AVAILABLE -6 +#define SBI_ERR_ALREADY_STARTED -7 +#define SBI_ERR_ALREADY_STOPPED -8 +
Add SBI_ERR_NO_SHMEM as well ?
Quoted 53 lines
tools/testing/selftests/kvm/include/riscv/processor.h +10
-0#define SBI_EXT_EXPERIMENTAL_START 0x08000000#define SBI_EXT_EXPERIMENTAL_END 0x08FFFFFF@@ -115,6 +126,14 @@ static inline uint64_t __kvm_reg_id(uint64_t type, uint64_t subtype,#define KVM_RISCV_SELFTESTS_SBI_UCALL 0#define KVM_RISCV_SELFTESTS_SBI_UNEXP 1 +enum sbi_ext_id { +SBI_EXT_BASE =0x10 , +}; + +enum sbi_ext_base_fid { +SBI_EXT_BASE_PROBE_EXT =3 , +}; +struct sbiret {long error;long value;@@ -125,4 +144,6 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0, unsigned long arg3, unsigned long arg4, unsigned long arg5); +bool guest_sbi_probe_extension (int extid,long *out_val); +#endif /* SELFTEST_KVM_PROCESSOR_H */ tools/testing/selftests/kvm/lib/riscv/processor.c +19
-1@@ -393,3 +393,22 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,return ret; } + +bool guest_sbi_probe_extension (int extid,long *out_val) +{ +struct sbiret ret; + + ret =sbi_ecall (SBI_EXT_BASE ,SBI_EXT_BASE_PROBE_EXT , extid, +0 ,0 ,0 ,0 ,0 ); + +__GUEST_ASSERT (!ret.error || ret.error ==SBI_ERR_NOT_SUPPORTED , +"ret.error=%ld, ret.value=%ld\n" , ret.error, ret.value); + +if (ret.error ==SBI_ERR_NOT_SUPPORTED ) +return false; + +if (out_val) + *out_val = ret.value; + +return true; +} --
Quoted 2 lines
2.43.0
Apart from that, LGTM.
Signature · 2 lines
Regards, Atish
PATCH v3 12/13 RISC-V: KVM: selftests: Add steal_time test support
In reply to Andrew JonesQuoted 163 lines · On Sun, Dec 17, 2023 at 12:40?PM Andrew Jones <[email protected]> wrote:
With the introduction of steal-time accounting support for RISC-V KVM we can add RISC-V support to the
steal_timetest.Reviewed-by Anup Patel <[email protected]>Signed-off-by Andrew Jones <[email protected]>--- tools/testing/selftests/kvm/Makefile | 5 +- .../selftests/kvm/include/riscv/processor.h | 1 + tools/testing/selftests/kvm/steal_time.c | 99 +++++++++++++++++++ 3 files changed, 103 insertions(+), 2 deletions(-)tools/testing/selftests/kvm/Makefile +3
-2@@ -194,12 +194,13 @@ TEST_GEN_PROGS_s390x += kvm_binary_stats_test TEST_GEN_PROGS_riscv += demand_paging_test TEST_GEN_PROGS_riscv += dirty_log_test -TEST_GEN_PROGS_riscv += guest_print_test TEST_GEN_PROGS_riscv += get-reg-list +TEST_GEN_PROGS_riscv += guest_print_test +TEST_GEN_PROGS_riscv += kvm_binary_stats_test TEST_GEN_PROGS_riscv += kvm_create_max_vcpus TEST_GEN_PROGS_riscv += kvm_page_table_test TEST_GEN_PROGS_riscv += set_memory_region_test -TEST_GEN_PROGS_riscv += kvm_binary_stats_test +TEST_GEN_PROGS_riscv += steal_time SPLIT_TESTS += get-reg-listtools/testing/selftests/kvm/include/riscv/processor.h +1
-0@@ -128,6 +128,7 @@ static inline uint64_t __kvm_reg_id(uint64_t type, uint64_t subtype,enum sbi_ext_id {SBI_EXT_BASE =0x10 , +SBI_EXT_STA =0x535441 , };enum sbi_ext_base_fid {tools/testing/selftests/kvm/steal_time.c +99
-1@@ -11,7 +11,9 @@#include <pthread.h> #include <linux/kernel.h> #include <asm/kvm.h> +#ifndef __riscv#include <asm/kvm_para.h> +#endif #include "test_util.h" #include "kvm_util.h" @@ -203,6 +205,103 @@ static void steal_time_dump(struct kvm_vm *vm, uint32_t vcpu_idx)pr_info (" st_time: %ld\n" , st->st_time); } +#elif defined(__riscv) + +/* SBI STA shmem must have 64-byte alignment */ +#define STEAL_TIME_SIZE ((sizeof(struct sta_struct) + 63) & ~63) + +static vm_paddr_t st_gpa[NR_VCPUS ]; + +struct sta_struct { +uint32_t sequence; +uint32_t flags; +uint64_t steal; +uint8_t preempted; +uint8_t pad[47 ]; +} __packed; + +static void sta_set_shmem (vm_paddr_t gpa,unsigned long flags) +{ +unsigned long lo = (unsigned long )gpa; +#if __riscv_xlen ==32 +unsigned long hi = (unsigned long )(gpa >>32 ); +#else +unsigned long hi = gpa ==-1 ?-1 :0 ; +#endif +struct sbiret ret =sbi_ecall (SBI_EXT_STA ,0 , lo, hi, flags,0 ,0 ,0 ); + +GUEST_ASSERT (ret.value ==0 && ret.error ==0 ); +} + +static void check_status (struct sta_struct *st) +{ +GUEST_ASSERT (!(READ_ONCE (st->sequence) &1 )); +GUEST_ASSERT (READ_ONCE (st->flags) ==0 ); +GUEST_ASSERT (READ_ONCE (st->preempted) ==0 ); +} + +static void guest_code (int cpu) +{ +struct sta_struct *st = st_gva[cpu]; +uint32_t sequence; +long out_val =0 ; +bool probe; + + probe =guest_sbi_probe_extension (SBI_EXT_STA , &out_val); +GUEST_ASSERT (probe && out_val ==1 ); + +sta_set_shmem (st_gpa[cpu],0 ); +GUEST_SYNC (0 ); + +check_status (st); +WRITE_ONCE (guest_stolen_time[cpu], st->steal); + sequence =READ_ONCE (st->sequence); +check_status (st); +GUEST_SYNC (1 ); + +check_status (st); +GUEST_ASSERT (sequence <READ_ONCE (st->sequence)); +WRITE_ONCE (guest_stolen_time[cpu], st->steal); +check_status (st); +GUEST_DONE (); +} + +static bool is_steal_time_supported (struct kvm_vcpu *vcpu) +{ +uint64_t id =RISCV_SBI_EXT_REG (KVM_RISCV_SBI_EXT_STA ); +unsigned long enabled; + +vcpu_get_reg (vcpu, id, &enabled); +TEST_ASSERT (enabled ==0 || enabled ==1 ,"Expected boolean result" ); + +return enabled; +} + +static void steal_time_init (struct kvm_vcpu *vcpu,uint32_t i) +{ +/* ST_GPA_BASE is identity mapped */ + st_gva[i] = (void *)(ST_GPA_BASE + i *STEAL_TIME_SIZE ); + st_gpa[i] =addr_gva2gpa (vcpu->vm, (vm_vaddr_t )st_gva[i]); +sync_global_to_guest (vcpu->vm, st_gva[i]); +sync_global_to_guest (vcpu->vm, st_gpa[i]); +} + +static void steal_time_dump (struct kvm_vm *vm,uint32_t vcpu_idx) +{ +struct sta_struct *st =addr_gva2hva (vm, (ulong )st_gva[vcpu_idx]); +int i; + +pr_info ("VCPU%d:\n" , vcpu_idx); +pr_info (" sequence: %d\n" , st->sequence); +pr_info (" flags: %d\n" , st->flags); +pr_info (" steal: %" PRIu64"\n" , st->steal); +pr_info (" preempted: %d\n" , st->preempted); +pr_info (" pad: " ); +for (i =0 ; i <47 ; ++i) +pr_info ("%d" , st->pad[i]); +pr_info ("\n" ); +} +#endif static void *do_steal_time (void *arg) --
Quoted 2 lines
2.43.0
Signature · 2 lines
Regards, Atish
PATCH v3 13/13 RISC-V: KVM: selftests: Add get-reg-list test for STA registers
In reply to Andrew JonesQuoted 113 lines · On Sun, Dec 17, 2023 at 12:41?PM Andrew Jones <[email protected]> wrote:
Add SBI STA and its two registers to the get-reg-list test.
Reviewed-by Anup Patel <[email protected]>Signed-off-by Andrew Jones <[email protected]>--- .../selftests/kvm/riscv/get-reg-list.c | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+)tools/testing/selftests/kvm/riscv/get-reg-list.c +43
-1@@ -71,6 +71,7 @@ bool filter_reg(__u64 reg)case KVM_REG_RISCV_SBI_EXT |KVM_REG_RISCV_SBI_SINGLE |KVM_RISCV_SBI_EXT_HSM :case KVM_REG_RISCV_SBI_EXT |KVM_REG_RISCV_SBI_SINGLE |KVM_RISCV_SBI_EXT_PMU :case KVM_REG_RISCV_SBI_EXT |KVM_REG_RISCV_SBI_SINGLE |KVM_RISCV_SBI_EXT_DBCN : +case KVM_REG_RISCV_SBI_EXT |KVM_REG_RISCV_SBI_SINGLE |KVM_RISCV_SBI_EXT_STA :case KVM_REG_RISCV_SBI_EXT |KVM_REG_RISCV_SBI_SINGLE |KVM_RISCV_SBI_EXT_EXPERIMENTAL :case KVM_REG_RISCV_SBI_EXT |KVM_REG_RISCV_SBI_SINGLE |KVM_RISCV_SBI_EXT_VENDOR :return true;@@ -461,6 +462,7 @@ static const char *sbi_ext_single_id_to_str(__u64 reg_off)KVM_SBI_EXT_ARR (KVM_RISCV_SBI_EXT_SRST ),KVM_SBI_EXT_ARR (KVM_RISCV_SBI_EXT_HSM ),KVM_SBI_EXT_ARR (KVM_RISCV_SBI_EXT_PMU ), +KVM_SBI_EXT_ARR (KVM_RISCV_SBI_EXT_STA ),KVM_SBI_EXT_ARR (KVM_RISCV_SBI_EXT_EXPERIMENTAL ),KVM_SBI_EXT_ARR (KVM_RISCV_SBI_EXT_VENDOR ), KVM_SBI_EXT_ARR(KVM_RISCV_SBI_EXT_DBCN),@@ -509,6 +511,32 @@ static const char *sbi_ext_id_to_str(const char *prefix, __u64 id)return strdup_printf ("%lld | %lld /* UNKNOWN */" , reg_subtype, reg_off); } +static const char *sbi_sta_id_to_str (__u64 reg_off) +{ +switch (reg_off) { +case 0 :return "KVM_REG_RISCV_SBI_STA | KVM_REG_RISCV_SBI_STA_REG(shmem_lo)" ; +case 1 :return "KVM_REG_RISCV_SBI_STA | KVM_REG_RISCV_SBI_STA_REG(shmem_hi)" ; + } +return strdup_printf ("KVM_REG_RISCV_SBI_STA | %lld /* UNKNOWN */" , reg_off); +} + +static const char *sbi_id_to_str (const char *prefix,__u64 id) +{ +__u64 reg_off = id & ~(REG_MASK |KVM_REG_RISCV_SBI ); +__u64 reg_subtype = reg_off &KVM_REG_RISCV_SUBTYPE_MASK ; + +assert ((id &KVM_REG_RISCV_TYPE_MASK ) ==KVM_REG_RISCV_SBI ); + + reg_off &= ~KVM_REG_RISCV_SUBTYPE_MASK ; + +switch (reg_subtype) { +case KVM_REG_RISCV_SBI_STA : +return sbi_sta_id_to_str (reg_off); + } + +return strdup_printf ("%lld | %lld /* UNKNOWN */" , reg_subtype, reg_off); +} +void print_reg (const char *prefix,__u64 id) {const char *reg_size =NULL ;@@ -565,6 +593,10 @@ void print_reg(const char *prefix, __u64 id)printf ("\tKVM_REG_RISCV | %s | KVM_REG_RISCV_SBI_EXT | %s,\n" , reg_size,sbi_ext_id_to_str (prefix, id));break ; +case KVM_REG_RISCV_SBI : +printf ("\tKVM_REG_RISCV | %s | KVM_REG_RISCV_SBI | %s,\n" , + reg_size,sbi_id_to_str (prefix, id)); +break ;default :printf ("\tKVM_REG_RISCV | %s | 0x%llx /* UNKNOWN */,\n" , reg_size, id & ~REG_MASK );@@ -651,6 +683,12 @@ static __u64 sbi_base_regs[] = { KVM_REG_RISCV | KVM_REG_SIZE_ULONG | KVM_REG_RISCV_SBI_EXT | KVM_REG_RISCV_SBI_SINGLE | KVM_RISCV_SBI_EXT_VENDOR, }; +static __u64 sbi_sta_regs[] = { +KVM_REG_RISCV |KVM_REG_SIZE_ULONG |KVM_REG_RISCV_SBI_EXT |KVM_REG_RISCV_SBI_SINGLE |KVM_RISCV_SBI_EXT_STA , +KVM_REG_RISCV |KVM_REG_SIZE_ULONG |KVM_REG_RISCV_SBI |KVM_REG_RISCV_SBI_STA |KVM_REG_RISCV_SBI_STA_REG (shmem_lo), +KVM_REG_RISCV |KVM_REG_SIZE_ULONG |KVM_REG_RISCV_SBI |KVM_REG_RISCV_SBI_STA |KVM_REG_RISCV_SBI_STA_REG (shmem_hi), +}; +static __u64 zicbom_regs[] = {KVM_REG_RISCV |KVM_REG_SIZE_ULONG |KVM_REG_RISCV_CONFIG |KVM_REG_RISCV_CONFIG_REG (zicbom_block_size),KVM_REG_RISCV |KVM_REG_SIZE_ULONG |KVM_REG_RISCV_ISA_EXT |KVM_REG_RISCV_ISA_SINGLE |KVM_RISCV_ISA_EXT_ZICBOM ,@@ -757,6 +795,9 @@ static __u64 fp_d_regs[] = {#define SUBLIST_SBI_BASE \ {"sbi-base", .feature_type = VCPU_FEATURE_SBI_EXT, .feature = KVM_RISCV_SBI_EXT_V01, \ .regs = sbi_base_regs, .regs_n = ARRAY_SIZE(sbi_base_regs),} +#define SUBLIST_SBI_STA \ + {"sbi-sta", .feature_type = VCPU_FEATURE_SBI_EXT, .feature = KVM_RISCV_SBI_EXT_STA, \ + .regs = sbi_sta_regs, .regs_n = ARRAY_SIZE(sbi_sta_regs),}#define SUBLIST_ZICBOM \ {"zicbom", .feature = KVM_RISCV_ISA_EXT_ZICBOM, .regs = zicbom_regs, .regs_n = ARRAY_SIZE(zicbom_regs),}#define SUBLIST_ZICBOZ \@@ -832,6 +873,7 @@ static struct vcpu_reg_list config_sbi_##ext = { \/* Note: The below list is alphabetically sorted. */ KVM_SBI_EXT_SUBLIST_CONFIG (base,BASE ); +KVM_SBI_EXT_SUBLIST_CONFIG (sta,STA );KVM_SBI_EXT_SIMPLE_CONFIG (pmu,PMU );KVM_SBI_EXT_SIMPLE_CONFIG (dbcn,DBCN );@@ -858,6 +900,7 @@ KVM_ISA_EXT_SIMPLE_CONFIG(zihpm, ZIHPM);struct vcpu_reg_list *vcpu_configs[] = { &config_sbi_base, + &config_sbi_sta, &config_sbi_pmu, &config_sbi_dbcn, &config_aia, --
Quoted 2 lines
2.43.0
Signature · 2 lines
Regards, Atish
PATCH v3 7/13 RISC-V: KVM: Add support for SBI extension registers
In reply to Atish PatraQuoted 49 lines · Atish Patra
Quoted 36 lines · On Sun, Dec 17, 2023 at 12:40?PM Andrew Jones <[email protected]> wrote:
Some SBI extensions have state that needs to be saved / restored when migrating the VM. Provide a get/set-one-reg register type for SBI extension registers. Each SBI extension that uses this type will have its own subtype. There are currently no subtypes defined. The next patch introduces the first one.
Reviewed-by Anup Patel <[email protected]>Signed-off-by Andrew Jones <[email protected]>--- arch/riscv/include/asm/kvm_vcpu_sbi.h | 4 ++ arch/riscv/include/uapi/asm/kvm.h | 3 ++ arch/riscv/kvm/vcpu_onereg.c | 42 +++++++++++++++++-- arch/riscv/kvm/vcpu_sbi.c | 58 +++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 4 deletions(-)arch/riscv/include/asm/kvm_vcpu_sbi.h +4
-0@@ -60,6 +60,10 @@ int kvm_riscv_vcpu_set_reg_sbi_ext(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);int kvm_riscv_vcpu_get_reg_sbi_ext (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg); +int kvm_riscv_vcpu_set_reg_sbi (struct kvm_vcpu *vcpu, +const struct kvm_one_reg *reg); +int kvm_riscv_vcpu_get_reg_sbi (struct kvm_vcpu *vcpu, +const struct kvm_one_reg *reg);const struct kvm_vcpu_sbi_extension *kvm_vcpu_sbi_find_ext (struct kvm_vcpu *vcpu,unsigned long extid);bool riscv_vcpu_supports_sbi_ext (struct kvm_vcpu *vcpu,int idx);arch/riscv/include/uapi/asm/kvm.h +0
-0arch/riscv/include/uapi/asm/kvm.h enum KVM_RISCV_SBI_EXT_ID { · line 247
Quoted 6 lines
arch/riscv/include/uapi/asm/kvm.h +3
-0@@ -242,6 +242,9 @@ enum KVM_RISCV_SBI_EXT_ID {#define KVM_REG_RISCV_VECTOR_REG (n) \ ((n) + sizeof(struct __riscv_v_ext_state) / sizeof(unsigned long)) +/* Registers for specific SBI extensions are mapped as type 10 */ +#define KVM_REG_RISCV_SBI (0x0a << KVM_REG_RISCV_TYPE_SHIFT) +nit comment:
KVM_REG_RISCV_SBIlooks bit odd when we already haveKVM_REG_RISCV_SBI_EXTfor
extension enabling/disabling.
Quoted 3 lines
How about renaming this to
KVM_REG_RISCV_SBI_EXT_STATEor something similar indicate that this
for a specific extension state ?
OK, will do for v4.
Quoted 169 lines
Quoted 164 lines
arch/riscv/include/uapi/asm/kvm.h +0
-0/* Device Control API: RISC-V AIA */ #define KVM_DEV_RISCV_APLIC_ALIGN 0x1000#define KVM_DEV_RISCV_APLIC_SIZE 0x4000arch/riscv/kvm/vcpu_onereg.c +38
-4@@ -961,6 +961,29 @@ static unsigned long num_sbi_ext_regs(struct kvm_vcpu *vcpu)return copy_sbi_ext_reg_indices (vcpu,NULL ); } +static inline unsigned long num_sbi_regs (struct kvm_vcpu *vcpu) +{ +return 0 ; +} + +static int copy_sbi_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices) +{ +int n =num_sbi_regs (vcpu); + +for (int i =0 ; i < n; i++) { +u64 reg =KVM_REG_RISCV |KVM_REG_SIZE_U64 | +KVM_REG_RISCV_SBI | i; + +if (uindices) { +if (put_user (reg, uindices)) +return -EFAULT ; + uindices++; + } + } + +return n; +} +static inline unsigned long num_vector_regs (const struct kvm_vcpu *vcpu) {if (!riscv_isa_extension_available (vcpu->arch.isa, v))@@ -1028,6 +1051,7 @@ unsigned long kvm_riscv_vcpu_num_regs(struct kvm_vcpu *vcpu) res +=num_vector_regs (vcpu); res +=num_isa_ext_regs (vcpu); res +=num_sbi_ext_regs (vcpu); + res +=num_sbi_regs (vcpu);return res; }@@ -1083,6 +1107,12 @@ int kvm_riscv_vcpu_copy_reg_indices(struct kvm_vcpu *vcpu, ret =copy_sbi_ext_reg_indices (vcpu, uindices);if (ret <0 )return ret; + uindices += ret; + + ret =copy_sbi_reg_indices (vcpu, uindices); +if (ret <0 ) +return ret; + uindices += ret;return 0 ; }@@ -1105,12 +1135,14 @@ int kvm_riscv_vcpu_set_reg(struct kvm_vcpu *vcpu,case KVM_REG_RISCV_FP_D :return kvm_riscv_vcpu_set_reg_fp (vcpu, reg,KVM_REG_RISCV_FP_D ); +case KVM_REG_RISCV_VECTOR : +return kvm_riscv_vcpu_set_reg_vector (vcpu, reg);case KVM_REG_RISCV_ISA_EXT :return kvm_riscv_vcpu_set_reg_isa_ext (vcpu, reg);case KVM_REG_RISCV_SBI_EXT :return kvm_riscv_vcpu_set_reg_sbi_ext (vcpu, reg); -case KVM_REG_RISCV_VECTOR : -return kvm_riscv_vcpu_set_reg_vector (vcpu, reg); +case KVM_REG_RISCV_SBI : +return kvm_riscv_vcpu_set_reg_sbi (vcpu, reg);default :break ; }@@ -1136,12 +1168,14 @@ int kvm_riscv_vcpu_get_reg(struct kvm_vcpu *vcpu,case KVM_REG_RISCV_FP_D :return kvm_riscv_vcpu_get_reg_fp (vcpu, reg,KVM_REG_RISCV_FP_D ); +case KVM_REG_RISCV_VECTOR : +return kvm_riscv_vcpu_get_reg_vector (vcpu, reg);case KVM_REG_RISCV_ISA_EXT :return kvm_riscv_vcpu_get_reg_isa_ext (vcpu, reg);case KVM_REG_RISCV_SBI_EXT :return kvm_riscv_vcpu_get_reg_sbi_ext (vcpu, reg); -case KVM_REG_RISCV_VECTOR : -return kvm_riscv_vcpu_get_reg_vector (vcpu, reg); +case KVM_REG_RISCV_SBI : +return kvm_riscv_vcpu_get_reg_sbi (vcpu, reg);default :break ; }arch/riscv/kvm/vcpu_sbi.c +58
-1@@ -325,6 +325,64 @@ int kvm_riscv_vcpu_get_reg_sbi_ext(struct kvm_vcpu *vcpu,return 0 ; } +int kvm_riscv_vcpu_set_reg_sbi (struct kvm_vcpu *vcpu, +const struct kvm_one_reg *reg) +{ +unsigned long __user *uaddr = + (unsigned long __user *)(unsigned long )reg->addr; +unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | +KVM_REG_SIZE_MASK | +KVM_REG_RISCV_SBI ); +unsigned long reg_subtype, reg_val; + +if (KVM_REG_SIZE (reg->id) !=sizeof (unsigned long )) +return -EINVAL ; + +if (copy_from_user (®_val, uaddr,KVM_REG_SIZE (reg->id))) +return -EFAULT ; + + reg_subtype = reg_num &KVM_REG_RISCV_SUBTYPE_MASK ; + reg_num &= ~KVM_REG_RISCV_SUBTYPE_MASK ; + +switch (reg_subtype) { +default : +return -EINVAL ; + } + +return 0 ; +} + +int kvm_riscv_vcpu_get_reg_sbi (struct kvm_vcpu *vcpu, +const struct kvm_one_reg *reg) +{ +unsigned long __user *uaddr = + (unsigned long __user *)(unsigned long )reg->addr; +unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | +KVM_REG_SIZE_MASK | +KVM_REG_RISCV_SBI ); +unsigned long reg_subtype, reg_val; +int ret; + +if (KVM_REG_SIZE (reg->id) !=sizeof (unsigned long )) +return -EINVAL ; + + reg_subtype = reg_num &KVM_REG_RISCV_SUBTYPE_MASK ; + reg_num &= ~KVM_REG_RISCV_SUBTYPE_MASK ; + +switch (reg_subtype) { +default : +return -EINVAL ; + } + +if (ret) +return ret; + +if (copy_to_user (uaddr, ®_val,KVM_REG_SIZE (reg->id))) +return -EFAULT ; + +return 0 ; +} +const struct kvm_vcpu_sbi_extension *kvm_vcpu_sbi_find_ext (struct kvm_vcpu *vcpu,unsigned long extid) { --Quoted 2 lines
2.43.0
Other than that, lgtm.
Quoted 1 line
Reviewed-by Atish Patra <[email protected]>
Thanks,
drew
PATCH v3 7/13 RISC-V: KVM: Add support for SBI extension registers
In reply to Atish PatraQuoted 50 lines · On Wed, Dec 20, 2023 at 1:28?AM Atish Patra <[email protected]> wrote:
Quoted 36 lines · On Sun, Dec 17, 2023 at 12:40?PM Andrew Jones <[email protected]> wrote:
Some SBI extensions have state that needs to be saved / restored when migrating the VM. Provide a get/set-one-reg register type for SBI extension registers. Each SBI extension that uses this type will have its own subtype. There are currently no subtypes defined. The next patch introduces the first one.
Reviewed-by Anup Patel <[email protected]>Signed-off-by Andrew Jones <[email protected]>--- arch/riscv/include/asm/kvm_vcpu_sbi.h | 4 ++ arch/riscv/include/uapi/asm/kvm.h | 3 ++ arch/riscv/kvm/vcpu_onereg.c | 42 +++++++++++++++++-- arch/riscv/kvm/vcpu_sbi.c | 58 +++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 4 deletions(-)arch/riscv/include/asm/kvm_vcpu_sbi.h +4
-0@@ -60,6 +60,10 @@ int kvm_riscv_vcpu_set_reg_sbi_ext(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);int kvm_riscv_vcpu_get_reg_sbi_ext (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg); +int kvm_riscv_vcpu_set_reg_sbi (struct kvm_vcpu *vcpu, +const struct kvm_one_reg *reg); +int kvm_riscv_vcpu_get_reg_sbi (struct kvm_vcpu *vcpu, +const struct kvm_one_reg *reg);const struct kvm_vcpu_sbi_extension *kvm_vcpu_sbi_find_ext (struct kvm_vcpu *vcpu,unsigned long extid);bool riscv_vcpu_supports_sbi_ext (struct kvm_vcpu *vcpu,int idx);arch/riscv/include/uapi/asm/kvm.h +0
-0arch/riscv/include/uapi/asm/kvm.h enum KVM_RISCV_SBI_EXT_ID { · line 247
Quoted 6 lines
arch/riscv/include/uapi/asm/kvm.h +3
-0@@ -242,6 +242,9 @@ enum KVM_RISCV_SBI_EXT_ID {#define KVM_REG_RISCV_VECTOR_REG (n) \ ((n) + sizeof(struct __riscv_v_ext_state) / sizeof(unsigned long)) +/* Registers for specific SBI extensions are mapped as type 10 */ +#define KVM_REG_RISCV_SBI (0x0a << KVM_REG_RISCV_TYPE_SHIFT) +nit comment:
KVM_REG_RISCV_SBIlooks bit odd when we already haveKVM_REG_RISCV_SBI_EXTfor
extension enabling/disabling.
Quoted 3 lines
How about renaming this to
KVM_REG_RISCV_SBI_EXT_STATEor something similar indicate that this
for a specific extension state ?
How about KVM_REG_RISCV_SBI_STATE ?
Regards,
Anup
Quoted 179 lines
Quoted 164 lines
arch/riscv/include/uapi/asm/kvm.h +0
-0/* Device Control API: RISC-V AIA */ #define KVM_DEV_RISCV_APLIC_ALIGN 0x1000#define KVM_DEV_RISCV_APLIC_SIZE 0x4000arch/riscv/kvm/vcpu_onereg.c +38
-4@@ -961,6 +961,29 @@ static unsigned long num_sbi_ext_regs(struct kvm_vcpu *vcpu)return copy_sbi_ext_reg_indices (vcpu,NULL ); } +static inline unsigned long num_sbi_regs (struct kvm_vcpu *vcpu) +{ +return 0 ; +} + +static int copy_sbi_reg_indices (struct kvm_vcpu *vcpu,u64 __user *uindices) +{ +int n =num_sbi_regs (vcpu); + +for (int i =0 ; i < n; i++) { +u64 reg =KVM_REG_RISCV |KVM_REG_SIZE_U64 | +KVM_REG_RISCV_SBI | i; + +if (uindices) { +if (put_user (reg, uindices)) +return -EFAULT ; + uindices++; + } + } + +return n; +} +static inline unsigned long num_vector_regs (const struct kvm_vcpu *vcpu) {if (!riscv_isa_extension_available (vcpu->arch.isa, v))@@ -1028,6 +1051,7 @@ unsigned long kvm_riscv_vcpu_num_regs(struct kvm_vcpu *vcpu) res +=num_vector_regs (vcpu); res +=num_isa_ext_regs (vcpu); res +=num_sbi_ext_regs (vcpu); + res +=num_sbi_regs (vcpu);return res; }@@ -1083,6 +1107,12 @@ int kvm_riscv_vcpu_copy_reg_indices(struct kvm_vcpu *vcpu, ret =copy_sbi_ext_reg_indices (vcpu, uindices);if (ret <0 )return ret; + uindices += ret; + + ret =copy_sbi_reg_indices (vcpu, uindices); +if (ret <0 ) +return ret; + uindices += ret;return 0 ; }@@ -1105,12 +1135,14 @@ int kvm_riscv_vcpu_set_reg(struct kvm_vcpu *vcpu,case KVM_REG_RISCV_FP_D :return kvm_riscv_vcpu_set_reg_fp (vcpu, reg,KVM_REG_RISCV_FP_D ); +case KVM_REG_RISCV_VECTOR : +return kvm_riscv_vcpu_set_reg_vector (vcpu, reg);case KVM_REG_RISCV_ISA_EXT :return kvm_riscv_vcpu_set_reg_isa_ext (vcpu, reg);case KVM_REG_RISCV_SBI_EXT :return kvm_riscv_vcpu_set_reg_sbi_ext (vcpu, reg); -case KVM_REG_RISCV_VECTOR : -return kvm_riscv_vcpu_set_reg_vector (vcpu, reg); +case KVM_REG_RISCV_SBI : +return kvm_riscv_vcpu_set_reg_sbi (vcpu, reg);default :break ; }@@ -1136,12 +1168,14 @@ int kvm_riscv_vcpu_get_reg(struct kvm_vcpu *vcpu,case KVM_REG_RISCV_FP_D :return kvm_riscv_vcpu_get_reg_fp (vcpu, reg,KVM_REG_RISCV_FP_D ); +case KVM_REG_RISCV_VECTOR : +return kvm_riscv_vcpu_get_reg_vector (vcpu, reg);case KVM_REG_RISCV_ISA_EXT :return kvm_riscv_vcpu_get_reg_isa_ext (vcpu, reg);case KVM_REG_RISCV_SBI_EXT :return kvm_riscv_vcpu_get_reg_sbi_ext (vcpu, reg); -case KVM_REG_RISCV_VECTOR : -return kvm_riscv_vcpu_get_reg_vector (vcpu, reg); +case KVM_REG_RISCV_SBI : +return kvm_riscv_vcpu_get_reg_sbi (vcpu, reg);default :break ; }arch/riscv/kvm/vcpu_sbi.c +58
-1@@ -325,6 +325,64 @@ int kvm_riscv_vcpu_get_reg_sbi_ext(struct kvm_vcpu *vcpu,return 0 ; } +int kvm_riscv_vcpu_set_reg_sbi (struct kvm_vcpu *vcpu, +const struct kvm_one_reg *reg) +{ +unsigned long __user *uaddr = + (unsigned long __user *)(unsigned long )reg->addr; +unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | +KVM_REG_SIZE_MASK | +KVM_REG_RISCV_SBI ); +unsigned long reg_subtype, reg_val; + +if (KVM_REG_SIZE (reg->id) !=sizeof (unsigned long )) +return -EINVAL ; + +if (copy_from_user (®_val, uaddr,KVM_REG_SIZE (reg->id))) +return -EFAULT ; + + reg_subtype = reg_num &KVM_REG_RISCV_SUBTYPE_MASK ; + reg_num &= ~KVM_REG_RISCV_SUBTYPE_MASK ; + +switch (reg_subtype) { +default : +return -EINVAL ; + } + +return 0 ; +} + +int kvm_riscv_vcpu_get_reg_sbi (struct kvm_vcpu *vcpu, +const struct kvm_one_reg *reg) +{ +unsigned long __user *uaddr = + (unsigned long __user *)(unsigned long )reg->addr; +unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | +KVM_REG_SIZE_MASK | +KVM_REG_RISCV_SBI ); +unsigned long reg_subtype, reg_val; +int ret; + +if (KVM_REG_SIZE (reg->id) !=sizeof (unsigned long )) +return -EINVAL ; + + reg_subtype = reg_num &KVM_REG_RISCV_SUBTYPE_MASK ; + reg_num &= ~KVM_REG_RISCV_SUBTYPE_MASK ; + +switch (reg_subtype) { +default : +return -EINVAL ; + } + +if (ret) +return ret; + +if (copy_to_user (uaddr, ®_val,KVM_REG_SIZE (reg->id))) +return -EFAULT ; + +return 0 ; +} +const struct kvm_vcpu_sbi_extension *kvm_vcpu_sbi_find_ext (struct kvm_vcpu *vcpu,unsigned long extid) { --Quoted 2 lines
2.43.0
Other than that, lgtm.
Reviewed-by Atish Patra <[email protected]>--
Regards,
Atish--
kvm-riscv mailing list
kvm-riscv at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv
PATCH v3 11/13 RISC-V: KVM: selftests: Add guest_sbi_probe_extension
In reply to Atish PatraQuoted 24 lines · Atish Patra
Quoted 23 lines · On Sun, Dec 17, 2023 at 12:40?PM Andrew Jones <[email protected]> wrote:
Add
guest_sbi_probe_extension(), allowing guest code to probe for SBI extensions. Asguest_sbi_probe_extension()needsSBI_ERR_NOT_SUPPORTED, take the opportunity to bring in all SBI error codes. We don't bring in all current extension IDs or base extension function IDs though, even though we need one of each, because we'd prefer to bring those in as necessary.Reviewed-by Anup Patel <[email protected]>Signed-off-by Andrew Jones <[email protected]>--- .../selftests/kvm/include/riscv/processor.h | 21 +++++++++++++++++++ .../selftests/kvm/lib/riscv/processor.c | 19 +++++++++++++++++ 2 files changed, 40 insertions(+)tools/testing/selftests/kvm/include/riscv/processor.h +0
-0@@ -108,6 +108,17 @@ static inline uint64_t __kvm_reg_id(uint64_t type, uint64_t subtype,#define SATP_ASID_SHIFT 44#define SATP_ASID_MASK _AC(0xFFFF, UL)
Quoted 13 lines
tools/testing/selftests/kvm/include/riscv/processor.h static inline uint64_t __kvm_reg_id(uint64_t type, uint64_t subtype, · line 121
Quoted 11 lines
tools/testing/selftests/kvm/include/riscv/processor.h +11
-0+/* SBI return error codes */ +#define SBI_SUCCESS 0 +#define SBI_ERR_FAILURE -1 +#define SBI_ERR_NOT_SUPPORTED -2 +#define SBI_ERR_INVALID_PARAM -3 +#define SBI_ERR_DENIED -4 +#define SBI_ERR_INVALID_ADDRESS -5 +#define SBI_ERR_ALREADY_AVAILABLE -6 +#define SBI_ERR_ALREADY_STARTED -7 +#define SBI_ERR_ALREADY_STOPPED -8 +Add
SBI_ERR_NO_SHMEMas well ?
I can add it now to selftests, but it won't get used (steal-time doesn't use it since it doesn't have any SBI functions that use an already set up shared memory region). It's also not yet in Linux, so KVM selftests would be "ahead" of Linux in this regard. And, while the naming shouldn't be controversial, I suppose we should commit the name to Linux first to be sure we port it correctly here. So let's wait until we have a KVM selftests user, which means it'll also be in Linux.
Quoted 60 lines
Quoted 54 lines
tools/testing/selftests/kvm/include/riscv/processor.h +10
-0#define SBI_EXT_EXPERIMENTAL_START 0x08000000#define SBI_EXT_EXPERIMENTAL_END 0x08FFFFFF@@ -115,6 +126,14 @@ static inline uint64_t __kvm_reg_id(uint64_t type, uint64_t subtype,#define KVM_RISCV_SELFTESTS_SBI_UCALL 0#define KVM_RISCV_SELFTESTS_SBI_UNEXP 1 +enum sbi_ext_id { +SBI_EXT_BASE =0x10 , +}; + +enum sbi_ext_base_fid { +SBI_EXT_BASE_PROBE_EXT =3 , +}; +struct sbiret {long error;long value;@@ -125,4 +144,6 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0, unsigned long arg3, unsigned long arg4, unsigned long arg5); +bool guest_sbi_probe_extension (int extid,long *out_val); +#endif /* SELFTEST_KVM_PROCESSOR_H */ tools/testing/selftests/kvm/lib/riscv/processor.c +19
-1@@ -393,3 +393,22 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,return ret; } + +bool guest_sbi_probe_extension (int extid,long *out_val) +{ +struct sbiret ret; + + ret =sbi_ecall (SBI_EXT_BASE ,SBI_EXT_BASE_PROBE_EXT , extid, +0 ,0 ,0 ,0 ,0 ); + +__GUEST_ASSERT (!ret.error || ret.error ==SBI_ERR_NOT_SUPPORTED , +"ret.error=%ld, ret.value=%ld\n" , ret.error, ret.value); + +if (ret.error ==SBI_ERR_NOT_SUPPORTED ) +return false; + +if (out_val) + *out_val = ret.value; + +return true; +} --Quoted 2 lines
2.43.0
Apart from that, LGTM.
Quoted 1 line
Reviewed-by Atish Patra <[email protected]>
Thanks,
drew
Quoted 4 lines
Signature · 3 lines
-- Regards, Atish
PATCH v3 7/13 RISC-V: KVM: Add support for SBI extension registers
In reply to Anup PatelQuoted 51 lines · Anup Patel
Quoted 50 lines · On Wed, Dec 20, 2023 at 1:28?AM Atish Patra <[email protected]> wrote:
Quoted 36 lines · On Sun, Dec 17, 2023 at 12:40?PM Andrew Jones <[email protected]> wrote:
Some SBI extensions have state that needs to be saved / restored when migrating the VM. Provide a get/set-one-reg register type for SBI extension registers. Each SBI extension that uses this type will have its own subtype. There are currently no subtypes defined. The next patch introduces the first one.
Reviewed-by Anup Patel <[email protected]>Signed-off-by Andrew Jones <[email protected]>--- arch/riscv/include/asm/kvm_vcpu_sbi.h | 4 ++ arch/riscv/include/uapi/asm/kvm.h | 3 ++ arch/riscv/kvm/vcpu_onereg.c | 42 +++++++++++++++++-- arch/riscv/kvm/vcpu_sbi.c | 58 +++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 4 deletions(-)arch/riscv/include/asm/kvm_vcpu_sbi.h +4
-0@@ -60,6 +60,10 @@ int kvm_riscv_vcpu_set_reg_sbi_ext(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg);int kvm_riscv_vcpu_get_reg_sbi_ext (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg); +int kvm_riscv_vcpu_set_reg_sbi (struct kvm_vcpu *vcpu, +const struct kvm_one_reg *reg); +int kvm_riscv_vcpu_get_reg_sbi (struct kvm_vcpu *vcpu, +const struct kvm_one_reg *reg);const struct kvm_vcpu_sbi_extension *kvm_vcpu_sbi_find_ext (struct kvm_vcpu *vcpu,unsigned long extid);bool riscv_vcpu_supports_sbi_ext (struct kvm_vcpu *vcpu,int idx);arch/riscv/include/uapi/asm/kvm.h +0
-0arch/riscv/include/uapi/asm/kvm.h enum KVM_RISCV_SBI_EXT_ID { · line 247
Quoted 6 lines
arch/riscv/include/uapi/asm/kvm.h +3
-0@@ -242,6 +242,9 @@ enum KVM_RISCV_SBI_EXT_ID {#define KVM_REG_RISCV_VECTOR_REG (n) \ ((n) + sizeof(struct __riscv_v_ext_state) / sizeof(unsigned long)) +/* Registers for specific SBI extensions are mapped as type 10 */ +#define KVM_REG_RISCV_SBI (0x0a << KVM_REG_RISCV_TYPE_SHIFT) +nit comment:
KVM_REG_RISCV_SBIlooks bit odd when we already haveKVM_REG_RISCV_SBI_EXTfor
extension enabling/disabling.
Quoted 5 lines
Quoted 3 lines
How about renaming this to
KVM_REG_RISCV_SBI_EXT_STATEor something similar indicate that this
for a specific extension state ?How about
KVM_REG_RISCV_SBI_STATE?
Also works for me.
Thanks,
drew
Anup Patel
maintainerPATCH v3 3/13 RISC-V: paravirt: Implement steal-time support
In reply to Andrew JonesQuoted 7 lines · On Mon, Dec 18, 2023 at 2:10?AM Andrew Jones <[email protected]> wrote:
When the SBI STA extension exists we can use it to implement paravirt steal-time support. Fill in the empty pv-time functions with an SBI STA implementation and add the
Kconfigknobs allowing it to be enabled.Signed-off-by Andrew Jones <[email protected]>
LGTM.
Regards,
Anup
Quoted 140 lines
--- arch/riscv/Kconfig | 19 +++++++++++ arch/riscv/kernel/paravirt.c | 63 ++++++++++++++++++++++++++++++++++-- 2 files changed, 79 insertions(+), 3 deletions(-)arch/riscv/Kconfig +19
-0@@ -724,6 +724,25 @@ config COMPAT If you want to execute 32-bit userspace applications, say Y. +config PARAVIRT + bool "Enable paravirtualization code" + depends on RISCV_SBI + help + This changes the kernel so it can modify itself when it is run + under a hypervisor, potentially improving performance significantly + over full virtualization. + +config PARAVIRT_TIME_ACCOUNTING + bool "Paravirtual steal time accounting" + depends on PARAVIRT + help + Select this option to enable fine granularity task steal time + accounting. Time spent executing other tasks in parallel with + the current vCPU is discounted from the vCPU power. To account for + that, there can be a small performance impact. + + If in doubt, say N here. + config RELOCATABLE bool "Build a relocatable kernel" depends on MMU && 64BIT && !XIP_KERNELarch/riscv/kernel/paravirt.c +60
-4@@ -6,12 +6,21 @@#define pr_fmt (fmt) "riscv-pv: " fmt#include <linux/cpuhotplug.h> +#include <linux/compiler.h> +#include <linux/errno.h> #include <linux/init.h> #include <linux/jump_label.h> +#include <linux/kconfig.h> +#include <linux/kernel.h> +#include <linux/percpu-defs.h> #include <linux/printk.h> #include <linux/static_call.h> #include <linux/types.h> +#include <asm/barrier.h> +#include <asm/page.h> +#include <asm/sbi.h> +struct static_key paravirt_steal_enabled;struct static_key paravirt_steal_rq_enabled;@@ -31,24 +40,72 @@ static int __init parse_no_stealacc(char *arg)early_param ("no-steal-acc" , parse_no_stealacc); +DEFINE_PER_CPU(struct sbi_sta_struct, steal_time) __aligned(64); +static bool __init has_pv_steal_clock (void ) { +if (sbi_spec_version >=sbi_mk_version (2 ,0 ) && +sbi_probe_extension (SBI_EXT_STA ) >0 ) { +pr_info ("SBI STA extension detected\n" ); +return true; + } +return false; } -static int pv_time_cpu_online (unsigned int cpu) +static int sbi_sta_steal_time_set_shmem (unsigned long lo,unsigned long hi, +unsigned long flags) { +struct sbiret ret; + + ret =sbi_ecall (SBI_EXT_STA ,SBI_EXT_STA_STEAL_TIME_SET_SHMEM , + lo, hi, flags,0 ,0 ,0 ); +if (ret.error) { +if (lo ==SBI_STA_SHMEM_DISABLE && hi ==SBI_STA_SHMEM_DISABLE ) +pr_warn ("Failed to disable steal-time shmem" ); +else +pr_warn ("Failed to set steal-time shmem" ); +return sbi_err_map_linux_errno (ret.error); + } +return 0 ; } +static int pv_time_cpu_online (unsigned int cpu) +{ +struct sbi_sta_struct *st =this_cpu_ptr (&steal_time); +phys_addr_t pa =__pa (st); +unsigned long lo = (unsigned long )pa; +unsigned long hi =IS_ENABLED (CONFIG_32BIT ) ?upper_32_bits ((u64 )pa) :0 ; + +return sbi_sta_steal_time_set_shmem (lo, hi,0 ); +} +static int pv_time_cpu_down_prepare (unsigned int cpu) { -return 0 ; +return sbi_sta_steal_time_set_shmem (SBI_STA_SHMEM_DISABLE , +SBI_STA_SHMEM_DISABLE ,0 ); }static u64 pv_time_steal_clock (int cpu) { -return 0 ; +struct sbi_sta_struct *st =per_cpu_ptr (&steal_time, cpu); +u32 sequence; +u64 steal; + +/* +* Check the sequence field before and after reading the steal +* field. Repeat the read if it is different or odd. +*/ +do { + sequence =READ_ONCE (st->sequence); +virt_rmb (); + steal =READ_ONCE (st->steal); +virt_rmb (); + }while ((le32_to_cpu (sequence) &1 ) || + sequence !=READ_ONCE (st->sequence)); + +return le64_to_cpu (steal); }int __init pv_time_init (void ) --2.43.0