svm.c 108 KB

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