powerpc.c 55 KB

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