powerpc.c 27 KB

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