powerpc.c 23 KB

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