emulate.c 61 KB

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