svm.c 141 KB

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