svm.c 111 KB

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