svm.c 80 KB

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