kvm_mips_emul.c 61 KB

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