powerpc.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  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 <linux/irqbypass.h>
  30. #include <linux/kvm_irqfd.h>
  31. #include <asm/cputable.h>
  32. #include <asm/uaccess.h>
  33. #include <asm/kvm_ppc.h>
  34. #include <asm/tlbflush.h>
  35. #include <asm/cputhreads.h>
  36. #include <asm/irqflags.h>
  37. #include <asm/iommu.h>
  38. #include "timing.h"
  39. #include "irq.h"
  40. #include "../mm/mmu_decl.h"
  41. #define CREATE_TRACE_POINTS
  42. #include "trace.h"
  43. struct kvmppc_ops *kvmppc_hv_ops;
  44. EXPORT_SYMBOL_GPL(kvmppc_hv_ops);
  45. struct kvmppc_ops *kvmppc_pr_ops;
  46. EXPORT_SYMBOL_GPL(kvmppc_pr_ops);
  47. int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
  48. {
  49. return !!(v->arch.pending_exceptions) ||
  50. v->requests;
  51. }
  52. int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
  53. {
  54. return 1;
  55. }
  56. /*
  57. * Common checks before entering the guest world. Call with interrupts
  58. * disabled.
  59. *
  60. * returns:
  61. *
  62. * == 1 if we're ready to go into guest state
  63. * <= 0 if we need to go back to the host with return value
  64. */
  65. int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu)
  66. {
  67. int r;
  68. WARN_ON(irqs_disabled());
  69. hard_irq_disable();
  70. while (true) {
  71. if (need_resched()) {
  72. local_irq_enable();
  73. cond_resched();
  74. hard_irq_disable();
  75. continue;
  76. }
  77. if (signal_pending(current)) {
  78. kvmppc_account_exit(vcpu, SIGNAL_EXITS);
  79. vcpu->run->exit_reason = KVM_EXIT_INTR;
  80. r = -EINTR;
  81. break;
  82. }
  83. vcpu->mode = IN_GUEST_MODE;
  84. /*
  85. * Reading vcpu->requests must happen after setting vcpu->mode,
  86. * so we don't miss a request because the requester sees
  87. * OUTSIDE_GUEST_MODE and assumes we'll be checking requests
  88. * before next entering the guest (and thus doesn't IPI).
  89. * This also orders the write to mode from any reads
  90. * to the page tables done while the VCPU is running.
  91. * Please see the comment in kvm_flush_remote_tlbs.
  92. */
  93. smp_mb();
  94. if (vcpu->requests) {
  95. /* Make sure we process requests preemptable */
  96. local_irq_enable();
  97. trace_kvm_check_requests(vcpu);
  98. r = kvmppc_core_check_requests(vcpu);
  99. hard_irq_disable();
  100. if (r > 0)
  101. continue;
  102. break;
  103. }
  104. if (kvmppc_core_prepare_to_enter(vcpu)) {
  105. /* interrupts got enabled in between, so we
  106. are back at square 1 */
  107. continue;
  108. }
  109. guest_enter_irqoff();
  110. return 1;
  111. }
  112. /* return to host */
  113. local_irq_enable();
  114. return r;
  115. }
  116. EXPORT_SYMBOL_GPL(kvmppc_prepare_to_enter);
  117. #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
  118. static void kvmppc_swab_shared(struct kvm_vcpu *vcpu)
  119. {
  120. struct kvm_vcpu_arch_shared *shared = vcpu->arch.shared;
  121. int i;
  122. shared->sprg0 = swab64(shared->sprg0);
  123. shared->sprg1 = swab64(shared->sprg1);
  124. shared->sprg2 = swab64(shared->sprg2);
  125. shared->sprg3 = swab64(shared->sprg3);
  126. shared->srr0 = swab64(shared->srr0);
  127. shared->srr1 = swab64(shared->srr1);
  128. shared->dar = swab64(shared->dar);
  129. shared->msr = swab64(shared->msr);
  130. shared->dsisr = swab32(shared->dsisr);
  131. shared->int_pending = swab32(shared->int_pending);
  132. for (i = 0; i < ARRAY_SIZE(shared->sr); i++)
  133. shared->sr[i] = swab32(shared->sr[i]);
  134. }
  135. #endif
  136. int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
  137. {
  138. int nr = kvmppc_get_gpr(vcpu, 11);
  139. int r;
  140. unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3);
  141. unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4);
  142. unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5);
  143. unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6);
  144. unsigned long r2 = 0;
  145. if (!(kvmppc_get_msr(vcpu) & MSR_SF)) {
  146. /* 32 bit mode */
  147. param1 &= 0xffffffff;
  148. param2 &= 0xffffffff;
  149. param3 &= 0xffffffff;
  150. param4 &= 0xffffffff;
  151. }
  152. switch (nr) {
  153. case KVM_HCALL_TOKEN(KVM_HC_PPC_MAP_MAGIC_PAGE):
  154. {
  155. #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
  156. /* Book3S can be little endian, find it out here */
  157. int shared_big_endian = true;
  158. if (vcpu->arch.intr_msr & MSR_LE)
  159. shared_big_endian = false;
  160. if (shared_big_endian != vcpu->arch.shared_big_endian)
  161. kvmppc_swab_shared(vcpu);
  162. vcpu->arch.shared_big_endian = shared_big_endian;
  163. #endif
  164. if (!(param2 & MAGIC_PAGE_FLAG_NOT_MAPPED_NX)) {
  165. /*
  166. * Older versions of the Linux magic page code had
  167. * a bug where they would map their trampoline code
  168. * NX. If that's the case, remove !PR NX capability.
  169. */
  170. vcpu->arch.disable_kernel_nx = true;
  171. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  172. }
  173. vcpu->arch.magic_page_pa = param1 & ~0xfffULL;
  174. vcpu->arch.magic_page_ea = param2 & ~0xfffULL;
  175. #ifdef CONFIG_PPC_64K_PAGES
  176. /*
  177. * Make sure our 4k magic page is in the same window of a 64k
  178. * page within the guest and within the host's page.
  179. */
  180. if ((vcpu->arch.magic_page_pa & 0xf000) !=
  181. ((ulong)vcpu->arch.shared & 0xf000)) {
  182. void *old_shared = vcpu->arch.shared;
  183. ulong shared = (ulong)vcpu->arch.shared;
  184. void *new_shared;
  185. shared &= PAGE_MASK;
  186. shared |= vcpu->arch.magic_page_pa & 0xf000;
  187. new_shared = (void*)shared;
  188. memcpy(new_shared, old_shared, 0x1000);
  189. vcpu->arch.shared = new_shared;
  190. }
  191. #endif
  192. r2 = KVM_MAGIC_FEAT_SR | KVM_MAGIC_FEAT_MAS0_TO_SPRG7;
  193. r = EV_SUCCESS;
  194. break;
  195. }
  196. case KVM_HCALL_TOKEN(KVM_HC_FEATURES):
  197. r = EV_SUCCESS;
  198. #if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500V2)
  199. r2 |= (1 << KVM_FEATURE_MAGIC_PAGE);
  200. #endif
  201. /* Second return value is in r4 */
  202. break;
  203. case EV_HCALL_TOKEN(EV_IDLE):
  204. r = EV_SUCCESS;
  205. kvm_vcpu_block(vcpu);
  206. clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
  207. break;
  208. default:
  209. r = EV_UNIMPLEMENTED;
  210. break;
  211. }
  212. kvmppc_set_gpr(vcpu, 4, r2);
  213. return r;
  214. }
  215. EXPORT_SYMBOL_GPL(kvmppc_kvm_pv);
  216. int kvmppc_sanity_check(struct kvm_vcpu *vcpu)
  217. {
  218. int r = false;
  219. /* We have to know what CPU to virtualize */
  220. if (!vcpu->arch.pvr)
  221. goto out;
  222. /* PAPR only works with book3s_64 */
  223. if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled)
  224. goto out;
  225. /* HV KVM can only do PAPR mode for now */
  226. if (!vcpu->arch.papr_enabled && is_kvmppc_hv_enabled(vcpu->kvm))
  227. goto out;
  228. #ifdef CONFIG_KVM_BOOKE_HV
  229. if (!cpu_has_feature(CPU_FTR_EMB_HV))
  230. goto out;
  231. #endif
  232. r = true;
  233. out:
  234. vcpu->arch.sane = r;
  235. return r ? 0 : -EINVAL;
  236. }
  237. EXPORT_SYMBOL_GPL(kvmppc_sanity_check);
  238. int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
  239. {
  240. enum emulation_result er;
  241. int r;
  242. er = kvmppc_emulate_loadstore(vcpu);
  243. switch (er) {
  244. case EMULATE_DONE:
  245. /* Future optimization: only reload non-volatiles if they were
  246. * actually modified. */
  247. r = RESUME_GUEST_NV;
  248. break;
  249. case EMULATE_AGAIN:
  250. r = RESUME_GUEST;
  251. break;
  252. case EMULATE_DO_MMIO:
  253. run->exit_reason = KVM_EXIT_MMIO;
  254. /* We must reload nonvolatiles because "update" load/store
  255. * instructions modify register state. */
  256. /* Future optimization: only reload non-volatiles if they were
  257. * actually modified. */
  258. r = RESUME_HOST_NV;
  259. break;
  260. case EMULATE_FAIL:
  261. {
  262. u32 last_inst;
  263. kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
  264. /* XXX Deliver Program interrupt to guest. */
  265. pr_emerg("%s: emulation failed (%08x)\n", __func__, last_inst);
  266. r = RESUME_HOST;
  267. break;
  268. }
  269. default:
  270. WARN_ON(1);
  271. r = RESUME_GUEST;
  272. }
  273. return r;
  274. }
  275. EXPORT_SYMBOL_GPL(kvmppc_emulate_mmio);
  276. int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
  277. bool data)
  278. {
  279. ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK;
  280. struct kvmppc_pte pte;
  281. int r;
  282. vcpu->stat.st++;
  283. r = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
  284. XLATE_WRITE, &pte);
  285. if (r < 0)
  286. return r;
  287. *eaddr = pte.raddr;
  288. if (!pte.may_write)
  289. return -EPERM;
  290. /* Magic page override */
  291. if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
  292. ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
  293. !(kvmppc_get_msr(vcpu) & MSR_PR)) {
  294. void *magic = vcpu->arch.shared;
  295. magic += pte.eaddr & 0xfff;
  296. memcpy(magic, ptr, size);
  297. return EMULATE_DONE;
  298. }
  299. if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
  300. return EMULATE_DO_MMIO;
  301. return EMULATE_DONE;
  302. }
  303. EXPORT_SYMBOL_GPL(kvmppc_st);
  304. int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
  305. bool data)
  306. {
  307. ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK;
  308. struct kvmppc_pte pte;
  309. int rc;
  310. vcpu->stat.ld++;
  311. rc = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
  312. XLATE_READ, &pte);
  313. if (rc)
  314. return rc;
  315. *eaddr = pte.raddr;
  316. if (!pte.may_read)
  317. return -EPERM;
  318. if (!data && !pte.may_execute)
  319. return -ENOEXEC;
  320. /* Magic page override */
  321. if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
  322. ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
  323. !(kvmppc_get_msr(vcpu) & MSR_PR)) {
  324. void *magic = vcpu->arch.shared;
  325. magic += pte.eaddr & 0xfff;
  326. memcpy(ptr, magic, size);
  327. return EMULATE_DONE;
  328. }
  329. if (kvm_read_guest(vcpu->kvm, pte.raddr, ptr, size))
  330. return EMULATE_DO_MMIO;
  331. return EMULATE_DONE;
  332. }
  333. EXPORT_SYMBOL_GPL(kvmppc_ld);
  334. int kvm_arch_hardware_enable(void)
  335. {
  336. return 0;
  337. }
  338. int kvm_arch_hardware_setup(void)
  339. {
  340. return 0;
  341. }
  342. void kvm_arch_check_processor_compat(void *rtn)
  343. {
  344. *(int *)rtn = kvmppc_core_check_processor_compat();
  345. }
  346. int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
  347. {
  348. struct kvmppc_ops *kvm_ops = NULL;
  349. /*
  350. * if we have both HV and PR enabled, default is HV
  351. */
  352. if (type == 0) {
  353. if (kvmppc_hv_ops)
  354. kvm_ops = kvmppc_hv_ops;
  355. else
  356. kvm_ops = kvmppc_pr_ops;
  357. if (!kvm_ops)
  358. goto err_out;
  359. } else if (type == KVM_VM_PPC_HV) {
  360. if (!kvmppc_hv_ops)
  361. goto err_out;
  362. kvm_ops = kvmppc_hv_ops;
  363. } else if (type == KVM_VM_PPC_PR) {
  364. if (!kvmppc_pr_ops)
  365. goto err_out;
  366. kvm_ops = kvmppc_pr_ops;
  367. } else
  368. goto err_out;
  369. if (kvm_ops->owner && !try_module_get(kvm_ops->owner))
  370. return -ENOENT;
  371. kvm->arch.kvm_ops = kvm_ops;
  372. return kvmppc_core_init_vm(kvm);
  373. err_out:
  374. return -EINVAL;
  375. }
  376. bool kvm_arch_has_vcpu_debugfs(void)
  377. {
  378. return false;
  379. }
  380. int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
  381. {
  382. return 0;
  383. }
  384. void kvm_arch_destroy_vm(struct kvm *kvm)
  385. {
  386. unsigned int i;
  387. struct kvm_vcpu *vcpu;
  388. #ifdef CONFIG_KVM_XICS
  389. /*
  390. * We call kick_all_cpus_sync() to ensure that all
  391. * CPUs have executed any pending IPIs before we
  392. * continue and free VCPUs structures below.
  393. */
  394. if (is_kvmppc_hv_enabled(kvm))
  395. kick_all_cpus_sync();
  396. #endif
  397. kvm_for_each_vcpu(i, vcpu, kvm)
  398. kvm_arch_vcpu_free(vcpu);
  399. mutex_lock(&kvm->lock);
  400. for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
  401. kvm->vcpus[i] = NULL;
  402. atomic_set(&kvm->online_vcpus, 0);
  403. kvmppc_core_destroy_vm(kvm);
  404. mutex_unlock(&kvm->lock);
  405. /* drop the module reference */
  406. module_put(kvm->arch.kvm_ops->owner);
  407. }
  408. int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
  409. {
  410. int r;
  411. /* Assume we're using HV mode when the HV module is loaded */
  412. int hv_enabled = kvmppc_hv_ops ? 1 : 0;
  413. if (kvm) {
  414. /*
  415. * Hooray - we know which VM type we're running on. Depend on
  416. * that rather than the guess above.
  417. */
  418. hv_enabled = is_kvmppc_hv_enabled(kvm);
  419. }
  420. switch (ext) {
  421. #ifdef CONFIG_BOOKE
  422. case KVM_CAP_PPC_BOOKE_SREGS:
  423. case KVM_CAP_PPC_BOOKE_WATCHDOG:
  424. case KVM_CAP_PPC_EPR:
  425. #else
  426. case KVM_CAP_PPC_SEGSTATE:
  427. case KVM_CAP_PPC_HIOR:
  428. case KVM_CAP_PPC_PAPR:
  429. #endif
  430. case KVM_CAP_PPC_UNSET_IRQ:
  431. case KVM_CAP_PPC_IRQ_LEVEL:
  432. case KVM_CAP_ENABLE_CAP:
  433. case KVM_CAP_ENABLE_CAP_VM:
  434. case KVM_CAP_ONE_REG:
  435. case KVM_CAP_IOEVENTFD:
  436. case KVM_CAP_DEVICE_CTRL:
  437. r = 1;
  438. break;
  439. case KVM_CAP_PPC_PAIRED_SINGLES:
  440. case KVM_CAP_PPC_OSI:
  441. case KVM_CAP_PPC_GET_PVINFO:
  442. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  443. case KVM_CAP_SW_TLB:
  444. #endif
  445. /* We support this only for PR */
  446. r = !hv_enabled;
  447. break;
  448. #ifdef CONFIG_KVM_MMIO
  449. case KVM_CAP_COALESCED_MMIO:
  450. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  451. break;
  452. #endif
  453. #ifdef CONFIG_KVM_MPIC
  454. case KVM_CAP_IRQ_MPIC:
  455. r = 1;
  456. break;
  457. #endif
  458. #ifdef CONFIG_PPC_BOOK3S_64
  459. case KVM_CAP_SPAPR_TCE:
  460. case KVM_CAP_SPAPR_TCE_64:
  461. case KVM_CAP_PPC_ALLOC_HTAB:
  462. case KVM_CAP_PPC_RTAS:
  463. case KVM_CAP_PPC_FIXUP_HCALL:
  464. case KVM_CAP_PPC_ENABLE_HCALL:
  465. #ifdef CONFIG_KVM_XICS
  466. case KVM_CAP_IRQ_XICS:
  467. #endif
  468. r = 1;
  469. break;
  470. #endif /* CONFIG_PPC_BOOK3S_64 */
  471. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  472. case KVM_CAP_PPC_SMT:
  473. if (hv_enabled)
  474. r = threads_per_subcore;
  475. else
  476. r = 0;
  477. break;
  478. case KVM_CAP_PPC_RMA:
  479. r = 0;
  480. break;
  481. case KVM_CAP_PPC_HWRNG:
  482. r = kvmppc_hwrng_present();
  483. break;
  484. #endif
  485. case KVM_CAP_SYNC_MMU:
  486. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  487. r = hv_enabled;
  488. #elif defined(KVM_ARCH_WANT_MMU_NOTIFIER)
  489. r = 1;
  490. #else
  491. r = 0;
  492. #endif
  493. break;
  494. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  495. case KVM_CAP_PPC_HTAB_FD:
  496. r = hv_enabled;
  497. break;
  498. #endif
  499. case KVM_CAP_NR_VCPUS:
  500. /*
  501. * Recommending a number of CPUs is somewhat arbitrary; we
  502. * return the number of present CPUs for -HV (since a host
  503. * will have secondary threads "offline"), and for other KVM
  504. * implementations just count online CPUs.
  505. */
  506. if (hv_enabled)
  507. r = num_present_cpus();
  508. else
  509. r = num_online_cpus();
  510. break;
  511. case KVM_CAP_NR_MEMSLOTS:
  512. r = KVM_USER_MEM_SLOTS;
  513. break;
  514. case KVM_CAP_MAX_VCPUS:
  515. r = KVM_MAX_VCPUS;
  516. break;
  517. #ifdef CONFIG_PPC_BOOK3S_64
  518. case KVM_CAP_PPC_GET_SMMU_INFO:
  519. r = 1;
  520. break;
  521. case KVM_CAP_SPAPR_MULTITCE:
  522. r = 1;
  523. break;
  524. #endif
  525. case KVM_CAP_PPC_HTM:
  526. r = cpu_has_feature(CPU_FTR_TM_COMP) && hv_enabled;
  527. break;
  528. default:
  529. r = 0;
  530. break;
  531. }
  532. return r;
  533. }
  534. long kvm_arch_dev_ioctl(struct file *filp,
  535. unsigned int ioctl, unsigned long arg)
  536. {
  537. return -EINVAL;
  538. }
  539. void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
  540. struct kvm_memory_slot *dont)
  541. {
  542. kvmppc_core_free_memslot(kvm, free, dont);
  543. }
  544. int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
  545. unsigned long npages)
  546. {
  547. return kvmppc_core_create_memslot(kvm, slot, npages);
  548. }
  549. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  550. struct kvm_memory_slot *memslot,
  551. const struct kvm_userspace_memory_region *mem,
  552. enum kvm_mr_change change)
  553. {
  554. return kvmppc_core_prepare_memory_region(kvm, memslot, mem);
  555. }
  556. void kvm_arch_commit_memory_region(struct kvm *kvm,
  557. const struct kvm_userspace_memory_region *mem,
  558. const struct kvm_memory_slot *old,
  559. const struct kvm_memory_slot *new,
  560. enum kvm_mr_change change)
  561. {
  562. kvmppc_core_commit_memory_region(kvm, mem, old, new);
  563. }
  564. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  565. struct kvm_memory_slot *slot)
  566. {
  567. kvmppc_core_flush_memslot(kvm, slot);
  568. }
  569. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  570. {
  571. struct kvm_vcpu *vcpu;
  572. vcpu = kvmppc_core_vcpu_create(kvm, id);
  573. if (!IS_ERR(vcpu)) {
  574. vcpu->arch.wqp = &vcpu->wq;
  575. kvmppc_create_vcpu_debugfs(vcpu, id);
  576. }
  577. return vcpu;
  578. }
  579. void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  580. {
  581. }
  582. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  583. {
  584. /* Make sure we're not using the vcpu anymore */
  585. hrtimer_cancel(&vcpu->arch.dec_timer);
  586. kvmppc_remove_vcpu_debugfs(vcpu);
  587. switch (vcpu->arch.irq_type) {
  588. case KVMPPC_IRQ_MPIC:
  589. kvmppc_mpic_disconnect_vcpu(vcpu->arch.mpic, vcpu);
  590. break;
  591. case KVMPPC_IRQ_XICS:
  592. kvmppc_xics_free_icp(vcpu);
  593. break;
  594. }
  595. kvmppc_core_vcpu_free(vcpu);
  596. }
  597. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  598. {
  599. kvm_arch_vcpu_free(vcpu);
  600. }
  601. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  602. {
  603. return kvmppc_core_pending_dec(vcpu);
  604. }
  605. static enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
  606. {
  607. struct kvm_vcpu *vcpu;
  608. vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
  609. kvmppc_decrementer_func(vcpu);
  610. return HRTIMER_NORESTART;
  611. }
  612. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  613. {
  614. int ret;
  615. hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  616. vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
  617. vcpu->arch.dec_expires = ~(u64)0;
  618. #ifdef CONFIG_KVM_EXIT_TIMING
  619. mutex_init(&vcpu->arch.exit_timing_lock);
  620. #endif
  621. ret = kvmppc_subarch_vcpu_init(vcpu);
  622. return ret;
  623. }
  624. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  625. {
  626. kvmppc_mmu_destroy(vcpu);
  627. kvmppc_subarch_vcpu_uninit(vcpu);
  628. }
  629. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  630. {
  631. #ifdef CONFIG_BOOKE
  632. /*
  633. * vrsave (formerly usprg0) isn't used by Linux, but may
  634. * be used by the guest.
  635. *
  636. * On non-booke this is associated with Altivec and
  637. * is handled by code in book3s.c.
  638. */
  639. mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
  640. #endif
  641. kvmppc_core_vcpu_load(vcpu, cpu);
  642. }
  643. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  644. {
  645. kvmppc_core_vcpu_put(vcpu);
  646. #ifdef CONFIG_BOOKE
  647. vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
  648. #endif
  649. }
  650. /*
  651. * irq_bypass_add_producer and irq_bypass_del_producer are only
  652. * useful if the architecture supports PCI passthrough.
  653. * irq_bypass_stop and irq_bypass_start are not needed and so
  654. * kvm_ops are not defined for them.
  655. */
  656. bool kvm_arch_has_irq_bypass(void)
  657. {
  658. return ((kvmppc_hv_ops && kvmppc_hv_ops->irq_bypass_add_producer) ||
  659. (kvmppc_pr_ops && kvmppc_pr_ops->irq_bypass_add_producer));
  660. }
  661. int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
  662. struct irq_bypass_producer *prod)
  663. {
  664. struct kvm_kernel_irqfd *irqfd =
  665. container_of(cons, struct kvm_kernel_irqfd, consumer);
  666. struct kvm *kvm = irqfd->kvm;
  667. if (kvm->arch.kvm_ops->irq_bypass_add_producer)
  668. return kvm->arch.kvm_ops->irq_bypass_add_producer(cons, prod);
  669. return 0;
  670. }
  671. void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
  672. struct irq_bypass_producer *prod)
  673. {
  674. struct kvm_kernel_irqfd *irqfd =
  675. container_of(cons, struct kvm_kernel_irqfd, consumer);
  676. struct kvm *kvm = irqfd->kvm;
  677. if (kvm->arch.kvm_ops->irq_bypass_del_producer)
  678. kvm->arch.kvm_ops->irq_bypass_del_producer(cons, prod);
  679. }
  680. static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
  681. struct kvm_run *run)
  682. {
  683. u64 uninitialized_var(gpr);
  684. if (run->mmio.len > sizeof(gpr)) {
  685. printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
  686. return;
  687. }
  688. if (!vcpu->arch.mmio_host_swabbed) {
  689. switch (run->mmio.len) {
  690. case 8: gpr = *(u64 *)run->mmio.data; break;
  691. case 4: gpr = *(u32 *)run->mmio.data; break;
  692. case 2: gpr = *(u16 *)run->mmio.data; break;
  693. case 1: gpr = *(u8 *)run->mmio.data; break;
  694. }
  695. } else {
  696. switch (run->mmio.len) {
  697. case 8: gpr = swab64(*(u64 *)run->mmio.data); break;
  698. case 4: gpr = swab32(*(u32 *)run->mmio.data); break;
  699. case 2: gpr = swab16(*(u16 *)run->mmio.data); break;
  700. case 1: gpr = *(u8 *)run->mmio.data; break;
  701. }
  702. }
  703. if (vcpu->arch.mmio_sign_extend) {
  704. switch (run->mmio.len) {
  705. #ifdef CONFIG_PPC64
  706. case 4:
  707. gpr = (s64)(s32)gpr;
  708. break;
  709. #endif
  710. case 2:
  711. gpr = (s64)(s16)gpr;
  712. break;
  713. case 1:
  714. gpr = (s64)(s8)gpr;
  715. break;
  716. }
  717. }
  718. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  719. switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
  720. case KVM_MMIO_REG_GPR:
  721. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  722. break;
  723. case KVM_MMIO_REG_FPR:
  724. VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
  725. break;
  726. #ifdef CONFIG_PPC_BOOK3S
  727. case KVM_MMIO_REG_QPR:
  728. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  729. break;
  730. case KVM_MMIO_REG_FQPR:
  731. VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
  732. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  733. break;
  734. #endif
  735. default:
  736. BUG();
  737. }
  738. }
  739. static int __kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  740. unsigned int rt, unsigned int bytes,
  741. int is_default_endian, int sign_extend)
  742. {
  743. int idx, ret;
  744. bool host_swabbed;
  745. /* Pity C doesn't have a logical XOR operator */
  746. if (kvmppc_need_byteswap(vcpu)) {
  747. host_swabbed = is_default_endian;
  748. } else {
  749. host_swabbed = !is_default_endian;
  750. }
  751. if (bytes > sizeof(run->mmio.data)) {
  752. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  753. run->mmio.len);
  754. }
  755. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  756. run->mmio.len = bytes;
  757. run->mmio.is_write = 0;
  758. vcpu->arch.io_gpr = rt;
  759. vcpu->arch.mmio_host_swabbed = host_swabbed;
  760. vcpu->mmio_needed = 1;
  761. vcpu->mmio_is_write = 0;
  762. vcpu->arch.mmio_sign_extend = sign_extend;
  763. idx = srcu_read_lock(&vcpu->kvm->srcu);
  764. ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
  765. bytes, &run->mmio.data);
  766. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  767. if (!ret) {
  768. kvmppc_complete_mmio_load(vcpu, run);
  769. vcpu->mmio_needed = 0;
  770. return EMULATE_DONE;
  771. }
  772. return EMULATE_DO_MMIO;
  773. }
  774. int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  775. unsigned int rt, unsigned int bytes,
  776. int is_default_endian)
  777. {
  778. return __kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian, 0);
  779. }
  780. EXPORT_SYMBOL_GPL(kvmppc_handle_load);
  781. /* Same as above, but sign extends */
  782. int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
  783. unsigned int rt, unsigned int bytes,
  784. int is_default_endian)
  785. {
  786. return __kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian, 1);
  787. }
  788. int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
  789. u64 val, unsigned int bytes, int is_default_endian)
  790. {
  791. void *data = run->mmio.data;
  792. int idx, ret;
  793. bool host_swabbed;
  794. /* Pity C doesn't have a logical XOR operator */
  795. if (kvmppc_need_byteswap(vcpu)) {
  796. host_swabbed = is_default_endian;
  797. } else {
  798. host_swabbed = !is_default_endian;
  799. }
  800. if (bytes > sizeof(run->mmio.data)) {
  801. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  802. run->mmio.len);
  803. }
  804. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  805. run->mmio.len = bytes;
  806. run->mmio.is_write = 1;
  807. vcpu->mmio_needed = 1;
  808. vcpu->mmio_is_write = 1;
  809. /* Store the value at the lowest bytes in 'data'. */
  810. if (!host_swabbed) {
  811. switch (bytes) {
  812. case 8: *(u64 *)data = val; break;
  813. case 4: *(u32 *)data = val; break;
  814. case 2: *(u16 *)data = val; break;
  815. case 1: *(u8 *)data = val; break;
  816. }
  817. } else {
  818. switch (bytes) {
  819. case 8: *(u64 *)data = swab64(val); break;
  820. case 4: *(u32 *)data = swab32(val); break;
  821. case 2: *(u16 *)data = swab16(val); break;
  822. case 1: *(u8 *)data = val; break;
  823. }
  824. }
  825. idx = srcu_read_lock(&vcpu->kvm->srcu);
  826. ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
  827. bytes, &run->mmio.data);
  828. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  829. if (!ret) {
  830. vcpu->mmio_needed = 0;
  831. return EMULATE_DONE;
  832. }
  833. return EMULATE_DO_MMIO;
  834. }
  835. EXPORT_SYMBOL_GPL(kvmppc_handle_store);
  836. int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  837. {
  838. int r = 0;
  839. union kvmppc_one_reg val;
  840. int size;
  841. size = one_reg_size(reg->id);
  842. if (size > sizeof(val))
  843. return -EINVAL;
  844. r = kvmppc_get_one_reg(vcpu, reg->id, &val);
  845. if (r == -EINVAL) {
  846. r = 0;
  847. switch (reg->id) {
  848. #ifdef CONFIG_ALTIVEC
  849. case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
  850. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  851. r = -ENXIO;
  852. break;
  853. }
  854. val.vval = vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0];
  855. break;
  856. case KVM_REG_PPC_VSCR:
  857. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  858. r = -ENXIO;
  859. break;
  860. }
  861. val = get_reg_val(reg->id, vcpu->arch.vr.vscr.u[3]);
  862. break;
  863. case KVM_REG_PPC_VRSAVE:
  864. val = get_reg_val(reg->id, vcpu->arch.vrsave);
  865. break;
  866. #endif /* CONFIG_ALTIVEC */
  867. default:
  868. r = -EINVAL;
  869. break;
  870. }
  871. }
  872. if (r)
  873. return r;
  874. if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size))
  875. r = -EFAULT;
  876. return r;
  877. }
  878. int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  879. {
  880. int r;
  881. union kvmppc_one_reg val;
  882. int size;
  883. size = one_reg_size(reg->id);
  884. if (size > sizeof(val))
  885. return -EINVAL;
  886. if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size))
  887. return -EFAULT;
  888. r = kvmppc_set_one_reg(vcpu, reg->id, &val);
  889. if (r == -EINVAL) {
  890. r = 0;
  891. switch (reg->id) {
  892. #ifdef CONFIG_ALTIVEC
  893. case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
  894. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  895. r = -ENXIO;
  896. break;
  897. }
  898. vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0] = val.vval;
  899. break;
  900. case KVM_REG_PPC_VSCR:
  901. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  902. r = -ENXIO;
  903. break;
  904. }
  905. vcpu->arch.vr.vscr.u[3] = set_reg_val(reg->id, val);
  906. break;
  907. case KVM_REG_PPC_VRSAVE:
  908. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  909. r = -ENXIO;
  910. break;
  911. }
  912. vcpu->arch.vrsave = set_reg_val(reg->id, val);
  913. break;
  914. #endif /* CONFIG_ALTIVEC */
  915. default:
  916. r = -EINVAL;
  917. break;
  918. }
  919. }
  920. return r;
  921. }
  922. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  923. {
  924. int r;
  925. sigset_t sigsaved;
  926. if (vcpu->sigset_active)
  927. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  928. if (vcpu->mmio_needed) {
  929. if (!vcpu->mmio_is_write)
  930. kvmppc_complete_mmio_load(vcpu, run);
  931. vcpu->mmio_needed = 0;
  932. } else if (vcpu->arch.osi_needed) {
  933. u64 *gprs = run->osi.gprs;
  934. int i;
  935. for (i = 0; i < 32; i++)
  936. kvmppc_set_gpr(vcpu, i, gprs[i]);
  937. vcpu->arch.osi_needed = 0;
  938. } else if (vcpu->arch.hcall_needed) {
  939. int i;
  940. kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
  941. for (i = 0; i < 9; ++i)
  942. kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
  943. vcpu->arch.hcall_needed = 0;
  944. #ifdef CONFIG_BOOKE
  945. } else if (vcpu->arch.epr_needed) {
  946. kvmppc_set_epr(vcpu, run->epr.epr);
  947. vcpu->arch.epr_needed = 0;
  948. #endif
  949. }
  950. r = kvmppc_vcpu_run(run, vcpu);
  951. if (vcpu->sigset_active)
  952. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  953. return r;
  954. }
  955. int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
  956. {
  957. if (irq->irq == KVM_INTERRUPT_UNSET) {
  958. kvmppc_core_dequeue_external(vcpu);
  959. return 0;
  960. }
  961. kvmppc_core_queue_external(vcpu, irq);
  962. kvm_vcpu_kick(vcpu);
  963. return 0;
  964. }
  965. static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
  966. struct kvm_enable_cap *cap)
  967. {
  968. int r;
  969. if (cap->flags)
  970. return -EINVAL;
  971. switch (cap->cap) {
  972. case KVM_CAP_PPC_OSI:
  973. r = 0;
  974. vcpu->arch.osi_enabled = true;
  975. break;
  976. case KVM_CAP_PPC_PAPR:
  977. r = 0;
  978. vcpu->arch.papr_enabled = true;
  979. break;
  980. case KVM_CAP_PPC_EPR:
  981. r = 0;
  982. if (cap->args[0])
  983. vcpu->arch.epr_flags |= KVMPPC_EPR_USER;
  984. else
  985. vcpu->arch.epr_flags &= ~KVMPPC_EPR_USER;
  986. break;
  987. #ifdef CONFIG_BOOKE
  988. case KVM_CAP_PPC_BOOKE_WATCHDOG:
  989. r = 0;
  990. vcpu->arch.watchdog_enabled = true;
  991. break;
  992. #endif
  993. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  994. case KVM_CAP_SW_TLB: {
  995. struct kvm_config_tlb cfg;
  996. void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
  997. r = -EFAULT;
  998. if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
  999. break;
  1000. r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
  1001. break;
  1002. }
  1003. #endif
  1004. #ifdef CONFIG_KVM_MPIC
  1005. case KVM_CAP_IRQ_MPIC: {
  1006. struct fd f;
  1007. struct kvm_device *dev;
  1008. r = -EBADF;
  1009. f = fdget(cap->args[0]);
  1010. if (!f.file)
  1011. break;
  1012. r = -EPERM;
  1013. dev = kvm_device_from_filp(f.file);
  1014. if (dev)
  1015. r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]);
  1016. fdput(f);
  1017. break;
  1018. }
  1019. #endif
  1020. #ifdef CONFIG_KVM_XICS
  1021. case KVM_CAP_IRQ_XICS: {
  1022. struct fd f;
  1023. struct kvm_device *dev;
  1024. r = -EBADF;
  1025. f = fdget(cap->args[0]);
  1026. if (!f.file)
  1027. break;
  1028. r = -EPERM;
  1029. dev = kvm_device_from_filp(f.file);
  1030. if (dev)
  1031. r = kvmppc_xics_connect_vcpu(dev, vcpu, cap->args[1]);
  1032. fdput(f);
  1033. break;
  1034. }
  1035. #endif /* CONFIG_KVM_XICS */
  1036. default:
  1037. r = -EINVAL;
  1038. break;
  1039. }
  1040. if (!r)
  1041. r = kvmppc_sanity_check(vcpu);
  1042. return r;
  1043. }
  1044. bool kvm_arch_intc_initialized(struct kvm *kvm)
  1045. {
  1046. #ifdef CONFIG_KVM_MPIC
  1047. if (kvm->arch.mpic)
  1048. return true;
  1049. #endif
  1050. #ifdef CONFIG_KVM_XICS
  1051. if (kvm->arch.xics)
  1052. return true;
  1053. #endif
  1054. return false;
  1055. }
  1056. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  1057. struct kvm_mp_state *mp_state)
  1058. {
  1059. return -EINVAL;
  1060. }
  1061. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  1062. struct kvm_mp_state *mp_state)
  1063. {
  1064. return -EINVAL;
  1065. }
  1066. long kvm_arch_vcpu_ioctl(struct file *filp,
  1067. unsigned int ioctl, unsigned long arg)
  1068. {
  1069. struct kvm_vcpu *vcpu = filp->private_data;
  1070. void __user *argp = (void __user *)arg;
  1071. long r;
  1072. switch (ioctl) {
  1073. case KVM_INTERRUPT: {
  1074. struct kvm_interrupt irq;
  1075. r = -EFAULT;
  1076. if (copy_from_user(&irq, argp, sizeof(irq)))
  1077. goto out;
  1078. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  1079. goto out;
  1080. }
  1081. case KVM_ENABLE_CAP:
  1082. {
  1083. struct kvm_enable_cap cap;
  1084. r = -EFAULT;
  1085. if (copy_from_user(&cap, argp, sizeof(cap)))
  1086. goto out;
  1087. r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
  1088. break;
  1089. }
  1090. case KVM_SET_ONE_REG:
  1091. case KVM_GET_ONE_REG:
  1092. {
  1093. struct kvm_one_reg reg;
  1094. r = -EFAULT;
  1095. if (copy_from_user(&reg, argp, sizeof(reg)))
  1096. goto out;
  1097. if (ioctl == KVM_SET_ONE_REG)
  1098. r = kvm_vcpu_ioctl_set_one_reg(vcpu, &reg);
  1099. else
  1100. r = kvm_vcpu_ioctl_get_one_reg(vcpu, &reg);
  1101. break;
  1102. }
  1103. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  1104. case KVM_DIRTY_TLB: {
  1105. struct kvm_dirty_tlb dirty;
  1106. r = -EFAULT;
  1107. if (copy_from_user(&dirty, argp, sizeof(dirty)))
  1108. goto out;
  1109. r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
  1110. break;
  1111. }
  1112. #endif
  1113. default:
  1114. r = -EINVAL;
  1115. }
  1116. out:
  1117. return r;
  1118. }
  1119. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  1120. {
  1121. return VM_FAULT_SIGBUS;
  1122. }
  1123. static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
  1124. {
  1125. u32 inst_nop = 0x60000000;
  1126. #ifdef CONFIG_KVM_BOOKE_HV
  1127. u32 inst_sc1 = 0x44000022;
  1128. pvinfo->hcall[0] = cpu_to_be32(inst_sc1);
  1129. pvinfo->hcall[1] = cpu_to_be32(inst_nop);
  1130. pvinfo->hcall[2] = cpu_to_be32(inst_nop);
  1131. pvinfo->hcall[3] = cpu_to_be32(inst_nop);
  1132. #else
  1133. u32 inst_lis = 0x3c000000;
  1134. u32 inst_ori = 0x60000000;
  1135. u32 inst_sc = 0x44000002;
  1136. u32 inst_imm_mask = 0xffff;
  1137. /*
  1138. * The hypercall to get into KVM from within guest context is as
  1139. * follows:
  1140. *
  1141. * lis r0, r0, KVM_SC_MAGIC_R0@h
  1142. * ori r0, KVM_SC_MAGIC_R0@l
  1143. * sc
  1144. * nop
  1145. */
  1146. pvinfo->hcall[0] = cpu_to_be32(inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask));
  1147. pvinfo->hcall[1] = cpu_to_be32(inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask));
  1148. pvinfo->hcall[2] = cpu_to_be32(inst_sc);
  1149. pvinfo->hcall[3] = cpu_to_be32(inst_nop);
  1150. #endif
  1151. pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
  1152. return 0;
  1153. }
  1154. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
  1155. bool line_status)
  1156. {
  1157. if (!irqchip_in_kernel(kvm))
  1158. return -ENXIO;
  1159. irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
  1160. irq_event->irq, irq_event->level,
  1161. line_status);
  1162. return 0;
  1163. }
  1164. static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  1165. struct kvm_enable_cap *cap)
  1166. {
  1167. int r;
  1168. if (cap->flags)
  1169. return -EINVAL;
  1170. switch (cap->cap) {
  1171. #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
  1172. case KVM_CAP_PPC_ENABLE_HCALL: {
  1173. unsigned long hcall = cap->args[0];
  1174. r = -EINVAL;
  1175. if (hcall > MAX_HCALL_OPCODE || (hcall & 3) ||
  1176. cap->args[1] > 1)
  1177. break;
  1178. if (!kvmppc_book3s_hcall_implemented(kvm, hcall))
  1179. break;
  1180. if (cap->args[1])
  1181. set_bit(hcall / 4, kvm->arch.enabled_hcalls);
  1182. else
  1183. clear_bit(hcall / 4, kvm->arch.enabled_hcalls);
  1184. r = 0;
  1185. break;
  1186. }
  1187. #endif
  1188. default:
  1189. r = -EINVAL;
  1190. break;
  1191. }
  1192. return r;
  1193. }
  1194. long kvm_arch_vm_ioctl(struct file *filp,
  1195. unsigned int ioctl, unsigned long arg)
  1196. {
  1197. struct kvm *kvm __maybe_unused = filp->private_data;
  1198. void __user *argp = (void __user *)arg;
  1199. long r;
  1200. switch (ioctl) {
  1201. case KVM_PPC_GET_PVINFO: {
  1202. struct kvm_ppc_pvinfo pvinfo;
  1203. memset(&pvinfo, 0, sizeof(pvinfo));
  1204. r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
  1205. if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
  1206. r = -EFAULT;
  1207. goto out;
  1208. }
  1209. break;
  1210. }
  1211. case KVM_ENABLE_CAP:
  1212. {
  1213. struct kvm_enable_cap cap;
  1214. r = -EFAULT;
  1215. if (copy_from_user(&cap, argp, sizeof(cap)))
  1216. goto out;
  1217. r = kvm_vm_ioctl_enable_cap(kvm, &cap);
  1218. break;
  1219. }
  1220. #ifdef CONFIG_PPC_BOOK3S_64
  1221. case KVM_CREATE_SPAPR_TCE_64: {
  1222. struct kvm_create_spapr_tce_64 create_tce_64;
  1223. r = -EFAULT;
  1224. if (copy_from_user(&create_tce_64, argp, sizeof(create_tce_64)))
  1225. goto out;
  1226. if (create_tce_64.flags) {
  1227. r = -EINVAL;
  1228. goto out;
  1229. }
  1230. r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
  1231. goto out;
  1232. }
  1233. case KVM_CREATE_SPAPR_TCE: {
  1234. struct kvm_create_spapr_tce create_tce;
  1235. struct kvm_create_spapr_tce_64 create_tce_64;
  1236. r = -EFAULT;
  1237. if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
  1238. goto out;
  1239. create_tce_64.liobn = create_tce.liobn;
  1240. create_tce_64.page_shift = IOMMU_PAGE_SHIFT_4K;
  1241. create_tce_64.offset = 0;
  1242. create_tce_64.size = create_tce.window_size >>
  1243. IOMMU_PAGE_SHIFT_4K;
  1244. create_tce_64.flags = 0;
  1245. r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
  1246. goto out;
  1247. }
  1248. case KVM_PPC_GET_SMMU_INFO: {
  1249. struct kvm_ppc_smmu_info info;
  1250. struct kvm *kvm = filp->private_data;
  1251. memset(&info, 0, sizeof(info));
  1252. r = kvm->arch.kvm_ops->get_smmu_info(kvm, &info);
  1253. if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
  1254. r = -EFAULT;
  1255. break;
  1256. }
  1257. case KVM_PPC_RTAS_DEFINE_TOKEN: {
  1258. struct kvm *kvm = filp->private_data;
  1259. r = kvm_vm_ioctl_rtas_define_token(kvm, argp);
  1260. break;
  1261. }
  1262. default: {
  1263. struct kvm *kvm = filp->private_data;
  1264. r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg);
  1265. }
  1266. #else /* CONFIG_PPC_BOOK3S_64 */
  1267. default:
  1268. r = -ENOTTY;
  1269. #endif
  1270. }
  1271. out:
  1272. return r;
  1273. }
  1274. static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
  1275. static unsigned long nr_lpids;
  1276. long kvmppc_alloc_lpid(void)
  1277. {
  1278. long lpid;
  1279. do {
  1280. lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
  1281. if (lpid >= nr_lpids) {
  1282. pr_err("%s: No LPIDs free\n", __func__);
  1283. return -ENOMEM;
  1284. }
  1285. } while (test_and_set_bit(lpid, lpid_inuse));
  1286. return lpid;
  1287. }
  1288. EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid);
  1289. void kvmppc_claim_lpid(long lpid)
  1290. {
  1291. set_bit(lpid, lpid_inuse);
  1292. }
  1293. EXPORT_SYMBOL_GPL(kvmppc_claim_lpid);
  1294. void kvmppc_free_lpid(long lpid)
  1295. {
  1296. clear_bit(lpid, lpid_inuse);
  1297. }
  1298. EXPORT_SYMBOL_GPL(kvmppc_free_lpid);
  1299. void kvmppc_init_lpid(unsigned long nr_lpids_param)
  1300. {
  1301. nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
  1302. memset(lpid_inuse, 0, sizeof(lpid_inuse));
  1303. }
  1304. EXPORT_SYMBOL_GPL(kvmppc_init_lpid);
  1305. int kvm_arch_init(void *opaque)
  1306. {
  1307. return 0;
  1308. }
  1309. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ppc_instr);