svm.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936
  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 DEBUGCTL_RESERVED_BITS (~(0x3fULL))
  41. /* Turn on to get debugging output*/
  42. /* #define NESTED_DEBUG */
  43. #ifdef NESTED_DEBUG
  44. #define nsvm_printk(fmt, args...) printk(KERN_INFO fmt, ## args)
  45. #else
  46. #define nsvm_printk(fmt, args...) do {} while(0)
  47. #endif
  48. static const u32 host_save_user_msrs[] = {
  49. #ifdef CONFIG_X86_64
  50. MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
  51. MSR_FS_BASE,
  52. #endif
  53. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  54. };
  55. #define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
  56. struct kvm_vcpu;
  57. struct nested_state {
  58. struct vmcb *hsave;
  59. u64 hsave_msr;
  60. u64 vmcb;
  61. /* These are the merged vectors */
  62. u32 *msrpm;
  63. /* gpa pointers to the real vectors */
  64. u64 vmcb_msrpm;
  65. /* cache for intercepts of the guest */
  66. u16 intercept_cr_read;
  67. u16 intercept_cr_write;
  68. u16 intercept_dr_read;
  69. u16 intercept_dr_write;
  70. u32 intercept_exceptions;
  71. u64 intercept;
  72. };
  73. struct vcpu_svm {
  74. struct kvm_vcpu vcpu;
  75. struct vmcb *vmcb;
  76. unsigned long vmcb_pa;
  77. struct svm_cpu_data *svm_data;
  78. uint64_t asid_generation;
  79. uint64_t sysenter_esp;
  80. uint64_t sysenter_eip;
  81. u64 next_rip;
  82. u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
  83. u64 host_gs_base;
  84. u32 *msrpm;
  85. struct nested_state nested;
  86. };
  87. /* enable NPT for AMD64 and X86 with PAE */
  88. #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
  89. static bool npt_enabled = true;
  90. #else
  91. static bool npt_enabled = false;
  92. #endif
  93. static int npt = 1;
  94. module_param(npt, int, S_IRUGO);
  95. static int nested = 0;
  96. module_param(nested, int, S_IRUGO);
  97. static void svm_flush_tlb(struct kvm_vcpu *vcpu);
  98. static void svm_complete_interrupts(struct vcpu_svm *svm);
  99. static int nested_svm_exit_handled(struct vcpu_svm *svm, bool kvm_override);
  100. static int nested_svm_vmexit(struct vcpu_svm *svm);
  101. static int nested_svm_vmsave(struct vcpu_svm *svm, void *nested_vmcb,
  102. void *arg2, void *opaque);
  103. static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
  104. bool has_error_code, u32 error_code);
  105. static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
  106. {
  107. return container_of(vcpu, struct vcpu_svm, vcpu);
  108. }
  109. static inline bool is_nested(struct vcpu_svm *svm)
  110. {
  111. return svm->nested.vmcb;
  112. }
  113. static inline void enable_gif(struct vcpu_svm *svm)
  114. {
  115. svm->vcpu.arch.hflags |= HF_GIF_MASK;
  116. }
  117. static inline void disable_gif(struct vcpu_svm *svm)
  118. {
  119. svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
  120. }
  121. static inline bool gif_set(struct vcpu_svm *svm)
  122. {
  123. return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
  124. }
  125. static unsigned long iopm_base;
  126. struct kvm_ldttss_desc {
  127. u16 limit0;
  128. u16 base0;
  129. unsigned base1 : 8, type : 5, dpl : 2, p : 1;
  130. unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8;
  131. u32 base3;
  132. u32 zero1;
  133. } __attribute__((packed));
  134. struct svm_cpu_data {
  135. int cpu;
  136. u64 asid_generation;
  137. u32 max_asid;
  138. u32 next_asid;
  139. struct kvm_ldttss_desc *tss_desc;
  140. struct page *save_area;
  141. };
  142. static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
  143. static uint32_t svm_features;
  144. struct svm_init_data {
  145. int cpu;
  146. int r;
  147. };
  148. static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
  149. #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
  150. #define MSRS_RANGE_SIZE 2048
  151. #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
  152. #define MAX_INST_SIZE 15
  153. static inline u32 svm_has(u32 feat)
  154. {
  155. return svm_features & feat;
  156. }
  157. static inline void clgi(void)
  158. {
  159. asm volatile (__ex(SVM_CLGI));
  160. }
  161. static inline void stgi(void)
  162. {
  163. asm volatile (__ex(SVM_STGI));
  164. }
  165. static inline void invlpga(unsigned long addr, u32 asid)
  166. {
  167. asm volatile (__ex(SVM_INVLPGA) :: "a"(addr), "c"(asid));
  168. }
  169. static inline void force_new_asid(struct kvm_vcpu *vcpu)
  170. {
  171. to_svm(vcpu)->asid_generation--;
  172. }
  173. static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
  174. {
  175. force_new_asid(vcpu);
  176. }
  177. static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
  178. {
  179. if (!npt_enabled && !(efer & EFER_LMA))
  180. efer &= ~EFER_LME;
  181. to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
  182. vcpu->arch.shadow_efer = efer;
  183. }
  184. static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
  185. bool has_error_code, u32 error_code)
  186. {
  187. struct vcpu_svm *svm = to_svm(vcpu);
  188. /* If we are within a nested VM we'd better #VMEXIT and let the
  189. guest handle the exception */
  190. if (nested_svm_check_exception(svm, nr, has_error_code, error_code))
  191. return;
  192. svm->vmcb->control.event_inj = nr
  193. | SVM_EVTINJ_VALID
  194. | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
  195. | SVM_EVTINJ_TYPE_EXEPT;
  196. svm->vmcb->control.event_inj_err = error_code;
  197. }
  198. static int is_external_interrupt(u32 info)
  199. {
  200. info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
  201. return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
  202. }
  203. static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  204. {
  205. struct vcpu_svm *svm = to_svm(vcpu);
  206. u32 ret = 0;
  207. if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
  208. ret |= X86_SHADOW_INT_STI | X86_SHADOW_INT_MOV_SS;
  209. return ret & mask;
  210. }
  211. static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
  212. {
  213. struct vcpu_svm *svm = to_svm(vcpu);
  214. if (mask == 0)
  215. svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
  216. else
  217. svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
  218. }
  219. static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
  220. {
  221. struct vcpu_svm *svm = to_svm(vcpu);
  222. if (!svm->next_rip) {
  223. if (emulate_instruction(vcpu, vcpu->run, 0, 0, EMULTYPE_SKIP) !=
  224. EMULATE_DONE)
  225. printk(KERN_DEBUG "%s: NOP\n", __func__);
  226. return;
  227. }
  228. if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
  229. printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
  230. __func__, kvm_rip_read(vcpu), svm->next_rip);
  231. kvm_rip_write(vcpu, svm->next_rip);
  232. svm_set_interrupt_shadow(vcpu, 0);
  233. }
  234. static int has_svm(void)
  235. {
  236. const char *msg;
  237. if (!cpu_has_svm(&msg)) {
  238. printk(KERN_INFO "has_svm: %s\n", msg);
  239. return 0;
  240. }
  241. return 1;
  242. }
  243. static void svm_hardware_disable(void *garbage)
  244. {
  245. cpu_svm_disable();
  246. }
  247. static void svm_hardware_enable(void *garbage)
  248. {
  249. struct svm_cpu_data *svm_data;
  250. uint64_t efer;
  251. struct descriptor_table gdt_descr;
  252. struct desc_struct *gdt;
  253. int me = raw_smp_processor_id();
  254. if (!has_svm()) {
  255. printk(KERN_ERR "svm_cpu_init: err EOPNOTSUPP on %d\n", me);
  256. return;
  257. }
  258. svm_data = per_cpu(svm_data, me);
  259. if (!svm_data) {
  260. printk(KERN_ERR "svm_cpu_init: svm_data is NULL on %d\n",
  261. me);
  262. return;
  263. }
  264. svm_data->asid_generation = 1;
  265. svm_data->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
  266. svm_data->next_asid = svm_data->max_asid + 1;
  267. kvm_get_gdt(&gdt_descr);
  268. gdt = (struct desc_struct *)gdt_descr.base;
  269. svm_data->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
  270. rdmsrl(MSR_EFER, efer);
  271. wrmsrl(MSR_EFER, efer | EFER_SVME);
  272. wrmsrl(MSR_VM_HSAVE_PA,
  273. page_to_pfn(svm_data->save_area) << PAGE_SHIFT);
  274. }
  275. static void svm_cpu_uninit(int cpu)
  276. {
  277. struct svm_cpu_data *svm_data
  278. = per_cpu(svm_data, raw_smp_processor_id());
  279. if (!svm_data)
  280. return;
  281. per_cpu(svm_data, raw_smp_processor_id()) = NULL;
  282. __free_page(svm_data->save_area);
  283. kfree(svm_data);
  284. }
  285. static int svm_cpu_init(int cpu)
  286. {
  287. struct svm_cpu_data *svm_data;
  288. int r;
  289. svm_data = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
  290. if (!svm_data)
  291. return -ENOMEM;
  292. svm_data->cpu = cpu;
  293. svm_data->save_area = alloc_page(GFP_KERNEL);
  294. r = -ENOMEM;
  295. if (!svm_data->save_area)
  296. goto err_1;
  297. per_cpu(svm_data, cpu) = svm_data;
  298. return 0;
  299. err_1:
  300. kfree(svm_data);
  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 *svm_data)
  890. {
  891. if (svm_data->next_asid > svm_data->max_asid) {
  892. ++svm_data->asid_generation;
  893. svm_data->next_asid = 1;
  894. svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
  895. }
  896. svm->asid_generation = svm_data->asid_generation;
  897. svm->vmcb->control.asid = svm_data->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. /*
  973. * FIXME: Tis shouldn't be necessary here, but there is a flush
  974. * missing in the MMU code. Until we find this bug, flush the
  975. * complete TLB here on an NPF
  976. */
  977. if (npt_enabled)
  978. svm_flush_tlb(&svm->vcpu);
  979. else {
  980. if (kvm_event_needs_reinjection(&svm->vcpu))
  981. kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
  982. }
  983. return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
  984. }
  985. static int db_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  986. {
  987. if (!(svm->vcpu.guest_debug &
  988. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
  989. !svm->vcpu.arch.singlestep) {
  990. kvm_queue_exception(&svm->vcpu, DB_VECTOR);
  991. return 1;
  992. }
  993. if (svm->vcpu.arch.singlestep) {
  994. svm->vcpu.arch.singlestep = false;
  995. if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
  996. svm->vmcb->save.rflags &=
  997. ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
  998. update_db_intercept(&svm->vcpu);
  999. }
  1000. if (svm->vcpu.guest_debug &
  1001. (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)){
  1002. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  1003. kvm_run->debug.arch.pc =
  1004. svm->vmcb->save.cs.base + svm->vmcb->save.rip;
  1005. kvm_run->debug.arch.exception = DB_VECTOR;
  1006. return 0;
  1007. }
  1008. return 1;
  1009. }
  1010. static int bp_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1011. {
  1012. kvm_run->exit_reason = KVM_EXIT_DEBUG;
  1013. kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
  1014. kvm_run->debug.arch.exception = BP_VECTOR;
  1015. return 0;
  1016. }
  1017. static int ud_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1018. {
  1019. int er;
  1020. er = emulate_instruction(&svm->vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
  1021. if (er != EMULATE_DONE)
  1022. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  1023. return 1;
  1024. }
  1025. static int nm_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1026. {
  1027. svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR);
  1028. if (!(svm->vcpu.arch.cr0 & X86_CR0_TS))
  1029. svm->vmcb->save.cr0 &= ~X86_CR0_TS;
  1030. svm->vcpu.fpu_active = 1;
  1031. return 1;
  1032. }
  1033. static int mc_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1034. {
  1035. /*
  1036. * On an #MC intercept the MCE handler is not called automatically in
  1037. * the host. So do it by hand here.
  1038. */
  1039. asm volatile (
  1040. "int $0x12\n");
  1041. /* not sure if we ever come back to this point */
  1042. return 1;
  1043. }
  1044. static int shutdown_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1045. {
  1046. /*
  1047. * VMCB is undefined after a SHUTDOWN intercept
  1048. * so reinitialize it.
  1049. */
  1050. clear_page(svm->vmcb);
  1051. init_vmcb(svm);
  1052. kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
  1053. return 0;
  1054. }
  1055. static int io_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1056. {
  1057. u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
  1058. int size, in, string;
  1059. unsigned port;
  1060. ++svm->vcpu.stat.io_exits;
  1061. svm->next_rip = svm->vmcb->control.exit_info_2;
  1062. string = (io_info & SVM_IOIO_STR_MASK) != 0;
  1063. if (string) {
  1064. if (emulate_instruction(&svm->vcpu,
  1065. kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
  1066. return 0;
  1067. return 1;
  1068. }
  1069. in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
  1070. port = io_info >> 16;
  1071. size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
  1072. skip_emulated_instruction(&svm->vcpu);
  1073. return kvm_emulate_pio(&svm->vcpu, kvm_run, in, size, port);
  1074. }
  1075. static int nmi_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1076. {
  1077. return 1;
  1078. }
  1079. static int intr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1080. {
  1081. ++svm->vcpu.stat.irq_exits;
  1082. return 1;
  1083. }
  1084. static int nop_on_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1085. {
  1086. return 1;
  1087. }
  1088. static int halt_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1089. {
  1090. svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
  1091. skip_emulated_instruction(&svm->vcpu);
  1092. return kvm_emulate_halt(&svm->vcpu);
  1093. }
  1094. static int vmmcall_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1095. {
  1096. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1097. skip_emulated_instruction(&svm->vcpu);
  1098. kvm_emulate_hypercall(&svm->vcpu);
  1099. return 1;
  1100. }
  1101. static int nested_svm_check_permissions(struct vcpu_svm *svm)
  1102. {
  1103. if (!(svm->vcpu.arch.shadow_efer & EFER_SVME)
  1104. || !is_paging(&svm->vcpu)) {
  1105. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  1106. return 1;
  1107. }
  1108. if (svm->vmcb->save.cpl) {
  1109. kvm_inject_gp(&svm->vcpu, 0);
  1110. return 1;
  1111. }
  1112. return 0;
  1113. }
  1114. static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
  1115. bool has_error_code, u32 error_code)
  1116. {
  1117. if (!is_nested(svm))
  1118. return 0;
  1119. svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
  1120. svm->vmcb->control.exit_code_hi = 0;
  1121. svm->vmcb->control.exit_info_1 = error_code;
  1122. svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
  1123. return nested_svm_exit_handled(svm, false);
  1124. }
  1125. static inline int nested_svm_intr(struct vcpu_svm *svm)
  1126. {
  1127. if (is_nested(svm)) {
  1128. if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
  1129. return 0;
  1130. if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
  1131. return 0;
  1132. svm->vmcb->control.exit_code = SVM_EXIT_INTR;
  1133. if (nested_svm_exit_handled(svm, false)) {
  1134. nsvm_printk("VMexit -> INTR\n");
  1135. return 1;
  1136. }
  1137. }
  1138. return 0;
  1139. }
  1140. static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, enum km_type idx)
  1141. {
  1142. struct page *page;
  1143. down_read(&current->mm->mmap_sem);
  1144. page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
  1145. up_read(&current->mm->mmap_sem);
  1146. if (is_error_page(page))
  1147. goto error;
  1148. return kmap_atomic(page, idx);
  1149. error:
  1150. kvm_release_page_clean(page);
  1151. kvm_inject_gp(&svm->vcpu, 0);
  1152. return NULL;
  1153. }
  1154. static void nested_svm_unmap(void *addr, enum km_type idx)
  1155. {
  1156. struct page *page;
  1157. if (!addr)
  1158. return;
  1159. page = kmap_atomic_to_page(addr);
  1160. kunmap_atomic(addr, idx);
  1161. kvm_release_page_dirty(page);
  1162. }
  1163. static struct page *nested_svm_get_page(struct vcpu_svm *svm, u64 gpa)
  1164. {
  1165. struct page *page;
  1166. down_read(&current->mm->mmap_sem);
  1167. page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
  1168. up_read(&current->mm->mmap_sem);
  1169. if (is_error_page(page)) {
  1170. printk(KERN_INFO "%s: could not find page at 0x%llx\n",
  1171. __func__, gpa);
  1172. kvm_release_page_clean(page);
  1173. kvm_inject_gp(&svm->vcpu, 0);
  1174. return NULL;
  1175. }
  1176. return page;
  1177. }
  1178. static int nested_svm_do(struct vcpu_svm *svm,
  1179. u64 arg1_gpa, u64 arg2_gpa, void *opaque,
  1180. int (*handler)(struct vcpu_svm *svm,
  1181. void *arg1,
  1182. void *arg2,
  1183. void *opaque))
  1184. {
  1185. struct page *arg1_page;
  1186. struct page *arg2_page = NULL;
  1187. void *arg1;
  1188. void *arg2 = NULL;
  1189. int retval;
  1190. arg1_page = nested_svm_get_page(svm, arg1_gpa);
  1191. if(arg1_page == NULL)
  1192. return 1;
  1193. if (arg2_gpa) {
  1194. arg2_page = nested_svm_get_page(svm, arg2_gpa);
  1195. if(arg2_page == NULL) {
  1196. kvm_release_page_clean(arg1_page);
  1197. return 1;
  1198. }
  1199. }
  1200. arg1 = kmap_atomic(arg1_page, KM_USER0);
  1201. if (arg2_gpa)
  1202. arg2 = kmap_atomic(arg2_page, KM_USER1);
  1203. retval = handler(svm, arg1, arg2, opaque);
  1204. kunmap_atomic(arg1, KM_USER0);
  1205. if (arg2_gpa)
  1206. kunmap_atomic(arg2, KM_USER1);
  1207. kvm_release_page_dirty(arg1_page);
  1208. if (arg2_gpa)
  1209. kvm_release_page_dirty(arg2_page);
  1210. return retval;
  1211. }
  1212. static bool nested_svm_exit_handled_msr(struct vcpu_svm *svm)
  1213. {
  1214. u32 param = svm->vmcb->control.exit_info_1 & 1;
  1215. u32 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  1216. bool ret = false;
  1217. u32 t0, t1;
  1218. u8 *msrpm;
  1219. if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
  1220. return false;
  1221. msrpm = nested_svm_map(svm, svm->nested.vmcb_msrpm, KM_USER0);
  1222. if (!msrpm)
  1223. goto out;
  1224. switch (msr) {
  1225. case 0 ... 0x1fff:
  1226. t0 = (msr * 2) % 8;
  1227. t1 = msr / 8;
  1228. break;
  1229. case 0xc0000000 ... 0xc0001fff:
  1230. t0 = (8192 + msr - 0xc0000000) * 2;
  1231. t1 = (t0 / 8);
  1232. t0 %= 8;
  1233. break;
  1234. case 0xc0010000 ... 0xc0011fff:
  1235. t0 = (16384 + msr - 0xc0010000) * 2;
  1236. t1 = (t0 / 8);
  1237. t0 %= 8;
  1238. break;
  1239. default:
  1240. ret = true;
  1241. goto out;
  1242. }
  1243. ret = msrpm[t1] & ((1 << param) << t0);
  1244. out:
  1245. nested_svm_unmap(msrpm, KM_USER0);
  1246. return ret;
  1247. }
  1248. static int nested_svm_exit_handled(struct vcpu_svm *svm, bool kvm_override)
  1249. {
  1250. u32 exit_code = svm->vmcb->control.exit_code;
  1251. bool vmexit = false;
  1252. if (kvm_override) {
  1253. switch (exit_code) {
  1254. case SVM_EXIT_INTR:
  1255. case SVM_EXIT_NMI:
  1256. return 0;
  1257. /* For now we are always handling NPFs when using them */
  1258. case SVM_EXIT_NPF:
  1259. if (npt_enabled)
  1260. return 0;
  1261. break;
  1262. /* When we're shadowing, trap PFs */
  1263. case SVM_EXIT_EXCP_BASE + PF_VECTOR:
  1264. if (!npt_enabled)
  1265. return 0;
  1266. break;
  1267. default:
  1268. break;
  1269. }
  1270. }
  1271. switch (exit_code) {
  1272. case SVM_EXIT_MSR:
  1273. vmexit = nested_svm_exit_handled_msr(svm);
  1274. break;
  1275. case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR8: {
  1276. u32 cr_bits = 1 << (exit_code - SVM_EXIT_READ_CR0);
  1277. if (svm->nested.intercept_cr_read & cr_bits)
  1278. vmexit = true;
  1279. break;
  1280. }
  1281. case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR8: {
  1282. u32 cr_bits = 1 << (exit_code - SVM_EXIT_WRITE_CR0);
  1283. if (svm->nested.intercept_cr_write & cr_bits)
  1284. vmexit = true;
  1285. break;
  1286. }
  1287. case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR7: {
  1288. u32 dr_bits = 1 << (exit_code - SVM_EXIT_READ_DR0);
  1289. if (svm->nested.intercept_dr_read & dr_bits)
  1290. vmexit = true;
  1291. break;
  1292. }
  1293. case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR7: {
  1294. u32 dr_bits = 1 << (exit_code - SVM_EXIT_WRITE_DR0);
  1295. if (svm->nested.intercept_dr_write & dr_bits)
  1296. vmexit = true;
  1297. break;
  1298. }
  1299. case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
  1300. u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
  1301. if (svm->nested.intercept_exceptions & excp_bits)
  1302. vmexit = true;
  1303. break;
  1304. }
  1305. default: {
  1306. u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
  1307. nsvm_printk("exit code: 0x%x\n", exit_code);
  1308. if (svm->nested.intercept & exit_bits)
  1309. vmexit = true;
  1310. }
  1311. }
  1312. if (vmexit) {
  1313. nsvm_printk("#VMEXIT reason=%04x\n", exit_code);
  1314. nested_svm_vmexit(svm);
  1315. }
  1316. return vmexit;
  1317. }
  1318. static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
  1319. {
  1320. struct vmcb_control_area *dst = &dst_vmcb->control;
  1321. struct vmcb_control_area *from = &from_vmcb->control;
  1322. dst->intercept_cr_read = from->intercept_cr_read;
  1323. dst->intercept_cr_write = from->intercept_cr_write;
  1324. dst->intercept_dr_read = from->intercept_dr_read;
  1325. dst->intercept_dr_write = from->intercept_dr_write;
  1326. dst->intercept_exceptions = from->intercept_exceptions;
  1327. dst->intercept = from->intercept;
  1328. dst->iopm_base_pa = from->iopm_base_pa;
  1329. dst->msrpm_base_pa = from->msrpm_base_pa;
  1330. dst->tsc_offset = from->tsc_offset;
  1331. dst->asid = from->asid;
  1332. dst->tlb_ctl = from->tlb_ctl;
  1333. dst->int_ctl = from->int_ctl;
  1334. dst->int_vector = from->int_vector;
  1335. dst->int_state = from->int_state;
  1336. dst->exit_code = from->exit_code;
  1337. dst->exit_code_hi = from->exit_code_hi;
  1338. dst->exit_info_1 = from->exit_info_1;
  1339. dst->exit_info_2 = from->exit_info_2;
  1340. dst->exit_int_info = from->exit_int_info;
  1341. dst->exit_int_info_err = from->exit_int_info_err;
  1342. dst->nested_ctl = from->nested_ctl;
  1343. dst->event_inj = from->event_inj;
  1344. dst->event_inj_err = from->event_inj_err;
  1345. dst->nested_cr3 = from->nested_cr3;
  1346. dst->lbr_ctl = from->lbr_ctl;
  1347. }
  1348. static int nested_svm_vmexit(struct vcpu_svm *svm)
  1349. {
  1350. struct vmcb *nested_vmcb;
  1351. struct vmcb *hsave = svm->nested.hsave;
  1352. struct vmcb *vmcb = svm->vmcb;
  1353. nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, KM_USER0);
  1354. if (!nested_vmcb)
  1355. return 1;
  1356. /* Give the current vmcb to the guest */
  1357. disable_gif(svm);
  1358. nested_vmcb->save.es = vmcb->save.es;
  1359. nested_vmcb->save.cs = vmcb->save.cs;
  1360. nested_vmcb->save.ss = vmcb->save.ss;
  1361. nested_vmcb->save.ds = vmcb->save.ds;
  1362. nested_vmcb->save.gdtr = vmcb->save.gdtr;
  1363. nested_vmcb->save.idtr = vmcb->save.idtr;
  1364. if (npt_enabled)
  1365. nested_vmcb->save.cr3 = vmcb->save.cr3;
  1366. nested_vmcb->save.cr2 = vmcb->save.cr2;
  1367. nested_vmcb->save.rflags = vmcb->save.rflags;
  1368. nested_vmcb->save.rip = vmcb->save.rip;
  1369. nested_vmcb->save.rsp = vmcb->save.rsp;
  1370. nested_vmcb->save.rax = vmcb->save.rax;
  1371. nested_vmcb->save.dr7 = vmcb->save.dr7;
  1372. nested_vmcb->save.dr6 = vmcb->save.dr6;
  1373. nested_vmcb->save.cpl = vmcb->save.cpl;
  1374. nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
  1375. nested_vmcb->control.int_vector = vmcb->control.int_vector;
  1376. nested_vmcb->control.int_state = vmcb->control.int_state;
  1377. nested_vmcb->control.exit_code = vmcb->control.exit_code;
  1378. nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
  1379. nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
  1380. nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
  1381. nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
  1382. nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
  1383. nested_vmcb->control.tlb_ctl = 0;
  1384. nested_vmcb->control.event_inj = 0;
  1385. nested_vmcb->control.event_inj_err = 0;
  1386. /* We always set V_INTR_MASKING and remember the old value in hflags */
  1387. if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
  1388. nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
  1389. /* Restore the original control entries */
  1390. copy_vmcb_control_area(vmcb, hsave);
  1391. /* Kill any pending exceptions */
  1392. if (svm->vcpu.arch.exception.pending == true)
  1393. nsvm_printk("WARNING: Pending Exception\n");
  1394. kvm_clear_exception_queue(&svm->vcpu);
  1395. kvm_clear_interrupt_queue(&svm->vcpu);
  1396. /* Restore selected save entries */
  1397. svm->vmcb->save.es = hsave->save.es;
  1398. svm->vmcb->save.cs = hsave->save.cs;
  1399. svm->vmcb->save.ss = hsave->save.ss;
  1400. svm->vmcb->save.ds = hsave->save.ds;
  1401. svm->vmcb->save.gdtr = hsave->save.gdtr;
  1402. svm->vmcb->save.idtr = hsave->save.idtr;
  1403. svm->vmcb->save.rflags = hsave->save.rflags;
  1404. svm_set_efer(&svm->vcpu, hsave->save.efer);
  1405. svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
  1406. svm_set_cr4(&svm->vcpu, hsave->save.cr4);
  1407. if (npt_enabled) {
  1408. svm->vmcb->save.cr3 = hsave->save.cr3;
  1409. svm->vcpu.arch.cr3 = hsave->save.cr3;
  1410. } else {
  1411. kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
  1412. }
  1413. kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
  1414. kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
  1415. kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
  1416. svm->vmcb->save.dr7 = 0;
  1417. svm->vmcb->save.cpl = 0;
  1418. svm->vmcb->control.exit_int_info = 0;
  1419. /* Exit nested SVM mode */
  1420. svm->nested.vmcb = 0;
  1421. nested_svm_unmap(nested_vmcb, KM_USER0);
  1422. kvm_mmu_reset_context(&svm->vcpu);
  1423. kvm_mmu_load(&svm->vcpu);
  1424. return 0;
  1425. }
  1426. static int nested_svm_vmrun_msrpm(struct vcpu_svm *svm, void *arg1,
  1427. void *arg2, void *opaque)
  1428. {
  1429. int i;
  1430. u32 *nested_msrpm = (u32*)arg1;
  1431. for (i=0; i< PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER) / 4; i++)
  1432. svm->nested.msrpm[i] = svm->msrpm[i] | nested_msrpm[i];
  1433. svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
  1434. return 0;
  1435. }
  1436. static int nested_svm_vmrun(struct vcpu_svm *svm, void *arg1,
  1437. void *arg2, void *opaque)
  1438. {
  1439. struct vmcb *nested_vmcb = (struct vmcb *)arg1;
  1440. struct vmcb *hsave = svm->nested.hsave;
  1441. struct vmcb *vmcb = svm->vmcb;
  1442. /* nested_vmcb is our indicator if nested SVM is activated */
  1443. svm->nested.vmcb = svm->vmcb->save.rax;
  1444. /* Clear internal status */
  1445. kvm_clear_exception_queue(&svm->vcpu);
  1446. kvm_clear_interrupt_queue(&svm->vcpu);
  1447. /* Save the old vmcb, so we don't need to pick what we save, but
  1448. can restore everything when a VMEXIT occurs */
  1449. hsave->save.es = vmcb->save.es;
  1450. hsave->save.cs = vmcb->save.cs;
  1451. hsave->save.ss = vmcb->save.ss;
  1452. hsave->save.ds = vmcb->save.ds;
  1453. hsave->save.gdtr = vmcb->save.gdtr;
  1454. hsave->save.idtr = vmcb->save.idtr;
  1455. hsave->save.efer = svm->vcpu.arch.shadow_efer;
  1456. hsave->save.cr0 = svm->vcpu.arch.cr0;
  1457. hsave->save.cr4 = svm->vcpu.arch.cr4;
  1458. hsave->save.rflags = vmcb->save.rflags;
  1459. hsave->save.rip = svm->next_rip;
  1460. hsave->save.rsp = vmcb->save.rsp;
  1461. hsave->save.rax = vmcb->save.rax;
  1462. if (npt_enabled)
  1463. hsave->save.cr3 = vmcb->save.cr3;
  1464. else
  1465. hsave->save.cr3 = svm->vcpu.arch.cr3;
  1466. copy_vmcb_control_area(hsave, vmcb);
  1467. if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
  1468. svm->vcpu.arch.hflags |= HF_HIF_MASK;
  1469. else
  1470. svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
  1471. /* Load the nested guest state */
  1472. svm->vmcb->save.es = nested_vmcb->save.es;
  1473. svm->vmcb->save.cs = nested_vmcb->save.cs;
  1474. svm->vmcb->save.ss = nested_vmcb->save.ss;
  1475. svm->vmcb->save.ds = nested_vmcb->save.ds;
  1476. svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
  1477. svm->vmcb->save.idtr = nested_vmcb->save.idtr;
  1478. svm->vmcb->save.rflags = nested_vmcb->save.rflags;
  1479. svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
  1480. svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
  1481. svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
  1482. if (npt_enabled) {
  1483. svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
  1484. svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
  1485. } else {
  1486. kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
  1487. kvm_mmu_reset_context(&svm->vcpu);
  1488. }
  1489. svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
  1490. kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
  1491. kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
  1492. kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
  1493. /* In case we don't even reach vcpu_run, the fields are not updated */
  1494. svm->vmcb->save.rax = nested_vmcb->save.rax;
  1495. svm->vmcb->save.rsp = nested_vmcb->save.rsp;
  1496. svm->vmcb->save.rip = nested_vmcb->save.rip;
  1497. svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
  1498. svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
  1499. svm->vmcb->save.cpl = nested_vmcb->save.cpl;
  1500. /* We don't want a nested guest to be more powerful than the guest,
  1501. so all intercepts are ORed */
  1502. svm->vmcb->control.intercept_cr_read |=
  1503. nested_vmcb->control.intercept_cr_read;
  1504. svm->vmcb->control.intercept_cr_write |=
  1505. nested_vmcb->control.intercept_cr_write;
  1506. svm->vmcb->control.intercept_dr_read |=
  1507. nested_vmcb->control.intercept_dr_read;
  1508. svm->vmcb->control.intercept_dr_write |=
  1509. nested_vmcb->control.intercept_dr_write;
  1510. svm->vmcb->control.intercept_exceptions |=
  1511. nested_vmcb->control.intercept_exceptions;
  1512. svm->vmcb->control.intercept |= nested_vmcb->control.intercept;
  1513. svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa;
  1514. /* cache intercepts */
  1515. svm->nested.intercept_cr_read = nested_vmcb->control.intercept_cr_read;
  1516. svm->nested.intercept_cr_write = nested_vmcb->control.intercept_cr_write;
  1517. svm->nested.intercept_dr_read = nested_vmcb->control.intercept_dr_read;
  1518. svm->nested.intercept_dr_write = nested_vmcb->control.intercept_dr_write;
  1519. svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
  1520. svm->nested.intercept = nested_vmcb->control.intercept;
  1521. force_new_asid(&svm->vcpu);
  1522. svm->vmcb->control.exit_int_info = nested_vmcb->control.exit_int_info;
  1523. svm->vmcb->control.exit_int_info_err = nested_vmcb->control.exit_int_info_err;
  1524. svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
  1525. if (nested_vmcb->control.int_ctl & V_IRQ_MASK) {
  1526. nsvm_printk("nSVM Injecting Interrupt: 0x%x\n",
  1527. nested_vmcb->control.int_ctl);
  1528. }
  1529. if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
  1530. svm->vcpu.arch.hflags |= HF_VINTR_MASK;
  1531. else
  1532. svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
  1533. nsvm_printk("nSVM exit_int_info: 0x%x | int_state: 0x%x\n",
  1534. nested_vmcb->control.exit_int_info,
  1535. nested_vmcb->control.int_state);
  1536. svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
  1537. svm->vmcb->control.int_state = nested_vmcb->control.int_state;
  1538. svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
  1539. if (nested_vmcb->control.event_inj & SVM_EVTINJ_VALID)
  1540. nsvm_printk("Injecting Event: 0x%x\n",
  1541. nested_vmcb->control.event_inj);
  1542. svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
  1543. svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
  1544. enable_gif(svm);
  1545. return 0;
  1546. }
  1547. static int nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
  1548. {
  1549. to_vmcb->save.fs = from_vmcb->save.fs;
  1550. to_vmcb->save.gs = from_vmcb->save.gs;
  1551. to_vmcb->save.tr = from_vmcb->save.tr;
  1552. to_vmcb->save.ldtr = from_vmcb->save.ldtr;
  1553. to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
  1554. to_vmcb->save.star = from_vmcb->save.star;
  1555. to_vmcb->save.lstar = from_vmcb->save.lstar;
  1556. to_vmcb->save.cstar = from_vmcb->save.cstar;
  1557. to_vmcb->save.sfmask = from_vmcb->save.sfmask;
  1558. to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
  1559. to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
  1560. to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
  1561. return 1;
  1562. }
  1563. static int nested_svm_vmload(struct vcpu_svm *svm, void *nested_vmcb,
  1564. void *arg2, void *opaque)
  1565. {
  1566. return nested_svm_vmloadsave((struct vmcb *)nested_vmcb, svm->vmcb);
  1567. }
  1568. static int nested_svm_vmsave(struct vcpu_svm *svm, void *nested_vmcb,
  1569. void *arg2, void *opaque)
  1570. {
  1571. return nested_svm_vmloadsave(svm->vmcb, (struct vmcb *)nested_vmcb);
  1572. }
  1573. static int vmload_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. nested_svm_do(svm, svm->vmcb->save.rax, 0, NULL, nested_svm_vmload);
  1580. return 1;
  1581. }
  1582. static int vmsave_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1583. {
  1584. if (nested_svm_check_permissions(svm))
  1585. return 1;
  1586. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1587. skip_emulated_instruction(&svm->vcpu);
  1588. nested_svm_do(svm, svm->vmcb->save.rax, 0, NULL, nested_svm_vmsave);
  1589. return 1;
  1590. }
  1591. static int vmrun_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1592. {
  1593. nsvm_printk("VMrun\n");
  1594. if (nested_svm_check_permissions(svm))
  1595. return 1;
  1596. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1597. skip_emulated_instruction(&svm->vcpu);
  1598. if (nested_svm_do(svm, svm->vmcb->save.rax, 0,
  1599. NULL, nested_svm_vmrun))
  1600. return 1;
  1601. if (nested_svm_do(svm, svm->nested.vmcb_msrpm, 0,
  1602. NULL, nested_svm_vmrun_msrpm))
  1603. return 1;
  1604. return 1;
  1605. }
  1606. static int stgi_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1607. {
  1608. if (nested_svm_check_permissions(svm))
  1609. return 1;
  1610. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1611. skip_emulated_instruction(&svm->vcpu);
  1612. enable_gif(svm);
  1613. return 1;
  1614. }
  1615. static int clgi_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1616. {
  1617. if (nested_svm_check_permissions(svm))
  1618. return 1;
  1619. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1620. skip_emulated_instruction(&svm->vcpu);
  1621. disable_gif(svm);
  1622. /* After a CLGI no interrupts should come */
  1623. svm_clear_vintr(svm);
  1624. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  1625. return 1;
  1626. }
  1627. static int invlpga_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1628. {
  1629. struct kvm_vcpu *vcpu = &svm->vcpu;
  1630. nsvm_printk("INVLPGA\n");
  1631. /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
  1632. kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
  1633. svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
  1634. skip_emulated_instruction(&svm->vcpu);
  1635. return 1;
  1636. }
  1637. static int invalid_op_interception(struct vcpu_svm *svm,
  1638. struct kvm_run *kvm_run)
  1639. {
  1640. kvm_queue_exception(&svm->vcpu, UD_VECTOR);
  1641. return 1;
  1642. }
  1643. static int task_switch_interception(struct vcpu_svm *svm,
  1644. struct kvm_run *kvm_run)
  1645. {
  1646. u16 tss_selector;
  1647. int reason;
  1648. int int_type = svm->vmcb->control.exit_int_info &
  1649. SVM_EXITINTINFO_TYPE_MASK;
  1650. int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
  1651. uint32_t type =
  1652. svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
  1653. uint32_t idt_v =
  1654. svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
  1655. tss_selector = (u16)svm->vmcb->control.exit_info_1;
  1656. if (svm->vmcb->control.exit_info_2 &
  1657. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
  1658. reason = TASK_SWITCH_IRET;
  1659. else if (svm->vmcb->control.exit_info_2 &
  1660. (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
  1661. reason = TASK_SWITCH_JMP;
  1662. else if (idt_v)
  1663. reason = TASK_SWITCH_GATE;
  1664. else
  1665. reason = TASK_SWITCH_CALL;
  1666. if (reason == TASK_SWITCH_GATE) {
  1667. switch (type) {
  1668. case SVM_EXITINTINFO_TYPE_NMI:
  1669. svm->vcpu.arch.nmi_injected = false;
  1670. break;
  1671. case SVM_EXITINTINFO_TYPE_EXEPT:
  1672. kvm_clear_exception_queue(&svm->vcpu);
  1673. break;
  1674. case SVM_EXITINTINFO_TYPE_INTR:
  1675. kvm_clear_interrupt_queue(&svm->vcpu);
  1676. break;
  1677. default:
  1678. break;
  1679. }
  1680. }
  1681. if (reason != TASK_SWITCH_GATE ||
  1682. int_type == SVM_EXITINTINFO_TYPE_SOFT ||
  1683. (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
  1684. (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
  1685. skip_emulated_instruction(&svm->vcpu);
  1686. return kvm_task_switch(&svm->vcpu, tss_selector, reason);
  1687. }
  1688. static int cpuid_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1689. {
  1690. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  1691. kvm_emulate_cpuid(&svm->vcpu);
  1692. return 1;
  1693. }
  1694. static int iret_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1695. {
  1696. ++svm->vcpu.stat.nmi_window_exits;
  1697. svm->vmcb->control.intercept &= ~(1UL << INTERCEPT_IRET);
  1698. svm->vcpu.arch.hflags |= HF_IRET_MASK;
  1699. return 1;
  1700. }
  1701. static int invlpg_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1702. {
  1703. if (emulate_instruction(&svm->vcpu, kvm_run, 0, 0, 0) != EMULATE_DONE)
  1704. pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
  1705. return 1;
  1706. }
  1707. static int emulate_on_interception(struct vcpu_svm *svm,
  1708. struct kvm_run *kvm_run)
  1709. {
  1710. if (emulate_instruction(&svm->vcpu, NULL, 0, 0, 0) != EMULATE_DONE)
  1711. pr_unimpl(&svm->vcpu, "%s: failed\n", __func__);
  1712. return 1;
  1713. }
  1714. static int cr8_write_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1715. {
  1716. u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
  1717. /* instruction emulation calls kvm_set_cr8() */
  1718. emulate_instruction(&svm->vcpu, NULL, 0, 0, 0);
  1719. if (irqchip_in_kernel(svm->vcpu.kvm)) {
  1720. svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
  1721. return 1;
  1722. }
  1723. if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
  1724. return 1;
  1725. kvm_run->exit_reason = KVM_EXIT_SET_TPR;
  1726. return 0;
  1727. }
  1728. static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
  1729. {
  1730. struct vcpu_svm *svm = to_svm(vcpu);
  1731. switch (ecx) {
  1732. case MSR_IA32_TSC: {
  1733. u64 tsc;
  1734. rdtscll(tsc);
  1735. *data = svm->vmcb->control.tsc_offset + tsc;
  1736. break;
  1737. }
  1738. case MSR_K6_STAR:
  1739. *data = svm->vmcb->save.star;
  1740. break;
  1741. #ifdef CONFIG_X86_64
  1742. case MSR_LSTAR:
  1743. *data = svm->vmcb->save.lstar;
  1744. break;
  1745. case MSR_CSTAR:
  1746. *data = svm->vmcb->save.cstar;
  1747. break;
  1748. case MSR_KERNEL_GS_BASE:
  1749. *data = svm->vmcb->save.kernel_gs_base;
  1750. break;
  1751. case MSR_SYSCALL_MASK:
  1752. *data = svm->vmcb->save.sfmask;
  1753. break;
  1754. #endif
  1755. case MSR_IA32_SYSENTER_CS:
  1756. *data = svm->vmcb->save.sysenter_cs;
  1757. break;
  1758. case MSR_IA32_SYSENTER_EIP:
  1759. *data = svm->sysenter_eip;
  1760. break;
  1761. case MSR_IA32_SYSENTER_ESP:
  1762. *data = svm->sysenter_esp;
  1763. break;
  1764. /* Nobody will change the following 5 values in the VMCB so
  1765. we can safely return them on rdmsr. They will always be 0
  1766. until LBRV is implemented. */
  1767. case MSR_IA32_DEBUGCTLMSR:
  1768. *data = svm->vmcb->save.dbgctl;
  1769. break;
  1770. case MSR_IA32_LASTBRANCHFROMIP:
  1771. *data = svm->vmcb->save.br_from;
  1772. break;
  1773. case MSR_IA32_LASTBRANCHTOIP:
  1774. *data = svm->vmcb->save.br_to;
  1775. break;
  1776. case MSR_IA32_LASTINTFROMIP:
  1777. *data = svm->vmcb->save.last_excp_from;
  1778. break;
  1779. case MSR_IA32_LASTINTTOIP:
  1780. *data = svm->vmcb->save.last_excp_to;
  1781. break;
  1782. case MSR_VM_HSAVE_PA:
  1783. *data = svm->nested.hsave_msr;
  1784. break;
  1785. case MSR_VM_CR:
  1786. *data = 0;
  1787. break;
  1788. case MSR_IA32_UCODE_REV:
  1789. *data = 0x01000065;
  1790. break;
  1791. default:
  1792. return kvm_get_msr_common(vcpu, ecx, data);
  1793. }
  1794. return 0;
  1795. }
  1796. static int rdmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1797. {
  1798. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  1799. u64 data;
  1800. if (svm_get_msr(&svm->vcpu, ecx, &data))
  1801. kvm_inject_gp(&svm->vcpu, 0);
  1802. else {
  1803. trace_kvm_msr_read(ecx, data);
  1804. svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
  1805. svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
  1806. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  1807. skip_emulated_instruction(&svm->vcpu);
  1808. }
  1809. return 1;
  1810. }
  1811. static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
  1812. {
  1813. struct vcpu_svm *svm = to_svm(vcpu);
  1814. switch (ecx) {
  1815. case MSR_IA32_TSC: {
  1816. u64 tsc;
  1817. rdtscll(tsc);
  1818. svm->vmcb->control.tsc_offset = data - tsc;
  1819. break;
  1820. }
  1821. case MSR_K6_STAR:
  1822. svm->vmcb->save.star = data;
  1823. break;
  1824. #ifdef CONFIG_X86_64
  1825. case MSR_LSTAR:
  1826. svm->vmcb->save.lstar = data;
  1827. break;
  1828. case MSR_CSTAR:
  1829. svm->vmcb->save.cstar = data;
  1830. break;
  1831. case MSR_KERNEL_GS_BASE:
  1832. svm->vmcb->save.kernel_gs_base = data;
  1833. break;
  1834. case MSR_SYSCALL_MASK:
  1835. svm->vmcb->save.sfmask = data;
  1836. break;
  1837. #endif
  1838. case MSR_IA32_SYSENTER_CS:
  1839. svm->vmcb->save.sysenter_cs = data;
  1840. break;
  1841. case MSR_IA32_SYSENTER_EIP:
  1842. svm->sysenter_eip = data;
  1843. svm->vmcb->save.sysenter_eip = data;
  1844. break;
  1845. case MSR_IA32_SYSENTER_ESP:
  1846. svm->sysenter_esp = data;
  1847. svm->vmcb->save.sysenter_esp = data;
  1848. break;
  1849. case MSR_IA32_DEBUGCTLMSR:
  1850. if (!svm_has(SVM_FEATURE_LBRV)) {
  1851. pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
  1852. __func__, data);
  1853. break;
  1854. }
  1855. if (data & DEBUGCTL_RESERVED_BITS)
  1856. return 1;
  1857. svm->vmcb->save.dbgctl = data;
  1858. if (data & (1ULL<<0))
  1859. svm_enable_lbrv(svm);
  1860. else
  1861. svm_disable_lbrv(svm);
  1862. break;
  1863. case MSR_VM_HSAVE_PA:
  1864. svm->nested.hsave_msr = data;
  1865. break;
  1866. case MSR_VM_CR:
  1867. case MSR_VM_IGNNE:
  1868. pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
  1869. break;
  1870. default:
  1871. return kvm_set_msr_common(vcpu, ecx, data);
  1872. }
  1873. return 0;
  1874. }
  1875. static int wrmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1876. {
  1877. u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
  1878. u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
  1879. | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
  1880. trace_kvm_msr_write(ecx, data);
  1881. svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
  1882. if (svm_set_msr(&svm->vcpu, ecx, data))
  1883. kvm_inject_gp(&svm->vcpu, 0);
  1884. else
  1885. skip_emulated_instruction(&svm->vcpu);
  1886. return 1;
  1887. }
  1888. static int msr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run)
  1889. {
  1890. if (svm->vmcb->control.exit_info_1)
  1891. return wrmsr_interception(svm, kvm_run);
  1892. else
  1893. return rdmsr_interception(svm, kvm_run);
  1894. }
  1895. static int interrupt_window_interception(struct vcpu_svm *svm,
  1896. struct kvm_run *kvm_run)
  1897. {
  1898. svm_clear_vintr(svm);
  1899. svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
  1900. /*
  1901. * If the user space waits to inject interrupts, exit as soon as
  1902. * possible
  1903. */
  1904. if (!irqchip_in_kernel(svm->vcpu.kvm) &&
  1905. kvm_run->request_interrupt_window &&
  1906. !kvm_cpu_has_interrupt(&svm->vcpu)) {
  1907. ++svm->vcpu.stat.irq_window_exits;
  1908. kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
  1909. return 0;
  1910. }
  1911. return 1;
  1912. }
  1913. static int (*svm_exit_handlers[])(struct vcpu_svm *svm,
  1914. struct kvm_run *kvm_run) = {
  1915. [SVM_EXIT_READ_CR0] = emulate_on_interception,
  1916. [SVM_EXIT_READ_CR3] = emulate_on_interception,
  1917. [SVM_EXIT_READ_CR4] = emulate_on_interception,
  1918. [SVM_EXIT_READ_CR8] = emulate_on_interception,
  1919. /* for now: */
  1920. [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
  1921. [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
  1922. [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
  1923. [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
  1924. [SVM_EXIT_READ_DR0] = emulate_on_interception,
  1925. [SVM_EXIT_READ_DR1] = emulate_on_interception,
  1926. [SVM_EXIT_READ_DR2] = emulate_on_interception,
  1927. [SVM_EXIT_READ_DR3] = emulate_on_interception,
  1928. [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
  1929. [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
  1930. [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
  1931. [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
  1932. [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
  1933. [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
  1934. [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
  1935. [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
  1936. [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
  1937. [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
  1938. [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
  1939. [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
  1940. [SVM_EXIT_INTR] = intr_interception,
  1941. [SVM_EXIT_NMI] = nmi_interception,
  1942. [SVM_EXIT_SMI] = nop_on_interception,
  1943. [SVM_EXIT_INIT] = nop_on_interception,
  1944. [SVM_EXIT_VINTR] = interrupt_window_interception,
  1945. /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */
  1946. [SVM_EXIT_CPUID] = cpuid_interception,
  1947. [SVM_EXIT_IRET] = iret_interception,
  1948. [SVM_EXIT_INVD] = emulate_on_interception,
  1949. [SVM_EXIT_HLT] = halt_interception,
  1950. [SVM_EXIT_INVLPG] = invlpg_interception,
  1951. [SVM_EXIT_INVLPGA] = invlpga_interception,
  1952. [SVM_EXIT_IOIO] = io_interception,
  1953. [SVM_EXIT_MSR] = msr_interception,
  1954. [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
  1955. [SVM_EXIT_SHUTDOWN] = shutdown_interception,
  1956. [SVM_EXIT_VMRUN] = vmrun_interception,
  1957. [SVM_EXIT_VMMCALL] = vmmcall_interception,
  1958. [SVM_EXIT_VMLOAD] = vmload_interception,
  1959. [SVM_EXIT_VMSAVE] = vmsave_interception,
  1960. [SVM_EXIT_STGI] = stgi_interception,
  1961. [SVM_EXIT_CLGI] = clgi_interception,
  1962. [SVM_EXIT_SKINIT] = invalid_op_interception,
  1963. [SVM_EXIT_WBINVD] = emulate_on_interception,
  1964. [SVM_EXIT_MONITOR] = invalid_op_interception,
  1965. [SVM_EXIT_MWAIT] = invalid_op_interception,
  1966. [SVM_EXIT_NPF] = pf_interception,
  1967. };
  1968. static int handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
  1969. {
  1970. struct vcpu_svm *svm = to_svm(vcpu);
  1971. u32 exit_code = svm->vmcb->control.exit_code;
  1972. trace_kvm_exit(exit_code, svm->vmcb->save.rip);
  1973. if (is_nested(svm)) {
  1974. nsvm_printk("nested handle_exit: 0x%x | 0x%lx | 0x%lx | 0x%lx\n",
  1975. exit_code, svm->vmcb->control.exit_info_1,
  1976. svm->vmcb->control.exit_info_2, svm->vmcb->save.rip);
  1977. if (nested_svm_exit_handled(svm, true))
  1978. return 1;
  1979. }
  1980. svm_complete_interrupts(svm);
  1981. if (npt_enabled) {
  1982. int mmu_reload = 0;
  1983. if ((vcpu->arch.cr0 ^ svm->vmcb->save.cr0) & X86_CR0_PG) {
  1984. svm_set_cr0(vcpu, svm->vmcb->save.cr0);
  1985. mmu_reload = 1;
  1986. }
  1987. vcpu->arch.cr0 = svm->vmcb->save.cr0;
  1988. vcpu->arch.cr3 = svm->vmcb->save.cr3;
  1989. if (mmu_reload) {
  1990. kvm_mmu_reset_context(vcpu);
  1991. kvm_mmu_load(vcpu);
  1992. }
  1993. }
  1994. if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
  1995. kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  1996. kvm_run->fail_entry.hardware_entry_failure_reason
  1997. = svm->vmcb->control.exit_code;
  1998. return 0;
  1999. }
  2000. if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
  2001. exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
  2002. exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH)
  2003. printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
  2004. "exit_code 0x%x\n",
  2005. __func__, svm->vmcb->control.exit_int_info,
  2006. exit_code);
  2007. if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
  2008. || !svm_exit_handlers[exit_code]) {
  2009. kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
  2010. kvm_run->hw.hardware_exit_reason = exit_code;
  2011. return 0;
  2012. }
  2013. return svm_exit_handlers[exit_code](svm, kvm_run);
  2014. }
  2015. static void reload_tss(struct kvm_vcpu *vcpu)
  2016. {
  2017. int cpu = raw_smp_processor_id();
  2018. struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
  2019. svm_data->tss_desc->type = 9; /* available 32/64-bit TSS */
  2020. load_TR_desc();
  2021. }
  2022. static void pre_svm_run(struct vcpu_svm *svm)
  2023. {
  2024. int cpu = raw_smp_processor_id();
  2025. struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu);
  2026. svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
  2027. /* FIXME: handle wraparound of asid_generation */
  2028. if (svm->asid_generation != svm_data->asid_generation)
  2029. new_asid(svm, svm_data);
  2030. }
  2031. static void svm_inject_nmi(struct kvm_vcpu *vcpu)
  2032. {
  2033. struct vcpu_svm *svm = to_svm(vcpu);
  2034. svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
  2035. vcpu->arch.hflags |= HF_NMI_MASK;
  2036. svm->vmcb->control.intercept |= (1UL << INTERCEPT_IRET);
  2037. ++vcpu->stat.nmi_injections;
  2038. }
  2039. static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
  2040. {
  2041. struct vmcb_control_area *control;
  2042. trace_kvm_inj_virq(irq);
  2043. ++svm->vcpu.stat.irq_injections;
  2044. control = &svm->vmcb->control;
  2045. control->int_vector = irq;
  2046. control->int_ctl &= ~V_INTR_PRIO_MASK;
  2047. control->int_ctl |= V_IRQ_MASK |
  2048. ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
  2049. }
  2050. static void svm_set_irq(struct kvm_vcpu *vcpu)
  2051. {
  2052. struct vcpu_svm *svm = to_svm(vcpu);
  2053. BUG_ON(!(gif_set(svm)));
  2054. svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
  2055. SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
  2056. }
  2057. static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
  2058. {
  2059. struct vcpu_svm *svm = to_svm(vcpu);
  2060. if (irr == -1)
  2061. return;
  2062. if (tpr >= irr)
  2063. svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
  2064. }
  2065. static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
  2066. {
  2067. struct vcpu_svm *svm = to_svm(vcpu);
  2068. struct vmcb *vmcb = svm->vmcb;
  2069. return !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
  2070. !(svm->vcpu.arch.hflags & HF_NMI_MASK);
  2071. }
  2072. static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
  2073. {
  2074. struct vcpu_svm *svm = to_svm(vcpu);
  2075. struct vmcb *vmcb = svm->vmcb;
  2076. return (vmcb->save.rflags & X86_EFLAGS_IF) &&
  2077. !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
  2078. gif_set(svm) &&
  2079. !is_nested(svm);
  2080. }
  2081. static void enable_irq_window(struct kvm_vcpu *vcpu)
  2082. {
  2083. struct vcpu_svm *svm = to_svm(vcpu);
  2084. nsvm_printk("Trying to open IRQ window\n");
  2085. nested_svm_intr(svm);
  2086. /* In case GIF=0 we can't rely on the CPU to tell us when
  2087. * GIF becomes 1, because that's a separate STGI/VMRUN intercept.
  2088. * The next time we get that intercept, this function will be
  2089. * called again though and we'll get the vintr intercept. */
  2090. if (gif_set(svm)) {
  2091. svm_set_vintr(svm);
  2092. svm_inject_irq(svm, 0x0);
  2093. }
  2094. }
  2095. static void enable_nmi_window(struct kvm_vcpu *vcpu)
  2096. {
  2097. struct vcpu_svm *svm = to_svm(vcpu);
  2098. if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
  2099. == HF_NMI_MASK)
  2100. return; /* IRET will cause a vm exit */
  2101. /* Something prevents NMI from been injected. Single step over
  2102. possible problem (IRET or exception injection or interrupt
  2103. shadow) */
  2104. vcpu->arch.singlestep = true;
  2105. svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
  2106. update_db_intercept(vcpu);
  2107. }
  2108. static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
  2109. {
  2110. return 0;
  2111. }
  2112. static void svm_flush_tlb(struct kvm_vcpu *vcpu)
  2113. {
  2114. force_new_asid(vcpu);
  2115. }
  2116. static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
  2117. {
  2118. }
  2119. static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
  2120. {
  2121. struct vcpu_svm *svm = to_svm(vcpu);
  2122. if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) {
  2123. int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
  2124. kvm_set_cr8(vcpu, cr8);
  2125. }
  2126. }
  2127. static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
  2128. {
  2129. struct vcpu_svm *svm = to_svm(vcpu);
  2130. u64 cr8;
  2131. cr8 = kvm_get_cr8(vcpu);
  2132. svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
  2133. svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
  2134. }
  2135. static void svm_complete_interrupts(struct vcpu_svm *svm)
  2136. {
  2137. u8 vector;
  2138. int type;
  2139. u32 exitintinfo = svm->vmcb->control.exit_int_info;
  2140. if (svm->vcpu.arch.hflags & HF_IRET_MASK)
  2141. svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
  2142. svm->vcpu.arch.nmi_injected = false;
  2143. kvm_clear_exception_queue(&svm->vcpu);
  2144. kvm_clear_interrupt_queue(&svm->vcpu);
  2145. if (!(exitintinfo & SVM_EXITINTINFO_VALID))
  2146. return;
  2147. vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
  2148. type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
  2149. switch (type) {
  2150. case SVM_EXITINTINFO_TYPE_NMI:
  2151. svm->vcpu.arch.nmi_injected = true;
  2152. break;
  2153. case SVM_EXITINTINFO_TYPE_EXEPT:
  2154. /* In case of software exception do not reinject an exception
  2155. vector, but re-execute and instruction instead */
  2156. if (is_nested(svm))
  2157. break;
  2158. if (kvm_exception_is_soft(vector))
  2159. break;
  2160. if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
  2161. u32 err = svm->vmcb->control.exit_int_info_err;
  2162. kvm_queue_exception_e(&svm->vcpu, vector, err);
  2163. } else
  2164. kvm_queue_exception(&svm->vcpu, vector);
  2165. break;
  2166. case SVM_EXITINTINFO_TYPE_INTR:
  2167. kvm_queue_interrupt(&svm->vcpu, vector, false);
  2168. break;
  2169. default:
  2170. break;
  2171. }
  2172. }
  2173. #ifdef CONFIG_X86_64
  2174. #define R "r"
  2175. #else
  2176. #define R "e"
  2177. #endif
  2178. static void svm_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2179. {
  2180. struct vcpu_svm *svm = to_svm(vcpu);
  2181. u16 fs_selector;
  2182. u16 gs_selector;
  2183. u16 ldt_selector;
  2184. svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
  2185. svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
  2186. svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
  2187. pre_svm_run(svm);
  2188. sync_lapic_to_cr8(vcpu);
  2189. save_host_msrs(vcpu);
  2190. fs_selector = kvm_read_fs();
  2191. gs_selector = kvm_read_gs();
  2192. ldt_selector = kvm_read_ldt();
  2193. if (!is_nested(svm))
  2194. svm->vmcb->save.cr2 = vcpu->arch.cr2;
  2195. /* required for live migration with NPT */
  2196. if (npt_enabled)
  2197. svm->vmcb->save.cr3 = vcpu->arch.cr3;
  2198. clgi();
  2199. local_irq_enable();
  2200. asm volatile (
  2201. "push %%"R"bp; \n\t"
  2202. "mov %c[rbx](%[svm]), %%"R"bx \n\t"
  2203. "mov %c[rcx](%[svm]), %%"R"cx \n\t"
  2204. "mov %c[rdx](%[svm]), %%"R"dx \n\t"
  2205. "mov %c[rsi](%[svm]), %%"R"si \n\t"
  2206. "mov %c[rdi](%[svm]), %%"R"di \n\t"
  2207. "mov %c[rbp](%[svm]), %%"R"bp \n\t"
  2208. #ifdef CONFIG_X86_64
  2209. "mov %c[r8](%[svm]), %%r8 \n\t"
  2210. "mov %c[r9](%[svm]), %%r9 \n\t"
  2211. "mov %c[r10](%[svm]), %%r10 \n\t"
  2212. "mov %c[r11](%[svm]), %%r11 \n\t"
  2213. "mov %c[r12](%[svm]), %%r12 \n\t"
  2214. "mov %c[r13](%[svm]), %%r13 \n\t"
  2215. "mov %c[r14](%[svm]), %%r14 \n\t"
  2216. "mov %c[r15](%[svm]), %%r15 \n\t"
  2217. #endif
  2218. /* Enter guest mode */
  2219. "push %%"R"ax \n\t"
  2220. "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
  2221. __ex(SVM_VMLOAD) "\n\t"
  2222. __ex(SVM_VMRUN) "\n\t"
  2223. __ex(SVM_VMSAVE) "\n\t"
  2224. "pop %%"R"ax \n\t"
  2225. /* Save guest registers, load host registers */
  2226. "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
  2227. "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
  2228. "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
  2229. "mov %%"R"si, %c[rsi](%[svm]) \n\t"
  2230. "mov %%"R"di, %c[rdi](%[svm]) \n\t"
  2231. "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
  2232. #ifdef CONFIG_X86_64
  2233. "mov %%r8, %c[r8](%[svm]) \n\t"
  2234. "mov %%r9, %c[r9](%[svm]) \n\t"
  2235. "mov %%r10, %c[r10](%[svm]) \n\t"
  2236. "mov %%r11, %c[r11](%[svm]) \n\t"
  2237. "mov %%r12, %c[r12](%[svm]) \n\t"
  2238. "mov %%r13, %c[r13](%[svm]) \n\t"
  2239. "mov %%r14, %c[r14](%[svm]) \n\t"
  2240. "mov %%r15, %c[r15](%[svm]) \n\t"
  2241. #endif
  2242. "pop %%"R"bp"
  2243. :
  2244. : [svm]"a"(svm),
  2245. [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
  2246. [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
  2247. [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
  2248. [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
  2249. [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
  2250. [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
  2251. [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
  2252. #ifdef CONFIG_X86_64
  2253. , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
  2254. [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
  2255. [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
  2256. [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
  2257. [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
  2258. [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
  2259. [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
  2260. [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
  2261. #endif
  2262. : "cc", "memory"
  2263. , R"bx", R"cx", R"dx", R"si", R"di"
  2264. #ifdef CONFIG_X86_64
  2265. , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
  2266. #endif
  2267. );
  2268. vcpu->arch.cr2 = svm->vmcb->save.cr2;
  2269. vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
  2270. vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
  2271. vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
  2272. kvm_load_fs(fs_selector);
  2273. kvm_load_gs(gs_selector);
  2274. kvm_load_ldt(ldt_selector);
  2275. load_host_msrs(vcpu);
  2276. reload_tss(vcpu);
  2277. local_irq_disable();
  2278. stgi();
  2279. sync_cr8_to_lapic(vcpu);
  2280. svm->next_rip = 0;
  2281. if (npt_enabled) {
  2282. vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
  2283. vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
  2284. }
  2285. }
  2286. #undef R
  2287. static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
  2288. {
  2289. struct vcpu_svm *svm = to_svm(vcpu);
  2290. if (npt_enabled) {
  2291. svm->vmcb->control.nested_cr3 = root;
  2292. force_new_asid(vcpu);
  2293. return;
  2294. }
  2295. svm->vmcb->save.cr3 = root;
  2296. force_new_asid(vcpu);
  2297. if (vcpu->fpu_active) {
  2298. svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR);
  2299. svm->vmcb->save.cr0 |= X86_CR0_TS;
  2300. vcpu->fpu_active = 0;
  2301. }
  2302. }
  2303. static int is_disabled(void)
  2304. {
  2305. u64 vm_cr;
  2306. rdmsrl(MSR_VM_CR, vm_cr);
  2307. if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
  2308. return 1;
  2309. return 0;
  2310. }
  2311. static void
  2312. svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
  2313. {
  2314. /*
  2315. * Patch in the VMMCALL instruction:
  2316. */
  2317. hypercall[0] = 0x0f;
  2318. hypercall[1] = 0x01;
  2319. hypercall[2] = 0xd9;
  2320. }
  2321. static void svm_check_processor_compat(void *rtn)
  2322. {
  2323. *(int *)rtn = 0;
  2324. }
  2325. static bool svm_cpu_has_accelerated_tpr(void)
  2326. {
  2327. return false;
  2328. }
  2329. static int get_npt_level(void)
  2330. {
  2331. #ifdef CONFIG_X86_64
  2332. return PT64_ROOT_LEVEL;
  2333. #else
  2334. return PT32E_ROOT_LEVEL;
  2335. #endif
  2336. }
  2337. static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
  2338. {
  2339. return 0;
  2340. }
  2341. static const struct trace_print_flags svm_exit_reasons_str[] = {
  2342. { SVM_EXIT_READ_CR0, "read_cr0" },
  2343. { SVM_EXIT_READ_CR3, "read_cr3" },
  2344. { SVM_EXIT_READ_CR4, "read_cr4" },
  2345. { SVM_EXIT_READ_CR8, "read_cr8" },
  2346. { SVM_EXIT_WRITE_CR0, "write_cr0" },
  2347. { SVM_EXIT_WRITE_CR3, "write_cr3" },
  2348. { SVM_EXIT_WRITE_CR4, "write_cr4" },
  2349. { SVM_EXIT_WRITE_CR8, "write_cr8" },
  2350. { SVM_EXIT_READ_DR0, "read_dr0" },
  2351. { SVM_EXIT_READ_DR1, "read_dr1" },
  2352. { SVM_EXIT_READ_DR2, "read_dr2" },
  2353. { SVM_EXIT_READ_DR3, "read_dr3" },
  2354. { SVM_EXIT_WRITE_DR0, "write_dr0" },
  2355. { SVM_EXIT_WRITE_DR1, "write_dr1" },
  2356. { SVM_EXIT_WRITE_DR2, "write_dr2" },
  2357. { SVM_EXIT_WRITE_DR3, "write_dr3" },
  2358. { SVM_EXIT_WRITE_DR5, "write_dr5" },
  2359. { SVM_EXIT_WRITE_DR7, "write_dr7" },
  2360. { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
  2361. { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
  2362. { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
  2363. { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
  2364. { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
  2365. { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
  2366. { SVM_EXIT_INTR, "interrupt" },
  2367. { SVM_EXIT_NMI, "nmi" },
  2368. { SVM_EXIT_SMI, "smi" },
  2369. { SVM_EXIT_INIT, "init" },
  2370. { SVM_EXIT_VINTR, "vintr" },
  2371. { SVM_EXIT_CPUID, "cpuid" },
  2372. { SVM_EXIT_INVD, "invd" },
  2373. { SVM_EXIT_HLT, "hlt" },
  2374. { SVM_EXIT_INVLPG, "invlpg" },
  2375. { SVM_EXIT_INVLPGA, "invlpga" },
  2376. { SVM_EXIT_IOIO, "io" },
  2377. { SVM_EXIT_MSR, "msr" },
  2378. { SVM_EXIT_TASK_SWITCH, "task_switch" },
  2379. { SVM_EXIT_SHUTDOWN, "shutdown" },
  2380. { SVM_EXIT_VMRUN, "vmrun" },
  2381. { SVM_EXIT_VMMCALL, "hypercall" },
  2382. { SVM_EXIT_VMLOAD, "vmload" },
  2383. { SVM_EXIT_VMSAVE, "vmsave" },
  2384. { SVM_EXIT_STGI, "stgi" },
  2385. { SVM_EXIT_CLGI, "clgi" },
  2386. { SVM_EXIT_SKINIT, "skinit" },
  2387. { SVM_EXIT_WBINVD, "wbinvd" },
  2388. { SVM_EXIT_MONITOR, "monitor" },
  2389. { SVM_EXIT_MWAIT, "mwait" },
  2390. { SVM_EXIT_NPF, "npf" },
  2391. { -1, NULL }
  2392. };
  2393. static bool svm_gb_page_enable(void)
  2394. {
  2395. return true;
  2396. }
  2397. static struct kvm_x86_ops svm_x86_ops = {
  2398. .cpu_has_kvm_support = has_svm,
  2399. .disabled_by_bios = is_disabled,
  2400. .hardware_setup = svm_hardware_setup,
  2401. .hardware_unsetup = svm_hardware_unsetup,
  2402. .check_processor_compatibility = svm_check_processor_compat,
  2403. .hardware_enable = svm_hardware_enable,
  2404. .hardware_disable = svm_hardware_disable,
  2405. .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
  2406. .vcpu_create = svm_create_vcpu,
  2407. .vcpu_free = svm_free_vcpu,
  2408. .vcpu_reset = svm_vcpu_reset,
  2409. .prepare_guest_switch = svm_prepare_guest_switch,
  2410. .vcpu_load = svm_vcpu_load,
  2411. .vcpu_put = svm_vcpu_put,
  2412. .set_guest_debug = svm_guest_debug,
  2413. .get_msr = svm_get_msr,
  2414. .set_msr = svm_set_msr,
  2415. .get_segment_base = svm_get_segment_base,
  2416. .get_segment = svm_get_segment,
  2417. .set_segment = svm_set_segment,
  2418. .get_cpl = svm_get_cpl,
  2419. .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
  2420. .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
  2421. .set_cr0 = svm_set_cr0,
  2422. .set_cr3 = svm_set_cr3,
  2423. .set_cr4 = svm_set_cr4,
  2424. .set_efer = svm_set_efer,
  2425. .get_idt = svm_get_idt,
  2426. .set_idt = svm_set_idt,
  2427. .get_gdt = svm_get_gdt,
  2428. .set_gdt = svm_set_gdt,
  2429. .get_dr = svm_get_dr,
  2430. .set_dr = svm_set_dr,
  2431. .cache_reg = svm_cache_reg,
  2432. .get_rflags = svm_get_rflags,
  2433. .set_rflags = svm_set_rflags,
  2434. .tlb_flush = svm_flush_tlb,
  2435. .run = svm_vcpu_run,
  2436. .handle_exit = handle_exit,
  2437. .skip_emulated_instruction = skip_emulated_instruction,
  2438. .set_interrupt_shadow = svm_set_interrupt_shadow,
  2439. .get_interrupt_shadow = svm_get_interrupt_shadow,
  2440. .patch_hypercall = svm_patch_hypercall,
  2441. .set_irq = svm_set_irq,
  2442. .set_nmi = svm_inject_nmi,
  2443. .queue_exception = svm_queue_exception,
  2444. .interrupt_allowed = svm_interrupt_allowed,
  2445. .nmi_allowed = svm_nmi_allowed,
  2446. .enable_nmi_window = enable_nmi_window,
  2447. .enable_irq_window = enable_irq_window,
  2448. .update_cr8_intercept = update_cr8_intercept,
  2449. .set_tss_addr = svm_set_tss_addr,
  2450. .get_tdp_level = get_npt_level,
  2451. .get_mt_mask = svm_get_mt_mask,
  2452. .exit_reasons_str = svm_exit_reasons_str,
  2453. .gb_page_enable = svm_gb_page_enable,
  2454. };
  2455. static int __init svm_init(void)
  2456. {
  2457. return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
  2458. THIS_MODULE);
  2459. }
  2460. static void __exit svm_exit(void)
  2461. {
  2462. kvm_exit();
  2463. }
  2464. module_init(svm_init)
  2465. module_exit(svm_exit)