booke.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697
  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. default:
  643. BUG();
  644. }
  645. }
  646. static void kvmppc_fill_pt_regs(struct pt_regs *regs)
  647. {
  648. ulong r1, ip, msr, lr;
  649. asm("mr %0, 1" : "=r"(r1));
  650. asm("mflr %0" : "=r"(lr));
  651. asm("mfmsr %0" : "=r"(msr));
  652. asm("bl 1f; 1: mflr %0" : "=r"(ip));
  653. memset(regs, 0, sizeof(*regs));
  654. regs->gpr[1] = r1;
  655. regs->nip = ip;
  656. regs->msr = msr;
  657. regs->link = lr;
  658. }
  659. /*
  660. * For interrupts needed to be handled by host interrupt handlers,
  661. * corresponding host handler are called from here in similar way
  662. * (but not exact) as they are called from low level handler
  663. * (such as from arch/powerpc/kernel/head_fsl_booke.S).
  664. */
  665. static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu,
  666. unsigned int exit_nr)
  667. {
  668. struct pt_regs regs;
  669. switch (exit_nr) {
  670. case BOOKE_INTERRUPT_EXTERNAL:
  671. kvmppc_fill_pt_regs(&regs);
  672. do_IRQ(&regs);
  673. break;
  674. case BOOKE_INTERRUPT_DECREMENTER:
  675. kvmppc_fill_pt_regs(&regs);
  676. timer_interrupt(&regs);
  677. break;
  678. #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3E_64)
  679. case BOOKE_INTERRUPT_DOORBELL:
  680. kvmppc_fill_pt_regs(&regs);
  681. doorbell_exception(&regs);
  682. break;
  683. #endif
  684. case BOOKE_INTERRUPT_MACHINE_CHECK:
  685. /* FIXME */
  686. break;
  687. case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
  688. kvmppc_fill_pt_regs(&regs);
  689. performance_monitor_exception(&regs);
  690. break;
  691. case BOOKE_INTERRUPT_WATCHDOG:
  692. kvmppc_fill_pt_regs(&regs);
  693. #ifdef CONFIG_BOOKE_WDT
  694. WatchdogException(&regs);
  695. #else
  696. unknown_exception(&regs);
  697. #endif
  698. break;
  699. case BOOKE_INTERRUPT_CRITICAL:
  700. unknown_exception(&regs);
  701. break;
  702. }
  703. }
  704. /**
  705. * kvmppc_handle_exit
  706. *
  707. * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
  708. */
  709. int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
  710. unsigned int exit_nr)
  711. {
  712. int r = RESUME_HOST;
  713. int s;
  714. /* update before a new last_exit_type is rewritten */
  715. kvmppc_update_timing_stats(vcpu);
  716. /* restart interrupts if they were meant for the host */
  717. kvmppc_restart_interrupt(vcpu, exit_nr);
  718. local_irq_enable();
  719. trace_kvm_exit(exit_nr, vcpu);
  720. kvm_guest_exit();
  721. run->exit_reason = KVM_EXIT_UNKNOWN;
  722. run->ready_for_interrupt_injection = 1;
  723. switch (exit_nr) {
  724. case BOOKE_INTERRUPT_MACHINE_CHECK:
  725. printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
  726. kvmppc_dump_vcpu(vcpu);
  727. /* For debugging, send invalid exit reason to user space */
  728. run->hw.hardware_exit_reason = ~1ULL << 32;
  729. run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR);
  730. r = RESUME_HOST;
  731. break;
  732. case BOOKE_INTERRUPT_EXTERNAL:
  733. kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
  734. r = RESUME_GUEST;
  735. break;
  736. case BOOKE_INTERRUPT_DECREMENTER:
  737. kvmppc_account_exit(vcpu, DEC_EXITS);
  738. r = RESUME_GUEST;
  739. break;
  740. case BOOKE_INTERRUPT_WATCHDOG:
  741. r = RESUME_GUEST;
  742. break;
  743. case BOOKE_INTERRUPT_DOORBELL:
  744. kvmppc_account_exit(vcpu, DBELL_EXITS);
  745. r = RESUME_GUEST;
  746. break;
  747. case BOOKE_INTERRUPT_GUEST_DBELL_CRIT:
  748. kvmppc_account_exit(vcpu, GDBELL_EXITS);
  749. /*
  750. * We are here because there is a pending guest interrupt
  751. * which could not be delivered as MSR_CE or MSR_ME was not
  752. * set. Once we break from here we will retry delivery.
  753. */
  754. r = RESUME_GUEST;
  755. break;
  756. case BOOKE_INTERRUPT_GUEST_DBELL:
  757. kvmppc_account_exit(vcpu, GDBELL_EXITS);
  758. /*
  759. * We are here because there is a pending guest interrupt
  760. * which could not be delivered as MSR_EE was not set. Once
  761. * we break from here we will retry delivery.
  762. */
  763. r = RESUME_GUEST;
  764. break;
  765. case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
  766. r = RESUME_GUEST;
  767. break;
  768. case BOOKE_INTERRUPT_HV_PRIV:
  769. r = emulation_exit(run, vcpu);
  770. break;
  771. case BOOKE_INTERRUPT_PROGRAM:
  772. if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) {
  773. /*
  774. * Program traps generated by user-level software must
  775. * be handled by the guest kernel.
  776. *
  777. * In GS mode, hypervisor privileged instructions trap
  778. * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are
  779. * actual program interrupts, handled by the guest.
  780. */
  781. kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
  782. r = RESUME_GUEST;
  783. kvmppc_account_exit(vcpu, USR_PR_INST);
  784. break;
  785. }
  786. r = emulation_exit(run, vcpu);
  787. break;
  788. case BOOKE_INTERRUPT_FP_UNAVAIL:
  789. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
  790. kvmppc_account_exit(vcpu, FP_UNAVAIL);
  791. r = RESUME_GUEST;
  792. break;
  793. #ifdef CONFIG_SPE
  794. case BOOKE_INTERRUPT_SPE_UNAVAIL: {
  795. if (vcpu->arch.shared->msr & MSR_SPE)
  796. kvmppc_vcpu_enable_spe(vcpu);
  797. else
  798. kvmppc_booke_queue_irqprio(vcpu,
  799. BOOKE_IRQPRIO_SPE_UNAVAIL);
  800. r = RESUME_GUEST;
  801. break;
  802. }
  803. case BOOKE_INTERRUPT_SPE_FP_DATA:
  804. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
  805. r = RESUME_GUEST;
  806. break;
  807. case BOOKE_INTERRUPT_SPE_FP_ROUND:
  808. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
  809. r = RESUME_GUEST;
  810. break;
  811. #else
  812. case BOOKE_INTERRUPT_SPE_UNAVAIL:
  813. /*
  814. * Guest wants SPE, but host kernel doesn't support it. Send
  815. * an "unimplemented operation" program check to the guest.
  816. */
  817. kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
  818. r = RESUME_GUEST;
  819. break;
  820. /*
  821. * These really should never happen without CONFIG_SPE,
  822. * as we should never enable the real MSR[SPE] in the guest.
  823. */
  824. case BOOKE_INTERRUPT_SPE_FP_DATA:
  825. case BOOKE_INTERRUPT_SPE_FP_ROUND:
  826. printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
  827. __func__, exit_nr, vcpu->arch.pc);
  828. run->hw.hardware_exit_reason = exit_nr;
  829. r = RESUME_HOST;
  830. break;
  831. #endif
  832. case BOOKE_INTERRUPT_DATA_STORAGE:
  833. kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
  834. vcpu->arch.fault_esr);
  835. kvmppc_account_exit(vcpu, DSI_EXITS);
  836. r = RESUME_GUEST;
  837. break;
  838. case BOOKE_INTERRUPT_INST_STORAGE:
  839. kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
  840. kvmppc_account_exit(vcpu, ISI_EXITS);
  841. r = RESUME_GUEST;
  842. break;
  843. case BOOKE_INTERRUPT_ALIGNMENT:
  844. kvmppc_core_queue_alignment(vcpu, vcpu->arch.fault_dear,
  845. vcpu->arch.fault_esr);
  846. r = RESUME_GUEST;
  847. break;
  848. #ifdef CONFIG_KVM_BOOKE_HV
  849. case BOOKE_INTERRUPT_HV_SYSCALL:
  850. if (!(vcpu->arch.shared->msr & MSR_PR)) {
  851. kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
  852. } else {
  853. /*
  854. * hcall from guest userspace -- send privileged
  855. * instruction program check.
  856. */
  857. kvmppc_core_queue_program(vcpu, ESR_PPR);
  858. }
  859. r = RESUME_GUEST;
  860. break;
  861. #else
  862. case BOOKE_INTERRUPT_SYSCALL:
  863. if (!(vcpu->arch.shared->msr & MSR_PR) &&
  864. (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
  865. /* KVM PV hypercalls */
  866. kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
  867. r = RESUME_GUEST;
  868. } else {
  869. /* Guest syscalls */
  870. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
  871. }
  872. kvmppc_account_exit(vcpu, SYSCALL_EXITS);
  873. r = RESUME_GUEST;
  874. break;
  875. #endif
  876. case BOOKE_INTERRUPT_DTLB_MISS: {
  877. unsigned long eaddr = vcpu->arch.fault_dear;
  878. int gtlb_index;
  879. gpa_t gpaddr;
  880. gfn_t gfn;
  881. #ifdef CONFIG_KVM_E500V2
  882. if (!(vcpu->arch.shared->msr & MSR_PR) &&
  883. (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
  884. kvmppc_map_magic(vcpu);
  885. kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
  886. r = RESUME_GUEST;
  887. break;
  888. }
  889. #endif
  890. /* Check the guest TLB. */
  891. gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
  892. if (gtlb_index < 0) {
  893. /* The guest didn't have a mapping for it. */
  894. kvmppc_core_queue_dtlb_miss(vcpu,
  895. vcpu->arch.fault_dear,
  896. vcpu->arch.fault_esr);
  897. kvmppc_mmu_dtlb_miss(vcpu);
  898. kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
  899. r = RESUME_GUEST;
  900. break;
  901. }
  902. gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
  903. gfn = gpaddr >> PAGE_SHIFT;
  904. if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
  905. /* The guest TLB had a mapping, but the shadow TLB
  906. * didn't, and it is RAM. This could be because:
  907. * a) the entry is mapping the host kernel, or
  908. * b) the guest used a large mapping which we're faking
  909. * Either way, we need to satisfy the fault without
  910. * invoking the guest. */
  911. kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
  912. kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
  913. r = RESUME_GUEST;
  914. } else {
  915. /* Guest has mapped and accessed a page which is not
  916. * actually RAM. */
  917. vcpu->arch.paddr_accessed = gpaddr;
  918. vcpu->arch.vaddr_accessed = eaddr;
  919. r = kvmppc_emulate_mmio(run, vcpu);
  920. kvmppc_account_exit(vcpu, MMIO_EXITS);
  921. }
  922. break;
  923. }
  924. case BOOKE_INTERRUPT_ITLB_MISS: {
  925. unsigned long eaddr = vcpu->arch.pc;
  926. gpa_t gpaddr;
  927. gfn_t gfn;
  928. int gtlb_index;
  929. r = RESUME_GUEST;
  930. /* Check the guest TLB. */
  931. gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
  932. if (gtlb_index < 0) {
  933. /* The guest didn't have a mapping for it. */
  934. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
  935. kvmppc_mmu_itlb_miss(vcpu);
  936. kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
  937. break;
  938. }
  939. kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
  940. gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
  941. gfn = gpaddr >> PAGE_SHIFT;
  942. if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
  943. /* The guest TLB had a mapping, but the shadow TLB
  944. * didn't. This could be because:
  945. * a) the entry is mapping the host kernel, or
  946. * b) the guest used a large mapping which we're faking
  947. * Either way, we need to satisfy the fault without
  948. * invoking the guest. */
  949. kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
  950. } else {
  951. /* Guest mapped and leaped at non-RAM! */
  952. kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
  953. }
  954. break;
  955. }
  956. case BOOKE_INTERRUPT_DEBUG: {
  957. u32 dbsr;
  958. vcpu->arch.pc = mfspr(SPRN_CSRR0);
  959. /* clear IAC events in DBSR register */
  960. dbsr = mfspr(SPRN_DBSR);
  961. dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
  962. mtspr(SPRN_DBSR, dbsr);
  963. run->exit_reason = KVM_EXIT_DEBUG;
  964. kvmppc_account_exit(vcpu, DEBUG_EXITS);
  965. r = RESUME_HOST;
  966. break;
  967. }
  968. default:
  969. printk(KERN_EMERG "exit_nr %d\n", exit_nr);
  970. BUG();
  971. }
  972. /*
  973. * To avoid clobbering exit_reason, only check for signals if we
  974. * aren't already exiting to userspace for some other reason.
  975. */
  976. if (!(r & RESUME_HOST)) {
  977. local_irq_disable();
  978. s = kvmppc_prepare_to_enter(vcpu);
  979. if (s <= 0) {
  980. local_irq_enable();
  981. r = (s << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
  982. } else {
  983. kvmppc_lazy_ee_enable();
  984. }
  985. }
  986. return r;
  987. }
  988. static void kvmppc_set_tsr(struct kvm_vcpu *vcpu, u32 new_tsr)
  989. {
  990. u32 old_tsr = vcpu->arch.tsr;
  991. vcpu->arch.tsr = new_tsr;
  992. if ((old_tsr ^ vcpu->arch.tsr) & (TSR_ENW | TSR_WIS))
  993. arm_next_watchdog(vcpu);
  994. update_timer_ints(vcpu);
  995. }
  996. /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
  997. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
  998. {
  999. int i;
  1000. int r;
  1001. vcpu->arch.pc = 0;
  1002. vcpu->arch.shared->pir = vcpu->vcpu_id;
  1003. kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
  1004. kvmppc_set_msr(vcpu, 0);
  1005. #ifndef CONFIG_KVM_BOOKE_HV
  1006. vcpu->arch.shadow_msr = MSR_USER | MSR_DE | MSR_IS | MSR_DS;
  1007. vcpu->arch.shadow_pid = 1;
  1008. vcpu->arch.shared->msr = 0;
  1009. #endif
  1010. /* Eye-catching numbers so we know if the guest takes an interrupt
  1011. * before it's programmed its own IVPR/IVORs. */
  1012. vcpu->arch.ivpr = 0x55550000;
  1013. for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
  1014. vcpu->arch.ivor[i] = 0x7700 | i * 4;
  1015. kvmppc_init_timing_stats(vcpu);
  1016. r = kvmppc_core_vcpu_setup(vcpu);
  1017. kvmppc_sanity_check(vcpu);
  1018. return r;
  1019. }
  1020. int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu)
  1021. {
  1022. /* setup watchdog timer once */
  1023. spin_lock_init(&vcpu->arch.wdt_lock);
  1024. setup_timer(&vcpu->arch.wdt_timer, kvmppc_watchdog_func,
  1025. (unsigned long)vcpu);
  1026. return 0;
  1027. }
  1028. void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu)
  1029. {
  1030. del_timer_sync(&vcpu->arch.wdt_timer);
  1031. }
  1032. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  1033. {
  1034. int i;
  1035. regs->pc = vcpu->arch.pc;
  1036. regs->cr = kvmppc_get_cr(vcpu);
  1037. regs->ctr = vcpu->arch.ctr;
  1038. regs->lr = vcpu->arch.lr;
  1039. regs->xer = kvmppc_get_xer(vcpu);
  1040. regs->msr = vcpu->arch.shared->msr;
  1041. regs->srr0 = vcpu->arch.shared->srr0;
  1042. regs->srr1 = vcpu->arch.shared->srr1;
  1043. regs->pid = vcpu->arch.pid;
  1044. regs->sprg0 = vcpu->arch.shared->sprg0;
  1045. regs->sprg1 = vcpu->arch.shared->sprg1;
  1046. regs->sprg2 = vcpu->arch.shared->sprg2;
  1047. regs->sprg3 = vcpu->arch.shared->sprg3;
  1048. regs->sprg4 = vcpu->arch.shared->sprg4;
  1049. regs->sprg5 = vcpu->arch.shared->sprg5;
  1050. regs->sprg6 = vcpu->arch.shared->sprg6;
  1051. regs->sprg7 = vcpu->arch.shared->sprg7;
  1052. for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
  1053. regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
  1054. return 0;
  1055. }
  1056. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  1057. {
  1058. int i;
  1059. vcpu->arch.pc = regs->pc;
  1060. kvmppc_set_cr(vcpu, regs->cr);
  1061. vcpu->arch.ctr = regs->ctr;
  1062. vcpu->arch.lr = regs->lr;
  1063. kvmppc_set_xer(vcpu, regs->xer);
  1064. kvmppc_set_msr(vcpu, regs->msr);
  1065. vcpu->arch.shared->srr0 = regs->srr0;
  1066. vcpu->arch.shared->srr1 = regs->srr1;
  1067. kvmppc_set_pid(vcpu, regs->pid);
  1068. vcpu->arch.shared->sprg0 = regs->sprg0;
  1069. vcpu->arch.shared->sprg1 = regs->sprg1;
  1070. vcpu->arch.shared->sprg2 = regs->sprg2;
  1071. vcpu->arch.shared->sprg3 = regs->sprg3;
  1072. vcpu->arch.shared->sprg4 = regs->sprg4;
  1073. vcpu->arch.shared->sprg5 = regs->sprg5;
  1074. vcpu->arch.shared->sprg6 = regs->sprg6;
  1075. vcpu->arch.shared->sprg7 = regs->sprg7;
  1076. for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
  1077. kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
  1078. return 0;
  1079. }
  1080. static void get_sregs_base(struct kvm_vcpu *vcpu,
  1081. struct kvm_sregs *sregs)
  1082. {
  1083. u64 tb = get_tb();
  1084. sregs->u.e.features |= KVM_SREGS_E_BASE;
  1085. sregs->u.e.csrr0 = vcpu->arch.csrr0;
  1086. sregs->u.e.csrr1 = vcpu->arch.csrr1;
  1087. sregs->u.e.mcsr = vcpu->arch.mcsr;
  1088. sregs->u.e.esr = get_guest_esr(vcpu);
  1089. sregs->u.e.dear = get_guest_dear(vcpu);
  1090. sregs->u.e.tsr = vcpu->arch.tsr;
  1091. sregs->u.e.tcr = vcpu->arch.tcr;
  1092. sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
  1093. sregs->u.e.tb = tb;
  1094. sregs->u.e.vrsave = vcpu->arch.vrsave;
  1095. }
  1096. static int set_sregs_base(struct kvm_vcpu *vcpu,
  1097. struct kvm_sregs *sregs)
  1098. {
  1099. if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
  1100. return 0;
  1101. vcpu->arch.csrr0 = sregs->u.e.csrr0;
  1102. vcpu->arch.csrr1 = sregs->u.e.csrr1;
  1103. vcpu->arch.mcsr = sregs->u.e.mcsr;
  1104. set_guest_esr(vcpu, sregs->u.e.esr);
  1105. set_guest_dear(vcpu, sregs->u.e.dear);
  1106. vcpu->arch.vrsave = sregs->u.e.vrsave;
  1107. kvmppc_set_tcr(vcpu, sregs->u.e.tcr);
  1108. if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) {
  1109. vcpu->arch.dec = sregs->u.e.dec;
  1110. kvmppc_emulate_dec(vcpu);
  1111. }
  1112. if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR)
  1113. kvmppc_set_tsr(vcpu, sregs->u.e.tsr);
  1114. return 0;
  1115. }
  1116. static void get_sregs_arch206(struct kvm_vcpu *vcpu,
  1117. struct kvm_sregs *sregs)
  1118. {
  1119. sregs->u.e.features |= KVM_SREGS_E_ARCH206;
  1120. sregs->u.e.pir = vcpu->vcpu_id;
  1121. sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
  1122. sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
  1123. sregs->u.e.decar = vcpu->arch.decar;
  1124. sregs->u.e.ivpr = vcpu->arch.ivpr;
  1125. }
  1126. static int set_sregs_arch206(struct kvm_vcpu *vcpu,
  1127. struct kvm_sregs *sregs)
  1128. {
  1129. if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
  1130. return 0;
  1131. if (sregs->u.e.pir != vcpu->vcpu_id)
  1132. return -EINVAL;
  1133. vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
  1134. vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
  1135. vcpu->arch.decar = sregs->u.e.decar;
  1136. vcpu->arch.ivpr = sregs->u.e.ivpr;
  1137. return 0;
  1138. }
  1139. void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  1140. {
  1141. sregs->u.e.features |= KVM_SREGS_E_IVOR;
  1142. sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
  1143. sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
  1144. sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
  1145. sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
  1146. sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
  1147. sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
  1148. sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
  1149. sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
  1150. sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
  1151. sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
  1152. sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
  1153. sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
  1154. sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
  1155. sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
  1156. sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
  1157. sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
  1158. }
  1159. int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
  1160. {
  1161. if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
  1162. return 0;
  1163. vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
  1164. vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
  1165. vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
  1166. vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
  1167. vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
  1168. vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
  1169. vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
  1170. vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
  1171. vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
  1172. vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
  1173. vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
  1174. vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
  1175. vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
  1176. vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
  1177. vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
  1178. vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
  1179. return 0;
  1180. }
  1181. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  1182. struct kvm_sregs *sregs)
  1183. {
  1184. sregs->pvr = vcpu->arch.pvr;
  1185. get_sregs_base(vcpu, sregs);
  1186. get_sregs_arch206(vcpu, sregs);
  1187. kvmppc_core_get_sregs(vcpu, sregs);
  1188. return 0;
  1189. }
  1190. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  1191. struct kvm_sregs *sregs)
  1192. {
  1193. int ret;
  1194. if (vcpu->arch.pvr != sregs->pvr)
  1195. return -EINVAL;
  1196. ret = set_sregs_base(vcpu, sregs);
  1197. if (ret < 0)
  1198. return ret;
  1199. ret = set_sregs_arch206(vcpu, sregs);
  1200. if (ret < 0)
  1201. return ret;
  1202. return kvmppc_core_set_sregs(vcpu, sregs);
  1203. }
  1204. int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  1205. {
  1206. int r = -EINVAL;
  1207. switch (reg->id) {
  1208. case KVM_REG_PPC_IAC1:
  1209. case KVM_REG_PPC_IAC2:
  1210. case KVM_REG_PPC_IAC3:
  1211. case KVM_REG_PPC_IAC4: {
  1212. int iac = reg->id - KVM_REG_PPC_IAC1;
  1213. r = copy_to_user((u64 __user *)(long)reg->addr,
  1214. &vcpu->arch.dbg_reg.iac[iac], sizeof(u64));
  1215. break;
  1216. }
  1217. case KVM_REG_PPC_DAC1:
  1218. case KVM_REG_PPC_DAC2: {
  1219. int dac = reg->id - KVM_REG_PPC_DAC1;
  1220. r = copy_to_user((u64 __user *)(long)reg->addr,
  1221. &vcpu->arch.dbg_reg.dac[dac], sizeof(u64));
  1222. break;
  1223. }
  1224. case KVM_REG_PPC_EPR: {
  1225. u32 epr = get_guest_epr(vcpu);
  1226. r = put_user(epr, (u32 __user *)(long)reg->addr);
  1227. break;
  1228. }
  1229. #if defined(CONFIG_64BIT)
  1230. case KVM_REG_PPC_EPCR:
  1231. r = put_user(vcpu->arch.epcr, (u32 __user *)(long)reg->addr);
  1232. break;
  1233. #endif
  1234. case KVM_REG_PPC_TCR:
  1235. r = put_user(vcpu->arch.tcr, (u32 __user *)(long)reg->addr);
  1236. break;
  1237. case KVM_REG_PPC_TSR:
  1238. r = put_user(vcpu->arch.tsr, (u32 __user *)(long)reg->addr);
  1239. break;
  1240. default:
  1241. break;
  1242. }
  1243. return r;
  1244. }
  1245. int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
  1246. {
  1247. int r = -EINVAL;
  1248. switch (reg->id) {
  1249. case KVM_REG_PPC_IAC1:
  1250. case KVM_REG_PPC_IAC2:
  1251. case KVM_REG_PPC_IAC3:
  1252. case KVM_REG_PPC_IAC4: {
  1253. int iac = reg->id - KVM_REG_PPC_IAC1;
  1254. r = copy_from_user(&vcpu->arch.dbg_reg.iac[iac],
  1255. (u64 __user *)(long)reg->addr, sizeof(u64));
  1256. break;
  1257. }
  1258. case KVM_REG_PPC_DAC1:
  1259. case KVM_REG_PPC_DAC2: {
  1260. int dac = reg->id - KVM_REG_PPC_DAC1;
  1261. r = copy_from_user(&vcpu->arch.dbg_reg.dac[dac],
  1262. (u64 __user *)(long)reg->addr, sizeof(u64));
  1263. break;
  1264. }
  1265. case KVM_REG_PPC_EPR: {
  1266. u32 new_epr;
  1267. r = get_user(new_epr, (u32 __user *)(long)reg->addr);
  1268. if (!r)
  1269. kvmppc_set_epr(vcpu, new_epr);
  1270. break;
  1271. }
  1272. #if defined(CONFIG_64BIT)
  1273. case KVM_REG_PPC_EPCR: {
  1274. u32 new_epcr;
  1275. r = get_user(new_epcr, (u32 __user *)(long)reg->addr);
  1276. if (r == 0)
  1277. kvmppc_set_epcr(vcpu, new_epcr);
  1278. break;
  1279. }
  1280. #endif
  1281. case KVM_REG_PPC_OR_TSR: {
  1282. u32 tsr_bits;
  1283. r = get_user(tsr_bits, (u32 __user *)(long)reg->addr);
  1284. kvmppc_set_tsr_bits(vcpu, tsr_bits);
  1285. break;
  1286. }
  1287. case KVM_REG_PPC_CLEAR_TSR: {
  1288. u32 tsr_bits;
  1289. r = get_user(tsr_bits, (u32 __user *)(long)reg->addr);
  1290. kvmppc_clr_tsr_bits(vcpu, tsr_bits);
  1291. break;
  1292. }
  1293. case KVM_REG_PPC_TSR: {
  1294. u32 tsr;
  1295. r = get_user(tsr, (u32 __user *)(long)reg->addr);
  1296. kvmppc_set_tsr(vcpu, tsr);
  1297. break;
  1298. }
  1299. case KVM_REG_PPC_TCR: {
  1300. u32 tcr;
  1301. r = get_user(tcr, (u32 __user *)(long)reg->addr);
  1302. kvmppc_set_tcr(vcpu, tcr);
  1303. break;
  1304. }
  1305. default:
  1306. break;
  1307. }
  1308. return r;
  1309. }
  1310. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  1311. {
  1312. return -ENOTSUPP;
  1313. }
  1314. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  1315. {
  1316. return -ENOTSUPP;
  1317. }
  1318. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  1319. struct kvm_translation *tr)
  1320. {
  1321. int r;
  1322. r = kvmppc_core_vcpu_translate(vcpu, tr);
  1323. return r;
  1324. }
  1325. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
  1326. {
  1327. return -ENOTSUPP;
  1328. }
  1329. void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
  1330. struct kvm_memory_slot *dont)
  1331. {
  1332. }
  1333. int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
  1334. unsigned long npages)
  1335. {
  1336. return 0;
  1337. }
  1338. int kvmppc_core_prepare_memory_region(struct kvm *kvm,
  1339. struct kvm_memory_slot *memslot,
  1340. struct kvm_userspace_memory_region *mem)
  1341. {
  1342. return 0;
  1343. }
  1344. void kvmppc_core_commit_memory_region(struct kvm *kvm,
  1345. struct kvm_userspace_memory_region *mem,
  1346. const struct kvm_memory_slot *old)
  1347. {
  1348. }
  1349. void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
  1350. {
  1351. }
  1352. void kvmppc_set_epcr(struct kvm_vcpu *vcpu, u32 new_epcr)
  1353. {
  1354. #if defined(CONFIG_64BIT)
  1355. vcpu->arch.epcr = new_epcr;
  1356. #ifdef CONFIG_KVM_BOOKE_HV
  1357. vcpu->arch.shadow_epcr &= ~SPRN_EPCR_GICM;
  1358. if (vcpu->arch.epcr & SPRN_EPCR_ICM)
  1359. vcpu->arch.shadow_epcr |= SPRN_EPCR_GICM;
  1360. #endif
  1361. #endif
  1362. }
  1363. void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr)
  1364. {
  1365. vcpu->arch.tcr = new_tcr;
  1366. arm_next_watchdog(vcpu);
  1367. update_timer_ints(vcpu);
  1368. }
  1369. void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
  1370. {
  1371. set_bits(tsr_bits, &vcpu->arch.tsr);
  1372. smp_wmb();
  1373. kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
  1374. kvm_vcpu_kick(vcpu);
  1375. }
  1376. void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
  1377. {
  1378. clear_bits(tsr_bits, &vcpu->arch.tsr);
  1379. /*
  1380. * We may have stopped the watchdog due to
  1381. * being stuck on final expiration.
  1382. */
  1383. if (tsr_bits & (TSR_ENW | TSR_WIS))
  1384. arm_next_watchdog(vcpu);
  1385. update_timer_ints(vcpu);
  1386. }
  1387. void kvmppc_decrementer_func(unsigned long data)
  1388. {
  1389. struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
  1390. if (vcpu->arch.tcr & TCR_ARE) {
  1391. vcpu->arch.dec = vcpu->arch.decar;
  1392. kvmppc_emulate_dec(vcpu);
  1393. }
  1394. kvmppc_set_tsr_bits(vcpu, TSR_DIS);
  1395. }
  1396. void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1397. {
  1398. vcpu->cpu = smp_processor_id();
  1399. current->thread.kvm_vcpu = vcpu;
  1400. }
  1401. void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu)
  1402. {
  1403. current->thread.kvm_vcpu = NULL;
  1404. vcpu->cpu = -1;
  1405. }
  1406. int __init kvmppc_booke_init(void)
  1407. {
  1408. #ifndef CONFIG_KVM_BOOKE_HV
  1409. unsigned long ivor[16];
  1410. unsigned long *handler = kvmppc_booke_handler_addr;
  1411. unsigned long max_ivor = 0;
  1412. unsigned long handler_len;
  1413. int i;
  1414. /* We install our own exception handlers by hijacking IVPR. IVPR must
  1415. * be 16-bit aligned, so we need a 64KB allocation. */
  1416. kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
  1417. VCPU_SIZE_ORDER);
  1418. if (!kvmppc_booke_handlers)
  1419. return -ENOMEM;
  1420. /* XXX make sure our handlers are smaller than Linux's */
  1421. /* Copy our interrupt handlers to match host IVORs. That way we don't
  1422. * have to swap the IVORs on every guest/host transition. */
  1423. ivor[0] = mfspr(SPRN_IVOR0);
  1424. ivor[1] = mfspr(SPRN_IVOR1);
  1425. ivor[2] = mfspr(SPRN_IVOR2);
  1426. ivor[3] = mfspr(SPRN_IVOR3);
  1427. ivor[4] = mfspr(SPRN_IVOR4);
  1428. ivor[5] = mfspr(SPRN_IVOR5);
  1429. ivor[6] = mfspr(SPRN_IVOR6);
  1430. ivor[7] = mfspr(SPRN_IVOR7);
  1431. ivor[8] = mfspr(SPRN_IVOR8);
  1432. ivor[9] = mfspr(SPRN_IVOR9);
  1433. ivor[10] = mfspr(SPRN_IVOR10);
  1434. ivor[11] = mfspr(SPRN_IVOR11);
  1435. ivor[12] = mfspr(SPRN_IVOR12);
  1436. ivor[13] = mfspr(SPRN_IVOR13);
  1437. ivor[14] = mfspr(SPRN_IVOR14);
  1438. ivor[15] = mfspr(SPRN_IVOR15);
  1439. for (i = 0; i < 16; i++) {
  1440. if (ivor[i] > max_ivor)
  1441. max_ivor = i;
  1442. handler_len = handler[i + 1] - handler[i];
  1443. memcpy((void *)kvmppc_booke_handlers + ivor[i],
  1444. (void *)handler[i], handler_len);
  1445. }
  1446. handler_len = handler[max_ivor + 1] - handler[max_ivor];
  1447. flush_icache_range(kvmppc_booke_handlers, kvmppc_booke_handlers +
  1448. ivor[max_ivor] + handler_len);
  1449. #endif /* !BOOKE_HV */
  1450. return 0;
  1451. }
  1452. void __exit kvmppc_booke_exit(void)
  1453. {
  1454. free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
  1455. kvm_exit();
  1456. }