booke.c 41 KB

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