powerpc.c 55 KB

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