svm.c 70 KB

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