booke.c 41 KB

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