booke.c 52 KB

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