svm.c 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445
  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. WARN_ON(adjustment < 0);
  828. if (host)
  829. adjustment = svm_scale_tsc(vcpu, adjustment);
  830. svm->vmcb->control.tsc_offset += adjustment;
  831. if (is_guest_mode(vcpu))
  832. svm->nested.hsave->control.tsc_offset += adjustment;
  833. else
  834. trace_kvm_write_tsc_offset(vcpu->vcpu_id,
  835. svm->vmcb->control.tsc_offset - adjustment,
  836. svm->vmcb->control.tsc_offset);
  837. mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
  838. }
  839. static u64 svm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
  840. {
  841. u64 tsc;
  842. tsc = svm_scale_tsc(vcpu, native_read_tsc());
  843. return target_tsc - tsc;
  844. }
  845. static void init_vmcb(struct vcpu_svm *svm)
  846. {
  847. struct vmcb_control_area *control = &svm->vmcb->control;
  848. struct vmcb_save_area *save = &svm->vmcb->save;
  849. svm->vcpu.fpu_active = 1;
  850. svm->vcpu.arch.hflags = 0;
  851. set_cr_intercept(svm, INTERCEPT_CR0_READ);
  852. set_cr_intercept(svm, INTERCEPT_CR3_READ);
  853. set_cr_intercept(svm, INTERCEPT_CR4_READ);
  854. set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
  855. set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
  856. set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
  857. set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  858. set_dr_intercepts(svm);
  859. set_exception_intercept(svm, PF_VECTOR);
  860. set_exception_intercept(svm, UD_VECTOR);
  861. set_exception_intercept(svm, MC_VECTOR);
  862. set_intercept(svm, INTERCEPT_INTR);
  863. set_intercept(svm, INTERCEPT_NMI);
  864. set_intercept(svm, INTERCEPT_SMI);
  865. set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
  866. set_intercept(svm, INTERCEPT_RDPMC);
  867. set_intercept(svm, INTERCEPT_CPUID);
  868. set_intercept(svm, INTERCEPT_INVD);
  869. set_intercept(svm, INTERCEPT_HLT);
  870. set_intercept(svm, INTERCEPT_INVLPG);
  871. set_intercept(svm, INTERCEPT_INVLPGA);
  872. set_intercept(svm, INTERCEPT_IOIO_PROT);
  873. set_intercept(svm, INTERCEPT_MSR_PROT);
  874. set_intercept(svm, INTERCEPT_TASK_SWITCH);
  875. set_intercept(svm, INTERCEPT_SHUTDOWN);
  876. set_intercept(svm, INTERCEPT_VMRUN);
  877. set_intercept(svm, INTERCEPT_VMMCALL);
  878. set_intercept(svm, INTERCEPT_VMLOAD);
  879. set_intercept(svm, INTERCEPT_VMSAVE);
  880. set_intercept(svm, INTERCEPT_STGI);
  881. set_intercept(svm, INTERCEPT_CLGI);
  882. set_intercept(svm, INTERCEPT_SKINIT);
  883. set_intercept(svm, INTERCEPT_WBINVD);
  884. set_intercept(svm, INTERCEPT_MONITOR);
  885. set_intercept(svm, INTERCEPT_MWAIT);
  886. set_intercept(svm, INTERCEPT_XSETBV);
  887. control->iopm_base_pa = iopm_base;
  888. control->msrpm_base_pa = __pa(svm->msrpm);
  889. control->int_ctl = V_INTR_MASKING_MASK;
  890. init_seg(&save->es);
  891. init_seg(&save->ss);
  892. init_seg(&save->ds);
  893. init_seg(&save->fs);
  894. init_seg(&save->gs);
  895. save->cs.selector = 0xf000;
  896. save->cs.base = 0xffff0000;
  897. /* Executable/Readable Code Segment */
  898. save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
  899. SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
  900. save->cs.limit = 0xffff;
  901. save->gdtr.limit = 0xffff;
  902. save->idtr.limit = 0xffff;
  903. init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
  904. init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
  905. svm_set_efer(&svm->vcpu, 0);
  906. save->dr6 = 0xffff0ff0;
  907. kvm_set_rflags(&svm->vcpu, 2);
  908. save->rip = 0x0000fff0;
  909. svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
  910. /*
  911. * This is the guest-visible cr0 value.
  912. * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
  913. */
  914. svm->vcpu.arch.cr0 = 0;
  915. (void)kvm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
  916. save->cr4 = X86_CR4_PAE;
  917. /* rdx = ?? */
  918. if (npt_enabled) {
  919. /* Setup VMCB for Nested Paging */
  920. control->nested_ctl = 1;
  921. clr_intercept(svm, INTERCEPT_INVLPG);
  922. clr_exception_intercept(svm, PF_VECTOR);
  923. clr_cr_intercept(svm, INTERCEPT_CR3_READ);
  924. clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
  925. save->g_pat = 0x0007040600070406ULL;
  926. save->cr3 = 0;
  927. save->cr4 = 0;
  928. }
  929. svm->asid_generation = 0;
  930. svm->nested.vmcb = 0;
  931. svm->vcpu.arch.hflags = 0;
  932. if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
  933. control->pause_filter_count = 3000;
  934. set_intercept(svm, INTERCEPT_PAUSE);
  935. }
  936. mark_all_dirty(svm->vmcb);
  937. enable_gif(svm);
  938. }
  939. static void svm_vcpu_reset(struct kvm_vcpu *vcpu)
  940. {
  941. struct vcpu_svm *svm = to_svm(vcpu);
  942. u32 dummy;
  943. u32 eax = 1;
  944. init_vmcb(svm);
  945. kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy);
  946. kvm_register_write(vcpu, VCPU_REGS_RDX, eax);
  947. }
  948. static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
  949. {
  950. struct vcpu_svm *svm;
  951. struct page *page;
  952. struct page *msrpm_pages;
  953. struct page *hsave_page;
  954. struct page *nested_msrpm_pages;
  955. int err;
  956. svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  957. if (!svm) {
  958. err = -ENOMEM;
  959. goto out;
  960. }
  961. svm->tsc_ratio = TSC_RATIO_DEFAULT;
  962. err = kvm_vcpu_init(&svm->vcpu, kvm, id);
  963. if (err)
  964. goto free_svm;
  965. err = -ENOMEM;
  966. page = alloc_page(GFP_KERNEL);
  967. if (!page)
  968. goto uninit;
  969. msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
  970. if (!msrpm_pages)
  971. goto free_page1;
  972. nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
  973. if (!nested_msrpm_pages)
  974. goto free_page2;
  975. hsave_page = alloc_page(GFP_KERNEL);
  976. if (!hsave_page)
  977. goto free_page3;
  978. svm->nested.hsave = page_address(hsave_page);
  979. svm->msrpm = page_address(msrpm_pages);
  980. svm_vcpu_init_msrpm(svm->msrpm);
  981. svm->nested.msrpm = page_address(nested_msrpm_pages);
  982. svm_vcpu_init_msrpm(svm->nested.msrpm);
  983. svm->vmcb = page_address(page);
  984. clear_page(svm->vmcb);
  985. svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
  986. svm->asid_generation = 0;
  987. init_vmcb(svm);
  988. svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE |
  989. MSR_IA32_APICBASE_ENABLE;
  990. if (kvm_vcpu_is_bsp(&svm->vcpu))
  991. svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
  992. svm_init_osvw(&svm->vcpu);
  993. return &svm->vcpu;
  994. free_page3:
  995. __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
  996. free_page2:
  997. __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
  998. free_page1:
  999. __free_page(page);
  1000. uninit:
  1001. kvm_vcpu_uninit(&svm->vcpu);
  1002. free_svm:
  1003. kmem_cache_free(kvm_vcpu_cache, svm);
  1004. out:
  1005. return ERR_PTR(err);
  1006. }
  1007. static void svm_free_vcpu(struct kvm_vcpu *vcpu)
  1008. {
  1009. struct vcpu_svm *svm = to_svm(vcpu);
  1010. __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
  1011. __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
  1012. __free_page(virt_to_page(svm->nested.hsave));
  1013. __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
  1014. kvm_vcpu_uninit(vcpu);
  1015. kmem_cache_free(kvm_vcpu_cache, svm);
  1016. }
  1017. static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1018. {
  1019. struct vcpu_svm *svm = to_svm(vcpu);
  1020. int i;
  1021. if (unlikely(cpu != vcpu->cpu)) {
  1022. svm->asid_generation = 0;
  1023. mark_all_dirty(svm->vmcb);
  1024. }
  1025. #ifdef CONFIG_X86_64
  1026. rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
  1027. #endif
  1028. savesegment(fs, svm->host.fs);
  1029. savesegment(gs, svm->host.gs);
  1030. svm->host.ldt = kvm_read_ldt();
  1031. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  1032. rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  1033. if (static_cpu_has(X86_FEATURE_TSCRATEMSR) &&
  1034. svm->tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
  1035. __this_cpu_write(current_tsc_ratio, svm->tsc_ratio);
  1036. wrmsrl(MSR_AMD64_TSC_RATIO, svm->tsc_ratio);
  1037. }
  1038. }
  1039. static void svm_vcpu_put(struct kvm_vcpu *vcpu)
  1040. {
  1041. struct vcpu_svm *svm = to_svm(vcpu);
  1042. int i;
  1043. ++vcpu->stat.host_state_reload;
  1044. kvm_load_ldt(svm->host.ldt);
  1045. #ifdef CONFIG_X86_64
  1046. loadsegment(fs, svm->host.fs);
  1047. wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
  1048. load_gs_index(svm->host.gs);
  1049. #else
  1050. #ifdef CONFIG_X86_32_LAZY_GS
  1051. loadsegment(gs, svm->host.gs);
  1052. #endif
  1053. #endif
  1054. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  1055. wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  1056. }
  1057. static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
  1058. {
  1059. return to_svm(vcpu)->vmcb->save.rflags;
  1060. }
  1061. static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  1062. {
  1063. /*
  1064. * Any change of EFLAGS.VM is accompained by a reload of SS
  1065. * (caused by either a task switch or an inter-privilege IRET),
  1066. * so we do not need to update the CPL here.
  1067. */
  1068. to_svm(vcpu)->vmcb->save.rflags = rflags;
  1069. }
  1070. static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  1071. {
  1072. switch (reg) {
  1073. case VCPU_EXREG_PDPTR:
  1074. BUG_ON(!npt_enabled);
  1075. load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
  1076. break;
  1077. default:
  1078. BUG();
  1079. }
  1080. }
  1081. static void svm_set_vintr(struct vcpu_svm *svm)
  1082. {
  1083. set_intercept(svm, INTERCEPT_VINTR);
  1084. }
  1085. static void svm_clear_vintr(struct vcpu_svm *svm)
  1086. {
  1087. clr_intercept(svm, INTERCEPT_VINTR);
  1088. }
  1089. static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
  1090. {
  1091. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  1092. switch (seg) {
  1093. case VCPU_SREG_CS: return &save->cs;
  1094. case VCPU_SREG_DS: return &save->ds;
  1095. case VCPU_SREG_ES: return &save->es;
  1096. case VCPU_SREG_FS: return &save->fs;
  1097. case VCPU_SREG_GS: return &save->gs;
  1098. case VCPU_SREG_SS: return &save->ss;
  1099. case VCPU_SREG_TR: return &save->tr;
  1100. case VCPU_SREG_LDTR: return &save->ldtr;
  1101. }
  1102. BUG();
  1103. return NULL;
  1104. }
  1105. static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  1106. {
  1107. struct vmcb_seg *s = svm_seg(vcpu, seg);
  1108. return s->base;
  1109. }
  1110. static void svm_get_segment(struct kvm_vcpu *vcpu,
  1111. struct kvm_segment *var, int seg)
  1112. {
  1113. struct vmcb_seg *s = svm_seg(vcpu, seg);
  1114. var->base = s->base;
  1115. var->limit = s->limit;
  1116. var->selector = s->selector;
  1117. var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
  1118. var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
  1119. var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
  1120. var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
  1121. var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
  1122. var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
  1123. var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
  1124. /*
  1125. * AMD CPUs circa 2014 track the G bit for all segments except CS.
  1126. * However, the SVM spec states that the G bit is not observed by the
  1127. * CPU, and some VMware virtual CPUs drop the G bit for all segments.
  1128. * So let's synthesize a legal G bit for all segments, this helps
  1129. * running KVM nested. It also helps cross-vendor migration, because
  1130. * Intel's vmentry has a check on the 'G' bit.
  1131. */
  1132. var->g = s->limit > 0xfffff;
  1133. /*
  1134. * AMD's VMCB does not have an explicit unusable field, so emulate it
  1135. * for cross vendor migration purposes by "not present"
  1136. */
  1137. var->unusable = !var->present || (var->type == 0);
  1138. switch (seg) {
  1139. case VCPU_SREG_TR:
  1140. /*
  1141. * Work around a bug where the busy flag in the tr selector
  1142. * isn't exposed
  1143. */
  1144. var->type |= 0x2;
  1145. break;
  1146. case VCPU_SREG_DS:
  1147. case VCPU_SREG_ES:
  1148. case VCPU_SREG_FS:
  1149. case VCPU_SREG_GS:
  1150. /*
  1151. * The accessed bit must always be set in the segment
  1152. * descriptor cache, although it can be cleared in the
  1153. * descriptor, the cached bit always remains at 1. Since
  1154. * Intel has a check on this, set it here to support
  1155. * cross-vendor migration.
  1156. */
  1157. if (!var->unusable)
  1158. var->type |= 0x1;
  1159. break;
  1160. case VCPU_SREG_SS:
  1161. /*
  1162. * On AMD CPUs sometimes the DB bit in the segment
  1163. * descriptor is left as 1, although the whole segment has
  1164. * been made unusable. Clear it here to pass an Intel VMX
  1165. * entry check when cross vendor migrating.
  1166. */
  1167. if (var->unusable)
  1168. var->db = 0;
  1169. var->dpl = to_svm(vcpu)->vmcb->save.cpl;
  1170. break;
  1171. }
  1172. }
  1173. static int svm_get_cpl(struct kvm_vcpu *vcpu)
  1174. {
  1175. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  1176. return save->cpl;
  1177. }
  1178. static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1179. {
  1180. struct vcpu_svm *svm = to_svm(vcpu);
  1181. dt->size = svm->vmcb->save.idtr.limit;
  1182. dt->address = svm->vmcb->save.idtr.base;
  1183. }
  1184. static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1185. {
  1186. struct vcpu_svm *svm = to_svm(vcpu);
  1187. svm->vmcb->save.idtr.limit = dt->size;
  1188. svm->vmcb->save.idtr.base = dt->address ;
  1189. mark_dirty(svm->vmcb, VMCB_DT);
  1190. }
  1191. static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1192. {
  1193. struct vcpu_svm *svm = to_svm(vcpu);
  1194. dt->size = svm->vmcb->save.gdtr.limit;
  1195. dt->address = svm->vmcb->save.gdtr.base;
  1196. }
  1197. static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1198. {
  1199. struct vcpu_svm *svm = to_svm(vcpu);
  1200. svm->vmcb->save.gdtr.limit = dt->size;
  1201. svm->vmcb->save.gdtr.base = dt->address ;
  1202. mark_dirty(svm->vmcb, VMCB_DT);
  1203. }
  1204. static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  1205. {
  1206. }
  1207. static void svm_decache_cr3(struct kvm_vcpu *vcpu)
  1208. {
  1209. }
  1210. static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  1211. {
  1212. }
  1213. static void update_cr0_intercept(struct vcpu_svm *svm)
  1214. {
  1215. ulong gcr0 = svm->vcpu.arch.cr0;
  1216. u64 *hcr0 = &svm->vmcb->save.cr0;
  1217. if (!svm->vcpu.fpu_active)
  1218. *hcr0 |= SVM_CR0_SELECTIVE_MASK;
  1219. else
  1220. *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
  1221. | (gcr0 & SVM_CR0_SELECTIVE_MASK);
  1222. mark_dirty(svm->vmcb, VMCB_CR);
  1223. if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
  1224. clr_cr_intercept(svm, INTERCEPT_CR0_READ);
  1225. clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
  1226. } else {
  1227. set_cr_intercept(svm, INTERCEPT_CR0_READ);
  1228. set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
  1229. }
  1230. }
  1231. static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  1232. {
  1233. struct vcpu_svm *svm = to_svm(vcpu);
  1234. #ifdef CONFIG_X86_64
  1235. if (vcpu->arch.efer & EFER_LME) {
  1236. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  1237. vcpu->arch.efer |= EFER_LMA;
  1238. svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
  1239. }
  1240. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
  1241. vcpu->arch.efer &= ~EFER_LMA;
  1242. svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
  1243. }
  1244. }
  1245. #endif
  1246. vcpu->arch.cr0 = cr0;
  1247. if (!npt_enabled)
  1248. cr0 |= X86_CR0_PG | X86_CR0_WP;
  1249. if (!vcpu->fpu_active)
  1250. cr0 |= X86_CR0_TS;
  1251. /*
  1252. * re-enable caching here because the QEMU bios
  1253. * does not do it - this results in some delay at
  1254. * reboot
  1255. */
  1256. cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
  1257. svm->vmcb->save.cr0 = cr0;
  1258. mark_dirty(svm->vmcb, VMCB_CR);
  1259. update_cr0_intercept(svm);
  1260. }
  1261. static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  1262. {
  1263. unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
  1264. unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
  1265. if (cr4 & X86_CR4_VMXE)
  1266. return 1;
  1267. if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
  1268. svm_flush_tlb(vcpu);
  1269. vcpu->arch.cr4 = cr4;
  1270. if (!npt_enabled)
  1271. cr4 |= X86_CR4_PAE;
  1272. cr4 |= host_cr4_mce;
  1273. to_svm(vcpu)->vmcb->save.cr4 = cr4;
  1274. mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
  1275. return 0;
  1276. }
  1277. static void svm_set_segment(struct kvm_vcpu *vcpu,
  1278. struct kvm_segment *var, int seg)
  1279. {
  1280. struct vcpu_svm *svm = to_svm(vcpu);
  1281. struct vmcb_seg *s = svm_seg(vcpu, seg);
  1282. s->base = var->base;
  1283. s->limit = var->limit;
  1284. s->selector = var->selector;
  1285. if (var->unusable)
  1286. s->attrib = 0;
  1287. else {
  1288. s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
  1289. s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
  1290. s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
  1291. s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
  1292. s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
  1293. s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
  1294. s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
  1295. s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
  1296. }
  1297. /*
  1298. * This is always accurate, except if SYSRET returned to a segment
  1299. * with SS.DPL != 3. Intel does not have this quirk, and always
  1300. * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
  1301. * would entail passing the CPL to userspace and back.
  1302. */
  1303. if (seg == VCPU_SREG_SS)
  1304. svm->vmcb->save.cpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
  1305. mark_dirty(svm->vmcb, VMCB_SEG);
  1306. }
  1307. static void update_db_bp_intercept(struct kvm_vcpu *vcpu)
  1308. {
  1309. struct vcpu_svm *svm = to_svm(vcpu);
  1310. clr_exception_intercept(svm, DB_VECTOR);
  1311. clr_exception_intercept(svm, BP_VECTOR);
  1312. if (svm->nmi_singlestep)
  1313. set_exception_intercept(svm, DB_VECTOR);
  1314. if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
  1315. if (vcpu->guest_debug &
  1316. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  1317. set_exception_intercept(svm, DB_VECTOR);
  1318. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  1319. set_exception_intercept(svm, BP_VECTOR);
  1320. } else
  1321. vcpu->guest_debug = 0;
  1322. }
  1323. static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
  1324. {
  1325. if (sd->next_asid > sd->max_asid) {
  1326. ++sd->asid_generation;
  1327. sd->next_asid = 1;
  1328. svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
  1329. }
  1330. svm->asid_generation = sd->asid_generation;
  1331. svm->vmcb->control.asid = sd->next_asid++;
  1332. mark_dirty(svm->vmcb, VMCB_ASID);
  1333. }
  1334. static u64 svm_get_dr6(struct kvm_vcpu *vcpu)
  1335. {
  1336. return to_svm(vcpu)->vmcb->save.dr6;
  1337. }
  1338. static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value)
  1339. {
  1340. struct vcpu_svm *svm = to_svm(vcpu);
  1341. svm->vmcb->save.dr6 = value;
  1342. mark_dirty(svm->vmcb, VMCB_DR);
  1343. }
  1344. static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
  1345. {
  1346. struct vcpu_svm *svm = to_svm(vcpu);
  1347. get_debugreg(vcpu->arch.db[0], 0);
  1348. get_debugreg(vcpu->arch.db[1], 1);
  1349. get_debugreg(vcpu->arch.db[2], 2);
  1350. get_debugreg(vcpu->arch.db[3], 3);
  1351. vcpu->arch.dr6 = svm_get_dr6(vcpu);
  1352. vcpu->arch.dr7 = svm->vmcb->save.dr7;
  1353. vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
  1354. set_dr_intercepts(svm);
  1355. }
  1356. static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
  1357. {
  1358. struct vcpu_svm *svm = to_svm(vcpu);
  1359. svm->vmcb->save.dr7 = value;
  1360. mark_dirty(svm->vmcb, VMCB_DR);
  1361. }
  1362. static int pf_interception(struct vcpu_svm *svm)
  1363. {
  1364. u64 fault_address = svm->vmcb->control.exit_info_2;
  1365. u32 error_code;
  1366. int r = 1;
  1367. switch (svm->apf_reason) {
  1368. default:
  1369. error_code = svm->vmcb->control.exit_info_1;
  1370. trace_kvm_page_fault(fault_address, error_code);
  1371. if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
  1372. kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
  1373. r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
  1374. svm->vmcb->control.insn_bytes,
  1375. svm->vmcb->control.insn_len);
  1376. break;
  1377. case KVM_PV_REASON_PAGE_NOT_PRESENT:
  1378. svm->apf_reason = 0;
  1379. local_irq_disable();
  1380. kvm_async_pf_task_wait(fault_address);
  1381. local_irq_enable();
  1382. break;
  1383. case KVM_PV_REASON_PAGE_READY:
  1384. svm->apf_reason = 0;
  1385. local_irq_disable();
  1386. kvm_async_pf_task_wake(fault_address);
  1387. local_irq_enable();
  1388. break;
  1389. }
  1390. return r;
  1391. }
  1392. static int db_interception(struct vcpu_svm *svm)
  1393. {
  1394. struct kvm_run *kvm_run = svm->vcpu.run;
  1395. if (!(svm->vcpu.guest_debug &
  1396. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
  1397. !svm->nmi_singlestep) {
  1398. kvm_queue_exception(&svm->vcpu, DB_VECTOR);
  1399. return 1;
  1400. }
  1401. if (svm->nmi_singlestep) {
  1402. svm->nmi_singlestep = false;
  1403. if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
  1404. svm->vmcb->save.rflags &=
  1405. ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
  1406. update_db_bp_intercept(&svm->vcpu);
  1407. }
  1408. if (svm->vcpu.guest_debug &
  1409. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
  1410. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  1411. kvm_run->debug.arch.pc =
  1412. svm->vmcb->save.cs.base + svm->vmcb->save.rip;
  1413. kvm_run->debug.arch.exception = DB_VECTOR;
  1414. return 0;
  1415. }
  1416. return 1;
  1417. }
  1418. static int bp_interception(struct vcpu_svm *svm)
  1419. {
  1420. struct kvm_run *kvm_run = svm->vcpu.run;
  1421. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  1422. kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
  1423. kvm_run->debug.arch.exception = BP_VECTOR;
  1424. return 0;
  1425. }
  1426. static int ud_interception(struct vcpu_svm *svm)
  1427. {
  1428. int er;
  1429. er = emulate_instruction(&svm->vcpu, EMULTYPE_TRAP_UD);
  1430. if (er != EMULATE_DONE)
  1431. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  1432. return 1;
  1433. }
  1434. static void svm_fpu_activate(struct kvm_vcpu *vcpu)
  1435. {
  1436. struct vcpu_svm *svm = to_svm(vcpu);
  1437. clr_exception_intercept(svm, NM_VECTOR);
  1438. svm->vcpu.fpu_active = 1;
  1439. update_cr0_intercept(svm);
  1440. }
  1441. static int nm_interception(struct vcpu_svm *svm)
  1442. {
  1443. svm_fpu_activate(&svm->vcpu);
  1444. return 1;
  1445. }
  1446. static bool is_erratum_383(void)
  1447. {
  1448. int err, i;
  1449. u64 value;
  1450. if (!erratum_383_found)
  1451. return false;
  1452. value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
  1453. if (err)
  1454. return false;
  1455. /* Bit 62 may or may not be set for this mce */
  1456. value &= ~(1ULL << 62);
  1457. if (value != 0xb600000000010015ULL)
  1458. return false;
  1459. /* Clear MCi_STATUS registers */
  1460. for (i = 0; i < 6; ++i)
  1461. native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
  1462. value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
  1463. if (!err) {
  1464. u32 low, high;
  1465. value &= ~(1ULL << 2);
  1466. low = lower_32_bits(value);
  1467. high = upper_32_bits(value);
  1468. native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
  1469. }
  1470. /* Flush tlb to evict multi-match entries */
  1471. __flush_tlb_all();
  1472. return true;
  1473. }
  1474. static void svm_handle_mce(struct vcpu_svm *svm)
  1475. {
  1476. if (is_erratum_383()) {
  1477. /*
  1478. * Erratum 383 triggered. Guest state is corrupt so kill the
  1479. * guest.
  1480. */
  1481. pr_err("KVM: Guest triggered AMD Erratum 383\n");
  1482. kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
  1483. return;
  1484. }
  1485. /*
  1486. * On an #MC intercept the MCE handler is not called automatically in
  1487. * the host. So do it by hand here.
  1488. */
  1489. asm volatile (
  1490. "int $0x12\n");
  1491. /* not sure if we ever come back to this point */
  1492. return;
  1493. }
  1494. static int mc_interception(struct vcpu_svm *svm)
  1495. {
  1496. return 1;
  1497. }
  1498. static int shutdown_interception(struct vcpu_svm *svm)
  1499. {
  1500. struct kvm_run *kvm_run = svm->vcpu.run;
  1501. /*
  1502. * VMCB is undefined after a SHUTDOWN intercept
  1503. * so reinitialize it.
  1504. */
  1505. clear_page(svm->vmcb);
  1506. init_vmcb(svm);
  1507. kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
  1508. return 0;
  1509. }
  1510. static int io_interception(struct vcpu_svm *svm)
  1511. {
  1512. struct kvm_vcpu *vcpu = &svm->vcpu;
  1513. u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
  1514. int size, in, string;
  1515. unsigned port;
  1516. ++svm->vcpu.stat.io_exits;
  1517. string = (io_info & SVM_IOIO_STR_MASK) != 0;
  1518. in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
  1519. if (string || in)
  1520. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  1521. port = io_info >> 16;
  1522. size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
  1523. svm->next_rip = svm->vmcb->control.exit_info_2;
  1524. skip_emulated_instruction(&svm->vcpu);
  1525. return kvm_fast_pio_out(vcpu, size, port);
  1526. }
  1527. static int nmi_interception(struct vcpu_svm *svm)
  1528. {
  1529. return 1;
  1530. }
  1531. static int intr_interception(struct vcpu_svm *svm)
  1532. {
  1533. ++svm->vcpu.stat.irq_exits;
  1534. return 1;
  1535. }
  1536. static int nop_on_interception(struct vcpu_svm *svm)
  1537. {
  1538. return 1;
  1539. }
  1540. static int halt_interception(struct vcpu_svm *svm)
  1541. {
  1542. svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
  1543. skip_emulated_instruction(&svm->vcpu);
  1544. return kvm_emulate_halt(&svm->vcpu);
  1545. }
  1546. static int vmmcall_interception(struct vcpu_svm *svm)
  1547. {
  1548. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1549. skip_emulated_instruction(&svm->vcpu);
  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. kvm_init_shadow_mmu(vcpu, &vcpu->arch.mmu);
  1605. vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
  1606. vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
  1607. vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr;
  1608. vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
  1609. vcpu->arch.mmu.shadow_root_level = get_npt_level();
  1610. vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
  1611. }
  1612. static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
  1613. {
  1614. vcpu->arch.walk_mmu = &vcpu->arch.mmu;
  1615. }
  1616. static int nested_svm_check_permissions(struct vcpu_svm *svm)
  1617. {
  1618. if (!(svm->vcpu.arch.efer & EFER_SVME)
  1619. || !is_paging(&svm->vcpu)) {
  1620. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  1621. return 1;
  1622. }
  1623. if (svm->vmcb->save.cpl) {
  1624. kvm_inject_gp(&svm->vcpu, 0);
  1625. return 1;
  1626. }
  1627. return 0;
  1628. }
  1629. static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
  1630. bool has_error_code, u32 error_code)
  1631. {
  1632. int vmexit;
  1633. if (!is_guest_mode(&svm->vcpu))
  1634. return 0;
  1635. svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
  1636. svm->vmcb->control.exit_code_hi = 0;
  1637. svm->vmcb->control.exit_info_1 = error_code;
  1638. svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
  1639. vmexit = nested_svm_intercept(svm);
  1640. if (vmexit == NESTED_EXIT_DONE)
  1641. svm->nested.exit_required = true;
  1642. return vmexit;
  1643. }
  1644. /* This function returns true if it is save to enable the irq window */
  1645. static inline bool nested_svm_intr(struct vcpu_svm *svm)
  1646. {
  1647. if (!is_guest_mode(&svm->vcpu))
  1648. return true;
  1649. if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
  1650. return true;
  1651. if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
  1652. return false;
  1653. /*
  1654. * if vmexit was already requested (by intercepted exception
  1655. * for instance) do not overwrite it with "external interrupt"
  1656. * vmexit.
  1657. */
  1658. if (svm->nested.exit_required)
  1659. return false;
  1660. svm->vmcb->control.exit_code = SVM_EXIT_INTR;
  1661. svm->vmcb->control.exit_info_1 = 0;
  1662. svm->vmcb->control.exit_info_2 = 0;
  1663. if (svm->nested.intercept & 1ULL) {
  1664. /*
  1665. * The #vmexit can't be emulated here directly because this
  1666. * code path runs with irqs and preemption disabled. A
  1667. * #vmexit emulation might sleep. Only signal request for
  1668. * the #vmexit here.
  1669. */
  1670. svm->nested.exit_required = true;
  1671. trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
  1672. return false;
  1673. }
  1674. return true;
  1675. }
  1676. /* This function returns true if it is save to enable the nmi window */
  1677. static inline bool nested_svm_nmi(struct vcpu_svm *svm)
  1678. {
  1679. if (!is_guest_mode(&svm->vcpu))
  1680. return true;
  1681. if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
  1682. return true;
  1683. svm->vmcb->control.exit_code = SVM_EXIT_NMI;
  1684. svm->nested.exit_required = true;
  1685. return false;
  1686. }
  1687. static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
  1688. {
  1689. struct page *page;
  1690. might_sleep();
  1691. page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
  1692. if (is_error_page(page))
  1693. goto error;
  1694. *_page = page;
  1695. return kmap(page);
  1696. error:
  1697. kvm_inject_gp(&svm->vcpu, 0);
  1698. return NULL;
  1699. }
  1700. static void nested_svm_unmap(struct page *page)
  1701. {
  1702. kunmap(page);
  1703. kvm_release_page_dirty(page);
  1704. }
  1705. static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
  1706. {
  1707. unsigned port, size, iopm_len;
  1708. u16 val, mask;
  1709. u8 start_bit;
  1710. u64 gpa;
  1711. if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
  1712. return NESTED_EXIT_HOST;
  1713. port = svm->vmcb->control.exit_info_1 >> 16;
  1714. size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
  1715. SVM_IOIO_SIZE_SHIFT;
  1716. gpa = svm->nested.vmcb_iopm + (port / 8);
  1717. start_bit = port % 8;
  1718. iopm_len = (start_bit + size > 8) ? 2 : 1;
  1719. mask = (0xf >> (4 - size)) << start_bit;
  1720. val = 0;
  1721. if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, iopm_len))
  1722. return NESTED_EXIT_DONE;
  1723. return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
  1724. }
  1725. static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
  1726. {
  1727. u32 offset, msr, value;
  1728. int write, mask;
  1729. if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
  1730. return NESTED_EXIT_HOST;
  1731. msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  1732. offset = svm_msrpm_offset(msr);
  1733. write = svm->vmcb->control.exit_info_1 & 1;
  1734. mask = 1 << ((2 * (msr & 0xf)) + write);
  1735. if (offset == MSR_INVALID)
  1736. return NESTED_EXIT_DONE;
  1737. /* Offset is in 32 bit units but need in 8 bit units */
  1738. offset *= 4;
  1739. if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
  1740. return NESTED_EXIT_DONE;
  1741. return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
  1742. }
  1743. static int nested_svm_exit_special(struct vcpu_svm *svm)
  1744. {
  1745. u32 exit_code = svm->vmcb->control.exit_code;
  1746. switch (exit_code) {
  1747. case SVM_EXIT_INTR:
  1748. case SVM_EXIT_NMI:
  1749. case SVM_EXIT_EXCP_BASE + MC_VECTOR:
  1750. return NESTED_EXIT_HOST;
  1751. case SVM_EXIT_NPF:
  1752. /* For now we are always handling NPFs when using them */
  1753. if (npt_enabled)
  1754. return NESTED_EXIT_HOST;
  1755. break;
  1756. case SVM_EXIT_EXCP_BASE + PF_VECTOR:
  1757. /* When we're shadowing, trap PFs, but not async PF */
  1758. if (!npt_enabled && svm->apf_reason == 0)
  1759. return NESTED_EXIT_HOST;
  1760. break;
  1761. case SVM_EXIT_EXCP_BASE + NM_VECTOR:
  1762. nm_interception(svm);
  1763. break;
  1764. default:
  1765. break;
  1766. }
  1767. return NESTED_EXIT_CONTINUE;
  1768. }
  1769. /*
  1770. * If this function returns true, this #vmexit was already handled
  1771. */
  1772. static int nested_svm_intercept(struct vcpu_svm *svm)
  1773. {
  1774. u32 exit_code = svm->vmcb->control.exit_code;
  1775. int vmexit = NESTED_EXIT_HOST;
  1776. switch (exit_code) {
  1777. case SVM_EXIT_MSR:
  1778. vmexit = nested_svm_exit_handled_msr(svm);
  1779. break;
  1780. case SVM_EXIT_IOIO:
  1781. vmexit = nested_svm_intercept_ioio(svm);
  1782. break;
  1783. case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
  1784. u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
  1785. if (svm->nested.intercept_cr & bit)
  1786. vmexit = NESTED_EXIT_DONE;
  1787. break;
  1788. }
  1789. case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
  1790. u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
  1791. if (svm->nested.intercept_dr & bit)
  1792. vmexit = NESTED_EXIT_DONE;
  1793. break;
  1794. }
  1795. case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
  1796. u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
  1797. if (svm->nested.intercept_exceptions & excp_bits)
  1798. vmexit = NESTED_EXIT_DONE;
  1799. /* async page fault always cause vmexit */
  1800. else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
  1801. svm->apf_reason != 0)
  1802. vmexit = NESTED_EXIT_DONE;
  1803. break;
  1804. }
  1805. case SVM_EXIT_ERR: {
  1806. vmexit = NESTED_EXIT_DONE;
  1807. break;
  1808. }
  1809. default: {
  1810. u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
  1811. if (svm->nested.intercept & exit_bits)
  1812. vmexit = NESTED_EXIT_DONE;
  1813. }
  1814. }
  1815. return vmexit;
  1816. }
  1817. static int nested_svm_exit_handled(struct vcpu_svm *svm)
  1818. {
  1819. int vmexit;
  1820. vmexit = nested_svm_intercept(svm);
  1821. if (vmexit == NESTED_EXIT_DONE)
  1822. nested_svm_vmexit(svm);
  1823. return vmexit;
  1824. }
  1825. static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
  1826. {
  1827. struct vmcb_control_area *dst = &dst_vmcb->control;
  1828. struct vmcb_control_area *from = &from_vmcb->control;
  1829. dst->intercept_cr = from->intercept_cr;
  1830. dst->intercept_dr = from->intercept_dr;
  1831. dst->intercept_exceptions = from->intercept_exceptions;
  1832. dst->intercept = from->intercept;
  1833. dst->iopm_base_pa = from->iopm_base_pa;
  1834. dst->msrpm_base_pa = from->msrpm_base_pa;
  1835. dst->tsc_offset = from->tsc_offset;
  1836. dst->asid = from->asid;
  1837. dst->tlb_ctl = from->tlb_ctl;
  1838. dst->int_ctl = from->int_ctl;
  1839. dst->int_vector = from->int_vector;
  1840. dst->int_state = from->int_state;
  1841. dst->exit_code = from->exit_code;
  1842. dst->exit_code_hi = from->exit_code_hi;
  1843. dst->exit_info_1 = from->exit_info_1;
  1844. dst->exit_info_2 = from->exit_info_2;
  1845. dst->exit_int_info = from->exit_int_info;
  1846. dst->exit_int_info_err = from->exit_int_info_err;
  1847. dst->nested_ctl = from->nested_ctl;
  1848. dst->event_inj = from->event_inj;
  1849. dst->event_inj_err = from->event_inj_err;
  1850. dst->nested_cr3 = from->nested_cr3;
  1851. dst->lbr_ctl = from->lbr_ctl;
  1852. }
  1853. static int nested_svm_vmexit(struct vcpu_svm *svm)
  1854. {
  1855. struct vmcb *nested_vmcb;
  1856. struct vmcb *hsave = svm->nested.hsave;
  1857. struct vmcb *vmcb = svm->vmcb;
  1858. struct page *page;
  1859. trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
  1860. vmcb->control.exit_info_1,
  1861. vmcb->control.exit_info_2,
  1862. vmcb->control.exit_int_info,
  1863. vmcb->control.exit_int_info_err,
  1864. KVM_ISA_SVM);
  1865. nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
  1866. if (!nested_vmcb)
  1867. return 1;
  1868. /* Exit Guest-Mode */
  1869. leave_guest_mode(&svm->vcpu);
  1870. svm->nested.vmcb = 0;
  1871. /* Give the current vmcb to the guest */
  1872. disable_gif(svm);
  1873. nested_vmcb->save.es = vmcb->save.es;
  1874. nested_vmcb->save.cs = vmcb->save.cs;
  1875. nested_vmcb->save.ss = vmcb->save.ss;
  1876. nested_vmcb->save.ds = vmcb->save.ds;
  1877. nested_vmcb->save.gdtr = vmcb->save.gdtr;
  1878. nested_vmcb->save.idtr = vmcb->save.idtr;
  1879. nested_vmcb->save.efer = svm->vcpu.arch.efer;
  1880. nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
  1881. nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
  1882. nested_vmcb->save.cr2 = vmcb->save.cr2;
  1883. nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
  1884. nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
  1885. nested_vmcb->save.rip = vmcb->save.rip;
  1886. nested_vmcb->save.rsp = vmcb->save.rsp;
  1887. nested_vmcb->save.rax = vmcb->save.rax;
  1888. nested_vmcb->save.dr7 = vmcb->save.dr7;
  1889. nested_vmcb->save.dr6 = vmcb->save.dr6;
  1890. nested_vmcb->save.cpl = vmcb->save.cpl;
  1891. nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
  1892. nested_vmcb->control.int_vector = vmcb->control.int_vector;
  1893. nested_vmcb->control.int_state = vmcb->control.int_state;
  1894. nested_vmcb->control.exit_code = vmcb->control.exit_code;
  1895. nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
  1896. nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
  1897. nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
  1898. nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
  1899. nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
  1900. nested_vmcb->control.next_rip = vmcb->control.next_rip;
  1901. /*
  1902. * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
  1903. * to make sure that we do not lose injected events. So check event_inj
  1904. * here and copy it to exit_int_info if it is valid.
  1905. * Exit_int_info and event_inj can't be both valid because the case
  1906. * below only happens on a VMRUN instruction intercept which has
  1907. * no valid exit_int_info set.
  1908. */
  1909. if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
  1910. struct vmcb_control_area *nc = &nested_vmcb->control;
  1911. nc->exit_int_info = vmcb->control.event_inj;
  1912. nc->exit_int_info_err = vmcb->control.event_inj_err;
  1913. }
  1914. nested_vmcb->control.tlb_ctl = 0;
  1915. nested_vmcb->control.event_inj = 0;
  1916. nested_vmcb->control.event_inj_err = 0;
  1917. /* We always set V_INTR_MASKING and remember the old value in hflags */
  1918. if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
  1919. nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
  1920. /* Restore the original control entries */
  1921. copy_vmcb_control_area(vmcb, hsave);
  1922. kvm_clear_exception_queue(&svm->vcpu);
  1923. kvm_clear_interrupt_queue(&svm->vcpu);
  1924. svm->nested.nested_cr3 = 0;
  1925. /* Restore selected save entries */
  1926. svm->vmcb->save.es = hsave->save.es;
  1927. svm->vmcb->save.cs = hsave->save.cs;
  1928. svm->vmcb->save.ss = hsave->save.ss;
  1929. svm->vmcb->save.ds = hsave->save.ds;
  1930. svm->vmcb->save.gdtr = hsave->save.gdtr;
  1931. svm->vmcb->save.idtr = hsave->save.idtr;
  1932. kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
  1933. svm_set_efer(&svm->vcpu, hsave->save.efer);
  1934. svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
  1935. svm_set_cr4(&svm->vcpu, hsave->save.cr4);
  1936. if (npt_enabled) {
  1937. svm->vmcb->save.cr3 = hsave->save.cr3;
  1938. svm->vcpu.arch.cr3 = hsave->save.cr3;
  1939. } else {
  1940. (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
  1941. }
  1942. kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
  1943. kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
  1944. kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
  1945. svm->vmcb->save.dr7 = 0;
  1946. svm->vmcb->save.cpl = 0;
  1947. svm->vmcb->control.exit_int_info = 0;
  1948. mark_all_dirty(svm->vmcb);
  1949. nested_svm_unmap(page);
  1950. nested_svm_uninit_mmu_context(&svm->vcpu);
  1951. kvm_mmu_reset_context(&svm->vcpu);
  1952. kvm_mmu_load(&svm->vcpu);
  1953. return 0;
  1954. }
  1955. static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
  1956. {
  1957. /*
  1958. * This function merges the msr permission bitmaps of kvm and the
  1959. * nested vmcb. It is optimized in that it only merges the parts where
  1960. * the kvm msr permission bitmap may contain zero bits
  1961. */
  1962. int i;
  1963. if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
  1964. return true;
  1965. for (i = 0; i < MSRPM_OFFSETS; i++) {
  1966. u32 value, p;
  1967. u64 offset;
  1968. if (msrpm_offsets[i] == 0xffffffff)
  1969. break;
  1970. p = msrpm_offsets[i];
  1971. offset = svm->nested.vmcb_msrpm + (p * 4);
  1972. if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
  1973. return false;
  1974. svm->nested.msrpm[p] = svm->msrpm[p] | value;
  1975. }
  1976. svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
  1977. return true;
  1978. }
  1979. static bool nested_vmcb_checks(struct vmcb *vmcb)
  1980. {
  1981. if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
  1982. return false;
  1983. if (vmcb->control.asid == 0)
  1984. return false;
  1985. if (vmcb->control.nested_ctl && !npt_enabled)
  1986. return false;
  1987. return true;
  1988. }
  1989. static bool nested_svm_vmrun(struct vcpu_svm *svm)
  1990. {
  1991. struct vmcb *nested_vmcb;
  1992. struct vmcb *hsave = svm->nested.hsave;
  1993. struct vmcb *vmcb = svm->vmcb;
  1994. struct page *page;
  1995. u64 vmcb_gpa;
  1996. vmcb_gpa = svm->vmcb->save.rax;
  1997. nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
  1998. if (!nested_vmcb)
  1999. return false;
  2000. if (!nested_vmcb_checks(nested_vmcb)) {
  2001. nested_vmcb->control.exit_code = SVM_EXIT_ERR;
  2002. nested_vmcb->control.exit_code_hi = 0;
  2003. nested_vmcb->control.exit_info_1 = 0;
  2004. nested_vmcb->control.exit_info_2 = 0;
  2005. nested_svm_unmap(page);
  2006. return false;
  2007. }
  2008. trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
  2009. nested_vmcb->save.rip,
  2010. nested_vmcb->control.int_ctl,
  2011. nested_vmcb->control.event_inj,
  2012. nested_vmcb->control.nested_ctl);
  2013. trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
  2014. nested_vmcb->control.intercept_cr >> 16,
  2015. nested_vmcb->control.intercept_exceptions,
  2016. nested_vmcb->control.intercept);
  2017. /* Clear internal status */
  2018. kvm_clear_exception_queue(&svm->vcpu);
  2019. kvm_clear_interrupt_queue(&svm->vcpu);
  2020. /*
  2021. * Save the old vmcb, so we don't need to pick what we save, but can
  2022. * restore everything when a VMEXIT occurs
  2023. */
  2024. hsave->save.es = vmcb->save.es;
  2025. hsave->save.cs = vmcb->save.cs;
  2026. hsave->save.ss = vmcb->save.ss;
  2027. hsave->save.ds = vmcb->save.ds;
  2028. hsave->save.gdtr = vmcb->save.gdtr;
  2029. hsave->save.idtr = vmcb->save.idtr;
  2030. hsave->save.efer = svm->vcpu.arch.efer;
  2031. hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
  2032. hsave->save.cr4 = svm->vcpu.arch.cr4;
  2033. hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
  2034. hsave->save.rip = kvm_rip_read(&svm->vcpu);
  2035. hsave->save.rsp = vmcb->save.rsp;
  2036. hsave->save.rax = vmcb->save.rax;
  2037. if (npt_enabled)
  2038. hsave->save.cr3 = vmcb->save.cr3;
  2039. else
  2040. hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
  2041. copy_vmcb_control_area(hsave, vmcb);
  2042. if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
  2043. svm->vcpu.arch.hflags |= HF_HIF_MASK;
  2044. else
  2045. svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
  2046. if (nested_vmcb->control.nested_ctl) {
  2047. kvm_mmu_unload(&svm->vcpu);
  2048. svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
  2049. nested_svm_init_mmu_context(&svm->vcpu);
  2050. }
  2051. /* Load the nested guest state */
  2052. svm->vmcb->save.es = nested_vmcb->save.es;
  2053. svm->vmcb->save.cs = nested_vmcb->save.cs;
  2054. svm->vmcb->save.ss = nested_vmcb->save.ss;
  2055. svm->vmcb->save.ds = nested_vmcb->save.ds;
  2056. svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
  2057. svm->vmcb->save.idtr = nested_vmcb->save.idtr;
  2058. kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
  2059. svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
  2060. svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
  2061. svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
  2062. if (npt_enabled) {
  2063. svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
  2064. svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
  2065. } else
  2066. (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
  2067. /* Guest paging mode is active - reset mmu */
  2068. kvm_mmu_reset_context(&svm->vcpu);
  2069. svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
  2070. kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
  2071. kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
  2072. kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
  2073. /* In case we don't even reach vcpu_run, the fields are not updated */
  2074. svm->vmcb->save.rax = nested_vmcb->save.rax;
  2075. svm->vmcb->save.rsp = nested_vmcb->save.rsp;
  2076. svm->vmcb->save.rip = nested_vmcb->save.rip;
  2077. svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
  2078. svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
  2079. svm->vmcb->save.cpl = nested_vmcb->save.cpl;
  2080. svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
  2081. svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
  2082. /* cache intercepts */
  2083. svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
  2084. svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
  2085. svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
  2086. svm->nested.intercept = nested_vmcb->control.intercept;
  2087. svm_flush_tlb(&svm->vcpu);
  2088. svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
  2089. if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
  2090. svm->vcpu.arch.hflags |= HF_VINTR_MASK;
  2091. else
  2092. svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
  2093. if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
  2094. /* We only want the cr8 intercept bits of the guest */
  2095. clr_cr_intercept(svm, INTERCEPT_CR8_READ);
  2096. clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  2097. }
  2098. /* We don't want to see VMMCALLs from a nested guest */
  2099. clr_intercept(svm, INTERCEPT_VMMCALL);
  2100. svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
  2101. svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
  2102. svm->vmcb->control.int_state = nested_vmcb->control.int_state;
  2103. svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
  2104. svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
  2105. svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
  2106. nested_svm_unmap(page);
  2107. /* Enter Guest-Mode */
  2108. enter_guest_mode(&svm->vcpu);
  2109. /*
  2110. * Merge guest and host intercepts - must be called with vcpu in
  2111. * guest-mode to take affect here
  2112. */
  2113. recalc_intercepts(svm);
  2114. svm->nested.vmcb = vmcb_gpa;
  2115. enable_gif(svm);
  2116. mark_all_dirty(svm->vmcb);
  2117. return true;
  2118. }
  2119. static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
  2120. {
  2121. to_vmcb->save.fs = from_vmcb->save.fs;
  2122. to_vmcb->save.gs = from_vmcb->save.gs;
  2123. to_vmcb->save.tr = from_vmcb->save.tr;
  2124. to_vmcb->save.ldtr = from_vmcb->save.ldtr;
  2125. to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
  2126. to_vmcb->save.star = from_vmcb->save.star;
  2127. to_vmcb->save.lstar = from_vmcb->save.lstar;
  2128. to_vmcb->save.cstar = from_vmcb->save.cstar;
  2129. to_vmcb->save.sfmask = from_vmcb->save.sfmask;
  2130. to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
  2131. to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
  2132. to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
  2133. }
  2134. static int vmload_interception(struct vcpu_svm *svm)
  2135. {
  2136. struct vmcb *nested_vmcb;
  2137. struct page *page;
  2138. if (nested_svm_check_permissions(svm))
  2139. return 1;
  2140. nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
  2141. if (!nested_vmcb)
  2142. return 1;
  2143. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2144. skip_emulated_instruction(&svm->vcpu);
  2145. nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
  2146. nested_svm_unmap(page);
  2147. return 1;
  2148. }
  2149. static int vmsave_interception(struct vcpu_svm *svm)
  2150. {
  2151. struct vmcb *nested_vmcb;
  2152. struct page *page;
  2153. if (nested_svm_check_permissions(svm))
  2154. return 1;
  2155. nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
  2156. if (!nested_vmcb)
  2157. return 1;
  2158. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2159. skip_emulated_instruction(&svm->vcpu);
  2160. nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
  2161. nested_svm_unmap(page);
  2162. return 1;
  2163. }
  2164. static int vmrun_interception(struct vcpu_svm *svm)
  2165. {
  2166. if (nested_svm_check_permissions(svm))
  2167. return 1;
  2168. /* Save rip after vmrun instruction */
  2169. kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
  2170. if (!nested_svm_vmrun(svm))
  2171. return 1;
  2172. if (!nested_svm_vmrun_msrpm(svm))
  2173. goto failed;
  2174. return 1;
  2175. failed:
  2176. svm->vmcb->control.exit_code = SVM_EXIT_ERR;
  2177. svm->vmcb->control.exit_code_hi = 0;
  2178. svm->vmcb->control.exit_info_1 = 0;
  2179. svm->vmcb->control.exit_info_2 = 0;
  2180. nested_svm_vmexit(svm);
  2181. return 1;
  2182. }
  2183. static int stgi_interception(struct vcpu_svm *svm)
  2184. {
  2185. if (nested_svm_check_permissions(svm))
  2186. return 1;
  2187. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2188. skip_emulated_instruction(&svm->vcpu);
  2189. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  2190. enable_gif(svm);
  2191. return 1;
  2192. }
  2193. static int clgi_interception(struct vcpu_svm *svm)
  2194. {
  2195. if (nested_svm_check_permissions(svm))
  2196. return 1;
  2197. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2198. skip_emulated_instruction(&svm->vcpu);
  2199. disable_gif(svm);
  2200. /* After a CLGI no interrupts should come */
  2201. svm_clear_vintr(svm);
  2202. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  2203. mark_dirty(svm->vmcb, VMCB_INTR);
  2204. return 1;
  2205. }
  2206. static int invlpga_interception(struct vcpu_svm *svm)
  2207. {
  2208. struct kvm_vcpu *vcpu = &svm->vcpu;
  2209. trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
  2210. vcpu->arch.regs[VCPU_REGS_RAX]);
  2211. /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
  2212. kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
  2213. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2214. skip_emulated_instruction(&svm->vcpu);
  2215. return 1;
  2216. }
  2217. static int skinit_interception(struct vcpu_svm *svm)
  2218. {
  2219. trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
  2220. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  2221. return 1;
  2222. }
  2223. static int xsetbv_interception(struct vcpu_svm *svm)
  2224. {
  2225. u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
  2226. u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
  2227. if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
  2228. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2229. skip_emulated_instruction(&svm->vcpu);
  2230. }
  2231. return 1;
  2232. }
  2233. static int task_switch_interception(struct vcpu_svm *svm)
  2234. {
  2235. u16 tss_selector;
  2236. int reason;
  2237. int int_type = svm->vmcb->control.exit_int_info &
  2238. SVM_EXITINTINFO_TYPE_MASK;
  2239. int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
  2240. uint32_t type =
  2241. svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
  2242. uint32_t idt_v =
  2243. svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
  2244. bool has_error_code = false;
  2245. u32 error_code = 0;
  2246. tss_selector = (u16)svm->vmcb->control.exit_info_1;
  2247. if (svm->vmcb->control.exit_info_2 &
  2248. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
  2249. reason = TASK_SWITCH_IRET;
  2250. else if (svm->vmcb->control.exit_info_2 &
  2251. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
  2252. reason = TASK_SWITCH_JMP;
  2253. else if (idt_v)
  2254. reason = TASK_SWITCH_GATE;
  2255. else
  2256. reason = TASK_SWITCH_CALL;
  2257. if (reason == TASK_SWITCH_GATE) {
  2258. switch (type) {
  2259. case SVM_EXITINTINFO_TYPE_NMI:
  2260. svm->vcpu.arch.nmi_injected = false;
  2261. break;
  2262. case SVM_EXITINTINFO_TYPE_EXEPT:
  2263. if (svm->vmcb->control.exit_info_2 &
  2264. (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
  2265. has_error_code = true;
  2266. error_code =
  2267. (u32)svm->vmcb->control.exit_info_2;
  2268. }
  2269. kvm_clear_exception_queue(&svm->vcpu);
  2270. break;
  2271. case SVM_EXITINTINFO_TYPE_INTR:
  2272. kvm_clear_interrupt_queue(&svm->vcpu);
  2273. break;
  2274. default:
  2275. break;
  2276. }
  2277. }
  2278. if (reason != TASK_SWITCH_GATE ||
  2279. int_type == SVM_EXITINTINFO_TYPE_SOFT ||
  2280. (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
  2281. (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
  2282. skip_emulated_instruction(&svm->vcpu);
  2283. if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
  2284. int_vec = -1;
  2285. if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
  2286. has_error_code, error_code) == EMULATE_FAIL) {
  2287. svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  2288. svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  2289. svm->vcpu.run->internal.ndata = 0;
  2290. return 0;
  2291. }
  2292. return 1;
  2293. }
  2294. static int cpuid_interception(struct vcpu_svm *svm)
  2295. {
  2296. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  2297. kvm_emulate_cpuid(&svm->vcpu);
  2298. return 1;
  2299. }
  2300. static int iret_interception(struct vcpu_svm *svm)
  2301. {
  2302. ++svm->vcpu.stat.nmi_window_exits;
  2303. clr_intercept(svm, INTERCEPT_IRET);
  2304. svm->vcpu.arch.hflags |= HF_IRET_MASK;
  2305. svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
  2306. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  2307. return 1;
  2308. }
  2309. static int invlpg_interception(struct vcpu_svm *svm)
  2310. {
  2311. if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
  2312. return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
  2313. kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
  2314. skip_emulated_instruction(&svm->vcpu);
  2315. return 1;
  2316. }
  2317. static int emulate_on_interception(struct vcpu_svm *svm)
  2318. {
  2319. return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
  2320. }
  2321. static int rdpmc_interception(struct vcpu_svm *svm)
  2322. {
  2323. int err;
  2324. if (!static_cpu_has(X86_FEATURE_NRIPS))
  2325. return emulate_on_interception(svm);
  2326. err = kvm_rdpmc(&svm->vcpu);
  2327. kvm_complete_insn_gp(&svm->vcpu, err);
  2328. return 1;
  2329. }
  2330. bool check_selective_cr0_intercepted(struct vcpu_svm *svm, unsigned long val)
  2331. {
  2332. unsigned long cr0 = svm->vcpu.arch.cr0;
  2333. bool ret = false;
  2334. u64 intercept;
  2335. intercept = svm->nested.intercept;
  2336. if (!is_guest_mode(&svm->vcpu) ||
  2337. (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
  2338. return false;
  2339. cr0 &= ~SVM_CR0_SELECTIVE_MASK;
  2340. val &= ~SVM_CR0_SELECTIVE_MASK;
  2341. if (cr0 ^ val) {
  2342. svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
  2343. ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
  2344. }
  2345. return ret;
  2346. }
  2347. #define CR_VALID (1ULL << 63)
  2348. static int cr_interception(struct vcpu_svm *svm)
  2349. {
  2350. int reg, cr;
  2351. unsigned long val;
  2352. int err;
  2353. if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
  2354. return emulate_on_interception(svm);
  2355. if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
  2356. return emulate_on_interception(svm);
  2357. reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
  2358. cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
  2359. err = 0;
  2360. if (cr >= 16) { /* mov to cr */
  2361. cr -= 16;
  2362. val = kvm_register_read(&svm->vcpu, reg);
  2363. switch (cr) {
  2364. case 0:
  2365. if (!check_selective_cr0_intercepted(svm, val))
  2366. err = kvm_set_cr0(&svm->vcpu, val);
  2367. else
  2368. return 1;
  2369. break;
  2370. case 3:
  2371. err = kvm_set_cr3(&svm->vcpu, val);
  2372. break;
  2373. case 4:
  2374. err = kvm_set_cr4(&svm->vcpu, val);
  2375. break;
  2376. case 8:
  2377. err = kvm_set_cr8(&svm->vcpu, val);
  2378. break;
  2379. default:
  2380. WARN(1, "unhandled write to CR%d", cr);
  2381. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  2382. return 1;
  2383. }
  2384. } else { /* mov from cr */
  2385. switch (cr) {
  2386. case 0:
  2387. val = kvm_read_cr0(&svm->vcpu);
  2388. break;
  2389. case 2:
  2390. val = svm->vcpu.arch.cr2;
  2391. break;
  2392. case 3:
  2393. val = kvm_read_cr3(&svm->vcpu);
  2394. break;
  2395. case 4:
  2396. val = kvm_read_cr4(&svm->vcpu);
  2397. break;
  2398. case 8:
  2399. val = kvm_get_cr8(&svm->vcpu);
  2400. break;
  2401. default:
  2402. WARN(1, "unhandled read from CR%d", cr);
  2403. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  2404. return 1;
  2405. }
  2406. kvm_register_write(&svm->vcpu, reg, val);
  2407. }
  2408. kvm_complete_insn_gp(&svm->vcpu, err);
  2409. return 1;
  2410. }
  2411. static int dr_interception(struct vcpu_svm *svm)
  2412. {
  2413. int reg, dr;
  2414. unsigned long val;
  2415. int err;
  2416. if (svm->vcpu.guest_debug == 0) {
  2417. /*
  2418. * No more DR vmexits; force a reload of the debug registers
  2419. * and reenter on this instruction. The next vmexit will
  2420. * retrieve the full state of the debug registers.
  2421. */
  2422. clr_dr_intercepts(svm);
  2423. svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
  2424. return 1;
  2425. }
  2426. if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
  2427. return emulate_on_interception(svm);
  2428. reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
  2429. dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
  2430. if (dr >= 16) { /* mov to DRn */
  2431. val = kvm_register_read(&svm->vcpu, reg);
  2432. kvm_set_dr(&svm->vcpu, dr - 16, val);
  2433. } else {
  2434. err = kvm_get_dr(&svm->vcpu, dr, &val);
  2435. if (!err)
  2436. kvm_register_write(&svm->vcpu, reg, val);
  2437. }
  2438. skip_emulated_instruction(&svm->vcpu);
  2439. return 1;
  2440. }
  2441. static int cr8_write_interception(struct vcpu_svm *svm)
  2442. {
  2443. struct kvm_run *kvm_run = svm->vcpu.run;
  2444. int r;
  2445. u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
  2446. /* instruction emulation calls kvm_set_cr8() */
  2447. r = cr_interception(svm);
  2448. if (irqchip_in_kernel(svm->vcpu.kvm))
  2449. return r;
  2450. if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
  2451. return r;
  2452. kvm_run->exit_reason = KVM_EXIT_SET_TPR;
  2453. return 0;
  2454. }
  2455. static u64 svm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
  2456. {
  2457. struct vmcb *vmcb = get_host_vmcb(to_svm(vcpu));
  2458. return vmcb->control.tsc_offset +
  2459. svm_scale_tsc(vcpu, host_tsc);
  2460. }
  2461. static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
  2462. {
  2463. struct vcpu_svm *svm = to_svm(vcpu);
  2464. switch (ecx) {
  2465. case MSR_IA32_TSC: {
  2466. *data = svm->vmcb->control.tsc_offset +
  2467. svm_scale_tsc(vcpu, native_read_tsc());
  2468. break;
  2469. }
  2470. case MSR_STAR:
  2471. *data = svm->vmcb->save.star;
  2472. break;
  2473. #ifdef CONFIG_X86_64
  2474. case MSR_LSTAR:
  2475. *data = svm->vmcb->save.lstar;
  2476. break;
  2477. case MSR_CSTAR:
  2478. *data = svm->vmcb->save.cstar;
  2479. break;
  2480. case MSR_KERNEL_GS_BASE:
  2481. *data = svm->vmcb->save.kernel_gs_base;
  2482. break;
  2483. case MSR_SYSCALL_MASK:
  2484. *data = svm->vmcb->save.sfmask;
  2485. break;
  2486. #endif
  2487. case MSR_IA32_SYSENTER_CS:
  2488. *data = svm->vmcb->save.sysenter_cs;
  2489. break;
  2490. case MSR_IA32_SYSENTER_EIP:
  2491. *data = svm->sysenter_eip;
  2492. break;
  2493. case MSR_IA32_SYSENTER_ESP:
  2494. *data = svm->sysenter_esp;
  2495. break;
  2496. /*
  2497. * Nobody will change the following 5 values in the VMCB so we can
  2498. * safely return them on rdmsr. They will always be 0 until LBRV is
  2499. * implemented.
  2500. */
  2501. case MSR_IA32_DEBUGCTLMSR:
  2502. *data = svm->vmcb->save.dbgctl;
  2503. break;
  2504. case MSR_IA32_LASTBRANCHFROMIP:
  2505. *data = svm->vmcb->save.br_from;
  2506. break;
  2507. case MSR_IA32_LASTBRANCHTOIP:
  2508. *data = svm->vmcb->save.br_to;
  2509. break;
  2510. case MSR_IA32_LASTINTFROMIP:
  2511. *data = svm->vmcb->save.last_excp_from;
  2512. break;
  2513. case MSR_IA32_LASTINTTOIP:
  2514. *data = svm->vmcb->save.last_excp_to;
  2515. break;
  2516. case MSR_VM_HSAVE_PA:
  2517. *data = svm->nested.hsave_msr;
  2518. break;
  2519. case MSR_VM_CR:
  2520. *data = svm->nested.vm_cr_msr;
  2521. break;
  2522. case MSR_IA32_UCODE_REV:
  2523. *data = 0x01000065;
  2524. break;
  2525. default:
  2526. return kvm_get_msr_common(vcpu, ecx, data);
  2527. }
  2528. return 0;
  2529. }
  2530. static int rdmsr_interception(struct vcpu_svm *svm)
  2531. {
  2532. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  2533. u64 data;
  2534. if (svm_get_msr(&svm->vcpu, ecx, &data)) {
  2535. trace_kvm_msr_read_ex(ecx);
  2536. kvm_inject_gp(&svm->vcpu, 0);
  2537. } else {
  2538. trace_kvm_msr_read(ecx, data);
  2539. svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
  2540. svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
  2541. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  2542. skip_emulated_instruction(&svm->vcpu);
  2543. }
  2544. return 1;
  2545. }
  2546. static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
  2547. {
  2548. struct vcpu_svm *svm = to_svm(vcpu);
  2549. int svm_dis, chg_mask;
  2550. if (data & ~SVM_VM_CR_VALID_MASK)
  2551. return 1;
  2552. chg_mask = SVM_VM_CR_VALID_MASK;
  2553. if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
  2554. chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
  2555. svm->nested.vm_cr_msr &= ~chg_mask;
  2556. svm->nested.vm_cr_msr |= (data & chg_mask);
  2557. svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
  2558. /* check for svm_disable while efer.svme is set */
  2559. if (svm_dis && (vcpu->arch.efer & EFER_SVME))
  2560. return 1;
  2561. return 0;
  2562. }
  2563. static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
  2564. {
  2565. struct vcpu_svm *svm = to_svm(vcpu);
  2566. u32 ecx = msr->index;
  2567. u64 data = msr->data;
  2568. switch (ecx) {
  2569. case MSR_IA32_TSC:
  2570. kvm_write_tsc(vcpu, msr);
  2571. break;
  2572. case MSR_STAR:
  2573. svm->vmcb->save.star = data;
  2574. break;
  2575. #ifdef CONFIG_X86_64
  2576. case MSR_LSTAR:
  2577. svm->vmcb->save.lstar = data;
  2578. break;
  2579. case MSR_CSTAR:
  2580. svm->vmcb->save.cstar = data;
  2581. break;
  2582. case MSR_KERNEL_GS_BASE:
  2583. svm->vmcb->save.kernel_gs_base = data;
  2584. break;
  2585. case MSR_SYSCALL_MASK:
  2586. svm->vmcb->save.sfmask = data;
  2587. break;
  2588. #endif
  2589. case MSR_IA32_SYSENTER_CS:
  2590. svm->vmcb->save.sysenter_cs = data;
  2591. break;
  2592. case MSR_IA32_SYSENTER_EIP:
  2593. svm->sysenter_eip = data;
  2594. svm->vmcb->save.sysenter_eip = data;
  2595. break;
  2596. case MSR_IA32_SYSENTER_ESP:
  2597. svm->sysenter_esp = data;
  2598. svm->vmcb->save.sysenter_esp = data;
  2599. break;
  2600. case MSR_IA32_DEBUGCTLMSR:
  2601. if (!boot_cpu_has(X86_FEATURE_LBRV)) {
  2602. vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
  2603. __func__, data);
  2604. break;
  2605. }
  2606. if (data & DEBUGCTL_RESERVED_BITS)
  2607. return 1;
  2608. svm->vmcb->save.dbgctl = data;
  2609. mark_dirty(svm->vmcb, VMCB_LBR);
  2610. if (data & (1ULL<<0))
  2611. svm_enable_lbrv(svm);
  2612. else
  2613. svm_disable_lbrv(svm);
  2614. break;
  2615. case MSR_VM_HSAVE_PA:
  2616. svm->nested.hsave_msr = data;
  2617. break;
  2618. case MSR_VM_CR:
  2619. return svm_set_vm_cr(vcpu, data);
  2620. case MSR_VM_IGNNE:
  2621. vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
  2622. break;
  2623. default:
  2624. return kvm_set_msr_common(vcpu, msr);
  2625. }
  2626. return 0;
  2627. }
  2628. static int wrmsr_interception(struct vcpu_svm *svm)
  2629. {
  2630. struct msr_data msr;
  2631. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  2632. u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
  2633. | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  2634. msr.data = data;
  2635. msr.index = ecx;
  2636. msr.host_initiated = false;
  2637. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  2638. if (svm_set_msr(&svm->vcpu, &msr)) {
  2639. trace_kvm_msr_write_ex(ecx, data);
  2640. kvm_inject_gp(&svm->vcpu, 0);
  2641. } else {
  2642. trace_kvm_msr_write(ecx, data);
  2643. skip_emulated_instruction(&svm->vcpu);
  2644. }
  2645. return 1;
  2646. }
  2647. static int msr_interception(struct vcpu_svm *svm)
  2648. {
  2649. if (svm->vmcb->control.exit_info_1)
  2650. return wrmsr_interception(svm);
  2651. else
  2652. return rdmsr_interception(svm);
  2653. }
  2654. static int interrupt_window_interception(struct vcpu_svm *svm)
  2655. {
  2656. struct kvm_run *kvm_run = svm->vcpu.run;
  2657. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  2658. svm_clear_vintr(svm);
  2659. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  2660. mark_dirty(svm->vmcb, VMCB_INTR);
  2661. ++svm->vcpu.stat.irq_window_exits;
  2662. /*
  2663. * If the user space waits to inject interrupts, exit as soon as
  2664. * possible
  2665. */
  2666. if (!irqchip_in_kernel(svm->vcpu.kvm) &&
  2667. kvm_run->request_interrupt_window &&
  2668. !kvm_cpu_has_interrupt(&svm->vcpu)) {
  2669. kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  2670. return 0;
  2671. }
  2672. return 1;
  2673. }
  2674. static int pause_interception(struct vcpu_svm *svm)
  2675. {
  2676. kvm_vcpu_on_spin(&(svm->vcpu));
  2677. return 1;
  2678. }
  2679. static int nop_interception(struct vcpu_svm *svm)
  2680. {
  2681. skip_emulated_instruction(&(svm->vcpu));
  2682. return 1;
  2683. }
  2684. static int monitor_interception(struct vcpu_svm *svm)
  2685. {
  2686. printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
  2687. return nop_interception(svm);
  2688. }
  2689. static int mwait_interception(struct vcpu_svm *svm)
  2690. {
  2691. printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
  2692. return nop_interception(svm);
  2693. }
  2694. static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
  2695. [SVM_EXIT_READ_CR0] = cr_interception,
  2696. [SVM_EXIT_READ_CR3] = cr_interception,
  2697. [SVM_EXIT_READ_CR4] = cr_interception,
  2698. [SVM_EXIT_READ_CR8] = cr_interception,
  2699. [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
  2700. [SVM_EXIT_WRITE_CR0] = cr_interception,
  2701. [SVM_EXIT_WRITE_CR3] = cr_interception,
  2702. [SVM_EXIT_WRITE_CR4] = cr_interception,
  2703. [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
  2704. [SVM_EXIT_READ_DR0] = dr_interception,
  2705. [SVM_EXIT_READ_DR1] = dr_interception,
  2706. [SVM_EXIT_READ_DR2] = dr_interception,
  2707. [SVM_EXIT_READ_DR3] = dr_interception,
  2708. [SVM_EXIT_READ_DR4] = dr_interception,
  2709. [SVM_EXIT_READ_DR5] = dr_interception,
  2710. [SVM_EXIT_READ_DR6] = dr_interception,
  2711. [SVM_EXIT_READ_DR7] = dr_interception,
  2712. [SVM_EXIT_WRITE_DR0] = dr_interception,
  2713. [SVM_EXIT_WRITE_DR1] = dr_interception,
  2714. [SVM_EXIT_WRITE_DR2] = dr_interception,
  2715. [SVM_EXIT_WRITE_DR3] = dr_interception,
  2716. [SVM_EXIT_WRITE_DR4] = dr_interception,
  2717. [SVM_EXIT_WRITE_DR5] = dr_interception,
  2718. [SVM_EXIT_WRITE_DR6] = dr_interception,
  2719. [SVM_EXIT_WRITE_DR7] = dr_interception,
  2720. [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
  2721. [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
  2722. [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
  2723. [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
  2724. [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
  2725. [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
  2726. [SVM_EXIT_INTR] = intr_interception,
  2727. [SVM_EXIT_NMI] = nmi_interception,
  2728. [SVM_EXIT_SMI] = nop_on_interception,
  2729. [SVM_EXIT_INIT] = nop_on_interception,
  2730. [SVM_EXIT_VINTR] = interrupt_window_interception,
  2731. [SVM_EXIT_RDPMC] = rdpmc_interception,
  2732. [SVM_EXIT_CPUID] = cpuid_interception,
  2733. [SVM_EXIT_IRET] = iret_interception,
  2734. [SVM_EXIT_INVD] = emulate_on_interception,
  2735. [SVM_EXIT_PAUSE] = pause_interception,
  2736. [SVM_EXIT_HLT] = halt_interception,
  2737. [SVM_EXIT_INVLPG] = invlpg_interception,
  2738. [SVM_EXIT_INVLPGA] = invlpga_interception,
  2739. [SVM_EXIT_IOIO] = io_interception,
  2740. [SVM_EXIT_MSR] = msr_interception,
  2741. [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
  2742. [SVM_EXIT_SHUTDOWN] = shutdown_interception,
  2743. [SVM_EXIT_VMRUN] = vmrun_interception,
  2744. [SVM_EXIT_VMMCALL] = vmmcall_interception,
  2745. [SVM_EXIT_VMLOAD] = vmload_interception,
  2746. [SVM_EXIT_VMSAVE] = vmsave_interception,
  2747. [SVM_EXIT_STGI] = stgi_interception,
  2748. [SVM_EXIT_CLGI] = clgi_interception,
  2749. [SVM_EXIT_SKINIT] = skinit_interception,
  2750. [SVM_EXIT_WBINVD] = emulate_on_interception,
  2751. [SVM_EXIT_MONITOR] = monitor_interception,
  2752. [SVM_EXIT_MWAIT] = mwait_interception,
  2753. [SVM_EXIT_XSETBV] = xsetbv_interception,
  2754. [SVM_EXIT_NPF] = pf_interception,
  2755. };
  2756. static void dump_vmcb(struct kvm_vcpu *vcpu)
  2757. {
  2758. struct vcpu_svm *svm = to_svm(vcpu);
  2759. struct vmcb_control_area *control = &svm->vmcb->control;
  2760. struct vmcb_save_area *save = &svm->vmcb->save;
  2761. pr_err("VMCB Control Area:\n");
  2762. pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
  2763. pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
  2764. pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
  2765. pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
  2766. pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
  2767. pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
  2768. pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
  2769. pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
  2770. pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
  2771. pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
  2772. pr_err("%-20s%d\n", "asid:", control->asid);
  2773. pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
  2774. pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
  2775. pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
  2776. pr_err("%-20s%08x\n", "int_state:", control->int_state);
  2777. pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
  2778. pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
  2779. pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
  2780. pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
  2781. pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
  2782. pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
  2783. pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
  2784. pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
  2785. pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
  2786. pr_err("%-20s%lld\n", "lbr_ctl:", control->lbr_ctl);
  2787. pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
  2788. pr_err("VMCB State Save Area:\n");
  2789. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  2790. "es:",
  2791. save->es.selector, save->es.attrib,
  2792. save->es.limit, save->es.base);
  2793. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  2794. "cs:",
  2795. save->cs.selector, save->cs.attrib,
  2796. save->cs.limit, save->cs.base);
  2797. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  2798. "ss:",
  2799. save->ss.selector, save->ss.attrib,
  2800. save->ss.limit, save->ss.base);
  2801. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  2802. "ds:",
  2803. save->ds.selector, save->ds.attrib,
  2804. save->ds.limit, save->ds.base);
  2805. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  2806. "fs:",
  2807. save->fs.selector, save->fs.attrib,
  2808. save->fs.limit, save->fs.base);
  2809. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  2810. "gs:",
  2811. save->gs.selector, save->gs.attrib,
  2812. save->gs.limit, save->gs.base);
  2813. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  2814. "gdtr:",
  2815. save->gdtr.selector, save->gdtr.attrib,
  2816. save->gdtr.limit, save->gdtr.base);
  2817. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  2818. "ldtr:",
  2819. save->ldtr.selector, save->ldtr.attrib,
  2820. save->ldtr.limit, save->ldtr.base);
  2821. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  2822. "idtr:",
  2823. save->idtr.selector, save->idtr.attrib,
  2824. save->idtr.limit, save->idtr.base);
  2825. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  2826. "tr:",
  2827. save->tr.selector, save->tr.attrib,
  2828. save->tr.limit, save->tr.base);
  2829. pr_err("cpl: %d efer: %016llx\n",
  2830. save->cpl, save->efer);
  2831. pr_err("%-15s %016llx %-13s %016llx\n",
  2832. "cr0:", save->cr0, "cr2:", save->cr2);
  2833. pr_err("%-15s %016llx %-13s %016llx\n",
  2834. "cr3:", save->cr3, "cr4:", save->cr4);
  2835. pr_err("%-15s %016llx %-13s %016llx\n",
  2836. "dr6:", save->dr6, "dr7:", save->dr7);
  2837. pr_err("%-15s %016llx %-13s %016llx\n",
  2838. "rip:", save->rip, "rflags:", save->rflags);
  2839. pr_err("%-15s %016llx %-13s %016llx\n",
  2840. "rsp:", save->rsp, "rax:", save->rax);
  2841. pr_err("%-15s %016llx %-13s %016llx\n",
  2842. "star:", save->star, "lstar:", save->lstar);
  2843. pr_err("%-15s %016llx %-13s %016llx\n",
  2844. "cstar:", save->cstar, "sfmask:", save->sfmask);
  2845. pr_err("%-15s %016llx %-13s %016llx\n",
  2846. "kernel_gs_base:", save->kernel_gs_base,
  2847. "sysenter_cs:", save->sysenter_cs);
  2848. pr_err("%-15s %016llx %-13s %016llx\n",
  2849. "sysenter_esp:", save->sysenter_esp,
  2850. "sysenter_eip:", save->sysenter_eip);
  2851. pr_err("%-15s %016llx %-13s %016llx\n",
  2852. "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
  2853. pr_err("%-15s %016llx %-13s %016llx\n",
  2854. "br_from:", save->br_from, "br_to:", save->br_to);
  2855. pr_err("%-15s %016llx %-13s %016llx\n",
  2856. "excp_from:", save->last_excp_from,
  2857. "excp_to:", save->last_excp_to);
  2858. }
  2859. static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
  2860. {
  2861. struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
  2862. *info1 = control->exit_info_1;
  2863. *info2 = control->exit_info_2;
  2864. }
  2865. static int handle_exit(struct kvm_vcpu *vcpu)
  2866. {
  2867. struct vcpu_svm *svm = to_svm(vcpu);
  2868. struct kvm_run *kvm_run = vcpu->run;
  2869. u32 exit_code = svm->vmcb->control.exit_code;
  2870. if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
  2871. vcpu->arch.cr0 = svm->vmcb->save.cr0;
  2872. if (npt_enabled)
  2873. vcpu->arch.cr3 = svm->vmcb->save.cr3;
  2874. if (unlikely(svm->nested.exit_required)) {
  2875. nested_svm_vmexit(svm);
  2876. svm->nested.exit_required = false;
  2877. return 1;
  2878. }
  2879. if (is_guest_mode(vcpu)) {
  2880. int vmexit;
  2881. trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
  2882. svm->vmcb->control.exit_info_1,
  2883. svm->vmcb->control.exit_info_2,
  2884. svm->vmcb->control.exit_int_info,
  2885. svm->vmcb->control.exit_int_info_err,
  2886. KVM_ISA_SVM);
  2887. vmexit = nested_svm_exit_special(svm);
  2888. if (vmexit == NESTED_EXIT_CONTINUE)
  2889. vmexit = nested_svm_exit_handled(svm);
  2890. if (vmexit == NESTED_EXIT_DONE)
  2891. return 1;
  2892. }
  2893. svm_complete_interrupts(svm);
  2894. if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
  2895. kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  2896. kvm_run->fail_entry.hardware_entry_failure_reason
  2897. = svm->vmcb->control.exit_code;
  2898. pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
  2899. dump_vmcb(vcpu);
  2900. return 0;
  2901. }
  2902. if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
  2903. exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
  2904. exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
  2905. exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
  2906. printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
  2907. "exit_code 0x%x\n",
  2908. __func__, svm->vmcb->control.exit_int_info,
  2909. exit_code);
  2910. if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
  2911. || !svm_exit_handlers[exit_code]) {
  2912. kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
  2913. kvm_run->hw.hardware_exit_reason = exit_code;
  2914. return 0;
  2915. }
  2916. return svm_exit_handlers[exit_code](svm);
  2917. }
  2918. static void reload_tss(struct kvm_vcpu *vcpu)
  2919. {
  2920. int cpu = raw_smp_processor_id();
  2921. struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
  2922. sd->tss_desc->type = 9; /* available 32/64-bit TSS */
  2923. load_TR_desc();
  2924. }
  2925. static void pre_svm_run(struct vcpu_svm *svm)
  2926. {
  2927. int cpu = raw_smp_processor_id();
  2928. struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
  2929. /* FIXME: handle wraparound of asid_generation */
  2930. if (svm->asid_generation != sd->asid_generation)
  2931. new_asid(svm, sd);
  2932. }
  2933. static void svm_inject_nmi(struct kvm_vcpu *vcpu)
  2934. {
  2935. struct vcpu_svm *svm = to_svm(vcpu);
  2936. svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
  2937. vcpu->arch.hflags |= HF_NMI_MASK;
  2938. set_intercept(svm, INTERCEPT_IRET);
  2939. ++vcpu->stat.nmi_injections;
  2940. }
  2941. static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
  2942. {
  2943. struct vmcb_control_area *control;
  2944. control = &svm->vmcb->control;
  2945. control->int_vector = irq;
  2946. control->int_ctl &= ~V_INTR_PRIO_MASK;
  2947. control->int_ctl |= V_IRQ_MASK |
  2948. ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
  2949. mark_dirty(svm->vmcb, VMCB_INTR);
  2950. }
  2951. static void svm_set_irq(struct kvm_vcpu *vcpu)
  2952. {
  2953. struct vcpu_svm *svm = to_svm(vcpu);
  2954. BUG_ON(!(gif_set(svm)));
  2955. trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
  2956. ++vcpu->stat.irq_injections;
  2957. svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
  2958. SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
  2959. }
  2960. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  2961. {
  2962. struct vcpu_svm *svm = to_svm(vcpu);
  2963. if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
  2964. return;
  2965. clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  2966. if (irr == -1)
  2967. return;
  2968. if (tpr >= irr)
  2969. set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  2970. }
  2971. static void svm_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
  2972. {
  2973. return;
  2974. }
  2975. static int svm_vm_has_apicv(struct kvm *kvm)
  2976. {
  2977. return 0;
  2978. }
  2979. static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
  2980. {
  2981. return;
  2982. }
  2983. static void svm_hwapic_isr_update(struct kvm *kvm, int isr)
  2984. {
  2985. return;
  2986. }
  2987. static void svm_sync_pir_to_irr(struct kvm_vcpu *vcpu)
  2988. {
  2989. return;
  2990. }
  2991. static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
  2992. {
  2993. struct vcpu_svm *svm = to_svm(vcpu);
  2994. struct vmcb *vmcb = svm->vmcb;
  2995. int ret;
  2996. ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
  2997. !(svm->vcpu.arch.hflags & HF_NMI_MASK);
  2998. ret = ret && gif_set(svm) && nested_svm_nmi(svm);
  2999. return ret;
  3000. }
  3001. static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
  3002. {
  3003. struct vcpu_svm *svm = to_svm(vcpu);
  3004. return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
  3005. }
  3006. static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  3007. {
  3008. struct vcpu_svm *svm = to_svm(vcpu);
  3009. if (masked) {
  3010. svm->vcpu.arch.hflags |= HF_NMI_MASK;
  3011. set_intercept(svm, INTERCEPT_IRET);
  3012. } else {
  3013. svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
  3014. clr_intercept(svm, INTERCEPT_IRET);
  3015. }
  3016. }
  3017. static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
  3018. {
  3019. struct vcpu_svm *svm = to_svm(vcpu);
  3020. struct vmcb *vmcb = svm->vmcb;
  3021. int ret;
  3022. if (!gif_set(svm) ||
  3023. (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
  3024. return 0;
  3025. ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
  3026. if (is_guest_mode(vcpu))
  3027. return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
  3028. return ret;
  3029. }
  3030. static void enable_irq_window(struct kvm_vcpu *vcpu)
  3031. {
  3032. struct vcpu_svm *svm = to_svm(vcpu);
  3033. /*
  3034. * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
  3035. * 1, because that's a separate STGI/VMRUN intercept. The next time we
  3036. * get that intercept, this function will be called again though and
  3037. * we'll get the vintr intercept.
  3038. */
  3039. if (gif_set(svm) && nested_svm_intr(svm)) {
  3040. svm_set_vintr(svm);
  3041. svm_inject_irq(svm, 0x0);
  3042. }
  3043. }
  3044. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  3045. {
  3046. struct vcpu_svm *svm = to_svm(vcpu);
  3047. if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
  3048. == HF_NMI_MASK)
  3049. return; /* IRET will cause a vm exit */
  3050. /*
  3051. * Something prevents NMI from been injected. Single step over possible
  3052. * problem (IRET or exception injection or interrupt shadow)
  3053. */
  3054. svm->nmi_singlestep = true;
  3055. svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
  3056. update_db_bp_intercept(vcpu);
  3057. }
  3058. static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
  3059. {
  3060. return 0;
  3061. }
  3062. static void svm_flush_tlb(struct kvm_vcpu *vcpu)
  3063. {
  3064. struct vcpu_svm *svm = to_svm(vcpu);
  3065. if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
  3066. svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
  3067. else
  3068. svm->asid_generation--;
  3069. }
  3070. static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
  3071. {
  3072. }
  3073. static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
  3074. {
  3075. struct vcpu_svm *svm = to_svm(vcpu);
  3076. if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
  3077. return;
  3078. if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
  3079. int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
  3080. kvm_set_cr8(vcpu, cr8);
  3081. }
  3082. }
  3083. static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
  3084. {
  3085. struct vcpu_svm *svm = to_svm(vcpu);
  3086. u64 cr8;
  3087. if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
  3088. return;
  3089. cr8 = kvm_get_cr8(vcpu);
  3090. svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
  3091. svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
  3092. }
  3093. static void svm_complete_interrupts(struct vcpu_svm *svm)
  3094. {
  3095. u8 vector;
  3096. int type;
  3097. u32 exitintinfo = svm->vmcb->control.exit_int_info;
  3098. unsigned int3_injected = svm->int3_injected;
  3099. svm->int3_injected = 0;
  3100. /*
  3101. * If we've made progress since setting HF_IRET_MASK, we've
  3102. * executed an IRET and can allow NMI injection.
  3103. */
  3104. if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
  3105. && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
  3106. svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
  3107. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  3108. }
  3109. svm->vcpu.arch.nmi_injected = false;
  3110. kvm_clear_exception_queue(&svm->vcpu);
  3111. kvm_clear_interrupt_queue(&svm->vcpu);
  3112. if (!(exitintinfo & SVM_EXITINTINFO_VALID))
  3113. return;
  3114. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  3115. vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
  3116. type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
  3117. switch (type) {
  3118. case SVM_EXITINTINFO_TYPE_NMI:
  3119. svm->vcpu.arch.nmi_injected = true;
  3120. break;
  3121. case SVM_EXITINTINFO_TYPE_EXEPT:
  3122. /*
  3123. * In case of software exceptions, do not reinject the vector,
  3124. * but re-execute the instruction instead. Rewind RIP first
  3125. * if we emulated INT3 before.
  3126. */
  3127. if (kvm_exception_is_soft(vector)) {
  3128. if (vector == BP_VECTOR && int3_injected &&
  3129. kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
  3130. kvm_rip_write(&svm->vcpu,
  3131. kvm_rip_read(&svm->vcpu) -
  3132. int3_injected);
  3133. break;
  3134. }
  3135. if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
  3136. u32 err = svm->vmcb->control.exit_int_info_err;
  3137. kvm_requeue_exception_e(&svm->vcpu, vector, err);
  3138. } else
  3139. kvm_requeue_exception(&svm->vcpu, vector);
  3140. break;
  3141. case SVM_EXITINTINFO_TYPE_INTR:
  3142. kvm_queue_interrupt(&svm->vcpu, vector, false);
  3143. break;
  3144. default:
  3145. break;
  3146. }
  3147. }
  3148. static void svm_cancel_injection(struct kvm_vcpu *vcpu)
  3149. {
  3150. struct vcpu_svm *svm = to_svm(vcpu);
  3151. struct vmcb_control_area *control = &svm->vmcb->control;
  3152. control->exit_int_info = control->event_inj;
  3153. control->exit_int_info_err = control->event_inj_err;
  3154. control->event_inj = 0;
  3155. svm_complete_interrupts(svm);
  3156. }
  3157. static void svm_vcpu_run(struct kvm_vcpu *vcpu)
  3158. {
  3159. struct vcpu_svm *svm = to_svm(vcpu);
  3160. svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
  3161. svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
  3162. svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
  3163. /*
  3164. * A vmexit emulation is required before the vcpu can be executed
  3165. * again.
  3166. */
  3167. if (unlikely(svm->nested.exit_required))
  3168. return;
  3169. pre_svm_run(svm);
  3170. sync_lapic_to_cr8(vcpu);
  3171. svm->vmcb->save.cr2 = vcpu->arch.cr2;
  3172. clgi();
  3173. local_irq_enable();
  3174. asm volatile (
  3175. "push %%" _ASM_BP "; \n\t"
  3176. "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t"
  3177. "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t"
  3178. "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t"
  3179. "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t"
  3180. "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t"
  3181. "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t"
  3182. #ifdef CONFIG_X86_64
  3183. "mov %c[r8](%[svm]), %%r8 \n\t"
  3184. "mov %c[r9](%[svm]), %%r9 \n\t"
  3185. "mov %c[r10](%[svm]), %%r10 \n\t"
  3186. "mov %c[r11](%[svm]), %%r11 \n\t"
  3187. "mov %c[r12](%[svm]), %%r12 \n\t"
  3188. "mov %c[r13](%[svm]), %%r13 \n\t"
  3189. "mov %c[r14](%[svm]), %%r14 \n\t"
  3190. "mov %c[r15](%[svm]), %%r15 \n\t"
  3191. #endif
  3192. /* Enter guest mode */
  3193. "push %%" _ASM_AX " \n\t"
  3194. "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t"
  3195. __ex(SVM_VMLOAD) "\n\t"
  3196. __ex(SVM_VMRUN) "\n\t"
  3197. __ex(SVM_VMSAVE) "\n\t"
  3198. "pop %%" _ASM_AX " \n\t"
  3199. /* Save guest registers, load host registers */
  3200. "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t"
  3201. "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t"
  3202. "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t"
  3203. "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t"
  3204. "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t"
  3205. "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t"
  3206. #ifdef CONFIG_X86_64
  3207. "mov %%r8, %c[r8](%[svm]) \n\t"
  3208. "mov %%r9, %c[r9](%[svm]) \n\t"
  3209. "mov %%r10, %c[r10](%[svm]) \n\t"
  3210. "mov %%r11, %c[r11](%[svm]) \n\t"
  3211. "mov %%r12, %c[r12](%[svm]) \n\t"
  3212. "mov %%r13, %c[r13](%[svm]) \n\t"
  3213. "mov %%r14, %c[r14](%[svm]) \n\t"
  3214. "mov %%r15, %c[r15](%[svm]) \n\t"
  3215. #endif
  3216. "pop %%" _ASM_BP
  3217. :
  3218. : [svm]"a"(svm),
  3219. [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
  3220. [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
  3221. [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
  3222. [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
  3223. [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
  3224. [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
  3225. [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
  3226. #ifdef CONFIG_X86_64
  3227. , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
  3228. [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
  3229. [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
  3230. [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
  3231. [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
  3232. [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
  3233. [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
  3234. [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
  3235. #endif
  3236. : "cc", "memory"
  3237. #ifdef CONFIG_X86_64
  3238. , "rbx", "rcx", "rdx", "rsi", "rdi"
  3239. , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
  3240. #else
  3241. , "ebx", "ecx", "edx", "esi", "edi"
  3242. #endif
  3243. );
  3244. #ifdef CONFIG_X86_64
  3245. wrmsrl(MSR_GS_BASE, svm->host.gs_base);
  3246. #else
  3247. loadsegment(fs, svm->host.fs);
  3248. #ifndef CONFIG_X86_32_LAZY_GS
  3249. loadsegment(gs, svm->host.gs);
  3250. #endif
  3251. #endif
  3252. reload_tss(vcpu);
  3253. local_irq_disable();
  3254. vcpu->arch.cr2 = svm->vmcb->save.cr2;
  3255. vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
  3256. vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
  3257. vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
  3258. trace_kvm_exit(svm->vmcb->control.exit_code, vcpu, KVM_ISA_SVM);
  3259. if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
  3260. kvm_before_handle_nmi(&svm->vcpu);
  3261. stgi();
  3262. /* Any pending NMI will happen here */
  3263. if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
  3264. kvm_after_handle_nmi(&svm->vcpu);
  3265. sync_cr8_to_lapic(vcpu);
  3266. svm->next_rip = 0;
  3267. svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
  3268. /* if exit due to PF check for async PF */
  3269. if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
  3270. svm->apf_reason = kvm_read_and_reset_pf_reason();
  3271. if (npt_enabled) {
  3272. vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
  3273. vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
  3274. }
  3275. /*
  3276. * We need to handle MC intercepts here before the vcpu has a chance to
  3277. * change the physical cpu
  3278. */
  3279. if (unlikely(svm->vmcb->control.exit_code ==
  3280. SVM_EXIT_EXCP_BASE + MC_VECTOR))
  3281. svm_handle_mce(svm);
  3282. mark_all_clean(svm->vmcb);
  3283. }
  3284. static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  3285. {
  3286. struct vcpu_svm *svm = to_svm(vcpu);
  3287. svm->vmcb->save.cr3 = root;
  3288. mark_dirty(svm->vmcb, VMCB_CR);
  3289. svm_flush_tlb(vcpu);
  3290. }
  3291. static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  3292. {
  3293. struct vcpu_svm *svm = to_svm(vcpu);
  3294. svm->vmcb->control.nested_cr3 = root;
  3295. mark_dirty(svm->vmcb, VMCB_NPT);
  3296. /* Also sync guest cr3 here in case we live migrate */
  3297. svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
  3298. mark_dirty(svm->vmcb, VMCB_CR);
  3299. svm_flush_tlb(vcpu);
  3300. }
  3301. static int is_disabled(void)
  3302. {
  3303. u64 vm_cr;
  3304. rdmsrl(MSR_VM_CR, vm_cr);
  3305. if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
  3306. return 1;
  3307. return 0;
  3308. }
  3309. static void
  3310. svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  3311. {
  3312. /*
  3313. * Patch in the VMMCALL instruction:
  3314. */
  3315. hypercall[0] = 0x0f;
  3316. hypercall[1] = 0x01;
  3317. hypercall[2] = 0xd9;
  3318. }
  3319. static void svm_check_processor_compat(void *rtn)
  3320. {
  3321. *(int *)rtn = 0;
  3322. }
  3323. static bool svm_cpu_has_accelerated_tpr(void)
  3324. {
  3325. return false;
  3326. }
  3327. static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  3328. {
  3329. return 0;
  3330. }
  3331. static void svm_cpuid_update(struct kvm_vcpu *vcpu)
  3332. {
  3333. }
  3334. static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
  3335. {
  3336. switch (func) {
  3337. case 0x80000001:
  3338. if (nested)
  3339. entry->ecx |= (1 << 2); /* Set SVM bit */
  3340. break;
  3341. case 0x8000000A:
  3342. entry->eax = 1; /* SVM revision 1 */
  3343. entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
  3344. ASID emulation to nested SVM */
  3345. entry->ecx = 0; /* Reserved */
  3346. entry->edx = 0; /* Per default do not support any
  3347. additional features */
  3348. /* Support next_rip if host supports it */
  3349. if (boot_cpu_has(X86_FEATURE_NRIPS))
  3350. entry->edx |= SVM_FEATURE_NRIP;
  3351. /* Support NPT for the guest if enabled */
  3352. if (npt_enabled)
  3353. entry->edx |= SVM_FEATURE_NPT;
  3354. break;
  3355. }
  3356. }
  3357. static int svm_get_lpage_level(void)
  3358. {
  3359. return PT_PDPE_LEVEL;
  3360. }
  3361. static bool svm_rdtscp_supported(void)
  3362. {
  3363. return false;
  3364. }
  3365. static bool svm_invpcid_supported(void)
  3366. {
  3367. return false;
  3368. }
  3369. static bool svm_mpx_supported(void)
  3370. {
  3371. return false;
  3372. }
  3373. static bool svm_has_wbinvd_exit(void)
  3374. {
  3375. return true;
  3376. }
  3377. static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
  3378. {
  3379. struct vcpu_svm *svm = to_svm(vcpu);
  3380. set_exception_intercept(svm, NM_VECTOR);
  3381. update_cr0_intercept(svm);
  3382. }
  3383. #define PRE_EX(exit) { .exit_code = (exit), \
  3384. .stage = X86_ICPT_PRE_EXCEPT, }
  3385. #define POST_EX(exit) { .exit_code = (exit), \
  3386. .stage = X86_ICPT_POST_EXCEPT, }
  3387. #define POST_MEM(exit) { .exit_code = (exit), \
  3388. .stage = X86_ICPT_POST_MEMACCESS, }
  3389. static const struct __x86_intercept {
  3390. u32 exit_code;
  3391. enum x86_intercept_stage stage;
  3392. } x86_intercept_map[] = {
  3393. [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
  3394. [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
  3395. [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
  3396. [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
  3397. [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
  3398. [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
  3399. [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
  3400. [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
  3401. [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
  3402. [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
  3403. [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
  3404. [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
  3405. [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
  3406. [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
  3407. [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
  3408. [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
  3409. [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
  3410. [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
  3411. [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
  3412. [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
  3413. [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
  3414. [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
  3415. [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
  3416. [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
  3417. [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
  3418. [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
  3419. [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
  3420. [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
  3421. [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
  3422. [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
  3423. [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
  3424. [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
  3425. [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
  3426. [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
  3427. [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
  3428. [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
  3429. [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
  3430. [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
  3431. [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
  3432. [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
  3433. [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
  3434. [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
  3435. [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
  3436. [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
  3437. [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
  3438. [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
  3439. };
  3440. #undef PRE_EX
  3441. #undef POST_EX
  3442. #undef POST_MEM
  3443. static int svm_check_intercept(struct kvm_vcpu *vcpu,
  3444. struct x86_instruction_info *info,
  3445. enum x86_intercept_stage stage)
  3446. {
  3447. struct vcpu_svm *svm = to_svm(vcpu);
  3448. int vmexit, ret = X86EMUL_CONTINUE;
  3449. struct __x86_intercept icpt_info;
  3450. struct vmcb *vmcb = svm->vmcb;
  3451. if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
  3452. goto out;
  3453. icpt_info = x86_intercept_map[info->intercept];
  3454. if (stage != icpt_info.stage)
  3455. goto out;
  3456. switch (icpt_info.exit_code) {
  3457. case SVM_EXIT_READ_CR0:
  3458. if (info->intercept == x86_intercept_cr_read)
  3459. icpt_info.exit_code += info->modrm_reg;
  3460. break;
  3461. case SVM_EXIT_WRITE_CR0: {
  3462. unsigned long cr0, val;
  3463. u64 intercept;
  3464. if (info->intercept == x86_intercept_cr_write)
  3465. icpt_info.exit_code += info->modrm_reg;
  3466. if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
  3467. info->intercept == x86_intercept_clts)
  3468. break;
  3469. intercept = svm->nested.intercept;
  3470. if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
  3471. break;
  3472. cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
  3473. val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
  3474. if (info->intercept == x86_intercept_lmsw) {
  3475. cr0 &= 0xfUL;
  3476. val &= 0xfUL;
  3477. /* lmsw can't clear PE - catch this here */
  3478. if (cr0 & X86_CR0_PE)
  3479. val |= X86_CR0_PE;
  3480. }
  3481. if (cr0 ^ val)
  3482. icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
  3483. break;
  3484. }
  3485. case SVM_EXIT_READ_DR0:
  3486. case SVM_EXIT_WRITE_DR0:
  3487. icpt_info.exit_code += info->modrm_reg;
  3488. break;
  3489. case SVM_EXIT_MSR:
  3490. if (info->intercept == x86_intercept_wrmsr)
  3491. vmcb->control.exit_info_1 = 1;
  3492. else
  3493. vmcb->control.exit_info_1 = 0;
  3494. break;
  3495. case SVM_EXIT_PAUSE:
  3496. /*
  3497. * We get this for NOP only, but pause
  3498. * is rep not, check this here
  3499. */
  3500. if (info->rep_prefix != REPE_PREFIX)
  3501. goto out;
  3502. case SVM_EXIT_IOIO: {
  3503. u64 exit_info;
  3504. u32 bytes;
  3505. if (info->intercept == x86_intercept_in ||
  3506. info->intercept == x86_intercept_ins) {
  3507. exit_info = ((info->src_val & 0xffff) << 16) |
  3508. SVM_IOIO_TYPE_MASK;
  3509. bytes = info->dst_bytes;
  3510. } else {
  3511. exit_info = (info->dst_val & 0xffff) << 16;
  3512. bytes = info->src_bytes;
  3513. }
  3514. if (info->intercept == x86_intercept_outs ||
  3515. info->intercept == x86_intercept_ins)
  3516. exit_info |= SVM_IOIO_STR_MASK;
  3517. if (info->rep_prefix)
  3518. exit_info |= SVM_IOIO_REP_MASK;
  3519. bytes = min(bytes, 4u);
  3520. exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
  3521. exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
  3522. vmcb->control.exit_info_1 = exit_info;
  3523. vmcb->control.exit_info_2 = info->next_rip;
  3524. break;
  3525. }
  3526. default:
  3527. break;
  3528. }
  3529. vmcb->control.next_rip = info->next_rip;
  3530. vmcb->control.exit_code = icpt_info.exit_code;
  3531. vmexit = nested_svm_exit_handled(svm);
  3532. ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
  3533. : X86EMUL_CONTINUE;
  3534. out:
  3535. return ret;
  3536. }
  3537. static void svm_handle_external_intr(struct kvm_vcpu *vcpu)
  3538. {
  3539. local_irq_enable();
  3540. }
  3541. static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
  3542. {
  3543. }
  3544. static struct kvm_x86_ops svm_x86_ops = {
  3545. .cpu_has_kvm_support = has_svm,
  3546. .disabled_by_bios = is_disabled,
  3547. .hardware_setup = svm_hardware_setup,
  3548. .hardware_unsetup = svm_hardware_unsetup,
  3549. .check_processor_compatibility = svm_check_processor_compat,
  3550. .hardware_enable = svm_hardware_enable,
  3551. .hardware_disable = svm_hardware_disable,
  3552. .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
  3553. .vcpu_create = svm_create_vcpu,
  3554. .vcpu_free = svm_free_vcpu,
  3555. .vcpu_reset = svm_vcpu_reset,
  3556. .prepare_guest_switch = svm_prepare_guest_switch,
  3557. .vcpu_load = svm_vcpu_load,
  3558. .vcpu_put = svm_vcpu_put,
  3559. .update_db_bp_intercept = update_db_bp_intercept,
  3560. .get_msr = svm_get_msr,
  3561. .set_msr = svm_set_msr,
  3562. .get_segment_base = svm_get_segment_base,
  3563. .get_segment = svm_get_segment,
  3564. .set_segment = svm_set_segment,
  3565. .get_cpl = svm_get_cpl,
  3566. .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
  3567. .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
  3568. .decache_cr3 = svm_decache_cr3,
  3569. .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
  3570. .set_cr0 = svm_set_cr0,
  3571. .set_cr3 = svm_set_cr3,
  3572. .set_cr4 = svm_set_cr4,
  3573. .set_efer = svm_set_efer,
  3574. .get_idt = svm_get_idt,
  3575. .set_idt = svm_set_idt,
  3576. .get_gdt = svm_get_gdt,
  3577. .set_gdt = svm_set_gdt,
  3578. .get_dr6 = svm_get_dr6,
  3579. .set_dr6 = svm_set_dr6,
  3580. .set_dr7 = svm_set_dr7,
  3581. .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
  3582. .cache_reg = svm_cache_reg,
  3583. .get_rflags = svm_get_rflags,
  3584. .set_rflags = svm_set_rflags,
  3585. .fpu_deactivate = svm_fpu_deactivate,
  3586. .tlb_flush = svm_flush_tlb,
  3587. .run = svm_vcpu_run,
  3588. .handle_exit = handle_exit,
  3589. .skip_emulated_instruction = skip_emulated_instruction,
  3590. .set_interrupt_shadow = svm_set_interrupt_shadow,
  3591. .get_interrupt_shadow = svm_get_interrupt_shadow,
  3592. .patch_hypercall = svm_patch_hypercall,
  3593. .set_irq = svm_set_irq,
  3594. .set_nmi = svm_inject_nmi,
  3595. .queue_exception = svm_queue_exception,
  3596. .cancel_injection = svm_cancel_injection,
  3597. .interrupt_allowed = svm_interrupt_allowed,
  3598. .nmi_allowed = svm_nmi_allowed,
  3599. .get_nmi_mask = svm_get_nmi_mask,
  3600. .set_nmi_mask = svm_set_nmi_mask,
  3601. .enable_nmi_window = enable_nmi_window,
  3602. .enable_irq_window = enable_irq_window,
  3603. .update_cr8_intercept = update_cr8_intercept,
  3604. .set_virtual_x2apic_mode = svm_set_virtual_x2apic_mode,
  3605. .vm_has_apicv = svm_vm_has_apicv,
  3606. .load_eoi_exitmap = svm_load_eoi_exitmap,
  3607. .hwapic_isr_update = svm_hwapic_isr_update,
  3608. .sync_pir_to_irr = svm_sync_pir_to_irr,
  3609. .set_tss_addr = svm_set_tss_addr,
  3610. .get_tdp_level = get_npt_level,
  3611. .get_mt_mask = svm_get_mt_mask,
  3612. .get_exit_info = svm_get_exit_info,
  3613. .get_lpage_level = svm_get_lpage_level,
  3614. .cpuid_update = svm_cpuid_update,
  3615. .rdtscp_supported = svm_rdtscp_supported,
  3616. .invpcid_supported = svm_invpcid_supported,
  3617. .mpx_supported = svm_mpx_supported,
  3618. .set_supported_cpuid = svm_set_supported_cpuid,
  3619. .has_wbinvd_exit = svm_has_wbinvd_exit,
  3620. .set_tsc_khz = svm_set_tsc_khz,
  3621. .read_tsc_offset = svm_read_tsc_offset,
  3622. .write_tsc_offset = svm_write_tsc_offset,
  3623. .adjust_tsc_offset = svm_adjust_tsc_offset,
  3624. .compute_tsc_offset = svm_compute_tsc_offset,
  3625. .read_l1_tsc = svm_read_l1_tsc,
  3626. .set_tdp_cr3 = set_tdp_cr3,
  3627. .check_intercept = svm_check_intercept,
  3628. .handle_external_intr = svm_handle_external_intr,
  3629. .sched_in = svm_sched_in,
  3630. };
  3631. static int __init svm_init(void)
  3632. {
  3633. return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
  3634. __alignof__(struct vcpu_svm), THIS_MODULE);
  3635. }
  3636. static void __exit svm_exit(void)
  3637. {
  3638. kvm_exit();
  3639. }
  3640. module_init(svm_init)
  3641. module_exit(svm_exit)