x86.c 71 KB

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