svm.c 42 KB

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