powerpc.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright IBM Corp. 2007
  5. *
  6. * Authors: Hollis Blanchard <hollisb@us.ibm.com>
  7. * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
  8. */
  9. #include <linux/errno.h>
  10. #include <linux/err.h>
  11. #include <linux/kvm_host.h>
  12. #include <linux/vmalloc.h>
  13. #include <linux/hrtimer.h>
  14. #include <linux/sched/signal.h>
  15. #include <linux/fs.h>
  16. #include <linux/slab.h>
  17. #include <linux/file.h>
  18. #include <linux/module.h>
  19. #include <linux/irqbypass.h>
  20. #include <linux/kvm_irqfd.h>
  21. #include <linux/of.h>
  22. #include <asm/cputable.h>
  23. #include <linux/uaccess.h>
  24. #include <asm/kvm_ppc.h>
  25. #include <asm/cputhreads.h>
  26. #include <asm/irqflags.h>
  27. #include <asm/iommu.h>
  28. #include <asm/switch_to.h>
  29. #include <asm/xive.h>
  30. #ifdef CONFIG_PPC_PSERIES
  31. #include <asm/hvcall.h>
  32. #include <asm/plpar_wrappers.h>
  33. #endif
  34. #include <asm/ultravisor.h>
  35. #include <asm/setup.h>
  36. #include "timing.h"
  37. #include "../mm/mmu_decl.h"
  38. #define CREATE_TRACE_POINTS
  39. #include "trace.h"
  40. struct kvmppc_ops *kvmppc_hv_ops;
  41. EXPORT_SYMBOL_GPL(kvmppc_hv_ops);
  42. struct kvmppc_ops *kvmppc_pr_ops;
  43. EXPORT_SYMBOL_GPL(kvmppc_pr_ops);
  44. int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
  45. {
  46. return !!(v->arch.pending_exceptions) || kvm_request_pending(v);
  47. }
  48. bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)
  49. {
  50. return kvm_arch_vcpu_runnable(vcpu);
  51. }
  52. bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
  53. {
  54. return false;
  55. }
  56. int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
  57. {
  58. return 1;
  59. }
  60. /*
  61. * Common checks before entering the guest world. Call with interrupts
  62. * disabled.
  63. *
  64. * returns:
  65. *
  66. * == 1 if we're ready to go into guest state
  67. * <= 0 if we need to go back to the host with return value
  68. */
  69. int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu)
  70. {
  71. int r;
  72. WARN_ON(irqs_disabled());
  73. hard_irq_disable();
  74. while (true) {
  75. if (need_resched()) {
  76. local_irq_enable();
  77. cond_resched();
  78. hard_irq_disable();
  79. continue;
  80. }
  81. if (signal_pending(current)) {
  82. kvmppc_account_exit(vcpu, SIGNAL_EXITS);
  83. vcpu->run->exit_reason = KVM_EXIT_INTR;
  84. r = -EINTR;
  85. break;
  86. }
  87. vcpu->mode = IN_GUEST_MODE;
  88. /*
  89. * Reading vcpu->requests must happen after setting vcpu->mode,
  90. * so we don't miss a request because the requester sees
  91. * OUTSIDE_GUEST_MODE and assumes we'll be checking requests
  92. * before next entering the guest (and thus doesn't IPI).
  93. * This also orders the write to mode from any reads
  94. * to the page tables done while the VCPU is running.
  95. * Please see the comment in kvm_flush_remote_tlbs.
  96. */
  97. smp_mb();
  98. if (kvm_request_pending(vcpu)) {
  99. /* Make sure we process requests preemptable */
  100. local_irq_enable();
  101. trace_kvm_check_requests(vcpu);
  102. r = kvmppc_core_check_requests(vcpu);
  103. hard_irq_disable();
  104. if (r > 0)
  105. continue;
  106. break;
  107. }
  108. if (kvmppc_core_prepare_to_enter(vcpu)) {
  109. /* interrupts got enabled in between, so we
  110. are back at square 1 */
  111. continue;
  112. }
  113. guest_enter_irqoff();
  114. return 1;
  115. }
  116. /* return to host */
  117. local_irq_enable();
  118. return r;
  119. }
  120. EXPORT_SYMBOL_GPL(kvmppc_prepare_to_enter);
  121. #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
  122. static void kvmppc_swab_shared(struct kvm_vcpu *vcpu)
  123. {
  124. struct kvm_vcpu_arch_shared *shared = vcpu->arch.shared;
  125. int i;
  126. shared->sprg0 = swab64(shared->sprg0);
  127. shared->sprg1 = swab64(shared->sprg1);
  128. shared->sprg2 = swab64(shared->sprg2);
  129. shared->sprg3 = swab64(shared->sprg3);
  130. shared->srr0 = swab64(shared->srr0);
  131. shared->srr1 = swab64(shared->srr1);
  132. shared->dar = swab64(shared->dar);
  133. shared->msr = swab64(shared->msr);
  134. shared->dsisr = swab32(shared->dsisr);
  135. shared->int_pending = swab32(shared->int_pending);
  136. for (i = 0; i < ARRAY_SIZE(shared->sr); i++)
  137. shared->sr[i] = swab32(shared->sr[i]);
  138. }
  139. #endif
  140. int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
  141. {
  142. int nr = kvmppc_get_gpr(vcpu, 11);
  143. int r;
  144. unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3);
  145. unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4);
  146. unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5);
  147. unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6);
  148. unsigned long r2 = 0;
  149. if (!(kvmppc_get_msr(vcpu) & MSR_SF)) {
  150. /* 32 bit mode */
  151. param1 &= 0xffffffff;
  152. param2 &= 0xffffffff;
  153. param3 &= 0xffffffff;
  154. param4 &= 0xffffffff;
  155. }
  156. switch (nr) {
  157. case KVM_HCALL_TOKEN(KVM_HC_PPC_MAP_MAGIC_PAGE):
  158. {
  159. #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
  160. /* Book3S can be little endian, find it out here */
  161. int shared_big_endian = true;
  162. if (vcpu->arch.intr_msr & MSR_LE)
  163. shared_big_endian = false;
  164. if (shared_big_endian != vcpu->arch.shared_big_endian)
  165. kvmppc_swab_shared(vcpu);
  166. vcpu->arch.shared_big_endian = shared_big_endian;
  167. #endif
  168. if (!(param2 & MAGIC_PAGE_FLAG_NOT_MAPPED_NX)) {
  169. /*
  170. * Older versions of the Linux magic page code had
  171. * a bug where they would map their trampoline code
  172. * NX. If that's the case, remove !PR NX capability.
  173. */
  174. vcpu->arch.disable_kernel_nx = true;
  175. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  176. }
  177. vcpu->arch.magic_page_pa = param1 & ~0xfffULL;
  178. vcpu->arch.magic_page_ea = param2 & ~0xfffULL;
  179. #ifdef CONFIG_PPC_64K_PAGES
  180. /*
  181. * Make sure our 4k magic page is in the same window of a 64k
  182. * page within the guest and within the host's page.
  183. */
  184. if ((vcpu->arch.magic_page_pa & 0xf000) !=
  185. ((ulong)vcpu->arch.shared & 0xf000)) {
  186. void *old_shared = vcpu->arch.shared;
  187. ulong shared = (ulong)vcpu->arch.shared;
  188. void *new_shared;
  189. shared &= PAGE_MASK;
  190. shared |= vcpu->arch.magic_page_pa & 0xf000;
  191. new_shared = (void*)shared;
  192. memcpy(new_shared, old_shared, 0x1000);
  193. vcpu->arch.shared = new_shared;
  194. }
  195. #endif
  196. r2 = KVM_MAGIC_FEAT_SR | KVM_MAGIC_FEAT_MAS0_TO_SPRG7;
  197. r = EV_SUCCESS;
  198. break;
  199. }
  200. case KVM_HCALL_TOKEN(KVM_HC_FEATURES):
  201. r = EV_SUCCESS;
  202. #if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500V2)
  203. r2 |= (1 << KVM_FEATURE_MAGIC_PAGE);
  204. #endif
  205. /* Second return value is in r4 */
  206. break;
  207. case EV_HCALL_TOKEN(EV_IDLE):
  208. r = EV_SUCCESS;
  209. kvm_vcpu_halt(vcpu);
  210. break;
  211. default:
  212. r = EV_UNIMPLEMENTED;
  213. break;
  214. }
  215. kvmppc_set_gpr(vcpu, 4, r2);
  216. return r;
  217. }
  218. EXPORT_SYMBOL_GPL(kvmppc_kvm_pv);
  219. int kvmppc_sanity_check(struct kvm_vcpu *vcpu)
  220. {
  221. int r = false;
  222. /* We have to know what CPU to virtualize */
  223. if (!vcpu->arch.pvr)
  224. goto out;
  225. /* PAPR only works with book3s_64 */
  226. if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled)
  227. goto out;
  228. /* HV KVM can only do PAPR mode for now */
  229. if (!vcpu->arch.papr_enabled && is_kvmppc_hv_enabled(vcpu->kvm))
  230. goto out;
  231. #ifdef CONFIG_KVM_BOOKE_HV
  232. if (!cpu_has_feature(CPU_FTR_EMB_HV))
  233. goto out;
  234. #endif
  235. r = true;
  236. out:
  237. vcpu->arch.sane = r;
  238. return r ? 0 : -EINVAL;
  239. }
  240. EXPORT_SYMBOL_GPL(kvmppc_sanity_check);
  241. int kvmppc_emulate_mmio(struct kvm_vcpu *vcpu)
  242. {
  243. enum emulation_result er;
  244. int r;
  245. er = kvmppc_emulate_loadstore(vcpu);
  246. switch (er) {
  247. case EMULATE_DONE:
  248. /* Future optimization: only reload non-volatiles if they were
  249. * actually modified. */
  250. r = RESUME_GUEST_NV;
  251. break;
  252. case EMULATE_AGAIN:
  253. r = RESUME_GUEST;
  254. break;
  255. case EMULATE_DO_MMIO:
  256. vcpu->run->exit_reason = KVM_EXIT_MMIO;
  257. /* We must reload nonvolatiles because "update" load/store
  258. * instructions modify register state. */
  259. /* Future optimization: only reload non-volatiles if they were
  260. * actually modified. */
  261. r = RESUME_HOST_NV;
  262. break;
  263. case EMULATE_FAIL:
  264. {
  265. ppc_inst_t last_inst;
  266. kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
  267. kvm_debug_ratelimited("Guest access to device memory using unsupported instruction (opcode: %#08x)\n",
  268. ppc_inst_val(last_inst));
  269. /*
  270. * Injecting a Data Storage here is a bit more
  271. * accurate since the instruction that caused the
  272. * access could still be a valid one.
  273. */
  274. if (!IS_ENABLED(CONFIG_BOOKE)) {
  275. ulong dsisr = DSISR_BADACCESS;
  276. if (vcpu->mmio_is_write)
  277. dsisr |= DSISR_ISSTORE;
  278. kvmppc_core_queue_data_storage(vcpu,
  279. kvmppc_get_msr(vcpu) & SRR1_PREFIXED,
  280. vcpu->arch.vaddr_accessed, dsisr);
  281. } else {
  282. /*
  283. * BookE does not send a SIGBUS on a bad
  284. * fault, so use a Program interrupt instead
  285. * to avoid a fault loop.
  286. */
  287. kvmppc_core_queue_program(vcpu, 0);
  288. }
  289. r = RESUME_GUEST;
  290. break;
  291. }
  292. default:
  293. WARN_ON(1);
  294. r = RESUME_GUEST;
  295. }
  296. return r;
  297. }
  298. EXPORT_SYMBOL_GPL(kvmppc_emulate_mmio);
  299. int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
  300. bool data)
  301. {
  302. ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK;
  303. struct kvmppc_pte pte;
  304. int r = -EINVAL;
  305. vcpu->stat.st++;
  306. if (vcpu->kvm->arch.kvm_ops && vcpu->kvm->arch.kvm_ops->store_to_eaddr)
  307. r = vcpu->kvm->arch.kvm_ops->store_to_eaddr(vcpu, eaddr, ptr,
  308. size);
  309. if ((!r) || (r == -EAGAIN))
  310. return r;
  311. r = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
  312. XLATE_WRITE, &pte);
  313. if (r < 0)
  314. return r;
  315. *eaddr = pte.raddr;
  316. if (!pte.may_write)
  317. return -EPERM;
  318. /* Magic page override */
  319. if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
  320. ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
  321. !(kvmppc_get_msr(vcpu) & MSR_PR)) {
  322. void *magic = vcpu->arch.shared;
  323. magic += pte.eaddr & 0xfff;
  324. memcpy(magic, ptr, size);
  325. return EMULATE_DONE;
  326. }
  327. if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
  328. return EMULATE_DO_MMIO;
  329. return EMULATE_DONE;
  330. }
  331. EXPORT_SYMBOL_GPL(kvmppc_st);
  332. int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
  333. bool data)
  334. {
  335. ulong mp_pa = vcpu->arch.magic_page_pa & KVM_PAM & PAGE_MASK;
  336. struct kvmppc_pte pte;
  337. int rc = -EINVAL;
  338. vcpu->stat.ld++;
  339. if (vcpu->kvm->arch.kvm_ops && vcpu->kvm->arch.kvm_ops->load_from_eaddr)
  340. rc = vcpu->kvm->arch.kvm_ops->load_from_eaddr(vcpu, eaddr, ptr,
  341. size);
  342. if ((!rc) || (rc == -EAGAIN))
  343. return rc;
  344. rc = kvmppc_xlate(vcpu, *eaddr, data ? XLATE_DATA : XLATE_INST,
  345. XLATE_READ, &pte);
  346. if (rc)
  347. return rc;
  348. *eaddr = pte.raddr;
  349. if (!pte.may_read)
  350. return -EPERM;
  351. if (!data && !pte.may_execute)
  352. return -ENOEXEC;
  353. /* Magic page override */
  354. if (kvmppc_supports_magic_page(vcpu) && mp_pa &&
  355. ((pte.raddr & KVM_PAM & PAGE_MASK) == mp_pa) &&
  356. !(kvmppc_get_msr(vcpu) & MSR_PR)) {
  357. void *magic = vcpu->arch.shared;
  358. magic += pte.eaddr & 0xfff;
  359. memcpy(ptr, magic, size);
  360. return EMULATE_DONE;
  361. }
  362. kvm_vcpu_srcu_read_lock(vcpu);
  363. rc = kvm_read_guest(vcpu->kvm, pte.raddr, ptr, size);
  364. kvm_vcpu_srcu_read_unlock(vcpu);
  365. if (rc)
  366. return EMULATE_DO_MMIO;
  367. return EMULATE_DONE;
  368. }
  369. EXPORT_SYMBOL_GPL(kvmppc_ld);
  370. int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
  371. {
  372. struct kvmppc_ops *kvm_ops = NULL;
  373. int r;
  374. /*
  375. * if we have both HV and PR enabled, default is HV
  376. */
  377. if (type == 0) {
  378. if (kvmppc_hv_ops)
  379. kvm_ops = kvmppc_hv_ops;
  380. else
  381. kvm_ops = kvmppc_pr_ops;
  382. if (!kvm_ops)
  383. goto err_out;
  384. } else if (type == KVM_VM_PPC_HV) {
  385. if (!kvmppc_hv_ops)
  386. goto err_out;
  387. kvm_ops = kvmppc_hv_ops;
  388. } else if (type == KVM_VM_PPC_PR) {
  389. if (!kvmppc_pr_ops)
  390. goto err_out;
  391. kvm_ops = kvmppc_pr_ops;
  392. } else
  393. goto err_out;
  394. if (!try_module_get(kvm_ops->owner))
  395. return -ENOENT;
  396. kvm->arch.kvm_ops = kvm_ops;
  397. r = kvmppc_core_init_vm(kvm);
  398. if (r)
  399. module_put(kvm_ops->owner);
  400. return r;
  401. err_out:
  402. return -EINVAL;
  403. }
  404. void kvm_arch_destroy_vm(struct kvm *kvm)
  405. {
  406. #ifdef CONFIG_KVM_XICS
  407. /*
  408. * We call kick_all_cpus_sync() to ensure that all
  409. * CPUs have executed any pending IPIs before we
  410. * continue and free VCPUs structures below.
  411. */
  412. if (is_kvmppc_hv_enabled(kvm))
  413. kick_all_cpus_sync();
  414. #endif
  415. kvm_destroy_vcpus(kvm);
  416. mutex_lock(&kvm->lock);
  417. kvmppc_core_destroy_vm(kvm);
  418. mutex_unlock(&kvm->lock);
  419. /* drop the module reference */
  420. module_put(kvm->arch.kvm_ops->owner);
  421. }
  422. int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
  423. {
  424. int r;
  425. /* Assume we're using HV mode when the HV module is loaded */
  426. int hv_enabled = kvmppc_hv_ops ? 1 : 0;
  427. if (kvm) {
  428. /*
  429. * Hooray - we know which VM type we're running on. Depend on
  430. * that rather than the guess above.
  431. */
  432. hv_enabled = is_kvmppc_hv_enabled(kvm);
  433. }
  434. switch (ext) {
  435. #ifdef CONFIG_BOOKE
  436. case KVM_CAP_PPC_BOOKE_SREGS:
  437. case KVM_CAP_PPC_BOOKE_WATCHDOG:
  438. case KVM_CAP_PPC_EPR:
  439. #else
  440. case KVM_CAP_PPC_SEGSTATE:
  441. case KVM_CAP_PPC_HIOR:
  442. case KVM_CAP_PPC_PAPR:
  443. #endif
  444. case KVM_CAP_PPC_UNSET_IRQ:
  445. case KVM_CAP_PPC_IRQ_LEVEL:
  446. case KVM_CAP_ENABLE_CAP:
  447. case KVM_CAP_ONE_REG:
  448. case KVM_CAP_IOEVENTFD:
  449. case KVM_CAP_IMMEDIATE_EXIT:
  450. case KVM_CAP_SET_GUEST_DEBUG:
  451. r = 1;
  452. break;
  453. case KVM_CAP_PPC_GUEST_DEBUG_SSTEP:
  454. case KVM_CAP_PPC_PAIRED_SINGLES:
  455. case KVM_CAP_PPC_OSI:
  456. case KVM_CAP_PPC_GET_PVINFO:
  457. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  458. case KVM_CAP_SW_TLB:
  459. #endif
  460. /* We support this only for PR */
  461. r = !hv_enabled;
  462. break;
  463. #ifdef CONFIG_KVM_MPIC
  464. case KVM_CAP_IRQ_MPIC:
  465. r = 1;
  466. break;
  467. #endif
  468. #ifdef CONFIG_PPC_BOOK3S_64
  469. case KVM_CAP_SPAPR_TCE:
  470. fallthrough;
  471. case KVM_CAP_SPAPR_TCE_64:
  472. case KVM_CAP_SPAPR_TCE_VFIO:
  473. case KVM_CAP_PPC_RTAS:
  474. case KVM_CAP_PPC_FIXUP_HCALL:
  475. case KVM_CAP_PPC_ENABLE_HCALL:
  476. #ifdef CONFIG_KVM_XICS
  477. case KVM_CAP_IRQ_XICS:
  478. #endif
  479. case KVM_CAP_PPC_GET_CPU_CHAR:
  480. r = 1;
  481. break;
  482. #ifdef CONFIG_KVM_XIVE
  483. case KVM_CAP_PPC_IRQ_XIVE:
  484. /*
  485. * We need XIVE to be enabled on the platform (implies
  486. * a POWER9 processor) and the PowerNV platform, as
  487. * nested is not yet supported.
  488. */
  489. r = xive_enabled() && !!cpu_has_feature(CPU_FTR_HVMODE) &&
  490. kvmppc_xive_native_supported();
  491. break;
  492. #endif
  493. #ifdef CONFIG_HAVE_KVM_IRQCHIP
  494. case KVM_CAP_IRQFD_RESAMPLE:
  495. r = !xive_enabled();
  496. break;
  497. #endif
  498. case KVM_CAP_PPC_ALLOC_HTAB:
  499. r = hv_enabled;
  500. break;
  501. #endif /* CONFIG_PPC_BOOK3S_64 */
  502. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  503. case KVM_CAP_PPC_SMT:
  504. r = 0;
  505. if (kvm) {
  506. if (kvm->arch.emul_smt_mode > 1)
  507. r = kvm->arch.emul_smt_mode;
  508. else
  509. r = kvm->arch.smt_mode;
  510. } else if (hv_enabled) {
  511. if (cpu_has_feature(CPU_FTR_ARCH_300))
  512. r = 1;
  513. else
  514. r = threads_per_subcore;
  515. }
  516. break;
  517. case KVM_CAP_PPC_SMT_POSSIBLE:
  518. r = 1;
  519. if (hv_enabled) {
  520. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  521. r = ((threads_per_subcore << 1) - 1);
  522. else
  523. /* P9 can emulate dbells, so allow any mode */
  524. r = 8 | 4 | 2 | 1;
  525. }
  526. break;
  527. case KVM_CAP_PPC_RMA:
  528. r = 0;
  529. break;
  530. case KVM_CAP_PPC_HWRNG:
  531. r = kvmppc_hwrng_present();
  532. break;
  533. case KVM_CAP_PPC_MMU_RADIX:
  534. r = !!(hv_enabled && radix_enabled());
  535. break;
  536. case KVM_CAP_PPC_MMU_HASH_V3:
  537. r = !!(hv_enabled && kvmppc_hv_ops->hash_v3_possible &&
  538. kvmppc_hv_ops->hash_v3_possible());
  539. break;
  540. case KVM_CAP_PPC_NESTED_HV:
  541. r = !!(hv_enabled && kvmppc_hv_ops->enable_nested &&
  542. !kvmppc_hv_ops->enable_nested(NULL));
  543. break;
  544. #endif
  545. case KVM_CAP_SYNC_MMU:
  546. BUILD_BUG_ON(!IS_ENABLED(CONFIG_KVM_GENERIC_MMU_NOTIFIER));
  547. r = 1;
  548. break;
  549. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  550. case KVM_CAP_PPC_HTAB_FD:
  551. r = hv_enabled;
  552. break;
  553. #endif
  554. case KVM_CAP_NR_VCPUS:
  555. /*
  556. * Recommending a number of CPUs is somewhat arbitrary; we
  557. * return the number of present CPUs for -HV (since a host
  558. * will have secondary threads "offline"), and for other KVM
  559. * implementations just count online CPUs.
  560. */
  561. if (hv_enabled)
  562. r = min_t(unsigned int, num_present_cpus(), KVM_MAX_VCPUS);
  563. else
  564. r = min_t(unsigned int, num_online_cpus(), KVM_MAX_VCPUS);
  565. break;
  566. case KVM_CAP_MAX_VCPUS:
  567. r = KVM_MAX_VCPUS;
  568. break;
  569. case KVM_CAP_MAX_VCPU_ID:
  570. r = KVM_MAX_VCPU_IDS;
  571. break;
  572. #ifdef CONFIG_PPC_BOOK3S_64
  573. case KVM_CAP_PPC_GET_SMMU_INFO:
  574. r = 1;
  575. break;
  576. case KVM_CAP_SPAPR_MULTITCE:
  577. r = 1;
  578. break;
  579. case KVM_CAP_SPAPR_RESIZE_HPT:
  580. r = !!hv_enabled;
  581. break;
  582. #endif
  583. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  584. case KVM_CAP_PPC_FWNMI:
  585. r = hv_enabled;
  586. break;
  587. #endif
  588. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  589. case KVM_CAP_PPC_HTM:
  590. r = !!(cur_cpu_spec->cpu_user_features2 & PPC_FEATURE2_HTM) ||
  591. (hv_enabled && cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST));
  592. break;
  593. #endif
  594. #if defined(CONFIG_KVM_BOOK3S_HV_POSSIBLE)
  595. case KVM_CAP_PPC_SECURE_GUEST:
  596. r = hv_enabled && kvmppc_hv_ops->enable_svm &&
  597. !kvmppc_hv_ops->enable_svm(NULL);
  598. break;
  599. case KVM_CAP_PPC_DAWR1:
  600. r = !!(hv_enabled && kvmppc_hv_ops->enable_dawr1 &&
  601. !kvmppc_hv_ops->enable_dawr1(NULL));
  602. break;
  603. case KVM_CAP_PPC_RPT_INVALIDATE:
  604. r = 1;
  605. break;
  606. #endif
  607. case KVM_CAP_PPC_AIL_MODE_3:
  608. r = 0;
  609. /*
  610. * KVM PR, POWER7, and some POWER9s don't support AIL=3 mode.
  611. * The POWER9s can support it if the guest runs in hash mode,
  612. * but QEMU doesn't necessarily query the capability in time.
  613. */
  614. if (hv_enabled) {
  615. if (kvmhv_on_pseries()) {
  616. if (pseries_reloc_on_exception())
  617. r = 1;
  618. } else if (cpu_has_feature(CPU_FTR_ARCH_207S) &&
  619. !cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG)) {
  620. r = 1;
  621. }
  622. }
  623. break;
  624. default:
  625. r = 0;
  626. break;
  627. }
  628. return r;
  629. }
  630. long kvm_arch_dev_ioctl(struct file *filp,
  631. unsigned int ioctl, unsigned long arg)
  632. {
  633. return -EINVAL;
  634. }
  635. void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
  636. {
  637. kvmppc_core_free_memslot(kvm, slot);
  638. }
  639. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  640. const struct kvm_memory_slot *old,
  641. struct kvm_memory_slot *new,
  642. enum kvm_mr_change change)
  643. {
  644. return kvmppc_core_prepare_memory_region(kvm, old, new, change);
  645. }
  646. void kvm_arch_commit_memory_region(struct kvm *kvm,
  647. struct kvm_memory_slot *old,
  648. const struct kvm_memory_slot *new,
  649. enum kvm_mr_change change)
  650. {
  651. kvmppc_core_commit_memory_region(kvm, old, new, change);
  652. }
  653. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  654. struct kvm_memory_slot *slot)
  655. {
  656. kvmppc_core_flush_memslot(kvm, slot);
  657. }
  658. int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
  659. {
  660. return 0;
  661. }
  662. static enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
  663. {
  664. struct kvm_vcpu *vcpu;
  665. vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
  666. kvmppc_decrementer_func(vcpu);
  667. return HRTIMER_NORESTART;
  668. }
  669. int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
  670. {
  671. int err;
  672. hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  673. vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
  674. #ifdef CONFIG_KVM_EXIT_TIMING
  675. mutex_init(&vcpu->arch.exit_timing_lock);
  676. #endif
  677. err = kvmppc_subarch_vcpu_init(vcpu);
  678. if (err)
  679. return err;
  680. err = kvmppc_core_vcpu_create(vcpu);
  681. if (err)
  682. goto out_vcpu_uninit;
  683. rcuwait_init(&vcpu->arch.wait);
  684. vcpu->arch.waitp = &vcpu->arch.wait;
  685. return 0;
  686. out_vcpu_uninit:
  687. kvmppc_subarch_vcpu_uninit(vcpu);
  688. return err;
  689. }
  690. void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  691. {
  692. }
  693. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  694. {
  695. /* Make sure we're not using the vcpu anymore */
  696. hrtimer_cancel(&vcpu->arch.dec_timer);
  697. switch (vcpu->arch.irq_type) {
  698. case KVMPPC_IRQ_MPIC:
  699. kvmppc_mpic_disconnect_vcpu(vcpu->arch.mpic, vcpu);
  700. break;
  701. case KVMPPC_IRQ_XICS:
  702. if (xics_on_xive())
  703. kvmppc_xive_cleanup_vcpu(vcpu);
  704. else
  705. kvmppc_xics_free_icp(vcpu);
  706. break;
  707. case KVMPPC_IRQ_XIVE:
  708. kvmppc_xive_native_cleanup_vcpu(vcpu);
  709. break;
  710. }
  711. kvmppc_core_vcpu_free(vcpu);
  712. kvmppc_subarch_vcpu_uninit(vcpu);
  713. }
  714. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  715. {
  716. return kvmppc_core_pending_dec(vcpu);
  717. }
  718. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  719. {
  720. #ifdef CONFIG_BOOKE
  721. /*
  722. * vrsave (formerly usprg0) isn't used by Linux, but may
  723. * be used by the guest.
  724. *
  725. * On non-booke this is associated with Altivec and
  726. * is handled by code in book3s.c.
  727. */
  728. mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
  729. #endif
  730. kvmppc_core_vcpu_load(vcpu, cpu);
  731. }
  732. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  733. {
  734. kvmppc_core_vcpu_put(vcpu);
  735. #ifdef CONFIG_BOOKE
  736. vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
  737. #endif
  738. }
  739. /*
  740. * irq_bypass_add_producer and irq_bypass_del_producer are only
  741. * useful if the architecture supports PCI passthrough.
  742. * irq_bypass_stop and irq_bypass_start are not needed and so
  743. * kvm_ops are not defined for them.
  744. */
  745. bool kvm_arch_has_irq_bypass(void)
  746. {
  747. return ((kvmppc_hv_ops && kvmppc_hv_ops->irq_bypass_add_producer) ||
  748. (kvmppc_pr_ops && kvmppc_pr_ops->irq_bypass_add_producer));
  749. }
  750. int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
  751. struct irq_bypass_producer *prod)
  752. {
  753. struct kvm_kernel_irqfd *irqfd =
  754. container_of(cons, struct kvm_kernel_irqfd, consumer);
  755. struct kvm *kvm = irqfd->kvm;
  756. if (kvm->arch.kvm_ops->irq_bypass_add_producer)
  757. return kvm->arch.kvm_ops->irq_bypass_add_producer(cons, prod);
  758. return 0;
  759. }
  760. void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
  761. struct irq_bypass_producer *prod)
  762. {
  763. struct kvm_kernel_irqfd *irqfd =
  764. container_of(cons, struct kvm_kernel_irqfd, consumer);
  765. struct kvm *kvm = irqfd->kvm;
  766. if (kvm->arch.kvm_ops->irq_bypass_del_producer)
  767. kvm->arch.kvm_ops->irq_bypass_del_producer(cons, prod);
  768. }
  769. #ifdef CONFIG_VSX
  770. static inline int kvmppc_get_vsr_dword_offset(int index)
  771. {
  772. int offset;
  773. if ((index != 0) && (index != 1))
  774. return -1;
  775. #ifdef __BIG_ENDIAN
  776. offset = index;
  777. #else
  778. offset = 1 - index;
  779. #endif
  780. return offset;
  781. }
  782. static inline int kvmppc_get_vsr_word_offset(int index)
  783. {
  784. int offset;
  785. if ((index > 3) || (index < 0))
  786. return -1;
  787. #ifdef __BIG_ENDIAN
  788. offset = index;
  789. #else
  790. offset = 3 - index;
  791. #endif
  792. return offset;
  793. }
  794. static inline void kvmppc_set_vsr_dword(struct kvm_vcpu *vcpu,
  795. u64 gpr)
  796. {
  797. union kvmppc_one_reg val;
  798. int offset = kvmppc_get_vsr_dword_offset(vcpu->arch.mmio_vsx_offset);
  799. int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
  800. if (offset == -1)
  801. return;
  802. if (index >= 32) {
  803. kvmppc_get_vsx_vr(vcpu, index - 32, &val.vval);
  804. val.vsxval[offset] = gpr;
  805. kvmppc_set_vsx_vr(vcpu, index - 32, &val.vval);
  806. } else {
  807. kvmppc_set_vsx_fpr(vcpu, index, offset, gpr);
  808. }
  809. }
  810. static inline void kvmppc_set_vsr_dword_dump(struct kvm_vcpu *vcpu,
  811. u64 gpr)
  812. {
  813. union kvmppc_one_reg val;
  814. int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
  815. if (index >= 32) {
  816. kvmppc_get_vsx_vr(vcpu, index - 32, &val.vval);
  817. val.vsxval[0] = gpr;
  818. val.vsxval[1] = gpr;
  819. kvmppc_set_vsx_vr(vcpu, index - 32, &val.vval);
  820. } else {
  821. kvmppc_set_vsx_fpr(vcpu, index, 0, gpr);
  822. kvmppc_set_vsx_fpr(vcpu, index, 1, gpr);
  823. }
  824. }
  825. static inline void kvmppc_set_vsr_word_dump(struct kvm_vcpu *vcpu,
  826. u32 gpr)
  827. {
  828. union kvmppc_one_reg val;
  829. int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
  830. if (index >= 32) {
  831. val.vsx32val[0] = gpr;
  832. val.vsx32val[1] = gpr;
  833. val.vsx32val[2] = gpr;
  834. val.vsx32val[3] = gpr;
  835. kvmppc_set_vsx_vr(vcpu, index - 32, &val.vval);
  836. } else {
  837. val.vsx32val[0] = gpr;
  838. val.vsx32val[1] = gpr;
  839. kvmppc_set_vsx_fpr(vcpu, index, 0, val.vsxval[0]);
  840. kvmppc_set_vsx_fpr(vcpu, index, 1, val.vsxval[0]);
  841. }
  842. }
  843. static inline void kvmppc_set_vsr_word(struct kvm_vcpu *vcpu,
  844. u32 gpr32)
  845. {
  846. union kvmppc_one_reg val;
  847. int offset = kvmppc_get_vsr_word_offset(vcpu->arch.mmio_vsx_offset);
  848. int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
  849. int dword_offset, word_offset;
  850. if (offset == -1)
  851. return;
  852. if (index >= 32) {
  853. kvmppc_get_vsx_vr(vcpu, index - 32, &val.vval);
  854. val.vsx32val[offset] = gpr32;
  855. kvmppc_set_vsx_vr(vcpu, index - 32, &val.vval);
  856. } else {
  857. dword_offset = offset / 2;
  858. word_offset = offset % 2;
  859. val.vsxval[0] = kvmppc_get_vsx_fpr(vcpu, index, dword_offset);
  860. val.vsx32val[word_offset] = gpr32;
  861. kvmppc_set_vsx_fpr(vcpu, index, dword_offset, val.vsxval[0]);
  862. }
  863. }
  864. #endif /* CONFIG_VSX */
  865. #ifdef CONFIG_ALTIVEC
  866. static inline int kvmppc_get_vmx_offset_generic(struct kvm_vcpu *vcpu,
  867. int index, int element_size)
  868. {
  869. int offset;
  870. int elts = sizeof(vector128)/element_size;
  871. if ((index < 0) || (index >= elts))
  872. return -1;
  873. if (kvmppc_need_byteswap(vcpu))
  874. offset = elts - index - 1;
  875. else
  876. offset = index;
  877. return offset;
  878. }
  879. static inline int kvmppc_get_vmx_dword_offset(struct kvm_vcpu *vcpu,
  880. int index)
  881. {
  882. return kvmppc_get_vmx_offset_generic(vcpu, index, 8);
  883. }
  884. static inline int kvmppc_get_vmx_word_offset(struct kvm_vcpu *vcpu,
  885. int index)
  886. {
  887. return kvmppc_get_vmx_offset_generic(vcpu, index, 4);
  888. }
  889. static inline int kvmppc_get_vmx_hword_offset(struct kvm_vcpu *vcpu,
  890. int index)
  891. {
  892. return kvmppc_get_vmx_offset_generic(vcpu, index, 2);
  893. }
  894. static inline int kvmppc_get_vmx_byte_offset(struct kvm_vcpu *vcpu,
  895. int index)
  896. {
  897. return kvmppc_get_vmx_offset_generic(vcpu, index, 1);
  898. }
  899. static inline void kvmppc_set_vmx_dword(struct kvm_vcpu *vcpu,
  900. u64 gpr)
  901. {
  902. union kvmppc_one_reg val;
  903. int offset = kvmppc_get_vmx_dword_offset(vcpu,
  904. vcpu->arch.mmio_vmx_offset);
  905. int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
  906. if (offset == -1)
  907. return;
  908. kvmppc_get_vsx_vr(vcpu, index, &val.vval);
  909. val.vsxval[offset] = gpr;
  910. kvmppc_set_vsx_vr(vcpu, index, &val.vval);
  911. }
  912. static inline void kvmppc_set_vmx_word(struct kvm_vcpu *vcpu,
  913. u32 gpr32)
  914. {
  915. union kvmppc_one_reg val;
  916. int offset = kvmppc_get_vmx_word_offset(vcpu,
  917. vcpu->arch.mmio_vmx_offset);
  918. int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
  919. if (offset == -1)
  920. return;
  921. kvmppc_get_vsx_vr(vcpu, index, &val.vval);
  922. val.vsx32val[offset] = gpr32;
  923. kvmppc_set_vsx_vr(vcpu, index, &val.vval);
  924. }
  925. static inline void kvmppc_set_vmx_hword(struct kvm_vcpu *vcpu,
  926. u16 gpr16)
  927. {
  928. union kvmppc_one_reg val;
  929. int offset = kvmppc_get_vmx_hword_offset(vcpu,
  930. vcpu->arch.mmio_vmx_offset);
  931. int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
  932. if (offset == -1)
  933. return;
  934. kvmppc_get_vsx_vr(vcpu, index, &val.vval);
  935. val.vsx16val[offset] = gpr16;
  936. kvmppc_set_vsx_vr(vcpu, index, &val.vval);
  937. }
  938. static inline void kvmppc_set_vmx_byte(struct kvm_vcpu *vcpu,
  939. u8 gpr8)
  940. {
  941. union kvmppc_one_reg val;
  942. int offset = kvmppc_get_vmx_byte_offset(vcpu,
  943. vcpu->arch.mmio_vmx_offset);
  944. int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
  945. if (offset == -1)
  946. return;
  947. kvmppc_get_vsx_vr(vcpu, index, &val.vval);
  948. val.vsx8val[offset] = gpr8;
  949. kvmppc_set_vsx_vr(vcpu, index, &val.vval);
  950. }
  951. #endif /* CONFIG_ALTIVEC */
  952. #ifdef CONFIG_PPC_FPU
  953. static inline u64 sp_to_dp(u32 fprs)
  954. {
  955. u64 fprd;
  956. preempt_disable();
  957. enable_kernel_fp();
  958. asm ("lfs%U1%X1 0,%1; stfd%U0%X0 0,%0" : "=m<>" (fprd) : "m<>" (fprs)
  959. : "fr0");
  960. preempt_enable();
  961. return fprd;
  962. }
  963. static inline u32 dp_to_sp(u64 fprd)
  964. {
  965. u32 fprs;
  966. preempt_disable();
  967. enable_kernel_fp();
  968. asm ("lfd%U1%X1 0,%1; stfs%U0%X0 0,%0" : "=m<>" (fprs) : "m<>" (fprd)
  969. : "fr0");
  970. preempt_enable();
  971. return fprs;
  972. }
  973. #else
  974. #define sp_to_dp(x) (x)
  975. #define dp_to_sp(x) (x)
  976. #endif /* CONFIG_PPC_FPU */
  977. static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu)
  978. {
  979. struct kvm_run *run = vcpu->run;
  980. u64 gpr;
  981. if (run->mmio.len > sizeof(gpr))
  982. return;
  983. if (!vcpu->arch.mmio_host_swabbed) {
  984. switch (run->mmio.len) {
  985. case 8: gpr = *(u64 *)run->mmio.data; break;
  986. case 4: gpr = *(u32 *)run->mmio.data; break;
  987. case 2: gpr = *(u16 *)run->mmio.data; break;
  988. case 1: gpr = *(u8 *)run->mmio.data; break;
  989. }
  990. } else {
  991. switch (run->mmio.len) {
  992. case 8: gpr = swab64(*(u64 *)run->mmio.data); break;
  993. case 4: gpr = swab32(*(u32 *)run->mmio.data); break;
  994. case 2: gpr = swab16(*(u16 *)run->mmio.data); break;
  995. case 1: gpr = *(u8 *)run->mmio.data; break;
  996. }
  997. }
  998. /* conversion between single and double precision */
  999. if ((vcpu->arch.mmio_sp64_extend) && (run->mmio.len == 4))
  1000. gpr = sp_to_dp(gpr);
  1001. if (vcpu->arch.mmio_sign_extend) {
  1002. switch (run->mmio.len) {
  1003. #ifdef CONFIG_PPC64
  1004. case 4:
  1005. gpr = (s64)(s32)gpr;
  1006. break;
  1007. #endif
  1008. case 2:
  1009. gpr = (s64)(s16)gpr;
  1010. break;
  1011. case 1:
  1012. gpr = (s64)(s8)gpr;
  1013. break;
  1014. }
  1015. }
  1016. switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
  1017. case KVM_MMIO_REG_GPR:
  1018. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  1019. break;
  1020. case KVM_MMIO_REG_FPR:
  1021. if (vcpu->kvm->arch.kvm_ops->giveup_ext)
  1022. vcpu->kvm->arch.kvm_ops->giveup_ext(vcpu, MSR_FP);
  1023. kvmppc_set_fpr(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK, gpr);
  1024. break;
  1025. #ifdef CONFIG_PPC_BOOK3S
  1026. case KVM_MMIO_REG_QPR:
  1027. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  1028. break;
  1029. case KVM_MMIO_REG_FQPR:
  1030. kvmppc_set_fpr(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK, gpr);
  1031. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  1032. break;
  1033. #endif
  1034. #ifdef CONFIG_VSX
  1035. case KVM_MMIO_REG_VSX:
  1036. if (vcpu->kvm->arch.kvm_ops->giveup_ext)
  1037. vcpu->kvm->arch.kvm_ops->giveup_ext(vcpu, MSR_VSX);
  1038. if (vcpu->arch.mmio_copy_type == KVMPPC_VSX_COPY_DWORD)
  1039. kvmppc_set_vsr_dword(vcpu, gpr);
  1040. else if (vcpu->arch.mmio_copy_type == KVMPPC_VSX_COPY_WORD)
  1041. kvmppc_set_vsr_word(vcpu, gpr);
  1042. else if (vcpu->arch.mmio_copy_type ==
  1043. KVMPPC_VSX_COPY_DWORD_LOAD_DUMP)
  1044. kvmppc_set_vsr_dword_dump(vcpu, gpr);
  1045. else if (vcpu->arch.mmio_copy_type ==
  1046. KVMPPC_VSX_COPY_WORD_LOAD_DUMP)
  1047. kvmppc_set_vsr_word_dump(vcpu, gpr);
  1048. break;
  1049. #endif
  1050. #ifdef CONFIG_ALTIVEC
  1051. case KVM_MMIO_REG_VMX:
  1052. if (vcpu->kvm->arch.kvm_ops->giveup_ext)
  1053. vcpu->kvm->arch.kvm_ops->giveup_ext(vcpu, MSR_VEC);
  1054. if (vcpu->arch.mmio_copy_type == KVMPPC_VMX_COPY_DWORD)
  1055. kvmppc_set_vmx_dword(vcpu, gpr);
  1056. else if (vcpu->arch.mmio_copy_type == KVMPPC_VMX_COPY_WORD)
  1057. kvmppc_set_vmx_word(vcpu, gpr);
  1058. else if (vcpu->arch.mmio_copy_type ==
  1059. KVMPPC_VMX_COPY_HWORD)
  1060. kvmppc_set_vmx_hword(vcpu, gpr);
  1061. else if (vcpu->arch.mmio_copy_type ==
  1062. KVMPPC_VMX_COPY_BYTE)
  1063. kvmppc_set_vmx_byte(vcpu, gpr);
  1064. break;
  1065. #endif
  1066. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  1067. case KVM_MMIO_REG_NESTED_GPR:
  1068. if (kvmppc_need_byteswap(vcpu))
  1069. gpr = swab64(gpr);
  1070. kvm_vcpu_write_guest(vcpu, vcpu->arch.nested_io_gpr, &gpr,
  1071. sizeof(gpr));
  1072. break;
  1073. #endif
  1074. default:
  1075. BUG();
  1076. }
  1077. }
  1078. static int __kvmppc_handle_load(struct kvm_vcpu *vcpu,
  1079. unsigned int rt, unsigned int bytes,
  1080. int is_default_endian, int sign_extend)
  1081. {
  1082. struct kvm_run *run = vcpu->run;
  1083. int idx, ret;
  1084. bool host_swabbed;
  1085. /* Pity C doesn't have a logical XOR operator */
  1086. if (kvmppc_need_byteswap(vcpu)) {
  1087. host_swabbed = is_default_endian;
  1088. } else {
  1089. host_swabbed = !is_default_endian;
  1090. }
  1091. if (bytes > sizeof(run->mmio.data))
  1092. return EMULATE_FAIL;
  1093. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  1094. run->mmio.len = bytes;
  1095. run->mmio.is_write = 0;
  1096. vcpu->arch.io_gpr = rt;
  1097. vcpu->arch.mmio_host_swabbed = host_swabbed;
  1098. vcpu->mmio_needed = 1;
  1099. vcpu->mmio_is_write = 0;
  1100. vcpu->arch.mmio_sign_extend = sign_extend;
  1101. idx = srcu_read_lock(&vcpu->kvm->srcu);
  1102. ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
  1103. bytes, &run->mmio.data);
  1104. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  1105. if (!ret) {
  1106. kvmppc_complete_mmio_load(vcpu);
  1107. vcpu->mmio_needed = 0;
  1108. return EMULATE_DONE;
  1109. }
  1110. return EMULATE_DO_MMIO;
  1111. }
  1112. int kvmppc_handle_load(struct kvm_vcpu *vcpu,
  1113. unsigned int rt, unsigned int bytes,
  1114. int is_default_endian)
  1115. {
  1116. return __kvmppc_handle_load(vcpu, rt, bytes, is_default_endian, 0);
  1117. }
  1118. EXPORT_SYMBOL_GPL(kvmppc_handle_load);
  1119. /* Same as above, but sign extends */
  1120. int kvmppc_handle_loads(struct kvm_vcpu *vcpu,
  1121. unsigned int rt, unsigned int bytes,
  1122. int is_default_endian)
  1123. {
  1124. return __kvmppc_handle_load(vcpu, rt, bytes, is_default_endian, 1);
  1125. }
  1126. #ifdef CONFIG_VSX
  1127. int kvmppc_handle_vsx_load(struct kvm_vcpu *vcpu,
  1128. unsigned int rt, unsigned int bytes,
  1129. int is_default_endian, int mmio_sign_extend)
  1130. {
  1131. enum emulation_result emulated = EMULATE_DONE;
  1132. /* Currently, mmio_vsx_copy_nums only allowed to be 4 or less */
  1133. if (vcpu->arch.mmio_vsx_copy_nums > 4)
  1134. return EMULATE_FAIL;
  1135. while (vcpu->arch.mmio_vsx_copy_nums) {
  1136. emulated = __kvmppc_handle_load(vcpu, rt, bytes,
  1137. is_default_endian, mmio_sign_extend);
  1138. if (emulated != EMULATE_DONE)
  1139. break;
  1140. vcpu->arch.paddr_accessed += vcpu->run->mmio.len;
  1141. vcpu->arch.mmio_vsx_copy_nums--;
  1142. vcpu->arch.mmio_vsx_offset++;
  1143. }
  1144. return emulated;
  1145. }
  1146. #endif /* CONFIG_VSX */
  1147. int kvmppc_handle_store(struct kvm_vcpu *vcpu,
  1148. u64 val, unsigned int bytes, int is_default_endian)
  1149. {
  1150. struct kvm_run *run = vcpu->run;
  1151. void *data = run->mmio.data;
  1152. int idx, ret;
  1153. bool host_swabbed;
  1154. /* Pity C doesn't have a logical XOR operator */
  1155. if (kvmppc_need_byteswap(vcpu)) {
  1156. host_swabbed = is_default_endian;
  1157. } else {
  1158. host_swabbed = !is_default_endian;
  1159. }
  1160. if (bytes > sizeof(run->mmio.data))
  1161. return EMULATE_FAIL;
  1162. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  1163. run->mmio.len = bytes;
  1164. run->mmio.is_write = 1;
  1165. vcpu->mmio_needed = 1;
  1166. vcpu->mmio_is_write = 1;
  1167. if ((vcpu->arch.mmio_sp64_extend) && (bytes == 4))
  1168. val = dp_to_sp(val);
  1169. /* Store the value at the lowest bytes in 'data'. */
  1170. if (!host_swabbed) {
  1171. switch (bytes) {
  1172. case 8: *(u64 *)data = val; break;
  1173. case 4: *(u32 *)data = val; break;
  1174. case 2: *(u16 *)data = val; break;
  1175. case 1: *(u8 *)data = val; break;
  1176. }
  1177. } else {
  1178. switch (bytes) {
  1179. case 8: *(u64 *)data = swab64(val); break;
  1180. case 4: *(u32 *)data = swab32(val); break;
  1181. case 2: *(u16 *)data = swab16(val); break;
  1182. case 1: *(u8 *)data = val; break;
  1183. }
  1184. }
  1185. idx = srcu_read_lock(&vcpu->kvm->srcu);
  1186. ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
  1187. bytes, &run->mmio.data);
  1188. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  1189. if (!ret) {
  1190. vcpu->mmio_needed = 0;
  1191. return EMULATE_DONE;
  1192. }
  1193. return EMULATE_DO_MMIO;
  1194. }
  1195. EXPORT_SYMBOL_GPL(kvmppc_handle_store);
  1196. #ifdef CONFIG_VSX
  1197. static inline int kvmppc_get_vsr_data(struct kvm_vcpu *vcpu, int rs, u64 *val)
  1198. {
  1199. u32 dword_offset, word_offset;
  1200. union kvmppc_one_reg reg;
  1201. int vsx_offset = 0;
  1202. int copy_type = vcpu->arch.mmio_copy_type;
  1203. int result = 0;
  1204. switch (copy_type) {
  1205. case KVMPPC_VSX_COPY_DWORD:
  1206. vsx_offset =
  1207. kvmppc_get_vsr_dword_offset(vcpu->arch.mmio_vsx_offset);
  1208. if (vsx_offset == -1) {
  1209. result = -1;
  1210. break;
  1211. }
  1212. if (rs < 32) {
  1213. *val = kvmppc_get_vsx_fpr(vcpu, rs, vsx_offset);
  1214. } else {
  1215. kvmppc_get_vsx_vr(vcpu, rs - 32, &reg.vval);
  1216. *val = reg.vsxval[vsx_offset];
  1217. }
  1218. break;
  1219. case KVMPPC_VSX_COPY_WORD:
  1220. vsx_offset =
  1221. kvmppc_get_vsr_word_offset(vcpu->arch.mmio_vsx_offset);
  1222. if (vsx_offset == -1) {
  1223. result = -1;
  1224. break;
  1225. }
  1226. if (rs < 32) {
  1227. dword_offset = vsx_offset / 2;
  1228. word_offset = vsx_offset % 2;
  1229. reg.vsxval[0] = kvmppc_get_vsx_fpr(vcpu, rs, dword_offset);
  1230. *val = reg.vsx32val[word_offset];
  1231. } else {
  1232. kvmppc_get_vsx_vr(vcpu, rs - 32, &reg.vval);
  1233. *val = reg.vsx32val[vsx_offset];
  1234. }
  1235. break;
  1236. default:
  1237. result = -1;
  1238. break;
  1239. }
  1240. return result;
  1241. }
  1242. int kvmppc_handle_vsx_store(struct kvm_vcpu *vcpu,
  1243. int rs, unsigned int bytes, int is_default_endian)
  1244. {
  1245. u64 val;
  1246. enum emulation_result emulated = EMULATE_DONE;
  1247. vcpu->arch.io_gpr = rs;
  1248. /* Currently, mmio_vsx_copy_nums only allowed to be 4 or less */
  1249. if (vcpu->arch.mmio_vsx_copy_nums > 4)
  1250. return EMULATE_FAIL;
  1251. while (vcpu->arch.mmio_vsx_copy_nums) {
  1252. if (kvmppc_get_vsr_data(vcpu, rs, &val) == -1)
  1253. return EMULATE_FAIL;
  1254. emulated = kvmppc_handle_store(vcpu,
  1255. val, bytes, is_default_endian);
  1256. if (emulated != EMULATE_DONE)
  1257. break;
  1258. vcpu->arch.paddr_accessed += vcpu->run->mmio.len;
  1259. vcpu->arch.mmio_vsx_copy_nums--;
  1260. vcpu->arch.mmio_vsx_offset++;
  1261. }
  1262. return emulated;
  1263. }
  1264. static int kvmppc_emulate_mmio_vsx_loadstore(struct kvm_vcpu *vcpu)
  1265. {
  1266. struct kvm_run *run = vcpu->run;
  1267. enum emulation_result emulated = EMULATE_FAIL;
  1268. int r;
  1269. vcpu->arch.paddr_accessed += run->mmio.len;
  1270. if (!vcpu->mmio_is_write) {
  1271. emulated = kvmppc_handle_vsx_load(vcpu, vcpu->arch.io_gpr,
  1272. run->mmio.len, 1, vcpu->arch.mmio_sign_extend);
  1273. } else {
  1274. emulated = kvmppc_handle_vsx_store(vcpu,
  1275. vcpu->arch.io_gpr, run->mmio.len, 1);
  1276. }
  1277. switch (emulated) {
  1278. case EMULATE_DO_MMIO:
  1279. run->exit_reason = KVM_EXIT_MMIO;
  1280. r = RESUME_HOST;
  1281. break;
  1282. case EMULATE_FAIL:
  1283. pr_info("KVM: MMIO emulation failed (VSX repeat)\n");
  1284. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  1285. run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  1286. r = RESUME_HOST;
  1287. break;
  1288. default:
  1289. r = RESUME_GUEST;
  1290. break;
  1291. }
  1292. return r;
  1293. }
  1294. #endif /* CONFIG_VSX */
  1295. #ifdef CONFIG_ALTIVEC
  1296. int kvmppc_handle_vmx_load(struct kvm_vcpu *vcpu,
  1297. unsigned int rt, unsigned int bytes, int is_default_endian)
  1298. {
  1299. enum emulation_result emulated = EMULATE_DONE;
  1300. if (vcpu->arch.mmio_vmx_copy_nums > 2)
  1301. return EMULATE_FAIL;
  1302. while (vcpu->arch.mmio_vmx_copy_nums) {
  1303. emulated = __kvmppc_handle_load(vcpu, rt, bytes,
  1304. is_default_endian, 0);
  1305. if (emulated != EMULATE_DONE)
  1306. break;
  1307. vcpu->arch.paddr_accessed += vcpu->run->mmio.len;
  1308. vcpu->arch.mmio_vmx_copy_nums--;
  1309. vcpu->arch.mmio_vmx_offset++;
  1310. }
  1311. return emulated;
  1312. }
  1313. static int kvmppc_get_vmx_dword(struct kvm_vcpu *vcpu, int index, u64 *val)
  1314. {
  1315. union kvmppc_one_reg reg;
  1316. int vmx_offset = 0;
  1317. int result = 0;
  1318. vmx_offset =
  1319. kvmppc_get_vmx_dword_offset(vcpu, vcpu->arch.mmio_vmx_offset);
  1320. if (vmx_offset == -1)
  1321. return -1;
  1322. kvmppc_get_vsx_vr(vcpu, index, &reg.vval);
  1323. *val = reg.vsxval[vmx_offset];
  1324. return result;
  1325. }
  1326. static int kvmppc_get_vmx_word(struct kvm_vcpu *vcpu, int index, u64 *val)
  1327. {
  1328. union kvmppc_one_reg reg;
  1329. int vmx_offset = 0;
  1330. int result = 0;
  1331. vmx_offset =
  1332. kvmppc_get_vmx_word_offset(vcpu, vcpu->arch.mmio_vmx_offset);
  1333. if (vmx_offset == -1)
  1334. return -1;
  1335. kvmppc_get_vsx_vr(vcpu, index, &reg.vval);
  1336. *val = reg.vsx32val[vmx_offset];
  1337. return result;
  1338. }
  1339. static int kvmppc_get_vmx_hword(struct kvm_vcpu *vcpu, int index, u64 *val)
  1340. {
  1341. union kvmppc_one_reg reg;
  1342. int vmx_offset = 0;
  1343. int result = 0;
  1344. vmx_offset =
  1345. kvmppc_get_vmx_hword_offset(vcpu, vcpu->arch.mmio_vmx_offset);
  1346. if (vmx_offset == -1)
  1347. return -1;
  1348. kvmppc_get_vsx_vr(vcpu, index, &reg.vval);
  1349. *val = reg.vsx16val[vmx_offset];
  1350. return result;
  1351. }
  1352. static int kvmppc_get_vmx_byte(struct kvm_vcpu *vcpu, int index, u64 *val)
  1353. {
  1354. union kvmppc_one_reg reg;
  1355. int vmx_offset = 0;
  1356. int result = 0;
  1357. vmx_offset =
  1358. kvmppc_get_vmx_byte_offset(vcpu, vcpu->arch.mmio_vmx_offset);
  1359. if (vmx_offset == -1)
  1360. return -1;
  1361. kvmppc_get_vsx_vr(vcpu, index, &reg.vval);
  1362. *val = reg.vsx8val[vmx_offset];
  1363. return result;
  1364. }
  1365. int kvmppc_handle_vmx_store(struct kvm_vcpu *vcpu,
  1366. unsigned int rs, unsigned int bytes, int is_default_endian)
  1367. {
  1368. u64 val = 0;
  1369. unsigned int index = rs & KVM_MMIO_REG_MASK;
  1370. enum emulation_result emulated = EMULATE_DONE;
  1371. if (vcpu->arch.mmio_vmx_copy_nums > 2)
  1372. return EMULATE_FAIL;
  1373. vcpu->arch.io_gpr = rs;
  1374. while (vcpu->arch.mmio_vmx_copy_nums) {
  1375. switch (vcpu->arch.mmio_copy_type) {
  1376. case KVMPPC_VMX_COPY_DWORD:
  1377. if (kvmppc_get_vmx_dword(vcpu, index, &val) == -1)
  1378. return EMULATE_FAIL;
  1379. break;
  1380. case KVMPPC_VMX_COPY_WORD:
  1381. if (kvmppc_get_vmx_word(vcpu, index, &val) == -1)
  1382. return EMULATE_FAIL;
  1383. break;
  1384. case KVMPPC_VMX_COPY_HWORD:
  1385. if (kvmppc_get_vmx_hword(vcpu, index, &val) == -1)
  1386. return EMULATE_FAIL;
  1387. break;
  1388. case KVMPPC_VMX_COPY_BYTE:
  1389. if (kvmppc_get_vmx_byte(vcpu, index, &val) == -1)
  1390. return EMULATE_FAIL;
  1391. break;
  1392. default:
  1393. return EMULATE_FAIL;
  1394. }
  1395. emulated = kvmppc_handle_store(vcpu, val, bytes,
  1396. is_default_endian);
  1397. if (emulated != EMULATE_DONE)
  1398. break;
  1399. vcpu->arch.paddr_accessed += vcpu->run->mmio.len;
  1400. vcpu->arch.mmio_vmx_copy_nums--;
  1401. vcpu->arch.mmio_vmx_offset++;
  1402. }
  1403. return emulated;
  1404. }
  1405. static int kvmppc_emulate_mmio_vmx_loadstore(struct kvm_vcpu *vcpu)
  1406. {
  1407. struct kvm_run *run = vcpu->run;
  1408. enum emulation_result emulated = EMULATE_FAIL;
  1409. int r;
  1410. vcpu->arch.paddr_accessed += run->mmio.len;
  1411. if (!vcpu->mmio_is_write) {
  1412. emulated = kvmppc_handle_vmx_load(vcpu,
  1413. vcpu->arch.io_gpr, run->mmio.len, 1);
  1414. } else {
  1415. emulated = kvmppc_handle_vmx_store(vcpu,
  1416. vcpu->arch.io_gpr, run->mmio.len, 1);
  1417. }
  1418. switch (emulated) {
  1419. case EMULATE_DO_MMIO:
  1420. run->exit_reason = KVM_EXIT_MMIO;
  1421. r = RESUME_HOST;
  1422. break;
  1423. case EMULATE_FAIL:
  1424. pr_info("KVM: MMIO emulation failed (VMX repeat)\n");
  1425. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  1426. run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  1427. r = RESUME_HOST;
  1428. break;
  1429. default:
  1430. r = RESUME_GUEST;
  1431. break;
  1432. }
  1433. return r;
  1434. }
  1435. #endif /* CONFIG_ALTIVEC */
  1436. int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  1437. {
  1438. int r = 0;
  1439. union kvmppc_one_reg val;
  1440. int size;
  1441. size = one_reg_size(reg->id);
  1442. if (size > sizeof(val))
  1443. return -EINVAL;
  1444. r = kvmppc_get_one_reg(vcpu, reg->id, &val);
  1445. if (r == -EINVAL) {
  1446. r = 0;
  1447. switch (reg->id) {
  1448. #ifdef CONFIG_ALTIVEC
  1449. case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
  1450. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  1451. r = -ENXIO;
  1452. break;
  1453. }
  1454. kvmppc_get_vsx_vr(vcpu, reg->id - KVM_REG_PPC_VR0, &val.vval);
  1455. break;
  1456. case KVM_REG_PPC_VSCR:
  1457. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  1458. r = -ENXIO;
  1459. break;
  1460. }
  1461. val = get_reg_val(reg->id, kvmppc_get_vscr(vcpu));
  1462. break;
  1463. case KVM_REG_PPC_VRSAVE:
  1464. val = get_reg_val(reg->id, kvmppc_get_vrsave(vcpu));
  1465. break;
  1466. #endif /* CONFIG_ALTIVEC */
  1467. default:
  1468. r = -EINVAL;
  1469. break;
  1470. }
  1471. }
  1472. if (r)
  1473. return r;
  1474. if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size))
  1475. r = -EFAULT;
  1476. return r;
  1477. }
  1478. int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  1479. {
  1480. int r;
  1481. union kvmppc_one_reg val;
  1482. int size;
  1483. size = one_reg_size(reg->id);
  1484. if (size > sizeof(val))
  1485. return -EINVAL;
  1486. if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size))
  1487. return -EFAULT;
  1488. r = kvmppc_set_one_reg(vcpu, reg->id, &val);
  1489. if (r == -EINVAL) {
  1490. r = 0;
  1491. switch (reg->id) {
  1492. #ifdef CONFIG_ALTIVEC
  1493. case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
  1494. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  1495. r = -ENXIO;
  1496. break;
  1497. }
  1498. kvmppc_set_vsx_vr(vcpu, reg->id - KVM_REG_PPC_VR0, &val.vval);
  1499. break;
  1500. case KVM_REG_PPC_VSCR:
  1501. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  1502. r = -ENXIO;
  1503. break;
  1504. }
  1505. kvmppc_set_vscr(vcpu, set_reg_val(reg->id, val));
  1506. break;
  1507. case KVM_REG_PPC_VRSAVE:
  1508. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  1509. r = -ENXIO;
  1510. break;
  1511. }
  1512. kvmppc_set_vrsave(vcpu, set_reg_val(reg->id, val));
  1513. break;
  1514. #endif /* CONFIG_ALTIVEC */
  1515. default:
  1516. r = -EINVAL;
  1517. break;
  1518. }
  1519. }
  1520. return r;
  1521. }
  1522. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
  1523. {
  1524. struct kvm_run *run = vcpu->run;
  1525. int r;
  1526. vcpu_load(vcpu);
  1527. if (vcpu->mmio_needed) {
  1528. vcpu->mmio_needed = 0;
  1529. if (!vcpu->mmio_is_write)
  1530. kvmppc_complete_mmio_load(vcpu);
  1531. #ifdef CONFIG_VSX
  1532. if (vcpu->arch.mmio_vsx_copy_nums > 0) {
  1533. vcpu->arch.mmio_vsx_copy_nums--;
  1534. vcpu->arch.mmio_vsx_offset++;
  1535. }
  1536. if (vcpu->arch.mmio_vsx_copy_nums > 0) {
  1537. r = kvmppc_emulate_mmio_vsx_loadstore(vcpu);
  1538. if (r == RESUME_HOST) {
  1539. vcpu->mmio_needed = 1;
  1540. goto out;
  1541. }
  1542. }
  1543. #endif
  1544. #ifdef CONFIG_ALTIVEC
  1545. if (vcpu->arch.mmio_vmx_copy_nums > 0) {
  1546. vcpu->arch.mmio_vmx_copy_nums--;
  1547. vcpu->arch.mmio_vmx_offset++;
  1548. }
  1549. if (vcpu->arch.mmio_vmx_copy_nums > 0) {
  1550. r = kvmppc_emulate_mmio_vmx_loadstore(vcpu);
  1551. if (r == RESUME_HOST) {
  1552. vcpu->mmio_needed = 1;
  1553. goto out;
  1554. }
  1555. }
  1556. #endif
  1557. } else if (vcpu->arch.osi_needed) {
  1558. u64 *gprs = run->osi.gprs;
  1559. int i;
  1560. for (i = 0; i < 32; i++)
  1561. kvmppc_set_gpr(vcpu, i, gprs[i]);
  1562. vcpu->arch.osi_needed = 0;
  1563. } else if (vcpu->arch.hcall_needed) {
  1564. int i;
  1565. kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
  1566. for (i = 0; i < 9; ++i)
  1567. kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
  1568. vcpu->arch.hcall_needed = 0;
  1569. #ifdef CONFIG_BOOKE
  1570. } else if (vcpu->arch.epr_needed) {
  1571. kvmppc_set_epr(vcpu, run->epr.epr);
  1572. vcpu->arch.epr_needed = 0;
  1573. #endif
  1574. }
  1575. kvm_sigset_activate(vcpu);
  1576. if (!vcpu->wants_to_run)
  1577. r = -EINTR;
  1578. else
  1579. r = kvmppc_vcpu_run(vcpu);
  1580. kvm_sigset_deactivate(vcpu);
  1581. #ifdef CONFIG_ALTIVEC
  1582. out:
  1583. #endif
  1584. /*
  1585. * We're already returning to userspace, don't pass the
  1586. * RESUME_HOST flags along.
  1587. */
  1588. if (r > 0)
  1589. r = 0;
  1590. vcpu_put(vcpu);
  1591. return r;
  1592. }
  1593. int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
  1594. {
  1595. if (irq->irq == KVM_INTERRUPT_UNSET) {
  1596. kvmppc_core_dequeue_external(vcpu);
  1597. return 0;
  1598. }
  1599. kvmppc_core_queue_external(vcpu, irq);
  1600. kvm_vcpu_kick(vcpu);
  1601. return 0;
  1602. }
  1603. static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
  1604. struct kvm_enable_cap *cap)
  1605. {
  1606. int r;
  1607. if (cap->flags)
  1608. return -EINVAL;
  1609. switch (cap->cap) {
  1610. case KVM_CAP_PPC_OSI:
  1611. r = 0;
  1612. vcpu->arch.osi_enabled = true;
  1613. break;
  1614. case KVM_CAP_PPC_PAPR:
  1615. r = 0;
  1616. vcpu->arch.papr_enabled = true;
  1617. break;
  1618. case KVM_CAP_PPC_EPR:
  1619. r = 0;
  1620. if (cap->args[0])
  1621. vcpu->arch.epr_flags |= KVMPPC_EPR_USER;
  1622. else
  1623. vcpu->arch.epr_flags &= ~KVMPPC_EPR_USER;
  1624. break;
  1625. #ifdef CONFIG_BOOKE
  1626. case KVM_CAP_PPC_BOOKE_WATCHDOG:
  1627. r = 0;
  1628. vcpu->arch.watchdog_enabled = true;
  1629. break;
  1630. #endif
  1631. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  1632. case KVM_CAP_SW_TLB: {
  1633. struct kvm_config_tlb cfg;
  1634. void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
  1635. r = -EFAULT;
  1636. if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
  1637. break;
  1638. r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
  1639. break;
  1640. }
  1641. #endif
  1642. #ifdef CONFIG_KVM_MPIC
  1643. case KVM_CAP_IRQ_MPIC: {
  1644. struct fd f;
  1645. struct kvm_device *dev;
  1646. r = -EBADF;
  1647. f = fdget(cap->args[0]);
  1648. if (!fd_file(f))
  1649. break;
  1650. r = -EPERM;
  1651. dev = kvm_device_from_filp(fd_file(f));
  1652. if (dev)
  1653. r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]);
  1654. fdput(f);
  1655. break;
  1656. }
  1657. #endif
  1658. #ifdef CONFIG_KVM_XICS
  1659. case KVM_CAP_IRQ_XICS: {
  1660. struct fd f;
  1661. struct kvm_device *dev;
  1662. r = -EBADF;
  1663. f = fdget(cap->args[0]);
  1664. if (!fd_file(f))
  1665. break;
  1666. r = -EPERM;
  1667. dev = kvm_device_from_filp(fd_file(f));
  1668. if (dev) {
  1669. if (xics_on_xive())
  1670. r = kvmppc_xive_connect_vcpu(dev, vcpu, cap->args[1]);
  1671. else
  1672. r = kvmppc_xics_connect_vcpu(dev, vcpu, cap->args[1]);
  1673. }
  1674. fdput(f);
  1675. break;
  1676. }
  1677. #endif /* CONFIG_KVM_XICS */
  1678. #ifdef CONFIG_KVM_XIVE
  1679. case KVM_CAP_PPC_IRQ_XIVE: {
  1680. struct fd f;
  1681. struct kvm_device *dev;
  1682. r = -EBADF;
  1683. f = fdget(cap->args[0]);
  1684. if (!fd_file(f))
  1685. break;
  1686. r = -ENXIO;
  1687. if (!xive_enabled()) {
  1688. fdput(f);
  1689. break;
  1690. }
  1691. r = -EPERM;
  1692. dev = kvm_device_from_filp(fd_file(f));
  1693. if (dev)
  1694. r = kvmppc_xive_native_connect_vcpu(dev, vcpu,
  1695. cap->args[1]);
  1696. fdput(f);
  1697. break;
  1698. }
  1699. #endif /* CONFIG_KVM_XIVE */
  1700. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  1701. case KVM_CAP_PPC_FWNMI:
  1702. r = -EINVAL;
  1703. if (!is_kvmppc_hv_enabled(vcpu->kvm))
  1704. break;
  1705. r = 0;
  1706. vcpu->kvm->arch.fwnmi_enabled = true;
  1707. break;
  1708. #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
  1709. default:
  1710. r = -EINVAL;
  1711. break;
  1712. }
  1713. if (!r)
  1714. r = kvmppc_sanity_check(vcpu);
  1715. return r;
  1716. }
  1717. bool kvm_arch_intc_initialized(struct kvm *kvm)
  1718. {
  1719. #ifdef CONFIG_KVM_MPIC
  1720. if (kvm->arch.mpic)
  1721. return true;
  1722. #endif
  1723. #ifdef CONFIG_KVM_XICS
  1724. if (kvm->arch.xics || kvm->arch.xive)
  1725. return true;
  1726. #endif
  1727. return false;
  1728. }
  1729. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  1730. struct kvm_mp_state *mp_state)
  1731. {
  1732. return -EINVAL;
  1733. }
  1734. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  1735. struct kvm_mp_state *mp_state)
  1736. {
  1737. return -EINVAL;
  1738. }
  1739. long kvm_arch_vcpu_async_ioctl(struct file *filp,
  1740. unsigned int ioctl, unsigned long arg)
  1741. {
  1742. struct kvm_vcpu *vcpu = filp->private_data;
  1743. void __user *argp = (void __user *)arg;
  1744. if (ioctl == KVM_INTERRUPT) {
  1745. struct kvm_interrupt irq;
  1746. if (copy_from_user(&irq, argp, sizeof(irq)))
  1747. return -EFAULT;
  1748. return kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  1749. }
  1750. return -ENOIOCTLCMD;
  1751. }
  1752. long kvm_arch_vcpu_ioctl(struct file *filp,
  1753. unsigned int ioctl, unsigned long arg)
  1754. {
  1755. struct kvm_vcpu *vcpu = filp->private_data;
  1756. void __user *argp = (void __user *)arg;
  1757. long r;
  1758. switch (ioctl) {
  1759. case KVM_ENABLE_CAP:
  1760. {
  1761. struct kvm_enable_cap cap;
  1762. r = -EFAULT;
  1763. if (copy_from_user(&cap, argp, sizeof(cap)))
  1764. goto out;
  1765. vcpu_load(vcpu);
  1766. r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
  1767. vcpu_put(vcpu);
  1768. break;
  1769. }
  1770. case KVM_SET_ONE_REG:
  1771. case KVM_GET_ONE_REG:
  1772. {
  1773. struct kvm_one_reg reg;
  1774. r = -EFAULT;
  1775. if (copy_from_user(&reg, argp, sizeof(reg)))
  1776. goto out;
  1777. if (ioctl == KVM_SET_ONE_REG)
  1778. r = kvm_vcpu_ioctl_set_one_reg(vcpu, &reg);
  1779. else
  1780. r = kvm_vcpu_ioctl_get_one_reg(vcpu, &reg);
  1781. break;
  1782. }
  1783. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  1784. case KVM_DIRTY_TLB: {
  1785. struct kvm_dirty_tlb dirty;
  1786. r = -EFAULT;
  1787. if (copy_from_user(&dirty, argp, sizeof(dirty)))
  1788. goto out;
  1789. vcpu_load(vcpu);
  1790. r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
  1791. vcpu_put(vcpu);
  1792. break;
  1793. }
  1794. #endif
  1795. default:
  1796. r = -EINVAL;
  1797. }
  1798. out:
  1799. return r;
  1800. }
  1801. vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  1802. {
  1803. return VM_FAULT_SIGBUS;
  1804. }
  1805. static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
  1806. {
  1807. u32 inst_nop = 0x60000000;
  1808. #ifdef CONFIG_KVM_BOOKE_HV
  1809. u32 inst_sc1 = 0x44000022;
  1810. pvinfo->hcall[0] = cpu_to_be32(inst_sc1);
  1811. pvinfo->hcall[1] = cpu_to_be32(inst_nop);
  1812. pvinfo->hcall[2] = cpu_to_be32(inst_nop);
  1813. pvinfo->hcall[3] = cpu_to_be32(inst_nop);
  1814. #else
  1815. u32 inst_lis = 0x3c000000;
  1816. u32 inst_ori = 0x60000000;
  1817. u32 inst_sc = 0x44000002;
  1818. u32 inst_imm_mask = 0xffff;
  1819. /*
  1820. * The hypercall to get into KVM from within guest context is as
  1821. * follows:
  1822. *
  1823. * lis r0, r0, KVM_SC_MAGIC_R0@h
  1824. * ori r0, KVM_SC_MAGIC_R0@l
  1825. * sc
  1826. * nop
  1827. */
  1828. pvinfo->hcall[0] = cpu_to_be32(inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask));
  1829. pvinfo->hcall[1] = cpu_to_be32(inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask));
  1830. pvinfo->hcall[2] = cpu_to_be32(inst_sc);
  1831. pvinfo->hcall[3] = cpu_to_be32(inst_nop);
  1832. #endif
  1833. pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
  1834. return 0;
  1835. }
  1836. bool kvm_arch_irqchip_in_kernel(struct kvm *kvm)
  1837. {
  1838. int ret = 0;
  1839. #ifdef CONFIG_KVM_MPIC
  1840. ret = ret || (kvm->arch.mpic != NULL);
  1841. #endif
  1842. #ifdef CONFIG_KVM_XICS
  1843. ret = ret || (kvm->arch.xics != NULL);
  1844. ret = ret || (kvm->arch.xive != NULL);
  1845. #endif
  1846. smp_rmb();
  1847. return ret;
  1848. }
  1849. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
  1850. bool line_status)
  1851. {
  1852. if (!kvm_arch_irqchip_in_kernel(kvm))
  1853. return -ENXIO;
  1854. irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
  1855. irq_event->irq, irq_event->level,
  1856. line_status);
  1857. return 0;
  1858. }
  1859. int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  1860. struct kvm_enable_cap *cap)
  1861. {
  1862. int r;
  1863. if (cap->flags)
  1864. return -EINVAL;
  1865. switch (cap->cap) {
  1866. #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
  1867. case KVM_CAP_PPC_ENABLE_HCALL: {
  1868. unsigned long hcall = cap->args[0];
  1869. r = -EINVAL;
  1870. if (hcall > MAX_HCALL_OPCODE || (hcall & 3) ||
  1871. cap->args[1] > 1)
  1872. break;
  1873. if (!kvmppc_book3s_hcall_implemented(kvm, hcall))
  1874. break;
  1875. if (cap->args[1])
  1876. set_bit(hcall / 4, kvm->arch.enabled_hcalls);
  1877. else
  1878. clear_bit(hcall / 4, kvm->arch.enabled_hcalls);
  1879. r = 0;
  1880. break;
  1881. }
  1882. case KVM_CAP_PPC_SMT: {
  1883. unsigned long mode = cap->args[0];
  1884. unsigned long flags = cap->args[1];
  1885. r = -EINVAL;
  1886. if (kvm->arch.kvm_ops->set_smt_mode)
  1887. r = kvm->arch.kvm_ops->set_smt_mode(kvm, mode, flags);
  1888. break;
  1889. }
  1890. case KVM_CAP_PPC_NESTED_HV:
  1891. r = -EINVAL;
  1892. if (!is_kvmppc_hv_enabled(kvm) ||
  1893. !kvm->arch.kvm_ops->enable_nested)
  1894. break;
  1895. r = kvm->arch.kvm_ops->enable_nested(kvm);
  1896. break;
  1897. #endif
  1898. #if defined(CONFIG_KVM_BOOK3S_HV_POSSIBLE)
  1899. case KVM_CAP_PPC_SECURE_GUEST:
  1900. r = -EINVAL;
  1901. if (!is_kvmppc_hv_enabled(kvm) || !kvm->arch.kvm_ops->enable_svm)
  1902. break;
  1903. r = kvm->arch.kvm_ops->enable_svm(kvm);
  1904. break;
  1905. case KVM_CAP_PPC_DAWR1:
  1906. r = -EINVAL;
  1907. if (!is_kvmppc_hv_enabled(kvm) || !kvm->arch.kvm_ops->enable_dawr1)
  1908. break;
  1909. r = kvm->arch.kvm_ops->enable_dawr1(kvm);
  1910. break;
  1911. #endif
  1912. default:
  1913. r = -EINVAL;
  1914. break;
  1915. }
  1916. return r;
  1917. }
  1918. #ifdef CONFIG_PPC_BOOK3S_64
  1919. /*
  1920. * These functions check whether the underlying hardware is safe
  1921. * against attacks based on observing the effects of speculatively
  1922. * executed instructions, and whether it supplies instructions for
  1923. * use in workarounds. The information comes from firmware, either
  1924. * via the device tree on powernv platforms or from an hcall on
  1925. * pseries platforms.
  1926. */
  1927. #ifdef CONFIG_PPC_PSERIES
  1928. static int pseries_get_cpu_char(struct kvm_ppc_cpu_char *cp)
  1929. {
  1930. struct h_cpu_char_result c;
  1931. unsigned long rc;
  1932. if (!machine_is(pseries))
  1933. return -ENOTTY;
  1934. rc = plpar_get_cpu_characteristics(&c);
  1935. if (rc == H_SUCCESS) {
  1936. cp->character = c.character;
  1937. cp->behaviour = c.behaviour;
  1938. cp->character_mask = KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31 |
  1939. KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED |
  1940. KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30 |
  1941. KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2 |
  1942. KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV |
  1943. KVM_PPC_CPU_CHAR_BR_HINT_HONOURED |
  1944. KVM_PPC_CPU_CHAR_MTTRIG_THR_RECONF |
  1945. KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS |
  1946. KVM_PPC_CPU_CHAR_BCCTR_FLUSH_ASSIST;
  1947. cp->behaviour_mask = KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY |
  1948. KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR |
  1949. KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR |
  1950. KVM_PPC_CPU_BEHAV_FLUSH_COUNT_CACHE;
  1951. }
  1952. return 0;
  1953. }
  1954. #else
  1955. static int pseries_get_cpu_char(struct kvm_ppc_cpu_char *cp)
  1956. {
  1957. return -ENOTTY;
  1958. }
  1959. #endif
  1960. static inline bool have_fw_feat(struct device_node *fw_features,
  1961. const char *state, const char *name)
  1962. {
  1963. struct device_node *np;
  1964. bool r = false;
  1965. np = of_get_child_by_name(fw_features, name);
  1966. if (np) {
  1967. r = of_property_read_bool(np, state);
  1968. of_node_put(np);
  1969. }
  1970. return r;
  1971. }
  1972. static int kvmppc_get_cpu_char(struct kvm_ppc_cpu_char *cp)
  1973. {
  1974. struct device_node *np, *fw_features;
  1975. int r;
  1976. memset(cp, 0, sizeof(*cp));
  1977. r = pseries_get_cpu_char(cp);
  1978. if (r != -ENOTTY)
  1979. return r;
  1980. np = of_find_node_by_name(NULL, "ibm,opal");
  1981. if (np) {
  1982. fw_features = of_get_child_by_name(np, "fw-features");
  1983. of_node_put(np);
  1984. if (!fw_features)
  1985. return 0;
  1986. if (have_fw_feat(fw_features, "enabled",
  1987. "inst-spec-barrier-ori31,31,0"))
  1988. cp->character |= KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31;
  1989. if (have_fw_feat(fw_features, "enabled",
  1990. "fw-bcctrl-serialized"))
  1991. cp->character |= KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED;
  1992. if (have_fw_feat(fw_features, "enabled",
  1993. "inst-l1d-flush-ori30,30,0"))
  1994. cp->character |= KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30;
  1995. if (have_fw_feat(fw_features, "enabled",
  1996. "inst-l1d-flush-trig2"))
  1997. cp->character |= KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2;
  1998. if (have_fw_feat(fw_features, "enabled",
  1999. "fw-l1d-thread-split"))
  2000. cp->character |= KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV;
  2001. if (have_fw_feat(fw_features, "enabled",
  2002. "fw-count-cache-disabled"))
  2003. cp->character |= KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS;
  2004. if (have_fw_feat(fw_features, "enabled",
  2005. "fw-count-cache-flush-bcctr2,0,0"))
  2006. cp->character |= KVM_PPC_CPU_CHAR_BCCTR_FLUSH_ASSIST;
  2007. cp->character_mask = KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31 |
  2008. KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED |
  2009. KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30 |
  2010. KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2 |
  2011. KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV |
  2012. KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS |
  2013. KVM_PPC_CPU_CHAR_BCCTR_FLUSH_ASSIST;
  2014. if (have_fw_feat(fw_features, "enabled",
  2015. "speculation-policy-favor-security"))
  2016. cp->behaviour |= KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY;
  2017. if (!have_fw_feat(fw_features, "disabled",
  2018. "needs-l1d-flush-msr-pr-0-to-1"))
  2019. cp->behaviour |= KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR;
  2020. if (!have_fw_feat(fw_features, "disabled",
  2021. "needs-spec-barrier-for-bound-checks"))
  2022. cp->behaviour |= KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR;
  2023. if (have_fw_feat(fw_features, "enabled",
  2024. "needs-count-cache-flush-on-context-switch"))
  2025. cp->behaviour |= KVM_PPC_CPU_BEHAV_FLUSH_COUNT_CACHE;
  2026. cp->behaviour_mask = KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY |
  2027. KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR |
  2028. KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR |
  2029. KVM_PPC_CPU_BEHAV_FLUSH_COUNT_CACHE;
  2030. of_node_put(fw_features);
  2031. }
  2032. return 0;
  2033. }
  2034. #endif
  2035. int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
  2036. {
  2037. struct kvm *kvm __maybe_unused = filp->private_data;
  2038. void __user *argp = (void __user *)arg;
  2039. int r;
  2040. switch (ioctl) {
  2041. case KVM_PPC_GET_PVINFO: {
  2042. struct kvm_ppc_pvinfo pvinfo;
  2043. memset(&pvinfo, 0, sizeof(pvinfo));
  2044. r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
  2045. if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
  2046. r = -EFAULT;
  2047. goto out;
  2048. }
  2049. break;
  2050. }
  2051. #ifdef CONFIG_SPAPR_TCE_IOMMU
  2052. case KVM_CREATE_SPAPR_TCE_64: {
  2053. struct kvm_create_spapr_tce_64 create_tce_64;
  2054. r = -EFAULT;
  2055. if (copy_from_user(&create_tce_64, argp, sizeof(create_tce_64)))
  2056. goto out;
  2057. if (create_tce_64.flags) {
  2058. r = -EINVAL;
  2059. goto out;
  2060. }
  2061. r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
  2062. goto out;
  2063. }
  2064. case KVM_CREATE_SPAPR_TCE: {
  2065. struct kvm_create_spapr_tce create_tce;
  2066. struct kvm_create_spapr_tce_64 create_tce_64;
  2067. r = -EFAULT;
  2068. if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
  2069. goto out;
  2070. create_tce_64.liobn = create_tce.liobn;
  2071. create_tce_64.page_shift = IOMMU_PAGE_SHIFT_4K;
  2072. create_tce_64.offset = 0;
  2073. create_tce_64.size = create_tce.window_size >>
  2074. IOMMU_PAGE_SHIFT_4K;
  2075. create_tce_64.flags = 0;
  2076. r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
  2077. goto out;
  2078. }
  2079. #endif
  2080. #ifdef CONFIG_PPC_BOOK3S_64
  2081. case KVM_PPC_GET_SMMU_INFO: {
  2082. struct kvm_ppc_smmu_info info;
  2083. struct kvm *kvm = filp->private_data;
  2084. memset(&info, 0, sizeof(info));
  2085. r = kvm->arch.kvm_ops->get_smmu_info(kvm, &info);
  2086. if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
  2087. r = -EFAULT;
  2088. break;
  2089. }
  2090. case KVM_PPC_RTAS_DEFINE_TOKEN: {
  2091. struct kvm *kvm = filp->private_data;
  2092. r = kvm_vm_ioctl_rtas_define_token(kvm, argp);
  2093. break;
  2094. }
  2095. case KVM_PPC_CONFIGURE_V3_MMU: {
  2096. struct kvm *kvm = filp->private_data;
  2097. struct kvm_ppc_mmuv3_cfg cfg;
  2098. r = -EINVAL;
  2099. if (!kvm->arch.kvm_ops->configure_mmu)
  2100. goto out;
  2101. r = -EFAULT;
  2102. if (copy_from_user(&cfg, argp, sizeof(cfg)))
  2103. goto out;
  2104. r = kvm->arch.kvm_ops->configure_mmu(kvm, &cfg);
  2105. break;
  2106. }
  2107. case KVM_PPC_GET_RMMU_INFO: {
  2108. struct kvm *kvm = filp->private_data;
  2109. struct kvm_ppc_rmmu_info info;
  2110. r = -EINVAL;
  2111. if (!kvm->arch.kvm_ops->get_rmmu_info)
  2112. goto out;
  2113. r = kvm->arch.kvm_ops->get_rmmu_info(kvm, &info);
  2114. if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
  2115. r = -EFAULT;
  2116. break;
  2117. }
  2118. case KVM_PPC_GET_CPU_CHAR: {
  2119. struct kvm_ppc_cpu_char cpuchar;
  2120. r = kvmppc_get_cpu_char(&cpuchar);
  2121. if (r >= 0 && copy_to_user(argp, &cpuchar, sizeof(cpuchar)))
  2122. r = -EFAULT;
  2123. break;
  2124. }
  2125. case KVM_PPC_SVM_OFF: {
  2126. struct kvm *kvm = filp->private_data;
  2127. r = 0;
  2128. if (!kvm->arch.kvm_ops->svm_off)
  2129. goto out;
  2130. r = kvm->arch.kvm_ops->svm_off(kvm);
  2131. break;
  2132. }
  2133. default: {
  2134. struct kvm *kvm = filp->private_data;
  2135. r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg);
  2136. }
  2137. #else /* CONFIG_PPC_BOOK3S_64 */
  2138. default:
  2139. r = -ENOTTY;
  2140. #endif
  2141. }
  2142. out:
  2143. return r;
  2144. }
  2145. static DEFINE_IDA(lpid_inuse);
  2146. static unsigned long nr_lpids;
  2147. long kvmppc_alloc_lpid(void)
  2148. {
  2149. int lpid;
  2150. /* The host LPID must always be 0 (allocation starts at 1) */
  2151. lpid = ida_alloc_range(&lpid_inuse, 1, nr_lpids - 1, GFP_KERNEL);
  2152. if (lpid < 0) {
  2153. if (lpid == -ENOMEM)
  2154. pr_err("%s: Out of memory\n", __func__);
  2155. else
  2156. pr_err("%s: No LPIDs free\n", __func__);
  2157. return -ENOMEM;
  2158. }
  2159. return lpid;
  2160. }
  2161. EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid);
  2162. void kvmppc_free_lpid(long lpid)
  2163. {
  2164. ida_free(&lpid_inuse, lpid);
  2165. }
  2166. EXPORT_SYMBOL_GPL(kvmppc_free_lpid);
  2167. /* nr_lpids_param includes the host LPID */
  2168. void kvmppc_init_lpid(unsigned long nr_lpids_param)
  2169. {
  2170. nr_lpids = nr_lpids_param;
  2171. }
  2172. EXPORT_SYMBOL_GPL(kvmppc_init_lpid);
  2173. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ppc_instr);
  2174. void kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu, struct dentry *debugfs_dentry)
  2175. {
  2176. if (vcpu->kvm->arch.kvm_ops->create_vcpu_debugfs)
  2177. vcpu->kvm->arch.kvm_ops->create_vcpu_debugfs(vcpu, debugfs_dentry);
  2178. }
  2179. void kvm_arch_create_vm_debugfs(struct kvm *kvm)
  2180. {
  2181. if (kvm->arch.kvm_ops->create_vm_debugfs)
  2182. kvm->arch.kvm_ops->create_vm_debugfs(kvm);
  2183. }