svm.c 69 KB

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