svm.c 69 KB

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