vz.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324
  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: Support for hardware virtualization extensions
  7. *
  8. * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
  9. * Authors: Yann Le Du <[email protected]>
  10. */
  11. #include <linux/errno.h>
  12. #include <linux/err.h>
  13. #include <linux/module.h>
  14. #include <linux/preempt.h>
  15. #include <linux/vmalloc.h>
  16. #include <asm/cacheflush.h>
  17. #include <asm/cacheops.h>
  18. #include <asm/cmpxchg.h>
  19. #include <asm/fpu.h>
  20. #include <asm/hazards.h>
  21. #include <asm/inst.h>
  22. #include <asm/mmu_context.h>
  23. #include <asm/r4kcache.h>
  24. #include <asm/time.h>
  25. #include <asm/tlb.h>
  26. #include <asm/tlbex.h>
  27. #include <linux/kvm_host.h>
  28. #include "interrupt.h"
  29. #ifdef CONFIG_CPU_LOONGSON64
  30. #include "loongson_regs.h"
  31. #endif
  32. #include "trace.h"
  33. /* Pointers to last VCPU loaded on each physical CPU */
  34. static struct kvm_vcpu *last_vcpu[NR_CPUS];
  35. /* Pointers to last VCPU executed on each physical CPU */
  36. static struct kvm_vcpu *last_exec_vcpu[NR_CPUS];
  37. /*
  38. * Number of guest VTLB entries to use, so we can catch inconsistency between
  39. * CPUs.
  40. */
  41. static unsigned int kvm_vz_guest_vtlb_size;
  42. static inline long kvm_vz_read_gc0_ebase(void)
  43. {
  44. if (sizeof(long) == 8 && cpu_has_ebase_wg)
  45. return read_gc0_ebase_64();
  46. else
  47. return read_gc0_ebase();
  48. }
  49. static inline void kvm_vz_write_gc0_ebase(long v)
  50. {
  51. /*
  52. * First write with WG=1 to write upper bits, then write again in case
  53. * WG should be left at 0.
  54. * write_gc0_ebase_64() is no longer UNDEFINED since R6.
  55. */
  56. if (sizeof(long) == 8 &&
  57. (cpu_has_mips64r6 || cpu_has_ebase_wg)) {
  58. write_gc0_ebase_64(v | MIPS_EBASE_WG);
  59. write_gc0_ebase_64(v);
  60. } else {
  61. write_gc0_ebase(v | MIPS_EBASE_WG);
  62. write_gc0_ebase(v);
  63. }
  64. }
  65. /*
  66. * These Config bits may be writable by the guest:
  67. * Config: [K23, KU] (!TLB), K0
  68. * Config1: (none)
  69. * Config2: [TU, SU] (impl)
  70. * Config3: ISAOnExc
  71. * Config4: FTLBPageSize
  72. * Config5: K, CV, MSAEn, UFE, FRE, SBRI, UFR
  73. */
  74. static inline unsigned int kvm_vz_config_guest_wrmask(struct kvm_vcpu *vcpu)
  75. {
  76. return CONF_CM_CMASK;
  77. }
  78. static inline unsigned int kvm_vz_config1_guest_wrmask(struct kvm_vcpu *vcpu)
  79. {
  80. return 0;
  81. }
  82. static inline unsigned int kvm_vz_config2_guest_wrmask(struct kvm_vcpu *vcpu)
  83. {
  84. return 0;
  85. }
  86. static inline unsigned int kvm_vz_config3_guest_wrmask(struct kvm_vcpu *vcpu)
  87. {
  88. return MIPS_CONF3_ISA_OE;
  89. }
  90. static inline unsigned int kvm_vz_config4_guest_wrmask(struct kvm_vcpu *vcpu)
  91. {
  92. /* no need to be exact */
  93. return MIPS_CONF4_VFTLBPAGESIZE;
  94. }
  95. static inline unsigned int kvm_vz_config5_guest_wrmask(struct kvm_vcpu *vcpu)
  96. {
  97. unsigned int mask = MIPS_CONF5_K | MIPS_CONF5_CV | MIPS_CONF5_SBRI;
  98. /* Permit MSAEn changes if MSA supported and enabled */
  99. if (kvm_mips_guest_has_msa(&vcpu->arch))
  100. mask |= MIPS_CONF5_MSAEN;
  101. /*
  102. * Permit guest FPU mode changes if FPU is enabled and the relevant
  103. * feature exists according to FIR register.
  104. */
  105. if (kvm_mips_guest_has_fpu(&vcpu->arch)) {
  106. if (cpu_has_ufr)
  107. mask |= MIPS_CONF5_UFR;
  108. if (cpu_has_fre)
  109. mask |= MIPS_CONF5_FRE | MIPS_CONF5_UFE;
  110. }
  111. return mask;
  112. }
  113. static inline unsigned int kvm_vz_config6_guest_wrmask(struct kvm_vcpu *vcpu)
  114. {
  115. return LOONGSON_CONF6_INTIMER | LOONGSON_CONF6_EXTIMER;
  116. }
  117. /*
  118. * VZ optionally allows these additional Config bits to be written by root:
  119. * Config: M, [MT]
  120. * Config1: M, [MMUSize-1, C2, MD, PC, WR, CA], FP
  121. * Config2: M
  122. * Config3: M, MSAP, [BPG], ULRI, [DSP2P, DSPP], CTXTC, [ITL, LPA, VEIC,
  123. * VInt, SP, CDMM, MT, SM, TL]
  124. * Config4: M, [VTLBSizeExt, MMUSizeExt]
  125. * Config5: MRP
  126. */
  127. static inline unsigned int kvm_vz_config_user_wrmask(struct kvm_vcpu *vcpu)
  128. {
  129. return kvm_vz_config_guest_wrmask(vcpu) | MIPS_CONF_M;
  130. }
  131. static inline unsigned int kvm_vz_config1_user_wrmask(struct kvm_vcpu *vcpu)
  132. {
  133. unsigned int mask = kvm_vz_config1_guest_wrmask(vcpu) | MIPS_CONF_M;
  134. /* Permit FPU to be present if FPU is supported */
  135. if (kvm_mips_guest_can_have_fpu(&vcpu->arch))
  136. mask |= MIPS_CONF1_FP;
  137. return mask;
  138. }
  139. static inline unsigned int kvm_vz_config2_user_wrmask(struct kvm_vcpu *vcpu)
  140. {
  141. return kvm_vz_config2_guest_wrmask(vcpu) | MIPS_CONF_M;
  142. }
  143. static inline unsigned int kvm_vz_config3_user_wrmask(struct kvm_vcpu *vcpu)
  144. {
  145. unsigned int mask = kvm_vz_config3_guest_wrmask(vcpu) | MIPS_CONF_M |
  146. MIPS_CONF3_ULRI | MIPS_CONF3_CTXTC;
  147. /* Permit MSA to be present if MSA is supported */
  148. if (kvm_mips_guest_can_have_msa(&vcpu->arch))
  149. mask |= MIPS_CONF3_MSA;
  150. return mask;
  151. }
  152. static inline unsigned int kvm_vz_config4_user_wrmask(struct kvm_vcpu *vcpu)
  153. {
  154. return kvm_vz_config4_guest_wrmask(vcpu) | MIPS_CONF_M;
  155. }
  156. static inline unsigned int kvm_vz_config5_user_wrmask(struct kvm_vcpu *vcpu)
  157. {
  158. return kvm_vz_config5_guest_wrmask(vcpu) | MIPS_CONF5_MRP;
  159. }
  160. static inline unsigned int kvm_vz_config6_user_wrmask(struct kvm_vcpu *vcpu)
  161. {
  162. return kvm_vz_config6_guest_wrmask(vcpu) |
  163. LOONGSON_CONF6_SFBEN | LOONGSON_CONF6_FTLBDIS;
  164. }
  165. static gpa_t kvm_vz_gva_to_gpa_cb(gva_t gva)
  166. {
  167. /* VZ guest has already converted gva to gpa */
  168. return gva;
  169. }
  170. static void kvm_vz_queue_irq(struct kvm_vcpu *vcpu, unsigned int priority)
  171. {
  172. set_bit(priority, &vcpu->arch.pending_exceptions);
  173. clear_bit(priority, &vcpu->arch.pending_exceptions_clr);
  174. }
  175. static void kvm_vz_dequeue_irq(struct kvm_vcpu *vcpu, unsigned int priority)
  176. {
  177. clear_bit(priority, &vcpu->arch.pending_exceptions);
  178. set_bit(priority, &vcpu->arch.pending_exceptions_clr);
  179. }
  180. static void kvm_vz_queue_timer_int_cb(struct kvm_vcpu *vcpu)
  181. {
  182. /*
  183. * timer expiry is asynchronous to vcpu execution therefore defer guest
  184. * cp0 accesses
  185. */
  186. kvm_vz_queue_irq(vcpu, MIPS_EXC_INT_TIMER);
  187. }
  188. static void kvm_vz_dequeue_timer_int_cb(struct kvm_vcpu *vcpu)
  189. {
  190. /*
  191. * timer expiry is asynchronous to vcpu execution therefore defer guest
  192. * cp0 accesses
  193. */
  194. kvm_vz_dequeue_irq(vcpu, MIPS_EXC_INT_TIMER);
  195. }
  196. static void kvm_vz_queue_io_int_cb(struct kvm_vcpu *vcpu,
  197. struct kvm_mips_interrupt *irq)
  198. {
  199. int intr = (int)irq->irq;
  200. /*
  201. * interrupts are asynchronous to vcpu execution therefore defer guest
  202. * cp0 accesses
  203. */
  204. kvm_vz_queue_irq(vcpu, kvm_irq_to_priority(intr));
  205. }
  206. static void kvm_vz_dequeue_io_int_cb(struct kvm_vcpu *vcpu,
  207. struct kvm_mips_interrupt *irq)
  208. {
  209. int intr = (int)irq->irq;
  210. /*
  211. * interrupts are asynchronous to vcpu execution therefore defer guest
  212. * cp0 accesses
  213. */
  214. kvm_vz_dequeue_irq(vcpu, kvm_irq_to_priority(-intr));
  215. }
  216. static int kvm_vz_irq_deliver_cb(struct kvm_vcpu *vcpu, unsigned int priority,
  217. u32 cause)
  218. {
  219. u32 irq = (priority < MIPS_EXC_MAX) ?
  220. kvm_priority_to_irq[priority] : 0;
  221. switch (priority) {
  222. case MIPS_EXC_INT_TIMER:
  223. set_gc0_cause(C_TI);
  224. break;
  225. case MIPS_EXC_INT_IO_1:
  226. case MIPS_EXC_INT_IO_2:
  227. case MIPS_EXC_INT_IPI_1:
  228. case MIPS_EXC_INT_IPI_2:
  229. if (cpu_has_guestctl2)
  230. set_c0_guestctl2(irq);
  231. else
  232. set_gc0_cause(irq);
  233. break;
  234. default:
  235. break;
  236. }
  237. clear_bit(priority, &vcpu->arch.pending_exceptions);
  238. return 1;
  239. }
  240. static int kvm_vz_irq_clear_cb(struct kvm_vcpu *vcpu, unsigned int priority,
  241. u32 cause)
  242. {
  243. u32 irq = (priority < MIPS_EXC_MAX) ?
  244. kvm_priority_to_irq[priority] : 0;
  245. switch (priority) {
  246. case MIPS_EXC_INT_TIMER:
  247. /*
  248. * Explicitly clear irq associated with Cause.IP[IPTI]
  249. * if GuestCtl2 virtual interrupt register not
  250. * supported or if not using GuestCtl2 Hardware Clear.
  251. */
  252. if (cpu_has_guestctl2) {
  253. if (!(read_c0_guestctl2() & (irq << 14)))
  254. clear_c0_guestctl2(irq);
  255. } else {
  256. clear_gc0_cause(irq);
  257. }
  258. break;
  259. case MIPS_EXC_INT_IO_1:
  260. case MIPS_EXC_INT_IO_2:
  261. case MIPS_EXC_INT_IPI_1:
  262. case MIPS_EXC_INT_IPI_2:
  263. /* Clear GuestCtl2.VIP irq if not using Hardware Clear */
  264. if (cpu_has_guestctl2) {
  265. if (!(read_c0_guestctl2() & (irq << 14)))
  266. clear_c0_guestctl2(irq);
  267. } else {
  268. clear_gc0_cause(irq);
  269. }
  270. break;
  271. default:
  272. break;
  273. }
  274. clear_bit(priority, &vcpu->arch.pending_exceptions_clr);
  275. return 1;
  276. }
  277. /*
  278. * VZ guest timer handling.
  279. */
  280. /**
  281. * kvm_vz_should_use_htimer() - Find whether to use the VZ hard guest timer.
  282. * @vcpu: Virtual CPU.
  283. *
  284. * Returns: true if the VZ GTOffset & real guest CP0_Count should be used
  285. * instead of software emulation of guest timer.
  286. * false otherwise.
  287. */
  288. static bool kvm_vz_should_use_htimer(struct kvm_vcpu *vcpu)
  289. {
  290. if (kvm_mips_count_disabled(vcpu))
  291. return false;
  292. /* Chosen frequency must match real frequency */
  293. if (mips_hpt_frequency != vcpu->arch.count_hz)
  294. return false;
  295. /* We don't support a CP0_GTOffset with fewer bits than CP0_Count */
  296. if (current_cpu_data.gtoffset_mask != 0xffffffff)
  297. return false;
  298. return true;
  299. }
  300. /**
  301. * _kvm_vz_restore_stimer() - Restore soft timer state.
  302. * @vcpu: Virtual CPU.
  303. * @compare: CP0_Compare register value, restored by caller.
  304. * @cause: CP0_Cause register to restore.
  305. *
  306. * Restore VZ state relating to the soft timer. The hard timer can be enabled
  307. * later.
  308. */
  309. static void _kvm_vz_restore_stimer(struct kvm_vcpu *vcpu, u32 compare,
  310. u32 cause)
  311. {
  312. /*
  313. * Avoid spurious counter interrupts by setting Guest CP0_Count to just
  314. * after Guest CP0_Compare.
  315. */
  316. write_c0_gtoffset(compare - read_c0_count());
  317. back_to_back_c0_hazard();
  318. write_gc0_cause(cause);
  319. }
  320. /**
  321. * _kvm_vz_restore_htimer() - Restore hard timer state.
  322. * @vcpu: Virtual CPU.
  323. * @compare: CP0_Compare register value, restored by caller.
  324. * @cause: CP0_Cause register to restore.
  325. *
  326. * Restore hard timer Guest.Count & Guest.Cause taking care to preserve the
  327. * value of Guest.CP0_Cause.TI while restoring Guest.CP0_Cause.
  328. */
  329. static void _kvm_vz_restore_htimer(struct kvm_vcpu *vcpu,
  330. u32 compare, u32 cause)
  331. {
  332. u32 start_count, after_count;
  333. unsigned long flags;
  334. /*
  335. * Freeze the soft-timer and sync the guest CP0_Count with it. We do
  336. * this with interrupts disabled to avoid latency.
  337. */
  338. local_irq_save(flags);
  339. kvm_mips_freeze_hrtimer(vcpu, &start_count);
  340. write_c0_gtoffset(start_count - read_c0_count());
  341. local_irq_restore(flags);
  342. /* restore guest CP0_Cause, as TI may already be set */
  343. back_to_back_c0_hazard();
  344. write_gc0_cause(cause);
  345. /*
  346. * The above sequence isn't atomic and would result in lost timer
  347. * interrupts if we're not careful. Detect if a timer interrupt is due
  348. * and assert it.
  349. */
  350. back_to_back_c0_hazard();
  351. after_count = read_gc0_count();
  352. if (after_count - start_count > compare - start_count - 1)
  353. kvm_vz_queue_irq(vcpu, MIPS_EXC_INT_TIMER);
  354. }
  355. /**
  356. * kvm_vz_restore_timer() - Restore timer state.
  357. * @vcpu: Virtual CPU.
  358. *
  359. * Restore soft timer state from saved context.
  360. */
  361. static void kvm_vz_restore_timer(struct kvm_vcpu *vcpu)
  362. {
  363. struct mips_coproc *cop0 = &vcpu->arch.cop0;
  364. u32 cause, compare;
  365. compare = kvm_read_sw_gc0_compare(cop0);
  366. cause = kvm_read_sw_gc0_cause(cop0);
  367. write_gc0_compare(compare);
  368. _kvm_vz_restore_stimer(vcpu, compare, cause);
  369. }
  370. /**
  371. * kvm_vz_acquire_htimer() - Switch to hard timer state.
  372. * @vcpu: Virtual CPU.
  373. *
  374. * Restore hard timer state on top of existing soft timer state if possible.
  375. *
  376. * Since hard timer won't remain active over preemption, preemption should be
  377. * disabled by the caller.
  378. */
  379. void kvm_vz_acquire_htimer(struct kvm_vcpu *vcpu)
  380. {
  381. u32 gctl0;
  382. gctl0 = read_c0_guestctl0();
  383. if (!(gctl0 & MIPS_GCTL0_GT) && kvm_vz_should_use_htimer(vcpu)) {
  384. /* enable guest access to hard timer */
  385. write_c0_guestctl0(gctl0 | MIPS_GCTL0_GT);
  386. _kvm_vz_restore_htimer(vcpu, read_gc0_compare(),
  387. read_gc0_cause());
  388. }
  389. }
  390. /**
  391. * _kvm_vz_save_htimer() - Switch to software emulation of guest timer.
  392. * @vcpu: Virtual CPU.
  393. * @out_compare: Pointer to write compare value to.
  394. * @out_cause: Pointer to write cause value to.
  395. *
  396. * Save VZ guest timer state and switch to software emulation of guest CP0
  397. * timer. The hard timer must already be in use, so preemption should be
  398. * disabled.
  399. */
  400. static void _kvm_vz_save_htimer(struct kvm_vcpu *vcpu,
  401. u32 *out_compare, u32 *out_cause)
  402. {
  403. u32 cause, compare, before_count, end_count;
  404. ktime_t before_time;
  405. compare = read_gc0_compare();
  406. *out_compare = compare;
  407. before_time = ktime_get();
  408. /*
  409. * Record the CP0_Count *prior* to saving CP0_Cause, so we have a time
  410. * at which no pending timer interrupt is missing.
  411. */
  412. before_count = read_gc0_count();
  413. back_to_back_c0_hazard();
  414. cause = read_gc0_cause();
  415. *out_cause = cause;
  416. /*
  417. * Record a final CP0_Count which we will transfer to the soft-timer.
  418. * This is recorded *after* saving CP0_Cause, so we don't get any timer
  419. * interrupts from just after the final CP0_Count point.
  420. */
  421. back_to_back_c0_hazard();
  422. end_count = read_gc0_count();
  423. /*
  424. * The above sequence isn't atomic, so we could miss a timer interrupt
  425. * between reading CP0_Cause and end_count. Detect and record any timer
  426. * interrupt due between before_count and end_count.
  427. */
  428. if (end_count - before_count > compare - before_count - 1)
  429. kvm_vz_queue_irq(vcpu, MIPS_EXC_INT_TIMER);
  430. /*
  431. * Restore soft-timer, ignoring a small amount of negative drift due to
  432. * delay between freeze_hrtimer and setting CP0_GTOffset.
  433. */
  434. kvm_mips_restore_hrtimer(vcpu, before_time, end_count, -0x10000);
  435. }
  436. /**
  437. * kvm_vz_save_timer() - Save guest timer state.
  438. * @vcpu: Virtual CPU.
  439. *
  440. * Save VZ guest timer state and switch to soft guest timer if hard timer was in
  441. * use.
  442. */
  443. static void kvm_vz_save_timer(struct kvm_vcpu *vcpu)
  444. {
  445. struct mips_coproc *cop0 = &vcpu->arch.cop0;
  446. u32 gctl0, compare, cause;
  447. gctl0 = read_c0_guestctl0();
  448. if (gctl0 & MIPS_GCTL0_GT) {
  449. /* disable guest use of hard timer */
  450. write_c0_guestctl0(gctl0 & ~MIPS_GCTL0_GT);
  451. /* save hard timer state */
  452. _kvm_vz_save_htimer(vcpu, &compare, &cause);
  453. } else {
  454. compare = read_gc0_compare();
  455. cause = read_gc0_cause();
  456. }
  457. /* save timer-related state to VCPU context */
  458. kvm_write_sw_gc0_cause(cop0, cause);
  459. kvm_write_sw_gc0_compare(cop0, compare);
  460. }
  461. /**
  462. * kvm_vz_lose_htimer() - Ensure hard guest timer is not in use.
  463. * @vcpu: Virtual CPU.
  464. *
  465. * Transfers the state of the hard guest timer to the soft guest timer, leaving
  466. * guest state intact so it can continue to be used with the soft timer.
  467. */
  468. void kvm_vz_lose_htimer(struct kvm_vcpu *vcpu)
  469. {
  470. u32 gctl0, compare, cause;
  471. preempt_disable();
  472. gctl0 = read_c0_guestctl0();
  473. if (gctl0 & MIPS_GCTL0_GT) {
  474. /* disable guest use of timer */
  475. write_c0_guestctl0(gctl0 & ~MIPS_GCTL0_GT);
  476. /* switch to soft timer */
  477. _kvm_vz_save_htimer(vcpu, &compare, &cause);
  478. /* leave soft timer in usable state */
  479. _kvm_vz_restore_stimer(vcpu, compare, cause);
  480. }
  481. preempt_enable();
  482. }
  483. /**
  484. * is_eva_access() - Find whether an instruction is an EVA memory accessor.
  485. * @inst: 32-bit instruction encoding.
  486. *
  487. * Finds whether @inst encodes an EVA memory access instruction, which would
  488. * indicate that emulation of it should access the user mode address space
  489. * instead of the kernel mode address space. This matters for MUSUK segments
  490. * which are TLB mapped for user mode but unmapped for kernel mode.
  491. *
  492. * Returns: Whether @inst encodes an EVA accessor instruction.
  493. */
  494. static bool is_eva_access(union mips_instruction inst)
  495. {
  496. if (inst.spec3_format.opcode != spec3_op)
  497. return false;
  498. switch (inst.spec3_format.func) {
  499. case lwle_op:
  500. case lwre_op:
  501. case cachee_op:
  502. case sbe_op:
  503. case she_op:
  504. case sce_op:
  505. case swe_op:
  506. case swle_op:
  507. case swre_op:
  508. case prefe_op:
  509. case lbue_op:
  510. case lhue_op:
  511. case lbe_op:
  512. case lhe_op:
  513. case lle_op:
  514. case lwe_op:
  515. return true;
  516. default:
  517. return false;
  518. }
  519. }
  520. /**
  521. * is_eva_am_mapped() - Find whether an access mode is mapped.
  522. * @vcpu: KVM VCPU state.
  523. * @am: 3-bit encoded access mode.
  524. * @eu: Segment becomes unmapped and uncached when Status.ERL=1.
  525. *
  526. * Decode @am to find whether it encodes a mapped segment for the current VCPU
  527. * state. Where necessary @eu and the actual instruction causing the fault are
  528. * taken into account to make the decision.
  529. *
  530. * Returns: Whether the VCPU faulted on a TLB mapped address.
  531. */
  532. static bool is_eva_am_mapped(struct kvm_vcpu *vcpu, unsigned int am, bool eu)
  533. {
  534. u32 am_lookup;
  535. int err;
  536. /*
  537. * Interpret access control mode. We assume address errors will already
  538. * have been caught by the guest, leaving us with:
  539. * AM UM SM KM 31..24 23..16
  540. * UK 0 000 Unm 0 0
  541. * MK 1 001 TLB 1
  542. * MSK 2 010 TLB TLB 1
  543. * MUSK 3 011 TLB TLB TLB 1
  544. * MUSUK 4 100 TLB TLB Unm 0 1
  545. * USK 5 101 Unm Unm 0 0
  546. * - 6 110 0 0
  547. * UUSK 7 111 Unm Unm Unm 0 0
  548. *
  549. * We shift a magic value by AM across the sign bit to find if always
  550. * TLB mapped, and if not shift by 8 again to find if it depends on KM.
  551. */
  552. am_lookup = 0x70080000 << am;
  553. if ((s32)am_lookup < 0) {
  554. /*
  555. * MK, MSK, MUSK
  556. * Always TLB mapped, unless SegCtl.EU && ERL
  557. */
  558. if (!eu || !(read_gc0_status() & ST0_ERL))
  559. return true;
  560. } else {
  561. am_lookup <<= 8;
  562. if ((s32)am_lookup < 0) {
  563. union mips_instruction inst;
  564. unsigned int status;
  565. u32 *opc;
  566. /*
  567. * MUSUK
  568. * TLB mapped if not in kernel mode
  569. */
  570. status = read_gc0_status();
  571. if (!(status & (ST0_EXL | ST0_ERL)) &&
  572. (status & ST0_KSU))
  573. return true;
  574. /*
  575. * EVA access instructions in kernel
  576. * mode access user address space.
  577. */
  578. opc = (u32 *)vcpu->arch.pc;
  579. if (vcpu->arch.host_cp0_cause & CAUSEF_BD)
  580. opc += 1;
  581. err = kvm_get_badinstr(opc, vcpu, &inst.word);
  582. if (!err && is_eva_access(inst))
  583. return true;
  584. }
  585. }
  586. return false;
  587. }
  588. /**
  589. * kvm_vz_gva_to_gpa() - Convert valid GVA to GPA.
  590. * @vcpu: KVM VCPU state.
  591. * @gva: Guest virtual address to convert.
  592. * @gpa: Output guest physical address.
  593. *
  594. * Convert a guest virtual address (GVA) which is valid according to the guest
  595. * context, to a guest physical address (GPA).
  596. *
  597. * Returns: 0 on success.
  598. * -errno on failure.
  599. */
  600. static int kvm_vz_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
  601. unsigned long *gpa)
  602. {
  603. u32 gva32 = gva;
  604. unsigned long segctl;
  605. if ((long)gva == (s32)gva32) {
  606. /* Handle canonical 32-bit virtual address */
  607. if (cpu_guest_has_segments) {
  608. unsigned long mask, pa;
  609. switch (gva32 >> 29) {
  610. case 0:
  611. case 1: /* CFG5 (1GB) */
  612. segctl = read_gc0_segctl2() >> 16;
  613. mask = (unsigned long)0xfc0000000ull;
  614. break;
  615. case 2:
  616. case 3: /* CFG4 (1GB) */
  617. segctl = read_gc0_segctl2();
  618. mask = (unsigned long)0xfc0000000ull;
  619. break;
  620. case 4: /* CFG3 (512MB) */
  621. segctl = read_gc0_segctl1() >> 16;
  622. mask = (unsigned long)0xfe0000000ull;
  623. break;
  624. case 5: /* CFG2 (512MB) */
  625. segctl = read_gc0_segctl1();
  626. mask = (unsigned long)0xfe0000000ull;
  627. break;
  628. case 6: /* CFG1 (512MB) */
  629. segctl = read_gc0_segctl0() >> 16;
  630. mask = (unsigned long)0xfe0000000ull;
  631. break;
  632. case 7: /* CFG0 (512MB) */
  633. segctl = read_gc0_segctl0();
  634. mask = (unsigned long)0xfe0000000ull;
  635. break;
  636. default:
  637. /*
  638. * GCC 4.9 isn't smart enough to figure out that
  639. * segctl and mask are always initialised.
  640. */
  641. unreachable();
  642. }
  643. if (is_eva_am_mapped(vcpu, (segctl >> 4) & 0x7,
  644. segctl & 0x0008))
  645. goto tlb_mapped;
  646. /* Unmapped, find guest physical address */
  647. pa = (segctl << 20) & mask;
  648. pa |= gva32 & ~mask;
  649. *gpa = pa;
  650. return 0;
  651. } else if ((s32)gva32 < (s32)0xc0000000) {
  652. /* legacy unmapped KSeg0 or KSeg1 */
  653. *gpa = gva32 & 0x1fffffff;
  654. return 0;
  655. }
  656. #ifdef CONFIG_64BIT
  657. } else if ((gva & 0xc000000000000000) == 0x8000000000000000) {
  658. /* XKPHYS */
  659. if (cpu_guest_has_segments) {
  660. /*
  661. * Each of the 8 regions can be overridden by SegCtl2.XR
  662. * to use SegCtl1.XAM.
  663. */
  664. segctl = read_gc0_segctl2();
  665. if (segctl & (1ull << (56 + ((gva >> 59) & 0x7)))) {
  666. segctl = read_gc0_segctl1();
  667. if (is_eva_am_mapped(vcpu, (segctl >> 59) & 0x7,
  668. 0))
  669. goto tlb_mapped;
  670. }
  671. }
  672. /*
  673. * Traditionally fully unmapped.
  674. * Bits 61:59 specify the CCA, which we can just mask off here.
  675. * Bits 58:PABITS should be zero, but we shouldn't have got here
  676. * if it wasn't.
  677. */
  678. *gpa = gva & 0x07ffffffffffffff;
  679. return 0;
  680. #endif
  681. }
  682. tlb_mapped:
  683. return kvm_vz_guest_tlb_lookup(vcpu, gva, gpa);
  684. }
  685. /**
  686. * kvm_vz_badvaddr_to_gpa() - Convert GVA BadVAddr from root exception to GPA.
  687. * @vcpu: KVM VCPU state.
  688. * @badvaddr: Root BadVAddr.
  689. * @gpa: Output guest physical address.
  690. *
  691. * VZ implementations are permitted to report guest virtual addresses (GVA) in
  692. * BadVAddr on a root exception during guest execution, instead of the more
  693. * convenient guest physical addresses (GPA). When we get a GVA, this function
  694. * converts it to a GPA, taking into account guest segmentation and guest TLB
  695. * state.
  696. *
  697. * Returns: 0 on success.
  698. * -errno on failure.
  699. */
  700. static int kvm_vz_badvaddr_to_gpa(struct kvm_vcpu *vcpu, unsigned long badvaddr,
  701. unsigned long *gpa)
  702. {
  703. unsigned int gexccode = (vcpu->arch.host_cp0_guestctl0 &
  704. MIPS_GCTL0_GEXC) >> MIPS_GCTL0_GEXC_SHIFT;
  705. /* If BadVAddr is GPA, then all is well in the world */
  706. if (likely(gexccode == MIPS_GCTL0_GEXC_GPA)) {
  707. *gpa = badvaddr;
  708. return 0;
  709. }
  710. /* Otherwise we'd expect it to be GVA ... */
  711. if (WARN(gexccode != MIPS_GCTL0_GEXC_GVA,
  712. "Unexpected gexccode %#x\n", gexccode))
  713. return -EINVAL;
  714. /* ... and we need to perform the GVA->GPA translation in software */
  715. return kvm_vz_gva_to_gpa(vcpu, badvaddr, gpa);
  716. }
  717. static int kvm_trap_vz_no_handler(struct kvm_vcpu *vcpu)
  718. {
  719. u32 *opc = (u32 *) vcpu->arch.pc;
  720. u32 cause = vcpu->arch.host_cp0_cause;
  721. u32 exccode = (cause & CAUSEF_EXCCODE) >> CAUSEB_EXCCODE;
  722. unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr;
  723. u32 inst = 0;
  724. /*
  725. * Fetch the instruction.
  726. */
  727. if (cause & CAUSEF_BD)
  728. opc += 1;
  729. kvm_get_badinstr(opc, vcpu, &inst);
  730. kvm_err("Exception Code: %d not handled @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#x\n",
  731. exccode, opc, inst, badvaddr,
  732. read_gc0_status());
  733. kvm_arch_vcpu_dump_regs(vcpu);
  734. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  735. return RESUME_HOST;
  736. }
  737. static unsigned long mips_process_maar(unsigned int op, unsigned long val)
  738. {
  739. /* Mask off unused bits */
  740. unsigned long mask = 0xfffff000 | MIPS_MAAR_S | MIPS_MAAR_VL;
  741. if (read_gc0_pagegrain() & PG_ELPA)
  742. mask |= 0x00ffffff00000000ull;
  743. if (cpu_guest_has_mvh)
  744. mask |= MIPS_MAAR_VH;
  745. /* Set or clear VH */
  746. if (op == mtc_op) {
  747. /* clear VH */
  748. val &= ~MIPS_MAAR_VH;
  749. } else if (op == dmtc_op) {
  750. /* set VH to match VL */
  751. val &= ~MIPS_MAAR_VH;
  752. if (val & MIPS_MAAR_VL)
  753. val |= MIPS_MAAR_VH;
  754. }
  755. return val & mask;
  756. }
  757. static void kvm_write_maari(struct kvm_vcpu *vcpu, unsigned long val)
  758. {
  759. struct mips_coproc *cop0 = &vcpu->arch.cop0;
  760. val &= MIPS_MAARI_INDEX;
  761. if (val == MIPS_MAARI_INDEX)
  762. kvm_write_sw_gc0_maari(cop0, ARRAY_SIZE(vcpu->arch.maar) - 1);
  763. else if (val < ARRAY_SIZE(vcpu->arch.maar))
  764. kvm_write_sw_gc0_maari(cop0, val);
  765. }
  766. static enum emulation_result kvm_vz_gpsi_cop0(union mips_instruction inst,
  767. u32 *opc, u32 cause,
  768. struct kvm_vcpu *vcpu)
  769. {
  770. struct mips_coproc *cop0 = &vcpu->arch.cop0;
  771. enum emulation_result er = EMULATE_DONE;
  772. u32 rt, rd, sel;
  773. unsigned long curr_pc;
  774. unsigned long val;
  775. /*
  776. * Update PC and hold onto current PC in case there is
  777. * an error and we want to rollback the PC
  778. */
  779. curr_pc = vcpu->arch.pc;
  780. er = update_pc(vcpu, cause);
  781. if (er == EMULATE_FAIL)
  782. return er;
  783. if (inst.co_format.co) {
  784. switch (inst.co_format.func) {
  785. case wait_op:
  786. er = kvm_mips_emul_wait(vcpu);
  787. break;
  788. default:
  789. er = EMULATE_FAIL;
  790. }
  791. } else {
  792. rt = inst.c0r_format.rt;
  793. rd = inst.c0r_format.rd;
  794. sel = inst.c0r_format.sel;
  795. switch (inst.c0r_format.rs) {
  796. case dmfc_op:
  797. case mfc_op:
  798. #ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
  799. cop0->stat[rd][sel]++;
  800. #endif
  801. if (rd == MIPS_CP0_COUNT &&
  802. sel == 0) { /* Count */
  803. val = kvm_mips_read_count(vcpu);
  804. } else if (rd == MIPS_CP0_COMPARE &&
  805. sel == 0) { /* Compare */
  806. val = read_gc0_compare();
  807. } else if (rd == MIPS_CP0_LLADDR &&
  808. sel == 0) { /* LLAddr */
  809. if (cpu_guest_has_rw_llb)
  810. val = read_gc0_lladdr() &
  811. MIPS_LLADDR_LLB;
  812. else
  813. val = 0;
  814. } else if (rd == MIPS_CP0_LLADDR &&
  815. sel == 1 && /* MAAR */
  816. cpu_guest_has_maar &&
  817. !cpu_guest_has_dyn_maar) {
  818. /* MAARI must be in range */
  819. BUG_ON(kvm_read_sw_gc0_maari(cop0) >=
  820. ARRAY_SIZE(vcpu->arch.maar));
  821. val = vcpu->arch.maar[
  822. kvm_read_sw_gc0_maari(cop0)];
  823. } else if ((rd == MIPS_CP0_PRID &&
  824. (sel == 0 || /* PRid */
  825. sel == 2 || /* CDMMBase */
  826. sel == 3)) || /* CMGCRBase */
  827. (rd == MIPS_CP0_STATUS &&
  828. (sel == 2 || /* SRSCtl */
  829. sel == 3)) || /* SRSMap */
  830. (rd == MIPS_CP0_CONFIG &&
  831. (sel == 6 || /* Config6 */
  832. sel == 7)) || /* Config7 */
  833. (rd == MIPS_CP0_LLADDR &&
  834. (sel == 2) && /* MAARI */
  835. cpu_guest_has_maar &&
  836. !cpu_guest_has_dyn_maar) ||
  837. (rd == MIPS_CP0_ERRCTL &&
  838. (sel == 0))) { /* ErrCtl */
  839. val = cop0->reg[rd][sel];
  840. #ifdef CONFIG_CPU_LOONGSON64
  841. } else if (rd == MIPS_CP0_DIAG &&
  842. (sel == 0)) { /* Diag */
  843. val = cop0->reg[rd][sel];
  844. #endif
  845. } else {
  846. val = 0;
  847. er = EMULATE_FAIL;
  848. }
  849. if (er != EMULATE_FAIL) {
  850. /* Sign extend */
  851. if (inst.c0r_format.rs == mfc_op)
  852. val = (int)val;
  853. vcpu->arch.gprs[rt] = val;
  854. }
  855. trace_kvm_hwr(vcpu, (inst.c0r_format.rs == mfc_op) ?
  856. KVM_TRACE_MFC0 : KVM_TRACE_DMFC0,
  857. KVM_TRACE_COP0(rd, sel), val);
  858. break;
  859. case dmtc_op:
  860. case mtc_op:
  861. #ifdef CONFIG_KVM_MIPS_DEBUG_COP0_COUNTERS
  862. cop0->stat[rd][sel]++;
  863. #endif
  864. val = vcpu->arch.gprs[rt];
  865. trace_kvm_hwr(vcpu, (inst.c0r_format.rs == mtc_op) ?
  866. KVM_TRACE_MTC0 : KVM_TRACE_DMTC0,
  867. KVM_TRACE_COP0(rd, sel), val);
  868. if (rd == MIPS_CP0_COUNT &&
  869. sel == 0) { /* Count */
  870. kvm_vz_lose_htimer(vcpu);
  871. kvm_mips_write_count(vcpu, vcpu->arch.gprs[rt]);
  872. } else if (rd == MIPS_CP0_COMPARE &&
  873. sel == 0) { /* Compare */
  874. kvm_mips_write_compare(vcpu,
  875. vcpu->arch.gprs[rt],
  876. true);
  877. } else if (rd == MIPS_CP0_LLADDR &&
  878. sel == 0) { /* LLAddr */
  879. /*
  880. * P5600 generates GPSI on guest MTC0 LLAddr.
  881. * Only allow the guest to clear LLB.
  882. */
  883. if (cpu_guest_has_rw_llb &&
  884. !(val & MIPS_LLADDR_LLB))
  885. write_gc0_lladdr(0);
  886. } else if (rd == MIPS_CP0_LLADDR &&
  887. sel == 1 && /* MAAR */
  888. cpu_guest_has_maar &&
  889. !cpu_guest_has_dyn_maar) {
  890. val = mips_process_maar(inst.c0r_format.rs,
  891. val);
  892. /* MAARI must be in range */
  893. BUG_ON(kvm_read_sw_gc0_maari(cop0) >=
  894. ARRAY_SIZE(vcpu->arch.maar));
  895. vcpu->arch.maar[kvm_read_sw_gc0_maari(cop0)] =
  896. val;
  897. } else if (rd == MIPS_CP0_LLADDR &&
  898. (sel == 2) && /* MAARI */
  899. cpu_guest_has_maar &&
  900. !cpu_guest_has_dyn_maar) {
  901. kvm_write_maari(vcpu, val);
  902. } else if (rd == MIPS_CP0_CONFIG &&
  903. (sel == 6)) {
  904. cop0->reg[rd][sel] = (int)val;
  905. } else if (rd == MIPS_CP0_ERRCTL &&
  906. (sel == 0)) { /* ErrCtl */
  907. /* ignore the written value */
  908. #ifdef CONFIG_CPU_LOONGSON64
  909. } else if (rd == MIPS_CP0_DIAG &&
  910. (sel == 0)) { /* Diag */
  911. unsigned long flags;
  912. local_irq_save(flags);
  913. if (val & LOONGSON_DIAG_BTB) {
  914. /* Flush BTB */
  915. set_c0_diag(LOONGSON_DIAG_BTB);
  916. }
  917. if (val & LOONGSON_DIAG_ITLB) {
  918. /* Flush ITLB */
  919. set_c0_diag(LOONGSON_DIAG_ITLB);
  920. }
  921. if (val & LOONGSON_DIAG_DTLB) {
  922. /* Flush DTLB */
  923. set_c0_diag(LOONGSON_DIAG_DTLB);
  924. }
  925. if (val & LOONGSON_DIAG_VTLB) {
  926. /* Flush VTLB */
  927. kvm_loongson_clear_guest_vtlb();
  928. }
  929. if (val & LOONGSON_DIAG_FTLB) {
  930. /* Flush FTLB */
  931. kvm_loongson_clear_guest_ftlb();
  932. }
  933. local_irq_restore(flags);
  934. #endif
  935. } else {
  936. er = EMULATE_FAIL;
  937. }
  938. break;
  939. default:
  940. er = EMULATE_FAIL;
  941. break;
  942. }
  943. }
  944. /* Rollback PC only if emulation was unsuccessful */
  945. if (er == EMULATE_FAIL) {
  946. kvm_err("[%#lx]%s: unsupported cop0 instruction 0x%08x\n",
  947. curr_pc, __func__, inst.word);
  948. vcpu->arch.pc = curr_pc;
  949. }
  950. return er;
  951. }
  952. static enum emulation_result kvm_vz_gpsi_cache(union mips_instruction inst,
  953. u32 *opc, u32 cause,
  954. struct kvm_vcpu *vcpu)
  955. {
  956. enum emulation_result er = EMULATE_DONE;
  957. u32 cache, op_inst, op, base;
  958. s16 offset;
  959. struct kvm_vcpu_arch *arch = &vcpu->arch;
  960. unsigned long va, curr_pc;
  961. /*
  962. * Update PC and hold onto current PC in case there is
  963. * an error and we want to rollback the PC
  964. */
  965. curr_pc = vcpu->arch.pc;
  966. er = update_pc(vcpu, cause);
  967. if (er == EMULATE_FAIL)
  968. return er;
  969. base = inst.i_format.rs;
  970. op_inst = inst.i_format.rt;
  971. if (cpu_has_mips_r6)
  972. offset = inst.spec3_format.simmediate;
  973. else
  974. offset = inst.i_format.simmediate;
  975. cache = op_inst & CacheOp_Cache;
  976. op = op_inst & CacheOp_Op;
  977. va = arch->gprs[base] + offset;
  978. kvm_debug("CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
  979. cache, op, base, arch->gprs[base], offset);
  980. /* Secondary or tirtiary cache ops ignored */
  981. if (cache != Cache_I && cache != Cache_D)
  982. return EMULATE_DONE;
  983. switch (op_inst) {
  984. case Index_Invalidate_I:
  985. flush_icache_line_indexed(va);
  986. return EMULATE_DONE;
  987. case Index_Writeback_Inv_D:
  988. flush_dcache_line_indexed(va);
  989. return EMULATE_DONE;
  990. case Hit_Invalidate_I:
  991. case Hit_Invalidate_D:
  992. case Hit_Writeback_Inv_D:
  993. if (boot_cpu_type() == CPU_CAVIUM_OCTEON3) {
  994. /* We can just flush entire icache */
  995. local_flush_icache_range(0, 0);
  996. return EMULATE_DONE;
  997. }
  998. /* So far, other platforms support guest hit cache ops */
  999. break;
  1000. default:
  1001. break;
  1002. }
  1003. kvm_err("@ %#lx/%#lx CACHE (cache: %#x, op: %#x, base[%d]: %#lx, offset: %#x\n",
  1004. curr_pc, vcpu->arch.gprs[31], cache, op, base, arch->gprs[base],
  1005. offset);
  1006. /* Rollback PC */
  1007. vcpu->arch.pc = curr_pc;
  1008. return EMULATE_FAIL;
  1009. }
  1010. #ifdef CONFIG_CPU_LOONGSON64
  1011. static enum emulation_result kvm_vz_gpsi_lwc2(union mips_instruction inst,
  1012. u32 *opc, u32 cause,
  1013. struct kvm_vcpu *vcpu)
  1014. {
  1015. unsigned int rs, rd;
  1016. unsigned int hostcfg;
  1017. unsigned long curr_pc;
  1018. enum emulation_result er = EMULATE_DONE;
  1019. /*
  1020. * Update PC and hold onto current PC in case there is
  1021. * an error and we want to rollback the PC
  1022. */
  1023. curr_pc = vcpu->arch.pc;
  1024. er = update_pc(vcpu, cause);
  1025. if (er == EMULATE_FAIL)
  1026. return er;
  1027. rs = inst.loongson3_lscsr_format.rs;
  1028. rd = inst.loongson3_lscsr_format.rd;
  1029. switch (inst.loongson3_lscsr_format.fr) {
  1030. case 0x8: /* Read CPUCFG */
  1031. ++vcpu->stat.vz_cpucfg_exits;
  1032. hostcfg = read_cpucfg(vcpu->arch.gprs[rs]);
  1033. switch (vcpu->arch.gprs[rs]) {
  1034. case LOONGSON_CFG0:
  1035. vcpu->arch.gprs[rd] = 0x14c000;
  1036. break;
  1037. case LOONGSON_CFG1:
  1038. hostcfg &= (LOONGSON_CFG1_FP | LOONGSON_CFG1_MMI |
  1039. LOONGSON_CFG1_MSA1 | LOONGSON_CFG1_MSA2 |
  1040. LOONGSON_CFG1_SFBP);
  1041. vcpu->arch.gprs[rd] = hostcfg;
  1042. break;
  1043. case LOONGSON_CFG2:
  1044. hostcfg &= (LOONGSON_CFG2_LEXT1 | LOONGSON_CFG2_LEXT2 |
  1045. LOONGSON_CFG2_LEXT3 | LOONGSON_CFG2_LSPW);
  1046. vcpu->arch.gprs[rd] = hostcfg;
  1047. break;
  1048. case LOONGSON_CFG3:
  1049. vcpu->arch.gprs[rd] = hostcfg;
  1050. break;
  1051. default:
  1052. /* Don't export any other advanced features to guest */
  1053. vcpu->arch.gprs[rd] = 0;
  1054. break;
  1055. }
  1056. break;
  1057. default:
  1058. kvm_err("lwc2 emulate not impl %d rs %lx @%lx\n",
  1059. inst.loongson3_lscsr_format.fr, vcpu->arch.gprs[rs], curr_pc);
  1060. er = EMULATE_FAIL;
  1061. break;
  1062. }
  1063. /* Rollback PC only if emulation was unsuccessful */
  1064. if (er == EMULATE_FAIL) {
  1065. kvm_err("[%#lx]%s: unsupported lwc2 instruction 0x%08x 0x%08x\n",
  1066. curr_pc, __func__, inst.word, inst.loongson3_lscsr_format.fr);
  1067. vcpu->arch.pc = curr_pc;
  1068. }
  1069. return er;
  1070. }
  1071. #endif
  1072. static enum emulation_result kvm_trap_vz_handle_gpsi(u32 cause, u32 *opc,
  1073. struct kvm_vcpu *vcpu)
  1074. {
  1075. enum emulation_result er = EMULATE_DONE;
  1076. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1077. union mips_instruction inst;
  1078. int rd, rt, sel;
  1079. int err;
  1080. /*
  1081. * Fetch the instruction.
  1082. */
  1083. if (cause & CAUSEF_BD)
  1084. opc += 1;
  1085. err = kvm_get_badinstr(opc, vcpu, &inst.word);
  1086. if (err)
  1087. return EMULATE_FAIL;
  1088. switch (inst.r_format.opcode) {
  1089. case cop0_op:
  1090. er = kvm_vz_gpsi_cop0(inst, opc, cause, vcpu);
  1091. break;
  1092. #ifndef CONFIG_CPU_MIPSR6
  1093. case cache_op:
  1094. trace_kvm_exit(vcpu, KVM_TRACE_EXIT_CACHE);
  1095. er = kvm_vz_gpsi_cache(inst, opc, cause, vcpu);
  1096. break;
  1097. #endif
  1098. #ifdef CONFIG_CPU_LOONGSON64
  1099. case lwc2_op:
  1100. er = kvm_vz_gpsi_lwc2(inst, opc, cause, vcpu);
  1101. break;
  1102. #endif
  1103. case spec3_op:
  1104. switch (inst.spec3_format.func) {
  1105. #ifdef CONFIG_CPU_MIPSR6
  1106. case cache6_op:
  1107. trace_kvm_exit(vcpu, KVM_TRACE_EXIT_CACHE);
  1108. er = kvm_vz_gpsi_cache(inst, opc, cause, vcpu);
  1109. break;
  1110. #endif
  1111. case rdhwr_op:
  1112. if (inst.r_format.rs || (inst.r_format.re >> 3))
  1113. goto unknown;
  1114. rd = inst.r_format.rd;
  1115. rt = inst.r_format.rt;
  1116. sel = inst.r_format.re & 0x7;
  1117. switch (rd) {
  1118. case MIPS_HWR_CC: /* Read count register */
  1119. arch->gprs[rt] =
  1120. (long)(int)kvm_mips_read_count(vcpu);
  1121. break;
  1122. default:
  1123. trace_kvm_hwr(vcpu, KVM_TRACE_RDHWR,
  1124. KVM_TRACE_HWR(rd, sel), 0);
  1125. goto unknown;
  1126. }
  1127. trace_kvm_hwr(vcpu, KVM_TRACE_RDHWR,
  1128. KVM_TRACE_HWR(rd, sel), arch->gprs[rt]);
  1129. er = update_pc(vcpu, cause);
  1130. break;
  1131. default:
  1132. goto unknown;
  1133. }
  1134. break;
  1135. unknown:
  1136. default:
  1137. kvm_err("GPSI exception not supported (%p/%#x)\n",
  1138. opc, inst.word);
  1139. kvm_arch_vcpu_dump_regs(vcpu);
  1140. er = EMULATE_FAIL;
  1141. break;
  1142. }
  1143. return er;
  1144. }
  1145. static enum emulation_result kvm_trap_vz_handle_gsfc(u32 cause, u32 *opc,
  1146. struct kvm_vcpu *vcpu)
  1147. {
  1148. enum emulation_result er = EMULATE_DONE;
  1149. struct kvm_vcpu_arch *arch = &vcpu->arch;
  1150. union mips_instruction inst;
  1151. int err;
  1152. /*
  1153. * Fetch the instruction.
  1154. */
  1155. if (cause & CAUSEF_BD)
  1156. opc += 1;
  1157. err = kvm_get_badinstr(opc, vcpu, &inst.word);
  1158. if (err)
  1159. return EMULATE_FAIL;
  1160. /* complete MTC0 on behalf of guest and advance EPC */
  1161. if (inst.c0r_format.opcode == cop0_op &&
  1162. inst.c0r_format.rs == mtc_op &&
  1163. inst.c0r_format.z == 0) {
  1164. int rt = inst.c0r_format.rt;
  1165. int rd = inst.c0r_format.rd;
  1166. int sel = inst.c0r_format.sel;
  1167. unsigned int val = arch->gprs[rt];
  1168. unsigned int old_val, change;
  1169. trace_kvm_hwr(vcpu, KVM_TRACE_MTC0, KVM_TRACE_COP0(rd, sel),
  1170. val);
  1171. if ((rd == MIPS_CP0_STATUS) && (sel == 0)) {
  1172. /* FR bit should read as zero if no FPU */
  1173. if (!kvm_mips_guest_has_fpu(&vcpu->arch))
  1174. val &= ~(ST0_CU1 | ST0_FR);
  1175. /*
  1176. * Also don't allow FR to be set if host doesn't support
  1177. * it.
  1178. */
  1179. if (!(boot_cpu_data.fpu_id & MIPS_FPIR_F64))
  1180. val &= ~ST0_FR;
  1181. old_val = read_gc0_status();
  1182. change = val ^ old_val;
  1183. if (change & ST0_FR) {
  1184. /*
  1185. * FPU and Vector register state is made
  1186. * UNPREDICTABLE by a change of FR, so don't
  1187. * even bother saving it.
  1188. */
  1189. kvm_drop_fpu(vcpu);
  1190. }
  1191. /*
  1192. * If MSA state is already live, it is undefined how it
  1193. * interacts with FR=0 FPU state, and we don't want to
  1194. * hit reserved instruction exceptions trying to save
  1195. * the MSA state later when CU=1 && FR=1, so play it
  1196. * safe and save it first.
  1197. */
  1198. if (change & ST0_CU1 && !(val & ST0_FR) &&
  1199. vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA)
  1200. kvm_lose_fpu(vcpu);
  1201. write_gc0_status(val);
  1202. } else if ((rd == MIPS_CP0_CAUSE) && (sel == 0)) {
  1203. u32 old_cause = read_gc0_cause();
  1204. u32 change = old_cause ^ val;
  1205. /* DC bit enabling/disabling timer? */
  1206. if (change & CAUSEF_DC) {
  1207. if (val & CAUSEF_DC) {
  1208. kvm_vz_lose_htimer(vcpu);
  1209. kvm_mips_count_disable_cause(vcpu);
  1210. } else {
  1211. kvm_mips_count_enable_cause(vcpu);
  1212. }
  1213. }
  1214. /* Only certain bits are RW to the guest */
  1215. change &= (CAUSEF_DC | CAUSEF_IV | CAUSEF_WP |
  1216. CAUSEF_IP0 | CAUSEF_IP1);
  1217. /* WP can only be cleared */
  1218. change &= ~CAUSEF_WP | old_cause;
  1219. write_gc0_cause(old_cause ^ change);
  1220. } else if ((rd == MIPS_CP0_STATUS) && (sel == 1)) { /* IntCtl */
  1221. write_gc0_intctl(val);
  1222. } else if ((rd == MIPS_CP0_CONFIG) && (sel == 5)) {
  1223. old_val = read_gc0_config5();
  1224. change = val ^ old_val;
  1225. /* Handle changes in FPU/MSA modes */
  1226. preempt_disable();
  1227. /*
  1228. * Propagate FRE changes immediately if the FPU
  1229. * context is already loaded.
  1230. */
  1231. if (change & MIPS_CONF5_FRE &&
  1232. vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)
  1233. change_c0_config5(MIPS_CONF5_FRE, val);
  1234. preempt_enable();
  1235. val = old_val ^
  1236. (change & kvm_vz_config5_guest_wrmask(vcpu));
  1237. write_gc0_config5(val);
  1238. } else {
  1239. kvm_err("Handle GSFC, unsupported field change @ %p: %#x\n",
  1240. opc, inst.word);
  1241. er = EMULATE_FAIL;
  1242. }
  1243. if (er != EMULATE_FAIL)
  1244. er = update_pc(vcpu, cause);
  1245. } else {
  1246. kvm_err("Handle GSFC, unrecognized instruction @ %p: %#x\n",
  1247. opc, inst.word);
  1248. er = EMULATE_FAIL;
  1249. }
  1250. return er;
  1251. }
  1252. static enum emulation_result kvm_trap_vz_handle_ghfc(u32 cause, u32 *opc,
  1253. struct kvm_vcpu *vcpu)
  1254. {
  1255. /*
  1256. * Presumably this is due to MC (guest mode change), so lets trace some
  1257. * relevant info.
  1258. */
  1259. trace_kvm_guest_mode_change(vcpu);
  1260. return EMULATE_DONE;
  1261. }
  1262. static enum emulation_result kvm_trap_vz_handle_hc(u32 cause, u32 *opc,
  1263. struct kvm_vcpu *vcpu)
  1264. {
  1265. enum emulation_result er;
  1266. union mips_instruction inst;
  1267. unsigned long curr_pc;
  1268. int err;
  1269. if (cause & CAUSEF_BD)
  1270. opc += 1;
  1271. err = kvm_get_badinstr(opc, vcpu, &inst.word);
  1272. if (err)
  1273. return EMULATE_FAIL;
  1274. /*
  1275. * Update PC and hold onto current PC in case there is
  1276. * an error and we want to rollback the PC
  1277. */
  1278. curr_pc = vcpu->arch.pc;
  1279. er = update_pc(vcpu, cause);
  1280. if (er == EMULATE_FAIL)
  1281. return er;
  1282. er = kvm_mips_emul_hypcall(vcpu, inst);
  1283. if (er == EMULATE_FAIL)
  1284. vcpu->arch.pc = curr_pc;
  1285. return er;
  1286. }
  1287. static enum emulation_result kvm_trap_vz_no_handler_guest_exit(u32 gexccode,
  1288. u32 cause,
  1289. u32 *opc,
  1290. struct kvm_vcpu *vcpu)
  1291. {
  1292. u32 inst;
  1293. /*
  1294. * Fetch the instruction.
  1295. */
  1296. if (cause & CAUSEF_BD)
  1297. opc += 1;
  1298. kvm_get_badinstr(opc, vcpu, &inst);
  1299. kvm_err("Guest Exception Code: %d not yet handled @ PC: %p, inst: 0x%08x Status: %#x\n",
  1300. gexccode, opc, inst, read_gc0_status());
  1301. return EMULATE_FAIL;
  1302. }
  1303. static int kvm_trap_vz_handle_guest_exit(struct kvm_vcpu *vcpu)
  1304. {
  1305. u32 *opc = (u32 *) vcpu->arch.pc;
  1306. u32 cause = vcpu->arch.host_cp0_cause;
  1307. enum emulation_result er = EMULATE_DONE;
  1308. u32 gexccode = (vcpu->arch.host_cp0_guestctl0 &
  1309. MIPS_GCTL0_GEXC) >> MIPS_GCTL0_GEXC_SHIFT;
  1310. int ret = RESUME_GUEST;
  1311. trace_kvm_exit(vcpu, KVM_TRACE_EXIT_GEXCCODE_BASE + gexccode);
  1312. switch (gexccode) {
  1313. case MIPS_GCTL0_GEXC_GPSI:
  1314. ++vcpu->stat.vz_gpsi_exits;
  1315. er = kvm_trap_vz_handle_gpsi(cause, opc, vcpu);
  1316. break;
  1317. case MIPS_GCTL0_GEXC_GSFC:
  1318. ++vcpu->stat.vz_gsfc_exits;
  1319. er = kvm_trap_vz_handle_gsfc(cause, opc, vcpu);
  1320. break;
  1321. case MIPS_GCTL0_GEXC_HC:
  1322. ++vcpu->stat.vz_hc_exits;
  1323. er = kvm_trap_vz_handle_hc(cause, opc, vcpu);
  1324. break;
  1325. case MIPS_GCTL0_GEXC_GRR:
  1326. ++vcpu->stat.vz_grr_exits;
  1327. er = kvm_trap_vz_no_handler_guest_exit(gexccode, cause, opc,
  1328. vcpu);
  1329. break;
  1330. case MIPS_GCTL0_GEXC_GVA:
  1331. ++vcpu->stat.vz_gva_exits;
  1332. er = kvm_trap_vz_no_handler_guest_exit(gexccode, cause, opc,
  1333. vcpu);
  1334. break;
  1335. case MIPS_GCTL0_GEXC_GHFC:
  1336. ++vcpu->stat.vz_ghfc_exits;
  1337. er = kvm_trap_vz_handle_ghfc(cause, opc, vcpu);
  1338. break;
  1339. case MIPS_GCTL0_GEXC_GPA:
  1340. ++vcpu->stat.vz_gpa_exits;
  1341. er = kvm_trap_vz_no_handler_guest_exit(gexccode, cause, opc,
  1342. vcpu);
  1343. break;
  1344. default:
  1345. ++vcpu->stat.vz_resvd_exits;
  1346. er = kvm_trap_vz_no_handler_guest_exit(gexccode, cause, opc,
  1347. vcpu);
  1348. break;
  1349. }
  1350. if (er == EMULATE_DONE) {
  1351. ret = RESUME_GUEST;
  1352. } else if (er == EMULATE_HYPERCALL) {
  1353. ret = kvm_mips_handle_hypcall(vcpu);
  1354. } else {
  1355. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  1356. ret = RESUME_HOST;
  1357. }
  1358. return ret;
  1359. }
  1360. /**
  1361. * kvm_trap_vz_handle_cop_unusable() - Guest used unusable coprocessor.
  1362. * @vcpu: Virtual CPU context.
  1363. *
  1364. * Handle when the guest attempts to use a coprocessor which hasn't been allowed
  1365. * by the root context.
  1366. *
  1367. * Return: value indicating whether to resume the host or the guest
  1368. * (RESUME_HOST or RESUME_GUEST)
  1369. */
  1370. static int kvm_trap_vz_handle_cop_unusable(struct kvm_vcpu *vcpu)
  1371. {
  1372. u32 cause = vcpu->arch.host_cp0_cause;
  1373. enum emulation_result er = EMULATE_FAIL;
  1374. int ret = RESUME_GUEST;
  1375. if (((cause & CAUSEF_CE) >> CAUSEB_CE) == 1) {
  1376. /*
  1377. * If guest FPU not present, the FPU operation should have been
  1378. * treated as a reserved instruction!
  1379. * If FPU already in use, we shouldn't get this at all.
  1380. */
  1381. if (WARN_ON(!kvm_mips_guest_has_fpu(&vcpu->arch) ||
  1382. vcpu->arch.aux_inuse & KVM_MIPS_AUX_FPU)) {
  1383. preempt_enable();
  1384. return EMULATE_FAIL;
  1385. }
  1386. kvm_own_fpu(vcpu);
  1387. er = EMULATE_DONE;
  1388. }
  1389. /* other coprocessors not handled */
  1390. switch (er) {
  1391. case EMULATE_DONE:
  1392. ret = RESUME_GUEST;
  1393. break;
  1394. case EMULATE_FAIL:
  1395. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  1396. ret = RESUME_HOST;
  1397. break;
  1398. default:
  1399. BUG();
  1400. }
  1401. return ret;
  1402. }
  1403. /**
  1404. * kvm_trap_vz_handle_msa_disabled() - Guest used MSA while disabled in root.
  1405. * @vcpu: Virtual CPU context.
  1406. *
  1407. * Handle when the guest attempts to use MSA when it is disabled in the root
  1408. * context.
  1409. *
  1410. * Return: value indicating whether to resume the host or the guest
  1411. * (RESUME_HOST or RESUME_GUEST)
  1412. */
  1413. static int kvm_trap_vz_handle_msa_disabled(struct kvm_vcpu *vcpu)
  1414. {
  1415. /*
  1416. * If MSA not present or not exposed to guest or FR=0, the MSA operation
  1417. * should have been treated as a reserved instruction!
  1418. * Same if CU1=1, FR=0.
  1419. * If MSA already in use, we shouldn't get this at all.
  1420. */
  1421. if (!kvm_mips_guest_has_msa(&vcpu->arch) ||
  1422. (read_gc0_status() & (ST0_CU1 | ST0_FR)) == ST0_CU1 ||
  1423. !(read_gc0_config5() & MIPS_CONF5_MSAEN) ||
  1424. vcpu->arch.aux_inuse & KVM_MIPS_AUX_MSA) {
  1425. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  1426. return RESUME_HOST;
  1427. }
  1428. kvm_own_msa(vcpu);
  1429. return RESUME_GUEST;
  1430. }
  1431. static int kvm_trap_vz_handle_tlb_ld_miss(struct kvm_vcpu *vcpu)
  1432. {
  1433. struct kvm_run *run = vcpu->run;
  1434. u32 *opc = (u32 *) vcpu->arch.pc;
  1435. u32 cause = vcpu->arch.host_cp0_cause;
  1436. ulong badvaddr = vcpu->arch.host_cp0_badvaddr;
  1437. union mips_instruction inst;
  1438. enum emulation_result er = EMULATE_DONE;
  1439. int err, ret = RESUME_GUEST;
  1440. if (kvm_mips_handle_vz_root_tlb_fault(badvaddr, vcpu, false)) {
  1441. /* A code fetch fault doesn't count as an MMIO */
  1442. if (kvm_is_ifetch_fault(&vcpu->arch)) {
  1443. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  1444. return RESUME_HOST;
  1445. }
  1446. /* Fetch the instruction */
  1447. if (cause & CAUSEF_BD)
  1448. opc += 1;
  1449. err = kvm_get_badinstr(opc, vcpu, &inst.word);
  1450. if (err) {
  1451. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  1452. return RESUME_HOST;
  1453. }
  1454. /* Treat as MMIO */
  1455. er = kvm_mips_emulate_load(inst, cause, vcpu);
  1456. if (er == EMULATE_FAIL) {
  1457. kvm_err("Guest Emulate Load from MMIO space failed: PC: %p, BadVaddr: %#lx\n",
  1458. opc, badvaddr);
  1459. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  1460. }
  1461. }
  1462. if (er == EMULATE_DONE) {
  1463. ret = RESUME_GUEST;
  1464. } else if (er == EMULATE_DO_MMIO) {
  1465. run->exit_reason = KVM_EXIT_MMIO;
  1466. ret = RESUME_HOST;
  1467. } else {
  1468. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  1469. ret = RESUME_HOST;
  1470. }
  1471. return ret;
  1472. }
  1473. static int kvm_trap_vz_handle_tlb_st_miss(struct kvm_vcpu *vcpu)
  1474. {
  1475. struct kvm_run *run = vcpu->run;
  1476. u32 *opc = (u32 *) vcpu->arch.pc;
  1477. u32 cause = vcpu->arch.host_cp0_cause;
  1478. ulong badvaddr = vcpu->arch.host_cp0_badvaddr;
  1479. union mips_instruction inst;
  1480. enum emulation_result er = EMULATE_DONE;
  1481. int err;
  1482. int ret = RESUME_GUEST;
  1483. /* Just try the access again if we couldn't do the translation */
  1484. if (kvm_vz_badvaddr_to_gpa(vcpu, badvaddr, &badvaddr))
  1485. return RESUME_GUEST;
  1486. vcpu->arch.host_cp0_badvaddr = badvaddr;
  1487. if (kvm_mips_handle_vz_root_tlb_fault(badvaddr, vcpu, true)) {
  1488. /* Fetch the instruction */
  1489. if (cause & CAUSEF_BD)
  1490. opc += 1;
  1491. err = kvm_get_badinstr(opc, vcpu, &inst.word);
  1492. if (err) {
  1493. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  1494. return RESUME_HOST;
  1495. }
  1496. /* Treat as MMIO */
  1497. er = kvm_mips_emulate_store(inst, cause, vcpu);
  1498. if (er == EMULATE_FAIL) {
  1499. kvm_err("Guest Emulate Store to MMIO space failed: PC: %p, BadVaddr: %#lx\n",
  1500. opc, badvaddr);
  1501. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  1502. }
  1503. }
  1504. if (er == EMULATE_DONE) {
  1505. ret = RESUME_GUEST;
  1506. } else if (er == EMULATE_DO_MMIO) {
  1507. run->exit_reason = KVM_EXIT_MMIO;
  1508. ret = RESUME_HOST;
  1509. } else {
  1510. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  1511. ret = RESUME_HOST;
  1512. }
  1513. return ret;
  1514. }
  1515. static u64 kvm_vz_get_one_regs[] = {
  1516. KVM_REG_MIPS_CP0_INDEX,
  1517. KVM_REG_MIPS_CP0_ENTRYLO0,
  1518. KVM_REG_MIPS_CP0_ENTRYLO1,
  1519. KVM_REG_MIPS_CP0_CONTEXT,
  1520. KVM_REG_MIPS_CP0_PAGEMASK,
  1521. KVM_REG_MIPS_CP0_PAGEGRAIN,
  1522. KVM_REG_MIPS_CP0_WIRED,
  1523. KVM_REG_MIPS_CP0_HWRENA,
  1524. KVM_REG_MIPS_CP0_BADVADDR,
  1525. KVM_REG_MIPS_CP0_COUNT,
  1526. KVM_REG_MIPS_CP0_ENTRYHI,
  1527. KVM_REG_MIPS_CP0_COMPARE,
  1528. KVM_REG_MIPS_CP0_STATUS,
  1529. KVM_REG_MIPS_CP0_INTCTL,
  1530. KVM_REG_MIPS_CP0_CAUSE,
  1531. KVM_REG_MIPS_CP0_EPC,
  1532. KVM_REG_MIPS_CP0_PRID,
  1533. KVM_REG_MIPS_CP0_EBASE,
  1534. KVM_REG_MIPS_CP0_CONFIG,
  1535. KVM_REG_MIPS_CP0_CONFIG1,
  1536. KVM_REG_MIPS_CP0_CONFIG2,
  1537. KVM_REG_MIPS_CP0_CONFIG3,
  1538. KVM_REG_MIPS_CP0_CONFIG4,
  1539. KVM_REG_MIPS_CP0_CONFIG5,
  1540. KVM_REG_MIPS_CP0_CONFIG6,
  1541. #ifdef CONFIG_64BIT
  1542. KVM_REG_MIPS_CP0_XCONTEXT,
  1543. #endif
  1544. KVM_REG_MIPS_CP0_ERROREPC,
  1545. KVM_REG_MIPS_COUNT_CTL,
  1546. KVM_REG_MIPS_COUNT_RESUME,
  1547. KVM_REG_MIPS_COUNT_HZ,
  1548. };
  1549. static u64 kvm_vz_get_one_regs_contextconfig[] = {
  1550. KVM_REG_MIPS_CP0_CONTEXTCONFIG,
  1551. #ifdef CONFIG_64BIT
  1552. KVM_REG_MIPS_CP0_XCONTEXTCONFIG,
  1553. #endif
  1554. };
  1555. static u64 kvm_vz_get_one_regs_segments[] = {
  1556. KVM_REG_MIPS_CP0_SEGCTL0,
  1557. KVM_REG_MIPS_CP0_SEGCTL1,
  1558. KVM_REG_MIPS_CP0_SEGCTL2,
  1559. };
  1560. static u64 kvm_vz_get_one_regs_htw[] = {
  1561. KVM_REG_MIPS_CP0_PWBASE,
  1562. KVM_REG_MIPS_CP0_PWFIELD,
  1563. KVM_REG_MIPS_CP0_PWSIZE,
  1564. KVM_REG_MIPS_CP0_PWCTL,
  1565. };
  1566. static u64 kvm_vz_get_one_regs_kscratch[] = {
  1567. KVM_REG_MIPS_CP0_KSCRATCH1,
  1568. KVM_REG_MIPS_CP0_KSCRATCH2,
  1569. KVM_REG_MIPS_CP0_KSCRATCH3,
  1570. KVM_REG_MIPS_CP0_KSCRATCH4,
  1571. KVM_REG_MIPS_CP0_KSCRATCH5,
  1572. KVM_REG_MIPS_CP0_KSCRATCH6,
  1573. };
  1574. static unsigned long kvm_vz_num_regs(struct kvm_vcpu *vcpu)
  1575. {
  1576. unsigned long ret;
  1577. ret = ARRAY_SIZE(kvm_vz_get_one_regs);
  1578. if (cpu_guest_has_userlocal)
  1579. ++ret;
  1580. if (cpu_guest_has_badinstr)
  1581. ++ret;
  1582. if (cpu_guest_has_badinstrp)
  1583. ++ret;
  1584. if (cpu_guest_has_contextconfig)
  1585. ret += ARRAY_SIZE(kvm_vz_get_one_regs_contextconfig);
  1586. if (cpu_guest_has_segments)
  1587. ret += ARRAY_SIZE(kvm_vz_get_one_regs_segments);
  1588. if (cpu_guest_has_htw || cpu_guest_has_ldpte)
  1589. ret += ARRAY_SIZE(kvm_vz_get_one_regs_htw);
  1590. if (cpu_guest_has_maar && !cpu_guest_has_dyn_maar)
  1591. ret += 1 + ARRAY_SIZE(vcpu->arch.maar);
  1592. ret += __arch_hweight8(cpu_data[0].guest.kscratch_mask);
  1593. return ret;
  1594. }
  1595. static int kvm_vz_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices)
  1596. {
  1597. u64 index;
  1598. unsigned int i;
  1599. if (copy_to_user(indices, kvm_vz_get_one_regs,
  1600. sizeof(kvm_vz_get_one_regs)))
  1601. return -EFAULT;
  1602. indices += ARRAY_SIZE(kvm_vz_get_one_regs);
  1603. if (cpu_guest_has_userlocal) {
  1604. index = KVM_REG_MIPS_CP0_USERLOCAL;
  1605. if (copy_to_user(indices, &index, sizeof(index)))
  1606. return -EFAULT;
  1607. ++indices;
  1608. }
  1609. if (cpu_guest_has_badinstr) {
  1610. index = KVM_REG_MIPS_CP0_BADINSTR;
  1611. if (copy_to_user(indices, &index, sizeof(index)))
  1612. return -EFAULT;
  1613. ++indices;
  1614. }
  1615. if (cpu_guest_has_badinstrp) {
  1616. index = KVM_REG_MIPS_CP0_BADINSTRP;
  1617. if (copy_to_user(indices, &index, sizeof(index)))
  1618. return -EFAULT;
  1619. ++indices;
  1620. }
  1621. if (cpu_guest_has_contextconfig) {
  1622. if (copy_to_user(indices, kvm_vz_get_one_regs_contextconfig,
  1623. sizeof(kvm_vz_get_one_regs_contextconfig)))
  1624. return -EFAULT;
  1625. indices += ARRAY_SIZE(kvm_vz_get_one_regs_contextconfig);
  1626. }
  1627. if (cpu_guest_has_segments) {
  1628. if (copy_to_user(indices, kvm_vz_get_one_regs_segments,
  1629. sizeof(kvm_vz_get_one_regs_segments)))
  1630. return -EFAULT;
  1631. indices += ARRAY_SIZE(kvm_vz_get_one_regs_segments);
  1632. }
  1633. if (cpu_guest_has_htw || cpu_guest_has_ldpte) {
  1634. if (copy_to_user(indices, kvm_vz_get_one_regs_htw,
  1635. sizeof(kvm_vz_get_one_regs_htw)))
  1636. return -EFAULT;
  1637. indices += ARRAY_SIZE(kvm_vz_get_one_regs_htw);
  1638. }
  1639. if (cpu_guest_has_maar && !cpu_guest_has_dyn_maar) {
  1640. for (i = 0; i < ARRAY_SIZE(vcpu->arch.maar); ++i) {
  1641. index = KVM_REG_MIPS_CP0_MAAR(i);
  1642. if (copy_to_user(indices, &index, sizeof(index)))
  1643. return -EFAULT;
  1644. ++indices;
  1645. }
  1646. index = KVM_REG_MIPS_CP0_MAARI;
  1647. if (copy_to_user(indices, &index, sizeof(index)))
  1648. return -EFAULT;
  1649. ++indices;
  1650. }
  1651. for (i = 0; i < 6; ++i) {
  1652. if (!cpu_guest_has_kscr(i + 2))
  1653. continue;
  1654. if (copy_to_user(indices, &kvm_vz_get_one_regs_kscratch[i],
  1655. sizeof(kvm_vz_get_one_regs_kscratch[i])))
  1656. return -EFAULT;
  1657. ++indices;
  1658. }
  1659. return 0;
  1660. }
  1661. static inline s64 entrylo_kvm_to_user(unsigned long v)
  1662. {
  1663. s64 mask, ret = v;
  1664. if (BITS_PER_LONG == 32) {
  1665. /*
  1666. * KVM API exposes 64-bit version of the register, so move the
  1667. * RI/XI bits up into place.
  1668. */
  1669. mask = MIPS_ENTRYLO_RI | MIPS_ENTRYLO_XI;
  1670. ret &= ~mask;
  1671. ret |= ((s64)v & mask) << 32;
  1672. }
  1673. return ret;
  1674. }
  1675. static inline unsigned long entrylo_user_to_kvm(s64 v)
  1676. {
  1677. unsigned long mask, ret = v;
  1678. if (BITS_PER_LONG == 32) {
  1679. /*
  1680. * KVM API exposes 64-bit versiono of the register, so move the
  1681. * RI/XI bits down into place.
  1682. */
  1683. mask = MIPS_ENTRYLO_RI | MIPS_ENTRYLO_XI;
  1684. ret &= ~mask;
  1685. ret |= (v >> 32) & mask;
  1686. }
  1687. return ret;
  1688. }
  1689. static int kvm_vz_get_one_reg(struct kvm_vcpu *vcpu,
  1690. const struct kvm_one_reg *reg,
  1691. s64 *v)
  1692. {
  1693. struct mips_coproc *cop0 = &vcpu->arch.cop0;
  1694. unsigned int idx;
  1695. switch (reg->id) {
  1696. case KVM_REG_MIPS_CP0_INDEX:
  1697. *v = (long)read_gc0_index();
  1698. break;
  1699. case KVM_REG_MIPS_CP0_ENTRYLO0:
  1700. *v = entrylo_kvm_to_user(read_gc0_entrylo0());
  1701. break;
  1702. case KVM_REG_MIPS_CP0_ENTRYLO1:
  1703. *v = entrylo_kvm_to_user(read_gc0_entrylo1());
  1704. break;
  1705. case KVM_REG_MIPS_CP0_CONTEXT:
  1706. *v = (long)read_gc0_context();
  1707. break;
  1708. case KVM_REG_MIPS_CP0_CONTEXTCONFIG:
  1709. if (!cpu_guest_has_contextconfig)
  1710. return -EINVAL;
  1711. *v = read_gc0_contextconfig();
  1712. break;
  1713. case KVM_REG_MIPS_CP0_USERLOCAL:
  1714. if (!cpu_guest_has_userlocal)
  1715. return -EINVAL;
  1716. *v = read_gc0_userlocal();
  1717. break;
  1718. #ifdef CONFIG_64BIT
  1719. case KVM_REG_MIPS_CP0_XCONTEXTCONFIG:
  1720. if (!cpu_guest_has_contextconfig)
  1721. return -EINVAL;
  1722. *v = read_gc0_xcontextconfig();
  1723. break;
  1724. #endif
  1725. case KVM_REG_MIPS_CP0_PAGEMASK:
  1726. *v = (long)read_gc0_pagemask();
  1727. break;
  1728. case KVM_REG_MIPS_CP0_PAGEGRAIN:
  1729. *v = (long)read_gc0_pagegrain();
  1730. break;
  1731. case KVM_REG_MIPS_CP0_SEGCTL0:
  1732. if (!cpu_guest_has_segments)
  1733. return -EINVAL;
  1734. *v = read_gc0_segctl0();
  1735. break;
  1736. case KVM_REG_MIPS_CP0_SEGCTL1:
  1737. if (!cpu_guest_has_segments)
  1738. return -EINVAL;
  1739. *v = read_gc0_segctl1();
  1740. break;
  1741. case KVM_REG_MIPS_CP0_SEGCTL2:
  1742. if (!cpu_guest_has_segments)
  1743. return -EINVAL;
  1744. *v = read_gc0_segctl2();
  1745. break;
  1746. case KVM_REG_MIPS_CP0_PWBASE:
  1747. if (!cpu_guest_has_htw && !cpu_guest_has_ldpte)
  1748. return -EINVAL;
  1749. *v = read_gc0_pwbase();
  1750. break;
  1751. case KVM_REG_MIPS_CP0_PWFIELD:
  1752. if (!cpu_guest_has_htw && !cpu_guest_has_ldpte)
  1753. return -EINVAL;
  1754. *v = read_gc0_pwfield();
  1755. break;
  1756. case KVM_REG_MIPS_CP0_PWSIZE:
  1757. if (!cpu_guest_has_htw && !cpu_guest_has_ldpte)
  1758. return -EINVAL;
  1759. *v = read_gc0_pwsize();
  1760. break;
  1761. case KVM_REG_MIPS_CP0_WIRED:
  1762. *v = (long)read_gc0_wired();
  1763. break;
  1764. case KVM_REG_MIPS_CP0_PWCTL:
  1765. if (!cpu_guest_has_htw && !cpu_guest_has_ldpte)
  1766. return -EINVAL;
  1767. *v = read_gc0_pwctl();
  1768. break;
  1769. case KVM_REG_MIPS_CP0_HWRENA:
  1770. *v = (long)read_gc0_hwrena();
  1771. break;
  1772. case KVM_REG_MIPS_CP0_BADVADDR:
  1773. *v = (long)read_gc0_badvaddr();
  1774. break;
  1775. case KVM_REG_MIPS_CP0_BADINSTR:
  1776. if (!cpu_guest_has_badinstr)
  1777. return -EINVAL;
  1778. *v = read_gc0_badinstr();
  1779. break;
  1780. case KVM_REG_MIPS_CP0_BADINSTRP:
  1781. if (!cpu_guest_has_badinstrp)
  1782. return -EINVAL;
  1783. *v = read_gc0_badinstrp();
  1784. break;
  1785. case KVM_REG_MIPS_CP0_COUNT:
  1786. *v = kvm_mips_read_count(vcpu);
  1787. break;
  1788. case KVM_REG_MIPS_CP0_ENTRYHI:
  1789. *v = (long)read_gc0_entryhi();
  1790. break;
  1791. case KVM_REG_MIPS_CP0_COMPARE:
  1792. *v = (long)read_gc0_compare();
  1793. break;
  1794. case KVM_REG_MIPS_CP0_STATUS:
  1795. *v = (long)read_gc0_status();
  1796. break;
  1797. case KVM_REG_MIPS_CP0_INTCTL:
  1798. *v = read_gc0_intctl();
  1799. break;
  1800. case KVM_REG_MIPS_CP0_CAUSE:
  1801. *v = (long)read_gc0_cause();
  1802. break;
  1803. case KVM_REG_MIPS_CP0_EPC:
  1804. *v = (long)read_gc0_epc();
  1805. break;
  1806. case KVM_REG_MIPS_CP0_PRID:
  1807. switch (boot_cpu_type()) {
  1808. case CPU_CAVIUM_OCTEON3:
  1809. /* Octeon III has a read-only guest.PRid */
  1810. *v = read_gc0_prid();
  1811. break;
  1812. default:
  1813. *v = (long)kvm_read_c0_guest_prid(cop0);
  1814. break;
  1815. }
  1816. break;
  1817. case KVM_REG_MIPS_CP0_EBASE:
  1818. *v = kvm_vz_read_gc0_ebase();
  1819. break;
  1820. case KVM_REG_MIPS_CP0_CONFIG:
  1821. *v = read_gc0_config();
  1822. break;
  1823. case KVM_REG_MIPS_CP0_CONFIG1:
  1824. if (!cpu_guest_has_conf1)
  1825. return -EINVAL;
  1826. *v = read_gc0_config1();
  1827. break;
  1828. case KVM_REG_MIPS_CP0_CONFIG2:
  1829. if (!cpu_guest_has_conf2)
  1830. return -EINVAL;
  1831. *v = read_gc0_config2();
  1832. break;
  1833. case KVM_REG_MIPS_CP0_CONFIG3:
  1834. if (!cpu_guest_has_conf3)
  1835. return -EINVAL;
  1836. *v = read_gc0_config3();
  1837. break;
  1838. case KVM_REG_MIPS_CP0_CONFIG4:
  1839. if (!cpu_guest_has_conf4)
  1840. return -EINVAL;
  1841. *v = read_gc0_config4();
  1842. break;
  1843. case KVM_REG_MIPS_CP0_CONFIG5:
  1844. if (!cpu_guest_has_conf5)
  1845. return -EINVAL;
  1846. *v = read_gc0_config5();
  1847. break;
  1848. case KVM_REG_MIPS_CP0_CONFIG6:
  1849. *v = kvm_read_sw_gc0_config6(cop0);
  1850. break;
  1851. case KVM_REG_MIPS_CP0_MAAR(0) ... KVM_REG_MIPS_CP0_MAAR(0x3f):
  1852. if (!cpu_guest_has_maar || cpu_guest_has_dyn_maar)
  1853. return -EINVAL;
  1854. idx = reg->id - KVM_REG_MIPS_CP0_MAAR(0);
  1855. if (idx >= ARRAY_SIZE(vcpu->arch.maar))
  1856. return -EINVAL;
  1857. *v = vcpu->arch.maar[idx];
  1858. break;
  1859. case KVM_REG_MIPS_CP0_MAARI:
  1860. if (!cpu_guest_has_maar || cpu_guest_has_dyn_maar)
  1861. return -EINVAL;
  1862. *v = kvm_read_sw_gc0_maari(&vcpu->arch.cop0);
  1863. break;
  1864. #ifdef CONFIG_64BIT
  1865. case KVM_REG_MIPS_CP0_XCONTEXT:
  1866. *v = read_gc0_xcontext();
  1867. break;
  1868. #endif
  1869. case KVM_REG_MIPS_CP0_ERROREPC:
  1870. *v = (long)read_gc0_errorepc();
  1871. break;
  1872. case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
  1873. idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
  1874. if (!cpu_guest_has_kscr(idx))
  1875. return -EINVAL;
  1876. switch (idx) {
  1877. case 2:
  1878. *v = (long)read_gc0_kscratch1();
  1879. break;
  1880. case 3:
  1881. *v = (long)read_gc0_kscratch2();
  1882. break;
  1883. case 4:
  1884. *v = (long)read_gc0_kscratch3();
  1885. break;
  1886. case 5:
  1887. *v = (long)read_gc0_kscratch4();
  1888. break;
  1889. case 6:
  1890. *v = (long)read_gc0_kscratch5();
  1891. break;
  1892. case 7:
  1893. *v = (long)read_gc0_kscratch6();
  1894. break;
  1895. }
  1896. break;
  1897. case KVM_REG_MIPS_COUNT_CTL:
  1898. *v = vcpu->arch.count_ctl;
  1899. break;
  1900. case KVM_REG_MIPS_COUNT_RESUME:
  1901. *v = ktime_to_ns(vcpu->arch.count_resume);
  1902. break;
  1903. case KVM_REG_MIPS_COUNT_HZ:
  1904. *v = vcpu->arch.count_hz;
  1905. break;
  1906. default:
  1907. return -EINVAL;
  1908. }
  1909. return 0;
  1910. }
  1911. static int kvm_vz_set_one_reg(struct kvm_vcpu *vcpu,
  1912. const struct kvm_one_reg *reg,
  1913. s64 v)
  1914. {
  1915. struct mips_coproc *cop0 = &vcpu->arch.cop0;
  1916. unsigned int idx;
  1917. int ret = 0;
  1918. unsigned int cur, change;
  1919. switch (reg->id) {
  1920. case KVM_REG_MIPS_CP0_INDEX:
  1921. write_gc0_index(v);
  1922. break;
  1923. case KVM_REG_MIPS_CP0_ENTRYLO0:
  1924. write_gc0_entrylo0(entrylo_user_to_kvm(v));
  1925. break;
  1926. case KVM_REG_MIPS_CP0_ENTRYLO1:
  1927. write_gc0_entrylo1(entrylo_user_to_kvm(v));
  1928. break;
  1929. case KVM_REG_MIPS_CP0_CONTEXT:
  1930. write_gc0_context(v);
  1931. break;
  1932. case KVM_REG_MIPS_CP0_CONTEXTCONFIG:
  1933. if (!cpu_guest_has_contextconfig)
  1934. return -EINVAL;
  1935. write_gc0_contextconfig(v);
  1936. break;
  1937. case KVM_REG_MIPS_CP0_USERLOCAL:
  1938. if (!cpu_guest_has_userlocal)
  1939. return -EINVAL;
  1940. write_gc0_userlocal(v);
  1941. break;
  1942. #ifdef CONFIG_64BIT
  1943. case KVM_REG_MIPS_CP0_XCONTEXTCONFIG:
  1944. if (!cpu_guest_has_contextconfig)
  1945. return -EINVAL;
  1946. write_gc0_xcontextconfig(v);
  1947. break;
  1948. #endif
  1949. case KVM_REG_MIPS_CP0_PAGEMASK:
  1950. write_gc0_pagemask(v);
  1951. break;
  1952. case KVM_REG_MIPS_CP0_PAGEGRAIN:
  1953. write_gc0_pagegrain(v);
  1954. break;
  1955. case KVM_REG_MIPS_CP0_SEGCTL0:
  1956. if (!cpu_guest_has_segments)
  1957. return -EINVAL;
  1958. write_gc0_segctl0(v);
  1959. break;
  1960. case KVM_REG_MIPS_CP0_SEGCTL1:
  1961. if (!cpu_guest_has_segments)
  1962. return -EINVAL;
  1963. write_gc0_segctl1(v);
  1964. break;
  1965. case KVM_REG_MIPS_CP0_SEGCTL2:
  1966. if (!cpu_guest_has_segments)
  1967. return -EINVAL;
  1968. write_gc0_segctl2(v);
  1969. break;
  1970. case KVM_REG_MIPS_CP0_PWBASE:
  1971. if (!cpu_guest_has_htw && !cpu_guest_has_ldpte)
  1972. return -EINVAL;
  1973. write_gc0_pwbase(v);
  1974. break;
  1975. case KVM_REG_MIPS_CP0_PWFIELD:
  1976. if (!cpu_guest_has_htw && !cpu_guest_has_ldpte)
  1977. return -EINVAL;
  1978. write_gc0_pwfield(v);
  1979. break;
  1980. case KVM_REG_MIPS_CP0_PWSIZE:
  1981. if (!cpu_guest_has_htw && !cpu_guest_has_ldpte)
  1982. return -EINVAL;
  1983. write_gc0_pwsize(v);
  1984. break;
  1985. case KVM_REG_MIPS_CP0_WIRED:
  1986. change_gc0_wired(MIPSR6_WIRED_WIRED, v);
  1987. break;
  1988. case KVM_REG_MIPS_CP0_PWCTL:
  1989. if (!cpu_guest_has_htw && !cpu_guest_has_ldpte)
  1990. return -EINVAL;
  1991. write_gc0_pwctl(v);
  1992. break;
  1993. case KVM_REG_MIPS_CP0_HWRENA:
  1994. write_gc0_hwrena(v);
  1995. break;
  1996. case KVM_REG_MIPS_CP0_BADVADDR:
  1997. write_gc0_badvaddr(v);
  1998. break;
  1999. case KVM_REG_MIPS_CP0_BADINSTR:
  2000. if (!cpu_guest_has_badinstr)
  2001. return -EINVAL;
  2002. write_gc0_badinstr(v);
  2003. break;
  2004. case KVM_REG_MIPS_CP0_BADINSTRP:
  2005. if (!cpu_guest_has_badinstrp)
  2006. return -EINVAL;
  2007. write_gc0_badinstrp(v);
  2008. break;
  2009. case KVM_REG_MIPS_CP0_COUNT:
  2010. kvm_mips_write_count(vcpu, v);
  2011. break;
  2012. case KVM_REG_MIPS_CP0_ENTRYHI:
  2013. write_gc0_entryhi(v);
  2014. break;
  2015. case KVM_REG_MIPS_CP0_COMPARE:
  2016. kvm_mips_write_compare(vcpu, v, false);
  2017. break;
  2018. case KVM_REG_MIPS_CP0_STATUS:
  2019. write_gc0_status(v);
  2020. break;
  2021. case KVM_REG_MIPS_CP0_INTCTL:
  2022. write_gc0_intctl(v);
  2023. break;
  2024. case KVM_REG_MIPS_CP0_CAUSE:
  2025. /*
  2026. * If the timer is stopped or started (DC bit) it must look
  2027. * atomic with changes to the timer interrupt pending bit (TI).
  2028. * A timer interrupt should not happen in between.
  2029. */
  2030. if ((read_gc0_cause() ^ v) & CAUSEF_DC) {
  2031. if (v & CAUSEF_DC) {
  2032. /* disable timer first */
  2033. kvm_mips_count_disable_cause(vcpu);
  2034. change_gc0_cause((u32)~CAUSEF_DC, v);
  2035. } else {
  2036. /* enable timer last */
  2037. change_gc0_cause((u32)~CAUSEF_DC, v);
  2038. kvm_mips_count_enable_cause(vcpu);
  2039. }
  2040. } else {
  2041. write_gc0_cause(v);
  2042. }
  2043. break;
  2044. case KVM_REG_MIPS_CP0_EPC:
  2045. write_gc0_epc(v);
  2046. break;
  2047. case KVM_REG_MIPS_CP0_PRID:
  2048. switch (boot_cpu_type()) {
  2049. case CPU_CAVIUM_OCTEON3:
  2050. /* Octeon III has a guest.PRid, but its read-only */
  2051. break;
  2052. default:
  2053. kvm_write_c0_guest_prid(cop0, v);
  2054. break;
  2055. }
  2056. break;
  2057. case KVM_REG_MIPS_CP0_EBASE:
  2058. kvm_vz_write_gc0_ebase(v);
  2059. break;
  2060. case KVM_REG_MIPS_CP0_CONFIG:
  2061. cur = read_gc0_config();
  2062. change = (cur ^ v) & kvm_vz_config_user_wrmask(vcpu);
  2063. if (change) {
  2064. v = cur ^ change;
  2065. write_gc0_config(v);
  2066. }
  2067. break;
  2068. case KVM_REG_MIPS_CP0_CONFIG1:
  2069. if (!cpu_guest_has_conf1)
  2070. break;
  2071. cur = read_gc0_config1();
  2072. change = (cur ^ v) & kvm_vz_config1_user_wrmask(vcpu);
  2073. if (change) {
  2074. v = cur ^ change;
  2075. write_gc0_config1(v);
  2076. }
  2077. break;
  2078. case KVM_REG_MIPS_CP0_CONFIG2:
  2079. if (!cpu_guest_has_conf2)
  2080. break;
  2081. cur = read_gc0_config2();
  2082. change = (cur ^ v) & kvm_vz_config2_user_wrmask(vcpu);
  2083. if (change) {
  2084. v = cur ^ change;
  2085. write_gc0_config2(v);
  2086. }
  2087. break;
  2088. case KVM_REG_MIPS_CP0_CONFIG3:
  2089. if (!cpu_guest_has_conf3)
  2090. break;
  2091. cur = read_gc0_config3();
  2092. change = (cur ^ v) & kvm_vz_config3_user_wrmask(vcpu);
  2093. if (change) {
  2094. v = cur ^ change;
  2095. write_gc0_config3(v);
  2096. }
  2097. break;
  2098. case KVM_REG_MIPS_CP0_CONFIG4:
  2099. if (!cpu_guest_has_conf4)
  2100. break;
  2101. cur = read_gc0_config4();
  2102. change = (cur ^ v) & kvm_vz_config4_user_wrmask(vcpu);
  2103. if (change) {
  2104. v = cur ^ change;
  2105. write_gc0_config4(v);
  2106. }
  2107. break;
  2108. case KVM_REG_MIPS_CP0_CONFIG5:
  2109. if (!cpu_guest_has_conf5)
  2110. break;
  2111. cur = read_gc0_config5();
  2112. change = (cur ^ v) & kvm_vz_config5_user_wrmask(vcpu);
  2113. if (change) {
  2114. v = cur ^ change;
  2115. write_gc0_config5(v);
  2116. }
  2117. break;
  2118. case KVM_REG_MIPS_CP0_CONFIG6:
  2119. cur = kvm_read_sw_gc0_config6(cop0);
  2120. change = (cur ^ v) & kvm_vz_config6_user_wrmask(vcpu);
  2121. if (change) {
  2122. v = cur ^ change;
  2123. kvm_write_sw_gc0_config6(cop0, (int)v);
  2124. }
  2125. break;
  2126. case KVM_REG_MIPS_CP0_MAAR(0) ... KVM_REG_MIPS_CP0_MAAR(0x3f):
  2127. if (!cpu_guest_has_maar || cpu_guest_has_dyn_maar)
  2128. return -EINVAL;
  2129. idx = reg->id - KVM_REG_MIPS_CP0_MAAR(0);
  2130. if (idx >= ARRAY_SIZE(vcpu->arch.maar))
  2131. return -EINVAL;
  2132. vcpu->arch.maar[idx] = mips_process_maar(dmtc_op, v);
  2133. break;
  2134. case KVM_REG_MIPS_CP0_MAARI:
  2135. if (!cpu_guest_has_maar || cpu_guest_has_dyn_maar)
  2136. return -EINVAL;
  2137. kvm_write_maari(vcpu, v);
  2138. break;
  2139. #ifdef CONFIG_64BIT
  2140. case KVM_REG_MIPS_CP0_XCONTEXT:
  2141. write_gc0_xcontext(v);
  2142. break;
  2143. #endif
  2144. case KVM_REG_MIPS_CP0_ERROREPC:
  2145. write_gc0_errorepc(v);
  2146. break;
  2147. case KVM_REG_MIPS_CP0_KSCRATCH1 ... KVM_REG_MIPS_CP0_KSCRATCH6:
  2148. idx = reg->id - KVM_REG_MIPS_CP0_KSCRATCH1 + 2;
  2149. if (!cpu_guest_has_kscr(idx))
  2150. return -EINVAL;
  2151. switch (idx) {
  2152. case 2:
  2153. write_gc0_kscratch1(v);
  2154. break;
  2155. case 3:
  2156. write_gc0_kscratch2(v);
  2157. break;
  2158. case 4:
  2159. write_gc0_kscratch3(v);
  2160. break;
  2161. case 5:
  2162. write_gc0_kscratch4(v);
  2163. break;
  2164. case 6:
  2165. write_gc0_kscratch5(v);
  2166. break;
  2167. case 7:
  2168. write_gc0_kscratch6(v);
  2169. break;
  2170. }
  2171. break;
  2172. case KVM_REG_MIPS_COUNT_CTL:
  2173. ret = kvm_mips_set_count_ctl(vcpu, v);
  2174. break;
  2175. case KVM_REG_MIPS_COUNT_RESUME:
  2176. ret = kvm_mips_set_count_resume(vcpu, v);
  2177. break;
  2178. case KVM_REG_MIPS_COUNT_HZ:
  2179. ret = kvm_mips_set_count_hz(vcpu, v);
  2180. break;
  2181. default:
  2182. return -EINVAL;
  2183. }
  2184. return ret;
  2185. }
  2186. #define guestid_cache(cpu) (cpu_data[cpu].guestid_cache)
  2187. static void kvm_vz_get_new_guestid(unsigned long cpu, struct kvm_vcpu *vcpu)
  2188. {
  2189. unsigned long guestid = guestid_cache(cpu);
  2190. if (!(++guestid & GUESTID_MASK)) {
  2191. if (cpu_has_vtag_icache)
  2192. flush_icache_all();
  2193. if (!guestid) /* fix version if needed */
  2194. guestid = GUESTID_FIRST_VERSION;
  2195. ++guestid; /* guestid 0 reserved for root */
  2196. /* start new guestid cycle */
  2197. kvm_vz_local_flush_roottlb_all_guests();
  2198. kvm_vz_local_flush_guesttlb_all();
  2199. }
  2200. guestid_cache(cpu) = guestid;
  2201. }
  2202. /* Returns 1 if the guest TLB may be clobbered */
  2203. static int kvm_vz_check_requests(struct kvm_vcpu *vcpu, int cpu)
  2204. {
  2205. int ret = 0;
  2206. int i;
  2207. if (!kvm_request_pending(vcpu))
  2208. return 0;
  2209. if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
  2210. if (cpu_has_guestid) {
  2211. /* Drop all GuestIDs for this VCPU */
  2212. for_each_possible_cpu(i)
  2213. vcpu->arch.vzguestid[i] = 0;
  2214. /* This will clobber guest TLB contents too */
  2215. ret = 1;
  2216. }
  2217. /*
  2218. * For Root ASID Dealias (RAD) we don't do anything here, but we
  2219. * still need the request to ensure we recheck asid_flush_mask.
  2220. * We can still return 0 as only the root TLB will be affected
  2221. * by a root ASID flush.
  2222. */
  2223. }
  2224. return ret;
  2225. }
  2226. static void kvm_vz_vcpu_save_wired(struct kvm_vcpu *vcpu)
  2227. {
  2228. unsigned int wired = read_gc0_wired();
  2229. struct kvm_mips_tlb *tlbs;
  2230. int i;
  2231. /* Expand the wired TLB array if necessary */
  2232. wired &= MIPSR6_WIRED_WIRED;
  2233. if (wired > vcpu->arch.wired_tlb_limit) {
  2234. tlbs = krealloc(vcpu->arch.wired_tlb, wired *
  2235. sizeof(*vcpu->arch.wired_tlb), GFP_ATOMIC);
  2236. if (WARN_ON(!tlbs)) {
  2237. /* Save whatever we can */
  2238. wired = vcpu->arch.wired_tlb_limit;
  2239. } else {
  2240. vcpu->arch.wired_tlb = tlbs;
  2241. vcpu->arch.wired_tlb_limit = wired;
  2242. }
  2243. }
  2244. if (wired)
  2245. /* Save wired entries from the guest TLB */
  2246. kvm_vz_save_guesttlb(vcpu->arch.wired_tlb, 0, wired);
  2247. /* Invalidate any dropped entries since last time */
  2248. for (i = wired; i < vcpu->arch.wired_tlb_used; ++i) {
  2249. vcpu->arch.wired_tlb[i].tlb_hi = UNIQUE_GUEST_ENTRYHI(i);
  2250. vcpu->arch.wired_tlb[i].tlb_lo[0] = 0;
  2251. vcpu->arch.wired_tlb[i].tlb_lo[1] = 0;
  2252. vcpu->arch.wired_tlb[i].tlb_mask = 0;
  2253. }
  2254. vcpu->arch.wired_tlb_used = wired;
  2255. }
  2256. static void kvm_vz_vcpu_load_wired(struct kvm_vcpu *vcpu)
  2257. {
  2258. /* Load wired entries into the guest TLB */
  2259. if (vcpu->arch.wired_tlb)
  2260. kvm_vz_load_guesttlb(vcpu->arch.wired_tlb, 0,
  2261. vcpu->arch.wired_tlb_used);
  2262. }
  2263. static void kvm_vz_vcpu_load_tlb(struct kvm_vcpu *vcpu, int cpu)
  2264. {
  2265. struct kvm *kvm = vcpu->kvm;
  2266. struct mm_struct *gpa_mm = &kvm->arch.gpa_mm;
  2267. bool migrated;
  2268. /*
  2269. * Are we entering guest context on a different CPU to last time?
  2270. * If so, the VCPU's guest TLB state on this CPU may be stale.
  2271. */
  2272. migrated = (vcpu->arch.last_exec_cpu != cpu);
  2273. vcpu->arch.last_exec_cpu = cpu;
  2274. /*
  2275. * A vcpu's GuestID is set in GuestCtl1.ID when the vcpu is loaded and
  2276. * remains set until another vcpu is loaded in. As a rule GuestRID
  2277. * remains zeroed when in root context unless the kernel is busy
  2278. * manipulating guest tlb entries.
  2279. */
  2280. if (cpu_has_guestid) {
  2281. /*
  2282. * Check if our GuestID is of an older version and thus invalid.
  2283. *
  2284. * We also discard the stored GuestID if we've executed on
  2285. * another CPU, as the guest mappings may have changed without
  2286. * hypervisor knowledge.
  2287. */
  2288. if (migrated ||
  2289. (vcpu->arch.vzguestid[cpu] ^ guestid_cache(cpu)) &
  2290. GUESTID_VERSION_MASK) {
  2291. kvm_vz_get_new_guestid(cpu, vcpu);
  2292. vcpu->arch.vzguestid[cpu] = guestid_cache(cpu);
  2293. trace_kvm_guestid_change(vcpu,
  2294. vcpu->arch.vzguestid[cpu]);
  2295. }
  2296. /* Restore GuestID */
  2297. change_c0_guestctl1(GUESTID_MASK, vcpu->arch.vzguestid[cpu]);
  2298. } else {
  2299. /*
  2300. * The Guest TLB only stores a single guest's TLB state, so
  2301. * flush it if another VCPU has executed on this CPU.
  2302. *
  2303. * We also flush if we've executed on another CPU, as the guest
  2304. * mappings may have changed without hypervisor knowledge.
  2305. */
  2306. if (migrated || last_exec_vcpu[cpu] != vcpu)
  2307. kvm_vz_local_flush_guesttlb_all();
  2308. last_exec_vcpu[cpu] = vcpu;
  2309. /*
  2310. * Root ASID dealiases guest GPA mappings in the root TLB.
  2311. * Allocate new root ASID if needed.
  2312. */
  2313. if (cpumask_test_and_clear_cpu(cpu, &kvm->arch.asid_flush_mask))
  2314. get_new_mmu_context(gpa_mm);
  2315. else
  2316. check_mmu_context(gpa_mm);
  2317. }
  2318. }
  2319. static int kvm_vz_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  2320. {
  2321. struct mips_coproc *cop0 = &vcpu->arch.cop0;
  2322. bool migrated, all;
  2323. /*
  2324. * Have we migrated to a different CPU?
  2325. * If so, any old guest TLB state may be stale.
  2326. */
  2327. migrated = (vcpu->arch.last_sched_cpu != cpu);
  2328. /*
  2329. * Was this the last VCPU to run on this CPU?
  2330. * If not, any old guest state from this VCPU will have been clobbered.
  2331. */
  2332. all = migrated || (last_vcpu[cpu] != vcpu);
  2333. last_vcpu[cpu] = vcpu;
  2334. /*
  2335. * Restore CP0_Wired unconditionally as we clear it after use, and
  2336. * restore wired guest TLB entries (while in guest context).
  2337. */
  2338. kvm_restore_gc0_wired(cop0);
  2339. if (current->flags & PF_VCPU) {
  2340. tlbw_use_hazard();
  2341. kvm_vz_vcpu_load_tlb(vcpu, cpu);
  2342. kvm_vz_vcpu_load_wired(vcpu);
  2343. }
  2344. /*
  2345. * Restore timer state regardless, as e.g. Cause.TI can change over time
  2346. * if left unmaintained.
  2347. */
  2348. kvm_vz_restore_timer(vcpu);
  2349. /* Set MC bit if we want to trace guest mode changes */
  2350. if (kvm_trace_guest_mode_change)
  2351. set_c0_guestctl0(MIPS_GCTL0_MC);
  2352. else
  2353. clear_c0_guestctl0(MIPS_GCTL0_MC);
  2354. /* Don't bother restoring registers multiple times unless necessary */
  2355. if (!all)
  2356. return 0;
  2357. /*
  2358. * Restore config registers first, as some implementations restrict
  2359. * writes to other registers when the corresponding feature bits aren't
  2360. * set. For example Status.CU1 cannot be set unless Config1.FP is set.
  2361. */
  2362. kvm_restore_gc0_config(cop0);
  2363. if (cpu_guest_has_conf1)
  2364. kvm_restore_gc0_config1(cop0);
  2365. if (cpu_guest_has_conf2)
  2366. kvm_restore_gc0_config2(cop0);
  2367. if (cpu_guest_has_conf3)
  2368. kvm_restore_gc0_config3(cop0);
  2369. if (cpu_guest_has_conf4)
  2370. kvm_restore_gc0_config4(cop0);
  2371. if (cpu_guest_has_conf5)
  2372. kvm_restore_gc0_config5(cop0);
  2373. if (cpu_guest_has_conf6)
  2374. kvm_restore_gc0_config6(cop0);
  2375. if (cpu_guest_has_conf7)
  2376. kvm_restore_gc0_config7(cop0);
  2377. kvm_restore_gc0_index(cop0);
  2378. kvm_restore_gc0_entrylo0(cop0);
  2379. kvm_restore_gc0_entrylo1(cop0);
  2380. kvm_restore_gc0_context(cop0);
  2381. if (cpu_guest_has_contextconfig)
  2382. kvm_restore_gc0_contextconfig(cop0);
  2383. #ifdef CONFIG_64BIT
  2384. kvm_restore_gc0_xcontext(cop0);
  2385. if (cpu_guest_has_contextconfig)
  2386. kvm_restore_gc0_xcontextconfig(cop0);
  2387. #endif
  2388. kvm_restore_gc0_pagemask(cop0);
  2389. kvm_restore_gc0_pagegrain(cop0);
  2390. kvm_restore_gc0_hwrena(cop0);
  2391. kvm_restore_gc0_badvaddr(cop0);
  2392. kvm_restore_gc0_entryhi(cop0);
  2393. kvm_restore_gc0_status(cop0);
  2394. kvm_restore_gc0_intctl(cop0);
  2395. kvm_restore_gc0_epc(cop0);
  2396. kvm_vz_write_gc0_ebase(kvm_read_sw_gc0_ebase(cop0));
  2397. if (cpu_guest_has_userlocal)
  2398. kvm_restore_gc0_userlocal(cop0);
  2399. kvm_restore_gc0_errorepc(cop0);
  2400. /* restore KScratch registers if enabled in guest */
  2401. if (cpu_guest_has_conf4) {
  2402. if (cpu_guest_has_kscr(2))
  2403. kvm_restore_gc0_kscratch1(cop0);
  2404. if (cpu_guest_has_kscr(3))
  2405. kvm_restore_gc0_kscratch2(cop0);
  2406. if (cpu_guest_has_kscr(4))
  2407. kvm_restore_gc0_kscratch3(cop0);
  2408. if (cpu_guest_has_kscr(5))
  2409. kvm_restore_gc0_kscratch4(cop0);
  2410. if (cpu_guest_has_kscr(6))
  2411. kvm_restore_gc0_kscratch5(cop0);
  2412. if (cpu_guest_has_kscr(7))
  2413. kvm_restore_gc0_kscratch6(cop0);
  2414. }
  2415. if (cpu_guest_has_badinstr)
  2416. kvm_restore_gc0_badinstr(cop0);
  2417. if (cpu_guest_has_badinstrp)
  2418. kvm_restore_gc0_badinstrp(cop0);
  2419. if (cpu_guest_has_segments) {
  2420. kvm_restore_gc0_segctl0(cop0);
  2421. kvm_restore_gc0_segctl1(cop0);
  2422. kvm_restore_gc0_segctl2(cop0);
  2423. }
  2424. /* restore HTW registers */
  2425. if (cpu_guest_has_htw || cpu_guest_has_ldpte) {
  2426. kvm_restore_gc0_pwbase(cop0);
  2427. kvm_restore_gc0_pwfield(cop0);
  2428. kvm_restore_gc0_pwsize(cop0);
  2429. kvm_restore_gc0_pwctl(cop0);
  2430. }
  2431. /* restore Root.GuestCtl2 from unused Guest guestctl2 register */
  2432. if (cpu_has_guestctl2)
  2433. write_c0_guestctl2(
  2434. cop0->reg[MIPS_CP0_GUESTCTL2][MIPS_CP0_GUESTCTL2_SEL]);
  2435. /*
  2436. * We should clear linked load bit to break interrupted atomics. This
  2437. * prevents a SC on the next VCPU from succeeding by matching a LL on
  2438. * the previous VCPU.
  2439. */
  2440. if (vcpu->kvm->created_vcpus > 1)
  2441. write_gc0_lladdr(0);
  2442. return 0;
  2443. }
  2444. static int kvm_vz_vcpu_put(struct kvm_vcpu *vcpu, int cpu)
  2445. {
  2446. struct mips_coproc *cop0 = &vcpu->arch.cop0;
  2447. if (current->flags & PF_VCPU)
  2448. kvm_vz_vcpu_save_wired(vcpu);
  2449. kvm_lose_fpu(vcpu);
  2450. kvm_save_gc0_index(cop0);
  2451. kvm_save_gc0_entrylo0(cop0);
  2452. kvm_save_gc0_entrylo1(cop0);
  2453. kvm_save_gc0_context(cop0);
  2454. if (cpu_guest_has_contextconfig)
  2455. kvm_save_gc0_contextconfig(cop0);
  2456. #ifdef CONFIG_64BIT
  2457. kvm_save_gc0_xcontext(cop0);
  2458. if (cpu_guest_has_contextconfig)
  2459. kvm_save_gc0_xcontextconfig(cop0);
  2460. #endif
  2461. kvm_save_gc0_pagemask(cop0);
  2462. kvm_save_gc0_pagegrain(cop0);
  2463. kvm_save_gc0_wired(cop0);
  2464. /* allow wired TLB entries to be overwritten */
  2465. clear_gc0_wired(MIPSR6_WIRED_WIRED);
  2466. kvm_save_gc0_hwrena(cop0);
  2467. kvm_save_gc0_badvaddr(cop0);
  2468. kvm_save_gc0_entryhi(cop0);
  2469. kvm_save_gc0_status(cop0);
  2470. kvm_save_gc0_intctl(cop0);
  2471. kvm_save_gc0_epc(cop0);
  2472. kvm_write_sw_gc0_ebase(cop0, kvm_vz_read_gc0_ebase());
  2473. if (cpu_guest_has_userlocal)
  2474. kvm_save_gc0_userlocal(cop0);
  2475. /* only save implemented config registers */
  2476. kvm_save_gc0_config(cop0);
  2477. if (cpu_guest_has_conf1)
  2478. kvm_save_gc0_config1(cop0);
  2479. if (cpu_guest_has_conf2)
  2480. kvm_save_gc0_config2(cop0);
  2481. if (cpu_guest_has_conf3)
  2482. kvm_save_gc0_config3(cop0);
  2483. if (cpu_guest_has_conf4)
  2484. kvm_save_gc0_config4(cop0);
  2485. if (cpu_guest_has_conf5)
  2486. kvm_save_gc0_config5(cop0);
  2487. if (cpu_guest_has_conf6)
  2488. kvm_save_gc0_config6(cop0);
  2489. if (cpu_guest_has_conf7)
  2490. kvm_save_gc0_config7(cop0);
  2491. kvm_save_gc0_errorepc(cop0);
  2492. /* save KScratch registers if enabled in guest */
  2493. if (cpu_guest_has_conf4) {
  2494. if (cpu_guest_has_kscr(2))
  2495. kvm_save_gc0_kscratch1(cop0);
  2496. if (cpu_guest_has_kscr(3))
  2497. kvm_save_gc0_kscratch2(cop0);
  2498. if (cpu_guest_has_kscr(4))
  2499. kvm_save_gc0_kscratch3(cop0);
  2500. if (cpu_guest_has_kscr(5))
  2501. kvm_save_gc0_kscratch4(cop0);
  2502. if (cpu_guest_has_kscr(6))
  2503. kvm_save_gc0_kscratch5(cop0);
  2504. if (cpu_guest_has_kscr(7))
  2505. kvm_save_gc0_kscratch6(cop0);
  2506. }
  2507. if (cpu_guest_has_badinstr)
  2508. kvm_save_gc0_badinstr(cop0);
  2509. if (cpu_guest_has_badinstrp)
  2510. kvm_save_gc0_badinstrp(cop0);
  2511. if (cpu_guest_has_segments) {
  2512. kvm_save_gc0_segctl0(cop0);
  2513. kvm_save_gc0_segctl1(cop0);
  2514. kvm_save_gc0_segctl2(cop0);
  2515. }
  2516. /* save HTW registers if enabled in guest */
  2517. if (cpu_guest_has_ldpte || (cpu_guest_has_htw &&
  2518. kvm_read_sw_gc0_config3(cop0) & MIPS_CONF3_PW)) {
  2519. kvm_save_gc0_pwbase(cop0);
  2520. kvm_save_gc0_pwfield(cop0);
  2521. kvm_save_gc0_pwsize(cop0);
  2522. kvm_save_gc0_pwctl(cop0);
  2523. }
  2524. kvm_vz_save_timer(vcpu);
  2525. /* save Root.GuestCtl2 in unused Guest guestctl2 register */
  2526. if (cpu_has_guestctl2)
  2527. cop0->reg[MIPS_CP0_GUESTCTL2][MIPS_CP0_GUESTCTL2_SEL] =
  2528. read_c0_guestctl2();
  2529. return 0;
  2530. }
  2531. /**
  2532. * kvm_vz_resize_guest_vtlb() - Attempt to resize guest VTLB.
  2533. * @size: Number of guest VTLB entries (0 < @size <= root VTLB entries).
  2534. *
  2535. * Attempt to resize the guest VTLB by writing guest Config registers. This is
  2536. * necessary for cores with a shared root/guest TLB to avoid overlap with wired
  2537. * entries in the root VTLB.
  2538. *
  2539. * Returns: The resulting guest VTLB size.
  2540. */
  2541. static unsigned int kvm_vz_resize_guest_vtlb(unsigned int size)
  2542. {
  2543. unsigned int config4 = 0, ret = 0, limit;
  2544. /* Write MMUSize - 1 into guest Config registers */
  2545. if (cpu_guest_has_conf1)
  2546. change_gc0_config1(MIPS_CONF1_TLBS,
  2547. (size - 1) << MIPS_CONF1_TLBS_SHIFT);
  2548. if (cpu_guest_has_conf4) {
  2549. config4 = read_gc0_config4();
  2550. if (cpu_has_mips_r6 || (config4 & MIPS_CONF4_MMUEXTDEF) ==
  2551. MIPS_CONF4_MMUEXTDEF_VTLBSIZEEXT) {
  2552. config4 &= ~MIPS_CONF4_VTLBSIZEEXT;
  2553. config4 |= ((size - 1) >> MIPS_CONF1_TLBS_SIZE) <<
  2554. MIPS_CONF4_VTLBSIZEEXT_SHIFT;
  2555. } else if ((config4 & MIPS_CONF4_MMUEXTDEF) ==
  2556. MIPS_CONF4_MMUEXTDEF_MMUSIZEEXT) {
  2557. config4 &= ~MIPS_CONF4_MMUSIZEEXT;
  2558. config4 |= ((size - 1) >> MIPS_CONF1_TLBS_SIZE) <<
  2559. MIPS_CONF4_MMUSIZEEXT_SHIFT;
  2560. }
  2561. write_gc0_config4(config4);
  2562. }
  2563. /*
  2564. * Set Guest.Wired.Limit = 0 (no limit up to Guest.MMUSize-1), unless it
  2565. * would exceed Root.Wired.Limit (clearing Guest.Wired.Wired so write
  2566. * not dropped)
  2567. */
  2568. if (cpu_has_mips_r6) {
  2569. limit = (read_c0_wired() & MIPSR6_WIRED_LIMIT) >>
  2570. MIPSR6_WIRED_LIMIT_SHIFT;
  2571. if (size - 1 <= limit)
  2572. limit = 0;
  2573. write_gc0_wired(limit << MIPSR6_WIRED_LIMIT_SHIFT);
  2574. }
  2575. /* Read back MMUSize - 1 */
  2576. back_to_back_c0_hazard();
  2577. if (cpu_guest_has_conf1)
  2578. ret = (read_gc0_config1() & MIPS_CONF1_TLBS) >>
  2579. MIPS_CONF1_TLBS_SHIFT;
  2580. if (config4) {
  2581. if (cpu_has_mips_r6 || (config4 & MIPS_CONF4_MMUEXTDEF) ==
  2582. MIPS_CONF4_MMUEXTDEF_VTLBSIZEEXT)
  2583. ret |= ((config4 & MIPS_CONF4_VTLBSIZEEXT) >>
  2584. MIPS_CONF4_VTLBSIZEEXT_SHIFT) <<
  2585. MIPS_CONF1_TLBS_SIZE;
  2586. else if ((config4 & MIPS_CONF4_MMUEXTDEF) ==
  2587. MIPS_CONF4_MMUEXTDEF_MMUSIZEEXT)
  2588. ret |= ((config4 & MIPS_CONF4_MMUSIZEEXT) >>
  2589. MIPS_CONF4_MMUSIZEEXT_SHIFT) <<
  2590. MIPS_CONF1_TLBS_SIZE;
  2591. }
  2592. return ret + 1;
  2593. }
  2594. static int kvm_vz_hardware_enable(void)
  2595. {
  2596. unsigned int mmu_size, guest_mmu_size, ftlb_size;
  2597. u64 guest_cvmctl, cvmvmconfig;
  2598. switch (current_cpu_type()) {
  2599. case CPU_CAVIUM_OCTEON3:
  2600. /* Set up guest timer/perfcount IRQ lines */
  2601. guest_cvmctl = read_gc0_cvmctl();
  2602. guest_cvmctl &= ~CVMCTL_IPTI;
  2603. guest_cvmctl |= 7ull << CVMCTL_IPTI_SHIFT;
  2604. guest_cvmctl &= ~CVMCTL_IPPCI;
  2605. guest_cvmctl |= 6ull << CVMCTL_IPPCI_SHIFT;
  2606. write_gc0_cvmctl(guest_cvmctl);
  2607. cvmvmconfig = read_c0_cvmvmconfig();
  2608. /* No I/O hole translation. */
  2609. cvmvmconfig |= CVMVMCONF_DGHT;
  2610. /* Halve the root MMU size */
  2611. mmu_size = ((cvmvmconfig & CVMVMCONF_MMUSIZEM1)
  2612. >> CVMVMCONF_MMUSIZEM1_S) + 1;
  2613. guest_mmu_size = mmu_size / 2;
  2614. mmu_size -= guest_mmu_size;
  2615. cvmvmconfig &= ~CVMVMCONF_RMMUSIZEM1;
  2616. cvmvmconfig |= mmu_size - 1;
  2617. write_c0_cvmvmconfig(cvmvmconfig);
  2618. /* Update our records */
  2619. current_cpu_data.tlbsize = mmu_size;
  2620. current_cpu_data.tlbsizevtlb = mmu_size;
  2621. current_cpu_data.guest.tlbsize = guest_mmu_size;
  2622. /* Flush moved entries in new (guest) context */
  2623. kvm_vz_local_flush_guesttlb_all();
  2624. break;
  2625. default:
  2626. /*
  2627. * ImgTec cores tend to use a shared root/guest TLB. To avoid
  2628. * overlap of root wired and guest entries, the guest TLB may
  2629. * need resizing.
  2630. */
  2631. mmu_size = current_cpu_data.tlbsizevtlb;
  2632. ftlb_size = current_cpu_data.tlbsize - mmu_size;
  2633. /* Try switching to maximum guest VTLB size for flush */
  2634. guest_mmu_size = kvm_vz_resize_guest_vtlb(mmu_size);
  2635. current_cpu_data.guest.tlbsize = guest_mmu_size + ftlb_size;
  2636. kvm_vz_local_flush_guesttlb_all();
  2637. /*
  2638. * Reduce to make space for root wired entries and at least 2
  2639. * root non-wired entries. This does assume that long-term wired
  2640. * entries won't be added later.
  2641. */
  2642. guest_mmu_size = mmu_size - num_wired_entries() - 2;
  2643. guest_mmu_size = kvm_vz_resize_guest_vtlb(guest_mmu_size);
  2644. current_cpu_data.guest.tlbsize = guest_mmu_size + ftlb_size;
  2645. /*
  2646. * Write the VTLB size, but if another CPU has already written,
  2647. * check it matches or we won't provide a consistent view to the
  2648. * guest. If this ever happens it suggests an asymmetric number
  2649. * of wired entries.
  2650. */
  2651. if (cmpxchg(&kvm_vz_guest_vtlb_size, 0, guest_mmu_size) &&
  2652. WARN(guest_mmu_size != kvm_vz_guest_vtlb_size,
  2653. "Available guest VTLB size mismatch"))
  2654. return -EINVAL;
  2655. break;
  2656. }
  2657. /*
  2658. * Enable virtualization features granting guest direct control of
  2659. * certain features:
  2660. * CP0=1: Guest coprocessor 0 context.
  2661. * AT=Guest: Guest MMU.
  2662. * CG=1: Hit (virtual address) CACHE operations (optional).
  2663. * CF=1: Guest Config registers.
  2664. * CGI=1: Indexed flush CACHE operations (optional).
  2665. */
  2666. write_c0_guestctl0(MIPS_GCTL0_CP0 |
  2667. (MIPS_GCTL0_AT_GUEST << MIPS_GCTL0_AT_SHIFT) |
  2668. MIPS_GCTL0_CG | MIPS_GCTL0_CF);
  2669. if (cpu_has_guestctl0ext) {
  2670. if (current_cpu_type() != CPU_LOONGSON64)
  2671. set_c0_guestctl0ext(MIPS_GCTL0EXT_CGI);
  2672. else
  2673. clear_c0_guestctl0ext(MIPS_GCTL0EXT_CGI);
  2674. }
  2675. if (cpu_has_guestid) {
  2676. write_c0_guestctl1(0);
  2677. kvm_vz_local_flush_roottlb_all_guests();
  2678. GUESTID_MASK = current_cpu_data.guestid_mask;
  2679. GUESTID_FIRST_VERSION = GUESTID_MASK + 1;
  2680. GUESTID_VERSION_MASK = ~GUESTID_MASK;
  2681. current_cpu_data.guestid_cache = GUESTID_FIRST_VERSION;
  2682. }
  2683. /* clear any pending injected virtual guest interrupts */
  2684. if (cpu_has_guestctl2)
  2685. clear_c0_guestctl2(0x3f << 10);
  2686. #ifdef CONFIG_CPU_LOONGSON64
  2687. /* Control guest CCA attribute */
  2688. if (cpu_has_csr())
  2689. csr_writel(csr_readl(0xffffffec) | 0x1, 0xffffffec);
  2690. #endif
  2691. return 0;
  2692. }
  2693. static void kvm_vz_hardware_disable(void)
  2694. {
  2695. u64 cvmvmconfig;
  2696. unsigned int mmu_size;
  2697. /* Flush any remaining guest TLB entries */
  2698. kvm_vz_local_flush_guesttlb_all();
  2699. switch (current_cpu_type()) {
  2700. case CPU_CAVIUM_OCTEON3:
  2701. /*
  2702. * Allocate whole TLB for root. Existing guest TLB entries will
  2703. * change ownership to the root TLB. We should be safe though as
  2704. * they've already been flushed above while in guest TLB.
  2705. */
  2706. cvmvmconfig = read_c0_cvmvmconfig();
  2707. mmu_size = ((cvmvmconfig & CVMVMCONF_MMUSIZEM1)
  2708. >> CVMVMCONF_MMUSIZEM1_S) + 1;
  2709. cvmvmconfig &= ~CVMVMCONF_RMMUSIZEM1;
  2710. cvmvmconfig |= mmu_size - 1;
  2711. write_c0_cvmvmconfig(cvmvmconfig);
  2712. /* Update our records */
  2713. current_cpu_data.tlbsize = mmu_size;
  2714. current_cpu_data.tlbsizevtlb = mmu_size;
  2715. current_cpu_data.guest.tlbsize = 0;
  2716. /* Flush moved entries in new (root) context */
  2717. local_flush_tlb_all();
  2718. break;
  2719. }
  2720. if (cpu_has_guestid) {
  2721. write_c0_guestctl1(0);
  2722. kvm_vz_local_flush_roottlb_all_guests();
  2723. }
  2724. }
  2725. static int kvm_vz_check_extension(struct kvm *kvm, long ext)
  2726. {
  2727. int r;
  2728. switch (ext) {
  2729. case KVM_CAP_MIPS_VZ:
  2730. /* we wouldn't be here unless cpu_has_vz */
  2731. r = 1;
  2732. break;
  2733. #ifdef CONFIG_64BIT
  2734. case KVM_CAP_MIPS_64BIT:
  2735. /* We support 64-bit registers/operations and addresses */
  2736. r = 2;
  2737. break;
  2738. #endif
  2739. case KVM_CAP_IOEVENTFD:
  2740. r = 1;
  2741. break;
  2742. default:
  2743. r = 0;
  2744. break;
  2745. }
  2746. return r;
  2747. }
  2748. static int kvm_vz_vcpu_init(struct kvm_vcpu *vcpu)
  2749. {
  2750. int i;
  2751. for_each_possible_cpu(i)
  2752. vcpu->arch.vzguestid[i] = 0;
  2753. return 0;
  2754. }
  2755. static void kvm_vz_vcpu_uninit(struct kvm_vcpu *vcpu)
  2756. {
  2757. int cpu;
  2758. /*
  2759. * If the VCPU is freed and reused as another VCPU, we don't want the
  2760. * matching pointer wrongly hanging around in last_vcpu[] or
  2761. * last_exec_vcpu[].
  2762. */
  2763. for_each_possible_cpu(cpu) {
  2764. if (last_vcpu[cpu] == vcpu)
  2765. last_vcpu[cpu] = NULL;
  2766. if (last_exec_vcpu[cpu] == vcpu)
  2767. last_exec_vcpu[cpu] = NULL;
  2768. }
  2769. }
  2770. static int kvm_vz_vcpu_setup(struct kvm_vcpu *vcpu)
  2771. {
  2772. struct mips_coproc *cop0 = &vcpu->arch.cop0;
  2773. unsigned long count_hz = 100*1000*1000; /* default to 100 MHz */
  2774. /*
  2775. * Start off the timer at the same frequency as the host timer, but the
  2776. * soft timer doesn't handle frequencies greater than 1GHz yet.
  2777. */
  2778. if (mips_hpt_frequency && mips_hpt_frequency <= NSEC_PER_SEC)
  2779. count_hz = mips_hpt_frequency;
  2780. kvm_mips_init_count(vcpu, count_hz);
  2781. /*
  2782. * Initialize guest register state to valid architectural reset state.
  2783. */
  2784. /* PageGrain */
  2785. if (cpu_has_mips_r5 || cpu_has_mips_r6)
  2786. kvm_write_sw_gc0_pagegrain(cop0, PG_RIE | PG_XIE | PG_IEC);
  2787. /* Wired */
  2788. if (cpu_has_mips_r6)
  2789. kvm_write_sw_gc0_wired(cop0,
  2790. read_gc0_wired() & MIPSR6_WIRED_LIMIT);
  2791. /* Status */
  2792. kvm_write_sw_gc0_status(cop0, ST0_BEV | ST0_ERL);
  2793. if (cpu_has_mips_r5 || cpu_has_mips_r6)
  2794. kvm_change_sw_gc0_status(cop0, ST0_FR, read_gc0_status());
  2795. /* IntCtl */
  2796. kvm_write_sw_gc0_intctl(cop0, read_gc0_intctl() &
  2797. (INTCTLF_IPFDC | INTCTLF_IPPCI | INTCTLF_IPTI));
  2798. /* PRId */
  2799. kvm_write_sw_gc0_prid(cop0, boot_cpu_data.processor_id);
  2800. /* EBase */
  2801. kvm_write_sw_gc0_ebase(cop0, (s32)0x80000000 | vcpu->vcpu_id);
  2802. /* Config */
  2803. kvm_save_gc0_config(cop0);
  2804. /* architecturally writable (e.g. from guest) */
  2805. kvm_change_sw_gc0_config(cop0, CONF_CM_CMASK,
  2806. _page_cachable_default >> _CACHE_SHIFT);
  2807. /* architecturally read only, but maybe writable from root */
  2808. kvm_change_sw_gc0_config(cop0, MIPS_CONF_MT, read_c0_config());
  2809. if (cpu_guest_has_conf1) {
  2810. kvm_set_sw_gc0_config(cop0, MIPS_CONF_M);
  2811. /* Config1 */
  2812. kvm_save_gc0_config1(cop0);
  2813. /* architecturally read only, but maybe writable from root */
  2814. kvm_clear_sw_gc0_config1(cop0, MIPS_CONF1_C2 |
  2815. MIPS_CONF1_MD |
  2816. MIPS_CONF1_PC |
  2817. MIPS_CONF1_WR |
  2818. MIPS_CONF1_CA |
  2819. MIPS_CONF1_FP);
  2820. }
  2821. if (cpu_guest_has_conf2) {
  2822. kvm_set_sw_gc0_config1(cop0, MIPS_CONF_M);
  2823. /* Config2 */
  2824. kvm_save_gc0_config2(cop0);
  2825. }
  2826. if (cpu_guest_has_conf3) {
  2827. kvm_set_sw_gc0_config2(cop0, MIPS_CONF_M);
  2828. /* Config3 */
  2829. kvm_save_gc0_config3(cop0);
  2830. /* architecturally writable (e.g. from guest) */
  2831. kvm_clear_sw_gc0_config3(cop0, MIPS_CONF3_ISA_OE);
  2832. /* architecturally read only, but maybe writable from root */
  2833. kvm_clear_sw_gc0_config3(cop0, MIPS_CONF3_MSA |
  2834. MIPS_CONF3_BPG |
  2835. MIPS_CONF3_ULRI |
  2836. MIPS_CONF3_DSP |
  2837. MIPS_CONF3_CTXTC |
  2838. MIPS_CONF3_ITL |
  2839. MIPS_CONF3_LPA |
  2840. MIPS_CONF3_VEIC |
  2841. MIPS_CONF3_VINT |
  2842. MIPS_CONF3_SP |
  2843. MIPS_CONF3_CDMM |
  2844. MIPS_CONF3_MT |
  2845. MIPS_CONF3_SM |
  2846. MIPS_CONF3_TL);
  2847. }
  2848. if (cpu_guest_has_conf4) {
  2849. kvm_set_sw_gc0_config3(cop0, MIPS_CONF_M);
  2850. /* Config4 */
  2851. kvm_save_gc0_config4(cop0);
  2852. }
  2853. if (cpu_guest_has_conf5) {
  2854. kvm_set_sw_gc0_config4(cop0, MIPS_CONF_M);
  2855. /* Config5 */
  2856. kvm_save_gc0_config5(cop0);
  2857. /* architecturally writable (e.g. from guest) */
  2858. kvm_clear_sw_gc0_config5(cop0, MIPS_CONF5_K |
  2859. MIPS_CONF5_CV |
  2860. MIPS_CONF5_MSAEN |
  2861. MIPS_CONF5_UFE |
  2862. MIPS_CONF5_FRE |
  2863. MIPS_CONF5_SBRI |
  2864. MIPS_CONF5_UFR);
  2865. /* architecturally read only, but maybe writable from root */
  2866. kvm_clear_sw_gc0_config5(cop0, MIPS_CONF5_MRP);
  2867. }
  2868. if (cpu_guest_has_contextconfig) {
  2869. /* ContextConfig */
  2870. kvm_write_sw_gc0_contextconfig(cop0, 0x007ffff0);
  2871. #ifdef CONFIG_64BIT
  2872. /* XContextConfig */
  2873. /* bits SEGBITS-13+3:4 set */
  2874. kvm_write_sw_gc0_xcontextconfig(cop0,
  2875. ((1ull << (cpu_vmbits - 13)) - 1) << 4);
  2876. #endif
  2877. }
  2878. /* Implementation dependent, use the legacy layout */
  2879. if (cpu_guest_has_segments) {
  2880. /* SegCtl0, SegCtl1, SegCtl2 */
  2881. kvm_write_sw_gc0_segctl0(cop0, 0x00200010);
  2882. kvm_write_sw_gc0_segctl1(cop0, 0x00000002 |
  2883. (_page_cachable_default >> _CACHE_SHIFT) <<
  2884. (16 + MIPS_SEGCFG_C_SHIFT));
  2885. kvm_write_sw_gc0_segctl2(cop0, 0x00380438);
  2886. }
  2887. /* reset HTW registers */
  2888. if (cpu_guest_has_htw && (cpu_has_mips_r5 || cpu_has_mips_r6)) {
  2889. /* PWField */
  2890. kvm_write_sw_gc0_pwfield(cop0, 0x0c30c302);
  2891. /* PWSize */
  2892. kvm_write_sw_gc0_pwsize(cop0, 1 << MIPS_PWSIZE_PTW_SHIFT);
  2893. }
  2894. /* start with no pending virtual guest interrupts */
  2895. if (cpu_has_guestctl2)
  2896. cop0->reg[MIPS_CP0_GUESTCTL2][MIPS_CP0_GUESTCTL2_SEL] = 0;
  2897. /* Put PC at reset vector */
  2898. vcpu->arch.pc = CKSEG1ADDR(0x1fc00000);
  2899. return 0;
  2900. }
  2901. static void kvm_vz_prepare_flush_shadow(struct kvm *kvm)
  2902. {
  2903. if (!cpu_has_guestid) {
  2904. /*
  2905. * For each CPU there is a single GPA ASID used by all VCPUs in
  2906. * the VM, so it doesn't make sense for the VCPUs to handle
  2907. * invalidation of these ASIDs individually.
  2908. *
  2909. * Instead mark all CPUs as needing ASID invalidation in
  2910. * asid_flush_mask, and kvm_flush_remote_tlbs(kvm) will
  2911. * kick any running VCPUs so they check asid_flush_mask.
  2912. */
  2913. cpumask_setall(&kvm->arch.asid_flush_mask);
  2914. }
  2915. }
  2916. static void kvm_vz_vcpu_reenter(struct kvm_vcpu *vcpu)
  2917. {
  2918. int cpu = smp_processor_id();
  2919. int preserve_guest_tlb;
  2920. preserve_guest_tlb = kvm_vz_check_requests(vcpu, cpu);
  2921. if (preserve_guest_tlb)
  2922. kvm_vz_vcpu_save_wired(vcpu);
  2923. kvm_vz_vcpu_load_tlb(vcpu, cpu);
  2924. if (preserve_guest_tlb)
  2925. kvm_vz_vcpu_load_wired(vcpu);
  2926. }
  2927. static int kvm_vz_vcpu_run(struct kvm_vcpu *vcpu)
  2928. {
  2929. int cpu = smp_processor_id();
  2930. int r;
  2931. kvm_vz_acquire_htimer(vcpu);
  2932. /* Check if we have any exceptions/interrupts pending */
  2933. kvm_mips_deliver_interrupts(vcpu, read_gc0_cause());
  2934. kvm_vz_check_requests(vcpu, cpu);
  2935. kvm_vz_vcpu_load_tlb(vcpu, cpu);
  2936. kvm_vz_vcpu_load_wired(vcpu);
  2937. r = vcpu->arch.vcpu_run(vcpu);
  2938. kvm_vz_vcpu_save_wired(vcpu);
  2939. return r;
  2940. }
  2941. static struct kvm_mips_callbacks kvm_vz_callbacks = {
  2942. .handle_cop_unusable = kvm_trap_vz_handle_cop_unusable,
  2943. .handle_tlb_mod = kvm_trap_vz_handle_tlb_st_miss,
  2944. .handle_tlb_ld_miss = kvm_trap_vz_handle_tlb_ld_miss,
  2945. .handle_tlb_st_miss = kvm_trap_vz_handle_tlb_st_miss,
  2946. .handle_addr_err_st = kvm_trap_vz_no_handler,
  2947. .handle_addr_err_ld = kvm_trap_vz_no_handler,
  2948. .handle_syscall = kvm_trap_vz_no_handler,
  2949. .handle_res_inst = kvm_trap_vz_no_handler,
  2950. .handle_break = kvm_trap_vz_no_handler,
  2951. .handle_msa_disabled = kvm_trap_vz_handle_msa_disabled,
  2952. .handle_guest_exit = kvm_trap_vz_handle_guest_exit,
  2953. .hardware_enable = kvm_vz_hardware_enable,
  2954. .hardware_disable = kvm_vz_hardware_disable,
  2955. .check_extension = kvm_vz_check_extension,
  2956. .vcpu_init = kvm_vz_vcpu_init,
  2957. .vcpu_uninit = kvm_vz_vcpu_uninit,
  2958. .vcpu_setup = kvm_vz_vcpu_setup,
  2959. .prepare_flush_shadow = kvm_vz_prepare_flush_shadow,
  2960. .gva_to_gpa = kvm_vz_gva_to_gpa_cb,
  2961. .queue_timer_int = kvm_vz_queue_timer_int_cb,
  2962. .dequeue_timer_int = kvm_vz_dequeue_timer_int_cb,
  2963. .queue_io_int = kvm_vz_queue_io_int_cb,
  2964. .dequeue_io_int = kvm_vz_dequeue_io_int_cb,
  2965. .irq_deliver = kvm_vz_irq_deliver_cb,
  2966. .irq_clear = kvm_vz_irq_clear_cb,
  2967. .num_regs = kvm_vz_num_regs,
  2968. .copy_reg_indices = kvm_vz_copy_reg_indices,
  2969. .get_one_reg = kvm_vz_get_one_reg,
  2970. .set_one_reg = kvm_vz_set_one_reg,
  2971. .vcpu_load = kvm_vz_vcpu_load,
  2972. .vcpu_put = kvm_vz_vcpu_put,
  2973. .vcpu_run = kvm_vz_vcpu_run,
  2974. .vcpu_reenter = kvm_vz_vcpu_reenter,
  2975. };
  2976. int kvm_mips_emulation_init(struct kvm_mips_callbacks **install_callbacks)
  2977. {
  2978. if (!cpu_has_vz)
  2979. return -ENODEV;
  2980. /*
  2981. * VZ requires at least 2 KScratch registers, so it should have been
  2982. * possible to allocate pgd_reg.
  2983. */
  2984. if (WARN(pgd_reg == -1,
  2985. "pgd_reg not allocated even though cpu_has_vz\n"))
  2986. return -ENODEV;
  2987. pr_info("Starting KVM with MIPS VZ extensions\n");
  2988. *install_callbacks = &kvm_vz_callbacks;
  2989. return 0;
  2990. }