Commit | Line | Data |
---|---|---|
bbf45ba5 HB |
1 | /* |
2 | * This program is free software; you can redistribute it and/or modify | |
3 | * it under the terms of the GNU General Public License, version 2, as | |
4 | * published by the Free Software Foundation. | |
5 | * | |
6 | * This program is distributed in the hope that it will be useful, | |
7 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
8 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
9 | * GNU General Public License for more details. | |
10 | * | |
11 | * You should have received a copy of the GNU General Public License | |
12 | * along with this program; if not, write to the Free Software | |
13 | * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
14 | * | |
15 | * Copyright IBM Corp. 2007 | |
16 | * | |
17 | * Authors: Hollis Blanchard <hollisb@us.ibm.com> | |
18 | * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com> | |
19 | */ | |
20 | ||
21 | #include <linux/errno.h> | |
22 | #include <linux/err.h> | |
23 | #include <linux/kvm_host.h> | |
bbf45ba5 | 24 | #include <linux/vmalloc.h> |
544c6761 | 25 | #include <linux/hrtimer.h> |
bbf45ba5 | 26 | #include <linux/fs.h> |
5a0e3ad6 | 27 | #include <linux/slab.h> |
bbf45ba5 HB |
28 | #include <asm/cputable.h> |
29 | #include <asm/uaccess.h> | |
30 | #include <asm/kvm_ppc.h> | |
83aae4a8 | 31 | #include <asm/tlbflush.h> |
371fefd6 | 32 | #include <asm/cputhreads.h> |
73e75b41 | 33 | #include "timing.h" |
fad7b9b5 | 34 | #include "../mm/mmu_decl.h" |
bbf45ba5 | 35 | |
46f43c6e MT |
36 | #define CREATE_TRACE_POINTS |
37 | #include "trace.h" | |
38 | ||
bbf45ba5 HB |
39 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *v) |
40 | { | |
9202e076 | 41 | return !!(v->arch.pending_exceptions) || |
dfd4d47e | 42 | v->requests; |
bbf45ba5 HB |
43 | } |
44 | ||
b6d33834 CD |
45 | int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) |
46 | { | |
47 | return 1; | |
48 | } | |
49 | ||
2a342ed5 AG |
50 | int kvmppc_kvm_pv(struct kvm_vcpu *vcpu) |
51 | { | |
52 | int nr = kvmppc_get_gpr(vcpu, 11); | |
53 | int r; | |
54 | unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3); | |
55 | unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4); | |
56 | unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5); | |
57 | unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6); | |
58 | unsigned long r2 = 0; | |
59 | ||
60 | if (!(vcpu->arch.shared->msr & MSR_SF)) { | |
61 | /* 32 bit mode */ | |
62 | param1 &= 0xffffffff; | |
63 | param2 &= 0xffffffff; | |
64 | param3 &= 0xffffffff; | |
65 | param4 &= 0xffffffff; | |
66 | } | |
67 | ||
68 | switch (nr) { | |
fdcf8bd7 | 69 | case KVM_HCALL_TOKEN(KVM_HC_PPC_MAP_MAGIC_PAGE): |
5fc87407 AG |
70 | { |
71 | vcpu->arch.magic_page_pa = param1; | |
72 | vcpu->arch.magic_page_ea = param2; | |
73 | ||
b5904972 | 74 | r2 = KVM_MAGIC_FEAT_SR | KVM_MAGIC_FEAT_MAS0_TO_SPRG7; |
7508e16c | 75 | |
fdcf8bd7 | 76 | r = EV_SUCCESS; |
5fc87407 AG |
77 | break; |
78 | } | |
fdcf8bd7 SY |
79 | case KVM_HCALL_TOKEN(KVM_HC_FEATURES): |
80 | r = EV_SUCCESS; | |
bf7ca4bd | 81 | #if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500V2) |
a4cd8b23 | 82 | /* XXX Missing magic page on 44x */ |
5fc87407 AG |
83 | r2 |= (1 << KVM_FEATURE_MAGIC_PAGE); |
84 | #endif | |
2a342ed5 AG |
85 | |
86 | /* Second return value is in r4 */ | |
2a342ed5 | 87 | break; |
9202e076 LYB |
88 | case EV_HCALL_TOKEN(EV_IDLE): |
89 | r = EV_SUCCESS; | |
90 | kvm_vcpu_block(vcpu); | |
91 | clear_bit(KVM_REQ_UNHALT, &vcpu->requests); | |
92 | break; | |
2a342ed5 | 93 | default: |
fdcf8bd7 | 94 | r = EV_UNIMPLEMENTED; |
2a342ed5 AG |
95 | break; |
96 | } | |
97 | ||
7508e16c AG |
98 | kvmppc_set_gpr(vcpu, 4, r2); |
99 | ||
2a342ed5 AG |
100 | return r; |
101 | } | |
bbf45ba5 | 102 | |
af8f38b3 AG |
103 | int kvmppc_sanity_check(struct kvm_vcpu *vcpu) |
104 | { | |
105 | int r = false; | |
106 | ||
107 | /* We have to know what CPU to virtualize */ | |
108 | if (!vcpu->arch.pvr) | |
109 | goto out; | |
110 | ||
111 | /* PAPR only works with book3s_64 */ | |
112 | if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled) | |
113 | goto out; | |
114 | ||
115 | #ifdef CONFIG_KVM_BOOK3S_64_HV | |
116 | /* HV KVM can only do PAPR mode for now */ | |
117 | if (!vcpu->arch.papr_enabled) | |
118 | goto out; | |
119 | #endif | |
120 | ||
d30f6e48 SW |
121 | #ifdef CONFIG_KVM_BOOKE_HV |
122 | if (!cpu_has_feature(CPU_FTR_EMB_HV)) | |
123 | goto out; | |
124 | #endif | |
125 | ||
af8f38b3 AG |
126 | r = true; |
127 | ||
128 | out: | |
129 | vcpu->arch.sane = r; | |
130 | return r ? 0 : -EINVAL; | |
131 | } | |
132 | ||
bbf45ba5 HB |
133 | int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu) |
134 | { | |
135 | enum emulation_result er; | |
136 | int r; | |
137 | ||
138 | er = kvmppc_emulate_instruction(run, vcpu); | |
139 | switch (er) { | |
140 | case EMULATE_DONE: | |
141 | /* Future optimization: only reload non-volatiles if they were | |
142 | * actually modified. */ | |
143 | r = RESUME_GUEST_NV; | |
144 | break; | |
145 | case EMULATE_DO_MMIO: | |
146 | run->exit_reason = KVM_EXIT_MMIO; | |
147 | /* We must reload nonvolatiles because "update" load/store | |
148 | * instructions modify register state. */ | |
149 | /* Future optimization: only reload non-volatiles if they were | |
150 | * actually modified. */ | |
151 | r = RESUME_HOST_NV; | |
152 | break; | |
153 | case EMULATE_FAIL: | |
154 | /* XXX Deliver Program interrupt to guest. */ | |
155 | printk(KERN_EMERG "%s: emulation failed (%08x)\n", __func__, | |
c7f38f46 | 156 | kvmppc_get_last_inst(vcpu)); |
bbf45ba5 HB |
157 | r = RESUME_HOST; |
158 | break; | |
159 | default: | |
160 | BUG(); | |
161 | } | |
162 | ||
163 | return r; | |
164 | } | |
165 | ||
10474ae8 | 166 | int kvm_arch_hardware_enable(void *garbage) |
bbf45ba5 | 167 | { |
10474ae8 | 168 | return 0; |
bbf45ba5 HB |
169 | } |
170 | ||
171 | void kvm_arch_hardware_disable(void *garbage) | |
172 | { | |
173 | } | |
174 | ||
175 | int kvm_arch_hardware_setup(void) | |
176 | { | |
177 | return 0; | |
178 | } | |
179 | ||
180 | void kvm_arch_hardware_unsetup(void) | |
181 | { | |
182 | } | |
183 | ||
184 | void kvm_arch_check_processor_compat(void *rtn) | |
185 | { | |
9dd921cf | 186 | *(int *)rtn = kvmppc_core_check_processor_compat(); |
bbf45ba5 HB |
187 | } |
188 | ||
e08b9637 | 189 | int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) |
bbf45ba5 | 190 | { |
e08b9637 CO |
191 | if (type) |
192 | return -EINVAL; | |
193 | ||
f9e0554d | 194 | return kvmppc_core_init_vm(kvm); |
bbf45ba5 HB |
195 | } |
196 | ||
d89f5eff | 197 | void kvm_arch_destroy_vm(struct kvm *kvm) |
bbf45ba5 HB |
198 | { |
199 | unsigned int i; | |
988a2cae | 200 | struct kvm_vcpu *vcpu; |
bbf45ba5 | 201 | |
988a2cae GN |
202 | kvm_for_each_vcpu(i, vcpu, kvm) |
203 | kvm_arch_vcpu_free(vcpu); | |
204 | ||
205 | mutex_lock(&kvm->lock); | |
206 | for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) | |
207 | kvm->vcpus[i] = NULL; | |
208 | ||
209 | atomic_set(&kvm->online_vcpus, 0); | |
f9e0554d PM |
210 | |
211 | kvmppc_core_destroy_vm(kvm); | |
212 | ||
988a2cae | 213 | mutex_unlock(&kvm->lock); |
bbf45ba5 HB |
214 | } |
215 | ||
ad8ba2cd SY |
216 | void kvm_arch_sync_events(struct kvm *kvm) |
217 | { | |
218 | } | |
219 | ||
bbf45ba5 HB |
220 | int kvm_dev_ioctl_check_extension(long ext) |
221 | { | |
222 | int r; | |
223 | ||
224 | switch (ext) { | |
5ce941ee SW |
225 | #ifdef CONFIG_BOOKE |
226 | case KVM_CAP_PPC_BOOKE_SREGS: | |
227 | #else | |
e15a1137 | 228 | case KVM_CAP_PPC_SEGSTATE: |
1022fc3d | 229 | case KVM_CAP_PPC_HIOR: |
930b412a | 230 | case KVM_CAP_PPC_PAPR: |
5ce941ee | 231 | #endif |
18978768 | 232 | case KVM_CAP_PPC_UNSET_IRQ: |
7b4203e8 | 233 | case KVM_CAP_PPC_IRQ_LEVEL: |
71fbfd5f | 234 | case KVM_CAP_ENABLE_CAP: |
e24ed81f | 235 | case KVM_CAP_ONE_REG: |
de56a948 PM |
236 | r = 1; |
237 | break; | |
238 | #ifndef CONFIG_KVM_BOOK3S_64_HV | |
239 | case KVM_CAP_PPC_PAIRED_SINGLES: | |
ad0a048b | 240 | case KVM_CAP_PPC_OSI: |
15711e9c | 241 | case KVM_CAP_PPC_GET_PVINFO: |
bf7ca4bd | 242 | #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC) |
dc83b8bc SW |
243 | case KVM_CAP_SW_TLB: |
244 | #endif | |
e15a1137 AG |
245 | r = 1; |
246 | break; | |
588968b6 LV |
247 | case KVM_CAP_COALESCED_MMIO: |
248 | r = KVM_COALESCED_MMIO_PAGE_OFFSET; | |
249 | break; | |
54738c09 | 250 | #endif |
f31e65e1 | 251 | #ifdef CONFIG_PPC_BOOK3S_64 |
54738c09 | 252 | case KVM_CAP_SPAPR_TCE: |
32fad281 | 253 | case KVM_CAP_PPC_ALLOC_HTAB: |
54738c09 DG |
254 | r = 1; |
255 | break; | |
f31e65e1 BH |
256 | #endif /* CONFIG_PPC_BOOK3S_64 */ |
257 | #ifdef CONFIG_KVM_BOOK3S_64_HV | |
371fefd6 PM |
258 | case KVM_CAP_PPC_SMT: |
259 | r = threads_per_core; | |
260 | break; | |
aa04b4cc PM |
261 | case KVM_CAP_PPC_RMA: |
262 | r = 1; | |
9e368f29 PM |
263 | /* PPC970 requires an RMA */ |
264 | if (cpu_has_feature(CPU_FTR_ARCH_201)) | |
265 | r = 2; | |
aa04b4cc | 266 | break; |
f4800b1f | 267 | #endif |
342d3db7 | 268 | case KVM_CAP_SYNC_MMU: |
f4800b1f | 269 | #ifdef CONFIG_KVM_BOOK3S_64_HV |
342d3db7 | 270 | r = cpu_has_feature(CPU_FTR_ARCH_206) ? 1 : 0; |
f4800b1f AG |
271 | #elif defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
272 | r = 1; | |
273 | #else | |
274 | r = 0; | |
de56a948 | 275 | #endif |
f4800b1f | 276 | break; |
b5434032 ME |
277 | case KVM_CAP_NR_VCPUS: |
278 | /* | |
279 | * Recommending a number of CPUs is somewhat arbitrary; we | |
280 | * return the number of present CPUs for -HV (since a host | |
281 | * will have secondary threads "offline"), and for other KVM | |
282 | * implementations just count online CPUs. | |
283 | */ | |
284 | #ifdef CONFIG_KVM_BOOK3S_64_HV | |
285 | r = num_present_cpus(); | |
286 | #else | |
287 | r = num_online_cpus(); | |
288 | #endif | |
289 | break; | |
290 | case KVM_CAP_MAX_VCPUS: | |
291 | r = KVM_MAX_VCPUS; | |
292 | break; | |
5b74716e BH |
293 | #ifdef CONFIG_PPC_BOOK3S_64 |
294 | case KVM_CAP_PPC_GET_SMMU_INFO: | |
295 | r = 1; | |
296 | break; | |
297 | #endif | |
bbf45ba5 HB |
298 | default: |
299 | r = 0; | |
300 | break; | |
301 | } | |
302 | return r; | |
303 | ||
304 | } | |
305 | ||
306 | long kvm_arch_dev_ioctl(struct file *filp, | |
307 | unsigned int ioctl, unsigned long arg) | |
308 | { | |
309 | return -EINVAL; | |
310 | } | |
311 | ||
db3fe4eb TY |
312 | void kvm_arch_free_memslot(struct kvm_memory_slot *free, |
313 | struct kvm_memory_slot *dont) | |
314 | { | |
d89cc617 TY |
315 | if (!dont || free->arch.rmap != dont->arch.rmap) { |
316 | vfree(free->arch.rmap); | |
317 | free->arch.rmap = NULL; | |
318 | } | |
db3fe4eb TY |
319 | } |
320 | ||
321 | int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages) | |
322 | { | |
d89cc617 TY |
323 | slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap)); |
324 | if (!slot->arch.rmap) | |
325 | return -ENOMEM; | |
326 | ||
db3fe4eb TY |
327 | return 0; |
328 | } | |
329 | ||
f7784b8e MT |
330 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
331 | struct kvm_memory_slot *memslot, | |
332 | struct kvm_memory_slot old, | |
333 | struct kvm_userspace_memory_region *mem, | |
334 | int user_alloc) | |
bbf45ba5 | 335 | { |
f9e0554d | 336 | return kvmppc_core_prepare_memory_region(kvm, mem); |
bbf45ba5 HB |
337 | } |
338 | ||
f7784b8e MT |
339 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
340 | struct kvm_userspace_memory_region *mem, | |
341 | struct kvm_memory_slot old, | |
342 | int user_alloc) | |
343 | { | |
f9e0554d | 344 | kvmppc_core_commit_memory_region(kvm, mem); |
f7784b8e MT |
345 | } |
346 | ||
2df72e9b MT |
347 | void kvm_arch_flush_shadow_all(struct kvm *kvm) |
348 | { | |
349 | } | |
f7784b8e | 350 | |
2df72e9b MT |
351 | void kvm_arch_flush_shadow_memslot(struct kvm *kvm, |
352 | struct kvm_memory_slot *slot) | |
34d4cb8f MT |
353 | { |
354 | } | |
355 | ||
bbf45ba5 HB |
356 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id) |
357 | { | |
73e75b41 HB |
358 | struct kvm_vcpu *vcpu; |
359 | vcpu = kvmppc_core_vcpu_create(kvm, id); | |
03cdab53 ME |
360 | if (!IS_ERR(vcpu)) { |
361 | vcpu->arch.wqp = &vcpu->wq; | |
06056bfb | 362 | kvmppc_create_vcpu_debugfs(vcpu, id); |
03cdab53 | 363 | } |
73e75b41 | 364 | return vcpu; |
bbf45ba5 HB |
365 | } |
366 | ||
367 | void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu) | |
368 | { | |
a595405d AG |
369 | /* Make sure we're not using the vcpu anymore */ |
370 | hrtimer_cancel(&vcpu->arch.dec_timer); | |
371 | tasklet_kill(&vcpu->arch.tasklet); | |
372 | ||
73e75b41 | 373 | kvmppc_remove_vcpu_debugfs(vcpu); |
db93f574 | 374 | kvmppc_core_vcpu_free(vcpu); |
bbf45ba5 HB |
375 | } |
376 | ||
377 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) | |
378 | { | |
379 | kvm_arch_vcpu_free(vcpu); | |
380 | } | |
381 | ||
382 | int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) | |
383 | { | |
9dd921cf | 384 | return kvmppc_core_pending_dec(vcpu); |
bbf45ba5 HB |
385 | } |
386 | ||
544c6761 AG |
387 | /* |
388 | * low level hrtimer wake routine. Because this runs in hardirq context | |
389 | * we schedule a tasklet to do the real work. | |
390 | */ | |
391 | enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer) | |
392 | { | |
393 | struct kvm_vcpu *vcpu; | |
394 | ||
395 | vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer); | |
396 | tasklet_schedule(&vcpu->arch.tasklet); | |
397 | ||
398 | return HRTIMER_NORESTART; | |
399 | } | |
400 | ||
bbf45ba5 HB |
401 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) |
402 | { | |
544c6761 AG |
403 | hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS); |
404 | tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu); | |
405 | vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup; | |
de56a948 | 406 | vcpu->arch.dec_expires = ~(u64)0; |
bbf45ba5 | 407 | |
09000adb BB |
408 | #ifdef CONFIG_KVM_EXIT_TIMING |
409 | mutex_init(&vcpu->arch.exit_timing_lock); | |
410 | #endif | |
411 | ||
bbf45ba5 HB |
412 | return 0; |
413 | } | |
414 | ||
415 | void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) | |
416 | { | |
ecc0981f | 417 | kvmppc_mmu_destroy(vcpu); |
bbf45ba5 HB |
418 | } |
419 | ||
420 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) | |
421 | { | |
eab17672 SW |
422 | #ifdef CONFIG_BOOKE |
423 | /* | |
424 | * vrsave (formerly usprg0) isn't used by Linux, but may | |
425 | * be used by the guest. | |
426 | * | |
427 | * On non-booke this is associated with Altivec and | |
428 | * is handled by code in book3s.c. | |
429 | */ | |
430 | mtspr(SPRN_VRSAVE, vcpu->arch.vrsave); | |
431 | #endif | |
9dd921cf | 432 | kvmppc_core_vcpu_load(vcpu, cpu); |
de56a948 | 433 | vcpu->cpu = smp_processor_id(); |
bbf45ba5 HB |
434 | } |
435 | ||
436 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) | |
437 | { | |
9dd921cf | 438 | kvmppc_core_vcpu_put(vcpu); |
eab17672 SW |
439 | #ifdef CONFIG_BOOKE |
440 | vcpu->arch.vrsave = mfspr(SPRN_VRSAVE); | |
441 | #endif | |
de56a948 | 442 | vcpu->cpu = -1; |
bbf45ba5 HB |
443 | } |
444 | ||
d0bfb940 | 445 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
f5d0906b | 446 | struct kvm_guest_debug *dbg) |
bbf45ba5 | 447 | { |
f5d0906b | 448 | return -EINVAL; |
bbf45ba5 HB |
449 | } |
450 | ||
451 | static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu, | |
452 | struct kvm_run *run) | |
453 | { | |
8e5b26b5 | 454 | kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, run->dcr.data); |
bbf45ba5 HB |
455 | } |
456 | ||
457 | static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu, | |
458 | struct kvm_run *run) | |
459 | { | |
69b61833 | 460 | u64 uninitialized_var(gpr); |
bbf45ba5 | 461 | |
8e5b26b5 | 462 | if (run->mmio.len > sizeof(gpr)) { |
bbf45ba5 HB |
463 | printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len); |
464 | return; | |
465 | } | |
466 | ||
467 | if (vcpu->arch.mmio_is_bigendian) { | |
468 | switch (run->mmio.len) { | |
b104d066 | 469 | case 8: gpr = *(u64 *)run->mmio.data; break; |
8e5b26b5 AG |
470 | case 4: gpr = *(u32 *)run->mmio.data; break; |
471 | case 2: gpr = *(u16 *)run->mmio.data; break; | |
472 | case 1: gpr = *(u8 *)run->mmio.data; break; | |
bbf45ba5 HB |
473 | } |
474 | } else { | |
475 | /* Convert BE data from userland back to LE. */ | |
476 | switch (run->mmio.len) { | |
8e5b26b5 AG |
477 | case 4: gpr = ld_le32((u32 *)run->mmio.data); break; |
478 | case 2: gpr = ld_le16((u16 *)run->mmio.data); break; | |
479 | case 1: gpr = *(u8 *)run->mmio.data; break; | |
bbf45ba5 HB |
480 | } |
481 | } | |
8e5b26b5 | 482 | |
3587d534 AG |
483 | if (vcpu->arch.mmio_sign_extend) { |
484 | switch (run->mmio.len) { | |
485 | #ifdef CONFIG_PPC64 | |
486 | case 4: | |
487 | gpr = (s64)(s32)gpr; | |
488 | break; | |
489 | #endif | |
490 | case 2: | |
491 | gpr = (s64)(s16)gpr; | |
492 | break; | |
493 | case 1: | |
494 | gpr = (s64)(s8)gpr; | |
495 | break; | |
496 | } | |
497 | } | |
498 | ||
8e5b26b5 | 499 | kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr); |
b104d066 | 500 | |
b3c5d3c2 AG |
501 | switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) { |
502 | case KVM_MMIO_REG_GPR: | |
b104d066 AG |
503 | kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr); |
504 | break; | |
b3c5d3c2 AG |
505 | case KVM_MMIO_REG_FPR: |
506 | vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr; | |
b104d066 | 507 | break; |
287d5611 | 508 | #ifdef CONFIG_PPC_BOOK3S |
b3c5d3c2 AG |
509 | case KVM_MMIO_REG_QPR: |
510 | vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr; | |
b104d066 | 511 | break; |
b3c5d3c2 AG |
512 | case KVM_MMIO_REG_FQPR: |
513 | vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr; | |
514 | vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr; | |
b104d066 | 515 | break; |
287d5611 | 516 | #endif |
b104d066 AG |
517 | default: |
518 | BUG(); | |
519 | } | |
bbf45ba5 HB |
520 | } |
521 | ||
522 | int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu, | |
523 | unsigned int rt, unsigned int bytes, int is_bigendian) | |
524 | { | |
525 | if (bytes > sizeof(run->mmio.data)) { | |
526 | printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__, | |
527 | run->mmio.len); | |
528 | } | |
529 | ||
530 | run->mmio.phys_addr = vcpu->arch.paddr_accessed; | |
531 | run->mmio.len = bytes; | |
532 | run->mmio.is_write = 0; | |
533 | ||
534 | vcpu->arch.io_gpr = rt; | |
535 | vcpu->arch.mmio_is_bigendian = is_bigendian; | |
536 | vcpu->mmio_needed = 1; | |
537 | vcpu->mmio_is_write = 0; | |
3587d534 | 538 | vcpu->arch.mmio_sign_extend = 0; |
bbf45ba5 HB |
539 | |
540 | return EMULATE_DO_MMIO; | |
541 | } | |
542 | ||
3587d534 AG |
543 | /* Same as above, but sign extends */ |
544 | int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu, | |
545 | unsigned int rt, unsigned int bytes, int is_bigendian) | |
546 | { | |
547 | int r; | |
548 | ||
549 | r = kvmppc_handle_load(run, vcpu, rt, bytes, is_bigendian); | |
550 | vcpu->arch.mmio_sign_extend = 1; | |
551 | ||
552 | return r; | |
553 | } | |
554 | ||
bbf45ba5 | 555 | int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu, |
b104d066 | 556 | u64 val, unsigned int bytes, int is_bigendian) |
bbf45ba5 HB |
557 | { |
558 | void *data = run->mmio.data; | |
559 | ||
560 | if (bytes > sizeof(run->mmio.data)) { | |
561 | printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__, | |
562 | run->mmio.len); | |
563 | } | |
564 | ||
565 | run->mmio.phys_addr = vcpu->arch.paddr_accessed; | |
566 | run->mmio.len = bytes; | |
567 | run->mmio.is_write = 1; | |
568 | vcpu->mmio_needed = 1; | |
569 | vcpu->mmio_is_write = 1; | |
570 | ||
571 | /* Store the value at the lowest bytes in 'data'. */ | |
572 | if (is_bigendian) { | |
573 | switch (bytes) { | |
b104d066 | 574 | case 8: *(u64 *)data = val; break; |
bbf45ba5 HB |
575 | case 4: *(u32 *)data = val; break; |
576 | case 2: *(u16 *)data = val; break; | |
577 | case 1: *(u8 *)data = val; break; | |
578 | } | |
579 | } else { | |
580 | /* Store LE value into 'data'. */ | |
581 | switch (bytes) { | |
582 | case 4: st_le32(data, val); break; | |
583 | case 2: st_le16(data, val); break; | |
584 | case 1: *(u8 *)data = val; break; | |
585 | } | |
586 | } | |
587 | ||
588 | return EMULATE_DO_MMIO; | |
589 | } | |
590 | ||
591 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) | |
592 | { | |
593 | int r; | |
594 | sigset_t sigsaved; | |
595 | ||
596 | if (vcpu->sigset_active) | |
597 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); | |
598 | ||
599 | if (vcpu->mmio_needed) { | |
600 | if (!vcpu->mmio_is_write) | |
601 | kvmppc_complete_mmio_load(vcpu, run); | |
602 | vcpu->mmio_needed = 0; | |
603 | } else if (vcpu->arch.dcr_needed) { | |
604 | if (!vcpu->arch.dcr_is_write) | |
605 | kvmppc_complete_dcr_load(vcpu, run); | |
606 | vcpu->arch.dcr_needed = 0; | |
ad0a048b AG |
607 | } else if (vcpu->arch.osi_needed) { |
608 | u64 *gprs = run->osi.gprs; | |
609 | int i; | |
610 | ||
611 | for (i = 0; i < 32; i++) | |
612 | kvmppc_set_gpr(vcpu, i, gprs[i]); | |
613 | vcpu->arch.osi_needed = 0; | |
de56a948 PM |
614 | } else if (vcpu->arch.hcall_needed) { |
615 | int i; | |
616 | ||
617 | kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret); | |
618 | for (i = 0; i < 9; ++i) | |
619 | kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]); | |
620 | vcpu->arch.hcall_needed = 0; | |
bbf45ba5 HB |
621 | } |
622 | ||
df6909e5 | 623 | r = kvmppc_vcpu_run(run, vcpu); |
bbf45ba5 HB |
624 | |
625 | if (vcpu->sigset_active) | |
626 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); | |
627 | ||
628 | return r; | |
629 | } | |
630 | ||
631 | int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq) | |
632 | { | |
19ccb76a | 633 | if (irq->irq == KVM_INTERRUPT_UNSET) { |
18978768 | 634 | kvmppc_core_dequeue_external(vcpu, irq); |
19ccb76a PM |
635 | return 0; |
636 | } | |
637 | ||
638 | kvmppc_core_queue_external(vcpu, irq); | |
b6d33834 | 639 | |
dfd4d47e | 640 | kvm_vcpu_kick(vcpu); |
45c5eb67 | 641 | |
bbf45ba5 HB |
642 | return 0; |
643 | } | |
644 | ||
71fbfd5f AG |
645 | static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, |
646 | struct kvm_enable_cap *cap) | |
647 | { | |
648 | int r; | |
649 | ||
650 | if (cap->flags) | |
651 | return -EINVAL; | |
652 | ||
653 | switch (cap->cap) { | |
ad0a048b AG |
654 | case KVM_CAP_PPC_OSI: |
655 | r = 0; | |
656 | vcpu->arch.osi_enabled = true; | |
657 | break; | |
930b412a AG |
658 | case KVM_CAP_PPC_PAPR: |
659 | r = 0; | |
660 | vcpu->arch.papr_enabled = true; | |
661 | break; | |
bf7ca4bd | 662 | #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC) |
dc83b8bc SW |
663 | case KVM_CAP_SW_TLB: { |
664 | struct kvm_config_tlb cfg; | |
665 | void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0]; | |
666 | ||
667 | r = -EFAULT; | |
668 | if (copy_from_user(&cfg, user_ptr, sizeof(cfg))) | |
669 | break; | |
670 | ||
671 | r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg); | |
672 | break; | |
673 | } | |
674 | #endif | |
71fbfd5f AG |
675 | default: |
676 | r = -EINVAL; | |
677 | break; | |
678 | } | |
679 | ||
af8f38b3 AG |
680 | if (!r) |
681 | r = kvmppc_sanity_check(vcpu); | |
682 | ||
71fbfd5f AG |
683 | return r; |
684 | } | |
685 | ||
bbf45ba5 HB |
686 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
687 | struct kvm_mp_state *mp_state) | |
688 | { | |
689 | return -EINVAL; | |
690 | } | |
691 | ||
692 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, | |
693 | struct kvm_mp_state *mp_state) | |
694 | { | |
695 | return -EINVAL; | |
696 | } | |
697 | ||
698 | long kvm_arch_vcpu_ioctl(struct file *filp, | |
699 | unsigned int ioctl, unsigned long arg) | |
700 | { | |
701 | struct kvm_vcpu *vcpu = filp->private_data; | |
702 | void __user *argp = (void __user *)arg; | |
703 | long r; | |
704 | ||
93736624 AK |
705 | switch (ioctl) { |
706 | case KVM_INTERRUPT: { | |
bbf45ba5 HB |
707 | struct kvm_interrupt irq; |
708 | r = -EFAULT; | |
709 | if (copy_from_user(&irq, argp, sizeof(irq))) | |
93736624 | 710 | goto out; |
bbf45ba5 | 711 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
93736624 | 712 | goto out; |
bbf45ba5 | 713 | } |
19483d14 | 714 | |
71fbfd5f AG |
715 | case KVM_ENABLE_CAP: |
716 | { | |
717 | struct kvm_enable_cap cap; | |
718 | r = -EFAULT; | |
719 | if (copy_from_user(&cap, argp, sizeof(cap))) | |
720 | goto out; | |
721 | r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); | |
722 | break; | |
723 | } | |
dc83b8bc | 724 | |
e24ed81f AG |
725 | case KVM_SET_ONE_REG: |
726 | case KVM_GET_ONE_REG: | |
727 | { | |
728 | struct kvm_one_reg reg; | |
729 | r = -EFAULT; | |
730 | if (copy_from_user(®, argp, sizeof(reg))) | |
731 | goto out; | |
732 | if (ioctl == KVM_SET_ONE_REG) | |
733 | r = kvm_vcpu_ioctl_set_one_reg(vcpu, ®); | |
734 | else | |
735 | r = kvm_vcpu_ioctl_get_one_reg(vcpu, ®); | |
736 | break; | |
737 | } | |
738 | ||
bf7ca4bd | 739 | #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC) |
dc83b8bc SW |
740 | case KVM_DIRTY_TLB: { |
741 | struct kvm_dirty_tlb dirty; | |
742 | r = -EFAULT; | |
743 | if (copy_from_user(&dirty, argp, sizeof(dirty))) | |
744 | goto out; | |
745 | r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty); | |
746 | break; | |
747 | } | |
748 | #endif | |
bbf45ba5 HB |
749 | default: |
750 | r = -EINVAL; | |
751 | } | |
752 | ||
753 | out: | |
754 | return r; | |
755 | } | |
756 | ||
5b1c1493 CO |
757 | int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) |
758 | { | |
759 | return VM_FAULT_SIGBUS; | |
760 | } | |
761 | ||
15711e9c AG |
762 | static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo) |
763 | { | |
784bafac SY |
764 | u32 inst_nop = 0x60000000; |
765 | #ifdef CONFIG_KVM_BOOKE_HV | |
766 | u32 inst_sc1 = 0x44000022; | |
767 | pvinfo->hcall[0] = inst_sc1; | |
768 | pvinfo->hcall[1] = inst_nop; | |
769 | pvinfo->hcall[2] = inst_nop; | |
770 | pvinfo->hcall[3] = inst_nop; | |
771 | #else | |
15711e9c AG |
772 | u32 inst_lis = 0x3c000000; |
773 | u32 inst_ori = 0x60000000; | |
15711e9c AG |
774 | u32 inst_sc = 0x44000002; |
775 | u32 inst_imm_mask = 0xffff; | |
776 | ||
777 | /* | |
778 | * The hypercall to get into KVM from within guest context is as | |
779 | * follows: | |
780 | * | |
781 | * lis r0, r0, KVM_SC_MAGIC_R0@h | |
782 | * ori r0, KVM_SC_MAGIC_R0@l | |
783 | * sc | |
784 | * nop | |
785 | */ | |
786 | pvinfo->hcall[0] = inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask); | |
787 | pvinfo->hcall[1] = inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask); | |
788 | pvinfo->hcall[2] = inst_sc; | |
789 | pvinfo->hcall[3] = inst_nop; | |
784bafac | 790 | #endif |
15711e9c | 791 | |
9202e076 LYB |
792 | pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE; |
793 | ||
15711e9c AG |
794 | return 0; |
795 | } | |
796 | ||
bbf45ba5 HB |
797 | long kvm_arch_vm_ioctl(struct file *filp, |
798 | unsigned int ioctl, unsigned long arg) | |
799 | { | |
15711e9c | 800 | void __user *argp = (void __user *)arg; |
bbf45ba5 HB |
801 | long r; |
802 | ||
803 | switch (ioctl) { | |
15711e9c AG |
804 | case KVM_PPC_GET_PVINFO: { |
805 | struct kvm_ppc_pvinfo pvinfo; | |
d8cdddcd | 806 | memset(&pvinfo, 0, sizeof(pvinfo)); |
15711e9c AG |
807 | r = kvm_vm_ioctl_get_pvinfo(&pvinfo); |
808 | if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) { | |
809 | r = -EFAULT; | |
810 | goto out; | |
811 | } | |
812 | ||
813 | break; | |
814 | } | |
f31e65e1 | 815 | #ifdef CONFIG_PPC_BOOK3S_64 |
54738c09 DG |
816 | case KVM_CREATE_SPAPR_TCE: { |
817 | struct kvm_create_spapr_tce create_tce; | |
818 | struct kvm *kvm = filp->private_data; | |
819 | ||
820 | r = -EFAULT; | |
821 | if (copy_from_user(&create_tce, argp, sizeof(create_tce))) | |
822 | goto out; | |
823 | r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce); | |
824 | goto out; | |
825 | } | |
f31e65e1 | 826 | #endif /* CONFIG_PPC_BOOK3S_64 */ |
aa04b4cc | 827 | |
f31e65e1 | 828 | #ifdef CONFIG_KVM_BOOK3S_64_HV |
aa04b4cc PM |
829 | case KVM_ALLOCATE_RMA: { |
830 | struct kvm *kvm = filp->private_data; | |
831 | struct kvm_allocate_rma rma; | |
832 | ||
833 | r = kvm_vm_ioctl_allocate_rma(kvm, &rma); | |
834 | if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma))) | |
835 | r = -EFAULT; | |
836 | break; | |
837 | } | |
32fad281 PM |
838 | |
839 | case KVM_PPC_ALLOCATE_HTAB: { | |
840 | struct kvm *kvm = filp->private_data; | |
841 | u32 htab_order; | |
842 | ||
843 | r = -EFAULT; | |
844 | if (get_user(htab_order, (u32 __user *)argp)) | |
845 | break; | |
846 | r = kvmppc_alloc_reset_hpt(kvm, &htab_order); | |
847 | if (r) | |
848 | break; | |
849 | r = -EFAULT; | |
850 | if (put_user(htab_order, (u32 __user *)argp)) | |
851 | break; | |
852 | r = 0; | |
853 | break; | |
854 | } | |
54738c09 DG |
855 | #endif /* CONFIG_KVM_BOOK3S_64_HV */ |
856 | ||
5b74716e BH |
857 | #ifdef CONFIG_PPC_BOOK3S_64 |
858 | case KVM_PPC_GET_SMMU_INFO: { | |
859 | struct kvm *kvm = filp->private_data; | |
860 | struct kvm_ppc_smmu_info info; | |
861 | ||
862 | memset(&info, 0, sizeof(info)); | |
863 | r = kvm_vm_ioctl_get_smmu_info(kvm, &info); | |
864 | if (r >= 0 && copy_to_user(argp, &info, sizeof(info))) | |
865 | r = -EFAULT; | |
866 | break; | |
867 | } | |
868 | #endif /* CONFIG_PPC_BOOK3S_64 */ | |
bbf45ba5 | 869 | default: |
367e1319 | 870 | r = -ENOTTY; |
bbf45ba5 HB |
871 | } |
872 | ||
15711e9c | 873 | out: |
bbf45ba5 HB |
874 | return r; |
875 | } | |
876 | ||
043cc4d7 SW |
877 | static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)]; |
878 | static unsigned long nr_lpids; | |
879 | ||
880 | long kvmppc_alloc_lpid(void) | |
881 | { | |
882 | long lpid; | |
883 | ||
884 | do { | |
885 | lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS); | |
886 | if (lpid >= nr_lpids) { | |
887 | pr_err("%s: No LPIDs free\n", __func__); | |
888 | return -ENOMEM; | |
889 | } | |
890 | } while (test_and_set_bit(lpid, lpid_inuse)); | |
891 | ||
892 | return lpid; | |
893 | } | |
894 | ||
895 | void kvmppc_claim_lpid(long lpid) | |
896 | { | |
897 | set_bit(lpid, lpid_inuse); | |
898 | } | |
899 | ||
900 | void kvmppc_free_lpid(long lpid) | |
901 | { | |
902 | clear_bit(lpid, lpid_inuse); | |
903 | } | |
904 | ||
905 | void kvmppc_init_lpid(unsigned long nr_lpids_param) | |
906 | { | |
907 | nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param); | |
908 | memset(lpid_inuse, 0, sizeof(lpid_inuse)); | |
909 | } | |
910 | ||
bbf45ba5 HB |
911 | int kvm_arch_init(void *opaque) |
912 | { | |
913 | return 0; | |
914 | } | |
915 | ||
916 | void kvm_arch_exit(void) | |
917 | { | |
918 | } |