kvm_main.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721
  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 "x86_emulate.h"
  42. #include "segment_descriptor.h"
  43. MODULE_AUTHOR("Qumranet");
  44. MODULE_LICENSE("GPL");
  45. static DEFINE_SPINLOCK(kvm_lock);
  46. static LIST_HEAD(vm_list);
  47. struct kvm_arch_ops *kvm_arch_ops;
  48. struct kvm_stat kvm_stat;
  49. EXPORT_SYMBOL_GPL(kvm_stat);
  50. static struct kvm_stats_debugfs_item {
  51. const char *name;
  52. u32 *data;
  53. struct dentry *dentry;
  54. } debugfs_entries[] = {
  55. { "pf_fixed", &kvm_stat.pf_fixed },
  56. { "pf_guest", &kvm_stat.pf_guest },
  57. { "tlb_flush", &kvm_stat.tlb_flush },
  58. { "invlpg", &kvm_stat.invlpg },
  59. { "exits", &kvm_stat.exits },
  60. { "io_exits", &kvm_stat.io_exits },
  61. { "mmio_exits", &kvm_stat.mmio_exits },
  62. { "signal_exits", &kvm_stat.signal_exits },
  63. { "irq_window", &kvm_stat.irq_window_exits },
  64. { "halt_exits", &kvm_stat.halt_exits },
  65. { "request_irq", &kvm_stat.request_irq_exits },
  66. { "irq_exits", &kvm_stat.irq_exits },
  67. { NULL, 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 kvm_free_vcpu(struct kvm_vcpu *vcpu)
  295. {
  296. if (!vcpu->vmcs)
  297. return;
  298. vcpu_load(vcpu);
  299. kvm_mmu_destroy(vcpu);
  300. vcpu_put(vcpu);
  301. kvm_arch_ops->vcpu_free(vcpu);
  302. free_page((unsigned long)vcpu->run);
  303. vcpu->run = NULL;
  304. }
  305. static void kvm_free_vcpus(struct kvm *kvm)
  306. {
  307. unsigned int i;
  308. for (i = 0; i < KVM_MAX_VCPUS; ++i)
  309. kvm_free_vcpu(&kvm->vcpus[i]);
  310. }
  311. static int kvm_dev_release(struct inode *inode, struct file *filp)
  312. {
  313. return 0;
  314. }
  315. static void kvm_destroy_vm(struct kvm *kvm)
  316. {
  317. spin_lock(&kvm_lock);
  318. list_del(&kvm->vm_list);
  319. spin_unlock(&kvm_lock);
  320. kvm_free_vcpus(kvm);
  321. kvm_free_physmem(kvm);
  322. kfree(kvm);
  323. }
  324. static int kvm_vm_release(struct inode *inode, struct file *filp)
  325. {
  326. struct kvm *kvm = filp->private_data;
  327. kvm_destroy_vm(kvm);
  328. return 0;
  329. }
  330. static void inject_gp(struct kvm_vcpu *vcpu)
  331. {
  332. kvm_arch_ops->inject_gp(vcpu, 0);
  333. }
  334. /*
  335. * Load the pae pdptrs. Return true is they are all valid.
  336. */
  337. static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
  338. {
  339. gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
  340. unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
  341. int i;
  342. u64 pdpte;
  343. u64 *pdpt;
  344. int ret;
  345. struct kvm_memory_slot *memslot;
  346. spin_lock(&vcpu->kvm->lock);
  347. memslot = gfn_to_memslot(vcpu->kvm, pdpt_gfn);
  348. /* FIXME: !memslot - emulate? 0xff? */
  349. pdpt = kmap_atomic(gfn_to_page(memslot, pdpt_gfn), KM_USER0);
  350. ret = 1;
  351. for (i = 0; i < 4; ++i) {
  352. pdpte = pdpt[offset + i];
  353. if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull)) {
  354. ret = 0;
  355. goto out;
  356. }
  357. }
  358. for (i = 0; i < 4; ++i)
  359. vcpu->pdptrs[i] = pdpt[offset + i];
  360. out:
  361. kunmap_atomic(pdpt, KM_USER0);
  362. spin_unlock(&vcpu->kvm->lock);
  363. return ret;
  364. }
  365. void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  366. {
  367. if (cr0 & CR0_RESEVED_BITS) {
  368. printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
  369. cr0, vcpu->cr0);
  370. inject_gp(vcpu);
  371. return;
  372. }
  373. if ((cr0 & CR0_NW_MASK) && !(cr0 & CR0_CD_MASK)) {
  374. printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
  375. inject_gp(vcpu);
  376. return;
  377. }
  378. if ((cr0 & CR0_PG_MASK) && !(cr0 & CR0_PE_MASK)) {
  379. printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
  380. "and a clear PE flag\n");
  381. inject_gp(vcpu);
  382. return;
  383. }
  384. if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK)) {
  385. #ifdef CONFIG_X86_64
  386. if ((vcpu->shadow_efer & EFER_LME)) {
  387. int cs_db, cs_l;
  388. if (!is_pae(vcpu)) {
  389. printk(KERN_DEBUG "set_cr0: #GP, start paging "
  390. "in long mode while PAE is disabled\n");
  391. inject_gp(vcpu);
  392. return;
  393. }
  394. kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  395. if (cs_l) {
  396. printk(KERN_DEBUG "set_cr0: #GP, start paging "
  397. "in long mode while CS.L == 1\n");
  398. inject_gp(vcpu);
  399. return;
  400. }
  401. } else
  402. #endif
  403. if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
  404. printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
  405. "reserved bits\n");
  406. inject_gp(vcpu);
  407. return;
  408. }
  409. }
  410. kvm_arch_ops->set_cr0(vcpu, cr0);
  411. vcpu->cr0 = cr0;
  412. spin_lock(&vcpu->kvm->lock);
  413. kvm_mmu_reset_context(vcpu);
  414. spin_unlock(&vcpu->kvm->lock);
  415. return;
  416. }
  417. EXPORT_SYMBOL_GPL(set_cr0);
  418. void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
  419. {
  420. kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
  421. set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
  422. }
  423. EXPORT_SYMBOL_GPL(lmsw);
  424. void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  425. {
  426. if (cr4 & CR4_RESEVED_BITS) {
  427. printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
  428. inject_gp(vcpu);
  429. return;
  430. }
  431. if (is_long_mode(vcpu)) {
  432. if (!(cr4 & CR4_PAE_MASK)) {
  433. printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
  434. "in long mode\n");
  435. inject_gp(vcpu);
  436. return;
  437. }
  438. } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & CR4_PAE_MASK)
  439. && !load_pdptrs(vcpu, vcpu->cr3)) {
  440. printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
  441. inject_gp(vcpu);
  442. }
  443. if (cr4 & CR4_VMXE_MASK) {
  444. printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
  445. inject_gp(vcpu);
  446. return;
  447. }
  448. kvm_arch_ops->set_cr4(vcpu, cr4);
  449. spin_lock(&vcpu->kvm->lock);
  450. kvm_mmu_reset_context(vcpu);
  451. spin_unlock(&vcpu->kvm->lock);
  452. }
  453. EXPORT_SYMBOL_GPL(set_cr4);
  454. void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  455. {
  456. if (is_long_mode(vcpu)) {
  457. if (cr3 & CR3_L_MODE_RESEVED_BITS) {
  458. printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
  459. inject_gp(vcpu);
  460. return;
  461. }
  462. } else {
  463. if (cr3 & CR3_RESEVED_BITS) {
  464. printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
  465. inject_gp(vcpu);
  466. return;
  467. }
  468. if (is_paging(vcpu) && is_pae(vcpu) &&
  469. !load_pdptrs(vcpu, cr3)) {
  470. printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
  471. "reserved bits\n");
  472. inject_gp(vcpu);
  473. return;
  474. }
  475. }
  476. vcpu->cr3 = cr3;
  477. spin_lock(&vcpu->kvm->lock);
  478. /*
  479. * Does the new cr3 value map to physical memory? (Note, we
  480. * catch an invalid cr3 even in real-mode, because it would
  481. * cause trouble later on when we turn on paging anyway.)
  482. *
  483. * A real CPU would silently accept an invalid cr3 and would
  484. * attempt to use it - with largely undefined (and often hard
  485. * to debug) behavior on the guest side.
  486. */
  487. if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
  488. inject_gp(vcpu);
  489. else
  490. vcpu->mmu.new_cr3(vcpu);
  491. spin_unlock(&vcpu->kvm->lock);
  492. }
  493. EXPORT_SYMBOL_GPL(set_cr3);
  494. void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
  495. {
  496. if ( cr8 & CR8_RESEVED_BITS) {
  497. printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
  498. inject_gp(vcpu);
  499. return;
  500. }
  501. vcpu->cr8 = cr8;
  502. }
  503. EXPORT_SYMBOL_GPL(set_cr8);
  504. void fx_init(struct kvm_vcpu *vcpu)
  505. {
  506. struct __attribute__ ((__packed__)) fx_image_s {
  507. u16 control; //fcw
  508. u16 status; //fsw
  509. u16 tag; // ftw
  510. u16 opcode; //fop
  511. u64 ip; // fpu ip
  512. u64 operand;// fpu dp
  513. u32 mxcsr;
  514. u32 mxcsr_mask;
  515. } *fx_image;
  516. fx_save(vcpu->host_fx_image);
  517. fpu_init();
  518. fx_save(vcpu->guest_fx_image);
  519. fx_restore(vcpu->host_fx_image);
  520. fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
  521. fx_image->mxcsr = 0x1f80;
  522. memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
  523. 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
  524. }
  525. EXPORT_SYMBOL_GPL(fx_init);
  526. static void do_remove_write_access(struct kvm_vcpu *vcpu, int slot)
  527. {
  528. spin_lock(&vcpu->kvm->lock);
  529. kvm_mmu_slot_remove_write_access(vcpu, slot);
  530. spin_unlock(&vcpu->kvm->lock);
  531. }
  532. /*
  533. * Allocate some memory and give it an address in the guest physical address
  534. * space.
  535. *
  536. * Discontiguous memory is allowed, mostly for framebuffers.
  537. */
  538. static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
  539. struct kvm_memory_region *mem)
  540. {
  541. int r;
  542. gfn_t base_gfn;
  543. unsigned long npages;
  544. unsigned long i;
  545. struct kvm_memory_slot *memslot;
  546. struct kvm_memory_slot old, new;
  547. int memory_config_version;
  548. r = -EINVAL;
  549. /* General sanity checks */
  550. if (mem->memory_size & (PAGE_SIZE - 1))
  551. goto out;
  552. if (mem->guest_phys_addr & (PAGE_SIZE - 1))
  553. goto out;
  554. if (mem->slot >= KVM_MEMORY_SLOTS)
  555. goto out;
  556. if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
  557. goto out;
  558. memslot = &kvm->memslots[mem->slot];
  559. base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
  560. npages = mem->memory_size >> PAGE_SHIFT;
  561. if (!npages)
  562. mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
  563. raced:
  564. spin_lock(&kvm->lock);
  565. memory_config_version = kvm->memory_config_version;
  566. new = old = *memslot;
  567. new.base_gfn = base_gfn;
  568. new.npages = npages;
  569. new.flags = mem->flags;
  570. /* Disallow changing a memory slot's size. */
  571. r = -EINVAL;
  572. if (npages && old.npages && npages != old.npages)
  573. goto out_unlock;
  574. /* Check for overlaps */
  575. r = -EEXIST;
  576. for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
  577. struct kvm_memory_slot *s = &kvm->memslots[i];
  578. if (s == memslot)
  579. continue;
  580. if (!((base_gfn + npages <= s->base_gfn) ||
  581. (base_gfn >= s->base_gfn + s->npages)))
  582. goto out_unlock;
  583. }
  584. /*
  585. * Do memory allocations outside lock. memory_config_version will
  586. * detect any races.
  587. */
  588. spin_unlock(&kvm->lock);
  589. /* Deallocate if slot is being removed */
  590. if (!npages)
  591. new.phys_mem = NULL;
  592. /* Free page dirty bitmap if unneeded */
  593. if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
  594. new.dirty_bitmap = NULL;
  595. r = -ENOMEM;
  596. /* Allocate if a slot is being created */
  597. if (npages && !new.phys_mem) {
  598. new.phys_mem = vmalloc(npages * sizeof(struct page *));
  599. if (!new.phys_mem)
  600. goto out_free;
  601. memset(new.phys_mem, 0, npages * sizeof(struct page *));
  602. for (i = 0; i < npages; ++i) {
  603. new.phys_mem[i] = alloc_page(GFP_HIGHUSER
  604. | __GFP_ZERO);
  605. if (!new.phys_mem[i])
  606. goto out_free;
  607. set_page_private(new.phys_mem[i],0);
  608. }
  609. }
  610. /* Allocate page dirty bitmap if needed */
  611. if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
  612. unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
  613. new.dirty_bitmap = vmalloc(dirty_bytes);
  614. if (!new.dirty_bitmap)
  615. goto out_free;
  616. memset(new.dirty_bitmap, 0, dirty_bytes);
  617. }
  618. spin_lock(&kvm->lock);
  619. if (memory_config_version != kvm->memory_config_version) {
  620. spin_unlock(&kvm->lock);
  621. kvm_free_physmem_slot(&new, &old);
  622. goto raced;
  623. }
  624. r = -EAGAIN;
  625. if (kvm->busy)
  626. goto out_unlock;
  627. if (mem->slot >= kvm->nmemslots)
  628. kvm->nmemslots = mem->slot + 1;
  629. *memslot = new;
  630. ++kvm->memory_config_version;
  631. spin_unlock(&kvm->lock);
  632. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  633. struct kvm_vcpu *vcpu;
  634. vcpu = vcpu_load_slot(kvm, i);
  635. if (!vcpu)
  636. continue;
  637. if (new.flags & KVM_MEM_LOG_DIRTY_PAGES)
  638. do_remove_write_access(vcpu, mem->slot);
  639. kvm_mmu_reset_context(vcpu);
  640. vcpu_put(vcpu);
  641. }
  642. kvm_free_physmem_slot(&old, &new);
  643. return 0;
  644. out_unlock:
  645. spin_unlock(&kvm->lock);
  646. out_free:
  647. kvm_free_physmem_slot(&new, &old);
  648. out:
  649. return r;
  650. }
  651. /*
  652. * Get (and clear) the dirty memory log for a memory slot.
  653. */
  654. static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
  655. struct kvm_dirty_log *log)
  656. {
  657. struct kvm_memory_slot *memslot;
  658. int r, i;
  659. int n;
  660. int cleared;
  661. unsigned long any = 0;
  662. spin_lock(&kvm->lock);
  663. /*
  664. * Prevent changes to guest memory configuration even while the lock
  665. * is not taken.
  666. */
  667. ++kvm->busy;
  668. spin_unlock(&kvm->lock);
  669. r = -EINVAL;
  670. if (log->slot >= KVM_MEMORY_SLOTS)
  671. goto out;
  672. memslot = &kvm->memslots[log->slot];
  673. r = -ENOENT;
  674. if (!memslot->dirty_bitmap)
  675. goto out;
  676. n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
  677. for (i = 0; !any && i < n/sizeof(long); ++i)
  678. any = memslot->dirty_bitmap[i];
  679. r = -EFAULT;
  680. if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
  681. goto out;
  682. if (any) {
  683. cleared = 0;
  684. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  685. struct kvm_vcpu *vcpu;
  686. vcpu = vcpu_load_slot(kvm, i);
  687. if (!vcpu)
  688. continue;
  689. if (!cleared) {
  690. do_remove_write_access(vcpu, log->slot);
  691. memset(memslot->dirty_bitmap, 0, n);
  692. cleared = 1;
  693. }
  694. kvm_arch_ops->tlb_flush(vcpu);
  695. vcpu_put(vcpu);
  696. }
  697. }
  698. r = 0;
  699. out:
  700. spin_lock(&kvm->lock);
  701. --kvm->busy;
  702. spin_unlock(&kvm->lock);
  703. return r;
  704. }
  705. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
  706. {
  707. int i;
  708. for (i = 0; i < kvm->nmemslots; ++i) {
  709. struct kvm_memory_slot *memslot = &kvm->memslots[i];
  710. if (gfn >= memslot->base_gfn
  711. && gfn < memslot->base_gfn + memslot->npages)
  712. return memslot;
  713. }
  714. return NULL;
  715. }
  716. EXPORT_SYMBOL_GPL(gfn_to_memslot);
  717. void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
  718. {
  719. int i;
  720. struct kvm_memory_slot *memslot = NULL;
  721. unsigned long rel_gfn;
  722. for (i = 0; i < kvm->nmemslots; ++i) {
  723. memslot = &kvm->memslots[i];
  724. if (gfn >= memslot->base_gfn
  725. && gfn < memslot->base_gfn + memslot->npages) {
  726. if (!memslot || !memslot->dirty_bitmap)
  727. return;
  728. rel_gfn = gfn - memslot->base_gfn;
  729. /* avoid RMW */
  730. if (!test_bit(rel_gfn, memslot->dirty_bitmap))
  731. set_bit(rel_gfn, memslot->dirty_bitmap);
  732. return;
  733. }
  734. }
  735. }
  736. static int emulator_read_std(unsigned long addr,
  737. unsigned long *val,
  738. unsigned int bytes,
  739. struct x86_emulate_ctxt *ctxt)
  740. {
  741. struct kvm_vcpu *vcpu = ctxt->vcpu;
  742. void *data = val;
  743. while (bytes) {
  744. gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
  745. unsigned offset = addr & (PAGE_SIZE-1);
  746. unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
  747. unsigned long pfn;
  748. struct kvm_memory_slot *memslot;
  749. void *page;
  750. if (gpa == UNMAPPED_GVA)
  751. return X86EMUL_PROPAGATE_FAULT;
  752. pfn = gpa >> PAGE_SHIFT;
  753. memslot = gfn_to_memslot(vcpu->kvm, pfn);
  754. if (!memslot)
  755. return X86EMUL_UNHANDLEABLE;
  756. page = kmap_atomic(gfn_to_page(memslot, pfn), KM_USER0);
  757. memcpy(data, page + offset, tocopy);
  758. kunmap_atomic(page, KM_USER0);
  759. bytes -= tocopy;
  760. data += tocopy;
  761. addr += tocopy;
  762. }
  763. return X86EMUL_CONTINUE;
  764. }
  765. static int emulator_write_std(unsigned long addr,
  766. unsigned long val,
  767. unsigned int bytes,
  768. struct x86_emulate_ctxt *ctxt)
  769. {
  770. printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
  771. addr, bytes);
  772. return X86EMUL_UNHANDLEABLE;
  773. }
  774. static int emulator_read_emulated(unsigned long addr,
  775. unsigned long *val,
  776. unsigned int bytes,
  777. struct x86_emulate_ctxt *ctxt)
  778. {
  779. struct kvm_vcpu *vcpu = ctxt->vcpu;
  780. if (vcpu->mmio_read_completed) {
  781. memcpy(val, vcpu->mmio_data, bytes);
  782. vcpu->mmio_read_completed = 0;
  783. return X86EMUL_CONTINUE;
  784. } else if (emulator_read_std(addr, val, bytes, ctxt)
  785. == X86EMUL_CONTINUE)
  786. return X86EMUL_CONTINUE;
  787. else {
  788. gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
  789. if (gpa == UNMAPPED_GVA)
  790. return X86EMUL_PROPAGATE_FAULT;
  791. vcpu->mmio_needed = 1;
  792. vcpu->mmio_phys_addr = gpa;
  793. vcpu->mmio_size = bytes;
  794. vcpu->mmio_is_write = 0;
  795. return X86EMUL_UNHANDLEABLE;
  796. }
  797. }
  798. static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
  799. unsigned long val, int bytes)
  800. {
  801. struct kvm_memory_slot *m;
  802. struct page *page;
  803. void *virt;
  804. if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
  805. return 0;
  806. m = gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT);
  807. if (!m)
  808. return 0;
  809. page = gfn_to_page(m, gpa >> PAGE_SHIFT);
  810. kvm_mmu_pre_write(vcpu, gpa, bytes);
  811. mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
  812. virt = kmap_atomic(page, KM_USER0);
  813. memcpy(virt + offset_in_page(gpa), &val, bytes);
  814. kunmap_atomic(virt, KM_USER0);
  815. kvm_mmu_post_write(vcpu, gpa, bytes);
  816. return 1;
  817. }
  818. static int emulator_write_emulated(unsigned long addr,
  819. unsigned long val,
  820. unsigned int bytes,
  821. struct x86_emulate_ctxt *ctxt)
  822. {
  823. struct kvm_vcpu *vcpu = ctxt->vcpu;
  824. gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
  825. if (gpa == UNMAPPED_GVA)
  826. return X86EMUL_PROPAGATE_FAULT;
  827. if (emulator_write_phys(vcpu, gpa, val, bytes))
  828. return X86EMUL_CONTINUE;
  829. vcpu->mmio_needed = 1;
  830. vcpu->mmio_phys_addr = gpa;
  831. vcpu->mmio_size = bytes;
  832. vcpu->mmio_is_write = 1;
  833. memcpy(vcpu->mmio_data, &val, bytes);
  834. return X86EMUL_CONTINUE;
  835. }
  836. static int emulator_cmpxchg_emulated(unsigned long addr,
  837. unsigned long old,
  838. unsigned long new,
  839. unsigned int bytes,
  840. struct x86_emulate_ctxt *ctxt)
  841. {
  842. static int reported;
  843. if (!reported) {
  844. reported = 1;
  845. printk(KERN_WARNING "kvm: emulating exchange as write\n");
  846. }
  847. return emulator_write_emulated(addr, new, bytes, ctxt);
  848. }
  849. #ifdef CONFIG_X86_32
  850. static int emulator_cmpxchg8b_emulated(unsigned long addr,
  851. unsigned long old_lo,
  852. unsigned long old_hi,
  853. unsigned long new_lo,
  854. unsigned long new_hi,
  855. struct x86_emulate_ctxt *ctxt)
  856. {
  857. static int reported;
  858. int r;
  859. if (!reported) {
  860. reported = 1;
  861. printk(KERN_WARNING "kvm: emulating exchange8b as write\n");
  862. }
  863. r = emulator_write_emulated(addr, new_lo, 4, ctxt);
  864. if (r != X86EMUL_CONTINUE)
  865. return r;
  866. return emulator_write_emulated(addr+4, new_hi, 4, ctxt);
  867. }
  868. #endif
  869. static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
  870. {
  871. return kvm_arch_ops->get_segment_base(vcpu, seg);
  872. }
  873. int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
  874. {
  875. return X86EMUL_CONTINUE;
  876. }
  877. int emulate_clts(struct kvm_vcpu *vcpu)
  878. {
  879. unsigned long cr0;
  880. kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
  881. cr0 = vcpu->cr0 & ~CR0_TS_MASK;
  882. kvm_arch_ops->set_cr0(vcpu, cr0);
  883. return X86EMUL_CONTINUE;
  884. }
  885. int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
  886. {
  887. struct kvm_vcpu *vcpu = ctxt->vcpu;
  888. switch (dr) {
  889. case 0 ... 3:
  890. *dest = kvm_arch_ops->get_dr(vcpu, dr);
  891. return X86EMUL_CONTINUE;
  892. default:
  893. printk(KERN_DEBUG "%s: unexpected dr %u\n",
  894. __FUNCTION__, dr);
  895. return X86EMUL_UNHANDLEABLE;
  896. }
  897. }
  898. int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
  899. {
  900. unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
  901. int exception;
  902. kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
  903. if (exception) {
  904. /* FIXME: better handling */
  905. return X86EMUL_UNHANDLEABLE;
  906. }
  907. return X86EMUL_CONTINUE;
  908. }
  909. static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
  910. {
  911. static int reported;
  912. u8 opcodes[4];
  913. unsigned long rip = ctxt->vcpu->rip;
  914. unsigned long rip_linear;
  915. rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
  916. if (reported)
  917. return;
  918. emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
  919. printk(KERN_ERR "emulation failed but !mmio_needed?"
  920. " rip %lx %02x %02x %02x %02x\n",
  921. rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
  922. reported = 1;
  923. }
  924. struct x86_emulate_ops emulate_ops = {
  925. .read_std = emulator_read_std,
  926. .write_std = emulator_write_std,
  927. .read_emulated = emulator_read_emulated,
  928. .write_emulated = emulator_write_emulated,
  929. .cmpxchg_emulated = emulator_cmpxchg_emulated,
  930. #ifdef CONFIG_X86_32
  931. .cmpxchg8b_emulated = emulator_cmpxchg8b_emulated,
  932. #endif
  933. };
  934. int emulate_instruction(struct kvm_vcpu *vcpu,
  935. struct kvm_run *run,
  936. unsigned long cr2,
  937. u16 error_code)
  938. {
  939. struct x86_emulate_ctxt emulate_ctxt;
  940. int r;
  941. int cs_db, cs_l;
  942. kvm_arch_ops->cache_regs(vcpu);
  943. kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  944. emulate_ctxt.vcpu = vcpu;
  945. emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
  946. emulate_ctxt.cr2 = cr2;
  947. emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
  948. ? X86EMUL_MODE_REAL : cs_l
  949. ? X86EMUL_MODE_PROT64 : cs_db
  950. ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
  951. if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
  952. emulate_ctxt.cs_base = 0;
  953. emulate_ctxt.ds_base = 0;
  954. emulate_ctxt.es_base = 0;
  955. emulate_ctxt.ss_base = 0;
  956. } else {
  957. emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
  958. emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
  959. emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
  960. emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
  961. }
  962. emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
  963. emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
  964. vcpu->mmio_is_write = 0;
  965. r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
  966. if ((r || vcpu->mmio_is_write) && run) {
  967. run->mmio.phys_addr = vcpu->mmio_phys_addr;
  968. memcpy(run->mmio.data, vcpu->mmio_data, 8);
  969. run->mmio.len = vcpu->mmio_size;
  970. run->mmio.is_write = vcpu->mmio_is_write;
  971. }
  972. if (r) {
  973. if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
  974. return EMULATE_DONE;
  975. if (!vcpu->mmio_needed) {
  976. report_emulation_failure(&emulate_ctxt);
  977. return EMULATE_FAIL;
  978. }
  979. return EMULATE_DO_MMIO;
  980. }
  981. kvm_arch_ops->decache_regs(vcpu);
  982. kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
  983. if (vcpu->mmio_is_write)
  984. return EMULATE_DO_MMIO;
  985. return EMULATE_DONE;
  986. }
  987. EXPORT_SYMBOL_GPL(emulate_instruction);
  988. int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
  989. {
  990. unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
  991. kvm_arch_ops->cache_regs(vcpu);
  992. ret = -KVM_EINVAL;
  993. #ifdef CONFIG_X86_64
  994. if (is_long_mode(vcpu)) {
  995. nr = vcpu->regs[VCPU_REGS_RAX];
  996. a0 = vcpu->regs[VCPU_REGS_RDI];
  997. a1 = vcpu->regs[VCPU_REGS_RSI];
  998. a2 = vcpu->regs[VCPU_REGS_RDX];
  999. a3 = vcpu->regs[VCPU_REGS_RCX];
  1000. a4 = vcpu->regs[VCPU_REGS_R8];
  1001. a5 = vcpu->regs[VCPU_REGS_R9];
  1002. } else
  1003. #endif
  1004. {
  1005. nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
  1006. a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
  1007. a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
  1008. a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
  1009. a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
  1010. a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
  1011. a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
  1012. }
  1013. switch (nr) {
  1014. default:
  1015. run->hypercall.args[0] = a0;
  1016. run->hypercall.args[1] = a1;
  1017. run->hypercall.args[2] = a2;
  1018. run->hypercall.args[3] = a3;
  1019. run->hypercall.args[4] = a4;
  1020. run->hypercall.args[5] = a5;
  1021. run->hypercall.ret = ret;
  1022. run->hypercall.longmode = is_long_mode(vcpu);
  1023. kvm_arch_ops->decache_regs(vcpu);
  1024. return 0;
  1025. }
  1026. vcpu->regs[VCPU_REGS_RAX] = ret;
  1027. kvm_arch_ops->decache_regs(vcpu);
  1028. return 1;
  1029. }
  1030. EXPORT_SYMBOL_GPL(kvm_hypercall);
  1031. static u64 mk_cr_64(u64 curr_cr, u32 new_val)
  1032. {
  1033. return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
  1034. }
  1035. void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  1036. {
  1037. struct descriptor_table dt = { limit, base };
  1038. kvm_arch_ops->set_gdt(vcpu, &dt);
  1039. }
  1040. void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  1041. {
  1042. struct descriptor_table dt = { limit, base };
  1043. kvm_arch_ops->set_idt(vcpu, &dt);
  1044. }
  1045. void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
  1046. unsigned long *rflags)
  1047. {
  1048. lmsw(vcpu, msw);
  1049. *rflags = kvm_arch_ops->get_rflags(vcpu);
  1050. }
  1051. unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
  1052. {
  1053. kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
  1054. switch (cr) {
  1055. case 0:
  1056. return vcpu->cr0;
  1057. case 2:
  1058. return vcpu->cr2;
  1059. case 3:
  1060. return vcpu->cr3;
  1061. case 4:
  1062. return vcpu->cr4;
  1063. default:
  1064. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
  1065. return 0;
  1066. }
  1067. }
  1068. void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
  1069. unsigned long *rflags)
  1070. {
  1071. switch (cr) {
  1072. case 0:
  1073. set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
  1074. *rflags = kvm_arch_ops->get_rflags(vcpu);
  1075. break;
  1076. case 2:
  1077. vcpu->cr2 = val;
  1078. break;
  1079. case 3:
  1080. set_cr3(vcpu, val);
  1081. break;
  1082. case 4:
  1083. set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
  1084. break;
  1085. default:
  1086. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
  1087. }
  1088. }
  1089. /*
  1090. * Register the para guest with the host:
  1091. */
  1092. static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
  1093. {
  1094. struct kvm_vcpu_para_state *para_state;
  1095. hpa_t para_state_hpa, hypercall_hpa;
  1096. struct page *para_state_page;
  1097. unsigned char *hypercall;
  1098. gpa_t hypercall_gpa;
  1099. printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
  1100. printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
  1101. /*
  1102. * Needs to be page aligned:
  1103. */
  1104. if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
  1105. goto err_gp;
  1106. para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
  1107. printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
  1108. if (is_error_hpa(para_state_hpa))
  1109. goto err_gp;
  1110. mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
  1111. para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
  1112. para_state = kmap_atomic(para_state_page, KM_USER0);
  1113. printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
  1114. printk(KERN_DEBUG ".... size: %d\n", para_state->size);
  1115. para_state->host_version = KVM_PARA_API_VERSION;
  1116. /*
  1117. * We cannot support guests that try to register themselves
  1118. * with a newer API version than the host supports:
  1119. */
  1120. if (para_state->guest_version > KVM_PARA_API_VERSION) {
  1121. para_state->ret = -KVM_EINVAL;
  1122. goto err_kunmap_skip;
  1123. }
  1124. hypercall_gpa = para_state->hypercall_gpa;
  1125. hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
  1126. printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
  1127. if (is_error_hpa(hypercall_hpa)) {
  1128. para_state->ret = -KVM_EINVAL;
  1129. goto err_kunmap_skip;
  1130. }
  1131. printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
  1132. vcpu->para_state_page = para_state_page;
  1133. vcpu->para_state_gpa = para_state_gpa;
  1134. vcpu->hypercall_gpa = hypercall_gpa;
  1135. mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
  1136. hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
  1137. KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
  1138. kvm_arch_ops->patch_hypercall(vcpu, hypercall);
  1139. kunmap_atomic(hypercall, KM_USER1);
  1140. para_state->ret = 0;
  1141. err_kunmap_skip:
  1142. kunmap_atomic(para_state, KM_USER0);
  1143. return 0;
  1144. err_gp:
  1145. return 1;
  1146. }
  1147. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1148. {
  1149. u64 data;
  1150. switch (msr) {
  1151. case 0xc0010010: /* SYSCFG */
  1152. case 0xc0010015: /* HWCR */
  1153. case MSR_IA32_PLATFORM_ID:
  1154. case MSR_IA32_P5_MC_ADDR:
  1155. case MSR_IA32_P5_MC_TYPE:
  1156. case MSR_IA32_MC0_CTL:
  1157. case MSR_IA32_MCG_STATUS:
  1158. case MSR_IA32_MCG_CAP:
  1159. case MSR_IA32_MC0_MISC:
  1160. case MSR_IA32_MC0_MISC+4:
  1161. case MSR_IA32_MC0_MISC+8:
  1162. case MSR_IA32_MC0_MISC+12:
  1163. case MSR_IA32_MC0_MISC+16:
  1164. case MSR_IA32_UCODE_REV:
  1165. case MSR_IA32_PERF_STATUS:
  1166. /* MTRR registers */
  1167. case 0xfe:
  1168. case 0x200 ... 0x2ff:
  1169. data = 0;
  1170. break;
  1171. case 0xcd: /* fsb frequency */
  1172. data = 3;
  1173. break;
  1174. case MSR_IA32_APICBASE:
  1175. data = vcpu->apic_base;
  1176. break;
  1177. case MSR_IA32_MISC_ENABLE:
  1178. data = vcpu->ia32_misc_enable_msr;
  1179. break;
  1180. #ifdef CONFIG_X86_64
  1181. case MSR_EFER:
  1182. data = vcpu->shadow_efer;
  1183. break;
  1184. #endif
  1185. default:
  1186. printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
  1187. return 1;
  1188. }
  1189. *pdata = data;
  1190. return 0;
  1191. }
  1192. EXPORT_SYMBOL_GPL(kvm_get_msr_common);
  1193. /*
  1194. * Reads an msr value (of 'msr_index') into 'pdata'.
  1195. * Returns 0 on success, non-0 otherwise.
  1196. * Assumes vcpu_load() was already called.
  1197. */
  1198. static int get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  1199. {
  1200. return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
  1201. }
  1202. #ifdef CONFIG_X86_64
  1203. static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
  1204. {
  1205. if (efer & EFER_RESERVED_BITS) {
  1206. printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
  1207. efer);
  1208. inject_gp(vcpu);
  1209. return;
  1210. }
  1211. if (is_paging(vcpu)
  1212. && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
  1213. printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
  1214. inject_gp(vcpu);
  1215. return;
  1216. }
  1217. kvm_arch_ops->set_efer(vcpu, efer);
  1218. efer &= ~EFER_LMA;
  1219. efer |= vcpu->shadow_efer & EFER_LMA;
  1220. vcpu->shadow_efer = efer;
  1221. }
  1222. #endif
  1223. int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  1224. {
  1225. switch (msr) {
  1226. #ifdef CONFIG_X86_64
  1227. case MSR_EFER:
  1228. set_efer(vcpu, data);
  1229. break;
  1230. #endif
  1231. case MSR_IA32_MC0_STATUS:
  1232. printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
  1233. __FUNCTION__, data);
  1234. break;
  1235. case MSR_IA32_UCODE_REV:
  1236. case MSR_IA32_UCODE_WRITE:
  1237. case 0x200 ... 0x2ff: /* MTRRs */
  1238. break;
  1239. case MSR_IA32_APICBASE:
  1240. vcpu->apic_base = data;
  1241. break;
  1242. case MSR_IA32_MISC_ENABLE:
  1243. vcpu->ia32_misc_enable_msr = data;
  1244. break;
  1245. /*
  1246. * This is the 'probe whether the host is KVM' logic:
  1247. */
  1248. case MSR_KVM_API_MAGIC:
  1249. return vcpu_register_para(vcpu, data);
  1250. default:
  1251. printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
  1252. return 1;
  1253. }
  1254. return 0;
  1255. }
  1256. EXPORT_SYMBOL_GPL(kvm_set_msr_common);
  1257. /*
  1258. * Writes msr value into into the appropriate "register".
  1259. * Returns 0 on success, non-0 otherwise.
  1260. * Assumes vcpu_load() was already called.
  1261. */
  1262. static int set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  1263. {
  1264. return kvm_arch_ops->set_msr(vcpu, msr_index, data);
  1265. }
  1266. void kvm_resched(struct kvm_vcpu *vcpu)
  1267. {
  1268. vcpu_put(vcpu);
  1269. cond_resched();
  1270. vcpu_load(vcpu);
  1271. }
  1272. EXPORT_SYMBOL_GPL(kvm_resched);
  1273. void load_msrs(struct vmx_msr_entry *e, int n)
  1274. {
  1275. int i;
  1276. for (i = 0; i < n; ++i)
  1277. wrmsrl(e[i].index, e[i].data);
  1278. }
  1279. EXPORT_SYMBOL_GPL(load_msrs);
  1280. void save_msrs(struct vmx_msr_entry *e, int n)
  1281. {
  1282. int i;
  1283. for (i = 0; i < n; ++i)
  1284. rdmsrl(e[i].index, e[i].data);
  1285. }
  1286. EXPORT_SYMBOL_GPL(save_msrs);
  1287. void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
  1288. {
  1289. int i;
  1290. u32 function;
  1291. struct kvm_cpuid_entry *e, *best;
  1292. kvm_arch_ops->cache_regs(vcpu);
  1293. function = vcpu->regs[VCPU_REGS_RAX];
  1294. vcpu->regs[VCPU_REGS_RAX] = 0;
  1295. vcpu->regs[VCPU_REGS_RBX] = 0;
  1296. vcpu->regs[VCPU_REGS_RCX] = 0;
  1297. vcpu->regs[VCPU_REGS_RDX] = 0;
  1298. best = NULL;
  1299. for (i = 0; i < vcpu->cpuid_nent; ++i) {
  1300. e = &vcpu->cpuid_entries[i];
  1301. if (e->function == function) {
  1302. best = e;
  1303. break;
  1304. }
  1305. /*
  1306. * Both basic or both extended?
  1307. */
  1308. if (((e->function ^ function) & 0x80000000) == 0)
  1309. if (!best || e->function > best->function)
  1310. best = e;
  1311. }
  1312. if (best) {
  1313. vcpu->regs[VCPU_REGS_RAX] = best->eax;
  1314. vcpu->regs[VCPU_REGS_RBX] = best->ebx;
  1315. vcpu->regs[VCPU_REGS_RCX] = best->ecx;
  1316. vcpu->regs[VCPU_REGS_RDX] = best->edx;
  1317. }
  1318. kvm_arch_ops->decache_regs(vcpu);
  1319. kvm_arch_ops->skip_emulated_instruction(vcpu);
  1320. }
  1321. EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
  1322. static void complete_pio(struct kvm_vcpu *vcpu)
  1323. {
  1324. struct kvm_io *io = &vcpu->run->io;
  1325. long delta;
  1326. kvm_arch_ops->cache_regs(vcpu);
  1327. if (!io->string) {
  1328. if (io->direction == KVM_EXIT_IO_IN)
  1329. memcpy(&vcpu->regs[VCPU_REGS_RAX], &io->value,
  1330. io->size);
  1331. } else {
  1332. delta = 1;
  1333. if (io->rep) {
  1334. delta *= io->count;
  1335. /*
  1336. * The size of the register should really depend on
  1337. * current address size.
  1338. */
  1339. vcpu->regs[VCPU_REGS_RCX] -= delta;
  1340. }
  1341. if (io->string_down)
  1342. delta = -delta;
  1343. delta *= io->size;
  1344. if (io->direction == KVM_EXIT_IO_IN)
  1345. vcpu->regs[VCPU_REGS_RDI] += delta;
  1346. else
  1347. vcpu->regs[VCPU_REGS_RSI] += delta;
  1348. }
  1349. vcpu->pio_pending = 0;
  1350. vcpu->run->io_completed = 0;
  1351. kvm_arch_ops->decache_regs(vcpu);
  1352. kvm_arch_ops->skip_emulated_instruction(vcpu);
  1353. }
  1354. static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  1355. {
  1356. int r;
  1357. vcpu_load(vcpu);
  1358. /* re-sync apic's tpr */
  1359. vcpu->cr8 = kvm_run->cr8;
  1360. if (kvm_run->io_completed) {
  1361. if (vcpu->pio_pending)
  1362. complete_pio(vcpu);
  1363. else {
  1364. memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
  1365. vcpu->mmio_read_completed = 1;
  1366. }
  1367. }
  1368. vcpu->mmio_needed = 0;
  1369. if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
  1370. kvm_arch_ops->cache_regs(vcpu);
  1371. vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
  1372. kvm_arch_ops->decache_regs(vcpu);
  1373. }
  1374. r = kvm_arch_ops->run(vcpu, kvm_run);
  1375. vcpu_put(vcpu);
  1376. return r;
  1377. }
  1378. static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
  1379. struct kvm_regs *regs)
  1380. {
  1381. vcpu_load(vcpu);
  1382. kvm_arch_ops->cache_regs(vcpu);
  1383. regs->rax = vcpu->regs[VCPU_REGS_RAX];
  1384. regs->rbx = vcpu->regs[VCPU_REGS_RBX];
  1385. regs->rcx = vcpu->regs[VCPU_REGS_RCX];
  1386. regs->rdx = vcpu->regs[VCPU_REGS_RDX];
  1387. regs->rsi = vcpu->regs[VCPU_REGS_RSI];
  1388. regs->rdi = vcpu->regs[VCPU_REGS_RDI];
  1389. regs->rsp = vcpu->regs[VCPU_REGS_RSP];
  1390. regs->rbp = vcpu->regs[VCPU_REGS_RBP];
  1391. #ifdef CONFIG_X86_64
  1392. regs->r8 = vcpu->regs[VCPU_REGS_R8];
  1393. regs->r9 = vcpu->regs[VCPU_REGS_R9];
  1394. regs->r10 = vcpu->regs[VCPU_REGS_R10];
  1395. regs->r11 = vcpu->regs[VCPU_REGS_R11];
  1396. regs->r12 = vcpu->regs[VCPU_REGS_R12];
  1397. regs->r13 = vcpu->regs[VCPU_REGS_R13];
  1398. regs->r14 = vcpu->regs[VCPU_REGS_R14];
  1399. regs->r15 = vcpu->regs[VCPU_REGS_R15];
  1400. #endif
  1401. regs->rip = vcpu->rip;
  1402. regs->rflags = kvm_arch_ops->get_rflags(vcpu);
  1403. /*
  1404. * Don't leak debug flags in case they were set for guest debugging
  1405. */
  1406. if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
  1407. regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
  1408. vcpu_put(vcpu);
  1409. return 0;
  1410. }
  1411. static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
  1412. struct kvm_regs *regs)
  1413. {
  1414. vcpu_load(vcpu);
  1415. vcpu->regs[VCPU_REGS_RAX] = regs->rax;
  1416. vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
  1417. vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
  1418. vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
  1419. vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
  1420. vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
  1421. vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
  1422. vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
  1423. #ifdef CONFIG_X86_64
  1424. vcpu->regs[VCPU_REGS_R8] = regs->r8;
  1425. vcpu->regs[VCPU_REGS_R9] = regs->r9;
  1426. vcpu->regs[VCPU_REGS_R10] = regs->r10;
  1427. vcpu->regs[VCPU_REGS_R11] = regs->r11;
  1428. vcpu->regs[VCPU_REGS_R12] = regs->r12;
  1429. vcpu->regs[VCPU_REGS_R13] = regs->r13;
  1430. vcpu->regs[VCPU_REGS_R14] = regs->r14;
  1431. vcpu->regs[VCPU_REGS_R15] = regs->r15;
  1432. #endif
  1433. vcpu->rip = regs->rip;
  1434. kvm_arch_ops->set_rflags(vcpu, regs->rflags);
  1435. kvm_arch_ops->decache_regs(vcpu);
  1436. vcpu_put(vcpu);
  1437. return 0;
  1438. }
  1439. static void get_segment(struct kvm_vcpu *vcpu,
  1440. struct kvm_segment *var, int seg)
  1441. {
  1442. return kvm_arch_ops->get_segment(vcpu, var, seg);
  1443. }
  1444. static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  1445. struct kvm_sregs *sregs)
  1446. {
  1447. struct descriptor_table dt;
  1448. vcpu_load(vcpu);
  1449. get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  1450. get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  1451. get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  1452. get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  1453. get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  1454. get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  1455. get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  1456. get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  1457. kvm_arch_ops->get_idt(vcpu, &dt);
  1458. sregs->idt.limit = dt.limit;
  1459. sregs->idt.base = dt.base;
  1460. kvm_arch_ops->get_gdt(vcpu, &dt);
  1461. sregs->gdt.limit = dt.limit;
  1462. sregs->gdt.base = dt.base;
  1463. kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
  1464. sregs->cr0 = vcpu->cr0;
  1465. sregs->cr2 = vcpu->cr2;
  1466. sregs->cr3 = vcpu->cr3;
  1467. sregs->cr4 = vcpu->cr4;
  1468. sregs->cr8 = vcpu->cr8;
  1469. sregs->efer = vcpu->shadow_efer;
  1470. sregs->apic_base = vcpu->apic_base;
  1471. memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
  1472. sizeof sregs->interrupt_bitmap);
  1473. vcpu_put(vcpu);
  1474. return 0;
  1475. }
  1476. static void set_segment(struct kvm_vcpu *vcpu,
  1477. struct kvm_segment *var, int seg)
  1478. {
  1479. return kvm_arch_ops->set_segment(vcpu, var, seg);
  1480. }
  1481. static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  1482. struct kvm_sregs *sregs)
  1483. {
  1484. int mmu_reset_needed = 0;
  1485. int i;
  1486. struct descriptor_table dt;
  1487. vcpu_load(vcpu);
  1488. set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  1489. set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  1490. set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  1491. set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  1492. set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  1493. set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  1494. set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  1495. set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  1496. dt.limit = sregs->idt.limit;
  1497. dt.base = sregs->idt.base;
  1498. kvm_arch_ops->set_idt(vcpu, &dt);
  1499. dt.limit = sregs->gdt.limit;
  1500. dt.base = sregs->gdt.base;
  1501. kvm_arch_ops->set_gdt(vcpu, &dt);
  1502. vcpu->cr2 = sregs->cr2;
  1503. mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
  1504. vcpu->cr3 = sregs->cr3;
  1505. vcpu->cr8 = sregs->cr8;
  1506. mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
  1507. #ifdef CONFIG_X86_64
  1508. kvm_arch_ops->set_efer(vcpu, sregs->efer);
  1509. #endif
  1510. vcpu->apic_base = sregs->apic_base;
  1511. kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
  1512. mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
  1513. kvm_arch_ops->set_cr0_no_modeswitch(vcpu, sregs->cr0);
  1514. mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
  1515. kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
  1516. if (!is_long_mode(vcpu) && is_pae(vcpu))
  1517. load_pdptrs(vcpu, vcpu->cr3);
  1518. if (mmu_reset_needed)
  1519. kvm_mmu_reset_context(vcpu);
  1520. memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
  1521. sizeof vcpu->irq_pending);
  1522. vcpu->irq_summary = 0;
  1523. for (i = 0; i < NR_IRQ_WORDS; ++i)
  1524. if (vcpu->irq_pending[i])
  1525. __set_bit(i, &vcpu->irq_summary);
  1526. vcpu_put(vcpu);
  1527. return 0;
  1528. }
  1529. /*
  1530. * List of msr numbers which we expose to userspace through KVM_GET_MSRS
  1531. * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
  1532. *
  1533. * This list is modified at module load time to reflect the
  1534. * capabilities of the host cpu.
  1535. */
  1536. static u32 msrs_to_save[] = {
  1537. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  1538. MSR_K6_STAR,
  1539. #ifdef CONFIG_X86_64
  1540. MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
  1541. #endif
  1542. MSR_IA32_TIME_STAMP_COUNTER,
  1543. };
  1544. static unsigned num_msrs_to_save;
  1545. static u32 emulated_msrs[] = {
  1546. MSR_IA32_MISC_ENABLE,
  1547. };
  1548. static __init void kvm_init_msr_list(void)
  1549. {
  1550. u32 dummy[2];
  1551. unsigned i, j;
  1552. for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
  1553. if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
  1554. continue;
  1555. if (j < i)
  1556. msrs_to_save[j] = msrs_to_save[i];
  1557. j++;
  1558. }
  1559. num_msrs_to_save = j;
  1560. }
  1561. /*
  1562. * Adapt set_msr() to msr_io()'s calling convention
  1563. */
  1564. static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
  1565. {
  1566. return set_msr(vcpu, index, *data);
  1567. }
  1568. /*
  1569. * Read or write a bunch of msrs. All parameters are kernel addresses.
  1570. *
  1571. * @return number of msrs set successfully.
  1572. */
  1573. static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
  1574. struct kvm_msr_entry *entries,
  1575. int (*do_msr)(struct kvm_vcpu *vcpu,
  1576. unsigned index, u64 *data))
  1577. {
  1578. int i;
  1579. vcpu_load(vcpu);
  1580. for (i = 0; i < msrs->nmsrs; ++i)
  1581. if (do_msr(vcpu, entries[i].index, &entries[i].data))
  1582. break;
  1583. vcpu_put(vcpu);
  1584. return i;
  1585. }
  1586. /*
  1587. * Read or write a bunch of msrs. Parameters are user addresses.
  1588. *
  1589. * @return number of msrs set successfully.
  1590. */
  1591. static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
  1592. int (*do_msr)(struct kvm_vcpu *vcpu,
  1593. unsigned index, u64 *data),
  1594. int writeback)
  1595. {
  1596. struct kvm_msrs msrs;
  1597. struct kvm_msr_entry *entries;
  1598. int r, n;
  1599. unsigned size;
  1600. r = -EFAULT;
  1601. if (copy_from_user(&msrs, user_msrs, sizeof msrs))
  1602. goto out;
  1603. r = -E2BIG;
  1604. if (msrs.nmsrs >= MAX_IO_MSRS)
  1605. goto out;
  1606. r = -ENOMEM;
  1607. size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
  1608. entries = vmalloc(size);
  1609. if (!entries)
  1610. goto out;
  1611. r = -EFAULT;
  1612. if (copy_from_user(entries, user_msrs->entries, size))
  1613. goto out_free;
  1614. r = n = __msr_io(vcpu, &msrs, entries, do_msr);
  1615. if (r < 0)
  1616. goto out_free;
  1617. r = -EFAULT;
  1618. if (writeback && copy_to_user(user_msrs->entries, entries, size))
  1619. goto out_free;
  1620. r = n;
  1621. out_free:
  1622. vfree(entries);
  1623. out:
  1624. return r;
  1625. }
  1626. /*
  1627. * Translate a guest virtual address to a guest physical address.
  1628. */
  1629. static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  1630. struct kvm_translation *tr)
  1631. {
  1632. unsigned long vaddr = tr->linear_address;
  1633. gpa_t gpa;
  1634. vcpu_load(vcpu);
  1635. spin_lock(&vcpu->kvm->lock);
  1636. gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
  1637. tr->physical_address = gpa;
  1638. tr->valid = gpa != UNMAPPED_GVA;
  1639. tr->writeable = 1;
  1640. tr->usermode = 0;
  1641. spin_unlock(&vcpu->kvm->lock);
  1642. vcpu_put(vcpu);
  1643. return 0;
  1644. }
  1645. static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
  1646. struct kvm_interrupt *irq)
  1647. {
  1648. if (irq->irq < 0 || irq->irq >= 256)
  1649. return -EINVAL;
  1650. vcpu_load(vcpu);
  1651. set_bit(irq->irq, vcpu->irq_pending);
  1652. set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
  1653. vcpu_put(vcpu);
  1654. return 0;
  1655. }
  1656. static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
  1657. struct kvm_debug_guest *dbg)
  1658. {
  1659. int r;
  1660. vcpu_load(vcpu);
  1661. r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
  1662. vcpu_put(vcpu);
  1663. return r;
  1664. }
  1665. static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
  1666. unsigned long address,
  1667. int *type)
  1668. {
  1669. struct kvm_vcpu *vcpu = vma->vm_file->private_data;
  1670. unsigned long pgoff;
  1671. struct page *page;
  1672. *type = VM_FAULT_MINOR;
  1673. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  1674. if (pgoff != 0)
  1675. return NOPAGE_SIGBUS;
  1676. page = virt_to_page(vcpu->run);
  1677. get_page(page);
  1678. return page;
  1679. }
  1680. static struct vm_operations_struct kvm_vcpu_vm_ops = {
  1681. .nopage = kvm_vcpu_nopage,
  1682. };
  1683. static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
  1684. {
  1685. vma->vm_ops = &kvm_vcpu_vm_ops;
  1686. return 0;
  1687. }
  1688. static int kvm_vcpu_release(struct inode *inode, struct file *filp)
  1689. {
  1690. struct kvm_vcpu *vcpu = filp->private_data;
  1691. fput(vcpu->kvm->filp);
  1692. return 0;
  1693. }
  1694. static struct file_operations kvm_vcpu_fops = {
  1695. .release = kvm_vcpu_release,
  1696. .unlocked_ioctl = kvm_vcpu_ioctl,
  1697. .compat_ioctl = kvm_vcpu_ioctl,
  1698. .mmap = kvm_vcpu_mmap,
  1699. };
  1700. /*
  1701. * Allocates an inode for the vcpu.
  1702. */
  1703. static int create_vcpu_fd(struct kvm_vcpu *vcpu)
  1704. {
  1705. int fd, r;
  1706. struct inode *inode;
  1707. struct file *file;
  1708. atomic_inc(&vcpu->kvm->filp->f_count);
  1709. inode = kvmfs_inode(&kvm_vcpu_fops);
  1710. if (IS_ERR(inode)) {
  1711. r = PTR_ERR(inode);
  1712. goto out1;
  1713. }
  1714. file = kvmfs_file(inode, vcpu);
  1715. if (IS_ERR(file)) {
  1716. r = PTR_ERR(file);
  1717. goto out2;
  1718. }
  1719. r = get_unused_fd();
  1720. if (r < 0)
  1721. goto out3;
  1722. fd = r;
  1723. fd_install(fd, file);
  1724. return fd;
  1725. out3:
  1726. fput(file);
  1727. out2:
  1728. iput(inode);
  1729. out1:
  1730. fput(vcpu->kvm->filp);
  1731. return r;
  1732. }
  1733. /*
  1734. * Creates some virtual cpus. Good luck creating more than one.
  1735. */
  1736. static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
  1737. {
  1738. int r;
  1739. struct kvm_vcpu *vcpu;
  1740. struct page *page;
  1741. r = -EINVAL;
  1742. if (!valid_vcpu(n))
  1743. goto out;
  1744. vcpu = &kvm->vcpus[n];
  1745. mutex_lock(&vcpu->mutex);
  1746. if (vcpu->vmcs) {
  1747. mutex_unlock(&vcpu->mutex);
  1748. return -EEXIST;
  1749. }
  1750. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  1751. r = -ENOMEM;
  1752. if (!page)
  1753. goto out_unlock;
  1754. vcpu->run = page_address(page);
  1755. vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
  1756. FX_IMAGE_ALIGN);
  1757. vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
  1758. r = kvm_arch_ops->vcpu_create(vcpu);
  1759. if (r < 0)
  1760. goto out_free_vcpus;
  1761. r = kvm_mmu_create(vcpu);
  1762. if (r < 0)
  1763. goto out_free_vcpus;
  1764. kvm_arch_ops->vcpu_load(vcpu);
  1765. r = kvm_mmu_setup(vcpu);
  1766. if (r >= 0)
  1767. r = kvm_arch_ops->vcpu_setup(vcpu);
  1768. vcpu_put(vcpu);
  1769. if (r < 0)
  1770. goto out_free_vcpus;
  1771. r = create_vcpu_fd(vcpu);
  1772. if (r < 0)
  1773. goto out_free_vcpus;
  1774. return r;
  1775. out_free_vcpus:
  1776. kvm_free_vcpu(vcpu);
  1777. out_unlock:
  1778. mutex_unlock(&vcpu->mutex);
  1779. out:
  1780. return r;
  1781. }
  1782. static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
  1783. struct kvm_cpuid *cpuid,
  1784. struct kvm_cpuid_entry __user *entries)
  1785. {
  1786. int r;
  1787. r = -E2BIG;
  1788. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  1789. goto out;
  1790. r = -EFAULT;
  1791. if (copy_from_user(&vcpu->cpuid_entries, entries,
  1792. cpuid->nent * sizeof(struct kvm_cpuid_entry)))
  1793. goto out;
  1794. vcpu->cpuid_nent = cpuid->nent;
  1795. return 0;
  1796. out:
  1797. return r;
  1798. }
  1799. static long kvm_vcpu_ioctl(struct file *filp,
  1800. unsigned int ioctl, unsigned long arg)
  1801. {
  1802. struct kvm_vcpu *vcpu = filp->private_data;
  1803. void __user *argp = (void __user *)arg;
  1804. int r = -EINVAL;
  1805. switch (ioctl) {
  1806. case KVM_RUN:
  1807. r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
  1808. break;
  1809. case KVM_GET_REGS: {
  1810. struct kvm_regs kvm_regs;
  1811. memset(&kvm_regs, 0, sizeof kvm_regs);
  1812. r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
  1813. if (r)
  1814. goto out;
  1815. r = -EFAULT;
  1816. if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
  1817. goto out;
  1818. r = 0;
  1819. break;
  1820. }
  1821. case KVM_SET_REGS: {
  1822. struct kvm_regs kvm_regs;
  1823. r = -EFAULT;
  1824. if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
  1825. goto out;
  1826. r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
  1827. if (r)
  1828. goto out;
  1829. r = 0;
  1830. break;
  1831. }
  1832. case KVM_GET_SREGS: {
  1833. struct kvm_sregs kvm_sregs;
  1834. memset(&kvm_sregs, 0, sizeof kvm_sregs);
  1835. r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
  1836. if (r)
  1837. goto out;
  1838. r = -EFAULT;
  1839. if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
  1840. goto out;
  1841. r = 0;
  1842. break;
  1843. }
  1844. case KVM_SET_SREGS: {
  1845. struct kvm_sregs kvm_sregs;
  1846. r = -EFAULT;
  1847. if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
  1848. goto out;
  1849. r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
  1850. if (r)
  1851. goto out;
  1852. r = 0;
  1853. break;
  1854. }
  1855. case KVM_TRANSLATE: {
  1856. struct kvm_translation tr;
  1857. r = -EFAULT;
  1858. if (copy_from_user(&tr, argp, sizeof tr))
  1859. goto out;
  1860. r = kvm_vcpu_ioctl_translate(vcpu, &tr);
  1861. if (r)
  1862. goto out;
  1863. r = -EFAULT;
  1864. if (copy_to_user(argp, &tr, sizeof tr))
  1865. goto out;
  1866. r = 0;
  1867. break;
  1868. }
  1869. case KVM_INTERRUPT: {
  1870. struct kvm_interrupt irq;
  1871. r = -EFAULT;
  1872. if (copy_from_user(&irq, argp, sizeof irq))
  1873. goto out;
  1874. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  1875. if (r)
  1876. goto out;
  1877. r = 0;
  1878. break;
  1879. }
  1880. case KVM_DEBUG_GUEST: {
  1881. struct kvm_debug_guest dbg;
  1882. r = -EFAULT;
  1883. if (copy_from_user(&dbg, argp, sizeof dbg))
  1884. goto out;
  1885. r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
  1886. if (r)
  1887. goto out;
  1888. r = 0;
  1889. break;
  1890. }
  1891. case KVM_GET_MSRS:
  1892. r = msr_io(vcpu, argp, get_msr, 1);
  1893. break;
  1894. case KVM_SET_MSRS:
  1895. r = msr_io(vcpu, argp, do_set_msr, 0);
  1896. break;
  1897. case KVM_SET_CPUID: {
  1898. struct kvm_cpuid __user *cpuid_arg = argp;
  1899. struct kvm_cpuid cpuid;
  1900. r = -EFAULT;
  1901. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1902. goto out;
  1903. r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
  1904. if (r)
  1905. goto out;
  1906. break;
  1907. }
  1908. default:
  1909. ;
  1910. }
  1911. out:
  1912. return r;
  1913. }
  1914. static long kvm_vm_ioctl(struct file *filp,
  1915. unsigned int ioctl, unsigned long arg)
  1916. {
  1917. struct kvm *kvm = filp->private_data;
  1918. void __user *argp = (void __user *)arg;
  1919. int r = -EINVAL;
  1920. switch (ioctl) {
  1921. case KVM_CREATE_VCPU:
  1922. r = kvm_vm_ioctl_create_vcpu(kvm, arg);
  1923. if (r < 0)
  1924. goto out;
  1925. break;
  1926. case KVM_SET_MEMORY_REGION: {
  1927. struct kvm_memory_region kvm_mem;
  1928. r = -EFAULT;
  1929. if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
  1930. goto out;
  1931. r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
  1932. if (r)
  1933. goto out;
  1934. break;
  1935. }
  1936. case KVM_GET_DIRTY_LOG: {
  1937. struct kvm_dirty_log log;
  1938. r = -EFAULT;
  1939. if (copy_from_user(&log, argp, sizeof log))
  1940. goto out;
  1941. r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
  1942. if (r)
  1943. goto out;
  1944. break;
  1945. }
  1946. default:
  1947. ;
  1948. }
  1949. out:
  1950. return r;
  1951. }
  1952. static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
  1953. unsigned long address,
  1954. int *type)
  1955. {
  1956. struct kvm *kvm = vma->vm_file->private_data;
  1957. unsigned long pgoff;
  1958. struct kvm_memory_slot *slot;
  1959. struct page *page;
  1960. *type = VM_FAULT_MINOR;
  1961. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  1962. slot = gfn_to_memslot(kvm, pgoff);
  1963. if (!slot)
  1964. return NOPAGE_SIGBUS;
  1965. page = gfn_to_page(slot, pgoff);
  1966. if (!page)
  1967. return NOPAGE_SIGBUS;
  1968. get_page(page);
  1969. return page;
  1970. }
  1971. static struct vm_operations_struct kvm_vm_vm_ops = {
  1972. .nopage = kvm_vm_nopage,
  1973. };
  1974. static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
  1975. {
  1976. vma->vm_ops = &kvm_vm_vm_ops;
  1977. return 0;
  1978. }
  1979. static struct file_operations kvm_vm_fops = {
  1980. .release = kvm_vm_release,
  1981. .unlocked_ioctl = kvm_vm_ioctl,
  1982. .compat_ioctl = kvm_vm_ioctl,
  1983. .mmap = kvm_vm_mmap,
  1984. };
  1985. static int kvm_dev_ioctl_create_vm(void)
  1986. {
  1987. int fd, r;
  1988. struct inode *inode;
  1989. struct file *file;
  1990. struct kvm *kvm;
  1991. inode = kvmfs_inode(&kvm_vm_fops);
  1992. if (IS_ERR(inode)) {
  1993. r = PTR_ERR(inode);
  1994. goto out1;
  1995. }
  1996. kvm = kvm_create_vm();
  1997. if (IS_ERR(kvm)) {
  1998. r = PTR_ERR(kvm);
  1999. goto out2;
  2000. }
  2001. file = kvmfs_file(inode, kvm);
  2002. if (IS_ERR(file)) {
  2003. r = PTR_ERR(file);
  2004. goto out3;
  2005. }
  2006. kvm->filp = file;
  2007. r = get_unused_fd();
  2008. if (r < 0)
  2009. goto out4;
  2010. fd = r;
  2011. fd_install(fd, file);
  2012. return fd;
  2013. out4:
  2014. fput(file);
  2015. out3:
  2016. kvm_destroy_vm(kvm);
  2017. out2:
  2018. iput(inode);
  2019. out1:
  2020. return r;
  2021. }
  2022. static long kvm_dev_ioctl(struct file *filp,
  2023. unsigned int ioctl, unsigned long arg)
  2024. {
  2025. void __user *argp = (void __user *)arg;
  2026. int r = -EINVAL;
  2027. switch (ioctl) {
  2028. case KVM_GET_API_VERSION:
  2029. r = KVM_API_VERSION;
  2030. break;
  2031. case KVM_CREATE_VM:
  2032. r = kvm_dev_ioctl_create_vm();
  2033. break;
  2034. case KVM_GET_MSR_INDEX_LIST: {
  2035. struct kvm_msr_list __user *user_msr_list = argp;
  2036. struct kvm_msr_list msr_list;
  2037. unsigned n;
  2038. r = -EFAULT;
  2039. if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
  2040. goto out;
  2041. n = msr_list.nmsrs;
  2042. msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
  2043. if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
  2044. goto out;
  2045. r = -E2BIG;
  2046. if (n < num_msrs_to_save)
  2047. goto out;
  2048. r = -EFAULT;
  2049. if (copy_to_user(user_msr_list->indices, &msrs_to_save,
  2050. num_msrs_to_save * sizeof(u32)))
  2051. goto out;
  2052. if (copy_to_user(user_msr_list->indices
  2053. + num_msrs_to_save * sizeof(u32),
  2054. &emulated_msrs,
  2055. ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
  2056. goto out;
  2057. r = 0;
  2058. break;
  2059. }
  2060. case KVM_CHECK_EXTENSION:
  2061. /*
  2062. * No extensions defined at present.
  2063. */
  2064. r = 0;
  2065. break;
  2066. default:
  2067. ;
  2068. }
  2069. out:
  2070. return r;
  2071. }
  2072. static struct file_operations kvm_chardev_ops = {
  2073. .open = kvm_dev_open,
  2074. .release = kvm_dev_release,
  2075. .unlocked_ioctl = kvm_dev_ioctl,
  2076. .compat_ioctl = kvm_dev_ioctl,
  2077. };
  2078. static struct miscdevice kvm_dev = {
  2079. KVM_MINOR,
  2080. "kvm",
  2081. &kvm_chardev_ops,
  2082. };
  2083. static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
  2084. void *v)
  2085. {
  2086. if (val == SYS_RESTART) {
  2087. /*
  2088. * Some (well, at least mine) BIOSes hang on reboot if
  2089. * in vmx root mode.
  2090. */
  2091. printk(KERN_INFO "kvm: exiting hardware virtualization\n");
  2092. on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
  2093. }
  2094. return NOTIFY_OK;
  2095. }
  2096. static struct notifier_block kvm_reboot_notifier = {
  2097. .notifier_call = kvm_reboot,
  2098. .priority = 0,
  2099. };
  2100. /*
  2101. * Make sure that a cpu that is being hot-unplugged does not have any vcpus
  2102. * cached on it.
  2103. */
  2104. static void decache_vcpus_on_cpu(int cpu)
  2105. {
  2106. struct kvm *vm;
  2107. struct kvm_vcpu *vcpu;
  2108. int i;
  2109. spin_lock(&kvm_lock);
  2110. list_for_each_entry(vm, &vm_list, vm_list)
  2111. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  2112. vcpu = &vm->vcpus[i];
  2113. /*
  2114. * If the vcpu is locked, then it is running on some
  2115. * other cpu and therefore it is not cached on the
  2116. * cpu in question.
  2117. *
  2118. * If it's not locked, check the last cpu it executed
  2119. * on.
  2120. */
  2121. if (mutex_trylock(&vcpu->mutex)) {
  2122. if (vcpu->cpu == cpu) {
  2123. kvm_arch_ops->vcpu_decache(vcpu);
  2124. vcpu->cpu = -1;
  2125. }
  2126. mutex_unlock(&vcpu->mutex);
  2127. }
  2128. }
  2129. spin_unlock(&kvm_lock);
  2130. }
  2131. static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
  2132. void *v)
  2133. {
  2134. int cpu = (long)v;
  2135. switch (val) {
  2136. case CPU_DOWN_PREPARE:
  2137. case CPU_UP_CANCELED:
  2138. printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
  2139. cpu);
  2140. decache_vcpus_on_cpu(cpu);
  2141. smp_call_function_single(cpu, kvm_arch_ops->hardware_disable,
  2142. NULL, 0, 1);
  2143. break;
  2144. case CPU_ONLINE:
  2145. printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
  2146. cpu);
  2147. smp_call_function_single(cpu, kvm_arch_ops->hardware_enable,
  2148. NULL, 0, 1);
  2149. break;
  2150. }
  2151. return NOTIFY_OK;
  2152. }
  2153. static struct notifier_block kvm_cpu_notifier = {
  2154. .notifier_call = kvm_cpu_hotplug,
  2155. .priority = 20, /* must be > scheduler priority */
  2156. };
  2157. static __init void kvm_init_debug(void)
  2158. {
  2159. struct kvm_stats_debugfs_item *p;
  2160. debugfs_dir = debugfs_create_dir("kvm", NULL);
  2161. for (p = debugfs_entries; p->name; ++p)
  2162. p->dentry = debugfs_create_u32(p->name, 0444, debugfs_dir,
  2163. p->data);
  2164. }
  2165. static void kvm_exit_debug(void)
  2166. {
  2167. struct kvm_stats_debugfs_item *p;
  2168. for (p = debugfs_entries; p->name; ++p)
  2169. debugfs_remove(p->dentry);
  2170. debugfs_remove(debugfs_dir);
  2171. }
  2172. static int kvm_suspend(struct sys_device *dev, pm_message_t state)
  2173. {
  2174. decache_vcpus_on_cpu(raw_smp_processor_id());
  2175. on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
  2176. return 0;
  2177. }
  2178. static int kvm_resume(struct sys_device *dev)
  2179. {
  2180. on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
  2181. return 0;
  2182. }
  2183. static struct sysdev_class kvm_sysdev_class = {
  2184. set_kset_name("kvm"),
  2185. .suspend = kvm_suspend,
  2186. .resume = kvm_resume,
  2187. };
  2188. static struct sys_device kvm_sysdev = {
  2189. .id = 0,
  2190. .cls = &kvm_sysdev_class,
  2191. };
  2192. hpa_t bad_page_address;
  2193. static int kvmfs_get_sb(struct file_system_type *fs_type, int flags,
  2194. const char *dev_name, void *data, struct vfsmount *mnt)
  2195. {
  2196. return get_sb_pseudo(fs_type, "kvm:", NULL, KVMFS_SUPER_MAGIC, mnt);
  2197. }
  2198. static struct file_system_type kvm_fs_type = {
  2199. .name = "kvmfs",
  2200. .get_sb = kvmfs_get_sb,
  2201. .kill_sb = kill_anon_super,
  2202. };
  2203. int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
  2204. {
  2205. int r;
  2206. if (kvm_arch_ops) {
  2207. printk(KERN_ERR "kvm: already loaded the other module\n");
  2208. return -EEXIST;
  2209. }
  2210. if (!ops->cpu_has_kvm_support()) {
  2211. printk(KERN_ERR "kvm: no hardware support\n");
  2212. return -EOPNOTSUPP;
  2213. }
  2214. if (ops->disabled_by_bios()) {
  2215. printk(KERN_ERR "kvm: disabled by bios\n");
  2216. return -EOPNOTSUPP;
  2217. }
  2218. kvm_arch_ops = ops;
  2219. r = kvm_arch_ops->hardware_setup();
  2220. if (r < 0)
  2221. goto out;
  2222. on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
  2223. r = register_cpu_notifier(&kvm_cpu_notifier);
  2224. if (r)
  2225. goto out_free_1;
  2226. register_reboot_notifier(&kvm_reboot_notifier);
  2227. r = sysdev_class_register(&kvm_sysdev_class);
  2228. if (r)
  2229. goto out_free_2;
  2230. r = sysdev_register(&kvm_sysdev);
  2231. if (r)
  2232. goto out_free_3;
  2233. kvm_chardev_ops.owner = module;
  2234. r = misc_register(&kvm_dev);
  2235. if (r) {
  2236. printk (KERN_ERR "kvm: misc device register failed\n");
  2237. goto out_free;
  2238. }
  2239. return r;
  2240. out_free:
  2241. sysdev_unregister(&kvm_sysdev);
  2242. out_free_3:
  2243. sysdev_class_unregister(&kvm_sysdev_class);
  2244. out_free_2:
  2245. unregister_reboot_notifier(&kvm_reboot_notifier);
  2246. unregister_cpu_notifier(&kvm_cpu_notifier);
  2247. out_free_1:
  2248. on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
  2249. kvm_arch_ops->hardware_unsetup();
  2250. out:
  2251. kvm_arch_ops = NULL;
  2252. return r;
  2253. }
  2254. void kvm_exit_arch(void)
  2255. {
  2256. misc_deregister(&kvm_dev);
  2257. sysdev_unregister(&kvm_sysdev);
  2258. sysdev_class_unregister(&kvm_sysdev_class);
  2259. unregister_reboot_notifier(&kvm_reboot_notifier);
  2260. unregister_cpu_notifier(&kvm_cpu_notifier);
  2261. on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
  2262. kvm_arch_ops->hardware_unsetup();
  2263. kvm_arch_ops = NULL;
  2264. }
  2265. static __init int kvm_init(void)
  2266. {
  2267. static struct page *bad_page;
  2268. int r;
  2269. r = register_filesystem(&kvm_fs_type);
  2270. if (r)
  2271. goto out3;
  2272. kvmfs_mnt = kern_mount(&kvm_fs_type);
  2273. r = PTR_ERR(kvmfs_mnt);
  2274. if (IS_ERR(kvmfs_mnt))
  2275. goto out2;
  2276. kvm_init_debug();
  2277. kvm_init_msr_list();
  2278. if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
  2279. r = -ENOMEM;
  2280. goto out;
  2281. }
  2282. bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
  2283. memset(__va(bad_page_address), 0, PAGE_SIZE);
  2284. return 0;
  2285. out:
  2286. kvm_exit_debug();
  2287. mntput(kvmfs_mnt);
  2288. out2:
  2289. unregister_filesystem(&kvm_fs_type);
  2290. out3:
  2291. return r;
  2292. }
  2293. static __exit void kvm_exit(void)
  2294. {
  2295. kvm_exit_debug();
  2296. __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
  2297. mntput(kvmfs_mnt);
  2298. unregister_filesystem(&kvm_fs_type);
  2299. }
  2300. module_init(kvm_init)
  2301. module_exit(kvm_exit)
  2302. EXPORT_SYMBOL_GPL(kvm_init_arch);
  2303. EXPORT_SYMBOL_GPL(kvm_exit_arch);