svm.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999
  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. 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. unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
  708. if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
  709. force_new_asid(vcpu);
  710. vcpu->arch.cr4 = cr4;
  711. if (!npt_enabled)
  712. cr4 |= X86_CR4_PAE;
  713. cr4 |= host_cr4_mce;
  714. to_svm(vcpu)->vmcb->save.cr4 = cr4;
  715. }
  716. static void svm_set_segment(struct kvm_vcpu *vcpu,
  717. struct kvm_segment *var, int seg)
  718. {
  719. struct vcpu_svm *svm = to_svm(vcpu);
  720. struct vmcb_seg *s = svm_seg(vcpu, seg);
  721. s->base = var->base;
  722. s->limit = var->limit;
  723. s->selector = var->selector;
  724. if (var->unusable)
  725. s->attrib = 0;
  726. else {
  727. s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
  728. s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
  729. s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
  730. s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
  731. s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
  732. s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
  733. s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
  734. s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
  735. }
  736. if (seg == VCPU_SREG_CS)
  737. svm->vmcb->save.cpl
  738. = (svm->vmcb->save.cs.attrib
  739. >> SVM_SELECTOR_DPL_SHIFT) & 3;
  740. }
  741. static int svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
  742. {
  743. return -EOPNOTSUPP;
  744. }
  745. static int svm_get_irq(struct kvm_vcpu *vcpu)
  746. {
  747. struct vcpu_svm *svm = to_svm(vcpu);
  748. u32 exit_int_info = svm->vmcb->control.exit_int_info;
  749. if (is_external_interrupt(exit_int_info))
  750. return exit_int_info & SVM_EVTINJ_VEC_MASK;
  751. return -1;
  752. }
  753. static void load_host_msrs(struct kvm_vcpu *vcpu)
  754. {
  755. #ifdef CONFIG_X86_64
  756. wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
  757. #endif
  758. }
  759. static void save_host_msrs(struct kvm_vcpu *vcpu)
  760. {
  761. #ifdef CONFIG_X86_64
  762. rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
  763. #endif
  764. }
  765. static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *svm_data)
  766. {
  767. if (svm_data->next_asid > svm_data->max_asid) {
  768. ++svm_data->asid_generation;
  769. svm_data->next_asid = 1;
  770. svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
  771. }
  772. svm->vcpu.cpu = svm_data->cpu;
  773. svm->asid_generation = svm_data->asid_generation;
  774. svm->vmcb->control.asid = svm_data->next_asid++;
  775. }
  776. static unsigned long svm_get_dr(struct kvm_vcpu *vcpu, int dr)
  777. {
  778. unsigned long val = to_svm(vcpu)->db_regs[dr];
  779. KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler);
  780. return val;
  781. }
  782. static void svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value,
  783. int *exception)
  784. {
  785. struct vcpu_svm *svm = to_svm(vcpu);
  786. *exception = 0;
  787. if (svm->vmcb->save.dr7 & DR7_GD_MASK) {
  788. svm->vmcb->save.dr7 &= ~DR7_GD_MASK;
  789. svm->vmcb->save.dr6 |= DR6_BD_MASK;
  790. *exception = DB_VECTOR;
  791. return;
  792. }
  793. switch (dr) {
  794. case 0 ... 3:
  795. svm->db_regs[dr] = value;
  796. return;
  797. case 4 ... 5:
  798. if (vcpu->arch.cr4 & X86_CR4_DE) {
  799. *exception = UD_VECTOR;
  800. return;
  801. }
  802. case 7: {
  803. if (value & ~((1ULL << 32) - 1)) {
  804. *exception = GP_VECTOR;
  805. return;
  806. }
  807. svm->vmcb->save.dr7 = value;
  808. return;
  809. }
  810. default:
  811. printk(KERN_DEBUG "%s: unexpected dr %u\n",
  812. __func__, dr);
  813. *exception = UD_VECTOR;
  814. return;
  815. }
  816. }
  817. static int pf_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  818. {
  819. u32 exit_int_info = svm->vmcb->control.exit_int_info;
  820. struct kvm *kvm = svm->vcpu.kvm;
  821. u64 fault_address;
  822. u32 error_code;
  823. bool event_injection = false;
  824. if (!irqchip_in_kernel(kvm) &&
  825. is_external_interrupt(exit_int_info)) {
  826. event_injection = true;
  827. push_irq(&svm->vcpu, exit_int_info & SVM_EVTINJ_VEC_MASK);
  828. }
  829. fault_address = svm->vmcb->control.exit_info_2;
  830. error_code = svm->vmcb->control.exit_info_1;
  831. if (!npt_enabled)
  832. KVMTRACE_3D(PAGE_FAULT, &svm->vcpu, error_code,
  833. (u32)fault_address, (u32)(fault_address >> 32),
  834. handler);
  835. else
  836. KVMTRACE_3D(TDP_FAULT, &svm->vcpu, error_code,
  837. (u32)fault_address, (u32)(fault_address >> 32),
  838. handler);
  839. /*
  840. * FIXME: Tis shouldn't be necessary here, but there is a flush
  841. * missing in the MMU code. Until we find this bug, flush the
  842. * complete TLB here on an NPF
  843. */
  844. if (npt_enabled)
  845. svm_flush_tlb(&svm->vcpu);
  846. if (!npt_enabled && event_injection)
  847. kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
  848. return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
  849. }
  850. static int ud_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  851. {
  852. int er;
  853. er = emulate_instruction(&svm->vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
  854. if (er != EMULATE_DONE)
  855. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  856. return 1;
  857. }
  858. static int nm_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  859. {
  860. svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
  861. if (!(svm->vcpu.arch.cr0 & X86_CR0_TS))
  862. svm->vmcb->save.cr0 &= ~X86_CR0_TS;
  863. svm->vcpu.fpu_active = 1;
  864. return 1;
  865. }
  866. static int mc_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  867. {
  868. /*
  869. * On an #MC intercept the MCE handler is not called automatically in
  870. * the host. So do it by hand here.
  871. */
  872. asm volatile (
  873. "int $0x12\n");
  874. /* not sure if we ever come back to this point */
  875. return 1;
  876. }
  877. static int shutdown_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  878. {
  879. /*
  880. * VMCB is undefined after a SHUTDOWN intercept
  881. * so reinitialize it.
  882. */
  883. clear_page(svm->vmcb);
  884. init_vmcb(svm);
  885. kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
  886. return 0;
  887. }
  888. static int io_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  889. {
  890. u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
  891. int size, down, in, string, rep;
  892. unsigned port;
  893. ++svm->vcpu.stat.io_exits;
  894. svm->next_rip = svm->vmcb->control.exit_info_2;
  895. string = (io_info & SVM_IOIO_STR_MASK) != 0;
  896. if (string) {
  897. if (emulate_instruction(&svm->vcpu,
  898. kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
  899. return 0;
  900. return 1;
  901. }
  902. in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
  903. port = io_info >> 16;
  904. size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
  905. rep = (io_info & SVM_IOIO_REP_MASK) != 0;
  906. down = (svm->vmcb->save.rflags & X86_EFLAGS_DF) != 0;
  907. return kvm_emulate_pio(&svm->vcpu, kvm_run, in, size, port);
  908. }
  909. static int nmi_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  910. {
  911. KVMTRACE_0D(NMI, &svm->vcpu, handler);
  912. return 1;
  913. }
  914. static int intr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  915. {
  916. ++svm->vcpu.stat.irq_exits;
  917. KVMTRACE_0D(INTR, &svm->vcpu, handler);
  918. return 1;
  919. }
  920. static int nop_on_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  921. {
  922. return 1;
  923. }
  924. static int halt_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  925. {
  926. svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
  927. skip_emulated_instruction(&svm->vcpu);
  928. return kvm_emulate_halt(&svm->vcpu);
  929. }
  930. static int vmmcall_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  931. {
  932. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  933. skip_emulated_instruction(&svm->vcpu);
  934. kvm_emulate_hypercall(&svm->vcpu);
  935. return 1;
  936. }
  937. static int invalid_op_interception(struct vcpu_svm *svm,
  938. struct kvm_run *kvm_run)
  939. {
  940. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  941. return 1;
  942. }
  943. static int task_switch_interception(struct vcpu_svm *svm,
  944. struct kvm_run *kvm_run)
  945. {
  946. u16 tss_selector;
  947. tss_selector = (u16)svm->vmcb->control.exit_info_1;
  948. if (svm->vmcb->control.exit_info_2 &
  949. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
  950. return kvm_task_switch(&svm->vcpu, tss_selector,
  951. TASK_SWITCH_IRET);
  952. if (svm->vmcb->control.exit_info_2 &
  953. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
  954. return kvm_task_switch(&svm->vcpu, tss_selector,
  955. TASK_SWITCH_JMP);
  956. return kvm_task_switch(&svm->vcpu, tss_selector, TASK_SWITCH_CALL);
  957. }
  958. static int cpuid_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  959. {
  960. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  961. kvm_emulate_cpuid(&svm->vcpu);
  962. return 1;
  963. }
  964. static int invlpg_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  965. {
  966. if (emulate_instruction(&svm->vcpu, kvm_run, 0, 0, 0) != EMULATE_DONE)
  967. pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
  968. return 1;
  969. }
  970. static int emulate_on_interception(struct vcpu_svm *svm,
  971. struct kvm_run *kvm_run)
  972. {
  973. if (emulate_instruction(&svm->vcpu, NULL, 0, 0, 0) != EMULATE_DONE)
  974. pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
  975. return 1;
  976. }
  977. static int cr8_write_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  978. {
  979. emulate_instruction(&svm->vcpu, NULL, 0, 0, 0);
  980. if (irqchip_in_kernel(svm->vcpu.kvm))
  981. return 1;
  982. kvm_run->exit_reason = KVM_EXIT_SET_TPR;
  983. return 0;
  984. }
  985. static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
  986. {
  987. struct vcpu_svm *svm = to_svm(vcpu);
  988. switch (ecx) {
  989. case MSR_IA32_TIME_STAMP_COUNTER: {
  990. u64 tsc;
  991. rdtscll(tsc);
  992. *data = svm->vmcb->control.tsc_offset + tsc;
  993. break;
  994. }
  995. case MSR_K6_STAR:
  996. *data = svm->vmcb->save.star;
  997. break;
  998. #ifdef CONFIG_X86_64
  999. case MSR_LSTAR:
  1000. *data = svm->vmcb->save.lstar;
  1001. break;
  1002. case MSR_CSTAR:
  1003. *data = svm->vmcb->save.cstar;
  1004. break;
  1005. case MSR_KERNEL_GS_BASE:
  1006. *data = svm->vmcb->save.kernel_gs_base;
  1007. break;
  1008. case MSR_SYSCALL_MASK:
  1009. *data = svm->vmcb->save.sfmask;
  1010. break;
  1011. #endif
  1012. case MSR_IA32_SYSENTER_CS:
  1013. *data = svm->vmcb->save.sysenter_cs;
  1014. break;
  1015. case MSR_IA32_SYSENTER_EIP:
  1016. *data = svm->vmcb->save.sysenter_eip;
  1017. break;
  1018. case MSR_IA32_SYSENTER_ESP:
  1019. *data = svm->vmcb->save.sysenter_esp;
  1020. break;
  1021. /* Nobody will change the following 5 values in the VMCB so
  1022. we can safely return them on rdmsr. They will always be 0
  1023. until LBRV is implemented. */
  1024. case MSR_IA32_DEBUGCTLMSR:
  1025. *data = svm->vmcb->save.dbgctl;
  1026. break;
  1027. case MSR_IA32_LASTBRANCHFROMIP:
  1028. *data = svm->vmcb->save.br_from;
  1029. break;
  1030. case MSR_IA32_LASTBRANCHTOIP:
  1031. *data = svm->vmcb->save.br_to;
  1032. break;
  1033. case MSR_IA32_LASTINTFROMIP:
  1034. *data = svm->vmcb->save.last_excp_from;
  1035. break;
  1036. case MSR_IA32_LASTINTTOIP:
  1037. *data = svm->vmcb->save.last_excp_to;
  1038. break;
  1039. default:
  1040. return kvm_get_msr_common(vcpu, ecx, data);
  1041. }
  1042. return 0;
  1043. }
  1044. static int rdmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1045. {
  1046. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  1047. u64 data;
  1048. if (svm_get_msr(&svm->vcpu, ecx, &data))
  1049. kvm_inject_gp(&svm->vcpu, 0);
  1050. else {
  1051. KVMTRACE_3D(MSR_READ, &svm->vcpu, ecx, (u32)data,
  1052. (u32)(data >> 32), handler);
  1053. svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
  1054. svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
  1055. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  1056. skip_emulated_instruction(&svm->vcpu);
  1057. }
  1058. return 1;
  1059. }
  1060. static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
  1061. {
  1062. struct vcpu_svm *svm = to_svm(vcpu);
  1063. switch (ecx) {
  1064. case MSR_IA32_TIME_STAMP_COUNTER: {
  1065. u64 tsc;
  1066. rdtscll(tsc);
  1067. svm->vmcb->control.tsc_offset = data - tsc;
  1068. break;
  1069. }
  1070. case MSR_K6_STAR:
  1071. svm->vmcb->save.star = data;
  1072. break;
  1073. #ifdef CONFIG_X86_64
  1074. case MSR_LSTAR:
  1075. svm->vmcb->save.lstar = data;
  1076. break;
  1077. case MSR_CSTAR:
  1078. svm->vmcb->save.cstar = data;
  1079. break;
  1080. case MSR_KERNEL_GS_BASE:
  1081. svm->vmcb->save.kernel_gs_base = data;
  1082. break;
  1083. case MSR_SYSCALL_MASK:
  1084. svm->vmcb->save.sfmask = data;
  1085. break;
  1086. #endif
  1087. case MSR_IA32_SYSENTER_CS:
  1088. svm->vmcb->save.sysenter_cs = data;
  1089. break;
  1090. case MSR_IA32_SYSENTER_EIP:
  1091. svm->vmcb->save.sysenter_eip = data;
  1092. break;
  1093. case MSR_IA32_SYSENTER_ESP:
  1094. svm->vmcb->save.sysenter_esp = data;
  1095. break;
  1096. case MSR_IA32_DEBUGCTLMSR:
  1097. if (!svm_has(SVM_FEATURE_LBRV)) {
  1098. pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
  1099. __func__, data);
  1100. break;
  1101. }
  1102. if (data & DEBUGCTL_RESERVED_BITS)
  1103. return 1;
  1104. svm->vmcb->save.dbgctl = data;
  1105. if (data & (1ULL<<0))
  1106. svm_enable_lbrv(svm);
  1107. else
  1108. svm_disable_lbrv(svm);
  1109. break;
  1110. case MSR_K7_EVNTSEL0:
  1111. case MSR_K7_EVNTSEL1:
  1112. case MSR_K7_EVNTSEL2:
  1113. case MSR_K7_EVNTSEL3:
  1114. case MSR_K7_PERFCTR0:
  1115. case MSR_K7_PERFCTR1:
  1116. case MSR_K7_PERFCTR2:
  1117. case MSR_K7_PERFCTR3:
  1118. /*
  1119. * Just discard all writes to the performance counters; this
  1120. * should keep both older linux and windows 64-bit guests
  1121. * happy
  1122. */
  1123. pr_unimpl(vcpu, "unimplemented perfctr wrmsr: 0x%x data 0x%llx\n", ecx, data);
  1124. break;
  1125. default:
  1126. return kvm_set_msr_common(vcpu, ecx, data);
  1127. }
  1128. return 0;
  1129. }
  1130. static int wrmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1131. {
  1132. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  1133. u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
  1134. | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  1135. KVMTRACE_3D(MSR_WRITE, &svm->vcpu, ecx, (u32)data, (u32)(data >> 32),
  1136. handler);
  1137. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  1138. if (svm_set_msr(&svm->vcpu, ecx, data))
  1139. kvm_inject_gp(&svm->vcpu, 0);
  1140. else
  1141. skip_emulated_instruction(&svm->vcpu);
  1142. return 1;
  1143. }
  1144. static int msr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1145. {
  1146. if (svm->vmcb->control.exit_info_1)
  1147. return wrmsr_interception(svm, kvm_run);
  1148. else
  1149. return rdmsr_interception(svm, kvm_run);
  1150. }
  1151. static int interrupt_window_interception(struct vcpu_svm *svm,
  1152. struct kvm_run *kvm_run)
  1153. {
  1154. KVMTRACE_0D(PEND_INTR, &svm->vcpu, handler);
  1155. svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
  1156. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  1157. /*
  1158. * If the user space waits to inject interrupts, exit as soon as
  1159. * possible
  1160. */
  1161. if (kvm_run->request_interrupt_window &&
  1162. !svm->vcpu.arch.irq_summary) {
  1163. ++svm->vcpu.stat.irq_window_exits;
  1164. kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  1165. return 0;
  1166. }
  1167. return 1;
  1168. }
  1169. static int (*svm_exit_handlers[])(struct vcpu_svm *svm,
  1170. struct kvm_run *kvm_run) = {
  1171. [SVM_EXIT_READ_CR0] = emulate_on_interception,
  1172. [SVM_EXIT_READ_CR3] = emulate_on_interception,
  1173. [SVM_EXIT_READ_CR4] = emulate_on_interception,
  1174. [SVM_EXIT_READ_CR8] = emulate_on_interception,
  1175. /* for now: */
  1176. [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
  1177. [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
  1178. [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
  1179. [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
  1180. [SVM_EXIT_READ_DR0] = emulate_on_interception,
  1181. [SVM_EXIT_READ_DR1] = emulate_on_interception,
  1182. [SVM_EXIT_READ_DR2] = emulate_on_interception,
  1183. [SVM_EXIT_READ_DR3] = emulate_on_interception,
  1184. [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
  1185. [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
  1186. [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
  1187. [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
  1188. [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
  1189. [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
  1190. [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
  1191. [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
  1192. [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
  1193. [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
  1194. [SVM_EXIT_INTR] = intr_interception,
  1195. [SVM_EXIT_NMI] = nmi_interception,
  1196. [SVM_EXIT_SMI] = nop_on_interception,
  1197. [SVM_EXIT_INIT] = nop_on_interception,
  1198. [SVM_EXIT_VINTR] = interrupt_window_interception,
  1199. /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */
  1200. [SVM_EXIT_CPUID] = cpuid_interception,
  1201. [SVM_EXIT_INVD] = emulate_on_interception,
  1202. [SVM_EXIT_HLT] = halt_interception,
  1203. [SVM_EXIT_INVLPG] = invlpg_interception,
  1204. [SVM_EXIT_INVLPGA] = invalid_op_interception,
  1205. [SVM_EXIT_IOIO] = io_interception,
  1206. [SVM_EXIT_MSR] = msr_interception,
  1207. [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
  1208. [SVM_EXIT_SHUTDOWN] = shutdown_interception,
  1209. [SVM_EXIT_VMRUN] = invalid_op_interception,
  1210. [SVM_EXIT_VMMCALL] = vmmcall_interception,
  1211. [SVM_EXIT_VMLOAD] = invalid_op_interception,
  1212. [SVM_EXIT_VMSAVE] = invalid_op_interception,
  1213. [SVM_EXIT_STGI] = invalid_op_interception,
  1214. [SVM_EXIT_CLGI] = invalid_op_interception,
  1215. [SVM_EXIT_SKINIT] = invalid_op_interception,
  1216. [SVM_EXIT_WBINVD] = emulate_on_interception,
  1217. [SVM_EXIT_MONITOR] = invalid_op_interception,
  1218. [SVM_EXIT_MWAIT] = invalid_op_interception,
  1219. [SVM_EXIT_NPF] = pf_interception,
  1220. };
  1221. static int handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  1222. {
  1223. struct vcpu_svm *svm = to_svm(vcpu);
  1224. u32 exit_code = svm->vmcb->control.exit_code;
  1225. KVMTRACE_3D(VMEXIT, vcpu, exit_code, (u32)svm->vmcb->save.rip,
  1226. (u32)((u64)svm->vmcb->save.rip >> 32), entryexit);
  1227. if (npt_enabled) {
  1228. int mmu_reload = 0;
  1229. if ((vcpu->arch.cr0 ^ svm->vmcb->save.cr0) & X86_CR0_PG) {
  1230. svm_set_cr0(vcpu, svm->vmcb->save.cr0);
  1231. mmu_reload = 1;
  1232. }
  1233. vcpu->arch.cr0 = svm->vmcb->save.cr0;
  1234. vcpu->arch.cr3 = svm->vmcb->save.cr3;
  1235. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  1236. if (!load_pdptrs(vcpu, vcpu->arch.cr3)) {
  1237. kvm_inject_gp(vcpu, 0);
  1238. return 1;
  1239. }
  1240. }
  1241. if (mmu_reload) {
  1242. kvm_mmu_reset_context(vcpu);
  1243. kvm_mmu_load(vcpu);
  1244. }
  1245. }
  1246. kvm_reput_irq(svm);
  1247. if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
  1248. kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  1249. kvm_run->fail_entry.hardware_entry_failure_reason
  1250. = svm->vmcb->control.exit_code;
  1251. return 0;
  1252. }
  1253. if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
  1254. exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
  1255. exit_code != SVM_EXIT_NPF)
  1256. printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
  1257. "exit_code 0x%x\n",
  1258. __func__, svm->vmcb->control.exit_int_info,
  1259. exit_code);
  1260. if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
  1261. || !svm_exit_handlers[exit_code]) {
  1262. kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
  1263. kvm_run->hw.hardware_exit_reason = exit_code;
  1264. return 0;
  1265. }
  1266. return svm_exit_handlers[exit_code](svm, kvm_run);
  1267. }
  1268. static void reload_tss(struct kvm_vcpu *vcpu)
  1269. {
  1270. int cpu = raw_smp_processor_id();
  1271. struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
  1272. svm_data->tss_desc->type = 9; /* available 32/64-bit TSS */
  1273. load_TR_desc();
  1274. }
  1275. static void pre_svm_run(struct vcpu_svm *svm)
  1276. {
  1277. int cpu = raw_smp_processor_id();
  1278. struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
  1279. svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
  1280. if (svm->vcpu.cpu != cpu ||
  1281. svm->asid_generation != svm_data->asid_generation)
  1282. new_asid(svm, svm_data);
  1283. }
  1284. static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
  1285. {
  1286. struct vmcb_control_area *control;
  1287. KVMTRACE_1D(INJ_VIRQ, &svm->vcpu, (u32)irq, handler);
  1288. ++svm->vcpu.stat.irq_injections;
  1289. control = &svm->vmcb->control;
  1290. control->int_vector = irq;
  1291. control->int_ctl &= ~V_INTR_PRIO_MASK;
  1292. control->int_ctl |= V_IRQ_MASK |
  1293. ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
  1294. }
  1295. static void svm_set_irq(struct kvm_vcpu *vcpu, int irq)
  1296. {
  1297. struct vcpu_svm *svm = to_svm(vcpu);
  1298. svm_inject_irq(svm, irq);
  1299. }
  1300. static void update_cr8_intercept(struct kvm_vcpu *vcpu)
  1301. {
  1302. struct vcpu_svm *svm = to_svm(vcpu);
  1303. struct vmcb *vmcb = svm->vmcb;
  1304. int max_irr, tpr;
  1305. if (!irqchip_in_kernel(vcpu->kvm) || vcpu->arch.apic->vapic_addr)
  1306. return;
  1307. vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
  1308. max_irr = kvm_lapic_find_highest_irr(vcpu);
  1309. if (max_irr == -1)
  1310. return;
  1311. tpr = kvm_lapic_get_cr8(vcpu) << 4;
  1312. if (tpr >= (max_irr & 0xf0))
  1313. vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
  1314. }
  1315. static void svm_intr_assist(struct kvm_vcpu *vcpu)
  1316. {
  1317. struct vcpu_svm *svm = to_svm(vcpu);
  1318. struct vmcb *vmcb = svm->vmcb;
  1319. int intr_vector = -1;
  1320. if ((vmcb->control.exit_int_info & SVM_EVTINJ_VALID) &&
  1321. ((vmcb->control.exit_int_info & SVM_EVTINJ_TYPE_MASK) == 0)) {
  1322. intr_vector = vmcb->control.exit_int_info &
  1323. SVM_EVTINJ_VEC_MASK;
  1324. vmcb->control.exit_int_info = 0;
  1325. svm_inject_irq(svm, intr_vector);
  1326. goto out;
  1327. }
  1328. if (vmcb->control.int_ctl & V_IRQ_MASK)
  1329. goto out;
  1330. if (!kvm_cpu_has_interrupt(vcpu))
  1331. goto out;
  1332. if (!(vmcb->save.rflags & X86_EFLAGS_IF) ||
  1333. (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) ||
  1334. (vmcb->control.event_inj & SVM_EVTINJ_VALID)) {
  1335. /* unable to deliver irq, set pending irq */
  1336. vmcb->control.intercept |= (1ULL << INTERCEPT_VINTR);
  1337. svm_inject_irq(svm, 0x0);
  1338. goto out;
  1339. }
  1340. /* Okay, we can deliver the interrupt: grab it and update PIC state. */
  1341. intr_vector = kvm_cpu_get_interrupt(vcpu);
  1342. svm_inject_irq(svm, intr_vector);
  1343. kvm_timer_intr_post(vcpu, intr_vector);
  1344. out:
  1345. update_cr8_intercept(vcpu);
  1346. }
  1347. static void kvm_reput_irq(struct vcpu_svm *svm)
  1348. {
  1349. struct vmcb_control_area *control = &svm->vmcb->control;
  1350. if ((control->int_ctl & V_IRQ_MASK)
  1351. && !irqchip_in_kernel(svm->vcpu.kvm)) {
  1352. control->int_ctl &= ~V_IRQ_MASK;
  1353. push_irq(&svm->vcpu, control->int_vector);
  1354. }
  1355. svm->vcpu.arch.interrupt_window_open =
  1356. !(control->int_state & SVM_INTERRUPT_SHADOW_MASK);
  1357. }
  1358. static void svm_do_inject_vector(struct vcpu_svm *svm)
  1359. {
  1360. struct kvm_vcpu *vcpu = &svm->vcpu;
  1361. int word_index = __ffs(vcpu->arch.irq_summary);
  1362. int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
  1363. int irq = word_index * BITS_PER_LONG + bit_index;
  1364. clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
  1365. if (!vcpu->arch.irq_pending[word_index])
  1366. clear_bit(word_index, &vcpu->arch.irq_summary);
  1367. svm_inject_irq(svm, irq);
  1368. }
  1369. static void do_interrupt_requests(struct kvm_vcpu *vcpu,
  1370. struct kvm_run *kvm_run)
  1371. {
  1372. struct vcpu_svm *svm = to_svm(vcpu);
  1373. struct vmcb_control_area *control = &svm->vmcb->control;
  1374. svm->vcpu.arch.interrupt_window_open =
  1375. (!(control->int_state & SVM_INTERRUPT_SHADOW_MASK) &&
  1376. (svm->vmcb->save.rflags & X86_EFLAGS_IF));
  1377. if (svm->vcpu.arch.interrupt_window_open && svm->vcpu.arch.irq_summary)
  1378. /*
  1379. * If interrupts enabled, and not blocked by sti or mov ss. Good.
  1380. */
  1381. svm_do_inject_vector(svm);
  1382. /*
  1383. * Interrupts blocked. Wait for unblock.
  1384. */
  1385. if (!svm->vcpu.arch.interrupt_window_open &&
  1386. (svm->vcpu.arch.irq_summary || kvm_run->request_interrupt_window))
  1387. control->intercept |= 1ULL << INTERCEPT_VINTR;
  1388. else
  1389. control->intercept &= ~(1ULL << INTERCEPT_VINTR);
  1390. }
  1391. static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
  1392. {
  1393. return 0;
  1394. }
  1395. static void save_db_regs(unsigned long *db_regs)
  1396. {
  1397. asm volatile ("mov %%dr0, %0" : "=r"(db_regs[0]));
  1398. asm volatile ("mov %%dr1, %0" : "=r"(db_regs[1]));
  1399. asm volatile ("mov %%dr2, %0" : "=r"(db_regs[2]));
  1400. asm volatile ("mov %%dr3, %0" : "=r"(db_regs[3]));
  1401. }
  1402. static void load_db_regs(unsigned long *db_regs)
  1403. {
  1404. asm volatile ("mov %0, %%dr0" : : "r"(db_regs[0]));
  1405. asm volatile ("mov %0, %%dr1" : : "r"(db_regs[1]));
  1406. asm volatile ("mov %0, %%dr2" : : "r"(db_regs[2]));
  1407. asm volatile ("mov %0, %%dr3" : : "r"(db_regs[3]));
  1408. }
  1409. static void svm_flush_tlb(struct kvm_vcpu *vcpu)
  1410. {
  1411. force_new_asid(vcpu);
  1412. }
  1413. static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
  1414. {
  1415. }
  1416. static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
  1417. {
  1418. struct vcpu_svm *svm = to_svm(vcpu);
  1419. if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) {
  1420. int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
  1421. kvm_lapic_set_tpr(vcpu, cr8);
  1422. }
  1423. }
  1424. static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
  1425. {
  1426. struct vcpu_svm *svm = to_svm(vcpu);
  1427. u64 cr8;
  1428. if (!irqchip_in_kernel(vcpu->kvm))
  1429. return;
  1430. cr8 = kvm_get_cr8(vcpu);
  1431. svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
  1432. svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
  1433. }
  1434. #ifdef CONFIG_X86_64
  1435. #define R "r"
  1436. #else
  1437. #define R "e"
  1438. #endif
  1439. static void svm_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  1440. {
  1441. struct vcpu_svm *svm = to_svm(vcpu);
  1442. u16 fs_selector;
  1443. u16 gs_selector;
  1444. u16 ldt_selector;
  1445. svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
  1446. svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
  1447. svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
  1448. pre_svm_run(svm);
  1449. sync_lapic_to_cr8(vcpu);
  1450. save_host_msrs(vcpu);
  1451. fs_selector = kvm_read_fs();
  1452. gs_selector = kvm_read_gs();
  1453. ldt_selector = kvm_read_ldt();
  1454. svm->host_cr2 = kvm_read_cr2();
  1455. svm->host_dr6 = read_dr6();
  1456. svm->host_dr7 = read_dr7();
  1457. svm->vmcb->save.cr2 = vcpu->arch.cr2;
  1458. /* required for live migration with NPT */
  1459. if (npt_enabled)
  1460. svm->vmcb->save.cr3 = vcpu->arch.cr3;
  1461. if (svm->vmcb->save.dr7 & 0xff) {
  1462. write_dr7(0);
  1463. save_db_regs(svm->host_db_regs);
  1464. load_db_regs(svm->db_regs);
  1465. }
  1466. clgi();
  1467. local_irq_enable();
  1468. asm volatile (
  1469. "push %%"R"bp; \n\t"
  1470. "mov %c[rbx](%[svm]), %%"R"bx \n\t"
  1471. "mov %c[rcx](%[svm]), %%"R"cx \n\t"
  1472. "mov %c[rdx](%[svm]), %%"R"dx \n\t"
  1473. "mov %c[rsi](%[svm]), %%"R"si \n\t"
  1474. "mov %c[rdi](%[svm]), %%"R"di \n\t"
  1475. "mov %c[rbp](%[svm]), %%"R"bp \n\t"
  1476. #ifdef CONFIG_X86_64
  1477. "mov %c[r8](%[svm]), %%r8 \n\t"
  1478. "mov %c[r9](%[svm]), %%r9 \n\t"
  1479. "mov %c[r10](%[svm]), %%r10 \n\t"
  1480. "mov %c[r11](%[svm]), %%r11 \n\t"
  1481. "mov %c[r12](%[svm]), %%r12 \n\t"
  1482. "mov %c[r13](%[svm]), %%r13 \n\t"
  1483. "mov %c[r14](%[svm]), %%r14 \n\t"
  1484. "mov %c[r15](%[svm]), %%r15 \n\t"
  1485. #endif
  1486. /* Enter guest mode */
  1487. "push %%"R"ax \n\t"
  1488. "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
  1489. __ex(SVM_VMLOAD) "\n\t"
  1490. __ex(SVM_VMRUN) "\n\t"
  1491. __ex(SVM_VMSAVE) "\n\t"
  1492. "pop %%"R"ax \n\t"
  1493. /* Save guest registers, load host registers */
  1494. "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
  1495. "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
  1496. "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
  1497. "mov %%"R"si, %c[rsi](%[svm]) \n\t"
  1498. "mov %%"R"di, %c[rdi](%[svm]) \n\t"
  1499. "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
  1500. #ifdef CONFIG_X86_64
  1501. "mov %%r8, %c[r8](%[svm]) \n\t"
  1502. "mov %%r9, %c[r9](%[svm]) \n\t"
  1503. "mov %%r10, %c[r10](%[svm]) \n\t"
  1504. "mov %%r11, %c[r11](%[svm]) \n\t"
  1505. "mov %%r12, %c[r12](%[svm]) \n\t"
  1506. "mov %%r13, %c[r13](%[svm]) \n\t"
  1507. "mov %%r14, %c[r14](%[svm]) \n\t"
  1508. "mov %%r15, %c[r15](%[svm]) \n\t"
  1509. #endif
  1510. "pop %%"R"bp"
  1511. :
  1512. : [svm]"a"(svm),
  1513. [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
  1514. [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
  1515. [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
  1516. [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
  1517. [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
  1518. [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
  1519. [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
  1520. #ifdef CONFIG_X86_64
  1521. , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
  1522. [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
  1523. [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
  1524. [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
  1525. [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
  1526. [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
  1527. [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
  1528. [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
  1529. #endif
  1530. : "cc", "memory"
  1531. , R"bx", R"cx", R"dx", R"si", R"di"
  1532. #ifdef CONFIG_X86_64
  1533. , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
  1534. #endif
  1535. );
  1536. if ((svm->vmcb->save.dr7 & 0xff))
  1537. load_db_regs(svm->host_db_regs);
  1538. vcpu->arch.cr2 = svm->vmcb->save.cr2;
  1539. vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
  1540. vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
  1541. vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
  1542. write_dr6(svm->host_dr6);
  1543. write_dr7(svm->host_dr7);
  1544. kvm_write_cr2(svm->host_cr2);
  1545. kvm_load_fs(fs_selector);
  1546. kvm_load_gs(gs_selector);
  1547. kvm_load_ldt(ldt_selector);
  1548. load_host_msrs(vcpu);
  1549. reload_tss(vcpu);
  1550. local_irq_disable();
  1551. stgi();
  1552. sync_cr8_to_lapic(vcpu);
  1553. svm->next_rip = 0;
  1554. }
  1555. #undef R
  1556. static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  1557. {
  1558. struct vcpu_svm *svm = to_svm(vcpu);
  1559. if (npt_enabled) {
  1560. svm->vmcb->control.nested_cr3 = root;
  1561. force_new_asid(vcpu);
  1562. return;
  1563. }
  1564. svm->vmcb->save.cr3 = root;
  1565. force_new_asid(vcpu);
  1566. if (vcpu->fpu_active) {
  1567. svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
  1568. svm->vmcb->save.cr0 |= X86_CR0_TS;
  1569. vcpu->fpu_active = 0;
  1570. }
  1571. }
  1572. static int is_disabled(void)
  1573. {
  1574. u64 vm_cr;
  1575. rdmsrl(MSR_VM_CR, vm_cr);
  1576. if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
  1577. return 1;
  1578. return 0;
  1579. }
  1580. static void
  1581. svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  1582. {
  1583. /*
  1584. * Patch in the VMMCALL instruction:
  1585. */
  1586. hypercall[0] = 0x0f;
  1587. hypercall[1] = 0x01;
  1588. hypercall[2] = 0xd9;
  1589. }
  1590. static void svm_check_processor_compat(void *rtn)
  1591. {
  1592. *(int *)rtn = 0;
  1593. }
  1594. static bool svm_cpu_has_accelerated_tpr(void)
  1595. {
  1596. return false;
  1597. }
  1598. static int get_npt_level(void)
  1599. {
  1600. #ifdef CONFIG_X86_64
  1601. return PT64_ROOT_LEVEL;
  1602. #else
  1603. return PT32E_ROOT_LEVEL;
  1604. #endif
  1605. }
  1606. static int svm_get_mt_mask_shift(void)
  1607. {
  1608. return 0;
  1609. }
  1610. static struct kvm_x86_ops svm_x86_ops = {
  1611. .cpu_has_kvm_support = has_svm,
  1612. .disabled_by_bios = is_disabled,
  1613. .hardware_setup = svm_hardware_setup,
  1614. .hardware_unsetup = svm_hardware_unsetup,
  1615. .check_processor_compatibility = svm_check_processor_compat,
  1616. .hardware_enable = svm_hardware_enable,
  1617. .hardware_disable = svm_hardware_disable,
  1618. .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
  1619. .vcpu_create = svm_create_vcpu,
  1620. .vcpu_free = svm_free_vcpu,
  1621. .vcpu_reset = svm_vcpu_reset,
  1622. .prepare_guest_switch = svm_prepare_guest_switch,
  1623. .vcpu_load = svm_vcpu_load,
  1624. .vcpu_put = svm_vcpu_put,
  1625. .set_guest_debug = svm_guest_debug,
  1626. .get_msr = svm_get_msr,
  1627. .set_msr = svm_set_msr,
  1628. .get_segment_base = svm_get_segment_base,
  1629. .get_segment = svm_get_segment,
  1630. .set_segment = svm_set_segment,
  1631. .get_cpl = svm_get_cpl,
  1632. .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
  1633. .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
  1634. .set_cr0 = svm_set_cr0,
  1635. .set_cr3 = svm_set_cr3,
  1636. .set_cr4 = svm_set_cr4,
  1637. .set_efer = svm_set_efer,
  1638. .get_idt = svm_get_idt,
  1639. .set_idt = svm_set_idt,
  1640. .get_gdt = svm_get_gdt,
  1641. .set_gdt = svm_set_gdt,
  1642. .get_dr = svm_get_dr,
  1643. .set_dr = svm_set_dr,
  1644. .get_rflags = svm_get_rflags,
  1645. .set_rflags = svm_set_rflags,
  1646. .tlb_flush = svm_flush_tlb,
  1647. .run = svm_vcpu_run,
  1648. .handle_exit = handle_exit,
  1649. .skip_emulated_instruction = skip_emulated_instruction,
  1650. .patch_hypercall = svm_patch_hypercall,
  1651. .get_irq = svm_get_irq,
  1652. .set_irq = svm_set_irq,
  1653. .queue_exception = svm_queue_exception,
  1654. .exception_injected = svm_exception_injected,
  1655. .inject_pending_irq = svm_intr_assist,
  1656. .inject_pending_vectors = do_interrupt_requests,
  1657. .set_tss_addr = svm_set_tss_addr,
  1658. .get_tdp_level = get_npt_level,
  1659. .get_mt_mask_shift = svm_get_mt_mask_shift,
  1660. };
  1661. static int __init svm_init(void)
  1662. {
  1663. return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
  1664. THIS_MODULE);
  1665. }
  1666. static void __exit svm_exit(void)
  1667. {
  1668. kvm_exit();
  1669. }
  1670. module_init(svm_init)
  1671. module_exit(svm_exit)