booke.c 45 KB

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