svm.c 108 KB

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