svm.c 70 KB

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