svm.c 49 KB

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