svm.c 69 KB

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