vmx.c 113 KB

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