powerpc.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  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. void kvm_arch_hardware_disable(void *garbage)
  333. {
  334. }
  335. int kvm_arch_hardware_setup(void)
  336. {
  337. return 0;
  338. }
  339. void kvm_arch_hardware_unsetup(void)
  340. {
  341. }
  342. void kvm_arch_check_processor_compat(void *rtn)
  343. {
  344. *(int *)rtn = kvmppc_core_check_processor_compat();
  345. }
  346. int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
  347. {
  348. struct kvmppc_ops *kvm_ops = NULL;
  349. /*
  350. * if we have both HV and PR enabled, default is HV
  351. */
  352. if (type == 0) {
  353. if (kvmppc_hv_ops)
  354. kvm_ops = kvmppc_hv_ops;
  355. else
  356. kvm_ops = kvmppc_pr_ops;
  357. if (!kvm_ops)
  358. goto err_out;
  359. } else if (type == KVM_VM_PPC_HV) {
  360. if (!kvmppc_hv_ops)
  361. goto err_out;
  362. kvm_ops = kvmppc_hv_ops;
  363. } else if (type == KVM_VM_PPC_PR) {
  364. if (!kvmppc_pr_ops)
  365. goto err_out;
  366. kvm_ops = kvmppc_pr_ops;
  367. } else
  368. goto err_out;
  369. if (kvm_ops->owner && !try_module_get(kvm_ops->owner))
  370. return -ENOENT;
  371. kvm->arch.kvm_ops = kvm_ops;
  372. return kvmppc_core_init_vm(kvm);
  373. err_out:
  374. return -EINVAL;
  375. }
  376. void kvm_arch_destroy_vm(struct kvm *kvm)
  377. {
  378. unsigned int i;
  379. struct kvm_vcpu *vcpu;
  380. kvm_for_each_vcpu(i, vcpu, kvm)
  381. kvm_arch_vcpu_free(vcpu);
  382. mutex_lock(&kvm->lock);
  383. for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
  384. kvm->vcpus[i] = NULL;
  385. atomic_set(&kvm->online_vcpus, 0);
  386. kvmppc_core_destroy_vm(kvm);
  387. mutex_unlock(&kvm->lock);
  388. /* drop the module reference */
  389. module_put(kvm->arch.kvm_ops->owner);
  390. }
  391. void kvm_arch_sync_events(struct kvm *kvm)
  392. {
  393. }
  394. int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
  395. {
  396. int r;
  397. /* Assume we're using HV mode when the HV module is loaded */
  398. int hv_enabled = kvmppc_hv_ops ? 1 : 0;
  399. if (kvm) {
  400. /*
  401. * Hooray - we know which VM type we're running on. Depend on
  402. * that rather than the guess above.
  403. */
  404. hv_enabled = is_kvmppc_hv_enabled(kvm);
  405. }
  406. switch (ext) {
  407. #ifdef CONFIG_BOOKE
  408. case KVM_CAP_PPC_BOOKE_SREGS:
  409. case KVM_CAP_PPC_BOOKE_WATCHDOG:
  410. case KVM_CAP_PPC_EPR:
  411. #else
  412. case KVM_CAP_PPC_SEGSTATE:
  413. case KVM_CAP_PPC_HIOR:
  414. case KVM_CAP_PPC_PAPR:
  415. #endif
  416. case KVM_CAP_PPC_UNSET_IRQ:
  417. case KVM_CAP_PPC_IRQ_LEVEL:
  418. case KVM_CAP_ENABLE_CAP:
  419. case KVM_CAP_ENABLE_CAP_VM:
  420. case KVM_CAP_ONE_REG:
  421. case KVM_CAP_IOEVENTFD:
  422. case KVM_CAP_DEVICE_CTRL:
  423. r = 1;
  424. break;
  425. case KVM_CAP_PPC_PAIRED_SINGLES:
  426. case KVM_CAP_PPC_OSI:
  427. case KVM_CAP_PPC_GET_PVINFO:
  428. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  429. case KVM_CAP_SW_TLB:
  430. #endif
  431. /* We support this only for PR */
  432. r = !hv_enabled;
  433. break;
  434. #ifdef CONFIG_KVM_MMIO
  435. case KVM_CAP_COALESCED_MMIO:
  436. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  437. break;
  438. #endif
  439. #ifdef CONFIG_KVM_MPIC
  440. case KVM_CAP_IRQ_MPIC:
  441. r = 1;
  442. break;
  443. #endif
  444. #ifdef CONFIG_PPC_BOOK3S_64
  445. case KVM_CAP_SPAPR_TCE:
  446. case KVM_CAP_PPC_ALLOC_HTAB:
  447. case KVM_CAP_PPC_RTAS:
  448. case KVM_CAP_PPC_FIXUP_HCALL:
  449. case KVM_CAP_PPC_ENABLE_HCALL:
  450. #ifdef CONFIG_KVM_XICS
  451. case KVM_CAP_IRQ_XICS:
  452. #endif
  453. r = 1;
  454. break;
  455. #endif /* CONFIG_PPC_BOOK3S_64 */
  456. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  457. case KVM_CAP_PPC_SMT:
  458. if (hv_enabled)
  459. r = threads_per_subcore;
  460. else
  461. r = 0;
  462. break;
  463. case KVM_CAP_PPC_RMA:
  464. r = hv_enabled;
  465. /* PPC970 requires an RMA */
  466. if (r && cpu_has_feature(CPU_FTR_ARCH_201))
  467. r = 2;
  468. break;
  469. #endif
  470. case KVM_CAP_SYNC_MMU:
  471. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  472. if (hv_enabled)
  473. r = cpu_has_feature(CPU_FTR_ARCH_206) ? 1 : 0;
  474. else
  475. r = 0;
  476. #elif defined(KVM_ARCH_WANT_MMU_NOTIFIER)
  477. r = 1;
  478. #else
  479. r = 0;
  480. #endif
  481. break;
  482. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  483. case KVM_CAP_PPC_HTAB_FD:
  484. r = hv_enabled;
  485. break;
  486. #endif
  487. case KVM_CAP_NR_VCPUS:
  488. /*
  489. * Recommending a number of CPUs is somewhat arbitrary; we
  490. * return the number of present CPUs for -HV (since a host
  491. * will have secondary threads "offline"), and for other KVM
  492. * implementations just count online CPUs.
  493. */
  494. if (hv_enabled)
  495. r = num_present_cpus();
  496. else
  497. r = num_online_cpus();
  498. break;
  499. case KVM_CAP_MAX_VCPUS:
  500. r = KVM_MAX_VCPUS;
  501. break;
  502. #ifdef CONFIG_PPC_BOOK3S_64
  503. case KVM_CAP_PPC_GET_SMMU_INFO:
  504. r = 1;
  505. break;
  506. #endif
  507. default:
  508. r = 0;
  509. break;
  510. }
  511. return r;
  512. }
  513. long kvm_arch_dev_ioctl(struct file *filp,
  514. unsigned int ioctl, unsigned long arg)
  515. {
  516. return -EINVAL;
  517. }
  518. void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
  519. struct kvm_memory_slot *dont)
  520. {
  521. kvmppc_core_free_memslot(kvm, free, dont);
  522. }
  523. int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
  524. unsigned long npages)
  525. {
  526. return kvmppc_core_create_memslot(kvm, slot, npages);
  527. }
  528. void kvm_arch_memslots_updated(struct kvm *kvm)
  529. {
  530. }
  531. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  532. struct kvm_memory_slot *memslot,
  533. struct kvm_userspace_memory_region *mem,
  534. enum kvm_mr_change change)
  535. {
  536. return kvmppc_core_prepare_memory_region(kvm, memslot, mem);
  537. }
  538. void kvm_arch_commit_memory_region(struct kvm *kvm,
  539. struct kvm_userspace_memory_region *mem,
  540. const struct kvm_memory_slot *old,
  541. enum kvm_mr_change change)
  542. {
  543. kvmppc_core_commit_memory_region(kvm, mem, old);
  544. }
  545. void kvm_arch_flush_shadow_all(struct kvm *kvm)
  546. {
  547. }
  548. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  549. struct kvm_memory_slot *slot)
  550. {
  551. kvmppc_core_flush_memslot(kvm, slot);
  552. }
  553. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  554. {
  555. struct kvm_vcpu *vcpu;
  556. vcpu = kvmppc_core_vcpu_create(kvm, id);
  557. if (!IS_ERR(vcpu)) {
  558. vcpu->arch.wqp = &vcpu->wq;
  559. kvmppc_create_vcpu_debugfs(vcpu, id);
  560. }
  561. return vcpu;
  562. }
  563. int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  564. {
  565. return 0;
  566. }
  567. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  568. {
  569. /* Make sure we're not using the vcpu anymore */
  570. hrtimer_cancel(&vcpu->arch.dec_timer);
  571. tasklet_kill(&vcpu->arch.tasklet);
  572. kvmppc_remove_vcpu_debugfs(vcpu);
  573. switch (vcpu->arch.irq_type) {
  574. case KVMPPC_IRQ_MPIC:
  575. kvmppc_mpic_disconnect_vcpu(vcpu->arch.mpic, vcpu);
  576. break;
  577. case KVMPPC_IRQ_XICS:
  578. kvmppc_xics_free_icp(vcpu);
  579. break;
  580. }
  581. kvmppc_core_vcpu_free(vcpu);
  582. }
  583. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  584. {
  585. kvm_arch_vcpu_free(vcpu);
  586. }
  587. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  588. {
  589. return kvmppc_core_pending_dec(vcpu);
  590. }
  591. /*
  592. * low level hrtimer wake routine. Because this runs in hardirq context
  593. * we schedule a tasklet to do the real work.
  594. */
  595. enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
  596. {
  597. struct kvm_vcpu *vcpu;
  598. vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
  599. tasklet_schedule(&vcpu->arch.tasklet);
  600. return HRTIMER_NORESTART;
  601. }
  602. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  603. {
  604. int ret;
  605. hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  606. tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu);
  607. vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
  608. vcpu->arch.dec_expires = ~(u64)0;
  609. #ifdef CONFIG_KVM_EXIT_TIMING
  610. mutex_init(&vcpu->arch.exit_timing_lock);
  611. #endif
  612. ret = kvmppc_subarch_vcpu_init(vcpu);
  613. return ret;
  614. }
  615. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  616. {
  617. kvmppc_mmu_destroy(vcpu);
  618. kvmppc_subarch_vcpu_uninit(vcpu);
  619. }
  620. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  621. {
  622. #ifdef CONFIG_BOOKE
  623. /*
  624. * vrsave (formerly usprg0) isn't used by Linux, but may
  625. * be used by the guest.
  626. *
  627. * On non-booke this is associated with Altivec and
  628. * is handled by code in book3s.c.
  629. */
  630. mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
  631. #endif
  632. kvmppc_core_vcpu_load(vcpu, cpu);
  633. }
  634. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  635. {
  636. kvmppc_core_vcpu_put(vcpu);
  637. #ifdef CONFIG_BOOKE
  638. vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
  639. #endif
  640. }
  641. static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
  642. struct kvm_run *run)
  643. {
  644. u64 uninitialized_var(gpr);
  645. if (run->mmio.len > sizeof(gpr)) {
  646. printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
  647. return;
  648. }
  649. if (vcpu->arch.mmio_is_bigendian) {
  650. switch (run->mmio.len) {
  651. case 8: gpr = *(u64 *)run->mmio.data; break;
  652. case 4: gpr = *(u32 *)run->mmio.data; break;
  653. case 2: gpr = *(u16 *)run->mmio.data; break;
  654. case 1: gpr = *(u8 *)run->mmio.data; break;
  655. }
  656. } else {
  657. /* Convert BE data from userland back to LE. */
  658. switch (run->mmio.len) {
  659. case 4: gpr = ld_le32((u32 *)run->mmio.data); break;
  660. case 2: gpr = ld_le16((u16 *)run->mmio.data); break;
  661. case 1: gpr = *(u8 *)run->mmio.data; break;
  662. }
  663. }
  664. if (vcpu->arch.mmio_sign_extend) {
  665. switch (run->mmio.len) {
  666. #ifdef CONFIG_PPC64
  667. case 4:
  668. gpr = (s64)(s32)gpr;
  669. break;
  670. #endif
  671. case 2:
  672. gpr = (s64)(s16)gpr;
  673. break;
  674. case 1:
  675. gpr = (s64)(s8)gpr;
  676. break;
  677. }
  678. }
  679. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  680. switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
  681. case KVM_MMIO_REG_GPR:
  682. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  683. break;
  684. case KVM_MMIO_REG_FPR:
  685. VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
  686. break;
  687. #ifdef CONFIG_PPC_BOOK3S
  688. case KVM_MMIO_REG_QPR:
  689. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  690. break;
  691. case KVM_MMIO_REG_FQPR:
  692. VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
  693. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  694. break;
  695. #endif
  696. default:
  697. BUG();
  698. }
  699. }
  700. int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  701. unsigned int rt, unsigned int bytes,
  702. int is_default_endian)
  703. {
  704. int idx, ret;
  705. int is_bigendian;
  706. if (kvmppc_need_byteswap(vcpu)) {
  707. /* Default endianness is "little endian". */
  708. is_bigendian = !is_default_endian;
  709. } else {
  710. /* Default endianness is "big endian". */
  711. is_bigendian = is_default_endian;
  712. }
  713. if (bytes > sizeof(run->mmio.data)) {
  714. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  715. run->mmio.len);
  716. }
  717. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  718. run->mmio.len = bytes;
  719. run->mmio.is_write = 0;
  720. vcpu->arch.io_gpr = rt;
  721. vcpu->arch.mmio_is_bigendian = is_bigendian;
  722. vcpu->mmio_needed = 1;
  723. vcpu->mmio_is_write = 0;
  724. vcpu->arch.mmio_sign_extend = 0;
  725. idx = srcu_read_lock(&vcpu->kvm->srcu);
  726. ret = kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, run->mmio.phys_addr,
  727. bytes, &run->mmio.data);
  728. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  729. if (!ret) {
  730. kvmppc_complete_mmio_load(vcpu, run);
  731. vcpu->mmio_needed = 0;
  732. return EMULATE_DONE;
  733. }
  734. return EMULATE_DO_MMIO;
  735. }
  736. EXPORT_SYMBOL_GPL(kvmppc_handle_load);
  737. /* Same as above, but sign extends */
  738. int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
  739. unsigned int rt, unsigned int bytes,
  740. int is_default_endian)
  741. {
  742. int r;
  743. vcpu->arch.mmio_sign_extend = 1;
  744. r = kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian);
  745. return r;
  746. }
  747. int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
  748. u64 val, unsigned int bytes, int is_default_endian)
  749. {
  750. void *data = run->mmio.data;
  751. int idx, ret;
  752. int is_bigendian;
  753. if (kvmppc_need_byteswap(vcpu)) {
  754. /* Default endianness is "little endian". */
  755. is_bigendian = !is_default_endian;
  756. } else {
  757. /* Default endianness is "big endian". */
  758. is_bigendian = is_default_endian;
  759. }
  760. if (bytes > sizeof(run->mmio.data)) {
  761. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  762. run->mmio.len);
  763. }
  764. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  765. run->mmio.len = bytes;
  766. run->mmio.is_write = 1;
  767. vcpu->mmio_needed = 1;
  768. vcpu->mmio_is_write = 1;
  769. /* Store the value at the lowest bytes in 'data'. */
  770. if (is_bigendian) {
  771. switch (bytes) {
  772. case 8: *(u64 *)data = val; break;
  773. case 4: *(u32 *)data = val; break;
  774. case 2: *(u16 *)data = val; break;
  775. case 1: *(u8 *)data = val; break;
  776. }
  777. } else {
  778. /* Store LE value into 'data'. */
  779. switch (bytes) {
  780. case 4: st_le32(data, val); break;
  781. case 2: st_le16(data, val); break;
  782. case 1: *(u8 *)data = val; break;
  783. }
  784. }
  785. idx = srcu_read_lock(&vcpu->kvm->srcu);
  786. ret = kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, run->mmio.phys_addr,
  787. bytes, &run->mmio.data);
  788. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  789. if (!ret) {
  790. vcpu->mmio_needed = 0;
  791. return EMULATE_DONE;
  792. }
  793. return EMULATE_DO_MMIO;
  794. }
  795. EXPORT_SYMBOL_GPL(kvmppc_handle_store);
  796. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  797. {
  798. int r;
  799. sigset_t sigsaved;
  800. if (vcpu->sigset_active)
  801. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  802. if (vcpu->mmio_needed) {
  803. if (!vcpu->mmio_is_write)
  804. kvmppc_complete_mmio_load(vcpu, run);
  805. vcpu->mmio_needed = 0;
  806. } else if (vcpu->arch.osi_needed) {
  807. u64 *gprs = run->osi.gprs;
  808. int i;
  809. for (i = 0; i < 32; i++)
  810. kvmppc_set_gpr(vcpu, i, gprs[i]);
  811. vcpu->arch.osi_needed = 0;
  812. } else if (vcpu->arch.hcall_needed) {
  813. int i;
  814. kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
  815. for (i = 0; i < 9; ++i)
  816. kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
  817. vcpu->arch.hcall_needed = 0;
  818. #ifdef CONFIG_BOOKE
  819. } else if (vcpu->arch.epr_needed) {
  820. kvmppc_set_epr(vcpu, run->epr.epr);
  821. vcpu->arch.epr_needed = 0;
  822. #endif
  823. }
  824. r = kvmppc_vcpu_run(run, vcpu);
  825. if (vcpu->sigset_active)
  826. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  827. return r;
  828. }
  829. int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
  830. {
  831. if (irq->irq == KVM_INTERRUPT_UNSET) {
  832. kvmppc_core_dequeue_external(vcpu);
  833. return 0;
  834. }
  835. kvmppc_core_queue_external(vcpu, irq);
  836. kvm_vcpu_kick(vcpu);
  837. return 0;
  838. }
  839. static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
  840. struct kvm_enable_cap *cap)
  841. {
  842. int r;
  843. if (cap->flags)
  844. return -EINVAL;
  845. switch (cap->cap) {
  846. case KVM_CAP_PPC_OSI:
  847. r = 0;
  848. vcpu->arch.osi_enabled = true;
  849. break;
  850. case KVM_CAP_PPC_PAPR:
  851. r = 0;
  852. vcpu->arch.papr_enabled = true;
  853. break;
  854. case KVM_CAP_PPC_EPR:
  855. r = 0;
  856. if (cap->args[0])
  857. vcpu->arch.epr_flags |= KVMPPC_EPR_USER;
  858. else
  859. vcpu->arch.epr_flags &= ~KVMPPC_EPR_USER;
  860. break;
  861. #ifdef CONFIG_BOOKE
  862. case KVM_CAP_PPC_BOOKE_WATCHDOG:
  863. r = 0;
  864. vcpu->arch.watchdog_enabled = true;
  865. break;
  866. #endif
  867. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  868. case KVM_CAP_SW_TLB: {
  869. struct kvm_config_tlb cfg;
  870. void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
  871. r = -EFAULT;
  872. if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
  873. break;
  874. r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
  875. break;
  876. }
  877. #endif
  878. #ifdef CONFIG_KVM_MPIC
  879. case KVM_CAP_IRQ_MPIC: {
  880. struct fd f;
  881. struct kvm_device *dev;
  882. r = -EBADF;
  883. f = fdget(cap->args[0]);
  884. if (!f.file)
  885. break;
  886. r = -EPERM;
  887. dev = kvm_device_from_filp(f.file);
  888. if (dev)
  889. r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]);
  890. fdput(f);
  891. break;
  892. }
  893. #endif
  894. #ifdef CONFIG_KVM_XICS
  895. case KVM_CAP_IRQ_XICS: {
  896. struct fd f;
  897. struct kvm_device *dev;
  898. r = -EBADF;
  899. f = fdget(cap->args[0]);
  900. if (!f.file)
  901. break;
  902. r = -EPERM;
  903. dev = kvm_device_from_filp(f.file);
  904. if (dev)
  905. r = kvmppc_xics_connect_vcpu(dev, vcpu, cap->args[1]);
  906. fdput(f);
  907. break;
  908. }
  909. #endif /* CONFIG_KVM_XICS */
  910. default:
  911. r = -EINVAL;
  912. break;
  913. }
  914. if (!r)
  915. r = kvmppc_sanity_check(vcpu);
  916. return r;
  917. }
  918. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  919. struct kvm_mp_state *mp_state)
  920. {
  921. return -EINVAL;
  922. }
  923. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  924. struct kvm_mp_state *mp_state)
  925. {
  926. return -EINVAL;
  927. }
  928. long kvm_arch_vcpu_ioctl(struct file *filp,
  929. unsigned int ioctl, unsigned long arg)
  930. {
  931. struct kvm_vcpu *vcpu = filp->private_data;
  932. void __user *argp = (void __user *)arg;
  933. long r;
  934. switch (ioctl) {
  935. case KVM_INTERRUPT: {
  936. struct kvm_interrupt irq;
  937. r = -EFAULT;
  938. if (copy_from_user(&irq, argp, sizeof(irq)))
  939. goto out;
  940. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  941. goto out;
  942. }
  943. case KVM_ENABLE_CAP:
  944. {
  945. struct kvm_enable_cap cap;
  946. r = -EFAULT;
  947. if (copy_from_user(&cap, argp, sizeof(cap)))
  948. goto out;
  949. r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
  950. break;
  951. }
  952. case KVM_SET_ONE_REG:
  953. case KVM_GET_ONE_REG:
  954. {
  955. struct kvm_one_reg reg;
  956. r = -EFAULT;
  957. if (copy_from_user(&reg, argp, sizeof(reg)))
  958. goto out;
  959. if (ioctl == KVM_SET_ONE_REG)
  960. r = kvm_vcpu_ioctl_set_one_reg(vcpu, &reg);
  961. else
  962. r = kvm_vcpu_ioctl_get_one_reg(vcpu, &reg);
  963. break;
  964. }
  965. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  966. case KVM_DIRTY_TLB: {
  967. struct kvm_dirty_tlb dirty;
  968. r = -EFAULT;
  969. if (copy_from_user(&dirty, argp, sizeof(dirty)))
  970. goto out;
  971. r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
  972. break;
  973. }
  974. #endif
  975. default:
  976. r = -EINVAL;
  977. }
  978. out:
  979. return r;
  980. }
  981. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  982. {
  983. return VM_FAULT_SIGBUS;
  984. }
  985. static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
  986. {
  987. u32 inst_nop = 0x60000000;
  988. #ifdef CONFIG_KVM_BOOKE_HV
  989. u32 inst_sc1 = 0x44000022;
  990. pvinfo->hcall[0] = cpu_to_be32(inst_sc1);
  991. pvinfo->hcall[1] = cpu_to_be32(inst_nop);
  992. pvinfo->hcall[2] = cpu_to_be32(inst_nop);
  993. pvinfo->hcall[3] = cpu_to_be32(inst_nop);
  994. #else
  995. u32 inst_lis = 0x3c000000;
  996. u32 inst_ori = 0x60000000;
  997. u32 inst_sc = 0x44000002;
  998. u32 inst_imm_mask = 0xffff;
  999. /*
  1000. * The hypercall to get into KVM from within guest context is as
  1001. * follows:
  1002. *
  1003. * lis r0, r0, KVM_SC_MAGIC_R0@h
  1004. * ori r0, KVM_SC_MAGIC_R0@l
  1005. * sc
  1006. * nop
  1007. */
  1008. pvinfo->hcall[0] = cpu_to_be32(inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask));
  1009. pvinfo->hcall[1] = cpu_to_be32(inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask));
  1010. pvinfo->hcall[2] = cpu_to_be32(inst_sc);
  1011. pvinfo->hcall[3] = cpu_to_be32(inst_nop);
  1012. #endif
  1013. pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
  1014. return 0;
  1015. }
  1016. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
  1017. bool line_status)
  1018. {
  1019. if (!irqchip_in_kernel(kvm))
  1020. return -ENXIO;
  1021. irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
  1022. irq_event->irq, irq_event->level,
  1023. line_status);
  1024. return 0;
  1025. }
  1026. static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  1027. struct kvm_enable_cap *cap)
  1028. {
  1029. int r;
  1030. if (cap->flags)
  1031. return -EINVAL;
  1032. switch (cap->cap) {
  1033. #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
  1034. case KVM_CAP_PPC_ENABLE_HCALL: {
  1035. unsigned long hcall = cap->args[0];
  1036. r = -EINVAL;
  1037. if (hcall > MAX_HCALL_OPCODE || (hcall & 3) ||
  1038. cap->args[1] > 1)
  1039. break;
  1040. if (!kvmppc_book3s_hcall_implemented(kvm, hcall))
  1041. break;
  1042. if (cap->args[1])
  1043. set_bit(hcall / 4, kvm->arch.enabled_hcalls);
  1044. else
  1045. clear_bit(hcall / 4, kvm->arch.enabled_hcalls);
  1046. r = 0;
  1047. break;
  1048. }
  1049. #endif
  1050. default:
  1051. r = -EINVAL;
  1052. break;
  1053. }
  1054. return r;
  1055. }
  1056. long kvm_arch_vm_ioctl(struct file *filp,
  1057. unsigned int ioctl, unsigned long arg)
  1058. {
  1059. struct kvm *kvm __maybe_unused = filp->private_data;
  1060. void __user *argp = (void __user *)arg;
  1061. long r;
  1062. switch (ioctl) {
  1063. case KVM_PPC_GET_PVINFO: {
  1064. struct kvm_ppc_pvinfo pvinfo;
  1065. memset(&pvinfo, 0, sizeof(pvinfo));
  1066. r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
  1067. if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
  1068. r = -EFAULT;
  1069. goto out;
  1070. }
  1071. break;
  1072. }
  1073. case KVM_ENABLE_CAP:
  1074. {
  1075. struct kvm_enable_cap cap;
  1076. r = -EFAULT;
  1077. if (copy_from_user(&cap, argp, sizeof(cap)))
  1078. goto out;
  1079. r = kvm_vm_ioctl_enable_cap(kvm, &cap);
  1080. break;
  1081. }
  1082. #ifdef CONFIG_PPC_BOOK3S_64
  1083. case KVM_CREATE_SPAPR_TCE: {
  1084. struct kvm_create_spapr_tce create_tce;
  1085. r = -EFAULT;
  1086. if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
  1087. goto out;
  1088. r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce);
  1089. goto out;
  1090. }
  1091. case KVM_PPC_GET_SMMU_INFO: {
  1092. struct kvm_ppc_smmu_info info;
  1093. struct kvm *kvm = filp->private_data;
  1094. memset(&info, 0, sizeof(info));
  1095. r = kvm->arch.kvm_ops->get_smmu_info(kvm, &info);
  1096. if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
  1097. r = -EFAULT;
  1098. break;
  1099. }
  1100. case KVM_PPC_RTAS_DEFINE_TOKEN: {
  1101. struct kvm *kvm = filp->private_data;
  1102. r = kvm_vm_ioctl_rtas_define_token(kvm, argp);
  1103. break;
  1104. }
  1105. default: {
  1106. struct kvm *kvm = filp->private_data;
  1107. r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg);
  1108. }
  1109. #else /* CONFIG_PPC_BOOK3S_64 */
  1110. default:
  1111. r = -ENOTTY;
  1112. #endif
  1113. }
  1114. out:
  1115. return r;
  1116. }
  1117. static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
  1118. static unsigned long nr_lpids;
  1119. long kvmppc_alloc_lpid(void)
  1120. {
  1121. long lpid;
  1122. do {
  1123. lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
  1124. if (lpid >= nr_lpids) {
  1125. pr_err("%s: No LPIDs free\n", __func__);
  1126. return -ENOMEM;
  1127. }
  1128. } while (test_and_set_bit(lpid, lpid_inuse));
  1129. return lpid;
  1130. }
  1131. EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid);
  1132. void kvmppc_claim_lpid(long lpid)
  1133. {
  1134. set_bit(lpid, lpid_inuse);
  1135. }
  1136. EXPORT_SYMBOL_GPL(kvmppc_claim_lpid);
  1137. void kvmppc_free_lpid(long lpid)
  1138. {
  1139. clear_bit(lpid, lpid_inuse);
  1140. }
  1141. EXPORT_SYMBOL_GPL(kvmppc_free_lpid);
  1142. void kvmppc_init_lpid(unsigned long nr_lpids_param)
  1143. {
  1144. nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
  1145. memset(lpid_inuse, 0, sizeof(lpid_inuse));
  1146. }
  1147. EXPORT_SYMBOL_GPL(kvmppc_init_lpid);
  1148. int kvm_arch_init(void *opaque)
  1149. {
  1150. return 0;
  1151. }
  1152. void kvm_arch_exit(void)
  1153. {
  1154. }
  1155. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ppc_instr);