powerpc.c 24 KB

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