svm.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791
  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/profile.h>
  24. #include <linux/sched.h>
  25. #include <asm/desc.h>
  26. MODULE_AUTHOR("Qumranet");
  27. MODULE_LICENSE("GPL");
  28. #define IOPM_ALLOC_ORDER 2
  29. #define MSRPM_ALLOC_ORDER 1
  30. #define DB_VECTOR 1
  31. #define UD_VECTOR 6
  32. #define GP_VECTOR 13
  33. #define DR7_GD_MASK (1 << 13)
  34. #define DR6_BD_MASK (1 << 13)
  35. #define SEG_TYPE_LDT 2
  36. #define SEG_TYPE_BUSY_TSS16 3
  37. #define KVM_EFER_LMA (1 << 10)
  38. #define KVM_EFER_LME (1 << 8)
  39. #define SVM_FEATURE_NPT (1 << 0)
  40. #define SVM_FEATURE_LBRV (1 << 1)
  41. #define SVM_DEATURE_SVML (1 << 2)
  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 Xgt_desc_struct gdt_descr;
  234. #endif
  235. struct desc_struct *gdt;
  236. int me = raw_smp_processor_id();
  237. if (!has_svm()) {
  238. printk(KERN_ERR "svm_cpu_init: err EOPNOTSUPP on %d\n", me);
  239. return;
  240. }
  241. svm_data = per_cpu(svm_data, me);
  242. if (!svm_data) {
  243. printk(KERN_ERR "svm_cpu_init: svm_data is NULL on %d\n",
  244. me);
  245. return;
  246. }
  247. svm_data->asid_generation = 1;
  248. svm_data->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
  249. svm_data->next_asid = svm_data->max_asid + 1;
  250. 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. kvm_emulator_want_group7_invlpg();
  304. iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
  305. if (!iopm_pages)
  306. return -ENOMEM;
  307. iopm_va = page_address(iopm_pages);
  308. memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
  309. clear_bit(0x80, iopm_va); /* allow direct access to PC debug port */
  310. iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
  311. msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
  312. r = -ENOMEM;
  313. if (!msrpm_pages)
  314. goto err_1;
  315. msrpm_va = page_address(msrpm_pages);
  316. memset(msrpm_va, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
  317. msrpm_base = page_to_pfn(msrpm_pages) << PAGE_SHIFT;
  318. #ifdef CONFIG_X86_64
  319. set_msr_interception(msrpm_va, MSR_GS_BASE, 1, 1);
  320. set_msr_interception(msrpm_va, MSR_FS_BASE, 1, 1);
  321. set_msr_interception(msrpm_va, MSR_KERNEL_GS_BASE, 1, 1);
  322. set_msr_interception(msrpm_va, MSR_LSTAR, 1, 1);
  323. set_msr_interception(msrpm_va, MSR_CSTAR, 1, 1);
  324. set_msr_interception(msrpm_va, MSR_SYSCALL_MASK, 1, 1);
  325. #endif
  326. set_msr_interception(msrpm_va, MSR_K6_STAR, 1, 1);
  327. set_msr_interception(msrpm_va, MSR_IA32_SYSENTER_CS, 1, 1);
  328. set_msr_interception(msrpm_va, MSR_IA32_SYSENTER_ESP, 1, 1);
  329. set_msr_interception(msrpm_va, MSR_IA32_SYSENTER_EIP, 1, 1);
  330. for_each_online_cpu(cpu) {
  331. r = svm_cpu_init(cpu);
  332. if (r)
  333. goto err_2;
  334. }
  335. return 0;
  336. err_2:
  337. __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
  338. msrpm_base = 0;
  339. err_1:
  340. __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
  341. iopm_base = 0;
  342. return r;
  343. }
  344. static __exit void svm_hardware_unsetup(void)
  345. {
  346. __free_pages(pfn_to_page(msrpm_base >> PAGE_SHIFT), MSRPM_ALLOC_ORDER);
  347. __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
  348. iopm_base = msrpm_base = 0;
  349. }
  350. static void init_seg(struct vmcb_seg *seg)
  351. {
  352. seg->selector = 0;
  353. seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
  354. SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
  355. seg->limit = 0xffff;
  356. seg->base = 0;
  357. }
  358. static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
  359. {
  360. seg->selector = 0;
  361. seg->attrib = SVM_SELECTOR_P_MASK | type;
  362. seg->limit = 0xffff;
  363. seg->base = 0;
  364. }
  365. static void init_vmcb(struct vmcb *vmcb)
  366. {
  367. struct vmcb_control_area *control = &vmcb->control;
  368. struct vmcb_save_area *save = &vmcb->save;
  369. control->intercept_cr_read = INTERCEPT_CR0_MASK |
  370. INTERCEPT_CR3_MASK |
  371. INTERCEPT_CR4_MASK;
  372. control->intercept_cr_write = INTERCEPT_CR0_MASK |
  373. INTERCEPT_CR3_MASK |
  374. INTERCEPT_CR4_MASK;
  375. control->intercept_dr_read = INTERCEPT_DR0_MASK |
  376. INTERCEPT_DR1_MASK |
  377. INTERCEPT_DR2_MASK |
  378. INTERCEPT_DR3_MASK;
  379. control->intercept_dr_write = INTERCEPT_DR0_MASK |
  380. INTERCEPT_DR1_MASK |
  381. INTERCEPT_DR2_MASK |
  382. INTERCEPT_DR3_MASK |
  383. INTERCEPT_DR5_MASK |
  384. INTERCEPT_DR7_MASK;
  385. control->intercept_exceptions = 1 << PF_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_HLT) |
  402. (1ULL << INTERCEPT_INVLPGA) |
  403. (1ULL << INTERCEPT_IOIO_PROT) |
  404. (1ULL << INTERCEPT_MSR_PROT) |
  405. (1ULL << INTERCEPT_TASK_SWITCH) |
  406. (1ULL << INTERCEPT_SHUTDOWN) |
  407. (1ULL << INTERCEPT_VMRUN) |
  408. (1ULL << INTERCEPT_VMMCALL) |
  409. (1ULL << INTERCEPT_VMLOAD) |
  410. (1ULL << INTERCEPT_VMSAVE) |
  411. (1ULL << INTERCEPT_STGI) |
  412. (1ULL << INTERCEPT_CLGI) |
  413. (1ULL << INTERCEPT_SKINIT) |
  414. (1ULL << INTERCEPT_MONITOR) |
  415. (1ULL << INTERCEPT_MWAIT);
  416. control->iopm_base_pa = iopm_base;
  417. control->msrpm_base_pa = msrpm_base;
  418. control->tsc_offset = 0;
  419. control->int_ctl = V_INTR_MASKING_MASK;
  420. init_seg(&save->es);
  421. init_seg(&save->ss);
  422. init_seg(&save->ds);
  423. init_seg(&save->fs);
  424. init_seg(&save->gs);
  425. save->cs.selector = 0xf000;
  426. /* Executable/Readable Code Segment */
  427. save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
  428. SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
  429. save->cs.limit = 0xffff;
  430. /*
  431. * cs.base should really be 0xffff0000, but vmx can't handle that, so
  432. * be consistent with it.
  433. *
  434. * Replace when we have real mode working for vmx.
  435. */
  436. save->cs.base = 0xf0000;
  437. save->gdtr.limit = 0xffff;
  438. save->idtr.limit = 0xffff;
  439. init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
  440. init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
  441. save->efer = MSR_EFER_SVME_MASK;
  442. save->dr6 = 0xffff0ff0;
  443. save->dr7 = 0x400;
  444. save->rflags = 2;
  445. save->rip = 0x0000fff0;
  446. /*
  447. * cr0 val on cpu init should be 0x60000010, we enable cpu
  448. * cache by default. the orderly way is to enable cache in bios.
  449. */
  450. save->cr0 = 0x00000010 | X86_CR0_PG | X86_CR0_WP;
  451. save->cr4 = X86_CR4_PAE;
  452. /* rdx = ?? */
  453. }
  454. static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
  455. {
  456. struct vcpu_svm *svm;
  457. struct page *page;
  458. int err;
  459. svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  460. if (!svm) {
  461. err = -ENOMEM;
  462. goto out;
  463. }
  464. err = kvm_vcpu_init(&svm->vcpu, kvm, id);
  465. if (err)
  466. goto free_svm;
  467. page = alloc_page(GFP_KERNEL);
  468. if (!page) {
  469. err = -ENOMEM;
  470. goto uninit;
  471. }
  472. svm->vmcb = page_address(page);
  473. clear_page(svm->vmcb);
  474. svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
  475. svm->asid_generation = 0;
  476. memset(svm->db_regs, 0, sizeof(svm->db_regs));
  477. init_vmcb(svm->vmcb);
  478. fx_init(&svm->vcpu);
  479. svm->vcpu.fpu_active = 1;
  480. svm->vcpu.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
  481. if (svm->vcpu.vcpu_id == 0)
  482. svm->vcpu.apic_base |= MSR_IA32_APICBASE_BSP;
  483. return &svm->vcpu;
  484. uninit:
  485. kvm_vcpu_uninit(&svm->vcpu);
  486. free_svm:
  487. kmem_cache_free(kvm_vcpu_cache, svm);
  488. out:
  489. return ERR_PTR(err);
  490. }
  491. static void svm_free_vcpu(struct kvm_vcpu *vcpu)
  492. {
  493. struct vcpu_svm *svm = to_svm(vcpu);
  494. __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
  495. kvm_vcpu_uninit(vcpu);
  496. kmem_cache_free(kvm_vcpu_cache, svm);
  497. }
  498. static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  499. {
  500. struct vcpu_svm *svm = to_svm(vcpu);
  501. int i;
  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. svm->vmcb->control.tsc_offset += delta;
  511. vcpu->cpu = cpu;
  512. }
  513. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  514. rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  515. }
  516. static void svm_vcpu_put(struct kvm_vcpu *vcpu)
  517. {
  518. struct vcpu_svm *svm = to_svm(vcpu);
  519. int i;
  520. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  521. wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  522. rdtscll(vcpu->host_tsc);
  523. }
  524. static void svm_vcpu_decache(struct kvm_vcpu *vcpu)
  525. {
  526. }
  527. static void svm_cache_regs(struct kvm_vcpu *vcpu)
  528. {
  529. struct vcpu_svm *svm = to_svm(vcpu);
  530. vcpu->regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
  531. vcpu->regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
  532. vcpu->rip = svm->vmcb->save.rip;
  533. }
  534. static void svm_decache_regs(struct kvm_vcpu *vcpu)
  535. {
  536. struct vcpu_svm *svm = to_svm(vcpu);
  537. svm->vmcb->save.rax = vcpu->regs[VCPU_REGS_RAX];
  538. svm->vmcb->save.rsp = vcpu->regs[VCPU_REGS_RSP];
  539. svm->vmcb->save.rip = vcpu->rip;
  540. }
  541. static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
  542. {
  543. return to_svm(vcpu)->vmcb->save.rflags;
  544. }
  545. static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  546. {
  547. to_svm(vcpu)->vmcb->save.rflags = rflags;
  548. }
  549. static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
  550. {
  551. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  552. switch (seg) {
  553. case VCPU_SREG_CS: return &save->cs;
  554. case VCPU_SREG_DS: return &save->ds;
  555. case VCPU_SREG_ES: return &save->es;
  556. case VCPU_SREG_FS: return &save->fs;
  557. case VCPU_SREG_GS: return &save->gs;
  558. case VCPU_SREG_SS: return &save->ss;
  559. case VCPU_SREG_TR: return &save->tr;
  560. case VCPU_SREG_LDTR: return &save->ldtr;
  561. }
  562. BUG();
  563. return NULL;
  564. }
  565. static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  566. {
  567. struct vmcb_seg *s = svm_seg(vcpu, seg);
  568. return s->base;
  569. }
  570. static void svm_get_segment(struct kvm_vcpu *vcpu,
  571. struct kvm_segment *var, int seg)
  572. {
  573. struct vmcb_seg *s = svm_seg(vcpu, seg);
  574. var->base = s->base;
  575. var->limit = s->limit;
  576. var->selector = s->selector;
  577. var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
  578. var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
  579. var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
  580. var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
  581. var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
  582. var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
  583. var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
  584. var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
  585. var->unusable = !var->present;
  586. }
  587. static void svm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  588. {
  589. struct vmcb_seg *s = svm_seg(vcpu, VCPU_SREG_CS);
  590. *db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
  591. *l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
  592. }
  593. static void svm_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  594. {
  595. struct vcpu_svm *svm = to_svm(vcpu);
  596. dt->limit = svm->vmcb->save.idtr.limit;
  597. dt->base = svm->vmcb->save.idtr.base;
  598. }
  599. static void svm_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  600. {
  601. struct vcpu_svm *svm = to_svm(vcpu);
  602. svm->vmcb->save.idtr.limit = dt->limit;
  603. svm->vmcb->save.idtr.base = dt->base ;
  604. }
  605. static void svm_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  606. {
  607. struct vcpu_svm *svm = to_svm(vcpu);
  608. dt->limit = svm->vmcb->save.gdtr.limit;
  609. dt->base = svm->vmcb->save.gdtr.base;
  610. }
  611. static void svm_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
  612. {
  613. struct vcpu_svm *svm = to_svm(vcpu);
  614. svm->vmcb->save.gdtr.limit = dt->limit;
  615. svm->vmcb->save.gdtr.base = dt->base ;
  616. }
  617. static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  618. {
  619. }
  620. static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  621. {
  622. struct vcpu_svm *svm = to_svm(vcpu);
  623. #ifdef CONFIG_X86_64
  624. if (vcpu->shadow_efer & KVM_EFER_LME) {
  625. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  626. vcpu->shadow_efer |= KVM_EFER_LMA;
  627. svm->vmcb->save.efer |= KVM_EFER_LMA | KVM_EFER_LME;
  628. }
  629. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG) ) {
  630. vcpu->shadow_efer &= ~KVM_EFER_LMA;
  631. svm->vmcb->save.efer &= ~(KVM_EFER_LMA | KVM_EFER_LME);
  632. }
  633. }
  634. #endif
  635. if ((vcpu->cr0 & X86_CR0_TS) && !(cr0 & X86_CR0_TS)) {
  636. svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
  637. vcpu->fpu_active = 1;
  638. }
  639. vcpu->cr0 = cr0;
  640. cr0 |= X86_CR0_PG | X86_CR0_WP;
  641. cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
  642. svm->vmcb->save.cr0 = cr0;
  643. }
  644. static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  645. {
  646. vcpu->cr4 = cr4;
  647. to_svm(vcpu)->vmcb->save.cr4 = cr4 | X86_CR4_PAE;
  648. }
  649. static void svm_set_segment(struct kvm_vcpu *vcpu,
  650. struct kvm_segment *var, int seg)
  651. {
  652. struct vcpu_svm *svm = to_svm(vcpu);
  653. struct vmcb_seg *s = svm_seg(vcpu, seg);
  654. s->base = var->base;
  655. s->limit = var->limit;
  656. s->selector = var->selector;
  657. if (var->unusable)
  658. s->attrib = 0;
  659. else {
  660. s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
  661. s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
  662. s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
  663. s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
  664. s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
  665. s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
  666. s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
  667. s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
  668. }
  669. if (seg == VCPU_SREG_CS)
  670. svm->vmcb->save.cpl
  671. = (svm->vmcb->save.cs.attrib
  672. >> SVM_SELECTOR_DPL_SHIFT) & 3;
  673. }
  674. /* FIXME:
  675. svm(vcpu)->vmcb->control.int_ctl &= ~V_TPR_MASK;
  676. svm(vcpu)->vmcb->control.int_ctl |= (sregs->cr8 & V_TPR_MASK);
  677. */
  678. static int svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
  679. {
  680. return -EOPNOTSUPP;
  681. }
  682. static void load_host_msrs(struct kvm_vcpu *vcpu)
  683. {
  684. #ifdef CONFIG_X86_64
  685. wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
  686. #endif
  687. }
  688. static void save_host_msrs(struct kvm_vcpu *vcpu)
  689. {
  690. #ifdef CONFIG_X86_64
  691. rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
  692. #endif
  693. }
  694. static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *svm_data)
  695. {
  696. if (svm_data->next_asid > svm_data->max_asid) {
  697. ++svm_data->asid_generation;
  698. svm_data->next_asid = 1;
  699. svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
  700. }
  701. svm->vcpu.cpu = svm_data->cpu;
  702. svm->asid_generation = svm_data->asid_generation;
  703. svm->vmcb->control.asid = svm_data->next_asid++;
  704. }
  705. static void svm_invlpg(struct kvm_vcpu *vcpu, gva_t address)
  706. {
  707. invlpga(address, to_svm(vcpu)->vmcb->control.asid); // is needed?
  708. }
  709. static unsigned long svm_get_dr(struct kvm_vcpu *vcpu, int dr)
  710. {
  711. return to_svm(vcpu)->db_regs[dr];
  712. }
  713. static void svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value,
  714. int *exception)
  715. {
  716. struct vcpu_svm *svm = to_svm(vcpu);
  717. *exception = 0;
  718. if (svm->vmcb->save.dr7 & DR7_GD_MASK) {
  719. svm->vmcb->save.dr7 &= ~DR7_GD_MASK;
  720. svm->vmcb->save.dr6 |= DR6_BD_MASK;
  721. *exception = DB_VECTOR;
  722. return;
  723. }
  724. switch (dr) {
  725. case 0 ... 3:
  726. svm->db_regs[dr] = value;
  727. return;
  728. case 4 ... 5:
  729. if (vcpu->cr4 & X86_CR4_DE) {
  730. *exception = UD_VECTOR;
  731. return;
  732. }
  733. case 7: {
  734. if (value & ~((1ULL << 32) - 1)) {
  735. *exception = GP_VECTOR;
  736. return;
  737. }
  738. svm->vmcb->save.dr7 = value;
  739. return;
  740. }
  741. default:
  742. printk(KERN_DEBUG "%s: unexpected dr %u\n",
  743. __FUNCTION__, dr);
  744. *exception = UD_VECTOR;
  745. return;
  746. }
  747. }
  748. static int pf_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  749. {
  750. u32 exit_int_info = svm->vmcb->control.exit_int_info;
  751. struct kvm *kvm = svm->vcpu.kvm;
  752. u64 fault_address;
  753. u32 error_code;
  754. enum emulation_result er;
  755. int r;
  756. if (!irqchip_in_kernel(kvm) &&
  757. is_external_interrupt(exit_int_info))
  758. push_irq(&svm->vcpu, exit_int_info & SVM_EVTINJ_VEC_MASK);
  759. mutex_lock(&kvm->lock);
  760. fault_address = svm->vmcb->control.exit_info_2;
  761. error_code = svm->vmcb->control.exit_info_1;
  762. r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
  763. if (r < 0) {
  764. mutex_unlock(&kvm->lock);
  765. return r;
  766. }
  767. if (!r) {
  768. mutex_unlock(&kvm->lock);
  769. return 1;
  770. }
  771. er = emulate_instruction(&svm->vcpu, kvm_run, fault_address,
  772. error_code);
  773. mutex_unlock(&kvm->lock);
  774. switch (er) {
  775. case EMULATE_DONE:
  776. return 1;
  777. case EMULATE_DO_MMIO:
  778. ++svm->vcpu.stat.mmio_exits;
  779. return 0;
  780. case EMULATE_FAIL:
  781. vcpu_printf(&svm->vcpu, "%s: emulate fail\n", __FUNCTION__);
  782. break;
  783. default:
  784. BUG();
  785. }
  786. kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
  787. return 0;
  788. }
  789. static int nm_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  790. {
  791. svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
  792. if (!(svm->vcpu.cr0 & X86_CR0_TS))
  793. svm->vmcb->save.cr0 &= ~X86_CR0_TS;
  794. svm->vcpu.fpu_active = 1;
  795. return 1;
  796. }
  797. static int shutdown_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  798. {
  799. /*
  800. * VMCB is undefined after a SHUTDOWN intercept
  801. * so reinitialize it.
  802. */
  803. clear_page(svm->vmcb);
  804. init_vmcb(svm->vmcb);
  805. kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
  806. return 0;
  807. }
  808. static int io_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  809. {
  810. u32 io_info = svm->vmcb->control.exit_info_1; //address size bug?
  811. int size, down, in, string, rep;
  812. unsigned port;
  813. ++svm->vcpu.stat.io_exits;
  814. svm->next_rip = svm->vmcb->control.exit_info_2;
  815. string = (io_info & SVM_IOIO_STR_MASK) != 0;
  816. if (string) {
  817. if (emulate_instruction(&svm->vcpu, kvm_run, 0, 0) == EMULATE_DO_MMIO)
  818. return 0;
  819. return 1;
  820. }
  821. in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
  822. port = io_info >> 16;
  823. size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
  824. rep = (io_info & SVM_IOIO_REP_MASK) != 0;
  825. down = (svm->vmcb->save.rflags & X86_EFLAGS_DF) != 0;
  826. return kvm_emulate_pio(&svm->vcpu, kvm_run, in, size, port);
  827. }
  828. static int nop_on_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  829. {
  830. return 1;
  831. }
  832. static int halt_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  833. {
  834. svm->next_rip = svm->vmcb->save.rip + 1;
  835. skip_emulated_instruction(&svm->vcpu);
  836. return kvm_emulate_halt(&svm->vcpu);
  837. }
  838. static int vmmcall_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  839. {
  840. svm->next_rip = svm->vmcb->save.rip + 3;
  841. skip_emulated_instruction(&svm->vcpu);
  842. return kvm_hypercall(&svm->vcpu, kvm_run);
  843. }
  844. static int invalid_op_interception(struct vcpu_svm *svm,
  845. struct kvm_run *kvm_run)
  846. {
  847. inject_ud(&svm->vcpu);
  848. return 1;
  849. }
  850. static int task_switch_interception(struct vcpu_svm *svm,
  851. struct kvm_run *kvm_run)
  852. {
  853. pr_unimpl(&svm->vcpu, "%s: task switch is unsupported\n", __FUNCTION__);
  854. kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
  855. return 0;
  856. }
  857. static int cpuid_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  858. {
  859. svm->next_rip = svm->vmcb->save.rip + 2;
  860. kvm_emulate_cpuid(&svm->vcpu);
  861. return 1;
  862. }
  863. static int emulate_on_interception(struct vcpu_svm *svm,
  864. struct kvm_run *kvm_run)
  865. {
  866. if (emulate_instruction(&svm->vcpu, NULL, 0, 0) != EMULATE_DONE)
  867. pr_unimpl(&svm->vcpu, "%s: failed\n", __FUNCTION__);
  868. return 1;
  869. }
  870. static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
  871. {
  872. struct vcpu_svm *svm = to_svm(vcpu);
  873. switch (ecx) {
  874. case MSR_IA32_TIME_STAMP_COUNTER: {
  875. u64 tsc;
  876. rdtscll(tsc);
  877. *data = svm->vmcb->control.tsc_offset + tsc;
  878. break;
  879. }
  880. case MSR_K6_STAR:
  881. *data = svm->vmcb->save.star;
  882. break;
  883. #ifdef CONFIG_X86_64
  884. case MSR_LSTAR:
  885. *data = svm->vmcb->save.lstar;
  886. break;
  887. case MSR_CSTAR:
  888. *data = svm->vmcb->save.cstar;
  889. break;
  890. case MSR_KERNEL_GS_BASE:
  891. *data = svm->vmcb->save.kernel_gs_base;
  892. break;
  893. case MSR_SYSCALL_MASK:
  894. *data = svm->vmcb->save.sfmask;
  895. break;
  896. #endif
  897. case MSR_IA32_SYSENTER_CS:
  898. *data = svm->vmcb->save.sysenter_cs;
  899. break;
  900. case MSR_IA32_SYSENTER_EIP:
  901. *data = svm->vmcb->save.sysenter_eip;
  902. break;
  903. case MSR_IA32_SYSENTER_ESP:
  904. *data = svm->vmcb->save.sysenter_esp;
  905. break;
  906. default:
  907. return kvm_get_msr_common(vcpu, ecx, data);
  908. }
  909. return 0;
  910. }
  911. static int rdmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  912. {
  913. u32 ecx = svm->vcpu.regs[VCPU_REGS_RCX];
  914. u64 data;
  915. if (svm_get_msr(&svm->vcpu, ecx, &data))
  916. svm_inject_gp(&svm->vcpu, 0);
  917. else {
  918. svm->vmcb->save.rax = data & 0xffffffff;
  919. svm->vcpu.regs[VCPU_REGS_RDX] = data >> 32;
  920. svm->next_rip = svm->vmcb->save.rip + 2;
  921. skip_emulated_instruction(&svm->vcpu);
  922. }
  923. return 1;
  924. }
  925. static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
  926. {
  927. struct vcpu_svm *svm = to_svm(vcpu);
  928. switch (ecx) {
  929. case MSR_IA32_TIME_STAMP_COUNTER: {
  930. u64 tsc;
  931. rdtscll(tsc);
  932. svm->vmcb->control.tsc_offset = data - tsc;
  933. break;
  934. }
  935. case MSR_K6_STAR:
  936. svm->vmcb->save.star = data;
  937. break;
  938. #ifdef CONFIG_X86_64
  939. case MSR_LSTAR:
  940. svm->vmcb->save.lstar = data;
  941. break;
  942. case MSR_CSTAR:
  943. svm->vmcb->save.cstar = data;
  944. break;
  945. case MSR_KERNEL_GS_BASE:
  946. svm->vmcb->save.kernel_gs_base = data;
  947. break;
  948. case MSR_SYSCALL_MASK:
  949. svm->vmcb->save.sfmask = data;
  950. break;
  951. #endif
  952. case MSR_IA32_SYSENTER_CS:
  953. svm->vmcb->save.sysenter_cs = data;
  954. break;
  955. case MSR_IA32_SYSENTER_EIP:
  956. svm->vmcb->save.sysenter_eip = data;
  957. break;
  958. case MSR_IA32_SYSENTER_ESP:
  959. svm->vmcb->save.sysenter_esp = data;
  960. break;
  961. default:
  962. return kvm_set_msr_common(vcpu, ecx, data);
  963. }
  964. return 0;
  965. }
  966. static int wrmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  967. {
  968. u32 ecx = svm->vcpu.regs[VCPU_REGS_RCX];
  969. u64 data = (svm->vmcb->save.rax & -1u)
  970. | ((u64)(svm->vcpu.regs[VCPU_REGS_RDX] & -1u) << 32);
  971. svm->next_rip = svm->vmcb->save.rip + 2;
  972. if (svm_set_msr(&svm->vcpu, ecx, data))
  973. svm_inject_gp(&svm->vcpu, 0);
  974. else
  975. skip_emulated_instruction(&svm->vcpu);
  976. return 1;
  977. }
  978. static int msr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  979. {
  980. if (svm->vmcb->control.exit_info_1)
  981. return wrmsr_interception(svm, kvm_run);
  982. else
  983. return rdmsr_interception(svm, kvm_run);
  984. }
  985. static int interrupt_window_interception(struct vcpu_svm *svm,
  986. struct kvm_run *kvm_run)
  987. {
  988. svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
  989. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  990. /*
  991. * If the user space waits to inject interrupts, exit as soon as
  992. * possible
  993. */
  994. if (kvm_run->request_interrupt_window &&
  995. !svm->vcpu.irq_summary) {
  996. ++svm->vcpu.stat.irq_window_exits;
  997. kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  998. return 0;
  999. }
  1000. return 1;
  1001. }
  1002. static int (*svm_exit_handlers[])(struct vcpu_svm *svm,
  1003. struct kvm_run *kvm_run) = {
  1004. [SVM_EXIT_READ_CR0] = emulate_on_interception,
  1005. [SVM_EXIT_READ_CR3] = emulate_on_interception,
  1006. [SVM_EXIT_READ_CR4] = emulate_on_interception,
  1007. /* for now: */
  1008. [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
  1009. [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
  1010. [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
  1011. [SVM_EXIT_READ_DR0] = emulate_on_interception,
  1012. [SVM_EXIT_READ_DR1] = emulate_on_interception,
  1013. [SVM_EXIT_READ_DR2] = emulate_on_interception,
  1014. [SVM_EXIT_READ_DR3] = emulate_on_interception,
  1015. [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
  1016. [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
  1017. [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
  1018. [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
  1019. [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
  1020. [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
  1021. [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
  1022. [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
  1023. [SVM_EXIT_INTR] = nop_on_interception,
  1024. [SVM_EXIT_NMI] = nop_on_interception,
  1025. [SVM_EXIT_SMI] = nop_on_interception,
  1026. [SVM_EXIT_INIT] = nop_on_interception,
  1027. [SVM_EXIT_VINTR] = interrupt_window_interception,
  1028. /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */
  1029. [SVM_EXIT_CPUID] = cpuid_interception,
  1030. [SVM_EXIT_HLT] = halt_interception,
  1031. [SVM_EXIT_INVLPG] = emulate_on_interception,
  1032. [SVM_EXIT_INVLPGA] = invalid_op_interception,
  1033. [SVM_EXIT_IOIO] = io_interception,
  1034. [SVM_EXIT_MSR] = msr_interception,
  1035. [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
  1036. [SVM_EXIT_SHUTDOWN] = shutdown_interception,
  1037. [SVM_EXIT_VMRUN] = invalid_op_interception,
  1038. [SVM_EXIT_VMMCALL] = vmmcall_interception,
  1039. [SVM_EXIT_VMLOAD] = invalid_op_interception,
  1040. [SVM_EXIT_VMSAVE] = invalid_op_interception,
  1041. [SVM_EXIT_STGI] = invalid_op_interception,
  1042. [SVM_EXIT_CLGI] = invalid_op_interception,
  1043. [SVM_EXIT_SKINIT] = invalid_op_interception,
  1044. [SVM_EXIT_MONITOR] = invalid_op_interception,
  1045. [SVM_EXIT_MWAIT] = invalid_op_interception,
  1046. };
  1047. static int handle_exit(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1048. {
  1049. u32 exit_code = svm->vmcb->control.exit_code;
  1050. if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
  1051. exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR)
  1052. printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
  1053. "exit_code 0x%x\n",
  1054. __FUNCTION__, svm->vmcb->control.exit_int_info,
  1055. exit_code);
  1056. if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
  1057. || svm_exit_handlers[exit_code] == 0) {
  1058. kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
  1059. kvm_run->hw.hardware_exit_reason = exit_code;
  1060. return 0;
  1061. }
  1062. return svm_exit_handlers[exit_code](svm, kvm_run);
  1063. }
  1064. static void reload_tss(struct kvm_vcpu *vcpu)
  1065. {
  1066. int cpu = raw_smp_processor_id();
  1067. struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
  1068. svm_data->tss_desc->type = 9; //available 32/64-bit TSS
  1069. load_TR_desc();
  1070. }
  1071. static void pre_svm_run(struct vcpu_svm *svm)
  1072. {
  1073. int cpu = raw_smp_processor_id();
  1074. struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
  1075. svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
  1076. if (svm->vcpu.cpu != cpu ||
  1077. svm->asid_generation != svm_data->asid_generation)
  1078. new_asid(svm, svm_data);
  1079. }
  1080. static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
  1081. {
  1082. struct vmcb_control_area *control;
  1083. control = &svm->vmcb->control;
  1084. control->int_vector = irq;
  1085. control->int_ctl &= ~V_INTR_PRIO_MASK;
  1086. control->int_ctl |= V_IRQ_MASK |
  1087. ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
  1088. }
  1089. static void svm_intr_assist(struct vcpu_svm *svm)
  1090. {
  1091. struct vmcb *vmcb = svm->vmcb;
  1092. int intr_vector = -1;
  1093. if ((vmcb->control.exit_int_info & SVM_EVTINJ_VALID) &&
  1094. ((vmcb->control.exit_int_info & SVM_EVTINJ_TYPE_MASK) == 0)) {
  1095. intr_vector = vmcb->control.exit_int_info &
  1096. SVM_EVTINJ_VEC_MASK;
  1097. vmcb->control.exit_int_info = 0;
  1098. svm_inject_irq(svm, intr_vector);
  1099. return;
  1100. }
  1101. if (vmcb->control.int_ctl & V_IRQ_MASK)
  1102. return;
  1103. if (!kvm_cpu_has_interrupt(&svm->vcpu))
  1104. return;
  1105. if (!(vmcb->save.rflags & X86_EFLAGS_IF) ||
  1106. (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) ||
  1107. (vmcb->control.event_inj & SVM_EVTINJ_VALID)) {
  1108. /* unable to deliver irq, set pending irq */
  1109. vmcb->control.intercept |= (1ULL << INTERCEPT_VINTR);
  1110. svm_inject_irq(svm, 0x0);
  1111. return;
  1112. }
  1113. /* Okay, we can deliver the interrupt: grab it and update PIC state. */
  1114. intr_vector = kvm_cpu_get_interrupt(&svm->vcpu);
  1115. svm_inject_irq(svm, intr_vector);
  1116. }
  1117. static void kvm_reput_irq(struct vcpu_svm *svm)
  1118. {
  1119. struct vmcb_control_area *control = &svm->vmcb->control;
  1120. if ((control->int_ctl & V_IRQ_MASK)
  1121. && !irqchip_in_kernel(svm->vcpu.kvm)) {
  1122. control->int_ctl &= ~V_IRQ_MASK;
  1123. push_irq(&svm->vcpu, control->int_vector);
  1124. }
  1125. svm->vcpu.interrupt_window_open =
  1126. !(control->int_state & SVM_INTERRUPT_SHADOW_MASK);
  1127. }
  1128. static void svm_do_inject_vector(struct vcpu_svm *svm)
  1129. {
  1130. struct kvm_vcpu *vcpu = &svm->vcpu;
  1131. int word_index = __ffs(vcpu->irq_summary);
  1132. int bit_index = __ffs(vcpu->irq_pending[word_index]);
  1133. int irq = word_index * BITS_PER_LONG + bit_index;
  1134. clear_bit(bit_index, &vcpu->irq_pending[word_index]);
  1135. if (!vcpu->irq_pending[word_index])
  1136. clear_bit(word_index, &vcpu->irq_summary);
  1137. svm_inject_irq(svm, irq);
  1138. }
  1139. static void do_interrupt_requests(struct vcpu_svm *svm,
  1140. struct kvm_run *kvm_run)
  1141. {
  1142. struct vmcb_control_area *control = &svm->vmcb->control;
  1143. svm->vcpu.interrupt_window_open =
  1144. (!(control->int_state & SVM_INTERRUPT_SHADOW_MASK) &&
  1145. (svm->vmcb->save.rflags & X86_EFLAGS_IF));
  1146. if (svm->vcpu.interrupt_window_open && svm->vcpu.irq_summary)
  1147. /*
  1148. * If interrupts enabled, and not blocked by sti or mov ss. Good.
  1149. */
  1150. svm_do_inject_vector(svm);
  1151. /*
  1152. * Interrupts blocked. Wait for unblock.
  1153. */
  1154. if (!svm->vcpu.interrupt_window_open &&
  1155. (svm->vcpu.irq_summary || kvm_run->request_interrupt_window)) {
  1156. control->intercept |= 1ULL << INTERCEPT_VINTR;
  1157. } else
  1158. control->intercept &= ~(1ULL << INTERCEPT_VINTR);
  1159. }
  1160. static void post_kvm_run_save(struct vcpu_svm *svm,
  1161. struct kvm_run *kvm_run)
  1162. {
  1163. kvm_run->ready_for_interrupt_injection
  1164. = (svm->vcpu.interrupt_window_open &&
  1165. svm->vcpu.irq_summary == 0);
  1166. kvm_run->if_flag = (svm->vmcb->save.rflags & X86_EFLAGS_IF) != 0;
  1167. kvm_run->cr8 = get_cr8(&svm->vcpu);
  1168. kvm_run->apic_base = kvm_get_apic_base(&svm->vcpu);
  1169. }
  1170. /*
  1171. * Check if userspace requested an interrupt window, and that the
  1172. * interrupt window is open.
  1173. *
  1174. * No need to exit to userspace if we already have an interrupt queued.
  1175. */
  1176. static int dm_request_for_irq_injection(struct vcpu_svm *svm,
  1177. struct kvm_run *kvm_run)
  1178. {
  1179. return (!svm->vcpu.irq_summary &&
  1180. kvm_run->request_interrupt_window &&
  1181. svm->vcpu.interrupt_window_open &&
  1182. (svm->vmcb->save.rflags & X86_EFLAGS_IF));
  1183. }
  1184. static void save_db_regs(unsigned long *db_regs)
  1185. {
  1186. asm volatile ("mov %%dr0, %0" : "=r"(db_regs[0]));
  1187. asm volatile ("mov %%dr1, %0" : "=r"(db_regs[1]));
  1188. asm volatile ("mov %%dr2, %0" : "=r"(db_regs[2]));
  1189. asm volatile ("mov %%dr3, %0" : "=r"(db_regs[3]));
  1190. }
  1191. static void load_db_regs(unsigned long *db_regs)
  1192. {
  1193. asm volatile ("mov %0, %%dr0" : : "r"(db_regs[0]));
  1194. asm volatile ("mov %0, %%dr1" : : "r"(db_regs[1]));
  1195. asm volatile ("mov %0, %%dr2" : : "r"(db_regs[2]));
  1196. asm volatile ("mov %0, %%dr3" : : "r"(db_regs[3]));
  1197. }
  1198. static void svm_flush_tlb(struct kvm_vcpu *vcpu)
  1199. {
  1200. force_new_asid(vcpu);
  1201. }
  1202. static int svm_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  1203. {
  1204. struct vcpu_svm *svm = to_svm(vcpu);
  1205. u16 fs_selector;
  1206. u16 gs_selector;
  1207. u16 ldt_selector;
  1208. int r;
  1209. again:
  1210. r = kvm_mmu_reload(vcpu);
  1211. if (unlikely(r))
  1212. return r;
  1213. clgi();
  1214. if (signal_pending(current)) {
  1215. stgi();
  1216. ++vcpu->stat.signal_exits;
  1217. post_kvm_run_save(svm, kvm_run);
  1218. kvm_run->exit_reason = KVM_EXIT_INTR;
  1219. return -EINTR;
  1220. }
  1221. if (irqchip_in_kernel(vcpu->kvm))
  1222. svm_intr_assist(svm);
  1223. else if (!vcpu->mmio_read_completed)
  1224. do_interrupt_requests(svm, kvm_run);
  1225. vcpu->guest_mode = 1;
  1226. if (vcpu->requests)
  1227. if (test_and_clear_bit(KVM_TLB_FLUSH, &vcpu->requests))
  1228. svm_flush_tlb(vcpu);
  1229. pre_svm_run(svm);
  1230. save_host_msrs(vcpu);
  1231. fs_selector = read_fs();
  1232. gs_selector = read_gs();
  1233. ldt_selector = read_ldt();
  1234. svm->host_cr2 = kvm_read_cr2();
  1235. svm->host_dr6 = read_dr6();
  1236. svm->host_dr7 = read_dr7();
  1237. svm->vmcb->save.cr2 = vcpu->cr2;
  1238. if (svm->vmcb->save.dr7 & 0xff) {
  1239. write_dr7(0);
  1240. save_db_regs(svm->host_db_regs);
  1241. load_db_regs(svm->db_regs);
  1242. }
  1243. if (vcpu->fpu_active) {
  1244. fx_save(&vcpu->host_fx_image);
  1245. fx_restore(&vcpu->guest_fx_image);
  1246. }
  1247. asm volatile (
  1248. #ifdef CONFIG_X86_64
  1249. "push %%rbx; push %%rcx; push %%rdx;"
  1250. "push %%rsi; push %%rdi; push %%rbp;"
  1251. "push %%r8; push %%r9; push %%r10; push %%r11;"
  1252. "push %%r12; push %%r13; push %%r14; push %%r15;"
  1253. #else
  1254. "push %%ebx; push %%ecx; push %%edx;"
  1255. "push %%esi; push %%edi; push %%ebp;"
  1256. #endif
  1257. #ifdef CONFIG_X86_64
  1258. "mov %c[rbx](%[svm]), %%rbx \n\t"
  1259. "mov %c[rcx](%[svm]), %%rcx \n\t"
  1260. "mov %c[rdx](%[svm]), %%rdx \n\t"
  1261. "mov %c[rsi](%[svm]), %%rsi \n\t"
  1262. "mov %c[rdi](%[svm]), %%rdi \n\t"
  1263. "mov %c[rbp](%[svm]), %%rbp \n\t"
  1264. "mov %c[r8](%[svm]), %%r8 \n\t"
  1265. "mov %c[r9](%[svm]), %%r9 \n\t"
  1266. "mov %c[r10](%[svm]), %%r10 \n\t"
  1267. "mov %c[r11](%[svm]), %%r11 \n\t"
  1268. "mov %c[r12](%[svm]), %%r12 \n\t"
  1269. "mov %c[r13](%[svm]), %%r13 \n\t"
  1270. "mov %c[r14](%[svm]), %%r14 \n\t"
  1271. "mov %c[r15](%[svm]), %%r15 \n\t"
  1272. #else
  1273. "mov %c[rbx](%[svm]), %%ebx \n\t"
  1274. "mov %c[rcx](%[svm]), %%ecx \n\t"
  1275. "mov %c[rdx](%[svm]), %%edx \n\t"
  1276. "mov %c[rsi](%[svm]), %%esi \n\t"
  1277. "mov %c[rdi](%[svm]), %%edi \n\t"
  1278. "mov %c[rbp](%[svm]), %%ebp \n\t"
  1279. #endif
  1280. #ifdef CONFIG_X86_64
  1281. /* Enter guest mode */
  1282. "push %%rax \n\t"
  1283. "mov %c[vmcb](%[svm]), %%rax \n\t"
  1284. SVM_VMLOAD "\n\t"
  1285. SVM_VMRUN "\n\t"
  1286. SVM_VMSAVE "\n\t"
  1287. "pop %%rax \n\t"
  1288. #else
  1289. /* Enter guest mode */
  1290. "push %%eax \n\t"
  1291. "mov %c[vmcb](%[svm]), %%eax \n\t"
  1292. SVM_VMLOAD "\n\t"
  1293. SVM_VMRUN "\n\t"
  1294. SVM_VMSAVE "\n\t"
  1295. "pop %%eax \n\t"
  1296. #endif
  1297. /* Save guest registers, load host registers */
  1298. #ifdef CONFIG_X86_64
  1299. "mov %%rbx, %c[rbx](%[svm]) \n\t"
  1300. "mov %%rcx, %c[rcx](%[svm]) \n\t"
  1301. "mov %%rdx, %c[rdx](%[svm]) \n\t"
  1302. "mov %%rsi, %c[rsi](%[svm]) \n\t"
  1303. "mov %%rdi, %c[rdi](%[svm]) \n\t"
  1304. "mov %%rbp, %c[rbp](%[svm]) \n\t"
  1305. "mov %%r8, %c[r8](%[svm]) \n\t"
  1306. "mov %%r9, %c[r9](%[svm]) \n\t"
  1307. "mov %%r10, %c[r10](%[svm]) \n\t"
  1308. "mov %%r11, %c[r11](%[svm]) \n\t"
  1309. "mov %%r12, %c[r12](%[svm]) \n\t"
  1310. "mov %%r13, %c[r13](%[svm]) \n\t"
  1311. "mov %%r14, %c[r14](%[svm]) \n\t"
  1312. "mov %%r15, %c[r15](%[svm]) \n\t"
  1313. "pop %%r15; pop %%r14; pop %%r13; pop %%r12;"
  1314. "pop %%r11; pop %%r10; pop %%r9; pop %%r8;"
  1315. "pop %%rbp; pop %%rdi; pop %%rsi;"
  1316. "pop %%rdx; pop %%rcx; pop %%rbx; \n\t"
  1317. #else
  1318. "mov %%ebx, %c[rbx](%[svm]) \n\t"
  1319. "mov %%ecx, %c[rcx](%[svm]) \n\t"
  1320. "mov %%edx, %c[rdx](%[svm]) \n\t"
  1321. "mov %%esi, %c[rsi](%[svm]) \n\t"
  1322. "mov %%edi, %c[rdi](%[svm]) \n\t"
  1323. "mov %%ebp, %c[rbp](%[svm]) \n\t"
  1324. "pop %%ebp; pop %%edi; pop %%esi;"
  1325. "pop %%edx; pop %%ecx; pop %%ebx; \n\t"
  1326. #endif
  1327. :
  1328. : [svm]"a"(svm),
  1329. [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
  1330. [rbx]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_RBX])),
  1331. [rcx]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_RCX])),
  1332. [rdx]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_RDX])),
  1333. [rsi]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_RSI])),
  1334. [rdi]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_RDI])),
  1335. [rbp]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_RBP]))
  1336. #ifdef CONFIG_X86_64
  1337. ,[r8 ]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_R8])),
  1338. [r9 ]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_R9 ])),
  1339. [r10]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_R10])),
  1340. [r11]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_R11])),
  1341. [r12]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_R12])),
  1342. [r13]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_R13])),
  1343. [r14]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_R14])),
  1344. [r15]"i"(offsetof(struct vcpu_svm,vcpu.regs[VCPU_REGS_R15]))
  1345. #endif
  1346. : "cc", "memory" );
  1347. vcpu->guest_mode = 0;
  1348. if (vcpu->fpu_active) {
  1349. fx_save(&vcpu->guest_fx_image);
  1350. fx_restore(&vcpu->host_fx_image);
  1351. }
  1352. if ((svm->vmcb->save.dr7 & 0xff))
  1353. load_db_regs(svm->host_db_regs);
  1354. vcpu->cr2 = svm->vmcb->save.cr2;
  1355. write_dr6(svm->host_dr6);
  1356. write_dr7(svm->host_dr7);
  1357. kvm_write_cr2(svm->host_cr2);
  1358. load_fs(fs_selector);
  1359. load_gs(gs_selector);
  1360. load_ldt(ldt_selector);
  1361. load_host_msrs(vcpu);
  1362. reload_tss(vcpu);
  1363. /*
  1364. * Profile KVM exit RIPs:
  1365. */
  1366. if (unlikely(prof_on == KVM_PROFILING))
  1367. profile_hit(KVM_PROFILING,
  1368. (void *)(unsigned long)svm->vmcb->save.rip);
  1369. stgi();
  1370. kvm_reput_irq(svm);
  1371. svm->next_rip = 0;
  1372. if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
  1373. kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  1374. kvm_run->fail_entry.hardware_entry_failure_reason
  1375. = svm->vmcb->control.exit_code;
  1376. post_kvm_run_save(svm, kvm_run);
  1377. return 0;
  1378. }
  1379. r = handle_exit(svm, kvm_run);
  1380. if (r > 0) {
  1381. if (dm_request_for_irq_injection(svm, kvm_run)) {
  1382. ++vcpu->stat.request_irq_exits;
  1383. post_kvm_run_save(svm, kvm_run);
  1384. kvm_run->exit_reason = KVM_EXIT_INTR;
  1385. return -EINTR;
  1386. }
  1387. kvm_resched(vcpu);
  1388. goto again;
  1389. }
  1390. post_kvm_run_save(svm, kvm_run);
  1391. return r;
  1392. }
  1393. static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  1394. {
  1395. struct vcpu_svm *svm = to_svm(vcpu);
  1396. svm->vmcb->save.cr3 = root;
  1397. force_new_asid(vcpu);
  1398. if (vcpu->fpu_active) {
  1399. svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
  1400. svm->vmcb->save.cr0 |= X86_CR0_TS;
  1401. vcpu->fpu_active = 0;
  1402. }
  1403. }
  1404. static void svm_inject_page_fault(struct kvm_vcpu *vcpu,
  1405. unsigned long addr,
  1406. uint32_t err_code)
  1407. {
  1408. struct vcpu_svm *svm = to_svm(vcpu);
  1409. uint32_t exit_int_info = svm->vmcb->control.exit_int_info;
  1410. ++vcpu->stat.pf_guest;
  1411. if (is_page_fault(exit_int_info)) {
  1412. svm->vmcb->control.event_inj_err = 0;
  1413. svm->vmcb->control.event_inj = SVM_EVTINJ_VALID |
  1414. SVM_EVTINJ_VALID_ERR |
  1415. SVM_EVTINJ_TYPE_EXEPT |
  1416. DF_VECTOR;
  1417. return;
  1418. }
  1419. vcpu->cr2 = addr;
  1420. svm->vmcb->save.cr2 = addr;
  1421. svm->vmcb->control.event_inj = SVM_EVTINJ_VALID |
  1422. SVM_EVTINJ_VALID_ERR |
  1423. SVM_EVTINJ_TYPE_EXEPT |
  1424. PF_VECTOR;
  1425. svm->vmcb->control.event_inj_err = err_code;
  1426. }
  1427. static int is_disabled(void)
  1428. {
  1429. u64 vm_cr;
  1430. rdmsrl(MSR_VM_CR, vm_cr);
  1431. if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
  1432. return 1;
  1433. return 0;
  1434. }
  1435. static void
  1436. svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  1437. {
  1438. /*
  1439. * Patch in the VMMCALL instruction:
  1440. */
  1441. hypercall[0] = 0x0f;
  1442. hypercall[1] = 0x01;
  1443. hypercall[2] = 0xd9;
  1444. hypercall[3] = 0xc3;
  1445. }
  1446. static void svm_check_processor_compat(void *rtn)
  1447. {
  1448. *(int *)rtn = 0;
  1449. }
  1450. static struct kvm_arch_ops svm_arch_ops = {
  1451. .cpu_has_kvm_support = has_svm,
  1452. .disabled_by_bios = is_disabled,
  1453. .hardware_setup = svm_hardware_setup,
  1454. .hardware_unsetup = svm_hardware_unsetup,
  1455. .check_processor_compatibility = svm_check_processor_compat,
  1456. .hardware_enable = svm_hardware_enable,
  1457. .hardware_disable = svm_hardware_disable,
  1458. .vcpu_create = svm_create_vcpu,
  1459. .vcpu_free = svm_free_vcpu,
  1460. .vcpu_load = svm_vcpu_load,
  1461. .vcpu_put = svm_vcpu_put,
  1462. .vcpu_decache = svm_vcpu_decache,
  1463. .set_guest_debug = svm_guest_debug,
  1464. .get_msr = svm_get_msr,
  1465. .set_msr = svm_set_msr,
  1466. .get_segment_base = svm_get_segment_base,
  1467. .get_segment = svm_get_segment,
  1468. .set_segment = svm_set_segment,
  1469. .get_cs_db_l_bits = svm_get_cs_db_l_bits,
  1470. .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
  1471. .set_cr0 = svm_set_cr0,
  1472. .set_cr3 = svm_set_cr3,
  1473. .set_cr4 = svm_set_cr4,
  1474. .set_efer = svm_set_efer,
  1475. .get_idt = svm_get_idt,
  1476. .set_idt = svm_set_idt,
  1477. .get_gdt = svm_get_gdt,
  1478. .set_gdt = svm_set_gdt,
  1479. .get_dr = svm_get_dr,
  1480. .set_dr = svm_set_dr,
  1481. .cache_regs = svm_cache_regs,
  1482. .decache_regs = svm_decache_regs,
  1483. .get_rflags = svm_get_rflags,
  1484. .set_rflags = svm_set_rflags,
  1485. .invlpg = svm_invlpg,
  1486. .tlb_flush = svm_flush_tlb,
  1487. .inject_page_fault = svm_inject_page_fault,
  1488. .inject_gp = svm_inject_gp,
  1489. .run = svm_vcpu_run,
  1490. .skip_emulated_instruction = skip_emulated_instruction,
  1491. .patch_hypercall = svm_patch_hypercall,
  1492. };
  1493. static int __init svm_init(void)
  1494. {
  1495. return kvm_init_arch(&svm_arch_ops, sizeof(struct vcpu_svm),
  1496. THIS_MODULE);
  1497. }
  1498. static void __exit svm_exit(void)
  1499. {
  1500. kvm_exit_arch();
  1501. }
  1502. module_init(svm_init)
  1503. module_exit(svm_exit)