kvm_main.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409
  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 "iodev.h"
  18. #include <linux/kvm_host.h>
  19. #include <linux/kvm.h>
  20. #include <linux/module.h>
  21. #include <linux/errno.h>
  22. #include <linux/percpu.h>
  23. #include <linux/gfp.h>
  24. #include <linux/mm.h>
  25. #include <linux/miscdevice.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/reboot.h>
  28. #include <linux/debugfs.h>
  29. #include <linux/highmem.h>
  30. #include <linux/file.h>
  31. #include <linux/sysdev.h>
  32. #include <linux/cpu.h>
  33. #include <linux/sched.h>
  34. #include <linux/cpumask.h>
  35. #include <linux/smp.h>
  36. #include <linux/anon_inodes.h>
  37. #include <linux/profile.h>
  38. #include <linux/kvm_para.h>
  39. #include <linux/pagemap.h>
  40. #include <linux/mman.h>
  41. #include <linux/swap.h>
  42. #include <asm/processor.h>
  43. #include <asm/io.h>
  44. #include <asm/uaccess.h>
  45. #include <asm/pgtable.h>
  46. #ifdef CONFIG_X86
  47. #include <asm/msidef.h>
  48. #endif
  49. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  50. #include "coalesced_mmio.h"
  51. #endif
  52. #ifdef KVM_CAP_DEVICE_ASSIGNMENT
  53. #include <linux/pci.h>
  54. #include <linux/interrupt.h>
  55. #include "irq.h"
  56. #endif
  57. MODULE_AUTHOR("Qumranet");
  58. MODULE_LICENSE("GPL");
  59. static int msi2intx = 1;
  60. module_param(msi2intx, bool, 0);
  61. DEFINE_SPINLOCK(kvm_lock);
  62. LIST_HEAD(vm_list);
  63. static cpumask_var_t cpus_hardware_enabled;
  64. struct kmem_cache *kvm_vcpu_cache;
  65. EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
  66. static __read_mostly struct preempt_ops kvm_preempt_ops;
  67. struct dentry *kvm_debugfs_dir;
  68. static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
  69. unsigned long arg);
  70. static bool kvm_rebooting;
  71. #ifdef KVM_CAP_DEVICE_ASSIGNMENT
  72. #ifdef CONFIG_X86
  73. static void assigned_device_msi_dispatch(struct kvm_assigned_dev_kernel *dev)
  74. {
  75. int vcpu_id;
  76. struct kvm_vcpu *vcpu;
  77. struct kvm_ioapic *ioapic = ioapic_irqchip(dev->kvm);
  78. int dest_id = (dev->guest_msi.address_lo & MSI_ADDR_DEST_ID_MASK)
  79. >> MSI_ADDR_DEST_ID_SHIFT;
  80. int vector = (dev->guest_msi.data & MSI_DATA_VECTOR_MASK)
  81. >> MSI_DATA_VECTOR_SHIFT;
  82. int dest_mode = test_bit(MSI_ADDR_DEST_MODE_SHIFT,
  83. (unsigned long *)&dev->guest_msi.address_lo);
  84. int trig_mode = test_bit(MSI_DATA_TRIGGER_SHIFT,
  85. (unsigned long *)&dev->guest_msi.data);
  86. int delivery_mode = test_bit(MSI_DATA_DELIVERY_MODE_SHIFT,
  87. (unsigned long *)&dev->guest_msi.data);
  88. u32 deliver_bitmask;
  89. BUG_ON(!ioapic);
  90. deliver_bitmask = kvm_ioapic_get_delivery_bitmask(ioapic,
  91. dest_id, dest_mode);
  92. /* IOAPIC delivery mode value is the same as MSI here */
  93. switch (delivery_mode) {
  94. case IOAPIC_LOWEST_PRIORITY:
  95. vcpu = kvm_get_lowest_prio_vcpu(ioapic->kvm, vector,
  96. deliver_bitmask);
  97. if (vcpu != NULL)
  98. kvm_apic_set_irq(vcpu, vector, trig_mode);
  99. else
  100. printk(KERN_INFO "kvm: null lowest priority vcpu!\n");
  101. break;
  102. case IOAPIC_FIXED:
  103. for (vcpu_id = 0; deliver_bitmask != 0; vcpu_id++) {
  104. if (!(deliver_bitmask & (1 << vcpu_id)))
  105. continue;
  106. deliver_bitmask &= ~(1 << vcpu_id);
  107. vcpu = ioapic->kvm->vcpus[vcpu_id];
  108. if (vcpu)
  109. kvm_apic_set_irq(vcpu, vector, trig_mode);
  110. }
  111. break;
  112. default:
  113. printk(KERN_INFO "kvm: unsupported MSI delivery mode\n");
  114. }
  115. }
  116. #else
  117. static void assigned_device_msi_dispatch(struct kvm_assigned_dev_kernel *dev) {}
  118. #endif
  119. static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
  120. int assigned_dev_id)
  121. {
  122. struct list_head *ptr;
  123. struct kvm_assigned_dev_kernel *match;
  124. list_for_each(ptr, head) {
  125. match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
  126. if (match->assigned_dev_id == assigned_dev_id)
  127. return match;
  128. }
  129. return NULL;
  130. }
  131. static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
  132. {
  133. struct kvm_assigned_dev_kernel *assigned_dev;
  134. assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
  135. interrupt_work);
  136. /* This is taken to safely inject irq inside the guest. When
  137. * the interrupt injection (or the ioapic code) uses a
  138. * finer-grained lock, update this
  139. */
  140. mutex_lock(&assigned_dev->kvm->lock);
  141. if (assigned_dev->irq_requested_type & KVM_ASSIGNED_DEV_GUEST_INTX)
  142. kvm_set_irq(assigned_dev->kvm,
  143. assigned_dev->irq_source_id,
  144. assigned_dev->guest_irq, 1);
  145. else if (assigned_dev->irq_requested_type &
  146. KVM_ASSIGNED_DEV_GUEST_MSI) {
  147. assigned_device_msi_dispatch(assigned_dev);
  148. enable_irq(assigned_dev->host_irq);
  149. assigned_dev->host_irq_disabled = false;
  150. }
  151. mutex_unlock(&assigned_dev->kvm->lock);
  152. }
  153. static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
  154. {
  155. struct kvm_assigned_dev_kernel *assigned_dev =
  156. (struct kvm_assigned_dev_kernel *) dev_id;
  157. schedule_work(&assigned_dev->interrupt_work);
  158. disable_irq_nosync(irq);
  159. assigned_dev->host_irq_disabled = true;
  160. return IRQ_HANDLED;
  161. }
  162. /* Ack the irq line for an assigned device */
  163. static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
  164. {
  165. struct kvm_assigned_dev_kernel *dev;
  166. if (kian->gsi == -1)
  167. return;
  168. dev = container_of(kian, struct kvm_assigned_dev_kernel,
  169. ack_notifier);
  170. kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
  171. /* The guest irq may be shared so this ack may be
  172. * from another device.
  173. */
  174. if (dev->host_irq_disabled) {
  175. enable_irq(dev->host_irq);
  176. dev->host_irq_disabled = false;
  177. }
  178. }
  179. /* The function implicit hold kvm->lock mutex due to cancel_work_sync() */
  180. static void kvm_free_assigned_irq(struct kvm *kvm,
  181. struct kvm_assigned_dev_kernel *assigned_dev)
  182. {
  183. if (!irqchip_in_kernel(kvm))
  184. return;
  185. kvm_unregister_irq_ack_notifier(&assigned_dev->ack_notifier);
  186. if (assigned_dev->irq_source_id != -1)
  187. kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
  188. assigned_dev->irq_source_id = -1;
  189. if (!assigned_dev->irq_requested_type)
  190. return;
  191. /*
  192. * In kvm_free_device_irq, cancel_work_sync return true if:
  193. * 1. work is scheduled, and then cancelled.
  194. * 2. work callback is executed.
  195. *
  196. * The first one ensured that the irq is disabled and no more events
  197. * would happen. But for the second one, the irq may be enabled (e.g.
  198. * for MSI). So we disable irq here to prevent further events.
  199. *
  200. * Notice this maybe result in nested disable if the interrupt type is
  201. * INTx, but it's OK for we are going to free it.
  202. *
  203. * If this function is a part of VM destroy, please ensure that till
  204. * now, the kvm state is still legal for probably we also have to wait
  205. * interrupt_work done.
  206. */
  207. disable_irq_nosync(assigned_dev->host_irq);
  208. cancel_work_sync(&assigned_dev->interrupt_work);
  209. free_irq(assigned_dev->host_irq, (void *)assigned_dev);
  210. if (assigned_dev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)
  211. pci_disable_msi(assigned_dev->dev);
  212. assigned_dev->irq_requested_type = 0;
  213. }
  214. static void kvm_free_assigned_device(struct kvm *kvm,
  215. struct kvm_assigned_dev_kernel
  216. *assigned_dev)
  217. {
  218. kvm_free_assigned_irq(kvm, assigned_dev);
  219. pci_reset_function(assigned_dev->dev);
  220. pci_release_regions(assigned_dev->dev);
  221. pci_disable_device(assigned_dev->dev);
  222. pci_dev_put(assigned_dev->dev);
  223. list_del(&assigned_dev->list);
  224. kfree(assigned_dev);
  225. }
  226. void kvm_free_all_assigned_devices(struct kvm *kvm)
  227. {
  228. struct list_head *ptr, *ptr2;
  229. struct kvm_assigned_dev_kernel *assigned_dev;
  230. list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
  231. assigned_dev = list_entry(ptr,
  232. struct kvm_assigned_dev_kernel,
  233. list);
  234. kvm_free_assigned_device(kvm, assigned_dev);
  235. }
  236. }
  237. static int assigned_device_update_intx(struct kvm *kvm,
  238. struct kvm_assigned_dev_kernel *adev,
  239. struct kvm_assigned_irq *airq)
  240. {
  241. adev->guest_irq = airq->guest_irq;
  242. adev->ack_notifier.gsi = airq->guest_irq;
  243. if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_INTX)
  244. return 0;
  245. if (irqchip_in_kernel(kvm)) {
  246. if (!msi2intx &&
  247. (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)) {
  248. free_irq(adev->host_irq, (void *)adev);
  249. pci_disable_msi(adev->dev);
  250. }
  251. if (!capable(CAP_SYS_RAWIO))
  252. return -EPERM;
  253. if (airq->host_irq)
  254. adev->host_irq = airq->host_irq;
  255. else
  256. adev->host_irq = adev->dev->irq;
  257. /* Even though this is PCI, we don't want to use shared
  258. * interrupts. Sharing host devices with guest-assigned devices
  259. * on the same interrupt line is not a happy situation: there
  260. * are going to be long delays in accepting, acking, etc.
  261. */
  262. if (request_irq(adev->host_irq, kvm_assigned_dev_intr,
  263. 0, "kvm_assigned_intx_device", (void *)adev))
  264. return -EIO;
  265. }
  266. adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_INTX |
  267. KVM_ASSIGNED_DEV_HOST_INTX;
  268. return 0;
  269. }
  270. #ifdef CONFIG_X86
  271. static int assigned_device_update_msi(struct kvm *kvm,
  272. struct kvm_assigned_dev_kernel *adev,
  273. struct kvm_assigned_irq *airq)
  274. {
  275. int r;
  276. if (airq->flags & KVM_DEV_IRQ_ASSIGN_ENABLE_MSI) {
  277. /* x86 don't care upper address of guest msi message addr */
  278. adev->irq_requested_type |= KVM_ASSIGNED_DEV_GUEST_MSI;
  279. adev->irq_requested_type &= ~KVM_ASSIGNED_DEV_GUEST_INTX;
  280. adev->guest_msi.address_lo = airq->guest_msi.addr_lo;
  281. adev->guest_msi.data = airq->guest_msi.data;
  282. adev->ack_notifier.gsi = -1;
  283. } else if (msi2intx) {
  284. adev->irq_requested_type |= KVM_ASSIGNED_DEV_GUEST_INTX;
  285. adev->irq_requested_type &= ~KVM_ASSIGNED_DEV_GUEST_MSI;
  286. adev->guest_irq = airq->guest_irq;
  287. adev->ack_notifier.gsi = airq->guest_irq;
  288. }
  289. if (adev->irq_requested_type & KVM_ASSIGNED_DEV_HOST_MSI)
  290. return 0;
  291. if (irqchip_in_kernel(kvm)) {
  292. if (!msi2intx) {
  293. if (adev->irq_requested_type &
  294. KVM_ASSIGNED_DEV_HOST_INTX)
  295. free_irq(adev->host_irq, (void *)adev);
  296. r = pci_enable_msi(adev->dev);
  297. if (r)
  298. return r;
  299. }
  300. adev->host_irq = adev->dev->irq;
  301. if (request_irq(adev->host_irq, kvm_assigned_dev_intr, 0,
  302. "kvm_assigned_msi_device", (void *)adev))
  303. return -EIO;
  304. }
  305. if (!msi2intx)
  306. adev->irq_requested_type = KVM_ASSIGNED_DEV_GUEST_MSI;
  307. adev->irq_requested_type |= KVM_ASSIGNED_DEV_HOST_MSI;
  308. return 0;
  309. }
  310. #endif
  311. static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
  312. struct kvm_assigned_irq
  313. *assigned_irq)
  314. {
  315. int r = 0;
  316. struct kvm_assigned_dev_kernel *match;
  317. mutex_lock(&kvm->lock);
  318. match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  319. assigned_irq->assigned_dev_id);
  320. if (!match) {
  321. mutex_unlock(&kvm->lock);
  322. return -EINVAL;
  323. }
  324. if (!match->irq_requested_type) {
  325. INIT_WORK(&match->interrupt_work,
  326. kvm_assigned_dev_interrupt_work_handler);
  327. if (irqchip_in_kernel(kvm)) {
  328. /* Register ack nofitier */
  329. match->ack_notifier.gsi = -1;
  330. match->ack_notifier.irq_acked =
  331. kvm_assigned_dev_ack_irq;
  332. kvm_register_irq_ack_notifier(kvm,
  333. &match->ack_notifier);
  334. /* Request IRQ source ID */
  335. r = kvm_request_irq_source_id(kvm);
  336. if (r < 0)
  337. goto out_release;
  338. else
  339. match->irq_source_id = r;
  340. #ifdef CONFIG_X86
  341. /* Determine host device irq type, we can know the
  342. * result from dev->msi_enabled */
  343. if (msi2intx)
  344. pci_enable_msi(match->dev);
  345. #endif
  346. }
  347. }
  348. if ((!msi2intx &&
  349. (assigned_irq->flags & KVM_DEV_IRQ_ASSIGN_ENABLE_MSI)) ||
  350. (msi2intx && match->dev->msi_enabled)) {
  351. #ifdef CONFIG_X86
  352. r = assigned_device_update_msi(kvm, match, assigned_irq);
  353. if (r) {
  354. printk(KERN_WARNING "kvm: failed to enable "
  355. "MSI device!\n");
  356. goto out_release;
  357. }
  358. #else
  359. r = -ENOTTY;
  360. #endif
  361. } else if (assigned_irq->host_irq == 0 && match->dev->irq == 0) {
  362. /* Host device IRQ 0 means don't support INTx */
  363. if (!msi2intx) {
  364. printk(KERN_WARNING
  365. "kvm: wait device to enable MSI!\n");
  366. r = 0;
  367. } else {
  368. printk(KERN_WARNING
  369. "kvm: failed to enable MSI device!\n");
  370. r = -ENOTTY;
  371. goto out_release;
  372. }
  373. } else {
  374. /* Non-sharing INTx mode */
  375. r = assigned_device_update_intx(kvm, match, assigned_irq);
  376. if (r) {
  377. printk(KERN_WARNING "kvm: failed to enable "
  378. "INTx device!\n");
  379. goto out_release;
  380. }
  381. }
  382. mutex_unlock(&kvm->lock);
  383. return r;
  384. out_release:
  385. mutex_unlock(&kvm->lock);
  386. kvm_free_assigned_device(kvm, match);
  387. return r;
  388. }
  389. static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
  390. struct kvm_assigned_pci_dev *assigned_dev)
  391. {
  392. int r = 0;
  393. struct kvm_assigned_dev_kernel *match;
  394. struct pci_dev *dev;
  395. down_read(&kvm->slots_lock);
  396. mutex_lock(&kvm->lock);
  397. match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  398. assigned_dev->assigned_dev_id);
  399. if (match) {
  400. /* device already assigned */
  401. r = -EINVAL;
  402. goto out;
  403. }
  404. match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
  405. if (match == NULL) {
  406. printk(KERN_INFO "%s: Couldn't allocate memory\n",
  407. __func__);
  408. r = -ENOMEM;
  409. goto out;
  410. }
  411. dev = pci_get_bus_and_slot(assigned_dev->busnr,
  412. assigned_dev->devfn);
  413. if (!dev) {
  414. printk(KERN_INFO "%s: host device not found\n", __func__);
  415. r = -EINVAL;
  416. goto out_free;
  417. }
  418. if (pci_enable_device(dev)) {
  419. printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
  420. r = -EBUSY;
  421. goto out_put;
  422. }
  423. r = pci_request_regions(dev, "kvm_assigned_device");
  424. if (r) {
  425. printk(KERN_INFO "%s: Could not get access to device regions\n",
  426. __func__);
  427. goto out_disable;
  428. }
  429. pci_reset_function(dev);
  430. match->assigned_dev_id = assigned_dev->assigned_dev_id;
  431. match->host_busnr = assigned_dev->busnr;
  432. match->host_devfn = assigned_dev->devfn;
  433. match->flags = assigned_dev->flags;
  434. match->dev = dev;
  435. match->irq_source_id = -1;
  436. match->kvm = kvm;
  437. list_add(&match->list, &kvm->arch.assigned_dev_head);
  438. if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
  439. if (!kvm->arch.iommu_domain) {
  440. r = kvm_iommu_map_guest(kvm);
  441. if (r)
  442. goto out_list_del;
  443. }
  444. r = kvm_assign_device(kvm, match);
  445. if (r)
  446. goto out_list_del;
  447. }
  448. out:
  449. mutex_unlock(&kvm->lock);
  450. up_read(&kvm->slots_lock);
  451. return r;
  452. out_list_del:
  453. list_del(&match->list);
  454. pci_release_regions(dev);
  455. out_disable:
  456. pci_disable_device(dev);
  457. out_put:
  458. pci_dev_put(dev);
  459. out_free:
  460. kfree(match);
  461. mutex_unlock(&kvm->lock);
  462. up_read(&kvm->slots_lock);
  463. return r;
  464. }
  465. #endif
  466. #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
  467. static int kvm_vm_ioctl_deassign_device(struct kvm *kvm,
  468. struct kvm_assigned_pci_dev *assigned_dev)
  469. {
  470. int r = 0;
  471. struct kvm_assigned_dev_kernel *match;
  472. mutex_lock(&kvm->lock);
  473. match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  474. assigned_dev->assigned_dev_id);
  475. if (!match) {
  476. printk(KERN_INFO "%s: device hasn't been assigned before, "
  477. "so cannot be deassigned\n", __func__);
  478. r = -EINVAL;
  479. goto out;
  480. }
  481. if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU)
  482. kvm_deassign_device(kvm, match);
  483. kvm_free_assigned_device(kvm, match);
  484. out:
  485. mutex_unlock(&kvm->lock);
  486. return r;
  487. }
  488. #endif
  489. static inline int valid_vcpu(int n)
  490. {
  491. return likely(n >= 0 && n < KVM_MAX_VCPUS);
  492. }
  493. inline int kvm_is_mmio_pfn(pfn_t pfn)
  494. {
  495. if (pfn_valid(pfn))
  496. return PageReserved(pfn_to_page(pfn));
  497. return true;
  498. }
  499. /*
  500. * Switches to specified vcpu, until a matching vcpu_put()
  501. */
  502. void vcpu_load(struct kvm_vcpu *vcpu)
  503. {
  504. int cpu;
  505. mutex_lock(&vcpu->mutex);
  506. cpu = get_cpu();
  507. preempt_notifier_register(&vcpu->preempt_notifier);
  508. kvm_arch_vcpu_load(vcpu, cpu);
  509. put_cpu();
  510. }
  511. void vcpu_put(struct kvm_vcpu *vcpu)
  512. {
  513. preempt_disable();
  514. kvm_arch_vcpu_put(vcpu);
  515. preempt_notifier_unregister(&vcpu->preempt_notifier);
  516. preempt_enable();
  517. mutex_unlock(&vcpu->mutex);
  518. }
  519. static void ack_flush(void *_completed)
  520. {
  521. }
  522. static bool make_all_cpus_request(struct kvm *kvm, unsigned int req)
  523. {
  524. int i, cpu, me;
  525. cpumask_var_t cpus;
  526. bool called = true;
  527. struct kvm_vcpu *vcpu;
  528. if (alloc_cpumask_var(&cpus, GFP_ATOMIC))
  529. cpumask_clear(cpus);
  530. me = get_cpu();
  531. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  532. vcpu = kvm->vcpus[i];
  533. if (!vcpu)
  534. continue;
  535. if (test_and_set_bit(req, &vcpu->requests))
  536. continue;
  537. cpu = vcpu->cpu;
  538. if (cpus != NULL && cpu != -1 && cpu != me)
  539. cpumask_set_cpu(cpu, cpus);
  540. }
  541. if (unlikely(cpus == NULL))
  542. smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
  543. else if (!cpumask_empty(cpus))
  544. smp_call_function_many(cpus, ack_flush, NULL, 1);
  545. else
  546. called = false;
  547. put_cpu();
  548. free_cpumask_var(cpus);
  549. return called;
  550. }
  551. void kvm_flush_remote_tlbs(struct kvm *kvm)
  552. {
  553. if (make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
  554. ++kvm->stat.remote_tlb_flush;
  555. }
  556. void kvm_reload_remote_mmus(struct kvm *kvm)
  557. {
  558. make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
  559. }
  560. int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
  561. {
  562. struct page *page;
  563. int r;
  564. mutex_init(&vcpu->mutex);
  565. vcpu->cpu = -1;
  566. vcpu->kvm = kvm;
  567. vcpu->vcpu_id = id;
  568. init_waitqueue_head(&vcpu->wq);
  569. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  570. if (!page) {
  571. r = -ENOMEM;
  572. goto fail;
  573. }
  574. vcpu->run = page_address(page);
  575. r = kvm_arch_vcpu_init(vcpu);
  576. if (r < 0)
  577. goto fail_free_run;
  578. return 0;
  579. fail_free_run:
  580. free_page((unsigned long)vcpu->run);
  581. fail:
  582. return r;
  583. }
  584. EXPORT_SYMBOL_GPL(kvm_vcpu_init);
  585. void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
  586. {
  587. kvm_arch_vcpu_uninit(vcpu);
  588. free_page((unsigned long)vcpu->run);
  589. }
  590. EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
  591. #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
  592. static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
  593. {
  594. return container_of(mn, struct kvm, mmu_notifier);
  595. }
  596. static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
  597. struct mm_struct *mm,
  598. unsigned long address)
  599. {
  600. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  601. int need_tlb_flush;
  602. /*
  603. * When ->invalidate_page runs, the linux pte has been zapped
  604. * already but the page is still allocated until
  605. * ->invalidate_page returns. So if we increase the sequence
  606. * here the kvm page fault will notice if the spte can't be
  607. * established because the page is going to be freed. If
  608. * instead the kvm page fault establishes the spte before
  609. * ->invalidate_page runs, kvm_unmap_hva will release it
  610. * before returning.
  611. *
  612. * The sequence increase only need to be seen at spin_unlock
  613. * time, and not at spin_lock time.
  614. *
  615. * Increasing the sequence after the spin_unlock would be
  616. * unsafe because the kvm page fault could then establish the
  617. * pte after kvm_unmap_hva returned, without noticing the page
  618. * is going to be freed.
  619. */
  620. spin_lock(&kvm->mmu_lock);
  621. kvm->mmu_notifier_seq++;
  622. need_tlb_flush = kvm_unmap_hva(kvm, address);
  623. spin_unlock(&kvm->mmu_lock);
  624. /* we've to flush the tlb before the pages can be freed */
  625. if (need_tlb_flush)
  626. kvm_flush_remote_tlbs(kvm);
  627. }
  628. static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
  629. struct mm_struct *mm,
  630. unsigned long start,
  631. unsigned long end)
  632. {
  633. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  634. int need_tlb_flush = 0;
  635. spin_lock(&kvm->mmu_lock);
  636. /*
  637. * The count increase must become visible at unlock time as no
  638. * spte can be established without taking the mmu_lock and
  639. * count is also read inside the mmu_lock critical section.
  640. */
  641. kvm->mmu_notifier_count++;
  642. for (; start < end; start += PAGE_SIZE)
  643. need_tlb_flush |= kvm_unmap_hva(kvm, start);
  644. spin_unlock(&kvm->mmu_lock);
  645. /* we've to flush the tlb before the pages can be freed */
  646. if (need_tlb_flush)
  647. kvm_flush_remote_tlbs(kvm);
  648. }
  649. static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
  650. struct mm_struct *mm,
  651. unsigned long start,
  652. unsigned long end)
  653. {
  654. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  655. spin_lock(&kvm->mmu_lock);
  656. /*
  657. * This sequence increase will notify the kvm page fault that
  658. * the page that is going to be mapped in the spte could have
  659. * been freed.
  660. */
  661. kvm->mmu_notifier_seq++;
  662. /*
  663. * The above sequence increase must be visible before the
  664. * below count decrease but both values are read by the kvm
  665. * page fault under mmu_lock spinlock so we don't need to add
  666. * a smb_wmb() here in between the two.
  667. */
  668. kvm->mmu_notifier_count--;
  669. spin_unlock(&kvm->mmu_lock);
  670. BUG_ON(kvm->mmu_notifier_count < 0);
  671. }
  672. static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
  673. struct mm_struct *mm,
  674. unsigned long address)
  675. {
  676. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  677. int young;
  678. spin_lock(&kvm->mmu_lock);
  679. young = kvm_age_hva(kvm, address);
  680. spin_unlock(&kvm->mmu_lock);
  681. if (young)
  682. kvm_flush_remote_tlbs(kvm);
  683. return young;
  684. }
  685. static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
  686. struct mm_struct *mm)
  687. {
  688. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  689. kvm_arch_flush_shadow(kvm);
  690. }
  691. static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
  692. .invalidate_page = kvm_mmu_notifier_invalidate_page,
  693. .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
  694. .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
  695. .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
  696. .release = kvm_mmu_notifier_release,
  697. };
  698. #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
  699. static struct kvm *kvm_create_vm(void)
  700. {
  701. struct kvm *kvm = kvm_arch_create_vm();
  702. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  703. struct page *page;
  704. #endif
  705. if (IS_ERR(kvm))
  706. goto out;
  707. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  708. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  709. if (!page) {
  710. kfree(kvm);
  711. return ERR_PTR(-ENOMEM);
  712. }
  713. kvm->coalesced_mmio_ring =
  714. (struct kvm_coalesced_mmio_ring *)page_address(page);
  715. #endif
  716. #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
  717. {
  718. int err;
  719. kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
  720. err = mmu_notifier_register(&kvm->mmu_notifier, current->mm);
  721. if (err) {
  722. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  723. put_page(page);
  724. #endif
  725. kfree(kvm);
  726. return ERR_PTR(err);
  727. }
  728. }
  729. #endif
  730. kvm->mm = current->mm;
  731. atomic_inc(&kvm->mm->mm_count);
  732. spin_lock_init(&kvm->mmu_lock);
  733. kvm_io_bus_init(&kvm->pio_bus);
  734. mutex_init(&kvm->lock);
  735. kvm_io_bus_init(&kvm->mmio_bus);
  736. init_rwsem(&kvm->slots_lock);
  737. atomic_set(&kvm->users_count, 1);
  738. spin_lock(&kvm_lock);
  739. list_add(&kvm->vm_list, &vm_list);
  740. spin_unlock(&kvm_lock);
  741. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  742. kvm_coalesced_mmio_init(kvm);
  743. #endif
  744. out:
  745. return kvm;
  746. }
  747. /*
  748. * Free any memory in @free but not in @dont.
  749. */
  750. static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
  751. struct kvm_memory_slot *dont)
  752. {
  753. if (!dont || free->rmap != dont->rmap)
  754. vfree(free->rmap);
  755. if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
  756. vfree(free->dirty_bitmap);
  757. if (!dont || free->lpage_info != dont->lpage_info)
  758. vfree(free->lpage_info);
  759. free->npages = 0;
  760. free->dirty_bitmap = NULL;
  761. free->rmap = NULL;
  762. free->lpage_info = NULL;
  763. }
  764. void kvm_free_physmem(struct kvm *kvm)
  765. {
  766. int i;
  767. for (i = 0; i < kvm->nmemslots; ++i)
  768. kvm_free_physmem_slot(&kvm->memslots[i], NULL);
  769. }
  770. static void kvm_destroy_vm(struct kvm *kvm)
  771. {
  772. struct mm_struct *mm = kvm->mm;
  773. kvm_arch_sync_events(kvm);
  774. spin_lock(&kvm_lock);
  775. list_del(&kvm->vm_list);
  776. spin_unlock(&kvm_lock);
  777. kvm_io_bus_destroy(&kvm->pio_bus);
  778. kvm_io_bus_destroy(&kvm->mmio_bus);
  779. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  780. if (kvm->coalesced_mmio_ring != NULL)
  781. free_page((unsigned long)kvm->coalesced_mmio_ring);
  782. #endif
  783. #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
  784. mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
  785. #endif
  786. kvm_arch_destroy_vm(kvm);
  787. mmdrop(mm);
  788. }
  789. void kvm_get_kvm(struct kvm *kvm)
  790. {
  791. atomic_inc(&kvm->users_count);
  792. }
  793. EXPORT_SYMBOL_GPL(kvm_get_kvm);
  794. void kvm_put_kvm(struct kvm *kvm)
  795. {
  796. if (atomic_dec_and_test(&kvm->users_count))
  797. kvm_destroy_vm(kvm);
  798. }
  799. EXPORT_SYMBOL_GPL(kvm_put_kvm);
  800. static int kvm_vm_release(struct inode *inode, struct file *filp)
  801. {
  802. struct kvm *kvm = filp->private_data;
  803. kvm_put_kvm(kvm);
  804. return 0;
  805. }
  806. /*
  807. * Allocate some memory and give it an address in the guest physical address
  808. * space.
  809. *
  810. * Discontiguous memory is allowed, mostly for framebuffers.
  811. *
  812. * Must be called holding mmap_sem for write.
  813. */
  814. int __kvm_set_memory_region(struct kvm *kvm,
  815. struct kvm_userspace_memory_region *mem,
  816. int user_alloc)
  817. {
  818. int r;
  819. gfn_t base_gfn;
  820. unsigned long npages;
  821. unsigned long i;
  822. struct kvm_memory_slot *memslot;
  823. struct kvm_memory_slot old, new;
  824. r = -EINVAL;
  825. /* General sanity checks */
  826. if (mem->memory_size & (PAGE_SIZE - 1))
  827. goto out;
  828. if (mem->guest_phys_addr & (PAGE_SIZE - 1))
  829. goto out;
  830. if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
  831. goto out;
  832. if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
  833. goto out;
  834. if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
  835. goto out;
  836. memslot = &kvm->memslots[mem->slot];
  837. base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
  838. npages = mem->memory_size >> PAGE_SHIFT;
  839. if (!npages)
  840. mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
  841. new = old = *memslot;
  842. new.base_gfn = base_gfn;
  843. new.npages = npages;
  844. new.flags = mem->flags;
  845. /* Disallow changing a memory slot's size. */
  846. r = -EINVAL;
  847. if (npages && old.npages && npages != old.npages)
  848. goto out_free;
  849. /* Check for overlaps */
  850. r = -EEXIST;
  851. for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
  852. struct kvm_memory_slot *s = &kvm->memslots[i];
  853. if (s == memslot)
  854. continue;
  855. if (!((base_gfn + npages <= s->base_gfn) ||
  856. (base_gfn >= s->base_gfn + s->npages)))
  857. goto out_free;
  858. }
  859. /* Free page dirty bitmap if unneeded */
  860. if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
  861. new.dirty_bitmap = NULL;
  862. r = -ENOMEM;
  863. /* Allocate if a slot is being created */
  864. #ifndef CONFIG_S390
  865. if (npages && !new.rmap) {
  866. new.rmap = vmalloc(npages * sizeof(struct page *));
  867. if (!new.rmap)
  868. goto out_free;
  869. memset(new.rmap, 0, npages * sizeof(*new.rmap));
  870. new.user_alloc = user_alloc;
  871. /*
  872. * hva_to_rmmap() serialzies with the mmu_lock and to be
  873. * safe it has to ignore memslots with !user_alloc &&
  874. * !userspace_addr.
  875. */
  876. if (user_alloc)
  877. new.userspace_addr = mem->userspace_addr;
  878. else
  879. new.userspace_addr = 0;
  880. }
  881. if (npages && !new.lpage_info) {
  882. int largepages = npages / KVM_PAGES_PER_HPAGE;
  883. if (npages % KVM_PAGES_PER_HPAGE)
  884. largepages++;
  885. if (base_gfn % KVM_PAGES_PER_HPAGE)
  886. largepages++;
  887. new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
  888. if (!new.lpage_info)
  889. goto out_free;
  890. memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
  891. if (base_gfn % KVM_PAGES_PER_HPAGE)
  892. new.lpage_info[0].write_count = 1;
  893. if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
  894. new.lpage_info[largepages-1].write_count = 1;
  895. }
  896. /* Allocate page dirty bitmap if needed */
  897. if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
  898. unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
  899. new.dirty_bitmap = vmalloc(dirty_bytes);
  900. if (!new.dirty_bitmap)
  901. goto out_free;
  902. memset(new.dirty_bitmap, 0, dirty_bytes);
  903. }
  904. #endif /* not defined CONFIG_S390 */
  905. if (!npages)
  906. kvm_arch_flush_shadow(kvm);
  907. spin_lock(&kvm->mmu_lock);
  908. if (mem->slot >= kvm->nmemslots)
  909. kvm->nmemslots = mem->slot + 1;
  910. *memslot = new;
  911. spin_unlock(&kvm->mmu_lock);
  912. r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
  913. if (r) {
  914. spin_lock(&kvm->mmu_lock);
  915. *memslot = old;
  916. spin_unlock(&kvm->mmu_lock);
  917. goto out_free;
  918. }
  919. kvm_free_physmem_slot(&old, npages ? &new : NULL);
  920. /* Slot deletion case: we have to update the current slot */
  921. if (!npages)
  922. *memslot = old;
  923. #ifdef CONFIG_DMAR
  924. /* map the pages in iommu page table */
  925. r = kvm_iommu_map_pages(kvm, base_gfn, npages);
  926. if (r)
  927. goto out;
  928. #endif
  929. return 0;
  930. out_free:
  931. kvm_free_physmem_slot(&new, &old);
  932. out:
  933. return r;
  934. }
  935. EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
  936. int kvm_set_memory_region(struct kvm *kvm,
  937. struct kvm_userspace_memory_region *mem,
  938. int user_alloc)
  939. {
  940. int r;
  941. down_write(&kvm->slots_lock);
  942. r = __kvm_set_memory_region(kvm, mem, user_alloc);
  943. up_write(&kvm->slots_lock);
  944. return r;
  945. }
  946. EXPORT_SYMBOL_GPL(kvm_set_memory_region);
  947. int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
  948. struct
  949. kvm_userspace_memory_region *mem,
  950. int user_alloc)
  951. {
  952. if (mem->slot >= KVM_MEMORY_SLOTS)
  953. return -EINVAL;
  954. return kvm_set_memory_region(kvm, mem, user_alloc);
  955. }
  956. int kvm_get_dirty_log(struct kvm *kvm,
  957. struct kvm_dirty_log *log, int *is_dirty)
  958. {
  959. struct kvm_memory_slot *memslot;
  960. int r, i;
  961. int n;
  962. unsigned long any = 0;
  963. r = -EINVAL;
  964. if (log->slot >= KVM_MEMORY_SLOTS)
  965. goto out;
  966. memslot = &kvm->memslots[log->slot];
  967. r = -ENOENT;
  968. if (!memslot->dirty_bitmap)
  969. goto out;
  970. n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
  971. for (i = 0; !any && i < n/sizeof(long); ++i)
  972. any = memslot->dirty_bitmap[i];
  973. r = -EFAULT;
  974. if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
  975. goto out;
  976. if (any)
  977. *is_dirty = 1;
  978. r = 0;
  979. out:
  980. return r;
  981. }
  982. int is_error_page(struct page *page)
  983. {
  984. return page == bad_page;
  985. }
  986. EXPORT_SYMBOL_GPL(is_error_page);
  987. int is_error_pfn(pfn_t pfn)
  988. {
  989. return pfn == bad_pfn;
  990. }
  991. EXPORT_SYMBOL_GPL(is_error_pfn);
  992. static inline unsigned long bad_hva(void)
  993. {
  994. return PAGE_OFFSET;
  995. }
  996. int kvm_is_error_hva(unsigned long addr)
  997. {
  998. return addr == bad_hva();
  999. }
  1000. EXPORT_SYMBOL_GPL(kvm_is_error_hva);
  1001. struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
  1002. {
  1003. int i;
  1004. for (i = 0; i < kvm->nmemslots; ++i) {
  1005. struct kvm_memory_slot *memslot = &kvm->memslots[i];
  1006. if (gfn >= memslot->base_gfn
  1007. && gfn < memslot->base_gfn + memslot->npages)
  1008. return memslot;
  1009. }
  1010. return NULL;
  1011. }
  1012. EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
  1013. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
  1014. {
  1015. gfn = unalias_gfn(kvm, gfn);
  1016. return gfn_to_memslot_unaliased(kvm, gfn);
  1017. }
  1018. int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
  1019. {
  1020. int i;
  1021. gfn = unalias_gfn(kvm, gfn);
  1022. for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
  1023. struct kvm_memory_slot *memslot = &kvm->memslots[i];
  1024. if (gfn >= memslot->base_gfn
  1025. && gfn < memslot->base_gfn + memslot->npages)
  1026. return 1;
  1027. }
  1028. return 0;
  1029. }
  1030. EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
  1031. unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
  1032. {
  1033. struct kvm_memory_slot *slot;
  1034. gfn = unalias_gfn(kvm, gfn);
  1035. slot = gfn_to_memslot_unaliased(kvm, gfn);
  1036. if (!slot)
  1037. return bad_hva();
  1038. return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
  1039. }
  1040. EXPORT_SYMBOL_GPL(gfn_to_hva);
  1041. pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
  1042. {
  1043. struct page *page[1];
  1044. unsigned long addr;
  1045. int npages;
  1046. pfn_t pfn;
  1047. might_sleep();
  1048. addr = gfn_to_hva(kvm, gfn);
  1049. if (kvm_is_error_hva(addr)) {
  1050. get_page(bad_page);
  1051. return page_to_pfn(bad_page);
  1052. }
  1053. npages = get_user_pages_fast(addr, 1, 1, page);
  1054. if (unlikely(npages != 1)) {
  1055. struct vm_area_struct *vma;
  1056. down_read(&current->mm->mmap_sem);
  1057. vma = find_vma(current->mm, addr);
  1058. if (vma == NULL || addr < vma->vm_start ||
  1059. !(vma->vm_flags & VM_PFNMAP)) {
  1060. up_read(&current->mm->mmap_sem);
  1061. get_page(bad_page);
  1062. return page_to_pfn(bad_page);
  1063. }
  1064. pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  1065. up_read(&current->mm->mmap_sem);
  1066. BUG_ON(!kvm_is_mmio_pfn(pfn));
  1067. } else
  1068. pfn = page_to_pfn(page[0]);
  1069. return pfn;
  1070. }
  1071. EXPORT_SYMBOL_GPL(gfn_to_pfn);
  1072. struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
  1073. {
  1074. pfn_t pfn;
  1075. pfn = gfn_to_pfn(kvm, gfn);
  1076. if (!kvm_is_mmio_pfn(pfn))
  1077. return pfn_to_page(pfn);
  1078. WARN_ON(kvm_is_mmio_pfn(pfn));
  1079. get_page(bad_page);
  1080. return bad_page;
  1081. }
  1082. EXPORT_SYMBOL_GPL(gfn_to_page);
  1083. void kvm_release_page_clean(struct page *page)
  1084. {
  1085. kvm_release_pfn_clean(page_to_pfn(page));
  1086. }
  1087. EXPORT_SYMBOL_GPL(kvm_release_page_clean);
  1088. void kvm_release_pfn_clean(pfn_t pfn)
  1089. {
  1090. if (!kvm_is_mmio_pfn(pfn))
  1091. put_page(pfn_to_page(pfn));
  1092. }
  1093. EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
  1094. void kvm_release_page_dirty(struct page *page)
  1095. {
  1096. kvm_release_pfn_dirty(page_to_pfn(page));
  1097. }
  1098. EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
  1099. void kvm_release_pfn_dirty(pfn_t pfn)
  1100. {
  1101. kvm_set_pfn_dirty(pfn);
  1102. kvm_release_pfn_clean(pfn);
  1103. }
  1104. EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
  1105. void kvm_set_page_dirty(struct page *page)
  1106. {
  1107. kvm_set_pfn_dirty(page_to_pfn(page));
  1108. }
  1109. EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
  1110. void kvm_set_pfn_dirty(pfn_t pfn)
  1111. {
  1112. if (!kvm_is_mmio_pfn(pfn)) {
  1113. struct page *page = pfn_to_page(pfn);
  1114. if (!PageReserved(page))
  1115. SetPageDirty(page);
  1116. }
  1117. }
  1118. EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
  1119. void kvm_set_pfn_accessed(pfn_t pfn)
  1120. {
  1121. if (!kvm_is_mmio_pfn(pfn))
  1122. mark_page_accessed(pfn_to_page(pfn));
  1123. }
  1124. EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
  1125. void kvm_get_pfn(pfn_t pfn)
  1126. {
  1127. if (!kvm_is_mmio_pfn(pfn))
  1128. get_page(pfn_to_page(pfn));
  1129. }
  1130. EXPORT_SYMBOL_GPL(kvm_get_pfn);
  1131. static int next_segment(unsigned long len, int offset)
  1132. {
  1133. if (len > PAGE_SIZE - offset)
  1134. return PAGE_SIZE - offset;
  1135. else
  1136. return len;
  1137. }
  1138. int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
  1139. int len)
  1140. {
  1141. int r;
  1142. unsigned long addr;
  1143. addr = gfn_to_hva(kvm, gfn);
  1144. if (kvm_is_error_hva(addr))
  1145. return -EFAULT;
  1146. r = copy_from_user(data, (void __user *)addr + offset, len);
  1147. if (r)
  1148. return -EFAULT;
  1149. return 0;
  1150. }
  1151. EXPORT_SYMBOL_GPL(kvm_read_guest_page);
  1152. int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
  1153. {
  1154. gfn_t gfn = gpa >> PAGE_SHIFT;
  1155. int seg;
  1156. int offset = offset_in_page(gpa);
  1157. int ret;
  1158. while ((seg = next_segment(len, offset)) != 0) {
  1159. ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
  1160. if (ret < 0)
  1161. return ret;
  1162. offset = 0;
  1163. len -= seg;
  1164. data += seg;
  1165. ++gfn;
  1166. }
  1167. return 0;
  1168. }
  1169. EXPORT_SYMBOL_GPL(kvm_read_guest);
  1170. int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
  1171. unsigned long len)
  1172. {
  1173. int r;
  1174. unsigned long addr;
  1175. gfn_t gfn = gpa >> PAGE_SHIFT;
  1176. int offset = offset_in_page(gpa);
  1177. addr = gfn_to_hva(kvm, gfn);
  1178. if (kvm_is_error_hva(addr))
  1179. return -EFAULT;
  1180. pagefault_disable();
  1181. r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
  1182. pagefault_enable();
  1183. if (r)
  1184. return -EFAULT;
  1185. return 0;
  1186. }
  1187. EXPORT_SYMBOL(kvm_read_guest_atomic);
  1188. int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
  1189. int offset, int len)
  1190. {
  1191. int r;
  1192. unsigned long addr;
  1193. addr = gfn_to_hva(kvm, gfn);
  1194. if (kvm_is_error_hva(addr))
  1195. return -EFAULT;
  1196. r = copy_to_user((void __user *)addr + offset, data, len);
  1197. if (r)
  1198. return -EFAULT;
  1199. mark_page_dirty(kvm, gfn);
  1200. return 0;
  1201. }
  1202. EXPORT_SYMBOL_GPL(kvm_write_guest_page);
  1203. int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
  1204. unsigned long len)
  1205. {
  1206. gfn_t gfn = gpa >> PAGE_SHIFT;
  1207. int seg;
  1208. int offset = offset_in_page(gpa);
  1209. int ret;
  1210. while ((seg = next_segment(len, offset)) != 0) {
  1211. ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
  1212. if (ret < 0)
  1213. return ret;
  1214. offset = 0;
  1215. len -= seg;
  1216. data += seg;
  1217. ++gfn;
  1218. }
  1219. return 0;
  1220. }
  1221. int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
  1222. {
  1223. return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
  1224. }
  1225. EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
  1226. int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
  1227. {
  1228. gfn_t gfn = gpa >> PAGE_SHIFT;
  1229. int seg;
  1230. int offset = offset_in_page(gpa);
  1231. int ret;
  1232. while ((seg = next_segment(len, offset)) != 0) {
  1233. ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
  1234. if (ret < 0)
  1235. return ret;
  1236. offset = 0;
  1237. len -= seg;
  1238. ++gfn;
  1239. }
  1240. return 0;
  1241. }
  1242. EXPORT_SYMBOL_GPL(kvm_clear_guest);
  1243. void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
  1244. {
  1245. struct kvm_memory_slot *memslot;
  1246. gfn = unalias_gfn(kvm, gfn);
  1247. memslot = gfn_to_memslot_unaliased(kvm, gfn);
  1248. if (memslot && memslot->dirty_bitmap) {
  1249. unsigned long rel_gfn = gfn - memslot->base_gfn;
  1250. /* avoid RMW */
  1251. if (!test_bit(rel_gfn, memslot->dirty_bitmap))
  1252. set_bit(rel_gfn, memslot->dirty_bitmap);
  1253. }
  1254. }
  1255. /*
  1256. * The vCPU has executed a HLT instruction with in-kernel mode enabled.
  1257. */
  1258. void kvm_vcpu_block(struct kvm_vcpu *vcpu)
  1259. {
  1260. DEFINE_WAIT(wait);
  1261. for (;;) {
  1262. prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
  1263. if (kvm_cpu_has_interrupt(vcpu) ||
  1264. kvm_cpu_has_pending_timer(vcpu) ||
  1265. kvm_arch_vcpu_runnable(vcpu)) {
  1266. set_bit(KVM_REQ_UNHALT, &vcpu->requests);
  1267. break;
  1268. }
  1269. if (signal_pending(current))
  1270. break;
  1271. vcpu_put(vcpu);
  1272. schedule();
  1273. vcpu_load(vcpu);
  1274. }
  1275. finish_wait(&vcpu->wq, &wait);
  1276. }
  1277. void kvm_resched(struct kvm_vcpu *vcpu)
  1278. {
  1279. if (!need_resched())
  1280. return;
  1281. cond_resched();
  1282. }
  1283. EXPORT_SYMBOL_GPL(kvm_resched);
  1284. static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1285. {
  1286. struct kvm_vcpu *vcpu = vma->vm_file->private_data;
  1287. struct page *page;
  1288. if (vmf->pgoff == 0)
  1289. page = virt_to_page(vcpu->run);
  1290. #ifdef CONFIG_X86
  1291. else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
  1292. page = virt_to_page(vcpu->arch.pio_data);
  1293. #endif
  1294. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  1295. else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
  1296. page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
  1297. #endif
  1298. else
  1299. return VM_FAULT_SIGBUS;
  1300. get_page(page);
  1301. vmf->page = page;
  1302. return 0;
  1303. }
  1304. static struct vm_operations_struct kvm_vcpu_vm_ops = {
  1305. .fault = kvm_vcpu_fault,
  1306. };
  1307. static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
  1308. {
  1309. vma->vm_ops = &kvm_vcpu_vm_ops;
  1310. return 0;
  1311. }
  1312. static int kvm_vcpu_release(struct inode *inode, struct file *filp)
  1313. {
  1314. struct kvm_vcpu *vcpu = filp->private_data;
  1315. kvm_put_kvm(vcpu->kvm);
  1316. return 0;
  1317. }
  1318. static struct file_operations kvm_vcpu_fops = {
  1319. .release = kvm_vcpu_release,
  1320. .unlocked_ioctl = kvm_vcpu_ioctl,
  1321. .compat_ioctl = kvm_vcpu_ioctl,
  1322. .mmap = kvm_vcpu_mmap,
  1323. };
  1324. /*
  1325. * Allocates an inode for the vcpu.
  1326. */
  1327. static int create_vcpu_fd(struct kvm_vcpu *vcpu)
  1328. {
  1329. int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
  1330. if (fd < 0)
  1331. kvm_put_kvm(vcpu->kvm);
  1332. return fd;
  1333. }
  1334. /*
  1335. * Creates some virtual cpus. Good luck creating more than one.
  1336. */
  1337. static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
  1338. {
  1339. int r;
  1340. struct kvm_vcpu *vcpu;
  1341. if (!valid_vcpu(n))
  1342. return -EINVAL;
  1343. vcpu = kvm_arch_vcpu_create(kvm, n);
  1344. if (IS_ERR(vcpu))
  1345. return PTR_ERR(vcpu);
  1346. preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
  1347. r = kvm_arch_vcpu_setup(vcpu);
  1348. if (r)
  1349. return r;
  1350. mutex_lock(&kvm->lock);
  1351. if (kvm->vcpus[n]) {
  1352. r = -EEXIST;
  1353. goto vcpu_destroy;
  1354. }
  1355. kvm->vcpus[n] = vcpu;
  1356. mutex_unlock(&kvm->lock);
  1357. /* Now it's all set up, let userspace reach it */
  1358. kvm_get_kvm(kvm);
  1359. r = create_vcpu_fd(vcpu);
  1360. if (r < 0)
  1361. goto unlink;
  1362. return r;
  1363. unlink:
  1364. mutex_lock(&kvm->lock);
  1365. kvm->vcpus[n] = NULL;
  1366. vcpu_destroy:
  1367. mutex_unlock(&kvm->lock);
  1368. kvm_arch_vcpu_destroy(vcpu);
  1369. return r;
  1370. }
  1371. static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
  1372. {
  1373. if (sigset) {
  1374. sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1375. vcpu->sigset_active = 1;
  1376. vcpu->sigset = *sigset;
  1377. } else
  1378. vcpu->sigset_active = 0;
  1379. return 0;
  1380. }
  1381. static long kvm_vcpu_ioctl(struct file *filp,
  1382. unsigned int ioctl, unsigned long arg)
  1383. {
  1384. struct kvm_vcpu *vcpu = filp->private_data;
  1385. void __user *argp = (void __user *)arg;
  1386. int r;
  1387. struct kvm_fpu *fpu = NULL;
  1388. struct kvm_sregs *kvm_sregs = NULL;
  1389. if (vcpu->kvm->mm != current->mm)
  1390. return -EIO;
  1391. switch (ioctl) {
  1392. case KVM_RUN:
  1393. r = -EINVAL;
  1394. if (arg)
  1395. goto out;
  1396. r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
  1397. break;
  1398. case KVM_GET_REGS: {
  1399. struct kvm_regs *kvm_regs;
  1400. r = -ENOMEM;
  1401. kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
  1402. if (!kvm_regs)
  1403. goto out;
  1404. r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
  1405. if (r)
  1406. goto out_free1;
  1407. r = -EFAULT;
  1408. if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
  1409. goto out_free1;
  1410. r = 0;
  1411. out_free1:
  1412. kfree(kvm_regs);
  1413. break;
  1414. }
  1415. case KVM_SET_REGS: {
  1416. struct kvm_regs *kvm_regs;
  1417. r = -ENOMEM;
  1418. kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
  1419. if (!kvm_regs)
  1420. goto out;
  1421. r = -EFAULT;
  1422. if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
  1423. goto out_free2;
  1424. r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
  1425. if (r)
  1426. goto out_free2;
  1427. r = 0;
  1428. out_free2:
  1429. kfree(kvm_regs);
  1430. break;
  1431. }
  1432. case KVM_GET_SREGS: {
  1433. kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
  1434. r = -ENOMEM;
  1435. if (!kvm_sregs)
  1436. goto out;
  1437. r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
  1438. if (r)
  1439. goto out;
  1440. r = -EFAULT;
  1441. if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
  1442. goto out;
  1443. r = 0;
  1444. break;
  1445. }
  1446. case KVM_SET_SREGS: {
  1447. kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
  1448. r = -ENOMEM;
  1449. if (!kvm_sregs)
  1450. goto out;
  1451. r = -EFAULT;
  1452. if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
  1453. goto out;
  1454. r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
  1455. if (r)
  1456. goto out;
  1457. r = 0;
  1458. break;
  1459. }
  1460. case KVM_GET_MP_STATE: {
  1461. struct kvm_mp_state mp_state;
  1462. r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
  1463. if (r)
  1464. goto out;
  1465. r = -EFAULT;
  1466. if (copy_to_user(argp, &mp_state, sizeof mp_state))
  1467. goto out;
  1468. r = 0;
  1469. break;
  1470. }
  1471. case KVM_SET_MP_STATE: {
  1472. struct kvm_mp_state mp_state;
  1473. r = -EFAULT;
  1474. if (copy_from_user(&mp_state, argp, sizeof mp_state))
  1475. goto out;
  1476. r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
  1477. if (r)
  1478. goto out;
  1479. r = 0;
  1480. break;
  1481. }
  1482. case KVM_TRANSLATE: {
  1483. struct kvm_translation tr;
  1484. r = -EFAULT;
  1485. if (copy_from_user(&tr, argp, sizeof tr))
  1486. goto out;
  1487. r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
  1488. if (r)
  1489. goto out;
  1490. r = -EFAULT;
  1491. if (copy_to_user(argp, &tr, sizeof tr))
  1492. goto out;
  1493. r = 0;
  1494. break;
  1495. }
  1496. case KVM_DEBUG_GUEST: {
  1497. struct kvm_debug_guest dbg;
  1498. r = -EFAULT;
  1499. if (copy_from_user(&dbg, argp, sizeof dbg))
  1500. goto out;
  1501. r = kvm_arch_vcpu_ioctl_debug_guest(vcpu, &dbg);
  1502. if (r)
  1503. goto out;
  1504. r = 0;
  1505. break;
  1506. }
  1507. case KVM_SET_SIGNAL_MASK: {
  1508. struct kvm_signal_mask __user *sigmask_arg = argp;
  1509. struct kvm_signal_mask kvm_sigmask;
  1510. sigset_t sigset, *p;
  1511. p = NULL;
  1512. if (argp) {
  1513. r = -EFAULT;
  1514. if (copy_from_user(&kvm_sigmask, argp,
  1515. sizeof kvm_sigmask))
  1516. goto out;
  1517. r = -EINVAL;
  1518. if (kvm_sigmask.len != sizeof sigset)
  1519. goto out;
  1520. r = -EFAULT;
  1521. if (copy_from_user(&sigset, sigmask_arg->sigset,
  1522. sizeof sigset))
  1523. goto out;
  1524. p = &sigset;
  1525. }
  1526. r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
  1527. break;
  1528. }
  1529. case KVM_GET_FPU: {
  1530. fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
  1531. r = -ENOMEM;
  1532. if (!fpu)
  1533. goto out;
  1534. r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
  1535. if (r)
  1536. goto out;
  1537. r = -EFAULT;
  1538. if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
  1539. goto out;
  1540. r = 0;
  1541. break;
  1542. }
  1543. case KVM_SET_FPU: {
  1544. fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
  1545. r = -ENOMEM;
  1546. if (!fpu)
  1547. goto out;
  1548. r = -EFAULT;
  1549. if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
  1550. goto out;
  1551. r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
  1552. if (r)
  1553. goto out;
  1554. r = 0;
  1555. break;
  1556. }
  1557. default:
  1558. r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
  1559. }
  1560. out:
  1561. kfree(fpu);
  1562. kfree(kvm_sregs);
  1563. return r;
  1564. }
  1565. static long kvm_vm_ioctl(struct file *filp,
  1566. unsigned int ioctl, unsigned long arg)
  1567. {
  1568. struct kvm *kvm = filp->private_data;
  1569. void __user *argp = (void __user *)arg;
  1570. int r;
  1571. if (kvm->mm != current->mm)
  1572. return -EIO;
  1573. switch (ioctl) {
  1574. case KVM_CREATE_VCPU:
  1575. r = kvm_vm_ioctl_create_vcpu(kvm, arg);
  1576. if (r < 0)
  1577. goto out;
  1578. break;
  1579. case KVM_SET_USER_MEMORY_REGION: {
  1580. struct kvm_userspace_memory_region kvm_userspace_mem;
  1581. r = -EFAULT;
  1582. if (copy_from_user(&kvm_userspace_mem, argp,
  1583. sizeof kvm_userspace_mem))
  1584. goto out;
  1585. r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
  1586. if (r)
  1587. goto out;
  1588. break;
  1589. }
  1590. case KVM_GET_DIRTY_LOG: {
  1591. struct kvm_dirty_log log;
  1592. r = -EFAULT;
  1593. if (copy_from_user(&log, argp, sizeof log))
  1594. goto out;
  1595. r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
  1596. if (r)
  1597. goto out;
  1598. break;
  1599. }
  1600. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  1601. case KVM_REGISTER_COALESCED_MMIO: {
  1602. struct kvm_coalesced_mmio_zone zone;
  1603. r = -EFAULT;
  1604. if (copy_from_user(&zone, argp, sizeof zone))
  1605. goto out;
  1606. r = -ENXIO;
  1607. r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
  1608. if (r)
  1609. goto out;
  1610. r = 0;
  1611. break;
  1612. }
  1613. case KVM_UNREGISTER_COALESCED_MMIO: {
  1614. struct kvm_coalesced_mmio_zone zone;
  1615. r = -EFAULT;
  1616. if (copy_from_user(&zone, argp, sizeof zone))
  1617. goto out;
  1618. r = -ENXIO;
  1619. r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
  1620. if (r)
  1621. goto out;
  1622. r = 0;
  1623. break;
  1624. }
  1625. #endif
  1626. #ifdef KVM_CAP_DEVICE_ASSIGNMENT
  1627. case KVM_ASSIGN_PCI_DEVICE: {
  1628. struct kvm_assigned_pci_dev assigned_dev;
  1629. r = -EFAULT;
  1630. if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
  1631. goto out;
  1632. r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
  1633. if (r)
  1634. goto out;
  1635. break;
  1636. }
  1637. case KVM_ASSIGN_IRQ: {
  1638. struct kvm_assigned_irq assigned_irq;
  1639. r = -EFAULT;
  1640. if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
  1641. goto out;
  1642. r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
  1643. if (r)
  1644. goto out;
  1645. break;
  1646. }
  1647. #endif
  1648. #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
  1649. case KVM_DEASSIGN_PCI_DEVICE: {
  1650. struct kvm_assigned_pci_dev assigned_dev;
  1651. r = -EFAULT;
  1652. if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
  1653. goto out;
  1654. r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
  1655. if (r)
  1656. goto out;
  1657. break;
  1658. }
  1659. #endif
  1660. default:
  1661. r = kvm_arch_vm_ioctl(filp, ioctl, arg);
  1662. }
  1663. out:
  1664. return r;
  1665. }
  1666. static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1667. {
  1668. struct page *page[1];
  1669. unsigned long addr;
  1670. int npages;
  1671. gfn_t gfn = vmf->pgoff;
  1672. struct kvm *kvm = vma->vm_file->private_data;
  1673. addr = gfn_to_hva(kvm, gfn);
  1674. if (kvm_is_error_hva(addr))
  1675. return VM_FAULT_SIGBUS;
  1676. npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
  1677. NULL);
  1678. if (unlikely(npages != 1))
  1679. return VM_FAULT_SIGBUS;
  1680. vmf->page = page[0];
  1681. return 0;
  1682. }
  1683. static struct vm_operations_struct kvm_vm_vm_ops = {
  1684. .fault = kvm_vm_fault,
  1685. };
  1686. static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
  1687. {
  1688. vma->vm_ops = &kvm_vm_vm_ops;
  1689. return 0;
  1690. }
  1691. static struct file_operations kvm_vm_fops = {
  1692. .release = kvm_vm_release,
  1693. .unlocked_ioctl = kvm_vm_ioctl,
  1694. .compat_ioctl = kvm_vm_ioctl,
  1695. .mmap = kvm_vm_mmap,
  1696. };
  1697. static int kvm_dev_ioctl_create_vm(void)
  1698. {
  1699. int fd;
  1700. struct kvm *kvm;
  1701. kvm = kvm_create_vm();
  1702. if (IS_ERR(kvm))
  1703. return PTR_ERR(kvm);
  1704. fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
  1705. if (fd < 0)
  1706. kvm_put_kvm(kvm);
  1707. return fd;
  1708. }
  1709. static long kvm_dev_ioctl_check_extension_generic(long arg)
  1710. {
  1711. switch (arg) {
  1712. case KVM_CAP_USER_MEMORY:
  1713. case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
  1714. return 1;
  1715. default:
  1716. break;
  1717. }
  1718. return kvm_dev_ioctl_check_extension(arg);
  1719. }
  1720. static long kvm_dev_ioctl(struct file *filp,
  1721. unsigned int ioctl, unsigned long arg)
  1722. {
  1723. long r = -EINVAL;
  1724. switch (ioctl) {
  1725. case KVM_GET_API_VERSION:
  1726. r = -EINVAL;
  1727. if (arg)
  1728. goto out;
  1729. r = KVM_API_VERSION;
  1730. break;
  1731. case KVM_CREATE_VM:
  1732. r = -EINVAL;
  1733. if (arg)
  1734. goto out;
  1735. r = kvm_dev_ioctl_create_vm();
  1736. break;
  1737. case KVM_CHECK_EXTENSION:
  1738. r = kvm_dev_ioctl_check_extension_generic(arg);
  1739. break;
  1740. case KVM_GET_VCPU_MMAP_SIZE:
  1741. r = -EINVAL;
  1742. if (arg)
  1743. goto out;
  1744. r = PAGE_SIZE; /* struct kvm_run */
  1745. #ifdef CONFIG_X86
  1746. r += PAGE_SIZE; /* pio data page */
  1747. #endif
  1748. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  1749. r += PAGE_SIZE; /* coalesced mmio ring page */
  1750. #endif
  1751. break;
  1752. case KVM_TRACE_ENABLE:
  1753. case KVM_TRACE_PAUSE:
  1754. case KVM_TRACE_DISABLE:
  1755. r = kvm_trace_ioctl(ioctl, arg);
  1756. break;
  1757. default:
  1758. return kvm_arch_dev_ioctl(filp, ioctl, arg);
  1759. }
  1760. out:
  1761. return r;
  1762. }
  1763. static struct file_operations kvm_chardev_ops = {
  1764. .unlocked_ioctl = kvm_dev_ioctl,
  1765. .compat_ioctl = kvm_dev_ioctl,
  1766. };
  1767. static struct miscdevice kvm_dev = {
  1768. KVM_MINOR,
  1769. "kvm",
  1770. &kvm_chardev_ops,
  1771. };
  1772. static void hardware_enable(void *junk)
  1773. {
  1774. int cpu = raw_smp_processor_id();
  1775. if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
  1776. return;
  1777. cpumask_set_cpu(cpu, cpus_hardware_enabled);
  1778. kvm_arch_hardware_enable(NULL);
  1779. }
  1780. static void hardware_disable(void *junk)
  1781. {
  1782. int cpu = raw_smp_processor_id();
  1783. if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
  1784. return;
  1785. cpumask_clear_cpu(cpu, cpus_hardware_enabled);
  1786. kvm_arch_hardware_disable(NULL);
  1787. }
  1788. static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
  1789. void *v)
  1790. {
  1791. int cpu = (long)v;
  1792. val &= ~CPU_TASKS_FROZEN;
  1793. switch (val) {
  1794. case CPU_DYING:
  1795. printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
  1796. cpu);
  1797. hardware_disable(NULL);
  1798. break;
  1799. case CPU_UP_CANCELED:
  1800. printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
  1801. cpu);
  1802. smp_call_function_single(cpu, hardware_disable, NULL, 1);
  1803. break;
  1804. case CPU_ONLINE:
  1805. printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
  1806. cpu);
  1807. smp_call_function_single(cpu, hardware_enable, NULL, 1);
  1808. break;
  1809. }
  1810. return NOTIFY_OK;
  1811. }
  1812. asmlinkage void kvm_handle_fault_on_reboot(void)
  1813. {
  1814. if (kvm_rebooting)
  1815. /* spin while reset goes on */
  1816. while (true)
  1817. ;
  1818. /* Fault while not rebooting. We want the trace. */
  1819. BUG();
  1820. }
  1821. EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
  1822. static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
  1823. void *v)
  1824. {
  1825. if (val == SYS_RESTART) {
  1826. /*
  1827. * Some (well, at least mine) BIOSes hang on reboot if
  1828. * in vmx root mode.
  1829. */
  1830. printk(KERN_INFO "kvm: exiting hardware virtualization\n");
  1831. kvm_rebooting = true;
  1832. on_each_cpu(hardware_disable, NULL, 1);
  1833. }
  1834. return NOTIFY_OK;
  1835. }
  1836. static struct notifier_block kvm_reboot_notifier = {
  1837. .notifier_call = kvm_reboot,
  1838. .priority = 0,
  1839. };
  1840. void kvm_io_bus_init(struct kvm_io_bus *bus)
  1841. {
  1842. memset(bus, 0, sizeof(*bus));
  1843. }
  1844. void kvm_io_bus_destroy(struct kvm_io_bus *bus)
  1845. {
  1846. int i;
  1847. for (i = 0; i < bus->dev_count; i++) {
  1848. struct kvm_io_device *pos = bus->devs[i];
  1849. kvm_iodevice_destructor(pos);
  1850. }
  1851. }
  1852. struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
  1853. gpa_t addr, int len, int is_write)
  1854. {
  1855. int i;
  1856. for (i = 0; i < bus->dev_count; i++) {
  1857. struct kvm_io_device *pos = bus->devs[i];
  1858. if (pos->in_range(pos, addr, len, is_write))
  1859. return pos;
  1860. }
  1861. return NULL;
  1862. }
  1863. void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
  1864. {
  1865. BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
  1866. bus->devs[bus->dev_count++] = dev;
  1867. }
  1868. static struct notifier_block kvm_cpu_notifier = {
  1869. .notifier_call = kvm_cpu_hotplug,
  1870. .priority = 20, /* must be > scheduler priority */
  1871. };
  1872. static int vm_stat_get(void *_offset, u64 *val)
  1873. {
  1874. unsigned offset = (long)_offset;
  1875. struct kvm *kvm;
  1876. *val = 0;
  1877. spin_lock(&kvm_lock);
  1878. list_for_each_entry(kvm, &vm_list, vm_list)
  1879. *val += *(u32 *)((void *)kvm + offset);
  1880. spin_unlock(&kvm_lock);
  1881. return 0;
  1882. }
  1883. DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
  1884. static int vcpu_stat_get(void *_offset, u64 *val)
  1885. {
  1886. unsigned offset = (long)_offset;
  1887. struct kvm *kvm;
  1888. struct kvm_vcpu *vcpu;
  1889. int i;
  1890. *val = 0;
  1891. spin_lock(&kvm_lock);
  1892. list_for_each_entry(kvm, &vm_list, vm_list)
  1893. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  1894. vcpu = kvm->vcpus[i];
  1895. if (vcpu)
  1896. *val += *(u32 *)((void *)vcpu + offset);
  1897. }
  1898. spin_unlock(&kvm_lock);
  1899. return 0;
  1900. }
  1901. DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
  1902. static struct file_operations *stat_fops[] = {
  1903. [KVM_STAT_VCPU] = &vcpu_stat_fops,
  1904. [KVM_STAT_VM] = &vm_stat_fops,
  1905. };
  1906. static void kvm_init_debug(void)
  1907. {
  1908. struct kvm_stats_debugfs_item *p;
  1909. kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
  1910. for (p = debugfs_entries; p->name; ++p)
  1911. p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
  1912. (void *)(long)p->offset,
  1913. stat_fops[p->kind]);
  1914. }
  1915. static void kvm_exit_debug(void)
  1916. {
  1917. struct kvm_stats_debugfs_item *p;
  1918. for (p = debugfs_entries; p->name; ++p)
  1919. debugfs_remove(p->dentry);
  1920. debugfs_remove(kvm_debugfs_dir);
  1921. }
  1922. static int kvm_suspend(struct sys_device *dev, pm_message_t state)
  1923. {
  1924. hardware_disable(NULL);
  1925. return 0;
  1926. }
  1927. static int kvm_resume(struct sys_device *dev)
  1928. {
  1929. hardware_enable(NULL);
  1930. return 0;
  1931. }
  1932. static struct sysdev_class kvm_sysdev_class = {
  1933. .name = "kvm",
  1934. .suspend = kvm_suspend,
  1935. .resume = kvm_resume,
  1936. };
  1937. static struct sys_device kvm_sysdev = {
  1938. .id = 0,
  1939. .cls = &kvm_sysdev_class,
  1940. };
  1941. struct page *bad_page;
  1942. pfn_t bad_pfn;
  1943. static inline
  1944. struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
  1945. {
  1946. return container_of(pn, struct kvm_vcpu, preempt_notifier);
  1947. }
  1948. static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
  1949. {
  1950. struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
  1951. kvm_arch_vcpu_load(vcpu, cpu);
  1952. }
  1953. static void kvm_sched_out(struct preempt_notifier *pn,
  1954. struct task_struct *next)
  1955. {
  1956. struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
  1957. kvm_arch_vcpu_put(vcpu);
  1958. }
  1959. int kvm_init(void *opaque, unsigned int vcpu_size,
  1960. struct module *module)
  1961. {
  1962. int r;
  1963. int cpu;
  1964. kvm_init_debug();
  1965. r = kvm_arch_init(opaque);
  1966. if (r)
  1967. goto out_fail;
  1968. bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  1969. if (bad_page == NULL) {
  1970. r = -ENOMEM;
  1971. goto out;
  1972. }
  1973. bad_pfn = page_to_pfn(bad_page);
  1974. if (!alloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
  1975. r = -ENOMEM;
  1976. goto out_free_0;
  1977. }
  1978. r = kvm_arch_hardware_setup();
  1979. if (r < 0)
  1980. goto out_free_0a;
  1981. for_each_online_cpu(cpu) {
  1982. smp_call_function_single(cpu,
  1983. kvm_arch_check_processor_compat,
  1984. &r, 1);
  1985. if (r < 0)
  1986. goto out_free_1;
  1987. }
  1988. on_each_cpu(hardware_enable, NULL, 1);
  1989. r = register_cpu_notifier(&kvm_cpu_notifier);
  1990. if (r)
  1991. goto out_free_2;
  1992. register_reboot_notifier(&kvm_reboot_notifier);
  1993. r = sysdev_class_register(&kvm_sysdev_class);
  1994. if (r)
  1995. goto out_free_3;
  1996. r = sysdev_register(&kvm_sysdev);
  1997. if (r)
  1998. goto out_free_4;
  1999. /* A kmem cache lets us meet the alignment requirements of fx_save. */
  2000. kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
  2001. __alignof__(struct kvm_vcpu),
  2002. 0, NULL);
  2003. if (!kvm_vcpu_cache) {
  2004. r = -ENOMEM;
  2005. goto out_free_5;
  2006. }
  2007. kvm_chardev_ops.owner = module;
  2008. kvm_vm_fops.owner = module;
  2009. kvm_vcpu_fops.owner = module;
  2010. r = misc_register(&kvm_dev);
  2011. if (r) {
  2012. printk(KERN_ERR "kvm: misc device register failed\n");
  2013. goto out_free;
  2014. }
  2015. kvm_preempt_ops.sched_in = kvm_sched_in;
  2016. kvm_preempt_ops.sched_out = kvm_sched_out;
  2017. #ifndef CONFIG_X86
  2018. msi2intx = 0;
  2019. #endif
  2020. return 0;
  2021. out_free:
  2022. kmem_cache_destroy(kvm_vcpu_cache);
  2023. out_free_5:
  2024. sysdev_unregister(&kvm_sysdev);
  2025. out_free_4:
  2026. sysdev_class_unregister(&kvm_sysdev_class);
  2027. out_free_3:
  2028. unregister_reboot_notifier(&kvm_reboot_notifier);
  2029. unregister_cpu_notifier(&kvm_cpu_notifier);
  2030. out_free_2:
  2031. on_each_cpu(hardware_disable, NULL, 1);
  2032. out_free_1:
  2033. kvm_arch_hardware_unsetup();
  2034. out_free_0a:
  2035. free_cpumask_var(cpus_hardware_enabled);
  2036. out_free_0:
  2037. __free_page(bad_page);
  2038. out:
  2039. kvm_arch_exit();
  2040. kvm_exit_debug();
  2041. out_fail:
  2042. return r;
  2043. }
  2044. EXPORT_SYMBOL_GPL(kvm_init);
  2045. void kvm_exit(void)
  2046. {
  2047. kvm_trace_cleanup();
  2048. misc_deregister(&kvm_dev);
  2049. kmem_cache_destroy(kvm_vcpu_cache);
  2050. sysdev_unregister(&kvm_sysdev);
  2051. sysdev_class_unregister(&kvm_sysdev_class);
  2052. unregister_reboot_notifier(&kvm_reboot_notifier);
  2053. unregister_cpu_notifier(&kvm_cpu_notifier);
  2054. on_each_cpu(hardware_disable, NULL, 1);
  2055. kvm_arch_hardware_unsetup();
  2056. kvm_arch_exit();
  2057. kvm_exit_debug();
  2058. free_cpumask_var(cpus_hardware_enabled);
  2059. __free_page(bad_page);
  2060. }
  2061. EXPORT_SYMBOL_GPL(kvm_exit);