svm.c 44 KB

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