booke.c 55 KB

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