svm.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906
  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 "kvm_svm.h"
  18. #include "irq.h"
  19. #include "mmu.h"
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/highmem.h>
  24. #include <linux/sched.h>
  25. #include <asm/desc.h>
  26. MODULE_AUTHOR("Qumranet");
  27. MODULE_LICENSE("GPL");
  28. #define IOPM_ALLOC_ORDER 2
  29. #define MSRPM_ALLOC_ORDER 1
  30. #define DB_VECTOR 1
  31. #define UD_VECTOR 6
  32. #define GP_VECTOR 13
  33. #define DR7_GD_MASK (1 << 13)
  34. #define DR6_BD_MASK (1 << 13)
  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_DEATURE_SVML (1 << 2)
  40. #define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
  41. /* enable NPT for AMD64 and X86 with PAE */
  42. #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
  43. static bool npt_enabled = true;
  44. #else
  45. static bool npt_enabled = false;
  46. #endif
  47. static int npt = 1;
  48. module_param(npt, int, S_IRUGO);
  49. static void kvm_reput_irq(struct vcpu_svm *svm);
  50. static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
  51. {
  52. return container_of(vcpu, struct vcpu_svm, vcpu);
  53. }
  54. static unsigned long iopm_base;
  55. struct kvm_ldttss_desc {
  56. u16 limit0;
  57. u16 base0;
  58. unsigned base1 : 8, type : 5, dpl : 2, p : 1;
  59. unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8;
  60. u32 base3;
  61. u32 zero1;
  62. } __attribute__((packed));
  63. struct svm_cpu_data {
  64. int cpu;
  65. u64 asid_generation;
  66. u32 max_asid;
  67. u32 next_asid;
  68. struct kvm_ldttss_desc *tss_desc;
  69. struct page *save_area;
  70. };
  71. static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
  72. static uint32_t svm_features;
  73. struct svm_init_data {
  74. int cpu;
  75. int r;
  76. };
  77. static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
  78. #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
  79. #define MSRS_RANGE_SIZE 2048
  80. #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
  81. #define MAX_INST_SIZE 15
  82. static inline u32 svm_has(u32 feat)
  83. {
  84. return svm_features & feat;
  85. }
  86. static inline u8 pop_irq(struct kvm_vcpu *vcpu)
  87. {
  88. int word_index = __ffs(vcpu->arch.irq_summary);
  89. int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
  90. int irq = word_index * BITS_PER_LONG + bit_index;
  91. clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
  92. if (!vcpu->arch.irq_pending[word_index])
  93. clear_bit(word_index, &vcpu->arch.irq_summary);
  94. return irq;
  95. }
  96. static inline void push_irq(struct kvm_vcpu *vcpu, u8 irq)
  97. {
  98. set_bit(irq, vcpu->arch.irq_pending);
  99. set_bit(irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
  100. }
  101. static inline void clgi(void)
  102. {
  103. asm volatile (SVM_CLGI);
  104. }
  105. static inline void stgi(void)
  106. {
  107. asm volatile (SVM_STGI);
  108. }
  109. static inline void invlpga(unsigned long addr, u32 asid)
  110. {
  111. asm volatile (SVM_INVLPGA :: "a"(addr), "c"(asid));
  112. }
  113. static inline unsigned long kvm_read_cr2(void)
  114. {
  115. unsigned long cr2;
  116. asm volatile ("mov %%cr2, %0" : "=r" (cr2));
  117. return cr2;
  118. }
  119. static inline void kvm_write_cr2(unsigned long val)
  120. {
  121. asm volatile ("mov %0, %%cr2" :: "r" (val));
  122. }
  123. static inline unsigned long read_dr6(void)
  124. {
  125. unsigned long dr6;
  126. asm volatile ("mov %%dr6, %0" : "=r" (dr6));
  127. return dr6;
  128. }
  129. static inline void write_dr6(unsigned long val)
  130. {
  131. asm volatile ("mov %0, %%dr6" :: "r" (val));
  132. }
  133. static inline unsigned long read_dr7(void)
  134. {
  135. unsigned long dr7;
  136. asm volatile ("mov %%dr7, %0" : "=r" (dr7));
  137. return dr7;
  138. }
  139. static inline void write_dr7(unsigned long val)
  140. {
  141. asm volatile ("mov %0, %%dr7" :: "r" (val));
  142. }
  143. static inline void force_new_asid(struct kvm_vcpu *vcpu)
  144. {
  145. to_svm(vcpu)->asid_generation--;
  146. }
  147. static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
  148. {
  149. force_new_asid(vcpu);
  150. }
  151. static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  152. {
  153. if (!npt_enabled && !(efer & EFER_LMA))
  154. efer &= ~EFER_LME;
  155. to_svm(vcpu)->vmcb->save.efer = efer | MSR_EFER_SVME_MASK;
  156. vcpu->arch.shadow_efer = efer;
  157. }
  158. static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
  159. bool has_error_code, u32 error_code)
  160. {
  161. struct vcpu_svm *svm = to_svm(vcpu);
  162. svm->vmcb->control.event_inj = nr
  163. | SVM_EVTINJ_VALID
  164. | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
  165. | SVM_EVTINJ_TYPE_EXEPT;
  166. svm->vmcb->control.event_inj_err = error_code;
  167. }
  168. static bool svm_exception_injected(struct kvm_vcpu *vcpu)
  169. {
  170. struct vcpu_svm *svm = to_svm(vcpu);
  171. return !(svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID);
  172. }
  173. static int is_external_interrupt(u32 info)
  174. {
  175. info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
  176. return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
  177. }
  178. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  179. {
  180. struct vcpu_svm *svm = to_svm(vcpu);
  181. if (!svm->next_rip) {
  182. printk(KERN_DEBUG "%s: NOP\n", __func__);
  183. return;
  184. }
  185. if (svm->next_rip - svm->vmcb->save.rip > MAX_INST_SIZE)
  186. printk(KERN_ERR "%s: ip 0x%llx next 0x%llx\n",
  187. __func__,
  188. svm->vmcb->save.rip,
  189. svm->next_rip);
  190. vcpu->arch.rip = svm->vmcb->save.rip = svm->next_rip;
  191. svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
  192. vcpu->arch.interrupt_window_open = 1;
  193. }
  194. static int has_svm(void)
  195. {
  196. uint32_t eax, ebx, ecx, edx;
  197. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
  198. printk(KERN_INFO "has_svm: not amd\n");
  199. return 0;
  200. }
  201. cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
  202. if (eax < SVM_CPUID_FUNC) {
  203. printk(KERN_INFO "has_svm: can't execute cpuid_8000000a\n");
  204. return 0;
  205. }
  206. cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
  207. if (!(ecx & (1 << SVM_CPUID_FEATURE_SHIFT))) {
  208. printk(KERN_DEBUG "has_svm: svm not available\n");
  209. return 0;
  210. }
  211. return 1;
  212. }
  213. static void svm_hardware_disable(void *garbage)
  214. {
  215. struct svm_cpu_data *svm_data
  216. = per_cpu(svm_data, raw_smp_processor_id());
  217. if (svm_data) {
  218. uint64_t efer;
  219. wrmsrl(MSR_VM_HSAVE_PA, 0);
  220. rdmsrl(MSR_EFER, efer);
  221. wrmsrl(MSR_EFER, efer & ~MSR_EFER_SVME_MASK);
  222. per_cpu(svm_data, raw_smp_processor_id()) = NULL;
  223. __free_page(svm_data->save_area);
  224. kfree(svm_data);
  225. }
  226. }
  227. static void svm_hardware_enable(void *garbage)
  228. {
  229. struct svm_cpu_data *svm_data;
  230. uint64_t efer;
  231. struct desc_ptr gdt_descr;
  232. struct desc_struct *gdt;
  233. int me = raw_smp_processor_id();
  234. if (!has_svm()) {
  235. printk(KERN_ERR "svm_cpu_init: err EOPNOTSUPP on %d\n", me);
  236. return;
  237. }
  238. svm_data = per_cpu(svm_data, me);
  239. if (!svm_data) {
  240. printk(KERN_ERR "svm_cpu_init: svm_data is NULL on %d\n",
  241. me);
  242. return;
  243. }
  244. svm_data->asid_generation = 1;
  245. svm_data->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
  246. svm_data->next_asid = svm_data->max_asid + 1;
  247. asm volatile ("sgdt %0" : "=m"(gdt_descr));
  248. gdt = (struct desc_struct *)gdt_descr.address;
  249. svm_data->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
  250. rdmsrl(MSR_EFER, efer);
  251. wrmsrl(MSR_EFER, efer | MSR_EFER_SVME_MASK);
  252. wrmsrl(MSR_VM_HSAVE_PA,
  253. page_to_pfn(svm_data->save_area) << PAGE_SHIFT);
  254. }
  255. static int svm_cpu_init(int cpu)
  256. {
  257. struct svm_cpu_data *svm_data;
  258. int r;
  259. svm_data = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
  260. if (!svm_data)
  261. return -ENOMEM;
  262. svm_data->cpu = cpu;
  263. svm_data->save_area = alloc_page(GFP_KERNEL);
  264. r = -ENOMEM;
  265. if (!svm_data->save_area)
  266. goto err_1;
  267. per_cpu(svm_data, cpu) = svm_data;
  268. return 0;
  269. err_1:
  270. kfree(svm_data);
  271. return r;
  272. }
  273. static void set_msr_interception(u32 *msrpm, unsigned msr,
  274. int read, int write)
  275. {
  276. int i;
  277. for (i = 0; i < NUM_MSR_MAPS; i++) {
  278. if (msr >= msrpm_ranges[i] &&
  279. msr < msrpm_ranges[i] + MSRS_IN_RANGE) {
  280. u32 msr_offset = (i * MSRS_IN_RANGE + msr -
  281. msrpm_ranges[i]) * 2;
  282. u32 *base = msrpm + (msr_offset / 32);
  283. u32 msr_shift = msr_offset % 32;
  284. u32 mask = ((write) ? 0 : 2) | ((read) ? 0 : 1);
  285. *base = (*base & ~(0x3 << msr_shift)) |
  286. (mask << msr_shift);
  287. return;
  288. }
  289. }
  290. BUG();
  291. }
  292. static void svm_vcpu_init_msrpm(u32 *msrpm)
  293. {
  294. memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
  295. #ifdef CONFIG_X86_64
  296. set_msr_interception(msrpm, MSR_GS_BASE, 1, 1);
  297. set_msr_interception(msrpm, MSR_FS_BASE, 1, 1);
  298. set_msr_interception(msrpm, MSR_KERNEL_GS_BASE, 1, 1);
  299. set_msr_interception(msrpm, MSR_LSTAR, 1, 1);
  300. set_msr_interception(msrpm, MSR_CSTAR, 1, 1);
  301. set_msr_interception(msrpm, MSR_SYSCALL_MASK, 1, 1);
  302. #endif
  303. set_msr_interception(msrpm, MSR_K6_STAR, 1, 1);
  304. set_msr_interception(msrpm, MSR_IA32_SYSENTER_CS, 1, 1);
  305. set_msr_interception(msrpm, MSR_IA32_SYSENTER_ESP, 1, 1);
  306. set_msr_interception(msrpm, MSR_IA32_SYSENTER_EIP, 1, 1);
  307. }
  308. static void svm_enable_lbrv(struct vcpu_svm *svm)
  309. {
  310. u32 *msrpm = svm->msrpm;
  311. svm->vmcb->control.lbr_ctl = 1;
  312. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
  313. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
  314. set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
  315. set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
  316. }
  317. static void svm_disable_lbrv(struct vcpu_svm *svm)
  318. {
  319. u32 *msrpm = svm->msrpm;
  320. svm->vmcb->control.lbr_ctl = 0;
  321. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
  322. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
  323. set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
  324. set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
  325. }
  326. static __init int svm_hardware_setup(void)
  327. {
  328. int cpu;
  329. struct page *iopm_pages;
  330. void *iopm_va;
  331. int r;
  332. iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
  333. if (!iopm_pages)
  334. return -ENOMEM;
  335. iopm_va = page_address(iopm_pages);
  336. memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
  337. clear_bit(0x80, iopm_va); /* allow direct access to PC debug port */
  338. iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
  339. if (boot_cpu_has(X86_FEATURE_NX))
  340. kvm_enable_efer_bits(EFER_NX);
  341. for_each_online_cpu(cpu) {
  342. r = svm_cpu_init(cpu);
  343. if (r)
  344. goto err;
  345. }
  346. svm_features = cpuid_edx(SVM_CPUID_FUNC);
  347. if (!svm_has(SVM_FEATURE_NPT))
  348. npt_enabled = false;
  349. if (npt_enabled && !npt) {
  350. printk(KERN_INFO "kvm: Nested Paging disabled\n");
  351. npt_enabled = false;
  352. }
  353. if (npt_enabled) {
  354. printk(KERN_INFO "kvm: Nested Paging enabled\n");
  355. kvm_enable_tdp();
  356. }
  357. return 0;
  358. err:
  359. __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
  360. iopm_base = 0;
  361. return r;
  362. }
  363. static __exit void svm_hardware_unsetup(void)
  364. {
  365. __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
  366. iopm_base = 0;
  367. }
  368. static void init_seg(struct vmcb_seg *seg)
  369. {
  370. seg->selector = 0;
  371. seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
  372. SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
  373. seg->limit = 0xffff;
  374. seg->base = 0;
  375. }
  376. static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
  377. {
  378. seg->selector = 0;
  379. seg->attrib = SVM_SELECTOR_P_MASK | type;
  380. seg->limit = 0xffff;
  381. seg->base = 0;
  382. }
  383. static void init_vmcb(struct vcpu_svm *svm)
  384. {
  385. struct vmcb_control_area *control = &svm->vmcb->control;
  386. struct vmcb_save_area *save = &svm->vmcb->save;
  387. control->intercept_cr_read = INTERCEPT_CR0_MASK |
  388. INTERCEPT_CR3_MASK |
  389. INTERCEPT_CR4_MASK |
  390. INTERCEPT_CR8_MASK;
  391. control->intercept_cr_write = INTERCEPT_CR0_MASK |
  392. INTERCEPT_CR3_MASK |
  393. INTERCEPT_CR4_MASK |
  394. INTERCEPT_CR8_MASK;
  395. control->intercept_dr_read = INTERCEPT_DR0_MASK |
  396. INTERCEPT_DR1_MASK |
  397. INTERCEPT_DR2_MASK |
  398. INTERCEPT_DR3_MASK;
  399. control->intercept_dr_write = INTERCEPT_DR0_MASK |
  400. INTERCEPT_DR1_MASK |
  401. INTERCEPT_DR2_MASK |
  402. INTERCEPT_DR3_MASK |
  403. INTERCEPT_DR5_MASK |
  404. INTERCEPT_DR7_MASK;
  405. control->intercept_exceptions = (1 << PF_VECTOR) |
  406. (1 << UD_VECTOR) |
  407. (1 << MC_VECTOR);
  408. control->intercept = (1ULL << INTERCEPT_INTR) |
  409. (1ULL << INTERCEPT_NMI) |
  410. (1ULL << INTERCEPT_SMI) |
  411. /*
  412. * selective cr0 intercept bug?
  413. * 0: 0f 22 d8 mov %eax,%cr3
  414. * 3: 0f 20 c0 mov %cr0,%eax
  415. * 6: 0d 00 00 00 80 or $0x80000000,%eax
  416. * b: 0f 22 c0 mov %eax,%cr0
  417. * set cr3 ->interception
  418. * get cr0 ->interception
  419. * set cr0 -> no interception
  420. */
  421. /* (1ULL << INTERCEPT_SELECTIVE_CR0) | */
  422. (1ULL << INTERCEPT_CPUID) |
  423. (1ULL << INTERCEPT_INVD) |
  424. (1ULL << INTERCEPT_HLT) |
  425. (1ULL << INTERCEPT_INVLPGA) |
  426. (1ULL << INTERCEPT_IOIO_PROT) |
  427. (1ULL << INTERCEPT_MSR_PROT) |
  428. (1ULL << INTERCEPT_TASK_SWITCH) |
  429. (1ULL << INTERCEPT_SHUTDOWN) |
  430. (1ULL << INTERCEPT_VMRUN) |
  431. (1ULL << INTERCEPT_VMMCALL) |
  432. (1ULL << INTERCEPT_VMLOAD) |
  433. (1ULL << INTERCEPT_VMSAVE) |
  434. (1ULL << INTERCEPT_STGI) |
  435. (1ULL << INTERCEPT_CLGI) |
  436. (1ULL << INTERCEPT_SKINIT) |
  437. (1ULL << INTERCEPT_WBINVD) |
  438. (1ULL << INTERCEPT_MONITOR) |
  439. (1ULL << INTERCEPT_MWAIT);
  440. control->iopm_base_pa = iopm_base;
  441. control->msrpm_base_pa = __pa(svm->msrpm);
  442. control->tsc_offset = 0;
  443. control->int_ctl = V_INTR_MASKING_MASK;
  444. init_seg(&save->es);
  445. init_seg(&save->ss);
  446. init_seg(&save->ds);
  447. init_seg(&save->fs);
  448. init_seg(&save->gs);
  449. save->cs.selector = 0xf000;
  450. /* Executable/Readable Code Segment */
  451. save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
  452. SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
  453. save->cs.limit = 0xffff;
  454. /*
  455. * cs.base should really be 0xffff0000, but vmx can't handle that, so
  456. * be consistent with it.
  457. *
  458. * Replace when we have real mode working for vmx.
  459. */
  460. save->cs.base = 0xf0000;
  461. save->gdtr.limit = 0xffff;
  462. save->idtr.limit = 0xffff;
  463. init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
  464. init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
  465. save->efer = MSR_EFER_SVME_MASK;
  466. save->dr6 = 0xffff0ff0;
  467. save->dr7 = 0x400;
  468. save->rflags = 2;
  469. save->rip = 0x0000fff0;
  470. /*
  471. * cr0 val on cpu init should be 0x60000010, we enable cpu
  472. * cache by default. the orderly way is to enable cache in bios.
  473. */
  474. save->cr0 = 0x00000010 | X86_CR0_PG | X86_CR0_WP;
  475. save->cr4 = X86_CR4_PAE;
  476. /* rdx = ?? */
  477. if (npt_enabled) {
  478. /* Setup VMCB for Nested Paging */
  479. control->nested_ctl = 1;
  480. control->intercept &= ~(1ULL << INTERCEPT_TASK_SWITCH);
  481. control->intercept_exceptions &= ~(1 << PF_VECTOR);
  482. control->intercept_cr_read &= ~(INTERCEPT_CR0_MASK|
  483. INTERCEPT_CR3_MASK);
  484. control->intercept_cr_write &= ~(INTERCEPT_CR0_MASK|
  485. INTERCEPT_CR3_MASK);
  486. save->g_pat = 0x0007040600070406ULL;
  487. /* enable caching because the QEMU Bios doesn't enable it */
  488. save->cr0 = X86_CR0_ET;
  489. save->cr3 = 0;
  490. save->cr4 = 0;
  491. }
  492. force_new_asid(&svm->vcpu);
  493. }
  494. static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
  495. {
  496. struct vcpu_svm *svm = to_svm(vcpu);
  497. init_vmcb(svm);
  498. if (vcpu->vcpu_id != 0) {
  499. svm->vmcb->save.rip = 0;
  500. svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
  501. svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
  502. }
  503. return 0;
  504. }
  505. static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
  506. {
  507. struct vcpu_svm *svm;
  508. struct page *page;
  509. struct page *msrpm_pages;
  510. int err;
  511. svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  512. if (!svm) {
  513. err = -ENOMEM;
  514. goto out;
  515. }
  516. err = kvm_vcpu_init(&svm->vcpu, kvm, id);
  517. if (err)
  518. goto free_svm;
  519. page = alloc_page(GFP_KERNEL);
  520. if (!page) {
  521. err = -ENOMEM;
  522. goto uninit;
  523. }
  524. err = -ENOMEM;
  525. msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
  526. if (!msrpm_pages)
  527. goto uninit;
  528. svm->msrpm = page_address(msrpm_pages);
  529. svm_vcpu_init_msrpm(svm->msrpm);
  530. svm->vmcb = page_address(page);
  531. clear_page(svm->vmcb);
  532. svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
  533. svm->asid_generation = 0;
  534. memset(svm->db_regs, 0, sizeof(svm->db_regs));
  535. init_vmcb(svm);
  536. fx_init(&svm->vcpu);
  537. svm->vcpu.fpu_active = 1;
  538. svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
  539. if (svm->vcpu.vcpu_id == 0)
  540. svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
  541. return &svm->vcpu;
  542. uninit:
  543. kvm_vcpu_uninit(&svm->vcpu);
  544. free_svm:
  545. kmem_cache_free(kvm_vcpu_cache, svm);
  546. out:
  547. return ERR_PTR(err);
  548. }
  549. static void svm_free_vcpu(struct kvm_vcpu *vcpu)
  550. {
  551. struct vcpu_svm *svm = to_svm(vcpu);
  552. __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
  553. __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
  554. kvm_vcpu_uninit(vcpu);
  555. kmem_cache_free(kvm_vcpu_cache, svm);
  556. }
  557. static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  558. {
  559. struct vcpu_svm *svm = to_svm(vcpu);
  560. int i;
  561. if (unlikely(cpu != vcpu->cpu)) {
  562. u64 tsc_this, delta;
  563. /*
  564. * Make sure that the guest sees a monotonically
  565. * increasing TSC.
  566. */
  567. rdtscll(tsc_this);
  568. delta = vcpu->arch.host_tsc - tsc_this;
  569. svm->vmcb->control.tsc_offset += delta;
  570. vcpu->cpu = cpu;
  571. kvm_migrate_apic_timer(vcpu);
  572. }
  573. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  574. rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  575. }
  576. static void svm_vcpu_put(struct kvm_vcpu *vcpu)
  577. {
  578. struct vcpu_svm *svm = to_svm(vcpu);
  579. int i;
  580. ++vcpu->stat.host_state_reload;
  581. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  582. wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  583. rdtscll(vcpu->arch.host_tsc);
  584. }
  585. static void svm_vcpu_decache(struct kvm_vcpu *vcpu)
  586. {
  587. }
  588. static void svm_cache_regs(struct kvm_vcpu *vcpu)
  589. {
  590. struct vcpu_svm *svm = to_svm(vcpu);
  591. vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
  592. vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
  593. vcpu->arch.rip = svm->vmcb->save.rip;
  594. }
  595. static void svm_decache_regs(struct kvm_vcpu *vcpu)
  596. {
  597. struct vcpu_svm *svm = to_svm(vcpu);
  598. svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
  599. svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
  600. svm->vmcb->save.rip = vcpu->arch.rip;
  601. }
  602. static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
  603. {
  604. return to_svm(vcpu)->vmcb->save.rflags;
  605. }
  606. static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  607. {
  608. to_svm(vcpu)->vmcb->save.rflags = rflags;
  609. }
  610. static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
  611. {
  612. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  613. switch (seg) {
  614. case VCPU_SREG_CS: return &save->cs;
  615. case VCPU_SREG_DS: return &save->ds;
  616. case VCPU_SREG_ES: return &save->es;
  617. case VCPU_SREG_FS: return &save->fs;
  618. case VCPU_SREG_GS: return &save->gs;
  619. case VCPU_SREG_SS: return &save->ss;
  620. case VCPU_SREG_TR: return &save->tr;
  621. case VCPU_SREG_LDTR: return &save->ldtr;
  622. }
  623. BUG();
  624. return NULL;
  625. }
  626. static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  627. {
  628. struct vmcb_seg *s = svm_seg(vcpu, seg);
  629. return s->base;
  630. }
  631. static void svm_get_segment(struct kvm_vcpu *vcpu,
  632. struct kvm_segment *var, int seg)
  633. {
  634. struct vmcb_seg *s = svm_seg(vcpu, seg);
  635. var->base = s->base;
  636. var->limit = s->limit;
  637. var->selector = s->selector;
  638. var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
  639. var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
  640. var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
  641. var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
  642. var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
  643. var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
  644. var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
  645. var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
  646. var->unusable = !var->present;
  647. }
  648. static int svm_get_cpl(struct kvm_vcpu *vcpu)
  649. {
  650. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  651. return save->cpl;
  652. }
  653. static void svm_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  654. {
  655. struct vcpu_svm *svm = to_svm(vcpu);
  656. dt->limit = svm->vmcb->save.idtr.limit;
  657. dt->base = svm->vmcb->save.idtr.base;
  658. }
  659. static void svm_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  660. {
  661. struct vcpu_svm *svm = to_svm(vcpu);
  662. svm->vmcb->save.idtr.limit = dt->limit;
  663. svm->vmcb->save.idtr.base = dt->base ;
  664. }
  665. static void svm_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  666. {
  667. struct vcpu_svm *svm = to_svm(vcpu);
  668. dt->limit = svm->vmcb->save.gdtr.limit;
  669. dt->base = svm->vmcb->save.gdtr.base;
  670. }
  671. static void svm_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  672. {
  673. struct vcpu_svm *svm = to_svm(vcpu);
  674. svm->vmcb->save.gdtr.limit = dt->limit;
  675. svm->vmcb->save.gdtr.base = dt->base ;
  676. }
  677. static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  678. {
  679. }
  680. static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  681. {
  682. struct vcpu_svm *svm = to_svm(vcpu);
  683. #ifdef CONFIG_X86_64
  684. if (vcpu->arch.shadow_efer & EFER_LME) {
  685. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  686. vcpu->arch.shadow_efer |= EFER_LMA;
  687. svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
  688. }
  689. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
  690. vcpu->arch.shadow_efer &= ~EFER_LMA;
  691. svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
  692. }
  693. }
  694. #endif
  695. if (npt_enabled)
  696. goto set;
  697. if ((vcpu->arch.cr0 & X86_CR0_TS) && !(cr0 & X86_CR0_TS)) {
  698. svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
  699. vcpu->fpu_active = 1;
  700. }
  701. vcpu->arch.cr0 = cr0;
  702. cr0 |= X86_CR0_PG | X86_CR0_WP;
  703. if (!vcpu->fpu_active) {
  704. svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
  705. cr0 |= X86_CR0_TS;
  706. }
  707. set:
  708. /*
  709. * re-enable caching here because the QEMU bios
  710. * does not do it - this results in some delay at
  711. * reboot
  712. */
  713. cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
  714. svm->vmcb->save.cr0 = cr0;
  715. }
  716. static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  717. {
  718. unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
  719. vcpu->arch.cr4 = cr4;
  720. if (!npt_enabled)
  721. cr4 |= X86_CR4_PAE;
  722. cr4 |= host_cr4_mce;
  723. to_svm(vcpu)->vmcb->save.cr4 = cr4;
  724. }
  725. static void svm_set_segment(struct kvm_vcpu *vcpu,
  726. struct kvm_segment *var, int seg)
  727. {
  728. struct vcpu_svm *svm = to_svm(vcpu);
  729. struct vmcb_seg *s = svm_seg(vcpu, seg);
  730. s->base = var->base;
  731. s->limit = var->limit;
  732. s->selector = var->selector;
  733. if (var->unusable)
  734. s->attrib = 0;
  735. else {
  736. s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
  737. s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
  738. s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
  739. s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
  740. s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
  741. s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
  742. s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
  743. s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
  744. }
  745. if (seg == VCPU_SREG_CS)
  746. svm->vmcb->save.cpl
  747. = (svm->vmcb->save.cs.attrib
  748. >> SVM_SELECTOR_DPL_SHIFT) & 3;
  749. }
  750. /* FIXME:
  751. svm(vcpu)->vmcb->control.int_ctl &= ~V_TPR_MASK;
  752. svm(vcpu)->vmcb->control.int_ctl |= (sregs->cr8 & V_TPR_MASK);
  753. */
  754. static int svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
  755. {
  756. return -EOPNOTSUPP;
  757. }
  758. static int svm_get_irq(struct kvm_vcpu *vcpu)
  759. {
  760. struct vcpu_svm *svm = to_svm(vcpu);
  761. u32 exit_int_info = svm->vmcb->control.exit_int_info;
  762. if (is_external_interrupt(exit_int_info))
  763. return exit_int_info & SVM_EVTINJ_VEC_MASK;
  764. return -1;
  765. }
  766. static void load_host_msrs(struct kvm_vcpu *vcpu)
  767. {
  768. #ifdef CONFIG_X86_64
  769. wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
  770. #endif
  771. }
  772. static void save_host_msrs(struct kvm_vcpu *vcpu)
  773. {
  774. #ifdef CONFIG_X86_64
  775. rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
  776. #endif
  777. }
  778. static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *svm_data)
  779. {
  780. if (svm_data->next_asid > svm_data->max_asid) {
  781. ++svm_data->asid_generation;
  782. svm_data->next_asid = 1;
  783. svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
  784. }
  785. svm->vcpu.cpu = svm_data->cpu;
  786. svm->asid_generation = svm_data->asid_generation;
  787. svm->vmcb->control.asid = svm_data->next_asid++;
  788. }
  789. static unsigned long svm_get_dr(struct kvm_vcpu *vcpu, int dr)
  790. {
  791. return to_svm(vcpu)->db_regs[dr];
  792. }
  793. static void svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value,
  794. int *exception)
  795. {
  796. struct vcpu_svm *svm = to_svm(vcpu);
  797. *exception = 0;
  798. if (svm->vmcb->save.dr7 & DR7_GD_MASK) {
  799. svm->vmcb->save.dr7 &= ~DR7_GD_MASK;
  800. svm->vmcb->save.dr6 |= DR6_BD_MASK;
  801. *exception = DB_VECTOR;
  802. return;
  803. }
  804. switch (dr) {
  805. case 0 ... 3:
  806. svm->db_regs[dr] = value;
  807. return;
  808. case 4 ... 5:
  809. if (vcpu->arch.cr4 & X86_CR4_DE) {
  810. *exception = UD_VECTOR;
  811. return;
  812. }
  813. case 7: {
  814. if (value & ~((1ULL << 32) - 1)) {
  815. *exception = GP_VECTOR;
  816. return;
  817. }
  818. svm->vmcb->save.dr7 = value;
  819. return;
  820. }
  821. default:
  822. printk(KERN_DEBUG "%s: unexpected dr %u\n",
  823. __func__, dr);
  824. *exception = UD_VECTOR;
  825. return;
  826. }
  827. }
  828. static int pf_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  829. {
  830. u32 exit_int_info = svm->vmcb->control.exit_int_info;
  831. struct kvm *kvm = svm->vcpu.kvm;
  832. u64 fault_address;
  833. u32 error_code;
  834. if (!irqchip_in_kernel(kvm) &&
  835. is_external_interrupt(exit_int_info))
  836. push_irq(&svm->vcpu, exit_int_info & SVM_EVTINJ_VEC_MASK);
  837. fault_address = svm->vmcb->control.exit_info_2;
  838. error_code = svm->vmcb->control.exit_info_1;
  839. return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
  840. }
  841. static int ud_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  842. {
  843. int er;
  844. er = emulate_instruction(&svm->vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
  845. if (er != EMULATE_DONE)
  846. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  847. return 1;
  848. }
  849. static int nm_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  850. {
  851. svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
  852. if (!(svm->vcpu.arch.cr0 & X86_CR0_TS))
  853. svm->vmcb->save.cr0 &= ~X86_CR0_TS;
  854. svm->vcpu.fpu_active = 1;
  855. return 1;
  856. }
  857. static int mc_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  858. {
  859. /*
  860. * On an #MC intercept the MCE handler is not called automatically in
  861. * the host. So do it by hand here.
  862. */
  863. asm volatile (
  864. "int $0x12\n");
  865. /* not sure if we ever come back to this point */
  866. return 1;
  867. }
  868. static int shutdown_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  869. {
  870. /*
  871. * VMCB is undefined after a SHUTDOWN intercept
  872. * so reinitialize it.
  873. */
  874. clear_page(svm->vmcb);
  875. init_vmcb(svm);
  876. kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
  877. return 0;
  878. }
  879. static int io_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  880. {
  881. u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
  882. int size, down, in, string, rep;
  883. unsigned port;
  884. ++svm->vcpu.stat.io_exits;
  885. svm->next_rip = svm->vmcb->control.exit_info_2;
  886. string = (io_info & SVM_IOIO_STR_MASK) != 0;
  887. if (string) {
  888. if (emulate_instruction(&svm->vcpu,
  889. kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
  890. return 0;
  891. return 1;
  892. }
  893. in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
  894. port = io_info >> 16;
  895. size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
  896. rep = (io_info & SVM_IOIO_REP_MASK) != 0;
  897. down = (svm->vmcb->save.rflags & X86_EFLAGS_DF) != 0;
  898. return kvm_emulate_pio(&svm->vcpu, kvm_run, in, size, port);
  899. }
  900. static int nop_on_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  901. {
  902. return 1;
  903. }
  904. static int halt_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  905. {
  906. svm->next_rip = svm->vmcb->save.rip + 1;
  907. skip_emulated_instruction(&svm->vcpu);
  908. return kvm_emulate_halt(&svm->vcpu);
  909. }
  910. static int vmmcall_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  911. {
  912. svm->next_rip = svm->vmcb->save.rip + 3;
  913. skip_emulated_instruction(&svm->vcpu);
  914. kvm_emulate_hypercall(&svm->vcpu);
  915. return 1;
  916. }
  917. static int invalid_op_interception(struct vcpu_svm *svm,
  918. struct kvm_run *kvm_run)
  919. {
  920. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  921. return 1;
  922. }
  923. static int task_switch_interception(struct vcpu_svm *svm,
  924. struct kvm_run *kvm_run)
  925. {
  926. u16 tss_selector;
  927. tss_selector = (u16)svm->vmcb->control.exit_info_1;
  928. if (svm->vmcb->control.exit_info_2 &
  929. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
  930. return kvm_task_switch(&svm->vcpu, tss_selector,
  931. TASK_SWITCH_IRET);
  932. if (svm->vmcb->control.exit_info_2 &
  933. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
  934. return kvm_task_switch(&svm->vcpu, tss_selector,
  935. TASK_SWITCH_JMP);
  936. return kvm_task_switch(&svm->vcpu, tss_selector, TASK_SWITCH_CALL);
  937. }
  938. static int cpuid_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  939. {
  940. svm->next_rip = svm->vmcb->save.rip + 2;
  941. kvm_emulate_cpuid(&svm->vcpu);
  942. return 1;
  943. }
  944. static int emulate_on_interception(struct vcpu_svm *svm,
  945. struct kvm_run *kvm_run)
  946. {
  947. if (emulate_instruction(&svm->vcpu, NULL, 0, 0, 0) != EMULATE_DONE)
  948. pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
  949. return 1;
  950. }
  951. static int cr8_write_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  952. {
  953. emulate_instruction(&svm->vcpu, NULL, 0, 0, 0);
  954. if (irqchip_in_kernel(svm->vcpu.kvm))
  955. return 1;
  956. kvm_run->exit_reason = KVM_EXIT_SET_TPR;
  957. return 0;
  958. }
  959. static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
  960. {
  961. struct vcpu_svm *svm = to_svm(vcpu);
  962. switch (ecx) {
  963. case MSR_IA32_TIME_STAMP_COUNTER: {
  964. u64 tsc;
  965. rdtscll(tsc);
  966. *data = svm->vmcb->control.tsc_offset + tsc;
  967. break;
  968. }
  969. case MSR_K6_STAR:
  970. *data = svm->vmcb->save.star;
  971. break;
  972. #ifdef CONFIG_X86_64
  973. case MSR_LSTAR:
  974. *data = svm->vmcb->save.lstar;
  975. break;
  976. case MSR_CSTAR:
  977. *data = svm->vmcb->save.cstar;
  978. break;
  979. case MSR_KERNEL_GS_BASE:
  980. *data = svm->vmcb->save.kernel_gs_base;
  981. break;
  982. case MSR_SYSCALL_MASK:
  983. *data = svm->vmcb->save.sfmask;
  984. break;
  985. #endif
  986. case MSR_IA32_SYSENTER_CS:
  987. *data = svm->vmcb->save.sysenter_cs;
  988. break;
  989. case MSR_IA32_SYSENTER_EIP:
  990. *data = svm->vmcb->save.sysenter_eip;
  991. break;
  992. case MSR_IA32_SYSENTER_ESP:
  993. *data = svm->vmcb->save.sysenter_esp;
  994. break;
  995. /* Nobody will change the following 5 values in the VMCB so
  996. we can safely return them on rdmsr. They will always be 0
  997. until LBRV is implemented. */
  998. case MSR_IA32_DEBUGCTLMSR:
  999. *data = svm->vmcb->save.dbgctl;
  1000. break;
  1001. case MSR_IA32_LASTBRANCHFROMIP:
  1002. *data = svm->vmcb->save.br_from;
  1003. break;
  1004. case MSR_IA32_LASTBRANCHTOIP:
  1005. *data = svm->vmcb->save.br_to;
  1006. break;
  1007. case MSR_IA32_LASTINTFROMIP:
  1008. *data = svm->vmcb->save.last_excp_from;
  1009. break;
  1010. case MSR_IA32_LASTINTTOIP:
  1011. *data = svm->vmcb->save.last_excp_to;
  1012. break;
  1013. default:
  1014. return kvm_get_msr_common(vcpu, ecx, data);
  1015. }
  1016. return 0;
  1017. }
  1018. static int rdmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1019. {
  1020. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  1021. u64 data;
  1022. if (svm_get_msr(&svm->vcpu, ecx, &data))
  1023. kvm_inject_gp(&svm->vcpu, 0);
  1024. else {
  1025. svm->vmcb->save.rax = data & 0xffffffff;
  1026. svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
  1027. svm->next_rip = svm->vmcb->save.rip + 2;
  1028. skip_emulated_instruction(&svm->vcpu);
  1029. }
  1030. return 1;
  1031. }
  1032. static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
  1033. {
  1034. struct vcpu_svm *svm = to_svm(vcpu);
  1035. switch (ecx) {
  1036. case MSR_IA32_TIME_STAMP_COUNTER: {
  1037. u64 tsc;
  1038. rdtscll(tsc);
  1039. svm->vmcb->control.tsc_offset = data - tsc;
  1040. break;
  1041. }
  1042. case MSR_K6_STAR:
  1043. svm->vmcb->save.star = data;
  1044. break;
  1045. #ifdef CONFIG_X86_64
  1046. case MSR_LSTAR:
  1047. svm->vmcb->save.lstar = data;
  1048. break;
  1049. case MSR_CSTAR:
  1050. svm->vmcb->save.cstar = data;
  1051. break;
  1052. case MSR_KERNEL_GS_BASE:
  1053. svm->vmcb->save.kernel_gs_base = data;
  1054. break;
  1055. case MSR_SYSCALL_MASK:
  1056. svm->vmcb->save.sfmask = data;
  1057. break;
  1058. #endif
  1059. case MSR_IA32_SYSENTER_CS:
  1060. svm->vmcb->save.sysenter_cs = data;
  1061. break;
  1062. case MSR_IA32_SYSENTER_EIP:
  1063. svm->vmcb->save.sysenter_eip = data;
  1064. break;
  1065. case MSR_IA32_SYSENTER_ESP:
  1066. svm->vmcb->save.sysenter_esp = data;
  1067. break;
  1068. case MSR_IA32_DEBUGCTLMSR:
  1069. if (!svm_has(SVM_FEATURE_LBRV)) {
  1070. pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
  1071. __func__, data);
  1072. break;
  1073. }
  1074. if (data & DEBUGCTL_RESERVED_BITS)
  1075. return 1;
  1076. svm->vmcb->save.dbgctl = data;
  1077. if (data & (1ULL<<0))
  1078. svm_enable_lbrv(svm);
  1079. else
  1080. svm_disable_lbrv(svm);
  1081. break;
  1082. case MSR_K7_EVNTSEL0:
  1083. case MSR_K7_EVNTSEL1:
  1084. case MSR_K7_EVNTSEL2:
  1085. case MSR_K7_EVNTSEL3:
  1086. /*
  1087. * only support writing 0 to the performance counters for now
  1088. * to make Windows happy. Should be replaced by a real
  1089. * performance counter emulation later.
  1090. */
  1091. if (data != 0)
  1092. goto unhandled;
  1093. break;
  1094. default:
  1095. unhandled:
  1096. return kvm_set_msr_common(vcpu, ecx, data);
  1097. }
  1098. return 0;
  1099. }
  1100. static int wrmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1101. {
  1102. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  1103. u64 data = (svm->vmcb->save.rax & -1u)
  1104. | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  1105. svm->next_rip = svm->vmcb->save.rip + 2;
  1106. if (svm_set_msr(&svm->vcpu, ecx, data))
  1107. kvm_inject_gp(&svm->vcpu, 0);
  1108. else
  1109. skip_emulated_instruction(&svm->vcpu);
  1110. return 1;
  1111. }
  1112. static int msr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1113. {
  1114. if (svm->vmcb->control.exit_info_1)
  1115. return wrmsr_interception(svm, kvm_run);
  1116. else
  1117. return rdmsr_interception(svm, kvm_run);
  1118. }
  1119. static int interrupt_window_interception(struct vcpu_svm *svm,
  1120. struct kvm_run *kvm_run)
  1121. {
  1122. svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
  1123. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  1124. /*
  1125. * If the user space waits to inject interrupts, exit as soon as
  1126. * possible
  1127. */
  1128. if (kvm_run->request_interrupt_window &&
  1129. !svm->vcpu.arch.irq_summary) {
  1130. ++svm->vcpu.stat.irq_window_exits;
  1131. kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  1132. return 0;
  1133. }
  1134. return 1;
  1135. }
  1136. static int (*svm_exit_handlers[])(struct vcpu_svm *svm,
  1137. struct kvm_run *kvm_run) = {
  1138. [SVM_EXIT_READ_CR0] = emulate_on_interception,
  1139. [SVM_EXIT_READ_CR3] = emulate_on_interception,
  1140. [SVM_EXIT_READ_CR4] = emulate_on_interception,
  1141. [SVM_EXIT_READ_CR8] = emulate_on_interception,
  1142. /* for now: */
  1143. [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
  1144. [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
  1145. [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
  1146. [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
  1147. [SVM_EXIT_READ_DR0] = emulate_on_interception,
  1148. [SVM_EXIT_READ_DR1] = emulate_on_interception,
  1149. [SVM_EXIT_READ_DR2] = emulate_on_interception,
  1150. [SVM_EXIT_READ_DR3] = emulate_on_interception,
  1151. [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
  1152. [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
  1153. [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
  1154. [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
  1155. [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
  1156. [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
  1157. [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
  1158. [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
  1159. [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
  1160. [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
  1161. [SVM_EXIT_INTR] = nop_on_interception,
  1162. [SVM_EXIT_NMI] = nop_on_interception,
  1163. [SVM_EXIT_SMI] = nop_on_interception,
  1164. [SVM_EXIT_INIT] = nop_on_interception,
  1165. [SVM_EXIT_VINTR] = interrupt_window_interception,
  1166. /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */
  1167. [SVM_EXIT_CPUID] = cpuid_interception,
  1168. [SVM_EXIT_INVD] = emulate_on_interception,
  1169. [SVM_EXIT_HLT] = halt_interception,
  1170. [SVM_EXIT_INVLPG] = emulate_on_interception,
  1171. [SVM_EXIT_INVLPGA] = invalid_op_interception,
  1172. [SVM_EXIT_IOIO] = io_interception,
  1173. [SVM_EXIT_MSR] = msr_interception,
  1174. [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
  1175. [SVM_EXIT_SHUTDOWN] = shutdown_interception,
  1176. [SVM_EXIT_VMRUN] = invalid_op_interception,
  1177. [SVM_EXIT_VMMCALL] = vmmcall_interception,
  1178. [SVM_EXIT_VMLOAD] = invalid_op_interception,
  1179. [SVM_EXIT_VMSAVE] = invalid_op_interception,
  1180. [SVM_EXIT_STGI] = invalid_op_interception,
  1181. [SVM_EXIT_CLGI] = invalid_op_interception,
  1182. [SVM_EXIT_SKINIT] = invalid_op_interception,
  1183. [SVM_EXIT_WBINVD] = emulate_on_interception,
  1184. [SVM_EXIT_MONITOR] = invalid_op_interception,
  1185. [SVM_EXIT_MWAIT] = invalid_op_interception,
  1186. [SVM_EXIT_NPF] = pf_interception,
  1187. };
  1188. static int handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  1189. {
  1190. struct vcpu_svm *svm = to_svm(vcpu);
  1191. u32 exit_code = svm->vmcb->control.exit_code;
  1192. if (npt_enabled) {
  1193. int mmu_reload = 0;
  1194. if ((vcpu->arch.cr0 ^ svm->vmcb->save.cr0) & X86_CR0_PG) {
  1195. svm_set_cr0(vcpu, svm->vmcb->save.cr0);
  1196. mmu_reload = 1;
  1197. }
  1198. vcpu->arch.cr0 = svm->vmcb->save.cr0;
  1199. vcpu->arch.cr3 = svm->vmcb->save.cr3;
  1200. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  1201. if (!load_pdptrs(vcpu, vcpu->arch.cr3)) {
  1202. kvm_inject_gp(vcpu, 0);
  1203. return 1;
  1204. }
  1205. }
  1206. if (mmu_reload) {
  1207. kvm_mmu_reset_context(vcpu);
  1208. kvm_mmu_load(vcpu);
  1209. }
  1210. }
  1211. kvm_reput_irq(svm);
  1212. if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
  1213. kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  1214. kvm_run->fail_entry.hardware_entry_failure_reason
  1215. = svm->vmcb->control.exit_code;
  1216. return 0;
  1217. }
  1218. if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
  1219. exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
  1220. exit_code != SVM_EXIT_NPF)
  1221. printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
  1222. "exit_code 0x%x\n",
  1223. __func__, svm->vmcb->control.exit_int_info,
  1224. exit_code);
  1225. if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
  1226. || !svm_exit_handlers[exit_code]) {
  1227. kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
  1228. kvm_run->hw.hardware_exit_reason = exit_code;
  1229. return 0;
  1230. }
  1231. return svm_exit_handlers[exit_code](svm, kvm_run);
  1232. }
  1233. static void reload_tss(struct kvm_vcpu *vcpu)
  1234. {
  1235. int cpu = raw_smp_processor_id();
  1236. struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
  1237. svm_data->tss_desc->type = 9; /* available 32/64-bit TSS */
  1238. load_TR_desc();
  1239. }
  1240. static void pre_svm_run(struct vcpu_svm *svm)
  1241. {
  1242. int cpu = raw_smp_processor_id();
  1243. struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
  1244. svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
  1245. if (svm->vcpu.cpu != cpu ||
  1246. svm->asid_generation != svm_data->asid_generation)
  1247. new_asid(svm, svm_data);
  1248. }
  1249. static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
  1250. {
  1251. struct vmcb_control_area *control;
  1252. control = &svm->vmcb->control;
  1253. control->int_vector = irq;
  1254. control->int_ctl &= ~V_INTR_PRIO_MASK;
  1255. control->int_ctl |= V_IRQ_MASK |
  1256. ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
  1257. }
  1258. static void svm_set_irq(struct kvm_vcpu *vcpu, int irq)
  1259. {
  1260. struct vcpu_svm *svm = to_svm(vcpu);
  1261. svm_inject_irq(svm, irq);
  1262. }
  1263. static void svm_intr_assist(struct kvm_vcpu *vcpu)
  1264. {
  1265. struct vcpu_svm *svm = to_svm(vcpu);
  1266. struct vmcb *vmcb = svm->vmcb;
  1267. int intr_vector = -1;
  1268. if ((vmcb->control.exit_int_info & SVM_EVTINJ_VALID) &&
  1269. ((vmcb->control.exit_int_info & SVM_EVTINJ_TYPE_MASK) == 0)) {
  1270. intr_vector = vmcb->control.exit_int_info &
  1271. SVM_EVTINJ_VEC_MASK;
  1272. vmcb->control.exit_int_info = 0;
  1273. svm_inject_irq(svm, intr_vector);
  1274. return;
  1275. }
  1276. if (vmcb->control.int_ctl & V_IRQ_MASK)
  1277. return;
  1278. if (!kvm_cpu_has_interrupt(vcpu))
  1279. return;
  1280. if (!(vmcb->save.rflags & X86_EFLAGS_IF) ||
  1281. (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) ||
  1282. (vmcb->control.event_inj & SVM_EVTINJ_VALID)) {
  1283. /* unable to deliver irq, set pending irq */
  1284. vmcb->control.intercept |= (1ULL << INTERCEPT_VINTR);
  1285. svm_inject_irq(svm, 0x0);
  1286. return;
  1287. }
  1288. /* Okay, we can deliver the interrupt: grab it and update PIC state. */
  1289. intr_vector = kvm_cpu_get_interrupt(vcpu);
  1290. svm_inject_irq(svm, intr_vector);
  1291. kvm_timer_intr_post(vcpu, intr_vector);
  1292. }
  1293. static void kvm_reput_irq(struct vcpu_svm *svm)
  1294. {
  1295. struct vmcb_control_area *control = &svm->vmcb->control;
  1296. if ((control->int_ctl & V_IRQ_MASK)
  1297. && !irqchip_in_kernel(svm->vcpu.kvm)) {
  1298. control->int_ctl &= ~V_IRQ_MASK;
  1299. push_irq(&svm->vcpu, control->int_vector);
  1300. }
  1301. svm->vcpu.arch.interrupt_window_open =
  1302. !(control->int_state & SVM_INTERRUPT_SHADOW_MASK);
  1303. }
  1304. static void svm_do_inject_vector(struct vcpu_svm *svm)
  1305. {
  1306. struct kvm_vcpu *vcpu = &svm->vcpu;
  1307. int word_index = __ffs(vcpu->arch.irq_summary);
  1308. int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
  1309. int irq = word_index * BITS_PER_LONG + bit_index;
  1310. clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
  1311. if (!vcpu->arch.irq_pending[word_index])
  1312. clear_bit(word_index, &vcpu->arch.irq_summary);
  1313. svm_inject_irq(svm, irq);
  1314. }
  1315. static void do_interrupt_requests(struct kvm_vcpu *vcpu,
  1316. struct kvm_run *kvm_run)
  1317. {
  1318. struct vcpu_svm *svm = to_svm(vcpu);
  1319. struct vmcb_control_area *control = &svm->vmcb->control;
  1320. svm->vcpu.arch.interrupt_window_open =
  1321. (!(control->int_state & SVM_INTERRUPT_SHADOW_MASK) &&
  1322. (svm->vmcb->save.rflags & X86_EFLAGS_IF));
  1323. if (svm->vcpu.arch.interrupt_window_open && svm->vcpu.arch.irq_summary)
  1324. /*
  1325. * If interrupts enabled, and not blocked by sti or mov ss. Good.
  1326. */
  1327. svm_do_inject_vector(svm);
  1328. /*
  1329. * Interrupts blocked. Wait for unblock.
  1330. */
  1331. if (!svm->vcpu.arch.interrupt_window_open &&
  1332. (svm->vcpu.arch.irq_summary || kvm_run->request_interrupt_window))
  1333. control->intercept |= 1ULL << INTERCEPT_VINTR;
  1334. else
  1335. control->intercept &= ~(1ULL << INTERCEPT_VINTR);
  1336. }
  1337. static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
  1338. {
  1339. return 0;
  1340. }
  1341. static void save_db_regs(unsigned long *db_regs)
  1342. {
  1343. asm volatile ("mov %%dr0, %0" : "=r"(db_regs[0]));
  1344. asm volatile ("mov %%dr1, %0" : "=r"(db_regs[1]));
  1345. asm volatile ("mov %%dr2, %0" : "=r"(db_regs[2]));
  1346. asm volatile ("mov %%dr3, %0" : "=r"(db_regs[3]));
  1347. }
  1348. static void load_db_regs(unsigned long *db_regs)
  1349. {
  1350. asm volatile ("mov %0, %%dr0" : : "r"(db_regs[0]));
  1351. asm volatile ("mov %0, %%dr1" : : "r"(db_regs[1]));
  1352. asm volatile ("mov %0, %%dr2" : : "r"(db_regs[2]));
  1353. asm volatile ("mov %0, %%dr3" : : "r"(db_regs[3]));
  1354. }
  1355. static void svm_flush_tlb(struct kvm_vcpu *vcpu)
  1356. {
  1357. force_new_asid(vcpu);
  1358. }
  1359. static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
  1360. {
  1361. }
  1362. static void svm_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  1363. {
  1364. struct vcpu_svm *svm = to_svm(vcpu);
  1365. u16 fs_selector;
  1366. u16 gs_selector;
  1367. u16 ldt_selector;
  1368. pre_svm_run(svm);
  1369. save_host_msrs(vcpu);
  1370. fs_selector = read_fs();
  1371. gs_selector = read_gs();
  1372. ldt_selector = read_ldt();
  1373. svm->host_cr2 = kvm_read_cr2();
  1374. svm->host_dr6 = read_dr6();
  1375. svm->host_dr7 = read_dr7();
  1376. svm->vmcb->save.cr2 = vcpu->arch.cr2;
  1377. /* required for live migration with NPT */
  1378. if (npt_enabled)
  1379. svm->vmcb->save.cr3 = vcpu->arch.cr3;
  1380. if (svm->vmcb->save.dr7 & 0xff) {
  1381. write_dr7(0);
  1382. save_db_regs(svm->host_db_regs);
  1383. load_db_regs(svm->db_regs);
  1384. }
  1385. clgi();
  1386. local_irq_enable();
  1387. asm volatile (
  1388. #ifdef CONFIG_X86_64
  1389. "push %%rbp; \n\t"
  1390. #else
  1391. "push %%ebp; \n\t"
  1392. #endif
  1393. #ifdef CONFIG_X86_64
  1394. "mov %c[rbx](%[svm]), %%rbx \n\t"
  1395. "mov %c[rcx](%[svm]), %%rcx \n\t"
  1396. "mov %c[rdx](%[svm]), %%rdx \n\t"
  1397. "mov %c[rsi](%[svm]), %%rsi \n\t"
  1398. "mov %c[rdi](%[svm]), %%rdi \n\t"
  1399. "mov %c[rbp](%[svm]), %%rbp \n\t"
  1400. "mov %c[r8](%[svm]), %%r8 \n\t"
  1401. "mov %c[r9](%[svm]), %%r9 \n\t"
  1402. "mov %c[r10](%[svm]), %%r10 \n\t"
  1403. "mov %c[r11](%[svm]), %%r11 \n\t"
  1404. "mov %c[r12](%[svm]), %%r12 \n\t"
  1405. "mov %c[r13](%[svm]), %%r13 \n\t"
  1406. "mov %c[r14](%[svm]), %%r14 \n\t"
  1407. "mov %c[r15](%[svm]), %%r15 \n\t"
  1408. #else
  1409. "mov %c[rbx](%[svm]), %%ebx \n\t"
  1410. "mov %c[rcx](%[svm]), %%ecx \n\t"
  1411. "mov %c[rdx](%[svm]), %%edx \n\t"
  1412. "mov %c[rsi](%[svm]), %%esi \n\t"
  1413. "mov %c[rdi](%[svm]), %%edi \n\t"
  1414. "mov %c[rbp](%[svm]), %%ebp \n\t"
  1415. #endif
  1416. #ifdef CONFIG_X86_64
  1417. /* Enter guest mode */
  1418. "push %%rax \n\t"
  1419. "mov %c[vmcb](%[svm]), %%rax \n\t"
  1420. SVM_VMLOAD "\n\t"
  1421. SVM_VMRUN "\n\t"
  1422. SVM_VMSAVE "\n\t"
  1423. "pop %%rax \n\t"
  1424. #else
  1425. /* Enter guest mode */
  1426. "push %%eax \n\t"
  1427. "mov %c[vmcb](%[svm]), %%eax \n\t"
  1428. SVM_VMLOAD "\n\t"
  1429. SVM_VMRUN "\n\t"
  1430. SVM_VMSAVE "\n\t"
  1431. "pop %%eax \n\t"
  1432. #endif
  1433. /* Save guest registers, load host registers */
  1434. #ifdef CONFIG_X86_64
  1435. "mov %%rbx, %c[rbx](%[svm]) \n\t"
  1436. "mov %%rcx, %c[rcx](%[svm]) \n\t"
  1437. "mov %%rdx, %c[rdx](%[svm]) \n\t"
  1438. "mov %%rsi, %c[rsi](%[svm]) \n\t"
  1439. "mov %%rdi, %c[rdi](%[svm]) \n\t"
  1440. "mov %%rbp, %c[rbp](%[svm]) \n\t"
  1441. "mov %%r8, %c[r8](%[svm]) \n\t"
  1442. "mov %%r9, %c[r9](%[svm]) \n\t"
  1443. "mov %%r10, %c[r10](%[svm]) \n\t"
  1444. "mov %%r11, %c[r11](%[svm]) \n\t"
  1445. "mov %%r12, %c[r12](%[svm]) \n\t"
  1446. "mov %%r13, %c[r13](%[svm]) \n\t"
  1447. "mov %%r14, %c[r14](%[svm]) \n\t"
  1448. "mov %%r15, %c[r15](%[svm]) \n\t"
  1449. "pop %%rbp; \n\t"
  1450. #else
  1451. "mov %%ebx, %c[rbx](%[svm]) \n\t"
  1452. "mov %%ecx, %c[rcx](%[svm]) \n\t"
  1453. "mov %%edx, %c[rdx](%[svm]) \n\t"
  1454. "mov %%esi, %c[rsi](%[svm]) \n\t"
  1455. "mov %%edi, %c[rdi](%[svm]) \n\t"
  1456. "mov %%ebp, %c[rbp](%[svm]) \n\t"
  1457. "pop %%ebp; \n\t"
  1458. #endif
  1459. :
  1460. : [svm]"a"(svm),
  1461. [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
  1462. [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
  1463. [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
  1464. [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
  1465. [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
  1466. [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
  1467. [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
  1468. #ifdef CONFIG_X86_64
  1469. , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
  1470. [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
  1471. [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
  1472. [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
  1473. [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
  1474. [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
  1475. [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
  1476. [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
  1477. #endif
  1478. : "cc", "memory"
  1479. #ifdef CONFIG_X86_64
  1480. , "rbx", "rcx", "rdx", "rsi", "rdi"
  1481. , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
  1482. #else
  1483. , "ebx", "ecx", "edx" , "esi", "edi"
  1484. #endif
  1485. );
  1486. if ((svm->vmcb->save.dr7 & 0xff))
  1487. load_db_regs(svm->host_db_regs);
  1488. vcpu->arch.cr2 = svm->vmcb->save.cr2;
  1489. write_dr6(svm->host_dr6);
  1490. write_dr7(svm->host_dr7);
  1491. kvm_write_cr2(svm->host_cr2);
  1492. load_fs(fs_selector);
  1493. load_gs(gs_selector);
  1494. load_ldt(ldt_selector);
  1495. load_host_msrs(vcpu);
  1496. reload_tss(vcpu);
  1497. local_irq_disable();
  1498. stgi();
  1499. svm->next_rip = 0;
  1500. }
  1501. static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  1502. {
  1503. struct vcpu_svm *svm = to_svm(vcpu);
  1504. if (npt_enabled) {
  1505. svm->vmcb->control.nested_cr3 = root;
  1506. force_new_asid(vcpu);
  1507. return;
  1508. }
  1509. svm->vmcb->save.cr3 = root;
  1510. force_new_asid(vcpu);
  1511. if (vcpu->fpu_active) {
  1512. svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
  1513. svm->vmcb->save.cr0 |= X86_CR0_TS;
  1514. vcpu->fpu_active = 0;
  1515. }
  1516. }
  1517. static int is_disabled(void)
  1518. {
  1519. u64 vm_cr;
  1520. rdmsrl(MSR_VM_CR, vm_cr);
  1521. if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
  1522. return 1;
  1523. return 0;
  1524. }
  1525. static void
  1526. svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  1527. {
  1528. /*
  1529. * Patch in the VMMCALL instruction:
  1530. */
  1531. hypercall[0] = 0x0f;
  1532. hypercall[1] = 0x01;
  1533. hypercall[2] = 0xd9;
  1534. }
  1535. static void svm_check_processor_compat(void *rtn)
  1536. {
  1537. *(int *)rtn = 0;
  1538. }
  1539. static bool svm_cpu_has_accelerated_tpr(void)
  1540. {
  1541. return false;
  1542. }
  1543. static struct kvm_x86_ops svm_x86_ops = {
  1544. .cpu_has_kvm_support = has_svm,
  1545. .disabled_by_bios = is_disabled,
  1546. .hardware_setup = svm_hardware_setup,
  1547. .hardware_unsetup = svm_hardware_unsetup,
  1548. .check_processor_compatibility = svm_check_processor_compat,
  1549. .hardware_enable = svm_hardware_enable,
  1550. .hardware_disable = svm_hardware_disable,
  1551. .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
  1552. .vcpu_create = svm_create_vcpu,
  1553. .vcpu_free = svm_free_vcpu,
  1554. .vcpu_reset = svm_vcpu_reset,
  1555. .prepare_guest_switch = svm_prepare_guest_switch,
  1556. .vcpu_load = svm_vcpu_load,
  1557. .vcpu_put = svm_vcpu_put,
  1558. .vcpu_decache = svm_vcpu_decache,
  1559. .set_guest_debug = svm_guest_debug,
  1560. .get_msr = svm_get_msr,
  1561. .set_msr = svm_set_msr,
  1562. .get_segment_base = svm_get_segment_base,
  1563. .get_segment = svm_get_segment,
  1564. .set_segment = svm_set_segment,
  1565. .get_cpl = svm_get_cpl,
  1566. .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
  1567. .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
  1568. .set_cr0 = svm_set_cr0,
  1569. .set_cr3 = svm_set_cr3,
  1570. .set_cr4 = svm_set_cr4,
  1571. .set_efer = svm_set_efer,
  1572. .get_idt = svm_get_idt,
  1573. .set_idt = svm_set_idt,
  1574. .get_gdt = svm_get_gdt,
  1575. .set_gdt = svm_set_gdt,
  1576. .get_dr = svm_get_dr,
  1577. .set_dr = svm_set_dr,
  1578. .cache_regs = svm_cache_regs,
  1579. .decache_regs = svm_decache_regs,
  1580. .get_rflags = svm_get_rflags,
  1581. .set_rflags = svm_set_rflags,
  1582. .tlb_flush = svm_flush_tlb,
  1583. .run = svm_vcpu_run,
  1584. .handle_exit = handle_exit,
  1585. .skip_emulated_instruction = skip_emulated_instruction,
  1586. .patch_hypercall = svm_patch_hypercall,
  1587. .get_irq = svm_get_irq,
  1588. .set_irq = svm_set_irq,
  1589. .queue_exception = svm_queue_exception,
  1590. .exception_injected = svm_exception_injected,
  1591. .inject_pending_irq = svm_intr_assist,
  1592. .inject_pending_vectors = do_interrupt_requests,
  1593. .set_tss_addr = svm_set_tss_addr,
  1594. };
  1595. static int __init svm_init(void)
  1596. {
  1597. return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
  1598. THIS_MODULE);
  1599. }
  1600. static void __exit svm_exit(void)
  1601. {
  1602. kvm_exit();
  1603. }
  1604. module_init(svm_init)
  1605. module_exit(svm_exit)