x86.c 132 KB

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