emulate.c 71 KB

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