svm.c 109 KB

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