svm.c 113 KB

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