powerpc.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License, version 2, as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program; if not, write to the Free Software
  13. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. *
  15. * Copyright IBM Corp. 2007
  16. *
  17. * Authors: Hollis Blanchard <hollisb@us.ibm.com>
  18. * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
  19. */
  20. #include <linux/errno.h>
  21. #include <linux/err.h>
  22. #include <linux/kvm_host.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/hrtimer.h>
  25. #include <linux/sched/signal.h>
  26. #include <linux/fs.h>
  27. #include <linux/slab.h>
  28. #include <linux/file.h>
  29. #include <linux/module.h>
  30. #include <linux/irqbypass.h>
  31. #include <linux/kvm_irqfd.h>
  32. #include <asm/cputable.h>
  33. #include <linux/uaccess.h>
  34. #include <asm/kvm_ppc.h>
  35. #include <asm/tlbflush.h>
  36. #include <asm/cputhreads.h>
  37. #include <asm/irqflags.h>
  38. #include <asm/iommu.h>
  39. #include <asm/switch_to.h>
  40. #include <asm/xive.h>
  41. #include "timing.h"
  42. #include "irq.h"
  43. #include "../mm/mmu_decl.h"
  44. #define CREATE_TRACE_POINTS
  45. #include "trace.h"
  46. struct kvmppc_ops *kvmppc_hv_ops;
  47. EXPORT_SYMBOL_GPL(kvmppc_hv_ops);
  48. struct kvmppc_ops *kvmppc_pr_ops;
  49. EXPORT_SYMBOL_GPL(kvmppc_pr_ops);
  50. int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
  51. {
  52. return !!(v->arch.pending_exceptions) || kvm_request_pending(v);
  53. }
  54. bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
  55. {
  56. return false;
  57. }
  58. int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
  59. {
  60. return 1;
  61. }
  62. /*
  63. * Common checks before entering the guest world. Call with interrupts
  64. * disabled.
  65. *
  66. * returns:
  67. *
  68. * == 1 if we're ready to go into guest state
  69. * <= 0 if we need to go back to the host with return value
  70. */
  71. int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu)
  72. {
  73. int r;
  74. WARN_ON(irqs_disabled());
  75. hard_irq_disable();
  76. while (true) {
  77. if (need_resched()) {
  78. local_irq_enable();
  79. cond_resched();
  80. hard_irq_disable();
  81. continue;
  82. }
  83. if (signal_pending(current)) {
  84. kvmppc_account_exit(vcpu, SIGNAL_EXITS);
  85. vcpu->run->exit_reason = KVM_EXIT_INTR;
  86. r = -EINTR;
  87. break;
  88. }
  89. vcpu->mode = IN_GUEST_MODE;
  90. /*
  91. * Reading vcpu->requests must happen after setting vcpu->mode,
  92. * so we don't miss a request because the requester sees
  93. * OUTSIDE_GUEST_MODE and assumes we'll be checking requests
  94. * before next entering the guest (and thus doesn't IPI).
  95. * This also orders the write to mode from any reads
  96. * to the page tables done while the VCPU is running.
  97. * Please see the comment in kvm_flush_remote_tlbs.
  98. */
  99. smp_mb();
  100. if (kvm_request_pending(vcpu)) {
  101. /* Make sure we process requests preemptable */
  102. local_irq_enable();
  103. trace_kvm_check_requests(vcpu);
  104. r = kvmppc_core_check_requests(vcpu);
  105. hard_irq_disable();
  106. if (r > 0)
  107. continue;
  108. break;
  109. }
  110. if (kvmppc_core_prepare_to_enter(vcpu)) {
  111. /* interrupts got enabled in between, so we
  112. are back at square 1 */
  113. continue;
  114. }
  115. guest_enter_irqoff();
  116. return 1;
  117. }
  118. /* return to host */
  119. local_irq_enable();
  120. return r;
  121. }
  122. EXPORT_SYMBOL_GPL(kvmppc_prepare_to_enter);
  123. #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
  124. static void kvmppc_swab_shared(struct kvm_vcpu *vcpu)
  125. {
  126. struct kvm_vcpu_arch_shared *shared = vcpu->arch.shared;
  127. int i;
  128. shared->sprg0 = swab64(shared->sprg0);
  129. shared->sprg1 = swab64(shared->sprg1);
  130. shared->sprg2 = swab64(shared->sprg2);
  131. shared->sprg3 = swab64(shared->sprg3);
  132. shared->srr0 = swab64(shared->srr0);
  133. shared->srr1 = swab64(shared->srr1);
  134. shared->dar = swab64(shared->dar);
  135. shared->msr = swab64(shared->msr);
  136. shared->dsisr = swab32(shared->dsisr);
  137. shared->int_pending = swab32(shared->int_pending);
  138. for (i = 0; i < ARRAY_SIZE(shared->sr); i++)
  139. shared->sr[i] = swab32(shared->sr[i]);
  140. }
  141. #endif
  142. int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
  143. {
  144. int nr = kvmppc_get_gpr(vcpu, 11);
  145. int r;
  146. unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3);
  147. unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4);
  148. unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5);
  149. unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6);
  150. unsigned long r2 = 0;
  151. if (!(kvmppc_get_msr(vcpu) & MSR_SF)) {
  152. /* 32 bit mode */
  153. param1 &= 0xffffffff;
  154. param2 &= 0xffffffff;
  155. param3 &= 0xffffffff;
  156. param4 &= 0xffffffff;
  157. }
  158. switch (nr) {
  159. case KVM_HCALL_TOKEN(KVM_HC_PPC_MAP_MAGIC_PAGE):
  160. {
  161. #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
  162. /* Book3S can be little endian, find it out here */
  163. int shared_big_endian = true;
  164. if (vcpu->arch.intr_msr & MSR_LE)
  165. shared_big_endian = false;
  166. if (shared_big_endian != vcpu->arch.shared_big_endian)
  167. kvmppc_swab_shared(vcpu);
  168. vcpu->arch.shared_big_endian = shared_big_endian;
  169. #endif
  170. if (!(param2 & MAGIC_PAGE_FLAG_NOT_MAPPED_NX)) {
  171. /*
  172. * Older versions of the Linux magic page code had
  173. * a bug where they would map their trampoline code
  174. * NX. If that's the case, remove !PR NX capability.
  175. */
  176. vcpu->arch.disable_kernel_nx = true;
  177. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  178. }
  179. vcpu->arch.magic_page_pa = param1 & ~0xfffULL;
  180. vcpu->arch.magic_page_ea = param2 & ~0xfffULL;
  181. #ifdef CONFIG_PPC_64K_PAGES
  182. /*
  183. * Make sure our 4k magic page is in the same window of a 64k
  184. * page within the guest and within the host's page.
  185. */
  186. if ((vcpu->arch.magic_page_pa & 0xf000) !=
  187. ((ulong)vcpu->arch.shared & 0xf000)) {
  188. void *old_shared = vcpu->arch.shared;
  189. ulong shared = (ulong)vcpu->arch.shared;
  190. void *new_shared;
  191. shared &= PAGE_MASK;
  192. shared |= vcpu->arch.magic_page_pa & 0xf000;
  193. new_shared = (void*)shared;
  194. memcpy(new_shared, old_shared, 0x1000);
  195. vcpu->arch.shared = new_shared;
  196. }
  197. #endif
  198. r2 = KVM_MAGIC_FEAT_SR | KVM_MAGIC_FEAT_MAS0_TO_SPRG7;
  199. r = EV_SUCCESS;
  200. break;
  201. }
  202. case KVM_HCALL_TOKEN(KVM_HC_FEATURES):
  203. r = EV_SUCCESS;
  204. #if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500V2)
  205. r2 |= (1 << KVM_FEATURE_MAGIC_PAGE);
  206. #endif
  207. /* Second return value is in r4 */
  208. break;
  209. case EV_HCALL_TOKEN(EV_IDLE):
  210. r = EV_SUCCESS;
  211. kvm_vcpu_block(vcpu);
  212. kvm_clear_request(KVM_REQ_UNHALT, vcpu);
  213. break;
  214. default:
  215. r = EV_UNIMPLEMENTED;
  216. break;
  217. }
  218. kvmppc_set_gpr(vcpu, 4, r2);
  219. return r;
  220. }
  221. EXPORT_SYMBOL_GPL(kvmppc_kvm_pv);
  222. int kvmppc_sanity_check(struct kvm_vcpu *vcpu)
  223. {
  224. int r = false;
  225. /* We have to know what CPU to virtualize */
  226. if (!vcpu->arch.pvr)
  227. goto out;
  228. /* PAPR only works with book3s_64 */
  229. if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled)
  230. goto out;
  231. /* HV KVM can only do PAPR mode for now */
  232. if (!vcpu->arch.papr_enabled && is_kvmppc_hv_enabled(vcpu->kvm))
  233. goto out;
  234. #ifdef CONFIG_KVM_BOOKE_HV
  235. if (!cpu_has_feature(CPU_FTR_EMB_HV))
  236. goto out;
  237. #endif
  238. r = true;
  239. out:
  240. vcpu->arch.sane = r;
  241. return r ? 0 : -EINVAL;
  242. }
  243. EXPORT_SYMBOL_GPL(kvmppc_sanity_check);
  244. int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
  245. {
  246. enum emulation_result er;
  247. int r;
  248. er = kvmppc_emulate_loadstore(vcpu);
  249. switch (er) {
  250. case EMULATE_DONE:
  251. /* Future optimization: only reload non-volatiles if they were
  252. * actually modified. */
  253. r = RESUME_GUEST_NV;
  254. break;
  255. case EMULATE_AGAIN:
  256. r = RESUME_GUEST;
  257. break;
  258. case EMULATE_DO_MMIO:
  259. run->exit_reason = KVM_EXIT_MMIO;
  260. /* We must reload nonvolatiles because "update" load/store
  261. * instructions modify register state. */
  262. /* Future optimization: only reload non-volatiles if they were
  263. * actually modified. */
  264. r = RESUME_HOST_NV;
  265. break;
  266. case EMULATE_FAIL:
  267. {
  268. u32 last_inst;
  269. kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
  270. /* XXX Deliver Program interrupt to guest. */
  271. pr_emerg("%s: emulation failed (%08x)\n", __func__, last_inst);
  272. r = RESUME_HOST;
  273. break;
  274. }
  275. default:
  276. WARN_ON(1);
  277. r = RESUME_GUEST;
  278. }
  279. return r;
  280. }
  281. EXPORT_SYMBOL_GPL(kvmppc_emulate_mmio);
  282. int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
  283. bool data)
  284. {
  285. ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK;
  286. struct kvmppc_pte pte;
  287. int r;
  288. vcpu->stat.st++;
  289. r = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
  290. XLATE_WRITE, &pte);
  291. if (r < 0)
  292. return r;
  293. *eaddr = pte.raddr;
  294. if (!pte.may_write)
  295. return -EPERM;
  296. /* Magic page override */
  297. if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
  298. ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
  299. !(kvmppc_get_msr(vcpu) & MSR_PR)) {
  300. void *magic = vcpu->arch.shared;
  301. magic += pte.eaddr & 0xfff;
  302. memcpy(magic, ptr, size);
  303. return EMULATE_DONE;
  304. }
  305. if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
  306. return EMULATE_DO_MMIO;
  307. return EMULATE_DONE;
  308. }
  309. EXPORT_SYMBOL_GPL(kvmppc_st);
  310. int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
  311. bool data)
  312. {
  313. ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK;
  314. struct kvmppc_pte pte;
  315. int rc;
  316. vcpu->stat.ld++;
  317. rc = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
  318. XLATE_READ, &pte);
  319. if (rc)
  320. return rc;
  321. *eaddr = pte.raddr;
  322. if (!pte.may_read)
  323. return -EPERM;
  324. if (!data && !pte.may_execute)
  325. return -ENOEXEC;
  326. /* Magic page override */
  327. if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
  328. ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
  329. !(kvmppc_get_msr(vcpu) & MSR_PR)) {
  330. void *magic = vcpu->arch.shared;
  331. magic += pte.eaddr & 0xfff;
  332. memcpy(ptr, magic, size);
  333. return EMULATE_DONE;
  334. }
  335. if (kvm_read_guest(vcpu->kvm, pte.raddr, ptr, size))
  336. return EMULATE_DO_MMIO;
  337. return EMULATE_DONE;
  338. }
  339. EXPORT_SYMBOL_GPL(kvmppc_ld);
  340. int kvm_arch_hardware_enable(void)
  341. {
  342. return 0;
  343. }
  344. int kvm_arch_hardware_setup(void)
  345. {
  346. return 0;
  347. }
  348. void kvm_arch_check_processor_compat(void *rtn)
  349. {
  350. *(int *)rtn = kvmppc_core_check_processor_compat();
  351. }
  352. int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
  353. {
  354. struct kvmppc_ops *kvm_ops = NULL;
  355. /*
  356. * if we have both HV and PR enabled, default is HV
  357. */
  358. if (type == 0) {
  359. if (kvmppc_hv_ops)
  360. kvm_ops = kvmppc_hv_ops;
  361. else
  362. kvm_ops = kvmppc_pr_ops;
  363. if (!kvm_ops)
  364. goto err_out;
  365. } else if (type == KVM_VM_PPC_HV) {
  366. if (!kvmppc_hv_ops)
  367. goto err_out;
  368. kvm_ops = kvmppc_hv_ops;
  369. } else if (type == KVM_VM_PPC_PR) {
  370. if (!kvmppc_pr_ops)
  371. goto err_out;
  372. kvm_ops = kvmppc_pr_ops;
  373. } else
  374. goto err_out;
  375. if (kvm_ops->owner && !try_module_get(kvm_ops->owner))
  376. return -ENOENT;
  377. kvm->arch.kvm_ops = kvm_ops;
  378. return kvmppc_core_init_vm(kvm);
  379. err_out:
  380. return -EINVAL;
  381. }
  382. bool kvm_arch_has_vcpu_debugfs(void)
  383. {
  384. return false;
  385. }
  386. int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
  387. {
  388. return 0;
  389. }
  390. void kvm_arch_destroy_vm(struct kvm *kvm)
  391. {
  392. unsigned int i;
  393. struct kvm_vcpu *vcpu;
  394. #ifdef CONFIG_KVM_XICS
  395. /*
  396. * We call kick_all_cpus_sync() to ensure that all
  397. * CPUs have executed any pending IPIs before we
  398. * continue and free VCPUs structures below.
  399. */
  400. if (is_kvmppc_hv_enabled(kvm))
  401. kick_all_cpus_sync();
  402. #endif
  403. kvm_for_each_vcpu(i, vcpu, kvm)
  404. kvm_arch_vcpu_free(vcpu);
  405. mutex_lock(&kvm->lock);
  406. for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
  407. kvm->vcpus[i] = NULL;
  408. atomic_set(&kvm->online_vcpus, 0);
  409. kvmppc_core_destroy_vm(kvm);
  410. mutex_unlock(&kvm->lock);
  411. /* drop the module reference */
  412. module_put(kvm->arch.kvm_ops->owner);
  413. }
  414. int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
  415. {
  416. int r;
  417. /* Assume we're using HV mode when the HV module is loaded */
  418. int hv_enabled = kvmppc_hv_ops ? 1 : 0;
  419. if (kvm) {
  420. /*
  421. * Hooray - we know which VM type we're running on. Depend on
  422. * that rather than the guess above.
  423. */
  424. hv_enabled = is_kvmppc_hv_enabled(kvm);
  425. }
  426. switch (ext) {
  427. #ifdef CONFIG_BOOKE
  428. case KVM_CAP_PPC_BOOKE_SREGS:
  429. case KVM_CAP_PPC_BOOKE_WATCHDOG:
  430. case KVM_CAP_PPC_EPR:
  431. #else
  432. case KVM_CAP_PPC_SEGSTATE:
  433. case KVM_CAP_PPC_HIOR:
  434. case KVM_CAP_PPC_PAPR:
  435. #endif
  436. case KVM_CAP_PPC_UNSET_IRQ:
  437. case KVM_CAP_PPC_IRQ_LEVEL:
  438. case KVM_CAP_ENABLE_CAP:
  439. case KVM_CAP_ENABLE_CAP_VM:
  440. case KVM_CAP_ONE_REG:
  441. case KVM_CAP_IOEVENTFD:
  442. case KVM_CAP_DEVICE_CTRL:
  443. case KVM_CAP_IMMEDIATE_EXIT:
  444. r = 1;
  445. break;
  446. case KVM_CAP_PPC_PAIRED_SINGLES:
  447. case KVM_CAP_PPC_OSI:
  448. case KVM_CAP_PPC_GET_PVINFO:
  449. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  450. case KVM_CAP_SW_TLB:
  451. #endif
  452. /* We support this only for PR */
  453. r = !hv_enabled;
  454. break;
  455. #ifdef CONFIG_KVM_MPIC
  456. case KVM_CAP_IRQ_MPIC:
  457. r = 1;
  458. break;
  459. #endif
  460. #ifdef CONFIG_PPC_BOOK3S_64
  461. case KVM_CAP_SPAPR_TCE:
  462. case KVM_CAP_SPAPR_TCE_64:
  463. /* fallthrough */
  464. case KVM_CAP_SPAPR_TCE_VFIO:
  465. case KVM_CAP_PPC_RTAS:
  466. case KVM_CAP_PPC_FIXUP_HCALL:
  467. case KVM_CAP_PPC_ENABLE_HCALL:
  468. #ifdef CONFIG_KVM_XICS
  469. case KVM_CAP_IRQ_XICS:
  470. #endif
  471. r = 1;
  472. break;
  473. case KVM_CAP_PPC_ALLOC_HTAB:
  474. r = hv_enabled;
  475. break;
  476. #endif /* CONFIG_PPC_BOOK3S_64 */
  477. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  478. case KVM_CAP_PPC_SMT:
  479. r = 0;
  480. if (kvm) {
  481. if (kvm->arch.emul_smt_mode > 1)
  482. r = kvm->arch.emul_smt_mode;
  483. else
  484. r = kvm->arch.smt_mode;
  485. } else if (hv_enabled) {
  486. if (cpu_has_feature(CPU_FTR_ARCH_300))
  487. r = 1;
  488. else
  489. r = threads_per_subcore;
  490. }
  491. break;
  492. case KVM_CAP_PPC_SMT_POSSIBLE:
  493. r = 1;
  494. if (hv_enabled) {
  495. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  496. r = ((threads_per_subcore << 1) - 1);
  497. else
  498. /* P9 can emulate dbells, so allow any mode */
  499. r = 8 | 4 | 2 | 1;
  500. }
  501. break;
  502. case KVM_CAP_PPC_RMA:
  503. r = 0;
  504. break;
  505. case KVM_CAP_PPC_HWRNG:
  506. r = kvmppc_hwrng_present();
  507. break;
  508. case KVM_CAP_PPC_MMU_RADIX:
  509. r = !!(hv_enabled && radix_enabled());
  510. break;
  511. case KVM_CAP_PPC_MMU_HASH_V3:
  512. r = !!(hv_enabled && !radix_enabled() &&
  513. cpu_has_feature(CPU_FTR_ARCH_300));
  514. break;
  515. #endif
  516. case KVM_CAP_SYNC_MMU:
  517. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  518. r = hv_enabled;
  519. #elif defined(KVM_ARCH_WANT_MMU_NOTIFIER)
  520. r = 1;
  521. #else
  522. r = 0;
  523. #endif
  524. break;
  525. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  526. case KVM_CAP_PPC_HTAB_FD:
  527. r = hv_enabled;
  528. break;
  529. #endif
  530. case KVM_CAP_NR_VCPUS:
  531. /*
  532. * Recommending a number of CPUs is somewhat arbitrary; we
  533. * return the number of present CPUs for -HV (since a host
  534. * will have secondary threads "offline"), and for other KVM
  535. * implementations just count online CPUs.
  536. */
  537. if (hv_enabled)
  538. r = num_present_cpus();
  539. else
  540. r = num_online_cpus();
  541. break;
  542. case KVM_CAP_NR_MEMSLOTS:
  543. r = KVM_USER_MEM_SLOTS;
  544. break;
  545. case KVM_CAP_MAX_VCPUS:
  546. r = KVM_MAX_VCPUS;
  547. break;
  548. #ifdef CONFIG_PPC_BOOK3S_64
  549. case KVM_CAP_PPC_GET_SMMU_INFO:
  550. r = 1;
  551. break;
  552. case KVM_CAP_SPAPR_MULTITCE:
  553. r = 1;
  554. break;
  555. case KVM_CAP_SPAPR_RESIZE_HPT:
  556. /* Disable this on POWER9 until code handles new HPTE format */
  557. r = !!hv_enabled && !cpu_has_feature(CPU_FTR_ARCH_300);
  558. break;
  559. #endif
  560. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  561. case KVM_CAP_PPC_FWNMI:
  562. r = hv_enabled;
  563. break;
  564. #endif
  565. case KVM_CAP_PPC_HTM:
  566. r = cpu_has_feature(CPU_FTR_TM_COMP) && hv_enabled;
  567. break;
  568. default:
  569. r = 0;
  570. break;
  571. }
  572. return r;
  573. }
  574. long kvm_arch_dev_ioctl(struct file *filp,
  575. unsigned int ioctl, unsigned long arg)
  576. {
  577. return -EINVAL;
  578. }
  579. void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
  580. struct kvm_memory_slot *dont)
  581. {
  582. kvmppc_core_free_memslot(kvm, free, dont);
  583. }
  584. int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
  585. unsigned long npages)
  586. {
  587. return kvmppc_core_create_memslot(kvm, slot, npages);
  588. }
  589. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  590. struct kvm_memory_slot *memslot,
  591. const struct kvm_userspace_memory_region *mem,
  592. enum kvm_mr_change change)
  593. {
  594. return kvmppc_core_prepare_memory_region(kvm, memslot, mem);
  595. }
  596. void kvm_arch_commit_memory_region(struct kvm *kvm,
  597. const struct kvm_userspace_memory_region *mem,
  598. const struct kvm_memory_slot *old,
  599. const struct kvm_memory_slot *new,
  600. enum kvm_mr_change change)
  601. {
  602. kvmppc_core_commit_memory_region(kvm, mem, old, new);
  603. }
  604. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  605. struct kvm_memory_slot *slot)
  606. {
  607. kvmppc_core_flush_memslot(kvm, slot);
  608. }
  609. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  610. {
  611. struct kvm_vcpu *vcpu;
  612. vcpu = kvmppc_core_vcpu_create(kvm, id);
  613. if (!IS_ERR(vcpu)) {
  614. vcpu->arch.wqp = &vcpu->wq;
  615. kvmppc_create_vcpu_debugfs(vcpu, id);
  616. }
  617. return vcpu;
  618. }
  619. void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  620. {
  621. }
  622. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  623. {
  624. /* Make sure we're not using the vcpu anymore */
  625. hrtimer_cancel(&vcpu->arch.dec_timer);
  626. kvmppc_remove_vcpu_debugfs(vcpu);
  627. switch (vcpu->arch.irq_type) {
  628. case KVMPPC_IRQ_MPIC:
  629. kvmppc_mpic_disconnect_vcpu(vcpu->arch.mpic, vcpu);
  630. break;
  631. case KVMPPC_IRQ_XICS:
  632. if (xive_enabled())
  633. kvmppc_xive_cleanup_vcpu(vcpu);
  634. else
  635. kvmppc_xics_free_icp(vcpu);
  636. break;
  637. }
  638. kvmppc_core_vcpu_free(vcpu);
  639. }
  640. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  641. {
  642. kvm_arch_vcpu_free(vcpu);
  643. }
  644. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  645. {
  646. return kvmppc_core_pending_dec(vcpu);
  647. }
  648. static enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
  649. {
  650. struct kvm_vcpu *vcpu;
  651. vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
  652. kvmppc_decrementer_func(vcpu);
  653. return HRTIMER_NORESTART;
  654. }
  655. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  656. {
  657. int ret;
  658. hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  659. vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
  660. vcpu->arch.dec_expires = ~(u64)0;
  661. #ifdef CONFIG_KVM_EXIT_TIMING
  662. mutex_init(&vcpu->arch.exit_timing_lock);
  663. #endif
  664. ret = kvmppc_subarch_vcpu_init(vcpu);
  665. return ret;
  666. }
  667. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  668. {
  669. kvmppc_mmu_destroy(vcpu);
  670. kvmppc_subarch_vcpu_uninit(vcpu);
  671. }
  672. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  673. {
  674. #ifdef CONFIG_BOOKE
  675. /*
  676. * vrsave (formerly usprg0) isn't used by Linux, but may
  677. * be used by the guest.
  678. *
  679. * On non-booke this is associated with Altivec and
  680. * is handled by code in book3s.c.
  681. */
  682. mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
  683. #endif
  684. kvmppc_core_vcpu_load(vcpu, cpu);
  685. }
  686. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  687. {
  688. kvmppc_core_vcpu_put(vcpu);
  689. #ifdef CONFIG_BOOKE
  690. vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
  691. #endif
  692. }
  693. /*
  694. * irq_bypass_add_producer and irq_bypass_del_producer are only
  695. * useful if the architecture supports PCI passthrough.
  696. * irq_bypass_stop and irq_bypass_start are not needed and so
  697. * kvm_ops are not defined for them.
  698. */
  699. bool kvm_arch_has_irq_bypass(void)
  700. {
  701. return ((kvmppc_hv_ops && kvmppc_hv_ops->irq_bypass_add_producer) ||
  702. (kvmppc_pr_ops && kvmppc_pr_ops->irq_bypass_add_producer));
  703. }
  704. int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
  705. struct irq_bypass_producer *prod)
  706. {
  707. struct kvm_kernel_irqfd *irqfd =
  708. container_of(cons, struct kvm_kernel_irqfd, consumer);
  709. struct kvm *kvm = irqfd->kvm;
  710. if (kvm->arch.kvm_ops->irq_bypass_add_producer)
  711. return kvm->arch.kvm_ops->irq_bypass_add_producer(cons, prod);
  712. return 0;
  713. }
  714. void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
  715. struct irq_bypass_producer *prod)
  716. {
  717. struct kvm_kernel_irqfd *irqfd =
  718. container_of(cons, struct kvm_kernel_irqfd, consumer);
  719. struct kvm *kvm = irqfd->kvm;
  720. if (kvm->arch.kvm_ops->irq_bypass_del_producer)
  721. kvm->arch.kvm_ops->irq_bypass_del_producer(cons, prod);
  722. }
  723. #ifdef CONFIG_VSX
  724. static inline int kvmppc_get_vsr_dword_offset(int index)
  725. {
  726. int offset;
  727. if ((index != 0) && (index != 1))
  728. return -1;
  729. #ifdef __BIG_ENDIAN
  730. offset = index;
  731. #else
  732. offset = 1 - index;
  733. #endif
  734. return offset;
  735. }
  736. static inline int kvmppc_get_vsr_word_offset(int index)
  737. {
  738. int offset;
  739. if ((index > 3) || (index < 0))
  740. return -1;
  741. #ifdef __BIG_ENDIAN
  742. offset = index;
  743. #else
  744. offset = 3 - index;
  745. #endif
  746. return offset;
  747. }
  748. static inline void kvmppc_set_vsr_dword(struct kvm_vcpu *vcpu,
  749. u64 gpr)
  750. {
  751. union kvmppc_one_reg val;
  752. int offset = kvmppc_get_vsr_dword_offset(vcpu->arch.mmio_vsx_offset);
  753. int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
  754. if (offset == -1)
  755. return;
  756. if (vcpu->arch.mmio_vsx_tx_sx_enabled) {
  757. val.vval = VCPU_VSX_VR(vcpu, index);
  758. val.vsxval[offset] = gpr;
  759. VCPU_VSX_VR(vcpu, index) = val.vval;
  760. } else {
  761. VCPU_VSX_FPR(vcpu, index, offset) = gpr;
  762. }
  763. }
  764. static inline void kvmppc_set_vsr_dword_dump(struct kvm_vcpu *vcpu,
  765. u64 gpr)
  766. {
  767. union kvmppc_one_reg val;
  768. int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
  769. if (vcpu->arch.mmio_vsx_tx_sx_enabled) {
  770. val.vval = VCPU_VSX_VR(vcpu, index);
  771. val.vsxval[0] = gpr;
  772. val.vsxval[1] = gpr;
  773. VCPU_VSX_VR(vcpu, index) = val.vval;
  774. } else {
  775. VCPU_VSX_FPR(vcpu, index, 0) = gpr;
  776. VCPU_VSX_FPR(vcpu, index, 1) = gpr;
  777. }
  778. }
  779. static inline void kvmppc_set_vsr_word(struct kvm_vcpu *vcpu,
  780. u32 gpr32)
  781. {
  782. union kvmppc_one_reg val;
  783. int offset = kvmppc_get_vsr_word_offset(vcpu->arch.mmio_vsx_offset);
  784. int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
  785. int dword_offset, word_offset;
  786. if (offset == -1)
  787. return;
  788. if (vcpu->arch.mmio_vsx_tx_sx_enabled) {
  789. val.vval = VCPU_VSX_VR(vcpu, index);
  790. val.vsx32val[offset] = gpr32;
  791. VCPU_VSX_VR(vcpu, index) = val.vval;
  792. } else {
  793. dword_offset = offset / 2;
  794. word_offset = offset % 2;
  795. val.vsxval[0] = VCPU_VSX_FPR(vcpu, index, dword_offset);
  796. val.vsx32val[word_offset] = gpr32;
  797. VCPU_VSX_FPR(vcpu, index, dword_offset) = val.vsxval[0];
  798. }
  799. }
  800. #endif /* CONFIG_VSX */
  801. #ifdef CONFIG_PPC_FPU
  802. static inline u64 sp_to_dp(u32 fprs)
  803. {
  804. u64 fprd;
  805. preempt_disable();
  806. enable_kernel_fp();
  807. asm ("lfs%U1%X1 0,%1; stfd%U0%X0 0,%0" : "=m" (fprd) : "m" (fprs)
  808. : "fr0");
  809. preempt_enable();
  810. return fprd;
  811. }
  812. static inline u32 dp_to_sp(u64 fprd)
  813. {
  814. u32 fprs;
  815. preempt_disable();
  816. enable_kernel_fp();
  817. asm ("lfd%U1%X1 0,%1; stfs%U0%X0 0,%0" : "=m" (fprs) : "m" (fprd)
  818. : "fr0");
  819. preempt_enable();
  820. return fprs;
  821. }
  822. #else
  823. #define sp_to_dp(x) (x)
  824. #define dp_to_sp(x) (x)
  825. #endif /* CONFIG_PPC_FPU */
  826. static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
  827. struct kvm_run *run)
  828. {
  829. u64 uninitialized_var(gpr);
  830. if (run->mmio.len > sizeof(gpr)) {
  831. printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
  832. return;
  833. }
  834. if (!vcpu->arch.mmio_host_swabbed) {
  835. switch (run->mmio.len) {
  836. case 8: gpr = *(u64 *)run->mmio.data; break;
  837. case 4: gpr = *(u32 *)run->mmio.data; break;
  838. case 2: gpr = *(u16 *)run->mmio.data; break;
  839. case 1: gpr = *(u8 *)run->mmio.data; break;
  840. }
  841. } else {
  842. switch (run->mmio.len) {
  843. case 8: gpr = swab64(*(u64 *)run->mmio.data); break;
  844. case 4: gpr = swab32(*(u32 *)run->mmio.data); break;
  845. case 2: gpr = swab16(*(u16 *)run->mmio.data); break;
  846. case 1: gpr = *(u8 *)run->mmio.data; break;
  847. }
  848. }
  849. /* conversion between single and double precision */
  850. if ((vcpu->arch.mmio_sp64_extend) && (run->mmio.len == 4))
  851. gpr = sp_to_dp(gpr);
  852. if (vcpu->arch.mmio_sign_extend) {
  853. switch (run->mmio.len) {
  854. #ifdef CONFIG_PPC64
  855. case 4:
  856. gpr = (s64)(s32)gpr;
  857. break;
  858. #endif
  859. case 2:
  860. gpr = (s64)(s16)gpr;
  861. break;
  862. case 1:
  863. gpr = (s64)(s8)gpr;
  864. break;
  865. }
  866. }
  867. switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
  868. case KVM_MMIO_REG_GPR:
  869. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  870. break;
  871. case KVM_MMIO_REG_FPR:
  872. VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
  873. break;
  874. #ifdef CONFIG_PPC_BOOK3S
  875. case KVM_MMIO_REG_QPR:
  876. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  877. break;
  878. case KVM_MMIO_REG_FQPR:
  879. VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
  880. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  881. break;
  882. #endif
  883. #ifdef CONFIG_VSX
  884. case KVM_MMIO_REG_VSX:
  885. if (vcpu->arch.mmio_vsx_copy_type == KVMPPC_VSX_COPY_DWORD)
  886. kvmppc_set_vsr_dword(vcpu, gpr);
  887. else if (vcpu->arch.mmio_vsx_copy_type == KVMPPC_VSX_COPY_WORD)
  888. kvmppc_set_vsr_word(vcpu, gpr);
  889. else if (vcpu->arch.mmio_vsx_copy_type ==
  890. KVMPPC_VSX_COPY_DWORD_LOAD_DUMP)
  891. kvmppc_set_vsr_dword_dump(vcpu, gpr);
  892. break;
  893. #endif
  894. default:
  895. BUG();
  896. }
  897. }
  898. static int __kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  899. unsigned int rt, unsigned int bytes,
  900. int is_default_endian, int sign_extend)
  901. {
  902. int idx, ret;
  903. bool host_swabbed;
  904. /* Pity C doesn't have a logical XOR operator */
  905. if (kvmppc_need_byteswap(vcpu)) {
  906. host_swabbed = is_default_endian;
  907. } else {
  908. host_swabbed = !is_default_endian;
  909. }
  910. if (bytes > sizeof(run->mmio.data)) {
  911. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  912. run->mmio.len);
  913. }
  914. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  915. run->mmio.len = bytes;
  916. run->mmio.is_write = 0;
  917. vcpu->arch.io_gpr = rt;
  918. vcpu->arch.mmio_host_swabbed = host_swabbed;
  919. vcpu->mmio_needed = 1;
  920. vcpu->mmio_is_write = 0;
  921. vcpu->arch.mmio_sign_extend = sign_extend;
  922. idx = srcu_read_lock(&vcpu->kvm->srcu);
  923. ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
  924. bytes, &run->mmio.data);
  925. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  926. if (!ret) {
  927. kvmppc_complete_mmio_load(vcpu, run);
  928. vcpu->mmio_needed = 0;
  929. return EMULATE_DONE;
  930. }
  931. return EMULATE_DO_MMIO;
  932. }
  933. int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  934. unsigned int rt, unsigned int bytes,
  935. int is_default_endian)
  936. {
  937. return __kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian, 0);
  938. }
  939. EXPORT_SYMBOL_GPL(kvmppc_handle_load);
  940. /* Same as above, but sign extends */
  941. int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
  942. unsigned int rt, unsigned int bytes,
  943. int is_default_endian)
  944. {
  945. return __kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian, 1);
  946. }
  947. #ifdef CONFIG_VSX
  948. int kvmppc_handle_vsx_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  949. unsigned int rt, unsigned int bytes,
  950. int is_default_endian, int mmio_sign_extend)
  951. {
  952. enum emulation_result emulated = EMULATE_DONE;
  953. /* Currently, mmio_vsx_copy_nums only allowed to be less than 4 */
  954. if ( (vcpu->arch.mmio_vsx_copy_nums > 4) ||
  955. (vcpu->arch.mmio_vsx_copy_nums < 0) ) {
  956. return EMULATE_FAIL;
  957. }
  958. while (vcpu->arch.mmio_vsx_copy_nums) {
  959. emulated = __kvmppc_handle_load(run, vcpu, rt, bytes,
  960. is_default_endian, mmio_sign_extend);
  961. if (emulated != EMULATE_DONE)
  962. break;
  963. vcpu->arch.paddr_accessed += run->mmio.len;
  964. vcpu->arch.mmio_vsx_copy_nums--;
  965. vcpu->arch.mmio_vsx_offset++;
  966. }
  967. return emulated;
  968. }
  969. #endif /* CONFIG_VSX */
  970. int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
  971. u64 val, unsigned int bytes, int is_default_endian)
  972. {
  973. void *data = run->mmio.data;
  974. int idx, ret;
  975. bool host_swabbed;
  976. /* Pity C doesn't have a logical XOR operator */
  977. if (kvmppc_need_byteswap(vcpu)) {
  978. host_swabbed = is_default_endian;
  979. } else {
  980. host_swabbed = !is_default_endian;
  981. }
  982. if (bytes > sizeof(run->mmio.data)) {
  983. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  984. run->mmio.len);
  985. }
  986. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  987. run->mmio.len = bytes;
  988. run->mmio.is_write = 1;
  989. vcpu->mmio_needed = 1;
  990. vcpu->mmio_is_write = 1;
  991. if ((vcpu->arch.mmio_sp64_extend) && (bytes == 4))
  992. val = dp_to_sp(val);
  993. /* Store the value at the lowest bytes in 'data'. */
  994. if (!host_swabbed) {
  995. switch (bytes) {
  996. case 8: *(u64 *)data = val; break;
  997. case 4: *(u32 *)data = val; break;
  998. case 2: *(u16 *)data = val; break;
  999. case 1: *(u8 *)data = val; break;
  1000. }
  1001. } else {
  1002. switch (bytes) {
  1003. case 8: *(u64 *)data = swab64(val); break;
  1004. case 4: *(u32 *)data = swab32(val); break;
  1005. case 2: *(u16 *)data = swab16(val); break;
  1006. case 1: *(u8 *)data = val; break;
  1007. }
  1008. }
  1009. idx = srcu_read_lock(&vcpu->kvm->srcu);
  1010. ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
  1011. bytes, &run->mmio.data);
  1012. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  1013. if (!ret) {
  1014. vcpu->mmio_needed = 0;
  1015. return EMULATE_DONE;
  1016. }
  1017. return EMULATE_DO_MMIO;
  1018. }
  1019. EXPORT_SYMBOL_GPL(kvmppc_handle_store);
  1020. #ifdef CONFIG_VSX
  1021. static inline int kvmppc_get_vsr_data(struct kvm_vcpu *vcpu, int rs, u64 *val)
  1022. {
  1023. u32 dword_offset, word_offset;
  1024. union kvmppc_one_reg reg;
  1025. int vsx_offset = 0;
  1026. int copy_type = vcpu->arch.mmio_vsx_copy_type;
  1027. int result = 0;
  1028. switch (copy_type) {
  1029. case KVMPPC_VSX_COPY_DWORD:
  1030. vsx_offset =
  1031. kvmppc_get_vsr_dword_offset(vcpu->arch.mmio_vsx_offset);
  1032. if (vsx_offset == -1) {
  1033. result = -1;
  1034. break;
  1035. }
  1036. if (!vcpu->arch.mmio_vsx_tx_sx_enabled) {
  1037. *val = VCPU_VSX_FPR(vcpu, rs, vsx_offset);
  1038. } else {
  1039. reg.vval = VCPU_VSX_VR(vcpu, rs);
  1040. *val = reg.vsxval[vsx_offset];
  1041. }
  1042. break;
  1043. case KVMPPC_VSX_COPY_WORD:
  1044. vsx_offset =
  1045. kvmppc_get_vsr_word_offset(vcpu->arch.mmio_vsx_offset);
  1046. if (vsx_offset == -1) {
  1047. result = -1;
  1048. break;
  1049. }
  1050. if (!vcpu->arch.mmio_vsx_tx_sx_enabled) {
  1051. dword_offset = vsx_offset / 2;
  1052. word_offset = vsx_offset % 2;
  1053. reg.vsxval[0] = VCPU_VSX_FPR(vcpu, rs, dword_offset);
  1054. *val = reg.vsx32val[word_offset];
  1055. } else {
  1056. reg.vval = VCPU_VSX_VR(vcpu, rs);
  1057. *val = reg.vsx32val[vsx_offset];
  1058. }
  1059. break;
  1060. default:
  1061. result = -1;
  1062. break;
  1063. }
  1064. return result;
  1065. }
  1066. int kvmppc_handle_vsx_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
  1067. int rs, unsigned int bytes, int is_default_endian)
  1068. {
  1069. u64 val;
  1070. enum emulation_result emulated = EMULATE_DONE;
  1071. vcpu->arch.io_gpr = rs;
  1072. /* Currently, mmio_vsx_copy_nums only allowed to be less than 4 */
  1073. if ( (vcpu->arch.mmio_vsx_copy_nums > 4) ||
  1074. (vcpu->arch.mmio_vsx_copy_nums < 0) ) {
  1075. return EMULATE_FAIL;
  1076. }
  1077. while (vcpu->arch.mmio_vsx_copy_nums) {
  1078. if (kvmppc_get_vsr_data(vcpu, rs, &val) == -1)
  1079. return EMULATE_FAIL;
  1080. emulated = kvmppc_handle_store(run, vcpu,
  1081. val, bytes, is_default_endian);
  1082. if (emulated != EMULATE_DONE)
  1083. break;
  1084. vcpu->arch.paddr_accessed += run->mmio.len;
  1085. vcpu->arch.mmio_vsx_copy_nums--;
  1086. vcpu->arch.mmio_vsx_offset++;
  1087. }
  1088. return emulated;
  1089. }
  1090. static int kvmppc_emulate_mmio_vsx_loadstore(struct kvm_vcpu *vcpu,
  1091. struct kvm_run *run)
  1092. {
  1093. enum emulation_result emulated = EMULATE_FAIL;
  1094. int r;
  1095. vcpu->arch.paddr_accessed += run->mmio.len;
  1096. if (!vcpu->mmio_is_write) {
  1097. emulated = kvmppc_handle_vsx_load(run, vcpu, vcpu->arch.io_gpr,
  1098. run->mmio.len, 1, vcpu->arch.mmio_sign_extend);
  1099. } else {
  1100. emulated = kvmppc_handle_vsx_store(run, vcpu,
  1101. vcpu->arch.io_gpr, run->mmio.len, 1);
  1102. }
  1103. switch (emulated) {
  1104. case EMULATE_DO_MMIO:
  1105. run->exit_reason = KVM_EXIT_MMIO;
  1106. r = RESUME_HOST;
  1107. break;
  1108. case EMULATE_FAIL:
  1109. pr_info("KVM: MMIO emulation failed (VSX repeat)\n");
  1110. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  1111. run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  1112. r = RESUME_HOST;
  1113. break;
  1114. default:
  1115. r = RESUME_GUEST;
  1116. break;
  1117. }
  1118. return r;
  1119. }
  1120. #endif /* CONFIG_VSX */
  1121. int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  1122. {
  1123. int r = 0;
  1124. union kvmppc_one_reg val;
  1125. int size;
  1126. size = one_reg_size(reg->id);
  1127. if (size > sizeof(val))
  1128. return -EINVAL;
  1129. r = kvmppc_get_one_reg(vcpu, reg->id, &val);
  1130. if (r == -EINVAL) {
  1131. r = 0;
  1132. switch (reg->id) {
  1133. #ifdef CONFIG_ALTIVEC
  1134. case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
  1135. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  1136. r = -ENXIO;
  1137. break;
  1138. }
  1139. val.vval = vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0];
  1140. break;
  1141. case KVM_REG_PPC_VSCR:
  1142. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  1143. r = -ENXIO;
  1144. break;
  1145. }
  1146. val = get_reg_val(reg->id, vcpu->arch.vr.vscr.u[3]);
  1147. break;
  1148. case KVM_REG_PPC_VRSAVE:
  1149. val = get_reg_val(reg->id, vcpu->arch.vrsave);
  1150. break;
  1151. #endif /* CONFIG_ALTIVEC */
  1152. default:
  1153. r = -EINVAL;
  1154. break;
  1155. }
  1156. }
  1157. if (r)
  1158. return r;
  1159. if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size))
  1160. r = -EFAULT;
  1161. return r;
  1162. }
  1163. int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  1164. {
  1165. int r;
  1166. union kvmppc_one_reg val;
  1167. int size;
  1168. size = one_reg_size(reg->id);
  1169. if (size > sizeof(val))
  1170. return -EINVAL;
  1171. if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size))
  1172. return -EFAULT;
  1173. r = kvmppc_set_one_reg(vcpu, reg->id, &val);
  1174. if (r == -EINVAL) {
  1175. r = 0;
  1176. switch (reg->id) {
  1177. #ifdef CONFIG_ALTIVEC
  1178. case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
  1179. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  1180. r = -ENXIO;
  1181. break;
  1182. }
  1183. vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0] = val.vval;
  1184. break;
  1185. case KVM_REG_PPC_VSCR:
  1186. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  1187. r = -ENXIO;
  1188. break;
  1189. }
  1190. vcpu->arch.vr.vscr.u[3] = set_reg_val(reg->id, val);
  1191. break;
  1192. case KVM_REG_PPC_VRSAVE:
  1193. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  1194. r = -ENXIO;
  1195. break;
  1196. }
  1197. vcpu->arch.vrsave = set_reg_val(reg->id, val);
  1198. break;
  1199. #endif /* CONFIG_ALTIVEC */
  1200. default:
  1201. r = -EINVAL;
  1202. break;
  1203. }
  1204. }
  1205. return r;
  1206. }
  1207. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  1208. {
  1209. int r;
  1210. sigset_t sigsaved;
  1211. if (vcpu->mmio_needed) {
  1212. vcpu->mmio_needed = 0;
  1213. if (!vcpu->mmio_is_write)
  1214. kvmppc_complete_mmio_load(vcpu, run);
  1215. #ifdef CONFIG_VSX
  1216. if (vcpu->arch.mmio_vsx_copy_nums > 0) {
  1217. vcpu->arch.mmio_vsx_copy_nums--;
  1218. vcpu->arch.mmio_vsx_offset++;
  1219. }
  1220. if (vcpu->arch.mmio_vsx_copy_nums > 0) {
  1221. r = kvmppc_emulate_mmio_vsx_loadstore(vcpu, run);
  1222. if (r == RESUME_HOST) {
  1223. vcpu->mmio_needed = 1;
  1224. return r;
  1225. }
  1226. }
  1227. #endif
  1228. } else if (vcpu->arch.osi_needed) {
  1229. u64 *gprs = run->osi.gprs;
  1230. int i;
  1231. for (i = 0; i < 32; i++)
  1232. kvmppc_set_gpr(vcpu, i, gprs[i]);
  1233. vcpu->arch.osi_needed = 0;
  1234. } else if (vcpu->arch.hcall_needed) {
  1235. int i;
  1236. kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
  1237. for (i = 0; i < 9; ++i)
  1238. kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
  1239. vcpu->arch.hcall_needed = 0;
  1240. #ifdef CONFIG_BOOKE
  1241. } else if (vcpu->arch.epr_needed) {
  1242. kvmppc_set_epr(vcpu, run->epr.epr);
  1243. vcpu->arch.epr_needed = 0;
  1244. #endif
  1245. }
  1246. if (vcpu->sigset_active)
  1247. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  1248. if (run->immediate_exit)
  1249. r = -EINTR;
  1250. else
  1251. r = kvmppc_vcpu_run(run, vcpu);
  1252. if (vcpu->sigset_active)
  1253. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1254. return r;
  1255. }
  1256. int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
  1257. {
  1258. if (irq->irq == KVM_INTERRUPT_UNSET) {
  1259. kvmppc_core_dequeue_external(vcpu);
  1260. return 0;
  1261. }
  1262. kvmppc_core_queue_external(vcpu, irq);
  1263. kvm_vcpu_kick(vcpu);
  1264. return 0;
  1265. }
  1266. static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
  1267. struct kvm_enable_cap *cap)
  1268. {
  1269. int r;
  1270. if (cap->flags)
  1271. return -EINVAL;
  1272. switch (cap->cap) {
  1273. case KVM_CAP_PPC_OSI:
  1274. r = 0;
  1275. vcpu->arch.osi_enabled = true;
  1276. break;
  1277. case KVM_CAP_PPC_PAPR:
  1278. r = 0;
  1279. vcpu->arch.papr_enabled = true;
  1280. break;
  1281. case KVM_CAP_PPC_EPR:
  1282. r = 0;
  1283. if (cap->args[0])
  1284. vcpu->arch.epr_flags |= KVMPPC_EPR_USER;
  1285. else
  1286. vcpu->arch.epr_flags &= ~KVMPPC_EPR_USER;
  1287. break;
  1288. #ifdef CONFIG_BOOKE
  1289. case KVM_CAP_PPC_BOOKE_WATCHDOG:
  1290. r = 0;
  1291. vcpu->arch.watchdog_enabled = true;
  1292. break;
  1293. #endif
  1294. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  1295. case KVM_CAP_SW_TLB: {
  1296. struct kvm_config_tlb cfg;
  1297. void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
  1298. r = -EFAULT;
  1299. if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
  1300. break;
  1301. r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
  1302. break;
  1303. }
  1304. #endif
  1305. #ifdef CONFIG_KVM_MPIC
  1306. case KVM_CAP_IRQ_MPIC: {
  1307. struct fd f;
  1308. struct kvm_device *dev;
  1309. r = -EBADF;
  1310. f = fdget(cap->args[0]);
  1311. if (!f.file)
  1312. break;
  1313. r = -EPERM;
  1314. dev = kvm_device_from_filp(f.file);
  1315. if (dev)
  1316. r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]);
  1317. fdput(f);
  1318. break;
  1319. }
  1320. #endif
  1321. #ifdef CONFIG_KVM_XICS
  1322. case KVM_CAP_IRQ_XICS: {
  1323. struct fd f;
  1324. struct kvm_device *dev;
  1325. r = -EBADF;
  1326. f = fdget(cap->args[0]);
  1327. if (!f.file)
  1328. break;
  1329. r = -EPERM;
  1330. dev = kvm_device_from_filp(f.file);
  1331. if (dev) {
  1332. if (xive_enabled())
  1333. r = kvmppc_xive_connect_vcpu(dev, vcpu, cap->args[1]);
  1334. else
  1335. r = kvmppc_xics_connect_vcpu(dev, vcpu, cap->args[1]);
  1336. }
  1337. fdput(f);
  1338. break;
  1339. }
  1340. #endif /* CONFIG_KVM_XICS */
  1341. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  1342. case KVM_CAP_PPC_FWNMI:
  1343. r = -EINVAL;
  1344. if (!is_kvmppc_hv_enabled(vcpu->kvm))
  1345. break;
  1346. r = 0;
  1347. vcpu->kvm->arch.fwnmi_enabled = true;
  1348. break;
  1349. #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
  1350. default:
  1351. r = -EINVAL;
  1352. break;
  1353. }
  1354. if (!r)
  1355. r = kvmppc_sanity_check(vcpu);
  1356. return r;
  1357. }
  1358. bool kvm_arch_intc_initialized(struct kvm *kvm)
  1359. {
  1360. #ifdef CONFIG_KVM_MPIC
  1361. if (kvm->arch.mpic)
  1362. return true;
  1363. #endif
  1364. #ifdef CONFIG_KVM_XICS
  1365. if (kvm->arch.xics || kvm->arch.xive)
  1366. return true;
  1367. #endif
  1368. return false;
  1369. }
  1370. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  1371. struct kvm_mp_state *mp_state)
  1372. {
  1373. return -EINVAL;
  1374. }
  1375. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  1376. struct kvm_mp_state *mp_state)
  1377. {
  1378. return -EINVAL;
  1379. }
  1380. long kvm_arch_vcpu_ioctl(struct file *filp,
  1381. unsigned int ioctl, unsigned long arg)
  1382. {
  1383. struct kvm_vcpu *vcpu = filp->private_data;
  1384. void __user *argp = (void __user *)arg;
  1385. long r;
  1386. switch (ioctl) {
  1387. case KVM_INTERRUPT: {
  1388. struct kvm_interrupt irq;
  1389. r = -EFAULT;
  1390. if (copy_from_user(&irq, argp, sizeof(irq)))
  1391. goto out;
  1392. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  1393. goto out;
  1394. }
  1395. case KVM_ENABLE_CAP:
  1396. {
  1397. struct kvm_enable_cap cap;
  1398. r = -EFAULT;
  1399. if (copy_from_user(&cap, argp, sizeof(cap)))
  1400. goto out;
  1401. r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
  1402. break;
  1403. }
  1404. case KVM_SET_ONE_REG:
  1405. case KVM_GET_ONE_REG:
  1406. {
  1407. struct kvm_one_reg reg;
  1408. r = -EFAULT;
  1409. if (copy_from_user(&reg, argp, sizeof(reg)))
  1410. goto out;
  1411. if (ioctl == KVM_SET_ONE_REG)
  1412. r = kvm_vcpu_ioctl_set_one_reg(vcpu, &reg);
  1413. else
  1414. r = kvm_vcpu_ioctl_get_one_reg(vcpu, &reg);
  1415. break;
  1416. }
  1417. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  1418. case KVM_DIRTY_TLB: {
  1419. struct kvm_dirty_tlb dirty;
  1420. r = -EFAULT;
  1421. if (copy_from_user(&dirty, argp, sizeof(dirty)))
  1422. goto out;
  1423. r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
  1424. break;
  1425. }
  1426. #endif
  1427. default:
  1428. r = -EINVAL;
  1429. }
  1430. out:
  1431. return r;
  1432. }
  1433. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  1434. {
  1435. return VM_FAULT_SIGBUS;
  1436. }
  1437. static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
  1438. {
  1439. u32 inst_nop = 0x60000000;
  1440. #ifdef CONFIG_KVM_BOOKE_HV
  1441. u32 inst_sc1 = 0x44000022;
  1442. pvinfo->hcall[0] = cpu_to_be32(inst_sc1);
  1443. pvinfo->hcall[1] = cpu_to_be32(inst_nop);
  1444. pvinfo->hcall[2] = cpu_to_be32(inst_nop);
  1445. pvinfo->hcall[3] = cpu_to_be32(inst_nop);
  1446. #else
  1447. u32 inst_lis = 0x3c000000;
  1448. u32 inst_ori = 0x60000000;
  1449. u32 inst_sc = 0x44000002;
  1450. u32 inst_imm_mask = 0xffff;
  1451. /*
  1452. * The hypercall to get into KVM from within guest context is as
  1453. * follows:
  1454. *
  1455. * lis r0, r0, KVM_SC_MAGIC_R0@h
  1456. * ori r0, KVM_SC_MAGIC_R0@l
  1457. * sc
  1458. * nop
  1459. */
  1460. pvinfo->hcall[0] = cpu_to_be32(inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask));
  1461. pvinfo->hcall[1] = cpu_to_be32(inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask));
  1462. pvinfo->hcall[2] = cpu_to_be32(inst_sc);
  1463. pvinfo->hcall[3] = cpu_to_be32(inst_nop);
  1464. #endif
  1465. pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
  1466. return 0;
  1467. }
  1468. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
  1469. bool line_status)
  1470. {
  1471. if (!irqchip_in_kernel(kvm))
  1472. return -ENXIO;
  1473. irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
  1474. irq_event->irq, irq_event->level,
  1475. line_status);
  1476. return 0;
  1477. }
  1478. static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  1479. struct kvm_enable_cap *cap)
  1480. {
  1481. int r;
  1482. if (cap->flags)
  1483. return -EINVAL;
  1484. switch (cap->cap) {
  1485. #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
  1486. case KVM_CAP_PPC_ENABLE_HCALL: {
  1487. unsigned long hcall = cap->args[0];
  1488. r = -EINVAL;
  1489. if (hcall > MAX_HCALL_OPCODE || (hcall & 3) ||
  1490. cap->args[1] > 1)
  1491. break;
  1492. if (!kvmppc_book3s_hcall_implemented(kvm, hcall))
  1493. break;
  1494. if (cap->args[1])
  1495. set_bit(hcall / 4, kvm->arch.enabled_hcalls);
  1496. else
  1497. clear_bit(hcall / 4, kvm->arch.enabled_hcalls);
  1498. r = 0;
  1499. break;
  1500. }
  1501. case KVM_CAP_PPC_SMT: {
  1502. unsigned long mode = cap->args[0];
  1503. unsigned long flags = cap->args[1];
  1504. r = -EINVAL;
  1505. if (kvm->arch.kvm_ops->set_smt_mode)
  1506. r = kvm->arch.kvm_ops->set_smt_mode(kvm, mode, flags);
  1507. break;
  1508. }
  1509. #endif
  1510. default:
  1511. r = -EINVAL;
  1512. break;
  1513. }
  1514. return r;
  1515. }
  1516. long kvm_arch_vm_ioctl(struct file *filp,
  1517. unsigned int ioctl, unsigned long arg)
  1518. {
  1519. struct kvm *kvm __maybe_unused = filp->private_data;
  1520. void __user *argp = (void __user *)arg;
  1521. long r;
  1522. switch (ioctl) {
  1523. case KVM_PPC_GET_PVINFO: {
  1524. struct kvm_ppc_pvinfo pvinfo;
  1525. memset(&pvinfo, 0, sizeof(pvinfo));
  1526. r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
  1527. if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
  1528. r = -EFAULT;
  1529. goto out;
  1530. }
  1531. break;
  1532. }
  1533. case KVM_ENABLE_CAP:
  1534. {
  1535. struct kvm_enable_cap cap;
  1536. r = -EFAULT;
  1537. if (copy_from_user(&cap, argp, sizeof(cap)))
  1538. goto out;
  1539. r = kvm_vm_ioctl_enable_cap(kvm, &cap);
  1540. break;
  1541. }
  1542. #ifdef CONFIG_SPAPR_TCE_IOMMU
  1543. case KVM_CREATE_SPAPR_TCE_64: {
  1544. struct kvm_create_spapr_tce_64 create_tce_64;
  1545. r = -EFAULT;
  1546. if (copy_from_user(&create_tce_64, argp, sizeof(create_tce_64)))
  1547. goto out;
  1548. if (create_tce_64.flags) {
  1549. r = -EINVAL;
  1550. goto out;
  1551. }
  1552. r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
  1553. goto out;
  1554. }
  1555. case KVM_CREATE_SPAPR_TCE: {
  1556. struct kvm_create_spapr_tce create_tce;
  1557. struct kvm_create_spapr_tce_64 create_tce_64;
  1558. r = -EFAULT;
  1559. if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
  1560. goto out;
  1561. create_tce_64.liobn = create_tce.liobn;
  1562. create_tce_64.page_shift = IOMMU_PAGE_SHIFT_4K;
  1563. create_tce_64.offset = 0;
  1564. create_tce_64.size = create_tce.window_size >>
  1565. IOMMU_PAGE_SHIFT_4K;
  1566. create_tce_64.flags = 0;
  1567. r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
  1568. goto out;
  1569. }
  1570. #endif
  1571. #ifdef CONFIG_PPC_BOOK3S_64
  1572. case KVM_PPC_GET_SMMU_INFO: {
  1573. struct kvm_ppc_smmu_info info;
  1574. struct kvm *kvm = filp->private_data;
  1575. memset(&info, 0, sizeof(info));
  1576. r = kvm->arch.kvm_ops->get_smmu_info(kvm, &info);
  1577. if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
  1578. r = -EFAULT;
  1579. break;
  1580. }
  1581. case KVM_PPC_RTAS_DEFINE_TOKEN: {
  1582. struct kvm *kvm = filp->private_data;
  1583. r = kvm_vm_ioctl_rtas_define_token(kvm, argp);
  1584. break;
  1585. }
  1586. case KVM_PPC_CONFIGURE_V3_MMU: {
  1587. struct kvm *kvm = filp->private_data;
  1588. struct kvm_ppc_mmuv3_cfg cfg;
  1589. r = -EINVAL;
  1590. if (!kvm->arch.kvm_ops->configure_mmu)
  1591. goto out;
  1592. r = -EFAULT;
  1593. if (copy_from_user(&cfg, argp, sizeof(cfg)))
  1594. goto out;
  1595. r = kvm->arch.kvm_ops->configure_mmu(kvm, &cfg);
  1596. break;
  1597. }
  1598. case KVM_PPC_GET_RMMU_INFO: {
  1599. struct kvm *kvm = filp->private_data;
  1600. struct kvm_ppc_rmmu_info info;
  1601. r = -EINVAL;
  1602. if (!kvm->arch.kvm_ops->get_rmmu_info)
  1603. goto out;
  1604. r = kvm->arch.kvm_ops->get_rmmu_info(kvm, &info);
  1605. if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
  1606. r = -EFAULT;
  1607. break;
  1608. }
  1609. default: {
  1610. struct kvm *kvm = filp->private_data;
  1611. r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg);
  1612. }
  1613. #else /* CONFIG_PPC_BOOK3S_64 */
  1614. default:
  1615. r = -ENOTTY;
  1616. #endif
  1617. }
  1618. out:
  1619. return r;
  1620. }
  1621. static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
  1622. static unsigned long nr_lpids;
  1623. long kvmppc_alloc_lpid(void)
  1624. {
  1625. long lpid;
  1626. do {
  1627. lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
  1628. if (lpid >= nr_lpids) {
  1629. pr_err("%s: No LPIDs free\n", __func__);
  1630. return -ENOMEM;
  1631. }
  1632. } while (test_and_set_bit(lpid, lpid_inuse));
  1633. return lpid;
  1634. }
  1635. EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid);
  1636. void kvmppc_claim_lpid(long lpid)
  1637. {
  1638. set_bit(lpid, lpid_inuse);
  1639. }
  1640. EXPORT_SYMBOL_GPL(kvmppc_claim_lpid);
  1641. void kvmppc_free_lpid(long lpid)
  1642. {
  1643. clear_bit(lpid, lpid_inuse);
  1644. }
  1645. EXPORT_SYMBOL_GPL(kvmppc_free_lpid);
  1646. void kvmppc_init_lpid(unsigned long nr_lpids_param)
  1647. {
  1648. nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
  1649. memset(lpid_inuse, 0, sizeof(lpid_inuse));
  1650. }
  1651. EXPORT_SYMBOL_GPL(kvmppc_init_lpid);
  1652. int kvm_arch_init(void *opaque)
  1653. {
  1654. return 0;
  1655. }
  1656. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ppc_instr);