svm.c 115 KB

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