kvm_main.c 70 KB

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