booke.c 56 KB

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