book3s_hv.c 32 KB

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