powerpc.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547
  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. case KVM_CAP_SPAPR_TCE_64:
  471. r = 1;
  472. break;
  473. case KVM_CAP_SPAPR_TCE_VFIO:
  474. r = !!cpu_has_feature(CPU_FTR_HVMODE);
  475. break;
  476. case KVM_CAP_PPC_RTAS:
  477. case KVM_CAP_PPC_FIXUP_HCALL:
  478. case KVM_CAP_PPC_ENABLE_HCALL:
  479. #ifdef CONFIG_KVM_XICS
  480. case KVM_CAP_IRQ_XICS:
  481. #endif
  482. case KVM_CAP_PPC_GET_CPU_CHAR:
  483. r = 1;
  484. break;
  485. #ifdef CONFIG_KVM_XIVE
  486. case KVM_CAP_PPC_IRQ_XIVE:
  487. /*
  488. * We need XIVE to be enabled on the platform (implies
  489. * a POWER9 processor) and the PowerNV platform, as
  490. * nested is not yet supported.
  491. */
  492. r = xive_enabled() && !!cpu_has_feature(CPU_FTR_HVMODE) &&
  493. kvmppc_xive_native_supported();
  494. break;
  495. #endif
  496. #ifdef CONFIG_HAVE_KVM_IRQCHIP
  497. case KVM_CAP_IRQFD_RESAMPLE:
  498. r = !xive_enabled();
  499. break;
  500. #endif
  501. case KVM_CAP_PPC_ALLOC_HTAB:
  502. r = hv_enabled;
  503. break;
  504. #endif /* CONFIG_PPC_BOOK3S_64 */
  505. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  506. case KVM_CAP_PPC_SMT:
  507. r = 0;
  508. if (kvm) {
  509. if (kvm->arch.emul_smt_mode > 1)
  510. r = kvm->arch.emul_smt_mode;
  511. else
  512. r = kvm->arch.smt_mode;
  513. } else if (hv_enabled) {
  514. if (cpu_has_feature(CPU_FTR_ARCH_300))
  515. r = 1;
  516. else
  517. r = threads_per_subcore;
  518. }
  519. break;
  520. case KVM_CAP_PPC_SMT_POSSIBLE:
  521. r = 1;
  522. if (hv_enabled) {
  523. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  524. r = ((threads_per_subcore << 1) - 1);
  525. else
  526. /* P9 can emulate dbells, so allow any mode */
  527. r = 8 | 4 | 2 | 1;
  528. }
  529. break;
  530. case KVM_CAP_PPC_RMA:
  531. r = 0;
  532. break;
  533. case KVM_CAP_PPC_HWRNG:
  534. r = kvmppc_hwrng_present();
  535. break;
  536. case KVM_CAP_PPC_MMU_RADIX:
  537. r = !!(hv_enabled && radix_enabled());
  538. break;
  539. case KVM_CAP_PPC_MMU_HASH_V3:
  540. r = !!(hv_enabled && kvmppc_hv_ops->hash_v3_possible &&
  541. kvmppc_hv_ops->hash_v3_possible());
  542. break;
  543. case KVM_CAP_PPC_NESTED_HV:
  544. r = !!(hv_enabled && kvmppc_hv_ops->enable_nested &&
  545. !kvmppc_hv_ops->enable_nested(NULL));
  546. break;
  547. #endif
  548. case KVM_CAP_SYNC_MMU:
  549. BUILD_BUG_ON(!IS_ENABLED(CONFIG_KVM_GENERIC_MMU_NOTIFIER));
  550. r = 1;
  551. break;
  552. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  553. case KVM_CAP_PPC_HTAB_FD:
  554. r = hv_enabled;
  555. break;
  556. #endif
  557. case KVM_CAP_NR_VCPUS:
  558. /*
  559. * Recommending a number of CPUs is somewhat arbitrary; we
  560. * return the number of present CPUs for -HV (since a host
  561. * will have secondary threads "offline"), and for other KVM
  562. * implementations just count online CPUs.
  563. */
  564. if (hv_enabled)
  565. r = min_t(unsigned int, num_present_cpus(), KVM_MAX_VCPUS);
  566. else
  567. r = min_t(unsigned int, num_online_cpus(), KVM_MAX_VCPUS);
  568. break;
  569. case KVM_CAP_MAX_VCPUS:
  570. r = KVM_MAX_VCPUS;
  571. break;
  572. case KVM_CAP_MAX_VCPU_ID:
  573. r = KVM_MAX_VCPU_IDS;
  574. break;
  575. #ifdef CONFIG_PPC_BOOK3S_64
  576. case KVM_CAP_PPC_GET_SMMU_INFO:
  577. r = 1;
  578. break;
  579. case KVM_CAP_SPAPR_MULTITCE:
  580. r = 1;
  581. break;
  582. case KVM_CAP_SPAPR_RESIZE_HPT:
  583. r = !!hv_enabled;
  584. break;
  585. #endif
  586. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  587. case KVM_CAP_PPC_FWNMI:
  588. r = hv_enabled;
  589. break;
  590. #endif
  591. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  592. case KVM_CAP_PPC_HTM:
  593. r = !!(cur_cpu_spec->cpu_user_features2 & PPC_FEATURE2_HTM) ||
  594. (hv_enabled && cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST));
  595. break;
  596. #endif
  597. #if defined(CONFIG_KVM_BOOK3S_HV_POSSIBLE)
  598. case KVM_CAP_PPC_SECURE_GUEST:
  599. r = hv_enabled && kvmppc_hv_ops->enable_svm &&
  600. !kvmppc_hv_ops->enable_svm(NULL);
  601. break;
  602. case KVM_CAP_PPC_DAWR1:
  603. r = !!(hv_enabled && kvmppc_hv_ops->enable_dawr1 &&
  604. !kvmppc_hv_ops->enable_dawr1(NULL));
  605. break;
  606. case KVM_CAP_PPC_RPT_INVALIDATE:
  607. r = 1;
  608. break;
  609. #endif
  610. case KVM_CAP_PPC_AIL_MODE_3:
  611. r = 0;
  612. /*
  613. * KVM PR, POWER7, and some POWER9s don't support AIL=3 mode.
  614. * The POWER9s can support it if the guest runs in hash mode,
  615. * but QEMU doesn't necessarily query the capability in time.
  616. */
  617. if (hv_enabled) {
  618. if (kvmhv_on_pseries()) {
  619. if (pseries_reloc_on_exception())
  620. r = 1;
  621. } else if (cpu_has_feature(CPU_FTR_ARCH_207S) &&
  622. !cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG)) {
  623. r = 1;
  624. }
  625. }
  626. break;
  627. default:
  628. r = 0;
  629. break;
  630. }
  631. return r;
  632. }
  633. long kvm_arch_dev_ioctl(struct file *filp,
  634. unsigned int ioctl, unsigned long arg)
  635. {
  636. return -EINVAL;
  637. }
  638. void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
  639. {
  640. kvmppc_core_free_memslot(kvm, slot);
  641. }
  642. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  643. const struct kvm_memory_slot *old,
  644. struct kvm_memory_slot *new,
  645. enum kvm_mr_change change)
  646. {
  647. return kvmppc_core_prepare_memory_region(kvm, old, new, change);
  648. }
  649. void kvm_arch_commit_memory_region(struct kvm *kvm,
  650. struct kvm_memory_slot *old,
  651. const struct kvm_memory_slot *new,
  652. enum kvm_mr_change change)
  653. {
  654. kvmppc_core_commit_memory_region(kvm, old, new, change);
  655. }
  656. void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
  657. struct kvm_memory_slot *slot)
  658. {
  659. kvmppc_core_flush_memslot(kvm, slot);
  660. }
  661. int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
  662. {
  663. return 0;
  664. }
  665. static enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
  666. {
  667. struct kvm_vcpu *vcpu;
  668. vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
  669. kvmppc_decrementer_func(vcpu);
  670. return HRTIMER_NORESTART;
  671. }
  672. int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
  673. {
  674. int err;
  675. hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  676. vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
  677. #ifdef CONFIG_KVM_EXIT_TIMING
  678. mutex_init(&vcpu->arch.exit_timing_lock);
  679. #endif
  680. err = kvmppc_subarch_vcpu_init(vcpu);
  681. if (err)
  682. return err;
  683. err = kvmppc_core_vcpu_create(vcpu);
  684. if (err)
  685. goto out_vcpu_uninit;
  686. rcuwait_init(&vcpu->arch.wait);
  687. vcpu->arch.waitp = &vcpu->arch.wait;
  688. return 0;
  689. out_vcpu_uninit:
  690. kvmppc_subarch_vcpu_uninit(vcpu);
  691. return err;
  692. }
  693. void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
  694. {
  695. }
  696. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  697. {
  698. /* Make sure we're not using the vcpu anymore */
  699. hrtimer_cancel(&vcpu->arch.dec_timer);
  700. switch (vcpu->arch.irq_type) {
  701. case KVMPPC_IRQ_MPIC:
  702. kvmppc_mpic_disconnect_vcpu(vcpu->arch.mpic, vcpu);
  703. break;
  704. case KVMPPC_IRQ_XICS:
  705. if (xics_on_xive())
  706. kvmppc_xive_cleanup_vcpu(vcpu);
  707. else
  708. kvmppc_xics_free_icp(vcpu);
  709. break;
  710. case KVMPPC_IRQ_XIVE:
  711. kvmppc_xive_native_cleanup_vcpu(vcpu);
  712. break;
  713. }
  714. kvmppc_core_vcpu_free(vcpu);
  715. kvmppc_subarch_vcpu_uninit(vcpu);
  716. }
  717. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  718. {
  719. return kvmppc_core_pending_dec(vcpu);
  720. }
  721. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  722. {
  723. #ifdef CONFIG_BOOKE
  724. /*
  725. * vrsave (formerly usprg0) isn't used by Linux, but may
  726. * be used by the guest.
  727. *
  728. * On non-booke this is associated with Altivec and
  729. * is handled by code in book3s.c.
  730. */
  731. mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
  732. #endif
  733. kvmppc_core_vcpu_load(vcpu, cpu);
  734. }
  735. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  736. {
  737. kvmppc_core_vcpu_put(vcpu);
  738. #ifdef CONFIG_BOOKE
  739. vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
  740. #endif
  741. }
  742. /*
  743. * irq_bypass_add_producer and irq_bypass_del_producer are only
  744. * useful if the architecture supports PCI passthrough.
  745. * irq_bypass_stop and irq_bypass_start are not needed and so
  746. * kvm_ops are not defined for them.
  747. */
  748. bool kvm_arch_has_irq_bypass(void)
  749. {
  750. return ((kvmppc_hv_ops && kvmppc_hv_ops->irq_bypass_add_producer) ||
  751. (kvmppc_pr_ops && kvmppc_pr_ops->irq_bypass_add_producer));
  752. }
  753. int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
  754. struct irq_bypass_producer *prod)
  755. {
  756. struct kvm_kernel_irqfd *irqfd =
  757. container_of(cons, struct kvm_kernel_irqfd, consumer);
  758. struct kvm *kvm = irqfd->kvm;
  759. if (kvm->arch.kvm_ops->irq_bypass_add_producer)
  760. return kvm->arch.kvm_ops->irq_bypass_add_producer(cons, prod);
  761. return 0;
  762. }
  763. void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
  764. struct irq_bypass_producer *prod)
  765. {
  766. struct kvm_kernel_irqfd *irqfd =
  767. container_of(cons, struct kvm_kernel_irqfd, consumer);
  768. struct kvm *kvm = irqfd->kvm;
  769. if (kvm->arch.kvm_ops->irq_bypass_del_producer)
  770. kvm->arch.kvm_ops->irq_bypass_del_producer(cons, prod);
  771. }
  772. #ifdef CONFIG_VSX
  773. static inline int kvmppc_get_vsr_dword_offset(int index)
  774. {
  775. int offset;
  776. if ((index != 0) && (index != 1))
  777. return -1;
  778. #ifdef __BIG_ENDIAN
  779. offset = index;
  780. #else
  781. offset = 1 - index;
  782. #endif
  783. return offset;
  784. }
  785. static inline int kvmppc_get_vsr_word_offset(int index)
  786. {
  787. int offset;
  788. if ((index > 3) || (index < 0))
  789. return -1;
  790. #ifdef __BIG_ENDIAN
  791. offset = index;
  792. #else
  793. offset = 3 - index;
  794. #endif
  795. return offset;
  796. }
  797. static inline void kvmppc_set_vsr_dword(struct kvm_vcpu *vcpu,
  798. u64 gpr)
  799. {
  800. union kvmppc_one_reg val;
  801. int offset = kvmppc_get_vsr_dword_offset(vcpu->arch.mmio_vsx_offset);
  802. int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
  803. if (offset == -1)
  804. return;
  805. if (index >= 32) {
  806. kvmppc_get_vsx_vr(vcpu, index - 32, &val.vval);
  807. val.vsxval[offset] = gpr;
  808. kvmppc_set_vsx_vr(vcpu, index - 32, &val.vval);
  809. } else {
  810. kvmppc_set_vsx_fpr(vcpu, index, offset, gpr);
  811. }
  812. }
  813. static inline void kvmppc_set_vsr_dword_dump(struct kvm_vcpu *vcpu,
  814. u64 gpr)
  815. {
  816. union kvmppc_one_reg val;
  817. int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
  818. if (index >= 32) {
  819. kvmppc_get_vsx_vr(vcpu, index - 32, &val.vval);
  820. val.vsxval[0] = gpr;
  821. val.vsxval[1] = gpr;
  822. kvmppc_set_vsx_vr(vcpu, index - 32, &val.vval);
  823. } else {
  824. kvmppc_set_vsx_fpr(vcpu, index, 0, gpr);
  825. kvmppc_set_vsx_fpr(vcpu, index, 1, gpr);
  826. }
  827. }
  828. static inline void kvmppc_set_vsr_word_dump(struct kvm_vcpu *vcpu,
  829. u32 gpr)
  830. {
  831. union kvmppc_one_reg val;
  832. int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
  833. if (index >= 32) {
  834. val.vsx32val[0] = gpr;
  835. val.vsx32val[1] = gpr;
  836. val.vsx32val[2] = gpr;
  837. val.vsx32val[3] = gpr;
  838. kvmppc_set_vsx_vr(vcpu, index - 32, &val.vval);
  839. } else {
  840. val.vsx32val[0] = gpr;
  841. val.vsx32val[1] = gpr;
  842. kvmppc_set_vsx_fpr(vcpu, index, 0, val.vsxval[0]);
  843. kvmppc_set_vsx_fpr(vcpu, index, 1, val.vsxval[0]);
  844. }
  845. }
  846. static inline void kvmppc_set_vsr_word(struct kvm_vcpu *vcpu,
  847. u32 gpr32)
  848. {
  849. union kvmppc_one_reg val;
  850. int offset = kvmppc_get_vsr_word_offset(vcpu->arch.mmio_vsx_offset);
  851. int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
  852. int dword_offset, word_offset;
  853. if (offset == -1)
  854. return;
  855. if (index >= 32) {
  856. kvmppc_get_vsx_vr(vcpu, index - 32, &val.vval);
  857. val.vsx32val[offset] = gpr32;
  858. kvmppc_set_vsx_vr(vcpu, index - 32, &val.vval);
  859. } else {
  860. dword_offset = offset / 2;
  861. word_offset = offset % 2;
  862. val.vsxval[0] = kvmppc_get_vsx_fpr(vcpu, index, dword_offset);
  863. val.vsx32val[word_offset] = gpr32;
  864. kvmppc_set_vsx_fpr(vcpu, index, dword_offset, val.vsxval[0]);
  865. }
  866. }
  867. #endif /* CONFIG_VSX */
  868. #ifdef CONFIG_ALTIVEC
  869. static inline int kvmppc_get_vmx_offset_generic(struct kvm_vcpu *vcpu,
  870. int index, int element_size)
  871. {
  872. int offset;
  873. int elts = sizeof(vector128)/element_size;
  874. if ((index < 0) || (index >= elts))
  875. return -1;
  876. if (kvmppc_need_byteswap(vcpu))
  877. offset = elts - index - 1;
  878. else
  879. offset = index;
  880. return offset;
  881. }
  882. static inline int kvmppc_get_vmx_dword_offset(struct kvm_vcpu *vcpu,
  883. int index)
  884. {
  885. return kvmppc_get_vmx_offset_generic(vcpu, index, 8);
  886. }
  887. static inline int kvmppc_get_vmx_word_offset(struct kvm_vcpu *vcpu,
  888. int index)
  889. {
  890. return kvmppc_get_vmx_offset_generic(vcpu, index, 4);
  891. }
  892. static inline int kvmppc_get_vmx_hword_offset(struct kvm_vcpu *vcpu,
  893. int index)
  894. {
  895. return kvmppc_get_vmx_offset_generic(vcpu, index, 2);
  896. }
  897. static inline int kvmppc_get_vmx_byte_offset(struct kvm_vcpu *vcpu,
  898. int index)
  899. {
  900. return kvmppc_get_vmx_offset_generic(vcpu, index, 1);
  901. }
  902. static inline void kvmppc_set_vmx_dword(struct kvm_vcpu *vcpu,
  903. u64 gpr)
  904. {
  905. union kvmppc_one_reg val;
  906. int offset = kvmppc_get_vmx_dword_offset(vcpu,
  907. vcpu->arch.mmio_vmx_offset);
  908. int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
  909. if (offset == -1)
  910. return;
  911. kvmppc_get_vsx_vr(vcpu, index, &val.vval);
  912. val.vsxval[offset] = gpr;
  913. kvmppc_set_vsx_vr(vcpu, index, &val.vval);
  914. }
  915. static inline void kvmppc_set_vmx_word(struct kvm_vcpu *vcpu,
  916. u32 gpr32)
  917. {
  918. union kvmppc_one_reg val;
  919. int offset = kvmppc_get_vmx_word_offset(vcpu,
  920. vcpu->arch.mmio_vmx_offset);
  921. int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
  922. if (offset == -1)
  923. return;
  924. kvmppc_get_vsx_vr(vcpu, index, &val.vval);
  925. val.vsx32val[offset] = gpr32;
  926. kvmppc_set_vsx_vr(vcpu, index, &val.vval);
  927. }
  928. static inline void kvmppc_set_vmx_hword(struct kvm_vcpu *vcpu,
  929. u16 gpr16)
  930. {
  931. union kvmppc_one_reg val;
  932. int offset = kvmppc_get_vmx_hword_offset(vcpu,
  933. vcpu->arch.mmio_vmx_offset);
  934. int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
  935. if (offset == -1)
  936. return;
  937. kvmppc_get_vsx_vr(vcpu, index, &val.vval);
  938. val.vsx16val[offset] = gpr16;
  939. kvmppc_set_vsx_vr(vcpu, index, &val.vval);
  940. }
  941. static inline void kvmppc_set_vmx_byte(struct kvm_vcpu *vcpu,
  942. u8 gpr8)
  943. {
  944. union kvmppc_one_reg val;
  945. int offset = kvmppc_get_vmx_byte_offset(vcpu,
  946. vcpu->arch.mmio_vmx_offset);
  947. int index = vcpu->arch.io_gpr & KVM_MMIO_REG_MASK;
  948. if (offset == -1)
  949. return;
  950. kvmppc_get_vsx_vr(vcpu, index, &val.vval);
  951. val.vsx8val[offset] = gpr8;
  952. kvmppc_set_vsx_vr(vcpu, index, &val.vval);
  953. }
  954. #endif /* CONFIG_ALTIVEC */
  955. #ifdef CONFIG_PPC_FPU
  956. static inline u64 sp_to_dp(u32 fprs)
  957. {
  958. u64 fprd;
  959. preempt_disable();
  960. enable_kernel_fp();
  961. asm ("lfs%U1%X1 0,%1; stfd%U0%X0 0,%0" : "=m<>" (fprd) : "m<>" (fprs)
  962. : "fr0");
  963. preempt_enable();
  964. return fprd;
  965. }
  966. static inline u32 dp_to_sp(u64 fprd)
  967. {
  968. u32 fprs;
  969. preempt_disable();
  970. enable_kernel_fp();
  971. asm ("lfd%U1%X1 0,%1; stfs%U0%X0 0,%0" : "=m<>" (fprs) : "m<>" (fprd)
  972. : "fr0");
  973. preempt_enable();
  974. return fprs;
  975. }
  976. #else
  977. #define sp_to_dp(x) (x)
  978. #define dp_to_sp(x) (x)
  979. #endif /* CONFIG_PPC_FPU */
  980. static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu)
  981. {
  982. struct kvm_run *run = vcpu->run;
  983. u64 gpr;
  984. if (run->mmio.len > sizeof(gpr))
  985. return;
  986. if (!vcpu->arch.mmio_host_swabbed) {
  987. switch (run->mmio.len) {
  988. case 8: gpr = *(u64 *)run->mmio.data; break;
  989. case 4: gpr = *(u32 *)run->mmio.data; break;
  990. case 2: gpr = *(u16 *)run->mmio.data; break;
  991. case 1: gpr = *(u8 *)run->mmio.data; break;
  992. }
  993. } else {
  994. switch (run->mmio.len) {
  995. case 8: gpr = swab64(*(u64 *)run->mmio.data); break;
  996. case 4: gpr = swab32(*(u32 *)run->mmio.data); break;
  997. case 2: gpr = swab16(*(u16 *)run->mmio.data); break;
  998. case 1: gpr = *(u8 *)run->mmio.data; break;
  999. }
  1000. }
  1001. /* conversion between single and double precision */
  1002. if ((vcpu->arch.mmio_sp64_extend) && (run->mmio.len == 4))
  1003. gpr = sp_to_dp(gpr);
  1004. if (vcpu->arch.mmio_sign_extend) {
  1005. switch (run->mmio.len) {
  1006. #ifdef CONFIG_PPC64
  1007. case 4:
  1008. gpr = (s64)(s32)gpr;
  1009. break;
  1010. #endif
  1011. case 2:
  1012. gpr = (s64)(s16)gpr;
  1013. break;
  1014. case 1:
  1015. gpr = (s64)(s8)gpr;
  1016. break;
  1017. }
  1018. }
  1019. switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
  1020. case KVM_MMIO_REG_GPR:
  1021. kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
  1022. break;
  1023. case KVM_MMIO_REG_FPR:
  1024. if (vcpu->kvm->arch.kvm_ops->giveup_ext)
  1025. vcpu->kvm->arch.kvm_ops->giveup_ext(vcpu, MSR_FP);
  1026. kvmppc_set_fpr(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK, gpr);
  1027. break;
  1028. #ifdef CONFIG_PPC_BOOK3S
  1029. case KVM_MMIO_REG_QPR:
  1030. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  1031. break;
  1032. case KVM_MMIO_REG_FQPR:
  1033. kvmppc_set_fpr(vcpu, vcpu->arch.io_gpr & KVM_MMIO_REG_MASK, gpr);
  1034. vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
  1035. break;
  1036. #endif
  1037. #ifdef CONFIG_VSX
  1038. case KVM_MMIO_REG_VSX:
  1039. if (vcpu->kvm->arch.kvm_ops->giveup_ext)
  1040. vcpu->kvm->arch.kvm_ops->giveup_ext(vcpu, MSR_VSX);
  1041. if (vcpu->arch.mmio_copy_type == KVMPPC_VSX_COPY_DWORD)
  1042. kvmppc_set_vsr_dword(vcpu, gpr);
  1043. else if (vcpu->arch.mmio_copy_type == KVMPPC_VSX_COPY_WORD)
  1044. kvmppc_set_vsr_word(vcpu, gpr);
  1045. else if (vcpu->arch.mmio_copy_type ==
  1046. KVMPPC_VSX_COPY_DWORD_LOAD_DUMP)
  1047. kvmppc_set_vsr_dword_dump(vcpu, gpr);
  1048. else if (vcpu->arch.mmio_copy_type ==
  1049. KVMPPC_VSX_COPY_WORD_LOAD_DUMP)
  1050. kvmppc_set_vsr_word_dump(vcpu, gpr);
  1051. break;
  1052. #endif
  1053. #ifdef CONFIG_ALTIVEC
  1054. case KVM_MMIO_REG_VMX:
  1055. if (vcpu->kvm->arch.kvm_ops->giveup_ext)
  1056. vcpu->kvm->arch.kvm_ops->giveup_ext(vcpu, MSR_VEC);
  1057. if (vcpu->arch.mmio_copy_type == KVMPPC_VMX_COPY_DWORD)
  1058. kvmppc_set_vmx_dword(vcpu, gpr);
  1059. else if (vcpu->arch.mmio_copy_type == KVMPPC_VMX_COPY_WORD)
  1060. kvmppc_set_vmx_word(vcpu, gpr);
  1061. else if (vcpu->arch.mmio_copy_type ==
  1062. KVMPPC_VMX_COPY_HWORD)
  1063. kvmppc_set_vmx_hword(vcpu, gpr);
  1064. else if (vcpu->arch.mmio_copy_type ==
  1065. KVMPPC_VMX_COPY_BYTE)
  1066. kvmppc_set_vmx_byte(vcpu, gpr);
  1067. break;
  1068. #endif
  1069. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  1070. case KVM_MMIO_REG_NESTED_GPR:
  1071. if (kvmppc_need_byteswap(vcpu))
  1072. gpr = swab64(gpr);
  1073. kvm_vcpu_write_guest(vcpu, vcpu->arch.nested_io_gpr, &gpr,
  1074. sizeof(gpr));
  1075. break;
  1076. #endif
  1077. default:
  1078. BUG();
  1079. }
  1080. }
  1081. static int __kvmppc_handle_load(struct kvm_vcpu *vcpu,
  1082. unsigned int rt, unsigned int bytes,
  1083. int is_default_endian, int sign_extend)
  1084. {
  1085. struct kvm_run *run = vcpu->run;
  1086. int idx, ret;
  1087. bool host_swabbed;
  1088. /* Pity C doesn't have a logical XOR operator */
  1089. if (kvmppc_need_byteswap(vcpu)) {
  1090. host_swabbed = is_default_endian;
  1091. } else {
  1092. host_swabbed = !is_default_endian;
  1093. }
  1094. if (bytes > sizeof(run->mmio.data))
  1095. return EMULATE_FAIL;
  1096. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  1097. run->mmio.len = bytes;
  1098. run->mmio.is_write = 0;
  1099. vcpu->arch.io_gpr = rt;
  1100. vcpu->arch.mmio_host_swabbed = host_swabbed;
  1101. vcpu->mmio_needed = 1;
  1102. vcpu->mmio_is_write = 0;
  1103. vcpu->arch.mmio_sign_extend = sign_extend;
  1104. idx = srcu_read_lock(&vcpu->kvm->srcu);
  1105. ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
  1106. bytes, &run->mmio.data);
  1107. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  1108. if (!ret) {
  1109. kvmppc_complete_mmio_load(vcpu);
  1110. vcpu->mmio_needed = 0;
  1111. return EMULATE_DONE;
  1112. }
  1113. return EMULATE_DO_MMIO;
  1114. }
  1115. int kvmppc_handle_load(struct kvm_vcpu *vcpu,
  1116. unsigned int rt, unsigned int bytes,
  1117. int is_default_endian)
  1118. {
  1119. return __kvmppc_handle_load(vcpu, rt, bytes, is_default_endian, 0);
  1120. }
  1121. EXPORT_SYMBOL_GPL(kvmppc_handle_load);
  1122. /* Same as above, but sign extends */
  1123. int kvmppc_handle_loads(struct kvm_vcpu *vcpu,
  1124. unsigned int rt, unsigned int bytes,
  1125. int is_default_endian)
  1126. {
  1127. return __kvmppc_handle_load(vcpu, rt, bytes, is_default_endian, 1);
  1128. }
  1129. #ifdef CONFIG_VSX
  1130. int kvmppc_handle_vsx_load(struct kvm_vcpu *vcpu,
  1131. unsigned int rt, unsigned int bytes,
  1132. int is_default_endian, int mmio_sign_extend)
  1133. {
  1134. enum emulation_result emulated = EMULATE_DONE;
  1135. /* Currently, mmio_vsx_copy_nums only allowed to be 4 or less */
  1136. if (vcpu->arch.mmio_vsx_copy_nums > 4)
  1137. return EMULATE_FAIL;
  1138. while (vcpu->arch.mmio_vsx_copy_nums) {
  1139. emulated = __kvmppc_handle_load(vcpu, rt, bytes,
  1140. is_default_endian, mmio_sign_extend);
  1141. if (emulated != EMULATE_DONE)
  1142. break;
  1143. vcpu->arch.paddr_accessed += vcpu->run->mmio.len;
  1144. vcpu->arch.mmio_vsx_copy_nums--;
  1145. vcpu->arch.mmio_vsx_offset++;
  1146. }
  1147. return emulated;
  1148. }
  1149. #endif /* CONFIG_VSX */
  1150. int kvmppc_handle_store(struct kvm_vcpu *vcpu,
  1151. u64 val, unsigned int bytes, int is_default_endian)
  1152. {
  1153. struct kvm_run *run = vcpu->run;
  1154. void *data = run->mmio.data;
  1155. int idx, ret;
  1156. bool host_swabbed;
  1157. /* Pity C doesn't have a logical XOR operator */
  1158. if (kvmppc_need_byteswap(vcpu)) {
  1159. host_swabbed = is_default_endian;
  1160. } else {
  1161. host_swabbed = !is_default_endian;
  1162. }
  1163. if (bytes > sizeof(run->mmio.data))
  1164. return EMULATE_FAIL;
  1165. run->mmio.phys_addr = vcpu->arch.paddr_accessed;
  1166. run->mmio.len = bytes;
  1167. run->mmio.is_write = 1;
  1168. vcpu->mmio_needed = 1;
  1169. vcpu->mmio_is_write = 1;
  1170. if ((vcpu->arch.mmio_sp64_extend) && (bytes == 4))
  1171. val = dp_to_sp(val);
  1172. /* Store the value at the lowest bytes in 'data'. */
  1173. if (!host_swabbed) {
  1174. switch (bytes) {
  1175. case 8: *(u64 *)data = val; break;
  1176. case 4: *(u32 *)data = val; break;
  1177. case 2: *(u16 *)data = val; break;
  1178. case 1: *(u8 *)data = val; break;
  1179. }
  1180. } else {
  1181. switch (bytes) {
  1182. case 8: *(u64 *)data = swab64(val); break;
  1183. case 4: *(u32 *)data = swab32(val); break;
  1184. case 2: *(u16 *)data = swab16(val); break;
  1185. case 1: *(u8 *)data = val; break;
  1186. }
  1187. }
  1188. idx = srcu_read_lock(&vcpu->kvm->srcu);
  1189. ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, run->mmio.phys_addr,
  1190. bytes, &run->mmio.data);
  1191. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  1192. if (!ret) {
  1193. vcpu->mmio_needed = 0;
  1194. return EMULATE_DONE;
  1195. }
  1196. return EMULATE_DO_MMIO;
  1197. }
  1198. EXPORT_SYMBOL_GPL(kvmppc_handle_store);
  1199. #ifdef CONFIG_VSX
  1200. static inline int kvmppc_get_vsr_data(struct kvm_vcpu *vcpu, int rs, u64 *val)
  1201. {
  1202. u32 dword_offset, word_offset;
  1203. union kvmppc_one_reg reg;
  1204. int vsx_offset = 0;
  1205. int copy_type = vcpu->arch.mmio_copy_type;
  1206. int result = 0;
  1207. switch (copy_type) {
  1208. case KVMPPC_VSX_COPY_DWORD:
  1209. vsx_offset =
  1210. kvmppc_get_vsr_dword_offset(vcpu->arch.mmio_vsx_offset);
  1211. if (vsx_offset == -1) {
  1212. result = -1;
  1213. break;
  1214. }
  1215. if (rs < 32) {
  1216. *val = kvmppc_get_vsx_fpr(vcpu, rs, vsx_offset);
  1217. } else {
  1218. kvmppc_get_vsx_vr(vcpu, rs - 32, &reg.vval);
  1219. *val = reg.vsxval[vsx_offset];
  1220. }
  1221. break;
  1222. case KVMPPC_VSX_COPY_WORD:
  1223. vsx_offset =
  1224. kvmppc_get_vsr_word_offset(vcpu->arch.mmio_vsx_offset);
  1225. if (vsx_offset == -1) {
  1226. result = -1;
  1227. break;
  1228. }
  1229. if (rs < 32) {
  1230. dword_offset = vsx_offset / 2;
  1231. word_offset = vsx_offset % 2;
  1232. reg.vsxval[0] = kvmppc_get_vsx_fpr(vcpu, rs, dword_offset);
  1233. *val = reg.vsx32val[word_offset];
  1234. } else {
  1235. kvmppc_get_vsx_vr(vcpu, rs - 32, &reg.vval);
  1236. *val = reg.vsx32val[vsx_offset];
  1237. }
  1238. break;
  1239. default:
  1240. result = -1;
  1241. break;
  1242. }
  1243. return result;
  1244. }
  1245. int kvmppc_handle_vsx_store(struct kvm_vcpu *vcpu,
  1246. int rs, unsigned int bytes, int is_default_endian)
  1247. {
  1248. u64 val;
  1249. enum emulation_result emulated = EMULATE_DONE;
  1250. vcpu->arch.io_gpr = rs;
  1251. /* Currently, mmio_vsx_copy_nums only allowed to be 4 or less */
  1252. if (vcpu->arch.mmio_vsx_copy_nums > 4)
  1253. return EMULATE_FAIL;
  1254. while (vcpu->arch.mmio_vsx_copy_nums) {
  1255. if (kvmppc_get_vsr_data(vcpu, rs, &val) == -1)
  1256. return EMULATE_FAIL;
  1257. emulated = kvmppc_handle_store(vcpu,
  1258. val, bytes, is_default_endian);
  1259. if (emulated != EMULATE_DONE)
  1260. break;
  1261. vcpu->arch.paddr_accessed += vcpu->run->mmio.len;
  1262. vcpu->arch.mmio_vsx_copy_nums--;
  1263. vcpu->arch.mmio_vsx_offset++;
  1264. }
  1265. return emulated;
  1266. }
  1267. static int kvmppc_emulate_mmio_vsx_loadstore(struct kvm_vcpu *vcpu)
  1268. {
  1269. struct kvm_run *run = vcpu->run;
  1270. enum emulation_result emulated = EMULATE_FAIL;
  1271. int r;
  1272. vcpu->arch.paddr_accessed += run->mmio.len;
  1273. if (!vcpu->mmio_is_write) {
  1274. emulated = kvmppc_handle_vsx_load(vcpu, vcpu->arch.io_gpr,
  1275. run->mmio.len, 1, vcpu->arch.mmio_sign_extend);
  1276. } else {
  1277. emulated = kvmppc_handle_vsx_store(vcpu,
  1278. vcpu->arch.io_gpr, run->mmio.len, 1);
  1279. }
  1280. switch (emulated) {
  1281. case EMULATE_DO_MMIO:
  1282. run->exit_reason = KVM_EXIT_MMIO;
  1283. r = RESUME_HOST;
  1284. break;
  1285. case EMULATE_FAIL:
  1286. pr_info("KVM: MMIO emulation failed (VSX repeat)\n");
  1287. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  1288. run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  1289. r = RESUME_HOST;
  1290. break;
  1291. default:
  1292. r = RESUME_GUEST;
  1293. break;
  1294. }
  1295. return r;
  1296. }
  1297. #endif /* CONFIG_VSX */
  1298. #ifdef CONFIG_ALTIVEC
  1299. int kvmppc_handle_vmx_load(struct kvm_vcpu *vcpu,
  1300. unsigned int rt, unsigned int bytes, int is_default_endian)
  1301. {
  1302. enum emulation_result emulated = EMULATE_DONE;
  1303. if (vcpu->arch.mmio_vmx_copy_nums > 2)
  1304. return EMULATE_FAIL;
  1305. while (vcpu->arch.mmio_vmx_copy_nums) {
  1306. emulated = __kvmppc_handle_load(vcpu, rt, bytes,
  1307. is_default_endian, 0);
  1308. if (emulated != EMULATE_DONE)
  1309. break;
  1310. vcpu->arch.paddr_accessed += vcpu->run->mmio.len;
  1311. vcpu->arch.mmio_vmx_copy_nums--;
  1312. vcpu->arch.mmio_vmx_offset++;
  1313. }
  1314. return emulated;
  1315. }
  1316. static int kvmppc_get_vmx_dword(struct kvm_vcpu *vcpu, int index, u64 *val)
  1317. {
  1318. union kvmppc_one_reg reg;
  1319. int vmx_offset = 0;
  1320. int result = 0;
  1321. vmx_offset =
  1322. kvmppc_get_vmx_dword_offset(vcpu, vcpu->arch.mmio_vmx_offset);
  1323. if (vmx_offset == -1)
  1324. return -1;
  1325. kvmppc_get_vsx_vr(vcpu, index, &reg.vval);
  1326. *val = reg.vsxval[vmx_offset];
  1327. return result;
  1328. }
  1329. static int kvmppc_get_vmx_word(struct kvm_vcpu *vcpu, int index, u64 *val)
  1330. {
  1331. union kvmppc_one_reg reg;
  1332. int vmx_offset = 0;
  1333. int result = 0;
  1334. vmx_offset =
  1335. kvmppc_get_vmx_word_offset(vcpu, vcpu->arch.mmio_vmx_offset);
  1336. if (vmx_offset == -1)
  1337. return -1;
  1338. kvmppc_get_vsx_vr(vcpu, index, &reg.vval);
  1339. *val = reg.vsx32val[vmx_offset];
  1340. return result;
  1341. }
  1342. static int kvmppc_get_vmx_hword(struct kvm_vcpu *vcpu, int index, u64 *val)
  1343. {
  1344. union kvmppc_one_reg reg;
  1345. int vmx_offset = 0;
  1346. int result = 0;
  1347. vmx_offset =
  1348. kvmppc_get_vmx_hword_offset(vcpu, vcpu->arch.mmio_vmx_offset);
  1349. if (vmx_offset == -1)
  1350. return -1;
  1351. kvmppc_get_vsx_vr(vcpu, index, &reg.vval);
  1352. *val = reg.vsx16val[vmx_offset];
  1353. return result;
  1354. }
  1355. static int kvmppc_get_vmx_byte(struct kvm_vcpu *vcpu, int index, u64 *val)
  1356. {
  1357. union kvmppc_one_reg reg;
  1358. int vmx_offset = 0;
  1359. int result = 0;
  1360. vmx_offset =
  1361. kvmppc_get_vmx_byte_offset(vcpu, vcpu->arch.mmio_vmx_offset);
  1362. if (vmx_offset == -1)
  1363. return -1;
  1364. kvmppc_get_vsx_vr(vcpu, index, &reg.vval);
  1365. *val = reg.vsx8val[vmx_offset];
  1366. return result;
  1367. }
  1368. int kvmppc_handle_vmx_store(struct kvm_vcpu *vcpu,
  1369. unsigned int rs, unsigned int bytes, int is_default_endian)
  1370. {
  1371. u64 val = 0;
  1372. unsigned int index = rs & KVM_MMIO_REG_MASK;
  1373. enum emulation_result emulated = EMULATE_DONE;
  1374. if (vcpu->arch.mmio_vmx_copy_nums > 2)
  1375. return EMULATE_FAIL;
  1376. vcpu->arch.io_gpr = rs;
  1377. while (vcpu->arch.mmio_vmx_copy_nums) {
  1378. switch (vcpu->arch.mmio_copy_type) {
  1379. case KVMPPC_VMX_COPY_DWORD:
  1380. if (kvmppc_get_vmx_dword(vcpu, index, &val) == -1)
  1381. return EMULATE_FAIL;
  1382. break;
  1383. case KVMPPC_VMX_COPY_WORD:
  1384. if (kvmppc_get_vmx_word(vcpu, index, &val) == -1)
  1385. return EMULATE_FAIL;
  1386. break;
  1387. case KVMPPC_VMX_COPY_HWORD:
  1388. if (kvmppc_get_vmx_hword(vcpu, index, &val) == -1)
  1389. return EMULATE_FAIL;
  1390. break;
  1391. case KVMPPC_VMX_COPY_BYTE:
  1392. if (kvmppc_get_vmx_byte(vcpu, index, &val) == -1)
  1393. return EMULATE_FAIL;
  1394. break;
  1395. default:
  1396. return EMULATE_FAIL;
  1397. }
  1398. emulated = kvmppc_handle_store(vcpu, val, bytes,
  1399. is_default_endian);
  1400. if (emulated != EMULATE_DONE)
  1401. break;
  1402. vcpu->arch.paddr_accessed += vcpu->run->mmio.len;
  1403. vcpu->arch.mmio_vmx_copy_nums--;
  1404. vcpu->arch.mmio_vmx_offset++;
  1405. }
  1406. return emulated;
  1407. }
  1408. static int kvmppc_emulate_mmio_vmx_loadstore(struct kvm_vcpu *vcpu)
  1409. {
  1410. struct kvm_run *run = vcpu->run;
  1411. enum emulation_result emulated = EMULATE_FAIL;
  1412. int r;
  1413. vcpu->arch.paddr_accessed += run->mmio.len;
  1414. if (!vcpu->mmio_is_write) {
  1415. emulated = kvmppc_handle_vmx_load(vcpu,
  1416. vcpu->arch.io_gpr, run->mmio.len, 1);
  1417. } else {
  1418. emulated = kvmppc_handle_vmx_store(vcpu,
  1419. vcpu->arch.io_gpr, run->mmio.len, 1);
  1420. }
  1421. switch (emulated) {
  1422. case EMULATE_DO_MMIO:
  1423. run->exit_reason = KVM_EXIT_MMIO;
  1424. r = RESUME_HOST;
  1425. break;
  1426. case EMULATE_FAIL:
  1427. pr_info("KVM: MMIO emulation failed (VMX repeat)\n");
  1428. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  1429. run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  1430. r = RESUME_HOST;
  1431. break;
  1432. default:
  1433. r = RESUME_GUEST;
  1434. break;
  1435. }
  1436. return r;
  1437. }
  1438. #endif /* CONFIG_ALTIVEC */
  1439. int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  1440. {
  1441. int r = 0;
  1442. union kvmppc_one_reg val;
  1443. int size;
  1444. size = one_reg_size(reg->id);
  1445. if (size > sizeof(val))
  1446. return -EINVAL;
  1447. r = kvmppc_get_one_reg(vcpu, reg->id, &val);
  1448. if (r == -EINVAL) {
  1449. r = 0;
  1450. switch (reg->id) {
  1451. #ifdef CONFIG_ALTIVEC
  1452. case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
  1453. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  1454. r = -ENXIO;
  1455. break;
  1456. }
  1457. kvmppc_get_vsx_vr(vcpu, reg->id - KVM_REG_PPC_VR0, &val.vval);
  1458. break;
  1459. case KVM_REG_PPC_VSCR:
  1460. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  1461. r = -ENXIO;
  1462. break;
  1463. }
  1464. val = get_reg_val(reg->id, kvmppc_get_vscr(vcpu));
  1465. break;
  1466. case KVM_REG_PPC_VRSAVE:
  1467. val = get_reg_val(reg->id, kvmppc_get_vrsave(vcpu));
  1468. break;
  1469. #endif /* CONFIG_ALTIVEC */
  1470. default:
  1471. r = -EINVAL;
  1472. break;
  1473. }
  1474. }
  1475. if (r)
  1476. return r;
  1477. if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size))
  1478. r = -EFAULT;
  1479. return r;
  1480. }
  1481. int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  1482. {
  1483. int r;
  1484. union kvmppc_one_reg val;
  1485. int size;
  1486. size = one_reg_size(reg->id);
  1487. if (size > sizeof(val))
  1488. return -EINVAL;
  1489. if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size))
  1490. return -EFAULT;
  1491. r = kvmppc_set_one_reg(vcpu, reg->id, &val);
  1492. if (r == -EINVAL) {
  1493. r = 0;
  1494. switch (reg->id) {
  1495. #ifdef CONFIG_ALTIVEC
  1496. case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31:
  1497. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  1498. r = -ENXIO;
  1499. break;
  1500. }
  1501. kvmppc_set_vsx_vr(vcpu, reg->id - KVM_REG_PPC_VR0, &val.vval);
  1502. break;
  1503. case KVM_REG_PPC_VSCR:
  1504. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  1505. r = -ENXIO;
  1506. break;
  1507. }
  1508. kvmppc_set_vscr(vcpu, set_reg_val(reg->id, val));
  1509. break;
  1510. case KVM_REG_PPC_VRSAVE:
  1511. if (!cpu_has_feature(CPU_FTR_ALTIVEC)) {
  1512. r = -ENXIO;
  1513. break;
  1514. }
  1515. kvmppc_set_vrsave(vcpu, set_reg_val(reg->id, val));
  1516. break;
  1517. #endif /* CONFIG_ALTIVEC */
  1518. default:
  1519. r = -EINVAL;
  1520. break;
  1521. }
  1522. }
  1523. return r;
  1524. }
  1525. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
  1526. {
  1527. struct kvm_run *run = vcpu->run;
  1528. int r;
  1529. vcpu_load(vcpu);
  1530. if (vcpu->mmio_needed) {
  1531. vcpu->mmio_needed = 0;
  1532. if (!vcpu->mmio_is_write)
  1533. kvmppc_complete_mmio_load(vcpu);
  1534. #ifdef CONFIG_VSX
  1535. if (vcpu->arch.mmio_vsx_copy_nums > 0) {
  1536. vcpu->arch.mmio_vsx_copy_nums--;
  1537. vcpu->arch.mmio_vsx_offset++;
  1538. }
  1539. if (vcpu->arch.mmio_vsx_copy_nums > 0) {
  1540. r = kvmppc_emulate_mmio_vsx_loadstore(vcpu);
  1541. if (r == RESUME_HOST) {
  1542. vcpu->mmio_needed = 1;
  1543. goto out;
  1544. }
  1545. }
  1546. #endif
  1547. #ifdef CONFIG_ALTIVEC
  1548. if (vcpu->arch.mmio_vmx_copy_nums > 0) {
  1549. vcpu->arch.mmio_vmx_copy_nums--;
  1550. vcpu->arch.mmio_vmx_offset++;
  1551. }
  1552. if (vcpu->arch.mmio_vmx_copy_nums > 0) {
  1553. r = kvmppc_emulate_mmio_vmx_loadstore(vcpu);
  1554. if (r == RESUME_HOST) {
  1555. vcpu->mmio_needed = 1;
  1556. goto out;
  1557. }
  1558. }
  1559. #endif
  1560. } else if (vcpu->arch.osi_needed) {
  1561. u64 *gprs = run->osi.gprs;
  1562. int i;
  1563. for (i = 0; i < 32; i++)
  1564. kvmppc_set_gpr(vcpu, i, gprs[i]);
  1565. vcpu->arch.osi_needed = 0;
  1566. } else if (vcpu->arch.hcall_needed) {
  1567. int i;
  1568. kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
  1569. for (i = 0; i < 9; ++i)
  1570. kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
  1571. vcpu->arch.hcall_needed = 0;
  1572. #ifdef CONFIG_BOOKE
  1573. } else if (vcpu->arch.epr_needed) {
  1574. kvmppc_set_epr(vcpu, run->epr.epr);
  1575. vcpu->arch.epr_needed = 0;
  1576. #endif
  1577. }
  1578. kvm_sigset_activate(vcpu);
  1579. if (!vcpu->wants_to_run)
  1580. r = -EINTR;
  1581. else
  1582. r = kvmppc_vcpu_run(vcpu);
  1583. kvm_sigset_deactivate(vcpu);
  1584. #ifdef CONFIG_ALTIVEC
  1585. out:
  1586. #endif
  1587. /*
  1588. * We're already returning to userspace, don't pass the
  1589. * RESUME_HOST flags along.
  1590. */
  1591. if (r > 0)
  1592. r = 0;
  1593. vcpu_put(vcpu);
  1594. return r;
  1595. }
  1596. int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
  1597. {
  1598. if (irq->irq == KVM_INTERRUPT_UNSET) {
  1599. kvmppc_core_dequeue_external(vcpu);
  1600. return 0;
  1601. }
  1602. kvmppc_core_queue_external(vcpu, irq);
  1603. kvm_vcpu_kick(vcpu);
  1604. return 0;
  1605. }
  1606. static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
  1607. struct kvm_enable_cap *cap)
  1608. {
  1609. int r;
  1610. if (cap->flags)
  1611. return -EINVAL;
  1612. switch (cap->cap) {
  1613. case KVM_CAP_PPC_OSI:
  1614. r = 0;
  1615. vcpu->arch.osi_enabled = true;
  1616. break;
  1617. case KVM_CAP_PPC_PAPR:
  1618. r = 0;
  1619. vcpu->arch.papr_enabled = true;
  1620. break;
  1621. case KVM_CAP_PPC_EPR:
  1622. r = 0;
  1623. if (cap->args[0])
  1624. vcpu->arch.epr_flags |= KVMPPC_EPR_USER;
  1625. else
  1626. vcpu->arch.epr_flags &= ~KVMPPC_EPR_USER;
  1627. break;
  1628. #ifdef CONFIG_BOOKE
  1629. case KVM_CAP_PPC_BOOKE_WATCHDOG:
  1630. r = 0;
  1631. vcpu->arch.watchdog_enabled = true;
  1632. break;
  1633. #endif
  1634. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  1635. case KVM_CAP_SW_TLB: {
  1636. struct kvm_config_tlb cfg;
  1637. void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
  1638. r = -EFAULT;
  1639. if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
  1640. break;
  1641. r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
  1642. break;
  1643. }
  1644. #endif
  1645. #ifdef CONFIG_KVM_MPIC
  1646. case KVM_CAP_IRQ_MPIC: {
  1647. struct fd f;
  1648. struct kvm_device *dev;
  1649. r = -EBADF;
  1650. f = fdget(cap->args[0]);
  1651. if (!fd_file(f))
  1652. break;
  1653. r = -EPERM;
  1654. dev = kvm_device_from_filp(fd_file(f));
  1655. if (dev)
  1656. r = kvmppc_mpic_connect_vcpu(dev, vcpu, cap->args[1]);
  1657. fdput(f);
  1658. break;
  1659. }
  1660. #endif
  1661. #ifdef CONFIG_KVM_XICS
  1662. case KVM_CAP_IRQ_XICS: {
  1663. struct fd f;
  1664. struct kvm_device *dev;
  1665. r = -EBADF;
  1666. f = fdget(cap->args[0]);
  1667. if (!fd_file(f))
  1668. break;
  1669. r = -EPERM;
  1670. dev = kvm_device_from_filp(fd_file(f));
  1671. if (dev) {
  1672. if (xics_on_xive())
  1673. r = kvmppc_xive_connect_vcpu(dev, vcpu, cap->args[1]);
  1674. else
  1675. r = kvmppc_xics_connect_vcpu(dev, vcpu, cap->args[1]);
  1676. }
  1677. fdput(f);
  1678. break;
  1679. }
  1680. #endif /* CONFIG_KVM_XICS */
  1681. #ifdef CONFIG_KVM_XIVE
  1682. case KVM_CAP_PPC_IRQ_XIVE: {
  1683. struct fd f;
  1684. struct kvm_device *dev;
  1685. r = -EBADF;
  1686. f = fdget(cap->args[0]);
  1687. if (!fd_file(f))
  1688. break;
  1689. r = -ENXIO;
  1690. if (!xive_enabled()) {
  1691. fdput(f);
  1692. break;
  1693. }
  1694. r = -EPERM;
  1695. dev = kvm_device_from_filp(fd_file(f));
  1696. if (dev)
  1697. r = kvmppc_xive_native_connect_vcpu(dev, vcpu,
  1698. cap->args[1]);
  1699. fdput(f);
  1700. break;
  1701. }
  1702. #endif /* CONFIG_KVM_XIVE */
  1703. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  1704. case KVM_CAP_PPC_FWNMI:
  1705. r = -EINVAL;
  1706. if (!is_kvmppc_hv_enabled(vcpu->kvm))
  1707. break;
  1708. r = 0;
  1709. vcpu->kvm->arch.fwnmi_enabled = true;
  1710. break;
  1711. #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
  1712. default:
  1713. r = -EINVAL;
  1714. break;
  1715. }
  1716. if (!r)
  1717. r = kvmppc_sanity_check(vcpu);
  1718. return r;
  1719. }
  1720. bool kvm_arch_intc_initialized(struct kvm *kvm)
  1721. {
  1722. #ifdef CONFIG_KVM_MPIC
  1723. if (kvm->arch.mpic)
  1724. return true;
  1725. #endif
  1726. #ifdef CONFIG_KVM_XICS
  1727. if (kvm->arch.xics || kvm->arch.xive)
  1728. return true;
  1729. #endif
  1730. return false;
  1731. }
  1732. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  1733. struct kvm_mp_state *mp_state)
  1734. {
  1735. return -EINVAL;
  1736. }
  1737. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  1738. struct kvm_mp_state *mp_state)
  1739. {
  1740. return -EINVAL;
  1741. }
  1742. long kvm_arch_vcpu_async_ioctl(struct file *filp,
  1743. unsigned int ioctl, unsigned long arg)
  1744. {
  1745. struct kvm_vcpu *vcpu = filp->private_data;
  1746. void __user *argp = (void __user *)arg;
  1747. if (ioctl == KVM_INTERRUPT) {
  1748. struct kvm_interrupt irq;
  1749. if (copy_from_user(&irq, argp, sizeof(irq)))
  1750. return -EFAULT;
  1751. return kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  1752. }
  1753. return -ENOIOCTLCMD;
  1754. }
  1755. long kvm_arch_vcpu_ioctl(struct file *filp,
  1756. unsigned int ioctl, unsigned long arg)
  1757. {
  1758. struct kvm_vcpu *vcpu = filp->private_data;
  1759. void __user *argp = (void __user *)arg;
  1760. long r;
  1761. switch (ioctl) {
  1762. case KVM_ENABLE_CAP:
  1763. {
  1764. struct kvm_enable_cap cap;
  1765. r = -EFAULT;
  1766. if (copy_from_user(&cap, argp, sizeof(cap)))
  1767. goto out;
  1768. vcpu_load(vcpu);
  1769. r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
  1770. vcpu_put(vcpu);
  1771. break;
  1772. }
  1773. case KVM_SET_ONE_REG:
  1774. case KVM_GET_ONE_REG:
  1775. {
  1776. struct kvm_one_reg reg;
  1777. r = -EFAULT;
  1778. if (copy_from_user(&reg, argp, sizeof(reg)))
  1779. goto out;
  1780. if (ioctl == KVM_SET_ONE_REG)
  1781. r = kvm_vcpu_ioctl_set_one_reg(vcpu, &reg);
  1782. else
  1783. r = kvm_vcpu_ioctl_get_one_reg(vcpu, &reg);
  1784. break;
  1785. }
  1786. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  1787. case KVM_DIRTY_TLB: {
  1788. struct kvm_dirty_tlb dirty;
  1789. r = -EFAULT;
  1790. if (copy_from_user(&dirty, argp, sizeof(dirty)))
  1791. goto out;
  1792. vcpu_load(vcpu);
  1793. r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
  1794. vcpu_put(vcpu);
  1795. break;
  1796. }
  1797. #endif
  1798. default:
  1799. r = -EINVAL;
  1800. }
  1801. out:
  1802. return r;
  1803. }
  1804. vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
  1805. {
  1806. return VM_FAULT_SIGBUS;
  1807. }
  1808. static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
  1809. {
  1810. u32 inst_nop = 0x60000000;
  1811. #ifdef CONFIG_KVM_BOOKE_HV
  1812. u32 inst_sc1 = 0x44000022;
  1813. pvinfo->hcall[0] = cpu_to_be32(inst_sc1);
  1814. pvinfo->hcall[1] = cpu_to_be32(inst_nop);
  1815. pvinfo->hcall[2] = cpu_to_be32(inst_nop);
  1816. pvinfo->hcall[3] = cpu_to_be32(inst_nop);
  1817. #else
  1818. u32 inst_lis = 0x3c000000;
  1819. u32 inst_ori = 0x60000000;
  1820. u32 inst_sc = 0x44000002;
  1821. u32 inst_imm_mask = 0xffff;
  1822. /*
  1823. * The hypercall to get into KVM from within guest context is as
  1824. * follows:
  1825. *
  1826. * lis r0, r0, KVM_SC_MAGIC_R0@h
  1827. * ori r0, KVM_SC_MAGIC_R0@l
  1828. * sc
  1829. * nop
  1830. */
  1831. pvinfo->hcall[0] = cpu_to_be32(inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask));
  1832. pvinfo->hcall[1] = cpu_to_be32(inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask));
  1833. pvinfo->hcall[2] = cpu_to_be32(inst_sc);
  1834. pvinfo->hcall[3] = cpu_to_be32(inst_nop);
  1835. #endif
  1836. pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
  1837. return 0;
  1838. }
  1839. bool kvm_arch_irqchip_in_kernel(struct kvm *kvm)
  1840. {
  1841. int ret = 0;
  1842. #ifdef CONFIG_KVM_MPIC
  1843. ret = ret || (kvm->arch.mpic != NULL);
  1844. #endif
  1845. #ifdef CONFIG_KVM_XICS
  1846. ret = ret || (kvm->arch.xics != NULL);
  1847. ret = ret || (kvm->arch.xive != NULL);
  1848. #endif
  1849. smp_rmb();
  1850. return ret;
  1851. }
  1852. int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
  1853. bool line_status)
  1854. {
  1855. if (!kvm_arch_irqchip_in_kernel(kvm))
  1856. return -ENXIO;
  1857. irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
  1858. irq_event->irq, irq_event->level,
  1859. line_status);
  1860. return 0;
  1861. }
  1862. int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  1863. struct kvm_enable_cap *cap)
  1864. {
  1865. int r;
  1866. if (cap->flags)
  1867. return -EINVAL;
  1868. switch (cap->cap) {
  1869. #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
  1870. case KVM_CAP_PPC_ENABLE_HCALL: {
  1871. unsigned long hcall = cap->args[0];
  1872. r = -EINVAL;
  1873. if (hcall > MAX_HCALL_OPCODE || (hcall & 3) ||
  1874. cap->args[1] > 1)
  1875. break;
  1876. if (!kvmppc_book3s_hcall_implemented(kvm, hcall))
  1877. break;
  1878. if (cap->args[1])
  1879. set_bit(hcall / 4, kvm->arch.enabled_hcalls);
  1880. else
  1881. clear_bit(hcall / 4, kvm->arch.enabled_hcalls);
  1882. r = 0;
  1883. break;
  1884. }
  1885. case KVM_CAP_PPC_SMT: {
  1886. unsigned long mode = cap->args[0];
  1887. unsigned long flags = cap->args[1];
  1888. r = -EINVAL;
  1889. if (kvm->arch.kvm_ops->set_smt_mode)
  1890. r = kvm->arch.kvm_ops->set_smt_mode(kvm, mode, flags);
  1891. break;
  1892. }
  1893. case KVM_CAP_PPC_NESTED_HV:
  1894. r = -EINVAL;
  1895. if (!is_kvmppc_hv_enabled(kvm) ||
  1896. !kvm->arch.kvm_ops->enable_nested)
  1897. break;
  1898. r = kvm->arch.kvm_ops->enable_nested(kvm);
  1899. break;
  1900. #endif
  1901. #if defined(CONFIG_KVM_BOOK3S_HV_POSSIBLE)
  1902. case KVM_CAP_PPC_SECURE_GUEST:
  1903. r = -EINVAL;
  1904. if (!is_kvmppc_hv_enabled(kvm) || !kvm->arch.kvm_ops->enable_svm)
  1905. break;
  1906. r = kvm->arch.kvm_ops->enable_svm(kvm);
  1907. break;
  1908. case KVM_CAP_PPC_DAWR1:
  1909. r = -EINVAL;
  1910. if (!is_kvmppc_hv_enabled(kvm) || !kvm->arch.kvm_ops->enable_dawr1)
  1911. break;
  1912. r = kvm->arch.kvm_ops->enable_dawr1(kvm);
  1913. break;
  1914. #endif
  1915. default:
  1916. r = -EINVAL;
  1917. break;
  1918. }
  1919. return r;
  1920. }
  1921. #ifdef CONFIG_PPC_BOOK3S_64
  1922. /*
  1923. * These functions check whether the underlying hardware is safe
  1924. * against attacks based on observing the effects of speculatively
  1925. * executed instructions, and whether it supplies instructions for
  1926. * use in workarounds. The information comes from firmware, either
  1927. * via the device tree on powernv platforms or from an hcall on
  1928. * pseries platforms.
  1929. */
  1930. #ifdef CONFIG_PPC_PSERIES
  1931. static int pseries_get_cpu_char(struct kvm_ppc_cpu_char *cp)
  1932. {
  1933. struct h_cpu_char_result c;
  1934. unsigned long rc;
  1935. if (!machine_is(pseries))
  1936. return -ENOTTY;
  1937. rc = plpar_get_cpu_characteristics(&c);
  1938. if (rc == H_SUCCESS) {
  1939. cp->character = c.character;
  1940. cp->behaviour = c.behaviour;
  1941. cp->character_mask = KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31 |
  1942. KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED |
  1943. KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30 |
  1944. KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2 |
  1945. KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV |
  1946. KVM_PPC_CPU_CHAR_BR_HINT_HONOURED |
  1947. KVM_PPC_CPU_CHAR_MTTRIG_THR_RECONF |
  1948. KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS |
  1949. KVM_PPC_CPU_CHAR_BCCTR_FLUSH_ASSIST;
  1950. cp->behaviour_mask = KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY |
  1951. KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR |
  1952. KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR |
  1953. KVM_PPC_CPU_BEHAV_FLUSH_COUNT_CACHE;
  1954. }
  1955. return 0;
  1956. }
  1957. #else
  1958. static int pseries_get_cpu_char(struct kvm_ppc_cpu_char *cp)
  1959. {
  1960. return -ENOTTY;
  1961. }
  1962. #endif
  1963. static inline bool have_fw_feat(struct device_node *fw_features,
  1964. const char *state, const char *name)
  1965. {
  1966. struct device_node *np;
  1967. bool r = false;
  1968. np = of_get_child_by_name(fw_features, name);
  1969. if (np) {
  1970. r = of_property_read_bool(np, state);
  1971. of_node_put(np);
  1972. }
  1973. return r;
  1974. }
  1975. static int kvmppc_get_cpu_char(struct kvm_ppc_cpu_char *cp)
  1976. {
  1977. struct device_node *np, *fw_features;
  1978. int r;
  1979. memset(cp, 0, sizeof(*cp));
  1980. r = pseries_get_cpu_char(cp);
  1981. if (r != -ENOTTY)
  1982. return r;
  1983. np = of_find_node_by_name(NULL, "ibm,opal");
  1984. if (np) {
  1985. fw_features = of_get_child_by_name(np, "fw-features");
  1986. of_node_put(np);
  1987. if (!fw_features)
  1988. return 0;
  1989. if (have_fw_feat(fw_features, "enabled",
  1990. "inst-spec-barrier-ori31,31,0"))
  1991. cp->character |= KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31;
  1992. if (have_fw_feat(fw_features, "enabled",
  1993. "fw-bcctrl-serialized"))
  1994. cp->character |= KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED;
  1995. if (have_fw_feat(fw_features, "enabled",
  1996. "inst-l1d-flush-ori30,30,0"))
  1997. cp->character |= KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30;
  1998. if (have_fw_feat(fw_features, "enabled",
  1999. "inst-l1d-flush-trig2"))
  2000. cp->character |= KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2;
  2001. if (have_fw_feat(fw_features, "enabled",
  2002. "fw-l1d-thread-split"))
  2003. cp->character |= KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV;
  2004. if (have_fw_feat(fw_features, "enabled",
  2005. "fw-count-cache-disabled"))
  2006. cp->character |= KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS;
  2007. if (have_fw_feat(fw_features, "enabled",
  2008. "fw-count-cache-flush-bcctr2,0,0"))
  2009. cp->character |= KVM_PPC_CPU_CHAR_BCCTR_FLUSH_ASSIST;
  2010. cp->character_mask = KVM_PPC_CPU_CHAR_SPEC_BAR_ORI31 |
  2011. KVM_PPC_CPU_CHAR_BCCTRL_SERIALISED |
  2012. KVM_PPC_CPU_CHAR_L1D_FLUSH_ORI30 |
  2013. KVM_PPC_CPU_CHAR_L1D_FLUSH_TRIG2 |
  2014. KVM_PPC_CPU_CHAR_L1D_THREAD_PRIV |
  2015. KVM_PPC_CPU_CHAR_COUNT_CACHE_DIS |
  2016. KVM_PPC_CPU_CHAR_BCCTR_FLUSH_ASSIST;
  2017. if (have_fw_feat(fw_features, "enabled",
  2018. "speculation-policy-favor-security"))
  2019. cp->behaviour |= KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY;
  2020. if (!have_fw_feat(fw_features, "disabled",
  2021. "needs-l1d-flush-msr-pr-0-to-1"))
  2022. cp->behaviour |= KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR;
  2023. if (!have_fw_feat(fw_features, "disabled",
  2024. "needs-spec-barrier-for-bound-checks"))
  2025. cp->behaviour |= KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR;
  2026. if (have_fw_feat(fw_features, "enabled",
  2027. "needs-count-cache-flush-on-context-switch"))
  2028. cp->behaviour |= KVM_PPC_CPU_BEHAV_FLUSH_COUNT_CACHE;
  2029. cp->behaviour_mask = KVM_PPC_CPU_BEHAV_FAVOUR_SECURITY |
  2030. KVM_PPC_CPU_BEHAV_L1D_FLUSH_PR |
  2031. KVM_PPC_CPU_BEHAV_BNDS_CHK_SPEC_BAR |
  2032. KVM_PPC_CPU_BEHAV_FLUSH_COUNT_CACHE;
  2033. of_node_put(fw_features);
  2034. }
  2035. return 0;
  2036. }
  2037. #endif
  2038. int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
  2039. {
  2040. struct kvm *kvm __maybe_unused = filp->private_data;
  2041. void __user *argp = (void __user *)arg;
  2042. int r;
  2043. switch (ioctl) {
  2044. case KVM_PPC_GET_PVINFO: {
  2045. struct kvm_ppc_pvinfo pvinfo;
  2046. memset(&pvinfo, 0, sizeof(pvinfo));
  2047. r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
  2048. if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
  2049. r = -EFAULT;
  2050. goto out;
  2051. }
  2052. break;
  2053. }
  2054. #ifdef CONFIG_SPAPR_TCE_IOMMU
  2055. case KVM_CREATE_SPAPR_TCE_64: {
  2056. struct kvm_create_spapr_tce_64 create_tce_64;
  2057. r = -EFAULT;
  2058. if (copy_from_user(&create_tce_64, argp, sizeof(create_tce_64)))
  2059. goto out;
  2060. if (create_tce_64.flags) {
  2061. r = -EINVAL;
  2062. goto out;
  2063. }
  2064. r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
  2065. goto out;
  2066. }
  2067. case KVM_CREATE_SPAPR_TCE: {
  2068. struct kvm_create_spapr_tce create_tce;
  2069. struct kvm_create_spapr_tce_64 create_tce_64;
  2070. r = -EFAULT;
  2071. if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
  2072. goto out;
  2073. create_tce_64.liobn = create_tce.liobn;
  2074. create_tce_64.page_shift = IOMMU_PAGE_SHIFT_4K;
  2075. create_tce_64.offset = 0;
  2076. create_tce_64.size = create_tce.window_size >>
  2077. IOMMU_PAGE_SHIFT_4K;
  2078. create_tce_64.flags = 0;
  2079. r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce_64);
  2080. goto out;
  2081. }
  2082. #endif
  2083. #ifdef CONFIG_PPC_BOOK3S_64
  2084. case KVM_PPC_GET_SMMU_INFO: {
  2085. struct kvm_ppc_smmu_info info;
  2086. struct kvm *kvm = filp->private_data;
  2087. memset(&info, 0, sizeof(info));
  2088. r = kvm->arch.kvm_ops->get_smmu_info(kvm, &info);
  2089. if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
  2090. r = -EFAULT;
  2091. break;
  2092. }
  2093. case KVM_PPC_RTAS_DEFINE_TOKEN: {
  2094. struct kvm *kvm = filp->private_data;
  2095. r = kvm_vm_ioctl_rtas_define_token(kvm, argp);
  2096. break;
  2097. }
  2098. case KVM_PPC_CONFIGURE_V3_MMU: {
  2099. struct kvm *kvm = filp->private_data;
  2100. struct kvm_ppc_mmuv3_cfg cfg;
  2101. r = -EINVAL;
  2102. if (!kvm->arch.kvm_ops->configure_mmu)
  2103. goto out;
  2104. r = -EFAULT;
  2105. if (copy_from_user(&cfg, argp, sizeof(cfg)))
  2106. goto out;
  2107. r = kvm->arch.kvm_ops->configure_mmu(kvm, &cfg);
  2108. break;
  2109. }
  2110. case KVM_PPC_GET_RMMU_INFO: {
  2111. struct kvm *kvm = filp->private_data;
  2112. struct kvm_ppc_rmmu_info info;
  2113. r = -EINVAL;
  2114. if (!kvm->arch.kvm_ops->get_rmmu_info)
  2115. goto out;
  2116. r = kvm->arch.kvm_ops->get_rmmu_info(kvm, &info);
  2117. if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
  2118. r = -EFAULT;
  2119. break;
  2120. }
  2121. case KVM_PPC_GET_CPU_CHAR: {
  2122. struct kvm_ppc_cpu_char cpuchar;
  2123. r = kvmppc_get_cpu_char(&cpuchar);
  2124. if (r >= 0 && copy_to_user(argp, &cpuchar, sizeof(cpuchar)))
  2125. r = -EFAULT;
  2126. break;
  2127. }
  2128. case KVM_PPC_SVM_OFF: {
  2129. struct kvm *kvm = filp->private_data;
  2130. r = 0;
  2131. if (!kvm->arch.kvm_ops->svm_off)
  2132. goto out;
  2133. r = kvm->arch.kvm_ops->svm_off(kvm);
  2134. break;
  2135. }
  2136. default: {
  2137. struct kvm *kvm = filp->private_data;
  2138. r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg);
  2139. }
  2140. #else /* CONFIG_PPC_BOOK3S_64 */
  2141. default:
  2142. r = -ENOTTY;
  2143. #endif
  2144. }
  2145. out:
  2146. return r;
  2147. }
  2148. static DEFINE_IDA(lpid_inuse);
  2149. static unsigned long nr_lpids;
  2150. long kvmppc_alloc_lpid(void)
  2151. {
  2152. int lpid;
  2153. /* The host LPID must always be 0 (allocation starts at 1) */
  2154. lpid = ida_alloc_range(&lpid_inuse, 1, nr_lpids - 1, GFP_KERNEL);
  2155. if (lpid < 0) {
  2156. if (lpid == -ENOMEM)
  2157. pr_err("%s: Out of memory\n", __func__);
  2158. else
  2159. pr_err("%s: No LPIDs free\n", __func__);
  2160. return -ENOMEM;
  2161. }
  2162. return lpid;
  2163. }
  2164. EXPORT_SYMBOL_GPL(kvmppc_alloc_lpid);
  2165. void kvmppc_free_lpid(long lpid)
  2166. {
  2167. ida_free(&lpid_inuse, lpid);
  2168. }
  2169. EXPORT_SYMBOL_GPL(kvmppc_free_lpid);
  2170. /* nr_lpids_param includes the host LPID */
  2171. void kvmppc_init_lpid(unsigned long nr_lpids_param)
  2172. {
  2173. nr_lpids = nr_lpids_param;
  2174. }
  2175. EXPORT_SYMBOL_GPL(kvmppc_init_lpid);
  2176. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ppc_instr);
  2177. void kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu, struct dentry *debugfs_dentry)
  2178. {
  2179. if (vcpu->kvm->arch.kvm_ops->create_vcpu_debugfs)
  2180. vcpu->kvm->arch.kvm_ops->create_vcpu_debugfs(vcpu, debugfs_dentry);
  2181. }
  2182. void kvm_arch_create_vm_debugfs(struct kvm *kvm)
  2183. {
  2184. if (kvm->arch.kvm_ops->create_vm_debugfs)
  2185. kvm->arch.kvm_ops->create_vm_debugfs(kvm);
  2186. }