svm.c 141 KB

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