svm.c 44 KB

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