powerpc.c 24 KB

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