svm.c 111 KB

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