powerpc.c 22 KB

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