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