powerpc.c 24 KB

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