svm.c 77 KB

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