book3s_hv.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  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_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. /* On the first time here, set up VRMA or RMA */
  975. if (!vcpu->kvm->arch.rma_setup_done) {
  976. r = kvmppc_hv_setup_rma(vcpu);
  977. if (r)
  978. return r;
  979. }
  980. flush_fp_to_thread(current);
  981. flush_altivec_to_thread(current);
  982. flush_vsx_to_thread(current);
  983. vcpu->arch.wqp = &vcpu->arch.vcore->wq;
  984. vcpu->arch.pgdir = current->mm->pgd;
  985. do {
  986. r = kvmppc_run_vcpu(run, vcpu);
  987. if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
  988. !(vcpu->arch.shregs.msr & MSR_PR)) {
  989. r = kvmppc_pseries_do_hcall(vcpu);
  990. kvmppc_core_prepare_to_enter(vcpu);
  991. }
  992. } while (r == RESUME_GUEST);
  993. return r;
  994. }
  995. /* Work out RMLS (real mode limit selector) field value for a given RMA size.
  996. Assumes POWER7 or PPC970. */
  997. static inline int lpcr_rmls(unsigned long rma_size)
  998. {
  999. switch (rma_size) {
  1000. case 32ul << 20: /* 32 MB */
  1001. if (cpu_has_feature(CPU_FTR_ARCH_206))
  1002. return 8; /* only supported on POWER7 */
  1003. return -1;
  1004. case 64ul << 20: /* 64 MB */
  1005. return 3;
  1006. case 128ul << 20: /* 128 MB */
  1007. return 7;
  1008. case 256ul << 20: /* 256 MB */
  1009. return 4;
  1010. case 1ul << 30: /* 1 GB */
  1011. return 2;
  1012. case 16ul << 30: /* 16 GB */
  1013. return 1;
  1014. case 256ul << 30: /* 256 GB */
  1015. return 0;
  1016. default:
  1017. return -1;
  1018. }
  1019. }
  1020. static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1021. {
  1022. struct kvmppc_linear_info *ri = vma->vm_file->private_data;
  1023. struct page *page;
  1024. if (vmf->pgoff >= ri->npages)
  1025. return VM_FAULT_SIGBUS;
  1026. page = pfn_to_page(ri->base_pfn + vmf->pgoff);
  1027. get_page(page);
  1028. vmf->page = page;
  1029. return 0;
  1030. }
  1031. static const struct vm_operations_struct kvm_rma_vm_ops = {
  1032. .fault = kvm_rma_fault,
  1033. };
  1034. static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
  1035. {
  1036. vma->vm_flags |= VM_RESERVED;
  1037. vma->vm_ops = &kvm_rma_vm_ops;
  1038. return 0;
  1039. }
  1040. static int kvm_rma_release(struct inode *inode, struct file *filp)
  1041. {
  1042. struct kvmppc_linear_info *ri = filp->private_data;
  1043. kvm_release_rma(ri);
  1044. return 0;
  1045. }
  1046. static struct file_operations kvm_rma_fops = {
  1047. .mmap = kvm_rma_mmap,
  1048. .release = kvm_rma_release,
  1049. };
  1050. long kvm_vm_ioctl_allocate_rma(struct kvm *kvm, struct kvm_allocate_rma *ret)
  1051. {
  1052. struct kvmppc_linear_info *ri;
  1053. long fd;
  1054. ri = kvm_alloc_rma();
  1055. if (!ri)
  1056. return -ENOMEM;
  1057. fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR);
  1058. if (fd < 0)
  1059. kvm_release_rma(ri);
  1060. ret->rma_size = ri->npages << PAGE_SHIFT;
  1061. return fd;
  1062. }
  1063. static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
  1064. int linux_psize)
  1065. {
  1066. struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
  1067. if (!def->shift)
  1068. return;
  1069. (*sps)->page_shift = def->shift;
  1070. (*sps)->slb_enc = def->sllp;
  1071. (*sps)->enc[0].page_shift = def->shift;
  1072. (*sps)->enc[0].pte_enc = def->penc;
  1073. (*sps)++;
  1074. }
  1075. int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm, struct kvm_ppc_smmu_info *info)
  1076. {
  1077. struct kvm_ppc_one_seg_page_size *sps;
  1078. info->flags = KVM_PPC_PAGE_SIZES_REAL;
  1079. if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
  1080. info->flags |= KVM_PPC_1T_SEGMENTS;
  1081. info->slb_size = mmu_slb_size;
  1082. /* We only support these sizes for now, and no muti-size segments */
  1083. sps = &info->sps[0];
  1084. kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
  1085. kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
  1086. kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
  1087. return 0;
  1088. }
  1089. /*
  1090. * Get (and clear) the dirty memory log for a memory slot.
  1091. */
  1092. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  1093. {
  1094. struct kvm_memory_slot *memslot;
  1095. int r;
  1096. unsigned long n;
  1097. mutex_lock(&kvm->slots_lock);
  1098. r = -EINVAL;
  1099. if (log->slot >= KVM_MEMORY_SLOTS)
  1100. goto out;
  1101. memslot = id_to_memslot(kvm->memslots, log->slot);
  1102. r = -ENOENT;
  1103. if (!memslot->dirty_bitmap)
  1104. goto out;
  1105. n = kvm_dirty_bitmap_bytes(memslot);
  1106. memset(memslot->dirty_bitmap, 0, n);
  1107. r = kvmppc_hv_get_dirty_log(kvm, memslot);
  1108. if (r)
  1109. goto out;
  1110. r = -EFAULT;
  1111. if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
  1112. goto out;
  1113. r = 0;
  1114. out:
  1115. mutex_unlock(&kvm->slots_lock);
  1116. return r;
  1117. }
  1118. static unsigned long slb_pgsize_encoding(unsigned long psize)
  1119. {
  1120. unsigned long senc = 0;
  1121. if (psize > 0x1000) {
  1122. senc = SLB_VSID_L;
  1123. if (psize == 0x10000)
  1124. senc |= SLB_VSID_LP_01;
  1125. }
  1126. return senc;
  1127. }
  1128. int kvmppc_core_prepare_memory_region(struct kvm *kvm,
  1129. struct kvm_userspace_memory_region *mem)
  1130. {
  1131. unsigned long npages;
  1132. unsigned long *phys;
  1133. /* Allocate a slot_phys array */
  1134. phys = kvm->arch.slot_phys[mem->slot];
  1135. if (!kvm->arch.using_mmu_notifiers && !phys) {
  1136. npages = mem->memory_size >> PAGE_SHIFT;
  1137. phys = vzalloc(npages * sizeof(unsigned long));
  1138. if (!phys)
  1139. return -ENOMEM;
  1140. kvm->arch.slot_phys[mem->slot] = phys;
  1141. kvm->arch.slot_npages[mem->slot] = npages;
  1142. }
  1143. return 0;
  1144. }
  1145. static void unpin_slot(struct kvm *kvm, int slot_id)
  1146. {
  1147. unsigned long *physp;
  1148. unsigned long j, npages, pfn;
  1149. struct page *page;
  1150. physp = kvm->arch.slot_phys[slot_id];
  1151. npages = kvm->arch.slot_npages[slot_id];
  1152. if (physp) {
  1153. spin_lock(&kvm->arch.slot_phys_lock);
  1154. for (j = 0; j < npages; j++) {
  1155. if (!(physp[j] & KVMPPC_GOT_PAGE))
  1156. continue;
  1157. pfn = physp[j] >> PAGE_SHIFT;
  1158. page = pfn_to_page(pfn);
  1159. SetPageDirty(page);
  1160. put_page(page);
  1161. }
  1162. kvm->arch.slot_phys[slot_id] = NULL;
  1163. spin_unlock(&kvm->arch.slot_phys_lock);
  1164. vfree(physp);
  1165. }
  1166. }
  1167. void kvmppc_core_commit_memory_region(struct kvm *kvm,
  1168. struct kvm_userspace_memory_region *mem)
  1169. {
  1170. }
  1171. static int kvmppc_hv_setup_rma(struct kvm_vcpu *vcpu)
  1172. {
  1173. int err = 0;
  1174. struct kvm *kvm = vcpu->kvm;
  1175. struct kvmppc_linear_info *ri = NULL;
  1176. unsigned long hva;
  1177. struct kvm_memory_slot *memslot;
  1178. struct vm_area_struct *vma;
  1179. unsigned long lpcr, senc;
  1180. unsigned long psize, porder;
  1181. unsigned long rma_size;
  1182. unsigned long rmls;
  1183. unsigned long *physp;
  1184. unsigned long i, npages;
  1185. mutex_lock(&kvm->lock);
  1186. if (kvm->arch.rma_setup_done)
  1187. goto out; /* another vcpu beat us to it */
  1188. /* Look up the memslot for guest physical address 0 */
  1189. memslot = gfn_to_memslot(kvm, 0);
  1190. /* We must have some memory at 0 by now */
  1191. err = -EINVAL;
  1192. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
  1193. goto out;
  1194. /* Look up the VMA for the start of this memory slot */
  1195. hva = memslot->userspace_addr;
  1196. down_read(&current->mm->mmap_sem);
  1197. vma = find_vma(current->mm, hva);
  1198. if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
  1199. goto up_out;
  1200. psize = vma_kernel_pagesize(vma);
  1201. porder = __ilog2(psize);
  1202. /* Is this one of our preallocated RMAs? */
  1203. if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
  1204. hva == vma->vm_start)
  1205. ri = vma->vm_file->private_data;
  1206. up_read(&current->mm->mmap_sem);
  1207. if (!ri) {
  1208. /* On POWER7, use VRMA; on PPC970, give up */
  1209. err = -EPERM;
  1210. if (cpu_has_feature(CPU_FTR_ARCH_201)) {
  1211. pr_err("KVM: CPU requires an RMO\n");
  1212. goto out;
  1213. }
  1214. /* We can handle 4k, 64k or 16M pages in the VRMA */
  1215. err = -EINVAL;
  1216. if (!(psize == 0x1000 || psize == 0x10000 ||
  1217. psize == 0x1000000))
  1218. goto out;
  1219. /* Update VRMASD field in the LPCR */
  1220. senc = slb_pgsize_encoding(psize);
  1221. kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
  1222. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  1223. lpcr = kvm->arch.lpcr & ~LPCR_VRMASD;
  1224. lpcr |= senc << (LPCR_VRMASD_SH - 4);
  1225. kvm->arch.lpcr = lpcr;
  1226. /* Create HPTEs in the hash page table for the VRMA */
  1227. kvmppc_map_vrma(vcpu, memslot, porder);
  1228. } else {
  1229. /* Set up to use an RMO region */
  1230. rma_size = ri->npages;
  1231. if (rma_size > memslot->npages)
  1232. rma_size = memslot->npages;
  1233. rma_size <<= PAGE_SHIFT;
  1234. rmls = lpcr_rmls(rma_size);
  1235. err = -EINVAL;
  1236. if (rmls < 0) {
  1237. pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
  1238. goto out;
  1239. }
  1240. atomic_inc(&ri->use_count);
  1241. kvm->arch.rma = ri;
  1242. /* Update LPCR and RMOR */
  1243. lpcr = kvm->arch.lpcr;
  1244. if (cpu_has_feature(CPU_FTR_ARCH_201)) {
  1245. /* PPC970; insert RMLS value (split field) in HID4 */
  1246. lpcr &= ~((1ul << HID4_RMLS0_SH) |
  1247. (3ul << HID4_RMLS2_SH));
  1248. lpcr |= ((rmls >> 2) << HID4_RMLS0_SH) |
  1249. ((rmls & 3) << HID4_RMLS2_SH);
  1250. /* RMOR is also in HID4 */
  1251. lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
  1252. << HID4_RMOR_SH;
  1253. } else {
  1254. /* POWER7 */
  1255. lpcr &= ~(LPCR_VPM0 | LPCR_VRMA_L);
  1256. lpcr |= rmls << LPCR_RMLS_SH;
  1257. kvm->arch.rmor = kvm->arch.rma->base_pfn << PAGE_SHIFT;
  1258. }
  1259. kvm->arch.lpcr = lpcr;
  1260. pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
  1261. ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
  1262. /* Initialize phys addrs of pages in RMO */
  1263. npages = ri->npages;
  1264. porder = __ilog2(npages);
  1265. physp = kvm->arch.slot_phys[memslot->id];
  1266. spin_lock(&kvm->arch.slot_phys_lock);
  1267. for (i = 0; i < npages; ++i)
  1268. physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) + porder;
  1269. spin_unlock(&kvm->arch.slot_phys_lock);
  1270. }
  1271. /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
  1272. smp_wmb();
  1273. kvm->arch.rma_setup_done = 1;
  1274. err = 0;
  1275. out:
  1276. mutex_unlock(&kvm->lock);
  1277. return err;
  1278. up_out:
  1279. up_read(&current->mm->mmap_sem);
  1280. goto out;
  1281. }
  1282. int kvmppc_core_init_vm(struct kvm *kvm)
  1283. {
  1284. long r;
  1285. unsigned long lpcr;
  1286. /* Allocate hashed page table */
  1287. r = kvmppc_alloc_hpt(kvm);
  1288. if (r)
  1289. return r;
  1290. INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables);
  1291. kvm->arch.rma = NULL;
  1292. kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
  1293. if (cpu_has_feature(CPU_FTR_ARCH_201)) {
  1294. /* PPC970; HID4 is effectively the LPCR */
  1295. unsigned long lpid = kvm->arch.lpid;
  1296. kvm->arch.host_lpid = 0;
  1297. kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
  1298. lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
  1299. lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
  1300. ((lpid & 0xf) << HID4_LPID5_SH);
  1301. } else {
  1302. /* POWER7; init LPCR for virtual RMA mode */
  1303. kvm->arch.host_lpid = mfspr(SPRN_LPID);
  1304. kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
  1305. lpcr &= LPCR_PECE | LPCR_LPES;
  1306. lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
  1307. LPCR_VPM0 | LPCR_VPM1;
  1308. kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
  1309. (VRMA_VSID << SLB_VSID_SHIFT_1T);
  1310. }
  1311. kvm->arch.lpcr = lpcr;
  1312. kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
  1313. spin_lock_init(&kvm->arch.slot_phys_lock);
  1314. return 0;
  1315. }
  1316. void kvmppc_core_destroy_vm(struct kvm *kvm)
  1317. {
  1318. unsigned long i;
  1319. if (!kvm->arch.using_mmu_notifiers)
  1320. for (i = 0; i < KVM_MEM_SLOTS_NUM; i++)
  1321. unpin_slot(kvm, i);
  1322. if (kvm->arch.rma) {
  1323. kvm_release_rma(kvm->arch.rma);
  1324. kvm->arch.rma = NULL;
  1325. }
  1326. kvmppc_free_hpt(kvm);
  1327. WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables));
  1328. }
  1329. /* These are stubs for now */
  1330. void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
  1331. {
  1332. }
  1333. /* We don't need to emulate any privileged instructions or dcbz */
  1334. int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
  1335. unsigned int inst, int *advance)
  1336. {
  1337. return EMULATE_FAIL;
  1338. }
  1339. int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
  1340. {
  1341. return EMULATE_FAIL;
  1342. }
  1343. int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
  1344. {
  1345. return EMULATE_FAIL;
  1346. }
  1347. static int kvmppc_book3s_hv_init(void)
  1348. {
  1349. int r;
  1350. r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
  1351. if (r)
  1352. return r;
  1353. r = kvmppc_mmu_hv_init();
  1354. return r;
  1355. }
  1356. static void kvmppc_book3s_hv_exit(void)
  1357. {
  1358. kvm_exit();
  1359. }
  1360. module_init(kvmppc_book3s_hv_init);
  1361. module_exit(kvmppc_book3s_hv_exit);