powerpc.c 58 KB

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