svm.c 42 KB

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