kvm_mips_emul.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * KVM/MIPS: Instruction/Exception emulation
  7. *
  8. * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
  9. * Authors: Sanjay Lal <sanjayl@kymasys.com>
  10. */
  11. #include <linux/errno.h>
  12. #include <linux/err.h>
  13. #include <linux/kvm_host.h>
  14. #include <linux/module.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/fs.h>
  17. #include <linux/bootmem.h>
  18. #include <linux/random.h>
  19. #include <asm/page.h>
  20. #include <asm/cacheflush.h>
  21. #include <asm/cpu-info.h>
  22. #include <asm/mmu_context.h>
  23. #include <asm/tlbflush.h>
  24. #include <asm/inst.h>
  25. #undef CONFIG_MIPS_MT
  26. #include <asm/r4kcache.h>
  27. #define CONFIG_MIPS_MT
  28. #include "kvm_mips_opcode.h"
  29. #include "kvm_mips_int.h"
  30. #include "kvm_mips_comm.h"
  31. #include "trace.h"
  32. /*
  33. * Compute the return address and do emulate branch simulation, if required.
  34. * This function should be called only in branch delay slot active.
  35. */
  36. unsigned long kvm_compute_return_epc(struct kvm_vcpu *vcpu,
  37. unsigned long instpc)
  38. {
  39. unsigned int dspcontrol;
  40. union mips_instruction insn;
  41. struct kvm_vcpu_arch *arch = &vcpu->arch;
  42. long epc = instpc;
  43. long nextpc = KVM_INVALID_INST;
  44. if (epc & 3)
  45. goto unaligned;
  46. /*
  47. * Read the instruction
  48. */
  49. insn.word = kvm_get_inst((uint32_t *) epc, vcpu);
  50. if (insn.word == KVM_INVALID_INST)
  51. return KVM_INVALID_INST;
  52. switch (insn.i_format.opcode) {
  53. /*
  54. * jr and jalr are in r_format format.
  55. */
  56. case spec_op:
  57. switch (insn.r_format.func) {
  58. case jalr_op:
  59. arch->gprs[insn.r_format.rd] = epc + 8;
  60. /* Fall through */
  61. case jr_op:
  62. nextpc = arch->gprs[insn.r_format.rs];
  63. break;
  64. }
  65. break;
  66. /*
  67. * This group contains:
  68. * bltz_op, bgez_op, bltzl_op, bgezl_op,
  69. * bltzal_op, bgezal_op, bltzall_op, bgezall_op.
  70. */
  71. case bcond_op:
  72. switch (insn.i_format.rt) {
  73. case bltz_op:
  74. case bltzl_op:
  75. if ((long)arch->gprs[insn.i_format.rs] < 0)
  76. epc = epc + 4 + (insn.i_format.simmediate << 2);
  77. else
  78. epc += 8;
  79. nextpc = epc;
  80. break;
  81. case bgez_op:
  82. case bgezl_op:
  83. if ((long)arch->gprs[insn.i_format.rs] >= 0)
  84. epc = epc + 4 + (insn.i_format.simmediate << 2);
  85. else
  86. epc += 8;
  87. nextpc = epc;
  88. break;
  89. case bltzal_op:
  90. case bltzall_op:
  91. arch->gprs[31] = epc + 8;
  92. if ((long)arch->gprs[insn.i_format.rs] < 0)
  93. epc = epc + 4 + (insn.i_format.simmediate << 2);
  94. else
  95. epc += 8;
  96. nextpc = epc;
  97. break;
  98. case bgezal_op:
  99. case bgezall_op:
  100. arch->gprs[31] = epc + 8;
  101. if ((long)arch->gprs[insn.i_format.rs] >= 0)
  102. epc = epc + 4 + (insn.i_format.simmediate << 2);
  103. else
  104. epc += 8;
  105. nextpc = epc;
  106. break;
  107. case bposge32_op:
  108. if (!cpu_has_dsp)
  109. goto sigill;
  110. dspcontrol = rddsp(0x01);
  111. if (dspcontrol >= 32) {
  112. epc = epc + 4 + (insn.i_format.simmediate << 2);
  113. } else
  114. epc += 8;
  115. nextpc = epc;
  116. break;
  117. }
  118. break;
  119. /*
  120. * These are unconditional and in j_format.
  121. */
  122. case jal_op:
  123. arch->gprs[31] = instpc + 8;
  124. case j_op:
  125. epc += 4;
  126. epc >>= 28;
  127. epc <<= 28;
  128. epc |= (insn.j_format.target << 2);
  129. nextpc = epc;
  130. break;
  131. /*
  132. * These are conditional and in i_format.
  133. */
  134. case beq_op:
  135. case beql_op:
  136. if (arch->gprs[insn.i_format.rs] ==
  137. arch->gprs[insn.i_format.rt])
  138. epc = epc + 4 + (insn.i_format.simmediate << 2);
  139. else
  140. epc += 8;
  141. nextpc = epc;
  142. break;
  143. case bne_op:
  144. case bnel_op:
  145. if (arch->gprs[insn.i_format.rs] !=
  146. arch->gprs[insn.i_format.rt])
  147. epc = epc + 4 + (insn.i_format.simmediate << 2);
  148. else
  149. epc += 8;
  150. nextpc = epc;
  151. break;
  152. case blez_op: /* not really i_format */
  153. case blezl_op:
  154. /* rt field assumed to be zero */
  155. if ((long)arch->gprs[insn.i_format.rs] <= 0)
  156. epc = epc + 4 + (insn.i_format.simmediate << 2);
  157. else
  158. epc += 8;
  159. nextpc = epc;
  160. break;
  161. case bgtz_op:
  162. case bgtzl_op:
  163. /* rt field assumed to be zero */
  164. if ((long)arch->gprs[insn.i_format.rs] > 0)
  165. epc = epc + 4 + (insn.i_format.simmediate << 2);
  166. else
  167. epc += 8;
  168. nextpc = epc;
  169. break;
  170. /*
  171. * And now the FPA/cp1 branch instructions.
  172. */
  173. case cop1_op:
  174. printk("%s: unsupported cop1_op\n", __func__);
  175. break;
  176. }
  177. return nextpc;
  178. unaligned:
  179. printk("%s: unaligned epc\n", __func__);
  180. return nextpc;
  181. sigill:
  182. printk("%s: DSP branch but not DSP ASE\n", __func__);
  183. return nextpc;
  184. }
  185. enum emulation_result update_pc(struct kvm_vcpu *vcpu, uint32_t cause)
  186. {
  187. unsigned long branch_pc;
  188. enum emulation_result er = EMULATE_DONE;
  189. if (cause & CAUSEF_BD) {
  190. branch_pc = kvm_compute_return_epc(vcpu, vcpu->arch.pc);
  191. if (branch_pc == KVM_INVALID_INST) {
  192. er = EMULATE_FAIL;
  193. } else {
  194. vcpu->arch.pc = branch_pc;
  195. kvm_debug("BD update_pc(): New PC: %#lx\n", vcpu->arch.pc);
  196. }
  197. } else
  198. vcpu->arch.pc += 4;
  199. kvm_debug("update_pc(): New PC: %#lx\n", vcpu->arch.pc);
  200. return er;
  201. }
  202. /* Everytime the compare register is written to, we need to decide when to fire
  203. * the timer that represents timer ticks to the GUEST.
  204. *
  205. */
  206. enum emulation_result kvm_mips_emulate_count(struct kvm_vcpu *vcpu)
  207. {
  208. struct mips_coproc *cop0 = vcpu->arch.cop0;
  209. enum emulation_result er = EMULATE_DONE;
  210. /* If COUNT is enabled */
  211. if (!(kvm_read_c0_guest_cause(cop0) & CAUSEF_DC)) {
  212. hrtimer_try_to_cancel(&vcpu->arch.comparecount_timer);
  213. hrtimer_start(&vcpu->arch.comparecount_timer,
  214. ktime_set(0, MS_TO_NS(10)), HRTIMER_MODE_REL);
  215. } else {
  216. hrtimer_try_to_cancel(&vcpu->arch.comparecount_timer);
  217. }
  218. return er;
  219. }
  220. enum emulation_result kvm_mips_emul_eret(struct kvm_vcpu *vcpu)
  221. {
  222. struct mips_coproc *cop0 = vcpu->arch.cop0;
  223. enum emulation_result er = EMULATE_DONE;
  224. if (kvm_read_c0_guest_status(cop0) & ST0_EXL) {
  225. kvm_debug("[%#lx] ERET to %#lx\n", vcpu->arch.pc,
  226. kvm_read_c0_guest_epc(cop0));
  227. kvm_clear_c0_guest_status(cop0, ST0_EXL);
  228. vcpu->arch.pc = kvm_read_c0_guest_epc(cop0);
  229. } else if (kvm_read_c0_guest_status(cop0) & ST0_ERL) {
  230. kvm_clear_c0_guest_status(cop0, ST0_ERL);
  231. vcpu->arch.pc = kvm_read_c0_guest_errorepc(cop0);
  232. } else {
  233. printk("[%#lx] ERET when MIPS_SR_EXL|MIPS_SR_ERL == 0\n",
  234. vcpu->arch.pc);
  235. er = EMULATE_FAIL;
  236. }
  237. return er;
  238. }
  239. enum emulation_result kvm_mips_emul_wait(struct kvm_vcpu *vcpu)
  240. {
  241. enum emulation_result er = EMULATE_DONE;
  242. kvm_debug("[%#lx] !!!WAIT!!! (%#lx)\n", vcpu->arch.pc,
  243. vcpu->arch.pending_exceptions);
  244. ++vcpu->stat.wait_exits;
  245. trace_kvm_exit(vcpu, WAIT_EXITS);
  246. if (!vcpu->arch.pending_exceptions) {
  247. vcpu->arch.wait = 1;
  248. kvm_vcpu_block(vcpu);
  249. /* We we are runnable, then definitely go off to user space to check if any
  250. * I/O interrupts are pending.
  251. */
  252. if (kvm_check_request(KVM_REQ_UNHALT, vcpu)) {
  253. clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
  254. vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  255. }
  256. }
  257. return er;
  258. }
  259. /* XXXKYMA: Linux doesn't seem to use TLBR, return EMULATE_FAIL for now so that we can catch
  260. * this, if things ever change
  261. */
  262. enum emulation_result kvm_mips_emul_tlbr(struct kvm_vcpu *vcpu)
  263. {
  264. struct mips_coproc *cop0 = vcpu->arch.cop0;
  265. enum emulation_result er = EMULATE_FAIL;
  266. uint32_t pc = vcpu->arch.pc;
  267. printk("[%#x] COP0_TLBR [%ld]\n", pc, kvm_read_c0_guest_index(cop0));
  268. return er;
  269. }
  270. /* Write Guest TLB Entry @ Index */
  271. enum emulation_result kvm_mips_emul_tlbwi(struct kvm_vcpu *vcpu)
  272. {
  273. struct mips_coproc *cop0 = vcpu->arch.cop0;
  274. int index = kvm_read_c0_guest_index(cop0);
  275. enum emulation_result er = EMULATE_DONE;
  276. struct kvm_mips_tlb *tlb = NULL;
  277. uint32_t pc = vcpu->arch.pc;
  278. if (index < 0 || index >= KVM_MIPS_GUEST_TLB_SIZE) {
  279. printk("%s: illegal index: %d\n", __func__, index);
  280. printk
  281. ("[%#x] COP0_TLBWI [%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx, mask: %#lx)\n",
  282. pc, index, kvm_read_c0_guest_entryhi(cop0),
  283. kvm_read_c0_guest_entrylo0(cop0),
  284. kvm_read_c0_guest_entrylo1(cop0),
  285. kvm_read_c0_guest_pagemask(cop0));
  286. index = (index & ~0x80000000) % KVM_MIPS_GUEST_TLB_SIZE;
  287. }
  288. tlb = &vcpu->arch.guest_tlb[index];
  289. #if 1
  290. /* Probe the shadow host TLB for the entry being overwritten, if one matches, invalidate it */
  291. kvm_mips_host_tlb_inv(vcpu, tlb->tlb_hi);
  292. #endif
  293. tlb->tlb_mask = kvm_read_c0_guest_pagemask(cop0);
  294. tlb->tlb_hi = kvm_read_c0_guest_entryhi(cop0);
  295. tlb->tlb_lo0 = kvm_read_c0_guest_entrylo0(cop0);
  296. tlb->tlb_lo1 = kvm_read_c0_guest_entrylo1(cop0);
  297. kvm_debug
  298. ("[%#x] COP0_TLBWI [%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx, mask: %#lx)\n",
  299. pc, index, kvm_read_c0_guest_entryhi(cop0),
  300. kvm_read_c0_guest_entrylo0(cop0), kvm_read_c0_guest_entrylo1(cop0),
  301. kvm_read_c0_guest_pagemask(cop0));
  302. return er;
  303. }
  304. /* Write Guest TLB Entry @ Random Index */
  305. enum emulation_result kvm_mips_emul_tlbwr(struct kvm_vcpu *vcpu)
  306. {
  307. struct mips_coproc *cop0 = vcpu->arch.cop0;
  308. enum emulation_result er = EMULATE_DONE;
  309. struct kvm_mips_tlb *tlb = NULL;
  310. uint32_t pc = vcpu->arch.pc;
  311. int index;
  312. #if 1
  313. get_random_bytes(&index, sizeof(index));
  314. index &= (KVM_MIPS_GUEST_TLB_SIZE - 1);
  315. #else
  316. index = jiffies % KVM_MIPS_GUEST_TLB_SIZE;
  317. #endif
  318. if (index < 0 || index >= KVM_MIPS_GUEST_TLB_SIZE) {
  319. printk("%s: illegal index: %d\n", __func__, index);
  320. return EMULATE_FAIL;
  321. }
  322. tlb = &vcpu->arch.guest_tlb[index];
  323. #if 1
  324. /* Probe the shadow host TLB for the entry being overwritten, if one matches, invalidate it */
  325. kvm_mips_host_tlb_inv(vcpu, tlb->tlb_hi);
  326. #endif
  327. tlb->tlb_mask = kvm_read_c0_guest_pagemask(cop0);
  328. tlb->tlb_hi = kvm_read_c0_guest_entryhi(cop0);
  329. tlb->tlb_lo0 = kvm_read_c0_guest_entrylo0(cop0);
  330. tlb->tlb_lo1 = kvm_read_c0_guest_entrylo1(cop0);
  331. kvm_debug
  332. ("[%#x] COP0_TLBWR[%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx)\n",
  333. pc, index, kvm_read_c0_guest_entryhi(cop0),
  334. kvm_read_c0_guest_entrylo0(cop0),
  335. kvm_read_c0_guest_entrylo1(cop0));
  336. return er;
  337. }
  338. enum emulation_result kvm_mips_emul_tlbp(struct kvm_vcpu *vcpu)
  339. {
  340. struct mips_coproc *cop0 = vcpu->arch.cop0;
  341. long entryhi = kvm_read_c0_guest_entryhi(cop0);
  342. enum emulation_result er = EMULATE_DONE;
  343. uint32_t pc = vcpu->arch.pc;
  344. int index = -1;
  345. index = kvm_mips_guest_tlb_lookup(vcpu, entryhi);
  346. kvm_write_c0_guest_index(cop0, index);
  347. kvm_debug("[%#x] COP0_TLBP (entryhi: %#lx), index: %d\n", pc, entryhi,
  348. index);
  349. return er;
  350. }
  351. enum emulation_result
  352. kvm_mips_emulate_CP0(uint32_t inst, uint32_t *opc, uint32_t cause,
  353. struct kvm_run *run, struct kvm_vcpu *vcpu)
  354. {
  355. struct mips_coproc *cop0 = vcpu->arch.cop0;
  356. enum emulation_result er = EMULATE_DONE;
  357. int32_t rt, rd, copz, sel, co_bit, op;
  358. uint32_t pc = vcpu->arch.pc;
  359. unsigned long curr_pc;
  360. /*
  361. * Update PC and hold onto current PC in case there is
  362. * an error and we want to rollback the PC
  363. */
  364. curr_pc = vcpu->arch.pc;
  365. er = update_pc(vcpu, cause);
  366. if (er == EMULATE_FAIL) {
  367. return er;
  368. }
  369. copz = (inst >> 21) & 0x1f;
  370. rt = (inst >> 16) & 0x1f;
  371. rd = (inst >> 11) & 0x1f;
  372. sel = inst & 0x7;
  373. co_bit = (inst >> 25) & 1;
  374. /* Verify that the register is valid */
  375. if (rd > MIPS_CP0_DESAVE) {
  376. printk("Invalid rd: %d\n", rd);
  377. er = EMULATE_FAIL;
  378. goto done;
  379. }
  380. if (co_bit) {
  381. op = (inst) & 0xff;
  382. switch (op) {
  383. case tlbr_op: /* Read indexed TLB entry */
  384. er = kvm_mips_emul_tlbr(vcpu);
  385. break;
  386. case tlbwi_op: /* Write indexed */
  387. er = kvm_mips_emul_tlbwi(vcpu);
  388. break;
  389. case tlbwr_op: /* Write random */
  390. er = kvm_mips_emul_tlbwr(vcpu);
  391. break;
  392. case tlbp_op: /* TLB Probe */
  393. er = kvm_mips_emul_tlbp(vcpu);
  394. break;
  395. case rfe_op:
  396. printk("!!!COP0_RFE!!!\n");
  397. break;
  398. case eret_op:
  399. er = kvm_mips_emul_eret(vcpu);
  400. goto dont_update_pc;
  401. break;
  402. case wait_op:
  403. er = kvm_mips_emul_wait(vcpu);
  404. break;
  405. }
  406. } else {
  407. switch (copz) {
  408. case mfc_op:
  409. #ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
  410. cop0->stat[rd][sel]++;
  411. #endif
  412. /* Get reg */
  413. if ((rd == MIPS_CP0_COUNT) && (sel == 0)) {
  414. /* XXXKYMA: Run the Guest count register @ 1/4 the rate of the host */
  415. vcpu->arch.gprs[rt] = (read_c0_count() >> 2);
  416. } else if ((rd == MIPS_CP0_ERRCTL) && (sel == 0)) {
  417. vcpu->arch.gprs[rt] = 0x0;
  418. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  419. kvm_mips_trans_mfc0(inst, opc, vcpu);
  420. #endif
  421. }
  422. else {
  423. vcpu->arch.gprs[rt] = cop0->reg[rd][sel];
  424. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  425. kvm_mips_trans_mfc0(inst, opc, vcpu);
  426. #endif
  427. }
  428. kvm_debug
  429. ("[%#x] MFCz[%d][%d], vcpu->arch.gprs[%d]: %#lx\n",
  430. pc, rd, sel, rt, vcpu->arch.gprs[rt]);
  431. break;
  432. case dmfc_op:
  433. vcpu->arch.gprs[rt] = cop0->reg[rd][sel];
  434. break;
  435. case mtc_op:
  436. #ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
  437. cop0->stat[rd][sel]++;
  438. #endif
  439. if ((rd == MIPS_CP0_TLB_INDEX)
  440. && (vcpu->arch.gprs[rt] >=
  441. KVM_MIPS_GUEST_TLB_SIZE)) {
  442. printk("Invalid TLB Index: %ld",
  443. vcpu->arch.gprs[rt]);
  444. er = EMULATE_FAIL;
  445. break;
  446. }
  447. #define C0_EBASE_CORE_MASK 0xff
  448. if ((rd == MIPS_CP0_PRID) && (sel == 1)) {
  449. /* Preserve CORE number */
  450. kvm_change_c0_guest_ebase(cop0,
  451. ~(C0_EBASE_CORE_MASK),
  452. vcpu->arch.gprs[rt]);
  453. printk("MTCz, cop0->reg[EBASE]: %#lx\n",
  454. kvm_read_c0_guest_ebase(cop0));
  455. } else if (rd == MIPS_CP0_TLB_HI && sel == 0) {
  456. uint32_t nasid = ASID_MASK(vcpu->arch.gprs[rt]);
  457. if ((KSEGX(vcpu->arch.gprs[rt]) != CKSEG0)
  458. &&
  459. (ASID_MASK(kvm_read_c0_guest_entryhi(cop0))
  460. != nasid)) {
  461. kvm_debug
  462. ("MTCz, change ASID from %#lx to %#lx\n",
  463. ASID_MASK(kvm_read_c0_guest_entryhi(cop0)),
  464. ASID_MASK(vcpu->arch.gprs[rt]));
  465. /* Blow away the shadow host TLBs */
  466. kvm_mips_flush_host_tlb(1);
  467. }
  468. kvm_write_c0_guest_entryhi(cop0,
  469. vcpu->arch.gprs[rt]);
  470. }
  471. /* Are we writing to COUNT */
  472. else if ((rd == MIPS_CP0_COUNT) && (sel == 0)) {
  473. /* Linux doesn't seem to write into COUNT, we throw an error
  474. * if we notice a write to COUNT
  475. */
  476. /*er = EMULATE_FAIL; */
  477. goto done;
  478. } else if ((rd == MIPS_CP0_COMPARE) && (sel == 0)) {
  479. kvm_debug("[%#x] MTCz, COMPARE %#lx <- %#lx\n",
  480. pc, kvm_read_c0_guest_compare(cop0),
  481. vcpu->arch.gprs[rt]);
  482. /* If we are writing to COMPARE */
  483. /* Clear pending timer interrupt, if any */
  484. kvm_mips_callbacks->dequeue_timer_int(vcpu);
  485. kvm_write_c0_guest_compare(cop0,
  486. vcpu->arch.gprs[rt]);
  487. } else if ((rd == MIPS_CP0_STATUS) && (sel == 0)) {
  488. kvm_write_c0_guest_status(cop0,
  489. vcpu->arch.gprs[rt]);
  490. /* Make sure that CU1 and NMI bits are never set */
  491. kvm_clear_c0_guest_status(cop0,
  492. (ST0_CU1 | ST0_NMI));
  493. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  494. kvm_mips_trans_mtc0(inst, opc, vcpu);
  495. #endif
  496. } else {
  497. cop0->reg[rd][sel] = vcpu->arch.gprs[rt];
  498. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  499. kvm_mips_trans_mtc0(inst, opc, vcpu);
  500. #endif
  501. }
  502. kvm_debug("[%#x] MTCz, cop0->reg[%d][%d]: %#lx\n", pc,
  503. rd, sel, cop0->reg[rd][sel]);
  504. break;
  505. case dmtc_op:
  506. printk
  507. ("!!!!!!![%#lx]dmtc_op: rt: %d, rd: %d, sel: %d!!!!!!\n",
  508. vcpu->arch.pc, rt, rd, sel);
  509. er = EMULATE_FAIL;
  510. break;
  511. case mfmcz_op:
  512. #ifdef KVM_MIPS_DEBUG_COP0_COUNTERS
  513. cop0->stat[MIPS_CP0_STATUS][0]++;
  514. #endif
  515. if (rt != 0) {
  516. vcpu->arch.gprs[rt] =
  517. kvm_read_c0_guest_status(cop0);
  518. }
  519. /* EI */
  520. if (inst & 0x20) {
  521. kvm_debug("[%#lx] mfmcz_op: EI\n",
  522. vcpu->arch.pc);
  523. kvm_set_c0_guest_status(cop0, ST0_IE);
  524. } else {
  525. kvm_debug("[%#lx] mfmcz_op: DI\n",
  526. vcpu->arch.pc);
  527. kvm_clear_c0_guest_status(cop0, ST0_IE);
  528. }
  529. break;
  530. case wrpgpr_op:
  531. {
  532. uint32_t css =
  533. cop0->reg[MIPS_CP0_STATUS][2] & 0xf;
  534. uint32_t pss =
  535. (cop0->reg[MIPS_CP0_STATUS][2] >> 6) & 0xf;
  536. /* We don't support any shadow register sets, so SRSCtl[PSS] == SRSCtl[CSS] = 0 */
  537. if (css || pss) {
  538. er = EMULATE_FAIL;
  539. break;
  540. }
  541. kvm_debug("WRPGPR[%d][%d] = %#lx\n", pss, rd,
  542. vcpu->arch.gprs[rt]);
  543. vcpu->arch.gprs[rd] = vcpu->arch.gprs[rt];
  544. }
  545. break;
  546. default:
  547. printk
  548. ("[%#lx]MachEmulateCP0: unsupported COP0, copz: 0x%x\n",
  549. vcpu->arch.pc, copz);
  550. er = EMULATE_FAIL;
  551. break;
  552. }
  553. }
  554. done:
  555. /*
  556. * Rollback PC only if emulation was unsuccessful
  557. */
  558. if (er == EMULATE_FAIL) {
  559. vcpu->arch.pc = curr_pc;
  560. }
  561. dont_update_pc:
  562. /*
  563. * This is for special instructions whose emulation
  564. * updates the PC, so do not overwrite the PC under
  565. * any circumstances
  566. */
  567. return er;
  568. }
  569. enum emulation_result
  570. kvm_mips_emulate_store(uint32_t inst, uint32_t cause,
  571. struct kvm_run *run, struct kvm_vcpu *vcpu)
  572. {
  573. enum emulation_result er = EMULATE_DO_MMIO;
  574. int32_t op, base, rt, offset;
  575. uint32_t bytes;
  576. void *data = run->mmio.data;
  577. unsigned long curr_pc;
  578. /*
  579. * Update PC and hold onto current PC in case there is
  580. * an error and we want to rollback the PC
  581. */
  582. curr_pc = vcpu->arch.pc;
  583. er = update_pc(vcpu, cause);
  584. if (er == EMULATE_FAIL)
  585. return er;
  586. rt = (inst >> 16) & 0x1f;
  587. base = (inst >> 21) & 0x1f;
  588. offset = inst & 0xffff;
  589. op = (inst >> 26) & 0x3f;
  590. switch (op) {
  591. case sb_op:
  592. bytes = 1;
  593. if (bytes > sizeof(run->mmio.data)) {
  594. kvm_err("%s: bad MMIO length: %d\n", __func__,
  595. run->mmio.len);
  596. }
  597. run->mmio.phys_addr =
  598. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  599. host_cp0_badvaddr);
  600. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  601. er = EMULATE_FAIL;
  602. break;
  603. }
  604. run->mmio.len = bytes;
  605. run->mmio.is_write = 1;
  606. vcpu->mmio_needed = 1;
  607. vcpu->mmio_is_write = 1;
  608. *(u8 *) data = vcpu->arch.gprs[rt];
  609. kvm_debug("OP_SB: eaddr: %#lx, gpr: %#lx, data: %#x\n",
  610. vcpu->arch.host_cp0_badvaddr, vcpu->arch.gprs[rt],
  611. *(uint8_t *) data);
  612. break;
  613. case sw_op:
  614. bytes = 4;
  615. if (bytes > sizeof(run->mmio.data)) {
  616. kvm_err("%s: bad MMIO length: %d\n", __func__,
  617. run->mmio.len);
  618. }
  619. run->mmio.phys_addr =
  620. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  621. host_cp0_badvaddr);
  622. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  623. er = EMULATE_FAIL;
  624. break;
  625. }
  626. run->mmio.len = bytes;
  627. run->mmio.is_write = 1;
  628. vcpu->mmio_needed = 1;
  629. vcpu->mmio_is_write = 1;
  630. *(uint32_t *) data = vcpu->arch.gprs[rt];
  631. kvm_debug("[%#lx] OP_SW: eaddr: %#lx, gpr: %#lx, data: %#x\n",
  632. vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
  633. vcpu->arch.gprs[rt], *(uint32_t *) data);
  634. break;
  635. case sh_op:
  636. bytes = 2;
  637. if (bytes > sizeof(run->mmio.data)) {
  638. kvm_err("%s: bad MMIO length: %d\n", __func__,
  639. run->mmio.len);
  640. }
  641. run->mmio.phys_addr =
  642. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  643. host_cp0_badvaddr);
  644. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  645. er = EMULATE_FAIL;
  646. break;
  647. }
  648. run->mmio.len = bytes;
  649. run->mmio.is_write = 1;
  650. vcpu->mmio_needed = 1;
  651. vcpu->mmio_is_write = 1;
  652. *(uint16_t *) data = vcpu->arch.gprs[rt];
  653. kvm_debug("[%#lx] OP_SH: eaddr: %#lx, gpr: %#lx, data: %#x\n",
  654. vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
  655. vcpu->arch.gprs[rt], *(uint32_t *) data);
  656. break;
  657. default:
  658. printk("Store not yet supported");
  659. er = EMULATE_FAIL;
  660. break;
  661. }
  662. /*
  663. * Rollback PC if emulation was unsuccessful
  664. */
  665. if (er == EMULATE_FAIL) {
  666. vcpu->arch.pc = curr_pc;
  667. }
  668. return er;
  669. }
  670. enum emulation_result
  671. kvm_mips_emulate_load(uint32_t inst, uint32_t cause,
  672. struct kvm_run *run, struct kvm_vcpu *vcpu)
  673. {
  674. enum emulation_result er = EMULATE_DO_MMIO;
  675. int32_t op, base, rt, offset;
  676. uint32_t bytes;
  677. rt = (inst >> 16) & 0x1f;
  678. base = (inst >> 21) & 0x1f;
  679. offset = inst & 0xffff;
  680. op = (inst >> 26) & 0x3f;
  681. vcpu->arch.pending_load_cause = cause;
  682. vcpu->arch.io_gpr = rt;
  683. switch (op) {
  684. case lw_op:
  685. bytes = 4;
  686. if (bytes > sizeof(run->mmio.data)) {
  687. kvm_err("%s: bad MMIO length: %d\n", __func__,
  688. run->mmio.len);
  689. er = EMULATE_FAIL;
  690. break;
  691. }
  692. run->mmio.phys_addr =
  693. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  694. host_cp0_badvaddr);
  695. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  696. er = EMULATE_FAIL;
  697. break;
  698. }
  699. run->mmio.len = bytes;
  700. run->mmio.is_write = 0;
  701. vcpu->mmio_needed = 1;
  702. vcpu->mmio_is_write = 0;
  703. break;
  704. case lh_op:
  705. case lhu_op:
  706. bytes = 2;
  707. if (bytes > sizeof(run->mmio.data)) {
  708. kvm_err("%s: bad MMIO length: %d\n", __func__,
  709. run->mmio.len);
  710. er = EMULATE_FAIL;
  711. break;
  712. }
  713. run->mmio.phys_addr =
  714. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  715. host_cp0_badvaddr);
  716. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  717. er = EMULATE_FAIL;
  718. break;
  719. }
  720. run->mmio.len = bytes;
  721. run->mmio.is_write = 0;
  722. vcpu->mmio_needed = 1;
  723. vcpu->mmio_is_write = 0;
  724. if (op == lh_op)
  725. vcpu->mmio_needed = 2;
  726. else
  727. vcpu->mmio_needed = 1;
  728. break;
  729. case lbu_op:
  730. case lb_op:
  731. bytes = 1;
  732. if (bytes > sizeof(run->mmio.data)) {
  733. kvm_err("%s: bad MMIO length: %d\n", __func__,
  734. run->mmio.len);
  735. er = EMULATE_FAIL;
  736. break;
  737. }
  738. run->mmio.phys_addr =
  739. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  740. host_cp0_badvaddr);
  741. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  742. er = EMULATE_FAIL;
  743. break;
  744. }
  745. run->mmio.len = bytes;
  746. run->mmio.is_write = 0;
  747. vcpu->mmio_is_write = 0;
  748. if (op == lb_op)
  749. vcpu->mmio_needed = 2;
  750. else
  751. vcpu->mmio_needed = 1;
  752. break;
  753. default:
  754. printk("Load not yet supported");
  755. er = EMULATE_FAIL;
  756. break;
  757. }
  758. return er;
  759. }
  760. int kvm_mips_sync_icache(unsigned long va, struct kvm_vcpu *vcpu)
  761. {
  762. unsigned long offset = (va & ~PAGE_MASK);
  763. struct kvm *kvm = vcpu->kvm;
  764. unsigned long pa;
  765. gfn_t gfn;
  766. pfn_t pfn;
  767. gfn = va >> PAGE_SHIFT;
  768. if (gfn >= kvm->arch.guest_pmap_npages) {
  769. printk("%s: Invalid gfn: %#llx\n", __func__, gfn);
  770. kvm_mips_dump_host_tlbs();
  771. kvm_arch_vcpu_dump_regs(vcpu);
  772. return -1;
  773. }
  774. pfn = kvm->arch.guest_pmap[gfn];
  775. pa = (pfn << PAGE_SHIFT) | offset;
  776. printk("%s: va: %#lx, unmapped: %#x\n", __func__, va, CKSEG0ADDR(pa));
  777. mips32_SyncICache(CKSEG0ADDR(pa), 32);
  778. return 0;
  779. }
  780. #define MIPS_CACHE_OP_INDEX_INV 0x0
  781. #define MIPS_CACHE_OP_INDEX_LD_TAG 0x1
  782. #define MIPS_CACHE_OP_INDEX_ST_TAG 0x2
  783. #define MIPS_CACHE_OP_IMP 0x3
  784. #define MIPS_CACHE_OP_HIT_INV 0x4
  785. #define MIPS_CACHE_OP_FILL_WB_INV 0x5
  786. #define MIPS_CACHE_OP_HIT_HB 0x6
  787. #define MIPS_CACHE_OP_FETCH_LOCK 0x7
  788. #define MIPS_CACHE_ICACHE 0x0
  789. #define MIPS_CACHE_DCACHE 0x1
  790. #define MIPS_CACHE_SEC 0x3
  791. enum emulation_result
  792. kvm_mips_emulate_cache(uint32_t inst, uint32_t *opc, uint32_t cause,
  793. struct kvm_run *run, struct kvm_vcpu *vcpu)
  794. {
  795. struct mips_coproc *cop0 = vcpu->arch.cop0;
  796. extern void (*r4k_blast_dcache) (void);
  797. extern void (*r4k_blast_icache) (void);
  798. enum emulation_result er = EMULATE_DONE;
  799. int32_t offset, cache, op_inst, op, base;
  800. struct kvm_vcpu_arch *arch = &vcpu->arch;
  801. unsigned long va;
  802. unsigned long curr_pc;
  803. /*
  804. * Update PC and hold onto current PC in case there is
  805. * an error and we want to rollback the PC
  806. */
  807. curr_pc = vcpu->arch.pc;
  808. er = update_pc(vcpu, cause);
  809. if (er == EMULATE_FAIL)
  810. return er;
  811. base = (inst >> 21) & 0x1f;
  812. op_inst = (inst >> 16) & 0x1f;
  813. offset = inst & 0xffff;
  814. cache = (inst >> 16) & 0x3;
  815. op = (inst >> 18) & 0x7;
  816. va = arch->gprs[base] + offset;
  817. kvm_debug("CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
  818. cache, op, base, arch->gprs[base], offset);
  819. /* Treat INDEX_INV as a nop, basically issued by Linux on startup to invalidate
  820. * the caches entirely by stepping through all the ways/indexes
  821. */
  822. if (op == MIPS_CACHE_OP_INDEX_INV) {
  823. kvm_debug
  824. ("@ %#lx/%#lx CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
  825. vcpu->arch.pc, vcpu->arch.gprs[31], cache, op, base,
  826. arch->gprs[base], offset);
  827. if (cache == MIPS_CACHE_DCACHE)
  828. r4k_blast_dcache();
  829. else if (cache == MIPS_CACHE_ICACHE)
  830. r4k_blast_icache();
  831. else {
  832. printk("%s: unsupported CACHE INDEX operation\n",
  833. __func__);
  834. return EMULATE_FAIL;
  835. }
  836. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  837. kvm_mips_trans_cache_index(inst, opc, vcpu);
  838. #endif
  839. goto done;
  840. }
  841. preempt_disable();
  842. if (KVM_GUEST_KSEGX(va) == KVM_GUEST_KSEG0) {
  843. if (kvm_mips_host_tlb_lookup(vcpu, va) < 0) {
  844. kvm_mips_handle_kseg0_tlb_fault(va, vcpu);
  845. }
  846. } else if ((KVM_GUEST_KSEGX(va) < KVM_GUEST_KSEG0) ||
  847. KVM_GUEST_KSEGX(va) == KVM_GUEST_KSEG23) {
  848. int index;
  849. /* If an entry already exists then skip */
  850. if (kvm_mips_host_tlb_lookup(vcpu, va) >= 0) {
  851. goto skip_fault;
  852. }
  853. /* If address not in the guest TLB, then give the guest a fault, the
  854. * resulting handler will do the right thing
  855. */
  856. index = kvm_mips_guest_tlb_lookup(vcpu, (va & VPN2_MASK) |
  857. ASID_MASK(kvm_read_c0_guest_entryhi(cop0)));
  858. if (index < 0) {
  859. vcpu->arch.host_cp0_entryhi = (va & VPN2_MASK);
  860. vcpu->arch.host_cp0_badvaddr = va;
  861. er = kvm_mips_emulate_tlbmiss_ld(cause, NULL, run,
  862. vcpu);
  863. preempt_enable();
  864. goto dont_update_pc;
  865. } else {
  866. struct kvm_mips_tlb *tlb = &vcpu->arch.guest_tlb[index];
  867. /* Check if the entry is valid, if not then setup a TLB invalid exception to the guest */
  868. if (!TLB_IS_VALID(*tlb, va)) {
  869. er = kvm_mips_emulate_tlbinv_ld(cause, NULL,
  870. run, vcpu);
  871. preempt_enable();
  872. goto dont_update_pc;
  873. } else {
  874. /* We fault an entry from the guest tlb to the shadow host TLB */
  875. kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb,
  876. NULL,
  877. NULL);
  878. }
  879. }
  880. } else {
  881. printk
  882. ("INVALID CACHE INDEX/ADDRESS (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
  883. cache, op, base, arch->gprs[base], offset);
  884. er = EMULATE_FAIL;
  885. preempt_enable();
  886. goto dont_update_pc;
  887. }
  888. skip_fault:
  889. /* XXXKYMA: Only a subset of cache ops are supported, used by Linux */
  890. if (cache == MIPS_CACHE_DCACHE
  891. && (op == MIPS_CACHE_OP_FILL_WB_INV
  892. || op == MIPS_CACHE_OP_HIT_INV)) {
  893. flush_dcache_line(va);
  894. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  895. /* Replace the CACHE instruction, with a SYNCI, not the same, but avoids a trap */
  896. kvm_mips_trans_cache_va(inst, opc, vcpu);
  897. #endif
  898. } else if (op == MIPS_CACHE_OP_HIT_INV && cache == MIPS_CACHE_ICACHE) {
  899. flush_dcache_line(va);
  900. flush_icache_line(va);
  901. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  902. /* Replace the CACHE instruction, with a SYNCI */
  903. kvm_mips_trans_cache_va(inst, opc, vcpu);
  904. #endif
  905. } else {
  906. printk
  907. ("NO-OP CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
  908. cache, op, base, arch->gprs[base], offset);
  909. er = EMULATE_FAIL;
  910. preempt_enable();
  911. goto dont_update_pc;
  912. }
  913. preempt_enable();
  914. dont_update_pc:
  915. /*
  916. * Rollback PC
  917. */
  918. vcpu->arch.pc = curr_pc;
  919. done:
  920. return er;
  921. }
  922. enum emulation_result
  923. kvm_mips_emulate_inst(unsigned long cause, uint32_t *opc,
  924. struct kvm_run *run, struct kvm_vcpu *vcpu)
  925. {
  926. enum emulation_result er = EMULATE_DONE;
  927. uint32_t inst;
  928. /*
  929. * Fetch the instruction.
  930. */
  931. if (cause & CAUSEF_BD) {
  932. opc += 1;
  933. }
  934. inst = kvm_get_inst(opc, vcpu);
  935. switch (((union mips_instruction)inst).r_format.opcode) {
  936. case cop0_op:
  937. er = kvm_mips_emulate_CP0(inst, opc, cause, run, vcpu);
  938. break;
  939. case sb_op:
  940. case sh_op:
  941. case sw_op:
  942. er = kvm_mips_emulate_store(inst, cause, run, vcpu);
  943. break;
  944. case lb_op:
  945. case lbu_op:
  946. case lhu_op:
  947. case lh_op:
  948. case lw_op:
  949. er = kvm_mips_emulate_load(inst, cause, run, vcpu);
  950. break;
  951. case cache_op:
  952. ++vcpu->stat.cache_exits;
  953. trace_kvm_exit(vcpu, CACHE_EXITS);
  954. er = kvm_mips_emulate_cache(inst, opc, cause, run, vcpu);
  955. break;
  956. default:
  957. printk("Instruction emulation not supported (%p/%#x)\n", opc,
  958. inst);
  959. kvm_arch_vcpu_dump_regs(vcpu);
  960. er = EMULATE_FAIL;
  961. break;
  962. }
  963. return er;
  964. }
  965. enum emulation_result
  966. kvm_mips_emulate_syscall(unsigned long cause, uint32_t *opc,
  967. struct kvm_run *run, struct kvm_vcpu *vcpu)
  968. {
  969. struct mips_coproc *cop0 = vcpu->arch.cop0;
  970. struct kvm_vcpu_arch *arch = &vcpu->arch;
  971. enum emulation_result er = EMULATE_DONE;
  972. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  973. /* save old pc */
  974. kvm_write_c0_guest_epc(cop0, arch->pc);
  975. kvm_set_c0_guest_status(cop0, ST0_EXL);
  976. if (cause & CAUSEF_BD)
  977. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  978. else
  979. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  980. kvm_debug("Delivering SYSCALL @ pc %#lx\n", arch->pc);
  981. kvm_change_c0_guest_cause(cop0, (0xff),
  982. (T_SYSCALL << CAUSEB_EXCCODE));
  983. /* Set PC to the exception entry point */
  984. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  985. } else {
  986. printk("Trying to deliver SYSCALL when EXL is already set\n");
  987. er = EMULATE_FAIL;
  988. }
  989. return er;
  990. }
  991. enum emulation_result
  992. kvm_mips_emulate_tlbmiss_ld(unsigned long cause, uint32_t *opc,
  993. struct kvm_run *run, struct kvm_vcpu *vcpu)
  994. {
  995. struct mips_coproc *cop0 = vcpu->arch.cop0;
  996. struct kvm_vcpu_arch *arch = &vcpu->arch;
  997. enum emulation_result er = EMULATE_DONE;
  998. unsigned long entryhi = (vcpu->arch. host_cp0_badvaddr & VPN2_MASK) |
  999. ASID_MASK(kvm_read_c0_guest_entryhi(cop0));
  1000. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1001. /* save old pc */
  1002. kvm_write_c0_guest_epc(cop0, arch->pc);
  1003. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1004. if (cause & CAUSEF_BD)
  1005. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1006. else
  1007. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1008. kvm_debug("[EXL == 0] delivering TLB MISS @ pc %#lx\n",
  1009. arch->pc);
  1010. /* set pc to the exception entry point */
  1011. arch->pc = KVM_GUEST_KSEG0 + 0x0;
  1012. } else {
  1013. kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n",
  1014. arch->pc);
  1015. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1016. }
  1017. kvm_change_c0_guest_cause(cop0, (0xff),
  1018. (T_TLB_LD_MISS << CAUSEB_EXCCODE));
  1019. /* setup badvaddr, context and entryhi registers for the guest */
  1020. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1021. /* XXXKYMA: is the context register used by linux??? */
  1022. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1023. /* Blow away the shadow host TLBs */
  1024. kvm_mips_flush_host_tlb(1);
  1025. return er;
  1026. }
  1027. enum emulation_result
  1028. kvm_mips_emulate_tlbinv_ld(unsigned long cause, uint32_t *opc,
  1029. struct kvm_run *run, struct kvm_vcpu *vcpu)
  1030. {
  1031. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1032. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1033. enum emulation_result er = EMULATE_DONE;
  1034. unsigned long entryhi =
  1035. (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
  1036. ASID_MASK(kvm_read_c0_guest_entryhi(cop0));
  1037. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1038. /* save old pc */
  1039. kvm_write_c0_guest_epc(cop0, arch->pc);
  1040. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1041. if (cause & CAUSEF_BD)
  1042. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1043. else
  1044. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1045. kvm_debug("[EXL == 0] delivering TLB INV @ pc %#lx\n",
  1046. arch->pc);
  1047. /* set pc to the exception entry point */
  1048. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1049. } else {
  1050. kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n",
  1051. arch->pc);
  1052. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1053. }
  1054. kvm_change_c0_guest_cause(cop0, (0xff),
  1055. (T_TLB_LD_MISS << CAUSEB_EXCCODE));
  1056. /* setup badvaddr, context and entryhi registers for the guest */
  1057. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1058. /* XXXKYMA: is the context register used by linux??? */
  1059. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1060. /* Blow away the shadow host TLBs */
  1061. kvm_mips_flush_host_tlb(1);
  1062. return er;
  1063. }
  1064. enum emulation_result
  1065. kvm_mips_emulate_tlbmiss_st(unsigned long cause, uint32_t *opc,
  1066. struct kvm_run *run, struct kvm_vcpu *vcpu)
  1067. {
  1068. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1069. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1070. enum emulation_result er = EMULATE_DONE;
  1071. unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
  1072. ASID_MASK(kvm_read_c0_guest_entryhi(cop0));
  1073. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1074. /* save old pc */
  1075. kvm_write_c0_guest_epc(cop0, arch->pc);
  1076. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1077. if (cause & CAUSEF_BD)
  1078. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1079. else
  1080. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1081. kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n",
  1082. arch->pc);
  1083. /* Set PC to the exception entry point */
  1084. arch->pc = KVM_GUEST_KSEG0 + 0x0;
  1085. } else {
  1086. kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n",
  1087. arch->pc);
  1088. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1089. }
  1090. kvm_change_c0_guest_cause(cop0, (0xff),
  1091. (T_TLB_ST_MISS << CAUSEB_EXCCODE));
  1092. /* setup badvaddr, context and entryhi registers for the guest */
  1093. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1094. /* XXXKYMA: is the context register used by linux??? */
  1095. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1096. /* Blow away the shadow host TLBs */
  1097. kvm_mips_flush_host_tlb(1);
  1098. return er;
  1099. }
  1100. enum emulation_result
  1101. kvm_mips_emulate_tlbinv_st(unsigned long cause, uint32_t *opc,
  1102. struct kvm_run *run, struct kvm_vcpu *vcpu)
  1103. {
  1104. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1105. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1106. enum emulation_result er = EMULATE_DONE;
  1107. unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
  1108. ASID_MASK(kvm_read_c0_guest_entryhi(cop0));
  1109. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1110. /* save old pc */
  1111. kvm_write_c0_guest_epc(cop0, arch->pc);
  1112. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1113. if (cause & CAUSEF_BD)
  1114. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1115. else
  1116. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1117. kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n",
  1118. arch->pc);
  1119. /* Set PC to the exception entry point */
  1120. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1121. } else {
  1122. kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n",
  1123. arch->pc);
  1124. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1125. }
  1126. kvm_change_c0_guest_cause(cop0, (0xff),
  1127. (T_TLB_ST_MISS << CAUSEB_EXCCODE));
  1128. /* setup badvaddr, context and entryhi registers for the guest */
  1129. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1130. /* XXXKYMA: is the context register used by linux??? */
  1131. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1132. /* Blow away the shadow host TLBs */
  1133. kvm_mips_flush_host_tlb(1);
  1134. return er;
  1135. }
  1136. /* TLBMOD: store into address matching TLB with Dirty bit off */
  1137. enum emulation_result
  1138. kvm_mips_handle_tlbmod(unsigned long cause, uint32_t *opc,
  1139. struct kvm_run *run, struct kvm_vcpu *vcpu)
  1140. {
  1141. enum emulation_result er = EMULATE_DONE;
  1142. #ifdef DEBUG
  1143. /*
  1144. * If address not in the guest TLB, then we are in trouble
  1145. */
  1146. index = kvm_mips_guest_tlb_lookup(vcpu, entryhi);
  1147. if (index < 0) {
  1148. /* XXXKYMA Invalidate and retry */
  1149. kvm_mips_host_tlb_inv(vcpu, vcpu->arch.host_cp0_badvaddr);
  1150. kvm_err("%s: host got TLBMOD for %#lx but entry not present in Guest TLB\n",
  1151. __func__, entryhi);
  1152. kvm_mips_dump_guest_tlbs(vcpu);
  1153. kvm_mips_dump_host_tlbs();
  1154. return EMULATE_FAIL;
  1155. }
  1156. #endif
  1157. er = kvm_mips_emulate_tlbmod(cause, opc, run, vcpu);
  1158. return er;
  1159. }
  1160. enum emulation_result
  1161. kvm_mips_emulate_tlbmod(unsigned long cause, uint32_t *opc,
  1162. struct kvm_run *run, struct kvm_vcpu *vcpu)
  1163. {
  1164. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1165. unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
  1166. ASID_MASK(kvm_read_c0_guest_entryhi(cop0));
  1167. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1168. enum emulation_result er = EMULATE_DONE;
  1169. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1170. /* save old pc */
  1171. kvm_write_c0_guest_epc(cop0, arch->pc);
  1172. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1173. if (cause & CAUSEF_BD)
  1174. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1175. else
  1176. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1177. kvm_debug("[EXL == 0] Delivering TLB MOD @ pc %#lx\n",
  1178. arch->pc);
  1179. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1180. } else {
  1181. kvm_debug("[EXL == 1] Delivering TLB MOD @ pc %#lx\n",
  1182. arch->pc);
  1183. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1184. }
  1185. kvm_change_c0_guest_cause(cop0, (0xff), (T_TLB_MOD << CAUSEB_EXCCODE));
  1186. /* setup badvaddr, context and entryhi registers for the guest */
  1187. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1188. /* XXXKYMA: is the context register used by linux??? */
  1189. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1190. /* Blow away the shadow host TLBs */
  1191. kvm_mips_flush_host_tlb(1);
  1192. return er;
  1193. }
  1194. enum emulation_result
  1195. kvm_mips_emulate_fpu_exc(unsigned long cause, uint32_t *opc,
  1196. struct kvm_run *run, struct kvm_vcpu *vcpu)
  1197. {
  1198. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1199. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1200. enum emulation_result er = EMULATE_DONE;
  1201. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1202. /* save old pc */
  1203. kvm_write_c0_guest_epc(cop0, arch->pc);
  1204. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1205. if (cause & CAUSEF_BD)
  1206. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1207. else
  1208. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1209. }
  1210. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1211. kvm_change_c0_guest_cause(cop0, (0xff),
  1212. (T_COP_UNUSABLE << CAUSEB_EXCCODE));
  1213. kvm_change_c0_guest_cause(cop0, (CAUSEF_CE), (0x1 << CAUSEB_CE));
  1214. return er;
  1215. }
  1216. enum emulation_result
  1217. kvm_mips_emulate_ri_exc(unsigned long cause, uint32_t *opc,
  1218. struct kvm_run *run, struct kvm_vcpu *vcpu)
  1219. {
  1220. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1221. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1222. enum emulation_result er = EMULATE_DONE;
  1223. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1224. /* save old pc */
  1225. kvm_write_c0_guest_epc(cop0, arch->pc);
  1226. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1227. if (cause & CAUSEF_BD)
  1228. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1229. else
  1230. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1231. kvm_debug("Delivering RI @ pc %#lx\n", arch->pc);
  1232. kvm_change_c0_guest_cause(cop0, (0xff),
  1233. (T_RES_INST << CAUSEB_EXCCODE));
  1234. /* Set PC to the exception entry point */
  1235. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1236. } else {
  1237. kvm_err("Trying to deliver RI when EXL is already set\n");
  1238. er = EMULATE_FAIL;
  1239. }
  1240. return er;
  1241. }
  1242. enum emulation_result
  1243. kvm_mips_emulate_bp_exc(unsigned long cause, uint32_t *opc,
  1244. struct kvm_run *run, struct kvm_vcpu *vcpu)
  1245. {
  1246. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1247. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1248. enum emulation_result er = EMULATE_DONE;
  1249. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1250. /* save old pc */
  1251. kvm_write_c0_guest_epc(cop0, arch->pc);
  1252. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1253. if (cause & CAUSEF_BD)
  1254. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1255. else
  1256. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1257. kvm_debug("Delivering BP @ pc %#lx\n", arch->pc);
  1258. kvm_change_c0_guest_cause(cop0, (0xff),
  1259. (T_BREAK << CAUSEB_EXCCODE));
  1260. /* Set PC to the exception entry point */
  1261. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1262. } else {
  1263. printk("Trying to deliver BP when EXL is already set\n");
  1264. er = EMULATE_FAIL;
  1265. }
  1266. return er;
  1267. }
  1268. /*
  1269. * ll/sc, rdhwr, sync emulation
  1270. */
  1271. #define OPCODE 0xfc000000
  1272. #define BASE 0x03e00000
  1273. #define RT 0x001f0000
  1274. #define OFFSET 0x0000ffff
  1275. #define LL 0xc0000000
  1276. #define SC 0xe0000000
  1277. #define SPEC0 0x00000000
  1278. #define SPEC3 0x7c000000
  1279. #define RD 0x0000f800
  1280. #define FUNC 0x0000003f
  1281. #define SYNC 0x0000000f
  1282. #define RDHWR 0x0000003b
  1283. enum emulation_result
  1284. kvm_mips_handle_ri(unsigned long cause, uint32_t *opc,
  1285. struct kvm_run *run, struct kvm_vcpu *vcpu)
  1286. {
  1287. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1288. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1289. enum emulation_result er = EMULATE_DONE;
  1290. unsigned long curr_pc;
  1291. uint32_t inst;
  1292. /*
  1293. * Update PC and hold onto current PC in case there is
  1294. * an error and we want to rollback the PC
  1295. */
  1296. curr_pc = vcpu->arch.pc;
  1297. er = update_pc(vcpu, cause);
  1298. if (er == EMULATE_FAIL)
  1299. return er;
  1300. /*
  1301. * Fetch the instruction.
  1302. */
  1303. if (cause & CAUSEF_BD)
  1304. opc += 1;
  1305. inst = kvm_get_inst(opc, vcpu);
  1306. if (inst == KVM_INVALID_INST) {
  1307. printk("%s: Cannot get inst @ %p\n", __func__, opc);
  1308. return EMULATE_FAIL;
  1309. }
  1310. if ((inst & OPCODE) == SPEC3 && (inst & FUNC) == RDHWR) {
  1311. int rd = (inst & RD) >> 11;
  1312. int rt = (inst & RT) >> 16;
  1313. switch (rd) {
  1314. case 0: /* CPU number */
  1315. arch->gprs[rt] = 0;
  1316. break;
  1317. case 1: /* SYNCI length */
  1318. arch->gprs[rt] = min(current_cpu_data.dcache.linesz,
  1319. current_cpu_data.icache.linesz);
  1320. break;
  1321. case 2: /* Read count register */
  1322. printk("RDHWR: Cont register\n");
  1323. arch->gprs[rt] = kvm_read_c0_guest_count(cop0);
  1324. break;
  1325. case 3: /* Count register resolution */
  1326. switch (current_cpu_data.cputype) {
  1327. case CPU_20KC:
  1328. case CPU_25KF:
  1329. arch->gprs[rt] = 1;
  1330. break;
  1331. default:
  1332. arch->gprs[rt] = 2;
  1333. }
  1334. break;
  1335. case 29:
  1336. #if 1
  1337. arch->gprs[rt] = kvm_read_c0_guest_userlocal(cop0);
  1338. #else
  1339. /* UserLocal not implemented */
  1340. er = kvm_mips_emulate_ri_exc(cause, opc, run, vcpu);
  1341. #endif
  1342. break;
  1343. default:
  1344. printk("RDHWR not supported\n");
  1345. er = EMULATE_FAIL;
  1346. break;
  1347. }
  1348. } else {
  1349. printk("Emulate RI not supported @ %p: %#x\n", opc, inst);
  1350. er = EMULATE_FAIL;
  1351. }
  1352. /*
  1353. * Rollback PC only if emulation was unsuccessful
  1354. */
  1355. if (er == EMULATE_FAIL) {
  1356. vcpu->arch.pc = curr_pc;
  1357. }
  1358. return er;
  1359. }
  1360. enum emulation_result
  1361. kvm_mips_complete_mmio_load(struct kvm_vcpu *vcpu, struct kvm_run *run)
  1362. {
  1363. unsigned long *gpr = &vcpu->arch.gprs[vcpu->arch.io_gpr];
  1364. enum emulation_result er = EMULATE_DONE;
  1365. unsigned long curr_pc;
  1366. if (run->mmio.len > sizeof(*gpr)) {
  1367. printk("Bad MMIO length: %d", run->mmio.len);
  1368. er = EMULATE_FAIL;
  1369. goto done;
  1370. }
  1371. /*
  1372. * Update PC and hold onto current PC in case there is
  1373. * an error and we want to rollback the PC
  1374. */
  1375. curr_pc = vcpu->arch.pc;
  1376. er = update_pc(vcpu, vcpu->arch.pending_load_cause);
  1377. if (er == EMULATE_FAIL)
  1378. return er;
  1379. switch (run->mmio.len) {
  1380. case 4:
  1381. *gpr = *(int32_t *) run->mmio.data;
  1382. break;
  1383. case 2:
  1384. if (vcpu->mmio_needed == 2)
  1385. *gpr = *(int16_t *) run->mmio.data;
  1386. else
  1387. *gpr = *(int16_t *) run->mmio.data;
  1388. break;
  1389. case 1:
  1390. if (vcpu->mmio_needed == 2)
  1391. *gpr = *(int8_t *) run->mmio.data;
  1392. else
  1393. *gpr = *(u8 *) run->mmio.data;
  1394. break;
  1395. }
  1396. if (vcpu->arch.pending_load_cause & CAUSEF_BD)
  1397. kvm_debug
  1398. ("[%#lx] Completing %d byte BD Load to gpr %d (0x%08lx) type %d\n",
  1399. vcpu->arch.pc, run->mmio.len, vcpu->arch.io_gpr, *gpr,
  1400. vcpu->mmio_needed);
  1401. done:
  1402. return er;
  1403. }
  1404. static enum emulation_result
  1405. kvm_mips_emulate_exc(unsigned long cause, uint32_t *opc,
  1406. struct kvm_run *run, struct kvm_vcpu *vcpu)
  1407. {
  1408. uint32_t exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
  1409. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1410. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1411. enum emulation_result er = EMULATE_DONE;
  1412. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1413. /* save old pc */
  1414. kvm_write_c0_guest_epc(cop0, arch->pc);
  1415. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1416. if (cause & CAUSEF_BD)
  1417. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1418. else
  1419. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1420. kvm_change_c0_guest_cause(cop0, (0xff),
  1421. (exccode << CAUSEB_EXCCODE));
  1422. /* Set PC to the exception entry point */
  1423. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1424. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1425. kvm_debug("Delivering EXC %d @ pc %#lx, badVaddr: %#lx\n",
  1426. exccode, kvm_read_c0_guest_epc(cop0),
  1427. kvm_read_c0_guest_badvaddr(cop0));
  1428. } else {
  1429. printk("Trying to deliver EXC when EXL is already set\n");
  1430. er = EMULATE_FAIL;
  1431. }
  1432. return er;
  1433. }
  1434. enum emulation_result
  1435. kvm_mips_check_privilege(unsigned long cause, uint32_t *opc,
  1436. struct kvm_run *run, struct kvm_vcpu *vcpu)
  1437. {
  1438. enum emulation_result er = EMULATE_DONE;
  1439. uint32_t exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
  1440. unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
  1441. int usermode = !KVM_GUEST_KERNEL_MODE(vcpu);
  1442. if (usermode) {
  1443. switch (exccode) {
  1444. case T_INT:
  1445. case T_SYSCALL:
  1446. case T_BREAK:
  1447. case T_RES_INST:
  1448. break;
  1449. case T_COP_UNUSABLE:
  1450. if (((cause & CAUSEF_CE) >> CAUSEB_CE) == 0)
  1451. er = EMULATE_PRIV_FAIL;
  1452. break;
  1453. case T_TLB_MOD:
  1454. break;
  1455. case T_TLB_LD_MISS:
  1456. /* We we are accessing Guest kernel space, then send an address error exception to the guest */
  1457. if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
  1458. printk("%s: LD MISS @ %#lx\n", __func__,
  1459. badvaddr);
  1460. cause &= ~0xff;
  1461. cause |= (T_ADDR_ERR_LD << CAUSEB_EXCCODE);
  1462. er = EMULATE_PRIV_FAIL;
  1463. }
  1464. break;
  1465. case T_TLB_ST_MISS:
  1466. /* We we are accessing Guest kernel space, then send an address error exception to the guest */
  1467. if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
  1468. printk("%s: ST MISS @ %#lx\n", __func__,
  1469. badvaddr);
  1470. cause &= ~0xff;
  1471. cause |= (T_ADDR_ERR_ST << CAUSEB_EXCCODE);
  1472. er = EMULATE_PRIV_FAIL;
  1473. }
  1474. break;
  1475. case T_ADDR_ERR_ST:
  1476. printk("%s: address error ST @ %#lx\n", __func__,
  1477. badvaddr);
  1478. if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
  1479. cause &= ~0xff;
  1480. cause |= (T_TLB_ST_MISS << CAUSEB_EXCCODE);
  1481. }
  1482. er = EMULATE_PRIV_FAIL;
  1483. break;
  1484. case T_ADDR_ERR_LD:
  1485. printk("%s: address error LD @ %#lx\n", __func__,
  1486. badvaddr);
  1487. if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
  1488. cause &= ~0xff;
  1489. cause |= (T_TLB_LD_MISS << CAUSEB_EXCCODE);
  1490. }
  1491. er = EMULATE_PRIV_FAIL;
  1492. break;
  1493. default:
  1494. er = EMULATE_PRIV_FAIL;
  1495. break;
  1496. }
  1497. }
  1498. if (er == EMULATE_PRIV_FAIL) {
  1499. kvm_mips_emulate_exc(cause, opc, run, vcpu);
  1500. }
  1501. return er;
  1502. }
  1503. /* User Address (UA) fault, this could happen if
  1504. * (1) TLB entry not present/valid in both Guest and shadow host TLBs, in this
  1505. * case we pass on the fault to the guest kernel and let it handle it.
  1506. * (2) TLB entry is present in the Guest TLB but not in the shadow, in this
  1507. * case we inject the TLB from the Guest TLB into the shadow host TLB
  1508. */
  1509. enum emulation_result
  1510. kvm_mips_handle_tlbmiss(unsigned long cause, uint32_t *opc,
  1511. struct kvm_run *run, struct kvm_vcpu *vcpu)
  1512. {
  1513. enum emulation_result er = EMULATE_DONE;
  1514. uint32_t exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
  1515. unsigned long va = vcpu->arch.host_cp0_badvaddr;
  1516. int index;
  1517. kvm_debug("kvm_mips_handle_tlbmiss: badvaddr: %#lx, entryhi: %#lx\n",
  1518. vcpu->arch.host_cp0_badvaddr, vcpu->arch.host_cp0_entryhi);
  1519. /* KVM would not have got the exception if this entry was valid in the shadow host TLB
  1520. * Check the Guest TLB, if the entry is not there then send the guest an
  1521. * exception. The guest exc handler should then inject an entry into the
  1522. * guest TLB
  1523. */
  1524. index = kvm_mips_guest_tlb_lookup(vcpu,
  1525. (va & VPN2_MASK) |
  1526. ASID_MASK(kvm_read_c0_guest_entryhi
  1527. (vcpu->arch.cop0)));
  1528. if (index < 0) {
  1529. if (exccode == T_TLB_LD_MISS) {
  1530. er = kvm_mips_emulate_tlbmiss_ld(cause, opc, run, vcpu);
  1531. } else if (exccode == T_TLB_ST_MISS) {
  1532. er = kvm_mips_emulate_tlbmiss_st(cause, opc, run, vcpu);
  1533. } else {
  1534. printk("%s: invalid exc code: %d\n", __func__, exccode);
  1535. er = EMULATE_FAIL;
  1536. }
  1537. } else {
  1538. struct kvm_mips_tlb *tlb = &vcpu->arch.guest_tlb[index];
  1539. /* Check if the entry is valid, if not then setup a TLB invalid exception to the guest */
  1540. if (!TLB_IS_VALID(*tlb, va)) {
  1541. if (exccode == T_TLB_LD_MISS) {
  1542. er = kvm_mips_emulate_tlbinv_ld(cause, opc, run,
  1543. vcpu);
  1544. } else if (exccode == T_TLB_ST_MISS) {
  1545. er = kvm_mips_emulate_tlbinv_st(cause, opc, run,
  1546. vcpu);
  1547. } else {
  1548. printk("%s: invalid exc code: %d\n", __func__,
  1549. exccode);
  1550. er = EMULATE_FAIL;
  1551. }
  1552. } else {
  1553. #ifdef DEBUG
  1554. kvm_debug
  1555. ("Injecting hi: %#lx, lo0: %#lx, lo1: %#lx into shadow host TLB\n",
  1556. tlb->tlb_hi, tlb->tlb_lo0, tlb->tlb_lo1);
  1557. #endif
  1558. /* OK we have a Guest TLB entry, now inject it into the shadow host TLB */
  1559. kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb, NULL,
  1560. NULL);
  1561. }
  1562. }
  1563. return er;
  1564. }