svm.c 112 KB

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