svm.c 138 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381
  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. #define pr_fmt(fmt) "SVM: " fmt
  18. #include <linux/kvm_host.h>
  19. #include "irq.h"
  20. #include "mmu.h"
  21. #include "kvm_cache_regs.h"
  22. #include "x86.h"
  23. #include "cpuid.h"
  24. #include "pmu.h"
  25. #include <linux/module.h>
  26. #include <linux/mod_devicetable.h>
  27. #include <linux/kernel.h>
  28. #include <linux/vmalloc.h>
  29. #include <linux/highmem.h>
  30. #include <linux/sched.h>
  31. #include <linux/trace_events.h>
  32. #include <linux/slab.h>
  33. #include <linux/amd-iommu.h>
  34. #include <linux/hashtable.h>
  35. #include <asm/apic.h>
  36. #include <asm/perf_event.h>
  37. #include <asm/tlbflush.h>
  38. #include <asm/desc.h>
  39. #include <asm/debugreg.h>
  40. #include <asm/kvm_para.h>
  41. #include <asm/irq_remapping.h>
  42. #include <asm/virtext.h>
  43. #include "trace.h"
  44. #define __ex(x) __kvm_handle_fault_on_reboot(x)
  45. MODULE_AUTHOR("Qumranet");
  46. MODULE_LICENSE("GPL");
  47. static const struct x86_cpu_id svm_cpu_id[] = {
  48. X86_FEATURE_MATCH(X86_FEATURE_SVM),
  49. {}
  50. };
  51. MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
  52. #define IOPM_ALLOC_ORDER 2
  53. #define MSRPM_ALLOC_ORDER 1
  54. #define SEG_TYPE_LDT 2
  55. #define SEG_TYPE_BUSY_TSS16 3
  56. #define SVM_FEATURE_NPT (1 << 0)
  57. #define SVM_FEATURE_LBRV (1 << 1)
  58. #define SVM_FEATURE_SVML (1 << 2)
  59. #define SVM_FEATURE_NRIP (1 << 3)
  60. #define SVM_FEATURE_TSC_RATE (1 << 4)
  61. #define SVM_FEATURE_VMCB_CLEAN (1 << 5)
  62. #define SVM_FEATURE_FLUSH_ASID (1 << 6)
  63. #define SVM_FEATURE_DECODE_ASSIST (1 << 7)
  64. #define SVM_FEATURE_PAUSE_FILTER (1 << 10)
  65. #define SVM_AVIC_DOORBELL 0xc001011b
  66. #define NESTED_EXIT_HOST 0 /* Exit handled on host level */
  67. #define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
  68. #define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
  69. #define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
  70. #define TSC_RATIO_RSVD 0xffffff0000000000ULL
  71. #define TSC_RATIO_MIN 0x0000000000000001ULL
  72. #define TSC_RATIO_MAX 0x000000ffffffffffULL
  73. #define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF)
  74. /*
  75. * 0xff is broadcast, so the max index allowed for physical APIC ID
  76. * table is 0xfe. APIC IDs above 0xff are reserved.
  77. */
  78. #define AVIC_MAX_PHYSICAL_ID_COUNT 255
  79. #define AVIC_UNACCEL_ACCESS_WRITE_MASK 1
  80. #define AVIC_UNACCEL_ACCESS_OFFSET_MASK 0xFF0
  81. #define AVIC_UNACCEL_ACCESS_VECTOR_MASK 0xFFFFFFFF
  82. /* AVIC GATAG is encoded using VM and VCPU IDs */
  83. #define AVIC_VCPU_ID_BITS 8
  84. #define AVIC_VCPU_ID_MASK ((1 << AVIC_VCPU_ID_BITS) - 1)
  85. #define AVIC_VM_ID_BITS 24
  86. #define AVIC_VM_ID_NR (1 << AVIC_VM_ID_BITS)
  87. #define AVIC_VM_ID_MASK ((1 << AVIC_VM_ID_BITS) - 1)
  88. #define AVIC_GATAG(x, y) (((x & AVIC_VM_ID_MASK) << AVIC_VCPU_ID_BITS) | \
  89. (y & AVIC_VCPU_ID_MASK))
  90. #define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VCPU_ID_BITS) & AVIC_VM_ID_MASK)
  91. #define AVIC_GATAG_TO_VCPUID(x) (x & AVIC_VCPU_ID_MASK)
  92. static bool erratum_383_found __read_mostly;
  93. static const u32 host_save_user_msrs[] = {
  94. #ifdef CONFIG_X86_64
  95. MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
  96. MSR_FS_BASE,
  97. #endif
  98. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  99. MSR_TSC_AUX,
  100. };
  101. #define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
  102. struct kvm_vcpu;
  103. struct nested_state {
  104. struct vmcb *hsave;
  105. u64 hsave_msr;
  106. u64 vm_cr_msr;
  107. u64 vmcb;
  108. /* These are the merged vectors */
  109. u32 *msrpm;
  110. /* gpa pointers to the real vectors */
  111. u64 vmcb_msrpm;
  112. u64 vmcb_iopm;
  113. /* A VMEXIT is required but not yet emulated */
  114. bool exit_required;
  115. /* cache for intercepts of the guest */
  116. u32 intercept_cr;
  117. u32 intercept_dr;
  118. u32 intercept_exceptions;
  119. u64 intercept;
  120. /* Nested Paging related state */
  121. u64 nested_cr3;
  122. };
  123. #define MSRPM_OFFSETS 16
  124. static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
  125. /*
  126. * Set osvw_len to higher value when updated Revision Guides
  127. * are published and we know what the new status bits are
  128. */
  129. static uint64_t osvw_len = 4, osvw_status;
  130. struct vcpu_svm {
  131. struct kvm_vcpu vcpu;
  132. struct vmcb *vmcb;
  133. unsigned long vmcb_pa;
  134. struct svm_cpu_data *svm_data;
  135. uint64_t asid_generation;
  136. uint64_t sysenter_esp;
  137. uint64_t sysenter_eip;
  138. uint64_t tsc_aux;
  139. u64 next_rip;
  140. u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
  141. struct {
  142. u16 fs;
  143. u16 gs;
  144. u16 ldt;
  145. u64 gs_base;
  146. } host;
  147. u32 *msrpm;
  148. ulong nmi_iret_rip;
  149. struct nested_state nested;
  150. bool nmi_singlestep;
  151. unsigned int3_injected;
  152. unsigned long int3_rip;
  153. u32 apf_reason;
  154. /* cached guest cpuid flags for faster access */
  155. bool nrips_enabled : 1;
  156. u32 ldr_reg;
  157. struct page *avic_backing_page;
  158. u64 *avic_physical_id_cache;
  159. bool avic_is_running;
  160. /*
  161. * Per-vcpu list of struct amd_svm_iommu_ir:
  162. * This is used mainly to store interrupt remapping information used
  163. * when update the vcpu affinity. This avoids the need to scan for
  164. * IRTE and try to match ga_tag in the IOMMU driver.
  165. */
  166. struct list_head ir_list;
  167. spinlock_t ir_list_lock;
  168. };
  169. /*
  170. * This is a wrapper of struct amd_iommu_ir_data.
  171. */
  172. struct amd_svm_iommu_ir {
  173. struct list_head node; /* Used by SVM for per-vcpu ir_list */
  174. void *data; /* Storing pointer to struct amd_ir_data */
  175. };
  176. #define AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK (0xFF)
  177. #define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31)
  178. #define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK (0xFFULL)
  179. #define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12)
  180. #define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62)
  181. #define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63)
  182. static DEFINE_PER_CPU(u64, current_tsc_ratio);
  183. #define TSC_RATIO_DEFAULT 0x0100000000ULL
  184. #define MSR_INVALID 0xffffffffU
  185. static const struct svm_direct_access_msrs {
  186. u32 index; /* Index of the MSR */
  187. bool always; /* True if intercept is always on */
  188. } direct_access_msrs[] = {
  189. { .index = MSR_STAR, .always = true },
  190. { .index = MSR_IA32_SYSENTER_CS, .always = true },
  191. #ifdef CONFIG_X86_64
  192. { .index = MSR_GS_BASE, .always = true },
  193. { .index = MSR_FS_BASE, .always = true },
  194. { .index = MSR_KERNEL_GS_BASE, .always = true },
  195. { .index = MSR_LSTAR, .always = true },
  196. { .index = MSR_CSTAR, .always = true },
  197. { .index = MSR_SYSCALL_MASK, .always = true },
  198. #endif
  199. { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
  200. { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
  201. { .index = MSR_IA32_LASTINTFROMIP, .always = false },
  202. { .index = MSR_IA32_LASTINTTOIP, .always = false },
  203. { .index = MSR_INVALID, .always = false },
  204. };
  205. /* enable NPT for AMD64 and X86 with PAE */
  206. #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
  207. static bool npt_enabled = true;
  208. #else
  209. static bool npt_enabled;
  210. #endif
  211. /* allow nested paging (virtualized MMU) for all guests */
  212. static int npt = true;
  213. module_param(npt, int, S_IRUGO);
  214. /* allow nested virtualization in KVM/SVM */
  215. static int nested = true;
  216. module_param(nested, int, S_IRUGO);
  217. /* enable / disable AVIC */
  218. static int avic;
  219. #ifdef CONFIG_X86_LOCAL_APIC
  220. module_param(avic, int, S_IRUGO);
  221. #endif
  222. /* AVIC VM ID bit masks and lock */
  223. static DECLARE_BITMAP(avic_vm_id_bitmap, AVIC_VM_ID_NR);
  224. static DEFINE_SPINLOCK(avic_vm_id_lock);
  225. static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
  226. static void svm_flush_tlb(struct kvm_vcpu *vcpu);
  227. static void svm_complete_interrupts(struct vcpu_svm *svm);
  228. static int nested_svm_exit_handled(struct vcpu_svm *svm);
  229. static int nested_svm_intercept(struct vcpu_svm *svm);
  230. static int nested_svm_vmexit(struct vcpu_svm *svm);
  231. static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
  232. bool has_error_code, u32 error_code);
  233. enum {
  234. VMCB_INTERCEPTS, /* Intercept vectors, TSC offset,
  235. pause filter count */
  236. VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */
  237. VMCB_ASID, /* ASID */
  238. VMCB_INTR, /* int_ctl, int_vector */
  239. VMCB_NPT, /* npt_en, nCR3, gPAT */
  240. VMCB_CR, /* CR0, CR3, CR4, EFER */
  241. VMCB_DR, /* DR6, DR7 */
  242. VMCB_DT, /* GDT, IDT */
  243. VMCB_SEG, /* CS, DS, SS, ES, CPL */
  244. VMCB_CR2, /* CR2 only */
  245. VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */
  246. VMCB_AVIC, /* AVIC APIC_BAR, AVIC APIC_BACKING_PAGE,
  247. * AVIC PHYSICAL_TABLE pointer,
  248. * AVIC LOGICAL_TABLE pointer
  249. */
  250. VMCB_DIRTY_MAX,
  251. };
  252. /* TPR and CR2 are always written before VMRUN */
  253. #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
  254. #define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL
  255. static inline void mark_all_dirty(struct vmcb *vmcb)
  256. {
  257. vmcb->control.clean = 0;
  258. }
  259. static inline void mark_all_clean(struct vmcb *vmcb)
  260. {
  261. vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1)
  262. & ~VMCB_ALWAYS_DIRTY_MASK;
  263. }
  264. static inline void mark_dirty(struct vmcb *vmcb, int bit)
  265. {
  266. vmcb->control.clean &= ~(1 << bit);
  267. }
  268. static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
  269. {
  270. return container_of(vcpu, struct vcpu_svm, vcpu);
  271. }
  272. static inline void avic_update_vapic_bar(struct vcpu_svm *svm, u64 data)
  273. {
  274. svm->vmcb->control.avic_vapic_bar = data & VMCB_AVIC_APIC_BAR_MASK;
  275. mark_dirty(svm->vmcb, VMCB_AVIC);
  276. }
  277. static inline bool avic_vcpu_is_running(struct kvm_vcpu *vcpu)
  278. {
  279. struct vcpu_svm *svm = to_svm(vcpu);
  280. u64 *entry = svm->avic_physical_id_cache;
  281. if (!entry)
  282. return false;
  283. return (READ_ONCE(*entry) & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
  284. }
  285. static void recalc_intercepts(struct vcpu_svm *svm)
  286. {
  287. struct vmcb_control_area *c, *h;
  288. struct nested_state *g;
  289. mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
  290. if (!is_guest_mode(&svm->vcpu))
  291. return;
  292. c = &svm->vmcb->control;
  293. h = &svm->nested.hsave->control;
  294. g = &svm->nested;
  295. c->intercept_cr = h->intercept_cr | g->intercept_cr;
  296. c->intercept_dr = h->intercept_dr | g->intercept_dr;
  297. c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions;
  298. c->intercept = h->intercept | g->intercept;
  299. }
  300. static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm)
  301. {
  302. if (is_guest_mode(&svm->vcpu))
  303. return svm->nested.hsave;
  304. else
  305. return svm->vmcb;
  306. }
  307. static inline void set_cr_intercept(struct vcpu_svm *svm, int bit)
  308. {
  309. struct vmcb *vmcb = get_host_vmcb(svm);
  310. vmcb->control.intercept_cr |= (1U << bit);
  311. recalc_intercepts(svm);
  312. }
  313. static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit)
  314. {
  315. struct vmcb *vmcb = get_host_vmcb(svm);
  316. vmcb->control.intercept_cr &= ~(1U << bit);
  317. recalc_intercepts(svm);
  318. }
  319. static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit)
  320. {
  321. struct vmcb *vmcb = get_host_vmcb(svm);
  322. return vmcb->control.intercept_cr & (1U << bit);
  323. }
  324. static inline void set_dr_intercepts(struct vcpu_svm *svm)
  325. {
  326. struct vmcb *vmcb = get_host_vmcb(svm);
  327. vmcb->control.intercept_dr = (1 << INTERCEPT_DR0_READ)
  328. | (1 << INTERCEPT_DR1_READ)
  329. | (1 << INTERCEPT_DR2_READ)
  330. | (1 << INTERCEPT_DR3_READ)
  331. | (1 << INTERCEPT_DR4_READ)
  332. | (1 << INTERCEPT_DR5_READ)
  333. | (1 << INTERCEPT_DR6_READ)
  334. | (1 << INTERCEPT_DR7_READ)
  335. | (1 << INTERCEPT_DR0_WRITE)
  336. | (1 << INTERCEPT_DR1_WRITE)
  337. | (1 << INTERCEPT_DR2_WRITE)
  338. | (1 << INTERCEPT_DR3_WRITE)
  339. | (1 << INTERCEPT_DR4_WRITE)
  340. | (1 << INTERCEPT_DR5_WRITE)
  341. | (1 << INTERCEPT_DR6_WRITE)
  342. | (1 << INTERCEPT_DR7_WRITE);
  343. recalc_intercepts(svm);
  344. }
  345. static inline void clr_dr_intercepts(struct vcpu_svm *svm)
  346. {
  347. struct vmcb *vmcb = get_host_vmcb(svm);
  348. vmcb->control.intercept_dr = 0;
  349. recalc_intercepts(svm);
  350. }
  351. static inline void set_exception_intercept(struct vcpu_svm *svm, int bit)
  352. {
  353. struct vmcb *vmcb = get_host_vmcb(svm);
  354. vmcb->control.intercept_exceptions |= (1U << bit);
  355. recalc_intercepts(svm);
  356. }
  357. static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit)
  358. {
  359. struct vmcb *vmcb = get_host_vmcb(svm);
  360. vmcb->control.intercept_exceptions &= ~(1U << bit);
  361. recalc_intercepts(svm);
  362. }
  363. static inline void set_intercept(struct vcpu_svm *svm, int bit)
  364. {
  365. struct vmcb *vmcb = get_host_vmcb(svm);
  366. vmcb->control.intercept |= (1ULL << bit);
  367. recalc_intercepts(svm);
  368. }
  369. static inline void clr_intercept(struct vcpu_svm *svm, int bit)
  370. {
  371. struct vmcb *vmcb = get_host_vmcb(svm);
  372. vmcb->control.intercept &= ~(1ULL << bit);
  373. recalc_intercepts(svm);
  374. }
  375. static inline void enable_gif(struct vcpu_svm *svm)
  376. {
  377. svm->vcpu.arch.hflags |= HF_GIF_MASK;
  378. }
  379. static inline void disable_gif(struct vcpu_svm *svm)
  380. {
  381. svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
  382. }
  383. static inline bool gif_set(struct vcpu_svm *svm)
  384. {
  385. return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
  386. }
  387. static unsigned long iopm_base;
  388. struct kvm_ldttss_desc {
  389. u16 limit0;
  390. u16 base0;
  391. unsigned base1:8, type:5, dpl:2, p:1;
  392. unsigned limit1:4, zero0:3, g:1, base2:8;
  393. u32 base3;
  394. u32 zero1;
  395. } __attribute__((packed));
  396. struct svm_cpu_data {
  397. int cpu;
  398. u64 asid_generation;
  399. u32 max_asid;
  400. u32 next_asid;
  401. struct kvm_ldttss_desc *tss_desc;
  402. struct page *save_area;
  403. };
  404. static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
  405. struct svm_init_data {
  406. int cpu;
  407. int r;
  408. };
  409. static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
  410. #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
  411. #define MSRS_RANGE_SIZE 2048
  412. #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
  413. static u32 svm_msrpm_offset(u32 msr)
  414. {
  415. u32 offset;
  416. int i;
  417. for (i = 0; i < NUM_MSR_MAPS; i++) {
  418. if (msr < msrpm_ranges[i] ||
  419. msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
  420. continue;
  421. offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
  422. offset += (i * MSRS_RANGE_SIZE); /* add range offset */
  423. /* Now we have the u8 offset - but need the u32 offset */
  424. return offset / 4;
  425. }
  426. /* MSR not in any range */
  427. return MSR_INVALID;
  428. }
  429. #define MAX_INST_SIZE 15
  430. static inline void clgi(void)
  431. {
  432. asm volatile (__ex(SVM_CLGI));
  433. }
  434. static inline void stgi(void)
  435. {
  436. asm volatile (__ex(SVM_STGI));
  437. }
  438. static inline void invlpga(unsigned long addr, u32 asid)
  439. {
  440. asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
  441. }
  442. static int get_npt_level(void)
  443. {
  444. #ifdef CONFIG_X86_64
  445. return PT64_ROOT_LEVEL;
  446. #else
  447. return PT32E_ROOT_LEVEL;
  448. #endif
  449. }
  450. static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  451. {
  452. vcpu->arch.efer = efer;
  453. if (!npt_enabled && !(efer & EFER_LMA))
  454. efer &= ~EFER_LME;
  455. to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
  456. mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
  457. }
  458. static int is_external_interrupt(u32 info)
  459. {
  460. info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
  461. return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
  462. }
  463. static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
  464. {
  465. struct vcpu_svm *svm = to_svm(vcpu);
  466. u32 ret = 0;
  467. if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
  468. ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
  469. return ret;
  470. }
  471. static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  472. {
  473. struct vcpu_svm *svm = to_svm(vcpu);
  474. if (mask == 0)
  475. svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
  476. else
  477. svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
  478. }
  479. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  480. {
  481. struct vcpu_svm *svm = to_svm(vcpu);
  482. if (svm->vmcb->control.next_rip != 0) {
  483. WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
  484. svm->next_rip = svm->vmcb->control.next_rip;
  485. }
  486. if (!svm->next_rip) {
  487. if (emulate_instruction(vcpu, EMULTYPE_SKIP) !=
  488. EMULATE_DONE)
  489. printk(KERN_DEBUG "%s: NOP\n", __func__);
  490. return;
  491. }
  492. if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
  493. printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
  494. __func__, kvm_rip_read(vcpu), svm->next_rip);
  495. kvm_rip_write(vcpu, svm->next_rip);
  496. svm_set_interrupt_shadow(vcpu, 0);
  497. }
  498. static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
  499. bool has_error_code, u32 error_code,
  500. bool reinject)
  501. {
  502. struct vcpu_svm *svm = to_svm(vcpu);
  503. /*
  504. * If we are within a nested VM we'd better #VMEXIT and let the guest
  505. * handle the exception
  506. */
  507. if (!reinject &&
  508. nested_svm_check_exception(svm, nr, has_error_code, error_code))
  509. return;
  510. if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
  511. unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
  512. /*
  513. * For guest debugging where we have to reinject #BP if some
  514. * INT3 is guest-owned:
  515. * Emulate nRIP by moving RIP forward. Will fail if injection
  516. * raises a fault that is not intercepted. Still better than
  517. * failing in all cases.
  518. */
  519. skip_emulated_instruction(&svm->vcpu);
  520. rip = kvm_rip_read(&svm->vcpu);
  521. svm->int3_rip = rip + svm->vmcb->save.cs.base;
  522. svm->int3_injected = rip - old_rip;
  523. }
  524. svm->vmcb->control.event_inj = nr
  525. | SVM_EVTINJ_VALID
  526. | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
  527. | SVM_EVTINJ_TYPE_EXEPT;
  528. svm->vmcb->control.event_inj_err = error_code;
  529. }
  530. static void svm_init_erratum_383(void)
  531. {
  532. u32 low, high;
  533. int err;
  534. u64 val;
  535. if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
  536. return;
  537. /* Use _safe variants to not break nested virtualization */
  538. val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
  539. if (err)
  540. return;
  541. val |= (1ULL << 47);
  542. low = lower_32_bits(val);
  543. high = upper_32_bits(val);
  544. native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
  545. erratum_383_found = true;
  546. }
  547. static void svm_init_osvw(struct kvm_vcpu *vcpu)
  548. {
  549. /*
  550. * Guests should see errata 400 and 415 as fixed (assuming that
  551. * HLT and IO instructions are intercepted).
  552. */
  553. vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
  554. vcpu->arch.osvw.status = osvw_status & ~(6ULL);
  555. /*
  556. * By increasing VCPU's osvw.length to 3 we are telling the guest that
  557. * all osvw.status bits inside that length, including bit 0 (which is
  558. * reserved for erratum 298), are valid. However, if host processor's
  559. * osvw_len is 0 then osvw_status[0] carries no information. We need to
  560. * be conservative here and therefore we tell the guest that erratum 298
  561. * is present (because we really don't know).
  562. */
  563. if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
  564. vcpu->arch.osvw.status |= 1;
  565. }
  566. static int has_svm(void)
  567. {
  568. const char *msg;
  569. if (!cpu_has_svm(&msg)) {
  570. printk(KERN_INFO "has_svm: %s\n", msg);
  571. return 0;
  572. }
  573. return 1;
  574. }
  575. static void svm_hardware_disable(void)
  576. {
  577. /* Make sure we clean up behind us */
  578. if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
  579. wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
  580. cpu_svm_disable();
  581. amd_pmu_disable_virt();
  582. }
  583. static int svm_hardware_enable(void)
  584. {
  585. struct svm_cpu_data *sd;
  586. uint64_t efer;
  587. struct desc_ptr gdt_descr;
  588. struct desc_struct *gdt;
  589. int me = raw_smp_processor_id();
  590. rdmsrl(MSR_EFER, efer);
  591. if (efer & EFER_SVME)
  592. return -EBUSY;
  593. if (!has_svm()) {
  594. pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
  595. return -EINVAL;
  596. }
  597. sd = per_cpu(svm_data, me);
  598. if (!sd) {
  599. pr_err("%s: svm_data is NULL on %d\n", __func__, me);
  600. return -EINVAL;
  601. }
  602. sd->asid_generation = 1;
  603. sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
  604. sd->next_asid = sd->max_asid + 1;
  605. native_store_gdt(&gdt_descr);
  606. gdt = (struct desc_struct *)gdt_descr.address;
  607. sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
  608. wrmsrl(MSR_EFER, efer | EFER_SVME);
  609. wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
  610. if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
  611. wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
  612. __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT);
  613. }
  614. /*
  615. * Get OSVW bits.
  616. *
  617. * Note that it is possible to have a system with mixed processor
  618. * revisions and therefore different OSVW bits. If bits are not the same
  619. * on different processors then choose the worst case (i.e. if erratum
  620. * is present on one processor and not on another then assume that the
  621. * erratum is present everywhere).
  622. */
  623. if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
  624. uint64_t len, status = 0;
  625. int err;
  626. len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
  627. if (!err)
  628. status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
  629. &err);
  630. if (err)
  631. osvw_status = osvw_len = 0;
  632. else {
  633. if (len < osvw_len)
  634. osvw_len = len;
  635. osvw_status |= status;
  636. osvw_status &= (1ULL << osvw_len) - 1;
  637. }
  638. } else
  639. osvw_status = osvw_len = 0;
  640. svm_init_erratum_383();
  641. amd_pmu_enable_virt();
  642. return 0;
  643. }
  644. static void svm_cpu_uninit(int cpu)
  645. {
  646. struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
  647. if (!sd)
  648. return;
  649. per_cpu(svm_data, raw_smp_processor_id()) = NULL;
  650. __free_page(sd->save_area);
  651. kfree(sd);
  652. }
  653. static int svm_cpu_init(int cpu)
  654. {
  655. struct svm_cpu_data *sd;
  656. int r;
  657. sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
  658. if (!sd)
  659. return -ENOMEM;
  660. sd->cpu = cpu;
  661. sd->save_area = alloc_page(GFP_KERNEL);
  662. r = -ENOMEM;
  663. if (!sd->save_area)
  664. goto err_1;
  665. per_cpu(svm_data, cpu) = sd;
  666. return 0;
  667. err_1:
  668. kfree(sd);
  669. return r;
  670. }
  671. static bool valid_msr_intercept(u32 index)
  672. {
  673. int i;
  674. for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
  675. if (direct_access_msrs[i].index == index)
  676. return true;
  677. return false;
  678. }
  679. static void set_msr_interception(u32 *msrpm, unsigned msr,
  680. int read, int write)
  681. {
  682. u8 bit_read, bit_write;
  683. unsigned long tmp;
  684. u32 offset;
  685. /*
  686. * If this warning triggers extend the direct_access_msrs list at the
  687. * beginning of the file
  688. */
  689. WARN_ON(!valid_msr_intercept(msr));
  690. offset = svm_msrpm_offset(msr);
  691. bit_read = 2 * (msr & 0x0f);
  692. bit_write = 2 * (msr & 0x0f) + 1;
  693. tmp = msrpm[offset];
  694. BUG_ON(offset == MSR_INVALID);
  695. read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
  696. write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
  697. msrpm[offset] = tmp;
  698. }
  699. static void svm_vcpu_init_msrpm(u32 *msrpm)
  700. {
  701. int i;
  702. memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
  703. for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
  704. if (!direct_access_msrs[i].always)
  705. continue;
  706. set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
  707. }
  708. }
  709. static void add_msr_offset(u32 offset)
  710. {
  711. int i;
  712. for (i = 0; i < MSRPM_OFFSETS; ++i) {
  713. /* Offset already in list? */
  714. if (msrpm_offsets[i] == offset)
  715. return;
  716. /* Slot used by another offset? */
  717. if (msrpm_offsets[i] != MSR_INVALID)
  718. continue;
  719. /* Add offset to list */
  720. msrpm_offsets[i] = offset;
  721. return;
  722. }
  723. /*
  724. * If this BUG triggers the msrpm_offsets table has an overflow. Just
  725. * increase MSRPM_OFFSETS in this case.
  726. */
  727. BUG();
  728. }
  729. static void init_msrpm_offsets(void)
  730. {
  731. int i;
  732. memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
  733. for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
  734. u32 offset;
  735. offset = svm_msrpm_offset(direct_access_msrs[i].index);
  736. BUG_ON(offset == MSR_INVALID);
  737. add_msr_offset(offset);
  738. }
  739. }
  740. static void svm_enable_lbrv(struct vcpu_svm *svm)
  741. {
  742. u32 *msrpm = svm->msrpm;
  743. svm->vmcb->control.lbr_ctl = 1;
  744. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
  745. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
  746. set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
  747. set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
  748. }
  749. static void svm_disable_lbrv(struct vcpu_svm *svm)
  750. {
  751. u32 *msrpm = svm->msrpm;
  752. svm->vmcb->control.lbr_ctl = 0;
  753. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
  754. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
  755. set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
  756. set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
  757. }
  758. /* Note:
  759. * This hash table is used to map VM_ID to a struct kvm_arch,
  760. * when handling AMD IOMMU GALOG notification to schedule in
  761. * a particular vCPU.
  762. */
  763. #define SVM_VM_DATA_HASH_BITS 8
  764. static DEFINE_HASHTABLE(svm_vm_data_hash, SVM_VM_DATA_HASH_BITS);
  765. static DEFINE_SPINLOCK(svm_vm_data_hash_lock);
  766. /* Note:
  767. * This function is called from IOMMU driver to notify
  768. * SVM to schedule in a particular vCPU of a particular VM.
  769. */
  770. static int avic_ga_log_notifier(u32 ga_tag)
  771. {
  772. unsigned long flags;
  773. struct kvm_arch *ka = NULL;
  774. struct kvm_vcpu *vcpu = NULL;
  775. u32 vm_id = AVIC_GATAG_TO_VMID(ga_tag);
  776. u32 vcpu_id = AVIC_GATAG_TO_VCPUID(ga_tag);
  777. pr_debug("SVM: %s: vm_id=%#x, vcpu_id=%#x\n", __func__, vm_id, vcpu_id);
  778. spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
  779. hash_for_each_possible(svm_vm_data_hash, ka, hnode, vm_id) {
  780. struct kvm *kvm = container_of(ka, struct kvm, arch);
  781. struct kvm_arch *vm_data = &kvm->arch;
  782. if (vm_data->avic_vm_id != vm_id)
  783. continue;
  784. vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
  785. break;
  786. }
  787. spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
  788. if (!vcpu)
  789. return 0;
  790. /* Note:
  791. * At this point, the IOMMU should have already set the pending
  792. * bit in the vAPIC backing page. So, we just need to schedule
  793. * in the vcpu.
  794. */
  795. if (vcpu->mode == OUTSIDE_GUEST_MODE)
  796. kvm_vcpu_wake_up(vcpu);
  797. return 0;
  798. }
  799. static __init int svm_hardware_setup(void)
  800. {
  801. int cpu;
  802. struct page *iopm_pages;
  803. void *iopm_va;
  804. int r;
  805. iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
  806. if (!iopm_pages)
  807. return -ENOMEM;
  808. iopm_va = page_address(iopm_pages);
  809. memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
  810. iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
  811. init_msrpm_offsets();
  812. if (boot_cpu_has(X86_FEATURE_NX))
  813. kvm_enable_efer_bits(EFER_NX);
  814. if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
  815. kvm_enable_efer_bits(EFER_FFXSR);
  816. if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
  817. kvm_has_tsc_control = true;
  818. kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX;
  819. kvm_tsc_scaling_ratio_frac_bits = 32;
  820. }
  821. if (nested) {
  822. printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
  823. kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
  824. }
  825. for_each_possible_cpu(cpu) {
  826. r = svm_cpu_init(cpu);
  827. if (r)
  828. goto err;
  829. }
  830. if (!boot_cpu_has(X86_FEATURE_NPT))
  831. npt_enabled = false;
  832. if (npt_enabled && !npt) {
  833. printk(KERN_INFO "kvm: Nested Paging disabled\n");
  834. npt_enabled = false;
  835. }
  836. if (npt_enabled) {
  837. printk(KERN_INFO "kvm: Nested Paging enabled\n");
  838. kvm_enable_tdp();
  839. } else
  840. kvm_disable_tdp();
  841. if (avic) {
  842. if (!npt_enabled ||
  843. !boot_cpu_has(X86_FEATURE_AVIC) ||
  844. !IS_ENABLED(CONFIG_X86_LOCAL_APIC)) {
  845. avic = false;
  846. } else {
  847. pr_info("AVIC enabled\n");
  848. amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
  849. }
  850. }
  851. return 0;
  852. err:
  853. __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
  854. iopm_base = 0;
  855. return r;
  856. }
  857. static __exit void svm_hardware_unsetup(void)
  858. {
  859. int cpu;
  860. for_each_possible_cpu(cpu)
  861. svm_cpu_uninit(cpu);
  862. __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
  863. iopm_base = 0;
  864. }
  865. static void init_seg(struct vmcb_seg *seg)
  866. {
  867. seg->selector = 0;
  868. seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
  869. SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
  870. seg->limit = 0xffff;
  871. seg->base = 0;
  872. }
  873. static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
  874. {
  875. seg->selector = 0;
  876. seg->attrib = SVM_SELECTOR_P_MASK | type;
  877. seg->limit = 0xffff;
  878. seg->base = 0;
  879. }
  880. static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
  881. {
  882. struct vcpu_svm *svm = to_svm(vcpu);
  883. u64 g_tsc_offset = 0;
  884. if (is_guest_mode(vcpu)) {
  885. g_tsc_offset = svm->vmcb->control.tsc_offset -
  886. svm->nested.hsave->control.tsc_offset;
  887. svm->nested.hsave->control.tsc_offset = offset;
  888. } else
  889. trace_kvm_write_tsc_offset(vcpu->vcpu_id,
  890. svm->vmcb->control.tsc_offset,
  891. offset);
  892. svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
  893. mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
  894. }
  895. static void avic_init_vmcb(struct vcpu_svm *svm)
  896. {
  897. struct vmcb *vmcb = svm->vmcb;
  898. struct kvm_arch *vm_data = &svm->vcpu.kvm->arch;
  899. phys_addr_t bpa = page_to_phys(svm->avic_backing_page);
  900. phys_addr_t lpa = page_to_phys(vm_data->avic_logical_id_table_page);
  901. phys_addr_t ppa = page_to_phys(vm_data->avic_physical_id_table_page);
  902. vmcb->control.avic_backing_page = bpa & AVIC_HPA_MASK;
  903. vmcb->control.avic_logical_id = lpa & AVIC_HPA_MASK;
  904. vmcb->control.avic_physical_id = ppa & AVIC_HPA_MASK;
  905. vmcb->control.avic_physical_id |= AVIC_MAX_PHYSICAL_ID_COUNT;
  906. vmcb->control.int_ctl |= AVIC_ENABLE_MASK;
  907. svm->vcpu.arch.apicv_active = true;
  908. }
  909. static void init_vmcb(struct vcpu_svm *svm)
  910. {
  911. struct vmcb_control_area *control = &svm->vmcb->control;
  912. struct vmcb_save_area *save = &svm->vmcb->save;
  913. svm->vcpu.arch.hflags = 0;
  914. set_cr_intercept(svm, INTERCEPT_CR0_READ);
  915. set_cr_intercept(svm, INTERCEPT_CR3_READ);
  916. set_cr_intercept(svm, INTERCEPT_CR4_READ);
  917. set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
  918. set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
  919. set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
  920. if (!kvm_vcpu_apicv_active(&svm->vcpu))
  921. set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  922. set_dr_intercepts(svm);
  923. set_exception_intercept(svm, PF_VECTOR);
  924. set_exception_intercept(svm, UD_VECTOR);
  925. set_exception_intercept(svm, MC_VECTOR);
  926. set_exception_intercept(svm, AC_VECTOR);
  927. set_exception_intercept(svm, DB_VECTOR);
  928. set_intercept(svm, INTERCEPT_INTR);
  929. set_intercept(svm, INTERCEPT_NMI);
  930. set_intercept(svm, INTERCEPT_SMI);
  931. set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
  932. set_intercept(svm, INTERCEPT_RDPMC);
  933. set_intercept(svm, INTERCEPT_CPUID);
  934. set_intercept(svm, INTERCEPT_INVD);
  935. set_intercept(svm, INTERCEPT_HLT);
  936. set_intercept(svm, INTERCEPT_INVLPG);
  937. set_intercept(svm, INTERCEPT_INVLPGA);
  938. set_intercept(svm, INTERCEPT_IOIO_PROT);
  939. set_intercept(svm, INTERCEPT_MSR_PROT);
  940. set_intercept(svm, INTERCEPT_TASK_SWITCH);
  941. set_intercept(svm, INTERCEPT_SHUTDOWN);
  942. set_intercept(svm, INTERCEPT_VMRUN);
  943. set_intercept(svm, INTERCEPT_VMMCALL);
  944. set_intercept(svm, INTERCEPT_VMLOAD);
  945. set_intercept(svm, INTERCEPT_VMSAVE);
  946. set_intercept(svm, INTERCEPT_STGI);
  947. set_intercept(svm, INTERCEPT_CLGI);
  948. set_intercept(svm, INTERCEPT_SKINIT);
  949. set_intercept(svm, INTERCEPT_WBINVD);
  950. set_intercept(svm, INTERCEPT_MONITOR);
  951. set_intercept(svm, INTERCEPT_MWAIT);
  952. set_intercept(svm, INTERCEPT_XSETBV);
  953. control->iopm_base_pa = iopm_base;
  954. control->msrpm_base_pa = __pa(svm->msrpm);
  955. control->int_ctl = V_INTR_MASKING_MASK;
  956. init_seg(&save->es);
  957. init_seg(&save->ss);
  958. init_seg(&save->ds);
  959. init_seg(&save->fs);
  960. init_seg(&save->gs);
  961. save->cs.selector = 0xf000;
  962. save->cs.base = 0xffff0000;
  963. /* Executable/Readable Code Segment */
  964. save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
  965. SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
  966. save->cs.limit = 0xffff;
  967. save->gdtr.limit = 0xffff;
  968. save->idtr.limit = 0xffff;
  969. init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
  970. init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
  971. svm_set_efer(&svm->vcpu, 0);
  972. save->dr6 = 0xffff0ff0;
  973. kvm_set_rflags(&svm->vcpu, 2);
  974. save->rip = 0x0000fff0;
  975. svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
  976. /*
  977. * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
  978. * It also updates the guest-visible cr0 value.
  979. */
  980. svm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
  981. kvm_mmu_reset_context(&svm->vcpu);
  982. save->cr4 = X86_CR4_PAE;
  983. /* rdx = ?? */
  984. if (npt_enabled) {
  985. /* Setup VMCB for Nested Paging */
  986. control->nested_ctl = 1;
  987. clr_intercept(svm, INTERCEPT_INVLPG);
  988. clr_exception_intercept(svm, PF_VECTOR);
  989. clr_cr_intercept(svm, INTERCEPT_CR3_READ);
  990. clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
  991. save->g_pat = svm->vcpu.arch.pat;
  992. save->cr3 = 0;
  993. save->cr4 = 0;
  994. }
  995. svm->asid_generation = 0;
  996. svm->nested.vmcb = 0;
  997. svm->vcpu.arch.hflags = 0;
  998. if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
  999. control->pause_filter_count = 3000;
  1000. set_intercept(svm, INTERCEPT_PAUSE);
  1001. }
  1002. if (avic)
  1003. avic_init_vmcb(svm);
  1004. mark_all_dirty(svm->vmcb);
  1005. enable_gif(svm);
  1006. }
  1007. static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu, int index)
  1008. {
  1009. u64 *avic_physical_id_table;
  1010. struct kvm_arch *vm_data = &vcpu->kvm->arch;
  1011. if (index >= AVIC_MAX_PHYSICAL_ID_COUNT)
  1012. return NULL;
  1013. avic_physical_id_table = page_address(vm_data->avic_physical_id_table_page);
  1014. return &avic_physical_id_table[index];
  1015. }
  1016. /**
  1017. * Note:
  1018. * AVIC hardware walks the nested page table to check permissions,
  1019. * but does not use the SPA address specified in the leaf page
  1020. * table entry since it uses address in the AVIC_BACKING_PAGE pointer
  1021. * field of the VMCB. Therefore, we set up the
  1022. * APIC_ACCESS_PAGE_PRIVATE_MEMSLOT (4KB) here.
  1023. */
  1024. static int avic_init_access_page(struct kvm_vcpu *vcpu)
  1025. {
  1026. struct kvm *kvm = vcpu->kvm;
  1027. int ret;
  1028. if (kvm->arch.apic_access_page_done)
  1029. return 0;
  1030. ret = x86_set_memory_region(kvm,
  1031. APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
  1032. APIC_DEFAULT_PHYS_BASE,
  1033. PAGE_SIZE);
  1034. if (ret)
  1035. return ret;
  1036. kvm->arch.apic_access_page_done = true;
  1037. return 0;
  1038. }
  1039. static int avic_init_backing_page(struct kvm_vcpu *vcpu)
  1040. {
  1041. int ret;
  1042. u64 *entry, new_entry;
  1043. int id = vcpu->vcpu_id;
  1044. struct vcpu_svm *svm = to_svm(vcpu);
  1045. ret = avic_init_access_page(vcpu);
  1046. if (ret)
  1047. return ret;
  1048. if (id >= AVIC_MAX_PHYSICAL_ID_COUNT)
  1049. return -EINVAL;
  1050. if (!svm->vcpu.arch.apic->regs)
  1051. return -EINVAL;
  1052. svm->avic_backing_page = virt_to_page(svm->vcpu.arch.apic->regs);
  1053. /* Setting AVIC backing page address in the phy APIC ID table */
  1054. entry = avic_get_physical_id_entry(vcpu, id);
  1055. if (!entry)
  1056. return -EINVAL;
  1057. new_entry = READ_ONCE(*entry);
  1058. new_entry = (page_to_phys(svm->avic_backing_page) &
  1059. AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK) |
  1060. AVIC_PHYSICAL_ID_ENTRY_VALID_MASK;
  1061. WRITE_ONCE(*entry, new_entry);
  1062. svm->avic_physical_id_cache = entry;
  1063. return 0;
  1064. }
  1065. static inline int avic_get_next_vm_id(void)
  1066. {
  1067. int id;
  1068. spin_lock(&avic_vm_id_lock);
  1069. /* AVIC VM ID is one-based. */
  1070. id = find_next_zero_bit(avic_vm_id_bitmap, AVIC_VM_ID_NR, 1);
  1071. if (id <= AVIC_VM_ID_MASK)
  1072. __set_bit(id, avic_vm_id_bitmap);
  1073. else
  1074. id = -EAGAIN;
  1075. spin_unlock(&avic_vm_id_lock);
  1076. return id;
  1077. }
  1078. static inline int avic_free_vm_id(int id)
  1079. {
  1080. if (id <= 0 || id > AVIC_VM_ID_MASK)
  1081. return -EINVAL;
  1082. spin_lock(&avic_vm_id_lock);
  1083. __clear_bit(id, avic_vm_id_bitmap);
  1084. spin_unlock(&avic_vm_id_lock);
  1085. return 0;
  1086. }
  1087. static void avic_vm_destroy(struct kvm *kvm)
  1088. {
  1089. unsigned long flags;
  1090. struct kvm_arch *vm_data = &kvm->arch;
  1091. avic_free_vm_id(vm_data->avic_vm_id);
  1092. if (vm_data->avic_logical_id_table_page)
  1093. __free_page(vm_data->avic_logical_id_table_page);
  1094. if (vm_data->avic_physical_id_table_page)
  1095. __free_page(vm_data->avic_physical_id_table_page);
  1096. spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
  1097. hash_del(&vm_data->hnode);
  1098. spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
  1099. }
  1100. static int avic_vm_init(struct kvm *kvm)
  1101. {
  1102. unsigned long flags;
  1103. int vm_id, err = -ENOMEM;
  1104. struct kvm_arch *vm_data = &kvm->arch;
  1105. struct page *p_page;
  1106. struct page *l_page;
  1107. if (!avic)
  1108. return 0;
  1109. vm_id = avic_get_next_vm_id();
  1110. if (vm_id < 0)
  1111. return vm_id;
  1112. vm_data->avic_vm_id = (u32)vm_id;
  1113. /* Allocating physical APIC ID table (4KB) */
  1114. p_page = alloc_page(GFP_KERNEL);
  1115. if (!p_page)
  1116. goto free_avic;
  1117. vm_data->avic_physical_id_table_page = p_page;
  1118. clear_page(page_address(p_page));
  1119. /* Allocating logical APIC ID table (4KB) */
  1120. l_page = alloc_page(GFP_KERNEL);
  1121. if (!l_page)
  1122. goto free_avic;
  1123. vm_data->avic_logical_id_table_page = l_page;
  1124. clear_page(page_address(l_page));
  1125. spin_lock_irqsave(&svm_vm_data_hash_lock, flags);
  1126. hash_add(svm_vm_data_hash, &vm_data->hnode, vm_data->avic_vm_id);
  1127. spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags);
  1128. return 0;
  1129. free_avic:
  1130. avic_vm_destroy(kvm);
  1131. return err;
  1132. }
  1133. static inline int
  1134. avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r)
  1135. {
  1136. int ret = 0;
  1137. unsigned long flags;
  1138. struct amd_svm_iommu_ir *ir;
  1139. struct vcpu_svm *svm = to_svm(vcpu);
  1140. if (!kvm_arch_has_assigned_device(vcpu->kvm))
  1141. return 0;
  1142. /*
  1143. * Here, we go through the per-vcpu ir_list to update all existing
  1144. * interrupt remapping table entry targeting this vcpu.
  1145. */
  1146. spin_lock_irqsave(&svm->ir_list_lock, flags);
  1147. if (list_empty(&svm->ir_list))
  1148. goto out;
  1149. list_for_each_entry(ir, &svm->ir_list, node) {
  1150. ret = amd_iommu_update_ga(cpu, r, ir->data);
  1151. if (ret)
  1152. break;
  1153. }
  1154. out:
  1155. spin_unlock_irqrestore(&svm->ir_list_lock, flags);
  1156. return ret;
  1157. }
  1158. static void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1159. {
  1160. u64 entry;
  1161. /* ID = 0xff (broadcast), ID > 0xff (reserved) */
  1162. int h_physical_id = kvm_cpu_get_apicid(cpu);
  1163. struct vcpu_svm *svm = to_svm(vcpu);
  1164. if (!kvm_vcpu_apicv_active(vcpu))
  1165. return;
  1166. if (WARN_ON(h_physical_id >= AVIC_MAX_PHYSICAL_ID_COUNT))
  1167. return;
  1168. entry = READ_ONCE(*(svm->avic_physical_id_cache));
  1169. WARN_ON(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK);
  1170. entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK;
  1171. entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK);
  1172. entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
  1173. if (svm->avic_is_running)
  1174. entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
  1175. WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
  1176. avic_update_iommu_vcpu_affinity(vcpu, h_physical_id,
  1177. svm->avic_is_running);
  1178. }
  1179. static void avic_vcpu_put(struct kvm_vcpu *vcpu)
  1180. {
  1181. u64 entry;
  1182. struct vcpu_svm *svm = to_svm(vcpu);
  1183. if (!kvm_vcpu_apicv_active(vcpu))
  1184. return;
  1185. entry = READ_ONCE(*(svm->avic_physical_id_cache));
  1186. if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)
  1187. avic_update_iommu_vcpu_affinity(vcpu, -1, 0);
  1188. entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK;
  1189. WRITE_ONCE(*(svm->avic_physical_id_cache), entry);
  1190. }
  1191. /**
  1192. * This function is called during VCPU halt/unhalt.
  1193. */
  1194. static void avic_set_running(struct kvm_vcpu *vcpu, bool is_run)
  1195. {
  1196. struct vcpu_svm *svm = to_svm(vcpu);
  1197. svm->avic_is_running = is_run;
  1198. if (is_run)
  1199. avic_vcpu_load(vcpu, vcpu->cpu);
  1200. else
  1201. avic_vcpu_put(vcpu);
  1202. }
  1203. static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
  1204. {
  1205. struct vcpu_svm *svm = to_svm(vcpu);
  1206. u32 dummy;
  1207. u32 eax = 1;
  1208. if (!init_event) {
  1209. svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE |
  1210. MSR_IA32_APICBASE_ENABLE;
  1211. if (kvm_vcpu_is_reset_bsp(&svm->vcpu))
  1212. svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
  1213. }
  1214. init_vmcb(svm);
  1215. kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy);
  1216. kvm_register_write(vcpu, VCPU_REGS_RDX, eax);
  1217. if (kvm_vcpu_apicv_active(vcpu) && !init_event)
  1218. avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE);
  1219. }
  1220. static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
  1221. {
  1222. struct vcpu_svm *svm;
  1223. struct page *page;
  1224. struct page *msrpm_pages;
  1225. struct page *hsave_page;
  1226. struct page *nested_msrpm_pages;
  1227. int err;
  1228. svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  1229. if (!svm) {
  1230. err = -ENOMEM;
  1231. goto out;
  1232. }
  1233. err = kvm_vcpu_init(&svm->vcpu, kvm, id);
  1234. if (err)
  1235. goto free_svm;
  1236. err = -ENOMEM;
  1237. page = alloc_page(GFP_KERNEL);
  1238. if (!page)
  1239. goto uninit;
  1240. msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
  1241. if (!msrpm_pages)
  1242. goto free_page1;
  1243. nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
  1244. if (!nested_msrpm_pages)
  1245. goto free_page2;
  1246. hsave_page = alloc_page(GFP_KERNEL);
  1247. if (!hsave_page)
  1248. goto free_page3;
  1249. if (avic) {
  1250. err = avic_init_backing_page(&svm->vcpu);
  1251. if (err)
  1252. goto free_page4;
  1253. INIT_LIST_HEAD(&svm->ir_list);
  1254. spin_lock_init(&svm->ir_list_lock);
  1255. }
  1256. /* We initialize this flag to true to make sure that the is_running
  1257. * bit would be set the first time the vcpu is loaded.
  1258. */
  1259. svm->avic_is_running = true;
  1260. svm->nested.hsave = page_address(hsave_page);
  1261. svm->msrpm = page_address(msrpm_pages);
  1262. svm_vcpu_init_msrpm(svm->msrpm);
  1263. svm->nested.msrpm = page_address(nested_msrpm_pages);
  1264. svm_vcpu_init_msrpm(svm->nested.msrpm);
  1265. svm->vmcb = page_address(page);
  1266. clear_page(svm->vmcb);
  1267. svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
  1268. svm->asid_generation = 0;
  1269. init_vmcb(svm);
  1270. svm_init_osvw(&svm->vcpu);
  1271. return &svm->vcpu;
  1272. free_page4:
  1273. __free_page(hsave_page);
  1274. free_page3:
  1275. __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
  1276. free_page2:
  1277. __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
  1278. free_page1:
  1279. __free_page(page);
  1280. uninit:
  1281. kvm_vcpu_uninit(&svm->vcpu);
  1282. free_svm:
  1283. kmem_cache_free(kvm_vcpu_cache, svm);
  1284. out:
  1285. return ERR_PTR(err);
  1286. }
  1287. static void svm_free_vcpu(struct kvm_vcpu *vcpu)
  1288. {
  1289. struct vcpu_svm *svm = to_svm(vcpu);
  1290. __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
  1291. __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
  1292. __free_page(virt_to_page(svm->nested.hsave));
  1293. __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
  1294. kvm_vcpu_uninit(vcpu);
  1295. kmem_cache_free(kvm_vcpu_cache, svm);
  1296. }
  1297. static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1298. {
  1299. struct vcpu_svm *svm = to_svm(vcpu);
  1300. int i;
  1301. if (unlikely(cpu != vcpu->cpu)) {
  1302. svm->asid_generation = 0;
  1303. mark_all_dirty(svm->vmcb);
  1304. }
  1305. #ifdef CONFIG_X86_64
  1306. rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
  1307. #endif
  1308. savesegment(fs, svm->host.fs);
  1309. savesegment(gs, svm->host.gs);
  1310. svm->host.ldt = kvm_read_ldt();
  1311. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  1312. rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  1313. if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
  1314. u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
  1315. if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
  1316. __this_cpu_write(current_tsc_ratio, tsc_ratio);
  1317. wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
  1318. }
  1319. }
  1320. /* This assumes that the kernel never uses MSR_TSC_AUX */
  1321. if (static_cpu_has(X86_FEATURE_RDTSCP))
  1322. wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
  1323. avic_vcpu_load(vcpu, cpu);
  1324. }
  1325. static void svm_vcpu_put(struct kvm_vcpu *vcpu)
  1326. {
  1327. struct vcpu_svm *svm = to_svm(vcpu);
  1328. int i;
  1329. avic_vcpu_put(vcpu);
  1330. ++vcpu->stat.host_state_reload;
  1331. kvm_load_ldt(svm->host.ldt);
  1332. #ifdef CONFIG_X86_64
  1333. loadsegment(fs, svm->host.fs);
  1334. wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gsbase);
  1335. load_gs_index(svm->host.gs);
  1336. #else
  1337. #ifdef CONFIG_X86_32_LAZY_GS
  1338. loadsegment(gs, svm->host.gs);
  1339. #endif
  1340. #endif
  1341. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  1342. wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  1343. }
  1344. static void svm_vcpu_blocking(struct kvm_vcpu *vcpu)
  1345. {
  1346. avic_set_running(vcpu, false);
  1347. }
  1348. static void svm_vcpu_unblocking(struct kvm_vcpu *vcpu)
  1349. {
  1350. avic_set_running(vcpu, true);
  1351. }
  1352. static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
  1353. {
  1354. return to_svm(vcpu)->vmcb->save.rflags;
  1355. }
  1356. static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  1357. {
  1358. /*
  1359. * Any change of EFLAGS.VM is accompanied by a reload of SS
  1360. * (caused by either a task switch or an inter-privilege IRET),
  1361. * so we do not need to update the CPL here.
  1362. */
  1363. to_svm(vcpu)->vmcb->save.rflags = rflags;
  1364. }
  1365. static u32 svm_get_pkru(struct kvm_vcpu *vcpu)
  1366. {
  1367. return 0;
  1368. }
  1369. static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  1370. {
  1371. switch (reg) {
  1372. case VCPU_EXREG_PDPTR:
  1373. BUG_ON(!npt_enabled);
  1374. load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
  1375. break;
  1376. default:
  1377. BUG();
  1378. }
  1379. }
  1380. static void svm_set_vintr(struct vcpu_svm *svm)
  1381. {
  1382. set_intercept(svm, INTERCEPT_VINTR);
  1383. }
  1384. static void svm_clear_vintr(struct vcpu_svm *svm)
  1385. {
  1386. clr_intercept(svm, INTERCEPT_VINTR);
  1387. }
  1388. static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
  1389. {
  1390. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  1391. switch (seg) {
  1392. case VCPU_SREG_CS: return &save->cs;
  1393. case VCPU_SREG_DS: return &save->ds;
  1394. case VCPU_SREG_ES: return &save->es;
  1395. case VCPU_SREG_FS: return &save->fs;
  1396. case VCPU_SREG_GS: return &save->gs;
  1397. case VCPU_SREG_SS: return &save->ss;
  1398. case VCPU_SREG_TR: return &save->tr;
  1399. case VCPU_SREG_LDTR: return &save->ldtr;
  1400. }
  1401. BUG();
  1402. return NULL;
  1403. }
  1404. static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  1405. {
  1406. struct vmcb_seg *s = svm_seg(vcpu, seg);
  1407. return s->base;
  1408. }
  1409. static void svm_get_segment(struct kvm_vcpu *vcpu,
  1410. struct kvm_segment *var, int seg)
  1411. {
  1412. struct vmcb_seg *s = svm_seg(vcpu, seg);
  1413. var->base = s->base;
  1414. var->limit = s->limit;
  1415. var->selector = s->selector;
  1416. var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
  1417. var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
  1418. var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
  1419. var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
  1420. var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
  1421. var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
  1422. var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
  1423. /*
  1424. * AMD CPUs circa 2014 track the G bit for all segments except CS.
  1425. * However, the SVM spec states that the G bit is not observed by the
  1426. * CPU, and some VMware virtual CPUs drop the G bit for all segments.
  1427. * So let's synthesize a legal G bit for all segments, this helps
  1428. * running KVM nested. It also helps cross-vendor migration, because
  1429. * Intel's vmentry has a check on the 'G' bit.
  1430. */
  1431. var->g = s->limit > 0xfffff;
  1432. /*
  1433. * AMD's VMCB does not have an explicit unusable field, so emulate it
  1434. * for cross vendor migration purposes by "not present"
  1435. */
  1436. var->unusable = !var->present || (var->type == 0);
  1437. switch (seg) {
  1438. case VCPU_SREG_TR:
  1439. /*
  1440. * Work around a bug where the busy flag in the tr selector
  1441. * isn't exposed
  1442. */
  1443. var->type |= 0x2;
  1444. break;
  1445. case VCPU_SREG_DS:
  1446. case VCPU_SREG_ES:
  1447. case VCPU_SREG_FS:
  1448. case VCPU_SREG_GS:
  1449. /*
  1450. * The accessed bit must always be set in the segment
  1451. * descriptor cache, although it can be cleared in the
  1452. * descriptor, the cached bit always remains at 1. Since
  1453. * Intel has a check on this, set it here to support
  1454. * cross-vendor migration.
  1455. */
  1456. if (!var->unusable)
  1457. var->type |= 0x1;
  1458. break;
  1459. case VCPU_SREG_SS:
  1460. /*
  1461. * On AMD CPUs sometimes the DB bit in the segment
  1462. * descriptor is left as 1, although the whole segment has
  1463. * been made unusable. Clear it here to pass an Intel VMX
  1464. * entry check when cross vendor migrating.
  1465. */
  1466. if (var->unusable)
  1467. var->db = 0;
  1468. var->dpl = to_svm(vcpu)->vmcb->save.cpl;
  1469. break;
  1470. }
  1471. }
  1472. static int svm_get_cpl(struct kvm_vcpu *vcpu)
  1473. {
  1474. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  1475. return save->cpl;
  1476. }
  1477. static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1478. {
  1479. struct vcpu_svm *svm = to_svm(vcpu);
  1480. dt->size = svm->vmcb->save.idtr.limit;
  1481. dt->address = svm->vmcb->save.idtr.base;
  1482. }
  1483. static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1484. {
  1485. struct vcpu_svm *svm = to_svm(vcpu);
  1486. svm->vmcb->save.idtr.limit = dt->size;
  1487. svm->vmcb->save.idtr.base = dt->address ;
  1488. mark_dirty(svm->vmcb, VMCB_DT);
  1489. }
  1490. static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1491. {
  1492. struct vcpu_svm *svm = to_svm(vcpu);
  1493. dt->size = svm->vmcb->save.gdtr.limit;
  1494. dt->address = svm->vmcb->save.gdtr.base;
  1495. }
  1496. static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1497. {
  1498. struct vcpu_svm *svm = to_svm(vcpu);
  1499. svm->vmcb->save.gdtr.limit = dt->size;
  1500. svm->vmcb->save.gdtr.base = dt->address ;
  1501. mark_dirty(svm->vmcb, VMCB_DT);
  1502. }
  1503. static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  1504. {
  1505. }
  1506. static void svm_decache_cr3(struct kvm_vcpu *vcpu)
  1507. {
  1508. }
  1509. static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  1510. {
  1511. }
  1512. static void update_cr0_intercept(struct vcpu_svm *svm)
  1513. {
  1514. ulong gcr0 = svm->vcpu.arch.cr0;
  1515. u64 *hcr0 = &svm->vmcb->save.cr0;
  1516. *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
  1517. | (gcr0 & SVM_CR0_SELECTIVE_MASK);
  1518. mark_dirty(svm->vmcb, VMCB_CR);
  1519. if (gcr0 == *hcr0) {
  1520. clr_cr_intercept(svm, INTERCEPT_CR0_READ);
  1521. clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
  1522. } else {
  1523. set_cr_intercept(svm, INTERCEPT_CR0_READ);
  1524. set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
  1525. }
  1526. }
  1527. static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  1528. {
  1529. struct vcpu_svm *svm = to_svm(vcpu);
  1530. #ifdef CONFIG_X86_64
  1531. if (vcpu->arch.efer & EFER_LME) {
  1532. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  1533. vcpu->arch.efer |= EFER_LMA;
  1534. svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
  1535. }
  1536. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
  1537. vcpu->arch.efer &= ~EFER_LMA;
  1538. svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
  1539. }
  1540. }
  1541. #endif
  1542. vcpu->arch.cr0 = cr0;
  1543. if (!npt_enabled)
  1544. cr0 |= X86_CR0_PG | X86_CR0_WP;
  1545. /*
  1546. * re-enable caching here because the QEMU bios
  1547. * does not do it - this results in some delay at
  1548. * reboot
  1549. */
  1550. if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
  1551. cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
  1552. svm->vmcb->save.cr0 = cr0;
  1553. mark_dirty(svm->vmcb, VMCB_CR);
  1554. update_cr0_intercept(svm);
  1555. }
  1556. static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  1557. {
  1558. unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
  1559. unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
  1560. if (cr4 & X86_CR4_VMXE)
  1561. return 1;
  1562. if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
  1563. svm_flush_tlb(vcpu);
  1564. vcpu->arch.cr4 = cr4;
  1565. if (!npt_enabled)
  1566. cr4 |= X86_CR4_PAE;
  1567. cr4 |= host_cr4_mce;
  1568. to_svm(vcpu)->vmcb->save.cr4 = cr4;
  1569. mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
  1570. return 0;
  1571. }
  1572. static void svm_set_segment(struct kvm_vcpu *vcpu,
  1573. struct kvm_segment *var, int seg)
  1574. {
  1575. struct vcpu_svm *svm = to_svm(vcpu);
  1576. struct vmcb_seg *s = svm_seg(vcpu, seg);
  1577. s->base = var->base;
  1578. s->limit = var->limit;
  1579. s->selector = var->selector;
  1580. if (var->unusable)
  1581. s->attrib = 0;
  1582. else {
  1583. s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
  1584. s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
  1585. s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
  1586. s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
  1587. s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
  1588. s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
  1589. s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
  1590. s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
  1591. }
  1592. /*
  1593. * This is always accurate, except if SYSRET returned to a segment
  1594. * with SS.DPL != 3. Intel does not have this quirk, and always
  1595. * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
  1596. * would entail passing the CPL to userspace and back.
  1597. */
  1598. if (seg == VCPU_SREG_SS)
  1599. svm->vmcb->save.cpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
  1600. mark_dirty(svm->vmcb, VMCB_SEG);
  1601. }
  1602. static void update_bp_intercept(struct kvm_vcpu *vcpu)
  1603. {
  1604. struct vcpu_svm *svm = to_svm(vcpu);
  1605. clr_exception_intercept(svm, BP_VECTOR);
  1606. if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
  1607. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  1608. set_exception_intercept(svm, BP_VECTOR);
  1609. } else
  1610. vcpu->guest_debug = 0;
  1611. }
  1612. static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
  1613. {
  1614. if (sd->next_asid > sd->max_asid) {
  1615. ++sd->asid_generation;
  1616. sd->next_asid = 1;
  1617. svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
  1618. }
  1619. svm->asid_generation = sd->asid_generation;
  1620. svm->vmcb->control.asid = sd->next_asid++;
  1621. mark_dirty(svm->vmcb, VMCB_ASID);
  1622. }
  1623. static u64 svm_get_dr6(struct kvm_vcpu *vcpu)
  1624. {
  1625. return to_svm(vcpu)->vmcb->save.dr6;
  1626. }
  1627. static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value)
  1628. {
  1629. struct vcpu_svm *svm = to_svm(vcpu);
  1630. svm->vmcb->save.dr6 = value;
  1631. mark_dirty(svm->vmcb, VMCB_DR);
  1632. }
  1633. static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
  1634. {
  1635. struct vcpu_svm *svm = to_svm(vcpu);
  1636. get_debugreg(vcpu->arch.db[0], 0);
  1637. get_debugreg(vcpu->arch.db[1], 1);
  1638. get_debugreg(vcpu->arch.db[2], 2);
  1639. get_debugreg(vcpu->arch.db[3], 3);
  1640. vcpu->arch.dr6 = svm_get_dr6(vcpu);
  1641. vcpu->arch.dr7 = svm->vmcb->save.dr7;
  1642. vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
  1643. set_dr_intercepts(svm);
  1644. }
  1645. static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
  1646. {
  1647. struct vcpu_svm *svm = to_svm(vcpu);
  1648. svm->vmcb->save.dr7 = value;
  1649. mark_dirty(svm->vmcb, VMCB_DR);
  1650. }
  1651. static int pf_interception(struct vcpu_svm *svm)
  1652. {
  1653. u64 fault_address = svm->vmcb->control.exit_info_2;
  1654. u64 error_code;
  1655. int r = 1;
  1656. switch (svm->apf_reason) {
  1657. default:
  1658. error_code = svm->vmcb->control.exit_info_1;
  1659. trace_kvm_page_fault(fault_address, error_code);
  1660. if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
  1661. kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
  1662. r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
  1663. svm->vmcb->control.insn_bytes,
  1664. svm->vmcb->control.insn_len);
  1665. break;
  1666. case KVM_PV_REASON_PAGE_NOT_PRESENT:
  1667. svm->apf_reason = 0;
  1668. local_irq_disable();
  1669. kvm_async_pf_task_wait(fault_address);
  1670. local_irq_enable();
  1671. break;
  1672. case KVM_PV_REASON_PAGE_READY:
  1673. svm->apf_reason = 0;
  1674. local_irq_disable();
  1675. kvm_async_pf_task_wake(fault_address);
  1676. local_irq_enable();
  1677. break;
  1678. }
  1679. return r;
  1680. }
  1681. static int db_interception(struct vcpu_svm *svm)
  1682. {
  1683. struct kvm_run *kvm_run = svm->vcpu.run;
  1684. if (!(svm->vcpu.guest_debug &
  1685. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
  1686. !svm->nmi_singlestep) {
  1687. kvm_queue_exception(&svm->vcpu, DB_VECTOR);
  1688. return 1;
  1689. }
  1690. if (svm->nmi_singlestep) {
  1691. svm->nmi_singlestep = false;
  1692. if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
  1693. svm->vmcb->save.rflags &=
  1694. ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
  1695. }
  1696. if (svm->vcpu.guest_debug &
  1697. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
  1698. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  1699. kvm_run->debug.arch.pc =
  1700. svm->vmcb->save.cs.base + svm->vmcb->save.rip;
  1701. kvm_run->debug.arch.exception = DB_VECTOR;
  1702. return 0;
  1703. }
  1704. return 1;
  1705. }
  1706. static int bp_interception(struct vcpu_svm *svm)
  1707. {
  1708. struct kvm_run *kvm_run = svm->vcpu.run;
  1709. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  1710. kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
  1711. kvm_run->debug.arch.exception = BP_VECTOR;
  1712. return 0;
  1713. }
  1714. static int ud_interception(struct vcpu_svm *svm)
  1715. {
  1716. int er;
  1717. er = emulate_instruction(&svm->vcpu, EMULTYPE_TRAP_UD);
  1718. if (er != EMULATE_DONE)
  1719. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  1720. return 1;
  1721. }
  1722. static int ac_interception(struct vcpu_svm *svm)
  1723. {
  1724. kvm_queue_exception_e(&svm->vcpu, AC_VECTOR, 0);
  1725. return 1;
  1726. }
  1727. static bool is_erratum_383(void)
  1728. {
  1729. int err, i;
  1730. u64 value;
  1731. if (!erratum_383_found)
  1732. return false;
  1733. value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
  1734. if (err)
  1735. return false;
  1736. /* Bit 62 may or may not be set for this mce */
  1737. value &= ~(1ULL << 62);
  1738. if (value != 0xb600000000010015ULL)
  1739. return false;
  1740. /* Clear MCi_STATUS registers */
  1741. for (i = 0; i < 6; ++i)
  1742. native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
  1743. value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
  1744. if (!err) {
  1745. u32 low, high;
  1746. value &= ~(1ULL << 2);
  1747. low = lower_32_bits(value);
  1748. high = upper_32_bits(value);
  1749. native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
  1750. }
  1751. /* Flush tlb to evict multi-match entries */
  1752. __flush_tlb_all();
  1753. return true;
  1754. }
  1755. static void svm_handle_mce(struct vcpu_svm *svm)
  1756. {
  1757. if (is_erratum_383()) {
  1758. /*
  1759. * Erratum 383 triggered. Guest state is corrupt so kill the
  1760. * guest.
  1761. */
  1762. pr_err("KVM: Guest triggered AMD Erratum 383\n");
  1763. kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
  1764. return;
  1765. }
  1766. /*
  1767. * On an #MC intercept the MCE handler is not called automatically in
  1768. * the host. So do it by hand here.
  1769. */
  1770. asm volatile (
  1771. "int $0x12\n");
  1772. /* not sure if we ever come back to this point */
  1773. return;
  1774. }
  1775. static int mc_interception(struct vcpu_svm *svm)
  1776. {
  1777. return 1;
  1778. }
  1779. static int shutdown_interception(struct vcpu_svm *svm)
  1780. {
  1781. struct kvm_run *kvm_run = svm->vcpu.run;
  1782. /*
  1783. * VMCB is undefined after a SHUTDOWN intercept
  1784. * so reinitialize it.
  1785. */
  1786. clear_page(svm->vmcb);
  1787. init_vmcb(svm);
  1788. kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
  1789. return 0;
  1790. }
  1791. static int io_interception(struct vcpu_svm *svm)
  1792. {
  1793. struct kvm_vcpu *vcpu = &svm->vcpu;
  1794. u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
  1795. int size, in, string;
  1796. unsigned port;
  1797. ++svm->vcpu.stat.io_exits;
  1798. string = (io_info & SVM_IOIO_STR_MASK) != 0;
  1799. in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
  1800. if (string)
  1801. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  1802. port = io_info >> 16;
  1803. size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
  1804. svm->next_rip = svm->vmcb->control.exit_info_2;
  1805. skip_emulated_instruction(&svm->vcpu);
  1806. return in ? kvm_fast_pio_in(vcpu, size, port)
  1807. : kvm_fast_pio_out(vcpu, size, port);
  1808. }
  1809. static int nmi_interception(struct vcpu_svm *svm)
  1810. {
  1811. return 1;
  1812. }
  1813. static int intr_interception(struct vcpu_svm *svm)
  1814. {
  1815. ++svm->vcpu.stat.irq_exits;
  1816. return 1;
  1817. }
  1818. static int nop_on_interception(struct vcpu_svm *svm)
  1819. {
  1820. return 1;
  1821. }
  1822. static int halt_interception(struct vcpu_svm *svm)
  1823. {
  1824. svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
  1825. return kvm_emulate_halt(&svm->vcpu);
  1826. }
  1827. static int vmmcall_interception(struct vcpu_svm *svm)
  1828. {
  1829. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1830. return kvm_emulate_hypercall(&svm->vcpu);
  1831. }
  1832. static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
  1833. {
  1834. struct vcpu_svm *svm = to_svm(vcpu);
  1835. return svm->nested.nested_cr3;
  1836. }
  1837. static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
  1838. {
  1839. struct vcpu_svm *svm = to_svm(vcpu);
  1840. u64 cr3 = svm->nested.nested_cr3;
  1841. u64 pdpte;
  1842. int ret;
  1843. ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte,
  1844. offset_in_page(cr3) + index * 8, 8);
  1845. if (ret)
  1846. return 0;
  1847. return pdpte;
  1848. }
  1849. static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
  1850. unsigned long root)
  1851. {
  1852. struct vcpu_svm *svm = to_svm(vcpu);
  1853. svm->vmcb->control.nested_cr3 = root;
  1854. mark_dirty(svm->vmcb, VMCB_NPT);
  1855. svm_flush_tlb(vcpu);
  1856. }
  1857. static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
  1858. struct x86_exception *fault)
  1859. {
  1860. struct vcpu_svm *svm = to_svm(vcpu);
  1861. if (svm->vmcb->control.exit_code != SVM_EXIT_NPF) {
  1862. /*
  1863. * TODO: track the cause of the nested page fault, and
  1864. * correctly fill in the high bits of exit_info_1.
  1865. */
  1866. svm->vmcb->control.exit_code = SVM_EXIT_NPF;
  1867. svm->vmcb->control.exit_code_hi = 0;
  1868. svm->vmcb->control.exit_info_1 = (1ULL << 32);
  1869. svm->vmcb->control.exit_info_2 = fault->address;
  1870. }
  1871. svm->vmcb->control.exit_info_1 &= ~0xffffffffULL;
  1872. svm->vmcb->control.exit_info_1 |= fault->error_code;
  1873. /*
  1874. * The present bit is always zero for page structure faults on real
  1875. * hardware.
  1876. */
  1877. if (svm->vmcb->control.exit_info_1 & (2ULL << 32))
  1878. svm->vmcb->control.exit_info_1 &= ~1;
  1879. nested_svm_vmexit(svm);
  1880. }
  1881. static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
  1882. {
  1883. WARN_ON(mmu_is_nested(vcpu));
  1884. kvm_init_shadow_mmu(vcpu);
  1885. vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
  1886. vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
  1887. vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr;
  1888. vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
  1889. vcpu->arch.mmu.shadow_root_level = get_npt_level();
  1890. reset_shadow_zero_bits_mask(vcpu, &vcpu->arch.mmu);
  1891. vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
  1892. }
  1893. static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
  1894. {
  1895. vcpu->arch.walk_mmu = &vcpu->arch.mmu;
  1896. }
  1897. static int nested_svm_check_permissions(struct vcpu_svm *svm)
  1898. {
  1899. if (!(svm->vcpu.arch.efer & EFER_SVME)
  1900. || !is_paging(&svm->vcpu)) {
  1901. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  1902. return 1;
  1903. }
  1904. if (svm->vmcb->save.cpl) {
  1905. kvm_inject_gp(&svm->vcpu, 0);
  1906. return 1;
  1907. }
  1908. return 0;
  1909. }
  1910. static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
  1911. bool has_error_code, u32 error_code)
  1912. {
  1913. int vmexit;
  1914. if (!is_guest_mode(&svm->vcpu))
  1915. return 0;
  1916. svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
  1917. svm->vmcb->control.exit_code_hi = 0;
  1918. svm->vmcb->control.exit_info_1 = error_code;
  1919. svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
  1920. vmexit = nested_svm_intercept(svm);
  1921. if (vmexit == NESTED_EXIT_DONE)
  1922. svm->nested.exit_required = true;
  1923. return vmexit;
  1924. }
  1925. /* This function returns true if it is save to enable the irq window */
  1926. static inline bool nested_svm_intr(struct vcpu_svm *svm)
  1927. {
  1928. if (!is_guest_mode(&svm->vcpu))
  1929. return true;
  1930. if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
  1931. return true;
  1932. if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
  1933. return false;
  1934. /*
  1935. * if vmexit was already requested (by intercepted exception
  1936. * for instance) do not overwrite it with "external interrupt"
  1937. * vmexit.
  1938. */
  1939. if (svm->nested.exit_required)
  1940. return false;
  1941. svm->vmcb->control.exit_code = SVM_EXIT_INTR;
  1942. svm->vmcb->control.exit_info_1 = 0;
  1943. svm->vmcb->control.exit_info_2 = 0;
  1944. if (svm->nested.intercept & 1ULL) {
  1945. /*
  1946. * The #vmexit can't be emulated here directly because this
  1947. * code path runs with irqs and preemption disabled. A
  1948. * #vmexit emulation might sleep. Only signal request for
  1949. * the #vmexit here.
  1950. */
  1951. svm->nested.exit_required = true;
  1952. trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
  1953. return false;
  1954. }
  1955. return true;
  1956. }
  1957. /* This function returns true if it is save to enable the nmi window */
  1958. static inline bool nested_svm_nmi(struct vcpu_svm *svm)
  1959. {
  1960. if (!is_guest_mode(&svm->vcpu))
  1961. return true;
  1962. if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
  1963. return true;
  1964. svm->vmcb->control.exit_code = SVM_EXIT_NMI;
  1965. svm->nested.exit_required = true;
  1966. return false;
  1967. }
  1968. static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
  1969. {
  1970. struct page *page;
  1971. might_sleep();
  1972. page = kvm_vcpu_gfn_to_page(&svm->vcpu, gpa >> PAGE_SHIFT);
  1973. if (is_error_page(page))
  1974. goto error;
  1975. *_page = page;
  1976. return kmap(page);
  1977. error:
  1978. kvm_inject_gp(&svm->vcpu, 0);
  1979. return NULL;
  1980. }
  1981. static void nested_svm_unmap(struct page *page)
  1982. {
  1983. kunmap(page);
  1984. kvm_release_page_dirty(page);
  1985. }
  1986. static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
  1987. {
  1988. unsigned port, size, iopm_len;
  1989. u16 val, mask;
  1990. u8 start_bit;
  1991. u64 gpa;
  1992. if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
  1993. return NESTED_EXIT_HOST;
  1994. port = svm->vmcb->control.exit_info_1 >> 16;
  1995. size = (svm->vmcb->control.exit_info_1 & SVM_IOIO_SIZE_MASK) >>
  1996. SVM_IOIO_SIZE_SHIFT;
  1997. gpa = svm->nested.vmcb_iopm + (port / 8);
  1998. start_bit = port % 8;
  1999. iopm_len = (start_bit + size > 8) ? 2 : 1;
  2000. mask = (0xf >> (4 - size)) << start_bit;
  2001. val = 0;
  2002. if (kvm_vcpu_read_guest(&svm->vcpu, gpa, &val, iopm_len))
  2003. return NESTED_EXIT_DONE;
  2004. return (val & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
  2005. }
  2006. static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
  2007. {
  2008. u32 offset, msr, value;
  2009. int write, mask;
  2010. if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
  2011. return NESTED_EXIT_HOST;
  2012. msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  2013. offset = svm_msrpm_offset(msr);
  2014. write = svm->vmcb->control.exit_info_1 & 1;
  2015. mask = 1 << ((2 * (msr & 0xf)) + write);
  2016. if (offset == MSR_INVALID)
  2017. return NESTED_EXIT_DONE;
  2018. /* Offset is in 32 bit units but need in 8 bit units */
  2019. offset *= 4;
  2020. if (kvm_vcpu_read_guest(&svm->vcpu, svm->nested.vmcb_msrpm + offset, &value, 4))
  2021. return NESTED_EXIT_DONE;
  2022. return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
  2023. }
  2024. static int nested_svm_exit_special(struct vcpu_svm *svm)
  2025. {
  2026. u32 exit_code = svm->vmcb->control.exit_code;
  2027. switch (exit_code) {
  2028. case SVM_EXIT_INTR:
  2029. case SVM_EXIT_NMI:
  2030. case SVM_EXIT_EXCP_BASE + MC_VECTOR:
  2031. return NESTED_EXIT_HOST;
  2032. case SVM_EXIT_NPF:
  2033. /* For now we are always handling NPFs when using them */
  2034. if (npt_enabled)
  2035. return NESTED_EXIT_HOST;
  2036. break;
  2037. case SVM_EXIT_EXCP_BASE + PF_VECTOR:
  2038. /* When we're shadowing, trap PFs, but not async PF */
  2039. if (!npt_enabled && svm->apf_reason == 0)
  2040. return NESTED_EXIT_HOST;
  2041. break;
  2042. default:
  2043. break;
  2044. }
  2045. return NESTED_EXIT_CONTINUE;
  2046. }
  2047. /*
  2048. * If this function returns true, this #vmexit was already handled
  2049. */
  2050. static int nested_svm_intercept(struct vcpu_svm *svm)
  2051. {
  2052. u32 exit_code = svm->vmcb->control.exit_code;
  2053. int vmexit = NESTED_EXIT_HOST;
  2054. switch (exit_code) {
  2055. case SVM_EXIT_MSR:
  2056. vmexit = nested_svm_exit_handled_msr(svm);
  2057. break;
  2058. case SVM_EXIT_IOIO:
  2059. vmexit = nested_svm_intercept_ioio(svm);
  2060. break;
  2061. case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: {
  2062. u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0);
  2063. if (svm->nested.intercept_cr & bit)
  2064. vmexit = NESTED_EXIT_DONE;
  2065. break;
  2066. }
  2067. case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: {
  2068. u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0);
  2069. if (svm->nested.intercept_dr & bit)
  2070. vmexit = NESTED_EXIT_DONE;
  2071. break;
  2072. }
  2073. case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
  2074. u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
  2075. if (svm->nested.intercept_exceptions & excp_bits)
  2076. vmexit = NESTED_EXIT_DONE;
  2077. /* async page fault always cause vmexit */
  2078. else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
  2079. svm->apf_reason != 0)
  2080. vmexit = NESTED_EXIT_DONE;
  2081. break;
  2082. }
  2083. case SVM_EXIT_ERR: {
  2084. vmexit = NESTED_EXIT_DONE;
  2085. break;
  2086. }
  2087. default: {
  2088. u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
  2089. if (svm->nested.intercept & exit_bits)
  2090. vmexit = NESTED_EXIT_DONE;
  2091. }
  2092. }
  2093. return vmexit;
  2094. }
  2095. static int nested_svm_exit_handled(struct vcpu_svm *svm)
  2096. {
  2097. int vmexit;
  2098. vmexit = nested_svm_intercept(svm);
  2099. if (vmexit == NESTED_EXIT_DONE)
  2100. nested_svm_vmexit(svm);
  2101. return vmexit;
  2102. }
  2103. static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
  2104. {
  2105. struct vmcb_control_area *dst = &dst_vmcb->control;
  2106. struct vmcb_control_area *from = &from_vmcb->control;
  2107. dst->intercept_cr = from->intercept_cr;
  2108. dst->intercept_dr = from->intercept_dr;
  2109. dst->intercept_exceptions = from->intercept_exceptions;
  2110. dst->intercept = from->intercept;
  2111. dst->iopm_base_pa = from->iopm_base_pa;
  2112. dst->msrpm_base_pa = from->msrpm_base_pa;
  2113. dst->tsc_offset = from->tsc_offset;
  2114. dst->asid = from->asid;
  2115. dst->tlb_ctl = from->tlb_ctl;
  2116. dst->int_ctl = from->int_ctl;
  2117. dst->int_vector = from->int_vector;
  2118. dst->int_state = from->int_state;
  2119. dst->exit_code = from->exit_code;
  2120. dst->exit_code_hi = from->exit_code_hi;
  2121. dst->exit_info_1 = from->exit_info_1;
  2122. dst->exit_info_2 = from->exit_info_2;
  2123. dst->exit_int_info = from->exit_int_info;
  2124. dst->exit_int_info_err = from->exit_int_info_err;
  2125. dst->nested_ctl = from->nested_ctl;
  2126. dst->event_inj = from->event_inj;
  2127. dst->event_inj_err = from->event_inj_err;
  2128. dst->nested_cr3 = from->nested_cr3;
  2129. dst->lbr_ctl = from->lbr_ctl;
  2130. }
  2131. static int nested_svm_vmexit(struct vcpu_svm *svm)
  2132. {
  2133. struct vmcb *nested_vmcb;
  2134. struct vmcb *hsave = svm->nested.hsave;
  2135. struct vmcb *vmcb = svm->vmcb;
  2136. struct page *page;
  2137. trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
  2138. vmcb->control.exit_info_1,
  2139. vmcb->control.exit_info_2,
  2140. vmcb->control.exit_int_info,
  2141. vmcb->control.exit_int_info_err,
  2142. KVM_ISA_SVM);
  2143. nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
  2144. if (!nested_vmcb)
  2145. return 1;
  2146. /* Exit Guest-Mode */
  2147. leave_guest_mode(&svm->vcpu);
  2148. svm->nested.vmcb = 0;
  2149. /* Give the current vmcb to the guest */
  2150. disable_gif(svm);
  2151. nested_vmcb->save.es = vmcb->save.es;
  2152. nested_vmcb->save.cs = vmcb->save.cs;
  2153. nested_vmcb->save.ss = vmcb->save.ss;
  2154. nested_vmcb->save.ds = vmcb->save.ds;
  2155. nested_vmcb->save.gdtr = vmcb->save.gdtr;
  2156. nested_vmcb->save.idtr = vmcb->save.idtr;
  2157. nested_vmcb->save.efer = svm->vcpu.arch.efer;
  2158. nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
  2159. nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu);
  2160. nested_vmcb->save.cr2 = vmcb->save.cr2;
  2161. nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
  2162. nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu);
  2163. nested_vmcb->save.rip = vmcb->save.rip;
  2164. nested_vmcb->save.rsp = vmcb->save.rsp;
  2165. nested_vmcb->save.rax = vmcb->save.rax;
  2166. nested_vmcb->save.dr7 = vmcb->save.dr7;
  2167. nested_vmcb->save.dr6 = vmcb->save.dr6;
  2168. nested_vmcb->save.cpl = vmcb->save.cpl;
  2169. nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
  2170. nested_vmcb->control.int_vector = vmcb->control.int_vector;
  2171. nested_vmcb->control.int_state = vmcb->control.int_state;
  2172. nested_vmcb->control.exit_code = vmcb->control.exit_code;
  2173. nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
  2174. nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
  2175. nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
  2176. nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
  2177. nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
  2178. if (svm->nrips_enabled)
  2179. nested_vmcb->control.next_rip = vmcb->control.next_rip;
  2180. /*
  2181. * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
  2182. * to make sure that we do not lose injected events. So check event_inj
  2183. * here and copy it to exit_int_info if it is valid.
  2184. * Exit_int_info and event_inj can't be both valid because the case
  2185. * below only happens on a VMRUN instruction intercept which has
  2186. * no valid exit_int_info set.
  2187. */
  2188. if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
  2189. struct vmcb_control_area *nc = &nested_vmcb->control;
  2190. nc->exit_int_info = vmcb->control.event_inj;
  2191. nc->exit_int_info_err = vmcb->control.event_inj_err;
  2192. }
  2193. nested_vmcb->control.tlb_ctl = 0;
  2194. nested_vmcb->control.event_inj = 0;
  2195. nested_vmcb->control.event_inj_err = 0;
  2196. /* We always set V_INTR_MASKING and remember the old value in hflags */
  2197. if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
  2198. nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
  2199. /* Restore the original control entries */
  2200. copy_vmcb_control_area(vmcb, hsave);
  2201. kvm_clear_exception_queue(&svm->vcpu);
  2202. kvm_clear_interrupt_queue(&svm->vcpu);
  2203. svm->nested.nested_cr3 = 0;
  2204. /* Restore selected save entries */
  2205. svm->vmcb->save.es = hsave->save.es;
  2206. svm->vmcb->save.cs = hsave->save.cs;
  2207. svm->vmcb->save.ss = hsave->save.ss;
  2208. svm->vmcb->save.ds = hsave->save.ds;
  2209. svm->vmcb->save.gdtr = hsave->save.gdtr;
  2210. svm->vmcb->save.idtr = hsave->save.idtr;
  2211. kvm_set_rflags(&svm->vcpu, hsave->save.rflags);
  2212. svm_set_efer(&svm->vcpu, hsave->save.efer);
  2213. svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
  2214. svm_set_cr4(&svm->vcpu, hsave->save.cr4);
  2215. if (npt_enabled) {
  2216. svm->vmcb->save.cr3 = hsave->save.cr3;
  2217. svm->vcpu.arch.cr3 = hsave->save.cr3;
  2218. } else {
  2219. (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
  2220. }
  2221. kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
  2222. kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
  2223. kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
  2224. svm->vmcb->save.dr7 = 0;
  2225. svm->vmcb->save.cpl = 0;
  2226. svm->vmcb->control.exit_int_info = 0;
  2227. mark_all_dirty(svm->vmcb);
  2228. nested_svm_unmap(page);
  2229. nested_svm_uninit_mmu_context(&svm->vcpu);
  2230. kvm_mmu_reset_context(&svm->vcpu);
  2231. kvm_mmu_load(&svm->vcpu);
  2232. return 0;
  2233. }
  2234. static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
  2235. {
  2236. /*
  2237. * This function merges the msr permission bitmaps of kvm and the
  2238. * nested vmcb. It is optimized in that it only merges the parts where
  2239. * the kvm msr permission bitmap may contain zero bits
  2240. */
  2241. int i;
  2242. if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
  2243. return true;
  2244. for (i = 0; i < MSRPM_OFFSETS; i++) {
  2245. u32 value, p;
  2246. u64 offset;
  2247. if (msrpm_offsets[i] == 0xffffffff)
  2248. break;
  2249. p = msrpm_offsets[i];
  2250. offset = svm->nested.vmcb_msrpm + (p * 4);
  2251. if (kvm_vcpu_read_guest(&svm->vcpu, offset, &value, 4))
  2252. return false;
  2253. svm->nested.msrpm[p] = svm->msrpm[p] | value;
  2254. }
  2255. svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
  2256. return true;
  2257. }
  2258. static bool nested_vmcb_checks(struct vmcb *vmcb)
  2259. {
  2260. if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
  2261. return false;
  2262. if (vmcb->control.asid == 0)
  2263. return false;
  2264. if (vmcb->control.nested_ctl && !npt_enabled)
  2265. return false;
  2266. return true;
  2267. }
  2268. static bool nested_svm_vmrun(struct vcpu_svm *svm)
  2269. {
  2270. struct vmcb *nested_vmcb;
  2271. struct vmcb *hsave = svm->nested.hsave;
  2272. struct vmcb *vmcb = svm->vmcb;
  2273. struct page *page;
  2274. u64 vmcb_gpa;
  2275. vmcb_gpa = svm->vmcb->save.rax;
  2276. nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
  2277. if (!nested_vmcb)
  2278. return false;
  2279. if (!nested_vmcb_checks(nested_vmcb)) {
  2280. nested_vmcb->control.exit_code = SVM_EXIT_ERR;
  2281. nested_vmcb->control.exit_code_hi = 0;
  2282. nested_vmcb->control.exit_info_1 = 0;
  2283. nested_vmcb->control.exit_info_2 = 0;
  2284. nested_svm_unmap(page);
  2285. return false;
  2286. }
  2287. trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
  2288. nested_vmcb->save.rip,
  2289. nested_vmcb->control.int_ctl,
  2290. nested_vmcb->control.event_inj,
  2291. nested_vmcb->control.nested_ctl);
  2292. trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff,
  2293. nested_vmcb->control.intercept_cr >> 16,
  2294. nested_vmcb->control.intercept_exceptions,
  2295. nested_vmcb->control.intercept);
  2296. /* Clear internal status */
  2297. kvm_clear_exception_queue(&svm->vcpu);
  2298. kvm_clear_interrupt_queue(&svm->vcpu);
  2299. /*
  2300. * Save the old vmcb, so we don't need to pick what we save, but can
  2301. * restore everything when a VMEXIT occurs
  2302. */
  2303. hsave->save.es = vmcb->save.es;
  2304. hsave->save.cs = vmcb->save.cs;
  2305. hsave->save.ss = vmcb->save.ss;
  2306. hsave->save.ds = vmcb->save.ds;
  2307. hsave->save.gdtr = vmcb->save.gdtr;
  2308. hsave->save.idtr = vmcb->save.idtr;
  2309. hsave->save.efer = svm->vcpu.arch.efer;
  2310. hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
  2311. hsave->save.cr4 = svm->vcpu.arch.cr4;
  2312. hsave->save.rflags = kvm_get_rflags(&svm->vcpu);
  2313. hsave->save.rip = kvm_rip_read(&svm->vcpu);
  2314. hsave->save.rsp = vmcb->save.rsp;
  2315. hsave->save.rax = vmcb->save.rax;
  2316. if (npt_enabled)
  2317. hsave->save.cr3 = vmcb->save.cr3;
  2318. else
  2319. hsave->save.cr3 = kvm_read_cr3(&svm->vcpu);
  2320. copy_vmcb_control_area(hsave, vmcb);
  2321. if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF)
  2322. svm->vcpu.arch.hflags |= HF_HIF_MASK;
  2323. else
  2324. svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
  2325. if (nested_vmcb->control.nested_ctl) {
  2326. kvm_mmu_unload(&svm->vcpu);
  2327. svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
  2328. nested_svm_init_mmu_context(&svm->vcpu);
  2329. }
  2330. /* Load the nested guest state */
  2331. svm->vmcb->save.es = nested_vmcb->save.es;
  2332. svm->vmcb->save.cs = nested_vmcb->save.cs;
  2333. svm->vmcb->save.ss = nested_vmcb->save.ss;
  2334. svm->vmcb->save.ds = nested_vmcb->save.ds;
  2335. svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
  2336. svm->vmcb->save.idtr = nested_vmcb->save.idtr;
  2337. kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags);
  2338. svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
  2339. svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
  2340. svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
  2341. if (npt_enabled) {
  2342. svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
  2343. svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
  2344. } else
  2345. (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
  2346. /* Guest paging mode is active - reset mmu */
  2347. kvm_mmu_reset_context(&svm->vcpu);
  2348. svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
  2349. kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
  2350. kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
  2351. kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
  2352. /* In case we don't even reach vcpu_run, the fields are not updated */
  2353. svm->vmcb->save.rax = nested_vmcb->save.rax;
  2354. svm->vmcb->save.rsp = nested_vmcb->save.rsp;
  2355. svm->vmcb->save.rip = nested_vmcb->save.rip;
  2356. svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
  2357. svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
  2358. svm->vmcb->save.cpl = nested_vmcb->save.cpl;
  2359. svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
  2360. svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
  2361. /* cache intercepts */
  2362. svm->nested.intercept_cr = nested_vmcb->control.intercept_cr;
  2363. svm->nested.intercept_dr = nested_vmcb->control.intercept_dr;
  2364. svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
  2365. svm->nested.intercept = nested_vmcb->control.intercept;
  2366. svm_flush_tlb(&svm->vcpu);
  2367. svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
  2368. if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
  2369. svm->vcpu.arch.hflags |= HF_VINTR_MASK;
  2370. else
  2371. svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
  2372. if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
  2373. /* We only want the cr8 intercept bits of the guest */
  2374. clr_cr_intercept(svm, INTERCEPT_CR8_READ);
  2375. clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  2376. }
  2377. /* We don't want to see VMMCALLs from a nested guest */
  2378. clr_intercept(svm, INTERCEPT_VMMCALL);
  2379. svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
  2380. svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
  2381. svm->vmcb->control.int_state = nested_vmcb->control.int_state;
  2382. svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
  2383. svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
  2384. svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
  2385. nested_svm_unmap(page);
  2386. /* Enter Guest-Mode */
  2387. enter_guest_mode(&svm->vcpu);
  2388. /*
  2389. * Merge guest and host intercepts - must be called with vcpu in
  2390. * guest-mode to take affect here
  2391. */
  2392. recalc_intercepts(svm);
  2393. svm->nested.vmcb = vmcb_gpa;
  2394. enable_gif(svm);
  2395. mark_all_dirty(svm->vmcb);
  2396. return true;
  2397. }
  2398. static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
  2399. {
  2400. to_vmcb->save.fs = from_vmcb->save.fs;
  2401. to_vmcb->save.gs = from_vmcb->save.gs;
  2402. to_vmcb->save.tr = from_vmcb->save.tr;
  2403. to_vmcb->save.ldtr = from_vmcb->save.ldtr;
  2404. to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
  2405. to_vmcb->save.star = from_vmcb->save.star;
  2406. to_vmcb->save.lstar = from_vmcb->save.lstar;
  2407. to_vmcb->save.cstar = from_vmcb->save.cstar;
  2408. to_vmcb->save.sfmask = from_vmcb->save.sfmask;
  2409. to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
  2410. to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
  2411. to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
  2412. }
  2413. static int vmload_interception(struct vcpu_svm *svm)
  2414. {
  2415. struct vmcb *nested_vmcb;
  2416. struct page *page;
  2417. if (nested_svm_check_permissions(svm))
  2418. return 1;
  2419. nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
  2420. if (!nested_vmcb)
  2421. return 1;
  2422. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2423. skip_emulated_instruction(&svm->vcpu);
  2424. nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
  2425. nested_svm_unmap(page);
  2426. return 1;
  2427. }
  2428. static int vmsave_interception(struct vcpu_svm *svm)
  2429. {
  2430. struct vmcb *nested_vmcb;
  2431. struct page *page;
  2432. if (nested_svm_check_permissions(svm))
  2433. return 1;
  2434. nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
  2435. if (!nested_vmcb)
  2436. return 1;
  2437. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2438. skip_emulated_instruction(&svm->vcpu);
  2439. nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
  2440. nested_svm_unmap(page);
  2441. return 1;
  2442. }
  2443. static int vmrun_interception(struct vcpu_svm *svm)
  2444. {
  2445. if (nested_svm_check_permissions(svm))
  2446. return 1;
  2447. /* Save rip after vmrun instruction */
  2448. kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
  2449. if (!nested_svm_vmrun(svm))
  2450. return 1;
  2451. if (!nested_svm_vmrun_msrpm(svm))
  2452. goto failed;
  2453. return 1;
  2454. failed:
  2455. svm->vmcb->control.exit_code = SVM_EXIT_ERR;
  2456. svm->vmcb->control.exit_code_hi = 0;
  2457. svm->vmcb->control.exit_info_1 = 0;
  2458. svm->vmcb->control.exit_info_2 = 0;
  2459. nested_svm_vmexit(svm);
  2460. return 1;
  2461. }
  2462. static int stgi_interception(struct vcpu_svm *svm)
  2463. {
  2464. if (nested_svm_check_permissions(svm))
  2465. return 1;
  2466. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2467. skip_emulated_instruction(&svm->vcpu);
  2468. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  2469. enable_gif(svm);
  2470. return 1;
  2471. }
  2472. static int clgi_interception(struct vcpu_svm *svm)
  2473. {
  2474. if (nested_svm_check_permissions(svm))
  2475. return 1;
  2476. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2477. skip_emulated_instruction(&svm->vcpu);
  2478. disable_gif(svm);
  2479. /* After a CLGI no interrupts should come */
  2480. if (!kvm_vcpu_apicv_active(&svm->vcpu)) {
  2481. svm_clear_vintr(svm);
  2482. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  2483. mark_dirty(svm->vmcb, VMCB_INTR);
  2484. }
  2485. return 1;
  2486. }
  2487. static int invlpga_interception(struct vcpu_svm *svm)
  2488. {
  2489. struct kvm_vcpu *vcpu = &svm->vcpu;
  2490. trace_kvm_invlpga(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RCX),
  2491. kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
  2492. /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
  2493. kvm_mmu_invlpg(vcpu, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
  2494. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2495. skip_emulated_instruction(&svm->vcpu);
  2496. return 1;
  2497. }
  2498. static int skinit_interception(struct vcpu_svm *svm)
  2499. {
  2500. trace_kvm_skinit(svm->vmcb->save.rip, kvm_register_read(&svm->vcpu, VCPU_REGS_RAX));
  2501. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  2502. return 1;
  2503. }
  2504. static int wbinvd_interception(struct vcpu_svm *svm)
  2505. {
  2506. return kvm_emulate_wbinvd(&svm->vcpu);
  2507. }
  2508. static int xsetbv_interception(struct vcpu_svm *svm)
  2509. {
  2510. u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
  2511. u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
  2512. if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
  2513. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  2514. skip_emulated_instruction(&svm->vcpu);
  2515. }
  2516. return 1;
  2517. }
  2518. static int task_switch_interception(struct vcpu_svm *svm)
  2519. {
  2520. u16 tss_selector;
  2521. int reason;
  2522. int int_type = svm->vmcb->control.exit_int_info &
  2523. SVM_EXITINTINFO_TYPE_MASK;
  2524. int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
  2525. uint32_t type =
  2526. svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
  2527. uint32_t idt_v =
  2528. svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
  2529. bool has_error_code = false;
  2530. u32 error_code = 0;
  2531. tss_selector = (u16)svm->vmcb->control.exit_info_1;
  2532. if (svm->vmcb->control.exit_info_2 &
  2533. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
  2534. reason = TASK_SWITCH_IRET;
  2535. else if (svm->vmcb->control.exit_info_2 &
  2536. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
  2537. reason = TASK_SWITCH_JMP;
  2538. else if (idt_v)
  2539. reason = TASK_SWITCH_GATE;
  2540. else
  2541. reason = TASK_SWITCH_CALL;
  2542. if (reason == TASK_SWITCH_GATE) {
  2543. switch (type) {
  2544. case SVM_EXITINTINFO_TYPE_NMI:
  2545. svm->vcpu.arch.nmi_injected = false;
  2546. break;
  2547. case SVM_EXITINTINFO_TYPE_EXEPT:
  2548. if (svm->vmcb->control.exit_info_2 &
  2549. (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
  2550. has_error_code = true;
  2551. error_code =
  2552. (u32)svm->vmcb->control.exit_info_2;
  2553. }
  2554. kvm_clear_exception_queue(&svm->vcpu);
  2555. break;
  2556. case SVM_EXITINTINFO_TYPE_INTR:
  2557. kvm_clear_interrupt_queue(&svm->vcpu);
  2558. break;
  2559. default:
  2560. break;
  2561. }
  2562. }
  2563. if (reason != TASK_SWITCH_GATE ||
  2564. int_type == SVM_EXITINTINFO_TYPE_SOFT ||
  2565. (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
  2566. (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
  2567. skip_emulated_instruction(&svm->vcpu);
  2568. if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
  2569. int_vec = -1;
  2570. if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
  2571. has_error_code, error_code) == EMULATE_FAIL) {
  2572. svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  2573. svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  2574. svm->vcpu.run->internal.ndata = 0;
  2575. return 0;
  2576. }
  2577. return 1;
  2578. }
  2579. static int cpuid_interception(struct vcpu_svm *svm)
  2580. {
  2581. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  2582. return kvm_emulate_cpuid(&svm->vcpu);
  2583. }
  2584. static int iret_interception(struct vcpu_svm *svm)
  2585. {
  2586. ++svm->vcpu.stat.nmi_window_exits;
  2587. clr_intercept(svm, INTERCEPT_IRET);
  2588. svm->vcpu.arch.hflags |= HF_IRET_MASK;
  2589. svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
  2590. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  2591. return 1;
  2592. }
  2593. static int invlpg_interception(struct vcpu_svm *svm)
  2594. {
  2595. if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
  2596. return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
  2597. kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
  2598. skip_emulated_instruction(&svm->vcpu);
  2599. return 1;
  2600. }
  2601. static int emulate_on_interception(struct vcpu_svm *svm)
  2602. {
  2603. return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE;
  2604. }
  2605. static int rdpmc_interception(struct vcpu_svm *svm)
  2606. {
  2607. int err;
  2608. if (!static_cpu_has(X86_FEATURE_NRIPS))
  2609. return emulate_on_interception(svm);
  2610. err = kvm_rdpmc(&svm->vcpu);
  2611. return kvm_complete_insn_gp(&svm->vcpu, err);
  2612. }
  2613. static bool check_selective_cr0_intercepted(struct vcpu_svm *svm,
  2614. unsigned long val)
  2615. {
  2616. unsigned long cr0 = svm->vcpu.arch.cr0;
  2617. bool ret = false;
  2618. u64 intercept;
  2619. intercept = svm->nested.intercept;
  2620. if (!is_guest_mode(&svm->vcpu) ||
  2621. (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
  2622. return false;
  2623. cr0 &= ~SVM_CR0_SELECTIVE_MASK;
  2624. val &= ~SVM_CR0_SELECTIVE_MASK;
  2625. if (cr0 ^ val) {
  2626. svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
  2627. ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
  2628. }
  2629. return ret;
  2630. }
  2631. #define CR_VALID (1ULL << 63)
  2632. static int cr_interception(struct vcpu_svm *svm)
  2633. {
  2634. int reg, cr;
  2635. unsigned long val;
  2636. int err;
  2637. if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
  2638. return emulate_on_interception(svm);
  2639. if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
  2640. return emulate_on_interception(svm);
  2641. reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
  2642. if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
  2643. cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
  2644. else
  2645. cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
  2646. err = 0;
  2647. if (cr >= 16) { /* mov to cr */
  2648. cr -= 16;
  2649. val = kvm_register_read(&svm->vcpu, reg);
  2650. switch (cr) {
  2651. case 0:
  2652. if (!check_selective_cr0_intercepted(svm, val))
  2653. err = kvm_set_cr0(&svm->vcpu, val);
  2654. else
  2655. return 1;
  2656. break;
  2657. case 3:
  2658. err = kvm_set_cr3(&svm->vcpu, val);
  2659. break;
  2660. case 4:
  2661. err = kvm_set_cr4(&svm->vcpu, val);
  2662. break;
  2663. case 8:
  2664. err = kvm_set_cr8(&svm->vcpu, val);
  2665. break;
  2666. default:
  2667. WARN(1, "unhandled write to CR%d", cr);
  2668. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  2669. return 1;
  2670. }
  2671. } else { /* mov from cr */
  2672. switch (cr) {
  2673. case 0:
  2674. val = kvm_read_cr0(&svm->vcpu);
  2675. break;
  2676. case 2:
  2677. val = svm->vcpu.arch.cr2;
  2678. break;
  2679. case 3:
  2680. val = kvm_read_cr3(&svm->vcpu);
  2681. break;
  2682. case 4:
  2683. val = kvm_read_cr4(&svm->vcpu);
  2684. break;
  2685. case 8:
  2686. val = kvm_get_cr8(&svm->vcpu);
  2687. break;
  2688. default:
  2689. WARN(1, "unhandled read from CR%d", cr);
  2690. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  2691. return 1;
  2692. }
  2693. kvm_register_write(&svm->vcpu, reg, val);
  2694. }
  2695. return kvm_complete_insn_gp(&svm->vcpu, err);
  2696. }
  2697. static int dr_interception(struct vcpu_svm *svm)
  2698. {
  2699. int reg, dr;
  2700. unsigned long val;
  2701. if (svm->vcpu.guest_debug == 0) {
  2702. /*
  2703. * No more DR vmexits; force a reload of the debug registers
  2704. * and reenter on this instruction. The next vmexit will
  2705. * retrieve the full state of the debug registers.
  2706. */
  2707. clr_dr_intercepts(svm);
  2708. svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
  2709. return 1;
  2710. }
  2711. if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
  2712. return emulate_on_interception(svm);
  2713. reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
  2714. dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
  2715. if (dr >= 16) { /* mov to DRn */
  2716. if (!kvm_require_dr(&svm->vcpu, dr - 16))
  2717. return 1;
  2718. val = kvm_register_read(&svm->vcpu, reg);
  2719. kvm_set_dr(&svm->vcpu, dr - 16, val);
  2720. } else {
  2721. if (!kvm_require_dr(&svm->vcpu, dr))
  2722. return 1;
  2723. kvm_get_dr(&svm->vcpu, dr, &val);
  2724. kvm_register_write(&svm->vcpu, reg, val);
  2725. }
  2726. skip_emulated_instruction(&svm->vcpu);
  2727. return 1;
  2728. }
  2729. static int cr8_write_interception(struct vcpu_svm *svm)
  2730. {
  2731. struct kvm_run *kvm_run = svm->vcpu.run;
  2732. int r;
  2733. u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
  2734. /* instruction emulation calls kvm_set_cr8() */
  2735. r = cr_interception(svm);
  2736. if (lapic_in_kernel(&svm->vcpu))
  2737. return r;
  2738. if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
  2739. return r;
  2740. kvm_run->exit_reason = KVM_EXIT_SET_TPR;
  2741. return 0;
  2742. }
  2743. static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
  2744. {
  2745. struct vcpu_svm *svm = to_svm(vcpu);
  2746. switch (msr_info->index) {
  2747. case MSR_IA32_TSC: {
  2748. msr_info->data = svm->vmcb->control.tsc_offset +
  2749. kvm_scale_tsc(vcpu, rdtsc());
  2750. break;
  2751. }
  2752. case MSR_STAR:
  2753. msr_info->data = svm->vmcb->save.star;
  2754. break;
  2755. #ifdef CONFIG_X86_64
  2756. case MSR_LSTAR:
  2757. msr_info->data = svm->vmcb->save.lstar;
  2758. break;
  2759. case MSR_CSTAR:
  2760. msr_info->data = svm->vmcb->save.cstar;
  2761. break;
  2762. case MSR_KERNEL_GS_BASE:
  2763. msr_info->data = svm->vmcb->save.kernel_gs_base;
  2764. break;
  2765. case MSR_SYSCALL_MASK:
  2766. msr_info->data = svm->vmcb->save.sfmask;
  2767. break;
  2768. #endif
  2769. case MSR_IA32_SYSENTER_CS:
  2770. msr_info->data = svm->vmcb->save.sysenter_cs;
  2771. break;
  2772. case MSR_IA32_SYSENTER_EIP:
  2773. msr_info->data = svm->sysenter_eip;
  2774. break;
  2775. case MSR_IA32_SYSENTER_ESP:
  2776. msr_info->data = svm->sysenter_esp;
  2777. break;
  2778. case MSR_TSC_AUX:
  2779. if (!boot_cpu_has(X86_FEATURE_RDTSCP))
  2780. return 1;
  2781. msr_info->data = svm->tsc_aux;
  2782. break;
  2783. /*
  2784. * Nobody will change the following 5 values in the VMCB so we can
  2785. * safely return them on rdmsr. They will always be 0 until LBRV is
  2786. * implemented.
  2787. */
  2788. case MSR_IA32_DEBUGCTLMSR:
  2789. msr_info->data = svm->vmcb->save.dbgctl;
  2790. break;
  2791. case MSR_IA32_LASTBRANCHFROMIP:
  2792. msr_info->data = svm->vmcb->save.br_from;
  2793. break;
  2794. case MSR_IA32_LASTBRANCHTOIP:
  2795. msr_info->data = svm->vmcb->save.br_to;
  2796. break;
  2797. case MSR_IA32_LASTINTFROMIP:
  2798. msr_info->data = svm->vmcb->save.last_excp_from;
  2799. break;
  2800. case MSR_IA32_LASTINTTOIP:
  2801. msr_info->data = svm->vmcb->save.last_excp_to;
  2802. break;
  2803. case MSR_VM_HSAVE_PA:
  2804. msr_info->data = svm->nested.hsave_msr;
  2805. break;
  2806. case MSR_VM_CR:
  2807. msr_info->data = svm->nested.vm_cr_msr;
  2808. break;
  2809. case MSR_IA32_UCODE_REV:
  2810. msr_info->data = 0x01000065;
  2811. break;
  2812. case MSR_F15H_IC_CFG: {
  2813. int family, model;
  2814. family = guest_cpuid_family(vcpu);
  2815. model = guest_cpuid_model(vcpu);
  2816. if (family < 0 || model < 0)
  2817. return kvm_get_msr_common(vcpu, msr_info);
  2818. msr_info->data = 0;
  2819. if (family == 0x15 &&
  2820. (model >= 0x2 && model < 0x20))
  2821. msr_info->data = 0x1E;
  2822. }
  2823. break;
  2824. default:
  2825. return kvm_get_msr_common(vcpu, msr_info);
  2826. }
  2827. return 0;
  2828. }
  2829. static int rdmsr_interception(struct vcpu_svm *svm)
  2830. {
  2831. u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
  2832. struct msr_data msr_info;
  2833. msr_info.index = ecx;
  2834. msr_info.host_initiated = false;
  2835. if (svm_get_msr(&svm->vcpu, &msr_info)) {
  2836. trace_kvm_msr_read_ex(ecx);
  2837. kvm_inject_gp(&svm->vcpu, 0);
  2838. } else {
  2839. trace_kvm_msr_read(ecx, msr_info.data);
  2840. kvm_register_write(&svm->vcpu, VCPU_REGS_RAX,
  2841. msr_info.data & 0xffffffff);
  2842. kvm_register_write(&svm->vcpu, VCPU_REGS_RDX,
  2843. msr_info.data >> 32);
  2844. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  2845. skip_emulated_instruction(&svm->vcpu);
  2846. }
  2847. return 1;
  2848. }
  2849. static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
  2850. {
  2851. struct vcpu_svm *svm = to_svm(vcpu);
  2852. int svm_dis, chg_mask;
  2853. if (data & ~SVM_VM_CR_VALID_MASK)
  2854. return 1;
  2855. chg_mask = SVM_VM_CR_VALID_MASK;
  2856. if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
  2857. chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
  2858. svm->nested.vm_cr_msr &= ~chg_mask;
  2859. svm->nested.vm_cr_msr |= (data & chg_mask);
  2860. svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
  2861. /* check for svm_disable while efer.svme is set */
  2862. if (svm_dis && (vcpu->arch.efer & EFER_SVME))
  2863. return 1;
  2864. return 0;
  2865. }
  2866. static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
  2867. {
  2868. struct vcpu_svm *svm = to_svm(vcpu);
  2869. u32 ecx = msr->index;
  2870. u64 data = msr->data;
  2871. switch (ecx) {
  2872. case MSR_IA32_TSC:
  2873. kvm_write_tsc(vcpu, msr);
  2874. break;
  2875. case MSR_STAR:
  2876. svm->vmcb->save.star = data;
  2877. break;
  2878. #ifdef CONFIG_X86_64
  2879. case MSR_LSTAR:
  2880. svm->vmcb->save.lstar = data;
  2881. break;
  2882. case MSR_CSTAR:
  2883. svm->vmcb->save.cstar = data;
  2884. break;
  2885. case MSR_KERNEL_GS_BASE:
  2886. svm->vmcb->save.kernel_gs_base = data;
  2887. break;
  2888. case MSR_SYSCALL_MASK:
  2889. svm->vmcb->save.sfmask = data;
  2890. break;
  2891. #endif
  2892. case MSR_IA32_SYSENTER_CS:
  2893. svm->vmcb->save.sysenter_cs = data;
  2894. break;
  2895. case MSR_IA32_SYSENTER_EIP:
  2896. svm->sysenter_eip = data;
  2897. svm->vmcb->save.sysenter_eip = data;
  2898. break;
  2899. case MSR_IA32_SYSENTER_ESP:
  2900. svm->sysenter_esp = data;
  2901. svm->vmcb->save.sysenter_esp = data;
  2902. break;
  2903. case MSR_TSC_AUX:
  2904. if (!boot_cpu_has(X86_FEATURE_RDTSCP))
  2905. return 1;
  2906. /*
  2907. * This is rare, so we update the MSR here instead of using
  2908. * direct_access_msrs. Doing that would require a rdmsr in
  2909. * svm_vcpu_put.
  2910. */
  2911. svm->tsc_aux = data;
  2912. wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
  2913. break;
  2914. case MSR_IA32_DEBUGCTLMSR:
  2915. if (!boot_cpu_has(X86_FEATURE_LBRV)) {
  2916. vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
  2917. __func__, data);
  2918. break;
  2919. }
  2920. if (data & DEBUGCTL_RESERVED_BITS)
  2921. return 1;
  2922. svm->vmcb->save.dbgctl = data;
  2923. mark_dirty(svm->vmcb, VMCB_LBR);
  2924. if (data & (1ULL<<0))
  2925. svm_enable_lbrv(svm);
  2926. else
  2927. svm_disable_lbrv(svm);
  2928. break;
  2929. case MSR_VM_HSAVE_PA:
  2930. svm->nested.hsave_msr = data;
  2931. break;
  2932. case MSR_VM_CR:
  2933. return svm_set_vm_cr(vcpu, data);
  2934. case MSR_VM_IGNNE:
  2935. vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
  2936. break;
  2937. case MSR_IA32_APICBASE:
  2938. if (kvm_vcpu_apicv_active(vcpu))
  2939. avic_update_vapic_bar(to_svm(vcpu), data);
  2940. /* Follow through */
  2941. default:
  2942. return kvm_set_msr_common(vcpu, msr);
  2943. }
  2944. return 0;
  2945. }
  2946. static int wrmsr_interception(struct vcpu_svm *svm)
  2947. {
  2948. struct msr_data msr;
  2949. u32 ecx = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX);
  2950. u64 data = kvm_read_edx_eax(&svm->vcpu);
  2951. msr.data = data;
  2952. msr.index = ecx;
  2953. msr.host_initiated = false;
  2954. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  2955. if (kvm_set_msr(&svm->vcpu, &msr)) {
  2956. trace_kvm_msr_write_ex(ecx, data);
  2957. kvm_inject_gp(&svm->vcpu, 0);
  2958. } else {
  2959. trace_kvm_msr_write(ecx, data);
  2960. skip_emulated_instruction(&svm->vcpu);
  2961. }
  2962. return 1;
  2963. }
  2964. static int msr_interception(struct vcpu_svm *svm)
  2965. {
  2966. if (svm->vmcb->control.exit_info_1)
  2967. return wrmsr_interception(svm);
  2968. else
  2969. return rdmsr_interception(svm);
  2970. }
  2971. static int interrupt_window_interception(struct vcpu_svm *svm)
  2972. {
  2973. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  2974. svm_clear_vintr(svm);
  2975. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  2976. mark_dirty(svm->vmcb, VMCB_INTR);
  2977. ++svm->vcpu.stat.irq_window_exits;
  2978. return 1;
  2979. }
  2980. static int pause_interception(struct vcpu_svm *svm)
  2981. {
  2982. kvm_vcpu_on_spin(&(svm->vcpu));
  2983. return 1;
  2984. }
  2985. static int nop_interception(struct vcpu_svm *svm)
  2986. {
  2987. skip_emulated_instruction(&(svm->vcpu));
  2988. return 1;
  2989. }
  2990. static int monitor_interception(struct vcpu_svm *svm)
  2991. {
  2992. printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
  2993. return nop_interception(svm);
  2994. }
  2995. static int mwait_interception(struct vcpu_svm *svm)
  2996. {
  2997. printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
  2998. return nop_interception(svm);
  2999. }
  3000. enum avic_ipi_failure_cause {
  3001. AVIC_IPI_FAILURE_INVALID_INT_TYPE,
  3002. AVIC_IPI_FAILURE_TARGET_NOT_RUNNING,
  3003. AVIC_IPI_FAILURE_INVALID_TARGET,
  3004. AVIC_IPI_FAILURE_INVALID_BACKING_PAGE,
  3005. };
  3006. static int avic_incomplete_ipi_interception(struct vcpu_svm *svm)
  3007. {
  3008. u32 icrh = svm->vmcb->control.exit_info_1 >> 32;
  3009. u32 icrl = svm->vmcb->control.exit_info_1;
  3010. u32 id = svm->vmcb->control.exit_info_2 >> 32;
  3011. u32 index = svm->vmcb->control.exit_info_2 & 0xFF;
  3012. struct kvm_lapic *apic = svm->vcpu.arch.apic;
  3013. trace_kvm_avic_incomplete_ipi(svm->vcpu.vcpu_id, icrh, icrl, id, index);
  3014. switch (id) {
  3015. case AVIC_IPI_FAILURE_INVALID_INT_TYPE:
  3016. /*
  3017. * AVIC hardware handles the generation of
  3018. * IPIs when the specified Message Type is Fixed
  3019. * (also known as fixed delivery mode) and
  3020. * the Trigger Mode is edge-triggered. The hardware
  3021. * also supports self and broadcast delivery modes
  3022. * specified via the Destination Shorthand(DSH)
  3023. * field of the ICRL. Logical and physical APIC ID
  3024. * formats are supported. All other IPI types cause
  3025. * a #VMEXIT, which needs to emulated.
  3026. */
  3027. kvm_lapic_reg_write(apic, APIC_ICR2, icrh);
  3028. kvm_lapic_reg_write(apic, APIC_ICR, icrl);
  3029. break;
  3030. case AVIC_IPI_FAILURE_TARGET_NOT_RUNNING: {
  3031. int i;
  3032. struct kvm_vcpu *vcpu;
  3033. struct kvm *kvm = svm->vcpu.kvm;
  3034. struct kvm_lapic *apic = svm->vcpu.arch.apic;
  3035. /*
  3036. * At this point, we expect that the AVIC HW has already
  3037. * set the appropriate IRR bits on the valid target
  3038. * vcpus. So, we just need to kick the appropriate vcpu.
  3039. */
  3040. kvm_for_each_vcpu(i, vcpu, kvm) {
  3041. bool m = kvm_apic_match_dest(vcpu, apic,
  3042. icrl & KVM_APIC_SHORT_MASK,
  3043. GET_APIC_DEST_FIELD(icrh),
  3044. icrl & KVM_APIC_DEST_MASK);
  3045. if (m && !avic_vcpu_is_running(vcpu))
  3046. kvm_vcpu_wake_up(vcpu);
  3047. }
  3048. break;
  3049. }
  3050. case AVIC_IPI_FAILURE_INVALID_TARGET:
  3051. break;
  3052. case AVIC_IPI_FAILURE_INVALID_BACKING_PAGE:
  3053. WARN_ONCE(1, "Invalid backing page\n");
  3054. break;
  3055. default:
  3056. pr_err("Unknown IPI interception\n");
  3057. }
  3058. return 1;
  3059. }
  3060. static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat)
  3061. {
  3062. struct kvm_arch *vm_data = &vcpu->kvm->arch;
  3063. int index;
  3064. u32 *logical_apic_id_table;
  3065. int dlid = GET_APIC_LOGICAL_ID(ldr);
  3066. if (!dlid)
  3067. return NULL;
  3068. if (flat) { /* flat */
  3069. index = ffs(dlid) - 1;
  3070. if (index > 7)
  3071. return NULL;
  3072. } else { /* cluster */
  3073. int cluster = (dlid & 0xf0) >> 4;
  3074. int apic = ffs(dlid & 0x0f) - 1;
  3075. if ((apic < 0) || (apic > 7) ||
  3076. (cluster >= 0xf))
  3077. return NULL;
  3078. index = (cluster << 2) + apic;
  3079. }
  3080. logical_apic_id_table = (u32 *) page_address(vm_data->avic_logical_id_table_page);
  3081. return &logical_apic_id_table[index];
  3082. }
  3083. static int avic_ldr_write(struct kvm_vcpu *vcpu, u8 g_physical_id, u32 ldr,
  3084. bool valid)
  3085. {
  3086. bool flat;
  3087. u32 *entry, new_entry;
  3088. flat = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR) == APIC_DFR_FLAT;
  3089. entry = avic_get_logical_id_entry(vcpu, ldr, flat);
  3090. if (!entry)
  3091. return -EINVAL;
  3092. new_entry = READ_ONCE(*entry);
  3093. new_entry &= ~AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK;
  3094. new_entry |= (g_physical_id & AVIC_LOGICAL_ID_ENTRY_GUEST_PHYSICAL_ID_MASK);
  3095. if (valid)
  3096. new_entry |= AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
  3097. else
  3098. new_entry &= ~AVIC_LOGICAL_ID_ENTRY_VALID_MASK;
  3099. WRITE_ONCE(*entry, new_entry);
  3100. return 0;
  3101. }
  3102. static int avic_handle_ldr_update(struct kvm_vcpu *vcpu)
  3103. {
  3104. int ret;
  3105. struct vcpu_svm *svm = to_svm(vcpu);
  3106. u32 ldr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LDR);
  3107. if (!ldr)
  3108. return 1;
  3109. ret = avic_ldr_write(vcpu, vcpu->vcpu_id, ldr, true);
  3110. if (ret && svm->ldr_reg) {
  3111. avic_ldr_write(vcpu, 0, svm->ldr_reg, false);
  3112. svm->ldr_reg = 0;
  3113. } else {
  3114. svm->ldr_reg = ldr;
  3115. }
  3116. return ret;
  3117. }
  3118. static int avic_handle_apic_id_update(struct kvm_vcpu *vcpu)
  3119. {
  3120. u64 *old, *new;
  3121. struct vcpu_svm *svm = to_svm(vcpu);
  3122. u32 apic_id_reg = kvm_lapic_get_reg(vcpu->arch.apic, APIC_ID);
  3123. u32 id = (apic_id_reg >> 24) & 0xff;
  3124. if (vcpu->vcpu_id == id)
  3125. return 0;
  3126. old = avic_get_physical_id_entry(vcpu, vcpu->vcpu_id);
  3127. new = avic_get_physical_id_entry(vcpu, id);
  3128. if (!new || !old)
  3129. return 1;
  3130. /* We need to move physical_id_entry to new offset */
  3131. *new = *old;
  3132. *old = 0ULL;
  3133. to_svm(vcpu)->avic_physical_id_cache = new;
  3134. /*
  3135. * Also update the guest physical APIC ID in the logical
  3136. * APIC ID table entry if already setup the LDR.
  3137. */
  3138. if (svm->ldr_reg)
  3139. avic_handle_ldr_update(vcpu);
  3140. return 0;
  3141. }
  3142. static int avic_handle_dfr_update(struct kvm_vcpu *vcpu)
  3143. {
  3144. struct vcpu_svm *svm = to_svm(vcpu);
  3145. struct kvm_arch *vm_data = &vcpu->kvm->arch;
  3146. u32 dfr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_DFR);
  3147. u32 mod = (dfr >> 28) & 0xf;
  3148. /*
  3149. * We assume that all local APICs are using the same type.
  3150. * If this changes, we need to flush the AVIC logical
  3151. * APID id table.
  3152. */
  3153. if (vm_data->ldr_mode == mod)
  3154. return 0;
  3155. clear_page(page_address(vm_data->avic_logical_id_table_page));
  3156. vm_data->ldr_mode = mod;
  3157. if (svm->ldr_reg)
  3158. avic_handle_ldr_update(vcpu);
  3159. return 0;
  3160. }
  3161. static int avic_unaccel_trap_write(struct vcpu_svm *svm)
  3162. {
  3163. struct kvm_lapic *apic = svm->vcpu.arch.apic;
  3164. u32 offset = svm->vmcb->control.exit_info_1 &
  3165. AVIC_UNACCEL_ACCESS_OFFSET_MASK;
  3166. switch (offset) {
  3167. case APIC_ID:
  3168. if (avic_handle_apic_id_update(&svm->vcpu))
  3169. return 0;
  3170. break;
  3171. case APIC_LDR:
  3172. if (avic_handle_ldr_update(&svm->vcpu))
  3173. return 0;
  3174. break;
  3175. case APIC_DFR:
  3176. avic_handle_dfr_update(&svm->vcpu);
  3177. break;
  3178. default:
  3179. break;
  3180. }
  3181. kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset));
  3182. return 1;
  3183. }
  3184. static bool is_avic_unaccelerated_access_trap(u32 offset)
  3185. {
  3186. bool ret = false;
  3187. switch (offset) {
  3188. case APIC_ID:
  3189. case APIC_EOI:
  3190. case APIC_RRR:
  3191. case APIC_LDR:
  3192. case APIC_DFR:
  3193. case APIC_SPIV:
  3194. case APIC_ESR:
  3195. case APIC_ICR:
  3196. case APIC_LVTT:
  3197. case APIC_LVTTHMR:
  3198. case APIC_LVTPC:
  3199. case APIC_LVT0:
  3200. case APIC_LVT1:
  3201. case APIC_LVTERR:
  3202. case APIC_TMICT:
  3203. case APIC_TDCR:
  3204. ret = true;
  3205. break;
  3206. default:
  3207. break;
  3208. }
  3209. return ret;
  3210. }
  3211. static int avic_unaccelerated_access_interception(struct vcpu_svm *svm)
  3212. {
  3213. int ret = 0;
  3214. u32 offset = svm->vmcb->control.exit_info_1 &
  3215. AVIC_UNACCEL_ACCESS_OFFSET_MASK;
  3216. u32 vector = svm->vmcb->control.exit_info_2 &
  3217. AVIC_UNACCEL_ACCESS_VECTOR_MASK;
  3218. bool write = (svm->vmcb->control.exit_info_1 >> 32) &
  3219. AVIC_UNACCEL_ACCESS_WRITE_MASK;
  3220. bool trap = is_avic_unaccelerated_access_trap(offset);
  3221. trace_kvm_avic_unaccelerated_access(svm->vcpu.vcpu_id, offset,
  3222. trap, write, vector);
  3223. if (trap) {
  3224. /* Handling Trap */
  3225. WARN_ONCE(!write, "svm: Handling trap read.\n");
  3226. ret = avic_unaccel_trap_write(svm);
  3227. } else {
  3228. /* Handling Fault */
  3229. ret = (emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE);
  3230. }
  3231. return ret;
  3232. }
  3233. static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
  3234. [SVM_EXIT_READ_CR0] = cr_interception,
  3235. [SVM_EXIT_READ_CR3] = cr_interception,
  3236. [SVM_EXIT_READ_CR4] = cr_interception,
  3237. [SVM_EXIT_READ_CR8] = cr_interception,
  3238. [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
  3239. [SVM_EXIT_WRITE_CR0] = cr_interception,
  3240. [SVM_EXIT_WRITE_CR3] = cr_interception,
  3241. [SVM_EXIT_WRITE_CR4] = cr_interception,
  3242. [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
  3243. [SVM_EXIT_READ_DR0] = dr_interception,
  3244. [SVM_EXIT_READ_DR1] = dr_interception,
  3245. [SVM_EXIT_READ_DR2] = dr_interception,
  3246. [SVM_EXIT_READ_DR3] = dr_interception,
  3247. [SVM_EXIT_READ_DR4] = dr_interception,
  3248. [SVM_EXIT_READ_DR5] = dr_interception,
  3249. [SVM_EXIT_READ_DR6] = dr_interception,
  3250. [SVM_EXIT_READ_DR7] = dr_interception,
  3251. [SVM_EXIT_WRITE_DR0] = dr_interception,
  3252. [SVM_EXIT_WRITE_DR1] = dr_interception,
  3253. [SVM_EXIT_WRITE_DR2] = dr_interception,
  3254. [SVM_EXIT_WRITE_DR3] = dr_interception,
  3255. [SVM_EXIT_WRITE_DR4] = dr_interception,
  3256. [SVM_EXIT_WRITE_DR5] = dr_interception,
  3257. [SVM_EXIT_WRITE_DR6] = dr_interception,
  3258. [SVM_EXIT_WRITE_DR7] = dr_interception,
  3259. [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
  3260. [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
  3261. [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
  3262. [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
  3263. [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
  3264. [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
  3265. [SVM_EXIT_INTR] = intr_interception,
  3266. [SVM_EXIT_NMI] = nmi_interception,
  3267. [SVM_EXIT_SMI] = nop_on_interception,
  3268. [SVM_EXIT_INIT] = nop_on_interception,
  3269. [SVM_EXIT_VINTR] = interrupt_window_interception,
  3270. [SVM_EXIT_RDPMC] = rdpmc_interception,
  3271. [SVM_EXIT_CPUID] = cpuid_interception,
  3272. [SVM_EXIT_IRET] = iret_interception,
  3273. [SVM_EXIT_INVD] = emulate_on_interception,
  3274. [SVM_EXIT_PAUSE] = pause_interception,
  3275. [SVM_EXIT_HLT] = halt_interception,
  3276. [SVM_EXIT_INVLPG] = invlpg_interception,
  3277. [SVM_EXIT_INVLPGA] = invlpga_interception,
  3278. [SVM_EXIT_IOIO] = io_interception,
  3279. [SVM_EXIT_MSR] = msr_interception,
  3280. [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
  3281. [SVM_EXIT_SHUTDOWN] = shutdown_interception,
  3282. [SVM_EXIT_VMRUN] = vmrun_interception,
  3283. [SVM_EXIT_VMMCALL] = vmmcall_interception,
  3284. [SVM_EXIT_VMLOAD] = vmload_interception,
  3285. [SVM_EXIT_VMSAVE] = vmsave_interception,
  3286. [SVM_EXIT_STGI] = stgi_interception,
  3287. [SVM_EXIT_CLGI] = clgi_interception,
  3288. [SVM_EXIT_SKINIT] = skinit_interception,
  3289. [SVM_EXIT_WBINVD] = wbinvd_interception,
  3290. [SVM_EXIT_MONITOR] = monitor_interception,
  3291. [SVM_EXIT_MWAIT] = mwait_interception,
  3292. [SVM_EXIT_XSETBV] = xsetbv_interception,
  3293. [SVM_EXIT_NPF] = pf_interception,
  3294. [SVM_EXIT_RSM] = emulate_on_interception,
  3295. [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
  3296. [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
  3297. };
  3298. static void dump_vmcb(struct kvm_vcpu *vcpu)
  3299. {
  3300. struct vcpu_svm *svm = to_svm(vcpu);
  3301. struct vmcb_control_area *control = &svm->vmcb->control;
  3302. struct vmcb_save_area *save = &svm->vmcb->save;
  3303. pr_err("VMCB Control Area:\n");
  3304. pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
  3305. pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
  3306. pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
  3307. pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
  3308. pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
  3309. pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
  3310. pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
  3311. pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
  3312. pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
  3313. pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
  3314. pr_err("%-20s%d\n", "asid:", control->asid);
  3315. pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
  3316. pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
  3317. pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
  3318. pr_err("%-20s%08x\n", "int_state:", control->int_state);
  3319. pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
  3320. pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
  3321. pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
  3322. pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
  3323. pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
  3324. pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
  3325. pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
  3326. pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
  3327. pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
  3328. pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
  3329. pr_err("%-20s%lld\n", "lbr_ctl:", control->lbr_ctl);
  3330. pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
  3331. pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
  3332. pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
  3333. pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
  3334. pr_err("VMCB State Save Area:\n");
  3335. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3336. "es:",
  3337. save->es.selector, save->es.attrib,
  3338. save->es.limit, save->es.base);
  3339. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3340. "cs:",
  3341. save->cs.selector, save->cs.attrib,
  3342. save->cs.limit, save->cs.base);
  3343. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3344. "ss:",
  3345. save->ss.selector, save->ss.attrib,
  3346. save->ss.limit, save->ss.base);
  3347. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3348. "ds:",
  3349. save->ds.selector, save->ds.attrib,
  3350. save->ds.limit, save->ds.base);
  3351. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3352. "fs:",
  3353. save->fs.selector, save->fs.attrib,
  3354. save->fs.limit, save->fs.base);
  3355. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3356. "gs:",
  3357. save->gs.selector, save->gs.attrib,
  3358. save->gs.limit, save->gs.base);
  3359. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3360. "gdtr:",
  3361. save->gdtr.selector, save->gdtr.attrib,
  3362. save->gdtr.limit, save->gdtr.base);
  3363. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3364. "ldtr:",
  3365. save->ldtr.selector, save->ldtr.attrib,
  3366. save->ldtr.limit, save->ldtr.base);
  3367. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3368. "idtr:",
  3369. save->idtr.selector, save->idtr.attrib,
  3370. save->idtr.limit, save->idtr.base);
  3371. pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
  3372. "tr:",
  3373. save->tr.selector, save->tr.attrib,
  3374. save->tr.limit, save->tr.base);
  3375. pr_err("cpl: %d efer: %016llx\n",
  3376. save->cpl, save->efer);
  3377. pr_err("%-15s %016llx %-13s %016llx\n",
  3378. "cr0:", save->cr0, "cr2:", save->cr2);
  3379. pr_err("%-15s %016llx %-13s %016llx\n",
  3380. "cr3:", save->cr3, "cr4:", save->cr4);
  3381. pr_err("%-15s %016llx %-13s %016llx\n",
  3382. "dr6:", save->dr6, "dr7:", save->dr7);
  3383. pr_err("%-15s %016llx %-13s %016llx\n",
  3384. "rip:", save->rip, "rflags:", save->rflags);
  3385. pr_err("%-15s %016llx %-13s %016llx\n",
  3386. "rsp:", save->rsp, "rax:", save->rax);
  3387. pr_err("%-15s %016llx %-13s %016llx\n",
  3388. "star:", save->star, "lstar:", save->lstar);
  3389. pr_err("%-15s %016llx %-13s %016llx\n",
  3390. "cstar:", save->cstar, "sfmask:", save->sfmask);
  3391. pr_err("%-15s %016llx %-13s %016llx\n",
  3392. "kernel_gs_base:", save->kernel_gs_base,
  3393. "sysenter_cs:", save->sysenter_cs);
  3394. pr_err("%-15s %016llx %-13s %016llx\n",
  3395. "sysenter_esp:", save->sysenter_esp,
  3396. "sysenter_eip:", save->sysenter_eip);
  3397. pr_err("%-15s %016llx %-13s %016llx\n",
  3398. "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
  3399. pr_err("%-15s %016llx %-13s %016llx\n",
  3400. "br_from:", save->br_from, "br_to:", save->br_to);
  3401. pr_err("%-15s %016llx %-13s %016llx\n",
  3402. "excp_from:", save->last_excp_from,
  3403. "excp_to:", save->last_excp_to);
  3404. }
  3405. static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
  3406. {
  3407. struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
  3408. *info1 = control->exit_info_1;
  3409. *info2 = control->exit_info_2;
  3410. }
  3411. static int handle_exit(struct kvm_vcpu *vcpu)
  3412. {
  3413. struct vcpu_svm *svm = to_svm(vcpu);
  3414. struct kvm_run *kvm_run = vcpu->run;
  3415. u32 exit_code = svm->vmcb->control.exit_code;
  3416. trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
  3417. vcpu->arch.gpa_available = (exit_code == SVM_EXIT_NPF);
  3418. if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
  3419. vcpu->arch.cr0 = svm->vmcb->save.cr0;
  3420. if (npt_enabled)
  3421. vcpu->arch.cr3 = svm->vmcb->save.cr3;
  3422. if (unlikely(svm->nested.exit_required)) {
  3423. nested_svm_vmexit(svm);
  3424. svm->nested.exit_required = false;
  3425. return 1;
  3426. }
  3427. if (is_guest_mode(vcpu)) {
  3428. int vmexit;
  3429. trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
  3430. svm->vmcb->control.exit_info_1,
  3431. svm->vmcb->control.exit_info_2,
  3432. svm->vmcb->control.exit_int_info,
  3433. svm->vmcb->control.exit_int_info_err,
  3434. KVM_ISA_SVM);
  3435. vmexit = nested_svm_exit_special(svm);
  3436. if (vmexit == NESTED_EXIT_CONTINUE)
  3437. vmexit = nested_svm_exit_handled(svm);
  3438. if (vmexit == NESTED_EXIT_DONE)
  3439. return 1;
  3440. }
  3441. svm_complete_interrupts(svm);
  3442. if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
  3443. kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  3444. kvm_run->fail_entry.hardware_entry_failure_reason
  3445. = svm->vmcb->control.exit_code;
  3446. pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
  3447. dump_vmcb(vcpu);
  3448. return 0;
  3449. }
  3450. if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
  3451. exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
  3452. exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
  3453. exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
  3454. printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
  3455. "exit_code 0x%x\n",
  3456. __func__, svm->vmcb->control.exit_int_info,
  3457. exit_code);
  3458. if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
  3459. || !svm_exit_handlers[exit_code]) {
  3460. WARN_ONCE(1, "svm: unexpected exit reason 0x%x\n", exit_code);
  3461. kvm_queue_exception(vcpu, UD_VECTOR);
  3462. return 1;
  3463. }
  3464. return svm_exit_handlers[exit_code](svm);
  3465. }
  3466. static void reload_tss(struct kvm_vcpu *vcpu)
  3467. {
  3468. int cpu = raw_smp_processor_id();
  3469. struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
  3470. sd->tss_desc->type = 9; /* available 32/64-bit TSS */
  3471. load_TR_desc();
  3472. }
  3473. static void pre_svm_run(struct vcpu_svm *svm)
  3474. {
  3475. int cpu = raw_smp_processor_id();
  3476. struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
  3477. /* FIXME: handle wraparound of asid_generation */
  3478. if (svm->asid_generation != sd->asid_generation)
  3479. new_asid(svm, sd);
  3480. }
  3481. static void svm_inject_nmi(struct kvm_vcpu *vcpu)
  3482. {
  3483. struct vcpu_svm *svm = to_svm(vcpu);
  3484. svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
  3485. vcpu->arch.hflags |= HF_NMI_MASK;
  3486. set_intercept(svm, INTERCEPT_IRET);
  3487. ++vcpu->stat.nmi_injections;
  3488. }
  3489. static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
  3490. {
  3491. struct vmcb_control_area *control;
  3492. /* The following fields are ignored when AVIC is enabled */
  3493. control = &svm->vmcb->control;
  3494. control->int_vector = irq;
  3495. control->int_ctl &= ~V_INTR_PRIO_MASK;
  3496. control->int_ctl |= V_IRQ_MASK |
  3497. ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
  3498. mark_dirty(svm->vmcb, VMCB_INTR);
  3499. }
  3500. static void svm_set_irq(struct kvm_vcpu *vcpu)
  3501. {
  3502. struct vcpu_svm *svm = to_svm(vcpu);
  3503. BUG_ON(!(gif_set(svm)));
  3504. trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
  3505. ++vcpu->stat.irq_injections;
  3506. svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
  3507. SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
  3508. }
  3509. static inline bool svm_nested_virtualize_tpr(struct kvm_vcpu *vcpu)
  3510. {
  3511. return is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK);
  3512. }
  3513. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  3514. {
  3515. struct vcpu_svm *svm = to_svm(vcpu);
  3516. if (svm_nested_virtualize_tpr(vcpu) ||
  3517. kvm_vcpu_apicv_active(vcpu))
  3518. return;
  3519. clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  3520. if (irr == -1)
  3521. return;
  3522. if (tpr >= irr)
  3523. set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
  3524. }
  3525. static void svm_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
  3526. {
  3527. return;
  3528. }
  3529. static bool svm_get_enable_apicv(void)
  3530. {
  3531. return avic;
  3532. }
  3533. static void svm_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
  3534. {
  3535. }
  3536. static void svm_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
  3537. {
  3538. }
  3539. /* Note: Currently only used by Hyper-V. */
  3540. static void svm_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
  3541. {
  3542. struct vcpu_svm *svm = to_svm(vcpu);
  3543. struct vmcb *vmcb = svm->vmcb;
  3544. if (!avic)
  3545. return;
  3546. vmcb->control.int_ctl &= ~AVIC_ENABLE_MASK;
  3547. mark_dirty(vmcb, VMCB_INTR);
  3548. }
  3549. static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
  3550. {
  3551. return;
  3552. }
  3553. static void svm_deliver_avic_intr(struct kvm_vcpu *vcpu, int vec)
  3554. {
  3555. kvm_lapic_set_irr(vec, vcpu->arch.apic);
  3556. smp_mb__after_atomic();
  3557. if (avic_vcpu_is_running(vcpu))
  3558. wrmsrl(SVM_AVIC_DOORBELL,
  3559. kvm_cpu_get_apicid(vcpu->cpu));
  3560. else
  3561. kvm_vcpu_wake_up(vcpu);
  3562. }
  3563. static void svm_ir_list_del(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
  3564. {
  3565. unsigned long flags;
  3566. struct amd_svm_iommu_ir *cur;
  3567. spin_lock_irqsave(&svm->ir_list_lock, flags);
  3568. list_for_each_entry(cur, &svm->ir_list, node) {
  3569. if (cur->data != pi->ir_data)
  3570. continue;
  3571. list_del(&cur->node);
  3572. kfree(cur);
  3573. break;
  3574. }
  3575. spin_unlock_irqrestore(&svm->ir_list_lock, flags);
  3576. }
  3577. static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi)
  3578. {
  3579. int ret = 0;
  3580. unsigned long flags;
  3581. struct amd_svm_iommu_ir *ir;
  3582. /**
  3583. * In some cases, the existing irte is updaed and re-set,
  3584. * so we need to check here if it's already been * added
  3585. * to the ir_list.
  3586. */
  3587. if (pi->ir_data && (pi->prev_ga_tag != 0)) {
  3588. struct kvm *kvm = svm->vcpu.kvm;
  3589. u32 vcpu_id = AVIC_GATAG_TO_VCPUID(pi->prev_ga_tag);
  3590. struct kvm_vcpu *prev_vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id);
  3591. struct vcpu_svm *prev_svm;
  3592. if (!prev_vcpu) {
  3593. ret = -EINVAL;
  3594. goto out;
  3595. }
  3596. prev_svm = to_svm(prev_vcpu);
  3597. svm_ir_list_del(prev_svm, pi);
  3598. }
  3599. /**
  3600. * Allocating new amd_iommu_pi_data, which will get
  3601. * add to the per-vcpu ir_list.
  3602. */
  3603. ir = kzalloc(sizeof(struct amd_svm_iommu_ir), GFP_KERNEL);
  3604. if (!ir) {
  3605. ret = -ENOMEM;
  3606. goto out;
  3607. }
  3608. ir->data = pi->ir_data;
  3609. spin_lock_irqsave(&svm->ir_list_lock, flags);
  3610. list_add(&ir->node, &svm->ir_list);
  3611. spin_unlock_irqrestore(&svm->ir_list_lock, flags);
  3612. out:
  3613. return ret;
  3614. }
  3615. /**
  3616. * Note:
  3617. * The HW cannot support posting multicast/broadcast
  3618. * interrupts to a vCPU. So, we still use legacy interrupt
  3619. * remapping for these kind of interrupts.
  3620. *
  3621. * For lowest-priority interrupts, we only support
  3622. * those with single CPU as the destination, e.g. user
  3623. * configures the interrupts via /proc/irq or uses
  3624. * irqbalance to make the interrupts single-CPU.
  3625. */
  3626. static int
  3627. get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
  3628. struct vcpu_data *vcpu_info, struct vcpu_svm **svm)
  3629. {
  3630. struct kvm_lapic_irq irq;
  3631. struct kvm_vcpu *vcpu = NULL;
  3632. kvm_set_msi_irq(kvm, e, &irq);
  3633. if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
  3634. pr_debug("SVM: %s: use legacy intr remap mode for irq %u\n",
  3635. __func__, irq.vector);
  3636. return -1;
  3637. }
  3638. pr_debug("SVM: %s: use GA mode for irq %u\n", __func__,
  3639. irq.vector);
  3640. *svm = to_svm(vcpu);
  3641. vcpu_info->pi_desc_addr = page_to_phys((*svm)->avic_backing_page);
  3642. vcpu_info->vector = irq.vector;
  3643. return 0;
  3644. }
  3645. /*
  3646. * svm_update_pi_irte - set IRTE for Posted-Interrupts
  3647. *
  3648. * @kvm: kvm
  3649. * @host_irq: host irq of the interrupt
  3650. * @guest_irq: gsi of the interrupt
  3651. * @set: set or unset PI
  3652. * returns 0 on success, < 0 on failure
  3653. */
  3654. static int svm_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
  3655. uint32_t guest_irq, bool set)
  3656. {
  3657. struct kvm_kernel_irq_routing_entry *e;
  3658. struct kvm_irq_routing_table *irq_rt;
  3659. int idx, ret = -EINVAL;
  3660. if (!kvm_arch_has_assigned_device(kvm) ||
  3661. !irq_remapping_cap(IRQ_POSTING_CAP))
  3662. return 0;
  3663. pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#x\n",
  3664. __func__, host_irq, guest_irq, set);
  3665. idx = srcu_read_lock(&kvm->irq_srcu);
  3666. irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
  3667. WARN_ON(guest_irq >= irq_rt->nr_rt_entries);
  3668. hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
  3669. struct vcpu_data vcpu_info;
  3670. struct vcpu_svm *svm = NULL;
  3671. if (e->type != KVM_IRQ_ROUTING_MSI)
  3672. continue;
  3673. /**
  3674. * Here, we setup with legacy mode in the following cases:
  3675. * 1. When cannot target interrupt to a specific vcpu.
  3676. * 2. Unsetting posted interrupt.
  3677. * 3. APIC virtialization is disabled for the vcpu.
  3678. */
  3679. if (!get_pi_vcpu_info(kvm, e, &vcpu_info, &svm) && set &&
  3680. kvm_vcpu_apicv_active(&svm->vcpu)) {
  3681. struct amd_iommu_pi_data pi;
  3682. /* Try to enable guest_mode in IRTE */
  3683. pi.base = page_to_phys(svm->avic_backing_page) & AVIC_HPA_MASK;
  3684. pi.ga_tag = AVIC_GATAG(kvm->arch.avic_vm_id,
  3685. svm->vcpu.vcpu_id);
  3686. pi.is_guest_mode = true;
  3687. pi.vcpu_data = &vcpu_info;
  3688. ret = irq_set_vcpu_affinity(host_irq, &pi);
  3689. /**
  3690. * Here, we successfully setting up vcpu affinity in
  3691. * IOMMU guest mode. Now, we need to store the posted
  3692. * interrupt information in a per-vcpu ir_list so that
  3693. * we can reference to them directly when we update vcpu
  3694. * scheduling information in IOMMU irte.
  3695. */
  3696. if (!ret && pi.is_guest_mode)
  3697. svm_ir_list_add(svm, &pi);
  3698. } else {
  3699. /* Use legacy mode in IRTE */
  3700. struct amd_iommu_pi_data pi;
  3701. /**
  3702. * Here, pi is used to:
  3703. * - Tell IOMMU to use legacy mode for this interrupt.
  3704. * - Retrieve ga_tag of prior interrupt remapping data.
  3705. */
  3706. pi.is_guest_mode = false;
  3707. ret = irq_set_vcpu_affinity(host_irq, &pi);
  3708. /**
  3709. * Check if the posted interrupt was previously
  3710. * setup with the guest_mode by checking if the ga_tag
  3711. * was cached. If so, we need to clean up the per-vcpu
  3712. * ir_list.
  3713. */
  3714. if (!ret && pi.prev_ga_tag) {
  3715. int id = AVIC_GATAG_TO_VCPUID(pi.prev_ga_tag);
  3716. struct kvm_vcpu *vcpu;
  3717. vcpu = kvm_get_vcpu_by_id(kvm, id);
  3718. if (vcpu)
  3719. svm_ir_list_del(to_svm(vcpu), &pi);
  3720. }
  3721. }
  3722. if (!ret && svm) {
  3723. trace_kvm_pi_irte_update(svm->vcpu.vcpu_id,
  3724. host_irq, e->gsi,
  3725. vcpu_info.vector,
  3726. vcpu_info.pi_desc_addr, set);
  3727. }
  3728. if (ret < 0) {
  3729. pr_err("%s: failed to update PI IRTE\n", __func__);
  3730. goto out;
  3731. }
  3732. }
  3733. ret = 0;
  3734. out:
  3735. srcu_read_unlock(&kvm->irq_srcu, idx);
  3736. return ret;
  3737. }
  3738. static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
  3739. {
  3740. struct vcpu_svm *svm = to_svm(vcpu);
  3741. struct vmcb *vmcb = svm->vmcb;
  3742. int ret;
  3743. ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
  3744. !(svm->vcpu.arch.hflags & HF_NMI_MASK);
  3745. ret = ret && gif_set(svm) && nested_svm_nmi(svm);
  3746. return ret;
  3747. }
  3748. static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
  3749. {
  3750. struct vcpu_svm *svm = to_svm(vcpu);
  3751. return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
  3752. }
  3753. static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  3754. {
  3755. struct vcpu_svm *svm = to_svm(vcpu);
  3756. if (masked) {
  3757. svm->vcpu.arch.hflags |= HF_NMI_MASK;
  3758. set_intercept(svm, INTERCEPT_IRET);
  3759. } else {
  3760. svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
  3761. clr_intercept(svm, INTERCEPT_IRET);
  3762. }
  3763. }
  3764. static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
  3765. {
  3766. struct vcpu_svm *svm = to_svm(vcpu);
  3767. struct vmcb *vmcb = svm->vmcb;
  3768. int ret;
  3769. if (!gif_set(svm) ||
  3770. (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
  3771. return 0;
  3772. ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
  3773. if (is_guest_mode(vcpu))
  3774. return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
  3775. return ret;
  3776. }
  3777. static void enable_irq_window(struct kvm_vcpu *vcpu)
  3778. {
  3779. struct vcpu_svm *svm = to_svm(vcpu);
  3780. if (kvm_vcpu_apicv_active(vcpu))
  3781. return;
  3782. /*
  3783. * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
  3784. * 1, because that's a separate STGI/VMRUN intercept. The next time we
  3785. * get that intercept, this function will be called again though and
  3786. * we'll get the vintr intercept.
  3787. */
  3788. if (gif_set(svm) && nested_svm_intr(svm)) {
  3789. svm_set_vintr(svm);
  3790. svm_inject_irq(svm, 0x0);
  3791. }
  3792. }
  3793. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  3794. {
  3795. struct vcpu_svm *svm = to_svm(vcpu);
  3796. if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
  3797. == HF_NMI_MASK)
  3798. return; /* IRET will cause a vm exit */
  3799. /*
  3800. * Something prevents NMI from been injected. Single step over possible
  3801. * problem (IRET or exception injection or interrupt shadow)
  3802. */
  3803. svm->nmi_singlestep = true;
  3804. svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
  3805. }
  3806. static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
  3807. {
  3808. return 0;
  3809. }
  3810. static void svm_flush_tlb(struct kvm_vcpu *vcpu)
  3811. {
  3812. struct vcpu_svm *svm = to_svm(vcpu);
  3813. if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
  3814. svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
  3815. else
  3816. svm->asid_generation--;
  3817. }
  3818. static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
  3819. {
  3820. }
  3821. static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
  3822. {
  3823. struct vcpu_svm *svm = to_svm(vcpu);
  3824. if (svm_nested_virtualize_tpr(vcpu))
  3825. return;
  3826. if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
  3827. int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
  3828. kvm_set_cr8(vcpu, cr8);
  3829. }
  3830. }
  3831. static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
  3832. {
  3833. struct vcpu_svm *svm = to_svm(vcpu);
  3834. u64 cr8;
  3835. if (svm_nested_virtualize_tpr(vcpu) ||
  3836. kvm_vcpu_apicv_active(vcpu))
  3837. return;
  3838. cr8 = kvm_get_cr8(vcpu);
  3839. svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
  3840. svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
  3841. }
  3842. static void svm_complete_interrupts(struct vcpu_svm *svm)
  3843. {
  3844. u8 vector;
  3845. int type;
  3846. u32 exitintinfo = svm->vmcb->control.exit_int_info;
  3847. unsigned int3_injected = svm->int3_injected;
  3848. svm->int3_injected = 0;
  3849. /*
  3850. * If we've made progress since setting HF_IRET_MASK, we've
  3851. * executed an IRET and can allow NMI injection.
  3852. */
  3853. if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
  3854. && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
  3855. svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
  3856. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  3857. }
  3858. svm->vcpu.arch.nmi_injected = false;
  3859. kvm_clear_exception_queue(&svm->vcpu);
  3860. kvm_clear_interrupt_queue(&svm->vcpu);
  3861. if (!(exitintinfo & SVM_EXITINTINFO_VALID))
  3862. return;
  3863. kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  3864. vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
  3865. type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
  3866. switch (type) {
  3867. case SVM_EXITINTINFO_TYPE_NMI:
  3868. svm->vcpu.arch.nmi_injected = true;
  3869. break;
  3870. case SVM_EXITINTINFO_TYPE_EXEPT:
  3871. /*
  3872. * In case of software exceptions, do not reinject the vector,
  3873. * but re-execute the instruction instead. Rewind RIP first
  3874. * if we emulated INT3 before.
  3875. */
  3876. if (kvm_exception_is_soft(vector)) {
  3877. if (vector == BP_VECTOR && int3_injected &&
  3878. kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
  3879. kvm_rip_write(&svm->vcpu,
  3880. kvm_rip_read(&svm->vcpu) -
  3881. int3_injected);
  3882. break;
  3883. }
  3884. if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
  3885. u32 err = svm->vmcb->control.exit_int_info_err;
  3886. kvm_requeue_exception_e(&svm->vcpu, vector, err);
  3887. } else
  3888. kvm_requeue_exception(&svm->vcpu, vector);
  3889. break;
  3890. case SVM_EXITINTINFO_TYPE_INTR:
  3891. kvm_queue_interrupt(&svm->vcpu, vector, false);
  3892. break;
  3893. default:
  3894. break;
  3895. }
  3896. }
  3897. static void svm_cancel_injection(struct kvm_vcpu *vcpu)
  3898. {
  3899. struct vcpu_svm *svm = to_svm(vcpu);
  3900. struct vmcb_control_area *control = &svm->vmcb->control;
  3901. control->exit_int_info = control->event_inj;
  3902. control->exit_int_info_err = control->event_inj_err;
  3903. control->event_inj = 0;
  3904. svm_complete_interrupts(svm);
  3905. }
  3906. static void svm_vcpu_run(struct kvm_vcpu *vcpu)
  3907. {
  3908. struct vcpu_svm *svm = to_svm(vcpu);
  3909. svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
  3910. svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
  3911. svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
  3912. /*
  3913. * A vmexit emulation is required before the vcpu can be executed
  3914. * again.
  3915. */
  3916. if (unlikely(svm->nested.exit_required))
  3917. return;
  3918. pre_svm_run(svm);
  3919. sync_lapic_to_cr8(vcpu);
  3920. svm->vmcb->save.cr2 = vcpu->arch.cr2;
  3921. clgi();
  3922. local_irq_enable();
  3923. asm volatile (
  3924. "push %%" _ASM_BP "; \n\t"
  3925. "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t"
  3926. "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t"
  3927. "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t"
  3928. "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t"
  3929. "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t"
  3930. "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t"
  3931. #ifdef CONFIG_X86_64
  3932. "mov %c[r8](%[svm]), %%r8 \n\t"
  3933. "mov %c[r9](%[svm]), %%r9 \n\t"
  3934. "mov %c[r10](%[svm]), %%r10 \n\t"
  3935. "mov %c[r11](%[svm]), %%r11 \n\t"
  3936. "mov %c[r12](%[svm]), %%r12 \n\t"
  3937. "mov %c[r13](%[svm]), %%r13 \n\t"
  3938. "mov %c[r14](%[svm]), %%r14 \n\t"
  3939. "mov %c[r15](%[svm]), %%r15 \n\t"
  3940. #endif
  3941. /* Enter guest mode */
  3942. "push %%" _ASM_AX " \n\t"
  3943. "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t"
  3944. __ex(SVM_VMLOAD) "\n\t"
  3945. __ex(SVM_VMRUN) "\n\t"
  3946. __ex(SVM_VMSAVE) "\n\t"
  3947. "pop %%" _ASM_AX " \n\t"
  3948. /* Save guest registers, load host registers */
  3949. "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t"
  3950. "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t"
  3951. "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t"
  3952. "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t"
  3953. "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t"
  3954. "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t"
  3955. #ifdef CONFIG_X86_64
  3956. "mov %%r8, %c[r8](%[svm]) \n\t"
  3957. "mov %%r9, %c[r9](%[svm]) \n\t"
  3958. "mov %%r10, %c[r10](%[svm]) \n\t"
  3959. "mov %%r11, %c[r11](%[svm]) \n\t"
  3960. "mov %%r12, %c[r12](%[svm]) \n\t"
  3961. "mov %%r13, %c[r13](%[svm]) \n\t"
  3962. "mov %%r14, %c[r14](%[svm]) \n\t"
  3963. "mov %%r15, %c[r15](%[svm]) \n\t"
  3964. #endif
  3965. "pop %%" _ASM_BP
  3966. :
  3967. : [svm]"a"(svm),
  3968. [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
  3969. [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
  3970. [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
  3971. [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
  3972. [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
  3973. [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
  3974. [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
  3975. #ifdef CONFIG_X86_64
  3976. , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
  3977. [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
  3978. [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
  3979. [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
  3980. [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
  3981. [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
  3982. [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
  3983. [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
  3984. #endif
  3985. : "cc", "memory"
  3986. #ifdef CONFIG_X86_64
  3987. , "rbx", "rcx", "rdx", "rsi", "rdi"
  3988. , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
  3989. #else
  3990. , "ebx", "ecx", "edx", "esi", "edi"
  3991. #endif
  3992. );
  3993. #ifdef CONFIG_X86_64
  3994. wrmsrl(MSR_GS_BASE, svm->host.gs_base);
  3995. #else
  3996. loadsegment(fs, svm->host.fs);
  3997. #ifndef CONFIG_X86_32_LAZY_GS
  3998. loadsegment(gs, svm->host.gs);
  3999. #endif
  4000. #endif
  4001. reload_tss(vcpu);
  4002. local_irq_disable();
  4003. vcpu->arch.cr2 = svm->vmcb->save.cr2;
  4004. vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
  4005. vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
  4006. vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
  4007. if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
  4008. kvm_before_handle_nmi(&svm->vcpu);
  4009. stgi();
  4010. /* Any pending NMI will happen here */
  4011. if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
  4012. kvm_after_handle_nmi(&svm->vcpu);
  4013. sync_cr8_to_lapic(vcpu);
  4014. svm->next_rip = 0;
  4015. svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
  4016. /* if exit due to PF check for async PF */
  4017. if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
  4018. svm->apf_reason = kvm_read_and_reset_pf_reason();
  4019. if (npt_enabled) {
  4020. vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
  4021. vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
  4022. }
  4023. /*
  4024. * We need to handle MC intercepts here before the vcpu has a chance to
  4025. * change the physical cpu
  4026. */
  4027. if (unlikely(svm->vmcb->control.exit_code ==
  4028. SVM_EXIT_EXCP_BASE + MC_VECTOR))
  4029. svm_handle_mce(svm);
  4030. mark_all_clean(svm->vmcb);
  4031. }
  4032. static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  4033. {
  4034. struct vcpu_svm *svm = to_svm(vcpu);
  4035. svm->vmcb->save.cr3 = root;
  4036. mark_dirty(svm->vmcb, VMCB_CR);
  4037. svm_flush_tlb(vcpu);
  4038. }
  4039. static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  4040. {
  4041. struct vcpu_svm *svm = to_svm(vcpu);
  4042. svm->vmcb->control.nested_cr3 = root;
  4043. mark_dirty(svm->vmcb, VMCB_NPT);
  4044. /* Also sync guest cr3 here in case we live migrate */
  4045. svm->vmcb->save.cr3 = kvm_read_cr3(vcpu);
  4046. mark_dirty(svm->vmcb, VMCB_CR);
  4047. svm_flush_tlb(vcpu);
  4048. }
  4049. static int is_disabled(void)
  4050. {
  4051. u64 vm_cr;
  4052. rdmsrl(MSR_VM_CR, vm_cr);
  4053. if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
  4054. return 1;
  4055. return 0;
  4056. }
  4057. static void
  4058. svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  4059. {
  4060. /*
  4061. * Patch in the VMMCALL instruction:
  4062. */
  4063. hypercall[0] = 0x0f;
  4064. hypercall[1] = 0x01;
  4065. hypercall[2] = 0xd9;
  4066. }
  4067. static void svm_check_processor_compat(void *rtn)
  4068. {
  4069. *(int *)rtn = 0;
  4070. }
  4071. static bool svm_cpu_has_accelerated_tpr(void)
  4072. {
  4073. return false;
  4074. }
  4075. static bool svm_has_high_real_mode_segbase(void)
  4076. {
  4077. return true;
  4078. }
  4079. static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  4080. {
  4081. return 0;
  4082. }
  4083. static void svm_cpuid_update(struct kvm_vcpu *vcpu)
  4084. {
  4085. struct vcpu_svm *svm = to_svm(vcpu);
  4086. struct kvm_cpuid_entry2 *entry;
  4087. /* Update nrips enabled cache */
  4088. svm->nrips_enabled = !!guest_cpuid_has_nrips(&svm->vcpu);
  4089. if (!kvm_vcpu_apicv_active(vcpu))
  4090. return;
  4091. entry = kvm_find_cpuid_entry(vcpu, 1, 0);
  4092. if (entry)
  4093. entry->ecx &= ~bit(X86_FEATURE_X2APIC);
  4094. }
  4095. static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
  4096. {
  4097. switch (func) {
  4098. case 0x1:
  4099. if (avic)
  4100. entry->ecx &= ~bit(X86_FEATURE_X2APIC);
  4101. break;
  4102. case 0x80000001:
  4103. if (nested)
  4104. entry->ecx |= (1 << 2); /* Set SVM bit */
  4105. break;
  4106. case 0x8000000A:
  4107. entry->eax = 1; /* SVM revision 1 */
  4108. entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
  4109. ASID emulation to nested SVM */
  4110. entry->ecx = 0; /* Reserved */
  4111. entry->edx = 0; /* Per default do not support any
  4112. additional features */
  4113. /* Support next_rip if host supports it */
  4114. if (boot_cpu_has(X86_FEATURE_NRIPS))
  4115. entry->edx |= SVM_FEATURE_NRIP;
  4116. /* Support NPT for the guest if enabled */
  4117. if (npt_enabled)
  4118. entry->edx |= SVM_FEATURE_NPT;
  4119. break;
  4120. }
  4121. }
  4122. static int svm_get_lpage_level(void)
  4123. {
  4124. return PT_PDPE_LEVEL;
  4125. }
  4126. static bool svm_rdtscp_supported(void)
  4127. {
  4128. return boot_cpu_has(X86_FEATURE_RDTSCP);
  4129. }
  4130. static bool svm_invpcid_supported(void)
  4131. {
  4132. return false;
  4133. }
  4134. static bool svm_mpx_supported(void)
  4135. {
  4136. return false;
  4137. }
  4138. static bool svm_xsaves_supported(void)
  4139. {
  4140. return false;
  4141. }
  4142. static bool svm_has_wbinvd_exit(void)
  4143. {
  4144. return true;
  4145. }
  4146. #define PRE_EX(exit) { .exit_code = (exit), \
  4147. .stage = X86_ICPT_PRE_EXCEPT, }
  4148. #define POST_EX(exit) { .exit_code = (exit), \
  4149. .stage = X86_ICPT_POST_EXCEPT, }
  4150. #define POST_MEM(exit) { .exit_code = (exit), \
  4151. .stage = X86_ICPT_POST_MEMACCESS, }
  4152. static const struct __x86_intercept {
  4153. u32 exit_code;
  4154. enum x86_intercept_stage stage;
  4155. } x86_intercept_map[] = {
  4156. [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
  4157. [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
  4158. [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
  4159. [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
  4160. [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
  4161. [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
  4162. [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
  4163. [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
  4164. [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
  4165. [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
  4166. [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
  4167. [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
  4168. [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
  4169. [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
  4170. [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
  4171. [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
  4172. [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
  4173. [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
  4174. [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
  4175. [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
  4176. [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
  4177. [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
  4178. [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
  4179. [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
  4180. [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
  4181. [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
  4182. [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
  4183. [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
  4184. [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
  4185. [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
  4186. [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
  4187. [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
  4188. [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
  4189. [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
  4190. [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
  4191. [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
  4192. [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
  4193. [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
  4194. [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
  4195. [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
  4196. [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
  4197. [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
  4198. [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
  4199. [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
  4200. [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
  4201. [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
  4202. };
  4203. #undef PRE_EX
  4204. #undef POST_EX
  4205. #undef POST_MEM
  4206. static int svm_check_intercept(struct kvm_vcpu *vcpu,
  4207. struct x86_instruction_info *info,
  4208. enum x86_intercept_stage stage)
  4209. {
  4210. struct vcpu_svm *svm = to_svm(vcpu);
  4211. int vmexit, ret = X86EMUL_CONTINUE;
  4212. struct __x86_intercept icpt_info;
  4213. struct vmcb *vmcb = svm->vmcb;
  4214. if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
  4215. goto out;
  4216. icpt_info = x86_intercept_map[info->intercept];
  4217. if (stage != icpt_info.stage)
  4218. goto out;
  4219. switch (icpt_info.exit_code) {
  4220. case SVM_EXIT_READ_CR0:
  4221. if (info->intercept == x86_intercept_cr_read)
  4222. icpt_info.exit_code += info->modrm_reg;
  4223. break;
  4224. case SVM_EXIT_WRITE_CR0: {
  4225. unsigned long cr0, val;
  4226. u64 intercept;
  4227. if (info->intercept == x86_intercept_cr_write)
  4228. icpt_info.exit_code += info->modrm_reg;
  4229. if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
  4230. info->intercept == x86_intercept_clts)
  4231. break;
  4232. intercept = svm->nested.intercept;
  4233. if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
  4234. break;
  4235. cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
  4236. val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
  4237. if (info->intercept == x86_intercept_lmsw) {
  4238. cr0 &= 0xfUL;
  4239. val &= 0xfUL;
  4240. /* lmsw can't clear PE - catch this here */
  4241. if (cr0 & X86_CR0_PE)
  4242. val |= X86_CR0_PE;
  4243. }
  4244. if (cr0 ^ val)
  4245. icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
  4246. break;
  4247. }
  4248. case SVM_EXIT_READ_DR0:
  4249. case SVM_EXIT_WRITE_DR0:
  4250. icpt_info.exit_code += info->modrm_reg;
  4251. break;
  4252. case SVM_EXIT_MSR:
  4253. if (info->intercept == x86_intercept_wrmsr)
  4254. vmcb->control.exit_info_1 = 1;
  4255. else
  4256. vmcb->control.exit_info_1 = 0;
  4257. break;
  4258. case SVM_EXIT_PAUSE:
  4259. /*
  4260. * We get this for NOP only, but pause
  4261. * is rep not, check this here
  4262. */
  4263. if (info->rep_prefix != REPE_PREFIX)
  4264. goto out;
  4265. case SVM_EXIT_IOIO: {
  4266. u64 exit_info;
  4267. u32 bytes;
  4268. if (info->intercept == x86_intercept_in ||
  4269. info->intercept == x86_intercept_ins) {
  4270. exit_info = ((info->src_val & 0xffff) << 16) |
  4271. SVM_IOIO_TYPE_MASK;
  4272. bytes = info->dst_bytes;
  4273. } else {
  4274. exit_info = (info->dst_val & 0xffff) << 16;
  4275. bytes = info->src_bytes;
  4276. }
  4277. if (info->intercept == x86_intercept_outs ||
  4278. info->intercept == x86_intercept_ins)
  4279. exit_info |= SVM_IOIO_STR_MASK;
  4280. if (info->rep_prefix)
  4281. exit_info |= SVM_IOIO_REP_MASK;
  4282. bytes = min(bytes, 4u);
  4283. exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
  4284. exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
  4285. vmcb->control.exit_info_1 = exit_info;
  4286. vmcb->control.exit_info_2 = info->next_rip;
  4287. break;
  4288. }
  4289. default:
  4290. break;
  4291. }
  4292. /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
  4293. if (static_cpu_has(X86_FEATURE_NRIPS))
  4294. vmcb->control.next_rip = info->next_rip;
  4295. vmcb->control.exit_code = icpt_info.exit_code;
  4296. vmexit = nested_svm_exit_handled(svm);
  4297. ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
  4298. : X86EMUL_CONTINUE;
  4299. out:
  4300. return ret;
  4301. }
  4302. static void svm_handle_external_intr(struct kvm_vcpu *vcpu)
  4303. {
  4304. local_irq_enable();
  4305. /*
  4306. * We must have an instruction with interrupts enabled, so
  4307. * the timer interrupt isn't delayed by the interrupt shadow.
  4308. */
  4309. asm("nop");
  4310. local_irq_disable();
  4311. }
  4312. static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
  4313. {
  4314. }
  4315. static inline void avic_post_state_restore(struct kvm_vcpu *vcpu)
  4316. {
  4317. if (avic_handle_apic_id_update(vcpu) != 0)
  4318. return;
  4319. if (avic_handle_dfr_update(vcpu) != 0)
  4320. return;
  4321. avic_handle_ldr_update(vcpu);
  4322. }
  4323. static struct kvm_x86_ops svm_x86_ops __ro_after_init = {
  4324. .cpu_has_kvm_support = has_svm,
  4325. .disabled_by_bios = is_disabled,
  4326. .hardware_setup = svm_hardware_setup,
  4327. .hardware_unsetup = svm_hardware_unsetup,
  4328. .check_processor_compatibility = svm_check_processor_compat,
  4329. .hardware_enable = svm_hardware_enable,
  4330. .hardware_disable = svm_hardware_disable,
  4331. .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
  4332. .cpu_has_high_real_mode_segbase = svm_has_high_real_mode_segbase,
  4333. .vcpu_create = svm_create_vcpu,
  4334. .vcpu_free = svm_free_vcpu,
  4335. .vcpu_reset = svm_vcpu_reset,
  4336. .vm_init = avic_vm_init,
  4337. .vm_destroy = avic_vm_destroy,
  4338. .prepare_guest_switch = svm_prepare_guest_switch,
  4339. .vcpu_load = svm_vcpu_load,
  4340. .vcpu_put = svm_vcpu_put,
  4341. .vcpu_blocking = svm_vcpu_blocking,
  4342. .vcpu_unblocking = svm_vcpu_unblocking,
  4343. .update_bp_intercept = update_bp_intercept,
  4344. .get_msr = svm_get_msr,
  4345. .set_msr = svm_set_msr,
  4346. .get_segment_base = svm_get_segment_base,
  4347. .get_segment = svm_get_segment,
  4348. .set_segment = svm_set_segment,
  4349. .get_cpl = svm_get_cpl,
  4350. .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
  4351. .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
  4352. .decache_cr3 = svm_decache_cr3,
  4353. .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
  4354. .set_cr0 = svm_set_cr0,
  4355. .set_cr3 = svm_set_cr3,
  4356. .set_cr4 = svm_set_cr4,
  4357. .set_efer = svm_set_efer,
  4358. .get_idt = svm_get_idt,
  4359. .set_idt = svm_set_idt,
  4360. .get_gdt = svm_get_gdt,
  4361. .set_gdt = svm_set_gdt,
  4362. .get_dr6 = svm_get_dr6,
  4363. .set_dr6 = svm_set_dr6,
  4364. .set_dr7 = svm_set_dr7,
  4365. .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
  4366. .cache_reg = svm_cache_reg,
  4367. .get_rflags = svm_get_rflags,
  4368. .set_rflags = svm_set_rflags,
  4369. .get_pkru = svm_get_pkru,
  4370. .tlb_flush = svm_flush_tlb,
  4371. .run = svm_vcpu_run,
  4372. .handle_exit = handle_exit,
  4373. .skip_emulated_instruction = skip_emulated_instruction,
  4374. .set_interrupt_shadow = svm_set_interrupt_shadow,
  4375. .get_interrupt_shadow = svm_get_interrupt_shadow,
  4376. .patch_hypercall = svm_patch_hypercall,
  4377. .set_irq = svm_set_irq,
  4378. .set_nmi = svm_inject_nmi,
  4379. .queue_exception = svm_queue_exception,
  4380. .cancel_injection = svm_cancel_injection,
  4381. .interrupt_allowed = svm_interrupt_allowed,
  4382. .nmi_allowed = svm_nmi_allowed,
  4383. .get_nmi_mask = svm_get_nmi_mask,
  4384. .set_nmi_mask = svm_set_nmi_mask,
  4385. .enable_nmi_window = enable_nmi_window,
  4386. .enable_irq_window = enable_irq_window,
  4387. .update_cr8_intercept = update_cr8_intercept,
  4388. .set_virtual_x2apic_mode = svm_set_virtual_x2apic_mode,
  4389. .get_enable_apicv = svm_get_enable_apicv,
  4390. .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
  4391. .load_eoi_exitmap = svm_load_eoi_exitmap,
  4392. .hwapic_irr_update = svm_hwapic_irr_update,
  4393. .hwapic_isr_update = svm_hwapic_isr_update,
  4394. .apicv_post_state_restore = avic_post_state_restore,
  4395. .set_tss_addr = svm_set_tss_addr,
  4396. .get_tdp_level = get_npt_level,
  4397. .get_mt_mask = svm_get_mt_mask,
  4398. .get_exit_info = svm_get_exit_info,
  4399. .get_lpage_level = svm_get_lpage_level,
  4400. .cpuid_update = svm_cpuid_update,
  4401. .rdtscp_supported = svm_rdtscp_supported,
  4402. .invpcid_supported = svm_invpcid_supported,
  4403. .mpx_supported = svm_mpx_supported,
  4404. .xsaves_supported = svm_xsaves_supported,
  4405. .set_supported_cpuid = svm_set_supported_cpuid,
  4406. .has_wbinvd_exit = svm_has_wbinvd_exit,
  4407. .write_tsc_offset = svm_write_tsc_offset,
  4408. .set_tdp_cr3 = set_tdp_cr3,
  4409. .check_intercept = svm_check_intercept,
  4410. .handle_external_intr = svm_handle_external_intr,
  4411. .sched_in = svm_sched_in,
  4412. .pmu_ops = &amd_pmu_ops,
  4413. .deliver_posted_interrupt = svm_deliver_avic_intr,
  4414. .update_pi_irte = svm_update_pi_irte,
  4415. };
  4416. static int __init svm_init(void)
  4417. {
  4418. return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
  4419. __alignof__(struct vcpu_svm), THIS_MODULE);
  4420. }
  4421. static void __exit svm_exit(void)
  4422. {
  4423. kvm_exit();
  4424. }
  4425. module_init(svm_init)
  4426. module_exit(svm_exit)