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) &&
  527. is_kvmppc_hv_enabled(kvm);
  528. break;
  529. default:
  530. r = 0;
  531. break;
  532. }
  533. return r;
  534. }
  535. long kvm_arch_dev_ioctl(struct file *filp,
  536. unsigned int ioctl, unsigned long arg)
  537. {
  538. return -EINVAL;
  539. }
  540. void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
  541. struct kvm_memory_slot *dont)
  542. {
  543. kvmppc_core_free_memslot(kvm, free, dont);
  544. }
  545. int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
  546. unsigned long npages)
  547. {
  548. return kvmppc_core_create_memslot(kvm, slot, npages);
  549. }
  550. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  551. struct kvm_memory_slot *memslot,
  552. const struct kvm_userspace_memory_region *mem,
  553. enum kvm_mr_change change)
  554. {
  555. return kvmppc_core_prepare_memory_region(kvm, memslot, mem);
  556. }
  557. void kvm_arch_commit_memory_region(struct kvm *kvm,
  558. const struct kvm_userspace_memory_region *mem,
  559. const struct kvm_memory_slot *old,
  560. const struct kvm_memory_slot *new,
  561. enum kvm_mr_change change)
  562. {
  563. kvmppc_core_commit_memory_region(kvm, mem, old, new);
  564. }
  565. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  566. struct kvm_memory_slot *slot)
  567. {
  568. kvmppc_core_flush_memslot(kvm, slot);
  569. }
  570. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
  571. {
  572. struct kvm_vcpu *vcpu;
  573. vcpu = kvmppc_core_vcpu_create(kvm, id);
  574. if (!IS_ERR(vcpu)) {
  575. vcpu->arch.wqp = &vcpu->wq;
  576. kvmppc_create_vcpu_debugfs(vcpu, id);
  577. }
  578. return vcpu;
  579. }
  580. void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  581. {
  582. }
  583. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  584. {
  585. /* Make sure we're not using the vcpu anymore */
  586. hrtimer_cancel(&vcpu->arch.dec_timer);
  587. kvmppc_remove_vcpu_debugfs(vcpu);
  588. switch (vcpu->arch.irq_type) {
  589. case KVMPPC_IRQ_MPIC:
  590. kvmppc_mpic_disconnect_vcpu(vcpu->arch.mpic, vcpu);
  591. break;
  592. case KVMPPC_IRQ_XICS:
  593. kvmppc_xics_free_icp(vcpu);
  594. break;
  595. }
  596. kvmppc_core_vcpu_free(vcpu);
  597. }
  598. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  599. {
  600. kvm_arch_vcpu_free(vcpu);
  601. }
  602. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  603. {
  604. return kvmppc_core_pending_dec(vcpu);
  605. }
  606. static enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
  607. {
  608. struct kvm_vcpu *vcpu;
  609. vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
  610. kvmppc_decrementer_func(vcpu);
  611. return HRTIMER_NORESTART;
  612. }
  613. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  614. {
  615. int ret;
  616. hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  617. vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
  618. vcpu->arch.dec_expires = ~(u64)0;
  619. #ifdef CONFIG_KVM_EXIT_TIMING
  620. mutex_init(&vcpu->arch.exit_timing_lock);
  621. #endif
  622. ret = kvmppc_subarch_vcpu_init(vcpu);
  623. return ret;
  624. }
  625. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  626. {
  627. kvmppc_mmu_destroy(vcpu);
  628. kvmppc_subarch_vcpu_uninit(vcpu);
  629. }
  630. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  631. {
  632. #ifdef CONFIG_BOOKE
  633. /*
  634. * vrsave (formerly usprg0) isn't used by Linux, but may
  635. * be used by the guest.
  636. *
  637. * On non-booke this is associated with Altivec and
  638. * is handled by code in book3s.c.
  639. */
  640. mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
  641. #endif
  642. kvmppc_core_vcpu_load(vcpu, cpu);
  643. }
  644. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  645. {
  646. kvmppc_core_vcpu_put(vcpu);
  647. #ifdef CONFIG_BOOKE
  648. vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
  649. #endif
  650. }
  651. /*
  652. * irq_bypass_add_producer and irq_bypass_del_producer are only
  653. * useful if the architecture supports PCI passthrough.
  654. * irq_bypass_stop and irq_bypass_start are not needed and so
  655. * kvm_ops are not defined for them.
  656. */
  657. bool kvm_arch_has_irq_bypass(void)
  658. {
  659. return ((kvmppc_hv_ops && kvmppc_hv_ops->irq_bypass_add_producer) ||
  660. (kvmppc_pr_ops && kvmppc_pr_ops->irq_bypass_add_producer));
  661. }
  662. int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
  663. struct irq_bypass_producer *prod)
  664. {
  665. struct kvm_kernel_irqfd *irqfd =
  666. container_of(cons, struct kvm_kernel_irqfd, consumer);
  667. struct kvm *kvm = irqfd->kvm;
  668. if (kvm->arch.kvm_ops->irq_bypass_add_producer)
  669. return kvm->arch.kvm_ops->irq_bypass_add_producer(cons, prod);
  670. return 0;
  671. }
  672. void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
  673. struct irq_bypass_producer *prod)
  674. {
  675. struct kvm_kernel_irqfd *irqfd =
  676. container_of(cons, struct kvm_kernel_irqfd, consumer);
  677. struct kvm *kvm = irqfd->kvm;
  678. if (kvm->arch.kvm_ops->irq_bypass_del_producer)
  679. kvm->arch.kvm_ops->irq_bypass_del_producer(cons, prod);
  680. }
  681. static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
  682. struct kvm_run *run)
  683. {
  684. u64 uninitialized_var(gpr);
  685. if (run->mmio.len > sizeof(gpr)) {
  686. printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
  687. return;
  688. }
  689. if (!vcpu->arch.mmio_host_swabbed) {
  690. switch (run->mmio.len) {
  691. case 8: gpr = *(u64 *)run->mmio.data; break;
  692. case 4: gpr = *(u32 *)run->mmio.data; break;
  693. case 2: gpr = *(u16 *)run->mmio.data; break;
  694. case 1: gpr = *(u8 *)run->mmio.data; break;
  695. }
  696. } else {
  697. switch (run->mmio.len) {
  698. case 8: gpr = swab64(*(u64 *)run->mmio.data); break;
  699. case 4: gpr = swab32(*(u32 *)run->mmio.data); break;
  700. case 2: gpr = swab16(*(u16 *)run->mmio.data); break;
  701. case 1: gpr = *(u8 *)run->mmio.data; break;
  702. }
  703. }
  704. if (vcpu->arch.mmio_sign_extend) {
  705. switch (run->mmio.len) {
  706. #ifdef CONFIG_PPC64
  707. case 4:
  708. gpr = (s64)(s32)gpr;
  709. break;
  710. #endif
  711. case 2:
  712. gpr = (s64)(s16)gpr;
  713. break;
  714. case 1:
  715. gpr = (s64)(s8)gpr;
  716. break;
  717. }
  718. }
  719. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  720. switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
  721. case KVM_MMIO_REG_GPR:
  722. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  723. break;
  724. case KVM_MMIO_REG_FPR:
  725. VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
  726. break;
  727. #ifdef CONFIG_PPC_BOOK3S
  728. case KVM_MMIO_REG_QPR:
  729. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  730. break;
  731. case KVM_MMIO_REG_FQPR:
  732. VCPU_FPR(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK) = gpr;
  733. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  734. break;
  735. #endif
  736. default:
  737. BUG();
  738. }
  739. }
  740. static int __kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  741. unsigned int rt, unsigned int bytes,
  742. int is_default_endian, int sign_extend)
  743. {
  744. int idx, ret;
  745. bool host_swabbed;
  746. /* Pity C doesn't have a logical XOR operator */
  747. if (kvmppc_need_byteswap(vcpu)) {
  748. host_swabbed = is_default_endian;
  749. } else {
  750. host_swabbed = !is_default_endian;
  751. }
  752. if (bytes > sizeof(run->mmio.data)) {
  753. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  754. run->mmio.len);
  755. }
  756. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  757. run->mmio.len = bytes;
  758. run->mmio.is_write = 0;
  759. vcpu->arch.io_gpr = rt;
  760. vcpu->arch.mmio_host_swabbed = host_swabbed;
  761. vcpu->mmio_needed = 1;
  762. vcpu->mmio_is_write = 0;
  763. vcpu->arch.mmio_sign_extend = sign_extend;
  764. idx = srcu_read_lock(&vcpu->kvm->srcu);
  765. ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
  766. bytes, &run->mmio.data);
  767. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  768. if (!ret) {
  769. kvmppc_complete_mmio_load(vcpu, run);
  770. vcpu->mmio_needed = 0;
  771. return EMULATE_DONE;
  772. }
  773. return EMULATE_DO_MMIO;
  774. }
  775. int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  776. unsigned int rt, unsigned int bytes,
  777. int is_default_endian)
  778. {
  779. return __kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian, 0);
  780. }
  781. EXPORT_SYMBOL_GPL(kvmppc_handle_load);
  782. /* Same as above, but sign extends */
  783. int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
  784. unsigned int rt, unsigned int bytes,
  785. int is_default_endian)
  786. {
  787. return __kvmppc_handle_load(run, vcpu, rt, bytes, is_default_endian, 1);
  788. }
  789. int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
  790. u64 val, unsigned int bytes, int is_default_endian)
  791. {
  792. void *data = run->mmio.data;
  793. int idx, ret;
  794. bool host_swabbed;
  795. /* Pity C doesn't have a logical XOR operator */
  796. if (kvmppc_need_byteswap(vcpu)) {
  797. host_swabbed = is_default_endian;
  798. } else {
  799. host_swabbed = !is_default_endian;
  800. }
  801. if (bytes > sizeof(run->mmio.data)) {
  802. printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
  803. run->mmio.len);
  804. }
  805. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  806. run->mmio.len = bytes;
  807. run->mmio.is_write = 1;
  808. vcpu->mmio_needed = 1;
  809. vcpu->mmio_is_write = 1;
  810. /* Store the value at the lowest bytes in 'data'. */
  811. if (!host_swabbed) {
  812. switch (bytes) {
  813. case 8: *(u64 *)data = val; break;
  814. case 4: *(u32 *)data = val; break;
  815. case 2: *(u16 *)data = val; break;
  816. case 1: *(u8 *)data = val; break;
  817. }
  818. } else {
  819. switch (bytes) {
  820. case 8: *(u64 *)data = swab64(val); break;
  821. case 4: *(u32 *)data = swab32(val); break;
  822. case 2: *(u16 *)data = swab16(val); break;
  823. case 1: *(u8 *)data = val; break;
  824. }
  825. }
  826. idx = srcu_read_lock(&vcpu->kvm->srcu);
  827. ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
  828. bytes, &run->mmio.data);
  829. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  830. if (!ret) {
  831. vcpu->mmio_needed = 0;
  832. return EMULATE_DONE;
  833. }
  834. return EMULATE_DO_MMIO;
  835. }
  836. EXPORT_SYMBOL_GPL(kvmppc_handle_store);
  837. int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  838. {
  839. int r = 0;
  840. union kvmppc_one_reg val;
  841. int size;
  842. size = one_reg_size(reg->id);
  843. if (size > sizeof(val))
  844. return -EINVAL;
  845. r = kvmppc_get_one_reg(vcpu, reg->id, &val);
  846. if (r == -EINVAL) {
  847. r = 0;
  848. switch (reg->id) {
  849. #ifdef CONFIG_ALTIVEC
  850. case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
  851. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  852. r = -ENXIO;
  853. break;
  854. }
  855. val.vval = vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0];
  856. break;
  857. case KVM_REG_PPC_VSCR:
  858. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  859. r = -ENXIO;
  860. break;
  861. }
  862. val = get_reg_val(reg->id, vcpu->arch.vr.vscr.u[3]);
  863. break;
  864. case KVM_REG_PPC_VRSAVE:
  865. val = get_reg_val(reg->id, vcpu->arch.vrsave);
  866. break;
  867. #endif /* CONFIG_ALTIVEC */
  868. default:
  869. r = -EINVAL;
  870. break;
  871. }
  872. }
  873. if (r)
  874. return r;
  875. if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size))
  876. r = -EFAULT;
  877. return r;
  878. }
  879. int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  880. {
  881. int r;
  882. union kvmppc_one_reg val;
  883. int size;
  884. size = one_reg_size(reg->id);
  885. if (size > sizeof(val))
  886. return -EINVAL;
  887. if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size))
  888. return -EFAULT;
  889. r = kvmppc_set_one_reg(vcpu, reg->id, &val);
  890. if (r == -EINVAL) {
  891. r = 0;
  892. switch (reg->id) {
  893. #ifdef CONFIG_ALTIVEC
  894. case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
  895. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  896. r = -ENXIO;
  897. break;
  898. }
  899. vcpu->arch.vr.vr[reg->id - KVM_REG_PPC_VR0] = val.vval;
  900. break;
  901. case KVM_REG_PPC_VSCR:
  902. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  903. r = -ENXIO;
  904. break;
  905. }
  906. vcpu->arch.vr.vscr.u[3] = set_reg_val(reg->id, val);
  907. break;
  908. case KVM_REG_PPC_VRSAVE:
  909. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  910. r = -ENXIO;
  911. break;
  912. }
  913. vcpu->arch.vrsave = set_reg_val(reg->id, val);
  914. break;
  915. #endif /* CONFIG_ALTIVEC */
  916. default:
  917. r = -EINVAL;
  918. break;
  919. }
  920. }
  921. return r;
  922. }
  923. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
  924. {
  925. int r;
  926. sigset_t sigsaved;
  927. if (vcpu->sigset_active)
  928. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  929. if (vcpu->mmio_needed) {
  930. if (!vcpu->mmio_is_write)
  931. kvmppc_complete_mmio_load(vcpu, run);
  932. vcpu->mmio_needed = 0;
  933. } else if (vcpu->arch.osi_needed) {
  934. u64 *gprs = run->osi.gprs;
  935. int i;
  936. for (i = 0; i < 32; i++)
  937. kvmppc_set_gpr(vcpu, i, gprs[i]);
  938. vcpu->arch.osi_needed = 0;
  939. } else if (vcpu->arch.hcall_needed) {
  940. int i;
  941. kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
  942. for (i = 0; i < 9; ++i)
  943. kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
  944. vcpu->arch.hcall_needed = 0;
  945. #ifdef CONFIG_BOOKE
  946. } else if (vcpu->arch.epr_needed) {
  947. kvmppc_set_epr(vcpu, run->epr.epr);
  948. vcpu->arch.epr_needed = 0;
  949. #endif
  950. }
  951. r = kvmppc_vcpu_run(run, vcpu);
  952. if (vcpu->sigset_active)
  953. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  954. return r;
  955. }
  956. int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
  957. {
  958. if (irq->irq == KVM_INTERRUPT_UNSET) {
  959. kvmppc_core_dequeue_external(vcpu);
  960. return 0;
  961. }
  962. kvmppc_core_queue_external(vcpu, irq);
  963. kvm_vcpu_kick(vcpu);
  964. return 0;
  965. }
  966. static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
  967. struct kvm_enable_cap *cap)
  968. {
  969. int r;
  970. if (cap->flags)
  971. return -EINVAL;
  972. switch (cap->cap) {
  973. case KVM_CAP_PPC_OSI:
  974. r = 0;
  975. vcpu->arch.osi_enabled = true;
  976. break;
  977. case KVM_CAP_PPC_PAPR:
  978. r = 0;
  979. vcpu->arch.papr_enabled = true;
  980. break;
  981. case KVM_CAP_PPC_EPR:
  982. r = 0;
  983. if (cap->args[0])
  984. vcpu->arch.epr_flags |= KVMPPC_EPR_USER;
  985. else
  986. vcpu->arch.epr_flags &= ~KVMPPC_EPR_USER;
  987. break;
  988. #ifdef CONFIG_BOOKE
  989. case KVM_CAP_PPC_BOOKE_WATCHDOG:
  990. r = 0;
  991. vcpu->arch.watchdog_enabled = true;
  992. break;
  993. #endif
  994. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  995. case KVM_CAP_SW_TLB: {
  996. struct kvm_config_tlb cfg;
  997. void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
  998. r = -EFAULT;
  999. if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
  1000. break;
  1001. r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
  1002. break;
  1003. }
  1004. #endif
  1005. #ifdef CONFIG_KVM_MPIC
  1006. case KVM_CAP_IRQ_MPIC: {
  1007. struct fd f;
  1008. struct kvm_device *dev;
  1009. r = -EBADF;
  1010. f = fdget(cap->args[0]);
  1011. if (!f.file)
  1012. break;
  1013. r = -EPERM;
  1014. dev = kvm_device_from_filp(f.file);
  1015. if (dev)
  1016. r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]);
  1017. fdput(f);
  1018. break;
  1019. }
  1020. #endif
  1021. #ifdef CONFIG_KVM_XICS
  1022. case KVM_CAP_IRQ_XICS: {
  1023. struct fd f;
  1024. struct kvm_device *dev;
  1025. r = -EBADF;
  1026. f = fdget(cap->args[0]);
  1027. if (!f.file)
  1028. break;
  1029. r = -EPERM;
  1030. dev = kvm_device_from_filp(f.file);
  1031. if (dev)
  1032. r = kvmppc_xics_connect_vcpu(dev, vcpu, cap->args[1]);
  1033. fdput(f);
  1034. break;
  1035. }
  1036. #endif /* CONFIG_KVM_XICS */
  1037. default:
  1038. r = -EINVAL;
  1039. break;
  1040. }
  1041. if (!r)
  1042. r = kvmppc_sanity_check(vcpu);
  1043. return r;
  1044. }
  1045. bool kvm_arch_intc_initialized(struct kvm *kvm)
  1046. {
  1047. #ifdef CONFIG_KVM_MPIC
  1048. if (kvm->arch.mpic)
  1049. return true;
  1050. #endif
  1051. #ifdef CONFIG_KVM_XICS
  1052. if (kvm->arch.xics)
  1053. return true;
  1054. #endif
  1055. return false;
  1056. }
  1057. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  1058. struct kvm_mp_state *mp_state)
  1059. {
  1060. return -EINVAL;
  1061. }
  1062. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  1063. struct kvm_mp_state *mp_state)
  1064. {
  1065. return -EINVAL;
  1066. }
  1067. long kvm_arch_vcpu_ioctl(struct file *filp,
  1068. unsigned int ioctl, unsigned long arg)
  1069. {
  1070. struct kvm_vcpu *vcpu = filp->private_data;
  1071. void __user *argp = (void __user *)arg;
  1072. long r;
  1073. switch (ioctl) {
  1074. case KVM_INTERRUPT: {
  1075. struct kvm_interrupt irq;
  1076. r = -EFAULT;
  1077. if (copy_from_user(&irq, argp, sizeof(irq)))
  1078. goto out;
  1079. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  1080. goto out;
  1081. }
  1082. case KVM_ENABLE_CAP:
  1083. {
  1084. struct kvm_enable_cap cap;
  1085. r = -EFAULT;
  1086. if (copy_from_user(&cap, argp, sizeof(cap)))
  1087. goto out;
  1088. r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
  1089. break;
  1090. }
  1091. case KVM_SET_ONE_REG:
  1092. case KVM_GET_ONE_REG:
  1093. {
  1094. struct kvm_one_reg reg;
  1095. r = -EFAULT;
  1096. if (copy_from_user(&reg, argp, sizeof(reg)))
  1097. goto out;
  1098. if (ioctl == KVM_SET_ONE_REG)
  1099. r = kvm_vcpu_ioctl_set_one_reg(vcpu, &reg);
  1100. else
  1101. r = kvm_vcpu_ioctl_get_one_reg(vcpu, &reg);
  1102. break;
  1103. }
  1104. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  1105. case KVM_DIRTY_TLB: {
  1106. struct kvm_dirty_tlb dirty;
  1107. r = -EFAULT;
  1108. if (copy_from_user(&dirty, argp, sizeof(dirty)))
  1109. goto out;
  1110. r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
  1111. break;
  1112. }
  1113. #endif
  1114. default:
  1115. r = -EINVAL;
  1116. }
  1117. out:
  1118. return r;
  1119. }
  1120. int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  1121. {
  1122. return VM_FAULT_SIGBUS;
  1123. }
  1124. static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
  1125. {
  1126. u32 inst_nop = 0x60000000;
  1127. #ifdef CONFIG_KVM_BOOKE_HV
  1128. u32 inst_sc1 = 0x44000022;
  1129. pvinfo->hcall[0] = cpu_to_be32(inst_sc1);
  1130. pvinfo->hcall[1] = cpu_to_be32(inst_nop);
  1131. pvinfo->hcall[2] = cpu_to_be32(inst_nop);
  1132. pvinfo->hcall[3] = cpu_to_be32(inst_nop);
  1133. #else
  1134. u32 inst_lis = 0x3c000000;
  1135. u32 inst_ori = 0x60000000;
  1136. u32 inst_sc = 0x44000002;
  1137. u32 inst_imm_mask = 0xffff;
  1138. /*
  1139. * The hypercall to get into KVM from within guest context is as
  1140. * follows:
  1141. *
  1142. * lis r0, r0, KVM_SC_MAGIC_R0@h
  1143. * ori r0, KVM_SC_MAGIC_R0@l
  1144. * sc
  1145. * nop
  1146. */
  1147. pvinfo->hcall[0] = cpu_to_be32(inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask));
  1148. pvinfo->hcall[1] = cpu_to_be32(inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask));
  1149. pvinfo->hcall[2] = cpu_to_be32(inst_sc);
  1150. pvinfo->hcall[3] = cpu_to_be32(inst_nop);
  1151. #endif
  1152. pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
  1153. return 0;
  1154. }
  1155. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
  1156. bool line_status)
  1157. {
  1158. if (!irqchip_in_kernel(kvm))
  1159. return -ENXIO;
  1160. irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
  1161. irq_event->irq, irq_event->level,
  1162. line_status);
  1163. return 0;
  1164. }
  1165. static int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  1166. struct kvm_enable_cap *cap)
  1167. {
  1168. int r;
  1169. if (cap->flags)
  1170. return -EINVAL;
  1171. switch (cap->cap) {
  1172. #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
  1173. case KVM_CAP_PPC_ENABLE_HCALL: {
  1174. unsigned long hcall = cap->args[0];
  1175. r = -EINVAL;
  1176. if (hcall > MAX_HCALL_OPCODE || (hcall & 3) ||
  1177. cap->args[1] > 1)
  1178. break;
  1179. if (!kvmppc_book3s_hcall_implemented(kvm, hcall))
  1180. break;
  1181. if (cap->args[1])
  1182. set_bit(hcall / 4, kvm->arch.enabled_hcalls);
  1183. else
  1184. clear_bit(hcall / 4, kvm->arch.enabled_hcalls);
  1185. r = 0;
  1186. break;
  1187. }
  1188. #endif
  1189. default:
  1190. r = -EINVAL;
  1191. break;
  1192. }
  1193. return r;
  1194. }
  1195. long kvm_arch_vm_ioctl(struct file *filp,
  1196. unsigned int ioctl, unsigned long arg)
  1197. {
  1198. struct kvm *kvm __maybe_unused = filp->private_data;
  1199. void __user *argp = (void __user *)arg;
  1200. long r;
  1201. switch (ioctl) {
  1202. case KVM_PPC_GET_PVINFO: {
  1203. struct kvm_ppc_pvinfo pvinfo;
  1204. memset(&pvinfo, 0, sizeof(pvinfo));
  1205. r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
  1206. if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
  1207. r = -EFAULT;
  1208. goto out;
  1209. }
  1210. break;
  1211. }
  1212. case KVM_ENABLE_CAP:
  1213. {
  1214. struct kvm_enable_cap cap;
  1215. r = -EFAULT;
  1216. if (copy_from_user(&cap, argp, sizeof(cap)))
  1217. goto out;
  1218. r = kvm_vm_ioctl_enable_cap(kvm, &cap);
  1219. break;
  1220. }
  1221. #ifdef CONFIG_PPC_BOOK3S_64
  1222. case KVM_CREATE_SPAPR_TCE_64: {
  1223. struct kvm_create_spapr_tce_64 create_tce_64;
  1224. r = -EFAULT;
  1225. if (copy_from_user(&create_tce_64, argp, sizeof(create_tce_64)))
  1226. goto out;
  1227. if (create_tce_64.flags) {
  1228. r = -EINVAL;
  1229. goto out;
  1230. }
  1231. r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
  1232. goto out;
  1233. }
  1234. case KVM_CREATE_SPAPR_TCE: {
  1235. struct kvm_create_spapr_tce create_tce;
  1236. struct kvm_create_spapr_tce_64 create_tce_64;
  1237. r = -EFAULT;
  1238. if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
  1239. goto out;
  1240. create_tce_64.liobn = create_tce.liobn;
  1241. create_tce_64.page_shift = IOMMU_PAGE_SHIFT_4K;
  1242. create_tce_64.offset = 0;
  1243. create_tce_64.size = create_tce.window_size >>
  1244. IOMMU_PAGE_SHIFT_4K;
  1245. create_tce_64.flags = 0;
  1246. r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
  1247. goto out;
  1248. }
  1249. case KVM_PPC_GET_SMMU_INFO: {
  1250. struct kvm_ppc_smmu_info info;
  1251. struct kvm *kvm = filp->private_data;
  1252. memset(&info, 0, sizeof(info));
  1253. r = kvm->arch.kvm_ops->get_smmu_info(kvm, &info);
  1254. if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
  1255. r = -EFAULT;
  1256. break;
  1257. }
  1258. case KVM_PPC_RTAS_DEFINE_TOKEN: {
  1259. struct kvm *kvm = filp->private_data;
  1260. r = kvm_vm_ioctl_rtas_define_token(kvm, argp);
  1261. break;
  1262. }
  1263. default: {
  1264. struct kvm *kvm = filp->private_data;
  1265. r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg);
  1266. }
  1267. #else /* CONFIG_PPC_BOOK3S_64 */
  1268. default:
  1269. r = -ENOTTY;
  1270. #endif
  1271. }
  1272. out:
  1273. return r;
  1274. }
  1275. static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
  1276. static unsigned long nr_lpids;
  1277. long kvmppc_alloc_lpid(void)
  1278. {
  1279. long lpid;
  1280. do {
  1281. lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
  1282. if (lpid >= nr_lpids) {
  1283. pr_err("%s: No LPIDs free\n", __func__);
  1284. return -ENOMEM;
  1285. }
  1286. } while (test_and_set_bit(lpid, lpid_inuse));
  1287. return lpid;
  1288. }
  1289. EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid);
  1290. void kvmppc_claim_lpid(long lpid)
  1291. {
  1292. set_bit(lpid, lpid_inuse);
  1293. }
  1294. EXPORT_SYMBOL_GPL(kvmppc_claim_lpid);
  1295. void kvmppc_free_lpid(long lpid)
  1296. {
  1297. clear_bit(lpid, lpid_inuse);
  1298. }
  1299. EXPORT_SYMBOL_GPL(kvmppc_free_lpid);
  1300. void kvmppc_init_lpid(unsigned long nr_lpids_param)
  1301. {
  1302. nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
  1303. memset(lpid_inuse, 0, sizeof(lpid_inuse));
  1304. }
  1305. EXPORT_SYMBOL_GPL(kvmppc_init_lpid);
  1306. int kvm_arch_init(void *opaque)
  1307. {
  1308. return 0;
  1309. }
  1310. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ppc_instr);