powerpc.c 50 KB

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