booke.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159
  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. * Copyright 2010-2011 Freescale Semiconductor, Inc.
  17. *
  18. * Authors: Hollis Blanchard <hollisb@us.ibm.com>
  19. * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
  20. * Scott Wood <scottwood@freescale.com>
  21. * Varun Sethi <varun.sethi@freescale.com>
  22. */
  23. #include <linux/errno.h>
  24. #include <linux/err.h>
  25. #include <linux/kvm_host.h>
  26. #include <linux/gfp.h>
  27. #include <linux/module.h>
  28. #include <linux/vmalloc.h>
  29. #include <linux/fs.h>
  30. #include <asm/cputable.h>
  31. #include <asm/uaccess.h>
  32. #include <asm/kvm_ppc.h>
  33. #include <asm/cacheflush.h>
  34. #include <asm/dbell.h>
  35. #include <asm/hw_irq.h>
  36. #include <asm/irq.h>
  37. #include <asm/time.h>
  38. #include "timing.h"
  39. #include "booke.h"
  40. #define CREATE_TRACE_POINTS
  41. #include "trace_booke.h"
  42. unsigned long kvmppc_booke_handlers;
  43. #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
  44. #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
  45. struct kvm_stats_debugfs_item debugfs_entries[] = {
  46. { "mmio", VCPU_STAT(mmio_exits) },
  47. { "sig", VCPU_STAT(signal_exits) },
  48. { "itlb_r", VCPU_STAT(itlb_real_miss_exits) },
  49. { "itlb_v", VCPU_STAT(itlb_virt_miss_exits) },
  50. { "dtlb_r", VCPU_STAT(dtlb_real_miss_exits) },
  51. { "dtlb_v", VCPU_STAT(dtlb_virt_miss_exits) },
  52. { "sysc", VCPU_STAT(syscall_exits) },
  53. { "isi", VCPU_STAT(isi_exits) },
  54. { "dsi", VCPU_STAT(dsi_exits) },
  55. { "inst_emu", VCPU_STAT(emulated_inst_exits) },
  56. { "dec", VCPU_STAT(dec_exits) },
  57. { "ext_intr", VCPU_STAT(ext_intr_exits) },
  58. { "halt_wakeup", VCPU_STAT(halt_wakeup) },
  59. { "doorbell", VCPU_STAT(dbell_exits) },
  60. { "guest doorbell", VCPU_STAT(gdbell_exits) },
  61. { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
  62. { NULL }
  63. };
  64. /* TODO: use vcpu_printf() */
  65. void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
  66. {
  67. int i;
  68. printk("pc: %08lx msr: %08llx\n", vcpu->arch.pc, vcpu->arch.shared->msr);
  69. printk("lr: %08lx ctr: %08lx\n", vcpu->arch.lr, vcpu->arch.ctr);
  70. printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0,
  71. vcpu->arch.shared->srr1);
  72. printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
  73. for (i = 0; i < 32; i += 4) {
  74. printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
  75. kvmppc_get_gpr(vcpu, i),
  76. kvmppc_get_gpr(vcpu, i+1),
  77. kvmppc_get_gpr(vcpu, i+2),
  78. kvmppc_get_gpr(vcpu, i+3));
  79. }
  80. }
  81. #ifdef CONFIG_SPE
  82. void kvmppc_vcpu_disable_spe(struct kvm_vcpu *vcpu)
  83. {
  84. preempt_disable();
  85. enable_kernel_spe();
  86. kvmppc_save_guest_spe(vcpu);
  87. vcpu->arch.shadow_msr &= ~MSR_SPE;
  88. preempt_enable();
  89. }
  90. static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu)
  91. {
  92. preempt_disable();
  93. enable_kernel_spe();
  94. kvmppc_load_guest_spe(vcpu);
  95. vcpu->arch.shadow_msr |= MSR_SPE;
  96. preempt_enable();
  97. }
  98. static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
  99. {
  100. if (vcpu->arch.shared->msr & MSR_SPE) {
  101. if (!(vcpu->arch.shadow_msr & MSR_SPE))
  102. kvmppc_vcpu_enable_spe(vcpu);
  103. } else if (vcpu->arch.shadow_msr & MSR_SPE) {
  104. kvmppc_vcpu_disable_spe(vcpu);
  105. }
  106. }
  107. #else
  108. static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
  109. {
  110. }
  111. #endif
  112. /*
  113. * Load up guest vcpu FP state if it's needed.
  114. * It also set the MSR_FP in thread so that host know
  115. * we're holding FPU, and then host can help to save
  116. * guest vcpu FP state if other threads require to use FPU.
  117. * This simulates an FP unavailable fault.
  118. *
  119. * It requires to be called with preemption disabled.
  120. */
  121. static inline void kvmppc_load_guest_fp(struct kvm_vcpu *vcpu)
  122. {
  123. #ifdef CONFIG_PPC_FPU
  124. if (!(current->thread.regs->msr & MSR_FP)) {
  125. enable_kernel_fp();
  126. load_fp_state(&vcpu->arch.fp);
  127. current->thread.fp_save_area = &vcpu->arch.fp;
  128. current->thread.regs->msr |= MSR_FP;
  129. }
  130. #endif
  131. }
  132. /*
  133. * Save guest vcpu FP state into thread.
  134. * It requires to be called with preemption disabled.
  135. */
  136. static inline void kvmppc_save_guest_fp(struct kvm_vcpu *vcpu)
  137. {
  138. #ifdef CONFIG_PPC_FPU
  139. if (current->thread.regs->msr & MSR_FP)
  140. giveup_fpu(current);
  141. current->thread.fp_save_area = NULL;
  142. #endif
  143. }
  144. static void kvmppc_vcpu_sync_fpu(struct kvm_vcpu *vcpu)
  145. {
  146. #if defined(CONFIG_PPC_FPU) && !defined(CONFIG_KVM_BOOKE_HV)
  147. /* We always treat the FP bit as enabled from the host
  148. perspective, so only need to adjust the shadow MSR */
  149. vcpu->arch.shadow_msr &= ~MSR_FP;
  150. vcpu->arch.shadow_msr |= vcpu->arch.shared->msr & MSR_FP;
  151. #endif
  152. }
  153. /*
  154. * Simulate AltiVec unavailable fault to load guest state
  155. * from thread to AltiVec unit.
  156. * It requires to be called with preemption disabled.
  157. */
  158. static inline void kvmppc_load_guest_altivec(struct kvm_vcpu *vcpu)
  159. {
  160. #ifdef CONFIG_ALTIVEC
  161. if (cpu_has_feature(CPU_FTR_ALTIVEC)) {
  162. if (!(current->thread.regs->msr & MSR_VEC)) {
  163. enable_kernel_altivec();
  164. load_vr_state(&vcpu->arch.vr);
  165. current->thread.vr_save_area = &vcpu->arch.vr;
  166. current->thread.regs->msr |= MSR_VEC;
  167. }
  168. }
  169. #endif
  170. }
  171. /*
  172. * Save guest vcpu AltiVec state into thread.
  173. * It requires to be called with preemption disabled.
  174. */
  175. static inline void kvmppc_save_guest_altivec(struct kvm_vcpu *vcpu)
  176. {
  177. #ifdef CONFIG_ALTIVEC
  178. if (cpu_has_feature(CPU_FTR_ALTIVEC)) {
  179. if (current->thread.regs->msr & MSR_VEC)
  180. giveup_altivec(current);
  181. current->thread.vr_save_area = NULL;
  182. }
  183. #endif
  184. }
  185. static void kvmppc_vcpu_sync_debug(struct kvm_vcpu *vcpu)
  186. {
  187. /* Synchronize guest's desire to get debug interrupts into shadow MSR */
  188. #ifndef CONFIG_KVM_BOOKE_HV
  189. vcpu->arch.shadow_msr &= ~MSR_DE;
  190. vcpu->arch.shadow_msr |= vcpu->arch.shared->msr & MSR_DE;
  191. #endif
  192. /* Force enable debug interrupts when user space wants to debug */
  193. if (vcpu->guest_debug) {
  194. #ifdef CONFIG_KVM_BOOKE_HV
  195. /*
  196. * Since there is no shadow MSR, sync MSR_DE into the guest
  197. * visible MSR.
  198. */
  199. vcpu->arch.shared->msr |= MSR_DE;
  200. #else
  201. vcpu->arch.shadow_msr |= MSR_DE;
  202. vcpu->arch.shared->msr &= ~MSR_DE;
  203. #endif
  204. }
  205. }
  206. /*
  207. * Helper function for "full" MSR writes. No need to call this if only
  208. * EE/CE/ME/DE/RI are changing.
  209. */
  210. void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr)
  211. {
  212. u32 old_msr = vcpu->arch.shared->msr;
  213. #ifdef CONFIG_KVM_BOOKE_HV
  214. new_msr |= MSR_GS;
  215. #endif
  216. vcpu->arch.shared->msr = new_msr;
  217. kvmppc_mmu_msr_notify(vcpu, old_msr);
  218. kvmppc_vcpu_sync_spe(vcpu);
  219. kvmppc_vcpu_sync_fpu(vcpu);
  220. kvmppc_vcpu_sync_debug(vcpu);
  221. }
  222. static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
  223. unsigned int priority)
  224. {
  225. trace_kvm_booke_queue_irqprio(vcpu, priority);
  226. set_bit(priority, &vcpu->arch.pending_exceptions);
  227. }
  228. void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
  229. ulong dear_flags, ulong esr_flags)
  230. {
  231. vcpu->arch.queued_dear = dear_flags;
  232. vcpu->arch.queued_esr = esr_flags;
  233. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
  234. }
  235. void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
  236. ulong dear_flags, ulong esr_flags)
  237. {
  238. vcpu->arch.queued_dear = dear_flags;
  239. vcpu->arch.queued_esr = esr_flags;
  240. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
  241. }
  242. void kvmppc_core_queue_itlb_miss(struct kvm_vcpu *vcpu)
  243. {
  244. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
  245. }
  246. void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu, ulong esr_flags)
  247. {
  248. vcpu->arch.queued_esr = esr_flags;
  249. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
  250. }
  251. static void kvmppc_core_queue_alignment(struct kvm_vcpu *vcpu, ulong dear_flags,
  252. ulong esr_flags)
  253. {
  254. vcpu->arch.queued_dear = dear_flags;
  255. vcpu->arch.queued_esr = esr_flags;
  256. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALIGNMENT);
  257. }
  258. void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
  259. {
  260. vcpu->arch.queued_esr = esr_flags;
  261. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
  262. }
  263. void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
  264. {
  265. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
  266. }
  267. int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
  268. {
  269. return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
  270. }
  271. void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
  272. {
  273. clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
  274. }
  275. void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
  276. struct kvm_interrupt *irq)
  277. {
  278. unsigned int prio = BOOKE_IRQPRIO_EXTERNAL;
  279. if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
  280. prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL;
  281. kvmppc_booke_queue_irqprio(vcpu, prio);
  282. }
  283. void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu)
  284. {
  285. clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
  286. clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
  287. }
  288. static void kvmppc_core_queue_watchdog(struct kvm_vcpu *vcpu)
  289. {
  290. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_WATCHDOG);
  291. }
  292. static void kvmppc_core_dequeue_watchdog(struct kvm_vcpu *vcpu)
  293. {
  294. clear_bit(BOOKE_IRQPRIO_WATCHDOG, &vcpu->arch.pending_exceptions);
  295. }
  296. void kvmppc_core_queue_debug(struct kvm_vcpu *vcpu)
  297. {
  298. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DEBUG);
  299. }
  300. void kvmppc_core_dequeue_debug(struct kvm_vcpu *vcpu)
  301. {
  302. clear_bit(BOOKE_IRQPRIO_DEBUG, &vcpu->arch.pending_exceptions);
  303. }
  304. static void set_guest_srr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
  305. {
  306. kvmppc_set_srr0(vcpu, srr0);
  307. kvmppc_set_srr1(vcpu, srr1);
  308. }
  309. static void set_guest_csrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
  310. {
  311. vcpu->arch.csrr0 = srr0;
  312. vcpu->arch.csrr1 = srr1;
  313. }
  314. static void set_guest_dsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
  315. {
  316. if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC)) {
  317. vcpu->arch.dsrr0 = srr0;
  318. vcpu->arch.dsrr1 = srr1;
  319. } else {
  320. set_guest_csrr(vcpu, srr0, srr1);
  321. }
  322. }
  323. static void set_guest_mcsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
  324. {
  325. vcpu->arch.mcsrr0 = srr0;
  326. vcpu->arch.mcsrr1 = srr1;
  327. }
  328. /* Deliver the interrupt of the corresponding priority, if possible. */
  329. static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
  330. unsigned int priority)
  331. {
  332. int allowed = 0;
  333. ulong msr_mask = 0;
  334. bool update_esr = false, update_dear = false, update_epr = false;
  335. ulong crit_raw = vcpu->arch.shared->critical;
  336. ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
  337. bool crit;
  338. bool keep_irq = false;
  339. enum int_class int_class;
  340. ulong new_msr = vcpu->arch.shared->msr;
  341. /* Truncate crit indicators in 32 bit mode */
  342. if (!(vcpu->arch.shared->msr & MSR_SF)) {
  343. crit_raw &= 0xffffffff;
  344. crit_r1 &= 0xffffffff;
  345. }
  346. /* Critical section when crit == r1 */
  347. crit = (crit_raw == crit_r1);
  348. /* ... and we're in supervisor mode */
  349. crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
  350. if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
  351. priority = BOOKE_IRQPRIO_EXTERNAL;
  352. keep_irq = true;
  353. }
  354. if ((priority == BOOKE_IRQPRIO_EXTERNAL) && vcpu->arch.epr_flags)
  355. update_epr = true;
  356. switch (priority) {
  357. case BOOKE_IRQPRIO_DTLB_MISS:
  358. case BOOKE_IRQPRIO_DATA_STORAGE:
  359. case BOOKE_IRQPRIO_ALIGNMENT:
  360. update_dear = true;
  361. /* fall through */
  362. case BOOKE_IRQPRIO_INST_STORAGE:
  363. case BOOKE_IRQPRIO_PROGRAM:
  364. update_esr = true;
  365. /* fall through */
  366. case BOOKE_IRQPRIO_ITLB_MISS:
  367. case BOOKE_IRQPRIO_SYSCALL:
  368. case BOOKE_IRQPRIO_FP_UNAVAIL:
  369. #ifdef CONFIG_SPE_POSSIBLE
  370. case BOOKE_IRQPRIO_SPE_UNAVAIL:
  371. case BOOKE_IRQPRIO_SPE_FP_DATA:
  372. case BOOKE_IRQPRIO_SPE_FP_ROUND:
  373. #endif
  374. #ifdef CONFIG_ALTIVEC
  375. case BOOKE_IRQPRIO_ALTIVEC_UNAVAIL:
  376. case BOOKE_IRQPRIO_ALTIVEC_ASSIST:
  377. #endif
  378. case BOOKE_IRQPRIO_AP_UNAVAIL:
  379. allowed = 1;
  380. msr_mask = MSR_CE | MSR_ME | MSR_DE;
  381. int_class = INT_CLASS_NONCRIT;
  382. break;
  383. case BOOKE_IRQPRIO_WATCHDOG:
  384. case BOOKE_IRQPRIO_CRITICAL:
  385. case BOOKE_IRQPRIO_DBELL_CRIT:
  386. allowed = vcpu->arch.shared->msr & MSR_CE;
  387. allowed = allowed && !crit;
  388. msr_mask = MSR_ME;
  389. int_class = INT_CLASS_CRIT;
  390. break;
  391. case BOOKE_IRQPRIO_MACHINE_CHECK:
  392. allowed = vcpu->arch.shared->msr & MSR_ME;
  393. allowed = allowed && !crit;
  394. int_class = INT_CLASS_MC;
  395. break;
  396. case BOOKE_IRQPRIO_DECREMENTER:
  397. case BOOKE_IRQPRIO_FIT:
  398. keep_irq = true;
  399. /* fall through */
  400. case BOOKE_IRQPRIO_EXTERNAL:
  401. case BOOKE_IRQPRIO_DBELL:
  402. allowed = vcpu->arch.shared->msr & MSR_EE;
  403. allowed = allowed && !crit;
  404. msr_mask = MSR_CE | MSR_ME | MSR_DE;
  405. int_class = INT_CLASS_NONCRIT;
  406. break;
  407. case BOOKE_IRQPRIO_DEBUG:
  408. allowed = vcpu->arch.shared->msr & MSR_DE;
  409. allowed = allowed && !crit;
  410. msr_mask = MSR_ME;
  411. if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC))
  412. int_class = INT_CLASS_DBG;
  413. else
  414. int_class = INT_CLASS_CRIT;
  415. break;
  416. }
  417. if (allowed) {
  418. switch (int_class) {
  419. case INT_CLASS_NONCRIT:
  420. set_guest_srr(vcpu, vcpu->arch.pc,
  421. vcpu->arch.shared->msr);
  422. break;
  423. case INT_CLASS_CRIT:
  424. set_guest_csrr(vcpu, vcpu->arch.pc,
  425. vcpu->arch.shared->msr);
  426. break;
  427. case INT_CLASS_DBG:
  428. set_guest_dsrr(vcpu, vcpu->arch.pc,
  429. vcpu->arch.shared->msr);
  430. break;
  431. case INT_CLASS_MC:
  432. set_guest_mcsrr(vcpu, vcpu->arch.pc,
  433. vcpu->arch.shared->msr);
  434. break;
  435. }
  436. vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
  437. if (update_esr == true)
  438. kvmppc_set_esr(vcpu, vcpu->arch.queued_esr);
  439. if (update_dear == true)
  440. kvmppc_set_dar(vcpu, vcpu->arch.queued_dear);
  441. if (update_epr == true) {
  442. if (vcpu->arch.epr_flags & KVMPPC_EPR_USER)
  443. kvm_make_request(KVM_REQ_EPR_EXIT, vcpu);
  444. else if (vcpu->arch.epr_flags & KVMPPC_EPR_KERNEL) {
  445. BUG_ON(vcpu->arch.irq_type != KVMPPC_IRQ_MPIC);
  446. kvmppc_mpic_set_epr(vcpu);
  447. }
  448. }
  449. new_msr &= msr_mask;
  450. #if defined(CONFIG_64BIT)
  451. if (vcpu->arch.epcr & SPRN_EPCR_ICM)
  452. new_msr |= MSR_CM;
  453. #endif
  454. kvmppc_set_msr(vcpu, new_msr);
  455. if (!keep_irq)
  456. clear_bit(priority, &vcpu->arch.pending_exceptions);
  457. }
  458. #ifdef CONFIG_KVM_BOOKE_HV
  459. /*
  460. * If an interrupt is pending but masked, raise a guest doorbell
  461. * so that we are notified when the guest enables the relevant
  462. * MSR bit.
  463. */
  464. if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE)
  465. kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT);
  466. if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE)
  467. kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT);
  468. if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK)
  469. kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC);
  470. #endif
  471. return allowed;
  472. }
  473. /*
  474. * Return the number of jiffies until the next timeout. If the timeout is
  475. * longer than the NEXT_TIMER_MAX_DELTA, then return NEXT_TIMER_MAX_DELTA
  476. * because the larger value can break the timer APIs.
  477. */
  478. static unsigned long watchdog_next_timeout(struct kvm_vcpu *vcpu)
  479. {
  480. u64 tb, wdt_tb, wdt_ticks = 0;
  481. u64 nr_jiffies = 0;
  482. u32 period = TCR_GET_WP(vcpu->arch.tcr);
  483. wdt_tb = 1ULL << (63 - period);
  484. tb = get_tb();
  485. /*
  486. * The watchdog timeout will hapeen when TB bit corresponding
  487. * to watchdog will toggle from 0 to 1.
  488. */
  489. if (tb & wdt_tb)
  490. wdt_ticks = wdt_tb;
  491. wdt_ticks += wdt_tb - (tb & (wdt_tb - 1));
  492. /* Convert timebase ticks to jiffies */
  493. nr_jiffies = wdt_ticks;
  494. if (do_div(nr_jiffies, tb_ticks_per_jiffy))
  495. nr_jiffies++;
  496. return min_t(unsigned long long, nr_jiffies, NEXT_TIMER_MAX_DELTA);
  497. }
  498. static void arm_next_watchdog(struct kvm_vcpu *vcpu)
  499. {
  500. unsigned long nr_jiffies;
  501. unsigned long flags;
  502. /*
  503. * If TSR_ENW and TSR_WIS are not set then no need to exit to
  504. * userspace, so clear the KVM_REQ_WATCHDOG request.
  505. */
  506. if ((vcpu->arch.tsr & (TSR_ENW | TSR_WIS)) != (TSR_ENW | TSR_WIS))
  507. clear_bit(KVM_REQ_WATCHDOG, &vcpu->requests);
  508. spin_lock_irqsave(&vcpu->arch.wdt_lock, flags);
  509. nr_jiffies = watchdog_next_timeout(vcpu);
  510. /*
  511. * If the number of jiffies of watchdog timer >= NEXT_TIMER_MAX_DELTA
  512. * then do not run the watchdog timer as this can break timer APIs.
  513. */
  514. if (nr_jiffies < NEXT_TIMER_MAX_DELTA)
  515. mod_timer(&vcpu->arch.wdt_timer, jiffies + nr_jiffies);
  516. else
  517. del_timer(&vcpu->arch.wdt_timer);
  518. spin_unlock_irqrestore(&vcpu->arch.wdt_lock, flags);
  519. }
  520. void kvmppc_watchdog_func(unsigned long data)
  521. {
  522. struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
  523. u32 tsr, new_tsr;
  524. int final;
  525. do {
  526. new_tsr = tsr = vcpu->arch.tsr;
  527. final = 0;
  528. /* Time out event */
  529. if (tsr & TSR_ENW) {
  530. if (tsr & TSR_WIS)
  531. final = 1;
  532. else
  533. new_tsr = tsr | TSR_WIS;
  534. } else {
  535. new_tsr = tsr | TSR_ENW;
  536. }
  537. } while (cmpxchg(&vcpu->arch.tsr, tsr, new_tsr) != tsr);
  538. if (new_tsr & TSR_WIS) {
  539. smp_wmb();
  540. kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
  541. kvm_vcpu_kick(vcpu);
  542. }
  543. /*
  544. * If this is final watchdog expiry and some action is required
  545. * then exit to userspace.
  546. */
  547. if (final && (vcpu->arch.tcr & TCR_WRC_MASK) &&
  548. vcpu->arch.watchdog_enabled) {
  549. smp_wmb();
  550. kvm_make_request(KVM_REQ_WATCHDOG, vcpu);
  551. kvm_vcpu_kick(vcpu);
  552. }
  553. /*
  554. * Stop running the watchdog timer after final expiration to
  555. * prevent the host from being flooded with timers if the
  556. * guest sets a short period.
  557. * Timers will resume when TSR/TCR is updated next time.
  558. */
  559. if (!final)
  560. arm_next_watchdog(vcpu);
  561. }
  562. static void update_timer_ints(struct kvm_vcpu *vcpu)
  563. {
  564. if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS))
  565. kvmppc_core_queue_dec(vcpu);
  566. else
  567. kvmppc_core_dequeue_dec(vcpu);
  568. if ((vcpu->arch.tcr & TCR_WIE) && (vcpu->arch.tsr & TSR_WIS))
  569. kvmppc_core_queue_watchdog(vcpu);
  570. else
  571. kvmppc_core_dequeue_watchdog(vcpu);
  572. }
  573. static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu)
  574. {
  575. unsigned long *pending = &vcpu->arch.pending_exceptions;
  576. unsigned int priority;
  577. priority = __ffs(*pending);
  578. while (priority < BOOKE_IRQPRIO_MAX) {
  579. if (kvmppc_booke_irqprio_deliver(vcpu, priority))
  580. break;
  581. priority = find_next_bit(pending,
  582. BITS_PER_BYTE * sizeof(*pending),
  583. priority + 1);
  584. }
  585. /* Tell the guest about our interrupt status */
  586. vcpu->arch.shared->int_pending = !!*pending;
  587. }
  588. /* Check pending exceptions and deliver one, if possible. */
  589. int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
  590. {
  591. int r = 0;
  592. WARN_ON_ONCE(!irqs_disabled());
  593. kvmppc_core_check_exceptions(vcpu);
  594. if (vcpu->requests) {
  595. /* Exception delivery raised request; start over */
  596. return 1;
  597. }
  598. if (vcpu->arch.shared->msr & MSR_WE) {
  599. local_irq_enable();
  600. kvm_vcpu_block(vcpu);
  601. clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
  602. hard_irq_disable();
  603. kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
  604. r = 1;
  605. };
  606. return r;
  607. }
  608. int kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
  609. {
  610. int r = 1; /* Indicate we want to get back into the guest */
  611. if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu))
  612. update_timer_ints(vcpu);
  613. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  614. if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
  615. kvmppc_core_flush_tlb(vcpu);
  616. #endif
  617. if (kvm_check_request(KVM_REQ_WATCHDOG, vcpu)) {
  618. vcpu->run->exit_reason = KVM_EXIT_WATCHDOG;
  619. r = 0;
  620. }
  621. if (kvm_check_request(KVM_REQ_EPR_EXIT, vcpu)) {
  622. vcpu->run->epr.epr = 0;
  623. vcpu->arch.epr_needed = true;
  624. vcpu->run->exit_reason = KVM_EXIT_EPR;
  625. r = 0;
  626. }
  627. return r;
  628. }
  629. int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  630. {
  631. int ret, s;
  632. struct debug_reg debug;
  633. if (!vcpu->arch.sane) {
  634. kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  635. return -EINVAL;
  636. }
  637. s = kvmppc_prepare_to_enter(vcpu);
  638. if (s <= 0) {
  639. ret = s;
  640. goto out;
  641. }
  642. /* interrupts now hard-disabled */
  643. #ifdef CONFIG_PPC_FPU
  644. /* Save userspace FPU state in stack */
  645. enable_kernel_fp();
  646. /*
  647. * Since we can't trap on MSR_FP in GS-mode, we consider the guest
  648. * as always using the FPU.
  649. */
  650. kvmppc_load_guest_fp(vcpu);
  651. #endif
  652. #ifdef CONFIG_ALTIVEC
  653. /* Save userspace AltiVec state in stack */
  654. if (cpu_has_feature(CPU_FTR_ALTIVEC))
  655. enable_kernel_altivec();
  656. /*
  657. * Since we can't trap on MSR_VEC in GS-mode, we consider the guest
  658. * as always using the AltiVec.
  659. */
  660. kvmppc_load_guest_altivec(vcpu);
  661. #endif
  662. /* Switch to guest debug context */
  663. debug = vcpu->arch.dbg_reg;
  664. switch_booke_debug_regs(&debug);
  665. debug = current->thread.debug;
  666. current->thread.debug = vcpu->arch.dbg_reg;
  667. vcpu->arch.pgdir = current->mm->pgd;
  668. kvmppc_fix_ee_before_entry();
  669. ret = __kvmppc_vcpu_run(kvm_run, vcpu);
  670. /* No need for kvm_guest_exit. It's done in handle_exit.
  671. We also get here with interrupts enabled. */
  672. /* Switch back to user space debug context */
  673. switch_booke_debug_regs(&debug);
  674. current->thread.debug = debug;
  675. #ifdef CONFIG_PPC_FPU
  676. kvmppc_save_guest_fp(vcpu);
  677. #endif
  678. #ifdef CONFIG_ALTIVEC
  679. kvmppc_save_guest_altivec(vcpu);
  680. #endif
  681. out:
  682. vcpu->mode = OUTSIDE_GUEST_MODE;
  683. return ret;
  684. }
  685. static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
  686. {
  687. enum emulation_result er;
  688. er = kvmppc_emulate_instruction(run, vcpu);
  689. switch (er) {
  690. case EMULATE_DONE:
  691. /* don't overwrite subtypes, just account kvm_stats */
  692. kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
  693. /* Future optimization: only reload non-volatiles if
  694. * they were actually modified by emulation. */
  695. return RESUME_GUEST_NV;
  696. case EMULATE_AGAIN:
  697. return RESUME_GUEST;
  698. case EMULATE_FAIL:
  699. printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
  700. __func__, vcpu->arch.pc, vcpu->arch.last_inst);
  701. /* For debugging, encode the failing instruction and
  702. * report it to userspace. */
  703. run->hw.hardware_exit_reason = ~0ULL << 32;
  704. run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
  705. kvmppc_core_queue_program(vcpu, ESR_PIL);
  706. return RESUME_HOST;
  707. case EMULATE_EXIT_USER:
  708. return RESUME_HOST;
  709. default:
  710. BUG();
  711. }
  712. }
  713. static int kvmppc_handle_debug(struct kvm_run *run, struct kvm_vcpu *vcpu)
  714. {
  715. struct debug_reg *dbg_reg = &(vcpu->arch.dbg_reg);
  716. u32 dbsr = vcpu->arch.dbsr;
  717. if (vcpu->guest_debug == 0) {
  718. /*
  719. * Debug resources belong to Guest.
  720. * Imprecise debug event is not injected
  721. */
  722. if (dbsr & DBSR_IDE) {
  723. dbsr &= ~DBSR_IDE;
  724. if (!dbsr)
  725. return RESUME_GUEST;
  726. }
  727. if (dbsr && (vcpu->arch.shared->msr & MSR_DE) &&
  728. (vcpu->arch.dbg_reg.dbcr0 & DBCR0_IDM))
  729. kvmppc_core_queue_debug(vcpu);
  730. /* Inject a program interrupt if trap debug is not allowed */
  731. if ((dbsr & DBSR_TIE) && !(vcpu->arch.shared->msr & MSR_DE))
  732. kvmppc_core_queue_program(vcpu, ESR_PTR);
  733. return RESUME_GUEST;
  734. }
  735. /*
  736. * Debug resource owned by userspace.
  737. * Clear guest dbsr (vcpu->arch.dbsr)
  738. */
  739. vcpu->arch.dbsr = 0;
  740. run->debug.arch.status = 0;
  741. run->debug.arch.address = vcpu->arch.pc;
  742. if (dbsr & (DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4)) {
  743. run->debug.arch.status |= KVMPPC_DEBUG_BREAKPOINT;
  744. } else {
  745. if (dbsr & (DBSR_DAC1W | DBSR_DAC2W))
  746. run->debug.arch.status |= KVMPPC_DEBUG_WATCH_WRITE;
  747. else if (dbsr & (DBSR_DAC1R | DBSR_DAC2R))
  748. run->debug.arch.status |= KVMPPC_DEBUG_WATCH_READ;
  749. if (dbsr & (DBSR_DAC1R | DBSR_DAC1W))
  750. run->debug.arch.address = dbg_reg->dac1;
  751. else if (dbsr & (DBSR_DAC2R | DBSR_DAC2W))
  752. run->debug.arch.address = dbg_reg->dac2;
  753. }
  754. return RESUME_HOST;
  755. }
  756. static void kvmppc_fill_pt_regs(struct pt_regs *regs)
  757. {
  758. ulong r1, ip, msr, lr;
  759. asm("mr %0, 1" : "=r"(r1));
  760. asm("mflr %0" : "=r"(lr));
  761. asm("mfmsr %0" : "=r"(msr));
  762. asm("bl 1f; 1: mflr %0" : "=r"(ip));
  763. memset(regs, 0, sizeof(*regs));
  764. regs->gpr[1] = r1;
  765. regs->nip = ip;
  766. regs->msr = msr;
  767. regs->link = lr;
  768. }
  769. /*
  770. * For interrupts needed to be handled by host interrupt handlers,
  771. * corresponding host handler are called from here in similar way
  772. * (but not exact) as they are called from low level handler
  773. * (such as from arch/powerpc/kernel/head_fsl_booke.S).
  774. */
  775. static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu,
  776. unsigned int exit_nr)
  777. {
  778. struct pt_regs regs;
  779. switch (exit_nr) {
  780. case BOOKE_INTERRUPT_EXTERNAL:
  781. kvmppc_fill_pt_regs(&regs);
  782. do_IRQ(&regs);
  783. break;
  784. case BOOKE_INTERRUPT_DECREMENTER:
  785. kvmppc_fill_pt_regs(&regs);
  786. timer_interrupt(&regs);
  787. break;
  788. #if defined(CONFIG_PPC_DOORBELL)
  789. case BOOKE_INTERRUPT_DOORBELL:
  790. kvmppc_fill_pt_regs(&regs);
  791. doorbell_exception(&regs);
  792. break;
  793. #endif
  794. case BOOKE_INTERRUPT_MACHINE_CHECK:
  795. /* FIXME */
  796. break;
  797. case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
  798. kvmppc_fill_pt_regs(&regs);
  799. performance_monitor_exception(&regs);
  800. break;
  801. case BOOKE_INTERRUPT_WATCHDOG:
  802. kvmppc_fill_pt_regs(&regs);
  803. #ifdef CONFIG_BOOKE_WDT
  804. WatchdogException(&regs);
  805. #else
  806. unknown_exception(&regs);
  807. #endif
  808. break;
  809. case BOOKE_INTERRUPT_CRITICAL:
  810. unknown_exception(&regs);
  811. break;
  812. case BOOKE_INTERRUPT_DEBUG:
  813. /* Save DBSR before preemption is enabled */
  814. vcpu->arch.dbsr = mfspr(SPRN_DBSR);
  815. kvmppc_clear_dbsr();
  816. break;
  817. }
  818. }
  819. static int kvmppc_resume_inst_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
  820. enum emulation_result emulated, u32 last_inst)
  821. {
  822. switch (emulated) {
  823. case EMULATE_AGAIN:
  824. return RESUME_GUEST;
  825. case EMULATE_FAIL:
  826. pr_debug("%s: load instruction from guest address %lx failed\n",
  827. __func__, vcpu->arch.pc);
  828. /* For debugging, encode the failing instruction and
  829. * report it to userspace. */
  830. run->hw.hardware_exit_reason = ~0ULL << 32;
  831. run->hw.hardware_exit_reason |= last_inst;
  832. kvmppc_core_queue_program(vcpu, ESR_PIL);
  833. return RESUME_HOST;
  834. default:
  835. BUG();
  836. }
  837. }
  838. /**
  839. * kvmppc_handle_exit
  840. *
  841. * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
  842. */
  843. int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
  844. unsigned int exit_nr)
  845. {
  846. int r = RESUME_HOST;
  847. int s;
  848. int idx;
  849. u32 last_inst = KVM_INST_FETCH_FAILED;
  850. enum emulation_result emulated = EMULATE_DONE;
  851. /* update before a new last_exit_type is rewritten */
  852. kvmppc_update_timing_stats(vcpu);
  853. /* restart interrupts if they were meant for the host */
  854. kvmppc_restart_interrupt(vcpu, exit_nr);
  855. /*
  856. * get last instruction before beeing preempted
  857. * TODO: for e6500 check also BOOKE_INTERRUPT_LRAT_ERROR & ESR_DATA
  858. */
  859. switch (exit_nr) {
  860. case BOOKE_INTERRUPT_DATA_STORAGE:
  861. case BOOKE_INTERRUPT_DTLB_MISS:
  862. case BOOKE_INTERRUPT_HV_PRIV:
  863. emulated = kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
  864. break;
  865. case BOOKE_INTERRUPT_PROGRAM:
  866. /* SW breakpoints arrive as illegal instructions on HV */
  867. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  868. emulated = kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
  869. break;
  870. default:
  871. break;
  872. }
  873. local_irq_enable();
  874. trace_kvm_exit(exit_nr, vcpu);
  875. kvm_guest_exit();
  876. run->exit_reason = KVM_EXIT_UNKNOWN;
  877. run->ready_for_interrupt_injection = 1;
  878. if (emulated != EMULATE_DONE) {
  879. r = kvmppc_resume_inst_load(run, vcpu, emulated, last_inst);
  880. goto out;
  881. }
  882. switch (exit_nr) {
  883. case BOOKE_INTERRUPT_MACHINE_CHECK:
  884. printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
  885. kvmppc_dump_vcpu(vcpu);
  886. /* For debugging, send invalid exit reason to user space */
  887. run->hw.hardware_exit_reason = ~1ULL << 32;
  888. run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR);
  889. r = RESUME_HOST;
  890. break;
  891. case BOOKE_INTERRUPT_EXTERNAL:
  892. kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
  893. r = RESUME_GUEST;
  894. break;
  895. case BOOKE_INTERRUPT_DECREMENTER:
  896. kvmppc_account_exit(vcpu, DEC_EXITS);
  897. r = RESUME_GUEST;
  898. break;
  899. case BOOKE_INTERRUPT_WATCHDOG:
  900. r = RESUME_GUEST;
  901. break;
  902. case BOOKE_INTERRUPT_DOORBELL:
  903. kvmppc_account_exit(vcpu, DBELL_EXITS);
  904. r = RESUME_GUEST;
  905. break;
  906. case BOOKE_INTERRUPT_GUEST_DBELL_CRIT:
  907. kvmppc_account_exit(vcpu, GDBELL_EXITS);
  908. /*
  909. * We are here because there is a pending guest interrupt
  910. * which could not be delivered as MSR_CE or MSR_ME was not
  911. * set. Once we break from here we will retry delivery.
  912. */
  913. r = RESUME_GUEST;
  914. break;
  915. case BOOKE_INTERRUPT_GUEST_DBELL:
  916. kvmppc_account_exit(vcpu, GDBELL_EXITS);
  917. /*
  918. * We are here because there is a pending guest interrupt
  919. * which could not be delivered as MSR_EE was not set. Once
  920. * we break from here we will retry delivery.
  921. */
  922. r = RESUME_GUEST;
  923. break;
  924. case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
  925. r = RESUME_GUEST;
  926. break;
  927. case BOOKE_INTERRUPT_HV_PRIV:
  928. r = emulation_exit(run, vcpu);
  929. break;
  930. case BOOKE_INTERRUPT_PROGRAM:
  931. if ((vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) &&
  932. (last_inst == KVMPPC_INST_SW_BREAKPOINT)) {
  933. /*
  934. * We are here because of an SW breakpoint instr,
  935. * so lets return to host to handle.
  936. */
  937. r = kvmppc_handle_debug(run, vcpu);
  938. run->exit_reason = KVM_EXIT_DEBUG;
  939. kvmppc_account_exit(vcpu, DEBUG_EXITS);
  940. break;
  941. }
  942. if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) {
  943. /*
  944. * Program traps generated by user-level software must
  945. * be handled by the guest kernel.
  946. *
  947. * In GS mode, hypervisor privileged instructions trap
  948. * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are
  949. * actual program interrupts, handled by the guest.
  950. */
  951. kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
  952. r = RESUME_GUEST;
  953. kvmppc_account_exit(vcpu, USR_PR_INST);
  954. break;
  955. }
  956. r = emulation_exit(run, vcpu);
  957. break;
  958. case BOOKE_INTERRUPT_FP_UNAVAIL:
  959. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
  960. kvmppc_account_exit(vcpu, FP_UNAVAIL);
  961. r = RESUME_GUEST;
  962. break;
  963. #ifdef CONFIG_SPE
  964. case BOOKE_INTERRUPT_SPE_UNAVAIL: {
  965. if (vcpu->arch.shared->msr & MSR_SPE)
  966. kvmppc_vcpu_enable_spe(vcpu);
  967. else
  968. kvmppc_booke_queue_irqprio(vcpu,
  969. BOOKE_IRQPRIO_SPE_UNAVAIL);
  970. r = RESUME_GUEST;
  971. break;
  972. }
  973. case BOOKE_INTERRUPT_SPE_FP_DATA:
  974. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
  975. r = RESUME_GUEST;
  976. break;
  977. case BOOKE_INTERRUPT_SPE_FP_ROUND:
  978. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
  979. r = RESUME_GUEST;
  980. break;
  981. #elif defined(CONFIG_SPE_POSSIBLE)
  982. case BOOKE_INTERRUPT_SPE_UNAVAIL:
  983. /*
  984. * Guest wants SPE, but host kernel doesn't support it. Send
  985. * an "unimplemented operation" program check to the guest.
  986. */
  987. kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
  988. r = RESUME_GUEST;
  989. break;
  990. /*
  991. * These really should never happen without CONFIG_SPE,
  992. * as we should never enable the real MSR[SPE] in the guest.
  993. */
  994. case BOOKE_INTERRUPT_SPE_FP_DATA:
  995. case BOOKE_INTERRUPT_SPE_FP_ROUND:
  996. printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
  997. __func__, exit_nr, vcpu->arch.pc);
  998. run->hw.hardware_exit_reason = exit_nr;
  999. r = RESUME_HOST;
  1000. break;
  1001. #endif /* CONFIG_SPE_POSSIBLE */
  1002. /*
  1003. * On cores with Vector category, KVM is loaded only if CONFIG_ALTIVEC,
  1004. * see kvmppc_core_check_processor_compat().
  1005. */
  1006. #ifdef CONFIG_ALTIVEC
  1007. case BOOKE_INTERRUPT_ALTIVEC_UNAVAIL:
  1008. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALTIVEC_UNAVAIL);
  1009. r = RESUME_GUEST;
  1010. break;
  1011. case BOOKE_INTERRUPT_ALTIVEC_ASSIST:
  1012. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALTIVEC_ASSIST);
  1013. r = RESUME_GUEST;
  1014. break;
  1015. #endif
  1016. case BOOKE_INTERRUPT_DATA_STORAGE:
  1017. kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
  1018. vcpu->arch.fault_esr);
  1019. kvmppc_account_exit(vcpu, DSI_EXITS);
  1020. r = RESUME_GUEST;
  1021. break;
  1022. case BOOKE_INTERRUPT_INST_STORAGE:
  1023. kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
  1024. kvmppc_account_exit(vcpu, ISI_EXITS);
  1025. r = RESUME_GUEST;
  1026. break;
  1027. case BOOKE_INTERRUPT_ALIGNMENT:
  1028. kvmppc_core_queue_alignment(vcpu, vcpu->arch.fault_dear,
  1029. vcpu->arch.fault_esr);
  1030. r = RESUME_GUEST;
  1031. break;
  1032. #ifdef CONFIG_KVM_BOOKE_HV
  1033. case BOOKE_INTERRUPT_HV_SYSCALL:
  1034. if (!(vcpu->arch.shared->msr & MSR_PR)) {
  1035. kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
  1036. } else {
  1037. /*
  1038. * hcall from guest userspace -- send privileged
  1039. * instruction program check.
  1040. */
  1041. kvmppc_core_queue_program(vcpu, ESR_PPR);
  1042. }
  1043. r = RESUME_GUEST;
  1044. break;
  1045. #else
  1046. case BOOKE_INTERRUPT_SYSCALL:
  1047. if (!(vcpu->arch.shared->msr & MSR_PR) &&
  1048. (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
  1049. /* KVM PV hypercalls */
  1050. kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
  1051. r = RESUME_GUEST;
  1052. } else {
  1053. /* Guest syscalls */
  1054. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
  1055. }
  1056. kvmppc_account_exit(vcpu, SYSCALL_EXITS);
  1057. r = RESUME_GUEST;
  1058. break;
  1059. #endif
  1060. case BOOKE_INTERRUPT_DTLB_MISS: {
  1061. unsigned long eaddr = vcpu->arch.fault_dear;
  1062. int gtlb_index;
  1063. gpa_t gpaddr;
  1064. gfn_t gfn;
  1065. #ifdef CONFIG_KVM_E500V2
  1066. if (!(vcpu->arch.shared->msr & MSR_PR) &&
  1067. (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
  1068. kvmppc_map_magic(vcpu);
  1069. kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
  1070. r = RESUME_GUEST;
  1071. break;
  1072. }
  1073. #endif
  1074. /* Check the guest TLB. */
  1075. gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
  1076. if (gtlb_index < 0) {
  1077. /* The guest didn't have a mapping for it. */
  1078. kvmppc_core_queue_dtlb_miss(vcpu,
  1079. vcpu->arch.fault_dear,
  1080. vcpu->arch.fault_esr);
  1081. kvmppc_mmu_dtlb_miss(vcpu);
  1082. kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
  1083. r = RESUME_GUEST;
  1084. break;
  1085. }
  1086. idx = srcu_read_lock(&vcpu->kvm->srcu);
  1087. gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
  1088. gfn = gpaddr >> PAGE_SHIFT;
  1089. if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
  1090. /* The guest TLB had a mapping, but the shadow TLB
  1091. * didn't, and it is RAM. This could be because:
  1092. * a) the entry is mapping the host kernel, or
  1093. * b) the guest used a large mapping which we're faking
  1094. * Either way, we need to satisfy the fault without
  1095. * invoking the guest. */
  1096. kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
  1097. kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
  1098. r = RESUME_GUEST;
  1099. } else {
  1100. /* Guest has mapped and accessed a page which is not
  1101. * actually RAM. */
  1102. vcpu->arch.paddr_accessed = gpaddr;
  1103. vcpu->arch.vaddr_accessed = eaddr;
  1104. r = kvmppc_emulate_mmio(run, vcpu);
  1105. kvmppc_account_exit(vcpu, MMIO_EXITS);
  1106. }
  1107. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  1108. break;
  1109. }
  1110. case BOOKE_INTERRUPT_ITLB_MISS: {
  1111. unsigned long eaddr = vcpu->arch.pc;
  1112. gpa_t gpaddr;
  1113. gfn_t gfn;
  1114. int gtlb_index;
  1115. r = RESUME_GUEST;
  1116. /* Check the guest TLB. */
  1117. gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
  1118. if (gtlb_index < 0) {
  1119. /* The guest didn't have a mapping for it. */
  1120. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
  1121. kvmppc_mmu_itlb_miss(vcpu);
  1122. kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
  1123. break;
  1124. }
  1125. kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
  1126. idx = srcu_read_lock(&vcpu->kvm->srcu);
  1127. gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
  1128. gfn = gpaddr >> PAGE_SHIFT;
  1129. if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
  1130. /* The guest TLB had a mapping, but the shadow TLB
  1131. * didn't. This could be because:
  1132. * a) the entry is mapping the host kernel, or
  1133. * b) the guest used a large mapping which we're faking
  1134. * Either way, we need to satisfy the fault without
  1135. * invoking the guest. */
  1136. kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
  1137. } else {
  1138. /* Guest mapped and leaped at non-RAM! */
  1139. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
  1140. }
  1141. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  1142. break;
  1143. }
  1144. case BOOKE_INTERRUPT_DEBUG: {
  1145. r = kvmppc_handle_debug(run, vcpu);
  1146. if (r == RESUME_HOST)
  1147. run->exit_reason = KVM_EXIT_DEBUG;
  1148. kvmppc_account_exit(vcpu, DEBUG_EXITS);
  1149. break;
  1150. }
  1151. default:
  1152. printk(KERN_EMERG "exit_nr %d\n", exit_nr);
  1153. BUG();
  1154. }
  1155. out:
  1156. /*
  1157. * To avoid clobbering exit_reason, only check for signals if we
  1158. * aren't already exiting to userspace for some other reason.
  1159. */
  1160. if (!(r & RESUME_HOST)) {
  1161. s = kvmppc_prepare_to_enter(vcpu);
  1162. if (s <= 0)
  1163. r = (s << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
  1164. else {
  1165. /* interrupts now hard-disabled */
  1166. kvmppc_fix_ee_before_entry();
  1167. kvmppc_load_guest_fp(vcpu);
  1168. kvmppc_load_guest_altivec(vcpu);
  1169. }
  1170. }
  1171. return r;
  1172. }
  1173. static void kvmppc_set_tsr(struct kvm_vcpu *vcpu, u32 new_tsr)
  1174. {
  1175. u32 old_tsr = vcpu->arch.tsr;
  1176. vcpu->arch.tsr = new_tsr;
  1177. if ((old_tsr ^ vcpu->arch.tsr) & (TSR_ENW | TSR_WIS))
  1178. arm_next_watchdog(vcpu);
  1179. update_timer_ints(vcpu);
  1180. }
  1181. /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
  1182. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
  1183. {
  1184. int i;
  1185. int r;
  1186. vcpu->arch.pc = 0;
  1187. vcpu->arch.shared->pir = vcpu->vcpu_id;
  1188. kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
  1189. kvmppc_set_msr(vcpu, 0);
  1190. #ifndef CONFIG_KVM_BOOKE_HV
  1191. vcpu->arch.shadow_msr = MSR_USER | MSR_IS | MSR_DS;
  1192. vcpu->arch.shadow_pid = 1;
  1193. vcpu->arch.shared->msr = 0;
  1194. #endif
  1195. /* Eye-catching numbers so we know if the guest takes an interrupt
  1196. * before it's programmed its own IVPR/IVORs. */
  1197. vcpu->arch.ivpr = 0x55550000;
  1198. for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
  1199. vcpu->arch.ivor[i] = 0x7700 | i * 4;
  1200. kvmppc_init_timing_stats(vcpu);
  1201. r = kvmppc_core_vcpu_setup(vcpu);
  1202. kvmppc_sanity_check(vcpu);
  1203. return r;
  1204. }
  1205. int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu)
  1206. {
  1207. /* setup watchdog timer once */
  1208. spin_lock_init(&vcpu->arch.wdt_lock);
  1209. setup_timer(&vcpu->arch.wdt_timer, kvmppc_watchdog_func,
  1210. (unsigned long)vcpu);
  1211. /*
  1212. * Clear DBSR.MRR to avoid guest debug interrupt as
  1213. * this is of host interest
  1214. */
  1215. mtspr(SPRN_DBSR, DBSR_MRR);
  1216. return 0;
  1217. }
  1218. void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu)
  1219. {
  1220. del_timer_sync(&vcpu->arch.wdt_timer);
  1221. }
  1222. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  1223. {
  1224. int i;
  1225. regs->pc = vcpu->arch.pc;
  1226. regs->cr = kvmppc_get_cr(vcpu);
  1227. regs->ctr = vcpu->arch.ctr;
  1228. regs->lr = vcpu->arch.lr;
  1229. regs->xer = kvmppc_get_xer(vcpu);
  1230. regs->msr = vcpu->arch.shared->msr;
  1231. regs->srr0 = kvmppc_get_srr0(vcpu);
  1232. regs->srr1 = kvmppc_get_srr1(vcpu);
  1233. regs->pid = vcpu->arch.pid;
  1234. regs->sprg0 = kvmppc_get_sprg0(vcpu);
  1235. regs->sprg1 = kvmppc_get_sprg1(vcpu);
  1236. regs->sprg2 = kvmppc_get_sprg2(vcpu);
  1237. regs->sprg3 = kvmppc_get_sprg3(vcpu);
  1238. regs->sprg4 = kvmppc_get_sprg4(vcpu);
  1239. regs->sprg5 = kvmppc_get_sprg5(vcpu);
  1240. regs->sprg6 = kvmppc_get_sprg6(vcpu);
  1241. regs->sprg7 = kvmppc_get_sprg7(vcpu);
  1242. for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
  1243. regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
  1244. return 0;
  1245. }
  1246. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  1247. {
  1248. int i;
  1249. vcpu->arch.pc = regs->pc;
  1250. kvmppc_set_cr(vcpu, regs->cr);
  1251. vcpu->arch.ctr = regs->ctr;
  1252. vcpu->arch.lr = regs->lr;
  1253. kvmppc_set_xer(vcpu, regs->xer);
  1254. kvmppc_set_msr(vcpu, regs->msr);
  1255. kvmppc_set_srr0(vcpu, regs->srr0);
  1256. kvmppc_set_srr1(vcpu, regs->srr1);
  1257. kvmppc_set_pid(vcpu, regs->pid);
  1258. kvmppc_set_sprg0(vcpu, regs->sprg0);
  1259. kvmppc_set_sprg1(vcpu, regs->sprg1);
  1260. kvmppc_set_sprg2(vcpu, regs->sprg2);
  1261. kvmppc_set_sprg3(vcpu, regs->sprg3);
  1262. kvmppc_set_sprg4(vcpu, regs->sprg4);
  1263. kvmppc_set_sprg5(vcpu, regs->sprg5);
  1264. kvmppc_set_sprg6(vcpu, regs->sprg6);
  1265. kvmppc_set_sprg7(vcpu, regs->sprg7);
  1266. for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
  1267. kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
  1268. return 0;
  1269. }
  1270. static void get_sregs_base(struct kvm_vcpu *vcpu,
  1271. struct kvm_sregs *sregs)
  1272. {
  1273. u64 tb = get_tb();
  1274. sregs->u.e.features |= KVM_SREGS_E_BASE;
  1275. sregs->u.e.csrr0 = vcpu->arch.csrr0;
  1276. sregs->u.e.csrr1 = vcpu->arch.csrr1;
  1277. sregs->u.e.mcsr = vcpu->arch.mcsr;
  1278. sregs->u.e.esr = kvmppc_get_esr(vcpu);
  1279. sregs->u.e.dear = kvmppc_get_dar(vcpu);
  1280. sregs->u.e.tsr = vcpu->arch.tsr;
  1281. sregs->u.e.tcr = vcpu->arch.tcr;
  1282. sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
  1283. sregs->u.e.tb = tb;
  1284. sregs->u.e.vrsave = vcpu->arch.vrsave;
  1285. }
  1286. static int set_sregs_base(struct kvm_vcpu *vcpu,
  1287. struct kvm_sregs *sregs)
  1288. {
  1289. if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
  1290. return 0;
  1291. vcpu->arch.csrr0 = sregs->u.e.csrr0;
  1292. vcpu->arch.csrr1 = sregs->u.e.csrr1;
  1293. vcpu->arch.mcsr = sregs->u.e.mcsr;
  1294. kvmppc_set_esr(vcpu, sregs->u.e.esr);
  1295. kvmppc_set_dar(vcpu, sregs->u.e.dear);
  1296. vcpu->arch.vrsave = sregs->u.e.vrsave;
  1297. kvmppc_set_tcr(vcpu, sregs->u.e.tcr);
  1298. if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) {
  1299. vcpu->arch.dec = sregs->u.e.dec;
  1300. kvmppc_emulate_dec(vcpu);
  1301. }
  1302. if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR)
  1303. kvmppc_set_tsr(vcpu, sregs->u.e.tsr);
  1304. return 0;
  1305. }
  1306. static void get_sregs_arch206(struct kvm_vcpu *vcpu,
  1307. struct kvm_sregs *sregs)
  1308. {
  1309. sregs->u.e.features |= KVM_SREGS_E_ARCH206;
  1310. sregs->u.e.pir = vcpu->vcpu_id;
  1311. sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
  1312. sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
  1313. sregs->u.e.decar = vcpu->arch.decar;
  1314. sregs->u.e.ivpr = vcpu->arch.ivpr;
  1315. }
  1316. static int set_sregs_arch206(struct kvm_vcpu *vcpu,
  1317. struct kvm_sregs *sregs)
  1318. {
  1319. if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
  1320. return 0;
  1321. if (sregs->u.e.pir != vcpu->vcpu_id)
  1322. return -EINVAL;
  1323. vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
  1324. vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
  1325. vcpu->arch.decar = sregs->u.e.decar;
  1326. vcpu->arch.ivpr = sregs->u.e.ivpr;
  1327. return 0;
  1328. }
  1329. int kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  1330. {
  1331. sregs->u.e.features |= KVM_SREGS_E_IVOR;
  1332. sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
  1333. sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
  1334. sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
  1335. sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
  1336. sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
  1337. sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
  1338. sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
  1339. sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
  1340. sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
  1341. sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
  1342. sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
  1343. sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
  1344. sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
  1345. sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
  1346. sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
  1347. sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
  1348. return 0;
  1349. }
  1350. int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  1351. {
  1352. if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
  1353. return 0;
  1354. vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
  1355. vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
  1356. vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
  1357. vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
  1358. vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
  1359. vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
  1360. vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
  1361. vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
  1362. vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
  1363. vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
  1364. vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
  1365. vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
  1366. vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
  1367. vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
  1368. vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
  1369. vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
  1370. return 0;
  1371. }
  1372. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  1373. struct kvm_sregs *sregs)
  1374. {
  1375. sregs->pvr = vcpu->arch.pvr;
  1376. get_sregs_base(vcpu, sregs);
  1377. get_sregs_arch206(vcpu, sregs);
  1378. return vcpu->kvm->arch.kvm_ops->get_sregs(vcpu, sregs);
  1379. }
  1380. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  1381. struct kvm_sregs *sregs)
  1382. {
  1383. int ret;
  1384. if (vcpu->arch.pvr != sregs->pvr)
  1385. return -EINVAL;
  1386. ret = set_sregs_base(vcpu, sregs);
  1387. if (ret < 0)
  1388. return ret;
  1389. ret = set_sregs_arch206(vcpu, sregs);
  1390. if (ret < 0)
  1391. return ret;
  1392. return vcpu->kvm->arch.kvm_ops->set_sregs(vcpu, sregs);
  1393. }
  1394. int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id,
  1395. union kvmppc_one_reg *val)
  1396. {
  1397. int r = 0;
  1398. switch (id) {
  1399. case KVM_REG_PPC_IAC1:
  1400. *val = get_reg_val(id, vcpu->arch.dbg_reg.iac1);
  1401. break;
  1402. case KVM_REG_PPC_IAC2:
  1403. *val = get_reg_val(id, vcpu->arch.dbg_reg.iac2);
  1404. break;
  1405. #if CONFIG_PPC_ADV_DEBUG_IACS > 2
  1406. case KVM_REG_PPC_IAC3:
  1407. *val = get_reg_val(id, vcpu->arch.dbg_reg.iac3);
  1408. break;
  1409. case KVM_REG_PPC_IAC4:
  1410. *val = get_reg_val(id, vcpu->arch.dbg_reg.iac4);
  1411. break;
  1412. #endif
  1413. case KVM_REG_PPC_DAC1:
  1414. *val = get_reg_val(id, vcpu->arch.dbg_reg.dac1);
  1415. break;
  1416. case KVM_REG_PPC_DAC2:
  1417. *val = get_reg_val(id, vcpu->arch.dbg_reg.dac2);
  1418. break;
  1419. case KVM_REG_PPC_EPR: {
  1420. u32 epr = kvmppc_get_epr(vcpu);
  1421. *val = get_reg_val(id, epr);
  1422. break;
  1423. }
  1424. #if defined(CONFIG_64BIT)
  1425. case KVM_REG_PPC_EPCR:
  1426. *val = get_reg_val(id, vcpu->arch.epcr);
  1427. break;
  1428. #endif
  1429. case KVM_REG_PPC_TCR:
  1430. *val = get_reg_val(id, vcpu->arch.tcr);
  1431. break;
  1432. case KVM_REG_PPC_TSR:
  1433. *val = get_reg_val(id, vcpu->arch.tsr);
  1434. break;
  1435. case KVM_REG_PPC_DEBUG_INST:
  1436. *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
  1437. break;
  1438. case KVM_REG_PPC_VRSAVE:
  1439. *val = get_reg_val(id, vcpu->arch.vrsave);
  1440. break;
  1441. default:
  1442. r = vcpu->kvm->arch.kvm_ops->get_one_reg(vcpu, id, val);
  1443. break;
  1444. }
  1445. return r;
  1446. }
  1447. int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id,
  1448. union kvmppc_one_reg *val)
  1449. {
  1450. int r = 0;
  1451. switch (id) {
  1452. case KVM_REG_PPC_IAC1:
  1453. vcpu->arch.dbg_reg.iac1 = set_reg_val(id, *val);
  1454. break;
  1455. case KVM_REG_PPC_IAC2:
  1456. vcpu->arch.dbg_reg.iac2 = set_reg_val(id, *val);
  1457. break;
  1458. #if CONFIG_PPC_ADV_DEBUG_IACS > 2
  1459. case KVM_REG_PPC_IAC3:
  1460. vcpu->arch.dbg_reg.iac3 = set_reg_val(id, *val);
  1461. break;
  1462. case KVM_REG_PPC_IAC4:
  1463. vcpu->arch.dbg_reg.iac4 = set_reg_val(id, *val);
  1464. break;
  1465. #endif
  1466. case KVM_REG_PPC_DAC1:
  1467. vcpu->arch.dbg_reg.dac1 = set_reg_val(id, *val);
  1468. break;
  1469. case KVM_REG_PPC_DAC2:
  1470. vcpu->arch.dbg_reg.dac2 = set_reg_val(id, *val);
  1471. break;
  1472. case KVM_REG_PPC_EPR: {
  1473. u32 new_epr = set_reg_val(id, *val);
  1474. kvmppc_set_epr(vcpu, new_epr);
  1475. break;
  1476. }
  1477. #if defined(CONFIG_64BIT)
  1478. case KVM_REG_PPC_EPCR: {
  1479. u32 new_epcr = set_reg_val(id, *val);
  1480. kvmppc_set_epcr(vcpu, new_epcr);
  1481. break;
  1482. }
  1483. #endif
  1484. case KVM_REG_PPC_OR_TSR: {
  1485. u32 tsr_bits = set_reg_val(id, *val);
  1486. kvmppc_set_tsr_bits(vcpu, tsr_bits);
  1487. break;
  1488. }
  1489. case KVM_REG_PPC_CLEAR_TSR: {
  1490. u32 tsr_bits = set_reg_val(id, *val);
  1491. kvmppc_clr_tsr_bits(vcpu, tsr_bits);
  1492. break;
  1493. }
  1494. case KVM_REG_PPC_TSR: {
  1495. u32 tsr = set_reg_val(id, *val);
  1496. kvmppc_set_tsr(vcpu, tsr);
  1497. break;
  1498. }
  1499. case KVM_REG_PPC_TCR: {
  1500. u32 tcr = set_reg_val(id, *val);
  1501. kvmppc_set_tcr(vcpu, tcr);
  1502. break;
  1503. }
  1504. case KVM_REG_PPC_VRSAVE:
  1505. vcpu->arch.vrsave = set_reg_val(id, *val);
  1506. break;
  1507. default:
  1508. r = vcpu->kvm->arch.kvm_ops->set_one_reg(vcpu, id, val);
  1509. break;
  1510. }
  1511. return r;
  1512. }
  1513. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  1514. {
  1515. return -ENOTSUPP;
  1516. }
  1517. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  1518. {
  1519. return -ENOTSUPP;
  1520. }
  1521. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  1522. struct kvm_translation *tr)
  1523. {
  1524. int r;
  1525. r = kvmppc_core_vcpu_translate(vcpu, tr);
  1526. return r;
  1527. }
  1528. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  1529. {
  1530. return -ENOTSUPP;
  1531. }
  1532. void kvmppc_core_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
  1533. struct kvm_memory_slot *dont)
  1534. {
  1535. }
  1536. int kvmppc_core_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
  1537. unsigned long npages)
  1538. {
  1539. return 0;
  1540. }
  1541. int kvmppc_core_prepare_memory_region(struct kvm *kvm,
  1542. struct kvm_memory_slot *memslot,
  1543. struct kvm_userspace_memory_region *mem)
  1544. {
  1545. return 0;
  1546. }
  1547. void kvmppc_core_commit_memory_region(struct kvm *kvm,
  1548. struct kvm_userspace_memory_region *mem,
  1549. const struct kvm_memory_slot *old)
  1550. {
  1551. }
  1552. void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
  1553. {
  1554. }
  1555. void kvmppc_set_epcr(struct kvm_vcpu *vcpu, u32 new_epcr)
  1556. {
  1557. #if defined(CONFIG_64BIT)
  1558. vcpu->arch.epcr = new_epcr;
  1559. #ifdef CONFIG_KVM_BOOKE_HV
  1560. vcpu->arch.shadow_epcr &= ~SPRN_EPCR_GICM;
  1561. if (vcpu->arch.epcr & SPRN_EPCR_ICM)
  1562. vcpu->arch.shadow_epcr |= SPRN_EPCR_GICM;
  1563. #endif
  1564. #endif
  1565. }
  1566. void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr)
  1567. {
  1568. vcpu->arch.tcr = new_tcr;
  1569. arm_next_watchdog(vcpu);
  1570. update_timer_ints(vcpu);
  1571. }
  1572. void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
  1573. {
  1574. set_bits(tsr_bits, &vcpu->arch.tsr);
  1575. smp_wmb();
  1576. kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
  1577. kvm_vcpu_kick(vcpu);
  1578. }
  1579. void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
  1580. {
  1581. clear_bits(tsr_bits, &vcpu->arch.tsr);
  1582. /*
  1583. * We may have stopped the watchdog due to
  1584. * being stuck on final expiration.
  1585. */
  1586. if (tsr_bits & (TSR_ENW | TSR_WIS))
  1587. arm_next_watchdog(vcpu);
  1588. update_timer_ints(vcpu);
  1589. }
  1590. void kvmppc_decrementer_func(struct kvm_vcpu *vcpu)
  1591. {
  1592. if (vcpu->arch.tcr & TCR_ARE) {
  1593. vcpu->arch.dec = vcpu->arch.decar;
  1594. kvmppc_emulate_dec(vcpu);
  1595. }
  1596. kvmppc_set_tsr_bits(vcpu, TSR_DIS);
  1597. }
  1598. static int kvmppc_booke_add_breakpoint(struct debug_reg *dbg_reg,
  1599. uint64_t addr, int index)
  1600. {
  1601. switch (index) {
  1602. case 0:
  1603. dbg_reg->dbcr0 |= DBCR0_IAC1;
  1604. dbg_reg->iac1 = addr;
  1605. break;
  1606. case 1:
  1607. dbg_reg->dbcr0 |= DBCR0_IAC2;
  1608. dbg_reg->iac2 = addr;
  1609. break;
  1610. #if CONFIG_PPC_ADV_DEBUG_IACS > 2
  1611. case 2:
  1612. dbg_reg->dbcr0 |= DBCR0_IAC3;
  1613. dbg_reg->iac3 = addr;
  1614. break;
  1615. case 3:
  1616. dbg_reg->dbcr0 |= DBCR0_IAC4;
  1617. dbg_reg->iac4 = addr;
  1618. break;
  1619. #endif
  1620. default:
  1621. return -EINVAL;
  1622. }
  1623. dbg_reg->dbcr0 |= DBCR0_IDM;
  1624. return 0;
  1625. }
  1626. static int kvmppc_booke_add_watchpoint(struct debug_reg *dbg_reg, uint64_t addr,
  1627. int type, int index)
  1628. {
  1629. switch (index) {
  1630. case 0:
  1631. if (type & KVMPPC_DEBUG_WATCH_READ)
  1632. dbg_reg->dbcr0 |= DBCR0_DAC1R;
  1633. if (type & KVMPPC_DEBUG_WATCH_WRITE)
  1634. dbg_reg->dbcr0 |= DBCR0_DAC1W;
  1635. dbg_reg->dac1 = addr;
  1636. break;
  1637. case 1:
  1638. if (type & KVMPPC_DEBUG_WATCH_READ)
  1639. dbg_reg->dbcr0 |= DBCR0_DAC2R;
  1640. if (type & KVMPPC_DEBUG_WATCH_WRITE)
  1641. dbg_reg->dbcr0 |= DBCR0_DAC2W;
  1642. dbg_reg->dac2 = addr;
  1643. break;
  1644. default:
  1645. return -EINVAL;
  1646. }
  1647. dbg_reg->dbcr0 |= DBCR0_IDM;
  1648. return 0;
  1649. }
  1650. void kvm_guest_protect_msr(struct kvm_vcpu *vcpu, ulong prot_bitmap, bool set)
  1651. {
  1652. /* XXX: Add similar MSR protection for BookE-PR */
  1653. #ifdef CONFIG_KVM_BOOKE_HV
  1654. BUG_ON(prot_bitmap & ~(MSRP_UCLEP | MSRP_DEP | MSRP_PMMP));
  1655. if (set) {
  1656. if (prot_bitmap & MSR_UCLE)
  1657. vcpu->arch.shadow_msrp |= MSRP_UCLEP;
  1658. if (prot_bitmap & MSR_DE)
  1659. vcpu->arch.shadow_msrp |= MSRP_DEP;
  1660. if (prot_bitmap & MSR_PMM)
  1661. vcpu->arch.shadow_msrp |= MSRP_PMMP;
  1662. } else {
  1663. if (prot_bitmap & MSR_UCLE)
  1664. vcpu->arch.shadow_msrp &= ~MSRP_UCLEP;
  1665. if (prot_bitmap & MSR_DE)
  1666. vcpu->arch.shadow_msrp &= ~MSRP_DEP;
  1667. if (prot_bitmap & MSR_PMM)
  1668. vcpu->arch.shadow_msrp &= ~MSRP_PMMP;
  1669. }
  1670. #endif
  1671. }
  1672. int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, enum xlate_instdata xlid,
  1673. enum xlate_readwrite xlrw, struct kvmppc_pte *pte)
  1674. {
  1675. int gtlb_index;
  1676. gpa_t gpaddr;
  1677. #ifdef CONFIG_KVM_E500V2
  1678. if (!(vcpu->arch.shared->msr & MSR_PR) &&
  1679. (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
  1680. pte->eaddr = eaddr;
  1681. pte->raddr = (vcpu->arch.magic_page_pa & PAGE_MASK) |
  1682. (eaddr & ~PAGE_MASK);
  1683. pte->vpage = eaddr >> PAGE_SHIFT;
  1684. pte->may_read = true;
  1685. pte->may_write = true;
  1686. pte->may_execute = true;
  1687. return 0;
  1688. }
  1689. #endif
  1690. /* Check the guest TLB. */
  1691. switch (xlid) {
  1692. case XLATE_INST:
  1693. gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
  1694. break;
  1695. case XLATE_DATA:
  1696. gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
  1697. break;
  1698. default:
  1699. BUG();
  1700. }
  1701. /* Do we have a TLB entry at all? */
  1702. if (gtlb_index < 0)
  1703. return -ENOENT;
  1704. gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
  1705. pte->eaddr = eaddr;
  1706. pte->raddr = (gpaddr & PAGE_MASK) | (eaddr & ~PAGE_MASK);
  1707. pte->vpage = eaddr >> PAGE_SHIFT;
  1708. /* XXX read permissions from the guest TLB */
  1709. pte->may_read = true;
  1710. pte->may_write = true;
  1711. pte->may_execute = true;
  1712. return 0;
  1713. }
  1714. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  1715. struct kvm_guest_debug *dbg)
  1716. {
  1717. struct debug_reg *dbg_reg;
  1718. int n, b = 0, w = 0;
  1719. if (!(dbg->control & KVM_GUESTDBG_ENABLE)) {
  1720. vcpu->arch.dbg_reg.dbcr0 = 0;
  1721. vcpu->guest_debug = 0;
  1722. kvm_guest_protect_msr(vcpu, MSR_DE, false);
  1723. return 0;
  1724. }
  1725. kvm_guest_protect_msr(vcpu, MSR_DE, true);
  1726. vcpu->guest_debug = dbg->control;
  1727. vcpu->arch.dbg_reg.dbcr0 = 0;
  1728. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  1729. vcpu->arch.dbg_reg.dbcr0 |= DBCR0_IDM | DBCR0_IC;
  1730. /* Code below handles only HW breakpoints */
  1731. dbg_reg = &(vcpu->arch.dbg_reg);
  1732. #ifdef CONFIG_KVM_BOOKE_HV
  1733. /*
  1734. * On BookE-HV (e500mc) the guest is always executed with MSR.GS=1
  1735. * DBCR1 and DBCR2 are set to trigger debug events when MSR.PR is 0
  1736. */
  1737. dbg_reg->dbcr1 = 0;
  1738. dbg_reg->dbcr2 = 0;
  1739. #else
  1740. /*
  1741. * On BookE-PR (e500v2) the guest is always executed with MSR.PR=1
  1742. * We set DBCR1 and DBCR2 to only trigger debug events when MSR.PR
  1743. * is set.
  1744. */
  1745. dbg_reg->dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US | DBCR1_IAC3US |
  1746. DBCR1_IAC4US;
  1747. dbg_reg->dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US;
  1748. #endif
  1749. if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
  1750. return 0;
  1751. for (n = 0; n < (KVMPPC_BOOKE_IAC_NUM + KVMPPC_BOOKE_DAC_NUM); n++) {
  1752. uint64_t addr = dbg->arch.bp[n].addr;
  1753. uint32_t type = dbg->arch.bp[n].type;
  1754. if (type == KVMPPC_DEBUG_NONE)
  1755. continue;
  1756. if (type & !(KVMPPC_DEBUG_WATCH_READ |
  1757. KVMPPC_DEBUG_WATCH_WRITE |
  1758. KVMPPC_DEBUG_BREAKPOINT))
  1759. return -EINVAL;
  1760. if (type & KVMPPC_DEBUG_BREAKPOINT) {
  1761. /* Setting H/W breakpoint */
  1762. if (kvmppc_booke_add_breakpoint(dbg_reg, addr, b++))
  1763. return -EINVAL;
  1764. } else {
  1765. /* Setting H/W watchpoint */
  1766. if (kvmppc_booke_add_watchpoint(dbg_reg, addr,
  1767. type, w++))
  1768. return -EINVAL;
  1769. }
  1770. }
  1771. return 0;
  1772. }
  1773. void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1774. {
  1775. vcpu->cpu = smp_processor_id();
  1776. current->thread.kvm_vcpu = vcpu;
  1777. }
  1778. void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu)
  1779. {
  1780. current->thread.kvm_vcpu = NULL;
  1781. vcpu->cpu = -1;
  1782. /* Clear pending debug event in DBSR */
  1783. kvmppc_clear_dbsr();
  1784. }
  1785. void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
  1786. {
  1787. vcpu->kvm->arch.kvm_ops->mmu_destroy(vcpu);
  1788. }
  1789. int kvmppc_core_init_vm(struct kvm *kvm)
  1790. {
  1791. return kvm->arch.kvm_ops->init_vm(kvm);
  1792. }
  1793. struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
  1794. {
  1795. return kvm->arch.kvm_ops->vcpu_create(kvm, id);
  1796. }
  1797. void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
  1798. {
  1799. vcpu->kvm->arch.kvm_ops->vcpu_free(vcpu);
  1800. }
  1801. void kvmppc_core_destroy_vm(struct kvm *kvm)
  1802. {
  1803. kvm->arch.kvm_ops->destroy_vm(kvm);
  1804. }
  1805. void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1806. {
  1807. vcpu->kvm->arch.kvm_ops->vcpu_load(vcpu, cpu);
  1808. }
  1809. void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
  1810. {
  1811. vcpu->kvm->arch.kvm_ops->vcpu_put(vcpu);
  1812. }
  1813. int __init kvmppc_booke_init(void)
  1814. {
  1815. #ifndef CONFIG_KVM_BOOKE_HV
  1816. unsigned long ivor[16];
  1817. unsigned long *handler = kvmppc_booke_handler_addr;
  1818. unsigned long max_ivor = 0;
  1819. unsigned long handler_len;
  1820. int i;
  1821. /* We install our own exception handlers by hijacking IVPR. IVPR must
  1822. * be 16-bit aligned, so we need a 64KB allocation. */
  1823. kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
  1824. VCPU_SIZE_ORDER);
  1825. if (!kvmppc_booke_handlers)
  1826. return -ENOMEM;
  1827. /* XXX make sure our handlers are smaller than Linux's */
  1828. /* Copy our interrupt handlers to match host IVORs. That way we don't
  1829. * have to swap the IVORs on every guest/host transition. */
  1830. ivor[0] = mfspr(SPRN_IVOR0);
  1831. ivor[1] = mfspr(SPRN_IVOR1);
  1832. ivor[2] = mfspr(SPRN_IVOR2);
  1833. ivor[3] = mfspr(SPRN_IVOR3);
  1834. ivor[4] = mfspr(SPRN_IVOR4);
  1835. ivor[5] = mfspr(SPRN_IVOR5);
  1836. ivor[6] = mfspr(SPRN_IVOR6);
  1837. ivor[7] = mfspr(SPRN_IVOR7);
  1838. ivor[8] = mfspr(SPRN_IVOR8);
  1839. ivor[9] = mfspr(SPRN_IVOR9);
  1840. ivor[10] = mfspr(SPRN_IVOR10);
  1841. ivor[11] = mfspr(SPRN_IVOR11);
  1842. ivor[12] = mfspr(SPRN_IVOR12);
  1843. ivor[13] = mfspr(SPRN_IVOR13);
  1844. ivor[14] = mfspr(SPRN_IVOR14);
  1845. ivor[15] = mfspr(SPRN_IVOR15);
  1846. for (i = 0; i < 16; i++) {
  1847. if (ivor[i] > max_ivor)
  1848. max_ivor = i;
  1849. handler_len = handler[i + 1] - handler[i];
  1850. memcpy((void *)kvmppc_booke_handlers + ivor[i],
  1851. (void *)handler[i], handler_len);
  1852. }
  1853. handler_len = handler[max_ivor + 1] - handler[max_ivor];
  1854. flush_icache_range(kvmppc_booke_handlers, kvmppc_booke_handlers +
  1855. ivor[max_ivor] + handler_len);
  1856. #endif /* !BOOKE_HV */
  1857. return 0;
  1858. }
  1859. void __exit kvmppc_booke_exit(void)
  1860. {
  1861. free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
  1862. kvm_exit();
  1863. }