x86.c 133 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * derived from drivers/kvm/kvm_main.c
  5. *
  6. * Copyright (C) 2006 Qumranet, Inc.
  7. * Copyright (C) 2008 Qumranet, Inc.
  8. * Copyright IBM Corporation, 2008
  9. *
  10. * Authors:
  11. * Avi Kivity <avi@qumranet.com>
  12. * Yaniv Kamay <yaniv@qumranet.com>
  13. * Amit Shah <amit.shah@qumranet.com>
  14. * Ben-Ami Yassour <benami@il.ibm.com>
  15. *
  16. * This work is licensed under the terms of the GNU GPL, version 2. See
  17. * the COPYING file in the top-level directory.
  18. *
  19. */
  20. #include <linux/kvm_host.h>
  21. #include "irq.h"
  22. #include "mmu.h"
  23. #include "i8254.h"
  24. #include "tss.h"
  25. #include "kvm_cache_regs.h"
  26. #include "x86.h"
  27. #include <linux/clocksource.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/kvm.h>
  30. #include <linux/fs.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/module.h>
  33. #include <linux/mman.h>
  34. #include <linux/highmem.h>
  35. #include <linux/iommu.h>
  36. #include <linux/intel-iommu.h>
  37. #include <linux/cpufreq.h>
  38. #include <linux/user-return-notifier.h>
  39. #include <linux/srcu.h>
  40. #include <linux/slab.h>
  41. #include <linux/perf_event.h>
  42. #include <trace/events/kvm.h>
  43. #define CREATE_TRACE_POINTS
  44. #include "trace.h"
  45. #include <asm/debugreg.h>
  46. #include <asm/uaccess.h>
  47. #include <asm/msr.h>
  48. #include <asm/desc.h>
  49. #include <asm/mtrr.h>
  50. #include <asm/mce.h>
  51. #define MAX_IO_MSRS 256
  52. #define CR0_RESERVED_BITS \
  53. (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
  54. | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
  55. | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
  56. #define CR4_RESERVED_BITS \
  57. (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
  58. | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
  59. | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
  60. | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
  61. #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
  62. #define KVM_MAX_MCE_BANKS 32
  63. #define KVM_MCE_CAP_SUPPORTED MCG_CTL_P
  64. /* EFER defaults:
  65. * - enable syscall per default because its emulated by KVM
  66. * - enable LME and LMA per default on 64 bit KVM
  67. */
  68. #ifdef CONFIG_X86_64
  69. static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
  70. #else
  71. static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
  72. #endif
  73. #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
  74. #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
  75. static void update_cr8_intercept(struct kvm_vcpu *vcpu);
  76. static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
  77. struct kvm_cpuid_entry2 __user *entries);
  78. struct kvm_x86_ops *kvm_x86_ops;
  79. EXPORT_SYMBOL_GPL(kvm_x86_ops);
  80. int ignore_msrs = 0;
  81. module_param_named(ignore_msrs, ignore_msrs, bool, S_IRUGO | S_IWUSR);
  82. #define KVM_NR_SHARED_MSRS 16
  83. struct kvm_shared_msrs_global {
  84. int nr;
  85. u32 msrs[KVM_NR_SHARED_MSRS];
  86. };
  87. struct kvm_shared_msrs {
  88. struct user_return_notifier urn;
  89. bool registered;
  90. struct kvm_shared_msr_values {
  91. u64 host;
  92. u64 curr;
  93. } values[KVM_NR_SHARED_MSRS];
  94. };
  95. static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
  96. static DEFINE_PER_CPU(struct kvm_shared_msrs, shared_msrs);
  97. struct kvm_stats_debugfs_item debugfs_entries[] = {
  98. { "pf_fixed", VCPU_STAT(pf_fixed) },
  99. { "pf_guest", VCPU_STAT(pf_guest) },
  100. { "tlb_flush", VCPU_STAT(tlb_flush) },
  101. { "invlpg", VCPU_STAT(invlpg) },
  102. { "exits", VCPU_STAT(exits) },
  103. { "io_exits", VCPU_STAT(io_exits) },
  104. { "mmio_exits", VCPU_STAT(mmio_exits) },
  105. { "signal_exits", VCPU_STAT(signal_exits) },
  106. { "irq_window", VCPU_STAT(irq_window_exits) },
  107. { "nmi_window", VCPU_STAT(nmi_window_exits) },
  108. { "halt_exits", VCPU_STAT(halt_exits) },
  109. { "halt_wakeup", VCPU_STAT(halt_wakeup) },
  110. { "hypercalls", VCPU_STAT(hypercalls) },
  111. { "request_irq", VCPU_STAT(request_irq_exits) },
  112. { "irq_exits", VCPU_STAT(irq_exits) },
  113. { "host_state_reload", VCPU_STAT(host_state_reload) },
  114. { "efer_reload", VCPU_STAT(efer_reload) },
  115. { "fpu_reload", VCPU_STAT(fpu_reload) },
  116. { "insn_emulation", VCPU_STAT(insn_emulation) },
  117. { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
  118. { "irq_injections", VCPU_STAT(irq_injections) },
  119. { "nmi_injections", VCPU_STAT(nmi_injections) },
  120. { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
  121. { "mmu_pte_write", VM_STAT(mmu_pte_write) },
  122. { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
  123. { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
  124. { "mmu_flooded", VM_STAT(mmu_flooded) },
  125. { "mmu_recycled", VM_STAT(mmu_recycled) },
  126. { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
  127. { "mmu_unsync", VM_STAT(mmu_unsync) },
  128. { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
  129. { "largepages", VM_STAT(lpages) },
  130. { NULL }
  131. };
  132. static void kvm_on_user_return(struct user_return_notifier *urn)
  133. {
  134. unsigned slot;
  135. struct kvm_shared_msrs *locals
  136. = container_of(urn, struct kvm_shared_msrs, urn);
  137. struct kvm_shared_msr_values *values;
  138. for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
  139. values = &locals->values[slot];
  140. if (values->host != values->curr) {
  141. wrmsrl(shared_msrs_global.msrs[slot], values->host);
  142. values->curr = values->host;
  143. }
  144. }
  145. locals->registered = false;
  146. user_return_notifier_unregister(urn);
  147. }
  148. static void shared_msr_update(unsigned slot, u32 msr)
  149. {
  150. struct kvm_shared_msrs *smsr;
  151. u64 value;
  152. smsr = &__get_cpu_var(shared_msrs);
  153. /* only read, and nobody should modify it at this time,
  154. * so don't need lock */
  155. if (slot >= shared_msrs_global.nr) {
  156. printk(KERN_ERR "kvm: invalid MSR slot!");
  157. return;
  158. }
  159. rdmsrl_safe(msr, &value);
  160. smsr->values[slot].host = value;
  161. smsr->values[slot].curr = value;
  162. }
  163. void kvm_define_shared_msr(unsigned slot, u32 msr)
  164. {
  165. if (slot >= shared_msrs_global.nr)
  166. shared_msrs_global.nr = slot + 1;
  167. shared_msrs_global.msrs[slot] = msr;
  168. /* we need ensured the shared_msr_global have been updated */
  169. smp_wmb();
  170. }
  171. EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
  172. static void kvm_shared_msr_cpu_online(void)
  173. {
  174. unsigned i;
  175. for (i = 0; i < shared_msrs_global.nr; ++i)
  176. shared_msr_update(i, shared_msrs_global.msrs[i]);
  177. }
  178. void kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
  179. {
  180. struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs);
  181. if (((value ^ smsr->values[slot].curr) & mask) == 0)
  182. return;
  183. smsr->values[slot].curr = value;
  184. wrmsrl(shared_msrs_global.msrs[slot], value);
  185. if (!smsr->registered) {
  186. smsr->urn.on_user_return = kvm_on_user_return;
  187. user_return_notifier_register(&smsr->urn);
  188. smsr->registered = true;
  189. }
  190. }
  191. EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
  192. static void drop_user_return_notifiers(void *ignore)
  193. {
  194. struct kvm_shared_msrs *smsr = &__get_cpu_var(shared_msrs);
  195. if (smsr->registered)
  196. kvm_on_user_return(&smsr->urn);
  197. }
  198. u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
  199. {
  200. if (irqchip_in_kernel(vcpu->kvm))
  201. return vcpu->arch.apic_base;
  202. else
  203. return vcpu->arch.apic_base;
  204. }
  205. EXPORT_SYMBOL_GPL(kvm_get_apic_base);
  206. void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
  207. {
  208. /* TODO: reserve bits check */
  209. if (irqchip_in_kernel(vcpu->kvm))
  210. kvm_lapic_set_base(vcpu, data);
  211. else
  212. vcpu->arch.apic_base = data;
  213. }
  214. EXPORT_SYMBOL_GPL(kvm_set_apic_base);
  215. #define EXCPT_BENIGN 0
  216. #define EXCPT_CONTRIBUTORY 1
  217. #define EXCPT_PF 2
  218. static int exception_class(int vector)
  219. {
  220. switch (vector) {
  221. case PF_VECTOR:
  222. return EXCPT_PF;
  223. case DE_VECTOR:
  224. case TS_VECTOR:
  225. case NP_VECTOR:
  226. case SS_VECTOR:
  227. case GP_VECTOR:
  228. return EXCPT_CONTRIBUTORY;
  229. default:
  230. break;
  231. }
  232. return EXCPT_BENIGN;
  233. }
  234. static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
  235. unsigned nr, bool has_error, u32 error_code,
  236. bool reinject)
  237. {
  238. u32 prev_nr;
  239. int class1, class2;
  240. if (!vcpu->arch.exception.pending) {
  241. queue:
  242. vcpu->arch.exception.pending = true;
  243. vcpu->arch.exception.has_error_code = has_error;
  244. vcpu->arch.exception.nr = nr;
  245. vcpu->arch.exception.error_code = error_code;
  246. vcpu->arch.exception.reinject = reinject;
  247. return;
  248. }
  249. /* to check exception */
  250. prev_nr = vcpu->arch.exception.nr;
  251. if (prev_nr == DF_VECTOR) {
  252. /* triple fault -> shutdown */
  253. set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
  254. return;
  255. }
  256. class1 = exception_class(prev_nr);
  257. class2 = exception_class(nr);
  258. if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
  259. || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
  260. /* generate double fault per SDM Table 5-5 */
  261. vcpu->arch.exception.pending = true;
  262. vcpu->arch.exception.has_error_code = true;
  263. vcpu->arch.exception.nr = DF_VECTOR;
  264. vcpu->arch.exception.error_code = 0;
  265. } else
  266. /* replace previous exception with a new one in a hope
  267. that instruction re-execution will regenerate lost
  268. exception */
  269. goto queue;
  270. }
  271. void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
  272. {
  273. kvm_multiple_exception(vcpu, nr, false, 0, false);
  274. }
  275. EXPORT_SYMBOL_GPL(kvm_queue_exception);
  276. void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
  277. {
  278. kvm_multiple_exception(vcpu, nr, false, 0, true);
  279. }
  280. EXPORT_SYMBOL_GPL(kvm_requeue_exception);
  281. void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
  282. u32 error_code)
  283. {
  284. ++vcpu->stat.pf_guest;
  285. vcpu->arch.cr2 = addr;
  286. kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
  287. }
  288. void kvm_inject_nmi(struct kvm_vcpu *vcpu)
  289. {
  290. vcpu->arch.nmi_pending = 1;
  291. }
  292. EXPORT_SYMBOL_GPL(kvm_inject_nmi);
  293. void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
  294. {
  295. kvm_multiple_exception(vcpu, nr, true, error_code, false);
  296. }
  297. EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
  298. void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
  299. {
  300. kvm_multiple_exception(vcpu, nr, true, error_code, true);
  301. }
  302. EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
  303. /*
  304. * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
  305. * a #GP and return false.
  306. */
  307. bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
  308. {
  309. if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
  310. return true;
  311. kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
  312. return false;
  313. }
  314. EXPORT_SYMBOL_GPL(kvm_require_cpl);
  315. /*
  316. * Load the pae pdptrs. Return true is they are all valid.
  317. */
  318. int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
  319. {
  320. gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
  321. unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
  322. int i;
  323. int ret;
  324. u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
  325. ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
  326. offset * sizeof(u64), sizeof(pdpte));
  327. if (ret < 0) {
  328. ret = 0;
  329. goto out;
  330. }
  331. for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
  332. if (is_present_gpte(pdpte[i]) &&
  333. (pdpte[i] & vcpu->arch.mmu.rsvd_bits_mask[0][2])) {
  334. ret = 0;
  335. goto out;
  336. }
  337. }
  338. ret = 1;
  339. memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
  340. __set_bit(VCPU_EXREG_PDPTR,
  341. (unsigned long *)&vcpu->arch.regs_avail);
  342. __set_bit(VCPU_EXREG_PDPTR,
  343. (unsigned long *)&vcpu->arch.regs_dirty);
  344. out:
  345. return ret;
  346. }
  347. EXPORT_SYMBOL_GPL(load_pdptrs);
  348. static bool pdptrs_changed(struct kvm_vcpu *vcpu)
  349. {
  350. u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
  351. bool changed = true;
  352. int r;
  353. if (is_long_mode(vcpu) || !is_pae(vcpu))
  354. return false;
  355. if (!test_bit(VCPU_EXREG_PDPTR,
  356. (unsigned long *)&vcpu->arch.regs_avail))
  357. return true;
  358. r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
  359. if (r < 0)
  360. goto out;
  361. changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
  362. out:
  363. return changed;
  364. }
  365. void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  366. {
  367. cr0 |= X86_CR0_ET;
  368. #ifdef CONFIG_X86_64
  369. if (cr0 & 0xffffffff00000000UL) {
  370. kvm_inject_gp(vcpu, 0);
  371. return;
  372. }
  373. #endif
  374. cr0 &= ~CR0_RESERVED_BITS;
  375. if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
  376. kvm_inject_gp(vcpu, 0);
  377. return;
  378. }
  379. if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
  380. kvm_inject_gp(vcpu, 0);
  381. return;
  382. }
  383. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  384. #ifdef CONFIG_X86_64
  385. if ((vcpu->arch.efer & EFER_LME)) {
  386. int cs_db, cs_l;
  387. if (!is_pae(vcpu)) {
  388. kvm_inject_gp(vcpu, 0);
  389. return;
  390. }
  391. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  392. if (cs_l) {
  393. kvm_inject_gp(vcpu, 0);
  394. return;
  395. }
  396. } else
  397. #endif
  398. if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
  399. kvm_inject_gp(vcpu, 0);
  400. return;
  401. }
  402. }
  403. kvm_x86_ops->set_cr0(vcpu, cr0);
  404. kvm_mmu_reset_context(vcpu);
  405. return;
  406. }
  407. EXPORT_SYMBOL_GPL(kvm_set_cr0);
  408. void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
  409. {
  410. kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0ful) | (msw & 0x0f));
  411. }
  412. EXPORT_SYMBOL_GPL(kvm_lmsw);
  413. void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  414. {
  415. unsigned long old_cr4 = kvm_read_cr4(vcpu);
  416. unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE;
  417. if (cr4 & CR4_RESERVED_BITS) {
  418. kvm_inject_gp(vcpu, 0);
  419. return;
  420. }
  421. if (is_long_mode(vcpu)) {
  422. if (!(cr4 & X86_CR4_PAE)) {
  423. kvm_inject_gp(vcpu, 0);
  424. return;
  425. }
  426. } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
  427. && ((cr4 ^ old_cr4) & pdptr_bits)
  428. && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
  429. kvm_inject_gp(vcpu, 0);
  430. return;
  431. }
  432. if (cr4 & X86_CR4_VMXE) {
  433. kvm_inject_gp(vcpu, 0);
  434. return;
  435. }
  436. kvm_x86_ops->set_cr4(vcpu, cr4);
  437. vcpu->arch.cr4 = cr4;
  438. kvm_mmu_reset_context(vcpu);
  439. }
  440. EXPORT_SYMBOL_GPL(kvm_set_cr4);
  441. void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  442. {
  443. if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
  444. kvm_mmu_sync_roots(vcpu);
  445. kvm_mmu_flush_tlb(vcpu);
  446. return;
  447. }
  448. if (is_long_mode(vcpu)) {
  449. if (cr3 & CR3_L_MODE_RESERVED_BITS) {
  450. kvm_inject_gp(vcpu, 0);
  451. return;
  452. }
  453. } else {
  454. if (is_pae(vcpu)) {
  455. if (cr3 & CR3_PAE_RESERVED_BITS) {
  456. kvm_inject_gp(vcpu, 0);
  457. return;
  458. }
  459. if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
  460. kvm_inject_gp(vcpu, 0);
  461. return;
  462. }
  463. }
  464. /*
  465. * We don't check reserved bits in nonpae mode, because
  466. * this isn't enforced, and VMware depends on this.
  467. */
  468. }
  469. /*
  470. * Does the new cr3 value map to physical memory? (Note, we
  471. * catch an invalid cr3 even in real-mode, because it would
  472. * cause trouble later on when we turn on paging anyway.)
  473. *
  474. * A real CPU would silently accept an invalid cr3 and would
  475. * attempt to use it - with largely undefined (and often hard
  476. * to debug) behavior on the guest side.
  477. */
  478. if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
  479. kvm_inject_gp(vcpu, 0);
  480. else {
  481. vcpu->arch.cr3 = cr3;
  482. vcpu->arch.mmu.new_cr3(vcpu);
  483. }
  484. }
  485. EXPORT_SYMBOL_GPL(kvm_set_cr3);
  486. void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
  487. {
  488. if (cr8 & CR8_RESERVED_BITS) {
  489. kvm_inject_gp(vcpu, 0);
  490. return;
  491. }
  492. if (irqchip_in_kernel(vcpu->kvm))
  493. kvm_lapic_set_tpr(vcpu, cr8);
  494. else
  495. vcpu->arch.cr8 = cr8;
  496. }
  497. EXPORT_SYMBOL_GPL(kvm_set_cr8);
  498. unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
  499. {
  500. if (irqchip_in_kernel(vcpu->kvm))
  501. return kvm_lapic_get_cr8(vcpu);
  502. else
  503. return vcpu->arch.cr8;
  504. }
  505. EXPORT_SYMBOL_GPL(kvm_get_cr8);
  506. int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
  507. {
  508. switch (dr) {
  509. case 0 ... 3:
  510. vcpu->arch.db[dr] = val;
  511. if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
  512. vcpu->arch.eff_db[dr] = val;
  513. break;
  514. case 4:
  515. if (kvm_read_cr4_bits(vcpu, X86_CR4_DE)) {
  516. kvm_queue_exception(vcpu, UD_VECTOR);
  517. return 1;
  518. }
  519. /* fall through */
  520. case 6:
  521. if (val & 0xffffffff00000000ULL) {
  522. kvm_inject_gp(vcpu, 0);
  523. return 1;
  524. }
  525. vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1;
  526. break;
  527. case 5:
  528. if (kvm_read_cr4_bits(vcpu, X86_CR4_DE)) {
  529. kvm_queue_exception(vcpu, UD_VECTOR);
  530. return 1;
  531. }
  532. /* fall through */
  533. default: /* 7 */
  534. if (val & 0xffffffff00000000ULL) {
  535. kvm_inject_gp(vcpu, 0);
  536. return 1;
  537. }
  538. vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
  539. if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
  540. kvm_x86_ops->set_dr7(vcpu, vcpu->arch.dr7);
  541. vcpu->arch.switch_db_regs = (val & DR7_BP_EN_MASK);
  542. }
  543. break;
  544. }
  545. return 0;
  546. }
  547. EXPORT_SYMBOL_GPL(kvm_set_dr);
  548. int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
  549. {
  550. switch (dr) {
  551. case 0 ... 3:
  552. *val = vcpu->arch.db[dr];
  553. break;
  554. case 4:
  555. if (kvm_read_cr4_bits(vcpu, X86_CR4_DE)) {
  556. kvm_queue_exception(vcpu, UD_VECTOR);
  557. return 1;
  558. }
  559. /* fall through */
  560. case 6:
  561. *val = vcpu->arch.dr6;
  562. break;
  563. case 5:
  564. if (kvm_read_cr4_bits(vcpu, X86_CR4_DE)) {
  565. kvm_queue_exception(vcpu, UD_VECTOR);
  566. return 1;
  567. }
  568. /* fall through */
  569. default: /* 7 */
  570. *val = vcpu->arch.dr7;
  571. break;
  572. }
  573. return 0;
  574. }
  575. EXPORT_SYMBOL_GPL(kvm_get_dr);
  576. static inline u32 bit(int bitno)
  577. {
  578. return 1 << (bitno & 31);
  579. }
  580. /*
  581. * List of msr numbers which we expose to userspace through KVM_GET_MSRS
  582. * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
  583. *
  584. * This list is modified at module load time to reflect the
  585. * capabilities of the host cpu. This capabilities test skips MSRs that are
  586. * kvm-specific. Those are put in the beginning of the list.
  587. */
  588. #define KVM_SAVE_MSRS_BEGIN 7
  589. static u32 msrs_to_save[] = {
  590. MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
  591. MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
  592. HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
  593. HV_X64_MSR_APIC_ASSIST_PAGE,
  594. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  595. MSR_K6_STAR,
  596. #ifdef CONFIG_X86_64
  597. MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
  598. #endif
  599. MSR_IA32_TSC, MSR_IA32_PERF_STATUS, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA
  600. };
  601. static unsigned num_msrs_to_save;
  602. static u32 emulated_msrs[] = {
  603. MSR_IA32_MISC_ENABLE,
  604. };
  605. static int set_efer(struct kvm_vcpu *vcpu, u64 efer)
  606. {
  607. if (efer & efer_reserved_bits)
  608. return 1;
  609. if (is_paging(vcpu)
  610. && (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
  611. return 1;
  612. if (efer & EFER_FFXSR) {
  613. struct kvm_cpuid_entry2 *feat;
  614. feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
  615. if (!feat || !(feat->edx & bit(X86_FEATURE_FXSR_OPT)))
  616. return 1;
  617. }
  618. if (efer & EFER_SVME) {
  619. struct kvm_cpuid_entry2 *feat;
  620. feat = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
  621. if (!feat || !(feat->ecx & bit(X86_FEATURE_SVM)))
  622. return 1;
  623. }
  624. kvm_x86_ops->set_efer(vcpu, efer);
  625. efer &= ~EFER_LMA;
  626. efer |= vcpu->arch.efer & EFER_LMA;
  627. vcpu->arch.efer = efer;
  628. vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled;
  629. kvm_mmu_reset_context(vcpu);
  630. return 0;
  631. }
  632. void kvm_enable_efer_bits(u64 mask)
  633. {
  634. efer_reserved_bits &= ~mask;
  635. }
  636. EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
  637. /*
  638. * Writes msr value into into the appropriate "register".
  639. * Returns 0 on success, non-0 otherwise.
  640. * Assumes vcpu_load() was already called.
  641. */
  642. int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  643. {
  644. return kvm_x86_ops->set_msr(vcpu, msr_index, data);
  645. }
  646. /*
  647. * Adapt set_msr() to msr_io()'s calling convention
  648. */
  649. static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
  650. {
  651. return kvm_set_msr(vcpu, index, *data);
  652. }
  653. static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
  654. {
  655. int version;
  656. int r;
  657. struct pvclock_wall_clock wc;
  658. struct timespec boot;
  659. if (!wall_clock)
  660. return;
  661. r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
  662. if (r)
  663. return;
  664. if (version & 1)
  665. ++version; /* first time write, random junk */
  666. ++version;
  667. kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
  668. /*
  669. * The guest calculates current wall clock time by adding
  670. * system time (updated by kvm_write_guest_time below) to the
  671. * wall clock specified here. guest system time equals host
  672. * system time for us, thus we must fill in host boot time here.
  673. */
  674. getboottime(&boot);
  675. wc.sec = boot.tv_sec;
  676. wc.nsec = boot.tv_nsec;
  677. wc.version = version;
  678. kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
  679. version++;
  680. kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
  681. }
  682. static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
  683. {
  684. uint32_t quotient, remainder;
  685. /* Don't try to replace with do_div(), this one calculates
  686. * "(dividend << 32) / divisor" */
  687. __asm__ ( "divl %4"
  688. : "=a" (quotient), "=d" (remainder)
  689. : "0" (0), "1" (dividend), "r" (divisor) );
  690. return quotient;
  691. }
  692. static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock)
  693. {
  694. uint64_t nsecs = 1000000000LL;
  695. int32_t shift = 0;
  696. uint64_t tps64;
  697. uint32_t tps32;
  698. tps64 = tsc_khz * 1000LL;
  699. while (tps64 > nsecs*2) {
  700. tps64 >>= 1;
  701. shift--;
  702. }
  703. tps32 = (uint32_t)tps64;
  704. while (tps32 <= (uint32_t)nsecs) {
  705. tps32 <<= 1;
  706. shift++;
  707. }
  708. hv_clock->tsc_shift = shift;
  709. hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32);
  710. pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
  711. __func__, tsc_khz, hv_clock->tsc_shift,
  712. hv_clock->tsc_to_system_mul);
  713. }
  714. static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
  715. static void kvm_write_guest_time(struct kvm_vcpu *v)
  716. {
  717. struct timespec ts;
  718. unsigned long flags;
  719. struct kvm_vcpu_arch *vcpu = &v->arch;
  720. void *shared_kaddr;
  721. unsigned long this_tsc_khz;
  722. if ((!vcpu->time_page))
  723. return;
  724. this_tsc_khz = get_cpu_var(cpu_tsc_khz);
  725. if (unlikely(vcpu->hv_clock_tsc_khz != this_tsc_khz)) {
  726. kvm_set_time_scale(this_tsc_khz, &vcpu->hv_clock);
  727. vcpu->hv_clock_tsc_khz = this_tsc_khz;
  728. }
  729. put_cpu_var(cpu_tsc_khz);
  730. /* Keep irq disabled to prevent changes to the clock */
  731. local_irq_save(flags);
  732. kvm_get_msr(v, MSR_IA32_TSC, &vcpu->hv_clock.tsc_timestamp);
  733. ktime_get_ts(&ts);
  734. monotonic_to_bootbased(&ts);
  735. local_irq_restore(flags);
  736. /* With all the info we got, fill in the values */
  737. vcpu->hv_clock.system_time = ts.tv_nsec +
  738. (NSEC_PER_SEC * (u64)ts.tv_sec) + v->kvm->arch.kvmclock_offset;
  739. /*
  740. * The interface expects us to write an even number signaling that the
  741. * update is finished. Since the guest won't see the intermediate
  742. * state, we just increase by 2 at the end.
  743. */
  744. vcpu->hv_clock.version += 2;
  745. shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
  746. memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
  747. sizeof(vcpu->hv_clock));
  748. kunmap_atomic(shared_kaddr, KM_USER0);
  749. mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
  750. }
  751. static int kvm_request_guest_time_update(struct kvm_vcpu *v)
  752. {
  753. struct kvm_vcpu_arch *vcpu = &v->arch;
  754. if (!vcpu->time_page)
  755. return 0;
  756. set_bit(KVM_REQ_KVMCLOCK_UPDATE, &v->requests);
  757. return 1;
  758. }
  759. static bool msr_mtrr_valid(unsigned msr)
  760. {
  761. switch (msr) {
  762. case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
  763. case MSR_MTRRfix64K_00000:
  764. case MSR_MTRRfix16K_80000:
  765. case MSR_MTRRfix16K_A0000:
  766. case MSR_MTRRfix4K_C0000:
  767. case MSR_MTRRfix4K_C8000:
  768. case MSR_MTRRfix4K_D0000:
  769. case MSR_MTRRfix4K_D8000:
  770. case MSR_MTRRfix4K_E0000:
  771. case MSR_MTRRfix4K_E8000:
  772. case MSR_MTRRfix4K_F0000:
  773. case MSR_MTRRfix4K_F8000:
  774. case MSR_MTRRdefType:
  775. case MSR_IA32_CR_PAT:
  776. return true;
  777. case 0x2f8:
  778. return true;
  779. }
  780. return false;
  781. }
  782. static bool valid_pat_type(unsigned t)
  783. {
  784. return t < 8 && (1 << t) & 0xf3; /* 0, 1, 4, 5, 6, 7 */
  785. }
  786. static bool valid_mtrr_type(unsigned t)
  787. {
  788. return t < 8 && (1 << t) & 0x73; /* 0, 1, 4, 5, 6 */
  789. }
  790. static bool mtrr_valid(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  791. {
  792. int i;
  793. if (!msr_mtrr_valid(msr))
  794. return false;
  795. if (msr == MSR_IA32_CR_PAT) {
  796. for (i = 0; i < 8; i++)
  797. if (!valid_pat_type((data >> (i * 8)) & 0xff))
  798. return false;
  799. return true;
  800. } else if (msr == MSR_MTRRdefType) {
  801. if (data & ~0xcff)
  802. return false;
  803. return valid_mtrr_type(data & 0xff);
  804. } else if (msr >= MSR_MTRRfix64K_00000 && msr <= MSR_MTRRfix4K_F8000) {
  805. for (i = 0; i < 8 ; i++)
  806. if (!valid_mtrr_type((data >> (i * 8)) & 0xff))
  807. return false;
  808. return true;
  809. }
  810. /* variable MTRRs */
  811. return valid_mtrr_type(data & 0xff);
  812. }
  813. static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  814. {
  815. u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
  816. if (!mtrr_valid(vcpu, msr, data))
  817. return 1;
  818. if (msr == MSR_MTRRdefType) {
  819. vcpu->arch.mtrr_state.def_type = data;
  820. vcpu->arch.mtrr_state.enabled = (data & 0xc00) >> 10;
  821. } else if (msr == MSR_MTRRfix64K_00000)
  822. p[0] = data;
  823. else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
  824. p[1 + msr - MSR_MTRRfix16K_80000] = data;
  825. else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
  826. p[3 + msr - MSR_MTRRfix4K_C0000] = data;
  827. else if (msr == MSR_IA32_CR_PAT)
  828. vcpu->arch.pat = data;
  829. else { /* Variable MTRRs */
  830. int idx, is_mtrr_mask;
  831. u64 *pt;
  832. idx = (msr - 0x200) / 2;
  833. is_mtrr_mask = msr - 0x200 - 2 * idx;
  834. if (!is_mtrr_mask)
  835. pt =
  836. (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
  837. else
  838. pt =
  839. (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
  840. *pt = data;
  841. }
  842. kvm_mmu_reset_context(vcpu);
  843. return 0;
  844. }
  845. static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  846. {
  847. u64 mcg_cap = vcpu->arch.mcg_cap;
  848. unsigned bank_num = mcg_cap & 0xff;
  849. switch (msr) {
  850. case MSR_IA32_MCG_STATUS:
  851. vcpu->arch.mcg_status = data;
  852. break;
  853. case MSR_IA32_MCG_CTL:
  854. if (!(mcg_cap & MCG_CTL_P))
  855. return 1;
  856. if (data != 0 && data != ~(u64)0)
  857. return -1;
  858. vcpu->arch.mcg_ctl = data;
  859. break;
  860. default:
  861. if (msr >= MSR_IA32_MC0_CTL &&
  862. msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
  863. u32 offset = msr - MSR_IA32_MC0_CTL;
  864. /* only 0 or all 1s can be written to IA32_MCi_CTL
  865. * some Linux kernels though clear bit 10 in bank 4 to
  866. * workaround a BIOS/GART TBL issue on AMD K8s, ignore
  867. * this to avoid an uncatched #GP in the guest
  868. */
  869. if ((offset & 0x3) == 0 &&
  870. data != 0 && (data | (1 << 10)) != ~(u64)0)
  871. return -1;
  872. vcpu->arch.mce_banks[offset] = data;
  873. break;
  874. }
  875. return 1;
  876. }
  877. return 0;
  878. }
  879. static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
  880. {
  881. struct kvm *kvm = vcpu->kvm;
  882. int lm = is_long_mode(vcpu);
  883. u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
  884. : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
  885. u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
  886. : kvm->arch.xen_hvm_config.blob_size_32;
  887. u32 page_num = data & ~PAGE_MASK;
  888. u64 page_addr = data & PAGE_MASK;
  889. u8 *page;
  890. int r;
  891. r = -E2BIG;
  892. if (page_num >= blob_size)
  893. goto out;
  894. r = -ENOMEM;
  895. page = kzalloc(PAGE_SIZE, GFP_KERNEL);
  896. if (!page)
  897. goto out;
  898. r = -EFAULT;
  899. if (copy_from_user(page, blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE))
  900. goto out_free;
  901. if (kvm_write_guest(kvm, page_addr, page, PAGE_SIZE))
  902. goto out_free;
  903. r = 0;
  904. out_free:
  905. kfree(page);
  906. out:
  907. return r;
  908. }
  909. static bool kvm_hv_hypercall_enabled(struct kvm *kvm)
  910. {
  911. return kvm->arch.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE;
  912. }
  913. static bool kvm_hv_msr_partition_wide(u32 msr)
  914. {
  915. bool r = false;
  916. switch (msr) {
  917. case HV_X64_MSR_GUEST_OS_ID:
  918. case HV_X64_MSR_HYPERCALL:
  919. r = true;
  920. break;
  921. }
  922. return r;
  923. }
  924. static int set_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  925. {
  926. struct kvm *kvm = vcpu->kvm;
  927. switch (msr) {
  928. case HV_X64_MSR_GUEST_OS_ID:
  929. kvm->arch.hv_guest_os_id = data;
  930. /* setting guest os id to zero disables hypercall page */
  931. if (!kvm->arch.hv_guest_os_id)
  932. kvm->arch.hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
  933. break;
  934. case HV_X64_MSR_HYPERCALL: {
  935. u64 gfn;
  936. unsigned long addr;
  937. u8 instructions[4];
  938. /* if guest os id is not set hypercall should remain disabled */
  939. if (!kvm->arch.hv_guest_os_id)
  940. break;
  941. if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
  942. kvm->arch.hv_hypercall = data;
  943. break;
  944. }
  945. gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
  946. addr = gfn_to_hva(kvm, gfn);
  947. if (kvm_is_error_hva(addr))
  948. return 1;
  949. kvm_x86_ops->patch_hypercall(vcpu, instructions);
  950. ((unsigned char *)instructions)[3] = 0xc3; /* ret */
  951. if (copy_to_user((void __user *)addr, instructions, 4))
  952. return 1;
  953. kvm->arch.hv_hypercall = data;
  954. break;
  955. }
  956. default:
  957. pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
  958. "data 0x%llx\n", msr, data);
  959. return 1;
  960. }
  961. return 0;
  962. }
  963. static int set_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  964. {
  965. switch (msr) {
  966. case HV_X64_MSR_APIC_ASSIST_PAGE: {
  967. unsigned long addr;
  968. if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) {
  969. vcpu->arch.hv_vapic = data;
  970. break;
  971. }
  972. addr = gfn_to_hva(vcpu->kvm, data >>
  973. HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT);
  974. if (kvm_is_error_hva(addr))
  975. return 1;
  976. if (clear_user((void __user *)addr, PAGE_SIZE))
  977. return 1;
  978. vcpu->arch.hv_vapic = data;
  979. break;
  980. }
  981. case HV_X64_MSR_EOI:
  982. return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
  983. case HV_X64_MSR_ICR:
  984. return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
  985. case HV_X64_MSR_TPR:
  986. return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
  987. default:
  988. pr_unimpl(vcpu, "HYPER-V unimplemented wrmsr: 0x%x "
  989. "data 0x%llx\n", msr, data);
  990. return 1;
  991. }
  992. return 0;
  993. }
  994. int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  995. {
  996. switch (msr) {
  997. case MSR_EFER:
  998. return set_efer(vcpu, data);
  999. case MSR_K7_HWCR:
  1000. data &= ~(u64)0x40; /* ignore flush filter disable */
  1001. data &= ~(u64)0x100; /* ignore ignne emulation enable */
  1002. if (data != 0) {
  1003. pr_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
  1004. data);
  1005. return 1;
  1006. }
  1007. break;
  1008. case MSR_FAM10H_MMIO_CONF_BASE:
  1009. if (data != 0) {
  1010. pr_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
  1011. "0x%llx\n", data);
  1012. return 1;
  1013. }
  1014. break;
  1015. case MSR_AMD64_NB_CFG:
  1016. break;
  1017. case MSR_IA32_DEBUGCTLMSR:
  1018. if (!data) {
  1019. /* We support the non-activated case already */
  1020. break;
  1021. } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
  1022. /* Values other than LBR and BTF are vendor-specific,
  1023. thus reserved and should throw a #GP */
  1024. return 1;
  1025. }
  1026. pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
  1027. __func__, data);
  1028. break;
  1029. case MSR_IA32_UCODE_REV:
  1030. case MSR_IA32_UCODE_WRITE:
  1031. case MSR_VM_HSAVE_PA:
  1032. case MSR_AMD64_PATCH_LOADER:
  1033. break;
  1034. case 0x200 ... 0x2ff:
  1035. return set_msr_mtrr(vcpu, msr, data);
  1036. case MSR_IA32_APICBASE:
  1037. kvm_set_apic_base(vcpu, data);
  1038. break;
  1039. case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
  1040. return kvm_x2apic_msr_write(vcpu, msr, data);
  1041. case MSR_IA32_MISC_ENABLE:
  1042. vcpu->arch.ia32_misc_enable_msr = data;
  1043. break;
  1044. case MSR_KVM_WALL_CLOCK_NEW:
  1045. case MSR_KVM_WALL_CLOCK:
  1046. vcpu->kvm->arch.wall_clock = data;
  1047. kvm_write_wall_clock(vcpu->kvm, data);
  1048. break;
  1049. case MSR_KVM_SYSTEM_TIME_NEW:
  1050. case MSR_KVM_SYSTEM_TIME: {
  1051. if (vcpu->arch.time_page) {
  1052. kvm_release_page_dirty(vcpu->arch.time_page);
  1053. vcpu->arch.time_page = NULL;
  1054. }
  1055. vcpu->arch.time = data;
  1056. /* we verify if the enable bit is set... */
  1057. if (!(data & 1))
  1058. break;
  1059. /* ...but clean it before doing the actual write */
  1060. vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
  1061. vcpu->arch.time_page =
  1062. gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
  1063. if (is_error_page(vcpu->arch.time_page)) {
  1064. kvm_release_page_clean(vcpu->arch.time_page);
  1065. vcpu->arch.time_page = NULL;
  1066. }
  1067. kvm_request_guest_time_update(vcpu);
  1068. break;
  1069. }
  1070. case MSR_IA32_MCG_CTL:
  1071. case MSR_IA32_MCG_STATUS:
  1072. case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
  1073. return set_msr_mce(vcpu, msr, data);
  1074. /* Performance counters are not protected by a CPUID bit,
  1075. * so we should check all of them in the generic path for the sake of
  1076. * cross vendor migration.
  1077. * Writing a zero into the event select MSRs disables them,
  1078. * which we perfectly emulate ;-). Any other value should be at least
  1079. * reported, some guests depend on them.
  1080. */
  1081. case MSR_P6_EVNTSEL0:
  1082. case MSR_P6_EVNTSEL1:
  1083. case MSR_K7_EVNTSEL0:
  1084. case MSR_K7_EVNTSEL1:
  1085. case MSR_K7_EVNTSEL2:
  1086. case MSR_K7_EVNTSEL3:
  1087. if (data != 0)
  1088. pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
  1089. "0x%x data 0x%llx\n", msr, data);
  1090. break;
  1091. /* at least RHEL 4 unconditionally writes to the perfctr registers,
  1092. * so we ignore writes to make it happy.
  1093. */
  1094. case MSR_P6_PERFCTR0:
  1095. case MSR_P6_PERFCTR1:
  1096. case MSR_K7_PERFCTR0:
  1097. case MSR_K7_PERFCTR1:
  1098. case MSR_K7_PERFCTR2:
  1099. case MSR_K7_PERFCTR3:
  1100. pr_unimpl(vcpu, "unimplemented perfctr wrmsr: "
  1101. "0x%x data 0x%llx\n", msr, data);
  1102. break;
  1103. case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
  1104. if (kvm_hv_msr_partition_wide(msr)) {
  1105. int r;
  1106. mutex_lock(&vcpu->kvm->lock);
  1107. r = set_msr_hyperv_pw(vcpu, msr, data);
  1108. mutex_unlock(&vcpu->kvm->lock);
  1109. return r;
  1110. } else
  1111. return set_msr_hyperv(vcpu, msr, data);
  1112. break;
  1113. default:
  1114. if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
  1115. return xen_hvm_config(vcpu, data);
  1116. if (!ignore_msrs) {
  1117. pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n",
  1118. msr, data);
  1119. return 1;
  1120. } else {
  1121. pr_unimpl(vcpu, "ignored wrmsr: 0x%x data %llx\n",
  1122. msr, data);
  1123. break;
  1124. }
  1125. }
  1126. return 0;
  1127. }
  1128. EXPORT_SYMBOL_GPL(kvm_set_msr_common);
  1129. /*
  1130. * Reads an msr value (of 'msr_index') into 'pdata'.
  1131. * Returns 0 on success, non-0 otherwise.
  1132. * Assumes vcpu_load() was already called.
  1133. */
  1134. int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  1135. {
  1136. return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
  1137. }
  1138. static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1139. {
  1140. u64 *p = (u64 *)&vcpu->arch.mtrr_state.fixed_ranges;
  1141. if (!msr_mtrr_valid(msr))
  1142. return 1;
  1143. if (msr == MSR_MTRRdefType)
  1144. *pdata = vcpu->arch.mtrr_state.def_type +
  1145. (vcpu->arch.mtrr_state.enabled << 10);
  1146. else if (msr == MSR_MTRRfix64K_00000)
  1147. *pdata = p[0];
  1148. else if (msr == MSR_MTRRfix16K_80000 || msr == MSR_MTRRfix16K_A0000)
  1149. *pdata = p[1 + msr - MSR_MTRRfix16K_80000];
  1150. else if (msr >= MSR_MTRRfix4K_C0000 && msr <= MSR_MTRRfix4K_F8000)
  1151. *pdata = p[3 + msr - MSR_MTRRfix4K_C0000];
  1152. else if (msr == MSR_IA32_CR_PAT)
  1153. *pdata = vcpu->arch.pat;
  1154. else { /* Variable MTRRs */
  1155. int idx, is_mtrr_mask;
  1156. u64 *pt;
  1157. idx = (msr - 0x200) / 2;
  1158. is_mtrr_mask = msr - 0x200 - 2 * idx;
  1159. if (!is_mtrr_mask)
  1160. pt =
  1161. (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].base_lo;
  1162. else
  1163. pt =
  1164. (u64 *)&vcpu->arch.mtrr_state.var_ranges[idx].mask_lo;
  1165. *pdata = *pt;
  1166. }
  1167. return 0;
  1168. }
  1169. static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1170. {
  1171. u64 data;
  1172. u64 mcg_cap = vcpu->arch.mcg_cap;
  1173. unsigned bank_num = mcg_cap & 0xff;
  1174. switch (msr) {
  1175. case MSR_IA32_P5_MC_ADDR:
  1176. case MSR_IA32_P5_MC_TYPE:
  1177. data = 0;
  1178. break;
  1179. case MSR_IA32_MCG_CAP:
  1180. data = vcpu->arch.mcg_cap;
  1181. break;
  1182. case MSR_IA32_MCG_CTL:
  1183. if (!(mcg_cap & MCG_CTL_P))
  1184. return 1;
  1185. data = vcpu->arch.mcg_ctl;
  1186. break;
  1187. case MSR_IA32_MCG_STATUS:
  1188. data = vcpu->arch.mcg_status;
  1189. break;
  1190. default:
  1191. if (msr >= MSR_IA32_MC0_CTL &&
  1192. msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
  1193. u32 offset = msr - MSR_IA32_MC0_CTL;
  1194. data = vcpu->arch.mce_banks[offset];
  1195. break;
  1196. }
  1197. return 1;
  1198. }
  1199. *pdata = data;
  1200. return 0;
  1201. }
  1202. static int get_msr_hyperv_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1203. {
  1204. u64 data = 0;
  1205. struct kvm *kvm = vcpu->kvm;
  1206. switch (msr) {
  1207. case HV_X64_MSR_GUEST_OS_ID:
  1208. data = kvm->arch.hv_guest_os_id;
  1209. break;
  1210. case HV_X64_MSR_HYPERCALL:
  1211. data = kvm->arch.hv_hypercall;
  1212. break;
  1213. default:
  1214. pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
  1215. return 1;
  1216. }
  1217. *pdata = data;
  1218. return 0;
  1219. }
  1220. static int get_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1221. {
  1222. u64 data = 0;
  1223. switch (msr) {
  1224. case HV_X64_MSR_VP_INDEX: {
  1225. int r;
  1226. struct kvm_vcpu *v;
  1227. kvm_for_each_vcpu(r, v, vcpu->kvm)
  1228. if (v == vcpu)
  1229. data = r;
  1230. break;
  1231. }
  1232. case HV_X64_MSR_EOI:
  1233. return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
  1234. case HV_X64_MSR_ICR:
  1235. return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
  1236. case HV_X64_MSR_TPR:
  1237. return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
  1238. default:
  1239. pr_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
  1240. return 1;
  1241. }
  1242. *pdata = data;
  1243. return 0;
  1244. }
  1245. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1246. {
  1247. u64 data;
  1248. switch (msr) {
  1249. case MSR_IA32_PLATFORM_ID:
  1250. case MSR_IA32_UCODE_REV:
  1251. case MSR_IA32_EBL_CR_POWERON:
  1252. case MSR_IA32_DEBUGCTLMSR:
  1253. case MSR_IA32_LASTBRANCHFROMIP:
  1254. case MSR_IA32_LASTBRANCHTOIP:
  1255. case MSR_IA32_LASTINTFROMIP:
  1256. case MSR_IA32_LASTINTTOIP:
  1257. case MSR_K8_SYSCFG:
  1258. case MSR_K7_HWCR:
  1259. case MSR_VM_HSAVE_PA:
  1260. case MSR_P6_PERFCTR0:
  1261. case MSR_P6_PERFCTR1:
  1262. case MSR_P6_EVNTSEL0:
  1263. case MSR_P6_EVNTSEL1:
  1264. case MSR_K7_EVNTSEL0:
  1265. case MSR_K7_PERFCTR0:
  1266. case MSR_K8_INT_PENDING_MSG:
  1267. case MSR_AMD64_NB_CFG:
  1268. case MSR_FAM10H_MMIO_CONF_BASE:
  1269. data = 0;
  1270. break;
  1271. case MSR_MTRRcap:
  1272. data = 0x500 | KVM_NR_VAR_MTRR;
  1273. break;
  1274. case 0x200 ... 0x2ff:
  1275. return get_msr_mtrr(vcpu, msr, pdata);
  1276. case 0xcd: /* fsb frequency */
  1277. data = 3;
  1278. break;
  1279. case MSR_IA32_APICBASE:
  1280. data = kvm_get_apic_base(vcpu);
  1281. break;
  1282. case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
  1283. return kvm_x2apic_msr_read(vcpu, msr, pdata);
  1284. break;
  1285. case MSR_IA32_MISC_ENABLE:
  1286. data = vcpu->arch.ia32_misc_enable_msr;
  1287. break;
  1288. case MSR_IA32_PERF_STATUS:
  1289. /* TSC increment by tick */
  1290. data = 1000ULL;
  1291. /* CPU multiplier */
  1292. data |= (((uint64_t)4ULL) << 40);
  1293. break;
  1294. case MSR_EFER:
  1295. data = vcpu->arch.efer;
  1296. break;
  1297. case MSR_KVM_WALL_CLOCK:
  1298. case MSR_KVM_WALL_CLOCK_NEW:
  1299. data = vcpu->kvm->arch.wall_clock;
  1300. break;
  1301. case MSR_KVM_SYSTEM_TIME:
  1302. case MSR_KVM_SYSTEM_TIME_NEW:
  1303. data = vcpu->arch.time;
  1304. break;
  1305. case MSR_IA32_P5_MC_ADDR:
  1306. case MSR_IA32_P5_MC_TYPE:
  1307. case MSR_IA32_MCG_CAP:
  1308. case MSR_IA32_MCG_CTL:
  1309. case MSR_IA32_MCG_STATUS:
  1310. case MSR_IA32_MC0_CTL ... MSR_IA32_MC0_CTL + 4 * KVM_MAX_MCE_BANKS - 1:
  1311. return get_msr_mce(vcpu, msr, pdata);
  1312. case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
  1313. if (kvm_hv_msr_partition_wide(msr)) {
  1314. int r;
  1315. mutex_lock(&vcpu->kvm->lock);
  1316. r = get_msr_hyperv_pw(vcpu, msr, pdata);
  1317. mutex_unlock(&vcpu->kvm->lock);
  1318. return r;
  1319. } else
  1320. return get_msr_hyperv(vcpu, msr, pdata);
  1321. break;
  1322. default:
  1323. if (!ignore_msrs) {
  1324. pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
  1325. return 1;
  1326. } else {
  1327. pr_unimpl(vcpu, "ignored rdmsr: 0x%x\n", msr);
  1328. data = 0;
  1329. }
  1330. break;
  1331. }
  1332. *pdata = data;
  1333. return 0;
  1334. }
  1335. EXPORT_SYMBOL_GPL(kvm_get_msr_common);
  1336. /*
  1337. * Read or write a bunch of msrs. All parameters are kernel addresses.
  1338. *
  1339. * @return number of msrs set successfully.
  1340. */
  1341. static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
  1342. struct kvm_msr_entry *entries,
  1343. int (*do_msr)(struct kvm_vcpu *vcpu,
  1344. unsigned index, u64 *data))
  1345. {
  1346. int i, idx;
  1347. vcpu_load(vcpu);
  1348. idx = srcu_read_lock(&vcpu->kvm->srcu);
  1349. for (i = 0; i < msrs->nmsrs; ++i)
  1350. if (do_msr(vcpu, entries[i].index, &entries[i].data))
  1351. break;
  1352. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  1353. vcpu_put(vcpu);
  1354. return i;
  1355. }
  1356. /*
  1357. * Read or write a bunch of msrs. Parameters are user addresses.
  1358. *
  1359. * @return number of msrs set successfully.
  1360. */
  1361. static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
  1362. int (*do_msr)(struct kvm_vcpu *vcpu,
  1363. unsigned index, u64 *data),
  1364. int writeback)
  1365. {
  1366. struct kvm_msrs msrs;
  1367. struct kvm_msr_entry *entries;
  1368. int r, n;
  1369. unsigned size;
  1370. r = -EFAULT;
  1371. if (copy_from_user(&msrs, user_msrs, sizeof msrs))
  1372. goto out;
  1373. r = -E2BIG;
  1374. if (msrs.nmsrs >= MAX_IO_MSRS)
  1375. goto out;
  1376. r = -ENOMEM;
  1377. size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
  1378. entries = vmalloc(size);
  1379. if (!entries)
  1380. goto out;
  1381. r = -EFAULT;
  1382. if (copy_from_user(entries, user_msrs->entries, size))
  1383. goto out_free;
  1384. r = n = __msr_io(vcpu, &msrs, entries, do_msr);
  1385. if (r < 0)
  1386. goto out_free;
  1387. r = -EFAULT;
  1388. if (writeback && copy_to_user(user_msrs->entries, entries, size))
  1389. goto out_free;
  1390. r = n;
  1391. out_free:
  1392. vfree(entries);
  1393. out:
  1394. return r;
  1395. }
  1396. int kvm_dev_ioctl_check_extension(long ext)
  1397. {
  1398. int r;
  1399. switch (ext) {
  1400. case KVM_CAP_IRQCHIP:
  1401. case KVM_CAP_HLT:
  1402. case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
  1403. case KVM_CAP_SET_TSS_ADDR:
  1404. case KVM_CAP_EXT_CPUID:
  1405. case KVM_CAP_CLOCKSOURCE:
  1406. case KVM_CAP_PIT:
  1407. case KVM_CAP_NOP_IO_DELAY:
  1408. case KVM_CAP_MP_STATE:
  1409. case KVM_CAP_SYNC_MMU:
  1410. case KVM_CAP_REINJECT_CONTROL:
  1411. case KVM_CAP_IRQ_INJECT_STATUS:
  1412. case KVM_CAP_ASSIGN_DEV_IRQ:
  1413. case KVM_CAP_IRQFD:
  1414. case KVM_CAP_IOEVENTFD:
  1415. case KVM_CAP_PIT2:
  1416. case KVM_CAP_PIT_STATE2:
  1417. case KVM_CAP_SET_IDENTITY_MAP_ADDR:
  1418. case KVM_CAP_XEN_HVM:
  1419. case KVM_CAP_ADJUST_CLOCK:
  1420. case KVM_CAP_VCPU_EVENTS:
  1421. case KVM_CAP_HYPERV:
  1422. case KVM_CAP_HYPERV_VAPIC:
  1423. case KVM_CAP_HYPERV_SPIN:
  1424. case KVM_CAP_PCI_SEGMENT:
  1425. case KVM_CAP_DEBUGREGS:
  1426. case KVM_CAP_X86_ROBUST_SINGLESTEP:
  1427. r = 1;
  1428. break;
  1429. case KVM_CAP_COALESCED_MMIO:
  1430. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  1431. break;
  1432. case KVM_CAP_VAPIC:
  1433. r = !kvm_x86_ops->cpu_has_accelerated_tpr();
  1434. break;
  1435. case KVM_CAP_NR_VCPUS:
  1436. r = KVM_MAX_VCPUS;
  1437. break;
  1438. case KVM_CAP_NR_MEMSLOTS:
  1439. r = KVM_MEMORY_SLOTS;
  1440. break;
  1441. case KVM_CAP_PV_MMU: /* obsolete */
  1442. r = 0;
  1443. break;
  1444. case KVM_CAP_IOMMU:
  1445. r = iommu_found();
  1446. break;
  1447. case KVM_CAP_MCE:
  1448. r = KVM_MAX_MCE_BANKS;
  1449. break;
  1450. default:
  1451. r = 0;
  1452. break;
  1453. }
  1454. return r;
  1455. }
  1456. long kvm_arch_dev_ioctl(struct file *filp,
  1457. unsigned int ioctl, unsigned long arg)
  1458. {
  1459. void __user *argp = (void __user *)arg;
  1460. long r;
  1461. switch (ioctl) {
  1462. case KVM_GET_MSR_INDEX_LIST: {
  1463. struct kvm_msr_list __user *user_msr_list = argp;
  1464. struct kvm_msr_list msr_list;
  1465. unsigned n;
  1466. r = -EFAULT;
  1467. if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
  1468. goto out;
  1469. n = msr_list.nmsrs;
  1470. msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
  1471. if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
  1472. goto out;
  1473. r = -E2BIG;
  1474. if (n < msr_list.nmsrs)
  1475. goto out;
  1476. r = -EFAULT;
  1477. if (copy_to_user(user_msr_list->indices, &msrs_to_save,
  1478. num_msrs_to_save * sizeof(u32)))
  1479. goto out;
  1480. if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
  1481. &emulated_msrs,
  1482. ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
  1483. goto out;
  1484. r = 0;
  1485. break;
  1486. }
  1487. case KVM_GET_SUPPORTED_CPUID: {
  1488. struct kvm_cpuid2 __user *cpuid_arg = argp;
  1489. struct kvm_cpuid2 cpuid;
  1490. r = -EFAULT;
  1491. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1492. goto out;
  1493. r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
  1494. cpuid_arg->entries);
  1495. if (r)
  1496. goto out;
  1497. r = -EFAULT;
  1498. if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
  1499. goto out;
  1500. r = 0;
  1501. break;
  1502. }
  1503. case KVM_X86_GET_MCE_CAP_SUPPORTED: {
  1504. u64 mce_cap;
  1505. mce_cap = KVM_MCE_CAP_SUPPORTED;
  1506. r = -EFAULT;
  1507. if (copy_to_user(argp, &mce_cap, sizeof mce_cap))
  1508. goto out;
  1509. r = 0;
  1510. break;
  1511. }
  1512. default:
  1513. r = -EINVAL;
  1514. }
  1515. out:
  1516. return r;
  1517. }
  1518. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1519. {
  1520. kvm_x86_ops->vcpu_load(vcpu, cpu);
  1521. if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0)) {
  1522. unsigned long khz = cpufreq_quick_get(cpu);
  1523. if (!khz)
  1524. khz = tsc_khz;
  1525. per_cpu(cpu_tsc_khz, cpu) = khz;
  1526. }
  1527. kvm_request_guest_time_update(vcpu);
  1528. }
  1529. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  1530. {
  1531. kvm_put_guest_fpu(vcpu);
  1532. kvm_x86_ops->vcpu_put(vcpu);
  1533. }
  1534. static int is_efer_nx(void)
  1535. {
  1536. unsigned long long efer = 0;
  1537. rdmsrl_safe(MSR_EFER, &efer);
  1538. return efer & EFER_NX;
  1539. }
  1540. static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
  1541. {
  1542. int i;
  1543. struct kvm_cpuid_entry2 *e, *entry;
  1544. entry = NULL;
  1545. for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
  1546. e = &vcpu->arch.cpuid_entries[i];
  1547. if (e->function == 0x80000001) {
  1548. entry = e;
  1549. break;
  1550. }
  1551. }
  1552. if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
  1553. entry->edx &= ~(1 << 20);
  1554. printk(KERN_INFO "kvm: guest NX capability removed\n");
  1555. }
  1556. }
  1557. /* when an old userspace process fills a new kernel module */
  1558. static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
  1559. struct kvm_cpuid *cpuid,
  1560. struct kvm_cpuid_entry __user *entries)
  1561. {
  1562. int r, i;
  1563. struct kvm_cpuid_entry *cpuid_entries;
  1564. r = -E2BIG;
  1565. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  1566. goto out;
  1567. r = -ENOMEM;
  1568. cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
  1569. if (!cpuid_entries)
  1570. goto out;
  1571. r = -EFAULT;
  1572. if (copy_from_user(cpuid_entries, entries,
  1573. cpuid->nent * sizeof(struct kvm_cpuid_entry)))
  1574. goto out_free;
  1575. for (i = 0; i < cpuid->nent; i++) {
  1576. vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
  1577. vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
  1578. vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
  1579. vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
  1580. vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
  1581. vcpu->arch.cpuid_entries[i].index = 0;
  1582. vcpu->arch.cpuid_entries[i].flags = 0;
  1583. vcpu->arch.cpuid_entries[i].padding[0] = 0;
  1584. vcpu->arch.cpuid_entries[i].padding[1] = 0;
  1585. vcpu->arch.cpuid_entries[i].padding[2] = 0;
  1586. }
  1587. vcpu->arch.cpuid_nent = cpuid->nent;
  1588. cpuid_fix_nx_cap(vcpu);
  1589. r = 0;
  1590. kvm_apic_set_version(vcpu);
  1591. kvm_x86_ops->cpuid_update(vcpu);
  1592. out_free:
  1593. vfree(cpuid_entries);
  1594. out:
  1595. return r;
  1596. }
  1597. static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
  1598. struct kvm_cpuid2 *cpuid,
  1599. struct kvm_cpuid_entry2 __user *entries)
  1600. {
  1601. int r;
  1602. r = -E2BIG;
  1603. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  1604. goto out;
  1605. r = -EFAULT;
  1606. if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
  1607. cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
  1608. goto out;
  1609. vcpu->arch.cpuid_nent = cpuid->nent;
  1610. kvm_apic_set_version(vcpu);
  1611. kvm_x86_ops->cpuid_update(vcpu);
  1612. return 0;
  1613. out:
  1614. return r;
  1615. }
  1616. static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
  1617. struct kvm_cpuid2 *cpuid,
  1618. struct kvm_cpuid_entry2 __user *entries)
  1619. {
  1620. int r;
  1621. r = -E2BIG;
  1622. if (cpuid->nent < vcpu->arch.cpuid_nent)
  1623. goto out;
  1624. r = -EFAULT;
  1625. if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
  1626. vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
  1627. goto out;
  1628. return 0;
  1629. out:
  1630. cpuid->nent = vcpu->arch.cpuid_nent;
  1631. return r;
  1632. }
  1633. static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
  1634. u32 index)
  1635. {
  1636. entry->function = function;
  1637. entry->index = index;
  1638. cpuid_count(entry->function, entry->index,
  1639. &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
  1640. entry->flags = 0;
  1641. }
  1642. #define F(x) bit(X86_FEATURE_##x)
  1643. static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
  1644. u32 index, int *nent, int maxnent)
  1645. {
  1646. unsigned f_nx = is_efer_nx() ? F(NX) : 0;
  1647. #ifdef CONFIG_X86_64
  1648. unsigned f_gbpages = (kvm_x86_ops->get_lpage_level() == PT_PDPE_LEVEL)
  1649. ? F(GBPAGES) : 0;
  1650. unsigned f_lm = F(LM);
  1651. #else
  1652. unsigned f_gbpages = 0;
  1653. unsigned f_lm = 0;
  1654. #endif
  1655. unsigned f_rdtscp = kvm_x86_ops->rdtscp_supported() ? F(RDTSCP) : 0;
  1656. /* cpuid 1.edx */
  1657. const u32 kvm_supported_word0_x86_features =
  1658. F(FPU) | F(VME) | F(DE) | F(PSE) |
  1659. F(TSC) | F(MSR) | F(PAE) | F(MCE) |
  1660. F(CX8) | F(APIC) | 0 /* Reserved */ | F(SEP) |
  1661. F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
  1662. F(PAT) | F(PSE36) | 0 /* PSN */ | F(CLFLSH) |
  1663. 0 /* Reserved, DS, ACPI */ | F(MMX) |
  1664. F(FXSR) | F(XMM) | F(XMM2) | F(SELFSNOOP) |
  1665. 0 /* HTT, TM, Reserved, PBE */;
  1666. /* cpuid 0x80000001.edx */
  1667. const u32 kvm_supported_word1_x86_features =
  1668. F(FPU) | F(VME) | F(DE) | F(PSE) |
  1669. F(TSC) | F(MSR) | F(PAE) | F(MCE) |
  1670. F(CX8) | F(APIC) | 0 /* Reserved */ | F(SYSCALL) |
  1671. F(MTRR) | F(PGE) | F(MCA) | F(CMOV) |
  1672. F(PAT) | F(PSE36) | 0 /* Reserved */ |
  1673. f_nx | 0 /* Reserved */ | F(MMXEXT) | F(MMX) |
  1674. F(FXSR) | F(FXSR_OPT) | f_gbpages | f_rdtscp |
  1675. 0 /* Reserved */ | f_lm | F(3DNOWEXT) | F(3DNOW);
  1676. /* cpuid 1.ecx */
  1677. const u32 kvm_supported_word4_x86_features =
  1678. F(XMM3) | 0 /* Reserved, DTES64, MONITOR */ |
  1679. 0 /* DS-CPL, VMX, SMX, EST */ |
  1680. 0 /* TM2 */ | F(SSSE3) | 0 /* CNXT-ID */ | 0 /* Reserved */ |
  1681. 0 /* Reserved */ | F(CX16) | 0 /* xTPR Update, PDCM */ |
  1682. 0 /* Reserved, DCA */ | F(XMM4_1) |
  1683. F(XMM4_2) | F(X2APIC) | F(MOVBE) | F(POPCNT) |
  1684. 0 /* Reserved, XSAVE, OSXSAVE */;
  1685. /* cpuid 0x80000001.ecx */
  1686. const u32 kvm_supported_word6_x86_features =
  1687. F(LAHF_LM) | F(CMP_LEGACY) | F(SVM) | 0 /* ExtApicSpace */ |
  1688. F(CR8_LEGACY) | F(ABM) | F(SSE4A) | F(MISALIGNSSE) |
  1689. F(3DNOWPREFETCH) | 0 /* OSVW */ | 0 /* IBS */ | F(SSE5) |
  1690. 0 /* SKINIT */ | 0 /* WDT */;
  1691. /* all calls to cpuid_count() should be made on the same cpu */
  1692. get_cpu();
  1693. do_cpuid_1_ent(entry, function, index);
  1694. ++*nent;
  1695. switch (function) {
  1696. case 0:
  1697. entry->eax = min(entry->eax, (u32)0xb);
  1698. break;
  1699. case 1:
  1700. entry->edx &= kvm_supported_word0_x86_features;
  1701. entry->ecx &= kvm_supported_word4_x86_features;
  1702. /* we support x2apic emulation even if host does not support
  1703. * it since we emulate x2apic in software */
  1704. entry->ecx |= F(X2APIC);
  1705. break;
  1706. /* function 2 entries are STATEFUL. That is, repeated cpuid commands
  1707. * may return different values. This forces us to get_cpu() before
  1708. * issuing the first command, and also to emulate this annoying behavior
  1709. * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
  1710. case 2: {
  1711. int t, times = entry->eax & 0xff;
  1712. entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
  1713. entry->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
  1714. for (t = 1; t < times && *nent < maxnent; ++t) {
  1715. do_cpuid_1_ent(&entry[t], function, 0);
  1716. entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
  1717. ++*nent;
  1718. }
  1719. break;
  1720. }
  1721. /* function 4 and 0xb have additional index. */
  1722. case 4: {
  1723. int i, cache_type;
  1724. entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  1725. /* read more entries until cache_type is zero */
  1726. for (i = 1; *nent < maxnent; ++i) {
  1727. cache_type = entry[i - 1].eax & 0x1f;
  1728. if (!cache_type)
  1729. break;
  1730. do_cpuid_1_ent(&entry[i], function, i);
  1731. entry[i].flags |=
  1732. KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  1733. ++*nent;
  1734. }
  1735. break;
  1736. }
  1737. case 0xb: {
  1738. int i, level_type;
  1739. entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  1740. /* read more entries until level_type is zero */
  1741. for (i = 1; *nent < maxnent; ++i) {
  1742. level_type = entry[i - 1].ecx & 0xff00;
  1743. if (!level_type)
  1744. break;
  1745. do_cpuid_1_ent(&entry[i], function, i);
  1746. entry[i].flags |=
  1747. KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  1748. ++*nent;
  1749. }
  1750. break;
  1751. }
  1752. case KVM_CPUID_SIGNATURE: {
  1753. char signature[12] = "KVMKVMKVM\0\0";
  1754. u32 *sigptr = (u32 *)signature;
  1755. entry->eax = 0;
  1756. entry->ebx = sigptr[0];
  1757. entry->ecx = sigptr[1];
  1758. entry->edx = sigptr[2];
  1759. break;
  1760. }
  1761. case KVM_CPUID_FEATURES:
  1762. entry->eax = (1 << KVM_FEATURE_CLOCKSOURCE) |
  1763. (1 << KVM_FEATURE_NOP_IO_DELAY) |
  1764. (1 << KVM_FEATURE_CLOCKSOURCE2);
  1765. entry->ebx = 0;
  1766. entry->ecx = 0;
  1767. entry->edx = 0;
  1768. break;
  1769. case 0x80000000:
  1770. entry->eax = min(entry->eax, 0x8000001a);
  1771. break;
  1772. case 0x80000001:
  1773. entry->edx &= kvm_supported_word1_x86_features;
  1774. entry->ecx &= kvm_supported_word6_x86_features;
  1775. break;
  1776. }
  1777. kvm_x86_ops->set_supported_cpuid(function, entry);
  1778. put_cpu();
  1779. }
  1780. #undef F
  1781. static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
  1782. struct kvm_cpuid_entry2 __user *entries)
  1783. {
  1784. struct kvm_cpuid_entry2 *cpuid_entries;
  1785. int limit, nent = 0, r = -E2BIG;
  1786. u32 func;
  1787. if (cpuid->nent < 1)
  1788. goto out;
  1789. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  1790. cpuid->nent = KVM_MAX_CPUID_ENTRIES;
  1791. r = -ENOMEM;
  1792. cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
  1793. if (!cpuid_entries)
  1794. goto out;
  1795. do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
  1796. limit = cpuid_entries[0].eax;
  1797. for (func = 1; func <= limit && nent < cpuid->nent; ++func)
  1798. do_cpuid_ent(&cpuid_entries[nent], func, 0,
  1799. &nent, cpuid->nent);
  1800. r = -E2BIG;
  1801. if (nent >= cpuid->nent)
  1802. goto out_free;
  1803. do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
  1804. limit = cpuid_entries[nent - 1].eax;
  1805. for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
  1806. do_cpuid_ent(&cpuid_entries[nent], func, 0,
  1807. &nent, cpuid->nent);
  1808. r = -E2BIG;
  1809. if (nent >= cpuid->nent)
  1810. goto out_free;
  1811. do_cpuid_ent(&cpuid_entries[nent], KVM_CPUID_SIGNATURE, 0, &nent,
  1812. cpuid->nent);
  1813. r = -E2BIG;
  1814. if (nent >= cpuid->nent)
  1815. goto out_free;
  1816. do_cpuid_ent(&cpuid_entries[nent], KVM_CPUID_FEATURES, 0, &nent,
  1817. cpuid->nent);
  1818. r = -E2BIG;
  1819. if (nent >= cpuid->nent)
  1820. goto out_free;
  1821. r = -EFAULT;
  1822. if (copy_to_user(entries, cpuid_entries,
  1823. nent * sizeof(struct kvm_cpuid_entry2)))
  1824. goto out_free;
  1825. cpuid->nent = nent;
  1826. r = 0;
  1827. out_free:
  1828. vfree(cpuid_entries);
  1829. out:
  1830. return r;
  1831. }
  1832. static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
  1833. struct kvm_lapic_state *s)
  1834. {
  1835. vcpu_load(vcpu);
  1836. memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
  1837. vcpu_put(vcpu);
  1838. return 0;
  1839. }
  1840. static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
  1841. struct kvm_lapic_state *s)
  1842. {
  1843. vcpu_load(vcpu);
  1844. memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
  1845. kvm_apic_post_state_restore(vcpu);
  1846. update_cr8_intercept(vcpu);
  1847. vcpu_put(vcpu);
  1848. return 0;
  1849. }
  1850. static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
  1851. struct kvm_interrupt *irq)
  1852. {
  1853. if (irq->irq < 0 || irq->irq >= 256)
  1854. return -EINVAL;
  1855. if (irqchip_in_kernel(vcpu->kvm))
  1856. return -ENXIO;
  1857. vcpu_load(vcpu);
  1858. kvm_queue_interrupt(vcpu, irq->irq, false);
  1859. vcpu_put(vcpu);
  1860. return 0;
  1861. }
  1862. static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
  1863. {
  1864. vcpu_load(vcpu);
  1865. kvm_inject_nmi(vcpu);
  1866. vcpu_put(vcpu);
  1867. return 0;
  1868. }
  1869. static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
  1870. struct kvm_tpr_access_ctl *tac)
  1871. {
  1872. if (tac->flags)
  1873. return -EINVAL;
  1874. vcpu->arch.tpr_access_reporting = !!tac->enabled;
  1875. return 0;
  1876. }
  1877. static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
  1878. u64 mcg_cap)
  1879. {
  1880. int r;
  1881. unsigned bank_num = mcg_cap & 0xff, bank;
  1882. r = -EINVAL;
  1883. if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
  1884. goto out;
  1885. if (mcg_cap & ~(KVM_MCE_CAP_SUPPORTED | 0xff | 0xff0000))
  1886. goto out;
  1887. r = 0;
  1888. vcpu->arch.mcg_cap = mcg_cap;
  1889. /* Init IA32_MCG_CTL to all 1s */
  1890. if (mcg_cap & MCG_CTL_P)
  1891. vcpu->arch.mcg_ctl = ~(u64)0;
  1892. /* Init IA32_MCi_CTL to all 1s */
  1893. for (bank = 0; bank < bank_num; bank++)
  1894. vcpu->arch.mce_banks[bank*4] = ~(u64)0;
  1895. out:
  1896. return r;
  1897. }
  1898. static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
  1899. struct kvm_x86_mce *mce)
  1900. {
  1901. u64 mcg_cap = vcpu->arch.mcg_cap;
  1902. unsigned bank_num = mcg_cap & 0xff;
  1903. u64 *banks = vcpu->arch.mce_banks;
  1904. if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
  1905. return -EINVAL;
  1906. /*
  1907. * if IA32_MCG_CTL is not all 1s, the uncorrected error
  1908. * reporting is disabled
  1909. */
  1910. if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
  1911. vcpu->arch.mcg_ctl != ~(u64)0)
  1912. return 0;
  1913. banks += 4 * mce->bank;
  1914. /*
  1915. * if IA32_MCi_CTL is not all 1s, the uncorrected error
  1916. * reporting is disabled for the bank
  1917. */
  1918. if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
  1919. return 0;
  1920. if (mce->status & MCI_STATUS_UC) {
  1921. if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
  1922. !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
  1923. printk(KERN_DEBUG "kvm: set_mce: "
  1924. "injects mce exception while "
  1925. "previous one is in progress!\n");
  1926. set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
  1927. return 0;
  1928. }
  1929. if (banks[1] & MCI_STATUS_VAL)
  1930. mce->status |= MCI_STATUS_OVER;
  1931. banks[2] = mce->addr;
  1932. banks[3] = mce->misc;
  1933. vcpu->arch.mcg_status = mce->mcg_status;
  1934. banks[1] = mce->status;
  1935. kvm_queue_exception(vcpu, MC_VECTOR);
  1936. } else if (!(banks[1] & MCI_STATUS_VAL)
  1937. || !(banks[1] & MCI_STATUS_UC)) {
  1938. if (banks[1] & MCI_STATUS_VAL)
  1939. mce->status |= MCI_STATUS_OVER;
  1940. banks[2] = mce->addr;
  1941. banks[3] = mce->misc;
  1942. banks[1] = mce->status;
  1943. } else
  1944. banks[1] |= MCI_STATUS_OVER;
  1945. return 0;
  1946. }
  1947. static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
  1948. struct kvm_vcpu_events *events)
  1949. {
  1950. vcpu_load(vcpu);
  1951. events->exception.injected =
  1952. vcpu->arch.exception.pending &&
  1953. !kvm_exception_is_soft(vcpu->arch.exception.nr);
  1954. events->exception.nr = vcpu->arch.exception.nr;
  1955. events->exception.has_error_code = vcpu->arch.exception.has_error_code;
  1956. events->exception.error_code = vcpu->arch.exception.error_code;
  1957. events->interrupt.injected =
  1958. vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft;
  1959. events->interrupt.nr = vcpu->arch.interrupt.nr;
  1960. events->interrupt.soft = 0;
  1961. events->interrupt.shadow =
  1962. kvm_x86_ops->get_interrupt_shadow(vcpu,
  1963. KVM_X86_SHADOW_INT_MOV_SS | KVM_X86_SHADOW_INT_STI);
  1964. events->nmi.injected = vcpu->arch.nmi_injected;
  1965. events->nmi.pending = vcpu->arch.nmi_pending;
  1966. events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
  1967. events->sipi_vector = vcpu->arch.sipi_vector;
  1968. events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
  1969. | KVM_VCPUEVENT_VALID_SIPI_VECTOR
  1970. | KVM_VCPUEVENT_VALID_SHADOW);
  1971. vcpu_put(vcpu);
  1972. }
  1973. static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
  1974. struct kvm_vcpu_events *events)
  1975. {
  1976. if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
  1977. | KVM_VCPUEVENT_VALID_SIPI_VECTOR
  1978. | KVM_VCPUEVENT_VALID_SHADOW))
  1979. return -EINVAL;
  1980. vcpu_load(vcpu);
  1981. vcpu->arch.exception.pending = events->exception.injected;
  1982. vcpu->arch.exception.nr = events->exception.nr;
  1983. vcpu->arch.exception.has_error_code = events->exception.has_error_code;
  1984. vcpu->arch.exception.error_code = events->exception.error_code;
  1985. vcpu->arch.interrupt.pending = events->interrupt.injected;
  1986. vcpu->arch.interrupt.nr = events->interrupt.nr;
  1987. vcpu->arch.interrupt.soft = events->interrupt.soft;
  1988. if (vcpu->arch.interrupt.pending && irqchip_in_kernel(vcpu->kvm))
  1989. kvm_pic_clear_isr_ack(vcpu->kvm);
  1990. if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
  1991. kvm_x86_ops->set_interrupt_shadow(vcpu,
  1992. events->interrupt.shadow);
  1993. vcpu->arch.nmi_injected = events->nmi.injected;
  1994. if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
  1995. vcpu->arch.nmi_pending = events->nmi.pending;
  1996. kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
  1997. if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR)
  1998. vcpu->arch.sipi_vector = events->sipi_vector;
  1999. vcpu_put(vcpu);
  2000. return 0;
  2001. }
  2002. static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
  2003. struct kvm_debugregs *dbgregs)
  2004. {
  2005. vcpu_load(vcpu);
  2006. memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
  2007. dbgregs->dr6 = vcpu->arch.dr6;
  2008. dbgregs->dr7 = vcpu->arch.dr7;
  2009. dbgregs->flags = 0;
  2010. vcpu_put(vcpu);
  2011. }
  2012. static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
  2013. struct kvm_debugregs *dbgregs)
  2014. {
  2015. if (dbgregs->flags)
  2016. return -EINVAL;
  2017. vcpu_load(vcpu);
  2018. memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
  2019. vcpu->arch.dr6 = dbgregs->dr6;
  2020. vcpu->arch.dr7 = dbgregs->dr7;
  2021. vcpu_put(vcpu);
  2022. return 0;
  2023. }
  2024. long kvm_arch_vcpu_ioctl(struct file *filp,
  2025. unsigned int ioctl, unsigned long arg)
  2026. {
  2027. struct kvm_vcpu *vcpu = filp->private_data;
  2028. void __user *argp = (void __user *)arg;
  2029. int r;
  2030. struct kvm_lapic_state *lapic = NULL;
  2031. switch (ioctl) {
  2032. case KVM_GET_LAPIC: {
  2033. r = -EINVAL;
  2034. if (!vcpu->arch.apic)
  2035. goto out;
  2036. lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
  2037. r = -ENOMEM;
  2038. if (!lapic)
  2039. goto out;
  2040. r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic);
  2041. if (r)
  2042. goto out;
  2043. r = -EFAULT;
  2044. if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state)))
  2045. goto out;
  2046. r = 0;
  2047. break;
  2048. }
  2049. case KVM_SET_LAPIC: {
  2050. r = -EINVAL;
  2051. if (!vcpu->arch.apic)
  2052. goto out;
  2053. lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
  2054. r = -ENOMEM;
  2055. if (!lapic)
  2056. goto out;
  2057. r = -EFAULT;
  2058. if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state)))
  2059. goto out;
  2060. r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic);
  2061. if (r)
  2062. goto out;
  2063. r = 0;
  2064. break;
  2065. }
  2066. case KVM_INTERRUPT: {
  2067. struct kvm_interrupt irq;
  2068. r = -EFAULT;
  2069. if (copy_from_user(&irq, argp, sizeof irq))
  2070. goto out;
  2071. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  2072. if (r)
  2073. goto out;
  2074. r = 0;
  2075. break;
  2076. }
  2077. case KVM_NMI: {
  2078. r = kvm_vcpu_ioctl_nmi(vcpu);
  2079. if (r)
  2080. goto out;
  2081. r = 0;
  2082. break;
  2083. }
  2084. case KVM_SET_CPUID: {
  2085. struct kvm_cpuid __user *cpuid_arg = argp;
  2086. struct kvm_cpuid cpuid;
  2087. r = -EFAULT;
  2088. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  2089. goto out;
  2090. r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
  2091. if (r)
  2092. goto out;
  2093. break;
  2094. }
  2095. case KVM_SET_CPUID2: {
  2096. struct kvm_cpuid2 __user *cpuid_arg = argp;
  2097. struct kvm_cpuid2 cpuid;
  2098. r = -EFAULT;
  2099. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  2100. goto out;
  2101. r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
  2102. cpuid_arg->entries);
  2103. if (r)
  2104. goto out;
  2105. break;
  2106. }
  2107. case KVM_GET_CPUID2: {
  2108. struct kvm_cpuid2 __user *cpuid_arg = argp;
  2109. struct kvm_cpuid2 cpuid;
  2110. r = -EFAULT;
  2111. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  2112. goto out;
  2113. r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
  2114. cpuid_arg->entries);
  2115. if (r)
  2116. goto out;
  2117. r = -EFAULT;
  2118. if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
  2119. goto out;
  2120. r = 0;
  2121. break;
  2122. }
  2123. case KVM_GET_MSRS:
  2124. r = msr_io(vcpu, argp, kvm_get_msr, 1);
  2125. break;
  2126. case KVM_SET_MSRS:
  2127. r = msr_io(vcpu, argp, do_set_msr, 0);
  2128. break;
  2129. case KVM_TPR_ACCESS_REPORTING: {
  2130. struct kvm_tpr_access_ctl tac;
  2131. r = -EFAULT;
  2132. if (copy_from_user(&tac, argp, sizeof tac))
  2133. goto out;
  2134. r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
  2135. if (r)
  2136. goto out;
  2137. r = -EFAULT;
  2138. if (copy_to_user(argp, &tac, sizeof tac))
  2139. goto out;
  2140. r = 0;
  2141. break;
  2142. };
  2143. case KVM_SET_VAPIC_ADDR: {
  2144. struct kvm_vapic_addr va;
  2145. r = -EINVAL;
  2146. if (!irqchip_in_kernel(vcpu->kvm))
  2147. goto out;
  2148. r = -EFAULT;
  2149. if (copy_from_user(&va, argp, sizeof va))
  2150. goto out;
  2151. r = 0;
  2152. kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
  2153. break;
  2154. }
  2155. case KVM_X86_SETUP_MCE: {
  2156. u64 mcg_cap;
  2157. r = -EFAULT;
  2158. if (copy_from_user(&mcg_cap, argp, sizeof mcg_cap))
  2159. goto out;
  2160. r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
  2161. break;
  2162. }
  2163. case KVM_X86_SET_MCE: {
  2164. struct kvm_x86_mce mce;
  2165. r = -EFAULT;
  2166. if (copy_from_user(&mce, argp, sizeof mce))
  2167. goto out;
  2168. r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
  2169. break;
  2170. }
  2171. case KVM_GET_VCPU_EVENTS: {
  2172. struct kvm_vcpu_events events;
  2173. kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
  2174. r = -EFAULT;
  2175. if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
  2176. break;
  2177. r = 0;
  2178. break;
  2179. }
  2180. case KVM_SET_VCPU_EVENTS: {
  2181. struct kvm_vcpu_events events;
  2182. r = -EFAULT;
  2183. if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
  2184. break;
  2185. r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
  2186. break;
  2187. }
  2188. case KVM_GET_DEBUGREGS: {
  2189. struct kvm_debugregs dbgregs;
  2190. kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
  2191. r = -EFAULT;
  2192. if (copy_to_user(argp, &dbgregs,
  2193. sizeof(struct kvm_debugregs)))
  2194. break;
  2195. r = 0;
  2196. break;
  2197. }
  2198. case KVM_SET_DEBUGREGS: {
  2199. struct kvm_debugregs dbgregs;
  2200. r = -EFAULT;
  2201. if (copy_from_user(&dbgregs, argp,
  2202. sizeof(struct kvm_debugregs)))
  2203. break;
  2204. r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
  2205. break;
  2206. }
  2207. default:
  2208. r = -EINVAL;
  2209. }
  2210. out:
  2211. kfree(lapic);
  2212. return r;
  2213. }
  2214. static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
  2215. {
  2216. int ret;
  2217. if (addr > (unsigned int)(-3 * PAGE_SIZE))
  2218. return -1;
  2219. ret = kvm_x86_ops->set_tss_addr(kvm, addr);
  2220. return ret;
  2221. }
  2222. static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
  2223. u64 ident_addr)
  2224. {
  2225. kvm->arch.ept_identity_map_addr = ident_addr;
  2226. return 0;
  2227. }
  2228. static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
  2229. u32 kvm_nr_mmu_pages)
  2230. {
  2231. if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
  2232. return -EINVAL;
  2233. mutex_lock(&kvm->slots_lock);
  2234. spin_lock(&kvm->mmu_lock);
  2235. kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
  2236. kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
  2237. spin_unlock(&kvm->mmu_lock);
  2238. mutex_unlock(&kvm->slots_lock);
  2239. return 0;
  2240. }
  2241. static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
  2242. {
  2243. return kvm->arch.n_alloc_mmu_pages;
  2244. }
  2245. gfn_t unalias_gfn_instantiation(struct kvm *kvm, gfn_t gfn)
  2246. {
  2247. int i;
  2248. struct kvm_mem_alias *alias;
  2249. struct kvm_mem_aliases *aliases;
  2250. aliases = kvm_aliases(kvm);
  2251. for (i = 0; i < aliases->naliases; ++i) {
  2252. alias = &aliases->aliases[i];
  2253. if (alias->flags & KVM_ALIAS_INVALID)
  2254. continue;
  2255. if (gfn >= alias->base_gfn
  2256. && gfn < alias->base_gfn + alias->npages)
  2257. return alias->target_gfn + gfn - alias->base_gfn;
  2258. }
  2259. return gfn;
  2260. }
  2261. gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
  2262. {
  2263. int i;
  2264. struct kvm_mem_alias *alias;
  2265. struct kvm_mem_aliases *aliases;
  2266. aliases = kvm_aliases(kvm);
  2267. for (i = 0; i < aliases->naliases; ++i) {
  2268. alias = &aliases->aliases[i];
  2269. if (gfn >= alias->base_gfn
  2270. && gfn < alias->base_gfn + alias->npages)
  2271. return alias->target_gfn + gfn - alias->base_gfn;
  2272. }
  2273. return gfn;
  2274. }
  2275. /*
  2276. * Set a new alias region. Aliases map a portion of physical memory into
  2277. * another portion. This is useful for memory windows, for example the PC
  2278. * VGA region.
  2279. */
  2280. static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
  2281. struct kvm_memory_alias *alias)
  2282. {
  2283. int r, n;
  2284. struct kvm_mem_alias *p;
  2285. struct kvm_mem_aliases *aliases, *old_aliases;
  2286. r = -EINVAL;
  2287. /* General sanity checks */
  2288. if (alias->memory_size & (PAGE_SIZE - 1))
  2289. goto out;
  2290. if (alias->guest_phys_addr & (PAGE_SIZE - 1))
  2291. goto out;
  2292. if (alias->slot >= KVM_ALIAS_SLOTS)
  2293. goto out;
  2294. if (alias->guest_phys_addr + alias->memory_size
  2295. < alias->guest_phys_addr)
  2296. goto out;
  2297. if (alias->target_phys_addr + alias->memory_size
  2298. < alias->target_phys_addr)
  2299. goto out;
  2300. r = -ENOMEM;
  2301. aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL);
  2302. if (!aliases)
  2303. goto out;
  2304. mutex_lock(&kvm->slots_lock);
  2305. /* invalidate any gfn reference in case of deletion/shrinking */
  2306. memcpy(aliases, kvm->arch.aliases, sizeof(struct kvm_mem_aliases));
  2307. aliases->aliases[alias->slot].flags |= KVM_ALIAS_INVALID;
  2308. old_aliases = kvm->arch.aliases;
  2309. rcu_assign_pointer(kvm->arch.aliases, aliases);
  2310. synchronize_srcu_expedited(&kvm->srcu);
  2311. kvm_mmu_zap_all(kvm);
  2312. kfree(old_aliases);
  2313. r = -ENOMEM;
  2314. aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL);
  2315. if (!aliases)
  2316. goto out_unlock;
  2317. memcpy(aliases, kvm->arch.aliases, sizeof(struct kvm_mem_aliases));
  2318. p = &aliases->aliases[alias->slot];
  2319. p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
  2320. p->npages = alias->memory_size >> PAGE_SHIFT;
  2321. p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
  2322. p->flags &= ~(KVM_ALIAS_INVALID);
  2323. for (n = KVM_ALIAS_SLOTS; n > 0; --n)
  2324. if (aliases->aliases[n - 1].npages)
  2325. break;
  2326. aliases->naliases = n;
  2327. old_aliases = kvm->arch.aliases;
  2328. rcu_assign_pointer(kvm->arch.aliases, aliases);
  2329. synchronize_srcu_expedited(&kvm->srcu);
  2330. kfree(old_aliases);
  2331. r = 0;
  2332. out_unlock:
  2333. mutex_unlock(&kvm->slots_lock);
  2334. out:
  2335. return r;
  2336. }
  2337. static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  2338. {
  2339. int r;
  2340. r = 0;
  2341. switch (chip->chip_id) {
  2342. case KVM_IRQCHIP_PIC_MASTER:
  2343. memcpy(&chip->chip.pic,
  2344. &pic_irqchip(kvm)->pics[0],
  2345. sizeof(struct kvm_pic_state));
  2346. break;
  2347. case KVM_IRQCHIP_PIC_SLAVE:
  2348. memcpy(&chip->chip.pic,
  2349. &pic_irqchip(kvm)->pics[1],
  2350. sizeof(struct kvm_pic_state));
  2351. break;
  2352. case KVM_IRQCHIP_IOAPIC:
  2353. r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
  2354. break;
  2355. default:
  2356. r = -EINVAL;
  2357. break;
  2358. }
  2359. return r;
  2360. }
  2361. static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  2362. {
  2363. int r;
  2364. r = 0;
  2365. switch (chip->chip_id) {
  2366. case KVM_IRQCHIP_PIC_MASTER:
  2367. raw_spin_lock(&pic_irqchip(kvm)->lock);
  2368. memcpy(&pic_irqchip(kvm)->pics[0],
  2369. &chip->chip.pic,
  2370. sizeof(struct kvm_pic_state));
  2371. raw_spin_unlock(&pic_irqchip(kvm)->lock);
  2372. break;
  2373. case KVM_IRQCHIP_PIC_SLAVE:
  2374. raw_spin_lock(&pic_irqchip(kvm)->lock);
  2375. memcpy(&pic_irqchip(kvm)->pics[1],
  2376. &chip->chip.pic,
  2377. sizeof(struct kvm_pic_state));
  2378. raw_spin_unlock(&pic_irqchip(kvm)->lock);
  2379. break;
  2380. case KVM_IRQCHIP_IOAPIC:
  2381. r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
  2382. break;
  2383. default:
  2384. r = -EINVAL;
  2385. break;
  2386. }
  2387. kvm_pic_update_irq(pic_irqchip(kvm));
  2388. return r;
  2389. }
  2390. static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
  2391. {
  2392. int r = 0;
  2393. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  2394. memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
  2395. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  2396. return r;
  2397. }
  2398. static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
  2399. {
  2400. int r = 0;
  2401. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  2402. memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
  2403. kvm_pit_load_count(kvm, 0, ps->channels[0].count, 0);
  2404. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  2405. return r;
  2406. }
  2407. static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
  2408. {
  2409. int r = 0;
  2410. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  2411. memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
  2412. sizeof(ps->channels));
  2413. ps->flags = kvm->arch.vpit->pit_state.flags;
  2414. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  2415. return r;
  2416. }
  2417. static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
  2418. {
  2419. int r = 0, start = 0;
  2420. u32 prev_legacy, cur_legacy;
  2421. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  2422. prev_legacy = kvm->arch.vpit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
  2423. cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
  2424. if (!prev_legacy && cur_legacy)
  2425. start = 1;
  2426. memcpy(&kvm->arch.vpit->pit_state.channels, &ps->channels,
  2427. sizeof(kvm->arch.vpit->pit_state.channels));
  2428. kvm->arch.vpit->pit_state.flags = ps->flags;
  2429. kvm_pit_load_count(kvm, 0, kvm->arch.vpit->pit_state.channels[0].count, start);
  2430. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  2431. return r;
  2432. }
  2433. static int kvm_vm_ioctl_reinject(struct kvm *kvm,
  2434. struct kvm_reinject_control *control)
  2435. {
  2436. if (!kvm->arch.vpit)
  2437. return -ENXIO;
  2438. mutex_lock(&kvm->arch.vpit->pit_state.lock);
  2439. kvm->arch.vpit->pit_state.pit_timer.reinject = control->pit_reinject;
  2440. mutex_unlock(&kvm->arch.vpit->pit_state.lock);
  2441. return 0;
  2442. }
  2443. /*
  2444. * Get (and clear) the dirty memory log for a memory slot.
  2445. */
  2446. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
  2447. struct kvm_dirty_log *log)
  2448. {
  2449. int r, i;
  2450. struct kvm_memory_slot *memslot;
  2451. unsigned long n;
  2452. unsigned long is_dirty = 0;
  2453. unsigned long *dirty_bitmap = NULL;
  2454. mutex_lock(&kvm->slots_lock);
  2455. r = -EINVAL;
  2456. if (log->slot >= KVM_MEMORY_SLOTS)
  2457. goto out;
  2458. memslot = &kvm->memslots->memslots[log->slot];
  2459. r = -ENOENT;
  2460. if (!memslot->dirty_bitmap)
  2461. goto out;
  2462. n = kvm_dirty_bitmap_bytes(memslot);
  2463. r = -ENOMEM;
  2464. dirty_bitmap = vmalloc(n);
  2465. if (!dirty_bitmap)
  2466. goto out;
  2467. memset(dirty_bitmap, 0, n);
  2468. for (i = 0; !is_dirty && i < n/sizeof(long); i++)
  2469. is_dirty = memslot->dirty_bitmap[i];
  2470. /* If nothing is dirty, don't bother messing with page tables. */
  2471. if (is_dirty) {
  2472. struct kvm_memslots *slots, *old_slots;
  2473. spin_lock(&kvm->mmu_lock);
  2474. kvm_mmu_slot_remove_write_access(kvm, log->slot);
  2475. spin_unlock(&kvm->mmu_lock);
  2476. slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
  2477. if (!slots)
  2478. goto out_free;
  2479. memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
  2480. slots->memslots[log->slot].dirty_bitmap = dirty_bitmap;
  2481. old_slots = kvm->memslots;
  2482. rcu_assign_pointer(kvm->memslots, slots);
  2483. synchronize_srcu_expedited(&kvm->srcu);
  2484. dirty_bitmap = old_slots->memslots[log->slot].dirty_bitmap;
  2485. kfree(old_slots);
  2486. }
  2487. r = 0;
  2488. if (copy_to_user(log->dirty_bitmap, dirty_bitmap, n))
  2489. r = -EFAULT;
  2490. out_free:
  2491. vfree(dirty_bitmap);
  2492. out:
  2493. mutex_unlock(&kvm->slots_lock);
  2494. return r;
  2495. }
  2496. long kvm_arch_vm_ioctl(struct file *filp,
  2497. unsigned int ioctl, unsigned long arg)
  2498. {
  2499. struct kvm *kvm = filp->private_data;
  2500. void __user *argp = (void __user *)arg;
  2501. int r = -ENOTTY;
  2502. /*
  2503. * This union makes it completely explicit to gcc-3.x
  2504. * that these two variables' stack usage should be
  2505. * combined, not added together.
  2506. */
  2507. union {
  2508. struct kvm_pit_state ps;
  2509. struct kvm_pit_state2 ps2;
  2510. struct kvm_memory_alias alias;
  2511. struct kvm_pit_config pit_config;
  2512. } u;
  2513. switch (ioctl) {
  2514. case KVM_SET_TSS_ADDR:
  2515. r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
  2516. if (r < 0)
  2517. goto out;
  2518. break;
  2519. case KVM_SET_IDENTITY_MAP_ADDR: {
  2520. u64 ident_addr;
  2521. r = -EFAULT;
  2522. if (copy_from_user(&ident_addr, argp, sizeof ident_addr))
  2523. goto out;
  2524. r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
  2525. if (r < 0)
  2526. goto out;
  2527. break;
  2528. }
  2529. case KVM_SET_MEMORY_REGION: {
  2530. struct kvm_memory_region kvm_mem;
  2531. struct kvm_userspace_memory_region kvm_userspace_mem;
  2532. r = -EFAULT;
  2533. if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
  2534. goto out;
  2535. kvm_userspace_mem.slot = kvm_mem.slot;
  2536. kvm_userspace_mem.flags = kvm_mem.flags;
  2537. kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
  2538. kvm_userspace_mem.memory_size = kvm_mem.memory_size;
  2539. r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
  2540. if (r)
  2541. goto out;
  2542. break;
  2543. }
  2544. case KVM_SET_NR_MMU_PAGES:
  2545. r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
  2546. if (r)
  2547. goto out;
  2548. break;
  2549. case KVM_GET_NR_MMU_PAGES:
  2550. r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
  2551. break;
  2552. case KVM_SET_MEMORY_ALIAS:
  2553. r = -EFAULT;
  2554. if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias)))
  2555. goto out;
  2556. r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias);
  2557. if (r)
  2558. goto out;
  2559. break;
  2560. case KVM_CREATE_IRQCHIP: {
  2561. struct kvm_pic *vpic;
  2562. mutex_lock(&kvm->lock);
  2563. r = -EEXIST;
  2564. if (kvm->arch.vpic)
  2565. goto create_irqchip_unlock;
  2566. r = -ENOMEM;
  2567. vpic = kvm_create_pic(kvm);
  2568. if (vpic) {
  2569. r = kvm_ioapic_init(kvm);
  2570. if (r) {
  2571. kvm_io_bus_unregister_dev(kvm, KVM_PIO_BUS,
  2572. &vpic->dev);
  2573. kfree(vpic);
  2574. goto create_irqchip_unlock;
  2575. }
  2576. } else
  2577. goto create_irqchip_unlock;
  2578. smp_wmb();
  2579. kvm->arch.vpic = vpic;
  2580. smp_wmb();
  2581. r = kvm_setup_default_irq_routing(kvm);
  2582. if (r) {
  2583. mutex_lock(&kvm->irq_lock);
  2584. kvm_ioapic_destroy(kvm);
  2585. kvm_destroy_pic(kvm);
  2586. mutex_unlock(&kvm->irq_lock);
  2587. }
  2588. create_irqchip_unlock:
  2589. mutex_unlock(&kvm->lock);
  2590. break;
  2591. }
  2592. case KVM_CREATE_PIT:
  2593. u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
  2594. goto create_pit;
  2595. case KVM_CREATE_PIT2:
  2596. r = -EFAULT;
  2597. if (copy_from_user(&u.pit_config, argp,
  2598. sizeof(struct kvm_pit_config)))
  2599. goto out;
  2600. create_pit:
  2601. mutex_lock(&kvm->slots_lock);
  2602. r = -EEXIST;
  2603. if (kvm->arch.vpit)
  2604. goto create_pit_unlock;
  2605. r = -ENOMEM;
  2606. kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
  2607. if (kvm->arch.vpit)
  2608. r = 0;
  2609. create_pit_unlock:
  2610. mutex_unlock(&kvm->slots_lock);
  2611. break;
  2612. case KVM_IRQ_LINE_STATUS:
  2613. case KVM_IRQ_LINE: {
  2614. struct kvm_irq_level irq_event;
  2615. r = -EFAULT;
  2616. if (copy_from_user(&irq_event, argp, sizeof irq_event))
  2617. goto out;
  2618. r = -ENXIO;
  2619. if (irqchip_in_kernel(kvm)) {
  2620. __s32 status;
  2621. status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
  2622. irq_event.irq, irq_event.level);
  2623. if (ioctl == KVM_IRQ_LINE_STATUS) {
  2624. r = -EFAULT;
  2625. irq_event.status = status;
  2626. if (copy_to_user(argp, &irq_event,
  2627. sizeof irq_event))
  2628. goto out;
  2629. }
  2630. r = 0;
  2631. }
  2632. break;
  2633. }
  2634. case KVM_GET_IRQCHIP: {
  2635. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  2636. struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
  2637. r = -ENOMEM;
  2638. if (!chip)
  2639. goto out;
  2640. r = -EFAULT;
  2641. if (copy_from_user(chip, argp, sizeof *chip))
  2642. goto get_irqchip_out;
  2643. r = -ENXIO;
  2644. if (!irqchip_in_kernel(kvm))
  2645. goto get_irqchip_out;
  2646. r = kvm_vm_ioctl_get_irqchip(kvm, chip);
  2647. if (r)
  2648. goto get_irqchip_out;
  2649. r = -EFAULT;
  2650. if (copy_to_user(argp, chip, sizeof *chip))
  2651. goto get_irqchip_out;
  2652. r = 0;
  2653. get_irqchip_out:
  2654. kfree(chip);
  2655. if (r)
  2656. goto out;
  2657. break;
  2658. }
  2659. case KVM_SET_IRQCHIP: {
  2660. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  2661. struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
  2662. r = -ENOMEM;
  2663. if (!chip)
  2664. goto out;
  2665. r = -EFAULT;
  2666. if (copy_from_user(chip, argp, sizeof *chip))
  2667. goto set_irqchip_out;
  2668. r = -ENXIO;
  2669. if (!irqchip_in_kernel(kvm))
  2670. goto set_irqchip_out;
  2671. r = kvm_vm_ioctl_set_irqchip(kvm, chip);
  2672. if (r)
  2673. goto set_irqchip_out;
  2674. r = 0;
  2675. set_irqchip_out:
  2676. kfree(chip);
  2677. if (r)
  2678. goto out;
  2679. break;
  2680. }
  2681. case KVM_GET_PIT: {
  2682. r = -EFAULT;
  2683. if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
  2684. goto out;
  2685. r = -ENXIO;
  2686. if (!kvm->arch.vpit)
  2687. goto out;
  2688. r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
  2689. if (r)
  2690. goto out;
  2691. r = -EFAULT;
  2692. if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
  2693. goto out;
  2694. r = 0;
  2695. break;
  2696. }
  2697. case KVM_SET_PIT: {
  2698. r = -EFAULT;
  2699. if (copy_from_user(&u.ps, argp, sizeof u.ps))
  2700. goto out;
  2701. r = -ENXIO;
  2702. if (!kvm->arch.vpit)
  2703. goto out;
  2704. r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
  2705. if (r)
  2706. goto out;
  2707. r = 0;
  2708. break;
  2709. }
  2710. case KVM_GET_PIT2: {
  2711. r = -ENXIO;
  2712. if (!kvm->arch.vpit)
  2713. goto out;
  2714. r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
  2715. if (r)
  2716. goto out;
  2717. r = -EFAULT;
  2718. if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
  2719. goto out;
  2720. r = 0;
  2721. break;
  2722. }
  2723. case KVM_SET_PIT2: {
  2724. r = -EFAULT;
  2725. if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
  2726. goto out;
  2727. r = -ENXIO;
  2728. if (!kvm->arch.vpit)
  2729. goto out;
  2730. r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
  2731. if (r)
  2732. goto out;
  2733. r = 0;
  2734. break;
  2735. }
  2736. case KVM_REINJECT_CONTROL: {
  2737. struct kvm_reinject_control control;
  2738. r = -EFAULT;
  2739. if (copy_from_user(&control, argp, sizeof(control)))
  2740. goto out;
  2741. r = kvm_vm_ioctl_reinject(kvm, &control);
  2742. if (r)
  2743. goto out;
  2744. r = 0;
  2745. break;
  2746. }
  2747. case KVM_XEN_HVM_CONFIG: {
  2748. r = -EFAULT;
  2749. if (copy_from_user(&kvm->arch.xen_hvm_config, argp,
  2750. sizeof(struct kvm_xen_hvm_config)))
  2751. goto out;
  2752. r = -EINVAL;
  2753. if (kvm->arch.xen_hvm_config.flags)
  2754. goto out;
  2755. r = 0;
  2756. break;
  2757. }
  2758. case KVM_SET_CLOCK: {
  2759. struct timespec now;
  2760. struct kvm_clock_data user_ns;
  2761. u64 now_ns;
  2762. s64 delta;
  2763. r = -EFAULT;
  2764. if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
  2765. goto out;
  2766. r = -EINVAL;
  2767. if (user_ns.flags)
  2768. goto out;
  2769. r = 0;
  2770. ktime_get_ts(&now);
  2771. now_ns = timespec_to_ns(&now);
  2772. delta = user_ns.clock - now_ns;
  2773. kvm->arch.kvmclock_offset = delta;
  2774. break;
  2775. }
  2776. case KVM_GET_CLOCK: {
  2777. struct timespec now;
  2778. struct kvm_clock_data user_ns;
  2779. u64 now_ns;
  2780. ktime_get_ts(&now);
  2781. now_ns = timespec_to_ns(&now);
  2782. user_ns.clock = kvm->arch.kvmclock_offset + now_ns;
  2783. user_ns.flags = 0;
  2784. r = -EFAULT;
  2785. if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
  2786. goto out;
  2787. r = 0;
  2788. break;
  2789. }
  2790. default:
  2791. ;
  2792. }
  2793. out:
  2794. return r;
  2795. }
  2796. static void kvm_init_msr_list(void)
  2797. {
  2798. u32 dummy[2];
  2799. unsigned i, j;
  2800. /* skip the first msrs in the list. KVM-specific */
  2801. for (i = j = KVM_SAVE_MSRS_BEGIN; i < ARRAY_SIZE(msrs_to_save); i++) {
  2802. if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
  2803. continue;
  2804. if (j < i)
  2805. msrs_to_save[j] = msrs_to_save[i];
  2806. j++;
  2807. }
  2808. num_msrs_to_save = j;
  2809. }
  2810. static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
  2811. const void *v)
  2812. {
  2813. if (vcpu->arch.apic &&
  2814. !kvm_iodevice_write(&vcpu->arch.apic->dev, addr, len, v))
  2815. return 0;
  2816. return kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, addr, len, v);
  2817. }
  2818. static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
  2819. {
  2820. if (vcpu->arch.apic &&
  2821. !kvm_iodevice_read(&vcpu->arch.apic->dev, addr, len, v))
  2822. return 0;
  2823. return kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, addr, len, v);
  2824. }
  2825. static void kvm_set_segment(struct kvm_vcpu *vcpu,
  2826. struct kvm_segment *var, int seg)
  2827. {
  2828. kvm_x86_ops->set_segment(vcpu, var, seg);
  2829. }
  2830. void kvm_get_segment(struct kvm_vcpu *vcpu,
  2831. struct kvm_segment *var, int seg)
  2832. {
  2833. kvm_x86_ops->get_segment(vcpu, var, seg);
  2834. }
  2835. gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
  2836. {
  2837. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  2838. return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
  2839. }
  2840. gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
  2841. {
  2842. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  2843. access |= PFERR_FETCH_MASK;
  2844. return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
  2845. }
  2846. gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
  2847. {
  2848. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  2849. access |= PFERR_WRITE_MASK;
  2850. return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, access, error);
  2851. }
  2852. /* uses this to access any guest's mapped memory without checking CPL */
  2853. gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva, u32 *error)
  2854. {
  2855. return vcpu->arch.mmu.gva_to_gpa(vcpu, gva, 0, error);
  2856. }
  2857. static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
  2858. struct kvm_vcpu *vcpu, u32 access,
  2859. u32 *error)
  2860. {
  2861. void *data = val;
  2862. int r = X86EMUL_CONTINUE;
  2863. while (bytes) {
  2864. gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr, access, error);
  2865. unsigned offset = addr & (PAGE_SIZE-1);
  2866. unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
  2867. int ret;
  2868. if (gpa == UNMAPPED_GVA) {
  2869. r = X86EMUL_PROPAGATE_FAULT;
  2870. goto out;
  2871. }
  2872. ret = kvm_read_guest(vcpu->kvm, gpa, data, toread);
  2873. if (ret < 0) {
  2874. r = X86EMUL_UNHANDLEABLE;
  2875. goto out;
  2876. }
  2877. bytes -= toread;
  2878. data += toread;
  2879. addr += toread;
  2880. }
  2881. out:
  2882. return r;
  2883. }
  2884. /* used for instruction fetching */
  2885. static int kvm_fetch_guest_virt(gva_t addr, void *val, unsigned int bytes,
  2886. struct kvm_vcpu *vcpu, u32 *error)
  2887. {
  2888. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  2889. return kvm_read_guest_virt_helper(addr, val, bytes, vcpu,
  2890. access | PFERR_FETCH_MASK, error);
  2891. }
  2892. static int kvm_read_guest_virt(gva_t addr, void *val, unsigned int bytes,
  2893. struct kvm_vcpu *vcpu, u32 *error)
  2894. {
  2895. u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
  2896. return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
  2897. error);
  2898. }
  2899. static int kvm_read_guest_virt_system(gva_t addr, void *val, unsigned int bytes,
  2900. struct kvm_vcpu *vcpu, u32 *error)
  2901. {
  2902. return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, 0, error);
  2903. }
  2904. static int kvm_write_guest_virt_system(gva_t addr, void *val,
  2905. unsigned int bytes,
  2906. struct kvm_vcpu *vcpu,
  2907. u32 *error)
  2908. {
  2909. void *data = val;
  2910. int r = X86EMUL_CONTINUE;
  2911. while (bytes) {
  2912. gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr,
  2913. PFERR_WRITE_MASK, error);
  2914. unsigned offset = addr & (PAGE_SIZE-1);
  2915. unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
  2916. int ret;
  2917. if (gpa == UNMAPPED_GVA) {
  2918. r = X86EMUL_PROPAGATE_FAULT;
  2919. goto out;
  2920. }
  2921. ret = kvm_write_guest(vcpu->kvm, gpa, data, towrite);
  2922. if (ret < 0) {
  2923. r = X86EMUL_UNHANDLEABLE;
  2924. goto out;
  2925. }
  2926. bytes -= towrite;
  2927. data += towrite;
  2928. addr += towrite;
  2929. }
  2930. out:
  2931. return r;
  2932. }
  2933. static int emulator_read_emulated(unsigned long addr,
  2934. void *val,
  2935. unsigned int bytes,
  2936. struct kvm_vcpu *vcpu)
  2937. {
  2938. gpa_t gpa;
  2939. u32 error_code;
  2940. if (vcpu->mmio_read_completed) {
  2941. memcpy(val, vcpu->mmio_data, bytes);
  2942. trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
  2943. vcpu->mmio_phys_addr, *(u64 *)val);
  2944. vcpu->mmio_read_completed = 0;
  2945. return X86EMUL_CONTINUE;
  2946. }
  2947. gpa = kvm_mmu_gva_to_gpa_read(vcpu, addr, &error_code);
  2948. if (gpa == UNMAPPED_GVA) {
  2949. kvm_inject_page_fault(vcpu, addr, error_code);
  2950. return X86EMUL_PROPAGATE_FAULT;
  2951. }
  2952. /* For APIC access vmexit */
  2953. if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  2954. goto mmio;
  2955. if (kvm_read_guest_virt(addr, val, bytes, vcpu, NULL)
  2956. == X86EMUL_CONTINUE)
  2957. return X86EMUL_CONTINUE;
  2958. mmio:
  2959. /*
  2960. * Is this MMIO handled locally?
  2961. */
  2962. if (!vcpu_mmio_read(vcpu, gpa, bytes, val)) {
  2963. trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, gpa, *(u64 *)val);
  2964. return X86EMUL_CONTINUE;
  2965. }
  2966. trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
  2967. vcpu->mmio_needed = 1;
  2968. vcpu->mmio_phys_addr = gpa;
  2969. vcpu->mmio_size = bytes;
  2970. vcpu->mmio_is_write = 0;
  2971. return X86EMUL_UNHANDLEABLE;
  2972. }
  2973. int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
  2974. const void *val, int bytes)
  2975. {
  2976. int ret;
  2977. ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
  2978. if (ret < 0)
  2979. return 0;
  2980. kvm_mmu_pte_write(vcpu, gpa, val, bytes, 1);
  2981. return 1;
  2982. }
  2983. static int emulator_write_emulated_onepage(unsigned long addr,
  2984. const void *val,
  2985. unsigned int bytes,
  2986. struct kvm_vcpu *vcpu)
  2987. {
  2988. gpa_t gpa;
  2989. u32 error_code;
  2990. gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, &error_code);
  2991. if (gpa == UNMAPPED_GVA) {
  2992. kvm_inject_page_fault(vcpu, addr, error_code);
  2993. return X86EMUL_PROPAGATE_FAULT;
  2994. }
  2995. /* For APIC access vmexit */
  2996. if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  2997. goto mmio;
  2998. if (emulator_write_phys(vcpu, gpa, val, bytes))
  2999. return X86EMUL_CONTINUE;
  3000. mmio:
  3001. trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
  3002. /*
  3003. * Is this MMIO handled locally?
  3004. */
  3005. if (!vcpu_mmio_write(vcpu, gpa, bytes, val))
  3006. return X86EMUL_CONTINUE;
  3007. vcpu->mmio_needed = 1;
  3008. vcpu->mmio_phys_addr = gpa;
  3009. vcpu->mmio_size = bytes;
  3010. vcpu->mmio_is_write = 1;
  3011. memcpy(vcpu->mmio_data, val, bytes);
  3012. return X86EMUL_CONTINUE;
  3013. }
  3014. int emulator_write_emulated(unsigned long addr,
  3015. const void *val,
  3016. unsigned int bytes,
  3017. struct kvm_vcpu *vcpu)
  3018. {
  3019. /* Crossing a page boundary? */
  3020. if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
  3021. int rc, now;
  3022. now = -addr & ~PAGE_MASK;
  3023. rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
  3024. if (rc != X86EMUL_CONTINUE)
  3025. return rc;
  3026. addr += now;
  3027. val += now;
  3028. bytes -= now;
  3029. }
  3030. return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
  3031. }
  3032. EXPORT_SYMBOL_GPL(emulator_write_emulated);
  3033. #define CMPXCHG_TYPE(t, ptr, old, new) \
  3034. (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
  3035. #ifdef CONFIG_X86_64
  3036. # define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
  3037. #else
  3038. # define CMPXCHG64(ptr, old, new) \
  3039. (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
  3040. #endif
  3041. static int emulator_cmpxchg_emulated(unsigned long addr,
  3042. const void *old,
  3043. const void *new,
  3044. unsigned int bytes,
  3045. struct kvm_vcpu *vcpu)
  3046. {
  3047. gpa_t gpa;
  3048. struct page *page;
  3049. char *kaddr;
  3050. bool exchanged;
  3051. /* guests cmpxchg8b have to be emulated atomically */
  3052. if (bytes > 8 || (bytes & (bytes - 1)))
  3053. goto emul_write;
  3054. gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
  3055. if (gpa == UNMAPPED_GVA ||
  3056. (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  3057. goto emul_write;
  3058. if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
  3059. goto emul_write;
  3060. page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
  3061. kaddr = kmap_atomic(page, KM_USER0);
  3062. kaddr += offset_in_page(gpa);
  3063. switch (bytes) {
  3064. case 1:
  3065. exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
  3066. break;
  3067. case 2:
  3068. exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
  3069. break;
  3070. case 4:
  3071. exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
  3072. break;
  3073. case 8:
  3074. exchanged = CMPXCHG64(kaddr, old, new);
  3075. break;
  3076. default:
  3077. BUG();
  3078. }
  3079. kunmap_atomic(kaddr, KM_USER0);
  3080. kvm_release_page_dirty(page);
  3081. if (!exchanged)
  3082. return X86EMUL_CMPXCHG_FAILED;
  3083. kvm_mmu_pte_write(vcpu, gpa, new, bytes, 1);
  3084. return X86EMUL_CONTINUE;
  3085. emul_write:
  3086. printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
  3087. return emulator_write_emulated(addr, new, bytes, vcpu);
  3088. }
  3089. static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
  3090. {
  3091. /* TODO: String I/O for in kernel device */
  3092. int r;
  3093. if (vcpu->arch.pio.in)
  3094. r = kvm_io_bus_read(vcpu->kvm, KVM_PIO_BUS, vcpu->arch.pio.port,
  3095. vcpu->arch.pio.size, pd);
  3096. else
  3097. r = kvm_io_bus_write(vcpu->kvm, KVM_PIO_BUS,
  3098. vcpu->arch.pio.port, vcpu->arch.pio.size,
  3099. pd);
  3100. return r;
  3101. }
  3102. static int emulator_pio_in_emulated(int size, unsigned short port, void *val,
  3103. unsigned int count, struct kvm_vcpu *vcpu)
  3104. {
  3105. if (vcpu->arch.pio.count)
  3106. goto data_avail;
  3107. trace_kvm_pio(1, port, size, 1);
  3108. vcpu->arch.pio.port = port;
  3109. vcpu->arch.pio.in = 1;
  3110. vcpu->arch.pio.count = count;
  3111. vcpu->arch.pio.size = size;
  3112. if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
  3113. data_avail:
  3114. memcpy(val, vcpu->arch.pio_data, size * count);
  3115. vcpu->arch.pio.count = 0;
  3116. return 1;
  3117. }
  3118. vcpu->run->exit_reason = KVM_EXIT_IO;
  3119. vcpu->run->io.direction = KVM_EXIT_IO_IN;
  3120. vcpu->run->io.size = size;
  3121. vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
  3122. vcpu->run->io.count = count;
  3123. vcpu->run->io.port = port;
  3124. return 0;
  3125. }
  3126. static int emulator_pio_out_emulated(int size, unsigned short port,
  3127. const void *val, unsigned int count,
  3128. struct kvm_vcpu *vcpu)
  3129. {
  3130. trace_kvm_pio(0, port, size, 1);
  3131. vcpu->arch.pio.port = port;
  3132. vcpu->arch.pio.in = 0;
  3133. vcpu->arch.pio.count = count;
  3134. vcpu->arch.pio.size = size;
  3135. memcpy(vcpu->arch.pio_data, val, size * count);
  3136. if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
  3137. vcpu->arch.pio.count = 0;
  3138. return 1;
  3139. }
  3140. vcpu->run->exit_reason = KVM_EXIT_IO;
  3141. vcpu->run->io.direction = KVM_EXIT_IO_OUT;
  3142. vcpu->run->io.size = size;
  3143. vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
  3144. vcpu->run->io.count = count;
  3145. vcpu->run->io.port = port;
  3146. return 0;
  3147. }
  3148. static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
  3149. {
  3150. return kvm_x86_ops->get_segment_base(vcpu, seg);
  3151. }
  3152. int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
  3153. {
  3154. kvm_mmu_invlpg(vcpu, address);
  3155. return X86EMUL_CONTINUE;
  3156. }
  3157. int emulate_clts(struct kvm_vcpu *vcpu)
  3158. {
  3159. kvm_x86_ops->set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
  3160. kvm_x86_ops->fpu_activate(vcpu);
  3161. return X86EMUL_CONTINUE;
  3162. }
  3163. int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
  3164. {
  3165. return kvm_get_dr(ctxt->vcpu, dr, dest);
  3166. }
  3167. int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
  3168. {
  3169. unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
  3170. return kvm_set_dr(ctxt->vcpu, dr, value & mask);
  3171. }
  3172. void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
  3173. {
  3174. u8 opcodes[4];
  3175. unsigned long rip = kvm_rip_read(vcpu);
  3176. unsigned long rip_linear;
  3177. if (!printk_ratelimit())
  3178. return;
  3179. rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
  3180. kvm_read_guest_virt(rip_linear, (void *)opcodes, 4, vcpu, NULL);
  3181. printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
  3182. context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
  3183. }
  3184. EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
  3185. static u64 mk_cr_64(u64 curr_cr, u32 new_val)
  3186. {
  3187. return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
  3188. }
  3189. static unsigned long emulator_get_cr(int cr, struct kvm_vcpu *vcpu)
  3190. {
  3191. unsigned long value;
  3192. switch (cr) {
  3193. case 0:
  3194. value = kvm_read_cr0(vcpu);
  3195. break;
  3196. case 2:
  3197. value = vcpu->arch.cr2;
  3198. break;
  3199. case 3:
  3200. value = vcpu->arch.cr3;
  3201. break;
  3202. case 4:
  3203. value = kvm_read_cr4(vcpu);
  3204. break;
  3205. case 8:
  3206. value = kvm_get_cr8(vcpu);
  3207. break;
  3208. default:
  3209. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
  3210. return 0;
  3211. }
  3212. return value;
  3213. }
  3214. static void emulator_set_cr(int cr, unsigned long val, struct kvm_vcpu *vcpu)
  3215. {
  3216. switch (cr) {
  3217. case 0:
  3218. kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
  3219. break;
  3220. case 2:
  3221. vcpu->arch.cr2 = val;
  3222. break;
  3223. case 3:
  3224. kvm_set_cr3(vcpu, val);
  3225. break;
  3226. case 4:
  3227. kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
  3228. break;
  3229. case 8:
  3230. kvm_set_cr8(vcpu, val & 0xfUL);
  3231. break;
  3232. default:
  3233. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
  3234. }
  3235. }
  3236. static int emulator_get_cpl(struct kvm_vcpu *vcpu)
  3237. {
  3238. return kvm_x86_ops->get_cpl(vcpu);
  3239. }
  3240. static void emulator_get_gdt(struct desc_ptr *dt, struct kvm_vcpu *vcpu)
  3241. {
  3242. kvm_x86_ops->get_gdt(vcpu, dt);
  3243. }
  3244. static bool emulator_get_cached_descriptor(struct desc_struct *desc, int seg,
  3245. struct kvm_vcpu *vcpu)
  3246. {
  3247. struct kvm_segment var;
  3248. kvm_get_segment(vcpu, &var, seg);
  3249. if (var.unusable)
  3250. return false;
  3251. if (var.g)
  3252. var.limit >>= 12;
  3253. set_desc_limit(desc, var.limit);
  3254. set_desc_base(desc, (unsigned long)var.base);
  3255. desc->type = var.type;
  3256. desc->s = var.s;
  3257. desc->dpl = var.dpl;
  3258. desc->p = var.present;
  3259. desc->avl = var.avl;
  3260. desc->l = var.l;
  3261. desc->d = var.db;
  3262. desc->g = var.g;
  3263. return true;
  3264. }
  3265. static void emulator_set_cached_descriptor(struct desc_struct *desc, int seg,
  3266. struct kvm_vcpu *vcpu)
  3267. {
  3268. struct kvm_segment var;
  3269. /* needed to preserve selector */
  3270. kvm_get_segment(vcpu, &var, seg);
  3271. var.base = get_desc_base(desc);
  3272. var.limit = get_desc_limit(desc);
  3273. if (desc->g)
  3274. var.limit = (var.limit << 12) | 0xfff;
  3275. var.type = desc->type;
  3276. var.present = desc->p;
  3277. var.dpl = desc->dpl;
  3278. var.db = desc->d;
  3279. var.s = desc->s;
  3280. var.l = desc->l;
  3281. var.g = desc->g;
  3282. var.avl = desc->avl;
  3283. var.present = desc->p;
  3284. var.unusable = !var.present;
  3285. var.padding = 0;
  3286. kvm_set_segment(vcpu, &var, seg);
  3287. return;
  3288. }
  3289. static u16 emulator_get_segment_selector(int seg, struct kvm_vcpu *vcpu)
  3290. {
  3291. struct kvm_segment kvm_seg;
  3292. kvm_get_segment(vcpu, &kvm_seg, seg);
  3293. return kvm_seg.selector;
  3294. }
  3295. static void emulator_set_segment_selector(u16 sel, int seg,
  3296. struct kvm_vcpu *vcpu)
  3297. {
  3298. struct kvm_segment kvm_seg;
  3299. kvm_get_segment(vcpu, &kvm_seg, seg);
  3300. kvm_seg.selector = sel;
  3301. kvm_set_segment(vcpu, &kvm_seg, seg);
  3302. }
  3303. static void emulator_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  3304. {
  3305. kvm_x86_ops->set_rflags(vcpu, rflags);
  3306. }
  3307. static struct x86_emulate_ops emulate_ops = {
  3308. .read_std = kvm_read_guest_virt_system,
  3309. .write_std = kvm_write_guest_virt_system,
  3310. .fetch = kvm_fetch_guest_virt,
  3311. .read_emulated = emulator_read_emulated,
  3312. .write_emulated = emulator_write_emulated,
  3313. .cmpxchg_emulated = emulator_cmpxchg_emulated,
  3314. .pio_in_emulated = emulator_pio_in_emulated,
  3315. .pio_out_emulated = emulator_pio_out_emulated,
  3316. .get_cached_descriptor = emulator_get_cached_descriptor,
  3317. .set_cached_descriptor = emulator_set_cached_descriptor,
  3318. .get_segment_selector = emulator_get_segment_selector,
  3319. .set_segment_selector = emulator_set_segment_selector,
  3320. .get_gdt = emulator_get_gdt,
  3321. .get_cr = emulator_get_cr,
  3322. .set_cr = emulator_set_cr,
  3323. .cpl = emulator_get_cpl,
  3324. .set_rflags = emulator_set_rflags,
  3325. };
  3326. static void cache_all_regs(struct kvm_vcpu *vcpu)
  3327. {
  3328. kvm_register_read(vcpu, VCPU_REGS_RAX);
  3329. kvm_register_read(vcpu, VCPU_REGS_RSP);
  3330. kvm_register_read(vcpu, VCPU_REGS_RIP);
  3331. vcpu->arch.regs_dirty = ~0;
  3332. }
  3333. int emulate_instruction(struct kvm_vcpu *vcpu,
  3334. unsigned long cr2,
  3335. u16 error_code,
  3336. int emulation_type)
  3337. {
  3338. int r, shadow_mask;
  3339. struct decode_cache *c;
  3340. struct kvm_run *run = vcpu->run;
  3341. kvm_clear_exception_queue(vcpu);
  3342. vcpu->arch.mmio_fault_cr2 = cr2;
  3343. /*
  3344. * TODO: fix emulate.c to use guest_read/write_register
  3345. * instead of direct ->regs accesses, can save hundred cycles
  3346. * on Intel for instructions that don't read/change RSP, for
  3347. * for example.
  3348. */
  3349. cache_all_regs(vcpu);
  3350. vcpu->mmio_is_write = 0;
  3351. if (!(emulation_type & EMULTYPE_NO_DECODE)) {
  3352. int cs_db, cs_l;
  3353. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  3354. vcpu->arch.emulate_ctxt.vcpu = vcpu;
  3355. vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
  3356. vcpu->arch.emulate_ctxt.eip = kvm_rip_read(vcpu);
  3357. vcpu->arch.emulate_ctxt.mode =
  3358. (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
  3359. (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
  3360. ? X86EMUL_MODE_VM86 : cs_l
  3361. ? X86EMUL_MODE_PROT64 : cs_db
  3362. ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
  3363. r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
  3364. trace_kvm_emulate_insn_start(vcpu);
  3365. /* Only allow emulation of specific instructions on #UD
  3366. * (namely VMMCALL, sysenter, sysexit, syscall)*/
  3367. c = &vcpu->arch.emulate_ctxt.decode;
  3368. if (emulation_type & EMULTYPE_TRAP_UD) {
  3369. if (!c->twobyte)
  3370. return EMULATE_FAIL;
  3371. switch (c->b) {
  3372. case 0x01: /* VMMCALL */
  3373. if (c->modrm_mod != 3 || c->modrm_rm != 1)
  3374. return EMULATE_FAIL;
  3375. break;
  3376. case 0x34: /* sysenter */
  3377. case 0x35: /* sysexit */
  3378. if (c->modrm_mod != 0 || c->modrm_rm != 0)
  3379. return EMULATE_FAIL;
  3380. break;
  3381. case 0x05: /* syscall */
  3382. if (c->modrm_mod != 0 || c->modrm_rm != 0)
  3383. return EMULATE_FAIL;
  3384. break;
  3385. default:
  3386. return EMULATE_FAIL;
  3387. }
  3388. if (!(c->modrm_reg == 0 || c->modrm_reg == 3))
  3389. return EMULATE_FAIL;
  3390. }
  3391. ++vcpu->stat.insn_emulation;
  3392. if (r) {
  3393. ++vcpu->stat.insn_emulation_fail;
  3394. trace_kvm_emulate_insn_failed(vcpu);
  3395. if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
  3396. return EMULATE_DONE;
  3397. return EMULATE_FAIL;
  3398. }
  3399. }
  3400. if (emulation_type & EMULTYPE_SKIP) {
  3401. kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.decode.eip);
  3402. return EMULATE_DONE;
  3403. }
  3404. restart:
  3405. r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
  3406. shadow_mask = vcpu->arch.emulate_ctxt.interruptibility;
  3407. if (r == 0)
  3408. kvm_x86_ops->set_interrupt_shadow(vcpu, shadow_mask);
  3409. if (vcpu->arch.pio.count) {
  3410. if (!vcpu->arch.pio.in)
  3411. vcpu->arch.pio.count = 0;
  3412. return EMULATE_DO_MMIO;
  3413. }
  3414. if (r || vcpu->mmio_is_write) {
  3415. run->exit_reason = KVM_EXIT_MMIO;
  3416. run->mmio.phys_addr = vcpu->mmio_phys_addr;
  3417. memcpy(run->mmio.data, vcpu->mmio_data, 8);
  3418. run->mmio.len = vcpu->mmio_size;
  3419. run->mmio.is_write = vcpu->mmio_is_write;
  3420. }
  3421. if (r) {
  3422. if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
  3423. goto done;
  3424. if (!vcpu->mmio_needed) {
  3425. ++vcpu->stat.insn_emulation_fail;
  3426. trace_kvm_emulate_insn_failed(vcpu);
  3427. kvm_report_emulation_failure(vcpu, "mmio");
  3428. return EMULATE_FAIL;
  3429. }
  3430. return EMULATE_DO_MMIO;
  3431. }
  3432. if (vcpu->mmio_is_write) {
  3433. vcpu->mmio_needed = 0;
  3434. return EMULATE_DO_MMIO;
  3435. }
  3436. done:
  3437. if (vcpu->arch.exception.pending)
  3438. vcpu->arch.emulate_ctxt.restart = false;
  3439. if (vcpu->arch.emulate_ctxt.restart)
  3440. goto restart;
  3441. return EMULATE_DONE;
  3442. }
  3443. EXPORT_SYMBOL_GPL(emulate_instruction);
  3444. int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port)
  3445. {
  3446. unsigned long val = kvm_register_read(vcpu, VCPU_REGS_RAX);
  3447. int ret = emulator_pio_out_emulated(size, port, &val, 1, vcpu);
  3448. /* do not return to emulator after return from userspace */
  3449. vcpu->arch.pio.count = 0;
  3450. return ret;
  3451. }
  3452. EXPORT_SYMBOL_GPL(kvm_fast_pio_out);
  3453. static void bounce_off(void *info)
  3454. {
  3455. /* nothing */
  3456. }
  3457. static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  3458. void *data)
  3459. {
  3460. struct cpufreq_freqs *freq = data;
  3461. struct kvm *kvm;
  3462. struct kvm_vcpu *vcpu;
  3463. int i, send_ipi = 0;
  3464. if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
  3465. return 0;
  3466. if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
  3467. return 0;
  3468. per_cpu(cpu_tsc_khz, freq->cpu) = freq->new;
  3469. spin_lock(&kvm_lock);
  3470. list_for_each_entry(kvm, &vm_list, vm_list) {
  3471. kvm_for_each_vcpu(i, vcpu, kvm) {
  3472. if (vcpu->cpu != freq->cpu)
  3473. continue;
  3474. if (!kvm_request_guest_time_update(vcpu))
  3475. continue;
  3476. if (vcpu->cpu != smp_processor_id())
  3477. send_ipi++;
  3478. }
  3479. }
  3480. spin_unlock(&kvm_lock);
  3481. if (freq->old < freq->new && send_ipi) {
  3482. /*
  3483. * We upscale the frequency. Must make the guest
  3484. * doesn't see old kvmclock values while running with
  3485. * the new frequency, otherwise we risk the guest sees
  3486. * time go backwards.
  3487. *
  3488. * In case we update the frequency for another cpu
  3489. * (which might be in guest context) send an interrupt
  3490. * to kick the cpu out of guest context. Next time
  3491. * guest context is entered kvmclock will be updated,
  3492. * so the guest will not see stale values.
  3493. */
  3494. smp_call_function_single(freq->cpu, bounce_off, NULL, 1);
  3495. }
  3496. return 0;
  3497. }
  3498. static struct notifier_block kvmclock_cpufreq_notifier_block = {
  3499. .notifier_call = kvmclock_cpufreq_notifier
  3500. };
  3501. static void kvm_timer_init(void)
  3502. {
  3503. int cpu;
  3504. if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
  3505. cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
  3506. CPUFREQ_TRANSITION_NOTIFIER);
  3507. for_each_online_cpu(cpu) {
  3508. unsigned long khz = cpufreq_get(cpu);
  3509. if (!khz)
  3510. khz = tsc_khz;
  3511. per_cpu(cpu_tsc_khz, cpu) = khz;
  3512. }
  3513. } else {
  3514. for_each_possible_cpu(cpu)
  3515. per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
  3516. }
  3517. }
  3518. static DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
  3519. static int kvm_is_in_guest(void)
  3520. {
  3521. return percpu_read(current_vcpu) != NULL;
  3522. }
  3523. static int kvm_is_user_mode(void)
  3524. {
  3525. int user_mode = 3;
  3526. if (percpu_read(current_vcpu))
  3527. user_mode = kvm_x86_ops->get_cpl(percpu_read(current_vcpu));
  3528. return user_mode != 0;
  3529. }
  3530. static unsigned long kvm_get_guest_ip(void)
  3531. {
  3532. unsigned long ip = 0;
  3533. if (percpu_read(current_vcpu))
  3534. ip = kvm_rip_read(percpu_read(current_vcpu));
  3535. return ip;
  3536. }
  3537. static struct perf_guest_info_callbacks kvm_guest_cbs = {
  3538. .is_in_guest = kvm_is_in_guest,
  3539. .is_user_mode = kvm_is_user_mode,
  3540. .get_guest_ip = kvm_get_guest_ip,
  3541. };
  3542. void kvm_before_handle_nmi(struct kvm_vcpu *vcpu)
  3543. {
  3544. percpu_write(current_vcpu, vcpu);
  3545. }
  3546. EXPORT_SYMBOL_GPL(kvm_before_handle_nmi);
  3547. void kvm_after_handle_nmi(struct kvm_vcpu *vcpu)
  3548. {
  3549. percpu_write(current_vcpu, NULL);
  3550. }
  3551. EXPORT_SYMBOL_GPL(kvm_after_handle_nmi);
  3552. int kvm_arch_init(void *opaque)
  3553. {
  3554. int r;
  3555. struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
  3556. if (kvm_x86_ops) {
  3557. printk(KERN_ERR "kvm: already loaded the other module\n");
  3558. r = -EEXIST;
  3559. goto out;
  3560. }
  3561. if (!ops->cpu_has_kvm_support()) {
  3562. printk(KERN_ERR "kvm: no hardware support\n");
  3563. r = -EOPNOTSUPP;
  3564. goto out;
  3565. }
  3566. if (ops->disabled_by_bios()) {
  3567. printk(KERN_ERR "kvm: disabled by bios\n");
  3568. r = -EOPNOTSUPP;
  3569. goto out;
  3570. }
  3571. r = kvm_mmu_module_init();
  3572. if (r)
  3573. goto out;
  3574. kvm_init_msr_list();
  3575. kvm_x86_ops = ops;
  3576. kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
  3577. kvm_mmu_set_base_ptes(PT_PRESENT_MASK);
  3578. kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
  3579. PT_DIRTY_MASK, PT64_NX_MASK, 0);
  3580. kvm_timer_init();
  3581. perf_register_guest_info_callbacks(&kvm_guest_cbs);
  3582. return 0;
  3583. out:
  3584. return r;
  3585. }
  3586. void kvm_arch_exit(void)
  3587. {
  3588. perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
  3589. if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
  3590. cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
  3591. CPUFREQ_TRANSITION_NOTIFIER);
  3592. kvm_x86_ops = NULL;
  3593. kvm_mmu_module_exit();
  3594. }
  3595. int kvm_emulate_halt(struct kvm_vcpu *vcpu)
  3596. {
  3597. ++vcpu->stat.halt_exits;
  3598. if (irqchip_in_kernel(vcpu->kvm)) {
  3599. vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
  3600. return 1;
  3601. } else {
  3602. vcpu->run->exit_reason = KVM_EXIT_HLT;
  3603. return 0;
  3604. }
  3605. }
  3606. EXPORT_SYMBOL_GPL(kvm_emulate_halt);
  3607. static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
  3608. unsigned long a1)
  3609. {
  3610. if (is_long_mode(vcpu))
  3611. return a0;
  3612. else
  3613. return a0 | ((gpa_t)a1 << 32);
  3614. }
  3615. int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
  3616. {
  3617. u64 param, ingpa, outgpa, ret;
  3618. uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
  3619. bool fast, longmode;
  3620. int cs_db, cs_l;
  3621. /*
  3622. * hypercall generates UD from non zero cpl and real mode
  3623. * per HYPER-V spec
  3624. */
  3625. if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
  3626. kvm_queue_exception(vcpu, UD_VECTOR);
  3627. return 0;
  3628. }
  3629. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  3630. longmode = is_long_mode(vcpu) && cs_l == 1;
  3631. if (!longmode) {
  3632. param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
  3633. (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
  3634. ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
  3635. (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
  3636. outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
  3637. (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
  3638. }
  3639. #ifdef CONFIG_X86_64
  3640. else {
  3641. param = kvm_register_read(vcpu, VCPU_REGS_RCX);
  3642. ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
  3643. outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
  3644. }
  3645. #endif
  3646. code = param & 0xffff;
  3647. fast = (param >> 16) & 0x1;
  3648. rep_cnt = (param >> 32) & 0xfff;
  3649. rep_idx = (param >> 48) & 0xfff;
  3650. trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
  3651. switch (code) {
  3652. case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT:
  3653. kvm_vcpu_on_spin(vcpu);
  3654. break;
  3655. default:
  3656. res = HV_STATUS_INVALID_HYPERCALL_CODE;
  3657. break;
  3658. }
  3659. ret = res | (((u64)rep_done & 0xfff) << 32);
  3660. if (longmode) {
  3661. kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
  3662. } else {
  3663. kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32);
  3664. kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff);
  3665. }
  3666. return 1;
  3667. }
  3668. int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
  3669. {
  3670. unsigned long nr, a0, a1, a2, a3, ret;
  3671. int r = 1;
  3672. if (kvm_hv_hypercall_enabled(vcpu->kvm))
  3673. return kvm_hv_hypercall(vcpu);
  3674. nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
  3675. a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
  3676. a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
  3677. a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
  3678. a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
  3679. trace_kvm_hypercall(nr, a0, a1, a2, a3);
  3680. if (!is_long_mode(vcpu)) {
  3681. nr &= 0xFFFFFFFF;
  3682. a0 &= 0xFFFFFFFF;
  3683. a1 &= 0xFFFFFFFF;
  3684. a2 &= 0xFFFFFFFF;
  3685. a3 &= 0xFFFFFFFF;
  3686. }
  3687. if (kvm_x86_ops->get_cpl(vcpu) != 0) {
  3688. ret = -KVM_EPERM;
  3689. goto out;
  3690. }
  3691. switch (nr) {
  3692. case KVM_HC_VAPIC_POLL_IRQ:
  3693. ret = 0;
  3694. break;
  3695. case KVM_HC_MMU_OP:
  3696. r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
  3697. break;
  3698. default:
  3699. ret = -KVM_ENOSYS;
  3700. break;
  3701. }
  3702. out:
  3703. kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
  3704. ++vcpu->stat.hypercalls;
  3705. return r;
  3706. }
  3707. EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
  3708. int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
  3709. {
  3710. char instruction[3];
  3711. unsigned long rip = kvm_rip_read(vcpu);
  3712. /*
  3713. * Blow out the MMU to ensure that no other VCPU has an active mapping
  3714. * to ensure that the updated hypercall appears atomically across all
  3715. * VCPUs.
  3716. */
  3717. kvm_mmu_zap_all(vcpu->kvm);
  3718. kvm_x86_ops->patch_hypercall(vcpu, instruction);
  3719. return emulator_write_emulated(rip, instruction, 3, vcpu);
  3720. }
  3721. void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  3722. {
  3723. struct desc_ptr dt = { limit, base };
  3724. kvm_x86_ops->set_gdt(vcpu, &dt);
  3725. }
  3726. void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  3727. {
  3728. struct desc_ptr dt = { limit, base };
  3729. kvm_x86_ops->set_idt(vcpu, &dt);
  3730. }
  3731. static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
  3732. {
  3733. struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
  3734. int j, nent = vcpu->arch.cpuid_nent;
  3735. e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
  3736. /* when no next entry is found, the current entry[i] is reselected */
  3737. for (j = i + 1; ; j = (j + 1) % nent) {
  3738. struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
  3739. if (ej->function == e->function) {
  3740. ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
  3741. return j;
  3742. }
  3743. }
  3744. return 0; /* silence gcc, even though control never reaches here */
  3745. }
  3746. /* find an entry with matching function, matching index (if needed), and that
  3747. * should be read next (if it's stateful) */
  3748. static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
  3749. u32 function, u32 index)
  3750. {
  3751. if (e->function != function)
  3752. return 0;
  3753. if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
  3754. return 0;
  3755. if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
  3756. !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
  3757. return 0;
  3758. return 1;
  3759. }
  3760. struct kvm_cpuid_entry2 *kvm_find_cpuid_entry(struct kvm_vcpu *vcpu,
  3761. u32 function, u32 index)
  3762. {
  3763. int i;
  3764. struct kvm_cpuid_entry2 *best = NULL;
  3765. for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
  3766. struct kvm_cpuid_entry2 *e;
  3767. e = &vcpu->arch.cpuid_entries[i];
  3768. if (is_matching_cpuid_entry(e, function, index)) {
  3769. if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
  3770. move_to_next_stateful_cpuid_entry(vcpu, i);
  3771. best = e;
  3772. break;
  3773. }
  3774. /*
  3775. * Both basic or both extended?
  3776. */
  3777. if (((e->function ^ function) & 0x80000000) == 0)
  3778. if (!best || e->function > best->function)
  3779. best = e;
  3780. }
  3781. return best;
  3782. }
  3783. EXPORT_SYMBOL_GPL(kvm_find_cpuid_entry);
  3784. int cpuid_maxphyaddr(struct kvm_vcpu *vcpu)
  3785. {
  3786. struct kvm_cpuid_entry2 *best;
  3787. best = kvm_find_cpuid_entry(vcpu, 0x80000000, 0);
  3788. if (!best || best->eax < 0x80000008)
  3789. goto not_found;
  3790. best = kvm_find_cpuid_entry(vcpu, 0x80000008, 0);
  3791. if (best)
  3792. return best->eax & 0xff;
  3793. not_found:
  3794. return 36;
  3795. }
  3796. void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
  3797. {
  3798. u32 function, index;
  3799. struct kvm_cpuid_entry2 *best;
  3800. function = kvm_register_read(vcpu, VCPU_REGS_RAX);
  3801. index = kvm_register_read(vcpu, VCPU_REGS_RCX);
  3802. kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
  3803. kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
  3804. kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
  3805. kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
  3806. best = kvm_find_cpuid_entry(vcpu, function, index);
  3807. if (best) {
  3808. kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
  3809. kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
  3810. kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
  3811. kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
  3812. }
  3813. kvm_x86_ops->skip_emulated_instruction(vcpu);
  3814. trace_kvm_cpuid(function,
  3815. kvm_register_read(vcpu, VCPU_REGS_RAX),
  3816. kvm_register_read(vcpu, VCPU_REGS_RBX),
  3817. kvm_register_read(vcpu, VCPU_REGS_RCX),
  3818. kvm_register_read(vcpu, VCPU_REGS_RDX));
  3819. }
  3820. EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
  3821. /*
  3822. * Check if userspace requested an interrupt window, and that the
  3823. * interrupt window is open.
  3824. *
  3825. * No need to exit to userspace if we already have an interrupt queued.
  3826. */
  3827. static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
  3828. {
  3829. return (!irqchip_in_kernel(vcpu->kvm) && !kvm_cpu_has_interrupt(vcpu) &&
  3830. vcpu->run->request_interrupt_window &&
  3831. kvm_arch_interrupt_allowed(vcpu));
  3832. }
  3833. static void post_kvm_run_save(struct kvm_vcpu *vcpu)
  3834. {
  3835. struct kvm_run *kvm_run = vcpu->run;
  3836. kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
  3837. kvm_run->cr8 = kvm_get_cr8(vcpu);
  3838. kvm_run->apic_base = kvm_get_apic_base(vcpu);
  3839. if (irqchip_in_kernel(vcpu->kvm))
  3840. kvm_run->ready_for_interrupt_injection = 1;
  3841. else
  3842. kvm_run->ready_for_interrupt_injection =
  3843. kvm_arch_interrupt_allowed(vcpu) &&
  3844. !kvm_cpu_has_interrupt(vcpu) &&
  3845. !kvm_event_needs_reinjection(vcpu);
  3846. }
  3847. static void vapic_enter(struct kvm_vcpu *vcpu)
  3848. {
  3849. struct kvm_lapic *apic = vcpu->arch.apic;
  3850. struct page *page;
  3851. if (!apic || !apic->vapic_addr)
  3852. return;
  3853. page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
  3854. vcpu->arch.apic->vapic_page = page;
  3855. }
  3856. static void vapic_exit(struct kvm_vcpu *vcpu)
  3857. {
  3858. struct kvm_lapic *apic = vcpu->arch.apic;
  3859. int idx;
  3860. if (!apic || !apic->vapic_addr)
  3861. return;
  3862. idx = srcu_read_lock(&vcpu->kvm->srcu);
  3863. kvm_release_page_dirty(apic->vapic_page);
  3864. mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
  3865. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  3866. }
  3867. static void update_cr8_intercept(struct kvm_vcpu *vcpu)
  3868. {
  3869. int max_irr, tpr;
  3870. if (!kvm_x86_ops->update_cr8_intercept)
  3871. return;
  3872. if (!vcpu->arch.apic)
  3873. return;
  3874. if (!vcpu->arch.apic->vapic_addr)
  3875. max_irr = kvm_lapic_find_highest_irr(vcpu);
  3876. else
  3877. max_irr = -1;
  3878. if (max_irr != -1)
  3879. max_irr >>= 4;
  3880. tpr = kvm_lapic_get_cr8(vcpu);
  3881. kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
  3882. }
  3883. static void inject_pending_event(struct kvm_vcpu *vcpu)
  3884. {
  3885. /* try to reinject previous events if any */
  3886. if (vcpu->arch.exception.pending) {
  3887. trace_kvm_inj_exception(vcpu->arch.exception.nr,
  3888. vcpu->arch.exception.has_error_code,
  3889. vcpu->arch.exception.error_code);
  3890. kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
  3891. vcpu->arch.exception.has_error_code,
  3892. vcpu->arch.exception.error_code,
  3893. vcpu->arch.exception.reinject);
  3894. return;
  3895. }
  3896. if (vcpu->arch.nmi_injected) {
  3897. kvm_x86_ops->set_nmi(vcpu);
  3898. return;
  3899. }
  3900. if (vcpu->arch.interrupt.pending) {
  3901. kvm_x86_ops->set_irq(vcpu);
  3902. return;
  3903. }
  3904. /* try to inject new event if pending */
  3905. if (vcpu->arch.nmi_pending) {
  3906. if (kvm_x86_ops->nmi_allowed(vcpu)) {
  3907. vcpu->arch.nmi_pending = false;
  3908. vcpu->arch.nmi_injected = true;
  3909. kvm_x86_ops->set_nmi(vcpu);
  3910. }
  3911. } else if (kvm_cpu_has_interrupt(vcpu)) {
  3912. if (kvm_x86_ops->interrupt_allowed(vcpu)) {
  3913. kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
  3914. false);
  3915. kvm_x86_ops->set_irq(vcpu);
  3916. }
  3917. }
  3918. }
  3919. static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
  3920. {
  3921. int r;
  3922. bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
  3923. vcpu->run->request_interrupt_window;
  3924. if (vcpu->requests)
  3925. if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
  3926. kvm_mmu_unload(vcpu);
  3927. r = kvm_mmu_reload(vcpu);
  3928. if (unlikely(r))
  3929. goto out;
  3930. if (vcpu->requests) {
  3931. if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
  3932. __kvm_migrate_timers(vcpu);
  3933. if (test_and_clear_bit(KVM_REQ_KVMCLOCK_UPDATE, &vcpu->requests))
  3934. kvm_write_guest_time(vcpu);
  3935. if (test_and_clear_bit(KVM_REQ_MMU_SYNC, &vcpu->requests))
  3936. kvm_mmu_sync_roots(vcpu);
  3937. if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
  3938. kvm_x86_ops->tlb_flush(vcpu);
  3939. if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
  3940. &vcpu->requests)) {
  3941. vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
  3942. r = 0;
  3943. goto out;
  3944. }
  3945. if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) {
  3946. vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
  3947. r = 0;
  3948. goto out;
  3949. }
  3950. if (test_and_clear_bit(KVM_REQ_DEACTIVATE_FPU, &vcpu->requests)) {
  3951. vcpu->fpu_active = 0;
  3952. kvm_x86_ops->fpu_deactivate(vcpu);
  3953. }
  3954. }
  3955. preempt_disable();
  3956. kvm_x86_ops->prepare_guest_switch(vcpu);
  3957. if (vcpu->fpu_active)
  3958. kvm_load_guest_fpu(vcpu);
  3959. local_irq_disable();
  3960. clear_bit(KVM_REQ_KICK, &vcpu->requests);
  3961. smp_mb__after_clear_bit();
  3962. if (vcpu->requests || need_resched() || signal_pending(current)) {
  3963. set_bit(KVM_REQ_KICK, &vcpu->requests);
  3964. local_irq_enable();
  3965. preempt_enable();
  3966. r = 1;
  3967. goto out;
  3968. }
  3969. inject_pending_event(vcpu);
  3970. /* enable NMI/IRQ window open exits if needed */
  3971. if (vcpu->arch.nmi_pending)
  3972. kvm_x86_ops->enable_nmi_window(vcpu);
  3973. else if (kvm_cpu_has_interrupt(vcpu) || req_int_win)
  3974. kvm_x86_ops->enable_irq_window(vcpu);
  3975. if (kvm_lapic_enabled(vcpu)) {
  3976. update_cr8_intercept(vcpu);
  3977. kvm_lapic_sync_to_vapic(vcpu);
  3978. }
  3979. srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
  3980. kvm_guest_enter();
  3981. if (unlikely(vcpu->arch.switch_db_regs)) {
  3982. set_debugreg(0, 7);
  3983. set_debugreg(vcpu->arch.eff_db[0], 0);
  3984. set_debugreg(vcpu->arch.eff_db[1], 1);
  3985. set_debugreg(vcpu->arch.eff_db[2], 2);
  3986. set_debugreg(vcpu->arch.eff_db[3], 3);
  3987. }
  3988. trace_kvm_entry(vcpu->vcpu_id);
  3989. kvm_x86_ops->run(vcpu);
  3990. /*
  3991. * If the guest has used debug registers, at least dr7
  3992. * will be disabled while returning to the host.
  3993. * If we don't have active breakpoints in the host, we don't
  3994. * care about the messed up debug address registers. But if
  3995. * we have some of them active, restore the old state.
  3996. */
  3997. if (hw_breakpoint_active())
  3998. hw_breakpoint_restore();
  3999. set_bit(KVM_REQ_KICK, &vcpu->requests);
  4000. local_irq_enable();
  4001. ++vcpu->stat.exits;
  4002. /*
  4003. * We must have an instruction between local_irq_enable() and
  4004. * kvm_guest_exit(), so the timer interrupt isn't delayed by
  4005. * the interrupt shadow. The stat.exits increment will do nicely.
  4006. * But we need to prevent reordering, hence this barrier():
  4007. */
  4008. barrier();
  4009. kvm_guest_exit();
  4010. preempt_enable();
  4011. vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  4012. /*
  4013. * Profile KVM exit RIPs:
  4014. */
  4015. if (unlikely(prof_on == KVM_PROFILING)) {
  4016. unsigned long rip = kvm_rip_read(vcpu);
  4017. profile_hit(KVM_PROFILING, (void *)rip);
  4018. }
  4019. kvm_lapic_sync_from_vapic(vcpu);
  4020. r = kvm_x86_ops->handle_exit(vcpu);
  4021. out:
  4022. return r;
  4023. }
  4024. static int __vcpu_run(struct kvm_vcpu *vcpu)
  4025. {
  4026. int r;
  4027. struct kvm *kvm = vcpu->kvm;
  4028. if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
  4029. pr_debug("vcpu %d received sipi with vector # %x\n",
  4030. vcpu->vcpu_id, vcpu->arch.sipi_vector);
  4031. kvm_lapic_reset(vcpu);
  4032. r = kvm_arch_vcpu_reset(vcpu);
  4033. if (r)
  4034. return r;
  4035. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  4036. }
  4037. vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
  4038. vapic_enter(vcpu);
  4039. r = 1;
  4040. while (r > 0) {
  4041. if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
  4042. r = vcpu_enter_guest(vcpu);
  4043. else {
  4044. srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
  4045. kvm_vcpu_block(vcpu);
  4046. vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
  4047. if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests))
  4048. {
  4049. switch(vcpu->arch.mp_state) {
  4050. case KVM_MP_STATE_HALTED:
  4051. vcpu->arch.mp_state =
  4052. KVM_MP_STATE_RUNNABLE;
  4053. case KVM_MP_STATE_RUNNABLE:
  4054. break;
  4055. case KVM_MP_STATE_SIPI_RECEIVED:
  4056. default:
  4057. r = -EINTR;
  4058. break;
  4059. }
  4060. }
  4061. }
  4062. if (r <= 0)
  4063. break;
  4064. clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
  4065. if (kvm_cpu_has_pending_timer(vcpu))
  4066. kvm_inject_pending_timer_irqs(vcpu);
  4067. if (dm_request_for_irq_injection(vcpu)) {
  4068. r = -EINTR;
  4069. vcpu->run->exit_reason = KVM_EXIT_INTR;
  4070. ++vcpu->stat.request_irq_exits;
  4071. }
  4072. if (signal_pending(current)) {
  4073. r = -EINTR;
  4074. vcpu->run->exit_reason = KVM_EXIT_INTR;
  4075. ++vcpu->stat.signal_exits;
  4076. }
  4077. if (need_resched()) {
  4078. srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
  4079. kvm_resched(vcpu);
  4080. vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
  4081. }
  4082. }
  4083. srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
  4084. vapic_exit(vcpu);
  4085. return r;
  4086. }
  4087. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  4088. {
  4089. int r;
  4090. sigset_t sigsaved;
  4091. vcpu_load(vcpu);
  4092. if (vcpu->sigset_active)
  4093. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  4094. if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
  4095. kvm_vcpu_block(vcpu);
  4096. clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
  4097. r = -EAGAIN;
  4098. goto out;
  4099. }
  4100. /* re-sync apic's tpr */
  4101. if (!irqchip_in_kernel(vcpu->kvm))
  4102. kvm_set_cr8(vcpu, kvm_run->cr8);
  4103. if (vcpu->arch.pio.count || vcpu->mmio_needed ||
  4104. vcpu->arch.emulate_ctxt.restart) {
  4105. if (vcpu->mmio_needed) {
  4106. memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
  4107. vcpu->mmio_read_completed = 1;
  4108. vcpu->mmio_needed = 0;
  4109. }
  4110. vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  4111. r = emulate_instruction(vcpu, 0, 0, EMULTYPE_NO_DECODE);
  4112. srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
  4113. if (r == EMULATE_DO_MMIO) {
  4114. r = 0;
  4115. goto out;
  4116. }
  4117. }
  4118. if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL)
  4119. kvm_register_write(vcpu, VCPU_REGS_RAX,
  4120. kvm_run->hypercall.ret);
  4121. r = __vcpu_run(vcpu);
  4122. out:
  4123. post_kvm_run_save(vcpu);
  4124. if (vcpu->sigset_active)
  4125. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  4126. vcpu_put(vcpu);
  4127. return r;
  4128. }
  4129. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  4130. {
  4131. vcpu_load(vcpu);
  4132. regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
  4133. regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
  4134. regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
  4135. regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
  4136. regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
  4137. regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
  4138. regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  4139. regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
  4140. #ifdef CONFIG_X86_64
  4141. regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
  4142. regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
  4143. regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
  4144. regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
  4145. regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
  4146. regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
  4147. regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
  4148. regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
  4149. #endif
  4150. regs->rip = kvm_rip_read(vcpu);
  4151. regs->rflags = kvm_get_rflags(vcpu);
  4152. vcpu_put(vcpu);
  4153. return 0;
  4154. }
  4155. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  4156. {
  4157. vcpu_load(vcpu);
  4158. kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
  4159. kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
  4160. kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
  4161. kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
  4162. kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
  4163. kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
  4164. kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
  4165. kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
  4166. #ifdef CONFIG_X86_64
  4167. kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
  4168. kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
  4169. kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
  4170. kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
  4171. kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
  4172. kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
  4173. kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
  4174. kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
  4175. #endif
  4176. kvm_rip_write(vcpu, regs->rip);
  4177. kvm_set_rflags(vcpu, regs->rflags);
  4178. vcpu->arch.exception.pending = false;
  4179. vcpu_put(vcpu);
  4180. return 0;
  4181. }
  4182. void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  4183. {
  4184. struct kvm_segment cs;
  4185. kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
  4186. *db = cs.db;
  4187. *l = cs.l;
  4188. }
  4189. EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
  4190. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  4191. struct kvm_sregs *sregs)
  4192. {
  4193. struct desc_ptr dt;
  4194. vcpu_load(vcpu);
  4195. kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  4196. kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  4197. kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  4198. kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  4199. kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  4200. kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  4201. kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  4202. kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  4203. kvm_x86_ops->get_idt(vcpu, &dt);
  4204. sregs->idt.limit = dt.size;
  4205. sregs->idt.base = dt.address;
  4206. kvm_x86_ops->get_gdt(vcpu, &dt);
  4207. sregs->gdt.limit = dt.size;
  4208. sregs->gdt.base = dt.address;
  4209. sregs->cr0 = kvm_read_cr0(vcpu);
  4210. sregs->cr2 = vcpu->arch.cr2;
  4211. sregs->cr3 = vcpu->arch.cr3;
  4212. sregs->cr4 = kvm_read_cr4(vcpu);
  4213. sregs->cr8 = kvm_get_cr8(vcpu);
  4214. sregs->efer = vcpu->arch.efer;
  4215. sregs->apic_base = kvm_get_apic_base(vcpu);
  4216. memset(sregs->interrupt_bitmap, 0, sizeof sregs->interrupt_bitmap);
  4217. if (vcpu->arch.interrupt.pending && !vcpu->arch.interrupt.soft)
  4218. set_bit(vcpu->arch.interrupt.nr,
  4219. (unsigned long *)sregs->interrupt_bitmap);
  4220. vcpu_put(vcpu);
  4221. return 0;
  4222. }
  4223. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  4224. struct kvm_mp_state *mp_state)
  4225. {
  4226. vcpu_load(vcpu);
  4227. mp_state->mp_state = vcpu->arch.mp_state;
  4228. vcpu_put(vcpu);
  4229. return 0;
  4230. }
  4231. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  4232. struct kvm_mp_state *mp_state)
  4233. {
  4234. vcpu_load(vcpu);
  4235. vcpu->arch.mp_state = mp_state->mp_state;
  4236. vcpu_put(vcpu);
  4237. return 0;
  4238. }
  4239. int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason,
  4240. bool has_error_code, u32 error_code)
  4241. {
  4242. int cs_db, cs_l, ret;
  4243. cache_all_regs(vcpu);
  4244. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  4245. vcpu->arch.emulate_ctxt.vcpu = vcpu;
  4246. vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
  4247. vcpu->arch.emulate_ctxt.eip = kvm_rip_read(vcpu);
  4248. vcpu->arch.emulate_ctxt.mode =
  4249. (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
  4250. (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
  4251. ? X86EMUL_MODE_VM86 : cs_l
  4252. ? X86EMUL_MODE_PROT64 : cs_db
  4253. ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
  4254. ret = emulator_task_switch(&vcpu->arch.emulate_ctxt, &emulate_ops,
  4255. tss_selector, reason, has_error_code,
  4256. error_code);
  4257. if (ret)
  4258. return EMULATE_FAIL;
  4259. kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
  4260. return EMULATE_DONE;
  4261. }
  4262. EXPORT_SYMBOL_GPL(kvm_task_switch);
  4263. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  4264. struct kvm_sregs *sregs)
  4265. {
  4266. int mmu_reset_needed = 0;
  4267. int pending_vec, max_bits;
  4268. struct desc_ptr dt;
  4269. vcpu_load(vcpu);
  4270. dt.size = sregs->idt.limit;
  4271. dt.address = sregs->idt.base;
  4272. kvm_x86_ops->set_idt(vcpu, &dt);
  4273. dt.size = sregs->gdt.limit;
  4274. dt.address = sregs->gdt.base;
  4275. kvm_x86_ops->set_gdt(vcpu, &dt);
  4276. vcpu->arch.cr2 = sregs->cr2;
  4277. mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
  4278. vcpu->arch.cr3 = sregs->cr3;
  4279. kvm_set_cr8(vcpu, sregs->cr8);
  4280. mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
  4281. kvm_x86_ops->set_efer(vcpu, sregs->efer);
  4282. kvm_set_apic_base(vcpu, sregs->apic_base);
  4283. mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
  4284. kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
  4285. vcpu->arch.cr0 = sregs->cr0;
  4286. mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
  4287. kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
  4288. if (!is_long_mode(vcpu) && is_pae(vcpu)) {
  4289. load_pdptrs(vcpu, vcpu->arch.cr3);
  4290. mmu_reset_needed = 1;
  4291. }
  4292. if (mmu_reset_needed)
  4293. kvm_mmu_reset_context(vcpu);
  4294. max_bits = (sizeof sregs->interrupt_bitmap) << 3;
  4295. pending_vec = find_first_bit(
  4296. (const unsigned long *)sregs->interrupt_bitmap, max_bits);
  4297. if (pending_vec < max_bits) {
  4298. kvm_queue_interrupt(vcpu, pending_vec, false);
  4299. pr_debug("Set back pending irq %d\n", pending_vec);
  4300. if (irqchip_in_kernel(vcpu->kvm))
  4301. kvm_pic_clear_isr_ack(vcpu->kvm);
  4302. }
  4303. kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  4304. kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  4305. kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  4306. kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  4307. kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  4308. kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  4309. kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  4310. kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  4311. update_cr8_intercept(vcpu);
  4312. /* Older userspace won't unhalt the vcpu on reset. */
  4313. if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
  4314. sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
  4315. !is_protmode(vcpu))
  4316. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  4317. vcpu_put(vcpu);
  4318. return 0;
  4319. }
  4320. int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
  4321. struct kvm_guest_debug *dbg)
  4322. {
  4323. unsigned long rflags;
  4324. int i, r;
  4325. vcpu_load(vcpu);
  4326. if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
  4327. r = -EBUSY;
  4328. if (vcpu->arch.exception.pending)
  4329. goto unlock_out;
  4330. if (dbg->control & KVM_GUESTDBG_INJECT_DB)
  4331. kvm_queue_exception(vcpu, DB_VECTOR);
  4332. else
  4333. kvm_queue_exception(vcpu, BP_VECTOR);
  4334. }
  4335. /*
  4336. * Read rflags as long as potentially injected trace flags are still
  4337. * filtered out.
  4338. */
  4339. rflags = kvm_get_rflags(vcpu);
  4340. vcpu->guest_debug = dbg->control;
  4341. if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
  4342. vcpu->guest_debug = 0;
  4343. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
  4344. for (i = 0; i < KVM_NR_DB_REGS; ++i)
  4345. vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
  4346. vcpu->arch.switch_db_regs =
  4347. (dbg->arch.debugreg[7] & DR7_BP_EN_MASK);
  4348. } else {
  4349. for (i = 0; i < KVM_NR_DB_REGS; i++)
  4350. vcpu->arch.eff_db[i] = vcpu->arch.db[i];
  4351. vcpu->arch.switch_db_regs = (vcpu->arch.dr7 & DR7_BP_EN_MASK);
  4352. }
  4353. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  4354. vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
  4355. get_segment_base(vcpu, VCPU_SREG_CS);
  4356. /*
  4357. * Trigger an rflags update that will inject or remove the trace
  4358. * flags.
  4359. */
  4360. kvm_set_rflags(vcpu, rflags);
  4361. kvm_x86_ops->set_guest_debug(vcpu, dbg);
  4362. r = 0;
  4363. unlock_out:
  4364. vcpu_put(vcpu);
  4365. return r;
  4366. }
  4367. /*
  4368. * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
  4369. * we have asm/x86/processor.h
  4370. */
  4371. struct fxsave {
  4372. u16 cwd;
  4373. u16 swd;
  4374. u16 twd;
  4375. u16 fop;
  4376. u64 rip;
  4377. u64 rdp;
  4378. u32 mxcsr;
  4379. u32 mxcsr_mask;
  4380. u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
  4381. #ifdef CONFIG_X86_64
  4382. u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
  4383. #else
  4384. u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
  4385. #endif
  4386. };
  4387. /*
  4388. * Translate a guest virtual address to a guest physical address.
  4389. */
  4390. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  4391. struct kvm_translation *tr)
  4392. {
  4393. unsigned long vaddr = tr->linear_address;
  4394. gpa_t gpa;
  4395. int idx;
  4396. vcpu_load(vcpu);
  4397. idx = srcu_read_lock(&vcpu->kvm->srcu);
  4398. gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
  4399. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  4400. tr->physical_address = gpa;
  4401. tr->valid = gpa != UNMAPPED_GVA;
  4402. tr->writeable = 1;
  4403. tr->usermode = 0;
  4404. vcpu_put(vcpu);
  4405. return 0;
  4406. }
  4407. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  4408. {
  4409. struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
  4410. vcpu_load(vcpu);
  4411. memcpy(fpu->fpr, fxsave->st_space, 128);
  4412. fpu->fcw = fxsave->cwd;
  4413. fpu->fsw = fxsave->swd;
  4414. fpu->ftwx = fxsave->twd;
  4415. fpu->last_opcode = fxsave->fop;
  4416. fpu->last_ip = fxsave->rip;
  4417. fpu->last_dp = fxsave->rdp;
  4418. memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
  4419. vcpu_put(vcpu);
  4420. return 0;
  4421. }
  4422. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  4423. {
  4424. struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
  4425. vcpu_load(vcpu);
  4426. memcpy(fxsave->st_space, fpu->fpr, 128);
  4427. fxsave->cwd = fpu->fcw;
  4428. fxsave->swd = fpu->fsw;
  4429. fxsave->twd = fpu->ftwx;
  4430. fxsave->fop = fpu->last_opcode;
  4431. fxsave->rip = fpu->last_ip;
  4432. fxsave->rdp = fpu->last_dp;
  4433. memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
  4434. vcpu_put(vcpu);
  4435. return 0;
  4436. }
  4437. void fx_init(struct kvm_vcpu *vcpu)
  4438. {
  4439. unsigned after_mxcsr_mask;
  4440. /*
  4441. * Touch the fpu the first time in non atomic context as if
  4442. * this is the first fpu instruction the exception handler
  4443. * will fire before the instruction returns and it'll have to
  4444. * allocate ram with GFP_KERNEL.
  4445. */
  4446. if (!used_math())
  4447. kvm_fx_save(&vcpu->arch.host_fx_image);
  4448. /* Initialize guest FPU by resetting ours and saving into guest's */
  4449. preempt_disable();
  4450. kvm_fx_save(&vcpu->arch.host_fx_image);
  4451. kvm_fx_finit();
  4452. kvm_fx_save(&vcpu->arch.guest_fx_image);
  4453. kvm_fx_restore(&vcpu->arch.host_fx_image);
  4454. preempt_enable();
  4455. vcpu->arch.cr0 |= X86_CR0_ET;
  4456. after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
  4457. vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
  4458. memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
  4459. 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
  4460. }
  4461. EXPORT_SYMBOL_GPL(fx_init);
  4462. void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
  4463. {
  4464. if (vcpu->guest_fpu_loaded)
  4465. return;
  4466. vcpu->guest_fpu_loaded = 1;
  4467. kvm_fx_save(&vcpu->arch.host_fx_image);
  4468. kvm_fx_restore(&vcpu->arch.guest_fx_image);
  4469. trace_kvm_fpu(1);
  4470. }
  4471. void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
  4472. {
  4473. if (!vcpu->guest_fpu_loaded)
  4474. return;
  4475. vcpu->guest_fpu_loaded = 0;
  4476. kvm_fx_save(&vcpu->arch.guest_fx_image);
  4477. kvm_fx_restore(&vcpu->arch.host_fx_image);
  4478. ++vcpu->stat.fpu_reload;
  4479. set_bit(KVM_REQ_DEACTIVATE_FPU, &vcpu->requests);
  4480. trace_kvm_fpu(0);
  4481. }
  4482. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  4483. {
  4484. if (vcpu->arch.time_page) {
  4485. kvm_release_page_dirty(vcpu->arch.time_page);
  4486. vcpu->arch.time_page = NULL;
  4487. }
  4488. kvm_x86_ops->vcpu_free(vcpu);
  4489. }
  4490. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
  4491. unsigned int id)
  4492. {
  4493. return kvm_x86_ops->vcpu_create(kvm, id);
  4494. }
  4495. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
  4496. {
  4497. int r;
  4498. /* We do fxsave: this must be aligned. */
  4499. BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
  4500. vcpu->arch.mtrr_state.have_fixed = 1;
  4501. vcpu_load(vcpu);
  4502. r = kvm_arch_vcpu_reset(vcpu);
  4503. if (r == 0)
  4504. r = kvm_mmu_setup(vcpu);
  4505. vcpu_put(vcpu);
  4506. if (r < 0)
  4507. goto free_vcpu;
  4508. return 0;
  4509. free_vcpu:
  4510. kvm_x86_ops->vcpu_free(vcpu);
  4511. return r;
  4512. }
  4513. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  4514. {
  4515. vcpu_load(vcpu);
  4516. kvm_mmu_unload(vcpu);
  4517. vcpu_put(vcpu);
  4518. kvm_x86_ops->vcpu_free(vcpu);
  4519. }
  4520. int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
  4521. {
  4522. vcpu->arch.nmi_pending = false;
  4523. vcpu->arch.nmi_injected = false;
  4524. vcpu->arch.switch_db_regs = 0;
  4525. memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
  4526. vcpu->arch.dr6 = DR6_FIXED_1;
  4527. vcpu->arch.dr7 = DR7_FIXED_1;
  4528. return kvm_x86_ops->vcpu_reset(vcpu);
  4529. }
  4530. int kvm_arch_hardware_enable(void *garbage)
  4531. {
  4532. /*
  4533. * Since this may be called from a hotplug notifcation,
  4534. * we can't get the CPU frequency directly.
  4535. */
  4536. if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
  4537. int cpu = raw_smp_processor_id();
  4538. per_cpu(cpu_tsc_khz, cpu) = 0;
  4539. }
  4540. kvm_shared_msr_cpu_online();
  4541. return kvm_x86_ops->hardware_enable(garbage);
  4542. }
  4543. void kvm_arch_hardware_disable(void *garbage)
  4544. {
  4545. kvm_x86_ops->hardware_disable(garbage);
  4546. drop_user_return_notifiers(garbage);
  4547. }
  4548. int kvm_arch_hardware_setup(void)
  4549. {
  4550. return kvm_x86_ops->hardware_setup();
  4551. }
  4552. void kvm_arch_hardware_unsetup(void)
  4553. {
  4554. kvm_x86_ops->hardware_unsetup();
  4555. }
  4556. void kvm_arch_check_processor_compat(void *rtn)
  4557. {
  4558. kvm_x86_ops->check_processor_compatibility(rtn);
  4559. }
  4560. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  4561. {
  4562. struct page *page;
  4563. struct kvm *kvm;
  4564. int r;
  4565. BUG_ON(vcpu->kvm == NULL);
  4566. kvm = vcpu->kvm;
  4567. vcpu->arch.mmu.root_hpa = INVALID_PAGE;
  4568. if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu))
  4569. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  4570. else
  4571. vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
  4572. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  4573. if (!page) {
  4574. r = -ENOMEM;
  4575. goto fail;
  4576. }
  4577. vcpu->arch.pio_data = page_address(page);
  4578. r = kvm_mmu_create(vcpu);
  4579. if (r < 0)
  4580. goto fail_free_pio_data;
  4581. if (irqchip_in_kernel(kvm)) {
  4582. r = kvm_create_lapic(vcpu);
  4583. if (r < 0)
  4584. goto fail_mmu_destroy;
  4585. }
  4586. vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
  4587. GFP_KERNEL);
  4588. if (!vcpu->arch.mce_banks) {
  4589. r = -ENOMEM;
  4590. goto fail_free_lapic;
  4591. }
  4592. vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
  4593. return 0;
  4594. fail_free_lapic:
  4595. kvm_free_lapic(vcpu);
  4596. fail_mmu_destroy:
  4597. kvm_mmu_destroy(vcpu);
  4598. fail_free_pio_data:
  4599. free_page((unsigned long)vcpu->arch.pio_data);
  4600. fail:
  4601. return r;
  4602. }
  4603. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  4604. {
  4605. int idx;
  4606. kfree(vcpu->arch.mce_banks);
  4607. kvm_free_lapic(vcpu);
  4608. idx = srcu_read_lock(&vcpu->kvm->srcu);
  4609. kvm_mmu_destroy(vcpu);
  4610. srcu_read_unlock(&vcpu->kvm->srcu, idx);
  4611. free_page((unsigned long)vcpu->arch.pio_data);
  4612. }
  4613. struct kvm *kvm_arch_create_vm(void)
  4614. {
  4615. struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
  4616. if (!kvm)
  4617. return ERR_PTR(-ENOMEM);
  4618. kvm->arch.aliases = kzalloc(sizeof(struct kvm_mem_aliases), GFP_KERNEL);
  4619. if (!kvm->arch.aliases) {
  4620. kfree(kvm);
  4621. return ERR_PTR(-ENOMEM);
  4622. }
  4623. INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
  4624. INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
  4625. /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
  4626. set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
  4627. rdtscll(kvm->arch.vm_init_tsc);
  4628. return kvm;
  4629. }
  4630. static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
  4631. {
  4632. vcpu_load(vcpu);
  4633. kvm_mmu_unload(vcpu);
  4634. vcpu_put(vcpu);
  4635. }
  4636. static void kvm_free_vcpus(struct kvm *kvm)
  4637. {
  4638. unsigned int i;
  4639. struct kvm_vcpu *vcpu;
  4640. /*
  4641. * Unpin any mmu pages first.
  4642. */
  4643. kvm_for_each_vcpu(i, vcpu, kvm)
  4644. kvm_unload_vcpu_mmu(vcpu);
  4645. kvm_for_each_vcpu(i, vcpu, kvm)
  4646. kvm_arch_vcpu_free(vcpu);
  4647. mutex_lock(&kvm->lock);
  4648. for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
  4649. kvm->vcpus[i] = NULL;
  4650. atomic_set(&kvm->online_vcpus, 0);
  4651. mutex_unlock(&kvm->lock);
  4652. }
  4653. void kvm_arch_sync_events(struct kvm *kvm)
  4654. {
  4655. kvm_free_all_assigned_devices(kvm);
  4656. }
  4657. void kvm_arch_destroy_vm(struct kvm *kvm)
  4658. {
  4659. kvm_iommu_unmap_guest(kvm);
  4660. kvm_free_pit(kvm);
  4661. kfree(kvm->arch.vpic);
  4662. kfree(kvm->arch.vioapic);
  4663. kvm_free_vcpus(kvm);
  4664. kvm_free_physmem(kvm);
  4665. if (kvm->arch.apic_access_page)
  4666. put_page(kvm->arch.apic_access_page);
  4667. if (kvm->arch.ept_identity_pagetable)
  4668. put_page(kvm->arch.ept_identity_pagetable);
  4669. cleanup_srcu_struct(&kvm->srcu);
  4670. kfree(kvm->arch.aliases);
  4671. kfree(kvm);
  4672. }
  4673. int kvm_arch_prepare_memory_region(struct kvm *kvm,
  4674. struct kvm_memory_slot *memslot,
  4675. struct kvm_memory_slot old,
  4676. struct kvm_userspace_memory_region *mem,
  4677. int user_alloc)
  4678. {
  4679. int npages = memslot->npages;
  4680. /*To keep backward compatibility with older userspace,
  4681. *x86 needs to hanlde !user_alloc case.
  4682. */
  4683. if (!user_alloc) {
  4684. if (npages && !old.rmap) {
  4685. unsigned long userspace_addr;
  4686. down_write(&current->mm->mmap_sem);
  4687. userspace_addr = do_mmap(NULL, 0,
  4688. npages * PAGE_SIZE,
  4689. PROT_READ | PROT_WRITE,
  4690. MAP_PRIVATE | MAP_ANONYMOUS,
  4691. 0);
  4692. up_write(&current->mm->mmap_sem);
  4693. if (IS_ERR((void *)userspace_addr))
  4694. return PTR_ERR((void *)userspace_addr);
  4695. memslot->userspace_addr = userspace_addr;
  4696. }
  4697. }
  4698. return 0;
  4699. }
  4700. void kvm_arch_commit_memory_region(struct kvm *kvm,
  4701. struct kvm_userspace_memory_region *mem,
  4702. struct kvm_memory_slot old,
  4703. int user_alloc)
  4704. {
  4705. int npages = mem->memory_size >> PAGE_SHIFT;
  4706. if (!user_alloc && !old.user_alloc && old.rmap && !npages) {
  4707. int ret;
  4708. down_write(&current->mm->mmap_sem);
  4709. ret = do_munmap(current->mm, old.userspace_addr,
  4710. old.npages * PAGE_SIZE);
  4711. up_write(&current->mm->mmap_sem);
  4712. if (ret < 0)
  4713. printk(KERN_WARNING
  4714. "kvm_vm_ioctl_set_memory_region: "
  4715. "failed to munmap memory\n");
  4716. }
  4717. spin_lock(&kvm->mmu_lock);
  4718. if (!kvm->arch.n_requested_mmu_pages) {
  4719. unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
  4720. kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
  4721. }
  4722. kvm_mmu_slot_remove_write_access(kvm, mem->slot);
  4723. spin_unlock(&kvm->mmu_lock);
  4724. }
  4725. void kvm_arch_flush_shadow(struct kvm *kvm)
  4726. {
  4727. kvm_mmu_zap_all(kvm);
  4728. kvm_reload_remote_mmus(kvm);
  4729. }
  4730. int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
  4731. {
  4732. return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
  4733. || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED
  4734. || vcpu->arch.nmi_pending ||
  4735. (kvm_arch_interrupt_allowed(vcpu) &&
  4736. kvm_cpu_has_interrupt(vcpu));
  4737. }
  4738. void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
  4739. {
  4740. int me;
  4741. int cpu = vcpu->cpu;
  4742. if (waitqueue_active(&vcpu->wq)) {
  4743. wake_up_interruptible(&vcpu->wq);
  4744. ++vcpu->stat.halt_wakeup;
  4745. }
  4746. me = get_cpu();
  4747. if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
  4748. if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
  4749. smp_send_reschedule(cpu);
  4750. put_cpu();
  4751. }
  4752. int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
  4753. {
  4754. return kvm_x86_ops->interrupt_allowed(vcpu);
  4755. }
  4756. bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
  4757. {
  4758. unsigned long current_rip = kvm_rip_read(vcpu) +
  4759. get_segment_base(vcpu, VCPU_SREG_CS);
  4760. return current_rip == linear_rip;
  4761. }
  4762. EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
  4763. unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
  4764. {
  4765. unsigned long rflags;
  4766. rflags = kvm_x86_ops->get_rflags(vcpu);
  4767. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  4768. rflags &= ~X86_EFLAGS_TF;
  4769. return rflags;
  4770. }
  4771. EXPORT_SYMBOL_GPL(kvm_get_rflags);
  4772. void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  4773. {
  4774. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
  4775. kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
  4776. rflags |= X86_EFLAGS_TF;
  4777. kvm_x86_ops->set_rflags(vcpu, rflags);
  4778. }
  4779. EXPORT_SYMBOL_GPL(kvm_set_rflags);
  4780. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
  4781. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
  4782. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
  4783. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
  4784. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
  4785. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
  4786. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
  4787. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
  4788. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
  4789. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
  4790. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
  4791. EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);