emulate.c 71 KB

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