powerpc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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. r = 1;
  296. break;
  297. #endif /* CONFIG_PPC_BOOK3S_64 */
  298. #ifdef CONFIG_KVM_BOOK3S_64_HV
  299. case KVM_CAP_PPC_SMT:
  300. r = threads_per_core;
  301. break;
  302. case KVM_CAP_PPC_RMA:
  303. r = 1;
  304. /* PPC970 requires an RMA */
  305. if (cpu_has_feature(CPU_FTR_ARCH_201))
  306. r = 2;
  307. break;
  308. #endif
  309. case KVM_CAP_SYNC_MMU:
  310. #ifdef CONFIG_KVM_BOOK3S_64_HV
  311. r = cpu_has_feature(CPU_FTR_ARCH_206) ? 1 : 0;
  312. #elif defined(KVM_ARCH_WANT_MMU_NOTIFIER)
  313. r = 1;
  314. #else
  315. r = 0;
  316. break;
  317. #endif
  318. #ifdef CONFIG_KVM_BOOK3S_64_HV
  319. case KVM_CAP_PPC_HTAB_FD:
  320. r = 1;
  321. break;
  322. #endif
  323. break;
  324. case KVM_CAP_NR_VCPUS:
  325. /*
  326. * Recommending a number of CPUs is somewhat arbitrary; we
  327. * return the number of present CPUs for -HV (since a host
  328. * will have secondary threads "offline"), and for other KVM
  329. * implementations just count online CPUs.
  330. */
  331. #ifdef CONFIG_KVM_BOOK3S_64_HV
  332. r = num_present_cpus();
  333. #else
  334. r = num_online_cpus();
  335. #endif
  336. break;
  337. case KVM_CAP_MAX_VCPUS:
  338. r = KVM_MAX_VCPUS;
  339. break;
  340. #ifdef CONFIG_PPC_BOOK3S_64
  341. case KVM_CAP_PPC_GET_SMMU_INFO:
  342. r = 1;
  343. break;
  344. #endif
  345. default:
  346. r = 0;
  347. break;
  348. }
  349. return r;
  350. }
  351. long kvm_arch_dev_ioctl(struct file *filp,
  352. unsigned int ioctl, unsigned long arg)
  353. {
  354. return -EINVAL;
  355. }
  356. void kvm_arch_free_memslot(struct kvm_memory_slot *free,
  357. struct kvm_memory_slot *dont)
  358. {
  359. kvmppc_core_free_memslot(free, dont);
  360. }
  361. int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
  362. {
  363. return kvmppc_core_create_memslot(slot, npages);
  364. }
  365. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  366. struct kvm_memory_slot *memslot,
  367. struct kvm_userspace_memory_region *mem,
  368. enum kvm_mr_change change)
  369. {
  370. return kvmppc_core_prepare_memory_region(kvm, memslot, mem);
  371. }
  372. void kvm_arch_commit_memory_region(struct kvm *kvm,
  373. struct kvm_userspace_memory_region *mem,
  374. const struct kvm_memory_slot *old,
  375. enum kvm_mr_change change)
  376. {
  377. kvmppc_core_commit_memory_region(kvm, mem, old);
  378. }
  379. void kvm_arch_flush_shadow_all(struct kvm *kvm)
  380. {
  381. }
  382. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  383. struct kvm_memory_slot *slot)
  384. {
  385. kvmppc_core_flush_memslot(kvm, slot);
  386. }
  387. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  388. {
  389. struct kvm_vcpu *vcpu;
  390. vcpu = kvmppc_core_vcpu_create(kvm, id);
  391. if (!IS_ERR(vcpu)) {
  392. vcpu->arch.wqp = &vcpu->wq;
  393. kvmppc_create_vcpu_debugfs(vcpu, id);
  394. }
  395. return vcpu;
  396. }
  397. int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  398. {
  399. return 0;
  400. }
  401. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  402. {
  403. /* Make sure we're not using the vcpu anymore */
  404. hrtimer_cancel(&vcpu->arch.dec_timer);
  405. tasklet_kill(&vcpu->arch.tasklet);
  406. kvmppc_remove_vcpu_debugfs(vcpu);
  407. switch (vcpu->arch.irq_type) {
  408. case KVMPPC_IRQ_MPIC:
  409. kvmppc_mpic_disconnect_vcpu(vcpu->arch.mpic, vcpu);
  410. break;
  411. }
  412. kvmppc_core_vcpu_free(vcpu);
  413. }
  414. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  415. {
  416. kvm_arch_vcpu_free(vcpu);
  417. }
  418. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  419. {
  420. return kvmppc_core_pending_dec(vcpu);
  421. }
  422. /*
  423. * low level hrtimer wake routine. Because this runs in hardirq context
  424. * we schedule a tasklet to do the real work.
  425. */
  426. enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
  427. {
  428. struct kvm_vcpu *vcpu;
  429. vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
  430. tasklet_schedule(&vcpu->arch.tasklet);
  431. return HRTIMER_NORESTART;
  432. }
  433. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  434. {
  435. int ret;
  436. hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  437. tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu);
  438. vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
  439. vcpu->arch.dec_expires = ~(u64)0;
  440. #ifdef CONFIG_KVM_EXIT_TIMING
  441. mutex_init(&vcpu->arch.exit_timing_lock);
  442. #endif
  443. ret = kvmppc_subarch_vcpu_init(vcpu);
  444. return ret;
  445. }
  446. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  447. {
  448. kvmppc_mmu_destroy(vcpu);
  449. kvmppc_subarch_vcpu_uninit(vcpu);
  450. }
  451. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  452. {
  453. #ifdef CONFIG_BOOKE
  454. /*
  455. * vrsave (formerly usprg0) isn't used by Linux, but may
  456. * be used by the guest.
  457. *
  458. * On non-booke this is associated with Altivec and
  459. * is handled by code in book3s.c.
  460. */
  461. mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
  462. #endif
  463. kvmppc_core_vcpu_load(vcpu, cpu);
  464. }
  465. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  466. {
  467. kvmppc_core_vcpu_put(vcpu);
  468. #ifdef CONFIG_BOOKE
  469. vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
  470. #endif
  471. }
  472. static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
  473. struct kvm_run *run)
  474. {
  475. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, run->dcr.data);
  476. }
  477. static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
  478. struct kvm_run *run)
  479. {
  480. u64 uninitialized_var(gpr);
  481. if (run->mmio.len > sizeof(gpr)) {
  482. printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
  483. return;
  484. }
  485. if (vcpu->arch.mmio_is_bigendian) {
  486. switch (run->mmio.len) {
  487. case 8: gpr = *(u64 *)run->mmio.data; break;
  488. case 4: gpr = *(u32 *)run->mmio.data; break;
  489. case 2: gpr = *(u16 *)run->mmio.data; break;
  490. case 1: gpr = *(u8 *)run->mmio.data; break;
  491. }
  492. } else {
  493. /* Convert BE data from userland back to LE. */
  494. switch (run->mmio.len) {
  495. case 4: gpr = ld_le32((u32 *)run->mmio.data); break;
  496. case 2: gpr = ld_le16((u16 *)run->mmio.data); break;
  497. case 1: gpr = *(u8 *)run->mmio.data; break;
  498. }
  499. }
  500. if (vcpu->arch.mmio_sign_extend) {
  501. switch (run->mmio.len) {
  502. #ifdef CONFIG_PPC64
  503. case 4:
  504. gpr = (s64)(s32)gpr;
  505. break;
  506. #endif
  507. case 2:
  508. gpr = (s64)(s16)gpr;
  509. break;
  510. case 1:
  511. gpr = (s64)(s8)gpr;
  512. break;
  513. }
  514. }
  515. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  516. switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
  517. case KVM_MMIO_REG_GPR:
  518. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  519. break;
  520. case KVM_MMIO_REG_FPR:
  521. vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  522. break;
  523. #ifdef CONFIG_PPC_BOOK3S
  524. case KVM_MMIO_REG_QPR:
  525. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  526. break;
  527. case KVM_MMIO_REG_FQPR:
  528. vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  529. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  530. break;
  531. #endif
  532. default:
  533. BUG();
  534. }
  535. }
  536. int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  537. unsigned int rt, unsigned int bytes, int is_bigendian)
  538. {
  539. if (bytes > sizeof(run->mmio.data)) {
  540. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  541. run->mmio.len);
  542. }
  543. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  544. run->mmio.len = bytes;
  545. run->mmio.is_write = 0;
  546. vcpu->arch.io_gpr = rt;
  547. vcpu->arch.mmio_is_bigendian = is_bigendian;
  548. vcpu->mmio_needed = 1;
  549. vcpu->mmio_is_write = 0;
  550. vcpu->arch.mmio_sign_extend = 0;
  551. if (!kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, run->mmio.phys_addr,
  552. bytes, &run->mmio.data)) {
  553. kvmppc_complete_mmio_load(vcpu, run);
  554. vcpu->mmio_needed = 0;
  555. return EMULATE_DONE;
  556. }
  557. return EMULATE_DO_MMIO;
  558. }
  559. /* Same as above, but sign extends */
  560. int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
  561. unsigned int rt, unsigned int bytes, int is_bigendian)
  562. {
  563. int r;
  564. vcpu->arch.mmio_sign_extend = 1;
  565. r = kvmppc_handle_load(run, vcpu, rt, bytes, is_bigendian);
  566. return r;
  567. }
  568. int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
  569. u64 val, unsigned int bytes, int is_bigendian)
  570. {
  571. void *data = run->mmio.data;
  572. if (bytes > sizeof(run->mmio.data)) {
  573. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  574. run->mmio.len);
  575. }
  576. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  577. run->mmio.len = bytes;
  578. run->mmio.is_write = 1;
  579. vcpu->mmio_needed = 1;
  580. vcpu->mmio_is_write = 1;
  581. /* Store the value at the lowest bytes in 'data'. */
  582. if (is_bigendian) {
  583. switch (bytes) {
  584. case 8: *(u64 *)data = val; break;
  585. case 4: *(u32 *)data = val; break;
  586. case 2: *(u16 *)data = val; break;
  587. case 1: *(u8 *)data = val; break;
  588. }
  589. } else {
  590. /* Store LE value into 'data'. */
  591. switch (bytes) {
  592. case 4: st_le32(data, val); break;
  593. case 2: st_le16(data, val); break;
  594. case 1: *(u8 *)data = val; break;
  595. }
  596. }
  597. if (!kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, run->mmio.phys_addr,
  598. bytes, &run->mmio.data)) {
  599. vcpu->mmio_needed = 0;
  600. return EMULATE_DONE;
  601. }
  602. return EMULATE_DO_MMIO;
  603. }
  604. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  605. {
  606. int r;
  607. sigset_t sigsaved;
  608. if (vcpu->sigset_active)
  609. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  610. if (vcpu->mmio_needed) {
  611. if (!vcpu->mmio_is_write)
  612. kvmppc_complete_mmio_load(vcpu, run);
  613. vcpu->mmio_needed = 0;
  614. } else if (vcpu->arch.dcr_needed) {
  615. if (!vcpu->arch.dcr_is_write)
  616. kvmppc_complete_dcr_load(vcpu, run);
  617. vcpu->arch.dcr_needed = 0;
  618. } else if (vcpu->arch.osi_needed) {
  619. u64 *gprs = run->osi.gprs;
  620. int i;
  621. for (i = 0; i < 32; i++)
  622. kvmppc_set_gpr(vcpu, i, gprs[i]);
  623. vcpu->arch.osi_needed = 0;
  624. } else if (vcpu->arch.hcall_needed) {
  625. int i;
  626. kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
  627. for (i = 0; i < 9; ++i)
  628. kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
  629. vcpu->arch.hcall_needed = 0;
  630. #ifdef CONFIG_BOOKE
  631. } else if (vcpu->arch.epr_needed) {
  632. kvmppc_set_epr(vcpu, run->epr.epr);
  633. vcpu->arch.epr_needed = 0;
  634. #endif
  635. }
  636. r = kvmppc_vcpu_run(run, vcpu);
  637. if (vcpu->sigset_active)
  638. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  639. return r;
  640. }
  641. int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
  642. {
  643. if (irq->irq == KVM_INTERRUPT_UNSET) {
  644. kvmppc_core_dequeue_external(vcpu);
  645. return 0;
  646. }
  647. kvmppc_core_queue_external(vcpu, irq);
  648. kvm_vcpu_kick(vcpu);
  649. return 0;
  650. }
  651. static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
  652. struct kvm_enable_cap *cap)
  653. {
  654. int r;
  655. if (cap->flags)
  656. return -EINVAL;
  657. switch (cap->cap) {
  658. case KVM_CAP_PPC_OSI:
  659. r = 0;
  660. vcpu->arch.osi_enabled = true;
  661. break;
  662. case KVM_CAP_PPC_PAPR:
  663. r = 0;
  664. vcpu->arch.papr_enabled = true;
  665. break;
  666. case KVM_CAP_PPC_EPR:
  667. r = 0;
  668. if (cap->args[0])
  669. vcpu->arch.epr_flags |= KVMPPC_EPR_USER;
  670. else
  671. vcpu->arch.epr_flags &= ~KVMPPC_EPR_USER;
  672. break;
  673. #ifdef CONFIG_BOOKE
  674. case KVM_CAP_PPC_BOOKE_WATCHDOG:
  675. r = 0;
  676. vcpu->arch.watchdog_enabled = true;
  677. break;
  678. #endif
  679. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  680. case KVM_CAP_SW_TLB: {
  681. struct kvm_config_tlb cfg;
  682. void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
  683. r = -EFAULT;
  684. if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
  685. break;
  686. r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
  687. break;
  688. }
  689. #endif
  690. #ifdef CONFIG_KVM_MPIC
  691. case KVM_CAP_IRQ_MPIC: {
  692. struct file *filp;
  693. struct kvm_device *dev;
  694. r = -EBADF;
  695. filp = fget(cap->args[0]);
  696. if (!filp)
  697. break;
  698. r = -EPERM;
  699. dev = kvm_device_from_filp(filp);
  700. if (dev)
  701. r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]);
  702. fput(filp);
  703. break;
  704. }
  705. #endif
  706. default:
  707. r = -EINVAL;
  708. break;
  709. }
  710. if (!r)
  711. r = kvmppc_sanity_check(vcpu);
  712. return r;
  713. }
  714. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  715. struct kvm_mp_state *mp_state)
  716. {
  717. return -EINVAL;
  718. }
  719. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  720. struct kvm_mp_state *mp_state)
  721. {
  722. return -EINVAL;
  723. }
  724. long kvm_arch_vcpu_ioctl(struct file *filp,
  725. unsigned int ioctl, unsigned long arg)
  726. {
  727. struct kvm_vcpu *vcpu = filp->private_data;
  728. void __user *argp = (void __user *)arg;
  729. long r;
  730. switch (ioctl) {
  731. case KVM_INTERRUPT: {
  732. struct kvm_interrupt irq;
  733. r = -EFAULT;
  734. if (copy_from_user(&irq, argp, sizeof(irq)))
  735. goto out;
  736. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  737. goto out;
  738. }
  739. case KVM_ENABLE_CAP:
  740. {
  741. struct kvm_enable_cap cap;
  742. r = -EFAULT;
  743. if (copy_from_user(&cap, argp, sizeof(cap)))
  744. goto out;
  745. r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
  746. break;
  747. }
  748. case KVM_SET_ONE_REG:
  749. case KVM_GET_ONE_REG:
  750. {
  751. struct kvm_one_reg reg;
  752. r = -EFAULT;
  753. if (copy_from_user(&reg, argp, sizeof(reg)))
  754. goto out;
  755. if (ioctl == KVM_SET_ONE_REG)
  756. r = kvm_vcpu_ioctl_set_one_reg(vcpu, &reg);
  757. else
  758. r = kvm_vcpu_ioctl_get_one_reg(vcpu, &reg);
  759. break;
  760. }
  761. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  762. case KVM_DIRTY_TLB: {
  763. struct kvm_dirty_tlb dirty;
  764. r = -EFAULT;
  765. if (copy_from_user(&dirty, argp, sizeof(dirty)))
  766. goto out;
  767. r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
  768. break;
  769. }
  770. #endif
  771. default:
  772. r = -EINVAL;
  773. }
  774. out:
  775. return r;
  776. }
  777. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  778. {
  779. return VM_FAULT_SIGBUS;
  780. }
  781. static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
  782. {
  783. u32 inst_nop = 0x60000000;
  784. #ifdef CONFIG_KVM_BOOKE_HV
  785. u32 inst_sc1 = 0x44000022;
  786. pvinfo->hcall[0] = inst_sc1;
  787. pvinfo->hcall[1] = inst_nop;
  788. pvinfo->hcall[2] = inst_nop;
  789. pvinfo->hcall[3] = inst_nop;
  790. #else
  791. u32 inst_lis = 0x3c000000;
  792. u32 inst_ori = 0x60000000;
  793. u32 inst_sc = 0x44000002;
  794. u32 inst_imm_mask = 0xffff;
  795. /*
  796. * The hypercall to get into KVM from within guest context is as
  797. * follows:
  798. *
  799. * lis r0, r0, KVM_SC_MAGIC_R0@h
  800. * ori r0, KVM_SC_MAGIC_R0@l
  801. * sc
  802. * nop
  803. */
  804. pvinfo->hcall[0] = inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask);
  805. pvinfo->hcall[1] = inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask);
  806. pvinfo->hcall[2] = inst_sc;
  807. pvinfo->hcall[3] = inst_nop;
  808. #endif
  809. pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
  810. return 0;
  811. }
  812. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
  813. bool line_status)
  814. {
  815. if (!irqchip_in_kernel(kvm))
  816. return -ENXIO;
  817. irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
  818. irq_event->irq, irq_event->level,
  819. line_status);
  820. return 0;
  821. }
  822. long kvm_arch_vm_ioctl(struct file *filp,
  823. unsigned int ioctl, unsigned long arg)
  824. {
  825. struct kvm *kvm __maybe_unused = filp->private_data;
  826. void __user *argp = (void __user *)arg;
  827. long r;
  828. switch (ioctl) {
  829. case KVM_PPC_GET_PVINFO: {
  830. struct kvm_ppc_pvinfo pvinfo;
  831. memset(&pvinfo, 0, sizeof(pvinfo));
  832. r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
  833. if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
  834. r = -EFAULT;
  835. goto out;
  836. }
  837. break;
  838. }
  839. #ifdef CONFIG_PPC_BOOK3S_64
  840. case KVM_CREATE_SPAPR_TCE: {
  841. struct kvm_create_spapr_tce create_tce;
  842. r = -EFAULT;
  843. if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
  844. goto out;
  845. r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce);
  846. goto out;
  847. }
  848. #endif /* CONFIG_PPC_BOOK3S_64 */
  849. #ifdef CONFIG_KVM_BOOK3S_64_HV
  850. case KVM_ALLOCATE_RMA: {
  851. struct kvm_allocate_rma rma;
  852. r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
  853. if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
  854. r = -EFAULT;
  855. break;
  856. }
  857. case KVM_PPC_ALLOCATE_HTAB: {
  858. u32 htab_order;
  859. r = -EFAULT;
  860. if (get_user(htab_order, (u32 __user *)argp))
  861. break;
  862. r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
  863. if (r)
  864. break;
  865. r = -EFAULT;
  866. if (put_user(htab_order, (u32 __user *)argp))
  867. break;
  868. r = 0;
  869. break;
  870. }
  871. case KVM_PPC_GET_HTAB_FD: {
  872. struct kvm_get_htab_fd ghf;
  873. r = -EFAULT;
  874. if (copy_from_user(&ghf, argp, sizeof(ghf)))
  875. break;
  876. r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
  877. break;
  878. }
  879. #endif /* CONFIG_KVM_BOOK3S_64_HV */
  880. #ifdef CONFIG_PPC_BOOK3S_64
  881. case KVM_PPC_GET_SMMU_INFO: {
  882. struct kvm_ppc_smmu_info info;
  883. memset(&info, 0, sizeof(info));
  884. r = kvm_vm_ioctl_get_smmu_info(kvm, &info);
  885. if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
  886. r = -EFAULT;
  887. break;
  888. }
  889. #endif /* CONFIG_PPC_BOOK3S_64 */
  890. default:
  891. r = -ENOTTY;
  892. }
  893. out:
  894. return r;
  895. }
  896. static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
  897. static unsigned long nr_lpids;
  898. long kvmppc_alloc_lpid(void)
  899. {
  900. long lpid;
  901. do {
  902. lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
  903. if (lpid >= nr_lpids) {
  904. pr_err("%s: No LPIDs free\n", __func__);
  905. return -ENOMEM;
  906. }
  907. } while (test_and_set_bit(lpid, lpid_inuse));
  908. return lpid;
  909. }
  910. void kvmppc_claim_lpid(long lpid)
  911. {
  912. set_bit(lpid, lpid_inuse);
  913. }
  914. void kvmppc_free_lpid(long lpid)
  915. {
  916. clear_bit(lpid, lpid_inuse);
  917. }
  918. void kvmppc_init_lpid(unsigned long nr_lpids_param)
  919. {
  920. nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
  921. memset(lpid_inuse, 0, sizeof(lpid_inuse));
  922. }
  923. int kvm_arch_init(void *opaque)
  924. {
  925. return 0;
  926. }
  927. void kvm_arch_exit(void)
  928. {
  929. }