powerpc.c 25 KB

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