booke.c 45 KB

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