vmx.c 119 KB

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