emulate.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684
  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/cacheops.h>
  23. #include <asm/cpu-info.h>
  24. #include <asm/mmu_context.h>
  25. #include <asm/tlbflush.h>
  26. #include <asm/inst.h>
  27. #undef CONFIG_MIPS_MT
  28. #include <asm/r4kcache.h>
  29. #define CONFIG_MIPS_MT
  30. #include "interrupt.h"
  31. #include "commpage.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((u32 *) 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: /* POP06 */
  146. #ifndef CONFIG_CPU_MIPSR6
  147. case blezl_op: /* removed in R6 */
  148. #endif
  149. if (insn.i_format.rt != 0)
  150. goto compact_branch;
  151. if ((long)arch->gprs[insn.i_format.rs] <= 0)
  152. epc = epc + 4 + (insn.i_format.simmediate << 2);
  153. else
  154. epc += 8;
  155. nextpc = epc;
  156. break;
  157. case bgtz_op: /* POP07 */
  158. #ifndef CONFIG_CPU_MIPSR6
  159. case bgtzl_op: /* removed in R6 */
  160. #endif
  161. if (insn.i_format.rt != 0)
  162. goto compact_branch;
  163. if ((long)arch->gprs[insn.i_format.rs] > 0)
  164. epc = epc + 4 + (insn.i_format.simmediate << 2);
  165. else
  166. epc += 8;
  167. nextpc = epc;
  168. break;
  169. /* And now the FPA/cp1 branch instructions. */
  170. case cop1_op:
  171. kvm_err("%s: unsupported cop1_op\n", __func__);
  172. break;
  173. #ifdef CONFIG_CPU_MIPSR6
  174. /* R6 added the following compact branches with forbidden slots */
  175. case blezl_op: /* POP26 */
  176. case bgtzl_op: /* POP27 */
  177. /* only rt == 0 isn't compact branch */
  178. if (insn.i_format.rt != 0)
  179. goto compact_branch;
  180. break;
  181. case pop10_op:
  182. case pop30_op:
  183. /* only rs == rt == 0 is reserved, rest are compact branches */
  184. if (insn.i_format.rs != 0 || insn.i_format.rt != 0)
  185. goto compact_branch;
  186. break;
  187. case pop66_op:
  188. case pop76_op:
  189. /* only rs == 0 isn't compact branch */
  190. if (insn.i_format.rs != 0)
  191. goto compact_branch;
  192. break;
  193. compact_branch:
  194. /*
  195. * If we've hit an exception on the forbidden slot, then
  196. * the branch must not have been taken.
  197. */
  198. epc += 8;
  199. nextpc = epc;
  200. break;
  201. #else
  202. compact_branch:
  203. /* Compact branches not supported before R6 */
  204. break;
  205. #endif
  206. }
  207. return nextpc;
  208. unaligned:
  209. kvm_err("%s: unaligned epc\n", __func__);
  210. return nextpc;
  211. sigill:
  212. kvm_err("%s: DSP branch but not DSP ASE\n", __func__);
  213. return nextpc;
  214. }
  215. enum emulation_result update_pc(struct kvm_vcpu *vcpu, u32 cause)
  216. {
  217. unsigned long branch_pc;
  218. enum emulation_result er = EMULATE_DONE;
  219. if (cause & CAUSEF_BD) {
  220. branch_pc = kvm_compute_return_epc(vcpu, vcpu->arch.pc);
  221. if (branch_pc == KVM_INVALID_INST) {
  222. er = EMULATE_FAIL;
  223. } else {
  224. vcpu->arch.pc = branch_pc;
  225. kvm_debug("BD update_pc(): New PC: %#lx\n",
  226. vcpu->arch.pc);
  227. }
  228. } else
  229. vcpu->arch.pc += 4;
  230. kvm_debug("update_pc(): New PC: %#lx\n", vcpu->arch.pc);
  231. return er;
  232. }
  233. /**
  234. * kvm_mips_count_disabled() - Find whether the CP0_Count timer is disabled.
  235. * @vcpu: Virtual CPU.
  236. *
  237. * Returns: 1 if the CP0_Count timer is disabled by either the guest
  238. * CP0_Cause.DC bit or the count_ctl.DC bit.
  239. * 0 otherwise (in which case CP0_Count timer is running).
  240. */
  241. static inline int kvm_mips_count_disabled(struct kvm_vcpu *vcpu)
  242. {
  243. struct mips_coproc *cop0 = vcpu->arch.cop0;
  244. return (vcpu->arch.count_ctl & KVM_REG_MIPS_COUNT_CTL_DC) ||
  245. (kvm_read_c0_guest_cause(cop0) & CAUSEF_DC);
  246. }
  247. /**
  248. * kvm_mips_ktime_to_count() - Scale ktime_t to a 32-bit count.
  249. *
  250. * Caches the dynamic nanosecond bias in vcpu->arch.count_dyn_bias.
  251. *
  252. * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
  253. */
  254. static u32 kvm_mips_ktime_to_count(struct kvm_vcpu *vcpu, ktime_t now)
  255. {
  256. s64 now_ns, periods;
  257. u64 delta;
  258. now_ns = ktime_to_ns(now);
  259. delta = now_ns + vcpu->arch.count_dyn_bias;
  260. if (delta >= vcpu->arch.count_period) {
  261. /* If delta is out of safe range the bias needs adjusting */
  262. periods = div64_s64(now_ns, vcpu->arch.count_period);
  263. vcpu->arch.count_dyn_bias = -periods * vcpu->arch.count_period;
  264. /* Recalculate delta with new bias */
  265. delta = now_ns + vcpu->arch.count_dyn_bias;
  266. }
  267. /*
  268. * We've ensured that:
  269. * delta < count_period
  270. *
  271. * Therefore the intermediate delta*count_hz will never overflow since
  272. * at the boundary condition:
  273. * delta = count_period
  274. * delta = NSEC_PER_SEC * 2^32 / count_hz
  275. * delta * count_hz = NSEC_PER_SEC * 2^32
  276. */
  277. return div_u64(delta * vcpu->arch.count_hz, NSEC_PER_SEC);
  278. }
  279. /**
  280. * kvm_mips_count_time() - Get effective current time.
  281. * @vcpu: Virtual CPU.
  282. *
  283. * Get effective monotonic ktime. This is usually a straightforward ktime_get(),
  284. * except when the master disable bit is set in count_ctl, in which case it is
  285. * count_resume, i.e. the time that the count was disabled.
  286. *
  287. * Returns: Effective monotonic ktime for CP0_Count.
  288. */
  289. static inline ktime_t kvm_mips_count_time(struct kvm_vcpu *vcpu)
  290. {
  291. if (unlikely(vcpu->arch.count_ctl & KVM_REG_MIPS_COUNT_CTL_DC))
  292. return vcpu->arch.count_resume;
  293. return ktime_get();
  294. }
  295. /**
  296. * kvm_mips_read_count_running() - Read the current count value as if running.
  297. * @vcpu: Virtual CPU.
  298. * @now: Kernel time to read CP0_Count at.
  299. *
  300. * Returns the current guest CP0_Count register at time @now and handles if the
  301. * timer interrupt is pending and hasn't been handled yet.
  302. *
  303. * Returns: The current value of the guest CP0_Count register.
  304. */
  305. static u32 kvm_mips_read_count_running(struct kvm_vcpu *vcpu, ktime_t now)
  306. {
  307. struct mips_coproc *cop0 = vcpu->arch.cop0;
  308. ktime_t expires, threshold;
  309. u32 count, compare;
  310. int running;
  311. /* Calculate the biased and scaled guest CP0_Count */
  312. count = vcpu->arch.count_bias + kvm_mips_ktime_to_count(vcpu, now);
  313. compare = kvm_read_c0_guest_compare(cop0);
  314. /*
  315. * Find whether CP0_Count has reached the closest timer interrupt. If
  316. * not, we shouldn't inject it.
  317. */
  318. if ((s32)(count - compare) < 0)
  319. return count;
  320. /*
  321. * The CP0_Count we're going to return has already reached the closest
  322. * timer interrupt. Quickly check if it really is a new interrupt by
  323. * looking at whether the interval until the hrtimer expiry time is
  324. * less than 1/4 of the timer period.
  325. */
  326. expires = hrtimer_get_expires(&vcpu->arch.comparecount_timer);
  327. threshold = ktime_add_ns(now, vcpu->arch.count_period / 4);
  328. if (ktime_before(expires, threshold)) {
  329. /*
  330. * Cancel it while we handle it so there's no chance of
  331. * interference with the timeout handler.
  332. */
  333. running = hrtimer_cancel(&vcpu->arch.comparecount_timer);
  334. /* Nothing should be waiting on the timeout */
  335. kvm_mips_callbacks->queue_timer_int(vcpu);
  336. /*
  337. * Restart the timer if it was running based on the expiry time
  338. * we read, so that we don't push it back 2 periods.
  339. */
  340. if (running) {
  341. expires = ktime_add_ns(expires,
  342. vcpu->arch.count_period);
  343. hrtimer_start(&vcpu->arch.comparecount_timer, expires,
  344. HRTIMER_MODE_ABS);
  345. }
  346. }
  347. return count;
  348. }
  349. /**
  350. * kvm_mips_read_count() - Read the current count value.
  351. * @vcpu: Virtual CPU.
  352. *
  353. * Read the current guest CP0_Count value, taking into account whether the timer
  354. * is stopped.
  355. *
  356. * Returns: The current guest CP0_Count value.
  357. */
  358. u32 kvm_mips_read_count(struct kvm_vcpu *vcpu)
  359. {
  360. struct mips_coproc *cop0 = vcpu->arch.cop0;
  361. /* If count disabled just read static copy of count */
  362. if (kvm_mips_count_disabled(vcpu))
  363. return kvm_read_c0_guest_count(cop0);
  364. return kvm_mips_read_count_running(vcpu, ktime_get());
  365. }
  366. /**
  367. * kvm_mips_freeze_hrtimer() - Safely stop the hrtimer.
  368. * @vcpu: Virtual CPU.
  369. * @count: Output pointer for CP0_Count value at point of freeze.
  370. *
  371. * Freeze the hrtimer safely and return both the ktime and the CP0_Count value
  372. * at the point it was frozen. It is guaranteed that any pending interrupts at
  373. * the point it was frozen are handled, and none after that point.
  374. *
  375. * This is useful where the time/CP0_Count is needed in the calculation of the
  376. * new parameters.
  377. *
  378. * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
  379. *
  380. * Returns: The ktime at the point of freeze.
  381. */
  382. static ktime_t kvm_mips_freeze_hrtimer(struct kvm_vcpu *vcpu, u32 *count)
  383. {
  384. ktime_t now;
  385. /* stop hrtimer before finding time */
  386. hrtimer_cancel(&vcpu->arch.comparecount_timer);
  387. now = ktime_get();
  388. /* find count at this point and handle pending hrtimer */
  389. *count = kvm_mips_read_count_running(vcpu, now);
  390. return now;
  391. }
  392. /**
  393. * kvm_mips_resume_hrtimer() - Resume hrtimer, updating expiry.
  394. * @vcpu: Virtual CPU.
  395. * @now: ktime at point of resume.
  396. * @count: CP0_Count at point of resume.
  397. *
  398. * Resumes the timer and updates the timer expiry based on @now and @count.
  399. * This can be used in conjunction with kvm_mips_freeze_timer() when timer
  400. * parameters need to be changed.
  401. *
  402. * It is guaranteed that a timer interrupt immediately after resume will be
  403. * handled, but not if CP_Compare is exactly at @count. That case is already
  404. * handled by kvm_mips_freeze_timer().
  405. *
  406. * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
  407. */
  408. static void kvm_mips_resume_hrtimer(struct kvm_vcpu *vcpu,
  409. ktime_t now, u32 count)
  410. {
  411. struct mips_coproc *cop0 = vcpu->arch.cop0;
  412. u32 compare;
  413. u64 delta;
  414. ktime_t expire;
  415. /* Calculate timeout (wrap 0 to 2^32) */
  416. compare = kvm_read_c0_guest_compare(cop0);
  417. delta = (u64)(u32)(compare - count - 1) + 1;
  418. delta = div_u64(delta * NSEC_PER_SEC, vcpu->arch.count_hz);
  419. expire = ktime_add_ns(now, delta);
  420. /* Update hrtimer to use new timeout */
  421. hrtimer_cancel(&vcpu->arch.comparecount_timer);
  422. hrtimer_start(&vcpu->arch.comparecount_timer, expire, HRTIMER_MODE_ABS);
  423. }
  424. /**
  425. * kvm_mips_write_count() - Modify the count and update timer.
  426. * @vcpu: Virtual CPU.
  427. * @count: Guest CP0_Count value to set.
  428. *
  429. * Sets the CP0_Count value and updates the timer accordingly.
  430. */
  431. void kvm_mips_write_count(struct kvm_vcpu *vcpu, u32 count)
  432. {
  433. struct mips_coproc *cop0 = vcpu->arch.cop0;
  434. ktime_t now;
  435. /* Calculate bias */
  436. now = kvm_mips_count_time(vcpu);
  437. vcpu->arch.count_bias = count - kvm_mips_ktime_to_count(vcpu, now);
  438. if (kvm_mips_count_disabled(vcpu))
  439. /* The timer's disabled, adjust the static count */
  440. kvm_write_c0_guest_count(cop0, count);
  441. else
  442. /* Update timeout */
  443. kvm_mips_resume_hrtimer(vcpu, now, count);
  444. }
  445. /**
  446. * kvm_mips_init_count() - Initialise timer.
  447. * @vcpu: Virtual CPU.
  448. *
  449. * Initialise the timer to a sensible frequency, namely 100MHz, zero it, and set
  450. * it going if it's enabled.
  451. */
  452. void kvm_mips_init_count(struct kvm_vcpu *vcpu)
  453. {
  454. /* 100 MHz */
  455. vcpu->arch.count_hz = 100*1000*1000;
  456. vcpu->arch.count_period = div_u64((u64)NSEC_PER_SEC << 32,
  457. vcpu->arch.count_hz);
  458. vcpu->arch.count_dyn_bias = 0;
  459. /* Starting at 0 */
  460. kvm_mips_write_count(vcpu, 0);
  461. }
  462. /**
  463. * kvm_mips_set_count_hz() - Update the frequency of the timer.
  464. * @vcpu: Virtual CPU.
  465. * @count_hz: Frequency of CP0_Count timer in Hz.
  466. *
  467. * Change the frequency of the CP0_Count timer. This is done atomically so that
  468. * CP0_Count is continuous and no timer interrupt is lost.
  469. *
  470. * Returns: -EINVAL if @count_hz is out of range.
  471. * 0 on success.
  472. */
  473. int kvm_mips_set_count_hz(struct kvm_vcpu *vcpu, s64 count_hz)
  474. {
  475. struct mips_coproc *cop0 = vcpu->arch.cop0;
  476. int dc;
  477. ktime_t now;
  478. u32 count;
  479. /* ensure the frequency is in a sensible range... */
  480. if (count_hz <= 0 || count_hz > NSEC_PER_SEC)
  481. return -EINVAL;
  482. /* ... and has actually changed */
  483. if (vcpu->arch.count_hz == count_hz)
  484. return 0;
  485. /* Safely freeze timer so we can keep it continuous */
  486. dc = kvm_mips_count_disabled(vcpu);
  487. if (dc) {
  488. now = kvm_mips_count_time(vcpu);
  489. count = kvm_read_c0_guest_count(cop0);
  490. } else {
  491. now = kvm_mips_freeze_hrtimer(vcpu, &count);
  492. }
  493. /* Update the frequency */
  494. vcpu->arch.count_hz = count_hz;
  495. vcpu->arch.count_period = div_u64((u64)NSEC_PER_SEC << 32, count_hz);
  496. vcpu->arch.count_dyn_bias = 0;
  497. /* Calculate adjusted bias so dynamic count is unchanged */
  498. vcpu->arch.count_bias = count - kvm_mips_ktime_to_count(vcpu, now);
  499. /* Update and resume hrtimer */
  500. if (!dc)
  501. kvm_mips_resume_hrtimer(vcpu, now, count);
  502. return 0;
  503. }
  504. /**
  505. * kvm_mips_write_compare() - Modify compare and update timer.
  506. * @vcpu: Virtual CPU.
  507. * @compare: New CP0_Compare value.
  508. * @ack: Whether to acknowledge timer interrupt.
  509. *
  510. * Update CP0_Compare to a new value and update the timeout.
  511. * If @ack, atomically acknowledge any pending timer interrupt, otherwise ensure
  512. * any pending timer interrupt is preserved.
  513. */
  514. void kvm_mips_write_compare(struct kvm_vcpu *vcpu, u32 compare, bool ack)
  515. {
  516. struct mips_coproc *cop0 = vcpu->arch.cop0;
  517. int dc;
  518. u32 old_compare = kvm_read_c0_guest_compare(cop0);
  519. ktime_t now;
  520. u32 count;
  521. /* if unchanged, must just be an ack */
  522. if (old_compare == compare) {
  523. if (!ack)
  524. return;
  525. kvm_mips_callbacks->dequeue_timer_int(vcpu);
  526. kvm_write_c0_guest_compare(cop0, compare);
  527. return;
  528. }
  529. /* freeze_hrtimer() takes care of timer interrupts <= count */
  530. dc = kvm_mips_count_disabled(vcpu);
  531. if (!dc)
  532. now = kvm_mips_freeze_hrtimer(vcpu, &count);
  533. if (ack)
  534. kvm_mips_callbacks->dequeue_timer_int(vcpu);
  535. kvm_write_c0_guest_compare(cop0, compare);
  536. /* resume_hrtimer() takes care of timer interrupts > count */
  537. if (!dc)
  538. kvm_mips_resume_hrtimer(vcpu, now, count);
  539. }
  540. /**
  541. * kvm_mips_count_disable() - Disable count.
  542. * @vcpu: Virtual CPU.
  543. *
  544. * Disable the CP0_Count timer. A timer interrupt on or before the final stop
  545. * time will be handled but not after.
  546. *
  547. * Assumes CP0_Count was previously enabled but now Guest.CP0_Cause.DC or
  548. * count_ctl.DC has been set (count disabled).
  549. *
  550. * Returns: The time that the timer was stopped.
  551. */
  552. static ktime_t kvm_mips_count_disable(struct kvm_vcpu *vcpu)
  553. {
  554. struct mips_coproc *cop0 = vcpu->arch.cop0;
  555. u32 count;
  556. ktime_t now;
  557. /* Stop hrtimer */
  558. hrtimer_cancel(&vcpu->arch.comparecount_timer);
  559. /* Set the static count from the dynamic count, handling pending TI */
  560. now = ktime_get();
  561. count = kvm_mips_read_count_running(vcpu, now);
  562. kvm_write_c0_guest_count(cop0, count);
  563. return now;
  564. }
  565. /**
  566. * kvm_mips_count_disable_cause() - Disable count using CP0_Cause.DC.
  567. * @vcpu: Virtual CPU.
  568. *
  569. * Disable the CP0_Count timer and set CP0_Cause.DC. A timer interrupt on or
  570. * before the final stop time will be handled if the timer isn't disabled by
  571. * count_ctl.DC, but not after.
  572. *
  573. * Assumes CP0_Cause.DC is clear (count enabled).
  574. */
  575. void kvm_mips_count_disable_cause(struct kvm_vcpu *vcpu)
  576. {
  577. struct mips_coproc *cop0 = vcpu->arch.cop0;
  578. kvm_set_c0_guest_cause(cop0, CAUSEF_DC);
  579. if (!(vcpu->arch.count_ctl & KVM_REG_MIPS_COUNT_CTL_DC))
  580. kvm_mips_count_disable(vcpu);
  581. }
  582. /**
  583. * kvm_mips_count_enable_cause() - Enable count using CP0_Cause.DC.
  584. * @vcpu: Virtual CPU.
  585. *
  586. * Enable the CP0_Count timer and clear CP0_Cause.DC. A timer interrupt after
  587. * the start time will be handled if the timer isn't disabled by count_ctl.DC,
  588. * potentially before even returning, so the caller should be careful with
  589. * ordering of CP0_Cause modifications so as not to lose it.
  590. *
  591. * Assumes CP0_Cause.DC is set (count disabled).
  592. */
  593. void kvm_mips_count_enable_cause(struct kvm_vcpu *vcpu)
  594. {
  595. struct mips_coproc *cop0 = vcpu->arch.cop0;
  596. u32 count;
  597. kvm_clear_c0_guest_cause(cop0, CAUSEF_DC);
  598. /*
  599. * Set the dynamic count to match the static count.
  600. * This starts the hrtimer if count_ctl.DC allows it.
  601. * Otherwise it conveniently updates the biases.
  602. */
  603. count = kvm_read_c0_guest_count(cop0);
  604. kvm_mips_write_count(vcpu, count);
  605. }
  606. /**
  607. * kvm_mips_set_count_ctl() - Update the count control KVM register.
  608. * @vcpu: Virtual CPU.
  609. * @count_ctl: Count control register new value.
  610. *
  611. * Set the count control KVM register. The timer is updated accordingly.
  612. *
  613. * Returns: -EINVAL if reserved bits are set.
  614. * 0 on success.
  615. */
  616. int kvm_mips_set_count_ctl(struct kvm_vcpu *vcpu, s64 count_ctl)
  617. {
  618. struct mips_coproc *cop0 = vcpu->arch.cop0;
  619. s64 changed = count_ctl ^ vcpu->arch.count_ctl;
  620. s64 delta;
  621. ktime_t expire, now;
  622. u32 count, compare;
  623. /* Only allow defined bits to be changed */
  624. if (changed & ~(s64)(KVM_REG_MIPS_COUNT_CTL_DC))
  625. return -EINVAL;
  626. /* Apply new value */
  627. vcpu->arch.count_ctl = count_ctl;
  628. /* Master CP0_Count disable */
  629. if (changed & KVM_REG_MIPS_COUNT_CTL_DC) {
  630. /* Is CP0_Cause.DC already disabling CP0_Count? */
  631. if (kvm_read_c0_guest_cause(cop0) & CAUSEF_DC) {
  632. if (count_ctl & KVM_REG_MIPS_COUNT_CTL_DC)
  633. /* Just record the current time */
  634. vcpu->arch.count_resume = ktime_get();
  635. } else if (count_ctl & KVM_REG_MIPS_COUNT_CTL_DC) {
  636. /* disable timer and record current time */
  637. vcpu->arch.count_resume = kvm_mips_count_disable(vcpu);
  638. } else {
  639. /*
  640. * Calculate timeout relative to static count at resume
  641. * time (wrap 0 to 2^32).
  642. */
  643. count = kvm_read_c0_guest_count(cop0);
  644. compare = kvm_read_c0_guest_compare(cop0);
  645. delta = (u64)(u32)(compare - count - 1) + 1;
  646. delta = div_u64(delta * NSEC_PER_SEC,
  647. vcpu->arch.count_hz);
  648. expire = ktime_add_ns(vcpu->arch.count_resume, delta);
  649. /* Handle pending interrupt */
  650. now = ktime_get();
  651. if (ktime_compare(now, expire) >= 0)
  652. /* Nothing should be waiting on the timeout */
  653. kvm_mips_callbacks->queue_timer_int(vcpu);
  654. /* Resume hrtimer without changing bias */
  655. count = kvm_mips_read_count_running(vcpu, now);
  656. kvm_mips_resume_hrtimer(vcpu, now, count);
  657. }
  658. }
  659. return 0;
  660. }
  661. /**
  662. * kvm_mips_set_count_resume() - Update the count resume KVM register.
  663. * @vcpu: Virtual CPU.
  664. * @count_resume: Count resume register new value.
  665. *
  666. * Set the count resume KVM register.
  667. *
  668. * Returns: -EINVAL if out of valid range (0..now).
  669. * 0 on success.
  670. */
  671. int kvm_mips_set_count_resume(struct kvm_vcpu *vcpu, s64 count_resume)
  672. {
  673. /*
  674. * It doesn't make sense for the resume time to be in the future, as it
  675. * would be possible for the next interrupt to be more than a full
  676. * period in the future.
  677. */
  678. if (count_resume < 0 || count_resume > ktime_to_ns(ktime_get()))
  679. return -EINVAL;
  680. vcpu->arch.count_resume = ns_to_ktime(count_resume);
  681. return 0;
  682. }
  683. /**
  684. * kvm_mips_count_timeout() - Push timer forward on timeout.
  685. * @vcpu: Virtual CPU.
  686. *
  687. * Handle an hrtimer event by push the hrtimer forward a period.
  688. *
  689. * Returns: The hrtimer_restart value to return to the hrtimer subsystem.
  690. */
  691. enum hrtimer_restart kvm_mips_count_timeout(struct kvm_vcpu *vcpu)
  692. {
  693. /* Add the Count period to the current expiry time */
  694. hrtimer_add_expires_ns(&vcpu->arch.comparecount_timer,
  695. vcpu->arch.count_period);
  696. return HRTIMER_RESTART;
  697. }
  698. enum emulation_result kvm_mips_emul_eret(struct kvm_vcpu *vcpu)
  699. {
  700. struct mips_coproc *cop0 = vcpu->arch.cop0;
  701. enum emulation_result er = EMULATE_DONE;
  702. if (kvm_read_c0_guest_status(cop0) & ST0_EXL) {
  703. kvm_debug("[%#lx] ERET to %#lx\n", vcpu->arch.pc,
  704. kvm_read_c0_guest_epc(cop0));
  705. kvm_clear_c0_guest_status(cop0, ST0_EXL);
  706. vcpu->arch.pc = kvm_read_c0_guest_epc(cop0);
  707. } else if (kvm_read_c0_guest_status(cop0) & ST0_ERL) {
  708. kvm_clear_c0_guest_status(cop0, ST0_ERL);
  709. vcpu->arch.pc = kvm_read_c0_guest_errorepc(cop0);
  710. } else {
  711. kvm_err("[%#lx] ERET when MIPS_SR_EXL|MIPS_SR_ERL == 0\n",
  712. vcpu->arch.pc);
  713. er = EMULATE_FAIL;
  714. }
  715. return er;
  716. }
  717. enum emulation_result kvm_mips_emul_wait(struct kvm_vcpu *vcpu)
  718. {
  719. kvm_debug("[%#lx] !!!WAIT!!! (%#lx)\n", vcpu->arch.pc,
  720. vcpu->arch.pending_exceptions);
  721. ++vcpu->stat.wait_exits;
  722. trace_kvm_exit(vcpu, KVM_TRACE_EXIT_WAIT);
  723. if (!vcpu->arch.pending_exceptions) {
  724. vcpu->arch.wait = 1;
  725. kvm_vcpu_block(vcpu);
  726. /*
  727. * We we are runnable, then definitely go off to user space to
  728. * check if any I/O interrupts are pending.
  729. */
  730. if (kvm_check_request(KVM_REQ_UNHALT, vcpu)) {
  731. clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
  732. vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  733. }
  734. }
  735. return EMULATE_DONE;
  736. }
  737. /*
  738. * XXXKYMA: Linux doesn't seem to use TLBR, return EMULATE_FAIL for now so that
  739. * we can catch this, if things ever change
  740. */
  741. enum emulation_result kvm_mips_emul_tlbr(struct kvm_vcpu *vcpu)
  742. {
  743. struct mips_coproc *cop0 = vcpu->arch.cop0;
  744. unsigned long pc = vcpu->arch.pc;
  745. kvm_err("[%#lx] COP0_TLBR [%ld]\n", pc, kvm_read_c0_guest_index(cop0));
  746. return EMULATE_FAIL;
  747. }
  748. /* Write Guest TLB Entry @ Index */
  749. enum emulation_result kvm_mips_emul_tlbwi(struct kvm_vcpu *vcpu)
  750. {
  751. struct mips_coproc *cop0 = vcpu->arch.cop0;
  752. int index = kvm_read_c0_guest_index(cop0);
  753. struct kvm_mips_tlb *tlb = NULL;
  754. unsigned long pc = vcpu->arch.pc;
  755. if (index < 0 || index >= KVM_MIPS_GUEST_TLB_SIZE) {
  756. kvm_debug("%s: illegal index: %d\n", __func__, index);
  757. kvm_debug("[%#lx] COP0_TLBWI [%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx, mask: %#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. kvm_read_c0_guest_pagemask(cop0));
  762. index = (index & ~0x80000000) % KVM_MIPS_GUEST_TLB_SIZE;
  763. }
  764. tlb = &vcpu->arch.guest_tlb[index];
  765. /*
  766. * Probe the shadow host TLB for the entry being overwritten, if one
  767. * matches, invalidate it
  768. */
  769. kvm_mips_host_tlb_inv(vcpu, tlb->tlb_hi);
  770. tlb->tlb_mask = kvm_read_c0_guest_pagemask(cop0);
  771. tlb->tlb_hi = kvm_read_c0_guest_entryhi(cop0);
  772. tlb->tlb_lo[0] = kvm_read_c0_guest_entrylo0(cop0);
  773. tlb->tlb_lo[1] = kvm_read_c0_guest_entrylo1(cop0);
  774. kvm_debug("[%#lx] COP0_TLBWI [%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx, mask: %#lx)\n",
  775. pc, index, kvm_read_c0_guest_entryhi(cop0),
  776. kvm_read_c0_guest_entrylo0(cop0),
  777. kvm_read_c0_guest_entrylo1(cop0),
  778. kvm_read_c0_guest_pagemask(cop0));
  779. return EMULATE_DONE;
  780. }
  781. /* Write Guest TLB Entry @ Random Index */
  782. enum emulation_result kvm_mips_emul_tlbwr(struct kvm_vcpu *vcpu)
  783. {
  784. struct mips_coproc *cop0 = vcpu->arch.cop0;
  785. struct kvm_mips_tlb *tlb = NULL;
  786. unsigned long pc = vcpu->arch.pc;
  787. int index;
  788. get_random_bytes(&index, sizeof(index));
  789. index &= (KVM_MIPS_GUEST_TLB_SIZE - 1);
  790. tlb = &vcpu->arch.guest_tlb[index];
  791. /*
  792. * Probe the shadow host TLB for the entry being overwritten, if one
  793. * matches, invalidate it
  794. */
  795. kvm_mips_host_tlb_inv(vcpu, tlb->tlb_hi);
  796. tlb->tlb_mask = kvm_read_c0_guest_pagemask(cop0);
  797. tlb->tlb_hi = kvm_read_c0_guest_entryhi(cop0);
  798. tlb->tlb_lo[0] = kvm_read_c0_guest_entrylo0(cop0);
  799. tlb->tlb_lo[1] = kvm_read_c0_guest_entrylo1(cop0);
  800. kvm_debug("[%#lx] COP0_TLBWR[%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx)\n",
  801. pc, index, kvm_read_c0_guest_entryhi(cop0),
  802. kvm_read_c0_guest_entrylo0(cop0),
  803. kvm_read_c0_guest_entrylo1(cop0));
  804. return EMULATE_DONE;
  805. }
  806. enum emulation_result kvm_mips_emul_tlbp(struct kvm_vcpu *vcpu)
  807. {
  808. struct mips_coproc *cop0 = vcpu->arch.cop0;
  809. long entryhi = kvm_read_c0_guest_entryhi(cop0);
  810. unsigned long pc = vcpu->arch.pc;
  811. int index = -1;
  812. index = kvm_mips_guest_tlb_lookup(vcpu, entryhi);
  813. kvm_write_c0_guest_index(cop0, index);
  814. kvm_debug("[%#lx] COP0_TLBP (entryhi: %#lx), index: %d\n", pc, entryhi,
  815. index);
  816. return EMULATE_DONE;
  817. }
  818. /**
  819. * kvm_mips_config1_wrmask() - Find mask of writable bits in guest Config1
  820. * @vcpu: Virtual CPU.
  821. *
  822. * Finds the mask of bits which are writable in the guest's Config1 CP0
  823. * register, by userland (currently read-only to the guest).
  824. */
  825. unsigned int kvm_mips_config1_wrmask(struct kvm_vcpu *vcpu)
  826. {
  827. unsigned int mask = 0;
  828. /* Permit FPU to be present if FPU is supported */
  829. if (kvm_mips_guest_can_have_fpu(&vcpu->arch))
  830. mask |= MIPS_CONF1_FP;
  831. return mask;
  832. }
  833. /**
  834. * kvm_mips_config3_wrmask() - Find mask of writable bits in guest Config3
  835. * @vcpu: Virtual CPU.
  836. *
  837. * Finds the mask of bits which are writable in the guest's Config3 CP0
  838. * register, by userland (currently read-only to the guest).
  839. */
  840. unsigned int kvm_mips_config3_wrmask(struct kvm_vcpu *vcpu)
  841. {
  842. /* Config4 and ULRI are optional */
  843. unsigned int mask = MIPS_CONF_M | MIPS_CONF3_ULRI;
  844. /* Permit MSA to be present if MSA is supported */
  845. if (kvm_mips_guest_can_have_msa(&vcpu->arch))
  846. mask |= MIPS_CONF3_MSA;
  847. return mask;
  848. }
  849. /**
  850. * kvm_mips_config4_wrmask() - Find mask of writable bits in guest Config4
  851. * @vcpu: Virtual CPU.
  852. *
  853. * Finds the mask of bits which are writable in the guest's Config4 CP0
  854. * register, by userland (currently read-only to the guest).
  855. */
  856. unsigned int kvm_mips_config4_wrmask(struct kvm_vcpu *vcpu)
  857. {
  858. /* Config5 is optional */
  859. unsigned int mask = MIPS_CONF_M;
  860. /* KScrExist */
  861. mask |= (unsigned int)vcpu->arch.kscratch_enabled << 16;
  862. return mask;
  863. }
  864. /**
  865. * kvm_mips_config5_wrmask() - Find mask of writable bits in guest Config5
  866. * @vcpu: Virtual CPU.
  867. *
  868. * Finds the mask of bits which are writable in the guest's Config5 CP0
  869. * register, by the guest itself.
  870. */
  871. unsigned int kvm_mips_config5_wrmask(struct kvm_vcpu *vcpu)
  872. {
  873. unsigned int mask = 0;
  874. /* Permit MSAEn changes if MSA supported and enabled */
  875. if (kvm_mips_guest_has_msa(&vcpu->arch))
  876. mask |= MIPS_CONF5_MSAEN;
  877. /*
  878. * Permit guest FPU mode changes if FPU is enabled and the relevant
  879. * feature exists according to FIR register.
  880. */
  881. if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
  882. if (cpu_has_fre)
  883. mask |= MIPS_CONF5_FRE;
  884. /* We don't support UFR or UFE */
  885. }
  886. return mask;
  887. }
  888. enum emulation_result kvm_mips_emulate_CP0(union mips_instruction inst,
  889. u32 *opc, u32 cause,
  890. struct kvm_run *run,
  891. struct kvm_vcpu *vcpu)
  892. {
  893. struct mips_coproc *cop0 = vcpu->arch.cop0;
  894. enum emulation_result er = EMULATE_DONE;
  895. u32 rt, rd, sel;
  896. unsigned long curr_pc;
  897. /*
  898. * Update PC and hold onto current PC in case there is
  899. * an error and we want to rollback the PC
  900. */
  901. curr_pc = vcpu->arch.pc;
  902. er = update_pc(vcpu, cause);
  903. if (er == EMULATE_FAIL)
  904. return er;
  905. if (inst.co_format.co) {
  906. switch (inst.co_format.func) {
  907. case tlbr_op: /* Read indexed TLB entry */
  908. er = kvm_mips_emul_tlbr(vcpu);
  909. break;
  910. case tlbwi_op: /* Write indexed */
  911. er = kvm_mips_emul_tlbwi(vcpu);
  912. break;
  913. case tlbwr_op: /* Write random */
  914. er = kvm_mips_emul_tlbwr(vcpu);
  915. break;
  916. case tlbp_op: /* TLB Probe */
  917. er = kvm_mips_emul_tlbp(vcpu);
  918. break;
  919. case rfe_op:
  920. kvm_err("!!!COP0_RFE!!!\n");
  921. break;
  922. case eret_op:
  923. er = kvm_mips_emul_eret(vcpu);
  924. goto dont_update_pc;
  925. case wait_op:
  926. er = kvm_mips_emul_wait(vcpu);
  927. break;
  928. }
  929. } else {
  930. rt = inst.c0r_format.rt;
  931. rd = inst.c0r_format.rd;
  932. sel = inst.c0r_format.sel;
  933. switch (inst.c0r_format.rs) {
  934. case mfc_op:
  935. #ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
  936. cop0->stat[rd][sel]++;
  937. #endif
  938. /* Get reg */
  939. if ((rd == MIPS_CP0_COUNT) && (sel == 0)) {
  940. vcpu->arch.gprs[rt] =
  941. (s32)kvm_mips_read_count(vcpu);
  942. } else if ((rd == MIPS_CP0_ERRCTL) && (sel == 0)) {
  943. vcpu->arch.gprs[rt] = 0x0;
  944. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  945. kvm_mips_trans_mfc0(inst, opc, vcpu);
  946. #endif
  947. } else {
  948. vcpu->arch.gprs[rt] = (s32)cop0->reg[rd][sel];
  949. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  950. kvm_mips_trans_mfc0(inst, opc, vcpu);
  951. #endif
  952. }
  953. trace_kvm_hwr(vcpu, KVM_TRACE_MFC0,
  954. KVM_TRACE_COP0(rd, sel),
  955. vcpu->arch.gprs[rt]);
  956. break;
  957. case dmfc_op:
  958. vcpu->arch.gprs[rt] = cop0->reg[rd][sel];
  959. trace_kvm_hwr(vcpu, KVM_TRACE_DMFC0,
  960. KVM_TRACE_COP0(rd, sel),
  961. vcpu->arch.gprs[rt]);
  962. break;
  963. case mtc_op:
  964. #ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
  965. cop0->stat[rd][sel]++;
  966. #endif
  967. trace_kvm_hwr(vcpu, KVM_TRACE_MTC0,
  968. KVM_TRACE_COP0(rd, sel),
  969. vcpu->arch.gprs[rt]);
  970. if ((rd == MIPS_CP0_TLB_INDEX)
  971. && (vcpu->arch.gprs[rt] >=
  972. KVM_MIPS_GUEST_TLB_SIZE)) {
  973. kvm_err("Invalid TLB Index: %ld",
  974. vcpu->arch.gprs[rt]);
  975. er = EMULATE_FAIL;
  976. break;
  977. }
  978. #define C0_EBASE_CORE_MASK 0xff
  979. if ((rd == MIPS_CP0_PRID) && (sel == 1)) {
  980. /* Preserve CORE number */
  981. kvm_change_c0_guest_ebase(cop0,
  982. ~(C0_EBASE_CORE_MASK),
  983. vcpu->arch.gprs[rt]);
  984. kvm_err("MTCz, cop0->reg[EBASE]: %#lx\n",
  985. kvm_read_c0_guest_ebase(cop0));
  986. } else if (rd == MIPS_CP0_TLB_HI && sel == 0) {
  987. u32 nasid =
  988. vcpu->arch.gprs[rt] & KVM_ENTRYHI_ASID;
  989. if ((KSEGX(vcpu->arch.gprs[rt]) != CKSEG0) &&
  990. ((kvm_read_c0_guest_entryhi(cop0) &
  991. KVM_ENTRYHI_ASID) != nasid)) {
  992. trace_kvm_asid_change(vcpu,
  993. kvm_read_c0_guest_entryhi(cop0)
  994. & KVM_ENTRYHI_ASID,
  995. nasid);
  996. /* Blow away the shadow host TLBs */
  997. kvm_mips_flush_host_tlb(1);
  998. }
  999. kvm_write_c0_guest_entryhi(cop0,
  1000. vcpu->arch.gprs[rt]);
  1001. }
  1002. /* Are we writing to COUNT */
  1003. else if ((rd == MIPS_CP0_COUNT) && (sel == 0)) {
  1004. kvm_mips_write_count(vcpu, vcpu->arch.gprs[rt]);
  1005. goto done;
  1006. } else if ((rd == MIPS_CP0_COMPARE) && (sel == 0)) {
  1007. /* If we are writing to COMPARE */
  1008. /* Clear pending timer interrupt, if any */
  1009. kvm_mips_write_compare(vcpu,
  1010. vcpu->arch.gprs[rt],
  1011. true);
  1012. } else if ((rd == MIPS_CP0_STATUS) && (sel == 0)) {
  1013. unsigned int old_val, val, change;
  1014. old_val = kvm_read_c0_guest_status(cop0);
  1015. val = vcpu->arch.gprs[rt];
  1016. change = val ^ old_val;
  1017. /* Make sure that the NMI bit is never set */
  1018. val &= ~ST0_NMI;
  1019. /*
  1020. * Don't allow CU1 or FR to be set unless FPU
  1021. * capability enabled and exists in guest
  1022. * configuration.
  1023. */
  1024. if (!kvm_mips_guest_has_fpu(&vcpu->arch))
  1025. val &= ~(ST0_CU1 | ST0_FR);
  1026. /*
  1027. * Also don't allow FR to be set if host doesn't
  1028. * support it.
  1029. */
  1030. if (!(current_cpu_data.fpu_id & MIPS_FPIR_F64))
  1031. val &= ~ST0_FR;
  1032. /* Handle changes in FPU mode */
  1033. preempt_disable();
  1034. /*
  1035. * FPU and Vector register state is made
  1036. * UNPREDICTABLE by a change of FR, so don't
  1037. * even bother saving it.
  1038. */
  1039. if (change & ST0_FR)
  1040. kvm_drop_fpu(vcpu);
  1041. /*
  1042. * If MSA state is already live, it is undefined
  1043. * how it interacts with FR=0 FPU state, and we
  1044. * don't want to hit reserved instruction
  1045. * exceptions trying to save the MSA state later
  1046. * when CU=1 && FR=1, so play it safe and save
  1047. * it first.
  1048. */
  1049. if (change & ST0_CU1 && !(val & ST0_FR) &&
  1050. vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
  1051. kvm_lose_fpu(vcpu);
  1052. /*
  1053. * Propagate CU1 (FPU enable) changes
  1054. * immediately if the FPU context is already
  1055. * loaded. When disabling we leave the context
  1056. * loaded so it can be quickly enabled again in
  1057. * the near future.
  1058. */
  1059. if (change & ST0_CU1 &&
  1060. vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)
  1061. change_c0_status(ST0_CU1, val);
  1062. preempt_enable();
  1063. kvm_write_c0_guest_status(cop0, val);
  1064. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  1065. /*
  1066. * If FPU present, we need CU1/FR bits to take
  1067. * effect fairly soon.
  1068. */
  1069. if (!kvm_mips_guest_has_fpu(&vcpu->arch))
  1070. kvm_mips_trans_mtc0(inst, opc, vcpu);
  1071. #endif
  1072. } else if ((rd == MIPS_CP0_CONFIG) && (sel == 5)) {
  1073. unsigned int old_val, val, change, wrmask;
  1074. old_val = kvm_read_c0_guest_config5(cop0);
  1075. val = vcpu->arch.gprs[rt];
  1076. /* Only a few bits are writable in Config5 */
  1077. wrmask = kvm_mips_config5_wrmask(vcpu);
  1078. change = (val ^ old_val) & wrmask;
  1079. val = old_val ^ change;
  1080. /* Handle changes in FPU/MSA modes */
  1081. preempt_disable();
  1082. /*
  1083. * Propagate FRE changes immediately if the FPU
  1084. * context is already loaded.
  1085. */
  1086. if (change & MIPS_CONF5_FRE &&
  1087. vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)
  1088. change_c0_config5(MIPS_CONF5_FRE, val);
  1089. /*
  1090. * Propagate MSAEn changes immediately if the
  1091. * MSA context is already loaded. When disabling
  1092. * we leave the context loaded so it can be
  1093. * quickly enabled again in the near future.
  1094. */
  1095. if (change & MIPS_CONF5_MSAEN &&
  1096. vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
  1097. change_c0_config5(MIPS_CONF5_MSAEN,
  1098. val);
  1099. preempt_enable();
  1100. kvm_write_c0_guest_config5(cop0, val);
  1101. } else if ((rd == MIPS_CP0_CAUSE) && (sel == 0)) {
  1102. u32 old_cause, new_cause;
  1103. old_cause = kvm_read_c0_guest_cause(cop0);
  1104. new_cause = vcpu->arch.gprs[rt];
  1105. /* Update R/W bits */
  1106. kvm_change_c0_guest_cause(cop0, 0x08800300,
  1107. new_cause);
  1108. /* DC bit enabling/disabling timer? */
  1109. if ((old_cause ^ new_cause) & CAUSEF_DC) {
  1110. if (new_cause & CAUSEF_DC)
  1111. kvm_mips_count_disable_cause(vcpu);
  1112. else
  1113. kvm_mips_count_enable_cause(vcpu);
  1114. }
  1115. } else if ((rd == MIPS_CP0_HWRENA) && (sel == 0)) {
  1116. u32 mask = MIPS_HWRENA_CPUNUM |
  1117. MIPS_HWRENA_SYNCISTEP |
  1118. MIPS_HWRENA_CC |
  1119. MIPS_HWRENA_CCRES;
  1120. if (kvm_read_c0_guest_config3(cop0) &
  1121. MIPS_CONF3_ULRI)
  1122. mask |= MIPS_HWRENA_ULR;
  1123. cop0->reg[rd][sel] = vcpu->arch.gprs[rt] & mask;
  1124. } else {
  1125. cop0->reg[rd][sel] = vcpu->arch.gprs[rt];
  1126. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  1127. kvm_mips_trans_mtc0(inst, opc, vcpu);
  1128. #endif
  1129. }
  1130. break;
  1131. case dmtc_op:
  1132. kvm_err("!!!!!!![%#lx]dmtc_op: rt: %d, rd: %d, sel: %d!!!!!!\n",
  1133. vcpu->arch.pc, rt, rd, sel);
  1134. trace_kvm_hwr(vcpu, KVM_TRACE_DMTC0,
  1135. KVM_TRACE_COP0(rd, sel),
  1136. vcpu->arch.gprs[rt]);
  1137. er = EMULATE_FAIL;
  1138. break;
  1139. case mfmc0_op:
  1140. #ifdef KVM_MIPS_DEBUG_COP0_COUNTERS
  1141. cop0->stat[MIPS_CP0_STATUS][0]++;
  1142. #endif
  1143. if (rt != 0)
  1144. vcpu->arch.gprs[rt] =
  1145. kvm_read_c0_guest_status(cop0);
  1146. /* EI */
  1147. if (inst.mfmc0_format.sc) {
  1148. kvm_debug("[%#lx] mfmc0_op: EI\n",
  1149. vcpu->arch.pc);
  1150. kvm_set_c0_guest_status(cop0, ST0_IE);
  1151. } else {
  1152. kvm_debug("[%#lx] mfmc0_op: DI\n",
  1153. vcpu->arch.pc);
  1154. kvm_clear_c0_guest_status(cop0, ST0_IE);
  1155. }
  1156. break;
  1157. case wrpgpr_op:
  1158. {
  1159. u32 css = cop0->reg[MIPS_CP0_STATUS][2] & 0xf;
  1160. u32 pss =
  1161. (cop0->reg[MIPS_CP0_STATUS][2] >> 6) & 0xf;
  1162. /*
  1163. * We don't support any shadow register sets, so
  1164. * SRSCtl[PSS] == SRSCtl[CSS] = 0
  1165. */
  1166. if (css || pss) {
  1167. er = EMULATE_FAIL;
  1168. break;
  1169. }
  1170. kvm_debug("WRPGPR[%d][%d] = %#lx\n", pss, rd,
  1171. vcpu->arch.gprs[rt]);
  1172. vcpu->arch.gprs[rd] = vcpu->arch.gprs[rt];
  1173. }
  1174. break;
  1175. default:
  1176. kvm_err("[%#lx]MachEmulateCP0: unsupported COP0, copz: 0x%x\n",
  1177. vcpu->arch.pc, inst.c0r_format.rs);
  1178. er = EMULATE_FAIL;
  1179. break;
  1180. }
  1181. }
  1182. done:
  1183. /* Rollback PC only if emulation was unsuccessful */
  1184. if (er == EMULATE_FAIL)
  1185. vcpu->arch.pc = curr_pc;
  1186. dont_update_pc:
  1187. /*
  1188. * This is for special instructions whose emulation
  1189. * updates the PC, so do not overwrite the PC under
  1190. * any circumstances
  1191. */
  1192. return er;
  1193. }
  1194. enum emulation_result kvm_mips_emulate_store(union mips_instruction inst,
  1195. u32 cause,
  1196. struct kvm_run *run,
  1197. struct kvm_vcpu *vcpu)
  1198. {
  1199. enum emulation_result er = EMULATE_DO_MMIO;
  1200. u32 rt;
  1201. u32 bytes;
  1202. void *data = run->mmio.data;
  1203. unsigned long curr_pc;
  1204. /*
  1205. * Update PC and hold onto current PC in case there is
  1206. * an error and we want to rollback the PC
  1207. */
  1208. curr_pc = vcpu->arch.pc;
  1209. er = update_pc(vcpu, cause);
  1210. if (er == EMULATE_FAIL)
  1211. return er;
  1212. rt = inst.i_format.rt;
  1213. switch (inst.i_format.opcode) {
  1214. case sb_op:
  1215. bytes = 1;
  1216. if (bytes > sizeof(run->mmio.data)) {
  1217. kvm_err("%s: bad MMIO length: %d\n", __func__,
  1218. run->mmio.len);
  1219. }
  1220. run->mmio.phys_addr =
  1221. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  1222. host_cp0_badvaddr);
  1223. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  1224. er = EMULATE_FAIL;
  1225. break;
  1226. }
  1227. run->mmio.len = bytes;
  1228. run->mmio.is_write = 1;
  1229. vcpu->mmio_needed = 1;
  1230. vcpu->mmio_is_write = 1;
  1231. *(u8 *) data = vcpu->arch.gprs[rt];
  1232. kvm_debug("OP_SB: eaddr: %#lx, gpr: %#lx, data: %#x\n",
  1233. vcpu->arch.host_cp0_badvaddr, vcpu->arch.gprs[rt],
  1234. *(u8 *) data);
  1235. break;
  1236. case sw_op:
  1237. bytes = 4;
  1238. if (bytes > sizeof(run->mmio.data)) {
  1239. kvm_err("%s: bad MMIO length: %d\n", __func__,
  1240. run->mmio.len);
  1241. }
  1242. run->mmio.phys_addr =
  1243. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  1244. host_cp0_badvaddr);
  1245. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  1246. er = EMULATE_FAIL;
  1247. break;
  1248. }
  1249. run->mmio.len = bytes;
  1250. run->mmio.is_write = 1;
  1251. vcpu->mmio_needed = 1;
  1252. vcpu->mmio_is_write = 1;
  1253. *(u32 *) data = vcpu->arch.gprs[rt];
  1254. kvm_debug("[%#lx] OP_SW: eaddr: %#lx, gpr: %#lx, data: %#x\n",
  1255. vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
  1256. vcpu->arch.gprs[rt], *(u32 *) data);
  1257. break;
  1258. case sh_op:
  1259. bytes = 2;
  1260. if (bytes > sizeof(run->mmio.data)) {
  1261. kvm_err("%s: bad MMIO length: %d\n", __func__,
  1262. run->mmio.len);
  1263. }
  1264. run->mmio.phys_addr =
  1265. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  1266. host_cp0_badvaddr);
  1267. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  1268. er = EMULATE_FAIL;
  1269. break;
  1270. }
  1271. run->mmio.len = bytes;
  1272. run->mmio.is_write = 1;
  1273. vcpu->mmio_needed = 1;
  1274. vcpu->mmio_is_write = 1;
  1275. *(u16 *) data = vcpu->arch.gprs[rt];
  1276. kvm_debug("[%#lx] OP_SH: eaddr: %#lx, gpr: %#lx, data: %#x\n",
  1277. vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
  1278. vcpu->arch.gprs[rt], *(u32 *) data);
  1279. break;
  1280. default:
  1281. kvm_err("Store not yet supported (inst=0x%08x)\n",
  1282. inst.word);
  1283. er = EMULATE_FAIL;
  1284. break;
  1285. }
  1286. /* Rollback PC if emulation was unsuccessful */
  1287. if (er == EMULATE_FAIL)
  1288. vcpu->arch.pc = curr_pc;
  1289. return er;
  1290. }
  1291. enum emulation_result kvm_mips_emulate_load(union mips_instruction inst,
  1292. u32 cause, struct kvm_run *run,
  1293. struct kvm_vcpu *vcpu)
  1294. {
  1295. enum emulation_result er = EMULATE_DO_MMIO;
  1296. u32 op, rt;
  1297. u32 bytes;
  1298. rt = inst.i_format.rt;
  1299. op = inst.i_format.opcode;
  1300. vcpu->arch.pending_load_cause = cause;
  1301. vcpu->arch.io_gpr = rt;
  1302. switch (op) {
  1303. case lw_op:
  1304. bytes = 4;
  1305. if (bytes > sizeof(run->mmio.data)) {
  1306. kvm_err("%s: bad MMIO length: %d\n", __func__,
  1307. run->mmio.len);
  1308. er = EMULATE_FAIL;
  1309. break;
  1310. }
  1311. run->mmio.phys_addr =
  1312. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  1313. host_cp0_badvaddr);
  1314. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  1315. er = EMULATE_FAIL;
  1316. break;
  1317. }
  1318. run->mmio.len = bytes;
  1319. run->mmio.is_write = 0;
  1320. vcpu->mmio_needed = 1;
  1321. vcpu->mmio_is_write = 0;
  1322. break;
  1323. case lh_op:
  1324. case lhu_op:
  1325. bytes = 2;
  1326. if (bytes > sizeof(run->mmio.data)) {
  1327. kvm_err("%s: bad MMIO length: %d\n", __func__,
  1328. run->mmio.len);
  1329. er = EMULATE_FAIL;
  1330. break;
  1331. }
  1332. run->mmio.phys_addr =
  1333. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  1334. host_cp0_badvaddr);
  1335. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  1336. er = EMULATE_FAIL;
  1337. break;
  1338. }
  1339. run->mmio.len = bytes;
  1340. run->mmio.is_write = 0;
  1341. vcpu->mmio_needed = 1;
  1342. vcpu->mmio_is_write = 0;
  1343. if (op == lh_op)
  1344. vcpu->mmio_needed = 2;
  1345. else
  1346. vcpu->mmio_needed = 1;
  1347. break;
  1348. case lbu_op:
  1349. case lb_op:
  1350. bytes = 1;
  1351. if (bytes > sizeof(run->mmio.data)) {
  1352. kvm_err("%s: bad MMIO length: %d\n", __func__,
  1353. run->mmio.len);
  1354. er = EMULATE_FAIL;
  1355. break;
  1356. }
  1357. run->mmio.phys_addr =
  1358. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  1359. host_cp0_badvaddr);
  1360. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  1361. er = EMULATE_FAIL;
  1362. break;
  1363. }
  1364. run->mmio.len = bytes;
  1365. run->mmio.is_write = 0;
  1366. vcpu->mmio_is_write = 0;
  1367. if (op == lb_op)
  1368. vcpu->mmio_needed = 2;
  1369. else
  1370. vcpu->mmio_needed = 1;
  1371. break;
  1372. default:
  1373. kvm_err("Load not yet supported (inst=0x%08x)\n",
  1374. inst.word);
  1375. er = EMULATE_FAIL;
  1376. break;
  1377. }
  1378. return er;
  1379. }
  1380. enum emulation_result kvm_mips_emulate_cache(union mips_instruction inst,
  1381. u32 *opc, u32 cause,
  1382. struct kvm_run *run,
  1383. struct kvm_vcpu *vcpu)
  1384. {
  1385. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1386. enum emulation_result er = EMULATE_DONE;
  1387. u32 cache, op_inst, op, base;
  1388. s16 offset;
  1389. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1390. unsigned long va;
  1391. unsigned long curr_pc;
  1392. /*
  1393. * Update PC and hold onto current PC in case there is
  1394. * an error and we want to rollback the PC
  1395. */
  1396. curr_pc = vcpu->arch.pc;
  1397. er = update_pc(vcpu, cause);
  1398. if (er == EMULATE_FAIL)
  1399. return er;
  1400. base = inst.i_format.rs;
  1401. op_inst = inst.i_format.rt;
  1402. if (cpu_has_mips_r6)
  1403. offset = inst.spec3_format.simmediate;
  1404. else
  1405. offset = inst.i_format.simmediate;
  1406. cache = op_inst & CacheOp_Cache;
  1407. op = op_inst & CacheOp_Op;
  1408. va = arch->gprs[base] + offset;
  1409. kvm_debug("CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
  1410. cache, op, base, arch->gprs[base], offset);
  1411. /*
  1412. * Treat INDEX_INV as a nop, basically issued by Linux on startup to
  1413. * invalidate the caches entirely by stepping through all the
  1414. * ways/indexes
  1415. */
  1416. if (op == Index_Writeback_Inv) {
  1417. kvm_debug("@ %#lx/%#lx CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
  1418. vcpu->arch.pc, vcpu->arch.gprs[31], cache, op, base,
  1419. arch->gprs[base], offset);
  1420. if (cache == Cache_D)
  1421. r4k_blast_dcache();
  1422. else if (cache == Cache_I)
  1423. r4k_blast_icache();
  1424. else {
  1425. kvm_err("%s: unsupported CACHE INDEX operation\n",
  1426. __func__);
  1427. return EMULATE_FAIL;
  1428. }
  1429. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  1430. kvm_mips_trans_cache_index(inst, opc, vcpu);
  1431. #endif
  1432. goto done;
  1433. }
  1434. preempt_disable();
  1435. if (KVM_GUEST_KSEGX(va) == KVM_GUEST_KSEG0) {
  1436. if (kvm_mips_host_tlb_lookup(vcpu, va) < 0 &&
  1437. kvm_mips_handle_kseg0_tlb_fault(va, vcpu)) {
  1438. kvm_err("%s: handling mapped kseg0 tlb fault for %lx, vcpu: %p, ASID: %#lx\n",
  1439. __func__, va, vcpu, read_c0_entryhi());
  1440. er = EMULATE_FAIL;
  1441. preempt_enable();
  1442. goto done;
  1443. }
  1444. } else if ((KVM_GUEST_KSEGX(va) < KVM_GUEST_KSEG0) ||
  1445. KVM_GUEST_KSEGX(va) == KVM_GUEST_KSEG23) {
  1446. int index;
  1447. /* If an entry already exists then skip */
  1448. if (kvm_mips_host_tlb_lookup(vcpu, va) >= 0)
  1449. goto skip_fault;
  1450. /*
  1451. * If address not in the guest TLB, then give the guest a fault,
  1452. * the resulting handler will do the right thing
  1453. */
  1454. index = kvm_mips_guest_tlb_lookup(vcpu, (va & VPN2_MASK) |
  1455. (kvm_read_c0_guest_entryhi
  1456. (cop0) & KVM_ENTRYHI_ASID));
  1457. if (index < 0) {
  1458. vcpu->arch.host_cp0_badvaddr = va;
  1459. vcpu->arch.pc = curr_pc;
  1460. er = kvm_mips_emulate_tlbmiss_ld(cause, NULL, run,
  1461. vcpu);
  1462. preempt_enable();
  1463. goto dont_update_pc;
  1464. } else {
  1465. struct kvm_mips_tlb *tlb = &vcpu->arch.guest_tlb[index];
  1466. /*
  1467. * Check if the entry is valid, if not then setup a TLB
  1468. * invalid exception to the guest
  1469. */
  1470. if (!TLB_IS_VALID(*tlb, va)) {
  1471. vcpu->arch.host_cp0_badvaddr = va;
  1472. vcpu->arch.pc = curr_pc;
  1473. er = kvm_mips_emulate_tlbinv_ld(cause, NULL,
  1474. run, vcpu);
  1475. preempt_enable();
  1476. goto dont_update_pc;
  1477. }
  1478. /*
  1479. * We fault an entry from the guest tlb to the
  1480. * shadow host TLB
  1481. */
  1482. if (kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb)) {
  1483. kvm_err("%s: handling mapped seg tlb fault for %lx, index: %u, vcpu: %p, ASID: %#lx\n",
  1484. __func__, va, index, vcpu,
  1485. read_c0_entryhi());
  1486. er = EMULATE_FAIL;
  1487. preempt_enable();
  1488. goto done;
  1489. }
  1490. }
  1491. } else {
  1492. kvm_err("INVALID CACHE INDEX/ADDRESS (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
  1493. cache, op, base, arch->gprs[base], offset);
  1494. er = EMULATE_FAIL;
  1495. preempt_enable();
  1496. goto done;
  1497. }
  1498. skip_fault:
  1499. /* XXXKYMA: Only a subset of cache ops are supported, used by Linux */
  1500. if (op_inst == Hit_Writeback_Inv_D || op_inst == Hit_Invalidate_D) {
  1501. flush_dcache_line(va);
  1502. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  1503. /*
  1504. * Replace the CACHE instruction, with a SYNCI, not the same,
  1505. * but avoids a trap
  1506. */
  1507. kvm_mips_trans_cache_va(inst, opc, vcpu);
  1508. #endif
  1509. } else if (op_inst == Hit_Invalidate_I) {
  1510. flush_dcache_line(va);
  1511. flush_icache_line(va);
  1512. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  1513. /* Replace the CACHE instruction, with a SYNCI */
  1514. kvm_mips_trans_cache_va(inst, opc, vcpu);
  1515. #endif
  1516. } else {
  1517. kvm_err("NO-OP CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
  1518. cache, op, base, arch->gprs[base], offset);
  1519. er = EMULATE_FAIL;
  1520. }
  1521. preempt_enable();
  1522. done:
  1523. /* Rollback PC only if emulation was unsuccessful */
  1524. if (er == EMULATE_FAIL)
  1525. vcpu->arch.pc = curr_pc;
  1526. dont_update_pc:
  1527. /*
  1528. * This is for exceptions whose emulation updates the PC, so do not
  1529. * overwrite the PC under any circumstances
  1530. */
  1531. return er;
  1532. }
  1533. enum emulation_result kvm_mips_emulate_inst(u32 cause, u32 *opc,
  1534. struct kvm_run *run,
  1535. struct kvm_vcpu *vcpu)
  1536. {
  1537. union mips_instruction inst;
  1538. enum emulation_result er = EMULATE_DONE;
  1539. /* Fetch the instruction. */
  1540. if (cause & CAUSEF_BD)
  1541. opc += 1;
  1542. inst.word = kvm_get_inst(opc, vcpu);
  1543. switch (inst.r_format.opcode) {
  1544. case cop0_op:
  1545. er = kvm_mips_emulate_CP0(inst, opc, cause, run, vcpu);
  1546. break;
  1547. case sb_op:
  1548. case sh_op:
  1549. case sw_op:
  1550. er = kvm_mips_emulate_store(inst, cause, run, vcpu);
  1551. break;
  1552. case lb_op:
  1553. case lbu_op:
  1554. case lhu_op:
  1555. case lh_op:
  1556. case lw_op:
  1557. er = kvm_mips_emulate_load(inst, cause, run, vcpu);
  1558. break;
  1559. #ifndef CONFIG_CPU_MIPSR6
  1560. case cache_op:
  1561. ++vcpu->stat.cache_exits;
  1562. trace_kvm_exit(vcpu, KVM_TRACE_EXIT_CACHE);
  1563. er = kvm_mips_emulate_cache(inst, opc, cause, run, vcpu);
  1564. break;
  1565. #else
  1566. case spec3_op:
  1567. switch (inst.spec3_format.func) {
  1568. case cache6_op:
  1569. ++vcpu->stat.cache_exits;
  1570. trace_kvm_exit(vcpu, KVM_TRACE_EXIT_CACHE);
  1571. er = kvm_mips_emulate_cache(inst, opc, cause, run,
  1572. vcpu);
  1573. break;
  1574. default:
  1575. goto unknown;
  1576. };
  1577. break;
  1578. unknown:
  1579. #endif
  1580. default:
  1581. kvm_err("Instruction emulation not supported (%p/%#x)\n", opc,
  1582. inst.word);
  1583. kvm_arch_vcpu_dump_regs(vcpu);
  1584. er = EMULATE_FAIL;
  1585. break;
  1586. }
  1587. return er;
  1588. }
  1589. enum emulation_result kvm_mips_emulate_syscall(u32 cause,
  1590. u32 *opc,
  1591. struct kvm_run *run,
  1592. struct kvm_vcpu *vcpu)
  1593. {
  1594. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1595. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1596. enum emulation_result er = EMULATE_DONE;
  1597. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1598. /* save old pc */
  1599. kvm_write_c0_guest_epc(cop0, arch->pc);
  1600. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1601. if (cause & CAUSEF_BD)
  1602. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1603. else
  1604. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1605. kvm_debug("Delivering SYSCALL @ pc %#lx\n", arch->pc);
  1606. kvm_change_c0_guest_cause(cop0, (0xff),
  1607. (EXCCODE_SYS << CAUSEB_EXCCODE));
  1608. /* Set PC to the exception entry point */
  1609. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1610. } else {
  1611. kvm_err("Trying to deliver SYSCALL when EXL is already set\n");
  1612. er = EMULATE_FAIL;
  1613. }
  1614. return er;
  1615. }
  1616. enum emulation_result kvm_mips_emulate_tlbmiss_ld(u32 cause,
  1617. u32 *opc,
  1618. struct kvm_run *run,
  1619. struct kvm_vcpu *vcpu)
  1620. {
  1621. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1622. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1623. unsigned long entryhi = (vcpu->arch. host_cp0_badvaddr & VPN2_MASK) |
  1624. (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
  1625. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1626. /* save old pc */
  1627. kvm_write_c0_guest_epc(cop0, arch->pc);
  1628. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1629. if (cause & CAUSEF_BD)
  1630. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1631. else
  1632. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1633. kvm_debug("[EXL == 0] delivering TLB MISS @ pc %#lx\n",
  1634. arch->pc);
  1635. /* set pc to the exception entry point */
  1636. arch->pc = KVM_GUEST_KSEG0 + 0x0;
  1637. } else {
  1638. kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n",
  1639. arch->pc);
  1640. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1641. }
  1642. kvm_change_c0_guest_cause(cop0, (0xff),
  1643. (EXCCODE_TLBL << CAUSEB_EXCCODE));
  1644. /* setup badvaddr, context and entryhi registers for the guest */
  1645. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1646. /* XXXKYMA: is the context register used by linux??? */
  1647. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1648. /* Blow away the shadow host TLBs */
  1649. kvm_mips_flush_host_tlb(1);
  1650. return EMULATE_DONE;
  1651. }
  1652. enum emulation_result kvm_mips_emulate_tlbinv_ld(u32 cause,
  1653. u32 *opc,
  1654. struct kvm_run *run,
  1655. struct kvm_vcpu *vcpu)
  1656. {
  1657. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1658. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1659. unsigned long entryhi =
  1660. (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
  1661. (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
  1662. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1663. /* save old pc */
  1664. kvm_write_c0_guest_epc(cop0, arch->pc);
  1665. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1666. if (cause & CAUSEF_BD)
  1667. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1668. else
  1669. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1670. kvm_debug("[EXL == 0] delivering TLB INV @ pc %#lx\n",
  1671. arch->pc);
  1672. /* set pc to the exception entry point */
  1673. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1674. } else {
  1675. kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n",
  1676. arch->pc);
  1677. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1678. }
  1679. kvm_change_c0_guest_cause(cop0, (0xff),
  1680. (EXCCODE_TLBL << CAUSEB_EXCCODE));
  1681. /* setup badvaddr, context and entryhi registers for the guest */
  1682. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1683. /* XXXKYMA: is the context register used by linux??? */
  1684. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1685. /* Blow away the shadow host TLBs */
  1686. kvm_mips_flush_host_tlb(1);
  1687. return EMULATE_DONE;
  1688. }
  1689. enum emulation_result kvm_mips_emulate_tlbmiss_st(u32 cause,
  1690. u32 *opc,
  1691. struct kvm_run *run,
  1692. struct kvm_vcpu *vcpu)
  1693. {
  1694. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1695. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1696. unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
  1697. (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
  1698. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1699. /* save old pc */
  1700. kvm_write_c0_guest_epc(cop0, arch->pc);
  1701. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1702. if (cause & CAUSEF_BD)
  1703. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1704. else
  1705. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1706. kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n",
  1707. arch->pc);
  1708. /* Set PC to the exception entry point */
  1709. arch->pc = KVM_GUEST_KSEG0 + 0x0;
  1710. } else {
  1711. kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n",
  1712. arch->pc);
  1713. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1714. }
  1715. kvm_change_c0_guest_cause(cop0, (0xff),
  1716. (EXCCODE_TLBS << CAUSEB_EXCCODE));
  1717. /* setup badvaddr, context and entryhi registers for the guest */
  1718. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1719. /* XXXKYMA: is the context register used by linux??? */
  1720. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1721. /* Blow away the shadow host TLBs */
  1722. kvm_mips_flush_host_tlb(1);
  1723. return EMULATE_DONE;
  1724. }
  1725. enum emulation_result kvm_mips_emulate_tlbinv_st(u32 cause,
  1726. u32 *opc,
  1727. struct kvm_run *run,
  1728. struct kvm_vcpu *vcpu)
  1729. {
  1730. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1731. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1732. unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
  1733. (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
  1734. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1735. /* save old pc */
  1736. kvm_write_c0_guest_epc(cop0, arch->pc);
  1737. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1738. if (cause & CAUSEF_BD)
  1739. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1740. else
  1741. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1742. kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n",
  1743. arch->pc);
  1744. /* Set PC to the exception entry point */
  1745. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1746. } else {
  1747. kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n",
  1748. arch->pc);
  1749. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1750. }
  1751. kvm_change_c0_guest_cause(cop0, (0xff),
  1752. (EXCCODE_TLBS << CAUSEB_EXCCODE));
  1753. /* setup badvaddr, context and entryhi registers for the guest */
  1754. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1755. /* XXXKYMA: is the context register used by linux??? */
  1756. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1757. /* Blow away the shadow host TLBs */
  1758. kvm_mips_flush_host_tlb(1);
  1759. return EMULATE_DONE;
  1760. }
  1761. /* TLBMOD: store into address matching TLB with Dirty bit off */
  1762. enum emulation_result kvm_mips_handle_tlbmod(u32 cause, u32 *opc,
  1763. struct kvm_run *run,
  1764. struct kvm_vcpu *vcpu)
  1765. {
  1766. enum emulation_result er = EMULATE_DONE;
  1767. #ifdef DEBUG
  1768. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1769. unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
  1770. (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
  1771. int index;
  1772. /* If address not in the guest TLB, then we are in trouble */
  1773. index = kvm_mips_guest_tlb_lookup(vcpu, entryhi);
  1774. if (index < 0) {
  1775. /* XXXKYMA Invalidate and retry */
  1776. kvm_mips_host_tlb_inv(vcpu, vcpu->arch.host_cp0_badvaddr);
  1777. kvm_err("%s: host got TLBMOD for %#lx but entry not present in Guest TLB\n",
  1778. __func__, entryhi);
  1779. kvm_mips_dump_guest_tlbs(vcpu);
  1780. kvm_mips_dump_host_tlbs();
  1781. return EMULATE_FAIL;
  1782. }
  1783. #endif
  1784. er = kvm_mips_emulate_tlbmod(cause, opc, run, vcpu);
  1785. return er;
  1786. }
  1787. enum emulation_result kvm_mips_emulate_tlbmod(u32 cause,
  1788. u32 *opc,
  1789. struct kvm_run *run,
  1790. struct kvm_vcpu *vcpu)
  1791. {
  1792. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1793. unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
  1794. (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
  1795. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1796. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1797. /* save old pc */
  1798. kvm_write_c0_guest_epc(cop0, arch->pc);
  1799. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1800. if (cause & CAUSEF_BD)
  1801. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1802. else
  1803. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1804. kvm_debug("[EXL == 0] Delivering TLB MOD @ pc %#lx\n",
  1805. arch->pc);
  1806. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1807. } else {
  1808. kvm_debug("[EXL == 1] Delivering TLB MOD @ pc %#lx\n",
  1809. arch->pc);
  1810. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1811. }
  1812. kvm_change_c0_guest_cause(cop0, (0xff),
  1813. (EXCCODE_MOD << CAUSEB_EXCCODE));
  1814. /* setup badvaddr, context and entryhi registers for the guest */
  1815. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1816. /* XXXKYMA: is the context register used by linux??? */
  1817. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1818. /* Blow away the shadow host TLBs */
  1819. kvm_mips_flush_host_tlb(1);
  1820. return EMULATE_DONE;
  1821. }
  1822. enum emulation_result kvm_mips_emulate_fpu_exc(u32 cause,
  1823. u32 *opc,
  1824. struct kvm_run *run,
  1825. struct kvm_vcpu *vcpu)
  1826. {
  1827. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1828. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1829. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1830. /* save old pc */
  1831. kvm_write_c0_guest_epc(cop0, arch->pc);
  1832. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1833. if (cause & CAUSEF_BD)
  1834. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1835. else
  1836. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1837. }
  1838. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1839. kvm_change_c0_guest_cause(cop0, (0xff),
  1840. (EXCCODE_CPU << CAUSEB_EXCCODE));
  1841. kvm_change_c0_guest_cause(cop0, (CAUSEF_CE), (0x1 << CAUSEB_CE));
  1842. return EMULATE_DONE;
  1843. }
  1844. enum emulation_result kvm_mips_emulate_ri_exc(u32 cause,
  1845. u32 *opc,
  1846. struct kvm_run *run,
  1847. struct kvm_vcpu *vcpu)
  1848. {
  1849. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1850. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1851. enum emulation_result er = EMULATE_DONE;
  1852. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1853. /* save old pc */
  1854. kvm_write_c0_guest_epc(cop0, arch->pc);
  1855. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1856. if (cause & CAUSEF_BD)
  1857. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1858. else
  1859. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1860. kvm_debug("Delivering RI @ pc %#lx\n", arch->pc);
  1861. kvm_change_c0_guest_cause(cop0, (0xff),
  1862. (EXCCODE_RI << CAUSEB_EXCCODE));
  1863. /* Set PC to the exception entry point */
  1864. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1865. } else {
  1866. kvm_err("Trying to deliver RI when EXL is already set\n");
  1867. er = EMULATE_FAIL;
  1868. }
  1869. return er;
  1870. }
  1871. enum emulation_result kvm_mips_emulate_bp_exc(u32 cause,
  1872. u32 *opc,
  1873. struct kvm_run *run,
  1874. struct kvm_vcpu *vcpu)
  1875. {
  1876. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1877. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1878. enum emulation_result er = EMULATE_DONE;
  1879. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1880. /* save old pc */
  1881. kvm_write_c0_guest_epc(cop0, arch->pc);
  1882. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1883. if (cause & CAUSEF_BD)
  1884. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1885. else
  1886. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1887. kvm_debug("Delivering BP @ pc %#lx\n", arch->pc);
  1888. kvm_change_c0_guest_cause(cop0, (0xff),
  1889. (EXCCODE_BP << CAUSEB_EXCCODE));
  1890. /* Set PC to the exception entry point */
  1891. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1892. } else {
  1893. kvm_err("Trying to deliver BP when EXL is already set\n");
  1894. er = EMULATE_FAIL;
  1895. }
  1896. return er;
  1897. }
  1898. enum emulation_result kvm_mips_emulate_trap_exc(u32 cause,
  1899. u32 *opc,
  1900. struct kvm_run *run,
  1901. struct kvm_vcpu *vcpu)
  1902. {
  1903. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1904. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1905. enum emulation_result er = EMULATE_DONE;
  1906. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1907. /* save old pc */
  1908. kvm_write_c0_guest_epc(cop0, arch->pc);
  1909. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1910. if (cause & CAUSEF_BD)
  1911. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1912. else
  1913. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1914. kvm_debug("Delivering TRAP @ pc %#lx\n", arch->pc);
  1915. kvm_change_c0_guest_cause(cop0, (0xff),
  1916. (EXCCODE_TR << CAUSEB_EXCCODE));
  1917. /* Set PC to the exception entry point */
  1918. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1919. } else {
  1920. kvm_err("Trying to deliver TRAP when EXL is already set\n");
  1921. er = EMULATE_FAIL;
  1922. }
  1923. return er;
  1924. }
  1925. enum emulation_result kvm_mips_emulate_msafpe_exc(u32 cause,
  1926. u32 *opc,
  1927. struct kvm_run *run,
  1928. struct kvm_vcpu *vcpu)
  1929. {
  1930. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1931. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1932. enum emulation_result er = EMULATE_DONE;
  1933. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1934. /* save old pc */
  1935. kvm_write_c0_guest_epc(cop0, arch->pc);
  1936. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1937. if (cause & CAUSEF_BD)
  1938. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1939. else
  1940. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1941. kvm_debug("Delivering MSAFPE @ pc %#lx\n", arch->pc);
  1942. kvm_change_c0_guest_cause(cop0, (0xff),
  1943. (EXCCODE_MSAFPE << CAUSEB_EXCCODE));
  1944. /* Set PC to the exception entry point */
  1945. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1946. } else {
  1947. kvm_err("Trying to deliver MSAFPE when EXL is already set\n");
  1948. er = EMULATE_FAIL;
  1949. }
  1950. return er;
  1951. }
  1952. enum emulation_result kvm_mips_emulate_fpe_exc(u32 cause,
  1953. u32 *opc,
  1954. struct kvm_run *run,
  1955. struct kvm_vcpu *vcpu)
  1956. {
  1957. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1958. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1959. enum emulation_result er = EMULATE_DONE;
  1960. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1961. /* save old pc */
  1962. kvm_write_c0_guest_epc(cop0, arch->pc);
  1963. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1964. if (cause & CAUSEF_BD)
  1965. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1966. else
  1967. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1968. kvm_debug("Delivering FPE @ pc %#lx\n", arch->pc);
  1969. kvm_change_c0_guest_cause(cop0, (0xff),
  1970. (EXCCODE_FPE << CAUSEB_EXCCODE));
  1971. /* Set PC to the exception entry point */
  1972. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1973. } else {
  1974. kvm_err("Trying to deliver FPE when EXL is already set\n");
  1975. er = EMULATE_FAIL;
  1976. }
  1977. return er;
  1978. }
  1979. enum emulation_result kvm_mips_emulate_msadis_exc(u32 cause,
  1980. u32 *opc,
  1981. struct kvm_run *run,
  1982. struct kvm_vcpu *vcpu)
  1983. {
  1984. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1985. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1986. enum emulation_result er = EMULATE_DONE;
  1987. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1988. /* save old pc */
  1989. kvm_write_c0_guest_epc(cop0, arch->pc);
  1990. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1991. if (cause & CAUSEF_BD)
  1992. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1993. else
  1994. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1995. kvm_debug("Delivering MSADIS @ pc %#lx\n", arch->pc);
  1996. kvm_change_c0_guest_cause(cop0, (0xff),
  1997. (EXCCODE_MSADIS << CAUSEB_EXCCODE));
  1998. /* Set PC to the exception entry point */
  1999. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  2000. } else {
  2001. kvm_err("Trying to deliver MSADIS when EXL is already set\n");
  2002. er = EMULATE_FAIL;
  2003. }
  2004. return er;
  2005. }
  2006. enum emulation_result kvm_mips_handle_ri(u32 cause, u32 *opc,
  2007. struct kvm_run *run,
  2008. struct kvm_vcpu *vcpu)
  2009. {
  2010. struct mips_coproc *cop0 = vcpu->arch.cop0;
  2011. struct kvm_vcpu_arch *arch = &vcpu->arch;
  2012. enum emulation_result er = EMULATE_DONE;
  2013. unsigned long curr_pc;
  2014. union mips_instruction inst;
  2015. /*
  2016. * Update PC and hold onto current PC in case there is
  2017. * an error and we want to rollback the PC
  2018. */
  2019. curr_pc = vcpu->arch.pc;
  2020. er = update_pc(vcpu, cause);
  2021. if (er == EMULATE_FAIL)
  2022. return er;
  2023. /* Fetch the instruction. */
  2024. if (cause & CAUSEF_BD)
  2025. opc += 1;
  2026. inst.word = kvm_get_inst(opc, vcpu);
  2027. if (inst.word == KVM_INVALID_INST) {
  2028. kvm_err("%s: Cannot get inst @ %p\n", __func__, opc);
  2029. return EMULATE_FAIL;
  2030. }
  2031. if (inst.r_format.opcode == spec3_op &&
  2032. inst.r_format.func == rdhwr_op &&
  2033. inst.r_format.rs == 0 &&
  2034. (inst.r_format.re >> 3) == 0) {
  2035. int usermode = !KVM_GUEST_KERNEL_MODE(vcpu);
  2036. int rd = inst.r_format.rd;
  2037. int rt = inst.r_format.rt;
  2038. int sel = inst.r_format.re & 0x7;
  2039. /* If usermode, check RDHWR rd is allowed by guest HWREna */
  2040. if (usermode && !(kvm_read_c0_guest_hwrena(cop0) & BIT(rd))) {
  2041. kvm_debug("RDHWR %#x disallowed by HWREna @ %p\n",
  2042. rd, opc);
  2043. goto emulate_ri;
  2044. }
  2045. switch (rd) {
  2046. case MIPS_HWR_CPUNUM: /* CPU number */
  2047. arch->gprs[rt] = vcpu->vcpu_id;
  2048. break;
  2049. case MIPS_HWR_SYNCISTEP: /* SYNCI length */
  2050. arch->gprs[rt] = min(current_cpu_data.dcache.linesz,
  2051. current_cpu_data.icache.linesz);
  2052. break;
  2053. case MIPS_HWR_CC: /* Read count register */
  2054. arch->gprs[rt] = (s32)kvm_mips_read_count(vcpu);
  2055. break;
  2056. case MIPS_HWR_CCRES: /* Count register resolution */
  2057. switch (current_cpu_data.cputype) {
  2058. case CPU_20KC:
  2059. case CPU_25KF:
  2060. arch->gprs[rt] = 1;
  2061. break;
  2062. default:
  2063. arch->gprs[rt] = 2;
  2064. }
  2065. break;
  2066. case MIPS_HWR_ULR: /* Read UserLocal register */
  2067. arch->gprs[rt] = kvm_read_c0_guest_userlocal(cop0);
  2068. break;
  2069. default:
  2070. kvm_debug("RDHWR %#x not supported @ %p\n", rd, opc);
  2071. goto emulate_ri;
  2072. }
  2073. trace_kvm_hwr(vcpu, KVM_TRACE_RDHWR, KVM_TRACE_HWR(rd, sel),
  2074. vcpu->arch.gprs[rt]);
  2075. } else {
  2076. kvm_debug("Emulate RI not supported @ %p: %#x\n",
  2077. opc, inst.word);
  2078. goto emulate_ri;
  2079. }
  2080. return EMULATE_DONE;
  2081. emulate_ri:
  2082. /*
  2083. * Rollback PC (if in branch delay slot then the PC already points to
  2084. * branch target), and pass the RI exception to the guest OS.
  2085. */
  2086. vcpu->arch.pc = curr_pc;
  2087. return kvm_mips_emulate_ri_exc(cause, opc, run, vcpu);
  2088. }
  2089. enum emulation_result kvm_mips_complete_mmio_load(struct kvm_vcpu *vcpu,
  2090. struct kvm_run *run)
  2091. {
  2092. unsigned long *gpr = &vcpu->arch.gprs[vcpu->arch.io_gpr];
  2093. enum emulation_result er = EMULATE_DONE;
  2094. if (run->mmio.len > sizeof(*gpr)) {
  2095. kvm_err("Bad MMIO length: %d", run->mmio.len);
  2096. er = EMULATE_FAIL;
  2097. goto done;
  2098. }
  2099. er = update_pc(vcpu, vcpu->arch.pending_load_cause);
  2100. if (er == EMULATE_FAIL)
  2101. return er;
  2102. switch (run->mmio.len) {
  2103. case 4:
  2104. *gpr = *(s32 *) run->mmio.data;
  2105. break;
  2106. case 2:
  2107. if (vcpu->mmio_needed == 2)
  2108. *gpr = *(s16 *) run->mmio.data;
  2109. else
  2110. *gpr = *(u16 *)run->mmio.data;
  2111. break;
  2112. case 1:
  2113. if (vcpu->mmio_needed == 2)
  2114. *gpr = *(s8 *) run->mmio.data;
  2115. else
  2116. *gpr = *(u8 *) run->mmio.data;
  2117. break;
  2118. }
  2119. if (vcpu->arch.pending_load_cause & CAUSEF_BD)
  2120. kvm_debug("[%#lx] Completing %d byte BD Load to gpr %d (0x%08lx) type %d\n",
  2121. vcpu->arch.pc, run->mmio.len, vcpu->arch.io_gpr, *gpr,
  2122. vcpu->mmio_needed);
  2123. done:
  2124. return er;
  2125. }
  2126. static enum emulation_result kvm_mips_emulate_exc(u32 cause,
  2127. u32 *opc,
  2128. struct kvm_run *run,
  2129. struct kvm_vcpu *vcpu)
  2130. {
  2131. u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
  2132. struct mips_coproc *cop0 = vcpu->arch.cop0;
  2133. struct kvm_vcpu_arch *arch = &vcpu->arch;
  2134. enum emulation_result er = EMULATE_DONE;
  2135. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  2136. /* save old pc */
  2137. kvm_write_c0_guest_epc(cop0, arch->pc);
  2138. kvm_set_c0_guest_status(cop0, ST0_EXL);
  2139. if (cause & CAUSEF_BD)
  2140. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  2141. else
  2142. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  2143. kvm_change_c0_guest_cause(cop0, (0xff),
  2144. (exccode << CAUSEB_EXCCODE));
  2145. /* Set PC to the exception entry point */
  2146. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  2147. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  2148. kvm_debug("Delivering EXC %d @ pc %#lx, badVaddr: %#lx\n",
  2149. exccode, kvm_read_c0_guest_epc(cop0),
  2150. kvm_read_c0_guest_badvaddr(cop0));
  2151. } else {
  2152. kvm_err("Trying to deliver EXC when EXL is already set\n");
  2153. er = EMULATE_FAIL;
  2154. }
  2155. return er;
  2156. }
  2157. enum emulation_result kvm_mips_check_privilege(u32 cause,
  2158. u32 *opc,
  2159. struct kvm_run *run,
  2160. struct kvm_vcpu *vcpu)
  2161. {
  2162. enum emulation_result er = EMULATE_DONE;
  2163. u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
  2164. unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
  2165. int usermode = !KVM_GUEST_KERNEL_MODE(vcpu);
  2166. if (usermode) {
  2167. switch (exccode) {
  2168. case EXCCODE_INT:
  2169. case EXCCODE_SYS:
  2170. case EXCCODE_BP:
  2171. case EXCCODE_RI:
  2172. case EXCCODE_TR:
  2173. case EXCCODE_MSAFPE:
  2174. case EXCCODE_FPE:
  2175. case EXCCODE_MSADIS:
  2176. break;
  2177. case EXCCODE_CPU:
  2178. if (((cause & CAUSEF_CE) >> CAUSEB_CE) == 0)
  2179. er = EMULATE_PRIV_FAIL;
  2180. break;
  2181. case EXCCODE_MOD:
  2182. break;
  2183. case EXCCODE_TLBL:
  2184. /*
  2185. * We we are accessing Guest kernel space, then send an
  2186. * address error exception to the guest
  2187. */
  2188. if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
  2189. kvm_debug("%s: LD MISS @ %#lx\n", __func__,
  2190. badvaddr);
  2191. cause &= ~0xff;
  2192. cause |= (EXCCODE_ADEL << CAUSEB_EXCCODE);
  2193. er = EMULATE_PRIV_FAIL;
  2194. }
  2195. break;
  2196. case EXCCODE_TLBS:
  2197. /*
  2198. * We we are accessing Guest kernel space, then send an
  2199. * address error exception to the guest
  2200. */
  2201. if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
  2202. kvm_debug("%s: ST MISS @ %#lx\n", __func__,
  2203. badvaddr);
  2204. cause &= ~0xff;
  2205. cause |= (EXCCODE_ADES << CAUSEB_EXCCODE);
  2206. er = EMULATE_PRIV_FAIL;
  2207. }
  2208. break;
  2209. case EXCCODE_ADES:
  2210. kvm_debug("%s: address error ST @ %#lx\n", __func__,
  2211. badvaddr);
  2212. if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
  2213. cause &= ~0xff;
  2214. cause |= (EXCCODE_TLBS << CAUSEB_EXCCODE);
  2215. }
  2216. er = EMULATE_PRIV_FAIL;
  2217. break;
  2218. case EXCCODE_ADEL:
  2219. kvm_debug("%s: address error LD @ %#lx\n", __func__,
  2220. badvaddr);
  2221. if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
  2222. cause &= ~0xff;
  2223. cause |= (EXCCODE_TLBL << CAUSEB_EXCCODE);
  2224. }
  2225. er = EMULATE_PRIV_FAIL;
  2226. break;
  2227. default:
  2228. er = EMULATE_PRIV_FAIL;
  2229. break;
  2230. }
  2231. }
  2232. if (er == EMULATE_PRIV_FAIL)
  2233. kvm_mips_emulate_exc(cause, opc, run, vcpu);
  2234. return er;
  2235. }
  2236. /*
  2237. * User Address (UA) fault, this could happen if
  2238. * (1) TLB entry not present/valid in both Guest and shadow host TLBs, in this
  2239. * case we pass on the fault to the guest kernel and let it handle it.
  2240. * (2) TLB entry is present in the Guest TLB but not in the shadow, in this
  2241. * case we inject the TLB from the Guest TLB into the shadow host TLB
  2242. */
  2243. enum emulation_result kvm_mips_handle_tlbmiss(u32 cause,
  2244. u32 *opc,
  2245. struct kvm_run *run,
  2246. struct kvm_vcpu *vcpu)
  2247. {
  2248. enum emulation_result er = EMULATE_DONE;
  2249. u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
  2250. unsigned long va = vcpu->arch.host_cp0_badvaddr;
  2251. int index;
  2252. kvm_debug("kvm_mips_handle_tlbmiss: badvaddr: %#lx\n",
  2253. vcpu->arch.host_cp0_badvaddr);
  2254. /*
  2255. * KVM would not have got the exception if this entry was valid in the
  2256. * shadow host TLB. Check the Guest TLB, if the entry is not there then
  2257. * send the guest an exception. The guest exc handler should then inject
  2258. * an entry into the guest TLB.
  2259. */
  2260. index = kvm_mips_guest_tlb_lookup(vcpu,
  2261. (va & VPN2_MASK) |
  2262. (kvm_read_c0_guest_entryhi(vcpu->arch.cop0) &
  2263. KVM_ENTRYHI_ASID));
  2264. if (index < 0) {
  2265. if (exccode == EXCCODE_TLBL) {
  2266. er = kvm_mips_emulate_tlbmiss_ld(cause, opc, run, vcpu);
  2267. } else if (exccode == EXCCODE_TLBS) {
  2268. er = kvm_mips_emulate_tlbmiss_st(cause, opc, run, vcpu);
  2269. } else {
  2270. kvm_err("%s: invalid exc code: %d\n", __func__,
  2271. exccode);
  2272. er = EMULATE_FAIL;
  2273. }
  2274. } else {
  2275. struct kvm_mips_tlb *tlb = &vcpu->arch.guest_tlb[index];
  2276. /*
  2277. * Check if the entry is valid, if not then setup a TLB invalid
  2278. * exception to the guest
  2279. */
  2280. if (!TLB_IS_VALID(*tlb, va)) {
  2281. if (exccode == EXCCODE_TLBL) {
  2282. er = kvm_mips_emulate_tlbinv_ld(cause, opc, run,
  2283. vcpu);
  2284. } else if (exccode == EXCCODE_TLBS) {
  2285. er = kvm_mips_emulate_tlbinv_st(cause, opc, run,
  2286. vcpu);
  2287. } else {
  2288. kvm_err("%s: invalid exc code: %d\n", __func__,
  2289. exccode);
  2290. er = EMULATE_FAIL;
  2291. }
  2292. } else {
  2293. kvm_debug("Injecting hi: %#lx, lo0: %#lx, lo1: %#lx into shadow host TLB\n",
  2294. tlb->tlb_hi, tlb->tlb_lo[0], tlb->tlb_lo[1]);
  2295. /*
  2296. * OK we have a Guest TLB entry, now inject it into the
  2297. * shadow host TLB
  2298. */
  2299. if (kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb)) {
  2300. kvm_err("%s: handling mapped seg tlb fault for %lx, index: %u, vcpu: %p, ASID: %#lx\n",
  2301. __func__, va, index, vcpu,
  2302. read_c0_entryhi());
  2303. er = EMULATE_FAIL;
  2304. }
  2305. }
  2306. }
  2307. return er;
  2308. }