svm.c 67 KB

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