svm.c 77 KB

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