emulate.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319
  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. break;
  1876. case T_COP_UNUSABLE:
  1877. if (((cause & CAUSEF_CE) >> CAUSEB_CE) == 0)
  1878. er = EMULATE_PRIV_FAIL;
  1879. break;
  1880. case T_TLB_MOD:
  1881. break;
  1882. case T_TLB_LD_MISS:
  1883. /*
  1884. * We we are accessing Guest kernel space, then send an
  1885. * address error exception to the guest
  1886. */
  1887. if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
  1888. kvm_debug("%s: LD MISS @ %#lx\n", __func__,
  1889. badvaddr);
  1890. cause &= ~0xff;
  1891. cause |= (T_ADDR_ERR_LD << CAUSEB_EXCCODE);
  1892. er = EMULATE_PRIV_FAIL;
  1893. }
  1894. break;
  1895. case T_TLB_ST_MISS:
  1896. /*
  1897. * We we are accessing Guest kernel space, then send an
  1898. * address error exception to the guest
  1899. */
  1900. if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
  1901. kvm_debug("%s: ST MISS @ %#lx\n", __func__,
  1902. badvaddr);
  1903. cause &= ~0xff;
  1904. cause |= (T_ADDR_ERR_ST << CAUSEB_EXCCODE);
  1905. er = EMULATE_PRIV_FAIL;
  1906. }
  1907. break;
  1908. case T_ADDR_ERR_ST:
  1909. kvm_debug("%s: address error ST @ %#lx\n", __func__,
  1910. badvaddr);
  1911. if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
  1912. cause &= ~0xff;
  1913. cause |= (T_TLB_ST_MISS << CAUSEB_EXCCODE);
  1914. }
  1915. er = EMULATE_PRIV_FAIL;
  1916. break;
  1917. case T_ADDR_ERR_LD:
  1918. kvm_debug("%s: address error LD @ %#lx\n", __func__,
  1919. badvaddr);
  1920. if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
  1921. cause &= ~0xff;
  1922. cause |= (T_TLB_LD_MISS << CAUSEB_EXCCODE);
  1923. }
  1924. er = EMULATE_PRIV_FAIL;
  1925. break;
  1926. default:
  1927. er = EMULATE_PRIV_FAIL;
  1928. break;
  1929. }
  1930. }
  1931. if (er == EMULATE_PRIV_FAIL)
  1932. kvm_mips_emulate_exc(cause, opc, run, vcpu);
  1933. return er;
  1934. }
  1935. /*
  1936. * User Address (UA) fault, this could happen if
  1937. * (1) TLB entry not present/valid in both Guest and shadow host TLBs, in this
  1938. * case we pass on the fault to the guest kernel and let it handle it.
  1939. * (2) TLB entry is present in the Guest TLB but not in the shadow, in this
  1940. * case we inject the TLB from the Guest TLB into the shadow host TLB
  1941. */
  1942. enum emulation_result kvm_mips_handle_tlbmiss(unsigned long cause,
  1943. uint32_t *opc,
  1944. struct kvm_run *run,
  1945. struct kvm_vcpu *vcpu)
  1946. {
  1947. enum emulation_result er = EMULATE_DONE;
  1948. uint32_t exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
  1949. unsigned long va = vcpu->arch.host_cp0_badvaddr;
  1950. int index;
  1951. kvm_debug("kvm_mips_handle_tlbmiss: badvaddr: %#lx, entryhi: %#lx\n",
  1952. vcpu->arch.host_cp0_badvaddr, vcpu->arch.host_cp0_entryhi);
  1953. /*
  1954. * KVM would not have got the exception if this entry was valid in the
  1955. * shadow host TLB. Check the Guest TLB, if the entry is not there then
  1956. * send the guest an exception. The guest exc handler should then inject
  1957. * an entry into the guest TLB.
  1958. */
  1959. index = kvm_mips_guest_tlb_lookup(vcpu,
  1960. (va & VPN2_MASK) |
  1961. (kvm_read_c0_guest_entryhi
  1962. (vcpu->arch.cop0) & ASID_MASK));
  1963. if (index < 0) {
  1964. if (exccode == T_TLB_LD_MISS) {
  1965. er = kvm_mips_emulate_tlbmiss_ld(cause, opc, run, vcpu);
  1966. } else if (exccode == T_TLB_ST_MISS) {
  1967. er = kvm_mips_emulate_tlbmiss_st(cause, opc, run, vcpu);
  1968. } else {
  1969. kvm_err("%s: invalid exc code: %d\n", __func__,
  1970. exccode);
  1971. er = EMULATE_FAIL;
  1972. }
  1973. } else {
  1974. struct kvm_mips_tlb *tlb = &vcpu->arch.guest_tlb[index];
  1975. /*
  1976. * Check if the entry is valid, if not then setup a TLB invalid
  1977. * exception to the guest
  1978. */
  1979. if (!TLB_IS_VALID(*tlb, va)) {
  1980. if (exccode == T_TLB_LD_MISS) {
  1981. er = kvm_mips_emulate_tlbinv_ld(cause, opc, run,
  1982. vcpu);
  1983. } else if (exccode == T_TLB_ST_MISS) {
  1984. er = kvm_mips_emulate_tlbinv_st(cause, opc, run,
  1985. vcpu);
  1986. } else {
  1987. kvm_err("%s: invalid exc code: %d\n", __func__,
  1988. exccode);
  1989. er = EMULATE_FAIL;
  1990. }
  1991. } else {
  1992. kvm_debug("Injecting hi: %#lx, lo0: %#lx, lo1: %#lx into shadow host TLB\n",
  1993. tlb->tlb_hi, tlb->tlb_lo0, tlb->tlb_lo1);
  1994. /*
  1995. * OK we have a Guest TLB entry, now inject it into the
  1996. * shadow host TLB
  1997. */
  1998. kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb, NULL,
  1999. NULL);
  2000. }
  2001. }
  2002. return er;
  2003. }