emulate.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * KVM/MIPS: Instruction/Exception emulation
  7. *
  8. * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
  9. * Authors: Sanjay Lal <sanjayl@kymasys.com>
  10. */
  11. #include <linux/errno.h>
  12. #include <linux/err.h>
  13. #include <linux/ktime.h>
  14. #include <linux/kvm_host.h>
  15. #include <linux/module.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/fs.h>
  18. #include <linux/bootmem.h>
  19. #include <linux/random.h>
  20. #include <asm/page.h>
  21. #include <asm/cacheflush.h>
  22. #include <asm/cacheops.h>
  23. #include <asm/cpu-info.h>
  24. #include <asm/mmu_context.h>
  25. #include <asm/tlbflush.h>
  26. #include <asm/inst.h>
  27. #undef CONFIG_MIPS_MT
  28. #include <asm/r4kcache.h>
  29. #define CONFIG_MIPS_MT
  30. #include "interrupt.h"
  31. #include "commpage.h"
  32. #include "trace.h"
  33. /*
  34. * Compute the return address and do emulate branch simulation, if required.
  35. * This function should be called only in branch delay slot active.
  36. */
  37. unsigned long kvm_compute_return_epc(struct kvm_vcpu *vcpu,
  38. unsigned long instpc)
  39. {
  40. unsigned int dspcontrol;
  41. union mips_instruction insn;
  42. struct kvm_vcpu_arch *arch = &vcpu->arch;
  43. long epc = instpc;
  44. long nextpc = KVM_INVALID_INST;
  45. if (epc & 3)
  46. goto unaligned;
  47. /* Read the instruction */
  48. insn.word = kvm_get_inst((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. struct mips_coproc *cop0 = vcpu->arch.cop0;
  269. ktime_t expires, threshold;
  270. uint32_t count, compare;
  271. int running;
  272. /* Calculate the biased and scaled guest CP0_Count */
  273. count = vcpu->arch.count_bias + kvm_mips_ktime_to_count(vcpu, now);
  274. compare = kvm_read_c0_guest_compare(cop0);
  275. /*
  276. * Find whether CP0_Count has reached the closest timer interrupt. If
  277. * not, we shouldn't inject it.
  278. */
  279. if ((int32_t)(count - compare) < 0)
  280. return count;
  281. /*
  282. * The CP0_Count we're going to return has already reached the closest
  283. * timer interrupt. Quickly check if it really is a new interrupt by
  284. * looking at whether the interval until the hrtimer expiry time is
  285. * less than 1/4 of the timer period.
  286. */
  287. expires = hrtimer_get_expires(&vcpu->arch.comparecount_timer);
  288. threshold = ktime_add_ns(now, vcpu->arch.count_period / 4);
  289. if (ktime_before(expires, threshold)) {
  290. /*
  291. * Cancel it while we handle it so there's no chance of
  292. * interference with the timeout handler.
  293. */
  294. running = hrtimer_cancel(&vcpu->arch.comparecount_timer);
  295. /* Nothing should be waiting on the timeout */
  296. kvm_mips_callbacks->queue_timer_int(vcpu);
  297. /*
  298. * Restart the timer if it was running based on the expiry time
  299. * we read, so that we don't push it back 2 periods.
  300. */
  301. if (running) {
  302. expires = ktime_add_ns(expires,
  303. vcpu->arch.count_period);
  304. hrtimer_start(&vcpu->arch.comparecount_timer, expires,
  305. HRTIMER_MODE_ABS);
  306. }
  307. }
  308. return count;
  309. }
  310. /**
  311. * kvm_mips_read_count() - Read the current count value.
  312. * @vcpu: Virtual CPU.
  313. *
  314. * Read the current guest CP0_Count value, taking into account whether the timer
  315. * is stopped.
  316. *
  317. * Returns: The current guest CP0_Count value.
  318. */
  319. uint32_t kvm_mips_read_count(struct kvm_vcpu *vcpu)
  320. {
  321. struct mips_coproc *cop0 = vcpu->arch.cop0;
  322. /* If count disabled just read static copy of count */
  323. if (kvm_mips_count_disabled(vcpu))
  324. return kvm_read_c0_guest_count(cop0);
  325. return kvm_mips_read_count_running(vcpu, ktime_get());
  326. }
  327. /**
  328. * kvm_mips_freeze_hrtimer() - Safely stop the hrtimer.
  329. * @vcpu: Virtual CPU.
  330. * @count: Output pointer for CP0_Count value at point of freeze.
  331. *
  332. * Freeze the hrtimer safely and return both the ktime and the CP0_Count value
  333. * at the point it was frozen. It is guaranteed that any pending interrupts at
  334. * the point it was frozen are handled, and none after that point.
  335. *
  336. * This is useful where the time/CP0_Count is needed in the calculation of the
  337. * new parameters.
  338. *
  339. * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
  340. *
  341. * Returns: The ktime at the point of freeze.
  342. */
  343. static ktime_t kvm_mips_freeze_hrtimer(struct kvm_vcpu *vcpu,
  344. uint32_t *count)
  345. {
  346. ktime_t now;
  347. /* stop hrtimer before finding time */
  348. hrtimer_cancel(&vcpu->arch.comparecount_timer);
  349. now = ktime_get();
  350. /* find count at this point and handle pending hrtimer */
  351. *count = kvm_mips_read_count_running(vcpu, now);
  352. return now;
  353. }
  354. /**
  355. * kvm_mips_resume_hrtimer() - Resume hrtimer, updating expiry.
  356. * @vcpu: Virtual CPU.
  357. * @now: ktime at point of resume.
  358. * @count: CP0_Count at point of resume.
  359. *
  360. * Resumes the timer and updates the timer expiry based on @now and @count.
  361. * This can be used in conjunction with kvm_mips_freeze_timer() when timer
  362. * parameters need to be changed.
  363. *
  364. * It is guaranteed that a timer interrupt immediately after resume will be
  365. * handled, but not if CP_Compare is exactly at @count. That case is already
  366. * handled by kvm_mips_freeze_timer().
  367. *
  368. * Assumes !kvm_mips_count_disabled(@vcpu) (guest CP0_Count timer is running).
  369. */
  370. static void kvm_mips_resume_hrtimer(struct kvm_vcpu *vcpu,
  371. ktime_t now, uint32_t count)
  372. {
  373. struct mips_coproc *cop0 = vcpu->arch.cop0;
  374. uint32_t compare;
  375. u64 delta;
  376. ktime_t expire;
  377. /* Calculate timeout (wrap 0 to 2^32) */
  378. compare = kvm_read_c0_guest_compare(cop0);
  379. delta = (u64)(uint32_t)(compare - count - 1) + 1;
  380. delta = div_u64(delta * NSEC_PER_SEC, vcpu->arch.count_hz);
  381. expire = ktime_add_ns(now, delta);
  382. /* Update hrtimer to use new timeout */
  383. hrtimer_cancel(&vcpu->arch.comparecount_timer);
  384. hrtimer_start(&vcpu->arch.comparecount_timer, expire, HRTIMER_MODE_ABS);
  385. }
  386. /**
  387. * kvm_mips_write_count() - Modify the count and update timer.
  388. * @vcpu: Virtual CPU.
  389. * @count: Guest CP0_Count value to set.
  390. *
  391. * Sets the CP0_Count value and updates the timer accordingly.
  392. */
  393. void kvm_mips_write_count(struct kvm_vcpu *vcpu, uint32_t count)
  394. {
  395. struct mips_coproc *cop0 = vcpu->arch.cop0;
  396. ktime_t now;
  397. /* Calculate bias */
  398. now = kvm_mips_count_time(vcpu);
  399. vcpu->arch.count_bias = count - kvm_mips_ktime_to_count(vcpu, now);
  400. if (kvm_mips_count_disabled(vcpu))
  401. /* The timer's disabled, adjust the static count */
  402. kvm_write_c0_guest_count(cop0, count);
  403. else
  404. /* Update timeout */
  405. kvm_mips_resume_hrtimer(vcpu, now, count);
  406. }
  407. /**
  408. * kvm_mips_init_count() - Initialise timer.
  409. * @vcpu: Virtual CPU.
  410. *
  411. * Initialise the timer to a sensible frequency, namely 100MHz, zero it, and set
  412. * it going if it's enabled.
  413. */
  414. void kvm_mips_init_count(struct kvm_vcpu *vcpu)
  415. {
  416. /* 100 MHz */
  417. vcpu->arch.count_hz = 100*1000*1000;
  418. vcpu->arch.count_period = div_u64((u64)NSEC_PER_SEC << 32,
  419. vcpu->arch.count_hz);
  420. vcpu->arch.count_dyn_bias = 0;
  421. /* Starting at 0 */
  422. kvm_mips_write_count(vcpu, 0);
  423. }
  424. /**
  425. * kvm_mips_set_count_hz() - Update the frequency of the timer.
  426. * @vcpu: Virtual CPU.
  427. * @count_hz: Frequency of CP0_Count timer in Hz.
  428. *
  429. * Change the frequency of the CP0_Count timer. This is done atomically so that
  430. * CP0_Count is continuous and no timer interrupt is lost.
  431. *
  432. * Returns: -EINVAL if @count_hz is out of range.
  433. * 0 on success.
  434. */
  435. int kvm_mips_set_count_hz(struct kvm_vcpu *vcpu, s64 count_hz)
  436. {
  437. struct mips_coproc *cop0 = vcpu->arch.cop0;
  438. int dc;
  439. ktime_t now;
  440. u32 count;
  441. /* ensure the frequency is in a sensible range... */
  442. if (count_hz <= 0 || count_hz > NSEC_PER_SEC)
  443. return -EINVAL;
  444. /* ... and has actually changed */
  445. if (vcpu->arch.count_hz == count_hz)
  446. return 0;
  447. /* Safely freeze timer so we can keep it continuous */
  448. dc = kvm_mips_count_disabled(vcpu);
  449. if (dc) {
  450. now = kvm_mips_count_time(vcpu);
  451. count = kvm_read_c0_guest_count(cop0);
  452. } else {
  453. now = kvm_mips_freeze_hrtimer(vcpu, &count);
  454. }
  455. /* Update the frequency */
  456. vcpu->arch.count_hz = count_hz;
  457. vcpu->arch.count_period = div_u64((u64)NSEC_PER_SEC << 32, count_hz);
  458. vcpu->arch.count_dyn_bias = 0;
  459. /* Calculate adjusted bias so dynamic count is unchanged */
  460. vcpu->arch.count_bias = count - kvm_mips_ktime_to_count(vcpu, now);
  461. /* Update and resume hrtimer */
  462. if (!dc)
  463. kvm_mips_resume_hrtimer(vcpu, now, count);
  464. return 0;
  465. }
  466. /**
  467. * kvm_mips_write_compare() - Modify compare and update timer.
  468. * @vcpu: Virtual CPU.
  469. * @compare: New CP0_Compare value.
  470. * @ack: Whether to acknowledge timer interrupt.
  471. *
  472. * Update CP0_Compare to a new value and update the timeout.
  473. * If @ack, atomically acknowledge any pending timer interrupt, otherwise ensure
  474. * any pending timer interrupt is preserved.
  475. */
  476. void kvm_mips_write_compare(struct kvm_vcpu *vcpu, uint32_t compare, bool ack)
  477. {
  478. struct mips_coproc *cop0 = vcpu->arch.cop0;
  479. int dc;
  480. u32 old_compare = kvm_read_c0_guest_compare(cop0);
  481. ktime_t now;
  482. uint32_t count;
  483. /* if unchanged, must just be an ack */
  484. if (old_compare == compare) {
  485. if (!ack)
  486. return;
  487. kvm_mips_callbacks->dequeue_timer_int(vcpu);
  488. kvm_write_c0_guest_compare(cop0, compare);
  489. return;
  490. }
  491. /* freeze_hrtimer() takes care of timer interrupts <= count */
  492. dc = kvm_mips_count_disabled(vcpu);
  493. if (!dc)
  494. now = kvm_mips_freeze_hrtimer(vcpu, &count);
  495. if (ack)
  496. kvm_mips_callbacks->dequeue_timer_int(vcpu);
  497. kvm_write_c0_guest_compare(cop0, compare);
  498. /* resume_hrtimer() takes care of timer interrupts > count */
  499. if (!dc)
  500. kvm_mips_resume_hrtimer(vcpu, now, count);
  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. kvm_err("[%#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. kvm_debug("[%#lx] !!!WAIT!!! (%#lx)\n", vcpu->arch.pc,
  682. vcpu->arch.pending_exceptions);
  683. ++vcpu->stat.wait_exits;
  684. trace_kvm_exit(vcpu, WAIT_EXITS);
  685. if (!vcpu->arch.pending_exceptions) {
  686. vcpu->arch.wait = 1;
  687. kvm_vcpu_block(vcpu);
  688. /*
  689. * We we are runnable, then definitely go off to user space to
  690. * check if any 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 EMULATE_DONE;
  698. }
  699. /*
  700. * XXXKYMA: Linux doesn't seem to use TLBR, return EMULATE_FAIL for now so that
  701. * we can catch this, if things ever change
  702. */
  703. enum emulation_result kvm_mips_emul_tlbr(struct kvm_vcpu *vcpu)
  704. {
  705. struct mips_coproc *cop0 = vcpu->arch.cop0;
  706. uint32_t pc = vcpu->arch.pc;
  707. kvm_err("[%#x] COP0_TLBR [%ld]\n", pc, kvm_read_c0_guest_index(cop0));
  708. return EMULATE_FAIL;
  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. struct kvm_mips_tlb *tlb = NULL;
  716. uint32_t pc = vcpu->arch.pc;
  717. if (index < 0 || index >= KVM_MIPS_GUEST_TLB_SIZE) {
  718. kvm_debug("%s: illegal index: %d\n", __func__, index);
  719. kvm_debug("[%#x] COP0_TLBWI [%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx, mask: %#lx)\n",
  720. pc, index, kvm_read_c0_guest_entryhi(cop0),
  721. kvm_read_c0_guest_entrylo0(cop0),
  722. kvm_read_c0_guest_entrylo1(cop0),
  723. kvm_read_c0_guest_pagemask(cop0));
  724. index = (index & ~0x80000000) % KVM_MIPS_GUEST_TLB_SIZE;
  725. }
  726. tlb = &vcpu->arch.guest_tlb[index];
  727. /*
  728. * Probe the shadow host TLB for the entry being overwritten, if one
  729. * matches, invalidate it
  730. */
  731. kvm_mips_host_tlb_inv(vcpu, tlb->tlb_hi);
  732. tlb->tlb_mask = kvm_read_c0_guest_pagemask(cop0);
  733. tlb->tlb_hi = kvm_read_c0_guest_entryhi(cop0);
  734. tlb->tlb_lo0 = kvm_read_c0_guest_entrylo0(cop0);
  735. tlb->tlb_lo1 = kvm_read_c0_guest_entrylo1(cop0);
  736. kvm_debug("[%#x] COP0_TLBWI [%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx, mask: %#lx)\n",
  737. pc, index, kvm_read_c0_guest_entryhi(cop0),
  738. kvm_read_c0_guest_entrylo0(cop0),
  739. kvm_read_c0_guest_entrylo1(cop0),
  740. kvm_read_c0_guest_pagemask(cop0));
  741. return EMULATE_DONE;
  742. }
  743. /* Write Guest TLB Entry @ Random Index */
  744. enum emulation_result kvm_mips_emul_tlbwr(struct kvm_vcpu *vcpu)
  745. {
  746. struct mips_coproc *cop0 = vcpu->arch.cop0;
  747. struct kvm_mips_tlb *tlb = NULL;
  748. uint32_t pc = vcpu->arch.pc;
  749. int index;
  750. get_random_bytes(&index, sizeof(index));
  751. index &= (KVM_MIPS_GUEST_TLB_SIZE - 1);
  752. tlb = &vcpu->arch.guest_tlb[index];
  753. /*
  754. * Probe the shadow host TLB for the entry being overwritten, if one
  755. * matches, invalidate it
  756. */
  757. kvm_mips_host_tlb_inv(vcpu, tlb->tlb_hi);
  758. tlb->tlb_mask = kvm_read_c0_guest_pagemask(cop0);
  759. tlb->tlb_hi = kvm_read_c0_guest_entryhi(cop0);
  760. tlb->tlb_lo0 = kvm_read_c0_guest_entrylo0(cop0);
  761. tlb->tlb_lo1 = kvm_read_c0_guest_entrylo1(cop0);
  762. kvm_debug("[%#x] COP0_TLBWR[%d] (entryhi: %#lx, entrylo0: %#lx entrylo1: %#lx)\n",
  763. pc, index, kvm_read_c0_guest_entryhi(cop0),
  764. kvm_read_c0_guest_entrylo0(cop0),
  765. kvm_read_c0_guest_entrylo1(cop0));
  766. return EMULATE_DONE;
  767. }
  768. enum emulation_result kvm_mips_emul_tlbp(struct kvm_vcpu *vcpu)
  769. {
  770. struct mips_coproc *cop0 = vcpu->arch.cop0;
  771. long entryhi = kvm_read_c0_guest_entryhi(cop0);
  772. uint32_t pc = vcpu->arch.pc;
  773. int index = -1;
  774. index = kvm_mips_guest_tlb_lookup(vcpu, entryhi);
  775. kvm_write_c0_guest_index(cop0, index);
  776. kvm_debug("[%#x] COP0_TLBP (entryhi: %#lx), index: %d\n", pc, entryhi,
  777. index);
  778. return EMULATE_DONE;
  779. }
  780. /**
  781. * kvm_mips_config1_wrmask() - Find mask of writable bits in guest Config1
  782. * @vcpu: Virtual CPU.
  783. *
  784. * Finds the mask of bits which are writable in the guest's Config1 CP0
  785. * register, by userland (currently read-only to the guest).
  786. */
  787. unsigned int kvm_mips_config1_wrmask(struct kvm_vcpu *vcpu)
  788. {
  789. unsigned int mask = 0;
  790. /* Permit FPU to be present if FPU is supported */
  791. if (kvm_mips_guest_can_have_fpu(&vcpu->arch))
  792. mask |= MIPS_CONF1_FP;
  793. return mask;
  794. }
  795. /**
  796. * kvm_mips_config3_wrmask() - Find mask of writable bits in guest Config3
  797. * @vcpu: Virtual CPU.
  798. *
  799. * Finds the mask of bits which are writable in the guest's Config3 CP0
  800. * register, by userland (currently read-only to the guest).
  801. */
  802. unsigned int kvm_mips_config3_wrmask(struct kvm_vcpu *vcpu)
  803. {
  804. /* Config4 is optional */
  805. unsigned int mask = MIPS_CONF_M;
  806. /* Permit MSA to be present if MSA is supported */
  807. if (kvm_mips_guest_can_have_msa(&vcpu->arch))
  808. mask |= MIPS_CONF3_MSA;
  809. return mask;
  810. }
  811. /**
  812. * kvm_mips_config4_wrmask() - Find mask of writable bits in guest Config4
  813. * @vcpu: Virtual CPU.
  814. *
  815. * Finds the mask of bits which are writable in the guest's Config4 CP0
  816. * register, by userland (currently read-only to the guest).
  817. */
  818. unsigned int kvm_mips_config4_wrmask(struct kvm_vcpu *vcpu)
  819. {
  820. /* Config5 is optional */
  821. return MIPS_CONF_M;
  822. }
  823. /**
  824. * kvm_mips_config5_wrmask() - Find mask of writable bits in guest Config5
  825. * @vcpu: Virtual CPU.
  826. *
  827. * Finds the mask of bits which are writable in the guest's Config5 CP0
  828. * register, by the guest itself.
  829. */
  830. unsigned int kvm_mips_config5_wrmask(struct kvm_vcpu *vcpu)
  831. {
  832. unsigned int mask = 0;
  833. /* Permit MSAEn changes if MSA supported and enabled */
  834. if (kvm_mips_guest_has_msa(&vcpu->arch))
  835. mask |= MIPS_CONF5_MSAEN;
  836. /*
  837. * Permit guest FPU mode changes if FPU is enabled and the relevant
  838. * feature exists according to FIR register.
  839. */
  840. if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
  841. if (cpu_has_fre)
  842. mask |= MIPS_CONF5_FRE;
  843. /* We don't support UFR or UFE */
  844. }
  845. return mask;
  846. }
  847. enum emulation_result kvm_mips_emulate_CP0(uint32_t inst, uint32_t *opc,
  848. uint32_t cause, struct kvm_run *run,
  849. struct kvm_vcpu *vcpu)
  850. {
  851. struct mips_coproc *cop0 = vcpu->arch.cop0;
  852. enum emulation_result er = EMULATE_DONE;
  853. int32_t rt, rd, copz, sel, co_bit, op;
  854. uint32_t pc = vcpu->arch.pc;
  855. unsigned long curr_pc;
  856. /*
  857. * Update PC and hold onto current PC in case there is
  858. * an error and we want to rollback the PC
  859. */
  860. curr_pc = vcpu->arch.pc;
  861. er = update_pc(vcpu, cause);
  862. if (er == EMULATE_FAIL)
  863. return er;
  864. copz = (inst >> 21) & 0x1f;
  865. rt = (inst >> 16) & 0x1f;
  866. rd = (inst >> 11) & 0x1f;
  867. sel = inst & 0x7;
  868. co_bit = (inst >> 25) & 1;
  869. if (co_bit) {
  870. op = (inst) & 0xff;
  871. switch (op) {
  872. case tlbr_op: /* Read indexed TLB entry */
  873. er = kvm_mips_emul_tlbr(vcpu);
  874. break;
  875. case tlbwi_op: /* Write indexed */
  876. er = kvm_mips_emul_tlbwi(vcpu);
  877. break;
  878. case tlbwr_op: /* Write random */
  879. er = kvm_mips_emul_tlbwr(vcpu);
  880. break;
  881. case tlbp_op: /* TLB Probe */
  882. er = kvm_mips_emul_tlbp(vcpu);
  883. break;
  884. case rfe_op:
  885. kvm_err("!!!COP0_RFE!!!\n");
  886. break;
  887. case eret_op:
  888. er = kvm_mips_emul_eret(vcpu);
  889. goto dont_update_pc;
  890. break;
  891. case wait_op:
  892. er = kvm_mips_emul_wait(vcpu);
  893. break;
  894. }
  895. } else {
  896. switch (copz) {
  897. case mfc_op:
  898. #ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
  899. cop0->stat[rd][sel]++;
  900. #endif
  901. /* Get reg */
  902. if ((rd == MIPS_CP0_COUNT) && (sel == 0)) {
  903. vcpu->arch.gprs[rt] = kvm_mips_read_count(vcpu);
  904. } else if ((rd == MIPS_CP0_ERRCTL) && (sel == 0)) {
  905. vcpu->arch.gprs[rt] = 0x0;
  906. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  907. kvm_mips_trans_mfc0(inst, opc, vcpu);
  908. #endif
  909. } else {
  910. vcpu->arch.gprs[rt] = cop0->reg[rd][sel];
  911. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  912. kvm_mips_trans_mfc0(inst, opc, vcpu);
  913. #endif
  914. }
  915. kvm_debug
  916. ("[%#x] MFCz[%d][%d], vcpu->arch.gprs[%d]: %#lx\n",
  917. pc, rd, sel, rt, vcpu->arch.gprs[rt]);
  918. break;
  919. case dmfc_op:
  920. vcpu->arch.gprs[rt] = cop0->reg[rd][sel];
  921. break;
  922. case mtc_op:
  923. #ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
  924. cop0->stat[rd][sel]++;
  925. #endif
  926. if ((rd == MIPS_CP0_TLB_INDEX)
  927. && (vcpu->arch.gprs[rt] >=
  928. KVM_MIPS_GUEST_TLB_SIZE)) {
  929. kvm_err("Invalid TLB Index: %ld",
  930. vcpu->arch.gprs[rt]);
  931. er = EMULATE_FAIL;
  932. break;
  933. }
  934. #define C0_EBASE_CORE_MASK 0xff
  935. if ((rd == MIPS_CP0_PRID) && (sel == 1)) {
  936. /* Preserve CORE number */
  937. kvm_change_c0_guest_ebase(cop0,
  938. ~(C0_EBASE_CORE_MASK),
  939. vcpu->arch.gprs[rt]);
  940. kvm_err("MTCz, cop0->reg[EBASE]: %#lx\n",
  941. kvm_read_c0_guest_ebase(cop0));
  942. } else if (rd == MIPS_CP0_TLB_HI && sel == 0) {
  943. uint32_t nasid =
  944. vcpu->arch.gprs[rt] & KVM_ENTRYHI_ASID;
  945. if ((KSEGX(vcpu->arch.gprs[rt]) != CKSEG0) &&
  946. ((kvm_read_c0_guest_entryhi(cop0) &
  947. KVM_ENTRYHI_ASID) != nasid)) {
  948. kvm_debug("MTCz, change ASID from %#lx to %#lx\n",
  949. kvm_read_c0_guest_entryhi(cop0)
  950. & KVM_ENTRYHI_ASID,
  951. vcpu->arch.gprs[rt]
  952. & KVM_ENTRYHI_ASID);
  953. /* Blow away the shadow host TLBs */
  954. kvm_mips_flush_host_tlb(1);
  955. }
  956. kvm_write_c0_guest_entryhi(cop0,
  957. vcpu->arch.gprs[rt]);
  958. }
  959. /* Are we writing to COUNT */
  960. else if ((rd == MIPS_CP0_COUNT) && (sel == 0)) {
  961. kvm_mips_write_count(vcpu, vcpu->arch.gprs[rt]);
  962. goto done;
  963. } else if ((rd == MIPS_CP0_COMPARE) && (sel == 0)) {
  964. kvm_debug("[%#x] MTCz, COMPARE %#lx <- %#lx\n",
  965. pc, kvm_read_c0_guest_compare(cop0),
  966. vcpu->arch.gprs[rt]);
  967. /* If we are writing to COMPARE */
  968. /* Clear pending timer interrupt, if any */
  969. kvm_mips_write_compare(vcpu,
  970. vcpu->arch.gprs[rt],
  971. true);
  972. } else if ((rd == MIPS_CP0_STATUS) && (sel == 0)) {
  973. unsigned int old_val, val, change;
  974. old_val = kvm_read_c0_guest_status(cop0);
  975. val = vcpu->arch.gprs[rt];
  976. change = val ^ old_val;
  977. /* Make sure that the NMI bit is never set */
  978. val &= ~ST0_NMI;
  979. /*
  980. * Don't allow CU1 or FR to be set unless FPU
  981. * capability enabled and exists in guest
  982. * configuration.
  983. */
  984. if (!kvm_mips_guest_has_fpu(&vcpu->arch))
  985. val &= ~(ST0_CU1 | ST0_FR);
  986. /*
  987. * Also don't allow FR to be set if host doesn't
  988. * support it.
  989. */
  990. if (!(current_cpu_data.fpu_id & MIPS_FPIR_F64))
  991. val &= ~ST0_FR;
  992. /* Handle changes in FPU mode */
  993. preempt_disable();
  994. /*
  995. * FPU and Vector register state is made
  996. * UNPREDICTABLE by a change of FR, so don't
  997. * even bother saving it.
  998. */
  999. if (change & ST0_FR)
  1000. kvm_drop_fpu(vcpu);
  1001. /*
  1002. * If MSA state is already live, it is undefined
  1003. * how it interacts with FR=0 FPU state, and we
  1004. * don't want to hit reserved instruction
  1005. * exceptions trying to save the MSA state later
  1006. * when CU=1 && FR=1, so play it safe and save
  1007. * it first.
  1008. */
  1009. if (change & ST0_CU1 && !(val & ST0_FR) &&
  1010. vcpu->arch.fpu_inuse & KVM_MIPS_FPU_MSA)
  1011. kvm_lose_fpu(vcpu);
  1012. /*
  1013. * Propagate CU1 (FPU enable) changes
  1014. * immediately if the FPU context is already
  1015. * loaded. When disabling we leave the context
  1016. * loaded so it can be quickly enabled again in
  1017. * the near future.
  1018. */
  1019. if (change & ST0_CU1 &&
  1020. vcpu->arch.fpu_inuse & KVM_MIPS_FPU_FPU)
  1021. change_c0_status(ST0_CU1, val);
  1022. preempt_enable();
  1023. kvm_write_c0_guest_status(cop0, val);
  1024. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  1025. /*
  1026. * If FPU present, we need CU1/FR bits to take
  1027. * effect fairly soon.
  1028. */
  1029. if (!kvm_mips_guest_has_fpu(&vcpu->arch))
  1030. kvm_mips_trans_mtc0(inst, opc, vcpu);
  1031. #endif
  1032. } else if ((rd == MIPS_CP0_CONFIG) && (sel == 5)) {
  1033. unsigned int old_val, val, change, wrmask;
  1034. old_val = kvm_read_c0_guest_config5(cop0);
  1035. val = vcpu->arch.gprs[rt];
  1036. /* Only a few bits are writable in Config5 */
  1037. wrmask = kvm_mips_config5_wrmask(vcpu);
  1038. change = (val ^ old_val) & wrmask;
  1039. val = old_val ^ change;
  1040. /* Handle changes in FPU/MSA modes */
  1041. preempt_disable();
  1042. /*
  1043. * Propagate FRE changes immediately if the FPU
  1044. * context is already loaded.
  1045. */
  1046. if (change & MIPS_CONF5_FRE &&
  1047. vcpu->arch.fpu_inuse & KVM_MIPS_FPU_FPU)
  1048. change_c0_config5(MIPS_CONF5_FRE, val);
  1049. /*
  1050. * Propagate MSAEn changes immediately if the
  1051. * MSA context is already loaded. When disabling
  1052. * we leave the context loaded so it can be
  1053. * quickly enabled again in the near future.
  1054. */
  1055. if (change & MIPS_CONF5_MSAEN &&
  1056. vcpu->arch.fpu_inuse & KVM_MIPS_FPU_MSA)
  1057. change_c0_config5(MIPS_CONF5_MSAEN,
  1058. val);
  1059. preempt_enable();
  1060. kvm_write_c0_guest_config5(cop0, val);
  1061. } else if ((rd == MIPS_CP0_CAUSE) && (sel == 0)) {
  1062. uint32_t old_cause, new_cause;
  1063. old_cause = kvm_read_c0_guest_cause(cop0);
  1064. new_cause = vcpu->arch.gprs[rt];
  1065. /* Update R/W bits */
  1066. kvm_change_c0_guest_cause(cop0, 0x08800300,
  1067. new_cause);
  1068. /* DC bit enabling/disabling timer? */
  1069. if ((old_cause ^ new_cause) & CAUSEF_DC) {
  1070. if (new_cause & CAUSEF_DC)
  1071. kvm_mips_count_disable_cause(vcpu);
  1072. else
  1073. kvm_mips_count_enable_cause(vcpu);
  1074. }
  1075. } else {
  1076. cop0->reg[rd][sel] = vcpu->arch.gprs[rt];
  1077. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  1078. kvm_mips_trans_mtc0(inst, opc, vcpu);
  1079. #endif
  1080. }
  1081. kvm_debug("[%#x] MTCz, cop0->reg[%d][%d]: %#lx\n", pc,
  1082. rd, sel, cop0->reg[rd][sel]);
  1083. break;
  1084. case dmtc_op:
  1085. kvm_err("!!!!!!![%#lx]dmtc_op: rt: %d, rd: %d, sel: %d!!!!!!\n",
  1086. vcpu->arch.pc, rt, rd, sel);
  1087. er = EMULATE_FAIL;
  1088. break;
  1089. case mfmc0_op:
  1090. #ifdef KVM_MIPS_DEBUG_COP0_COUNTERS
  1091. cop0->stat[MIPS_CP0_STATUS][0]++;
  1092. #endif
  1093. if (rt != 0)
  1094. vcpu->arch.gprs[rt] =
  1095. kvm_read_c0_guest_status(cop0);
  1096. /* EI */
  1097. if (inst & 0x20) {
  1098. kvm_debug("[%#lx] mfmc0_op: EI\n",
  1099. vcpu->arch.pc);
  1100. kvm_set_c0_guest_status(cop0, ST0_IE);
  1101. } else {
  1102. kvm_debug("[%#lx] mfmc0_op: DI\n",
  1103. vcpu->arch.pc);
  1104. kvm_clear_c0_guest_status(cop0, ST0_IE);
  1105. }
  1106. break;
  1107. case wrpgpr_op:
  1108. {
  1109. uint32_t css =
  1110. cop0->reg[MIPS_CP0_STATUS][2] & 0xf;
  1111. uint32_t pss =
  1112. (cop0->reg[MIPS_CP0_STATUS][2] >> 6) & 0xf;
  1113. /*
  1114. * We don't support any shadow register sets, so
  1115. * SRSCtl[PSS] == SRSCtl[CSS] = 0
  1116. */
  1117. if (css || pss) {
  1118. er = EMULATE_FAIL;
  1119. break;
  1120. }
  1121. kvm_debug("WRPGPR[%d][%d] = %#lx\n", pss, rd,
  1122. vcpu->arch.gprs[rt]);
  1123. vcpu->arch.gprs[rd] = vcpu->arch.gprs[rt];
  1124. }
  1125. break;
  1126. default:
  1127. kvm_err("[%#lx]MachEmulateCP0: unsupported COP0, copz: 0x%x\n",
  1128. vcpu->arch.pc, copz);
  1129. er = EMULATE_FAIL;
  1130. break;
  1131. }
  1132. }
  1133. done:
  1134. /* Rollback PC only if emulation was unsuccessful */
  1135. if (er == EMULATE_FAIL)
  1136. vcpu->arch.pc = curr_pc;
  1137. dont_update_pc:
  1138. /*
  1139. * This is for special instructions whose emulation
  1140. * updates the PC, so do not overwrite the PC under
  1141. * any circumstances
  1142. */
  1143. return er;
  1144. }
  1145. enum emulation_result kvm_mips_emulate_store(uint32_t inst, uint32_t cause,
  1146. struct kvm_run *run,
  1147. struct kvm_vcpu *vcpu)
  1148. {
  1149. enum emulation_result er = EMULATE_DO_MMIO;
  1150. int32_t op, base, rt, offset;
  1151. uint32_t bytes;
  1152. void *data = run->mmio.data;
  1153. unsigned long curr_pc;
  1154. /*
  1155. * Update PC and hold onto current PC in case there is
  1156. * an error and we want to rollback the PC
  1157. */
  1158. curr_pc = vcpu->arch.pc;
  1159. er = update_pc(vcpu, cause);
  1160. if (er == EMULATE_FAIL)
  1161. return er;
  1162. rt = (inst >> 16) & 0x1f;
  1163. base = (inst >> 21) & 0x1f;
  1164. offset = inst & 0xffff;
  1165. op = (inst >> 26) & 0x3f;
  1166. switch (op) {
  1167. case sb_op:
  1168. bytes = 1;
  1169. if (bytes > sizeof(run->mmio.data)) {
  1170. kvm_err("%s: bad MMIO length: %d\n", __func__,
  1171. run->mmio.len);
  1172. }
  1173. run->mmio.phys_addr =
  1174. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  1175. host_cp0_badvaddr);
  1176. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  1177. er = EMULATE_FAIL;
  1178. break;
  1179. }
  1180. run->mmio.len = bytes;
  1181. run->mmio.is_write = 1;
  1182. vcpu->mmio_needed = 1;
  1183. vcpu->mmio_is_write = 1;
  1184. *(u8 *) data = vcpu->arch.gprs[rt];
  1185. kvm_debug("OP_SB: eaddr: %#lx, gpr: %#lx, data: %#x\n",
  1186. vcpu->arch.host_cp0_badvaddr, vcpu->arch.gprs[rt],
  1187. *(uint8_t *) data);
  1188. break;
  1189. case sw_op:
  1190. bytes = 4;
  1191. if (bytes > sizeof(run->mmio.data)) {
  1192. kvm_err("%s: bad MMIO length: %d\n", __func__,
  1193. run->mmio.len);
  1194. }
  1195. run->mmio.phys_addr =
  1196. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  1197. host_cp0_badvaddr);
  1198. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  1199. er = EMULATE_FAIL;
  1200. break;
  1201. }
  1202. run->mmio.len = bytes;
  1203. run->mmio.is_write = 1;
  1204. vcpu->mmio_needed = 1;
  1205. vcpu->mmio_is_write = 1;
  1206. *(uint32_t *) data = vcpu->arch.gprs[rt];
  1207. kvm_debug("[%#lx] OP_SW: eaddr: %#lx, gpr: %#lx, data: %#x\n",
  1208. vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
  1209. vcpu->arch.gprs[rt], *(uint32_t *) data);
  1210. break;
  1211. case sh_op:
  1212. bytes = 2;
  1213. if (bytes > sizeof(run->mmio.data)) {
  1214. kvm_err("%s: bad MMIO length: %d\n", __func__,
  1215. run->mmio.len);
  1216. }
  1217. run->mmio.phys_addr =
  1218. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  1219. host_cp0_badvaddr);
  1220. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  1221. er = EMULATE_FAIL;
  1222. break;
  1223. }
  1224. run->mmio.len = bytes;
  1225. run->mmio.is_write = 1;
  1226. vcpu->mmio_needed = 1;
  1227. vcpu->mmio_is_write = 1;
  1228. *(uint16_t *) data = vcpu->arch.gprs[rt];
  1229. kvm_debug("[%#lx] OP_SH: eaddr: %#lx, gpr: %#lx, data: %#x\n",
  1230. vcpu->arch.pc, vcpu->arch.host_cp0_badvaddr,
  1231. vcpu->arch.gprs[rt], *(uint32_t *) data);
  1232. break;
  1233. default:
  1234. kvm_err("Store not yet supported");
  1235. er = EMULATE_FAIL;
  1236. break;
  1237. }
  1238. /* Rollback PC if emulation was unsuccessful */
  1239. if (er == EMULATE_FAIL)
  1240. vcpu->arch.pc = curr_pc;
  1241. return er;
  1242. }
  1243. enum emulation_result kvm_mips_emulate_load(uint32_t inst, uint32_t cause,
  1244. struct kvm_run *run,
  1245. struct kvm_vcpu *vcpu)
  1246. {
  1247. enum emulation_result er = EMULATE_DO_MMIO;
  1248. int32_t op, base, rt, offset;
  1249. uint32_t bytes;
  1250. rt = (inst >> 16) & 0x1f;
  1251. base = (inst >> 21) & 0x1f;
  1252. offset = inst & 0xffff;
  1253. op = (inst >> 26) & 0x3f;
  1254. vcpu->arch.pending_load_cause = cause;
  1255. vcpu->arch.io_gpr = rt;
  1256. switch (op) {
  1257. case lw_op:
  1258. bytes = 4;
  1259. if (bytes > sizeof(run->mmio.data)) {
  1260. kvm_err("%s: bad MMIO length: %d\n", __func__,
  1261. run->mmio.len);
  1262. er = EMULATE_FAIL;
  1263. break;
  1264. }
  1265. run->mmio.phys_addr =
  1266. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  1267. host_cp0_badvaddr);
  1268. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  1269. er = EMULATE_FAIL;
  1270. break;
  1271. }
  1272. run->mmio.len = bytes;
  1273. run->mmio.is_write = 0;
  1274. vcpu->mmio_needed = 1;
  1275. vcpu->mmio_is_write = 0;
  1276. break;
  1277. case lh_op:
  1278. case lhu_op:
  1279. bytes = 2;
  1280. if (bytes > sizeof(run->mmio.data)) {
  1281. kvm_err("%s: bad MMIO length: %d\n", __func__,
  1282. run->mmio.len);
  1283. er = EMULATE_FAIL;
  1284. break;
  1285. }
  1286. run->mmio.phys_addr =
  1287. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  1288. host_cp0_badvaddr);
  1289. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  1290. er = EMULATE_FAIL;
  1291. break;
  1292. }
  1293. run->mmio.len = bytes;
  1294. run->mmio.is_write = 0;
  1295. vcpu->mmio_needed = 1;
  1296. vcpu->mmio_is_write = 0;
  1297. if (op == lh_op)
  1298. vcpu->mmio_needed = 2;
  1299. else
  1300. vcpu->mmio_needed = 1;
  1301. break;
  1302. case lbu_op:
  1303. case lb_op:
  1304. bytes = 1;
  1305. if (bytes > sizeof(run->mmio.data)) {
  1306. kvm_err("%s: bad MMIO length: %d\n", __func__,
  1307. run->mmio.len);
  1308. er = EMULATE_FAIL;
  1309. break;
  1310. }
  1311. run->mmio.phys_addr =
  1312. kvm_mips_callbacks->gva_to_gpa(vcpu->arch.
  1313. host_cp0_badvaddr);
  1314. if (run->mmio.phys_addr == KVM_INVALID_ADDR) {
  1315. er = EMULATE_FAIL;
  1316. break;
  1317. }
  1318. run->mmio.len = bytes;
  1319. run->mmio.is_write = 0;
  1320. vcpu->mmio_is_write = 0;
  1321. if (op == lb_op)
  1322. vcpu->mmio_needed = 2;
  1323. else
  1324. vcpu->mmio_needed = 1;
  1325. break;
  1326. default:
  1327. kvm_err("Load not yet supported");
  1328. er = EMULATE_FAIL;
  1329. break;
  1330. }
  1331. return er;
  1332. }
  1333. int kvm_mips_sync_icache(unsigned long va, struct kvm_vcpu *vcpu)
  1334. {
  1335. unsigned long offset = (va & ~PAGE_MASK);
  1336. struct kvm *kvm = vcpu->kvm;
  1337. unsigned long pa;
  1338. gfn_t gfn;
  1339. kvm_pfn_t pfn;
  1340. gfn = va >> PAGE_SHIFT;
  1341. if (gfn >= kvm->arch.guest_pmap_npages) {
  1342. kvm_err("%s: Invalid gfn: %#llx\n", __func__, gfn);
  1343. kvm_mips_dump_host_tlbs();
  1344. kvm_arch_vcpu_dump_regs(vcpu);
  1345. return -1;
  1346. }
  1347. pfn = kvm->arch.guest_pmap[gfn];
  1348. pa = (pfn << PAGE_SHIFT) | offset;
  1349. kvm_debug("%s: va: %#lx, unmapped: %#x\n", __func__, va,
  1350. CKSEG0ADDR(pa));
  1351. local_flush_icache_range(CKSEG0ADDR(pa), 32);
  1352. return 0;
  1353. }
  1354. enum emulation_result kvm_mips_emulate_cache(uint32_t inst, uint32_t *opc,
  1355. uint32_t cause,
  1356. struct kvm_run *run,
  1357. struct kvm_vcpu *vcpu)
  1358. {
  1359. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1360. enum emulation_result er = EMULATE_DONE;
  1361. int32_t offset, cache, op_inst, op, base;
  1362. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1363. unsigned long va;
  1364. unsigned long curr_pc;
  1365. /*
  1366. * Update PC and hold onto current PC in case there is
  1367. * an error and we want to rollback the PC
  1368. */
  1369. curr_pc = vcpu->arch.pc;
  1370. er = update_pc(vcpu, cause);
  1371. if (er == EMULATE_FAIL)
  1372. return er;
  1373. base = (inst >> 21) & 0x1f;
  1374. op_inst = (inst >> 16) & 0x1f;
  1375. offset = (int16_t)inst;
  1376. cache = op_inst & CacheOp_Cache;
  1377. op = op_inst & CacheOp_Op;
  1378. va = arch->gprs[base] + offset;
  1379. kvm_debug("CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
  1380. cache, op, base, arch->gprs[base], offset);
  1381. /*
  1382. * Treat INDEX_INV as a nop, basically issued by Linux on startup to
  1383. * invalidate the caches entirely by stepping through all the
  1384. * ways/indexes
  1385. */
  1386. if (op == Index_Writeback_Inv) {
  1387. kvm_debug("@ %#lx/%#lx CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
  1388. vcpu->arch.pc, vcpu->arch.gprs[31], cache, op, base,
  1389. arch->gprs[base], offset);
  1390. if (cache == Cache_D)
  1391. r4k_blast_dcache();
  1392. else if (cache == Cache_I)
  1393. r4k_blast_icache();
  1394. else {
  1395. kvm_err("%s: unsupported CACHE INDEX operation\n",
  1396. __func__);
  1397. return EMULATE_FAIL;
  1398. }
  1399. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  1400. kvm_mips_trans_cache_index(inst, opc, vcpu);
  1401. #endif
  1402. goto done;
  1403. }
  1404. preempt_disable();
  1405. if (KVM_GUEST_KSEGX(va) == KVM_GUEST_KSEG0) {
  1406. if (kvm_mips_host_tlb_lookup(vcpu, va) < 0)
  1407. kvm_mips_handle_kseg0_tlb_fault(va, vcpu);
  1408. } else if ((KVM_GUEST_KSEGX(va) < KVM_GUEST_KSEG0) ||
  1409. KVM_GUEST_KSEGX(va) == KVM_GUEST_KSEG23) {
  1410. int index;
  1411. /* If an entry already exists then skip */
  1412. if (kvm_mips_host_tlb_lookup(vcpu, va) >= 0)
  1413. goto skip_fault;
  1414. /*
  1415. * If address not in the guest TLB, then give the guest a fault,
  1416. * the resulting handler will do the right thing
  1417. */
  1418. index = kvm_mips_guest_tlb_lookup(vcpu, (va & VPN2_MASK) |
  1419. (kvm_read_c0_guest_entryhi
  1420. (cop0) & KVM_ENTRYHI_ASID));
  1421. if (index < 0) {
  1422. vcpu->arch.host_cp0_entryhi = (va & VPN2_MASK);
  1423. vcpu->arch.host_cp0_badvaddr = va;
  1424. vcpu->arch.pc = curr_pc;
  1425. er = kvm_mips_emulate_tlbmiss_ld(cause, NULL, run,
  1426. vcpu);
  1427. preempt_enable();
  1428. goto dont_update_pc;
  1429. } else {
  1430. struct kvm_mips_tlb *tlb = &vcpu->arch.guest_tlb[index];
  1431. /*
  1432. * Check if the entry is valid, if not then setup a TLB
  1433. * invalid exception to the guest
  1434. */
  1435. if (!TLB_IS_VALID(*tlb, va)) {
  1436. vcpu->arch.host_cp0_badvaddr = va;
  1437. vcpu->arch.pc = curr_pc;
  1438. er = kvm_mips_emulate_tlbinv_ld(cause, NULL,
  1439. run, vcpu);
  1440. preempt_enable();
  1441. goto dont_update_pc;
  1442. } else {
  1443. /*
  1444. * We fault an entry from the guest tlb to the
  1445. * shadow host TLB
  1446. */
  1447. kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb,
  1448. NULL,
  1449. NULL);
  1450. }
  1451. }
  1452. } else {
  1453. kvm_err("INVALID CACHE INDEX/ADDRESS (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
  1454. cache, op, base, arch->gprs[base], offset);
  1455. er = EMULATE_FAIL;
  1456. preempt_enable();
  1457. goto done;
  1458. }
  1459. skip_fault:
  1460. /* XXXKYMA: Only a subset of cache ops are supported, used by Linux */
  1461. if (op_inst == Hit_Writeback_Inv_D || op_inst == Hit_Invalidate_D) {
  1462. flush_dcache_line(va);
  1463. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  1464. /*
  1465. * Replace the CACHE instruction, with a SYNCI, not the same,
  1466. * but avoids a trap
  1467. */
  1468. kvm_mips_trans_cache_va(inst, opc, vcpu);
  1469. #endif
  1470. } else if (op_inst == Hit_Invalidate_I) {
  1471. flush_dcache_line(va);
  1472. flush_icache_line(va);
  1473. #ifdef CONFIG_KVM_MIPS_DYN_TRANS
  1474. /* Replace the CACHE instruction, with a SYNCI */
  1475. kvm_mips_trans_cache_va(inst, opc, vcpu);
  1476. #endif
  1477. } else {
  1478. kvm_err("NO-OP CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
  1479. cache, op, base, arch->gprs[base], offset);
  1480. er = EMULATE_FAIL;
  1481. }
  1482. preempt_enable();
  1483. done:
  1484. /* Rollback PC only if emulation was unsuccessful */
  1485. if (er == EMULATE_FAIL)
  1486. vcpu->arch.pc = curr_pc;
  1487. dont_update_pc:
  1488. /*
  1489. * This is for exceptions whose emulation updates the PC, so do not
  1490. * overwrite the PC under any circumstances
  1491. */
  1492. return er;
  1493. }
  1494. enum emulation_result kvm_mips_emulate_inst(unsigned long cause, uint32_t *opc,
  1495. struct kvm_run *run,
  1496. struct kvm_vcpu *vcpu)
  1497. {
  1498. enum emulation_result er = EMULATE_DONE;
  1499. uint32_t inst;
  1500. /* Fetch the instruction. */
  1501. if (cause & CAUSEF_BD)
  1502. opc += 1;
  1503. inst = kvm_get_inst(opc, vcpu);
  1504. switch (((union mips_instruction)inst).r_format.opcode) {
  1505. case cop0_op:
  1506. er = kvm_mips_emulate_CP0(inst, opc, cause, run, vcpu);
  1507. break;
  1508. case sb_op:
  1509. case sh_op:
  1510. case sw_op:
  1511. er = kvm_mips_emulate_store(inst, cause, run, vcpu);
  1512. break;
  1513. case lb_op:
  1514. case lbu_op:
  1515. case lhu_op:
  1516. case lh_op:
  1517. case lw_op:
  1518. er = kvm_mips_emulate_load(inst, cause, run, vcpu);
  1519. break;
  1520. case cache_op:
  1521. ++vcpu->stat.cache_exits;
  1522. trace_kvm_exit(vcpu, CACHE_EXITS);
  1523. er = kvm_mips_emulate_cache(inst, opc, cause, run, vcpu);
  1524. break;
  1525. default:
  1526. kvm_err("Instruction emulation not supported (%p/%#x)\n", opc,
  1527. inst);
  1528. kvm_arch_vcpu_dump_regs(vcpu);
  1529. er = EMULATE_FAIL;
  1530. break;
  1531. }
  1532. return er;
  1533. }
  1534. enum emulation_result kvm_mips_emulate_syscall(unsigned long cause,
  1535. uint32_t *opc,
  1536. struct kvm_run *run,
  1537. struct kvm_vcpu *vcpu)
  1538. {
  1539. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1540. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1541. enum emulation_result er = EMULATE_DONE;
  1542. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1543. /* save old pc */
  1544. kvm_write_c0_guest_epc(cop0, arch->pc);
  1545. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1546. if (cause & CAUSEF_BD)
  1547. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1548. else
  1549. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1550. kvm_debug("Delivering SYSCALL @ pc %#lx\n", arch->pc);
  1551. kvm_change_c0_guest_cause(cop0, (0xff),
  1552. (EXCCODE_SYS << CAUSEB_EXCCODE));
  1553. /* Set PC to the exception entry point */
  1554. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1555. } else {
  1556. kvm_err("Trying to deliver SYSCALL when EXL is already set\n");
  1557. er = EMULATE_FAIL;
  1558. }
  1559. return er;
  1560. }
  1561. enum emulation_result kvm_mips_emulate_tlbmiss_ld(unsigned long cause,
  1562. uint32_t *opc,
  1563. struct kvm_run *run,
  1564. struct kvm_vcpu *vcpu)
  1565. {
  1566. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1567. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1568. unsigned long entryhi = (vcpu->arch. host_cp0_badvaddr & VPN2_MASK) |
  1569. (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
  1570. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1571. /* save old pc */
  1572. kvm_write_c0_guest_epc(cop0, arch->pc);
  1573. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1574. if (cause & CAUSEF_BD)
  1575. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1576. else
  1577. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1578. kvm_debug("[EXL == 0] delivering TLB MISS @ pc %#lx\n",
  1579. arch->pc);
  1580. /* set pc to the exception entry point */
  1581. arch->pc = KVM_GUEST_KSEG0 + 0x0;
  1582. } else {
  1583. kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n",
  1584. arch->pc);
  1585. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1586. }
  1587. kvm_change_c0_guest_cause(cop0, (0xff),
  1588. (EXCCODE_TLBL << CAUSEB_EXCCODE));
  1589. /* setup badvaddr, context and entryhi registers for the guest */
  1590. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1591. /* XXXKYMA: is the context register used by linux??? */
  1592. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1593. /* Blow away the shadow host TLBs */
  1594. kvm_mips_flush_host_tlb(1);
  1595. return EMULATE_DONE;
  1596. }
  1597. enum emulation_result kvm_mips_emulate_tlbinv_ld(unsigned long cause,
  1598. uint32_t *opc,
  1599. struct kvm_run *run,
  1600. struct kvm_vcpu *vcpu)
  1601. {
  1602. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1603. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1604. unsigned long entryhi =
  1605. (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
  1606. (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
  1607. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1608. /* save old pc */
  1609. kvm_write_c0_guest_epc(cop0, arch->pc);
  1610. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1611. if (cause & CAUSEF_BD)
  1612. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1613. else
  1614. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1615. kvm_debug("[EXL == 0] delivering TLB INV @ pc %#lx\n",
  1616. arch->pc);
  1617. /* set pc to the exception entry point */
  1618. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1619. } else {
  1620. kvm_debug("[EXL == 1] delivering TLB MISS @ pc %#lx\n",
  1621. arch->pc);
  1622. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1623. }
  1624. kvm_change_c0_guest_cause(cop0, (0xff),
  1625. (EXCCODE_TLBL << CAUSEB_EXCCODE));
  1626. /* setup badvaddr, context and entryhi registers for the guest */
  1627. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1628. /* XXXKYMA: is the context register used by linux??? */
  1629. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1630. /* Blow away the shadow host TLBs */
  1631. kvm_mips_flush_host_tlb(1);
  1632. return EMULATE_DONE;
  1633. }
  1634. enum emulation_result kvm_mips_emulate_tlbmiss_st(unsigned long cause,
  1635. uint32_t *opc,
  1636. struct kvm_run *run,
  1637. struct kvm_vcpu *vcpu)
  1638. {
  1639. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1640. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1641. unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
  1642. (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
  1643. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1644. /* save old pc */
  1645. kvm_write_c0_guest_epc(cop0, arch->pc);
  1646. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1647. if (cause & CAUSEF_BD)
  1648. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1649. else
  1650. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1651. kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n",
  1652. arch->pc);
  1653. /* Set PC to the exception entry point */
  1654. arch->pc = KVM_GUEST_KSEG0 + 0x0;
  1655. } else {
  1656. kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n",
  1657. arch->pc);
  1658. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1659. }
  1660. kvm_change_c0_guest_cause(cop0, (0xff),
  1661. (EXCCODE_TLBS << CAUSEB_EXCCODE));
  1662. /* setup badvaddr, context and entryhi registers for the guest */
  1663. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1664. /* XXXKYMA: is the context register used by linux??? */
  1665. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1666. /* Blow away the shadow host TLBs */
  1667. kvm_mips_flush_host_tlb(1);
  1668. return EMULATE_DONE;
  1669. }
  1670. enum emulation_result kvm_mips_emulate_tlbinv_st(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. unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
  1678. (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
  1679. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1680. /* save old pc */
  1681. kvm_write_c0_guest_epc(cop0, arch->pc);
  1682. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1683. if (cause & CAUSEF_BD)
  1684. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1685. else
  1686. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1687. kvm_debug("[EXL == 0] Delivering TLB MISS @ pc %#lx\n",
  1688. arch->pc);
  1689. /* Set PC to the exception entry point */
  1690. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1691. } else {
  1692. kvm_debug("[EXL == 1] Delivering TLB MISS @ pc %#lx\n",
  1693. arch->pc);
  1694. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1695. }
  1696. kvm_change_c0_guest_cause(cop0, (0xff),
  1697. (EXCCODE_TLBS << CAUSEB_EXCCODE));
  1698. /* setup badvaddr, context and entryhi registers for the guest */
  1699. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1700. /* XXXKYMA: is the context register used by linux??? */
  1701. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1702. /* Blow away the shadow host TLBs */
  1703. kvm_mips_flush_host_tlb(1);
  1704. return EMULATE_DONE;
  1705. }
  1706. /* TLBMOD: store into address matching TLB with Dirty bit off */
  1707. enum emulation_result kvm_mips_handle_tlbmod(unsigned long cause, uint32_t *opc,
  1708. struct kvm_run *run,
  1709. struct kvm_vcpu *vcpu)
  1710. {
  1711. enum emulation_result er = EMULATE_DONE;
  1712. #ifdef DEBUG
  1713. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1714. unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
  1715. (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
  1716. int index;
  1717. /* If address not in the guest TLB, then we are in trouble */
  1718. index = kvm_mips_guest_tlb_lookup(vcpu, entryhi);
  1719. if (index < 0) {
  1720. /* XXXKYMA Invalidate and retry */
  1721. kvm_mips_host_tlb_inv(vcpu, vcpu->arch.host_cp0_badvaddr);
  1722. kvm_err("%s: host got TLBMOD for %#lx but entry not present in Guest TLB\n",
  1723. __func__, entryhi);
  1724. kvm_mips_dump_guest_tlbs(vcpu);
  1725. kvm_mips_dump_host_tlbs();
  1726. return EMULATE_FAIL;
  1727. }
  1728. #endif
  1729. er = kvm_mips_emulate_tlbmod(cause, opc, run, vcpu);
  1730. return er;
  1731. }
  1732. enum emulation_result kvm_mips_emulate_tlbmod(unsigned long cause,
  1733. uint32_t *opc,
  1734. struct kvm_run *run,
  1735. struct kvm_vcpu *vcpu)
  1736. {
  1737. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1738. unsigned long entryhi = (vcpu->arch.host_cp0_badvaddr & VPN2_MASK) |
  1739. (kvm_read_c0_guest_entryhi(cop0) & KVM_ENTRYHI_ASID);
  1740. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1741. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1742. /* save old pc */
  1743. kvm_write_c0_guest_epc(cop0, arch->pc);
  1744. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1745. if (cause & CAUSEF_BD)
  1746. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1747. else
  1748. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1749. kvm_debug("[EXL == 0] Delivering TLB MOD @ pc %#lx\n",
  1750. arch->pc);
  1751. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1752. } else {
  1753. kvm_debug("[EXL == 1] Delivering TLB MOD @ pc %#lx\n",
  1754. arch->pc);
  1755. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1756. }
  1757. kvm_change_c0_guest_cause(cop0, (0xff),
  1758. (EXCCODE_MOD << CAUSEB_EXCCODE));
  1759. /* setup badvaddr, context and entryhi registers for the guest */
  1760. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  1761. /* XXXKYMA: is the context register used by linux??? */
  1762. kvm_write_c0_guest_entryhi(cop0, entryhi);
  1763. /* Blow away the shadow host TLBs */
  1764. kvm_mips_flush_host_tlb(1);
  1765. return EMULATE_DONE;
  1766. }
  1767. enum emulation_result kvm_mips_emulate_fpu_exc(unsigned long cause,
  1768. uint32_t *opc,
  1769. struct kvm_run *run,
  1770. struct kvm_vcpu *vcpu)
  1771. {
  1772. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1773. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1774. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1775. /* save old pc */
  1776. kvm_write_c0_guest_epc(cop0, arch->pc);
  1777. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1778. if (cause & CAUSEF_BD)
  1779. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1780. else
  1781. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1782. }
  1783. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1784. kvm_change_c0_guest_cause(cop0, (0xff),
  1785. (EXCCODE_CPU << CAUSEB_EXCCODE));
  1786. kvm_change_c0_guest_cause(cop0, (CAUSEF_CE), (0x1 << CAUSEB_CE));
  1787. return EMULATE_DONE;
  1788. }
  1789. enum emulation_result kvm_mips_emulate_ri_exc(unsigned long cause,
  1790. uint32_t *opc,
  1791. struct kvm_run *run,
  1792. struct kvm_vcpu *vcpu)
  1793. {
  1794. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1795. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1796. enum emulation_result er = EMULATE_DONE;
  1797. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1798. /* save old pc */
  1799. kvm_write_c0_guest_epc(cop0, arch->pc);
  1800. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1801. if (cause & CAUSEF_BD)
  1802. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1803. else
  1804. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1805. kvm_debug("Delivering RI @ pc %#lx\n", arch->pc);
  1806. kvm_change_c0_guest_cause(cop0, (0xff),
  1807. (EXCCODE_RI << CAUSEB_EXCCODE));
  1808. /* Set PC to the exception entry point */
  1809. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1810. } else {
  1811. kvm_err("Trying to deliver RI when EXL is already set\n");
  1812. er = EMULATE_FAIL;
  1813. }
  1814. return er;
  1815. }
  1816. enum emulation_result kvm_mips_emulate_bp_exc(unsigned long cause,
  1817. uint32_t *opc,
  1818. struct kvm_run *run,
  1819. struct kvm_vcpu *vcpu)
  1820. {
  1821. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1822. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1823. enum emulation_result er = EMULATE_DONE;
  1824. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1825. /* save old pc */
  1826. kvm_write_c0_guest_epc(cop0, arch->pc);
  1827. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1828. if (cause & CAUSEF_BD)
  1829. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1830. else
  1831. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1832. kvm_debug("Delivering BP @ pc %#lx\n", arch->pc);
  1833. kvm_change_c0_guest_cause(cop0, (0xff),
  1834. (EXCCODE_BP << CAUSEB_EXCCODE));
  1835. /* Set PC to the exception entry point */
  1836. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1837. } else {
  1838. kvm_err("Trying to deliver BP when EXL is already set\n");
  1839. er = EMULATE_FAIL;
  1840. }
  1841. return er;
  1842. }
  1843. enum emulation_result kvm_mips_emulate_trap_exc(unsigned long cause,
  1844. uint32_t *opc,
  1845. struct kvm_run *run,
  1846. struct kvm_vcpu *vcpu)
  1847. {
  1848. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1849. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1850. enum emulation_result er = EMULATE_DONE;
  1851. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1852. /* save old pc */
  1853. kvm_write_c0_guest_epc(cop0, arch->pc);
  1854. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1855. if (cause & CAUSEF_BD)
  1856. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1857. else
  1858. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1859. kvm_debug("Delivering TRAP @ pc %#lx\n", arch->pc);
  1860. kvm_change_c0_guest_cause(cop0, (0xff),
  1861. (EXCCODE_TR << CAUSEB_EXCCODE));
  1862. /* Set PC to the exception entry point */
  1863. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1864. } else {
  1865. kvm_err("Trying to deliver TRAP when EXL is already set\n");
  1866. er = EMULATE_FAIL;
  1867. }
  1868. return er;
  1869. }
  1870. enum emulation_result kvm_mips_emulate_msafpe_exc(unsigned long cause,
  1871. uint32_t *opc,
  1872. struct kvm_run *run,
  1873. struct kvm_vcpu *vcpu)
  1874. {
  1875. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1876. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1877. enum emulation_result er = EMULATE_DONE;
  1878. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1879. /* save old pc */
  1880. kvm_write_c0_guest_epc(cop0, arch->pc);
  1881. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1882. if (cause & CAUSEF_BD)
  1883. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1884. else
  1885. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1886. kvm_debug("Delivering MSAFPE @ pc %#lx\n", arch->pc);
  1887. kvm_change_c0_guest_cause(cop0, (0xff),
  1888. (EXCCODE_MSAFPE << CAUSEB_EXCCODE));
  1889. /* Set PC to the exception entry point */
  1890. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1891. } else {
  1892. kvm_err("Trying to deliver MSAFPE when EXL is already set\n");
  1893. er = EMULATE_FAIL;
  1894. }
  1895. return er;
  1896. }
  1897. enum emulation_result kvm_mips_emulate_fpe_exc(unsigned long cause,
  1898. uint32_t *opc,
  1899. struct kvm_run *run,
  1900. struct kvm_vcpu *vcpu)
  1901. {
  1902. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1903. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1904. enum emulation_result er = EMULATE_DONE;
  1905. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1906. /* save old pc */
  1907. kvm_write_c0_guest_epc(cop0, arch->pc);
  1908. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1909. if (cause & CAUSEF_BD)
  1910. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1911. else
  1912. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1913. kvm_debug("Delivering FPE @ pc %#lx\n", arch->pc);
  1914. kvm_change_c0_guest_cause(cop0, (0xff),
  1915. (EXCCODE_FPE << CAUSEB_EXCCODE));
  1916. /* Set PC to the exception entry point */
  1917. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1918. } else {
  1919. kvm_err("Trying to deliver FPE when EXL is already set\n");
  1920. er = EMULATE_FAIL;
  1921. }
  1922. return er;
  1923. }
  1924. enum emulation_result kvm_mips_emulate_msadis_exc(unsigned long cause,
  1925. uint32_t *opc,
  1926. struct kvm_run *run,
  1927. struct kvm_vcpu *vcpu)
  1928. {
  1929. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1930. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1931. enum emulation_result er = EMULATE_DONE;
  1932. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  1933. /* save old pc */
  1934. kvm_write_c0_guest_epc(cop0, arch->pc);
  1935. kvm_set_c0_guest_status(cop0, ST0_EXL);
  1936. if (cause & CAUSEF_BD)
  1937. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  1938. else
  1939. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  1940. kvm_debug("Delivering MSADIS @ pc %#lx\n", arch->pc);
  1941. kvm_change_c0_guest_cause(cop0, (0xff),
  1942. (EXCCODE_MSADIS << CAUSEB_EXCCODE));
  1943. /* Set PC to the exception entry point */
  1944. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  1945. } else {
  1946. kvm_err("Trying to deliver MSADIS when EXL is already set\n");
  1947. er = EMULATE_FAIL;
  1948. }
  1949. return er;
  1950. }
  1951. /* ll/sc, rdhwr, sync emulation */
  1952. #define OPCODE 0xfc000000
  1953. #define BASE 0x03e00000
  1954. #define RT 0x001f0000
  1955. #define OFFSET 0x0000ffff
  1956. #define LL 0xc0000000
  1957. #define SC 0xe0000000
  1958. #define SPEC0 0x00000000
  1959. #define SPEC3 0x7c000000
  1960. #define RD 0x0000f800
  1961. #define FUNC 0x0000003f
  1962. #define SYNC 0x0000000f
  1963. #define RDHWR 0x0000003b
  1964. enum emulation_result kvm_mips_handle_ri(unsigned long cause, uint32_t *opc,
  1965. struct kvm_run *run,
  1966. struct kvm_vcpu *vcpu)
  1967. {
  1968. struct mips_coproc *cop0 = vcpu->arch.cop0;
  1969. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1970. enum emulation_result er = EMULATE_DONE;
  1971. unsigned long curr_pc;
  1972. uint32_t inst;
  1973. /*
  1974. * Update PC and hold onto current PC in case there is
  1975. * an error and we want to rollback the PC
  1976. */
  1977. curr_pc = vcpu->arch.pc;
  1978. er = update_pc(vcpu, cause);
  1979. if (er == EMULATE_FAIL)
  1980. return er;
  1981. /* Fetch the instruction. */
  1982. if (cause & CAUSEF_BD)
  1983. opc += 1;
  1984. inst = kvm_get_inst(opc, vcpu);
  1985. if (inst == KVM_INVALID_INST) {
  1986. kvm_err("%s: Cannot get inst @ %p\n", __func__, opc);
  1987. return EMULATE_FAIL;
  1988. }
  1989. if ((inst & OPCODE) == SPEC3 && (inst & FUNC) == RDHWR) {
  1990. int usermode = !KVM_GUEST_KERNEL_MODE(vcpu);
  1991. int rd = (inst & RD) >> 11;
  1992. int rt = (inst & RT) >> 16;
  1993. /* If usermode, check RDHWR rd is allowed by guest HWREna */
  1994. if (usermode && !(kvm_read_c0_guest_hwrena(cop0) & BIT(rd))) {
  1995. kvm_debug("RDHWR %#x disallowed by HWREna @ %p\n",
  1996. rd, opc);
  1997. goto emulate_ri;
  1998. }
  1999. switch (rd) {
  2000. case 0: /* CPU number */
  2001. arch->gprs[rt] = 0;
  2002. break;
  2003. case 1: /* SYNCI length */
  2004. arch->gprs[rt] = min(current_cpu_data.dcache.linesz,
  2005. current_cpu_data.icache.linesz);
  2006. break;
  2007. case 2: /* Read count register */
  2008. arch->gprs[rt] = kvm_mips_read_count(vcpu);
  2009. break;
  2010. case 3: /* Count register resolution */
  2011. switch (current_cpu_data.cputype) {
  2012. case CPU_20KC:
  2013. case CPU_25KF:
  2014. arch->gprs[rt] = 1;
  2015. break;
  2016. default:
  2017. arch->gprs[rt] = 2;
  2018. }
  2019. break;
  2020. case 29:
  2021. arch->gprs[rt] = kvm_read_c0_guest_userlocal(cop0);
  2022. break;
  2023. default:
  2024. kvm_debug("RDHWR %#x not supported @ %p\n", rd, opc);
  2025. goto emulate_ri;
  2026. }
  2027. } else {
  2028. kvm_debug("Emulate RI not supported @ %p: %#x\n", opc, inst);
  2029. goto emulate_ri;
  2030. }
  2031. return EMULATE_DONE;
  2032. emulate_ri:
  2033. /*
  2034. * Rollback PC (if in branch delay slot then the PC already points to
  2035. * branch target), and pass the RI exception to the guest OS.
  2036. */
  2037. vcpu->arch.pc = curr_pc;
  2038. return kvm_mips_emulate_ri_exc(cause, opc, run, vcpu);
  2039. }
  2040. enum emulation_result kvm_mips_complete_mmio_load(struct kvm_vcpu *vcpu,
  2041. struct kvm_run *run)
  2042. {
  2043. unsigned long *gpr = &vcpu->arch.gprs[vcpu->arch.io_gpr];
  2044. enum emulation_result er = EMULATE_DONE;
  2045. if (run->mmio.len > sizeof(*gpr)) {
  2046. kvm_err("Bad MMIO length: %d", run->mmio.len);
  2047. er = EMULATE_FAIL;
  2048. goto done;
  2049. }
  2050. er = update_pc(vcpu, vcpu->arch.pending_load_cause);
  2051. if (er == EMULATE_FAIL)
  2052. return er;
  2053. switch (run->mmio.len) {
  2054. case 4:
  2055. *gpr = *(int32_t *) run->mmio.data;
  2056. break;
  2057. case 2:
  2058. if (vcpu->mmio_needed == 2)
  2059. *gpr = *(int16_t *) run->mmio.data;
  2060. else
  2061. *gpr = *(uint16_t *)run->mmio.data;
  2062. break;
  2063. case 1:
  2064. if (vcpu->mmio_needed == 2)
  2065. *gpr = *(int8_t *) run->mmio.data;
  2066. else
  2067. *gpr = *(u8 *) run->mmio.data;
  2068. break;
  2069. }
  2070. if (vcpu->arch.pending_load_cause & CAUSEF_BD)
  2071. kvm_debug("[%#lx] Completing %d byte BD Load to gpr %d (0x%08lx) type %d\n",
  2072. vcpu->arch.pc, run->mmio.len, vcpu->arch.io_gpr, *gpr,
  2073. vcpu->mmio_needed);
  2074. done:
  2075. return er;
  2076. }
  2077. static enum emulation_result kvm_mips_emulate_exc(unsigned long cause,
  2078. uint32_t *opc,
  2079. struct kvm_run *run,
  2080. struct kvm_vcpu *vcpu)
  2081. {
  2082. uint32_t exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
  2083. struct mips_coproc *cop0 = vcpu->arch.cop0;
  2084. struct kvm_vcpu_arch *arch = &vcpu->arch;
  2085. enum emulation_result er = EMULATE_DONE;
  2086. if ((kvm_read_c0_guest_status(cop0) & ST0_EXL) == 0) {
  2087. /* save old pc */
  2088. kvm_write_c0_guest_epc(cop0, arch->pc);
  2089. kvm_set_c0_guest_status(cop0, ST0_EXL);
  2090. if (cause & CAUSEF_BD)
  2091. kvm_set_c0_guest_cause(cop0, CAUSEF_BD);
  2092. else
  2093. kvm_clear_c0_guest_cause(cop0, CAUSEF_BD);
  2094. kvm_change_c0_guest_cause(cop0, (0xff),
  2095. (exccode << CAUSEB_EXCCODE));
  2096. /* Set PC to the exception entry point */
  2097. arch->pc = KVM_GUEST_KSEG0 + 0x180;
  2098. kvm_write_c0_guest_badvaddr(cop0, vcpu->arch.host_cp0_badvaddr);
  2099. kvm_debug("Delivering EXC %d @ pc %#lx, badVaddr: %#lx\n",
  2100. exccode, kvm_read_c0_guest_epc(cop0),
  2101. kvm_read_c0_guest_badvaddr(cop0));
  2102. } else {
  2103. kvm_err("Trying to deliver EXC when EXL is already set\n");
  2104. er = EMULATE_FAIL;
  2105. }
  2106. return er;
  2107. }
  2108. enum emulation_result kvm_mips_check_privilege(unsigned long cause,
  2109. uint32_t *opc,
  2110. struct kvm_run *run,
  2111. struct kvm_vcpu *vcpu)
  2112. {
  2113. enum emulation_result er = EMULATE_DONE;
  2114. uint32_t exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
  2115. unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
  2116. int usermode = !KVM_GUEST_KERNEL_MODE(vcpu);
  2117. if (usermode) {
  2118. switch (exccode) {
  2119. case EXCCODE_INT:
  2120. case EXCCODE_SYS:
  2121. case EXCCODE_BP:
  2122. case EXCCODE_RI:
  2123. case EXCCODE_TR:
  2124. case EXCCODE_MSAFPE:
  2125. case EXCCODE_FPE:
  2126. case EXCCODE_MSADIS:
  2127. break;
  2128. case EXCCODE_CPU:
  2129. if (((cause & CAUSEF_CE) >> CAUSEB_CE) == 0)
  2130. er = EMULATE_PRIV_FAIL;
  2131. break;
  2132. case EXCCODE_MOD:
  2133. break;
  2134. case EXCCODE_TLBL:
  2135. /*
  2136. * We we are accessing Guest kernel space, then send an
  2137. * address error exception to the guest
  2138. */
  2139. if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
  2140. kvm_debug("%s: LD MISS @ %#lx\n", __func__,
  2141. badvaddr);
  2142. cause &= ~0xff;
  2143. cause |= (EXCCODE_ADEL << CAUSEB_EXCCODE);
  2144. er = EMULATE_PRIV_FAIL;
  2145. }
  2146. break;
  2147. case EXCCODE_TLBS:
  2148. /*
  2149. * We we are accessing Guest kernel space, then send an
  2150. * address error exception to the guest
  2151. */
  2152. if (badvaddr >= (unsigned long) KVM_GUEST_KSEG0) {
  2153. kvm_debug("%s: ST MISS @ %#lx\n", __func__,
  2154. badvaddr);
  2155. cause &= ~0xff;
  2156. cause |= (EXCCODE_ADES << CAUSEB_EXCCODE);
  2157. er = EMULATE_PRIV_FAIL;
  2158. }
  2159. break;
  2160. case EXCCODE_ADES:
  2161. kvm_debug("%s: address error ST @ %#lx\n", __func__,
  2162. badvaddr);
  2163. if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
  2164. cause &= ~0xff;
  2165. cause |= (EXCCODE_TLBS << CAUSEB_EXCCODE);
  2166. }
  2167. er = EMULATE_PRIV_FAIL;
  2168. break;
  2169. case EXCCODE_ADEL:
  2170. kvm_debug("%s: address error LD @ %#lx\n", __func__,
  2171. badvaddr);
  2172. if ((badvaddr & PAGE_MASK) == KVM_GUEST_COMMPAGE_ADDR) {
  2173. cause &= ~0xff;
  2174. cause |= (EXCCODE_TLBL << CAUSEB_EXCCODE);
  2175. }
  2176. er = EMULATE_PRIV_FAIL;
  2177. break;
  2178. default:
  2179. er = EMULATE_PRIV_FAIL;
  2180. break;
  2181. }
  2182. }
  2183. if (er == EMULATE_PRIV_FAIL)
  2184. kvm_mips_emulate_exc(cause, opc, run, vcpu);
  2185. return er;
  2186. }
  2187. /*
  2188. * User Address (UA) fault, this could happen if
  2189. * (1) TLB entry not present/valid in both Guest and shadow host TLBs, in this
  2190. * case we pass on the fault to the guest kernel and let it handle it.
  2191. * (2) TLB entry is present in the Guest TLB but not in the shadow, in this
  2192. * case we inject the TLB from the Guest TLB into the shadow host TLB
  2193. */
  2194. enum emulation_result kvm_mips_handle_tlbmiss(unsigned long cause,
  2195. uint32_t *opc,
  2196. struct kvm_run *run,
  2197. struct kvm_vcpu *vcpu)
  2198. {
  2199. enum emulation_result er = EMULATE_DONE;
  2200. uint32_t exccode = (cause >> CAUSEB_EXCCODE) & 0x1f;
  2201. unsigned long va = vcpu->arch.host_cp0_badvaddr;
  2202. int index;
  2203. kvm_debug("kvm_mips_handle_tlbmiss: badvaddr: %#lx, entryhi: %#lx\n",
  2204. vcpu->arch.host_cp0_badvaddr, vcpu->arch.host_cp0_entryhi);
  2205. /*
  2206. * KVM would not have got the exception if this entry was valid in the
  2207. * shadow host TLB. Check the Guest TLB, if the entry is not there then
  2208. * send the guest an exception. The guest exc handler should then inject
  2209. * an entry into the guest TLB.
  2210. */
  2211. index = kvm_mips_guest_tlb_lookup(vcpu,
  2212. (va & VPN2_MASK) |
  2213. (kvm_read_c0_guest_entryhi(vcpu->arch.cop0) &
  2214. KVM_ENTRYHI_ASID));
  2215. if (index < 0) {
  2216. if (exccode == EXCCODE_TLBL) {
  2217. er = kvm_mips_emulate_tlbmiss_ld(cause, opc, run, vcpu);
  2218. } else if (exccode == EXCCODE_TLBS) {
  2219. er = kvm_mips_emulate_tlbmiss_st(cause, opc, run, vcpu);
  2220. } else {
  2221. kvm_err("%s: invalid exc code: %d\n", __func__,
  2222. exccode);
  2223. er = EMULATE_FAIL;
  2224. }
  2225. } else {
  2226. struct kvm_mips_tlb *tlb = &vcpu->arch.guest_tlb[index];
  2227. /*
  2228. * Check if the entry is valid, if not then setup a TLB invalid
  2229. * exception to the guest
  2230. */
  2231. if (!TLB_IS_VALID(*tlb, va)) {
  2232. if (exccode == EXCCODE_TLBL) {
  2233. er = kvm_mips_emulate_tlbinv_ld(cause, opc, run,
  2234. vcpu);
  2235. } else if (exccode == EXCCODE_TLBS) {
  2236. er = kvm_mips_emulate_tlbinv_st(cause, opc, run,
  2237. vcpu);
  2238. } else {
  2239. kvm_err("%s: invalid exc code: %d\n", __func__,
  2240. exccode);
  2241. er = EMULATE_FAIL;
  2242. }
  2243. } else {
  2244. kvm_debug("Injecting hi: %#lx, lo0: %#lx, lo1: %#lx into shadow host TLB\n",
  2245. tlb->tlb_hi, tlb->tlb_lo0, tlb->tlb_lo1);
  2246. /*
  2247. * OK we have a Guest TLB entry, now inject it into the
  2248. * shadow host TLB
  2249. */
  2250. kvm_mips_handle_mapped_seg_tlb_fault(vcpu, tlb, NULL,
  2251. NULL);
  2252. }
  2253. }
  2254. return er;
  2255. }