svm.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205
  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. #ifdef CONFIG_X86_64
  846. if (vcpu->arch.efer & EFER_LME) {
  847. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  848. vcpu->arch.efer |= EFER_LMA;
  849. svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
  850. }
  851. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
  852. vcpu->arch.efer &= ~EFER_LMA;
  853. svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
  854. }
  855. }
  856. #endif
  857. vcpu->arch.cr0 = cr0;
  858. if (!npt_enabled)
  859. cr0 |= X86_CR0_PG | X86_CR0_WP;
  860. if (!vcpu->fpu_active)
  861. cr0 |= X86_CR0_TS;
  862. /*
  863. * re-enable caching here because the QEMU bios
  864. * does not do it - this results in some delay at
  865. * reboot
  866. */
  867. cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
  868. svm->vmcb->save.cr0 = cr0;
  869. update_cr0_intercept(svm);
  870. }
  871. static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  872. {
  873. unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
  874. unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
  875. if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
  876. force_new_asid(vcpu);
  877. vcpu->arch.cr4 = cr4;
  878. if (!npt_enabled)
  879. cr4 |= X86_CR4_PAE;
  880. cr4 |= host_cr4_mce;
  881. to_svm(vcpu)->vmcb->save.cr4 = cr4;
  882. }
  883. static void svm_set_segment(struct kvm_vcpu *vcpu,
  884. struct kvm_segment *var, int seg)
  885. {
  886. struct vcpu_svm *svm = to_svm(vcpu);
  887. struct vmcb_seg *s = svm_seg(vcpu, seg);
  888. s->base = var->base;
  889. s->limit = var->limit;
  890. s->selector = var->selector;
  891. if (var->unusable)
  892. s->attrib = 0;
  893. else {
  894. s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
  895. s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
  896. s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
  897. s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
  898. s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
  899. s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
  900. s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
  901. s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
  902. }
  903. if (seg == VCPU_SREG_CS)
  904. svm->vmcb->save.cpl
  905. = (svm->vmcb->save.cs.attrib
  906. >> SVM_SELECTOR_DPL_SHIFT) & 3;
  907. }
  908. static void update_db_intercept(struct kvm_vcpu *vcpu)
  909. {
  910. struct vcpu_svm *svm = to_svm(vcpu);
  911. svm->vmcb->control.intercept_exceptions &=
  912. ~((1 << DB_VECTOR) | (1 << BP_VECTOR));
  913. if (svm->nmi_singlestep)
  914. svm->vmcb->control.intercept_exceptions |= (1 << DB_VECTOR);
  915. if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
  916. if (vcpu->guest_debug &
  917. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  918. svm->vmcb->control.intercept_exceptions |=
  919. 1 << DB_VECTOR;
  920. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  921. svm->vmcb->control.intercept_exceptions |=
  922. 1 << BP_VECTOR;
  923. } else
  924. vcpu->guest_debug = 0;
  925. }
  926. static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
  927. {
  928. struct vcpu_svm *svm = to_svm(vcpu);
  929. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
  930. svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
  931. else
  932. svm->vmcb->save.dr7 = vcpu->arch.dr7;
  933. update_db_intercept(vcpu);
  934. }
  935. static void load_host_msrs(struct kvm_vcpu *vcpu)
  936. {
  937. #ifdef CONFIG_X86_64
  938. wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
  939. #endif
  940. }
  941. static void save_host_msrs(struct kvm_vcpu *vcpu)
  942. {
  943. #ifdef CONFIG_X86_64
  944. rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
  945. #endif
  946. }
  947. static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
  948. {
  949. if (sd->next_asid > sd->max_asid) {
  950. ++sd->asid_generation;
  951. sd->next_asid = 1;
  952. svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
  953. }
  954. svm->asid_generation = sd->asid_generation;
  955. svm->vmcb->control.asid = sd->next_asid++;
  956. }
  957. static int svm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *dest)
  958. {
  959. struct vcpu_svm *svm = to_svm(vcpu);
  960. switch (dr) {
  961. case 0 ... 3:
  962. *dest = vcpu->arch.db[dr];
  963. break;
  964. case 4:
  965. if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
  966. return EMULATE_FAIL; /* will re-inject UD */
  967. /* fall through */
  968. case 6:
  969. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
  970. *dest = vcpu->arch.dr6;
  971. else
  972. *dest = svm->vmcb->save.dr6;
  973. break;
  974. case 5:
  975. if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
  976. return EMULATE_FAIL; /* will re-inject UD */
  977. /* fall through */
  978. case 7:
  979. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
  980. *dest = vcpu->arch.dr7;
  981. else
  982. *dest = svm->vmcb->save.dr7;
  983. break;
  984. }
  985. return EMULATE_DONE;
  986. }
  987. static int svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value)
  988. {
  989. struct vcpu_svm *svm = to_svm(vcpu);
  990. switch (dr) {
  991. case 0 ... 3:
  992. vcpu->arch.db[dr] = value;
  993. if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
  994. vcpu->arch.eff_db[dr] = value;
  995. break;
  996. case 4:
  997. if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
  998. return EMULATE_FAIL; /* will re-inject UD */
  999. /* fall through */
  1000. case 6:
  1001. vcpu->arch.dr6 = (value & DR6_VOLATILE) | DR6_FIXED_1;
  1002. break;
  1003. case 5:
  1004. if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
  1005. return EMULATE_FAIL; /* will re-inject UD */
  1006. /* fall through */
  1007. case 7:
  1008. vcpu->arch.dr7 = (value & DR7_VOLATILE) | DR7_FIXED_1;
  1009. if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
  1010. svm->vmcb->save.dr7 = vcpu->arch.dr7;
  1011. vcpu->arch.switch_db_regs = (value & DR7_BP_EN_MASK);
  1012. }
  1013. break;
  1014. }
  1015. return EMULATE_DONE;
  1016. }
  1017. static int pf_interception(struct vcpu_svm *svm)
  1018. {
  1019. u64 fault_address;
  1020. u32 error_code;
  1021. fault_address = svm->vmcb->control.exit_info_2;
  1022. error_code = svm->vmcb->control.exit_info_1;
  1023. trace_kvm_page_fault(fault_address, error_code);
  1024. if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
  1025. kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
  1026. return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
  1027. }
  1028. static int db_interception(struct vcpu_svm *svm)
  1029. {
  1030. struct kvm_run *kvm_run = svm->vcpu.run;
  1031. if (!(svm->vcpu.guest_debug &
  1032. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
  1033. !svm->nmi_singlestep) {
  1034. kvm_queue_exception(&svm->vcpu, DB_VECTOR);
  1035. return 1;
  1036. }
  1037. if (svm->nmi_singlestep) {
  1038. svm->nmi_singlestep = false;
  1039. if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
  1040. svm->vmcb->save.rflags &=
  1041. ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
  1042. update_db_intercept(&svm->vcpu);
  1043. }
  1044. if (svm->vcpu.guest_debug &
  1045. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
  1046. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  1047. kvm_run->debug.arch.pc =
  1048. svm->vmcb->save.cs.base + svm->vmcb->save.rip;
  1049. kvm_run->debug.arch.exception = DB_VECTOR;
  1050. return 0;
  1051. }
  1052. return 1;
  1053. }
  1054. static int bp_interception(struct vcpu_svm *svm)
  1055. {
  1056. struct kvm_run *kvm_run = svm->vcpu.run;
  1057. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  1058. kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
  1059. kvm_run->debug.arch.exception = BP_VECTOR;
  1060. return 0;
  1061. }
  1062. static int ud_interception(struct vcpu_svm *svm)
  1063. {
  1064. int er;
  1065. er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
  1066. if (er != EMULATE_DONE)
  1067. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  1068. return 1;
  1069. }
  1070. static void svm_fpu_activate(struct kvm_vcpu *vcpu)
  1071. {
  1072. struct vcpu_svm *svm = to_svm(vcpu);
  1073. u32 excp;
  1074. if (is_nested(svm)) {
  1075. u32 h_excp, n_excp;
  1076. h_excp = svm->nested.hsave->control.intercept_exceptions;
  1077. n_excp = svm->nested.intercept_exceptions;
  1078. h_excp &= ~(1 << NM_VECTOR);
  1079. excp = h_excp | n_excp;
  1080. } else {
  1081. excp = svm->vmcb->control.intercept_exceptions;
  1082. excp &= ~(1 << NM_VECTOR);
  1083. }
  1084. svm->vmcb->control.intercept_exceptions = excp;
  1085. svm->vcpu.fpu_active = 1;
  1086. update_cr0_intercept(svm);
  1087. }
  1088. static int nm_interception(struct vcpu_svm *svm)
  1089. {
  1090. svm_fpu_activate(&svm->vcpu);
  1091. return 1;
  1092. }
  1093. static int mc_interception(struct vcpu_svm *svm)
  1094. {
  1095. /*
  1096. * On an #MC intercept the MCE handler is not called automatically in
  1097. * the host. So do it by hand here.
  1098. */
  1099. asm volatile (
  1100. "int $0x12\n");
  1101. /* not sure if we ever come back to this point */
  1102. return 1;
  1103. }
  1104. static int shutdown_interception(struct vcpu_svm *svm)
  1105. {
  1106. struct kvm_run *kvm_run = svm->vcpu.run;
  1107. /*
  1108. * VMCB is undefined after a SHUTDOWN intercept
  1109. * so reinitialize it.
  1110. */
  1111. clear_page(svm->vmcb);
  1112. init_vmcb(svm);
  1113. kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
  1114. return 0;
  1115. }
  1116. static int io_interception(struct vcpu_svm *svm)
  1117. {
  1118. u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
  1119. int size, in, string;
  1120. unsigned port;
  1121. ++svm->vcpu.stat.io_exits;
  1122. svm->next_rip = svm->vmcb->control.exit_info_2;
  1123. string = (io_info & SVM_IOIO_STR_MASK) != 0;
  1124. if (string) {
  1125. if (emulate_instruction(&svm->vcpu,
  1126. 0, 0, 0) == EMULATE_DO_MMIO)
  1127. return 0;
  1128. return 1;
  1129. }
  1130. in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
  1131. port = io_info >> 16;
  1132. size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
  1133. skip_emulated_instruction(&svm->vcpu);
  1134. return kvm_emulate_pio(&svm->vcpu, in, size, port);
  1135. }
  1136. static int nmi_interception(struct vcpu_svm *svm)
  1137. {
  1138. return 1;
  1139. }
  1140. static int intr_interception(struct vcpu_svm *svm)
  1141. {
  1142. ++svm->vcpu.stat.irq_exits;
  1143. return 1;
  1144. }
  1145. static int nop_on_interception(struct vcpu_svm *svm)
  1146. {
  1147. return 1;
  1148. }
  1149. static int halt_interception(struct vcpu_svm *svm)
  1150. {
  1151. svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
  1152. skip_emulated_instruction(&svm->vcpu);
  1153. return kvm_emulate_halt(&svm->vcpu);
  1154. }
  1155. static int vmmcall_interception(struct vcpu_svm *svm)
  1156. {
  1157. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1158. skip_emulated_instruction(&svm->vcpu);
  1159. kvm_emulate_hypercall(&svm->vcpu);
  1160. return 1;
  1161. }
  1162. static int nested_svm_check_permissions(struct vcpu_svm *svm)
  1163. {
  1164. if (!(svm->vcpu.arch.efer & EFER_SVME)
  1165. || !is_paging(&svm->vcpu)) {
  1166. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  1167. return 1;
  1168. }
  1169. if (svm->vmcb->save.cpl) {
  1170. kvm_inject_gp(&svm->vcpu, 0);
  1171. return 1;
  1172. }
  1173. return 0;
  1174. }
  1175. static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
  1176. bool has_error_code, u32 error_code)
  1177. {
  1178. int vmexit;
  1179. if (!is_nested(svm))
  1180. return 0;
  1181. svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
  1182. svm->vmcb->control.exit_code_hi = 0;
  1183. svm->vmcb->control.exit_info_1 = error_code;
  1184. svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
  1185. vmexit = nested_svm_intercept(svm);
  1186. if (vmexit == NESTED_EXIT_DONE)
  1187. svm->nested.exit_required = true;
  1188. return vmexit;
  1189. }
  1190. /* This function returns true if it is save to enable the irq window */
  1191. static inline bool nested_svm_intr(struct vcpu_svm *svm)
  1192. {
  1193. if (!is_nested(svm))
  1194. return true;
  1195. if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
  1196. return true;
  1197. if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
  1198. return false;
  1199. svm->vmcb->control.exit_code = SVM_EXIT_INTR;
  1200. if (svm->nested.intercept & 1ULL) {
  1201. /*
  1202. * The #vmexit can't be emulated here directly because this
  1203. * code path runs with irqs and preemtion disabled. A
  1204. * #vmexit emulation might sleep. Only signal request for
  1205. * the #vmexit here.
  1206. */
  1207. svm->nested.exit_required = true;
  1208. trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
  1209. return false;
  1210. }
  1211. return true;
  1212. }
  1213. /* This function returns true if it is save to enable the nmi window */
  1214. static inline bool nested_svm_nmi(struct vcpu_svm *svm)
  1215. {
  1216. if (!is_nested(svm))
  1217. return true;
  1218. if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
  1219. return true;
  1220. svm->vmcb->control.exit_code = SVM_EXIT_NMI;
  1221. svm->nested.exit_required = true;
  1222. return false;
  1223. }
  1224. static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
  1225. {
  1226. struct page *page;
  1227. might_sleep();
  1228. page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
  1229. if (is_error_page(page))
  1230. goto error;
  1231. *_page = page;
  1232. return kmap(page);
  1233. error:
  1234. kvm_release_page_clean(page);
  1235. kvm_inject_gp(&svm->vcpu, 0);
  1236. return NULL;
  1237. }
  1238. static void nested_svm_unmap(struct page *page)
  1239. {
  1240. kunmap(page);
  1241. kvm_release_page_dirty(page);
  1242. }
  1243. static bool nested_svm_exit_handled_msr(struct vcpu_svm *svm)
  1244. {
  1245. u32 param = svm->vmcb->control.exit_info_1 & 1;
  1246. u32 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  1247. bool ret = false;
  1248. u32 t0, t1;
  1249. u8 val;
  1250. if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
  1251. return false;
  1252. switch (msr) {
  1253. case 0 ... 0x1fff:
  1254. t0 = (msr * 2) % 8;
  1255. t1 = msr / 8;
  1256. break;
  1257. case 0xc0000000 ... 0xc0001fff:
  1258. t0 = (8192 + msr - 0xc0000000) * 2;
  1259. t1 = (t0 / 8);
  1260. t0 %= 8;
  1261. break;
  1262. case 0xc0010000 ... 0xc0011fff:
  1263. t0 = (16384 + msr - 0xc0010000) * 2;
  1264. t1 = (t0 / 8);
  1265. t0 %= 8;
  1266. break;
  1267. default:
  1268. ret = true;
  1269. goto out;
  1270. }
  1271. if (!kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + t1, &val, 1))
  1272. ret = val & ((1 << param) << t0);
  1273. out:
  1274. return ret;
  1275. }
  1276. static int nested_svm_exit_special(struct vcpu_svm *svm)
  1277. {
  1278. u32 exit_code = svm->vmcb->control.exit_code;
  1279. switch (exit_code) {
  1280. case SVM_EXIT_INTR:
  1281. case SVM_EXIT_NMI:
  1282. return NESTED_EXIT_HOST;
  1283. case SVM_EXIT_NPF:
  1284. /* For now we are always handling NPFs when using them */
  1285. if (npt_enabled)
  1286. return NESTED_EXIT_HOST;
  1287. break;
  1288. case SVM_EXIT_EXCP_BASE + PF_VECTOR:
  1289. /* When we're shadowing, trap PFs */
  1290. if (!npt_enabled)
  1291. return NESTED_EXIT_HOST;
  1292. break;
  1293. case SVM_EXIT_EXCP_BASE + NM_VECTOR:
  1294. nm_interception(svm);
  1295. break;
  1296. default:
  1297. break;
  1298. }
  1299. return NESTED_EXIT_CONTINUE;
  1300. }
  1301. /*
  1302. * If this function returns true, this #vmexit was already handled
  1303. */
  1304. static int nested_svm_intercept(struct vcpu_svm *svm)
  1305. {
  1306. u32 exit_code = svm->vmcb->control.exit_code;
  1307. int vmexit = NESTED_EXIT_HOST;
  1308. switch (exit_code) {
  1309. case SVM_EXIT_MSR:
  1310. vmexit = nested_svm_exit_handled_msr(svm);
  1311. break;
  1312. case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR8: {
  1313. u32 cr_bits = 1 << (exit_code - SVM_EXIT_READ_CR0);
  1314. if (svm->nested.intercept_cr_read & cr_bits)
  1315. vmexit = NESTED_EXIT_DONE;
  1316. break;
  1317. }
  1318. case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR8: {
  1319. u32 cr_bits = 1 << (exit_code - SVM_EXIT_WRITE_CR0);
  1320. if (svm->nested.intercept_cr_write & cr_bits)
  1321. vmexit = NESTED_EXIT_DONE;
  1322. break;
  1323. }
  1324. case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR7: {
  1325. u32 dr_bits = 1 << (exit_code - SVM_EXIT_READ_DR0);
  1326. if (svm->nested.intercept_dr_read & dr_bits)
  1327. vmexit = NESTED_EXIT_DONE;
  1328. break;
  1329. }
  1330. case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR7: {
  1331. u32 dr_bits = 1 << (exit_code - SVM_EXIT_WRITE_DR0);
  1332. if (svm->nested.intercept_dr_write & dr_bits)
  1333. vmexit = NESTED_EXIT_DONE;
  1334. break;
  1335. }
  1336. case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
  1337. u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
  1338. if (svm->nested.intercept_exceptions & excp_bits)
  1339. vmexit = NESTED_EXIT_DONE;
  1340. break;
  1341. }
  1342. default: {
  1343. u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
  1344. if (svm->nested.intercept & exit_bits)
  1345. vmexit = NESTED_EXIT_DONE;
  1346. }
  1347. }
  1348. return vmexit;
  1349. }
  1350. static int nested_svm_exit_handled(struct vcpu_svm *svm)
  1351. {
  1352. int vmexit;
  1353. vmexit = nested_svm_intercept(svm);
  1354. if (vmexit == NESTED_EXIT_DONE)
  1355. nested_svm_vmexit(svm);
  1356. return vmexit;
  1357. }
  1358. static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
  1359. {
  1360. struct vmcb_control_area *dst = &dst_vmcb->control;
  1361. struct vmcb_control_area *from = &from_vmcb->control;
  1362. dst->intercept_cr_read = from->intercept_cr_read;
  1363. dst->intercept_cr_write = from->intercept_cr_write;
  1364. dst->intercept_dr_read = from->intercept_dr_read;
  1365. dst->intercept_dr_write = from->intercept_dr_write;
  1366. dst->intercept_exceptions = from->intercept_exceptions;
  1367. dst->intercept = from->intercept;
  1368. dst->iopm_base_pa = from->iopm_base_pa;
  1369. dst->msrpm_base_pa = from->msrpm_base_pa;
  1370. dst->tsc_offset = from->tsc_offset;
  1371. dst->asid = from->asid;
  1372. dst->tlb_ctl = from->tlb_ctl;
  1373. dst->int_ctl = from->int_ctl;
  1374. dst->int_vector = from->int_vector;
  1375. dst->int_state = from->int_state;
  1376. dst->exit_code = from->exit_code;
  1377. dst->exit_code_hi = from->exit_code_hi;
  1378. dst->exit_info_1 = from->exit_info_1;
  1379. dst->exit_info_2 = from->exit_info_2;
  1380. dst->exit_int_info = from->exit_int_info;
  1381. dst->exit_int_info_err = from->exit_int_info_err;
  1382. dst->nested_ctl = from->nested_ctl;
  1383. dst->event_inj = from->event_inj;
  1384. dst->event_inj_err = from->event_inj_err;
  1385. dst->nested_cr3 = from->nested_cr3;
  1386. dst->lbr_ctl = from->lbr_ctl;
  1387. }
  1388. static int nested_svm_vmexit(struct vcpu_svm *svm)
  1389. {
  1390. struct vmcb *nested_vmcb;
  1391. struct vmcb *hsave = svm->nested.hsave;
  1392. struct vmcb *vmcb = svm->vmcb;
  1393. struct page *page;
  1394. trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
  1395. vmcb->control.exit_info_1,
  1396. vmcb->control.exit_info_2,
  1397. vmcb->control.exit_int_info,
  1398. vmcb->control.exit_int_info_err);
  1399. nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
  1400. if (!nested_vmcb)
  1401. return 1;
  1402. /* Exit nested SVM mode */
  1403. svm->nested.vmcb = 0;
  1404. /* Give the current vmcb to the guest */
  1405. disable_gif(svm);
  1406. nested_vmcb->save.es = vmcb->save.es;
  1407. nested_vmcb->save.cs = vmcb->save.cs;
  1408. nested_vmcb->save.ss = vmcb->save.ss;
  1409. nested_vmcb->save.ds = vmcb->save.ds;
  1410. nested_vmcb->save.gdtr = vmcb->save.gdtr;
  1411. nested_vmcb->save.idtr = vmcb->save.idtr;
  1412. nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
  1413. if (npt_enabled)
  1414. nested_vmcb->save.cr3 = vmcb->save.cr3;
  1415. else
  1416. nested_vmcb->save.cr3 = svm->vcpu.arch.cr3;
  1417. nested_vmcb->save.cr2 = vmcb->save.cr2;
  1418. nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
  1419. nested_vmcb->save.rflags = vmcb->save.rflags;
  1420. nested_vmcb->save.rip = vmcb->save.rip;
  1421. nested_vmcb->save.rsp = vmcb->save.rsp;
  1422. nested_vmcb->save.rax = vmcb->save.rax;
  1423. nested_vmcb->save.dr7 = vmcb->save.dr7;
  1424. nested_vmcb->save.dr6 = vmcb->save.dr6;
  1425. nested_vmcb->save.cpl = vmcb->save.cpl;
  1426. nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
  1427. nested_vmcb->control.int_vector = vmcb->control.int_vector;
  1428. nested_vmcb->control.int_state = vmcb->control.int_state;
  1429. nested_vmcb->control.exit_code = vmcb->control.exit_code;
  1430. nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
  1431. nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
  1432. nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
  1433. nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
  1434. nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
  1435. /*
  1436. * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
  1437. * to make sure that we do not lose injected events. So check event_inj
  1438. * here and copy it to exit_int_info if it is valid.
  1439. * Exit_int_info and event_inj can't be both valid because the case
  1440. * below only happens on a VMRUN instruction intercept which has
  1441. * no valid exit_int_info set.
  1442. */
  1443. if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
  1444. struct vmcb_control_area *nc = &nested_vmcb->control;
  1445. nc->exit_int_info = vmcb->control.event_inj;
  1446. nc->exit_int_info_err = vmcb->control.event_inj_err;
  1447. }
  1448. nested_vmcb->control.tlb_ctl = 0;
  1449. nested_vmcb->control.event_inj = 0;
  1450. nested_vmcb->control.event_inj_err = 0;
  1451. /* We always set V_INTR_MASKING and remember the old value in hflags */
  1452. if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
  1453. nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
  1454. /* Restore the original control entries */
  1455. copy_vmcb_control_area(vmcb, hsave);
  1456. kvm_clear_exception_queue(&svm->vcpu);
  1457. kvm_clear_interrupt_queue(&svm->vcpu);
  1458. /* Restore selected save entries */
  1459. svm->vmcb->save.es = hsave->save.es;
  1460. svm->vmcb->save.cs = hsave->save.cs;
  1461. svm->vmcb->save.ss = hsave->save.ss;
  1462. svm->vmcb->save.ds = hsave->save.ds;
  1463. svm->vmcb->save.gdtr = hsave->save.gdtr;
  1464. svm->vmcb->save.idtr = hsave->save.idtr;
  1465. svm->vmcb->save.rflags = hsave->save.rflags;
  1466. svm_set_efer(&svm->vcpu, hsave->save.efer);
  1467. svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
  1468. svm_set_cr4(&svm->vcpu, hsave->save.cr4);
  1469. if (npt_enabled) {
  1470. svm->vmcb->save.cr3 = hsave->save.cr3;
  1471. svm->vcpu.arch.cr3 = hsave->save.cr3;
  1472. } else {
  1473. kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
  1474. }
  1475. kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
  1476. kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
  1477. kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
  1478. svm->vmcb->save.dr7 = 0;
  1479. svm->vmcb->save.cpl = 0;
  1480. svm->vmcb->control.exit_int_info = 0;
  1481. nested_svm_unmap(page);
  1482. kvm_mmu_reset_context(&svm->vcpu);
  1483. kvm_mmu_load(&svm->vcpu);
  1484. return 0;
  1485. }
  1486. static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
  1487. {
  1488. u32 *nested_msrpm;
  1489. struct page *page;
  1490. int i;
  1491. nested_msrpm = nested_svm_map(svm, svm->nested.vmcb_msrpm, &page);
  1492. if (!nested_msrpm)
  1493. return false;
  1494. for (i = 0; i < PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER) / 4; i++)
  1495. svm->nested.msrpm[i] = svm->msrpm[i] | nested_msrpm[i];
  1496. svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
  1497. nested_svm_unmap(page);
  1498. return true;
  1499. }
  1500. static bool nested_svm_vmrun(struct vcpu_svm *svm)
  1501. {
  1502. struct vmcb *nested_vmcb;
  1503. struct vmcb *hsave = svm->nested.hsave;
  1504. struct vmcb *vmcb = svm->vmcb;
  1505. struct page *page;
  1506. u64 vmcb_gpa;
  1507. vmcb_gpa = svm->vmcb->save.rax;
  1508. nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
  1509. if (!nested_vmcb)
  1510. return false;
  1511. trace_kvm_nested_vmrun(svm->vmcb->save.rip - 3, vmcb_gpa,
  1512. nested_vmcb->save.rip,
  1513. nested_vmcb->control.int_ctl,
  1514. nested_vmcb->control.event_inj,
  1515. nested_vmcb->control.nested_ctl);
  1516. trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr_read,
  1517. nested_vmcb->control.intercept_cr_write,
  1518. nested_vmcb->control.intercept_exceptions,
  1519. nested_vmcb->control.intercept);
  1520. /* Clear internal status */
  1521. kvm_clear_exception_queue(&svm->vcpu);
  1522. kvm_clear_interrupt_queue(&svm->vcpu);
  1523. /*
  1524. * Save the old vmcb, so we don't need to pick what we save, but can
  1525. * restore everything when a VMEXIT occurs
  1526. */
  1527. hsave->save.es = vmcb->save.es;
  1528. hsave->save.cs = vmcb->save.cs;
  1529. hsave->save.ss = vmcb->save.ss;
  1530. hsave->save.ds = vmcb->save.ds;
  1531. hsave->save.gdtr = vmcb->save.gdtr;
  1532. hsave->save.idtr = vmcb->save.idtr;
  1533. hsave->save.efer = svm->vcpu.arch.efer;
  1534. hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
  1535. hsave->save.cr4 = svm->vcpu.arch.cr4;
  1536. hsave->save.rflags = vmcb->save.rflags;
  1537. hsave->save.rip = svm->next_rip;
  1538. hsave->save.rsp = vmcb->save.rsp;
  1539. hsave->save.rax = vmcb->save.rax;
  1540. if (npt_enabled)
  1541. hsave->save.cr3 = vmcb->save.cr3;
  1542. else
  1543. hsave->save.cr3 = svm->vcpu.arch.cr3;
  1544. copy_vmcb_control_area(hsave, vmcb);
  1545. if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
  1546. svm->vcpu.arch.hflags |= HF_HIF_MASK;
  1547. else
  1548. svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
  1549. /* Load the nested guest state */
  1550. svm->vmcb->save.es = nested_vmcb->save.es;
  1551. svm->vmcb->save.cs = nested_vmcb->save.cs;
  1552. svm->vmcb->save.ss = nested_vmcb->save.ss;
  1553. svm->vmcb->save.ds = nested_vmcb->save.ds;
  1554. svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
  1555. svm->vmcb->save.idtr = nested_vmcb->save.idtr;
  1556. svm->vmcb->save.rflags = nested_vmcb->save.rflags;
  1557. svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
  1558. svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
  1559. svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
  1560. if (npt_enabled) {
  1561. svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
  1562. svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
  1563. } else
  1564. kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
  1565. /* Guest paging mode is active - reset mmu */
  1566. kvm_mmu_reset_context(&svm->vcpu);
  1567. svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
  1568. kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
  1569. kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
  1570. kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
  1571. /* In case we don't even reach vcpu_run, the fields are not updated */
  1572. svm->vmcb->save.rax = nested_vmcb->save.rax;
  1573. svm->vmcb->save.rsp = nested_vmcb->save.rsp;
  1574. svm->vmcb->save.rip = nested_vmcb->save.rip;
  1575. svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
  1576. svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
  1577. svm->vmcb->save.cpl = nested_vmcb->save.cpl;
  1578. svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa;
  1579. /* cache intercepts */
  1580. svm->nested.intercept_cr_read = nested_vmcb->control.intercept_cr_read;
  1581. svm->nested.intercept_cr_write = nested_vmcb->control.intercept_cr_write;
  1582. svm->nested.intercept_dr_read = nested_vmcb->control.intercept_dr_read;
  1583. svm->nested.intercept_dr_write = nested_vmcb->control.intercept_dr_write;
  1584. svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
  1585. svm->nested.intercept = nested_vmcb->control.intercept;
  1586. force_new_asid(&svm->vcpu);
  1587. svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
  1588. if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
  1589. svm->vcpu.arch.hflags |= HF_VINTR_MASK;
  1590. else
  1591. svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
  1592. if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
  1593. /* We only want the cr8 intercept bits of the guest */
  1594. svm->vmcb->control.intercept_cr_read &= ~INTERCEPT_CR8_MASK;
  1595. svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
  1596. }
  1597. /*
  1598. * We don't want a nested guest to be more powerful than the guest, so
  1599. * all intercepts are ORed
  1600. */
  1601. svm->vmcb->control.intercept_cr_read |=
  1602. nested_vmcb->control.intercept_cr_read;
  1603. svm->vmcb->control.intercept_cr_write |=
  1604. nested_vmcb->control.intercept_cr_write;
  1605. svm->vmcb->control.intercept_dr_read |=
  1606. nested_vmcb->control.intercept_dr_read;
  1607. svm->vmcb->control.intercept_dr_write |=
  1608. nested_vmcb->control.intercept_dr_write;
  1609. svm->vmcb->control.intercept_exceptions |=
  1610. nested_vmcb->control.intercept_exceptions;
  1611. svm->vmcb->control.intercept |= nested_vmcb->control.intercept;
  1612. svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
  1613. svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
  1614. svm->vmcb->control.int_state = nested_vmcb->control.int_state;
  1615. svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
  1616. svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
  1617. svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
  1618. nested_svm_unmap(page);
  1619. /* nested_vmcb is our indicator if nested SVM is activated */
  1620. svm->nested.vmcb = vmcb_gpa;
  1621. enable_gif(svm);
  1622. return true;
  1623. }
  1624. static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
  1625. {
  1626. to_vmcb->save.fs = from_vmcb->save.fs;
  1627. to_vmcb->save.gs = from_vmcb->save.gs;
  1628. to_vmcb->save.tr = from_vmcb->save.tr;
  1629. to_vmcb->save.ldtr = from_vmcb->save.ldtr;
  1630. to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
  1631. to_vmcb->save.star = from_vmcb->save.star;
  1632. to_vmcb->save.lstar = from_vmcb->save.lstar;
  1633. to_vmcb->save.cstar = from_vmcb->save.cstar;
  1634. to_vmcb->save.sfmask = from_vmcb->save.sfmask;
  1635. to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
  1636. to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
  1637. to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
  1638. }
  1639. static int vmload_interception(struct vcpu_svm *svm)
  1640. {
  1641. struct vmcb *nested_vmcb;
  1642. struct page *page;
  1643. if (nested_svm_check_permissions(svm))
  1644. return 1;
  1645. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1646. skip_emulated_instruction(&svm->vcpu);
  1647. nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
  1648. if (!nested_vmcb)
  1649. return 1;
  1650. nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
  1651. nested_svm_unmap(page);
  1652. return 1;
  1653. }
  1654. static int vmsave_interception(struct vcpu_svm *svm)
  1655. {
  1656. struct vmcb *nested_vmcb;
  1657. struct page *page;
  1658. if (nested_svm_check_permissions(svm))
  1659. return 1;
  1660. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1661. skip_emulated_instruction(&svm->vcpu);
  1662. nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
  1663. if (!nested_vmcb)
  1664. return 1;
  1665. nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
  1666. nested_svm_unmap(page);
  1667. return 1;
  1668. }
  1669. static int vmrun_interception(struct vcpu_svm *svm)
  1670. {
  1671. if (nested_svm_check_permissions(svm))
  1672. return 1;
  1673. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1674. skip_emulated_instruction(&svm->vcpu);
  1675. if (!nested_svm_vmrun(svm))
  1676. return 1;
  1677. if (!nested_svm_vmrun_msrpm(svm))
  1678. goto failed;
  1679. return 1;
  1680. failed:
  1681. svm->vmcb->control.exit_code = SVM_EXIT_ERR;
  1682. svm->vmcb->control.exit_code_hi = 0;
  1683. svm->vmcb->control.exit_info_1 = 0;
  1684. svm->vmcb->control.exit_info_2 = 0;
  1685. nested_svm_vmexit(svm);
  1686. return 1;
  1687. }
  1688. static int stgi_interception(struct vcpu_svm *svm)
  1689. {
  1690. if (nested_svm_check_permissions(svm))
  1691. return 1;
  1692. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1693. skip_emulated_instruction(&svm->vcpu);
  1694. enable_gif(svm);
  1695. return 1;
  1696. }
  1697. static int clgi_interception(struct vcpu_svm *svm)
  1698. {
  1699. if (nested_svm_check_permissions(svm))
  1700. return 1;
  1701. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1702. skip_emulated_instruction(&svm->vcpu);
  1703. disable_gif(svm);
  1704. /* After a CLGI no interrupts should come */
  1705. svm_clear_vintr(svm);
  1706. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  1707. return 1;
  1708. }
  1709. static int invlpga_interception(struct vcpu_svm *svm)
  1710. {
  1711. struct kvm_vcpu *vcpu = &svm->vcpu;
  1712. trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
  1713. vcpu->arch.regs[VCPU_REGS_RAX]);
  1714. /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
  1715. kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
  1716. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1717. skip_emulated_instruction(&svm->vcpu);
  1718. return 1;
  1719. }
  1720. static int skinit_interception(struct vcpu_svm *svm)
  1721. {
  1722. trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
  1723. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  1724. return 1;
  1725. }
  1726. static int invalid_op_interception(struct vcpu_svm *svm)
  1727. {
  1728. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  1729. return 1;
  1730. }
  1731. static int task_switch_interception(struct vcpu_svm *svm)
  1732. {
  1733. u16 tss_selector;
  1734. int reason;
  1735. int int_type = svm->vmcb->control.exit_int_info &
  1736. SVM_EXITINTINFO_TYPE_MASK;
  1737. int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
  1738. uint32_t type =
  1739. svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
  1740. uint32_t idt_v =
  1741. svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
  1742. tss_selector = (u16)svm->vmcb->control.exit_info_1;
  1743. if (svm->vmcb->control.exit_info_2 &
  1744. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
  1745. reason = TASK_SWITCH_IRET;
  1746. else if (svm->vmcb->control.exit_info_2 &
  1747. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
  1748. reason = TASK_SWITCH_JMP;
  1749. else if (idt_v)
  1750. reason = TASK_SWITCH_GATE;
  1751. else
  1752. reason = TASK_SWITCH_CALL;
  1753. if (reason == TASK_SWITCH_GATE) {
  1754. switch (type) {
  1755. case SVM_EXITINTINFO_TYPE_NMI:
  1756. svm->vcpu.arch.nmi_injected = false;
  1757. break;
  1758. case SVM_EXITINTINFO_TYPE_EXEPT:
  1759. kvm_clear_exception_queue(&svm->vcpu);
  1760. break;
  1761. case SVM_EXITINTINFO_TYPE_INTR:
  1762. kvm_clear_interrupt_queue(&svm->vcpu);
  1763. break;
  1764. default:
  1765. break;
  1766. }
  1767. }
  1768. if (reason != TASK_SWITCH_GATE ||
  1769. int_type == SVM_EXITINTINFO_TYPE_SOFT ||
  1770. (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
  1771. (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
  1772. skip_emulated_instruction(&svm->vcpu);
  1773. return kvm_task_switch(&svm->vcpu, tss_selector, reason);
  1774. }
  1775. static int cpuid_interception(struct vcpu_svm *svm)
  1776. {
  1777. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  1778. kvm_emulate_cpuid(&svm->vcpu);
  1779. return 1;
  1780. }
  1781. static int iret_interception(struct vcpu_svm *svm)
  1782. {
  1783. ++svm->vcpu.stat.nmi_window_exits;
  1784. svm->vmcb->control.intercept &= ~(1UL << INTERCEPT_IRET);
  1785. svm->vcpu.arch.hflags |= HF_IRET_MASK;
  1786. return 1;
  1787. }
  1788. static int invlpg_interception(struct vcpu_svm *svm)
  1789. {
  1790. if (emulate_instruction(&svm->vcpu, 0, 0, 0) != EMULATE_DONE)
  1791. pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
  1792. return 1;
  1793. }
  1794. static int emulate_on_interception(struct vcpu_svm *svm)
  1795. {
  1796. if (emulate_instruction(&svm->vcpu, 0, 0, 0) != EMULATE_DONE)
  1797. pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
  1798. return 1;
  1799. }
  1800. static int cr8_write_interception(struct vcpu_svm *svm)
  1801. {
  1802. struct kvm_run *kvm_run = svm->vcpu.run;
  1803. u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
  1804. /* instruction emulation calls kvm_set_cr8() */
  1805. emulate_instruction(&svm->vcpu, 0, 0, 0);
  1806. if (irqchip_in_kernel(svm->vcpu.kvm)) {
  1807. svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
  1808. return 1;
  1809. }
  1810. if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
  1811. return 1;
  1812. kvm_run->exit_reason = KVM_EXIT_SET_TPR;
  1813. return 0;
  1814. }
  1815. static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
  1816. {
  1817. struct vcpu_svm *svm = to_svm(vcpu);
  1818. switch (ecx) {
  1819. case MSR_IA32_TSC: {
  1820. u64 tsc_offset;
  1821. if (is_nested(svm))
  1822. tsc_offset = svm->nested.hsave->control.tsc_offset;
  1823. else
  1824. tsc_offset = svm->vmcb->control.tsc_offset;
  1825. *data = tsc_offset + native_read_tsc();
  1826. break;
  1827. }
  1828. case MSR_K6_STAR:
  1829. *data = svm->vmcb->save.star;
  1830. break;
  1831. #ifdef CONFIG_X86_64
  1832. case MSR_LSTAR:
  1833. *data = svm->vmcb->save.lstar;
  1834. break;
  1835. case MSR_CSTAR:
  1836. *data = svm->vmcb->save.cstar;
  1837. break;
  1838. case MSR_KERNEL_GS_BASE:
  1839. *data = svm->vmcb->save.kernel_gs_base;
  1840. break;
  1841. case MSR_SYSCALL_MASK:
  1842. *data = svm->vmcb->save.sfmask;
  1843. break;
  1844. #endif
  1845. case MSR_IA32_SYSENTER_CS:
  1846. *data = svm->vmcb->save.sysenter_cs;
  1847. break;
  1848. case MSR_IA32_SYSENTER_EIP:
  1849. *data = svm->sysenter_eip;
  1850. break;
  1851. case MSR_IA32_SYSENTER_ESP:
  1852. *data = svm->sysenter_esp;
  1853. break;
  1854. /*
  1855. * Nobody will change the following 5 values in the VMCB so we can
  1856. * safely return them on rdmsr. They will always be 0 until LBRV is
  1857. * implemented.
  1858. */
  1859. case MSR_IA32_DEBUGCTLMSR:
  1860. *data = svm->vmcb->save.dbgctl;
  1861. break;
  1862. case MSR_IA32_LASTBRANCHFROMIP:
  1863. *data = svm->vmcb->save.br_from;
  1864. break;
  1865. case MSR_IA32_LASTBRANCHTOIP:
  1866. *data = svm->vmcb->save.br_to;
  1867. break;
  1868. case MSR_IA32_LASTINTFROMIP:
  1869. *data = svm->vmcb->save.last_excp_from;
  1870. break;
  1871. case MSR_IA32_LASTINTTOIP:
  1872. *data = svm->vmcb->save.last_excp_to;
  1873. break;
  1874. case MSR_VM_HSAVE_PA:
  1875. *data = svm->nested.hsave_msr;
  1876. break;
  1877. case MSR_VM_CR:
  1878. *data = svm->nested.vm_cr_msr;
  1879. break;
  1880. case MSR_IA32_UCODE_REV:
  1881. *data = 0x01000065;
  1882. break;
  1883. default:
  1884. return kvm_get_msr_common(vcpu, ecx, data);
  1885. }
  1886. return 0;
  1887. }
  1888. static int rdmsr_interception(struct vcpu_svm *svm)
  1889. {
  1890. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  1891. u64 data;
  1892. if (svm_get_msr(&svm->vcpu, ecx, &data)) {
  1893. trace_kvm_msr_read_ex(ecx);
  1894. kvm_inject_gp(&svm->vcpu, 0);
  1895. } else {
  1896. trace_kvm_msr_read(ecx, data);
  1897. svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
  1898. svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
  1899. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  1900. skip_emulated_instruction(&svm->vcpu);
  1901. }
  1902. return 1;
  1903. }
  1904. static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
  1905. {
  1906. struct vcpu_svm *svm = to_svm(vcpu);
  1907. int svm_dis, chg_mask;
  1908. if (data & ~SVM_VM_CR_VALID_MASK)
  1909. return 1;
  1910. chg_mask = SVM_VM_CR_VALID_MASK;
  1911. if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
  1912. chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
  1913. svm->nested.vm_cr_msr &= ~chg_mask;
  1914. svm->nested.vm_cr_msr |= (data & chg_mask);
  1915. svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
  1916. /* check for svm_disable while efer.svme is set */
  1917. if (svm_dis && (vcpu->arch.efer & EFER_SVME))
  1918. return 1;
  1919. return 0;
  1920. }
  1921. static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
  1922. {
  1923. struct vcpu_svm *svm = to_svm(vcpu);
  1924. switch (ecx) {
  1925. case MSR_IA32_TSC: {
  1926. u64 tsc_offset = data - native_read_tsc();
  1927. u64 g_tsc_offset = 0;
  1928. if (is_nested(svm)) {
  1929. g_tsc_offset = svm->vmcb->control.tsc_offset -
  1930. svm->nested.hsave->control.tsc_offset;
  1931. svm->nested.hsave->control.tsc_offset = tsc_offset;
  1932. }
  1933. svm->vmcb->control.tsc_offset = tsc_offset + g_tsc_offset;
  1934. break;
  1935. }
  1936. case MSR_K6_STAR:
  1937. svm->vmcb->save.star = data;
  1938. break;
  1939. #ifdef CONFIG_X86_64
  1940. case MSR_LSTAR:
  1941. svm->vmcb->save.lstar = data;
  1942. break;
  1943. case MSR_CSTAR:
  1944. svm->vmcb->save.cstar = data;
  1945. break;
  1946. case MSR_KERNEL_GS_BASE:
  1947. svm->vmcb->save.kernel_gs_base = data;
  1948. break;
  1949. case MSR_SYSCALL_MASK:
  1950. svm->vmcb->save.sfmask = data;
  1951. break;
  1952. #endif
  1953. case MSR_IA32_SYSENTER_CS:
  1954. svm->vmcb->save.sysenter_cs = data;
  1955. break;
  1956. case MSR_IA32_SYSENTER_EIP:
  1957. svm->sysenter_eip = data;
  1958. svm->vmcb->save.sysenter_eip = data;
  1959. break;
  1960. case MSR_IA32_SYSENTER_ESP:
  1961. svm->sysenter_esp = data;
  1962. svm->vmcb->save.sysenter_esp = data;
  1963. break;
  1964. case MSR_IA32_DEBUGCTLMSR:
  1965. if (!svm_has(SVM_FEATURE_LBRV)) {
  1966. pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
  1967. __func__, data);
  1968. break;
  1969. }
  1970. if (data & DEBUGCTL_RESERVED_BITS)
  1971. return 1;
  1972. svm->vmcb->save.dbgctl = data;
  1973. if (data & (1ULL<<0))
  1974. svm_enable_lbrv(svm);
  1975. else
  1976. svm_disable_lbrv(svm);
  1977. break;
  1978. case MSR_VM_HSAVE_PA:
  1979. svm->nested.hsave_msr = data;
  1980. break;
  1981. case MSR_VM_CR:
  1982. return svm_set_vm_cr(vcpu, data);
  1983. case MSR_VM_IGNNE:
  1984. pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
  1985. break;
  1986. default:
  1987. return kvm_set_msr_common(vcpu, ecx, data);
  1988. }
  1989. return 0;
  1990. }
  1991. static int wrmsr_interception(struct vcpu_svm *svm)
  1992. {
  1993. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  1994. u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
  1995. | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  1996. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  1997. if (svm_set_msr(&svm->vcpu, ecx, data)) {
  1998. trace_kvm_msr_write_ex(ecx, data);
  1999. kvm_inject_gp(&svm->vcpu, 0);
  2000. } else {
  2001. trace_kvm_msr_write(ecx, data);
  2002. skip_emulated_instruction(&svm->vcpu);
  2003. }
  2004. return 1;
  2005. }
  2006. static int msr_interception(struct vcpu_svm *svm)
  2007. {
  2008. if (svm->vmcb->control.exit_info_1)
  2009. return wrmsr_interception(svm);
  2010. else
  2011. return rdmsr_interception(svm);
  2012. }
  2013. static int interrupt_window_interception(struct vcpu_svm *svm)
  2014. {
  2015. struct kvm_run *kvm_run = svm->vcpu.run;
  2016. svm_clear_vintr(svm);
  2017. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  2018. /*
  2019. * If the user space waits to inject interrupts, exit as soon as
  2020. * possible
  2021. */
  2022. if (!irqchip_in_kernel(svm->vcpu.kvm) &&
  2023. kvm_run->request_interrupt_window &&
  2024. !kvm_cpu_has_interrupt(&svm->vcpu)) {
  2025. ++svm->vcpu.stat.irq_window_exits;
  2026. kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  2027. return 0;
  2028. }
  2029. return 1;
  2030. }
  2031. static int pause_interception(struct vcpu_svm *svm)
  2032. {
  2033. kvm_vcpu_on_spin(&(svm->vcpu));
  2034. return 1;
  2035. }
  2036. static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
  2037. [SVM_EXIT_READ_CR0] = emulate_on_interception,
  2038. [SVM_EXIT_READ_CR3] = emulate_on_interception,
  2039. [SVM_EXIT_READ_CR4] = emulate_on_interception,
  2040. [SVM_EXIT_READ_CR8] = emulate_on_interception,
  2041. [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
  2042. [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
  2043. [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
  2044. [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
  2045. [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
  2046. [SVM_EXIT_READ_DR0] = emulate_on_interception,
  2047. [SVM_EXIT_READ_DR1] = emulate_on_interception,
  2048. [SVM_EXIT_READ_DR2] = emulate_on_interception,
  2049. [SVM_EXIT_READ_DR3] = emulate_on_interception,
  2050. [SVM_EXIT_READ_DR4] = emulate_on_interception,
  2051. [SVM_EXIT_READ_DR5] = emulate_on_interception,
  2052. [SVM_EXIT_READ_DR6] = emulate_on_interception,
  2053. [SVM_EXIT_READ_DR7] = emulate_on_interception,
  2054. [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
  2055. [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
  2056. [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
  2057. [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
  2058. [SVM_EXIT_WRITE_DR4] = emulate_on_interception,
  2059. [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
  2060. [SVM_EXIT_WRITE_DR6] = emulate_on_interception,
  2061. [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
  2062. [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
  2063. [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
  2064. [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
  2065. [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
  2066. [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
  2067. [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
  2068. [SVM_EXIT_INTR] = intr_interception,
  2069. [SVM_EXIT_NMI] = nmi_interception,
  2070. [SVM_EXIT_SMI] = nop_on_interception,
  2071. [SVM_EXIT_INIT] = nop_on_interception,
  2072. [SVM_EXIT_VINTR] = interrupt_window_interception,
  2073. [SVM_EXIT_CPUID] = cpuid_interception,
  2074. [SVM_EXIT_IRET] = iret_interception,
  2075. [SVM_EXIT_INVD] = emulate_on_interception,
  2076. [SVM_EXIT_PAUSE] = pause_interception,
  2077. [SVM_EXIT_HLT] = halt_interception,
  2078. [SVM_EXIT_INVLPG] = invlpg_interception,
  2079. [SVM_EXIT_INVLPGA] = invlpga_interception,
  2080. [SVM_EXIT_IOIO] = io_interception,
  2081. [SVM_EXIT_MSR] = msr_interception,
  2082. [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
  2083. [SVM_EXIT_SHUTDOWN] = shutdown_interception,
  2084. [SVM_EXIT_VMRUN] = vmrun_interception,
  2085. [SVM_EXIT_VMMCALL] = vmmcall_interception,
  2086. [SVM_EXIT_VMLOAD] = vmload_interception,
  2087. [SVM_EXIT_VMSAVE] = vmsave_interception,
  2088. [SVM_EXIT_STGI] = stgi_interception,
  2089. [SVM_EXIT_CLGI] = clgi_interception,
  2090. [SVM_EXIT_SKINIT] = skinit_interception,
  2091. [SVM_EXIT_WBINVD] = emulate_on_interception,
  2092. [SVM_EXIT_MONITOR] = invalid_op_interception,
  2093. [SVM_EXIT_MWAIT] = invalid_op_interception,
  2094. [SVM_EXIT_NPF] = pf_interception,
  2095. };
  2096. static int handle_exit(struct kvm_vcpu *vcpu)
  2097. {
  2098. struct vcpu_svm *svm = to_svm(vcpu);
  2099. struct kvm_run *kvm_run = vcpu->run;
  2100. u32 exit_code = svm->vmcb->control.exit_code;
  2101. trace_kvm_exit(exit_code, svm->vmcb->save.rip);
  2102. if (unlikely(svm->nested.exit_required)) {
  2103. nested_svm_vmexit(svm);
  2104. svm->nested.exit_required = false;
  2105. return 1;
  2106. }
  2107. if (is_nested(svm)) {
  2108. int vmexit;
  2109. trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
  2110. svm->vmcb->control.exit_info_1,
  2111. svm->vmcb->control.exit_info_2,
  2112. svm->vmcb->control.exit_int_info,
  2113. svm->vmcb->control.exit_int_info_err);
  2114. vmexit = nested_svm_exit_special(svm);
  2115. if (vmexit == NESTED_EXIT_CONTINUE)
  2116. vmexit = nested_svm_exit_handled(svm);
  2117. if (vmexit == NESTED_EXIT_DONE)
  2118. return 1;
  2119. }
  2120. svm_complete_interrupts(svm);
  2121. if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR0_MASK))
  2122. vcpu->arch.cr0 = svm->vmcb->save.cr0;
  2123. if (npt_enabled)
  2124. vcpu->arch.cr3 = svm->vmcb->save.cr3;
  2125. if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
  2126. kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  2127. kvm_run->fail_entry.hardware_entry_failure_reason
  2128. = svm->vmcb->control.exit_code;
  2129. return 0;
  2130. }
  2131. if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
  2132. exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
  2133. exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH)
  2134. printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
  2135. "exit_code 0x%x\n",
  2136. __func__, svm->vmcb->control.exit_int_info,
  2137. exit_code);
  2138. if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
  2139. || !svm_exit_handlers[exit_code]) {
  2140. kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
  2141. kvm_run->hw.hardware_exit_reason = exit_code;
  2142. return 0;
  2143. }
  2144. return svm_exit_handlers[exit_code](svm);
  2145. }
  2146. static void reload_tss(struct kvm_vcpu *vcpu)
  2147. {
  2148. int cpu = raw_smp_processor_id();
  2149. struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
  2150. sd->tss_desc->type = 9; /* available 32/64-bit TSS */
  2151. load_TR_desc();
  2152. }
  2153. static void pre_svm_run(struct vcpu_svm *svm)
  2154. {
  2155. int cpu = raw_smp_processor_id();
  2156. struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
  2157. svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
  2158. /* FIXME: handle wraparound of asid_generation */
  2159. if (svm->asid_generation != sd->asid_generation)
  2160. new_asid(svm, sd);
  2161. }
  2162. static void svm_inject_nmi(struct kvm_vcpu *vcpu)
  2163. {
  2164. struct vcpu_svm *svm = to_svm(vcpu);
  2165. svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
  2166. vcpu->arch.hflags |= HF_NMI_MASK;
  2167. svm->vmcb->control.intercept |= (1UL << INTERCEPT_IRET);
  2168. ++vcpu->stat.nmi_injections;
  2169. }
  2170. static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
  2171. {
  2172. struct vmcb_control_area *control;
  2173. trace_kvm_inj_virq(irq);
  2174. ++svm->vcpu.stat.irq_injections;
  2175. control = &svm->vmcb->control;
  2176. control->int_vector = irq;
  2177. control->int_ctl &= ~V_INTR_PRIO_MASK;
  2178. control->int_ctl |= V_IRQ_MASK |
  2179. ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
  2180. }
  2181. static void svm_set_irq(struct kvm_vcpu *vcpu)
  2182. {
  2183. struct vcpu_svm *svm = to_svm(vcpu);
  2184. BUG_ON(!(gif_set(svm)));
  2185. svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
  2186. SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
  2187. }
  2188. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  2189. {
  2190. struct vcpu_svm *svm = to_svm(vcpu);
  2191. if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
  2192. return;
  2193. if (irr == -1)
  2194. return;
  2195. if (tpr >= irr)
  2196. svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
  2197. }
  2198. static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
  2199. {
  2200. struct vcpu_svm *svm = to_svm(vcpu);
  2201. struct vmcb *vmcb = svm->vmcb;
  2202. return !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
  2203. !(svm->vcpu.arch.hflags & HF_NMI_MASK);
  2204. }
  2205. static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
  2206. {
  2207. struct vcpu_svm *svm = to_svm(vcpu);
  2208. return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
  2209. }
  2210. static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  2211. {
  2212. struct vcpu_svm *svm = to_svm(vcpu);
  2213. if (masked) {
  2214. svm->vcpu.arch.hflags |= HF_NMI_MASK;
  2215. svm->vmcb->control.intercept |= (1UL << INTERCEPT_IRET);
  2216. } else {
  2217. svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
  2218. svm->vmcb->control.intercept &= ~(1UL << INTERCEPT_IRET);
  2219. }
  2220. }
  2221. static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
  2222. {
  2223. struct vcpu_svm *svm = to_svm(vcpu);
  2224. struct vmcb *vmcb = svm->vmcb;
  2225. int ret;
  2226. if (!gif_set(svm) ||
  2227. (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
  2228. return 0;
  2229. ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
  2230. if (is_nested(svm))
  2231. return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
  2232. return ret;
  2233. }
  2234. static void enable_irq_window(struct kvm_vcpu *vcpu)
  2235. {
  2236. struct vcpu_svm *svm = to_svm(vcpu);
  2237. /*
  2238. * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
  2239. * 1, because that's a separate STGI/VMRUN intercept. The next time we
  2240. * get that intercept, this function will be called again though and
  2241. * we'll get the vintr intercept.
  2242. */
  2243. if (gif_set(svm) && nested_svm_intr(svm)) {
  2244. svm_set_vintr(svm);
  2245. svm_inject_irq(svm, 0x0);
  2246. }
  2247. }
  2248. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  2249. {
  2250. struct vcpu_svm *svm = to_svm(vcpu);
  2251. if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
  2252. == HF_NMI_MASK)
  2253. return; /* IRET will cause a vm exit */
  2254. /*
  2255. * Something prevents NMI from been injected. Single step over possible
  2256. * problem (IRET or exception injection or interrupt shadow)
  2257. */
  2258. if (gif_set(svm) && nested_svm_nmi(svm)) {
  2259. svm->nmi_singlestep = true;
  2260. svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
  2261. update_db_intercept(vcpu);
  2262. }
  2263. }
  2264. static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
  2265. {
  2266. return 0;
  2267. }
  2268. static void svm_flush_tlb(struct kvm_vcpu *vcpu)
  2269. {
  2270. force_new_asid(vcpu);
  2271. }
  2272. static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
  2273. {
  2274. }
  2275. static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
  2276. {
  2277. struct vcpu_svm *svm = to_svm(vcpu);
  2278. if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
  2279. return;
  2280. if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) {
  2281. int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
  2282. kvm_set_cr8(vcpu, cr8);
  2283. }
  2284. }
  2285. static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
  2286. {
  2287. struct vcpu_svm *svm = to_svm(vcpu);
  2288. u64 cr8;
  2289. if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
  2290. return;
  2291. cr8 = kvm_get_cr8(vcpu);
  2292. svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
  2293. svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
  2294. }
  2295. static void svm_complete_interrupts(struct vcpu_svm *svm)
  2296. {
  2297. u8 vector;
  2298. int type;
  2299. u32 exitintinfo = svm->vmcb->control.exit_int_info;
  2300. unsigned int3_injected = svm->int3_injected;
  2301. svm->int3_injected = 0;
  2302. if (svm->vcpu.arch.hflags & HF_IRET_MASK)
  2303. svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
  2304. svm->vcpu.arch.nmi_injected = false;
  2305. kvm_clear_exception_queue(&svm->vcpu);
  2306. kvm_clear_interrupt_queue(&svm->vcpu);
  2307. if (!(exitintinfo & SVM_EXITINTINFO_VALID))
  2308. return;
  2309. vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
  2310. type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
  2311. switch (type) {
  2312. case SVM_EXITINTINFO_TYPE_NMI:
  2313. svm->vcpu.arch.nmi_injected = true;
  2314. break;
  2315. case SVM_EXITINTINFO_TYPE_EXEPT:
  2316. if (is_nested(svm))
  2317. break;
  2318. /*
  2319. * In case of software exceptions, do not reinject the vector,
  2320. * but re-execute the instruction instead. Rewind RIP first
  2321. * if we emulated INT3 before.
  2322. */
  2323. if (kvm_exception_is_soft(vector)) {
  2324. if (vector == BP_VECTOR && int3_injected &&
  2325. kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
  2326. kvm_rip_write(&svm->vcpu,
  2327. kvm_rip_read(&svm->vcpu) -
  2328. int3_injected);
  2329. break;
  2330. }
  2331. if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
  2332. u32 err = svm->vmcb->control.exit_int_info_err;
  2333. kvm_queue_exception_e(&svm->vcpu, vector, err);
  2334. } else
  2335. kvm_queue_exception(&svm->vcpu, vector);
  2336. break;
  2337. case SVM_EXITINTINFO_TYPE_INTR:
  2338. kvm_queue_interrupt(&svm->vcpu, vector, false);
  2339. break;
  2340. default:
  2341. break;
  2342. }
  2343. }
  2344. #ifdef CONFIG_X86_64
  2345. #define R "r"
  2346. #else
  2347. #define R "e"
  2348. #endif
  2349. static void svm_vcpu_run(struct kvm_vcpu *vcpu)
  2350. {
  2351. struct vcpu_svm *svm = to_svm(vcpu);
  2352. u16 fs_selector;
  2353. u16 gs_selector;
  2354. u16 ldt_selector;
  2355. /*
  2356. * A vmexit emulation is required before the vcpu can be executed
  2357. * again.
  2358. */
  2359. if (unlikely(svm->nested.exit_required))
  2360. return;
  2361. svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
  2362. svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
  2363. svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
  2364. pre_svm_run(svm);
  2365. sync_lapic_to_cr8(vcpu);
  2366. save_host_msrs(vcpu);
  2367. fs_selector = kvm_read_fs();
  2368. gs_selector = kvm_read_gs();
  2369. ldt_selector = kvm_read_ldt();
  2370. svm->vmcb->save.cr2 = vcpu->arch.cr2;
  2371. /* required for live migration with NPT */
  2372. if (npt_enabled)
  2373. svm->vmcb->save.cr3 = vcpu->arch.cr3;
  2374. clgi();
  2375. local_irq_enable();
  2376. asm volatile (
  2377. "push %%"R"bp; \n\t"
  2378. "mov %c[rbx](%[svm]), %%"R"bx \n\t"
  2379. "mov %c[rcx](%[svm]), %%"R"cx \n\t"
  2380. "mov %c[rdx](%[svm]), %%"R"dx \n\t"
  2381. "mov %c[rsi](%[svm]), %%"R"si \n\t"
  2382. "mov %c[rdi](%[svm]), %%"R"di \n\t"
  2383. "mov %c[rbp](%[svm]), %%"R"bp \n\t"
  2384. #ifdef CONFIG_X86_64
  2385. "mov %c[r8](%[svm]), %%r8 \n\t"
  2386. "mov %c[r9](%[svm]), %%r9 \n\t"
  2387. "mov %c[r10](%[svm]), %%r10 \n\t"
  2388. "mov %c[r11](%[svm]), %%r11 \n\t"
  2389. "mov %c[r12](%[svm]), %%r12 \n\t"
  2390. "mov %c[r13](%[svm]), %%r13 \n\t"
  2391. "mov %c[r14](%[svm]), %%r14 \n\t"
  2392. "mov %c[r15](%[svm]), %%r15 \n\t"
  2393. #endif
  2394. /* Enter guest mode */
  2395. "push %%"R"ax \n\t"
  2396. "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
  2397. __ex(SVM_VMLOAD) "\n\t"
  2398. __ex(SVM_VMRUN) "\n\t"
  2399. __ex(SVM_VMSAVE) "\n\t"
  2400. "pop %%"R"ax \n\t"
  2401. /* Save guest registers, load host registers */
  2402. "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
  2403. "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
  2404. "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
  2405. "mov %%"R"si, %c[rsi](%[svm]) \n\t"
  2406. "mov %%"R"di, %c[rdi](%[svm]) \n\t"
  2407. "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
  2408. #ifdef CONFIG_X86_64
  2409. "mov %%r8, %c[r8](%[svm]) \n\t"
  2410. "mov %%r9, %c[r9](%[svm]) \n\t"
  2411. "mov %%r10, %c[r10](%[svm]) \n\t"
  2412. "mov %%r11, %c[r11](%[svm]) \n\t"
  2413. "mov %%r12, %c[r12](%[svm]) \n\t"
  2414. "mov %%r13, %c[r13](%[svm]) \n\t"
  2415. "mov %%r14, %c[r14](%[svm]) \n\t"
  2416. "mov %%r15, %c[r15](%[svm]) \n\t"
  2417. #endif
  2418. "pop %%"R"bp"
  2419. :
  2420. : [svm]"a"(svm),
  2421. [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
  2422. [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
  2423. [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
  2424. [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
  2425. [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
  2426. [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
  2427. [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
  2428. #ifdef CONFIG_X86_64
  2429. , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
  2430. [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
  2431. [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
  2432. [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
  2433. [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
  2434. [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
  2435. [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
  2436. [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
  2437. #endif
  2438. : "cc", "memory"
  2439. , R"bx", R"cx", R"dx", R"si", R"di"
  2440. #ifdef CONFIG_X86_64
  2441. , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
  2442. #endif
  2443. );
  2444. vcpu->arch.cr2 = svm->vmcb->save.cr2;
  2445. vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
  2446. vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
  2447. vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
  2448. kvm_load_fs(fs_selector);
  2449. kvm_load_gs(gs_selector);
  2450. kvm_load_ldt(ldt_selector);
  2451. load_host_msrs(vcpu);
  2452. reload_tss(vcpu);
  2453. local_irq_disable();
  2454. stgi();
  2455. sync_cr8_to_lapic(vcpu);
  2456. svm->next_rip = 0;
  2457. if (npt_enabled) {
  2458. vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
  2459. vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
  2460. }
  2461. }
  2462. #undef R
  2463. static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  2464. {
  2465. struct vcpu_svm *svm = to_svm(vcpu);
  2466. if (npt_enabled) {
  2467. svm->vmcb->control.nested_cr3 = root;
  2468. force_new_asid(vcpu);
  2469. return;
  2470. }
  2471. svm->vmcb->save.cr3 = root;
  2472. force_new_asid(vcpu);
  2473. }
  2474. static int is_disabled(void)
  2475. {
  2476. u64 vm_cr;
  2477. rdmsrl(MSR_VM_CR, vm_cr);
  2478. if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
  2479. return 1;
  2480. return 0;
  2481. }
  2482. static void
  2483. svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  2484. {
  2485. /*
  2486. * Patch in the VMMCALL instruction:
  2487. */
  2488. hypercall[0] = 0x0f;
  2489. hypercall[1] = 0x01;
  2490. hypercall[2] = 0xd9;
  2491. }
  2492. static void svm_check_processor_compat(void *rtn)
  2493. {
  2494. *(int *)rtn = 0;
  2495. }
  2496. static bool svm_cpu_has_accelerated_tpr(void)
  2497. {
  2498. return false;
  2499. }
  2500. static int get_npt_level(void)
  2501. {
  2502. #ifdef CONFIG_X86_64
  2503. return PT64_ROOT_LEVEL;
  2504. #else
  2505. return PT32E_ROOT_LEVEL;
  2506. #endif
  2507. }
  2508. static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  2509. {
  2510. return 0;
  2511. }
  2512. static void svm_cpuid_update(struct kvm_vcpu *vcpu)
  2513. {
  2514. }
  2515. static const struct trace_print_flags svm_exit_reasons_str[] = {
  2516. { SVM_EXIT_READ_CR0, "read_cr0" },
  2517. { SVM_EXIT_READ_CR3, "read_cr3" },
  2518. { SVM_EXIT_READ_CR4, "read_cr4" },
  2519. { SVM_EXIT_READ_CR8, "read_cr8" },
  2520. { SVM_EXIT_WRITE_CR0, "write_cr0" },
  2521. { SVM_EXIT_WRITE_CR3, "write_cr3" },
  2522. { SVM_EXIT_WRITE_CR4, "write_cr4" },
  2523. { SVM_EXIT_WRITE_CR8, "write_cr8" },
  2524. { SVM_EXIT_READ_DR0, "read_dr0" },
  2525. { SVM_EXIT_READ_DR1, "read_dr1" },
  2526. { SVM_EXIT_READ_DR2, "read_dr2" },
  2527. { SVM_EXIT_READ_DR3, "read_dr3" },
  2528. { SVM_EXIT_WRITE_DR0, "write_dr0" },
  2529. { SVM_EXIT_WRITE_DR1, "write_dr1" },
  2530. { SVM_EXIT_WRITE_DR2, "write_dr2" },
  2531. { SVM_EXIT_WRITE_DR3, "write_dr3" },
  2532. { SVM_EXIT_WRITE_DR5, "write_dr5" },
  2533. { SVM_EXIT_WRITE_DR7, "write_dr7" },
  2534. { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
  2535. { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
  2536. { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
  2537. { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
  2538. { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
  2539. { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
  2540. { SVM_EXIT_INTR, "interrupt" },
  2541. { SVM_EXIT_NMI, "nmi" },
  2542. { SVM_EXIT_SMI, "smi" },
  2543. { SVM_EXIT_INIT, "init" },
  2544. { SVM_EXIT_VINTR, "vintr" },
  2545. { SVM_EXIT_CPUID, "cpuid" },
  2546. { SVM_EXIT_INVD, "invd" },
  2547. { SVM_EXIT_HLT, "hlt" },
  2548. { SVM_EXIT_INVLPG, "invlpg" },
  2549. { SVM_EXIT_INVLPGA, "invlpga" },
  2550. { SVM_EXIT_IOIO, "io" },
  2551. { SVM_EXIT_MSR, "msr" },
  2552. { SVM_EXIT_TASK_SWITCH, "task_switch" },
  2553. { SVM_EXIT_SHUTDOWN, "shutdown" },
  2554. { SVM_EXIT_VMRUN, "vmrun" },
  2555. { SVM_EXIT_VMMCALL, "hypercall" },
  2556. { SVM_EXIT_VMLOAD, "vmload" },
  2557. { SVM_EXIT_VMSAVE, "vmsave" },
  2558. { SVM_EXIT_STGI, "stgi" },
  2559. { SVM_EXIT_CLGI, "clgi" },
  2560. { SVM_EXIT_SKINIT, "skinit" },
  2561. { SVM_EXIT_WBINVD, "wbinvd" },
  2562. { SVM_EXIT_MONITOR, "monitor" },
  2563. { SVM_EXIT_MWAIT, "mwait" },
  2564. { SVM_EXIT_NPF, "npf" },
  2565. { -1, NULL }
  2566. };
  2567. static int svm_get_lpage_level(void)
  2568. {
  2569. return PT_PDPE_LEVEL;
  2570. }
  2571. static bool svm_rdtscp_supported(void)
  2572. {
  2573. return false;
  2574. }
  2575. static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
  2576. {
  2577. struct vcpu_svm *svm = to_svm(vcpu);
  2578. svm->vmcb->control.intercept_exceptions |= 1 << NM_VECTOR;
  2579. if (is_nested(svm))
  2580. svm->nested.hsave->control.intercept_exceptions |= 1 << NM_VECTOR;
  2581. update_cr0_intercept(svm);
  2582. }
  2583. static struct kvm_x86_ops svm_x86_ops = {
  2584. .cpu_has_kvm_support = has_svm,
  2585. .disabled_by_bios = is_disabled,
  2586. .hardware_setup = svm_hardware_setup,
  2587. .hardware_unsetup = svm_hardware_unsetup,
  2588. .check_processor_compatibility = svm_check_processor_compat,
  2589. .hardware_enable = svm_hardware_enable,
  2590. .hardware_disable = svm_hardware_disable,
  2591. .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
  2592. .vcpu_create = svm_create_vcpu,
  2593. .vcpu_free = svm_free_vcpu,
  2594. .vcpu_reset = svm_vcpu_reset,
  2595. .prepare_guest_switch = svm_prepare_guest_switch,
  2596. .vcpu_load = svm_vcpu_load,
  2597. .vcpu_put = svm_vcpu_put,
  2598. .set_guest_debug = svm_guest_debug,
  2599. .get_msr = svm_get_msr,
  2600. .set_msr = svm_set_msr,
  2601. .get_segment_base = svm_get_segment_base,
  2602. .get_segment = svm_get_segment,
  2603. .set_segment = svm_set_segment,
  2604. .get_cpl = svm_get_cpl,
  2605. .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
  2606. .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
  2607. .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
  2608. .set_cr0 = svm_set_cr0,
  2609. .set_cr3 = svm_set_cr3,
  2610. .set_cr4 = svm_set_cr4,
  2611. .set_efer = svm_set_efer,
  2612. .get_idt = svm_get_idt,
  2613. .set_idt = svm_set_idt,
  2614. .get_gdt = svm_get_gdt,
  2615. .set_gdt = svm_set_gdt,
  2616. .get_dr = svm_get_dr,
  2617. .set_dr = svm_set_dr,
  2618. .cache_reg = svm_cache_reg,
  2619. .get_rflags = svm_get_rflags,
  2620. .set_rflags = svm_set_rflags,
  2621. .fpu_activate = svm_fpu_activate,
  2622. .fpu_deactivate = svm_fpu_deactivate,
  2623. .tlb_flush = svm_flush_tlb,
  2624. .run = svm_vcpu_run,
  2625. .handle_exit = handle_exit,
  2626. .skip_emulated_instruction = skip_emulated_instruction,
  2627. .set_interrupt_shadow = svm_set_interrupt_shadow,
  2628. .get_interrupt_shadow = svm_get_interrupt_shadow,
  2629. .patch_hypercall = svm_patch_hypercall,
  2630. .set_irq = svm_set_irq,
  2631. .set_nmi = svm_inject_nmi,
  2632. .queue_exception = svm_queue_exception,
  2633. .interrupt_allowed = svm_interrupt_allowed,
  2634. .nmi_allowed = svm_nmi_allowed,
  2635. .get_nmi_mask = svm_get_nmi_mask,
  2636. .set_nmi_mask = svm_set_nmi_mask,
  2637. .enable_nmi_window = enable_nmi_window,
  2638. .enable_irq_window = enable_irq_window,
  2639. .update_cr8_intercept = update_cr8_intercept,
  2640. .set_tss_addr = svm_set_tss_addr,
  2641. .get_tdp_level = get_npt_level,
  2642. .get_mt_mask = svm_get_mt_mask,
  2643. .exit_reasons_str = svm_exit_reasons_str,
  2644. .get_lpage_level = svm_get_lpage_level,
  2645. .cpuid_update = svm_cpuid_update,
  2646. .rdtscp_supported = svm_rdtscp_supported,
  2647. };
  2648. static int __init svm_init(void)
  2649. {
  2650. return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
  2651. THIS_MODULE);
  2652. }
  2653. static void __exit svm_exit(void)
  2654. {
  2655. kvm_exit();
  2656. }
  2657. module_init(svm_init)
  2658. module_exit(svm_exit)