powerpc.c 21 KB

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