emulate.c 72 KB

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