svm.c 75 KB

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