book3s_hv.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  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. #include <linux/kvm_host.h>
  21. #include <linux/err.h>
  22. #include <linux/slab.h>
  23. #include <linux/preempt.h>
  24. #include <linux/sched.h>
  25. #include <linux/delay.h>
  26. #include <linux/export.h>
  27. #include <linux/fs.h>
  28. #include <linux/anon_inodes.h>
  29. #include <linux/cpumask.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/page-flags.h>
  32. #include <asm/reg.h>
  33. #include <asm/cputable.h>
  34. #include <asm/cacheflush.h>
  35. #include <asm/tlbflush.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/io.h>
  38. #include <asm/kvm_ppc.h>
  39. #include <asm/kvm_book3s.h>
  40. #include <asm/mmu_context.h>
  41. #include <asm/lppaca.h>
  42. #include <asm/processor.h>
  43. #include <asm/cputhreads.h>
  44. #include <asm/page.h>
  45. #include <linux/gfp.h>
  46. #include <linux/sched.h>
  47. #include <linux/vmalloc.h>
  48. #include <linux/highmem.h>
  49. /*
  50. * For now, limit memory to 64GB and require it to be large pages.
  51. * This value is chosen because it makes the ram_pginfo array be
  52. * 64kB in size, which is about as large as we want to be trying
  53. * to allocate with kmalloc.
  54. */
  55. #define MAX_MEM_ORDER 36
  56. #define LARGE_PAGE_ORDER 24 /* 16MB pages */
  57. /* #define EXIT_DEBUG */
  58. /* #define EXIT_DEBUG_SIMPLE */
  59. /* #define EXIT_DEBUG_INT */
  60. static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
  61. void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  62. {
  63. local_paca->kvm_hstate.kvm_vcpu = vcpu;
  64. local_paca->kvm_hstate.kvm_vcore = vcpu->arch.vcore;
  65. }
  66. void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
  67. {
  68. }
  69. void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
  70. {
  71. vcpu->arch.shregs.msr = msr;
  72. kvmppc_end_cede(vcpu);
  73. }
  74. void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
  75. {
  76. vcpu->arch.pvr = pvr;
  77. }
  78. void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
  79. {
  80. int r;
  81. pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
  82. pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
  83. vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
  84. for (r = 0; r < 16; ++r)
  85. pr_err("r%2d = %.16lx r%d = %.16lx\n",
  86. r, kvmppc_get_gpr(vcpu, r),
  87. r+16, kvmppc_get_gpr(vcpu, r+16));
  88. pr_err("ctr = %.16lx lr = %.16lx\n",
  89. vcpu->arch.ctr, vcpu->arch.lr);
  90. pr_err("srr0 = %.16llx srr1 = %.16llx\n",
  91. vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
  92. pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
  93. vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
  94. pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
  95. vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
  96. pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
  97. vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
  98. pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
  99. pr_err("fault dar = %.16lx dsisr = %.8x\n",
  100. vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
  101. pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
  102. for (r = 0; r < vcpu->arch.slb_max; ++r)
  103. pr_err(" ESID = %.16llx VSID = %.16llx\n",
  104. vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
  105. pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
  106. vcpu->kvm->arch.lpcr, vcpu->kvm->arch.sdr1,
  107. vcpu->arch.last_inst);
  108. }
  109. struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
  110. {
  111. int r;
  112. struct kvm_vcpu *v, *ret = NULL;
  113. mutex_lock(&kvm->lock);
  114. kvm_for_each_vcpu(r, v, kvm) {
  115. if (v->vcpu_id == id) {
  116. ret = v;
  117. break;
  118. }
  119. }
  120. mutex_unlock(&kvm->lock);
  121. return ret;
  122. }
  123. static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
  124. {
  125. vpa->shared_proc = 1;
  126. vpa->yield_count = 1;
  127. }
  128. static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
  129. unsigned long flags,
  130. unsigned long vcpuid, unsigned long vpa)
  131. {
  132. struct kvm *kvm = vcpu->kvm;
  133. unsigned long pg_index, ra, len;
  134. unsigned long pg_offset;
  135. void *va;
  136. struct kvm_vcpu *tvcpu;
  137. tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
  138. if (!tvcpu)
  139. return H_PARAMETER;
  140. flags >>= 63 - 18;
  141. flags &= 7;
  142. if (flags == 0 || flags == 4)
  143. return H_PARAMETER;
  144. if (flags < 4) {
  145. if (vpa & 0x7f)
  146. return H_PARAMETER;
  147. /* registering new area; convert logical addr to real */
  148. pg_index = vpa >> kvm->arch.ram_porder;
  149. pg_offset = vpa & (kvm->arch.ram_psize - 1);
  150. if (pg_index >= kvm->arch.ram_npages)
  151. return H_PARAMETER;
  152. if (kvm->arch.ram_pginfo[pg_index].pfn == 0)
  153. return H_PARAMETER;
  154. ra = kvm->arch.ram_pginfo[pg_index].pfn << PAGE_SHIFT;
  155. ra |= pg_offset;
  156. va = __va(ra);
  157. if (flags <= 1)
  158. len = *(unsigned short *)(va + 4);
  159. else
  160. len = *(unsigned int *)(va + 4);
  161. if (pg_offset + len > kvm->arch.ram_psize)
  162. return H_PARAMETER;
  163. switch (flags) {
  164. case 1: /* register VPA */
  165. if (len < 640)
  166. return H_PARAMETER;
  167. tvcpu->arch.vpa = va;
  168. init_vpa(vcpu, va);
  169. break;
  170. case 2: /* register DTL */
  171. if (len < 48)
  172. return H_PARAMETER;
  173. if (!tvcpu->arch.vpa)
  174. return H_RESOURCE;
  175. len -= len % 48;
  176. tvcpu->arch.dtl = va;
  177. tvcpu->arch.dtl_end = va + len;
  178. break;
  179. case 3: /* register SLB shadow buffer */
  180. if (len < 8)
  181. return H_PARAMETER;
  182. if (!tvcpu->arch.vpa)
  183. return H_RESOURCE;
  184. tvcpu->arch.slb_shadow = va;
  185. len = (len - 16) / 16;
  186. tvcpu->arch.slb_shadow = va;
  187. break;
  188. }
  189. } else {
  190. switch (flags) {
  191. case 5: /* unregister VPA */
  192. if (tvcpu->arch.slb_shadow || tvcpu->arch.dtl)
  193. return H_RESOURCE;
  194. tvcpu->arch.vpa = NULL;
  195. break;
  196. case 6: /* unregister DTL */
  197. tvcpu->arch.dtl = NULL;
  198. break;
  199. case 7: /* unregister SLB shadow buffer */
  200. tvcpu->arch.slb_shadow = NULL;
  201. break;
  202. }
  203. }
  204. return H_SUCCESS;
  205. }
  206. int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
  207. {
  208. unsigned long req = kvmppc_get_gpr(vcpu, 3);
  209. unsigned long target, ret = H_SUCCESS;
  210. struct kvm_vcpu *tvcpu;
  211. switch (req) {
  212. case H_CEDE:
  213. break;
  214. case H_PROD:
  215. target = kvmppc_get_gpr(vcpu, 4);
  216. tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
  217. if (!tvcpu) {
  218. ret = H_PARAMETER;
  219. break;
  220. }
  221. tvcpu->arch.prodded = 1;
  222. smp_mb();
  223. if (vcpu->arch.ceded) {
  224. if (waitqueue_active(&vcpu->wq)) {
  225. wake_up_interruptible(&vcpu->wq);
  226. vcpu->stat.halt_wakeup++;
  227. }
  228. }
  229. break;
  230. case H_CONFER:
  231. break;
  232. case H_REGISTER_VPA:
  233. ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
  234. kvmppc_get_gpr(vcpu, 5),
  235. kvmppc_get_gpr(vcpu, 6));
  236. break;
  237. default:
  238. return RESUME_HOST;
  239. }
  240. kvmppc_set_gpr(vcpu, 3, ret);
  241. vcpu->arch.hcall_needed = 0;
  242. return RESUME_GUEST;
  243. }
  244. static int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
  245. struct task_struct *tsk)
  246. {
  247. int r = RESUME_HOST;
  248. vcpu->stat.sum_exits++;
  249. run->exit_reason = KVM_EXIT_UNKNOWN;
  250. run->ready_for_interrupt_injection = 1;
  251. switch (vcpu->arch.trap) {
  252. /* We're good on these - the host merely wanted to get our attention */
  253. case BOOK3S_INTERRUPT_HV_DECREMENTER:
  254. vcpu->stat.dec_exits++;
  255. r = RESUME_GUEST;
  256. break;
  257. case BOOK3S_INTERRUPT_EXTERNAL:
  258. vcpu->stat.ext_intr_exits++;
  259. r = RESUME_GUEST;
  260. break;
  261. case BOOK3S_INTERRUPT_PERFMON:
  262. r = RESUME_GUEST;
  263. break;
  264. case BOOK3S_INTERRUPT_PROGRAM:
  265. {
  266. ulong flags;
  267. /*
  268. * Normally program interrupts are delivered directly
  269. * to the guest by the hardware, but we can get here
  270. * as a result of a hypervisor emulation interrupt
  271. * (e40) getting turned into a 700 by BML RTAS.
  272. */
  273. flags = vcpu->arch.shregs.msr & 0x1f0000ull;
  274. kvmppc_core_queue_program(vcpu, flags);
  275. r = RESUME_GUEST;
  276. break;
  277. }
  278. case BOOK3S_INTERRUPT_SYSCALL:
  279. {
  280. /* hcall - punt to userspace */
  281. int i;
  282. if (vcpu->arch.shregs.msr & MSR_PR) {
  283. /* sc 1 from userspace - reflect to guest syscall */
  284. kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_SYSCALL);
  285. r = RESUME_GUEST;
  286. break;
  287. }
  288. run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
  289. for (i = 0; i < 9; ++i)
  290. run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
  291. run->exit_reason = KVM_EXIT_PAPR_HCALL;
  292. vcpu->arch.hcall_needed = 1;
  293. r = RESUME_HOST;
  294. break;
  295. }
  296. /*
  297. * We get these next two if the guest does a bad real-mode access,
  298. * as we have enabled VRMA (virtualized real mode area) mode in the
  299. * LPCR. We just generate an appropriate DSI/ISI to the guest.
  300. */
  301. case BOOK3S_INTERRUPT_H_DATA_STORAGE:
  302. vcpu->arch.shregs.dsisr = vcpu->arch.fault_dsisr;
  303. vcpu->arch.shregs.dar = vcpu->arch.fault_dar;
  304. kvmppc_inject_interrupt(vcpu, BOOK3S_INTERRUPT_DATA_STORAGE, 0);
  305. r = RESUME_GUEST;
  306. break;
  307. case BOOK3S_INTERRUPT_H_INST_STORAGE:
  308. kvmppc_inject_interrupt(vcpu, BOOK3S_INTERRUPT_INST_STORAGE,
  309. 0x08000000);
  310. r = RESUME_GUEST;
  311. break;
  312. /*
  313. * This occurs if the guest executes an illegal instruction.
  314. * We just generate a program interrupt to the guest, since
  315. * we don't emulate any guest instructions at this stage.
  316. */
  317. case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
  318. kvmppc_core_queue_program(vcpu, 0x80000);
  319. r = RESUME_GUEST;
  320. break;
  321. default:
  322. kvmppc_dump_regs(vcpu);
  323. printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
  324. vcpu->arch.trap, kvmppc_get_pc(vcpu),
  325. vcpu->arch.shregs.msr);
  326. r = RESUME_HOST;
  327. BUG();
  328. break;
  329. }
  330. return r;
  331. }
  332. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  333. struct kvm_sregs *sregs)
  334. {
  335. int i;
  336. sregs->pvr = vcpu->arch.pvr;
  337. memset(sregs, 0, sizeof(struct kvm_sregs));
  338. for (i = 0; i < vcpu->arch.slb_max; i++) {
  339. sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
  340. sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
  341. }
  342. return 0;
  343. }
  344. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  345. struct kvm_sregs *sregs)
  346. {
  347. int i, j;
  348. kvmppc_set_pvr(vcpu, sregs->pvr);
  349. j = 0;
  350. for (i = 0; i < vcpu->arch.slb_nr; i++) {
  351. if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
  352. vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
  353. vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
  354. ++j;
  355. }
  356. }
  357. vcpu->arch.slb_max = j;
  358. return 0;
  359. }
  360. int kvmppc_core_check_processor_compat(void)
  361. {
  362. if (cpu_has_feature(CPU_FTR_HVMODE))
  363. return 0;
  364. return -EIO;
  365. }
  366. struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
  367. {
  368. struct kvm_vcpu *vcpu;
  369. int err = -EINVAL;
  370. int core;
  371. struct kvmppc_vcore *vcore;
  372. core = id / threads_per_core;
  373. if (core >= KVM_MAX_VCORES)
  374. goto out;
  375. err = -ENOMEM;
  376. vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
  377. if (!vcpu)
  378. goto out;
  379. err = kvm_vcpu_init(vcpu, kvm, id);
  380. if (err)
  381. goto free_vcpu;
  382. vcpu->arch.shared = &vcpu->arch.shregs;
  383. vcpu->arch.last_cpu = -1;
  384. vcpu->arch.mmcr[0] = MMCR0_FC;
  385. vcpu->arch.ctrl = CTRL_RUNLATCH;
  386. /* default to host PVR, since we can't spoof it */
  387. vcpu->arch.pvr = mfspr(SPRN_PVR);
  388. kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
  389. kvmppc_mmu_book3s_hv_init(vcpu);
  390. /*
  391. * We consider the vcpu stopped until we see the first run ioctl for it.
  392. */
  393. vcpu->arch.state = KVMPPC_VCPU_STOPPED;
  394. init_waitqueue_head(&vcpu->arch.cpu_run);
  395. mutex_lock(&kvm->lock);
  396. vcore = kvm->arch.vcores[core];
  397. if (!vcore) {
  398. vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
  399. if (vcore) {
  400. INIT_LIST_HEAD(&vcore->runnable_threads);
  401. spin_lock_init(&vcore->lock);
  402. init_waitqueue_head(&vcore->wq);
  403. }
  404. kvm->arch.vcores[core] = vcore;
  405. }
  406. mutex_unlock(&kvm->lock);
  407. if (!vcore)
  408. goto free_vcpu;
  409. spin_lock(&vcore->lock);
  410. ++vcore->num_threads;
  411. spin_unlock(&vcore->lock);
  412. vcpu->arch.vcore = vcore;
  413. vcpu->arch.cpu_type = KVM_CPU_3S_64;
  414. kvmppc_sanity_check(vcpu);
  415. return vcpu;
  416. free_vcpu:
  417. kfree(vcpu);
  418. out:
  419. return ERR_PTR(err);
  420. }
  421. void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
  422. {
  423. kvm_vcpu_uninit(vcpu);
  424. kfree(vcpu);
  425. }
  426. static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
  427. {
  428. unsigned long dec_nsec, now;
  429. now = get_tb();
  430. if (now > vcpu->arch.dec_expires) {
  431. /* decrementer has already gone negative */
  432. kvmppc_core_queue_dec(vcpu);
  433. kvmppc_core_deliver_interrupts(vcpu);
  434. return;
  435. }
  436. dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
  437. / tb_ticks_per_sec;
  438. hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
  439. HRTIMER_MODE_REL);
  440. vcpu->arch.timer_running = 1;
  441. }
  442. static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
  443. {
  444. vcpu->arch.ceded = 0;
  445. if (vcpu->arch.timer_running) {
  446. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  447. vcpu->arch.timer_running = 0;
  448. }
  449. }
  450. extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
  451. extern void xics_wake_cpu(int cpu);
  452. static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
  453. struct kvm_vcpu *vcpu)
  454. {
  455. struct kvm_vcpu *v;
  456. if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
  457. return;
  458. vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
  459. --vc->n_runnable;
  460. ++vc->n_busy;
  461. /* decrement the physical thread id of each following vcpu */
  462. v = vcpu;
  463. list_for_each_entry_continue(v, &vc->runnable_threads, arch.run_list)
  464. --v->arch.ptid;
  465. list_del(&vcpu->arch.run_list);
  466. }
  467. static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
  468. {
  469. int cpu;
  470. struct paca_struct *tpaca;
  471. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  472. if (vcpu->arch.timer_running) {
  473. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  474. vcpu->arch.timer_running = 0;
  475. }
  476. cpu = vc->pcpu + vcpu->arch.ptid;
  477. tpaca = &paca[cpu];
  478. tpaca->kvm_hstate.kvm_vcpu = vcpu;
  479. tpaca->kvm_hstate.kvm_vcore = vc;
  480. tpaca->kvm_hstate.napping = 0;
  481. vcpu->cpu = vc->pcpu;
  482. smp_wmb();
  483. #ifdef CONFIG_PPC_ICP_NATIVE
  484. if (vcpu->arch.ptid) {
  485. tpaca->cpu_start = 0x80;
  486. wmb();
  487. xics_wake_cpu(cpu);
  488. ++vc->n_woken;
  489. }
  490. #endif
  491. }
  492. static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
  493. {
  494. int i;
  495. HMT_low();
  496. i = 0;
  497. while (vc->nap_count < vc->n_woken) {
  498. if (++i >= 1000000) {
  499. pr_err("kvmppc_wait_for_nap timeout %d %d\n",
  500. vc->nap_count, vc->n_woken);
  501. break;
  502. }
  503. cpu_relax();
  504. }
  505. HMT_medium();
  506. }
  507. /*
  508. * Check that we are on thread 0 and that any other threads in
  509. * this core are off-line.
  510. */
  511. static int on_primary_thread(void)
  512. {
  513. int cpu = smp_processor_id();
  514. int thr = cpu_thread_in_core(cpu);
  515. if (thr)
  516. return 0;
  517. while (++thr < threads_per_core)
  518. if (cpu_online(cpu + thr))
  519. return 0;
  520. return 1;
  521. }
  522. /*
  523. * Run a set of guest threads on a physical core.
  524. * Called with vc->lock held.
  525. */
  526. static int kvmppc_run_core(struct kvmppc_vcore *vc)
  527. {
  528. struct kvm_vcpu *vcpu, *vcpu0, *vnext;
  529. long ret;
  530. u64 now;
  531. int ptid;
  532. /* don't start if any threads have a signal pending */
  533. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  534. if (signal_pending(vcpu->arch.run_task))
  535. return 0;
  536. /*
  537. * Make sure we are running on thread 0, and that
  538. * secondary threads are offline.
  539. * XXX we should also block attempts to bring any
  540. * secondary threads online.
  541. */
  542. if (threads_per_core > 1 && !on_primary_thread()) {
  543. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  544. vcpu->arch.ret = -EBUSY;
  545. goto out;
  546. }
  547. /*
  548. * Assign physical thread IDs, first to non-ceded vcpus
  549. * and then to ceded ones.
  550. */
  551. ptid = 0;
  552. vcpu0 = NULL;
  553. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
  554. if (!vcpu->arch.ceded) {
  555. if (!ptid)
  556. vcpu0 = vcpu;
  557. vcpu->arch.ptid = ptid++;
  558. }
  559. }
  560. if (!vcpu0)
  561. return 0; /* nothing to run */
  562. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  563. if (vcpu->arch.ceded)
  564. vcpu->arch.ptid = ptid++;
  565. vc->n_woken = 0;
  566. vc->nap_count = 0;
  567. vc->entry_exit_count = 0;
  568. vc->vcore_state = VCORE_RUNNING;
  569. vc->in_guest = 0;
  570. vc->pcpu = smp_processor_id();
  571. vc->napping_threads = 0;
  572. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  573. kvmppc_start_thread(vcpu);
  574. preempt_disable();
  575. spin_unlock(&vc->lock);
  576. kvm_guest_enter();
  577. __kvmppc_vcore_entry(NULL, vcpu0);
  578. spin_lock(&vc->lock);
  579. /* disable sending of IPIs on virtual external irqs */
  580. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  581. vcpu->cpu = -1;
  582. /* wait for secondary threads to finish writing their state to memory */
  583. if (vc->nap_count < vc->n_woken)
  584. kvmppc_wait_for_nap(vc);
  585. /* prevent other vcpu threads from doing kvmppc_start_thread() now */
  586. vc->vcore_state = VCORE_EXITING;
  587. spin_unlock(&vc->lock);
  588. /* make sure updates to secondary vcpu structs are visible now */
  589. smp_mb();
  590. kvm_guest_exit();
  591. preempt_enable();
  592. kvm_resched(vcpu);
  593. now = get_tb();
  594. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
  595. /* cancel pending dec exception if dec is positive */
  596. if (now < vcpu->arch.dec_expires &&
  597. kvmppc_core_pending_dec(vcpu))
  598. kvmppc_core_dequeue_dec(vcpu);
  599. ret = RESUME_GUEST;
  600. if (vcpu->arch.trap)
  601. ret = kvmppc_handle_exit(vcpu->arch.kvm_run, vcpu,
  602. vcpu->arch.run_task);
  603. vcpu->arch.ret = ret;
  604. vcpu->arch.trap = 0;
  605. if (vcpu->arch.ceded) {
  606. if (ret != RESUME_GUEST)
  607. kvmppc_end_cede(vcpu);
  608. else
  609. kvmppc_set_timer(vcpu);
  610. }
  611. }
  612. spin_lock(&vc->lock);
  613. out:
  614. vc->vcore_state = VCORE_INACTIVE;
  615. list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
  616. arch.run_list) {
  617. if (vcpu->arch.ret != RESUME_GUEST) {
  618. kvmppc_remove_runnable(vc, vcpu);
  619. wake_up(&vcpu->arch.cpu_run);
  620. }
  621. }
  622. return 1;
  623. }
  624. /*
  625. * Wait for some other vcpu thread to execute us, and
  626. * wake us up when we need to handle something in the host.
  627. */
  628. static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
  629. {
  630. DEFINE_WAIT(wait);
  631. prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
  632. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
  633. schedule();
  634. finish_wait(&vcpu->arch.cpu_run, &wait);
  635. }
  636. /*
  637. * All the vcpus in this vcore are idle, so wait for a decrementer
  638. * or external interrupt to one of the vcpus. vc->lock is held.
  639. */
  640. static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
  641. {
  642. DEFINE_WAIT(wait);
  643. struct kvm_vcpu *v;
  644. int all_idle = 1;
  645. prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
  646. vc->vcore_state = VCORE_SLEEPING;
  647. spin_unlock(&vc->lock);
  648. list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
  649. if (!v->arch.ceded || v->arch.pending_exceptions) {
  650. all_idle = 0;
  651. break;
  652. }
  653. }
  654. if (all_idle)
  655. schedule();
  656. finish_wait(&vc->wq, &wait);
  657. spin_lock(&vc->lock);
  658. vc->vcore_state = VCORE_INACTIVE;
  659. }
  660. static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  661. {
  662. int n_ceded;
  663. int prev_state;
  664. struct kvmppc_vcore *vc;
  665. struct kvm_vcpu *v, *vn;
  666. kvm_run->exit_reason = 0;
  667. vcpu->arch.ret = RESUME_GUEST;
  668. vcpu->arch.trap = 0;
  669. /*
  670. * Synchronize with other threads in this virtual core
  671. */
  672. vc = vcpu->arch.vcore;
  673. spin_lock(&vc->lock);
  674. vcpu->arch.ceded = 0;
  675. vcpu->arch.run_task = current;
  676. vcpu->arch.kvm_run = kvm_run;
  677. prev_state = vcpu->arch.state;
  678. vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
  679. list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
  680. ++vc->n_runnable;
  681. /*
  682. * This happens the first time this is called for a vcpu.
  683. * If the vcore is already running, we may be able to start
  684. * this thread straight away and have it join in.
  685. */
  686. if (prev_state == KVMPPC_VCPU_STOPPED) {
  687. if (vc->vcore_state == VCORE_RUNNING &&
  688. VCORE_EXIT_COUNT(vc) == 0) {
  689. vcpu->arch.ptid = vc->n_runnable - 1;
  690. kvmppc_start_thread(vcpu);
  691. }
  692. } else if (prev_state == KVMPPC_VCPU_BUSY_IN_HOST)
  693. --vc->n_busy;
  694. while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
  695. !signal_pending(current)) {
  696. if (vc->n_busy || vc->vcore_state != VCORE_INACTIVE) {
  697. spin_unlock(&vc->lock);
  698. kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
  699. spin_lock(&vc->lock);
  700. continue;
  701. }
  702. n_ceded = 0;
  703. list_for_each_entry(v, &vc->runnable_threads, arch.run_list)
  704. n_ceded += v->arch.ceded;
  705. if (n_ceded == vc->n_runnable)
  706. kvmppc_vcore_blocked(vc);
  707. else
  708. kvmppc_run_core(vc);
  709. list_for_each_entry_safe(v, vn, &vc->runnable_threads,
  710. arch.run_list) {
  711. kvmppc_core_deliver_interrupts(v);
  712. if (signal_pending(v->arch.run_task)) {
  713. kvmppc_remove_runnable(vc, v);
  714. v->stat.signal_exits++;
  715. v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
  716. v->arch.ret = -EINTR;
  717. wake_up(&v->arch.cpu_run);
  718. }
  719. }
  720. }
  721. if (signal_pending(current)) {
  722. if (vc->vcore_state == VCORE_RUNNING ||
  723. vc->vcore_state == VCORE_EXITING) {
  724. spin_unlock(&vc->lock);
  725. kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
  726. spin_lock(&vc->lock);
  727. }
  728. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
  729. kvmppc_remove_runnable(vc, vcpu);
  730. vcpu->stat.signal_exits++;
  731. kvm_run->exit_reason = KVM_EXIT_INTR;
  732. vcpu->arch.ret = -EINTR;
  733. }
  734. }
  735. spin_unlock(&vc->lock);
  736. return vcpu->arch.ret;
  737. }
  738. int kvmppc_vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu)
  739. {
  740. int r;
  741. if (!vcpu->arch.sane) {
  742. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  743. return -EINVAL;
  744. }
  745. /* No need to go into the guest when all we'll do is come back out */
  746. if (signal_pending(current)) {
  747. run->exit_reason = KVM_EXIT_INTR;
  748. return -EINTR;
  749. }
  750. /* On PPC970, check that we have an RMA region */
  751. if (!vcpu->kvm->arch.rma && cpu_has_feature(CPU_FTR_ARCH_201))
  752. return -EPERM;
  753. flush_fp_to_thread(current);
  754. flush_altivec_to_thread(current);
  755. flush_vsx_to_thread(current);
  756. vcpu->arch.wqp = &vcpu->arch.vcore->wq;
  757. do {
  758. r = kvmppc_run_vcpu(run, vcpu);
  759. if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
  760. !(vcpu->arch.shregs.msr & MSR_PR)) {
  761. r = kvmppc_pseries_do_hcall(vcpu);
  762. kvmppc_core_deliver_interrupts(vcpu);
  763. }
  764. } while (r == RESUME_GUEST);
  765. return r;
  766. }
  767. static long kvmppc_stt_npages(unsigned long window_size)
  768. {
  769. return ALIGN((window_size >> SPAPR_TCE_SHIFT)
  770. * sizeof(u64), PAGE_SIZE) / PAGE_SIZE;
  771. }
  772. static void release_spapr_tce_table(struct kvmppc_spapr_tce_table *stt)
  773. {
  774. struct kvm *kvm = stt->kvm;
  775. int i;
  776. mutex_lock(&kvm->lock);
  777. list_del(&stt->list);
  778. for (i = 0; i < kvmppc_stt_npages(stt->window_size); i++)
  779. __free_page(stt->pages[i]);
  780. kfree(stt);
  781. mutex_unlock(&kvm->lock);
  782. kvm_put_kvm(kvm);
  783. }
  784. static int kvm_spapr_tce_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  785. {
  786. struct kvmppc_spapr_tce_table *stt = vma->vm_file->private_data;
  787. struct page *page;
  788. if (vmf->pgoff >= kvmppc_stt_npages(stt->window_size))
  789. return VM_FAULT_SIGBUS;
  790. page = stt->pages[vmf->pgoff];
  791. get_page(page);
  792. vmf->page = page;
  793. return 0;
  794. }
  795. static const struct vm_operations_struct kvm_spapr_tce_vm_ops = {
  796. .fault = kvm_spapr_tce_fault,
  797. };
  798. static int kvm_spapr_tce_mmap(struct file *file, struct vm_area_struct *vma)
  799. {
  800. vma->vm_ops = &kvm_spapr_tce_vm_ops;
  801. return 0;
  802. }
  803. static int kvm_spapr_tce_release(struct inode *inode, struct file *filp)
  804. {
  805. struct kvmppc_spapr_tce_table *stt = filp->private_data;
  806. release_spapr_tce_table(stt);
  807. return 0;
  808. }
  809. static struct file_operations kvm_spapr_tce_fops = {
  810. .mmap = kvm_spapr_tce_mmap,
  811. .release = kvm_spapr_tce_release,
  812. };
  813. long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
  814. struct kvm_create_spapr_tce *args)
  815. {
  816. struct kvmppc_spapr_tce_table *stt = NULL;
  817. long npages;
  818. int ret = -ENOMEM;
  819. int i;
  820. /* Check this LIOBN hasn't been previously allocated */
  821. list_for_each_entry(stt, &kvm->arch.spapr_tce_tables, list) {
  822. if (stt->liobn == args->liobn)
  823. return -EBUSY;
  824. }
  825. npages = kvmppc_stt_npages(args->window_size);
  826. stt = kzalloc(sizeof(*stt) + npages* sizeof(struct page *),
  827. GFP_KERNEL);
  828. if (!stt)
  829. goto fail;
  830. stt->liobn = args->liobn;
  831. stt->window_size = args->window_size;
  832. stt->kvm = kvm;
  833. for (i = 0; i < npages; i++) {
  834. stt->pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
  835. if (!stt->pages[i])
  836. goto fail;
  837. }
  838. kvm_get_kvm(kvm);
  839. mutex_lock(&kvm->lock);
  840. list_add(&stt->list, &kvm->arch.spapr_tce_tables);
  841. mutex_unlock(&kvm->lock);
  842. return anon_inode_getfd("kvm-spapr-tce", &kvm_spapr_tce_fops,
  843. stt, O_RDWR);
  844. fail:
  845. if (stt) {
  846. for (i = 0; i < npages; i++)
  847. if (stt->pages[i])
  848. __free_page(stt->pages[i]);
  849. kfree(stt);
  850. }
  851. return ret;
  852. }
  853. /* Work out RMLS (real mode limit selector) field value for a given RMA size.
  854. Assumes POWER7 or PPC970. */
  855. static inline int lpcr_rmls(unsigned long rma_size)
  856. {
  857. switch (rma_size) {
  858. case 32ul << 20: /* 32 MB */
  859. if (cpu_has_feature(CPU_FTR_ARCH_206))
  860. return 8; /* only supported on POWER7 */
  861. return -1;
  862. case 64ul << 20: /* 64 MB */
  863. return 3;
  864. case 128ul << 20: /* 128 MB */
  865. return 7;
  866. case 256ul << 20: /* 256 MB */
  867. return 4;
  868. case 1ul << 30: /* 1 GB */
  869. return 2;
  870. case 16ul << 30: /* 16 GB */
  871. return 1;
  872. case 256ul << 30: /* 256 GB */
  873. return 0;
  874. default:
  875. return -1;
  876. }
  877. }
  878. static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  879. {
  880. struct kvmppc_rma_info *ri = vma->vm_file->private_data;
  881. struct page *page;
  882. if (vmf->pgoff >= ri->npages)
  883. return VM_FAULT_SIGBUS;
  884. page = pfn_to_page(ri->base_pfn + vmf->pgoff);
  885. get_page(page);
  886. vmf->page = page;
  887. return 0;
  888. }
  889. static const struct vm_operations_struct kvm_rma_vm_ops = {
  890. .fault = kvm_rma_fault,
  891. };
  892. static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
  893. {
  894. vma->vm_flags |= VM_RESERVED;
  895. vma->vm_ops = &kvm_rma_vm_ops;
  896. return 0;
  897. }
  898. static int kvm_rma_release(struct inode *inode, struct file *filp)
  899. {
  900. struct kvmppc_rma_info *ri = filp->private_data;
  901. kvm_release_rma(ri);
  902. return 0;
  903. }
  904. static struct file_operations kvm_rma_fops = {
  905. .mmap = kvm_rma_mmap,
  906. .release = kvm_rma_release,
  907. };
  908. long kvm_vm_ioctl_allocate_rma(struct kvm *kvm, struct kvm_allocate_rma *ret)
  909. {
  910. struct kvmppc_rma_info *ri;
  911. long fd;
  912. ri = kvm_alloc_rma();
  913. if (!ri)
  914. return -ENOMEM;
  915. fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR);
  916. if (fd < 0)
  917. kvm_release_rma(ri);
  918. ret->rma_size = ri->npages << PAGE_SHIFT;
  919. return fd;
  920. }
  921. static struct page *hva_to_page(unsigned long addr)
  922. {
  923. struct page *page[1];
  924. int npages;
  925. might_sleep();
  926. npages = get_user_pages_fast(addr, 1, 1, page);
  927. if (unlikely(npages != 1))
  928. return 0;
  929. return page[0];
  930. }
  931. int kvmppc_core_prepare_memory_region(struct kvm *kvm,
  932. struct kvm_userspace_memory_region *mem)
  933. {
  934. unsigned long psize, porder;
  935. unsigned long i, npages, totalpages;
  936. unsigned long pg_ix;
  937. struct kvmppc_pginfo *pginfo;
  938. unsigned long hva;
  939. struct kvmppc_rma_info *ri = NULL;
  940. struct page *page;
  941. /* For now, only allow 16MB pages */
  942. porder = LARGE_PAGE_ORDER;
  943. psize = 1ul << porder;
  944. if ((mem->memory_size & (psize - 1)) ||
  945. (mem->guest_phys_addr & (psize - 1))) {
  946. pr_err("bad memory_size=%llx @ %llx\n",
  947. mem->memory_size, mem->guest_phys_addr);
  948. return -EINVAL;
  949. }
  950. npages = mem->memory_size >> porder;
  951. totalpages = (mem->guest_phys_addr + mem->memory_size) >> porder;
  952. /* More memory than we have space to track? */
  953. if (totalpages > (1ul << (MAX_MEM_ORDER - LARGE_PAGE_ORDER)))
  954. return -EINVAL;
  955. /* Do we already have an RMA registered? */
  956. if (mem->guest_phys_addr == 0 && kvm->arch.rma)
  957. return -EINVAL;
  958. if (totalpages > kvm->arch.ram_npages)
  959. kvm->arch.ram_npages = totalpages;
  960. /* Is this one of our preallocated RMAs? */
  961. if (mem->guest_phys_addr == 0) {
  962. struct vm_area_struct *vma;
  963. down_read(&current->mm->mmap_sem);
  964. vma = find_vma(current->mm, mem->userspace_addr);
  965. if (vma && vma->vm_file &&
  966. vma->vm_file->f_op == &kvm_rma_fops &&
  967. mem->userspace_addr == vma->vm_start)
  968. ri = vma->vm_file->private_data;
  969. up_read(&current->mm->mmap_sem);
  970. if (!ri && cpu_has_feature(CPU_FTR_ARCH_201)) {
  971. pr_err("CPU requires an RMO\n");
  972. return -EINVAL;
  973. }
  974. }
  975. if (ri) {
  976. unsigned long rma_size;
  977. unsigned long lpcr;
  978. long rmls;
  979. rma_size = ri->npages << PAGE_SHIFT;
  980. if (rma_size > mem->memory_size)
  981. rma_size = mem->memory_size;
  982. rmls = lpcr_rmls(rma_size);
  983. if (rmls < 0) {
  984. pr_err("Can't use RMA of 0x%lx bytes\n", rma_size);
  985. return -EINVAL;
  986. }
  987. atomic_inc(&ri->use_count);
  988. kvm->arch.rma = ri;
  989. kvm->arch.n_rma_pages = rma_size >> porder;
  990. /* Update LPCR and RMOR */
  991. lpcr = kvm->arch.lpcr;
  992. if (cpu_has_feature(CPU_FTR_ARCH_201)) {
  993. /* PPC970; insert RMLS value (split field) in HID4 */
  994. lpcr &= ~((1ul << HID4_RMLS0_SH) |
  995. (3ul << HID4_RMLS2_SH));
  996. lpcr |= ((rmls >> 2) << HID4_RMLS0_SH) |
  997. ((rmls & 3) << HID4_RMLS2_SH);
  998. /* RMOR is also in HID4 */
  999. lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
  1000. << HID4_RMOR_SH;
  1001. } else {
  1002. /* POWER7 */
  1003. lpcr &= ~(LPCR_VPM0 | LPCR_VRMA_L);
  1004. lpcr |= rmls << LPCR_RMLS_SH;
  1005. kvm->arch.rmor = kvm->arch.rma->base_pfn << PAGE_SHIFT;
  1006. }
  1007. kvm->arch.lpcr = lpcr;
  1008. pr_info("Using RMO at %lx size %lx (LPCR = %lx)\n",
  1009. ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
  1010. }
  1011. pg_ix = mem->guest_phys_addr >> porder;
  1012. pginfo = kvm->arch.ram_pginfo + pg_ix;
  1013. for (i = 0; i < npages; ++i, ++pg_ix) {
  1014. if (ri && pg_ix < kvm->arch.n_rma_pages) {
  1015. pginfo[i].pfn = ri->base_pfn +
  1016. (pg_ix << (porder - PAGE_SHIFT));
  1017. continue;
  1018. }
  1019. hva = mem->userspace_addr + (i << porder);
  1020. page = hva_to_page(hva);
  1021. if (!page) {
  1022. pr_err("oops, no pfn for hva %lx\n", hva);
  1023. goto err;
  1024. }
  1025. /* Check it's a 16MB page */
  1026. if (!PageHead(page) ||
  1027. compound_order(page) != (LARGE_PAGE_ORDER - PAGE_SHIFT)) {
  1028. pr_err("page at %lx isn't 16MB (o=%d)\n",
  1029. hva, compound_order(page));
  1030. goto err;
  1031. }
  1032. pginfo[i].pfn = page_to_pfn(page);
  1033. }
  1034. return 0;
  1035. err:
  1036. return -EINVAL;
  1037. }
  1038. void kvmppc_core_commit_memory_region(struct kvm *kvm,
  1039. struct kvm_userspace_memory_region *mem)
  1040. {
  1041. if (mem->guest_phys_addr == 0 && mem->memory_size != 0 &&
  1042. !kvm->arch.rma)
  1043. kvmppc_map_vrma(kvm, mem);
  1044. }
  1045. int kvmppc_core_init_vm(struct kvm *kvm)
  1046. {
  1047. long r;
  1048. unsigned long npages = 1ul << (MAX_MEM_ORDER - LARGE_PAGE_ORDER);
  1049. long err = -ENOMEM;
  1050. unsigned long lpcr;
  1051. /* Allocate hashed page table */
  1052. r = kvmppc_alloc_hpt(kvm);
  1053. if (r)
  1054. return r;
  1055. INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
  1056. kvm->arch.ram_pginfo = kzalloc(npages * sizeof(struct kvmppc_pginfo),
  1057. GFP_KERNEL);
  1058. if (!kvm->arch.ram_pginfo) {
  1059. pr_err("kvmppc_core_init_vm: couldn't alloc %lu bytes\n",
  1060. npages * sizeof(struct kvmppc_pginfo));
  1061. goto out_free;
  1062. }
  1063. kvm->arch.ram_npages = 0;
  1064. kvm->arch.ram_psize = 1ul << LARGE_PAGE_ORDER;
  1065. kvm->arch.ram_porder = LARGE_PAGE_ORDER;
  1066. kvm->arch.rma = NULL;
  1067. kvm->arch.n_rma_pages = 0;
  1068. kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
  1069. if (cpu_has_feature(CPU_FTR_ARCH_201)) {
  1070. /* PPC970; HID4 is effectively the LPCR */
  1071. unsigned long lpid = kvm->arch.lpid;
  1072. kvm->arch.host_lpid = 0;
  1073. kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
  1074. lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
  1075. lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
  1076. ((lpid & 0xf) << HID4_LPID5_SH);
  1077. } else {
  1078. /* POWER7; init LPCR for virtual RMA mode */
  1079. kvm->arch.host_lpid = mfspr(SPRN_LPID);
  1080. kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
  1081. lpcr &= LPCR_PECE | LPCR_LPES;
  1082. lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
  1083. LPCR_VPM0 | LPCR_VRMA_L;
  1084. }
  1085. kvm->arch.lpcr = lpcr;
  1086. return 0;
  1087. out_free:
  1088. kvmppc_free_hpt(kvm);
  1089. return err;
  1090. }
  1091. void kvmppc_core_destroy_vm(struct kvm *kvm)
  1092. {
  1093. struct kvmppc_pginfo *pginfo;
  1094. unsigned long i;
  1095. if (kvm->arch.ram_pginfo) {
  1096. pginfo = kvm->arch.ram_pginfo;
  1097. kvm->arch.ram_pginfo = NULL;
  1098. for (i = kvm->arch.n_rma_pages; i < kvm->arch.ram_npages; ++i)
  1099. if (pginfo[i].pfn)
  1100. put_page(pfn_to_page(pginfo[i].pfn));
  1101. kfree(pginfo);
  1102. }
  1103. if (kvm->arch.rma) {
  1104. kvm_release_rma(kvm->arch.rma);
  1105. kvm->arch.rma = NULL;
  1106. }
  1107. kvmppc_free_hpt(kvm);
  1108. WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
  1109. }
  1110. /* These are stubs for now */
  1111. void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
  1112. {
  1113. }
  1114. /* We don't need to emulate any privileged instructions or dcbz */
  1115. int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
  1116. unsigned int inst, int *advance)
  1117. {
  1118. return EMULATE_FAIL;
  1119. }
  1120. int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
  1121. {
  1122. return EMULATE_FAIL;
  1123. }
  1124. int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
  1125. {
  1126. return EMULATE_FAIL;
  1127. }
  1128. static int kvmppc_book3s_hv_init(void)
  1129. {
  1130. int r;
  1131. r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  1132. if (r)
  1133. return r;
  1134. r = kvmppc_mmu_hv_init();
  1135. return r;
  1136. }
  1137. static void kvmppc_book3s_hv_exit(void)
  1138. {
  1139. kvm_exit();
  1140. }
  1141. module_init(kvmppc_book3s_hv_init);
  1142. module_exit(kvmppc_book3s_hv_exit);