kvm_main.c 53 KB

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