svm.c 130 KB

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