svm.c 141 KB

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