svm.c 98 KB

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