svm.c 110 KB

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