booke.c 55 KB

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