svm.c 42 KB

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