kvm_main.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * This module enables machines with Intel VT-x extensions to run virtual
  5. * machines without emulation or binary translation.
  6. *
  7. * Copyright (C) 2006 Qumranet, Inc.
  8. *
  9. * Authors:
  10. * Avi Kivity <avi@qumranet.com>
  11. * Yaniv Kamay <yaniv@qumranet.com>
  12. *
  13. * This work is licensed under the terms of the GNU GPL, version 2. See
  14. * the COPYING file in the top-level directory.
  15. *
  16. */
  17. #include "kvm.h"
  18. #include <linux/kvm.h>
  19. #include <linux/module.h>
  20. #include <linux/errno.h>
  21. #include <linux/magic.h>
  22. #include <asm/processor.h>
  23. #include <linux/percpu.h>
  24. #include <linux/gfp.h>
  25. #include <asm/msr.h>
  26. #include <linux/mm.h>
  27. #include <linux/miscdevice.h>
  28. #include <linux/vmalloc.h>
  29. #include <asm/uaccess.h>
  30. #include <linux/reboot.h>
  31. #include <asm/io.h>
  32. #include <linux/debugfs.h>
  33. #include <linux/highmem.h>
  34. #include <linux/file.h>
  35. #include <asm/desc.h>
  36. #include <linux/sysdev.h>
  37. #include <linux/cpu.h>
  38. #include <linux/file.h>
  39. #include <linux/fs.h>
  40. #include <linux/mount.h>
  41. #include <linux/sched.h>
  42. #include "x86_emulate.h"
  43. #include "segment_descriptor.h"
  44. MODULE_AUTHOR("Qumranet");
  45. MODULE_LICENSE("GPL");
  46. static DEFINE_SPINLOCK(kvm_lock);
  47. static LIST_HEAD(vm_list);
  48. struct kvm_arch_ops *kvm_arch_ops;
  49. #define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
  50. static struct kvm_stats_debugfs_item {
  51. const char *name;
  52. int offset;
  53. struct dentry *dentry;
  54. } debugfs_entries[] = {
  55. { "pf_fixed", STAT_OFFSET(pf_fixed) },
  56. { "pf_guest", STAT_OFFSET(pf_guest) },
  57. { "tlb_flush", STAT_OFFSET(tlb_flush) },
  58. { "invlpg", STAT_OFFSET(invlpg) },
  59. { "exits", STAT_OFFSET(exits) },
  60. { "io_exits", STAT_OFFSET(io_exits) },
  61. { "mmio_exits", STAT_OFFSET(mmio_exits) },
  62. { "signal_exits", STAT_OFFSET(signal_exits) },
  63. { "irq_window", STAT_OFFSET(irq_window_exits) },
  64. { "halt_exits", STAT_OFFSET(halt_exits) },
  65. { "request_irq", STAT_OFFSET(request_irq_exits) },
  66. { "irq_exits", STAT_OFFSET(irq_exits) },
  67. { NULL }
  68. };
  69. static struct dentry *debugfs_dir;
  70. struct vfsmount *kvmfs_mnt;
  71. #define MAX_IO_MSRS 256
  72. #define CR0_RESEVED_BITS 0xffffffff1ffaffc0ULL
  73. #define LMSW_GUEST_MASK 0x0eULL
  74. #define CR4_RESEVED_BITS (~((1ULL << 11) - 1))
  75. #define CR8_RESEVED_BITS (~0x0fULL)
  76. #define EFER_RESERVED_BITS 0xfffffffffffff2fe
  77. #ifdef CONFIG_X86_64
  78. // LDT or TSS descriptor in the GDT. 16 bytes.
  79. struct segment_descriptor_64 {
  80. struct segment_descriptor s;
  81. u32 base_higher;
  82. u32 pad_zero;
  83. };
  84. #endif
  85. static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
  86. unsigned long arg);
  87. static struct inode *kvmfs_inode(struct file_operations *fops)
  88. {
  89. int error = -ENOMEM;
  90. struct inode *inode = new_inode(kvmfs_mnt->mnt_sb);
  91. if (!inode)
  92. goto eexit_1;
  93. inode->i_fop = fops;
  94. /*
  95. * Mark the inode dirty from the very beginning,
  96. * that way it will never be moved to the dirty
  97. * list because mark_inode_dirty() will think
  98. * that it already _is_ on the dirty list.
  99. */
  100. inode->i_state = I_DIRTY;
  101. inode->i_mode = S_IRUSR | S_IWUSR;
  102. inode->i_uid = current->fsuid;
  103. inode->i_gid = current->fsgid;
  104. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  105. return inode;
  106. eexit_1:
  107. return ERR_PTR(error);
  108. }
  109. static struct file *kvmfs_file(struct inode *inode, void *private_data)
  110. {
  111. struct file *file = get_empty_filp();
  112. if (!file)
  113. return ERR_PTR(-ENFILE);
  114. file->f_path.mnt = mntget(kvmfs_mnt);
  115. file->f_path.dentry = d_alloc_anon(inode);
  116. if (!file->f_path.dentry)
  117. return ERR_PTR(-ENOMEM);
  118. file->f_mapping = inode->i_mapping;
  119. file->f_pos = 0;
  120. file->f_flags = O_RDWR;
  121. file->f_op = inode->i_fop;
  122. file->f_mode = FMODE_READ | FMODE_WRITE;
  123. file->f_version = 0;
  124. file->private_data = private_data;
  125. return file;
  126. }
  127. unsigned long segment_base(u16 selector)
  128. {
  129. struct descriptor_table gdt;
  130. struct segment_descriptor *d;
  131. unsigned long table_base;
  132. typedef unsigned long ul;
  133. unsigned long v;
  134. if (selector == 0)
  135. return 0;
  136. asm ("sgdt %0" : "=m"(gdt));
  137. table_base = gdt.base;
  138. if (selector & 4) { /* from ldt */
  139. u16 ldt_selector;
  140. asm ("sldt %0" : "=g"(ldt_selector));
  141. table_base = segment_base(ldt_selector);
  142. }
  143. d = (struct segment_descriptor *)(table_base + (selector & ~7));
  144. v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
  145. #ifdef CONFIG_X86_64
  146. if (d->system == 0
  147. && (d->type == 2 || d->type == 9 || d->type == 11))
  148. v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
  149. #endif
  150. return v;
  151. }
  152. EXPORT_SYMBOL_GPL(segment_base);
  153. static inline int valid_vcpu(int n)
  154. {
  155. return likely(n >= 0 && n < KVM_MAX_VCPUS);
  156. }
  157. int kvm_read_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
  158. void *dest)
  159. {
  160. unsigned char *host_buf = dest;
  161. unsigned long req_size = size;
  162. while (size) {
  163. hpa_t paddr;
  164. unsigned now;
  165. unsigned offset;
  166. hva_t guest_buf;
  167. paddr = gva_to_hpa(vcpu, addr);
  168. if (is_error_hpa(paddr))
  169. break;
  170. guest_buf = (hva_t)kmap_atomic(
  171. pfn_to_page(paddr >> PAGE_SHIFT),
  172. KM_USER0);
  173. offset = addr & ~PAGE_MASK;
  174. guest_buf |= offset;
  175. now = min(size, PAGE_SIZE - offset);
  176. memcpy(host_buf, (void*)guest_buf, now);
  177. host_buf += now;
  178. addr += now;
  179. size -= now;
  180. kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
  181. }
  182. return req_size - size;
  183. }
  184. EXPORT_SYMBOL_GPL(kvm_read_guest);
  185. int kvm_write_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
  186. void *data)
  187. {
  188. unsigned char *host_buf = data;
  189. unsigned long req_size = size;
  190. while (size) {
  191. hpa_t paddr;
  192. unsigned now;
  193. unsigned offset;
  194. hva_t guest_buf;
  195. gfn_t gfn;
  196. paddr = gva_to_hpa(vcpu, addr);
  197. if (is_error_hpa(paddr))
  198. break;
  199. gfn = vcpu->mmu.gva_to_gpa(vcpu, addr) >> PAGE_SHIFT;
  200. mark_page_dirty(vcpu->kvm, gfn);
  201. guest_buf = (hva_t)kmap_atomic(
  202. pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0);
  203. offset = addr & ~PAGE_MASK;
  204. guest_buf |= offset;
  205. now = min(size, PAGE_SIZE - offset);
  206. memcpy((void*)guest_buf, host_buf, now);
  207. host_buf += now;
  208. addr += now;
  209. size -= now;
  210. kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
  211. }
  212. return req_size - size;
  213. }
  214. EXPORT_SYMBOL_GPL(kvm_write_guest);
  215. /*
  216. * Switches to specified vcpu, until a matching vcpu_put()
  217. */
  218. static void vcpu_load(struct kvm_vcpu *vcpu)
  219. {
  220. mutex_lock(&vcpu->mutex);
  221. kvm_arch_ops->vcpu_load(vcpu);
  222. }
  223. /*
  224. * Switches to specified vcpu, until a matching vcpu_put(). Will return NULL
  225. * if the slot is not populated.
  226. */
  227. static struct kvm_vcpu *vcpu_load_slot(struct kvm *kvm, int slot)
  228. {
  229. struct kvm_vcpu *vcpu = &kvm->vcpus[slot];
  230. mutex_lock(&vcpu->mutex);
  231. if (!vcpu->vmcs) {
  232. mutex_unlock(&vcpu->mutex);
  233. return NULL;
  234. }
  235. kvm_arch_ops->vcpu_load(vcpu);
  236. return vcpu;
  237. }
  238. static void vcpu_put(struct kvm_vcpu *vcpu)
  239. {
  240. kvm_arch_ops->vcpu_put(vcpu);
  241. mutex_unlock(&vcpu->mutex);
  242. }
  243. static struct kvm *kvm_create_vm(void)
  244. {
  245. struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
  246. int i;
  247. if (!kvm)
  248. return ERR_PTR(-ENOMEM);
  249. spin_lock_init(&kvm->lock);
  250. INIT_LIST_HEAD(&kvm->active_mmu_pages);
  251. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  252. struct kvm_vcpu *vcpu = &kvm->vcpus[i];
  253. mutex_init(&vcpu->mutex);
  254. vcpu->cpu = -1;
  255. vcpu->kvm = kvm;
  256. vcpu->mmu.root_hpa = INVALID_PAGE;
  257. INIT_LIST_HEAD(&vcpu->free_pages);
  258. spin_lock(&kvm_lock);
  259. list_add(&kvm->vm_list, &vm_list);
  260. spin_unlock(&kvm_lock);
  261. }
  262. return kvm;
  263. }
  264. static int kvm_dev_open(struct inode *inode, struct file *filp)
  265. {
  266. return 0;
  267. }
  268. /*
  269. * Free any memory in @free but not in @dont.
  270. */
  271. static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
  272. struct kvm_memory_slot *dont)
  273. {
  274. int i;
  275. if (!dont || free->phys_mem != dont->phys_mem)
  276. if (free->phys_mem) {
  277. for (i = 0; i < free->npages; ++i)
  278. if (free->phys_mem[i])
  279. __free_page(free->phys_mem[i]);
  280. vfree(free->phys_mem);
  281. }
  282. if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
  283. vfree(free->dirty_bitmap);
  284. free->phys_mem = NULL;
  285. free->npages = 0;
  286. free->dirty_bitmap = NULL;
  287. }
  288. static void kvm_free_physmem(struct kvm *kvm)
  289. {
  290. int i;
  291. for (i = 0; i < kvm->nmemslots; ++i)
  292. kvm_free_physmem_slot(&kvm->memslots[i], NULL);
  293. }
  294. static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
  295. {
  296. int i;
  297. for (i = 0; i < 2; ++i)
  298. if (vcpu->pio.guest_pages[i]) {
  299. __free_page(vcpu->pio.guest_pages[i]);
  300. vcpu->pio.guest_pages[i] = NULL;
  301. }
  302. }
  303. static void kvm_free_vcpu(struct kvm_vcpu *vcpu)
  304. {
  305. if (!vcpu->vmcs)
  306. return;
  307. vcpu_load(vcpu);
  308. kvm_mmu_destroy(vcpu);
  309. vcpu_put(vcpu);
  310. kvm_arch_ops->vcpu_free(vcpu);
  311. free_page((unsigned long)vcpu->run);
  312. vcpu->run = NULL;
  313. free_page((unsigned long)vcpu->pio_data);
  314. vcpu->pio_data = NULL;
  315. free_pio_guest_pages(vcpu);
  316. }
  317. static void kvm_free_vcpus(struct kvm *kvm)
  318. {
  319. unsigned int i;
  320. for (i = 0; i < KVM_MAX_VCPUS; ++i)
  321. kvm_free_vcpu(&kvm->vcpus[i]);
  322. }
  323. static int kvm_dev_release(struct inode *inode, struct file *filp)
  324. {
  325. return 0;
  326. }
  327. static void kvm_destroy_vm(struct kvm *kvm)
  328. {
  329. spin_lock(&kvm_lock);
  330. list_del(&kvm->vm_list);
  331. spin_unlock(&kvm_lock);
  332. kvm_free_vcpus(kvm);
  333. kvm_free_physmem(kvm);
  334. kfree(kvm);
  335. }
  336. static int kvm_vm_release(struct inode *inode, struct file *filp)
  337. {
  338. struct kvm *kvm = filp->private_data;
  339. kvm_destroy_vm(kvm);
  340. return 0;
  341. }
  342. static void inject_gp(struct kvm_vcpu *vcpu)
  343. {
  344. kvm_arch_ops->inject_gp(vcpu, 0);
  345. }
  346. /*
  347. * Load the pae pdptrs. Return true is they are all valid.
  348. */
  349. static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
  350. {
  351. gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
  352. unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
  353. int i;
  354. u64 pdpte;
  355. u64 *pdpt;
  356. int ret;
  357. struct page *page;
  358. spin_lock(&vcpu->kvm->lock);
  359. page = gfn_to_page(vcpu->kvm, pdpt_gfn);
  360. /* FIXME: !page - emulate? 0xff? */
  361. pdpt = kmap_atomic(page, KM_USER0);
  362. ret = 1;
  363. for (i = 0; i < 4; ++i) {
  364. pdpte = pdpt[offset + i];
  365. if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull)) {
  366. ret = 0;
  367. goto out;
  368. }
  369. }
  370. for (i = 0; i < 4; ++i)
  371. vcpu->pdptrs[i] = pdpt[offset + i];
  372. out:
  373. kunmap_atomic(pdpt, KM_USER0);
  374. spin_unlock(&vcpu->kvm->lock);
  375. return ret;
  376. }
  377. void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  378. {
  379. if (cr0 & CR0_RESEVED_BITS) {
  380. printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
  381. cr0, vcpu->cr0);
  382. inject_gp(vcpu);
  383. return;
  384. }
  385. if ((cr0 & CR0_NW_MASK) && !(cr0 & CR0_CD_MASK)) {
  386. printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
  387. inject_gp(vcpu);
  388. return;
  389. }
  390. if ((cr0 & CR0_PG_MASK) && !(cr0 & CR0_PE_MASK)) {
  391. printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
  392. "and a clear PE flag\n");
  393. inject_gp(vcpu);
  394. return;
  395. }
  396. if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK)) {
  397. #ifdef CONFIG_X86_64
  398. if ((vcpu->shadow_efer & EFER_LME)) {
  399. int cs_db, cs_l;
  400. if (!is_pae(vcpu)) {
  401. printk(KERN_DEBUG "set_cr0: #GP, start paging "
  402. "in long mode while PAE is disabled\n");
  403. inject_gp(vcpu);
  404. return;
  405. }
  406. kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  407. if (cs_l) {
  408. printk(KERN_DEBUG "set_cr0: #GP, start paging "
  409. "in long mode while CS.L == 1\n");
  410. inject_gp(vcpu);
  411. return;
  412. }
  413. } else
  414. #endif
  415. if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
  416. printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
  417. "reserved bits\n");
  418. inject_gp(vcpu);
  419. return;
  420. }
  421. }
  422. kvm_arch_ops->set_cr0(vcpu, cr0);
  423. vcpu->cr0 = cr0;
  424. spin_lock(&vcpu->kvm->lock);
  425. kvm_mmu_reset_context(vcpu);
  426. spin_unlock(&vcpu->kvm->lock);
  427. return;
  428. }
  429. EXPORT_SYMBOL_GPL(set_cr0);
  430. void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
  431. {
  432. set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
  433. }
  434. EXPORT_SYMBOL_GPL(lmsw);
  435. void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  436. {
  437. if (cr4 & CR4_RESEVED_BITS) {
  438. printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
  439. inject_gp(vcpu);
  440. return;
  441. }
  442. if (is_long_mode(vcpu)) {
  443. if (!(cr4 & CR4_PAE_MASK)) {
  444. printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
  445. "in long mode\n");
  446. inject_gp(vcpu);
  447. return;
  448. }
  449. } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & CR4_PAE_MASK)
  450. && !load_pdptrs(vcpu, vcpu->cr3)) {
  451. printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
  452. inject_gp(vcpu);
  453. }
  454. if (cr4 & CR4_VMXE_MASK) {
  455. printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
  456. inject_gp(vcpu);
  457. return;
  458. }
  459. kvm_arch_ops->set_cr4(vcpu, cr4);
  460. spin_lock(&vcpu->kvm->lock);
  461. kvm_mmu_reset_context(vcpu);
  462. spin_unlock(&vcpu->kvm->lock);
  463. }
  464. EXPORT_SYMBOL_GPL(set_cr4);
  465. void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  466. {
  467. if (is_long_mode(vcpu)) {
  468. if (cr3 & CR3_L_MODE_RESEVED_BITS) {
  469. printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
  470. inject_gp(vcpu);
  471. return;
  472. }
  473. } else {
  474. if (cr3 & CR3_RESEVED_BITS) {
  475. printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
  476. inject_gp(vcpu);
  477. return;
  478. }
  479. if (is_paging(vcpu) && is_pae(vcpu) &&
  480. !load_pdptrs(vcpu, cr3)) {
  481. printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
  482. "reserved bits\n");
  483. inject_gp(vcpu);
  484. return;
  485. }
  486. }
  487. vcpu->cr3 = cr3;
  488. spin_lock(&vcpu->kvm->lock);
  489. /*
  490. * Does the new cr3 value map to physical memory? (Note, we
  491. * catch an invalid cr3 even in real-mode, because it would
  492. * cause trouble later on when we turn on paging anyway.)
  493. *
  494. * A real CPU would silently accept an invalid cr3 and would
  495. * attempt to use it - with largely undefined (and often hard
  496. * to debug) behavior on the guest side.
  497. */
  498. if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
  499. inject_gp(vcpu);
  500. else
  501. vcpu->mmu.new_cr3(vcpu);
  502. spin_unlock(&vcpu->kvm->lock);
  503. }
  504. EXPORT_SYMBOL_GPL(set_cr3);
  505. void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
  506. {
  507. if ( cr8 & CR8_RESEVED_BITS) {
  508. printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
  509. inject_gp(vcpu);
  510. return;
  511. }
  512. vcpu->cr8 = cr8;
  513. }
  514. EXPORT_SYMBOL_GPL(set_cr8);
  515. void fx_init(struct kvm_vcpu *vcpu)
  516. {
  517. struct __attribute__ ((__packed__)) fx_image_s {
  518. u16 control; //fcw
  519. u16 status; //fsw
  520. u16 tag; // ftw
  521. u16 opcode; //fop
  522. u64 ip; // fpu ip
  523. u64 operand;// fpu dp
  524. u32 mxcsr;
  525. u32 mxcsr_mask;
  526. } *fx_image;
  527. fx_save(vcpu->host_fx_image);
  528. fpu_init();
  529. fx_save(vcpu->guest_fx_image);
  530. fx_restore(vcpu->host_fx_image);
  531. fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
  532. fx_image->mxcsr = 0x1f80;
  533. memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
  534. 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
  535. }
  536. EXPORT_SYMBOL_GPL(fx_init);
  537. static void do_remove_write_access(struct kvm_vcpu *vcpu, int slot)
  538. {
  539. spin_lock(&vcpu->kvm->lock);
  540. kvm_mmu_slot_remove_write_access(vcpu, slot);
  541. spin_unlock(&vcpu->kvm->lock);
  542. }
  543. /*
  544. * Allocate some memory and give it an address in the guest physical address
  545. * space.
  546. *
  547. * Discontiguous memory is allowed, mostly for framebuffers.
  548. */
  549. static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
  550. struct kvm_memory_region *mem)
  551. {
  552. int r;
  553. gfn_t base_gfn;
  554. unsigned long npages;
  555. unsigned long i;
  556. struct kvm_memory_slot *memslot;
  557. struct kvm_memory_slot old, new;
  558. int memory_config_version;
  559. r = -EINVAL;
  560. /* General sanity checks */
  561. if (mem->memory_size & (PAGE_SIZE - 1))
  562. goto out;
  563. if (mem->guest_phys_addr & (PAGE_SIZE - 1))
  564. goto out;
  565. if (mem->slot >= KVM_MEMORY_SLOTS)
  566. goto out;
  567. if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
  568. goto out;
  569. memslot = &kvm->memslots[mem->slot];
  570. base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
  571. npages = mem->memory_size >> PAGE_SHIFT;
  572. if (!npages)
  573. mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
  574. raced:
  575. spin_lock(&kvm->lock);
  576. memory_config_version = kvm->memory_config_version;
  577. new = old = *memslot;
  578. new.base_gfn = base_gfn;
  579. new.npages = npages;
  580. new.flags = mem->flags;
  581. /* Disallow changing a memory slot's size. */
  582. r = -EINVAL;
  583. if (npages && old.npages && npages != old.npages)
  584. goto out_unlock;
  585. /* Check for overlaps */
  586. r = -EEXIST;
  587. for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
  588. struct kvm_memory_slot *s = &kvm->memslots[i];
  589. if (s == memslot)
  590. continue;
  591. if (!((base_gfn + npages <= s->base_gfn) ||
  592. (base_gfn >= s->base_gfn + s->npages)))
  593. goto out_unlock;
  594. }
  595. /*
  596. * Do memory allocations outside lock. memory_config_version will
  597. * detect any races.
  598. */
  599. spin_unlock(&kvm->lock);
  600. /* Deallocate if slot is being removed */
  601. if (!npages)
  602. new.phys_mem = NULL;
  603. /* Free page dirty bitmap if unneeded */
  604. if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
  605. new.dirty_bitmap = NULL;
  606. r = -ENOMEM;
  607. /* Allocate if a slot is being created */
  608. if (npages && !new.phys_mem) {
  609. new.phys_mem = vmalloc(npages * sizeof(struct page *));
  610. if (!new.phys_mem)
  611. goto out_free;
  612. memset(new.phys_mem, 0, npages * sizeof(struct page *));
  613. for (i = 0; i < npages; ++i) {
  614. new.phys_mem[i] = alloc_page(GFP_HIGHUSER
  615. | __GFP_ZERO);
  616. if (!new.phys_mem[i])
  617. goto out_free;
  618. set_page_private(new.phys_mem[i],0);
  619. }
  620. }
  621. /* Allocate page dirty bitmap if needed */
  622. if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
  623. unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
  624. new.dirty_bitmap = vmalloc(dirty_bytes);
  625. if (!new.dirty_bitmap)
  626. goto out_free;
  627. memset(new.dirty_bitmap, 0, dirty_bytes);
  628. }
  629. spin_lock(&kvm->lock);
  630. if (memory_config_version != kvm->memory_config_version) {
  631. spin_unlock(&kvm->lock);
  632. kvm_free_physmem_slot(&new, &old);
  633. goto raced;
  634. }
  635. r = -EAGAIN;
  636. if (kvm->busy)
  637. goto out_unlock;
  638. if (mem->slot >= kvm->nmemslots)
  639. kvm->nmemslots = mem->slot + 1;
  640. *memslot = new;
  641. ++kvm->memory_config_version;
  642. spin_unlock(&kvm->lock);
  643. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  644. struct kvm_vcpu *vcpu;
  645. vcpu = vcpu_load_slot(kvm, i);
  646. if (!vcpu)
  647. continue;
  648. if (new.flags & KVM_MEM_LOG_DIRTY_PAGES)
  649. do_remove_write_access(vcpu, mem->slot);
  650. kvm_mmu_reset_context(vcpu);
  651. vcpu_put(vcpu);
  652. }
  653. kvm_free_physmem_slot(&old, &new);
  654. return 0;
  655. out_unlock:
  656. spin_unlock(&kvm->lock);
  657. out_free:
  658. kvm_free_physmem_slot(&new, &old);
  659. out:
  660. return r;
  661. }
  662. /*
  663. * Get (and clear) the dirty memory log for a memory slot.
  664. */
  665. static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
  666. struct kvm_dirty_log *log)
  667. {
  668. struct kvm_memory_slot *memslot;
  669. int r, i;
  670. int n;
  671. int cleared;
  672. unsigned long any = 0;
  673. spin_lock(&kvm->lock);
  674. /*
  675. * Prevent changes to guest memory configuration even while the lock
  676. * is not taken.
  677. */
  678. ++kvm->busy;
  679. spin_unlock(&kvm->lock);
  680. r = -EINVAL;
  681. if (log->slot >= KVM_MEMORY_SLOTS)
  682. goto out;
  683. memslot = &kvm->memslots[log->slot];
  684. r = -ENOENT;
  685. if (!memslot->dirty_bitmap)
  686. goto out;
  687. n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
  688. for (i = 0; !any && i < n/sizeof(long); ++i)
  689. any = memslot->dirty_bitmap[i];
  690. r = -EFAULT;
  691. if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
  692. goto out;
  693. if (any) {
  694. cleared = 0;
  695. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  696. struct kvm_vcpu *vcpu;
  697. vcpu = vcpu_load_slot(kvm, i);
  698. if (!vcpu)
  699. continue;
  700. if (!cleared) {
  701. do_remove_write_access(vcpu, log->slot);
  702. memset(memslot->dirty_bitmap, 0, n);
  703. cleared = 1;
  704. }
  705. kvm_arch_ops->tlb_flush(vcpu);
  706. vcpu_put(vcpu);
  707. }
  708. }
  709. r = 0;
  710. out:
  711. spin_lock(&kvm->lock);
  712. --kvm->busy;
  713. spin_unlock(&kvm->lock);
  714. return r;
  715. }
  716. /*
  717. * Set a new alias region. Aliases map a portion of physical memory into
  718. * another portion. This is useful for memory windows, for example the PC
  719. * VGA region.
  720. */
  721. static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
  722. struct kvm_memory_alias *alias)
  723. {
  724. int r, n;
  725. struct kvm_mem_alias *p;
  726. r = -EINVAL;
  727. /* General sanity checks */
  728. if (alias->memory_size & (PAGE_SIZE - 1))
  729. goto out;
  730. if (alias->guest_phys_addr & (PAGE_SIZE - 1))
  731. goto out;
  732. if (alias->slot >= KVM_ALIAS_SLOTS)
  733. goto out;
  734. if (alias->guest_phys_addr + alias->memory_size
  735. < alias->guest_phys_addr)
  736. goto out;
  737. if (alias->target_phys_addr + alias->memory_size
  738. < alias->target_phys_addr)
  739. goto out;
  740. spin_lock(&kvm->lock);
  741. p = &kvm->aliases[alias->slot];
  742. p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
  743. p->npages = alias->memory_size >> PAGE_SHIFT;
  744. p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
  745. for (n = KVM_ALIAS_SLOTS; n > 0; --n)
  746. if (kvm->aliases[n - 1].npages)
  747. break;
  748. kvm->naliases = n;
  749. spin_unlock(&kvm->lock);
  750. vcpu_load(&kvm->vcpus[0]);
  751. spin_lock(&kvm->lock);
  752. kvm_mmu_zap_all(&kvm->vcpus[0]);
  753. spin_unlock(&kvm->lock);
  754. vcpu_put(&kvm->vcpus[0]);
  755. return 0;
  756. out:
  757. return r;
  758. }
  759. static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
  760. {
  761. int i;
  762. struct kvm_mem_alias *alias;
  763. for (i = 0; i < kvm->naliases; ++i) {
  764. alias = &kvm->aliases[i];
  765. if (gfn >= alias->base_gfn
  766. && gfn < alias->base_gfn + alias->npages)
  767. return alias->target_gfn + gfn - alias->base_gfn;
  768. }
  769. return gfn;
  770. }
  771. static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
  772. {
  773. int i;
  774. for (i = 0; i < kvm->nmemslots; ++i) {
  775. struct kvm_memory_slot *memslot = &kvm->memslots[i];
  776. if (gfn >= memslot->base_gfn
  777. && gfn < memslot->base_gfn + memslot->npages)
  778. return memslot;
  779. }
  780. return NULL;
  781. }
  782. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
  783. {
  784. gfn = unalias_gfn(kvm, gfn);
  785. return __gfn_to_memslot(kvm, gfn);
  786. }
  787. struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
  788. {
  789. struct kvm_memory_slot *slot;
  790. gfn = unalias_gfn(kvm, gfn);
  791. slot = __gfn_to_memslot(kvm, gfn);
  792. if (!slot)
  793. return NULL;
  794. return slot->phys_mem[gfn - slot->base_gfn];
  795. }
  796. EXPORT_SYMBOL_GPL(gfn_to_page);
  797. void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
  798. {
  799. int i;
  800. struct kvm_memory_slot *memslot = NULL;
  801. unsigned long rel_gfn;
  802. for (i = 0; i < kvm->nmemslots; ++i) {
  803. memslot = &kvm->memslots[i];
  804. if (gfn >= memslot->base_gfn
  805. && gfn < memslot->base_gfn + memslot->npages) {
  806. if (!memslot || !memslot->dirty_bitmap)
  807. return;
  808. rel_gfn = gfn - memslot->base_gfn;
  809. /* avoid RMW */
  810. if (!test_bit(rel_gfn, memslot->dirty_bitmap))
  811. set_bit(rel_gfn, memslot->dirty_bitmap);
  812. return;
  813. }
  814. }
  815. }
  816. static int emulator_read_std(unsigned long addr,
  817. void *val,
  818. unsigned int bytes,
  819. struct x86_emulate_ctxt *ctxt)
  820. {
  821. struct kvm_vcpu *vcpu = ctxt->vcpu;
  822. void *data = val;
  823. while (bytes) {
  824. gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
  825. unsigned offset = addr & (PAGE_SIZE-1);
  826. unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
  827. unsigned long pfn;
  828. struct page *page;
  829. void *page_virt;
  830. if (gpa == UNMAPPED_GVA)
  831. return X86EMUL_PROPAGATE_FAULT;
  832. pfn = gpa >> PAGE_SHIFT;
  833. page = gfn_to_page(vcpu->kvm, pfn);
  834. if (!page)
  835. return X86EMUL_UNHANDLEABLE;
  836. page_virt = kmap_atomic(page, KM_USER0);
  837. memcpy(data, page_virt + offset, tocopy);
  838. kunmap_atomic(page_virt, KM_USER0);
  839. bytes -= tocopy;
  840. data += tocopy;
  841. addr += tocopy;
  842. }
  843. return X86EMUL_CONTINUE;
  844. }
  845. static int emulator_write_std(unsigned long addr,
  846. const void *val,
  847. unsigned int bytes,
  848. struct x86_emulate_ctxt *ctxt)
  849. {
  850. printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
  851. addr, bytes);
  852. return X86EMUL_UNHANDLEABLE;
  853. }
  854. static int emulator_read_emulated(unsigned long addr,
  855. void *val,
  856. unsigned int bytes,
  857. struct x86_emulate_ctxt *ctxt)
  858. {
  859. struct kvm_vcpu *vcpu = ctxt->vcpu;
  860. if (vcpu->mmio_read_completed) {
  861. memcpy(val, vcpu->mmio_data, bytes);
  862. vcpu->mmio_read_completed = 0;
  863. return X86EMUL_CONTINUE;
  864. } else if (emulator_read_std(addr, val, bytes, ctxt)
  865. == X86EMUL_CONTINUE)
  866. return X86EMUL_CONTINUE;
  867. else {
  868. gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
  869. if (gpa == UNMAPPED_GVA)
  870. return X86EMUL_PROPAGATE_FAULT;
  871. vcpu->mmio_needed = 1;
  872. vcpu->mmio_phys_addr = gpa;
  873. vcpu->mmio_size = bytes;
  874. vcpu->mmio_is_write = 0;
  875. return X86EMUL_UNHANDLEABLE;
  876. }
  877. }
  878. static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
  879. const void *val, int bytes)
  880. {
  881. struct page *page;
  882. void *virt;
  883. if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
  884. return 0;
  885. page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
  886. if (!page)
  887. return 0;
  888. kvm_mmu_pre_write(vcpu, gpa, bytes);
  889. mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
  890. virt = kmap_atomic(page, KM_USER0);
  891. memcpy(virt + offset_in_page(gpa), val, bytes);
  892. kunmap_atomic(virt, KM_USER0);
  893. kvm_mmu_post_write(vcpu, gpa, bytes);
  894. return 1;
  895. }
  896. static int emulator_write_emulated(unsigned long addr,
  897. const void *val,
  898. unsigned int bytes,
  899. struct x86_emulate_ctxt *ctxt)
  900. {
  901. struct kvm_vcpu *vcpu = ctxt->vcpu;
  902. gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
  903. if (gpa == UNMAPPED_GVA) {
  904. kvm_arch_ops->inject_page_fault(vcpu, addr, 2);
  905. return X86EMUL_PROPAGATE_FAULT;
  906. }
  907. if (emulator_write_phys(vcpu, gpa, val, bytes))
  908. return X86EMUL_CONTINUE;
  909. vcpu->mmio_needed = 1;
  910. vcpu->mmio_phys_addr = gpa;
  911. vcpu->mmio_size = bytes;
  912. vcpu->mmio_is_write = 1;
  913. memcpy(vcpu->mmio_data, val, bytes);
  914. return X86EMUL_CONTINUE;
  915. }
  916. static int emulator_cmpxchg_emulated(unsigned long addr,
  917. const void *old,
  918. const void *new,
  919. unsigned int bytes,
  920. struct x86_emulate_ctxt *ctxt)
  921. {
  922. static int reported;
  923. if (!reported) {
  924. reported = 1;
  925. printk(KERN_WARNING "kvm: emulating exchange as write\n");
  926. }
  927. return emulator_write_emulated(addr, new, bytes, ctxt);
  928. }
  929. static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
  930. {
  931. return kvm_arch_ops->get_segment_base(vcpu, seg);
  932. }
  933. int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
  934. {
  935. return X86EMUL_CONTINUE;
  936. }
  937. int emulate_clts(struct kvm_vcpu *vcpu)
  938. {
  939. unsigned long cr0;
  940. cr0 = vcpu->cr0 & ~CR0_TS_MASK;
  941. kvm_arch_ops->set_cr0(vcpu, cr0);
  942. return X86EMUL_CONTINUE;
  943. }
  944. int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
  945. {
  946. struct kvm_vcpu *vcpu = ctxt->vcpu;
  947. switch (dr) {
  948. case 0 ... 3:
  949. *dest = kvm_arch_ops->get_dr(vcpu, dr);
  950. return X86EMUL_CONTINUE;
  951. default:
  952. printk(KERN_DEBUG "%s: unexpected dr %u\n",
  953. __FUNCTION__, dr);
  954. return X86EMUL_UNHANDLEABLE;
  955. }
  956. }
  957. int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
  958. {
  959. unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
  960. int exception;
  961. kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
  962. if (exception) {
  963. /* FIXME: better handling */
  964. return X86EMUL_UNHANDLEABLE;
  965. }
  966. return X86EMUL_CONTINUE;
  967. }
  968. static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
  969. {
  970. static int reported;
  971. u8 opcodes[4];
  972. unsigned long rip = ctxt->vcpu->rip;
  973. unsigned long rip_linear;
  974. rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
  975. if (reported)
  976. return;
  977. emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
  978. printk(KERN_ERR "emulation failed but !mmio_needed?"
  979. " rip %lx %02x %02x %02x %02x\n",
  980. rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
  981. reported = 1;
  982. }
  983. struct x86_emulate_ops emulate_ops = {
  984. .read_std = emulator_read_std,
  985. .write_std = emulator_write_std,
  986. .read_emulated = emulator_read_emulated,
  987. .write_emulated = emulator_write_emulated,
  988. .cmpxchg_emulated = emulator_cmpxchg_emulated,
  989. };
  990. int emulate_instruction(struct kvm_vcpu *vcpu,
  991. struct kvm_run *run,
  992. unsigned long cr2,
  993. u16 error_code)
  994. {
  995. struct x86_emulate_ctxt emulate_ctxt;
  996. int r;
  997. int cs_db, cs_l;
  998. vcpu->mmio_fault_cr2 = cr2;
  999. kvm_arch_ops->cache_regs(vcpu);
  1000. kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  1001. emulate_ctxt.vcpu = vcpu;
  1002. emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
  1003. emulate_ctxt.cr2 = cr2;
  1004. emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
  1005. ? X86EMUL_MODE_REAL : cs_l
  1006. ? X86EMUL_MODE_PROT64 : cs_db
  1007. ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
  1008. if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
  1009. emulate_ctxt.cs_base = 0;
  1010. emulate_ctxt.ds_base = 0;
  1011. emulate_ctxt.es_base = 0;
  1012. emulate_ctxt.ss_base = 0;
  1013. } else {
  1014. emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
  1015. emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
  1016. emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
  1017. emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
  1018. }
  1019. emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
  1020. emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
  1021. vcpu->mmio_is_write = 0;
  1022. r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
  1023. if ((r || vcpu->mmio_is_write) && run) {
  1024. run->mmio.phys_addr = vcpu->mmio_phys_addr;
  1025. memcpy(run->mmio.data, vcpu->mmio_data, 8);
  1026. run->mmio.len = vcpu->mmio_size;
  1027. run->mmio.is_write = vcpu->mmio_is_write;
  1028. }
  1029. if (r) {
  1030. if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
  1031. return EMULATE_DONE;
  1032. if (!vcpu->mmio_needed) {
  1033. report_emulation_failure(&emulate_ctxt);
  1034. return EMULATE_FAIL;
  1035. }
  1036. return EMULATE_DO_MMIO;
  1037. }
  1038. kvm_arch_ops->decache_regs(vcpu);
  1039. kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
  1040. if (vcpu->mmio_is_write) {
  1041. vcpu->mmio_needed = 0;
  1042. return EMULATE_DO_MMIO;
  1043. }
  1044. return EMULATE_DONE;
  1045. }
  1046. EXPORT_SYMBOL_GPL(emulate_instruction);
  1047. int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
  1048. {
  1049. unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
  1050. kvm_arch_ops->cache_regs(vcpu);
  1051. ret = -KVM_EINVAL;
  1052. #ifdef CONFIG_X86_64
  1053. if (is_long_mode(vcpu)) {
  1054. nr = vcpu->regs[VCPU_REGS_RAX];
  1055. a0 = vcpu->regs[VCPU_REGS_RDI];
  1056. a1 = vcpu->regs[VCPU_REGS_RSI];
  1057. a2 = vcpu->regs[VCPU_REGS_RDX];
  1058. a3 = vcpu->regs[VCPU_REGS_RCX];
  1059. a4 = vcpu->regs[VCPU_REGS_R8];
  1060. a5 = vcpu->regs[VCPU_REGS_R9];
  1061. } else
  1062. #endif
  1063. {
  1064. nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
  1065. a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
  1066. a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
  1067. a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
  1068. a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
  1069. a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
  1070. a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
  1071. }
  1072. switch (nr) {
  1073. default:
  1074. run->hypercall.args[0] = a0;
  1075. run->hypercall.args[1] = a1;
  1076. run->hypercall.args[2] = a2;
  1077. run->hypercall.args[3] = a3;
  1078. run->hypercall.args[4] = a4;
  1079. run->hypercall.args[5] = a5;
  1080. run->hypercall.ret = ret;
  1081. run->hypercall.longmode = is_long_mode(vcpu);
  1082. kvm_arch_ops->decache_regs(vcpu);
  1083. return 0;
  1084. }
  1085. vcpu->regs[VCPU_REGS_RAX] = ret;
  1086. kvm_arch_ops->decache_regs(vcpu);
  1087. return 1;
  1088. }
  1089. EXPORT_SYMBOL_GPL(kvm_hypercall);
  1090. static u64 mk_cr_64(u64 curr_cr, u32 new_val)
  1091. {
  1092. return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
  1093. }
  1094. void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  1095. {
  1096. struct descriptor_table dt = { limit, base };
  1097. kvm_arch_ops->set_gdt(vcpu, &dt);
  1098. }
  1099. void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  1100. {
  1101. struct descriptor_table dt = { limit, base };
  1102. kvm_arch_ops->set_idt(vcpu, &dt);
  1103. }
  1104. void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
  1105. unsigned long *rflags)
  1106. {
  1107. lmsw(vcpu, msw);
  1108. *rflags = kvm_arch_ops->get_rflags(vcpu);
  1109. }
  1110. unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
  1111. {
  1112. kvm_arch_ops->decache_cr4_guest_bits(vcpu);
  1113. switch (cr) {
  1114. case 0:
  1115. return vcpu->cr0;
  1116. case 2:
  1117. return vcpu->cr2;
  1118. case 3:
  1119. return vcpu->cr3;
  1120. case 4:
  1121. return vcpu->cr4;
  1122. default:
  1123. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
  1124. return 0;
  1125. }
  1126. }
  1127. void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
  1128. unsigned long *rflags)
  1129. {
  1130. switch (cr) {
  1131. case 0:
  1132. set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
  1133. *rflags = kvm_arch_ops->get_rflags(vcpu);
  1134. break;
  1135. case 2:
  1136. vcpu->cr2 = val;
  1137. break;
  1138. case 3:
  1139. set_cr3(vcpu, val);
  1140. break;
  1141. case 4:
  1142. set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
  1143. break;
  1144. default:
  1145. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
  1146. }
  1147. }
  1148. /*
  1149. * Register the para guest with the host:
  1150. */
  1151. static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
  1152. {
  1153. struct kvm_vcpu_para_state *para_state;
  1154. hpa_t para_state_hpa, hypercall_hpa;
  1155. struct page *para_state_page;
  1156. unsigned char *hypercall;
  1157. gpa_t hypercall_gpa;
  1158. printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
  1159. printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
  1160. /*
  1161. * Needs to be page aligned:
  1162. */
  1163. if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
  1164. goto err_gp;
  1165. para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
  1166. printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
  1167. if (is_error_hpa(para_state_hpa))
  1168. goto err_gp;
  1169. mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
  1170. para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
  1171. para_state = kmap_atomic(para_state_page, KM_USER0);
  1172. printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
  1173. printk(KERN_DEBUG ".... size: %d\n", para_state->size);
  1174. para_state->host_version = KVM_PARA_API_VERSION;
  1175. /*
  1176. * We cannot support guests that try to register themselves
  1177. * with a newer API version than the host supports:
  1178. */
  1179. if (para_state->guest_version > KVM_PARA_API_VERSION) {
  1180. para_state->ret = -KVM_EINVAL;
  1181. goto err_kunmap_skip;
  1182. }
  1183. hypercall_gpa = para_state->hypercall_gpa;
  1184. hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
  1185. printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
  1186. if (is_error_hpa(hypercall_hpa)) {
  1187. para_state->ret = -KVM_EINVAL;
  1188. goto err_kunmap_skip;
  1189. }
  1190. printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
  1191. vcpu->para_state_page = para_state_page;
  1192. vcpu->para_state_gpa = para_state_gpa;
  1193. vcpu->hypercall_gpa = hypercall_gpa;
  1194. mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
  1195. hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
  1196. KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
  1197. kvm_arch_ops->patch_hypercall(vcpu, hypercall);
  1198. kunmap_atomic(hypercall, KM_USER1);
  1199. para_state->ret = 0;
  1200. err_kunmap_skip:
  1201. kunmap_atomic(para_state, KM_USER0);
  1202. return 0;
  1203. err_gp:
  1204. return 1;
  1205. }
  1206. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1207. {
  1208. u64 data;
  1209. switch (msr) {
  1210. case 0xc0010010: /* SYSCFG */
  1211. case 0xc0010015: /* HWCR */
  1212. case MSR_IA32_PLATFORM_ID:
  1213. case MSR_IA32_P5_MC_ADDR:
  1214. case MSR_IA32_P5_MC_TYPE:
  1215. case MSR_IA32_MC0_CTL:
  1216. case MSR_IA32_MCG_STATUS:
  1217. case MSR_IA32_MCG_CAP:
  1218. case MSR_IA32_MC0_MISC:
  1219. case MSR_IA32_MC0_MISC+4:
  1220. case MSR_IA32_MC0_MISC+8:
  1221. case MSR_IA32_MC0_MISC+12:
  1222. case MSR_IA32_MC0_MISC+16:
  1223. case MSR_IA32_UCODE_REV:
  1224. case MSR_IA32_PERF_STATUS:
  1225. /* MTRR registers */
  1226. case 0xfe:
  1227. case 0x200 ... 0x2ff:
  1228. data = 0;
  1229. break;
  1230. case 0xcd: /* fsb frequency */
  1231. data = 3;
  1232. break;
  1233. case MSR_IA32_APICBASE:
  1234. data = vcpu->apic_base;
  1235. break;
  1236. case MSR_IA32_MISC_ENABLE:
  1237. data = vcpu->ia32_misc_enable_msr;
  1238. break;
  1239. #ifdef CONFIG_X86_64
  1240. case MSR_EFER:
  1241. data = vcpu->shadow_efer;
  1242. break;
  1243. #endif
  1244. default:
  1245. printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
  1246. return 1;
  1247. }
  1248. *pdata = data;
  1249. return 0;
  1250. }
  1251. EXPORT_SYMBOL_GPL(kvm_get_msr_common);
  1252. /*
  1253. * Reads an msr value (of 'msr_index') into 'pdata'.
  1254. * Returns 0 on success, non-0 otherwise.
  1255. * Assumes vcpu_load() was already called.
  1256. */
  1257. static int get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  1258. {
  1259. return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
  1260. }
  1261. #ifdef CONFIG_X86_64
  1262. static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
  1263. {
  1264. if (efer & EFER_RESERVED_BITS) {
  1265. printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
  1266. efer);
  1267. inject_gp(vcpu);
  1268. return;
  1269. }
  1270. if (is_paging(vcpu)
  1271. && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
  1272. printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
  1273. inject_gp(vcpu);
  1274. return;
  1275. }
  1276. kvm_arch_ops->set_efer(vcpu, efer);
  1277. efer &= ~EFER_LMA;
  1278. efer |= vcpu->shadow_efer & EFER_LMA;
  1279. vcpu->shadow_efer = efer;
  1280. }
  1281. #endif
  1282. int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  1283. {
  1284. switch (msr) {
  1285. #ifdef CONFIG_X86_64
  1286. case MSR_EFER:
  1287. set_efer(vcpu, data);
  1288. break;
  1289. #endif
  1290. case MSR_IA32_MC0_STATUS:
  1291. printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
  1292. __FUNCTION__, data);
  1293. break;
  1294. case MSR_IA32_MCG_STATUS:
  1295. printk(KERN_WARNING "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
  1296. __FUNCTION__, data);
  1297. break;
  1298. case MSR_IA32_UCODE_REV:
  1299. case MSR_IA32_UCODE_WRITE:
  1300. case 0x200 ... 0x2ff: /* MTRRs */
  1301. break;
  1302. case MSR_IA32_APICBASE:
  1303. vcpu->apic_base = data;
  1304. break;
  1305. case MSR_IA32_MISC_ENABLE:
  1306. vcpu->ia32_misc_enable_msr = data;
  1307. break;
  1308. /*
  1309. * This is the 'probe whether the host is KVM' logic:
  1310. */
  1311. case MSR_KVM_API_MAGIC:
  1312. return vcpu_register_para(vcpu, data);
  1313. default:
  1314. printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
  1315. return 1;
  1316. }
  1317. return 0;
  1318. }
  1319. EXPORT_SYMBOL_GPL(kvm_set_msr_common);
  1320. /*
  1321. * Writes msr value into into the appropriate "register".
  1322. * Returns 0 on success, non-0 otherwise.
  1323. * Assumes vcpu_load() was already called.
  1324. */
  1325. static int set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  1326. {
  1327. return kvm_arch_ops->set_msr(vcpu, msr_index, data);
  1328. }
  1329. void kvm_resched(struct kvm_vcpu *vcpu)
  1330. {
  1331. if (!need_resched())
  1332. return;
  1333. vcpu_put(vcpu);
  1334. cond_resched();
  1335. vcpu_load(vcpu);
  1336. }
  1337. EXPORT_SYMBOL_GPL(kvm_resched);
  1338. void load_msrs(struct vmx_msr_entry *e, int n)
  1339. {
  1340. int i;
  1341. for (i = 0; i < n; ++i)
  1342. wrmsrl(e[i].index, e[i].data);
  1343. }
  1344. EXPORT_SYMBOL_GPL(load_msrs);
  1345. void save_msrs(struct vmx_msr_entry *e, int n)
  1346. {
  1347. int i;
  1348. for (i = 0; i < n; ++i)
  1349. rdmsrl(e[i].index, e[i].data);
  1350. }
  1351. EXPORT_SYMBOL_GPL(save_msrs);
  1352. void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
  1353. {
  1354. int i;
  1355. u32 function;
  1356. struct kvm_cpuid_entry *e, *best;
  1357. kvm_arch_ops->cache_regs(vcpu);
  1358. function = vcpu->regs[VCPU_REGS_RAX];
  1359. vcpu->regs[VCPU_REGS_RAX] = 0;
  1360. vcpu->regs[VCPU_REGS_RBX] = 0;
  1361. vcpu->regs[VCPU_REGS_RCX] = 0;
  1362. vcpu->regs[VCPU_REGS_RDX] = 0;
  1363. best = NULL;
  1364. for (i = 0; i < vcpu->cpuid_nent; ++i) {
  1365. e = &vcpu->cpuid_entries[i];
  1366. if (e->function == function) {
  1367. best = e;
  1368. break;
  1369. }
  1370. /*
  1371. * Both basic or both extended?
  1372. */
  1373. if (((e->function ^ function) & 0x80000000) == 0)
  1374. if (!best || e->function > best->function)
  1375. best = e;
  1376. }
  1377. if (best) {
  1378. vcpu->regs[VCPU_REGS_RAX] = best->eax;
  1379. vcpu->regs[VCPU_REGS_RBX] = best->ebx;
  1380. vcpu->regs[VCPU_REGS_RCX] = best->ecx;
  1381. vcpu->regs[VCPU_REGS_RDX] = best->edx;
  1382. }
  1383. kvm_arch_ops->decache_regs(vcpu);
  1384. kvm_arch_ops->skip_emulated_instruction(vcpu);
  1385. }
  1386. EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
  1387. static int pio_copy_data(struct kvm_vcpu *vcpu)
  1388. {
  1389. void *p = vcpu->pio_data;
  1390. void *q;
  1391. unsigned bytes;
  1392. int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
  1393. kvm_arch_ops->vcpu_put(vcpu);
  1394. q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
  1395. PAGE_KERNEL);
  1396. if (!q) {
  1397. kvm_arch_ops->vcpu_load(vcpu);
  1398. free_pio_guest_pages(vcpu);
  1399. return -ENOMEM;
  1400. }
  1401. q += vcpu->pio.guest_page_offset;
  1402. bytes = vcpu->pio.size * vcpu->pio.cur_count;
  1403. if (vcpu->pio.in)
  1404. memcpy(q, p, bytes);
  1405. else
  1406. memcpy(p, q, bytes);
  1407. q -= vcpu->pio.guest_page_offset;
  1408. vunmap(q);
  1409. kvm_arch_ops->vcpu_load(vcpu);
  1410. free_pio_guest_pages(vcpu);
  1411. return 0;
  1412. }
  1413. static int complete_pio(struct kvm_vcpu *vcpu)
  1414. {
  1415. struct kvm_pio_request *io = &vcpu->pio;
  1416. long delta;
  1417. int r;
  1418. kvm_arch_ops->cache_regs(vcpu);
  1419. if (!io->string) {
  1420. if (io->in)
  1421. memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
  1422. io->size);
  1423. } else {
  1424. if (io->in) {
  1425. r = pio_copy_data(vcpu);
  1426. if (r) {
  1427. kvm_arch_ops->cache_regs(vcpu);
  1428. return r;
  1429. }
  1430. }
  1431. delta = 1;
  1432. if (io->rep) {
  1433. delta *= io->cur_count;
  1434. /*
  1435. * The size of the register should really depend on
  1436. * current address size.
  1437. */
  1438. vcpu->regs[VCPU_REGS_RCX] -= delta;
  1439. }
  1440. if (io->down)
  1441. delta = -delta;
  1442. delta *= io->size;
  1443. if (io->in)
  1444. vcpu->regs[VCPU_REGS_RDI] += delta;
  1445. else
  1446. vcpu->regs[VCPU_REGS_RSI] += delta;
  1447. }
  1448. kvm_arch_ops->decache_regs(vcpu);
  1449. io->count -= io->cur_count;
  1450. io->cur_count = 0;
  1451. if (!io->count)
  1452. kvm_arch_ops->skip_emulated_instruction(vcpu);
  1453. return 0;
  1454. }
  1455. int kvm_setup_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  1456. int size, unsigned long count, int string, int down,
  1457. gva_t address, int rep, unsigned port)
  1458. {
  1459. unsigned now, in_page;
  1460. int i;
  1461. int nr_pages = 1;
  1462. struct page *page;
  1463. vcpu->run->exit_reason = KVM_EXIT_IO;
  1464. vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
  1465. vcpu->run->io.size = size;
  1466. vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
  1467. vcpu->run->io.count = count;
  1468. vcpu->run->io.port = port;
  1469. vcpu->pio.count = count;
  1470. vcpu->pio.cur_count = count;
  1471. vcpu->pio.size = size;
  1472. vcpu->pio.in = in;
  1473. vcpu->pio.string = string;
  1474. vcpu->pio.down = down;
  1475. vcpu->pio.guest_page_offset = offset_in_page(address);
  1476. vcpu->pio.rep = rep;
  1477. if (!string) {
  1478. kvm_arch_ops->cache_regs(vcpu);
  1479. memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
  1480. kvm_arch_ops->decache_regs(vcpu);
  1481. return 0;
  1482. }
  1483. if (!count) {
  1484. kvm_arch_ops->skip_emulated_instruction(vcpu);
  1485. return 1;
  1486. }
  1487. now = min(count, PAGE_SIZE / size);
  1488. if (!down)
  1489. in_page = PAGE_SIZE - offset_in_page(address);
  1490. else
  1491. in_page = offset_in_page(address) + size;
  1492. now = min(count, (unsigned long)in_page / size);
  1493. if (!now) {
  1494. /*
  1495. * String I/O straddles page boundary. Pin two guest pages
  1496. * so that we satisfy atomicity constraints. Do just one
  1497. * transaction to avoid complexity.
  1498. */
  1499. nr_pages = 2;
  1500. now = 1;
  1501. }
  1502. if (down) {
  1503. /*
  1504. * String I/O in reverse. Yuck. Kill the guest, fix later.
  1505. */
  1506. printk(KERN_ERR "kvm: guest string pio down\n");
  1507. inject_gp(vcpu);
  1508. return 1;
  1509. }
  1510. vcpu->run->io.count = now;
  1511. vcpu->pio.cur_count = now;
  1512. for (i = 0; i < nr_pages; ++i) {
  1513. spin_lock(&vcpu->kvm->lock);
  1514. page = gva_to_page(vcpu, address + i * PAGE_SIZE);
  1515. if (page)
  1516. get_page(page);
  1517. vcpu->pio.guest_pages[i] = page;
  1518. spin_unlock(&vcpu->kvm->lock);
  1519. if (!page) {
  1520. inject_gp(vcpu);
  1521. free_pio_guest_pages(vcpu);
  1522. return 1;
  1523. }
  1524. }
  1525. if (!vcpu->pio.in)
  1526. return pio_copy_data(vcpu);
  1527. return 0;
  1528. }
  1529. EXPORT_SYMBOL_GPL(kvm_setup_pio);
  1530. static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  1531. {
  1532. int r;
  1533. sigset_t sigsaved;
  1534. vcpu_load(vcpu);
  1535. if (vcpu->sigset_active)
  1536. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  1537. /* re-sync apic's tpr */
  1538. vcpu->cr8 = kvm_run->cr8;
  1539. if (vcpu->pio.cur_count) {
  1540. r = complete_pio(vcpu);
  1541. if (r)
  1542. goto out;
  1543. }
  1544. if (vcpu->mmio_needed) {
  1545. memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
  1546. vcpu->mmio_read_completed = 1;
  1547. vcpu->mmio_needed = 0;
  1548. r = emulate_instruction(vcpu, kvm_run,
  1549. vcpu->mmio_fault_cr2, 0);
  1550. if (r == EMULATE_DO_MMIO) {
  1551. /*
  1552. * Read-modify-write. Back to userspace.
  1553. */
  1554. kvm_run->exit_reason = KVM_EXIT_MMIO;
  1555. r = 0;
  1556. goto out;
  1557. }
  1558. }
  1559. if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
  1560. kvm_arch_ops->cache_regs(vcpu);
  1561. vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
  1562. kvm_arch_ops->decache_regs(vcpu);
  1563. }
  1564. r = kvm_arch_ops->run(vcpu, kvm_run);
  1565. out:
  1566. if (vcpu->sigset_active)
  1567. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1568. vcpu_put(vcpu);
  1569. return r;
  1570. }
  1571. static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
  1572. struct kvm_regs *regs)
  1573. {
  1574. vcpu_load(vcpu);
  1575. kvm_arch_ops->cache_regs(vcpu);
  1576. regs->rax = vcpu->regs[VCPU_REGS_RAX];
  1577. regs->rbx = vcpu->regs[VCPU_REGS_RBX];
  1578. regs->rcx = vcpu->regs[VCPU_REGS_RCX];
  1579. regs->rdx = vcpu->regs[VCPU_REGS_RDX];
  1580. regs->rsi = vcpu->regs[VCPU_REGS_RSI];
  1581. regs->rdi = vcpu->regs[VCPU_REGS_RDI];
  1582. regs->rsp = vcpu->regs[VCPU_REGS_RSP];
  1583. regs->rbp = vcpu->regs[VCPU_REGS_RBP];
  1584. #ifdef CONFIG_X86_64
  1585. regs->r8 = vcpu->regs[VCPU_REGS_R8];
  1586. regs->r9 = vcpu->regs[VCPU_REGS_R9];
  1587. regs->r10 = vcpu->regs[VCPU_REGS_R10];
  1588. regs->r11 = vcpu->regs[VCPU_REGS_R11];
  1589. regs->r12 = vcpu->regs[VCPU_REGS_R12];
  1590. regs->r13 = vcpu->regs[VCPU_REGS_R13];
  1591. regs->r14 = vcpu->regs[VCPU_REGS_R14];
  1592. regs->r15 = vcpu->regs[VCPU_REGS_R15];
  1593. #endif
  1594. regs->rip = vcpu->rip;
  1595. regs->rflags = kvm_arch_ops->get_rflags(vcpu);
  1596. /*
  1597. * Don't leak debug flags in case they were set for guest debugging
  1598. */
  1599. if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
  1600. regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
  1601. vcpu_put(vcpu);
  1602. return 0;
  1603. }
  1604. static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
  1605. struct kvm_regs *regs)
  1606. {
  1607. vcpu_load(vcpu);
  1608. vcpu->regs[VCPU_REGS_RAX] = regs->rax;
  1609. vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
  1610. vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
  1611. vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
  1612. vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
  1613. vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
  1614. vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
  1615. vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
  1616. #ifdef CONFIG_X86_64
  1617. vcpu->regs[VCPU_REGS_R8] = regs->r8;
  1618. vcpu->regs[VCPU_REGS_R9] = regs->r9;
  1619. vcpu->regs[VCPU_REGS_R10] = regs->r10;
  1620. vcpu->regs[VCPU_REGS_R11] = regs->r11;
  1621. vcpu->regs[VCPU_REGS_R12] = regs->r12;
  1622. vcpu->regs[VCPU_REGS_R13] = regs->r13;
  1623. vcpu->regs[VCPU_REGS_R14] = regs->r14;
  1624. vcpu->regs[VCPU_REGS_R15] = regs->r15;
  1625. #endif
  1626. vcpu->rip = regs->rip;
  1627. kvm_arch_ops->set_rflags(vcpu, regs->rflags);
  1628. kvm_arch_ops->decache_regs(vcpu);
  1629. vcpu_put(vcpu);
  1630. return 0;
  1631. }
  1632. static void get_segment(struct kvm_vcpu *vcpu,
  1633. struct kvm_segment *var, int seg)
  1634. {
  1635. return kvm_arch_ops->get_segment(vcpu, var, seg);
  1636. }
  1637. static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  1638. struct kvm_sregs *sregs)
  1639. {
  1640. struct descriptor_table dt;
  1641. vcpu_load(vcpu);
  1642. get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  1643. get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  1644. get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  1645. get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  1646. get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  1647. get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  1648. get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  1649. get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  1650. kvm_arch_ops->get_idt(vcpu, &dt);
  1651. sregs->idt.limit = dt.limit;
  1652. sregs->idt.base = dt.base;
  1653. kvm_arch_ops->get_gdt(vcpu, &dt);
  1654. sregs->gdt.limit = dt.limit;
  1655. sregs->gdt.base = dt.base;
  1656. kvm_arch_ops->decache_cr4_guest_bits(vcpu);
  1657. sregs->cr0 = vcpu->cr0;
  1658. sregs->cr2 = vcpu->cr2;
  1659. sregs->cr3 = vcpu->cr3;
  1660. sregs->cr4 = vcpu->cr4;
  1661. sregs->cr8 = vcpu->cr8;
  1662. sregs->efer = vcpu->shadow_efer;
  1663. sregs->apic_base = vcpu->apic_base;
  1664. memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
  1665. sizeof sregs->interrupt_bitmap);
  1666. vcpu_put(vcpu);
  1667. return 0;
  1668. }
  1669. static void set_segment(struct kvm_vcpu *vcpu,
  1670. struct kvm_segment *var, int seg)
  1671. {
  1672. return kvm_arch_ops->set_segment(vcpu, var, seg);
  1673. }
  1674. static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  1675. struct kvm_sregs *sregs)
  1676. {
  1677. int mmu_reset_needed = 0;
  1678. int i;
  1679. struct descriptor_table dt;
  1680. vcpu_load(vcpu);
  1681. dt.limit = sregs->idt.limit;
  1682. dt.base = sregs->idt.base;
  1683. kvm_arch_ops->set_idt(vcpu, &dt);
  1684. dt.limit = sregs->gdt.limit;
  1685. dt.base = sregs->gdt.base;
  1686. kvm_arch_ops->set_gdt(vcpu, &dt);
  1687. vcpu->cr2 = sregs->cr2;
  1688. mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
  1689. vcpu->cr3 = sregs->cr3;
  1690. vcpu->cr8 = sregs->cr8;
  1691. mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
  1692. #ifdef CONFIG_X86_64
  1693. kvm_arch_ops->set_efer(vcpu, sregs->efer);
  1694. #endif
  1695. vcpu->apic_base = sregs->apic_base;
  1696. kvm_arch_ops->decache_cr4_guest_bits(vcpu);
  1697. mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
  1698. kvm_arch_ops->set_cr0(vcpu, sregs->cr0);
  1699. mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
  1700. kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
  1701. if (!is_long_mode(vcpu) && is_pae(vcpu))
  1702. load_pdptrs(vcpu, vcpu->cr3);
  1703. if (mmu_reset_needed)
  1704. kvm_mmu_reset_context(vcpu);
  1705. memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
  1706. sizeof vcpu->irq_pending);
  1707. vcpu->irq_summary = 0;
  1708. for (i = 0; i < NR_IRQ_WORDS; ++i)
  1709. if (vcpu->irq_pending[i])
  1710. __set_bit(i, &vcpu->irq_summary);
  1711. set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  1712. set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  1713. set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  1714. set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  1715. set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  1716. set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  1717. set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  1718. set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  1719. vcpu_put(vcpu);
  1720. return 0;
  1721. }
  1722. /*
  1723. * List of msr numbers which we expose to userspace through KVM_GET_MSRS
  1724. * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
  1725. *
  1726. * This list is modified at module load time to reflect the
  1727. * capabilities of the host cpu.
  1728. */
  1729. static u32 msrs_to_save[] = {
  1730. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  1731. MSR_K6_STAR,
  1732. #ifdef CONFIG_X86_64
  1733. MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
  1734. #endif
  1735. MSR_IA32_TIME_STAMP_COUNTER,
  1736. };
  1737. static unsigned num_msrs_to_save;
  1738. static u32 emulated_msrs[] = {
  1739. MSR_IA32_MISC_ENABLE,
  1740. };
  1741. static __init void kvm_init_msr_list(void)
  1742. {
  1743. u32 dummy[2];
  1744. unsigned i, j;
  1745. for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
  1746. if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
  1747. continue;
  1748. if (j < i)
  1749. msrs_to_save[j] = msrs_to_save[i];
  1750. j++;
  1751. }
  1752. num_msrs_to_save = j;
  1753. }
  1754. /*
  1755. * Adapt set_msr() to msr_io()'s calling convention
  1756. */
  1757. static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
  1758. {
  1759. return set_msr(vcpu, index, *data);
  1760. }
  1761. /*
  1762. * Read or write a bunch of msrs. All parameters are kernel addresses.
  1763. *
  1764. * @return number of msrs set successfully.
  1765. */
  1766. static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
  1767. struct kvm_msr_entry *entries,
  1768. int (*do_msr)(struct kvm_vcpu *vcpu,
  1769. unsigned index, u64 *data))
  1770. {
  1771. int i;
  1772. vcpu_load(vcpu);
  1773. for (i = 0; i < msrs->nmsrs; ++i)
  1774. if (do_msr(vcpu, entries[i].index, &entries[i].data))
  1775. break;
  1776. vcpu_put(vcpu);
  1777. return i;
  1778. }
  1779. /*
  1780. * Read or write a bunch of msrs. Parameters are user addresses.
  1781. *
  1782. * @return number of msrs set successfully.
  1783. */
  1784. static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
  1785. int (*do_msr)(struct kvm_vcpu *vcpu,
  1786. unsigned index, u64 *data),
  1787. int writeback)
  1788. {
  1789. struct kvm_msrs msrs;
  1790. struct kvm_msr_entry *entries;
  1791. int r, n;
  1792. unsigned size;
  1793. r = -EFAULT;
  1794. if (copy_from_user(&msrs, user_msrs, sizeof msrs))
  1795. goto out;
  1796. r = -E2BIG;
  1797. if (msrs.nmsrs >= MAX_IO_MSRS)
  1798. goto out;
  1799. r = -ENOMEM;
  1800. size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
  1801. entries = vmalloc(size);
  1802. if (!entries)
  1803. goto out;
  1804. r = -EFAULT;
  1805. if (copy_from_user(entries, user_msrs->entries, size))
  1806. goto out_free;
  1807. r = n = __msr_io(vcpu, &msrs, entries, do_msr);
  1808. if (r < 0)
  1809. goto out_free;
  1810. r = -EFAULT;
  1811. if (writeback && copy_to_user(user_msrs->entries, entries, size))
  1812. goto out_free;
  1813. r = n;
  1814. out_free:
  1815. vfree(entries);
  1816. out:
  1817. return r;
  1818. }
  1819. /*
  1820. * Translate a guest virtual address to a guest physical address.
  1821. */
  1822. static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  1823. struct kvm_translation *tr)
  1824. {
  1825. unsigned long vaddr = tr->linear_address;
  1826. gpa_t gpa;
  1827. vcpu_load(vcpu);
  1828. spin_lock(&vcpu->kvm->lock);
  1829. gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
  1830. tr->physical_address = gpa;
  1831. tr->valid = gpa != UNMAPPED_GVA;
  1832. tr->writeable = 1;
  1833. tr->usermode = 0;
  1834. spin_unlock(&vcpu->kvm->lock);
  1835. vcpu_put(vcpu);
  1836. return 0;
  1837. }
  1838. static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
  1839. struct kvm_interrupt *irq)
  1840. {
  1841. if (irq->irq < 0 || irq->irq >= 256)
  1842. return -EINVAL;
  1843. vcpu_load(vcpu);
  1844. set_bit(irq->irq, vcpu->irq_pending);
  1845. set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
  1846. vcpu_put(vcpu);
  1847. return 0;
  1848. }
  1849. static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
  1850. struct kvm_debug_guest *dbg)
  1851. {
  1852. int r;
  1853. vcpu_load(vcpu);
  1854. r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
  1855. vcpu_put(vcpu);
  1856. return r;
  1857. }
  1858. static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
  1859. unsigned long address,
  1860. int *type)
  1861. {
  1862. struct kvm_vcpu *vcpu = vma->vm_file->private_data;
  1863. unsigned long pgoff;
  1864. struct page *page;
  1865. *type = VM_FAULT_MINOR;
  1866. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  1867. if (pgoff == 0)
  1868. page = virt_to_page(vcpu->run);
  1869. else if (pgoff == KVM_PIO_PAGE_OFFSET)
  1870. page = virt_to_page(vcpu->pio_data);
  1871. else
  1872. return NOPAGE_SIGBUS;
  1873. get_page(page);
  1874. return page;
  1875. }
  1876. static struct vm_operations_struct kvm_vcpu_vm_ops = {
  1877. .nopage = kvm_vcpu_nopage,
  1878. };
  1879. static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
  1880. {
  1881. vma->vm_ops = &kvm_vcpu_vm_ops;
  1882. return 0;
  1883. }
  1884. static int kvm_vcpu_release(struct inode *inode, struct file *filp)
  1885. {
  1886. struct kvm_vcpu *vcpu = filp->private_data;
  1887. fput(vcpu->kvm->filp);
  1888. return 0;
  1889. }
  1890. static struct file_operations kvm_vcpu_fops = {
  1891. .release = kvm_vcpu_release,
  1892. .unlocked_ioctl = kvm_vcpu_ioctl,
  1893. .compat_ioctl = kvm_vcpu_ioctl,
  1894. .mmap = kvm_vcpu_mmap,
  1895. };
  1896. /*
  1897. * Allocates an inode for the vcpu.
  1898. */
  1899. static int create_vcpu_fd(struct kvm_vcpu *vcpu)
  1900. {
  1901. int fd, r;
  1902. struct inode *inode;
  1903. struct file *file;
  1904. atomic_inc(&vcpu->kvm->filp->f_count);
  1905. inode = kvmfs_inode(&kvm_vcpu_fops);
  1906. if (IS_ERR(inode)) {
  1907. r = PTR_ERR(inode);
  1908. goto out1;
  1909. }
  1910. file = kvmfs_file(inode, vcpu);
  1911. if (IS_ERR(file)) {
  1912. r = PTR_ERR(file);
  1913. goto out2;
  1914. }
  1915. r = get_unused_fd();
  1916. if (r < 0)
  1917. goto out3;
  1918. fd = r;
  1919. fd_install(fd, file);
  1920. return fd;
  1921. out3:
  1922. fput(file);
  1923. out2:
  1924. iput(inode);
  1925. out1:
  1926. fput(vcpu->kvm->filp);
  1927. return r;
  1928. }
  1929. /*
  1930. * Creates some virtual cpus. Good luck creating more than one.
  1931. */
  1932. static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
  1933. {
  1934. int r;
  1935. struct kvm_vcpu *vcpu;
  1936. struct page *page;
  1937. r = -EINVAL;
  1938. if (!valid_vcpu(n))
  1939. goto out;
  1940. vcpu = &kvm->vcpus[n];
  1941. mutex_lock(&vcpu->mutex);
  1942. if (vcpu->vmcs) {
  1943. mutex_unlock(&vcpu->mutex);
  1944. return -EEXIST;
  1945. }
  1946. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  1947. r = -ENOMEM;
  1948. if (!page)
  1949. goto out_unlock;
  1950. vcpu->run = page_address(page);
  1951. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  1952. r = -ENOMEM;
  1953. if (!page)
  1954. goto out_free_run;
  1955. vcpu->pio_data = page_address(page);
  1956. vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
  1957. FX_IMAGE_ALIGN);
  1958. vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
  1959. vcpu->cr0 = 0x10;
  1960. r = kvm_arch_ops->vcpu_create(vcpu);
  1961. if (r < 0)
  1962. goto out_free_vcpus;
  1963. r = kvm_mmu_create(vcpu);
  1964. if (r < 0)
  1965. goto out_free_vcpus;
  1966. kvm_arch_ops->vcpu_load(vcpu);
  1967. r = kvm_mmu_setup(vcpu);
  1968. if (r >= 0)
  1969. r = kvm_arch_ops->vcpu_setup(vcpu);
  1970. vcpu_put(vcpu);
  1971. if (r < 0)
  1972. goto out_free_vcpus;
  1973. r = create_vcpu_fd(vcpu);
  1974. if (r < 0)
  1975. goto out_free_vcpus;
  1976. return r;
  1977. out_free_vcpus:
  1978. kvm_free_vcpu(vcpu);
  1979. out_free_run:
  1980. free_page((unsigned long)vcpu->run);
  1981. vcpu->run = NULL;
  1982. out_unlock:
  1983. mutex_unlock(&vcpu->mutex);
  1984. out:
  1985. return r;
  1986. }
  1987. static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
  1988. struct kvm_cpuid *cpuid,
  1989. struct kvm_cpuid_entry __user *entries)
  1990. {
  1991. int r;
  1992. r = -E2BIG;
  1993. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  1994. goto out;
  1995. r = -EFAULT;
  1996. if (copy_from_user(&vcpu->cpuid_entries, entries,
  1997. cpuid->nent * sizeof(struct kvm_cpuid_entry)))
  1998. goto out;
  1999. vcpu->cpuid_nent = cpuid->nent;
  2000. return 0;
  2001. out:
  2002. return r;
  2003. }
  2004. static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
  2005. {
  2006. if (sigset) {
  2007. sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
  2008. vcpu->sigset_active = 1;
  2009. vcpu->sigset = *sigset;
  2010. } else
  2011. vcpu->sigset_active = 0;
  2012. return 0;
  2013. }
  2014. /*
  2015. * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
  2016. * we have asm/x86/processor.h
  2017. */
  2018. struct fxsave {
  2019. u16 cwd;
  2020. u16 swd;
  2021. u16 twd;
  2022. u16 fop;
  2023. u64 rip;
  2024. u64 rdp;
  2025. u32 mxcsr;
  2026. u32 mxcsr_mask;
  2027. u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
  2028. #ifdef CONFIG_X86_64
  2029. u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
  2030. #else
  2031. u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
  2032. #endif
  2033. };
  2034. static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  2035. {
  2036. struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
  2037. vcpu_load(vcpu);
  2038. memcpy(fpu->fpr, fxsave->st_space, 128);
  2039. fpu->fcw = fxsave->cwd;
  2040. fpu->fsw = fxsave->swd;
  2041. fpu->ftwx = fxsave->twd;
  2042. fpu->last_opcode = fxsave->fop;
  2043. fpu->last_ip = fxsave->rip;
  2044. fpu->last_dp = fxsave->rdp;
  2045. memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
  2046. vcpu_put(vcpu);
  2047. return 0;
  2048. }
  2049. static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  2050. {
  2051. struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
  2052. vcpu_load(vcpu);
  2053. memcpy(fxsave->st_space, fpu->fpr, 128);
  2054. fxsave->cwd = fpu->fcw;
  2055. fxsave->swd = fpu->fsw;
  2056. fxsave->twd = fpu->ftwx;
  2057. fxsave->fop = fpu->last_opcode;
  2058. fxsave->rip = fpu->last_ip;
  2059. fxsave->rdp = fpu->last_dp;
  2060. memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
  2061. vcpu_put(vcpu);
  2062. return 0;
  2063. }
  2064. static long kvm_vcpu_ioctl(struct file *filp,
  2065. unsigned int ioctl, unsigned long arg)
  2066. {
  2067. struct kvm_vcpu *vcpu = filp->private_data;
  2068. void __user *argp = (void __user *)arg;
  2069. int r = -EINVAL;
  2070. switch (ioctl) {
  2071. case KVM_RUN:
  2072. r = -EINVAL;
  2073. if (arg)
  2074. goto out;
  2075. r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
  2076. break;
  2077. case KVM_GET_REGS: {
  2078. struct kvm_regs kvm_regs;
  2079. memset(&kvm_regs, 0, sizeof kvm_regs);
  2080. r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
  2081. if (r)
  2082. goto out;
  2083. r = -EFAULT;
  2084. if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
  2085. goto out;
  2086. r = 0;
  2087. break;
  2088. }
  2089. case KVM_SET_REGS: {
  2090. struct kvm_regs kvm_regs;
  2091. r = -EFAULT;
  2092. if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
  2093. goto out;
  2094. r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
  2095. if (r)
  2096. goto out;
  2097. r = 0;
  2098. break;
  2099. }
  2100. case KVM_GET_SREGS: {
  2101. struct kvm_sregs kvm_sregs;
  2102. memset(&kvm_sregs, 0, sizeof kvm_sregs);
  2103. r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
  2104. if (r)
  2105. goto out;
  2106. r = -EFAULT;
  2107. if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
  2108. goto out;
  2109. r = 0;
  2110. break;
  2111. }
  2112. case KVM_SET_SREGS: {
  2113. struct kvm_sregs kvm_sregs;
  2114. r = -EFAULT;
  2115. if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
  2116. goto out;
  2117. r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
  2118. if (r)
  2119. goto out;
  2120. r = 0;
  2121. break;
  2122. }
  2123. case KVM_TRANSLATE: {
  2124. struct kvm_translation tr;
  2125. r = -EFAULT;
  2126. if (copy_from_user(&tr, argp, sizeof tr))
  2127. goto out;
  2128. r = kvm_vcpu_ioctl_translate(vcpu, &tr);
  2129. if (r)
  2130. goto out;
  2131. r = -EFAULT;
  2132. if (copy_to_user(argp, &tr, sizeof tr))
  2133. goto out;
  2134. r = 0;
  2135. break;
  2136. }
  2137. case KVM_INTERRUPT: {
  2138. struct kvm_interrupt irq;
  2139. r = -EFAULT;
  2140. if (copy_from_user(&irq, argp, sizeof irq))
  2141. goto out;
  2142. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  2143. if (r)
  2144. goto out;
  2145. r = 0;
  2146. break;
  2147. }
  2148. case KVM_DEBUG_GUEST: {
  2149. struct kvm_debug_guest dbg;
  2150. r = -EFAULT;
  2151. if (copy_from_user(&dbg, argp, sizeof dbg))
  2152. goto out;
  2153. r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
  2154. if (r)
  2155. goto out;
  2156. r = 0;
  2157. break;
  2158. }
  2159. case KVM_GET_MSRS:
  2160. r = msr_io(vcpu, argp, get_msr, 1);
  2161. break;
  2162. case KVM_SET_MSRS:
  2163. r = msr_io(vcpu, argp, do_set_msr, 0);
  2164. break;
  2165. case KVM_SET_CPUID: {
  2166. struct kvm_cpuid __user *cpuid_arg = argp;
  2167. struct kvm_cpuid cpuid;
  2168. r = -EFAULT;
  2169. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  2170. goto out;
  2171. r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
  2172. if (r)
  2173. goto out;
  2174. break;
  2175. }
  2176. case KVM_SET_SIGNAL_MASK: {
  2177. struct kvm_signal_mask __user *sigmask_arg = argp;
  2178. struct kvm_signal_mask kvm_sigmask;
  2179. sigset_t sigset, *p;
  2180. p = NULL;
  2181. if (argp) {
  2182. r = -EFAULT;
  2183. if (copy_from_user(&kvm_sigmask, argp,
  2184. sizeof kvm_sigmask))
  2185. goto out;
  2186. r = -EINVAL;
  2187. if (kvm_sigmask.len != sizeof sigset)
  2188. goto out;
  2189. r = -EFAULT;
  2190. if (copy_from_user(&sigset, sigmask_arg->sigset,
  2191. sizeof sigset))
  2192. goto out;
  2193. p = &sigset;
  2194. }
  2195. r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
  2196. break;
  2197. }
  2198. case KVM_GET_FPU: {
  2199. struct kvm_fpu fpu;
  2200. memset(&fpu, 0, sizeof fpu);
  2201. r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
  2202. if (r)
  2203. goto out;
  2204. r = -EFAULT;
  2205. if (copy_to_user(argp, &fpu, sizeof fpu))
  2206. goto out;
  2207. r = 0;
  2208. break;
  2209. }
  2210. case KVM_SET_FPU: {
  2211. struct kvm_fpu fpu;
  2212. r = -EFAULT;
  2213. if (copy_from_user(&fpu, argp, sizeof fpu))
  2214. goto out;
  2215. r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
  2216. if (r)
  2217. goto out;
  2218. r = 0;
  2219. break;
  2220. }
  2221. default:
  2222. ;
  2223. }
  2224. out:
  2225. return r;
  2226. }
  2227. static long kvm_vm_ioctl(struct file *filp,
  2228. unsigned int ioctl, unsigned long arg)
  2229. {
  2230. struct kvm *kvm = filp->private_data;
  2231. void __user *argp = (void __user *)arg;
  2232. int r = -EINVAL;
  2233. switch (ioctl) {
  2234. case KVM_CREATE_VCPU:
  2235. r = kvm_vm_ioctl_create_vcpu(kvm, arg);
  2236. if (r < 0)
  2237. goto out;
  2238. break;
  2239. case KVM_SET_MEMORY_REGION: {
  2240. struct kvm_memory_region kvm_mem;
  2241. r = -EFAULT;
  2242. if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
  2243. goto out;
  2244. r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
  2245. if (r)
  2246. goto out;
  2247. break;
  2248. }
  2249. case KVM_GET_DIRTY_LOG: {
  2250. struct kvm_dirty_log log;
  2251. r = -EFAULT;
  2252. if (copy_from_user(&log, argp, sizeof log))
  2253. goto out;
  2254. r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
  2255. if (r)
  2256. goto out;
  2257. break;
  2258. }
  2259. case KVM_SET_MEMORY_ALIAS: {
  2260. struct kvm_memory_alias alias;
  2261. r = -EFAULT;
  2262. if (copy_from_user(&alias, argp, sizeof alias))
  2263. goto out;
  2264. r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
  2265. if (r)
  2266. goto out;
  2267. break;
  2268. }
  2269. default:
  2270. ;
  2271. }
  2272. out:
  2273. return r;
  2274. }
  2275. static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
  2276. unsigned long address,
  2277. int *type)
  2278. {
  2279. struct kvm *kvm = vma->vm_file->private_data;
  2280. unsigned long pgoff;
  2281. struct page *page;
  2282. *type = VM_FAULT_MINOR;
  2283. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  2284. page = gfn_to_page(kvm, pgoff);
  2285. if (!page)
  2286. return NOPAGE_SIGBUS;
  2287. get_page(page);
  2288. return page;
  2289. }
  2290. static struct vm_operations_struct kvm_vm_vm_ops = {
  2291. .nopage = kvm_vm_nopage,
  2292. };
  2293. static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
  2294. {
  2295. vma->vm_ops = &kvm_vm_vm_ops;
  2296. return 0;
  2297. }
  2298. static struct file_operations kvm_vm_fops = {
  2299. .release = kvm_vm_release,
  2300. .unlocked_ioctl = kvm_vm_ioctl,
  2301. .compat_ioctl = kvm_vm_ioctl,
  2302. .mmap = kvm_vm_mmap,
  2303. };
  2304. static int kvm_dev_ioctl_create_vm(void)
  2305. {
  2306. int fd, r;
  2307. struct inode *inode;
  2308. struct file *file;
  2309. struct kvm *kvm;
  2310. inode = kvmfs_inode(&kvm_vm_fops);
  2311. if (IS_ERR(inode)) {
  2312. r = PTR_ERR(inode);
  2313. goto out1;
  2314. }
  2315. kvm = kvm_create_vm();
  2316. if (IS_ERR(kvm)) {
  2317. r = PTR_ERR(kvm);
  2318. goto out2;
  2319. }
  2320. file = kvmfs_file(inode, kvm);
  2321. if (IS_ERR(file)) {
  2322. r = PTR_ERR(file);
  2323. goto out3;
  2324. }
  2325. kvm->filp = file;
  2326. r = get_unused_fd();
  2327. if (r < 0)
  2328. goto out4;
  2329. fd = r;
  2330. fd_install(fd, file);
  2331. return fd;
  2332. out4:
  2333. fput(file);
  2334. out3:
  2335. kvm_destroy_vm(kvm);
  2336. out2:
  2337. iput(inode);
  2338. out1:
  2339. return r;
  2340. }
  2341. static long kvm_dev_ioctl(struct file *filp,
  2342. unsigned int ioctl, unsigned long arg)
  2343. {
  2344. void __user *argp = (void __user *)arg;
  2345. long r = -EINVAL;
  2346. switch (ioctl) {
  2347. case KVM_GET_API_VERSION:
  2348. r = -EINVAL;
  2349. if (arg)
  2350. goto out;
  2351. r = KVM_API_VERSION;
  2352. break;
  2353. case KVM_CREATE_VM:
  2354. r = -EINVAL;
  2355. if (arg)
  2356. goto out;
  2357. r = kvm_dev_ioctl_create_vm();
  2358. break;
  2359. case KVM_GET_MSR_INDEX_LIST: {
  2360. struct kvm_msr_list __user *user_msr_list = argp;
  2361. struct kvm_msr_list msr_list;
  2362. unsigned n;
  2363. r = -EFAULT;
  2364. if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
  2365. goto out;
  2366. n = msr_list.nmsrs;
  2367. msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
  2368. if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
  2369. goto out;
  2370. r = -E2BIG;
  2371. if (n < num_msrs_to_save)
  2372. goto out;
  2373. r = -EFAULT;
  2374. if (copy_to_user(user_msr_list->indices, &msrs_to_save,
  2375. num_msrs_to_save * sizeof(u32)))
  2376. goto out;
  2377. if (copy_to_user(user_msr_list->indices
  2378. + num_msrs_to_save * sizeof(u32),
  2379. &emulated_msrs,
  2380. ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
  2381. goto out;
  2382. r = 0;
  2383. break;
  2384. }
  2385. case KVM_CHECK_EXTENSION:
  2386. /*
  2387. * No extensions defined at present.
  2388. */
  2389. r = 0;
  2390. break;
  2391. case KVM_GET_VCPU_MMAP_SIZE:
  2392. r = -EINVAL;
  2393. if (arg)
  2394. goto out;
  2395. r = 2 * PAGE_SIZE;
  2396. break;
  2397. default:
  2398. ;
  2399. }
  2400. out:
  2401. return r;
  2402. }
  2403. static struct file_operations kvm_chardev_ops = {
  2404. .open = kvm_dev_open,
  2405. .release = kvm_dev_release,
  2406. .unlocked_ioctl = kvm_dev_ioctl,
  2407. .compat_ioctl = kvm_dev_ioctl,
  2408. };
  2409. static struct miscdevice kvm_dev = {
  2410. KVM_MINOR,
  2411. "kvm",
  2412. &kvm_chardev_ops,
  2413. };
  2414. static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
  2415. void *v)
  2416. {
  2417. if (val == SYS_RESTART) {
  2418. /*
  2419. * Some (well, at least mine) BIOSes hang on reboot if
  2420. * in vmx root mode.
  2421. */
  2422. printk(KERN_INFO "kvm: exiting hardware virtualization\n");
  2423. on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
  2424. }
  2425. return NOTIFY_OK;
  2426. }
  2427. static struct notifier_block kvm_reboot_notifier = {
  2428. .notifier_call = kvm_reboot,
  2429. .priority = 0,
  2430. };
  2431. /*
  2432. * Make sure that a cpu that is being hot-unplugged does not have any vcpus
  2433. * cached on it.
  2434. */
  2435. static void decache_vcpus_on_cpu(int cpu)
  2436. {
  2437. struct kvm *vm;
  2438. struct kvm_vcpu *vcpu;
  2439. int i;
  2440. spin_lock(&kvm_lock);
  2441. list_for_each_entry(vm, &vm_list, vm_list)
  2442. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  2443. vcpu = &vm->vcpus[i];
  2444. /*
  2445. * If the vcpu is locked, then it is running on some
  2446. * other cpu and therefore it is not cached on the
  2447. * cpu in question.
  2448. *
  2449. * If it's not locked, check the last cpu it executed
  2450. * on.
  2451. */
  2452. if (mutex_trylock(&vcpu->mutex)) {
  2453. if (vcpu->cpu == cpu) {
  2454. kvm_arch_ops->vcpu_decache(vcpu);
  2455. vcpu->cpu = -1;
  2456. }
  2457. mutex_unlock(&vcpu->mutex);
  2458. }
  2459. }
  2460. spin_unlock(&kvm_lock);
  2461. }
  2462. static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
  2463. void *v)
  2464. {
  2465. int cpu = (long)v;
  2466. switch (val) {
  2467. case CPU_DOWN_PREPARE:
  2468. case CPU_DOWN_PREPARE_FROZEN:
  2469. case CPU_UP_CANCELED:
  2470. case CPU_UP_CANCELED_FROZEN:
  2471. printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
  2472. cpu);
  2473. decache_vcpus_on_cpu(cpu);
  2474. smp_call_function_single(cpu, kvm_arch_ops->hardware_disable,
  2475. NULL, 0, 1);
  2476. break;
  2477. case CPU_ONLINE:
  2478. case CPU_ONLINE_FROZEN:
  2479. printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
  2480. cpu);
  2481. smp_call_function_single(cpu, kvm_arch_ops->hardware_enable,
  2482. NULL, 0, 1);
  2483. break;
  2484. }
  2485. return NOTIFY_OK;
  2486. }
  2487. static struct notifier_block kvm_cpu_notifier = {
  2488. .notifier_call = kvm_cpu_hotplug,
  2489. .priority = 20, /* must be > scheduler priority */
  2490. };
  2491. static u64 stat_get(void *_offset)
  2492. {
  2493. unsigned offset = (long)_offset;
  2494. u64 total = 0;
  2495. struct kvm *kvm;
  2496. struct kvm_vcpu *vcpu;
  2497. int i;
  2498. spin_lock(&kvm_lock);
  2499. list_for_each_entry(kvm, &vm_list, vm_list)
  2500. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  2501. vcpu = &kvm->vcpus[i];
  2502. total += *(u32 *)((void *)vcpu + offset);
  2503. }
  2504. spin_unlock(&kvm_lock);
  2505. return total;
  2506. }
  2507. static void stat_set(void *offset, u64 val)
  2508. {
  2509. }
  2510. DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, stat_set, "%llu\n");
  2511. static __init void kvm_init_debug(void)
  2512. {
  2513. struct kvm_stats_debugfs_item *p;
  2514. debugfs_dir = debugfs_create_dir("kvm", NULL);
  2515. for (p = debugfs_entries; p->name; ++p)
  2516. p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
  2517. (void *)(long)p->offset,
  2518. &stat_fops);
  2519. }
  2520. static void kvm_exit_debug(void)
  2521. {
  2522. struct kvm_stats_debugfs_item *p;
  2523. for (p = debugfs_entries; p->name; ++p)
  2524. debugfs_remove(p->dentry);
  2525. debugfs_remove(debugfs_dir);
  2526. }
  2527. static int kvm_suspend(struct sys_device *dev, pm_message_t state)
  2528. {
  2529. decache_vcpus_on_cpu(raw_smp_processor_id());
  2530. on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
  2531. return 0;
  2532. }
  2533. static int kvm_resume(struct sys_device *dev)
  2534. {
  2535. on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
  2536. return 0;
  2537. }
  2538. static struct sysdev_class kvm_sysdev_class = {
  2539. set_kset_name("kvm"),
  2540. .suspend = kvm_suspend,
  2541. .resume = kvm_resume,
  2542. };
  2543. static struct sys_device kvm_sysdev = {
  2544. .id = 0,
  2545. .cls = &kvm_sysdev_class,
  2546. };
  2547. hpa_t bad_page_address;
  2548. static int kvmfs_get_sb(struct file_system_type *fs_type, int flags,
  2549. const char *dev_name, void *data, struct vfsmount *mnt)
  2550. {
  2551. return get_sb_pseudo(fs_type, "kvm:", NULL, KVMFS_SUPER_MAGIC, mnt);
  2552. }
  2553. static struct file_system_type kvm_fs_type = {
  2554. .name = "kvmfs",
  2555. .get_sb = kvmfs_get_sb,
  2556. .kill_sb = kill_anon_super,
  2557. };
  2558. int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
  2559. {
  2560. int r;
  2561. if (kvm_arch_ops) {
  2562. printk(KERN_ERR "kvm: already loaded the other module\n");
  2563. return -EEXIST;
  2564. }
  2565. if (!ops->cpu_has_kvm_support()) {
  2566. printk(KERN_ERR "kvm: no hardware support\n");
  2567. return -EOPNOTSUPP;
  2568. }
  2569. if (ops->disabled_by_bios()) {
  2570. printk(KERN_ERR "kvm: disabled by bios\n");
  2571. return -EOPNOTSUPP;
  2572. }
  2573. kvm_arch_ops = ops;
  2574. r = kvm_arch_ops->hardware_setup();
  2575. if (r < 0)
  2576. goto out;
  2577. on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
  2578. r = register_cpu_notifier(&kvm_cpu_notifier);
  2579. if (r)
  2580. goto out_free_1;
  2581. register_reboot_notifier(&kvm_reboot_notifier);
  2582. r = sysdev_class_register(&kvm_sysdev_class);
  2583. if (r)
  2584. goto out_free_2;
  2585. r = sysdev_register(&kvm_sysdev);
  2586. if (r)
  2587. goto out_free_3;
  2588. kvm_chardev_ops.owner = module;
  2589. r = misc_register(&kvm_dev);
  2590. if (r) {
  2591. printk (KERN_ERR "kvm: misc device register failed\n");
  2592. goto out_free;
  2593. }
  2594. return r;
  2595. out_free:
  2596. sysdev_unregister(&kvm_sysdev);
  2597. out_free_3:
  2598. sysdev_class_unregister(&kvm_sysdev_class);
  2599. out_free_2:
  2600. unregister_reboot_notifier(&kvm_reboot_notifier);
  2601. unregister_cpu_notifier(&kvm_cpu_notifier);
  2602. out_free_1:
  2603. on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
  2604. kvm_arch_ops->hardware_unsetup();
  2605. out:
  2606. kvm_arch_ops = NULL;
  2607. return r;
  2608. }
  2609. void kvm_exit_arch(void)
  2610. {
  2611. misc_deregister(&kvm_dev);
  2612. sysdev_unregister(&kvm_sysdev);
  2613. sysdev_class_unregister(&kvm_sysdev_class);
  2614. unregister_reboot_notifier(&kvm_reboot_notifier);
  2615. unregister_cpu_notifier(&kvm_cpu_notifier);
  2616. on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
  2617. kvm_arch_ops->hardware_unsetup();
  2618. kvm_arch_ops = NULL;
  2619. }
  2620. static __init int kvm_init(void)
  2621. {
  2622. static struct page *bad_page;
  2623. int r;
  2624. r = kvm_mmu_module_init();
  2625. if (r)
  2626. goto out4;
  2627. r = register_filesystem(&kvm_fs_type);
  2628. if (r)
  2629. goto out3;
  2630. kvmfs_mnt = kern_mount(&kvm_fs_type);
  2631. r = PTR_ERR(kvmfs_mnt);
  2632. if (IS_ERR(kvmfs_mnt))
  2633. goto out2;
  2634. kvm_init_debug();
  2635. kvm_init_msr_list();
  2636. if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
  2637. r = -ENOMEM;
  2638. goto out;
  2639. }
  2640. bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
  2641. memset(__va(bad_page_address), 0, PAGE_SIZE);
  2642. return 0;
  2643. out:
  2644. kvm_exit_debug();
  2645. mntput(kvmfs_mnt);
  2646. out2:
  2647. unregister_filesystem(&kvm_fs_type);
  2648. out3:
  2649. kvm_mmu_module_exit();
  2650. out4:
  2651. return r;
  2652. }
  2653. static __exit void kvm_exit(void)
  2654. {
  2655. kvm_exit_debug();
  2656. __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
  2657. mntput(kvmfs_mnt);
  2658. unregister_filesystem(&kvm_fs_type);
  2659. kvm_mmu_module_exit();
  2660. }
  2661. module_init(kvm_init)
  2662. module_exit(kvm_exit)
  2663. EXPORT_SYMBOL_GPL(kvm_init_arch);
  2664. EXPORT_SYMBOL_GPL(kvm_exit_arch);