svm.c 80 KB

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