svm.c 113 KB

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