kvm_main.c 68 KB

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