powerpc.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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. if (bytes > sizeof(run->mmio.data)) {
  544. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  545. run->mmio.len);
  546. }
  547. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  548. run->mmio.len = bytes;
  549. run->mmio.is_write = 0;
  550. vcpu->arch.io_gpr = rt;
  551. vcpu->arch.mmio_is_bigendian = is_bigendian;
  552. vcpu->mmio_needed = 1;
  553. vcpu->mmio_is_write = 0;
  554. vcpu->arch.mmio_sign_extend = 0;
  555. if (!kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, run->mmio.phys_addr,
  556. bytes, &run->mmio.data)) {
  557. kvmppc_complete_mmio_load(vcpu, run);
  558. vcpu->mmio_needed = 0;
  559. return EMULATE_DONE;
  560. }
  561. return EMULATE_DO_MMIO;
  562. }
  563. /* Same as above, but sign extends */
  564. int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
  565. unsigned int rt, unsigned int bytes, int is_bigendian)
  566. {
  567. int r;
  568. vcpu->arch.mmio_sign_extend = 1;
  569. r = kvmppc_handle_load(run, vcpu, rt, bytes, is_bigendian);
  570. return r;
  571. }
  572. int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
  573. u64 val, unsigned int bytes, int is_bigendian)
  574. {
  575. void *data = run->mmio.data;
  576. if (bytes > sizeof(run->mmio.data)) {
  577. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  578. run->mmio.len);
  579. }
  580. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  581. run->mmio.len = bytes;
  582. run->mmio.is_write = 1;
  583. vcpu->mmio_needed = 1;
  584. vcpu->mmio_is_write = 1;
  585. /* Store the value at the lowest bytes in 'data'. */
  586. if (is_bigendian) {
  587. switch (bytes) {
  588. case 8: *(u64 *)data = val; break;
  589. case 4: *(u32 *)data = val; break;
  590. case 2: *(u16 *)data = val; break;
  591. case 1: *(u8 *)data = val; break;
  592. }
  593. } else {
  594. /* Store LE value into 'data'. */
  595. switch (bytes) {
  596. case 4: st_le32(data, val); break;
  597. case 2: st_le16(data, val); break;
  598. case 1: *(u8 *)data = val; break;
  599. }
  600. }
  601. if (!kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, run->mmio.phys_addr,
  602. bytes, &run->mmio.data)) {
  603. vcpu->mmio_needed = 0;
  604. return EMULATE_DONE;
  605. }
  606. return EMULATE_DO_MMIO;
  607. }
  608. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  609. {
  610. int r;
  611. sigset_t sigsaved;
  612. if (vcpu->sigset_active)
  613. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  614. if (vcpu->mmio_needed) {
  615. if (!vcpu->mmio_is_write)
  616. kvmppc_complete_mmio_load(vcpu, run);
  617. vcpu->mmio_needed = 0;
  618. } else if (vcpu->arch.dcr_needed) {
  619. if (!vcpu->arch.dcr_is_write)
  620. kvmppc_complete_dcr_load(vcpu, run);
  621. vcpu->arch.dcr_needed = 0;
  622. } else if (vcpu->arch.osi_needed) {
  623. u64 *gprs = run->osi.gprs;
  624. int i;
  625. for (i = 0; i < 32; i++)
  626. kvmppc_set_gpr(vcpu, i, gprs[i]);
  627. vcpu->arch.osi_needed = 0;
  628. } else if (vcpu->arch.hcall_needed) {
  629. int i;
  630. kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
  631. for (i = 0; i < 9; ++i)
  632. kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
  633. vcpu->arch.hcall_needed = 0;
  634. #ifdef CONFIG_BOOKE
  635. } else if (vcpu->arch.epr_needed) {
  636. kvmppc_set_epr(vcpu, run->epr.epr);
  637. vcpu->arch.epr_needed = 0;
  638. #endif
  639. }
  640. r = kvmppc_vcpu_run(run, vcpu);
  641. if (vcpu->sigset_active)
  642. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  643. return r;
  644. }
  645. int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
  646. {
  647. if (irq->irq == KVM_INTERRUPT_UNSET) {
  648. kvmppc_core_dequeue_external(vcpu);
  649. return 0;
  650. }
  651. kvmppc_core_queue_external(vcpu, irq);
  652. kvm_vcpu_kick(vcpu);
  653. return 0;
  654. }
  655. static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
  656. struct kvm_enable_cap *cap)
  657. {
  658. int r;
  659. if (cap->flags)
  660. return -EINVAL;
  661. switch (cap->cap) {
  662. case KVM_CAP_PPC_OSI:
  663. r = 0;
  664. vcpu->arch.osi_enabled = true;
  665. break;
  666. case KVM_CAP_PPC_PAPR:
  667. r = 0;
  668. vcpu->arch.papr_enabled = true;
  669. break;
  670. case KVM_CAP_PPC_EPR:
  671. r = 0;
  672. if (cap->args[0])
  673. vcpu->arch.epr_flags |= KVMPPC_EPR_USER;
  674. else
  675. vcpu->arch.epr_flags &= ~KVMPPC_EPR_USER;
  676. break;
  677. #ifdef CONFIG_BOOKE
  678. case KVM_CAP_PPC_BOOKE_WATCHDOG:
  679. r = 0;
  680. vcpu->arch.watchdog_enabled = true;
  681. break;
  682. #endif
  683. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  684. case KVM_CAP_SW_TLB: {
  685. struct kvm_config_tlb cfg;
  686. void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
  687. r = -EFAULT;
  688. if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
  689. break;
  690. r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
  691. break;
  692. }
  693. #endif
  694. #ifdef CONFIG_KVM_MPIC
  695. case KVM_CAP_IRQ_MPIC: {
  696. struct file *filp;
  697. struct kvm_device *dev;
  698. r = -EBADF;
  699. filp = fget(cap->args[0]);
  700. if (!filp)
  701. break;
  702. r = -EPERM;
  703. dev = kvm_device_from_filp(filp);
  704. if (dev)
  705. r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]);
  706. fput(filp);
  707. break;
  708. }
  709. #endif
  710. default:
  711. r = -EINVAL;
  712. break;
  713. }
  714. if (!r)
  715. r = kvmppc_sanity_check(vcpu);
  716. return r;
  717. }
  718. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  719. struct kvm_mp_state *mp_state)
  720. {
  721. return -EINVAL;
  722. }
  723. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  724. struct kvm_mp_state *mp_state)
  725. {
  726. return -EINVAL;
  727. }
  728. long kvm_arch_vcpu_ioctl(struct file *filp,
  729. unsigned int ioctl, unsigned long arg)
  730. {
  731. struct kvm_vcpu *vcpu = filp->private_data;
  732. void __user *argp = (void __user *)arg;
  733. long r;
  734. switch (ioctl) {
  735. case KVM_INTERRUPT: {
  736. struct kvm_interrupt irq;
  737. r = -EFAULT;
  738. if (copy_from_user(&irq, argp, sizeof(irq)))
  739. goto out;
  740. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  741. goto out;
  742. }
  743. case KVM_ENABLE_CAP:
  744. {
  745. struct kvm_enable_cap cap;
  746. r = -EFAULT;
  747. if (copy_from_user(&cap, argp, sizeof(cap)))
  748. goto out;
  749. r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
  750. break;
  751. }
  752. case KVM_SET_ONE_REG:
  753. case KVM_GET_ONE_REG:
  754. {
  755. struct kvm_one_reg reg;
  756. r = -EFAULT;
  757. if (copy_from_user(&reg, argp, sizeof(reg)))
  758. goto out;
  759. if (ioctl == KVM_SET_ONE_REG)
  760. r = kvm_vcpu_ioctl_set_one_reg(vcpu, &reg);
  761. else
  762. r = kvm_vcpu_ioctl_get_one_reg(vcpu, &reg);
  763. break;
  764. }
  765. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  766. case KVM_DIRTY_TLB: {
  767. struct kvm_dirty_tlb dirty;
  768. r = -EFAULT;
  769. if (copy_from_user(&dirty, argp, sizeof(dirty)))
  770. goto out;
  771. r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
  772. break;
  773. }
  774. #endif
  775. default:
  776. r = -EINVAL;
  777. }
  778. out:
  779. return r;
  780. }
  781. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  782. {
  783. return VM_FAULT_SIGBUS;
  784. }
  785. static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
  786. {
  787. u32 inst_nop = 0x60000000;
  788. #ifdef CONFIG_KVM_BOOKE_HV
  789. u32 inst_sc1 = 0x44000022;
  790. pvinfo->hcall[0] = inst_sc1;
  791. pvinfo->hcall[1] = inst_nop;
  792. pvinfo->hcall[2] = inst_nop;
  793. pvinfo->hcall[3] = inst_nop;
  794. #else
  795. u32 inst_lis = 0x3c000000;
  796. u32 inst_ori = 0x60000000;
  797. u32 inst_sc = 0x44000002;
  798. u32 inst_imm_mask = 0xffff;
  799. /*
  800. * The hypercall to get into KVM from within guest context is as
  801. * follows:
  802. *
  803. * lis r0, r0, KVM_SC_MAGIC_R0@h
  804. * ori r0, KVM_SC_MAGIC_R0@l
  805. * sc
  806. * nop
  807. */
  808. pvinfo->hcall[0] = inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask);
  809. pvinfo->hcall[1] = inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask);
  810. pvinfo->hcall[2] = inst_sc;
  811. pvinfo->hcall[3] = inst_nop;
  812. #endif
  813. pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
  814. return 0;
  815. }
  816. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
  817. bool line_status)
  818. {
  819. if (!irqchip_in_kernel(kvm))
  820. return -ENXIO;
  821. irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
  822. irq_event->irq, irq_event->level,
  823. line_status);
  824. return 0;
  825. }
  826. long kvm_arch_vm_ioctl(struct file *filp,
  827. unsigned int ioctl, unsigned long arg)
  828. {
  829. struct kvm *kvm __maybe_unused = filp->private_data;
  830. void __user *argp = (void __user *)arg;
  831. long r;
  832. switch (ioctl) {
  833. case KVM_PPC_GET_PVINFO: {
  834. struct kvm_ppc_pvinfo pvinfo;
  835. memset(&pvinfo, 0, sizeof(pvinfo));
  836. r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
  837. if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
  838. r = -EFAULT;
  839. goto out;
  840. }
  841. break;
  842. }
  843. #ifdef CONFIG_PPC_BOOK3S_64
  844. case KVM_CREATE_SPAPR_TCE: {
  845. struct kvm_create_spapr_tce create_tce;
  846. r = -EFAULT;
  847. if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
  848. goto out;
  849. r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce);
  850. goto out;
  851. }
  852. #endif /* CONFIG_PPC_BOOK3S_64 */
  853. #ifdef CONFIG_KVM_BOOK3S_64_HV
  854. case KVM_ALLOCATE_RMA: {
  855. struct kvm_allocate_rma rma;
  856. struct kvm *kvm = filp->private_data;
  857. r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
  858. if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
  859. r = -EFAULT;
  860. break;
  861. }
  862. case KVM_PPC_ALLOCATE_HTAB: {
  863. u32 htab_order;
  864. r = -EFAULT;
  865. if (get_user(htab_order, (u32 __user *)argp))
  866. break;
  867. r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
  868. if (r)
  869. break;
  870. r = -EFAULT;
  871. if (put_user(htab_order, (u32 __user *)argp))
  872. break;
  873. r = 0;
  874. break;
  875. }
  876. case KVM_PPC_GET_HTAB_FD: {
  877. struct kvm_get_htab_fd ghf;
  878. r = -EFAULT;
  879. if (copy_from_user(&ghf, argp, sizeof(ghf)))
  880. break;
  881. r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
  882. break;
  883. }
  884. #endif /* CONFIG_KVM_BOOK3S_64_HV */
  885. #ifdef CONFIG_PPC_BOOK3S_64
  886. case KVM_PPC_GET_SMMU_INFO: {
  887. struct kvm_ppc_smmu_info info;
  888. memset(&info, 0, sizeof(info));
  889. r = kvm_vm_ioctl_get_smmu_info(kvm, &info);
  890. if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
  891. r = -EFAULT;
  892. break;
  893. }
  894. case KVM_PPC_RTAS_DEFINE_TOKEN: {
  895. struct kvm *kvm = filp->private_data;
  896. r = kvm_vm_ioctl_rtas_define_token(kvm, argp);
  897. break;
  898. }
  899. #endif /* CONFIG_PPC_BOOK3S_64 */
  900. default:
  901. r = -ENOTTY;
  902. }
  903. out:
  904. return r;
  905. }
  906. static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
  907. static unsigned long nr_lpids;
  908. long kvmppc_alloc_lpid(void)
  909. {
  910. long lpid;
  911. do {
  912. lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
  913. if (lpid >= nr_lpids) {
  914. pr_err("%s: No LPIDs free\n", __func__);
  915. return -ENOMEM;
  916. }
  917. } while (test_and_set_bit(lpid, lpid_inuse));
  918. return lpid;
  919. }
  920. void kvmppc_claim_lpid(long lpid)
  921. {
  922. set_bit(lpid, lpid_inuse);
  923. }
  924. void kvmppc_free_lpid(long lpid)
  925. {
  926. clear_bit(lpid, lpid_inuse);
  927. }
  928. void kvmppc_init_lpid(unsigned long nr_lpids_param)
  929. {
  930. nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
  931. memset(lpid_inuse, 0, sizeof(lpid_inuse));
  932. }
  933. int kvm_arch_init(void *opaque)
  934. {
  935. return 0;
  936. }
  937. void kvm_arch_exit(void)
  938. {
  939. }