svm.c 74 KB

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