powerpc.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  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. }
  413. kvmppc_core_vcpu_free(vcpu);
  414. }
  415. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  416. {
  417. kvm_arch_vcpu_free(vcpu);
  418. }
  419. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  420. {
  421. return kvmppc_core_pending_dec(vcpu);
  422. }
  423. /*
  424. * low level hrtimer wake routine. Because this runs in hardirq context
  425. * we schedule a tasklet to do the real work.
  426. */
  427. enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
  428. {
  429. struct kvm_vcpu *vcpu;
  430. vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
  431. tasklet_schedule(&vcpu->arch.tasklet);
  432. return HRTIMER_NORESTART;
  433. }
  434. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  435. {
  436. int ret;
  437. hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  438. tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu);
  439. vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
  440. vcpu->arch.dec_expires = ~(u64)0;
  441. #ifdef CONFIG_KVM_EXIT_TIMING
  442. mutex_init(&vcpu->arch.exit_timing_lock);
  443. #endif
  444. ret = kvmppc_subarch_vcpu_init(vcpu);
  445. return ret;
  446. }
  447. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  448. {
  449. kvmppc_mmu_destroy(vcpu);
  450. kvmppc_subarch_vcpu_uninit(vcpu);
  451. }
  452. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  453. {
  454. #ifdef CONFIG_BOOKE
  455. /*
  456. * vrsave (formerly usprg0) isn't used by Linux, but may
  457. * be used by the guest.
  458. *
  459. * On non-booke this is associated with Altivec and
  460. * is handled by code in book3s.c.
  461. */
  462. mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
  463. #endif
  464. kvmppc_core_vcpu_load(vcpu, cpu);
  465. }
  466. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  467. {
  468. kvmppc_core_vcpu_put(vcpu);
  469. #ifdef CONFIG_BOOKE
  470. vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
  471. #endif
  472. }
  473. static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
  474. struct kvm_run *run)
  475. {
  476. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, run->dcr.data);
  477. }
  478. static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
  479. struct kvm_run *run)
  480. {
  481. u64 uninitialized_var(gpr);
  482. if (run->mmio.len > sizeof(gpr)) {
  483. printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
  484. return;
  485. }
  486. if (vcpu->arch.mmio_is_bigendian) {
  487. switch (run->mmio.len) {
  488. case 8: gpr = *(u64 *)run->mmio.data; break;
  489. case 4: gpr = *(u32 *)run->mmio.data; break;
  490. case 2: gpr = *(u16 *)run->mmio.data; break;
  491. case 1: gpr = *(u8 *)run->mmio.data; break;
  492. }
  493. } else {
  494. /* Convert BE data from userland back to LE. */
  495. switch (run->mmio.len) {
  496. case 4: gpr = ld_le32((u32 *)run->mmio.data); break;
  497. case 2: gpr = ld_le16((u16 *)run->mmio.data); break;
  498. case 1: gpr = *(u8 *)run->mmio.data; break;
  499. }
  500. }
  501. if (vcpu->arch.mmio_sign_extend) {
  502. switch (run->mmio.len) {
  503. #ifdef CONFIG_PPC64
  504. case 4:
  505. gpr = (s64)(s32)gpr;
  506. break;
  507. #endif
  508. case 2:
  509. gpr = (s64)(s16)gpr;
  510. break;
  511. case 1:
  512. gpr = (s64)(s8)gpr;
  513. break;
  514. }
  515. }
  516. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  517. switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
  518. case KVM_MMIO_REG_GPR:
  519. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  520. break;
  521. case KVM_MMIO_REG_FPR:
  522. vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  523. break;
  524. #ifdef CONFIG_PPC_BOOK3S
  525. case KVM_MMIO_REG_QPR:
  526. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  527. break;
  528. case KVM_MMIO_REG_FQPR:
  529. vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  530. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  531. break;
  532. #endif
  533. default:
  534. BUG();
  535. }
  536. }
  537. int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  538. unsigned int rt, unsigned int bytes, int is_bigendian)
  539. {
  540. if (bytes > sizeof(run->mmio.data)) {
  541. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  542. run->mmio.len);
  543. }
  544. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  545. run->mmio.len = bytes;
  546. run->mmio.is_write = 0;
  547. vcpu->arch.io_gpr = rt;
  548. vcpu->arch.mmio_is_bigendian = is_bigendian;
  549. vcpu->mmio_needed = 1;
  550. vcpu->mmio_is_write = 0;
  551. vcpu->arch.mmio_sign_extend = 0;
  552. if (!kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, run->mmio.phys_addr,
  553. bytes, &run->mmio.data)) {
  554. kvmppc_complete_mmio_load(vcpu, run);
  555. vcpu->mmio_needed = 0;
  556. return EMULATE_DONE;
  557. }
  558. return EMULATE_DO_MMIO;
  559. }
  560. /* Same as above, but sign extends */
  561. int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
  562. unsigned int rt, unsigned int bytes, int is_bigendian)
  563. {
  564. int r;
  565. vcpu->arch.mmio_sign_extend = 1;
  566. r = kvmppc_handle_load(run, vcpu, rt, bytes, is_bigendian);
  567. return r;
  568. }
  569. int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
  570. u64 val, unsigned int bytes, int is_bigendian)
  571. {
  572. void *data = run->mmio.data;
  573. if (bytes > sizeof(run->mmio.data)) {
  574. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  575. run->mmio.len);
  576. }
  577. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  578. run->mmio.len = bytes;
  579. run->mmio.is_write = 1;
  580. vcpu->mmio_needed = 1;
  581. vcpu->mmio_is_write = 1;
  582. /* Store the value at the lowest bytes in 'data'. */
  583. if (is_bigendian) {
  584. switch (bytes) {
  585. case 8: *(u64 *)data = val; break;
  586. case 4: *(u32 *)data = val; break;
  587. case 2: *(u16 *)data = val; break;
  588. case 1: *(u8 *)data = val; break;
  589. }
  590. } else {
  591. /* Store LE value into 'data'. */
  592. switch (bytes) {
  593. case 4: st_le32(data, val); break;
  594. case 2: st_le16(data, val); break;
  595. case 1: *(u8 *)data = val; break;
  596. }
  597. }
  598. if (!kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, run->mmio.phys_addr,
  599. bytes, &run->mmio.data)) {
  600. vcpu->mmio_needed = 0;
  601. return EMULATE_DONE;
  602. }
  603. return EMULATE_DO_MMIO;
  604. }
  605. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  606. {
  607. int r;
  608. sigset_t sigsaved;
  609. if (vcpu->sigset_active)
  610. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  611. if (vcpu->mmio_needed) {
  612. if (!vcpu->mmio_is_write)
  613. kvmppc_complete_mmio_load(vcpu, run);
  614. vcpu->mmio_needed = 0;
  615. } else if (vcpu->arch.dcr_needed) {
  616. if (!vcpu->arch.dcr_is_write)
  617. kvmppc_complete_dcr_load(vcpu, run);
  618. vcpu->arch.dcr_needed = 0;
  619. } else if (vcpu->arch.osi_needed) {
  620. u64 *gprs = run->osi.gprs;
  621. int i;
  622. for (i = 0; i < 32; i++)
  623. kvmppc_set_gpr(vcpu, i, gprs[i]);
  624. vcpu->arch.osi_needed = 0;
  625. } else if (vcpu->arch.hcall_needed) {
  626. int i;
  627. kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
  628. for (i = 0; i < 9; ++i)
  629. kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
  630. vcpu->arch.hcall_needed = 0;
  631. #ifdef CONFIG_BOOKE
  632. } else if (vcpu->arch.epr_needed) {
  633. kvmppc_set_epr(vcpu, run->epr.epr);
  634. vcpu->arch.epr_needed = 0;
  635. #endif
  636. }
  637. r = kvmppc_vcpu_run(run, vcpu);
  638. if (vcpu->sigset_active)
  639. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  640. return r;
  641. }
  642. int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
  643. {
  644. if (irq->irq == KVM_INTERRUPT_UNSET) {
  645. kvmppc_core_dequeue_external(vcpu);
  646. return 0;
  647. }
  648. kvmppc_core_queue_external(vcpu, irq);
  649. kvm_vcpu_kick(vcpu);
  650. return 0;
  651. }
  652. static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
  653. struct kvm_enable_cap *cap)
  654. {
  655. int r;
  656. if (cap->flags)
  657. return -EINVAL;
  658. switch (cap->cap) {
  659. case KVM_CAP_PPC_OSI:
  660. r = 0;
  661. vcpu->arch.osi_enabled = true;
  662. break;
  663. case KVM_CAP_PPC_PAPR:
  664. r = 0;
  665. vcpu->arch.papr_enabled = true;
  666. break;
  667. case KVM_CAP_PPC_EPR:
  668. r = 0;
  669. if (cap->args[0])
  670. vcpu->arch.epr_flags |= KVMPPC_EPR_USER;
  671. else
  672. vcpu->arch.epr_flags &= ~KVMPPC_EPR_USER;
  673. break;
  674. #ifdef CONFIG_BOOKE
  675. case KVM_CAP_PPC_BOOKE_WATCHDOG:
  676. r = 0;
  677. vcpu->arch.watchdog_enabled = true;
  678. break;
  679. #endif
  680. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  681. case KVM_CAP_SW_TLB: {
  682. struct kvm_config_tlb cfg;
  683. void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
  684. r = -EFAULT;
  685. if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
  686. break;
  687. r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
  688. break;
  689. }
  690. #endif
  691. #ifdef CONFIG_KVM_MPIC
  692. case KVM_CAP_IRQ_MPIC: {
  693. struct file *filp;
  694. struct kvm_device *dev;
  695. r = -EBADF;
  696. filp = fget(cap->args[0]);
  697. if (!filp)
  698. break;
  699. r = -EPERM;
  700. dev = kvm_device_from_filp(filp);
  701. if (dev)
  702. r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]);
  703. fput(filp);
  704. break;
  705. }
  706. #endif
  707. default:
  708. r = -EINVAL;
  709. break;
  710. }
  711. if (!r)
  712. r = kvmppc_sanity_check(vcpu);
  713. return r;
  714. }
  715. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  716. struct kvm_mp_state *mp_state)
  717. {
  718. return -EINVAL;
  719. }
  720. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  721. struct kvm_mp_state *mp_state)
  722. {
  723. return -EINVAL;
  724. }
  725. long kvm_arch_vcpu_ioctl(struct file *filp,
  726. unsigned int ioctl, unsigned long arg)
  727. {
  728. struct kvm_vcpu *vcpu = filp->private_data;
  729. void __user *argp = (void __user *)arg;
  730. long r;
  731. switch (ioctl) {
  732. case KVM_INTERRUPT: {
  733. struct kvm_interrupt irq;
  734. r = -EFAULT;
  735. if (copy_from_user(&irq, argp, sizeof(irq)))
  736. goto out;
  737. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  738. goto out;
  739. }
  740. case KVM_ENABLE_CAP:
  741. {
  742. struct kvm_enable_cap cap;
  743. r = -EFAULT;
  744. if (copy_from_user(&cap, argp, sizeof(cap)))
  745. goto out;
  746. r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
  747. break;
  748. }
  749. case KVM_SET_ONE_REG:
  750. case KVM_GET_ONE_REG:
  751. {
  752. struct kvm_one_reg reg;
  753. r = -EFAULT;
  754. if (copy_from_user(&reg, argp, sizeof(reg)))
  755. goto out;
  756. if (ioctl == KVM_SET_ONE_REG)
  757. r = kvm_vcpu_ioctl_set_one_reg(vcpu, &reg);
  758. else
  759. r = kvm_vcpu_ioctl_get_one_reg(vcpu, &reg);
  760. break;
  761. }
  762. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  763. case KVM_DIRTY_TLB: {
  764. struct kvm_dirty_tlb dirty;
  765. r = -EFAULT;
  766. if (copy_from_user(&dirty, argp, sizeof(dirty)))
  767. goto out;
  768. r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
  769. break;
  770. }
  771. #endif
  772. default:
  773. r = -EINVAL;
  774. }
  775. out:
  776. return r;
  777. }
  778. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  779. {
  780. return VM_FAULT_SIGBUS;
  781. }
  782. static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
  783. {
  784. u32 inst_nop = 0x60000000;
  785. #ifdef CONFIG_KVM_BOOKE_HV
  786. u32 inst_sc1 = 0x44000022;
  787. pvinfo->hcall[0] = inst_sc1;
  788. pvinfo->hcall[1] = inst_nop;
  789. pvinfo->hcall[2] = inst_nop;
  790. pvinfo->hcall[3] = inst_nop;
  791. #else
  792. u32 inst_lis = 0x3c000000;
  793. u32 inst_ori = 0x60000000;
  794. u32 inst_sc = 0x44000002;
  795. u32 inst_imm_mask = 0xffff;
  796. /*
  797. * The hypercall to get into KVM from within guest context is as
  798. * follows:
  799. *
  800. * lis r0, r0, KVM_SC_MAGIC_R0@h
  801. * ori r0, KVM_SC_MAGIC_R0@l
  802. * sc
  803. * nop
  804. */
  805. pvinfo->hcall[0] = inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask);
  806. pvinfo->hcall[1] = inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask);
  807. pvinfo->hcall[2] = inst_sc;
  808. pvinfo->hcall[3] = inst_nop;
  809. #endif
  810. pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
  811. return 0;
  812. }
  813. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
  814. bool line_status)
  815. {
  816. if (!irqchip_in_kernel(kvm))
  817. return -ENXIO;
  818. irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
  819. irq_event->irq, irq_event->level,
  820. line_status);
  821. return 0;
  822. }
  823. long kvm_arch_vm_ioctl(struct file *filp,
  824. unsigned int ioctl, unsigned long arg)
  825. {
  826. struct kvm *kvm __maybe_unused = filp->private_data;
  827. void __user *argp = (void __user *)arg;
  828. long r;
  829. switch (ioctl) {
  830. case KVM_PPC_GET_PVINFO: {
  831. struct kvm_ppc_pvinfo pvinfo;
  832. memset(&pvinfo, 0, sizeof(pvinfo));
  833. r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
  834. if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
  835. r = -EFAULT;
  836. goto out;
  837. }
  838. break;
  839. }
  840. #ifdef CONFIG_PPC_BOOK3S_64
  841. case KVM_CREATE_SPAPR_TCE: {
  842. struct kvm_create_spapr_tce create_tce;
  843. r = -EFAULT;
  844. if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
  845. goto out;
  846. r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce);
  847. goto out;
  848. }
  849. #endif /* CONFIG_PPC_BOOK3S_64 */
  850. #ifdef CONFIG_KVM_BOOK3S_64_HV
  851. case KVM_ALLOCATE_RMA: {
  852. struct kvm_allocate_rma rma;
  853. struct kvm *kvm = filp->private_data;
  854. r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
  855. if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
  856. r = -EFAULT;
  857. break;
  858. }
  859. case KVM_PPC_ALLOCATE_HTAB: {
  860. u32 htab_order;
  861. r = -EFAULT;
  862. if (get_user(htab_order, (u32 __user *)argp))
  863. break;
  864. r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
  865. if (r)
  866. break;
  867. r = -EFAULT;
  868. if (put_user(htab_order, (u32 __user *)argp))
  869. break;
  870. r = 0;
  871. break;
  872. }
  873. case KVM_PPC_GET_HTAB_FD: {
  874. struct kvm_get_htab_fd ghf;
  875. r = -EFAULT;
  876. if (copy_from_user(&ghf, argp, sizeof(ghf)))
  877. break;
  878. r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
  879. break;
  880. }
  881. #endif /* CONFIG_KVM_BOOK3S_64_HV */
  882. #ifdef CONFIG_PPC_BOOK3S_64
  883. case KVM_PPC_GET_SMMU_INFO: {
  884. struct kvm_ppc_smmu_info info;
  885. memset(&info, 0, sizeof(info));
  886. r = kvm_vm_ioctl_get_smmu_info(kvm, &info);
  887. if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
  888. r = -EFAULT;
  889. break;
  890. }
  891. case KVM_PPC_RTAS_DEFINE_TOKEN: {
  892. struct kvm *kvm = filp->private_data;
  893. r = kvm_vm_ioctl_rtas_define_token(kvm, argp);
  894. break;
  895. }
  896. #endif /* CONFIG_PPC_BOOK3S_64 */
  897. default:
  898. r = -ENOTTY;
  899. }
  900. out:
  901. return r;
  902. }
  903. static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
  904. static unsigned long nr_lpids;
  905. long kvmppc_alloc_lpid(void)
  906. {
  907. long lpid;
  908. do {
  909. lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
  910. if (lpid >= nr_lpids) {
  911. pr_err("%s: No LPIDs free\n", __func__);
  912. return -ENOMEM;
  913. }
  914. } while (test_and_set_bit(lpid, lpid_inuse));
  915. return lpid;
  916. }
  917. void kvmppc_claim_lpid(long lpid)
  918. {
  919. set_bit(lpid, lpid_inuse);
  920. }
  921. void kvmppc_free_lpid(long lpid)
  922. {
  923. clear_bit(lpid, lpid_inuse);
  924. }
  925. void kvmppc_init_lpid(unsigned long nr_lpids_param)
  926. {
  927. nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
  928. memset(lpid_inuse, 0, sizeof(lpid_inuse));
  929. }
  930. int kvm_arch_init(void *opaque)
  931. {
  932. return 0;
  933. }
  934. void kvm_arch_exit(void)
  935. {
  936. }