vmx.c 102 KB

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