* [RFC PATCH kvmtool 1/5] TESTONLY: Sync KVM headers with pending changes
2022-03-11 17:57 ` [RFC PATCH kvmtool 0/5] ARM: Implement PSCI SYSTEM_SUSPEND Oliver Upton
@ 2022-03-11 17:57 ` Oliver Upton
2022-03-11 17:57 ` [RFC PATCH kvmtool 2/5] Allow architectures to hook KVM_EXIT_SYSTEM_EVENT Oliver Upton
` (4 subsequent siblings)
5 siblings, 0 replies; 8+ messages in thread
From: Oliver Upton @ 2022-03-11 17:57 UTC (permalink / raw)
To: kvmarm
Cc: kvm, Marc Zyngier, James Morse, Alexandru Elisei,
Suzuki K Poulose, linux-arm-kernel, Peter Shier, Ricardo Koller,
Reiji Watanabe, Will Deacon, Julien Thierry, Oliver Upton
Signed-off-by: Oliver Upton <oupton@google.com>
---
include/linux/kvm.h | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git a/include/linux/kvm.h b/include/linux/kvm.h
index 1daa452..e5bb5f1 100644
--- a/include/linux/kvm.h
+++ b/include/linux/kvm.h
@@ -444,6 +444,8 @@ struct kvm_run {
#define KVM_SYSTEM_EVENT_SHUTDOWN 1
#define KVM_SYSTEM_EVENT_RESET 2
#define KVM_SYSTEM_EVENT_CRASH 3
+#define KVM_SYSTEM_EVENT_WAKEUP 4
+#define KVM_SYSTEM_EVENT_SUSPEND 5
__u32 type;
__u64 flags;
} system_event;
@@ -634,6 +636,7 @@ struct kvm_vapic_addr {
#define KVM_MP_STATE_OPERATING 7
#define KVM_MP_STATE_LOAD 8
#define KVM_MP_STATE_AP_RESET_HOLD 9
+#define KVM_MP_STATE_SUSPENDED 10
struct kvm_mp_state {
__u32 mp_state;
@@ -1131,6 +1134,10 @@ struct kvm_ppc_resize_hpt {
#define KVM_CAP_EXIT_ON_EMULATION_FAILURE 204
#define KVM_CAP_ARM_MTE 205
#define KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM 206
+#define KVM_CAP_VM_GPA_BITS 207
+#define KVM_CAP_XSAVE2 208
+#define KVM_CAP_SYS_ATTRIBUTES 209
+#define KVM_CAP_ARM_SYSTEM_SUSPEND 210
#ifdef KVM_CAP_IRQ_ROUTING
@@ -1162,11 +1169,20 @@ struct kvm_irq_routing_hv_sint {
__u32 sint;
};
+struct kvm_irq_routing_xen_evtchn {
+ __u32 port;
+ __u32 vcpu;
+ __u32 priority;
+};
+
+#define KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL ((__u32)(-1))
+
/* gsi routing entry types */
#define KVM_IRQ_ROUTING_IRQCHIP 1
#define KVM_IRQ_ROUTING_MSI 2
#define KVM_IRQ_ROUTING_S390_ADAPTER 3
#define KVM_IRQ_ROUTING_HV_SINT 4
+#define KVM_IRQ_ROUTING_XEN_EVTCHN 5
struct kvm_irq_routing_entry {
__u32 gsi;
@@ -1178,6 +1194,7 @@ struct kvm_irq_routing_entry {
struct kvm_irq_routing_msi msi;
struct kvm_irq_routing_s390_adapter adapter;
struct kvm_irq_routing_hv_sint hv_sint;
+ struct kvm_irq_routing_xen_evtchn xen_evtchn;
__u32 pad[8];
} u;
};
@@ -1208,6 +1225,7 @@ struct kvm_x86_mce {
#define KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL (1 << 1)
#define KVM_XEN_HVM_CONFIG_SHARED_INFO (1 << 2)
#define KVM_XEN_HVM_CONFIG_RUNSTATE (1 << 3)
+#define KVM_XEN_HVM_CONFIG_EVTCHN_2LEVEL (1 << 4)
struct kvm_xen_hvm_config {
__u32 flags;
@@ -2031,4 +2049,7 @@ struct kvm_stats_desc {
#define KVM_GET_STATS_FD _IO(KVMIO, 0xce)
+/* Available with KVM_CAP_XSAVE2 */
+#define KVM_GET_XSAVE2 _IOR(KVMIO, 0xcf, struct kvm_xsave)
+
#endif /* __LINUX_KVM_H */
--
2.35.1.723.g4982287a31-goog
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 8+ messages in thread* [RFC PATCH kvmtool 2/5] Allow architectures to hook KVM_EXIT_SYSTEM_EVENT
2022-03-11 17:57 ` [RFC PATCH kvmtool 0/5] ARM: Implement PSCI SYSTEM_SUSPEND Oliver Upton
2022-03-11 17:57 ` [RFC PATCH kvmtool 1/5] TESTONLY: Sync KVM headers with pending changes Oliver Upton
@ 2022-03-11 17:57 ` Oliver Upton
2022-03-11 17:57 ` [RFC PATCH kvmtool 3/5] ARM: Stash vcpu_init in the vCPU structure Oliver Upton
` (3 subsequent siblings)
5 siblings, 0 replies; 8+ messages in thread
From: Oliver Upton @ 2022-03-11 17:57 UTC (permalink / raw)
To: kvmarm
Cc: kvm, Marc Zyngier, James Morse, Alexandru Elisei,
Suzuki K Poulose, linux-arm-kernel, Peter Shier, Ricardo Koller,
Reiji Watanabe, Will Deacon, Julien Thierry, Oliver Upton
Certain system events require architecture-specific handling. Allow
architectures to intervene for exits unhandled by the default exit
handler.
Signed-off-by: Oliver Upton <oupton@google.com>
---
include/kvm/kvm-cpu.h | 1 +
kvm-cpu.c | 8 ++++++++
2 files changed, 9 insertions(+)
diff --git a/include/kvm/kvm-cpu.h b/include/kvm/kvm-cpu.h
index 0f16f8d..75e42d8 100644
--- a/include/kvm/kvm-cpu.h
+++ b/include/kvm/kvm-cpu.h
@@ -20,6 +20,7 @@ void kvm_cpu__run(struct kvm_cpu *vcpu);
int kvm_cpu__start(struct kvm_cpu *cpu);
bool kvm_cpu__handle_exit(struct kvm_cpu *vcpu);
int kvm_cpu__get_endianness(struct kvm_cpu *vcpu);
+bool kvm_cpu__arch_handle_system_event(struct kvm_cpu *vcpu);
int kvm_cpu__get_debug_fd(void);
void kvm_cpu__set_debug_fd(int fd);
diff --git a/kvm-cpu.c b/kvm-cpu.c
index 7dec088..d615c37 100644
--- a/kvm-cpu.c
+++ b/kvm-cpu.c
@@ -23,6 +23,11 @@ int __attribute__((weak)) kvm_cpu__get_endianness(struct kvm_cpu *vcpu)
return VIRTIO_ENDIAN_HOST;
}
+bool __attribute__((weak)) kvm_cpu__arch_handle_system_event(struct kvm_cpu *vcpu)
+{
+ return false;
+}
+
void kvm_cpu__enable_singlestep(struct kvm_cpu *vcpu)
{
struct kvm_guest_debug debug = {
@@ -224,6 +229,9 @@ int kvm_cpu__start(struct kvm_cpu *cpu)
*/
switch (cpu->kvm_run->system_event.type) {
default:
+ if (kvm_cpu__arch_handle_system_event(cpu))
+ break;
+
pr_warning("unknown system event type %d",
cpu->kvm_run->system_event.type);
/* fall through for now */
--
2.35.1.723.g4982287a31-goog
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 8+ messages in thread* [RFC PATCH kvmtool 3/5] ARM: Stash vcpu_init in the vCPU structure
2022-03-11 17:57 ` [RFC PATCH kvmtool 0/5] ARM: Implement PSCI SYSTEM_SUSPEND Oliver Upton
2022-03-11 17:57 ` [RFC PATCH kvmtool 1/5] TESTONLY: Sync KVM headers with pending changes Oliver Upton
2022-03-11 17:57 ` [RFC PATCH kvmtool 2/5] Allow architectures to hook KVM_EXIT_SYSTEM_EVENT Oliver Upton
@ 2022-03-11 17:57 ` Oliver Upton
2022-03-11 17:57 ` [RFC PATCH kvmtool 4/5] ARM: Add a helper to re-init a vCPU Oliver Upton
` (2 subsequent siblings)
5 siblings, 0 replies; 8+ messages in thread
From: Oliver Upton @ 2022-03-11 17:57 UTC (permalink / raw)
To: kvmarm
Cc: kvm, Marc Zyngier, James Morse, Alexandru Elisei,
Suzuki K Poulose, linux-arm-kernel, Peter Shier, Ricardo Koller,
Reiji Watanabe, Will Deacon, Julien Thierry, Oliver Upton
A subsequent change to kvmtool will require that a vCPU be reset more
than once. Derive a valid target/feature set exactly once and stash that
for later use.
Signed-off-by: Oliver Upton <oupton@google.com>
---
arm/include/arm-common/kvm-cpu-arch.h | 18 +++++++++---------
arm/kvm-cpu.c | 2 +-
2 files changed, 10 insertions(+), 10 deletions(-)
diff --git a/arm/include/arm-common/kvm-cpu-arch.h b/arm/include/arm-common/kvm-cpu-arch.h
index 923d2c4..4027afe 100644
--- a/arm/include/arm-common/kvm-cpu-arch.h
+++ b/arm/include/arm-common/kvm-cpu-arch.h
@@ -10,18 +10,18 @@ struct kvm;
struct kvm_cpu {
pthread_t thread;
- unsigned long cpu_id;
- unsigned long cpu_type;
- const char *cpu_compatible;
+ unsigned long cpu_id;
+ struct kvm_vcpu_init init;
+ const char *cpu_compatible;
- struct kvm *kvm;
- int vcpu_fd;
- struct kvm_run *kvm_run;
+ struct kvm *kvm;
+ int vcpu_fd;
+ struct kvm_run *kvm_run;
struct kvm_cpu_task *task;
- u8 is_running;
- u8 paused;
- u8 needs_nmi;
+ u8 is_running;
+ u8 paused;
+ u8 needs_nmi;
struct kvm_coalesced_mmio_ring *ring;
diff --git a/arm/kvm-cpu.c b/arm/kvm-cpu.c
index 6a2408c..1ea56bb 100644
--- a/arm/kvm-cpu.c
+++ b/arm/kvm-cpu.c
@@ -128,7 +128,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;
--
2.35.1.723.g4982287a31-goog
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 8+ messages in thread* [RFC PATCH kvmtool 4/5] ARM: Add a helper to re-init a vCPU
2022-03-11 17:57 ` [RFC PATCH kvmtool 0/5] ARM: Implement PSCI SYSTEM_SUSPEND Oliver Upton
` (2 preceding siblings ...)
2022-03-11 17:57 ` [RFC PATCH kvmtool 3/5] ARM: Stash vcpu_init in the vCPU structure Oliver Upton
@ 2022-03-11 17:57 ` Oliver Upton
2022-03-11 17:57 ` [RFC PATCH kvmtool 5/5] ARM: Implement PSCI SYSTEM_SUSPEND Oliver Upton
2022-05-06 13:01 ` [RFC PATCH kvmtool 0/5] " Will Deacon
5 siblings, 0 replies; 8+ messages in thread
From: Oliver Upton @ 2022-03-11 17:57 UTC (permalink / raw)
To: kvmarm
Cc: kvm, Marc Zyngier, James Morse, Alexandru Elisei,
Suzuki K Poulose, linux-arm-kernel, Peter Shier, Ricardo Koller,
Reiji Watanabe, Will Deacon, Julien Thierry, Oliver Upton
Create a helper that can be called to reinitialize a particular vCPU, or
in other words issue the KVM_ARM_VCPU_INIT and KVM_ARM_VCPU_FINALIZE
ioctls accordingly.
Make use of the helper from kvm_cpu__arch_init() after the correct
target/feature set have been identified. Calling KVM_ARM_VCPU_INIT with
the same target more than once is benign.
Signed-off-by: Oliver Upton <oupton@google.com>
---
arm/kvm-cpu.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/arm/kvm-cpu.c b/arm/kvm-cpu.c
index 1ea56bb..164e399 100644
--- a/arm/kvm-cpu.c
+++ b/arm/kvm-cpu.c
@@ -35,6 +35,15 @@ int kvm_cpu__register_kvm_arm_target(struct kvm_arm_target *target)
return -ENOSPC;
}
+static void kvm_cpu__arch_reinit(struct kvm_cpu *vcpu)
+{
+ if (ioctl(vcpu->vcpu_fd, KVM_ARM_VCPU_INIT, &vcpu->init) < 0)
+ die("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;
@@ -132,8 +141,7 @@ struct kvm_cpu *kvm_cpu__arch_init(struct kvm *kvm, unsigned long cpu_id)
vcpu->cpu_compatible = target->compatible;
vcpu->is_running = true;
- if (kvm_cpu__configure_features(vcpu))
- die("Unable to configure requested vcpu features");
+ kvm_cpu__arch_reinit(vcpu);
return vcpu;
}
--
2.35.1.723.g4982287a31-goog
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 8+ messages in thread* [RFC PATCH kvmtool 5/5] ARM: Implement PSCI SYSTEM_SUSPEND
2022-03-11 17:57 ` [RFC PATCH kvmtool 0/5] ARM: Implement PSCI SYSTEM_SUSPEND Oliver Upton
` (3 preceding siblings ...)
2022-03-11 17:57 ` [RFC PATCH kvmtool 4/5] ARM: Add a helper to re-init a vCPU Oliver Upton
@ 2022-03-11 17:57 ` Oliver Upton
2022-05-06 13:01 ` [RFC PATCH kvmtool 0/5] " Will Deacon
5 siblings, 0 replies; 8+ messages in thread
From: Oliver Upton @ 2022-03-11 17:57 UTC (permalink / raw)
To: kvmarm
Cc: kvm, Marc Zyngier, James Morse, Alexandru Elisei,
Suzuki K Poulose, linux-arm-kernel, Peter Shier, Ricardo Koller,
Reiji Watanabe, Will Deacon, Julien Thierry, Oliver Upton
KVM_CAP_ARM_SYSTEM_SUSPEND allows VMMs to trap guest attempts to use the
PSCI SYSTEM_SUSPEND hypercall. Make use of that capability in KVM tool
to implement guest suspend support.
Add some minimal SMCCC register handling (params, return values) for
AArch32 and AArch64. Perform only the required sanity check before
suspending the VM by ensuring all other vCPUs besides the caller are
powered off. Leverage KVM_MP_STATE_SUSPENDED to emulate the suspend as
an architectural WFI.
Signed-off-by: Oliver Upton <oupton@google.com>
---
arm/aarch32/kvm-cpu.c | 72 ++++++++++++++++++++++++
arm/aarch64/kvm-cpu.c | 66 ++++++++++++++++++++++
arm/include/arm-common/kvm-cpu-arch.h | 5 ++
arm/kvm-cpu.c | 81 +++++++++++++++++++++++++++
arm/kvm.c | 9 +++
5 files changed, 233 insertions(+)
diff --git a/arm/aarch32/kvm-cpu.c b/arm/aarch32/kvm-cpu.c
index 95fb1da..dd5f70f 100644
--- a/arm/aarch32/kvm-cpu.c
+++ b/arm/aarch32/kvm-cpu.c
@@ -130,3 +130,75 @@ 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);
}
+
+u64 kvm_cpu__smccc_get_arg1(struct kvm_vcpu *vcpu)
+{
+ struct kvm_one_reg reg;
+ u32 data;
+
+ reg.addr = (u64)&data;
+ reg.id = ARM_CORE_REG(usr_regs.ARM_r1);
+ if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0)
+ die("KVM_GET_ONE_REG failed (r1)");
+
+ return data;
+}
+
+u64 kvm_cpu__smccc_get_arg2(struct kvm_vcpu *vcpu)
+{
+ struct kvm_one_reg reg;
+ u32 data;
+
+ reg.addr = (u64)&data;
+ reg.id = ARM_CORE_REG(usr_regs.ARM_r2);
+ if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0)
+ die("KVM_GET_ONE_REG failed (r2)");
+
+ return data;
+}
+
+void kvm_cpu__smccc_return(struct kvm_cpu *vcpu, u64 a0, u64 a1, u64 a2, u64 a3)
+{
+ struct kvm_one_reg reg;
+ u32 data;
+
+ reg.addr = (u64)&data;
+
+ data = (u32)a0;
+ reg.id = ARM_CORE_REG(usr_regs.ARM_r0);
+ if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0)
+ die("KVM_SET_ONE_REG failed (r0)");
+
+ data = (u32)a1;
+ reg.id = ARM_CORE_REG(usr_regs.ARM_r1);
+ if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0)
+ die("KVM_SET_ONE_REG failed (r1)");
+
+ data = (u32)a2;
+ reg.id = ARM_CORE_REG(usr_regs.ARM_r2);
+ if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0)
+ die("KVM_SET_ONE_REG failed (r2)");
+
+ data = (u32)a3;
+ reg.id = ARM_CORE_REG(usr_regs.ARM_r3);
+ if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0)
+ die("KVM_SET_ONE_REG failed (r3)");
+}
+
+void kvm_cpu__psci_set_entry(struct kvm_cpu *vcpu, u64 entry_addr, u64 context_id)
+{
+ struct kvm_one_reg reg;
+ u32 data;
+
+ reg.addr = (u64)&data;
+
+ data = (u32)entry_addr;
+ reg.id = ARM_CORE_REG(usr_regs.ARM_pc);
+ if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0)
+ die("KVM_SET_ONE_REG failed (pc)");
+
+ data = (u32)context_id;
+ reg.id = ARM64_CORE_REG(usr_regs.ARM_r0);
+ if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0)
+ die("KVM_SET_ONE_REG failed (r0)");
+}
diff --git a/arm/aarch64/kvm-cpu.c b/arm/aarch64/kvm-cpu.c
index 9f3e858..3118c54 100644
--- a/arm/aarch64/kvm-cpu.c
+++ b/arm/aarch64/kvm-cpu.c
@@ -254,3 +254,69 @@ void kvm_cpu__show_registers(struct kvm_cpu *vcpu)
die("KVM_GET_ONE_REG failed (lr)");
dprintf(debug_fd, " LR: 0x%lx\n", data);
}
+
+u64 kvm_cpu__smccc_get_arg1(struct kvm_cpu *vcpu)
+{
+ struct kvm_one_reg reg;
+ u64 data;
+
+ reg.addr = (u64)&data;
+ reg.id = ARM64_CORE_REG(regs.regs[1]);
+ if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0)
+ die("KVM_GET_ONE_REG failed (x1)");
+
+ return data;
+}
+
+u64 kvm_cpu__smccc_get_arg2(struct kvm_cpu *vcpu)
+{
+ struct kvm_one_reg reg;
+ u64 data;
+
+ reg.addr = (u64)&data;
+ reg.id = ARM64_CORE_REG(regs.regs[2]);
+ if (ioctl(vcpu->vcpu_fd, KVM_GET_ONE_REG, ®) < 0)
+ die("KVM_GET_ONE_REG failed (x2)");
+
+ return data;
+}
+
+void kvm_cpu__smccc_return(struct kvm_cpu *vcpu, u64 a0, u64 a1, u64 a2, u64 a3)
+{
+ struct kvm_one_reg reg;
+
+ reg.addr = (u64)&a0;
+ reg.id = ARM64_CORE_REG(regs.regs[0]);
+ if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0)
+ die("KVM_SET_ONE_REG failed (x0)");
+
+ reg.addr = (u64)&a1;
+ reg.id = ARM64_CORE_REG(regs.regs[1]);
+ if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0)
+ die("KVM_SET_ONE_REG failed (x1)");
+
+ reg.addr = (u64)&a2;
+ reg.id = ARM64_CORE_REG(regs.regs[2]);
+ if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0)
+ die("KVM_SET_ONE_REG failed (x2)");
+
+ reg.addr = (u64)&a3;
+ reg.id = ARM64_CORE_REG(regs.regs[3]);
+ if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0)
+ die("KVM_SET_ONE_REG failed (x3)");
+}
+
+void kvm_cpu__psci_set_entry(struct kvm_cpu *vcpu, u64 entry_addr, u64 context_id)
+{
+ struct kvm_one_reg reg;
+
+ reg.addr = (u64)&entry_addr;
+ reg.id = ARM64_CORE_REG(regs.pc);
+ if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0)
+ die("KVM_SET_ONE_REG failed (pc)");
+
+ reg.addr = (u64)&context_id;
+ reg.id = ARM64_CORE_REG(regs.regs[0]);
+ if (ioctl(vcpu->vcpu_fd, KVM_SET_ONE_REG, ®) < 0)
+ die("KVM_SET_ONE_REG failed (x0)");
+}
diff --git a/arm/include/arm-common/kvm-cpu-arch.h b/arm/include/arm-common/kvm-cpu-arch.h
index 4027afe..17179c2 100644
--- a/arm/include/arm-common/kvm-cpu-arch.h
+++ b/arm/include/arm-common/kvm-cpu-arch.h
@@ -59,4 +59,9 @@ static inline bool kvm_cpu__emulate_mmio(struct kvm_cpu *vcpu, u64 phys_addr,
unsigned long kvm_cpu__get_vcpu_mpidr(struct kvm_cpu *vcpu);
+u64 kvm_cpu__smccc_get_arg1(struct kvm_cpu *vcpu);
+u64 kvm_cpu__smccc_get_arg2(struct kvm_cpu *vcpu);
+void kvm_cpu__smccc_return(struct kvm_cpu *vcpu, u64 a0, u64 a1, u64 a2, u64 a3);
+void kvm_cpu__psci_set_entry(struct kvm_cpu *vcpu, u64 entry_addr, u64 context_id);
+
#endif /* ARM_COMMON__KVM_CPU_ARCH_H */
diff --git a/arm/kvm-cpu.c b/arm/kvm-cpu.c
index 164e399..677f8bb 100644
--- a/arm/kvm-cpu.c
+++ b/arm/kvm-cpu.c
@@ -163,3 +163,84 @@ bool kvm_cpu__handle_exit(struct kvm_cpu *vcpu)
void kvm_cpu__show_page_tables(struct kvm_cpu *vcpu)
{
}
+
+static void kvm_cpu__arm_suspend(struct kvm_cpu *vcpu)
+{
+ struct kvm_mp_state mp_state = {
+ .mp_state = KVM_MP_STATE_SUSPENDED,
+ };
+
+ if (ioctl(vcpu->vcpu_fd, KVM_SET_MP_STATE, &mp_state) < 0)
+ die("KVM_SET_MP_STATE failed");
+}
+
+static void kvm_cpu__wakeup(struct kvm_cpu *vcpu)
+{
+ u64 entry_addr, context_id;
+
+ entry_addr = kvm_cpu__smccc_get_arg1(vcpu);
+ context_id = kvm_cpu__smccc_get_arg2(vcpu);
+
+ /*
+ * The resuming CPU could have been a secondary CPU at boot. Ensure the
+ * vCPU is made runnable.
+ */
+ vcpu->init.features[0] &= ~(1ul << KVM_ARM_VCPU_POWER_OFF);
+
+ kvm_cpu__arch_reinit(vcpu);
+ kvm_cpu__reset_vcpu(vcpu);
+ kvm_cpu__psci_set_entry(vcpu, entry_addr, context_id);
+}
+
+static void kvm_cpu__psci_system_suspend(struct kvm_cpu *vcpu)
+{
+ struct kvm *kvm = vcpu->kvm;
+ bool denied = false;
+ int i;
+
+ /*
+ * Mark the caller as paused before actually pausing the VM. This avoids
+ * the hazard of attempting to acquire the pause_lock in the SIGKVMPAUSE
+ * handler from the thread that already holds it.
+ */
+ vcpu->paused = 1;
+
+ kvm__pause(kvm);
+ for (i = 0; i < kvm->nrcpus; i++) {
+ struct kvm_cpu *tmp = kvm->cpus[i];
+ struct kvm_mp_state mp_state;
+
+ if (vcpu == tmp)
+ continue;
+
+ if (ioctl(tmp->vcpu_fd, KVM_GET_MP_STATE, &mp_state) < 0)
+ die("KVM_GET_MP_STATE failed");
+
+ if (mp_state.mp_state != KVM_MP_STATE_STOPPED) {
+ denied = true;
+ break;
+ }
+ }
+
+ if (!denied)
+ kvm_cpu__arm_suspend(vcpu);
+ else
+ kvm_cpu__smccc_return(vcpu, PSCI_RET_DENIED, 0, 0, 0);
+
+ vcpu->paused = 0;
+ kvm__continue(kvm);
+}
+
+bool kvm_cpu__arch_handle_system_event(struct kvm_cpu *vcpu)
+{
+ switch (vcpu->kvm_run->system_event.type) {
+ case KVM_SYSTEM_EVENT_SUSPEND:
+ kvm_cpu__psci_system_suspend(vcpu);
+ return true;
+ case KVM_SYSTEM_EVENT_WAKEUP:
+ kvm_cpu__wakeup(vcpu);
+ return true;
+ default:
+ return false;
+ }
+}
diff --git a/arm/kvm.c b/arm/kvm.c
index 80d233f..8ae4711 100644
--- a/arm/kvm.c
+++ b/arm/kvm.c
@@ -86,6 +86,15 @@ void kvm__arch_init(struct kvm *kvm, const char *hugetlbfs_path, u64 ram_size)
/* Create the virtual GIC. */
if (gic__create(kvm, kvm->cfg.arch.irqchip))
die("Failed to create virtual GIC");
+
+ if (kvm__supports_extension(kvm, KVM_CAP_ARM_SYSTEM_SUSPEND)) {
+ struct kvm_enable_cap cap = {
+ .cap = KVM_CAP_ARM_SYSTEM_SUSPEND,
+ };
+
+ if (ioctl(kvm->vm_fd, KVM_ENABLE_CAP, &cap) < 0)
+ die("Enabling KVM_CAP_ARM_SYSTEM_SUSPEND failed");
+ }
}
#define FDT_ALIGN SZ_2M
--
2.35.1.723.g4982287a31-goog
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [RFC PATCH kvmtool 0/5] ARM: Implement PSCI SYSTEM_SUSPEND
2022-03-11 17:57 ` [RFC PATCH kvmtool 0/5] ARM: Implement PSCI SYSTEM_SUSPEND Oliver Upton
` (4 preceding siblings ...)
2022-03-11 17:57 ` [RFC PATCH kvmtool 5/5] ARM: Implement PSCI SYSTEM_SUSPEND Oliver Upton
@ 2022-05-06 13:01 ` Will Deacon
2022-05-06 19:18 ` Oliver Upton
5 siblings, 1 reply; 8+ messages in thread
From: Will Deacon @ 2022-05-06 13:01 UTC (permalink / raw)
To: Oliver Upton
Cc: kvmarm, kvm, Marc Zyngier, James Morse, Alexandru Elisei,
Suzuki K Poulose, linux-arm-kernel, Peter Shier, Ricardo Koller,
Reiji Watanabe, Julien Thierry
On Fri, Mar 11, 2022 at 05:57:12PM +0000, Oliver Upton wrote:
> This is a prototype for supporting KVM_CAP_ARM_SYSTEM_SUSPEND on
> kvmtool. The capability allows userspace to expose the SYSTEM_SUSPEND
> PSCI call to its guests.
>
> Implement SYSTEM_SUSPEND using KVM_MP_STATE_SUSPENDED, which emulates
> the execution of a WFI instruction in the kernel. Resume the guest when
> a wakeup event is recognized and reset it to the requested entry address
> and context ID.
>
> Patches 2-4 are small reworks to more easily shoehorn PSCI support into
> kvmtool.
>
> Patch 5 adds some SMCCC handlers and makes use of them to implement PSCI
> SYSTEM_SUSPEND. For now, just check the bare-minimum, that all vCPUs
> besides the caller have stopped. There are also checks that can be made
> against the requested entry address, but they are at the discretion of
> the implementation.
>
> Tested with 'echo mem > /sys/power/state' to see that the vCPU is in
> fact placed in a suspended state for the PSCI call. Hacked the switch
> statement to fall through to WAKEUP immediately after to verify the vCPU
> is set up correctly for resume.
>
> It would be nice if kvmtool actually provided a device good for wakeups,
> since the RTC implementation has omitted any interrupt support.
>
> kernel changes: http://lore.kernel.org/r/20220311174001.605719-1-oupton@google.com
>
> Oliver Upton (5):
> TESTONLY: Sync KVM headers with pending changes
> Allow architectures to hook KVM_EXIT_SYSTEM_EVENT
> ARM: Stash vcpu_init in the vCPU structure
> ARM: Add a helper to re-init a vCPU
> ARM: Implement PSCI SYSTEM_SUSPEND
>
> arm/aarch32/kvm-cpu.c | 72 ++++++++++++++++++++
> arm/aarch64/kvm-cpu.c | 66 +++++++++++++++++++
> arm/include/arm-common/kvm-cpu-arch.h | 23 ++++---
> arm/kvm-cpu.c | 95 ++++++++++++++++++++++++++-
> arm/kvm.c | 9 +++
> include/kvm/kvm-cpu.h | 1 +
> include/linux/kvm.h | 21 ++++++
> kvm-cpu.c | 8 +++
> 8 files changed, 283 insertions(+), 12 deletions(-)
Looks like the kernel-side changes are queued now, so please can you resend
this series? I also think you can drop the AArch32 support, unless you see a
compelling reason for it?
Will
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [RFC PATCH kvmtool 0/5] ARM: Implement PSCI SYSTEM_SUSPEND
2022-05-06 13:01 ` [RFC PATCH kvmtool 0/5] " Will Deacon
@ 2022-05-06 19:18 ` Oliver Upton
0 siblings, 0 replies; 8+ messages in thread
From: Oliver Upton @ 2022-05-06 19:18 UTC (permalink / raw)
To: Will Deacon
Cc: kvmarm, kvm, Marc Zyngier, James Morse, Alexandru Elisei,
Suzuki K Poulose, linux-arm-kernel, Peter Shier, Ricardo Koller,
Reiji Watanabe, Julien Thierry
Hi Will,
On Fri, May 6, 2022 at 6:01 AM Will Deacon <will@kernel.org> wrote:
>
> Looks like the kernel-side changes are queued now, so please can you resend
> this series? I also think you can drop the AArch32 support, unless you see a
> compelling reason for it?
You bet. I was going to wait for 5.19-rc1 just for the dust to settle
and get a stable number for the UAPI bits. I think the ARM changes
have some light conflicts with SEV work in Paolo's tree.
All for dropping AArch32, means less work for me :-P
--
Thanks,
Oliver
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 8+ messages in thread