xref: /openbmc/linux/arch/powerpc/kvm/book3s_hv.c (revision f6723b56)
1 /*
2  * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
3  * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
4  *
5  * Authors:
6  *    Paul Mackerras <paulus@au1.ibm.com>
7  *    Alexander Graf <agraf@suse.de>
8  *    Kevin Wolf <mail@kevin-wolf.de>
9  *
10  * Description: KVM functions specific to running on Book 3S
11  * processors in hypervisor mode (specifically POWER7 and later).
12  *
13  * This file is derived from arch/powerpc/kvm/book3s.c,
14  * by Alexander Graf <agraf@suse.de>.
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License, version 2, as
18  * published by the Free Software Foundation.
19  */
20 
21 #include <linux/kvm_host.h>
22 #include <linux/err.h>
23 #include <linux/slab.h>
24 #include <linux/preempt.h>
25 #include <linux/sched.h>
26 #include <linux/delay.h>
27 #include <linux/export.h>
28 #include <linux/fs.h>
29 #include <linux/anon_inodes.h>
30 #include <linux/cpumask.h>
31 #include <linux/spinlock.h>
32 #include <linux/page-flags.h>
33 #include <linux/srcu.h>
34 #include <linux/miscdevice.h>
35 
36 #include <asm/reg.h>
37 #include <asm/cputable.h>
38 #include <asm/cacheflush.h>
39 #include <asm/tlbflush.h>
40 #include <asm/uaccess.h>
41 #include <asm/io.h>
42 #include <asm/kvm_ppc.h>
43 #include <asm/kvm_book3s.h>
44 #include <asm/mmu_context.h>
45 #include <asm/lppaca.h>
46 #include <asm/processor.h>
47 #include <asm/cputhreads.h>
48 #include <asm/page.h>
49 #include <asm/hvcall.h>
50 #include <asm/switch_to.h>
51 #include <asm/smp.h>
52 #include <linux/gfp.h>
53 #include <linux/vmalloc.h>
54 #include <linux/highmem.h>
55 #include <linux/hugetlb.h>
56 #include <linux/module.h>
57 
58 #include "book3s.h"
59 
60 /* #define EXIT_DEBUG */
61 /* #define EXIT_DEBUG_SIMPLE */
62 /* #define EXIT_DEBUG_INT */
63 
64 /* Used to indicate that a guest page fault needs to be handled */
65 #define RESUME_PAGE_FAULT	(RESUME_GUEST | RESUME_FLAG_ARCH1)
66 
67 /* Used as a "null" value for timebase values */
68 #define TB_NIL	(~(u64)0)
69 
70 static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
71 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
72 
73 static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
74 {
75 	int me;
76 	int cpu = vcpu->cpu;
77 	wait_queue_head_t *wqp;
78 
79 	wqp = kvm_arch_vcpu_wq(vcpu);
80 	if (waitqueue_active(wqp)) {
81 		wake_up_interruptible(wqp);
82 		++vcpu->stat.halt_wakeup;
83 	}
84 
85 	me = get_cpu();
86 
87 	/* CPU points to the first thread of the core */
88 	if (cpu != me && cpu >= 0 && cpu < nr_cpu_ids) {
89 #ifdef CONFIG_KVM_XICS
90 		int real_cpu = cpu + vcpu->arch.ptid;
91 		if (paca[real_cpu].kvm_hstate.xics_phys)
92 			xics_wake_cpu(real_cpu);
93 		else
94 #endif
95 		if (cpu_online(cpu))
96 			smp_send_reschedule(cpu);
97 	}
98 	put_cpu();
99 }
100 
101 /*
102  * We use the vcpu_load/put functions to measure stolen time.
103  * Stolen time is counted as time when either the vcpu is able to
104  * run as part of a virtual core, but the task running the vcore
105  * is preempted or sleeping, or when the vcpu needs something done
106  * in the kernel by the task running the vcpu, but that task is
107  * preempted or sleeping.  Those two things have to be counted
108  * separately, since one of the vcpu tasks will take on the job
109  * of running the core, and the other vcpu tasks in the vcore will
110  * sleep waiting for it to do that, but that sleep shouldn't count
111  * as stolen time.
112  *
113  * Hence we accumulate stolen time when the vcpu can run as part of
114  * a vcore using vc->stolen_tb, and the stolen time when the vcpu
115  * needs its task to do other things in the kernel (for example,
116  * service a page fault) in busy_stolen.  We don't accumulate
117  * stolen time for a vcore when it is inactive, or for a vcpu
118  * when it is in state RUNNING or NOTREADY.  NOTREADY is a bit of
119  * a misnomer; it means that the vcpu task is not executing in
120  * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
121  * the kernel.  We don't have any way of dividing up that time
122  * between time that the vcpu is genuinely stopped, time that
123  * the task is actively working on behalf of the vcpu, and time
124  * that the task is preempted, so we don't count any of it as
125  * stolen.
126  *
127  * Updates to busy_stolen are protected by arch.tbacct_lock;
128  * updates to vc->stolen_tb are protected by the arch.tbacct_lock
129  * of the vcpu that has taken responsibility for running the vcore
130  * (i.e. vc->runner).  The stolen times are measured in units of
131  * timebase ticks.  (Note that the != TB_NIL checks below are
132  * purely defensive; they should never fail.)
133  */
134 
135 static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
136 {
137 	struct kvmppc_vcore *vc = vcpu->arch.vcore;
138 	unsigned long flags;
139 
140 	spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
141 	if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE &&
142 	    vc->preempt_tb != TB_NIL) {
143 		vc->stolen_tb += mftb() - vc->preempt_tb;
144 		vc->preempt_tb = TB_NIL;
145 	}
146 	if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
147 	    vcpu->arch.busy_preempt != TB_NIL) {
148 		vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
149 		vcpu->arch.busy_preempt = TB_NIL;
150 	}
151 	spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
152 }
153 
154 static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
155 {
156 	struct kvmppc_vcore *vc = vcpu->arch.vcore;
157 	unsigned long flags;
158 
159 	spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
160 	if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
161 		vc->preempt_tb = mftb();
162 	if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
163 		vcpu->arch.busy_preempt = mftb();
164 	spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
165 }
166 
167 static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
168 {
169 	vcpu->arch.shregs.msr = msr;
170 	kvmppc_end_cede(vcpu);
171 }
172 
173 void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
174 {
175 	vcpu->arch.pvr = pvr;
176 }
177 
178 int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
179 {
180 	unsigned long pcr = 0;
181 	struct kvmppc_vcore *vc = vcpu->arch.vcore;
182 
183 	if (arch_compat) {
184 		if (!cpu_has_feature(CPU_FTR_ARCH_206))
185 			return -EINVAL;	/* 970 has no compat mode support */
186 
187 		switch (arch_compat) {
188 		case PVR_ARCH_205:
189 			/*
190 			 * If an arch bit is set in PCR, all the defined
191 			 * higher-order arch bits also have to be set.
192 			 */
193 			pcr = PCR_ARCH_206 | PCR_ARCH_205;
194 			break;
195 		case PVR_ARCH_206:
196 		case PVR_ARCH_206p:
197 			pcr = PCR_ARCH_206;
198 			break;
199 		case PVR_ARCH_207:
200 			break;
201 		default:
202 			return -EINVAL;
203 		}
204 
205 		if (!cpu_has_feature(CPU_FTR_ARCH_207S)) {
206 			/* POWER7 can't emulate POWER8 */
207 			if (!(pcr & PCR_ARCH_206))
208 				return -EINVAL;
209 			pcr &= ~PCR_ARCH_206;
210 		}
211 	}
212 
213 	spin_lock(&vc->lock);
214 	vc->arch_compat = arch_compat;
215 	vc->pcr = pcr;
216 	spin_unlock(&vc->lock);
217 
218 	return 0;
219 }
220 
221 void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
222 {
223 	int r;
224 
225 	pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
226 	pr_err("pc  = %.16lx  msr = %.16llx  trap = %x\n",
227 	       vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
228 	for (r = 0; r < 16; ++r)
229 		pr_err("r%2d = %.16lx  r%d = %.16lx\n",
230 		       r, kvmppc_get_gpr(vcpu, r),
231 		       r+16, kvmppc_get_gpr(vcpu, r+16));
232 	pr_err("ctr = %.16lx  lr  = %.16lx\n",
233 	       vcpu->arch.ctr, vcpu->arch.lr);
234 	pr_err("srr0 = %.16llx srr1 = %.16llx\n",
235 	       vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
236 	pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
237 	       vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
238 	pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
239 	       vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
240 	pr_err("cr = %.8x  xer = %.16lx  dsisr = %.8x\n",
241 	       vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
242 	pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
243 	pr_err("fault dar = %.16lx dsisr = %.8x\n",
244 	       vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
245 	pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
246 	for (r = 0; r < vcpu->arch.slb_max; ++r)
247 		pr_err("  ESID = %.16llx VSID = %.16llx\n",
248 		       vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
249 	pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
250 	       vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
251 	       vcpu->arch.last_inst);
252 }
253 
254 struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
255 {
256 	int r;
257 	struct kvm_vcpu *v, *ret = NULL;
258 
259 	mutex_lock(&kvm->lock);
260 	kvm_for_each_vcpu(r, v, kvm) {
261 		if (v->vcpu_id == id) {
262 			ret = v;
263 			break;
264 		}
265 	}
266 	mutex_unlock(&kvm->lock);
267 	return ret;
268 }
269 
270 static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
271 {
272 	vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
273 	vpa->yield_count = 1;
274 }
275 
276 static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
277 		   unsigned long addr, unsigned long len)
278 {
279 	/* check address is cacheline aligned */
280 	if (addr & (L1_CACHE_BYTES - 1))
281 		return -EINVAL;
282 	spin_lock(&vcpu->arch.vpa_update_lock);
283 	if (v->next_gpa != addr || v->len != len) {
284 		v->next_gpa = addr;
285 		v->len = addr ? len : 0;
286 		v->update_pending = 1;
287 	}
288 	spin_unlock(&vcpu->arch.vpa_update_lock);
289 	return 0;
290 }
291 
292 /* Length for a per-processor buffer is passed in at offset 4 in the buffer */
293 struct reg_vpa {
294 	u32 dummy;
295 	union {
296 		u16 hword;
297 		u32 word;
298 	} length;
299 };
300 
301 static int vpa_is_registered(struct kvmppc_vpa *vpap)
302 {
303 	if (vpap->update_pending)
304 		return vpap->next_gpa != 0;
305 	return vpap->pinned_addr != NULL;
306 }
307 
308 static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
309 				       unsigned long flags,
310 				       unsigned long vcpuid, unsigned long vpa)
311 {
312 	struct kvm *kvm = vcpu->kvm;
313 	unsigned long len, nb;
314 	void *va;
315 	struct kvm_vcpu *tvcpu;
316 	int err;
317 	int subfunc;
318 	struct kvmppc_vpa *vpap;
319 
320 	tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
321 	if (!tvcpu)
322 		return H_PARAMETER;
323 
324 	subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
325 	if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
326 	    subfunc == H_VPA_REG_SLB) {
327 		/* Registering new area - address must be cache-line aligned */
328 		if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
329 			return H_PARAMETER;
330 
331 		/* convert logical addr to kernel addr and read length */
332 		va = kvmppc_pin_guest_page(kvm, vpa, &nb);
333 		if (va == NULL)
334 			return H_PARAMETER;
335 		if (subfunc == H_VPA_REG_VPA)
336 			len = ((struct reg_vpa *)va)->length.hword;
337 		else
338 			len = ((struct reg_vpa *)va)->length.word;
339 		kvmppc_unpin_guest_page(kvm, va, vpa, false);
340 
341 		/* Check length */
342 		if (len > nb || len < sizeof(struct reg_vpa))
343 			return H_PARAMETER;
344 	} else {
345 		vpa = 0;
346 		len = 0;
347 	}
348 
349 	err = H_PARAMETER;
350 	vpap = NULL;
351 	spin_lock(&tvcpu->arch.vpa_update_lock);
352 
353 	switch (subfunc) {
354 	case H_VPA_REG_VPA:		/* register VPA */
355 		if (len < sizeof(struct lppaca))
356 			break;
357 		vpap = &tvcpu->arch.vpa;
358 		err = 0;
359 		break;
360 
361 	case H_VPA_REG_DTL:		/* register DTL */
362 		if (len < sizeof(struct dtl_entry))
363 			break;
364 		len -= len % sizeof(struct dtl_entry);
365 
366 		/* Check that they have previously registered a VPA */
367 		err = H_RESOURCE;
368 		if (!vpa_is_registered(&tvcpu->arch.vpa))
369 			break;
370 
371 		vpap = &tvcpu->arch.dtl;
372 		err = 0;
373 		break;
374 
375 	case H_VPA_REG_SLB:		/* register SLB shadow buffer */
376 		/* Check that they have previously registered a VPA */
377 		err = H_RESOURCE;
378 		if (!vpa_is_registered(&tvcpu->arch.vpa))
379 			break;
380 
381 		vpap = &tvcpu->arch.slb_shadow;
382 		err = 0;
383 		break;
384 
385 	case H_VPA_DEREG_VPA:		/* deregister VPA */
386 		/* Check they don't still have a DTL or SLB buf registered */
387 		err = H_RESOURCE;
388 		if (vpa_is_registered(&tvcpu->arch.dtl) ||
389 		    vpa_is_registered(&tvcpu->arch.slb_shadow))
390 			break;
391 
392 		vpap = &tvcpu->arch.vpa;
393 		err = 0;
394 		break;
395 
396 	case H_VPA_DEREG_DTL:		/* deregister DTL */
397 		vpap = &tvcpu->arch.dtl;
398 		err = 0;
399 		break;
400 
401 	case H_VPA_DEREG_SLB:		/* deregister SLB shadow buffer */
402 		vpap = &tvcpu->arch.slb_shadow;
403 		err = 0;
404 		break;
405 	}
406 
407 	if (vpap) {
408 		vpap->next_gpa = vpa;
409 		vpap->len = len;
410 		vpap->update_pending = 1;
411 	}
412 
413 	spin_unlock(&tvcpu->arch.vpa_update_lock);
414 
415 	return err;
416 }
417 
418 static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
419 {
420 	struct kvm *kvm = vcpu->kvm;
421 	void *va;
422 	unsigned long nb;
423 	unsigned long gpa;
424 
425 	/*
426 	 * We need to pin the page pointed to by vpap->next_gpa,
427 	 * but we can't call kvmppc_pin_guest_page under the lock
428 	 * as it does get_user_pages() and down_read().  So we
429 	 * have to drop the lock, pin the page, then get the lock
430 	 * again and check that a new area didn't get registered
431 	 * in the meantime.
432 	 */
433 	for (;;) {
434 		gpa = vpap->next_gpa;
435 		spin_unlock(&vcpu->arch.vpa_update_lock);
436 		va = NULL;
437 		nb = 0;
438 		if (gpa)
439 			va = kvmppc_pin_guest_page(kvm, gpa, &nb);
440 		spin_lock(&vcpu->arch.vpa_update_lock);
441 		if (gpa == vpap->next_gpa)
442 			break;
443 		/* sigh... unpin that one and try again */
444 		if (va)
445 			kvmppc_unpin_guest_page(kvm, va, gpa, false);
446 	}
447 
448 	vpap->update_pending = 0;
449 	if (va && nb < vpap->len) {
450 		/*
451 		 * If it's now too short, it must be that userspace
452 		 * has changed the mappings underlying guest memory,
453 		 * so unregister the region.
454 		 */
455 		kvmppc_unpin_guest_page(kvm, va, gpa, false);
456 		va = NULL;
457 	}
458 	if (vpap->pinned_addr)
459 		kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
460 					vpap->dirty);
461 	vpap->gpa = gpa;
462 	vpap->pinned_addr = va;
463 	vpap->dirty = false;
464 	if (va)
465 		vpap->pinned_end = va + vpap->len;
466 }
467 
468 static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
469 {
470 	if (!(vcpu->arch.vpa.update_pending ||
471 	      vcpu->arch.slb_shadow.update_pending ||
472 	      vcpu->arch.dtl.update_pending))
473 		return;
474 
475 	spin_lock(&vcpu->arch.vpa_update_lock);
476 	if (vcpu->arch.vpa.update_pending) {
477 		kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
478 		if (vcpu->arch.vpa.pinned_addr)
479 			init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
480 	}
481 	if (vcpu->arch.dtl.update_pending) {
482 		kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
483 		vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
484 		vcpu->arch.dtl_index = 0;
485 	}
486 	if (vcpu->arch.slb_shadow.update_pending)
487 		kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
488 	spin_unlock(&vcpu->arch.vpa_update_lock);
489 }
490 
491 /*
492  * Return the accumulated stolen time for the vcore up until `now'.
493  * The caller should hold the vcore lock.
494  */
495 static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
496 {
497 	u64 p;
498 
499 	/*
500 	 * If we are the task running the vcore, then since we hold
501 	 * the vcore lock, we can't be preempted, so stolen_tb/preempt_tb
502 	 * can't be updated, so we don't need the tbacct_lock.
503 	 * If the vcore is inactive, it can't become active (since we
504 	 * hold the vcore lock), so the vcpu load/put functions won't
505 	 * update stolen_tb/preempt_tb, and we don't need tbacct_lock.
506 	 */
507 	if (vc->vcore_state != VCORE_INACTIVE &&
508 	    vc->runner->arch.run_task != current) {
509 		spin_lock_irq(&vc->runner->arch.tbacct_lock);
510 		p = vc->stolen_tb;
511 		if (vc->preempt_tb != TB_NIL)
512 			p += now - vc->preempt_tb;
513 		spin_unlock_irq(&vc->runner->arch.tbacct_lock);
514 	} else {
515 		p = vc->stolen_tb;
516 	}
517 	return p;
518 }
519 
520 static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
521 				    struct kvmppc_vcore *vc)
522 {
523 	struct dtl_entry *dt;
524 	struct lppaca *vpa;
525 	unsigned long stolen;
526 	unsigned long core_stolen;
527 	u64 now;
528 
529 	dt = vcpu->arch.dtl_ptr;
530 	vpa = vcpu->arch.vpa.pinned_addr;
531 	now = mftb();
532 	core_stolen = vcore_stolen_time(vc, now);
533 	stolen = core_stolen - vcpu->arch.stolen_logged;
534 	vcpu->arch.stolen_logged = core_stolen;
535 	spin_lock_irq(&vcpu->arch.tbacct_lock);
536 	stolen += vcpu->arch.busy_stolen;
537 	vcpu->arch.busy_stolen = 0;
538 	spin_unlock_irq(&vcpu->arch.tbacct_lock);
539 	if (!dt || !vpa)
540 		return;
541 	memset(dt, 0, sizeof(struct dtl_entry));
542 	dt->dispatch_reason = 7;
543 	dt->processor_id = vc->pcpu + vcpu->arch.ptid;
544 	dt->timebase = now + vc->tb_offset;
545 	dt->enqueue_to_dispatch_time = stolen;
546 	dt->srr0 = kvmppc_get_pc(vcpu);
547 	dt->srr1 = vcpu->arch.shregs.msr;
548 	++dt;
549 	if (dt == vcpu->arch.dtl.pinned_end)
550 		dt = vcpu->arch.dtl.pinned_addr;
551 	vcpu->arch.dtl_ptr = dt;
552 	/* order writing *dt vs. writing vpa->dtl_idx */
553 	smp_wmb();
554 	vpa->dtl_idx = ++vcpu->arch.dtl_index;
555 	vcpu->arch.dtl.dirty = true;
556 }
557 
558 int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
559 {
560 	unsigned long req = kvmppc_get_gpr(vcpu, 3);
561 	unsigned long target, ret = H_SUCCESS;
562 	struct kvm_vcpu *tvcpu;
563 	int idx, rc;
564 
565 	switch (req) {
566 	case H_ENTER:
567 		idx = srcu_read_lock(&vcpu->kvm->srcu);
568 		ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
569 					      kvmppc_get_gpr(vcpu, 5),
570 					      kvmppc_get_gpr(vcpu, 6),
571 					      kvmppc_get_gpr(vcpu, 7));
572 		srcu_read_unlock(&vcpu->kvm->srcu, idx);
573 		break;
574 	case H_CEDE:
575 		break;
576 	case H_PROD:
577 		target = kvmppc_get_gpr(vcpu, 4);
578 		tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
579 		if (!tvcpu) {
580 			ret = H_PARAMETER;
581 			break;
582 		}
583 		tvcpu->arch.prodded = 1;
584 		smp_mb();
585 		if (vcpu->arch.ceded) {
586 			if (waitqueue_active(&vcpu->wq)) {
587 				wake_up_interruptible(&vcpu->wq);
588 				vcpu->stat.halt_wakeup++;
589 			}
590 		}
591 		break;
592 	case H_CONFER:
593 		target = kvmppc_get_gpr(vcpu, 4);
594 		if (target == -1)
595 			break;
596 		tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
597 		if (!tvcpu) {
598 			ret = H_PARAMETER;
599 			break;
600 		}
601 		kvm_vcpu_yield_to(tvcpu);
602 		break;
603 	case H_REGISTER_VPA:
604 		ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
605 					kvmppc_get_gpr(vcpu, 5),
606 					kvmppc_get_gpr(vcpu, 6));
607 		break;
608 	case H_RTAS:
609 		if (list_empty(&vcpu->kvm->arch.rtas_tokens))
610 			return RESUME_HOST;
611 
612 		idx = srcu_read_lock(&vcpu->kvm->srcu);
613 		rc = kvmppc_rtas_hcall(vcpu);
614 		srcu_read_unlock(&vcpu->kvm->srcu, idx);
615 
616 		if (rc == -ENOENT)
617 			return RESUME_HOST;
618 		else if (rc == 0)
619 			break;
620 
621 		/* Send the error out to userspace via KVM_RUN */
622 		return rc;
623 
624 	case H_XIRR:
625 	case H_CPPR:
626 	case H_EOI:
627 	case H_IPI:
628 	case H_IPOLL:
629 	case H_XIRR_X:
630 		if (kvmppc_xics_enabled(vcpu)) {
631 			ret = kvmppc_xics_hcall(vcpu, req);
632 			break;
633 		} /* fallthrough */
634 	default:
635 		return RESUME_HOST;
636 	}
637 	kvmppc_set_gpr(vcpu, 3, ret);
638 	vcpu->arch.hcall_needed = 0;
639 	return RESUME_GUEST;
640 }
641 
642 static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
643 				 struct task_struct *tsk)
644 {
645 	int r = RESUME_HOST;
646 
647 	vcpu->stat.sum_exits++;
648 
649 	run->exit_reason = KVM_EXIT_UNKNOWN;
650 	run->ready_for_interrupt_injection = 1;
651 	switch (vcpu->arch.trap) {
652 	/* We're good on these - the host merely wanted to get our attention */
653 	case BOOK3S_INTERRUPT_HV_DECREMENTER:
654 		vcpu->stat.dec_exits++;
655 		r = RESUME_GUEST;
656 		break;
657 	case BOOK3S_INTERRUPT_EXTERNAL:
658 	case BOOK3S_INTERRUPT_H_DOORBELL:
659 		vcpu->stat.ext_intr_exits++;
660 		r = RESUME_GUEST;
661 		break;
662 	case BOOK3S_INTERRUPT_PERFMON:
663 		r = RESUME_GUEST;
664 		break;
665 	case BOOK3S_INTERRUPT_MACHINE_CHECK:
666 		/*
667 		 * Deliver a machine check interrupt to the guest.
668 		 * We have to do this, even if the host has handled the
669 		 * machine check, because machine checks use SRR0/1 and
670 		 * the interrupt might have trashed guest state in them.
671 		 */
672 		kvmppc_book3s_queue_irqprio(vcpu,
673 					    BOOK3S_INTERRUPT_MACHINE_CHECK);
674 		r = RESUME_GUEST;
675 		break;
676 	case BOOK3S_INTERRUPT_PROGRAM:
677 	{
678 		ulong flags;
679 		/*
680 		 * Normally program interrupts are delivered directly
681 		 * to the guest by the hardware, but we can get here
682 		 * as a result of a hypervisor emulation interrupt
683 		 * (e40) getting turned into a 700 by BML RTAS.
684 		 */
685 		flags = vcpu->arch.shregs.msr & 0x1f0000ull;
686 		kvmppc_core_queue_program(vcpu, flags);
687 		r = RESUME_GUEST;
688 		break;
689 	}
690 	case BOOK3S_INTERRUPT_SYSCALL:
691 	{
692 		/* hcall - punt to userspace */
693 		int i;
694 
695 		/* hypercall with MSR_PR has already been handled in rmode,
696 		 * and never reaches here.
697 		 */
698 
699 		run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
700 		for (i = 0; i < 9; ++i)
701 			run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
702 		run->exit_reason = KVM_EXIT_PAPR_HCALL;
703 		vcpu->arch.hcall_needed = 1;
704 		r = RESUME_HOST;
705 		break;
706 	}
707 	/*
708 	 * We get these next two if the guest accesses a page which it thinks
709 	 * it has mapped but which is not actually present, either because
710 	 * it is for an emulated I/O device or because the corresonding
711 	 * host page has been paged out.  Any other HDSI/HISI interrupts
712 	 * have been handled already.
713 	 */
714 	case BOOK3S_INTERRUPT_H_DATA_STORAGE:
715 		r = RESUME_PAGE_FAULT;
716 		break;
717 	case BOOK3S_INTERRUPT_H_INST_STORAGE:
718 		vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
719 		vcpu->arch.fault_dsisr = 0;
720 		r = RESUME_PAGE_FAULT;
721 		break;
722 	/*
723 	 * This occurs if the guest executes an illegal instruction.
724 	 * We just generate a program interrupt to the guest, since
725 	 * we don't emulate any guest instructions at this stage.
726 	 */
727 	case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
728 		kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
729 		r = RESUME_GUEST;
730 		break;
731 	/*
732 	 * This occurs if the guest (kernel or userspace), does something that
733 	 * is prohibited by HFSCR.  We just generate a program interrupt to
734 	 * the guest.
735 	 */
736 	case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
737 		kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
738 		r = RESUME_GUEST;
739 		break;
740 	default:
741 		kvmppc_dump_regs(vcpu);
742 		printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
743 			vcpu->arch.trap, kvmppc_get_pc(vcpu),
744 			vcpu->arch.shregs.msr);
745 		run->hw.hardware_exit_reason = vcpu->arch.trap;
746 		r = RESUME_HOST;
747 		break;
748 	}
749 
750 	return r;
751 }
752 
753 static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
754 					    struct kvm_sregs *sregs)
755 {
756 	int i;
757 
758 	memset(sregs, 0, sizeof(struct kvm_sregs));
759 	sregs->pvr = vcpu->arch.pvr;
760 	for (i = 0; i < vcpu->arch.slb_max; i++) {
761 		sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
762 		sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
763 	}
764 
765 	return 0;
766 }
767 
768 static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
769 					    struct kvm_sregs *sregs)
770 {
771 	int i, j;
772 
773 	kvmppc_set_pvr_hv(vcpu, sregs->pvr);
774 
775 	j = 0;
776 	for (i = 0; i < vcpu->arch.slb_nr; i++) {
777 		if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
778 			vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
779 			vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
780 			++j;
781 		}
782 	}
783 	vcpu->arch.slb_max = j;
784 
785 	return 0;
786 }
787 
788 static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr)
789 {
790 	struct kvmppc_vcore *vc = vcpu->arch.vcore;
791 	u64 mask;
792 
793 	spin_lock(&vc->lock);
794 	/*
795 	 * If ILE (interrupt little-endian) has changed, update the
796 	 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
797 	 */
798 	if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
799 		struct kvm *kvm = vcpu->kvm;
800 		struct kvm_vcpu *vcpu;
801 		int i;
802 
803 		mutex_lock(&kvm->lock);
804 		kvm_for_each_vcpu(i, vcpu, kvm) {
805 			if (vcpu->arch.vcore != vc)
806 				continue;
807 			if (new_lpcr & LPCR_ILE)
808 				vcpu->arch.intr_msr |= MSR_LE;
809 			else
810 				vcpu->arch.intr_msr &= ~MSR_LE;
811 		}
812 		mutex_unlock(&kvm->lock);
813 	}
814 
815 	/*
816 	 * Userspace can only modify DPFD (default prefetch depth),
817 	 * ILE (interrupt little-endian) and TC (translation control).
818 	 * On POWER8 userspace can also modify AIL (alt. interrupt loc.)
819 	 */
820 	mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
821 	if (cpu_has_feature(CPU_FTR_ARCH_207S))
822 		mask |= LPCR_AIL;
823 	vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
824 	spin_unlock(&vc->lock);
825 }
826 
827 static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
828 				 union kvmppc_one_reg *val)
829 {
830 	int r = 0;
831 	long int i;
832 
833 	switch (id) {
834 	case KVM_REG_PPC_HIOR:
835 		*val = get_reg_val(id, 0);
836 		break;
837 	case KVM_REG_PPC_DABR:
838 		*val = get_reg_val(id, vcpu->arch.dabr);
839 		break;
840 	case KVM_REG_PPC_DABRX:
841 		*val = get_reg_val(id, vcpu->arch.dabrx);
842 		break;
843 	case KVM_REG_PPC_DSCR:
844 		*val = get_reg_val(id, vcpu->arch.dscr);
845 		break;
846 	case KVM_REG_PPC_PURR:
847 		*val = get_reg_val(id, vcpu->arch.purr);
848 		break;
849 	case KVM_REG_PPC_SPURR:
850 		*val = get_reg_val(id, vcpu->arch.spurr);
851 		break;
852 	case KVM_REG_PPC_AMR:
853 		*val = get_reg_val(id, vcpu->arch.amr);
854 		break;
855 	case KVM_REG_PPC_UAMOR:
856 		*val = get_reg_val(id, vcpu->arch.uamor);
857 		break;
858 	case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
859 		i = id - KVM_REG_PPC_MMCR0;
860 		*val = get_reg_val(id, vcpu->arch.mmcr[i]);
861 		break;
862 	case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
863 		i = id - KVM_REG_PPC_PMC1;
864 		*val = get_reg_val(id, vcpu->arch.pmc[i]);
865 		break;
866 	case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
867 		i = id - KVM_REG_PPC_SPMC1;
868 		*val = get_reg_val(id, vcpu->arch.spmc[i]);
869 		break;
870 	case KVM_REG_PPC_SIAR:
871 		*val = get_reg_val(id, vcpu->arch.siar);
872 		break;
873 	case KVM_REG_PPC_SDAR:
874 		*val = get_reg_val(id, vcpu->arch.sdar);
875 		break;
876 	case KVM_REG_PPC_SIER:
877 		*val = get_reg_val(id, vcpu->arch.sier);
878 		break;
879 	case KVM_REG_PPC_IAMR:
880 		*val = get_reg_val(id, vcpu->arch.iamr);
881 		break;
882 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
883 	case KVM_REG_PPC_TFHAR:
884 		*val = get_reg_val(id, vcpu->arch.tfhar);
885 		break;
886 	case KVM_REG_PPC_TFIAR:
887 		*val = get_reg_val(id, vcpu->arch.tfiar);
888 		break;
889 	case KVM_REG_PPC_TEXASR:
890 		*val = get_reg_val(id, vcpu->arch.texasr);
891 		break;
892 #endif
893 	case KVM_REG_PPC_FSCR:
894 		*val = get_reg_val(id, vcpu->arch.fscr);
895 		break;
896 	case KVM_REG_PPC_PSPB:
897 		*val = get_reg_val(id, vcpu->arch.pspb);
898 		break;
899 	case KVM_REG_PPC_EBBHR:
900 		*val = get_reg_val(id, vcpu->arch.ebbhr);
901 		break;
902 	case KVM_REG_PPC_EBBRR:
903 		*val = get_reg_val(id, vcpu->arch.ebbrr);
904 		break;
905 	case KVM_REG_PPC_BESCR:
906 		*val = get_reg_val(id, vcpu->arch.bescr);
907 		break;
908 	case KVM_REG_PPC_TAR:
909 		*val = get_reg_val(id, vcpu->arch.tar);
910 		break;
911 	case KVM_REG_PPC_DPDES:
912 		*val = get_reg_val(id, vcpu->arch.vcore->dpdes);
913 		break;
914 	case KVM_REG_PPC_DAWR:
915 		*val = get_reg_val(id, vcpu->arch.dawr);
916 		break;
917 	case KVM_REG_PPC_DAWRX:
918 		*val = get_reg_val(id, vcpu->arch.dawrx);
919 		break;
920 	case KVM_REG_PPC_CIABR:
921 		*val = get_reg_val(id, vcpu->arch.ciabr);
922 		break;
923 	case KVM_REG_PPC_IC:
924 		*val = get_reg_val(id, vcpu->arch.ic);
925 		break;
926 	case KVM_REG_PPC_VTB:
927 		*val = get_reg_val(id, vcpu->arch.vtb);
928 		break;
929 	case KVM_REG_PPC_CSIGR:
930 		*val = get_reg_val(id, vcpu->arch.csigr);
931 		break;
932 	case KVM_REG_PPC_TACR:
933 		*val = get_reg_val(id, vcpu->arch.tacr);
934 		break;
935 	case KVM_REG_PPC_TCSCR:
936 		*val = get_reg_val(id, vcpu->arch.tcscr);
937 		break;
938 	case KVM_REG_PPC_PID:
939 		*val = get_reg_val(id, vcpu->arch.pid);
940 		break;
941 	case KVM_REG_PPC_ACOP:
942 		*val = get_reg_val(id, vcpu->arch.acop);
943 		break;
944 	case KVM_REG_PPC_WORT:
945 		*val = get_reg_val(id, vcpu->arch.wort);
946 		break;
947 	case KVM_REG_PPC_VPA_ADDR:
948 		spin_lock(&vcpu->arch.vpa_update_lock);
949 		*val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
950 		spin_unlock(&vcpu->arch.vpa_update_lock);
951 		break;
952 	case KVM_REG_PPC_VPA_SLB:
953 		spin_lock(&vcpu->arch.vpa_update_lock);
954 		val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
955 		val->vpaval.length = vcpu->arch.slb_shadow.len;
956 		spin_unlock(&vcpu->arch.vpa_update_lock);
957 		break;
958 	case KVM_REG_PPC_VPA_DTL:
959 		spin_lock(&vcpu->arch.vpa_update_lock);
960 		val->vpaval.addr = vcpu->arch.dtl.next_gpa;
961 		val->vpaval.length = vcpu->arch.dtl.len;
962 		spin_unlock(&vcpu->arch.vpa_update_lock);
963 		break;
964 	case KVM_REG_PPC_TB_OFFSET:
965 		*val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
966 		break;
967 	case KVM_REG_PPC_LPCR:
968 		*val = get_reg_val(id, vcpu->arch.vcore->lpcr);
969 		break;
970 	case KVM_REG_PPC_PPR:
971 		*val = get_reg_val(id, vcpu->arch.ppr);
972 		break;
973 	case KVM_REG_PPC_ARCH_COMPAT:
974 		*val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
975 		break;
976 	default:
977 		r = -EINVAL;
978 		break;
979 	}
980 
981 	return r;
982 }
983 
984 static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
985 				 union kvmppc_one_reg *val)
986 {
987 	int r = 0;
988 	long int i;
989 	unsigned long addr, len;
990 
991 	switch (id) {
992 	case KVM_REG_PPC_HIOR:
993 		/* Only allow this to be set to zero */
994 		if (set_reg_val(id, *val))
995 			r = -EINVAL;
996 		break;
997 	case KVM_REG_PPC_DABR:
998 		vcpu->arch.dabr = set_reg_val(id, *val);
999 		break;
1000 	case KVM_REG_PPC_DABRX:
1001 		vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
1002 		break;
1003 	case KVM_REG_PPC_DSCR:
1004 		vcpu->arch.dscr = set_reg_val(id, *val);
1005 		break;
1006 	case KVM_REG_PPC_PURR:
1007 		vcpu->arch.purr = set_reg_val(id, *val);
1008 		break;
1009 	case KVM_REG_PPC_SPURR:
1010 		vcpu->arch.spurr = set_reg_val(id, *val);
1011 		break;
1012 	case KVM_REG_PPC_AMR:
1013 		vcpu->arch.amr = set_reg_val(id, *val);
1014 		break;
1015 	case KVM_REG_PPC_UAMOR:
1016 		vcpu->arch.uamor = set_reg_val(id, *val);
1017 		break;
1018 	case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
1019 		i = id - KVM_REG_PPC_MMCR0;
1020 		vcpu->arch.mmcr[i] = set_reg_val(id, *val);
1021 		break;
1022 	case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1023 		i = id - KVM_REG_PPC_PMC1;
1024 		vcpu->arch.pmc[i] = set_reg_val(id, *val);
1025 		break;
1026 	case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1027 		i = id - KVM_REG_PPC_SPMC1;
1028 		vcpu->arch.spmc[i] = set_reg_val(id, *val);
1029 		break;
1030 	case KVM_REG_PPC_SIAR:
1031 		vcpu->arch.siar = set_reg_val(id, *val);
1032 		break;
1033 	case KVM_REG_PPC_SDAR:
1034 		vcpu->arch.sdar = set_reg_val(id, *val);
1035 		break;
1036 	case KVM_REG_PPC_SIER:
1037 		vcpu->arch.sier = set_reg_val(id, *val);
1038 		break;
1039 	case KVM_REG_PPC_IAMR:
1040 		vcpu->arch.iamr = set_reg_val(id, *val);
1041 		break;
1042 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1043 	case KVM_REG_PPC_TFHAR:
1044 		vcpu->arch.tfhar = set_reg_val(id, *val);
1045 		break;
1046 	case KVM_REG_PPC_TFIAR:
1047 		vcpu->arch.tfiar = set_reg_val(id, *val);
1048 		break;
1049 	case KVM_REG_PPC_TEXASR:
1050 		vcpu->arch.texasr = set_reg_val(id, *val);
1051 		break;
1052 #endif
1053 	case KVM_REG_PPC_FSCR:
1054 		vcpu->arch.fscr = set_reg_val(id, *val);
1055 		break;
1056 	case KVM_REG_PPC_PSPB:
1057 		vcpu->arch.pspb = set_reg_val(id, *val);
1058 		break;
1059 	case KVM_REG_PPC_EBBHR:
1060 		vcpu->arch.ebbhr = set_reg_val(id, *val);
1061 		break;
1062 	case KVM_REG_PPC_EBBRR:
1063 		vcpu->arch.ebbrr = set_reg_val(id, *val);
1064 		break;
1065 	case KVM_REG_PPC_BESCR:
1066 		vcpu->arch.bescr = set_reg_val(id, *val);
1067 		break;
1068 	case KVM_REG_PPC_TAR:
1069 		vcpu->arch.tar = set_reg_val(id, *val);
1070 		break;
1071 	case KVM_REG_PPC_DPDES:
1072 		vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
1073 		break;
1074 	case KVM_REG_PPC_DAWR:
1075 		vcpu->arch.dawr = set_reg_val(id, *val);
1076 		break;
1077 	case KVM_REG_PPC_DAWRX:
1078 		vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
1079 		break;
1080 	case KVM_REG_PPC_CIABR:
1081 		vcpu->arch.ciabr = set_reg_val(id, *val);
1082 		/* Don't allow setting breakpoints in hypervisor code */
1083 		if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
1084 			vcpu->arch.ciabr &= ~CIABR_PRIV;	/* disable */
1085 		break;
1086 	case KVM_REG_PPC_IC:
1087 		vcpu->arch.ic = set_reg_val(id, *val);
1088 		break;
1089 	case KVM_REG_PPC_VTB:
1090 		vcpu->arch.vtb = set_reg_val(id, *val);
1091 		break;
1092 	case KVM_REG_PPC_CSIGR:
1093 		vcpu->arch.csigr = set_reg_val(id, *val);
1094 		break;
1095 	case KVM_REG_PPC_TACR:
1096 		vcpu->arch.tacr = set_reg_val(id, *val);
1097 		break;
1098 	case KVM_REG_PPC_TCSCR:
1099 		vcpu->arch.tcscr = set_reg_val(id, *val);
1100 		break;
1101 	case KVM_REG_PPC_PID:
1102 		vcpu->arch.pid = set_reg_val(id, *val);
1103 		break;
1104 	case KVM_REG_PPC_ACOP:
1105 		vcpu->arch.acop = set_reg_val(id, *val);
1106 		break;
1107 	case KVM_REG_PPC_WORT:
1108 		vcpu->arch.wort = set_reg_val(id, *val);
1109 		break;
1110 	case KVM_REG_PPC_VPA_ADDR:
1111 		addr = set_reg_val(id, *val);
1112 		r = -EINVAL;
1113 		if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
1114 			      vcpu->arch.dtl.next_gpa))
1115 			break;
1116 		r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
1117 		break;
1118 	case KVM_REG_PPC_VPA_SLB:
1119 		addr = val->vpaval.addr;
1120 		len = val->vpaval.length;
1121 		r = -EINVAL;
1122 		if (addr && !vcpu->arch.vpa.next_gpa)
1123 			break;
1124 		r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
1125 		break;
1126 	case KVM_REG_PPC_VPA_DTL:
1127 		addr = val->vpaval.addr;
1128 		len = val->vpaval.length;
1129 		r = -EINVAL;
1130 		if (addr && (len < sizeof(struct dtl_entry) ||
1131 			     !vcpu->arch.vpa.next_gpa))
1132 			break;
1133 		len -= len % sizeof(struct dtl_entry);
1134 		r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
1135 		break;
1136 	case KVM_REG_PPC_TB_OFFSET:
1137 		/* round up to multiple of 2^24 */
1138 		vcpu->arch.vcore->tb_offset =
1139 			ALIGN(set_reg_val(id, *val), 1UL << 24);
1140 		break;
1141 	case KVM_REG_PPC_LPCR:
1142 		kvmppc_set_lpcr(vcpu, set_reg_val(id, *val));
1143 		break;
1144 	case KVM_REG_PPC_PPR:
1145 		vcpu->arch.ppr = set_reg_val(id, *val);
1146 		break;
1147 	case KVM_REG_PPC_ARCH_COMPAT:
1148 		r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
1149 		break;
1150 	default:
1151 		r = -EINVAL;
1152 		break;
1153 	}
1154 
1155 	return r;
1156 }
1157 
1158 static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
1159 						   unsigned int id)
1160 {
1161 	struct kvm_vcpu *vcpu;
1162 	int err = -EINVAL;
1163 	int core;
1164 	struct kvmppc_vcore *vcore;
1165 
1166 	core = id / threads_per_core;
1167 	if (core >= KVM_MAX_VCORES)
1168 		goto out;
1169 
1170 	err = -ENOMEM;
1171 	vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
1172 	if (!vcpu)
1173 		goto out;
1174 
1175 	err = kvm_vcpu_init(vcpu, kvm, id);
1176 	if (err)
1177 		goto free_vcpu;
1178 
1179 	vcpu->arch.shared = &vcpu->arch.shregs;
1180 	vcpu->arch.mmcr[0] = MMCR0_FC;
1181 	vcpu->arch.ctrl = CTRL_RUNLATCH;
1182 	/* default to host PVR, since we can't spoof it */
1183 	kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
1184 	spin_lock_init(&vcpu->arch.vpa_update_lock);
1185 	spin_lock_init(&vcpu->arch.tbacct_lock);
1186 	vcpu->arch.busy_preempt = TB_NIL;
1187 	vcpu->arch.intr_msr = MSR_SF | MSR_ME;
1188 
1189 	kvmppc_mmu_book3s_hv_init(vcpu);
1190 
1191 	vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
1192 
1193 	init_waitqueue_head(&vcpu->arch.cpu_run);
1194 
1195 	mutex_lock(&kvm->lock);
1196 	vcore = kvm->arch.vcores[core];
1197 	if (!vcore) {
1198 		vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1199 		if (vcore) {
1200 			INIT_LIST_HEAD(&vcore->runnable_threads);
1201 			spin_lock_init(&vcore->lock);
1202 			init_waitqueue_head(&vcore->wq);
1203 			vcore->preempt_tb = TB_NIL;
1204 			vcore->lpcr = kvm->arch.lpcr;
1205 			vcore->first_vcpuid = core * threads_per_core;
1206 			vcore->kvm = kvm;
1207 		}
1208 		kvm->arch.vcores[core] = vcore;
1209 		kvm->arch.online_vcores++;
1210 	}
1211 	mutex_unlock(&kvm->lock);
1212 
1213 	if (!vcore)
1214 		goto free_vcpu;
1215 
1216 	spin_lock(&vcore->lock);
1217 	++vcore->num_threads;
1218 	spin_unlock(&vcore->lock);
1219 	vcpu->arch.vcore = vcore;
1220 	vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
1221 
1222 	vcpu->arch.cpu_type = KVM_CPU_3S_64;
1223 	kvmppc_sanity_check(vcpu);
1224 
1225 	return vcpu;
1226 
1227 free_vcpu:
1228 	kmem_cache_free(kvm_vcpu_cache, vcpu);
1229 out:
1230 	return ERR_PTR(err);
1231 }
1232 
1233 static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1234 {
1235 	if (vpa->pinned_addr)
1236 		kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1237 					vpa->dirty);
1238 }
1239 
1240 static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
1241 {
1242 	spin_lock(&vcpu->arch.vpa_update_lock);
1243 	unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1244 	unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1245 	unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
1246 	spin_unlock(&vcpu->arch.vpa_update_lock);
1247 	kvm_vcpu_uninit(vcpu);
1248 	kmem_cache_free(kvm_vcpu_cache, vcpu);
1249 }
1250 
1251 static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1252 {
1253 	/* Indicate we want to get back into the guest */
1254 	return 1;
1255 }
1256 
1257 static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
1258 {
1259 	unsigned long dec_nsec, now;
1260 
1261 	now = get_tb();
1262 	if (now > vcpu->arch.dec_expires) {
1263 		/* decrementer has already gone negative */
1264 		kvmppc_core_queue_dec(vcpu);
1265 		kvmppc_core_prepare_to_enter(vcpu);
1266 		return;
1267 	}
1268 	dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1269 		   / tb_ticks_per_sec;
1270 	hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1271 		      HRTIMER_MODE_REL);
1272 	vcpu->arch.timer_running = 1;
1273 }
1274 
1275 static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
1276 {
1277 	vcpu->arch.ceded = 0;
1278 	if (vcpu->arch.timer_running) {
1279 		hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1280 		vcpu->arch.timer_running = 0;
1281 	}
1282 }
1283 
1284 extern void __kvmppc_vcore_entry(void);
1285 
1286 static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1287 				   struct kvm_vcpu *vcpu)
1288 {
1289 	u64 now;
1290 
1291 	if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1292 		return;
1293 	spin_lock_irq(&vcpu->arch.tbacct_lock);
1294 	now = mftb();
1295 	vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1296 		vcpu->arch.stolen_logged;
1297 	vcpu->arch.busy_preempt = now;
1298 	vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
1299 	spin_unlock_irq(&vcpu->arch.tbacct_lock);
1300 	--vc->n_runnable;
1301 	list_del(&vcpu->arch.run_list);
1302 }
1303 
1304 static int kvmppc_grab_hwthread(int cpu)
1305 {
1306 	struct paca_struct *tpaca;
1307 	long timeout = 1000;
1308 
1309 	tpaca = &paca[cpu];
1310 
1311 	/* Ensure the thread won't go into the kernel if it wakes */
1312 	tpaca->kvm_hstate.hwthread_req = 1;
1313 	tpaca->kvm_hstate.kvm_vcpu = NULL;
1314 
1315 	/*
1316 	 * If the thread is already executing in the kernel (e.g. handling
1317 	 * a stray interrupt), wait for it to get back to nap mode.
1318 	 * The smp_mb() is to ensure that our setting of hwthread_req
1319 	 * is visible before we look at hwthread_state, so if this
1320 	 * races with the code at system_reset_pSeries and the thread
1321 	 * misses our setting of hwthread_req, we are sure to see its
1322 	 * setting of hwthread_state, and vice versa.
1323 	 */
1324 	smp_mb();
1325 	while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1326 		if (--timeout <= 0) {
1327 			pr_err("KVM: couldn't grab cpu %d\n", cpu);
1328 			return -EBUSY;
1329 		}
1330 		udelay(1);
1331 	}
1332 	return 0;
1333 }
1334 
1335 static void kvmppc_release_hwthread(int cpu)
1336 {
1337 	struct paca_struct *tpaca;
1338 
1339 	tpaca = &paca[cpu];
1340 	tpaca->kvm_hstate.hwthread_req = 0;
1341 	tpaca->kvm_hstate.kvm_vcpu = NULL;
1342 }
1343 
1344 static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1345 {
1346 	int cpu;
1347 	struct paca_struct *tpaca;
1348 	struct kvmppc_vcore *vc = vcpu->arch.vcore;
1349 
1350 	if (vcpu->arch.timer_running) {
1351 		hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1352 		vcpu->arch.timer_running = 0;
1353 	}
1354 	cpu = vc->pcpu + vcpu->arch.ptid;
1355 	tpaca = &paca[cpu];
1356 	tpaca->kvm_hstate.kvm_vcpu = vcpu;
1357 	tpaca->kvm_hstate.kvm_vcore = vc;
1358 	tpaca->kvm_hstate.ptid = vcpu->arch.ptid;
1359 	vcpu->cpu = vc->pcpu;
1360 	smp_wmb();
1361 #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
1362 	if (cpu != smp_processor_id()) {
1363 #ifdef CONFIG_KVM_XICS
1364 		xics_wake_cpu(cpu);
1365 #endif
1366 		if (vcpu->arch.ptid)
1367 			++vc->n_woken;
1368 	}
1369 #endif
1370 }
1371 
1372 static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
1373 {
1374 	int i;
1375 
1376 	HMT_low();
1377 	i = 0;
1378 	while (vc->nap_count < vc->n_woken) {
1379 		if (++i >= 1000000) {
1380 			pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1381 			       vc->nap_count, vc->n_woken);
1382 			break;
1383 		}
1384 		cpu_relax();
1385 	}
1386 	HMT_medium();
1387 }
1388 
1389 /*
1390  * Check that we are on thread 0 and that any other threads in
1391  * this core are off-line.  Then grab the threads so they can't
1392  * enter the kernel.
1393  */
1394 static int on_primary_thread(void)
1395 {
1396 	int cpu = smp_processor_id();
1397 	int thr = cpu_thread_in_core(cpu);
1398 
1399 	if (thr)
1400 		return 0;
1401 	while (++thr < threads_per_core)
1402 		if (cpu_online(cpu + thr))
1403 			return 0;
1404 
1405 	/* Grab all hw threads so they can't go into the kernel */
1406 	for (thr = 1; thr < threads_per_core; ++thr) {
1407 		if (kvmppc_grab_hwthread(cpu + thr)) {
1408 			/* Couldn't grab one; let the others go */
1409 			do {
1410 				kvmppc_release_hwthread(cpu + thr);
1411 			} while (--thr > 0);
1412 			return 0;
1413 		}
1414 	}
1415 	return 1;
1416 }
1417 
1418 /*
1419  * Run a set of guest threads on a physical core.
1420  * Called with vc->lock held.
1421  */
1422 static void kvmppc_run_core(struct kvmppc_vcore *vc)
1423 {
1424 	struct kvm_vcpu *vcpu, *vnext;
1425 	long ret;
1426 	u64 now;
1427 	int i, need_vpa_update;
1428 	int srcu_idx;
1429 	struct kvm_vcpu *vcpus_to_update[threads_per_core];
1430 
1431 	/* don't start if any threads have a signal pending */
1432 	need_vpa_update = 0;
1433 	list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1434 		if (signal_pending(vcpu->arch.run_task))
1435 			return;
1436 		if (vcpu->arch.vpa.update_pending ||
1437 		    vcpu->arch.slb_shadow.update_pending ||
1438 		    vcpu->arch.dtl.update_pending)
1439 			vcpus_to_update[need_vpa_update++] = vcpu;
1440 	}
1441 
1442 	/*
1443 	 * Initialize *vc, in particular vc->vcore_state, so we can
1444 	 * drop the vcore lock if necessary.
1445 	 */
1446 	vc->n_woken = 0;
1447 	vc->nap_count = 0;
1448 	vc->entry_exit_count = 0;
1449 	vc->vcore_state = VCORE_STARTING;
1450 	vc->in_guest = 0;
1451 	vc->napping_threads = 0;
1452 
1453 	/*
1454 	 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1455 	 * which can't be called with any spinlocks held.
1456 	 */
1457 	if (need_vpa_update) {
1458 		spin_unlock(&vc->lock);
1459 		for (i = 0; i < need_vpa_update; ++i)
1460 			kvmppc_update_vpas(vcpus_to_update[i]);
1461 		spin_lock(&vc->lock);
1462 	}
1463 
1464 	/*
1465 	 * Make sure we are running on thread 0, and that
1466 	 * secondary threads are offline.
1467 	 */
1468 	if (threads_per_core > 1 && !on_primary_thread()) {
1469 		list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1470 			vcpu->arch.ret = -EBUSY;
1471 		goto out;
1472 	}
1473 
1474 	vc->pcpu = smp_processor_id();
1475 	list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1476 		kvmppc_start_thread(vcpu);
1477 		kvmppc_create_dtl_entry(vcpu, vc);
1478 	}
1479 
1480 	/* Set this explicitly in case thread 0 doesn't have a vcpu */
1481 	get_paca()->kvm_hstate.kvm_vcore = vc;
1482 	get_paca()->kvm_hstate.ptid = 0;
1483 
1484 	vc->vcore_state = VCORE_RUNNING;
1485 	preempt_disable();
1486 	spin_unlock(&vc->lock);
1487 
1488 	kvm_guest_enter();
1489 
1490 	srcu_idx = srcu_read_lock(&vc->kvm->srcu);
1491 
1492 	__kvmppc_vcore_entry();
1493 
1494 	spin_lock(&vc->lock);
1495 	/* disable sending of IPIs on virtual external irqs */
1496 	list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1497 		vcpu->cpu = -1;
1498 	/* wait for secondary threads to finish writing their state to memory */
1499 	if (vc->nap_count < vc->n_woken)
1500 		kvmppc_wait_for_nap(vc);
1501 	for (i = 0; i < threads_per_core; ++i)
1502 		kvmppc_release_hwthread(vc->pcpu + i);
1503 	/* prevent other vcpu threads from doing kvmppc_start_thread() now */
1504 	vc->vcore_state = VCORE_EXITING;
1505 	spin_unlock(&vc->lock);
1506 
1507 	srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
1508 
1509 	/* make sure updates to secondary vcpu structs are visible now */
1510 	smp_mb();
1511 	kvm_guest_exit();
1512 
1513 	preempt_enable();
1514 	cond_resched();
1515 
1516 	spin_lock(&vc->lock);
1517 	now = get_tb();
1518 	list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1519 		/* cancel pending dec exception if dec is positive */
1520 		if (now < vcpu->arch.dec_expires &&
1521 		    kvmppc_core_pending_dec(vcpu))
1522 			kvmppc_core_dequeue_dec(vcpu);
1523 
1524 		ret = RESUME_GUEST;
1525 		if (vcpu->arch.trap)
1526 			ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
1527 						    vcpu->arch.run_task);
1528 
1529 		vcpu->arch.ret = ret;
1530 		vcpu->arch.trap = 0;
1531 
1532 		if (vcpu->arch.ceded) {
1533 			if (ret != RESUME_GUEST)
1534 				kvmppc_end_cede(vcpu);
1535 			else
1536 				kvmppc_set_timer(vcpu);
1537 		}
1538 	}
1539 
1540  out:
1541 	vc->vcore_state = VCORE_INACTIVE;
1542 	list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1543 				 arch.run_list) {
1544 		if (vcpu->arch.ret != RESUME_GUEST) {
1545 			kvmppc_remove_runnable(vc, vcpu);
1546 			wake_up(&vcpu->arch.cpu_run);
1547 		}
1548 	}
1549 }
1550 
1551 /*
1552  * Wait for some other vcpu thread to execute us, and
1553  * wake us up when we need to handle something in the host.
1554  */
1555 static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
1556 {
1557 	DEFINE_WAIT(wait);
1558 
1559 	prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1560 	if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1561 		schedule();
1562 	finish_wait(&vcpu->arch.cpu_run, &wait);
1563 }
1564 
1565 /*
1566  * All the vcpus in this vcore are idle, so wait for a decrementer
1567  * or external interrupt to one of the vcpus.  vc->lock is held.
1568  */
1569 static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1570 {
1571 	DEFINE_WAIT(wait);
1572 
1573 	prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1574 	vc->vcore_state = VCORE_SLEEPING;
1575 	spin_unlock(&vc->lock);
1576 	schedule();
1577 	finish_wait(&vc->wq, &wait);
1578 	spin_lock(&vc->lock);
1579 	vc->vcore_state = VCORE_INACTIVE;
1580 }
1581 
1582 static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1583 {
1584 	int n_ceded;
1585 	struct kvmppc_vcore *vc;
1586 	struct kvm_vcpu *v, *vn;
1587 
1588 	kvm_run->exit_reason = 0;
1589 	vcpu->arch.ret = RESUME_GUEST;
1590 	vcpu->arch.trap = 0;
1591 	kvmppc_update_vpas(vcpu);
1592 
1593 	/*
1594 	 * Synchronize with other threads in this virtual core
1595 	 */
1596 	vc = vcpu->arch.vcore;
1597 	spin_lock(&vc->lock);
1598 	vcpu->arch.ceded = 0;
1599 	vcpu->arch.run_task = current;
1600 	vcpu->arch.kvm_run = kvm_run;
1601 	vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
1602 	vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
1603 	vcpu->arch.busy_preempt = TB_NIL;
1604 	list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1605 	++vc->n_runnable;
1606 
1607 	/*
1608 	 * This happens the first time this is called for a vcpu.
1609 	 * If the vcore is already running, we may be able to start
1610 	 * this thread straight away and have it join in.
1611 	 */
1612 	if (!signal_pending(current)) {
1613 		if (vc->vcore_state == VCORE_RUNNING &&
1614 		    VCORE_EXIT_COUNT(vc) == 0) {
1615 			kvmppc_create_dtl_entry(vcpu, vc);
1616 			kvmppc_start_thread(vcpu);
1617 		} else if (vc->vcore_state == VCORE_SLEEPING) {
1618 			wake_up(&vc->wq);
1619 		}
1620 
1621 	}
1622 
1623 	while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1624 	       !signal_pending(current)) {
1625 		if (vc->vcore_state != VCORE_INACTIVE) {
1626 			spin_unlock(&vc->lock);
1627 			kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1628 			spin_lock(&vc->lock);
1629 			continue;
1630 		}
1631 		list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1632 					 arch.run_list) {
1633 			kvmppc_core_prepare_to_enter(v);
1634 			if (signal_pending(v->arch.run_task)) {
1635 				kvmppc_remove_runnable(vc, v);
1636 				v->stat.signal_exits++;
1637 				v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1638 				v->arch.ret = -EINTR;
1639 				wake_up(&v->arch.cpu_run);
1640 			}
1641 		}
1642 		if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1643 			break;
1644 		vc->runner = vcpu;
1645 		n_ceded = 0;
1646 		list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
1647 			if (!v->arch.pending_exceptions)
1648 				n_ceded += v->arch.ceded;
1649 			else
1650 				v->arch.ceded = 0;
1651 		}
1652 		if (n_ceded == vc->n_runnable)
1653 			kvmppc_vcore_blocked(vc);
1654 		else
1655 			kvmppc_run_core(vc);
1656 		vc->runner = NULL;
1657 	}
1658 
1659 	while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1660 	       (vc->vcore_state == VCORE_RUNNING ||
1661 		vc->vcore_state == VCORE_EXITING)) {
1662 		spin_unlock(&vc->lock);
1663 		kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1664 		spin_lock(&vc->lock);
1665 	}
1666 
1667 	if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1668 		kvmppc_remove_runnable(vc, vcpu);
1669 		vcpu->stat.signal_exits++;
1670 		kvm_run->exit_reason = KVM_EXIT_INTR;
1671 		vcpu->arch.ret = -EINTR;
1672 	}
1673 
1674 	if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
1675 		/* Wake up some vcpu to run the core */
1676 		v = list_first_entry(&vc->runnable_threads,
1677 				     struct kvm_vcpu, arch.run_list);
1678 		wake_up(&v->arch.cpu_run);
1679 	}
1680 
1681 	spin_unlock(&vc->lock);
1682 	return vcpu->arch.ret;
1683 }
1684 
1685 static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
1686 {
1687 	int r;
1688 	int srcu_idx;
1689 
1690 	if (!vcpu->arch.sane) {
1691 		run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1692 		return -EINVAL;
1693 	}
1694 
1695 	kvmppc_core_prepare_to_enter(vcpu);
1696 
1697 	/* No need to go into the guest when all we'll do is come back out */
1698 	if (signal_pending(current)) {
1699 		run->exit_reason = KVM_EXIT_INTR;
1700 		return -EINTR;
1701 	}
1702 
1703 	atomic_inc(&vcpu->kvm->arch.vcpus_running);
1704 	/* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1705 	smp_mb();
1706 
1707 	/* On the first time here, set up HTAB and VRMA or RMA */
1708 	if (!vcpu->kvm->arch.rma_setup_done) {
1709 		r = kvmppc_hv_setup_htab_rma(vcpu);
1710 		if (r)
1711 			goto out;
1712 	}
1713 
1714 	flush_fp_to_thread(current);
1715 	flush_altivec_to_thread(current);
1716 	flush_vsx_to_thread(current);
1717 	vcpu->arch.wqp = &vcpu->arch.vcore->wq;
1718 	vcpu->arch.pgdir = current->mm->pgd;
1719 	vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
1720 
1721 	do {
1722 		r = kvmppc_run_vcpu(run, vcpu);
1723 
1724 		if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
1725 		    !(vcpu->arch.shregs.msr & MSR_PR)) {
1726 			r = kvmppc_pseries_do_hcall(vcpu);
1727 			kvmppc_core_prepare_to_enter(vcpu);
1728 		} else if (r == RESUME_PAGE_FAULT) {
1729 			srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1730 			r = kvmppc_book3s_hv_page_fault(run, vcpu,
1731 				vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
1732 			srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
1733 		}
1734 	} while (r == RESUME_GUEST);
1735 
1736  out:
1737 	vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
1738 	atomic_dec(&vcpu->kvm->arch.vcpus_running);
1739 	return r;
1740 }
1741 
1742 
1743 /* Work out RMLS (real mode limit selector) field value for a given RMA size.
1744    Assumes POWER7 or PPC970. */
1745 static inline int lpcr_rmls(unsigned long rma_size)
1746 {
1747 	switch (rma_size) {
1748 	case 32ul << 20:	/* 32 MB */
1749 		if (cpu_has_feature(CPU_FTR_ARCH_206))
1750 			return 8;	/* only supported on POWER7 */
1751 		return -1;
1752 	case 64ul << 20:	/* 64 MB */
1753 		return 3;
1754 	case 128ul << 20:	/* 128 MB */
1755 		return 7;
1756 	case 256ul << 20:	/* 256 MB */
1757 		return 4;
1758 	case 1ul << 30:		/* 1 GB */
1759 		return 2;
1760 	case 16ul << 30:	/* 16 GB */
1761 		return 1;
1762 	case 256ul << 30:	/* 256 GB */
1763 		return 0;
1764 	default:
1765 		return -1;
1766 	}
1767 }
1768 
1769 static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1770 {
1771 	struct page *page;
1772 	struct kvm_rma_info *ri = vma->vm_file->private_data;
1773 
1774 	if (vmf->pgoff >= kvm_rma_pages)
1775 		return VM_FAULT_SIGBUS;
1776 
1777 	page = pfn_to_page(ri->base_pfn + vmf->pgoff);
1778 	get_page(page);
1779 	vmf->page = page;
1780 	return 0;
1781 }
1782 
1783 static const struct vm_operations_struct kvm_rma_vm_ops = {
1784 	.fault = kvm_rma_fault,
1785 };
1786 
1787 static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
1788 {
1789 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
1790 	vma->vm_ops = &kvm_rma_vm_ops;
1791 	return 0;
1792 }
1793 
1794 static int kvm_rma_release(struct inode *inode, struct file *filp)
1795 {
1796 	struct kvm_rma_info *ri = filp->private_data;
1797 
1798 	kvm_release_rma(ri);
1799 	return 0;
1800 }
1801 
1802 static const struct file_operations kvm_rma_fops = {
1803 	.mmap           = kvm_rma_mmap,
1804 	.release	= kvm_rma_release,
1805 };
1806 
1807 static long kvm_vm_ioctl_allocate_rma(struct kvm *kvm,
1808 				      struct kvm_allocate_rma *ret)
1809 {
1810 	long fd;
1811 	struct kvm_rma_info *ri;
1812 	/*
1813 	 * Only do this on PPC970 in HV mode
1814 	 */
1815 	if (!cpu_has_feature(CPU_FTR_HVMODE) ||
1816 	    !cpu_has_feature(CPU_FTR_ARCH_201))
1817 		return -EINVAL;
1818 
1819 	if (!kvm_rma_pages)
1820 		return -EINVAL;
1821 
1822 	ri = kvm_alloc_rma();
1823 	if (!ri)
1824 		return -ENOMEM;
1825 
1826 	fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR | O_CLOEXEC);
1827 	if (fd < 0)
1828 		kvm_release_rma(ri);
1829 
1830 	ret->rma_size = kvm_rma_pages << PAGE_SHIFT;
1831 	return fd;
1832 }
1833 
1834 static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
1835 				     int linux_psize)
1836 {
1837 	struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
1838 
1839 	if (!def->shift)
1840 		return;
1841 	(*sps)->page_shift = def->shift;
1842 	(*sps)->slb_enc = def->sllp;
1843 	(*sps)->enc[0].page_shift = def->shift;
1844 	/*
1845 	 * Only return base page encoding. We don't want to return
1846 	 * all the supporting pte_enc, because our H_ENTER doesn't
1847 	 * support MPSS yet. Once they do, we can start passing all
1848 	 * support pte_enc here
1849 	 */
1850 	(*sps)->enc[0].pte_enc = def->penc[linux_psize];
1851 	(*sps)++;
1852 }
1853 
1854 static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
1855 					 struct kvm_ppc_smmu_info *info)
1856 {
1857 	struct kvm_ppc_one_seg_page_size *sps;
1858 
1859 	info->flags = KVM_PPC_PAGE_SIZES_REAL;
1860 	if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1861 		info->flags |= KVM_PPC_1T_SEGMENTS;
1862 	info->slb_size = mmu_slb_size;
1863 
1864 	/* We only support these sizes for now, and no muti-size segments */
1865 	sps = &info->sps[0];
1866 	kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
1867 	kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
1868 	kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
1869 
1870 	return 0;
1871 }
1872 
1873 /*
1874  * Get (and clear) the dirty memory log for a memory slot.
1875  */
1876 static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
1877 					 struct kvm_dirty_log *log)
1878 {
1879 	struct kvm_memory_slot *memslot;
1880 	int r;
1881 	unsigned long n;
1882 
1883 	mutex_lock(&kvm->slots_lock);
1884 
1885 	r = -EINVAL;
1886 	if (log->slot >= KVM_USER_MEM_SLOTS)
1887 		goto out;
1888 
1889 	memslot = id_to_memslot(kvm->memslots, log->slot);
1890 	r = -ENOENT;
1891 	if (!memslot->dirty_bitmap)
1892 		goto out;
1893 
1894 	n = kvm_dirty_bitmap_bytes(memslot);
1895 	memset(memslot->dirty_bitmap, 0, n);
1896 
1897 	r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
1898 	if (r)
1899 		goto out;
1900 
1901 	r = -EFAULT;
1902 	if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1903 		goto out;
1904 
1905 	r = 0;
1906 out:
1907 	mutex_unlock(&kvm->slots_lock);
1908 	return r;
1909 }
1910 
1911 static void unpin_slot(struct kvm_memory_slot *memslot)
1912 {
1913 	unsigned long *physp;
1914 	unsigned long j, npages, pfn;
1915 	struct page *page;
1916 
1917 	physp = memslot->arch.slot_phys;
1918 	npages = memslot->npages;
1919 	if (!physp)
1920 		return;
1921 	for (j = 0; j < npages; j++) {
1922 		if (!(physp[j] & KVMPPC_GOT_PAGE))
1923 			continue;
1924 		pfn = physp[j] >> PAGE_SHIFT;
1925 		page = pfn_to_page(pfn);
1926 		SetPageDirty(page);
1927 		put_page(page);
1928 	}
1929 }
1930 
1931 static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
1932 					struct kvm_memory_slot *dont)
1933 {
1934 	if (!dont || free->arch.rmap != dont->arch.rmap) {
1935 		vfree(free->arch.rmap);
1936 		free->arch.rmap = NULL;
1937 	}
1938 	if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
1939 		unpin_slot(free);
1940 		vfree(free->arch.slot_phys);
1941 		free->arch.slot_phys = NULL;
1942 	}
1943 }
1944 
1945 static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
1946 					 unsigned long npages)
1947 {
1948 	slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
1949 	if (!slot->arch.rmap)
1950 		return -ENOMEM;
1951 	slot->arch.slot_phys = NULL;
1952 
1953 	return 0;
1954 }
1955 
1956 static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
1957 					struct kvm_memory_slot *memslot,
1958 					struct kvm_userspace_memory_region *mem)
1959 {
1960 	unsigned long *phys;
1961 
1962 	/* Allocate a slot_phys array if needed */
1963 	phys = memslot->arch.slot_phys;
1964 	if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
1965 		phys = vzalloc(memslot->npages * sizeof(unsigned long));
1966 		if (!phys)
1967 			return -ENOMEM;
1968 		memslot->arch.slot_phys = phys;
1969 	}
1970 
1971 	return 0;
1972 }
1973 
1974 static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
1975 				struct kvm_userspace_memory_region *mem,
1976 				const struct kvm_memory_slot *old)
1977 {
1978 	unsigned long npages = mem->memory_size >> PAGE_SHIFT;
1979 	struct kvm_memory_slot *memslot;
1980 
1981 	if (npages && old->npages) {
1982 		/*
1983 		 * If modifying a memslot, reset all the rmap dirty bits.
1984 		 * If this is a new memslot, we don't need to do anything
1985 		 * since the rmap array starts out as all zeroes,
1986 		 * i.e. no pages are dirty.
1987 		 */
1988 		memslot = id_to_memslot(kvm->memslots, mem->slot);
1989 		kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
1990 	}
1991 }
1992 
1993 /*
1994  * Update LPCR values in kvm->arch and in vcores.
1995  * Caller must hold kvm->lock.
1996  */
1997 void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
1998 {
1999 	long int i;
2000 	u32 cores_done = 0;
2001 
2002 	if ((kvm->arch.lpcr & mask) == lpcr)
2003 		return;
2004 
2005 	kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
2006 
2007 	for (i = 0; i < KVM_MAX_VCORES; ++i) {
2008 		struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2009 		if (!vc)
2010 			continue;
2011 		spin_lock(&vc->lock);
2012 		vc->lpcr = (vc->lpcr & ~mask) | lpcr;
2013 		spin_unlock(&vc->lock);
2014 		if (++cores_done >= kvm->arch.online_vcores)
2015 			break;
2016 	}
2017 }
2018 
2019 static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
2020 {
2021 	return;
2022 }
2023 
2024 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
2025 {
2026 	int err = 0;
2027 	struct kvm *kvm = vcpu->kvm;
2028 	struct kvm_rma_info *ri = NULL;
2029 	unsigned long hva;
2030 	struct kvm_memory_slot *memslot;
2031 	struct vm_area_struct *vma;
2032 	unsigned long lpcr = 0, senc;
2033 	unsigned long lpcr_mask = 0;
2034 	unsigned long psize, porder;
2035 	unsigned long rma_size;
2036 	unsigned long rmls;
2037 	unsigned long *physp;
2038 	unsigned long i, npages;
2039 	int srcu_idx;
2040 
2041 	mutex_lock(&kvm->lock);
2042 	if (kvm->arch.rma_setup_done)
2043 		goto out;	/* another vcpu beat us to it */
2044 
2045 	/* Allocate hashed page table (if not done already) and reset it */
2046 	if (!kvm->arch.hpt_virt) {
2047 		err = kvmppc_alloc_hpt(kvm, NULL);
2048 		if (err) {
2049 			pr_err("KVM: Couldn't alloc HPT\n");
2050 			goto out;
2051 		}
2052 	}
2053 
2054 	/* Look up the memslot for guest physical address 0 */
2055 	srcu_idx = srcu_read_lock(&kvm->srcu);
2056 	memslot = gfn_to_memslot(kvm, 0);
2057 
2058 	/* We must have some memory at 0 by now */
2059 	err = -EINVAL;
2060 	if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
2061 		goto out_srcu;
2062 
2063 	/* Look up the VMA for the start of this memory slot */
2064 	hva = memslot->userspace_addr;
2065 	down_read(&current->mm->mmap_sem);
2066 	vma = find_vma(current->mm, hva);
2067 	if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
2068 		goto up_out;
2069 
2070 	psize = vma_kernel_pagesize(vma);
2071 	porder = __ilog2(psize);
2072 
2073 	/* Is this one of our preallocated RMAs? */
2074 	if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
2075 	    hva == vma->vm_start)
2076 		ri = vma->vm_file->private_data;
2077 
2078 	up_read(&current->mm->mmap_sem);
2079 
2080 	if (!ri) {
2081 		/* On POWER7, use VRMA; on PPC970, give up */
2082 		err = -EPERM;
2083 		if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2084 			pr_err("KVM: CPU requires an RMO\n");
2085 			goto out_srcu;
2086 		}
2087 
2088 		/* We can handle 4k, 64k or 16M pages in the VRMA */
2089 		err = -EINVAL;
2090 		if (!(psize == 0x1000 || psize == 0x10000 ||
2091 		      psize == 0x1000000))
2092 			goto out_srcu;
2093 
2094 		/* Update VRMASD field in the LPCR */
2095 		senc = slb_pgsize_encoding(psize);
2096 		kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
2097 			(VRMA_VSID << SLB_VSID_SHIFT_1T);
2098 		lpcr_mask = LPCR_VRMASD;
2099 		/* the -4 is to account for senc values starting at 0x10 */
2100 		lpcr = senc << (LPCR_VRMASD_SH - 4);
2101 
2102 		/* Create HPTEs in the hash page table for the VRMA */
2103 		kvmppc_map_vrma(vcpu, memslot, porder);
2104 
2105 	} else {
2106 		/* Set up to use an RMO region */
2107 		rma_size = kvm_rma_pages;
2108 		if (rma_size > memslot->npages)
2109 			rma_size = memslot->npages;
2110 		rma_size <<= PAGE_SHIFT;
2111 		rmls = lpcr_rmls(rma_size);
2112 		err = -EINVAL;
2113 		if ((long)rmls < 0) {
2114 			pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
2115 			goto out_srcu;
2116 		}
2117 		atomic_inc(&ri->use_count);
2118 		kvm->arch.rma = ri;
2119 
2120 		/* Update LPCR and RMOR */
2121 		if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2122 			/* PPC970; insert RMLS value (split field) in HID4 */
2123 			lpcr_mask = (1ul << HID4_RMLS0_SH) |
2124 				(3ul << HID4_RMLS2_SH) | HID4_RMOR;
2125 			lpcr = ((rmls >> 2) << HID4_RMLS0_SH) |
2126 				((rmls & 3) << HID4_RMLS2_SH);
2127 			/* RMOR is also in HID4 */
2128 			lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
2129 				<< HID4_RMOR_SH;
2130 		} else {
2131 			/* POWER7 */
2132 			lpcr_mask = LPCR_VPM0 | LPCR_VRMA_L | LPCR_RMLS;
2133 			lpcr = rmls << LPCR_RMLS_SH;
2134 			kvm->arch.rmor = ri->base_pfn << PAGE_SHIFT;
2135 		}
2136 		pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
2137 			ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
2138 
2139 		/* Initialize phys addrs of pages in RMO */
2140 		npages = kvm_rma_pages;
2141 		porder = __ilog2(npages);
2142 		physp = memslot->arch.slot_phys;
2143 		if (physp) {
2144 			if (npages > memslot->npages)
2145 				npages = memslot->npages;
2146 			spin_lock(&kvm->arch.slot_phys_lock);
2147 			for (i = 0; i < npages; ++i)
2148 				physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
2149 					porder;
2150 			spin_unlock(&kvm->arch.slot_phys_lock);
2151 		}
2152 	}
2153 
2154 	kvmppc_update_lpcr(kvm, lpcr, lpcr_mask);
2155 
2156 	/* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
2157 	smp_wmb();
2158 	kvm->arch.rma_setup_done = 1;
2159 	err = 0;
2160  out_srcu:
2161 	srcu_read_unlock(&kvm->srcu, srcu_idx);
2162  out:
2163 	mutex_unlock(&kvm->lock);
2164 	return err;
2165 
2166  up_out:
2167 	up_read(&current->mm->mmap_sem);
2168 	goto out_srcu;
2169 }
2170 
2171 static int kvmppc_core_init_vm_hv(struct kvm *kvm)
2172 {
2173 	unsigned long lpcr, lpid;
2174 
2175 	/* Allocate the guest's logical partition ID */
2176 
2177 	lpid = kvmppc_alloc_lpid();
2178 	if ((long)lpid < 0)
2179 		return -ENOMEM;
2180 	kvm->arch.lpid = lpid;
2181 
2182 	/*
2183 	 * Since we don't flush the TLB when tearing down a VM,
2184 	 * and this lpid might have previously been used,
2185 	 * make sure we flush on each core before running the new VM.
2186 	 */
2187 	cpumask_setall(&kvm->arch.need_tlb_flush);
2188 
2189 	kvm->arch.rma = NULL;
2190 
2191 	kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
2192 
2193 	if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2194 		/* PPC970; HID4 is effectively the LPCR */
2195 		kvm->arch.host_lpid = 0;
2196 		kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
2197 		lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
2198 		lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
2199 			((lpid & 0xf) << HID4_LPID5_SH);
2200 	} else {
2201 		/* POWER7; init LPCR for virtual RMA mode */
2202 		kvm->arch.host_lpid = mfspr(SPRN_LPID);
2203 		kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
2204 		lpcr &= LPCR_PECE | LPCR_LPES;
2205 		lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
2206 			LPCR_VPM0 | LPCR_VPM1;
2207 		kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
2208 			(VRMA_VSID << SLB_VSID_SHIFT_1T);
2209 		/* On POWER8 turn on online bit to enable PURR/SPURR */
2210 		if (cpu_has_feature(CPU_FTR_ARCH_207S))
2211 			lpcr |= LPCR_ONL;
2212 	}
2213 	kvm->arch.lpcr = lpcr;
2214 
2215 	kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
2216 	spin_lock_init(&kvm->arch.slot_phys_lock);
2217 
2218 	/*
2219 	 * Don't allow secondary CPU threads to come online
2220 	 * while any KVM VMs exist.
2221 	 */
2222 	inhibit_secondary_onlining();
2223 
2224 	return 0;
2225 }
2226 
2227 static void kvmppc_free_vcores(struct kvm *kvm)
2228 {
2229 	long int i;
2230 
2231 	for (i = 0; i < KVM_MAX_VCORES; ++i)
2232 		kfree(kvm->arch.vcores[i]);
2233 	kvm->arch.online_vcores = 0;
2234 }
2235 
2236 static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
2237 {
2238 	uninhibit_secondary_onlining();
2239 
2240 	kvmppc_free_vcores(kvm);
2241 	if (kvm->arch.rma) {
2242 		kvm_release_rma(kvm->arch.rma);
2243 		kvm->arch.rma = NULL;
2244 	}
2245 
2246 	kvmppc_free_hpt(kvm);
2247 }
2248 
2249 /* We don't need to emulate any privileged instructions or dcbz */
2250 static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
2251 				     unsigned int inst, int *advance)
2252 {
2253 	return EMULATE_FAIL;
2254 }
2255 
2256 static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
2257 					ulong spr_val)
2258 {
2259 	return EMULATE_FAIL;
2260 }
2261 
2262 static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
2263 					ulong *spr_val)
2264 {
2265 	return EMULATE_FAIL;
2266 }
2267 
2268 static int kvmppc_core_check_processor_compat_hv(void)
2269 {
2270 	if (!cpu_has_feature(CPU_FTR_HVMODE))
2271 		return -EIO;
2272 	return 0;
2273 }
2274 
2275 static long kvm_arch_vm_ioctl_hv(struct file *filp,
2276 				 unsigned int ioctl, unsigned long arg)
2277 {
2278 	struct kvm *kvm __maybe_unused = filp->private_data;
2279 	void __user *argp = (void __user *)arg;
2280 	long r;
2281 
2282 	switch (ioctl) {
2283 
2284 	case KVM_ALLOCATE_RMA: {
2285 		struct kvm_allocate_rma rma;
2286 		struct kvm *kvm = filp->private_data;
2287 
2288 		r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
2289 		if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
2290 			r = -EFAULT;
2291 		break;
2292 	}
2293 
2294 	case KVM_PPC_ALLOCATE_HTAB: {
2295 		u32 htab_order;
2296 
2297 		r = -EFAULT;
2298 		if (get_user(htab_order, (u32 __user *)argp))
2299 			break;
2300 		r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
2301 		if (r)
2302 			break;
2303 		r = -EFAULT;
2304 		if (put_user(htab_order, (u32 __user *)argp))
2305 			break;
2306 		r = 0;
2307 		break;
2308 	}
2309 
2310 	case KVM_PPC_GET_HTAB_FD: {
2311 		struct kvm_get_htab_fd ghf;
2312 
2313 		r = -EFAULT;
2314 		if (copy_from_user(&ghf, argp, sizeof(ghf)))
2315 			break;
2316 		r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
2317 		break;
2318 	}
2319 
2320 	default:
2321 		r = -ENOTTY;
2322 	}
2323 
2324 	return r;
2325 }
2326 
2327 static struct kvmppc_ops kvm_ops_hv = {
2328 	.get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
2329 	.set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
2330 	.get_one_reg = kvmppc_get_one_reg_hv,
2331 	.set_one_reg = kvmppc_set_one_reg_hv,
2332 	.vcpu_load   = kvmppc_core_vcpu_load_hv,
2333 	.vcpu_put    = kvmppc_core_vcpu_put_hv,
2334 	.set_msr     = kvmppc_set_msr_hv,
2335 	.vcpu_run    = kvmppc_vcpu_run_hv,
2336 	.vcpu_create = kvmppc_core_vcpu_create_hv,
2337 	.vcpu_free   = kvmppc_core_vcpu_free_hv,
2338 	.check_requests = kvmppc_core_check_requests_hv,
2339 	.get_dirty_log  = kvm_vm_ioctl_get_dirty_log_hv,
2340 	.flush_memslot  = kvmppc_core_flush_memslot_hv,
2341 	.prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
2342 	.commit_memory_region  = kvmppc_core_commit_memory_region_hv,
2343 	.unmap_hva = kvm_unmap_hva_hv,
2344 	.unmap_hva_range = kvm_unmap_hva_range_hv,
2345 	.age_hva  = kvm_age_hva_hv,
2346 	.test_age_hva = kvm_test_age_hva_hv,
2347 	.set_spte_hva = kvm_set_spte_hva_hv,
2348 	.mmu_destroy  = kvmppc_mmu_destroy_hv,
2349 	.free_memslot = kvmppc_core_free_memslot_hv,
2350 	.create_memslot = kvmppc_core_create_memslot_hv,
2351 	.init_vm =  kvmppc_core_init_vm_hv,
2352 	.destroy_vm = kvmppc_core_destroy_vm_hv,
2353 	.get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
2354 	.emulate_op = kvmppc_core_emulate_op_hv,
2355 	.emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
2356 	.emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
2357 	.fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
2358 	.arch_vm_ioctl  = kvm_arch_vm_ioctl_hv,
2359 };
2360 
2361 static int kvmppc_book3s_init_hv(void)
2362 {
2363 	int r;
2364 	/*
2365 	 * FIXME!! Do we need to check on all cpus ?
2366 	 */
2367 	r = kvmppc_core_check_processor_compat_hv();
2368 	if (r < 0)
2369 		return r;
2370 
2371 	kvm_ops_hv.owner = THIS_MODULE;
2372 	kvmppc_hv_ops = &kvm_ops_hv;
2373 
2374 	r = kvmppc_mmu_hv_init();
2375 	return r;
2376 }
2377 
2378 static void kvmppc_book3s_exit_hv(void)
2379 {
2380 	kvmppc_hv_ops = NULL;
2381 }
2382 
2383 module_init(kvmppc_book3s_init_hv);
2384 module_exit(kvmppc_book3s_exit_hv);
2385 MODULE_LICENSE("GPL");
2386 MODULE_ALIAS_MISCDEV(KVM_MINOR);
2387 MODULE_ALIAS("devname:kvm");
2388