svm.c 71 KB

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