emulate.c 69 KB

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