svm.c 77 KB

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