svm.c 110 KB

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