svm.c 139 KB

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