]>
Commit | Line | Data |
---|---|---|
d94d71cb | 1 | // SPDX-License-Identifier: GPL-2.0-only |
749cf76c CD |
2 | /* |
3 | * Copyright (C) 2012 - Virtual Open Systems and Columbia University | |
4 | * Author: Christoffer Dall <c.dall@virtualopensystems.com> | |
749cf76c CD |
5 | */ |
6 | ||
85acda3b | 7 | #include <linux/bug.h> |
1fcf7ce0 | 8 | #include <linux/cpu_pm.h> |
749cf76c CD |
9 | #include <linux/errno.h> |
10 | #include <linux/err.h> | |
11 | #include <linux/kvm_host.h> | |
1085fdc6 | 12 | #include <linux/list.h> |
749cf76c CD |
13 | #include <linux/module.h> |
14 | #include <linux/vmalloc.h> | |
15 | #include <linux/fs.h> | |
16 | #include <linux/mman.h> | |
17 | #include <linux/sched.h> | |
86ce8535 | 18 | #include <linux/kvm.h> |
2412405b EA |
19 | #include <linux/kvm_irqfd.h> |
20 | #include <linux/irqbypass.h> | |
de737089 | 21 | #include <linux/sched/stat.h> |
749cf76c | 22 | #include <trace/events/kvm.h> |
b02386eb | 23 | #include <kvm/arm_pmu.h> |
1a2fb94e | 24 | #include <kvm/arm_psci.h> |
749cf76c CD |
25 | |
26 | #define CREATE_TRACE_POINTS | |
27 | #include "trace.h" | |
28 | ||
7c0f6ba6 | 29 | #include <linux/uaccess.h> |
749cf76c CD |
30 | #include <asm/ptrace.h> |
31 | #include <asm/mman.h> | |
342cd0ab | 32 | #include <asm/tlbflush.h> |
5b3e5e5b | 33 | #include <asm/cacheflush.h> |
85acda3b | 34 | #include <asm/cpufeature.h> |
342cd0ab CD |
35 | #include <asm/virt.h> |
36 | #include <asm/kvm_arm.h> | |
37 | #include <asm/kvm_asm.h> | |
38 | #include <asm/kvm_mmu.h> | |
f7ed45be | 39 | #include <asm/kvm_emulate.h> |
5b3e5e5b | 40 | #include <asm/kvm_coproc.h> |
910917bb | 41 | #include <asm/sections.h> |
749cf76c CD |
42 | |
43 | #ifdef REQUIRES_VIRT | |
44 | __asm__(".arch_extension virt"); | |
45 | #endif | |
46 | ||
630a1685 | 47 | DEFINE_PER_CPU(kvm_host_data_t, kvm_host_data); |
342cd0ab | 48 | static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page); |
342cd0ab | 49 | |
1638a12d MZ |
50 | /* Per-CPU variable containing the currently running vcpu. */ |
51 | static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_arm_running_vcpu); | |
52 | ||
f7ed45be CD |
53 | /* The VMID used in the VTTBR */ |
54 | static atomic64_t kvm_vmid_gen = ATOMIC64_INIT(1); | |
20475f78 | 55 | static u32 kvm_next_vmid; |
fb544d1c | 56 | static DEFINE_SPINLOCK(kvm_vmid_lock); |
342cd0ab | 57 | |
c7da6fa4 PF |
58 | static bool vgic_present; |
59 | ||
67f69197 AT |
60 | static DEFINE_PER_CPU(unsigned char, kvm_arm_hardware_enabled); |
61 | ||
1638a12d MZ |
62 | static void kvm_arm_set_running_vcpu(struct kvm_vcpu *vcpu) |
63 | { | |
1436c1aa | 64 | __this_cpu_write(kvm_arm_running_vcpu, vcpu); |
1638a12d MZ |
65 | } |
66 | ||
61bbe380 CD |
67 | DEFINE_STATIC_KEY_FALSE(userspace_irqchip_in_use); |
68 | ||
1638a12d MZ |
69 | /** |
70 | * kvm_arm_get_running_vcpu - get the vcpu running on the current CPU. | |
71 | * Must be called from non-preemptible context | |
72 | */ | |
73 | struct kvm_vcpu *kvm_arm_get_running_vcpu(void) | |
74 | { | |
1436c1aa | 75 | return __this_cpu_read(kvm_arm_running_vcpu); |
1638a12d MZ |
76 | } |
77 | ||
78 | /** | |
79 | * kvm_arm_get_running_vcpus - get the per-CPU array of currently running vcpus. | |
80 | */ | |
4000be42 | 81 | struct kvm_vcpu * __percpu *kvm_get_running_vcpus(void) |
1638a12d MZ |
82 | { |
83 | return &kvm_arm_running_vcpu; | |
84 | } | |
85 | ||
749cf76c CD |
86 | int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) |
87 | { | |
88 | return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE; | |
89 | } | |
90 | ||
749cf76c CD |
91 | int kvm_arch_hardware_setup(void) |
92 | { | |
93 | return 0; | |
94 | } | |
95 | ||
f257d6dc | 96 | int kvm_arch_check_processor_compat(void) |
749cf76c | 97 | { |
f257d6dc | 98 | return 0; |
749cf76c CD |
99 | } |
100 | ||
749cf76c | 101 | |
d5d8184d CD |
102 | /** |
103 | * kvm_arch_init_vm - initializes a VM data structure | |
104 | * @kvm: pointer to the KVM struct | |
105 | */ | |
749cf76c CD |
106 | int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) |
107 | { | |
94d0e598 | 108 | int ret, cpu; |
d5d8184d | 109 | |
bca607eb | 110 | ret = kvm_arm_setup_stage2(kvm, type); |
5b6c6742 SP |
111 | if (ret) |
112 | return ret; | |
749cf76c | 113 | |
94d0e598 MZ |
114 | kvm->arch.last_vcpu_ran = alloc_percpu(typeof(*kvm->arch.last_vcpu_ran)); |
115 | if (!kvm->arch.last_vcpu_ran) | |
116 | return -ENOMEM; | |
117 | ||
118 | for_each_possible_cpu(cpu) | |
119 | *per_cpu_ptr(kvm->arch.last_vcpu_ran, cpu) = -1; | |
120 | ||
d5d8184d CD |
121 | ret = kvm_alloc_stage2_pgd(kvm); |
122 | if (ret) | |
123 | goto out_fail_alloc; | |
124 | ||
c8dddecd | 125 | ret = create_hyp_mappings(kvm, kvm + 1, PAGE_HYP); |
d5d8184d CD |
126 | if (ret) |
127 | goto out_free_stage2_pgd; | |
128 | ||
6c3d63c9 | 129 | kvm_vgic_early_init(kvm); |
a1a64387 | 130 | |
d5d8184d | 131 | /* Mark the initial VMID generation invalid */ |
e329fb75 | 132 | kvm->arch.vmid.vmid_gen = 0; |
d5d8184d | 133 | |
3caa2d8c | 134 | /* The maximum number of VCPUs is limited by the host's GIC model */ |
c7da6fa4 PF |
135 | kvm->arch.max_vcpus = vgic_present ? |
136 | kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS; | |
3caa2d8c | 137 | |
d5d8184d CD |
138 | return ret; |
139 | out_free_stage2_pgd: | |
140 | kvm_free_stage2_pgd(kvm); | |
141 | out_fail_alloc: | |
94d0e598 MZ |
142 | free_percpu(kvm->arch.last_vcpu_ran); |
143 | kvm->arch.last_vcpu_ran = NULL; | |
d5d8184d | 144 | return ret; |
749cf76c CD |
145 | } |
146 | ||
235539b4 LC |
147 | bool kvm_arch_has_vcpu_debugfs(void) |
148 | { | |
149 | return false; | |
150 | } | |
151 | ||
152 | int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu) | |
153 | { | |
154 | return 0; | |
155 | } | |
156 | ||
1499fa80 | 157 | vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) |
749cf76c CD |
158 | { |
159 | return VM_FAULT_SIGBUS; | |
160 | } | |
161 | ||
749cf76c | 162 | |
d5d8184d CD |
163 | /** |
164 | * kvm_arch_destroy_vm - destroy the VM data structure | |
165 | * @kvm: pointer to the KVM struct | |
166 | */ | |
749cf76c CD |
167 | void kvm_arch_destroy_vm(struct kvm *kvm) |
168 | { | |
169 | int i; | |
170 | ||
b2c9a85d MZ |
171 | kvm_vgic_destroy(kvm); |
172 | ||
94d0e598 MZ |
173 | free_percpu(kvm->arch.last_vcpu_ran); |
174 | kvm->arch.last_vcpu_ran = NULL; | |
175 | ||
749cf76c CD |
176 | for (i = 0; i < KVM_MAX_VCPUS; ++i) { |
177 | if (kvm->vcpus[i]) { | |
178 | kvm_arch_vcpu_free(kvm->vcpus[i]); | |
179 | kvm->vcpus[i] = NULL; | |
180 | } | |
181 | } | |
6b2ad81b | 182 | atomic_set(&kvm->online_vcpus, 0); |
749cf76c CD |
183 | } |
184 | ||
784aa3d7 | 185 | int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) |
749cf76c CD |
186 | { |
187 | int r; | |
188 | switch (ext) { | |
1a89dd91 | 189 | case KVM_CAP_IRQCHIP: |
c7da6fa4 PF |
190 | r = vgic_present; |
191 | break; | |
d44758c0 | 192 | case KVM_CAP_IOEVENTFD: |
7330672b | 193 | case KVM_CAP_DEVICE_CTRL: |
749cf76c CD |
194 | case KVM_CAP_USER_MEMORY: |
195 | case KVM_CAP_SYNC_MMU: | |
196 | case KVM_CAP_DESTROY_MEMORY_REGION_WORKS: | |
197 | case KVM_CAP_ONE_REG: | |
aa024c2f | 198 | case KVM_CAP_ARM_PSCI: |
4447a208 | 199 | case KVM_CAP_ARM_PSCI_0_2: |
98047888 | 200 | case KVM_CAP_READONLY_MEM: |
ecccf0cc | 201 | case KVM_CAP_MP_STATE: |
460df4c1 | 202 | case KVM_CAP_IMMEDIATE_EXIT: |
58bf437f | 203 | case KVM_CAP_VCPU_EVENTS: |
749cf76c CD |
204 | r = 1; |
205 | break; | |
3401d546 CD |
206 | case KVM_CAP_ARM_SET_DEVICE_ADDR: |
207 | r = 1; | |
ca46e10f | 208 | break; |
749cf76c CD |
209 | case KVM_CAP_NR_VCPUS: |
210 | r = num_online_cpus(); | |
211 | break; | |
212 | case KVM_CAP_MAX_VCPUS: | |
213 | r = KVM_MAX_VCPUS; | |
214 | break; | |
a86cb413 TH |
215 | case KVM_CAP_MAX_VCPU_ID: |
216 | r = KVM_MAX_VCPU_ID; | |
217 | break; | |
2988509d VM |
218 | case KVM_CAP_MSI_DEVID: |
219 | if (!kvm) | |
220 | r = -EINVAL; | |
221 | else | |
222 | r = kvm->arch.vgic.msis_require_devid; | |
223 | break; | |
f7214e60 CD |
224 | case KVM_CAP_ARM_USER_IRQ: |
225 | /* | |
226 | * 1: EL1_VTIMER, EL1_PTIMER, and PMU. | |
227 | * (bump this number if adding more devices) | |
228 | */ | |
229 | r = 1; | |
230 | break; | |
749cf76c | 231 | default: |
375bdd3b | 232 | r = kvm_arch_vm_ioctl_check_extension(kvm, ext); |
749cf76c CD |
233 | break; |
234 | } | |
235 | return r; | |
236 | } | |
237 | ||
238 | long kvm_arch_dev_ioctl(struct file *filp, | |
239 | unsigned int ioctl, unsigned long arg) | |
240 | { | |
241 | return -EINVAL; | |
242 | } | |
243 | ||
d1e5b0e9 MO |
244 | struct kvm *kvm_arch_alloc_vm(void) |
245 | { | |
246 | if (!has_vhe()) | |
247 | return kzalloc(sizeof(struct kvm), GFP_KERNEL); | |
248 | ||
249 | return vzalloc(sizeof(struct kvm)); | |
250 | } | |
251 | ||
252 | void kvm_arch_free_vm(struct kvm *kvm) | |
253 | { | |
254 | if (!has_vhe()) | |
255 | kfree(kvm); | |
256 | else | |
257 | vfree(kvm); | |
258 | } | |
749cf76c CD |
259 | |
260 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id) | |
261 | { | |
262 | int err; | |
263 | struct kvm_vcpu *vcpu; | |
264 | ||
716139df CD |
265 | if (irqchip_in_kernel(kvm) && vgic_initialized(kvm)) { |
266 | err = -EBUSY; | |
267 | goto out; | |
268 | } | |
269 | ||
3caa2d8c AP |
270 | if (id >= kvm->arch.max_vcpus) { |
271 | err = -EINVAL; | |
272 | goto out; | |
273 | } | |
274 | ||
749cf76c CD |
275 | vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
276 | if (!vcpu) { | |
277 | err = -ENOMEM; | |
278 | goto out; | |
279 | } | |
280 | ||
281 | err = kvm_vcpu_init(vcpu, kvm, id); | |
282 | if (err) | |
283 | goto free_vcpu; | |
284 | ||
c8dddecd | 285 | err = create_hyp_mappings(vcpu, vcpu + 1, PAGE_HYP); |
d5d8184d CD |
286 | if (err) |
287 | goto vcpu_uninit; | |
288 | ||
749cf76c | 289 | return vcpu; |
d5d8184d CD |
290 | vcpu_uninit: |
291 | kvm_vcpu_uninit(vcpu); | |
749cf76c CD |
292 | free_vcpu: |
293 | kmem_cache_free(kvm_vcpu_cache, vcpu); | |
294 | out: | |
295 | return ERR_PTR(err); | |
296 | } | |
297 | ||
31928aa5 | 298 | void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) |
749cf76c | 299 | { |
749cf76c CD |
300 | } |
301 | ||
302 | void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu) | |
303 | { | |
f1d7231c CD |
304 | if (vcpu->arch.has_run_once && unlikely(!irqchip_in_kernel(vcpu->kvm))) |
305 | static_branch_dec(&userspace_irqchip_in_use); | |
306 | ||
d5d8184d | 307 | kvm_mmu_free_memory_caches(vcpu); |
967f8427 | 308 | kvm_timer_vcpu_terminate(vcpu); |
5f0a714a | 309 | kvm_pmu_vcpu_destroy(vcpu); |
591d215a | 310 | kvm_vcpu_uninit(vcpu); |
d5d8184d | 311 | kmem_cache_free(kvm_vcpu_cache, vcpu); |
749cf76c CD |
312 | } |
313 | ||
314 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) | |
315 | { | |
316 | kvm_arch_vcpu_free(vcpu); | |
317 | } | |
318 | ||
319 | int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) | |
320 | { | |
1c88ab7e | 321 | return kvm_timer_is_pending(vcpu); |
749cf76c CD |
322 | } |
323 | ||
d35268da CD |
324 | void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) |
325 | { | |
df9ba959 | 326 | kvm_vgic_v4_enable_doorbell(vcpu); |
d35268da CD |
327 | } |
328 | ||
329 | void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) | |
330 | { | |
df9ba959 | 331 | kvm_vgic_v4_disable_doorbell(vcpu); |
d35268da CD |
332 | } |
333 | ||
749cf76c CD |
334 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) |
335 | { | |
f7ed45be CD |
336 | /* Force users to call KVM_ARM_VCPU_INIT */ |
337 | vcpu->arch.target = -1; | |
f7fa034d | 338 | bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES); |
1a89dd91 | 339 | |
967f8427 MZ |
340 | /* Set up the timer */ |
341 | kvm_timer_vcpu_init(vcpu); | |
342 | ||
bca031e2 ZY |
343 | kvm_pmu_vcpu_init(vcpu); |
344 | ||
84e690bf AB |
345 | kvm_arm_reset_debug_ptr(vcpu); |
346 | ||
1aab6f46 | 347 | return kvm_vgic_vcpu_init(vcpu); |
749cf76c CD |
348 | } |
349 | ||
749cf76c CD |
350 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
351 | { | |
94d0e598 | 352 | int *last_ran; |
630a1685 | 353 | kvm_host_data_t *cpu_data; |
94d0e598 MZ |
354 | |
355 | last_ran = this_cpu_ptr(vcpu->kvm->arch.last_vcpu_ran); | |
630a1685 | 356 | cpu_data = this_cpu_ptr(&kvm_host_data); |
94d0e598 MZ |
357 | |
358 | /* | |
359 | * We might get preempted before the vCPU actually runs, but | |
360 | * over-invalidation doesn't affect correctness. | |
361 | */ | |
362 | if (*last_ran != vcpu->vcpu_id) { | |
363 | kvm_call_hyp(__kvm_tlb_flush_local_vmid, vcpu); | |
364 | *last_ran = vcpu->vcpu_id; | |
365 | } | |
366 | ||
86ce8535 | 367 | vcpu->cpu = cpu; |
630a1685 | 368 | vcpu->arch.host_cpu_context = &cpu_data->host_ctxt; |
5b3e5e5b | 369 | |
1638a12d | 370 | kvm_arm_set_running_vcpu(vcpu); |
328e5664 | 371 | kvm_vgic_load(vcpu); |
b103cc3f | 372 | kvm_timer_vcpu_load(vcpu); |
bc192cee | 373 | kvm_vcpu_load_sysregs(vcpu); |
e6b673b7 | 374 | kvm_arch_vcpu_load_fp(vcpu); |
435e53fb | 375 | kvm_vcpu_pmu_restore_guest(vcpu); |
de737089 MZ |
376 | |
377 | if (single_task_running()) | |
378 | vcpu_clear_wfe_traps(vcpu); | |
379 | else | |
380 | vcpu_set_wfe_traps(vcpu); | |
384b40ca MR |
381 | |
382 | vcpu_ptrauth_setup_lazy(vcpu); | |
749cf76c CD |
383 | } |
384 | ||
385 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) | |
386 | { | |
e6b673b7 | 387 | kvm_arch_vcpu_put_fp(vcpu); |
bc192cee | 388 | kvm_vcpu_put_sysregs(vcpu); |
b103cc3f | 389 | kvm_timer_vcpu_put(vcpu); |
328e5664 | 390 | kvm_vgic_put(vcpu); |
435e53fb | 391 | kvm_vcpu_pmu_restore_host(vcpu); |
328e5664 | 392 | |
e9b152cb CD |
393 | vcpu->cpu = -1; |
394 | ||
1638a12d | 395 | kvm_arm_set_running_vcpu(NULL); |
749cf76c CD |
396 | } |
397 | ||
424c989b AJ |
398 | static void vcpu_power_off(struct kvm_vcpu *vcpu) |
399 | { | |
400 | vcpu->arch.power_off = true; | |
7b244e2b | 401 | kvm_make_request(KVM_REQ_SLEEP, vcpu); |
424c989b AJ |
402 | kvm_vcpu_kick(vcpu); |
403 | } | |
404 | ||
749cf76c CD |
405 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
406 | struct kvm_mp_state *mp_state) | |
407 | { | |
3781528e | 408 | if (vcpu->arch.power_off) |
ecccf0cc AB |
409 | mp_state->mp_state = KVM_MP_STATE_STOPPED; |
410 | else | |
411 | mp_state->mp_state = KVM_MP_STATE_RUNNABLE; | |
412 | ||
413 | return 0; | |
749cf76c CD |
414 | } |
415 | ||
416 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, | |
417 | struct kvm_mp_state *mp_state) | |
418 | { | |
e83dff5e CD |
419 | int ret = 0; |
420 | ||
ecccf0cc AB |
421 | switch (mp_state->mp_state) { |
422 | case KVM_MP_STATE_RUNNABLE: | |
3781528e | 423 | vcpu->arch.power_off = false; |
ecccf0cc AB |
424 | break; |
425 | case KVM_MP_STATE_STOPPED: | |
424c989b | 426 | vcpu_power_off(vcpu); |
ecccf0cc AB |
427 | break; |
428 | default: | |
e83dff5e | 429 | ret = -EINVAL; |
ecccf0cc AB |
430 | } |
431 | ||
e83dff5e | 432 | return ret; |
749cf76c CD |
433 | } |
434 | ||
5b3e5e5b CD |
435 | /** |
436 | * kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled | |
437 | * @v: The VCPU pointer | |
438 | * | |
439 | * If the guest CPU is not waiting for interrupts or an interrupt line is | |
440 | * asserted, the CPU is by definition runnable. | |
441 | */ | |
749cf76c CD |
442 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *v) |
443 | { | |
3df59d8d CD |
444 | bool irq_lines = *vcpu_hcr(v) & (HCR_VI | HCR_VF); |
445 | return ((irq_lines || kvm_vgic_vcpu_pending_irq(v)) | |
3b92830a | 446 | && !v->arch.power_off && !v->arch.pause); |
749cf76c CD |
447 | } |
448 | ||
199b5763 LM |
449 | bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) |
450 | { | |
f01fbd2f | 451 | return vcpu_mode_priv(vcpu); |
199b5763 LM |
452 | } |
453 | ||
f7ed45be CD |
454 | /* Just ensure a guest exit from a particular CPU */ |
455 | static void exit_vm_noop(void *info) | |
456 | { | |
457 | } | |
458 | ||
459 | void force_vm_exit(const cpumask_t *mask) | |
460 | { | |
898f949f | 461 | preempt_disable(); |
f7ed45be | 462 | smp_call_function_many(mask, exit_vm_noop, NULL, true); |
898f949f | 463 | preempt_enable(); |
f7ed45be CD |
464 | } |
465 | ||
466 | /** | |
467 | * need_new_vmid_gen - check that the VMID is still valid | |
e329fb75 | 468 | * @vmid: The VMID to check |
f7ed45be CD |
469 | * |
470 | * return true if there is a new generation of VMIDs being used | |
471 | * | |
e329fb75 CD |
472 | * The hardware supports a limited set of values with the value zero reserved |
473 | * for the host, so we check if an assigned value belongs to a previous | |
474 | * generation, which which requires us to assign a new value. If we're the | |
475 | * first to use a VMID for the new generation, we must flush necessary caches | |
476 | * and TLBs on all CPUs. | |
f7ed45be | 477 | */ |
e329fb75 | 478 | static bool need_new_vmid_gen(struct kvm_vmid *vmid) |
f7ed45be | 479 | { |
fb544d1c CD |
480 | u64 current_vmid_gen = atomic64_read(&kvm_vmid_gen); |
481 | smp_rmb(); /* Orders read of kvm_vmid_gen and kvm->arch.vmid */ | |
e329fb75 | 482 | return unlikely(READ_ONCE(vmid->vmid_gen) != current_vmid_gen); |
f7ed45be CD |
483 | } |
484 | ||
485 | /** | |
e329fb75 CD |
486 | * update_vmid - Update the vmid with a valid VMID for the current generation |
487 | * @kvm: The guest that struct vmid belongs to | |
488 | * @vmid: The stage-2 VMID information struct | |
f7ed45be | 489 | */ |
e329fb75 | 490 | static void update_vmid(struct kvm_vmid *vmid) |
f7ed45be | 491 | { |
e329fb75 | 492 | if (!need_new_vmid_gen(vmid)) |
f7ed45be CD |
493 | return; |
494 | ||
fb544d1c | 495 | spin_lock(&kvm_vmid_lock); |
f7ed45be CD |
496 | |
497 | /* | |
498 | * We need to re-check the vmid_gen here to ensure that if another vcpu | |
499 | * already allocated a valid vmid for this vm, then this vcpu should | |
500 | * use the same vmid. | |
501 | */ | |
e329fb75 | 502 | if (!need_new_vmid_gen(vmid)) { |
fb544d1c | 503 | spin_unlock(&kvm_vmid_lock); |
f7ed45be CD |
504 | return; |
505 | } | |
506 | ||
507 | /* First user of a new VMID generation? */ | |
508 | if (unlikely(kvm_next_vmid == 0)) { | |
509 | atomic64_inc(&kvm_vmid_gen); | |
510 | kvm_next_vmid = 1; | |
511 | ||
512 | /* | |
513 | * On SMP we know no other CPUs can use this CPU's or each | |
514 | * other's VMID after force_vm_exit returns since the | |
515 | * kvm_vmid_lock blocks them from reentry to the guest. | |
516 | */ | |
517 | force_vm_exit(cpu_all_mask); | |
518 | /* | |
519 | * Now broadcast TLB + ICACHE invalidation over the inner | |
520 | * shareable domain to make sure all data structures are | |
521 | * clean. | |
522 | */ | |
523 | kvm_call_hyp(__kvm_flush_vm_context); | |
524 | } | |
525 | ||
e329fb75 | 526 | vmid->vmid = kvm_next_vmid; |
f7ed45be | 527 | kvm_next_vmid++; |
e329fb75 | 528 | kvm_next_vmid &= (1 << kvm_get_vmid_bits()) - 1; |
f7ed45be | 529 | |
fb544d1c | 530 | smp_wmb(); |
e329fb75 | 531 | WRITE_ONCE(vmid->vmid_gen, atomic64_read(&kvm_vmid_gen)); |
fb544d1c CD |
532 | |
533 | spin_unlock(&kvm_vmid_lock); | |
f7ed45be CD |
534 | } |
535 | ||
f7ed45be CD |
536 | static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu) |
537 | { | |
05971120 | 538 | struct kvm *kvm = vcpu->kvm; |
41a54482 | 539 | int ret = 0; |
e1ba0207 | 540 | |
f7ed45be CD |
541 | if (likely(vcpu->arch.has_run_once)) |
542 | return 0; | |
543 | ||
7dd32a0d DM |
544 | if (!kvm_arm_vcpu_is_finalized(vcpu)) |
545 | return -EPERM; | |
546 | ||
f7ed45be | 547 | vcpu->arch.has_run_once = true; |
aa024c2f | 548 | |
61bbe380 CD |
549 | if (likely(irqchip_in_kernel(kvm))) { |
550 | /* | |
551 | * Map the VGIC hardware resources before running a vcpu the | |
552 | * first time on this VM. | |
553 | */ | |
554 | if (unlikely(!vgic_ready(kvm))) { | |
555 | ret = kvm_vgic_map_resources(kvm); | |
556 | if (ret) | |
557 | return ret; | |
558 | } | |
559 | } else { | |
560 | /* | |
561 | * Tell the rest of the code that there are userspace irqchip | |
562 | * VMs in the wild. | |
563 | */ | |
564 | static_branch_inc(&userspace_irqchip_in_use); | |
01ac5e34 MZ |
565 | } |
566 | ||
d9e13977 | 567 | ret = kvm_timer_enable(vcpu); |
a2befacf CD |
568 | if (ret) |
569 | return ret; | |
570 | ||
571 | ret = kvm_arm_pmu_v3_enable(vcpu); | |
05971120 | 572 | |
41a54482 | 573 | return ret; |
f7ed45be CD |
574 | } |
575 | ||
c1426e4c EA |
576 | bool kvm_arch_intc_initialized(struct kvm *kvm) |
577 | { | |
578 | return vgic_initialized(kvm); | |
579 | } | |
580 | ||
b13216cf | 581 | void kvm_arm_halt_guest(struct kvm *kvm) |
3b92830a EA |
582 | { |
583 | int i; | |
584 | struct kvm_vcpu *vcpu; | |
585 | ||
586 | kvm_for_each_vcpu(i, vcpu, kvm) | |
587 | vcpu->arch.pause = true; | |
7b244e2b | 588 | kvm_make_all_cpus_request(kvm, KVM_REQ_SLEEP); |
3b92830a EA |
589 | } |
590 | ||
b13216cf | 591 | void kvm_arm_resume_guest(struct kvm *kvm) |
3b92830a EA |
592 | { |
593 | int i; | |
594 | struct kvm_vcpu *vcpu; | |
595 | ||
abd72296 CD |
596 | kvm_for_each_vcpu(i, vcpu, kvm) { |
597 | vcpu->arch.pause = false; | |
b3dae109 | 598 | swake_up_one(kvm_arch_vcpu_wq(vcpu)); |
abd72296 | 599 | } |
3b92830a EA |
600 | } |
601 | ||
7b244e2b | 602 | static void vcpu_req_sleep(struct kvm_vcpu *vcpu) |
aa024c2f | 603 | { |
8577370f | 604 | struct swait_queue_head *wq = kvm_arch_vcpu_wq(vcpu); |
aa024c2f | 605 | |
b3dae109 | 606 | swait_event_interruptible_exclusive(*wq, ((!vcpu->arch.power_off) && |
3b92830a | 607 | (!vcpu->arch.pause))); |
0592c005 | 608 | |
424c989b | 609 | if (vcpu->arch.power_off || vcpu->arch.pause) { |
0592c005 | 610 | /* Awaken to handle a signal, request we sleep again later. */ |
7b244e2b | 611 | kvm_make_request(KVM_REQ_SLEEP, vcpu); |
0592c005 | 612 | } |
358b28f0 MZ |
613 | |
614 | /* | |
615 | * Make sure we will observe a potential reset request if we've | |
616 | * observed a change to the power state. Pairs with the smp_wmb() in | |
617 | * kvm_psci_vcpu_on(). | |
618 | */ | |
619 | smp_rmb(); | |
aa024c2f MZ |
620 | } |
621 | ||
e8180dca AP |
622 | static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu) |
623 | { | |
624 | return vcpu->arch.target >= 0; | |
625 | } | |
626 | ||
0592c005 AJ |
627 | static void check_vcpu_requests(struct kvm_vcpu *vcpu) |
628 | { | |
629 | if (kvm_request_pending(vcpu)) { | |
7b244e2b AJ |
630 | if (kvm_check_request(KVM_REQ_SLEEP, vcpu)) |
631 | vcpu_req_sleep(vcpu); | |
325f9c64 | 632 | |
358b28f0 MZ |
633 | if (kvm_check_request(KVM_REQ_VCPU_RESET, vcpu)) |
634 | kvm_reset_vcpu(vcpu); | |
635 | ||
325f9c64 AJ |
636 | /* |
637 | * Clear IRQ_PENDING requests that were made to guarantee | |
638 | * that a VCPU sees new virtual interrupts. | |
639 | */ | |
640 | kvm_check_request(KVM_REQ_IRQ_PENDING, vcpu); | |
0592c005 AJ |
641 | } |
642 | } | |
643 | ||
f7ed45be CD |
644 | /** |
645 | * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code | |
646 | * @vcpu: The VCPU pointer | |
647 | * @run: The kvm_run structure pointer used for userspace state exchange | |
648 | * | |
649 | * This function is called through the VCPU_RUN ioctl called from user space. It | |
650 | * will execute VM code in a loop until the time slice for the process is used | |
651 | * or some emulation is needed from user space in which case the function will | |
652 | * return with return value 0 and with the kvm_run structure filled in with the | |
653 | * required data for the requested emulation. | |
654 | */ | |
749cf76c CD |
655 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) |
656 | { | |
f7ed45be | 657 | int ret; |
f7ed45be | 658 | |
e8180dca | 659 | if (unlikely(!kvm_vcpu_initialized(vcpu))) |
f7ed45be CD |
660 | return -ENOEXEC; |
661 | ||
662 | ret = kvm_vcpu_first_run_init(vcpu); | |
663 | if (ret) | |
829a5863 | 664 | return ret; |
f7ed45be | 665 | |
45e96ea6 CD |
666 | if (run->exit_reason == KVM_EXIT_MMIO) { |
667 | ret = kvm_handle_mmio_return(vcpu, vcpu->run); | |
668 | if (ret) | |
829a5863 | 669 | return ret; |
45e96ea6 CD |
670 | } |
671 | ||
829a5863 CD |
672 | if (run->immediate_exit) |
673 | return -EINTR; | |
674 | ||
675 | vcpu_load(vcpu); | |
460df4c1 | 676 | |
20b7035c | 677 | kvm_sigset_activate(vcpu); |
f7ed45be CD |
678 | |
679 | ret = 1; | |
680 | run->exit_reason = KVM_EXIT_UNKNOWN; | |
681 | while (ret > 0) { | |
682 | /* | |
683 | * Check conditions before entering the guest | |
684 | */ | |
685 | cond_resched(); | |
686 | ||
e329fb75 | 687 | update_vmid(&vcpu->kvm->arch.vmid); |
f7ed45be | 688 | |
0592c005 AJ |
689 | check_vcpu_requests(vcpu); |
690 | ||
abdf5843 MZ |
691 | /* |
692 | * Preparing the interrupts to be injected also | |
693 | * involves poking the GIC, which must be done in a | |
694 | * non-preemptible context. | |
695 | */ | |
1b3d546d | 696 | preempt_disable(); |
328e5664 | 697 | |
b02386eb | 698 | kvm_pmu_flush_hwstate(vcpu); |
328e5664 | 699 | |
f7ed45be CD |
700 | local_irq_disable(); |
701 | ||
abdf5843 MZ |
702 | kvm_vgic_flush_hwstate(vcpu); |
703 | ||
f7ed45be | 704 | /* |
61bbe380 CD |
705 | * Exit if we have a signal pending so that we can deliver the |
706 | * signal to user space. | |
f7ed45be | 707 | */ |
61bbe380 | 708 | if (signal_pending(current)) { |
f7ed45be CD |
709 | ret = -EINTR; |
710 | run->exit_reason = KVM_EXIT_INTR; | |
711 | } | |
712 | ||
61bbe380 CD |
713 | /* |
714 | * If we're using a userspace irqchip, then check if we need | |
715 | * to tell a userspace irqchip about timer or PMU level | |
716 | * changes and if so, exit to userspace (the actual level | |
717 | * state gets updated in kvm_timer_update_run and | |
718 | * kvm_pmu_update_run below). | |
719 | */ | |
720 | if (static_branch_unlikely(&userspace_irqchip_in_use)) { | |
721 | if (kvm_timer_should_notify_user(vcpu) || | |
722 | kvm_pmu_should_notify_user(vcpu)) { | |
723 | ret = -EINTR; | |
724 | run->exit_reason = KVM_EXIT_INTR; | |
725 | } | |
726 | } | |
727 | ||
6a6d73be AJ |
728 | /* |
729 | * Ensure we set mode to IN_GUEST_MODE after we disable | |
730 | * interrupts and before the final VCPU requests check. | |
731 | * See the comment in kvm_vcpu_exiting_guest_mode() and | |
732 | * Documentation/virtual/kvm/vcpu-requests.rst | |
733 | */ | |
734 | smp_store_mb(vcpu->mode, IN_GUEST_MODE); | |
735 | ||
e329fb75 | 736 | if (ret <= 0 || need_new_vmid_gen(&vcpu->kvm->arch.vmid) || |
424c989b | 737 | kvm_request_pending(vcpu)) { |
6a6d73be | 738 | vcpu->mode = OUTSIDE_GUEST_MODE; |
771621b0 | 739 | isb(); /* Ensure work in x_flush_hwstate is committed */ |
b02386eb | 740 | kvm_pmu_sync_hwstate(vcpu); |
61bbe380 CD |
741 | if (static_branch_unlikely(&userspace_irqchip_in_use)) |
742 | kvm_timer_sync_hwstate(vcpu); | |
1a89dd91 | 743 | kvm_vgic_sync_hwstate(vcpu); |
ee9bb9a1 | 744 | local_irq_enable(); |
abdf5843 | 745 | preempt_enable(); |
f7ed45be CD |
746 | continue; |
747 | } | |
748 | ||
56c7f5e7 AB |
749 | kvm_arm_setup_debug(vcpu); |
750 | ||
f7ed45be CD |
751 | /************************************************************** |
752 | * Enter the guest | |
753 | */ | |
754 | trace_kvm_entry(*vcpu_pc(vcpu)); | |
6edaa530 | 755 | guest_enter_irqoff(); |
f7ed45be | 756 | |
3f5c90b8 CD |
757 | if (has_vhe()) { |
758 | kvm_arm_vhe_guest_enter(); | |
759 | ret = kvm_vcpu_run_vhe(vcpu); | |
4f5abad9 | 760 | kvm_arm_vhe_guest_exit(); |
3f5c90b8 | 761 | } else { |
7aa8d146 | 762 | ret = kvm_call_hyp_ret(__kvm_vcpu_run_nvhe, vcpu); |
3f5c90b8 CD |
763 | } |
764 | ||
f7ed45be | 765 | vcpu->mode = OUTSIDE_GUEST_MODE; |
b19e6892 | 766 | vcpu->stat.exits++; |
1b3d546d CD |
767 | /* |
768 | * Back from guest | |
769 | *************************************************************/ | |
770 | ||
56c7f5e7 AB |
771 | kvm_arm_clear_debug(vcpu); |
772 | ||
ee9bb9a1 | 773 | /* |
b103cc3f | 774 | * We must sync the PMU state before the vgic state so |
ee9bb9a1 CD |
775 | * that the vgic can properly sample the updated state of the |
776 | * interrupt line. | |
777 | */ | |
778 | kvm_pmu_sync_hwstate(vcpu); | |
ee9bb9a1 | 779 | |
b103cc3f CD |
780 | /* |
781 | * Sync the vgic state before syncing the timer state because | |
782 | * the timer code needs to know if the virtual timer | |
783 | * interrupts are active. | |
784 | */ | |
ee9bb9a1 CD |
785 | kvm_vgic_sync_hwstate(vcpu); |
786 | ||
b103cc3f CD |
787 | /* |
788 | * Sync the timer hardware state before enabling interrupts as | |
789 | * we don't want vtimer interrupts to race with syncing the | |
790 | * timer virtual interrupt state. | |
791 | */ | |
61bbe380 CD |
792 | if (static_branch_unlikely(&userspace_irqchip_in_use)) |
793 | kvm_timer_sync_hwstate(vcpu); | |
b103cc3f | 794 | |
e6b673b7 DM |
795 | kvm_arch_vcpu_ctxsync_fp(vcpu); |
796 | ||
f7ed45be CD |
797 | /* |
798 | * We may have taken a host interrupt in HYP mode (ie | |
799 | * while executing the guest). This interrupt is still | |
800 | * pending, as we haven't serviced it yet! | |
801 | * | |
802 | * We're now back in SVC mode, with interrupts | |
803 | * disabled. Enabling the interrupts now will have | |
804 | * the effect of taking the interrupt again, in SVC | |
805 | * mode this time. | |
806 | */ | |
807 | local_irq_enable(); | |
808 | ||
809 | /* | |
6edaa530 | 810 | * We do local_irq_enable() before calling guest_exit() so |
1b3d546d CD |
811 | * that if a timer interrupt hits while running the guest we |
812 | * account that tick as being spent in the guest. We enable | |
6edaa530 | 813 | * preemption after calling guest_exit() so that if we get |
1b3d546d CD |
814 | * preempted we make sure ticks after that is not counted as |
815 | * guest time. | |
816 | */ | |
6edaa530 | 817 | guest_exit(); |
b5905dc1 | 818 | trace_kvm_exit(ret, kvm_vcpu_trap_get_class(vcpu), *vcpu_pc(vcpu)); |
1b3d546d | 819 | |
3368bd80 JM |
820 | /* Exit types that need handling before we can be preempted */ |
821 | handle_exit_early(vcpu, run, ret); | |
822 | ||
abdf5843 MZ |
823 | preempt_enable(); |
824 | ||
f7ed45be CD |
825 | ret = handle_exit(vcpu, run, ret); |
826 | } | |
827 | ||
d9e13977 | 828 | /* Tell userspace about in-kernel device output levels */ |
3dbbdf78 CD |
829 | if (unlikely(!irqchip_in_kernel(vcpu->kvm))) { |
830 | kvm_timer_update_run(vcpu); | |
831 | kvm_pmu_update_run(vcpu); | |
832 | } | |
d9e13977 | 833 | |
20b7035c JS |
834 | kvm_sigset_deactivate(vcpu); |
835 | ||
accb757d | 836 | vcpu_put(vcpu); |
f7ed45be | 837 | return ret; |
749cf76c CD |
838 | } |
839 | ||
86ce8535 CD |
840 | static int vcpu_interrupt_line(struct kvm_vcpu *vcpu, int number, bool level) |
841 | { | |
842 | int bit_index; | |
843 | bool set; | |
3df59d8d | 844 | unsigned long *hcr; |
86ce8535 CD |
845 | |
846 | if (number == KVM_ARM_IRQ_CPU_IRQ) | |
847 | bit_index = __ffs(HCR_VI); | |
848 | else /* KVM_ARM_IRQ_CPU_FIQ */ | |
849 | bit_index = __ffs(HCR_VF); | |
850 | ||
3df59d8d | 851 | hcr = vcpu_hcr(vcpu); |
86ce8535 | 852 | if (level) |
3df59d8d | 853 | set = test_and_set_bit(bit_index, hcr); |
86ce8535 | 854 | else |
3df59d8d | 855 | set = test_and_clear_bit(bit_index, hcr); |
86ce8535 CD |
856 | |
857 | /* | |
858 | * If we didn't change anything, no need to wake up or kick other CPUs | |
859 | */ | |
860 | if (set == level) | |
861 | return 0; | |
862 | ||
863 | /* | |
864 | * The vcpu irq_lines field was updated, wake up sleeping VCPUs and | |
865 | * trigger a world-switch round on the running physical CPU to set the | |
866 | * virtual IRQ/FIQ fields in the HCR appropriately. | |
867 | */ | |
325f9c64 | 868 | kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu); |
86ce8535 CD |
869 | kvm_vcpu_kick(vcpu); |
870 | ||
871 | return 0; | |
872 | } | |
873 | ||
79558f11 AG |
874 | int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level, |
875 | bool line_status) | |
86ce8535 CD |
876 | { |
877 | u32 irq = irq_level->irq; | |
878 | unsigned int irq_type, vcpu_idx, irq_num; | |
879 | int nrcpus = atomic_read(&kvm->online_vcpus); | |
880 | struct kvm_vcpu *vcpu = NULL; | |
881 | bool level = irq_level->level; | |
882 | ||
883 | irq_type = (irq >> KVM_ARM_IRQ_TYPE_SHIFT) & KVM_ARM_IRQ_TYPE_MASK; | |
884 | vcpu_idx = (irq >> KVM_ARM_IRQ_VCPU_SHIFT) & KVM_ARM_IRQ_VCPU_MASK; | |
885 | irq_num = (irq >> KVM_ARM_IRQ_NUM_SHIFT) & KVM_ARM_IRQ_NUM_MASK; | |
886 | ||
887 | trace_kvm_irq_line(irq_type, vcpu_idx, irq_num, irq_level->level); | |
888 | ||
5863c2ce MZ |
889 | switch (irq_type) { |
890 | case KVM_ARM_IRQ_TYPE_CPU: | |
891 | if (irqchip_in_kernel(kvm)) | |
892 | return -ENXIO; | |
86ce8535 | 893 | |
5863c2ce MZ |
894 | if (vcpu_idx >= nrcpus) |
895 | return -EINVAL; | |
86ce8535 | 896 | |
5863c2ce MZ |
897 | vcpu = kvm_get_vcpu(kvm, vcpu_idx); |
898 | if (!vcpu) | |
899 | return -EINVAL; | |
86ce8535 | 900 | |
5863c2ce MZ |
901 | if (irq_num > KVM_ARM_IRQ_CPU_FIQ) |
902 | return -EINVAL; | |
903 | ||
904 | return vcpu_interrupt_line(vcpu, irq_num, level); | |
905 | case KVM_ARM_IRQ_TYPE_PPI: | |
906 | if (!irqchip_in_kernel(kvm)) | |
907 | return -ENXIO; | |
908 | ||
909 | if (vcpu_idx >= nrcpus) | |
910 | return -EINVAL; | |
911 | ||
912 | vcpu = kvm_get_vcpu(kvm, vcpu_idx); | |
913 | if (!vcpu) | |
914 | return -EINVAL; | |
915 | ||
916 | if (irq_num < VGIC_NR_SGIS || irq_num >= VGIC_NR_PRIVATE_IRQS) | |
917 | return -EINVAL; | |
86ce8535 | 918 | |
cb3f0ad8 | 919 | return kvm_vgic_inject_irq(kvm, vcpu->vcpu_id, irq_num, level, NULL); |
5863c2ce MZ |
920 | case KVM_ARM_IRQ_TYPE_SPI: |
921 | if (!irqchip_in_kernel(kvm)) | |
922 | return -ENXIO; | |
923 | ||
fd1d0ddf | 924 | if (irq_num < VGIC_NR_PRIVATE_IRQS) |
5863c2ce MZ |
925 | return -EINVAL; |
926 | ||
cb3f0ad8 | 927 | return kvm_vgic_inject_irq(kvm, 0, irq_num, level, NULL); |
5863c2ce MZ |
928 | } |
929 | ||
930 | return -EINVAL; | |
86ce8535 CD |
931 | } |
932 | ||
f7fa034d CD |
933 | static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu, |
934 | const struct kvm_vcpu_init *init) | |
935 | { | |
811328fc | 936 | unsigned int i, ret; |
f7fa034d CD |
937 | int phys_target = kvm_target_cpu(); |
938 | ||
939 | if (init->target != phys_target) | |
940 | return -EINVAL; | |
941 | ||
942 | /* | |
943 | * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must | |
944 | * use the same target. | |
945 | */ | |
946 | if (vcpu->arch.target != -1 && vcpu->arch.target != init->target) | |
947 | return -EINVAL; | |
948 | ||
949 | /* -ENOENT for unknown features, -EINVAL for invalid combinations. */ | |
950 | for (i = 0; i < sizeof(init->features) * 8; i++) { | |
951 | bool set = (init->features[i / 32] & (1 << (i % 32))); | |
952 | ||
953 | if (set && i >= KVM_VCPU_MAX_FEATURES) | |
954 | return -ENOENT; | |
955 | ||
956 | /* | |
957 | * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must | |
958 | * use the same feature set. | |
959 | */ | |
960 | if (vcpu->arch.target != -1 && i < KVM_VCPU_MAX_FEATURES && | |
961 | test_bit(i, vcpu->arch.features) != set) | |
962 | return -EINVAL; | |
963 | ||
964 | if (set) | |
965 | set_bit(i, vcpu->arch.features); | |
966 | } | |
967 | ||
968 | vcpu->arch.target = phys_target; | |
969 | ||
970 | /* Now we know what it is, we can reset it. */ | |
811328fc AJ |
971 | ret = kvm_reset_vcpu(vcpu); |
972 | if (ret) { | |
973 | vcpu->arch.target = -1; | |
974 | bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES); | |
975 | } | |
f7fa034d | 976 | |
811328fc AJ |
977 | return ret; |
978 | } | |
f7fa034d | 979 | |
478a8237 CD |
980 | static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu, |
981 | struct kvm_vcpu_init *init) | |
982 | { | |
983 | int ret; | |
984 | ||
985 | ret = kvm_vcpu_set_target(vcpu, init); | |
986 | if (ret) | |
987 | return ret; | |
988 | ||
957db105 CD |
989 | /* |
990 | * Ensure a rebooted VM will fault in RAM pages and detect if the | |
991 | * guest MMU is turned off and flush the caches as needed. | |
992 | */ | |
993 | if (vcpu->arch.has_run_once) | |
994 | stage2_unmap_vm(vcpu->kvm); | |
995 | ||
b856a591 CD |
996 | vcpu_reset_hcr(vcpu); |
997 | ||
478a8237 | 998 | /* |
3781528e | 999 | * Handle the "start in power-off" case. |
478a8237 | 1000 | */ |
03f1d4c1 | 1001 | if (test_bit(KVM_ARM_VCPU_POWER_OFF, vcpu->arch.features)) |
424c989b | 1002 | vcpu_power_off(vcpu); |
3ad8b3de | 1003 | else |
3781528e | 1004 | vcpu->arch.power_off = false; |
478a8237 CD |
1005 | |
1006 | return 0; | |
1007 | } | |
1008 | ||
f577f6c2 SZ |
1009 | static int kvm_arm_vcpu_set_attr(struct kvm_vcpu *vcpu, |
1010 | struct kvm_device_attr *attr) | |
1011 | { | |
1012 | int ret = -ENXIO; | |
1013 | ||
1014 | switch (attr->group) { | |
1015 | default: | |
bb0c70bc | 1016 | ret = kvm_arm_vcpu_arch_set_attr(vcpu, attr); |
f577f6c2 SZ |
1017 | break; |
1018 | } | |
1019 | ||
1020 | return ret; | |
1021 | } | |
1022 | ||
1023 | static int kvm_arm_vcpu_get_attr(struct kvm_vcpu *vcpu, | |
1024 | struct kvm_device_attr *attr) | |
1025 | { | |
1026 | int ret = -ENXIO; | |
1027 | ||
1028 | switch (attr->group) { | |
1029 | default: | |
bb0c70bc | 1030 | ret = kvm_arm_vcpu_arch_get_attr(vcpu, attr); |
f577f6c2 SZ |
1031 | break; |
1032 | } | |
1033 | ||
1034 | return ret; | |
1035 | } | |
1036 | ||
1037 | static int kvm_arm_vcpu_has_attr(struct kvm_vcpu *vcpu, | |
1038 | struct kvm_device_attr *attr) | |
1039 | { | |
1040 | int ret = -ENXIO; | |
1041 | ||
1042 | switch (attr->group) { | |
1043 | default: | |
bb0c70bc | 1044 | ret = kvm_arm_vcpu_arch_has_attr(vcpu, attr); |
f577f6c2 SZ |
1045 | break; |
1046 | } | |
1047 | ||
1048 | return ret; | |
1049 | } | |
1050 | ||
539aee0e JM |
1051 | static int kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu, |
1052 | struct kvm_vcpu_events *events) | |
1053 | { | |
1054 | memset(events, 0, sizeof(*events)); | |
1055 | ||
1056 | return __kvm_arm_vcpu_get_events(vcpu, events); | |
1057 | } | |
1058 | ||
1059 | static int kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu, | |
1060 | struct kvm_vcpu_events *events) | |
1061 | { | |
1062 | int i; | |
1063 | ||
1064 | /* check whether the reserved field is zero */ | |
1065 | for (i = 0; i < ARRAY_SIZE(events->reserved); i++) | |
1066 | if (events->reserved[i]) | |
1067 | return -EINVAL; | |
1068 | ||
1069 | /* check whether the pad field is zero */ | |
1070 | for (i = 0; i < ARRAY_SIZE(events->exception.pad); i++) | |
1071 | if (events->exception.pad[i]) | |
1072 | return -EINVAL; | |
1073 | ||
1074 | return __kvm_arm_vcpu_set_events(vcpu, events); | |
1075 | } | |
539aee0e | 1076 | |
749cf76c CD |
1077 | long kvm_arch_vcpu_ioctl(struct file *filp, |
1078 | unsigned int ioctl, unsigned long arg) | |
1079 | { | |
1080 | struct kvm_vcpu *vcpu = filp->private_data; | |
1081 | void __user *argp = (void __user *)arg; | |
f577f6c2 | 1082 | struct kvm_device_attr attr; |
9b062471 CD |
1083 | long r; |
1084 | ||
749cf76c CD |
1085 | switch (ioctl) { |
1086 | case KVM_ARM_VCPU_INIT: { | |
1087 | struct kvm_vcpu_init init; | |
1088 | ||
9b062471 | 1089 | r = -EFAULT; |
749cf76c | 1090 | if (copy_from_user(&init, argp, sizeof(init))) |
9b062471 | 1091 | break; |
749cf76c | 1092 | |
9b062471 CD |
1093 | r = kvm_arch_vcpu_ioctl_vcpu_init(vcpu, &init); |
1094 | break; | |
749cf76c CD |
1095 | } |
1096 | case KVM_SET_ONE_REG: | |
1097 | case KVM_GET_ONE_REG: { | |
1098 | struct kvm_one_reg reg; | |
e8180dca | 1099 | |
9b062471 | 1100 | r = -ENOEXEC; |
e8180dca | 1101 | if (unlikely(!kvm_vcpu_initialized(vcpu))) |
9b062471 | 1102 | break; |
e8180dca | 1103 | |
9b062471 | 1104 | r = -EFAULT; |
749cf76c | 1105 | if (copy_from_user(®, argp, sizeof(reg))) |
9b062471 CD |
1106 | break; |
1107 | ||
749cf76c | 1108 | if (ioctl == KVM_SET_ONE_REG) |
9b062471 | 1109 | r = kvm_arm_set_reg(vcpu, ®); |
749cf76c | 1110 | else |
9b062471 CD |
1111 | r = kvm_arm_get_reg(vcpu, ®); |
1112 | break; | |
749cf76c CD |
1113 | } |
1114 | case KVM_GET_REG_LIST: { | |
1115 | struct kvm_reg_list __user *user_list = argp; | |
1116 | struct kvm_reg_list reg_list; | |
1117 | unsigned n; | |
1118 | ||
9b062471 | 1119 | r = -ENOEXEC; |
e8180dca | 1120 | if (unlikely(!kvm_vcpu_initialized(vcpu))) |
9b062471 | 1121 | break; |
e8180dca | 1122 | |
7dd32a0d DM |
1123 | r = -EPERM; |
1124 | if (!kvm_arm_vcpu_is_finalized(vcpu)) | |
1125 | break; | |
1126 | ||
9b062471 | 1127 | r = -EFAULT; |
749cf76c | 1128 | if (copy_from_user(®_list, user_list, sizeof(reg_list))) |
9b062471 | 1129 | break; |
749cf76c CD |
1130 | n = reg_list.n; |
1131 | reg_list.n = kvm_arm_num_regs(vcpu); | |
1132 | if (copy_to_user(user_list, ®_list, sizeof(reg_list))) | |
9b062471 CD |
1133 | break; |
1134 | r = -E2BIG; | |
749cf76c | 1135 | if (n < reg_list.n) |
9b062471 CD |
1136 | break; |
1137 | r = kvm_arm_copy_reg_indices(vcpu, user_list->reg); | |
1138 | break; | |
749cf76c | 1139 | } |
f577f6c2 | 1140 | case KVM_SET_DEVICE_ATTR: { |
9b062471 | 1141 | r = -EFAULT; |
f577f6c2 | 1142 | if (copy_from_user(&attr, argp, sizeof(attr))) |
9b062471 CD |
1143 | break; |
1144 | r = kvm_arm_vcpu_set_attr(vcpu, &attr); | |
1145 | break; | |
f577f6c2 SZ |
1146 | } |
1147 | case KVM_GET_DEVICE_ATTR: { | |
9b062471 | 1148 | r = -EFAULT; |
f577f6c2 | 1149 | if (copy_from_user(&attr, argp, sizeof(attr))) |
9b062471 CD |
1150 | break; |
1151 | r = kvm_arm_vcpu_get_attr(vcpu, &attr); | |
1152 | break; | |
f577f6c2 SZ |
1153 | } |
1154 | case KVM_HAS_DEVICE_ATTR: { | |
9b062471 | 1155 | r = -EFAULT; |
f577f6c2 | 1156 | if (copy_from_user(&attr, argp, sizeof(attr))) |
9b062471 CD |
1157 | break; |
1158 | r = kvm_arm_vcpu_has_attr(vcpu, &attr); | |
1159 | break; | |
f577f6c2 | 1160 | } |
b7b27fac DG |
1161 | case KVM_GET_VCPU_EVENTS: { |
1162 | struct kvm_vcpu_events events; | |
1163 | ||
1164 | if (kvm_arm_vcpu_get_events(vcpu, &events)) | |
1165 | return -EINVAL; | |
1166 | ||
1167 | if (copy_to_user(argp, &events, sizeof(events))) | |
1168 | return -EFAULT; | |
1169 | ||
1170 | return 0; | |
1171 | } | |
1172 | case KVM_SET_VCPU_EVENTS: { | |
1173 | struct kvm_vcpu_events events; | |
1174 | ||
1175 | if (copy_from_user(&events, argp, sizeof(events))) | |
1176 | return -EFAULT; | |
1177 | ||
1178 | return kvm_arm_vcpu_set_events(vcpu, &events); | |
1179 | } | |
7dd32a0d DM |
1180 | case KVM_ARM_VCPU_FINALIZE: { |
1181 | int what; | |
1182 | ||
1183 | if (!kvm_vcpu_initialized(vcpu)) | |
1184 | return -ENOEXEC; | |
1185 | ||
1186 | if (get_user(what, (const int __user *)argp)) | |
1187 | return -EFAULT; | |
1188 | ||
1189 | return kvm_arm_vcpu_finalize(vcpu, what); | |
1190 | } | |
749cf76c | 1191 | default: |
9b062471 | 1192 | r = -EINVAL; |
749cf76c | 1193 | } |
9b062471 | 1194 | |
9b062471 | 1195 | return r; |
749cf76c CD |
1196 | } |
1197 | ||
53c810c3 MS |
1198 | /** |
1199 | * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot | |
1200 | * @kvm: kvm instance | |
1201 | * @log: slot id and address to which we copy the log | |
1202 | * | |
1203 | * Steps 1-4 below provide general overview of dirty page logging. See | |
1204 | * kvm_get_dirty_log_protect() function description for additional details. | |
1205 | * | |
1206 | * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we | |
1207 | * always flush the TLB (step 4) even if previous step failed and the dirty | |
1208 | * bitmap may be corrupt. Regardless of previous outcome the KVM logging API | |
1209 | * does not preclude user space subsequent dirty log read. Flushing TLB ensures | |
1210 | * writes will be marked dirty for next log read. | |
1211 | * | |
1212 | * 1. Take a snapshot of the bit and clear it if needed. | |
1213 | * 2. Write protect the corresponding page. | |
1214 | * 3. Copy the snapshot to the userspace. | |
1215 | * 4. Flush TLB's if needed. | |
1216 | */ | |
749cf76c CD |
1217 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log) |
1218 | { | |
8fe65a82 | 1219 | bool flush = false; |
53c810c3 MS |
1220 | int r; |
1221 | ||
1222 | mutex_lock(&kvm->slots_lock); | |
1223 | ||
8fe65a82 | 1224 | r = kvm_get_dirty_log_protect(kvm, log, &flush); |
53c810c3 | 1225 | |
8fe65a82 | 1226 | if (flush) |
53c810c3 MS |
1227 | kvm_flush_remote_tlbs(kvm); |
1228 | ||
1229 | mutex_unlock(&kvm->slots_lock); | |
1230 | return r; | |
749cf76c CD |
1231 | } |
1232 | ||
2a31b9db PB |
1233 | int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, struct kvm_clear_dirty_log *log) |
1234 | { | |
1235 | bool flush = false; | |
53c810c3 MS |
1236 | int r; |
1237 | ||
1238 | mutex_lock(&kvm->slots_lock); | |
1239 | ||
2a31b9db | 1240 | r = kvm_clear_dirty_log_protect(kvm, log, &flush); |
53c810c3 | 1241 | |
2a31b9db | 1242 | if (flush) |
53c810c3 MS |
1243 | kvm_flush_remote_tlbs(kvm); |
1244 | ||
1245 | mutex_unlock(&kvm->slots_lock); | |
1246 | return r; | |
749cf76c CD |
1247 | } |
1248 | ||
3401d546 CD |
1249 | static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm, |
1250 | struct kvm_arm_device_addr *dev_addr) | |
1251 | { | |
330690cd CD |
1252 | unsigned long dev_id, type; |
1253 | ||
1254 | dev_id = (dev_addr->id & KVM_ARM_DEVICE_ID_MASK) >> | |
1255 | KVM_ARM_DEVICE_ID_SHIFT; | |
1256 | type = (dev_addr->id & KVM_ARM_DEVICE_TYPE_MASK) >> | |
1257 | KVM_ARM_DEVICE_TYPE_SHIFT; | |
1258 | ||
1259 | switch (dev_id) { | |
1260 | case KVM_ARM_DEVICE_VGIC_V2: | |
c7da6fa4 PF |
1261 | if (!vgic_present) |
1262 | return -ENXIO; | |
ce01e4e8 | 1263 | return kvm_vgic_addr(kvm, type, &dev_addr->addr, true); |
330690cd CD |
1264 | default: |
1265 | return -ENODEV; | |
1266 | } | |
3401d546 CD |
1267 | } |
1268 | ||
749cf76c CD |
1269 | long kvm_arch_vm_ioctl(struct file *filp, |
1270 | unsigned int ioctl, unsigned long arg) | |
1271 | { | |
3401d546 CD |
1272 | struct kvm *kvm = filp->private_data; |
1273 | void __user *argp = (void __user *)arg; | |
1274 | ||
1275 | switch (ioctl) { | |
5863c2ce | 1276 | case KVM_CREATE_IRQCHIP: { |
a28ebea2 | 1277 | int ret; |
c7da6fa4 PF |
1278 | if (!vgic_present) |
1279 | return -ENXIO; | |
a28ebea2 CD |
1280 | mutex_lock(&kvm->lock); |
1281 | ret = kvm_vgic_create(kvm, KVM_DEV_TYPE_ARM_VGIC_V2); | |
1282 | mutex_unlock(&kvm->lock); | |
1283 | return ret; | |
5863c2ce | 1284 | } |
3401d546 CD |
1285 | case KVM_ARM_SET_DEVICE_ADDR: { |
1286 | struct kvm_arm_device_addr dev_addr; | |
1287 | ||
1288 | if (copy_from_user(&dev_addr, argp, sizeof(dev_addr))) | |
1289 | return -EFAULT; | |
1290 | return kvm_vm_ioctl_set_device_addr(kvm, &dev_addr); | |
1291 | } | |
42c4e0c7 AP |
1292 | case KVM_ARM_PREFERRED_TARGET: { |
1293 | int err; | |
1294 | struct kvm_vcpu_init init; | |
1295 | ||
1296 | err = kvm_vcpu_preferred_target(&init); | |
1297 | if (err) | |
1298 | return err; | |
1299 | ||
1300 | if (copy_to_user(argp, &init, sizeof(init))) | |
1301 | return -EFAULT; | |
1302 | ||
1303 | return 0; | |
1304 | } | |
3401d546 CD |
1305 | default: |
1306 | return -EINVAL; | |
1307 | } | |
749cf76c CD |
1308 | } |
1309 | ||
d157f4a5 | 1310 | static void cpu_init_hyp_mode(void *dummy) |
342cd0ab | 1311 | { |
dac288f7 | 1312 | phys_addr_t pgd_ptr; |
342cd0ab CD |
1313 | unsigned long hyp_stack_ptr; |
1314 | unsigned long stack_page; | |
1315 | unsigned long vector_ptr; | |
1316 | ||
1317 | /* Switch from the HYP stub to our own HYP init vector */ | |
5a677ce0 | 1318 | __hyp_set_vectors(kvm_get_idmap_vector()); |
342cd0ab | 1319 | |
dac288f7 | 1320 | pgd_ptr = kvm_mmu_get_httbr(); |
1436c1aa | 1321 | stack_page = __this_cpu_read(kvm_arm_hyp_stack_page); |
342cd0ab | 1322 | hyp_stack_ptr = stack_page + PAGE_SIZE; |
6840bdd7 | 1323 | vector_ptr = (unsigned long)kvm_get_hyp_vector(); |
342cd0ab | 1324 | |
12fda812 | 1325 | __cpu_init_hyp_mode(pgd_ptr, hyp_stack_ptr, vector_ptr); |
35a2491a | 1326 | __cpu_init_stage2(); |
342cd0ab CD |
1327 | } |
1328 | ||
47eb3cba MZ |
1329 | static void cpu_hyp_reset(void) |
1330 | { | |
1331 | if (!is_kernel_in_hyp_mode()) | |
1332 | __hyp_reset_vectors(); | |
1333 | } | |
1334 | ||
5f5560b1 JM |
1335 | static void cpu_hyp_reinit(void) |
1336 | { | |
1e0cf16c MZ |
1337 | kvm_init_host_cpu_context(&this_cpu_ptr(&kvm_host_data)->host_ctxt); |
1338 | ||
47eb3cba MZ |
1339 | cpu_hyp_reset(); |
1340 | ||
9d47bb0d | 1341 | if (is_kernel_in_hyp_mode()) |
02d50cda | 1342 | kvm_timer_init_vhe(); |
9d47bb0d | 1343 | else |
47eb3cba | 1344 | cpu_init_hyp_mode(NULL); |
5b0d2cc2 | 1345 | |
da5a3ce6 | 1346 | kvm_arm_init_debug(); |
5b0d2cc2 CD |
1347 | |
1348 | if (vgic_present) | |
1349 | kvm_vgic_init_cpu_hardware(); | |
5f5560b1 JM |
1350 | } |
1351 | ||
67f69197 AT |
1352 | static void _kvm_arch_hardware_enable(void *discard) |
1353 | { | |
1354 | if (!__this_cpu_read(kvm_arm_hardware_enabled)) { | |
5f5560b1 | 1355 | cpu_hyp_reinit(); |
67f69197 | 1356 | __this_cpu_write(kvm_arm_hardware_enabled, 1); |
d157f4a5 | 1357 | } |
67f69197 | 1358 | } |
d157f4a5 | 1359 | |
67f69197 AT |
1360 | int kvm_arch_hardware_enable(void) |
1361 | { | |
1362 | _kvm_arch_hardware_enable(NULL); | |
1363 | return 0; | |
342cd0ab CD |
1364 | } |
1365 | ||
67f69197 AT |
1366 | static void _kvm_arch_hardware_disable(void *discard) |
1367 | { | |
1368 | if (__this_cpu_read(kvm_arm_hardware_enabled)) { | |
1369 | cpu_hyp_reset(); | |
1370 | __this_cpu_write(kvm_arm_hardware_enabled, 0); | |
1371 | } | |
1372 | } | |
1373 | ||
1374 | void kvm_arch_hardware_disable(void) | |
1375 | { | |
1376 | _kvm_arch_hardware_disable(NULL); | |
1377 | } | |
d157f4a5 | 1378 | |
1fcf7ce0 LP |
1379 | #ifdef CONFIG_CPU_PM |
1380 | static int hyp_init_cpu_pm_notifier(struct notifier_block *self, | |
1381 | unsigned long cmd, | |
1382 | void *v) | |
1383 | { | |
67f69197 AT |
1384 | /* |
1385 | * kvm_arm_hardware_enabled is left with its old value over | |
1386 | * PM_ENTER->PM_EXIT. It is used to indicate PM_EXIT should | |
1387 | * re-enable hyp. | |
1388 | */ | |
1389 | switch (cmd) { | |
1390 | case CPU_PM_ENTER: | |
1391 | if (__this_cpu_read(kvm_arm_hardware_enabled)) | |
1392 | /* | |
1393 | * don't update kvm_arm_hardware_enabled here | |
1394 | * so that the hardware will be re-enabled | |
1395 | * when we resume. See below. | |
1396 | */ | |
1397 | cpu_hyp_reset(); | |
1398 | ||
1fcf7ce0 | 1399 | return NOTIFY_OK; |
58d6b15e | 1400 | case CPU_PM_ENTER_FAILED: |
67f69197 AT |
1401 | case CPU_PM_EXIT: |
1402 | if (__this_cpu_read(kvm_arm_hardware_enabled)) | |
1403 | /* The hardware was enabled before suspend. */ | |
1404 | cpu_hyp_reinit(); | |
1fcf7ce0 | 1405 | |
67f69197 AT |
1406 | return NOTIFY_OK; |
1407 | ||
1408 | default: | |
1409 | return NOTIFY_DONE; | |
1410 | } | |
1fcf7ce0 LP |
1411 | } |
1412 | ||
1413 | static struct notifier_block hyp_init_cpu_pm_nb = { | |
1414 | .notifier_call = hyp_init_cpu_pm_notifier, | |
1415 | }; | |
1416 | ||
1417 | static void __init hyp_cpu_pm_init(void) | |
1418 | { | |
1419 | cpu_pm_register_notifier(&hyp_init_cpu_pm_nb); | |
1420 | } | |
06a71a24 SH |
1421 | static void __init hyp_cpu_pm_exit(void) |
1422 | { | |
1423 | cpu_pm_unregister_notifier(&hyp_init_cpu_pm_nb); | |
1424 | } | |
1fcf7ce0 LP |
1425 | #else |
1426 | static inline void hyp_cpu_pm_init(void) | |
1427 | { | |
1428 | } | |
06a71a24 SH |
1429 | static inline void hyp_cpu_pm_exit(void) |
1430 | { | |
1431 | } | |
1fcf7ce0 LP |
1432 | #endif |
1433 | ||
1e947bad MZ |
1434 | static int init_common_resources(void) |
1435 | { | |
0f62f0e9 SP |
1436 | kvm_set_ipa_limit(); |
1437 | ||
1e947bad MZ |
1438 | return 0; |
1439 | } | |
1440 | ||
1441 | static int init_subsystems(void) | |
1442 | { | |
67f69197 | 1443 | int err = 0; |
1e947bad | 1444 | |
5f5560b1 | 1445 | /* |
67f69197 | 1446 | * Enable hardware so that subsystem initialisation can access EL2. |
5f5560b1 | 1447 | */ |
67f69197 | 1448 | on_each_cpu(_kvm_arch_hardware_enable, NULL, 1); |
5f5560b1 JM |
1449 | |
1450 | /* | |
1451 | * Register CPU lower-power notifier | |
1452 | */ | |
1453 | hyp_cpu_pm_init(); | |
1454 | ||
1e947bad MZ |
1455 | /* |
1456 | * Init HYP view of VGIC | |
1457 | */ | |
1458 | err = kvm_vgic_hyp_init(); | |
1459 | switch (err) { | |
1460 | case 0: | |
1461 | vgic_present = true; | |
1462 | break; | |
1463 | case -ENODEV: | |
1464 | case -ENXIO: | |
1465 | vgic_present = false; | |
67f69197 | 1466 | err = 0; |
1e947bad MZ |
1467 | break; |
1468 | default: | |
67f69197 | 1469 | goto out; |
1e947bad MZ |
1470 | } |
1471 | ||
1472 | /* | |
1473 | * Init HYP architected timer support | |
1474 | */ | |
f384dcfe | 1475 | err = kvm_timer_hyp_init(vgic_present); |
1e947bad | 1476 | if (err) |
67f69197 | 1477 | goto out; |
1e947bad MZ |
1478 | |
1479 | kvm_perf_init(); | |
1480 | kvm_coproc_table_init(); | |
1481 | ||
67f69197 AT |
1482 | out: |
1483 | on_each_cpu(_kvm_arch_hardware_disable, NULL, 1); | |
1484 | ||
1485 | return err; | |
1e947bad MZ |
1486 | } |
1487 | ||
1488 | static void teardown_hyp_mode(void) | |
1489 | { | |
1490 | int cpu; | |
1491 | ||
1e947bad MZ |
1492 | free_hyp_pgds(); |
1493 | for_each_possible_cpu(cpu) | |
1494 | free_page(per_cpu(kvm_arm_hyp_stack_page, cpu)); | |
06a71a24 | 1495 | hyp_cpu_pm_exit(); |
1e947bad MZ |
1496 | } |
1497 | ||
342cd0ab CD |
1498 | /** |
1499 | * Inits Hyp-mode on all online CPUs | |
1500 | */ | |
1501 | static int init_hyp_mode(void) | |
1502 | { | |
342cd0ab CD |
1503 | int cpu; |
1504 | int err = 0; | |
1505 | ||
1506 | /* | |
1507 | * Allocate Hyp PGD and setup Hyp identity mapping | |
1508 | */ | |
1509 | err = kvm_mmu_init(); | |
1510 | if (err) | |
1511 | goto out_err; | |
1512 | ||
342cd0ab CD |
1513 | /* |
1514 | * Allocate stack pages for Hypervisor-mode | |
1515 | */ | |
1516 | for_each_possible_cpu(cpu) { | |
1517 | unsigned long stack_page; | |
1518 | ||
1519 | stack_page = __get_free_page(GFP_KERNEL); | |
1520 | if (!stack_page) { | |
1521 | err = -ENOMEM; | |
1e947bad | 1522 | goto out_err; |
342cd0ab CD |
1523 | } |
1524 | ||
1525 | per_cpu(kvm_arm_hyp_stack_page, cpu) = stack_page; | |
1526 | } | |
1527 | ||
342cd0ab CD |
1528 | /* |
1529 | * Map the Hyp-code called directly from the host | |
1530 | */ | |
588ab3f9 | 1531 | err = create_hyp_mappings(kvm_ksym_ref(__hyp_text_start), |
59002705 | 1532 | kvm_ksym_ref(__hyp_text_end), PAGE_HYP_EXEC); |
342cd0ab CD |
1533 | if (err) { |
1534 | kvm_err("Cannot map world-switch code\n"); | |
1e947bad | 1535 | goto out_err; |
342cd0ab CD |
1536 | } |
1537 | ||
a0bf9776 | 1538 | err = create_hyp_mappings(kvm_ksym_ref(__start_rodata), |
74a6b888 | 1539 | kvm_ksym_ref(__end_rodata), PAGE_HYP_RO); |
910917bb MZ |
1540 | if (err) { |
1541 | kvm_err("Cannot map rodata section\n"); | |
c8ea0395 MZ |
1542 | goto out_err; |
1543 | } | |
1544 | ||
1545 | err = create_hyp_mappings(kvm_ksym_ref(__bss_start), | |
1546 | kvm_ksym_ref(__bss_stop), PAGE_HYP_RO); | |
1547 | if (err) { | |
1548 | kvm_err("Cannot map bss section\n"); | |
1e947bad | 1549 | goto out_err; |
910917bb MZ |
1550 | } |
1551 | ||
6840bdd7 MZ |
1552 | err = kvm_map_vectors(); |
1553 | if (err) { | |
1554 | kvm_err("Cannot map vectors\n"); | |
1555 | goto out_err; | |
1556 | } | |
1557 | ||
342cd0ab CD |
1558 | /* |
1559 | * Map the Hyp stack pages | |
1560 | */ | |
1561 | for_each_possible_cpu(cpu) { | |
1562 | char *stack_page = (char *)per_cpu(kvm_arm_hyp_stack_page, cpu); | |
c8dddecd MZ |
1563 | err = create_hyp_mappings(stack_page, stack_page + PAGE_SIZE, |
1564 | PAGE_HYP); | |
342cd0ab CD |
1565 | |
1566 | if (err) { | |
1567 | kvm_err("Cannot map hyp stack\n"); | |
1e947bad | 1568 | goto out_err; |
342cd0ab CD |
1569 | } |
1570 | } | |
1571 | ||
342cd0ab | 1572 | for_each_possible_cpu(cpu) { |
630a1685 | 1573 | kvm_host_data_t *cpu_data; |
342cd0ab | 1574 | |
630a1685 | 1575 | cpu_data = per_cpu_ptr(&kvm_host_data, cpu); |
630a1685 | 1576 | err = create_hyp_mappings(cpu_data, cpu_data + 1, PAGE_HYP); |
342cd0ab CD |
1577 | |
1578 | if (err) { | |
3de50da6 | 1579 | kvm_err("Cannot map host CPU state: %d\n", err); |
1e947bad | 1580 | goto out_err; |
342cd0ab CD |
1581 | } |
1582 | } | |
1583 | ||
55e3748e MZ |
1584 | err = hyp_map_aux_data(); |
1585 | if (err) | |
a37f0c3c | 1586 | kvm_err("Cannot map host auxiliary data: %d\n", err); |
55e3748e | 1587 | |
342cd0ab | 1588 | return 0; |
1e947bad | 1589 | |
342cd0ab | 1590 | out_err: |
1e947bad | 1591 | teardown_hyp_mode(); |
342cd0ab CD |
1592 | kvm_err("error initializing Hyp mode: %d\n", err); |
1593 | return err; | |
1594 | } | |
1595 | ||
d4e071ce AP |
1596 | static void check_kvm_target_cpu(void *ret) |
1597 | { | |
1598 | *(int *)ret = kvm_target_cpu(); | |
1599 | } | |
1600 | ||
4429fc64 AP |
1601 | struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr) |
1602 | { | |
1603 | struct kvm_vcpu *vcpu; | |
1604 | int i; | |
1605 | ||
1606 | mpidr &= MPIDR_HWID_BITMASK; | |
1607 | kvm_for_each_vcpu(i, vcpu, kvm) { | |
1608 | if (mpidr == kvm_vcpu_get_mpidr_aff(vcpu)) | |
1609 | return vcpu; | |
1610 | } | |
1611 | return NULL; | |
1612 | } | |
1613 | ||
2412405b EA |
1614 | bool kvm_arch_has_irq_bypass(void) |
1615 | { | |
1616 | return true; | |
1617 | } | |
1618 | ||
1619 | int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, | |
1620 | struct irq_bypass_producer *prod) | |
1621 | { | |
1622 | struct kvm_kernel_irqfd *irqfd = | |
1623 | container_of(cons, struct kvm_kernel_irqfd, consumer); | |
1624 | ||
196b1364 MZ |
1625 | return kvm_vgic_v4_set_forwarding(irqfd->kvm, prod->irq, |
1626 | &irqfd->irq_entry); | |
2412405b EA |
1627 | } |
1628 | void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, | |
1629 | struct irq_bypass_producer *prod) | |
1630 | { | |
1631 | struct kvm_kernel_irqfd *irqfd = | |
1632 | container_of(cons, struct kvm_kernel_irqfd, consumer); | |
1633 | ||
196b1364 MZ |
1634 | kvm_vgic_v4_unset_forwarding(irqfd->kvm, prod->irq, |
1635 | &irqfd->irq_entry); | |
2412405b EA |
1636 | } |
1637 | ||
1638 | void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *cons) | |
1639 | { | |
1640 | struct kvm_kernel_irqfd *irqfd = | |
1641 | container_of(cons, struct kvm_kernel_irqfd, consumer); | |
1642 | ||
1643 | kvm_arm_halt_guest(irqfd->kvm); | |
1644 | } | |
1645 | ||
1646 | void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *cons) | |
1647 | { | |
1648 | struct kvm_kernel_irqfd *irqfd = | |
1649 | container_of(cons, struct kvm_kernel_irqfd, consumer); | |
1650 | ||
1651 | kvm_arm_resume_guest(irqfd->kvm); | |
1652 | } | |
1653 | ||
342cd0ab CD |
1654 | /** |
1655 | * Initialize Hyp-mode and memory mappings on all CPUs. | |
1656 | */ | |
749cf76c CD |
1657 | int kvm_arch_init(void *opaque) |
1658 | { | |
342cd0ab | 1659 | int err; |
d4e071ce | 1660 | int ret, cpu; |
fe7d7b03 | 1661 | bool in_hyp_mode; |
342cd0ab CD |
1662 | |
1663 | if (!is_hyp_mode_available()) { | |
58d0d19a | 1664 | kvm_info("HYP mode not available\n"); |
342cd0ab CD |
1665 | return -ENODEV; |
1666 | } | |
1667 | ||
33e5f4e5 MZ |
1668 | in_hyp_mode = is_kernel_in_hyp_mode(); |
1669 | ||
1670 | if (!in_hyp_mode && kvm_arch_requires_vhe()) { | |
1671 | kvm_pr_unimpl("CPU unsupported in non-VHE mode, not initializing\n"); | |
85acda3b DM |
1672 | return -ENODEV; |
1673 | } | |
1674 | ||
d4e071ce AP |
1675 | for_each_online_cpu(cpu) { |
1676 | smp_call_function_single(cpu, check_kvm_target_cpu, &ret, 1); | |
1677 | if (ret < 0) { | |
1678 | kvm_err("Error, CPU %d not supported!\n", cpu); | |
1679 | return -ENODEV; | |
1680 | } | |
342cd0ab CD |
1681 | } |
1682 | ||
1e947bad | 1683 | err = init_common_resources(); |
342cd0ab | 1684 | if (err) |
1e947bad | 1685 | return err; |
342cd0ab | 1686 | |
a3be836d | 1687 | err = kvm_arm_init_sve(); |
0f062bfe DM |
1688 | if (err) |
1689 | return err; | |
1690 | ||
fe7d7b03 | 1691 | if (!in_hyp_mode) { |
1e947bad | 1692 | err = init_hyp_mode(); |
fe7d7b03 JT |
1693 | if (err) |
1694 | goto out_err; | |
1695 | } | |
8146875d | 1696 | |
1e947bad MZ |
1697 | err = init_subsystems(); |
1698 | if (err) | |
1699 | goto out_hyp; | |
1fcf7ce0 | 1700 | |
fe7d7b03 JT |
1701 | if (in_hyp_mode) |
1702 | kvm_info("VHE mode initialized successfully\n"); | |
1703 | else | |
1704 | kvm_info("Hyp mode initialized successfully\n"); | |
1705 | ||
749cf76c | 1706 | return 0; |
1e947bad MZ |
1707 | |
1708 | out_hyp: | |
fe7d7b03 JT |
1709 | if (!in_hyp_mode) |
1710 | teardown_hyp_mode(); | |
342cd0ab CD |
1711 | out_err: |
1712 | return err; | |
749cf76c CD |
1713 | } |
1714 | ||
1715 | /* NOP: Compiling as a module not supported */ | |
1716 | void kvm_arch_exit(void) | |
1717 | { | |
210552c1 | 1718 | kvm_perf_teardown(); |
749cf76c CD |
1719 | } |
1720 | ||
1721 | static int arm_init(void) | |
1722 | { | |
1723 | int rc = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); | |
1724 | return rc; | |
1725 | } | |
1726 | ||
1727 | module_init(arm_init); |