svm.c 96 KB

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