svm.c 68 KB

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