vmx.c 115 KB

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