svm.c 101 KB

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