vz.c 82 KB

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