svm.c 98 KB

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