svm.c 109 KB

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