Skip to content

Commit 6917fd7

Browse files
AKASHI TakahiroAlex Shi
authored andcommitted
arm64: kvm: allows kvm cpu hotplug
The current kvm implementation on arm64 does cpu-specific initialization at system boot, and has no way to gracefully shutdown a core in terms of kvm. This prevents kexec from rebooting the system at EL2. This patch adds a cpu tear-down function and also puts an existing cpu-init code into a separate function, kvm_arch_hardware_disable() and kvm_arch_hardware_enable() respectively. We don't need the arm64 specific cpu hotplug hook any more. Since this patch modifies common code between arm and arm64, one stub definition, __cpu_reset_hyp_mode(), is added on arm side to avoid compilation errors. Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org> [Rebase, added separate VHE init/exit path, changed resets use of kvm_call_hyp() to the __version, en/disabled hardware in init_subsystems(), added icache maintenance to __kvm_hyp_reset() and removed lr restore, removed guest-enter after teardown handling] Signed-off-by: James Morse <james.morse@arm.com> Acked-by: Marc Zyngier <marc.zyngier@arm.com> Signed-off-by: Will Deacon <will.deacon@arm.com> (cherry picked from commit 67f6919766620e7ea7aab11a6a3470dc7b451359) Signed-off-by: Alex Shi <alex.shi@linaro.org> Conflicts: arch/arm64/include/asm/kvm_host.h
1 parent 563a1d1 commit 6917fd7

9 files changed

Lines changed: 152 additions & 50 deletions

File tree

arch/arm/include/asm/kvm_host.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,15 @@ static inline void __cpu_init_stage2(void)
218218
{
219219
}
220220

221+
static inline void __cpu_reset_hyp_mode(phys_addr_t boot_pgd_ptr,
222+
phys_addr_t phys_idmap_start)
223+
{
224+
/*
225+
* TODO
226+
* kvm_call_reset(boot_pgd_ptr, phys_idmap_start);
227+
*/
228+
}
229+
221230
static inline int kvm_arch_dev_ioctl_check_extension(long ext)
222231
{
223232
return 0;
@@ -230,7 +239,6 @@ void kvm_mmu_wp_memory_region(struct kvm *kvm, int slot);
230239

231240
struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr);
232241

233-
static inline void kvm_arch_hardware_disable(void) {}
234242
static inline void kvm_arch_hardware_unsetup(void) {}
235243
static inline void kvm_arch_sync_events(struct kvm *kvm) {}
236244
static inline void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) {}

arch/arm/include/asm/kvm_mmu.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu);
6666
phys_addr_t kvm_mmu_get_httbr(void);
6767
phys_addr_t kvm_mmu_get_boot_httbr(void);
6868
phys_addr_t kvm_get_idmap_vector(void);
69+
phys_addr_t kvm_get_idmap_start(void);
6970
int kvm_mmu_init(void);
7071
void kvm_clear_hyp_idmap(void);
7172

arch/arm/kvm/arm.c

Lines changed: 71 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
* Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1717
*/
1818

19-
#include <linux/cpu.h>
2019
#include <linux/cpu_pm.h>
2120
#include <linux/errno.h>
2221
#include <linux/err.h>
@@ -65,6 +64,8 @@ static DEFINE_SPINLOCK(kvm_vmid_lock);
6564

6665
static bool vgic_present;
6766

67+
static DEFINE_PER_CPU(unsigned char, kvm_arm_hardware_enabled);
68+
6869
static void kvm_arm_set_running_vcpu(struct kvm_vcpu *vcpu)
6970
{
7071
BUG_ON(preemptible());
@@ -89,11 +90,6 @@ struct kvm_vcpu * __percpu *kvm_get_running_vcpus(void)
8990
return &kvm_arm_running_vcpu;
9091
}
9192

92-
int kvm_arch_hardware_enable(void)
93-
{
94-
return 0;
95-
}
96-
9793
int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
9894
{
9995
return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
@@ -964,11 +960,6 @@ long kvm_arch_vm_ioctl(struct file *filp,
964960
}
965961
}
966962

967-
static void cpu_init_stage2(void *dummy)
968-
{
969-
__cpu_init_stage2();
970-
}
971-
972963
static void cpu_init_hyp_mode(void *dummy)
973964
{
974965
phys_addr_t boot_pgd_ptr;
@@ -996,43 +987,87 @@ static void cpu_hyp_reinit(void)
996987
{
997988
if (is_kernel_in_hyp_mode()) {
998989
/*
999-
* cpu_init_stage2() is safe to call even if the PM
990+
* __cpu_init_stage2() is safe to call even if the PM
1000991
* event was cancelled before the CPU was reset.
1001992
*/
1002-
cpu_init_stage2(NULL);
993+
__cpu_init_stage2();
1003994
} else {
1004995
if (__hyp_get_vectors() == hyp_default_vectors)
1005996
cpu_init_hyp_mode(NULL);
1006997
}
1007998
}
1008999

1009-
static int hyp_init_cpu_notify(struct notifier_block *self,
1010-
unsigned long action, void *cpu)
1000+
static void cpu_hyp_reset(void)
1001+
{
1002+
phys_addr_t boot_pgd_ptr;
1003+
phys_addr_t phys_idmap_start;
1004+
1005+
if (!is_kernel_in_hyp_mode()) {
1006+
boot_pgd_ptr = kvm_mmu_get_boot_httbr();
1007+
phys_idmap_start = kvm_get_idmap_start();
1008+
1009+
__cpu_reset_hyp_mode(boot_pgd_ptr, phys_idmap_start);
1010+
}
1011+
}
1012+
1013+
static void _kvm_arch_hardware_enable(void *discard)
10111014
{
1012-
switch (action) {
1013-
case CPU_STARTING:
1014-
case CPU_STARTING_FROZEN:
1015+
if (!__this_cpu_read(kvm_arm_hardware_enabled)) {
10151016
cpu_hyp_reinit();
1017+
__this_cpu_write(kvm_arm_hardware_enabled, 1);
10161018
}
1019+
}
1020+
1021+
int kvm_arch_hardware_enable(void)
1022+
{
1023+
_kvm_arch_hardware_enable(NULL);
1024+
return 0;
1025+
}
10171026

1018-
return NOTIFY_OK;
1027+
static void _kvm_arch_hardware_disable(void *discard)
1028+
{
1029+
if (__this_cpu_read(kvm_arm_hardware_enabled)) {
1030+
cpu_hyp_reset();
1031+
__this_cpu_write(kvm_arm_hardware_enabled, 0);
1032+
}
10191033
}
10201034

1021-
static struct notifier_block hyp_init_cpu_nb = {
1022-
.notifier_call = hyp_init_cpu_notify,
1023-
};
1035+
void kvm_arch_hardware_disable(void)
1036+
{
1037+
_kvm_arch_hardware_disable(NULL);
1038+
}
10241039

10251040
#ifdef CONFIG_CPU_PM
10261041
static int hyp_init_cpu_pm_notifier(struct notifier_block *self,
10271042
unsigned long cmd,
10281043
void *v)
10291044
{
1030-
if (cmd == CPU_PM_EXIT) {
1031-
cpu_hyp_reinit();
1045+
/*
1046+
* kvm_arm_hardware_enabled is left with its old value over
1047+
* PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should
1048+
* re-enable hyp.
1049+
*/
1050+
switch (cmd) {
1051+
case CPU_PM_ENTER:
1052+
if (__this_cpu_read(kvm_arm_hardware_enabled))
1053+
/*
1054+
* don't update kvm_arm_hardware_enabled here
1055+
* so that the hardware will be re-enabled
1056+
* when we resume. See below.
1057+
*/
1058+
cpu_hyp_reset();
1059+
1060+
return NOTIFY_OK;
1061+
case CPU_PM_EXIT:
1062+
if (__this_cpu_read(kvm_arm_hardware_enabled))
1063+
/* The hardware was enabled before suspend. */
1064+
cpu_hyp_reinit();
1065+
10321066
return NOTIFY_OK;
1033-
}
10341067

1035-
return NOTIFY_DONE;
1068+
default:
1069+
return NOTIFY_DONE;
1070+
}
10361071
}
10371072

10381073
static struct notifier_block hyp_init_cpu_pm_nb = {
@@ -1067,18 +1102,12 @@ static int init_common_resources(void)
10671102

10681103
static int init_subsystems(void)
10691104
{
1070-
int err;
1105+
int err = 0;
10711106

10721107
/*
1073-
* Register CPU Hotplug notifier
1108+
* Enable hardware so that subsystem initialisation can access EL2.
10741109
*/
1075-
cpu_notifier_register_begin();
1076-
err = __register_cpu_notifier(&hyp_init_cpu_nb);
1077-
cpu_notifier_register_done();
1078-
if (err) {
1079-
kvm_err("Cannot register KVM init CPU notifier (%d)\n", err);
1080-
return err;
1081-
}
1110+
on_each_cpu(_kvm_arch_hardware_enable, NULL, 1);
10821111

10831112
/*
10841113
* Register CPU lower-power notifier
@@ -1096,22 +1125,26 @@ static int init_subsystems(void)
10961125
case -ENODEV:
10971126
case -ENXIO:
10981127
vgic_present = false;
1128+
err = 0;
10991129
break;
11001130
default:
1101-
return err;
1131+
goto out;
11021132
}
11031133

11041134
/*
11051135
* Init HYP architected timer support
11061136
*/
11071137
err = kvm_timer_hyp_init();
11081138
if (err)
1109-
return err;
1139+
goto out;
11101140

11111141
kvm_perf_init();
11121142
kvm_coproc_table_init();
11131143

1114-
return 0;
1144+
out:
1145+
on_each_cpu(_kvm_arch_hardware_disable, NULL, 1);
1146+
1147+
return err;
11151148
}
11161149

11171150
static void teardown_hyp_mode(void)
@@ -1128,11 +1161,6 @@ static void teardown_hyp_mode(void)
11281161

11291162
static int init_vhe_mode(void)
11301163
{
1131-
/*
1132-
* Execute the init code on each CPU.
1133-
*/
1134-
on_each_cpu(cpu_init_stage2, NULL, 1);
1135-
11361164
/* set size of VMID supported by CPU */
11371165
kvm_vmid_bits = kvm_get_vmid_bits();
11381166
kvm_info("%d-bit VMID\n", kvm_vmid_bits);
@@ -1219,11 +1247,6 @@ static int init_hyp_mode(void)
12191247
}
12201248
}
12211249

1222-
/*
1223-
* Execute the init code on each CPU.
1224-
*/
1225-
on_each_cpu(cpu_init_hyp_mode, NULL, 1);
1226-
12271250
#ifndef CONFIG_HOTPLUG_CPU
12281251
free_boot_hyp_pgd();
12291252
#endif

arch/arm/kvm/mmu.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,6 +1655,11 @@ phys_addr_t kvm_get_idmap_vector(void)
16551655
return hyp_idmap_vector;
16561656
}
16571657

1658+
phys_addr_t kvm_get_idmap_start(void)
1659+
{
1660+
return hyp_idmap_start;
1661+
}
1662+
16581663
int kvm_mmu_init(void)
16591664
{
16601665
int err;

arch/arm64/include/asm/kvm_asm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ struct kvm_vcpu;
5151

5252
extern char __kvm_hyp_init[];
5353
extern char __kvm_hyp_init_end[];
54+
extern char __kvm_hyp_reset[];
5455

5556
extern char __kvm_hyp_vector[];
5657

arch/arm64/include/asm/kvm_host.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
int __attribute_const__ kvm_target_cpu(void);
4545
int kvm_reset_vcpu(struct kvm_vcpu *vcpu);
4646
int kvm_arch_dev_ioctl_check_extension(long ext);
47+
phys_addr_t kvm_hyp_reset_entry(void);
4748

4849
struct kvm_arch {
4950
/* The VMID generation used for the virt. memory system */
@@ -330,7 +331,17 @@ static inline void __cpu_init_stage2(void)
330331
{
331332
}
332333

333-
static inline void kvm_arch_hardware_disable(void) {}
334+
static inline void __cpu_reset_hyp_mode(phys_addr_t boot_pgd_ptr,
335+
phys_addr_t phys_idmap_start)
336+
{
337+
/*
338+
* Call reset code, and switch back to stub hyp vectors.
339+
* Uses __kvm_call_hyp() to avoid kaslr's kvm_ksym_ref() translation.
340+
*/
341+
__kvm_call_hyp((void *)kvm_hyp_reset_entry(),
342+
boot_pgd_ptr, phys_idmap_start);
343+
}
344+
334345
static inline void kvm_arch_hardware_unsetup(void) {}
335346
static inline void kvm_arch_sync_events(struct kvm *kvm) {}
336347
static inline void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) {}

arch/arm64/include/asm/kvm_mmu.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu);
9999
phys_addr_t kvm_mmu_get_httbr(void);
100100
phys_addr_t kvm_mmu_get_boot_httbr(void);
101101
phys_addr_t kvm_get_idmap_vector(void);
102+
phys_addr_t kvm_get_idmap_start(void);
102103
int kvm_mmu_init(void);
103104
void kvm_clear_hyp_idmap(void);
104105

arch/arm64/kvm/hyp-init.S

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,44 @@ merged:
152152
eret
153153
ENDPROC(__kvm_hyp_init)
154154

155+
/*
156+
* x0: HYP boot pgd
157+
* x1: HYP phys_idmap_start
158+
*/
159+
ENTRY(__kvm_hyp_reset)
160+
/* We're in trampoline code in VA, switch back to boot page tables */
161+
msr ttbr0_el2, x0
162+
isb
163+
164+
/* Ensure the PA branch doesn't find a stale tlb entry or stale code. */
165+
ic iallu
166+
tlbi alle2
167+
dsb sy
168+
isb
169+
170+
/* Branch into PA space */
171+
adr x0, 1f
172+
bfi x1, x0, #0, #PAGE_SHIFT
173+
br x1
174+
175+
/* We're now in idmap, disable MMU */
176+
1: mrs x0, sctlr_el2
177+
ldr x1, =SCTLR_ELx_FLAGS
178+
bic x0, x0, x1 // Clear SCTL_M and etc
179+
msr sctlr_el2, x0
180+
isb
181+
182+
/* Invalidate the old TLBs */
183+
tlbi alle2
184+
dsb sy
185+
186+
/* Install stub vectors */
187+
adr_l x0, __hyp_stub_vectors
188+
msr vbar_el2, x0
189+
190+
eret
191+
ENDPROC(__kvm_hyp_reset)
192+
155193
.ltorg
156194

157195
.popsection

arch/arm64/kvm/reset.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
#include <asm/cputype.h>
3030
#include <asm/ptrace.h>
3131
#include <asm/kvm_arm.h>
32+
#include <asm/kvm_asm.h>
3233
#include <asm/kvm_coproc.h>
34+
#include <asm/kvm_mmu.h>
3335

3436
/*
3537
* ARMv8 Reset Values
@@ -123,3 +125,15 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu)
123125
/* Reset timer */
124126
return kvm_timer_vcpu_reset(vcpu, cpu_vtimer_irq);
125127
}
128+
129+
extern char __hyp_idmap_text_start[];
130+
131+
phys_addr_t kvm_hyp_reset_entry(void)
132+
{
133+
unsigned long offset;
134+
135+
offset = (unsigned long)__kvm_hyp_reset
136+
- ((unsigned long)__hyp_idmap_text_start & PAGE_MASK);
137+
138+
return TRAMPOLINE_VA + offset;
139+
}

0 commit comments

Comments
 (0)