powerpc.c 18 KB

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