vmx.c 121 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * This module enables machines with Intel VT-x extensions to run virtual
  5. * machines without emulation or binary translation.
  6. *
  7. * Copyright (C) 2006 Qumranet, Inc.
  8. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  9. *
  10. * Authors:
  11. * Avi Kivity <avi@qumranet.com>
  12. * Yaniv Kamay <yaniv@qumranet.com>
  13. *
  14. * This work is licensed under the terms of the GNU GPL, version 2. See
  15. * the COPYING file in the top-level directory.
  16. *
  17. */
  18. #include "irq.h"
  19. #include "mmu.h"
  20. #include <linux/kvm_host.h>
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/mm.h>
  24. #include <linux/highmem.h>
  25. #include <linux/sched.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/ftrace_event.h>
  28. #include <linux/slab.h>
  29. #include <linux/tboot.h>
  30. #include "kvm_cache_regs.h"
  31. #include "x86.h"
  32. #include <asm/io.h>
  33. #include <asm/desc.h>
  34. #include <asm/vmx.h>
  35. #include <asm/virtext.h>
  36. #include <asm/mce.h>
  37. #include <asm/i387.h>
  38. #include <asm/xcr.h>
  39. #include "trace.h"
  40. #define __ex(x) __kvm_handle_fault_on_reboot(x)
  41. MODULE_AUTHOR("Qumranet");
  42. MODULE_LICENSE("GPL");
  43. static int __read_mostly bypass_guest_pf = 1;
  44. module_param(bypass_guest_pf, bool, S_IRUGO);
  45. static int __read_mostly enable_vpid = 1;
  46. module_param_named(vpid, enable_vpid, bool, 0444);
  47. static int __read_mostly flexpriority_enabled = 1;
  48. module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
  49. static int __read_mostly enable_ept = 1;
  50. module_param_named(ept, enable_ept, bool, S_IRUGO);
  51. static int __read_mostly enable_unrestricted_guest = 1;
  52. module_param_named(unrestricted_guest,
  53. enable_unrestricted_guest, bool, S_IRUGO);
  54. static int __read_mostly emulate_invalid_guest_state = 0;
  55. module_param(emulate_invalid_guest_state, bool, S_IRUGO);
  56. static int __read_mostly vmm_exclusive = 1;
  57. module_param(vmm_exclusive, bool, S_IRUGO);
  58. static int __read_mostly yield_on_hlt = 1;
  59. module_param(yield_on_hlt, bool, S_IRUGO);
  60. #define KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST \
  61. (X86_CR0_WP | X86_CR0_NE | X86_CR0_NW | X86_CR0_CD)
  62. #define KVM_GUEST_CR0_MASK \
  63. (KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
  64. #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST \
  65. (X86_CR0_WP | X86_CR0_NE)
  66. #define KVM_VM_CR0_ALWAYS_ON \
  67. (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
  68. #define KVM_CR4_GUEST_OWNED_BITS \
  69. (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
  70. | X86_CR4_OSXMMEXCPT)
  71. #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
  72. #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
  73. #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
  74. /*
  75. * These 2 parameters are used to config the controls for Pause-Loop Exiting:
  76. * ple_gap: upper bound on the amount of time between two successive
  77. * executions of PAUSE in a loop. Also indicate if ple enabled.
  78. * According to test, this time is usually smaller than 128 cycles.
  79. * ple_window: upper bound on the amount of time a guest is allowed to execute
  80. * in a PAUSE loop. Tests indicate that most spinlocks are held for
  81. * less than 2^12 cycles
  82. * Time is measured based on a counter that runs at the same rate as the TSC,
  83. * refer SDM volume 3b section 21.6.13 & 22.1.3.
  84. */
  85. #define KVM_VMX_DEFAULT_PLE_GAP 128
  86. #define KVM_VMX_DEFAULT_PLE_WINDOW 4096
  87. static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
  88. module_param(ple_gap, int, S_IRUGO);
  89. static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
  90. module_param(ple_window, int, S_IRUGO);
  91. #define NR_AUTOLOAD_MSRS 1
  92. struct vmcs {
  93. u32 revision_id;
  94. u32 abort;
  95. char data[0];
  96. };
  97. struct shared_msr_entry {
  98. unsigned index;
  99. u64 data;
  100. u64 mask;
  101. };
  102. struct vcpu_vmx {
  103. struct kvm_vcpu vcpu;
  104. struct list_head local_vcpus_link;
  105. unsigned long host_rsp;
  106. int launched;
  107. u8 fail;
  108. u8 cpl;
  109. bool nmi_known_unmasked;
  110. u32 exit_intr_info;
  111. u32 idt_vectoring_info;
  112. ulong rflags;
  113. struct shared_msr_entry *guest_msrs;
  114. int nmsrs;
  115. int save_nmsrs;
  116. #ifdef CONFIG_X86_64
  117. u64 msr_host_kernel_gs_base;
  118. u64 msr_guest_kernel_gs_base;
  119. #endif
  120. struct vmcs *vmcs;
  121. struct msr_autoload {
  122. unsigned nr;
  123. struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
  124. struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
  125. } msr_autoload;
  126. struct {
  127. int loaded;
  128. u16 fs_sel, gs_sel, ldt_sel;
  129. int gs_ldt_reload_needed;
  130. int fs_reload_needed;
  131. } host_state;
  132. struct {
  133. int vm86_active;
  134. ulong save_rflags;
  135. struct kvm_save_segment {
  136. u16 selector;
  137. unsigned long base;
  138. u32 limit;
  139. u32 ar;
  140. } tr, es, ds, fs, gs;
  141. } rmode;
  142. struct {
  143. u32 bitmask; /* 4 bits per segment (1 bit per field) */
  144. struct kvm_save_segment seg[8];
  145. } segment_cache;
  146. int vpid;
  147. bool emulation_required;
  148. /* Support for vnmi-less CPUs */
  149. int soft_vnmi_blocked;
  150. ktime_t entry_time;
  151. s64 vnmi_blocked_time;
  152. u32 exit_reason;
  153. bool rdtscp_enabled;
  154. };
  155. enum segment_cache_field {
  156. SEG_FIELD_SEL = 0,
  157. SEG_FIELD_BASE = 1,
  158. SEG_FIELD_LIMIT = 2,
  159. SEG_FIELD_AR = 3,
  160. SEG_FIELD_NR = 4
  161. };
  162. static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
  163. {
  164. return container_of(vcpu, struct vcpu_vmx, vcpu);
  165. }
  166. static u64 construct_eptp(unsigned long root_hpa);
  167. static void kvm_cpu_vmxon(u64 addr);
  168. static void kvm_cpu_vmxoff(void);
  169. static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3);
  170. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
  171. static DEFINE_PER_CPU(struct vmcs *, vmxarea);
  172. static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
  173. static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu);
  174. static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
  175. static unsigned long *vmx_io_bitmap_a;
  176. static unsigned long *vmx_io_bitmap_b;
  177. static unsigned long *vmx_msr_bitmap_legacy;
  178. static unsigned long *vmx_msr_bitmap_longmode;
  179. static bool cpu_has_load_ia32_efer;
  180. static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
  181. static DEFINE_SPINLOCK(vmx_vpid_lock);
  182. static struct vmcs_config {
  183. int size;
  184. int order;
  185. u32 revision_id;
  186. u32 pin_based_exec_ctrl;
  187. u32 cpu_based_exec_ctrl;
  188. u32 cpu_based_2nd_exec_ctrl;
  189. u32 vmexit_ctrl;
  190. u32 vmentry_ctrl;
  191. } vmcs_config;
  192. static struct vmx_capability {
  193. u32 ept;
  194. u32 vpid;
  195. } vmx_capability;
  196. #define VMX_SEGMENT_FIELD(seg) \
  197. [VCPU_SREG_##seg] = { \
  198. .selector = GUEST_##seg##_SELECTOR, \
  199. .base = GUEST_##seg##_BASE, \
  200. .limit = GUEST_##seg##_LIMIT, \
  201. .ar_bytes = GUEST_##seg##_AR_BYTES, \
  202. }
  203. static struct kvm_vmx_segment_field {
  204. unsigned selector;
  205. unsigned base;
  206. unsigned limit;
  207. unsigned ar_bytes;
  208. } kvm_vmx_segment_fields[] = {
  209. VMX_SEGMENT_FIELD(CS),
  210. VMX_SEGMENT_FIELD(DS),
  211. VMX_SEGMENT_FIELD(ES),
  212. VMX_SEGMENT_FIELD(FS),
  213. VMX_SEGMENT_FIELD(GS),
  214. VMX_SEGMENT_FIELD(SS),
  215. VMX_SEGMENT_FIELD(TR),
  216. VMX_SEGMENT_FIELD(LDTR),
  217. };
  218. static u64 host_efer;
  219. static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
  220. /*
  221. * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
  222. * away by decrementing the array size.
  223. */
  224. static const u32 vmx_msr_index[] = {
  225. #ifdef CONFIG_X86_64
  226. MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
  227. #endif
  228. MSR_EFER, MSR_TSC_AUX, MSR_STAR,
  229. };
  230. #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
  231. static inline bool is_page_fault(u32 intr_info)
  232. {
  233. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  234. INTR_INFO_VALID_MASK)) ==
  235. (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
  236. }
  237. static inline bool is_no_device(u32 intr_info)
  238. {
  239. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  240. INTR_INFO_VALID_MASK)) ==
  241. (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
  242. }
  243. static inline bool is_invalid_opcode(u32 intr_info)
  244. {
  245. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  246. INTR_INFO_VALID_MASK)) ==
  247. (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
  248. }
  249. static inline bool is_external_interrupt(u32 intr_info)
  250. {
  251. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
  252. == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
  253. }
  254. static inline bool is_machine_check(u32 intr_info)
  255. {
  256. return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
  257. INTR_INFO_VALID_MASK)) ==
  258. (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
  259. }
  260. static inline bool cpu_has_vmx_msr_bitmap(void)
  261. {
  262. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
  263. }
  264. static inline bool cpu_has_vmx_tpr_shadow(void)
  265. {
  266. return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
  267. }
  268. static inline bool vm_need_tpr_shadow(struct kvm *kvm)
  269. {
  270. return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
  271. }
  272. static inline bool cpu_has_secondary_exec_ctrls(void)
  273. {
  274. return vmcs_config.cpu_based_exec_ctrl &
  275. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  276. }
  277. static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
  278. {
  279. return vmcs_config.cpu_based_2nd_exec_ctrl &
  280. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  281. }
  282. static inline bool cpu_has_vmx_flexpriority(void)
  283. {
  284. return cpu_has_vmx_tpr_shadow() &&
  285. cpu_has_vmx_virtualize_apic_accesses();
  286. }
  287. static inline bool cpu_has_vmx_ept_execute_only(void)
  288. {
  289. return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
  290. }
  291. static inline bool cpu_has_vmx_eptp_uncacheable(void)
  292. {
  293. return vmx_capability.ept & VMX_EPTP_UC_BIT;
  294. }
  295. static inline bool cpu_has_vmx_eptp_writeback(void)
  296. {
  297. return vmx_capability.ept & VMX_EPTP_WB_BIT;
  298. }
  299. static inline bool cpu_has_vmx_ept_2m_page(void)
  300. {
  301. return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
  302. }
  303. static inline bool cpu_has_vmx_ept_1g_page(void)
  304. {
  305. return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
  306. }
  307. static inline bool cpu_has_vmx_ept_4levels(void)
  308. {
  309. return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
  310. }
  311. static inline bool cpu_has_vmx_invept_individual_addr(void)
  312. {
  313. return vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT;
  314. }
  315. static inline bool cpu_has_vmx_invept_context(void)
  316. {
  317. return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
  318. }
  319. static inline bool cpu_has_vmx_invept_global(void)
  320. {
  321. return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
  322. }
  323. static inline bool cpu_has_vmx_invvpid_single(void)
  324. {
  325. return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
  326. }
  327. static inline bool cpu_has_vmx_invvpid_global(void)
  328. {
  329. return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
  330. }
  331. static inline bool cpu_has_vmx_ept(void)
  332. {
  333. return vmcs_config.cpu_based_2nd_exec_ctrl &
  334. SECONDARY_EXEC_ENABLE_EPT;
  335. }
  336. static inline bool cpu_has_vmx_unrestricted_guest(void)
  337. {
  338. return vmcs_config.cpu_based_2nd_exec_ctrl &
  339. SECONDARY_EXEC_UNRESTRICTED_GUEST;
  340. }
  341. static inline bool cpu_has_vmx_ple(void)
  342. {
  343. return vmcs_config.cpu_based_2nd_exec_ctrl &
  344. SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  345. }
  346. static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm)
  347. {
  348. return flexpriority_enabled && irqchip_in_kernel(kvm);
  349. }
  350. static inline bool cpu_has_vmx_vpid(void)
  351. {
  352. return vmcs_config.cpu_based_2nd_exec_ctrl &
  353. SECONDARY_EXEC_ENABLE_VPID;
  354. }
  355. static inline bool cpu_has_vmx_rdtscp(void)
  356. {
  357. return vmcs_config.cpu_based_2nd_exec_ctrl &
  358. SECONDARY_EXEC_RDTSCP;
  359. }
  360. static inline bool cpu_has_virtual_nmis(void)
  361. {
  362. return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
  363. }
  364. static inline bool cpu_has_vmx_wbinvd_exit(void)
  365. {
  366. return vmcs_config.cpu_based_2nd_exec_ctrl &
  367. SECONDARY_EXEC_WBINVD_EXITING;
  368. }
  369. static inline bool report_flexpriority(void)
  370. {
  371. return flexpriority_enabled;
  372. }
  373. static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
  374. {
  375. int i;
  376. for (i = 0; i < vmx->nmsrs; ++i)
  377. if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
  378. return i;
  379. return -1;
  380. }
  381. static inline void __invvpid(int ext, u16 vpid, gva_t gva)
  382. {
  383. struct {
  384. u64 vpid : 16;
  385. u64 rsvd : 48;
  386. u64 gva;
  387. } operand = { vpid, 0, gva };
  388. asm volatile (__ex(ASM_VMX_INVVPID)
  389. /* CF==1 or ZF==1 --> rc = -1 */
  390. "; ja 1f ; ud2 ; 1:"
  391. : : "a"(&operand), "c"(ext) : "cc", "memory");
  392. }
  393. static inline void __invept(int ext, u64 eptp, gpa_t gpa)
  394. {
  395. struct {
  396. u64 eptp, gpa;
  397. } operand = {eptp, gpa};
  398. asm volatile (__ex(ASM_VMX_INVEPT)
  399. /* CF==1 or ZF==1 --> rc = -1 */
  400. "; ja 1f ; ud2 ; 1:\n"
  401. : : "a" (&operand), "c" (ext) : "cc", "memory");
  402. }
  403. static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
  404. {
  405. int i;
  406. i = __find_msr_index(vmx, msr);
  407. if (i >= 0)
  408. return &vmx->guest_msrs[i];
  409. return NULL;
  410. }
  411. static void vmcs_clear(struct vmcs *vmcs)
  412. {
  413. u64 phys_addr = __pa(vmcs);
  414. u8 error;
  415. asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
  416. : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
  417. : "cc", "memory");
  418. if (error)
  419. printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
  420. vmcs, phys_addr);
  421. }
  422. static void vmcs_load(struct vmcs *vmcs)
  423. {
  424. u64 phys_addr = __pa(vmcs);
  425. u8 error;
  426. asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
  427. : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
  428. : "cc", "memory");
  429. if (error)
  430. printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
  431. vmcs, phys_addr);
  432. }
  433. static void __vcpu_clear(void *arg)
  434. {
  435. struct vcpu_vmx *vmx = arg;
  436. int cpu = raw_smp_processor_id();
  437. if (vmx->vcpu.cpu == cpu)
  438. vmcs_clear(vmx->vmcs);
  439. if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
  440. per_cpu(current_vmcs, cpu) = NULL;
  441. list_del(&vmx->local_vcpus_link);
  442. vmx->vcpu.cpu = -1;
  443. vmx->launched = 0;
  444. }
  445. static void vcpu_clear(struct vcpu_vmx *vmx)
  446. {
  447. if (vmx->vcpu.cpu == -1)
  448. return;
  449. smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
  450. }
  451. static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx)
  452. {
  453. if (vmx->vpid == 0)
  454. return;
  455. if (cpu_has_vmx_invvpid_single())
  456. __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
  457. }
  458. static inline void vpid_sync_vcpu_global(void)
  459. {
  460. if (cpu_has_vmx_invvpid_global())
  461. __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
  462. }
  463. static inline void vpid_sync_context(struct vcpu_vmx *vmx)
  464. {
  465. if (cpu_has_vmx_invvpid_single())
  466. vpid_sync_vcpu_single(vmx);
  467. else
  468. vpid_sync_vcpu_global();
  469. }
  470. static inline void ept_sync_global(void)
  471. {
  472. if (cpu_has_vmx_invept_global())
  473. __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
  474. }
  475. static inline void ept_sync_context(u64 eptp)
  476. {
  477. if (enable_ept) {
  478. if (cpu_has_vmx_invept_context())
  479. __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
  480. else
  481. ept_sync_global();
  482. }
  483. }
  484. static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
  485. {
  486. if (enable_ept) {
  487. if (cpu_has_vmx_invept_individual_addr())
  488. __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
  489. eptp, gpa);
  490. else
  491. ept_sync_context(eptp);
  492. }
  493. }
  494. static unsigned long vmcs_readl(unsigned long field)
  495. {
  496. unsigned long value = 0;
  497. asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
  498. : "+a"(value) : "d"(field) : "cc");
  499. return value;
  500. }
  501. static u16 vmcs_read16(unsigned long field)
  502. {
  503. return vmcs_readl(field);
  504. }
  505. static u32 vmcs_read32(unsigned long field)
  506. {
  507. return vmcs_readl(field);
  508. }
  509. static u64 vmcs_read64(unsigned long field)
  510. {
  511. #ifdef CONFIG_X86_64
  512. return vmcs_readl(field);
  513. #else
  514. return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
  515. #endif
  516. }
  517. static noinline void vmwrite_error(unsigned long field, unsigned long value)
  518. {
  519. printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
  520. field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
  521. dump_stack();
  522. }
  523. static void vmcs_writel(unsigned long field, unsigned long value)
  524. {
  525. u8 error;
  526. asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
  527. : "=q"(error) : "a"(value), "d"(field) : "cc");
  528. if (unlikely(error))
  529. vmwrite_error(field, value);
  530. }
  531. static void vmcs_write16(unsigned long field, u16 value)
  532. {
  533. vmcs_writel(field, value);
  534. }
  535. static void vmcs_write32(unsigned long field, u32 value)
  536. {
  537. vmcs_writel(field, value);
  538. }
  539. static void vmcs_write64(unsigned long field, u64 value)
  540. {
  541. vmcs_writel(field, value);
  542. #ifndef CONFIG_X86_64
  543. asm volatile ("");
  544. vmcs_writel(field+1, value >> 32);
  545. #endif
  546. }
  547. static void vmcs_clear_bits(unsigned long field, u32 mask)
  548. {
  549. vmcs_writel(field, vmcs_readl(field) & ~mask);
  550. }
  551. static void vmcs_set_bits(unsigned long field, u32 mask)
  552. {
  553. vmcs_writel(field, vmcs_readl(field) | mask);
  554. }
  555. static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
  556. {
  557. vmx->segment_cache.bitmask = 0;
  558. }
  559. static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
  560. unsigned field)
  561. {
  562. bool ret;
  563. u32 mask = 1 << (seg * SEG_FIELD_NR + field);
  564. if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
  565. vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
  566. vmx->segment_cache.bitmask = 0;
  567. }
  568. ret = vmx->segment_cache.bitmask & mask;
  569. vmx->segment_cache.bitmask |= mask;
  570. return ret;
  571. }
  572. static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
  573. {
  574. u16 *p = &vmx->segment_cache.seg[seg].selector;
  575. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
  576. *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
  577. return *p;
  578. }
  579. static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
  580. {
  581. ulong *p = &vmx->segment_cache.seg[seg].base;
  582. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
  583. *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
  584. return *p;
  585. }
  586. static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
  587. {
  588. u32 *p = &vmx->segment_cache.seg[seg].limit;
  589. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
  590. *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
  591. return *p;
  592. }
  593. static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
  594. {
  595. u32 *p = &vmx->segment_cache.seg[seg].ar;
  596. if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
  597. *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
  598. return *p;
  599. }
  600. static void update_exception_bitmap(struct kvm_vcpu *vcpu)
  601. {
  602. u32 eb;
  603. eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
  604. (1u << NM_VECTOR) | (1u << DB_VECTOR);
  605. if ((vcpu->guest_debug &
  606. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
  607. (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
  608. eb |= 1u << BP_VECTOR;
  609. if (to_vmx(vcpu)->rmode.vm86_active)
  610. eb = ~0;
  611. if (enable_ept)
  612. eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
  613. if (vcpu->fpu_active)
  614. eb &= ~(1u << NM_VECTOR);
  615. vmcs_write32(EXCEPTION_BITMAP, eb);
  616. }
  617. static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
  618. {
  619. unsigned i;
  620. struct msr_autoload *m = &vmx->msr_autoload;
  621. if (msr == MSR_EFER && cpu_has_load_ia32_efer) {
  622. vmcs_clear_bits(VM_ENTRY_CONTROLS, VM_ENTRY_LOAD_IA32_EFER);
  623. vmcs_clear_bits(VM_EXIT_CONTROLS, VM_EXIT_LOAD_IA32_EFER);
  624. return;
  625. }
  626. for (i = 0; i < m->nr; ++i)
  627. if (m->guest[i].index == msr)
  628. break;
  629. if (i == m->nr)
  630. return;
  631. --m->nr;
  632. m->guest[i] = m->guest[m->nr];
  633. m->host[i] = m->host[m->nr];
  634. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
  635. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
  636. }
  637. static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
  638. u64 guest_val, u64 host_val)
  639. {
  640. unsigned i;
  641. struct msr_autoload *m = &vmx->msr_autoload;
  642. if (msr == MSR_EFER && cpu_has_load_ia32_efer) {
  643. vmcs_write64(GUEST_IA32_EFER, guest_val);
  644. vmcs_write64(HOST_IA32_EFER, host_val);
  645. vmcs_set_bits(VM_ENTRY_CONTROLS, VM_ENTRY_LOAD_IA32_EFER);
  646. vmcs_set_bits(VM_EXIT_CONTROLS, VM_EXIT_LOAD_IA32_EFER);
  647. return;
  648. }
  649. for (i = 0; i < m->nr; ++i)
  650. if (m->guest[i].index == msr)
  651. break;
  652. if (i == m->nr) {
  653. ++m->nr;
  654. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
  655. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
  656. }
  657. m->guest[i].index = msr;
  658. m->guest[i].value = guest_val;
  659. m->host[i].index = msr;
  660. m->host[i].value = host_val;
  661. }
  662. static void reload_tss(void)
  663. {
  664. /*
  665. * VT restores TR but not its size. Useless.
  666. */
  667. struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
  668. struct desc_struct *descs;
  669. descs = (void *)gdt->address;
  670. descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
  671. load_TR_desc();
  672. }
  673. static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
  674. {
  675. u64 guest_efer;
  676. u64 ignore_bits;
  677. guest_efer = vmx->vcpu.arch.efer;
  678. /*
  679. * NX is emulated; LMA and LME handled by hardware; SCE meaninless
  680. * outside long mode
  681. */
  682. ignore_bits = EFER_NX | EFER_SCE;
  683. #ifdef CONFIG_X86_64
  684. ignore_bits |= EFER_LMA | EFER_LME;
  685. /* SCE is meaningful only in long mode on Intel */
  686. if (guest_efer & EFER_LMA)
  687. ignore_bits &= ~(u64)EFER_SCE;
  688. #endif
  689. guest_efer &= ~ignore_bits;
  690. guest_efer |= host_efer & ignore_bits;
  691. vmx->guest_msrs[efer_offset].data = guest_efer;
  692. vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
  693. clear_atomic_switch_msr(vmx, MSR_EFER);
  694. /* On ept, can't emulate nx, and must switch nx atomically */
  695. if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) {
  696. guest_efer = vmx->vcpu.arch.efer;
  697. if (!(guest_efer & EFER_LMA))
  698. guest_efer &= ~EFER_LME;
  699. add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer);
  700. return false;
  701. }
  702. return true;
  703. }
  704. static unsigned long segment_base(u16 selector)
  705. {
  706. struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
  707. struct desc_struct *d;
  708. unsigned long table_base;
  709. unsigned long v;
  710. if (!(selector & ~3))
  711. return 0;
  712. table_base = gdt->address;
  713. if (selector & 4) { /* from ldt */
  714. u16 ldt_selector = kvm_read_ldt();
  715. if (!(ldt_selector & ~3))
  716. return 0;
  717. table_base = segment_base(ldt_selector);
  718. }
  719. d = (struct desc_struct *)(table_base + (selector & ~7));
  720. v = get_desc_base(d);
  721. #ifdef CONFIG_X86_64
  722. if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
  723. v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
  724. #endif
  725. return v;
  726. }
  727. static inline unsigned long kvm_read_tr_base(void)
  728. {
  729. u16 tr;
  730. asm("str %0" : "=g"(tr));
  731. return segment_base(tr);
  732. }
  733. static void vmx_save_host_state(struct kvm_vcpu *vcpu)
  734. {
  735. struct vcpu_vmx *vmx = to_vmx(vcpu);
  736. int i;
  737. if (vmx->host_state.loaded)
  738. return;
  739. vmx->host_state.loaded = 1;
  740. /*
  741. * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
  742. * allow segment selectors with cpl > 0 or ti == 1.
  743. */
  744. vmx->host_state.ldt_sel = kvm_read_ldt();
  745. vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
  746. savesegment(fs, vmx->host_state.fs_sel);
  747. if (!(vmx->host_state.fs_sel & 7)) {
  748. vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
  749. vmx->host_state.fs_reload_needed = 0;
  750. } else {
  751. vmcs_write16(HOST_FS_SELECTOR, 0);
  752. vmx->host_state.fs_reload_needed = 1;
  753. }
  754. savesegment(gs, vmx->host_state.gs_sel);
  755. if (!(vmx->host_state.gs_sel & 7))
  756. vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
  757. else {
  758. vmcs_write16(HOST_GS_SELECTOR, 0);
  759. vmx->host_state.gs_ldt_reload_needed = 1;
  760. }
  761. #ifdef CONFIG_X86_64
  762. vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
  763. vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
  764. #else
  765. vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
  766. vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
  767. #endif
  768. #ifdef CONFIG_X86_64
  769. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  770. if (is_long_mode(&vmx->vcpu))
  771. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  772. #endif
  773. for (i = 0; i < vmx->save_nmsrs; ++i)
  774. kvm_set_shared_msr(vmx->guest_msrs[i].index,
  775. vmx->guest_msrs[i].data,
  776. vmx->guest_msrs[i].mask);
  777. }
  778. static void __vmx_load_host_state(struct vcpu_vmx *vmx)
  779. {
  780. if (!vmx->host_state.loaded)
  781. return;
  782. ++vmx->vcpu.stat.host_state_reload;
  783. vmx->host_state.loaded = 0;
  784. #ifdef CONFIG_X86_64
  785. if (is_long_mode(&vmx->vcpu))
  786. rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
  787. #endif
  788. if (vmx->host_state.gs_ldt_reload_needed) {
  789. kvm_load_ldt(vmx->host_state.ldt_sel);
  790. #ifdef CONFIG_X86_64
  791. load_gs_index(vmx->host_state.gs_sel);
  792. #else
  793. loadsegment(gs, vmx->host_state.gs_sel);
  794. #endif
  795. }
  796. if (vmx->host_state.fs_reload_needed)
  797. loadsegment(fs, vmx->host_state.fs_sel);
  798. reload_tss();
  799. #ifdef CONFIG_X86_64
  800. wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
  801. #endif
  802. if (__thread_has_fpu(current))
  803. clts();
  804. load_gdt(&__get_cpu_var(host_gdt));
  805. }
  806. static void vmx_load_host_state(struct vcpu_vmx *vmx)
  807. {
  808. preempt_disable();
  809. __vmx_load_host_state(vmx);
  810. preempt_enable();
  811. }
  812. /*
  813. * Switches to specified vcpu, until a matching vcpu_put(), but assumes
  814. * vcpu mutex is already taken.
  815. */
  816. static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  817. {
  818. struct vcpu_vmx *vmx = to_vmx(vcpu);
  819. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  820. if (!vmm_exclusive)
  821. kvm_cpu_vmxon(phys_addr);
  822. else if (vcpu->cpu != cpu)
  823. vcpu_clear(vmx);
  824. if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
  825. per_cpu(current_vmcs, cpu) = vmx->vmcs;
  826. vmcs_load(vmx->vmcs);
  827. }
  828. if (vcpu->cpu != cpu) {
  829. struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
  830. unsigned long sysenter_esp;
  831. kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
  832. local_irq_disable();
  833. list_add(&vmx->local_vcpus_link,
  834. &per_cpu(vcpus_on_cpu, cpu));
  835. local_irq_enable();
  836. /*
  837. * Linux uses per-cpu TSS and GDT, so set these when switching
  838. * processors.
  839. */
  840. vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
  841. vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
  842. rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
  843. vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
  844. }
  845. }
  846. static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
  847. {
  848. __vmx_load_host_state(to_vmx(vcpu));
  849. if (!vmm_exclusive) {
  850. __vcpu_clear(to_vmx(vcpu));
  851. kvm_cpu_vmxoff();
  852. }
  853. }
  854. static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
  855. {
  856. ulong cr0;
  857. if (vcpu->fpu_active)
  858. return;
  859. vcpu->fpu_active = 1;
  860. cr0 = vmcs_readl(GUEST_CR0);
  861. cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
  862. cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
  863. vmcs_writel(GUEST_CR0, cr0);
  864. update_exception_bitmap(vcpu);
  865. vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
  866. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  867. }
  868. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
  869. static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
  870. {
  871. vmx_decache_cr0_guest_bits(vcpu);
  872. vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
  873. update_exception_bitmap(vcpu);
  874. vcpu->arch.cr0_guest_owned_bits = 0;
  875. vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
  876. vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
  877. }
  878. static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
  879. {
  880. unsigned long rflags, save_rflags;
  881. if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
  882. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  883. rflags = vmcs_readl(GUEST_RFLAGS);
  884. if (to_vmx(vcpu)->rmode.vm86_active) {
  885. rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  886. save_rflags = to_vmx(vcpu)->rmode.save_rflags;
  887. rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  888. }
  889. to_vmx(vcpu)->rflags = rflags;
  890. }
  891. return to_vmx(vcpu)->rflags;
  892. }
  893. static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  894. {
  895. __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
  896. __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
  897. to_vmx(vcpu)->rflags = rflags;
  898. if (to_vmx(vcpu)->rmode.vm86_active) {
  899. to_vmx(vcpu)->rmode.save_rflags = rflags;
  900. rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  901. }
  902. vmcs_writel(GUEST_RFLAGS, rflags);
  903. }
  904. static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  905. {
  906. u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  907. int ret = 0;
  908. if (interruptibility & GUEST_INTR_STATE_STI)
  909. ret |= KVM_X86_SHADOW_INT_STI;
  910. if (interruptibility & GUEST_INTR_STATE_MOV_SS)
  911. ret |= KVM_X86_SHADOW_INT_MOV_SS;
  912. return ret & mask;
  913. }
  914. static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  915. {
  916. u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
  917. u32 interruptibility = interruptibility_old;
  918. interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
  919. if (mask & KVM_X86_SHADOW_INT_MOV_SS)
  920. interruptibility |= GUEST_INTR_STATE_MOV_SS;
  921. else if (mask & KVM_X86_SHADOW_INT_STI)
  922. interruptibility |= GUEST_INTR_STATE_STI;
  923. if ((interruptibility != interruptibility_old))
  924. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
  925. }
  926. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  927. {
  928. unsigned long rip;
  929. rip = kvm_rip_read(vcpu);
  930. rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  931. kvm_rip_write(vcpu, rip);
  932. /* skipping an emulated instruction also counts */
  933. vmx_set_interrupt_shadow(vcpu, 0);
  934. }
  935. static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
  936. {
  937. /* Ensure that we clear the HLT state in the VMCS. We don't need to
  938. * explicitly skip the instruction because if the HLT state is set, then
  939. * the instruction is already executing and RIP has already been
  940. * advanced. */
  941. if (!yield_on_hlt &&
  942. vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
  943. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  944. }
  945. static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
  946. bool has_error_code, u32 error_code,
  947. bool reinject)
  948. {
  949. struct vcpu_vmx *vmx = to_vmx(vcpu);
  950. u32 intr_info = nr | INTR_INFO_VALID_MASK;
  951. if (has_error_code) {
  952. vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
  953. intr_info |= INTR_INFO_DELIVER_CODE_MASK;
  954. }
  955. if (vmx->rmode.vm86_active) {
  956. int inc_eip = 0;
  957. if (kvm_exception_is_soft(nr))
  958. inc_eip = vcpu->arch.event_exit_inst_len;
  959. if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
  960. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  961. return;
  962. }
  963. if (kvm_exception_is_soft(nr)) {
  964. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  965. vmx->vcpu.arch.event_exit_inst_len);
  966. intr_info |= INTR_TYPE_SOFT_EXCEPTION;
  967. } else
  968. intr_info |= INTR_TYPE_HARD_EXCEPTION;
  969. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
  970. vmx_clear_hlt(vcpu);
  971. }
  972. static bool vmx_rdtscp_supported(void)
  973. {
  974. return cpu_has_vmx_rdtscp();
  975. }
  976. /*
  977. * Swap MSR entry in host/guest MSR entry array.
  978. */
  979. static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
  980. {
  981. struct shared_msr_entry tmp;
  982. tmp = vmx->guest_msrs[to];
  983. vmx->guest_msrs[to] = vmx->guest_msrs[from];
  984. vmx->guest_msrs[from] = tmp;
  985. }
  986. /*
  987. * Set up the vmcs to automatically save and restore system
  988. * msrs. Don't touch the 64-bit msrs if the guest is in legacy
  989. * mode, as fiddling with msrs is very expensive.
  990. */
  991. static void setup_msrs(struct vcpu_vmx *vmx)
  992. {
  993. int save_nmsrs, index;
  994. unsigned long *msr_bitmap;
  995. vmx_load_host_state(vmx);
  996. save_nmsrs = 0;
  997. #ifdef CONFIG_X86_64
  998. if (is_long_mode(&vmx->vcpu)) {
  999. index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
  1000. if (index >= 0)
  1001. move_msr_up(vmx, index, save_nmsrs++);
  1002. index = __find_msr_index(vmx, MSR_LSTAR);
  1003. if (index >= 0)
  1004. move_msr_up(vmx, index, save_nmsrs++);
  1005. index = __find_msr_index(vmx, MSR_CSTAR);
  1006. if (index >= 0)
  1007. move_msr_up(vmx, index, save_nmsrs++);
  1008. index = __find_msr_index(vmx, MSR_TSC_AUX);
  1009. if (index >= 0 && vmx->rdtscp_enabled)
  1010. move_msr_up(vmx, index, save_nmsrs++);
  1011. /*
  1012. * MSR_STAR is only needed on long mode guests, and only
  1013. * if efer.sce is enabled.
  1014. */
  1015. index = __find_msr_index(vmx, MSR_STAR);
  1016. if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
  1017. move_msr_up(vmx, index, save_nmsrs++);
  1018. }
  1019. #endif
  1020. index = __find_msr_index(vmx, MSR_EFER);
  1021. if (index >= 0 && update_transition_efer(vmx, index))
  1022. move_msr_up(vmx, index, save_nmsrs++);
  1023. vmx->save_nmsrs = save_nmsrs;
  1024. if (cpu_has_vmx_msr_bitmap()) {
  1025. if (is_long_mode(&vmx->vcpu))
  1026. msr_bitmap = vmx_msr_bitmap_longmode;
  1027. else
  1028. msr_bitmap = vmx_msr_bitmap_legacy;
  1029. vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
  1030. }
  1031. }
  1032. /*
  1033. * reads and returns guest's timestamp counter "register"
  1034. * guest_tsc = host_tsc + tsc_offset -- 21.3
  1035. */
  1036. static u64 guest_read_tsc(void)
  1037. {
  1038. u64 host_tsc, tsc_offset;
  1039. rdtscll(host_tsc);
  1040. tsc_offset = vmcs_read64(TSC_OFFSET);
  1041. return host_tsc + tsc_offset;
  1042. }
  1043. /*
  1044. * Empty call-back. Needs to be implemented when VMX enables the SET_TSC_KHZ
  1045. * ioctl. In this case the call-back should update internal vmx state to make
  1046. * the changes effective.
  1047. */
  1048. static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
  1049. {
  1050. /* Nothing to do here */
  1051. }
  1052. /*
  1053. * writes 'offset' into guest's timestamp counter offset register
  1054. */
  1055. static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
  1056. {
  1057. vmcs_write64(TSC_OFFSET, offset);
  1058. }
  1059. static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
  1060. {
  1061. u64 offset = vmcs_read64(TSC_OFFSET);
  1062. vmcs_write64(TSC_OFFSET, offset + adjustment);
  1063. }
  1064. static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
  1065. {
  1066. return target_tsc - native_read_tsc();
  1067. }
  1068. /*
  1069. * Reads an msr value (of 'msr_index') into 'pdata'.
  1070. * Returns 0 on success, non-0 otherwise.
  1071. * Assumes vcpu_load() was already called.
  1072. */
  1073. static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  1074. {
  1075. u64 data;
  1076. struct shared_msr_entry *msr;
  1077. if (!pdata) {
  1078. printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
  1079. return -EINVAL;
  1080. }
  1081. switch (msr_index) {
  1082. #ifdef CONFIG_X86_64
  1083. case MSR_FS_BASE:
  1084. data = vmcs_readl(GUEST_FS_BASE);
  1085. break;
  1086. case MSR_GS_BASE:
  1087. data = vmcs_readl(GUEST_GS_BASE);
  1088. break;
  1089. case MSR_KERNEL_GS_BASE:
  1090. vmx_load_host_state(to_vmx(vcpu));
  1091. data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
  1092. break;
  1093. #endif
  1094. case MSR_EFER:
  1095. return kvm_get_msr_common(vcpu, msr_index, pdata);
  1096. case MSR_IA32_TSC:
  1097. data = guest_read_tsc();
  1098. break;
  1099. case MSR_IA32_SYSENTER_CS:
  1100. data = vmcs_read32(GUEST_SYSENTER_CS);
  1101. break;
  1102. case MSR_IA32_SYSENTER_EIP:
  1103. data = vmcs_readl(GUEST_SYSENTER_EIP);
  1104. break;
  1105. case MSR_IA32_SYSENTER_ESP:
  1106. data = vmcs_readl(GUEST_SYSENTER_ESP);
  1107. break;
  1108. case MSR_TSC_AUX:
  1109. if (!to_vmx(vcpu)->rdtscp_enabled)
  1110. return 1;
  1111. /* Otherwise falls through */
  1112. default:
  1113. vmx_load_host_state(to_vmx(vcpu));
  1114. msr = find_msr_entry(to_vmx(vcpu), msr_index);
  1115. if (msr) {
  1116. vmx_load_host_state(to_vmx(vcpu));
  1117. data = msr->data;
  1118. break;
  1119. }
  1120. return kvm_get_msr_common(vcpu, msr_index, pdata);
  1121. }
  1122. *pdata = data;
  1123. return 0;
  1124. }
  1125. /*
  1126. * Writes msr value into into the appropriate "register".
  1127. * Returns 0 on success, non-0 otherwise.
  1128. * Assumes vcpu_load() was already called.
  1129. */
  1130. static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  1131. {
  1132. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1133. struct shared_msr_entry *msr;
  1134. int ret = 0;
  1135. switch (msr_index) {
  1136. case MSR_EFER:
  1137. vmx_load_host_state(vmx);
  1138. ret = kvm_set_msr_common(vcpu, msr_index, data);
  1139. break;
  1140. #ifdef CONFIG_X86_64
  1141. case MSR_FS_BASE:
  1142. vmx_segment_cache_clear(vmx);
  1143. vmcs_writel(GUEST_FS_BASE, data);
  1144. break;
  1145. case MSR_GS_BASE:
  1146. vmx_segment_cache_clear(vmx);
  1147. vmcs_writel(GUEST_GS_BASE, data);
  1148. break;
  1149. case MSR_KERNEL_GS_BASE:
  1150. vmx_load_host_state(vmx);
  1151. vmx->msr_guest_kernel_gs_base = data;
  1152. break;
  1153. #endif
  1154. case MSR_IA32_SYSENTER_CS:
  1155. vmcs_write32(GUEST_SYSENTER_CS, data);
  1156. break;
  1157. case MSR_IA32_SYSENTER_EIP:
  1158. vmcs_writel(GUEST_SYSENTER_EIP, data);
  1159. break;
  1160. case MSR_IA32_SYSENTER_ESP:
  1161. vmcs_writel(GUEST_SYSENTER_ESP, data);
  1162. break;
  1163. case MSR_IA32_TSC:
  1164. kvm_write_tsc(vcpu, data);
  1165. break;
  1166. case MSR_IA32_CR_PAT:
  1167. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  1168. vmcs_write64(GUEST_IA32_PAT, data);
  1169. vcpu->arch.pat = data;
  1170. break;
  1171. }
  1172. ret = kvm_set_msr_common(vcpu, msr_index, data);
  1173. break;
  1174. case MSR_TSC_AUX:
  1175. if (!vmx->rdtscp_enabled)
  1176. return 1;
  1177. /* Check reserved bit, higher 32 bits should be zero */
  1178. if ((data >> 32) != 0)
  1179. return 1;
  1180. /* Otherwise falls through */
  1181. default:
  1182. msr = find_msr_entry(vmx, msr_index);
  1183. if (msr) {
  1184. vmx_load_host_state(vmx);
  1185. msr->data = data;
  1186. break;
  1187. }
  1188. ret = kvm_set_msr_common(vcpu, msr_index, data);
  1189. }
  1190. return ret;
  1191. }
  1192. static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  1193. {
  1194. __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
  1195. switch (reg) {
  1196. case VCPU_REGS_RSP:
  1197. vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
  1198. break;
  1199. case VCPU_REGS_RIP:
  1200. vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
  1201. break;
  1202. case VCPU_EXREG_PDPTR:
  1203. if (enable_ept)
  1204. ept_save_pdptrs(vcpu);
  1205. break;
  1206. default:
  1207. break;
  1208. }
  1209. }
  1210. static void set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
  1211. {
  1212. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
  1213. vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
  1214. else
  1215. vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
  1216. update_exception_bitmap(vcpu);
  1217. }
  1218. static __init int cpu_has_kvm_support(void)
  1219. {
  1220. return cpu_has_vmx();
  1221. }
  1222. static __init int vmx_disabled_by_bios(void)
  1223. {
  1224. u64 msr;
  1225. rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
  1226. if (msr & FEATURE_CONTROL_LOCKED) {
  1227. /* launched w/ TXT and VMX disabled */
  1228. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  1229. && tboot_enabled())
  1230. return 1;
  1231. /* launched w/o TXT and VMX only enabled w/ TXT */
  1232. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  1233. && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
  1234. && !tboot_enabled()) {
  1235. printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
  1236. "activate TXT before enabling KVM\n");
  1237. return 1;
  1238. }
  1239. /* launched w/o TXT and VMX disabled */
  1240. if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
  1241. && !tboot_enabled())
  1242. return 1;
  1243. }
  1244. return 0;
  1245. }
  1246. static void kvm_cpu_vmxon(u64 addr)
  1247. {
  1248. asm volatile (ASM_VMX_VMXON_RAX
  1249. : : "a"(&addr), "m"(addr)
  1250. : "memory", "cc");
  1251. }
  1252. static int hardware_enable(void *garbage)
  1253. {
  1254. int cpu = raw_smp_processor_id();
  1255. u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
  1256. u64 old, test_bits;
  1257. if (read_cr4() & X86_CR4_VMXE)
  1258. return -EBUSY;
  1259. INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
  1260. rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
  1261. test_bits = FEATURE_CONTROL_LOCKED;
  1262. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
  1263. if (tboot_enabled())
  1264. test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
  1265. if ((old & test_bits) != test_bits) {
  1266. /* enable and lock */
  1267. wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
  1268. }
  1269. write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
  1270. if (vmm_exclusive) {
  1271. kvm_cpu_vmxon(phys_addr);
  1272. ept_sync_global();
  1273. }
  1274. store_gdt(&__get_cpu_var(host_gdt));
  1275. return 0;
  1276. }
  1277. static void vmclear_local_vcpus(void)
  1278. {
  1279. int cpu = raw_smp_processor_id();
  1280. struct vcpu_vmx *vmx, *n;
  1281. list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
  1282. local_vcpus_link)
  1283. __vcpu_clear(vmx);
  1284. }
  1285. /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
  1286. * tricks.
  1287. */
  1288. static void kvm_cpu_vmxoff(void)
  1289. {
  1290. asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
  1291. }
  1292. static void hardware_disable(void *garbage)
  1293. {
  1294. if (vmm_exclusive) {
  1295. vmclear_local_vcpus();
  1296. kvm_cpu_vmxoff();
  1297. }
  1298. write_cr4(read_cr4() & ~X86_CR4_VMXE);
  1299. }
  1300. static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
  1301. u32 msr, u32 *result)
  1302. {
  1303. u32 vmx_msr_low, vmx_msr_high;
  1304. u32 ctl = ctl_min | ctl_opt;
  1305. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  1306. ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
  1307. ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
  1308. /* Ensure minimum (required) set of control bits are supported. */
  1309. if (ctl_min & ~ctl)
  1310. return -EIO;
  1311. *result = ctl;
  1312. return 0;
  1313. }
  1314. static __init bool allow_1_setting(u32 msr, u32 ctl)
  1315. {
  1316. u32 vmx_msr_low, vmx_msr_high;
  1317. rdmsr(msr, vmx_msr_low, vmx_msr_high);
  1318. return vmx_msr_high & ctl;
  1319. }
  1320. static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
  1321. {
  1322. u32 vmx_msr_low, vmx_msr_high;
  1323. u32 min, opt, min2, opt2;
  1324. u32 _pin_based_exec_control = 0;
  1325. u32 _cpu_based_exec_control = 0;
  1326. u32 _cpu_based_2nd_exec_control = 0;
  1327. u32 _vmexit_control = 0;
  1328. u32 _vmentry_control = 0;
  1329. min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
  1330. opt = PIN_BASED_VIRTUAL_NMIS;
  1331. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
  1332. &_pin_based_exec_control) < 0)
  1333. return -EIO;
  1334. min =
  1335. #ifdef CONFIG_X86_64
  1336. CPU_BASED_CR8_LOAD_EXITING |
  1337. CPU_BASED_CR8_STORE_EXITING |
  1338. #endif
  1339. CPU_BASED_CR3_LOAD_EXITING |
  1340. CPU_BASED_CR3_STORE_EXITING |
  1341. CPU_BASED_USE_IO_BITMAPS |
  1342. CPU_BASED_MOV_DR_EXITING |
  1343. CPU_BASED_USE_TSC_OFFSETING |
  1344. CPU_BASED_MWAIT_EXITING |
  1345. CPU_BASED_MONITOR_EXITING |
  1346. CPU_BASED_INVLPG_EXITING;
  1347. if (yield_on_hlt)
  1348. min |= CPU_BASED_HLT_EXITING;
  1349. opt = CPU_BASED_TPR_SHADOW |
  1350. CPU_BASED_USE_MSR_BITMAPS |
  1351. CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
  1352. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
  1353. &_cpu_based_exec_control) < 0)
  1354. return -EIO;
  1355. #ifdef CONFIG_X86_64
  1356. if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
  1357. _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
  1358. ~CPU_BASED_CR8_STORE_EXITING;
  1359. #endif
  1360. if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
  1361. min2 = 0;
  1362. opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
  1363. SECONDARY_EXEC_WBINVD_EXITING |
  1364. SECONDARY_EXEC_ENABLE_VPID |
  1365. SECONDARY_EXEC_ENABLE_EPT |
  1366. SECONDARY_EXEC_UNRESTRICTED_GUEST |
  1367. SECONDARY_EXEC_PAUSE_LOOP_EXITING |
  1368. SECONDARY_EXEC_RDTSCP;
  1369. if (adjust_vmx_controls(min2, opt2,
  1370. MSR_IA32_VMX_PROCBASED_CTLS2,
  1371. &_cpu_based_2nd_exec_control) < 0)
  1372. return -EIO;
  1373. }
  1374. #ifndef CONFIG_X86_64
  1375. if (!(_cpu_based_2nd_exec_control &
  1376. SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
  1377. _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
  1378. #endif
  1379. if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
  1380. /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
  1381. enabled */
  1382. _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
  1383. CPU_BASED_CR3_STORE_EXITING |
  1384. CPU_BASED_INVLPG_EXITING);
  1385. rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
  1386. vmx_capability.ept, vmx_capability.vpid);
  1387. }
  1388. min = 0;
  1389. #ifdef CONFIG_X86_64
  1390. min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
  1391. #endif
  1392. opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
  1393. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
  1394. &_vmexit_control) < 0)
  1395. return -EIO;
  1396. min = 0;
  1397. opt = VM_ENTRY_LOAD_IA32_PAT;
  1398. if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
  1399. &_vmentry_control) < 0)
  1400. return -EIO;
  1401. rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
  1402. /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
  1403. if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
  1404. return -EIO;
  1405. #ifdef CONFIG_X86_64
  1406. /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
  1407. if (vmx_msr_high & (1u<<16))
  1408. return -EIO;
  1409. #endif
  1410. /* Require Write-Back (WB) memory type for VMCS accesses. */
  1411. if (((vmx_msr_high >> 18) & 15) != 6)
  1412. return -EIO;
  1413. vmcs_conf->size = vmx_msr_high & 0x1fff;
  1414. vmcs_conf->order = get_order(vmcs_config.size);
  1415. vmcs_conf->revision_id = vmx_msr_low;
  1416. vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
  1417. vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
  1418. vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
  1419. vmcs_conf->vmexit_ctrl = _vmexit_control;
  1420. vmcs_conf->vmentry_ctrl = _vmentry_control;
  1421. cpu_has_load_ia32_efer =
  1422. allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
  1423. VM_ENTRY_LOAD_IA32_EFER)
  1424. && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
  1425. VM_EXIT_LOAD_IA32_EFER);
  1426. return 0;
  1427. }
  1428. static struct vmcs *alloc_vmcs_cpu(int cpu)
  1429. {
  1430. int node = cpu_to_node(cpu);
  1431. struct page *pages;
  1432. struct vmcs *vmcs;
  1433. pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
  1434. if (!pages)
  1435. return NULL;
  1436. vmcs = page_address(pages);
  1437. memset(vmcs, 0, vmcs_config.size);
  1438. vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
  1439. return vmcs;
  1440. }
  1441. static struct vmcs *alloc_vmcs(void)
  1442. {
  1443. return alloc_vmcs_cpu(raw_smp_processor_id());
  1444. }
  1445. static void free_vmcs(struct vmcs *vmcs)
  1446. {
  1447. free_pages((unsigned long)vmcs, vmcs_config.order);
  1448. }
  1449. static void free_kvm_area(void)
  1450. {
  1451. int cpu;
  1452. for_each_possible_cpu(cpu) {
  1453. free_vmcs(per_cpu(vmxarea, cpu));
  1454. per_cpu(vmxarea, cpu) = NULL;
  1455. }
  1456. }
  1457. static __init int alloc_kvm_area(void)
  1458. {
  1459. int cpu;
  1460. for_each_possible_cpu(cpu) {
  1461. struct vmcs *vmcs;
  1462. vmcs = alloc_vmcs_cpu(cpu);
  1463. if (!vmcs) {
  1464. free_kvm_area();
  1465. return -ENOMEM;
  1466. }
  1467. per_cpu(vmxarea, cpu) = vmcs;
  1468. }
  1469. return 0;
  1470. }
  1471. static __init int hardware_setup(void)
  1472. {
  1473. if (setup_vmcs_config(&vmcs_config) < 0)
  1474. return -EIO;
  1475. if (boot_cpu_has(X86_FEATURE_NX))
  1476. kvm_enable_efer_bits(EFER_NX);
  1477. if (!cpu_has_vmx_vpid())
  1478. enable_vpid = 0;
  1479. if (!cpu_has_vmx_ept() ||
  1480. !cpu_has_vmx_ept_4levels()) {
  1481. enable_ept = 0;
  1482. enable_unrestricted_guest = 0;
  1483. }
  1484. if (!cpu_has_vmx_unrestricted_guest())
  1485. enable_unrestricted_guest = 0;
  1486. if (!cpu_has_vmx_flexpriority())
  1487. flexpriority_enabled = 0;
  1488. if (!cpu_has_vmx_tpr_shadow())
  1489. kvm_x86_ops->update_cr8_intercept = NULL;
  1490. if (enable_ept && !cpu_has_vmx_ept_2m_page())
  1491. kvm_disable_largepages();
  1492. if (!cpu_has_vmx_ple())
  1493. ple_gap = 0;
  1494. return alloc_kvm_area();
  1495. }
  1496. static __exit void hardware_unsetup(void)
  1497. {
  1498. free_kvm_area();
  1499. }
  1500. static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
  1501. {
  1502. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  1503. if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
  1504. vmcs_write16(sf->selector, save->selector);
  1505. vmcs_writel(sf->base, save->base);
  1506. vmcs_write32(sf->limit, save->limit);
  1507. vmcs_write32(sf->ar_bytes, save->ar);
  1508. } else {
  1509. u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
  1510. << AR_DPL_SHIFT;
  1511. vmcs_write32(sf->ar_bytes, 0x93 | dpl);
  1512. }
  1513. }
  1514. static void enter_pmode(struct kvm_vcpu *vcpu)
  1515. {
  1516. unsigned long flags;
  1517. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1518. vmx->emulation_required = 1;
  1519. vmx->rmode.vm86_active = 0;
  1520. vmx_segment_cache_clear(vmx);
  1521. vmcs_write16(GUEST_TR_SELECTOR, vmx->rmode.tr.selector);
  1522. vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base);
  1523. vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit);
  1524. vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar);
  1525. flags = vmcs_readl(GUEST_RFLAGS);
  1526. flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
  1527. flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
  1528. vmcs_writel(GUEST_RFLAGS, flags);
  1529. vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
  1530. (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
  1531. update_exception_bitmap(vcpu);
  1532. if (emulate_invalid_guest_state)
  1533. return;
  1534. fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es);
  1535. fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds);
  1536. fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs);
  1537. fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs);
  1538. vmx_segment_cache_clear(vmx);
  1539. vmcs_write16(GUEST_SS_SELECTOR, 0);
  1540. vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
  1541. vmcs_write16(GUEST_CS_SELECTOR,
  1542. vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
  1543. vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
  1544. }
  1545. static gva_t rmode_tss_base(struct kvm *kvm)
  1546. {
  1547. if (!kvm->arch.tss_addr) {
  1548. struct kvm_memslots *slots;
  1549. gfn_t base_gfn;
  1550. slots = kvm_memslots(kvm);
  1551. base_gfn = slots->memslots[0].base_gfn +
  1552. kvm->memslots->memslots[0].npages - 3;
  1553. return base_gfn << PAGE_SHIFT;
  1554. }
  1555. return kvm->arch.tss_addr;
  1556. }
  1557. static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
  1558. {
  1559. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  1560. save->selector = vmcs_read16(sf->selector);
  1561. save->base = vmcs_readl(sf->base);
  1562. save->limit = vmcs_read32(sf->limit);
  1563. save->ar = vmcs_read32(sf->ar_bytes);
  1564. vmcs_write16(sf->selector, save->base >> 4);
  1565. vmcs_write32(sf->base, save->base & 0xffff0);
  1566. vmcs_write32(sf->limit, 0xffff);
  1567. vmcs_write32(sf->ar_bytes, 0xf3);
  1568. if (save->base & 0xf)
  1569. printk_once(KERN_WARNING "kvm: segment base is not paragraph"
  1570. " aligned when entering protected mode (seg=%d)",
  1571. seg);
  1572. }
  1573. static void enter_rmode(struct kvm_vcpu *vcpu)
  1574. {
  1575. unsigned long flags;
  1576. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1577. if (enable_unrestricted_guest)
  1578. return;
  1579. vmx->emulation_required = 1;
  1580. vmx->rmode.vm86_active = 1;
  1581. /*
  1582. * Very old userspace does not call KVM_SET_TSS_ADDR before entering
  1583. * vcpu. Call it here with phys address pointing 16M below 4G.
  1584. */
  1585. if (!vcpu->kvm->arch.tss_addr) {
  1586. printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
  1587. "called before entering vcpu\n");
  1588. srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
  1589. vmx_set_tss_addr(vcpu->kvm, 0xfeffd000);
  1590. vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
  1591. }
  1592. vmx_segment_cache_clear(vmx);
  1593. vmx->rmode.tr.selector = vmcs_read16(GUEST_TR_SELECTOR);
  1594. vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
  1595. vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
  1596. vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
  1597. vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
  1598. vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
  1599. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  1600. flags = vmcs_readl(GUEST_RFLAGS);
  1601. vmx->rmode.save_rflags = flags;
  1602. flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
  1603. vmcs_writel(GUEST_RFLAGS, flags);
  1604. vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
  1605. update_exception_bitmap(vcpu);
  1606. if (emulate_invalid_guest_state)
  1607. goto continue_rmode;
  1608. vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
  1609. vmcs_write32(GUEST_SS_LIMIT, 0xffff);
  1610. vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
  1611. vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
  1612. vmcs_write32(GUEST_CS_LIMIT, 0xffff);
  1613. if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
  1614. vmcs_writel(GUEST_CS_BASE, 0xf0000);
  1615. vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
  1616. fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es);
  1617. fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds);
  1618. fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs);
  1619. fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs);
  1620. continue_rmode:
  1621. kvm_mmu_reset_context(vcpu);
  1622. }
  1623. static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  1624. {
  1625. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1626. struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
  1627. if (!msr)
  1628. return;
  1629. /*
  1630. * Force kernel_gs_base reloading before EFER changes, as control
  1631. * of this msr depends on is_long_mode().
  1632. */
  1633. vmx_load_host_state(to_vmx(vcpu));
  1634. vcpu->arch.efer = efer;
  1635. if (efer & EFER_LMA) {
  1636. vmcs_write32(VM_ENTRY_CONTROLS,
  1637. vmcs_read32(VM_ENTRY_CONTROLS) |
  1638. VM_ENTRY_IA32E_MODE);
  1639. msr->data = efer;
  1640. } else {
  1641. vmcs_write32(VM_ENTRY_CONTROLS,
  1642. vmcs_read32(VM_ENTRY_CONTROLS) &
  1643. ~VM_ENTRY_IA32E_MODE);
  1644. msr->data = efer & ~EFER_LME;
  1645. }
  1646. setup_msrs(vmx);
  1647. }
  1648. #ifdef CONFIG_X86_64
  1649. static void enter_lmode(struct kvm_vcpu *vcpu)
  1650. {
  1651. u32 guest_tr_ar;
  1652. vmx_segment_cache_clear(to_vmx(vcpu));
  1653. guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
  1654. if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
  1655. printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
  1656. __func__);
  1657. vmcs_write32(GUEST_TR_AR_BYTES,
  1658. (guest_tr_ar & ~AR_TYPE_MASK)
  1659. | AR_TYPE_BUSY_64_TSS);
  1660. }
  1661. vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
  1662. }
  1663. static void exit_lmode(struct kvm_vcpu *vcpu)
  1664. {
  1665. vmcs_write32(VM_ENTRY_CONTROLS,
  1666. vmcs_read32(VM_ENTRY_CONTROLS)
  1667. & ~VM_ENTRY_IA32E_MODE);
  1668. vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
  1669. }
  1670. #endif
  1671. static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
  1672. {
  1673. vpid_sync_context(to_vmx(vcpu));
  1674. if (enable_ept) {
  1675. if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
  1676. return;
  1677. ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
  1678. }
  1679. }
  1680. static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  1681. {
  1682. ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
  1683. vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
  1684. vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
  1685. }
  1686. static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
  1687. {
  1688. if (enable_ept && is_paging(vcpu))
  1689. vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
  1690. __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
  1691. }
  1692. static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  1693. {
  1694. ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
  1695. vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
  1696. vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
  1697. }
  1698. static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
  1699. {
  1700. if (!test_bit(VCPU_EXREG_PDPTR,
  1701. (unsigned long *)&vcpu->arch.regs_dirty))
  1702. return;
  1703. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  1704. vmcs_write64(GUEST_PDPTR0, vcpu->arch.mmu.pdptrs[0]);
  1705. vmcs_write64(GUEST_PDPTR1, vcpu->arch.mmu.pdptrs[1]);
  1706. vmcs_write64(GUEST_PDPTR2, vcpu->arch.mmu.pdptrs[2]);
  1707. vmcs_write64(GUEST_PDPTR3, vcpu->arch.mmu.pdptrs[3]);
  1708. }
  1709. }
  1710. static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
  1711. {
  1712. if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
  1713. vcpu->arch.mmu.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
  1714. vcpu->arch.mmu.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
  1715. vcpu->arch.mmu.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
  1716. vcpu->arch.mmu.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
  1717. }
  1718. __set_bit(VCPU_EXREG_PDPTR,
  1719. (unsigned long *)&vcpu->arch.regs_avail);
  1720. __set_bit(VCPU_EXREG_PDPTR,
  1721. (unsigned long *)&vcpu->arch.regs_dirty);
  1722. }
  1723. static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
  1724. static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
  1725. unsigned long cr0,
  1726. struct kvm_vcpu *vcpu)
  1727. {
  1728. if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
  1729. vmx_decache_cr3(vcpu);
  1730. if (!(cr0 & X86_CR0_PG)) {
  1731. /* From paging/starting to nonpaging */
  1732. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  1733. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
  1734. (CPU_BASED_CR3_LOAD_EXITING |
  1735. CPU_BASED_CR3_STORE_EXITING));
  1736. vcpu->arch.cr0 = cr0;
  1737. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  1738. } else if (!is_paging(vcpu)) {
  1739. /* From nonpaging to paging */
  1740. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
  1741. vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
  1742. ~(CPU_BASED_CR3_LOAD_EXITING |
  1743. CPU_BASED_CR3_STORE_EXITING));
  1744. vcpu->arch.cr0 = cr0;
  1745. vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
  1746. }
  1747. if (!(cr0 & X86_CR0_WP))
  1748. *hw_cr0 &= ~X86_CR0_WP;
  1749. }
  1750. static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  1751. {
  1752. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1753. unsigned long hw_cr0;
  1754. if (enable_unrestricted_guest)
  1755. hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
  1756. | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
  1757. else
  1758. hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
  1759. if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
  1760. enter_pmode(vcpu);
  1761. if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
  1762. enter_rmode(vcpu);
  1763. #ifdef CONFIG_X86_64
  1764. if (vcpu->arch.efer & EFER_LME) {
  1765. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
  1766. enter_lmode(vcpu);
  1767. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
  1768. exit_lmode(vcpu);
  1769. }
  1770. #endif
  1771. if (enable_ept)
  1772. ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
  1773. if (!vcpu->fpu_active)
  1774. hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
  1775. vmcs_writel(CR0_READ_SHADOW, cr0);
  1776. vmcs_writel(GUEST_CR0, hw_cr0);
  1777. vcpu->arch.cr0 = cr0;
  1778. __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
  1779. }
  1780. static u64 construct_eptp(unsigned long root_hpa)
  1781. {
  1782. u64 eptp;
  1783. /* TODO write the value reading from MSR */
  1784. eptp = VMX_EPT_DEFAULT_MT |
  1785. VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
  1786. eptp |= (root_hpa & PAGE_MASK);
  1787. return eptp;
  1788. }
  1789. static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  1790. {
  1791. unsigned long guest_cr3;
  1792. u64 eptp;
  1793. guest_cr3 = cr3;
  1794. if (enable_ept) {
  1795. eptp = construct_eptp(cr3);
  1796. vmcs_write64(EPT_POINTER, eptp);
  1797. guest_cr3 = is_paging(vcpu) ? kvm_read_cr3(vcpu) :
  1798. vcpu->kvm->arch.ept_identity_map_addr;
  1799. ept_load_pdptrs(vcpu);
  1800. }
  1801. vmx_flush_tlb(vcpu);
  1802. vmcs_writel(GUEST_CR3, guest_cr3);
  1803. }
  1804. static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  1805. {
  1806. unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
  1807. KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
  1808. vcpu->arch.cr4 = cr4;
  1809. if (enable_ept) {
  1810. if (!is_paging(vcpu)) {
  1811. hw_cr4 &= ~X86_CR4_PAE;
  1812. hw_cr4 |= X86_CR4_PSE;
  1813. } else if (!(cr4 & X86_CR4_PAE)) {
  1814. hw_cr4 &= ~X86_CR4_PAE;
  1815. }
  1816. }
  1817. vmcs_writel(CR4_READ_SHADOW, cr4);
  1818. vmcs_writel(GUEST_CR4, hw_cr4);
  1819. }
  1820. static void vmx_get_segment(struct kvm_vcpu *vcpu,
  1821. struct kvm_segment *var, int seg)
  1822. {
  1823. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1824. struct kvm_save_segment *save;
  1825. u32 ar;
  1826. if (vmx->rmode.vm86_active
  1827. && (seg == VCPU_SREG_TR || seg == VCPU_SREG_ES
  1828. || seg == VCPU_SREG_DS || seg == VCPU_SREG_FS
  1829. || seg == VCPU_SREG_GS)
  1830. && !emulate_invalid_guest_state) {
  1831. switch (seg) {
  1832. case VCPU_SREG_TR: save = &vmx->rmode.tr; break;
  1833. case VCPU_SREG_ES: save = &vmx->rmode.es; break;
  1834. case VCPU_SREG_DS: save = &vmx->rmode.ds; break;
  1835. case VCPU_SREG_FS: save = &vmx->rmode.fs; break;
  1836. case VCPU_SREG_GS: save = &vmx->rmode.gs; break;
  1837. default: BUG();
  1838. }
  1839. var->selector = save->selector;
  1840. var->base = save->base;
  1841. var->limit = save->limit;
  1842. ar = save->ar;
  1843. if (seg == VCPU_SREG_TR
  1844. || var->selector == vmx_read_guest_seg_selector(vmx, seg))
  1845. goto use_saved_rmode_seg;
  1846. }
  1847. var->base = vmx_read_guest_seg_base(vmx, seg);
  1848. var->limit = vmx_read_guest_seg_limit(vmx, seg);
  1849. var->selector = vmx_read_guest_seg_selector(vmx, seg);
  1850. ar = vmx_read_guest_seg_ar(vmx, seg);
  1851. use_saved_rmode_seg:
  1852. if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
  1853. ar = 0;
  1854. var->type = ar & 15;
  1855. var->s = (ar >> 4) & 1;
  1856. var->dpl = (ar >> 5) & 3;
  1857. var->present = (ar >> 7) & 1;
  1858. var->avl = (ar >> 12) & 1;
  1859. var->l = (ar >> 13) & 1;
  1860. var->db = (ar >> 14) & 1;
  1861. var->g = (ar >> 15) & 1;
  1862. var->unusable = (ar >> 16) & 1;
  1863. }
  1864. static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  1865. {
  1866. struct kvm_segment s;
  1867. if (to_vmx(vcpu)->rmode.vm86_active) {
  1868. vmx_get_segment(vcpu, &s, seg);
  1869. return s.base;
  1870. }
  1871. return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
  1872. }
  1873. static int __vmx_get_cpl(struct kvm_vcpu *vcpu)
  1874. {
  1875. if (!is_protmode(vcpu))
  1876. return 0;
  1877. if (!is_long_mode(vcpu)
  1878. && (kvm_get_rflags(vcpu) & X86_EFLAGS_VM)) /* if virtual 8086 */
  1879. return 3;
  1880. return vmx_read_guest_seg_selector(to_vmx(vcpu), VCPU_SREG_CS) & 3;
  1881. }
  1882. static int vmx_get_cpl(struct kvm_vcpu *vcpu)
  1883. {
  1884. if (!test_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail)) {
  1885. __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
  1886. to_vmx(vcpu)->cpl = __vmx_get_cpl(vcpu);
  1887. }
  1888. return to_vmx(vcpu)->cpl;
  1889. }
  1890. static u32 vmx_segment_access_rights(struct kvm_segment *var)
  1891. {
  1892. u32 ar;
  1893. if (var->unusable)
  1894. ar = 1 << 16;
  1895. else {
  1896. ar = var->type & 15;
  1897. ar |= (var->s & 1) << 4;
  1898. ar |= (var->dpl & 3) << 5;
  1899. ar |= (var->present & 1) << 7;
  1900. ar |= (var->avl & 1) << 12;
  1901. ar |= (var->l & 1) << 13;
  1902. ar |= (var->db & 1) << 14;
  1903. ar |= (var->g & 1) << 15;
  1904. }
  1905. if (ar == 0) /* a 0 value means unusable */
  1906. ar = AR_UNUSABLE_MASK;
  1907. return ar;
  1908. }
  1909. static void vmx_set_segment(struct kvm_vcpu *vcpu,
  1910. struct kvm_segment *var, int seg)
  1911. {
  1912. struct vcpu_vmx *vmx = to_vmx(vcpu);
  1913. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  1914. u32 ar;
  1915. vmx_segment_cache_clear(vmx);
  1916. if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) {
  1917. vmcs_write16(sf->selector, var->selector);
  1918. vmx->rmode.tr.selector = var->selector;
  1919. vmx->rmode.tr.base = var->base;
  1920. vmx->rmode.tr.limit = var->limit;
  1921. vmx->rmode.tr.ar = vmx_segment_access_rights(var);
  1922. return;
  1923. }
  1924. vmcs_writel(sf->base, var->base);
  1925. vmcs_write32(sf->limit, var->limit);
  1926. vmcs_write16(sf->selector, var->selector);
  1927. if (vmx->rmode.vm86_active && var->s) {
  1928. /*
  1929. * Hack real-mode segments into vm86 compatibility.
  1930. */
  1931. if (var->base == 0xffff0000 && var->selector == 0xf000)
  1932. vmcs_writel(sf->base, 0xf0000);
  1933. ar = 0xf3;
  1934. } else
  1935. ar = vmx_segment_access_rights(var);
  1936. /*
  1937. * Fix the "Accessed" bit in AR field of segment registers for older
  1938. * qemu binaries.
  1939. * IA32 arch specifies that at the time of processor reset the
  1940. * "Accessed" bit in the AR field of segment registers is 1. And qemu
  1941. * is setting it to 0 in the usedland code. This causes invalid guest
  1942. * state vmexit when "unrestricted guest" mode is turned on.
  1943. * Fix for this setup issue in cpu_reset is being pushed in the qemu
  1944. * tree. Newer qemu binaries with that qemu fix would not need this
  1945. * kvm hack.
  1946. */
  1947. if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
  1948. ar |= 0x1; /* Accessed */
  1949. vmcs_write32(sf->ar_bytes, ar);
  1950. __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
  1951. }
  1952. static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  1953. {
  1954. u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
  1955. *db = (ar >> 14) & 1;
  1956. *l = (ar >> 13) & 1;
  1957. }
  1958. static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1959. {
  1960. dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
  1961. dt->address = vmcs_readl(GUEST_IDTR_BASE);
  1962. }
  1963. static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1964. {
  1965. vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
  1966. vmcs_writel(GUEST_IDTR_BASE, dt->address);
  1967. }
  1968. static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1969. {
  1970. dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
  1971. dt->address = vmcs_readl(GUEST_GDTR_BASE);
  1972. }
  1973. static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  1974. {
  1975. vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
  1976. vmcs_writel(GUEST_GDTR_BASE, dt->address);
  1977. }
  1978. static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
  1979. {
  1980. struct kvm_segment var;
  1981. u32 ar;
  1982. vmx_get_segment(vcpu, &var, seg);
  1983. ar = vmx_segment_access_rights(&var);
  1984. if (var.base != (var.selector << 4))
  1985. return false;
  1986. if (var.limit != 0xffff)
  1987. return false;
  1988. if (ar != 0xf3)
  1989. return false;
  1990. return true;
  1991. }
  1992. static bool code_segment_valid(struct kvm_vcpu *vcpu)
  1993. {
  1994. struct kvm_segment cs;
  1995. unsigned int cs_rpl;
  1996. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  1997. cs_rpl = cs.selector & SELECTOR_RPL_MASK;
  1998. if (cs.unusable)
  1999. return false;
  2000. if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
  2001. return false;
  2002. if (!cs.s)
  2003. return false;
  2004. if (cs.type & AR_TYPE_WRITEABLE_MASK) {
  2005. if (cs.dpl > cs_rpl)
  2006. return false;
  2007. } else {
  2008. if (cs.dpl != cs_rpl)
  2009. return false;
  2010. }
  2011. if (!cs.present)
  2012. return false;
  2013. /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
  2014. return true;
  2015. }
  2016. static bool stack_segment_valid(struct kvm_vcpu *vcpu)
  2017. {
  2018. struct kvm_segment ss;
  2019. unsigned int ss_rpl;
  2020. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  2021. ss_rpl = ss.selector & SELECTOR_RPL_MASK;
  2022. if (ss.unusable)
  2023. return true;
  2024. if (ss.type != 3 && ss.type != 7)
  2025. return false;
  2026. if (!ss.s)
  2027. return false;
  2028. if (ss.dpl != ss_rpl) /* DPL != RPL */
  2029. return false;
  2030. if (!ss.present)
  2031. return false;
  2032. return true;
  2033. }
  2034. static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
  2035. {
  2036. struct kvm_segment var;
  2037. unsigned int rpl;
  2038. vmx_get_segment(vcpu, &var, seg);
  2039. rpl = var.selector & SELECTOR_RPL_MASK;
  2040. if (var.unusable)
  2041. return true;
  2042. if (!var.s)
  2043. return false;
  2044. if (!var.present)
  2045. return false;
  2046. if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
  2047. if (var.dpl < rpl) /* DPL < RPL */
  2048. return false;
  2049. }
  2050. /* TODO: Add other members to kvm_segment_field to allow checking for other access
  2051. * rights flags
  2052. */
  2053. return true;
  2054. }
  2055. static bool tr_valid(struct kvm_vcpu *vcpu)
  2056. {
  2057. struct kvm_segment tr;
  2058. vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
  2059. if (tr.unusable)
  2060. return false;
  2061. if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
  2062. return false;
  2063. if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
  2064. return false;
  2065. if (!tr.present)
  2066. return false;
  2067. return true;
  2068. }
  2069. static bool ldtr_valid(struct kvm_vcpu *vcpu)
  2070. {
  2071. struct kvm_segment ldtr;
  2072. vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
  2073. if (ldtr.unusable)
  2074. return true;
  2075. if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
  2076. return false;
  2077. if (ldtr.type != 2)
  2078. return false;
  2079. if (!ldtr.present)
  2080. return false;
  2081. return true;
  2082. }
  2083. static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
  2084. {
  2085. struct kvm_segment cs, ss;
  2086. vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
  2087. vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
  2088. return ((cs.selector & SELECTOR_RPL_MASK) ==
  2089. (ss.selector & SELECTOR_RPL_MASK));
  2090. }
  2091. /*
  2092. * Check if guest state is valid. Returns true if valid, false if
  2093. * not.
  2094. * We assume that registers are always usable
  2095. */
  2096. static bool guest_state_valid(struct kvm_vcpu *vcpu)
  2097. {
  2098. /* real mode guest state checks */
  2099. if (!is_protmode(vcpu)) {
  2100. if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
  2101. return false;
  2102. if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
  2103. return false;
  2104. if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
  2105. return false;
  2106. if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
  2107. return false;
  2108. if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
  2109. return false;
  2110. if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
  2111. return false;
  2112. } else {
  2113. /* protected mode guest state checks */
  2114. if (!cs_ss_rpl_check(vcpu))
  2115. return false;
  2116. if (!code_segment_valid(vcpu))
  2117. return false;
  2118. if (!stack_segment_valid(vcpu))
  2119. return false;
  2120. if (!data_segment_valid(vcpu, VCPU_SREG_DS))
  2121. return false;
  2122. if (!data_segment_valid(vcpu, VCPU_SREG_ES))
  2123. return false;
  2124. if (!data_segment_valid(vcpu, VCPU_SREG_FS))
  2125. return false;
  2126. if (!data_segment_valid(vcpu, VCPU_SREG_GS))
  2127. return false;
  2128. if (!tr_valid(vcpu))
  2129. return false;
  2130. if (!ldtr_valid(vcpu))
  2131. return false;
  2132. }
  2133. /* TODO:
  2134. * - Add checks on RIP
  2135. * - Add checks on RFLAGS
  2136. */
  2137. return true;
  2138. }
  2139. static int init_rmode_tss(struct kvm *kvm)
  2140. {
  2141. gfn_t fn;
  2142. u16 data = 0;
  2143. int r, idx, ret = 0;
  2144. idx = srcu_read_lock(&kvm->srcu);
  2145. fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
  2146. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  2147. if (r < 0)
  2148. goto out;
  2149. data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
  2150. r = kvm_write_guest_page(kvm, fn++, &data,
  2151. TSS_IOPB_BASE_OFFSET, sizeof(u16));
  2152. if (r < 0)
  2153. goto out;
  2154. r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
  2155. if (r < 0)
  2156. goto out;
  2157. r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
  2158. if (r < 0)
  2159. goto out;
  2160. data = ~0;
  2161. r = kvm_write_guest_page(kvm, fn, &data,
  2162. RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
  2163. sizeof(u8));
  2164. if (r < 0)
  2165. goto out;
  2166. ret = 1;
  2167. out:
  2168. srcu_read_unlock(&kvm->srcu, idx);
  2169. return ret;
  2170. }
  2171. static int init_rmode_identity_map(struct kvm *kvm)
  2172. {
  2173. int i, idx, r, ret;
  2174. pfn_t identity_map_pfn;
  2175. u32 tmp;
  2176. if (!enable_ept)
  2177. return 1;
  2178. if (unlikely(!kvm->arch.ept_identity_pagetable)) {
  2179. printk(KERN_ERR "EPT: identity-mapping pagetable "
  2180. "haven't been allocated!\n");
  2181. return 0;
  2182. }
  2183. if (likely(kvm->arch.ept_identity_pagetable_done))
  2184. return 1;
  2185. ret = 0;
  2186. identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
  2187. idx = srcu_read_lock(&kvm->srcu);
  2188. r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
  2189. if (r < 0)
  2190. goto out;
  2191. /* Set up identity-mapping pagetable for EPT in real mode */
  2192. for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
  2193. tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
  2194. _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
  2195. r = kvm_write_guest_page(kvm, identity_map_pfn,
  2196. &tmp, i * sizeof(tmp), sizeof(tmp));
  2197. if (r < 0)
  2198. goto out;
  2199. }
  2200. kvm->arch.ept_identity_pagetable_done = true;
  2201. ret = 1;
  2202. out:
  2203. srcu_read_unlock(&kvm->srcu, idx);
  2204. return ret;
  2205. }
  2206. static void seg_setup(int seg)
  2207. {
  2208. struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
  2209. unsigned int ar;
  2210. vmcs_write16(sf->selector, 0);
  2211. vmcs_writel(sf->base, 0);
  2212. vmcs_write32(sf->limit, 0xffff);
  2213. if (enable_unrestricted_guest) {
  2214. ar = 0x93;
  2215. if (seg == VCPU_SREG_CS)
  2216. ar |= 0x08; /* code segment */
  2217. } else
  2218. ar = 0xf3;
  2219. vmcs_write32(sf->ar_bytes, ar);
  2220. }
  2221. static int alloc_apic_access_page(struct kvm *kvm)
  2222. {
  2223. struct kvm_userspace_memory_region kvm_userspace_mem;
  2224. int r = 0;
  2225. mutex_lock(&kvm->slots_lock);
  2226. if (kvm->arch.apic_access_page)
  2227. goto out;
  2228. kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
  2229. kvm_userspace_mem.flags = 0;
  2230. kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
  2231. kvm_userspace_mem.memory_size = PAGE_SIZE;
  2232. r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
  2233. if (r)
  2234. goto out;
  2235. kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
  2236. out:
  2237. mutex_unlock(&kvm->slots_lock);
  2238. return r;
  2239. }
  2240. static int alloc_identity_pagetable(struct kvm *kvm)
  2241. {
  2242. struct kvm_userspace_memory_region kvm_userspace_mem;
  2243. int r = 0;
  2244. mutex_lock(&kvm->slots_lock);
  2245. if (kvm->arch.ept_identity_pagetable)
  2246. goto out;
  2247. kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
  2248. kvm_userspace_mem.flags = 0;
  2249. kvm_userspace_mem.guest_phys_addr =
  2250. kvm->arch.ept_identity_map_addr;
  2251. kvm_userspace_mem.memory_size = PAGE_SIZE;
  2252. r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
  2253. if (r)
  2254. goto out;
  2255. kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
  2256. kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
  2257. out:
  2258. mutex_unlock(&kvm->slots_lock);
  2259. return r;
  2260. }
  2261. static void allocate_vpid(struct vcpu_vmx *vmx)
  2262. {
  2263. int vpid;
  2264. vmx->vpid = 0;
  2265. if (!enable_vpid)
  2266. return;
  2267. spin_lock(&vmx_vpid_lock);
  2268. vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
  2269. if (vpid < VMX_NR_VPIDS) {
  2270. vmx->vpid = vpid;
  2271. __set_bit(vpid, vmx_vpid_bitmap);
  2272. }
  2273. spin_unlock(&vmx_vpid_lock);
  2274. }
  2275. static void free_vpid(struct vcpu_vmx *vmx)
  2276. {
  2277. if (!enable_vpid)
  2278. return;
  2279. spin_lock(&vmx_vpid_lock);
  2280. if (vmx->vpid != 0)
  2281. __clear_bit(vmx->vpid, vmx_vpid_bitmap);
  2282. spin_unlock(&vmx_vpid_lock);
  2283. }
  2284. static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
  2285. {
  2286. int f = sizeof(unsigned long);
  2287. if (!cpu_has_vmx_msr_bitmap())
  2288. return;
  2289. /*
  2290. * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
  2291. * have the write-low and read-high bitmap offsets the wrong way round.
  2292. * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
  2293. */
  2294. if (msr <= 0x1fff) {
  2295. __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
  2296. __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
  2297. } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
  2298. msr &= 0x1fff;
  2299. __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
  2300. __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
  2301. }
  2302. }
  2303. static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
  2304. {
  2305. if (!longmode_only)
  2306. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
  2307. __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
  2308. }
  2309. /*
  2310. * Sets up the vmcs for emulated real mode.
  2311. */
  2312. static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
  2313. {
  2314. u32 host_sysenter_cs, msr_low, msr_high;
  2315. u32 junk;
  2316. u64 host_pat;
  2317. unsigned long a;
  2318. struct desc_ptr dt;
  2319. int i;
  2320. unsigned long kvm_vmx_return;
  2321. u32 exec_control;
  2322. /* I/O */
  2323. vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
  2324. vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
  2325. if (cpu_has_vmx_msr_bitmap())
  2326. vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
  2327. vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
  2328. /* Control */
  2329. vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
  2330. vmcs_config.pin_based_exec_ctrl);
  2331. exec_control = vmcs_config.cpu_based_exec_ctrl;
  2332. if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
  2333. exec_control &= ~CPU_BASED_TPR_SHADOW;
  2334. #ifdef CONFIG_X86_64
  2335. exec_control |= CPU_BASED_CR8_STORE_EXITING |
  2336. CPU_BASED_CR8_LOAD_EXITING;
  2337. #endif
  2338. }
  2339. if (!enable_ept)
  2340. exec_control |= CPU_BASED_CR3_STORE_EXITING |
  2341. CPU_BASED_CR3_LOAD_EXITING |
  2342. CPU_BASED_INVLPG_EXITING;
  2343. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
  2344. if (cpu_has_secondary_exec_ctrls()) {
  2345. exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
  2346. if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
  2347. exec_control &=
  2348. ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
  2349. if (vmx->vpid == 0)
  2350. exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
  2351. if (!enable_ept) {
  2352. exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
  2353. enable_unrestricted_guest = 0;
  2354. }
  2355. if (!enable_unrestricted_guest)
  2356. exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
  2357. if (!ple_gap)
  2358. exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
  2359. vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
  2360. }
  2361. if (ple_gap) {
  2362. vmcs_write32(PLE_GAP, ple_gap);
  2363. vmcs_write32(PLE_WINDOW, ple_window);
  2364. }
  2365. vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
  2366. vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
  2367. vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
  2368. vmcs_writel(HOST_CR0, read_cr0() | X86_CR0_TS); /* 22.2.3 */
  2369. vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
  2370. vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
  2371. vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
  2372. vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  2373. vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  2374. vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
  2375. vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
  2376. vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
  2377. #ifdef CONFIG_X86_64
  2378. rdmsrl(MSR_FS_BASE, a);
  2379. vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
  2380. rdmsrl(MSR_GS_BASE, a);
  2381. vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
  2382. #else
  2383. vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
  2384. vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
  2385. #endif
  2386. vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
  2387. native_store_idt(&dt);
  2388. vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
  2389. asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
  2390. vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
  2391. vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
  2392. vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
  2393. vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
  2394. vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
  2395. vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
  2396. rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
  2397. vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
  2398. rdmsrl(MSR_IA32_SYSENTER_ESP, a);
  2399. vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
  2400. rdmsrl(MSR_IA32_SYSENTER_EIP, a);
  2401. vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
  2402. if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
  2403. rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
  2404. host_pat = msr_low | ((u64) msr_high << 32);
  2405. vmcs_write64(HOST_IA32_PAT, host_pat);
  2406. }
  2407. if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
  2408. rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
  2409. host_pat = msr_low | ((u64) msr_high << 32);
  2410. /* Write the default value follow host pat */
  2411. vmcs_write64(GUEST_IA32_PAT, host_pat);
  2412. /* Keep arch.pat sync with GUEST_IA32_PAT */
  2413. vmx->vcpu.arch.pat = host_pat;
  2414. }
  2415. for (i = 0; i < NR_VMX_MSR; ++i) {
  2416. u32 index = vmx_msr_index[i];
  2417. u32 data_low, data_high;
  2418. int j = vmx->nmsrs;
  2419. if (rdmsr_safe(index, &data_low, &data_high) < 0)
  2420. continue;
  2421. if (wrmsr_safe(index, data_low, data_high) < 0)
  2422. continue;
  2423. vmx->guest_msrs[j].index = i;
  2424. vmx->guest_msrs[j].data = 0;
  2425. vmx->guest_msrs[j].mask = -1ull;
  2426. ++vmx->nmsrs;
  2427. }
  2428. vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
  2429. /* 22.2.1, 20.8.1 */
  2430. vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
  2431. vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
  2432. vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
  2433. if (enable_ept)
  2434. vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
  2435. vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
  2436. kvm_write_tsc(&vmx->vcpu, 0);
  2437. return 0;
  2438. }
  2439. static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
  2440. {
  2441. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2442. u64 msr;
  2443. int ret;
  2444. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
  2445. vmx->rmode.vm86_active = 0;
  2446. vmx->soft_vnmi_blocked = 0;
  2447. vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
  2448. kvm_set_cr8(&vmx->vcpu, 0);
  2449. msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
  2450. if (kvm_vcpu_is_bsp(&vmx->vcpu))
  2451. msr |= MSR_IA32_APICBASE_BSP;
  2452. kvm_set_apic_base(&vmx->vcpu, msr);
  2453. ret = fx_init(&vmx->vcpu);
  2454. if (ret != 0)
  2455. goto out;
  2456. vmx_segment_cache_clear(vmx);
  2457. seg_setup(VCPU_SREG_CS);
  2458. /*
  2459. * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
  2460. * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
  2461. */
  2462. if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
  2463. vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
  2464. vmcs_writel(GUEST_CS_BASE, 0x000f0000);
  2465. } else {
  2466. vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
  2467. vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
  2468. }
  2469. seg_setup(VCPU_SREG_DS);
  2470. seg_setup(VCPU_SREG_ES);
  2471. seg_setup(VCPU_SREG_FS);
  2472. seg_setup(VCPU_SREG_GS);
  2473. seg_setup(VCPU_SREG_SS);
  2474. vmcs_write16(GUEST_TR_SELECTOR, 0);
  2475. vmcs_writel(GUEST_TR_BASE, 0);
  2476. vmcs_write32(GUEST_TR_LIMIT, 0xffff);
  2477. vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
  2478. vmcs_write16(GUEST_LDTR_SELECTOR, 0);
  2479. vmcs_writel(GUEST_LDTR_BASE, 0);
  2480. vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
  2481. vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
  2482. vmcs_write32(GUEST_SYSENTER_CS, 0);
  2483. vmcs_writel(GUEST_SYSENTER_ESP, 0);
  2484. vmcs_writel(GUEST_SYSENTER_EIP, 0);
  2485. vmcs_writel(GUEST_RFLAGS, 0x02);
  2486. if (kvm_vcpu_is_bsp(&vmx->vcpu))
  2487. kvm_rip_write(vcpu, 0xfff0);
  2488. else
  2489. kvm_rip_write(vcpu, 0);
  2490. kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
  2491. vmcs_writel(GUEST_DR7, 0x400);
  2492. vmcs_writel(GUEST_GDTR_BASE, 0);
  2493. vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
  2494. vmcs_writel(GUEST_IDTR_BASE, 0);
  2495. vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
  2496. vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
  2497. vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
  2498. vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
  2499. /* Special registers */
  2500. vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
  2501. setup_msrs(vmx);
  2502. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
  2503. if (cpu_has_vmx_tpr_shadow()) {
  2504. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
  2505. if (vm_need_tpr_shadow(vmx->vcpu.kvm))
  2506. vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
  2507. __pa(vmx->vcpu.arch.apic->regs));
  2508. vmcs_write32(TPR_THRESHOLD, 0);
  2509. }
  2510. if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
  2511. vmcs_write64(APIC_ACCESS_ADDR,
  2512. page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
  2513. if (vmx->vpid != 0)
  2514. vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
  2515. vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
  2516. vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
  2517. vmx_set_cr4(&vmx->vcpu, 0);
  2518. vmx_set_efer(&vmx->vcpu, 0);
  2519. vmx_fpu_activate(&vmx->vcpu);
  2520. update_exception_bitmap(&vmx->vcpu);
  2521. vpid_sync_context(vmx);
  2522. ret = 0;
  2523. /* HACK: Don't enable emulation on guest boot/reset */
  2524. vmx->emulation_required = 0;
  2525. out:
  2526. return ret;
  2527. }
  2528. static void enable_irq_window(struct kvm_vcpu *vcpu)
  2529. {
  2530. u32 cpu_based_vm_exec_control;
  2531. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  2532. cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
  2533. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  2534. }
  2535. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  2536. {
  2537. u32 cpu_based_vm_exec_control;
  2538. if (!cpu_has_virtual_nmis()) {
  2539. enable_irq_window(vcpu);
  2540. return;
  2541. }
  2542. if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
  2543. enable_irq_window(vcpu);
  2544. return;
  2545. }
  2546. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  2547. cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
  2548. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  2549. }
  2550. static void vmx_inject_irq(struct kvm_vcpu *vcpu)
  2551. {
  2552. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2553. uint32_t intr;
  2554. int irq = vcpu->arch.interrupt.nr;
  2555. trace_kvm_inj_virq(irq);
  2556. ++vcpu->stat.irq_injections;
  2557. if (vmx->rmode.vm86_active) {
  2558. int inc_eip = 0;
  2559. if (vcpu->arch.interrupt.soft)
  2560. inc_eip = vcpu->arch.event_exit_inst_len;
  2561. if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
  2562. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  2563. return;
  2564. }
  2565. intr = irq | INTR_INFO_VALID_MASK;
  2566. if (vcpu->arch.interrupt.soft) {
  2567. intr |= INTR_TYPE_SOFT_INTR;
  2568. vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
  2569. vmx->vcpu.arch.event_exit_inst_len);
  2570. } else
  2571. intr |= INTR_TYPE_EXT_INTR;
  2572. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
  2573. vmx_clear_hlt(vcpu);
  2574. }
  2575. static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
  2576. {
  2577. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2578. if (!cpu_has_virtual_nmis()) {
  2579. /*
  2580. * Tracking the NMI-blocked state in software is built upon
  2581. * finding the next open IRQ window. This, in turn, depends on
  2582. * well-behaving guests: They have to keep IRQs disabled at
  2583. * least as long as the NMI handler runs. Otherwise we may
  2584. * cause NMI nesting, maybe breaking the guest. But as this is
  2585. * highly unlikely, we can live with the residual risk.
  2586. */
  2587. vmx->soft_vnmi_blocked = 1;
  2588. vmx->vnmi_blocked_time = 0;
  2589. }
  2590. ++vcpu->stat.nmi_injections;
  2591. vmx->nmi_known_unmasked = false;
  2592. if (vmx->rmode.vm86_active) {
  2593. if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
  2594. kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
  2595. return;
  2596. }
  2597. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
  2598. INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
  2599. vmx_clear_hlt(vcpu);
  2600. }
  2601. static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
  2602. {
  2603. if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
  2604. return 0;
  2605. return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  2606. (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
  2607. | GUEST_INTR_STATE_NMI));
  2608. }
  2609. static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
  2610. {
  2611. if (!cpu_has_virtual_nmis())
  2612. return to_vmx(vcpu)->soft_vnmi_blocked;
  2613. if (to_vmx(vcpu)->nmi_known_unmasked)
  2614. return false;
  2615. return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
  2616. }
  2617. static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  2618. {
  2619. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2620. if (!cpu_has_virtual_nmis()) {
  2621. if (vmx->soft_vnmi_blocked != masked) {
  2622. vmx->soft_vnmi_blocked = masked;
  2623. vmx->vnmi_blocked_time = 0;
  2624. }
  2625. } else {
  2626. vmx->nmi_known_unmasked = !masked;
  2627. if (masked)
  2628. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  2629. GUEST_INTR_STATE_NMI);
  2630. else
  2631. vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
  2632. GUEST_INTR_STATE_NMI);
  2633. }
  2634. }
  2635. static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
  2636. {
  2637. return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
  2638. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
  2639. (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
  2640. }
  2641. static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
  2642. {
  2643. int ret;
  2644. struct kvm_userspace_memory_region tss_mem = {
  2645. .slot = TSS_PRIVATE_MEMSLOT,
  2646. .guest_phys_addr = addr,
  2647. .memory_size = PAGE_SIZE * 3,
  2648. .flags = 0,
  2649. };
  2650. ret = kvm_set_memory_region(kvm, &tss_mem, 0);
  2651. if (ret)
  2652. return ret;
  2653. kvm->arch.tss_addr = addr;
  2654. if (!init_rmode_tss(kvm))
  2655. return -ENOMEM;
  2656. return 0;
  2657. }
  2658. static int handle_rmode_exception(struct kvm_vcpu *vcpu,
  2659. int vec, u32 err_code)
  2660. {
  2661. /*
  2662. * Instruction with address size override prefix opcode 0x67
  2663. * Cause the #SS fault with 0 error code in VM86 mode.
  2664. */
  2665. if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
  2666. if (emulate_instruction(vcpu, 0) == EMULATE_DONE)
  2667. return 1;
  2668. /*
  2669. * Forward all other exceptions that are valid in real mode.
  2670. * FIXME: Breaks guest debugging in real mode, needs to be fixed with
  2671. * the required debugging infrastructure rework.
  2672. */
  2673. switch (vec) {
  2674. case DB_VECTOR:
  2675. if (vcpu->guest_debug &
  2676. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  2677. return 0;
  2678. kvm_queue_exception(vcpu, vec);
  2679. return 1;
  2680. case BP_VECTOR:
  2681. /*
  2682. * Update instruction length as we may reinject the exception
  2683. * from user space while in guest debugging mode.
  2684. */
  2685. to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
  2686. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  2687. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  2688. return 0;
  2689. /* fall through */
  2690. case DE_VECTOR:
  2691. case OF_VECTOR:
  2692. case BR_VECTOR:
  2693. case UD_VECTOR:
  2694. case DF_VECTOR:
  2695. case SS_VECTOR:
  2696. case GP_VECTOR:
  2697. case MF_VECTOR:
  2698. kvm_queue_exception(vcpu, vec);
  2699. return 1;
  2700. }
  2701. return 0;
  2702. }
  2703. /*
  2704. * Trigger machine check on the host. We assume all the MSRs are already set up
  2705. * by the CPU and that we still run on the same CPU as the MCE occurred on.
  2706. * We pass a fake environment to the machine check handler because we want
  2707. * the guest to be always treated like user space, no matter what context
  2708. * it used internally.
  2709. */
  2710. static void kvm_machine_check(void)
  2711. {
  2712. #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
  2713. struct pt_regs regs = {
  2714. .cs = 3, /* Fake ring 3 no matter what the guest ran on */
  2715. .flags = X86_EFLAGS_IF,
  2716. };
  2717. do_machine_check(&regs, 0);
  2718. #endif
  2719. }
  2720. static int handle_machine_check(struct kvm_vcpu *vcpu)
  2721. {
  2722. /* already handled by vcpu_run */
  2723. return 1;
  2724. }
  2725. static int handle_exception(struct kvm_vcpu *vcpu)
  2726. {
  2727. struct vcpu_vmx *vmx = to_vmx(vcpu);
  2728. struct kvm_run *kvm_run = vcpu->run;
  2729. u32 intr_info, ex_no, error_code;
  2730. unsigned long cr2, rip, dr6;
  2731. u32 vect_info;
  2732. enum emulation_result er;
  2733. vect_info = vmx->idt_vectoring_info;
  2734. intr_info = vmx->exit_intr_info;
  2735. if (is_machine_check(intr_info))
  2736. return handle_machine_check(vcpu);
  2737. if ((vect_info & VECTORING_INFO_VALID_MASK) &&
  2738. !is_page_fault(intr_info)) {
  2739. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  2740. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
  2741. vcpu->run->internal.ndata = 2;
  2742. vcpu->run->internal.data[0] = vect_info;
  2743. vcpu->run->internal.data[1] = intr_info;
  2744. return 0;
  2745. }
  2746. if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
  2747. return 1; /* already handled by vmx_vcpu_run() */
  2748. if (is_no_device(intr_info)) {
  2749. vmx_fpu_activate(vcpu);
  2750. return 1;
  2751. }
  2752. if (is_invalid_opcode(intr_info)) {
  2753. er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
  2754. if (er != EMULATE_DONE)
  2755. kvm_queue_exception(vcpu, UD_VECTOR);
  2756. return 1;
  2757. }
  2758. error_code = 0;
  2759. if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
  2760. error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
  2761. if (is_page_fault(intr_info)) {
  2762. /* EPT won't cause page fault directly */
  2763. if (enable_ept)
  2764. BUG();
  2765. cr2 = vmcs_readl(EXIT_QUALIFICATION);
  2766. trace_kvm_page_fault(cr2, error_code);
  2767. if (kvm_event_needs_reinjection(vcpu))
  2768. kvm_mmu_unprotect_page_virt(vcpu, cr2);
  2769. return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
  2770. }
  2771. if (vmx->rmode.vm86_active &&
  2772. handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
  2773. error_code)) {
  2774. if (vcpu->arch.halt_request) {
  2775. vcpu->arch.halt_request = 0;
  2776. return kvm_emulate_halt(vcpu);
  2777. }
  2778. return 1;
  2779. }
  2780. ex_no = intr_info & INTR_INFO_VECTOR_MASK;
  2781. switch (ex_no) {
  2782. case DB_VECTOR:
  2783. dr6 = vmcs_readl(EXIT_QUALIFICATION);
  2784. if (!(vcpu->guest_debug &
  2785. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
  2786. vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
  2787. kvm_queue_exception(vcpu, DB_VECTOR);
  2788. return 1;
  2789. }
  2790. kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
  2791. kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
  2792. /* fall through */
  2793. case BP_VECTOR:
  2794. /*
  2795. * Update instruction length as we may reinject #BP from
  2796. * user space while in guest debugging mode. Reading it for
  2797. * #DB as well causes no harm, it is not used in that case.
  2798. */
  2799. vmx->vcpu.arch.event_exit_inst_len =
  2800. vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
  2801. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  2802. rip = kvm_rip_read(vcpu);
  2803. kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
  2804. kvm_run->debug.arch.exception = ex_no;
  2805. break;
  2806. default:
  2807. kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
  2808. kvm_run->ex.exception = ex_no;
  2809. kvm_run->ex.error_code = error_code;
  2810. break;
  2811. }
  2812. return 0;
  2813. }
  2814. static int handle_external_interrupt(struct kvm_vcpu *vcpu)
  2815. {
  2816. ++vcpu->stat.irq_exits;
  2817. return 1;
  2818. }
  2819. static int handle_triple_fault(struct kvm_vcpu *vcpu)
  2820. {
  2821. vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
  2822. return 0;
  2823. }
  2824. static int handle_io(struct kvm_vcpu *vcpu)
  2825. {
  2826. unsigned long exit_qualification;
  2827. int size, in, string;
  2828. unsigned port;
  2829. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  2830. string = (exit_qualification & 16) != 0;
  2831. in = (exit_qualification & 8) != 0;
  2832. ++vcpu->stat.io_exits;
  2833. if (string || in)
  2834. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  2835. port = exit_qualification >> 16;
  2836. size = (exit_qualification & 7) + 1;
  2837. skip_emulated_instruction(vcpu);
  2838. return kvm_fast_pio_out(vcpu, size, port);
  2839. }
  2840. static void
  2841. vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  2842. {
  2843. /*
  2844. * Patch in the VMCALL instruction:
  2845. */
  2846. hypercall[0] = 0x0f;
  2847. hypercall[1] = 0x01;
  2848. hypercall[2] = 0xc1;
  2849. }
  2850. static int handle_cr(struct kvm_vcpu *vcpu)
  2851. {
  2852. unsigned long exit_qualification, val;
  2853. int cr;
  2854. int reg;
  2855. int err;
  2856. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  2857. cr = exit_qualification & 15;
  2858. reg = (exit_qualification >> 8) & 15;
  2859. switch ((exit_qualification >> 4) & 3) {
  2860. case 0: /* mov to cr */
  2861. val = kvm_register_read(vcpu, reg);
  2862. trace_kvm_cr_write(cr, val);
  2863. switch (cr) {
  2864. case 0:
  2865. err = kvm_set_cr0(vcpu, val);
  2866. kvm_complete_insn_gp(vcpu, err);
  2867. return 1;
  2868. case 3:
  2869. err = kvm_set_cr3(vcpu, val);
  2870. kvm_complete_insn_gp(vcpu, err);
  2871. return 1;
  2872. case 4:
  2873. err = kvm_set_cr4(vcpu, val);
  2874. kvm_complete_insn_gp(vcpu, err);
  2875. return 1;
  2876. case 8: {
  2877. u8 cr8_prev = kvm_get_cr8(vcpu);
  2878. u8 cr8 = kvm_register_read(vcpu, reg);
  2879. err = kvm_set_cr8(vcpu, cr8);
  2880. kvm_complete_insn_gp(vcpu, err);
  2881. if (irqchip_in_kernel(vcpu->kvm))
  2882. return 1;
  2883. if (cr8_prev <= cr8)
  2884. return 1;
  2885. vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
  2886. return 0;
  2887. }
  2888. };
  2889. break;
  2890. case 2: /* clts */
  2891. vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
  2892. trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
  2893. skip_emulated_instruction(vcpu);
  2894. vmx_fpu_activate(vcpu);
  2895. return 1;
  2896. case 1: /*mov from cr*/
  2897. switch (cr) {
  2898. case 3:
  2899. val = kvm_read_cr3(vcpu);
  2900. kvm_register_write(vcpu, reg, val);
  2901. trace_kvm_cr_read(cr, val);
  2902. skip_emulated_instruction(vcpu);
  2903. return 1;
  2904. case 8:
  2905. val = kvm_get_cr8(vcpu);
  2906. kvm_register_write(vcpu, reg, val);
  2907. trace_kvm_cr_read(cr, val);
  2908. skip_emulated_instruction(vcpu);
  2909. return 1;
  2910. }
  2911. break;
  2912. case 3: /* lmsw */
  2913. val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
  2914. trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
  2915. kvm_lmsw(vcpu, val);
  2916. skip_emulated_instruction(vcpu);
  2917. return 1;
  2918. default:
  2919. break;
  2920. }
  2921. vcpu->run->exit_reason = 0;
  2922. pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
  2923. (int)(exit_qualification >> 4) & 3, cr);
  2924. return 0;
  2925. }
  2926. static int handle_dr(struct kvm_vcpu *vcpu)
  2927. {
  2928. unsigned long exit_qualification;
  2929. int dr, reg;
  2930. /* Do not handle if the CPL > 0, will trigger GP on re-entry */
  2931. if (!kvm_require_cpl(vcpu, 0))
  2932. return 1;
  2933. dr = vmcs_readl(GUEST_DR7);
  2934. if (dr & DR7_GD) {
  2935. /*
  2936. * As the vm-exit takes precedence over the debug trap, we
  2937. * need to emulate the latter, either for the host or the
  2938. * guest debugging itself.
  2939. */
  2940. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
  2941. vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
  2942. vcpu->run->debug.arch.dr7 = dr;
  2943. vcpu->run->debug.arch.pc =
  2944. vmcs_readl(GUEST_CS_BASE) +
  2945. vmcs_readl(GUEST_RIP);
  2946. vcpu->run->debug.arch.exception = DB_VECTOR;
  2947. vcpu->run->exit_reason = KVM_EXIT_DEBUG;
  2948. return 0;
  2949. } else {
  2950. vcpu->arch.dr7 &= ~DR7_GD;
  2951. vcpu->arch.dr6 |= DR6_BD;
  2952. vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
  2953. kvm_queue_exception(vcpu, DB_VECTOR);
  2954. return 1;
  2955. }
  2956. }
  2957. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  2958. dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
  2959. reg = DEBUG_REG_ACCESS_REG(exit_qualification);
  2960. if (exit_qualification & TYPE_MOV_FROM_DR) {
  2961. unsigned long val;
  2962. if (!kvm_get_dr(vcpu, dr, &val))
  2963. kvm_register_write(vcpu, reg, val);
  2964. } else
  2965. kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]);
  2966. skip_emulated_instruction(vcpu);
  2967. return 1;
  2968. }
  2969. static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
  2970. {
  2971. vmcs_writel(GUEST_DR7, val);
  2972. }
  2973. static int handle_cpuid(struct kvm_vcpu *vcpu)
  2974. {
  2975. kvm_emulate_cpuid(vcpu);
  2976. return 1;
  2977. }
  2978. static int handle_rdmsr(struct kvm_vcpu *vcpu)
  2979. {
  2980. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  2981. u64 data;
  2982. if (vmx_get_msr(vcpu, ecx, &data)) {
  2983. trace_kvm_msr_read_ex(ecx);
  2984. kvm_inject_gp(vcpu, 0);
  2985. return 1;
  2986. }
  2987. trace_kvm_msr_read(ecx, data);
  2988. /* FIXME: handling of bits 32:63 of rax, rdx */
  2989. vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
  2990. vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
  2991. skip_emulated_instruction(vcpu);
  2992. return 1;
  2993. }
  2994. static int handle_wrmsr(struct kvm_vcpu *vcpu)
  2995. {
  2996. u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
  2997. u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
  2998. | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  2999. if (vmx_set_msr(vcpu, ecx, data) != 0) {
  3000. trace_kvm_msr_write_ex(ecx, data);
  3001. kvm_inject_gp(vcpu, 0);
  3002. return 1;
  3003. }
  3004. trace_kvm_msr_write(ecx, data);
  3005. skip_emulated_instruction(vcpu);
  3006. return 1;
  3007. }
  3008. static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
  3009. {
  3010. kvm_make_request(KVM_REQ_EVENT, vcpu);
  3011. return 1;
  3012. }
  3013. static int handle_interrupt_window(struct kvm_vcpu *vcpu)
  3014. {
  3015. u32 cpu_based_vm_exec_control;
  3016. /* clear pending irq */
  3017. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  3018. cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
  3019. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  3020. kvm_make_request(KVM_REQ_EVENT, vcpu);
  3021. ++vcpu->stat.irq_window_exits;
  3022. /*
  3023. * If the user space waits to inject interrupts, exit as soon as
  3024. * possible
  3025. */
  3026. if (!irqchip_in_kernel(vcpu->kvm) &&
  3027. vcpu->run->request_interrupt_window &&
  3028. !kvm_cpu_has_interrupt(vcpu)) {
  3029. vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  3030. return 0;
  3031. }
  3032. return 1;
  3033. }
  3034. static int handle_halt(struct kvm_vcpu *vcpu)
  3035. {
  3036. skip_emulated_instruction(vcpu);
  3037. return kvm_emulate_halt(vcpu);
  3038. }
  3039. static int handle_vmcall(struct kvm_vcpu *vcpu)
  3040. {
  3041. skip_emulated_instruction(vcpu);
  3042. kvm_emulate_hypercall(vcpu);
  3043. return 1;
  3044. }
  3045. static int handle_vmx_insn(struct kvm_vcpu *vcpu)
  3046. {
  3047. kvm_queue_exception(vcpu, UD_VECTOR);
  3048. return 1;
  3049. }
  3050. static int handle_invd(struct kvm_vcpu *vcpu)
  3051. {
  3052. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  3053. }
  3054. static int handle_invlpg(struct kvm_vcpu *vcpu)
  3055. {
  3056. unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  3057. kvm_mmu_invlpg(vcpu, exit_qualification);
  3058. skip_emulated_instruction(vcpu);
  3059. return 1;
  3060. }
  3061. static int handle_wbinvd(struct kvm_vcpu *vcpu)
  3062. {
  3063. skip_emulated_instruction(vcpu);
  3064. kvm_emulate_wbinvd(vcpu);
  3065. return 1;
  3066. }
  3067. static int handle_xsetbv(struct kvm_vcpu *vcpu)
  3068. {
  3069. u64 new_bv = kvm_read_edx_eax(vcpu);
  3070. u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
  3071. if (kvm_set_xcr(vcpu, index, new_bv) == 0)
  3072. skip_emulated_instruction(vcpu);
  3073. return 1;
  3074. }
  3075. static int handle_apic_access(struct kvm_vcpu *vcpu)
  3076. {
  3077. return emulate_instruction(vcpu, 0) == EMULATE_DONE;
  3078. }
  3079. static int handle_task_switch(struct kvm_vcpu *vcpu)
  3080. {
  3081. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3082. unsigned long exit_qualification;
  3083. bool has_error_code = false;
  3084. u32 error_code = 0;
  3085. u16 tss_selector;
  3086. int reason, type, idt_v;
  3087. idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
  3088. type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
  3089. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  3090. reason = (u32)exit_qualification >> 30;
  3091. if (reason == TASK_SWITCH_GATE && idt_v) {
  3092. switch (type) {
  3093. case INTR_TYPE_NMI_INTR:
  3094. vcpu->arch.nmi_injected = false;
  3095. vmx_set_nmi_mask(vcpu, true);
  3096. break;
  3097. case INTR_TYPE_EXT_INTR:
  3098. case INTR_TYPE_SOFT_INTR:
  3099. kvm_clear_interrupt_queue(vcpu);
  3100. break;
  3101. case INTR_TYPE_HARD_EXCEPTION:
  3102. if (vmx->idt_vectoring_info &
  3103. VECTORING_INFO_DELIVER_CODE_MASK) {
  3104. has_error_code = true;
  3105. error_code =
  3106. vmcs_read32(IDT_VECTORING_ERROR_CODE);
  3107. }
  3108. /* fall through */
  3109. case INTR_TYPE_SOFT_EXCEPTION:
  3110. kvm_clear_exception_queue(vcpu);
  3111. break;
  3112. default:
  3113. break;
  3114. }
  3115. }
  3116. tss_selector = exit_qualification;
  3117. if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
  3118. type != INTR_TYPE_EXT_INTR &&
  3119. type != INTR_TYPE_NMI_INTR))
  3120. skip_emulated_instruction(vcpu);
  3121. if (kvm_task_switch(vcpu, tss_selector, reason,
  3122. has_error_code, error_code) == EMULATE_FAIL) {
  3123. vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  3124. vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
  3125. vcpu->run->internal.ndata = 0;
  3126. return 0;
  3127. }
  3128. /* clear all local breakpoint enable flags */
  3129. vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
  3130. /*
  3131. * TODO: What about debug traps on tss switch?
  3132. * Are we supposed to inject them and update dr6?
  3133. */
  3134. return 1;
  3135. }
  3136. static int handle_ept_violation(struct kvm_vcpu *vcpu)
  3137. {
  3138. unsigned long exit_qualification;
  3139. gpa_t gpa;
  3140. int gla_validity;
  3141. exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
  3142. if (exit_qualification & (1 << 6)) {
  3143. printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
  3144. return -EINVAL;
  3145. }
  3146. gla_validity = (exit_qualification >> 7) & 0x3;
  3147. if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
  3148. printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
  3149. printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
  3150. (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
  3151. vmcs_readl(GUEST_LINEAR_ADDRESS));
  3152. printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
  3153. (long unsigned int)exit_qualification);
  3154. vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
  3155. vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
  3156. return 0;
  3157. }
  3158. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  3159. trace_kvm_page_fault(gpa, exit_qualification);
  3160. return kvm_mmu_page_fault(vcpu, gpa, exit_qualification & 0x3, NULL, 0);
  3161. }
  3162. static u64 ept_rsvd_mask(u64 spte, int level)
  3163. {
  3164. int i;
  3165. u64 mask = 0;
  3166. for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
  3167. mask |= (1ULL << i);
  3168. if (level > 2)
  3169. /* bits 7:3 reserved */
  3170. mask |= 0xf8;
  3171. else if (level == 2) {
  3172. if (spte & (1ULL << 7))
  3173. /* 2MB ref, bits 20:12 reserved */
  3174. mask |= 0x1ff000;
  3175. else
  3176. /* bits 6:3 reserved */
  3177. mask |= 0x78;
  3178. }
  3179. return mask;
  3180. }
  3181. static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
  3182. int level)
  3183. {
  3184. printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
  3185. /* 010b (write-only) */
  3186. WARN_ON((spte & 0x7) == 0x2);
  3187. /* 110b (write/execute) */
  3188. WARN_ON((spte & 0x7) == 0x6);
  3189. /* 100b (execute-only) and value not supported by logical processor */
  3190. if (!cpu_has_vmx_ept_execute_only())
  3191. WARN_ON((spte & 0x7) == 0x4);
  3192. /* not 000b */
  3193. if ((spte & 0x7)) {
  3194. u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
  3195. if (rsvd_bits != 0) {
  3196. printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
  3197. __func__, rsvd_bits);
  3198. WARN_ON(1);
  3199. }
  3200. if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
  3201. u64 ept_mem_type = (spte & 0x38) >> 3;
  3202. if (ept_mem_type == 2 || ept_mem_type == 3 ||
  3203. ept_mem_type == 7) {
  3204. printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
  3205. __func__, ept_mem_type);
  3206. WARN_ON(1);
  3207. }
  3208. }
  3209. }
  3210. }
  3211. static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
  3212. {
  3213. u64 sptes[4];
  3214. int nr_sptes, i;
  3215. gpa_t gpa;
  3216. gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
  3217. printk(KERN_ERR "EPT: Misconfiguration.\n");
  3218. printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
  3219. nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
  3220. for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
  3221. ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
  3222. vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
  3223. vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
  3224. return 0;
  3225. }
  3226. static int handle_nmi_window(struct kvm_vcpu *vcpu)
  3227. {
  3228. u32 cpu_based_vm_exec_control;
  3229. /* clear pending NMI */
  3230. cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  3231. cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
  3232. vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
  3233. ++vcpu->stat.nmi_window_exits;
  3234. kvm_make_request(KVM_REQ_EVENT, vcpu);
  3235. return 1;
  3236. }
  3237. static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
  3238. {
  3239. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3240. enum emulation_result err = EMULATE_DONE;
  3241. int ret = 1;
  3242. u32 cpu_exec_ctrl;
  3243. bool intr_window_requested;
  3244. cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
  3245. intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
  3246. while (!guest_state_valid(vcpu)) {
  3247. if (intr_window_requested
  3248. && (kvm_get_rflags(&vmx->vcpu) & X86_EFLAGS_IF))
  3249. return handle_interrupt_window(&vmx->vcpu);
  3250. err = emulate_instruction(vcpu, 0);
  3251. if (err == EMULATE_DO_MMIO) {
  3252. ret = 0;
  3253. goto out;
  3254. }
  3255. if (err != EMULATE_DONE)
  3256. return 0;
  3257. if (signal_pending(current))
  3258. goto out;
  3259. if (need_resched())
  3260. schedule();
  3261. }
  3262. vmx->emulation_required = 0;
  3263. out:
  3264. return ret;
  3265. }
  3266. /*
  3267. * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
  3268. * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
  3269. */
  3270. static int handle_pause(struct kvm_vcpu *vcpu)
  3271. {
  3272. skip_emulated_instruction(vcpu);
  3273. kvm_vcpu_on_spin(vcpu);
  3274. return 1;
  3275. }
  3276. static int handle_invalid_op(struct kvm_vcpu *vcpu)
  3277. {
  3278. kvm_queue_exception(vcpu, UD_VECTOR);
  3279. return 1;
  3280. }
  3281. /*
  3282. * The exit handlers return 1 if the exit was handled fully and guest execution
  3283. * may resume. Otherwise they set the kvm_run parameter to indicate what needs
  3284. * to be done to userspace and return 0.
  3285. */
  3286. static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
  3287. [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
  3288. [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
  3289. [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
  3290. [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
  3291. [EXIT_REASON_IO_INSTRUCTION] = handle_io,
  3292. [EXIT_REASON_CR_ACCESS] = handle_cr,
  3293. [EXIT_REASON_DR_ACCESS] = handle_dr,
  3294. [EXIT_REASON_CPUID] = handle_cpuid,
  3295. [EXIT_REASON_MSR_READ] = handle_rdmsr,
  3296. [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
  3297. [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
  3298. [EXIT_REASON_HLT] = handle_halt,
  3299. [EXIT_REASON_INVD] = handle_invd,
  3300. [EXIT_REASON_INVLPG] = handle_invlpg,
  3301. [EXIT_REASON_VMCALL] = handle_vmcall,
  3302. [EXIT_REASON_VMCLEAR] = handle_vmx_insn,
  3303. [EXIT_REASON_VMLAUNCH] = handle_vmx_insn,
  3304. [EXIT_REASON_VMPTRLD] = handle_vmx_insn,
  3305. [EXIT_REASON_VMPTRST] = handle_vmx_insn,
  3306. [EXIT_REASON_VMREAD] = handle_vmx_insn,
  3307. [EXIT_REASON_VMRESUME] = handle_vmx_insn,
  3308. [EXIT_REASON_VMWRITE] = handle_vmx_insn,
  3309. [EXIT_REASON_VMOFF] = handle_vmx_insn,
  3310. [EXIT_REASON_VMON] = handle_vmx_insn,
  3311. [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
  3312. [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
  3313. [EXIT_REASON_WBINVD] = handle_wbinvd,
  3314. [EXIT_REASON_XSETBV] = handle_xsetbv,
  3315. [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
  3316. [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
  3317. [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
  3318. [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
  3319. [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
  3320. [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op,
  3321. [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op,
  3322. };
  3323. static const int kvm_vmx_max_exit_handlers =
  3324. ARRAY_SIZE(kvm_vmx_exit_handlers);
  3325. static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
  3326. {
  3327. *info1 = vmcs_readl(EXIT_QUALIFICATION);
  3328. *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
  3329. }
  3330. /*
  3331. * The guest has exited. See if we can fix it or if we need userspace
  3332. * assistance.
  3333. */
  3334. static int vmx_handle_exit(struct kvm_vcpu *vcpu)
  3335. {
  3336. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3337. u32 exit_reason = vmx->exit_reason;
  3338. u32 vectoring_info = vmx->idt_vectoring_info;
  3339. trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
  3340. /* If guest state is invalid, start emulating */
  3341. if (vmx->emulation_required && emulate_invalid_guest_state)
  3342. return handle_invalid_guest_state(vcpu);
  3343. if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
  3344. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  3345. vcpu->run->fail_entry.hardware_entry_failure_reason
  3346. = exit_reason;
  3347. return 0;
  3348. }
  3349. if (unlikely(vmx->fail)) {
  3350. vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  3351. vcpu->run->fail_entry.hardware_entry_failure_reason
  3352. = vmcs_read32(VM_INSTRUCTION_ERROR);
  3353. return 0;
  3354. }
  3355. if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
  3356. (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
  3357. exit_reason != EXIT_REASON_EPT_VIOLATION &&
  3358. exit_reason != EXIT_REASON_TASK_SWITCH))
  3359. printk(KERN_WARNING "%s: unexpected, valid vectoring info "
  3360. "(0x%x) and exit reason is 0x%x\n",
  3361. __func__, vectoring_info, exit_reason);
  3362. if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
  3363. if (vmx_interrupt_allowed(vcpu)) {
  3364. vmx->soft_vnmi_blocked = 0;
  3365. } else if (vmx->vnmi_blocked_time > 1000000000LL &&
  3366. vcpu->arch.nmi_pending) {
  3367. /*
  3368. * This CPU don't support us in finding the end of an
  3369. * NMI-blocked window if the guest runs with IRQs
  3370. * disabled. So we pull the trigger after 1 s of
  3371. * futile waiting, but inform the user about this.
  3372. */
  3373. printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
  3374. "state on VCPU %d after 1 s timeout\n",
  3375. __func__, vcpu->vcpu_id);
  3376. vmx->soft_vnmi_blocked = 0;
  3377. }
  3378. }
  3379. if (exit_reason < kvm_vmx_max_exit_handlers
  3380. && kvm_vmx_exit_handlers[exit_reason])
  3381. return kvm_vmx_exit_handlers[exit_reason](vcpu);
  3382. else {
  3383. vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
  3384. vcpu->run->hw.hardware_exit_reason = exit_reason;
  3385. }
  3386. return 0;
  3387. }
  3388. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  3389. {
  3390. if (irr == -1 || tpr < irr) {
  3391. vmcs_write32(TPR_THRESHOLD, 0);
  3392. return;
  3393. }
  3394. vmcs_write32(TPR_THRESHOLD, irr);
  3395. }
  3396. static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
  3397. {
  3398. u32 exit_intr_info;
  3399. if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
  3400. || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
  3401. return;
  3402. vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  3403. exit_intr_info = vmx->exit_intr_info;
  3404. /* Handle machine checks before interrupts are enabled */
  3405. if (is_machine_check(exit_intr_info))
  3406. kvm_machine_check();
  3407. /* We need to handle NMIs before interrupts are enabled */
  3408. if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
  3409. (exit_intr_info & INTR_INFO_VALID_MASK)) {
  3410. kvm_before_handle_nmi(&vmx->vcpu);
  3411. asm("int $2");
  3412. kvm_after_handle_nmi(&vmx->vcpu);
  3413. }
  3414. }
  3415. static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
  3416. {
  3417. u32 exit_intr_info;
  3418. bool unblock_nmi;
  3419. u8 vector;
  3420. bool idtv_info_valid;
  3421. idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  3422. if (cpu_has_virtual_nmis()) {
  3423. if (vmx->nmi_known_unmasked)
  3424. return;
  3425. /*
  3426. * Can't use vmx->exit_intr_info since we're not sure what
  3427. * the exit reason is.
  3428. */
  3429. exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
  3430. unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
  3431. vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
  3432. /*
  3433. * SDM 3: 27.7.1.2 (September 2008)
  3434. * Re-set bit "block by NMI" before VM entry if vmexit caused by
  3435. * a guest IRET fault.
  3436. * SDM 3: 23.2.2 (September 2008)
  3437. * Bit 12 is undefined in any of the following cases:
  3438. * If the VM exit sets the valid bit in the IDT-vectoring
  3439. * information field.
  3440. * If the VM exit is due to a double fault.
  3441. */
  3442. if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
  3443. vector != DF_VECTOR && !idtv_info_valid)
  3444. vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
  3445. GUEST_INTR_STATE_NMI);
  3446. else
  3447. vmx->nmi_known_unmasked =
  3448. !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
  3449. & GUEST_INTR_STATE_NMI);
  3450. } else if (unlikely(vmx->soft_vnmi_blocked))
  3451. vmx->vnmi_blocked_time +=
  3452. ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
  3453. }
  3454. static void __vmx_complete_interrupts(struct vcpu_vmx *vmx,
  3455. u32 idt_vectoring_info,
  3456. int instr_len_field,
  3457. int error_code_field)
  3458. {
  3459. u8 vector;
  3460. int type;
  3461. bool idtv_info_valid;
  3462. idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
  3463. vmx->vcpu.arch.nmi_injected = false;
  3464. kvm_clear_exception_queue(&vmx->vcpu);
  3465. kvm_clear_interrupt_queue(&vmx->vcpu);
  3466. if (!idtv_info_valid)
  3467. return;
  3468. kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
  3469. vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
  3470. type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
  3471. switch (type) {
  3472. case INTR_TYPE_NMI_INTR:
  3473. vmx->vcpu.arch.nmi_injected = true;
  3474. /*
  3475. * SDM 3: 27.7.1.2 (September 2008)
  3476. * Clear bit "block by NMI" before VM entry if a NMI
  3477. * delivery faulted.
  3478. */
  3479. vmx_set_nmi_mask(&vmx->vcpu, false);
  3480. break;
  3481. case INTR_TYPE_SOFT_EXCEPTION:
  3482. vmx->vcpu.arch.event_exit_inst_len =
  3483. vmcs_read32(instr_len_field);
  3484. /* fall through */
  3485. case INTR_TYPE_HARD_EXCEPTION:
  3486. if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
  3487. u32 err = vmcs_read32(error_code_field);
  3488. kvm_queue_exception_e(&vmx->vcpu, vector, err);
  3489. } else
  3490. kvm_queue_exception(&vmx->vcpu, vector);
  3491. break;
  3492. case INTR_TYPE_SOFT_INTR:
  3493. vmx->vcpu.arch.event_exit_inst_len =
  3494. vmcs_read32(instr_len_field);
  3495. /* fall through */
  3496. case INTR_TYPE_EXT_INTR:
  3497. kvm_queue_interrupt(&vmx->vcpu, vector,
  3498. type == INTR_TYPE_SOFT_INTR);
  3499. break;
  3500. default:
  3501. break;
  3502. }
  3503. }
  3504. static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
  3505. {
  3506. __vmx_complete_interrupts(vmx, vmx->idt_vectoring_info,
  3507. VM_EXIT_INSTRUCTION_LEN,
  3508. IDT_VECTORING_ERROR_CODE);
  3509. }
  3510. static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
  3511. {
  3512. __vmx_complete_interrupts(to_vmx(vcpu),
  3513. vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
  3514. VM_ENTRY_INSTRUCTION_LEN,
  3515. VM_ENTRY_EXCEPTION_ERROR_CODE);
  3516. vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
  3517. }
  3518. #ifdef CONFIG_X86_64
  3519. #define R "r"
  3520. #define Q "q"
  3521. #else
  3522. #define R "e"
  3523. #define Q "l"
  3524. #endif
  3525. static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
  3526. {
  3527. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3528. /* Record the guest's net vcpu time for enforced NMI injections. */
  3529. if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
  3530. vmx->entry_time = ktime_get();
  3531. /* Don't enter VMX if guest state is invalid, let the exit handler
  3532. start emulation until we arrive back to a valid state */
  3533. if (vmx->emulation_required && emulate_invalid_guest_state)
  3534. return;
  3535. if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
  3536. vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
  3537. if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
  3538. vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
  3539. /* When single-stepping over STI and MOV SS, we must clear the
  3540. * corresponding interruptibility bits in the guest state. Otherwise
  3541. * vmentry fails as it then expects bit 14 (BS) in pending debug
  3542. * exceptions being set, but that's not correct for the guest debugging
  3543. * case. */
  3544. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
  3545. vmx_set_interrupt_shadow(vcpu, 0);
  3546. asm(
  3547. /* Store host registers */
  3548. "push %%"R"dx; push %%"R"bp;"
  3549. "push %%"R"cx \n\t" /* placeholder for guest rcx */
  3550. "push %%"R"cx \n\t"
  3551. "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
  3552. "je 1f \n\t"
  3553. "mov %%"R"sp, %c[host_rsp](%0) \n\t"
  3554. __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
  3555. "1: \n\t"
  3556. /* Reload cr2 if changed */
  3557. "mov %c[cr2](%0), %%"R"ax \n\t"
  3558. "mov %%cr2, %%"R"dx \n\t"
  3559. "cmp %%"R"ax, %%"R"dx \n\t"
  3560. "je 2f \n\t"
  3561. "mov %%"R"ax, %%cr2 \n\t"
  3562. "2: \n\t"
  3563. /* Check if vmlaunch of vmresume is needed */
  3564. "cmpl $0, %c[launched](%0) \n\t"
  3565. /* Load guest registers. Don't clobber flags. */
  3566. "mov %c[rax](%0), %%"R"ax \n\t"
  3567. "mov %c[rbx](%0), %%"R"bx \n\t"
  3568. "mov %c[rdx](%0), %%"R"dx \n\t"
  3569. "mov %c[rsi](%0), %%"R"si \n\t"
  3570. "mov %c[rdi](%0), %%"R"di \n\t"
  3571. "mov %c[rbp](%0), %%"R"bp \n\t"
  3572. #ifdef CONFIG_X86_64
  3573. "mov %c[r8](%0), %%r8 \n\t"
  3574. "mov %c[r9](%0), %%r9 \n\t"
  3575. "mov %c[r10](%0), %%r10 \n\t"
  3576. "mov %c[r11](%0), %%r11 \n\t"
  3577. "mov %c[r12](%0), %%r12 \n\t"
  3578. "mov %c[r13](%0), %%r13 \n\t"
  3579. "mov %c[r14](%0), %%r14 \n\t"
  3580. "mov %c[r15](%0), %%r15 \n\t"
  3581. #endif
  3582. "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
  3583. /* Enter guest mode */
  3584. "jne .Llaunched \n\t"
  3585. __ex(ASM_VMX_VMLAUNCH) "\n\t"
  3586. "jmp .Lkvm_vmx_return \n\t"
  3587. ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
  3588. ".Lkvm_vmx_return: "
  3589. /* Save guest registers, load host registers, keep flags */
  3590. "mov %0, %c[wordsize](%%"R"sp) \n\t"
  3591. "pop %0 \n\t"
  3592. "mov %%"R"ax, %c[rax](%0) \n\t"
  3593. "mov %%"R"bx, %c[rbx](%0) \n\t"
  3594. "pop"Q" %c[rcx](%0) \n\t"
  3595. "mov %%"R"dx, %c[rdx](%0) \n\t"
  3596. "mov %%"R"si, %c[rsi](%0) \n\t"
  3597. "mov %%"R"di, %c[rdi](%0) \n\t"
  3598. "mov %%"R"bp, %c[rbp](%0) \n\t"
  3599. #ifdef CONFIG_X86_64
  3600. "mov %%r8, %c[r8](%0) \n\t"
  3601. "mov %%r9, %c[r9](%0) \n\t"
  3602. "mov %%r10, %c[r10](%0) \n\t"
  3603. "mov %%r11, %c[r11](%0) \n\t"
  3604. "mov %%r12, %c[r12](%0) \n\t"
  3605. "mov %%r13, %c[r13](%0) \n\t"
  3606. "mov %%r14, %c[r14](%0) \n\t"
  3607. "mov %%r15, %c[r15](%0) \n\t"
  3608. #endif
  3609. "mov %%cr2, %%"R"ax \n\t"
  3610. "mov %%"R"ax, %c[cr2](%0) \n\t"
  3611. "pop %%"R"bp; pop %%"R"dx \n\t"
  3612. "setbe %c[fail](%0) \n\t"
  3613. : : "c"(vmx), "d"((unsigned long)HOST_RSP),
  3614. [launched]"i"(offsetof(struct vcpu_vmx, launched)),
  3615. [fail]"i"(offsetof(struct vcpu_vmx, fail)),
  3616. [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
  3617. [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
  3618. [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
  3619. [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
  3620. [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
  3621. [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
  3622. [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
  3623. [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
  3624. #ifdef CONFIG_X86_64
  3625. [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
  3626. [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
  3627. [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
  3628. [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
  3629. [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
  3630. [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
  3631. [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
  3632. [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
  3633. #endif
  3634. [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
  3635. [wordsize]"i"(sizeof(ulong))
  3636. : "cc", "memory"
  3637. , R"ax", R"bx", R"di", R"si"
  3638. #ifdef CONFIG_X86_64
  3639. , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
  3640. #endif
  3641. );
  3642. vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
  3643. | (1 << VCPU_EXREG_RFLAGS)
  3644. | (1 << VCPU_EXREG_CPL)
  3645. | (1 << VCPU_EXREG_PDPTR)
  3646. | (1 << VCPU_EXREG_SEGMENTS)
  3647. | (1 << VCPU_EXREG_CR3));
  3648. vcpu->arch.regs_dirty = 0;
  3649. vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
  3650. asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
  3651. vmx->launched = 1;
  3652. vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
  3653. vmx_complete_atomic_exit(vmx);
  3654. vmx_recover_nmi_blocking(vmx);
  3655. vmx_complete_interrupts(vmx);
  3656. }
  3657. #undef R
  3658. #undef Q
  3659. static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
  3660. {
  3661. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3662. if (vmx->vmcs) {
  3663. vcpu_clear(vmx);
  3664. free_vmcs(vmx->vmcs);
  3665. vmx->vmcs = NULL;
  3666. }
  3667. }
  3668. static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
  3669. {
  3670. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3671. free_vpid(vmx);
  3672. vmx_free_vmcs(vcpu);
  3673. kfree(vmx->guest_msrs);
  3674. kvm_vcpu_uninit(vcpu);
  3675. kmem_cache_free(kvm_vcpu_cache, vmx);
  3676. }
  3677. static inline void vmcs_init(struct vmcs *vmcs)
  3678. {
  3679. u64 phys_addr = __pa(per_cpu(vmxarea, raw_smp_processor_id()));
  3680. if (!vmm_exclusive)
  3681. kvm_cpu_vmxon(phys_addr);
  3682. vmcs_clear(vmcs);
  3683. if (!vmm_exclusive)
  3684. kvm_cpu_vmxoff();
  3685. }
  3686. static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
  3687. {
  3688. int err;
  3689. struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  3690. int cpu;
  3691. if (!vmx)
  3692. return ERR_PTR(-ENOMEM);
  3693. allocate_vpid(vmx);
  3694. err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
  3695. if (err)
  3696. goto free_vcpu;
  3697. vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
  3698. err = -ENOMEM;
  3699. if (!vmx->guest_msrs) {
  3700. goto uninit_vcpu;
  3701. }
  3702. vmx->vmcs = alloc_vmcs();
  3703. if (!vmx->vmcs)
  3704. goto free_msrs;
  3705. vmcs_init(vmx->vmcs);
  3706. cpu = get_cpu();
  3707. vmx_vcpu_load(&vmx->vcpu, cpu);
  3708. vmx->vcpu.cpu = cpu;
  3709. err = vmx_vcpu_setup(vmx);
  3710. vmx_vcpu_put(&vmx->vcpu);
  3711. put_cpu();
  3712. if (err)
  3713. goto free_vmcs;
  3714. if (vm_need_virtualize_apic_accesses(kvm))
  3715. err = alloc_apic_access_page(kvm);
  3716. if (err)
  3717. goto free_vmcs;
  3718. if (enable_ept) {
  3719. if (!kvm->arch.ept_identity_map_addr)
  3720. kvm->arch.ept_identity_map_addr =
  3721. VMX_EPT_IDENTITY_PAGETABLE_ADDR;
  3722. err = -ENOMEM;
  3723. if (alloc_identity_pagetable(kvm) != 0)
  3724. goto free_vmcs;
  3725. if (!init_rmode_identity_map(kvm))
  3726. goto free_vmcs;
  3727. }
  3728. return &vmx->vcpu;
  3729. free_vmcs:
  3730. free_vmcs(vmx->vmcs);
  3731. free_msrs:
  3732. kfree(vmx->guest_msrs);
  3733. uninit_vcpu:
  3734. kvm_vcpu_uninit(&vmx->vcpu);
  3735. free_vcpu:
  3736. free_vpid(vmx);
  3737. kmem_cache_free(kvm_vcpu_cache, vmx);
  3738. return ERR_PTR(err);
  3739. }
  3740. static void __init vmx_check_processor_compat(void *rtn)
  3741. {
  3742. struct vmcs_config vmcs_conf;
  3743. *(int *)rtn = 0;
  3744. if (setup_vmcs_config(&vmcs_conf) < 0)
  3745. *(int *)rtn = -EIO;
  3746. if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
  3747. printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
  3748. smp_processor_id());
  3749. *(int *)rtn = -EIO;
  3750. }
  3751. }
  3752. static int get_ept_level(void)
  3753. {
  3754. return VMX_EPT_DEFAULT_GAW + 1;
  3755. }
  3756. static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  3757. {
  3758. u64 ret;
  3759. /* For VT-d and EPT combination
  3760. * 1. MMIO: always map as UC
  3761. * 2. EPT with VT-d:
  3762. * a. VT-d without snooping control feature: can't guarantee the
  3763. * result, try to trust guest.
  3764. * b. VT-d with snooping control feature: snooping control feature of
  3765. * VT-d engine can guarantee the cache correctness. Just set it
  3766. * to WB to keep consistent with host. So the same as item 3.
  3767. * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
  3768. * consistent with host MTRR
  3769. */
  3770. if (is_mmio)
  3771. ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
  3772. else if (vcpu->kvm->arch.iommu_domain &&
  3773. !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
  3774. ret = kvm_get_guest_memory_type(vcpu, gfn) <<
  3775. VMX_EPT_MT_EPTE_SHIFT;
  3776. else
  3777. ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
  3778. | VMX_EPT_IPAT_BIT;
  3779. return ret;
  3780. }
  3781. #define _ER(x) { EXIT_REASON_##x, #x }
  3782. static const struct trace_print_flags vmx_exit_reasons_str[] = {
  3783. _ER(EXCEPTION_NMI),
  3784. _ER(EXTERNAL_INTERRUPT),
  3785. _ER(TRIPLE_FAULT),
  3786. _ER(PENDING_INTERRUPT),
  3787. _ER(NMI_WINDOW),
  3788. _ER(TASK_SWITCH),
  3789. _ER(CPUID),
  3790. _ER(HLT),
  3791. _ER(INVLPG),
  3792. _ER(RDPMC),
  3793. _ER(RDTSC),
  3794. _ER(VMCALL),
  3795. _ER(VMCLEAR),
  3796. _ER(VMLAUNCH),
  3797. _ER(VMPTRLD),
  3798. _ER(VMPTRST),
  3799. _ER(VMREAD),
  3800. _ER(VMRESUME),
  3801. _ER(VMWRITE),
  3802. _ER(VMOFF),
  3803. _ER(VMON),
  3804. _ER(CR_ACCESS),
  3805. _ER(DR_ACCESS),
  3806. _ER(IO_INSTRUCTION),
  3807. _ER(MSR_READ),
  3808. _ER(MSR_WRITE),
  3809. _ER(MWAIT_INSTRUCTION),
  3810. _ER(MONITOR_INSTRUCTION),
  3811. _ER(PAUSE_INSTRUCTION),
  3812. _ER(MCE_DURING_VMENTRY),
  3813. _ER(TPR_BELOW_THRESHOLD),
  3814. _ER(APIC_ACCESS),
  3815. _ER(EPT_VIOLATION),
  3816. _ER(EPT_MISCONFIG),
  3817. _ER(WBINVD),
  3818. { -1, NULL }
  3819. };
  3820. #undef _ER
  3821. static int vmx_get_lpage_level(void)
  3822. {
  3823. if (enable_ept && !cpu_has_vmx_ept_1g_page())
  3824. return PT_DIRECTORY_LEVEL;
  3825. else
  3826. /* For shadow and EPT supported 1GB page */
  3827. return PT_PDPE_LEVEL;
  3828. }
  3829. static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
  3830. {
  3831. struct kvm_cpuid_entry2 *best;
  3832. struct vcpu_vmx *vmx = to_vmx(vcpu);
  3833. u32 exec_control;
  3834. vmx->rdtscp_enabled = false;
  3835. if (vmx_rdtscp_supported()) {
  3836. exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
  3837. if (exec_control & SECONDARY_EXEC_RDTSCP) {
  3838. best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
  3839. if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
  3840. vmx->rdtscp_enabled = true;
  3841. else {
  3842. exec_control &= ~SECONDARY_EXEC_RDTSCP;
  3843. vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
  3844. exec_control);
  3845. }
  3846. }
  3847. }
  3848. }
  3849. static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
  3850. {
  3851. }
  3852. static int vmx_check_intercept(struct kvm_vcpu *vcpu,
  3853. struct x86_instruction_info *info,
  3854. enum x86_intercept_stage stage)
  3855. {
  3856. return X86EMUL_CONTINUE;
  3857. }
  3858. static struct kvm_x86_ops vmx_x86_ops = {
  3859. .cpu_has_kvm_support = cpu_has_kvm_support,
  3860. .disabled_by_bios = vmx_disabled_by_bios,
  3861. .hardware_setup = hardware_setup,
  3862. .hardware_unsetup = hardware_unsetup,
  3863. .check_processor_compatibility = vmx_check_processor_compat,
  3864. .hardware_enable = hardware_enable,
  3865. .hardware_disable = hardware_disable,
  3866. .cpu_has_accelerated_tpr = report_flexpriority,
  3867. .vcpu_create = vmx_create_vcpu,
  3868. .vcpu_free = vmx_free_vcpu,
  3869. .vcpu_reset = vmx_vcpu_reset,
  3870. .prepare_guest_switch = vmx_save_host_state,
  3871. .vcpu_load = vmx_vcpu_load,
  3872. .vcpu_put = vmx_vcpu_put,
  3873. .set_guest_debug = set_guest_debug,
  3874. .get_msr = vmx_get_msr,
  3875. .set_msr = vmx_set_msr,
  3876. .get_segment_base = vmx_get_segment_base,
  3877. .get_segment = vmx_get_segment,
  3878. .set_segment = vmx_set_segment,
  3879. .get_cpl = vmx_get_cpl,
  3880. .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
  3881. .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
  3882. .decache_cr3 = vmx_decache_cr3,
  3883. .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
  3884. .set_cr0 = vmx_set_cr0,
  3885. .set_cr3 = vmx_set_cr3,
  3886. .set_cr4 = vmx_set_cr4,
  3887. .set_efer = vmx_set_efer,
  3888. .get_idt = vmx_get_idt,
  3889. .set_idt = vmx_set_idt,
  3890. .get_gdt = vmx_get_gdt,
  3891. .set_gdt = vmx_set_gdt,
  3892. .set_dr7 = vmx_set_dr7,
  3893. .cache_reg = vmx_cache_reg,
  3894. .get_rflags = vmx_get_rflags,
  3895. .set_rflags = vmx_set_rflags,
  3896. .fpu_activate = vmx_fpu_activate,
  3897. .fpu_deactivate = vmx_fpu_deactivate,
  3898. .tlb_flush = vmx_flush_tlb,
  3899. .run = vmx_vcpu_run,
  3900. .handle_exit = vmx_handle_exit,
  3901. .skip_emulated_instruction = skip_emulated_instruction,
  3902. .set_interrupt_shadow = vmx_set_interrupt_shadow,
  3903. .get_interrupt_shadow = vmx_get_interrupt_shadow,
  3904. .patch_hypercall = vmx_patch_hypercall,
  3905. .set_irq = vmx_inject_irq,
  3906. .set_nmi = vmx_inject_nmi,
  3907. .queue_exception = vmx_queue_exception,
  3908. .cancel_injection = vmx_cancel_injection,
  3909. .interrupt_allowed = vmx_interrupt_allowed,
  3910. .nmi_allowed = vmx_nmi_allowed,
  3911. .get_nmi_mask = vmx_get_nmi_mask,
  3912. .set_nmi_mask = vmx_set_nmi_mask,
  3913. .enable_nmi_window = enable_nmi_window,
  3914. .enable_irq_window = enable_irq_window,
  3915. .update_cr8_intercept = update_cr8_intercept,
  3916. .set_tss_addr = vmx_set_tss_addr,
  3917. .get_tdp_level = get_ept_level,
  3918. .get_mt_mask = vmx_get_mt_mask,
  3919. .get_exit_info = vmx_get_exit_info,
  3920. .exit_reasons_str = vmx_exit_reasons_str,
  3921. .get_lpage_level = vmx_get_lpage_level,
  3922. .cpuid_update = vmx_cpuid_update,
  3923. .rdtscp_supported = vmx_rdtscp_supported,
  3924. .set_supported_cpuid = vmx_set_supported_cpuid,
  3925. .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
  3926. .set_tsc_khz = vmx_set_tsc_khz,
  3927. .write_tsc_offset = vmx_write_tsc_offset,
  3928. .adjust_tsc_offset = vmx_adjust_tsc_offset,
  3929. .compute_tsc_offset = vmx_compute_tsc_offset,
  3930. .set_tdp_cr3 = vmx_set_cr3,
  3931. .check_intercept = vmx_check_intercept,
  3932. };
  3933. static int __init vmx_init(void)
  3934. {
  3935. int r, i;
  3936. rdmsrl_safe(MSR_EFER, &host_efer);
  3937. for (i = 0; i < NR_VMX_MSR; ++i)
  3938. kvm_define_shared_msr(i, vmx_msr_index[i]);
  3939. vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
  3940. if (!vmx_io_bitmap_a)
  3941. return -ENOMEM;
  3942. vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
  3943. if (!vmx_io_bitmap_b) {
  3944. r = -ENOMEM;
  3945. goto out;
  3946. }
  3947. vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
  3948. if (!vmx_msr_bitmap_legacy) {
  3949. r = -ENOMEM;
  3950. goto out1;
  3951. }
  3952. vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
  3953. if (!vmx_msr_bitmap_longmode) {
  3954. r = -ENOMEM;
  3955. goto out2;
  3956. }
  3957. /*
  3958. * Allow direct access to the PC debug port (it is often used for I/O
  3959. * delays, but the vmexits simply slow things down).
  3960. */
  3961. memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
  3962. clear_bit(0x80, vmx_io_bitmap_a);
  3963. memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
  3964. memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
  3965. memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
  3966. set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
  3967. r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
  3968. __alignof__(struct vcpu_vmx), THIS_MODULE);
  3969. if (r)
  3970. goto out3;
  3971. vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
  3972. vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
  3973. vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
  3974. vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
  3975. vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
  3976. vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
  3977. if (enable_ept) {
  3978. bypass_guest_pf = 0;
  3979. kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
  3980. VMX_EPT_EXECUTABLE_MASK);
  3981. kvm_enable_tdp();
  3982. } else
  3983. kvm_disable_tdp();
  3984. if (bypass_guest_pf)
  3985. kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
  3986. return 0;
  3987. out3:
  3988. free_page((unsigned long)vmx_msr_bitmap_longmode);
  3989. out2:
  3990. free_page((unsigned long)vmx_msr_bitmap_legacy);
  3991. out1:
  3992. free_page((unsigned long)vmx_io_bitmap_b);
  3993. out:
  3994. free_page((unsigned long)vmx_io_bitmap_a);
  3995. return r;
  3996. }
  3997. static void __exit vmx_exit(void)
  3998. {
  3999. free_page((unsigned long)vmx_msr_bitmap_legacy);
  4000. free_page((unsigned long)vmx_msr_bitmap_longmode);
  4001. free_page((unsigned long)vmx_io_bitmap_b);
  4002. free_page((unsigned long)vmx_io_bitmap_a);
  4003. kvm_exit();
  4004. }
  4005. module_init(vmx_init)
  4006. module_exit(vmx_exit)