svm.c 76 KB

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