kvm_main.c 54 KB

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