svm.c 54 KB

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