svm.c 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * AMD SVM support
  5. *
  6. * Copyright (C) 2006 Qumranet, Inc.
  7. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  8. *
  9. * Authors:
  10. * Yaniv Kamay <yaniv@qumranet.com>
  11. * Avi Kivity <avi@qumranet.com>
  12. *
  13. * This work is licensed under the terms of the GNU GPL, version 2. See
  14. * the COPYING file in the top-level directory.
  15. *
  16. */
  17. #include <linux/kvm_host.h>
  18. #include "irq.h"
  19. #include "mmu.h"
  20. #include "kvm_cache_regs.h"
  21. #include "x86.h"
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/highmem.h>
  26. #include <linux/sched.h>
  27. #include <linux/ftrace_event.h>
  28. #include <linux/slab.h>
  29. #include <asm/tlbflush.h>
  30. #include <asm/desc.h>
  31. #include <asm/kvm_para.h>
  32. #include <asm/virtext.h>
  33. #include "trace.h"
  34. #define __ex(x) __kvm_handle_fault_on_reboot(x)
  35. MODULE_AUTHOR("Qumranet");
  36. MODULE_LICENSE("GPL");
  37. #define IOPM_ALLOC_ORDER 2
  38. #define MSRPM_ALLOC_ORDER 1
  39. #define SEG_TYPE_LDT 2
  40. #define SEG_TYPE_BUSY_TSS16 3
  41. #define SVM_FEATURE_NPT (1 << 0)
  42. #define SVM_FEATURE_LBRV (1 << 1)
  43. #define SVM_FEATURE_SVML (1 << 2)
  44. #define SVM_FEATURE_NRIP (1 << 3)
  45. #define SVM_FEATURE_PAUSE_FILTER (1 << 10)
  46. #define NESTED_EXIT_HOST 0 /* Exit handled on host level */
  47. #define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
  48. #define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
  49. #define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
  50. static bool erratum_383_found __read_mostly;
  51. static const u32 host_save_user_msrs[] = {
  52. #ifdef CONFIG_X86_64
  53. MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
  54. MSR_FS_BASE,
  55. #endif
  56. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  57. };
  58. #define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
  59. struct kvm_vcpu;
  60. struct nested_state {
  61. struct vmcb *hsave;
  62. u64 hsave_msr;
  63. u64 vm_cr_msr;
  64. u64 vmcb;
  65. /* These are the merged vectors */
  66. u32 *msrpm;
  67. /* gpa pointers to the real vectors */
  68. u64 vmcb_msrpm;
  69. u64 vmcb_iopm;
  70. /* A VMEXIT is required but not yet emulated */
  71. bool exit_required;
  72. /*
  73. * If we vmexit during an instruction emulation we need this to restore
  74. * the l1 guest rip after the emulation
  75. */
  76. unsigned long vmexit_rip;
  77. unsigned long vmexit_rsp;
  78. unsigned long vmexit_rax;
  79. /* cache for intercepts of the guest */
  80. u32 intercept_cr;
  81. u32 intercept_dr;
  82. u32 intercept_exceptions;
  83. u64 intercept;
  84. /* Nested Paging related state */
  85. u64 nested_cr3;
  86. };
  87. #define MSRPM_OFFSETS 16
  88. static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
  89. struct vcpu_svm {
  90. struct kvm_vcpu vcpu;
  91. struct vmcb *vmcb;
  92. unsigned long vmcb_pa;
  93. struct svm_cpu_data *svm_data;
  94. uint64_t asid_generation;
  95. uint64_t sysenter_esp;
  96. uint64_t sysenter_eip;
  97. u64 next_rip;
  98. u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
  99. struct {
  100. u16 fs;
  101. u16 gs;
  102. u16 ldt;
  103. u64 gs_base;
  104. } host;
  105. u32 *msrpm;
  106. struct nested_state nested;
  107. bool nmi_singlestep;
  108. unsigned int3_injected;
  109. unsigned long int3_rip;
  110. u32 apf_reason;
  111. };
  112. #define MSR_INVALID 0xffffffffU
  113. static struct svm_direct_access_msrs {
  114. u32 index; /* Index of the MSR */
  115. bool always; /* True if intercept is always on */
  116. } direct_access_msrs[] = {
  117. { .index = MSR_STAR, .always = true },
  118. { .index = MSR_IA32_SYSENTER_CS, .always = true },
  119. #ifdef CONFIG_X86_64
  120. { .index = MSR_GS_BASE, .always = true },
  121. { .index = MSR_FS_BASE, .always = true },
  122. { .index = MSR_KERNEL_GS_BASE, .always = true },
  123. { .index = MSR_LSTAR, .always = true },
  124. { .index = MSR_CSTAR, .always = true },
  125. { .index = MSR_SYSCALL_MASK, .always = true },
  126. #endif
  127. { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
  128. { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
  129. { .index = MSR_IA32_LASTINTFROMIP, .always = false },
  130. { .index = MSR_IA32_LASTINTTOIP, .always = false },
  131. { .index = MSR_INVALID, .always = false },
  132. };
  133. /* enable NPT for AMD64 and X86 with PAE */
  134. #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
  135. static bool npt_enabled = true;
  136. #else
  137. static bool npt_enabled;
  138. #endif
  139. static int npt = 1;
  140. module_param(npt, int, S_IRUGO);
  141. static int nested = 1;
  142. module_param(nested, int, S_IRUGO);
  143. static void svm_flush_tlb(struct kvm_vcpu *vcpu);
  144. static void svm_complete_interrupts(struct vcpu_svm *svm);
  145. static int nested_svm_exit_handled(struct vcpu_svm *svm);
  146. static int nested_svm_intercept(struct vcpu_svm *svm);
  147. static int nested_svm_vmexit(struct vcpu_svm *svm);
  148. static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
  149. bool has_error_code, u32 error_code);
  150. enum {
  151. VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
  152. pause filter count */
  153. VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
  154. VMCB_ASID, /* ASID */
  155. VMCB_INTR, /* int_ctl, int_vector */
  156. VMCB_NPT, /* npt_en, nCR3, gPAT */
  157. VMCB_CR, /* CR0, CR3, CR4, EFER */
  158. VMCB_DR, /* DR6, DR7 */
  159. VMCB_DT, /* GDT, IDT */
  160. VMCB_SEG, /* CS, DS, SS, ES, CPL */
  161. VMCB_CR2, /* CR2 only */
  162. VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
  163. VMCB_DIRTY_MAX,
  164. };
  165. /* TPR and CR2 are always written before VMRUN */
  166. #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
  167. static inline void mark_all_dirty(struct vmcb *vmcb)
  168. {
  169. vmcb->control.clean = 0;
  170. }
  171. static inline void mark_all_clean(struct vmcb *vmcb)
  172. {
  173. vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
  174. & ~VMCB_ALWAYS_DIRTY_MASK;
  175. }
  176. static inline void mark_dirty(struct vmcb *vmcb, int bit)
  177. {
  178. vmcb->control.clean &= ~(1 << bit);
  179. }
  180. static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
  181. {
  182. return container_of(vcpu, struct vcpu_svm, vcpu);
  183. }
  184. static void recalc_intercepts(struct vcpu_svm *svm)
  185. {
  186. struct vmcb_control_area *c, *h;
  187. struct nested_state *g;
  188. mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
  189. if (!is_guest_mode(&svm->vcpu))
  190. return;
  191. c = &svm->vmcb->control;
  192. h = &svm->nested.hsave->control;
  193. g = &svm->nested;
  194. c->intercept_cr = h->intercept_cr | g->intercept_cr;
  195. c->intercept_dr = h->intercept_dr | g->intercept_dr;
  196. c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
  197. c->intercept = h->intercept | g->intercept;
  198. }
  199. static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
  200. {
  201. if (is_guest_mode(&svm->vcpu))
  202. return svm->nested.hsave;
  203. else
  204. return svm->vmcb;
  205. }
  206. static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
  207. {
  208. struct vmcb *vmcb = get_host_vmcb(svm);
  209. vmcb->control.intercept_cr |= (1U << bit);
  210. recalc_intercepts(svm);
  211. }
  212. static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
  213. {
  214. struct vmcb *vmcb = get_host_vmcb(svm);
  215. vmcb->control.intercept_cr &= ~(1U << bit);
  216. recalc_intercepts(svm);
  217. }
  218. static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
  219. {
  220. struct vmcb *vmcb = get_host_vmcb(svm);
  221. return vmcb->control.intercept_cr & (1U << bit);
  222. }
  223. static inline void set_dr_intercept(struct vcpu_svm *svm, int bit)
  224. {
  225. struct vmcb *vmcb = get_host_vmcb(svm);
  226. vmcb->control.intercept_dr |= (1U << bit);
  227. recalc_intercepts(svm);
  228. }
  229. static inline void clr_dr_intercept(struct vcpu_svm *svm, int bit)
  230. {
  231. struct vmcb *vmcb = get_host_vmcb(svm);
  232. vmcb->control.intercept_dr &= ~(1U << bit);
  233. recalc_intercepts(svm);
  234. }
  235. static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
  236. {
  237. struct vmcb *vmcb = get_host_vmcb(svm);
  238. vmcb->control.intercept_exceptions |= (1U << bit);
  239. recalc_intercepts(svm);
  240. }
  241. static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
  242. {
  243. struct vmcb *vmcb = get_host_vmcb(svm);
  244. vmcb->control.intercept_exceptions &= ~(1U << bit);
  245. recalc_intercepts(svm);
  246. }
  247. static inline void set_intercept(struct vcpu_svm *svm, int bit)
  248. {
  249. struct vmcb *vmcb = get_host_vmcb(svm);
  250. vmcb->control.intercept |= (1ULL << bit);
  251. recalc_intercepts(svm);
  252. }
  253. static inline void clr_intercept(struct vcpu_svm *svm, int bit)
  254. {
  255. struct vmcb *vmcb = get_host_vmcb(svm);
  256. vmcb->control.intercept &= ~(1ULL << bit);
  257. recalc_intercepts(svm);
  258. }
  259. static inline void enable_gif(struct vcpu_svm *svm)
  260. {
  261. svm->vcpu.arch.hflags |= HF_GIF_MASK;
  262. }
  263. static inline void disable_gif(struct vcpu_svm *svm)
  264. {
  265. svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
  266. }
  267. static inline bool gif_set(struct vcpu_svm *svm)
  268. {
  269. return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
  270. }
  271. static unsigned long iopm_base;
  272. struct kvm_ldttss_desc {
  273. u16 limit0;
  274. u16 base0;
  275. unsigned base1:8, type:5, dpl:2, p:1;
  276. unsigned limit1:4, zero0:3, g:1, base2:8;
  277. u32 base3;
  278. u32 zero1;
  279. } __attribute__((packed));
  280. struct svm_cpu_data {
  281. int cpu;
  282. u64 asid_generation;
  283. u32 max_asid;
  284. u32 next_asid;
  285. struct kvm_ldttss_desc *tss_desc;
  286. struct page *save_area;
  287. };
  288. static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
  289. static uint32_t svm_features;
  290. struct svm_init_data {
  291. int cpu;
  292. int r;
  293. };
  294. static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
  295. #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
  296. #define MSRS_RANGE_SIZE 2048
  297. #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
  298. static u32 svm_msrpm_offset(u32 msr)
  299. {
  300. u32 offset;
  301. int i;
  302. for (i = 0; i < NUM_MSR_MAPS; i++) {
  303. if (msr < msrpm_ranges[i] ||
  304. msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
  305. continue;
  306. offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
  307. offset += (i * MSRS_RANGE_SIZE); /* add range offset */
  308. /* Now we have the u8 offset - but need the u32 offset */
  309. return offset / 4;
  310. }
  311. /* MSR not in any range */
  312. return MSR_INVALID;
  313. }
  314. #define MAX_INST_SIZE 15
  315. static inline void clgi(void)
  316. {
  317. asm volatile (__ex(SVM_CLGI));
  318. }
  319. static inline void stgi(void)
  320. {
  321. asm volatile (__ex(SVM_STGI));
  322. }
  323. static inline void invlpga(unsigned long addr, u32 asid)
  324. {
  325. asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
  326. }
  327. static inline void force_new_asid(struct kvm_vcpu *vcpu)
  328. {
  329. to_svm(vcpu)->asid_generation--;
  330. }
  331. static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
  332. {
  333. force_new_asid(vcpu);
  334. }
  335. static int get_npt_level(void)
  336. {
  337. #ifdef CONFIG_X86_64
  338. return PT64_ROOT_LEVEL;
  339. #else
  340. return PT32E_ROOT_LEVEL;
  341. #endif
  342. }
  343. static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  344. {
  345. vcpu->arch.efer = efer;
  346. if (!npt_enabled && !(efer & EFER_LMA))
  347. efer &= ~EFER_LME;
  348. to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
  349. mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
  350. }
  351. static int is_external_interrupt(u32 info)
  352. {
  353. info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
  354. return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
  355. }
  356. static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  357. {
  358. struct vcpu_svm *svm = to_svm(vcpu);
  359. u32 ret = 0;
  360. if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
  361. ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
  362. return ret & mask;
  363. }
  364. static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  365. {
  366. struct vcpu_svm *svm = to_svm(vcpu);
  367. if (mask == 0)
  368. svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
  369. else
  370. svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
  371. }
  372. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  373. {
  374. struct vcpu_svm *svm = to_svm(vcpu);
  375. if (svm->vmcb->control.next_rip != 0)
  376. svm->next_rip = svm->vmcb->control.next_rip;
  377. if (!svm->next_rip) {
  378. if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) !=
  379. EMULATE_DONE)
  380. printk(KERN_DEBUG "%s: NOP\n", __func__);
  381. return;
  382. }
  383. if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
  384. printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
  385. __func__, kvm_rip_read(vcpu), svm->next_rip);
  386. kvm_rip_write(vcpu, svm->next_rip);
  387. svm_set_interrupt_shadow(vcpu, 0);
  388. }
  389. static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
  390. bool has_error_code, u32 error_code,
  391. bool reinject)
  392. {
  393. struct vcpu_svm *svm = to_svm(vcpu);
  394. /*
  395. * If we are within a nested VM we'd better #VMEXIT and let the guest
  396. * handle the exception
  397. */
  398. if (!reinject &&
  399. nested_svm_check_exception(svm, nr, has_error_code, error_code))
  400. return;
  401. if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
  402. unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
  403. /*
  404. * For guest debugging where we have to reinject #BP if some
  405. * INT3 is guest-owned:
  406. * Emulate nRIP by moving RIP forward. Will fail if injection
  407. * raises a fault that is not intercepted. Still better than
  408. * failing in all cases.
  409. */
  410. skip_emulated_instruction(&svm->vcpu);
  411. rip = kvm_rip_read(&svm->vcpu);
  412. svm->int3_rip = rip + svm->vmcb->save.cs.base;
  413. svm->int3_injected = rip - old_rip;
  414. }
  415. svm->vmcb->control.event_inj = nr
  416. | SVM_EVTINJ_VALID
  417. | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
  418. | SVM_EVTINJ_TYPE_EXEPT;
  419. svm->vmcb->control.event_inj_err = error_code;
  420. }
  421. static void svm_init_erratum_383(void)
  422. {
  423. u32 low, high;
  424. int err;
  425. u64 val;
  426. if (!cpu_has_amd_erratum(amd_erratum_383))
  427. return;
  428. /* Use _safe variants to not break nested virtualization */
  429. val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
  430. if (err)
  431. return;
  432. val |= (1ULL << 47);
  433. low = lower_32_bits(val);
  434. high = upper_32_bits(val);
  435. native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
  436. erratum_383_found = true;
  437. }
  438. static int has_svm(void)
  439. {
  440. const char *msg;
  441. if (!cpu_has_svm(&msg)) {
  442. printk(KERN_INFO "has_svm: %s\n", msg);
  443. return 0;
  444. }
  445. return 1;
  446. }
  447. static void svm_hardware_disable(void *garbage)
  448. {
  449. cpu_svm_disable();
  450. }
  451. static int svm_hardware_enable(void *garbage)
  452. {
  453. struct svm_cpu_data *sd;
  454. uint64_t efer;
  455. struct desc_ptr gdt_descr;
  456. struct desc_struct *gdt;
  457. int me = raw_smp_processor_id();
  458. rdmsrl(MSR_EFER, efer);
  459. if (efer & EFER_SVME)
  460. return -EBUSY;
  461. if (!has_svm()) {
  462. printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
  463. me);
  464. return -EINVAL;
  465. }
  466. sd = per_cpu(svm_data, me);
  467. if (!sd) {
  468. printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
  469. me);
  470. return -EINVAL;
  471. }
  472. sd->asid_generation = 1;
  473. sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
  474. sd->next_asid = sd->max_asid + 1;
  475. native_store_gdt(&gdt_descr);
  476. gdt = (struct desc_struct *)gdt_descr.address;
  477. sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
  478. wrmsrl(MSR_EFER, efer | EFER_SVME);
  479. wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
  480. svm_init_erratum_383();
  481. return 0;
  482. }
  483. static void svm_cpu_uninit(int cpu)
  484. {
  485. struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
  486. if (!sd)
  487. return;
  488. per_cpu(svm_data, raw_smp_processor_id()) = NULL;
  489. __free_page(sd->save_area);
  490. kfree(sd);
  491. }
  492. static int svm_cpu_init(int cpu)
  493. {
  494. struct svm_cpu_data *sd;
  495. int r;
  496. sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
  497. if (!sd)
  498. return -ENOMEM;
  499. sd->cpu = cpu;
  500. sd->save_area = alloc_page(GFP_KERNEL);
  501. r = -ENOMEM;
  502. if (!sd->save_area)
  503. goto err_1;
  504. per_cpu(svm_data, cpu) = sd;
  505. return 0;
  506. err_1:
  507. kfree(sd);
  508. return r;
  509. }
  510. static bool valid_msr_intercept(u32 index)
  511. {
  512. int i;
  513. for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
  514. if (direct_access_msrs[i].index == index)
  515. return true;
  516. return false;
  517. }
  518. static void set_msr_interception(u32 *msrpm, unsigned msr,
  519. int read, int write)
  520. {
  521. u8 bit_read, bit_write;
  522. unsigned long tmp;
  523. u32 offset;
  524. /*
  525. * If this warning triggers extend the direct_access_msrs list at the
  526. * beginning of the file
  527. */
  528. WARN_ON(!valid_msr_intercept(msr));
  529. offset = svm_msrpm_offset(msr);
  530. bit_read = 2 * (msr & 0x0f);
  531. bit_write = 2 * (msr & 0x0f) + 1;
  532. tmp = msrpm[offset];
  533. BUG_ON(offset == MSR_INVALID);
  534. read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
  535. write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
  536. msrpm[offset] = tmp;
  537. }
  538. static void svm_vcpu_init_msrpm(u32 *msrpm)
  539. {
  540. int i;
  541. memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
  542. for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
  543. if (!direct_access_msrs[i].always)
  544. continue;
  545. set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
  546. }
  547. }
  548. static void add_msr_offset(u32 offset)
  549. {
  550. int i;
  551. for (i = 0; i < MSRPM_OFFSETS; ++i) {
  552. /* Offset already in list? */
  553. if (msrpm_offsets[i] == offset)
  554. return;
  555. /* Slot used by another offset? */
  556. if (msrpm_offsets[i] != MSR_INVALID)
  557. continue;
  558. /* Add offset to list */
  559. msrpm_offsets[i] = offset;
  560. return;
  561. }
  562. /*
  563. * If this BUG triggers the msrpm_offsets table has an overflow. Just
  564. * increase MSRPM_OFFSETS in this case.
  565. */
  566. BUG();
  567. }
  568. static void init_msrpm_offsets(void)
  569. {
  570. int i;
  571. memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
  572. for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
  573. u32 offset;
  574. offset = svm_msrpm_offset(direct_access_msrs[i].index);
  575. BUG_ON(offset == MSR_INVALID);
  576. add_msr_offset(offset);
  577. }
  578. }
  579. static void svm_enable_lbrv(struct vcpu_svm *svm)
  580. {
  581. u32 *msrpm = svm->msrpm;
  582. svm->vmcb->control.lbr_ctl = 1;
  583. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
  584. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
  585. set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
  586. set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
  587. }
  588. static void svm_disable_lbrv(struct vcpu_svm *svm)
  589. {
  590. u32 *msrpm = svm->msrpm;
  591. svm->vmcb->control.lbr_ctl = 0;
  592. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
  593. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
  594. set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
  595. set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
  596. }
  597. static __init int svm_hardware_setup(void)
  598. {
  599. int cpu;
  600. struct page *iopm_pages;
  601. void *iopm_va;
  602. int r;
  603. iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
  604. if (!iopm_pages)
  605. return -ENOMEM;
  606. iopm_va = page_address(iopm_pages);
  607. memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
  608. iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
  609. init_msrpm_offsets();
  610. if (boot_cpu_has(X86_FEATURE_NX))
  611. kvm_enable_efer_bits(EFER_NX);
  612. if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
  613. kvm_enable_efer_bits(EFER_FFXSR);
  614. if (nested) {
  615. printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
  616. kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
  617. }
  618. for_each_possible_cpu(cpu) {
  619. r = svm_cpu_init(cpu);
  620. if (r)
  621. goto err;
  622. }
  623. svm_features = cpuid_edx(SVM_CPUID_FUNC);
  624. if (!boot_cpu_has(X86_FEATURE_NPT))
  625. npt_enabled = false;
  626. if (npt_enabled && !npt) {
  627. printk(KERN_INFO "kvm: Nested Paging disabled\n");
  628. npt_enabled = false;
  629. }
  630. if (npt_enabled) {
  631. printk(KERN_INFO "kvm: Nested Paging enabled\n");
  632. kvm_enable_tdp();
  633. } else
  634. kvm_disable_tdp();
  635. return 0;
  636. err:
  637. __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
  638. iopm_base = 0;
  639. return r;
  640. }
  641. static __exit void svm_hardware_unsetup(void)
  642. {
  643. int cpu;
  644. for_each_possible_cpu(cpu)
  645. svm_cpu_uninit(cpu);
  646. __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
  647. iopm_base = 0;
  648. }
  649. static void init_seg(struct vmcb_seg *seg)
  650. {
  651. seg->selector = 0;
  652. seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
  653. SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
  654. seg->limit = 0xffff;
  655. seg->base = 0;
  656. }
  657. static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
  658. {
  659. seg->selector = 0;
  660. seg->attrib = SVM_SELECTOR_P_MASK | type;
  661. seg->limit = 0xffff;
  662. seg->base = 0;
  663. }
  664. static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
  665. {
  666. struct vcpu_svm *svm = to_svm(vcpu);
  667. u64 g_tsc_offset = 0;
  668. if (is_guest_mode(vcpu)) {
  669. g_tsc_offset = svm->vmcb->control.tsc_offset -
  670. svm->nested.hsave->control.tsc_offset;
  671. svm->nested.hsave->control.tsc_offset = offset;
  672. }
  673. svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
  674. mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
  675. }
  676. static void svm_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
  677. {
  678. struct vcpu_svm *svm = to_svm(vcpu);
  679. svm->vmcb->control.tsc_offset += adjustment;
  680. if (is_guest_mode(vcpu))
  681. svm->nested.hsave->control.tsc_offset += adjustment;
  682. mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
  683. }
  684. static void init_vmcb(struct vcpu_svm *svm)
  685. {
  686. struct vmcb_control_area *control = &svm->vmcb->control;
  687. struct vmcb_save_area *save = &svm->vmcb->save;
  688. svm->vcpu.fpu_active = 1;
  689. svm->vcpu.arch.hflags = 0;
  690. set_cr_intercept(svm, INTERCEPT_CR0_READ);
  691. set_cr_intercept(svm, INTERCEPT_CR3_READ);
  692. set_cr_intercept(svm, INTERCEPT_CR4_READ);
  693. set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
  694. set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
  695. set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
  696. set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  697. set_dr_intercept(svm, INTERCEPT_DR0_READ);
  698. set_dr_intercept(svm, INTERCEPT_DR1_READ);
  699. set_dr_intercept(svm, INTERCEPT_DR2_READ);
  700. set_dr_intercept(svm, INTERCEPT_DR3_READ);
  701. set_dr_intercept(svm, INTERCEPT_DR4_READ);
  702. set_dr_intercept(svm, INTERCEPT_DR5_READ);
  703. set_dr_intercept(svm, INTERCEPT_DR6_READ);
  704. set_dr_intercept(svm, INTERCEPT_DR7_READ);
  705. set_dr_intercept(svm, INTERCEPT_DR0_WRITE);
  706. set_dr_intercept(svm, INTERCEPT_DR1_WRITE);
  707. set_dr_intercept(svm, INTERCEPT_DR2_WRITE);
  708. set_dr_intercept(svm, INTERCEPT_DR3_WRITE);
  709. set_dr_intercept(svm, INTERCEPT_DR4_WRITE);
  710. set_dr_intercept(svm, INTERCEPT_DR5_WRITE);
  711. set_dr_intercept(svm, INTERCEPT_DR6_WRITE);
  712. set_dr_intercept(svm, INTERCEPT_DR7_WRITE);
  713. set_exception_intercept(svm, PF_VECTOR);
  714. set_exception_intercept(svm, UD_VECTOR);
  715. set_exception_intercept(svm, MC_VECTOR);
  716. set_intercept(svm, INTERCEPT_INTR);
  717. set_intercept(svm, INTERCEPT_NMI);
  718. set_intercept(svm, INTERCEPT_SMI);
  719. set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
  720. set_intercept(svm, INTERCEPT_CPUID);
  721. set_intercept(svm, INTERCEPT_INVD);
  722. set_intercept(svm, INTERCEPT_HLT);
  723. set_intercept(svm, INTERCEPT_INVLPG);
  724. set_intercept(svm, INTERCEPT_INVLPGA);
  725. set_intercept(svm, INTERCEPT_IOIO_PROT);
  726. set_intercept(svm, INTERCEPT_MSR_PROT);
  727. set_intercept(svm, INTERCEPT_TASK_SWITCH);
  728. set_intercept(svm, INTERCEPT_SHUTDOWN);
  729. set_intercept(svm, INTERCEPT_VMRUN);
  730. set_intercept(svm, INTERCEPT_VMMCALL);
  731. set_intercept(svm, INTERCEPT_VMLOAD);
  732. set_intercept(svm, INTERCEPT_VMSAVE);
  733. set_intercept(svm, INTERCEPT_STGI);
  734. set_intercept(svm, INTERCEPT_CLGI);
  735. set_intercept(svm, INTERCEPT_SKINIT);
  736. set_intercept(svm, INTERCEPT_WBINVD);
  737. set_intercept(svm, INTERCEPT_MONITOR);
  738. set_intercept(svm, INTERCEPT_MWAIT);
  739. control->iopm_base_pa = iopm_base;
  740. control->msrpm_base_pa = __pa(svm->msrpm);
  741. control->int_ctl = V_INTR_MASKING_MASK;
  742. init_seg(&save->es);
  743. init_seg(&save->ss);
  744. init_seg(&save->ds);
  745. init_seg(&save->fs);
  746. init_seg(&save->gs);
  747. save->cs.selector = 0xf000;
  748. /* Executable/Readable Code Segment */
  749. save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
  750. SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
  751. save->cs.limit = 0xffff;
  752. /*
  753. * cs.base should really be 0xffff0000, but vmx can't handle that, so
  754. * be consistent with it.
  755. *
  756. * Replace when we have real mode working for vmx.
  757. */
  758. save->cs.base = 0xf0000;
  759. save->gdtr.limit = 0xffff;
  760. save->idtr.limit = 0xffff;
  761. init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
  762. init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
  763. svm_set_efer(&svm->vcpu, 0);
  764. save->dr6 = 0xffff0ff0;
  765. save->dr7 = 0x400;
  766. save->rflags = 2;
  767. save->rip = 0x0000fff0;
  768. svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
  769. /*
  770. * This is the guest-visible cr0 value.
  771. * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
  772. */
  773. svm->vcpu.arch.cr0 = 0;
  774. (void)kvm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
  775. save->cr4 = X86_CR4_PAE;
  776. /* rdx = ?? */
  777. if (npt_enabled) {
  778. /* Setup VMCB for Nested Paging */
  779. control->nested_ctl = 1;
  780. clr_intercept(svm, INTERCEPT_TASK_SWITCH);
  781. clr_intercept(svm, INTERCEPT_INVLPG);
  782. clr_exception_intercept(svm, PF_VECTOR);
  783. clr_cr_intercept(svm, INTERCEPT_CR3_READ);
  784. clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
  785. save->g_pat = 0x0007040600070406ULL;
  786. save->cr3 = 0;
  787. save->cr4 = 0;
  788. }
  789. force_new_asid(&svm->vcpu);
  790. svm->nested.vmcb = 0;
  791. svm->vcpu.arch.hflags = 0;
  792. if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
  793. control->pause_filter_count = 3000;
  794. set_intercept(svm, INTERCEPT_PAUSE);
  795. }
  796. mark_all_dirty(svm->vmcb);
  797. enable_gif(svm);
  798. }
  799. static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
  800. {
  801. struct vcpu_svm *svm = to_svm(vcpu);
  802. init_vmcb(svm);
  803. if (!kvm_vcpu_is_bsp(vcpu)) {
  804. kvm_rip_write(vcpu, 0);
  805. svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
  806. svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
  807. }
  808. vcpu->arch.regs_avail = ~0;
  809. vcpu->arch.regs_dirty = ~0;
  810. return 0;
  811. }
  812. static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
  813. {
  814. struct vcpu_svm *svm;
  815. struct page *page;
  816. struct page *msrpm_pages;
  817. struct page *hsave_page;
  818. struct page *nested_msrpm_pages;
  819. int err;
  820. svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  821. if (!svm) {
  822. err = -ENOMEM;
  823. goto out;
  824. }
  825. err = kvm_vcpu_init(&svm->vcpu, kvm, id);
  826. if (err)
  827. goto free_svm;
  828. err = -ENOMEM;
  829. page = alloc_page(GFP_KERNEL);
  830. if (!page)
  831. goto uninit;
  832. msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
  833. if (!msrpm_pages)
  834. goto free_page1;
  835. nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
  836. if (!nested_msrpm_pages)
  837. goto free_page2;
  838. hsave_page = alloc_page(GFP_KERNEL);
  839. if (!hsave_page)
  840. goto free_page3;
  841. svm->nested.hsave = page_address(hsave_page);
  842. svm->msrpm = page_address(msrpm_pages);
  843. svm_vcpu_init_msrpm(svm->msrpm);
  844. svm->nested.msrpm = page_address(nested_msrpm_pages);
  845. svm_vcpu_init_msrpm(svm->nested.msrpm);
  846. svm->vmcb = page_address(page);
  847. clear_page(svm->vmcb);
  848. svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
  849. svm->asid_generation = 0;
  850. init_vmcb(svm);
  851. kvm_write_tsc(&svm->vcpu, 0);
  852. err = fx_init(&svm->vcpu);
  853. if (err)
  854. goto free_page4;
  855. svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
  856. if (kvm_vcpu_is_bsp(&svm->vcpu))
  857. svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
  858. return &svm->vcpu;
  859. free_page4:
  860. __free_page(hsave_page);
  861. free_page3:
  862. __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
  863. free_page2:
  864. __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
  865. free_page1:
  866. __free_page(page);
  867. uninit:
  868. kvm_vcpu_uninit(&svm->vcpu);
  869. free_svm:
  870. kmem_cache_free(kvm_vcpu_cache, svm);
  871. out:
  872. return ERR_PTR(err);
  873. }
  874. static void svm_free_vcpu(struct kvm_vcpu *vcpu)
  875. {
  876. struct vcpu_svm *svm = to_svm(vcpu);
  877. __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
  878. __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
  879. __free_page(virt_to_page(svm->nested.hsave));
  880. __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
  881. kvm_vcpu_uninit(vcpu);
  882. kmem_cache_free(kvm_vcpu_cache, svm);
  883. }
  884. static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  885. {
  886. struct vcpu_svm *svm = to_svm(vcpu);
  887. int i;
  888. if (unlikely(cpu != vcpu->cpu)) {
  889. svm->asid_generation = 0;
  890. mark_all_dirty(svm->vmcb);
  891. }
  892. #ifdef CONFIG_X86_64
  893. rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
  894. #endif
  895. savesegment(fs, svm->host.fs);
  896. savesegment(gs, svm->host.gs);
  897. svm->host.ldt = kvm_read_ldt();
  898. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  899. rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  900. }
  901. static void svm_vcpu_put(struct kvm_vcpu *vcpu)
  902. {
  903. struct vcpu_svm *svm = to_svm(vcpu);
  904. int i;
  905. ++vcpu->stat.host_state_reload;
  906. kvm_load_ldt(svm->host.ldt);
  907. #ifdef CONFIG_X86_64
  908. loadsegment(fs, svm->host.fs);
  909. load_gs_index(svm->host.gs);
  910. wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
  911. #else
  912. loadsegment(gs, svm->host.gs);
  913. #endif
  914. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  915. wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  916. }
  917. static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
  918. {
  919. return to_svm(vcpu)->vmcb->save.rflags;
  920. }
  921. static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  922. {
  923. to_svm(vcpu)->vmcb->save.rflags = rflags;
  924. }
  925. static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  926. {
  927. switch (reg) {
  928. case VCPU_EXREG_PDPTR:
  929. BUG_ON(!npt_enabled);
  930. load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3);
  931. break;
  932. default:
  933. BUG();
  934. }
  935. }
  936. static void svm_set_vintr(struct vcpu_svm *svm)
  937. {
  938. set_intercept(svm, INTERCEPT_VINTR);
  939. }
  940. static void svm_clear_vintr(struct vcpu_svm *svm)
  941. {
  942. clr_intercept(svm, INTERCEPT_VINTR);
  943. }
  944. static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
  945. {
  946. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  947. switch (seg) {
  948. case VCPU_SREG_CS: return &save->cs;
  949. case VCPU_SREG_DS: return &save->ds;
  950. case VCPU_SREG_ES: return &save->es;
  951. case VCPU_SREG_FS: return &save->fs;
  952. case VCPU_SREG_GS: return &save->gs;
  953. case VCPU_SREG_SS: return &save->ss;
  954. case VCPU_SREG_TR: return &save->tr;
  955. case VCPU_SREG_LDTR: return &save->ldtr;
  956. }
  957. BUG();
  958. return NULL;
  959. }
  960. static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  961. {
  962. struct vmcb_seg *s = svm_seg(vcpu, seg);
  963. return s->base;
  964. }
  965. static void svm_get_segment(struct kvm_vcpu *vcpu,
  966. struct kvm_segment *var, int seg)
  967. {
  968. struct vmcb_seg *s = svm_seg(vcpu, seg);
  969. var->base = s->base;
  970. var->limit = s->limit;
  971. var->selector = s->selector;
  972. var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
  973. var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
  974. var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
  975. var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
  976. var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
  977. var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
  978. var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
  979. var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
  980. /*
  981. * AMD's VMCB does not have an explicit unusable field, so emulate it
  982. * for cross vendor migration purposes by "not present"
  983. */
  984. var->unusable = !var->present || (var->type == 0);
  985. switch (seg) {
  986. case VCPU_SREG_CS:
  987. /*
  988. * SVM always stores 0 for the 'G' bit in the CS selector in
  989. * the VMCB on a VMEXIT. This hurts cross-vendor migration:
  990. * Intel's VMENTRY has a check on the 'G' bit.
  991. */
  992. var->g = s->limit > 0xfffff;
  993. break;
  994. case VCPU_SREG_TR:
  995. /*
  996. * Work around a bug where the busy flag in the tr selector
  997. * isn't exposed
  998. */
  999. var->type |= 0x2;
  1000. break;
  1001. case VCPU_SREG_DS:
  1002. case VCPU_SREG_ES:
  1003. case VCPU_SREG_FS:
  1004. case VCPU_SREG_GS:
  1005. /*
  1006. * The accessed bit must always be set in the segment
  1007. * descriptor cache, although it can be cleared in the
  1008. * descriptor, the cached bit always remains at 1. Since
  1009. * Intel has a check on this, set it here to support
  1010. * cross-vendor migration.
  1011. */
  1012. if (!var->unusable)
  1013. var->type |= 0x1;
  1014. break;
  1015. case VCPU_SREG_SS:
  1016. /*
  1017. * On AMD CPUs sometimes the DB bit in the segment
  1018. * descriptor is left as 1, although the whole segment has
  1019. * been made unusable. Clear it here to pass an Intel VMX
  1020. * entry check when cross vendor migrating.
  1021. */
  1022. if (var->unusable)
  1023. var->db = 0;
  1024. break;
  1025. }
  1026. }
  1027. static int svm_get_cpl(struct kvm_vcpu *vcpu)
  1028. {
  1029. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  1030. return save->cpl;
  1031. }
  1032. static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1033. {
  1034. struct vcpu_svm *svm = to_svm(vcpu);
  1035. dt->size = svm->vmcb->save.idtr.limit;
  1036. dt->address = svm->vmcb->save.idtr.base;
  1037. }
  1038. static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1039. {
  1040. struct vcpu_svm *svm = to_svm(vcpu);
  1041. svm->vmcb->save.idtr.limit = dt->size;
  1042. svm->vmcb->save.idtr.base = dt->address ;
  1043. mark_dirty(svm->vmcb, VMCB_DT);
  1044. }
  1045. static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1046. {
  1047. struct vcpu_svm *svm = to_svm(vcpu);
  1048. dt->size = svm->vmcb->save.gdtr.limit;
  1049. dt->address = svm->vmcb->save.gdtr.base;
  1050. }
  1051. static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1052. {
  1053. struct vcpu_svm *svm = to_svm(vcpu);
  1054. svm->vmcb->save.gdtr.limit = dt->size;
  1055. svm->vmcb->save.gdtr.base = dt->address ;
  1056. mark_dirty(svm->vmcb, VMCB_DT);
  1057. }
  1058. static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  1059. {
  1060. }
  1061. static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  1062. {
  1063. }
  1064. static void update_cr0_intercept(struct vcpu_svm *svm)
  1065. {
  1066. ulong gcr0 = svm->vcpu.arch.cr0;
  1067. u64 *hcr0 = &svm->vmcb->save.cr0;
  1068. if (!svm->vcpu.fpu_active)
  1069. *hcr0 |= SVM_CR0_SELECTIVE_MASK;
  1070. else
  1071. *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
  1072. | (gcr0 & SVM_CR0_SELECTIVE_MASK);
  1073. mark_dirty(svm->vmcb, VMCB_CR);
  1074. if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
  1075. clr_cr_intercept(svm, INTERCEPT_CR0_READ);
  1076. clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
  1077. } else {
  1078. set_cr_intercept(svm, INTERCEPT_CR0_READ);
  1079. set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
  1080. }
  1081. }
  1082. static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  1083. {
  1084. struct vcpu_svm *svm = to_svm(vcpu);
  1085. if (is_guest_mode(vcpu)) {
  1086. /*
  1087. * We are here because we run in nested mode, the host kvm
  1088. * intercepts cr0 writes but the l1 hypervisor does not.
  1089. * But the L1 hypervisor may intercept selective cr0 writes.
  1090. * This needs to be checked here.
  1091. */
  1092. unsigned long old, new;
  1093. /* Remove bits that would trigger a real cr0 write intercept */
  1094. old = vcpu->arch.cr0 & SVM_CR0_SELECTIVE_MASK;
  1095. new = cr0 & SVM_CR0_SELECTIVE_MASK;
  1096. if (old == new) {
  1097. /* cr0 write with ts and mp unchanged */
  1098. svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
  1099. if (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE) {
  1100. svm->nested.vmexit_rip = kvm_rip_read(vcpu);
  1101. svm->nested.vmexit_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  1102. svm->nested.vmexit_rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
  1103. return;
  1104. }
  1105. }
  1106. }
  1107. #ifdef CONFIG_X86_64
  1108. if (vcpu->arch.efer & EFER_LME) {
  1109. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  1110. vcpu->arch.efer |= EFER_LMA;
  1111. svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
  1112. }
  1113. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
  1114. vcpu->arch.efer &= ~EFER_LMA;
  1115. svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
  1116. }
  1117. }
  1118. #endif
  1119. vcpu->arch.cr0 = cr0;
  1120. if (!npt_enabled)
  1121. cr0 |= X86_CR0_PG | X86_CR0_WP;
  1122. if (!vcpu->fpu_active)
  1123. cr0 |= X86_CR0_TS;
  1124. /*
  1125. * re-enable caching here because the QEMU bios
  1126. * does not do it - this results in some delay at
  1127. * reboot
  1128. */
  1129. cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
  1130. svm->vmcb->save.cr0 = cr0;
  1131. mark_dirty(svm->vmcb, VMCB_CR);
  1132. update_cr0_intercept(svm);
  1133. }
  1134. static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  1135. {
  1136. unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
  1137. unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
  1138. if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
  1139. force_new_asid(vcpu);
  1140. vcpu->arch.cr4 = cr4;
  1141. if (!npt_enabled)
  1142. cr4 |= X86_CR4_PAE;
  1143. cr4 |= host_cr4_mce;
  1144. to_svm(vcpu)->vmcb->save.cr4 = cr4;
  1145. mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
  1146. }
  1147. static void svm_set_segment(struct kvm_vcpu *vcpu,
  1148. struct kvm_segment *var, int seg)
  1149. {
  1150. struct vcpu_svm *svm = to_svm(vcpu);
  1151. struct vmcb_seg *s = svm_seg(vcpu, seg);
  1152. s->base = var->base;
  1153. s->limit = var->limit;
  1154. s->selector = var->selector;
  1155. if (var->unusable)
  1156. s->attrib = 0;
  1157. else {
  1158. s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
  1159. s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
  1160. s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
  1161. s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
  1162. s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
  1163. s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
  1164. s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
  1165. s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
  1166. }
  1167. if (seg == VCPU_SREG_CS)
  1168. svm->vmcb->save.cpl
  1169. = (svm->vmcb->save.cs.attrib
  1170. >> SVM_SELECTOR_DPL_SHIFT) & 3;
  1171. mark_dirty(svm->vmcb, VMCB_SEG);
  1172. }
  1173. static void update_db_intercept(struct kvm_vcpu *vcpu)
  1174. {
  1175. struct vcpu_svm *svm = to_svm(vcpu);
  1176. clr_exception_intercept(svm, DB_VECTOR);
  1177. clr_exception_intercept(svm, BP_VECTOR);
  1178. if (svm->nmi_singlestep)
  1179. set_exception_intercept(svm, DB_VECTOR);
  1180. if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
  1181. if (vcpu->guest_debug &
  1182. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  1183. set_exception_intercept(svm, DB_VECTOR);
  1184. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  1185. set_exception_intercept(svm, BP_VECTOR);
  1186. } else
  1187. vcpu->guest_debug = 0;
  1188. }
  1189. static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
  1190. {
  1191. struct vcpu_svm *svm = to_svm(vcpu);
  1192. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
  1193. svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
  1194. else
  1195. svm->vmcb->save.dr7 = vcpu->arch.dr7;
  1196. mark_dirty(svm->vmcb, VMCB_DR);
  1197. update_db_intercept(vcpu);
  1198. }
  1199. static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
  1200. {
  1201. if (sd->next_asid > sd->max_asid) {
  1202. ++sd->asid_generation;
  1203. sd->next_asid = 1;
  1204. svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
  1205. }
  1206. svm->asid_generation = sd->asid_generation;
  1207. svm->vmcb->control.asid = sd->next_asid++;
  1208. mark_dirty(svm->vmcb, VMCB_ASID);
  1209. }
  1210. static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
  1211. {
  1212. struct vcpu_svm *svm = to_svm(vcpu);
  1213. svm->vmcb->save.dr7 = value;
  1214. mark_dirty(svm->vmcb, VMCB_DR);
  1215. }
  1216. static int pf_interception(struct vcpu_svm *svm)
  1217. {
  1218. u64 fault_address = svm->vmcb->control.exit_info_2;
  1219. u32 error_code;
  1220. int r = 1;
  1221. switch (svm->apf_reason) {
  1222. default:
  1223. error_code = svm->vmcb->control.exit_info_1;
  1224. trace_kvm_page_fault(fault_address, error_code);
  1225. if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
  1226. kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
  1227. r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
  1228. break;
  1229. case KVM_PV_REASON_PAGE_NOT_PRESENT:
  1230. svm->apf_reason = 0;
  1231. local_irq_disable();
  1232. kvm_async_pf_task_wait(fault_address);
  1233. local_irq_enable();
  1234. break;
  1235. case KVM_PV_REASON_PAGE_READY:
  1236. svm->apf_reason = 0;
  1237. local_irq_disable();
  1238. kvm_async_pf_task_wake(fault_address);
  1239. local_irq_enable();
  1240. break;
  1241. }
  1242. return r;
  1243. }
  1244. static int db_interception(struct vcpu_svm *svm)
  1245. {
  1246. struct kvm_run *kvm_run = svm->vcpu.run;
  1247. if (!(svm->vcpu.guest_debug &
  1248. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
  1249. !svm->nmi_singlestep) {
  1250. kvm_queue_exception(&svm->vcpu, DB_VECTOR);
  1251. return 1;
  1252. }
  1253. if (svm->nmi_singlestep) {
  1254. svm->nmi_singlestep = false;
  1255. if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
  1256. svm->vmcb->save.rflags &=
  1257. ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
  1258. update_db_intercept(&svm->vcpu);
  1259. }
  1260. if (svm->vcpu.guest_debug &
  1261. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
  1262. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  1263. kvm_run->debug.arch.pc =
  1264. svm->vmcb->save.cs.base + svm->vmcb->save.rip;
  1265. kvm_run->debug.arch.exception = DB_VECTOR;
  1266. return 0;
  1267. }
  1268. return 1;
  1269. }
  1270. static int bp_interception(struct vcpu_svm *svm)
  1271. {
  1272. struct kvm_run *kvm_run = svm->vcpu.run;
  1273. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  1274. kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
  1275. kvm_run->debug.arch.exception = BP_VECTOR;
  1276. return 0;
  1277. }
  1278. static int ud_interception(struct vcpu_svm *svm)
  1279. {
  1280. int er;
  1281. er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
  1282. if (er != EMULATE_DONE)
  1283. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  1284. return 1;
  1285. }
  1286. static void svm_fpu_activate(struct kvm_vcpu *vcpu)
  1287. {
  1288. struct vcpu_svm *svm = to_svm(vcpu);
  1289. clr_exception_intercept(svm, NM_VECTOR);
  1290. svm->vcpu.fpu_active = 1;
  1291. update_cr0_intercept(svm);
  1292. }
  1293. static int nm_interception(struct vcpu_svm *svm)
  1294. {
  1295. svm_fpu_activate(&svm->vcpu);
  1296. return 1;
  1297. }
  1298. static bool is_erratum_383(void)
  1299. {
  1300. int err, i;
  1301. u64 value;
  1302. if (!erratum_383_found)
  1303. return false;
  1304. value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
  1305. if (err)
  1306. return false;
  1307. /* Bit 62 may or may not be set for this mce */
  1308. value &= ~(1ULL << 62);
  1309. if (value != 0xb600000000010015ULL)
  1310. return false;
  1311. /* Clear MCi_STATUS registers */
  1312. for (i = 0; i < 6; ++i)
  1313. native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
  1314. value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
  1315. if (!err) {
  1316. u32 low, high;
  1317. value &= ~(1ULL << 2);
  1318. low = lower_32_bits(value);
  1319. high = upper_32_bits(value);
  1320. native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
  1321. }
  1322. /* Flush tlb to evict multi-match entries */
  1323. __flush_tlb_all();
  1324. return true;
  1325. }
  1326. static void svm_handle_mce(struct vcpu_svm *svm)
  1327. {
  1328. if (is_erratum_383()) {
  1329. /*
  1330. * Erratum 383 triggered. Guest state is corrupt so kill the
  1331. * guest.
  1332. */
  1333. pr_err("KVM: Guest triggered AMD Erratum 383\n");
  1334. kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
  1335. return;
  1336. }
  1337. /*
  1338. * On an #MC intercept the MCE handler is not called automatically in
  1339. * the host. So do it by hand here.
  1340. */
  1341. asm volatile (
  1342. "int $0x12\n");
  1343. /* not sure if we ever come back to this point */
  1344. return;
  1345. }
  1346. static int mc_interception(struct vcpu_svm *svm)
  1347. {
  1348. return 1;
  1349. }
  1350. static int shutdown_interception(struct vcpu_svm *svm)
  1351. {
  1352. struct kvm_run *kvm_run = svm->vcpu.run;
  1353. /*
  1354. * VMCB is undefined after a SHUTDOWN intercept
  1355. * so reinitialize it.
  1356. */
  1357. clear_page(svm->vmcb);
  1358. init_vmcb(svm);
  1359. kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
  1360. return 0;
  1361. }
  1362. static int io_interception(struct vcpu_svm *svm)
  1363. {
  1364. struct kvm_vcpu *vcpu = &svm->vcpu;
  1365. u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
  1366. int size, in, string;
  1367. unsigned port;
  1368. ++svm->vcpu.stat.io_exits;
  1369. string = (io_info & SVM_IOIO_STR_MASK) != 0;
  1370. in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
  1371. if (string || in)
  1372. return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE;
  1373. port = io_info >> 16;
  1374. size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
  1375. svm->next_rip = svm->vmcb->control.exit_info_2;
  1376. skip_emulated_instruction(&svm->vcpu);
  1377. return kvm_fast_pio_out(vcpu, size, port);
  1378. }
  1379. static int nmi_interception(struct vcpu_svm *svm)
  1380. {
  1381. return 1;
  1382. }
  1383. static int intr_interception(struct vcpu_svm *svm)
  1384. {
  1385. ++svm->vcpu.stat.irq_exits;
  1386. return 1;
  1387. }
  1388. static int nop_on_interception(struct vcpu_svm *svm)
  1389. {
  1390. return 1;
  1391. }
  1392. static int halt_interception(struct vcpu_svm *svm)
  1393. {
  1394. svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
  1395. skip_emulated_instruction(&svm->vcpu);
  1396. return kvm_emulate_halt(&svm->vcpu);
  1397. }
  1398. static int vmmcall_interception(struct vcpu_svm *svm)
  1399. {
  1400. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1401. skip_emulated_instruction(&svm->vcpu);
  1402. kvm_emulate_hypercall(&svm->vcpu);
  1403. return 1;
  1404. }
  1405. static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
  1406. {
  1407. struct vcpu_svm *svm = to_svm(vcpu);
  1408. return svm->nested.nested_cr3;
  1409. }
  1410. static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
  1411. unsigned long root)
  1412. {
  1413. struct vcpu_svm *svm = to_svm(vcpu);
  1414. svm->vmcb->control.nested_cr3 = root;
  1415. mark_dirty(svm->vmcb, VMCB_NPT);
  1416. force_new_asid(vcpu);
  1417. }
  1418. static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
  1419. struct x86_exception *fault)
  1420. {
  1421. struct vcpu_svm *svm = to_svm(vcpu);
  1422. svm->vmcb->control.exit_code = SVM_EXIT_NPF;
  1423. svm->vmcb->control.exit_code_hi = 0;
  1424. svm->vmcb->control.exit_info_1 = fault->error_code;
  1425. svm->vmcb->control.exit_info_2 = fault->address;
  1426. nested_svm_vmexit(svm);
  1427. }
  1428. static int nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
  1429. {
  1430. int r;
  1431. r = kvm_init_shadow_mmu(vcpu, &vcpu->arch.mmu);
  1432. vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
  1433. vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
  1434. vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
  1435. vcpu->arch.mmu.shadow_root_level = get_npt_level();
  1436. vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
  1437. return r;
  1438. }
  1439. static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
  1440. {
  1441. vcpu->arch.walk_mmu = &vcpu->arch.mmu;
  1442. }
  1443. static int nested_svm_check_permissions(struct vcpu_svm *svm)
  1444. {
  1445. if (!(svm->vcpu.arch.efer & EFER_SVME)
  1446. || !is_paging(&svm->vcpu)) {
  1447. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  1448. return 1;
  1449. }
  1450. if (svm->vmcb->save.cpl) {
  1451. kvm_inject_gp(&svm->vcpu, 0);
  1452. return 1;
  1453. }
  1454. return 0;
  1455. }
  1456. static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
  1457. bool has_error_code, u32 error_code)
  1458. {
  1459. int vmexit;
  1460. if (!is_guest_mode(&svm->vcpu))
  1461. return 0;
  1462. svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
  1463. svm->vmcb->control.exit_code_hi = 0;
  1464. svm->vmcb->control.exit_info_1 = error_code;
  1465. svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
  1466. vmexit = nested_svm_intercept(svm);
  1467. if (vmexit == NESTED_EXIT_DONE)
  1468. svm->nested.exit_required = true;
  1469. return vmexit;
  1470. }
  1471. /* This function returns true if it is save to enable the irq window */
  1472. static inline bool nested_svm_intr(struct vcpu_svm *svm)
  1473. {
  1474. if (!is_guest_mode(&svm->vcpu))
  1475. return true;
  1476. if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
  1477. return true;
  1478. if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
  1479. return false;
  1480. /*
  1481. * if vmexit was already requested (by intercepted exception
  1482. * for instance) do not overwrite it with "external interrupt"
  1483. * vmexit.
  1484. */
  1485. if (svm->nested.exit_required)
  1486. return false;
  1487. svm->vmcb->control.exit_code = SVM_EXIT_INTR;
  1488. svm->vmcb->control.exit_info_1 = 0;
  1489. svm->vmcb->control.exit_info_2 = 0;
  1490. if (svm->nested.intercept & 1ULL) {
  1491. /*
  1492. * The #vmexit can't be emulated here directly because this
  1493. * code path runs with irqs and preemtion disabled. A
  1494. * #vmexit emulation might sleep. Only signal request for
  1495. * the #vmexit here.
  1496. */
  1497. svm->nested.exit_required = true;
  1498. trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
  1499. return false;
  1500. }
  1501. return true;
  1502. }
  1503. /* This function returns true if it is save to enable the nmi window */
  1504. static inline bool nested_svm_nmi(struct vcpu_svm *svm)
  1505. {
  1506. if (!is_guest_mode(&svm->vcpu))
  1507. return true;
  1508. if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
  1509. return true;
  1510. svm->vmcb->control.exit_code = SVM_EXIT_NMI;
  1511. svm->nested.exit_required = true;
  1512. return false;
  1513. }
  1514. static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
  1515. {
  1516. struct page *page;
  1517. might_sleep();
  1518. page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
  1519. if (is_error_page(page))
  1520. goto error;
  1521. *_page = page;
  1522. return kmap(page);
  1523. error:
  1524. kvm_release_page_clean(page);
  1525. kvm_inject_gp(&svm->vcpu, 0);
  1526. return NULL;
  1527. }
  1528. static void nested_svm_unmap(struct page *page)
  1529. {
  1530. kunmap(page);
  1531. kvm_release_page_dirty(page);
  1532. }
  1533. static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
  1534. {
  1535. unsigned port;
  1536. u8 val, bit;
  1537. u64 gpa;
  1538. if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
  1539. return NESTED_EXIT_HOST;
  1540. port = svm->vmcb->control.exit_info_1 >> 16;
  1541. gpa = svm->nested.vmcb_iopm + (port / 8);
  1542. bit = port % 8;
  1543. val = 0;
  1544. if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
  1545. val &= (1 << bit);
  1546. return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
  1547. }
  1548. static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
  1549. {
  1550. u32 offset, msr, value;
  1551. int write, mask;
  1552. if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
  1553. return NESTED_EXIT_HOST;
  1554. msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  1555. offset = svm_msrpm_offset(msr);
  1556. write = svm->vmcb->control.exit_info_1 & 1;
  1557. mask = 1 << ((2 * (msr & 0xf)) + write);
  1558. if (offset == MSR_INVALID)
  1559. return NESTED_EXIT_DONE;
  1560. /* Offset is in 32 bit units but need in 8 bit units */
  1561. offset *= 4;
  1562. if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
  1563. return NESTED_EXIT_DONE;
  1564. return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
  1565. }
  1566. static int nested_svm_exit_special(struct vcpu_svm *svm)
  1567. {
  1568. u32 exit_code = svm->vmcb->control.exit_code;
  1569. switch (exit_code) {
  1570. case SVM_EXIT_INTR:
  1571. case SVM_EXIT_NMI:
  1572. case SVM_EXIT_EXCP_BASE + MC_VECTOR:
  1573. return NESTED_EXIT_HOST;
  1574. case SVM_EXIT_NPF:
  1575. /* For now we are always handling NPFs when using them */
  1576. if (npt_enabled)
  1577. return NESTED_EXIT_HOST;
  1578. break;
  1579. case SVM_EXIT_EXCP_BASE + PF_VECTOR:
  1580. /* When we're shadowing, trap PFs, but not async PF */
  1581. if (!npt_enabled && svm->apf_reason == 0)
  1582. return NESTED_EXIT_HOST;
  1583. break;
  1584. case SVM_EXIT_EXCP_BASE + NM_VECTOR:
  1585. nm_interception(svm);
  1586. break;
  1587. default:
  1588. break;
  1589. }
  1590. return NESTED_EXIT_CONTINUE;
  1591. }
  1592. /*
  1593. * If this function returns true, this #vmexit was already handled
  1594. */
  1595. static int nested_svm_intercept(struct vcpu_svm *svm)
  1596. {
  1597. u32 exit_code = svm->vmcb->control.exit_code;
  1598. int vmexit = NESTED_EXIT_HOST;
  1599. switch (exit_code) {
  1600. case SVM_EXIT_MSR:
  1601. vmexit = nested_svm_exit_handled_msr(svm);
  1602. break;
  1603. case SVM_EXIT_IOIO:
  1604. vmexit = nested_svm_intercept_ioio(svm);
  1605. break;
  1606. case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
  1607. u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
  1608. if (svm->nested.intercept_cr & bit)
  1609. vmexit = NESTED_EXIT_DONE;
  1610. break;
  1611. }
  1612. case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
  1613. u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
  1614. if (svm->nested.intercept_dr & bit)
  1615. vmexit = NESTED_EXIT_DONE;
  1616. break;
  1617. }
  1618. case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
  1619. u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
  1620. if (svm->nested.intercept_exceptions & excp_bits)
  1621. vmexit = NESTED_EXIT_DONE;
  1622. /* async page fault always cause vmexit */
  1623. else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
  1624. svm->apf_reason != 0)
  1625. vmexit = NESTED_EXIT_DONE;
  1626. break;
  1627. }
  1628. case SVM_EXIT_ERR: {
  1629. vmexit = NESTED_EXIT_DONE;
  1630. break;
  1631. }
  1632. default: {
  1633. u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
  1634. if (svm->nested.intercept & exit_bits)
  1635. vmexit = NESTED_EXIT_DONE;
  1636. }
  1637. }
  1638. return vmexit;
  1639. }
  1640. static int nested_svm_exit_handled(struct vcpu_svm *svm)
  1641. {
  1642. int vmexit;
  1643. vmexit = nested_svm_intercept(svm);
  1644. if (vmexit == NESTED_EXIT_DONE)
  1645. nested_svm_vmexit(svm);
  1646. return vmexit;
  1647. }
  1648. static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
  1649. {
  1650. struct vmcb_control_area *dst = &dst_vmcb->control;
  1651. struct vmcb_control_area *from = &from_vmcb->control;
  1652. dst->intercept_cr = from->intercept_cr;
  1653. dst->intercept_dr = from->intercept_dr;
  1654. dst->intercept_exceptions = from->intercept_exceptions;
  1655. dst->intercept = from->intercept;
  1656. dst->iopm_base_pa = from->iopm_base_pa;
  1657. dst->msrpm_base_pa = from->msrpm_base_pa;
  1658. dst->tsc_offset = from->tsc_offset;
  1659. dst->asid = from->asid;
  1660. dst->tlb_ctl = from->tlb_ctl;
  1661. dst->int_ctl = from->int_ctl;
  1662. dst->int_vector = from->int_vector;
  1663. dst->int_state = from->int_state;
  1664. dst->exit_code = from->exit_code;
  1665. dst->exit_code_hi = from->exit_code_hi;
  1666. dst->exit_info_1 = from->exit_info_1;
  1667. dst->exit_info_2 = from->exit_info_2;
  1668. dst->exit_int_info = from->exit_int_info;
  1669. dst->exit_int_info_err = from->exit_int_info_err;
  1670. dst->nested_ctl = from->nested_ctl;
  1671. dst->event_inj = from->event_inj;
  1672. dst->event_inj_err = from->event_inj_err;
  1673. dst->nested_cr3 = from->nested_cr3;
  1674. dst->lbr_ctl = from->lbr_ctl;
  1675. }
  1676. static int nested_svm_vmexit(struct vcpu_svm *svm)
  1677. {
  1678. struct vmcb *nested_vmcb;
  1679. struct vmcb *hsave = svm->nested.hsave;
  1680. struct vmcb *vmcb = svm->vmcb;
  1681. struct page *page;
  1682. trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
  1683. vmcb->control.exit_info_1,
  1684. vmcb->control.exit_info_2,
  1685. vmcb->control.exit_int_info,
  1686. vmcb->control.exit_int_info_err);
  1687. nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
  1688. if (!nested_vmcb)
  1689. return 1;
  1690. /* Exit Guest-Mode */
  1691. leave_guest_mode(&svm->vcpu);
  1692. svm->nested.vmcb = 0;
  1693. /* Give the current vmcb to the guest */
  1694. disable_gif(svm);
  1695. nested_vmcb->save.es = vmcb->save.es;
  1696. nested_vmcb->save.cs = vmcb->save.cs;
  1697. nested_vmcb->save.ss = vmcb->save.ss;
  1698. nested_vmcb->save.ds = vmcb->save.ds;
  1699. nested_vmcb->save.gdtr = vmcb->save.gdtr;
  1700. nested_vmcb->save.idtr = vmcb->save.idtr;
  1701. nested_vmcb->save.efer = svm->vcpu.arch.efer;
  1702. nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
  1703. nested_vmcb->save.cr3 = svm->vcpu.arch.cr3;
  1704. nested_vmcb->save.cr2 = vmcb->save.cr2;
  1705. nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
  1706. nested_vmcb->save.rflags = vmcb->save.rflags;
  1707. nested_vmcb->save.rip = vmcb->save.rip;
  1708. nested_vmcb->save.rsp = vmcb->save.rsp;
  1709. nested_vmcb->save.rax = vmcb->save.rax;
  1710. nested_vmcb->save.dr7 = vmcb->save.dr7;
  1711. nested_vmcb->save.dr6 = vmcb->save.dr6;
  1712. nested_vmcb->save.cpl = vmcb->save.cpl;
  1713. nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
  1714. nested_vmcb->control.int_vector = vmcb->control.int_vector;
  1715. nested_vmcb->control.int_state = vmcb->control.int_state;
  1716. nested_vmcb->control.exit_code = vmcb->control.exit_code;
  1717. nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
  1718. nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
  1719. nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
  1720. nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
  1721. nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
  1722. nested_vmcb->control.next_rip = vmcb->control.next_rip;
  1723. /*
  1724. * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
  1725. * to make sure that we do not lose injected events. So check event_inj
  1726. * here and copy it to exit_int_info if it is valid.
  1727. * Exit_int_info and event_inj can't be both valid because the case
  1728. * below only happens on a VMRUN instruction intercept which has
  1729. * no valid exit_int_info set.
  1730. */
  1731. if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
  1732. struct vmcb_control_area *nc = &nested_vmcb->control;
  1733. nc->exit_int_info = vmcb->control.event_inj;
  1734. nc->exit_int_info_err = vmcb->control.event_inj_err;
  1735. }
  1736. nested_vmcb->control.tlb_ctl = 0;
  1737. nested_vmcb->control.event_inj = 0;
  1738. nested_vmcb->control.event_inj_err = 0;
  1739. /* We always set V_INTR_MASKING and remember the old value in hflags */
  1740. if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
  1741. nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
  1742. /* Restore the original control entries */
  1743. copy_vmcb_control_area(vmcb, hsave);
  1744. kvm_clear_exception_queue(&svm->vcpu);
  1745. kvm_clear_interrupt_queue(&svm->vcpu);
  1746. svm->nested.nested_cr3 = 0;
  1747. /* Restore selected save entries */
  1748. svm->vmcb->save.es = hsave->save.es;
  1749. svm->vmcb->save.cs = hsave->save.cs;
  1750. svm->vmcb->save.ss = hsave->save.ss;
  1751. svm->vmcb->save.ds = hsave->save.ds;
  1752. svm->vmcb->save.gdtr = hsave->save.gdtr;
  1753. svm->vmcb->save.idtr = hsave->save.idtr;
  1754. svm->vmcb->save.rflags = hsave->save.rflags;
  1755. svm_set_efer(&svm->vcpu, hsave->save.efer);
  1756. svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
  1757. svm_set_cr4(&svm->vcpu, hsave->save.cr4);
  1758. if (npt_enabled) {
  1759. svm->vmcb->save.cr3 = hsave->save.cr3;
  1760. svm->vcpu.arch.cr3 = hsave->save.cr3;
  1761. } else {
  1762. (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
  1763. }
  1764. kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
  1765. kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
  1766. kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
  1767. svm->vmcb->save.dr7 = 0;
  1768. svm->vmcb->save.cpl = 0;
  1769. svm->vmcb->control.exit_int_info = 0;
  1770. mark_all_dirty(svm->vmcb);
  1771. nested_svm_unmap(page);
  1772. nested_svm_uninit_mmu_context(&svm->vcpu);
  1773. kvm_mmu_reset_context(&svm->vcpu);
  1774. kvm_mmu_load(&svm->vcpu);
  1775. return 0;
  1776. }
  1777. static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
  1778. {
  1779. /*
  1780. * This function merges the msr permission bitmaps of kvm and the
  1781. * nested vmcb. It is omptimized in that it only merges the parts where
  1782. * the kvm msr permission bitmap may contain zero bits
  1783. */
  1784. int i;
  1785. if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
  1786. return true;
  1787. for (i = 0; i < MSRPM_OFFSETS; i++) {
  1788. u32 value, p;
  1789. u64 offset;
  1790. if (msrpm_offsets[i] == 0xffffffff)
  1791. break;
  1792. p = msrpm_offsets[i];
  1793. offset = svm->nested.vmcb_msrpm + (p * 4);
  1794. if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
  1795. return false;
  1796. svm->nested.msrpm[p] = svm->msrpm[p] | value;
  1797. }
  1798. svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
  1799. return true;
  1800. }
  1801. static bool nested_vmcb_checks(struct vmcb *vmcb)
  1802. {
  1803. if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
  1804. return false;
  1805. if (vmcb->control.asid == 0)
  1806. return false;
  1807. if (vmcb->control.nested_ctl && !npt_enabled)
  1808. return false;
  1809. return true;
  1810. }
  1811. static bool nested_svm_vmrun(struct vcpu_svm *svm)
  1812. {
  1813. struct vmcb *nested_vmcb;
  1814. struct vmcb *hsave = svm->nested.hsave;
  1815. struct vmcb *vmcb = svm->vmcb;
  1816. struct page *page;
  1817. u64 vmcb_gpa;
  1818. vmcb_gpa = svm->vmcb->save.rax;
  1819. nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
  1820. if (!nested_vmcb)
  1821. return false;
  1822. if (!nested_vmcb_checks(nested_vmcb)) {
  1823. nested_vmcb->control.exit_code = SVM_EXIT_ERR;
  1824. nested_vmcb->control.exit_code_hi = 0;
  1825. nested_vmcb->control.exit_info_1 = 0;
  1826. nested_vmcb->control.exit_info_2 = 0;
  1827. nested_svm_unmap(page);
  1828. return false;
  1829. }
  1830. trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
  1831. nested_vmcb->save.rip,
  1832. nested_vmcb->control.int_ctl,
  1833. nested_vmcb->control.event_inj,
  1834. nested_vmcb->control.nested_ctl);
  1835. trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
  1836. nested_vmcb->control.intercept_cr >> 16,
  1837. nested_vmcb->control.intercept_exceptions,
  1838. nested_vmcb->control.intercept);
  1839. /* Clear internal status */
  1840. kvm_clear_exception_queue(&svm->vcpu);
  1841. kvm_clear_interrupt_queue(&svm->vcpu);
  1842. /*
  1843. * Save the old vmcb, so we don't need to pick what we save, but can
  1844. * restore everything when a VMEXIT occurs
  1845. */
  1846. hsave->save.es = vmcb->save.es;
  1847. hsave->save.cs = vmcb->save.cs;
  1848. hsave->save.ss = vmcb->save.ss;
  1849. hsave->save.ds = vmcb->save.ds;
  1850. hsave->save.gdtr = vmcb->save.gdtr;
  1851. hsave->save.idtr = vmcb->save.idtr;
  1852. hsave->save.efer = svm->vcpu.arch.efer;
  1853. hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
  1854. hsave->save.cr4 = svm->vcpu.arch.cr4;
  1855. hsave->save.rflags = vmcb->save.rflags;
  1856. hsave->save.rip = kvm_rip_read(&svm->vcpu);
  1857. hsave->save.rsp = vmcb->save.rsp;
  1858. hsave->save.rax = vmcb->save.rax;
  1859. if (npt_enabled)
  1860. hsave->save.cr3 = vmcb->save.cr3;
  1861. else
  1862. hsave->save.cr3 = svm->vcpu.arch.cr3;
  1863. copy_vmcb_control_area(hsave, vmcb);
  1864. if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
  1865. svm->vcpu.arch.hflags |= HF_HIF_MASK;
  1866. else
  1867. svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
  1868. if (nested_vmcb->control.nested_ctl) {
  1869. kvm_mmu_unload(&svm->vcpu);
  1870. svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
  1871. nested_svm_init_mmu_context(&svm->vcpu);
  1872. }
  1873. /* Load the nested guest state */
  1874. svm->vmcb->save.es = nested_vmcb->save.es;
  1875. svm->vmcb->save.cs = nested_vmcb->save.cs;
  1876. svm->vmcb->save.ss = nested_vmcb->save.ss;
  1877. svm->vmcb->save.ds = nested_vmcb->save.ds;
  1878. svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
  1879. svm->vmcb->save.idtr = nested_vmcb->save.idtr;
  1880. svm->vmcb->save.rflags = nested_vmcb->save.rflags;
  1881. svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
  1882. svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
  1883. svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
  1884. if (npt_enabled) {
  1885. svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
  1886. svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
  1887. } else
  1888. (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
  1889. /* Guest paging mode is active - reset mmu */
  1890. kvm_mmu_reset_context(&svm->vcpu);
  1891. svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
  1892. kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
  1893. kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
  1894. kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
  1895. /* In case we don't even reach vcpu_run, the fields are not updated */
  1896. svm->vmcb->save.rax = nested_vmcb->save.rax;
  1897. svm->vmcb->save.rsp = nested_vmcb->save.rsp;
  1898. svm->vmcb->save.rip = nested_vmcb->save.rip;
  1899. svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
  1900. svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
  1901. svm->vmcb->save.cpl = nested_vmcb->save.cpl;
  1902. svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
  1903. svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
  1904. /* cache intercepts */
  1905. svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
  1906. svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
  1907. svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
  1908. svm->nested.intercept = nested_vmcb->control.intercept;
  1909. force_new_asid(&svm->vcpu);
  1910. svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
  1911. if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
  1912. svm->vcpu.arch.hflags |= HF_VINTR_MASK;
  1913. else
  1914. svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
  1915. if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
  1916. /* We only want the cr8 intercept bits of the guest */
  1917. clr_cr_intercept(svm, INTERCEPT_CR8_READ);
  1918. clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  1919. }
  1920. /* We don't want to see VMMCALLs from a nested guest */
  1921. clr_intercept(svm, INTERCEPT_VMMCALL);
  1922. svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
  1923. svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
  1924. svm->vmcb->control.int_state = nested_vmcb->control.int_state;
  1925. svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
  1926. svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
  1927. svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
  1928. nested_svm_unmap(page);
  1929. /* Enter Guest-Mode */
  1930. enter_guest_mode(&svm->vcpu);
  1931. /*
  1932. * Merge guest and host intercepts - must be called with vcpu in
  1933. * guest-mode to take affect here
  1934. */
  1935. recalc_intercepts(svm);
  1936. svm->nested.vmcb = vmcb_gpa;
  1937. enable_gif(svm);
  1938. mark_all_dirty(svm->vmcb);
  1939. return true;
  1940. }
  1941. static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
  1942. {
  1943. to_vmcb->save.fs = from_vmcb->save.fs;
  1944. to_vmcb->save.gs = from_vmcb->save.gs;
  1945. to_vmcb->save.tr = from_vmcb->save.tr;
  1946. to_vmcb->save.ldtr = from_vmcb->save.ldtr;
  1947. to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
  1948. to_vmcb->save.star = from_vmcb->save.star;
  1949. to_vmcb->save.lstar = from_vmcb->save.lstar;
  1950. to_vmcb->save.cstar = from_vmcb->save.cstar;
  1951. to_vmcb->save.sfmask = from_vmcb->save.sfmask;
  1952. to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
  1953. to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
  1954. to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
  1955. }
  1956. static int vmload_interception(struct vcpu_svm *svm)
  1957. {
  1958. struct vmcb *nested_vmcb;
  1959. struct page *page;
  1960. if (nested_svm_check_permissions(svm))
  1961. return 1;
  1962. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1963. skip_emulated_instruction(&svm->vcpu);
  1964. nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
  1965. if (!nested_vmcb)
  1966. return 1;
  1967. nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
  1968. nested_svm_unmap(page);
  1969. return 1;
  1970. }
  1971. static int vmsave_interception(struct vcpu_svm *svm)
  1972. {
  1973. struct vmcb *nested_vmcb;
  1974. struct page *page;
  1975. if (nested_svm_check_permissions(svm))
  1976. return 1;
  1977. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1978. skip_emulated_instruction(&svm->vcpu);
  1979. nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
  1980. if (!nested_vmcb)
  1981. return 1;
  1982. nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
  1983. nested_svm_unmap(page);
  1984. return 1;
  1985. }
  1986. static int vmrun_interception(struct vcpu_svm *svm)
  1987. {
  1988. if (nested_svm_check_permissions(svm))
  1989. return 1;
  1990. /* Save rip after vmrun instruction */
  1991. kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
  1992. if (!nested_svm_vmrun(svm))
  1993. return 1;
  1994. if (!nested_svm_vmrun_msrpm(svm))
  1995. goto failed;
  1996. return 1;
  1997. failed:
  1998. svm->vmcb->control.exit_code = SVM_EXIT_ERR;
  1999. svm->vmcb->control.exit_code_hi = 0;
  2000. svm->vmcb->control.exit_info_1 = 0;
  2001. svm->vmcb->control.exit_info_2 = 0;
  2002. nested_svm_vmexit(svm);
  2003. return 1;
  2004. }
  2005. static int stgi_interception(struct vcpu_svm *svm)
  2006. {
  2007. if (nested_svm_check_permissions(svm))
  2008. return 1;
  2009. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2010. skip_emulated_instruction(&svm->vcpu);
  2011. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  2012. enable_gif(svm);
  2013. return 1;
  2014. }
  2015. static int clgi_interception(struct vcpu_svm *svm)
  2016. {
  2017. if (nested_svm_check_permissions(svm))
  2018. return 1;
  2019. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2020. skip_emulated_instruction(&svm->vcpu);
  2021. disable_gif(svm);
  2022. /* After a CLGI no interrupts should come */
  2023. svm_clear_vintr(svm);
  2024. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  2025. mark_dirty(svm->vmcb, VMCB_INTR);
  2026. return 1;
  2027. }
  2028. static int invlpga_interception(struct vcpu_svm *svm)
  2029. {
  2030. struct kvm_vcpu *vcpu = &svm->vcpu;
  2031. trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
  2032. vcpu->arch.regs[VCPU_REGS_RAX]);
  2033. /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
  2034. kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
  2035. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2036. skip_emulated_instruction(&svm->vcpu);
  2037. return 1;
  2038. }
  2039. static int skinit_interception(struct vcpu_svm *svm)
  2040. {
  2041. trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
  2042. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  2043. return 1;
  2044. }
  2045. static int invalid_op_interception(struct vcpu_svm *svm)
  2046. {
  2047. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  2048. return 1;
  2049. }
  2050. static int task_switch_interception(struct vcpu_svm *svm)
  2051. {
  2052. u16 tss_selector;
  2053. int reason;
  2054. int int_type = svm->vmcb->control.exit_int_info &
  2055. SVM_EXITINTINFO_TYPE_MASK;
  2056. int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
  2057. uint32_t type =
  2058. svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
  2059. uint32_t idt_v =
  2060. svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
  2061. bool has_error_code = false;
  2062. u32 error_code = 0;
  2063. tss_selector = (u16)svm->vmcb->control.exit_info_1;
  2064. if (svm->vmcb->control.exit_info_2 &
  2065. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
  2066. reason = TASK_SWITCH_IRET;
  2067. else if (svm->vmcb->control.exit_info_2 &
  2068. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
  2069. reason = TASK_SWITCH_JMP;
  2070. else if (idt_v)
  2071. reason = TASK_SWITCH_GATE;
  2072. else
  2073. reason = TASK_SWITCH_CALL;
  2074. if (reason == TASK_SWITCH_GATE) {
  2075. switch (type) {
  2076. case SVM_EXITINTINFO_TYPE_NMI:
  2077. svm->vcpu.arch.nmi_injected = false;
  2078. break;
  2079. case SVM_EXITINTINFO_TYPE_EXEPT:
  2080. if (svm->vmcb->control.exit_info_2 &
  2081. (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
  2082. has_error_code = true;
  2083. error_code =
  2084. (u32)svm->vmcb->control.exit_info_2;
  2085. }
  2086. kvm_clear_exception_queue(&svm->vcpu);
  2087. break;
  2088. case SVM_EXITINTINFO_TYPE_INTR:
  2089. kvm_clear_interrupt_queue(&svm->vcpu);
  2090. break;
  2091. default:
  2092. break;
  2093. }
  2094. }
  2095. if (reason != TASK_SWITCH_GATE ||
  2096. int_type == SVM_EXITINTINFO_TYPE_SOFT ||
  2097. (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
  2098. (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
  2099. skip_emulated_instruction(&svm->vcpu);
  2100. if (kvm_task_switch(&svm->vcpu, tss_selector, reason,
  2101. has_error_code, error_code) == EMULATE_FAIL) {
  2102. svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  2103. svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  2104. svm->vcpu.run->internal.ndata = 0;
  2105. return 0;
  2106. }
  2107. return 1;
  2108. }
  2109. static int cpuid_interception(struct vcpu_svm *svm)
  2110. {
  2111. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  2112. kvm_emulate_cpuid(&svm->vcpu);
  2113. return 1;
  2114. }
  2115. static int iret_interception(struct vcpu_svm *svm)
  2116. {
  2117. ++svm->vcpu.stat.nmi_window_exits;
  2118. clr_intercept(svm, INTERCEPT_IRET);
  2119. svm->vcpu.arch.hflags |= HF_IRET_MASK;
  2120. return 1;
  2121. }
  2122. static int invlpg_interception(struct vcpu_svm *svm)
  2123. {
  2124. return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
  2125. }
  2126. static int emulate_on_interception(struct vcpu_svm *svm)
  2127. {
  2128. return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
  2129. }
  2130. static int cr0_write_interception(struct vcpu_svm *svm)
  2131. {
  2132. struct kvm_vcpu *vcpu = &svm->vcpu;
  2133. int r;
  2134. r = emulate_instruction(&svm->vcpu, 0, 0, 0);
  2135. if (svm->nested.vmexit_rip) {
  2136. kvm_register_write(vcpu, VCPU_REGS_RIP, svm->nested.vmexit_rip);
  2137. kvm_register_write(vcpu, VCPU_REGS_RSP, svm->nested.vmexit_rsp);
  2138. kvm_register_write(vcpu, VCPU_REGS_RAX, svm->nested.vmexit_rax);
  2139. svm->nested.vmexit_rip = 0;
  2140. }
  2141. return r == EMULATE_DONE;
  2142. }
  2143. static int cr8_write_interception(struct vcpu_svm *svm)
  2144. {
  2145. struct kvm_run *kvm_run = svm->vcpu.run;
  2146. u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
  2147. /* instruction emulation calls kvm_set_cr8() */
  2148. emulate_instruction(&svm->vcpu, 0, 0, 0);
  2149. if (irqchip_in_kernel(svm->vcpu.kvm)) {
  2150. clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  2151. return 1;
  2152. }
  2153. if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
  2154. return 1;
  2155. kvm_run->exit_reason = KVM_EXIT_SET_TPR;
  2156. return 0;
  2157. }
  2158. static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
  2159. {
  2160. struct vcpu_svm *svm = to_svm(vcpu);
  2161. switch (ecx) {
  2162. case MSR_IA32_TSC: {
  2163. struct vmcb *vmcb = get_host_vmcb(svm);
  2164. *data = vmcb->control.tsc_offset + native_read_tsc();
  2165. break;
  2166. }
  2167. case MSR_STAR:
  2168. *data = svm->vmcb->save.star;
  2169. break;
  2170. #ifdef CONFIG_X86_64
  2171. case MSR_LSTAR:
  2172. *data = svm->vmcb->save.lstar;
  2173. break;
  2174. case MSR_CSTAR:
  2175. *data = svm->vmcb->save.cstar;
  2176. break;
  2177. case MSR_KERNEL_GS_BASE:
  2178. *data = svm->vmcb->save.kernel_gs_base;
  2179. break;
  2180. case MSR_SYSCALL_MASK:
  2181. *data = svm->vmcb->save.sfmask;
  2182. break;
  2183. #endif
  2184. case MSR_IA32_SYSENTER_CS:
  2185. *data = svm->vmcb->save.sysenter_cs;
  2186. break;
  2187. case MSR_IA32_SYSENTER_EIP:
  2188. *data = svm->sysenter_eip;
  2189. break;
  2190. case MSR_IA32_SYSENTER_ESP:
  2191. *data = svm->sysenter_esp;
  2192. break;
  2193. /*
  2194. * Nobody will change the following 5 values in the VMCB so we can
  2195. * safely return them on rdmsr. They will always be 0 until LBRV is
  2196. * implemented.
  2197. */
  2198. case MSR_IA32_DEBUGCTLMSR:
  2199. *data = svm->vmcb->save.dbgctl;
  2200. break;
  2201. case MSR_IA32_LASTBRANCHFROMIP:
  2202. *data = svm->vmcb->save.br_from;
  2203. break;
  2204. case MSR_IA32_LASTBRANCHTOIP:
  2205. *data = svm->vmcb->save.br_to;
  2206. break;
  2207. case MSR_IA32_LASTINTFROMIP:
  2208. *data = svm->vmcb->save.last_excp_from;
  2209. break;
  2210. case MSR_IA32_LASTINTTOIP:
  2211. *data = svm->vmcb->save.last_excp_to;
  2212. break;
  2213. case MSR_VM_HSAVE_PA:
  2214. *data = svm->nested.hsave_msr;
  2215. break;
  2216. case MSR_VM_CR:
  2217. *data = svm->nested.vm_cr_msr;
  2218. break;
  2219. case MSR_IA32_UCODE_REV:
  2220. *data = 0x01000065;
  2221. break;
  2222. default:
  2223. return kvm_get_msr_common(vcpu, ecx, data);
  2224. }
  2225. return 0;
  2226. }
  2227. static int rdmsr_interception(struct vcpu_svm *svm)
  2228. {
  2229. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  2230. u64 data;
  2231. if (svm_get_msr(&svm->vcpu, ecx, &data)) {
  2232. trace_kvm_msr_read_ex(ecx);
  2233. kvm_inject_gp(&svm->vcpu, 0);
  2234. } else {
  2235. trace_kvm_msr_read(ecx, data);
  2236. svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
  2237. svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
  2238. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  2239. skip_emulated_instruction(&svm->vcpu);
  2240. }
  2241. return 1;
  2242. }
  2243. static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
  2244. {
  2245. struct vcpu_svm *svm = to_svm(vcpu);
  2246. int svm_dis, chg_mask;
  2247. if (data & ~SVM_VM_CR_VALID_MASK)
  2248. return 1;
  2249. chg_mask = SVM_VM_CR_VALID_MASK;
  2250. if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
  2251. chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
  2252. svm->nested.vm_cr_msr &= ~chg_mask;
  2253. svm->nested.vm_cr_msr |= (data & chg_mask);
  2254. svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
  2255. /* check for svm_disable while efer.svme is set */
  2256. if (svm_dis && (vcpu->arch.efer & EFER_SVME))
  2257. return 1;
  2258. return 0;
  2259. }
  2260. static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
  2261. {
  2262. struct vcpu_svm *svm = to_svm(vcpu);
  2263. switch (ecx) {
  2264. case MSR_IA32_TSC:
  2265. kvm_write_tsc(vcpu, data);
  2266. break;
  2267. case MSR_STAR:
  2268. svm->vmcb->save.star = data;
  2269. break;
  2270. #ifdef CONFIG_X86_64
  2271. case MSR_LSTAR:
  2272. svm->vmcb->save.lstar = data;
  2273. break;
  2274. case MSR_CSTAR:
  2275. svm->vmcb->save.cstar = data;
  2276. break;
  2277. case MSR_KERNEL_GS_BASE:
  2278. svm->vmcb->save.kernel_gs_base = data;
  2279. break;
  2280. case MSR_SYSCALL_MASK:
  2281. svm->vmcb->save.sfmask = data;
  2282. break;
  2283. #endif
  2284. case MSR_IA32_SYSENTER_CS:
  2285. svm->vmcb->save.sysenter_cs = data;
  2286. break;
  2287. case MSR_IA32_SYSENTER_EIP:
  2288. svm->sysenter_eip = data;
  2289. svm->vmcb->save.sysenter_eip = data;
  2290. break;
  2291. case MSR_IA32_SYSENTER_ESP:
  2292. svm->sysenter_esp = data;
  2293. svm->vmcb->save.sysenter_esp = data;
  2294. break;
  2295. case MSR_IA32_DEBUGCTLMSR:
  2296. if (!boot_cpu_has(X86_FEATURE_LBRV)) {
  2297. pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
  2298. __func__, data);
  2299. break;
  2300. }
  2301. if (data & DEBUGCTL_RESERVED_BITS)
  2302. return 1;
  2303. svm->vmcb->save.dbgctl = data;
  2304. mark_dirty(svm->vmcb, VMCB_LBR);
  2305. if (data & (1ULL<<0))
  2306. svm_enable_lbrv(svm);
  2307. else
  2308. svm_disable_lbrv(svm);
  2309. break;
  2310. case MSR_VM_HSAVE_PA:
  2311. svm->nested.hsave_msr = data;
  2312. break;
  2313. case MSR_VM_CR:
  2314. return svm_set_vm_cr(vcpu, data);
  2315. case MSR_VM_IGNNE:
  2316. pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
  2317. break;
  2318. default:
  2319. return kvm_set_msr_common(vcpu, ecx, data);
  2320. }
  2321. return 0;
  2322. }
  2323. static int wrmsr_interception(struct vcpu_svm *svm)
  2324. {
  2325. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  2326. u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
  2327. | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  2328. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  2329. if (svm_set_msr(&svm->vcpu, ecx, data)) {
  2330. trace_kvm_msr_write_ex(ecx, data);
  2331. kvm_inject_gp(&svm->vcpu, 0);
  2332. } else {
  2333. trace_kvm_msr_write(ecx, data);
  2334. skip_emulated_instruction(&svm->vcpu);
  2335. }
  2336. return 1;
  2337. }
  2338. static int msr_interception(struct vcpu_svm *svm)
  2339. {
  2340. if (svm->vmcb->control.exit_info_1)
  2341. return wrmsr_interception(svm);
  2342. else
  2343. return rdmsr_interception(svm);
  2344. }
  2345. static int interrupt_window_interception(struct vcpu_svm *svm)
  2346. {
  2347. struct kvm_run *kvm_run = svm->vcpu.run;
  2348. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  2349. svm_clear_vintr(svm);
  2350. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  2351. mark_dirty(svm->vmcb, VMCB_INTR);
  2352. /*
  2353. * If the user space waits to inject interrupts, exit as soon as
  2354. * possible
  2355. */
  2356. if (!irqchip_in_kernel(svm->vcpu.kvm) &&
  2357. kvm_run->request_interrupt_window &&
  2358. !kvm_cpu_has_interrupt(&svm->vcpu)) {
  2359. ++svm->vcpu.stat.irq_window_exits;
  2360. kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  2361. return 0;
  2362. }
  2363. return 1;
  2364. }
  2365. static int pause_interception(struct vcpu_svm *svm)
  2366. {
  2367. kvm_vcpu_on_spin(&(svm->vcpu));
  2368. return 1;
  2369. }
  2370. static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
  2371. [SVM_EXIT_READ_CR0] = emulate_on_interception,
  2372. [SVM_EXIT_READ_CR3] = emulate_on_interception,
  2373. [SVM_EXIT_READ_CR4] = emulate_on_interception,
  2374. [SVM_EXIT_READ_CR8] = emulate_on_interception,
  2375. [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
  2376. [SVM_EXIT_WRITE_CR0] = cr0_write_interception,
  2377. [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
  2378. [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
  2379. [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
  2380. [SVM_EXIT_READ_DR0] = emulate_on_interception,
  2381. [SVM_EXIT_READ_DR1] = emulate_on_interception,
  2382. [SVM_EXIT_READ_DR2] = emulate_on_interception,
  2383. [SVM_EXIT_READ_DR3] = emulate_on_interception,
  2384. [SVM_EXIT_READ_DR4] = emulate_on_interception,
  2385. [SVM_EXIT_READ_DR5] = emulate_on_interception,
  2386. [SVM_EXIT_READ_DR6] = emulate_on_interception,
  2387. [SVM_EXIT_READ_DR7] = emulate_on_interception,
  2388. [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
  2389. [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
  2390. [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
  2391. [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
  2392. [SVM_EXIT_WRITE_DR4] = emulate_on_interception,
  2393. [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
  2394. [SVM_EXIT_WRITE_DR6] = emulate_on_interception,
  2395. [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
  2396. [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
  2397. [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
  2398. [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
  2399. [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
  2400. [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
  2401. [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
  2402. [SVM_EXIT_INTR] = intr_interception,
  2403. [SVM_EXIT_NMI] = nmi_interception,
  2404. [SVM_EXIT_SMI] = nop_on_interception,
  2405. [SVM_EXIT_INIT] = nop_on_interception,
  2406. [SVM_EXIT_VINTR] = interrupt_window_interception,
  2407. [SVM_EXIT_CPUID] = cpuid_interception,
  2408. [SVM_EXIT_IRET] = iret_interception,
  2409. [SVM_EXIT_INVD] = emulate_on_interception,
  2410. [SVM_EXIT_PAUSE] = pause_interception,
  2411. [SVM_EXIT_HLT] = halt_interception,
  2412. [SVM_EXIT_INVLPG] = invlpg_interception,
  2413. [SVM_EXIT_INVLPGA] = invlpga_interception,
  2414. [SVM_EXIT_IOIO] = io_interception,
  2415. [SVM_EXIT_MSR] = msr_interception,
  2416. [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
  2417. [SVM_EXIT_SHUTDOWN] = shutdown_interception,
  2418. [SVM_EXIT_VMRUN] = vmrun_interception,
  2419. [SVM_EXIT_VMMCALL] = vmmcall_interception,
  2420. [SVM_EXIT_VMLOAD] = vmload_interception,
  2421. [SVM_EXIT_VMSAVE] = vmsave_interception,
  2422. [SVM_EXIT_STGI] = stgi_interception,
  2423. [SVM_EXIT_CLGI] = clgi_interception,
  2424. [SVM_EXIT_SKINIT] = skinit_interception,
  2425. [SVM_EXIT_WBINVD] = emulate_on_interception,
  2426. [SVM_EXIT_MONITOR] = invalid_op_interception,
  2427. [SVM_EXIT_MWAIT] = invalid_op_interception,
  2428. [SVM_EXIT_NPF] = pf_interception,
  2429. };
  2430. void dump_vmcb(struct kvm_vcpu *vcpu)
  2431. {
  2432. struct vcpu_svm *svm = to_svm(vcpu);
  2433. struct vmcb_control_area *control = &svm->vmcb->control;
  2434. struct vmcb_save_area *save = &svm->vmcb->save;
  2435. pr_err("VMCB Control Area:\n");
  2436. pr_err("cr_read: %04x\n", control->intercept_cr & 0xffff);
  2437. pr_err("cr_write: %04x\n", control->intercept_cr >> 16);
  2438. pr_err("dr_read: %04x\n", control->intercept_dr & 0xffff);
  2439. pr_err("dr_write: %04x\n", control->intercept_dr >> 16);
  2440. pr_err("exceptions: %08x\n", control->intercept_exceptions);
  2441. pr_err("intercepts: %016llx\n", control->intercept);
  2442. pr_err("pause filter count: %d\n", control->pause_filter_count);
  2443. pr_err("iopm_base_pa: %016llx\n", control->iopm_base_pa);
  2444. pr_err("msrpm_base_pa: %016llx\n", control->msrpm_base_pa);
  2445. pr_err("tsc_offset: %016llx\n", control->tsc_offset);
  2446. pr_err("asid: %d\n", control->asid);
  2447. pr_err("tlb_ctl: %d\n", control->tlb_ctl);
  2448. pr_err("int_ctl: %08x\n", control->int_ctl);
  2449. pr_err("int_vector: %08x\n", control->int_vector);
  2450. pr_err("int_state: %08x\n", control->int_state);
  2451. pr_err("exit_code: %08x\n", control->exit_code);
  2452. pr_err("exit_info1: %016llx\n", control->exit_info_1);
  2453. pr_err("exit_info2: %016llx\n", control->exit_info_2);
  2454. pr_err("exit_int_info: %08x\n", control->exit_int_info);
  2455. pr_err("exit_int_info_err: %08x\n", control->exit_int_info_err);
  2456. pr_err("nested_ctl: %lld\n", control->nested_ctl);
  2457. pr_err("nested_cr3: %016llx\n", control->nested_cr3);
  2458. pr_err("event_inj: %08x\n", control->event_inj);
  2459. pr_err("event_inj_err: %08x\n", control->event_inj_err);
  2460. pr_err("lbr_ctl: %lld\n", control->lbr_ctl);
  2461. pr_err("next_rip: %016llx\n", control->next_rip);
  2462. pr_err("VMCB State Save Area:\n");
  2463. pr_err("es: s: %04x a: %04x l: %08x b: %016llx\n",
  2464. save->es.selector, save->es.attrib,
  2465. save->es.limit, save->es.base);
  2466. pr_err("cs: s: %04x a: %04x l: %08x b: %016llx\n",
  2467. save->cs.selector, save->cs.attrib,
  2468. save->cs.limit, save->cs.base);
  2469. pr_err("ss: s: %04x a: %04x l: %08x b: %016llx\n",
  2470. save->ss.selector, save->ss.attrib,
  2471. save->ss.limit, save->ss.base);
  2472. pr_err("ds: s: %04x a: %04x l: %08x b: %016llx\n",
  2473. save->ds.selector, save->ds.attrib,
  2474. save->ds.limit, save->ds.base);
  2475. pr_err("fs: s: %04x a: %04x l: %08x b: %016llx\n",
  2476. save->fs.selector, save->fs.attrib,
  2477. save->fs.limit, save->fs.base);
  2478. pr_err("gs: s: %04x a: %04x l: %08x b: %016llx\n",
  2479. save->gs.selector, save->gs.attrib,
  2480. save->gs.limit, save->gs.base);
  2481. pr_err("gdtr: s: %04x a: %04x l: %08x b: %016llx\n",
  2482. save->gdtr.selector, save->gdtr.attrib,
  2483. save->gdtr.limit, save->gdtr.base);
  2484. pr_err("ldtr: s: %04x a: %04x l: %08x b: %016llx\n",
  2485. save->ldtr.selector, save->ldtr.attrib,
  2486. save->ldtr.limit, save->ldtr.base);
  2487. pr_err("idtr: s: %04x a: %04x l: %08x b: %016llx\n",
  2488. save->idtr.selector, save->idtr.attrib,
  2489. save->idtr.limit, save->idtr.base);
  2490. pr_err("tr: s: %04x a: %04x l: %08x b: %016llx\n",
  2491. save->tr.selector, save->tr.attrib,
  2492. save->tr.limit, save->tr.base);
  2493. pr_err("cpl: %d efer: %016llx\n",
  2494. save->cpl, save->efer);
  2495. pr_err("cr0: %016llx cr2: %016llx\n",
  2496. save->cr0, save->cr2);
  2497. pr_err("cr3: %016llx cr4: %016llx\n",
  2498. save->cr3, save->cr4);
  2499. pr_err("dr6: %016llx dr7: %016llx\n",
  2500. save->dr6, save->dr7);
  2501. pr_err("rip: %016llx rflags: %016llx\n",
  2502. save->rip, save->rflags);
  2503. pr_err("rsp: %016llx rax: %016llx\n",
  2504. save->rsp, save->rax);
  2505. pr_err("star: %016llx lstar: %016llx\n",
  2506. save->star, save->lstar);
  2507. pr_err("cstar: %016llx sfmask: %016llx\n",
  2508. save->cstar, save->sfmask);
  2509. pr_err("kernel_gs_base: %016llx sysenter_cs: %016llx\n",
  2510. save->kernel_gs_base, save->sysenter_cs);
  2511. pr_err("sysenter_esp: %016llx sysenter_eip: %016llx\n",
  2512. save->sysenter_esp, save->sysenter_eip);
  2513. pr_err("gpat: %016llx dbgctl: %016llx\n",
  2514. save->g_pat, save->dbgctl);
  2515. pr_err("br_from: %016llx br_to: %016llx\n",
  2516. save->br_from, save->br_to);
  2517. pr_err("excp_from: %016llx excp_to: %016llx\n",
  2518. save->last_excp_from, save->last_excp_to);
  2519. }
  2520. static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
  2521. {
  2522. struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
  2523. *info1 = control->exit_info_1;
  2524. *info2 = control->exit_info_2;
  2525. }
  2526. static int handle_exit(struct kvm_vcpu *vcpu)
  2527. {
  2528. struct vcpu_svm *svm = to_svm(vcpu);
  2529. struct kvm_run *kvm_run = vcpu->run;
  2530. u32 exit_code = svm->vmcb->control.exit_code;
  2531. trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
  2532. if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
  2533. vcpu->arch.cr0 = svm->vmcb->save.cr0;
  2534. if (npt_enabled)
  2535. vcpu->arch.cr3 = svm->vmcb->save.cr3;
  2536. if (unlikely(svm->nested.exit_required)) {
  2537. nested_svm_vmexit(svm);
  2538. svm->nested.exit_required = false;
  2539. return 1;
  2540. }
  2541. if (is_guest_mode(vcpu)) {
  2542. int vmexit;
  2543. trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
  2544. svm->vmcb->control.exit_info_1,
  2545. svm->vmcb->control.exit_info_2,
  2546. svm->vmcb->control.exit_int_info,
  2547. svm->vmcb->control.exit_int_info_err);
  2548. vmexit = nested_svm_exit_special(svm);
  2549. if (vmexit == NESTED_EXIT_CONTINUE)
  2550. vmexit = nested_svm_exit_handled(svm);
  2551. if (vmexit == NESTED_EXIT_DONE)
  2552. return 1;
  2553. }
  2554. svm_complete_interrupts(svm);
  2555. if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
  2556. kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  2557. kvm_run->fail_entry.hardware_entry_failure_reason
  2558. = svm->vmcb->control.exit_code;
  2559. pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
  2560. dump_vmcb(vcpu);
  2561. return 0;
  2562. }
  2563. if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
  2564. exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
  2565. exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
  2566. exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
  2567. printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
  2568. "exit_code 0x%x\n",
  2569. __func__, svm->vmcb->control.exit_int_info,
  2570. exit_code);
  2571. if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
  2572. || !svm_exit_handlers[exit_code]) {
  2573. kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
  2574. kvm_run->hw.hardware_exit_reason = exit_code;
  2575. return 0;
  2576. }
  2577. return svm_exit_handlers[exit_code](svm);
  2578. }
  2579. static void reload_tss(struct kvm_vcpu *vcpu)
  2580. {
  2581. int cpu = raw_smp_processor_id();
  2582. struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
  2583. sd->tss_desc->type = 9; /* available 32/64-bit TSS */
  2584. load_TR_desc();
  2585. }
  2586. static void pre_svm_run(struct vcpu_svm *svm)
  2587. {
  2588. int cpu = raw_smp_processor_id();
  2589. struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
  2590. svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
  2591. /* FIXME: handle wraparound of asid_generation */
  2592. if (svm->asid_generation != sd->asid_generation)
  2593. new_asid(svm, sd);
  2594. }
  2595. static void svm_inject_nmi(struct kvm_vcpu *vcpu)
  2596. {
  2597. struct vcpu_svm *svm = to_svm(vcpu);
  2598. svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
  2599. vcpu->arch.hflags |= HF_NMI_MASK;
  2600. set_intercept(svm, INTERCEPT_IRET);
  2601. ++vcpu->stat.nmi_injections;
  2602. }
  2603. static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
  2604. {
  2605. struct vmcb_control_area *control;
  2606. control = &svm->vmcb->control;
  2607. control->int_vector = irq;
  2608. control->int_ctl &= ~V_INTR_PRIO_MASK;
  2609. control->int_ctl |= V_IRQ_MASK |
  2610. ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
  2611. mark_dirty(svm->vmcb, VMCB_INTR);
  2612. }
  2613. static void svm_set_irq(struct kvm_vcpu *vcpu)
  2614. {
  2615. struct vcpu_svm *svm = to_svm(vcpu);
  2616. BUG_ON(!(gif_set(svm)));
  2617. trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
  2618. ++vcpu->stat.irq_injections;
  2619. svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
  2620. SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
  2621. }
  2622. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  2623. {
  2624. struct vcpu_svm *svm = to_svm(vcpu);
  2625. if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
  2626. return;
  2627. if (irr == -1)
  2628. return;
  2629. if (tpr >= irr)
  2630. set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  2631. }
  2632. static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
  2633. {
  2634. struct vcpu_svm *svm = to_svm(vcpu);
  2635. struct vmcb *vmcb = svm->vmcb;
  2636. int ret;
  2637. ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
  2638. !(svm->vcpu.arch.hflags & HF_NMI_MASK);
  2639. ret = ret && gif_set(svm) && nested_svm_nmi(svm);
  2640. return ret;
  2641. }
  2642. static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
  2643. {
  2644. struct vcpu_svm *svm = to_svm(vcpu);
  2645. return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
  2646. }
  2647. static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  2648. {
  2649. struct vcpu_svm *svm = to_svm(vcpu);
  2650. if (masked) {
  2651. svm->vcpu.arch.hflags |= HF_NMI_MASK;
  2652. set_intercept(svm, INTERCEPT_IRET);
  2653. } else {
  2654. svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
  2655. clr_intercept(svm, INTERCEPT_IRET);
  2656. }
  2657. }
  2658. static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
  2659. {
  2660. struct vcpu_svm *svm = to_svm(vcpu);
  2661. struct vmcb *vmcb = svm->vmcb;
  2662. int ret;
  2663. if (!gif_set(svm) ||
  2664. (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
  2665. return 0;
  2666. ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
  2667. if (is_guest_mode(vcpu))
  2668. return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
  2669. return ret;
  2670. }
  2671. static void enable_irq_window(struct kvm_vcpu *vcpu)
  2672. {
  2673. struct vcpu_svm *svm = to_svm(vcpu);
  2674. /*
  2675. * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
  2676. * 1, because that's a separate STGI/VMRUN intercept. The next time we
  2677. * get that intercept, this function will be called again though and
  2678. * we'll get the vintr intercept.
  2679. */
  2680. if (gif_set(svm) && nested_svm_intr(svm)) {
  2681. svm_set_vintr(svm);
  2682. svm_inject_irq(svm, 0x0);
  2683. }
  2684. }
  2685. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  2686. {
  2687. struct vcpu_svm *svm = to_svm(vcpu);
  2688. if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
  2689. == HF_NMI_MASK)
  2690. return; /* IRET will cause a vm exit */
  2691. /*
  2692. * Something prevents NMI from been injected. Single step over possible
  2693. * problem (IRET or exception injection or interrupt shadow)
  2694. */
  2695. svm->nmi_singlestep = true;
  2696. svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
  2697. update_db_intercept(vcpu);
  2698. }
  2699. static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
  2700. {
  2701. return 0;
  2702. }
  2703. static void svm_flush_tlb(struct kvm_vcpu *vcpu)
  2704. {
  2705. force_new_asid(vcpu);
  2706. }
  2707. static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
  2708. {
  2709. }
  2710. static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
  2711. {
  2712. struct vcpu_svm *svm = to_svm(vcpu);
  2713. if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
  2714. return;
  2715. if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
  2716. int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
  2717. kvm_set_cr8(vcpu, cr8);
  2718. }
  2719. }
  2720. static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
  2721. {
  2722. struct vcpu_svm *svm = to_svm(vcpu);
  2723. u64 cr8;
  2724. if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
  2725. return;
  2726. cr8 = kvm_get_cr8(vcpu);
  2727. svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
  2728. svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
  2729. }
  2730. static void svm_complete_interrupts(struct vcpu_svm *svm)
  2731. {
  2732. u8 vector;
  2733. int type;
  2734. u32 exitintinfo = svm->vmcb->control.exit_int_info;
  2735. unsigned int3_injected = svm->int3_injected;
  2736. svm->int3_injected = 0;
  2737. if (svm->vcpu.arch.hflags & HF_IRET_MASK) {
  2738. svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
  2739. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  2740. }
  2741. svm->vcpu.arch.nmi_injected = false;
  2742. kvm_clear_exception_queue(&svm->vcpu);
  2743. kvm_clear_interrupt_queue(&svm->vcpu);
  2744. if (!(exitintinfo & SVM_EXITINTINFO_VALID))
  2745. return;
  2746. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  2747. vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
  2748. type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
  2749. switch (type) {
  2750. case SVM_EXITINTINFO_TYPE_NMI:
  2751. svm->vcpu.arch.nmi_injected = true;
  2752. break;
  2753. case SVM_EXITINTINFO_TYPE_EXEPT:
  2754. /*
  2755. * In case of software exceptions, do not reinject the vector,
  2756. * but re-execute the instruction instead. Rewind RIP first
  2757. * if we emulated INT3 before.
  2758. */
  2759. if (kvm_exception_is_soft(vector)) {
  2760. if (vector == BP_VECTOR && int3_injected &&
  2761. kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
  2762. kvm_rip_write(&svm->vcpu,
  2763. kvm_rip_read(&svm->vcpu) -
  2764. int3_injected);
  2765. break;
  2766. }
  2767. if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
  2768. u32 err = svm->vmcb->control.exit_int_info_err;
  2769. kvm_requeue_exception_e(&svm->vcpu, vector, err);
  2770. } else
  2771. kvm_requeue_exception(&svm->vcpu, vector);
  2772. break;
  2773. case SVM_EXITINTINFO_TYPE_INTR:
  2774. kvm_queue_interrupt(&svm->vcpu, vector, false);
  2775. break;
  2776. default:
  2777. break;
  2778. }
  2779. }
  2780. static void svm_cancel_injection(struct kvm_vcpu *vcpu)
  2781. {
  2782. struct vcpu_svm *svm = to_svm(vcpu);
  2783. struct vmcb_control_area *control = &svm->vmcb->control;
  2784. control->exit_int_info = control->event_inj;
  2785. control->exit_int_info_err = control->event_inj_err;
  2786. control->event_inj = 0;
  2787. svm_complete_interrupts(svm);
  2788. }
  2789. #ifdef CONFIG_X86_64
  2790. #define R "r"
  2791. #else
  2792. #define R "e"
  2793. #endif
  2794. static void svm_vcpu_run(struct kvm_vcpu *vcpu)
  2795. {
  2796. struct vcpu_svm *svm = to_svm(vcpu);
  2797. svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
  2798. svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
  2799. svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
  2800. /*
  2801. * A vmexit emulation is required before the vcpu can be executed
  2802. * again.
  2803. */
  2804. if (unlikely(svm->nested.exit_required))
  2805. return;
  2806. pre_svm_run(svm);
  2807. sync_lapic_to_cr8(vcpu);
  2808. svm->vmcb->save.cr2 = vcpu->arch.cr2;
  2809. clgi();
  2810. local_irq_enable();
  2811. asm volatile (
  2812. "push %%"R"bp; \n\t"
  2813. "mov %c[rbx](%[svm]), %%"R"bx \n\t"
  2814. "mov %c[rcx](%[svm]), %%"R"cx \n\t"
  2815. "mov %c[rdx](%[svm]), %%"R"dx \n\t"
  2816. "mov %c[rsi](%[svm]), %%"R"si \n\t"
  2817. "mov %c[rdi](%[svm]), %%"R"di \n\t"
  2818. "mov %c[rbp](%[svm]), %%"R"bp \n\t"
  2819. #ifdef CONFIG_X86_64
  2820. "mov %c[r8](%[svm]), %%r8 \n\t"
  2821. "mov %c[r9](%[svm]), %%r9 \n\t"
  2822. "mov %c[r10](%[svm]), %%r10 \n\t"
  2823. "mov %c[r11](%[svm]), %%r11 \n\t"
  2824. "mov %c[r12](%[svm]), %%r12 \n\t"
  2825. "mov %c[r13](%[svm]), %%r13 \n\t"
  2826. "mov %c[r14](%[svm]), %%r14 \n\t"
  2827. "mov %c[r15](%[svm]), %%r15 \n\t"
  2828. #endif
  2829. /* Enter guest mode */
  2830. "push %%"R"ax \n\t"
  2831. "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
  2832. __ex(SVM_VMLOAD) "\n\t"
  2833. __ex(SVM_VMRUN) "\n\t"
  2834. __ex(SVM_VMSAVE) "\n\t"
  2835. "pop %%"R"ax \n\t"
  2836. /* Save guest registers, load host registers */
  2837. "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
  2838. "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
  2839. "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
  2840. "mov %%"R"si, %c[rsi](%[svm]) \n\t"
  2841. "mov %%"R"di, %c[rdi](%[svm]) \n\t"
  2842. "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
  2843. #ifdef CONFIG_X86_64
  2844. "mov %%r8, %c[r8](%[svm]) \n\t"
  2845. "mov %%r9, %c[r9](%[svm]) \n\t"
  2846. "mov %%r10, %c[r10](%[svm]) \n\t"
  2847. "mov %%r11, %c[r11](%[svm]) \n\t"
  2848. "mov %%r12, %c[r12](%[svm]) \n\t"
  2849. "mov %%r13, %c[r13](%[svm]) \n\t"
  2850. "mov %%r14, %c[r14](%[svm]) \n\t"
  2851. "mov %%r15, %c[r15](%[svm]) \n\t"
  2852. #endif
  2853. "pop %%"R"bp"
  2854. :
  2855. : [svm]"a"(svm),
  2856. [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
  2857. [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
  2858. [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
  2859. [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
  2860. [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
  2861. [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
  2862. [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
  2863. #ifdef CONFIG_X86_64
  2864. , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
  2865. [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
  2866. [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
  2867. [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
  2868. [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
  2869. [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
  2870. [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
  2871. [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
  2872. #endif
  2873. : "cc", "memory"
  2874. , R"bx", R"cx", R"dx", R"si", R"di"
  2875. #ifdef CONFIG_X86_64
  2876. , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
  2877. #endif
  2878. );
  2879. #ifdef CONFIG_X86_64
  2880. wrmsrl(MSR_GS_BASE, svm->host.gs_base);
  2881. #else
  2882. loadsegment(fs, svm->host.fs);
  2883. #endif
  2884. reload_tss(vcpu);
  2885. local_irq_disable();
  2886. stgi();
  2887. vcpu->arch.cr2 = svm->vmcb->save.cr2;
  2888. vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
  2889. vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
  2890. vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
  2891. sync_cr8_to_lapic(vcpu);
  2892. svm->next_rip = 0;
  2893. /* if exit due to PF check for async PF */
  2894. if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
  2895. svm->apf_reason = kvm_read_and_reset_pf_reason();
  2896. if (npt_enabled) {
  2897. vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
  2898. vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
  2899. }
  2900. /*
  2901. * We need to handle MC intercepts here before the vcpu has a chance to
  2902. * change the physical cpu
  2903. */
  2904. if (unlikely(svm->vmcb->control.exit_code ==
  2905. SVM_EXIT_EXCP_BASE + MC_VECTOR))
  2906. svm_handle_mce(svm);
  2907. mark_all_clean(svm->vmcb);
  2908. }
  2909. #undef R
  2910. static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  2911. {
  2912. struct vcpu_svm *svm = to_svm(vcpu);
  2913. svm->vmcb->save.cr3 = root;
  2914. mark_dirty(svm->vmcb, VMCB_CR);
  2915. force_new_asid(vcpu);
  2916. }
  2917. static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  2918. {
  2919. struct vcpu_svm *svm = to_svm(vcpu);
  2920. svm->vmcb->control.nested_cr3 = root;
  2921. mark_dirty(svm->vmcb, VMCB_NPT);
  2922. /* Also sync guest cr3 here in case we live migrate */
  2923. svm->vmcb->save.cr3 = vcpu->arch.cr3;
  2924. mark_dirty(svm->vmcb, VMCB_CR);
  2925. force_new_asid(vcpu);
  2926. }
  2927. static int is_disabled(void)
  2928. {
  2929. u64 vm_cr;
  2930. rdmsrl(MSR_VM_CR, vm_cr);
  2931. if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
  2932. return 1;
  2933. return 0;
  2934. }
  2935. static void
  2936. svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  2937. {
  2938. /*
  2939. * Patch in the VMMCALL instruction:
  2940. */
  2941. hypercall[0] = 0x0f;
  2942. hypercall[1] = 0x01;
  2943. hypercall[2] = 0xd9;
  2944. }
  2945. static void svm_check_processor_compat(void *rtn)
  2946. {
  2947. *(int *)rtn = 0;
  2948. }
  2949. static bool svm_cpu_has_accelerated_tpr(void)
  2950. {
  2951. return false;
  2952. }
  2953. static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  2954. {
  2955. return 0;
  2956. }
  2957. static void svm_cpuid_update(struct kvm_vcpu *vcpu)
  2958. {
  2959. }
  2960. static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
  2961. {
  2962. switch (func) {
  2963. case 0x00000001:
  2964. /* Mask out xsave bit as long as it is not supported by SVM */
  2965. entry->ecx &= ~(bit(X86_FEATURE_XSAVE));
  2966. break;
  2967. case 0x80000001:
  2968. if (nested)
  2969. entry->ecx |= (1 << 2); /* Set SVM bit */
  2970. break;
  2971. case 0x8000000A:
  2972. entry->eax = 1; /* SVM revision 1 */
  2973. entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
  2974. ASID emulation to nested SVM */
  2975. entry->ecx = 0; /* Reserved */
  2976. entry->edx = 0; /* Per default do not support any
  2977. additional features */
  2978. /* Support next_rip if host supports it */
  2979. if (boot_cpu_has(X86_FEATURE_NRIPS))
  2980. entry->edx |= SVM_FEATURE_NRIP;
  2981. /* Support NPT for the guest if enabled */
  2982. if (npt_enabled)
  2983. entry->edx |= SVM_FEATURE_NPT;
  2984. break;
  2985. }
  2986. }
  2987. static const struct trace_print_flags svm_exit_reasons_str[] = {
  2988. { SVM_EXIT_READ_CR0, "read_cr0" },
  2989. { SVM_EXIT_READ_CR3, "read_cr3" },
  2990. { SVM_EXIT_READ_CR4, "read_cr4" },
  2991. { SVM_EXIT_READ_CR8, "read_cr8" },
  2992. { SVM_EXIT_WRITE_CR0, "write_cr0" },
  2993. { SVM_EXIT_WRITE_CR3, "write_cr3" },
  2994. { SVM_EXIT_WRITE_CR4, "write_cr4" },
  2995. { SVM_EXIT_WRITE_CR8, "write_cr8" },
  2996. { SVM_EXIT_READ_DR0, "read_dr0" },
  2997. { SVM_EXIT_READ_DR1, "read_dr1" },
  2998. { SVM_EXIT_READ_DR2, "read_dr2" },
  2999. { SVM_EXIT_READ_DR3, "read_dr3" },
  3000. { SVM_EXIT_WRITE_DR0, "write_dr0" },
  3001. { SVM_EXIT_WRITE_DR1, "write_dr1" },
  3002. { SVM_EXIT_WRITE_DR2, "write_dr2" },
  3003. { SVM_EXIT_WRITE_DR3, "write_dr3" },
  3004. { SVM_EXIT_WRITE_DR5, "write_dr5" },
  3005. { SVM_EXIT_WRITE_DR7, "write_dr7" },
  3006. { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
  3007. { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
  3008. { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
  3009. { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
  3010. { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
  3011. { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
  3012. { SVM_EXIT_INTR, "interrupt" },
  3013. { SVM_EXIT_NMI, "nmi" },
  3014. { SVM_EXIT_SMI, "smi" },
  3015. { SVM_EXIT_INIT, "init" },
  3016. { SVM_EXIT_VINTR, "vintr" },
  3017. { SVM_EXIT_CPUID, "cpuid" },
  3018. { SVM_EXIT_INVD, "invd" },
  3019. { SVM_EXIT_HLT, "hlt" },
  3020. { SVM_EXIT_INVLPG, "invlpg" },
  3021. { SVM_EXIT_INVLPGA, "invlpga" },
  3022. { SVM_EXIT_IOIO, "io" },
  3023. { SVM_EXIT_MSR, "msr" },
  3024. { SVM_EXIT_TASK_SWITCH, "task_switch" },
  3025. { SVM_EXIT_SHUTDOWN, "shutdown" },
  3026. { SVM_EXIT_VMRUN, "vmrun" },
  3027. { SVM_EXIT_VMMCALL, "hypercall" },
  3028. { SVM_EXIT_VMLOAD, "vmload" },
  3029. { SVM_EXIT_VMSAVE, "vmsave" },
  3030. { SVM_EXIT_STGI, "stgi" },
  3031. { SVM_EXIT_CLGI, "clgi" },
  3032. { SVM_EXIT_SKINIT, "skinit" },
  3033. { SVM_EXIT_WBINVD, "wbinvd" },
  3034. { SVM_EXIT_MONITOR, "monitor" },
  3035. { SVM_EXIT_MWAIT, "mwait" },
  3036. { SVM_EXIT_NPF, "npf" },
  3037. { -1, NULL }
  3038. };
  3039. static int svm_get_lpage_level(void)
  3040. {
  3041. return PT_PDPE_LEVEL;
  3042. }
  3043. static bool svm_rdtscp_supported(void)
  3044. {
  3045. return false;
  3046. }
  3047. static bool svm_has_wbinvd_exit(void)
  3048. {
  3049. return true;
  3050. }
  3051. static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
  3052. {
  3053. struct vcpu_svm *svm = to_svm(vcpu);
  3054. set_exception_intercept(svm, NM_VECTOR);
  3055. update_cr0_intercept(svm);
  3056. }
  3057. static struct kvm_x86_ops svm_x86_ops = {
  3058. .cpu_has_kvm_support = has_svm,
  3059. .disabled_by_bios = is_disabled,
  3060. .hardware_setup = svm_hardware_setup,
  3061. .hardware_unsetup = svm_hardware_unsetup,
  3062. .check_processor_compatibility = svm_check_processor_compat,
  3063. .hardware_enable = svm_hardware_enable,
  3064. .hardware_disable = svm_hardware_disable,
  3065. .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
  3066. .vcpu_create = svm_create_vcpu,
  3067. .vcpu_free = svm_free_vcpu,
  3068. .vcpu_reset = svm_vcpu_reset,
  3069. .prepare_guest_switch = svm_prepare_guest_switch,
  3070. .vcpu_load = svm_vcpu_load,
  3071. .vcpu_put = svm_vcpu_put,
  3072. .set_guest_debug = svm_guest_debug,
  3073. .get_msr = svm_get_msr,
  3074. .set_msr = svm_set_msr,
  3075. .get_segment_base = svm_get_segment_base,
  3076. .get_segment = svm_get_segment,
  3077. .set_segment = svm_set_segment,
  3078. .get_cpl = svm_get_cpl,
  3079. .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
  3080. .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
  3081. .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
  3082. .set_cr0 = svm_set_cr0,
  3083. .set_cr3 = svm_set_cr3,
  3084. .set_cr4 = svm_set_cr4,
  3085. .set_efer = svm_set_efer,
  3086. .get_idt = svm_get_idt,
  3087. .set_idt = svm_set_idt,
  3088. .get_gdt = svm_get_gdt,
  3089. .set_gdt = svm_set_gdt,
  3090. .set_dr7 = svm_set_dr7,
  3091. .cache_reg = svm_cache_reg,
  3092. .get_rflags = svm_get_rflags,
  3093. .set_rflags = svm_set_rflags,
  3094. .fpu_activate = svm_fpu_activate,
  3095. .fpu_deactivate = svm_fpu_deactivate,
  3096. .tlb_flush = svm_flush_tlb,
  3097. .run = svm_vcpu_run,
  3098. .handle_exit = handle_exit,
  3099. .skip_emulated_instruction = skip_emulated_instruction,
  3100. .set_interrupt_shadow = svm_set_interrupt_shadow,
  3101. .get_interrupt_shadow = svm_get_interrupt_shadow,
  3102. .patch_hypercall = svm_patch_hypercall,
  3103. .set_irq = svm_set_irq,
  3104. .set_nmi = svm_inject_nmi,
  3105. .queue_exception = svm_queue_exception,
  3106. .cancel_injection = svm_cancel_injection,
  3107. .interrupt_allowed = svm_interrupt_allowed,
  3108. .nmi_allowed = svm_nmi_allowed,
  3109. .get_nmi_mask = svm_get_nmi_mask,
  3110. .set_nmi_mask = svm_set_nmi_mask,
  3111. .enable_nmi_window = enable_nmi_window,
  3112. .enable_irq_window = enable_irq_window,
  3113. .update_cr8_intercept = update_cr8_intercept,
  3114. .set_tss_addr = svm_set_tss_addr,
  3115. .get_tdp_level = get_npt_level,
  3116. .get_mt_mask = svm_get_mt_mask,
  3117. .get_exit_info = svm_get_exit_info,
  3118. .exit_reasons_str = svm_exit_reasons_str,
  3119. .get_lpage_level = svm_get_lpage_level,
  3120. .cpuid_update = svm_cpuid_update,
  3121. .rdtscp_supported = svm_rdtscp_supported,
  3122. .set_supported_cpuid = svm_set_supported_cpuid,
  3123. .has_wbinvd_exit = svm_has_wbinvd_exit,
  3124. .write_tsc_offset = svm_write_tsc_offset,
  3125. .adjust_tsc_offset = svm_adjust_tsc_offset,
  3126. .set_tdp_cr3 = set_tdp_cr3,
  3127. };
  3128. static int __init svm_init(void)
  3129. {
  3130. return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
  3131. __alignof__(struct vcpu_svm), THIS_MODULE);
  3132. }
  3133. static void __exit svm_exit(void)
  3134. {
  3135. kvm_exit();
  3136. }
  3137. module_init(svm_init)
  3138. module_exit(svm_exit)