powerpc.c 24 KB

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