powerpc.c 31 KB

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