svm.c 40 KB

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