powerpc.c 25 KB

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