1 /*
2 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
3 *
4 * Authors:
5 * Alexander Graf <agraf@suse.de>
6 * Kevin Wolf <mail@kevin-wolf.de>
7 *
8 * Description:
9 * This file is derived from arch/powerpc/kvm/44x.c,
10 * by Hollis Blanchard <hollisb@us.ibm.com>.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License, version 2, as
14 * published by the Free Software Foundation.
15 */
16
17 #include <linux/kvm_host.h>
18 #include <linux/err.h>
19 #include <linux/export.h>
20 #include <linux/slab.h>
21 #include <linux/module.h>
22 #include <linux/miscdevice.h>
23
24 #include <asm/reg.h>
25 #include <asm/cputable.h>
26 #include <asm/cacheflush.h>
27 #include <asm/tlbflush.h>
28 #include <asm/uaccess.h>
29 #include <asm/io.h>
30 #include <asm/kvm_ppc.h>
31 #include <asm/kvm_book3s.h>
32 #include <asm/mmu_context.h>
33 #include <asm/page.h>
34 #include <linux/gfp.h>
35 #include <linux/sched.h>
36 #include <linux/vmalloc.h>
37 #include <linux/highmem.h>
38
39 #include "book3s.h"
40 #include "trace.h"
41
42 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
43
44 /* #define EXIT_DEBUG */
45
46 struct kvm_stats_debugfs_item debugfs_entries[] = {
47 { "exits", VCPU_STAT(sum_exits) },
48 { "mmio", VCPU_STAT(mmio_exits) },
49 { "sig", VCPU_STAT(signal_exits) },
50 { "sysc", VCPU_STAT(syscall_exits) },
51 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
52 { "dec", VCPU_STAT(dec_exits) },
53 { "ext_intr", VCPU_STAT(ext_intr_exits) },
54 { "queue_intr", VCPU_STAT(queue_intr) },
55 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
56 { "pf_storage", VCPU_STAT(pf_storage) },
57 { "sp_storage", VCPU_STAT(sp_storage) },
58 { "pf_instruc", VCPU_STAT(pf_instruc) },
59 { "sp_instruc", VCPU_STAT(sp_instruc) },
60 { "ld", VCPU_STAT(ld) },
61 { "ld_slow", VCPU_STAT(ld_slow) },
62 { "st", VCPU_STAT(st) },
63 { "st_slow", VCPU_STAT(st_slow) },
64 { NULL }
65 };
66
kvmppc_core_load_host_debugstate(struct kvm_vcpu * vcpu)67 void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
68 {
69 }
70
kvmppc_core_load_guest_debugstate(struct kvm_vcpu * vcpu)71 void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu)
72 {
73 }
74
kvmppc_unfixup_split_real(struct kvm_vcpu * vcpu)75 void kvmppc_unfixup_split_real(struct kvm_vcpu *vcpu)
76 {
77 if (vcpu->arch.hflags & BOOK3S_HFLAG_SPLIT_HACK) {
78 ulong pc = kvmppc_get_pc(vcpu);
79 if ((pc & SPLIT_HACK_MASK) == SPLIT_HACK_OFFS)
80 kvmppc_set_pc(vcpu, pc & ~SPLIT_HACK_MASK);
81 vcpu->arch.hflags &= ~BOOK3S_HFLAG_SPLIT_HACK;
82 }
83 }
84 EXPORT_SYMBOL_GPL(kvmppc_unfixup_split_real);
85
kvmppc_interrupt_offset(struct kvm_vcpu * vcpu)86 static inline unsigned long kvmppc_interrupt_offset(struct kvm_vcpu *vcpu)
87 {
88 if (!is_kvmppc_hv_enabled(vcpu->kvm))
89 return to_book3s(vcpu)->hior;
90 return 0;
91 }
92
kvmppc_update_int_pending(struct kvm_vcpu * vcpu,unsigned long pending_now,unsigned long old_pending)93 static inline void kvmppc_update_int_pending(struct kvm_vcpu *vcpu,
94 unsigned long pending_now, unsigned long old_pending)
95 {
96 if (is_kvmppc_hv_enabled(vcpu->kvm))
97 return;
98 if (pending_now)
99 kvmppc_set_int_pending(vcpu, 1);
100 else if (old_pending)
101 kvmppc_set_int_pending(vcpu, 0);
102 }
103
kvmppc_critical_section(struct kvm_vcpu * vcpu)104 static inline bool kvmppc_critical_section(struct kvm_vcpu *vcpu)
105 {
106 ulong crit_raw;
107 ulong crit_r1;
108 bool crit;
109
110 if (is_kvmppc_hv_enabled(vcpu->kvm))
111 return false;
112
113 crit_raw = kvmppc_get_critical(vcpu);
114 crit_r1 = kvmppc_get_gpr(vcpu, 1);
115
116 /* Truncate crit indicators in 32 bit mode */
117 if (!(kvmppc_get_msr(vcpu) & MSR_SF)) {
118 crit_raw &= 0xffffffff;
119 crit_r1 &= 0xffffffff;
120 }
121
122 /* Critical section when crit == r1 */
123 crit = (crit_raw == crit_r1);
124 /* ... and we're in supervisor mode */
125 crit = crit && !(kvmppc_get_msr(vcpu) & MSR_PR);
126
127 return crit;
128 }
129
kvmppc_inject_interrupt(struct kvm_vcpu * vcpu,int vec,u64 flags)130 void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags)
131 {
132 kvmppc_unfixup_split_real(vcpu);
133 kvmppc_set_srr0(vcpu, kvmppc_get_pc(vcpu));
134 kvmppc_set_srr1(vcpu, kvmppc_get_msr(vcpu) | flags);
135 kvmppc_set_pc(vcpu, kvmppc_interrupt_offset(vcpu) + vec);
136 vcpu->arch.mmu.reset_msr(vcpu);
137 }
138
kvmppc_book3s_vec2irqprio(unsigned int vec)139 static int kvmppc_book3s_vec2irqprio(unsigned int vec)
140 {
141 unsigned int prio;
142
143 switch (vec) {
144 case 0x100: prio = BOOK3S_IRQPRIO_SYSTEM_RESET; break;
145 case 0x200: prio = BOOK3S_IRQPRIO_MACHINE_CHECK; break;
146 case 0x300: prio = BOOK3S_IRQPRIO_DATA_STORAGE; break;
147 case 0x380: prio = BOOK3S_IRQPRIO_DATA_SEGMENT; break;
148 case 0x400: prio = BOOK3S_IRQPRIO_INST_STORAGE; break;
149 case 0x480: prio = BOOK3S_IRQPRIO_INST_SEGMENT; break;
150 case 0x500: prio = BOOK3S_IRQPRIO_EXTERNAL; break;
151 case 0x501: prio = BOOK3S_IRQPRIO_EXTERNAL_LEVEL; break;
152 case 0x600: prio = BOOK3S_IRQPRIO_ALIGNMENT; break;
153 case 0x700: prio = BOOK3S_IRQPRIO_PROGRAM; break;
154 case 0x800: prio = BOOK3S_IRQPRIO_FP_UNAVAIL; break;
155 case 0x900: prio = BOOK3S_IRQPRIO_DECREMENTER; break;
156 case 0xc00: prio = BOOK3S_IRQPRIO_SYSCALL; break;
157 case 0xd00: prio = BOOK3S_IRQPRIO_DEBUG; break;
158 case 0xf20: prio = BOOK3S_IRQPRIO_ALTIVEC; break;
159 case 0xf40: prio = BOOK3S_IRQPRIO_VSX; break;
160 case 0xf60: prio = BOOK3S_IRQPRIO_FAC_UNAVAIL; break;
161 default: prio = BOOK3S_IRQPRIO_MAX; break;
162 }
163
164 return prio;
165 }
166
kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu * vcpu,unsigned int vec)167 void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu,
168 unsigned int vec)
169 {
170 unsigned long old_pending = vcpu->arch.pending_exceptions;
171
172 clear_bit(kvmppc_book3s_vec2irqprio(vec),
173 &vcpu->arch.pending_exceptions);
174
175 kvmppc_update_int_pending(vcpu, vcpu->arch.pending_exceptions,
176 old_pending);
177 }
178
kvmppc_book3s_queue_irqprio(struct kvm_vcpu * vcpu,unsigned int vec)179 void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec)
180 {
181 vcpu->stat.queue_intr++;
182
183 set_bit(kvmppc_book3s_vec2irqprio(vec),
184 &vcpu->arch.pending_exceptions);
185 #ifdef EXIT_DEBUG
186 printk(KERN_INFO "Queueing interrupt %x\n", vec);
187 #endif
188 }
189 EXPORT_SYMBOL_GPL(kvmppc_book3s_queue_irqprio);
190
kvmppc_core_queue_program(struct kvm_vcpu * vcpu,ulong flags)191 void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags)
192 {
193 /* might as well deliver this straight away */
194 kvmppc_inject_interrupt(vcpu, BOOK3S_INTERRUPT_PROGRAM, flags);
195 }
196 EXPORT_SYMBOL_GPL(kvmppc_core_queue_program);
197
kvmppc_core_queue_dec(struct kvm_vcpu * vcpu)198 void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
199 {
200 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
201 }
202 EXPORT_SYMBOL_GPL(kvmppc_core_queue_dec);
203
kvmppc_core_pending_dec(struct kvm_vcpu * vcpu)204 int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
205 {
206 return test_bit(BOOK3S_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
207 }
208 EXPORT_SYMBOL_GPL(kvmppc_core_pending_dec);
209
kvmppc_core_dequeue_dec(struct kvm_vcpu * vcpu)210 void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
211 {
212 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
213 }
214 EXPORT_SYMBOL_GPL(kvmppc_core_dequeue_dec);
215
kvmppc_core_queue_external(struct kvm_vcpu * vcpu,struct kvm_interrupt * irq)216 void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
217 struct kvm_interrupt *irq)
218 {
219 unsigned int vec = BOOK3S_INTERRUPT_EXTERNAL;
220
221 if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
222 vec = BOOK3S_INTERRUPT_EXTERNAL_LEVEL;
223
224 kvmppc_book3s_queue_irqprio(vcpu, vec);
225 }
226
kvmppc_core_dequeue_external(struct kvm_vcpu * vcpu)227 void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu)
228 {
229 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
230 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
231 }
232
kvmppc_core_queue_data_storage(struct kvm_vcpu * vcpu,ulong dar,ulong flags)233 void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu, ulong dar,
234 ulong flags)
235 {
236 kvmppc_set_dar(vcpu, dar);
237 kvmppc_set_dsisr(vcpu, flags);
238 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_DATA_STORAGE);
239 }
240
kvmppc_core_queue_inst_storage(struct kvm_vcpu * vcpu,ulong flags)241 void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu, ulong flags)
242 {
243 u64 msr = kvmppc_get_msr(vcpu);
244 msr &= ~(SRR1_ISI_NOPT | SRR1_ISI_N_OR_G | SRR1_ISI_PROT);
245 msr |= flags & (SRR1_ISI_NOPT | SRR1_ISI_N_OR_G | SRR1_ISI_PROT);
246 kvmppc_set_msr_fast(vcpu, msr);
247 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
248 }
249
kvmppc_book3s_irqprio_deliver(struct kvm_vcpu * vcpu,unsigned int priority)250 int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu *vcpu, unsigned int priority)
251 {
252 int deliver = 1;
253 int vec = 0;
254 bool crit = kvmppc_critical_section(vcpu);
255
256 switch (priority) {
257 case BOOK3S_IRQPRIO_DECREMENTER:
258 deliver = (kvmppc_get_msr(vcpu) & MSR_EE) && !crit;
259 vec = BOOK3S_INTERRUPT_DECREMENTER;
260 break;
261 case BOOK3S_IRQPRIO_EXTERNAL:
262 case BOOK3S_IRQPRIO_EXTERNAL_LEVEL:
263 deliver = (kvmppc_get_msr(vcpu) & MSR_EE) && !crit;
264 vec = BOOK3S_INTERRUPT_EXTERNAL;
265 break;
266 case BOOK3S_IRQPRIO_SYSTEM_RESET:
267 vec = BOOK3S_INTERRUPT_SYSTEM_RESET;
268 break;
269 case BOOK3S_IRQPRIO_MACHINE_CHECK:
270 vec = BOOK3S_INTERRUPT_MACHINE_CHECK;
271 break;
272 case BOOK3S_IRQPRIO_DATA_STORAGE:
273 vec = BOOK3S_INTERRUPT_DATA_STORAGE;
274 break;
275 case BOOK3S_IRQPRIO_INST_STORAGE:
276 vec = BOOK3S_INTERRUPT_INST_STORAGE;
277 break;
278 case BOOK3S_IRQPRIO_DATA_SEGMENT:
279 vec = BOOK3S_INTERRUPT_DATA_SEGMENT;
280 break;
281 case BOOK3S_IRQPRIO_INST_SEGMENT:
282 vec = BOOK3S_INTERRUPT_INST_SEGMENT;
283 break;
284 case BOOK3S_IRQPRIO_ALIGNMENT:
285 vec = BOOK3S_INTERRUPT_ALIGNMENT;
286 break;
287 case BOOK3S_IRQPRIO_PROGRAM:
288 vec = BOOK3S_INTERRUPT_PROGRAM;
289 break;
290 case BOOK3S_IRQPRIO_VSX:
291 vec = BOOK3S_INTERRUPT_VSX;
292 break;
293 case BOOK3S_IRQPRIO_ALTIVEC:
294 vec = BOOK3S_INTERRUPT_ALTIVEC;
295 break;
296 case BOOK3S_IRQPRIO_FP_UNAVAIL:
297 vec = BOOK3S_INTERRUPT_FP_UNAVAIL;
298 break;
299 case BOOK3S_IRQPRIO_SYSCALL:
300 vec = BOOK3S_INTERRUPT_SYSCALL;
301 break;
302 case BOOK3S_IRQPRIO_DEBUG:
303 vec = BOOK3S_INTERRUPT_TRACE;
304 break;
305 case BOOK3S_IRQPRIO_PERFORMANCE_MONITOR:
306 vec = BOOK3S_INTERRUPT_PERFMON;
307 break;
308 case BOOK3S_IRQPRIO_FAC_UNAVAIL:
309 vec = BOOK3S_INTERRUPT_FAC_UNAVAIL;
310 break;
311 default:
312 deliver = 0;
313 printk(KERN_ERR "KVM: Unknown interrupt: 0x%x\n", priority);
314 break;
315 }
316
317 #if 0
318 printk(KERN_INFO "Deliver interrupt 0x%x? %x\n", vec, deliver);
319 #endif
320
321 if (deliver)
322 kvmppc_inject_interrupt(vcpu, vec, 0);
323
324 return deliver;
325 }
326
327 /*
328 * This function determines if an irqprio should be cleared once issued.
329 */
clear_irqprio(struct kvm_vcpu * vcpu,unsigned int priority)330 static bool clear_irqprio(struct kvm_vcpu *vcpu, unsigned int priority)
331 {
332 switch (priority) {
333 case BOOK3S_IRQPRIO_DECREMENTER:
334 /* DEC interrupts get cleared by mtdec */
335 return false;
336 case BOOK3S_IRQPRIO_EXTERNAL_LEVEL:
337 /* External interrupts get cleared by userspace */
338 return false;
339 }
340
341 return true;
342 }
343
kvmppc_core_prepare_to_enter(struct kvm_vcpu * vcpu)344 int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
345 {
346 unsigned long *pending = &vcpu->arch.pending_exceptions;
347 unsigned long old_pending = vcpu->arch.pending_exceptions;
348 unsigned int priority;
349
350 #ifdef EXIT_DEBUG
351 if (vcpu->arch.pending_exceptions)
352 printk(KERN_EMERG "KVM: Check pending: %lx\n", vcpu->arch.pending_exceptions);
353 #endif
354 priority = __ffs(*pending);
355 while (priority < BOOK3S_IRQPRIO_MAX) {
356 if (kvmppc_book3s_irqprio_deliver(vcpu, priority) &&
357 clear_irqprio(vcpu, priority)) {
358 clear_bit(priority, &vcpu->arch.pending_exceptions);
359 break;
360 }
361
362 priority = find_next_bit(pending,
363 BITS_PER_BYTE * sizeof(*pending),
364 priority + 1);
365 }
366
367 /* Tell the guest about our interrupt status */
368 kvmppc_update_int_pending(vcpu, *pending, old_pending);
369
370 return 0;
371 }
372 EXPORT_SYMBOL_GPL(kvmppc_core_prepare_to_enter);
373
kvmppc_gpa_to_pfn(struct kvm_vcpu * vcpu,gpa_t gpa,bool writing,bool * writable)374 pfn_t kvmppc_gpa_to_pfn(struct kvm_vcpu *vcpu, gpa_t gpa, bool writing,
375 bool *writable)
376 {
377 ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM;
378 gfn_t gfn = gpa >> PAGE_SHIFT;
379
380 if (!(kvmppc_get_msr(vcpu) & MSR_SF))
381 mp_pa = (uint32_t)mp_pa;
382
383 /* Magic page override */
384 gpa &= ~0xFFFULL;
385 if (unlikely(mp_pa) && unlikely((gpa & KVM_PAM) == mp_pa)) {
386 ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK;
387 pfn_t pfn;
388
389 pfn = (pfn_t)virt_to_phys((void*)shared_page) >> PAGE_SHIFT;
390 get_page(pfn_to_page(pfn));
391 if (writable)
392 *writable = true;
393 return pfn;
394 }
395
396 return gfn_to_pfn_prot(vcpu->kvm, gfn, writing, writable);
397 }
398 EXPORT_SYMBOL_GPL(kvmppc_gpa_to_pfn);
399
kvmppc_xlate(struct kvm_vcpu * vcpu,ulong eaddr,enum xlate_instdata xlid,enum xlate_readwrite xlrw,struct kvmppc_pte * pte)400 int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, enum xlate_instdata xlid,
401 enum xlate_readwrite xlrw, struct kvmppc_pte *pte)
402 {
403 bool data = (xlid == XLATE_DATA);
404 bool iswrite = (xlrw == XLATE_WRITE);
405 int relocated = (kvmppc_get_msr(vcpu) & (data ? MSR_DR : MSR_IR));
406 int r;
407
408 if (relocated) {
409 r = vcpu->arch.mmu.xlate(vcpu, eaddr, pte, data, iswrite);
410 } else {
411 pte->eaddr = eaddr;
412 pte->raddr = eaddr & KVM_PAM;
413 pte->vpage = VSID_REAL | eaddr >> 12;
414 pte->may_read = true;
415 pte->may_write = true;
416 pte->may_execute = true;
417 r = 0;
418
419 if ((kvmppc_get_msr(vcpu) & (MSR_IR | MSR_DR)) == MSR_DR &&
420 !data) {
421 if ((vcpu->arch.hflags & BOOK3S_HFLAG_SPLIT_HACK) &&
422 ((eaddr & SPLIT_HACK_MASK) == SPLIT_HACK_OFFS))
423 pte->raddr &= ~SPLIT_HACK_MASK;
424 }
425 }
426
427 return r;
428 }
429
kvmppc_load_last_inst(struct kvm_vcpu * vcpu,enum instruction_type type,u32 * inst)430 int kvmppc_load_last_inst(struct kvm_vcpu *vcpu, enum instruction_type type,
431 u32 *inst)
432 {
433 ulong pc = kvmppc_get_pc(vcpu);
434 int r;
435
436 if (type == INST_SC)
437 pc -= 4;
438
439 r = kvmppc_ld(vcpu, &pc, sizeof(u32), inst, false);
440 if (r == EMULATE_DONE)
441 return r;
442 else
443 return EMULATE_AGAIN;
444 }
445 EXPORT_SYMBOL_GPL(kvmppc_load_last_inst);
446
kvm_arch_vcpu_setup(struct kvm_vcpu * vcpu)447 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
448 {
449 return 0;
450 }
451
kvmppc_subarch_vcpu_init(struct kvm_vcpu * vcpu)452 int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu)
453 {
454 return 0;
455 }
456
kvmppc_subarch_vcpu_uninit(struct kvm_vcpu * vcpu)457 void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu)
458 {
459 }
460
kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu * vcpu,struct kvm_sregs * sregs)461 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
462 struct kvm_sregs *sregs)
463 {
464 return vcpu->kvm->arch.kvm_ops->get_sregs(vcpu, sregs);
465 }
466
kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu * vcpu,struct kvm_sregs * sregs)467 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
468 struct kvm_sregs *sregs)
469 {
470 return vcpu->kvm->arch.kvm_ops->set_sregs(vcpu, sregs);
471 }
472
kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu * vcpu,struct kvm_regs * regs)473 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
474 {
475 int i;
476
477 regs->pc = kvmppc_get_pc(vcpu);
478 regs->cr = kvmppc_get_cr(vcpu);
479 regs->ctr = kvmppc_get_ctr(vcpu);
480 regs->lr = kvmppc_get_lr(vcpu);
481 regs->xer = kvmppc_get_xer(vcpu);
482 regs->msr = kvmppc_get_msr(vcpu);
483 regs->srr0 = kvmppc_get_srr0(vcpu);
484 regs->srr1 = kvmppc_get_srr1(vcpu);
485 regs->pid = vcpu->arch.pid;
486 regs->sprg0 = kvmppc_get_sprg0(vcpu);
487 regs->sprg1 = kvmppc_get_sprg1(vcpu);
488 regs->sprg2 = kvmppc_get_sprg2(vcpu);
489 regs->sprg3 = kvmppc_get_sprg3(vcpu);
490 regs->sprg4 = kvmppc_get_sprg4(vcpu);
491 regs->sprg5 = kvmppc_get_sprg5(vcpu);
492 regs->sprg6 = kvmppc_get_sprg6(vcpu);
493 regs->sprg7 = kvmppc_get_sprg7(vcpu);
494
495 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
496 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
497
498 return 0;
499 }
500
kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu * vcpu,struct kvm_regs * regs)501 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
502 {
503 int i;
504
505 kvmppc_set_pc(vcpu, regs->pc);
506 kvmppc_set_cr(vcpu, regs->cr);
507 kvmppc_set_ctr(vcpu, regs->ctr);
508 kvmppc_set_lr(vcpu, regs->lr);
509 kvmppc_set_xer(vcpu, regs->xer);
510 kvmppc_set_msr(vcpu, regs->msr);
511 kvmppc_set_srr0(vcpu, regs->srr0);
512 kvmppc_set_srr1(vcpu, regs->srr1);
513 kvmppc_set_sprg0(vcpu, regs->sprg0);
514 kvmppc_set_sprg1(vcpu, regs->sprg1);
515 kvmppc_set_sprg2(vcpu, regs->sprg2);
516 kvmppc_set_sprg3(vcpu, regs->sprg3);
517 kvmppc_set_sprg4(vcpu, regs->sprg4);
518 kvmppc_set_sprg5(vcpu, regs->sprg5);
519 kvmppc_set_sprg6(vcpu, regs->sprg6);
520 kvmppc_set_sprg7(vcpu, regs->sprg7);
521
522 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
523 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
524
525 return 0;
526 }
527
kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu * vcpu,struct kvm_fpu * fpu)528 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
529 {
530 return -ENOTSUPP;
531 }
532
kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu * vcpu,struct kvm_fpu * fpu)533 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
534 {
535 return -ENOTSUPP;
536 }
537
kvmppc_get_one_reg(struct kvm_vcpu * vcpu,u64 id,union kvmppc_one_reg * val)538 int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id,
539 union kvmppc_one_reg *val)
540 {
541 int r = 0;
542 long int i;
543
544 r = vcpu->kvm->arch.kvm_ops->get_one_reg(vcpu, id, val);
545 if (r == -EINVAL) {
546 r = 0;
547 switch (id) {
548 case KVM_REG_PPC_DAR:
549 *val = get_reg_val(id, kvmppc_get_dar(vcpu));
550 break;
551 case KVM_REG_PPC_DSISR:
552 *val = get_reg_val(id, kvmppc_get_dsisr(vcpu));
553 break;
554 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
555 i = id - KVM_REG_PPC_FPR0;
556 *val = get_reg_val(id, VCPU_FPR(vcpu, i));
557 break;
558 case KVM_REG_PPC_FPSCR:
559 *val = get_reg_val(id, vcpu->arch.fp.fpscr);
560 break;
561 #ifdef CONFIG_VSX
562 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
563 if (cpu_has_feature(CPU_FTR_VSX)) {
564 i = id - KVM_REG_PPC_VSR0;
565 val->vsxval[0] = vcpu->arch.fp.fpr[i][0];
566 val->vsxval[1] = vcpu->arch.fp.fpr[i][1];
567 } else {
568 r = -ENXIO;
569 }
570 break;
571 #endif /* CONFIG_VSX */
572 case KVM_REG_PPC_DEBUG_INST:
573 *val = get_reg_val(id, INS_TW);
574 break;
575 #ifdef CONFIG_KVM_XICS
576 case KVM_REG_PPC_ICP_STATE:
577 if (!vcpu->arch.icp) {
578 r = -ENXIO;
579 break;
580 }
581 *val = get_reg_val(id, kvmppc_xics_get_icp(vcpu));
582 break;
583 #endif /* CONFIG_KVM_XICS */
584 case KVM_REG_PPC_FSCR:
585 *val = get_reg_val(id, vcpu->arch.fscr);
586 break;
587 case KVM_REG_PPC_TAR:
588 *val = get_reg_val(id, vcpu->arch.tar);
589 break;
590 case KVM_REG_PPC_EBBHR:
591 *val = get_reg_val(id, vcpu->arch.ebbhr);
592 break;
593 case KVM_REG_PPC_EBBRR:
594 *val = get_reg_val(id, vcpu->arch.ebbrr);
595 break;
596 case KVM_REG_PPC_BESCR:
597 *val = get_reg_val(id, vcpu->arch.bescr);
598 break;
599 case KVM_REG_PPC_VTB:
600 *val = get_reg_val(id, vcpu->arch.vtb);
601 break;
602 case KVM_REG_PPC_IC:
603 *val = get_reg_val(id, vcpu->arch.ic);
604 break;
605 default:
606 r = -EINVAL;
607 break;
608 }
609 }
610
611 return r;
612 }
613
kvmppc_set_one_reg(struct kvm_vcpu * vcpu,u64 id,union kvmppc_one_reg * val)614 int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id,
615 union kvmppc_one_reg *val)
616 {
617 int r = 0;
618 long int i;
619
620 r = vcpu->kvm->arch.kvm_ops->set_one_reg(vcpu, id, val);
621 if (r == -EINVAL) {
622 r = 0;
623 switch (id) {
624 case KVM_REG_PPC_DAR:
625 kvmppc_set_dar(vcpu, set_reg_val(id, *val));
626 break;
627 case KVM_REG_PPC_DSISR:
628 kvmppc_set_dsisr(vcpu, set_reg_val(id, *val));
629 break;
630 case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
631 i = id - KVM_REG_PPC_FPR0;
632 VCPU_FPR(vcpu, i) = set_reg_val(id, *val);
633 break;
634 case KVM_REG_PPC_FPSCR:
635 vcpu->arch.fp.fpscr = set_reg_val(id, *val);
636 break;
637 #ifdef CONFIG_VSX
638 case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
639 if (cpu_has_feature(CPU_FTR_VSX)) {
640 i = id - KVM_REG_PPC_VSR0;
641 vcpu->arch.fp.fpr[i][0] = val->vsxval[0];
642 vcpu->arch.fp.fpr[i][1] = val->vsxval[1];
643 } else {
644 r = -ENXIO;
645 }
646 break;
647 #endif /* CONFIG_VSX */
648 #ifdef CONFIG_KVM_XICS
649 case KVM_REG_PPC_ICP_STATE:
650 if (!vcpu->arch.icp) {
651 r = -ENXIO;
652 break;
653 }
654 r = kvmppc_xics_set_icp(vcpu,
655 set_reg_val(id, *val));
656 break;
657 #endif /* CONFIG_KVM_XICS */
658 case KVM_REG_PPC_FSCR:
659 vcpu->arch.fscr = set_reg_val(id, *val);
660 break;
661 case KVM_REG_PPC_TAR:
662 vcpu->arch.tar = set_reg_val(id, *val);
663 break;
664 case KVM_REG_PPC_EBBHR:
665 vcpu->arch.ebbhr = set_reg_val(id, *val);
666 break;
667 case KVM_REG_PPC_EBBRR:
668 vcpu->arch.ebbrr = set_reg_val(id, *val);
669 break;
670 case KVM_REG_PPC_BESCR:
671 vcpu->arch.bescr = set_reg_val(id, *val);
672 break;
673 case KVM_REG_PPC_VTB:
674 vcpu->arch.vtb = set_reg_val(id, *val);
675 break;
676 case KVM_REG_PPC_IC:
677 vcpu->arch.ic = set_reg_val(id, *val);
678 break;
679 default:
680 r = -EINVAL;
681 break;
682 }
683 }
684
685 return r;
686 }
687
kvmppc_core_vcpu_load(struct kvm_vcpu * vcpu,int cpu)688 void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
689 {
690 vcpu->kvm->arch.kvm_ops->vcpu_load(vcpu, cpu);
691 }
692
kvmppc_core_vcpu_put(struct kvm_vcpu * vcpu)693 void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
694 {
695 vcpu->kvm->arch.kvm_ops->vcpu_put(vcpu);
696 }
697
kvmppc_set_msr(struct kvm_vcpu * vcpu,u64 msr)698 void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
699 {
700 vcpu->kvm->arch.kvm_ops->set_msr(vcpu, msr);
701 }
702 EXPORT_SYMBOL_GPL(kvmppc_set_msr);
703
kvmppc_vcpu_run(struct kvm_run * kvm_run,struct kvm_vcpu * vcpu)704 int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
705 {
706 return vcpu->kvm->arch.kvm_ops->vcpu_run(kvm_run, vcpu);
707 }
708
kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu * vcpu,struct kvm_translation * tr)709 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
710 struct kvm_translation *tr)
711 {
712 return 0;
713 }
714
kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu * vcpu,struct kvm_guest_debug * dbg)715 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
716 struct kvm_guest_debug *dbg)
717 {
718 vcpu->guest_debug = dbg->control;
719 return 0;
720 }
721
kvmppc_decrementer_func(struct kvm_vcpu * vcpu)722 void kvmppc_decrementer_func(struct kvm_vcpu *vcpu)
723 {
724 kvmppc_core_queue_dec(vcpu);
725 kvm_vcpu_kick(vcpu);
726 }
727
kvmppc_core_vcpu_create(struct kvm * kvm,unsigned int id)728 struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
729 {
730 return kvm->arch.kvm_ops->vcpu_create(kvm, id);
731 }
732
kvmppc_core_vcpu_free(struct kvm_vcpu * vcpu)733 void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
734 {
735 vcpu->kvm->arch.kvm_ops->vcpu_free(vcpu);
736 }
737
kvmppc_core_check_requests(struct kvm_vcpu * vcpu)738 int kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
739 {
740 return vcpu->kvm->arch.kvm_ops->check_requests(vcpu);
741 }
742
kvm_vm_ioctl_get_dirty_log(struct kvm * kvm,struct kvm_dirty_log * log)743 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
744 {
745 return kvm->arch.kvm_ops->get_dirty_log(kvm, log);
746 }
747
kvmppc_core_free_memslot(struct kvm * kvm,struct kvm_memory_slot * free,struct kvm_memory_slot * dont)748 void kvmppc_core_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
749 struct kvm_memory_slot *dont)
750 {
751 kvm->arch.kvm_ops->free_memslot(free, dont);
752 }
753
kvmppc_core_create_memslot(struct kvm * kvm,struct kvm_memory_slot * slot,unsigned long npages)754 int kvmppc_core_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
755 unsigned long npages)
756 {
757 return kvm->arch.kvm_ops->create_memslot(slot, npages);
758 }
759
kvmppc_core_flush_memslot(struct kvm * kvm,struct kvm_memory_slot * memslot)760 void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
761 {
762 kvm->arch.kvm_ops->flush_memslot(kvm, memslot);
763 }
764
kvmppc_core_prepare_memory_region(struct kvm * kvm,struct kvm_memory_slot * memslot,struct kvm_userspace_memory_region * mem)765 int kvmppc_core_prepare_memory_region(struct kvm *kvm,
766 struct kvm_memory_slot *memslot,
767 struct kvm_userspace_memory_region *mem)
768 {
769 return kvm->arch.kvm_ops->prepare_memory_region(kvm, memslot, mem);
770 }
771
kvmppc_core_commit_memory_region(struct kvm * kvm,struct kvm_userspace_memory_region * mem,const struct kvm_memory_slot * old)772 void kvmppc_core_commit_memory_region(struct kvm *kvm,
773 struct kvm_userspace_memory_region *mem,
774 const struct kvm_memory_slot *old)
775 {
776 kvm->arch.kvm_ops->commit_memory_region(kvm, mem, old);
777 }
778
kvm_unmap_hva(struct kvm * kvm,unsigned long hva)779 int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
780 {
781 return kvm->arch.kvm_ops->unmap_hva(kvm, hva);
782 }
783 EXPORT_SYMBOL_GPL(kvm_unmap_hva);
784
kvm_unmap_hva_range(struct kvm * kvm,unsigned long start,unsigned long end)785 int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end)
786 {
787 return kvm->arch.kvm_ops->unmap_hva_range(kvm, start, end);
788 }
789
kvm_age_hva(struct kvm * kvm,unsigned long start,unsigned long end)790 int kvm_age_hva(struct kvm *kvm, unsigned long start, unsigned long end)
791 {
792 return kvm->arch.kvm_ops->age_hva(kvm, start, end);
793 }
794
kvm_test_age_hva(struct kvm * kvm,unsigned long hva)795 int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
796 {
797 return kvm->arch.kvm_ops->test_age_hva(kvm, hva);
798 }
799
kvm_set_spte_hva(struct kvm * kvm,unsigned long hva,pte_t pte)800 void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
801 {
802 kvm->arch.kvm_ops->set_spte_hva(kvm, hva, pte);
803 }
804
kvmppc_mmu_destroy(struct kvm_vcpu * vcpu)805 void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
806 {
807 vcpu->kvm->arch.kvm_ops->mmu_destroy(vcpu);
808 }
809
kvmppc_core_init_vm(struct kvm * kvm)810 int kvmppc_core_init_vm(struct kvm *kvm)
811 {
812
813 #ifdef CONFIG_PPC64
814 INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
815 INIT_LIST_HEAD(&kvm->arch.rtas_tokens);
816 #endif
817
818 return kvm->arch.kvm_ops->init_vm(kvm);
819 }
820
kvmppc_core_destroy_vm(struct kvm * kvm)821 void kvmppc_core_destroy_vm(struct kvm *kvm)
822 {
823 kvm->arch.kvm_ops->destroy_vm(kvm);
824
825 #ifdef CONFIG_PPC64
826 kvmppc_rtas_tokens_free(kvm);
827 WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
828 #endif
829 }
830
kvmppc_core_check_processor_compat(void)831 int kvmppc_core_check_processor_compat(void)
832 {
833 /*
834 * We always return 0 for book3s. We check
835 * for compatability while loading the HV
836 * or PR module
837 */
838 return 0;
839 }
840
kvmppc_book3s_hcall_implemented(struct kvm * kvm,unsigned long hcall)841 int kvmppc_book3s_hcall_implemented(struct kvm *kvm, unsigned long hcall)
842 {
843 return kvm->arch.kvm_ops->hcall_implemented(hcall);
844 }
845
kvmppc_book3s_init(void)846 static int kvmppc_book3s_init(void)
847 {
848 int r;
849
850 r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
851 if (r)
852 return r;
853 #ifdef CONFIG_KVM_BOOK3S_32_HANDLER
854 r = kvmppc_book3s_init_pr();
855 #endif
856 return r;
857
858 }
859
kvmppc_book3s_exit(void)860 static void kvmppc_book3s_exit(void)
861 {
862 #ifdef CONFIG_KVM_BOOK3S_32_HANDLER
863 kvmppc_book3s_exit_pr();
864 #endif
865 kvm_exit();
866 }
867
868 module_init(kvmppc_book3s_init);
869 module_exit(kvmppc_book3s_exit);
870
871 /* On 32bit this is our one and only kernel module */
872 #ifdef CONFIG_KVM_BOOK3S_32_HANDLER
873 MODULE_ALIAS_MISCDEV(KVM_MINOR);
874 MODULE_ALIAS("devname:kvm");
875 #endif
876