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