svm.c 75 KB

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