svm.c 43 KB

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