svm.c 117 KB

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