kvm_main.c 72 KB

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