svm.c 106 KB

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