svm.c 76 KB

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