x86.c 119 KB

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