powerpc.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  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, INST_GENERIC, &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)
  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 = 0;
  456. break;
  457. case KVM_CAP_PPC_HWRNG:
  458. r = kvmppc_hwrng_present();
  459. break;
  460. #endif
  461. case KVM_CAP_SYNC_MMU:
  462. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  463. r = hv_enabled;
  464. #elif defined(KVM_ARCH_WANT_MMU_NOTIFIER)
  465. r = 1;
  466. #else
  467. r = 0;
  468. #endif
  469. break;
  470. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  471. case KVM_CAP_PPC_HTAB_FD:
  472. r = hv_enabled;
  473. break;
  474. #endif
  475. case KVM_CAP_NR_VCPUS:
  476. /*
  477. * Recommending a number of CPUs is somewhat arbitrary; we
  478. * return the number of present CPUs for -HV (since a host
  479. * will have secondary threads "offline"), and for other KVM
  480. * implementations just count online CPUs.
  481. */
  482. if (hv_enabled)
  483. r = num_present_cpus();
  484. else
  485. r = num_online_cpus();
  486. break;
  487. case KVM_CAP_MAX_VCPUS:
  488. r = KVM_MAX_VCPUS;
  489. break;
  490. #ifdef CONFIG_PPC_BOOK3S_64
  491. case KVM_CAP_PPC_GET_SMMU_INFO:
  492. r = 1;
  493. break;
  494. #endif
  495. default:
  496. r = 0;
  497. break;
  498. }
  499. return r;
  500. }
  501. long kvm_arch_dev_ioctl(struct file *filp,
  502. unsigned int ioctl, unsigned long arg)
  503. {
  504. return -EINVAL;
  505. }
  506. void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
  507. struct kvm_memory_slot *dont)
  508. {
  509. kvmppc_core_free_memslot(kvm, free, dont);
  510. }
  511. int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
  512. unsigned long npages)
  513. {
  514. return kvmppc_core_create_memslot(kvm, slot, npages);
  515. }
  516. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  517. struct kvm_memory_slot *memslot,
  518. struct kvm_userspace_memory_region *mem,
  519. enum kvm_mr_change change)
  520. {
  521. return kvmppc_core_prepare_memory_region(kvm, memslot, mem);
  522. }
  523. void kvm_arch_commit_memory_region(struct kvm *kvm,
  524. struct kvm_userspace_memory_region *mem,
  525. const struct kvm_memory_slot *old,
  526. enum kvm_mr_change change)
  527. {
  528. kvmppc_core_commit_memory_region(kvm, mem, old);
  529. }
  530. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  531. struct kvm_memory_slot *slot)
  532. {
  533. kvmppc_core_flush_memslot(kvm, slot);
  534. }
  535. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  536. {
  537. struct kvm_vcpu *vcpu;
  538. vcpu = kvmppc_core_vcpu_create(kvm, id);
  539. if (!IS_ERR(vcpu)) {
  540. vcpu->arch.wqp = &vcpu->wq;
  541. kvmppc_create_vcpu_debugfs(vcpu, id);
  542. }
  543. return vcpu;
  544. }
  545. void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  546. {
  547. }
  548. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  549. {
  550. /* Make sure we're not using the vcpu anymore */
  551. hrtimer_cancel(&vcpu->arch.dec_timer);
  552. kvmppc_remove_vcpu_debugfs(vcpu);
  553. switch (vcpu->arch.irq_type) {
  554. case KVMPPC_IRQ_MPIC:
  555. kvmppc_mpic_disconnect_vcpu(vcpu->arch.mpic, vcpu);
  556. break;
  557. case KVMPPC_IRQ_XICS:
  558. kvmppc_xics_free_icp(vcpu);
  559. break;
  560. }
  561. kvmppc_core_vcpu_free(vcpu);
  562. }
  563. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  564. {
  565. kvm_arch_vcpu_free(vcpu);
  566. }
  567. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  568. {
  569. return kvmppc_core_pending_dec(vcpu);
  570. }
  571. enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
  572. {
  573. struct kvm_vcpu *vcpu;
  574. vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
  575. kvmppc_decrementer_func(vcpu);
  576. return HRTIMER_NORESTART;
  577. }
  578. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  579. {
  580. int ret;
  581. hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  582. vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
  583. vcpu->arch.dec_expires = ~(u64)0;
  584. #ifdef CONFIG_KVM_EXIT_TIMING
  585. mutex_init(&vcpu->arch.exit_timing_lock);
  586. #endif
  587. ret = kvmppc_subarch_vcpu_init(vcpu);
  588. return ret;
  589. }
  590. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  591. {
  592. kvmppc_mmu_destroy(vcpu);
  593. kvmppc_subarch_vcpu_uninit(vcpu);
  594. }
  595. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  596. {
  597. #ifdef CONFIG_BOOKE
  598. /*
  599. * vrsave (formerly usprg0) isn't used by Linux, but may
  600. * be used by the guest.
  601. *
  602. * On non-booke this is associated with Altivec and
  603. * is handled by code in book3s.c.
  604. */
  605. mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
  606. #endif
  607. kvmppc_core_vcpu_load(vcpu, cpu);
  608. }
  609. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  610. {
  611. kvmppc_core_vcpu_put(vcpu);
  612. #ifdef CONFIG_BOOKE
  613. vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
  614. #endif
  615. }
  616. static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
  617. struct kvm_run *run)
  618. {
  619. u64 uninitialized_var(gpr);
  620. if (run->mmio.len > sizeof(gpr)) {
  621. printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
  622. return;
  623. }
  624. if (!vcpu->arch.mmio_host_swabbed) {
  625. switch (run->mmio.len) {
  626. case 8: gpr = *(u64 *)run->mmio.data; break;
  627. case 4: gpr = *(u32 *)run->mmio.data; break;
  628. case 2: gpr = *(u16 *)run->mmio.data; break;
  629. case 1: gpr = *(u8 *)run->mmio.data; break;
  630. }
  631. } else {
  632. switch (run->mmio.len) {
  633. case 8: gpr = swab64(*(u64 *)run->mmio.data); break;
  634. case 4: gpr = swab32(*(u32 *)run->mmio.data); break;
  635. case 2: gpr = swab16(*(u16 *)run->mmio.data); break;
  636. case 1: gpr = *(u8 *)run->mmio.data; break;
  637. }
  638. }
  639. if (vcpu->arch.mmio_sign_extend) {
  640. switch (run->mmio.len) {
  641. #ifdef CONFIG_PPC64
  642. case 4:
  643. gpr = (s64)(s32)gpr;
  644. break;
  645. #endif
  646. case 2:
  647. gpr = (s64)(s16)gpr;
  648. break;
  649. case 1:
  650. gpr = (s64)(s8)gpr;
  651. break;
  652. }
  653. }
  654. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  655. switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
  656. case KVM_MMIO_REG_GPR:
  657. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  658. break;
  659. case KVM_MMIO_REG_FPR:
  660. VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
  661. break;
  662. #ifdef CONFIG_PPC_BOOK3S
  663. case KVM_MMIO_REG_QPR:
  664. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  665. break;
  666. case KVM_MMIO_REG_FQPR:
  667. VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
  668. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  669. break;
  670. #endif
  671. default:
  672. BUG();
  673. }
  674. }
  675. int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  676. unsigned int rt, unsigned int bytes,
  677. int is_default_endian)
  678. {
  679. int idx, ret;
  680. bool host_swabbed;
  681. /* Pity C doesn't have a logical XOR operator */
  682. if (kvmppc_need_byteswap(vcpu)) {
  683. host_swabbed = is_default_endian;
  684. } else {
  685. host_swabbed = !is_default_endian;
  686. }
  687. if (bytes > sizeof(run->mmio.data)) {
  688. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  689. run->mmio.len);
  690. }
  691. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  692. run->mmio.len = bytes;
  693. run->mmio.is_write = 0;
  694. vcpu->arch.io_gpr = rt;
  695. vcpu->arch.mmio_host_swabbed = host_swabbed;
  696. vcpu->mmio_needed = 1;
  697. vcpu->mmio_is_write = 0;
  698. vcpu->arch.mmio_sign_extend = 0;
  699. idx = srcu_read_lock(&vcpu->kvm->srcu);
  700. ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
  701. bytes, &run->mmio.data);
  702. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  703. if (!ret) {
  704. kvmppc_complete_mmio_load(vcpu, run);
  705. vcpu->mmio_needed = 0;
  706. return EMULATE_DONE;
  707. }
  708. return EMULATE_DO_MMIO;
  709. }
  710. EXPORT_SYMBOL_GPL(kvmppc_handle_load);
  711. /* Same as above, but sign extends */
  712. int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
  713. unsigned int rt, unsigned int bytes,
  714. int is_default_endian)
  715. {
  716. int r;
  717. vcpu->arch.mmio_sign_extend = 1;
  718. r = kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian);
  719. return r;
  720. }
  721. int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
  722. u64 val, unsigned int bytes, int is_default_endian)
  723. {
  724. void *data = run->mmio.data;
  725. int idx, ret;
  726. bool host_swabbed;
  727. /* Pity C doesn't have a logical XOR operator */
  728. if (kvmppc_need_byteswap(vcpu)) {
  729. host_swabbed = is_default_endian;
  730. } else {
  731. host_swabbed = !is_default_endian;
  732. }
  733. if (bytes > sizeof(run->mmio.data)) {
  734. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  735. run->mmio.len);
  736. }
  737. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  738. run->mmio.len = bytes;
  739. run->mmio.is_write = 1;
  740. vcpu->mmio_needed = 1;
  741. vcpu->mmio_is_write = 1;
  742. /* Store the value at the lowest bytes in 'data'. */
  743. if (!host_swabbed) {
  744. switch (bytes) {
  745. case 8: *(u64 *)data = val; break;
  746. case 4: *(u32 *)data = val; break;
  747. case 2: *(u16 *)data = val; break;
  748. case 1: *(u8 *)data = val; break;
  749. }
  750. } else {
  751. switch (bytes) {
  752. case 8: *(u64 *)data = swab64(val); break;
  753. case 4: *(u32 *)data = swab32(val); break;
  754. case 2: *(u16 *)data = swab16(val); break;
  755. case 1: *(u8 *)data = val; break;
  756. }
  757. }
  758. idx = srcu_read_lock(&vcpu->kvm->srcu);
  759. ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
  760. bytes, &run->mmio.data);
  761. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  762. if (!ret) {
  763. vcpu->mmio_needed = 0;
  764. return EMULATE_DONE;
  765. }
  766. return EMULATE_DO_MMIO;
  767. }
  768. EXPORT_SYMBOL_GPL(kvmppc_handle_store);
  769. int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  770. {
  771. int r = 0;
  772. union kvmppc_one_reg val;
  773. int size;
  774. size = one_reg_size(reg->id);
  775. if (size > sizeof(val))
  776. return -EINVAL;
  777. r = kvmppc_get_one_reg(vcpu, reg->id, &val);
  778. if (r == -EINVAL) {
  779. r = 0;
  780. switch (reg->id) {
  781. #ifdef CONFIG_ALTIVEC
  782. case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
  783. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  784. r = -ENXIO;
  785. break;
  786. }
  787. vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0] = val.vval;
  788. break;
  789. case KVM_REG_PPC_VSCR:
  790. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  791. r = -ENXIO;
  792. break;
  793. }
  794. vcpu->arch.vr.vscr.u[3] = set_reg_val(reg->id, val);
  795. break;
  796. case KVM_REG_PPC_VRSAVE:
  797. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  798. r = -ENXIO;
  799. break;
  800. }
  801. vcpu->arch.vrsave = set_reg_val(reg->id, val);
  802. break;
  803. #endif /* CONFIG_ALTIVEC */
  804. default:
  805. r = -EINVAL;
  806. break;
  807. }
  808. }
  809. if (r)
  810. return r;
  811. if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size))
  812. r = -EFAULT;
  813. return r;
  814. }
  815. int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  816. {
  817. int r;
  818. union kvmppc_one_reg val;
  819. int size;
  820. size = one_reg_size(reg->id);
  821. if (size > sizeof(val))
  822. return -EINVAL;
  823. if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size))
  824. return -EFAULT;
  825. r = kvmppc_set_one_reg(vcpu, reg->id, &val);
  826. if (r == -EINVAL) {
  827. r = 0;
  828. switch (reg->id) {
  829. #ifdef CONFIG_ALTIVEC
  830. case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
  831. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  832. r = -ENXIO;
  833. break;
  834. }
  835. val.vval = vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0];
  836. break;
  837. case KVM_REG_PPC_VSCR:
  838. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  839. r = -ENXIO;
  840. break;
  841. }
  842. val = get_reg_val(reg->id, vcpu->arch.vr.vscr.u[3]);
  843. break;
  844. case KVM_REG_PPC_VRSAVE:
  845. val = get_reg_val(reg->id, vcpu->arch.vrsave);
  846. break;
  847. #endif /* CONFIG_ALTIVEC */
  848. default:
  849. r = -EINVAL;
  850. break;
  851. }
  852. }
  853. return r;
  854. }
  855. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  856. {
  857. int r;
  858. sigset_t sigsaved;
  859. if (vcpu->sigset_active)
  860. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  861. if (vcpu->mmio_needed) {
  862. if (!vcpu->mmio_is_write)
  863. kvmppc_complete_mmio_load(vcpu, run);
  864. vcpu->mmio_needed = 0;
  865. } else if (vcpu->arch.osi_needed) {
  866. u64 *gprs = run->osi.gprs;
  867. int i;
  868. for (i = 0; i < 32; i++)
  869. kvmppc_set_gpr(vcpu, i, gprs[i]);
  870. vcpu->arch.osi_needed = 0;
  871. } else if (vcpu->arch.hcall_needed) {
  872. int i;
  873. kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
  874. for (i = 0; i < 9; ++i)
  875. kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
  876. vcpu->arch.hcall_needed = 0;
  877. #ifdef CONFIG_BOOKE
  878. } else if (vcpu->arch.epr_needed) {
  879. kvmppc_set_epr(vcpu, run->epr.epr);
  880. vcpu->arch.epr_needed = 0;
  881. #endif
  882. }
  883. r = kvmppc_vcpu_run(run, vcpu);
  884. if (vcpu->sigset_active)
  885. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  886. return r;
  887. }
  888. int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
  889. {
  890. if (irq->irq == KVM_INTERRUPT_UNSET) {
  891. kvmppc_core_dequeue_external(vcpu);
  892. return 0;
  893. }
  894. kvmppc_core_queue_external(vcpu, irq);
  895. kvm_vcpu_kick(vcpu);
  896. return 0;
  897. }
  898. static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
  899. struct kvm_enable_cap *cap)
  900. {
  901. int r;
  902. if (cap->flags)
  903. return -EINVAL;
  904. switch (cap->cap) {
  905. case KVM_CAP_PPC_OSI:
  906. r = 0;
  907. vcpu->arch.osi_enabled = true;
  908. break;
  909. case KVM_CAP_PPC_PAPR:
  910. r = 0;
  911. vcpu->arch.papr_enabled = true;
  912. break;
  913. case KVM_CAP_PPC_EPR:
  914. r = 0;
  915. if (cap->args[0])
  916. vcpu->arch.epr_flags |= KVMPPC_EPR_USER;
  917. else
  918. vcpu->arch.epr_flags &= ~KVMPPC_EPR_USER;
  919. break;
  920. #ifdef CONFIG_BOOKE
  921. case KVM_CAP_PPC_BOOKE_WATCHDOG:
  922. r = 0;
  923. vcpu->arch.watchdog_enabled = true;
  924. break;
  925. #endif
  926. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  927. case KVM_CAP_SW_TLB: {
  928. struct kvm_config_tlb cfg;
  929. void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
  930. r = -EFAULT;
  931. if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
  932. break;
  933. r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
  934. break;
  935. }
  936. #endif
  937. #ifdef CONFIG_KVM_MPIC
  938. case KVM_CAP_IRQ_MPIC: {
  939. struct fd f;
  940. struct kvm_device *dev;
  941. r = -EBADF;
  942. f = fdget(cap->args[0]);
  943. if (!f.file)
  944. break;
  945. r = -EPERM;
  946. dev = kvm_device_from_filp(f.file);
  947. if (dev)
  948. r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]);
  949. fdput(f);
  950. break;
  951. }
  952. #endif
  953. #ifdef CONFIG_KVM_XICS
  954. case KVM_CAP_IRQ_XICS: {
  955. struct fd f;
  956. struct kvm_device *dev;
  957. r = -EBADF;
  958. f = fdget(cap->args[0]);
  959. if (!f.file)
  960. break;
  961. r = -EPERM;
  962. dev = kvm_device_from_filp(f.file);
  963. if (dev)
  964. r = kvmppc_xics_connect_vcpu(dev, vcpu, cap->args[1]);
  965. fdput(f);
  966. break;
  967. }
  968. #endif /* CONFIG_KVM_XICS */
  969. default:
  970. r = -EINVAL;
  971. break;
  972. }
  973. if (!r)
  974. r = kvmppc_sanity_check(vcpu);
  975. return r;
  976. }
  977. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  978. struct kvm_mp_state *mp_state)
  979. {
  980. return -EINVAL;
  981. }
  982. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  983. struct kvm_mp_state *mp_state)
  984. {
  985. return -EINVAL;
  986. }
  987. long kvm_arch_vcpu_ioctl(struct file *filp,
  988. unsigned int ioctl, unsigned long arg)
  989. {
  990. struct kvm_vcpu *vcpu = filp->private_data;
  991. void __user *argp = (void __user *)arg;
  992. long r;
  993. switch (ioctl) {
  994. case KVM_INTERRUPT: {
  995. struct kvm_interrupt irq;
  996. r = -EFAULT;
  997. if (copy_from_user(&irq, argp, sizeof(irq)))
  998. goto out;
  999. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  1000. goto out;
  1001. }
  1002. case KVM_ENABLE_CAP:
  1003. {
  1004. struct kvm_enable_cap cap;
  1005. r = -EFAULT;
  1006. if (copy_from_user(&cap, argp, sizeof(cap)))
  1007. goto out;
  1008. r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
  1009. break;
  1010. }
  1011. case KVM_SET_ONE_REG:
  1012. case KVM_GET_ONE_REG:
  1013. {
  1014. struct kvm_one_reg reg;
  1015. r = -EFAULT;
  1016. if (copy_from_user(&reg, argp, sizeof(reg)))
  1017. goto out;
  1018. if (ioctl == KVM_SET_ONE_REG)
  1019. r = kvm_vcpu_ioctl_set_one_reg(vcpu, &reg);
  1020. else
  1021. r = kvm_vcpu_ioctl_get_one_reg(vcpu, &reg);
  1022. break;
  1023. }
  1024. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  1025. case KVM_DIRTY_TLB: {
  1026. struct kvm_dirty_tlb dirty;
  1027. r = -EFAULT;
  1028. if (copy_from_user(&dirty, argp, sizeof(dirty)))
  1029. goto out;
  1030. r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
  1031. break;
  1032. }
  1033. #endif
  1034. default:
  1035. r = -EINVAL;
  1036. }
  1037. out:
  1038. return r;
  1039. }
  1040. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  1041. {
  1042. return VM_FAULT_SIGBUS;
  1043. }
  1044. static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
  1045. {
  1046. u32 inst_nop = 0x60000000;
  1047. #ifdef CONFIG_KVM_BOOKE_HV
  1048. u32 inst_sc1 = 0x44000022;
  1049. pvinfo->hcall[0] = cpu_to_be32(inst_sc1);
  1050. pvinfo->hcall[1] = cpu_to_be32(inst_nop);
  1051. pvinfo->hcall[2] = cpu_to_be32(inst_nop);
  1052. pvinfo->hcall[3] = cpu_to_be32(inst_nop);
  1053. #else
  1054. u32 inst_lis = 0x3c000000;
  1055. u32 inst_ori = 0x60000000;
  1056. u32 inst_sc = 0x44000002;
  1057. u32 inst_imm_mask = 0xffff;
  1058. /*
  1059. * The hypercall to get into KVM from within guest context is as
  1060. * follows:
  1061. *
  1062. * lis r0, r0, KVM_SC_MAGIC_R0@h
  1063. * ori r0, KVM_SC_MAGIC_R0@l
  1064. * sc
  1065. * nop
  1066. */
  1067. pvinfo->hcall[0] = cpu_to_be32(inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask));
  1068. pvinfo->hcall[1] = cpu_to_be32(inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask));
  1069. pvinfo->hcall[2] = cpu_to_be32(inst_sc);
  1070. pvinfo->hcall[3] = cpu_to_be32(inst_nop);
  1071. #endif
  1072. pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
  1073. return 0;
  1074. }
  1075. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
  1076. bool line_status)
  1077. {
  1078. if (!irqchip_in_kernel(kvm))
  1079. return -ENXIO;
  1080. irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
  1081. irq_event->irq, irq_event->level,
  1082. line_status);
  1083. return 0;
  1084. }
  1085. static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  1086. struct kvm_enable_cap *cap)
  1087. {
  1088. int r;
  1089. if (cap->flags)
  1090. return -EINVAL;
  1091. switch (cap->cap) {
  1092. #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
  1093. case KVM_CAP_PPC_ENABLE_HCALL: {
  1094. unsigned long hcall = cap->args[0];
  1095. r = -EINVAL;
  1096. if (hcall > MAX_HCALL_OPCODE || (hcall & 3) ||
  1097. cap->args[1] > 1)
  1098. break;
  1099. if (!kvmppc_book3s_hcall_implemented(kvm, hcall))
  1100. break;
  1101. if (cap->args[1])
  1102. set_bit(hcall / 4, kvm->arch.enabled_hcalls);
  1103. else
  1104. clear_bit(hcall / 4, kvm->arch.enabled_hcalls);
  1105. r = 0;
  1106. break;
  1107. }
  1108. #endif
  1109. default:
  1110. r = -EINVAL;
  1111. break;
  1112. }
  1113. return r;
  1114. }
  1115. long kvm_arch_vm_ioctl(struct file *filp,
  1116. unsigned int ioctl, unsigned long arg)
  1117. {
  1118. struct kvm *kvm __maybe_unused = filp->private_data;
  1119. void __user *argp = (void __user *)arg;
  1120. long r;
  1121. switch (ioctl) {
  1122. case KVM_PPC_GET_PVINFO: {
  1123. struct kvm_ppc_pvinfo pvinfo;
  1124. memset(&pvinfo, 0, sizeof(pvinfo));
  1125. r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
  1126. if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
  1127. r = -EFAULT;
  1128. goto out;
  1129. }
  1130. break;
  1131. }
  1132. case KVM_ENABLE_CAP:
  1133. {
  1134. struct kvm_enable_cap cap;
  1135. r = -EFAULT;
  1136. if (copy_from_user(&cap, argp, sizeof(cap)))
  1137. goto out;
  1138. r = kvm_vm_ioctl_enable_cap(kvm, &cap);
  1139. break;
  1140. }
  1141. #ifdef CONFIG_PPC_BOOK3S_64
  1142. case KVM_CREATE_SPAPR_TCE: {
  1143. struct kvm_create_spapr_tce create_tce;
  1144. r = -EFAULT;
  1145. if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
  1146. goto out;
  1147. r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce);
  1148. goto out;
  1149. }
  1150. case KVM_PPC_GET_SMMU_INFO: {
  1151. struct kvm_ppc_smmu_info info;
  1152. struct kvm *kvm = filp->private_data;
  1153. memset(&info, 0, sizeof(info));
  1154. r = kvm->arch.kvm_ops->get_smmu_info(kvm, &info);
  1155. if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
  1156. r = -EFAULT;
  1157. break;
  1158. }
  1159. case KVM_PPC_RTAS_DEFINE_TOKEN: {
  1160. struct kvm *kvm = filp->private_data;
  1161. r = kvm_vm_ioctl_rtas_define_token(kvm, argp);
  1162. break;
  1163. }
  1164. default: {
  1165. struct kvm *kvm = filp->private_data;
  1166. r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg);
  1167. }
  1168. #else /* CONFIG_PPC_BOOK3S_64 */
  1169. default:
  1170. r = -ENOTTY;
  1171. #endif
  1172. }
  1173. out:
  1174. return r;
  1175. }
  1176. static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
  1177. static unsigned long nr_lpids;
  1178. long kvmppc_alloc_lpid(void)
  1179. {
  1180. long lpid;
  1181. do {
  1182. lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
  1183. if (lpid >= nr_lpids) {
  1184. pr_err("%s: No LPIDs free\n", __func__);
  1185. return -ENOMEM;
  1186. }
  1187. } while (test_and_set_bit(lpid, lpid_inuse));
  1188. return lpid;
  1189. }
  1190. EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid);
  1191. void kvmppc_claim_lpid(long lpid)
  1192. {
  1193. set_bit(lpid, lpid_inuse);
  1194. }
  1195. EXPORT_SYMBOL_GPL(kvmppc_claim_lpid);
  1196. void kvmppc_free_lpid(long lpid)
  1197. {
  1198. clear_bit(lpid, lpid_inuse);
  1199. }
  1200. EXPORT_SYMBOL_GPL(kvmppc_free_lpid);
  1201. void kvmppc_init_lpid(unsigned long nr_lpids_param)
  1202. {
  1203. nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
  1204. memset(lpid_inuse, 0, sizeof(lpid_inuse));
  1205. }
  1206. EXPORT_SYMBOL_GPL(kvmppc_init_lpid);
  1207. int kvm_arch_init(void *opaque)
  1208. {
  1209. return 0;
  1210. }
  1211. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ppc_instr);