kvm_main.c 53 KB

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