svm.c 139 KB

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