svm.c 110 KB

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