x86.c 136 KB

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