powerpc.c 55 KB

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