powerpc.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  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/fs.h>
  26. #include <linux/slab.h>
  27. #include <linux/file.h>
  28. #include <linux/module.h>
  29. #include <asm/cputable.h>
  30. #include <asm/uaccess.h>
  31. #include <asm/kvm_ppc.h>
  32. #include <asm/tlbflush.h>
  33. #include <asm/cputhreads.h>
  34. #include <asm/irqflags.h>
  35. #include "timing.h"
  36. #include "irq.h"
  37. #include "../mm/mmu_decl.h"
  38. #define CREATE_TRACE_POINTS
  39. #include "trace.h"
  40. struct kvmppc_ops *kvmppc_hv_ops;
  41. EXPORT_SYMBOL_GPL(kvmppc_hv_ops);
  42. struct kvmppc_ops *kvmppc_pr_ops;
  43. EXPORT_SYMBOL_GPL(kvmppc_pr_ops);
  44. int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
  45. {
  46. return !!(v->arch.pending_exceptions) ||
  47. v->requests;
  48. }
  49. int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
  50. {
  51. return 1;
  52. }
  53. /*
  54. * Common checks before entering the guest world. Call with interrupts
  55. * disabled.
  56. *
  57. * returns:
  58. *
  59. * == 1 if we're ready to go into guest state
  60. * <= 0 if we need to go back to the host with return value
  61. */
  62. int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu)
  63. {
  64. int r;
  65. WARN_ON(irqs_disabled());
  66. hard_irq_disable();
  67. while (true) {
  68. if (need_resched()) {
  69. local_irq_enable();
  70. cond_resched();
  71. hard_irq_disable();
  72. continue;
  73. }
  74. if (signal_pending(current)) {
  75. kvmppc_account_exit(vcpu, SIGNAL_EXITS);
  76. vcpu->run->exit_reason = KVM_EXIT_INTR;
  77. r = -EINTR;
  78. break;
  79. }
  80. vcpu->mode = IN_GUEST_MODE;
  81. /*
  82. * Reading vcpu->requests must happen after setting vcpu->mode,
  83. * so we don't miss a request because the requester sees
  84. * OUTSIDE_GUEST_MODE and assumes we'll be checking requests
  85. * before next entering the guest (and thus doesn't IPI).
  86. */
  87. smp_mb();
  88. if (vcpu->requests) {
  89. /* Make sure we process requests preemptable */
  90. local_irq_enable();
  91. trace_kvm_check_requests(vcpu);
  92. r = kvmppc_core_check_requests(vcpu);
  93. hard_irq_disable();
  94. if (r > 0)
  95. continue;
  96. break;
  97. }
  98. if (kvmppc_core_prepare_to_enter(vcpu)) {
  99. /* interrupts got enabled in between, so we
  100. are back at square 1 */
  101. continue;
  102. }
  103. kvm_guest_enter();
  104. return 1;
  105. }
  106. /* return to host */
  107. local_irq_enable();
  108. return r;
  109. }
  110. EXPORT_SYMBOL_GPL(kvmppc_prepare_to_enter);
  111. #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
  112. static void kvmppc_swab_shared(struct kvm_vcpu *vcpu)
  113. {
  114. struct kvm_vcpu_arch_shared *shared = vcpu->arch.shared;
  115. int i;
  116. shared->sprg0 = swab64(shared->sprg0);
  117. shared->sprg1 = swab64(shared->sprg1);
  118. shared->sprg2 = swab64(shared->sprg2);
  119. shared->sprg3 = swab64(shared->sprg3);
  120. shared->srr0 = swab64(shared->srr0);
  121. shared->srr1 = swab64(shared->srr1);
  122. shared->dar = swab64(shared->dar);
  123. shared->msr = swab64(shared->msr);
  124. shared->dsisr = swab32(shared->dsisr);
  125. shared->int_pending = swab32(shared->int_pending);
  126. for (i = 0; i < ARRAY_SIZE(shared->sr); i++)
  127. shared->sr[i] = swab32(shared->sr[i]);
  128. }
  129. #endif
  130. int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
  131. {
  132. int nr = kvmppc_get_gpr(vcpu, 11);
  133. int r;
  134. unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3);
  135. unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4);
  136. unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5);
  137. unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6);
  138. unsigned long r2 = 0;
  139. if (!(kvmppc_get_msr(vcpu) & MSR_SF)) {
  140. /* 32 bit mode */
  141. param1 &= 0xffffffff;
  142. param2 &= 0xffffffff;
  143. param3 &= 0xffffffff;
  144. param4 &= 0xffffffff;
  145. }
  146. switch (nr) {
  147. case KVM_HCALL_TOKEN(KVM_HC_PPC_MAP_MAGIC_PAGE):
  148. {
  149. #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
  150. /* Book3S can be little endian, find it out here */
  151. int shared_big_endian = true;
  152. if (vcpu->arch.intr_msr & MSR_LE)
  153. shared_big_endian = false;
  154. if (shared_big_endian != vcpu->arch.shared_big_endian)
  155. kvmppc_swab_shared(vcpu);
  156. vcpu->arch.shared_big_endian = shared_big_endian;
  157. #endif
  158. if (!(param2 & MAGIC_PAGE_FLAG_NOT_MAPPED_NX)) {
  159. /*
  160. * Older versions of the Linux magic page code had
  161. * a bug where they would map their trampoline code
  162. * NX. If that's the case, remove !PR NX capability.
  163. */
  164. vcpu->arch.disable_kernel_nx = true;
  165. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  166. }
  167. vcpu->arch.magic_page_pa = param1 & ~0xfffULL;
  168. vcpu->arch.magic_page_ea = param2 & ~0xfffULL;
  169. #ifdef CONFIG_PPC_64K_PAGES
  170. /*
  171. * Make sure our 4k magic page is in the same window of a 64k
  172. * page within the guest and within the host's page.
  173. */
  174. if ((vcpu->arch.magic_page_pa & 0xf000) !=
  175. ((ulong)vcpu->arch.shared & 0xf000)) {
  176. void *old_shared = vcpu->arch.shared;
  177. ulong shared = (ulong)vcpu->arch.shared;
  178. void *new_shared;
  179. shared &= PAGE_MASK;
  180. shared |= vcpu->arch.magic_page_pa & 0xf000;
  181. new_shared = (void*)shared;
  182. memcpy(new_shared, old_shared, 0x1000);
  183. vcpu->arch.shared = new_shared;
  184. }
  185. #endif
  186. r2 = KVM_MAGIC_FEAT_SR | KVM_MAGIC_FEAT_MAS0_TO_SPRG7;
  187. r = EV_SUCCESS;
  188. break;
  189. }
  190. case KVM_HCALL_TOKEN(KVM_HC_FEATURES):
  191. r = EV_SUCCESS;
  192. #if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500V2)
  193. r2 |= (1 << KVM_FEATURE_MAGIC_PAGE);
  194. #endif
  195. /* Second return value is in r4 */
  196. break;
  197. case EV_HCALL_TOKEN(EV_IDLE):
  198. r = EV_SUCCESS;
  199. kvm_vcpu_block(vcpu);
  200. clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
  201. break;
  202. default:
  203. r = EV_UNIMPLEMENTED;
  204. break;
  205. }
  206. kvmppc_set_gpr(vcpu, 4, r2);
  207. return r;
  208. }
  209. EXPORT_SYMBOL_GPL(kvmppc_kvm_pv);
  210. int kvmppc_sanity_check(struct kvm_vcpu *vcpu)
  211. {
  212. int r = false;
  213. /* We have to know what CPU to virtualize */
  214. if (!vcpu->arch.pvr)
  215. goto out;
  216. /* PAPR only works with book3s_64 */
  217. if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled)
  218. goto out;
  219. /* HV KVM can only do PAPR mode for now */
  220. if (!vcpu->arch.papr_enabled && is_kvmppc_hv_enabled(vcpu->kvm))
  221. goto out;
  222. #ifdef CONFIG_KVM_BOOKE_HV
  223. if (!cpu_has_feature(CPU_FTR_EMB_HV))
  224. goto out;
  225. #endif
  226. r = true;
  227. out:
  228. vcpu->arch.sane = r;
  229. return r ? 0 : -EINVAL;
  230. }
  231. EXPORT_SYMBOL_GPL(kvmppc_sanity_check);
  232. int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
  233. {
  234. enum emulation_result er;
  235. int r;
  236. er = kvmppc_emulate_loadstore(vcpu);
  237. switch (er) {
  238. case EMULATE_DONE:
  239. /* Future optimization: only reload non-volatiles if they were
  240. * actually modified. */
  241. r = RESUME_GUEST_NV;
  242. break;
  243. case EMULATE_AGAIN:
  244. r = RESUME_GUEST;
  245. break;
  246. case EMULATE_DO_MMIO:
  247. run->exit_reason = KVM_EXIT_MMIO;
  248. /* We must reload nonvolatiles because "update" load/store
  249. * instructions modify register state. */
  250. /* Future optimization: only reload non-volatiles if they were
  251. * actually modified. */
  252. r = RESUME_HOST_NV;
  253. break;
  254. case EMULATE_FAIL:
  255. {
  256. u32 last_inst;
  257. kvmppc_get_last_inst(vcpu, false, &last_inst);
  258. /* XXX Deliver Program interrupt to guest. */
  259. pr_emerg("%s: emulation failed (%08x)\n", __func__, last_inst);
  260. r = RESUME_HOST;
  261. break;
  262. }
  263. default:
  264. WARN_ON(1);
  265. r = RESUME_GUEST;
  266. }
  267. return r;
  268. }
  269. EXPORT_SYMBOL_GPL(kvmppc_emulate_mmio);
  270. int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
  271. bool data)
  272. {
  273. ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK;
  274. struct kvmppc_pte pte;
  275. int r;
  276. vcpu->stat.st++;
  277. r = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
  278. XLATE_WRITE, &pte);
  279. if (r < 0)
  280. return r;
  281. *eaddr = pte.raddr;
  282. if (!pte.may_write)
  283. return -EPERM;
  284. /* Magic page override */
  285. if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
  286. ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
  287. !(kvmppc_get_msr(vcpu) & MSR_PR)) {
  288. void *magic = vcpu->arch.shared;
  289. magic += pte.eaddr & 0xfff;
  290. memcpy(magic, ptr, size);
  291. return EMULATE_DONE;
  292. }
  293. if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
  294. return EMULATE_DO_MMIO;
  295. return EMULATE_DONE;
  296. }
  297. EXPORT_SYMBOL_GPL(kvmppc_st);
  298. int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
  299. bool data)
  300. {
  301. ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK;
  302. struct kvmppc_pte pte;
  303. int rc;
  304. vcpu->stat.ld++;
  305. rc = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
  306. XLATE_READ, &pte);
  307. if (rc)
  308. return rc;
  309. *eaddr = pte.raddr;
  310. if (!pte.may_read)
  311. return -EPERM;
  312. if (!data && !pte.may_execute)
  313. return -ENOEXEC;
  314. /* Magic page override */
  315. if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
  316. ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
  317. !(kvmppc_get_msr(vcpu) & MSR_PR)) {
  318. void *magic = vcpu->arch.shared;
  319. magic += pte.eaddr & 0xfff;
  320. memcpy(ptr, magic, size);
  321. return EMULATE_DONE;
  322. }
  323. if (kvm_read_guest(vcpu->kvm, pte.raddr, ptr, size))
  324. return EMULATE_DO_MMIO;
  325. return EMULATE_DONE;
  326. }
  327. EXPORT_SYMBOL_GPL(kvmppc_ld);
  328. int kvm_arch_hardware_enable(void *garbage)
  329. {
  330. return 0;
  331. }
  332. int kvm_arch_hardware_setup(void)
  333. {
  334. return 0;
  335. }
  336. void kvm_arch_check_processor_compat(void *rtn)
  337. {
  338. *(int *)rtn = kvmppc_core_check_processor_compat();
  339. }
  340. int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
  341. {
  342. struct kvmppc_ops *kvm_ops = NULL;
  343. /*
  344. * if we have both HV and PR enabled, default is HV
  345. */
  346. if (type == 0) {
  347. if (kvmppc_hv_ops)
  348. kvm_ops = kvmppc_hv_ops;
  349. else
  350. kvm_ops = kvmppc_pr_ops;
  351. if (!kvm_ops)
  352. goto err_out;
  353. } else if (type == KVM_VM_PPC_HV) {
  354. if (!kvmppc_hv_ops)
  355. goto err_out;
  356. kvm_ops = kvmppc_hv_ops;
  357. } else if (type == KVM_VM_PPC_PR) {
  358. if (!kvmppc_pr_ops)
  359. goto err_out;
  360. kvm_ops = kvmppc_pr_ops;
  361. } else
  362. goto err_out;
  363. if (kvm_ops->owner && !try_module_get(kvm_ops->owner))
  364. return -ENOENT;
  365. kvm->arch.kvm_ops = kvm_ops;
  366. return kvmppc_core_init_vm(kvm);
  367. err_out:
  368. return -EINVAL;
  369. }
  370. void kvm_arch_destroy_vm(struct kvm *kvm)
  371. {
  372. unsigned int i;
  373. struct kvm_vcpu *vcpu;
  374. kvm_for_each_vcpu(i, vcpu, kvm)
  375. kvm_arch_vcpu_free(vcpu);
  376. mutex_lock(&kvm->lock);
  377. for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
  378. kvm->vcpus[i] = NULL;
  379. atomic_set(&kvm->online_vcpus, 0);
  380. kvmppc_core_destroy_vm(kvm);
  381. mutex_unlock(&kvm->lock);
  382. /* drop the module reference */
  383. module_put(kvm->arch.kvm_ops->owner);
  384. }
  385. int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
  386. {
  387. int r;
  388. /* Assume we're using HV mode when the HV module is loaded */
  389. int hv_enabled = kvmppc_hv_ops ? 1 : 0;
  390. if (kvm) {
  391. /*
  392. * Hooray - we know which VM type we're running on. Depend on
  393. * that rather than the guess above.
  394. */
  395. hv_enabled = is_kvmppc_hv_enabled(kvm);
  396. }
  397. switch (ext) {
  398. #ifdef CONFIG_BOOKE
  399. case KVM_CAP_PPC_BOOKE_SREGS:
  400. case KVM_CAP_PPC_BOOKE_WATCHDOG:
  401. case KVM_CAP_PPC_EPR:
  402. #else
  403. case KVM_CAP_PPC_SEGSTATE:
  404. case KVM_CAP_PPC_HIOR:
  405. case KVM_CAP_PPC_PAPR:
  406. #endif
  407. case KVM_CAP_PPC_UNSET_IRQ:
  408. case KVM_CAP_PPC_IRQ_LEVEL:
  409. case KVM_CAP_ENABLE_CAP:
  410. case KVM_CAP_ENABLE_CAP_VM:
  411. case KVM_CAP_ONE_REG:
  412. case KVM_CAP_IOEVENTFD:
  413. case KVM_CAP_DEVICE_CTRL:
  414. r = 1;
  415. break;
  416. case KVM_CAP_PPC_PAIRED_SINGLES:
  417. case KVM_CAP_PPC_OSI:
  418. case KVM_CAP_PPC_GET_PVINFO:
  419. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  420. case KVM_CAP_SW_TLB:
  421. #endif
  422. /* We support this only for PR */
  423. r = !hv_enabled;
  424. break;
  425. #ifdef CONFIG_KVM_MMIO
  426. case KVM_CAP_COALESCED_MMIO:
  427. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  428. break;
  429. #endif
  430. #ifdef CONFIG_KVM_MPIC
  431. case KVM_CAP_IRQ_MPIC:
  432. r = 1;
  433. break;
  434. #endif
  435. #ifdef CONFIG_PPC_BOOK3S_64
  436. case KVM_CAP_SPAPR_TCE:
  437. case KVM_CAP_PPC_ALLOC_HTAB:
  438. case KVM_CAP_PPC_RTAS:
  439. case KVM_CAP_PPC_FIXUP_HCALL:
  440. case KVM_CAP_PPC_ENABLE_HCALL:
  441. #ifdef CONFIG_KVM_XICS
  442. case KVM_CAP_IRQ_XICS:
  443. #endif
  444. r = 1;
  445. break;
  446. #endif /* CONFIG_PPC_BOOK3S_64 */
  447. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  448. case KVM_CAP_PPC_SMT:
  449. if (hv_enabled)
  450. r = threads_per_subcore;
  451. else
  452. r = 0;
  453. break;
  454. case KVM_CAP_PPC_RMA:
  455. r = hv_enabled;
  456. /* PPC970 requires an RMA */
  457. if (r && cpu_has_feature(CPU_FTR_ARCH_201))
  458. r = 2;
  459. break;
  460. #endif
  461. case KVM_CAP_SYNC_MMU:
  462. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  463. if (hv_enabled)
  464. r = cpu_has_feature(CPU_FTR_ARCH_206) ? 1 : 0;
  465. else
  466. r = 0;
  467. #elif defined(KVM_ARCH_WANT_MMU_NOTIFIER)
  468. r = 1;
  469. #else
  470. r = 0;
  471. #endif
  472. break;
  473. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  474. case KVM_CAP_PPC_HTAB_FD:
  475. r = hv_enabled;
  476. break;
  477. #endif
  478. case KVM_CAP_NR_VCPUS:
  479. /*
  480. * Recommending a number of CPUs is somewhat arbitrary; we
  481. * return the number of present CPUs for -HV (since a host
  482. * will have secondary threads "offline"), and for other KVM
  483. * implementations just count online CPUs.
  484. */
  485. if (hv_enabled)
  486. r = num_present_cpus();
  487. else
  488. r = num_online_cpus();
  489. break;
  490. case KVM_CAP_MAX_VCPUS:
  491. r = KVM_MAX_VCPUS;
  492. break;
  493. #ifdef CONFIG_PPC_BOOK3S_64
  494. case KVM_CAP_PPC_GET_SMMU_INFO:
  495. r = 1;
  496. break;
  497. #endif
  498. default:
  499. r = 0;
  500. break;
  501. }
  502. return r;
  503. }
  504. long kvm_arch_dev_ioctl(struct file *filp,
  505. unsigned int ioctl, unsigned long arg)
  506. {
  507. return -EINVAL;
  508. }
  509. void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
  510. struct kvm_memory_slot *dont)
  511. {
  512. kvmppc_core_free_memslot(kvm, free, dont);
  513. }
  514. int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
  515. unsigned long npages)
  516. {
  517. return kvmppc_core_create_memslot(kvm, slot, npages);
  518. }
  519. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  520. struct kvm_memory_slot *memslot,
  521. struct kvm_userspace_memory_region *mem,
  522. enum kvm_mr_change change)
  523. {
  524. return kvmppc_core_prepare_memory_region(kvm, memslot, mem);
  525. }
  526. void kvm_arch_commit_memory_region(struct kvm *kvm,
  527. struct kvm_userspace_memory_region *mem,
  528. const struct kvm_memory_slot *old,
  529. enum kvm_mr_change change)
  530. {
  531. kvmppc_core_commit_memory_region(kvm, mem, old);
  532. }
  533. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  534. struct kvm_memory_slot *slot)
  535. {
  536. kvmppc_core_flush_memslot(kvm, slot);
  537. }
  538. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  539. {
  540. struct kvm_vcpu *vcpu;
  541. vcpu = kvmppc_core_vcpu_create(kvm, id);
  542. if (!IS_ERR(vcpu)) {
  543. vcpu->arch.wqp = &vcpu->wq;
  544. kvmppc_create_vcpu_debugfs(vcpu, id);
  545. }
  546. return vcpu;
  547. }
  548. int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  549. {
  550. return 0;
  551. }
  552. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  553. {
  554. /* Make sure we're not using the vcpu anymore */
  555. hrtimer_cancel(&vcpu->arch.dec_timer);
  556. tasklet_kill(&vcpu->arch.tasklet);
  557. kvmppc_remove_vcpu_debugfs(vcpu);
  558. switch (vcpu->arch.irq_type) {
  559. case KVMPPC_IRQ_MPIC:
  560. kvmppc_mpic_disconnect_vcpu(vcpu->arch.mpic, vcpu);
  561. break;
  562. case KVMPPC_IRQ_XICS:
  563. kvmppc_xics_free_icp(vcpu);
  564. break;
  565. }
  566. kvmppc_core_vcpu_free(vcpu);
  567. }
  568. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  569. {
  570. kvm_arch_vcpu_free(vcpu);
  571. }
  572. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  573. {
  574. return kvmppc_core_pending_dec(vcpu);
  575. }
  576. /*
  577. * low level hrtimer wake routine. Because this runs in hardirq context
  578. * we schedule a tasklet to do the real work.
  579. */
  580. enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
  581. {
  582. struct kvm_vcpu *vcpu;
  583. vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
  584. tasklet_schedule(&vcpu->arch.tasklet);
  585. return HRTIMER_NORESTART;
  586. }
  587. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  588. {
  589. int ret;
  590. hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  591. tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu);
  592. vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
  593. vcpu->arch.dec_expires = ~(u64)0;
  594. #ifdef CONFIG_KVM_EXIT_TIMING
  595. mutex_init(&vcpu->arch.exit_timing_lock);
  596. #endif
  597. ret = kvmppc_subarch_vcpu_init(vcpu);
  598. return ret;
  599. }
  600. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  601. {
  602. kvmppc_mmu_destroy(vcpu);
  603. kvmppc_subarch_vcpu_uninit(vcpu);
  604. }
  605. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  606. {
  607. #ifdef CONFIG_BOOKE
  608. /*
  609. * vrsave (formerly usprg0) isn't used by Linux, but may
  610. * be used by the guest.
  611. *
  612. * On non-booke this is associated with Altivec and
  613. * is handled by code in book3s.c.
  614. */
  615. mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
  616. #endif
  617. kvmppc_core_vcpu_load(vcpu, cpu);
  618. }
  619. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  620. {
  621. kvmppc_core_vcpu_put(vcpu);
  622. #ifdef CONFIG_BOOKE
  623. vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
  624. #endif
  625. }
  626. static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
  627. struct kvm_run *run)
  628. {
  629. u64 uninitialized_var(gpr);
  630. if (run->mmio.len > sizeof(gpr)) {
  631. printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
  632. return;
  633. }
  634. if (vcpu->arch.mmio_is_bigendian) {
  635. switch (run->mmio.len) {
  636. case 8: gpr = *(u64 *)run->mmio.data; break;
  637. case 4: gpr = *(u32 *)run->mmio.data; break;
  638. case 2: gpr = *(u16 *)run->mmio.data; break;
  639. case 1: gpr = *(u8 *)run->mmio.data; break;
  640. }
  641. } else {
  642. /* Convert BE data from userland back to LE. */
  643. switch (run->mmio.len) {
  644. case 4: gpr = ld_le32((u32 *)run->mmio.data); break;
  645. case 2: gpr = ld_le16((u16 *)run->mmio.data); break;
  646. case 1: gpr = *(u8 *)run->mmio.data; break;
  647. }
  648. }
  649. if (vcpu->arch.mmio_sign_extend) {
  650. switch (run->mmio.len) {
  651. #ifdef CONFIG_PPC64
  652. case 4:
  653. gpr = (s64)(s32)gpr;
  654. break;
  655. #endif
  656. case 2:
  657. gpr = (s64)(s16)gpr;
  658. break;
  659. case 1:
  660. gpr = (s64)(s8)gpr;
  661. break;
  662. }
  663. }
  664. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  665. switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
  666. case KVM_MMIO_REG_GPR:
  667. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  668. break;
  669. case KVM_MMIO_REG_FPR:
  670. VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
  671. break;
  672. #ifdef CONFIG_PPC_BOOK3S
  673. case KVM_MMIO_REG_QPR:
  674. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  675. break;
  676. case KVM_MMIO_REG_FQPR:
  677. VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
  678. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  679. break;
  680. #endif
  681. default:
  682. BUG();
  683. }
  684. }
  685. int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  686. unsigned int rt, unsigned int bytes,
  687. int is_default_endian)
  688. {
  689. int idx, ret;
  690. int is_bigendian;
  691. if (kvmppc_need_byteswap(vcpu)) {
  692. /* Default endianness is "little endian". */
  693. is_bigendian = !is_default_endian;
  694. } else {
  695. /* Default endianness is "big endian". */
  696. is_bigendian = is_default_endian;
  697. }
  698. if (bytes > sizeof(run->mmio.data)) {
  699. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  700. run->mmio.len);
  701. }
  702. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  703. run->mmio.len = bytes;
  704. run->mmio.is_write = 0;
  705. vcpu->arch.io_gpr = rt;
  706. vcpu->arch.mmio_is_bigendian = is_bigendian;
  707. vcpu->mmio_needed = 1;
  708. vcpu->mmio_is_write = 0;
  709. vcpu->arch.mmio_sign_extend = 0;
  710. idx = srcu_read_lock(&vcpu->kvm->srcu);
  711. ret = kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, run->mmio.phys_addr,
  712. bytes, &run->mmio.data);
  713. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  714. if (!ret) {
  715. kvmppc_complete_mmio_load(vcpu, run);
  716. vcpu->mmio_needed = 0;
  717. return EMULATE_DONE;
  718. }
  719. return EMULATE_DO_MMIO;
  720. }
  721. EXPORT_SYMBOL_GPL(kvmppc_handle_load);
  722. /* Same as above, but sign extends */
  723. int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
  724. unsigned int rt, unsigned int bytes,
  725. int is_default_endian)
  726. {
  727. int r;
  728. vcpu->arch.mmio_sign_extend = 1;
  729. r = kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian);
  730. return r;
  731. }
  732. int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
  733. u64 val, unsigned int bytes, int is_default_endian)
  734. {
  735. void *data = run->mmio.data;
  736. int idx, ret;
  737. int is_bigendian;
  738. if (kvmppc_need_byteswap(vcpu)) {
  739. /* Default endianness is "little endian". */
  740. is_bigendian = !is_default_endian;
  741. } else {
  742. /* Default endianness is "big endian". */
  743. is_bigendian = is_default_endian;
  744. }
  745. if (bytes > sizeof(run->mmio.data)) {
  746. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  747. run->mmio.len);
  748. }
  749. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  750. run->mmio.len = bytes;
  751. run->mmio.is_write = 1;
  752. vcpu->mmio_needed = 1;
  753. vcpu->mmio_is_write = 1;
  754. /* Store the value at the lowest bytes in 'data'. */
  755. if (is_bigendian) {
  756. switch (bytes) {
  757. case 8: *(u64 *)data = val; break;
  758. case 4: *(u32 *)data = val; break;
  759. case 2: *(u16 *)data = val; break;
  760. case 1: *(u8 *)data = val; break;
  761. }
  762. } else {
  763. /* Store LE value into 'data'. */
  764. switch (bytes) {
  765. case 4: st_le32(data, val); break;
  766. case 2: st_le16(data, val); break;
  767. case 1: *(u8 *)data = val; break;
  768. }
  769. }
  770. idx = srcu_read_lock(&vcpu->kvm->srcu);
  771. ret = kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, run->mmio.phys_addr,
  772. bytes, &run->mmio.data);
  773. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  774. if (!ret) {
  775. vcpu->mmio_needed = 0;
  776. return EMULATE_DONE;
  777. }
  778. return EMULATE_DO_MMIO;
  779. }
  780. EXPORT_SYMBOL_GPL(kvmppc_handle_store);
  781. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  782. {
  783. int r;
  784. sigset_t sigsaved;
  785. if (vcpu->sigset_active)
  786. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  787. if (vcpu->mmio_needed) {
  788. if (!vcpu->mmio_is_write)
  789. kvmppc_complete_mmio_load(vcpu, run);
  790. vcpu->mmio_needed = 0;
  791. } else if (vcpu->arch.osi_needed) {
  792. u64 *gprs = run->osi.gprs;
  793. int i;
  794. for (i = 0; i < 32; i++)
  795. kvmppc_set_gpr(vcpu, i, gprs[i]);
  796. vcpu->arch.osi_needed = 0;
  797. } else if (vcpu->arch.hcall_needed) {
  798. int i;
  799. kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
  800. for (i = 0; i < 9; ++i)
  801. kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
  802. vcpu->arch.hcall_needed = 0;
  803. #ifdef CONFIG_BOOKE
  804. } else if (vcpu->arch.epr_needed) {
  805. kvmppc_set_epr(vcpu, run->epr.epr);
  806. vcpu->arch.epr_needed = 0;
  807. #endif
  808. }
  809. r = kvmppc_vcpu_run(run, vcpu);
  810. if (vcpu->sigset_active)
  811. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  812. return r;
  813. }
  814. int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
  815. {
  816. if (irq->irq == KVM_INTERRUPT_UNSET) {
  817. kvmppc_core_dequeue_external(vcpu);
  818. return 0;
  819. }
  820. kvmppc_core_queue_external(vcpu, irq);
  821. kvm_vcpu_kick(vcpu);
  822. return 0;
  823. }
  824. static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
  825. struct kvm_enable_cap *cap)
  826. {
  827. int r;
  828. if (cap->flags)
  829. return -EINVAL;
  830. switch (cap->cap) {
  831. case KVM_CAP_PPC_OSI:
  832. r = 0;
  833. vcpu->arch.osi_enabled = true;
  834. break;
  835. case KVM_CAP_PPC_PAPR:
  836. r = 0;
  837. vcpu->arch.papr_enabled = true;
  838. break;
  839. case KVM_CAP_PPC_EPR:
  840. r = 0;
  841. if (cap->args[0])
  842. vcpu->arch.epr_flags |= KVMPPC_EPR_USER;
  843. else
  844. vcpu->arch.epr_flags &= ~KVMPPC_EPR_USER;
  845. break;
  846. #ifdef CONFIG_BOOKE
  847. case KVM_CAP_PPC_BOOKE_WATCHDOG:
  848. r = 0;
  849. vcpu->arch.watchdog_enabled = true;
  850. break;
  851. #endif
  852. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  853. case KVM_CAP_SW_TLB: {
  854. struct kvm_config_tlb cfg;
  855. void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
  856. r = -EFAULT;
  857. if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
  858. break;
  859. r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
  860. break;
  861. }
  862. #endif
  863. #ifdef CONFIG_KVM_MPIC
  864. case KVM_CAP_IRQ_MPIC: {
  865. struct fd f;
  866. struct kvm_device *dev;
  867. r = -EBADF;
  868. f = fdget(cap->args[0]);
  869. if (!f.file)
  870. break;
  871. r = -EPERM;
  872. dev = kvm_device_from_filp(f.file);
  873. if (dev)
  874. r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]);
  875. fdput(f);
  876. break;
  877. }
  878. #endif
  879. #ifdef CONFIG_KVM_XICS
  880. case KVM_CAP_IRQ_XICS: {
  881. struct fd f;
  882. struct kvm_device *dev;
  883. r = -EBADF;
  884. f = fdget(cap->args[0]);
  885. if (!f.file)
  886. break;
  887. r = -EPERM;
  888. dev = kvm_device_from_filp(f.file);
  889. if (dev)
  890. r = kvmppc_xics_connect_vcpu(dev, vcpu, cap->args[1]);
  891. fdput(f);
  892. break;
  893. }
  894. #endif /* CONFIG_KVM_XICS */
  895. default:
  896. r = -EINVAL;
  897. break;
  898. }
  899. if (!r)
  900. r = kvmppc_sanity_check(vcpu);
  901. return r;
  902. }
  903. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  904. struct kvm_mp_state *mp_state)
  905. {
  906. return -EINVAL;
  907. }
  908. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  909. struct kvm_mp_state *mp_state)
  910. {
  911. return -EINVAL;
  912. }
  913. long kvm_arch_vcpu_ioctl(struct file *filp,
  914. unsigned int ioctl, unsigned long arg)
  915. {
  916. struct kvm_vcpu *vcpu = filp->private_data;
  917. void __user *argp = (void __user *)arg;
  918. long r;
  919. switch (ioctl) {
  920. case KVM_INTERRUPT: {
  921. struct kvm_interrupt irq;
  922. r = -EFAULT;
  923. if (copy_from_user(&irq, argp, sizeof(irq)))
  924. goto out;
  925. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  926. goto out;
  927. }
  928. case KVM_ENABLE_CAP:
  929. {
  930. struct kvm_enable_cap cap;
  931. r = -EFAULT;
  932. if (copy_from_user(&cap, argp, sizeof(cap)))
  933. goto out;
  934. r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
  935. break;
  936. }
  937. case KVM_SET_ONE_REG:
  938. case KVM_GET_ONE_REG:
  939. {
  940. struct kvm_one_reg reg;
  941. r = -EFAULT;
  942. if (copy_from_user(&reg, argp, sizeof(reg)))
  943. goto out;
  944. if (ioctl == KVM_SET_ONE_REG)
  945. r = kvm_vcpu_ioctl_set_one_reg(vcpu, &reg);
  946. else
  947. r = kvm_vcpu_ioctl_get_one_reg(vcpu, &reg);
  948. break;
  949. }
  950. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  951. case KVM_DIRTY_TLB: {
  952. struct kvm_dirty_tlb dirty;
  953. r = -EFAULT;
  954. if (copy_from_user(&dirty, argp, sizeof(dirty)))
  955. goto out;
  956. r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
  957. break;
  958. }
  959. #endif
  960. default:
  961. r = -EINVAL;
  962. }
  963. out:
  964. return r;
  965. }
  966. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  967. {
  968. return VM_FAULT_SIGBUS;
  969. }
  970. static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
  971. {
  972. u32 inst_nop = 0x60000000;
  973. #ifdef CONFIG_KVM_BOOKE_HV
  974. u32 inst_sc1 = 0x44000022;
  975. pvinfo->hcall[0] = cpu_to_be32(inst_sc1);
  976. pvinfo->hcall[1] = cpu_to_be32(inst_nop);
  977. pvinfo->hcall[2] = cpu_to_be32(inst_nop);
  978. pvinfo->hcall[3] = cpu_to_be32(inst_nop);
  979. #else
  980. u32 inst_lis = 0x3c000000;
  981. u32 inst_ori = 0x60000000;
  982. u32 inst_sc = 0x44000002;
  983. u32 inst_imm_mask = 0xffff;
  984. /*
  985. * The hypercall to get into KVM from within guest context is as
  986. * follows:
  987. *
  988. * lis r0, r0, KVM_SC_MAGIC_R0@h
  989. * ori r0, KVM_SC_MAGIC_R0@l
  990. * sc
  991. * nop
  992. */
  993. pvinfo->hcall[0] = cpu_to_be32(inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask));
  994. pvinfo->hcall[1] = cpu_to_be32(inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask));
  995. pvinfo->hcall[2] = cpu_to_be32(inst_sc);
  996. pvinfo->hcall[3] = cpu_to_be32(inst_nop);
  997. #endif
  998. pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
  999. return 0;
  1000. }
  1001. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
  1002. bool line_status)
  1003. {
  1004. if (!irqchip_in_kernel(kvm))
  1005. return -ENXIO;
  1006. irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
  1007. irq_event->irq, irq_event->level,
  1008. line_status);
  1009. return 0;
  1010. }
  1011. static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  1012. struct kvm_enable_cap *cap)
  1013. {
  1014. int r;
  1015. if (cap->flags)
  1016. return -EINVAL;
  1017. switch (cap->cap) {
  1018. #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
  1019. case KVM_CAP_PPC_ENABLE_HCALL: {
  1020. unsigned long hcall = cap->args[0];
  1021. r = -EINVAL;
  1022. if (hcall > MAX_HCALL_OPCODE || (hcall & 3) ||
  1023. cap->args[1] > 1)
  1024. break;
  1025. if (!kvmppc_book3s_hcall_implemented(kvm, hcall))
  1026. break;
  1027. if (cap->args[1])
  1028. set_bit(hcall / 4, kvm->arch.enabled_hcalls);
  1029. else
  1030. clear_bit(hcall / 4, kvm->arch.enabled_hcalls);
  1031. r = 0;
  1032. break;
  1033. }
  1034. #endif
  1035. default:
  1036. r = -EINVAL;
  1037. break;
  1038. }
  1039. return r;
  1040. }
  1041. long kvm_arch_vm_ioctl(struct file *filp,
  1042. unsigned int ioctl, unsigned long arg)
  1043. {
  1044. struct kvm *kvm __maybe_unused = filp->private_data;
  1045. void __user *argp = (void __user *)arg;
  1046. long r;
  1047. switch (ioctl) {
  1048. case KVM_PPC_GET_PVINFO: {
  1049. struct kvm_ppc_pvinfo pvinfo;
  1050. memset(&pvinfo, 0, sizeof(pvinfo));
  1051. r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
  1052. if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
  1053. r = -EFAULT;
  1054. goto out;
  1055. }
  1056. break;
  1057. }
  1058. case KVM_ENABLE_CAP:
  1059. {
  1060. struct kvm_enable_cap cap;
  1061. r = -EFAULT;
  1062. if (copy_from_user(&cap, argp, sizeof(cap)))
  1063. goto out;
  1064. r = kvm_vm_ioctl_enable_cap(kvm, &cap);
  1065. break;
  1066. }
  1067. #ifdef CONFIG_PPC_BOOK3S_64
  1068. case KVM_CREATE_SPAPR_TCE: {
  1069. struct kvm_create_spapr_tce create_tce;
  1070. r = -EFAULT;
  1071. if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
  1072. goto out;
  1073. r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce);
  1074. goto out;
  1075. }
  1076. case KVM_PPC_GET_SMMU_INFO: {
  1077. struct kvm_ppc_smmu_info info;
  1078. struct kvm *kvm = filp->private_data;
  1079. memset(&info, 0, sizeof(info));
  1080. r = kvm->arch.kvm_ops->get_smmu_info(kvm, &info);
  1081. if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
  1082. r = -EFAULT;
  1083. break;
  1084. }
  1085. case KVM_PPC_RTAS_DEFINE_TOKEN: {
  1086. struct kvm *kvm = filp->private_data;
  1087. r = kvm_vm_ioctl_rtas_define_token(kvm, argp);
  1088. break;
  1089. }
  1090. default: {
  1091. struct kvm *kvm = filp->private_data;
  1092. r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg);
  1093. }
  1094. #else /* CONFIG_PPC_BOOK3S_64 */
  1095. default:
  1096. r = -ENOTTY;
  1097. #endif
  1098. }
  1099. out:
  1100. return r;
  1101. }
  1102. static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
  1103. static unsigned long nr_lpids;
  1104. long kvmppc_alloc_lpid(void)
  1105. {
  1106. long lpid;
  1107. do {
  1108. lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
  1109. if (lpid >= nr_lpids) {
  1110. pr_err("%s: No LPIDs free\n", __func__);
  1111. return -ENOMEM;
  1112. }
  1113. } while (test_and_set_bit(lpid, lpid_inuse));
  1114. return lpid;
  1115. }
  1116. EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid);
  1117. void kvmppc_claim_lpid(long lpid)
  1118. {
  1119. set_bit(lpid, lpid_inuse);
  1120. }
  1121. EXPORT_SYMBOL_GPL(kvmppc_claim_lpid);
  1122. void kvmppc_free_lpid(long lpid)
  1123. {
  1124. clear_bit(lpid, lpid_inuse);
  1125. }
  1126. EXPORT_SYMBOL_GPL(kvmppc_free_lpid);
  1127. void kvmppc_init_lpid(unsigned long nr_lpids_param)
  1128. {
  1129. nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
  1130. memset(lpid_inuse, 0, sizeof(lpid_inuse));
  1131. }
  1132. EXPORT_SYMBOL_GPL(kvmppc_init_lpid);
  1133. int kvm_arch_init(void *opaque)
  1134. {
  1135. return 0;
  1136. }
  1137. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ppc_instr);