svm.c 45 KB

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