svm.c 74 KB

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