svm.c 68 KB

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