kvm_mips_emul.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  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 =
  457. vcpu->arch.gprs[rt] & ASID_MASK;
  458. if ((KSEGX(vcpu->arch.gprs[rt]) != CKSEG0)
  459. &&
  460. ((kvm_read_c0_guest_entryhi(cop0) &
  461. ASID_MASK) != nasid)) {
  462. kvm_debug
  463. ("MTCz, change ASID from %#lx to %#lx\n",
  464. kvm_read_c0_guest_entryhi(cop0) &
  465. ASID_MASK,
  466. vcpu->arch.gprs[rt] & ASID_MASK);
  467. /* Blow away the shadow host TLBs */
  468. kvm_mips_flush_host_tlb(1);
  469. }
  470. kvm_write_c0_guest_entryhi(cop0,
  471. vcpu->arch.gprs[rt]);
  472. }
  473. /* Are we writing to COUNT */
  474. else if ((rd == MIPS_CP0_COUNT) && (sel == 0)) {
  475. /* Linux doesn't seem to write into COUNT, we throw an error
  476. * if we notice a write to COUNT
  477. */
  478. /*er = EMULATE_FAIL; */
  479. goto done;
  480. } else if ((rd == MIPS_CP0_COMPARE) && (sel == 0)) {
  481. kvm_debug("[%#x] MTCz, COMPARE %#lx <- %#lx\n",
  482. pc, kvm_read_c0_guest_compare(cop0),
  483. vcpu->arch.gprs[rt]);
  484. /* If we are writing to COMPARE */
  485. /* Clear pending timer interrupt, if any */
  486. kvm_mips_callbacks->dequeue_timer_int(vcpu);
  487. kvm_write_c0_guest_compare(cop0,
  488. vcpu->arch.gprs[rt]);
  489. } else if ((rd == MIPS_CP0_STATUS) && (sel == 0)) {
  490. kvm_write_c0_guest_status(cop0,
  491. vcpu->arch.gprs[rt]);
  492. /* Make sure that CU1 and NMI bits are never set */
  493. kvm_clear_c0_guest_status(cop0,
  494. (ST0_CU1 | ST0_NMI));
  495. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  496. kvm_mips_trans_mtc0(inst, opc, vcpu);
  497. #endif
  498. } else {
  499. cop0->reg[rd][sel] = vcpu->arch.gprs[rt];
  500. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  501. kvm_mips_trans_mtc0(inst, opc, vcpu);
  502. #endif
  503. }
  504. kvm_debug("[%#x] MTCz, cop0->reg[%d][%d]: %#lx\n", pc,
  505. rd, sel, cop0->reg[rd][sel]);
  506. break;
  507. case dmtc_op:
  508. printk
  509. ("!!!!!!![%#lx]dmtc_op: rt: %d, rd: %d, sel: %d!!!!!!\n",
  510. vcpu->arch.pc, rt, rd, sel);
  511. er = EMULATE_FAIL;
  512. break;
  513. case mfmcz_op:
  514. #ifdef KVM_MIPS_DEBUG_COP0_COUNTERS
  515. cop0->stat[MIPS_CP0_STATUS][0]++;
  516. #endif
  517. if (rt != 0) {
  518. vcpu->arch.gprs[rt] =
  519. kvm_read_c0_guest_status(cop0);
  520. }
  521. /* EI */
  522. if (inst & 0x20) {
  523. kvm_debug("[%#lx] mfmcz_op: EI\n",
  524. vcpu->arch.pc);
  525. kvm_set_c0_guest_status(cop0, ST0_IE);
  526. } else {
  527. kvm_debug("[%#lx] mfmcz_op: DI\n",
  528. vcpu->arch.pc);
  529. kvm_clear_c0_guest_status(cop0, ST0_IE);
  530. }
  531. break;
  532. case wrpgpr_op:
  533. {
  534. uint32_t css =
  535. cop0->reg[MIPS_CP0_STATUS][2] & 0xf;
  536. uint32_t pss =
  537. (cop0->reg[MIPS_CP0_STATUS][2] >> 6) & 0xf;
  538. /* We don't support any shadow register sets, so SRSCtl[PSS] == SRSCtl[CSS] = 0 */
  539. if (css || pss) {
  540. er = EMULATE_FAIL;
  541. break;
  542. }
  543. kvm_debug("WRPGPR[%d][%d] = %#lx\n", pss, rd,
  544. vcpu->arch.gprs[rt]);
  545. vcpu->arch.gprs[rd] = vcpu->arch.gprs[rt];
  546. }
  547. break;
  548. default:
  549. printk
  550. ("[%#lx]MachEmulateCP0: unsupported COP0, copz: 0x%x\n",
  551. vcpu->arch.pc, copz);
  552. er = EMULATE_FAIL;
  553. break;
  554. }
  555. }
  556. done:
  557. /*
  558. * Rollback PC only if emulation was unsuccessful
  559. */
  560. if (er == EMULATE_FAIL) {
  561. vcpu->arch.pc = curr_pc;
  562. }
  563. dont_update_pc:
  564. /*
  565. * This is for special instructions whose emulation
  566. * updates the PC, so do not overwrite the PC under
  567. * any circumstances
  568. */
  569. return er;
  570. }
  571. enum emulation_result
  572. kvm_mips_emulate_store(uint32_t inst, uint32_t cause,
  573. struct kvm_run *run, struct kvm_vcpu *vcpu)
  574. {
  575. enum emulation_result er = EMULATE_DO_MMIO;
  576. int32_t op, base, rt, offset;
  577. uint32_t bytes;
  578. void *data = run->mmio.data;
  579. unsigned long curr_pc;
  580. /*
  581. * Update PC and hold onto current PC in case there is
  582. * an error and we want to rollback the PC
  583. */
  584. curr_pc = vcpu->arch.pc;
  585. er = update_pc(vcpu, cause);
  586. if (er == EMULATE_FAIL)
  587. return er;
  588. rt = (inst >> 16) & 0x1f;
  589. base = (inst >> 21) & 0x1f;
  590. offset = inst & 0xffff;
  591. op = (inst >> 26) & 0x3f;
  592. switch (op) {
  593. case sb_op:
  594. bytes = 1;
  595. if (bytes > sizeof(run->mmio.data)) {
  596. kvm_err("%s: bad MMIO length: %d\n", __func__,
  597. run->mmio.len);
  598. }
  599. run->mmio.phys_addr =
  600. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  601. host_cp0_badvaddr);
  602. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  603. er = EMULATE_FAIL;
  604. break;
  605. }
  606. run->mmio.len = bytes;
  607. run->mmio.is_write = 1;
  608. vcpu->mmio_needed = 1;
  609. vcpu->mmio_is_write = 1;
  610. *(u8 *) data = vcpu->arch.gprs[rt];
  611. kvm_debug("OP_SB: eaddr: %#lx, gpr: %#lx, data: %#x\n",
  612. vcpu->arch.host_cp0_badvaddr, vcpu->arch.gprs[rt],
  613. *(uint8_t *) data);
  614. break;
  615. case sw_op:
  616. bytes = 4;
  617. if (bytes > sizeof(run->mmio.data)) {
  618. kvm_err("%s: bad MMIO length: %d\n", __func__,
  619. run->mmio.len);
  620. }
  621. run->mmio.phys_addr =
  622. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  623. host_cp0_badvaddr);
  624. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  625. er = EMULATE_FAIL;
  626. break;
  627. }
  628. run->mmio.len = bytes;
  629. run->mmio.is_write = 1;
  630. vcpu->mmio_needed = 1;
  631. vcpu->mmio_is_write = 1;
  632. *(uint32_t *) data = vcpu->arch.gprs[rt];
  633. kvm_debug("[%#lx] OP_SW: eaddr: %#lx, gpr: %#lx, data: %#x\n",
  634. vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
  635. vcpu->arch.gprs[rt], *(uint32_t *) data);
  636. break;
  637. case sh_op:
  638. bytes = 2;
  639. if (bytes > sizeof(run->mmio.data)) {
  640. kvm_err("%s: bad MMIO length: %d\n", __func__,
  641. run->mmio.len);
  642. }
  643. run->mmio.phys_addr =
  644. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  645. host_cp0_badvaddr);
  646. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  647. er = EMULATE_FAIL;
  648. break;
  649. }
  650. run->mmio.len = bytes;
  651. run->mmio.is_write = 1;
  652. vcpu->mmio_needed = 1;
  653. vcpu->mmio_is_write = 1;
  654. *(uint16_t *) data = vcpu->arch.gprs[rt];
  655. kvm_debug("[%#lx] OP_SH: eaddr: %#lx, gpr: %#lx, data: %#x\n",
  656. vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
  657. vcpu->arch.gprs[rt], *(uint32_t *) data);
  658. break;
  659. default:
  660. printk("Store not yet supported");
  661. er = EMULATE_FAIL;
  662. break;
  663. }
  664. /*
  665. * Rollback PC if emulation was unsuccessful
  666. */
  667. if (er == EMULATE_FAIL) {
  668. vcpu->arch.pc = curr_pc;
  669. }
  670. return er;
  671. }
  672. enum emulation_result
  673. kvm_mips_emulate_load(uint32_t inst, uint32_t cause,
  674. struct kvm_run *run, struct kvm_vcpu *vcpu)
  675. {
  676. enum emulation_result er = EMULATE_DO_MMIO;
  677. int32_t op, base, rt, offset;
  678. uint32_t bytes;
  679. rt = (inst >> 16) & 0x1f;
  680. base = (inst >> 21) & 0x1f;
  681. offset = inst & 0xffff;
  682. op = (inst >> 26) & 0x3f;
  683. vcpu->arch.pending_load_cause = cause;
  684. vcpu->arch.io_gpr = rt;
  685. switch (op) {
  686. case lw_op:
  687. bytes = 4;
  688. if (bytes > sizeof(run->mmio.data)) {
  689. kvm_err("%s: bad MMIO length: %d\n", __func__,
  690. run->mmio.len);
  691. er = EMULATE_FAIL;
  692. break;
  693. }
  694. run->mmio.phys_addr =
  695. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  696. host_cp0_badvaddr);
  697. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  698. er = EMULATE_FAIL;
  699. break;
  700. }
  701. run->mmio.len = bytes;
  702. run->mmio.is_write = 0;
  703. vcpu->mmio_needed = 1;
  704. vcpu->mmio_is_write = 0;
  705. break;
  706. case lh_op:
  707. case lhu_op:
  708. bytes = 2;
  709. if (bytes > sizeof(run->mmio.data)) {
  710. kvm_err("%s: bad MMIO length: %d\n", __func__,
  711. run->mmio.len);
  712. er = EMULATE_FAIL;
  713. break;
  714. }
  715. run->mmio.phys_addr =
  716. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  717. host_cp0_badvaddr);
  718. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  719. er = EMULATE_FAIL;
  720. break;
  721. }
  722. run->mmio.len = bytes;
  723. run->mmio.is_write = 0;
  724. vcpu->mmio_needed = 1;
  725. vcpu->mmio_is_write = 0;
  726. if (op == lh_op)
  727. vcpu->mmio_needed = 2;
  728. else
  729. vcpu->mmio_needed = 1;
  730. break;
  731. case lbu_op:
  732. case lb_op:
  733. bytes = 1;
  734. if (bytes > sizeof(run->mmio.data)) {
  735. kvm_err("%s: bad MMIO length: %d\n", __func__,
  736. run->mmio.len);
  737. er = EMULATE_FAIL;
  738. break;
  739. }
  740. run->mmio.phys_addr =
  741. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  742. host_cp0_badvaddr);
  743. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  744. er = EMULATE_FAIL;
  745. break;
  746. }
  747. run->mmio.len = bytes;
  748. run->mmio.is_write = 0;
  749. vcpu->mmio_is_write = 0;
  750. if (op == lb_op)
  751. vcpu->mmio_needed = 2;
  752. else
  753. vcpu->mmio_needed = 1;
  754. break;
  755. default:
  756. printk("Load not yet supported");
  757. er = EMULATE_FAIL;
  758. break;
  759. }
  760. return er;
  761. }
  762. int kvm_mips_sync_icache(unsigned long va, struct kvm_vcpu *vcpu)
  763. {
  764. unsigned long offset = (va & ~PAGE_MASK);
  765. struct kvm *kvm = vcpu->kvm;
  766. unsigned long pa;
  767. gfn_t gfn;
  768. pfn_t pfn;
  769. gfn = va >> PAGE_SHIFT;
  770. if (gfn >= kvm->arch.guest_pmap_npages) {
  771. printk("%s: Invalid gfn: %#llx\n", __func__, gfn);
  772. kvm_mips_dump_host_tlbs();
  773. kvm_arch_vcpu_dump_regs(vcpu);
  774. return -1;
  775. }
  776. pfn = kvm->arch.guest_pmap[gfn];
  777. pa = (pfn << PAGE_SHIFT) | offset;
  778. printk("%s: va: %#lx, unmapped: %#x\n", __func__, va, CKSEG0ADDR(pa));
  779. mips32_SyncICache(CKSEG0ADDR(pa), 32);
  780. return 0;
  781. }
  782. #define MIPS_CACHE_OP_INDEX_INV 0x0
  783. #define MIPS_CACHE_OP_INDEX_LD_TAG 0x1
  784. #define MIPS_CACHE_OP_INDEX_ST_TAG 0x2
  785. #define MIPS_CACHE_OP_IMP 0x3
  786. #define MIPS_CACHE_OP_HIT_INV 0x4
  787. #define MIPS_CACHE_OP_FILL_WB_INV 0x5
  788. #define MIPS_CACHE_OP_HIT_HB 0x6
  789. #define MIPS_CACHE_OP_FETCH_LOCK 0x7
  790. #define MIPS_CACHE_ICACHE 0x0
  791. #define MIPS_CACHE_DCACHE 0x1
  792. #define MIPS_CACHE_SEC 0x3
  793. enum emulation_result
  794. kvm_mips_emulate_cache(uint32_t inst, uint32_t *opc, uint32_t cause,
  795. struct kvm_run *run, struct kvm_vcpu *vcpu)
  796. {
  797. struct mips_coproc *cop0 = vcpu->arch.cop0;
  798. extern void (*r4k_blast_dcache) (void);
  799. extern void (*r4k_blast_icache) (void);
  800. enum emulation_result er = EMULATE_DONE;
  801. int32_t offset, cache, op_inst, op, base;
  802. struct kvm_vcpu_arch *arch = &vcpu->arch;
  803. unsigned long va;
  804. unsigned long curr_pc;
  805. /*
  806. * Update PC and hold onto current PC in case there is
  807. * an error and we want to rollback the PC
  808. */
  809. curr_pc = vcpu->arch.pc;
  810. er = update_pc(vcpu, cause);
  811. if (er == EMULATE_FAIL)
  812. return er;
  813. base = (inst >> 21) & 0x1f;
  814. op_inst = (inst >> 16) & 0x1f;
  815. offset = inst & 0xffff;
  816. cache = (inst >> 16) & 0x3;
  817. op = (inst >> 18) & 0x7;
  818. va = arch->gprs[base] + offset;
  819. kvm_debug("CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
  820. cache, op, base, arch->gprs[base], offset);
  821. /* Treat INDEX_INV as a nop, basically issued by Linux on startup to invalidate
  822. * the caches entirely by stepping through all the ways/indexes
  823. */
  824. if (op == MIPS_CACHE_OP_INDEX_INV) {
  825. kvm_debug
  826. ("@ %#lx/%#lx CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
  827. vcpu->arch.pc, vcpu->arch.gprs[31], cache, op, base,
  828. arch->gprs[base], offset);
  829. if (cache == MIPS_CACHE_DCACHE)
  830. r4k_blast_dcache();
  831. else if (cache == MIPS_CACHE_ICACHE)
  832. r4k_blast_icache();
  833. else {
  834. printk("%s: unsupported CACHE INDEX operation\n",
  835. __func__);
  836. return EMULATE_FAIL;
  837. }
  838. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  839. kvm_mips_trans_cache_index(inst, opc, vcpu);
  840. #endif
  841. goto done;
  842. }
  843. preempt_disable();
  844. if (KVM_GUEST_KSEGX(va) == KVM_GUEST_KSEG0) {
  845. if (kvm_mips_host_tlb_lookup(vcpu, va) < 0) {
  846. kvm_mips_handle_kseg0_tlb_fault(va, vcpu);
  847. }
  848. } else if ((KVM_GUEST_KSEGX(va) < KVM_GUEST_KSEG0) ||
  849. KVM_GUEST_KSEGX(va) == KVM_GUEST_KSEG23) {
  850. int index;
  851. /* If an entry already exists then skip */
  852. if (kvm_mips_host_tlb_lookup(vcpu, va) >= 0) {
  853. goto skip_fault;
  854. }
  855. /* If address not in the guest TLB, then give the guest a fault, the
  856. * resulting handler will do the right thing
  857. */
  858. index = kvm_mips_guest_tlb_lookup(vcpu, (va & VPN2_MASK) |
  859. (kvm_read_c0_guest_entryhi
  860. (cop0) & ASID_MASK));
  861. if (index < 0) {
  862. vcpu->arch.host_cp0_entryhi = (va & VPN2_MASK);
  863. vcpu->arch.host_cp0_badvaddr = va;
  864. er = kvm_mips_emulate_tlbmiss_ld(cause, NULL, run,
  865. vcpu);
  866. preempt_enable();
  867. goto dont_update_pc;
  868. } else {
  869. struct kvm_mips_tlb *tlb = &vcpu->arch.guest_tlb[index];
  870. /* Check if the entry is valid, if not then setup a TLB invalid exception to the guest */
  871. if (!TLB_IS_VALID(*tlb, va)) {
  872. er = kvm_mips_emulate_tlbinv_ld(cause, NULL,
  873. run, vcpu);
  874. preempt_enable();
  875. goto dont_update_pc;
  876. } else {
  877. /* We fault an entry from the guest tlb to the shadow host TLB */
  878. kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb,
  879. NULL,
  880. NULL);
  881. }
  882. }
  883. } else {
  884. printk
  885. ("INVALID CACHE INDEX/ADDRESS (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
  886. cache, op, base, arch->gprs[base], offset);
  887. er = EMULATE_FAIL;
  888. preempt_enable();
  889. goto dont_update_pc;
  890. }
  891. skip_fault:
  892. /* XXXKYMA: Only a subset of cache ops are supported, used by Linux */
  893. if (cache == MIPS_CACHE_DCACHE
  894. && (op == MIPS_CACHE_OP_FILL_WB_INV
  895. || op == MIPS_CACHE_OP_HIT_INV)) {
  896. flush_dcache_line(va);
  897. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  898. /* Replace the CACHE instruction, with a SYNCI, not the same, but avoids a trap */
  899. kvm_mips_trans_cache_va(inst, opc, vcpu);
  900. #endif
  901. } else if (op == MIPS_CACHE_OP_HIT_INV && cache == MIPS_CACHE_ICACHE) {
  902. flush_dcache_line(va);
  903. flush_icache_line(va);
  904. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  905. /* Replace the CACHE instruction, with a SYNCI */
  906. kvm_mips_trans_cache_va(inst, opc, vcpu);
  907. #endif
  908. } else {
  909. printk
  910. ("NO-OP CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
  911. cache, op, base, arch->gprs[base], offset);
  912. er = EMULATE_FAIL;
  913. preempt_enable();
  914. goto dont_update_pc;
  915. }
  916. preempt_enable();
  917. dont_update_pc:
  918. /*
  919. * Rollback PC
  920. */
  921. vcpu->arch.pc = curr_pc;
  922. done:
  923. return er;
  924. }
  925. enum emulation_result
  926. kvm_mips_emulate_inst(unsigned long cause, uint32_t *opc,
  927. struct kvm_run *run, struct kvm_vcpu *vcpu)
  928. {
  929. enum emulation_result er = EMULATE_DONE;
  930. uint32_t inst;
  931. /*
  932. * Fetch the instruction.
  933. */
  934. if (cause & CAUSEF_BD) {
  935. opc += 1;
  936. }
  937. inst = kvm_get_inst(opc, vcpu);
  938. switch (((union mips_instruction)inst).r_format.opcode) {
  939. case cop0_op:
  940. er = kvm_mips_emulate_CP0(inst, opc, cause, run, vcpu);
  941. break;
  942. case sb_op:
  943. case sh_op:
  944. case sw_op:
  945. er = kvm_mips_emulate_store(inst, cause, run, vcpu);
  946. break;
  947. case lb_op:
  948. case lbu_op:
  949. case lhu_op:
  950. case lh_op:
  951. case lw_op:
  952. er = kvm_mips_emulate_load(inst, cause, run, vcpu);
  953. break;
  954. case cache_op:
  955. ++vcpu->stat.cache_exits;
  956. trace_kvm_exit(vcpu, CACHE_EXITS);
  957. er = kvm_mips_emulate_cache(inst, opc, cause, run, vcpu);
  958. break;
  959. default:
  960. printk("Instruction emulation not supported (%p/%#x)\n", opc,
  961. inst);
  962. kvm_arch_vcpu_dump_regs(vcpu);
  963. er = EMULATE_FAIL;
  964. break;
  965. }
  966. return er;
  967. }
  968. enum emulation_result
  969. kvm_mips_emulate_syscall(unsigned long cause, uint32_t *opc,
  970. struct kvm_run *run, struct kvm_vcpu *vcpu)
  971. {
  972. struct mips_coproc *cop0 = vcpu->arch.cop0;
  973. struct kvm_vcpu_arch *arch = &vcpu->arch;
  974. enum emulation_result er = EMULATE_DONE;
  975. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  976. /* save old pc */
  977. kvm_write_c0_guest_epc(cop0, arch->pc);
  978. kvm_set_c0_guest_status(cop0, ST0_EXL);
  979. if (cause & CAUSEF_BD)
  980. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  981. else
  982. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  983. kvm_debug("Delivering SYSCALL @ pc %#lx\n", arch->pc);
  984. kvm_change_c0_guest_cause(cop0, (0xff),
  985. (T_SYSCALL << CAUSEB_EXCCODE));
  986. /* Set PC to the exception entry point */
  987. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  988. } else {
  989. printk("Trying to deliver SYSCALL when EXL is already set\n");
  990. er = EMULATE_FAIL;
  991. }
  992. return er;
  993. }
  994. enum emulation_result
  995. kvm_mips_emulate_tlbmiss_ld(unsigned long cause, uint32_t *opc,
  996. struct kvm_run *run, struct kvm_vcpu *vcpu)
  997. {
  998. struct mips_coproc *cop0 = vcpu->arch.cop0;
  999. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1000. enum emulation_result er = EMULATE_DONE;
  1001. unsigned long entryhi = (vcpu->arch. host_cp0_badvaddr & VPN2_MASK) |
  1002. (kvm_read_c0_guest_entryhi(cop0) & ASID_MASK);
  1003. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1004. /* save old pc */
  1005. kvm_write_c0_guest_epc(cop0, arch->pc);
  1006. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1007. if (cause & CAUSEF_BD)
  1008. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1009. else
  1010. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1011. kvm_debug("[EXL == 0] delivering TLB MISS @ pc %#lx\n",
  1012. arch->pc);
  1013. /* set pc to the exception entry point */
  1014. arch->pc = KVM_GUEST_KSEG0 + 0x0;
  1015. } else {
  1016. kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n",
  1017. arch->pc);
  1018. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1019. }
  1020. kvm_change_c0_guest_cause(cop0, (0xff),
  1021. (T_TLB_LD_MISS << CAUSEB_EXCCODE));
  1022. /* setup badvaddr, context and entryhi registers for the guest */
  1023. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1024. /* XXXKYMA: is the context register used by linux??? */
  1025. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1026. /* Blow away the shadow host TLBs */
  1027. kvm_mips_flush_host_tlb(1);
  1028. return er;
  1029. }
  1030. enum emulation_result
  1031. kvm_mips_emulate_tlbinv_ld(unsigned long cause, uint32_t *opc,
  1032. struct kvm_run *run, struct kvm_vcpu *vcpu)
  1033. {
  1034. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1035. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1036. enum emulation_result er = EMULATE_DONE;
  1037. unsigned long entryhi =
  1038. (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
  1039. (kvm_read_c0_guest_entryhi(cop0) & ASID_MASK);
  1040. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1041. /* save old pc */
  1042. kvm_write_c0_guest_epc(cop0, arch->pc);
  1043. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1044. if (cause & CAUSEF_BD)
  1045. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1046. else
  1047. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1048. kvm_debug("[EXL == 0] delivering TLB INV @ pc %#lx\n",
  1049. arch->pc);
  1050. /* set pc to the exception entry point */
  1051. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1052. } else {
  1053. kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n",
  1054. arch->pc);
  1055. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1056. }
  1057. kvm_change_c0_guest_cause(cop0, (0xff),
  1058. (T_TLB_LD_MISS << CAUSEB_EXCCODE));
  1059. /* setup badvaddr, context and entryhi registers for the guest */
  1060. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1061. /* XXXKYMA: is the context register used by linux??? */
  1062. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1063. /* Blow away the shadow host TLBs */
  1064. kvm_mips_flush_host_tlb(1);
  1065. return er;
  1066. }
  1067. enum emulation_result
  1068. kvm_mips_emulate_tlbmiss_st(unsigned long cause, uint32_t *opc,
  1069. struct kvm_run *run, struct kvm_vcpu *vcpu)
  1070. {
  1071. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1072. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1073. enum emulation_result er = EMULATE_DONE;
  1074. unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
  1075. (kvm_read_c0_guest_entryhi(cop0) & ASID_MASK);
  1076. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1077. /* save old pc */
  1078. kvm_write_c0_guest_epc(cop0, arch->pc);
  1079. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1080. if (cause & CAUSEF_BD)
  1081. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1082. else
  1083. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1084. kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n",
  1085. arch->pc);
  1086. /* Set PC to the exception entry point */
  1087. arch->pc = KVM_GUEST_KSEG0 + 0x0;
  1088. } else {
  1089. kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n",
  1090. arch->pc);
  1091. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1092. }
  1093. kvm_change_c0_guest_cause(cop0, (0xff),
  1094. (T_TLB_ST_MISS << CAUSEB_EXCCODE));
  1095. /* setup badvaddr, context and entryhi registers for the guest */
  1096. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1097. /* XXXKYMA: is the context register used by linux??? */
  1098. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1099. /* Blow away the shadow host TLBs */
  1100. kvm_mips_flush_host_tlb(1);
  1101. return er;
  1102. }
  1103. enum emulation_result
  1104. kvm_mips_emulate_tlbinv_st(unsigned long cause, uint32_t *opc,
  1105. struct kvm_run *run, struct kvm_vcpu *vcpu)
  1106. {
  1107. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1108. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1109. enum emulation_result er = EMULATE_DONE;
  1110. unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
  1111. (kvm_read_c0_guest_entryhi(cop0) & ASID_MASK);
  1112. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1113. /* save old pc */
  1114. kvm_write_c0_guest_epc(cop0, arch->pc);
  1115. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1116. if (cause & CAUSEF_BD)
  1117. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1118. else
  1119. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1120. kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n",
  1121. arch->pc);
  1122. /* Set PC to the exception entry point */
  1123. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1124. } else {
  1125. kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n",
  1126. arch->pc);
  1127. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1128. }
  1129. kvm_change_c0_guest_cause(cop0, (0xff),
  1130. (T_TLB_ST_MISS << CAUSEB_EXCCODE));
  1131. /* setup badvaddr, context and entryhi registers for the guest */
  1132. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1133. /* XXXKYMA: is the context register used by linux??? */
  1134. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1135. /* Blow away the shadow host TLBs */
  1136. kvm_mips_flush_host_tlb(1);
  1137. return er;
  1138. }
  1139. /* TLBMOD: store into address matching TLB with Dirty bit off */
  1140. enum emulation_result
  1141. kvm_mips_handle_tlbmod(unsigned long cause, uint32_t *opc,
  1142. struct kvm_run *run, struct kvm_vcpu *vcpu)
  1143. {
  1144. enum emulation_result er = EMULATE_DONE;
  1145. #ifdef DEBUG
  1146. /*
  1147. * If address not in the guest TLB, then we are in trouble
  1148. */
  1149. index = kvm_mips_guest_tlb_lookup(vcpu, entryhi);
  1150. if (index < 0) {
  1151. /* XXXKYMA Invalidate and retry */
  1152. kvm_mips_host_tlb_inv(vcpu, vcpu->arch.host_cp0_badvaddr);
  1153. kvm_err("%s: host got TLBMOD for %#lx but entry not present in Guest TLB\n",
  1154. __func__, entryhi);
  1155. kvm_mips_dump_guest_tlbs(vcpu);
  1156. kvm_mips_dump_host_tlbs();
  1157. return EMULATE_FAIL;
  1158. }
  1159. #endif
  1160. er = kvm_mips_emulate_tlbmod(cause, opc, run, vcpu);
  1161. return er;
  1162. }
  1163. enum emulation_result
  1164. kvm_mips_emulate_tlbmod(unsigned long cause, uint32_t *opc,
  1165. struct kvm_run *run, struct kvm_vcpu *vcpu)
  1166. {
  1167. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1168. unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
  1169. (kvm_read_c0_guest_entryhi(cop0) & ASID_MASK);
  1170. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1171. enum emulation_result er = EMULATE_DONE;
  1172. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1173. /* save old pc */
  1174. kvm_write_c0_guest_epc(cop0, arch->pc);
  1175. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1176. if (cause & CAUSEF_BD)
  1177. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1178. else
  1179. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1180. kvm_debug("[EXL == 0] Delivering TLB MOD @ pc %#lx\n",
  1181. arch->pc);
  1182. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1183. } else {
  1184. kvm_debug("[EXL == 1] Delivering TLB MOD @ pc %#lx\n",
  1185. arch->pc);
  1186. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1187. }
  1188. kvm_change_c0_guest_cause(cop0, (0xff), (T_TLB_MOD << CAUSEB_EXCCODE));
  1189. /* setup badvaddr, context and entryhi registers for the guest */
  1190. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1191. /* XXXKYMA: is the context register used by linux??? */
  1192. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1193. /* Blow away the shadow host TLBs */
  1194. kvm_mips_flush_host_tlb(1);
  1195. return er;
  1196. }
  1197. enum emulation_result
  1198. kvm_mips_emulate_fpu_exc(unsigned long cause, uint32_t *opc,
  1199. struct kvm_run *run, struct kvm_vcpu *vcpu)
  1200. {
  1201. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1202. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1203. enum emulation_result er = EMULATE_DONE;
  1204. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1205. /* save old pc */
  1206. kvm_write_c0_guest_epc(cop0, arch->pc);
  1207. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1208. if (cause & CAUSEF_BD)
  1209. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1210. else
  1211. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1212. }
  1213. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1214. kvm_change_c0_guest_cause(cop0, (0xff),
  1215. (T_COP_UNUSABLE << CAUSEB_EXCCODE));
  1216. kvm_change_c0_guest_cause(cop0, (CAUSEF_CE), (0x1 << CAUSEB_CE));
  1217. return er;
  1218. }
  1219. enum emulation_result
  1220. kvm_mips_emulate_ri_exc(unsigned long cause, uint32_t *opc,
  1221. struct kvm_run *run, struct kvm_vcpu *vcpu)
  1222. {
  1223. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1224. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1225. enum emulation_result er = EMULATE_DONE;
  1226. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1227. /* save old pc */
  1228. kvm_write_c0_guest_epc(cop0, arch->pc);
  1229. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1230. if (cause & CAUSEF_BD)
  1231. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1232. else
  1233. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1234. kvm_debug("Delivering RI @ pc %#lx\n", arch->pc);
  1235. kvm_change_c0_guest_cause(cop0, (0xff),
  1236. (T_RES_INST << CAUSEB_EXCCODE));
  1237. /* Set PC to the exception entry point */
  1238. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1239. } else {
  1240. kvm_err("Trying to deliver RI when EXL is already set\n");
  1241. er = EMULATE_FAIL;
  1242. }
  1243. return er;
  1244. }
  1245. enum emulation_result
  1246. kvm_mips_emulate_bp_exc(unsigned long cause, uint32_t *opc,
  1247. struct kvm_run *run, struct kvm_vcpu *vcpu)
  1248. {
  1249. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1250. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1251. enum emulation_result er = EMULATE_DONE;
  1252. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1253. /* save old pc */
  1254. kvm_write_c0_guest_epc(cop0, arch->pc);
  1255. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1256. if (cause & CAUSEF_BD)
  1257. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1258. else
  1259. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1260. kvm_debug("Delivering BP @ pc %#lx\n", arch->pc);
  1261. kvm_change_c0_guest_cause(cop0, (0xff),
  1262. (T_BREAK << CAUSEB_EXCCODE));
  1263. /* Set PC to the exception entry point */
  1264. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1265. } else {
  1266. printk("Trying to deliver BP when EXL is already set\n");
  1267. er = EMULATE_FAIL;
  1268. }
  1269. return er;
  1270. }
  1271. /*
  1272. * ll/sc, rdhwr, sync emulation
  1273. */
  1274. #define OPCODE 0xfc000000
  1275. #define BASE 0x03e00000
  1276. #define RT 0x001f0000
  1277. #define OFFSET 0x0000ffff
  1278. #define LL 0xc0000000
  1279. #define SC 0xe0000000
  1280. #define SPEC0 0x00000000
  1281. #define SPEC3 0x7c000000
  1282. #define RD 0x0000f800
  1283. #define FUNC 0x0000003f
  1284. #define SYNC 0x0000000f
  1285. #define RDHWR 0x0000003b
  1286. enum emulation_result
  1287. kvm_mips_handle_ri(unsigned long cause, uint32_t *opc,
  1288. struct kvm_run *run, struct kvm_vcpu *vcpu)
  1289. {
  1290. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1291. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1292. enum emulation_result er = EMULATE_DONE;
  1293. unsigned long curr_pc;
  1294. uint32_t inst;
  1295. /*
  1296. * Update PC and hold onto current PC in case there is
  1297. * an error and we want to rollback the PC
  1298. */
  1299. curr_pc = vcpu->arch.pc;
  1300. er = update_pc(vcpu, cause);
  1301. if (er == EMULATE_FAIL)
  1302. return er;
  1303. /*
  1304. * Fetch the instruction.
  1305. */
  1306. if (cause & CAUSEF_BD)
  1307. opc += 1;
  1308. inst = kvm_get_inst(opc, vcpu);
  1309. if (inst == KVM_INVALID_INST) {
  1310. printk("%s: Cannot get inst @ %p\n", __func__, opc);
  1311. return EMULATE_FAIL;
  1312. }
  1313. if ((inst & OPCODE) == SPEC3 && (inst & FUNC) == RDHWR) {
  1314. int rd = (inst & RD) >> 11;
  1315. int rt = (inst & RT) >> 16;
  1316. switch (rd) {
  1317. case 0: /* CPU number */
  1318. arch->gprs[rt] = 0;
  1319. break;
  1320. case 1: /* SYNCI length */
  1321. arch->gprs[rt] = min(current_cpu_data.dcache.linesz,
  1322. current_cpu_data.icache.linesz);
  1323. break;
  1324. case 2: /* Read count register */
  1325. printk("RDHWR: Cont register\n");
  1326. arch->gprs[rt] = kvm_read_c0_guest_count(cop0);
  1327. break;
  1328. case 3: /* Count register resolution */
  1329. switch (current_cpu_data.cputype) {
  1330. case CPU_20KC:
  1331. case CPU_25KF:
  1332. arch->gprs[rt] = 1;
  1333. break;
  1334. default:
  1335. arch->gprs[rt] = 2;
  1336. }
  1337. break;
  1338. case 29:
  1339. #if 1
  1340. arch->gprs[rt] = kvm_read_c0_guest_userlocal(cop0);
  1341. #else
  1342. /* UserLocal not implemented */
  1343. er = kvm_mips_emulate_ri_exc(cause, opc, run, vcpu);
  1344. #endif
  1345. break;
  1346. default:
  1347. printk("RDHWR not supported\n");
  1348. er = EMULATE_FAIL;
  1349. break;
  1350. }
  1351. } else {
  1352. printk("Emulate RI not supported @ %p: %#x\n", opc, inst);
  1353. er = EMULATE_FAIL;
  1354. }
  1355. /*
  1356. * Rollback PC only if emulation was unsuccessful
  1357. */
  1358. if (er == EMULATE_FAIL) {
  1359. vcpu->arch.pc = curr_pc;
  1360. }
  1361. return er;
  1362. }
  1363. enum emulation_result
  1364. kvm_mips_complete_mmio_load(struct kvm_vcpu *vcpu, struct kvm_run *run)
  1365. {
  1366. unsigned long *gpr = &vcpu->arch.gprs[vcpu->arch.io_gpr];
  1367. enum emulation_result er = EMULATE_DONE;
  1368. unsigned long curr_pc;
  1369. if (run->mmio.len > sizeof(*gpr)) {
  1370. printk("Bad MMIO length: %d", run->mmio.len);
  1371. er = EMULATE_FAIL;
  1372. goto done;
  1373. }
  1374. /*
  1375. * Update PC and hold onto current PC in case there is
  1376. * an error and we want to rollback the PC
  1377. */
  1378. curr_pc = vcpu->arch.pc;
  1379. er = update_pc(vcpu, vcpu->arch.pending_load_cause);
  1380. if (er == EMULATE_FAIL)
  1381. return er;
  1382. switch (run->mmio.len) {
  1383. case 4:
  1384. *gpr = *(int32_t *) run->mmio.data;
  1385. break;
  1386. case 2:
  1387. if (vcpu->mmio_needed == 2)
  1388. *gpr = *(int16_t *) run->mmio.data;
  1389. else
  1390. *gpr = *(int16_t *) run->mmio.data;
  1391. break;
  1392. case 1:
  1393. if (vcpu->mmio_needed == 2)
  1394. *gpr = *(int8_t *) run->mmio.data;
  1395. else
  1396. *gpr = *(u8 *) run->mmio.data;
  1397. break;
  1398. }
  1399. if (vcpu->arch.pending_load_cause & CAUSEF_BD)
  1400. kvm_debug
  1401. ("[%#lx] Completing %d byte BD Load to gpr %d (0x%08lx) type %d\n",
  1402. vcpu->arch.pc, run->mmio.len, vcpu->arch.io_gpr, *gpr,
  1403. vcpu->mmio_needed);
  1404. done:
  1405. return er;
  1406. }
  1407. static enum emulation_result
  1408. kvm_mips_emulate_exc(unsigned long cause, uint32_t *opc,
  1409. struct kvm_run *run, struct kvm_vcpu *vcpu)
  1410. {
  1411. uint32_t exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
  1412. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1413. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1414. enum emulation_result er = EMULATE_DONE;
  1415. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1416. /* save old pc */
  1417. kvm_write_c0_guest_epc(cop0, arch->pc);
  1418. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1419. if (cause & CAUSEF_BD)
  1420. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1421. else
  1422. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1423. kvm_change_c0_guest_cause(cop0, (0xff),
  1424. (exccode << CAUSEB_EXCCODE));
  1425. /* Set PC to the exception entry point */
  1426. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1427. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1428. kvm_debug("Delivering EXC %d @ pc %#lx, badVaddr: %#lx\n",
  1429. exccode, kvm_read_c0_guest_epc(cop0),
  1430. kvm_read_c0_guest_badvaddr(cop0));
  1431. } else {
  1432. printk("Trying to deliver EXC when EXL is already set\n");
  1433. er = EMULATE_FAIL;
  1434. }
  1435. return er;
  1436. }
  1437. enum emulation_result
  1438. kvm_mips_check_privilege(unsigned long cause, uint32_t *opc,
  1439. struct kvm_run *run, struct kvm_vcpu *vcpu)
  1440. {
  1441. enum emulation_result er = EMULATE_DONE;
  1442. uint32_t exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
  1443. unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
  1444. int usermode = !KVM_GUEST_KERNEL_MODE(vcpu);
  1445. if (usermode) {
  1446. switch (exccode) {
  1447. case T_INT:
  1448. case T_SYSCALL:
  1449. case T_BREAK:
  1450. case T_RES_INST:
  1451. break;
  1452. case T_COP_UNUSABLE:
  1453. if (((cause & CAUSEF_CE) >> CAUSEB_CE) == 0)
  1454. er = EMULATE_PRIV_FAIL;
  1455. break;
  1456. case T_TLB_MOD:
  1457. break;
  1458. case T_TLB_LD_MISS:
  1459. /* We we are accessing Guest kernel space, then send an address error exception to the guest */
  1460. if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
  1461. printk("%s: LD MISS @ %#lx\n", __func__,
  1462. badvaddr);
  1463. cause &= ~0xff;
  1464. cause |= (T_ADDR_ERR_LD << CAUSEB_EXCCODE);
  1465. er = EMULATE_PRIV_FAIL;
  1466. }
  1467. break;
  1468. case T_TLB_ST_MISS:
  1469. /* We we are accessing Guest kernel space, then send an address error exception to the guest */
  1470. if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
  1471. printk("%s: ST MISS @ %#lx\n", __func__,
  1472. badvaddr);
  1473. cause &= ~0xff;
  1474. cause |= (T_ADDR_ERR_ST << CAUSEB_EXCCODE);
  1475. er = EMULATE_PRIV_FAIL;
  1476. }
  1477. break;
  1478. case T_ADDR_ERR_ST:
  1479. printk("%s: address error ST @ %#lx\n", __func__,
  1480. badvaddr);
  1481. if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
  1482. cause &= ~0xff;
  1483. cause |= (T_TLB_ST_MISS << CAUSEB_EXCCODE);
  1484. }
  1485. er = EMULATE_PRIV_FAIL;
  1486. break;
  1487. case T_ADDR_ERR_LD:
  1488. printk("%s: address error LD @ %#lx\n", __func__,
  1489. badvaddr);
  1490. if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
  1491. cause &= ~0xff;
  1492. cause |= (T_TLB_LD_MISS << CAUSEB_EXCCODE);
  1493. }
  1494. er = EMULATE_PRIV_FAIL;
  1495. break;
  1496. default:
  1497. er = EMULATE_PRIV_FAIL;
  1498. break;
  1499. }
  1500. }
  1501. if (er == EMULATE_PRIV_FAIL) {
  1502. kvm_mips_emulate_exc(cause, opc, run, vcpu);
  1503. }
  1504. return er;
  1505. }
  1506. /* User Address (UA) fault, this could happen if
  1507. * (1) TLB entry not present/valid in both Guest and shadow host TLBs, in this
  1508. * case we pass on the fault to the guest kernel and let it handle it.
  1509. * (2) TLB entry is present in the Guest TLB but not in the shadow, in this
  1510. * case we inject the TLB from the Guest TLB into the shadow host TLB
  1511. */
  1512. enum emulation_result
  1513. kvm_mips_handle_tlbmiss(unsigned long cause, uint32_t *opc,
  1514. struct kvm_run *run, struct kvm_vcpu *vcpu)
  1515. {
  1516. enum emulation_result er = EMULATE_DONE;
  1517. uint32_t exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
  1518. unsigned long va = vcpu->arch.host_cp0_badvaddr;
  1519. int index;
  1520. kvm_debug("kvm_mips_handle_tlbmiss: badvaddr: %#lx, entryhi: %#lx\n",
  1521. vcpu->arch.host_cp0_badvaddr, vcpu->arch.host_cp0_entryhi);
  1522. /* KVM would not have got the exception if this entry was valid in the shadow host TLB
  1523. * Check the Guest TLB, if the entry is not there then send the guest an
  1524. * exception. The guest exc handler should then inject an entry into the
  1525. * guest TLB
  1526. */
  1527. index = kvm_mips_guest_tlb_lookup(vcpu,
  1528. (va & VPN2_MASK) |
  1529. (kvm_read_c0_guest_entryhi
  1530. (vcpu->arch.cop0) & ASID_MASK));
  1531. if (index < 0) {
  1532. if (exccode == T_TLB_LD_MISS) {
  1533. er = kvm_mips_emulate_tlbmiss_ld(cause, opc, run, vcpu);
  1534. } else if (exccode == T_TLB_ST_MISS) {
  1535. er = kvm_mips_emulate_tlbmiss_st(cause, opc, run, vcpu);
  1536. } else {
  1537. printk("%s: invalid exc code: %d\n", __func__, exccode);
  1538. er = EMULATE_FAIL;
  1539. }
  1540. } else {
  1541. struct kvm_mips_tlb *tlb = &vcpu->arch.guest_tlb[index];
  1542. /* Check if the entry is valid, if not then setup a TLB invalid exception to the guest */
  1543. if (!TLB_IS_VALID(*tlb, va)) {
  1544. if (exccode == T_TLB_LD_MISS) {
  1545. er = kvm_mips_emulate_tlbinv_ld(cause, opc, run,
  1546. vcpu);
  1547. } else if (exccode == T_TLB_ST_MISS) {
  1548. er = kvm_mips_emulate_tlbinv_st(cause, opc, run,
  1549. vcpu);
  1550. } else {
  1551. printk("%s: invalid exc code: %d\n", __func__,
  1552. exccode);
  1553. er = EMULATE_FAIL;
  1554. }
  1555. } else {
  1556. #ifdef DEBUG
  1557. kvm_debug
  1558. ("Injecting hi: %#lx, lo0: %#lx, lo1: %#lx into shadow host TLB\n",
  1559. tlb->tlb_hi, tlb->tlb_lo0, tlb->tlb_lo1);
  1560. #endif
  1561. /* OK we have a Guest TLB entry, now inject it into the shadow host TLB */
  1562. kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb, NULL,
  1563. NULL);
  1564. }
  1565. }
  1566. return er;
  1567. }