booke.c 40 KB

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