book3s_hv.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  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 <asm/switch_to.h>
  47. #include <linux/gfp.h>
  48. #include <linux/vmalloc.h>
  49. #include <linux/highmem.h>
  50. #include <linux/hugetlb.h>
  51. /* #define EXIT_DEBUG */
  52. /* #define EXIT_DEBUG_SIMPLE */
  53. /* #define EXIT_DEBUG_INT */
  54. static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
  55. static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
  56. void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  57. {
  58. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  59. local_paca->kvm_hstate.kvm_vcpu = vcpu;
  60. local_paca->kvm_hstate.kvm_vcore = vc;
  61. if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
  62. vc->stolen_tb += mftb() - vc->preempt_tb;
  63. }
  64. void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
  65. {
  66. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  67. if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
  68. vc->preempt_tb = mftb();
  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. /* Length for a per-processor buffer is passed in at offset 4 in the buffer */
  130. struct reg_vpa {
  131. u32 dummy;
  132. union {
  133. u16 hword;
  134. u32 word;
  135. } length;
  136. };
  137. static int vpa_is_registered(struct kvmppc_vpa *vpap)
  138. {
  139. if (vpap->update_pending)
  140. return vpap->next_gpa != 0;
  141. return vpap->pinned_addr != NULL;
  142. }
  143. static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
  144. unsigned long flags,
  145. unsigned long vcpuid, unsigned long vpa)
  146. {
  147. struct kvm *kvm = vcpu->kvm;
  148. unsigned long len, nb;
  149. void *va;
  150. struct kvm_vcpu *tvcpu;
  151. int err;
  152. int subfunc;
  153. struct kvmppc_vpa *vpap;
  154. tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
  155. if (!tvcpu)
  156. return H_PARAMETER;
  157. subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
  158. if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
  159. subfunc == H_VPA_REG_SLB) {
  160. /* Registering new area - address must be cache-line aligned */
  161. if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
  162. return H_PARAMETER;
  163. /* convert logical addr to kernel addr and read length */
  164. va = kvmppc_pin_guest_page(kvm, vpa, &nb);
  165. if (va == NULL)
  166. return H_PARAMETER;
  167. if (subfunc == H_VPA_REG_VPA)
  168. len = ((struct reg_vpa *)va)->length.hword;
  169. else
  170. len = ((struct reg_vpa *)va)->length.word;
  171. kvmppc_unpin_guest_page(kvm, va);
  172. /* Check length */
  173. if (len > nb || len < sizeof(struct reg_vpa))
  174. return H_PARAMETER;
  175. } else {
  176. vpa = 0;
  177. len = 0;
  178. }
  179. err = H_PARAMETER;
  180. vpap = NULL;
  181. spin_lock(&tvcpu->arch.vpa_update_lock);
  182. switch (subfunc) {
  183. case H_VPA_REG_VPA: /* register VPA */
  184. if (len < sizeof(struct lppaca))
  185. break;
  186. vpap = &tvcpu->arch.vpa;
  187. err = 0;
  188. break;
  189. case H_VPA_REG_DTL: /* register DTL */
  190. if (len < sizeof(struct dtl_entry))
  191. break;
  192. len -= len % sizeof(struct dtl_entry);
  193. /* Check that they have previously registered a VPA */
  194. err = H_RESOURCE;
  195. if (!vpa_is_registered(&tvcpu->arch.vpa))
  196. break;
  197. vpap = &tvcpu->arch.dtl;
  198. err = 0;
  199. break;
  200. case H_VPA_REG_SLB: /* register SLB shadow buffer */
  201. /* Check that they have previously registered a VPA */
  202. err = H_RESOURCE;
  203. if (!vpa_is_registered(&tvcpu->arch.vpa))
  204. break;
  205. vpap = &tvcpu->arch.slb_shadow;
  206. err = 0;
  207. break;
  208. case H_VPA_DEREG_VPA: /* deregister VPA */
  209. /* Check they don't still have a DTL or SLB buf registered */
  210. err = H_RESOURCE;
  211. if (vpa_is_registered(&tvcpu->arch.dtl) ||
  212. vpa_is_registered(&tvcpu->arch.slb_shadow))
  213. break;
  214. vpap = &tvcpu->arch.vpa;
  215. err = 0;
  216. break;
  217. case H_VPA_DEREG_DTL: /* deregister DTL */
  218. vpap = &tvcpu->arch.dtl;
  219. err = 0;
  220. break;
  221. case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
  222. vpap = &tvcpu->arch.slb_shadow;
  223. err = 0;
  224. break;
  225. }
  226. if (vpap) {
  227. vpap->next_gpa = vpa;
  228. vpap->len = len;
  229. vpap->update_pending = 1;
  230. }
  231. spin_unlock(&tvcpu->arch.vpa_update_lock);
  232. return err;
  233. }
  234. static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
  235. {
  236. struct kvm *kvm = vcpu->kvm;
  237. void *va;
  238. unsigned long nb;
  239. unsigned long gpa;
  240. /*
  241. * We need to pin the page pointed to by vpap->next_gpa,
  242. * but we can't call kvmppc_pin_guest_page under the lock
  243. * as it does get_user_pages() and down_read(). So we
  244. * have to drop the lock, pin the page, then get the lock
  245. * again and check that a new area didn't get registered
  246. * in the meantime.
  247. */
  248. for (;;) {
  249. gpa = vpap->next_gpa;
  250. spin_unlock(&vcpu->arch.vpa_update_lock);
  251. va = NULL;
  252. nb = 0;
  253. if (gpa)
  254. va = kvmppc_pin_guest_page(kvm, vpap->next_gpa, &nb);
  255. spin_lock(&vcpu->arch.vpa_update_lock);
  256. if (gpa == vpap->next_gpa)
  257. break;
  258. /* sigh... unpin that one and try again */
  259. if (va)
  260. kvmppc_unpin_guest_page(kvm, va);
  261. }
  262. vpap->update_pending = 0;
  263. if (va && nb < vpap->len) {
  264. /*
  265. * If it's now too short, it must be that userspace
  266. * has changed the mappings underlying guest memory,
  267. * so unregister the region.
  268. */
  269. kvmppc_unpin_guest_page(kvm, va);
  270. va = NULL;
  271. }
  272. if (vpap->pinned_addr)
  273. kvmppc_unpin_guest_page(kvm, vpap->pinned_addr);
  274. vpap->pinned_addr = va;
  275. if (va)
  276. vpap->pinned_end = va + vpap->len;
  277. }
  278. static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
  279. {
  280. spin_lock(&vcpu->arch.vpa_update_lock);
  281. if (vcpu->arch.vpa.update_pending) {
  282. kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
  283. init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
  284. }
  285. if (vcpu->arch.dtl.update_pending) {
  286. kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
  287. vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
  288. vcpu->arch.dtl_index = 0;
  289. }
  290. if (vcpu->arch.slb_shadow.update_pending)
  291. kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
  292. spin_unlock(&vcpu->arch.vpa_update_lock);
  293. }
  294. static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
  295. struct kvmppc_vcore *vc)
  296. {
  297. struct dtl_entry *dt;
  298. struct lppaca *vpa;
  299. unsigned long old_stolen;
  300. dt = vcpu->arch.dtl_ptr;
  301. vpa = vcpu->arch.vpa.pinned_addr;
  302. old_stolen = vcpu->arch.stolen_logged;
  303. vcpu->arch.stolen_logged = vc->stolen_tb;
  304. if (!dt || !vpa)
  305. return;
  306. memset(dt, 0, sizeof(struct dtl_entry));
  307. dt->dispatch_reason = 7;
  308. dt->processor_id = vc->pcpu + vcpu->arch.ptid;
  309. dt->timebase = mftb();
  310. dt->enqueue_to_dispatch_time = vc->stolen_tb - old_stolen;
  311. dt->srr0 = kvmppc_get_pc(vcpu);
  312. dt->srr1 = vcpu->arch.shregs.msr;
  313. ++dt;
  314. if (dt == vcpu->arch.dtl.pinned_end)
  315. dt = vcpu->arch.dtl.pinned_addr;
  316. vcpu->arch.dtl_ptr = dt;
  317. /* order writing *dt vs. writing vpa->dtl_idx */
  318. smp_wmb();
  319. vpa->dtl_idx = ++vcpu->arch.dtl_index;
  320. }
  321. int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
  322. {
  323. unsigned long req = kvmppc_get_gpr(vcpu, 3);
  324. unsigned long target, ret = H_SUCCESS;
  325. struct kvm_vcpu *tvcpu;
  326. switch (req) {
  327. case H_ENTER:
  328. ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
  329. kvmppc_get_gpr(vcpu, 5),
  330. kvmppc_get_gpr(vcpu, 6),
  331. kvmppc_get_gpr(vcpu, 7));
  332. break;
  333. case H_CEDE:
  334. break;
  335. case H_PROD:
  336. target = kvmppc_get_gpr(vcpu, 4);
  337. tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
  338. if (!tvcpu) {
  339. ret = H_PARAMETER;
  340. break;
  341. }
  342. tvcpu->arch.prodded = 1;
  343. smp_mb();
  344. if (vcpu->arch.ceded) {
  345. if (waitqueue_active(&vcpu->wq)) {
  346. wake_up_interruptible(&vcpu->wq);
  347. vcpu->stat.halt_wakeup++;
  348. }
  349. }
  350. break;
  351. case H_CONFER:
  352. break;
  353. case H_REGISTER_VPA:
  354. ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
  355. kvmppc_get_gpr(vcpu, 5),
  356. kvmppc_get_gpr(vcpu, 6));
  357. break;
  358. default:
  359. return RESUME_HOST;
  360. }
  361. kvmppc_set_gpr(vcpu, 3, ret);
  362. vcpu->arch.hcall_needed = 0;
  363. return RESUME_GUEST;
  364. }
  365. static int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
  366. struct task_struct *tsk)
  367. {
  368. int r = RESUME_HOST;
  369. vcpu->stat.sum_exits++;
  370. run->exit_reason = KVM_EXIT_UNKNOWN;
  371. run->ready_for_interrupt_injection = 1;
  372. switch (vcpu->arch.trap) {
  373. /* We're good on these - the host merely wanted to get our attention */
  374. case BOOK3S_INTERRUPT_HV_DECREMENTER:
  375. vcpu->stat.dec_exits++;
  376. r = RESUME_GUEST;
  377. break;
  378. case BOOK3S_INTERRUPT_EXTERNAL:
  379. vcpu->stat.ext_intr_exits++;
  380. r = RESUME_GUEST;
  381. break;
  382. case BOOK3S_INTERRUPT_PERFMON:
  383. r = RESUME_GUEST;
  384. break;
  385. case BOOK3S_INTERRUPT_PROGRAM:
  386. {
  387. ulong flags;
  388. /*
  389. * Normally program interrupts are delivered directly
  390. * to the guest by the hardware, but we can get here
  391. * as a result of a hypervisor emulation interrupt
  392. * (e40) getting turned into a 700 by BML RTAS.
  393. */
  394. flags = vcpu->arch.shregs.msr & 0x1f0000ull;
  395. kvmppc_core_queue_program(vcpu, flags);
  396. r = RESUME_GUEST;
  397. break;
  398. }
  399. case BOOK3S_INTERRUPT_SYSCALL:
  400. {
  401. /* hcall - punt to userspace */
  402. int i;
  403. if (vcpu->arch.shregs.msr & MSR_PR) {
  404. /* sc 1 from userspace - reflect to guest syscall */
  405. kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_SYSCALL);
  406. r = RESUME_GUEST;
  407. break;
  408. }
  409. run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
  410. for (i = 0; i < 9; ++i)
  411. run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
  412. run->exit_reason = KVM_EXIT_PAPR_HCALL;
  413. vcpu->arch.hcall_needed = 1;
  414. r = RESUME_HOST;
  415. break;
  416. }
  417. /*
  418. * We get these next two if the guest accesses a page which it thinks
  419. * it has mapped but which is not actually present, either because
  420. * it is for an emulated I/O device or because the corresonding
  421. * host page has been paged out. Any other HDSI/HISI interrupts
  422. * have been handled already.
  423. */
  424. case BOOK3S_INTERRUPT_H_DATA_STORAGE:
  425. r = kvmppc_book3s_hv_page_fault(run, vcpu,
  426. vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
  427. break;
  428. case BOOK3S_INTERRUPT_H_INST_STORAGE:
  429. r = kvmppc_book3s_hv_page_fault(run, vcpu,
  430. kvmppc_get_pc(vcpu), 0);
  431. break;
  432. /*
  433. * This occurs if the guest executes an illegal instruction.
  434. * We just generate a program interrupt to the guest, since
  435. * we don't emulate any guest instructions at this stage.
  436. */
  437. case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
  438. kvmppc_core_queue_program(vcpu, 0x80000);
  439. r = RESUME_GUEST;
  440. break;
  441. default:
  442. kvmppc_dump_regs(vcpu);
  443. printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
  444. vcpu->arch.trap, kvmppc_get_pc(vcpu),
  445. vcpu->arch.shregs.msr);
  446. r = RESUME_HOST;
  447. BUG();
  448. break;
  449. }
  450. return r;
  451. }
  452. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  453. struct kvm_sregs *sregs)
  454. {
  455. int i;
  456. sregs->pvr = vcpu->arch.pvr;
  457. memset(sregs, 0, sizeof(struct kvm_sregs));
  458. for (i = 0; i < vcpu->arch.slb_max; i++) {
  459. sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
  460. sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
  461. }
  462. return 0;
  463. }
  464. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  465. struct kvm_sregs *sregs)
  466. {
  467. int i, j;
  468. kvmppc_set_pvr(vcpu, sregs->pvr);
  469. j = 0;
  470. for (i = 0; i < vcpu->arch.slb_nr; i++) {
  471. if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
  472. vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
  473. vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
  474. ++j;
  475. }
  476. }
  477. vcpu->arch.slb_max = j;
  478. return 0;
  479. }
  480. int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  481. {
  482. int r = -EINVAL;
  483. switch (reg->id) {
  484. case KVM_REG_PPC_HIOR:
  485. r = put_user(0, (u64 __user *)reg->addr);
  486. break;
  487. default:
  488. break;
  489. }
  490. return r;
  491. }
  492. int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  493. {
  494. int r = -EINVAL;
  495. switch (reg->id) {
  496. case KVM_REG_PPC_HIOR:
  497. {
  498. u64 hior;
  499. /* Only allow this to be set to zero */
  500. r = get_user(hior, (u64 __user *)reg->addr);
  501. if (!r && (hior != 0))
  502. r = -EINVAL;
  503. break;
  504. }
  505. default:
  506. break;
  507. }
  508. return r;
  509. }
  510. int kvmppc_core_check_processor_compat(void)
  511. {
  512. if (cpu_has_feature(CPU_FTR_HVMODE))
  513. return 0;
  514. return -EIO;
  515. }
  516. struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
  517. {
  518. struct kvm_vcpu *vcpu;
  519. int err = -EINVAL;
  520. int core;
  521. struct kvmppc_vcore *vcore;
  522. core = id / threads_per_core;
  523. if (core >= KVM_MAX_VCORES)
  524. goto out;
  525. err = -ENOMEM;
  526. vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  527. if (!vcpu)
  528. goto out;
  529. err = kvm_vcpu_init(vcpu, kvm, id);
  530. if (err)
  531. goto free_vcpu;
  532. vcpu->arch.shared = &vcpu->arch.shregs;
  533. vcpu->arch.last_cpu = -1;
  534. vcpu->arch.mmcr[0] = MMCR0_FC;
  535. vcpu->arch.ctrl = CTRL_RUNLATCH;
  536. /* default to host PVR, since we can't spoof it */
  537. vcpu->arch.pvr = mfspr(SPRN_PVR);
  538. kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
  539. spin_lock_init(&vcpu->arch.vpa_update_lock);
  540. kvmppc_mmu_book3s_hv_init(vcpu);
  541. /*
  542. * We consider the vcpu stopped until we see the first run ioctl for it.
  543. */
  544. vcpu->arch.state = KVMPPC_VCPU_STOPPED;
  545. init_waitqueue_head(&vcpu->arch.cpu_run);
  546. mutex_lock(&kvm->lock);
  547. vcore = kvm->arch.vcores[core];
  548. if (!vcore) {
  549. vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
  550. if (vcore) {
  551. INIT_LIST_HEAD(&vcore->runnable_threads);
  552. spin_lock_init(&vcore->lock);
  553. init_waitqueue_head(&vcore->wq);
  554. vcore->preempt_tb = mftb();
  555. }
  556. kvm->arch.vcores[core] = vcore;
  557. }
  558. mutex_unlock(&kvm->lock);
  559. if (!vcore)
  560. goto free_vcpu;
  561. spin_lock(&vcore->lock);
  562. ++vcore->num_threads;
  563. spin_unlock(&vcore->lock);
  564. vcpu->arch.vcore = vcore;
  565. vcpu->arch.stolen_logged = vcore->stolen_tb;
  566. vcpu->arch.cpu_type = KVM_CPU_3S_64;
  567. kvmppc_sanity_check(vcpu);
  568. return vcpu;
  569. free_vcpu:
  570. kmem_cache_free(kvm_vcpu_cache, vcpu);
  571. out:
  572. return ERR_PTR(err);
  573. }
  574. void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
  575. {
  576. spin_lock(&vcpu->arch.vpa_update_lock);
  577. if (vcpu->arch.dtl.pinned_addr)
  578. kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.dtl.pinned_addr);
  579. if (vcpu->arch.slb_shadow.pinned_addr)
  580. kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.slb_shadow.pinned_addr);
  581. if (vcpu->arch.vpa.pinned_addr)
  582. kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.vpa.pinned_addr);
  583. spin_unlock(&vcpu->arch.vpa_update_lock);
  584. kvm_vcpu_uninit(vcpu);
  585. kmem_cache_free(kvm_vcpu_cache, vcpu);
  586. }
  587. static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
  588. {
  589. unsigned long dec_nsec, now;
  590. now = get_tb();
  591. if (now > vcpu->arch.dec_expires) {
  592. /* decrementer has already gone negative */
  593. kvmppc_core_queue_dec(vcpu);
  594. kvmppc_core_prepare_to_enter(vcpu);
  595. return;
  596. }
  597. dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
  598. / tb_ticks_per_sec;
  599. hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
  600. HRTIMER_MODE_REL);
  601. vcpu->arch.timer_running = 1;
  602. }
  603. static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
  604. {
  605. vcpu->arch.ceded = 0;
  606. if (vcpu->arch.timer_running) {
  607. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  608. vcpu->arch.timer_running = 0;
  609. }
  610. }
  611. extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
  612. extern void xics_wake_cpu(int cpu);
  613. static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
  614. struct kvm_vcpu *vcpu)
  615. {
  616. struct kvm_vcpu *v;
  617. if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
  618. return;
  619. vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
  620. --vc->n_runnable;
  621. ++vc->n_busy;
  622. /* decrement the physical thread id of each following vcpu */
  623. v = vcpu;
  624. list_for_each_entry_continue(v, &vc->runnable_threads, arch.run_list)
  625. --v->arch.ptid;
  626. list_del(&vcpu->arch.run_list);
  627. }
  628. static int kvmppc_grab_hwthread(int cpu)
  629. {
  630. struct paca_struct *tpaca;
  631. long timeout = 1000;
  632. tpaca = &paca[cpu];
  633. /* Ensure the thread won't go into the kernel if it wakes */
  634. tpaca->kvm_hstate.hwthread_req = 1;
  635. /*
  636. * If the thread is already executing in the kernel (e.g. handling
  637. * a stray interrupt), wait for it to get back to nap mode.
  638. * The smp_mb() is to ensure that our setting of hwthread_req
  639. * is visible before we look at hwthread_state, so if this
  640. * races with the code at system_reset_pSeries and the thread
  641. * misses our setting of hwthread_req, we are sure to see its
  642. * setting of hwthread_state, and vice versa.
  643. */
  644. smp_mb();
  645. while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
  646. if (--timeout <= 0) {
  647. pr_err("KVM: couldn't grab cpu %d\n", cpu);
  648. return -EBUSY;
  649. }
  650. udelay(1);
  651. }
  652. return 0;
  653. }
  654. static void kvmppc_release_hwthread(int cpu)
  655. {
  656. struct paca_struct *tpaca;
  657. tpaca = &paca[cpu];
  658. tpaca->kvm_hstate.hwthread_req = 0;
  659. tpaca->kvm_hstate.kvm_vcpu = NULL;
  660. }
  661. static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
  662. {
  663. int cpu;
  664. struct paca_struct *tpaca;
  665. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  666. if (vcpu->arch.timer_running) {
  667. hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
  668. vcpu->arch.timer_running = 0;
  669. }
  670. cpu = vc->pcpu + vcpu->arch.ptid;
  671. tpaca = &paca[cpu];
  672. tpaca->kvm_hstate.kvm_vcpu = vcpu;
  673. tpaca->kvm_hstate.kvm_vcore = vc;
  674. tpaca->kvm_hstate.napping = 0;
  675. vcpu->cpu = vc->pcpu;
  676. smp_wmb();
  677. #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
  678. if (vcpu->arch.ptid) {
  679. kvmppc_grab_hwthread(cpu);
  680. xics_wake_cpu(cpu);
  681. ++vc->n_woken;
  682. }
  683. #endif
  684. }
  685. static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
  686. {
  687. int i;
  688. HMT_low();
  689. i = 0;
  690. while (vc->nap_count < vc->n_woken) {
  691. if (++i >= 1000000) {
  692. pr_err("kvmppc_wait_for_nap timeout %d %d\n",
  693. vc->nap_count, vc->n_woken);
  694. break;
  695. }
  696. cpu_relax();
  697. }
  698. HMT_medium();
  699. }
  700. /*
  701. * Check that we are on thread 0 and that any other threads in
  702. * this core are off-line.
  703. */
  704. static int on_primary_thread(void)
  705. {
  706. int cpu = smp_processor_id();
  707. int thr = cpu_thread_in_core(cpu);
  708. if (thr)
  709. return 0;
  710. while (++thr < threads_per_core)
  711. if (cpu_online(cpu + thr))
  712. return 0;
  713. return 1;
  714. }
  715. /*
  716. * Run a set of guest threads on a physical core.
  717. * Called with vc->lock held.
  718. */
  719. static int kvmppc_run_core(struct kvmppc_vcore *vc)
  720. {
  721. struct kvm_vcpu *vcpu, *vcpu0, *vnext;
  722. long ret;
  723. u64 now;
  724. int ptid, i, need_vpa_update;
  725. /* don't start if any threads have a signal pending */
  726. need_vpa_update = 0;
  727. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
  728. if (signal_pending(vcpu->arch.run_task))
  729. return 0;
  730. need_vpa_update |= vcpu->arch.vpa.update_pending |
  731. vcpu->arch.slb_shadow.update_pending |
  732. vcpu->arch.dtl.update_pending;
  733. }
  734. /*
  735. * Initialize *vc, in particular vc->vcore_state, so we can
  736. * drop the vcore lock if necessary.
  737. */
  738. vc->n_woken = 0;
  739. vc->nap_count = 0;
  740. vc->entry_exit_count = 0;
  741. vc->vcore_state = VCORE_RUNNING;
  742. vc->in_guest = 0;
  743. vc->napping_threads = 0;
  744. /*
  745. * Updating any of the vpas requires calling kvmppc_pin_guest_page,
  746. * which can't be called with any spinlocks held.
  747. */
  748. if (need_vpa_update) {
  749. spin_unlock(&vc->lock);
  750. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  751. kvmppc_update_vpas(vcpu);
  752. spin_lock(&vc->lock);
  753. }
  754. /*
  755. * Make sure we are running on thread 0, and that
  756. * secondary threads are offline.
  757. * XXX we should also block attempts to bring any
  758. * secondary threads online.
  759. */
  760. if (threads_per_core > 1 && !on_primary_thread()) {
  761. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  762. vcpu->arch.ret = -EBUSY;
  763. goto out;
  764. }
  765. /*
  766. * Assign physical thread IDs, first to non-ceded vcpus
  767. * and then to ceded ones.
  768. */
  769. ptid = 0;
  770. vcpu0 = NULL;
  771. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
  772. if (!vcpu->arch.ceded) {
  773. if (!ptid)
  774. vcpu0 = vcpu;
  775. vcpu->arch.ptid = ptid++;
  776. }
  777. }
  778. if (!vcpu0)
  779. return 0; /* nothing to run */
  780. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  781. if (vcpu->arch.ceded)
  782. vcpu->arch.ptid = ptid++;
  783. vc->stolen_tb += mftb() - vc->preempt_tb;
  784. vc->pcpu = smp_processor_id();
  785. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
  786. kvmppc_start_thread(vcpu);
  787. kvmppc_create_dtl_entry(vcpu, vc);
  788. }
  789. /* Grab any remaining hw threads so they can't go into the kernel */
  790. for (i = ptid; i < threads_per_core; ++i)
  791. kvmppc_grab_hwthread(vc->pcpu + i);
  792. preempt_disable();
  793. spin_unlock(&vc->lock);
  794. kvm_guest_enter();
  795. __kvmppc_vcore_entry(NULL, vcpu0);
  796. for (i = 0; i < threads_per_core; ++i)
  797. kvmppc_release_hwthread(vc->pcpu + i);
  798. spin_lock(&vc->lock);
  799. /* disable sending of IPIs on virtual external irqs */
  800. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
  801. vcpu->cpu = -1;
  802. /* wait for secondary threads to finish writing their state to memory */
  803. if (vc->nap_count < vc->n_woken)
  804. kvmppc_wait_for_nap(vc);
  805. /* prevent other vcpu threads from doing kvmppc_start_thread() now */
  806. vc->vcore_state = VCORE_EXITING;
  807. spin_unlock(&vc->lock);
  808. /* make sure updates to secondary vcpu structs are visible now */
  809. smp_mb();
  810. kvm_guest_exit();
  811. preempt_enable();
  812. kvm_resched(vcpu);
  813. now = get_tb();
  814. list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
  815. /* cancel pending dec exception if dec is positive */
  816. if (now < vcpu->arch.dec_expires &&
  817. kvmppc_core_pending_dec(vcpu))
  818. kvmppc_core_dequeue_dec(vcpu);
  819. ret = RESUME_GUEST;
  820. if (vcpu->arch.trap)
  821. ret = kvmppc_handle_exit(vcpu->arch.kvm_run, vcpu,
  822. vcpu->arch.run_task);
  823. vcpu->arch.ret = ret;
  824. vcpu->arch.trap = 0;
  825. if (vcpu->arch.ceded) {
  826. if (ret != RESUME_GUEST)
  827. kvmppc_end_cede(vcpu);
  828. else
  829. kvmppc_set_timer(vcpu);
  830. }
  831. }
  832. spin_lock(&vc->lock);
  833. out:
  834. vc->vcore_state = VCORE_INACTIVE;
  835. vc->preempt_tb = mftb();
  836. list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
  837. arch.run_list) {
  838. if (vcpu->arch.ret != RESUME_GUEST) {
  839. kvmppc_remove_runnable(vc, vcpu);
  840. wake_up(&vcpu->arch.cpu_run);
  841. }
  842. }
  843. return 1;
  844. }
  845. /*
  846. * Wait for some other vcpu thread to execute us, and
  847. * wake us up when we need to handle something in the host.
  848. */
  849. static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
  850. {
  851. DEFINE_WAIT(wait);
  852. prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
  853. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
  854. schedule();
  855. finish_wait(&vcpu->arch.cpu_run, &wait);
  856. }
  857. /*
  858. * All the vcpus in this vcore are idle, so wait for a decrementer
  859. * or external interrupt to one of the vcpus. vc->lock is held.
  860. */
  861. static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
  862. {
  863. DEFINE_WAIT(wait);
  864. struct kvm_vcpu *v;
  865. int all_idle = 1;
  866. prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
  867. vc->vcore_state = VCORE_SLEEPING;
  868. spin_unlock(&vc->lock);
  869. list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
  870. if (!v->arch.ceded || v->arch.pending_exceptions) {
  871. all_idle = 0;
  872. break;
  873. }
  874. }
  875. if (all_idle)
  876. schedule();
  877. finish_wait(&vc->wq, &wait);
  878. spin_lock(&vc->lock);
  879. vc->vcore_state = VCORE_INACTIVE;
  880. }
  881. static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  882. {
  883. int n_ceded;
  884. int prev_state;
  885. struct kvmppc_vcore *vc;
  886. struct kvm_vcpu *v, *vn;
  887. kvm_run->exit_reason = 0;
  888. vcpu->arch.ret = RESUME_GUEST;
  889. vcpu->arch.trap = 0;
  890. /*
  891. * Synchronize with other threads in this virtual core
  892. */
  893. vc = vcpu->arch.vcore;
  894. spin_lock(&vc->lock);
  895. vcpu->arch.ceded = 0;
  896. vcpu->arch.run_task = current;
  897. vcpu->arch.kvm_run = kvm_run;
  898. prev_state = vcpu->arch.state;
  899. vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
  900. list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
  901. ++vc->n_runnable;
  902. /*
  903. * This happens the first time this is called for a vcpu.
  904. * If the vcore is already running, we may be able to start
  905. * this thread straight away and have it join in.
  906. */
  907. if (prev_state == KVMPPC_VCPU_STOPPED) {
  908. if (vc->vcore_state == VCORE_RUNNING &&
  909. VCORE_EXIT_COUNT(vc) == 0) {
  910. vcpu->arch.ptid = vc->n_runnable - 1;
  911. kvmppc_start_thread(vcpu);
  912. }
  913. } else if (prev_state == KVMPPC_VCPU_BUSY_IN_HOST)
  914. --vc->n_busy;
  915. while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
  916. !signal_pending(current)) {
  917. if (vc->n_busy || vc->vcore_state != VCORE_INACTIVE) {
  918. spin_unlock(&vc->lock);
  919. kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
  920. spin_lock(&vc->lock);
  921. continue;
  922. }
  923. vc->runner = vcpu;
  924. n_ceded = 0;
  925. list_for_each_entry(v, &vc->runnable_threads, arch.run_list)
  926. n_ceded += v->arch.ceded;
  927. if (n_ceded == vc->n_runnable)
  928. kvmppc_vcore_blocked(vc);
  929. else
  930. kvmppc_run_core(vc);
  931. list_for_each_entry_safe(v, vn, &vc->runnable_threads,
  932. arch.run_list) {
  933. kvmppc_core_prepare_to_enter(v);
  934. if (signal_pending(v->arch.run_task)) {
  935. kvmppc_remove_runnable(vc, v);
  936. v->stat.signal_exits++;
  937. v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
  938. v->arch.ret = -EINTR;
  939. wake_up(&v->arch.cpu_run);
  940. }
  941. }
  942. vc->runner = NULL;
  943. }
  944. if (signal_pending(current)) {
  945. if (vc->vcore_state == VCORE_RUNNING ||
  946. vc->vcore_state == VCORE_EXITING) {
  947. spin_unlock(&vc->lock);
  948. kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
  949. spin_lock(&vc->lock);
  950. }
  951. if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
  952. kvmppc_remove_runnable(vc, vcpu);
  953. vcpu->stat.signal_exits++;
  954. kvm_run->exit_reason = KVM_EXIT_INTR;
  955. vcpu->arch.ret = -EINTR;
  956. }
  957. }
  958. spin_unlock(&vc->lock);
  959. return vcpu->arch.ret;
  960. }
  961. int kvmppc_vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu)
  962. {
  963. int r;
  964. if (!vcpu->arch.sane) {
  965. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  966. return -EINVAL;
  967. }
  968. kvmppc_core_prepare_to_enter(vcpu);
  969. /* No need to go into the guest when all we'll do is come back out */
  970. if (signal_pending(current)) {
  971. run->exit_reason = KVM_EXIT_INTR;
  972. return -EINTR;
  973. }
  974. atomic_inc(&vcpu->kvm->arch.vcpus_running);
  975. /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
  976. smp_mb();
  977. /* On the first time here, set up HTAB and VRMA or RMA */
  978. if (!vcpu->kvm->arch.rma_setup_done) {
  979. r = kvmppc_hv_setup_htab_rma(vcpu);
  980. if (r)
  981. goto out;
  982. }
  983. flush_fp_to_thread(current);
  984. flush_altivec_to_thread(current);
  985. flush_vsx_to_thread(current);
  986. vcpu->arch.wqp = &vcpu->arch.vcore->wq;
  987. vcpu->arch.pgdir = current->mm->pgd;
  988. do {
  989. r = kvmppc_run_vcpu(run, vcpu);
  990. if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
  991. !(vcpu->arch.shregs.msr & MSR_PR)) {
  992. r = kvmppc_pseries_do_hcall(vcpu);
  993. kvmppc_core_prepare_to_enter(vcpu);
  994. }
  995. } while (r == RESUME_GUEST);
  996. out:
  997. atomic_dec(&vcpu->kvm->arch.vcpus_running);
  998. return r;
  999. }
  1000. /* Work out RMLS (real mode limit selector) field value for a given RMA size.
  1001. Assumes POWER7 or PPC970. */
  1002. static inline int lpcr_rmls(unsigned long rma_size)
  1003. {
  1004. switch (rma_size) {
  1005. case 32ul << 20: /* 32 MB */
  1006. if (cpu_has_feature(CPU_FTR_ARCH_206))
  1007. return 8; /* only supported on POWER7 */
  1008. return -1;
  1009. case 64ul << 20: /* 64 MB */
  1010. return 3;
  1011. case 128ul << 20: /* 128 MB */
  1012. return 7;
  1013. case 256ul << 20: /* 256 MB */
  1014. return 4;
  1015. case 1ul << 30: /* 1 GB */
  1016. return 2;
  1017. case 16ul << 30: /* 16 GB */
  1018. return 1;
  1019. case 256ul << 30: /* 256 GB */
  1020. return 0;
  1021. default:
  1022. return -1;
  1023. }
  1024. }
  1025. static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1026. {
  1027. struct kvmppc_linear_info *ri = vma->vm_file->private_data;
  1028. struct page *page;
  1029. if (vmf->pgoff >= ri->npages)
  1030. return VM_FAULT_SIGBUS;
  1031. page = pfn_to_page(ri->base_pfn + vmf->pgoff);
  1032. get_page(page);
  1033. vmf->page = page;
  1034. return 0;
  1035. }
  1036. static const struct vm_operations_struct kvm_rma_vm_ops = {
  1037. .fault = kvm_rma_fault,
  1038. };
  1039. static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
  1040. {
  1041. vma->vm_flags |= VM_RESERVED;
  1042. vma->vm_ops = &kvm_rma_vm_ops;
  1043. return 0;
  1044. }
  1045. static int kvm_rma_release(struct inode *inode, struct file *filp)
  1046. {
  1047. struct kvmppc_linear_info *ri = filp->private_data;
  1048. kvm_release_rma(ri);
  1049. return 0;
  1050. }
  1051. static struct file_operations kvm_rma_fops = {
  1052. .mmap = kvm_rma_mmap,
  1053. .release = kvm_rma_release,
  1054. };
  1055. long kvm_vm_ioctl_allocate_rma(struct kvm *kvm, struct kvm_allocate_rma *ret)
  1056. {
  1057. struct kvmppc_linear_info *ri;
  1058. long fd;
  1059. ri = kvm_alloc_rma();
  1060. if (!ri)
  1061. return -ENOMEM;
  1062. fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR);
  1063. if (fd < 0)
  1064. kvm_release_rma(ri);
  1065. ret->rma_size = ri->npages << PAGE_SHIFT;
  1066. return fd;
  1067. }
  1068. static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
  1069. int linux_psize)
  1070. {
  1071. struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
  1072. if (!def->shift)
  1073. return;
  1074. (*sps)->page_shift = def->shift;
  1075. (*sps)->slb_enc = def->sllp;
  1076. (*sps)->enc[0].page_shift = def->shift;
  1077. (*sps)->enc[0].pte_enc = def->penc;
  1078. (*sps)++;
  1079. }
  1080. int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
  1081. {
  1082. struct kvm_ppc_one_seg_page_size *sps;
  1083. info->flags = KVM_PPC_PAGE_SIZES_REAL;
  1084. if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
  1085. info->flags |= KVM_PPC_1T_SEGMENTS;
  1086. info->slb_size = mmu_slb_size;
  1087. /* We only support these sizes for now, and no muti-size segments */
  1088. sps = &info->sps[0];
  1089. kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
  1090. kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
  1091. kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
  1092. return 0;
  1093. }
  1094. /*
  1095. * Get (and clear) the dirty memory log for a memory slot.
  1096. */
  1097. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  1098. {
  1099. struct kvm_memory_slot *memslot;
  1100. int r;
  1101. unsigned long n;
  1102. mutex_lock(&kvm->slots_lock);
  1103. r = -EINVAL;
  1104. if (log->slot >= KVM_MEMORY_SLOTS)
  1105. goto out;
  1106. memslot = id_to_memslot(kvm->memslots, log->slot);
  1107. r = -ENOENT;
  1108. if (!memslot->dirty_bitmap)
  1109. goto out;
  1110. n = kvm_dirty_bitmap_bytes(memslot);
  1111. memset(memslot->dirty_bitmap, 0, n);
  1112. r = kvmppc_hv_get_dirty_log(kvm, memslot);
  1113. if (r)
  1114. goto out;
  1115. r = -EFAULT;
  1116. if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
  1117. goto out;
  1118. r = 0;
  1119. out:
  1120. mutex_unlock(&kvm->slots_lock);
  1121. return r;
  1122. }
  1123. static unsigned long slb_pgsize_encoding(unsigned long psize)
  1124. {
  1125. unsigned long senc = 0;
  1126. if (psize > 0x1000) {
  1127. senc = SLB_VSID_L;
  1128. if (psize == 0x10000)
  1129. senc |= SLB_VSID_LP_01;
  1130. }
  1131. return senc;
  1132. }
  1133. int kvmppc_core_prepare_memory_region(struct kvm *kvm,
  1134. struct kvm_userspace_memory_region *mem)
  1135. {
  1136. unsigned long npages;
  1137. unsigned long *phys;
  1138. /* Allocate a slot_phys array */
  1139. phys = kvm->arch.slot_phys[mem->slot];
  1140. if (!kvm->arch.using_mmu_notifiers && !phys) {
  1141. npages = mem->memory_size >> PAGE_SHIFT;
  1142. phys = vzalloc(npages * sizeof(unsigned long));
  1143. if (!phys)
  1144. return -ENOMEM;
  1145. kvm->arch.slot_phys[mem->slot] = phys;
  1146. kvm->arch.slot_npages[mem->slot] = npages;
  1147. }
  1148. return 0;
  1149. }
  1150. static void unpin_slot(struct kvm *kvm, int slot_id)
  1151. {
  1152. unsigned long *physp;
  1153. unsigned long j, npages, pfn;
  1154. struct page *page;
  1155. physp = kvm->arch.slot_phys[slot_id];
  1156. npages = kvm->arch.slot_npages[slot_id];
  1157. if (physp) {
  1158. spin_lock(&kvm->arch.slot_phys_lock);
  1159. for (j = 0; j < npages; j++) {
  1160. if (!(physp[j] & KVMPPC_GOT_PAGE))
  1161. continue;
  1162. pfn = physp[j] >> PAGE_SHIFT;
  1163. page = pfn_to_page(pfn);
  1164. SetPageDirty(page);
  1165. put_page(page);
  1166. }
  1167. kvm->arch.slot_phys[slot_id] = NULL;
  1168. spin_unlock(&kvm->arch.slot_phys_lock);
  1169. vfree(physp);
  1170. }
  1171. }
  1172. void kvmppc_core_commit_memory_region(struct kvm *kvm,
  1173. struct kvm_userspace_memory_region *mem)
  1174. {
  1175. }
  1176. static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
  1177. {
  1178. int err = 0;
  1179. struct kvm *kvm = vcpu->kvm;
  1180. struct kvmppc_linear_info *ri = NULL;
  1181. unsigned long hva;
  1182. struct kvm_memory_slot *memslot;
  1183. struct vm_area_struct *vma;
  1184. unsigned long lpcr, senc;
  1185. unsigned long psize, porder;
  1186. unsigned long rma_size;
  1187. unsigned long rmls;
  1188. unsigned long *physp;
  1189. unsigned long i, npages;
  1190. mutex_lock(&kvm->lock);
  1191. if (kvm->arch.rma_setup_done)
  1192. goto out; /* another vcpu beat us to it */
  1193. /* Allocate hashed page table (if not done already) and reset it */
  1194. if (!kvm->arch.hpt_virt) {
  1195. err = kvmppc_alloc_hpt(kvm, NULL);
  1196. if (err) {
  1197. pr_err("KVM: Couldn't alloc HPT\n");
  1198. goto out;
  1199. }
  1200. }
  1201. /* Look up the memslot for guest physical address 0 */
  1202. memslot = gfn_to_memslot(kvm, 0);
  1203. /* We must have some memory at 0 by now */
  1204. err = -EINVAL;
  1205. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
  1206. goto out;
  1207. /* Look up the VMA for the start of this memory slot */
  1208. hva = memslot->userspace_addr;
  1209. down_read(&current->mm->mmap_sem);
  1210. vma = find_vma(current->mm, hva);
  1211. if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
  1212. goto up_out;
  1213. psize = vma_kernel_pagesize(vma);
  1214. porder = __ilog2(psize);
  1215. /* Is this one of our preallocated RMAs? */
  1216. if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
  1217. hva == vma->vm_start)
  1218. ri = vma->vm_file->private_data;
  1219. up_read(&current->mm->mmap_sem);
  1220. if (!ri) {
  1221. /* On POWER7, use VRMA; on PPC970, give up */
  1222. err = -EPERM;
  1223. if (cpu_has_feature(CPU_FTR_ARCH_201)) {
  1224. pr_err("KVM: CPU requires an RMO\n");
  1225. goto out;
  1226. }
  1227. /* We can handle 4k, 64k or 16M pages in the VRMA */
  1228. err = -EINVAL;
  1229. if (!(psize == 0x1000 || psize == 0x10000 ||
  1230. psize == 0x1000000))
  1231. goto out;
  1232. /* Update VRMASD field in the LPCR */
  1233. senc = slb_pgsize_encoding(psize);
  1234. kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
  1235. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  1236. lpcr = kvm->arch.lpcr & ~LPCR_VRMASD;
  1237. lpcr |= senc << (LPCR_VRMASD_SH - 4);
  1238. kvm->arch.lpcr = lpcr;
  1239. /* Create HPTEs in the hash page table for the VRMA */
  1240. kvmppc_map_vrma(vcpu, memslot, porder);
  1241. } else {
  1242. /* Set up to use an RMO region */
  1243. rma_size = ri->npages;
  1244. if (rma_size > memslot->npages)
  1245. rma_size = memslot->npages;
  1246. rma_size <<= PAGE_SHIFT;
  1247. rmls = lpcr_rmls(rma_size);
  1248. err = -EINVAL;
  1249. if (rmls < 0) {
  1250. pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
  1251. goto out;
  1252. }
  1253. atomic_inc(&ri->use_count);
  1254. kvm->arch.rma = ri;
  1255. /* Update LPCR and RMOR */
  1256. lpcr = kvm->arch.lpcr;
  1257. if (cpu_has_feature(CPU_FTR_ARCH_201)) {
  1258. /* PPC970; insert RMLS value (split field) in HID4 */
  1259. lpcr &= ~((1ul << HID4_RMLS0_SH) |
  1260. (3ul << HID4_RMLS2_SH));
  1261. lpcr |= ((rmls >> 2) << HID4_RMLS0_SH) |
  1262. ((rmls & 3) << HID4_RMLS2_SH);
  1263. /* RMOR is also in HID4 */
  1264. lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
  1265. << HID4_RMOR_SH;
  1266. } else {
  1267. /* POWER7 */
  1268. lpcr &= ~(LPCR_VPM0 | LPCR_VRMA_L);
  1269. lpcr |= rmls << LPCR_RMLS_SH;
  1270. kvm->arch.rmor = kvm->arch.rma->base_pfn << PAGE_SHIFT;
  1271. }
  1272. kvm->arch.lpcr = lpcr;
  1273. pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
  1274. ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
  1275. /* Initialize phys addrs of pages in RMO */
  1276. npages = ri->npages;
  1277. porder = __ilog2(npages);
  1278. physp = kvm->arch.slot_phys[memslot->id];
  1279. spin_lock(&kvm->arch.slot_phys_lock);
  1280. for (i = 0; i < npages; ++i)
  1281. physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) + porder;
  1282. spin_unlock(&kvm->arch.slot_phys_lock);
  1283. }
  1284. /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
  1285. smp_wmb();
  1286. kvm->arch.rma_setup_done = 1;
  1287. err = 0;
  1288. out:
  1289. mutex_unlock(&kvm->lock);
  1290. return err;
  1291. up_out:
  1292. up_read(&current->mm->mmap_sem);
  1293. goto out;
  1294. }
  1295. int kvmppc_core_init_vm(struct kvm *kvm)
  1296. {
  1297. unsigned long lpcr, lpid;
  1298. /* Allocate the guest's logical partition ID */
  1299. lpid = kvmppc_alloc_lpid();
  1300. if (lpid < 0)
  1301. return -ENOMEM;
  1302. kvm->arch.lpid = lpid;
  1303. INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
  1304. kvm->arch.rma = NULL;
  1305. kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
  1306. if (cpu_has_feature(CPU_FTR_ARCH_201)) {
  1307. /* PPC970; HID4 is effectively the LPCR */
  1308. kvm->arch.host_lpid = 0;
  1309. kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
  1310. lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
  1311. lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
  1312. ((lpid & 0xf) << HID4_LPID5_SH);
  1313. } else {
  1314. /* POWER7; init LPCR for virtual RMA mode */
  1315. kvm->arch.host_lpid = mfspr(SPRN_LPID);
  1316. kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
  1317. lpcr &= LPCR_PECE | LPCR_LPES;
  1318. lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
  1319. LPCR_VPM0 | LPCR_VPM1;
  1320. kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
  1321. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  1322. }
  1323. kvm->arch.lpcr = lpcr;
  1324. kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
  1325. spin_lock_init(&kvm->arch.slot_phys_lock);
  1326. return 0;
  1327. }
  1328. void kvmppc_core_destroy_vm(struct kvm *kvm)
  1329. {
  1330. unsigned long i;
  1331. if (!kvm->arch.using_mmu_notifiers)
  1332. for (i = 0; i < KVM_MEM_SLOTS_NUM; i++)
  1333. unpin_slot(kvm, i);
  1334. if (kvm->arch.rma) {
  1335. kvm_release_rma(kvm->arch.rma);
  1336. kvm->arch.rma = NULL;
  1337. }
  1338. kvmppc_free_hpt(kvm);
  1339. WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
  1340. }
  1341. /* These are stubs for now */
  1342. void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
  1343. {
  1344. }
  1345. /* We don't need to emulate any privileged instructions or dcbz */
  1346. int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
  1347. unsigned int inst, int *advance)
  1348. {
  1349. return EMULATE_FAIL;
  1350. }
  1351. int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
  1352. {
  1353. return EMULATE_FAIL;
  1354. }
  1355. int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
  1356. {
  1357. return EMULATE_FAIL;
  1358. }
  1359. static int kvmppc_book3s_hv_init(void)
  1360. {
  1361. int r;
  1362. r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  1363. if (r)
  1364. return r;
  1365. r = kvmppc_mmu_hv_init();
  1366. return r;
  1367. }
  1368. static void kvmppc_book3s_hv_exit(void)
  1369. {
  1370. kvm_exit();
  1371. }
  1372. module_init(kvmppc_book3s_hv_init);
  1373. module_exit(kvmppc_book3s_hv_exit);