svm.c 107 KB

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