svm.c 143 KB

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