arm64: Handle PSCI calls in userspace
Subsystems (9)
cover arm64: Handle PSCI calls in userspace
The 6.4 kernel picks up support for a generalized SMCCC filter, allowing userspace to select hypercall ranges that should be forwarded to userspace. This is a shameless attempt of making future SMCCC interfaces the responsibility of userspace :)
As a starting point, let's move PSCI up into userspace. KVM already leans on userspace for handling calls that have a system-wide effect.
Tested on linux-next with a 64 vCPU VM. Additionally, I took a stab at running kvm-unit-test's psci test, which passes.
Apologies for some of the changelogs being a bit short. It's Friday, and I'm lazy.
Oliver Upton (21):
update_headers: Use a list for arch-generic headers
update_headers: Add missing entries to list of headers to copy Copy 64-bit alignment attrtibutes from Linux 6.4-rc1 Update headers with Linux 6.4-rc1
Import arm-smccc.h from Linux 6.4-rc1
aarch64: Copy cputype.h from Linux 6.4-rc1
arm: Stash kvm_vcpu_init for later use
arm: Add support for resetting a vCPU
arm: Use KVM_SET_MP_STATE ioctl to power off non-boot vCPUs
aarch64: Expose ARM64_CORE_REG() for general use
arm: Generalize execution state specific VM initialization Add helpers to pause the VM from vCPU thread
aarch64: Add support for finding vCPU for given MPIDR
aarch64: Add skeleton implementation for PSCI
aarch64: psci: Implement CPU_SUSPEND
aarch64: psci: Implement CPU_OFF
aarch64: psci: Implement CPU_ON
aarch64: psci: Implement AFFINITY_INFO
aarch64: psci: Implement MIGRATE_INFO_TYPE
aarch64: psci: Implement SYSTEM_{OFF,RESET}
aarch64: smccc: Start sending PSCI to userspace
Makefile | 4 +- arm/aarch32/include/kvm/kvm-arch.h | 2 +- arm/aarch32/kvm-cpu.c | 5 + arm/aarch64/include/asm/cputype.h | 186 +++++ arm/aarch64/include/asm/kvm.h | 38 + arm/aarch64/include/asm/smccc.h | 65 ++ arm/aarch64/include/kvm/kvm-arch.h | 2 +- arm/aarch64/include/kvm/kvm-config-arch.h | 6 +- arm/aarch64/include/kvm/kvm-cpu-arch.h | 28 +- arm/aarch64/kvm-cpu.c | 48 +- arm/aarch64/kvm.c | 25 +- arm/aarch64/psci.c | 206 +++++ arm/aarch64/smccc.c | 82 ++ arm/include/arm-common/kvm-arch.h | 2 + arm/include/arm-common/kvm-config-arch.h | 1 + arm/include/arm-common/kvm-cpu-arch.h | 4 +- arm/kvm-cpu.c | 35 +- arm/kvm.c | 2 +- include/kvm/kvm-cpu.h | 3 + include/linux/arm-smccc.h | 240 ++++++ include/linux/kvm.h | 55 +- include/linux/psci.h | 47 ++ include/linux/types.h | 13 + include/linux/vfio.h | 920 +++++++++++++++++++++- include/linux/vhost.h | 186 ++--- include/linux/virtio_blk.h | 105 +++ include/linux/virtio_net.h | 4 + kvm-cpu.c | 15 + riscv/include/asm/kvm.h | 3 + util/update_headers.sh | 25 +- x86/include/asm/kvm.h | 50 +- 31 files changed, 2225 insertions(+), 182 deletions(-) create mode 100644 arm/aarch64/include/asm/cputype.h create mode 100644 arm/aarch64/include/asm/smccc.h create mode 100644 arm/aarch64/psci.c create mode 100644 arm/aarch64/smccc.c create mode 100644 include/linux/arm-smccc.h
base-commit: 77b108c6a6f1c66fb7f60a80d17596bb80bda8ad
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
PATCH 1/21 update_headers: Use a list for arch-generic headers
Until now, all of the virtio header names are stuffed in a list and iteratively copied from the kernel directory. Repurpose this as a list of arch-generic headers, adding kvm.h to the bunch.
While at it, spread out the definition to have a single element per line, making it easier to insert elements alphabetically in the future.
--- util/update_headers.sh | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-)
util/update_headers.sh +15 -7
@@ -9,10 +9,20 @@ set -ue -VIRTIO_LIST="virtio_9p.h virtio_balloon.h virtio_blk.h virtio_config.h \ - virtio_console.h virtio_ids.h virtio_mmio.h virtio_net.h \ - virtio_pci.h virtio_ring.h virtio_rng.h virtio_scsi.h \ - virtio_vsock.h" +GENERIC_LIST="kvm.h \ + virtio_9p.h \ + virtio_balloon.h \ + virtio_blk.h \ + virtio_config.h \ + virtio_console.h \ + virtio_ids.h \ + virtio_mmio.h \ + virtio_net.h \ + virtio_pci.h \ + virtio_ring.h \ + virtio_rng.h \ + virtio_scsi.h \ + virtio_vsock.h" if [ "$#" -ge 1 ] then
@@ -28,9 +38,7 @@ then exit 1 fi -cp -- "$LINUX_ROOT/include/uapi/linux/kvm.h" include/linux - -for header in $VIRTIO_LIST +for header in $GENERIC_LIST do cp -- "$LINUX_ROOT/include/uapi/linux/$header" include/linux done
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
PATCH 2/21 update_headers: Add missing entries to list of headers to copy
In reply to Oliver UptonThere are a few headers in kvmtool that are not handled by the updater script. Add them to the list of headers to update.
--- util/update_headers.sh | 3 +++ 1 file changed, 3 insertions(+)
util/update_headers.sh +3 -0
@@ -10,6 +10,9 @@ set -ue GENERIC_LIST="kvm.h \ + psci.h \ + vfio.h \ + vhost.h \ virtio_9p.h \ virtio_balloon.h \ virtio_blk.h \
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
PATCH 3/21 Copy 64-bit alignment attrtibutes from Linux 6.4-rc1
In reply to Oliver UptonAn update to vfio.h requires these macros.
--- include/linux/types.h | 13 +++++++++++++ 1 file changed, 13 insertions(+)
include/linux/types.h +13 -0
@@ -36,6 +36,19 @@ typedef __u32 __bitwise __be32;typedef __u64 __bitwise __le64 ;typedef __u64 __bitwise __be64 ; +/* +* aligned_u64 should be used in defining kernel<->userspace ABIs to avoid +* common 32/64-bit compat problems. +* 64-bit values align to 4-byte boundaries on x86_32 (and possibly other +* architectures) and to 8-byte boundaries on 64-bit architectures. The new +* aligned_64 type enforces 8-byte alignment so that structs containing +* aligned_64 values have the same alignment on 32-bit and 64-bit architectures. +* No conversions are necessary between 32-bit user-space and a 64-bit kernel. +*/ +#define __aligned_u64 __u64 __attribute__((aligned(8))) +#define __aligned_be64 __be64 __attribute__((aligned(8))) +#define __aligned_le64 __le64 __attribute__((aligned(8))) +struct list_head {struct list_head *next, *prev; };
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
PATCH 4/21 Update headers with Linux 6.4-rc1
In reply to Oliver Upton--- arm/aarch64/include/asm/kvm.h | 38 ++ include/linux/kvm.h | 55 +- include/linux/psci.h | 47 ++ include/linux/vfio.h | 920 +++++++++++++++++++++++++++++++++- include/linux/vhost.h | 186 +++---- include/linux/virtio_blk.h | 105 ++++ include/linux/virtio_net.h | 4 + riscv/include/asm/kvm.h | 3 + x86/include/asm/kvm.h | 50 +- 9 files changed, 1270 insertions(+), 138 deletions(-)
arm/aarch64/include/asm/kvm.h +38 -0
@@ -43,6 +43,7 @@#define __KVM_HAVE_VCPU_EVENTS#define KVM_COALESCED_MMIO_PAGE_OFFSET 1 +#define KVM_DIRTY_LOG_PAGE_OFFSET 64#define KVM_REG_SIZE (id) \ (1U << (((id) & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT))
@@ -108,6 +109,7 @@ struct kvm_regs {#define KVM_ARM_VCPU_SVE 4/* enable SVE for this CPU */ #define KVM_ARM_VCPU_PTRAUTH_ADDRESS 5/* VCPU uses address authentication */ #define KVM_ARM_VCPU_PTRAUTH_GENERIC 6/* VCPU uses generic authentication */ +#define KVM_ARM_VCPU_HAS_EL2 7/* Support nested virtualization */ struct kvm_vcpu_init {__u32 target;
@@ -196,6 +198,15 @@ struct kvm_arm_copy_mte_tags {__u64 reserved[2 ]; }; +/* +* Counter/Timer offset structure. Describe the virtual/physical offset. +* To be used with KVM_ARM_SET_COUNTER_OFFSET. +*/ +struct kvm_arm_counter_offset { +__u64 counter_offset; +__u64 reserved; +}; +#define KVM_ARM_TAGS_TO_GUEST 0#define KVM_ARM_TAGS_FROM_GUEST 1
@@ -370,6 +381,10 @@ enum {#endif }; +/* Device Control API on vm fd */ +#define KVM_ARM_VM_SMCCC_CTRL 0 +#define KVM_ARM_VM_SMCCC_FILTER 0 +/* Device Control API: ARM VGIC */ #define KVM_DEV_ARM_VGIC_GRP_ADDR 0#define KVM_DEV_ARM_VGIC_GRP_DIST_REGS 1
@@ -409,6 +424,8 @@ enum {#define KVM_ARM_VCPU_TIMER_CTRL 1#define KVM_ARM_VCPU_TIMER_IRQ_VTIMER 0#define KVM_ARM_VCPU_TIMER_IRQ_PTIMER 1 +#define KVM_ARM_VCPU_TIMER_IRQ_HVTIMER 2 +#define KVM_ARM_VCPU_TIMER_IRQ_HPTIMER 3#define KVM_ARM_VCPU_PVTIME_CTRL 2#define KVM_ARM_VCPU_PVTIME_IPA 0
@@ -467,6 +484,27 @@ enum {/* run->fail_entry.hardware_entry_failure_reason codes. */ #define KVM_EXIT_FAIL_ENTRY_CPU_UNSUPPORTED (1ULL << 0) +enum kvm_smccc_filter_action { +KVM_SMCCC_FILTER_HANDLE =0 , +KVM_SMCCC_FILTER_DENY , +KVM_SMCCC_FILTER_FWD_TO_USER , + +#ifdef __KERNEL__ +NR_SMCCC_FILTER_ACTIONS +#endif +}; + +struct kvm_smccc_filter { +__u32 base; +__u32 nr_functions; +__u8 action; +__u8 pad[15 ]; +}; + +/* arm64-specific KVM_EXIT_HYPERCALL flags */ +#define KVM_HYPERCALL_EXIT_SMC (1U << 0) +#define KVM_HYPERCALL_EXIT_16BIT (1U << 1) +#endif #endif /* __ARM_KVM_H__ */
include/linux/kvm.h +36 -19
158 lines not on this page · raw message
include/linux/psci.h +47 -0
79 lines not on this page · raw message
include/linux/vfio.h +914 -6
1,023 lines not on this page · raw message
include/linux/vhost.h +83 -103
213 lines not on this page · raw message
include/linux/virtio_blk.h +105 -0
133 lines not on this page · raw message
include/linux/virtio_net.h +4 -0
@@ -57,6 +57,9 @@* Steering */ #define VIRTIO_NET_F_CTRL_MAC_ADDR 23/* Set MAC address */ #define VIRTIO_NET_F_NOTF_COAL 53/* Device supports notifications coalescing */ +#define VIRTIO_NET_F_GUEST_USO4 54/* Guest can handle USOv4 in. */ +#define VIRTIO_NET_F_GUEST_USO6 55/* Guest can handle USOv6 in. */ +#define VIRTIO_NET_F_HOST_USO 56/* Host can handle USO in. */ #define VIRTIO_NET_F_HASH_REPORT 57/* Supports hash report */ #define VIRTIO_NET_F_RSS 60/* Supports RSS RX steering */ #define VIRTIO_NET_F_RSC_EXT 61/* extended coalescing info */
@@ -130,6 +133,7 @@ struct virtio_net_hdr_v1 {#define VIRTIO_NET_HDR_GSO_TCPV4 1/* GSO frame, IPv4 TCP (TSO) */ #define VIRTIO_NET_HDR_GSO_UDP 3/* GSO frame, IPv4 UDP (UFO) */ #define VIRTIO_NET_HDR_GSO_TCPV6 4/* GSO frame, IPv6 TCP */ +#define VIRTIO_NET_HDR_GSO_UDP_L4 5/* GSO frame, IPv4& IPv6 UDP (USO) */ #define VIRTIO_NET_HDR_GSO_ECN 0x80/* TCP has ECN set */ __u8 gso_type;__virtio16 hdr_len;/* Ethernet + IP + tcp/udp hdrs */
riscv/include/asm/kvm.h +3 -0
@@ -49,6 +49,9 @@ struct kvm_sregs {struct kvm_riscv_config {unsigned long isa;unsigned long zicbom_block_size; +unsigned long mvendorid; +unsigned long marchid; +unsigned long mimpid; };/* CORE registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */
x86/include/asm/kvm.h +40 -10
90 lines not on this page · raw message
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
PATCH 5/21 Import arm-smccc.h from Linux 6.4-rc1
In reply to Oliver UptonCopy in the SMCCC definitions from the kernel, which will be used to implement SMCCC handling in userspace.
--- include/linux/arm-smccc.h | 240 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 240 insertions(+) create mode 100644 include/linux/arm-smccc.h
include/linux/arm-smccc.h new file +240 -0
@@ -0,0 +1,240 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (c) 2015, Linaro Limited +*/ +#ifndef __LINUX_ARM_SMCCC_H +#define __LINUX_ARM_SMCCC_H + +#include <linux/const.h> + +/* +* This file provides common defines for ARM SMC Calling Convention as +* specified in +* https://developer.arm.com/docs/den0028/latest +* +* This code is up-to-date with version DEN 0028 C +*/ + +#define ARM_SMCCC_STD_CALL _AC(0,U) +#define ARM_SMCCC_FAST_CALL _AC(1,U) +#define ARM_SMCCC_TYPE_SHIFT 31 + +#define ARM_SMCCC_SMC_32 0 +#define ARM_SMCCC_SMC_64 1 +#define ARM_SMCCC_CALL_CONV_SHIFT 30 + +#define ARM_SMCCC_OWNER_MASK 0x3F +#define ARM_SMCCC_OWNER_SHIFT 24 + +#define ARM_SMCCC_FUNC_MASK 0xFFFF + +#define ARM_SMCCC_IS_FAST_CALL (smc_val) \ + ((smc_val) & (ARM_SMCCC_FAST_CALL << ARM_SMCCC_TYPE_SHIFT)) +#define ARM_SMCCC_IS_64 (smc_val) \ + ((smc_val) & (ARM_SMCCC_SMC_64 << ARM_SMCCC_CALL_CONV_SHIFT)) +#define ARM_SMCCC_FUNC_NUM (smc_val) ((smc_val) & ARM_SMCCC_FUNC_MASK) +#define ARM_SMCCC_OWNER_NUM (smc_val) \ + (((smc_val) >> ARM_SMCCC_OWNER_SHIFT) & ARM_SMCCC_OWNER_MASK) + +#define ARM_SMCCC_CALL_VAL (type, calling_convention, owner, func_num) \ + (((type) << ARM_SMCCC_TYPE_SHIFT) | \ + ((calling_convention) << ARM_SMCCC_CALL_CONV_SHIFT) | \ + (((owner) & ARM_SMCCC_OWNER_MASK) << ARM_SMCCC_OWNER_SHIFT) | \ + ((func_num) & ARM_SMCCC_FUNC_MASK)) + +#define ARM_SMCCC_OWNER_ARCH 0 +#define ARM_SMCCC_OWNER_CPU 1 +#define ARM_SMCCC_OWNER_SIP 2 +#define ARM_SMCCC_OWNER_OEM 3 +#define ARM_SMCCC_OWNER_STANDARD 4 +#define ARM_SMCCC_OWNER_STANDARD_HYP 5 +#define ARM_SMCCC_OWNER_VENDOR_HYP 6 +#define ARM_SMCCC_OWNER_TRUSTED_APP 48 +#define ARM_SMCCC_OWNER_TRUSTED_APP_END 49 +#define ARM_SMCCC_OWNER_TRUSTED_OS 50 +#define ARM_SMCCC_OWNER_TRUSTED_OS_END 63 + +#define ARM_SMCCC_FUNC_QUERY_CALL_UID 0xff01 + +#define ARM_SMCCC_QUIRK_NONE 0 +#define ARM_SMCCC_QUIRK_QCOM_A6 1/* Save/restore register a6 */ + +#define ARM_SMCCC_VERSION_1_0 0x10000 +#define ARM_SMCCC_VERSION_1_1 0x10001 +#define ARM_SMCCC_VERSION_1_2 0x10002 +#define ARM_SMCCC_VERSION_1_3 0x10003 + +#define ARM_SMCCC_1_3_SVE_HINT 0x10000 + +#define ARM_SMCCC_VERSION_FUNC_ID \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + 0, 0) + +#define ARM_SMCCC_ARCH_FEATURES_FUNC_ID \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + 0, 1) + +#define ARM_SMCCC_ARCH_SOC_ID \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + 0, 2) + +#define ARM_SMCCC_ARCH_WORKAROUND_1 \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + 0, 0x8000) + +#define ARM_SMCCC_ARCH_WORKAROUND_2 \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + 0, 0x7fff) + +#define ARM_SMCCC_ARCH_WORKAROUND_3 \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + 0, 0x3fff) + +#define ARM_SMCCC_VENDOR_HYP_CALL_UID_FUNC_ID \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + ARM_SMCCC_OWNER_VENDOR_HYP, \ + ARM_SMCCC_FUNC_QUERY_CALL_UID) + +/* KVM UID value: 28b46fb6-2ec5-11e9-a9ca-4b564d003a74 */ +#define ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_0 0xb66fb428U +#define ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_1 0xe911c52eU +#define ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_2 0x564bcaa9U +#define ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_3 0x743a004dU + +/* KVM "vendor specific" services */ +#define ARM_SMCCC_KVM_FUNC_FEATURES 0 +#define ARM_SMCCC_KVM_FUNC_PTP 1 +#define ARM_SMCCC_KVM_FUNC_FEATURES_2 127 +#define ARM_SMCCC_KVM_NUM_FUNCS 128 + +#define ARM_SMCCC_VENDOR_HYP_KVM_FEATURES_FUNC_ID \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + ARM_SMCCC_OWNER_VENDOR_HYP, \ + ARM_SMCCC_KVM_FUNC_FEATURES) + +#define SMCCC_ARCH_WORKAROUND_RET_UNAFFECTED 1 + +/* +* ptp_kvm is a feature used for time sync between vm and host. +* ptp_kvm module in guest kernel will get service from host using +* this hypercall ID. +*/ +#define ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + ARM_SMCCC_OWNER_VENDOR_HYP, \ + ARM_SMCCC_KVM_FUNC_PTP) + +/* ptp_kvm counter type ID */ +#define KVM_PTP_VIRT_COUNTER 0 +#define KVM_PTP_PHYS_COUNTER 1 + +/* Paravirtualised time calls (defined by ARM DEN0057A) */ +#define ARM_SMCCC_HV_PV_TIME_FEATURES \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_64, \ + ARM_SMCCC_OWNER_STANDARD_HYP, \ + 0x20) + +#define ARM_SMCCC_HV_PV_TIME_ST \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_64, \ + ARM_SMCCC_OWNER_STANDARD_HYP, \ + 0x21) + +/* TRNG entropy source calls (defined by ARM DEN0098) */ +#define ARM_SMCCC_TRNG_VERSION \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + ARM_SMCCC_OWNER_STANDARD, \ + 0x50) + +#define ARM_SMCCC_TRNG_FEATURES \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + ARM_SMCCC_OWNER_STANDARD, \ + 0x51) + +#define ARM_SMCCC_TRNG_GET_UUID \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + ARM_SMCCC_OWNER_STANDARD, \ + 0x52) + +#define ARM_SMCCC_TRNG_RND32 \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + ARM_SMCCC_OWNER_STANDARD, \ + 0x53) + +#define ARM_SMCCC_TRNG_RND64 \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_64, \ + ARM_SMCCC_OWNER_STANDARD, \ + 0x53) + +/* +* Return codes defined in ARM DEN 0070A +* ARM DEN 0070A is now merged/consolidated into ARM DEN 0028 C +*/ +#define SMCCC_RET_SUCCESS 0 +#define SMCCC_RET_NOT_SUPPORTED -1 +#define SMCCC_RET_NOT_REQUIRED -2 +#define SMCCC_RET_INVALID_PARAMETER -3 + +#ifndef __ASSEMBLY__ + +#include <linux/types.h> + +enum arm_smccc_conduit { +SMCCC_CONDUIT_NONE , +SMCCC_CONDUIT_SMC , +SMCCC_CONDUIT_HVC , +}; + +/** +* struct arm_smccc_res - Result from SMC/HVC call +* @a0-a3 result values from registers 0 to 3 +*/ +struct arm_smccc_res { +unsigned long a0; +unsigned long a1; +unsigned long a2; +unsigned long a3; +}; + +/** +* struct arm_smccc_1_2_regs - Arguments for or Results from SMC/HVC call +* @a0-a17 argument values from registers 0 to 17 +*/ +struct arm_smccc_1_2_regs { +unsigned long a0; +unsigned long a1; +unsigned long a2; +unsigned long a3; +unsigned long a4; +unsigned long a5; +unsigned long a6; +unsigned long a7; +unsigned long a8; +unsigned long a9; +unsigned long a10; +unsigned long a11; +unsigned long a12; +unsigned long a13; +unsigned long a14; +unsigned long a15; +unsigned long a16; +unsigned long a17; +}; + +#endif /*__ASSEMBLY__*/ +#endif /*__LINUX_ARM_SMCCC_H*/
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
PATCH 6/21 aarch64: Copy cputype.h from Linux 6.4-rc1
In reply to Oliver Uptoncputype.h has some helpful definitions for working with mpidrs and affinity masks.
--- arm/aarch64/include/asm/cputype.h | 186 ++++++++++++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 arm/aarch64/include/asm/cputype.h
arm/aarch64/include/asm/cputype.h new file +186 -0
@@ -0,0 +1,186 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2012 ARM Ltd. +*/ +#ifndef __ASM_CPUTYPE_H +#define __ASM_CPUTYPE_H + +#define INVALID_HWID ULONG_MAX + +#define MPIDR_UP_BITMASK (0x1 << 30) +#define MPIDR_MT_BITMASK (0x1 << 24) +#define MPIDR_HWID_BITMASK UL(0xff00ffffff) + +#define MPIDR_LEVEL_BITS_SHIFT 3 +#define MPIDR_LEVEL_BITS (1 << MPIDR_LEVEL_BITS_SHIFT) +#define MPIDR_LEVEL_MASK ((1 << MPIDR_LEVEL_BITS) - 1) + +#define MPIDR_LEVEL_SHIFT (level) \ + (((1 << level) >> 1) << MPIDR_LEVEL_BITS_SHIFT) + +#define MPIDR_AFFINITY_LEVEL (mpidr, level) \ + ((mpidr >> MPIDR_LEVEL_SHIFT(level)) & MPIDR_LEVEL_MASK) + +#define MIDR_REVISION_MASK 0xf +#define MIDR_REVISION (midr) ((midr) & MIDR_REVISION_MASK) +#define MIDR_PARTNUM_SHIFT 4 +#define MIDR_PARTNUM_MASK (0xfff << MIDR_PARTNUM_SHIFT) +#define MIDR_PARTNUM (midr) \ + (((midr) & MIDR_PARTNUM_MASK) >> MIDR_PARTNUM_SHIFT) +#define MIDR_ARCHITECTURE_SHIFT 16 +#define MIDR_ARCHITECTURE_MASK (0xf << MIDR_ARCHITECTURE_SHIFT) +#define MIDR_ARCHITECTURE (midr) \ + (((midr) & MIDR_ARCHITECTURE_MASK) >> MIDR_ARCHITECTURE_SHIFT) +#define MIDR_VARIANT_SHIFT 20 +#define MIDR_VARIANT_MASK (0xf << MIDR_VARIANT_SHIFT) +#define MIDR_VARIANT (midr) \ + (((midr) & MIDR_VARIANT_MASK) >> MIDR_VARIANT_SHIFT) +#define MIDR_IMPLEMENTOR_SHIFT 24 +#define MIDR_IMPLEMENTOR_MASK (0xffU << MIDR_IMPLEMENTOR_SHIFT) +#define MIDR_IMPLEMENTOR (midr) \ + (((midr) & MIDR_IMPLEMENTOR_MASK) >> MIDR_IMPLEMENTOR_SHIFT) + +#define MIDR_CPU_MODEL (imp, partnum) \ + ((_AT(u32, imp) << MIDR_IMPLEMENTOR_SHIFT) | \ + (0xf << MIDR_ARCHITECTURE_SHIFT) | \ + ((partnum) << MIDR_PARTNUM_SHIFT)) + +#define MIDR_CPU_VAR_REV (var, rev) \ + (((var) << MIDR_VARIANT_SHIFT) | (rev)) + +#define MIDR_CPU_MODEL_MASK (MIDR_IMPLEMENTOR_MASK | MIDR_PARTNUM_MASK | \ + MIDR_ARCHITECTURE_MASK) + +#define ARM_CPU_IMP_ARM 0x41 +#define ARM_CPU_IMP_APM 0x50 +#define ARM_CPU_IMP_CAVIUM 0x43 +#define ARM_CPU_IMP_BRCM 0x42 +#define ARM_CPU_IMP_QCOM 0x51 +#define ARM_CPU_IMP_NVIDIA 0x4E +#define ARM_CPU_IMP_FUJITSU 0x46 +#define ARM_CPU_IMP_HISI 0x48 +#define ARM_CPU_IMP_APPLE 0x61 +#define ARM_CPU_IMP_AMPERE 0xC0 + +#define ARM_CPU_PART_AEM_V8 0xD0F +#define ARM_CPU_PART_FOUNDATION 0xD00 +#define ARM_CPU_PART_CORTEX_A57 0xD07 +#define ARM_CPU_PART_CORTEX_A72 0xD08 +#define ARM_CPU_PART_CORTEX_A53 0xD03 +#define ARM_CPU_PART_CORTEX_A73 0xD09 +#define ARM_CPU_PART_CORTEX_A75 0xD0A +#define ARM_CPU_PART_CORTEX_A35 0xD04 +#define ARM_CPU_PART_CORTEX_A55 0xD05 +#define ARM_CPU_PART_CORTEX_A76 0xD0B +#define ARM_CPU_PART_NEOVERSE_N1 0xD0C +#define ARM_CPU_PART_CORTEX_A77 0xD0D +#define ARM_CPU_PART_NEOVERSE_V1 0xD40 +#define ARM_CPU_PART_CORTEX_A78 0xD41 +#define ARM_CPU_PART_CORTEX_A78AE 0xD42 +#define ARM_CPU_PART_CORTEX_X1 0xD44 +#define ARM_CPU_PART_CORTEX_A510 0xD46 +#define ARM_CPU_PART_CORTEX_A710 0xD47 +#define ARM_CPU_PART_CORTEX_A715 0xD4D +#define ARM_CPU_PART_CORTEX_X2 0xD48 +#define ARM_CPU_PART_NEOVERSE_N2 0xD49 +#define ARM_CPU_PART_CORTEX_A78C 0xD4B + +#define APM_CPU_PART_POTENZA 0x000 + +#define CAVIUM_CPU_PART_THUNDERX 0x0A1 +#define CAVIUM_CPU_PART_THUNDERX_81XX 0x0A2 +#define CAVIUM_CPU_PART_THUNDERX_83XX 0x0A3 +#define CAVIUM_CPU_PART_THUNDERX2 0x0AF +/* OcteonTx2 series */ +#define CAVIUM_CPU_PART_OCTX2_98XX 0x0B1 +#define CAVIUM_CPU_PART_OCTX2_96XX 0x0B2 +#define CAVIUM_CPU_PART_OCTX2_95XX 0x0B3 +#define CAVIUM_CPU_PART_OCTX2_95XXN 0x0B4 +#define CAVIUM_CPU_PART_OCTX2_95XXMM 0x0B5 +#define CAVIUM_CPU_PART_OCTX2_95XXO 0x0B6 + +#define BRCM_CPU_PART_BRAHMA_B53 0x100 +#define BRCM_CPU_PART_VULCAN 0x516 + +#define QCOM_CPU_PART_FALKOR_V1 0x800 +#define QCOM_CPU_PART_FALKOR 0xC00 +#define QCOM_CPU_PART_KRYO 0x200 +#define QCOM_CPU_PART_KRYO_2XX_GOLD 0x800 +#define QCOM_CPU_PART_KRYO_2XX_SILVER 0x801 +#define QCOM_CPU_PART_KRYO_3XX_SILVER 0x803 +#define QCOM_CPU_PART_KRYO_4XX_GOLD 0x804 +#define QCOM_CPU_PART_KRYO_4XX_SILVER 0x805 + +#define NVIDIA_CPU_PART_DENVER 0x003 +#define NVIDIA_CPU_PART_CARMEL 0x004 + +#define FUJITSU_CPU_PART_A64FX 0x001 + +#define HISI_CPU_PART_TSV110 0xD01 + +#define APPLE_CPU_PART_M1_ICESTORM 0x022 +#define APPLE_CPU_PART_M1_FIRESTORM 0x023 +#define APPLE_CPU_PART_M1_ICESTORM_PRO 0x024 +#define APPLE_CPU_PART_M1_FIRESTORM_PRO 0x025 +#define APPLE_CPU_PART_M1_ICESTORM_MAX 0x028 +#define APPLE_CPU_PART_M1_FIRESTORM_MAX 0x029 +#define APPLE_CPU_PART_M2_BLIZZARD 0x032 +#define APPLE_CPU_PART_M2_AVALANCHE 0x033 + +#define AMPERE_CPU_PART_AMPERE1 0xAC3 + +#define MIDR_CORTEX_A53 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A53) +#define MIDR_CORTEX_A57 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A57) +#define MIDR_CORTEX_A72 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A72) +#define MIDR_CORTEX_A73 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A73) +#define MIDR_CORTEX_A75 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A75) +#define MIDR_CORTEX_A35 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A35) +#define MIDR_CORTEX_A55 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A55) +#define MIDR_CORTEX_A76 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A76) +#define MIDR_NEOVERSE_N1 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_NEOVERSE_N1) +#define MIDR_CORTEX_A77 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A77) +#define MIDR_NEOVERSE_V1 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_NEOVERSE_V1) +#define MIDR_CORTEX_A78 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A78) +#define MIDR_CORTEX_A78AE MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A78AE) +#define MIDR_CORTEX_X1 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_X1) +#define MIDR_CORTEX_A510 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A510) +#define MIDR_CORTEX_A710 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A710) +#define MIDR_CORTEX_A715 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A715) +#define MIDR_CORTEX_X2 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_X2) +#define MIDR_NEOVERSE_N2 MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_NEOVERSE_N2) +#define MIDR_CORTEX_A78C MIDR_CPU_MODEL(ARM_CPU_IMP_ARM, ARM_CPU_PART_CORTEX_A78C) +#define MIDR_THUNDERX MIDR_CPU_MODEL(ARM_CPU_IMP_CAVIUM, CAVIUM_CPU_PART_THUNDERX) +#define MIDR_THUNDERX_81XX MIDR_CPU_MODEL(ARM_CPU_IMP_CAVIUM, CAVIUM_CPU_PART_THUNDERX_81XX) +#define MIDR_THUNDERX_83XX MIDR_CPU_MODEL(ARM_CPU_IMP_CAVIUM, CAVIUM_CPU_PART_THUNDERX_83XX) +#define MIDR_OCTX2_98XX MIDR_CPU_MODEL(ARM_CPU_IMP_CAVIUM, CAVIUM_CPU_PART_OCTX2_98XX) +#define MIDR_OCTX2_96XX MIDR_CPU_MODEL(ARM_CPU_IMP_CAVIUM, CAVIUM_CPU_PART_OCTX2_96XX) +#define MIDR_OCTX2_95XX MIDR_CPU_MODEL(ARM_CPU_IMP_CAVIUM, CAVIUM_CPU_PART_OCTX2_95XX) +#define MIDR_OCTX2_95XXN MIDR_CPU_MODEL(ARM_CPU_IMP_CAVIUM, CAVIUM_CPU_PART_OCTX2_95XXN) +#define MIDR_OCTX2_95XXMM MIDR_CPU_MODEL(ARM_CPU_IMP_CAVIUM, CAVIUM_CPU_PART_OCTX2_95XXMM) +#define MIDR_OCTX2_95XXO MIDR_CPU_MODEL(ARM_CPU_IMP_CAVIUM, CAVIUM_CPU_PART_OCTX2_95XXO) +#define MIDR_CAVIUM_THUNDERX2 MIDR_CPU_MODEL(ARM_CPU_IMP_CAVIUM, CAVIUM_CPU_PART_THUNDERX2) +#define MIDR_BRAHMA_B53 MIDR_CPU_MODEL(ARM_CPU_IMP_BRCM, BRCM_CPU_PART_BRAHMA_B53) +#define MIDR_BRCM_VULCAN MIDR_CPU_MODEL(ARM_CPU_IMP_BRCM, BRCM_CPU_PART_VULCAN) +#define MIDR_QCOM_FALKOR_V1 MIDR_CPU_MODEL(ARM_CPU_IMP_QCOM, QCOM_CPU_PART_FALKOR_V1) +#define MIDR_QCOM_FALKOR MIDR_CPU_MODEL(ARM_CPU_IMP_QCOM, QCOM_CPU_PART_FALKOR) +#define MIDR_QCOM_KRYO MIDR_CPU_MODEL(ARM_CPU_IMP_QCOM, QCOM_CPU_PART_KRYO) +#define MIDR_QCOM_KRYO_2XX_GOLD MIDR_CPU_MODEL(ARM_CPU_IMP_QCOM, QCOM_CPU_PART_KRYO_2XX_GOLD) +#define MIDR_QCOM_KRYO_2XX_SILVER MIDR_CPU_MODEL(ARM_CPU_IMP_QCOM, QCOM_CPU_PART_KRYO_2XX_SILVER) +#define MIDR_QCOM_KRYO_3XX_SILVER MIDR_CPU_MODEL(ARM_CPU_IMP_QCOM, QCOM_CPU_PART_KRYO_3XX_SILVER) +#define MIDR_QCOM_KRYO_4XX_GOLD MIDR_CPU_MODEL(ARM_CPU_IMP_QCOM, QCOM_CPU_PART_KRYO_4XX_GOLD) +#define MIDR_QCOM_KRYO_4XX_SILVER MIDR_CPU_MODEL(ARM_CPU_IMP_QCOM, QCOM_CPU_PART_KRYO_4XX_SILVER) +#define MIDR_NVIDIA_DENVER MIDR_CPU_MODEL(ARM_CPU_IMP_NVIDIA, NVIDIA_CPU_PART_DENVER) +#define MIDR_NVIDIA_CARMEL MIDR_CPU_MODEL(ARM_CPU_IMP_NVIDIA, NVIDIA_CPU_PART_CARMEL) +#define MIDR_FUJITSU_A64FX MIDR_CPU_MODEL(ARM_CPU_IMP_FUJITSU, FUJITSU_CPU_PART_A64FX) +#define MIDR_HISI_TSV110 MIDR_CPU_MODEL(ARM_CPU_IMP_HISI, HISI_CPU_PART_TSV110) +#define MIDR_APPLE_M1_ICESTORM MIDR_CPU_MODEL(ARM_CPU_IMP_APPLE, APPLE_CPU_PART_M1_ICESTORM) +#define MIDR_APPLE_M1_FIRESTORM MIDR_CPU_MODEL(ARM_CPU_IMP_APPLE, APPLE_CPU_PART_M1_FIRESTORM) +#define MIDR_APPLE_M1_ICESTORM_PRO MIDR_CPU_MODEL(ARM_CPU_IMP_APPLE, APPLE_CPU_PART_M1_ICESTORM_PRO) +#define MIDR_APPLE_M1_FIRESTORM_PRO MIDR_CPU_MODEL(ARM_CPU_IMP_APPLE, APPLE_CPU_PART_M1_FIRESTORM_PRO) +#define MIDR_APPLE_M1_ICESTORM_MAX MIDR_CPU_MODEL(ARM_CPU_IMP_APPLE, APPLE_CPU_PART_M1_ICESTORM_MAX) +#define MIDR_APPLE_M1_FIRESTORM_MAX MIDR_CPU_MODEL(ARM_CPU_IMP_APPLE, APPLE_CPU_PART_M1_FIRESTORM_MAX) +#define MIDR_APPLE_M2_BLIZZARD MIDR_CPU_MODEL(ARM_CPU_IMP_APPLE, APPLE_CPU_PART_M2_BLIZZARD) +#define MIDR_APPLE_M2_AVALANCHE MIDR_CPU_MODEL(ARM_CPU_IMP_APPLE, APPLE_CPU_PART_M2_AVALANCHE) +#define MIDR_AMPERE1 MIDR_CPU_MODEL(ARM_CPU_IMP_AMPERE, AMPERE_CPU_PART_AMPERE1) + +#endif
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
PATCH 7/21 arm: Stash kvm_vcpu_init for later use
In reply to Oliver UptonA subsequent change will add support for resetting a vCPU, which requires reissuing the KVM_ARM_VCPU_INIT ioctl. Save the kvm_vcpu_init worked out for later use.
--- arm/include/arm-common/kvm-cpu-arch.h | 2 +- arm/kvm-cpu.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
arm/include/arm-common/kvm-cpu-arch.h +1 -1
@@ -11,7 +11,7 @@ struct kvm_cpu {pthread_t thread;unsigned long cpu_id; -unsigned long cpu_type; +struct kvm_vcpu_init init;const char *cpu_compatible;struct kvm *kvm;
arm/kvm-cpu.c +1 -1
@@ -115,7 +115,7 @@ struct kvm_cpu *kvm_cpu__arch_init(struct kvm *kvm, unsigned long cpu_id)/* Populate the vcpu structure. */ vcpu->kvm = kvm; vcpu->cpu_id = cpu_id; - vcpu->cpu_type = vcpu_init.target; + vcpu->init = vcpu_init; vcpu->cpu_compatible = target->compatible; vcpu->is_running = true;
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
PATCH 8/21 arm: Add support for resetting a vCPU
In reply to Oliver UptonIn order to handle PSCI, we need a way to reset a vCPU. Fortunately, the KVM_ARM_VCPU_INIT ioctl is said to have the effect of an architectural reset. Reconfigure features after the INIT ioctl, if necessary (SVE).
--- arm/include/arm-common/kvm-cpu-arch.h | 2 ++ arm/kvm-cpu.c | 14 +++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-)
arm/include/arm-common/kvm-cpu-arch.h +2 -0
@@ -38,6 +38,8 @@ void kvm_cpu__set_kvm_arm_generic_target(struct kvm_arm_target *target);int kvm_cpu__register_kvm_arm_target (struct kvm_arm_target *target); +void kvm_cpu__arm_reset (struct kvm_cpu *vcpu); +static inline bool kvm_cpu__emulate_io (struct kvm_cpu *vcpu,u16 port,void *data,int direction,int size,u32 count) {
arm/kvm-cpu.c +11 -3
@@ -35,6 +35,15 @@ int kvm_cpu__register_kvm_arm_target(struct kvm_arm_target *target)return -ENOSPC ; } +void kvm_cpu__arm_reset (struct kvm_cpu *vcpu) +{ +if (ioctl (vcpu->vcpu_fd,KVM_ARM_VCPU_INIT , &vcpu->init)) +die_perror ("KVM_ARM_VCPU_INIT failed" ); + +if (kvm_cpu__configure_features (vcpu)) +die ("Unable to configure requested vcpu features" ); +} +struct kvm_cpu *kvm_cpu__arch_init (struct kvm *kvm,unsigned long cpu_id) {struct kvm_arm_target *target;
@@ -122,15 +131,14 @@ struct kvm_cpu *kvm_cpu__arch_init(struct kvm *kvm, unsigned long cpu_id)if (err || target->init (vcpu))die ("Unable to initialise vcpu" ); +kvm_cpu__arm_reset (vcpu); + coalesced_offset =ioctl (kvm->sys_fd,KVM_CHECK_EXTENSION ,KVM_CAP_COALESCED_MMIO );if (coalesced_offset) vcpu->ring = (void *)vcpu->kvm_run + (coalesced_offset *PAGE_SIZE ); -if (kvm_cpu__configure_features (vcpu)) -die ("Unable to configure requested vcpu features" ); -return vcpu; }
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
PATCH 9/21 arm: Use KVM_SET_MP_STATE ioctl to power off non-boot vCPUs
In reply to Oliver UptonUsing the POWER_OFF flag in kvm_vcpu_init gets in the way of resetting a vCPU in response to a PSCI CPU_ON call, for obvious reasons. Drop the flag in favor of using the KVM_SET_MP_STATE call for non-boot vCPUs.
--- arm/kvm-cpu.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
arm/kvm-cpu.c +10 -4
@@ -72,10 +72,6 @@ struct kvm_cpu *kvm_cpu__arch_init(struct kvm *kvm, unsigned long cpu_id)if (vcpu->kvm_run ==MAP_FAILED )die ("unable to mmap vcpu fd" ); -/* VCPU 0 is the boot CPU, the others start in a poweroff state. */ -if (cpu_id >0 ) - vcpu_init.features[0 ] |= (1UL <<KVM_ARM_VCPU_POWER_OFF ); -/* Set KVM_ARM_VCPU_PSCI_0_2 if available */ if (kvm__supports_extension (kvm,KVM_CAP_ARM_PSCI_0_2 )) { vcpu_init.features[0 ] |= (1UL <<KVM_ARM_VCPU_PSCI_0_2 );
@@ -133,6 +129,16 @@ struct kvm_cpu *kvm_cpu__arch_init(struct kvm *kvm, unsigned long cpu_id)kvm_cpu__arm_reset (vcpu); +/* VCPU 0 is the boot CPU, the others start in a poweroff state. */ +if (cpu_id >0 ) { +struct kvm_mp_state mp_state = { + .mp_state =KVM_MP_STATE_STOPPED , + }; + +if (ioctl (vcpu->vcpu_fd,KVM_SET_MP_STATE , &mp_state)) +die_perror ("KVM_SET_MP_STATE failed" ); + } + coalesced_offset =ioctl (kvm->sys_fd,KVM_CHECK_EXTENSION ,KVM_CAP_COALESCED_MMIO );if (coalesced_offset)
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
PATCH 10/21 aarch64: Expose ARM64_CORE_REG() for general use
In reply to Oliver UptonExpose the macro such that it may be used to get SMCCC arguments in a future change.
--- arm/aarch64/include/kvm/kvm-cpu-arch.h | 16 ++++++++++++++++ arm/aarch64/kvm-cpu.c | 16 ---------------- 2 files changed, 16 insertions(+), 16 deletions(-)
arm/aarch64/include/kvm/kvm-cpu-arch.h +16 -0
@@ -11,6 +11,22 @@#define ARM_CPU_CTRL 3, 0, 1, 0#define ARM_CPU_CTRL_SCTLR_EL1 0 +static inline __u64 __core_reg_id (__u64 offset) +{ +__u64 id =KVM_REG_ARM64 |KVM_REG_ARM_CORE | offset; + +if (offset <KVM_REG_ARM_CORE_REG (fp_regs)) + id |=KVM_REG_SIZE_U64 ; +else if (offset <KVM_REG_ARM_CORE_REG (fp_regs.fpsr)) + id |=KVM_REG_SIZE_U128 ; +else + id |=KVM_REG_SIZE_U32 ; + +return id; +} + +#define ARM64_CORE_REG (x) __core_reg_id(KVM_REG_ARM_CORE_REG(x)) +void kvm_cpu__select_features (struct kvm *kvm,struct kvm_vcpu_init *init);int kvm_cpu__configure_features (struct kvm_cpu *vcpu);int kvm_cpu__setup_pvtime (struct kvm_cpu *vcpu);
arm/aarch64/kvm-cpu.c +0 -16
@@ -12,22 +12,6 @@#define SCTLR_EL1_E0E_MASK (1 << 24)#define SCTLR_EL1_EE_MASK (1 << 25) -static __u64 __core_reg_id (__u64 offset) -{ -__u64 id =KVM_REG_ARM64 |KVM_REG_ARM_CORE | offset; - -if (offset <KVM_REG_ARM_CORE_REG (fp_regs)) - id |=KVM_REG_SIZE_U64 ; -else if (offset <KVM_REG_ARM_CORE_REG (fp_regs.fpsr)) - id |=KVM_REG_SIZE_U128 ; -else - id |=KVM_REG_SIZE_U32 ; - -return id; -} - -#define ARM64_CORE_REG (x) __core_reg_id(KVM_REG_ARM_CORE_REG(x)) -unsigned long kvm_cpu__get_vcpu_mpidr (struct kvm_cpu *vcpu) {struct kvm_one_reg reg;
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
PATCH 11/21 arm: Generalize execution state specific VM initialization
In reply to Oliver UptonThe current state of MTE initialization sticks out a bit: an MTE-specific init routine is implemented for both aarch32 and aarch64. Generalize on the notion of execution state specific VM initialization in preparation for SMCCC filter creation, as the feature is only present on aarch64.
--- arm/aarch32/include/kvm/kvm-arch.h | 2 +- arm/aarch64/include/kvm/kvm-arch.h | 1 - arm/aarch64/kvm.c | 7 ++++++- arm/include/arm-common/kvm-arch.h | 2 ++ arm/kvm.c | 2 +- 5 files changed, 10 insertions(+), 4 deletions(-)
arm/aarch32/include/kvm/kvm-arch.h +1 -1
@@ -6,7 +6,7 @@#define kvm__arch_get_kern_offset (...) 0x8000struct kvm ; -static inline void kvm__arch_enable_mte (struct kvm *kvm) {} +static inline void __kvm__arm_init (struct kvm *kvm) {}#define MAX_PAGE_SIZE SZ_4K
arm/aarch64/include/kvm/kvm-arch.h +0 -1
@@ -6,7 +6,6 @@struct kvm ;unsigned long long kvm__arch_get_kern_offset (struct kvm *kvm,int fd);int kvm__arch_get_ipa_limit (struct kvm *kvm); -void kvm__arch_enable_mte (struct kvm *kvm);#define MAX_PAGE_SIZE SZ_64K
arm/aarch64/kvm.c +6 -1
@@ -134,7 +134,7 @@ int kvm__get_vm_type(struct kvm *kvm)return KVM_VM_TYPE_ARM_IPA_SIZE (ipa_bits); } -void kvm__arch_enable_mte (struct kvm *kvm) +static void kvm__arch_enable_mte (struct kvm *kvm) {struct kvm_enable_cap cap = { .cap =KVM_CAP_ARM_MTE ,
@@ -160,3 +160,8 @@ void kvm__arch_enable_mte(struct kvm *kvm)pr_debug ("MTE capability enabled" ); } + +void __kvm__arm_init (struct kvm *kvm) +{ +kvm__arch_enable_mte (kvm); +}
arm/include/arm-common/kvm-arch.h +2 -0
@@ -110,4 +110,6 @@ struct kvm_arch { cpu_set_t *vcpu_affinity_cpuset; }; +void __kvm__arm_init (struct kvm *kvm); +#endif /* ARM_COMMON__KVM_ARCH_H */
arm/kvm.c +1 -1
@@ -93,7 +93,7 @@ void kvm__arch_init(struct kvm *kvm)if (gic__create (kvm, kvm->cfg.arch.irqchip))die ("Failed to create virtual GIC" ); -kvm__arch_enable_mte (kvm); +__kvm__arm_init (kvm); }#define FDT_ALIGN SZ_2M
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
PATCH 12/21 Add helpers to pause the VM from vCPU thread
In reply to Oliver UptonPausing the VM from a vCPU thread is perilous with the current helpers, as it waits indefinitely for a signal that never comes when invoked from a vCPU thread. Instead, add a helper for pausing the VM from a vCPU, working around the issue by explicitly marking the caller as paused before proceeding.
--- include/kvm/kvm-cpu.h | 3 +++ kvm-cpu.c | 15 +++++++++++++++ 2 files changed, 18 insertions(+)
include/kvm/kvm-cpu.h +3 -0
@@ -29,4 +29,7 @@ void kvm_cpu__show_page_tables(struct kvm_cpu *vcpu);void kvm_cpu__arch_nmi (struct kvm_cpu *cpu);void kvm_cpu__run_on_all_cpus (struct kvm *kvm,struct kvm_cpu_task *task); +void kvm_cpu__pause_vm (struct kvm_cpu *vcpu); +void kvm_cpu__continue_vm (struct kvm_cpu *vcpu); +#endif /* KVM__KVM_CPU_H */
kvm-cpu.c +15 -0
@@ -141,6 +141,21 @@ void kvm_cpu__run_on_all_cpus(struct kvm *kvm, struct kvm_cpu_task *task)mutex_unlock (&task_lock); } +void kvm_cpu__pause_vm (struct kvm_cpu *vcpu) +{ +/* +* Mark the calling vCPU as paused to avoid waiting indefinitely for a +* signal exit. +*/ + vcpu->paused = true; +kvm__pause (vcpu->kvm); +} + +void kvm_cpu__continue_vm (struct kvm_cpu *vcpu) +{ +kvm__continue (vcpu->kvm); +} +int kvm_cpu__start (struct kvm_cpu *cpu) {sigset_t sigset;
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
Re: [PATCH kvmtool 12/21] Add helpers to pause the VM from vCPU thread
Hi Oliver,
I have a play with this series, the guest always hang when hotplug two more cpus, it seems the kvm_cpu_continue_vm forget to continue the current cpu.
Quoted 34 lines · Oliver Upton
Pausing the VM from a vCPU thread is perilous with the current helpers, as it waits indefinitely for a signal that never comes when invoked from a vCPU thread. Instead, add a helper for pausing the VM from a vCPU, working around the issue by explicitly marking the caller as paused before proceeding.
Signed-off-by Oliver Upton <[email protected]>--- include/kvm/kvm-cpu.h | 3 +++ kvm-cpu.c | 15 +++++++++++++++ 2 files changed, 18 insertions(+)include/kvm/kvm-cpu.h +3
-0@@ -29,4 +29,7 @@ void kvm_cpu__show_page_tables(struct kvm_cpu *vcpu);void kvm_cpu__arch_nmi (struct kvm_cpu *cpu);void kvm_cpu__run_on_all_cpus (struct kvm *kvm,struct kvm_cpu_task *task); +void kvm_cpu__pause_vm (struct kvm_cpu *vcpu); +void kvm_cpu__continue_vm (struct kvm_cpu *vcpu); +#endif /* KVM__KVM_CPU_H */ kvm-cpu.c +2
-0@@ -141,6 +141,21 @@ void kvm_cpu__run_on_all_cpus(struct kvm *kvm, struct kvm_cpu_task *task)mutex_unlock (&task_lock); } +void kvm_cpu__pause_vm (struct kvm_cpu *vcpu) +{
kvm-cpu.c void kvm_cpu__run_on_all_cpus(struct kvm *kvm, struct kvm_cpu_task *task) · line 155
Quoted 10 lines
kvm-cpu.c +10
-0+/* +* Mark the calling vCPU as paused to avoid waiting indefinitely for a +* signal exit. +*/ + vcpu->paused = true; +kvm__pause (vcpu->kvm); +} + +void kvm_cpu__continue_vm (struct kvm_cpu *vcpu) +{
Here should add:
vcpu->paused = false;
Quoted 6 lines
kvm-cpu.c +3
-0+kvm__continue (vcpu->kvm); +} +int kvm_cpu__start (struct kvm_cpu *cpu) {sigset_t sigset;
Signature · 1 line
Shaoqin
Re: [PATCH kvmtool 12/21] Add helpers to pause the VM from vCPU thread
Hi Shaoqin,
Quoted 4 lines · Shaoqin Huang
Hi Oliver,
I have a play with this series, the guest always hang when hotplug two more cpus, it seems the
kvm_cpu_continue_vmforget to continue the current cpu.
Thanks for testing the series out. Heh, a bit of a silly bug on my part, sorry about that.
Quoted 13 lines
kvm-cpu.c void kvm_cpu__run_on_all_cpus(struct kvm *kvm, struct kvm_cpu_task *task) · line 155
Quoted 12 lines
kvm-cpu.c +12
-0+void kvm_cpu__pause_vm (struct kvm_cpu *vcpu) +{ +/* +* Mark the calling vCPU as paused to avoid waiting indefinitely for a +* signal exit. +*/ + vcpu->paused = true; +kvm__pause (vcpu->kvm); +} + +void kvm_cpu__continue_vm (struct kvm_cpu *vcpu) +{Here should add:
Quoted 4 lines
vcpu->paused = false;kvm-cpu.c void kvm_cpu__run_on_all_cpus(struct kvm *kvm, struct kvm_cpu_task *task) · line 158
Quoted 3 lines
kvm-cpu.c +3
-0+kvm__continue (vcpu->kvm); +} +
LGTM, I'll stick this in v2.
Quoted 7 lines
Quoted 3 lines
int kvm_cpu__start(struct kvm_cpu *cpu) { sigset_t sigset;Signature · 1 line
Shaoqin
Signature · 2 lines
Thanks, Oliver
PATCH 13/21 aarch64: Add support for finding vCPU for given MPIDR
In reply to Oliver UptonSome PSCI calls take an MPIDR affinity as an argument. Add a helper to get the vCPU that matches an MPIDR so we can find the intended recipient.
--- arm/aarch64/include/kvm/kvm-arch.h | 1 + arm/aarch64/kvm.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+)
arm/aarch64/include/kvm/kvm-arch.h +1 -0
@@ -6,6 +6,7 @@struct kvm ;unsigned long long kvm__arch_get_kern_offset (struct kvm *kvm,int fd);int kvm__arch_get_ipa_limit (struct kvm *kvm); +struct kvm_cpu *kvm__arch_mpidr_to_vcpu (struct kvm *kvm,u64 target_mpidr);#define MAX_PAGE_SIZE SZ_64K
arm/aarch64/kvm.c +16 -0
@@ -1,4 +1,5 @@#include "kvm/kvm.h" +#include "kvm/kvm-cpu.h" #include <asm/image.h>
@@ -165,3 +166,18 @@ void __kvm__arm_init(struct kvm *kvm) {kvm__arch_enable_mte (kvm); } + +struct kvm_cpu *kvm__arch_mpidr_to_vcpu (struct kvm *kvm,u64 target_mpidr) +{ +int i; + +for (i =0 ; i < kvm->nrcpus; i++) { +struct kvm_cpu *tmp = kvm->cpus[i]; +u64 mpidr =kvm_cpu__get_vcpu_mpidr (tmp) &ARM_MPIDR_HWID_BITMASK ; + +if (mpidr == target_mpidr) +return tmp; + } + +return NULL ; +}
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
PATCH 14/21 aarch64: Add skeleton implementation for PSCI
In reply to Oliver UptonAdd an extremely barebones implementation for handling PSCI, where the only supported call is PSCI_VERSION.
--- Makefile | 4 +- arm/aarch32/kvm-cpu.c | 5 +++ arm/aarch64/include/asm/smccc.h | 65 +++++++++++++++++++++++++++++++++ arm/aarch64/kvm-cpu.c | 14 +++++++ arm/aarch64/kvm.c | 2 + arm/aarch64/psci.c | 36 ++++++++++++++++++ arm/aarch64/smccc.c | 45 +++++++++++++++++++++++ arm/kvm-cpu.c | 5 --- 8 files changed, 170 insertions(+), 6 deletions(-) create mode 100644 arm/aarch64/include/asm/smccc.h create mode 100644 arm/aarch64/psci.c create mode 100644 arm/aarch64/smccc.c
Makefile +3 -1
@@ -190,8 +190,10 @@ ifeq ($(ARCH), arm64) OBJS += arm/aarch64/arm-cpu.o OBJS += arm/aarch64/kvm-cpu.o OBJS += arm/aarch64/kvm.o - OBJS += arm/aarch64/pvtime.o OBJS += arm/aarch64/pmu.o + OBJS += arm/aarch64/psci.o + OBJS += arm/aarch64/pvtime.o + OBJS += arm/aarch64/smccc.o ARCH_INCLUDE := $(HDRS_ARM_COMMON) ARCH_INCLUDE += -Iarm/aarch64/include
arm/aarch32/kvm-cpu.c +5 -0
@@ -130,3 +130,8 @@ void kvm_cpu__show_registers(struct kvm_cpu *vcpu)die ("KVM_GET_ONE_REG failed (LR_svc)" );dprintf (debug_fd," LR_svc: 0x%x\n" , data); } + +bool kvm_cpu__handle_exit (struct kvm_cpu *vcpu) +{ +return false; +}
arm/aarch64/include/asm/smccc.h new file +65 -0
65 lines not on this page · raw message
arm/aarch64/kvm-cpu.c +14 -0
@@ -1,3 +1,4 @@ +#include "asm/smccc.h" #include "kvm/kvm-cpu.h" #include "kvm/kvm.h" #include "kvm/virtio.h"
@@ -261,3 +262,16 @@ void kvm_cpu__show_registers(struct kvm_cpu *vcpu)die ("KVM_GET_ONE_REG failed (lr)" );dprintf (debug_fd," LR: 0x%lx\n" , data); } + +bool kvm_cpu__handle_exit (struct kvm_cpu *vcpu) +{ +struct kvm_run *run = vcpu->kvm_run; + +switch (run->exit_reason) { +case KVM_EXIT_HYPERCALL : +handle_hypercall (vcpu); +return true; +default : +return false; + } +}
arm/aarch64/kvm.c +2 -0
@@ -1,3 +1,4 @@ +#include "asm/smccc.h" #include "kvm/kvm.h" #include "kvm/kvm-cpu.h"
@@ -165,6 +166,7 @@ static void kvm__arch_enable_mte(struct kvm *kvm)void __kvm__arm_init (struct kvm *kvm) {kvm__arch_enable_mte (kvm); +kvm__setup_smccc (kvm); }struct kvm_cpu *kvm__arch_mpidr_to_vcpu (struct kvm *kvm,u64 target_mpidr)
arm/aarch64/psci.c new file +36 -0
@@ -0,0 +1,36 @@ +#include "asm/smccc.h" +#include "kvm/kvm.h" +#include "kvm/kvm-cpu.h" +#include "kvm/util.h" + +#include <linux/psci.h> +#include <linux/types.h> + +static void psci_features (struct kvm_cpu *vcpu,struct arm_smccc_res *res) +{ +u32 arg =smccc_get_arg (vcpu,1 ); + + res->a0 =PSCI_RET_NOT_SUPPORTED ; +if (!smccc_calling_conv_allowed (vcpu, arg)) +return ; + +switch (arg) { +case ARM_SMCCC_VERSION_FUNC_ID : + res->a0 =PSCI_RET_SUCCESS ; +break ; + } +} + +void handle_psci (struct kvm_cpu *vcpu,struct arm_smccc_res *res) +{ +switch (vcpu->kvm_run->hypercall.nr) { +case PSCI_0_2_FN_PSCI_VERSION : + res->a0 =PSCI_VERSION (1 ,0 ); +break ; +case PSCI_1_0_FN_PSCI_FEATURES : +psci_features (vcpu, res); +break ; +default : + res->a0 =PSCI_RET_NOT_SUPPORTED ; + } +}
arm/aarch64/smccc.c new file +45 -0
45 lines not on this page · raw message
arm/kvm-cpu.c +0 -5
@@ -158,11 +158,6 @@ void kvm_cpu__delete(struct kvm_cpu *vcpu)free (vcpu); } -bool kvm_cpu__handle_exit (struct kvm_cpu *vcpu) -{ -return false; -} -void kvm_cpu__show_page_tables (struct kvm_cpu *vcpu) { }
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
Joey Gouly
reviewerRe: [PATCH kvmtool 14/21] aarch64: Add skeleton implementation for PSCI
Hi Oliver,
Quoted 233 lines · Oliver Upton
Add an extremely barebones implementation for handling PSCI, where the only supported call is
PSCI_VERSION.Signed-off-by Oliver Upton <[email protected]>--- Makefile | 4 +- arm/aarch32/kvm-cpu.c | 5 +++ arm/aarch64/include/asm/smccc.h | 65 +++++++++++++++++++++++++++++++++ arm/aarch64/kvm-cpu.c | 14 +++++++ arm/aarch64/kvm.c | 2 + arm/aarch64/psci.c | 36 ++++++++++++++++++ arm/aarch64/smccc.c | 45 +++++++++++++++++++++++ arm/kvm-cpu.c | 5 --- 8 files changed, 170 insertions(+), 6 deletions(-) create mode 100644 arm/aarch64/include/asm/smccc.h create mode 100644 arm/aarch64/psci.c create mode 100644 arm/aarch64/smccc.cMakefile +3
-1@@ -190,8 +190,10 @@ ifeq ($(ARCH), arm64) OBJS += arm/aarch64/arm-cpu.o OBJS += arm/aarch64/kvm-cpu.o OBJS += arm/aarch64/kvm.o - OBJS += arm/aarch64/pvtime.o OBJS += arm/aarch64/pmu.o + OBJS += arm/aarch64/psci.o + OBJS += arm/aarch64/pvtime.o + OBJS += arm/aarch64/smccc.o ARCH_INCLUDE := $(HDRS_ARM_COMMON) ARCH_INCLUDE += -Iarm/aarch64/includearm/aarch32/kvm-cpu.c +5
-0@@ -130,3 +130,8 @@ void kvm_cpu__show_registers(struct kvm_cpu *vcpu)die ("KVM_GET_ONE_REG failed (LR_svc)" );dprintf (debug_fd," LR_svc: 0x%x\n" , data); } + +bool kvm_cpu__handle_exit (struct kvm_cpu *vcpu) +{ +return false; +}arm/aarch64/include/asm/smccc.h new file +65
-0@@ -0,0 +1,65 @@ +#ifndef __ARM_SMCCC_H__ +#define __ARM_SMCCC_H__ + +#include "kvm/kvm-cpu.h" + +#include <linux/arm-smccc.h> +#include <linux/types.h> + +static inline bool smccc_is_64bit (struct kvm_cpu *vcpu) +{ +return ARM_SMCCC_IS_64 (vcpu->kvm_run->hypercall.nr); +} + +static inline bool smccc_calling_conv_allowed (struct kvm_cpu *vcpu,u32 fn) +{ +return !(vcpu->kvm->cfg.arch.aarch32_guest &&ARM_SMCCC_IS_64 (fn)); +} + +static inline u64 smccc_get_arg (struct kvm_cpu *vcpu,u8 arg) +{ +u64 val; +struct kvm_one_reg reg = { + .id =ARM64_CORE_REG (regs.regs[arg]), + .addr = (u64)&val, + }; + +if (ioctl (vcpu->vcpu_fd,KVM_GET_ONE_REG , ®)) +die_perror ("KVM_GET_ONE_REG failed" ); + +if (!smccc_is_64bit (vcpu)) + val = (u32 )val; + +return val; +} + +static inline void smccc_return_result (struct kvm_cpu *vcpu,struct arm_smccc_res *res) +{ +unsigned long *vals = (unsigned long *)res; +unsigned long i; + +/* +* The author was lazy and chose to abuse the layout of struct +* arm_smccc_res to write a loop set the retvals. +*/ +for (i =0 ; i <sizeof (*res) /sizeof (unsigned long ); i++) { +u64 val = vals[i]; +struct kvm_one_reg reg = { + .id =ARM64_CORE_REG (regs.regs[i]), + .addr = (u64)&val, + }; + +if (!smccc_is_64bit (vcpu)) + val = (u32 )val; + +if (ioctl (vcpu->vcpu_fd,KVM_SET_ONE_REG , ®)) +die_perror ("KVM_SET_ONE_REG failed" ); + } +} + +bool handle_hypercall (struct kvm_cpu *vcpu); +void handle_psci (struct kvm_cpu *vcpu,struct arm_smccc_res *res); + +void kvm__setup_smccc (struct kvm *kvm); + +#endif /* __ARM_SMCCC_H__ */ arm/aarch64/kvm-cpu.c +14
-0@@ -1,3 +1,4 @@ +#include "asm/smccc.h" #include "kvm/kvm-cpu.h" #include "kvm/kvm.h" #include "kvm/virtio.h" @@ -261,3 +262,16 @@ void kvm_cpu__show_registers(struct kvm_cpu *vcpu)die ("KVM_GET_ONE_REG failed (lr)" );dprintf (debug_fd," LR: 0x%lx\n" , data); } + +bool kvm_cpu__handle_exit (struct kvm_cpu *vcpu) +{ +struct kvm_run *run = vcpu->kvm_run; + +switch (run->exit_reason) { +case KVM_EXIT_HYPERCALL : +handle_hypercall (vcpu); +return true; +default : +return false; + } +}arm/aarch64/kvm.c +2
-0@@ -1,3 +1,4 @@ +#include "asm/smccc.h" #include "kvm/kvm.h" #include "kvm/kvm-cpu.h" @@ -165,6 +166,7 @@ static void kvm__arch_enable_mte(struct kvm *kvm)void __kvm__arm_init (struct kvm *kvm) {kvm__arch_enable_mte (kvm); +kvm__setup_smccc (kvm); }struct kvm_cpu *kvm__arch_mpidr_to_vcpu (struct kvm *kvm,u64 target_mpidr)arm/aarch64/psci.c new file +36
-0@@ -0,0 +1,36 @@ +#include "asm/smccc.h" +#include "kvm/kvm.h" +#include "kvm/kvm-cpu.h" +#include "kvm/util.h" + +#include <linux/psci.h> +#include <linux/types.h> + +static void psci_features (struct kvm_cpu *vcpu,struct arm_smccc_res *res) +{ +u32 arg =smccc_get_arg (vcpu,1 ); + + res->a0 =PSCI_RET_NOT_SUPPORTED ; +if (!smccc_calling_conv_allowed (vcpu, arg)) +return ; + +switch (arg) { +case ARM_SMCCC_VERSION_FUNC_ID : + res->a0 =PSCI_RET_SUCCESS ; +break ; + } +} + +void handle_psci (struct kvm_cpu *vcpu,struct arm_smccc_res *res) +{ +switch (vcpu->kvm_run->hypercall.nr) { +case PSCI_0_2_FN_PSCI_VERSION : + res->a0 =PSCI_VERSION (1 ,0 ); +break ; +case PSCI_1_0_FN_PSCI_FEATURES : +psci_features (vcpu, res); +break ; +default : + res->a0 =PSCI_RET_NOT_SUPPORTED ; + } +}arm/aarch64/smccc.c new file +24
-0@@ -0,0 +1,45 @@ +#include "asm/smccc.h" +#include "kvm/kvm.h" +#include "kvm/kvm-cpu.h" +#include "kvm/util.h" + +#include <linux/types.h> + +static void handle_std_call (struct kvm_cpu *vcpu,struct arm_smccc_res *res) +{ +u32 fn = vcpu->kvm_run->hypercall.nr; + +switch (ARM_SMCCC_FUNC_NUM (fn)) { +/* PSCI */ + case 0x00 ... 0x1F: +handle_psci (vcpu, res); +break ; + } +} + +bool handle_hypercall (struct kvm_cpu *vcpu) +{ +u32 fn = vcpu->kvm_run->hypercall.nr; +struct arm_smccc_res res = { + .a0 =SMCCC_RET_NOT_SUPPORTED ,
arm/aarch64/smccc.c line 34
Quoted 10 lines
arm/aarch64/smccc.c new file +10
-0+ }; + +if (!smccc_calling_conv_allowed (vcpu, fn)) + goto out; + +switch (ARM_SMCCC_OWNER_NUM (fn)) { +case ARM_SMCCC_OWNER_STANDARD : +handle_std_call (vcpu, &res); +break ; +default :
I get the following error while compiling (adding a break; fixes things):
arm/aarch64/smccc.c: In function 'handle_hypercall': arm/aarch64/smccc.c:34:2: error: label at end of compound statement 34 | default: | ^~~~~~~
Quoted 17 lines
arm/aarch64/smccc.c new file +11
-0+ } + +out: +smccc_return_result (vcpu, &res); +return true; +} + +void kvm__setup_smccc (struct kvm *kvm) +{ + +}arm/kvm-cpu.c +0
-0@@ -158,11 +158,6 @@ void kvm_cpu__delete(struct kvm_cpu *vcpu)free (vcpu);
arm/kvm-cpu.c void kvm_cpu__delete(struct kvm_cpu *vcpu) · line 163
Quoted 10 lines
arm/kvm-cpu.c +0
-5} -bool kvm_cpu__handle_exit (struct kvm_cpu *vcpu) -{ -return false; -} -void kvm_cpu__show_page_tables (struct kvm_cpu *vcpu) { }
Thanks,
Joey
PATCH 15/21 aarch64: psci: Implement CPU_SUSPEND
In reply to Oliver UptonImplement support for PSCI CPU_SUSPEND, leveraging in-kernel suspend emulation (i.e. a WFI state). Eagerly resume the vCPU for any wakeup event.
--- arm/aarch64/kvm-cpu.c | 18 ++++++++++++++++++ arm/aarch64/psci.c | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+)
arm/aarch64/kvm-cpu.c +18 -0
@@ -263,6 +263,16 @@ void kvm_cpu__show_registers(struct kvm_cpu *vcpu)dprintf (debug_fd," LR: 0x%lx\n" , data); } +static void handle_wakeup (struct kvm_cpu *vcpu) +{ +struct kvm_mp_state mp_state = { + .mp_state =KVM_MP_STATE_RUNNABLE , + }; + +if (ioctl (vcpu->vcpu_fd,KVM_SET_MP_STATE , &mp_state)) +die_perror ("KVM_SET_MP_STATE failed" ); +} +bool kvm_cpu__handle_exit (struct kvm_cpu *vcpu) {struct kvm_run *run = vcpu->kvm_run;
@@ -271,6 +281,14 @@ bool kvm_cpu__handle_exit(struct kvm_cpu *vcpu)case KVM_EXIT_HYPERCALL :handle_hypercall (vcpu);return true; +case KVM_EXIT_SYSTEM_EVENT : +switch (run->system_event.type) { +case KVM_SYSTEM_EVENT_WAKEUP : +handle_wakeup (vcpu); +return true; +default : +return false; + }default :return false; }
arm/aarch64/psci.c +19 -0
@@ -15,12 +15,27 @@ static void psci_features(struct kvm_cpu *vcpu, struct arm_smccc_res *res)return ;switch (arg) { +case PSCI_0_2_FN_CPU_SUSPEND : +case PSCI_0_2_FN64_CPU_SUSPEND :case ARM_SMCCC_VERSION_FUNC_ID : res->a0 =PSCI_RET_SUCCESS ;break ; } } +static void cpu_suspend (struct kvm_cpu *vcpu,struct arm_smccc_res *res) +{ +struct kvm_mp_state mp_state = { + .mp_state =KVM_MP_STATE_SUSPENDED , + }; + +/* Rely on in-kernel emulation of a 'suspended' (i.e. WFI) state. */ +if (ioctl (vcpu->vcpu_fd,KVM_SET_MP_STATE , &mp_state)) +die_perror ("KVM_SET_MP_STATE failed" ); + + res->a0 =PSCI_RET_SUCCESS ; +} +void handle_psci (struct kvm_cpu *vcpu,struct arm_smccc_res *res) {switch (vcpu->kvm_run->hypercall.nr) {
@@ -30,6 +45,10 @@ void handle_psci(struct kvm_cpu *vcpu, struct arm_smccc_res *res)case PSCI_1_0_FN_PSCI_FEATURES :psci_features (vcpu, res);break ; +case PSCI_0_2_FN_CPU_SUSPEND : +case PSCI_0_2_FN64_CPU_SUSPEND : +cpu_suspend (vcpu, res); +break ;default : res->a0 =PSCI_RET_NOT_SUPPORTED ; }
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
PATCH 16/21 aarch64: psci: Implement CPU_OFF
In reply to Oliver UptonAdd support for PSCI CPU_OFF, relying on KVM emulation of a 'powered off' vCPU by setting the MP state to KVM_MP_STATE_STOPPED.
--- arm/aarch64/psci.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
arm/aarch64/psci.c +14 -0
@@ -17,6 +17,7 @@ static void psci_features(struct kvm_cpu *vcpu, struct arm_smccc_res *res)switch (arg) {case PSCI_0_2_FN_CPU_SUSPEND :case PSCI_0_2_FN64_CPU_SUSPEND : +case PSCI_0_2_FN_CPU_OFF :case ARM_SMCCC_VERSION_FUNC_ID : res->a0 =PSCI_RET_SUCCESS ;break ;
@@ -36,6 +37,16 @@ static void cpu_suspend(struct kvm_cpu *vcpu, struct arm_smccc_res *res) res->a0 =PSCI_RET_SUCCESS ; } +static void cpu_off (struct kvm_cpu *vcpu,struct arm_smccc_res *res) +{ +struct kvm_mp_state mp_state = { + .mp_state =KVM_MP_STATE_STOPPED , + }; + +if (ioctl (vcpu->vcpu_fd,KVM_SET_MP_STATE , &mp_state)) +die_perror ("KVM_SET_MP_STATE failed" ); +} +void handle_psci (struct kvm_cpu *vcpu,struct arm_smccc_res *res) {switch (vcpu->kvm_run->hypercall.nr) {
@@ -49,6 +60,9 @@ void handle_psci(struct kvm_cpu *vcpu, struct arm_smccc_res *res)case PSCI_0_2_FN64_CPU_SUSPEND :cpu_suspend (vcpu, res);break ; +case PSCI_0_2_FN_CPU_OFF : +cpu_off (vcpu, res); +break ;default : res->a0 =PSCI_RET_NOT_SUPPORTED ; }
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
PATCH 17/21 aarch64: psci: Implement CPU_ON
In reply to Oliver UptonAdd support for the PSCI CPU_ON call, wherein a caller can power on a targeted CPU and reset it with the provided context (i.e. entrypoint and context id). Rely on the KVM_ARM_VCPU_INIT ioctl, which has the effect of an architectural warm reset, to do the heavy lifting.
--- arm/aarch64/psci.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+)
arm/aarch64/psci.c +67 -0
@@ -18,6 +18,8 @@ static void psci_features(struct kvm_cpu *vcpu, struct arm_smccc_res *res)case PSCI_0_2_FN_CPU_SUSPEND :case PSCI_0_2_FN64_CPU_SUSPEND :case PSCI_0_2_FN_CPU_OFF : +case PSCI_0_2_FN_CPU_ON : +case PSCI_0_2_FN64_CPU_ON :case ARM_SMCCC_VERSION_FUNC_ID : res->a0 =PSCI_RET_SUCCESS ;break ;
@@ -47,6 +49,67 @@ static void cpu_off(struct kvm_cpu *vcpu, struct arm_smccc_res *res)die_perror ("KVM_SET_MP_STATE failed" ); } +static void reset_cpu_with_context (struct kvm_cpu *vcpu,u64 entry_addr,u64 ctx_id) +{ +struct kvm_one_reg reg; + +kvm_cpu__arm_reset (vcpu); + + reg = (struct kvm_one_reg ) { + .id =ARM64_CORE_REG (regs.pc), + .addr = (u64)&entry_addr, + }; +if (ioctl (vcpu->vcpu_fd,KVM_SET_ONE_REG , ®)) +die_perror ("KVM_SET_ONE_REG failed" ); + + reg = (struct kvm_one_reg ) { + .id =ARM64_CORE_REG (regs.regs[0 ]), + .addr = (u64)&ctx_id, + }; +if (ioctl (vcpu->vcpu_fd,KVM_SET_ONE_REG , ®)) +die_perror ("KVM_SET_ONE_REG failed" ); +} + +static bool psci_valid_affinity (u64 affinity) +{ +return !(affinity & ~ARM_MPIDR_HWID_BITMASK ); +} + +static void cpu_on (struct kvm_cpu *vcpu,struct arm_smccc_res *res) +{ +u64 target_mpidr =smccc_get_arg (vcpu,1 ); +u64 entry_addr =smccc_get_arg (vcpu,2 ); +u64 ctx_id =smccc_get_arg (vcpu,3 ); +struct kvm_mp_state mp_state; +struct kvm_cpu *target; + +if (!psci_valid_affinity (target_mpidr)) { + res->a0 =PSCI_RET_INVALID_PARAMS ; +return ; + } + +kvm_cpu__pause_vm (vcpu); + + target =kvm__arch_mpidr_to_vcpu (vcpu->kvm, target_mpidr); +if (!target) { + res->a0 =PSCI_RET_INVALID_PARAMS ; + goto out_continue; + } + +if (ioctl (target->vcpu_fd,KVM_GET_MP_STATE , &mp_state)) +die_perror ("KVM_GET_MP_STATE failed" ); + +if (mp_state.mp_state !=KVM_MP_STATE_STOPPED ) { + res->a0 =PSCI_RET_ALREADY_ON ; + goto out_continue; + } + +reset_cpu_with_context (target, entry_addr, ctx_id); + res->a0 =PSCI_RET_SUCCESS ; +out_continue: +kvm_cpu__continue_vm (vcpu); +} +void handle_psci (struct kvm_cpu *vcpu,struct arm_smccc_res *res) {switch (vcpu->kvm_run->hypercall.nr) {
@@ -63,6 +126,10 @@ void handle_psci(struct kvm_cpu *vcpu, struct arm_smccc_res *res)case PSCI_0_2_FN_CPU_OFF :cpu_off (vcpu, res);break ; +case PSCI_0_2_FN_CPU_ON : +case PSCI_0_2_FN64_CPU_ON : +cpu_on (vcpu, res); +break ;default : res->a0 =PSCI_RET_NOT_SUPPORTED ; }
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
PATCH 18/21 aarch64: psci: Implement AFFINITY_INFO
In reply to Oliver UptonImplement support for PSCI AFFINITY_INFO by iteratively searching all of the vCPUs in a VM for those that match the specified affinity. Pause the VM to avoid racing against other PSCI calls in the system that might change the power state of the vCPUs.
--- arm/aarch64/include/kvm/kvm-cpu-arch.h | 12 +++++- arm/aarch64/psci.c | 59 ++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-)
arm/aarch64/include/kvm/kvm-cpu-arch.h +11 -1
@@ -5,12 +5,22 @@#include "arm-common/kvm-cpu-arch.h" -#define ARM_MPIDR_HWID_BITMASK 0xFF00FFFFFFUL#define ARM_CPU_ID 3, 0, 0, 0#define ARM_CPU_ID_MPIDR 5#define ARM_CPU_CTRL 3, 0, 1, 0#define ARM_CPU_CTRL_SCTLR_EL1 0 +#define ARM_MPIDR_HWID_BITMASK 0xFF00FFFFFFUL +#define ARM_MPIDR_LEVEL_BITS_SHIFT 3 +#define ARM_MPIDR_LEVEL_BITS (1 << ARM_MPIDR_LEVEL_BITS_SHIFT) +#define ARM_MPIDR_LEVEL_MASK ((1 << ARM_MPIDR_LEVEL_BITS) - 1) + +#define ARM_MPIDR_LEVEL_SHIFT (level) \ + (((1 << level) >> 1) << ARM_MPIDR_LEVEL_BITS_SHIFT) + +#define ARM_MPIDR_AFFINITY_LEVEL (mpidr, level) \ + ((mpidr >> ARM_MPIDR_LEVEL_SHIFT(level)) & ARM_MPIDR_LEVEL_MASK) +static inline __u64 __core_reg_id (__u64 offset) {__u64 id =KVM_REG_ARM64 |KVM_REG_ARM_CORE | offset;
arm/aarch64/psci.c +59 -0
83 lines not on this page · raw message
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
PATCH 19/21 aarch64: psci: Implement MIGRATE_INFO_TYPE
In reply to Oliver UptonLet the guest know that our PSCI implementation is entirely oblivious to the existence of a Trusted OS, and thus shouldn't care about it.
--- arm/aarch64/psci.c | 5 +++++ 1 file changed, 5 insertions(+)
arm/aarch64/psci.c +5 -0
@@ -32,6 +32,7 @@ static void psci_features(struct kvm_cpu *vcpu, struct arm_smccc_res *res)case PSCI_0_2_FN64_CPU_ON :case PSCI_0_2_FN_AFFINITY_INFO :case PSCI_0_2_FN64_AFFINITY_INFO : +case PSCI_0_2_FN_MIGRATE_INFO_TYPE :case ARM_SMCCC_VERSION_FUNC_ID : res->a0 =PSCI_RET_SUCCESS ;break ;
@@ -189,6 +190,10 @@ void handle_psci(struct kvm_cpu *vcpu, struct arm_smccc_res *res)case PSCI_0_2_FN64_AFFINITY_INFO :affinity_info (vcpu, res);break ; +case PSCI_0_2_FN_MIGRATE_INFO_TYPE : +/* Trusted OS not present */ + res->a0 =PSCI_0_2_TOS_MP ; +break ;default : res->a0 =PSCI_RET_NOT_SUPPORTED ; }
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
PATCH 20/21 aarch64: psci: Implement SYSTEM_{OFF,RESET}
In reply to Oliver UptonAdd support for the PSCI SYSTEM_{OFF,RESET} calls. Match the behavior of the SYSTEM_EVENT based implementation and just terminate the VM.
--- arm/aarch64/psci.c | 6 ++++++ 1 file changed, 6 insertions(+)
arm/aarch64/psci.c +6 -0
@@ -33,6 +33,8 @@ static void psci_features(struct kvm_cpu *vcpu, struct arm_smccc_res *res)case PSCI_0_2_FN_AFFINITY_INFO :case PSCI_0_2_FN64_AFFINITY_INFO :case PSCI_0_2_FN_MIGRATE_INFO_TYPE : +case PSCI_0_2_FN_SYSTEM_OFF : +case PSCI_0_2_FN_SYSTEM_RESET :case ARM_SMCCC_VERSION_FUNC_ID : res->a0 =PSCI_RET_SUCCESS ;break ;
@@ -194,6 +196,10 @@ void handle_psci(struct kvm_cpu *vcpu, struct arm_smccc_res *res)/* Trusted OS not present */ res->a0 =PSCI_0_2_TOS_MP ;break ; +case PSCI_0_2_FN_SYSTEM_OFF : +case PSCI_0_2_FN_SYSTEM_RESET : +kvm__reboot (vcpu->kvm); +break ;default : res->a0 =PSCI_RET_NOT_SUPPORTED ; }
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
PATCH 21/21 aarch64: smccc: Start sending PSCI to userspace
In reply to Oliver Uptonkvmtool now has a PSCI implementation that complies with v1.0 of the specification. Use the SMCCC filter to start sending these calls out to userspace for further handling. While at it, shut the door on the legacy, KVM-specific v0.1 functions.
--- arm/aarch64/include/kvm/kvm-config-arch.h | 6 +++- arm/aarch64/smccc.c | 37 +++++++++++++++++++++++ arm/include/arm-common/kvm-config-arch.h | 1 + 3 files changed, 43 insertions(+), 1 deletion(-)
arm/aarch64/include/kvm/kvm-config-arch.h +5 -1
@@ -19,7 +19,11 @@ int vcpu_affinity_parser(const struct option *opt, const char *arg, int unset);"Specify random seed for Kernel Address Space " \ "Layout Randomization (KASLR)"), \OPT_BOOLEAN ('\0' ,"no-pvtime" , &(cfg)->no_pvtime,"Disable" \ -" stolen time" ), +" stolen time" ), \ +OPT_BOOLEAN ('\0' ,"in-kernel-smccc" , &(cfg)->in_kernel_smccc, \ +"Disable userspace handling of SMCCC, instead" \ +" relying on the in-kernel implementation" ), + #include "arm-common/kvm-config-arch.h"#endif /* KVM__KVM_CONFIG_ARCH_H */
arm/aarch64/smccc.c +37 -0
@@ -39,7 +39,44 @@ out:return true; } +static struct kvm_smccc_filter filter_ranges[] = { + { + .base =KVM_PSCI_FN_BASE , + .nr_functions =4 , + .action =KVM_SMCCC_FILTER_DENY , + }, + { + .base =PSCI_0_2_FN_BASE , + .nr_functions =0x20 , + .action =KVM_SMCCC_FILTER_FWD_TO_USER , + }, + { + .base =PSCI_0_2_FN64_BASE , + .nr_functions =0x20 , + .action =KVM_SMCCC_FILTER_FWD_TO_USER , + }, +}; +void kvm__setup_smccc (struct kvm *kvm) { +struct kvm_device_attr attr = { + .group =KVM_ARM_VM_SMCCC_CTRL , + .attr =KVM_ARM_VM_SMCCC_FILTER , + }; +unsigned int i; +if (kvm->cfg.arch.in_kernel_smccc) +return ; + +if (ioctl (kvm->vm_fd,KVM_HAS_DEVICE_ATTR , &attr)) { +pr_debug ("KVM SMCCC filter not supported" ); +return ; + } + +for (i =0 ; i <ARRAY_SIZE (filter_ranges); i++) { + attr.addr = (u64)&filter_ranges[i]; + +if (ioctl (kvm->vm_fd,KVM_SET_DEVICE_ATTR , &attr)) +die_perror ("KVM_SET_DEVICE_ATTR failed" ); + } }
arm/include/arm-common/kvm-config-arch.h +1 -0
@@ -14,6 +14,7 @@ struct kvm_config_arch {enum irqchip_type irqchip;u64 fw_addr;bool no_pvtime; +bool in_kernel_smccc; };int irqchip_parser (const struct option *opt,const char *arg,int unset);
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
Joey Gouly
reviewerHi Oliver,
Quoted 10 lines · Oliver Upton
The 6.4 kernel picks up support for a generalized SMCCC filter, allowing userspace to select hypercall ranges that should be forwarded to userspace. This is a shameless attempt of making future SMCCC interfaces the responsibility of userspace :)
As a starting point, let's move PSCI up into userspace. KVM already leans on userspace for handling calls that have a system-wide effect.
Tested on linux-next with a 64 vCPU VM. Additionally, I took a stab at running kvm-unit-test's psci test, which passes.
I applied these patches to kvmtool and tested 6.4, however they don't work with an SVE enabled kernel/device.
# ./lkvm run -c 2 -m 4 -k psci.flat INFO: psci: PSCI version 1.0 PASS: psci: invalid-function PASS: psci: affinity-info-on PASS: psci: affinity-info-off Error: KVM_ARM_VCPU_FINALIZE: Operation not permitted Fatal: Unable to configure requested vcpu features
kvm_cpu__configure_features in kvmtool is failing because Linux returns an error if SVE was already finalised (arch/arm64/kvm/reset.c):
```
int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature)
{
switch (feature) {
case KVM_ARM_VCPU_SVE:
if (!vcpu_has_sve(vcpu))
return -EINVAL;
if (kvm_arm_vcpu_sve_finalized(vcpu))
return -EPERM; // <---- returns here
return kvm_vcpu_finalize_sve(vcpu);
}
return -EINVAL;
}
```It's not immediately obvious to me why finalising SVE twice is an error. Changing that to return 0; gets the test passing, but not sure if there are other implications.
I also booted with arm64.nosve and the test passed.
Thanks,
Joey
Hey Joey,
Thanks for the review and taking the patches for a spin.
Quoted 21 lines · Joey Gouly
kvm_cpu__configure_featuresin kvmtool is failing because Linux returns an error if SVE was already finalised (arch/arm64/kvm/reset.c):``` int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature) { switch (feature) { case KVM_ARM_VCPU_SVE: if (!vcpu_has_sve(vcpu)) return -EINVAL; if (kvm_arm_vcpu_sve_finalized(vcpu)) return -EPERM; // <---- returns here return kvm_vcpu_finalize_sve(vcpu); } return -EINVAL; } ```
Quoted 3 lines
It's not immediately obvious to me why finalising SVE twice is an error. Changing that to
return 0;gets the test passing, but not sure if there are other implications.
This is utterly mindless on my part, apologies. The SVE feature shouldn't be finalised (again). I'll probably drop patch 8 altogether and replace its usage with a direct call to KVM_ARM_VCPU_INIT.
Signature · 2 lines
Thanks, Oliver