kvm_mips_emul.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324
  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/ktime.h>
  14. #include <linux/kvm_host.h>
  15. #include <linux/module.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/fs.h>
  18. #include <linux/bootmem.h>
  19. #include <linux/random.h>
  20. #include <asm/page.h>
  21. #include <asm/cacheflush.h>
  22. #include <asm/cpu-info.h>
  23. #include <asm/mmu_context.h>
  24. #include <asm/tlbflush.h>
  25. #include <asm/inst.h>
  26. #undef CONFIG_MIPS_MT
  27. #include <asm/r4kcache.h>
  28. #define CONFIG_MIPS_MT
  29. #include "kvm_mips_opcode.h"
  30. #include "kvm_mips_int.h"
  31. #include "kvm_mips_comm.h"
  32. #include "trace.h"
  33. /*
  34. * Compute the return address and do emulate branch simulation, if required.
  35. * This function should be called only in branch delay slot active.
  36. */
  37. unsigned long kvm_compute_return_epc(struct kvm_vcpu *vcpu,
  38. unsigned long instpc)
  39. {
  40. unsigned int dspcontrol;
  41. union mips_instruction insn;
  42. struct kvm_vcpu_arch *arch = &vcpu->arch;
  43. long epc = instpc;
  44. long nextpc = KVM_INVALID_INST;
  45. if (epc & 3)
  46. goto unaligned;
  47. /* Read the instruction */
  48. insn.word = kvm_get_inst((uint32_t *) epc, vcpu);
  49. if (insn.word == KVM_INVALID_INST)
  50. return KVM_INVALID_INST;
  51. switch (insn.i_format.opcode) {
  52. /* jr and jalr are in r_format format. */
  53. case spec_op:
  54. switch (insn.r_format.func) {
  55. case jalr_op:
  56. arch->gprs[insn.r_format.rd] = epc + 8;
  57. /* Fall through */
  58. case jr_op:
  59. nextpc = arch->gprs[insn.r_format.rs];
  60. break;
  61. }
  62. break;
  63. /*
  64. * This group contains:
  65. * bltz_op, bgez_op, bltzl_op, bgezl_op,
  66. * bltzal_op, bgezal_op, bltzall_op, bgezall_op.
  67. */
  68. case bcond_op:
  69. switch (insn.i_format.rt) {
  70. case bltz_op:
  71. case bltzl_op:
  72. if ((long)arch->gprs[insn.i_format.rs] < 0)
  73. epc = epc + 4 + (insn.i_format.simmediate << 2);
  74. else
  75. epc += 8;
  76. nextpc = epc;
  77. break;
  78. case bgez_op:
  79. case bgezl_op:
  80. if ((long)arch->gprs[insn.i_format.rs] >= 0)
  81. epc = epc + 4 + (insn.i_format.simmediate << 2);
  82. else
  83. epc += 8;
  84. nextpc = epc;
  85. break;
  86. case bltzal_op:
  87. case bltzall_op:
  88. arch->gprs[31] = epc + 8;
  89. if ((long)arch->gprs[insn.i_format.rs] < 0)
  90. epc = epc + 4 + (insn.i_format.simmediate << 2);
  91. else
  92. epc += 8;
  93. nextpc = epc;
  94. break;
  95. case bgezal_op:
  96. case bgezall_op:
  97. arch->gprs[31] = epc + 8;
  98. if ((long)arch->gprs[insn.i_format.rs] >= 0)
  99. epc = epc + 4 + (insn.i_format.simmediate << 2);
  100. else
  101. epc += 8;
  102. nextpc = epc;
  103. break;
  104. case bposge32_op:
  105. if (!cpu_has_dsp)
  106. goto sigill;
  107. dspcontrol = rddsp(0x01);
  108. if (dspcontrol >= 32)
  109. epc = epc + 4 + (insn.i_format.simmediate << 2);
  110. else
  111. epc += 8;
  112. nextpc = epc;
  113. break;
  114. }
  115. break;
  116. /* These are unconditional and in j_format. */
  117. case jal_op:
  118. arch->gprs[31] = instpc + 8;
  119. case j_op:
  120. epc += 4;
  121. epc >>= 28;
  122. epc <<= 28;
  123. epc |= (insn.j_format.target << 2);
  124. nextpc = epc;
  125. break;
  126. /* These are conditional and in i_format. */
  127. case beq_op:
  128. case beql_op:
  129. if (arch->gprs[insn.i_format.rs] ==
  130. arch->gprs[insn.i_format.rt])
  131. epc = epc + 4 + (insn.i_format.simmediate << 2);
  132. else
  133. epc += 8;
  134. nextpc = epc;
  135. break;
  136. case bne_op:
  137. case bnel_op:
  138. if (arch->gprs[insn.i_format.rs] !=
  139. arch->gprs[insn.i_format.rt])
  140. epc = epc + 4 + (insn.i_format.simmediate << 2);
  141. else
  142. epc += 8;
  143. nextpc = epc;
  144. break;
  145. case blez_op: /* not really i_format */
  146. case blezl_op:
  147. /* rt field assumed to be zero */
  148. if ((long)arch->gprs[insn.i_format.rs] <= 0)
  149. epc = epc + 4 + (insn.i_format.simmediate << 2);
  150. else
  151. epc += 8;
  152. nextpc = epc;
  153. break;
  154. case bgtz_op:
  155. case bgtzl_op:
  156. /* rt field assumed to be zero */
  157. if ((long)arch->gprs[insn.i_format.rs] > 0)
  158. epc = epc + 4 + (insn.i_format.simmediate << 2);
  159. else
  160. epc += 8;
  161. nextpc = epc;
  162. break;
  163. /* And now the FPA/cp1 branch instructions. */
  164. case cop1_op:
  165. kvm_err("%s: unsupported cop1_op\n", __func__);
  166. break;
  167. }
  168. return nextpc;
  169. unaligned:
  170. kvm_err("%s: unaligned epc\n", __func__);
  171. return nextpc;
  172. sigill:
  173. kvm_err("%s: DSP branch but not DSP ASE\n", __func__);
  174. return nextpc;
  175. }
  176. enum emulation_result update_pc(struct kvm_vcpu *vcpu, uint32_t cause)
  177. {
  178. unsigned long branch_pc;
  179. enum emulation_result er = EMULATE_DONE;
  180. if (cause & CAUSEF_BD) {
  181. branch_pc = kvm_compute_return_epc(vcpu, vcpu->arch.pc);
  182. if (branch_pc == KVM_INVALID_INST) {
  183. er = EMULATE_FAIL;
  184. } else {
  185. vcpu->arch.pc = branch_pc;
  186. kvm_debug("BD update_pc(): New PC: %#lx\n",
  187. vcpu->arch.pc);
  188. }
  189. } else
  190. vcpu->arch.pc += 4;
  191. kvm_debug("update_pc(): New PC: %#lx\n", vcpu->arch.pc);
  192. return er;
  193. }
  194. /**
  195. * kvm_mips_count_disabled() - Find whether the CP0_Count timer is disabled.
  196. * @vcpu: Virtual CPU.
  197. *
  198. * Returns: 1 if the CP0_Count timer is disabled by either the guest
  199. * CP0_Cause.DC bit or the count_ctl.DC bit.
  200. * 0 otherwise (in which case CP0_Count timer is running).
  201. */
  202. static inline int kvm_mips_count_disabled(struct kvm_vcpu *vcpu)
  203. {
  204. struct mips_coproc *cop0 = vcpu->arch.cop0;
  205. return (vcpu->arch.count_ctl & KVM_REG_MIPS_COUNT_CTL_DC) ||
  206. (kvm_read_c0_guest_cause(cop0) & CAUSEF_DC);
  207. }
  208. /**
  209. * kvm_mips_ktime_to_count() - Scale ktime_t to a 32-bit count.
  210. *
  211. * Caches the dynamic nanosecond bias in vcpu->arch.count_dyn_bias.
  212. *
  213. * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
  214. */
  215. static uint32_t kvm_mips_ktime_to_count(struct kvm_vcpu *vcpu, ktime_t now)
  216. {
  217. s64 now_ns, periods;
  218. u64 delta;
  219. now_ns = ktime_to_ns(now);
  220. delta = now_ns + vcpu->arch.count_dyn_bias;
  221. if (delta >= vcpu->arch.count_period) {
  222. /* If delta is out of safe range the bias needs adjusting */
  223. periods = div64_s64(now_ns, vcpu->arch.count_period);
  224. vcpu->arch.count_dyn_bias = -periods * vcpu->arch.count_period;
  225. /* Recalculate delta with new bias */
  226. delta = now_ns + vcpu->arch.count_dyn_bias;
  227. }
  228. /*
  229. * We've ensured that:
  230. * delta < count_period
  231. *
  232. * Therefore the intermediate delta*count_hz will never overflow since
  233. * at the boundary condition:
  234. * delta = count_period
  235. * delta = NSEC_PER_SEC * 2^32 / count_hz
  236. * delta * count_hz = NSEC_PER_SEC * 2^32
  237. */
  238. return div_u64(delta * vcpu->arch.count_hz, NSEC_PER_SEC);
  239. }
  240. /**
  241. * kvm_mips_count_time() - Get effective current time.
  242. * @vcpu: Virtual CPU.
  243. *
  244. * Get effective monotonic ktime. This is usually a straightforward ktime_get(),
  245. * except when the master disable bit is set in count_ctl, in which case it is
  246. * count_resume, i.e. the time that the count was disabled.
  247. *
  248. * Returns: Effective monotonic ktime for CP0_Count.
  249. */
  250. static inline ktime_t kvm_mips_count_time(struct kvm_vcpu *vcpu)
  251. {
  252. if (unlikely(vcpu->arch.count_ctl & KVM_REG_MIPS_COUNT_CTL_DC))
  253. return vcpu->arch.count_resume;
  254. return ktime_get();
  255. }
  256. /**
  257. * kvm_mips_read_count_running() - Read the current count value as if running.
  258. * @vcpu: Virtual CPU.
  259. * @now: Kernel time to read CP0_Count at.
  260. *
  261. * Returns the current guest CP0_Count register at time @now and handles if the
  262. * timer interrupt is pending and hasn't been handled yet.
  263. *
  264. * Returns: The current value of the guest CP0_Count register.
  265. */
  266. static uint32_t kvm_mips_read_count_running(struct kvm_vcpu *vcpu, ktime_t now)
  267. {
  268. ktime_t expires;
  269. int running;
  270. /* Is the hrtimer pending? */
  271. expires = hrtimer_get_expires(&vcpu->arch.comparecount_timer);
  272. if (ktime_compare(now, expires) >= 0) {
  273. /*
  274. * Cancel it while we handle it so there's no chance of
  275. * interference with the timeout handler.
  276. */
  277. running = hrtimer_cancel(&vcpu->arch.comparecount_timer);
  278. /* Nothing should be waiting on the timeout */
  279. kvm_mips_callbacks->queue_timer_int(vcpu);
  280. /*
  281. * Restart the timer if it was running based on the expiry time
  282. * we read, so that we don't push it back 2 periods.
  283. */
  284. if (running) {
  285. expires = ktime_add_ns(expires,
  286. vcpu->arch.count_period);
  287. hrtimer_start(&vcpu->arch.comparecount_timer, expires,
  288. HRTIMER_MODE_ABS);
  289. }
  290. }
  291. /* Return the biased and scaled guest CP0_Count */
  292. return vcpu->arch.count_bias + kvm_mips_ktime_to_count(vcpu, now);
  293. }
  294. /**
  295. * kvm_mips_read_count() - Read the current count value.
  296. * @vcpu: Virtual CPU.
  297. *
  298. * Read the current guest CP0_Count value, taking into account whether the timer
  299. * is stopped.
  300. *
  301. * Returns: The current guest CP0_Count value.
  302. */
  303. uint32_t kvm_mips_read_count(struct kvm_vcpu *vcpu)
  304. {
  305. struct mips_coproc *cop0 = vcpu->arch.cop0;
  306. /* If count disabled just read static copy of count */
  307. if (kvm_mips_count_disabled(vcpu))
  308. return kvm_read_c0_guest_count(cop0);
  309. return kvm_mips_read_count_running(vcpu, ktime_get());
  310. }
  311. /**
  312. * kvm_mips_freeze_hrtimer() - Safely stop the hrtimer.
  313. * @vcpu: Virtual CPU.
  314. * @count: Output pointer for CP0_Count value at point of freeze.
  315. *
  316. * Freeze the hrtimer safely and return both the ktime and the CP0_Count value
  317. * at the point it was frozen. It is guaranteed that any pending interrupts at
  318. * the point it was frozen are handled, and none after that point.
  319. *
  320. * This is useful where the time/CP0_Count is needed in the calculation of the
  321. * new parameters.
  322. *
  323. * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
  324. *
  325. * Returns: The ktime at the point of freeze.
  326. */
  327. static ktime_t kvm_mips_freeze_hrtimer(struct kvm_vcpu *vcpu,
  328. uint32_t *count)
  329. {
  330. ktime_t now;
  331. /* stop hrtimer before finding time */
  332. hrtimer_cancel(&vcpu->arch.comparecount_timer);
  333. now = ktime_get();
  334. /* find count at this point and handle pending hrtimer */
  335. *count = kvm_mips_read_count_running(vcpu, now);
  336. return now;
  337. }
  338. /**
  339. * kvm_mips_resume_hrtimer() - Resume hrtimer, updating expiry.
  340. * @vcpu: Virtual CPU.
  341. * @now: ktime at point of resume.
  342. * @count: CP0_Count at point of resume.
  343. *
  344. * Resumes the timer and updates the timer expiry based on @now and @count.
  345. * This can be used in conjunction with kvm_mips_freeze_timer() when timer
  346. * parameters need to be changed.
  347. *
  348. * It is guaranteed that a timer interrupt immediately after resume will be
  349. * handled, but not if CP_Compare is exactly at @count. That case is already
  350. * handled by kvm_mips_freeze_timer().
  351. *
  352. * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
  353. */
  354. static void kvm_mips_resume_hrtimer(struct kvm_vcpu *vcpu,
  355. ktime_t now, uint32_t count)
  356. {
  357. struct mips_coproc *cop0 = vcpu->arch.cop0;
  358. uint32_t compare;
  359. u64 delta;
  360. ktime_t expire;
  361. /* Calculate timeout (wrap 0 to 2^32) */
  362. compare = kvm_read_c0_guest_compare(cop0);
  363. delta = (u64)(uint32_t)(compare - count - 1) + 1;
  364. delta = div_u64(delta * NSEC_PER_SEC, vcpu->arch.count_hz);
  365. expire = ktime_add_ns(now, delta);
  366. /* Update hrtimer to use new timeout */
  367. hrtimer_cancel(&vcpu->arch.comparecount_timer);
  368. hrtimer_start(&vcpu->arch.comparecount_timer, expire, HRTIMER_MODE_ABS);
  369. }
  370. /**
  371. * kvm_mips_update_hrtimer() - Update next expiry time of hrtimer.
  372. * @vcpu: Virtual CPU.
  373. *
  374. * Recalculates and updates the expiry time of the hrtimer. This can be used
  375. * after timer parameters have been altered which do not depend on the time that
  376. * the change occurs (in those cases kvm_mips_freeze_hrtimer() and
  377. * kvm_mips_resume_hrtimer() are used directly).
  378. *
  379. * It is guaranteed that no timer interrupts will be lost in the process.
  380. *
  381. * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
  382. */
  383. static void kvm_mips_update_hrtimer(struct kvm_vcpu *vcpu)
  384. {
  385. ktime_t now;
  386. uint32_t count;
  387. /*
  388. * freeze_hrtimer takes care of a timer interrupts <= count, and
  389. * resume_hrtimer the hrtimer takes care of a timer interrupts > count.
  390. */
  391. now = kvm_mips_freeze_hrtimer(vcpu, &count);
  392. kvm_mips_resume_hrtimer(vcpu, now, count);
  393. }
  394. /**
  395. * kvm_mips_write_count() - Modify the count and update timer.
  396. * @vcpu: Virtual CPU.
  397. * @count: Guest CP0_Count value to set.
  398. *
  399. * Sets the CP0_Count value and updates the timer accordingly.
  400. */
  401. void kvm_mips_write_count(struct kvm_vcpu *vcpu, uint32_t count)
  402. {
  403. struct mips_coproc *cop0 = vcpu->arch.cop0;
  404. ktime_t now;
  405. /* Calculate bias */
  406. now = kvm_mips_count_time(vcpu);
  407. vcpu->arch.count_bias = count - kvm_mips_ktime_to_count(vcpu, now);
  408. if (kvm_mips_count_disabled(vcpu))
  409. /* The timer's disabled, adjust the static count */
  410. kvm_write_c0_guest_count(cop0, count);
  411. else
  412. /* Update timeout */
  413. kvm_mips_resume_hrtimer(vcpu, now, count);
  414. }
  415. /**
  416. * kvm_mips_init_count() - Initialise timer.
  417. * @vcpu: Virtual CPU.
  418. *
  419. * Initialise the timer to a sensible frequency, namely 100MHz, zero it, and set
  420. * it going if it's enabled.
  421. */
  422. void kvm_mips_init_count(struct kvm_vcpu *vcpu)
  423. {
  424. /* 100 MHz */
  425. vcpu->arch.count_hz = 100*1000*1000;
  426. vcpu->arch.count_period = div_u64((u64)NSEC_PER_SEC << 32,
  427. vcpu->arch.count_hz);
  428. vcpu->arch.count_dyn_bias = 0;
  429. /* Starting at 0 */
  430. kvm_mips_write_count(vcpu, 0);
  431. }
  432. /**
  433. * kvm_mips_set_count_hz() - Update the frequency of the timer.
  434. * @vcpu: Virtual CPU.
  435. * @count_hz: Frequency of CP0_Count timer in Hz.
  436. *
  437. * Change the frequency of the CP0_Count timer. This is done atomically so that
  438. * CP0_Count is continuous and no timer interrupt is lost.
  439. *
  440. * Returns: -EINVAL if @count_hz is out of range.
  441. * 0 on success.
  442. */
  443. int kvm_mips_set_count_hz(struct kvm_vcpu *vcpu, s64 count_hz)
  444. {
  445. struct mips_coproc *cop0 = vcpu->arch.cop0;
  446. int dc;
  447. ktime_t now;
  448. u32 count;
  449. /* ensure the frequency is in a sensible range... */
  450. if (count_hz <= 0 || count_hz > NSEC_PER_SEC)
  451. return -EINVAL;
  452. /* ... and has actually changed */
  453. if (vcpu->arch.count_hz == count_hz)
  454. return 0;
  455. /* Safely freeze timer so we can keep it continuous */
  456. dc = kvm_mips_count_disabled(vcpu);
  457. if (dc) {
  458. now = kvm_mips_count_time(vcpu);
  459. count = kvm_read_c0_guest_count(cop0);
  460. } else {
  461. now = kvm_mips_freeze_hrtimer(vcpu, &count);
  462. }
  463. /* Update the frequency */
  464. vcpu->arch.count_hz = count_hz;
  465. vcpu->arch.count_period = div_u64((u64)NSEC_PER_SEC << 32, count_hz);
  466. vcpu->arch.count_dyn_bias = 0;
  467. /* Calculate adjusted bias so dynamic count is unchanged */
  468. vcpu->arch.count_bias = count - kvm_mips_ktime_to_count(vcpu, now);
  469. /* Update and resume hrtimer */
  470. if (!dc)
  471. kvm_mips_resume_hrtimer(vcpu, now, count);
  472. return 0;
  473. }
  474. /**
  475. * kvm_mips_write_compare() - Modify compare and update timer.
  476. * @vcpu: Virtual CPU.
  477. * @compare: New CP0_Compare value.
  478. *
  479. * Update CP0_Compare to a new value and update the timeout.
  480. */
  481. void kvm_mips_write_compare(struct kvm_vcpu *vcpu, uint32_t compare)
  482. {
  483. struct mips_coproc *cop0 = vcpu->arch.cop0;
  484. /* if unchanged, must just be an ack */
  485. if (kvm_read_c0_guest_compare(cop0) == compare)
  486. return;
  487. /* Update compare */
  488. kvm_write_c0_guest_compare(cop0, compare);
  489. /* Update timeout if count enabled */
  490. if (!kvm_mips_count_disabled(vcpu))
  491. kvm_mips_update_hrtimer(vcpu);
  492. }
  493. /**
  494. * kvm_mips_count_disable() - Disable count.
  495. * @vcpu: Virtual CPU.
  496. *
  497. * Disable the CP0_Count timer. A timer interrupt on or before the final stop
  498. * time will be handled but not after.
  499. *
  500. * Assumes CP0_Count was previously enabled but now Guest.CP0_Cause.DC or
  501. * count_ctl.DC has been set (count disabled).
  502. *
  503. * Returns: The time that the timer was stopped.
  504. */
  505. static ktime_t kvm_mips_count_disable(struct kvm_vcpu *vcpu)
  506. {
  507. struct mips_coproc *cop0 = vcpu->arch.cop0;
  508. uint32_t count;
  509. ktime_t now;
  510. /* Stop hrtimer */
  511. hrtimer_cancel(&vcpu->arch.comparecount_timer);
  512. /* Set the static count from the dynamic count, handling pending TI */
  513. now = ktime_get();
  514. count = kvm_mips_read_count_running(vcpu, now);
  515. kvm_write_c0_guest_count(cop0, count);
  516. return now;
  517. }
  518. /**
  519. * kvm_mips_count_disable_cause() - Disable count using CP0_Cause.DC.
  520. * @vcpu: Virtual CPU.
  521. *
  522. * Disable the CP0_Count timer and set CP0_Cause.DC. A timer interrupt on or
  523. * before the final stop time will be handled if the timer isn't disabled by
  524. * count_ctl.DC, but not after.
  525. *
  526. * Assumes CP0_Cause.DC is clear (count enabled).
  527. */
  528. void kvm_mips_count_disable_cause(struct kvm_vcpu *vcpu)
  529. {
  530. struct mips_coproc *cop0 = vcpu->arch.cop0;
  531. kvm_set_c0_guest_cause(cop0, CAUSEF_DC);
  532. if (!(vcpu->arch.count_ctl & KVM_REG_MIPS_COUNT_CTL_DC))
  533. kvm_mips_count_disable(vcpu);
  534. }
  535. /**
  536. * kvm_mips_count_enable_cause() - Enable count using CP0_Cause.DC.
  537. * @vcpu: Virtual CPU.
  538. *
  539. * Enable the CP0_Count timer and clear CP0_Cause.DC. A timer interrupt after
  540. * the start time will be handled if the timer isn't disabled by count_ctl.DC,
  541. * potentially before even returning, so the caller should be careful with
  542. * ordering of CP0_Cause modifications so as not to lose it.
  543. *
  544. * Assumes CP0_Cause.DC is set (count disabled).
  545. */
  546. void kvm_mips_count_enable_cause(struct kvm_vcpu *vcpu)
  547. {
  548. struct mips_coproc *cop0 = vcpu->arch.cop0;
  549. uint32_t count;
  550. kvm_clear_c0_guest_cause(cop0, CAUSEF_DC);
  551. /*
  552. * Set the dynamic count to match the static count.
  553. * This starts the hrtimer if count_ctl.DC allows it.
  554. * Otherwise it conveniently updates the biases.
  555. */
  556. count = kvm_read_c0_guest_count(cop0);
  557. kvm_mips_write_count(vcpu, count);
  558. }
  559. /**
  560. * kvm_mips_set_count_ctl() - Update the count control KVM register.
  561. * @vcpu: Virtual CPU.
  562. * @count_ctl: Count control register new value.
  563. *
  564. * Set the count control KVM register. The timer is updated accordingly.
  565. *
  566. * Returns: -EINVAL if reserved bits are set.
  567. * 0 on success.
  568. */
  569. int kvm_mips_set_count_ctl(struct kvm_vcpu *vcpu, s64 count_ctl)
  570. {
  571. struct mips_coproc *cop0 = vcpu->arch.cop0;
  572. s64 changed = count_ctl ^ vcpu->arch.count_ctl;
  573. s64 delta;
  574. ktime_t expire, now;
  575. uint32_t count, compare;
  576. /* Only allow defined bits to be changed */
  577. if (changed & ~(s64)(KVM_REG_MIPS_COUNT_CTL_DC))
  578. return -EINVAL;
  579. /* Apply new value */
  580. vcpu->arch.count_ctl = count_ctl;
  581. /* Master CP0_Count disable */
  582. if (changed & KVM_REG_MIPS_COUNT_CTL_DC) {
  583. /* Is CP0_Cause.DC already disabling CP0_Count? */
  584. if (kvm_read_c0_guest_cause(cop0) & CAUSEF_DC) {
  585. if (count_ctl & KVM_REG_MIPS_COUNT_CTL_DC)
  586. /* Just record the current time */
  587. vcpu->arch.count_resume = ktime_get();
  588. } else if (count_ctl & KVM_REG_MIPS_COUNT_CTL_DC) {
  589. /* disable timer and record current time */
  590. vcpu->arch.count_resume = kvm_mips_count_disable(vcpu);
  591. } else {
  592. /*
  593. * Calculate timeout relative to static count at resume
  594. * time (wrap 0 to 2^32).
  595. */
  596. count = kvm_read_c0_guest_count(cop0);
  597. compare = kvm_read_c0_guest_compare(cop0);
  598. delta = (u64)(uint32_t)(compare - count - 1) + 1;
  599. delta = div_u64(delta * NSEC_PER_SEC,
  600. vcpu->arch.count_hz);
  601. expire = ktime_add_ns(vcpu->arch.count_resume, delta);
  602. /* Handle pending interrupt */
  603. now = ktime_get();
  604. if (ktime_compare(now, expire) >= 0)
  605. /* Nothing should be waiting on the timeout */
  606. kvm_mips_callbacks->queue_timer_int(vcpu);
  607. /* Resume hrtimer without changing bias */
  608. count = kvm_mips_read_count_running(vcpu, now);
  609. kvm_mips_resume_hrtimer(vcpu, now, count);
  610. }
  611. }
  612. return 0;
  613. }
  614. /**
  615. * kvm_mips_set_count_resume() - Update the count resume KVM register.
  616. * @vcpu: Virtual CPU.
  617. * @count_resume: Count resume register new value.
  618. *
  619. * Set the count resume KVM register.
  620. *
  621. * Returns: -EINVAL if out of valid range (0..now).
  622. * 0 on success.
  623. */
  624. int kvm_mips_set_count_resume(struct kvm_vcpu *vcpu, s64 count_resume)
  625. {
  626. /*
  627. * It doesn't make sense for the resume time to be in the future, as it
  628. * would be possible for the next interrupt to be more than a full
  629. * period in the future.
  630. */
  631. if (count_resume < 0 || count_resume > ktime_to_ns(ktime_get()))
  632. return -EINVAL;
  633. vcpu->arch.count_resume = ns_to_ktime(count_resume);
  634. return 0;
  635. }
  636. /**
  637. * kvm_mips_count_timeout() - Push timer forward on timeout.
  638. * @vcpu: Virtual CPU.
  639. *
  640. * Handle an hrtimer event by push the hrtimer forward a period.
  641. *
  642. * Returns: The hrtimer_restart value to return to the hrtimer subsystem.
  643. */
  644. enum hrtimer_restart kvm_mips_count_timeout(struct kvm_vcpu *vcpu)
  645. {
  646. /* Add the Count period to the current expiry time */
  647. hrtimer_add_expires_ns(&vcpu->arch.comparecount_timer,
  648. vcpu->arch.count_period);
  649. return HRTIMER_RESTART;
  650. }
  651. enum emulation_result kvm_mips_emul_eret(struct kvm_vcpu *vcpu)
  652. {
  653. struct mips_coproc *cop0 = vcpu->arch.cop0;
  654. enum emulation_result er = EMULATE_DONE;
  655. if (kvm_read_c0_guest_status(cop0) & ST0_EXL) {
  656. kvm_debug("[%#lx] ERET to %#lx\n", vcpu->arch.pc,
  657. kvm_read_c0_guest_epc(cop0));
  658. kvm_clear_c0_guest_status(cop0, ST0_EXL);
  659. vcpu->arch.pc = kvm_read_c0_guest_epc(cop0);
  660. } else if (kvm_read_c0_guest_status(cop0) & ST0_ERL) {
  661. kvm_clear_c0_guest_status(cop0, ST0_ERL);
  662. vcpu->arch.pc = kvm_read_c0_guest_errorepc(cop0);
  663. } else {
  664. kvm_err("[%#lx] ERET when MIPS_SR_EXL|MIPS_SR_ERL == 0\n",
  665. vcpu->arch.pc);
  666. er = EMULATE_FAIL;
  667. }
  668. return er;
  669. }
  670. enum emulation_result kvm_mips_emul_wait(struct kvm_vcpu *vcpu)
  671. {
  672. kvm_debug("[%#lx] !!!WAIT!!! (%#lx)\n", vcpu->arch.pc,
  673. vcpu->arch.pending_exceptions);
  674. ++vcpu->stat.wait_exits;
  675. trace_kvm_exit(vcpu, WAIT_EXITS);
  676. if (!vcpu->arch.pending_exceptions) {
  677. vcpu->arch.wait = 1;
  678. kvm_vcpu_block(vcpu);
  679. /*
  680. * We we are runnable, then definitely go off to user space to
  681. * check if any I/O interrupts are pending.
  682. */
  683. if (kvm_check_request(KVM_REQ_UNHALT, vcpu)) {
  684. clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
  685. vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  686. }
  687. }
  688. return EMULATE_DONE;
  689. }
  690. /*
  691. * XXXKYMA: Linux doesn't seem to use TLBR, return EMULATE_FAIL for now so that
  692. * we can catch this, if things ever change
  693. */
  694. enum emulation_result kvm_mips_emul_tlbr(struct kvm_vcpu *vcpu)
  695. {
  696. struct mips_coproc *cop0 = vcpu->arch.cop0;
  697. uint32_t pc = vcpu->arch.pc;
  698. kvm_err("[%#x] COP0_TLBR [%ld]\n", pc, kvm_read_c0_guest_index(cop0));
  699. return EMULATE_FAIL;
  700. }
  701. /* Write Guest TLB Entry @ Index */
  702. enum emulation_result kvm_mips_emul_tlbwi(struct kvm_vcpu *vcpu)
  703. {
  704. struct mips_coproc *cop0 = vcpu->arch.cop0;
  705. int index = kvm_read_c0_guest_index(cop0);
  706. struct kvm_mips_tlb *tlb = NULL;
  707. uint32_t pc = vcpu->arch.pc;
  708. if (index < 0 || index >= KVM_MIPS_GUEST_TLB_SIZE) {
  709. kvm_debug("%s: illegal index: %d\n", __func__, index);
  710. kvm_debug("[%#x] COP0_TLBWI [%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx, mask: %#lx)\n",
  711. pc, index, kvm_read_c0_guest_entryhi(cop0),
  712. kvm_read_c0_guest_entrylo0(cop0),
  713. kvm_read_c0_guest_entrylo1(cop0),
  714. kvm_read_c0_guest_pagemask(cop0));
  715. index = (index & ~0x80000000) % KVM_MIPS_GUEST_TLB_SIZE;
  716. }
  717. tlb = &vcpu->arch.guest_tlb[index];
  718. /*
  719. * Probe the shadow host TLB for the entry being overwritten, if one
  720. * matches, invalidate it
  721. */
  722. kvm_mips_host_tlb_inv(vcpu, tlb->tlb_hi);
  723. tlb->tlb_mask = kvm_read_c0_guest_pagemask(cop0);
  724. tlb->tlb_hi = kvm_read_c0_guest_entryhi(cop0);
  725. tlb->tlb_lo0 = kvm_read_c0_guest_entrylo0(cop0);
  726. tlb->tlb_lo1 = kvm_read_c0_guest_entrylo1(cop0);
  727. kvm_debug("[%#x] COP0_TLBWI [%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx, mask: %#lx)\n",
  728. pc, index, kvm_read_c0_guest_entryhi(cop0),
  729. kvm_read_c0_guest_entrylo0(cop0),
  730. kvm_read_c0_guest_entrylo1(cop0),
  731. kvm_read_c0_guest_pagemask(cop0));
  732. return EMULATE_DONE;
  733. }
  734. /* Write Guest TLB Entry @ Random Index */
  735. enum emulation_result kvm_mips_emul_tlbwr(struct kvm_vcpu *vcpu)
  736. {
  737. struct mips_coproc *cop0 = vcpu->arch.cop0;
  738. struct kvm_mips_tlb *tlb = NULL;
  739. uint32_t pc = vcpu->arch.pc;
  740. int index;
  741. get_random_bytes(&index, sizeof(index));
  742. index &= (KVM_MIPS_GUEST_TLB_SIZE - 1);
  743. if (index < 0 || index >= KVM_MIPS_GUEST_TLB_SIZE) {
  744. kvm_err("%s: illegal index: %d\n", __func__, index);
  745. return EMULATE_FAIL;
  746. }
  747. tlb = &vcpu->arch.guest_tlb[index];
  748. /*
  749. * Probe the shadow host TLB for the entry being overwritten, if one
  750. * matches, invalidate it
  751. */
  752. kvm_mips_host_tlb_inv(vcpu, tlb->tlb_hi);
  753. tlb->tlb_mask = kvm_read_c0_guest_pagemask(cop0);
  754. tlb->tlb_hi = kvm_read_c0_guest_entryhi(cop0);
  755. tlb->tlb_lo0 = kvm_read_c0_guest_entrylo0(cop0);
  756. tlb->tlb_lo1 = kvm_read_c0_guest_entrylo1(cop0);
  757. kvm_debug("[%#x] COP0_TLBWR[%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx)\n",
  758. pc, index, kvm_read_c0_guest_entryhi(cop0),
  759. kvm_read_c0_guest_entrylo0(cop0),
  760. kvm_read_c0_guest_entrylo1(cop0));
  761. return EMULATE_DONE;
  762. }
  763. enum emulation_result kvm_mips_emul_tlbp(struct kvm_vcpu *vcpu)
  764. {
  765. struct mips_coproc *cop0 = vcpu->arch.cop0;
  766. long entryhi = kvm_read_c0_guest_entryhi(cop0);
  767. uint32_t pc = vcpu->arch.pc;
  768. int index = -1;
  769. index = kvm_mips_guest_tlb_lookup(vcpu, entryhi);
  770. kvm_write_c0_guest_index(cop0, index);
  771. kvm_debug("[%#x] COP0_TLBP (entryhi: %#lx), index: %d\n", pc, entryhi,
  772. index);
  773. return EMULATE_DONE;
  774. }
  775. enum emulation_result kvm_mips_emulate_CP0(uint32_t inst, uint32_t *opc,
  776. uint32_t cause, struct kvm_run *run,
  777. struct kvm_vcpu *vcpu)
  778. {
  779. struct mips_coproc *cop0 = vcpu->arch.cop0;
  780. enum emulation_result er = EMULATE_DONE;
  781. int32_t rt, rd, copz, sel, co_bit, op;
  782. uint32_t pc = vcpu->arch.pc;
  783. unsigned long curr_pc;
  784. /*
  785. * Update PC and hold onto current PC in case there is
  786. * an error and we want to rollback the PC
  787. */
  788. curr_pc = vcpu->arch.pc;
  789. er = update_pc(vcpu, cause);
  790. if (er == EMULATE_FAIL)
  791. return er;
  792. copz = (inst >> 21) & 0x1f;
  793. rt = (inst >> 16) & 0x1f;
  794. rd = (inst >> 11) & 0x1f;
  795. sel = inst & 0x7;
  796. co_bit = (inst >> 25) & 1;
  797. if (co_bit) {
  798. op = (inst) & 0xff;
  799. switch (op) {
  800. case tlbr_op: /* Read indexed TLB entry */
  801. er = kvm_mips_emul_tlbr(vcpu);
  802. break;
  803. case tlbwi_op: /* Write indexed */
  804. er = kvm_mips_emul_tlbwi(vcpu);
  805. break;
  806. case tlbwr_op: /* Write random */
  807. er = kvm_mips_emul_tlbwr(vcpu);
  808. break;
  809. case tlbp_op: /* TLB Probe */
  810. er = kvm_mips_emul_tlbp(vcpu);
  811. break;
  812. case rfe_op:
  813. kvm_err("!!!COP0_RFE!!!\n");
  814. break;
  815. case eret_op:
  816. er = kvm_mips_emul_eret(vcpu);
  817. goto dont_update_pc;
  818. break;
  819. case wait_op:
  820. er = kvm_mips_emul_wait(vcpu);
  821. break;
  822. }
  823. } else {
  824. switch (copz) {
  825. case mfc_op:
  826. #ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
  827. cop0->stat[rd][sel]++;
  828. #endif
  829. /* Get reg */
  830. if ((rd == MIPS_CP0_COUNT) && (sel == 0)) {
  831. vcpu->arch.gprs[rt] = kvm_mips_read_count(vcpu);
  832. } else if ((rd == MIPS_CP0_ERRCTL) && (sel == 0)) {
  833. vcpu->arch.gprs[rt] = 0x0;
  834. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  835. kvm_mips_trans_mfc0(inst, opc, vcpu);
  836. #endif
  837. } else {
  838. vcpu->arch.gprs[rt] = cop0->reg[rd][sel];
  839. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  840. kvm_mips_trans_mfc0(inst, opc, vcpu);
  841. #endif
  842. }
  843. kvm_debug
  844. ("[%#x] MFCz[%d][%d], vcpu->arch.gprs[%d]: %#lx\n",
  845. pc, rd, sel, rt, vcpu->arch.gprs[rt]);
  846. break;
  847. case dmfc_op:
  848. vcpu->arch.gprs[rt] = cop0->reg[rd][sel];
  849. break;
  850. case mtc_op:
  851. #ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
  852. cop0->stat[rd][sel]++;
  853. #endif
  854. if ((rd == MIPS_CP0_TLB_INDEX)
  855. && (vcpu->arch.gprs[rt] >=
  856. KVM_MIPS_GUEST_TLB_SIZE)) {
  857. kvm_err("Invalid TLB Index: %ld",
  858. vcpu->arch.gprs[rt]);
  859. er = EMULATE_FAIL;
  860. break;
  861. }
  862. #define C0_EBASE_CORE_MASK 0xff
  863. if ((rd == MIPS_CP0_PRID) && (sel == 1)) {
  864. /* Preserve CORE number */
  865. kvm_change_c0_guest_ebase(cop0,
  866. ~(C0_EBASE_CORE_MASK),
  867. vcpu->arch.gprs[rt]);
  868. kvm_err("MTCz, cop0->reg[EBASE]: %#lx\n",
  869. kvm_read_c0_guest_ebase(cop0));
  870. } else if (rd == MIPS_CP0_TLB_HI && sel == 0) {
  871. uint32_t nasid =
  872. vcpu->arch.gprs[rt] & ASID_MASK;
  873. if ((KSEGX(vcpu->arch.gprs[rt]) != CKSEG0) &&
  874. ((kvm_read_c0_guest_entryhi(cop0) &
  875. ASID_MASK) != nasid)) {
  876. kvm_debug("MTCz, change ASID from %#lx to %#lx\n",
  877. kvm_read_c0_guest_entryhi(cop0)
  878. & ASID_MASK,
  879. vcpu->arch.gprs[rt]
  880. & ASID_MASK);
  881. /* Blow away the shadow host TLBs */
  882. kvm_mips_flush_host_tlb(1);
  883. }
  884. kvm_write_c0_guest_entryhi(cop0,
  885. vcpu->arch.gprs[rt]);
  886. }
  887. /* Are we writing to COUNT */
  888. else if ((rd == MIPS_CP0_COUNT) && (sel == 0)) {
  889. kvm_mips_write_count(vcpu, vcpu->arch.gprs[rt]);
  890. goto done;
  891. } else if ((rd == MIPS_CP0_COMPARE) && (sel == 0)) {
  892. kvm_debug("[%#x] MTCz, COMPARE %#lx <- %#lx\n",
  893. pc, kvm_read_c0_guest_compare(cop0),
  894. vcpu->arch.gprs[rt]);
  895. /* If we are writing to COMPARE */
  896. /* Clear pending timer interrupt, if any */
  897. kvm_mips_callbacks->dequeue_timer_int(vcpu);
  898. kvm_mips_write_compare(vcpu,
  899. vcpu->arch.gprs[rt]);
  900. } else if ((rd == MIPS_CP0_STATUS) && (sel == 0)) {
  901. kvm_write_c0_guest_status(cop0,
  902. vcpu->arch.gprs[rt]);
  903. /*
  904. * Make sure that CU1 and NMI bits are
  905. * never set
  906. */
  907. kvm_clear_c0_guest_status(cop0,
  908. (ST0_CU1 | ST0_NMI));
  909. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  910. kvm_mips_trans_mtc0(inst, opc, vcpu);
  911. #endif
  912. } else if ((rd == MIPS_CP0_CAUSE) && (sel == 0)) {
  913. uint32_t old_cause, new_cause;
  914. old_cause = kvm_read_c0_guest_cause(cop0);
  915. new_cause = vcpu->arch.gprs[rt];
  916. /* Update R/W bits */
  917. kvm_change_c0_guest_cause(cop0, 0x08800300,
  918. new_cause);
  919. /* DC bit enabling/disabling timer? */
  920. if ((old_cause ^ new_cause) & CAUSEF_DC) {
  921. if (new_cause & CAUSEF_DC)
  922. kvm_mips_count_disable_cause(vcpu);
  923. else
  924. kvm_mips_count_enable_cause(vcpu);
  925. }
  926. } else {
  927. cop0->reg[rd][sel] = vcpu->arch.gprs[rt];
  928. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  929. kvm_mips_trans_mtc0(inst, opc, vcpu);
  930. #endif
  931. }
  932. kvm_debug("[%#x] MTCz, cop0->reg[%d][%d]: %#lx\n", pc,
  933. rd, sel, cop0->reg[rd][sel]);
  934. break;
  935. case dmtc_op:
  936. kvm_err("!!!!!!![%#lx]dmtc_op: rt: %d, rd: %d, sel: %d!!!!!!\n",
  937. vcpu->arch.pc, rt, rd, sel);
  938. er = EMULATE_FAIL;
  939. break;
  940. case mfmcz_op:
  941. #ifdef KVM_MIPS_DEBUG_COP0_COUNTERS
  942. cop0->stat[MIPS_CP0_STATUS][0]++;
  943. #endif
  944. if (rt != 0) {
  945. vcpu->arch.gprs[rt] =
  946. kvm_read_c0_guest_status(cop0);
  947. }
  948. /* EI */
  949. if (inst & 0x20) {
  950. kvm_debug("[%#lx] mfmcz_op: EI\n",
  951. vcpu->arch.pc);
  952. kvm_set_c0_guest_status(cop0, ST0_IE);
  953. } else {
  954. kvm_debug("[%#lx] mfmcz_op: DI\n",
  955. vcpu->arch.pc);
  956. kvm_clear_c0_guest_status(cop0, ST0_IE);
  957. }
  958. break;
  959. case wrpgpr_op:
  960. {
  961. uint32_t css =
  962. cop0->reg[MIPS_CP0_STATUS][2] & 0xf;
  963. uint32_t pss =
  964. (cop0->reg[MIPS_CP0_STATUS][2] >> 6) & 0xf;
  965. /*
  966. * We don't support any shadow register sets, so
  967. * SRSCtl[PSS] == SRSCtl[CSS] = 0
  968. */
  969. if (css || pss) {
  970. er = EMULATE_FAIL;
  971. break;
  972. }
  973. kvm_debug("WRPGPR[%d][%d] = %#lx\n", pss, rd,
  974. vcpu->arch.gprs[rt]);
  975. vcpu->arch.gprs[rd] = vcpu->arch.gprs[rt];
  976. }
  977. break;
  978. default:
  979. kvm_err("[%#lx]MachEmulateCP0: unsupported COP0, copz: 0x%x\n",
  980. vcpu->arch.pc, copz);
  981. er = EMULATE_FAIL;
  982. break;
  983. }
  984. }
  985. done:
  986. /* Rollback PC only if emulation was unsuccessful */
  987. if (er == EMULATE_FAIL)
  988. vcpu->arch.pc = curr_pc;
  989. dont_update_pc:
  990. /*
  991. * This is for special instructions whose emulation
  992. * updates the PC, so do not overwrite the PC under
  993. * any circumstances
  994. */
  995. return er;
  996. }
  997. enum emulation_result kvm_mips_emulate_store(uint32_t inst, uint32_t cause,
  998. struct kvm_run *run,
  999. struct kvm_vcpu *vcpu)
  1000. {
  1001. enum emulation_result er = EMULATE_DO_MMIO;
  1002. int32_t op, base, rt, offset;
  1003. uint32_t bytes;
  1004. void *data = run->mmio.data;
  1005. unsigned long curr_pc;
  1006. /*
  1007. * Update PC and hold onto current PC in case there is
  1008. * an error and we want to rollback the PC
  1009. */
  1010. curr_pc = vcpu->arch.pc;
  1011. er = update_pc(vcpu, cause);
  1012. if (er == EMULATE_FAIL)
  1013. return er;
  1014. rt = (inst >> 16) & 0x1f;
  1015. base = (inst >> 21) & 0x1f;
  1016. offset = inst & 0xffff;
  1017. op = (inst >> 26) & 0x3f;
  1018. switch (op) {
  1019. case sb_op:
  1020. bytes = 1;
  1021. if (bytes > sizeof(run->mmio.data)) {
  1022. kvm_err("%s: bad MMIO length: %d\n", __func__,
  1023. run->mmio.len);
  1024. }
  1025. run->mmio.phys_addr =
  1026. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  1027. host_cp0_badvaddr);
  1028. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  1029. er = EMULATE_FAIL;
  1030. break;
  1031. }
  1032. run->mmio.len = bytes;
  1033. run->mmio.is_write = 1;
  1034. vcpu->mmio_needed = 1;
  1035. vcpu->mmio_is_write = 1;
  1036. *(u8 *) data = vcpu->arch.gprs[rt];
  1037. kvm_debug("OP_SB: eaddr: %#lx, gpr: %#lx, data: %#x\n",
  1038. vcpu->arch.host_cp0_badvaddr, vcpu->arch.gprs[rt],
  1039. *(uint8_t *) data);
  1040. break;
  1041. case sw_op:
  1042. bytes = 4;
  1043. if (bytes > sizeof(run->mmio.data)) {
  1044. kvm_err("%s: bad MMIO length: %d\n", __func__,
  1045. run->mmio.len);
  1046. }
  1047. run->mmio.phys_addr =
  1048. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  1049. host_cp0_badvaddr);
  1050. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  1051. er = EMULATE_FAIL;
  1052. break;
  1053. }
  1054. run->mmio.len = bytes;
  1055. run->mmio.is_write = 1;
  1056. vcpu->mmio_needed = 1;
  1057. vcpu->mmio_is_write = 1;
  1058. *(uint32_t *) data = vcpu->arch.gprs[rt];
  1059. kvm_debug("[%#lx] OP_SW: eaddr: %#lx, gpr: %#lx, data: %#x\n",
  1060. vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
  1061. vcpu->arch.gprs[rt], *(uint32_t *) data);
  1062. break;
  1063. case sh_op:
  1064. bytes = 2;
  1065. if (bytes > sizeof(run->mmio.data)) {
  1066. kvm_err("%s: bad MMIO length: %d\n", __func__,
  1067. run->mmio.len);
  1068. }
  1069. run->mmio.phys_addr =
  1070. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  1071. host_cp0_badvaddr);
  1072. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  1073. er = EMULATE_FAIL;
  1074. break;
  1075. }
  1076. run->mmio.len = bytes;
  1077. run->mmio.is_write = 1;
  1078. vcpu->mmio_needed = 1;
  1079. vcpu->mmio_is_write = 1;
  1080. *(uint16_t *) data = vcpu->arch.gprs[rt];
  1081. kvm_debug("[%#lx] OP_SH: eaddr: %#lx, gpr: %#lx, data: %#x\n",
  1082. vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
  1083. vcpu->arch.gprs[rt], *(uint32_t *) data);
  1084. break;
  1085. default:
  1086. kvm_err("Store not yet supported");
  1087. er = EMULATE_FAIL;
  1088. break;
  1089. }
  1090. /* Rollback PC if emulation was unsuccessful */
  1091. if (er == EMULATE_FAIL)
  1092. vcpu->arch.pc = curr_pc;
  1093. return er;
  1094. }
  1095. enum emulation_result kvm_mips_emulate_load(uint32_t inst, uint32_t cause,
  1096. struct kvm_run *run,
  1097. struct kvm_vcpu *vcpu)
  1098. {
  1099. enum emulation_result er = EMULATE_DO_MMIO;
  1100. int32_t op, base, rt, offset;
  1101. uint32_t bytes;
  1102. rt = (inst >> 16) & 0x1f;
  1103. base = (inst >> 21) & 0x1f;
  1104. offset = inst & 0xffff;
  1105. op = (inst >> 26) & 0x3f;
  1106. vcpu->arch.pending_load_cause = cause;
  1107. vcpu->arch.io_gpr = rt;
  1108. switch (op) {
  1109. case lw_op:
  1110. bytes = 4;
  1111. if (bytes > sizeof(run->mmio.data)) {
  1112. kvm_err("%s: bad MMIO length: %d\n", __func__,
  1113. run->mmio.len);
  1114. er = EMULATE_FAIL;
  1115. break;
  1116. }
  1117. run->mmio.phys_addr =
  1118. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  1119. host_cp0_badvaddr);
  1120. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  1121. er = EMULATE_FAIL;
  1122. break;
  1123. }
  1124. run->mmio.len = bytes;
  1125. run->mmio.is_write = 0;
  1126. vcpu->mmio_needed = 1;
  1127. vcpu->mmio_is_write = 0;
  1128. break;
  1129. case lh_op:
  1130. case lhu_op:
  1131. bytes = 2;
  1132. if (bytes > sizeof(run->mmio.data)) {
  1133. kvm_err("%s: bad MMIO length: %d\n", __func__,
  1134. run->mmio.len);
  1135. er = EMULATE_FAIL;
  1136. break;
  1137. }
  1138. run->mmio.phys_addr =
  1139. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  1140. host_cp0_badvaddr);
  1141. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  1142. er = EMULATE_FAIL;
  1143. break;
  1144. }
  1145. run->mmio.len = bytes;
  1146. run->mmio.is_write = 0;
  1147. vcpu->mmio_needed = 1;
  1148. vcpu->mmio_is_write = 0;
  1149. if (op == lh_op)
  1150. vcpu->mmio_needed = 2;
  1151. else
  1152. vcpu->mmio_needed = 1;
  1153. break;
  1154. case lbu_op:
  1155. case lb_op:
  1156. bytes = 1;
  1157. if (bytes > sizeof(run->mmio.data)) {
  1158. kvm_err("%s: bad MMIO length: %d\n", __func__,
  1159. run->mmio.len);
  1160. er = EMULATE_FAIL;
  1161. break;
  1162. }
  1163. run->mmio.phys_addr =
  1164. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  1165. host_cp0_badvaddr);
  1166. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  1167. er = EMULATE_FAIL;
  1168. break;
  1169. }
  1170. run->mmio.len = bytes;
  1171. run->mmio.is_write = 0;
  1172. vcpu->mmio_is_write = 0;
  1173. if (op == lb_op)
  1174. vcpu->mmio_needed = 2;
  1175. else
  1176. vcpu->mmio_needed = 1;
  1177. break;
  1178. default:
  1179. kvm_err("Load not yet supported");
  1180. er = EMULATE_FAIL;
  1181. break;
  1182. }
  1183. return er;
  1184. }
  1185. int kvm_mips_sync_icache(unsigned long va, struct kvm_vcpu *vcpu)
  1186. {
  1187. unsigned long offset = (va & ~PAGE_MASK);
  1188. struct kvm *kvm = vcpu->kvm;
  1189. unsigned long pa;
  1190. gfn_t gfn;
  1191. pfn_t pfn;
  1192. gfn = va >> PAGE_SHIFT;
  1193. if (gfn >= kvm->arch.guest_pmap_npages) {
  1194. kvm_err("%s: Invalid gfn: %#llx\n", __func__, gfn);
  1195. kvm_mips_dump_host_tlbs();
  1196. kvm_arch_vcpu_dump_regs(vcpu);
  1197. return -1;
  1198. }
  1199. pfn = kvm->arch.guest_pmap[gfn];
  1200. pa = (pfn << PAGE_SHIFT) | offset;
  1201. kvm_debug("%s: va: %#lx, unmapped: %#x\n", __func__, va,
  1202. CKSEG0ADDR(pa));
  1203. local_flush_icache_range(CKSEG0ADDR(pa), 32);
  1204. return 0;
  1205. }
  1206. #define MIPS_CACHE_OP_INDEX_INV 0x0
  1207. #define MIPS_CACHE_OP_INDEX_LD_TAG 0x1
  1208. #define MIPS_CACHE_OP_INDEX_ST_TAG 0x2
  1209. #define MIPS_CACHE_OP_IMP 0x3
  1210. #define MIPS_CACHE_OP_HIT_INV 0x4
  1211. #define MIPS_CACHE_OP_FILL_WB_INV 0x5
  1212. #define MIPS_CACHE_OP_HIT_HB 0x6
  1213. #define MIPS_CACHE_OP_FETCH_LOCK 0x7
  1214. #define MIPS_CACHE_ICACHE 0x0
  1215. #define MIPS_CACHE_DCACHE 0x1
  1216. #define MIPS_CACHE_SEC 0x3
  1217. enum emulation_result kvm_mips_emulate_cache(uint32_t inst, uint32_t *opc,
  1218. uint32_t cause,
  1219. struct kvm_run *run,
  1220. struct kvm_vcpu *vcpu)
  1221. {
  1222. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1223. enum emulation_result er = EMULATE_DONE;
  1224. int32_t offset, cache, op_inst, op, base;
  1225. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1226. unsigned long va;
  1227. unsigned long curr_pc;
  1228. /*
  1229. * Update PC and hold onto current PC in case there is
  1230. * an error and we want to rollback the PC
  1231. */
  1232. curr_pc = vcpu->arch.pc;
  1233. er = update_pc(vcpu, cause);
  1234. if (er == EMULATE_FAIL)
  1235. return er;
  1236. base = (inst >> 21) & 0x1f;
  1237. op_inst = (inst >> 16) & 0x1f;
  1238. offset = inst & 0xffff;
  1239. cache = (inst >> 16) & 0x3;
  1240. op = (inst >> 18) & 0x7;
  1241. va = arch->gprs[base] + offset;
  1242. kvm_debug("CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
  1243. cache, op, base, arch->gprs[base], offset);
  1244. /*
  1245. * Treat INDEX_INV as a nop, basically issued by Linux on startup to
  1246. * invalidate the caches entirely by stepping through all the
  1247. * ways/indexes
  1248. */
  1249. if (op == MIPS_CACHE_OP_INDEX_INV) {
  1250. kvm_debug("@ %#lx/%#lx CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
  1251. vcpu->arch.pc, vcpu->arch.gprs[31], cache, op, base,
  1252. arch->gprs[base], offset);
  1253. if (cache == MIPS_CACHE_DCACHE)
  1254. r4k_blast_dcache();
  1255. else if (cache == MIPS_CACHE_ICACHE)
  1256. r4k_blast_icache();
  1257. else {
  1258. kvm_err("%s: unsupported CACHE INDEX operation\n",
  1259. __func__);
  1260. return EMULATE_FAIL;
  1261. }
  1262. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  1263. kvm_mips_trans_cache_index(inst, opc, vcpu);
  1264. #endif
  1265. goto done;
  1266. }
  1267. preempt_disable();
  1268. if (KVM_GUEST_KSEGX(va) == KVM_GUEST_KSEG0) {
  1269. if (kvm_mips_host_tlb_lookup(vcpu, va) < 0)
  1270. kvm_mips_handle_kseg0_tlb_fault(va, vcpu);
  1271. } else if ((KVM_GUEST_KSEGX(va) < KVM_GUEST_KSEG0) ||
  1272. KVM_GUEST_KSEGX(va) == KVM_GUEST_KSEG23) {
  1273. int index;
  1274. /* If an entry already exists then skip */
  1275. if (kvm_mips_host_tlb_lookup(vcpu, va) >= 0)
  1276. goto skip_fault;
  1277. /*
  1278. * If address not in the guest TLB, then give the guest a fault,
  1279. * the resulting handler will do the right thing
  1280. */
  1281. index = kvm_mips_guest_tlb_lookup(vcpu, (va & VPN2_MASK) |
  1282. (kvm_read_c0_guest_entryhi
  1283. (cop0) & ASID_MASK));
  1284. if (index < 0) {
  1285. vcpu->arch.host_cp0_entryhi = (va & VPN2_MASK);
  1286. vcpu->arch.host_cp0_badvaddr = va;
  1287. er = kvm_mips_emulate_tlbmiss_ld(cause, NULL, run,
  1288. vcpu);
  1289. preempt_enable();
  1290. goto dont_update_pc;
  1291. } else {
  1292. struct kvm_mips_tlb *tlb = &vcpu->arch.guest_tlb[index];
  1293. /*
  1294. * Check if the entry is valid, if not then setup a TLB
  1295. * invalid exception to the guest
  1296. */
  1297. if (!TLB_IS_VALID(*tlb, va)) {
  1298. er = kvm_mips_emulate_tlbinv_ld(cause, NULL,
  1299. run, vcpu);
  1300. preempt_enable();
  1301. goto dont_update_pc;
  1302. } else {
  1303. /*
  1304. * We fault an entry from the guest tlb to the
  1305. * shadow host TLB
  1306. */
  1307. kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb,
  1308. NULL,
  1309. NULL);
  1310. }
  1311. }
  1312. } else {
  1313. kvm_err("INVALID CACHE INDEX/ADDRESS (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
  1314. cache, op, base, arch->gprs[base], offset);
  1315. er = EMULATE_FAIL;
  1316. preempt_enable();
  1317. goto dont_update_pc;
  1318. }
  1319. skip_fault:
  1320. /* XXXKYMA: Only a subset of cache ops are supported, used by Linux */
  1321. if (cache == MIPS_CACHE_DCACHE
  1322. && (op == MIPS_CACHE_OP_FILL_WB_INV
  1323. || op == MIPS_CACHE_OP_HIT_INV)) {
  1324. flush_dcache_line(va);
  1325. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  1326. /*
  1327. * Replace the CACHE instruction, with a SYNCI, not the same,
  1328. * but avoids a trap
  1329. */
  1330. kvm_mips_trans_cache_va(inst, opc, vcpu);
  1331. #endif
  1332. } else if (op == MIPS_CACHE_OP_HIT_INV && cache == MIPS_CACHE_ICACHE) {
  1333. flush_dcache_line(va);
  1334. flush_icache_line(va);
  1335. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  1336. /* Replace the CACHE instruction, with a SYNCI */
  1337. kvm_mips_trans_cache_va(inst, opc, vcpu);
  1338. #endif
  1339. } else {
  1340. kvm_err("NO-OP CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
  1341. cache, op, base, arch->gprs[base], offset);
  1342. er = EMULATE_FAIL;
  1343. preempt_enable();
  1344. goto dont_update_pc;
  1345. }
  1346. preempt_enable();
  1347. dont_update_pc:
  1348. /* Rollback PC */
  1349. vcpu->arch.pc = curr_pc;
  1350. done:
  1351. return er;
  1352. }
  1353. enum emulation_result kvm_mips_emulate_inst(unsigned long cause, uint32_t *opc,
  1354. struct kvm_run *run,
  1355. struct kvm_vcpu *vcpu)
  1356. {
  1357. enum emulation_result er = EMULATE_DONE;
  1358. uint32_t inst;
  1359. /* Fetch the instruction. */
  1360. if (cause & CAUSEF_BD)
  1361. opc += 1;
  1362. inst = kvm_get_inst(opc, vcpu);
  1363. switch (((union mips_instruction)inst).r_format.opcode) {
  1364. case cop0_op:
  1365. er = kvm_mips_emulate_CP0(inst, opc, cause, run, vcpu);
  1366. break;
  1367. case sb_op:
  1368. case sh_op:
  1369. case sw_op:
  1370. er = kvm_mips_emulate_store(inst, cause, run, vcpu);
  1371. break;
  1372. case lb_op:
  1373. case lbu_op:
  1374. case lhu_op:
  1375. case lh_op:
  1376. case lw_op:
  1377. er = kvm_mips_emulate_load(inst, cause, run, vcpu);
  1378. break;
  1379. case cache_op:
  1380. ++vcpu->stat.cache_exits;
  1381. trace_kvm_exit(vcpu, CACHE_EXITS);
  1382. er = kvm_mips_emulate_cache(inst, opc, cause, run, vcpu);
  1383. break;
  1384. default:
  1385. kvm_err("Instruction emulation not supported (%p/%#x)\n", opc,
  1386. inst);
  1387. kvm_arch_vcpu_dump_regs(vcpu);
  1388. er = EMULATE_FAIL;
  1389. break;
  1390. }
  1391. return er;
  1392. }
  1393. enum emulation_result kvm_mips_emulate_syscall(unsigned long cause,
  1394. uint32_t *opc,
  1395. struct kvm_run *run,
  1396. struct kvm_vcpu *vcpu)
  1397. {
  1398. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1399. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1400. enum emulation_result er = EMULATE_DONE;
  1401. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1402. /* save old pc */
  1403. kvm_write_c0_guest_epc(cop0, arch->pc);
  1404. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1405. if (cause & CAUSEF_BD)
  1406. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1407. else
  1408. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1409. kvm_debug("Delivering SYSCALL @ pc %#lx\n", arch->pc);
  1410. kvm_change_c0_guest_cause(cop0, (0xff),
  1411. (T_SYSCALL << CAUSEB_EXCCODE));
  1412. /* Set PC to the exception entry point */
  1413. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1414. } else {
  1415. kvm_err("Trying to deliver SYSCALL when EXL is already set\n");
  1416. er = EMULATE_FAIL;
  1417. }
  1418. return er;
  1419. }
  1420. enum emulation_result kvm_mips_emulate_tlbmiss_ld(unsigned long cause,
  1421. uint32_t *opc,
  1422. struct kvm_run *run,
  1423. struct kvm_vcpu *vcpu)
  1424. {
  1425. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1426. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1427. unsigned long entryhi = (vcpu->arch. host_cp0_badvaddr & VPN2_MASK) |
  1428. (kvm_read_c0_guest_entryhi(cop0) & ASID_MASK);
  1429. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1430. /* save old pc */
  1431. kvm_write_c0_guest_epc(cop0, arch->pc);
  1432. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1433. if (cause & CAUSEF_BD)
  1434. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1435. else
  1436. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1437. kvm_debug("[EXL == 0] delivering TLB MISS @ pc %#lx\n",
  1438. arch->pc);
  1439. /* set pc to the exception entry point */
  1440. arch->pc = KVM_GUEST_KSEG0 + 0x0;
  1441. } else {
  1442. kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n",
  1443. arch->pc);
  1444. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1445. }
  1446. kvm_change_c0_guest_cause(cop0, (0xff),
  1447. (T_TLB_LD_MISS << CAUSEB_EXCCODE));
  1448. /* setup badvaddr, context and entryhi registers for the guest */
  1449. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1450. /* XXXKYMA: is the context register used by linux??? */
  1451. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1452. /* Blow away the shadow host TLBs */
  1453. kvm_mips_flush_host_tlb(1);
  1454. return EMULATE_DONE;
  1455. }
  1456. enum emulation_result kvm_mips_emulate_tlbinv_ld(unsigned long cause,
  1457. uint32_t *opc,
  1458. struct kvm_run *run,
  1459. struct kvm_vcpu *vcpu)
  1460. {
  1461. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1462. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1463. unsigned long entryhi =
  1464. (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
  1465. (kvm_read_c0_guest_entryhi(cop0) & ASID_MASK);
  1466. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1467. /* save old pc */
  1468. kvm_write_c0_guest_epc(cop0, arch->pc);
  1469. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1470. if (cause & CAUSEF_BD)
  1471. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1472. else
  1473. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1474. kvm_debug("[EXL == 0] delivering TLB INV @ pc %#lx\n",
  1475. arch->pc);
  1476. /* set pc to the exception entry point */
  1477. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1478. } else {
  1479. kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n",
  1480. arch->pc);
  1481. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1482. }
  1483. kvm_change_c0_guest_cause(cop0, (0xff),
  1484. (T_TLB_LD_MISS << CAUSEB_EXCCODE));
  1485. /* setup badvaddr, context and entryhi registers for the guest */
  1486. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1487. /* XXXKYMA: is the context register used by linux??? */
  1488. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1489. /* Blow away the shadow host TLBs */
  1490. kvm_mips_flush_host_tlb(1);
  1491. return EMULATE_DONE;
  1492. }
  1493. enum emulation_result kvm_mips_emulate_tlbmiss_st(unsigned long cause,
  1494. uint32_t *opc,
  1495. struct kvm_run *run,
  1496. struct kvm_vcpu *vcpu)
  1497. {
  1498. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1499. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1500. unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
  1501. (kvm_read_c0_guest_entryhi(cop0) & ASID_MASK);
  1502. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1503. /* save old pc */
  1504. kvm_write_c0_guest_epc(cop0, arch->pc);
  1505. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1506. if (cause & CAUSEF_BD)
  1507. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1508. else
  1509. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1510. kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n",
  1511. arch->pc);
  1512. /* Set PC to the exception entry point */
  1513. arch->pc = KVM_GUEST_KSEG0 + 0x0;
  1514. } else {
  1515. kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n",
  1516. arch->pc);
  1517. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1518. }
  1519. kvm_change_c0_guest_cause(cop0, (0xff),
  1520. (T_TLB_ST_MISS << CAUSEB_EXCCODE));
  1521. /* setup badvaddr, context and entryhi registers for the guest */
  1522. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1523. /* XXXKYMA: is the context register used by linux??? */
  1524. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1525. /* Blow away the shadow host TLBs */
  1526. kvm_mips_flush_host_tlb(1);
  1527. return EMULATE_DONE;
  1528. }
  1529. enum emulation_result kvm_mips_emulate_tlbinv_st(unsigned long cause,
  1530. uint32_t *opc,
  1531. struct kvm_run *run,
  1532. struct kvm_vcpu *vcpu)
  1533. {
  1534. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1535. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1536. unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
  1537. (kvm_read_c0_guest_entryhi(cop0) & ASID_MASK);
  1538. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1539. /* save old pc */
  1540. kvm_write_c0_guest_epc(cop0, arch->pc);
  1541. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1542. if (cause & CAUSEF_BD)
  1543. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1544. else
  1545. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1546. kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n",
  1547. arch->pc);
  1548. /* Set PC to the exception entry point */
  1549. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1550. } else {
  1551. kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n",
  1552. arch->pc);
  1553. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1554. }
  1555. kvm_change_c0_guest_cause(cop0, (0xff),
  1556. (T_TLB_ST_MISS << CAUSEB_EXCCODE));
  1557. /* setup badvaddr, context and entryhi registers for the guest */
  1558. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1559. /* XXXKYMA: is the context register used by linux??? */
  1560. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1561. /* Blow away the shadow host TLBs */
  1562. kvm_mips_flush_host_tlb(1);
  1563. return EMULATE_DONE;
  1564. }
  1565. /* TLBMOD: store into address matching TLB with Dirty bit off */
  1566. enum emulation_result kvm_mips_handle_tlbmod(unsigned long cause, uint32_t *opc,
  1567. struct kvm_run *run,
  1568. struct kvm_vcpu *vcpu)
  1569. {
  1570. enum emulation_result er = EMULATE_DONE;
  1571. #ifdef DEBUG
  1572. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1573. unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
  1574. (kvm_read_c0_guest_entryhi(cop0) & ASID_MASK);
  1575. int index;
  1576. /* If address not in the guest TLB, then we are in trouble */
  1577. index = kvm_mips_guest_tlb_lookup(vcpu, entryhi);
  1578. if (index < 0) {
  1579. /* XXXKYMA Invalidate and retry */
  1580. kvm_mips_host_tlb_inv(vcpu, vcpu->arch.host_cp0_badvaddr);
  1581. kvm_err("%s: host got TLBMOD for %#lx but entry not present in Guest TLB\n",
  1582. __func__, entryhi);
  1583. kvm_mips_dump_guest_tlbs(vcpu);
  1584. kvm_mips_dump_host_tlbs();
  1585. return EMULATE_FAIL;
  1586. }
  1587. #endif
  1588. er = kvm_mips_emulate_tlbmod(cause, opc, run, vcpu);
  1589. return er;
  1590. }
  1591. enum emulation_result kvm_mips_emulate_tlbmod(unsigned long cause,
  1592. uint32_t *opc,
  1593. struct kvm_run *run,
  1594. struct kvm_vcpu *vcpu)
  1595. {
  1596. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1597. unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
  1598. (kvm_read_c0_guest_entryhi(cop0) & ASID_MASK);
  1599. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1600. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1601. /* save old pc */
  1602. kvm_write_c0_guest_epc(cop0, arch->pc);
  1603. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1604. if (cause & CAUSEF_BD)
  1605. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1606. else
  1607. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1608. kvm_debug("[EXL == 0] Delivering TLB MOD @ pc %#lx\n",
  1609. arch->pc);
  1610. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1611. } else {
  1612. kvm_debug("[EXL == 1] Delivering TLB MOD @ pc %#lx\n",
  1613. arch->pc);
  1614. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1615. }
  1616. kvm_change_c0_guest_cause(cop0, (0xff), (T_TLB_MOD << CAUSEB_EXCCODE));
  1617. /* setup badvaddr, context and entryhi registers for the guest */
  1618. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1619. /* XXXKYMA: is the context register used by linux??? */
  1620. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1621. /* Blow away the shadow host TLBs */
  1622. kvm_mips_flush_host_tlb(1);
  1623. return EMULATE_DONE;
  1624. }
  1625. enum emulation_result kvm_mips_emulate_fpu_exc(unsigned long cause,
  1626. uint32_t *opc,
  1627. struct kvm_run *run,
  1628. struct kvm_vcpu *vcpu)
  1629. {
  1630. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1631. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1632. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1633. /* save old pc */
  1634. kvm_write_c0_guest_epc(cop0, arch->pc);
  1635. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1636. if (cause & CAUSEF_BD)
  1637. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1638. else
  1639. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1640. }
  1641. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1642. kvm_change_c0_guest_cause(cop0, (0xff),
  1643. (T_COP_UNUSABLE << CAUSEB_EXCCODE));
  1644. kvm_change_c0_guest_cause(cop0, (CAUSEF_CE), (0x1 << CAUSEB_CE));
  1645. return EMULATE_DONE;
  1646. }
  1647. enum emulation_result kvm_mips_emulate_ri_exc(unsigned long cause,
  1648. uint32_t *opc,
  1649. struct kvm_run *run,
  1650. struct kvm_vcpu *vcpu)
  1651. {
  1652. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1653. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1654. enum emulation_result er = EMULATE_DONE;
  1655. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1656. /* save old pc */
  1657. kvm_write_c0_guest_epc(cop0, arch->pc);
  1658. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1659. if (cause & CAUSEF_BD)
  1660. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1661. else
  1662. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1663. kvm_debug("Delivering RI @ pc %#lx\n", arch->pc);
  1664. kvm_change_c0_guest_cause(cop0, (0xff),
  1665. (T_RES_INST << CAUSEB_EXCCODE));
  1666. /* Set PC to the exception entry point */
  1667. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1668. } else {
  1669. kvm_err("Trying to deliver RI when EXL is already set\n");
  1670. er = EMULATE_FAIL;
  1671. }
  1672. return er;
  1673. }
  1674. enum emulation_result kvm_mips_emulate_bp_exc(unsigned long cause,
  1675. uint32_t *opc,
  1676. struct kvm_run *run,
  1677. struct kvm_vcpu *vcpu)
  1678. {
  1679. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1680. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1681. enum emulation_result er = EMULATE_DONE;
  1682. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1683. /* save old pc */
  1684. kvm_write_c0_guest_epc(cop0, arch->pc);
  1685. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1686. if (cause & CAUSEF_BD)
  1687. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1688. else
  1689. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1690. kvm_debug("Delivering BP @ pc %#lx\n", arch->pc);
  1691. kvm_change_c0_guest_cause(cop0, (0xff),
  1692. (T_BREAK << CAUSEB_EXCCODE));
  1693. /* Set PC to the exception entry point */
  1694. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1695. } else {
  1696. kvm_err("Trying to deliver BP when EXL is already set\n");
  1697. er = EMULATE_FAIL;
  1698. }
  1699. return er;
  1700. }
  1701. /* ll/sc, rdhwr, sync emulation */
  1702. #define OPCODE 0xfc000000
  1703. #define BASE 0x03e00000
  1704. #define RT 0x001f0000
  1705. #define OFFSET 0x0000ffff
  1706. #define LL 0xc0000000
  1707. #define SC 0xe0000000
  1708. #define SPEC0 0x00000000
  1709. #define SPEC3 0x7c000000
  1710. #define RD 0x0000f800
  1711. #define FUNC 0x0000003f
  1712. #define SYNC 0x0000000f
  1713. #define RDHWR 0x0000003b
  1714. enum emulation_result kvm_mips_handle_ri(unsigned long cause, uint32_t *opc,
  1715. struct kvm_run *run,
  1716. struct kvm_vcpu *vcpu)
  1717. {
  1718. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1719. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1720. enum emulation_result er = EMULATE_DONE;
  1721. unsigned long curr_pc;
  1722. uint32_t inst;
  1723. /*
  1724. * Update PC and hold onto current PC in case there is
  1725. * an error and we want to rollback the PC
  1726. */
  1727. curr_pc = vcpu->arch.pc;
  1728. er = update_pc(vcpu, cause);
  1729. if (er == EMULATE_FAIL)
  1730. return er;
  1731. /* Fetch the instruction. */
  1732. if (cause & CAUSEF_BD)
  1733. opc += 1;
  1734. inst = kvm_get_inst(opc, vcpu);
  1735. if (inst == KVM_INVALID_INST) {
  1736. kvm_err("%s: Cannot get inst @ %p\n", __func__, opc);
  1737. return EMULATE_FAIL;
  1738. }
  1739. if ((inst & OPCODE) == SPEC3 && (inst & FUNC) == RDHWR) {
  1740. int usermode = !KVM_GUEST_KERNEL_MODE(vcpu);
  1741. int rd = (inst & RD) >> 11;
  1742. int rt = (inst & RT) >> 16;
  1743. /* If usermode, check RDHWR rd is allowed by guest HWREna */
  1744. if (usermode && !(kvm_read_c0_guest_hwrena(cop0) & BIT(rd))) {
  1745. kvm_debug("RDHWR %#x disallowed by HWREna @ %p\n",
  1746. rd, opc);
  1747. goto emulate_ri;
  1748. }
  1749. switch (rd) {
  1750. case 0: /* CPU number */
  1751. arch->gprs[rt] = 0;
  1752. break;
  1753. case 1: /* SYNCI length */
  1754. arch->gprs[rt] = min(current_cpu_data.dcache.linesz,
  1755. current_cpu_data.icache.linesz);
  1756. break;
  1757. case 2: /* Read count register */
  1758. arch->gprs[rt] = kvm_mips_read_count(vcpu);
  1759. break;
  1760. case 3: /* Count register resolution */
  1761. switch (current_cpu_data.cputype) {
  1762. case CPU_20KC:
  1763. case CPU_25KF:
  1764. arch->gprs[rt] = 1;
  1765. break;
  1766. default:
  1767. arch->gprs[rt] = 2;
  1768. }
  1769. break;
  1770. case 29:
  1771. arch->gprs[rt] = kvm_read_c0_guest_userlocal(cop0);
  1772. break;
  1773. default:
  1774. kvm_debug("RDHWR %#x not supported @ %p\n", rd, opc);
  1775. goto emulate_ri;
  1776. }
  1777. } else {
  1778. kvm_debug("Emulate RI not supported @ %p: %#x\n", opc, inst);
  1779. goto emulate_ri;
  1780. }
  1781. return EMULATE_DONE;
  1782. emulate_ri:
  1783. /*
  1784. * Rollback PC (if in branch delay slot then the PC already points to
  1785. * branch target), and pass the RI exception to the guest OS.
  1786. */
  1787. vcpu->arch.pc = curr_pc;
  1788. return kvm_mips_emulate_ri_exc(cause, opc, run, vcpu);
  1789. }
  1790. enum emulation_result kvm_mips_complete_mmio_load(struct kvm_vcpu *vcpu,
  1791. struct kvm_run *run)
  1792. {
  1793. unsigned long *gpr = &vcpu->arch.gprs[vcpu->arch.io_gpr];
  1794. enum emulation_result er = EMULATE_DONE;
  1795. unsigned long curr_pc;
  1796. if (run->mmio.len > sizeof(*gpr)) {
  1797. kvm_err("Bad MMIO length: %d", run->mmio.len);
  1798. er = EMULATE_FAIL;
  1799. goto done;
  1800. }
  1801. /*
  1802. * Update PC and hold onto current PC in case there is
  1803. * an error and we want to rollback the PC
  1804. */
  1805. curr_pc = vcpu->arch.pc;
  1806. er = update_pc(vcpu, vcpu->arch.pending_load_cause);
  1807. if (er == EMULATE_FAIL)
  1808. return er;
  1809. switch (run->mmio.len) {
  1810. case 4:
  1811. *gpr = *(int32_t *) run->mmio.data;
  1812. break;
  1813. case 2:
  1814. if (vcpu->mmio_needed == 2)
  1815. *gpr = *(int16_t *) run->mmio.data;
  1816. else
  1817. *gpr = *(int16_t *) run->mmio.data;
  1818. break;
  1819. case 1:
  1820. if (vcpu->mmio_needed == 2)
  1821. *gpr = *(int8_t *) run->mmio.data;
  1822. else
  1823. *gpr = *(u8 *) run->mmio.data;
  1824. break;
  1825. }
  1826. if (vcpu->arch.pending_load_cause & CAUSEF_BD)
  1827. kvm_debug("[%#lx] Completing %d byte BD Load to gpr %d (0x%08lx) type %d\n",
  1828. vcpu->arch.pc, run->mmio.len, vcpu->arch.io_gpr, *gpr,
  1829. vcpu->mmio_needed);
  1830. done:
  1831. return er;
  1832. }
  1833. static enum emulation_result kvm_mips_emulate_exc(unsigned long cause,
  1834. uint32_t *opc,
  1835. struct kvm_run *run,
  1836. struct kvm_vcpu *vcpu)
  1837. {
  1838. uint32_t exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
  1839. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1840. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1841. enum emulation_result er = EMULATE_DONE;
  1842. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1843. /* save old pc */
  1844. kvm_write_c0_guest_epc(cop0, arch->pc);
  1845. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1846. if (cause & CAUSEF_BD)
  1847. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1848. else
  1849. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1850. kvm_change_c0_guest_cause(cop0, (0xff),
  1851. (exccode << CAUSEB_EXCCODE));
  1852. /* Set PC to the exception entry point */
  1853. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1854. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1855. kvm_debug("Delivering EXC %d @ pc %#lx, badVaddr: %#lx\n",
  1856. exccode, kvm_read_c0_guest_epc(cop0),
  1857. kvm_read_c0_guest_badvaddr(cop0));
  1858. } else {
  1859. kvm_err("Trying to deliver EXC when EXL is already set\n");
  1860. er = EMULATE_FAIL;
  1861. }
  1862. return er;
  1863. }
  1864. enum emulation_result kvm_mips_check_privilege(unsigned long cause,
  1865. uint32_t *opc,
  1866. struct kvm_run *run,
  1867. struct kvm_vcpu *vcpu)
  1868. {
  1869. enum emulation_result er = EMULATE_DONE;
  1870. uint32_t exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
  1871. unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
  1872. int usermode = !KVM_GUEST_KERNEL_MODE(vcpu);
  1873. if (usermode) {
  1874. switch (exccode) {
  1875. case T_INT:
  1876. case T_SYSCALL:
  1877. case T_BREAK:
  1878. case T_RES_INST:
  1879. break;
  1880. case T_COP_UNUSABLE:
  1881. if (((cause & CAUSEF_CE) >> CAUSEB_CE) == 0)
  1882. er = EMULATE_PRIV_FAIL;
  1883. break;
  1884. case T_TLB_MOD:
  1885. break;
  1886. case T_TLB_LD_MISS:
  1887. /*
  1888. * We we are accessing Guest kernel space, then send an
  1889. * address error exception to the guest
  1890. */
  1891. if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
  1892. kvm_debug("%s: LD MISS @ %#lx\n", __func__,
  1893. badvaddr);
  1894. cause &= ~0xff;
  1895. cause |= (T_ADDR_ERR_LD << CAUSEB_EXCCODE);
  1896. er = EMULATE_PRIV_FAIL;
  1897. }
  1898. break;
  1899. case T_TLB_ST_MISS:
  1900. /*
  1901. * We we are accessing Guest kernel space, then send an
  1902. * address error exception to the guest
  1903. */
  1904. if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
  1905. kvm_debug("%s: ST MISS @ %#lx\n", __func__,
  1906. badvaddr);
  1907. cause &= ~0xff;
  1908. cause |= (T_ADDR_ERR_ST << CAUSEB_EXCCODE);
  1909. er = EMULATE_PRIV_FAIL;
  1910. }
  1911. break;
  1912. case T_ADDR_ERR_ST:
  1913. kvm_debug("%s: address error ST @ %#lx\n", __func__,
  1914. badvaddr);
  1915. if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
  1916. cause &= ~0xff;
  1917. cause |= (T_TLB_ST_MISS << CAUSEB_EXCCODE);
  1918. }
  1919. er = EMULATE_PRIV_FAIL;
  1920. break;
  1921. case T_ADDR_ERR_LD:
  1922. kvm_debug("%s: address error LD @ %#lx\n", __func__,
  1923. badvaddr);
  1924. if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
  1925. cause &= ~0xff;
  1926. cause |= (T_TLB_LD_MISS << CAUSEB_EXCCODE);
  1927. }
  1928. er = EMULATE_PRIV_FAIL;
  1929. break;
  1930. default:
  1931. er = EMULATE_PRIV_FAIL;
  1932. break;
  1933. }
  1934. }
  1935. if (er == EMULATE_PRIV_FAIL)
  1936. kvm_mips_emulate_exc(cause, opc, run, vcpu);
  1937. return er;
  1938. }
  1939. /*
  1940. * User Address (UA) fault, this could happen if
  1941. * (1) TLB entry not present/valid in both Guest and shadow host TLBs, in this
  1942. * case we pass on the fault to the guest kernel and let it handle it.
  1943. * (2) TLB entry is present in the Guest TLB but not in the shadow, in this
  1944. * case we inject the TLB from the Guest TLB into the shadow host TLB
  1945. */
  1946. enum emulation_result kvm_mips_handle_tlbmiss(unsigned long cause,
  1947. uint32_t *opc,
  1948. struct kvm_run *run,
  1949. struct kvm_vcpu *vcpu)
  1950. {
  1951. enum emulation_result er = EMULATE_DONE;
  1952. uint32_t exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
  1953. unsigned long va = vcpu->arch.host_cp0_badvaddr;
  1954. int index;
  1955. kvm_debug("kvm_mips_handle_tlbmiss: badvaddr: %#lx, entryhi: %#lx\n",
  1956. vcpu->arch.host_cp0_badvaddr, vcpu->arch.host_cp0_entryhi);
  1957. /*
  1958. * KVM would not have got the exception if this entry was valid in the
  1959. * shadow host TLB. Check the Guest TLB, if the entry is not there then
  1960. * send the guest an exception. The guest exc handler should then inject
  1961. * an entry into the guest TLB.
  1962. */
  1963. index = kvm_mips_guest_tlb_lookup(vcpu,
  1964. (va & VPN2_MASK) |
  1965. (kvm_read_c0_guest_entryhi
  1966. (vcpu->arch.cop0) & ASID_MASK));
  1967. if (index < 0) {
  1968. if (exccode == T_TLB_LD_MISS) {
  1969. er = kvm_mips_emulate_tlbmiss_ld(cause, opc, run, vcpu);
  1970. } else if (exccode == T_TLB_ST_MISS) {
  1971. er = kvm_mips_emulate_tlbmiss_st(cause, opc, run, vcpu);
  1972. } else {
  1973. kvm_err("%s: invalid exc code: %d\n", __func__,
  1974. exccode);
  1975. er = EMULATE_FAIL;
  1976. }
  1977. } else {
  1978. struct kvm_mips_tlb *tlb = &vcpu->arch.guest_tlb[index];
  1979. /*
  1980. * Check if the entry is valid, if not then setup a TLB invalid
  1981. * exception to the guest
  1982. */
  1983. if (!TLB_IS_VALID(*tlb, va)) {
  1984. if (exccode == T_TLB_LD_MISS) {
  1985. er = kvm_mips_emulate_tlbinv_ld(cause, opc, run,
  1986. vcpu);
  1987. } else if (exccode == T_TLB_ST_MISS) {
  1988. er = kvm_mips_emulate_tlbinv_st(cause, opc, run,
  1989. vcpu);
  1990. } else {
  1991. kvm_err("%s: invalid exc code: %d\n", __func__,
  1992. exccode);
  1993. er = EMULATE_FAIL;
  1994. }
  1995. } else {
  1996. kvm_debug("Injecting hi: %#lx, lo0: %#lx, lo1: %#lx into shadow host TLB\n",
  1997. tlb->tlb_hi, tlb->tlb_lo0, tlb->tlb_lo1);
  1998. /*
  1999. * OK we have a Guest TLB entry, now inject it into the
  2000. * shadow host TLB
  2001. */
  2002. kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb, NULL,
  2003. NULL);
  2004. }
  2005. }
  2006. return er;
  2007. }