x86.c 133 KB

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