svm.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * AMD SVM support
  5. *
  6. * Copyright (C) 2006 Qumranet, Inc.
  7. *
  8. * Authors:
  9. * Yaniv Kamay <yaniv@qumranet.com>
  10. * Avi Kivity <avi@qumranet.com>
  11. *
  12. * This work is licensed under the terms of the GNU GPL, version 2. See
  13. * the COPYING file in the top-level directory.
  14. *
  15. */
  16. #include <linux/kvm_host.h>
  17. #include "irq.h"
  18. #include "mmu.h"
  19. #include "kvm_cache_regs.h"
  20. #include "x86.h"
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/highmem.h>
  25. #include <linux/sched.h>
  26. #include <linux/ftrace_event.h>
  27. #include <linux/slab.h>
  28. #include <asm/desc.h>
  29. #include <asm/virtext.h>
  30. #include "trace.h"
  31. #define __ex(x) __kvm_handle_fault_on_reboot(x)
  32. MODULE_AUTHOR("Qumranet");
  33. MODULE_LICENSE("GPL");
  34. #define IOPM_ALLOC_ORDER 2
  35. #define MSRPM_ALLOC_ORDER 1
  36. #define SEG_TYPE_LDT 2
  37. #define SEG_TYPE_BUSY_TSS16 3
  38. #define SVM_FEATURE_NPT (1 << 0)
  39. #define SVM_FEATURE_LBRV (1 << 1)
  40. #define SVM_FEATURE_SVML (1 << 2)
  41. #define SVM_FEATURE_NRIP (1 << 3)
  42. #define SVM_FEATURE_PAUSE_FILTER (1 << 10)
  43. #define NESTED_EXIT_HOST 0 /* Exit handled on host level */
  44. #define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
  45. #define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
  46. #define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
  47. static const u32 host_save_user_msrs[] = {
  48. #ifdef CONFIG_X86_64
  49. MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
  50. MSR_FS_BASE,
  51. #endif
  52. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  53. };
  54. #define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
  55. struct kvm_vcpu;
  56. struct nested_state {
  57. struct vmcb *hsave;
  58. u64 hsave_msr;
  59. u64 vmcb;
  60. /* These are the merged vectors */
  61. u32 *msrpm;
  62. /* gpa pointers to the real vectors */
  63. u64 vmcb_msrpm;
  64. /* A VMEXIT is required but not yet emulated */
  65. bool exit_required;
  66. /* cache for intercepts of the guest */
  67. u16 intercept_cr_read;
  68. u16 intercept_cr_write;
  69. u16 intercept_dr_read;
  70. u16 intercept_dr_write;
  71. u32 intercept_exceptions;
  72. u64 intercept;
  73. };
  74. struct vcpu_svm {
  75. struct kvm_vcpu vcpu;
  76. struct vmcb *vmcb;
  77. unsigned long vmcb_pa;
  78. struct svm_cpu_data *svm_data;
  79. uint64_t asid_generation;
  80. uint64_t sysenter_esp;
  81. uint64_t sysenter_eip;
  82. u64 next_rip;
  83. u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
  84. u64 host_gs_base;
  85. u32 *msrpm;
  86. struct nested_state nested;
  87. bool nmi_singlestep;
  88. unsigned int3_injected;
  89. unsigned long int3_rip;
  90. };
  91. /* enable NPT for AMD64 and X86 with PAE */
  92. #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
  93. static bool npt_enabled = true;
  94. #else
  95. static bool npt_enabled = false;
  96. #endif
  97. static int npt = 1;
  98. module_param(npt, int, S_IRUGO);
  99. static int nested = 1;
  100. module_param(nested, int, S_IRUGO);
  101. static void svm_flush_tlb(struct kvm_vcpu *vcpu);
  102. static void svm_complete_interrupts(struct vcpu_svm *svm);
  103. static int nested_svm_exit_handled(struct vcpu_svm *svm);
  104. static int nested_svm_intercept(struct vcpu_svm *svm);
  105. static int nested_svm_vmexit(struct vcpu_svm *svm);
  106. static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
  107. bool has_error_code, u32 error_code);
  108. static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
  109. {
  110. return container_of(vcpu, struct vcpu_svm, vcpu);
  111. }
  112. static inline bool is_nested(struct vcpu_svm *svm)
  113. {
  114. return svm->nested.vmcb;
  115. }
  116. static inline void enable_gif(struct vcpu_svm *svm)
  117. {
  118. svm->vcpu.arch.hflags |= HF_GIF_MASK;
  119. }
  120. static inline void disable_gif(struct vcpu_svm *svm)
  121. {
  122. svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
  123. }
  124. static inline bool gif_set(struct vcpu_svm *svm)
  125. {
  126. return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
  127. }
  128. static unsigned long iopm_base;
  129. struct kvm_ldttss_desc {
  130. u16 limit0;
  131. u16 base0;
  132. unsigned base1 : 8, type : 5, dpl : 2, p : 1;
  133. unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8;
  134. u32 base3;
  135. u32 zero1;
  136. } __attribute__((packed));
  137. struct svm_cpu_data {
  138. int cpu;
  139. u64 asid_generation;
  140. u32 max_asid;
  141. u32 next_asid;
  142. struct kvm_ldttss_desc *tss_desc;
  143. struct page *save_area;
  144. };
  145. static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
  146. static uint32_t svm_features;
  147. struct svm_init_data {
  148. int cpu;
  149. int r;
  150. };
  151. static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
  152. #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
  153. #define MSRS_RANGE_SIZE 2048
  154. #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
  155. #define MAX_INST_SIZE 15
  156. static inline u32 svm_has(u32 feat)
  157. {
  158. return svm_features & feat;
  159. }
  160. static inline void clgi(void)
  161. {
  162. asm volatile (__ex(SVM_CLGI));
  163. }
  164. static inline void stgi(void)
  165. {
  166. asm volatile (__ex(SVM_STGI));
  167. }
  168. static inline void invlpga(unsigned long addr, u32 asid)
  169. {
  170. asm volatile (__ex(SVM_INVLPGA) :: "a"(addr), "c"(asid));
  171. }
  172. static inline void force_new_asid(struct kvm_vcpu *vcpu)
  173. {
  174. to_svm(vcpu)->asid_generation--;
  175. }
  176. static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
  177. {
  178. force_new_asid(vcpu);
  179. }
  180. static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  181. {
  182. if (!npt_enabled && !(efer & EFER_LMA))
  183. efer &= ~EFER_LME;
  184. to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
  185. vcpu->arch.efer = efer;
  186. }
  187. static int is_external_interrupt(u32 info)
  188. {
  189. info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
  190. return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
  191. }
  192. static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  193. {
  194. struct vcpu_svm *svm = to_svm(vcpu);
  195. u32 ret = 0;
  196. if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
  197. ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
  198. return ret & mask;
  199. }
  200. static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  201. {
  202. struct vcpu_svm *svm = to_svm(vcpu);
  203. if (mask == 0)
  204. svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
  205. else
  206. svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
  207. }
  208. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  209. {
  210. struct vcpu_svm *svm = to_svm(vcpu);
  211. if (!svm->next_rip) {
  212. if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) !=
  213. EMULATE_DONE)
  214. printk(KERN_DEBUG "%s: NOP\n", __func__);
  215. return;
  216. }
  217. if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
  218. printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
  219. __func__, kvm_rip_read(vcpu), svm->next_rip);
  220. kvm_rip_write(vcpu, svm->next_rip);
  221. svm_set_interrupt_shadow(vcpu, 0);
  222. }
  223. static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
  224. bool has_error_code, u32 error_code)
  225. {
  226. struct vcpu_svm *svm = to_svm(vcpu);
  227. /* If we are within a nested VM we'd better #VMEXIT and let the
  228. guest handle the exception */
  229. if (nested_svm_check_exception(svm, nr, has_error_code, error_code))
  230. return;
  231. if (nr == BP_VECTOR && !svm_has(SVM_FEATURE_NRIP)) {
  232. unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
  233. /*
  234. * For guest debugging where we have to reinject #BP if some
  235. * INT3 is guest-owned:
  236. * Emulate nRIP by moving RIP forward. Will fail if injection
  237. * raises a fault that is not intercepted. Still better than
  238. * failing in all cases.
  239. */
  240. skip_emulated_instruction(&svm->vcpu);
  241. rip = kvm_rip_read(&svm->vcpu);
  242. svm->int3_rip = rip + svm->vmcb->save.cs.base;
  243. svm->int3_injected = rip - old_rip;
  244. }
  245. svm->vmcb->control.event_inj = nr
  246. | SVM_EVTINJ_VALID
  247. | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
  248. | SVM_EVTINJ_TYPE_EXEPT;
  249. svm->vmcb->control.event_inj_err = error_code;
  250. }
  251. static int has_svm(void)
  252. {
  253. const char *msg;
  254. if (!cpu_has_svm(&msg)) {
  255. printk(KERN_INFO "has_svm: %s\n", msg);
  256. return 0;
  257. }
  258. return 1;
  259. }
  260. static void svm_hardware_disable(void *garbage)
  261. {
  262. cpu_svm_disable();
  263. }
  264. static int svm_hardware_enable(void *garbage)
  265. {
  266. struct svm_cpu_data *sd;
  267. uint64_t efer;
  268. struct desc_ptr gdt_descr;
  269. struct desc_struct *gdt;
  270. int me = raw_smp_processor_id();
  271. rdmsrl(MSR_EFER, efer);
  272. if (efer & EFER_SVME)
  273. return -EBUSY;
  274. if (!has_svm()) {
  275. printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
  276. me);
  277. return -EINVAL;
  278. }
  279. sd = per_cpu(svm_data, me);
  280. if (!sd) {
  281. printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
  282. me);
  283. return -EINVAL;
  284. }
  285. sd->asid_generation = 1;
  286. sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
  287. sd->next_asid = sd->max_asid + 1;
  288. kvm_get_gdt(&gdt_descr);
  289. gdt = (struct desc_struct *)gdt_descr.address;
  290. sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
  291. wrmsrl(MSR_EFER, efer | EFER_SVME);
  292. wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
  293. return 0;
  294. }
  295. static void svm_cpu_uninit(int cpu)
  296. {
  297. struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
  298. if (!sd)
  299. return;
  300. per_cpu(svm_data, raw_smp_processor_id()) = NULL;
  301. __free_page(sd->save_area);
  302. kfree(sd);
  303. }
  304. static int svm_cpu_init(int cpu)
  305. {
  306. struct svm_cpu_data *sd;
  307. int r;
  308. sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
  309. if (!sd)
  310. return -ENOMEM;
  311. sd->cpu = cpu;
  312. sd->save_area = alloc_page(GFP_KERNEL);
  313. r = -ENOMEM;
  314. if (!sd->save_area)
  315. goto err_1;
  316. per_cpu(svm_data, cpu) = sd;
  317. return 0;
  318. err_1:
  319. kfree(sd);
  320. return r;
  321. }
  322. static void set_msr_interception(u32 *msrpm, unsigned msr,
  323. int read, int write)
  324. {
  325. int i;
  326. for (i = 0; i < NUM_MSR_MAPS; i++) {
  327. if (msr >= msrpm_ranges[i] &&
  328. msr < msrpm_ranges[i] + MSRS_IN_RANGE) {
  329. u32 msr_offset = (i * MSRS_IN_RANGE + msr -
  330. msrpm_ranges[i]) * 2;
  331. u32 *base = msrpm + (msr_offset / 32);
  332. u32 msr_shift = msr_offset % 32;
  333. u32 mask = ((write) ? 0 : 2) | ((read) ? 0 : 1);
  334. *base = (*base & ~(0x3 << msr_shift)) |
  335. (mask << msr_shift);
  336. return;
  337. }
  338. }
  339. BUG();
  340. }
  341. static void svm_vcpu_init_msrpm(u32 *msrpm)
  342. {
  343. memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
  344. #ifdef CONFIG_X86_64
  345. set_msr_interception(msrpm, MSR_GS_BASE, 1, 1);
  346. set_msr_interception(msrpm, MSR_FS_BASE, 1, 1);
  347. set_msr_interception(msrpm, MSR_KERNEL_GS_BASE, 1, 1);
  348. set_msr_interception(msrpm, MSR_LSTAR, 1, 1);
  349. set_msr_interception(msrpm, MSR_CSTAR, 1, 1);
  350. set_msr_interception(msrpm, MSR_SYSCALL_MASK, 1, 1);
  351. #endif
  352. set_msr_interception(msrpm, MSR_K6_STAR, 1, 1);
  353. set_msr_interception(msrpm, MSR_IA32_SYSENTER_CS, 1, 1);
  354. }
  355. static void svm_enable_lbrv(struct vcpu_svm *svm)
  356. {
  357. u32 *msrpm = svm->msrpm;
  358. svm->vmcb->control.lbr_ctl = 1;
  359. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
  360. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
  361. set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
  362. set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
  363. }
  364. static void svm_disable_lbrv(struct vcpu_svm *svm)
  365. {
  366. u32 *msrpm = svm->msrpm;
  367. svm->vmcb->control.lbr_ctl = 0;
  368. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
  369. set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
  370. set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
  371. set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
  372. }
  373. static __init int svm_hardware_setup(void)
  374. {
  375. int cpu;
  376. struct page *iopm_pages;
  377. void *iopm_va;
  378. int r;
  379. iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
  380. if (!iopm_pages)
  381. return -ENOMEM;
  382. iopm_va = page_address(iopm_pages);
  383. memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
  384. iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
  385. if (boot_cpu_has(X86_FEATURE_NX))
  386. kvm_enable_efer_bits(EFER_NX);
  387. if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
  388. kvm_enable_efer_bits(EFER_FFXSR);
  389. if (nested) {
  390. printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
  391. kvm_enable_efer_bits(EFER_SVME);
  392. }
  393. for_each_possible_cpu(cpu) {
  394. r = svm_cpu_init(cpu);
  395. if (r)
  396. goto err;
  397. }
  398. svm_features = cpuid_edx(SVM_CPUID_FUNC);
  399. if (!svm_has(SVM_FEATURE_NPT))
  400. npt_enabled = false;
  401. if (npt_enabled && !npt) {
  402. printk(KERN_INFO "kvm: Nested Paging disabled\n");
  403. npt_enabled = false;
  404. }
  405. if (npt_enabled) {
  406. printk(KERN_INFO "kvm: Nested Paging enabled\n");
  407. kvm_enable_tdp();
  408. } else
  409. kvm_disable_tdp();
  410. return 0;
  411. err:
  412. __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
  413. iopm_base = 0;
  414. return r;
  415. }
  416. static __exit void svm_hardware_unsetup(void)
  417. {
  418. int cpu;
  419. for_each_possible_cpu(cpu)
  420. svm_cpu_uninit(cpu);
  421. __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
  422. iopm_base = 0;
  423. }
  424. static void init_seg(struct vmcb_seg *seg)
  425. {
  426. seg->selector = 0;
  427. seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
  428. SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
  429. seg->limit = 0xffff;
  430. seg->base = 0;
  431. }
  432. static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
  433. {
  434. seg->selector = 0;
  435. seg->attrib = SVM_SELECTOR_P_MASK | type;
  436. seg->limit = 0xffff;
  437. seg->base = 0;
  438. }
  439. static void init_vmcb(struct vcpu_svm *svm)
  440. {
  441. struct vmcb_control_area *control = &svm->vmcb->control;
  442. struct vmcb_save_area *save = &svm->vmcb->save;
  443. svm->vcpu.fpu_active = 1;
  444. control->intercept_cr_read = INTERCEPT_CR0_MASK |
  445. INTERCEPT_CR3_MASK |
  446. INTERCEPT_CR4_MASK;
  447. control->intercept_cr_write = INTERCEPT_CR0_MASK |
  448. INTERCEPT_CR3_MASK |
  449. INTERCEPT_CR4_MASK |
  450. INTERCEPT_CR8_MASK;
  451. control->intercept_dr_read = INTERCEPT_DR0_MASK |
  452. INTERCEPT_DR1_MASK |
  453. INTERCEPT_DR2_MASK |
  454. INTERCEPT_DR3_MASK |
  455. INTERCEPT_DR4_MASK |
  456. INTERCEPT_DR5_MASK |
  457. INTERCEPT_DR6_MASK |
  458. INTERCEPT_DR7_MASK;
  459. control->intercept_dr_write = INTERCEPT_DR0_MASK |
  460. INTERCEPT_DR1_MASK |
  461. INTERCEPT_DR2_MASK |
  462. INTERCEPT_DR3_MASK |
  463. INTERCEPT_DR4_MASK |
  464. INTERCEPT_DR5_MASK |
  465. INTERCEPT_DR6_MASK |
  466. INTERCEPT_DR7_MASK;
  467. control->intercept_exceptions = (1 << PF_VECTOR) |
  468. (1 << UD_VECTOR) |
  469. (1 << MC_VECTOR);
  470. control->intercept = (1ULL << INTERCEPT_INTR) |
  471. (1ULL << INTERCEPT_NMI) |
  472. (1ULL << INTERCEPT_SMI) |
  473. (1ULL << INTERCEPT_SELECTIVE_CR0) |
  474. (1ULL << INTERCEPT_CPUID) |
  475. (1ULL << INTERCEPT_INVD) |
  476. (1ULL << INTERCEPT_HLT) |
  477. (1ULL << INTERCEPT_INVLPG) |
  478. (1ULL << INTERCEPT_INVLPGA) |
  479. (1ULL << INTERCEPT_IOIO_PROT) |
  480. (1ULL << INTERCEPT_MSR_PROT) |
  481. (1ULL << INTERCEPT_TASK_SWITCH) |
  482. (1ULL << INTERCEPT_SHUTDOWN) |
  483. (1ULL << INTERCEPT_VMRUN) |
  484. (1ULL << INTERCEPT_VMMCALL) |
  485. (1ULL << INTERCEPT_VMLOAD) |
  486. (1ULL << INTERCEPT_VMSAVE) |
  487. (1ULL << INTERCEPT_STGI) |
  488. (1ULL << INTERCEPT_CLGI) |
  489. (1ULL << INTERCEPT_SKINIT) |
  490. (1ULL << INTERCEPT_WBINVD) |
  491. (1ULL << INTERCEPT_MONITOR) |
  492. (1ULL << INTERCEPT_MWAIT);
  493. control->iopm_base_pa = iopm_base;
  494. control->msrpm_base_pa = __pa(svm->msrpm);
  495. control->tsc_offset = 0;
  496. control->int_ctl = V_INTR_MASKING_MASK;
  497. init_seg(&save->es);
  498. init_seg(&save->ss);
  499. init_seg(&save->ds);
  500. init_seg(&save->fs);
  501. init_seg(&save->gs);
  502. save->cs.selector = 0xf000;
  503. /* Executable/Readable Code Segment */
  504. save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
  505. SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
  506. save->cs.limit = 0xffff;
  507. /*
  508. * cs.base should really be 0xffff0000, but vmx can't handle that, so
  509. * be consistent with it.
  510. *
  511. * Replace when we have real mode working for vmx.
  512. */
  513. save->cs.base = 0xf0000;
  514. save->gdtr.limit = 0xffff;
  515. save->idtr.limit = 0xffff;
  516. init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
  517. init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
  518. save->efer = EFER_SVME;
  519. save->dr6 = 0xffff0ff0;
  520. save->dr7 = 0x400;
  521. save->rflags = 2;
  522. save->rip = 0x0000fff0;
  523. svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
  524. /* This is the guest-visible cr0 value.
  525. * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
  526. */
  527. svm->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
  528. kvm_set_cr0(&svm->vcpu, svm->vcpu.arch.cr0);
  529. save->cr4 = X86_CR4_PAE;
  530. /* rdx = ?? */
  531. if (npt_enabled) {
  532. /* Setup VMCB for Nested Paging */
  533. control->nested_ctl = 1;
  534. control->intercept &= ~((1ULL << INTERCEPT_TASK_SWITCH) |
  535. (1ULL << INTERCEPT_INVLPG));
  536. control->intercept_exceptions &= ~(1 << PF_VECTOR);
  537. control->intercept_cr_read &= ~INTERCEPT_CR3_MASK;
  538. control->intercept_cr_write &= ~INTERCEPT_CR3_MASK;
  539. save->g_pat = 0x0007040600070406ULL;
  540. save->cr3 = 0;
  541. save->cr4 = 0;
  542. }
  543. force_new_asid(&svm->vcpu);
  544. svm->nested.vmcb = 0;
  545. svm->vcpu.arch.hflags = 0;
  546. if (svm_has(SVM_FEATURE_PAUSE_FILTER)) {
  547. control->pause_filter_count = 3000;
  548. control->intercept |= (1ULL << INTERCEPT_PAUSE);
  549. }
  550. enable_gif(svm);
  551. }
  552. static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
  553. {
  554. struct vcpu_svm *svm = to_svm(vcpu);
  555. init_vmcb(svm);
  556. if (!kvm_vcpu_is_bsp(vcpu)) {
  557. kvm_rip_write(vcpu, 0);
  558. svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
  559. svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
  560. }
  561. vcpu->arch.regs_avail = ~0;
  562. vcpu->arch.regs_dirty = ~0;
  563. return 0;
  564. }
  565. static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
  566. {
  567. struct vcpu_svm *svm;
  568. struct page *page;
  569. struct page *msrpm_pages;
  570. struct page *hsave_page;
  571. struct page *nested_msrpm_pages;
  572. int err;
  573. svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
  574. if (!svm) {
  575. err = -ENOMEM;
  576. goto out;
  577. }
  578. err = kvm_vcpu_init(&svm->vcpu, kvm, id);
  579. if (err)
  580. goto free_svm;
  581. err = -ENOMEM;
  582. page = alloc_page(GFP_KERNEL);
  583. if (!page)
  584. goto uninit;
  585. msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
  586. if (!msrpm_pages)
  587. goto free_page1;
  588. nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
  589. if (!nested_msrpm_pages)
  590. goto free_page2;
  591. hsave_page = alloc_page(GFP_KERNEL);
  592. if (!hsave_page)
  593. goto free_page3;
  594. svm->nested.hsave = page_address(hsave_page);
  595. svm->msrpm = page_address(msrpm_pages);
  596. svm_vcpu_init_msrpm(svm->msrpm);
  597. svm->nested.msrpm = page_address(nested_msrpm_pages);
  598. svm->vmcb = page_address(page);
  599. clear_page(svm->vmcb);
  600. svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
  601. svm->asid_generation = 0;
  602. init_vmcb(svm);
  603. fx_init(&svm->vcpu);
  604. svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
  605. if (kvm_vcpu_is_bsp(&svm->vcpu))
  606. svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
  607. return &svm->vcpu;
  608. free_page3:
  609. __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
  610. free_page2:
  611. __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
  612. free_page1:
  613. __free_page(page);
  614. uninit:
  615. kvm_vcpu_uninit(&svm->vcpu);
  616. free_svm:
  617. kmem_cache_free(kvm_vcpu_cache, svm);
  618. out:
  619. return ERR_PTR(err);
  620. }
  621. static void svm_free_vcpu(struct kvm_vcpu *vcpu)
  622. {
  623. struct vcpu_svm *svm = to_svm(vcpu);
  624. __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
  625. __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
  626. __free_page(virt_to_page(svm->nested.hsave));
  627. __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
  628. kvm_vcpu_uninit(vcpu);
  629. kmem_cache_free(kvm_vcpu_cache, svm);
  630. }
  631. static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  632. {
  633. struct vcpu_svm *svm = to_svm(vcpu);
  634. int i;
  635. if (unlikely(cpu != vcpu->cpu)) {
  636. u64 delta;
  637. if (check_tsc_unstable()) {
  638. /*
  639. * Make sure that the guest sees a monotonically
  640. * increasing TSC.
  641. */
  642. delta = vcpu->arch.host_tsc - native_read_tsc();
  643. svm->vmcb->control.tsc_offset += delta;
  644. if (is_nested(svm))
  645. svm->nested.hsave->control.tsc_offset += delta;
  646. }
  647. vcpu->cpu = cpu;
  648. kvm_migrate_timers(vcpu);
  649. svm->asid_generation = 0;
  650. }
  651. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  652. rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  653. }
  654. static void svm_vcpu_put(struct kvm_vcpu *vcpu)
  655. {
  656. struct vcpu_svm *svm = to_svm(vcpu);
  657. int i;
  658. ++vcpu->stat.host_state_reload;
  659. for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
  660. wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
  661. vcpu->arch.host_tsc = native_read_tsc();
  662. }
  663. static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
  664. {
  665. return to_svm(vcpu)->vmcb->save.rflags;
  666. }
  667. static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
  668. {
  669. to_svm(vcpu)->vmcb->save.rflags = rflags;
  670. }
  671. static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
  672. {
  673. switch (reg) {
  674. case VCPU_EXREG_PDPTR:
  675. BUG_ON(!npt_enabled);
  676. load_pdptrs(vcpu, vcpu->arch.cr3);
  677. break;
  678. default:
  679. BUG();
  680. }
  681. }
  682. static void svm_set_vintr(struct vcpu_svm *svm)
  683. {
  684. svm->vmcb->control.intercept |= 1ULL << INTERCEPT_VINTR;
  685. }
  686. static void svm_clear_vintr(struct vcpu_svm *svm)
  687. {
  688. svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
  689. }
  690. static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
  691. {
  692. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  693. switch (seg) {
  694. case VCPU_SREG_CS: return &save->cs;
  695. case VCPU_SREG_DS: return &save->ds;
  696. case VCPU_SREG_ES: return &save->es;
  697. case VCPU_SREG_FS: return &save->fs;
  698. case VCPU_SREG_GS: return &save->gs;
  699. case VCPU_SREG_SS: return &save->ss;
  700. case VCPU_SREG_TR: return &save->tr;
  701. case VCPU_SREG_LDTR: return &save->ldtr;
  702. }
  703. BUG();
  704. return NULL;
  705. }
  706. static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
  707. {
  708. struct vmcb_seg *s = svm_seg(vcpu, seg);
  709. return s->base;
  710. }
  711. static void svm_get_segment(struct kvm_vcpu *vcpu,
  712. struct kvm_segment *var, int seg)
  713. {
  714. struct vmcb_seg *s = svm_seg(vcpu, seg);
  715. var->base = s->base;
  716. var->limit = s->limit;
  717. var->selector = s->selector;
  718. var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
  719. var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
  720. var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
  721. var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
  722. var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
  723. var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
  724. var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
  725. var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
  726. /* AMD's VMCB does not have an explicit unusable field, so emulate it
  727. * for cross vendor migration purposes by "not present"
  728. */
  729. var->unusable = !var->present || (var->type == 0);
  730. switch (seg) {
  731. case VCPU_SREG_CS:
  732. /*
  733. * SVM always stores 0 for the 'G' bit in the CS selector in
  734. * the VMCB on a VMEXIT. This hurts cross-vendor migration:
  735. * Intel's VMENTRY has a check on the 'G' bit.
  736. */
  737. var->g = s->limit > 0xfffff;
  738. break;
  739. case VCPU_SREG_TR:
  740. /*
  741. * Work around a bug where the busy flag in the tr selector
  742. * isn't exposed
  743. */
  744. var->type |= 0x2;
  745. break;
  746. case VCPU_SREG_DS:
  747. case VCPU_SREG_ES:
  748. case VCPU_SREG_FS:
  749. case VCPU_SREG_GS:
  750. /*
  751. * The accessed bit must always be set in the segment
  752. * descriptor cache, although it can be cleared in the
  753. * descriptor, the cached bit always remains at 1. Since
  754. * Intel has a check on this, set it here to support
  755. * cross-vendor migration.
  756. */
  757. if (!var->unusable)
  758. var->type |= 0x1;
  759. break;
  760. case VCPU_SREG_SS:
  761. /* On AMD CPUs sometimes the DB bit in the segment
  762. * descriptor is left as 1, although the whole segment has
  763. * been made unusable. Clear it here to pass an Intel VMX
  764. * entry check when cross vendor migrating.
  765. */
  766. if (var->unusable)
  767. var->db = 0;
  768. break;
  769. }
  770. }
  771. static int svm_get_cpl(struct kvm_vcpu *vcpu)
  772. {
  773. struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
  774. return save->cpl;
  775. }
  776. static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  777. {
  778. struct vcpu_svm *svm = to_svm(vcpu);
  779. dt->size = svm->vmcb->save.idtr.limit;
  780. dt->address = svm->vmcb->save.idtr.base;
  781. }
  782. static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  783. {
  784. struct vcpu_svm *svm = to_svm(vcpu);
  785. svm->vmcb->save.idtr.limit = dt->size;
  786. svm->vmcb->save.idtr.base = dt->address ;
  787. }
  788. static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  789. {
  790. struct vcpu_svm *svm = to_svm(vcpu);
  791. dt->size = svm->vmcb->save.gdtr.limit;
  792. dt->address = svm->vmcb->save.gdtr.base;
  793. }
  794. static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
  795. {
  796. struct vcpu_svm *svm = to_svm(vcpu);
  797. svm->vmcb->save.gdtr.limit = dt->size;
  798. svm->vmcb->save.gdtr.base = dt->address ;
  799. }
  800. static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
  801. {
  802. }
  803. static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
  804. {
  805. }
  806. static void update_cr0_intercept(struct vcpu_svm *svm)
  807. {
  808. struct vmcb *vmcb = svm->vmcb;
  809. ulong gcr0 = svm->vcpu.arch.cr0;
  810. u64 *hcr0 = &svm->vmcb->save.cr0;
  811. if (!svm->vcpu.fpu_active)
  812. *hcr0 |= SVM_CR0_SELECTIVE_MASK;
  813. else
  814. *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
  815. | (gcr0 & SVM_CR0_SELECTIVE_MASK);
  816. if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
  817. vmcb->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
  818. vmcb->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
  819. if (is_nested(svm)) {
  820. struct vmcb *hsave = svm->nested.hsave;
  821. hsave->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
  822. hsave->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
  823. vmcb->control.intercept_cr_read |= svm->nested.intercept_cr_read;
  824. vmcb->control.intercept_cr_write |= svm->nested.intercept_cr_write;
  825. }
  826. } else {
  827. svm->vmcb->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
  828. svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
  829. if (is_nested(svm)) {
  830. struct vmcb *hsave = svm->nested.hsave;
  831. hsave->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
  832. hsave->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
  833. }
  834. }
  835. }
  836. static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  837. {
  838. struct vcpu_svm *svm = to_svm(vcpu);
  839. #ifdef CONFIG_X86_64
  840. if (vcpu->arch.efer & EFER_LME) {
  841. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  842. vcpu->arch.efer |= EFER_LMA;
  843. svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
  844. }
  845. if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
  846. vcpu->arch.efer &= ~EFER_LMA;
  847. svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
  848. }
  849. }
  850. #endif
  851. vcpu->arch.cr0 = cr0;
  852. if (!npt_enabled)
  853. cr0 |= X86_CR0_PG | X86_CR0_WP;
  854. if (!vcpu->fpu_active)
  855. cr0 |= X86_CR0_TS;
  856. /*
  857. * re-enable caching here because the QEMU bios
  858. * does not do it - this results in some delay at
  859. * reboot
  860. */
  861. cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
  862. svm->vmcb->save.cr0 = cr0;
  863. update_cr0_intercept(svm);
  864. }
  865. static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  866. {
  867. unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
  868. unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
  869. if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
  870. force_new_asid(vcpu);
  871. vcpu->arch.cr4 = cr4;
  872. if (!npt_enabled)
  873. cr4 |= X86_CR4_PAE;
  874. cr4 |= host_cr4_mce;
  875. to_svm(vcpu)->vmcb->save.cr4 = cr4;
  876. }
  877. static void svm_set_segment(struct kvm_vcpu *vcpu,
  878. struct kvm_segment *var, int seg)
  879. {
  880. struct vcpu_svm *svm = to_svm(vcpu);
  881. struct vmcb_seg *s = svm_seg(vcpu, seg);
  882. s->base = var->base;
  883. s->limit = var->limit;
  884. s->selector = var->selector;
  885. if (var->unusable)
  886. s->attrib = 0;
  887. else {
  888. s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
  889. s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
  890. s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
  891. s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
  892. s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
  893. s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
  894. s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
  895. s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
  896. }
  897. if (seg == VCPU_SREG_CS)
  898. svm->vmcb->save.cpl
  899. = (svm->vmcb->save.cs.attrib
  900. >> SVM_SELECTOR_DPL_SHIFT) & 3;
  901. }
  902. static void update_db_intercept(struct kvm_vcpu *vcpu)
  903. {
  904. struct vcpu_svm *svm = to_svm(vcpu);
  905. svm->vmcb->control.intercept_exceptions &=
  906. ~((1 << DB_VECTOR) | (1 << BP_VECTOR));
  907. if (svm->nmi_singlestep)
  908. svm->vmcb->control.intercept_exceptions |= (1 << DB_VECTOR);
  909. if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
  910. if (vcpu->guest_debug &
  911. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
  912. svm->vmcb->control.intercept_exceptions |=
  913. 1 << DB_VECTOR;
  914. if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
  915. svm->vmcb->control.intercept_exceptions |=
  916. 1 << BP_VECTOR;
  917. } else
  918. vcpu->guest_debug = 0;
  919. }
  920. static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
  921. {
  922. struct vcpu_svm *svm = to_svm(vcpu);
  923. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
  924. svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
  925. else
  926. svm->vmcb->save.dr7 = vcpu->arch.dr7;
  927. update_db_intercept(vcpu);
  928. }
  929. static void load_host_msrs(struct kvm_vcpu *vcpu)
  930. {
  931. #ifdef CONFIG_X86_64
  932. wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
  933. #endif
  934. }
  935. static void save_host_msrs(struct kvm_vcpu *vcpu)
  936. {
  937. #ifdef CONFIG_X86_64
  938. rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
  939. #endif
  940. }
  941. static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
  942. {
  943. if (sd->next_asid > sd->max_asid) {
  944. ++sd->asid_generation;
  945. sd->next_asid = 1;
  946. svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
  947. }
  948. svm->asid_generation = sd->asid_generation;
  949. svm->vmcb->control.asid = sd->next_asid++;
  950. }
  951. static int svm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *dest)
  952. {
  953. struct vcpu_svm *svm = to_svm(vcpu);
  954. switch (dr) {
  955. case 0 ... 3:
  956. *dest = vcpu->arch.db[dr];
  957. break;
  958. case 4:
  959. if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
  960. return EMULATE_FAIL; /* will re-inject UD */
  961. /* fall through */
  962. case 6:
  963. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
  964. *dest = vcpu->arch.dr6;
  965. else
  966. *dest = svm->vmcb->save.dr6;
  967. break;
  968. case 5:
  969. if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
  970. return EMULATE_FAIL; /* will re-inject UD */
  971. /* fall through */
  972. case 7:
  973. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
  974. *dest = vcpu->arch.dr7;
  975. else
  976. *dest = svm->vmcb->save.dr7;
  977. break;
  978. }
  979. return EMULATE_DONE;
  980. }
  981. static int svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value)
  982. {
  983. struct vcpu_svm *svm = to_svm(vcpu);
  984. switch (dr) {
  985. case 0 ... 3:
  986. vcpu->arch.db[dr] = value;
  987. if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
  988. vcpu->arch.eff_db[dr] = value;
  989. break;
  990. case 4:
  991. if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
  992. return EMULATE_FAIL; /* will re-inject UD */
  993. /* fall through */
  994. case 6:
  995. vcpu->arch.dr6 = (value & DR6_VOLATILE) | DR6_FIXED_1;
  996. break;
  997. case 5:
  998. if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
  999. return EMULATE_FAIL; /* will re-inject UD */
  1000. /* fall through */
  1001. case 7:
  1002. vcpu->arch.dr7 = (value & DR7_VOLATILE) | DR7_FIXED_1;
  1003. if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
  1004. svm->vmcb->save.dr7 = vcpu->arch.dr7;
  1005. vcpu->arch.switch_db_regs = (value & DR7_BP_EN_MASK);
  1006. }
  1007. break;
  1008. }
  1009. return EMULATE_DONE;
  1010. }
  1011. static int pf_interception(struct vcpu_svm *svm)
  1012. {
  1013. u64 fault_address;
  1014. u32 error_code;
  1015. fault_address = svm->vmcb->control.exit_info_2;
  1016. error_code = svm->vmcb->control.exit_info_1;
  1017. trace_kvm_page_fault(fault_address, error_code);
  1018. if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
  1019. kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
  1020. return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
  1021. }
  1022. static int db_interception(struct vcpu_svm *svm)
  1023. {
  1024. struct kvm_run *kvm_run = svm->vcpu.run;
  1025. if (!(svm->vcpu.guest_debug &
  1026. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
  1027. !svm->nmi_singlestep) {
  1028. kvm_queue_exception(&svm->vcpu, DB_VECTOR);
  1029. return 1;
  1030. }
  1031. if (svm->nmi_singlestep) {
  1032. svm->nmi_singlestep = false;
  1033. if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
  1034. svm->vmcb->save.rflags &=
  1035. ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
  1036. update_db_intercept(&svm->vcpu);
  1037. }
  1038. if (svm->vcpu.guest_debug &
  1039. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)){
  1040. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  1041. kvm_run->debug.arch.pc =
  1042. svm->vmcb->save.cs.base + svm->vmcb->save.rip;
  1043. kvm_run->debug.arch.exception = DB_VECTOR;
  1044. return 0;
  1045. }
  1046. return 1;
  1047. }
  1048. static int bp_interception(struct vcpu_svm *svm)
  1049. {
  1050. struct kvm_run *kvm_run = svm->vcpu.run;
  1051. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  1052. kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
  1053. kvm_run->debug.arch.exception = BP_VECTOR;
  1054. return 0;
  1055. }
  1056. static int ud_interception(struct vcpu_svm *svm)
  1057. {
  1058. int er;
  1059. er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
  1060. if (er != EMULATE_DONE)
  1061. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  1062. return 1;
  1063. }
  1064. static void svm_fpu_activate(struct kvm_vcpu *vcpu)
  1065. {
  1066. struct vcpu_svm *svm = to_svm(vcpu);
  1067. u32 excp;
  1068. if (is_nested(svm)) {
  1069. u32 h_excp, n_excp;
  1070. h_excp = svm->nested.hsave->control.intercept_exceptions;
  1071. n_excp = svm->nested.intercept_exceptions;
  1072. h_excp &= ~(1 << NM_VECTOR);
  1073. excp = h_excp | n_excp;
  1074. } else {
  1075. excp = svm->vmcb->control.intercept_exceptions;
  1076. excp &= ~(1 << NM_VECTOR);
  1077. }
  1078. svm->vmcb->control.intercept_exceptions = excp;
  1079. svm->vcpu.fpu_active = 1;
  1080. update_cr0_intercept(svm);
  1081. }
  1082. static int nm_interception(struct vcpu_svm *svm)
  1083. {
  1084. svm_fpu_activate(&svm->vcpu);
  1085. return 1;
  1086. }
  1087. static int mc_interception(struct vcpu_svm *svm)
  1088. {
  1089. /*
  1090. * On an #MC intercept the MCE handler is not called automatically in
  1091. * the host. So do it by hand here.
  1092. */
  1093. asm volatile (
  1094. "int $0x12\n");
  1095. /* not sure if we ever come back to this point */
  1096. return 1;
  1097. }
  1098. static int shutdown_interception(struct vcpu_svm *svm)
  1099. {
  1100. struct kvm_run *kvm_run = svm->vcpu.run;
  1101. /*
  1102. * VMCB is undefined after a SHUTDOWN intercept
  1103. * so reinitialize it.
  1104. */
  1105. clear_page(svm->vmcb);
  1106. init_vmcb(svm);
  1107. kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
  1108. return 0;
  1109. }
  1110. static int io_interception(struct vcpu_svm *svm)
  1111. {
  1112. u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
  1113. int size, in, string;
  1114. unsigned port;
  1115. ++svm->vcpu.stat.io_exits;
  1116. svm->next_rip = svm->vmcb->control.exit_info_2;
  1117. string = (io_info & SVM_IOIO_STR_MASK) != 0;
  1118. if (string) {
  1119. if (emulate_instruction(&svm->vcpu,
  1120. 0, 0, 0) == EMULATE_DO_MMIO)
  1121. return 0;
  1122. return 1;
  1123. }
  1124. in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
  1125. port = io_info >> 16;
  1126. size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
  1127. skip_emulated_instruction(&svm->vcpu);
  1128. return kvm_emulate_pio(&svm->vcpu, in, size, port);
  1129. }
  1130. static int nmi_interception(struct vcpu_svm *svm)
  1131. {
  1132. return 1;
  1133. }
  1134. static int intr_interception(struct vcpu_svm *svm)
  1135. {
  1136. ++svm->vcpu.stat.irq_exits;
  1137. return 1;
  1138. }
  1139. static int nop_on_interception(struct vcpu_svm *svm)
  1140. {
  1141. return 1;
  1142. }
  1143. static int halt_interception(struct vcpu_svm *svm)
  1144. {
  1145. svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
  1146. skip_emulated_instruction(&svm->vcpu);
  1147. return kvm_emulate_halt(&svm->vcpu);
  1148. }
  1149. static int vmmcall_interception(struct vcpu_svm *svm)
  1150. {
  1151. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1152. skip_emulated_instruction(&svm->vcpu);
  1153. kvm_emulate_hypercall(&svm->vcpu);
  1154. return 1;
  1155. }
  1156. static int nested_svm_check_permissions(struct vcpu_svm *svm)
  1157. {
  1158. if (!(svm->vcpu.arch.efer & EFER_SVME)
  1159. || !is_paging(&svm->vcpu)) {
  1160. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  1161. return 1;
  1162. }
  1163. if (svm->vmcb->save.cpl) {
  1164. kvm_inject_gp(&svm->vcpu, 0);
  1165. return 1;
  1166. }
  1167. return 0;
  1168. }
  1169. static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
  1170. bool has_error_code, u32 error_code)
  1171. {
  1172. int vmexit;
  1173. if (!is_nested(svm))
  1174. return 0;
  1175. svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
  1176. svm->vmcb->control.exit_code_hi = 0;
  1177. svm->vmcb->control.exit_info_1 = error_code;
  1178. svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
  1179. vmexit = nested_svm_intercept(svm);
  1180. if (vmexit == NESTED_EXIT_DONE)
  1181. svm->nested.exit_required = true;
  1182. return vmexit;
  1183. }
  1184. /* This function returns true if it is save to enable the irq window */
  1185. static inline bool nested_svm_intr(struct vcpu_svm *svm)
  1186. {
  1187. if (!is_nested(svm))
  1188. return true;
  1189. if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
  1190. return true;
  1191. if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
  1192. return false;
  1193. svm->vmcb->control.exit_code = SVM_EXIT_INTR;
  1194. if (svm->nested.intercept & 1ULL) {
  1195. /*
  1196. * The #vmexit can't be emulated here directly because this
  1197. * code path runs with irqs and preemtion disabled. A
  1198. * #vmexit emulation might sleep. Only signal request for
  1199. * the #vmexit here.
  1200. */
  1201. svm->nested.exit_required = true;
  1202. trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
  1203. return false;
  1204. }
  1205. return true;
  1206. }
  1207. static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
  1208. {
  1209. struct page *page;
  1210. might_sleep();
  1211. page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
  1212. if (is_error_page(page))
  1213. goto error;
  1214. *_page = page;
  1215. return kmap(page);
  1216. error:
  1217. kvm_release_page_clean(page);
  1218. kvm_inject_gp(&svm->vcpu, 0);
  1219. return NULL;
  1220. }
  1221. static void nested_svm_unmap(struct page *page)
  1222. {
  1223. kunmap(page);
  1224. kvm_release_page_dirty(page);
  1225. }
  1226. static bool nested_svm_exit_handled_msr(struct vcpu_svm *svm)
  1227. {
  1228. u32 param = svm->vmcb->control.exit_info_1 & 1;
  1229. u32 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  1230. bool ret = false;
  1231. u32 t0, t1;
  1232. u8 val;
  1233. if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
  1234. return false;
  1235. switch (msr) {
  1236. case 0 ... 0x1fff:
  1237. t0 = (msr * 2) % 8;
  1238. t1 = msr / 8;
  1239. break;
  1240. case 0xc0000000 ... 0xc0001fff:
  1241. t0 = (8192 + msr - 0xc0000000) * 2;
  1242. t1 = (t0 / 8);
  1243. t0 %= 8;
  1244. break;
  1245. case 0xc0010000 ... 0xc0011fff:
  1246. t0 = (16384 + msr - 0xc0010000) * 2;
  1247. t1 = (t0 / 8);
  1248. t0 %= 8;
  1249. break;
  1250. default:
  1251. ret = true;
  1252. goto out;
  1253. }
  1254. if (!kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + t1, &val, 1))
  1255. ret = val & ((1 << param) << t0);
  1256. out:
  1257. return ret;
  1258. }
  1259. static int nested_svm_exit_special(struct vcpu_svm *svm)
  1260. {
  1261. u32 exit_code = svm->vmcb->control.exit_code;
  1262. switch (exit_code) {
  1263. case SVM_EXIT_INTR:
  1264. case SVM_EXIT_NMI:
  1265. return NESTED_EXIT_HOST;
  1266. /* For now we are always handling NPFs when using them */
  1267. case SVM_EXIT_NPF:
  1268. if (npt_enabled)
  1269. return NESTED_EXIT_HOST;
  1270. break;
  1271. /* When we're shadowing, trap PFs */
  1272. case SVM_EXIT_EXCP_BASE + PF_VECTOR:
  1273. if (!npt_enabled)
  1274. return NESTED_EXIT_HOST;
  1275. break;
  1276. case SVM_EXIT_EXCP_BASE + NM_VECTOR:
  1277. nm_interception(svm);
  1278. break;
  1279. default:
  1280. break;
  1281. }
  1282. return NESTED_EXIT_CONTINUE;
  1283. }
  1284. /*
  1285. * If this function returns true, this #vmexit was already handled
  1286. */
  1287. static int nested_svm_intercept(struct vcpu_svm *svm)
  1288. {
  1289. u32 exit_code = svm->vmcb->control.exit_code;
  1290. int vmexit = NESTED_EXIT_HOST;
  1291. switch (exit_code) {
  1292. case SVM_EXIT_MSR:
  1293. vmexit = nested_svm_exit_handled_msr(svm);
  1294. break;
  1295. case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR8: {
  1296. u32 cr_bits = 1 << (exit_code - SVM_EXIT_READ_CR0);
  1297. if (svm->nested.intercept_cr_read & cr_bits)
  1298. vmexit = NESTED_EXIT_DONE;
  1299. break;
  1300. }
  1301. case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR8: {
  1302. u32 cr_bits = 1 << (exit_code - SVM_EXIT_WRITE_CR0);
  1303. if (svm->nested.intercept_cr_write & cr_bits)
  1304. vmexit = NESTED_EXIT_DONE;
  1305. break;
  1306. }
  1307. case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR7: {
  1308. u32 dr_bits = 1 << (exit_code - SVM_EXIT_READ_DR0);
  1309. if (svm->nested.intercept_dr_read & dr_bits)
  1310. vmexit = NESTED_EXIT_DONE;
  1311. break;
  1312. }
  1313. case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR7: {
  1314. u32 dr_bits = 1 << (exit_code - SVM_EXIT_WRITE_DR0);
  1315. if (svm->nested.intercept_dr_write & dr_bits)
  1316. vmexit = NESTED_EXIT_DONE;
  1317. break;
  1318. }
  1319. case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
  1320. u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
  1321. if (svm->nested.intercept_exceptions & excp_bits)
  1322. vmexit = NESTED_EXIT_DONE;
  1323. break;
  1324. }
  1325. default: {
  1326. u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
  1327. if (svm->nested.intercept & exit_bits)
  1328. vmexit = NESTED_EXIT_DONE;
  1329. }
  1330. }
  1331. return vmexit;
  1332. }
  1333. static int nested_svm_exit_handled(struct vcpu_svm *svm)
  1334. {
  1335. int vmexit;
  1336. vmexit = nested_svm_intercept(svm);
  1337. if (vmexit == NESTED_EXIT_DONE)
  1338. nested_svm_vmexit(svm);
  1339. return vmexit;
  1340. }
  1341. static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
  1342. {
  1343. struct vmcb_control_area *dst = &dst_vmcb->control;
  1344. struct vmcb_control_area *from = &from_vmcb->control;
  1345. dst->intercept_cr_read = from->intercept_cr_read;
  1346. dst->intercept_cr_write = from->intercept_cr_write;
  1347. dst->intercept_dr_read = from->intercept_dr_read;
  1348. dst->intercept_dr_write = from->intercept_dr_write;
  1349. dst->intercept_exceptions = from->intercept_exceptions;
  1350. dst->intercept = from->intercept;
  1351. dst->iopm_base_pa = from->iopm_base_pa;
  1352. dst->msrpm_base_pa = from->msrpm_base_pa;
  1353. dst->tsc_offset = from->tsc_offset;
  1354. dst->asid = from->asid;
  1355. dst->tlb_ctl = from->tlb_ctl;
  1356. dst->int_ctl = from->int_ctl;
  1357. dst->int_vector = from->int_vector;
  1358. dst->int_state = from->int_state;
  1359. dst->exit_code = from->exit_code;
  1360. dst->exit_code_hi = from->exit_code_hi;
  1361. dst->exit_info_1 = from->exit_info_1;
  1362. dst->exit_info_2 = from->exit_info_2;
  1363. dst->exit_int_info = from->exit_int_info;
  1364. dst->exit_int_info_err = from->exit_int_info_err;
  1365. dst->nested_ctl = from->nested_ctl;
  1366. dst->event_inj = from->event_inj;
  1367. dst->event_inj_err = from->event_inj_err;
  1368. dst->nested_cr3 = from->nested_cr3;
  1369. dst->lbr_ctl = from->lbr_ctl;
  1370. }
  1371. static int nested_svm_vmexit(struct vcpu_svm *svm)
  1372. {
  1373. struct vmcb *nested_vmcb;
  1374. struct vmcb *hsave = svm->nested.hsave;
  1375. struct vmcb *vmcb = svm->vmcb;
  1376. struct page *page;
  1377. trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
  1378. vmcb->control.exit_info_1,
  1379. vmcb->control.exit_info_2,
  1380. vmcb->control.exit_int_info,
  1381. vmcb->control.exit_int_info_err);
  1382. nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
  1383. if (!nested_vmcb)
  1384. return 1;
  1385. /* Exit nested SVM mode */
  1386. svm->nested.vmcb = 0;
  1387. /* Give the current vmcb to the guest */
  1388. disable_gif(svm);
  1389. nested_vmcb->save.es = vmcb->save.es;
  1390. nested_vmcb->save.cs = vmcb->save.cs;
  1391. nested_vmcb->save.ss = vmcb->save.ss;
  1392. nested_vmcb->save.ds = vmcb->save.ds;
  1393. nested_vmcb->save.gdtr = vmcb->save.gdtr;
  1394. nested_vmcb->save.idtr = vmcb->save.idtr;
  1395. nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
  1396. if (npt_enabled)
  1397. nested_vmcb->save.cr3 = vmcb->save.cr3;
  1398. else
  1399. nested_vmcb->save.cr3 = svm->vcpu.arch.cr3;
  1400. nested_vmcb->save.cr2 = vmcb->save.cr2;
  1401. nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
  1402. nested_vmcb->save.rflags = vmcb->save.rflags;
  1403. nested_vmcb->save.rip = vmcb->save.rip;
  1404. nested_vmcb->save.rsp = vmcb->save.rsp;
  1405. nested_vmcb->save.rax = vmcb->save.rax;
  1406. nested_vmcb->save.dr7 = vmcb->save.dr7;
  1407. nested_vmcb->save.dr6 = vmcb->save.dr6;
  1408. nested_vmcb->save.cpl = vmcb->save.cpl;
  1409. nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
  1410. nested_vmcb->control.int_vector = vmcb->control.int_vector;
  1411. nested_vmcb->control.int_state = vmcb->control.int_state;
  1412. nested_vmcb->control.exit_code = vmcb->control.exit_code;
  1413. nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
  1414. nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
  1415. nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
  1416. nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
  1417. nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
  1418. /*
  1419. * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
  1420. * to make sure that we do not lose injected events. So check event_inj
  1421. * here and copy it to exit_int_info if it is valid.
  1422. * Exit_int_info and event_inj can't be both valid because the case
  1423. * below only happens on a VMRUN instruction intercept which has
  1424. * no valid exit_int_info set.
  1425. */
  1426. if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
  1427. struct vmcb_control_area *nc = &nested_vmcb->control;
  1428. nc->exit_int_info = vmcb->control.event_inj;
  1429. nc->exit_int_info_err = vmcb->control.event_inj_err;
  1430. }
  1431. nested_vmcb->control.tlb_ctl = 0;
  1432. nested_vmcb->control.event_inj = 0;
  1433. nested_vmcb->control.event_inj_err = 0;
  1434. /* We always set V_INTR_MASKING and remember the old value in hflags */
  1435. if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
  1436. nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
  1437. /* Restore the original control entries */
  1438. copy_vmcb_control_area(vmcb, hsave);
  1439. kvm_clear_exception_queue(&svm->vcpu);
  1440. kvm_clear_interrupt_queue(&svm->vcpu);
  1441. /* Restore selected save entries */
  1442. svm->vmcb->save.es = hsave->save.es;
  1443. svm->vmcb->save.cs = hsave->save.cs;
  1444. svm->vmcb->save.ss = hsave->save.ss;
  1445. svm->vmcb->save.ds = hsave->save.ds;
  1446. svm->vmcb->save.gdtr = hsave->save.gdtr;
  1447. svm->vmcb->save.idtr = hsave->save.idtr;
  1448. svm->vmcb->save.rflags = hsave->save.rflags;
  1449. svm_set_efer(&svm->vcpu, hsave->save.efer);
  1450. svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
  1451. svm_set_cr4(&svm->vcpu, hsave->save.cr4);
  1452. if (npt_enabled) {
  1453. svm->vmcb->save.cr3 = hsave->save.cr3;
  1454. svm->vcpu.arch.cr3 = hsave->save.cr3;
  1455. } else {
  1456. kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
  1457. }
  1458. kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
  1459. kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
  1460. kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
  1461. svm->vmcb->save.dr7 = 0;
  1462. svm->vmcb->save.cpl = 0;
  1463. svm->vmcb->control.exit_int_info = 0;
  1464. nested_svm_unmap(page);
  1465. kvm_mmu_reset_context(&svm->vcpu);
  1466. kvm_mmu_load(&svm->vcpu);
  1467. return 0;
  1468. }
  1469. static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
  1470. {
  1471. u32 *nested_msrpm;
  1472. struct page *page;
  1473. int i;
  1474. nested_msrpm = nested_svm_map(svm, svm->nested.vmcb_msrpm, &page);
  1475. if (!nested_msrpm)
  1476. return false;
  1477. for (i=0; i< PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER) / 4; i++)
  1478. svm->nested.msrpm[i] = svm->msrpm[i] | nested_msrpm[i];
  1479. svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
  1480. nested_svm_unmap(page);
  1481. return true;
  1482. }
  1483. static bool nested_svm_vmrun(struct vcpu_svm *svm)
  1484. {
  1485. struct vmcb *nested_vmcb;
  1486. struct vmcb *hsave = svm->nested.hsave;
  1487. struct vmcb *vmcb = svm->vmcb;
  1488. struct page *page;
  1489. u64 vmcb_gpa;
  1490. vmcb_gpa = svm->vmcb->save.rax;
  1491. nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
  1492. if (!nested_vmcb)
  1493. return false;
  1494. trace_kvm_nested_vmrun(svm->vmcb->save.rip - 3, svm->nested.vmcb,
  1495. nested_vmcb->save.rip,
  1496. nested_vmcb->control.int_ctl,
  1497. nested_vmcb->control.event_inj,
  1498. nested_vmcb->control.nested_ctl);
  1499. /* Clear internal status */
  1500. kvm_clear_exception_queue(&svm->vcpu);
  1501. kvm_clear_interrupt_queue(&svm->vcpu);
  1502. /* Save the old vmcb, so we don't need to pick what we save, but
  1503. can restore everything when a VMEXIT occurs */
  1504. hsave->save.es = vmcb->save.es;
  1505. hsave->save.cs = vmcb->save.cs;
  1506. hsave->save.ss = vmcb->save.ss;
  1507. hsave->save.ds = vmcb->save.ds;
  1508. hsave->save.gdtr = vmcb->save.gdtr;
  1509. hsave->save.idtr = vmcb->save.idtr;
  1510. hsave->save.efer = svm->vcpu.arch.efer;
  1511. hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
  1512. hsave->save.cr4 = svm->vcpu.arch.cr4;
  1513. hsave->save.rflags = vmcb->save.rflags;
  1514. hsave->save.rip = svm->next_rip;
  1515. hsave->save.rsp = vmcb->save.rsp;
  1516. hsave->save.rax = vmcb->save.rax;
  1517. if (npt_enabled)
  1518. hsave->save.cr3 = vmcb->save.cr3;
  1519. else
  1520. hsave->save.cr3 = svm->vcpu.arch.cr3;
  1521. copy_vmcb_control_area(hsave, vmcb);
  1522. if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
  1523. svm->vcpu.arch.hflags |= HF_HIF_MASK;
  1524. else
  1525. svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
  1526. /* Load the nested guest state */
  1527. svm->vmcb->save.es = nested_vmcb->save.es;
  1528. svm->vmcb->save.cs = nested_vmcb->save.cs;
  1529. svm->vmcb->save.ss = nested_vmcb->save.ss;
  1530. svm->vmcb->save.ds = nested_vmcb->save.ds;
  1531. svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
  1532. svm->vmcb->save.idtr = nested_vmcb->save.idtr;
  1533. svm->vmcb->save.rflags = nested_vmcb->save.rflags;
  1534. svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
  1535. svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
  1536. svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
  1537. if (npt_enabled) {
  1538. svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
  1539. svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
  1540. } else {
  1541. kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
  1542. kvm_mmu_reset_context(&svm->vcpu);
  1543. }
  1544. svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
  1545. kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
  1546. kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
  1547. kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
  1548. /* In case we don't even reach vcpu_run, the fields are not updated */
  1549. svm->vmcb->save.rax = nested_vmcb->save.rax;
  1550. svm->vmcb->save.rsp = nested_vmcb->save.rsp;
  1551. svm->vmcb->save.rip = nested_vmcb->save.rip;
  1552. svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
  1553. svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
  1554. svm->vmcb->save.cpl = nested_vmcb->save.cpl;
  1555. svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa;
  1556. /* cache intercepts */
  1557. svm->nested.intercept_cr_read = nested_vmcb->control.intercept_cr_read;
  1558. svm->nested.intercept_cr_write = nested_vmcb->control.intercept_cr_write;
  1559. svm->nested.intercept_dr_read = nested_vmcb->control.intercept_dr_read;
  1560. svm->nested.intercept_dr_write = nested_vmcb->control.intercept_dr_write;
  1561. svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
  1562. svm->nested.intercept = nested_vmcb->control.intercept;
  1563. force_new_asid(&svm->vcpu);
  1564. svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
  1565. if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
  1566. svm->vcpu.arch.hflags |= HF_VINTR_MASK;
  1567. else
  1568. svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
  1569. if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
  1570. /* We only want the cr8 intercept bits of the guest */
  1571. svm->vmcb->control.intercept_cr_read &= ~INTERCEPT_CR8_MASK;
  1572. svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
  1573. }
  1574. /* We don't want a nested guest to be more powerful than the guest,
  1575. so all intercepts are ORed */
  1576. svm->vmcb->control.intercept_cr_read |=
  1577. nested_vmcb->control.intercept_cr_read;
  1578. svm->vmcb->control.intercept_cr_write |=
  1579. nested_vmcb->control.intercept_cr_write;
  1580. svm->vmcb->control.intercept_dr_read |=
  1581. nested_vmcb->control.intercept_dr_read;
  1582. svm->vmcb->control.intercept_dr_write |=
  1583. nested_vmcb->control.intercept_dr_write;
  1584. svm->vmcb->control.intercept_exceptions |=
  1585. nested_vmcb->control.intercept_exceptions;
  1586. svm->vmcb->control.intercept |= nested_vmcb->control.intercept;
  1587. svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
  1588. svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
  1589. svm->vmcb->control.int_state = nested_vmcb->control.int_state;
  1590. svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
  1591. svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
  1592. svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
  1593. nested_svm_unmap(page);
  1594. /* nested_vmcb is our indicator if nested SVM is activated */
  1595. svm->nested.vmcb = vmcb_gpa;
  1596. enable_gif(svm);
  1597. return true;
  1598. }
  1599. static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
  1600. {
  1601. to_vmcb->save.fs = from_vmcb->save.fs;
  1602. to_vmcb->save.gs = from_vmcb->save.gs;
  1603. to_vmcb->save.tr = from_vmcb->save.tr;
  1604. to_vmcb->save.ldtr = from_vmcb->save.ldtr;
  1605. to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
  1606. to_vmcb->save.star = from_vmcb->save.star;
  1607. to_vmcb->save.lstar = from_vmcb->save.lstar;
  1608. to_vmcb->save.cstar = from_vmcb->save.cstar;
  1609. to_vmcb->save.sfmask = from_vmcb->save.sfmask;
  1610. to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
  1611. to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
  1612. to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
  1613. }
  1614. static int vmload_interception(struct vcpu_svm *svm)
  1615. {
  1616. struct vmcb *nested_vmcb;
  1617. struct page *page;
  1618. if (nested_svm_check_permissions(svm))
  1619. return 1;
  1620. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1621. skip_emulated_instruction(&svm->vcpu);
  1622. nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
  1623. if (!nested_vmcb)
  1624. return 1;
  1625. nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
  1626. nested_svm_unmap(page);
  1627. return 1;
  1628. }
  1629. static int vmsave_interception(struct vcpu_svm *svm)
  1630. {
  1631. struct vmcb *nested_vmcb;
  1632. struct page *page;
  1633. if (nested_svm_check_permissions(svm))
  1634. return 1;
  1635. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1636. skip_emulated_instruction(&svm->vcpu);
  1637. nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
  1638. if (!nested_vmcb)
  1639. return 1;
  1640. nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
  1641. nested_svm_unmap(page);
  1642. return 1;
  1643. }
  1644. static int vmrun_interception(struct vcpu_svm *svm)
  1645. {
  1646. if (nested_svm_check_permissions(svm))
  1647. return 1;
  1648. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1649. skip_emulated_instruction(&svm->vcpu);
  1650. if (!nested_svm_vmrun(svm))
  1651. return 1;
  1652. if (!nested_svm_vmrun_msrpm(svm))
  1653. goto failed;
  1654. return 1;
  1655. failed:
  1656. svm->vmcb->control.exit_code = SVM_EXIT_ERR;
  1657. svm->vmcb->control.exit_code_hi = 0;
  1658. svm->vmcb->control.exit_info_1 = 0;
  1659. svm->vmcb->control.exit_info_2 = 0;
  1660. nested_svm_vmexit(svm);
  1661. return 1;
  1662. }
  1663. static int stgi_interception(struct vcpu_svm *svm)
  1664. {
  1665. if (nested_svm_check_permissions(svm))
  1666. return 1;
  1667. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1668. skip_emulated_instruction(&svm->vcpu);
  1669. enable_gif(svm);
  1670. return 1;
  1671. }
  1672. static int clgi_interception(struct vcpu_svm *svm)
  1673. {
  1674. if (nested_svm_check_permissions(svm))
  1675. return 1;
  1676. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1677. skip_emulated_instruction(&svm->vcpu);
  1678. disable_gif(svm);
  1679. /* After a CLGI no interrupts should come */
  1680. svm_clear_vintr(svm);
  1681. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  1682. return 1;
  1683. }
  1684. static int invlpga_interception(struct vcpu_svm *svm)
  1685. {
  1686. struct kvm_vcpu *vcpu = &svm->vcpu;
  1687. trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
  1688. vcpu->arch.regs[VCPU_REGS_RAX]);
  1689. /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
  1690. kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
  1691. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1692. skip_emulated_instruction(&svm->vcpu);
  1693. return 1;
  1694. }
  1695. static int skinit_interception(struct vcpu_svm *svm)
  1696. {
  1697. trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
  1698. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  1699. return 1;
  1700. }
  1701. static int invalid_op_interception(struct vcpu_svm *svm)
  1702. {
  1703. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  1704. return 1;
  1705. }
  1706. static int task_switch_interception(struct vcpu_svm *svm)
  1707. {
  1708. u16 tss_selector;
  1709. int reason;
  1710. int int_type = svm->vmcb->control.exit_int_info &
  1711. SVM_EXITINTINFO_TYPE_MASK;
  1712. int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
  1713. uint32_t type =
  1714. svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
  1715. uint32_t idt_v =
  1716. svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
  1717. tss_selector = (u16)svm->vmcb->control.exit_info_1;
  1718. if (svm->vmcb->control.exit_info_2 &
  1719. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
  1720. reason = TASK_SWITCH_IRET;
  1721. else if (svm->vmcb->control.exit_info_2 &
  1722. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
  1723. reason = TASK_SWITCH_JMP;
  1724. else if (idt_v)
  1725. reason = TASK_SWITCH_GATE;
  1726. else
  1727. reason = TASK_SWITCH_CALL;
  1728. if (reason == TASK_SWITCH_GATE) {
  1729. switch (type) {
  1730. case SVM_EXITINTINFO_TYPE_NMI:
  1731. svm->vcpu.arch.nmi_injected = false;
  1732. break;
  1733. case SVM_EXITINTINFO_TYPE_EXEPT:
  1734. kvm_clear_exception_queue(&svm->vcpu);
  1735. break;
  1736. case SVM_EXITINTINFO_TYPE_INTR:
  1737. kvm_clear_interrupt_queue(&svm->vcpu);
  1738. break;
  1739. default:
  1740. break;
  1741. }
  1742. }
  1743. if (reason != TASK_SWITCH_GATE ||
  1744. int_type == SVM_EXITINTINFO_TYPE_SOFT ||
  1745. (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
  1746. (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
  1747. skip_emulated_instruction(&svm->vcpu);
  1748. return kvm_task_switch(&svm->vcpu, tss_selector, reason);
  1749. }
  1750. static int cpuid_interception(struct vcpu_svm *svm)
  1751. {
  1752. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  1753. kvm_emulate_cpuid(&svm->vcpu);
  1754. return 1;
  1755. }
  1756. static int iret_interception(struct vcpu_svm *svm)
  1757. {
  1758. ++svm->vcpu.stat.nmi_window_exits;
  1759. svm->vmcb->control.intercept &= ~(1UL << INTERCEPT_IRET);
  1760. svm->vcpu.arch.hflags |= HF_IRET_MASK;
  1761. return 1;
  1762. }
  1763. static int invlpg_interception(struct vcpu_svm *svm)
  1764. {
  1765. if (emulate_instruction(&svm->vcpu, 0, 0, 0) != EMULATE_DONE)
  1766. pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
  1767. return 1;
  1768. }
  1769. static int emulate_on_interception(struct vcpu_svm *svm)
  1770. {
  1771. if (emulate_instruction(&svm->vcpu, 0, 0, 0) != EMULATE_DONE)
  1772. pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
  1773. return 1;
  1774. }
  1775. static int cr8_write_interception(struct vcpu_svm *svm)
  1776. {
  1777. struct kvm_run *kvm_run = svm->vcpu.run;
  1778. u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
  1779. /* instruction emulation calls kvm_set_cr8() */
  1780. emulate_instruction(&svm->vcpu, 0, 0, 0);
  1781. if (irqchip_in_kernel(svm->vcpu.kvm)) {
  1782. svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
  1783. return 1;
  1784. }
  1785. if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
  1786. return 1;
  1787. kvm_run->exit_reason = KVM_EXIT_SET_TPR;
  1788. return 0;
  1789. }
  1790. static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
  1791. {
  1792. struct vcpu_svm *svm = to_svm(vcpu);
  1793. switch (ecx) {
  1794. case MSR_IA32_TSC: {
  1795. u64 tsc_offset;
  1796. if (is_nested(svm))
  1797. tsc_offset = svm->nested.hsave->control.tsc_offset;
  1798. else
  1799. tsc_offset = svm->vmcb->control.tsc_offset;
  1800. *data = tsc_offset + native_read_tsc();
  1801. break;
  1802. }
  1803. case MSR_K6_STAR:
  1804. *data = svm->vmcb->save.star;
  1805. break;
  1806. #ifdef CONFIG_X86_64
  1807. case MSR_LSTAR:
  1808. *data = svm->vmcb->save.lstar;
  1809. break;
  1810. case MSR_CSTAR:
  1811. *data = svm->vmcb->save.cstar;
  1812. break;
  1813. case MSR_KERNEL_GS_BASE:
  1814. *data = svm->vmcb->save.kernel_gs_base;
  1815. break;
  1816. case MSR_SYSCALL_MASK:
  1817. *data = svm->vmcb->save.sfmask;
  1818. break;
  1819. #endif
  1820. case MSR_IA32_SYSENTER_CS:
  1821. *data = svm->vmcb->save.sysenter_cs;
  1822. break;
  1823. case MSR_IA32_SYSENTER_EIP:
  1824. *data = svm->sysenter_eip;
  1825. break;
  1826. case MSR_IA32_SYSENTER_ESP:
  1827. *data = svm->sysenter_esp;
  1828. break;
  1829. /* Nobody will change the following 5 values in the VMCB so
  1830. we can safely return them on rdmsr. They will always be 0
  1831. until LBRV is implemented. */
  1832. case MSR_IA32_DEBUGCTLMSR:
  1833. *data = svm->vmcb->save.dbgctl;
  1834. break;
  1835. case MSR_IA32_LASTBRANCHFROMIP:
  1836. *data = svm->vmcb->save.br_from;
  1837. break;
  1838. case MSR_IA32_LASTBRANCHTOIP:
  1839. *data = svm->vmcb->save.br_to;
  1840. break;
  1841. case MSR_IA32_LASTINTFROMIP:
  1842. *data = svm->vmcb->save.last_excp_from;
  1843. break;
  1844. case MSR_IA32_LASTINTTOIP:
  1845. *data = svm->vmcb->save.last_excp_to;
  1846. break;
  1847. case MSR_VM_HSAVE_PA:
  1848. *data = svm->nested.hsave_msr;
  1849. break;
  1850. case MSR_VM_CR:
  1851. *data = 0;
  1852. break;
  1853. case MSR_IA32_UCODE_REV:
  1854. *data = 0x01000065;
  1855. break;
  1856. default:
  1857. return kvm_get_msr_common(vcpu, ecx, data);
  1858. }
  1859. return 0;
  1860. }
  1861. static int rdmsr_interception(struct vcpu_svm *svm)
  1862. {
  1863. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  1864. u64 data;
  1865. if (svm_get_msr(&svm->vcpu, ecx, &data)) {
  1866. trace_kvm_msr_read_ex(ecx);
  1867. kvm_inject_gp(&svm->vcpu, 0);
  1868. } else {
  1869. trace_kvm_msr_read(ecx, data);
  1870. svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
  1871. svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
  1872. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  1873. skip_emulated_instruction(&svm->vcpu);
  1874. }
  1875. return 1;
  1876. }
  1877. static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
  1878. {
  1879. struct vcpu_svm *svm = to_svm(vcpu);
  1880. switch (ecx) {
  1881. case MSR_IA32_TSC: {
  1882. u64 tsc_offset = data - native_read_tsc();
  1883. u64 g_tsc_offset = 0;
  1884. if (is_nested(svm)) {
  1885. g_tsc_offset = svm->vmcb->control.tsc_offset -
  1886. svm->nested.hsave->control.tsc_offset;
  1887. svm->nested.hsave->control.tsc_offset = tsc_offset;
  1888. }
  1889. svm->vmcb->control.tsc_offset = tsc_offset + g_tsc_offset;
  1890. break;
  1891. }
  1892. case MSR_K6_STAR:
  1893. svm->vmcb->save.star = data;
  1894. break;
  1895. #ifdef CONFIG_X86_64
  1896. case MSR_LSTAR:
  1897. svm->vmcb->save.lstar = data;
  1898. break;
  1899. case MSR_CSTAR:
  1900. svm->vmcb->save.cstar = data;
  1901. break;
  1902. case MSR_KERNEL_GS_BASE:
  1903. svm->vmcb->save.kernel_gs_base = data;
  1904. break;
  1905. case MSR_SYSCALL_MASK:
  1906. svm->vmcb->save.sfmask = data;
  1907. break;
  1908. #endif
  1909. case MSR_IA32_SYSENTER_CS:
  1910. svm->vmcb->save.sysenter_cs = data;
  1911. break;
  1912. case MSR_IA32_SYSENTER_EIP:
  1913. svm->sysenter_eip = data;
  1914. svm->vmcb->save.sysenter_eip = data;
  1915. break;
  1916. case MSR_IA32_SYSENTER_ESP:
  1917. svm->sysenter_esp = data;
  1918. svm->vmcb->save.sysenter_esp = data;
  1919. break;
  1920. case MSR_IA32_DEBUGCTLMSR:
  1921. if (!svm_has(SVM_FEATURE_LBRV)) {
  1922. pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
  1923. __func__, data);
  1924. break;
  1925. }
  1926. if (data & DEBUGCTL_RESERVED_BITS)
  1927. return 1;
  1928. svm->vmcb->save.dbgctl = data;
  1929. if (data & (1ULL<<0))
  1930. svm_enable_lbrv(svm);
  1931. else
  1932. svm_disable_lbrv(svm);
  1933. break;
  1934. case MSR_VM_HSAVE_PA:
  1935. svm->nested.hsave_msr = data;
  1936. break;
  1937. case MSR_VM_CR:
  1938. case MSR_VM_IGNNE:
  1939. pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
  1940. break;
  1941. default:
  1942. return kvm_set_msr_common(vcpu, ecx, data);
  1943. }
  1944. return 0;
  1945. }
  1946. static int wrmsr_interception(struct vcpu_svm *svm)
  1947. {
  1948. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  1949. u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
  1950. | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  1951. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  1952. if (svm_set_msr(&svm->vcpu, ecx, data)) {
  1953. trace_kvm_msr_write_ex(ecx, data);
  1954. kvm_inject_gp(&svm->vcpu, 0);
  1955. } else {
  1956. trace_kvm_msr_write(ecx, data);
  1957. skip_emulated_instruction(&svm->vcpu);
  1958. }
  1959. return 1;
  1960. }
  1961. static int msr_interception(struct vcpu_svm *svm)
  1962. {
  1963. if (svm->vmcb->control.exit_info_1)
  1964. return wrmsr_interception(svm);
  1965. else
  1966. return rdmsr_interception(svm);
  1967. }
  1968. static int interrupt_window_interception(struct vcpu_svm *svm)
  1969. {
  1970. struct kvm_run *kvm_run = svm->vcpu.run;
  1971. svm_clear_vintr(svm);
  1972. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  1973. /*
  1974. * If the user space waits to inject interrupts, exit as soon as
  1975. * possible
  1976. */
  1977. if (!irqchip_in_kernel(svm->vcpu.kvm) &&
  1978. kvm_run->request_interrupt_window &&
  1979. !kvm_cpu_has_interrupt(&svm->vcpu)) {
  1980. ++svm->vcpu.stat.irq_window_exits;
  1981. kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  1982. return 0;
  1983. }
  1984. return 1;
  1985. }
  1986. static int pause_interception(struct vcpu_svm *svm)
  1987. {
  1988. kvm_vcpu_on_spin(&(svm->vcpu));
  1989. return 1;
  1990. }
  1991. static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
  1992. [SVM_EXIT_READ_CR0] = emulate_on_interception,
  1993. [SVM_EXIT_READ_CR3] = emulate_on_interception,
  1994. [SVM_EXIT_READ_CR4] = emulate_on_interception,
  1995. [SVM_EXIT_READ_CR8] = emulate_on_interception,
  1996. [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
  1997. [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
  1998. [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
  1999. [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
  2000. [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
  2001. [SVM_EXIT_READ_DR0] = emulate_on_interception,
  2002. [SVM_EXIT_READ_DR1] = emulate_on_interception,
  2003. [SVM_EXIT_READ_DR2] = emulate_on_interception,
  2004. [SVM_EXIT_READ_DR3] = emulate_on_interception,
  2005. [SVM_EXIT_READ_DR4] = emulate_on_interception,
  2006. [SVM_EXIT_READ_DR5] = emulate_on_interception,
  2007. [SVM_EXIT_READ_DR6] = emulate_on_interception,
  2008. [SVM_EXIT_READ_DR7] = emulate_on_interception,
  2009. [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
  2010. [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
  2011. [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
  2012. [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
  2013. [SVM_EXIT_WRITE_DR4] = emulate_on_interception,
  2014. [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
  2015. [SVM_EXIT_WRITE_DR6] = emulate_on_interception,
  2016. [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
  2017. [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
  2018. [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
  2019. [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
  2020. [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
  2021. [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
  2022. [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
  2023. [SVM_EXIT_INTR] = intr_interception,
  2024. [SVM_EXIT_NMI] = nmi_interception,
  2025. [SVM_EXIT_SMI] = nop_on_interception,
  2026. [SVM_EXIT_INIT] = nop_on_interception,
  2027. [SVM_EXIT_VINTR] = interrupt_window_interception,
  2028. /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */
  2029. [SVM_EXIT_CPUID] = cpuid_interception,
  2030. [SVM_EXIT_IRET] = iret_interception,
  2031. [SVM_EXIT_INVD] = emulate_on_interception,
  2032. [SVM_EXIT_PAUSE] = pause_interception,
  2033. [SVM_EXIT_HLT] = halt_interception,
  2034. [SVM_EXIT_INVLPG] = invlpg_interception,
  2035. [SVM_EXIT_INVLPGA] = invlpga_interception,
  2036. [SVM_EXIT_IOIO] = io_interception,
  2037. [SVM_EXIT_MSR] = msr_interception,
  2038. [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
  2039. [SVM_EXIT_SHUTDOWN] = shutdown_interception,
  2040. [SVM_EXIT_VMRUN] = vmrun_interception,
  2041. [SVM_EXIT_VMMCALL] = vmmcall_interception,
  2042. [SVM_EXIT_VMLOAD] = vmload_interception,
  2043. [SVM_EXIT_VMSAVE] = vmsave_interception,
  2044. [SVM_EXIT_STGI] = stgi_interception,
  2045. [SVM_EXIT_CLGI] = clgi_interception,
  2046. [SVM_EXIT_SKINIT] = skinit_interception,
  2047. [SVM_EXIT_WBINVD] = emulate_on_interception,
  2048. [SVM_EXIT_MONITOR] = invalid_op_interception,
  2049. [SVM_EXIT_MWAIT] = invalid_op_interception,
  2050. [SVM_EXIT_NPF] = pf_interception,
  2051. };
  2052. static int handle_exit(struct kvm_vcpu *vcpu)
  2053. {
  2054. struct vcpu_svm *svm = to_svm(vcpu);
  2055. struct kvm_run *kvm_run = vcpu->run;
  2056. u32 exit_code = svm->vmcb->control.exit_code;
  2057. trace_kvm_exit(exit_code, svm->vmcb->save.rip);
  2058. if (unlikely(svm->nested.exit_required)) {
  2059. nested_svm_vmexit(svm);
  2060. svm->nested.exit_required = false;
  2061. return 1;
  2062. }
  2063. if (is_nested(svm)) {
  2064. int vmexit;
  2065. trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
  2066. svm->vmcb->control.exit_info_1,
  2067. svm->vmcb->control.exit_info_2,
  2068. svm->vmcb->control.exit_int_info,
  2069. svm->vmcb->control.exit_int_info_err);
  2070. vmexit = nested_svm_exit_special(svm);
  2071. if (vmexit == NESTED_EXIT_CONTINUE)
  2072. vmexit = nested_svm_exit_handled(svm);
  2073. if (vmexit == NESTED_EXIT_DONE)
  2074. return 1;
  2075. }
  2076. svm_complete_interrupts(svm);
  2077. if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR0_MASK))
  2078. vcpu->arch.cr0 = svm->vmcb->save.cr0;
  2079. if (npt_enabled)
  2080. vcpu->arch.cr3 = svm->vmcb->save.cr3;
  2081. if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
  2082. kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  2083. kvm_run->fail_entry.hardware_entry_failure_reason
  2084. = svm->vmcb->control.exit_code;
  2085. return 0;
  2086. }
  2087. if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
  2088. exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
  2089. exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH)
  2090. printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
  2091. "exit_code 0x%x\n",
  2092. __func__, svm->vmcb->control.exit_int_info,
  2093. exit_code);
  2094. if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
  2095. || !svm_exit_handlers[exit_code]) {
  2096. kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
  2097. kvm_run->hw.hardware_exit_reason = exit_code;
  2098. return 0;
  2099. }
  2100. return svm_exit_handlers[exit_code](svm);
  2101. }
  2102. static void reload_tss(struct kvm_vcpu *vcpu)
  2103. {
  2104. int cpu = raw_smp_processor_id();
  2105. struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
  2106. sd->tss_desc->type = 9; /* available 32/64-bit TSS */
  2107. load_TR_desc();
  2108. }
  2109. static void pre_svm_run(struct vcpu_svm *svm)
  2110. {
  2111. int cpu = raw_smp_processor_id();
  2112. struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
  2113. svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
  2114. /* FIXME: handle wraparound of asid_generation */
  2115. if (svm->asid_generation != sd->asid_generation)
  2116. new_asid(svm, sd);
  2117. }
  2118. static void svm_inject_nmi(struct kvm_vcpu *vcpu)
  2119. {
  2120. struct vcpu_svm *svm = to_svm(vcpu);
  2121. svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
  2122. vcpu->arch.hflags |= HF_NMI_MASK;
  2123. svm->vmcb->control.intercept |= (1UL << INTERCEPT_IRET);
  2124. ++vcpu->stat.nmi_injections;
  2125. }
  2126. static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
  2127. {
  2128. struct vmcb_control_area *control;
  2129. trace_kvm_inj_virq(irq);
  2130. ++svm->vcpu.stat.irq_injections;
  2131. control = &svm->vmcb->control;
  2132. control->int_vector = irq;
  2133. control->int_ctl &= ~V_INTR_PRIO_MASK;
  2134. control->int_ctl |= V_IRQ_MASK |
  2135. ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
  2136. }
  2137. static void svm_set_irq(struct kvm_vcpu *vcpu)
  2138. {
  2139. struct vcpu_svm *svm = to_svm(vcpu);
  2140. BUG_ON(!(gif_set(svm)));
  2141. svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
  2142. SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
  2143. }
  2144. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  2145. {
  2146. struct vcpu_svm *svm = to_svm(vcpu);
  2147. if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
  2148. return;
  2149. if (irr == -1)
  2150. return;
  2151. if (tpr >= irr)
  2152. svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
  2153. }
  2154. static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
  2155. {
  2156. struct vcpu_svm *svm = to_svm(vcpu);
  2157. struct vmcb *vmcb = svm->vmcb;
  2158. return !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
  2159. !(svm->vcpu.arch.hflags & HF_NMI_MASK);
  2160. }
  2161. static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
  2162. {
  2163. struct vcpu_svm *svm = to_svm(vcpu);
  2164. return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
  2165. }
  2166. static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
  2167. {
  2168. struct vcpu_svm *svm = to_svm(vcpu);
  2169. if (masked) {
  2170. svm->vcpu.arch.hflags |= HF_NMI_MASK;
  2171. svm->vmcb->control.intercept |= (1UL << INTERCEPT_IRET);
  2172. } else {
  2173. svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
  2174. svm->vmcb->control.intercept &= ~(1UL << INTERCEPT_IRET);
  2175. }
  2176. }
  2177. static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
  2178. {
  2179. struct vcpu_svm *svm = to_svm(vcpu);
  2180. struct vmcb *vmcb = svm->vmcb;
  2181. int ret;
  2182. if (!gif_set(svm) ||
  2183. (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
  2184. return 0;
  2185. ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
  2186. if (is_nested(svm))
  2187. return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
  2188. return ret;
  2189. }
  2190. static void enable_irq_window(struct kvm_vcpu *vcpu)
  2191. {
  2192. struct vcpu_svm *svm = to_svm(vcpu);
  2193. /* In case GIF=0 we can't rely on the CPU to tell us when
  2194. * GIF becomes 1, because that's a separate STGI/VMRUN intercept.
  2195. * The next time we get that intercept, this function will be
  2196. * called again though and we'll get the vintr intercept. */
  2197. if (gif_set(svm) && nested_svm_intr(svm)) {
  2198. svm_set_vintr(svm);
  2199. svm_inject_irq(svm, 0x0);
  2200. }
  2201. }
  2202. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  2203. {
  2204. struct vcpu_svm *svm = to_svm(vcpu);
  2205. if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
  2206. == HF_NMI_MASK)
  2207. return; /* IRET will cause a vm exit */
  2208. /* Something prevents NMI from been injected. Single step over
  2209. possible problem (IRET or exception injection or interrupt
  2210. shadow) */
  2211. svm->nmi_singlestep = true;
  2212. svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
  2213. update_db_intercept(vcpu);
  2214. }
  2215. static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
  2216. {
  2217. return 0;
  2218. }
  2219. static void svm_flush_tlb(struct kvm_vcpu *vcpu)
  2220. {
  2221. force_new_asid(vcpu);
  2222. }
  2223. static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
  2224. {
  2225. }
  2226. static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
  2227. {
  2228. struct vcpu_svm *svm = to_svm(vcpu);
  2229. if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
  2230. return;
  2231. if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) {
  2232. int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
  2233. kvm_set_cr8(vcpu, cr8);
  2234. }
  2235. }
  2236. static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
  2237. {
  2238. struct vcpu_svm *svm = to_svm(vcpu);
  2239. u64 cr8;
  2240. if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
  2241. return;
  2242. cr8 = kvm_get_cr8(vcpu);
  2243. svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
  2244. svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
  2245. }
  2246. static void svm_complete_interrupts(struct vcpu_svm *svm)
  2247. {
  2248. u8 vector;
  2249. int type;
  2250. u32 exitintinfo = svm->vmcb->control.exit_int_info;
  2251. unsigned int3_injected = svm->int3_injected;
  2252. svm->int3_injected = 0;
  2253. if (svm->vcpu.arch.hflags & HF_IRET_MASK)
  2254. svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
  2255. svm->vcpu.arch.nmi_injected = false;
  2256. kvm_clear_exception_queue(&svm->vcpu);
  2257. kvm_clear_interrupt_queue(&svm->vcpu);
  2258. if (!(exitintinfo & SVM_EXITINTINFO_VALID))
  2259. return;
  2260. vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
  2261. type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
  2262. switch (type) {
  2263. case SVM_EXITINTINFO_TYPE_NMI:
  2264. svm->vcpu.arch.nmi_injected = true;
  2265. break;
  2266. case SVM_EXITINTINFO_TYPE_EXEPT:
  2267. if (is_nested(svm))
  2268. break;
  2269. /*
  2270. * In case of software exceptions, do not reinject the vector,
  2271. * but re-execute the instruction instead. Rewind RIP first
  2272. * if we emulated INT3 before.
  2273. */
  2274. if (kvm_exception_is_soft(vector)) {
  2275. if (vector == BP_VECTOR && int3_injected &&
  2276. kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
  2277. kvm_rip_write(&svm->vcpu,
  2278. kvm_rip_read(&svm->vcpu) -
  2279. int3_injected);
  2280. break;
  2281. }
  2282. if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
  2283. u32 err = svm->vmcb->control.exit_int_info_err;
  2284. kvm_queue_exception_e(&svm->vcpu, vector, err);
  2285. } else
  2286. kvm_queue_exception(&svm->vcpu, vector);
  2287. break;
  2288. case SVM_EXITINTINFO_TYPE_INTR:
  2289. kvm_queue_interrupt(&svm->vcpu, vector, false);
  2290. break;
  2291. default:
  2292. break;
  2293. }
  2294. }
  2295. #ifdef CONFIG_X86_64
  2296. #define R "r"
  2297. #else
  2298. #define R "e"
  2299. #endif
  2300. static void svm_vcpu_run(struct kvm_vcpu *vcpu)
  2301. {
  2302. struct vcpu_svm *svm = to_svm(vcpu);
  2303. u16 fs_selector;
  2304. u16 gs_selector;
  2305. u16 ldt_selector;
  2306. /*
  2307. * A vmexit emulation is required before the vcpu can be executed
  2308. * again.
  2309. */
  2310. if (unlikely(svm->nested.exit_required))
  2311. return;
  2312. svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
  2313. svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
  2314. svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
  2315. pre_svm_run(svm);
  2316. sync_lapic_to_cr8(vcpu);
  2317. save_host_msrs(vcpu);
  2318. fs_selector = kvm_read_fs();
  2319. gs_selector = kvm_read_gs();
  2320. ldt_selector = kvm_read_ldt();
  2321. svm->vmcb->save.cr2 = vcpu->arch.cr2;
  2322. /* required for live migration with NPT */
  2323. if (npt_enabled)
  2324. svm->vmcb->save.cr3 = vcpu->arch.cr3;
  2325. clgi();
  2326. local_irq_enable();
  2327. asm volatile (
  2328. "push %%"R"bp; \n\t"
  2329. "mov %c[rbx](%[svm]), %%"R"bx \n\t"
  2330. "mov %c[rcx](%[svm]), %%"R"cx \n\t"
  2331. "mov %c[rdx](%[svm]), %%"R"dx \n\t"
  2332. "mov %c[rsi](%[svm]), %%"R"si \n\t"
  2333. "mov %c[rdi](%[svm]), %%"R"di \n\t"
  2334. "mov %c[rbp](%[svm]), %%"R"bp \n\t"
  2335. #ifdef CONFIG_X86_64
  2336. "mov %c[r8](%[svm]), %%r8 \n\t"
  2337. "mov %c[r9](%[svm]), %%r9 \n\t"
  2338. "mov %c[r10](%[svm]), %%r10 \n\t"
  2339. "mov %c[r11](%[svm]), %%r11 \n\t"
  2340. "mov %c[r12](%[svm]), %%r12 \n\t"
  2341. "mov %c[r13](%[svm]), %%r13 \n\t"
  2342. "mov %c[r14](%[svm]), %%r14 \n\t"
  2343. "mov %c[r15](%[svm]), %%r15 \n\t"
  2344. #endif
  2345. /* Enter guest mode */
  2346. "push %%"R"ax \n\t"
  2347. "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
  2348. __ex(SVM_VMLOAD) "\n\t"
  2349. __ex(SVM_VMRUN) "\n\t"
  2350. __ex(SVM_VMSAVE) "\n\t"
  2351. "pop %%"R"ax \n\t"
  2352. /* Save guest registers, load host registers */
  2353. "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
  2354. "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
  2355. "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
  2356. "mov %%"R"si, %c[rsi](%[svm]) \n\t"
  2357. "mov %%"R"di, %c[rdi](%[svm]) \n\t"
  2358. "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
  2359. #ifdef CONFIG_X86_64
  2360. "mov %%r8, %c[r8](%[svm]) \n\t"
  2361. "mov %%r9, %c[r9](%[svm]) \n\t"
  2362. "mov %%r10, %c[r10](%[svm]) \n\t"
  2363. "mov %%r11, %c[r11](%[svm]) \n\t"
  2364. "mov %%r12, %c[r12](%[svm]) \n\t"
  2365. "mov %%r13, %c[r13](%[svm]) \n\t"
  2366. "mov %%r14, %c[r14](%[svm]) \n\t"
  2367. "mov %%r15, %c[r15](%[svm]) \n\t"
  2368. #endif
  2369. "pop %%"R"bp"
  2370. :
  2371. : [svm]"a"(svm),
  2372. [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
  2373. [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
  2374. [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
  2375. [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
  2376. [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
  2377. [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
  2378. [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
  2379. #ifdef CONFIG_X86_64
  2380. , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
  2381. [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
  2382. [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
  2383. [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
  2384. [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
  2385. [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
  2386. [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
  2387. [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
  2388. #endif
  2389. : "cc", "memory"
  2390. , R"bx", R"cx", R"dx", R"si", R"di"
  2391. #ifdef CONFIG_X86_64
  2392. , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
  2393. #endif
  2394. );
  2395. vcpu->arch.cr2 = svm->vmcb->save.cr2;
  2396. vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
  2397. vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
  2398. vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
  2399. kvm_load_fs(fs_selector);
  2400. kvm_load_gs(gs_selector);
  2401. kvm_load_ldt(ldt_selector);
  2402. load_host_msrs(vcpu);
  2403. reload_tss(vcpu);
  2404. local_irq_disable();
  2405. stgi();
  2406. sync_cr8_to_lapic(vcpu);
  2407. svm->next_rip = 0;
  2408. if (npt_enabled) {
  2409. vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
  2410. vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
  2411. }
  2412. }
  2413. #undef R
  2414. static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  2415. {
  2416. struct vcpu_svm *svm = to_svm(vcpu);
  2417. if (npt_enabled) {
  2418. svm->vmcb->control.nested_cr3 = root;
  2419. force_new_asid(vcpu);
  2420. return;
  2421. }
  2422. svm->vmcb->save.cr3 = root;
  2423. force_new_asid(vcpu);
  2424. }
  2425. static int is_disabled(void)
  2426. {
  2427. u64 vm_cr;
  2428. rdmsrl(MSR_VM_CR, vm_cr);
  2429. if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
  2430. return 1;
  2431. return 0;
  2432. }
  2433. static void
  2434. svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  2435. {
  2436. /*
  2437. * Patch in the VMMCALL instruction:
  2438. */
  2439. hypercall[0] = 0x0f;
  2440. hypercall[1] = 0x01;
  2441. hypercall[2] = 0xd9;
  2442. }
  2443. static void svm_check_processor_compat(void *rtn)
  2444. {
  2445. *(int *)rtn = 0;
  2446. }
  2447. static bool svm_cpu_has_accelerated_tpr(void)
  2448. {
  2449. return false;
  2450. }
  2451. static int get_npt_level(void)
  2452. {
  2453. #ifdef CONFIG_X86_64
  2454. return PT64_ROOT_LEVEL;
  2455. #else
  2456. return PT32E_ROOT_LEVEL;
  2457. #endif
  2458. }
  2459. static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  2460. {
  2461. return 0;
  2462. }
  2463. static void svm_cpuid_update(struct kvm_vcpu *vcpu)
  2464. {
  2465. }
  2466. static const struct trace_print_flags svm_exit_reasons_str[] = {
  2467. { SVM_EXIT_READ_CR0, "read_cr0" },
  2468. { SVM_EXIT_READ_CR3, "read_cr3" },
  2469. { SVM_EXIT_READ_CR4, "read_cr4" },
  2470. { SVM_EXIT_READ_CR8, "read_cr8" },
  2471. { SVM_EXIT_WRITE_CR0, "write_cr0" },
  2472. { SVM_EXIT_WRITE_CR3, "write_cr3" },
  2473. { SVM_EXIT_WRITE_CR4, "write_cr4" },
  2474. { SVM_EXIT_WRITE_CR8, "write_cr8" },
  2475. { SVM_EXIT_READ_DR0, "read_dr0" },
  2476. { SVM_EXIT_READ_DR1, "read_dr1" },
  2477. { SVM_EXIT_READ_DR2, "read_dr2" },
  2478. { SVM_EXIT_READ_DR3, "read_dr3" },
  2479. { SVM_EXIT_WRITE_DR0, "write_dr0" },
  2480. { SVM_EXIT_WRITE_DR1, "write_dr1" },
  2481. { SVM_EXIT_WRITE_DR2, "write_dr2" },
  2482. { SVM_EXIT_WRITE_DR3, "write_dr3" },
  2483. { SVM_EXIT_WRITE_DR5, "write_dr5" },
  2484. { SVM_EXIT_WRITE_DR7, "write_dr7" },
  2485. { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
  2486. { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
  2487. { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
  2488. { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
  2489. { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
  2490. { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
  2491. { SVM_EXIT_INTR, "interrupt" },
  2492. { SVM_EXIT_NMI, "nmi" },
  2493. { SVM_EXIT_SMI, "smi" },
  2494. { SVM_EXIT_INIT, "init" },
  2495. { SVM_EXIT_VINTR, "vintr" },
  2496. { SVM_EXIT_CPUID, "cpuid" },
  2497. { SVM_EXIT_INVD, "invd" },
  2498. { SVM_EXIT_HLT, "hlt" },
  2499. { SVM_EXIT_INVLPG, "invlpg" },
  2500. { SVM_EXIT_INVLPGA, "invlpga" },
  2501. { SVM_EXIT_IOIO, "io" },
  2502. { SVM_EXIT_MSR, "msr" },
  2503. { SVM_EXIT_TASK_SWITCH, "task_switch" },
  2504. { SVM_EXIT_SHUTDOWN, "shutdown" },
  2505. { SVM_EXIT_VMRUN, "vmrun" },
  2506. { SVM_EXIT_VMMCALL, "hypercall" },
  2507. { SVM_EXIT_VMLOAD, "vmload" },
  2508. { SVM_EXIT_VMSAVE, "vmsave" },
  2509. { SVM_EXIT_STGI, "stgi" },
  2510. { SVM_EXIT_CLGI, "clgi" },
  2511. { SVM_EXIT_SKINIT, "skinit" },
  2512. { SVM_EXIT_WBINVD, "wbinvd" },
  2513. { SVM_EXIT_MONITOR, "monitor" },
  2514. { SVM_EXIT_MWAIT, "mwait" },
  2515. { SVM_EXIT_NPF, "npf" },
  2516. { -1, NULL }
  2517. };
  2518. static int svm_get_lpage_level(void)
  2519. {
  2520. return PT_PDPE_LEVEL;
  2521. }
  2522. static bool svm_rdtscp_supported(void)
  2523. {
  2524. return false;
  2525. }
  2526. static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
  2527. {
  2528. struct vcpu_svm *svm = to_svm(vcpu);
  2529. svm->vmcb->control.intercept_exceptions |= 1 << NM_VECTOR;
  2530. if (is_nested(svm))
  2531. svm->nested.hsave->control.intercept_exceptions |= 1 << NM_VECTOR;
  2532. update_cr0_intercept(svm);
  2533. }
  2534. static struct kvm_x86_ops svm_x86_ops = {
  2535. .cpu_has_kvm_support = has_svm,
  2536. .disabled_by_bios = is_disabled,
  2537. .hardware_setup = svm_hardware_setup,
  2538. .hardware_unsetup = svm_hardware_unsetup,
  2539. .check_processor_compatibility = svm_check_processor_compat,
  2540. .hardware_enable = svm_hardware_enable,
  2541. .hardware_disable = svm_hardware_disable,
  2542. .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
  2543. .vcpu_create = svm_create_vcpu,
  2544. .vcpu_free = svm_free_vcpu,
  2545. .vcpu_reset = svm_vcpu_reset,
  2546. .prepare_guest_switch = svm_prepare_guest_switch,
  2547. .vcpu_load = svm_vcpu_load,
  2548. .vcpu_put = svm_vcpu_put,
  2549. .set_guest_debug = svm_guest_debug,
  2550. .get_msr = svm_get_msr,
  2551. .set_msr = svm_set_msr,
  2552. .get_segment_base = svm_get_segment_base,
  2553. .get_segment = svm_get_segment,
  2554. .set_segment = svm_set_segment,
  2555. .get_cpl = svm_get_cpl,
  2556. .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
  2557. .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
  2558. .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
  2559. .set_cr0 = svm_set_cr0,
  2560. .set_cr3 = svm_set_cr3,
  2561. .set_cr4 = svm_set_cr4,
  2562. .set_efer = svm_set_efer,
  2563. .get_idt = svm_get_idt,
  2564. .set_idt = svm_set_idt,
  2565. .get_gdt = svm_get_gdt,
  2566. .set_gdt = svm_set_gdt,
  2567. .get_dr = svm_get_dr,
  2568. .set_dr = svm_set_dr,
  2569. .cache_reg = svm_cache_reg,
  2570. .get_rflags = svm_get_rflags,
  2571. .set_rflags = svm_set_rflags,
  2572. .fpu_activate = svm_fpu_activate,
  2573. .fpu_deactivate = svm_fpu_deactivate,
  2574. .tlb_flush = svm_flush_tlb,
  2575. .run = svm_vcpu_run,
  2576. .handle_exit = handle_exit,
  2577. .skip_emulated_instruction = skip_emulated_instruction,
  2578. .set_interrupt_shadow = svm_set_interrupt_shadow,
  2579. .get_interrupt_shadow = svm_get_interrupt_shadow,
  2580. .patch_hypercall = svm_patch_hypercall,
  2581. .set_irq = svm_set_irq,
  2582. .set_nmi = svm_inject_nmi,
  2583. .queue_exception = svm_queue_exception,
  2584. .interrupt_allowed = svm_interrupt_allowed,
  2585. .nmi_allowed = svm_nmi_allowed,
  2586. .get_nmi_mask = svm_get_nmi_mask,
  2587. .set_nmi_mask = svm_set_nmi_mask,
  2588. .enable_nmi_window = enable_nmi_window,
  2589. .enable_irq_window = enable_irq_window,
  2590. .update_cr8_intercept = update_cr8_intercept,
  2591. .set_tss_addr = svm_set_tss_addr,
  2592. .get_tdp_level = get_npt_level,
  2593. .get_mt_mask = svm_get_mt_mask,
  2594. .exit_reasons_str = svm_exit_reasons_str,
  2595. .get_lpage_level = svm_get_lpage_level,
  2596. .cpuid_update = svm_cpuid_update,
  2597. .rdtscp_supported = svm_rdtscp_supported,
  2598. };
  2599. static int __init svm_init(void)
  2600. {
  2601. return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
  2602. THIS_MODULE);
  2603. }
  2604. static void __exit svm_exit(void)
  2605. {
  2606. kvm_exit();
  2607. }
  2608. module_init(svm_init)
  2609. module_exit(svm_exit)