booke.c 34 KB

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