powerpc.c 22 KB

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