svm.c 59 KB

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