powerpc.c 23 KB

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