svm.c 110 KB

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