kvm_main.c 68 KB

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