staging: brcm80211: decreased indentation level of brcms_c_wme_setparams function
[zen-stable.git] / arch / powerpc / kvm / book3s.c
blobf68a34d160356bbaab228bc1b59509e0af1c6f4c
1 /*
2 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
4 * Authors:
5 * Alexander Graf <agraf@suse.de>
6 * Kevin Wolf <mail@kevin-wolf.de>
8 * Description:
9 * This file is derived from arch/powerpc/kvm/44x.c,
10 * by Hollis Blanchard <hollisb@us.ibm.com>.
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.
17 #include <linux/kvm_host.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
21 #include <asm/reg.h>
22 #include <asm/cputable.h>
23 #include <asm/cacheflush.h>
24 #include <asm/tlbflush.h>
25 #include <asm/uaccess.h>
26 #include <asm/io.h>
27 #include <asm/kvm_ppc.h>
28 #include <asm/kvm_book3s.h>
29 #include <asm/mmu_context.h>
30 #include <asm/page.h>
31 #include <linux/gfp.h>
32 #include <linux/sched.h>
33 #include <linux/vmalloc.h>
34 #include <linux/highmem.h>
36 #include "trace.h"
38 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
40 /* #define EXIT_DEBUG */
42 struct kvm_stats_debugfs_item debugfs_entries[] = {
43 { "exits", VCPU_STAT(sum_exits) },
44 { "mmio", VCPU_STAT(mmio_exits) },
45 { "sig", VCPU_STAT(signal_exits) },
46 { "sysc", VCPU_STAT(syscall_exits) },
47 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
48 { "dec", VCPU_STAT(dec_exits) },
49 { "ext_intr", VCPU_STAT(ext_intr_exits) },
50 { "queue_intr", VCPU_STAT(queue_intr) },
51 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
52 { "pf_storage", VCPU_STAT(pf_storage) },
53 { "sp_storage", VCPU_STAT(sp_storage) },
54 { "pf_instruc", VCPU_STAT(pf_instruc) },
55 { "sp_instruc", VCPU_STAT(sp_instruc) },
56 { "ld", VCPU_STAT(ld) },
57 { "ld_slow", VCPU_STAT(ld_slow) },
58 { "st", VCPU_STAT(st) },
59 { "st_slow", VCPU_STAT(st_slow) },
60 { NULL }
63 void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
67 void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu)
71 void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags)
73 vcpu->arch.shared->srr0 = kvmppc_get_pc(vcpu);
74 vcpu->arch.shared->srr1 = vcpu->arch.shared->msr | flags;
75 kvmppc_set_pc(vcpu, kvmppc_interrupt_offset(vcpu) + vec);
76 vcpu->arch.mmu.reset_msr(vcpu);
79 static int kvmppc_book3s_vec2irqprio(unsigned int vec)
81 unsigned int prio;
83 switch (vec) {
84 case 0x100: prio = BOOK3S_IRQPRIO_SYSTEM_RESET; break;
85 case 0x200: prio = BOOK3S_IRQPRIO_MACHINE_CHECK; break;
86 case 0x300: prio = BOOK3S_IRQPRIO_DATA_STORAGE; break;
87 case 0x380: prio = BOOK3S_IRQPRIO_DATA_SEGMENT; break;
88 case 0x400: prio = BOOK3S_IRQPRIO_INST_STORAGE; break;
89 case 0x480: prio = BOOK3S_IRQPRIO_INST_SEGMENT; break;
90 case 0x500: prio = BOOK3S_IRQPRIO_EXTERNAL; break;
91 case 0x501: prio = BOOK3S_IRQPRIO_EXTERNAL_LEVEL; break;
92 case 0x600: prio = BOOK3S_IRQPRIO_ALIGNMENT; break;
93 case 0x700: prio = BOOK3S_IRQPRIO_PROGRAM; break;
94 case 0x800: prio = BOOK3S_IRQPRIO_FP_UNAVAIL; break;
95 case 0x900: prio = BOOK3S_IRQPRIO_DECREMENTER; break;
96 case 0xc00: prio = BOOK3S_IRQPRIO_SYSCALL; break;
97 case 0xd00: prio = BOOK3S_IRQPRIO_DEBUG; break;
98 case 0xf20: prio = BOOK3S_IRQPRIO_ALTIVEC; break;
99 case 0xf40: prio = BOOK3S_IRQPRIO_VSX; break;
100 default: prio = BOOK3S_IRQPRIO_MAX; break;
103 return prio;
106 static void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu,
107 unsigned int vec)
109 unsigned long old_pending = vcpu->arch.pending_exceptions;
111 clear_bit(kvmppc_book3s_vec2irqprio(vec),
112 &vcpu->arch.pending_exceptions);
114 kvmppc_update_int_pending(vcpu, vcpu->arch.pending_exceptions,
115 old_pending);
118 void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec)
120 vcpu->stat.queue_intr++;
122 set_bit(kvmppc_book3s_vec2irqprio(vec),
123 &vcpu->arch.pending_exceptions);
124 #ifdef EXIT_DEBUG
125 printk(KERN_INFO "Queueing interrupt %x\n", vec);
126 #endif
130 void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags)
132 /* might as well deliver this straight away */
133 kvmppc_inject_interrupt(vcpu, BOOK3S_INTERRUPT_PROGRAM, flags);
136 void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
138 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
141 int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
143 return test_bit(BOOK3S_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
146 void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
148 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
151 void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
152 struct kvm_interrupt *irq)
154 unsigned int vec = BOOK3S_INTERRUPT_EXTERNAL;
156 if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
157 vec = BOOK3S_INTERRUPT_EXTERNAL_LEVEL;
159 kvmppc_book3s_queue_irqprio(vcpu, vec);
162 void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
163 struct kvm_interrupt *irq)
165 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
166 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
169 int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu *vcpu, unsigned int priority)
171 int deliver = 1;
172 int vec = 0;
173 bool crit = kvmppc_critical_section(vcpu);
175 switch (priority) {
176 case BOOK3S_IRQPRIO_DECREMENTER:
177 deliver = (vcpu->arch.shared->msr & MSR_EE) && !crit;
178 vec = BOOK3S_INTERRUPT_DECREMENTER;
179 break;
180 case BOOK3S_IRQPRIO_EXTERNAL:
181 case BOOK3S_IRQPRIO_EXTERNAL_LEVEL:
182 deliver = (vcpu->arch.shared->msr & MSR_EE) && !crit;
183 vec = BOOK3S_INTERRUPT_EXTERNAL;
184 break;
185 case BOOK3S_IRQPRIO_SYSTEM_RESET:
186 vec = BOOK3S_INTERRUPT_SYSTEM_RESET;
187 break;
188 case BOOK3S_IRQPRIO_MACHINE_CHECK:
189 vec = BOOK3S_INTERRUPT_MACHINE_CHECK;
190 break;
191 case BOOK3S_IRQPRIO_DATA_STORAGE:
192 vec = BOOK3S_INTERRUPT_DATA_STORAGE;
193 break;
194 case BOOK3S_IRQPRIO_INST_STORAGE:
195 vec = BOOK3S_INTERRUPT_INST_STORAGE;
196 break;
197 case BOOK3S_IRQPRIO_DATA_SEGMENT:
198 vec = BOOK3S_INTERRUPT_DATA_SEGMENT;
199 break;
200 case BOOK3S_IRQPRIO_INST_SEGMENT:
201 vec = BOOK3S_INTERRUPT_INST_SEGMENT;
202 break;
203 case BOOK3S_IRQPRIO_ALIGNMENT:
204 vec = BOOK3S_INTERRUPT_ALIGNMENT;
205 break;
206 case BOOK3S_IRQPRIO_PROGRAM:
207 vec = BOOK3S_INTERRUPT_PROGRAM;
208 break;
209 case BOOK3S_IRQPRIO_VSX:
210 vec = BOOK3S_INTERRUPT_VSX;
211 break;
212 case BOOK3S_IRQPRIO_ALTIVEC:
213 vec = BOOK3S_INTERRUPT_ALTIVEC;
214 break;
215 case BOOK3S_IRQPRIO_FP_UNAVAIL:
216 vec = BOOK3S_INTERRUPT_FP_UNAVAIL;
217 break;
218 case BOOK3S_IRQPRIO_SYSCALL:
219 vec = BOOK3S_INTERRUPT_SYSCALL;
220 break;
221 case BOOK3S_IRQPRIO_DEBUG:
222 vec = BOOK3S_INTERRUPT_TRACE;
223 break;
224 case BOOK3S_IRQPRIO_PERFORMANCE_MONITOR:
225 vec = BOOK3S_INTERRUPT_PERFMON;
226 break;
227 default:
228 deliver = 0;
229 printk(KERN_ERR "KVM: Unknown interrupt: 0x%x\n", priority);
230 break;
233 #if 0
234 printk(KERN_INFO "Deliver interrupt 0x%x? %x\n", vec, deliver);
235 #endif
237 if (deliver)
238 kvmppc_inject_interrupt(vcpu, vec, 0);
240 return deliver;
244 * This function determines if an irqprio should be cleared once issued.
246 static bool clear_irqprio(struct kvm_vcpu *vcpu, unsigned int priority)
248 switch (priority) {
249 case BOOK3S_IRQPRIO_DECREMENTER:
250 /* DEC interrupts get cleared by mtdec */
251 return false;
252 case BOOK3S_IRQPRIO_EXTERNAL_LEVEL:
253 /* External interrupts get cleared by userspace */
254 return false;
257 return true;
260 void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
262 unsigned long *pending = &vcpu->arch.pending_exceptions;
263 unsigned long old_pending = vcpu->arch.pending_exceptions;
264 unsigned int priority;
266 #ifdef EXIT_DEBUG
267 if (vcpu->arch.pending_exceptions)
268 printk(KERN_EMERG "KVM: Check pending: %lx\n", vcpu->arch.pending_exceptions);
269 #endif
270 priority = __ffs(*pending);
271 while (priority < BOOK3S_IRQPRIO_MAX) {
272 if (kvmppc_book3s_irqprio_deliver(vcpu, priority) &&
273 clear_irqprio(vcpu, priority)) {
274 clear_bit(priority, &vcpu->arch.pending_exceptions);
275 break;
278 priority = find_next_bit(pending,
279 BITS_PER_BYTE * sizeof(*pending),
280 priority + 1);
283 /* Tell the guest about our interrupt status */
284 kvmppc_update_int_pending(vcpu, *pending, old_pending);
287 pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn)
289 ulong mp_pa = vcpu->arch.magic_page_pa;
291 /* Magic page override */
292 if (unlikely(mp_pa) &&
293 unlikely(((gfn << PAGE_SHIFT) & KVM_PAM) ==
294 ((mp_pa & PAGE_MASK) & KVM_PAM))) {
295 ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK;
296 pfn_t pfn;
298 pfn = (pfn_t)virt_to_phys((void*)shared_page) >> PAGE_SHIFT;
299 get_page(pfn_to_page(pfn));
300 return pfn;
303 return gfn_to_pfn(vcpu->kvm, gfn);
306 static int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, bool data,
307 struct kvmppc_pte *pte)
309 int relocated = (vcpu->arch.shared->msr & (data ? MSR_DR : MSR_IR));
310 int r;
312 if (relocated) {
313 r = vcpu->arch.mmu.xlate(vcpu, eaddr, pte, data);
314 } else {
315 pte->eaddr = eaddr;
316 pte->raddr = eaddr & KVM_PAM;
317 pte->vpage = VSID_REAL | eaddr >> 12;
318 pte->may_read = true;
319 pte->may_write = true;
320 pte->may_execute = true;
321 r = 0;
324 return r;
327 static hva_t kvmppc_bad_hva(void)
329 return PAGE_OFFSET;
332 static hva_t kvmppc_pte_to_hva(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte,
333 bool read)
335 hva_t hpage;
337 if (read && !pte->may_read)
338 goto err;
340 if (!read && !pte->may_write)
341 goto err;
343 hpage = gfn_to_hva(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
344 if (kvm_is_error_hva(hpage))
345 goto err;
347 return hpage | (pte->raddr & ~PAGE_MASK);
348 err:
349 return kvmppc_bad_hva();
352 int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
353 bool data)
355 struct kvmppc_pte pte;
357 vcpu->stat.st++;
359 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
360 return -ENOENT;
362 *eaddr = pte.raddr;
364 if (!pte.may_write)
365 return -EPERM;
367 if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
368 return EMULATE_DO_MMIO;
370 return EMULATE_DONE;
373 int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
374 bool data)
376 struct kvmppc_pte pte;
377 hva_t hva = *eaddr;
379 vcpu->stat.ld++;
381 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
382 goto nopte;
384 *eaddr = pte.raddr;
386 hva = kvmppc_pte_to_hva(vcpu, &pte, true);
387 if (kvm_is_error_hva(hva))
388 goto mmio;
390 if (copy_from_user(ptr, (void __user *)hva, size)) {
391 printk(KERN_INFO "kvmppc_ld at 0x%lx failed\n", hva);
392 goto mmio;
395 return EMULATE_DONE;
397 nopte:
398 return -ENOENT;
399 mmio:
400 return EMULATE_DO_MMIO;
403 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
405 return 0;
408 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
410 int i;
412 regs->pc = kvmppc_get_pc(vcpu);
413 regs->cr = kvmppc_get_cr(vcpu);
414 regs->ctr = kvmppc_get_ctr(vcpu);
415 regs->lr = kvmppc_get_lr(vcpu);
416 regs->xer = kvmppc_get_xer(vcpu);
417 regs->msr = vcpu->arch.shared->msr;
418 regs->srr0 = vcpu->arch.shared->srr0;
419 regs->srr1 = vcpu->arch.shared->srr1;
420 regs->pid = vcpu->arch.pid;
421 regs->sprg0 = vcpu->arch.shared->sprg0;
422 regs->sprg1 = vcpu->arch.shared->sprg1;
423 regs->sprg2 = vcpu->arch.shared->sprg2;
424 regs->sprg3 = vcpu->arch.shared->sprg3;
425 regs->sprg4 = vcpu->arch.sprg4;
426 regs->sprg5 = vcpu->arch.sprg5;
427 regs->sprg6 = vcpu->arch.sprg6;
428 regs->sprg7 = vcpu->arch.sprg7;
430 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
431 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
433 return 0;
436 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
438 int i;
440 kvmppc_set_pc(vcpu, regs->pc);
441 kvmppc_set_cr(vcpu, regs->cr);
442 kvmppc_set_ctr(vcpu, regs->ctr);
443 kvmppc_set_lr(vcpu, regs->lr);
444 kvmppc_set_xer(vcpu, regs->xer);
445 kvmppc_set_msr(vcpu, regs->msr);
446 vcpu->arch.shared->srr0 = regs->srr0;
447 vcpu->arch.shared->srr1 = regs->srr1;
448 vcpu->arch.shared->sprg0 = regs->sprg0;
449 vcpu->arch.shared->sprg1 = regs->sprg1;
450 vcpu->arch.shared->sprg2 = regs->sprg2;
451 vcpu->arch.shared->sprg3 = regs->sprg3;
452 vcpu->arch.sprg4 = regs->sprg4;
453 vcpu->arch.sprg5 = regs->sprg5;
454 vcpu->arch.sprg6 = regs->sprg6;
455 vcpu->arch.sprg7 = regs->sprg7;
457 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
458 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
460 return 0;
463 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
465 return -ENOTSUPP;
468 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
470 return -ENOTSUPP;
473 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
474 struct kvm_translation *tr)
476 return 0;
480 * Get (and clear) the dirty memory log for a memory slot.
482 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
483 struct kvm_dirty_log *log)
485 struct kvm_memory_slot *memslot;
486 struct kvm_vcpu *vcpu;
487 ulong ga, ga_end;
488 int is_dirty = 0;
489 int r;
490 unsigned long n;
492 mutex_lock(&kvm->slots_lock);
494 r = kvm_get_dirty_log(kvm, log, &is_dirty);
495 if (r)
496 goto out;
498 /* If nothing is dirty, don't bother messing with page tables. */
499 if (is_dirty) {
500 memslot = &kvm->memslots->memslots[log->slot];
502 ga = memslot->base_gfn << PAGE_SHIFT;
503 ga_end = ga + (memslot->npages << PAGE_SHIFT);
505 kvm_for_each_vcpu(n, vcpu, kvm)
506 kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
508 n = kvm_dirty_bitmap_bytes(memslot);
509 memset(memslot->dirty_bitmap, 0, n);
512 r = 0;
513 out:
514 mutex_unlock(&kvm->slots_lock);
515 return r;