svm.c 76 KB

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