svm.c 110 KB

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