svm.c 73 KB

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