x86.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * derived from drivers/kvm/kvm_main.c
  5. *
  6. * Copyright (C) 2006 Qumranet, Inc.
  7. *
  8. * Authors:
  9. * Avi Kivity <avi@qumranet.com>
  10. * Yaniv Kamay <yaniv@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 "kvm.h"
  17. #include "x86.h"
  18. #include "x86_emulate.h"
  19. #include "segment_descriptor.h"
  20. #include "irq.h"
  21. #include "mmu.h"
  22. #include <linux/kvm.h>
  23. #include <linux/fs.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/module.h>
  26. #include <linux/mman.h>
  27. #include <linux/highmem.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/msr.h>
  30. #define MAX_IO_MSRS 256
  31. #define CR0_RESERVED_BITS \
  32. (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
  33. | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
  34. | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
  35. #define CR4_RESERVED_BITS \
  36. (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
  37. | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
  38. | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
  39. | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
  40. #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
  41. #define EFER_RESERVED_BITS 0xfffffffffffff2fe
  42. #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
  43. #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
  44. struct kvm_x86_ops *kvm_x86_ops;
  45. struct kvm_stats_debugfs_item debugfs_entries[] = {
  46. { "pf_fixed", VCPU_STAT(pf_fixed) },
  47. { "pf_guest", VCPU_STAT(pf_guest) },
  48. { "tlb_flush", VCPU_STAT(tlb_flush) },
  49. { "invlpg", VCPU_STAT(invlpg) },
  50. { "exits", VCPU_STAT(exits) },
  51. { "io_exits", VCPU_STAT(io_exits) },
  52. { "mmio_exits", VCPU_STAT(mmio_exits) },
  53. { "signal_exits", VCPU_STAT(signal_exits) },
  54. { "irq_window", VCPU_STAT(irq_window_exits) },
  55. { "halt_exits", VCPU_STAT(halt_exits) },
  56. { "halt_wakeup", VCPU_STAT(halt_wakeup) },
  57. { "request_irq", VCPU_STAT(request_irq_exits) },
  58. { "irq_exits", VCPU_STAT(irq_exits) },
  59. { "host_state_reload", VCPU_STAT(host_state_reload) },
  60. { "efer_reload", VCPU_STAT(efer_reload) },
  61. { "fpu_reload", VCPU_STAT(fpu_reload) },
  62. { "insn_emulation", VCPU_STAT(insn_emulation) },
  63. { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
  64. { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
  65. { "mmu_pte_write", VM_STAT(mmu_pte_write) },
  66. { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
  67. { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
  68. { "mmu_flooded", VM_STAT(mmu_flooded) },
  69. { "mmu_recycled", VM_STAT(mmu_recycled) },
  70. { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
  71. { NULL }
  72. };
  73. unsigned long segment_base(u16 selector)
  74. {
  75. struct descriptor_table gdt;
  76. struct segment_descriptor *d;
  77. unsigned long table_base;
  78. unsigned long v;
  79. if (selector == 0)
  80. return 0;
  81. asm("sgdt %0" : "=m"(gdt));
  82. table_base = gdt.base;
  83. if (selector & 4) { /* from ldt */
  84. u16 ldt_selector;
  85. asm("sldt %0" : "=g"(ldt_selector));
  86. table_base = segment_base(ldt_selector);
  87. }
  88. d = (struct segment_descriptor *)(table_base + (selector & ~7));
  89. v = d->base_low | ((unsigned long)d->base_mid << 16) |
  90. ((unsigned long)d->base_high << 24);
  91. #ifdef CONFIG_X86_64
  92. if (d->system == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
  93. v |= ((unsigned long) \
  94. ((struct segment_descriptor_64 *)d)->base_higher) << 32;
  95. #endif
  96. return v;
  97. }
  98. EXPORT_SYMBOL_GPL(segment_base);
  99. u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
  100. {
  101. if (irqchip_in_kernel(vcpu->kvm))
  102. return vcpu->arch.apic_base;
  103. else
  104. return vcpu->arch.apic_base;
  105. }
  106. EXPORT_SYMBOL_GPL(kvm_get_apic_base);
  107. void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
  108. {
  109. /* TODO: reserve bits check */
  110. if (irqchip_in_kernel(vcpu->kvm))
  111. kvm_lapic_set_base(vcpu, data);
  112. else
  113. vcpu->arch.apic_base = data;
  114. }
  115. EXPORT_SYMBOL_GPL(kvm_set_apic_base);
  116. void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
  117. {
  118. WARN_ON(vcpu->arch.exception.pending);
  119. vcpu->arch.exception.pending = true;
  120. vcpu->arch.exception.has_error_code = false;
  121. vcpu->arch.exception.nr = nr;
  122. }
  123. EXPORT_SYMBOL_GPL(kvm_queue_exception);
  124. void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
  125. u32 error_code)
  126. {
  127. ++vcpu->stat.pf_guest;
  128. if (vcpu->arch.exception.pending && vcpu->arch.exception.nr == PF_VECTOR) {
  129. printk(KERN_DEBUG "kvm: inject_page_fault:"
  130. " double fault 0x%lx\n", addr);
  131. vcpu->arch.exception.nr = DF_VECTOR;
  132. vcpu->arch.exception.error_code = 0;
  133. return;
  134. }
  135. vcpu->arch.cr2 = addr;
  136. kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
  137. }
  138. void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
  139. {
  140. WARN_ON(vcpu->arch.exception.pending);
  141. vcpu->arch.exception.pending = true;
  142. vcpu->arch.exception.has_error_code = true;
  143. vcpu->arch.exception.nr = nr;
  144. vcpu->arch.exception.error_code = error_code;
  145. }
  146. EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
  147. static void __queue_exception(struct kvm_vcpu *vcpu)
  148. {
  149. kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
  150. vcpu->arch.exception.has_error_code,
  151. vcpu->arch.exception.error_code);
  152. }
  153. /*
  154. * Load the pae pdptrs. Return true is they are all valid.
  155. */
  156. int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
  157. {
  158. gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
  159. unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
  160. int i;
  161. int ret;
  162. u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
  163. mutex_lock(&vcpu->kvm->lock);
  164. ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
  165. offset * sizeof(u64), sizeof(pdpte));
  166. if (ret < 0) {
  167. ret = 0;
  168. goto out;
  169. }
  170. for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
  171. if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
  172. ret = 0;
  173. goto out;
  174. }
  175. }
  176. ret = 1;
  177. memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
  178. out:
  179. mutex_unlock(&vcpu->kvm->lock);
  180. return ret;
  181. }
  182. static bool pdptrs_changed(struct kvm_vcpu *vcpu)
  183. {
  184. u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
  185. bool changed = true;
  186. int r;
  187. if (is_long_mode(vcpu) || !is_pae(vcpu))
  188. return false;
  189. mutex_lock(&vcpu->kvm->lock);
  190. r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
  191. if (r < 0)
  192. goto out;
  193. changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
  194. out:
  195. mutex_unlock(&vcpu->kvm->lock);
  196. return changed;
  197. }
  198. void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  199. {
  200. if (cr0 & CR0_RESERVED_BITS) {
  201. printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
  202. cr0, vcpu->arch.cr0);
  203. kvm_inject_gp(vcpu, 0);
  204. return;
  205. }
  206. if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
  207. printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
  208. kvm_inject_gp(vcpu, 0);
  209. return;
  210. }
  211. if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
  212. printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
  213. "and a clear PE flag\n");
  214. kvm_inject_gp(vcpu, 0);
  215. return;
  216. }
  217. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  218. #ifdef CONFIG_X86_64
  219. if ((vcpu->arch.shadow_efer & EFER_LME)) {
  220. int cs_db, cs_l;
  221. if (!is_pae(vcpu)) {
  222. printk(KERN_DEBUG "set_cr0: #GP, start paging "
  223. "in long mode while PAE is disabled\n");
  224. kvm_inject_gp(vcpu, 0);
  225. return;
  226. }
  227. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  228. if (cs_l) {
  229. printk(KERN_DEBUG "set_cr0: #GP, start paging "
  230. "in long mode while CS.L == 1\n");
  231. kvm_inject_gp(vcpu, 0);
  232. return;
  233. }
  234. } else
  235. #endif
  236. if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
  237. printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
  238. "reserved bits\n");
  239. kvm_inject_gp(vcpu, 0);
  240. return;
  241. }
  242. }
  243. kvm_x86_ops->set_cr0(vcpu, cr0);
  244. vcpu->arch.cr0 = cr0;
  245. mutex_lock(&vcpu->kvm->lock);
  246. kvm_mmu_reset_context(vcpu);
  247. mutex_unlock(&vcpu->kvm->lock);
  248. return;
  249. }
  250. EXPORT_SYMBOL_GPL(set_cr0);
  251. void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
  252. {
  253. set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f));
  254. }
  255. EXPORT_SYMBOL_GPL(lmsw);
  256. void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  257. {
  258. if (cr4 & CR4_RESERVED_BITS) {
  259. printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
  260. kvm_inject_gp(vcpu, 0);
  261. return;
  262. }
  263. if (is_long_mode(vcpu)) {
  264. if (!(cr4 & X86_CR4_PAE)) {
  265. printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
  266. "in long mode\n");
  267. kvm_inject_gp(vcpu, 0);
  268. return;
  269. }
  270. } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
  271. && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
  272. printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
  273. kvm_inject_gp(vcpu, 0);
  274. return;
  275. }
  276. if (cr4 & X86_CR4_VMXE) {
  277. printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
  278. kvm_inject_gp(vcpu, 0);
  279. return;
  280. }
  281. kvm_x86_ops->set_cr4(vcpu, cr4);
  282. vcpu->arch.cr4 = cr4;
  283. mutex_lock(&vcpu->kvm->lock);
  284. kvm_mmu_reset_context(vcpu);
  285. mutex_unlock(&vcpu->kvm->lock);
  286. }
  287. EXPORT_SYMBOL_GPL(set_cr4);
  288. void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  289. {
  290. if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
  291. kvm_mmu_flush_tlb(vcpu);
  292. return;
  293. }
  294. if (is_long_mode(vcpu)) {
  295. if (cr3 & CR3_L_MODE_RESERVED_BITS) {
  296. printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
  297. kvm_inject_gp(vcpu, 0);
  298. return;
  299. }
  300. } else {
  301. if (is_pae(vcpu)) {
  302. if (cr3 & CR3_PAE_RESERVED_BITS) {
  303. printk(KERN_DEBUG
  304. "set_cr3: #GP, reserved bits\n");
  305. kvm_inject_gp(vcpu, 0);
  306. return;
  307. }
  308. if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
  309. printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
  310. "reserved bits\n");
  311. kvm_inject_gp(vcpu, 0);
  312. return;
  313. }
  314. }
  315. /*
  316. * We don't check reserved bits in nonpae mode, because
  317. * this isn't enforced, and VMware depends on this.
  318. */
  319. }
  320. mutex_lock(&vcpu->kvm->lock);
  321. /*
  322. * Does the new cr3 value map to physical memory? (Note, we
  323. * catch an invalid cr3 even in real-mode, because it would
  324. * cause trouble later on when we turn on paging anyway.)
  325. *
  326. * A real CPU would silently accept an invalid cr3 and would
  327. * attempt to use it - with largely undefined (and often hard
  328. * to debug) behavior on the guest side.
  329. */
  330. if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
  331. kvm_inject_gp(vcpu, 0);
  332. else {
  333. vcpu->arch.cr3 = cr3;
  334. vcpu->arch.mmu.new_cr3(vcpu);
  335. }
  336. mutex_unlock(&vcpu->kvm->lock);
  337. }
  338. EXPORT_SYMBOL_GPL(set_cr3);
  339. void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
  340. {
  341. if (cr8 & CR8_RESERVED_BITS) {
  342. printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
  343. kvm_inject_gp(vcpu, 0);
  344. return;
  345. }
  346. if (irqchip_in_kernel(vcpu->kvm))
  347. kvm_lapic_set_tpr(vcpu, cr8);
  348. else
  349. vcpu->arch.cr8 = cr8;
  350. }
  351. EXPORT_SYMBOL_GPL(set_cr8);
  352. unsigned long get_cr8(struct kvm_vcpu *vcpu)
  353. {
  354. if (irqchip_in_kernel(vcpu->kvm))
  355. return kvm_lapic_get_cr8(vcpu);
  356. else
  357. return vcpu->arch.cr8;
  358. }
  359. EXPORT_SYMBOL_GPL(get_cr8);
  360. /*
  361. * List of msr numbers which we expose to userspace through KVM_GET_MSRS
  362. * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
  363. *
  364. * This list is modified at module load time to reflect the
  365. * capabilities of the host cpu.
  366. */
  367. static u32 msrs_to_save[] = {
  368. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  369. MSR_K6_STAR,
  370. #ifdef CONFIG_X86_64
  371. MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
  372. #endif
  373. MSR_IA32_TIME_STAMP_COUNTER,
  374. };
  375. static unsigned num_msrs_to_save;
  376. static u32 emulated_msrs[] = {
  377. MSR_IA32_MISC_ENABLE,
  378. };
  379. #ifdef CONFIG_X86_64
  380. static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
  381. {
  382. if (efer & EFER_RESERVED_BITS) {
  383. printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
  384. efer);
  385. kvm_inject_gp(vcpu, 0);
  386. return;
  387. }
  388. if (is_paging(vcpu)
  389. && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) {
  390. printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
  391. kvm_inject_gp(vcpu, 0);
  392. return;
  393. }
  394. kvm_x86_ops->set_efer(vcpu, efer);
  395. efer &= ~EFER_LMA;
  396. efer |= vcpu->arch.shadow_efer & EFER_LMA;
  397. vcpu->arch.shadow_efer = efer;
  398. }
  399. #endif
  400. /*
  401. * Writes msr value into into the appropriate "register".
  402. * Returns 0 on success, non-0 otherwise.
  403. * Assumes vcpu_load() was already called.
  404. */
  405. int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  406. {
  407. return kvm_x86_ops->set_msr(vcpu, msr_index, data);
  408. }
  409. /*
  410. * Adapt set_msr() to msr_io()'s calling convention
  411. */
  412. static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
  413. {
  414. return kvm_set_msr(vcpu, index, *data);
  415. }
  416. int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  417. {
  418. switch (msr) {
  419. #ifdef CONFIG_X86_64
  420. case MSR_EFER:
  421. set_efer(vcpu, data);
  422. break;
  423. #endif
  424. case MSR_IA32_MC0_STATUS:
  425. pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
  426. __FUNCTION__, data);
  427. break;
  428. case MSR_IA32_MCG_STATUS:
  429. pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
  430. __FUNCTION__, data);
  431. break;
  432. case MSR_IA32_UCODE_REV:
  433. case MSR_IA32_UCODE_WRITE:
  434. case 0x200 ... 0x2ff: /* MTRRs */
  435. break;
  436. case MSR_IA32_APICBASE:
  437. kvm_set_apic_base(vcpu, data);
  438. break;
  439. case MSR_IA32_MISC_ENABLE:
  440. vcpu->arch.ia32_misc_enable_msr = data;
  441. break;
  442. default:
  443. pr_unimpl(vcpu, "unhandled wrmsr: 0x%x\n", msr);
  444. return 1;
  445. }
  446. return 0;
  447. }
  448. EXPORT_SYMBOL_GPL(kvm_set_msr_common);
  449. /*
  450. * Reads an msr value (of 'msr_index') into 'pdata'.
  451. * Returns 0 on success, non-0 otherwise.
  452. * Assumes vcpu_load() was already called.
  453. */
  454. int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  455. {
  456. return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
  457. }
  458. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  459. {
  460. u64 data;
  461. switch (msr) {
  462. case 0xc0010010: /* SYSCFG */
  463. case 0xc0010015: /* HWCR */
  464. case MSR_IA32_PLATFORM_ID:
  465. case MSR_IA32_P5_MC_ADDR:
  466. case MSR_IA32_P5_MC_TYPE:
  467. case MSR_IA32_MC0_CTL:
  468. case MSR_IA32_MCG_STATUS:
  469. case MSR_IA32_MCG_CAP:
  470. case MSR_IA32_MC0_MISC:
  471. case MSR_IA32_MC0_MISC+4:
  472. case MSR_IA32_MC0_MISC+8:
  473. case MSR_IA32_MC0_MISC+12:
  474. case MSR_IA32_MC0_MISC+16:
  475. case MSR_IA32_UCODE_REV:
  476. case MSR_IA32_PERF_STATUS:
  477. case MSR_IA32_EBL_CR_POWERON:
  478. /* MTRR registers */
  479. case 0xfe:
  480. case 0x200 ... 0x2ff:
  481. data = 0;
  482. break;
  483. case 0xcd: /* fsb frequency */
  484. data = 3;
  485. break;
  486. case MSR_IA32_APICBASE:
  487. data = kvm_get_apic_base(vcpu);
  488. break;
  489. case MSR_IA32_MISC_ENABLE:
  490. data = vcpu->arch.ia32_misc_enable_msr;
  491. break;
  492. #ifdef CONFIG_X86_64
  493. case MSR_EFER:
  494. data = vcpu->arch.shadow_efer;
  495. break;
  496. #endif
  497. default:
  498. pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
  499. return 1;
  500. }
  501. *pdata = data;
  502. return 0;
  503. }
  504. EXPORT_SYMBOL_GPL(kvm_get_msr_common);
  505. /*
  506. * Read or write a bunch of msrs. All parameters are kernel addresses.
  507. *
  508. * @return number of msrs set successfully.
  509. */
  510. static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
  511. struct kvm_msr_entry *entries,
  512. int (*do_msr)(struct kvm_vcpu *vcpu,
  513. unsigned index, u64 *data))
  514. {
  515. int i;
  516. vcpu_load(vcpu);
  517. for (i = 0; i < msrs->nmsrs; ++i)
  518. if (do_msr(vcpu, entries[i].index, &entries[i].data))
  519. break;
  520. vcpu_put(vcpu);
  521. return i;
  522. }
  523. /*
  524. * Read or write a bunch of msrs. Parameters are user addresses.
  525. *
  526. * @return number of msrs set successfully.
  527. */
  528. static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
  529. int (*do_msr)(struct kvm_vcpu *vcpu,
  530. unsigned index, u64 *data),
  531. int writeback)
  532. {
  533. struct kvm_msrs msrs;
  534. struct kvm_msr_entry *entries;
  535. int r, n;
  536. unsigned size;
  537. r = -EFAULT;
  538. if (copy_from_user(&msrs, user_msrs, sizeof msrs))
  539. goto out;
  540. r = -E2BIG;
  541. if (msrs.nmsrs >= MAX_IO_MSRS)
  542. goto out;
  543. r = -ENOMEM;
  544. size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
  545. entries = vmalloc(size);
  546. if (!entries)
  547. goto out;
  548. r = -EFAULT;
  549. if (copy_from_user(entries, user_msrs->entries, size))
  550. goto out_free;
  551. r = n = __msr_io(vcpu, &msrs, entries, do_msr);
  552. if (r < 0)
  553. goto out_free;
  554. r = -EFAULT;
  555. if (writeback && copy_to_user(user_msrs->entries, entries, size))
  556. goto out_free;
  557. r = n;
  558. out_free:
  559. vfree(entries);
  560. out:
  561. return r;
  562. }
  563. /*
  564. * Make sure that a cpu that is being hot-unplugged does not have any vcpus
  565. * cached on it.
  566. */
  567. void decache_vcpus_on_cpu(int cpu)
  568. {
  569. struct kvm *vm;
  570. struct kvm_vcpu *vcpu;
  571. int i;
  572. spin_lock(&kvm_lock);
  573. list_for_each_entry(vm, &vm_list, vm_list)
  574. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  575. vcpu = vm->vcpus[i];
  576. if (!vcpu)
  577. continue;
  578. /*
  579. * If the vcpu is locked, then it is running on some
  580. * other cpu and therefore it is not cached on the
  581. * cpu in question.
  582. *
  583. * If it's not locked, check the last cpu it executed
  584. * on.
  585. */
  586. if (mutex_trylock(&vcpu->mutex)) {
  587. if (vcpu->cpu == cpu) {
  588. kvm_x86_ops->vcpu_decache(vcpu);
  589. vcpu->cpu = -1;
  590. }
  591. mutex_unlock(&vcpu->mutex);
  592. }
  593. }
  594. spin_unlock(&kvm_lock);
  595. }
  596. int kvm_dev_ioctl_check_extension(long ext)
  597. {
  598. int r;
  599. switch (ext) {
  600. case KVM_CAP_IRQCHIP:
  601. case KVM_CAP_HLT:
  602. case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
  603. case KVM_CAP_USER_MEMORY:
  604. case KVM_CAP_SET_TSS_ADDR:
  605. case KVM_CAP_EXT_CPUID:
  606. r = 1;
  607. break;
  608. default:
  609. r = 0;
  610. break;
  611. }
  612. return r;
  613. }
  614. long kvm_arch_dev_ioctl(struct file *filp,
  615. unsigned int ioctl, unsigned long arg)
  616. {
  617. void __user *argp = (void __user *)arg;
  618. long r;
  619. switch (ioctl) {
  620. case KVM_GET_MSR_INDEX_LIST: {
  621. struct kvm_msr_list __user *user_msr_list = argp;
  622. struct kvm_msr_list msr_list;
  623. unsigned n;
  624. r = -EFAULT;
  625. if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
  626. goto out;
  627. n = msr_list.nmsrs;
  628. msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
  629. if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
  630. goto out;
  631. r = -E2BIG;
  632. if (n < num_msrs_to_save)
  633. goto out;
  634. r = -EFAULT;
  635. if (copy_to_user(user_msr_list->indices, &msrs_to_save,
  636. num_msrs_to_save * sizeof(u32)))
  637. goto out;
  638. if (copy_to_user(user_msr_list->indices
  639. + num_msrs_to_save * sizeof(u32),
  640. &emulated_msrs,
  641. ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
  642. goto out;
  643. r = 0;
  644. break;
  645. }
  646. default:
  647. r = -EINVAL;
  648. }
  649. out:
  650. return r;
  651. }
  652. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  653. {
  654. kvm_x86_ops->vcpu_load(vcpu, cpu);
  655. }
  656. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  657. {
  658. kvm_x86_ops->vcpu_put(vcpu);
  659. kvm_put_guest_fpu(vcpu);
  660. }
  661. static int is_efer_nx(void)
  662. {
  663. u64 efer;
  664. rdmsrl(MSR_EFER, efer);
  665. return efer & EFER_NX;
  666. }
  667. static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
  668. {
  669. int i;
  670. struct kvm_cpuid_entry2 *e, *entry;
  671. entry = NULL;
  672. for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
  673. e = &vcpu->arch.cpuid_entries[i];
  674. if (e->function == 0x80000001) {
  675. entry = e;
  676. break;
  677. }
  678. }
  679. if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
  680. entry->edx &= ~(1 << 20);
  681. printk(KERN_INFO "kvm: guest NX capability removed\n");
  682. }
  683. }
  684. /* when an old userspace process fills a new kernel module */
  685. static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
  686. struct kvm_cpuid *cpuid,
  687. struct kvm_cpuid_entry __user *entries)
  688. {
  689. int r, i;
  690. struct kvm_cpuid_entry *cpuid_entries;
  691. r = -E2BIG;
  692. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  693. goto out;
  694. r = -ENOMEM;
  695. cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
  696. if (!cpuid_entries)
  697. goto out;
  698. r = -EFAULT;
  699. if (copy_from_user(cpuid_entries, entries,
  700. cpuid->nent * sizeof(struct kvm_cpuid_entry)))
  701. goto out_free;
  702. for (i = 0; i < cpuid->nent; i++) {
  703. vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
  704. vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
  705. vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
  706. vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
  707. vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
  708. vcpu->arch.cpuid_entries[i].index = 0;
  709. vcpu->arch.cpuid_entries[i].flags = 0;
  710. vcpu->arch.cpuid_entries[i].padding[0] = 0;
  711. vcpu->arch.cpuid_entries[i].padding[1] = 0;
  712. vcpu->arch.cpuid_entries[i].padding[2] = 0;
  713. }
  714. vcpu->arch.cpuid_nent = cpuid->nent;
  715. cpuid_fix_nx_cap(vcpu);
  716. r = 0;
  717. out_free:
  718. vfree(cpuid_entries);
  719. out:
  720. return r;
  721. }
  722. static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
  723. struct kvm_cpuid2 *cpuid,
  724. struct kvm_cpuid_entry2 __user *entries)
  725. {
  726. int r;
  727. r = -E2BIG;
  728. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  729. goto out;
  730. r = -EFAULT;
  731. if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
  732. cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
  733. goto out;
  734. vcpu->arch.cpuid_nent = cpuid->nent;
  735. return 0;
  736. out:
  737. return r;
  738. }
  739. static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
  740. struct kvm_cpuid2 *cpuid,
  741. struct kvm_cpuid_entry2 __user *entries)
  742. {
  743. int r;
  744. r = -E2BIG;
  745. if (cpuid->nent < vcpu->arch.cpuid_nent)
  746. goto out;
  747. r = -EFAULT;
  748. if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
  749. vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
  750. goto out;
  751. return 0;
  752. out:
  753. cpuid->nent = vcpu->arch.cpuid_nent;
  754. return r;
  755. }
  756. static inline u32 bit(int bitno)
  757. {
  758. return 1 << (bitno & 31);
  759. }
  760. static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
  761. u32 index)
  762. {
  763. entry->function = function;
  764. entry->index = index;
  765. cpuid_count(entry->function, entry->index,
  766. &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
  767. entry->flags = 0;
  768. }
  769. static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
  770. u32 index, int *nent, int maxnent)
  771. {
  772. const u32 kvm_supported_word0_x86_features = bit(X86_FEATURE_FPU) |
  773. bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
  774. bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
  775. bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
  776. bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
  777. bit(X86_FEATURE_SEP) | bit(X86_FEATURE_PGE) |
  778. bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
  779. bit(X86_FEATURE_CLFLSH) | bit(X86_FEATURE_MMX) |
  780. bit(X86_FEATURE_FXSR) | bit(X86_FEATURE_XMM) |
  781. bit(X86_FEATURE_XMM2) | bit(X86_FEATURE_SELFSNOOP);
  782. const u32 kvm_supported_word1_x86_features = bit(X86_FEATURE_FPU) |
  783. bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
  784. bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
  785. bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
  786. bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
  787. bit(X86_FEATURE_PGE) |
  788. bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
  789. bit(X86_FEATURE_MMX) | bit(X86_FEATURE_FXSR) |
  790. bit(X86_FEATURE_SYSCALL) |
  791. (bit(X86_FEATURE_NX) && is_efer_nx()) |
  792. #ifdef CONFIG_X86_64
  793. bit(X86_FEATURE_LM) |
  794. #endif
  795. bit(X86_FEATURE_MMXEXT) |
  796. bit(X86_FEATURE_3DNOWEXT) |
  797. bit(X86_FEATURE_3DNOW);
  798. const u32 kvm_supported_word3_x86_features =
  799. bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16);
  800. const u32 kvm_supported_word6_x86_features =
  801. bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY);
  802. /* all func 2 cpuid_count() should be called on the same cpu */
  803. get_cpu();
  804. do_cpuid_1_ent(entry, function, index);
  805. ++*nent;
  806. switch (function) {
  807. case 0:
  808. entry->eax = min(entry->eax, (u32)0xb);
  809. break;
  810. case 1:
  811. entry->edx &= kvm_supported_word0_x86_features;
  812. entry->ecx &= kvm_supported_word3_x86_features;
  813. break;
  814. /* function 2 entries are STATEFUL. That is, repeated cpuid commands
  815. * may return different values. This forces us to get_cpu() before
  816. * issuing the first command, and also to emulate this annoying behavior
  817. * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
  818. case 2: {
  819. int t, times = entry->eax & 0xff;
  820. entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
  821. for (t = 1; t < times && *nent < maxnent; ++t) {
  822. do_cpuid_1_ent(&entry[t], function, 0);
  823. entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
  824. ++*nent;
  825. }
  826. break;
  827. }
  828. /* function 4 and 0xb have additional index. */
  829. case 4: {
  830. int index, cache_type;
  831. entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  832. /* read more entries until cache_type is zero */
  833. for (index = 1; *nent < maxnent; ++index) {
  834. cache_type = entry[index - 1].eax & 0x1f;
  835. if (!cache_type)
  836. break;
  837. do_cpuid_1_ent(&entry[index], function, index);
  838. entry[index].flags |=
  839. KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  840. ++*nent;
  841. }
  842. break;
  843. }
  844. case 0xb: {
  845. int index, level_type;
  846. entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  847. /* read more entries until level_type is zero */
  848. for (index = 1; *nent < maxnent; ++index) {
  849. level_type = entry[index - 1].ecx & 0xff;
  850. if (!level_type)
  851. break;
  852. do_cpuid_1_ent(&entry[index], function, index);
  853. entry[index].flags |=
  854. KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  855. ++*nent;
  856. }
  857. break;
  858. }
  859. case 0x80000000:
  860. entry->eax = min(entry->eax, 0x8000001a);
  861. break;
  862. case 0x80000001:
  863. entry->edx &= kvm_supported_word1_x86_features;
  864. entry->ecx &= kvm_supported_word6_x86_features;
  865. break;
  866. }
  867. put_cpu();
  868. }
  869. static int kvm_vm_ioctl_get_supported_cpuid(struct kvm *kvm,
  870. struct kvm_cpuid2 *cpuid,
  871. struct kvm_cpuid_entry2 __user *entries)
  872. {
  873. struct kvm_cpuid_entry2 *cpuid_entries;
  874. int limit, nent = 0, r = -E2BIG;
  875. u32 func;
  876. if (cpuid->nent < 1)
  877. goto out;
  878. r = -ENOMEM;
  879. cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
  880. if (!cpuid_entries)
  881. goto out;
  882. do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
  883. limit = cpuid_entries[0].eax;
  884. for (func = 1; func <= limit && nent < cpuid->nent; ++func)
  885. do_cpuid_ent(&cpuid_entries[nent], func, 0,
  886. &nent, cpuid->nent);
  887. r = -E2BIG;
  888. if (nent >= cpuid->nent)
  889. goto out_free;
  890. do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
  891. limit = cpuid_entries[nent - 1].eax;
  892. for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
  893. do_cpuid_ent(&cpuid_entries[nent], func, 0,
  894. &nent, cpuid->nent);
  895. r = -EFAULT;
  896. if (copy_to_user(entries, cpuid_entries,
  897. nent * sizeof(struct kvm_cpuid_entry2)))
  898. goto out_free;
  899. cpuid->nent = nent;
  900. r = 0;
  901. out_free:
  902. vfree(cpuid_entries);
  903. out:
  904. return r;
  905. }
  906. static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
  907. struct kvm_lapic_state *s)
  908. {
  909. vcpu_load(vcpu);
  910. memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
  911. vcpu_put(vcpu);
  912. return 0;
  913. }
  914. static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
  915. struct kvm_lapic_state *s)
  916. {
  917. vcpu_load(vcpu);
  918. memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
  919. kvm_apic_post_state_restore(vcpu);
  920. vcpu_put(vcpu);
  921. return 0;
  922. }
  923. static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
  924. struct kvm_interrupt *irq)
  925. {
  926. if (irq->irq < 0 || irq->irq >= 256)
  927. return -EINVAL;
  928. if (irqchip_in_kernel(vcpu->kvm))
  929. return -ENXIO;
  930. vcpu_load(vcpu);
  931. set_bit(irq->irq, vcpu->arch.irq_pending);
  932. set_bit(irq->irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
  933. vcpu_put(vcpu);
  934. return 0;
  935. }
  936. long kvm_arch_vcpu_ioctl(struct file *filp,
  937. unsigned int ioctl, unsigned long arg)
  938. {
  939. struct kvm_vcpu *vcpu = filp->private_data;
  940. void __user *argp = (void __user *)arg;
  941. int r;
  942. switch (ioctl) {
  943. case KVM_GET_LAPIC: {
  944. struct kvm_lapic_state lapic;
  945. memset(&lapic, 0, sizeof lapic);
  946. r = kvm_vcpu_ioctl_get_lapic(vcpu, &lapic);
  947. if (r)
  948. goto out;
  949. r = -EFAULT;
  950. if (copy_to_user(argp, &lapic, sizeof lapic))
  951. goto out;
  952. r = 0;
  953. break;
  954. }
  955. case KVM_SET_LAPIC: {
  956. struct kvm_lapic_state lapic;
  957. r = -EFAULT;
  958. if (copy_from_user(&lapic, argp, sizeof lapic))
  959. goto out;
  960. r = kvm_vcpu_ioctl_set_lapic(vcpu, &lapic);;
  961. if (r)
  962. goto out;
  963. r = 0;
  964. break;
  965. }
  966. case KVM_INTERRUPT: {
  967. struct kvm_interrupt irq;
  968. r = -EFAULT;
  969. if (copy_from_user(&irq, argp, sizeof irq))
  970. goto out;
  971. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  972. if (r)
  973. goto out;
  974. r = 0;
  975. break;
  976. }
  977. case KVM_SET_CPUID: {
  978. struct kvm_cpuid __user *cpuid_arg = argp;
  979. struct kvm_cpuid cpuid;
  980. r = -EFAULT;
  981. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  982. goto out;
  983. r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
  984. if (r)
  985. goto out;
  986. break;
  987. }
  988. case KVM_SET_CPUID2: {
  989. struct kvm_cpuid2 __user *cpuid_arg = argp;
  990. struct kvm_cpuid2 cpuid;
  991. r = -EFAULT;
  992. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  993. goto out;
  994. r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
  995. cpuid_arg->entries);
  996. if (r)
  997. goto out;
  998. break;
  999. }
  1000. case KVM_GET_CPUID2: {
  1001. struct kvm_cpuid2 __user *cpuid_arg = argp;
  1002. struct kvm_cpuid2 cpuid;
  1003. r = -EFAULT;
  1004. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1005. goto out;
  1006. r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
  1007. cpuid_arg->entries);
  1008. if (r)
  1009. goto out;
  1010. r = -EFAULT;
  1011. if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
  1012. goto out;
  1013. r = 0;
  1014. break;
  1015. }
  1016. case KVM_GET_MSRS:
  1017. r = msr_io(vcpu, argp, kvm_get_msr, 1);
  1018. break;
  1019. case KVM_SET_MSRS:
  1020. r = msr_io(vcpu, argp, do_set_msr, 0);
  1021. break;
  1022. default:
  1023. r = -EINVAL;
  1024. }
  1025. out:
  1026. return r;
  1027. }
  1028. static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
  1029. {
  1030. int ret;
  1031. if (addr > (unsigned int)(-3 * PAGE_SIZE))
  1032. return -1;
  1033. ret = kvm_x86_ops->set_tss_addr(kvm, addr);
  1034. return ret;
  1035. }
  1036. static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
  1037. u32 kvm_nr_mmu_pages)
  1038. {
  1039. if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
  1040. return -EINVAL;
  1041. mutex_lock(&kvm->lock);
  1042. kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
  1043. kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
  1044. mutex_unlock(&kvm->lock);
  1045. return 0;
  1046. }
  1047. static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
  1048. {
  1049. return kvm->arch.n_alloc_mmu_pages;
  1050. }
  1051. gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
  1052. {
  1053. int i;
  1054. struct kvm_mem_alias *alias;
  1055. for (i = 0; i < kvm->arch.naliases; ++i) {
  1056. alias = &kvm->arch.aliases[i];
  1057. if (gfn >= alias->base_gfn
  1058. && gfn < alias->base_gfn + alias->npages)
  1059. return alias->target_gfn + gfn - alias->base_gfn;
  1060. }
  1061. return gfn;
  1062. }
  1063. /*
  1064. * Set a new alias region. Aliases map a portion of physical memory into
  1065. * another portion. This is useful for memory windows, for example the PC
  1066. * VGA region.
  1067. */
  1068. static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
  1069. struct kvm_memory_alias *alias)
  1070. {
  1071. int r, n;
  1072. struct kvm_mem_alias *p;
  1073. r = -EINVAL;
  1074. /* General sanity checks */
  1075. if (alias->memory_size & (PAGE_SIZE - 1))
  1076. goto out;
  1077. if (alias->guest_phys_addr & (PAGE_SIZE - 1))
  1078. goto out;
  1079. if (alias->slot >= KVM_ALIAS_SLOTS)
  1080. goto out;
  1081. if (alias->guest_phys_addr + alias->memory_size
  1082. < alias->guest_phys_addr)
  1083. goto out;
  1084. if (alias->target_phys_addr + alias->memory_size
  1085. < alias->target_phys_addr)
  1086. goto out;
  1087. mutex_lock(&kvm->lock);
  1088. p = &kvm->arch.aliases[alias->slot];
  1089. p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
  1090. p->npages = alias->memory_size >> PAGE_SHIFT;
  1091. p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
  1092. for (n = KVM_ALIAS_SLOTS; n > 0; --n)
  1093. if (kvm->arch.aliases[n - 1].npages)
  1094. break;
  1095. kvm->arch.naliases = n;
  1096. kvm_mmu_zap_all(kvm);
  1097. mutex_unlock(&kvm->lock);
  1098. return 0;
  1099. out:
  1100. return r;
  1101. }
  1102. static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  1103. {
  1104. int r;
  1105. r = 0;
  1106. switch (chip->chip_id) {
  1107. case KVM_IRQCHIP_PIC_MASTER:
  1108. memcpy(&chip->chip.pic,
  1109. &pic_irqchip(kvm)->pics[0],
  1110. sizeof(struct kvm_pic_state));
  1111. break;
  1112. case KVM_IRQCHIP_PIC_SLAVE:
  1113. memcpy(&chip->chip.pic,
  1114. &pic_irqchip(kvm)->pics[1],
  1115. sizeof(struct kvm_pic_state));
  1116. break;
  1117. case KVM_IRQCHIP_IOAPIC:
  1118. memcpy(&chip->chip.ioapic,
  1119. ioapic_irqchip(kvm),
  1120. sizeof(struct kvm_ioapic_state));
  1121. break;
  1122. default:
  1123. r = -EINVAL;
  1124. break;
  1125. }
  1126. return r;
  1127. }
  1128. static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  1129. {
  1130. int r;
  1131. r = 0;
  1132. switch (chip->chip_id) {
  1133. case KVM_IRQCHIP_PIC_MASTER:
  1134. memcpy(&pic_irqchip(kvm)->pics[0],
  1135. &chip->chip.pic,
  1136. sizeof(struct kvm_pic_state));
  1137. break;
  1138. case KVM_IRQCHIP_PIC_SLAVE:
  1139. memcpy(&pic_irqchip(kvm)->pics[1],
  1140. &chip->chip.pic,
  1141. sizeof(struct kvm_pic_state));
  1142. break;
  1143. case KVM_IRQCHIP_IOAPIC:
  1144. memcpy(ioapic_irqchip(kvm),
  1145. &chip->chip.ioapic,
  1146. sizeof(struct kvm_ioapic_state));
  1147. break;
  1148. default:
  1149. r = -EINVAL;
  1150. break;
  1151. }
  1152. kvm_pic_update_irq(pic_irqchip(kvm));
  1153. return r;
  1154. }
  1155. /*
  1156. * Get (and clear) the dirty memory log for a memory slot.
  1157. */
  1158. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
  1159. struct kvm_dirty_log *log)
  1160. {
  1161. int r;
  1162. int n;
  1163. struct kvm_memory_slot *memslot;
  1164. int is_dirty = 0;
  1165. mutex_lock(&kvm->lock);
  1166. r = kvm_get_dirty_log(kvm, log, &is_dirty);
  1167. if (r)
  1168. goto out;
  1169. /* If nothing is dirty, don't bother messing with page tables. */
  1170. if (is_dirty) {
  1171. kvm_mmu_slot_remove_write_access(kvm, log->slot);
  1172. kvm_flush_remote_tlbs(kvm);
  1173. memslot = &kvm->memslots[log->slot];
  1174. n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
  1175. memset(memslot->dirty_bitmap, 0, n);
  1176. }
  1177. r = 0;
  1178. out:
  1179. mutex_unlock(&kvm->lock);
  1180. return r;
  1181. }
  1182. long kvm_arch_vm_ioctl(struct file *filp,
  1183. unsigned int ioctl, unsigned long arg)
  1184. {
  1185. struct kvm *kvm = filp->private_data;
  1186. void __user *argp = (void __user *)arg;
  1187. int r = -EINVAL;
  1188. switch (ioctl) {
  1189. case KVM_SET_TSS_ADDR:
  1190. r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
  1191. if (r < 0)
  1192. goto out;
  1193. break;
  1194. case KVM_SET_MEMORY_REGION: {
  1195. struct kvm_memory_region kvm_mem;
  1196. struct kvm_userspace_memory_region kvm_userspace_mem;
  1197. r = -EFAULT;
  1198. if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
  1199. goto out;
  1200. kvm_userspace_mem.slot = kvm_mem.slot;
  1201. kvm_userspace_mem.flags = kvm_mem.flags;
  1202. kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
  1203. kvm_userspace_mem.memory_size = kvm_mem.memory_size;
  1204. r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
  1205. if (r)
  1206. goto out;
  1207. break;
  1208. }
  1209. case KVM_SET_NR_MMU_PAGES:
  1210. r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
  1211. if (r)
  1212. goto out;
  1213. break;
  1214. case KVM_GET_NR_MMU_PAGES:
  1215. r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
  1216. break;
  1217. case KVM_SET_MEMORY_ALIAS: {
  1218. struct kvm_memory_alias alias;
  1219. r = -EFAULT;
  1220. if (copy_from_user(&alias, argp, sizeof alias))
  1221. goto out;
  1222. r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
  1223. if (r)
  1224. goto out;
  1225. break;
  1226. }
  1227. case KVM_CREATE_IRQCHIP:
  1228. r = -ENOMEM;
  1229. kvm->vpic = kvm_create_pic(kvm);
  1230. if (kvm->vpic) {
  1231. r = kvm_ioapic_init(kvm);
  1232. if (r) {
  1233. kfree(kvm->vpic);
  1234. kvm->vpic = NULL;
  1235. goto out;
  1236. }
  1237. } else
  1238. goto out;
  1239. break;
  1240. case KVM_IRQ_LINE: {
  1241. struct kvm_irq_level irq_event;
  1242. r = -EFAULT;
  1243. if (copy_from_user(&irq_event, argp, sizeof irq_event))
  1244. goto out;
  1245. if (irqchip_in_kernel(kvm)) {
  1246. mutex_lock(&kvm->lock);
  1247. if (irq_event.irq < 16)
  1248. kvm_pic_set_irq(pic_irqchip(kvm),
  1249. irq_event.irq,
  1250. irq_event.level);
  1251. kvm_ioapic_set_irq(kvm->vioapic,
  1252. irq_event.irq,
  1253. irq_event.level);
  1254. mutex_unlock(&kvm->lock);
  1255. r = 0;
  1256. }
  1257. break;
  1258. }
  1259. case KVM_GET_IRQCHIP: {
  1260. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  1261. struct kvm_irqchip chip;
  1262. r = -EFAULT;
  1263. if (copy_from_user(&chip, argp, sizeof chip))
  1264. goto out;
  1265. r = -ENXIO;
  1266. if (!irqchip_in_kernel(kvm))
  1267. goto out;
  1268. r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
  1269. if (r)
  1270. goto out;
  1271. r = -EFAULT;
  1272. if (copy_to_user(argp, &chip, sizeof chip))
  1273. goto out;
  1274. r = 0;
  1275. break;
  1276. }
  1277. case KVM_SET_IRQCHIP: {
  1278. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  1279. struct kvm_irqchip chip;
  1280. r = -EFAULT;
  1281. if (copy_from_user(&chip, argp, sizeof chip))
  1282. goto out;
  1283. r = -ENXIO;
  1284. if (!irqchip_in_kernel(kvm))
  1285. goto out;
  1286. r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
  1287. if (r)
  1288. goto out;
  1289. r = 0;
  1290. break;
  1291. }
  1292. case KVM_GET_SUPPORTED_CPUID: {
  1293. struct kvm_cpuid2 __user *cpuid_arg = argp;
  1294. struct kvm_cpuid2 cpuid;
  1295. r = -EFAULT;
  1296. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1297. goto out;
  1298. r = kvm_vm_ioctl_get_supported_cpuid(kvm, &cpuid,
  1299. cpuid_arg->entries);
  1300. if (r)
  1301. goto out;
  1302. r = -EFAULT;
  1303. if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
  1304. goto out;
  1305. r = 0;
  1306. break;
  1307. }
  1308. default:
  1309. ;
  1310. }
  1311. out:
  1312. return r;
  1313. }
  1314. static void kvm_init_msr_list(void)
  1315. {
  1316. u32 dummy[2];
  1317. unsigned i, j;
  1318. for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
  1319. if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
  1320. continue;
  1321. if (j < i)
  1322. msrs_to_save[j] = msrs_to_save[i];
  1323. j++;
  1324. }
  1325. num_msrs_to_save = j;
  1326. }
  1327. /*
  1328. * Only apic need an MMIO device hook, so shortcut now..
  1329. */
  1330. static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
  1331. gpa_t addr)
  1332. {
  1333. struct kvm_io_device *dev;
  1334. if (vcpu->arch.apic) {
  1335. dev = &vcpu->arch.apic->dev;
  1336. if (dev->in_range(dev, addr))
  1337. return dev;
  1338. }
  1339. return NULL;
  1340. }
  1341. static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
  1342. gpa_t addr)
  1343. {
  1344. struct kvm_io_device *dev;
  1345. dev = vcpu_find_pervcpu_dev(vcpu, addr);
  1346. if (dev == NULL)
  1347. dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
  1348. return dev;
  1349. }
  1350. int emulator_read_std(unsigned long addr,
  1351. void *val,
  1352. unsigned int bytes,
  1353. struct kvm_vcpu *vcpu)
  1354. {
  1355. void *data = val;
  1356. while (bytes) {
  1357. gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
  1358. unsigned offset = addr & (PAGE_SIZE-1);
  1359. unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
  1360. int ret;
  1361. if (gpa == UNMAPPED_GVA)
  1362. return X86EMUL_PROPAGATE_FAULT;
  1363. ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
  1364. if (ret < 0)
  1365. return X86EMUL_UNHANDLEABLE;
  1366. bytes -= tocopy;
  1367. data += tocopy;
  1368. addr += tocopy;
  1369. }
  1370. return X86EMUL_CONTINUE;
  1371. }
  1372. EXPORT_SYMBOL_GPL(emulator_read_std);
  1373. static int emulator_read_emulated(unsigned long addr,
  1374. void *val,
  1375. unsigned int bytes,
  1376. struct kvm_vcpu *vcpu)
  1377. {
  1378. struct kvm_io_device *mmio_dev;
  1379. gpa_t gpa;
  1380. if (vcpu->mmio_read_completed) {
  1381. memcpy(val, vcpu->mmio_data, bytes);
  1382. vcpu->mmio_read_completed = 0;
  1383. return X86EMUL_CONTINUE;
  1384. }
  1385. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
  1386. /* For APIC access vmexit */
  1387. if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  1388. goto mmio;
  1389. if (emulator_read_std(addr, val, bytes, vcpu)
  1390. == X86EMUL_CONTINUE)
  1391. return X86EMUL_CONTINUE;
  1392. if (gpa == UNMAPPED_GVA)
  1393. return X86EMUL_PROPAGATE_FAULT;
  1394. mmio:
  1395. /*
  1396. * Is this MMIO handled locally?
  1397. */
  1398. mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
  1399. if (mmio_dev) {
  1400. kvm_iodevice_read(mmio_dev, gpa, bytes, val);
  1401. return X86EMUL_CONTINUE;
  1402. }
  1403. vcpu->mmio_needed = 1;
  1404. vcpu->mmio_phys_addr = gpa;
  1405. vcpu->mmio_size = bytes;
  1406. vcpu->mmio_is_write = 0;
  1407. return X86EMUL_UNHANDLEABLE;
  1408. }
  1409. static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
  1410. const void *val, int bytes)
  1411. {
  1412. int ret;
  1413. ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
  1414. if (ret < 0)
  1415. return 0;
  1416. kvm_mmu_pte_write(vcpu, gpa, val, bytes);
  1417. return 1;
  1418. }
  1419. static int emulator_write_emulated_onepage(unsigned long addr,
  1420. const void *val,
  1421. unsigned int bytes,
  1422. struct kvm_vcpu *vcpu)
  1423. {
  1424. struct kvm_io_device *mmio_dev;
  1425. gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
  1426. if (gpa == UNMAPPED_GVA) {
  1427. kvm_inject_page_fault(vcpu, addr, 2);
  1428. return X86EMUL_PROPAGATE_FAULT;
  1429. }
  1430. /* For APIC access vmexit */
  1431. if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  1432. goto mmio;
  1433. if (emulator_write_phys(vcpu, gpa, val, bytes))
  1434. return X86EMUL_CONTINUE;
  1435. mmio:
  1436. /*
  1437. * Is this MMIO handled locally?
  1438. */
  1439. mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
  1440. if (mmio_dev) {
  1441. kvm_iodevice_write(mmio_dev, gpa, bytes, val);
  1442. return X86EMUL_CONTINUE;
  1443. }
  1444. vcpu->mmio_needed = 1;
  1445. vcpu->mmio_phys_addr = gpa;
  1446. vcpu->mmio_size = bytes;
  1447. vcpu->mmio_is_write = 1;
  1448. memcpy(vcpu->mmio_data, val, bytes);
  1449. return X86EMUL_CONTINUE;
  1450. }
  1451. int emulator_write_emulated(unsigned long addr,
  1452. const void *val,
  1453. unsigned int bytes,
  1454. struct kvm_vcpu *vcpu)
  1455. {
  1456. /* Crossing a page boundary? */
  1457. if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
  1458. int rc, now;
  1459. now = -addr & ~PAGE_MASK;
  1460. rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
  1461. if (rc != X86EMUL_CONTINUE)
  1462. return rc;
  1463. addr += now;
  1464. val += now;
  1465. bytes -= now;
  1466. }
  1467. return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
  1468. }
  1469. EXPORT_SYMBOL_GPL(emulator_write_emulated);
  1470. static int emulator_cmpxchg_emulated(unsigned long addr,
  1471. const void *old,
  1472. const void *new,
  1473. unsigned int bytes,
  1474. struct kvm_vcpu *vcpu)
  1475. {
  1476. static int reported;
  1477. if (!reported) {
  1478. reported = 1;
  1479. printk(KERN_WARNING "kvm: emulating exchange as write\n");
  1480. }
  1481. #ifndef CONFIG_X86_64
  1482. /* guests cmpxchg8b have to be emulated atomically */
  1483. if (bytes == 8) {
  1484. gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
  1485. struct page *page;
  1486. char *addr;
  1487. u64 val;
  1488. if (gpa == UNMAPPED_GVA ||
  1489. (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  1490. goto emul_write;
  1491. if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
  1492. goto emul_write;
  1493. val = *(u64 *)new;
  1494. page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
  1495. addr = kmap_atomic(page, KM_USER0);
  1496. set_64bit((u64 *)(addr + offset_in_page(gpa)), val);
  1497. kunmap_atomic(addr, KM_USER0);
  1498. kvm_release_page_dirty(page);
  1499. }
  1500. emul_write:
  1501. #endif
  1502. return emulator_write_emulated(addr, new, bytes, vcpu);
  1503. }
  1504. static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
  1505. {
  1506. return kvm_x86_ops->get_segment_base(vcpu, seg);
  1507. }
  1508. int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
  1509. {
  1510. return X86EMUL_CONTINUE;
  1511. }
  1512. int emulate_clts(struct kvm_vcpu *vcpu)
  1513. {
  1514. kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS);
  1515. return X86EMUL_CONTINUE;
  1516. }
  1517. int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
  1518. {
  1519. struct kvm_vcpu *vcpu = ctxt->vcpu;
  1520. switch (dr) {
  1521. case 0 ... 3:
  1522. *dest = kvm_x86_ops->get_dr(vcpu, dr);
  1523. return X86EMUL_CONTINUE;
  1524. default:
  1525. pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
  1526. return X86EMUL_UNHANDLEABLE;
  1527. }
  1528. }
  1529. int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
  1530. {
  1531. unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
  1532. int exception;
  1533. kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
  1534. if (exception) {
  1535. /* FIXME: better handling */
  1536. return X86EMUL_UNHANDLEABLE;
  1537. }
  1538. return X86EMUL_CONTINUE;
  1539. }
  1540. void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
  1541. {
  1542. static int reported;
  1543. u8 opcodes[4];
  1544. unsigned long rip = vcpu->arch.rip;
  1545. unsigned long rip_linear;
  1546. rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
  1547. if (reported)
  1548. return;
  1549. emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
  1550. printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
  1551. context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
  1552. reported = 1;
  1553. }
  1554. EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
  1555. struct x86_emulate_ops emulate_ops = {
  1556. .read_std = emulator_read_std,
  1557. .read_emulated = emulator_read_emulated,
  1558. .write_emulated = emulator_write_emulated,
  1559. .cmpxchg_emulated = emulator_cmpxchg_emulated,
  1560. };
  1561. int emulate_instruction(struct kvm_vcpu *vcpu,
  1562. struct kvm_run *run,
  1563. unsigned long cr2,
  1564. u16 error_code,
  1565. int no_decode)
  1566. {
  1567. int r;
  1568. vcpu->arch.mmio_fault_cr2 = cr2;
  1569. kvm_x86_ops->cache_regs(vcpu);
  1570. vcpu->mmio_is_write = 0;
  1571. vcpu->arch.pio.string = 0;
  1572. if (!no_decode) {
  1573. int cs_db, cs_l;
  1574. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  1575. vcpu->arch.emulate_ctxt.vcpu = vcpu;
  1576. vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
  1577. vcpu->arch.emulate_ctxt.mode =
  1578. (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
  1579. ? X86EMUL_MODE_REAL : cs_l
  1580. ? X86EMUL_MODE_PROT64 : cs_db
  1581. ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
  1582. if (vcpu->arch.emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
  1583. vcpu->arch.emulate_ctxt.cs_base = 0;
  1584. vcpu->arch.emulate_ctxt.ds_base = 0;
  1585. vcpu->arch.emulate_ctxt.es_base = 0;
  1586. vcpu->arch.emulate_ctxt.ss_base = 0;
  1587. } else {
  1588. vcpu->arch.emulate_ctxt.cs_base =
  1589. get_segment_base(vcpu, VCPU_SREG_CS);
  1590. vcpu->arch.emulate_ctxt.ds_base =
  1591. get_segment_base(vcpu, VCPU_SREG_DS);
  1592. vcpu->arch.emulate_ctxt.es_base =
  1593. get_segment_base(vcpu, VCPU_SREG_ES);
  1594. vcpu->arch.emulate_ctxt.ss_base =
  1595. get_segment_base(vcpu, VCPU_SREG_SS);
  1596. }
  1597. vcpu->arch.emulate_ctxt.gs_base =
  1598. get_segment_base(vcpu, VCPU_SREG_GS);
  1599. vcpu->arch.emulate_ctxt.fs_base =
  1600. get_segment_base(vcpu, VCPU_SREG_FS);
  1601. r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
  1602. ++vcpu->stat.insn_emulation;
  1603. if (r) {
  1604. ++vcpu->stat.insn_emulation_fail;
  1605. if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
  1606. return EMULATE_DONE;
  1607. return EMULATE_FAIL;
  1608. }
  1609. }
  1610. r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
  1611. if (vcpu->arch.pio.string)
  1612. return EMULATE_DO_MMIO;
  1613. if ((r || vcpu->mmio_is_write) && run) {
  1614. run->exit_reason = KVM_EXIT_MMIO;
  1615. run->mmio.phys_addr = vcpu->mmio_phys_addr;
  1616. memcpy(run->mmio.data, vcpu->mmio_data, 8);
  1617. run->mmio.len = vcpu->mmio_size;
  1618. run->mmio.is_write = vcpu->mmio_is_write;
  1619. }
  1620. if (r) {
  1621. if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
  1622. return EMULATE_DONE;
  1623. if (!vcpu->mmio_needed) {
  1624. kvm_report_emulation_failure(vcpu, "mmio");
  1625. return EMULATE_FAIL;
  1626. }
  1627. return EMULATE_DO_MMIO;
  1628. }
  1629. kvm_x86_ops->decache_regs(vcpu);
  1630. kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
  1631. if (vcpu->mmio_is_write) {
  1632. vcpu->mmio_needed = 0;
  1633. return EMULATE_DO_MMIO;
  1634. }
  1635. return EMULATE_DONE;
  1636. }
  1637. EXPORT_SYMBOL_GPL(emulate_instruction);
  1638. static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
  1639. {
  1640. int i;
  1641. for (i = 0; i < ARRAY_SIZE(vcpu->arch.pio.guest_pages); ++i)
  1642. if (vcpu->arch.pio.guest_pages[i]) {
  1643. kvm_release_page_dirty(vcpu->arch.pio.guest_pages[i]);
  1644. vcpu->arch.pio.guest_pages[i] = NULL;
  1645. }
  1646. }
  1647. static int pio_copy_data(struct kvm_vcpu *vcpu)
  1648. {
  1649. void *p = vcpu->arch.pio_data;
  1650. void *q;
  1651. unsigned bytes;
  1652. int nr_pages = vcpu->arch.pio.guest_pages[1] ? 2 : 1;
  1653. q = vmap(vcpu->arch.pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
  1654. PAGE_KERNEL);
  1655. if (!q) {
  1656. free_pio_guest_pages(vcpu);
  1657. return -ENOMEM;
  1658. }
  1659. q += vcpu->arch.pio.guest_page_offset;
  1660. bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
  1661. if (vcpu->arch.pio.in)
  1662. memcpy(q, p, bytes);
  1663. else
  1664. memcpy(p, q, bytes);
  1665. q -= vcpu->arch.pio.guest_page_offset;
  1666. vunmap(q);
  1667. free_pio_guest_pages(vcpu);
  1668. return 0;
  1669. }
  1670. int complete_pio(struct kvm_vcpu *vcpu)
  1671. {
  1672. struct kvm_pio_request *io = &vcpu->arch.pio;
  1673. long delta;
  1674. int r;
  1675. kvm_x86_ops->cache_regs(vcpu);
  1676. if (!io->string) {
  1677. if (io->in)
  1678. memcpy(&vcpu->arch.regs[VCPU_REGS_RAX], vcpu->arch.pio_data,
  1679. io->size);
  1680. } else {
  1681. if (io->in) {
  1682. r = pio_copy_data(vcpu);
  1683. if (r) {
  1684. kvm_x86_ops->cache_regs(vcpu);
  1685. return r;
  1686. }
  1687. }
  1688. delta = 1;
  1689. if (io->rep) {
  1690. delta *= io->cur_count;
  1691. /*
  1692. * The size of the register should really depend on
  1693. * current address size.
  1694. */
  1695. vcpu->arch.regs[VCPU_REGS_RCX] -= delta;
  1696. }
  1697. if (io->down)
  1698. delta = -delta;
  1699. delta *= io->size;
  1700. if (io->in)
  1701. vcpu->arch.regs[VCPU_REGS_RDI] += delta;
  1702. else
  1703. vcpu->arch.regs[VCPU_REGS_RSI] += delta;
  1704. }
  1705. kvm_x86_ops->decache_regs(vcpu);
  1706. io->count -= io->cur_count;
  1707. io->cur_count = 0;
  1708. return 0;
  1709. }
  1710. static void kernel_pio(struct kvm_io_device *pio_dev,
  1711. struct kvm_vcpu *vcpu,
  1712. void *pd)
  1713. {
  1714. /* TODO: String I/O for in kernel device */
  1715. mutex_lock(&vcpu->kvm->lock);
  1716. if (vcpu->arch.pio.in)
  1717. kvm_iodevice_read(pio_dev, vcpu->arch.pio.port,
  1718. vcpu->arch.pio.size,
  1719. pd);
  1720. else
  1721. kvm_iodevice_write(pio_dev, vcpu->arch.pio.port,
  1722. vcpu->arch.pio.size,
  1723. pd);
  1724. mutex_unlock(&vcpu->kvm->lock);
  1725. }
  1726. static void pio_string_write(struct kvm_io_device *pio_dev,
  1727. struct kvm_vcpu *vcpu)
  1728. {
  1729. struct kvm_pio_request *io = &vcpu->arch.pio;
  1730. void *pd = vcpu->arch.pio_data;
  1731. int i;
  1732. mutex_lock(&vcpu->kvm->lock);
  1733. for (i = 0; i < io->cur_count; i++) {
  1734. kvm_iodevice_write(pio_dev, io->port,
  1735. io->size,
  1736. pd);
  1737. pd += io->size;
  1738. }
  1739. mutex_unlock(&vcpu->kvm->lock);
  1740. }
  1741. static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
  1742. gpa_t addr)
  1743. {
  1744. return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
  1745. }
  1746. int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  1747. int size, unsigned port)
  1748. {
  1749. struct kvm_io_device *pio_dev;
  1750. vcpu->run->exit_reason = KVM_EXIT_IO;
  1751. vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
  1752. vcpu->run->io.size = vcpu->arch.pio.size = size;
  1753. vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
  1754. vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
  1755. vcpu->run->io.port = vcpu->arch.pio.port = port;
  1756. vcpu->arch.pio.in = in;
  1757. vcpu->arch.pio.string = 0;
  1758. vcpu->arch.pio.down = 0;
  1759. vcpu->arch.pio.guest_page_offset = 0;
  1760. vcpu->arch.pio.rep = 0;
  1761. kvm_x86_ops->cache_regs(vcpu);
  1762. memcpy(vcpu->arch.pio_data, &vcpu->arch.regs[VCPU_REGS_RAX], 4);
  1763. kvm_x86_ops->decache_regs(vcpu);
  1764. kvm_x86_ops->skip_emulated_instruction(vcpu);
  1765. pio_dev = vcpu_find_pio_dev(vcpu, port);
  1766. if (pio_dev) {
  1767. kernel_pio(pio_dev, vcpu, vcpu->arch.pio_data);
  1768. complete_pio(vcpu);
  1769. return 1;
  1770. }
  1771. return 0;
  1772. }
  1773. EXPORT_SYMBOL_GPL(kvm_emulate_pio);
  1774. int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  1775. int size, unsigned long count, int down,
  1776. gva_t address, int rep, unsigned port)
  1777. {
  1778. unsigned now, in_page;
  1779. int i, ret = 0;
  1780. int nr_pages = 1;
  1781. struct page *page;
  1782. struct kvm_io_device *pio_dev;
  1783. vcpu->run->exit_reason = KVM_EXIT_IO;
  1784. vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
  1785. vcpu->run->io.size = vcpu->arch.pio.size = size;
  1786. vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
  1787. vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
  1788. vcpu->run->io.port = vcpu->arch.pio.port = port;
  1789. vcpu->arch.pio.in = in;
  1790. vcpu->arch.pio.string = 1;
  1791. vcpu->arch.pio.down = down;
  1792. vcpu->arch.pio.guest_page_offset = offset_in_page(address);
  1793. vcpu->arch.pio.rep = rep;
  1794. if (!count) {
  1795. kvm_x86_ops->skip_emulated_instruction(vcpu);
  1796. return 1;
  1797. }
  1798. if (!down)
  1799. in_page = PAGE_SIZE - offset_in_page(address);
  1800. else
  1801. in_page = offset_in_page(address) + size;
  1802. now = min(count, (unsigned long)in_page / size);
  1803. if (!now) {
  1804. /*
  1805. * String I/O straddles page boundary. Pin two guest pages
  1806. * so that we satisfy atomicity constraints. Do just one
  1807. * transaction to avoid complexity.
  1808. */
  1809. nr_pages = 2;
  1810. now = 1;
  1811. }
  1812. if (down) {
  1813. /*
  1814. * String I/O in reverse. Yuck. Kill the guest, fix later.
  1815. */
  1816. pr_unimpl(vcpu, "guest string pio down\n");
  1817. kvm_inject_gp(vcpu, 0);
  1818. return 1;
  1819. }
  1820. vcpu->run->io.count = now;
  1821. vcpu->arch.pio.cur_count = now;
  1822. if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
  1823. kvm_x86_ops->skip_emulated_instruction(vcpu);
  1824. for (i = 0; i < nr_pages; ++i) {
  1825. mutex_lock(&vcpu->kvm->lock);
  1826. page = gva_to_page(vcpu, address + i * PAGE_SIZE);
  1827. vcpu->arch.pio.guest_pages[i] = page;
  1828. mutex_unlock(&vcpu->kvm->lock);
  1829. if (!page) {
  1830. kvm_inject_gp(vcpu, 0);
  1831. free_pio_guest_pages(vcpu);
  1832. return 1;
  1833. }
  1834. }
  1835. pio_dev = vcpu_find_pio_dev(vcpu, port);
  1836. if (!vcpu->arch.pio.in) {
  1837. /* string PIO write */
  1838. ret = pio_copy_data(vcpu);
  1839. if (ret >= 0 && pio_dev) {
  1840. pio_string_write(pio_dev, vcpu);
  1841. complete_pio(vcpu);
  1842. if (vcpu->arch.pio.count == 0)
  1843. ret = 1;
  1844. }
  1845. } else if (pio_dev)
  1846. pr_unimpl(vcpu, "no string pio read support yet, "
  1847. "port %x size %d count %ld\n",
  1848. port, size, count);
  1849. return ret;
  1850. }
  1851. EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
  1852. int kvm_arch_init(void *opaque)
  1853. {
  1854. int r;
  1855. struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
  1856. r = kvm_mmu_module_init();
  1857. if (r)
  1858. goto out_fail;
  1859. kvm_init_msr_list();
  1860. if (kvm_x86_ops) {
  1861. printk(KERN_ERR "kvm: already loaded the other module\n");
  1862. r = -EEXIST;
  1863. goto out;
  1864. }
  1865. if (!ops->cpu_has_kvm_support()) {
  1866. printk(KERN_ERR "kvm: no hardware support\n");
  1867. r = -EOPNOTSUPP;
  1868. goto out;
  1869. }
  1870. if (ops->disabled_by_bios()) {
  1871. printk(KERN_ERR "kvm: disabled by bios\n");
  1872. r = -EOPNOTSUPP;
  1873. goto out;
  1874. }
  1875. kvm_x86_ops = ops;
  1876. kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
  1877. return 0;
  1878. out:
  1879. kvm_mmu_module_exit();
  1880. out_fail:
  1881. return r;
  1882. }
  1883. void kvm_arch_exit(void)
  1884. {
  1885. kvm_x86_ops = NULL;
  1886. kvm_mmu_module_exit();
  1887. }
  1888. int kvm_emulate_halt(struct kvm_vcpu *vcpu)
  1889. {
  1890. ++vcpu->stat.halt_exits;
  1891. if (irqchip_in_kernel(vcpu->kvm)) {
  1892. vcpu->arch.mp_state = VCPU_MP_STATE_HALTED;
  1893. kvm_vcpu_block(vcpu);
  1894. if (vcpu->arch.mp_state != VCPU_MP_STATE_RUNNABLE)
  1895. return -EINTR;
  1896. return 1;
  1897. } else {
  1898. vcpu->run->exit_reason = KVM_EXIT_HLT;
  1899. return 0;
  1900. }
  1901. }
  1902. EXPORT_SYMBOL_GPL(kvm_emulate_halt);
  1903. int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
  1904. {
  1905. unsigned long nr, a0, a1, a2, a3, ret;
  1906. kvm_x86_ops->cache_regs(vcpu);
  1907. nr = vcpu->arch.regs[VCPU_REGS_RAX];
  1908. a0 = vcpu->arch.regs[VCPU_REGS_RBX];
  1909. a1 = vcpu->arch.regs[VCPU_REGS_RCX];
  1910. a2 = vcpu->arch.regs[VCPU_REGS_RDX];
  1911. a3 = vcpu->arch.regs[VCPU_REGS_RSI];
  1912. if (!is_long_mode(vcpu)) {
  1913. nr &= 0xFFFFFFFF;
  1914. a0 &= 0xFFFFFFFF;
  1915. a1 &= 0xFFFFFFFF;
  1916. a2 &= 0xFFFFFFFF;
  1917. a3 &= 0xFFFFFFFF;
  1918. }
  1919. switch (nr) {
  1920. default:
  1921. ret = -KVM_ENOSYS;
  1922. break;
  1923. }
  1924. vcpu->arch.regs[VCPU_REGS_RAX] = ret;
  1925. kvm_x86_ops->decache_regs(vcpu);
  1926. return 0;
  1927. }
  1928. EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
  1929. int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
  1930. {
  1931. char instruction[3];
  1932. int ret = 0;
  1933. mutex_lock(&vcpu->kvm->lock);
  1934. /*
  1935. * Blow out the MMU to ensure that no other VCPU has an active mapping
  1936. * to ensure that the updated hypercall appears atomically across all
  1937. * VCPUs.
  1938. */
  1939. kvm_mmu_zap_all(vcpu->kvm);
  1940. kvm_x86_ops->cache_regs(vcpu);
  1941. kvm_x86_ops->patch_hypercall(vcpu, instruction);
  1942. if (emulator_write_emulated(vcpu->arch.rip, instruction, 3, vcpu)
  1943. != X86EMUL_CONTINUE)
  1944. ret = -EFAULT;
  1945. mutex_unlock(&vcpu->kvm->lock);
  1946. return ret;
  1947. }
  1948. static u64 mk_cr_64(u64 curr_cr, u32 new_val)
  1949. {
  1950. return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
  1951. }
  1952. void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  1953. {
  1954. struct descriptor_table dt = { limit, base };
  1955. kvm_x86_ops->set_gdt(vcpu, &dt);
  1956. }
  1957. void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  1958. {
  1959. struct descriptor_table dt = { limit, base };
  1960. kvm_x86_ops->set_idt(vcpu, &dt);
  1961. }
  1962. void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
  1963. unsigned long *rflags)
  1964. {
  1965. lmsw(vcpu, msw);
  1966. *rflags = kvm_x86_ops->get_rflags(vcpu);
  1967. }
  1968. unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
  1969. {
  1970. kvm_x86_ops->decache_cr4_guest_bits(vcpu);
  1971. switch (cr) {
  1972. case 0:
  1973. return vcpu->arch.cr0;
  1974. case 2:
  1975. return vcpu->arch.cr2;
  1976. case 3:
  1977. return vcpu->arch.cr3;
  1978. case 4:
  1979. return vcpu->arch.cr4;
  1980. case 8:
  1981. return get_cr8(vcpu);
  1982. default:
  1983. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
  1984. return 0;
  1985. }
  1986. }
  1987. void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
  1988. unsigned long *rflags)
  1989. {
  1990. switch (cr) {
  1991. case 0:
  1992. set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val));
  1993. *rflags = kvm_x86_ops->get_rflags(vcpu);
  1994. break;
  1995. case 2:
  1996. vcpu->arch.cr2 = val;
  1997. break;
  1998. case 3:
  1999. set_cr3(vcpu, val);
  2000. break;
  2001. case 4:
  2002. set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val));
  2003. break;
  2004. case 8:
  2005. set_cr8(vcpu, val & 0xfUL);
  2006. break;
  2007. default:
  2008. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
  2009. }
  2010. }
  2011. static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
  2012. {
  2013. struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
  2014. int j, nent = vcpu->arch.cpuid_nent;
  2015. e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
  2016. /* when no next entry is found, the current entry[i] is reselected */
  2017. for (j = i + 1; j == i; j = (j + 1) % nent) {
  2018. struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
  2019. if (ej->function == e->function) {
  2020. ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
  2021. return j;
  2022. }
  2023. }
  2024. return 0; /* silence gcc, even though control never reaches here */
  2025. }
  2026. /* find an entry with matching function, matching index (if needed), and that
  2027. * should be read next (if it's stateful) */
  2028. static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
  2029. u32 function, u32 index)
  2030. {
  2031. if (e->function != function)
  2032. return 0;
  2033. if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
  2034. return 0;
  2035. if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
  2036. !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
  2037. return 0;
  2038. return 1;
  2039. }
  2040. void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
  2041. {
  2042. int i;
  2043. u32 function, index;
  2044. struct kvm_cpuid_entry2 *e, *best;
  2045. kvm_x86_ops->cache_regs(vcpu);
  2046. function = vcpu->arch.regs[VCPU_REGS_RAX];
  2047. index = vcpu->arch.regs[VCPU_REGS_RCX];
  2048. vcpu->arch.regs[VCPU_REGS_RAX] = 0;
  2049. vcpu->arch.regs[VCPU_REGS_RBX] = 0;
  2050. vcpu->arch.regs[VCPU_REGS_RCX] = 0;
  2051. vcpu->arch.regs[VCPU_REGS_RDX] = 0;
  2052. best = NULL;
  2053. for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
  2054. e = &vcpu->arch.cpuid_entries[i];
  2055. if (is_matching_cpuid_entry(e, function, index)) {
  2056. if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
  2057. move_to_next_stateful_cpuid_entry(vcpu, i);
  2058. best = e;
  2059. break;
  2060. }
  2061. /*
  2062. * Both basic or both extended?
  2063. */
  2064. if (((e->function ^ function) & 0x80000000) == 0)
  2065. if (!best || e->function > best->function)
  2066. best = e;
  2067. }
  2068. if (best) {
  2069. vcpu->arch.regs[VCPU_REGS_RAX] = best->eax;
  2070. vcpu->arch.regs[VCPU_REGS_RBX] = best->ebx;
  2071. vcpu->arch.regs[VCPU_REGS_RCX] = best->ecx;
  2072. vcpu->arch.regs[VCPU_REGS_RDX] = best->edx;
  2073. }
  2074. kvm_x86_ops->decache_regs(vcpu);
  2075. kvm_x86_ops->skip_emulated_instruction(vcpu);
  2076. }
  2077. EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
  2078. /*
  2079. * Check if userspace requested an interrupt window, and that the
  2080. * interrupt window is open.
  2081. *
  2082. * No need to exit to userspace if we already have an interrupt queued.
  2083. */
  2084. static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
  2085. struct kvm_run *kvm_run)
  2086. {
  2087. return (!vcpu->arch.irq_summary &&
  2088. kvm_run->request_interrupt_window &&
  2089. vcpu->arch.interrupt_window_open &&
  2090. (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
  2091. }
  2092. static void post_kvm_run_save(struct kvm_vcpu *vcpu,
  2093. struct kvm_run *kvm_run)
  2094. {
  2095. kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
  2096. kvm_run->cr8 = get_cr8(vcpu);
  2097. kvm_run->apic_base = kvm_get_apic_base(vcpu);
  2098. if (irqchip_in_kernel(vcpu->kvm))
  2099. kvm_run->ready_for_interrupt_injection = 1;
  2100. else
  2101. kvm_run->ready_for_interrupt_injection =
  2102. (vcpu->arch.interrupt_window_open &&
  2103. vcpu->arch.irq_summary == 0);
  2104. }
  2105. static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2106. {
  2107. int r;
  2108. if (unlikely(vcpu->arch.mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
  2109. pr_debug("vcpu %d received sipi with vector # %x\n",
  2110. vcpu->vcpu_id, vcpu->arch.sipi_vector);
  2111. kvm_lapic_reset(vcpu);
  2112. r = kvm_x86_ops->vcpu_reset(vcpu);
  2113. if (r)
  2114. return r;
  2115. vcpu->arch.mp_state = VCPU_MP_STATE_RUNNABLE;
  2116. }
  2117. preempted:
  2118. if (vcpu->guest_debug.enabled)
  2119. kvm_x86_ops->guest_debug_pre(vcpu);
  2120. again:
  2121. r = kvm_mmu_reload(vcpu);
  2122. if (unlikely(r))
  2123. goto out;
  2124. kvm_inject_pending_timer_irqs(vcpu);
  2125. preempt_disable();
  2126. kvm_x86_ops->prepare_guest_switch(vcpu);
  2127. kvm_load_guest_fpu(vcpu);
  2128. local_irq_disable();
  2129. if (signal_pending(current)) {
  2130. local_irq_enable();
  2131. preempt_enable();
  2132. r = -EINTR;
  2133. kvm_run->exit_reason = KVM_EXIT_INTR;
  2134. ++vcpu->stat.signal_exits;
  2135. goto out;
  2136. }
  2137. if (vcpu->arch.exception.pending)
  2138. __queue_exception(vcpu);
  2139. else if (irqchip_in_kernel(vcpu->kvm))
  2140. kvm_x86_ops->inject_pending_irq(vcpu);
  2141. else
  2142. kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
  2143. vcpu->guest_mode = 1;
  2144. kvm_guest_enter();
  2145. if (vcpu->requests)
  2146. if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
  2147. kvm_x86_ops->tlb_flush(vcpu);
  2148. kvm_x86_ops->run(vcpu, kvm_run);
  2149. vcpu->guest_mode = 0;
  2150. local_irq_enable();
  2151. ++vcpu->stat.exits;
  2152. /*
  2153. * We must have an instruction between local_irq_enable() and
  2154. * kvm_guest_exit(), so the timer interrupt isn't delayed by
  2155. * the interrupt shadow. The stat.exits increment will do nicely.
  2156. * But we need to prevent reordering, hence this barrier():
  2157. */
  2158. barrier();
  2159. kvm_guest_exit();
  2160. preempt_enable();
  2161. /*
  2162. * Profile KVM exit RIPs:
  2163. */
  2164. if (unlikely(prof_on == KVM_PROFILING)) {
  2165. kvm_x86_ops->cache_regs(vcpu);
  2166. profile_hit(KVM_PROFILING, (void *)vcpu->arch.rip);
  2167. }
  2168. if (vcpu->arch.exception.pending && kvm_x86_ops->exception_injected(vcpu))
  2169. vcpu->arch.exception.pending = false;
  2170. r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
  2171. if (r > 0) {
  2172. if (dm_request_for_irq_injection(vcpu, kvm_run)) {
  2173. r = -EINTR;
  2174. kvm_run->exit_reason = KVM_EXIT_INTR;
  2175. ++vcpu->stat.request_irq_exits;
  2176. goto out;
  2177. }
  2178. if (!need_resched())
  2179. goto again;
  2180. }
  2181. out:
  2182. if (r > 0) {
  2183. kvm_resched(vcpu);
  2184. goto preempted;
  2185. }
  2186. post_kvm_run_save(vcpu, kvm_run);
  2187. return r;
  2188. }
  2189. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2190. {
  2191. int r;
  2192. sigset_t sigsaved;
  2193. vcpu_load(vcpu);
  2194. if (unlikely(vcpu->arch.mp_state == VCPU_MP_STATE_UNINITIALIZED)) {
  2195. kvm_vcpu_block(vcpu);
  2196. vcpu_put(vcpu);
  2197. return -EAGAIN;
  2198. }
  2199. if (vcpu->sigset_active)
  2200. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  2201. /* re-sync apic's tpr */
  2202. if (!irqchip_in_kernel(vcpu->kvm))
  2203. set_cr8(vcpu, kvm_run->cr8);
  2204. if (vcpu->arch.pio.cur_count) {
  2205. r = complete_pio(vcpu);
  2206. if (r)
  2207. goto out;
  2208. }
  2209. #if CONFIG_HAS_IOMEM
  2210. if (vcpu->mmio_needed) {
  2211. memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
  2212. vcpu->mmio_read_completed = 1;
  2213. vcpu->mmio_needed = 0;
  2214. r = emulate_instruction(vcpu, kvm_run,
  2215. vcpu->arch.mmio_fault_cr2, 0, 1);
  2216. if (r == EMULATE_DO_MMIO) {
  2217. /*
  2218. * Read-modify-write. Back to userspace.
  2219. */
  2220. r = 0;
  2221. goto out;
  2222. }
  2223. }
  2224. #endif
  2225. if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
  2226. kvm_x86_ops->cache_regs(vcpu);
  2227. vcpu->arch.regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
  2228. kvm_x86_ops->decache_regs(vcpu);
  2229. }
  2230. r = __vcpu_run(vcpu, kvm_run);
  2231. out:
  2232. if (vcpu->sigset_active)
  2233. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  2234. vcpu_put(vcpu);
  2235. return r;
  2236. }
  2237. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  2238. {
  2239. vcpu_load(vcpu);
  2240. kvm_x86_ops->cache_regs(vcpu);
  2241. regs->rax = vcpu->arch.regs[VCPU_REGS_RAX];
  2242. regs->rbx = vcpu->arch.regs[VCPU_REGS_RBX];
  2243. regs->rcx = vcpu->arch.regs[VCPU_REGS_RCX];
  2244. regs->rdx = vcpu->arch.regs[VCPU_REGS_RDX];
  2245. regs->rsi = vcpu->arch.regs[VCPU_REGS_RSI];
  2246. regs->rdi = vcpu->arch.regs[VCPU_REGS_RDI];
  2247. regs->rsp = vcpu->arch.regs[VCPU_REGS_RSP];
  2248. regs->rbp = vcpu->arch.regs[VCPU_REGS_RBP];
  2249. #ifdef CONFIG_X86_64
  2250. regs->r8 = vcpu->arch.regs[VCPU_REGS_R8];
  2251. regs->r9 = vcpu->arch.regs[VCPU_REGS_R9];
  2252. regs->r10 = vcpu->arch.regs[VCPU_REGS_R10];
  2253. regs->r11 = vcpu->arch.regs[VCPU_REGS_R11];
  2254. regs->r12 = vcpu->arch.regs[VCPU_REGS_R12];
  2255. regs->r13 = vcpu->arch.regs[VCPU_REGS_R13];
  2256. regs->r14 = vcpu->arch.regs[VCPU_REGS_R14];
  2257. regs->r15 = vcpu->arch.regs[VCPU_REGS_R15];
  2258. #endif
  2259. regs->rip = vcpu->arch.rip;
  2260. regs->rflags = kvm_x86_ops->get_rflags(vcpu);
  2261. /*
  2262. * Don't leak debug flags in case they were set for guest debugging
  2263. */
  2264. if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
  2265. regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
  2266. vcpu_put(vcpu);
  2267. return 0;
  2268. }
  2269. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  2270. {
  2271. vcpu_load(vcpu);
  2272. vcpu->arch.regs[VCPU_REGS_RAX] = regs->rax;
  2273. vcpu->arch.regs[VCPU_REGS_RBX] = regs->rbx;
  2274. vcpu->arch.regs[VCPU_REGS_RCX] = regs->rcx;
  2275. vcpu->arch.regs[VCPU_REGS_RDX] = regs->rdx;
  2276. vcpu->arch.regs[VCPU_REGS_RSI] = regs->rsi;
  2277. vcpu->arch.regs[VCPU_REGS_RDI] = regs->rdi;
  2278. vcpu->arch.regs[VCPU_REGS_RSP] = regs->rsp;
  2279. vcpu->arch.regs[VCPU_REGS_RBP] = regs->rbp;
  2280. #ifdef CONFIG_X86_64
  2281. vcpu->arch.regs[VCPU_REGS_R8] = regs->r8;
  2282. vcpu->arch.regs[VCPU_REGS_R9] = regs->r9;
  2283. vcpu->arch.regs[VCPU_REGS_R10] = regs->r10;
  2284. vcpu->arch.regs[VCPU_REGS_R11] = regs->r11;
  2285. vcpu->arch.regs[VCPU_REGS_R12] = regs->r12;
  2286. vcpu->arch.regs[VCPU_REGS_R13] = regs->r13;
  2287. vcpu->arch.regs[VCPU_REGS_R14] = regs->r14;
  2288. vcpu->arch.regs[VCPU_REGS_R15] = regs->r15;
  2289. #endif
  2290. vcpu->arch.rip = regs->rip;
  2291. kvm_x86_ops->set_rflags(vcpu, regs->rflags);
  2292. kvm_x86_ops->decache_regs(vcpu);
  2293. vcpu_put(vcpu);
  2294. return 0;
  2295. }
  2296. static void get_segment(struct kvm_vcpu *vcpu,
  2297. struct kvm_segment *var, int seg)
  2298. {
  2299. return kvm_x86_ops->get_segment(vcpu, var, seg);
  2300. }
  2301. void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  2302. {
  2303. struct kvm_segment cs;
  2304. get_segment(vcpu, &cs, VCPU_SREG_CS);
  2305. *db = cs.db;
  2306. *l = cs.l;
  2307. }
  2308. EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
  2309. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  2310. struct kvm_sregs *sregs)
  2311. {
  2312. struct descriptor_table dt;
  2313. int pending_vec;
  2314. vcpu_load(vcpu);
  2315. get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  2316. get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  2317. get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  2318. get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  2319. get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  2320. get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  2321. get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  2322. get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  2323. kvm_x86_ops->get_idt(vcpu, &dt);
  2324. sregs->idt.limit = dt.limit;
  2325. sregs->idt.base = dt.base;
  2326. kvm_x86_ops->get_gdt(vcpu, &dt);
  2327. sregs->gdt.limit = dt.limit;
  2328. sregs->gdt.base = dt.base;
  2329. kvm_x86_ops->decache_cr4_guest_bits(vcpu);
  2330. sregs->cr0 = vcpu->arch.cr0;
  2331. sregs->cr2 = vcpu->arch.cr2;
  2332. sregs->cr3 = vcpu->arch.cr3;
  2333. sregs->cr4 = vcpu->arch.cr4;
  2334. sregs->cr8 = get_cr8(vcpu);
  2335. sregs->efer = vcpu->arch.shadow_efer;
  2336. sregs->apic_base = kvm_get_apic_base(vcpu);
  2337. if (irqchip_in_kernel(vcpu->kvm)) {
  2338. memset(sregs->interrupt_bitmap, 0,
  2339. sizeof sregs->interrupt_bitmap);
  2340. pending_vec = kvm_x86_ops->get_irq(vcpu);
  2341. if (pending_vec >= 0)
  2342. set_bit(pending_vec,
  2343. (unsigned long *)sregs->interrupt_bitmap);
  2344. } else
  2345. memcpy(sregs->interrupt_bitmap, vcpu->arch.irq_pending,
  2346. sizeof sregs->interrupt_bitmap);
  2347. vcpu_put(vcpu);
  2348. return 0;
  2349. }
  2350. static void set_segment(struct kvm_vcpu *vcpu,
  2351. struct kvm_segment *var, int seg)
  2352. {
  2353. return kvm_x86_ops->set_segment(vcpu, var, seg);
  2354. }
  2355. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  2356. struct kvm_sregs *sregs)
  2357. {
  2358. int mmu_reset_needed = 0;
  2359. int i, pending_vec, max_bits;
  2360. struct descriptor_table dt;
  2361. vcpu_load(vcpu);
  2362. dt.limit = sregs->idt.limit;
  2363. dt.base = sregs->idt.base;
  2364. kvm_x86_ops->set_idt(vcpu, &dt);
  2365. dt.limit = sregs->gdt.limit;
  2366. dt.base = sregs->gdt.base;
  2367. kvm_x86_ops->set_gdt(vcpu, &dt);
  2368. vcpu->arch.cr2 = sregs->cr2;
  2369. mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
  2370. vcpu->arch.cr3 = sregs->cr3;
  2371. set_cr8(vcpu, sregs->cr8);
  2372. mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
  2373. #ifdef CONFIG_X86_64
  2374. kvm_x86_ops->set_efer(vcpu, sregs->efer);
  2375. #endif
  2376. kvm_set_apic_base(vcpu, sregs->apic_base);
  2377. kvm_x86_ops->decache_cr4_guest_bits(vcpu);
  2378. mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0;
  2379. vcpu->arch.cr0 = sregs->cr0;
  2380. kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
  2381. mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4;
  2382. kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
  2383. if (!is_long_mode(vcpu) && is_pae(vcpu))
  2384. load_pdptrs(vcpu, vcpu->arch.cr3);
  2385. if (mmu_reset_needed)
  2386. kvm_mmu_reset_context(vcpu);
  2387. if (!irqchip_in_kernel(vcpu->kvm)) {
  2388. memcpy(vcpu->arch.irq_pending, sregs->interrupt_bitmap,
  2389. sizeof vcpu->arch.irq_pending);
  2390. vcpu->arch.irq_summary = 0;
  2391. for (i = 0; i < ARRAY_SIZE(vcpu->arch.irq_pending); ++i)
  2392. if (vcpu->arch.irq_pending[i])
  2393. __set_bit(i, &vcpu->arch.irq_summary);
  2394. } else {
  2395. max_bits = (sizeof sregs->interrupt_bitmap) << 3;
  2396. pending_vec = find_first_bit(
  2397. (const unsigned long *)sregs->interrupt_bitmap,
  2398. max_bits);
  2399. /* Only pending external irq is handled here */
  2400. if (pending_vec < max_bits) {
  2401. kvm_x86_ops->set_irq(vcpu, pending_vec);
  2402. pr_debug("Set back pending irq %d\n",
  2403. pending_vec);
  2404. }
  2405. }
  2406. set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  2407. set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  2408. set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  2409. set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  2410. set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  2411. set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  2412. set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  2413. set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  2414. vcpu_put(vcpu);
  2415. return 0;
  2416. }
  2417. int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
  2418. struct kvm_debug_guest *dbg)
  2419. {
  2420. int r;
  2421. vcpu_load(vcpu);
  2422. r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
  2423. vcpu_put(vcpu);
  2424. return r;
  2425. }
  2426. /*
  2427. * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
  2428. * we have asm/x86/processor.h
  2429. */
  2430. struct fxsave {
  2431. u16 cwd;
  2432. u16 swd;
  2433. u16 twd;
  2434. u16 fop;
  2435. u64 rip;
  2436. u64 rdp;
  2437. u32 mxcsr;
  2438. u32 mxcsr_mask;
  2439. u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
  2440. #ifdef CONFIG_X86_64
  2441. u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
  2442. #else
  2443. u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
  2444. #endif
  2445. };
  2446. /*
  2447. * Translate a guest virtual address to a guest physical address.
  2448. */
  2449. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  2450. struct kvm_translation *tr)
  2451. {
  2452. unsigned long vaddr = tr->linear_address;
  2453. gpa_t gpa;
  2454. vcpu_load(vcpu);
  2455. mutex_lock(&vcpu->kvm->lock);
  2456. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
  2457. tr->physical_address = gpa;
  2458. tr->valid = gpa != UNMAPPED_GVA;
  2459. tr->writeable = 1;
  2460. tr->usermode = 0;
  2461. mutex_unlock(&vcpu->kvm->lock);
  2462. vcpu_put(vcpu);
  2463. return 0;
  2464. }
  2465. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  2466. {
  2467. struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
  2468. vcpu_load(vcpu);
  2469. memcpy(fpu->fpr, fxsave->st_space, 128);
  2470. fpu->fcw = fxsave->cwd;
  2471. fpu->fsw = fxsave->swd;
  2472. fpu->ftwx = fxsave->twd;
  2473. fpu->last_opcode = fxsave->fop;
  2474. fpu->last_ip = fxsave->rip;
  2475. fpu->last_dp = fxsave->rdp;
  2476. memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
  2477. vcpu_put(vcpu);
  2478. return 0;
  2479. }
  2480. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  2481. {
  2482. struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
  2483. vcpu_load(vcpu);
  2484. memcpy(fxsave->st_space, fpu->fpr, 128);
  2485. fxsave->cwd = fpu->fcw;
  2486. fxsave->swd = fpu->fsw;
  2487. fxsave->twd = fpu->ftwx;
  2488. fxsave->fop = fpu->last_opcode;
  2489. fxsave->rip = fpu->last_ip;
  2490. fxsave->rdp = fpu->last_dp;
  2491. memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
  2492. vcpu_put(vcpu);
  2493. return 0;
  2494. }
  2495. void fx_init(struct kvm_vcpu *vcpu)
  2496. {
  2497. unsigned after_mxcsr_mask;
  2498. /* Initialize guest FPU by resetting ours and saving into guest's */
  2499. preempt_disable();
  2500. fx_save(&vcpu->arch.host_fx_image);
  2501. fpu_init();
  2502. fx_save(&vcpu->arch.guest_fx_image);
  2503. fx_restore(&vcpu->arch.host_fx_image);
  2504. preempt_enable();
  2505. vcpu->arch.cr0 |= X86_CR0_ET;
  2506. after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
  2507. vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
  2508. memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
  2509. 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
  2510. }
  2511. EXPORT_SYMBOL_GPL(fx_init);
  2512. void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
  2513. {
  2514. if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
  2515. return;
  2516. vcpu->guest_fpu_loaded = 1;
  2517. fx_save(&vcpu->arch.host_fx_image);
  2518. fx_restore(&vcpu->arch.guest_fx_image);
  2519. }
  2520. EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
  2521. void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
  2522. {
  2523. if (!vcpu->guest_fpu_loaded)
  2524. return;
  2525. vcpu->guest_fpu_loaded = 0;
  2526. fx_save(&vcpu->arch.guest_fx_image);
  2527. fx_restore(&vcpu->arch.host_fx_image);
  2528. ++vcpu->stat.fpu_reload;
  2529. }
  2530. EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
  2531. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  2532. {
  2533. kvm_x86_ops->vcpu_free(vcpu);
  2534. }
  2535. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
  2536. unsigned int id)
  2537. {
  2538. return kvm_x86_ops->vcpu_create(kvm, id);
  2539. }
  2540. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
  2541. {
  2542. int r;
  2543. /* We do fxsave: this must be aligned. */
  2544. BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
  2545. vcpu_load(vcpu);
  2546. r = kvm_arch_vcpu_reset(vcpu);
  2547. if (r == 0)
  2548. r = kvm_mmu_setup(vcpu);
  2549. vcpu_put(vcpu);
  2550. if (r < 0)
  2551. goto free_vcpu;
  2552. return 0;
  2553. free_vcpu:
  2554. kvm_x86_ops->vcpu_free(vcpu);
  2555. return r;
  2556. }
  2557. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  2558. {
  2559. vcpu_load(vcpu);
  2560. kvm_mmu_unload(vcpu);
  2561. vcpu_put(vcpu);
  2562. kvm_x86_ops->vcpu_free(vcpu);
  2563. }
  2564. int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
  2565. {
  2566. return kvm_x86_ops->vcpu_reset(vcpu);
  2567. }
  2568. void kvm_arch_hardware_enable(void *garbage)
  2569. {
  2570. kvm_x86_ops->hardware_enable(garbage);
  2571. }
  2572. void kvm_arch_hardware_disable(void *garbage)
  2573. {
  2574. kvm_x86_ops->hardware_disable(garbage);
  2575. }
  2576. int kvm_arch_hardware_setup(void)
  2577. {
  2578. return kvm_x86_ops->hardware_setup();
  2579. }
  2580. void kvm_arch_hardware_unsetup(void)
  2581. {
  2582. kvm_x86_ops->hardware_unsetup();
  2583. }
  2584. void kvm_arch_check_processor_compat(void *rtn)
  2585. {
  2586. kvm_x86_ops->check_processor_compatibility(rtn);
  2587. }
  2588. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  2589. {
  2590. struct page *page;
  2591. struct kvm *kvm;
  2592. int r;
  2593. BUG_ON(vcpu->kvm == NULL);
  2594. kvm = vcpu->kvm;
  2595. vcpu->arch.mmu.root_hpa = INVALID_PAGE;
  2596. if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0)
  2597. vcpu->arch.mp_state = VCPU_MP_STATE_RUNNABLE;
  2598. else
  2599. vcpu->arch.mp_state = VCPU_MP_STATE_UNINITIALIZED;
  2600. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  2601. if (!page) {
  2602. r = -ENOMEM;
  2603. goto fail;
  2604. }
  2605. vcpu->arch.pio_data = page_address(page);
  2606. r = kvm_mmu_create(vcpu);
  2607. if (r < 0)
  2608. goto fail_free_pio_data;
  2609. if (irqchip_in_kernel(kvm)) {
  2610. r = kvm_create_lapic(vcpu);
  2611. if (r < 0)
  2612. goto fail_mmu_destroy;
  2613. }
  2614. return 0;
  2615. fail_mmu_destroy:
  2616. kvm_mmu_destroy(vcpu);
  2617. fail_free_pio_data:
  2618. free_page((unsigned long)vcpu->arch.pio_data);
  2619. fail:
  2620. return r;
  2621. }
  2622. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  2623. {
  2624. kvm_free_lapic(vcpu);
  2625. kvm_mmu_destroy(vcpu);
  2626. free_page((unsigned long)vcpu->arch.pio_data);
  2627. }
  2628. struct kvm *kvm_arch_create_vm(void)
  2629. {
  2630. struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
  2631. if (!kvm)
  2632. return ERR_PTR(-ENOMEM);
  2633. INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
  2634. return kvm;
  2635. }
  2636. static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
  2637. {
  2638. vcpu_load(vcpu);
  2639. kvm_mmu_unload(vcpu);
  2640. vcpu_put(vcpu);
  2641. }
  2642. static void kvm_free_vcpus(struct kvm *kvm)
  2643. {
  2644. unsigned int i;
  2645. /*
  2646. * Unpin any mmu pages first.
  2647. */
  2648. for (i = 0; i < KVM_MAX_VCPUS; ++i)
  2649. if (kvm->vcpus[i])
  2650. kvm_unload_vcpu_mmu(kvm->vcpus[i]);
  2651. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  2652. if (kvm->vcpus[i]) {
  2653. kvm_arch_vcpu_free(kvm->vcpus[i]);
  2654. kvm->vcpus[i] = NULL;
  2655. }
  2656. }
  2657. }
  2658. void kvm_arch_destroy_vm(struct kvm *kvm)
  2659. {
  2660. kfree(kvm->vpic);
  2661. kfree(kvm->vioapic);
  2662. kvm_free_vcpus(kvm);
  2663. kvm_free_physmem(kvm);
  2664. kfree(kvm);
  2665. }
  2666. int kvm_arch_set_memory_region(struct kvm *kvm,
  2667. struct kvm_userspace_memory_region *mem,
  2668. struct kvm_memory_slot old,
  2669. int user_alloc)
  2670. {
  2671. int npages = mem->memory_size >> PAGE_SHIFT;
  2672. struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
  2673. /*To keep backward compatibility with older userspace,
  2674. *x86 needs to hanlde !user_alloc case.
  2675. */
  2676. if (!user_alloc) {
  2677. if (npages && !old.rmap) {
  2678. down_write(&current->mm->mmap_sem);
  2679. memslot->userspace_addr = do_mmap(NULL, 0,
  2680. npages * PAGE_SIZE,
  2681. PROT_READ | PROT_WRITE,
  2682. MAP_SHARED | MAP_ANONYMOUS,
  2683. 0);
  2684. up_write(&current->mm->mmap_sem);
  2685. if (IS_ERR((void *)memslot->userspace_addr))
  2686. return PTR_ERR((void *)memslot->userspace_addr);
  2687. } else {
  2688. if (!old.user_alloc && old.rmap) {
  2689. int ret;
  2690. down_write(&current->mm->mmap_sem);
  2691. ret = do_munmap(current->mm, old.userspace_addr,
  2692. old.npages * PAGE_SIZE);
  2693. up_write(&current->mm->mmap_sem);
  2694. if (ret < 0)
  2695. printk(KERN_WARNING
  2696. "kvm_vm_ioctl_set_memory_region: "
  2697. "failed to munmap memory\n");
  2698. }
  2699. }
  2700. }
  2701. if (!kvm->arch.n_requested_mmu_pages) {
  2702. unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
  2703. kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
  2704. }
  2705. kvm_mmu_slot_remove_write_access(kvm, mem->slot);
  2706. kvm_flush_remote_tlbs(kvm);
  2707. return 0;
  2708. }
  2709. int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
  2710. {
  2711. return vcpu->arch.mp_state == VCPU_MP_STATE_RUNNABLE
  2712. || vcpu->arch.mp_state == VCPU_MP_STATE_SIPI_RECEIVED;
  2713. }