svm.c 82 KB

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