powerpc.c 20 KB

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