svm.c 114 KB

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