svm.c 43 KB

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