svm.c 74 KB

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