svm.c 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * AMD SVM support
  5. *
  6. * Copyright (C) 2006 Qumranet, Inc.
  7. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  8. *
  9. * Authors:
  10. * Yaniv Kamay <yaniv@qumranet.com>
  11. * Avi Kivity <avi@qumranet.com>
  12. *
  13. * This work is licensed under the terms of the GNU GPL, version 2. See
  14. * the COPYING file in the top-level directory.
  15. *
  16. */
  17. #include <linux/kvm_host.h>
  18. #include "irq.h"
  19. #include "mmu.h"
  20. #include "kvm_cache_regs.h"
  21. #include "x86.h"
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/highmem.h>
  26. #include <linux/sched.h>
  27. #include <linux/ftrace_event.h>
  28. #include <linux/slab.h>
  29. #include <asm/tlbflush.h>
  30. #include <asm/desc.h>
  31. #include <asm/kvm_para.h>
  32. #include <asm/virtext.h>
  33. #include "trace.h"
  34. #define __ex(x) __kvm_handle_fault_on_reboot(x)
  35. MODULE_AUTHOR("Qumranet");
  36. MODULE_LICENSE("GPL");
  37. #define IOPM_ALLOC_ORDER 2
  38. #define MSRPM_ALLOC_ORDER 1
  39. #define SEG_TYPE_LDT 2
  40. #define SEG_TYPE_BUSY_TSS16 3
  41. #define SVM_FEATURE_NPT (1 << 0)
  42. #define SVM_FEATURE_LBRV (1 << 1)
  43. #define SVM_FEATURE_SVML (1 << 2)
  44. #define SVM_FEATURE_NRIP (1 << 3)
  45. #define SVM_FEATURE_PAUSE_FILTER (1 << 10)
  46. #define NESTED_EXIT_HOST 0 /* Exit handled on host level */
  47. #define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
  48. #define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
  49. #define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
  50. static bool erratum_383_found __read_mostly;
  51. static const u32 host_save_user_msrs[] = {
  52. #ifdef CONFIG_X86_64
  53. MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
  54. MSR_FS_BASE,
  55. #endif
  56. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  57. };
  58. #define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
  59. struct kvm_vcpu;
  60. struct nested_state {
  61. struct vmcb *hsave;
  62. u64 hsave_msr;
  63. u64 vm_cr_msr;
  64. u64 vmcb;
  65. /* These are the merged vectors */
  66. u32 *msrpm;
  67. /* gpa pointers to the real vectors */
  68. u64 vmcb_msrpm;
  69. u64 vmcb_iopm;
  70. /* A VMEXIT is required but not yet emulated */
  71. bool exit_required;
  72. /*
  73. * If we vmexit during an instruction emulation we need this to restore
  74. * the l1 guest rip after the emulation
  75. */
  76. unsigned long vmexit_rip;
  77. unsigned long vmexit_rsp;
  78. unsigned long vmexit_rax;
  79. /* cache for intercepts of the guest */
  80. u32 intercept_cr;
  81. u32 intercept_dr;
  82. u32 intercept_exceptions;
  83. u64 intercept;
  84. /* Nested Paging related state */
  85. u64 nested_cr3;
  86. };
  87. #define MSRPM_OFFSETS 16
  88. static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
  89. struct vcpu_svm {
  90. struct kvm_vcpu vcpu;
  91. struct vmcb *vmcb;
  92. unsigned long vmcb_pa;
  93. struct svm_cpu_data *svm_data;
  94. uint64_t asid_generation;
  95. uint64_t sysenter_esp;
  96. uint64_t sysenter_eip;
  97. u64 next_rip;
  98. u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
  99. struct {
  100. u16 fs;
  101. u16 gs;
  102. u16 ldt;
  103. u64 gs_base;
  104. } host;
  105. u32 *msrpm;
  106. struct nested_state nested;
  107. bool nmi_singlestep;
  108. unsigned int3_injected;
  109. unsigned long int3_rip;
  110. u32 apf_reason;
  111. };
  112. #define MSR_INVALID 0xffffffffU
  113. static struct svm_direct_access_msrs {
  114. u32 index; /* Index of the MSR */
  115. bool always; /* True if intercept is always on */
  116. } direct_access_msrs[] = {
  117. { .index = MSR_STAR, .always = true },
  118. { .index = MSR_IA32_SYSENTER_CS, .always = true },
  119. #ifdef CONFIG_X86_64
  120. { .index = MSR_GS_BASE, .always = true },
  121. { .index = MSR_FS_BASE, .always = true },
  122. { .index = MSR_KERNEL_GS_BASE, .always = true },
  123. { .index = MSR_LSTAR, .always = true },
  124. { .index = MSR_CSTAR, .always = true },
  125. { .index = MSR_SYSCALL_MASK, .always = true },
  126. #endif
  127. { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
  128. { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
  129. { .index = MSR_IA32_LASTINTFROMIP, .always = false },
  130. { .index = MSR_IA32_LASTINTTOIP, .always = false },
  131. { .index = MSR_INVALID, .always = false },
  132. };
  133. /* enable NPT for AMD64 and X86 with PAE */
  134. #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
  135. static bool npt_enabled = true;
  136. #else
  137. static bool npt_enabled;
  138. #endif
  139. static int npt = 1;
  140. module_param(npt, int, S_IRUGO);
  141. static int nested = 1;
  142. module_param(nested, int, S_IRUGO);
  143. static void svm_flush_tlb(struct kvm_vcpu *vcpu);
  144. static void svm_complete_interrupts(struct vcpu_svm *svm);
  145. static int nested_svm_exit_handled(struct vcpu_svm *svm);
  146. static int nested_svm_intercept(struct vcpu_svm *svm);
  147. static int nested_svm_vmexit(struct vcpu_svm *svm);
  148. static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
  149. bool has_error_code, u32 error_code);
  150. enum {
  151. VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
  152. pause filter count */
  153. VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
  154. VMCB_ASID, /* ASID */
  155. VMCB_INTR, /* int_ctl, int_vector */
  156. VMCB_DIRTY_MAX,
  157. };
  158. /* TPR is always written before VMRUN */
  159. #define VMCB_ALWAYS_DIRTY_MASK (1U << VMCB_INTR)
  160. static inline void mark_all_dirty(struct vmcb *vmcb)
  161. {
  162. vmcb->control.clean = 0;
  163. }
  164. static inline void mark_all_clean(struct vmcb *vmcb)
  165. {
  166. vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
  167. & ~VMCB_ALWAYS_DIRTY_MASK;
  168. }
  169. static inline void mark_dirty(struct vmcb *vmcb, int bit)
  170. {
  171. vmcb->control.clean &= ~(1 << bit);
  172. }
  173. static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
  174. {
  175. return container_of(vcpu, struct vcpu_svm, vcpu);
  176. }
  177. static void recalc_intercepts(struct vcpu_svm *svm)
  178. {
  179. struct vmcb_control_area *c, *h;
  180. struct nested_state *g;
  181. mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
  182. if (!is_guest_mode(&svm->vcpu))
  183. return;
  184. c = &svm->vmcb->control;
  185. h = &svm->nested.hsave->control;
  186. g = &svm->nested;
  187. c->intercept_cr = h->intercept_cr | g->intercept_cr;
  188. c->intercept_dr = h->intercept_dr | g->intercept_dr;
  189. c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
  190. c->intercept = h->intercept | g->intercept;
  191. }
  192. static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
  193. {
  194. if (is_guest_mode(&svm->vcpu))
  195. return svm->nested.hsave;
  196. else
  197. return svm->vmcb;
  198. }
  199. static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
  200. {
  201. struct vmcb *vmcb = get_host_vmcb(svm);
  202. vmcb->control.intercept_cr |= (1U << bit);
  203. recalc_intercepts(svm);
  204. }
  205. static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
  206. {
  207. struct vmcb *vmcb = get_host_vmcb(svm);
  208. vmcb->control.intercept_cr &= ~(1U << bit);
  209. recalc_intercepts(svm);
  210. }
  211. static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
  212. {
  213. struct vmcb *vmcb = get_host_vmcb(svm);
  214. return vmcb->control.intercept_cr & (1U << bit);
  215. }
  216. static inline void set_dr_intercept(struct vcpu_svm *svm, int bit)
  217. {
  218. struct vmcb *vmcb = get_host_vmcb(svm);
  219. vmcb->control.intercept_dr |= (1U << bit);
  220. recalc_intercepts(svm);
  221. }
  222. static inline void clr_dr_intercept(struct vcpu_svm *svm, int bit)
  223. {
  224. struct vmcb *vmcb = get_host_vmcb(svm);
  225. vmcb->control.intercept_dr &= ~(1U << bit);
  226. recalc_intercepts(svm);
  227. }
  228. static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
  229. {
  230. struct vmcb *vmcb = get_host_vmcb(svm);
  231. vmcb->control.intercept_exceptions |= (1U << bit);
  232. recalc_intercepts(svm);
  233. }
  234. static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
  235. {
  236. struct vmcb *vmcb = get_host_vmcb(svm);
  237. vmcb->control.intercept_exceptions &= ~(1U << bit);
  238. recalc_intercepts(svm);
  239. }
  240. static inline void set_intercept(struct vcpu_svm *svm, int bit)
  241. {
  242. struct vmcb *vmcb = get_host_vmcb(svm);
  243. vmcb->control.intercept |= (1ULL << bit);
  244. recalc_intercepts(svm);
  245. }
  246. static inline void clr_intercept(struct vcpu_svm *svm, int bit)
  247. {
  248. struct vmcb *vmcb = get_host_vmcb(svm);
  249. vmcb->control.intercept &= ~(1ULL << bit);
  250. recalc_intercepts(svm);
  251. }
  252. static inline void enable_gif(struct vcpu_svm *svm)
  253. {
  254. svm->vcpu.arch.hflags |= HF_GIF_MASK;
  255. }
  256. static inline void disable_gif(struct vcpu_svm *svm)
  257. {
  258. svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
  259. }
  260. static inline bool gif_set(struct vcpu_svm *svm)
  261. {
  262. return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
  263. }
  264. static unsigned long iopm_base;
  265. struct kvm_ldttss_desc {
  266. u16 limit0;
  267. u16 base0;
  268. unsigned base1:8, type:5, dpl:2, p:1;
  269. unsigned limit1:4, zero0:3, g:1, base2:8;
  270. u32 base3;
  271. u32 zero1;
  272. } __attribute__((packed));
  273. struct svm_cpu_data {
  274. int cpu;
  275. u64 asid_generation;
  276. u32 max_asid;
  277. u32 next_asid;
  278. struct kvm_ldttss_desc *tss_desc;
  279. struct page *save_area;
  280. };
  281. static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
  282. static uint32_t svm_features;
  283. struct svm_init_data {
  284. int cpu;
  285. int r;
  286. };
  287. static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
  288. #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
  289. #define MSRS_RANGE_SIZE 2048
  290. #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
  291. static u32 svm_msrpm_offset(u32 msr)
  292. {
  293. u32 offset;
  294. int i;
  295. for (i = 0; i < NUM_MSR_MAPS; i++) {
  296. if (msr < msrpm_ranges[i] ||
  297. msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
  298. continue;
  299. offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
  300. offset += (i * MSRS_RANGE_SIZE); /* add range offset */
  301. /* Now we have the u8 offset - but need the u32 offset */
  302. return offset / 4;
  303. }
  304. /* MSR not in any range */
  305. return MSR_INVALID;
  306. }
  307. #define MAX_INST_SIZE 15
  308. static inline void clgi(void)
  309. {
  310. asm volatile (__ex(SVM_CLGI));
  311. }
  312. static inline void stgi(void)
  313. {
  314. asm volatile (__ex(SVM_STGI));
  315. }
  316. static inline void invlpga(unsigned long addr, u32 asid)
  317. {
  318. asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
  319. }
  320. static inline void force_new_asid(struct kvm_vcpu *vcpu)
  321. {
  322. to_svm(vcpu)->asid_generation--;
  323. }
  324. static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
  325. {
  326. force_new_asid(vcpu);
  327. }
  328. static int get_npt_level(void)
  329. {
  330. #ifdef CONFIG_X86_64
  331. return PT64_ROOT_LEVEL;
  332. #else
  333. return PT32E_ROOT_LEVEL;
  334. #endif
  335. }
  336. static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  337. {
  338. vcpu->arch.efer = efer;
  339. if (!npt_enabled && !(efer & EFER_LMA))
  340. efer &= ~EFER_LME;
  341. to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
  342. }
  343. static int is_external_interrupt(u32 info)
  344. {
  345. info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
  346. return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
  347. }
  348. static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  349. {
  350. struct vcpu_svm *svm = to_svm(vcpu);
  351. u32 ret = 0;
  352. if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
  353. ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
  354. return ret & mask;
  355. }
  356. static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  357. {
  358. struct vcpu_svm *svm = to_svm(vcpu);
  359. if (mask == 0)
  360. svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
  361. else
  362. svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
  363. }
  364. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  365. {
  366. struct vcpu_svm *svm = to_svm(vcpu);
  367. if (svm->vmcb->control.next_rip != 0)
  368. svm->next_rip = svm->vmcb->control.next_rip;
  369. if (!svm->next_rip) {
  370. if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) !=
  371. EMULATE_DONE)
  372. printk(KERN_DEBUG "%s: NOP\n", __func__);
  373. return;
  374. }
  375. if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
  376. printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
  377. __func__, kvm_rip_read(vcpu), svm->next_rip);
  378. kvm_rip_write(vcpu, svm->next_rip);
  379. svm_set_interrupt_shadow(vcpu, 0);
  380. }
  381. static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
  382. bool has_error_code, u32 error_code,
  383. bool reinject)
  384. {
  385. struct vcpu_svm *svm = to_svm(vcpu);
  386. /*
  387. * If we are within a nested VM we'd better #VMEXIT and let the guest
  388. * handle the exception
  389. */
  390. if (!reinject &&
  391. nested_svm_check_exception(svm, nr, has_error_code, error_code))
  392. return;
  393. if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
  394. unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
  395. /*
  396. * For guest debugging where we have to reinject #BP if some
  397. * INT3 is guest-owned:
  398. * Emulate nRIP by moving RIP forward. Will fail if injection
  399. * raises a fault that is not intercepted. Still better than
  400. * failing in all cases.
  401. */
  402. skip_emulated_instruction(&svm->vcpu);
  403. rip = kvm_rip_read(&svm->vcpu);
  404. svm->int3_rip = rip + svm->vmcb->save.cs.base;
  405. svm->int3_injected = rip - old_rip;
  406. }
  407. svm->vmcb->control.event_inj = nr
  408. | SVM_EVTINJ_VALID
  409. | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
  410. | SVM_EVTINJ_TYPE_EXEPT;
  411. svm->vmcb->control.event_inj_err = error_code;
  412. }
  413. static void svm_init_erratum_383(void)
  414. {
  415. u32 low, high;
  416. int err;
  417. u64 val;
  418. if (!cpu_has_amd_erratum(amd_erratum_383))
  419. return;
  420. /* Use _safe variants to not break nested virtualization */
  421. val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
  422. if (err)
  423. return;
  424. val |= (1ULL << 47);
  425. low = lower_32_bits(val);
  426. high = upper_32_bits(val);
  427. native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
  428. erratum_383_found = true;
  429. }
  430. static int has_svm(void)
  431. {
  432. const char *msg;
  433. if (!cpu_has_svm(&msg)) {
  434. printk(KERN_INFO "has_svm: %s\n", msg);
  435. return 0;
  436. }
  437. return 1;
  438. }
  439. static void svm_hardware_disable(void *garbage)
  440. {
  441. cpu_svm_disable();
  442. }
  443. static int svm_hardware_enable(void *garbage)
  444. {
  445. struct svm_cpu_data *sd;
  446. uint64_t efer;
  447. struct desc_ptr gdt_descr;
  448. struct desc_struct *gdt;
  449. int me = raw_smp_processor_id();
  450. rdmsrl(MSR_EFER, efer);
  451. if (efer & EFER_SVME)
  452. return -EBUSY;
  453. if (!has_svm()) {
  454. printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
  455. me);
  456. return -EINVAL;
  457. }
  458. sd = per_cpu(svm_data, me);
  459. if (!sd) {
  460. printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
  461. me);
  462. return -EINVAL;
  463. }
  464. sd->asid_generation = 1;
  465. sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
  466. sd->next_asid = sd->max_asid + 1;
  467. native_store_gdt(&gdt_descr);
  468. gdt = (struct desc_struct *)gdt_descr.address;
  469. sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
  470. wrmsrl(MSR_EFER, efer | EFER_SVME);
  471. wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
  472. svm_init_erratum_383();
  473. return 0;
  474. }
  475. static void svm_cpu_uninit(int cpu)
  476. {
  477. struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
  478. if (!sd)
  479. return;
  480. per_cpu(svm_data, raw_smp_processor_id()) = NULL;
  481. __free_page(sd->save_area);
  482. kfree(sd);
  483. }
  484. static int svm_cpu_init(int cpu)
  485. {
  486. struct svm_cpu_data *sd;
  487. int r;
  488. sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
  489. if (!sd)
  490. return -ENOMEM;
  491. sd->cpu = cpu;
  492. sd->save_area = alloc_page(GFP_KERNEL);
  493. r = -ENOMEM;
  494. if (!sd->save_area)
  495. goto err_1;
  496. per_cpu(svm_data, cpu) = sd;
  497. return 0;
  498. err_1:
  499. kfree(sd);
  500. return r;
  501. }
  502. static bool valid_msr_intercept(u32 index)
  503. {
  504. int i;
  505. for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
  506. if (direct_access_msrs[i].index == index)
  507. return true;
  508. return false;
  509. }
  510. static void set_msr_interception(u32 *msrpm, unsigned msr,
  511. int read, int write)
  512. {
  513. u8 bit_read, bit_write;
  514. unsigned long tmp;
  515. u32 offset;
  516. /*
  517. * If this warning triggers extend the direct_access_msrs list at the
  518. * beginning of the file
  519. */
  520. WARN_ON(!valid_msr_intercept(msr));
  521. offset = svm_msrpm_offset(msr);
  522. bit_read = 2 * (msr & 0x0f);
  523. bit_write = 2 * (msr & 0x0f) + 1;
  524. tmp = msrpm[offset];
  525. BUG_ON(offset == MSR_INVALID);
  526. read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
  527. write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
  528. msrpm[offset] = tmp;
  529. }
  530. static void svm_vcpu_init_msrpm(u32 *msrpm)
  531. {
  532. int i;
  533. memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
  534. for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
  535. if (!direct_access_msrs[i].always)
  536. continue;
  537. set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
  538. }
  539. }
  540. static void add_msr_offset(u32 offset)
  541. {
  542. int i;
  543. for (i = 0; i < MSRPM_OFFSETS; ++i) {
  544. /* Offset already in list? */
  545. if (msrpm_offsets[i] == offset)
  546. return;
  547. /* Slot used by another offset? */
  548. if (msrpm_offsets[i] != MSR_INVALID)
  549. continue;
  550. /* Add offset to list */
  551. msrpm_offsets[i] = offset;
  552. return;
  553. }
  554. /*
  555. * If this BUG triggers the msrpm_offsets table has an overflow. Just
  556. * increase MSRPM_OFFSETS in this case.
  557. */
  558. BUG();
  559. }
  560. static void init_msrpm_offsets(void)
  561. {
  562. int i;
  563. memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
  564. for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
  565. u32 offset;
  566. offset = svm_msrpm_offset(direct_access_msrs[i].index);
  567. BUG_ON(offset == MSR_INVALID);
  568. add_msr_offset(offset);
  569. }
  570. }
  571. static void svm_enable_lbrv(struct vcpu_svm *svm)
  572. {
  573. u32 *msrpm = svm->msrpm;
  574. svm->vmcb->control.lbr_ctl = 1;
  575. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
  576. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
  577. set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
  578. set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
  579. }
  580. static void svm_disable_lbrv(struct vcpu_svm *svm)
  581. {
  582. u32 *msrpm = svm->msrpm;
  583. svm->vmcb->control.lbr_ctl = 0;
  584. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
  585. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
  586. set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
  587. set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
  588. }
  589. static __init int svm_hardware_setup(void)
  590. {
  591. int cpu;
  592. struct page *iopm_pages;
  593. void *iopm_va;
  594. int r;
  595. iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
  596. if (!iopm_pages)
  597. return -ENOMEM;
  598. iopm_va = page_address(iopm_pages);
  599. memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
  600. iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
  601. init_msrpm_offsets();
  602. if (boot_cpu_has(X86_FEATURE_NX))
  603. kvm_enable_efer_bits(EFER_NX);
  604. if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
  605. kvm_enable_efer_bits(EFER_FFXSR);
  606. if (nested) {
  607. printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
  608. kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
  609. }
  610. for_each_possible_cpu(cpu) {
  611. r = svm_cpu_init(cpu);
  612. if (r)
  613. goto err;
  614. }
  615. svm_features = cpuid_edx(SVM_CPUID_FUNC);
  616. if (!boot_cpu_has(X86_FEATURE_NPT))
  617. npt_enabled = false;
  618. if (npt_enabled && !npt) {
  619. printk(KERN_INFO "kvm: Nested Paging disabled\n");
  620. npt_enabled = false;
  621. }
  622. if (npt_enabled) {
  623. printk(KERN_INFO "kvm: Nested Paging enabled\n");
  624. kvm_enable_tdp();
  625. } else
  626. kvm_disable_tdp();
  627. return 0;
  628. err:
  629. __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
  630. iopm_base = 0;
  631. return r;
  632. }
  633. static __exit void svm_hardware_unsetup(void)
  634. {
  635. int cpu;
  636. for_each_possible_cpu(cpu)
  637. svm_cpu_uninit(cpu);
  638. __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
  639. iopm_base = 0;
  640. }
  641. static void init_seg(struct vmcb_seg *seg)
  642. {
  643. seg->selector = 0;
  644. seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
  645. SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
  646. seg->limit = 0xffff;
  647. seg->base = 0;
  648. }
  649. static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
  650. {
  651. seg->selector = 0;
  652. seg->attrib = SVM_SELECTOR_P_MASK | type;
  653. seg->limit = 0xffff;
  654. seg->base = 0;
  655. }
  656. static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
  657. {
  658. struct vcpu_svm *svm = to_svm(vcpu);
  659. u64 g_tsc_offset = 0;
  660. if (is_guest_mode(vcpu)) {
  661. g_tsc_offset = svm->vmcb->control.tsc_offset -
  662. svm->nested.hsave->control.tsc_offset;
  663. svm->nested.hsave->control.tsc_offset = offset;
  664. }
  665. svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
  666. mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
  667. }
  668. static void svm_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
  669. {
  670. struct vcpu_svm *svm = to_svm(vcpu);
  671. svm->vmcb->control.tsc_offset += adjustment;
  672. if (is_guest_mode(vcpu))
  673. svm->nested.hsave->control.tsc_offset += adjustment;
  674. mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
  675. }
  676. static void init_vmcb(struct vcpu_svm *svm)
  677. {
  678. struct vmcb_control_area *control = &svm->vmcb->control;
  679. struct vmcb_save_area *save = &svm->vmcb->save;
  680. svm->vcpu.fpu_active = 1;
  681. svm->vcpu.arch.hflags = 0;
  682. set_cr_intercept(svm, INTERCEPT_CR0_READ);
  683. set_cr_intercept(svm, INTERCEPT_CR3_READ);
  684. set_cr_intercept(svm, INTERCEPT_CR4_READ);
  685. set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
  686. set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
  687. set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
  688. set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  689. set_dr_intercept(svm, INTERCEPT_DR0_READ);
  690. set_dr_intercept(svm, INTERCEPT_DR1_READ);
  691. set_dr_intercept(svm, INTERCEPT_DR2_READ);
  692. set_dr_intercept(svm, INTERCEPT_DR3_READ);
  693. set_dr_intercept(svm, INTERCEPT_DR4_READ);
  694. set_dr_intercept(svm, INTERCEPT_DR5_READ);
  695. set_dr_intercept(svm, INTERCEPT_DR6_READ);
  696. set_dr_intercept(svm, INTERCEPT_DR7_READ);
  697. set_dr_intercept(svm, INTERCEPT_DR0_WRITE);
  698. set_dr_intercept(svm, INTERCEPT_DR1_WRITE);
  699. set_dr_intercept(svm, INTERCEPT_DR2_WRITE);
  700. set_dr_intercept(svm, INTERCEPT_DR3_WRITE);
  701. set_dr_intercept(svm, INTERCEPT_DR4_WRITE);
  702. set_dr_intercept(svm, INTERCEPT_DR5_WRITE);
  703. set_dr_intercept(svm, INTERCEPT_DR6_WRITE);
  704. set_dr_intercept(svm, INTERCEPT_DR7_WRITE);
  705. set_exception_intercept(svm, PF_VECTOR);
  706. set_exception_intercept(svm, UD_VECTOR);
  707. set_exception_intercept(svm, MC_VECTOR);
  708. set_intercept(svm, INTERCEPT_INTR);
  709. set_intercept(svm, INTERCEPT_NMI);
  710. set_intercept(svm, INTERCEPT_SMI);
  711. set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
  712. set_intercept(svm, INTERCEPT_CPUID);
  713. set_intercept(svm, INTERCEPT_INVD);
  714. set_intercept(svm, INTERCEPT_HLT);
  715. set_intercept(svm, INTERCEPT_INVLPG);
  716. set_intercept(svm, INTERCEPT_INVLPGA);
  717. set_intercept(svm, INTERCEPT_IOIO_PROT);
  718. set_intercept(svm, INTERCEPT_MSR_PROT);
  719. set_intercept(svm, INTERCEPT_TASK_SWITCH);
  720. set_intercept(svm, INTERCEPT_SHUTDOWN);
  721. set_intercept(svm, INTERCEPT_VMRUN);
  722. set_intercept(svm, INTERCEPT_VMMCALL);
  723. set_intercept(svm, INTERCEPT_VMLOAD);
  724. set_intercept(svm, INTERCEPT_VMSAVE);
  725. set_intercept(svm, INTERCEPT_STGI);
  726. set_intercept(svm, INTERCEPT_CLGI);
  727. set_intercept(svm, INTERCEPT_SKINIT);
  728. set_intercept(svm, INTERCEPT_WBINVD);
  729. set_intercept(svm, INTERCEPT_MONITOR);
  730. set_intercept(svm, INTERCEPT_MWAIT);
  731. control->iopm_base_pa = iopm_base;
  732. control->msrpm_base_pa = __pa(svm->msrpm);
  733. control->int_ctl = V_INTR_MASKING_MASK;
  734. init_seg(&save->es);
  735. init_seg(&save->ss);
  736. init_seg(&save->ds);
  737. init_seg(&save->fs);
  738. init_seg(&save->gs);
  739. save->cs.selector = 0xf000;
  740. /* Executable/Readable Code Segment */
  741. save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
  742. SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
  743. save->cs.limit = 0xffff;
  744. /*
  745. * cs.base should really be 0xffff0000, but vmx can't handle that, so
  746. * be consistent with it.
  747. *
  748. * Replace when we have real mode working for vmx.
  749. */
  750. save->cs.base = 0xf0000;
  751. save->gdtr.limit = 0xffff;
  752. save->idtr.limit = 0xffff;
  753. init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
  754. init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
  755. svm_set_efer(&svm->vcpu, 0);
  756. save->dr6 = 0xffff0ff0;
  757. save->dr7 = 0x400;
  758. save->rflags = 2;
  759. save->rip = 0x0000fff0;
  760. svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
  761. /*
  762. * This is the guest-visible cr0 value.
  763. * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
  764. */
  765. svm->vcpu.arch.cr0 = 0;
  766. (void)kvm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
  767. save->cr4 = X86_CR4_PAE;
  768. /* rdx = ?? */
  769. if (npt_enabled) {
  770. /* Setup VMCB for Nested Paging */
  771. control->nested_ctl = 1;
  772. clr_intercept(svm, INTERCEPT_TASK_SWITCH);
  773. clr_intercept(svm, INTERCEPT_INVLPG);
  774. clr_exception_intercept(svm, PF_VECTOR);
  775. clr_cr_intercept(svm, INTERCEPT_CR3_READ);
  776. clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
  777. save->g_pat = 0x0007040600070406ULL;
  778. save->cr3 = 0;
  779. save->cr4 = 0;
  780. }
  781. force_new_asid(&svm->vcpu);
  782. svm->nested.vmcb = 0;
  783. svm->vcpu.arch.hflags = 0;
  784. if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
  785. control->pause_filter_count = 3000;
  786. set_intercept(svm, INTERCEPT_PAUSE);
  787. }
  788. mark_all_dirty(svm->vmcb);
  789. enable_gif(svm);
  790. }
  791. static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
  792. {
  793. struct vcpu_svm *svm = to_svm(vcpu);
  794. init_vmcb(svm);
  795. if (!kvm_vcpu_is_bsp(vcpu)) {
  796. kvm_rip_write(vcpu, 0);
  797. svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
  798. svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
  799. }
  800. vcpu->arch.regs_avail = ~0;
  801. vcpu->arch.regs_dirty = ~0;
  802. return 0;
  803. }
  804. static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
  805. {
  806. struct vcpu_svm *svm;
  807. struct page *page;
  808. struct page *msrpm_pages;
  809. struct page *hsave_page;
  810. struct page *nested_msrpm_pages;
  811. int err;
  812. svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  813. if (!svm) {
  814. err = -ENOMEM;
  815. goto out;
  816. }
  817. err = kvm_vcpu_init(&svm->vcpu, kvm, id);
  818. if (err)
  819. goto free_svm;
  820. err = -ENOMEM;
  821. page = alloc_page(GFP_KERNEL);
  822. if (!page)
  823. goto uninit;
  824. msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
  825. if (!msrpm_pages)
  826. goto free_page1;
  827. nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
  828. if (!nested_msrpm_pages)
  829. goto free_page2;
  830. hsave_page = alloc_page(GFP_KERNEL);
  831. if (!hsave_page)
  832. goto free_page3;
  833. svm->nested.hsave = page_address(hsave_page);
  834. svm->msrpm = page_address(msrpm_pages);
  835. svm_vcpu_init_msrpm(svm->msrpm);
  836. svm->nested.msrpm = page_address(nested_msrpm_pages);
  837. svm_vcpu_init_msrpm(svm->nested.msrpm);
  838. svm->vmcb = page_address(page);
  839. clear_page(svm->vmcb);
  840. svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
  841. svm->asid_generation = 0;
  842. init_vmcb(svm);
  843. kvm_write_tsc(&svm->vcpu, 0);
  844. err = fx_init(&svm->vcpu);
  845. if (err)
  846. goto free_page4;
  847. svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
  848. if (kvm_vcpu_is_bsp(&svm->vcpu))
  849. svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
  850. return &svm->vcpu;
  851. free_page4:
  852. __free_page(hsave_page);
  853. free_page3:
  854. __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
  855. free_page2:
  856. __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
  857. free_page1:
  858. __free_page(page);
  859. uninit:
  860. kvm_vcpu_uninit(&svm->vcpu);
  861. free_svm:
  862. kmem_cache_free(kvm_vcpu_cache, svm);
  863. out:
  864. return ERR_PTR(err);
  865. }
  866. static void svm_free_vcpu(struct kvm_vcpu *vcpu)
  867. {
  868. struct vcpu_svm *svm = to_svm(vcpu);
  869. __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
  870. __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
  871. __free_page(virt_to_page(svm->nested.hsave));
  872. __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
  873. kvm_vcpu_uninit(vcpu);
  874. kmem_cache_free(kvm_vcpu_cache, svm);
  875. }
  876. static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  877. {
  878. struct vcpu_svm *svm = to_svm(vcpu);
  879. int i;
  880. if (unlikely(cpu != vcpu->cpu)) {
  881. svm->asid_generation = 0;
  882. mark_all_dirty(svm->vmcb);
  883. }
  884. #ifdef CONFIG_X86_64
  885. rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
  886. #endif
  887. savesegment(fs, svm->host.fs);
  888. savesegment(gs, svm->host.gs);
  889. svm->host.ldt = kvm_read_ldt();
  890. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  891. rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  892. }
  893. static void svm_vcpu_put(struct kvm_vcpu *vcpu)
  894. {
  895. struct vcpu_svm *svm = to_svm(vcpu);
  896. int i;
  897. ++vcpu->stat.host_state_reload;
  898. kvm_load_ldt(svm->host.ldt);
  899. #ifdef CONFIG_X86_64
  900. loadsegment(fs, svm->host.fs);
  901. load_gs_index(svm->host.gs);
  902. wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
  903. #else
  904. loadsegment(gs, svm->host.gs);
  905. #endif
  906. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  907. wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  908. }
  909. static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
  910. {
  911. return to_svm(vcpu)->vmcb->save.rflags;
  912. }
  913. static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  914. {
  915. to_svm(vcpu)->vmcb->save.rflags = rflags;
  916. }
  917. static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  918. {
  919. switch (reg) {
  920. case VCPU_EXREG_PDPTR:
  921. BUG_ON(!npt_enabled);
  922. load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3);
  923. break;
  924. default:
  925. BUG();
  926. }
  927. }
  928. static void svm_set_vintr(struct vcpu_svm *svm)
  929. {
  930. set_intercept(svm, INTERCEPT_VINTR);
  931. }
  932. static void svm_clear_vintr(struct vcpu_svm *svm)
  933. {
  934. clr_intercept(svm, INTERCEPT_VINTR);
  935. }
  936. static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
  937. {
  938. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  939. switch (seg) {
  940. case VCPU_SREG_CS: return &save->cs;
  941. case VCPU_SREG_DS: return &save->ds;
  942. case VCPU_SREG_ES: return &save->es;
  943. case VCPU_SREG_FS: return &save->fs;
  944. case VCPU_SREG_GS: return &save->gs;
  945. case VCPU_SREG_SS: return &save->ss;
  946. case VCPU_SREG_TR: return &save->tr;
  947. case VCPU_SREG_LDTR: return &save->ldtr;
  948. }
  949. BUG();
  950. return NULL;
  951. }
  952. static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  953. {
  954. struct vmcb_seg *s = svm_seg(vcpu, seg);
  955. return s->base;
  956. }
  957. static void svm_get_segment(struct kvm_vcpu *vcpu,
  958. struct kvm_segment *var, int seg)
  959. {
  960. struct vmcb_seg *s = svm_seg(vcpu, seg);
  961. var->base = s->base;
  962. var->limit = s->limit;
  963. var->selector = s->selector;
  964. var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
  965. var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
  966. var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
  967. var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
  968. var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
  969. var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
  970. var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
  971. var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
  972. /*
  973. * AMD's VMCB does not have an explicit unusable field, so emulate it
  974. * for cross vendor migration purposes by "not present"
  975. */
  976. var->unusable = !var->present || (var->type == 0);
  977. switch (seg) {
  978. case VCPU_SREG_CS:
  979. /*
  980. * SVM always stores 0 for the 'G' bit in the CS selector in
  981. * the VMCB on a VMEXIT. This hurts cross-vendor migration:
  982. * Intel's VMENTRY has a check on the 'G' bit.
  983. */
  984. var->g = s->limit > 0xfffff;
  985. break;
  986. case VCPU_SREG_TR:
  987. /*
  988. * Work around a bug where the busy flag in the tr selector
  989. * isn't exposed
  990. */
  991. var->type |= 0x2;
  992. break;
  993. case VCPU_SREG_DS:
  994. case VCPU_SREG_ES:
  995. case VCPU_SREG_FS:
  996. case VCPU_SREG_GS:
  997. /*
  998. * The accessed bit must always be set in the segment
  999. * descriptor cache, although it can be cleared in the
  1000. * descriptor, the cached bit always remains at 1. Since
  1001. * Intel has a check on this, set it here to support
  1002. * cross-vendor migration.
  1003. */
  1004. if (!var->unusable)
  1005. var->type |= 0x1;
  1006. break;
  1007. case VCPU_SREG_SS:
  1008. /*
  1009. * On AMD CPUs sometimes the DB bit in the segment
  1010. * descriptor is left as 1, although the whole segment has
  1011. * been made unusable. Clear it here to pass an Intel VMX
  1012. * entry check when cross vendor migrating.
  1013. */
  1014. if (var->unusable)
  1015. var->db = 0;
  1016. break;
  1017. }
  1018. }
  1019. static int svm_get_cpl(struct kvm_vcpu *vcpu)
  1020. {
  1021. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  1022. return save->cpl;
  1023. }
  1024. static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1025. {
  1026. struct vcpu_svm *svm = to_svm(vcpu);
  1027. dt->size = svm->vmcb->save.idtr.limit;
  1028. dt->address = svm->vmcb->save.idtr.base;
  1029. }
  1030. static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1031. {
  1032. struct vcpu_svm *svm = to_svm(vcpu);
  1033. svm->vmcb->save.idtr.limit = dt->size;
  1034. svm->vmcb->save.idtr.base = dt->address ;
  1035. }
  1036. static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1037. {
  1038. struct vcpu_svm *svm = to_svm(vcpu);
  1039. dt->size = svm->vmcb->save.gdtr.limit;
  1040. dt->address = svm->vmcb->save.gdtr.base;
  1041. }
  1042. static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1043. {
  1044. struct vcpu_svm *svm = to_svm(vcpu);
  1045. svm->vmcb->save.gdtr.limit = dt->size;
  1046. svm->vmcb->save.gdtr.base = dt->address ;
  1047. }
  1048. static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  1049. {
  1050. }
  1051. static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  1052. {
  1053. }
  1054. static void update_cr0_intercept(struct vcpu_svm *svm)
  1055. {
  1056. ulong gcr0 = svm->vcpu.arch.cr0;
  1057. u64 *hcr0 = &svm->vmcb->save.cr0;
  1058. if (!svm->vcpu.fpu_active)
  1059. *hcr0 |= SVM_CR0_SELECTIVE_MASK;
  1060. else
  1061. *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
  1062. | (gcr0 & SVM_CR0_SELECTIVE_MASK);
  1063. if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
  1064. clr_cr_intercept(svm, INTERCEPT_CR0_READ);
  1065. clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
  1066. } else {
  1067. set_cr_intercept(svm, INTERCEPT_CR0_READ);
  1068. set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
  1069. }
  1070. }
  1071. static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  1072. {
  1073. struct vcpu_svm *svm = to_svm(vcpu);
  1074. if (is_guest_mode(vcpu)) {
  1075. /*
  1076. * We are here because we run in nested mode, the host kvm
  1077. * intercepts cr0 writes but the l1 hypervisor does not.
  1078. * But the L1 hypervisor may intercept selective cr0 writes.
  1079. * This needs to be checked here.
  1080. */
  1081. unsigned long old, new;
  1082. /* Remove bits that would trigger a real cr0 write intercept */
  1083. old = vcpu->arch.cr0 & SVM_CR0_SELECTIVE_MASK;
  1084. new = cr0 & SVM_CR0_SELECTIVE_MASK;
  1085. if (old == new) {
  1086. /* cr0 write with ts and mp unchanged */
  1087. svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
  1088. if (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE) {
  1089. svm->nested.vmexit_rip = kvm_rip_read(vcpu);
  1090. svm->nested.vmexit_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  1091. svm->nested.vmexit_rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
  1092. return;
  1093. }
  1094. }
  1095. }
  1096. #ifdef CONFIG_X86_64
  1097. if (vcpu->arch.efer & EFER_LME) {
  1098. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  1099. vcpu->arch.efer |= EFER_LMA;
  1100. svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
  1101. }
  1102. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
  1103. vcpu->arch.efer &= ~EFER_LMA;
  1104. svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
  1105. }
  1106. }
  1107. #endif
  1108. vcpu->arch.cr0 = cr0;
  1109. if (!npt_enabled)
  1110. cr0 |= X86_CR0_PG | X86_CR0_WP;
  1111. if (!vcpu->fpu_active)
  1112. cr0 |= X86_CR0_TS;
  1113. /*
  1114. * re-enable caching here because the QEMU bios
  1115. * does not do it - this results in some delay at
  1116. * reboot
  1117. */
  1118. cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
  1119. svm->vmcb->save.cr0 = cr0;
  1120. update_cr0_intercept(svm);
  1121. }
  1122. static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  1123. {
  1124. unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
  1125. unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
  1126. if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
  1127. force_new_asid(vcpu);
  1128. vcpu->arch.cr4 = cr4;
  1129. if (!npt_enabled)
  1130. cr4 |= X86_CR4_PAE;
  1131. cr4 |= host_cr4_mce;
  1132. to_svm(vcpu)->vmcb->save.cr4 = cr4;
  1133. }
  1134. static void svm_set_segment(struct kvm_vcpu *vcpu,
  1135. struct kvm_segment *var, int seg)
  1136. {
  1137. struct vcpu_svm *svm = to_svm(vcpu);
  1138. struct vmcb_seg *s = svm_seg(vcpu, seg);
  1139. s->base = var->base;
  1140. s->limit = var->limit;
  1141. s->selector = var->selector;
  1142. if (var->unusable)
  1143. s->attrib = 0;
  1144. else {
  1145. s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
  1146. s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
  1147. s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
  1148. s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
  1149. s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
  1150. s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
  1151. s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
  1152. s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
  1153. }
  1154. if (seg == VCPU_SREG_CS)
  1155. svm->vmcb->save.cpl
  1156. = (svm->vmcb->save.cs.attrib
  1157. >> SVM_SELECTOR_DPL_SHIFT) & 3;
  1158. }
  1159. static void update_db_intercept(struct kvm_vcpu *vcpu)
  1160. {
  1161. struct vcpu_svm *svm = to_svm(vcpu);
  1162. clr_exception_intercept(svm, DB_VECTOR);
  1163. clr_exception_intercept(svm, BP_VECTOR);
  1164. if (svm->nmi_singlestep)
  1165. set_exception_intercept(svm, DB_VECTOR);
  1166. if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
  1167. if (vcpu->guest_debug &
  1168. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  1169. set_exception_intercept(svm, DB_VECTOR);
  1170. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  1171. set_exception_intercept(svm, BP_VECTOR);
  1172. } else
  1173. vcpu->guest_debug = 0;
  1174. }
  1175. static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
  1176. {
  1177. struct vcpu_svm *svm = to_svm(vcpu);
  1178. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
  1179. svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
  1180. else
  1181. svm->vmcb->save.dr7 = vcpu->arch.dr7;
  1182. update_db_intercept(vcpu);
  1183. }
  1184. static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
  1185. {
  1186. if (sd->next_asid > sd->max_asid) {
  1187. ++sd->asid_generation;
  1188. sd->next_asid = 1;
  1189. svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
  1190. }
  1191. svm->asid_generation = sd->asid_generation;
  1192. svm->vmcb->control.asid = sd->next_asid++;
  1193. mark_dirty(svm->vmcb, VMCB_ASID);
  1194. }
  1195. static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
  1196. {
  1197. struct vcpu_svm *svm = to_svm(vcpu);
  1198. svm->vmcb->save.dr7 = value;
  1199. }
  1200. static int pf_interception(struct vcpu_svm *svm)
  1201. {
  1202. u64 fault_address = svm->vmcb->control.exit_info_2;
  1203. u32 error_code;
  1204. int r = 1;
  1205. switch (svm->apf_reason) {
  1206. default:
  1207. error_code = svm->vmcb->control.exit_info_1;
  1208. trace_kvm_page_fault(fault_address, error_code);
  1209. if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
  1210. kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
  1211. r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
  1212. break;
  1213. case KVM_PV_REASON_PAGE_NOT_PRESENT:
  1214. svm->apf_reason = 0;
  1215. local_irq_disable();
  1216. kvm_async_pf_task_wait(fault_address);
  1217. local_irq_enable();
  1218. break;
  1219. case KVM_PV_REASON_PAGE_READY:
  1220. svm->apf_reason = 0;
  1221. local_irq_disable();
  1222. kvm_async_pf_task_wake(fault_address);
  1223. local_irq_enable();
  1224. break;
  1225. }
  1226. return r;
  1227. }
  1228. static int db_interception(struct vcpu_svm *svm)
  1229. {
  1230. struct kvm_run *kvm_run = svm->vcpu.run;
  1231. if (!(svm->vcpu.guest_debug &
  1232. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
  1233. !svm->nmi_singlestep) {
  1234. kvm_queue_exception(&svm->vcpu, DB_VECTOR);
  1235. return 1;
  1236. }
  1237. if (svm->nmi_singlestep) {
  1238. svm->nmi_singlestep = false;
  1239. if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
  1240. svm->vmcb->save.rflags &=
  1241. ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
  1242. update_db_intercept(&svm->vcpu);
  1243. }
  1244. if (svm->vcpu.guest_debug &
  1245. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
  1246. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  1247. kvm_run->debug.arch.pc =
  1248. svm->vmcb->save.cs.base + svm->vmcb->save.rip;
  1249. kvm_run->debug.arch.exception = DB_VECTOR;
  1250. return 0;
  1251. }
  1252. return 1;
  1253. }
  1254. static int bp_interception(struct vcpu_svm *svm)
  1255. {
  1256. struct kvm_run *kvm_run = svm->vcpu.run;
  1257. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  1258. kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
  1259. kvm_run->debug.arch.exception = BP_VECTOR;
  1260. return 0;
  1261. }
  1262. static int ud_interception(struct vcpu_svm *svm)
  1263. {
  1264. int er;
  1265. er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
  1266. if (er != EMULATE_DONE)
  1267. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  1268. return 1;
  1269. }
  1270. static void svm_fpu_activate(struct kvm_vcpu *vcpu)
  1271. {
  1272. struct vcpu_svm *svm = to_svm(vcpu);
  1273. clr_exception_intercept(svm, NM_VECTOR);
  1274. svm->vcpu.fpu_active = 1;
  1275. update_cr0_intercept(svm);
  1276. }
  1277. static int nm_interception(struct vcpu_svm *svm)
  1278. {
  1279. svm_fpu_activate(&svm->vcpu);
  1280. return 1;
  1281. }
  1282. static bool is_erratum_383(void)
  1283. {
  1284. int err, i;
  1285. u64 value;
  1286. if (!erratum_383_found)
  1287. return false;
  1288. value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
  1289. if (err)
  1290. return false;
  1291. /* Bit 62 may or may not be set for this mce */
  1292. value &= ~(1ULL << 62);
  1293. if (value != 0xb600000000010015ULL)
  1294. return false;
  1295. /* Clear MCi_STATUS registers */
  1296. for (i = 0; i < 6; ++i)
  1297. native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
  1298. value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
  1299. if (!err) {
  1300. u32 low, high;
  1301. value &= ~(1ULL << 2);
  1302. low = lower_32_bits(value);
  1303. high = upper_32_bits(value);
  1304. native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
  1305. }
  1306. /* Flush tlb to evict multi-match entries */
  1307. __flush_tlb_all();
  1308. return true;
  1309. }
  1310. static void svm_handle_mce(struct vcpu_svm *svm)
  1311. {
  1312. if (is_erratum_383()) {
  1313. /*
  1314. * Erratum 383 triggered. Guest state is corrupt so kill the
  1315. * guest.
  1316. */
  1317. pr_err("KVM: Guest triggered AMD Erratum 383\n");
  1318. kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
  1319. return;
  1320. }
  1321. /*
  1322. * On an #MC intercept the MCE handler is not called automatically in
  1323. * the host. So do it by hand here.
  1324. */
  1325. asm volatile (
  1326. "int $0x12\n");
  1327. /* not sure if we ever come back to this point */
  1328. return;
  1329. }
  1330. static int mc_interception(struct vcpu_svm *svm)
  1331. {
  1332. return 1;
  1333. }
  1334. static int shutdown_interception(struct vcpu_svm *svm)
  1335. {
  1336. struct kvm_run *kvm_run = svm->vcpu.run;
  1337. /*
  1338. * VMCB is undefined after a SHUTDOWN intercept
  1339. * so reinitialize it.
  1340. */
  1341. clear_page(svm->vmcb);
  1342. init_vmcb(svm);
  1343. kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
  1344. return 0;
  1345. }
  1346. static int io_interception(struct vcpu_svm *svm)
  1347. {
  1348. struct kvm_vcpu *vcpu = &svm->vcpu;
  1349. u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
  1350. int size, in, string;
  1351. unsigned port;
  1352. ++svm->vcpu.stat.io_exits;
  1353. string = (io_info & SVM_IOIO_STR_MASK) != 0;
  1354. in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
  1355. if (string || in)
  1356. return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE;
  1357. port = io_info >> 16;
  1358. size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
  1359. svm->next_rip = svm->vmcb->control.exit_info_2;
  1360. skip_emulated_instruction(&svm->vcpu);
  1361. return kvm_fast_pio_out(vcpu, size, port);
  1362. }
  1363. static int nmi_interception(struct vcpu_svm *svm)
  1364. {
  1365. return 1;
  1366. }
  1367. static int intr_interception(struct vcpu_svm *svm)
  1368. {
  1369. ++svm->vcpu.stat.irq_exits;
  1370. return 1;
  1371. }
  1372. static int nop_on_interception(struct vcpu_svm *svm)
  1373. {
  1374. return 1;
  1375. }
  1376. static int halt_interception(struct vcpu_svm *svm)
  1377. {
  1378. svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
  1379. skip_emulated_instruction(&svm->vcpu);
  1380. return kvm_emulate_halt(&svm->vcpu);
  1381. }
  1382. static int vmmcall_interception(struct vcpu_svm *svm)
  1383. {
  1384. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1385. skip_emulated_instruction(&svm->vcpu);
  1386. kvm_emulate_hypercall(&svm->vcpu);
  1387. return 1;
  1388. }
  1389. static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
  1390. {
  1391. struct vcpu_svm *svm = to_svm(vcpu);
  1392. return svm->nested.nested_cr3;
  1393. }
  1394. static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
  1395. unsigned long root)
  1396. {
  1397. struct vcpu_svm *svm = to_svm(vcpu);
  1398. svm->vmcb->control.nested_cr3 = root;
  1399. force_new_asid(vcpu);
  1400. }
  1401. static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
  1402. struct x86_exception *fault)
  1403. {
  1404. struct vcpu_svm *svm = to_svm(vcpu);
  1405. svm->vmcb->control.exit_code = SVM_EXIT_NPF;
  1406. svm->vmcb->control.exit_code_hi = 0;
  1407. svm->vmcb->control.exit_info_1 = fault->error_code;
  1408. svm->vmcb->control.exit_info_2 = fault->address;
  1409. nested_svm_vmexit(svm);
  1410. }
  1411. static int nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
  1412. {
  1413. int r;
  1414. r = kvm_init_shadow_mmu(vcpu, &vcpu->arch.mmu);
  1415. vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
  1416. vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
  1417. vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
  1418. vcpu->arch.mmu.shadow_root_level = get_npt_level();
  1419. vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
  1420. return r;
  1421. }
  1422. static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
  1423. {
  1424. vcpu->arch.walk_mmu = &vcpu->arch.mmu;
  1425. }
  1426. static int nested_svm_check_permissions(struct vcpu_svm *svm)
  1427. {
  1428. if (!(svm->vcpu.arch.efer & EFER_SVME)
  1429. || !is_paging(&svm->vcpu)) {
  1430. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  1431. return 1;
  1432. }
  1433. if (svm->vmcb->save.cpl) {
  1434. kvm_inject_gp(&svm->vcpu, 0);
  1435. return 1;
  1436. }
  1437. return 0;
  1438. }
  1439. static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
  1440. bool has_error_code, u32 error_code)
  1441. {
  1442. int vmexit;
  1443. if (!is_guest_mode(&svm->vcpu))
  1444. return 0;
  1445. svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
  1446. svm->vmcb->control.exit_code_hi = 0;
  1447. svm->vmcb->control.exit_info_1 = error_code;
  1448. svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
  1449. vmexit = nested_svm_intercept(svm);
  1450. if (vmexit == NESTED_EXIT_DONE)
  1451. svm->nested.exit_required = true;
  1452. return vmexit;
  1453. }
  1454. /* This function returns true if it is save to enable the irq window */
  1455. static inline bool nested_svm_intr(struct vcpu_svm *svm)
  1456. {
  1457. if (!is_guest_mode(&svm->vcpu))
  1458. return true;
  1459. if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
  1460. return true;
  1461. if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
  1462. return false;
  1463. /*
  1464. * if vmexit was already requested (by intercepted exception
  1465. * for instance) do not overwrite it with "external interrupt"
  1466. * vmexit.
  1467. */
  1468. if (svm->nested.exit_required)
  1469. return false;
  1470. svm->vmcb->control.exit_code = SVM_EXIT_INTR;
  1471. svm->vmcb->control.exit_info_1 = 0;
  1472. svm->vmcb->control.exit_info_2 = 0;
  1473. if (svm->nested.intercept & 1ULL) {
  1474. /*
  1475. * The #vmexit can't be emulated here directly because this
  1476. * code path runs with irqs and preemtion disabled. A
  1477. * #vmexit emulation might sleep. Only signal request for
  1478. * the #vmexit here.
  1479. */
  1480. svm->nested.exit_required = true;
  1481. trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
  1482. return false;
  1483. }
  1484. return true;
  1485. }
  1486. /* This function returns true if it is save to enable the nmi window */
  1487. static inline bool nested_svm_nmi(struct vcpu_svm *svm)
  1488. {
  1489. if (!is_guest_mode(&svm->vcpu))
  1490. return true;
  1491. if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
  1492. return true;
  1493. svm->vmcb->control.exit_code = SVM_EXIT_NMI;
  1494. svm->nested.exit_required = true;
  1495. return false;
  1496. }
  1497. static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
  1498. {
  1499. struct page *page;
  1500. might_sleep();
  1501. page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
  1502. if (is_error_page(page))
  1503. goto error;
  1504. *_page = page;
  1505. return kmap(page);
  1506. error:
  1507. kvm_release_page_clean(page);
  1508. kvm_inject_gp(&svm->vcpu, 0);
  1509. return NULL;
  1510. }
  1511. static void nested_svm_unmap(struct page *page)
  1512. {
  1513. kunmap(page);
  1514. kvm_release_page_dirty(page);
  1515. }
  1516. static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
  1517. {
  1518. unsigned port;
  1519. u8 val, bit;
  1520. u64 gpa;
  1521. if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
  1522. return NESTED_EXIT_HOST;
  1523. port = svm->vmcb->control.exit_info_1 >> 16;
  1524. gpa = svm->nested.vmcb_iopm + (port / 8);
  1525. bit = port % 8;
  1526. val = 0;
  1527. if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
  1528. val &= (1 << bit);
  1529. return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
  1530. }
  1531. static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
  1532. {
  1533. u32 offset, msr, value;
  1534. int write, mask;
  1535. if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
  1536. return NESTED_EXIT_HOST;
  1537. msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  1538. offset = svm_msrpm_offset(msr);
  1539. write = svm->vmcb->control.exit_info_1 & 1;
  1540. mask = 1 << ((2 * (msr & 0xf)) + write);
  1541. if (offset == MSR_INVALID)
  1542. return NESTED_EXIT_DONE;
  1543. /* Offset is in 32 bit units but need in 8 bit units */
  1544. offset *= 4;
  1545. if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
  1546. return NESTED_EXIT_DONE;
  1547. return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
  1548. }
  1549. static int nested_svm_exit_special(struct vcpu_svm *svm)
  1550. {
  1551. u32 exit_code = svm->vmcb->control.exit_code;
  1552. switch (exit_code) {
  1553. case SVM_EXIT_INTR:
  1554. case SVM_EXIT_NMI:
  1555. case SVM_EXIT_EXCP_BASE + MC_VECTOR:
  1556. return NESTED_EXIT_HOST;
  1557. case SVM_EXIT_NPF:
  1558. /* For now we are always handling NPFs when using them */
  1559. if (npt_enabled)
  1560. return NESTED_EXIT_HOST;
  1561. break;
  1562. case SVM_EXIT_EXCP_BASE + PF_VECTOR:
  1563. /* When we're shadowing, trap PFs, but not async PF */
  1564. if (!npt_enabled && svm->apf_reason == 0)
  1565. return NESTED_EXIT_HOST;
  1566. break;
  1567. case SVM_EXIT_EXCP_BASE + NM_VECTOR:
  1568. nm_interception(svm);
  1569. break;
  1570. default:
  1571. break;
  1572. }
  1573. return NESTED_EXIT_CONTINUE;
  1574. }
  1575. /*
  1576. * If this function returns true, this #vmexit was already handled
  1577. */
  1578. static int nested_svm_intercept(struct vcpu_svm *svm)
  1579. {
  1580. u32 exit_code = svm->vmcb->control.exit_code;
  1581. int vmexit = NESTED_EXIT_HOST;
  1582. switch (exit_code) {
  1583. case SVM_EXIT_MSR:
  1584. vmexit = nested_svm_exit_handled_msr(svm);
  1585. break;
  1586. case SVM_EXIT_IOIO:
  1587. vmexit = nested_svm_intercept_ioio(svm);
  1588. break;
  1589. case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
  1590. u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
  1591. if (svm->nested.intercept_cr & bit)
  1592. vmexit = NESTED_EXIT_DONE;
  1593. break;
  1594. }
  1595. case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
  1596. u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
  1597. if (svm->nested.intercept_dr & bit)
  1598. vmexit = NESTED_EXIT_DONE;
  1599. break;
  1600. }
  1601. case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
  1602. u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
  1603. if (svm->nested.intercept_exceptions & excp_bits)
  1604. vmexit = NESTED_EXIT_DONE;
  1605. /* async page fault always cause vmexit */
  1606. else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
  1607. svm->apf_reason != 0)
  1608. vmexit = NESTED_EXIT_DONE;
  1609. break;
  1610. }
  1611. case SVM_EXIT_ERR: {
  1612. vmexit = NESTED_EXIT_DONE;
  1613. break;
  1614. }
  1615. default: {
  1616. u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
  1617. if (svm->nested.intercept & exit_bits)
  1618. vmexit = NESTED_EXIT_DONE;
  1619. }
  1620. }
  1621. return vmexit;
  1622. }
  1623. static int nested_svm_exit_handled(struct vcpu_svm *svm)
  1624. {
  1625. int vmexit;
  1626. vmexit = nested_svm_intercept(svm);
  1627. if (vmexit == NESTED_EXIT_DONE)
  1628. nested_svm_vmexit(svm);
  1629. return vmexit;
  1630. }
  1631. static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
  1632. {
  1633. struct vmcb_control_area *dst = &dst_vmcb->control;
  1634. struct vmcb_control_area *from = &from_vmcb->control;
  1635. dst->intercept_cr = from->intercept_cr;
  1636. dst->intercept_dr = from->intercept_dr;
  1637. dst->intercept_exceptions = from->intercept_exceptions;
  1638. dst->intercept = from->intercept;
  1639. dst->iopm_base_pa = from->iopm_base_pa;
  1640. dst->msrpm_base_pa = from->msrpm_base_pa;
  1641. dst->tsc_offset = from->tsc_offset;
  1642. dst->asid = from->asid;
  1643. dst->tlb_ctl = from->tlb_ctl;
  1644. dst->int_ctl = from->int_ctl;
  1645. dst->int_vector = from->int_vector;
  1646. dst->int_state = from->int_state;
  1647. dst->exit_code = from->exit_code;
  1648. dst->exit_code_hi = from->exit_code_hi;
  1649. dst->exit_info_1 = from->exit_info_1;
  1650. dst->exit_info_2 = from->exit_info_2;
  1651. dst->exit_int_info = from->exit_int_info;
  1652. dst->exit_int_info_err = from->exit_int_info_err;
  1653. dst->nested_ctl = from->nested_ctl;
  1654. dst->event_inj = from->event_inj;
  1655. dst->event_inj_err = from->event_inj_err;
  1656. dst->nested_cr3 = from->nested_cr3;
  1657. dst->lbr_ctl = from->lbr_ctl;
  1658. }
  1659. static int nested_svm_vmexit(struct vcpu_svm *svm)
  1660. {
  1661. struct vmcb *nested_vmcb;
  1662. struct vmcb *hsave = svm->nested.hsave;
  1663. struct vmcb *vmcb = svm->vmcb;
  1664. struct page *page;
  1665. trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
  1666. vmcb->control.exit_info_1,
  1667. vmcb->control.exit_info_2,
  1668. vmcb->control.exit_int_info,
  1669. vmcb->control.exit_int_info_err);
  1670. nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
  1671. if (!nested_vmcb)
  1672. return 1;
  1673. /* Exit Guest-Mode */
  1674. leave_guest_mode(&svm->vcpu);
  1675. svm->nested.vmcb = 0;
  1676. /* Give the current vmcb to the guest */
  1677. disable_gif(svm);
  1678. nested_vmcb->save.es = vmcb->save.es;
  1679. nested_vmcb->save.cs = vmcb->save.cs;
  1680. nested_vmcb->save.ss = vmcb->save.ss;
  1681. nested_vmcb->save.ds = vmcb->save.ds;
  1682. nested_vmcb->save.gdtr = vmcb->save.gdtr;
  1683. nested_vmcb->save.idtr = vmcb->save.idtr;
  1684. nested_vmcb->save.efer = svm->vcpu.arch.efer;
  1685. nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
  1686. nested_vmcb->save.cr3 = svm->vcpu.arch.cr3;
  1687. nested_vmcb->save.cr2 = vmcb->save.cr2;
  1688. nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
  1689. nested_vmcb->save.rflags = vmcb->save.rflags;
  1690. nested_vmcb->save.rip = vmcb->save.rip;
  1691. nested_vmcb->save.rsp = vmcb->save.rsp;
  1692. nested_vmcb->save.rax = vmcb->save.rax;
  1693. nested_vmcb->save.dr7 = vmcb->save.dr7;
  1694. nested_vmcb->save.dr6 = vmcb->save.dr6;
  1695. nested_vmcb->save.cpl = vmcb->save.cpl;
  1696. nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
  1697. nested_vmcb->control.int_vector = vmcb->control.int_vector;
  1698. nested_vmcb->control.int_state = vmcb->control.int_state;
  1699. nested_vmcb->control.exit_code = vmcb->control.exit_code;
  1700. nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
  1701. nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
  1702. nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
  1703. nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
  1704. nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
  1705. nested_vmcb->control.next_rip = vmcb->control.next_rip;
  1706. /*
  1707. * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
  1708. * to make sure that we do not lose injected events. So check event_inj
  1709. * here and copy it to exit_int_info if it is valid.
  1710. * Exit_int_info and event_inj can't be both valid because the case
  1711. * below only happens on a VMRUN instruction intercept which has
  1712. * no valid exit_int_info set.
  1713. */
  1714. if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
  1715. struct vmcb_control_area *nc = &nested_vmcb->control;
  1716. nc->exit_int_info = vmcb->control.event_inj;
  1717. nc->exit_int_info_err = vmcb->control.event_inj_err;
  1718. }
  1719. nested_vmcb->control.tlb_ctl = 0;
  1720. nested_vmcb->control.event_inj = 0;
  1721. nested_vmcb->control.event_inj_err = 0;
  1722. /* We always set V_INTR_MASKING and remember the old value in hflags */
  1723. if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
  1724. nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
  1725. /* Restore the original control entries */
  1726. copy_vmcb_control_area(vmcb, hsave);
  1727. kvm_clear_exception_queue(&svm->vcpu);
  1728. kvm_clear_interrupt_queue(&svm->vcpu);
  1729. svm->nested.nested_cr3 = 0;
  1730. /* Restore selected save entries */
  1731. svm->vmcb->save.es = hsave->save.es;
  1732. svm->vmcb->save.cs = hsave->save.cs;
  1733. svm->vmcb->save.ss = hsave->save.ss;
  1734. svm->vmcb->save.ds = hsave->save.ds;
  1735. svm->vmcb->save.gdtr = hsave->save.gdtr;
  1736. svm->vmcb->save.idtr = hsave->save.idtr;
  1737. svm->vmcb->save.rflags = hsave->save.rflags;
  1738. svm_set_efer(&svm->vcpu, hsave->save.efer);
  1739. svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
  1740. svm_set_cr4(&svm->vcpu, hsave->save.cr4);
  1741. if (npt_enabled) {
  1742. svm->vmcb->save.cr3 = hsave->save.cr3;
  1743. svm->vcpu.arch.cr3 = hsave->save.cr3;
  1744. } else {
  1745. (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
  1746. }
  1747. kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
  1748. kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
  1749. kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
  1750. svm->vmcb->save.dr7 = 0;
  1751. svm->vmcb->save.cpl = 0;
  1752. svm->vmcb->control.exit_int_info = 0;
  1753. mark_all_dirty(svm->vmcb);
  1754. nested_svm_unmap(page);
  1755. nested_svm_uninit_mmu_context(&svm->vcpu);
  1756. kvm_mmu_reset_context(&svm->vcpu);
  1757. kvm_mmu_load(&svm->vcpu);
  1758. return 0;
  1759. }
  1760. static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
  1761. {
  1762. /*
  1763. * This function merges the msr permission bitmaps of kvm and the
  1764. * nested vmcb. It is omptimized in that it only merges the parts where
  1765. * the kvm msr permission bitmap may contain zero bits
  1766. */
  1767. int i;
  1768. if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
  1769. return true;
  1770. for (i = 0; i < MSRPM_OFFSETS; i++) {
  1771. u32 value, p;
  1772. u64 offset;
  1773. if (msrpm_offsets[i] == 0xffffffff)
  1774. break;
  1775. p = msrpm_offsets[i];
  1776. offset = svm->nested.vmcb_msrpm + (p * 4);
  1777. if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
  1778. return false;
  1779. svm->nested.msrpm[p] = svm->msrpm[p] | value;
  1780. }
  1781. svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
  1782. return true;
  1783. }
  1784. static bool nested_vmcb_checks(struct vmcb *vmcb)
  1785. {
  1786. if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
  1787. return false;
  1788. if (vmcb->control.asid == 0)
  1789. return false;
  1790. if (vmcb->control.nested_ctl && !npt_enabled)
  1791. return false;
  1792. return true;
  1793. }
  1794. static bool nested_svm_vmrun(struct vcpu_svm *svm)
  1795. {
  1796. struct vmcb *nested_vmcb;
  1797. struct vmcb *hsave = svm->nested.hsave;
  1798. struct vmcb *vmcb = svm->vmcb;
  1799. struct page *page;
  1800. u64 vmcb_gpa;
  1801. vmcb_gpa = svm->vmcb->save.rax;
  1802. nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
  1803. if (!nested_vmcb)
  1804. return false;
  1805. if (!nested_vmcb_checks(nested_vmcb)) {
  1806. nested_vmcb->control.exit_code = SVM_EXIT_ERR;
  1807. nested_vmcb->control.exit_code_hi = 0;
  1808. nested_vmcb->control.exit_info_1 = 0;
  1809. nested_vmcb->control.exit_info_2 = 0;
  1810. nested_svm_unmap(page);
  1811. return false;
  1812. }
  1813. trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
  1814. nested_vmcb->save.rip,
  1815. nested_vmcb->control.int_ctl,
  1816. nested_vmcb->control.event_inj,
  1817. nested_vmcb->control.nested_ctl);
  1818. trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
  1819. nested_vmcb->control.intercept_cr >> 16,
  1820. nested_vmcb->control.intercept_exceptions,
  1821. nested_vmcb->control.intercept);
  1822. /* Clear internal status */
  1823. kvm_clear_exception_queue(&svm->vcpu);
  1824. kvm_clear_interrupt_queue(&svm->vcpu);
  1825. /*
  1826. * Save the old vmcb, so we don't need to pick what we save, but can
  1827. * restore everything when a VMEXIT occurs
  1828. */
  1829. hsave->save.es = vmcb->save.es;
  1830. hsave->save.cs = vmcb->save.cs;
  1831. hsave->save.ss = vmcb->save.ss;
  1832. hsave->save.ds = vmcb->save.ds;
  1833. hsave->save.gdtr = vmcb->save.gdtr;
  1834. hsave->save.idtr = vmcb->save.idtr;
  1835. hsave->save.efer = svm->vcpu.arch.efer;
  1836. hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
  1837. hsave->save.cr4 = svm->vcpu.arch.cr4;
  1838. hsave->save.rflags = vmcb->save.rflags;
  1839. hsave->save.rip = kvm_rip_read(&svm->vcpu);
  1840. hsave->save.rsp = vmcb->save.rsp;
  1841. hsave->save.rax = vmcb->save.rax;
  1842. if (npt_enabled)
  1843. hsave->save.cr3 = vmcb->save.cr3;
  1844. else
  1845. hsave->save.cr3 = svm->vcpu.arch.cr3;
  1846. copy_vmcb_control_area(hsave, vmcb);
  1847. if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
  1848. svm->vcpu.arch.hflags |= HF_HIF_MASK;
  1849. else
  1850. svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
  1851. if (nested_vmcb->control.nested_ctl) {
  1852. kvm_mmu_unload(&svm->vcpu);
  1853. svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
  1854. nested_svm_init_mmu_context(&svm->vcpu);
  1855. }
  1856. /* Load the nested guest state */
  1857. svm->vmcb->save.es = nested_vmcb->save.es;
  1858. svm->vmcb->save.cs = nested_vmcb->save.cs;
  1859. svm->vmcb->save.ss = nested_vmcb->save.ss;
  1860. svm->vmcb->save.ds = nested_vmcb->save.ds;
  1861. svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
  1862. svm->vmcb->save.idtr = nested_vmcb->save.idtr;
  1863. svm->vmcb->save.rflags = nested_vmcb->save.rflags;
  1864. svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
  1865. svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
  1866. svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
  1867. if (npt_enabled) {
  1868. svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
  1869. svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
  1870. } else
  1871. (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
  1872. /* Guest paging mode is active - reset mmu */
  1873. kvm_mmu_reset_context(&svm->vcpu);
  1874. svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
  1875. kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
  1876. kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
  1877. kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
  1878. /* In case we don't even reach vcpu_run, the fields are not updated */
  1879. svm->vmcb->save.rax = nested_vmcb->save.rax;
  1880. svm->vmcb->save.rsp = nested_vmcb->save.rsp;
  1881. svm->vmcb->save.rip = nested_vmcb->save.rip;
  1882. svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
  1883. svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
  1884. svm->vmcb->save.cpl = nested_vmcb->save.cpl;
  1885. svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
  1886. svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
  1887. /* cache intercepts */
  1888. svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
  1889. svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
  1890. svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
  1891. svm->nested.intercept = nested_vmcb->control.intercept;
  1892. force_new_asid(&svm->vcpu);
  1893. svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
  1894. if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
  1895. svm->vcpu.arch.hflags |= HF_VINTR_MASK;
  1896. else
  1897. svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
  1898. if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
  1899. /* We only want the cr8 intercept bits of the guest */
  1900. clr_cr_intercept(svm, INTERCEPT_CR8_READ);
  1901. clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  1902. }
  1903. /* We don't want to see VMMCALLs from a nested guest */
  1904. clr_intercept(svm, INTERCEPT_VMMCALL);
  1905. svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
  1906. svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
  1907. svm->vmcb->control.int_state = nested_vmcb->control.int_state;
  1908. svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
  1909. svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
  1910. svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
  1911. nested_svm_unmap(page);
  1912. /* Enter Guest-Mode */
  1913. enter_guest_mode(&svm->vcpu);
  1914. /*
  1915. * Merge guest and host intercepts - must be called with vcpu in
  1916. * guest-mode to take affect here
  1917. */
  1918. recalc_intercepts(svm);
  1919. svm->nested.vmcb = vmcb_gpa;
  1920. enable_gif(svm);
  1921. mark_all_dirty(svm->vmcb);
  1922. return true;
  1923. }
  1924. static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
  1925. {
  1926. to_vmcb->save.fs = from_vmcb->save.fs;
  1927. to_vmcb->save.gs = from_vmcb->save.gs;
  1928. to_vmcb->save.tr = from_vmcb->save.tr;
  1929. to_vmcb->save.ldtr = from_vmcb->save.ldtr;
  1930. to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
  1931. to_vmcb->save.star = from_vmcb->save.star;
  1932. to_vmcb->save.lstar = from_vmcb->save.lstar;
  1933. to_vmcb->save.cstar = from_vmcb->save.cstar;
  1934. to_vmcb->save.sfmask = from_vmcb->save.sfmask;
  1935. to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
  1936. to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
  1937. to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
  1938. }
  1939. static int vmload_interception(struct vcpu_svm *svm)
  1940. {
  1941. struct vmcb *nested_vmcb;
  1942. struct page *page;
  1943. if (nested_svm_check_permissions(svm))
  1944. return 1;
  1945. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1946. skip_emulated_instruction(&svm->vcpu);
  1947. nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
  1948. if (!nested_vmcb)
  1949. return 1;
  1950. nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
  1951. nested_svm_unmap(page);
  1952. return 1;
  1953. }
  1954. static int vmsave_interception(struct vcpu_svm *svm)
  1955. {
  1956. struct vmcb *nested_vmcb;
  1957. struct page *page;
  1958. if (nested_svm_check_permissions(svm))
  1959. return 1;
  1960. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1961. skip_emulated_instruction(&svm->vcpu);
  1962. nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
  1963. if (!nested_vmcb)
  1964. return 1;
  1965. nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
  1966. nested_svm_unmap(page);
  1967. return 1;
  1968. }
  1969. static int vmrun_interception(struct vcpu_svm *svm)
  1970. {
  1971. if (nested_svm_check_permissions(svm))
  1972. return 1;
  1973. /* Save rip after vmrun instruction */
  1974. kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
  1975. if (!nested_svm_vmrun(svm))
  1976. return 1;
  1977. if (!nested_svm_vmrun_msrpm(svm))
  1978. goto failed;
  1979. return 1;
  1980. failed:
  1981. svm->vmcb->control.exit_code = SVM_EXIT_ERR;
  1982. svm->vmcb->control.exit_code_hi = 0;
  1983. svm->vmcb->control.exit_info_1 = 0;
  1984. svm->vmcb->control.exit_info_2 = 0;
  1985. nested_svm_vmexit(svm);
  1986. return 1;
  1987. }
  1988. static int stgi_interception(struct vcpu_svm *svm)
  1989. {
  1990. if (nested_svm_check_permissions(svm))
  1991. return 1;
  1992. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1993. skip_emulated_instruction(&svm->vcpu);
  1994. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  1995. enable_gif(svm);
  1996. return 1;
  1997. }
  1998. static int clgi_interception(struct vcpu_svm *svm)
  1999. {
  2000. if (nested_svm_check_permissions(svm))
  2001. return 1;
  2002. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2003. skip_emulated_instruction(&svm->vcpu);
  2004. disable_gif(svm);
  2005. /* After a CLGI no interrupts should come */
  2006. svm_clear_vintr(svm);
  2007. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  2008. mark_dirty(svm->vmcb, VMCB_INTR);
  2009. return 1;
  2010. }
  2011. static int invlpga_interception(struct vcpu_svm *svm)
  2012. {
  2013. struct kvm_vcpu *vcpu = &svm->vcpu;
  2014. trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
  2015. vcpu->arch.regs[VCPU_REGS_RAX]);
  2016. /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
  2017. kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
  2018. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2019. skip_emulated_instruction(&svm->vcpu);
  2020. return 1;
  2021. }
  2022. static int skinit_interception(struct vcpu_svm *svm)
  2023. {
  2024. trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
  2025. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  2026. return 1;
  2027. }
  2028. static int invalid_op_interception(struct vcpu_svm *svm)
  2029. {
  2030. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  2031. return 1;
  2032. }
  2033. static int task_switch_interception(struct vcpu_svm *svm)
  2034. {
  2035. u16 tss_selector;
  2036. int reason;
  2037. int int_type = svm->vmcb->control.exit_int_info &
  2038. SVM_EXITINTINFO_TYPE_MASK;
  2039. int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
  2040. uint32_t type =
  2041. svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
  2042. uint32_t idt_v =
  2043. svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
  2044. bool has_error_code = false;
  2045. u32 error_code = 0;
  2046. tss_selector = (u16)svm->vmcb->control.exit_info_1;
  2047. if (svm->vmcb->control.exit_info_2 &
  2048. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
  2049. reason = TASK_SWITCH_IRET;
  2050. else if (svm->vmcb->control.exit_info_2 &
  2051. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
  2052. reason = TASK_SWITCH_JMP;
  2053. else if (idt_v)
  2054. reason = TASK_SWITCH_GATE;
  2055. else
  2056. reason = TASK_SWITCH_CALL;
  2057. if (reason == TASK_SWITCH_GATE) {
  2058. switch (type) {
  2059. case SVM_EXITINTINFO_TYPE_NMI:
  2060. svm->vcpu.arch.nmi_injected = false;
  2061. break;
  2062. case SVM_EXITINTINFO_TYPE_EXEPT:
  2063. if (svm->vmcb->control.exit_info_2 &
  2064. (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
  2065. has_error_code = true;
  2066. error_code =
  2067. (u32)svm->vmcb->control.exit_info_2;
  2068. }
  2069. kvm_clear_exception_queue(&svm->vcpu);
  2070. break;
  2071. case SVM_EXITINTINFO_TYPE_INTR:
  2072. kvm_clear_interrupt_queue(&svm->vcpu);
  2073. break;
  2074. default:
  2075. break;
  2076. }
  2077. }
  2078. if (reason != TASK_SWITCH_GATE ||
  2079. int_type == SVM_EXITINTINFO_TYPE_SOFT ||
  2080. (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
  2081. (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
  2082. skip_emulated_instruction(&svm->vcpu);
  2083. if (kvm_task_switch(&svm->vcpu, tss_selector, reason,
  2084. has_error_code, error_code) == EMULATE_FAIL) {
  2085. svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  2086. svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  2087. svm->vcpu.run->internal.ndata = 0;
  2088. return 0;
  2089. }
  2090. return 1;
  2091. }
  2092. static int cpuid_interception(struct vcpu_svm *svm)
  2093. {
  2094. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  2095. kvm_emulate_cpuid(&svm->vcpu);
  2096. return 1;
  2097. }
  2098. static int iret_interception(struct vcpu_svm *svm)
  2099. {
  2100. ++svm->vcpu.stat.nmi_window_exits;
  2101. clr_intercept(svm, INTERCEPT_IRET);
  2102. svm->vcpu.arch.hflags |= HF_IRET_MASK;
  2103. return 1;
  2104. }
  2105. static int invlpg_interception(struct vcpu_svm *svm)
  2106. {
  2107. return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
  2108. }
  2109. static int emulate_on_interception(struct vcpu_svm *svm)
  2110. {
  2111. return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
  2112. }
  2113. static int cr0_write_interception(struct vcpu_svm *svm)
  2114. {
  2115. struct kvm_vcpu *vcpu = &svm->vcpu;
  2116. int r;
  2117. r = emulate_instruction(&svm->vcpu, 0, 0, 0);
  2118. if (svm->nested.vmexit_rip) {
  2119. kvm_register_write(vcpu, VCPU_REGS_RIP, svm->nested.vmexit_rip);
  2120. kvm_register_write(vcpu, VCPU_REGS_RSP, svm->nested.vmexit_rsp);
  2121. kvm_register_write(vcpu, VCPU_REGS_RAX, svm->nested.vmexit_rax);
  2122. svm->nested.vmexit_rip = 0;
  2123. }
  2124. return r == EMULATE_DONE;
  2125. }
  2126. static int cr8_write_interception(struct vcpu_svm *svm)
  2127. {
  2128. struct kvm_run *kvm_run = svm->vcpu.run;
  2129. u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
  2130. /* instruction emulation calls kvm_set_cr8() */
  2131. emulate_instruction(&svm->vcpu, 0, 0, 0);
  2132. if (irqchip_in_kernel(svm->vcpu.kvm)) {
  2133. clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  2134. return 1;
  2135. }
  2136. if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
  2137. return 1;
  2138. kvm_run->exit_reason = KVM_EXIT_SET_TPR;
  2139. return 0;
  2140. }
  2141. static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
  2142. {
  2143. struct vcpu_svm *svm = to_svm(vcpu);
  2144. switch (ecx) {
  2145. case MSR_IA32_TSC: {
  2146. struct vmcb *vmcb = get_host_vmcb(svm);
  2147. *data = vmcb->control.tsc_offset + native_read_tsc();
  2148. break;
  2149. }
  2150. case MSR_STAR:
  2151. *data = svm->vmcb->save.star;
  2152. break;
  2153. #ifdef CONFIG_X86_64
  2154. case MSR_LSTAR:
  2155. *data = svm->vmcb->save.lstar;
  2156. break;
  2157. case MSR_CSTAR:
  2158. *data = svm->vmcb->save.cstar;
  2159. break;
  2160. case MSR_KERNEL_GS_BASE:
  2161. *data = svm->vmcb->save.kernel_gs_base;
  2162. break;
  2163. case MSR_SYSCALL_MASK:
  2164. *data = svm->vmcb->save.sfmask;
  2165. break;
  2166. #endif
  2167. case MSR_IA32_SYSENTER_CS:
  2168. *data = svm->vmcb->save.sysenter_cs;
  2169. break;
  2170. case MSR_IA32_SYSENTER_EIP:
  2171. *data = svm->sysenter_eip;
  2172. break;
  2173. case MSR_IA32_SYSENTER_ESP:
  2174. *data = svm->sysenter_esp;
  2175. break;
  2176. /*
  2177. * Nobody will change the following 5 values in the VMCB so we can
  2178. * safely return them on rdmsr. They will always be 0 until LBRV is
  2179. * implemented.
  2180. */
  2181. case MSR_IA32_DEBUGCTLMSR:
  2182. *data = svm->vmcb->save.dbgctl;
  2183. break;
  2184. case MSR_IA32_LASTBRANCHFROMIP:
  2185. *data = svm->vmcb->save.br_from;
  2186. break;
  2187. case MSR_IA32_LASTBRANCHTOIP:
  2188. *data = svm->vmcb->save.br_to;
  2189. break;
  2190. case MSR_IA32_LASTINTFROMIP:
  2191. *data = svm->vmcb->save.last_excp_from;
  2192. break;
  2193. case MSR_IA32_LASTINTTOIP:
  2194. *data = svm->vmcb->save.last_excp_to;
  2195. break;
  2196. case MSR_VM_HSAVE_PA:
  2197. *data = svm->nested.hsave_msr;
  2198. break;
  2199. case MSR_VM_CR:
  2200. *data = svm->nested.vm_cr_msr;
  2201. break;
  2202. case MSR_IA32_UCODE_REV:
  2203. *data = 0x01000065;
  2204. break;
  2205. default:
  2206. return kvm_get_msr_common(vcpu, ecx, data);
  2207. }
  2208. return 0;
  2209. }
  2210. static int rdmsr_interception(struct vcpu_svm *svm)
  2211. {
  2212. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  2213. u64 data;
  2214. if (svm_get_msr(&svm->vcpu, ecx, &data)) {
  2215. trace_kvm_msr_read_ex(ecx);
  2216. kvm_inject_gp(&svm->vcpu, 0);
  2217. } else {
  2218. trace_kvm_msr_read(ecx, data);
  2219. svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
  2220. svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
  2221. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  2222. skip_emulated_instruction(&svm->vcpu);
  2223. }
  2224. return 1;
  2225. }
  2226. static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
  2227. {
  2228. struct vcpu_svm *svm = to_svm(vcpu);
  2229. int svm_dis, chg_mask;
  2230. if (data & ~SVM_VM_CR_VALID_MASK)
  2231. return 1;
  2232. chg_mask = SVM_VM_CR_VALID_MASK;
  2233. if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
  2234. chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
  2235. svm->nested.vm_cr_msr &= ~chg_mask;
  2236. svm->nested.vm_cr_msr |= (data & chg_mask);
  2237. svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
  2238. /* check for svm_disable while efer.svme is set */
  2239. if (svm_dis && (vcpu->arch.efer & EFER_SVME))
  2240. return 1;
  2241. return 0;
  2242. }
  2243. static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
  2244. {
  2245. struct vcpu_svm *svm = to_svm(vcpu);
  2246. switch (ecx) {
  2247. case MSR_IA32_TSC:
  2248. kvm_write_tsc(vcpu, data);
  2249. break;
  2250. case MSR_STAR:
  2251. svm->vmcb->save.star = data;
  2252. break;
  2253. #ifdef CONFIG_X86_64
  2254. case MSR_LSTAR:
  2255. svm->vmcb->save.lstar = data;
  2256. break;
  2257. case MSR_CSTAR:
  2258. svm->vmcb->save.cstar = data;
  2259. break;
  2260. case MSR_KERNEL_GS_BASE:
  2261. svm->vmcb->save.kernel_gs_base = data;
  2262. break;
  2263. case MSR_SYSCALL_MASK:
  2264. svm->vmcb->save.sfmask = data;
  2265. break;
  2266. #endif
  2267. case MSR_IA32_SYSENTER_CS:
  2268. svm->vmcb->save.sysenter_cs = data;
  2269. break;
  2270. case MSR_IA32_SYSENTER_EIP:
  2271. svm->sysenter_eip = data;
  2272. svm->vmcb->save.sysenter_eip = data;
  2273. break;
  2274. case MSR_IA32_SYSENTER_ESP:
  2275. svm->sysenter_esp = data;
  2276. svm->vmcb->save.sysenter_esp = data;
  2277. break;
  2278. case MSR_IA32_DEBUGCTLMSR:
  2279. if (!boot_cpu_has(X86_FEATURE_LBRV)) {
  2280. pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
  2281. __func__, data);
  2282. break;
  2283. }
  2284. if (data & DEBUGCTL_RESERVED_BITS)
  2285. return 1;
  2286. svm->vmcb->save.dbgctl = data;
  2287. if (data & (1ULL<<0))
  2288. svm_enable_lbrv(svm);
  2289. else
  2290. svm_disable_lbrv(svm);
  2291. break;
  2292. case MSR_VM_HSAVE_PA:
  2293. svm->nested.hsave_msr = data;
  2294. break;
  2295. case MSR_VM_CR:
  2296. return svm_set_vm_cr(vcpu, data);
  2297. case MSR_VM_IGNNE:
  2298. pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
  2299. break;
  2300. default:
  2301. return kvm_set_msr_common(vcpu, ecx, data);
  2302. }
  2303. return 0;
  2304. }
  2305. static int wrmsr_interception(struct vcpu_svm *svm)
  2306. {
  2307. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  2308. u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
  2309. | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  2310. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  2311. if (svm_set_msr(&svm->vcpu, ecx, data)) {
  2312. trace_kvm_msr_write_ex(ecx, data);
  2313. kvm_inject_gp(&svm->vcpu, 0);
  2314. } else {
  2315. trace_kvm_msr_write(ecx, data);
  2316. skip_emulated_instruction(&svm->vcpu);
  2317. }
  2318. return 1;
  2319. }
  2320. static int msr_interception(struct vcpu_svm *svm)
  2321. {
  2322. if (svm->vmcb->control.exit_info_1)
  2323. return wrmsr_interception(svm);
  2324. else
  2325. return rdmsr_interception(svm);
  2326. }
  2327. static int interrupt_window_interception(struct vcpu_svm *svm)
  2328. {
  2329. struct kvm_run *kvm_run = svm->vcpu.run;
  2330. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  2331. svm_clear_vintr(svm);
  2332. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  2333. mark_dirty(svm->vmcb, VMCB_INTR);
  2334. /*
  2335. * If the user space waits to inject interrupts, exit as soon as
  2336. * possible
  2337. */
  2338. if (!irqchip_in_kernel(svm->vcpu.kvm) &&
  2339. kvm_run->request_interrupt_window &&
  2340. !kvm_cpu_has_interrupt(&svm->vcpu)) {
  2341. ++svm->vcpu.stat.irq_window_exits;
  2342. kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  2343. return 0;
  2344. }
  2345. return 1;
  2346. }
  2347. static int pause_interception(struct vcpu_svm *svm)
  2348. {
  2349. kvm_vcpu_on_spin(&(svm->vcpu));
  2350. return 1;
  2351. }
  2352. static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
  2353. [SVM_EXIT_READ_CR0] = emulate_on_interception,
  2354. [SVM_EXIT_READ_CR3] = emulate_on_interception,
  2355. [SVM_EXIT_READ_CR4] = emulate_on_interception,
  2356. [SVM_EXIT_READ_CR8] = emulate_on_interception,
  2357. [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
  2358. [SVM_EXIT_WRITE_CR0] = cr0_write_interception,
  2359. [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
  2360. [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
  2361. [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
  2362. [SVM_EXIT_READ_DR0] = emulate_on_interception,
  2363. [SVM_EXIT_READ_DR1] = emulate_on_interception,
  2364. [SVM_EXIT_READ_DR2] = emulate_on_interception,
  2365. [SVM_EXIT_READ_DR3] = emulate_on_interception,
  2366. [SVM_EXIT_READ_DR4] = emulate_on_interception,
  2367. [SVM_EXIT_READ_DR5] = emulate_on_interception,
  2368. [SVM_EXIT_READ_DR6] = emulate_on_interception,
  2369. [SVM_EXIT_READ_DR7] = emulate_on_interception,
  2370. [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
  2371. [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
  2372. [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
  2373. [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
  2374. [SVM_EXIT_WRITE_DR4] = emulate_on_interception,
  2375. [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
  2376. [SVM_EXIT_WRITE_DR6] = emulate_on_interception,
  2377. [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
  2378. [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
  2379. [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
  2380. [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
  2381. [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
  2382. [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
  2383. [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
  2384. [SVM_EXIT_INTR] = intr_interception,
  2385. [SVM_EXIT_NMI] = nmi_interception,
  2386. [SVM_EXIT_SMI] = nop_on_interception,
  2387. [SVM_EXIT_INIT] = nop_on_interception,
  2388. [SVM_EXIT_VINTR] = interrupt_window_interception,
  2389. [SVM_EXIT_CPUID] = cpuid_interception,
  2390. [SVM_EXIT_IRET] = iret_interception,
  2391. [SVM_EXIT_INVD] = emulate_on_interception,
  2392. [SVM_EXIT_PAUSE] = pause_interception,
  2393. [SVM_EXIT_HLT] = halt_interception,
  2394. [SVM_EXIT_INVLPG] = invlpg_interception,
  2395. [SVM_EXIT_INVLPGA] = invlpga_interception,
  2396. [SVM_EXIT_IOIO] = io_interception,
  2397. [SVM_EXIT_MSR] = msr_interception,
  2398. [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
  2399. [SVM_EXIT_SHUTDOWN] = shutdown_interception,
  2400. [SVM_EXIT_VMRUN] = vmrun_interception,
  2401. [SVM_EXIT_VMMCALL] = vmmcall_interception,
  2402. [SVM_EXIT_VMLOAD] = vmload_interception,
  2403. [SVM_EXIT_VMSAVE] = vmsave_interception,
  2404. [SVM_EXIT_STGI] = stgi_interception,
  2405. [SVM_EXIT_CLGI] = clgi_interception,
  2406. [SVM_EXIT_SKINIT] = skinit_interception,
  2407. [SVM_EXIT_WBINVD] = emulate_on_interception,
  2408. [SVM_EXIT_MONITOR] = invalid_op_interception,
  2409. [SVM_EXIT_MWAIT] = invalid_op_interception,
  2410. [SVM_EXIT_NPF] = pf_interception,
  2411. };
  2412. void dump_vmcb(struct kvm_vcpu *vcpu)
  2413. {
  2414. struct vcpu_svm *svm = to_svm(vcpu);
  2415. struct vmcb_control_area *control = &svm->vmcb->control;
  2416. struct vmcb_save_area *save = &svm->vmcb->save;
  2417. pr_err("VMCB Control Area:\n");
  2418. pr_err("cr_read: %04x\n", control->intercept_cr & 0xffff);
  2419. pr_err("cr_write: %04x\n", control->intercept_cr >> 16);
  2420. pr_err("dr_read: %04x\n", control->intercept_dr & 0xffff);
  2421. pr_err("dr_write: %04x\n", control->intercept_dr >> 16);
  2422. pr_err("exceptions: %08x\n", control->intercept_exceptions);
  2423. pr_err("intercepts: %016llx\n", control->intercept);
  2424. pr_err("pause filter count: %d\n", control->pause_filter_count);
  2425. pr_err("iopm_base_pa: %016llx\n", control->iopm_base_pa);
  2426. pr_err("msrpm_base_pa: %016llx\n", control->msrpm_base_pa);
  2427. pr_err("tsc_offset: %016llx\n", control->tsc_offset);
  2428. pr_err("asid: %d\n", control->asid);
  2429. pr_err("tlb_ctl: %d\n", control->tlb_ctl);
  2430. pr_err("int_ctl: %08x\n", control->int_ctl);
  2431. pr_err("int_vector: %08x\n", control->int_vector);
  2432. pr_err("int_state: %08x\n", control->int_state);
  2433. pr_err("exit_code: %08x\n", control->exit_code);
  2434. pr_err("exit_info1: %016llx\n", control->exit_info_1);
  2435. pr_err("exit_info2: %016llx\n", control->exit_info_2);
  2436. pr_err("exit_int_info: %08x\n", control->exit_int_info);
  2437. pr_err("exit_int_info_err: %08x\n", control->exit_int_info_err);
  2438. pr_err("nested_ctl: %lld\n", control->nested_ctl);
  2439. pr_err("nested_cr3: %016llx\n", control->nested_cr3);
  2440. pr_err("event_inj: %08x\n", control->event_inj);
  2441. pr_err("event_inj_err: %08x\n", control->event_inj_err);
  2442. pr_err("lbr_ctl: %lld\n", control->lbr_ctl);
  2443. pr_err("next_rip: %016llx\n", control->next_rip);
  2444. pr_err("VMCB State Save Area:\n");
  2445. pr_err("es: s: %04x a: %04x l: %08x b: %016llx\n",
  2446. save->es.selector, save->es.attrib,
  2447. save->es.limit, save->es.base);
  2448. pr_err("cs: s: %04x a: %04x l: %08x b: %016llx\n",
  2449. save->cs.selector, save->cs.attrib,
  2450. save->cs.limit, save->cs.base);
  2451. pr_err("ss: s: %04x a: %04x l: %08x b: %016llx\n",
  2452. save->ss.selector, save->ss.attrib,
  2453. save->ss.limit, save->ss.base);
  2454. pr_err("ds: s: %04x a: %04x l: %08x b: %016llx\n",
  2455. save->ds.selector, save->ds.attrib,
  2456. save->ds.limit, save->ds.base);
  2457. pr_err("fs: s: %04x a: %04x l: %08x b: %016llx\n",
  2458. save->fs.selector, save->fs.attrib,
  2459. save->fs.limit, save->fs.base);
  2460. pr_err("gs: s: %04x a: %04x l: %08x b: %016llx\n",
  2461. save->gs.selector, save->gs.attrib,
  2462. save->gs.limit, save->gs.base);
  2463. pr_err("gdtr: s: %04x a: %04x l: %08x b: %016llx\n",
  2464. save->gdtr.selector, save->gdtr.attrib,
  2465. save->gdtr.limit, save->gdtr.base);
  2466. pr_err("ldtr: s: %04x a: %04x l: %08x b: %016llx\n",
  2467. save->ldtr.selector, save->ldtr.attrib,
  2468. save->ldtr.limit, save->ldtr.base);
  2469. pr_err("idtr: s: %04x a: %04x l: %08x b: %016llx\n",
  2470. save->idtr.selector, save->idtr.attrib,
  2471. save->idtr.limit, save->idtr.base);
  2472. pr_err("tr: s: %04x a: %04x l: %08x b: %016llx\n",
  2473. save->tr.selector, save->tr.attrib,
  2474. save->tr.limit, save->tr.base);
  2475. pr_err("cpl: %d efer: %016llx\n",
  2476. save->cpl, save->efer);
  2477. pr_err("cr0: %016llx cr2: %016llx\n",
  2478. save->cr0, save->cr2);
  2479. pr_err("cr3: %016llx cr4: %016llx\n",
  2480. save->cr3, save->cr4);
  2481. pr_err("dr6: %016llx dr7: %016llx\n",
  2482. save->dr6, save->dr7);
  2483. pr_err("rip: %016llx rflags: %016llx\n",
  2484. save->rip, save->rflags);
  2485. pr_err("rsp: %016llx rax: %016llx\n",
  2486. save->rsp, save->rax);
  2487. pr_err("star: %016llx lstar: %016llx\n",
  2488. save->star, save->lstar);
  2489. pr_err("cstar: %016llx sfmask: %016llx\n",
  2490. save->cstar, save->sfmask);
  2491. pr_err("kernel_gs_base: %016llx sysenter_cs: %016llx\n",
  2492. save->kernel_gs_base, save->sysenter_cs);
  2493. pr_err("sysenter_esp: %016llx sysenter_eip: %016llx\n",
  2494. save->sysenter_esp, save->sysenter_eip);
  2495. pr_err("gpat: %016llx dbgctl: %016llx\n",
  2496. save->g_pat, save->dbgctl);
  2497. pr_err("br_from: %016llx br_to: %016llx\n",
  2498. save->br_from, save->br_to);
  2499. pr_err("excp_from: %016llx excp_to: %016llx\n",
  2500. save->last_excp_from, save->last_excp_to);
  2501. }
  2502. static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
  2503. {
  2504. struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
  2505. *info1 = control->exit_info_1;
  2506. *info2 = control->exit_info_2;
  2507. }
  2508. static int handle_exit(struct kvm_vcpu *vcpu)
  2509. {
  2510. struct vcpu_svm *svm = to_svm(vcpu);
  2511. struct kvm_run *kvm_run = vcpu->run;
  2512. u32 exit_code = svm->vmcb->control.exit_code;
  2513. trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
  2514. if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
  2515. vcpu->arch.cr0 = svm->vmcb->save.cr0;
  2516. if (npt_enabled)
  2517. vcpu->arch.cr3 = svm->vmcb->save.cr3;
  2518. if (unlikely(svm->nested.exit_required)) {
  2519. nested_svm_vmexit(svm);
  2520. svm->nested.exit_required = false;
  2521. return 1;
  2522. }
  2523. if (is_guest_mode(vcpu)) {
  2524. int vmexit;
  2525. trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
  2526. svm->vmcb->control.exit_info_1,
  2527. svm->vmcb->control.exit_info_2,
  2528. svm->vmcb->control.exit_int_info,
  2529. svm->vmcb->control.exit_int_info_err);
  2530. vmexit = nested_svm_exit_special(svm);
  2531. if (vmexit == NESTED_EXIT_CONTINUE)
  2532. vmexit = nested_svm_exit_handled(svm);
  2533. if (vmexit == NESTED_EXIT_DONE)
  2534. return 1;
  2535. }
  2536. svm_complete_interrupts(svm);
  2537. if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
  2538. kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  2539. kvm_run->fail_entry.hardware_entry_failure_reason
  2540. = svm->vmcb->control.exit_code;
  2541. pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
  2542. dump_vmcb(vcpu);
  2543. return 0;
  2544. }
  2545. if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
  2546. exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
  2547. exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
  2548. exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
  2549. printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
  2550. "exit_code 0x%x\n",
  2551. __func__, svm->vmcb->control.exit_int_info,
  2552. exit_code);
  2553. if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
  2554. || !svm_exit_handlers[exit_code]) {
  2555. kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
  2556. kvm_run->hw.hardware_exit_reason = exit_code;
  2557. return 0;
  2558. }
  2559. return svm_exit_handlers[exit_code](svm);
  2560. }
  2561. static void reload_tss(struct kvm_vcpu *vcpu)
  2562. {
  2563. int cpu = raw_smp_processor_id();
  2564. struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
  2565. sd->tss_desc->type = 9; /* available 32/64-bit TSS */
  2566. load_TR_desc();
  2567. }
  2568. static void pre_svm_run(struct vcpu_svm *svm)
  2569. {
  2570. int cpu = raw_smp_processor_id();
  2571. struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
  2572. svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
  2573. /* FIXME: handle wraparound of asid_generation */
  2574. if (svm->asid_generation != sd->asid_generation)
  2575. new_asid(svm, sd);
  2576. }
  2577. static void svm_inject_nmi(struct kvm_vcpu *vcpu)
  2578. {
  2579. struct vcpu_svm *svm = to_svm(vcpu);
  2580. svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
  2581. vcpu->arch.hflags |= HF_NMI_MASK;
  2582. set_intercept(svm, INTERCEPT_IRET);
  2583. ++vcpu->stat.nmi_injections;
  2584. }
  2585. static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
  2586. {
  2587. struct vmcb_control_area *control;
  2588. control = &svm->vmcb->control;
  2589. control->int_vector = irq;
  2590. control->int_ctl &= ~V_INTR_PRIO_MASK;
  2591. control->int_ctl |= V_IRQ_MASK |
  2592. ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
  2593. mark_dirty(svm->vmcb, VMCB_INTR);
  2594. }
  2595. static void svm_set_irq(struct kvm_vcpu *vcpu)
  2596. {
  2597. struct vcpu_svm *svm = to_svm(vcpu);
  2598. BUG_ON(!(gif_set(svm)));
  2599. trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
  2600. ++vcpu->stat.irq_injections;
  2601. svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
  2602. SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
  2603. }
  2604. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  2605. {
  2606. struct vcpu_svm *svm = to_svm(vcpu);
  2607. if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
  2608. return;
  2609. if (irr == -1)
  2610. return;
  2611. if (tpr >= irr)
  2612. set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  2613. }
  2614. static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
  2615. {
  2616. struct vcpu_svm *svm = to_svm(vcpu);
  2617. struct vmcb *vmcb = svm->vmcb;
  2618. int ret;
  2619. ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
  2620. !(svm->vcpu.arch.hflags & HF_NMI_MASK);
  2621. ret = ret && gif_set(svm) && nested_svm_nmi(svm);
  2622. return ret;
  2623. }
  2624. static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
  2625. {
  2626. struct vcpu_svm *svm = to_svm(vcpu);
  2627. return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
  2628. }
  2629. static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  2630. {
  2631. struct vcpu_svm *svm = to_svm(vcpu);
  2632. if (masked) {
  2633. svm->vcpu.arch.hflags |= HF_NMI_MASK;
  2634. set_intercept(svm, INTERCEPT_IRET);
  2635. } else {
  2636. svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
  2637. clr_intercept(svm, INTERCEPT_IRET);
  2638. }
  2639. }
  2640. static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
  2641. {
  2642. struct vcpu_svm *svm = to_svm(vcpu);
  2643. struct vmcb *vmcb = svm->vmcb;
  2644. int ret;
  2645. if (!gif_set(svm) ||
  2646. (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
  2647. return 0;
  2648. ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
  2649. if (is_guest_mode(vcpu))
  2650. return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
  2651. return ret;
  2652. }
  2653. static void enable_irq_window(struct kvm_vcpu *vcpu)
  2654. {
  2655. struct vcpu_svm *svm = to_svm(vcpu);
  2656. /*
  2657. * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
  2658. * 1, because that's a separate STGI/VMRUN intercept. The next time we
  2659. * get that intercept, this function will be called again though and
  2660. * we'll get the vintr intercept.
  2661. */
  2662. if (gif_set(svm) && nested_svm_intr(svm)) {
  2663. svm_set_vintr(svm);
  2664. svm_inject_irq(svm, 0x0);
  2665. }
  2666. }
  2667. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  2668. {
  2669. struct vcpu_svm *svm = to_svm(vcpu);
  2670. if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
  2671. == HF_NMI_MASK)
  2672. return; /* IRET will cause a vm exit */
  2673. /*
  2674. * Something prevents NMI from been injected. Single step over possible
  2675. * problem (IRET or exception injection or interrupt shadow)
  2676. */
  2677. svm->nmi_singlestep = true;
  2678. svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
  2679. update_db_intercept(vcpu);
  2680. }
  2681. static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
  2682. {
  2683. return 0;
  2684. }
  2685. static void svm_flush_tlb(struct kvm_vcpu *vcpu)
  2686. {
  2687. force_new_asid(vcpu);
  2688. }
  2689. static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
  2690. {
  2691. }
  2692. static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
  2693. {
  2694. struct vcpu_svm *svm = to_svm(vcpu);
  2695. if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
  2696. return;
  2697. if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
  2698. int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
  2699. kvm_set_cr8(vcpu, cr8);
  2700. }
  2701. }
  2702. static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
  2703. {
  2704. struct vcpu_svm *svm = to_svm(vcpu);
  2705. u64 cr8;
  2706. if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
  2707. return;
  2708. cr8 = kvm_get_cr8(vcpu);
  2709. svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
  2710. svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
  2711. }
  2712. static void svm_complete_interrupts(struct vcpu_svm *svm)
  2713. {
  2714. u8 vector;
  2715. int type;
  2716. u32 exitintinfo = svm->vmcb->control.exit_int_info;
  2717. unsigned int3_injected = svm->int3_injected;
  2718. svm->int3_injected = 0;
  2719. if (svm->vcpu.arch.hflags & HF_IRET_MASK) {
  2720. svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
  2721. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  2722. }
  2723. svm->vcpu.arch.nmi_injected = false;
  2724. kvm_clear_exception_queue(&svm->vcpu);
  2725. kvm_clear_interrupt_queue(&svm->vcpu);
  2726. if (!(exitintinfo & SVM_EXITINTINFO_VALID))
  2727. return;
  2728. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  2729. vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
  2730. type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
  2731. switch (type) {
  2732. case SVM_EXITINTINFO_TYPE_NMI:
  2733. svm->vcpu.arch.nmi_injected = true;
  2734. break;
  2735. case SVM_EXITINTINFO_TYPE_EXEPT:
  2736. /*
  2737. * In case of software exceptions, do not reinject the vector,
  2738. * but re-execute the instruction instead. Rewind RIP first
  2739. * if we emulated INT3 before.
  2740. */
  2741. if (kvm_exception_is_soft(vector)) {
  2742. if (vector == BP_VECTOR && int3_injected &&
  2743. kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
  2744. kvm_rip_write(&svm->vcpu,
  2745. kvm_rip_read(&svm->vcpu) -
  2746. int3_injected);
  2747. break;
  2748. }
  2749. if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
  2750. u32 err = svm->vmcb->control.exit_int_info_err;
  2751. kvm_requeue_exception_e(&svm->vcpu, vector, err);
  2752. } else
  2753. kvm_requeue_exception(&svm->vcpu, vector);
  2754. break;
  2755. case SVM_EXITINTINFO_TYPE_INTR:
  2756. kvm_queue_interrupt(&svm->vcpu, vector, false);
  2757. break;
  2758. default:
  2759. break;
  2760. }
  2761. }
  2762. static void svm_cancel_injection(struct kvm_vcpu *vcpu)
  2763. {
  2764. struct vcpu_svm *svm = to_svm(vcpu);
  2765. struct vmcb_control_area *control = &svm->vmcb->control;
  2766. control->exit_int_info = control->event_inj;
  2767. control->exit_int_info_err = control->event_inj_err;
  2768. control->event_inj = 0;
  2769. svm_complete_interrupts(svm);
  2770. }
  2771. #ifdef CONFIG_X86_64
  2772. #define R "r"
  2773. #else
  2774. #define R "e"
  2775. #endif
  2776. static void svm_vcpu_run(struct kvm_vcpu *vcpu)
  2777. {
  2778. struct vcpu_svm *svm = to_svm(vcpu);
  2779. svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
  2780. svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
  2781. svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
  2782. /*
  2783. * A vmexit emulation is required before the vcpu can be executed
  2784. * again.
  2785. */
  2786. if (unlikely(svm->nested.exit_required))
  2787. return;
  2788. pre_svm_run(svm);
  2789. sync_lapic_to_cr8(vcpu);
  2790. svm->vmcb->save.cr2 = vcpu->arch.cr2;
  2791. clgi();
  2792. local_irq_enable();
  2793. asm volatile (
  2794. "push %%"R"bp; \n\t"
  2795. "mov %c[rbx](%[svm]), %%"R"bx \n\t"
  2796. "mov %c[rcx](%[svm]), %%"R"cx \n\t"
  2797. "mov %c[rdx](%[svm]), %%"R"dx \n\t"
  2798. "mov %c[rsi](%[svm]), %%"R"si \n\t"
  2799. "mov %c[rdi](%[svm]), %%"R"di \n\t"
  2800. "mov %c[rbp](%[svm]), %%"R"bp \n\t"
  2801. #ifdef CONFIG_X86_64
  2802. "mov %c[r8](%[svm]), %%r8 \n\t"
  2803. "mov %c[r9](%[svm]), %%r9 \n\t"
  2804. "mov %c[r10](%[svm]), %%r10 \n\t"
  2805. "mov %c[r11](%[svm]), %%r11 \n\t"
  2806. "mov %c[r12](%[svm]), %%r12 \n\t"
  2807. "mov %c[r13](%[svm]), %%r13 \n\t"
  2808. "mov %c[r14](%[svm]), %%r14 \n\t"
  2809. "mov %c[r15](%[svm]), %%r15 \n\t"
  2810. #endif
  2811. /* Enter guest mode */
  2812. "push %%"R"ax \n\t"
  2813. "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
  2814. __ex(SVM_VMLOAD) "\n\t"
  2815. __ex(SVM_VMRUN) "\n\t"
  2816. __ex(SVM_VMSAVE) "\n\t"
  2817. "pop %%"R"ax \n\t"
  2818. /* Save guest registers, load host registers */
  2819. "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
  2820. "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
  2821. "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
  2822. "mov %%"R"si, %c[rsi](%[svm]) \n\t"
  2823. "mov %%"R"di, %c[rdi](%[svm]) \n\t"
  2824. "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
  2825. #ifdef CONFIG_X86_64
  2826. "mov %%r8, %c[r8](%[svm]) \n\t"
  2827. "mov %%r9, %c[r9](%[svm]) \n\t"
  2828. "mov %%r10, %c[r10](%[svm]) \n\t"
  2829. "mov %%r11, %c[r11](%[svm]) \n\t"
  2830. "mov %%r12, %c[r12](%[svm]) \n\t"
  2831. "mov %%r13, %c[r13](%[svm]) \n\t"
  2832. "mov %%r14, %c[r14](%[svm]) \n\t"
  2833. "mov %%r15, %c[r15](%[svm]) \n\t"
  2834. #endif
  2835. "pop %%"R"bp"
  2836. :
  2837. : [svm]"a"(svm),
  2838. [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
  2839. [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
  2840. [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
  2841. [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
  2842. [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
  2843. [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
  2844. [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
  2845. #ifdef CONFIG_X86_64
  2846. , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
  2847. [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
  2848. [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
  2849. [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
  2850. [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
  2851. [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
  2852. [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
  2853. [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
  2854. #endif
  2855. : "cc", "memory"
  2856. , R"bx", R"cx", R"dx", R"si", R"di"
  2857. #ifdef CONFIG_X86_64
  2858. , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
  2859. #endif
  2860. );
  2861. #ifdef CONFIG_X86_64
  2862. wrmsrl(MSR_GS_BASE, svm->host.gs_base);
  2863. #else
  2864. loadsegment(fs, svm->host.fs);
  2865. #endif
  2866. reload_tss(vcpu);
  2867. local_irq_disable();
  2868. stgi();
  2869. vcpu->arch.cr2 = svm->vmcb->save.cr2;
  2870. vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
  2871. vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
  2872. vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
  2873. sync_cr8_to_lapic(vcpu);
  2874. svm->next_rip = 0;
  2875. /* if exit due to PF check for async PF */
  2876. if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
  2877. svm->apf_reason = kvm_read_and_reset_pf_reason();
  2878. if (npt_enabled) {
  2879. vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
  2880. vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
  2881. }
  2882. /*
  2883. * We need to handle MC intercepts here before the vcpu has a chance to
  2884. * change the physical cpu
  2885. */
  2886. if (unlikely(svm->vmcb->control.exit_code ==
  2887. SVM_EXIT_EXCP_BASE + MC_VECTOR))
  2888. svm_handle_mce(svm);
  2889. mark_all_clean(svm->vmcb);
  2890. }
  2891. #undef R
  2892. static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  2893. {
  2894. struct vcpu_svm *svm = to_svm(vcpu);
  2895. svm->vmcb->save.cr3 = root;
  2896. force_new_asid(vcpu);
  2897. }
  2898. static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  2899. {
  2900. struct vcpu_svm *svm = to_svm(vcpu);
  2901. svm->vmcb->control.nested_cr3 = root;
  2902. /* Also sync guest cr3 here in case we live migrate */
  2903. svm->vmcb->save.cr3 = vcpu->arch.cr3;
  2904. force_new_asid(vcpu);
  2905. }
  2906. static int is_disabled(void)
  2907. {
  2908. u64 vm_cr;
  2909. rdmsrl(MSR_VM_CR, vm_cr);
  2910. if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
  2911. return 1;
  2912. return 0;
  2913. }
  2914. static void
  2915. svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  2916. {
  2917. /*
  2918. * Patch in the VMMCALL instruction:
  2919. */
  2920. hypercall[0] = 0x0f;
  2921. hypercall[1] = 0x01;
  2922. hypercall[2] = 0xd9;
  2923. }
  2924. static void svm_check_processor_compat(void *rtn)
  2925. {
  2926. *(int *)rtn = 0;
  2927. }
  2928. static bool svm_cpu_has_accelerated_tpr(void)
  2929. {
  2930. return false;
  2931. }
  2932. static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  2933. {
  2934. return 0;
  2935. }
  2936. static void svm_cpuid_update(struct kvm_vcpu *vcpu)
  2937. {
  2938. }
  2939. static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
  2940. {
  2941. switch (func) {
  2942. case 0x00000001:
  2943. /* Mask out xsave bit as long as it is not supported by SVM */
  2944. entry->ecx &= ~(bit(X86_FEATURE_XSAVE));
  2945. break;
  2946. case 0x80000001:
  2947. if (nested)
  2948. entry->ecx |= (1 << 2); /* Set SVM bit */
  2949. break;
  2950. case 0x8000000A:
  2951. entry->eax = 1; /* SVM revision 1 */
  2952. entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
  2953. ASID emulation to nested SVM */
  2954. entry->ecx = 0; /* Reserved */
  2955. entry->edx = 0; /* Per default do not support any
  2956. additional features */
  2957. /* Support next_rip if host supports it */
  2958. if (boot_cpu_has(X86_FEATURE_NRIPS))
  2959. entry->edx |= SVM_FEATURE_NRIP;
  2960. /* Support NPT for the guest if enabled */
  2961. if (npt_enabled)
  2962. entry->edx |= SVM_FEATURE_NPT;
  2963. break;
  2964. }
  2965. }
  2966. static const struct trace_print_flags svm_exit_reasons_str[] = {
  2967. { SVM_EXIT_READ_CR0, "read_cr0" },
  2968. { SVM_EXIT_READ_CR3, "read_cr3" },
  2969. { SVM_EXIT_READ_CR4, "read_cr4" },
  2970. { SVM_EXIT_READ_CR8, "read_cr8" },
  2971. { SVM_EXIT_WRITE_CR0, "write_cr0" },
  2972. { SVM_EXIT_WRITE_CR3, "write_cr3" },
  2973. { SVM_EXIT_WRITE_CR4, "write_cr4" },
  2974. { SVM_EXIT_WRITE_CR8, "write_cr8" },
  2975. { SVM_EXIT_READ_DR0, "read_dr0" },
  2976. { SVM_EXIT_READ_DR1, "read_dr1" },
  2977. { SVM_EXIT_READ_DR2, "read_dr2" },
  2978. { SVM_EXIT_READ_DR3, "read_dr3" },
  2979. { SVM_EXIT_WRITE_DR0, "write_dr0" },
  2980. { SVM_EXIT_WRITE_DR1, "write_dr1" },
  2981. { SVM_EXIT_WRITE_DR2, "write_dr2" },
  2982. { SVM_EXIT_WRITE_DR3, "write_dr3" },
  2983. { SVM_EXIT_WRITE_DR5, "write_dr5" },
  2984. { SVM_EXIT_WRITE_DR7, "write_dr7" },
  2985. { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
  2986. { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
  2987. { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
  2988. { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
  2989. { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
  2990. { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
  2991. { SVM_EXIT_INTR, "interrupt" },
  2992. { SVM_EXIT_NMI, "nmi" },
  2993. { SVM_EXIT_SMI, "smi" },
  2994. { SVM_EXIT_INIT, "init" },
  2995. { SVM_EXIT_VINTR, "vintr" },
  2996. { SVM_EXIT_CPUID, "cpuid" },
  2997. { SVM_EXIT_INVD, "invd" },
  2998. { SVM_EXIT_HLT, "hlt" },
  2999. { SVM_EXIT_INVLPG, "invlpg" },
  3000. { SVM_EXIT_INVLPGA, "invlpga" },
  3001. { SVM_EXIT_IOIO, "io" },
  3002. { SVM_EXIT_MSR, "msr" },
  3003. { SVM_EXIT_TASK_SWITCH, "task_switch" },
  3004. { SVM_EXIT_SHUTDOWN, "shutdown" },
  3005. { SVM_EXIT_VMRUN, "vmrun" },
  3006. { SVM_EXIT_VMMCALL, "hypercall" },
  3007. { SVM_EXIT_VMLOAD, "vmload" },
  3008. { SVM_EXIT_VMSAVE, "vmsave" },
  3009. { SVM_EXIT_STGI, "stgi" },
  3010. { SVM_EXIT_CLGI, "clgi" },
  3011. { SVM_EXIT_SKINIT, "skinit" },
  3012. { SVM_EXIT_WBINVD, "wbinvd" },
  3013. { SVM_EXIT_MONITOR, "monitor" },
  3014. { SVM_EXIT_MWAIT, "mwait" },
  3015. { SVM_EXIT_NPF, "npf" },
  3016. { -1, NULL }
  3017. };
  3018. static int svm_get_lpage_level(void)
  3019. {
  3020. return PT_PDPE_LEVEL;
  3021. }
  3022. static bool svm_rdtscp_supported(void)
  3023. {
  3024. return false;
  3025. }
  3026. static bool svm_has_wbinvd_exit(void)
  3027. {
  3028. return true;
  3029. }
  3030. static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
  3031. {
  3032. struct vcpu_svm *svm = to_svm(vcpu);
  3033. set_exception_intercept(svm, NM_VECTOR);
  3034. update_cr0_intercept(svm);
  3035. }
  3036. static struct kvm_x86_ops svm_x86_ops = {
  3037. .cpu_has_kvm_support = has_svm,
  3038. .disabled_by_bios = is_disabled,
  3039. .hardware_setup = svm_hardware_setup,
  3040. .hardware_unsetup = svm_hardware_unsetup,
  3041. .check_processor_compatibility = svm_check_processor_compat,
  3042. .hardware_enable = svm_hardware_enable,
  3043. .hardware_disable = svm_hardware_disable,
  3044. .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
  3045. .vcpu_create = svm_create_vcpu,
  3046. .vcpu_free = svm_free_vcpu,
  3047. .vcpu_reset = svm_vcpu_reset,
  3048. .prepare_guest_switch = svm_prepare_guest_switch,
  3049. .vcpu_load = svm_vcpu_load,
  3050. .vcpu_put = svm_vcpu_put,
  3051. .set_guest_debug = svm_guest_debug,
  3052. .get_msr = svm_get_msr,
  3053. .set_msr = svm_set_msr,
  3054. .get_segment_base = svm_get_segment_base,
  3055. .get_segment = svm_get_segment,
  3056. .set_segment = svm_set_segment,
  3057. .get_cpl = svm_get_cpl,
  3058. .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
  3059. .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
  3060. .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
  3061. .set_cr0 = svm_set_cr0,
  3062. .set_cr3 = svm_set_cr3,
  3063. .set_cr4 = svm_set_cr4,
  3064. .set_efer = svm_set_efer,
  3065. .get_idt = svm_get_idt,
  3066. .set_idt = svm_set_idt,
  3067. .get_gdt = svm_get_gdt,
  3068. .set_gdt = svm_set_gdt,
  3069. .set_dr7 = svm_set_dr7,
  3070. .cache_reg = svm_cache_reg,
  3071. .get_rflags = svm_get_rflags,
  3072. .set_rflags = svm_set_rflags,
  3073. .fpu_activate = svm_fpu_activate,
  3074. .fpu_deactivate = svm_fpu_deactivate,
  3075. .tlb_flush = svm_flush_tlb,
  3076. .run = svm_vcpu_run,
  3077. .handle_exit = handle_exit,
  3078. .skip_emulated_instruction = skip_emulated_instruction,
  3079. .set_interrupt_shadow = svm_set_interrupt_shadow,
  3080. .get_interrupt_shadow = svm_get_interrupt_shadow,
  3081. .patch_hypercall = svm_patch_hypercall,
  3082. .set_irq = svm_set_irq,
  3083. .set_nmi = svm_inject_nmi,
  3084. .queue_exception = svm_queue_exception,
  3085. .cancel_injection = svm_cancel_injection,
  3086. .interrupt_allowed = svm_interrupt_allowed,
  3087. .nmi_allowed = svm_nmi_allowed,
  3088. .get_nmi_mask = svm_get_nmi_mask,
  3089. .set_nmi_mask = svm_set_nmi_mask,
  3090. .enable_nmi_window = enable_nmi_window,
  3091. .enable_irq_window = enable_irq_window,
  3092. .update_cr8_intercept = update_cr8_intercept,
  3093. .set_tss_addr = svm_set_tss_addr,
  3094. .get_tdp_level = get_npt_level,
  3095. .get_mt_mask = svm_get_mt_mask,
  3096. .get_exit_info = svm_get_exit_info,
  3097. .exit_reasons_str = svm_exit_reasons_str,
  3098. .get_lpage_level = svm_get_lpage_level,
  3099. .cpuid_update = svm_cpuid_update,
  3100. .rdtscp_supported = svm_rdtscp_supported,
  3101. .set_supported_cpuid = svm_set_supported_cpuid,
  3102. .has_wbinvd_exit = svm_has_wbinvd_exit,
  3103. .write_tsc_offset = svm_write_tsc_offset,
  3104. .adjust_tsc_offset = svm_adjust_tsc_offset,
  3105. .set_tdp_cr3 = set_tdp_cr3,
  3106. };
  3107. static int __init svm_init(void)
  3108. {
  3109. return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
  3110. __alignof__(struct vcpu_svm), THIS_MODULE);
  3111. }
  3112. static void __exit svm_exit(void)
  3113. {
  3114. kvm_exit();
  3115. }
  3116. module_init(svm_init)
  3117. module_exit(svm_exit)