svm.c 100 KB

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