svm.c 97 KB

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