svm.c 101 KB

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