booke.c 44 KB

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