vmx.c 115 KB

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