booke.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668
  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. #include "trace.h"
  41. unsigned long kvmppc_booke_handlers;
  42. #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
  43. #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
  44. struct kvm_stats_debugfs_item debugfs_entries[] = {
  45. { "mmio", VCPU_STAT(mmio_exits) },
  46. { "dcr", VCPU_STAT(dcr_exits) },
  47. { "sig", VCPU_STAT(signal_exits) },
  48. { "itlb_r", VCPU_STAT(itlb_real_miss_exits) },
  49. { "itlb_v", VCPU_STAT(itlb_virt_miss_exits) },
  50. { "dtlb_r", VCPU_STAT(dtlb_real_miss_exits) },
  51. { "dtlb_v", VCPU_STAT(dtlb_virt_miss_exits) },
  52. { "sysc", VCPU_STAT(syscall_exits) },
  53. { "isi", VCPU_STAT(isi_exits) },
  54. { "dsi", VCPU_STAT(dsi_exits) },
  55. { "inst_emu", VCPU_STAT(emulated_inst_exits) },
  56. { "dec", VCPU_STAT(dec_exits) },
  57. { "ext_intr", VCPU_STAT(ext_intr_exits) },
  58. { "halt_wakeup", VCPU_STAT(halt_wakeup) },
  59. { "doorbell", VCPU_STAT(dbell_exits) },
  60. { "guest doorbell", VCPU_STAT(gdbell_exits) },
  61. { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
  62. { NULL }
  63. };
  64. /* TODO: use vcpu_printf() */
  65. void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
  66. {
  67. int i;
  68. printk("pc: %08lx msr: %08llx\n", vcpu->arch.pc, vcpu->arch.shared->msr);
  69. printk("lr: %08lx ctr: %08lx\n", vcpu->arch.lr, vcpu->arch.ctr);
  70. printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0,
  71. vcpu->arch.shared->srr1);
  72. printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
  73. for (i = 0; i < 32; i += 4) {
  74. printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
  75. kvmppc_get_gpr(vcpu, i),
  76. kvmppc_get_gpr(vcpu, i+1),
  77. kvmppc_get_gpr(vcpu, i+2),
  78. kvmppc_get_gpr(vcpu, i+3));
  79. }
  80. }
  81. #ifdef CONFIG_SPE
  82. void kvmppc_vcpu_disable_spe(struct kvm_vcpu *vcpu)
  83. {
  84. preempt_disable();
  85. enable_kernel_spe();
  86. kvmppc_save_guest_spe(vcpu);
  87. vcpu->arch.shadow_msr &= ~MSR_SPE;
  88. preempt_enable();
  89. }
  90. static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu)
  91. {
  92. preempt_disable();
  93. enable_kernel_spe();
  94. kvmppc_load_guest_spe(vcpu);
  95. vcpu->arch.shadow_msr |= MSR_SPE;
  96. preempt_enable();
  97. }
  98. static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
  99. {
  100. if (vcpu->arch.shared->msr & MSR_SPE) {
  101. if (!(vcpu->arch.shadow_msr & MSR_SPE))
  102. kvmppc_vcpu_enable_spe(vcpu);
  103. } else if (vcpu->arch.shadow_msr & MSR_SPE) {
  104. kvmppc_vcpu_disable_spe(vcpu);
  105. }
  106. }
  107. #else
  108. static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
  109. {
  110. }
  111. #endif
  112. static void kvmppc_vcpu_sync_fpu(struct kvm_vcpu *vcpu)
  113. {
  114. #if defined(CONFIG_PPC_FPU) && !defined(CONFIG_KVM_BOOKE_HV)
  115. /* We always treat the FP bit as enabled from the host
  116. perspective, so only need to adjust the shadow MSR */
  117. vcpu->arch.shadow_msr &= ~MSR_FP;
  118. vcpu->arch.shadow_msr |= vcpu->arch.shared->msr & MSR_FP;
  119. #endif
  120. }
  121. /*
  122. * Helper function for "full" MSR writes. No need to call this if only
  123. * EE/CE/ME/DE/RI are changing.
  124. */
  125. void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr)
  126. {
  127. u32 old_msr = vcpu->arch.shared->msr;
  128. #ifdef CONFIG_KVM_BOOKE_HV
  129. new_msr |= MSR_GS;
  130. #endif
  131. vcpu->arch.shared->msr = new_msr;
  132. kvmppc_mmu_msr_notify(vcpu, old_msr);
  133. kvmppc_vcpu_sync_spe(vcpu);
  134. kvmppc_vcpu_sync_fpu(vcpu);
  135. }
  136. static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
  137. unsigned int priority)
  138. {
  139. trace_kvm_booke_queue_irqprio(vcpu, priority);
  140. set_bit(priority, &vcpu->arch.pending_exceptions);
  141. }
  142. static void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
  143. ulong dear_flags, ulong esr_flags)
  144. {
  145. vcpu->arch.queued_dear = dear_flags;
  146. vcpu->arch.queued_esr = esr_flags;
  147. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
  148. }
  149. static void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
  150. ulong dear_flags, ulong esr_flags)
  151. {
  152. vcpu->arch.queued_dear = dear_flags;
  153. vcpu->arch.queued_esr = esr_flags;
  154. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
  155. }
  156. static void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
  157. ulong esr_flags)
  158. {
  159. vcpu->arch.queued_esr = esr_flags;
  160. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
  161. }
  162. static void kvmppc_core_queue_alignment(struct kvm_vcpu *vcpu, ulong dear_flags,
  163. ulong esr_flags)
  164. {
  165. vcpu->arch.queued_dear = dear_flags;
  166. vcpu->arch.queued_esr = esr_flags;
  167. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALIGNMENT);
  168. }
  169. void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
  170. {
  171. vcpu->arch.queued_esr = esr_flags;
  172. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
  173. }
  174. void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
  175. {
  176. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
  177. }
  178. int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
  179. {
  180. return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
  181. }
  182. void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
  183. {
  184. clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
  185. }
  186. void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
  187. struct kvm_interrupt *irq)
  188. {
  189. unsigned int prio = BOOKE_IRQPRIO_EXTERNAL;
  190. if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
  191. prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL;
  192. kvmppc_booke_queue_irqprio(vcpu, prio);
  193. }
  194. void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
  195. struct kvm_interrupt *irq)
  196. {
  197. clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
  198. clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
  199. }
  200. static void kvmppc_core_queue_watchdog(struct kvm_vcpu *vcpu)
  201. {
  202. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_WATCHDOG);
  203. }
  204. static void kvmppc_core_dequeue_watchdog(struct kvm_vcpu *vcpu)
  205. {
  206. clear_bit(BOOKE_IRQPRIO_WATCHDOG, &vcpu->arch.pending_exceptions);
  207. }
  208. static void set_guest_srr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
  209. {
  210. #ifdef CONFIG_KVM_BOOKE_HV
  211. mtspr(SPRN_GSRR0, srr0);
  212. mtspr(SPRN_GSRR1, srr1);
  213. #else
  214. vcpu->arch.shared->srr0 = srr0;
  215. vcpu->arch.shared->srr1 = srr1;
  216. #endif
  217. }
  218. static void set_guest_csrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
  219. {
  220. vcpu->arch.csrr0 = srr0;
  221. vcpu->arch.csrr1 = srr1;
  222. }
  223. static void set_guest_dsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
  224. {
  225. if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC)) {
  226. vcpu->arch.dsrr0 = srr0;
  227. vcpu->arch.dsrr1 = srr1;
  228. } else {
  229. set_guest_csrr(vcpu, srr0, srr1);
  230. }
  231. }
  232. static void set_guest_mcsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
  233. {
  234. vcpu->arch.mcsrr0 = srr0;
  235. vcpu->arch.mcsrr1 = srr1;
  236. }
  237. static unsigned long get_guest_dear(struct kvm_vcpu *vcpu)
  238. {
  239. #ifdef CONFIG_KVM_BOOKE_HV
  240. return mfspr(SPRN_GDEAR);
  241. #else
  242. return vcpu->arch.shared->dar;
  243. #endif
  244. }
  245. static void set_guest_dear(struct kvm_vcpu *vcpu, unsigned long dear)
  246. {
  247. #ifdef CONFIG_KVM_BOOKE_HV
  248. mtspr(SPRN_GDEAR, dear);
  249. #else
  250. vcpu->arch.shared->dar = dear;
  251. #endif
  252. }
  253. static unsigned long get_guest_esr(struct kvm_vcpu *vcpu)
  254. {
  255. #ifdef CONFIG_KVM_BOOKE_HV
  256. return mfspr(SPRN_GESR);
  257. #else
  258. return vcpu->arch.shared->esr;
  259. #endif
  260. }
  261. static void set_guest_esr(struct kvm_vcpu *vcpu, u32 esr)
  262. {
  263. #ifdef CONFIG_KVM_BOOKE_HV
  264. mtspr(SPRN_GESR, esr);
  265. #else
  266. vcpu->arch.shared->esr = esr;
  267. #endif
  268. }
  269. static unsigned long get_guest_epr(struct kvm_vcpu *vcpu)
  270. {
  271. #ifdef CONFIG_KVM_BOOKE_HV
  272. return mfspr(SPRN_GEPR);
  273. #else
  274. return vcpu->arch.epr;
  275. #endif
  276. }
  277. /* Deliver the interrupt of the corresponding priority, if possible. */
  278. static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
  279. unsigned int priority)
  280. {
  281. int allowed = 0;
  282. ulong msr_mask = 0;
  283. bool update_esr = false, update_dear = false, update_epr = false;
  284. ulong crit_raw = vcpu->arch.shared->critical;
  285. ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
  286. bool crit;
  287. bool keep_irq = false;
  288. enum int_class int_class;
  289. ulong new_msr = vcpu->arch.shared->msr;
  290. /* Truncate crit indicators in 32 bit mode */
  291. if (!(vcpu->arch.shared->msr & MSR_SF)) {
  292. crit_raw &= 0xffffffff;
  293. crit_r1 &= 0xffffffff;
  294. }
  295. /* Critical section when crit == r1 */
  296. crit = (crit_raw == crit_r1);
  297. /* ... and we're in supervisor mode */
  298. crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
  299. if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
  300. priority = BOOKE_IRQPRIO_EXTERNAL;
  301. keep_irq = true;
  302. }
  303. if ((priority == BOOKE_IRQPRIO_EXTERNAL) && vcpu->arch.epr_enabled)
  304. update_epr = true;
  305. switch (priority) {
  306. case BOOKE_IRQPRIO_DTLB_MISS:
  307. case BOOKE_IRQPRIO_DATA_STORAGE:
  308. case BOOKE_IRQPRIO_ALIGNMENT:
  309. update_dear = true;
  310. /* fall through */
  311. case BOOKE_IRQPRIO_INST_STORAGE:
  312. case BOOKE_IRQPRIO_PROGRAM:
  313. update_esr = true;
  314. /* fall through */
  315. case BOOKE_IRQPRIO_ITLB_MISS:
  316. case BOOKE_IRQPRIO_SYSCALL:
  317. case BOOKE_IRQPRIO_FP_UNAVAIL:
  318. case BOOKE_IRQPRIO_SPE_UNAVAIL:
  319. case BOOKE_IRQPRIO_SPE_FP_DATA:
  320. case BOOKE_IRQPRIO_SPE_FP_ROUND:
  321. case BOOKE_IRQPRIO_AP_UNAVAIL:
  322. allowed = 1;
  323. msr_mask = MSR_CE | MSR_ME | MSR_DE;
  324. int_class = INT_CLASS_NONCRIT;
  325. break;
  326. case BOOKE_IRQPRIO_WATCHDOG:
  327. case BOOKE_IRQPRIO_CRITICAL:
  328. case BOOKE_IRQPRIO_DBELL_CRIT:
  329. allowed = vcpu->arch.shared->msr & MSR_CE;
  330. allowed = allowed && !crit;
  331. msr_mask = MSR_ME;
  332. int_class = INT_CLASS_CRIT;
  333. break;
  334. case BOOKE_IRQPRIO_MACHINE_CHECK:
  335. allowed = vcpu->arch.shared->msr & MSR_ME;
  336. allowed = allowed && !crit;
  337. int_class = INT_CLASS_MC;
  338. break;
  339. case BOOKE_IRQPRIO_DECREMENTER:
  340. case BOOKE_IRQPRIO_FIT:
  341. keep_irq = true;
  342. /* fall through */
  343. case BOOKE_IRQPRIO_EXTERNAL:
  344. case BOOKE_IRQPRIO_DBELL:
  345. allowed = vcpu->arch.shared->msr & MSR_EE;
  346. allowed = allowed && !crit;
  347. msr_mask = MSR_CE | MSR_ME | MSR_DE;
  348. int_class = INT_CLASS_NONCRIT;
  349. break;
  350. case BOOKE_IRQPRIO_DEBUG:
  351. allowed = vcpu->arch.shared->msr & MSR_DE;
  352. allowed = allowed && !crit;
  353. msr_mask = MSR_ME;
  354. int_class = INT_CLASS_CRIT;
  355. break;
  356. }
  357. if (allowed) {
  358. switch (int_class) {
  359. case INT_CLASS_NONCRIT:
  360. set_guest_srr(vcpu, vcpu->arch.pc,
  361. vcpu->arch.shared->msr);
  362. break;
  363. case INT_CLASS_CRIT:
  364. set_guest_csrr(vcpu, vcpu->arch.pc,
  365. vcpu->arch.shared->msr);
  366. break;
  367. case INT_CLASS_DBG:
  368. set_guest_dsrr(vcpu, vcpu->arch.pc,
  369. vcpu->arch.shared->msr);
  370. break;
  371. case INT_CLASS_MC:
  372. set_guest_mcsrr(vcpu, vcpu->arch.pc,
  373. vcpu->arch.shared->msr);
  374. break;
  375. }
  376. vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
  377. if (update_esr == true)
  378. set_guest_esr(vcpu, vcpu->arch.queued_esr);
  379. if (update_dear == true)
  380. set_guest_dear(vcpu, vcpu->arch.queued_dear);
  381. if (update_epr == true)
  382. kvm_make_request(KVM_REQ_EPR_EXIT, vcpu);
  383. new_msr &= msr_mask;
  384. #if defined(CONFIG_64BIT)
  385. if (vcpu->arch.epcr & SPRN_EPCR_ICM)
  386. new_msr |= MSR_CM;
  387. #endif
  388. kvmppc_set_msr(vcpu, new_msr);
  389. if (!keep_irq)
  390. clear_bit(priority, &vcpu->arch.pending_exceptions);
  391. }
  392. #ifdef CONFIG_KVM_BOOKE_HV
  393. /*
  394. * If an interrupt is pending but masked, raise a guest doorbell
  395. * so that we are notified when the guest enables the relevant
  396. * MSR bit.
  397. */
  398. if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE)
  399. kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT);
  400. if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE)
  401. kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT);
  402. if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK)
  403. kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC);
  404. #endif
  405. return allowed;
  406. }
  407. /*
  408. * Return the number of jiffies until the next timeout. If the timeout is
  409. * longer than the NEXT_TIMER_MAX_DELTA, then return NEXT_TIMER_MAX_DELTA
  410. * because the larger value can break the timer APIs.
  411. */
  412. static unsigned long watchdog_next_timeout(struct kvm_vcpu *vcpu)
  413. {
  414. u64 tb, wdt_tb, wdt_ticks = 0;
  415. u64 nr_jiffies = 0;
  416. u32 period = TCR_GET_WP(vcpu->arch.tcr);
  417. wdt_tb = 1ULL << (63 - period);
  418. tb = get_tb();
  419. /*
  420. * The watchdog timeout will hapeen when TB bit corresponding
  421. * to watchdog will toggle from 0 to 1.
  422. */
  423. if (tb & wdt_tb)
  424. wdt_ticks = wdt_tb;
  425. wdt_ticks += wdt_tb - (tb & (wdt_tb - 1));
  426. /* Convert timebase ticks to jiffies */
  427. nr_jiffies = wdt_ticks;
  428. if (do_div(nr_jiffies, tb_ticks_per_jiffy))
  429. nr_jiffies++;
  430. return min_t(unsigned long long, nr_jiffies, NEXT_TIMER_MAX_DELTA);
  431. }
  432. static void arm_next_watchdog(struct kvm_vcpu *vcpu)
  433. {
  434. unsigned long nr_jiffies;
  435. unsigned long flags;
  436. /*
  437. * If TSR_ENW and TSR_WIS are not set then no need to exit to
  438. * userspace, so clear the KVM_REQ_WATCHDOG request.
  439. */
  440. if ((vcpu->arch.tsr & (TSR_ENW | TSR_WIS)) != (TSR_ENW | TSR_WIS))
  441. clear_bit(KVM_REQ_WATCHDOG, &vcpu->requests);
  442. spin_lock_irqsave(&vcpu->arch.wdt_lock, flags);
  443. nr_jiffies = watchdog_next_timeout(vcpu);
  444. /*
  445. * If the number of jiffies of watchdog timer >= NEXT_TIMER_MAX_DELTA
  446. * then do not run the watchdog timer as this can break timer APIs.
  447. */
  448. if (nr_jiffies < NEXT_TIMER_MAX_DELTA)
  449. mod_timer(&vcpu->arch.wdt_timer, jiffies + nr_jiffies);
  450. else
  451. del_timer(&vcpu->arch.wdt_timer);
  452. spin_unlock_irqrestore(&vcpu->arch.wdt_lock, flags);
  453. }
  454. void kvmppc_watchdog_func(unsigned long data)
  455. {
  456. struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
  457. u32 tsr, new_tsr;
  458. int final;
  459. do {
  460. new_tsr = tsr = vcpu->arch.tsr;
  461. final = 0;
  462. /* Time out event */
  463. if (tsr & TSR_ENW) {
  464. if (tsr & TSR_WIS)
  465. final = 1;
  466. else
  467. new_tsr = tsr | TSR_WIS;
  468. } else {
  469. new_tsr = tsr | TSR_ENW;
  470. }
  471. } while (cmpxchg(&vcpu->arch.tsr, tsr, new_tsr) != tsr);
  472. if (new_tsr & TSR_WIS) {
  473. smp_wmb();
  474. kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
  475. kvm_vcpu_kick(vcpu);
  476. }
  477. /*
  478. * If this is final watchdog expiry and some action is required
  479. * then exit to userspace.
  480. */
  481. if (final && (vcpu->arch.tcr & TCR_WRC_MASK) &&
  482. vcpu->arch.watchdog_enabled) {
  483. smp_wmb();
  484. kvm_make_request(KVM_REQ_WATCHDOG, vcpu);
  485. kvm_vcpu_kick(vcpu);
  486. }
  487. /*
  488. * Stop running the watchdog timer after final expiration to
  489. * prevent the host from being flooded with timers if the
  490. * guest sets a short period.
  491. * Timers will resume when TSR/TCR is updated next time.
  492. */
  493. if (!final)
  494. arm_next_watchdog(vcpu);
  495. }
  496. static void update_timer_ints(struct kvm_vcpu *vcpu)
  497. {
  498. if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS))
  499. kvmppc_core_queue_dec(vcpu);
  500. else
  501. kvmppc_core_dequeue_dec(vcpu);
  502. if ((vcpu->arch.tcr & TCR_WIE) && (vcpu->arch.tsr & TSR_WIS))
  503. kvmppc_core_queue_watchdog(vcpu);
  504. else
  505. kvmppc_core_dequeue_watchdog(vcpu);
  506. }
  507. static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu)
  508. {
  509. unsigned long *pending = &vcpu->arch.pending_exceptions;
  510. unsigned int priority;
  511. priority = __ffs(*pending);
  512. while (priority < BOOKE_IRQPRIO_MAX) {
  513. if (kvmppc_booke_irqprio_deliver(vcpu, priority))
  514. break;
  515. priority = find_next_bit(pending,
  516. BITS_PER_BYTE * sizeof(*pending),
  517. priority + 1);
  518. }
  519. /* Tell the guest about our interrupt status */
  520. vcpu->arch.shared->int_pending = !!*pending;
  521. }
  522. /* Check pending exceptions and deliver one, if possible. */
  523. int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
  524. {
  525. int r = 0;
  526. WARN_ON_ONCE(!irqs_disabled());
  527. kvmppc_core_check_exceptions(vcpu);
  528. if (vcpu->requests) {
  529. /* Exception delivery raised request; start over */
  530. return 1;
  531. }
  532. if (vcpu->arch.shared->msr & MSR_WE) {
  533. local_irq_enable();
  534. kvm_vcpu_block(vcpu);
  535. clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
  536. local_irq_disable();
  537. kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
  538. r = 1;
  539. };
  540. return r;
  541. }
  542. int kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
  543. {
  544. int r = 1; /* Indicate we want to get back into the guest */
  545. if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu))
  546. update_timer_ints(vcpu);
  547. #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
  548. if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
  549. kvmppc_core_flush_tlb(vcpu);
  550. #endif
  551. if (kvm_check_request(KVM_REQ_WATCHDOG, vcpu)) {
  552. vcpu->run->exit_reason = KVM_EXIT_WATCHDOG;
  553. r = 0;
  554. }
  555. if (kvm_check_request(KVM_REQ_EPR_EXIT, vcpu)) {
  556. vcpu->run->epr.epr = 0;
  557. vcpu->arch.epr_needed = true;
  558. vcpu->run->exit_reason = KVM_EXIT_EPR;
  559. r = 0;
  560. }
  561. return r;
  562. }
  563. int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  564. {
  565. int ret, s;
  566. #ifdef CONFIG_PPC_FPU
  567. unsigned int fpscr;
  568. int fpexc_mode;
  569. u64 fpr[32];
  570. #endif
  571. if (!vcpu->arch.sane) {
  572. kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  573. return -EINVAL;
  574. }
  575. local_irq_disable();
  576. s = kvmppc_prepare_to_enter(vcpu);
  577. if (s <= 0) {
  578. local_irq_enable();
  579. ret = s;
  580. goto out;
  581. }
  582. kvmppc_lazy_ee_enable();
  583. kvm_guest_enter();
  584. #ifdef CONFIG_PPC_FPU
  585. /* Save userspace FPU state in stack */
  586. enable_kernel_fp();
  587. memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
  588. fpscr = current->thread.fpscr.val;
  589. fpexc_mode = current->thread.fpexc_mode;
  590. /* Restore guest FPU state to thread */
  591. memcpy(current->thread.fpr, vcpu->arch.fpr, sizeof(vcpu->arch.fpr));
  592. current->thread.fpscr.val = vcpu->arch.fpscr;
  593. /*
  594. * Since we can't trap on MSR_FP in GS-mode, we consider the guest
  595. * as always using the FPU. Kernel usage of FP (via
  596. * enable_kernel_fp()) in this thread must not occur while
  597. * vcpu->fpu_active is set.
  598. */
  599. vcpu->fpu_active = 1;
  600. kvmppc_load_guest_fp(vcpu);
  601. #endif
  602. ret = __kvmppc_vcpu_run(kvm_run, vcpu);
  603. /* No need for kvm_guest_exit. It's done in handle_exit.
  604. We also get here with interrupts enabled. */
  605. #ifdef CONFIG_PPC_FPU
  606. kvmppc_save_guest_fp(vcpu);
  607. vcpu->fpu_active = 0;
  608. /* Save guest FPU state from thread */
  609. memcpy(vcpu->arch.fpr, current->thread.fpr, sizeof(vcpu->arch.fpr));
  610. vcpu->arch.fpscr = current->thread.fpscr.val;
  611. /* Restore userspace FPU state from stack */
  612. memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
  613. current->thread.fpscr.val = fpscr;
  614. current->thread.fpexc_mode = fpexc_mode;
  615. #endif
  616. out:
  617. vcpu->mode = OUTSIDE_GUEST_MODE;
  618. return ret;
  619. }
  620. static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
  621. {
  622. enum emulation_result er;
  623. er = kvmppc_emulate_instruction(run, vcpu);
  624. switch (er) {
  625. case EMULATE_DONE:
  626. /* don't overwrite subtypes, just account kvm_stats */
  627. kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
  628. /* Future optimization: only reload non-volatiles if
  629. * they were actually modified by emulation. */
  630. return RESUME_GUEST_NV;
  631. case EMULATE_DO_DCR:
  632. run->exit_reason = KVM_EXIT_DCR;
  633. return RESUME_HOST;
  634. case EMULATE_FAIL:
  635. printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
  636. __func__, vcpu->arch.pc, vcpu->arch.last_inst);
  637. /* For debugging, encode the failing instruction and
  638. * report it to userspace. */
  639. run->hw.hardware_exit_reason = ~0ULL << 32;
  640. run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
  641. kvmppc_core_queue_program(vcpu, ESR_PIL);
  642. return RESUME_HOST;
  643. default:
  644. BUG();
  645. }
  646. }
  647. static void kvmppc_fill_pt_regs(struct pt_regs *regs)
  648. {
  649. ulong r1, ip, msr, lr;
  650. asm("mr %0, 1" : "=r"(r1));
  651. asm("mflr %0" : "=r"(lr));
  652. asm("mfmsr %0" : "=r"(msr));
  653. asm("bl 1f; 1: mflr %0" : "=r"(ip));
  654. memset(regs, 0, sizeof(*regs));
  655. regs->gpr[1] = r1;
  656. regs->nip = ip;
  657. regs->msr = msr;
  658. regs->link = lr;
  659. }
  660. /*
  661. * For interrupts needed to be handled by host interrupt handlers,
  662. * corresponding host handler are called from here in similar way
  663. * (but not exact) as they are called from low level handler
  664. * (such as from arch/powerpc/kernel/head_fsl_booke.S).
  665. */
  666. static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu,
  667. unsigned int exit_nr)
  668. {
  669. struct pt_regs regs;
  670. switch (exit_nr) {
  671. case BOOKE_INTERRUPT_EXTERNAL:
  672. kvmppc_fill_pt_regs(&regs);
  673. do_IRQ(&regs);
  674. break;
  675. case BOOKE_INTERRUPT_DECREMENTER:
  676. kvmppc_fill_pt_regs(&regs);
  677. timer_interrupt(&regs);
  678. break;
  679. #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3E_64)
  680. case BOOKE_INTERRUPT_DOORBELL:
  681. kvmppc_fill_pt_regs(&regs);
  682. doorbell_exception(&regs);
  683. break;
  684. #endif
  685. case BOOKE_INTERRUPT_MACHINE_CHECK:
  686. /* FIXME */
  687. break;
  688. case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
  689. kvmppc_fill_pt_regs(&regs);
  690. performance_monitor_exception(&regs);
  691. break;
  692. case BOOKE_INTERRUPT_WATCHDOG:
  693. kvmppc_fill_pt_regs(&regs);
  694. #ifdef CONFIG_BOOKE_WDT
  695. WatchdogException(&regs);
  696. #else
  697. unknown_exception(&regs);
  698. #endif
  699. break;
  700. case BOOKE_INTERRUPT_CRITICAL:
  701. unknown_exception(&regs);
  702. break;
  703. }
  704. }
  705. /**
  706. * kvmppc_handle_exit
  707. *
  708. * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
  709. */
  710. int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
  711. unsigned int exit_nr)
  712. {
  713. int r = RESUME_HOST;
  714. int s;
  715. /* update before a new last_exit_type is rewritten */
  716. kvmppc_update_timing_stats(vcpu);
  717. /* restart interrupts if they were meant for the host */
  718. kvmppc_restart_interrupt(vcpu, exit_nr);
  719. local_irq_enable();
  720. trace_kvm_exit(exit_nr, vcpu);
  721. kvm_guest_exit();
  722. run->exit_reason = KVM_EXIT_UNKNOWN;
  723. run->ready_for_interrupt_injection = 1;
  724. switch (exit_nr) {
  725. case BOOKE_INTERRUPT_MACHINE_CHECK:
  726. printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
  727. kvmppc_dump_vcpu(vcpu);
  728. /* For debugging, send invalid exit reason to user space */
  729. run->hw.hardware_exit_reason = ~1ULL << 32;
  730. run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR);
  731. r = RESUME_HOST;
  732. break;
  733. case BOOKE_INTERRUPT_EXTERNAL:
  734. kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
  735. r = RESUME_GUEST;
  736. break;
  737. case BOOKE_INTERRUPT_DECREMENTER:
  738. kvmppc_account_exit(vcpu, DEC_EXITS);
  739. r = RESUME_GUEST;
  740. break;
  741. case BOOKE_INTERRUPT_WATCHDOG:
  742. r = RESUME_GUEST;
  743. break;
  744. case BOOKE_INTERRUPT_DOORBELL:
  745. kvmppc_account_exit(vcpu, DBELL_EXITS);
  746. r = RESUME_GUEST;
  747. break;
  748. case BOOKE_INTERRUPT_GUEST_DBELL_CRIT:
  749. kvmppc_account_exit(vcpu, GDBELL_EXITS);
  750. /*
  751. * We are here because there is a pending guest interrupt
  752. * which could not be delivered as MSR_CE or MSR_ME was not
  753. * set. Once we break from here we will retry delivery.
  754. */
  755. r = RESUME_GUEST;
  756. break;
  757. case BOOKE_INTERRUPT_GUEST_DBELL:
  758. kvmppc_account_exit(vcpu, GDBELL_EXITS);
  759. /*
  760. * We are here because there is a pending guest interrupt
  761. * which could not be delivered as MSR_EE was not set. Once
  762. * we break from here we will retry delivery.
  763. */
  764. r = RESUME_GUEST;
  765. break;
  766. case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
  767. r = RESUME_GUEST;
  768. break;
  769. case BOOKE_INTERRUPT_HV_PRIV:
  770. r = emulation_exit(run, vcpu);
  771. break;
  772. case BOOKE_INTERRUPT_PROGRAM:
  773. if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) {
  774. /*
  775. * Program traps generated by user-level software must
  776. * be handled by the guest kernel.
  777. *
  778. * In GS mode, hypervisor privileged instructions trap
  779. * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are
  780. * actual program interrupts, handled by the guest.
  781. */
  782. kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
  783. r = RESUME_GUEST;
  784. kvmppc_account_exit(vcpu, USR_PR_INST);
  785. break;
  786. }
  787. r = emulation_exit(run, vcpu);
  788. break;
  789. case BOOKE_INTERRUPT_FP_UNAVAIL:
  790. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
  791. kvmppc_account_exit(vcpu, FP_UNAVAIL);
  792. r = RESUME_GUEST;
  793. break;
  794. #ifdef CONFIG_SPE
  795. case BOOKE_INTERRUPT_SPE_UNAVAIL: {
  796. if (vcpu->arch.shared->msr & MSR_SPE)
  797. kvmppc_vcpu_enable_spe(vcpu);
  798. else
  799. kvmppc_booke_queue_irqprio(vcpu,
  800. BOOKE_IRQPRIO_SPE_UNAVAIL);
  801. r = RESUME_GUEST;
  802. break;
  803. }
  804. case BOOKE_INTERRUPT_SPE_FP_DATA:
  805. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
  806. r = RESUME_GUEST;
  807. break;
  808. case BOOKE_INTERRUPT_SPE_FP_ROUND:
  809. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
  810. r = RESUME_GUEST;
  811. break;
  812. #else
  813. case BOOKE_INTERRUPT_SPE_UNAVAIL:
  814. /*
  815. * Guest wants SPE, but host kernel doesn't support it. Send
  816. * an "unimplemented operation" program check to the guest.
  817. */
  818. kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
  819. r = RESUME_GUEST;
  820. break;
  821. /*
  822. * These really should never happen without CONFIG_SPE,
  823. * as we should never enable the real MSR[SPE] in the guest.
  824. */
  825. case BOOKE_INTERRUPT_SPE_FP_DATA:
  826. case BOOKE_INTERRUPT_SPE_FP_ROUND:
  827. printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
  828. __func__, exit_nr, vcpu->arch.pc);
  829. run->hw.hardware_exit_reason = exit_nr;
  830. r = RESUME_HOST;
  831. break;
  832. #endif
  833. case BOOKE_INTERRUPT_DATA_STORAGE:
  834. kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
  835. vcpu->arch.fault_esr);
  836. kvmppc_account_exit(vcpu, DSI_EXITS);
  837. r = RESUME_GUEST;
  838. break;
  839. case BOOKE_INTERRUPT_INST_STORAGE:
  840. kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
  841. kvmppc_account_exit(vcpu, ISI_EXITS);
  842. r = RESUME_GUEST;
  843. break;
  844. case BOOKE_INTERRUPT_ALIGNMENT:
  845. kvmppc_core_queue_alignment(vcpu, vcpu->arch.fault_dear,
  846. vcpu->arch.fault_esr);
  847. r = RESUME_GUEST;
  848. break;
  849. #ifdef CONFIG_KVM_BOOKE_HV
  850. case BOOKE_INTERRUPT_HV_SYSCALL:
  851. if (!(vcpu->arch.shared->msr & MSR_PR)) {
  852. kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
  853. } else {
  854. /*
  855. * hcall from guest userspace -- send privileged
  856. * instruction program check.
  857. */
  858. kvmppc_core_queue_program(vcpu, ESR_PPR);
  859. }
  860. r = RESUME_GUEST;
  861. break;
  862. #else
  863. case BOOKE_INTERRUPT_SYSCALL:
  864. if (!(vcpu->arch.shared->msr & MSR_PR) &&
  865. (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
  866. /* KVM PV hypercalls */
  867. kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
  868. r = RESUME_GUEST;
  869. } else {
  870. /* Guest syscalls */
  871. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
  872. }
  873. kvmppc_account_exit(vcpu, SYSCALL_EXITS);
  874. r = RESUME_GUEST;
  875. break;
  876. #endif
  877. case BOOKE_INTERRUPT_DTLB_MISS: {
  878. unsigned long eaddr = vcpu->arch.fault_dear;
  879. int gtlb_index;
  880. gpa_t gpaddr;
  881. gfn_t gfn;
  882. #ifdef CONFIG_KVM_E500V2
  883. if (!(vcpu->arch.shared->msr & MSR_PR) &&
  884. (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
  885. kvmppc_map_magic(vcpu);
  886. kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
  887. r = RESUME_GUEST;
  888. break;
  889. }
  890. #endif
  891. /* Check the guest TLB. */
  892. gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
  893. if (gtlb_index < 0) {
  894. /* The guest didn't have a mapping for it. */
  895. kvmppc_core_queue_dtlb_miss(vcpu,
  896. vcpu->arch.fault_dear,
  897. vcpu->arch.fault_esr);
  898. kvmppc_mmu_dtlb_miss(vcpu);
  899. kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
  900. r = RESUME_GUEST;
  901. break;
  902. }
  903. gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
  904. gfn = gpaddr >> PAGE_SHIFT;
  905. if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
  906. /* The guest TLB had a mapping, but the shadow TLB
  907. * didn't, and it is RAM. This could be because:
  908. * a) the entry is mapping the host kernel, or
  909. * b) the guest used a large mapping which we're faking
  910. * Either way, we need to satisfy the fault without
  911. * invoking the guest. */
  912. kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
  913. kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
  914. r = RESUME_GUEST;
  915. } else {
  916. /* Guest has mapped and accessed a page which is not
  917. * actually RAM. */
  918. vcpu->arch.paddr_accessed = gpaddr;
  919. vcpu->arch.vaddr_accessed = eaddr;
  920. r = kvmppc_emulate_mmio(run, vcpu);
  921. kvmppc_account_exit(vcpu, MMIO_EXITS);
  922. }
  923. break;
  924. }
  925. case BOOKE_INTERRUPT_ITLB_MISS: {
  926. unsigned long eaddr = vcpu->arch.pc;
  927. gpa_t gpaddr;
  928. gfn_t gfn;
  929. int gtlb_index;
  930. r = RESUME_GUEST;
  931. /* Check the guest TLB. */
  932. gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
  933. if (gtlb_index < 0) {
  934. /* The guest didn't have a mapping for it. */
  935. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
  936. kvmppc_mmu_itlb_miss(vcpu);
  937. kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
  938. break;
  939. }
  940. kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
  941. gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
  942. gfn = gpaddr >> PAGE_SHIFT;
  943. if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
  944. /* The guest TLB had a mapping, but the shadow TLB
  945. * didn't. This could be because:
  946. * a) the entry is mapping the host kernel, or
  947. * b) the guest used a large mapping which we're faking
  948. * Either way, we need to satisfy the fault without
  949. * invoking the guest. */
  950. kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
  951. } else {
  952. /* Guest mapped and leaped at non-RAM! */
  953. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
  954. }
  955. break;
  956. }
  957. case BOOKE_INTERRUPT_DEBUG: {
  958. u32 dbsr;
  959. vcpu->arch.pc = mfspr(SPRN_CSRR0);
  960. /* clear IAC events in DBSR register */
  961. dbsr = mfspr(SPRN_DBSR);
  962. dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
  963. mtspr(SPRN_DBSR, dbsr);
  964. run->exit_reason = KVM_EXIT_DEBUG;
  965. kvmppc_account_exit(vcpu, DEBUG_EXITS);
  966. r = RESUME_HOST;
  967. break;
  968. }
  969. default:
  970. printk(KERN_EMERG "exit_nr %d\n", exit_nr);
  971. BUG();
  972. }
  973. /*
  974. * To avoid clobbering exit_reason, only check for signals if we
  975. * aren't already exiting to userspace for some other reason.
  976. */
  977. if (!(r & RESUME_HOST)) {
  978. local_irq_disable();
  979. s = kvmppc_prepare_to_enter(vcpu);
  980. if (s <= 0) {
  981. local_irq_enable();
  982. r = (s << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
  983. } else {
  984. kvmppc_lazy_ee_enable();
  985. }
  986. }
  987. return r;
  988. }
  989. static void kvmppc_set_tsr(struct kvm_vcpu *vcpu, u32 new_tsr)
  990. {
  991. u32 old_tsr = vcpu->arch.tsr;
  992. vcpu->arch.tsr = new_tsr;
  993. if ((old_tsr ^ vcpu->arch.tsr) & (TSR_ENW | TSR_WIS))
  994. arm_next_watchdog(vcpu);
  995. update_timer_ints(vcpu);
  996. }
  997. /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
  998. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
  999. {
  1000. int i;
  1001. int r;
  1002. vcpu->arch.pc = 0;
  1003. vcpu->arch.shared->pir = vcpu->vcpu_id;
  1004. kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
  1005. kvmppc_set_msr(vcpu, 0);
  1006. #ifndef CONFIG_KVM_BOOKE_HV
  1007. vcpu->arch.shadow_msr = MSR_USER | MSR_DE | MSR_IS | MSR_DS;
  1008. vcpu->arch.shadow_pid = 1;
  1009. vcpu->arch.shared->msr = 0;
  1010. #endif
  1011. /* Eye-catching numbers so we know if the guest takes an interrupt
  1012. * before it's programmed its own IVPR/IVORs. */
  1013. vcpu->arch.ivpr = 0x55550000;
  1014. for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
  1015. vcpu->arch.ivor[i] = 0x7700 | i * 4;
  1016. kvmppc_init_timing_stats(vcpu);
  1017. r = kvmppc_core_vcpu_setup(vcpu);
  1018. kvmppc_sanity_check(vcpu);
  1019. return r;
  1020. }
  1021. int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu)
  1022. {
  1023. /* setup watchdog timer once */
  1024. spin_lock_init(&vcpu->arch.wdt_lock);
  1025. setup_timer(&vcpu->arch.wdt_timer, kvmppc_watchdog_func,
  1026. (unsigned long)vcpu);
  1027. return 0;
  1028. }
  1029. void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu)
  1030. {
  1031. del_timer_sync(&vcpu->arch.wdt_timer);
  1032. }
  1033. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  1034. {
  1035. int i;
  1036. regs->pc = vcpu->arch.pc;
  1037. regs->cr = kvmppc_get_cr(vcpu);
  1038. regs->ctr = vcpu->arch.ctr;
  1039. regs->lr = vcpu->arch.lr;
  1040. regs->xer = kvmppc_get_xer(vcpu);
  1041. regs->msr = vcpu->arch.shared->msr;
  1042. regs->srr0 = vcpu->arch.shared->srr0;
  1043. regs->srr1 = vcpu->arch.shared->srr1;
  1044. regs->pid = vcpu->arch.pid;
  1045. regs->sprg0 = vcpu->arch.shared->sprg0;
  1046. regs->sprg1 = vcpu->arch.shared->sprg1;
  1047. regs->sprg2 = vcpu->arch.shared->sprg2;
  1048. regs->sprg3 = vcpu->arch.shared->sprg3;
  1049. regs->sprg4 = vcpu->arch.shared->sprg4;
  1050. regs->sprg5 = vcpu->arch.shared->sprg5;
  1051. regs->sprg6 = vcpu->arch.shared->sprg6;
  1052. regs->sprg7 = vcpu->arch.shared->sprg7;
  1053. for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
  1054. regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
  1055. return 0;
  1056. }
  1057. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  1058. {
  1059. int i;
  1060. vcpu->arch.pc = regs->pc;
  1061. kvmppc_set_cr(vcpu, regs->cr);
  1062. vcpu->arch.ctr = regs->ctr;
  1063. vcpu->arch.lr = regs->lr;
  1064. kvmppc_set_xer(vcpu, regs->xer);
  1065. kvmppc_set_msr(vcpu, regs->msr);
  1066. vcpu->arch.shared->srr0 = regs->srr0;
  1067. vcpu->arch.shared->srr1 = regs->srr1;
  1068. kvmppc_set_pid(vcpu, regs->pid);
  1069. vcpu->arch.shared->sprg0 = regs->sprg0;
  1070. vcpu->arch.shared->sprg1 = regs->sprg1;
  1071. vcpu->arch.shared->sprg2 = regs->sprg2;
  1072. vcpu->arch.shared->sprg3 = regs->sprg3;
  1073. vcpu->arch.shared->sprg4 = regs->sprg4;
  1074. vcpu->arch.shared->sprg5 = regs->sprg5;
  1075. vcpu->arch.shared->sprg6 = regs->sprg6;
  1076. vcpu->arch.shared->sprg7 = regs->sprg7;
  1077. for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
  1078. kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
  1079. return 0;
  1080. }
  1081. static void get_sregs_base(struct kvm_vcpu *vcpu,
  1082. struct kvm_sregs *sregs)
  1083. {
  1084. u64 tb = get_tb();
  1085. sregs->u.e.features |= KVM_SREGS_E_BASE;
  1086. sregs->u.e.csrr0 = vcpu->arch.csrr0;
  1087. sregs->u.e.csrr1 = vcpu->arch.csrr1;
  1088. sregs->u.e.mcsr = vcpu->arch.mcsr;
  1089. sregs->u.e.esr = get_guest_esr(vcpu);
  1090. sregs->u.e.dear = get_guest_dear(vcpu);
  1091. sregs->u.e.tsr = vcpu->arch.tsr;
  1092. sregs->u.e.tcr = vcpu->arch.tcr;
  1093. sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
  1094. sregs->u.e.tb = tb;
  1095. sregs->u.e.vrsave = vcpu->arch.vrsave;
  1096. }
  1097. static int set_sregs_base(struct kvm_vcpu *vcpu,
  1098. struct kvm_sregs *sregs)
  1099. {
  1100. if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
  1101. return 0;
  1102. vcpu->arch.csrr0 = sregs->u.e.csrr0;
  1103. vcpu->arch.csrr1 = sregs->u.e.csrr1;
  1104. vcpu->arch.mcsr = sregs->u.e.mcsr;
  1105. set_guest_esr(vcpu, sregs->u.e.esr);
  1106. set_guest_dear(vcpu, sregs->u.e.dear);
  1107. vcpu->arch.vrsave = sregs->u.e.vrsave;
  1108. kvmppc_set_tcr(vcpu, sregs->u.e.tcr);
  1109. if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) {
  1110. vcpu->arch.dec = sregs->u.e.dec;
  1111. kvmppc_emulate_dec(vcpu);
  1112. }
  1113. if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR)
  1114. kvmppc_set_tsr(vcpu, sregs->u.e.tsr);
  1115. return 0;
  1116. }
  1117. static void get_sregs_arch206(struct kvm_vcpu *vcpu,
  1118. struct kvm_sregs *sregs)
  1119. {
  1120. sregs->u.e.features |= KVM_SREGS_E_ARCH206;
  1121. sregs->u.e.pir = vcpu->vcpu_id;
  1122. sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
  1123. sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
  1124. sregs->u.e.decar = vcpu->arch.decar;
  1125. sregs->u.e.ivpr = vcpu->arch.ivpr;
  1126. }
  1127. static int set_sregs_arch206(struct kvm_vcpu *vcpu,
  1128. struct kvm_sregs *sregs)
  1129. {
  1130. if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
  1131. return 0;
  1132. if (sregs->u.e.pir != vcpu->vcpu_id)
  1133. return -EINVAL;
  1134. vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
  1135. vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
  1136. vcpu->arch.decar = sregs->u.e.decar;
  1137. vcpu->arch.ivpr = sregs->u.e.ivpr;
  1138. return 0;
  1139. }
  1140. void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  1141. {
  1142. sregs->u.e.features |= KVM_SREGS_E_IVOR;
  1143. sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
  1144. sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
  1145. sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
  1146. sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
  1147. sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
  1148. sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
  1149. sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
  1150. sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
  1151. sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
  1152. sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
  1153. sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
  1154. sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
  1155. sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
  1156. sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
  1157. sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
  1158. sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
  1159. }
  1160. int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  1161. {
  1162. if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
  1163. return 0;
  1164. vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
  1165. vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
  1166. vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
  1167. vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
  1168. vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
  1169. vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
  1170. vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
  1171. vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
  1172. vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
  1173. vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
  1174. vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
  1175. vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
  1176. vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
  1177. vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
  1178. vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
  1179. vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
  1180. return 0;
  1181. }
  1182. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  1183. struct kvm_sregs *sregs)
  1184. {
  1185. sregs->pvr = vcpu->arch.pvr;
  1186. get_sregs_base(vcpu, sregs);
  1187. get_sregs_arch206(vcpu, sregs);
  1188. kvmppc_core_get_sregs(vcpu, sregs);
  1189. return 0;
  1190. }
  1191. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  1192. struct kvm_sregs *sregs)
  1193. {
  1194. int ret;
  1195. if (vcpu->arch.pvr != sregs->pvr)
  1196. return -EINVAL;
  1197. ret = set_sregs_base(vcpu, sregs);
  1198. if (ret < 0)
  1199. return ret;
  1200. ret = set_sregs_arch206(vcpu, sregs);
  1201. if (ret < 0)
  1202. return ret;
  1203. return kvmppc_core_set_sregs(vcpu, sregs);
  1204. }
  1205. int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  1206. {
  1207. int r = -EINVAL;
  1208. switch (reg->id) {
  1209. case KVM_REG_PPC_IAC1:
  1210. case KVM_REG_PPC_IAC2:
  1211. case KVM_REG_PPC_IAC3:
  1212. case KVM_REG_PPC_IAC4: {
  1213. int iac = reg->id - KVM_REG_PPC_IAC1;
  1214. r = copy_to_user((u64 __user *)(long)reg->addr,
  1215. &vcpu->arch.dbg_reg.iac[iac], sizeof(u64));
  1216. break;
  1217. }
  1218. case KVM_REG_PPC_DAC1:
  1219. case KVM_REG_PPC_DAC2: {
  1220. int dac = reg->id - KVM_REG_PPC_DAC1;
  1221. r = copy_to_user((u64 __user *)(long)reg->addr,
  1222. &vcpu->arch.dbg_reg.dac[dac], sizeof(u64));
  1223. break;
  1224. }
  1225. case KVM_REG_PPC_EPR: {
  1226. u32 epr = get_guest_epr(vcpu);
  1227. r = put_user(epr, (u32 __user *)(long)reg->addr);
  1228. break;
  1229. }
  1230. #if defined(CONFIG_64BIT)
  1231. case KVM_REG_PPC_EPCR:
  1232. r = put_user(vcpu->arch.epcr, (u32 __user *)(long)reg->addr);
  1233. break;
  1234. #endif
  1235. default:
  1236. break;
  1237. }
  1238. return r;
  1239. }
  1240. int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  1241. {
  1242. int r = -EINVAL;
  1243. switch (reg->id) {
  1244. case KVM_REG_PPC_IAC1:
  1245. case KVM_REG_PPC_IAC2:
  1246. case KVM_REG_PPC_IAC3:
  1247. case KVM_REG_PPC_IAC4: {
  1248. int iac = reg->id - KVM_REG_PPC_IAC1;
  1249. r = copy_from_user(&vcpu->arch.dbg_reg.iac[iac],
  1250. (u64 __user *)(long)reg->addr, sizeof(u64));
  1251. break;
  1252. }
  1253. case KVM_REG_PPC_DAC1:
  1254. case KVM_REG_PPC_DAC2: {
  1255. int dac = reg->id - KVM_REG_PPC_DAC1;
  1256. r = copy_from_user(&vcpu->arch.dbg_reg.dac[dac],
  1257. (u64 __user *)(long)reg->addr, sizeof(u64));
  1258. break;
  1259. }
  1260. case KVM_REG_PPC_EPR: {
  1261. u32 new_epr;
  1262. r = get_user(new_epr, (u32 __user *)(long)reg->addr);
  1263. if (!r)
  1264. kvmppc_set_epr(vcpu, new_epr);
  1265. break;
  1266. }
  1267. #if defined(CONFIG_64BIT)
  1268. case KVM_REG_PPC_EPCR: {
  1269. u32 new_epcr;
  1270. r = get_user(new_epcr, (u32 __user *)(long)reg->addr);
  1271. if (r == 0)
  1272. kvmppc_set_epcr(vcpu, new_epcr);
  1273. break;
  1274. }
  1275. #endif
  1276. default:
  1277. break;
  1278. }
  1279. return r;
  1280. }
  1281. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  1282. {
  1283. return -ENOTSUPP;
  1284. }
  1285. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  1286. {
  1287. return -ENOTSUPP;
  1288. }
  1289. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  1290. struct kvm_translation *tr)
  1291. {
  1292. int r;
  1293. r = kvmppc_core_vcpu_translate(vcpu, tr);
  1294. return r;
  1295. }
  1296. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  1297. {
  1298. return -ENOTSUPP;
  1299. }
  1300. void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
  1301. struct kvm_memory_slot *dont)
  1302. {
  1303. }
  1304. int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
  1305. unsigned long npages)
  1306. {
  1307. return 0;
  1308. }
  1309. int kvmppc_core_prepare_memory_region(struct kvm *kvm,
  1310. struct kvm_memory_slot *memslot,
  1311. struct kvm_userspace_memory_region *mem)
  1312. {
  1313. return 0;
  1314. }
  1315. void kvmppc_core_commit_memory_region(struct kvm *kvm,
  1316. struct kvm_userspace_memory_region *mem,
  1317. const struct kvm_memory_slot *old)
  1318. {
  1319. }
  1320. void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
  1321. {
  1322. }
  1323. void kvmppc_set_epcr(struct kvm_vcpu *vcpu, u32 new_epcr)
  1324. {
  1325. #if defined(CONFIG_64BIT)
  1326. vcpu->arch.epcr = new_epcr;
  1327. #ifdef CONFIG_KVM_BOOKE_HV
  1328. vcpu->arch.shadow_epcr &= ~SPRN_EPCR_GICM;
  1329. if (vcpu->arch.epcr & SPRN_EPCR_ICM)
  1330. vcpu->arch.shadow_epcr |= SPRN_EPCR_GICM;
  1331. #endif
  1332. #endif
  1333. }
  1334. void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr)
  1335. {
  1336. vcpu->arch.tcr = new_tcr;
  1337. arm_next_watchdog(vcpu);
  1338. update_timer_ints(vcpu);
  1339. }
  1340. void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
  1341. {
  1342. set_bits(tsr_bits, &vcpu->arch.tsr);
  1343. smp_wmb();
  1344. kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
  1345. kvm_vcpu_kick(vcpu);
  1346. }
  1347. void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
  1348. {
  1349. clear_bits(tsr_bits, &vcpu->arch.tsr);
  1350. /*
  1351. * We may have stopped the watchdog due to
  1352. * being stuck on final expiration.
  1353. */
  1354. if (tsr_bits & (TSR_ENW | TSR_WIS))
  1355. arm_next_watchdog(vcpu);
  1356. update_timer_ints(vcpu);
  1357. }
  1358. void kvmppc_decrementer_func(unsigned long data)
  1359. {
  1360. struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
  1361. if (vcpu->arch.tcr & TCR_ARE) {
  1362. vcpu->arch.dec = vcpu->arch.decar;
  1363. kvmppc_emulate_dec(vcpu);
  1364. }
  1365. kvmppc_set_tsr_bits(vcpu, TSR_DIS);
  1366. }
  1367. void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1368. {
  1369. vcpu->cpu = smp_processor_id();
  1370. current->thread.kvm_vcpu = vcpu;
  1371. }
  1372. void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu)
  1373. {
  1374. current->thread.kvm_vcpu = NULL;
  1375. vcpu->cpu = -1;
  1376. }
  1377. int __init kvmppc_booke_init(void)
  1378. {
  1379. #ifndef CONFIG_KVM_BOOKE_HV
  1380. unsigned long ivor[16];
  1381. unsigned long *handler = kvmppc_booke_handler_addr;
  1382. unsigned long max_ivor = 0;
  1383. unsigned long handler_len;
  1384. int i;
  1385. /* We install our own exception handlers by hijacking IVPR. IVPR must
  1386. * be 16-bit aligned, so we need a 64KB allocation. */
  1387. kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
  1388. VCPU_SIZE_ORDER);
  1389. if (!kvmppc_booke_handlers)
  1390. return -ENOMEM;
  1391. /* XXX make sure our handlers are smaller than Linux's */
  1392. /* Copy our interrupt handlers to match host IVORs. That way we don't
  1393. * have to swap the IVORs on every guest/host transition. */
  1394. ivor[0] = mfspr(SPRN_IVOR0);
  1395. ivor[1] = mfspr(SPRN_IVOR1);
  1396. ivor[2] = mfspr(SPRN_IVOR2);
  1397. ivor[3] = mfspr(SPRN_IVOR3);
  1398. ivor[4] = mfspr(SPRN_IVOR4);
  1399. ivor[5] = mfspr(SPRN_IVOR5);
  1400. ivor[6] = mfspr(SPRN_IVOR6);
  1401. ivor[7] = mfspr(SPRN_IVOR7);
  1402. ivor[8] = mfspr(SPRN_IVOR8);
  1403. ivor[9] = mfspr(SPRN_IVOR9);
  1404. ivor[10] = mfspr(SPRN_IVOR10);
  1405. ivor[11] = mfspr(SPRN_IVOR11);
  1406. ivor[12] = mfspr(SPRN_IVOR12);
  1407. ivor[13] = mfspr(SPRN_IVOR13);
  1408. ivor[14] = mfspr(SPRN_IVOR14);
  1409. ivor[15] = mfspr(SPRN_IVOR15);
  1410. for (i = 0; i < 16; i++) {
  1411. if (ivor[i] > max_ivor)
  1412. max_ivor = i;
  1413. handler_len = handler[i + 1] - handler[i];
  1414. memcpy((void *)kvmppc_booke_handlers + ivor[i],
  1415. (void *)handler[i], handler_len);
  1416. }
  1417. handler_len = handler[max_ivor + 1] - handler[max_ivor];
  1418. flush_icache_range(kvmppc_booke_handlers, kvmppc_booke_handlers +
  1419. ivor[max_ivor] + handler_len);
  1420. #endif /* !BOOKE_HV */
  1421. return 0;
  1422. }
  1423. void __exit kvmppc_booke_exit(void)
  1424. {
  1425. free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
  1426. kvm_exit();
  1427. }