svm.c 110 KB

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