svm.c 50 KB

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