powerpc.c 24 KB

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