svm.c 117 KB

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