svm.c 48 KB

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