powerpc.c 55 KB

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