powerpc.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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. case KVM_CAP_IOEVENTFD:
  269. r = 1;
  270. break;
  271. #ifndef CONFIG_KVM_BOOK3S_64_HV
  272. case KVM_CAP_PPC_PAIRED_SINGLES:
  273. case KVM_CAP_PPC_OSI:
  274. case KVM_CAP_PPC_GET_PVINFO:
  275. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  276. case KVM_CAP_SW_TLB:
  277. #endif
  278. r = 1;
  279. break;
  280. case KVM_CAP_COALESCED_MMIO:
  281. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  282. break;
  283. #endif
  284. #ifdef CONFIG_PPC_BOOK3S_64
  285. case KVM_CAP_SPAPR_TCE:
  286. case KVM_CAP_PPC_ALLOC_HTAB:
  287. r = 1;
  288. break;
  289. #endif /* CONFIG_PPC_BOOK3S_64 */
  290. #ifdef CONFIG_KVM_BOOK3S_64_HV
  291. case KVM_CAP_PPC_SMT:
  292. r = threads_per_core;
  293. break;
  294. case KVM_CAP_PPC_RMA:
  295. r = 1;
  296. /* PPC970 requires an RMA */
  297. if (cpu_has_feature(CPU_FTR_ARCH_201))
  298. r = 2;
  299. break;
  300. #endif
  301. case KVM_CAP_SYNC_MMU:
  302. #ifdef CONFIG_KVM_BOOK3S_64_HV
  303. r = cpu_has_feature(CPU_FTR_ARCH_206) ? 1 : 0;
  304. #elif defined(KVM_ARCH_WANT_MMU_NOTIFIER)
  305. r = 1;
  306. #else
  307. r = 0;
  308. break;
  309. #endif
  310. #ifdef CONFIG_KVM_BOOK3S_64_HV
  311. case KVM_CAP_PPC_HTAB_FD:
  312. r = 1;
  313. break;
  314. #endif
  315. break;
  316. case KVM_CAP_NR_VCPUS:
  317. /*
  318. * Recommending a number of CPUs is somewhat arbitrary; we
  319. * return the number of present CPUs for -HV (since a host
  320. * will have secondary threads "offline"), and for other KVM
  321. * implementations just count online CPUs.
  322. */
  323. #ifdef CONFIG_KVM_BOOK3S_64_HV
  324. r = num_present_cpus();
  325. #else
  326. r = num_online_cpus();
  327. #endif
  328. break;
  329. case KVM_CAP_MAX_VCPUS:
  330. r = KVM_MAX_VCPUS;
  331. break;
  332. #ifdef CONFIG_PPC_BOOK3S_64
  333. case KVM_CAP_PPC_GET_SMMU_INFO:
  334. r = 1;
  335. break;
  336. #endif
  337. default:
  338. r = 0;
  339. break;
  340. }
  341. return r;
  342. }
  343. long kvm_arch_dev_ioctl(struct file *filp,
  344. unsigned int ioctl, unsigned long arg)
  345. {
  346. return -EINVAL;
  347. }
  348. void kvm_arch_free_memslot(struct kvm_memory_slot *free,
  349. struct kvm_memory_slot *dont)
  350. {
  351. kvmppc_core_free_memslot(free, dont);
  352. }
  353. int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
  354. {
  355. return kvmppc_core_create_memslot(slot, npages);
  356. }
  357. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  358. struct kvm_memory_slot *memslot,
  359. struct kvm_memory_slot old,
  360. struct kvm_userspace_memory_region *mem,
  361. int user_alloc)
  362. {
  363. return kvmppc_core_prepare_memory_region(kvm, memslot, mem);
  364. }
  365. void kvm_arch_commit_memory_region(struct kvm *kvm,
  366. struct kvm_userspace_memory_region *mem,
  367. struct kvm_memory_slot old,
  368. int user_alloc)
  369. {
  370. kvmppc_core_commit_memory_region(kvm, mem, old);
  371. }
  372. void kvm_arch_flush_shadow_all(struct kvm *kvm)
  373. {
  374. }
  375. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  376. struct kvm_memory_slot *slot)
  377. {
  378. kvmppc_core_flush_memslot(kvm, slot);
  379. }
  380. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  381. {
  382. struct kvm_vcpu *vcpu;
  383. vcpu = kvmppc_core_vcpu_create(kvm, id);
  384. if (!IS_ERR(vcpu)) {
  385. vcpu->arch.wqp = &vcpu->wq;
  386. kvmppc_create_vcpu_debugfs(vcpu, id);
  387. }
  388. return vcpu;
  389. }
  390. int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  391. {
  392. return 0;
  393. }
  394. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  395. {
  396. /* Make sure we're not using the vcpu anymore */
  397. hrtimer_cancel(&vcpu->arch.dec_timer);
  398. tasklet_kill(&vcpu->arch.tasklet);
  399. kvmppc_remove_vcpu_debugfs(vcpu);
  400. kvmppc_core_vcpu_free(vcpu);
  401. }
  402. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  403. {
  404. kvm_arch_vcpu_free(vcpu);
  405. }
  406. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  407. {
  408. return kvmppc_core_pending_dec(vcpu);
  409. }
  410. /*
  411. * low level hrtimer wake routine. Because this runs in hardirq context
  412. * we schedule a tasklet to do the real work.
  413. */
  414. enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
  415. {
  416. struct kvm_vcpu *vcpu;
  417. vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
  418. tasklet_schedule(&vcpu->arch.tasklet);
  419. return HRTIMER_NORESTART;
  420. }
  421. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  422. {
  423. int ret;
  424. hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  425. tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu);
  426. vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
  427. vcpu->arch.dec_expires = ~(u64)0;
  428. #ifdef CONFIG_KVM_EXIT_TIMING
  429. mutex_init(&vcpu->arch.exit_timing_lock);
  430. #endif
  431. ret = kvmppc_subarch_vcpu_init(vcpu);
  432. return ret;
  433. }
  434. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  435. {
  436. kvmppc_mmu_destroy(vcpu);
  437. kvmppc_subarch_vcpu_uninit(vcpu);
  438. }
  439. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  440. {
  441. #ifdef CONFIG_BOOKE
  442. /*
  443. * vrsave (formerly usprg0) isn't used by Linux, but may
  444. * be used by the guest.
  445. *
  446. * On non-booke this is associated with Altivec and
  447. * is handled by code in book3s.c.
  448. */
  449. mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
  450. #endif
  451. kvmppc_core_vcpu_load(vcpu, cpu);
  452. }
  453. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  454. {
  455. kvmppc_core_vcpu_put(vcpu);
  456. #ifdef CONFIG_BOOKE
  457. vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
  458. #endif
  459. }
  460. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  461. struct kvm_guest_debug *dbg)
  462. {
  463. return -EINVAL;
  464. }
  465. static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
  466. struct kvm_run *run)
  467. {
  468. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, run->dcr.data);
  469. }
  470. static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
  471. struct kvm_run *run)
  472. {
  473. u64 uninitialized_var(gpr);
  474. if (run->mmio.len > sizeof(gpr)) {
  475. printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
  476. return;
  477. }
  478. if (vcpu->arch.mmio_is_bigendian) {
  479. switch (run->mmio.len) {
  480. case 8: gpr = *(u64 *)run->mmio.data; break;
  481. case 4: gpr = *(u32 *)run->mmio.data; break;
  482. case 2: gpr = *(u16 *)run->mmio.data; break;
  483. case 1: gpr = *(u8 *)run->mmio.data; break;
  484. }
  485. } else {
  486. /* Convert BE data from userland back to LE. */
  487. switch (run->mmio.len) {
  488. case 4: gpr = ld_le32((u32 *)run->mmio.data); break;
  489. case 2: gpr = ld_le16((u16 *)run->mmio.data); break;
  490. case 1: gpr = *(u8 *)run->mmio.data; break;
  491. }
  492. }
  493. if (vcpu->arch.mmio_sign_extend) {
  494. switch (run->mmio.len) {
  495. #ifdef CONFIG_PPC64
  496. case 4:
  497. gpr = (s64)(s32)gpr;
  498. break;
  499. #endif
  500. case 2:
  501. gpr = (s64)(s16)gpr;
  502. break;
  503. case 1:
  504. gpr = (s64)(s8)gpr;
  505. break;
  506. }
  507. }
  508. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  509. switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
  510. case KVM_MMIO_REG_GPR:
  511. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  512. break;
  513. case KVM_MMIO_REG_FPR:
  514. vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  515. break;
  516. #ifdef CONFIG_PPC_BOOK3S
  517. case KVM_MMIO_REG_QPR:
  518. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  519. break;
  520. case KVM_MMIO_REG_FQPR:
  521. vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  522. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  523. break;
  524. #endif
  525. default:
  526. BUG();
  527. }
  528. }
  529. int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  530. unsigned int rt, unsigned int bytes, int is_bigendian)
  531. {
  532. if (bytes > sizeof(run->mmio.data)) {
  533. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  534. run->mmio.len);
  535. }
  536. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  537. run->mmio.len = bytes;
  538. run->mmio.is_write = 0;
  539. vcpu->arch.io_gpr = rt;
  540. vcpu->arch.mmio_is_bigendian = is_bigendian;
  541. vcpu->mmio_needed = 1;
  542. vcpu->mmio_is_write = 0;
  543. vcpu->arch.mmio_sign_extend = 0;
  544. if (!kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, run->mmio.phys_addr,
  545. bytes, &run->mmio.data)) {
  546. kvmppc_complete_mmio_load(vcpu, run);
  547. vcpu->mmio_needed = 0;
  548. return EMULATE_DONE;
  549. }
  550. return EMULATE_DO_MMIO;
  551. }
  552. /* Same as above, but sign extends */
  553. int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
  554. unsigned int rt, unsigned int bytes, int is_bigendian)
  555. {
  556. int r;
  557. vcpu->arch.mmio_sign_extend = 1;
  558. r = kvmppc_handle_load(run, vcpu, rt, bytes, is_bigendian);
  559. return r;
  560. }
  561. int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
  562. u64 val, unsigned int bytes, int is_bigendian)
  563. {
  564. void *data = run->mmio.data;
  565. if (bytes > sizeof(run->mmio.data)) {
  566. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  567. run->mmio.len);
  568. }
  569. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  570. run->mmio.len = bytes;
  571. run->mmio.is_write = 1;
  572. vcpu->mmio_needed = 1;
  573. vcpu->mmio_is_write = 1;
  574. /* Store the value at the lowest bytes in 'data'. */
  575. if (is_bigendian) {
  576. switch (bytes) {
  577. case 8: *(u64 *)data = val; break;
  578. case 4: *(u32 *)data = val; break;
  579. case 2: *(u16 *)data = val; break;
  580. case 1: *(u8 *)data = val; break;
  581. }
  582. } else {
  583. /* Store LE value into 'data'. */
  584. switch (bytes) {
  585. case 4: st_le32(data, val); break;
  586. case 2: st_le16(data, val); break;
  587. case 1: *(u8 *)data = val; break;
  588. }
  589. }
  590. if (!kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, run->mmio.phys_addr,
  591. bytes, &run->mmio.data)) {
  592. kvmppc_complete_mmio_load(vcpu, run);
  593. vcpu->mmio_needed = 0;
  594. return EMULATE_DONE;
  595. }
  596. return EMULATE_DO_MMIO;
  597. }
  598. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  599. {
  600. int r;
  601. sigset_t sigsaved;
  602. if (vcpu->sigset_active)
  603. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  604. if (vcpu->mmio_needed) {
  605. if (!vcpu->mmio_is_write)
  606. kvmppc_complete_mmio_load(vcpu, run);
  607. vcpu->mmio_needed = 0;
  608. } else if (vcpu->arch.dcr_needed) {
  609. if (!vcpu->arch.dcr_is_write)
  610. kvmppc_complete_dcr_load(vcpu, run);
  611. vcpu->arch.dcr_needed = 0;
  612. } else if (vcpu->arch.osi_needed) {
  613. u64 *gprs = run->osi.gprs;
  614. int i;
  615. for (i = 0; i < 32; i++)
  616. kvmppc_set_gpr(vcpu, i, gprs[i]);
  617. vcpu->arch.osi_needed = 0;
  618. } else if (vcpu->arch.hcall_needed) {
  619. int i;
  620. kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
  621. for (i = 0; i < 9; ++i)
  622. kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
  623. vcpu->arch.hcall_needed = 0;
  624. }
  625. r = kvmppc_vcpu_run(run, vcpu);
  626. if (vcpu->sigset_active)
  627. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  628. return r;
  629. }
  630. int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
  631. {
  632. if (irq->irq == KVM_INTERRUPT_UNSET) {
  633. kvmppc_core_dequeue_external(vcpu, irq);
  634. return 0;
  635. }
  636. kvmppc_core_queue_external(vcpu, irq);
  637. kvm_vcpu_kick(vcpu);
  638. return 0;
  639. }
  640. static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
  641. struct kvm_enable_cap *cap)
  642. {
  643. int r;
  644. if (cap->flags)
  645. return -EINVAL;
  646. switch (cap->cap) {
  647. case KVM_CAP_PPC_OSI:
  648. r = 0;
  649. vcpu->arch.osi_enabled = true;
  650. break;
  651. case KVM_CAP_PPC_PAPR:
  652. r = 0;
  653. vcpu->arch.papr_enabled = true;
  654. break;
  655. #ifdef CONFIG_BOOKE
  656. case KVM_CAP_PPC_BOOKE_WATCHDOG:
  657. r = 0;
  658. vcpu->arch.watchdog_enabled = true;
  659. break;
  660. #endif
  661. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  662. case KVM_CAP_SW_TLB: {
  663. struct kvm_config_tlb cfg;
  664. void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
  665. r = -EFAULT;
  666. if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
  667. break;
  668. r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
  669. break;
  670. }
  671. #endif
  672. default:
  673. r = -EINVAL;
  674. break;
  675. }
  676. if (!r)
  677. r = kvmppc_sanity_check(vcpu);
  678. return r;
  679. }
  680. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  681. struct kvm_mp_state *mp_state)
  682. {
  683. return -EINVAL;
  684. }
  685. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  686. struct kvm_mp_state *mp_state)
  687. {
  688. return -EINVAL;
  689. }
  690. long kvm_arch_vcpu_ioctl(struct file *filp,
  691. unsigned int ioctl, unsigned long arg)
  692. {
  693. struct kvm_vcpu *vcpu = filp->private_data;
  694. void __user *argp = (void __user *)arg;
  695. long r;
  696. switch (ioctl) {
  697. case KVM_INTERRUPT: {
  698. struct kvm_interrupt irq;
  699. r = -EFAULT;
  700. if (copy_from_user(&irq, argp, sizeof(irq)))
  701. goto out;
  702. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  703. goto out;
  704. }
  705. case KVM_ENABLE_CAP:
  706. {
  707. struct kvm_enable_cap cap;
  708. r = -EFAULT;
  709. if (copy_from_user(&cap, argp, sizeof(cap)))
  710. goto out;
  711. r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
  712. break;
  713. }
  714. case KVM_SET_ONE_REG:
  715. case KVM_GET_ONE_REG:
  716. {
  717. struct kvm_one_reg reg;
  718. r = -EFAULT;
  719. if (copy_from_user(&reg, argp, sizeof(reg)))
  720. goto out;
  721. if (ioctl == KVM_SET_ONE_REG)
  722. r = kvm_vcpu_ioctl_set_one_reg(vcpu, &reg);
  723. else
  724. r = kvm_vcpu_ioctl_get_one_reg(vcpu, &reg);
  725. break;
  726. }
  727. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  728. case KVM_DIRTY_TLB: {
  729. struct kvm_dirty_tlb dirty;
  730. r = -EFAULT;
  731. if (copy_from_user(&dirty, argp, sizeof(dirty)))
  732. goto out;
  733. r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
  734. break;
  735. }
  736. #endif
  737. default:
  738. r = -EINVAL;
  739. }
  740. out:
  741. return r;
  742. }
  743. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  744. {
  745. return VM_FAULT_SIGBUS;
  746. }
  747. static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
  748. {
  749. u32 inst_nop = 0x60000000;
  750. #ifdef CONFIG_KVM_BOOKE_HV
  751. u32 inst_sc1 = 0x44000022;
  752. pvinfo->hcall[0] = inst_sc1;
  753. pvinfo->hcall[1] = inst_nop;
  754. pvinfo->hcall[2] = inst_nop;
  755. pvinfo->hcall[3] = inst_nop;
  756. #else
  757. u32 inst_lis = 0x3c000000;
  758. u32 inst_ori = 0x60000000;
  759. u32 inst_sc = 0x44000002;
  760. u32 inst_imm_mask = 0xffff;
  761. /*
  762. * The hypercall to get into KVM from within guest context is as
  763. * follows:
  764. *
  765. * lis r0, r0, KVM_SC_MAGIC_R0@h
  766. * ori r0, KVM_SC_MAGIC_R0@l
  767. * sc
  768. * nop
  769. */
  770. pvinfo->hcall[0] = inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask);
  771. pvinfo->hcall[1] = inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask);
  772. pvinfo->hcall[2] = inst_sc;
  773. pvinfo->hcall[3] = inst_nop;
  774. #endif
  775. pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
  776. return 0;
  777. }
  778. long kvm_arch_vm_ioctl(struct file *filp,
  779. unsigned int ioctl, unsigned long arg)
  780. {
  781. void __user *argp = (void __user *)arg;
  782. long r;
  783. switch (ioctl) {
  784. case KVM_PPC_GET_PVINFO: {
  785. struct kvm_ppc_pvinfo pvinfo;
  786. memset(&pvinfo, 0, sizeof(pvinfo));
  787. r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
  788. if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
  789. r = -EFAULT;
  790. goto out;
  791. }
  792. break;
  793. }
  794. #ifdef CONFIG_PPC_BOOK3S_64
  795. case KVM_CREATE_SPAPR_TCE: {
  796. struct kvm_create_spapr_tce create_tce;
  797. struct kvm *kvm = filp->private_data;
  798. r = -EFAULT;
  799. if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
  800. goto out;
  801. r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce);
  802. goto out;
  803. }
  804. #endif /* CONFIG_PPC_BOOK3S_64 */
  805. #ifdef CONFIG_KVM_BOOK3S_64_HV
  806. case KVM_ALLOCATE_RMA: {
  807. struct kvm *kvm = filp->private_data;
  808. struct kvm_allocate_rma rma;
  809. r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
  810. if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
  811. r = -EFAULT;
  812. break;
  813. }
  814. case KVM_PPC_ALLOCATE_HTAB: {
  815. struct kvm *kvm = filp->private_data;
  816. u32 htab_order;
  817. r = -EFAULT;
  818. if (get_user(htab_order, (u32 __user *)argp))
  819. break;
  820. r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
  821. if (r)
  822. break;
  823. r = -EFAULT;
  824. if (put_user(htab_order, (u32 __user *)argp))
  825. break;
  826. r = 0;
  827. break;
  828. }
  829. case KVM_PPC_GET_HTAB_FD: {
  830. struct kvm *kvm = filp->private_data;
  831. struct kvm_get_htab_fd ghf;
  832. r = -EFAULT;
  833. if (copy_from_user(&ghf, argp, sizeof(ghf)))
  834. break;
  835. r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
  836. break;
  837. }
  838. #endif /* CONFIG_KVM_BOOK3S_64_HV */
  839. #ifdef CONFIG_PPC_BOOK3S_64
  840. case KVM_PPC_GET_SMMU_INFO: {
  841. struct kvm *kvm = filp->private_data;
  842. struct kvm_ppc_smmu_info info;
  843. memset(&info, 0, sizeof(info));
  844. r = kvm_vm_ioctl_get_smmu_info(kvm, &info);
  845. if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
  846. r = -EFAULT;
  847. break;
  848. }
  849. #endif /* CONFIG_PPC_BOOK3S_64 */
  850. default:
  851. r = -ENOTTY;
  852. }
  853. out:
  854. return r;
  855. }
  856. static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
  857. static unsigned long nr_lpids;
  858. long kvmppc_alloc_lpid(void)
  859. {
  860. long lpid;
  861. do {
  862. lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
  863. if (lpid >= nr_lpids) {
  864. pr_err("%s: No LPIDs free\n", __func__);
  865. return -ENOMEM;
  866. }
  867. } while (test_and_set_bit(lpid, lpid_inuse));
  868. return lpid;
  869. }
  870. void kvmppc_claim_lpid(long lpid)
  871. {
  872. set_bit(lpid, lpid_inuse);
  873. }
  874. void kvmppc_free_lpid(long lpid)
  875. {
  876. clear_bit(lpid, lpid_inuse);
  877. }
  878. void kvmppc_init_lpid(unsigned long nr_lpids_param)
  879. {
  880. nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
  881. memset(lpid_inuse, 0, sizeof(lpid_inuse));
  882. }
  883. int kvm_arch_init(void *opaque)
  884. {
  885. return 0;
  886. }
  887. void kvm_arch_exit(void)
  888. {
  889. }