kvm_main.c 69 KB

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