x86.c 143 KB

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