emulate.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830
  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. 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. 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_restore_hrtimer() - Restore hrtimer after a gap, updating expiry.
  467. * @vcpu: Virtual CPU.
  468. * @before: Time before Count was saved, lower bound of drift calculation.
  469. * @count: CP0_Count at point of restore.
  470. * @min_drift: Minimum amount of drift permitted before correction.
  471. * Must be <= 0.
  472. *
  473. * Restores the timer from a particular @count, accounting for drift. This can
  474. * be used in conjunction with kvm_mips_freeze_timer() when a hardware timer is
  475. * to be used for a period of time, but the exact ktime corresponding to the
  476. * final Count that must be restored is not known.
  477. *
  478. * It is gauranteed that a timer interrupt immediately after restore will be
  479. * handled, but not if CP0_Compare is exactly at @count. That case should
  480. * already be handled when the hardware timer state is saved.
  481. *
  482. * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is not
  483. * stopped).
  484. *
  485. * Returns: Amount of correction to count_bias due to drift.
  486. */
  487. int kvm_mips_restore_hrtimer(struct kvm_vcpu *vcpu, ktime_t before,
  488. u32 count, int min_drift)
  489. {
  490. ktime_t now, count_time;
  491. u32 now_count, before_count;
  492. u64 delta;
  493. int drift, ret = 0;
  494. /* Calculate expected count at before */
  495. before_count = vcpu->arch.count_bias +
  496. kvm_mips_ktime_to_count(vcpu, before);
  497. /*
  498. * Detect significantly negative drift, where count is lower than
  499. * expected. Some negative drift is expected when hardware counter is
  500. * set after kvm_mips_freeze_timer(), and it is harmless to allow the
  501. * time to jump forwards a little, within reason. If the drift is too
  502. * significant, adjust the bias to avoid a big Guest.CP0_Count jump.
  503. */
  504. drift = count - before_count;
  505. if (drift < min_drift) {
  506. count_time = before;
  507. vcpu->arch.count_bias += drift;
  508. ret = drift;
  509. goto resume;
  510. }
  511. /* Calculate expected count right now */
  512. now = ktime_get();
  513. now_count = vcpu->arch.count_bias + kvm_mips_ktime_to_count(vcpu, now);
  514. /*
  515. * Detect positive drift, where count is higher than expected, and
  516. * adjust the bias to avoid guest time going backwards.
  517. */
  518. drift = count - now_count;
  519. if (drift > 0) {
  520. count_time = now;
  521. vcpu->arch.count_bias += drift;
  522. ret = drift;
  523. goto resume;
  524. }
  525. /* Subtract nanosecond delta to find ktime when count was read */
  526. delta = (u64)(u32)(now_count - count);
  527. delta = div_u64(delta * NSEC_PER_SEC, vcpu->arch.count_hz);
  528. count_time = ktime_sub_ns(now, delta);
  529. resume:
  530. /* Resume using the calculated ktime */
  531. kvm_mips_resume_hrtimer(vcpu, count_time, count);
  532. return ret;
  533. }
  534. /**
  535. * kvm_mips_write_count() - Modify the count and update timer.
  536. * @vcpu: Virtual CPU.
  537. * @count: Guest CP0_Count value to set.
  538. *
  539. * Sets the CP0_Count value and updates the timer accordingly.
  540. */
  541. void kvm_mips_write_count(struct kvm_vcpu *vcpu, u32 count)
  542. {
  543. struct mips_coproc *cop0 = vcpu->arch.cop0;
  544. ktime_t now;
  545. /* Calculate bias */
  546. now = kvm_mips_count_time(vcpu);
  547. vcpu->arch.count_bias = count - kvm_mips_ktime_to_count(vcpu, now);
  548. if (kvm_mips_count_disabled(vcpu))
  549. /* The timer's disabled, adjust the static count */
  550. kvm_write_c0_guest_count(cop0, count);
  551. else
  552. /* Update timeout */
  553. kvm_mips_resume_hrtimer(vcpu, now, count);
  554. }
  555. /**
  556. * kvm_mips_init_count() - Initialise timer.
  557. * @vcpu: Virtual CPU.
  558. * @count_hz: Frequency of timer.
  559. *
  560. * Initialise the timer to the specified frequency, zero it, and set it going if
  561. * it's enabled.
  562. */
  563. void kvm_mips_init_count(struct kvm_vcpu *vcpu, unsigned long count_hz)
  564. {
  565. vcpu->arch.count_hz = count_hz;
  566. vcpu->arch.count_period = div_u64((u64)NSEC_PER_SEC << 32, count_hz);
  567. vcpu->arch.count_dyn_bias = 0;
  568. /* Starting at 0 */
  569. kvm_mips_write_count(vcpu, 0);
  570. }
  571. /**
  572. * kvm_mips_set_count_hz() - Update the frequency of the timer.
  573. * @vcpu: Virtual CPU.
  574. * @count_hz: Frequency of CP0_Count timer in Hz.
  575. *
  576. * Change the frequency of the CP0_Count timer. This is done atomically so that
  577. * CP0_Count is continuous and no timer interrupt is lost.
  578. *
  579. * Returns: -EINVAL if @count_hz is out of range.
  580. * 0 on success.
  581. */
  582. int kvm_mips_set_count_hz(struct kvm_vcpu *vcpu, s64 count_hz)
  583. {
  584. struct mips_coproc *cop0 = vcpu->arch.cop0;
  585. int dc;
  586. ktime_t now;
  587. u32 count;
  588. /* ensure the frequency is in a sensible range... */
  589. if (count_hz <= 0 || count_hz > NSEC_PER_SEC)
  590. return -EINVAL;
  591. /* ... and has actually changed */
  592. if (vcpu->arch.count_hz == count_hz)
  593. return 0;
  594. /* Safely freeze timer so we can keep it continuous */
  595. dc = kvm_mips_count_disabled(vcpu);
  596. if (dc) {
  597. now = kvm_mips_count_time(vcpu);
  598. count = kvm_read_c0_guest_count(cop0);
  599. } else {
  600. now = kvm_mips_freeze_hrtimer(vcpu, &count);
  601. }
  602. /* Update the frequency */
  603. vcpu->arch.count_hz = count_hz;
  604. vcpu->arch.count_period = div_u64((u64)NSEC_PER_SEC << 32, count_hz);
  605. vcpu->arch.count_dyn_bias = 0;
  606. /* Calculate adjusted bias so dynamic count is unchanged */
  607. vcpu->arch.count_bias = count - kvm_mips_ktime_to_count(vcpu, now);
  608. /* Update and resume hrtimer */
  609. if (!dc)
  610. kvm_mips_resume_hrtimer(vcpu, now, count);
  611. return 0;
  612. }
  613. /**
  614. * kvm_mips_write_compare() - Modify compare and update timer.
  615. * @vcpu: Virtual CPU.
  616. * @compare: New CP0_Compare value.
  617. * @ack: Whether to acknowledge timer interrupt.
  618. *
  619. * Update CP0_Compare to a new value and update the timeout.
  620. * If @ack, atomically acknowledge any pending timer interrupt, otherwise ensure
  621. * any pending timer interrupt is preserved.
  622. */
  623. void kvm_mips_write_compare(struct kvm_vcpu *vcpu, u32 compare, bool ack)
  624. {
  625. struct mips_coproc *cop0 = vcpu->arch.cop0;
  626. int dc;
  627. u32 old_compare = kvm_read_c0_guest_compare(cop0);
  628. s32 delta = compare - old_compare;
  629. u32 cause;
  630. ktime_t now = ktime_set(0, 0); /* silence bogus GCC warning */
  631. u32 count;
  632. /* if unchanged, must just be an ack */
  633. if (old_compare == compare) {
  634. if (!ack)
  635. return;
  636. kvm_mips_callbacks->dequeue_timer_int(vcpu);
  637. kvm_write_c0_guest_compare(cop0, compare);
  638. return;
  639. }
  640. /*
  641. * If guest CP0_Compare moves forward, CP0_GTOffset should be adjusted
  642. * too to prevent guest CP0_Count hitting guest CP0_Compare.
  643. *
  644. * The new GTOffset corresponds to the new value of CP0_Compare, and is
  645. * set prior to it being written into the guest context. We disable
  646. * preemption until the new value is written to prevent restore of a
  647. * GTOffset corresponding to the old CP0_Compare value.
  648. */
  649. if (IS_ENABLED(CONFIG_KVM_MIPS_VZ) && delta > 0) {
  650. preempt_disable();
  651. write_c0_gtoffset(compare - read_c0_count());
  652. back_to_back_c0_hazard();
  653. }
  654. /* freeze_hrtimer() takes care of timer interrupts <= count */
  655. dc = kvm_mips_count_disabled(vcpu);
  656. if (!dc)
  657. now = kvm_mips_freeze_hrtimer(vcpu, &count);
  658. if (ack)
  659. kvm_mips_callbacks->dequeue_timer_int(vcpu);
  660. else if (IS_ENABLED(CONFIG_KVM_MIPS_VZ))
  661. /*
  662. * With VZ, writing CP0_Compare acks (clears) CP0_Cause.TI, so
  663. * preserve guest CP0_Cause.TI if we don't want to ack it.
  664. */
  665. cause = kvm_read_c0_guest_cause(cop0);
  666. kvm_write_c0_guest_compare(cop0, compare);
  667. if (IS_ENABLED(CONFIG_KVM_MIPS_VZ)) {
  668. if (delta > 0)
  669. preempt_enable();
  670. back_to_back_c0_hazard();
  671. if (!ack && cause & CAUSEF_TI)
  672. kvm_write_c0_guest_cause(cop0, cause);
  673. }
  674. /* resume_hrtimer() takes care of timer interrupts > count */
  675. if (!dc)
  676. kvm_mips_resume_hrtimer(vcpu, now, count);
  677. /*
  678. * If guest CP0_Compare is moving backward, we delay CP0_GTOffset change
  679. * until after the new CP0_Compare is written, otherwise new guest
  680. * CP0_Count could hit new guest CP0_Compare.
  681. */
  682. if (IS_ENABLED(CONFIG_KVM_MIPS_VZ) && delta <= 0)
  683. write_c0_gtoffset(compare - read_c0_count());
  684. }
  685. /**
  686. * kvm_mips_count_disable() - Disable count.
  687. * @vcpu: Virtual CPU.
  688. *
  689. * Disable the CP0_Count timer. A timer interrupt on or before the final stop
  690. * time will be handled but not after.
  691. *
  692. * Assumes CP0_Count was previously enabled but now Guest.CP0_Cause.DC or
  693. * count_ctl.DC has been set (count disabled).
  694. *
  695. * Returns: The time that the timer was stopped.
  696. */
  697. static ktime_t kvm_mips_count_disable(struct kvm_vcpu *vcpu)
  698. {
  699. struct mips_coproc *cop0 = vcpu->arch.cop0;
  700. u32 count;
  701. ktime_t now;
  702. /* Stop hrtimer */
  703. hrtimer_cancel(&vcpu->arch.comparecount_timer);
  704. /* Set the static count from the dynamic count, handling pending TI */
  705. now = ktime_get();
  706. count = kvm_mips_read_count_running(vcpu, now);
  707. kvm_write_c0_guest_count(cop0, count);
  708. return now;
  709. }
  710. /**
  711. * kvm_mips_count_disable_cause() - Disable count using CP0_Cause.DC.
  712. * @vcpu: Virtual CPU.
  713. *
  714. * Disable the CP0_Count timer and set CP0_Cause.DC. A timer interrupt on or
  715. * before the final stop time will be handled if the timer isn't disabled by
  716. * count_ctl.DC, but not after.
  717. *
  718. * Assumes CP0_Cause.DC is clear (count enabled).
  719. */
  720. void kvm_mips_count_disable_cause(struct kvm_vcpu *vcpu)
  721. {
  722. struct mips_coproc *cop0 = vcpu->arch.cop0;
  723. kvm_set_c0_guest_cause(cop0, CAUSEF_DC);
  724. if (!(vcpu->arch.count_ctl & KVM_REG_MIPS_COUNT_CTL_DC))
  725. kvm_mips_count_disable(vcpu);
  726. }
  727. /**
  728. * kvm_mips_count_enable_cause() - Enable count using CP0_Cause.DC.
  729. * @vcpu: Virtual CPU.
  730. *
  731. * Enable the CP0_Count timer and clear CP0_Cause.DC. A timer interrupt after
  732. * the start time will be handled if the timer isn't disabled by count_ctl.DC,
  733. * potentially before even returning, so the caller should be careful with
  734. * ordering of CP0_Cause modifications so as not to lose it.
  735. *
  736. * Assumes CP0_Cause.DC is set (count disabled).
  737. */
  738. void kvm_mips_count_enable_cause(struct kvm_vcpu *vcpu)
  739. {
  740. struct mips_coproc *cop0 = vcpu->arch.cop0;
  741. u32 count;
  742. kvm_clear_c0_guest_cause(cop0, CAUSEF_DC);
  743. /*
  744. * Set the dynamic count to match the static count.
  745. * This starts the hrtimer if count_ctl.DC allows it.
  746. * Otherwise it conveniently updates the biases.
  747. */
  748. count = kvm_read_c0_guest_count(cop0);
  749. kvm_mips_write_count(vcpu, count);
  750. }
  751. /**
  752. * kvm_mips_set_count_ctl() - Update the count control KVM register.
  753. * @vcpu: Virtual CPU.
  754. * @count_ctl: Count control register new value.
  755. *
  756. * Set the count control KVM register. The timer is updated accordingly.
  757. *
  758. * Returns: -EINVAL if reserved bits are set.
  759. * 0 on success.
  760. */
  761. int kvm_mips_set_count_ctl(struct kvm_vcpu *vcpu, s64 count_ctl)
  762. {
  763. struct mips_coproc *cop0 = vcpu->arch.cop0;
  764. s64 changed = count_ctl ^ vcpu->arch.count_ctl;
  765. s64 delta;
  766. ktime_t expire, now;
  767. u32 count, compare;
  768. /* Only allow defined bits to be changed */
  769. if (changed & ~(s64)(KVM_REG_MIPS_COUNT_CTL_DC))
  770. return -EINVAL;
  771. /* Apply new value */
  772. vcpu->arch.count_ctl = count_ctl;
  773. /* Master CP0_Count disable */
  774. if (changed & KVM_REG_MIPS_COUNT_CTL_DC) {
  775. /* Is CP0_Cause.DC already disabling CP0_Count? */
  776. if (kvm_read_c0_guest_cause(cop0) & CAUSEF_DC) {
  777. if (count_ctl & KVM_REG_MIPS_COUNT_CTL_DC)
  778. /* Just record the current time */
  779. vcpu->arch.count_resume = ktime_get();
  780. } else if (count_ctl & KVM_REG_MIPS_COUNT_CTL_DC) {
  781. /* disable timer and record current time */
  782. vcpu->arch.count_resume = kvm_mips_count_disable(vcpu);
  783. } else {
  784. /*
  785. * Calculate timeout relative to static count at resume
  786. * time (wrap 0 to 2^32).
  787. */
  788. count = kvm_read_c0_guest_count(cop0);
  789. compare = kvm_read_c0_guest_compare(cop0);
  790. delta = (u64)(u32)(compare - count - 1) + 1;
  791. delta = div_u64(delta * NSEC_PER_SEC,
  792. vcpu->arch.count_hz);
  793. expire = ktime_add_ns(vcpu->arch.count_resume, delta);
  794. /* Handle pending interrupt */
  795. now = ktime_get();
  796. if (ktime_compare(now, expire) >= 0)
  797. /* Nothing should be waiting on the timeout */
  798. kvm_mips_callbacks->queue_timer_int(vcpu);
  799. /* Resume hrtimer without changing bias */
  800. count = kvm_mips_read_count_running(vcpu, now);
  801. kvm_mips_resume_hrtimer(vcpu, now, count);
  802. }
  803. }
  804. return 0;
  805. }
  806. /**
  807. * kvm_mips_set_count_resume() - Update the count resume KVM register.
  808. * @vcpu: Virtual CPU.
  809. * @count_resume: Count resume register new value.
  810. *
  811. * Set the count resume KVM register.
  812. *
  813. * Returns: -EINVAL if out of valid range (0..now).
  814. * 0 on success.
  815. */
  816. int kvm_mips_set_count_resume(struct kvm_vcpu *vcpu, s64 count_resume)
  817. {
  818. /*
  819. * It doesn't make sense for the resume time to be in the future, as it
  820. * would be possible for the next interrupt to be more than a full
  821. * period in the future.
  822. */
  823. if (count_resume < 0 || count_resume > ktime_to_ns(ktime_get()))
  824. return -EINVAL;
  825. vcpu->arch.count_resume = ns_to_ktime(count_resume);
  826. return 0;
  827. }
  828. /**
  829. * kvm_mips_count_timeout() - Push timer forward on timeout.
  830. * @vcpu: Virtual CPU.
  831. *
  832. * Handle an hrtimer event by push the hrtimer forward a period.
  833. *
  834. * Returns: The hrtimer_restart value to return to the hrtimer subsystem.
  835. */
  836. enum hrtimer_restart kvm_mips_count_timeout(struct kvm_vcpu *vcpu)
  837. {
  838. /* Add the Count period to the current expiry time */
  839. hrtimer_add_expires_ns(&vcpu->arch.comparecount_timer,
  840. vcpu->arch.count_period);
  841. return HRTIMER_RESTART;
  842. }
  843. enum emulation_result kvm_mips_emul_eret(struct kvm_vcpu *vcpu)
  844. {
  845. struct mips_coproc *cop0 = vcpu->arch.cop0;
  846. enum emulation_result er = EMULATE_DONE;
  847. if (kvm_read_c0_guest_status(cop0) & ST0_ERL) {
  848. kvm_clear_c0_guest_status(cop0, ST0_ERL);
  849. vcpu->arch.pc = kvm_read_c0_guest_errorepc(cop0);
  850. } else if (kvm_read_c0_guest_status(cop0) & ST0_EXL) {
  851. kvm_debug("[%#lx] ERET to %#lx\n", vcpu->arch.pc,
  852. kvm_read_c0_guest_epc(cop0));
  853. kvm_clear_c0_guest_status(cop0, ST0_EXL);
  854. vcpu->arch.pc = kvm_read_c0_guest_epc(cop0);
  855. } else {
  856. kvm_err("[%#lx] ERET when MIPS_SR_EXL|MIPS_SR_ERL == 0\n",
  857. vcpu->arch.pc);
  858. er = EMULATE_FAIL;
  859. }
  860. return er;
  861. }
  862. enum emulation_result kvm_mips_emul_wait(struct kvm_vcpu *vcpu)
  863. {
  864. kvm_debug("[%#lx] !!!WAIT!!! (%#lx)\n", vcpu->arch.pc,
  865. vcpu->arch.pending_exceptions);
  866. ++vcpu->stat.wait_exits;
  867. trace_kvm_exit(vcpu, KVM_TRACE_EXIT_WAIT);
  868. if (!vcpu->arch.pending_exceptions) {
  869. kvm_vz_lose_htimer(vcpu);
  870. vcpu->arch.wait = 1;
  871. kvm_vcpu_block(vcpu);
  872. /*
  873. * We we are runnable, then definitely go off to user space to
  874. * check if any I/O interrupts are pending.
  875. */
  876. if (kvm_check_request(KVM_REQ_UNHALT, vcpu)) {
  877. kvm_clear_request(KVM_REQ_UNHALT, vcpu);
  878. vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  879. }
  880. }
  881. return EMULATE_DONE;
  882. }
  883. static void kvm_mips_change_entryhi(struct kvm_vcpu *vcpu,
  884. unsigned long entryhi)
  885. {
  886. struct mips_coproc *cop0 = vcpu->arch.cop0;
  887. struct mm_struct *kern_mm = &vcpu->arch.guest_kernel_mm;
  888. int cpu, i;
  889. u32 nasid = entryhi & KVM_ENTRYHI_ASID;
  890. if (((kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID) != nasid)) {
  891. trace_kvm_asid_change(vcpu, kvm_read_c0_guest_entryhi(cop0) &
  892. KVM_ENTRYHI_ASID, nasid);
  893. /*
  894. * Flush entries from the GVA page tables.
  895. * Guest user page table will get flushed lazily on re-entry to
  896. * guest user if the guest ASID actually changes.
  897. */
  898. kvm_mips_flush_gva_pt(kern_mm->pgd, KMF_KERN);
  899. /*
  900. * Regenerate/invalidate kernel MMU context.
  901. * The user MMU context will be regenerated lazily on re-entry
  902. * to guest user if the guest ASID actually changes.
  903. */
  904. preempt_disable();
  905. cpu = smp_processor_id();
  906. get_new_mmu_context(kern_mm, cpu);
  907. for_each_possible_cpu(i)
  908. if (i != cpu)
  909. cpu_context(i, kern_mm) = 0;
  910. preempt_enable();
  911. }
  912. kvm_write_c0_guest_entryhi(cop0, entryhi);
  913. }
  914. enum emulation_result kvm_mips_emul_tlbr(struct kvm_vcpu *vcpu)
  915. {
  916. struct mips_coproc *cop0 = vcpu->arch.cop0;
  917. struct kvm_mips_tlb *tlb;
  918. unsigned long pc = vcpu->arch.pc;
  919. int index;
  920. index = kvm_read_c0_guest_index(cop0);
  921. if (index < 0 || index >= KVM_MIPS_GUEST_TLB_SIZE) {
  922. /* UNDEFINED */
  923. kvm_debug("[%#lx] TLBR Index %#x out of range\n", pc, index);
  924. index &= KVM_MIPS_GUEST_TLB_SIZE - 1;
  925. }
  926. tlb = &vcpu->arch.guest_tlb[index];
  927. kvm_write_c0_guest_pagemask(cop0, tlb->tlb_mask);
  928. kvm_write_c0_guest_entrylo0(cop0, tlb->tlb_lo[0]);
  929. kvm_write_c0_guest_entrylo1(cop0, tlb->tlb_lo[1]);
  930. kvm_mips_change_entryhi(vcpu, tlb->tlb_hi);
  931. return EMULATE_DONE;
  932. }
  933. /**
  934. * kvm_mips_invalidate_guest_tlb() - Indicates a change in guest MMU map.
  935. * @vcpu: VCPU with changed mappings.
  936. * @tlb: TLB entry being removed.
  937. *
  938. * This is called to indicate a single change in guest MMU mappings, so that we
  939. * can arrange TLB flushes on this and other CPUs.
  940. */
  941. static void kvm_mips_invalidate_guest_tlb(struct kvm_vcpu *vcpu,
  942. struct kvm_mips_tlb *tlb)
  943. {
  944. struct mm_struct *kern_mm = &vcpu->arch.guest_kernel_mm;
  945. struct mm_struct *user_mm = &vcpu->arch.guest_user_mm;
  946. int cpu, i;
  947. bool user;
  948. /* No need to flush for entries which are already invalid */
  949. if (!((tlb->tlb_lo[0] | tlb->tlb_lo[1]) & ENTRYLO_V))
  950. return;
  951. /* Don't touch host kernel page tables or TLB mappings */
  952. if ((unsigned long)tlb->tlb_hi > 0x7fffffff)
  953. return;
  954. /* User address space doesn't need flushing for KSeg2/3 changes */
  955. user = tlb->tlb_hi < KVM_GUEST_KSEG0;
  956. preempt_disable();
  957. /* Invalidate page table entries */
  958. kvm_trap_emul_invalidate_gva(vcpu, tlb->tlb_hi & VPN2_MASK, user);
  959. /*
  960. * Probe the shadow host TLB for the entry being overwritten, if one
  961. * matches, invalidate it
  962. */
  963. kvm_mips_host_tlb_inv(vcpu, tlb->tlb_hi, user, true);
  964. /* Invalidate the whole ASID on other CPUs */
  965. cpu = smp_processor_id();
  966. for_each_possible_cpu(i) {
  967. if (i == cpu)
  968. continue;
  969. if (user)
  970. cpu_context(i, user_mm) = 0;
  971. cpu_context(i, kern_mm) = 0;
  972. }
  973. preempt_enable();
  974. }
  975. /* Write Guest TLB Entry @ Index */
  976. enum emulation_result kvm_mips_emul_tlbwi(struct kvm_vcpu *vcpu)
  977. {
  978. struct mips_coproc *cop0 = vcpu->arch.cop0;
  979. int index = kvm_read_c0_guest_index(cop0);
  980. struct kvm_mips_tlb *tlb = NULL;
  981. unsigned long pc = vcpu->arch.pc;
  982. if (index < 0 || index >= KVM_MIPS_GUEST_TLB_SIZE) {
  983. kvm_debug("%s: illegal index: %d\n", __func__, index);
  984. kvm_debug("[%#lx] COP0_TLBWI [%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx, mask: %#lx)\n",
  985. pc, index, kvm_read_c0_guest_entryhi(cop0),
  986. kvm_read_c0_guest_entrylo0(cop0),
  987. kvm_read_c0_guest_entrylo1(cop0),
  988. kvm_read_c0_guest_pagemask(cop0));
  989. index = (index & ~0x80000000) % KVM_MIPS_GUEST_TLB_SIZE;
  990. }
  991. tlb = &vcpu->arch.guest_tlb[index];
  992. kvm_mips_invalidate_guest_tlb(vcpu, tlb);
  993. tlb->tlb_mask = kvm_read_c0_guest_pagemask(cop0);
  994. tlb->tlb_hi = kvm_read_c0_guest_entryhi(cop0);
  995. tlb->tlb_lo[0] = kvm_read_c0_guest_entrylo0(cop0);
  996. tlb->tlb_lo[1] = kvm_read_c0_guest_entrylo1(cop0);
  997. kvm_debug("[%#lx] COP0_TLBWI [%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx, mask: %#lx)\n",
  998. pc, index, kvm_read_c0_guest_entryhi(cop0),
  999. kvm_read_c0_guest_entrylo0(cop0),
  1000. kvm_read_c0_guest_entrylo1(cop0),
  1001. kvm_read_c0_guest_pagemask(cop0));
  1002. return EMULATE_DONE;
  1003. }
  1004. /* Write Guest TLB Entry @ Random Index */
  1005. enum emulation_result kvm_mips_emul_tlbwr(struct kvm_vcpu *vcpu)
  1006. {
  1007. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1008. struct kvm_mips_tlb *tlb = NULL;
  1009. unsigned long pc = vcpu->arch.pc;
  1010. int index;
  1011. get_random_bytes(&index, sizeof(index));
  1012. index &= (KVM_MIPS_GUEST_TLB_SIZE - 1);
  1013. tlb = &vcpu->arch.guest_tlb[index];
  1014. kvm_mips_invalidate_guest_tlb(vcpu, tlb);
  1015. tlb->tlb_mask = kvm_read_c0_guest_pagemask(cop0);
  1016. tlb->tlb_hi = kvm_read_c0_guest_entryhi(cop0);
  1017. tlb->tlb_lo[0] = kvm_read_c0_guest_entrylo0(cop0);
  1018. tlb->tlb_lo[1] = kvm_read_c0_guest_entrylo1(cop0);
  1019. kvm_debug("[%#lx] COP0_TLBWR[%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx)\n",
  1020. pc, index, kvm_read_c0_guest_entryhi(cop0),
  1021. kvm_read_c0_guest_entrylo0(cop0),
  1022. kvm_read_c0_guest_entrylo1(cop0));
  1023. return EMULATE_DONE;
  1024. }
  1025. enum emulation_result kvm_mips_emul_tlbp(struct kvm_vcpu *vcpu)
  1026. {
  1027. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1028. long entryhi = kvm_read_c0_guest_entryhi(cop0);
  1029. unsigned long pc = vcpu->arch.pc;
  1030. int index = -1;
  1031. index = kvm_mips_guest_tlb_lookup(vcpu, entryhi);
  1032. kvm_write_c0_guest_index(cop0, index);
  1033. kvm_debug("[%#lx] COP0_TLBP (entryhi: %#lx), index: %d\n", pc, entryhi,
  1034. index);
  1035. return EMULATE_DONE;
  1036. }
  1037. /**
  1038. * kvm_mips_config1_wrmask() - Find mask of writable bits in guest Config1
  1039. * @vcpu: Virtual CPU.
  1040. *
  1041. * Finds the mask of bits which are writable in the guest's Config1 CP0
  1042. * register, by userland (currently read-only to the guest).
  1043. */
  1044. unsigned int kvm_mips_config1_wrmask(struct kvm_vcpu *vcpu)
  1045. {
  1046. unsigned int mask = 0;
  1047. /* Permit FPU to be present if FPU is supported */
  1048. if (kvm_mips_guest_can_have_fpu(&vcpu->arch))
  1049. mask |= MIPS_CONF1_FP;
  1050. return mask;
  1051. }
  1052. /**
  1053. * kvm_mips_config3_wrmask() - Find mask of writable bits in guest Config3
  1054. * @vcpu: Virtual CPU.
  1055. *
  1056. * Finds the mask of bits which are writable in the guest's Config3 CP0
  1057. * register, by userland (currently read-only to the guest).
  1058. */
  1059. unsigned int kvm_mips_config3_wrmask(struct kvm_vcpu *vcpu)
  1060. {
  1061. /* Config4 and ULRI are optional */
  1062. unsigned int mask = MIPS_CONF_M | MIPS_CONF3_ULRI;
  1063. /* Permit MSA to be present if MSA is supported */
  1064. if (kvm_mips_guest_can_have_msa(&vcpu->arch))
  1065. mask |= MIPS_CONF3_MSA;
  1066. return mask;
  1067. }
  1068. /**
  1069. * kvm_mips_config4_wrmask() - Find mask of writable bits in guest Config4
  1070. * @vcpu: Virtual CPU.
  1071. *
  1072. * Finds the mask of bits which are writable in the guest's Config4 CP0
  1073. * register, by userland (currently read-only to the guest).
  1074. */
  1075. unsigned int kvm_mips_config4_wrmask(struct kvm_vcpu *vcpu)
  1076. {
  1077. /* Config5 is optional */
  1078. unsigned int mask = MIPS_CONF_M;
  1079. /* KScrExist */
  1080. mask |= 0xfc << MIPS_CONF4_KSCREXIST_SHIFT;
  1081. return mask;
  1082. }
  1083. /**
  1084. * kvm_mips_config5_wrmask() - Find mask of writable bits in guest Config5
  1085. * @vcpu: Virtual CPU.
  1086. *
  1087. * Finds the mask of bits which are writable in the guest's Config5 CP0
  1088. * register, by the guest itself.
  1089. */
  1090. unsigned int kvm_mips_config5_wrmask(struct kvm_vcpu *vcpu)
  1091. {
  1092. unsigned int mask = 0;
  1093. /* Permit MSAEn changes if MSA supported and enabled */
  1094. if (kvm_mips_guest_has_msa(&vcpu->arch))
  1095. mask |= MIPS_CONF5_MSAEN;
  1096. /*
  1097. * Permit guest FPU mode changes if FPU is enabled and the relevant
  1098. * feature exists according to FIR register.
  1099. */
  1100. if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
  1101. if (cpu_has_fre)
  1102. mask |= MIPS_CONF5_FRE;
  1103. /* We don't support UFR or UFE */
  1104. }
  1105. return mask;
  1106. }
  1107. enum emulation_result kvm_mips_emulate_CP0(union mips_instruction inst,
  1108. u32 *opc, u32 cause,
  1109. struct kvm_run *run,
  1110. struct kvm_vcpu *vcpu)
  1111. {
  1112. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1113. enum emulation_result er = EMULATE_DONE;
  1114. u32 rt, rd, sel;
  1115. unsigned long curr_pc;
  1116. /*
  1117. * Update PC and hold onto current PC in case there is
  1118. * an error and we want to rollback the PC
  1119. */
  1120. curr_pc = vcpu->arch.pc;
  1121. er = update_pc(vcpu, cause);
  1122. if (er == EMULATE_FAIL)
  1123. return er;
  1124. if (inst.co_format.co) {
  1125. switch (inst.co_format.func) {
  1126. case tlbr_op: /* Read indexed TLB entry */
  1127. er = kvm_mips_emul_tlbr(vcpu);
  1128. break;
  1129. case tlbwi_op: /* Write indexed */
  1130. er = kvm_mips_emul_tlbwi(vcpu);
  1131. break;
  1132. case tlbwr_op: /* Write random */
  1133. er = kvm_mips_emul_tlbwr(vcpu);
  1134. break;
  1135. case tlbp_op: /* TLB Probe */
  1136. er = kvm_mips_emul_tlbp(vcpu);
  1137. break;
  1138. case rfe_op:
  1139. kvm_err("!!!COP0_RFE!!!\n");
  1140. break;
  1141. case eret_op:
  1142. er = kvm_mips_emul_eret(vcpu);
  1143. goto dont_update_pc;
  1144. case wait_op:
  1145. er = kvm_mips_emul_wait(vcpu);
  1146. break;
  1147. case hypcall_op:
  1148. er = kvm_mips_emul_hypcall(vcpu, inst);
  1149. break;
  1150. }
  1151. } else {
  1152. rt = inst.c0r_format.rt;
  1153. rd = inst.c0r_format.rd;
  1154. sel = inst.c0r_format.sel;
  1155. switch (inst.c0r_format.rs) {
  1156. case mfc_op:
  1157. #ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
  1158. cop0->stat[rd][sel]++;
  1159. #endif
  1160. /* Get reg */
  1161. if ((rd == MIPS_CP0_COUNT) && (sel == 0)) {
  1162. vcpu->arch.gprs[rt] =
  1163. (s32)kvm_mips_read_count(vcpu);
  1164. } else if ((rd == MIPS_CP0_ERRCTL) && (sel == 0)) {
  1165. vcpu->arch.gprs[rt] = 0x0;
  1166. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  1167. kvm_mips_trans_mfc0(inst, opc, vcpu);
  1168. #endif
  1169. } else {
  1170. vcpu->arch.gprs[rt] = (s32)cop0->reg[rd][sel];
  1171. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  1172. kvm_mips_trans_mfc0(inst, opc, vcpu);
  1173. #endif
  1174. }
  1175. trace_kvm_hwr(vcpu, KVM_TRACE_MFC0,
  1176. KVM_TRACE_COP0(rd, sel),
  1177. vcpu->arch.gprs[rt]);
  1178. break;
  1179. case dmfc_op:
  1180. vcpu->arch.gprs[rt] = cop0->reg[rd][sel];
  1181. trace_kvm_hwr(vcpu, KVM_TRACE_DMFC0,
  1182. KVM_TRACE_COP0(rd, sel),
  1183. vcpu->arch.gprs[rt]);
  1184. break;
  1185. case mtc_op:
  1186. #ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
  1187. cop0->stat[rd][sel]++;
  1188. #endif
  1189. trace_kvm_hwr(vcpu, KVM_TRACE_MTC0,
  1190. KVM_TRACE_COP0(rd, sel),
  1191. vcpu->arch.gprs[rt]);
  1192. if ((rd == MIPS_CP0_TLB_INDEX)
  1193. && (vcpu->arch.gprs[rt] >=
  1194. KVM_MIPS_GUEST_TLB_SIZE)) {
  1195. kvm_err("Invalid TLB Index: %ld",
  1196. vcpu->arch.gprs[rt]);
  1197. er = EMULATE_FAIL;
  1198. break;
  1199. }
  1200. if ((rd == MIPS_CP0_PRID) && (sel == 1)) {
  1201. /*
  1202. * Preserve core number, and keep the exception
  1203. * base in guest KSeg0.
  1204. */
  1205. kvm_change_c0_guest_ebase(cop0, 0x1ffff000,
  1206. vcpu->arch.gprs[rt]);
  1207. } else if (rd == MIPS_CP0_TLB_HI && sel == 0) {
  1208. kvm_mips_change_entryhi(vcpu,
  1209. vcpu->arch.gprs[rt]);
  1210. }
  1211. /* Are we writing to COUNT */
  1212. else if ((rd == MIPS_CP0_COUNT) && (sel == 0)) {
  1213. kvm_mips_write_count(vcpu, vcpu->arch.gprs[rt]);
  1214. goto done;
  1215. } else if ((rd == MIPS_CP0_COMPARE) && (sel == 0)) {
  1216. /* If we are writing to COMPARE */
  1217. /* Clear pending timer interrupt, if any */
  1218. kvm_mips_write_compare(vcpu,
  1219. vcpu->arch.gprs[rt],
  1220. true);
  1221. } else if ((rd == MIPS_CP0_STATUS) && (sel == 0)) {
  1222. unsigned int old_val, val, change;
  1223. old_val = kvm_read_c0_guest_status(cop0);
  1224. val = vcpu->arch.gprs[rt];
  1225. change = val ^ old_val;
  1226. /* Make sure that the NMI bit is never set */
  1227. val &= ~ST0_NMI;
  1228. /*
  1229. * Don't allow CU1 or FR to be set unless FPU
  1230. * capability enabled and exists in guest
  1231. * configuration.
  1232. */
  1233. if (!kvm_mips_guest_has_fpu(&vcpu->arch))
  1234. val &= ~(ST0_CU1 | ST0_FR);
  1235. /*
  1236. * Also don't allow FR to be set if host doesn't
  1237. * support it.
  1238. */
  1239. if (!(current_cpu_data.fpu_id & MIPS_FPIR_F64))
  1240. val &= ~ST0_FR;
  1241. /* Handle changes in FPU mode */
  1242. preempt_disable();
  1243. /*
  1244. * FPU and Vector register state is made
  1245. * UNPREDICTABLE by a change of FR, so don't
  1246. * even bother saving it.
  1247. */
  1248. if (change & ST0_FR)
  1249. kvm_drop_fpu(vcpu);
  1250. /*
  1251. * If MSA state is already live, it is undefined
  1252. * how it interacts with FR=0 FPU state, and we
  1253. * don't want to hit reserved instruction
  1254. * exceptions trying to save the MSA state later
  1255. * when CU=1 && FR=1, so play it safe and save
  1256. * it first.
  1257. */
  1258. if (change & ST0_CU1 && !(val & ST0_FR) &&
  1259. vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
  1260. kvm_lose_fpu(vcpu);
  1261. /*
  1262. * Propagate CU1 (FPU enable) changes
  1263. * immediately if the FPU context is already
  1264. * loaded. When disabling we leave the context
  1265. * loaded so it can be quickly enabled again in
  1266. * the near future.
  1267. */
  1268. if (change & ST0_CU1 &&
  1269. vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)
  1270. change_c0_status(ST0_CU1, val);
  1271. preempt_enable();
  1272. kvm_write_c0_guest_status(cop0, val);
  1273. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  1274. /*
  1275. * If FPU present, we need CU1/FR bits to take
  1276. * effect fairly soon.
  1277. */
  1278. if (!kvm_mips_guest_has_fpu(&vcpu->arch))
  1279. kvm_mips_trans_mtc0(inst, opc, vcpu);
  1280. #endif
  1281. } else if ((rd == MIPS_CP0_CONFIG) && (sel == 5)) {
  1282. unsigned int old_val, val, change, wrmask;
  1283. old_val = kvm_read_c0_guest_config5(cop0);
  1284. val = vcpu->arch.gprs[rt];
  1285. /* Only a few bits are writable in Config5 */
  1286. wrmask = kvm_mips_config5_wrmask(vcpu);
  1287. change = (val ^ old_val) & wrmask;
  1288. val = old_val ^ change;
  1289. /* Handle changes in FPU/MSA modes */
  1290. preempt_disable();
  1291. /*
  1292. * Propagate FRE changes immediately if the FPU
  1293. * context is already loaded.
  1294. */
  1295. if (change & MIPS_CONF5_FRE &&
  1296. vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)
  1297. change_c0_config5(MIPS_CONF5_FRE, val);
  1298. /*
  1299. * Propagate MSAEn changes immediately if the
  1300. * MSA context is already loaded. When disabling
  1301. * we leave the context loaded so it can be
  1302. * quickly enabled again in the near future.
  1303. */
  1304. if (change & MIPS_CONF5_MSAEN &&
  1305. vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
  1306. change_c0_config5(MIPS_CONF5_MSAEN,
  1307. val);
  1308. preempt_enable();
  1309. kvm_write_c0_guest_config5(cop0, val);
  1310. } else if ((rd == MIPS_CP0_CAUSE) && (sel == 0)) {
  1311. u32 old_cause, new_cause;
  1312. old_cause = kvm_read_c0_guest_cause(cop0);
  1313. new_cause = vcpu->arch.gprs[rt];
  1314. /* Update R/W bits */
  1315. kvm_change_c0_guest_cause(cop0, 0x08800300,
  1316. new_cause);
  1317. /* DC bit enabling/disabling timer? */
  1318. if ((old_cause ^ new_cause) & CAUSEF_DC) {
  1319. if (new_cause & CAUSEF_DC)
  1320. kvm_mips_count_disable_cause(vcpu);
  1321. else
  1322. kvm_mips_count_enable_cause(vcpu);
  1323. }
  1324. } else if ((rd == MIPS_CP0_HWRENA) && (sel == 0)) {
  1325. u32 mask = MIPS_HWRENA_CPUNUM |
  1326. MIPS_HWRENA_SYNCISTEP |
  1327. MIPS_HWRENA_CC |
  1328. MIPS_HWRENA_CCRES;
  1329. if (kvm_read_c0_guest_config3(cop0) &
  1330. MIPS_CONF3_ULRI)
  1331. mask |= MIPS_HWRENA_ULR;
  1332. cop0->reg[rd][sel] = vcpu->arch.gprs[rt] & mask;
  1333. } else {
  1334. cop0->reg[rd][sel] = vcpu->arch.gprs[rt];
  1335. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  1336. kvm_mips_trans_mtc0(inst, opc, vcpu);
  1337. #endif
  1338. }
  1339. break;
  1340. case dmtc_op:
  1341. kvm_err("!!!!!!![%#lx]dmtc_op: rt: %d, rd: %d, sel: %d!!!!!!\n",
  1342. vcpu->arch.pc, rt, rd, sel);
  1343. trace_kvm_hwr(vcpu, KVM_TRACE_DMTC0,
  1344. KVM_TRACE_COP0(rd, sel),
  1345. vcpu->arch.gprs[rt]);
  1346. er = EMULATE_FAIL;
  1347. break;
  1348. case mfmc0_op:
  1349. #ifdef KVM_MIPS_DEBUG_COP0_COUNTERS
  1350. cop0->stat[MIPS_CP0_STATUS][0]++;
  1351. #endif
  1352. if (rt != 0)
  1353. vcpu->arch.gprs[rt] =
  1354. kvm_read_c0_guest_status(cop0);
  1355. /* EI */
  1356. if (inst.mfmc0_format.sc) {
  1357. kvm_debug("[%#lx] mfmc0_op: EI\n",
  1358. vcpu->arch.pc);
  1359. kvm_set_c0_guest_status(cop0, ST0_IE);
  1360. } else {
  1361. kvm_debug("[%#lx] mfmc0_op: DI\n",
  1362. vcpu->arch.pc);
  1363. kvm_clear_c0_guest_status(cop0, ST0_IE);
  1364. }
  1365. break;
  1366. case wrpgpr_op:
  1367. {
  1368. u32 css = cop0->reg[MIPS_CP0_STATUS][2] & 0xf;
  1369. u32 pss =
  1370. (cop0->reg[MIPS_CP0_STATUS][2] >> 6) & 0xf;
  1371. /*
  1372. * We don't support any shadow register sets, so
  1373. * SRSCtl[PSS] == SRSCtl[CSS] = 0
  1374. */
  1375. if (css || pss) {
  1376. er = EMULATE_FAIL;
  1377. break;
  1378. }
  1379. kvm_debug("WRPGPR[%d][%d] = %#lx\n", pss, rd,
  1380. vcpu->arch.gprs[rt]);
  1381. vcpu->arch.gprs[rd] = vcpu->arch.gprs[rt];
  1382. }
  1383. break;
  1384. default:
  1385. kvm_err("[%#lx]MachEmulateCP0: unsupported COP0, copz: 0x%x\n",
  1386. vcpu->arch.pc, inst.c0r_format.rs);
  1387. er = EMULATE_FAIL;
  1388. break;
  1389. }
  1390. }
  1391. done:
  1392. /* Rollback PC only if emulation was unsuccessful */
  1393. if (er == EMULATE_FAIL)
  1394. vcpu->arch.pc = curr_pc;
  1395. dont_update_pc:
  1396. /*
  1397. * This is for special instructions whose emulation
  1398. * updates the PC, so do not overwrite the PC under
  1399. * any circumstances
  1400. */
  1401. return er;
  1402. }
  1403. enum emulation_result kvm_mips_emulate_store(union mips_instruction inst,
  1404. u32 cause,
  1405. struct kvm_run *run,
  1406. struct kvm_vcpu *vcpu)
  1407. {
  1408. enum emulation_result er;
  1409. u32 rt;
  1410. void *data = run->mmio.data;
  1411. unsigned long curr_pc;
  1412. /*
  1413. * Update PC and hold onto current PC in case there is
  1414. * an error and we want to rollback the PC
  1415. */
  1416. curr_pc = vcpu->arch.pc;
  1417. er = update_pc(vcpu, cause);
  1418. if (er == EMULATE_FAIL)
  1419. return er;
  1420. rt = inst.i_format.rt;
  1421. run->mmio.phys_addr = kvm_mips_callbacks->gva_to_gpa(
  1422. vcpu->arch.host_cp0_badvaddr);
  1423. if (run->mmio.phys_addr == KVM_INVALID_ADDR)
  1424. goto out_fail;
  1425. switch (inst.i_format.opcode) {
  1426. #if defined(CONFIG_64BIT) && defined(CONFIG_KVM_MIPS_VZ)
  1427. case sd_op:
  1428. run->mmio.len = 8;
  1429. *(u64 *)data = vcpu->arch.gprs[rt];
  1430. kvm_debug("[%#lx] OP_SD: eaddr: %#lx, gpr: %#lx, data: %#llx\n",
  1431. vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
  1432. vcpu->arch.gprs[rt], *(u64 *)data);
  1433. break;
  1434. #endif
  1435. case sw_op:
  1436. run->mmio.len = 4;
  1437. *(u32 *)data = vcpu->arch.gprs[rt];
  1438. kvm_debug("[%#lx] OP_SW: eaddr: %#lx, gpr: %#lx, data: %#x\n",
  1439. vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
  1440. vcpu->arch.gprs[rt], *(u32 *)data);
  1441. break;
  1442. case sh_op:
  1443. run->mmio.len = 2;
  1444. *(u16 *)data = vcpu->arch.gprs[rt];
  1445. kvm_debug("[%#lx] OP_SH: eaddr: %#lx, gpr: %#lx, data: %#x\n",
  1446. vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
  1447. vcpu->arch.gprs[rt], *(u16 *)data);
  1448. break;
  1449. case sb_op:
  1450. run->mmio.len = 1;
  1451. *(u8 *)data = vcpu->arch.gprs[rt];
  1452. kvm_debug("[%#lx] OP_SB: eaddr: %#lx, gpr: %#lx, data: %#x\n",
  1453. vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
  1454. vcpu->arch.gprs[rt], *(u8 *)data);
  1455. break;
  1456. default:
  1457. kvm_err("Store not yet supported (inst=0x%08x)\n",
  1458. inst.word);
  1459. goto out_fail;
  1460. }
  1461. run->mmio.is_write = 1;
  1462. vcpu->mmio_needed = 1;
  1463. vcpu->mmio_is_write = 1;
  1464. return EMULATE_DO_MMIO;
  1465. out_fail:
  1466. /* Rollback PC if emulation was unsuccessful */
  1467. vcpu->arch.pc = curr_pc;
  1468. return EMULATE_FAIL;
  1469. }
  1470. enum emulation_result kvm_mips_emulate_load(union mips_instruction inst,
  1471. u32 cause, struct kvm_run *run,
  1472. struct kvm_vcpu *vcpu)
  1473. {
  1474. enum emulation_result er;
  1475. unsigned long curr_pc;
  1476. u32 op, rt;
  1477. rt = inst.i_format.rt;
  1478. op = inst.i_format.opcode;
  1479. /*
  1480. * Find the resume PC now while we have safe and easy access to the
  1481. * prior branch instruction, and save it for
  1482. * kvm_mips_complete_mmio_load() to restore later.
  1483. */
  1484. curr_pc = vcpu->arch.pc;
  1485. er = update_pc(vcpu, cause);
  1486. if (er == EMULATE_FAIL)
  1487. return er;
  1488. vcpu->arch.io_pc = vcpu->arch.pc;
  1489. vcpu->arch.pc = curr_pc;
  1490. vcpu->arch.io_gpr = rt;
  1491. run->mmio.phys_addr = kvm_mips_callbacks->gva_to_gpa(
  1492. vcpu->arch.host_cp0_badvaddr);
  1493. if (run->mmio.phys_addr == KVM_INVALID_ADDR)
  1494. return EMULATE_FAIL;
  1495. vcpu->mmio_needed = 2; /* signed */
  1496. switch (op) {
  1497. #if defined(CONFIG_64BIT) && defined(CONFIG_KVM_MIPS_VZ)
  1498. case ld_op:
  1499. run->mmio.len = 8;
  1500. break;
  1501. case lwu_op:
  1502. vcpu->mmio_needed = 1; /* unsigned */
  1503. /* fall through */
  1504. #endif
  1505. case lw_op:
  1506. run->mmio.len = 4;
  1507. break;
  1508. case lhu_op:
  1509. vcpu->mmio_needed = 1; /* unsigned */
  1510. /* fall through */
  1511. case lh_op:
  1512. run->mmio.len = 2;
  1513. break;
  1514. case lbu_op:
  1515. vcpu->mmio_needed = 1; /* unsigned */
  1516. /* fall through */
  1517. case lb_op:
  1518. run->mmio.len = 1;
  1519. break;
  1520. default:
  1521. kvm_err("Load not yet supported (inst=0x%08x)\n",
  1522. inst.word);
  1523. vcpu->mmio_needed = 0;
  1524. return EMULATE_FAIL;
  1525. }
  1526. run->mmio.is_write = 0;
  1527. vcpu->mmio_is_write = 0;
  1528. return EMULATE_DO_MMIO;
  1529. }
  1530. #ifndef CONFIG_KVM_MIPS_VZ
  1531. static enum emulation_result kvm_mips_guest_cache_op(int (*fn)(unsigned long),
  1532. unsigned long curr_pc,
  1533. unsigned long addr,
  1534. struct kvm_run *run,
  1535. struct kvm_vcpu *vcpu,
  1536. u32 cause)
  1537. {
  1538. int err;
  1539. for (;;) {
  1540. /* Carefully attempt the cache operation */
  1541. kvm_trap_emul_gva_lockless_begin(vcpu);
  1542. err = fn(addr);
  1543. kvm_trap_emul_gva_lockless_end(vcpu);
  1544. if (likely(!err))
  1545. return EMULATE_DONE;
  1546. /*
  1547. * Try to handle the fault and retry, maybe we just raced with a
  1548. * GVA invalidation.
  1549. */
  1550. switch (kvm_trap_emul_gva_fault(vcpu, addr, false)) {
  1551. case KVM_MIPS_GVA:
  1552. case KVM_MIPS_GPA:
  1553. /* bad virtual or physical address */
  1554. return EMULATE_FAIL;
  1555. case KVM_MIPS_TLB:
  1556. /* no matching guest TLB */
  1557. vcpu->arch.host_cp0_badvaddr = addr;
  1558. vcpu->arch.pc = curr_pc;
  1559. kvm_mips_emulate_tlbmiss_ld(cause, NULL, run, vcpu);
  1560. return EMULATE_EXCEPT;
  1561. case KVM_MIPS_TLBINV:
  1562. /* invalid matching guest TLB */
  1563. vcpu->arch.host_cp0_badvaddr = addr;
  1564. vcpu->arch.pc = curr_pc;
  1565. kvm_mips_emulate_tlbinv_ld(cause, NULL, run, vcpu);
  1566. return EMULATE_EXCEPT;
  1567. default:
  1568. break;
  1569. };
  1570. }
  1571. }
  1572. enum emulation_result kvm_mips_emulate_cache(union mips_instruction inst,
  1573. u32 *opc, u32 cause,
  1574. struct kvm_run *run,
  1575. struct kvm_vcpu *vcpu)
  1576. {
  1577. enum emulation_result er = EMULATE_DONE;
  1578. u32 cache, op_inst, op, base;
  1579. s16 offset;
  1580. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1581. unsigned long va;
  1582. unsigned long curr_pc;
  1583. /*
  1584. * Update PC and hold onto current PC in case there is
  1585. * an error and we want to rollback the PC
  1586. */
  1587. curr_pc = vcpu->arch.pc;
  1588. er = update_pc(vcpu, cause);
  1589. if (er == EMULATE_FAIL)
  1590. return er;
  1591. base = inst.i_format.rs;
  1592. op_inst = inst.i_format.rt;
  1593. if (cpu_has_mips_r6)
  1594. offset = inst.spec3_format.simmediate;
  1595. else
  1596. offset = inst.i_format.simmediate;
  1597. cache = op_inst & CacheOp_Cache;
  1598. op = op_inst & CacheOp_Op;
  1599. va = arch->gprs[base] + offset;
  1600. kvm_debug("CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
  1601. cache, op, base, arch->gprs[base], offset);
  1602. /*
  1603. * Treat INDEX_INV as a nop, basically issued by Linux on startup to
  1604. * invalidate the caches entirely by stepping through all the
  1605. * ways/indexes
  1606. */
  1607. if (op == Index_Writeback_Inv) {
  1608. kvm_debug("@ %#lx/%#lx CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
  1609. vcpu->arch.pc, vcpu->arch.gprs[31], cache, op, base,
  1610. arch->gprs[base], offset);
  1611. if (cache == Cache_D) {
  1612. #ifdef CONFIG_CPU_R4K_CACHE_TLB
  1613. r4k_blast_dcache();
  1614. #else
  1615. switch (boot_cpu_type()) {
  1616. case CPU_CAVIUM_OCTEON3:
  1617. /* locally flush icache */
  1618. local_flush_icache_range(0, 0);
  1619. break;
  1620. default:
  1621. __flush_cache_all();
  1622. break;
  1623. }
  1624. #endif
  1625. } else if (cache == Cache_I) {
  1626. #ifdef CONFIG_CPU_R4K_CACHE_TLB
  1627. r4k_blast_icache();
  1628. #else
  1629. switch (boot_cpu_type()) {
  1630. case CPU_CAVIUM_OCTEON3:
  1631. /* locally flush icache */
  1632. local_flush_icache_range(0, 0);
  1633. break;
  1634. default:
  1635. flush_icache_all();
  1636. break;
  1637. }
  1638. #endif
  1639. } else {
  1640. kvm_err("%s: unsupported CACHE INDEX operation\n",
  1641. __func__);
  1642. return EMULATE_FAIL;
  1643. }
  1644. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  1645. kvm_mips_trans_cache_index(inst, opc, vcpu);
  1646. #endif
  1647. goto done;
  1648. }
  1649. /* XXXKYMA: Only a subset of cache ops are supported, used by Linux */
  1650. if (op_inst == Hit_Writeback_Inv_D || op_inst == Hit_Invalidate_D) {
  1651. /*
  1652. * Perform the dcache part of icache synchronisation on the
  1653. * guest's behalf.
  1654. */
  1655. er = kvm_mips_guest_cache_op(protected_writeback_dcache_line,
  1656. curr_pc, va, run, vcpu, cause);
  1657. if (er != EMULATE_DONE)
  1658. goto done;
  1659. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  1660. /*
  1661. * Replace the CACHE instruction, with a SYNCI, not the same,
  1662. * but avoids a trap
  1663. */
  1664. kvm_mips_trans_cache_va(inst, opc, vcpu);
  1665. #endif
  1666. } else if (op_inst == Hit_Invalidate_I) {
  1667. /* Perform the icache synchronisation on the guest's behalf */
  1668. er = kvm_mips_guest_cache_op(protected_writeback_dcache_line,
  1669. curr_pc, va, run, vcpu, cause);
  1670. if (er != EMULATE_DONE)
  1671. goto done;
  1672. er = kvm_mips_guest_cache_op(protected_flush_icache_line,
  1673. curr_pc, va, run, vcpu, cause);
  1674. if (er != EMULATE_DONE)
  1675. goto done;
  1676. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  1677. /* Replace the CACHE instruction, with a SYNCI */
  1678. kvm_mips_trans_cache_va(inst, opc, vcpu);
  1679. #endif
  1680. } else {
  1681. kvm_err("NO-OP CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
  1682. cache, op, base, arch->gprs[base], offset);
  1683. er = EMULATE_FAIL;
  1684. }
  1685. done:
  1686. /* Rollback PC only if emulation was unsuccessful */
  1687. if (er == EMULATE_FAIL)
  1688. vcpu->arch.pc = curr_pc;
  1689. /* Guest exception needs guest to resume */
  1690. if (er == EMULATE_EXCEPT)
  1691. er = EMULATE_DONE;
  1692. return er;
  1693. }
  1694. enum emulation_result kvm_mips_emulate_inst(u32 cause, u32 *opc,
  1695. struct kvm_run *run,
  1696. struct kvm_vcpu *vcpu)
  1697. {
  1698. union mips_instruction inst;
  1699. enum emulation_result er = EMULATE_DONE;
  1700. int err;
  1701. /* Fetch the instruction. */
  1702. if (cause & CAUSEF_BD)
  1703. opc += 1;
  1704. err = kvm_get_badinstr(opc, vcpu, &inst.word);
  1705. if (err)
  1706. return EMULATE_FAIL;
  1707. switch (inst.r_format.opcode) {
  1708. case cop0_op:
  1709. er = kvm_mips_emulate_CP0(inst, opc, cause, run, vcpu);
  1710. break;
  1711. #ifndef CONFIG_CPU_MIPSR6
  1712. case cache_op:
  1713. ++vcpu->stat.cache_exits;
  1714. trace_kvm_exit(vcpu, KVM_TRACE_EXIT_CACHE);
  1715. er = kvm_mips_emulate_cache(inst, opc, cause, run, vcpu);
  1716. break;
  1717. #else
  1718. case spec3_op:
  1719. switch (inst.spec3_format.func) {
  1720. case cache6_op:
  1721. ++vcpu->stat.cache_exits;
  1722. trace_kvm_exit(vcpu, KVM_TRACE_EXIT_CACHE);
  1723. er = kvm_mips_emulate_cache(inst, opc, cause, run,
  1724. vcpu);
  1725. break;
  1726. default:
  1727. goto unknown;
  1728. };
  1729. break;
  1730. unknown:
  1731. #endif
  1732. default:
  1733. kvm_err("Instruction emulation not supported (%p/%#x)\n", opc,
  1734. inst.word);
  1735. kvm_arch_vcpu_dump_regs(vcpu);
  1736. er = EMULATE_FAIL;
  1737. break;
  1738. }
  1739. return er;
  1740. }
  1741. #endif /* CONFIG_KVM_MIPS_VZ */
  1742. /**
  1743. * kvm_mips_guest_exception_base() - Find guest exception vector base address.
  1744. *
  1745. * Returns: The base address of the current guest exception vector, taking
  1746. * both Guest.CP0_Status.BEV and Guest.CP0_EBase into account.
  1747. */
  1748. long kvm_mips_guest_exception_base(struct kvm_vcpu *vcpu)
  1749. {
  1750. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1751. if (kvm_read_c0_guest_status(cop0) & ST0_BEV)
  1752. return KVM_GUEST_CKSEG1ADDR(0x1fc00200);
  1753. else
  1754. return kvm_read_c0_guest_ebase(cop0) & MIPS_EBASE_BASE;
  1755. }
  1756. enum emulation_result kvm_mips_emulate_syscall(u32 cause,
  1757. u32 *opc,
  1758. struct kvm_run *run,
  1759. struct kvm_vcpu *vcpu)
  1760. {
  1761. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1762. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1763. enum emulation_result er = EMULATE_DONE;
  1764. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1765. /* save old pc */
  1766. kvm_write_c0_guest_epc(cop0, arch->pc);
  1767. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1768. if (cause & CAUSEF_BD)
  1769. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1770. else
  1771. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1772. kvm_debug("Delivering SYSCALL @ pc %#lx\n", arch->pc);
  1773. kvm_change_c0_guest_cause(cop0, (0xff),
  1774. (EXCCODE_SYS << CAUSEB_EXCCODE));
  1775. /* Set PC to the exception entry point */
  1776. arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
  1777. } else {
  1778. kvm_err("Trying to deliver SYSCALL when EXL is already set\n");
  1779. er = EMULATE_FAIL;
  1780. }
  1781. return er;
  1782. }
  1783. enum emulation_result kvm_mips_emulate_tlbmiss_ld(u32 cause,
  1784. u32 *opc,
  1785. struct kvm_run *run,
  1786. struct kvm_vcpu *vcpu)
  1787. {
  1788. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1789. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1790. unsigned long entryhi = (vcpu->arch. host_cp0_badvaddr & VPN2_MASK) |
  1791. (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
  1792. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1793. /* save old pc */
  1794. kvm_write_c0_guest_epc(cop0, arch->pc);
  1795. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1796. if (cause & CAUSEF_BD)
  1797. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1798. else
  1799. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1800. kvm_debug("[EXL == 0] delivering TLB MISS @ pc %#lx\n",
  1801. arch->pc);
  1802. /* set pc to the exception entry point */
  1803. arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x0;
  1804. } else {
  1805. kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n",
  1806. arch->pc);
  1807. arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
  1808. }
  1809. kvm_change_c0_guest_cause(cop0, (0xff),
  1810. (EXCCODE_TLBL << CAUSEB_EXCCODE));
  1811. /* setup badvaddr, context and entryhi registers for the guest */
  1812. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1813. /* XXXKYMA: is the context register used by linux??? */
  1814. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1815. return EMULATE_DONE;
  1816. }
  1817. enum emulation_result kvm_mips_emulate_tlbinv_ld(u32 cause,
  1818. u32 *opc,
  1819. struct kvm_run *run,
  1820. struct kvm_vcpu *vcpu)
  1821. {
  1822. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1823. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1824. unsigned long entryhi =
  1825. (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 INV @ pc %#lx\n",
  1836. arch->pc);
  1837. } else {
  1838. kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n",
  1839. arch->pc);
  1840. }
  1841. /* set pc to the exception entry point */
  1842. arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
  1843. kvm_change_c0_guest_cause(cop0, (0xff),
  1844. (EXCCODE_TLBL << CAUSEB_EXCCODE));
  1845. /* setup badvaddr, context and entryhi registers for the guest */
  1846. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1847. /* XXXKYMA: is the context register used by linux??? */
  1848. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1849. return EMULATE_DONE;
  1850. }
  1851. enum emulation_result kvm_mips_emulate_tlbmiss_st(u32 cause,
  1852. u32 *opc,
  1853. struct kvm_run *run,
  1854. struct kvm_vcpu *vcpu)
  1855. {
  1856. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1857. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1858. unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
  1859. (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
  1860. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1861. /* save old pc */
  1862. kvm_write_c0_guest_epc(cop0, arch->pc);
  1863. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1864. if (cause & CAUSEF_BD)
  1865. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1866. else
  1867. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1868. kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n",
  1869. arch->pc);
  1870. /* Set PC to the exception entry point */
  1871. arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x0;
  1872. } else {
  1873. kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n",
  1874. arch->pc);
  1875. arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
  1876. }
  1877. kvm_change_c0_guest_cause(cop0, (0xff),
  1878. (EXCCODE_TLBS << 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_tlbinv_st(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. unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
  1893. (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
  1894. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1895. /* save old pc */
  1896. kvm_write_c0_guest_epc(cop0, arch->pc);
  1897. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1898. if (cause & CAUSEF_BD)
  1899. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1900. else
  1901. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1902. kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n",
  1903. arch->pc);
  1904. } else {
  1905. kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n",
  1906. arch->pc);
  1907. }
  1908. /* Set PC to the exception entry point */
  1909. arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
  1910. kvm_change_c0_guest_cause(cop0, (0xff),
  1911. (EXCCODE_TLBS << CAUSEB_EXCCODE));
  1912. /* setup badvaddr, context and entryhi registers for the guest */
  1913. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1914. /* XXXKYMA: is the context register used by linux??? */
  1915. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1916. return EMULATE_DONE;
  1917. }
  1918. enum emulation_result kvm_mips_emulate_tlbmod(u32 cause,
  1919. u32 *opc,
  1920. struct kvm_run *run,
  1921. struct kvm_vcpu *vcpu)
  1922. {
  1923. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1924. unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
  1925. (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
  1926. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1927. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1928. /* save old pc */
  1929. kvm_write_c0_guest_epc(cop0, arch->pc);
  1930. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1931. if (cause & CAUSEF_BD)
  1932. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1933. else
  1934. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1935. kvm_debug("[EXL == 0] Delivering TLB MOD @ pc %#lx\n",
  1936. arch->pc);
  1937. } else {
  1938. kvm_debug("[EXL == 1] Delivering TLB MOD @ pc %#lx\n",
  1939. arch->pc);
  1940. }
  1941. arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
  1942. kvm_change_c0_guest_cause(cop0, (0xff),
  1943. (EXCCODE_MOD << CAUSEB_EXCCODE));
  1944. /* setup badvaddr, context and entryhi registers for the guest */
  1945. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1946. /* XXXKYMA: is the context register used by linux??? */
  1947. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1948. return EMULATE_DONE;
  1949. }
  1950. enum emulation_result kvm_mips_emulate_fpu_exc(u32 cause,
  1951. u32 *opc,
  1952. struct kvm_run *run,
  1953. struct kvm_vcpu *vcpu)
  1954. {
  1955. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1956. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1957. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1958. /* save old pc */
  1959. kvm_write_c0_guest_epc(cop0, arch->pc);
  1960. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1961. if (cause & CAUSEF_BD)
  1962. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1963. else
  1964. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1965. }
  1966. arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
  1967. kvm_change_c0_guest_cause(cop0, (0xff),
  1968. (EXCCODE_CPU << CAUSEB_EXCCODE));
  1969. kvm_change_c0_guest_cause(cop0, (CAUSEF_CE), (0x1 << CAUSEB_CE));
  1970. return EMULATE_DONE;
  1971. }
  1972. enum emulation_result kvm_mips_emulate_ri_exc(u32 cause,
  1973. u32 *opc,
  1974. struct kvm_run *run,
  1975. struct kvm_vcpu *vcpu)
  1976. {
  1977. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1978. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1979. enum emulation_result er = EMULATE_DONE;
  1980. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1981. /* save old pc */
  1982. kvm_write_c0_guest_epc(cop0, arch->pc);
  1983. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1984. if (cause & CAUSEF_BD)
  1985. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1986. else
  1987. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1988. kvm_debug("Delivering RI @ pc %#lx\n", arch->pc);
  1989. kvm_change_c0_guest_cause(cop0, (0xff),
  1990. (EXCCODE_RI << CAUSEB_EXCCODE));
  1991. /* Set PC to the exception entry point */
  1992. arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
  1993. } else {
  1994. kvm_err("Trying to deliver RI when EXL is already set\n");
  1995. er = EMULATE_FAIL;
  1996. }
  1997. return er;
  1998. }
  1999. enum emulation_result kvm_mips_emulate_bp_exc(u32 cause,
  2000. u32 *opc,
  2001. struct kvm_run *run,
  2002. struct kvm_vcpu *vcpu)
  2003. {
  2004. struct mips_coproc *cop0 = vcpu->arch.cop0;
  2005. struct kvm_vcpu_arch *arch = &vcpu->arch;
  2006. enum emulation_result er = EMULATE_DONE;
  2007. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  2008. /* save old pc */
  2009. kvm_write_c0_guest_epc(cop0, arch->pc);
  2010. kvm_set_c0_guest_status(cop0, ST0_EXL);
  2011. if (cause & CAUSEF_BD)
  2012. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  2013. else
  2014. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  2015. kvm_debug("Delivering BP @ pc %#lx\n", arch->pc);
  2016. kvm_change_c0_guest_cause(cop0, (0xff),
  2017. (EXCCODE_BP << CAUSEB_EXCCODE));
  2018. /* Set PC to the exception entry point */
  2019. arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
  2020. } else {
  2021. kvm_err("Trying to deliver BP when EXL is already set\n");
  2022. er = EMULATE_FAIL;
  2023. }
  2024. return er;
  2025. }
  2026. enum emulation_result kvm_mips_emulate_trap_exc(u32 cause,
  2027. u32 *opc,
  2028. struct kvm_run *run,
  2029. struct kvm_vcpu *vcpu)
  2030. {
  2031. struct mips_coproc *cop0 = vcpu->arch.cop0;
  2032. struct kvm_vcpu_arch *arch = &vcpu->arch;
  2033. enum emulation_result er = EMULATE_DONE;
  2034. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  2035. /* save old pc */
  2036. kvm_write_c0_guest_epc(cop0, arch->pc);
  2037. kvm_set_c0_guest_status(cop0, ST0_EXL);
  2038. if (cause & CAUSEF_BD)
  2039. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  2040. else
  2041. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  2042. kvm_debug("Delivering TRAP @ pc %#lx\n", arch->pc);
  2043. kvm_change_c0_guest_cause(cop0, (0xff),
  2044. (EXCCODE_TR << CAUSEB_EXCCODE));
  2045. /* Set PC to the exception entry point */
  2046. arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
  2047. } else {
  2048. kvm_err("Trying to deliver TRAP when EXL is already set\n");
  2049. er = EMULATE_FAIL;
  2050. }
  2051. return er;
  2052. }
  2053. enum emulation_result kvm_mips_emulate_msafpe_exc(u32 cause,
  2054. u32 *opc,
  2055. struct kvm_run *run,
  2056. struct kvm_vcpu *vcpu)
  2057. {
  2058. struct mips_coproc *cop0 = vcpu->arch.cop0;
  2059. struct kvm_vcpu_arch *arch = &vcpu->arch;
  2060. enum emulation_result er = EMULATE_DONE;
  2061. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  2062. /* save old pc */
  2063. kvm_write_c0_guest_epc(cop0, arch->pc);
  2064. kvm_set_c0_guest_status(cop0, ST0_EXL);
  2065. if (cause & CAUSEF_BD)
  2066. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  2067. else
  2068. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  2069. kvm_debug("Delivering MSAFPE @ pc %#lx\n", arch->pc);
  2070. kvm_change_c0_guest_cause(cop0, (0xff),
  2071. (EXCCODE_MSAFPE << CAUSEB_EXCCODE));
  2072. /* Set PC to the exception entry point */
  2073. arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
  2074. } else {
  2075. kvm_err("Trying to deliver MSAFPE when EXL is already set\n");
  2076. er = EMULATE_FAIL;
  2077. }
  2078. return er;
  2079. }
  2080. enum emulation_result kvm_mips_emulate_fpe_exc(u32 cause,
  2081. u32 *opc,
  2082. struct kvm_run *run,
  2083. struct kvm_vcpu *vcpu)
  2084. {
  2085. struct mips_coproc *cop0 = vcpu->arch.cop0;
  2086. struct kvm_vcpu_arch *arch = &vcpu->arch;
  2087. enum emulation_result er = EMULATE_DONE;
  2088. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  2089. /* save old pc */
  2090. kvm_write_c0_guest_epc(cop0, arch->pc);
  2091. kvm_set_c0_guest_status(cop0, ST0_EXL);
  2092. if (cause & CAUSEF_BD)
  2093. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  2094. else
  2095. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  2096. kvm_debug("Delivering FPE @ pc %#lx\n", arch->pc);
  2097. kvm_change_c0_guest_cause(cop0, (0xff),
  2098. (EXCCODE_FPE << CAUSEB_EXCCODE));
  2099. /* Set PC to the exception entry point */
  2100. arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
  2101. } else {
  2102. kvm_err("Trying to deliver FPE when EXL is already set\n");
  2103. er = EMULATE_FAIL;
  2104. }
  2105. return er;
  2106. }
  2107. enum emulation_result kvm_mips_emulate_msadis_exc(u32 cause,
  2108. u32 *opc,
  2109. struct kvm_run *run,
  2110. struct kvm_vcpu *vcpu)
  2111. {
  2112. struct mips_coproc *cop0 = vcpu->arch.cop0;
  2113. struct kvm_vcpu_arch *arch = &vcpu->arch;
  2114. enum emulation_result er = EMULATE_DONE;
  2115. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  2116. /* save old pc */
  2117. kvm_write_c0_guest_epc(cop0, arch->pc);
  2118. kvm_set_c0_guest_status(cop0, ST0_EXL);
  2119. if (cause & CAUSEF_BD)
  2120. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  2121. else
  2122. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  2123. kvm_debug("Delivering MSADIS @ pc %#lx\n", arch->pc);
  2124. kvm_change_c0_guest_cause(cop0, (0xff),
  2125. (EXCCODE_MSADIS << CAUSEB_EXCCODE));
  2126. /* Set PC to the exception entry point */
  2127. arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
  2128. } else {
  2129. kvm_err("Trying to deliver MSADIS when EXL is already set\n");
  2130. er = EMULATE_FAIL;
  2131. }
  2132. return er;
  2133. }
  2134. enum emulation_result kvm_mips_handle_ri(u32 cause, u32 *opc,
  2135. struct kvm_run *run,
  2136. struct kvm_vcpu *vcpu)
  2137. {
  2138. struct mips_coproc *cop0 = vcpu->arch.cop0;
  2139. struct kvm_vcpu_arch *arch = &vcpu->arch;
  2140. enum emulation_result er = EMULATE_DONE;
  2141. unsigned long curr_pc;
  2142. union mips_instruction inst;
  2143. int err;
  2144. /*
  2145. * Update PC and hold onto current PC in case there is
  2146. * an error and we want to rollback the PC
  2147. */
  2148. curr_pc = vcpu->arch.pc;
  2149. er = update_pc(vcpu, cause);
  2150. if (er == EMULATE_FAIL)
  2151. return er;
  2152. /* Fetch the instruction. */
  2153. if (cause & CAUSEF_BD)
  2154. opc += 1;
  2155. err = kvm_get_badinstr(opc, vcpu, &inst.word);
  2156. if (err) {
  2157. kvm_err("%s: Cannot get inst @ %p (%d)\n", __func__, opc, err);
  2158. return EMULATE_FAIL;
  2159. }
  2160. if (inst.r_format.opcode == spec3_op &&
  2161. inst.r_format.func == rdhwr_op &&
  2162. inst.r_format.rs == 0 &&
  2163. (inst.r_format.re >> 3) == 0) {
  2164. int usermode = !KVM_GUEST_KERNEL_MODE(vcpu);
  2165. int rd = inst.r_format.rd;
  2166. int rt = inst.r_format.rt;
  2167. int sel = inst.r_format.re & 0x7;
  2168. /* If usermode, check RDHWR rd is allowed by guest HWREna */
  2169. if (usermode && !(kvm_read_c0_guest_hwrena(cop0) & BIT(rd))) {
  2170. kvm_debug("RDHWR %#x disallowed by HWREna @ %p\n",
  2171. rd, opc);
  2172. goto emulate_ri;
  2173. }
  2174. switch (rd) {
  2175. case MIPS_HWR_CPUNUM: /* CPU number */
  2176. arch->gprs[rt] = vcpu->vcpu_id;
  2177. break;
  2178. case MIPS_HWR_SYNCISTEP: /* SYNCI length */
  2179. arch->gprs[rt] = min(current_cpu_data.dcache.linesz,
  2180. current_cpu_data.icache.linesz);
  2181. break;
  2182. case MIPS_HWR_CC: /* Read count register */
  2183. arch->gprs[rt] = (s32)kvm_mips_read_count(vcpu);
  2184. break;
  2185. case MIPS_HWR_CCRES: /* Count register resolution */
  2186. switch (current_cpu_data.cputype) {
  2187. case CPU_20KC:
  2188. case CPU_25KF:
  2189. arch->gprs[rt] = 1;
  2190. break;
  2191. default:
  2192. arch->gprs[rt] = 2;
  2193. }
  2194. break;
  2195. case MIPS_HWR_ULR: /* Read UserLocal register */
  2196. arch->gprs[rt] = kvm_read_c0_guest_userlocal(cop0);
  2197. break;
  2198. default:
  2199. kvm_debug("RDHWR %#x not supported @ %p\n", rd, opc);
  2200. goto emulate_ri;
  2201. }
  2202. trace_kvm_hwr(vcpu, KVM_TRACE_RDHWR, KVM_TRACE_HWR(rd, sel),
  2203. vcpu->arch.gprs[rt]);
  2204. } else {
  2205. kvm_debug("Emulate RI not supported @ %p: %#x\n",
  2206. opc, inst.word);
  2207. goto emulate_ri;
  2208. }
  2209. return EMULATE_DONE;
  2210. emulate_ri:
  2211. /*
  2212. * Rollback PC (if in branch delay slot then the PC already points to
  2213. * branch target), and pass the RI exception to the guest OS.
  2214. */
  2215. vcpu->arch.pc = curr_pc;
  2216. return kvm_mips_emulate_ri_exc(cause, opc, run, vcpu);
  2217. }
  2218. enum emulation_result kvm_mips_complete_mmio_load(struct kvm_vcpu *vcpu,
  2219. struct kvm_run *run)
  2220. {
  2221. unsigned long *gpr = &vcpu->arch.gprs[vcpu->arch.io_gpr];
  2222. enum emulation_result er = EMULATE_DONE;
  2223. if (run->mmio.len > sizeof(*gpr)) {
  2224. kvm_err("Bad MMIO length: %d", run->mmio.len);
  2225. er = EMULATE_FAIL;
  2226. goto done;
  2227. }
  2228. /* Restore saved resume PC */
  2229. vcpu->arch.pc = vcpu->arch.io_pc;
  2230. switch (run->mmio.len) {
  2231. case 8:
  2232. *gpr = *(s64 *)run->mmio.data;
  2233. break;
  2234. case 4:
  2235. if (vcpu->mmio_needed == 2)
  2236. *gpr = *(s32 *)run->mmio.data;
  2237. else
  2238. *gpr = *(u32 *)run->mmio.data;
  2239. break;
  2240. case 2:
  2241. if (vcpu->mmio_needed == 2)
  2242. *gpr = *(s16 *) run->mmio.data;
  2243. else
  2244. *gpr = *(u16 *)run->mmio.data;
  2245. break;
  2246. case 1:
  2247. if (vcpu->mmio_needed == 2)
  2248. *gpr = *(s8 *) run->mmio.data;
  2249. else
  2250. *gpr = *(u8 *) run->mmio.data;
  2251. break;
  2252. }
  2253. done:
  2254. return er;
  2255. }
  2256. static enum emulation_result kvm_mips_emulate_exc(u32 cause,
  2257. u32 *opc,
  2258. struct kvm_run *run,
  2259. struct kvm_vcpu *vcpu)
  2260. {
  2261. u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
  2262. struct mips_coproc *cop0 = vcpu->arch.cop0;
  2263. struct kvm_vcpu_arch *arch = &vcpu->arch;
  2264. enum emulation_result er = EMULATE_DONE;
  2265. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  2266. /* save old pc */
  2267. kvm_write_c0_guest_epc(cop0, arch->pc);
  2268. kvm_set_c0_guest_status(cop0, ST0_EXL);
  2269. if (cause & CAUSEF_BD)
  2270. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  2271. else
  2272. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  2273. kvm_change_c0_guest_cause(cop0, (0xff),
  2274. (exccode << CAUSEB_EXCCODE));
  2275. /* Set PC to the exception entry point */
  2276. arch->pc = kvm_mips_guest_exception_base(vcpu) + 0x180;
  2277. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  2278. kvm_debug("Delivering EXC %d @ pc %#lx, badVaddr: %#lx\n",
  2279. exccode, kvm_read_c0_guest_epc(cop0),
  2280. kvm_read_c0_guest_badvaddr(cop0));
  2281. } else {
  2282. kvm_err("Trying to deliver EXC when EXL is already set\n");
  2283. er = EMULATE_FAIL;
  2284. }
  2285. return er;
  2286. }
  2287. enum emulation_result kvm_mips_check_privilege(u32 cause,
  2288. u32 *opc,
  2289. struct kvm_run *run,
  2290. struct kvm_vcpu *vcpu)
  2291. {
  2292. enum emulation_result er = EMULATE_DONE;
  2293. u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
  2294. unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
  2295. int usermode = !KVM_GUEST_KERNEL_MODE(vcpu);
  2296. if (usermode) {
  2297. switch (exccode) {
  2298. case EXCCODE_INT:
  2299. case EXCCODE_SYS:
  2300. case EXCCODE_BP:
  2301. case EXCCODE_RI:
  2302. case EXCCODE_TR:
  2303. case EXCCODE_MSAFPE:
  2304. case EXCCODE_FPE:
  2305. case EXCCODE_MSADIS:
  2306. break;
  2307. case EXCCODE_CPU:
  2308. if (((cause & CAUSEF_CE) >> CAUSEB_CE) == 0)
  2309. er = EMULATE_PRIV_FAIL;
  2310. break;
  2311. case EXCCODE_MOD:
  2312. break;
  2313. case EXCCODE_TLBL:
  2314. /*
  2315. * We we are accessing Guest kernel space, then send an
  2316. * address error exception to the guest
  2317. */
  2318. if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
  2319. kvm_debug("%s: LD MISS @ %#lx\n", __func__,
  2320. badvaddr);
  2321. cause &= ~0xff;
  2322. cause |= (EXCCODE_ADEL << CAUSEB_EXCCODE);
  2323. er = EMULATE_PRIV_FAIL;
  2324. }
  2325. break;
  2326. case EXCCODE_TLBS:
  2327. /*
  2328. * We we are accessing Guest kernel space, then send an
  2329. * address error exception to the guest
  2330. */
  2331. if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
  2332. kvm_debug("%s: ST MISS @ %#lx\n", __func__,
  2333. badvaddr);
  2334. cause &= ~0xff;
  2335. cause |= (EXCCODE_ADES << CAUSEB_EXCCODE);
  2336. er = EMULATE_PRIV_FAIL;
  2337. }
  2338. break;
  2339. case EXCCODE_ADES:
  2340. kvm_debug("%s: address error ST @ %#lx\n", __func__,
  2341. badvaddr);
  2342. if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
  2343. cause &= ~0xff;
  2344. cause |= (EXCCODE_TLBS << CAUSEB_EXCCODE);
  2345. }
  2346. er = EMULATE_PRIV_FAIL;
  2347. break;
  2348. case EXCCODE_ADEL:
  2349. kvm_debug("%s: address error LD @ %#lx\n", __func__,
  2350. badvaddr);
  2351. if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
  2352. cause &= ~0xff;
  2353. cause |= (EXCCODE_TLBL << CAUSEB_EXCCODE);
  2354. }
  2355. er = EMULATE_PRIV_FAIL;
  2356. break;
  2357. default:
  2358. er = EMULATE_PRIV_FAIL;
  2359. break;
  2360. }
  2361. }
  2362. if (er == EMULATE_PRIV_FAIL)
  2363. kvm_mips_emulate_exc(cause, opc, run, vcpu);
  2364. return er;
  2365. }
  2366. /*
  2367. * User Address (UA) fault, this could happen if
  2368. * (1) TLB entry not present/valid in both Guest and shadow host TLBs, in this
  2369. * case we pass on the fault to the guest kernel and let it handle it.
  2370. * (2) TLB entry is present in the Guest TLB but not in the shadow, in this
  2371. * case we inject the TLB from the Guest TLB into the shadow host TLB
  2372. */
  2373. enum emulation_result kvm_mips_handle_tlbmiss(u32 cause,
  2374. u32 *opc,
  2375. struct kvm_run *run,
  2376. struct kvm_vcpu *vcpu,
  2377. bool write_fault)
  2378. {
  2379. enum emulation_result er = EMULATE_DONE;
  2380. u32 exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
  2381. unsigned long va = vcpu->arch.host_cp0_badvaddr;
  2382. int index;
  2383. kvm_debug("kvm_mips_handle_tlbmiss: badvaddr: %#lx\n",
  2384. vcpu->arch.host_cp0_badvaddr);
  2385. /*
  2386. * KVM would not have got the exception if this entry was valid in the
  2387. * shadow host TLB. Check the Guest TLB, if the entry is not there then
  2388. * send the guest an exception. The guest exc handler should then inject
  2389. * an entry into the guest TLB.
  2390. */
  2391. index = kvm_mips_guest_tlb_lookup(vcpu,
  2392. (va & VPN2_MASK) |
  2393. (kvm_read_c0_guest_entryhi(vcpu->arch.cop0) &
  2394. KVM_ENTRYHI_ASID));
  2395. if (index < 0) {
  2396. if (exccode == EXCCODE_TLBL) {
  2397. er = kvm_mips_emulate_tlbmiss_ld(cause, opc, run, vcpu);
  2398. } else if (exccode == EXCCODE_TLBS) {
  2399. er = kvm_mips_emulate_tlbmiss_st(cause, opc, run, vcpu);
  2400. } else {
  2401. kvm_err("%s: invalid exc code: %d\n", __func__,
  2402. exccode);
  2403. er = EMULATE_FAIL;
  2404. }
  2405. } else {
  2406. struct kvm_mips_tlb *tlb = &vcpu->arch.guest_tlb[index];
  2407. /*
  2408. * Check if the entry is valid, if not then setup a TLB invalid
  2409. * exception to the guest
  2410. */
  2411. if (!TLB_IS_VALID(*tlb, va)) {
  2412. if (exccode == EXCCODE_TLBL) {
  2413. er = kvm_mips_emulate_tlbinv_ld(cause, opc, run,
  2414. vcpu);
  2415. } else if (exccode == EXCCODE_TLBS) {
  2416. er = kvm_mips_emulate_tlbinv_st(cause, opc, run,
  2417. vcpu);
  2418. } else {
  2419. kvm_err("%s: invalid exc code: %d\n", __func__,
  2420. exccode);
  2421. er = EMULATE_FAIL;
  2422. }
  2423. } else {
  2424. kvm_debug("Injecting hi: %#lx, lo0: %#lx, lo1: %#lx into shadow host TLB\n",
  2425. tlb->tlb_hi, tlb->tlb_lo[0], tlb->tlb_lo[1]);
  2426. /*
  2427. * OK we have a Guest TLB entry, now inject it into the
  2428. * shadow host TLB
  2429. */
  2430. if (kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb, va,
  2431. write_fault)) {
  2432. kvm_err("%s: handling mapped seg tlb fault for %lx, index: %u, vcpu: %p, ASID: %#lx\n",
  2433. __func__, va, index, vcpu,
  2434. read_c0_entryhi());
  2435. er = EMULATE_FAIL;
  2436. }
  2437. }
  2438. }
  2439. return er;
  2440. }