emulate.c 71 KB

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