svm.c 109 KB

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