kvm_main.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403
  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. int largepages;
  785. unsigned long i;
  786. struct kvm_memory_slot *memslot;
  787. struct kvm_memory_slot old, new;
  788. r = -EINVAL;
  789. /* General sanity checks */
  790. if (mem->memory_size & (PAGE_SIZE - 1))
  791. goto out;
  792. if (mem->guest_phys_addr & (PAGE_SIZE - 1))
  793. goto out;
  794. if (user_alloc && (mem->userspace_addr & (PAGE_SIZE - 1)))
  795. goto out;
  796. if (mem->slot >= KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
  797. goto out;
  798. if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
  799. goto out;
  800. memslot = &kvm->memslots[mem->slot];
  801. base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
  802. npages = mem->memory_size >> PAGE_SHIFT;
  803. if (!npages)
  804. mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
  805. new = old = *memslot;
  806. new.base_gfn = base_gfn;
  807. new.npages = npages;
  808. new.flags = mem->flags;
  809. /* Disallow changing a memory slot's size. */
  810. r = -EINVAL;
  811. if (npages && old.npages && npages != old.npages)
  812. goto out_free;
  813. /* Check for overlaps */
  814. r = -EEXIST;
  815. for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
  816. struct kvm_memory_slot *s = &kvm->memslots[i];
  817. if (s == memslot || !s->npages)
  818. continue;
  819. if (!((base_gfn + npages <= s->base_gfn) ||
  820. (base_gfn >= s->base_gfn + s->npages)))
  821. goto out_free;
  822. }
  823. /* Free page dirty bitmap if unneeded */
  824. if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
  825. new.dirty_bitmap = NULL;
  826. r = -ENOMEM;
  827. /* Allocate if a slot is being created */
  828. #ifndef CONFIG_S390
  829. if (npages && !new.rmap) {
  830. new.rmap = vmalloc(npages * sizeof(struct page *));
  831. if (!new.rmap)
  832. goto out_free;
  833. memset(new.rmap, 0, npages * sizeof(*new.rmap));
  834. new.user_alloc = user_alloc;
  835. /*
  836. * hva_to_rmmap() serialzies with the mmu_lock and to be
  837. * safe it has to ignore memslots with !user_alloc &&
  838. * !userspace_addr.
  839. */
  840. if (user_alloc)
  841. new.userspace_addr = mem->userspace_addr;
  842. else
  843. new.userspace_addr = 0;
  844. }
  845. if (npages && !new.lpage_info) {
  846. largepages = 1 + (base_gfn + npages - 1) / KVM_PAGES_PER_HPAGE;
  847. largepages -= base_gfn / KVM_PAGES_PER_HPAGE;
  848. new.lpage_info = vmalloc(largepages * sizeof(*new.lpage_info));
  849. if (!new.lpage_info)
  850. goto out_free;
  851. memset(new.lpage_info, 0, largepages * sizeof(*new.lpage_info));
  852. if (base_gfn % KVM_PAGES_PER_HPAGE)
  853. new.lpage_info[0].write_count = 1;
  854. if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE)
  855. new.lpage_info[largepages-1].write_count = 1;
  856. }
  857. /* Allocate page dirty bitmap if needed */
  858. if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
  859. unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
  860. new.dirty_bitmap = vmalloc(dirty_bytes);
  861. if (!new.dirty_bitmap)
  862. goto out_free;
  863. memset(new.dirty_bitmap, 0, dirty_bytes);
  864. }
  865. #endif /* not defined CONFIG_S390 */
  866. if (!npages)
  867. kvm_arch_flush_shadow(kvm);
  868. spin_lock(&kvm->mmu_lock);
  869. if (mem->slot >= kvm->nmemslots)
  870. kvm->nmemslots = mem->slot + 1;
  871. *memslot = new;
  872. spin_unlock(&kvm->mmu_lock);
  873. r = kvm_arch_set_memory_region(kvm, mem, old, user_alloc);
  874. if (r) {
  875. spin_lock(&kvm->mmu_lock);
  876. *memslot = old;
  877. spin_unlock(&kvm->mmu_lock);
  878. goto out_free;
  879. }
  880. kvm_free_physmem_slot(&old, npages ? &new : NULL);
  881. /* Slot deletion case: we have to update the current slot */
  882. if (!npages)
  883. *memslot = old;
  884. #ifdef CONFIG_DMAR
  885. /* map the pages in iommu page table */
  886. r = kvm_iommu_map_pages(kvm, base_gfn, npages);
  887. if (r)
  888. goto out;
  889. #endif
  890. return 0;
  891. out_free:
  892. kvm_free_physmem_slot(&new, &old);
  893. out:
  894. return r;
  895. }
  896. EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
  897. int kvm_set_memory_region(struct kvm *kvm,
  898. struct kvm_userspace_memory_region *mem,
  899. int user_alloc)
  900. {
  901. int r;
  902. down_write(&kvm->slots_lock);
  903. r = __kvm_set_memory_region(kvm, mem, user_alloc);
  904. up_write(&kvm->slots_lock);
  905. return r;
  906. }
  907. EXPORT_SYMBOL_GPL(kvm_set_memory_region);
  908. int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
  909. struct
  910. kvm_userspace_memory_region *mem,
  911. int user_alloc)
  912. {
  913. if (mem->slot >= KVM_MEMORY_SLOTS)
  914. return -EINVAL;
  915. return kvm_set_memory_region(kvm, mem, user_alloc);
  916. }
  917. int kvm_get_dirty_log(struct kvm *kvm,
  918. struct kvm_dirty_log *log, int *is_dirty)
  919. {
  920. struct kvm_memory_slot *memslot;
  921. int r, i;
  922. int n;
  923. unsigned long any = 0;
  924. r = -EINVAL;
  925. if (log->slot >= KVM_MEMORY_SLOTS)
  926. goto out;
  927. memslot = &kvm->memslots[log->slot];
  928. r = -ENOENT;
  929. if (!memslot->dirty_bitmap)
  930. goto out;
  931. n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
  932. for (i = 0; !any && i < n/sizeof(long); ++i)
  933. any = memslot->dirty_bitmap[i];
  934. r = -EFAULT;
  935. if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
  936. goto out;
  937. if (any)
  938. *is_dirty = 1;
  939. r = 0;
  940. out:
  941. return r;
  942. }
  943. int is_error_page(struct page *page)
  944. {
  945. return page == bad_page;
  946. }
  947. EXPORT_SYMBOL_GPL(is_error_page);
  948. int is_error_pfn(pfn_t pfn)
  949. {
  950. return pfn == bad_pfn;
  951. }
  952. EXPORT_SYMBOL_GPL(is_error_pfn);
  953. static inline unsigned long bad_hva(void)
  954. {
  955. return PAGE_OFFSET;
  956. }
  957. int kvm_is_error_hva(unsigned long addr)
  958. {
  959. return addr == bad_hva();
  960. }
  961. EXPORT_SYMBOL_GPL(kvm_is_error_hva);
  962. struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn)
  963. {
  964. int i;
  965. for (i = 0; i < kvm->nmemslots; ++i) {
  966. struct kvm_memory_slot *memslot = &kvm->memslots[i];
  967. if (gfn >= memslot->base_gfn
  968. && gfn < memslot->base_gfn + memslot->npages)
  969. return memslot;
  970. }
  971. return NULL;
  972. }
  973. EXPORT_SYMBOL_GPL(gfn_to_memslot_unaliased);
  974. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
  975. {
  976. gfn = unalias_gfn(kvm, gfn);
  977. return gfn_to_memslot_unaliased(kvm, gfn);
  978. }
  979. int kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
  980. {
  981. int i;
  982. gfn = unalias_gfn(kvm, gfn);
  983. for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
  984. struct kvm_memory_slot *memslot = &kvm->memslots[i];
  985. if (gfn >= memslot->base_gfn
  986. && gfn < memslot->base_gfn + memslot->npages)
  987. return 1;
  988. }
  989. return 0;
  990. }
  991. EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
  992. unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
  993. {
  994. struct kvm_memory_slot *slot;
  995. gfn = unalias_gfn(kvm, gfn);
  996. slot = gfn_to_memslot_unaliased(kvm, gfn);
  997. if (!slot)
  998. return bad_hva();
  999. return (slot->userspace_addr + (gfn - slot->base_gfn) * PAGE_SIZE);
  1000. }
  1001. EXPORT_SYMBOL_GPL(gfn_to_hva);
  1002. pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
  1003. {
  1004. struct page *page[1];
  1005. unsigned long addr;
  1006. int npages;
  1007. pfn_t pfn;
  1008. might_sleep();
  1009. addr = gfn_to_hva(kvm, gfn);
  1010. if (kvm_is_error_hva(addr)) {
  1011. get_page(bad_page);
  1012. return page_to_pfn(bad_page);
  1013. }
  1014. npages = get_user_pages_fast(addr, 1, 1, page);
  1015. if (unlikely(npages != 1)) {
  1016. struct vm_area_struct *vma;
  1017. down_read(&current->mm->mmap_sem);
  1018. vma = find_vma(current->mm, addr);
  1019. if (vma == NULL || addr < vma->vm_start ||
  1020. !(vma->vm_flags & VM_PFNMAP)) {
  1021. up_read(&current->mm->mmap_sem);
  1022. get_page(bad_page);
  1023. return page_to_pfn(bad_page);
  1024. }
  1025. pfn = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  1026. up_read(&current->mm->mmap_sem);
  1027. BUG_ON(!kvm_is_mmio_pfn(pfn));
  1028. } else
  1029. pfn = page_to_pfn(page[0]);
  1030. return pfn;
  1031. }
  1032. EXPORT_SYMBOL_GPL(gfn_to_pfn);
  1033. struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
  1034. {
  1035. pfn_t pfn;
  1036. pfn = gfn_to_pfn(kvm, gfn);
  1037. if (!kvm_is_mmio_pfn(pfn))
  1038. return pfn_to_page(pfn);
  1039. WARN_ON(kvm_is_mmio_pfn(pfn));
  1040. get_page(bad_page);
  1041. return bad_page;
  1042. }
  1043. EXPORT_SYMBOL_GPL(gfn_to_page);
  1044. void kvm_release_page_clean(struct page *page)
  1045. {
  1046. kvm_release_pfn_clean(page_to_pfn(page));
  1047. }
  1048. EXPORT_SYMBOL_GPL(kvm_release_page_clean);
  1049. void kvm_release_pfn_clean(pfn_t pfn)
  1050. {
  1051. if (!kvm_is_mmio_pfn(pfn))
  1052. put_page(pfn_to_page(pfn));
  1053. }
  1054. EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
  1055. void kvm_release_page_dirty(struct page *page)
  1056. {
  1057. kvm_release_pfn_dirty(page_to_pfn(page));
  1058. }
  1059. EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
  1060. void kvm_release_pfn_dirty(pfn_t pfn)
  1061. {
  1062. kvm_set_pfn_dirty(pfn);
  1063. kvm_release_pfn_clean(pfn);
  1064. }
  1065. EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
  1066. void kvm_set_page_dirty(struct page *page)
  1067. {
  1068. kvm_set_pfn_dirty(page_to_pfn(page));
  1069. }
  1070. EXPORT_SYMBOL_GPL(kvm_set_page_dirty);
  1071. void kvm_set_pfn_dirty(pfn_t pfn)
  1072. {
  1073. if (!kvm_is_mmio_pfn(pfn)) {
  1074. struct page *page = pfn_to_page(pfn);
  1075. if (!PageReserved(page))
  1076. SetPageDirty(page);
  1077. }
  1078. }
  1079. EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
  1080. void kvm_set_pfn_accessed(pfn_t pfn)
  1081. {
  1082. if (!kvm_is_mmio_pfn(pfn))
  1083. mark_page_accessed(pfn_to_page(pfn));
  1084. }
  1085. EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
  1086. void kvm_get_pfn(pfn_t pfn)
  1087. {
  1088. if (!kvm_is_mmio_pfn(pfn))
  1089. get_page(pfn_to_page(pfn));
  1090. }
  1091. EXPORT_SYMBOL_GPL(kvm_get_pfn);
  1092. static int next_segment(unsigned long len, int offset)
  1093. {
  1094. if (len > PAGE_SIZE - offset)
  1095. return PAGE_SIZE - offset;
  1096. else
  1097. return len;
  1098. }
  1099. int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
  1100. int len)
  1101. {
  1102. int r;
  1103. unsigned long addr;
  1104. addr = gfn_to_hva(kvm, gfn);
  1105. if (kvm_is_error_hva(addr))
  1106. return -EFAULT;
  1107. r = copy_from_user(data, (void __user *)addr + offset, len);
  1108. if (r)
  1109. return -EFAULT;
  1110. return 0;
  1111. }
  1112. EXPORT_SYMBOL_GPL(kvm_read_guest_page);
  1113. int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
  1114. {
  1115. gfn_t gfn = gpa >> PAGE_SHIFT;
  1116. int seg;
  1117. int offset = offset_in_page(gpa);
  1118. int ret;
  1119. while ((seg = next_segment(len, offset)) != 0) {
  1120. ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
  1121. if (ret < 0)
  1122. return ret;
  1123. offset = 0;
  1124. len -= seg;
  1125. data += seg;
  1126. ++gfn;
  1127. }
  1128. return 0;
  1129. }
  1130. EXPORT_SYMBOL_GPL(kvm_read_guest);
  1131. int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
  1132. unsigned long len)
  1133. {
  1134. int r;
  1135. unsigned long addr;
  1136. gfn_t gfn = gpa >> PAGE_SHIFT;
  1137. int offset = offset_in_page(gpa);
  1138. addr = gfn_to_hva(kvm, gfn);
  1139. if (kvm_is_error_hva(addr))
  1140. return -EFAULT;
  1141. pagefault_disable();
  1142. r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
  1143. pagefault_enable();
  1144. if (r)
  1145. return -EFAULT;
  1146. return 0;
  1147. }
  1148. EXPORT_SYMBOL(kvm_read_guest_atomic);
  1149. int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data,
  1150. int offset, int len)
  1151. {
  1152. int r;
  1153. unsigned long addr;
  1154. addr = gfn_to_hva(kvm, gfn);
  1155. if (kvm_is_error_hva(addr))
  1156. return -EFAULT;
  1157. r = copy_to_user((void __user *)addr + offset, data, len);
  1158. if (r)
  1159. return -EFAULT;
  1160. mark_page_dirty(kvm, gfn);
  1161. return 0;
  1162. }
  1163. EXPORT_SYMBOL_GPL(kvm_write_guest_page);
  1164. int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
  1165. unsigned long len)
  1166. {
  1167. gfn_t gfn = gpa >> PAGE_SHIFT;
  1168. int seg;
  1169. int offset = offset_in_page(gpa);
  1170. int ret;
  1171. while ((seg = next_segment(len, offset)) != 0) {
  1172. ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
  1173. if (ret < 0)
  1174. return ret;
  1175. offset = 0;
  1176. len -= seg;
  1177. data += seg;
  1178. ++gfn;
  1179. }
  1180. return 0;
  1181. }
  1182. int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
  1183. {
  1184. return kvm_write_guest_page(kvm, gfn, empty_zero_page, offset, len);
  1185. }
  1186. EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
  1187. int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
  1188. {
  1189. gfn_t gfn = gpa >> PAGE_SHIFT;
  1190. int seg;
  1191. int offset = offset_in_page(gpa);
  1192. int ret;
  1193. while ((seg = next_segment(len, offset)) != 0) {
  1194. ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
  1195. if (ret < 0)
  1196. return ret;
  1197. offset = 0;
  1198. len -= seg;
  1199. ++gfn;
  1200. }
  1201. return 0;
  1202. }
  1203. EXPORT_SYMBOL_GPL(kvm_clear_guest);
  1204. void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
  1205. {
  1206. struct kvm_memory_slot *memslot;
  1207. gfn = unalias_gfn(kvm, gfn);
  1208. memslot = gfn_to_memslot_unaliased(kvm, gfn);
  1209. if (memslot && memslot->dirty_bitmap) {
  1210. unsigned long rel_gfn = gfn - memslot->base_gfn;
  1211. /* avoid RMW */
  1212. if (!test_bit(rel_gfn, memslot->dirty_bitmap))
  1213. set_bit(rel_gfn, memslot->dirty_bitmap);
  1214. }
  1215. }
  1216. /*
  1217. * The vCPU has executed a HLT instruction with in-kernel mode enabled.
  1218. */
  1219. void kvm_vcpu_block(struct kvm_vcpu *vcpu)
  1220. {
  1221. DEFINE_WAIT(wait);
  1222. for (;;) {
  1223. prepare_to_wait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
  1224. if (kvm_cpu_has_interrupt(vcpu) ||
  1225. kvm_cpu_has_pending_timer(vcpu) ||
  1226. kvm_arch_vcpu_runnable(vcpu)) {
  1227. set_bit(KVM_REQ_UNHALT, &vcpu->requests);
  1228. break;
  1229. }
  1230. if (signal_pending(current))
  1231. break;
  1232. vcpu_put(vcpu);
  1233. schedule();
  1234. vcpu_load(vcpu);
  1235. }
  1236. finish_wait(&vcpu->wq, &wait);
  1237. }
  1238. void kvm_resched(struct kvm_vcpu *vcpu)
  1239. {
  1240. if (!need_resched())
  1241. return;
  1242. cond_resched();
  1243. }
  1244. EXPORT_SYMBOL_GPL(kvm_resched);
  1245. static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1246. {
  1247. struct kvm_vcpu *vcpu = vma->vm_file->private_data;
  1248. struct page *page;
  1249. if (vmf->pgoff == 0)
  1250. page = virt_to_page(vcpu->run);
  1251. #ifdef CONFIG_X86
  1252. else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
  1253. page = virt_to_page(vcpu->arch.pio_data);
  1254. #endif
  1255. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  1256. else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
  1257. page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
  1258. #endif
  1259. else
  1260. return VM_FAULT_SIGBUS;
  1261. get_page(page);
  1262. vmf->page = page;
  1263. return 0;
  1264. }
  1265. static struct vm_operations_struct kvm_vcpu_vm_ops = {
  1266. .fault = kvm_vcpu_fault,
  1267. };
  1268. static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
  1269. {
  1270. vma->vm_ops = &kvm_vcpu_vm_ops;
  1271. return 0;
  1272. }
  1273. static int kvm_vcpu_release(struct inode *inode, struct file *filp)
  1274. {
  1275. struct kvm_vcpu *vcpu = filp->private_data;
  1276. kvm_put_kvm(vcpu->kvm);
  1277. return 0;
  1278. }
  1279. static struct file_operations kvm_vcpu_fops = {
  1280. .release = kvm_vcpu_release,
  1281. .unlocked_ioctl = kvm_vcpu_ioctl,
  1282. .compat_ioctl = kvm_vcpu_ioctl,
  1283. .mmap = kvm_vcpu_mmap,
  1284. };
  1285. /*
  1286. * Allocates an inode for the vcpu.
  1287. */
  1288. static int create_vcpu_fd(struct kvm_vcpu *vcpu)
  1289. {
  1290. int fd = anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, 0);
  1291. if (fd < 0)
  1292. kvm_put_kvm(vcpu->kvm);
  1293. return fd;
  1294. }
  1295. /*
  1296. * Creates some virtual cpus. Good luck creating more than one.
  1297. */
  1298. static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
  1299. {
  1300. int r;
  1301. struct kvm_vcpu *vcpu;
  1302. if (!valid_vcpu(n))
  1303. return -EINVAL;
  1304. vcpu = kvm_arch_vcpu_create(kvm, n);
  1305. if (IS_ERR(vcpu))
  1306. return PTR_ERR(vcpu);
  1307. preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
  1308. r = kvm_arch_vcpu_setup(vcpu);
  1309. if (r)
  1310. return r;
  1311. mutex_lock(&kvm->lock);
  1312. if (kvm->vcpus[n]) {
  1313. r = -EEXIST;
  1314. goto vcpu_destroy;
  1315. }
  1316. kvm->vcpus[n] = vcpu;
  1317. mutex_unlock(&kvm->lock);
  1318. /* Now it's all set up, let userspace reach it */
  1319. kvm_get_kvm(kvm);
  1320. r = create_vcpu_fd(vcpu);
  1321. if (r < 0)
  1322. goto unlink;
  1323. return r;
  1324. unlink:
  1325. mutex_lock(&kvm->lock);
  1326. kvm->vcpus[n] = NULL;
  1327. vcpu_destroy:
  1328. mutex_unlock(&kvm->lock);
  1329. kvm_arch_vcpu_destroy(vcpu);
  1330. return r;
  1331. }
  1332. static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
  1333. {
  1334. if (sigset) {
  1335. sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1336. vcpu->sigset_active = 1;
  1337. vcpu->sigset = *sigset;
  1338. } else
  1339. vcpu->sigset_active = 0;
  1340. return 0;
  1341. }
  1342. static long kvm_vcpu_ioctl(struct file *filp,
  1343. unsigned int ioctl, unsigned long arg)
  1344. {
  1345. struct kvm_vcpu *vcpu = filp->private_data;
  1346. void __user *argp = (void __user *)arg;
  1347. int r;
  1348. struct kvm_fpu *fpu = NULL;
  1349. struct kvm_sregs *kvm_sregs = NULL;
  1350. if (vcpu->kvm->mm != current->mm)
  1351. return -EIO;
  1352. switch (ioctl) {
  1353. case KVM_RUN:
  1354. r = -EINVAL;
  1355. if (arg)
  1356. goto out;
  1357. r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
  1358. break;
  1359. case KVM_GET_REGS: {
  1360. struct kvm_regs *kvm_regs;
  1361. r = -ENOMEM;
  1362. kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
  1363. if (!kvm_regs)
  1364. goto out;
  1365. r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
  1366. if (r)
  1367. goto out_free1;
  1368. r = -EFAULT;
  1369. if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
  1370. goto out_free1;
  1371. r = 0;
  1372. out_free1:
  1373. kfree(kvm_regs);
  1374. break;
  1375. }
  1376. case KVM_SET_REGS: {
  1377. struct kvm_regs *kvm_regs;
  1378. r = -ENOMEM;
  1379. kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
  1380. if (!kvm_regs)
  1381. goto out;
  1382. r = -EFAULT;
  1383. if (copy_from_user(kvm_regs, argp, sizeof(struct kvm_regs)))
  1384. goto out_free2;
  1385. r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
  1386. if (r)
  1387. goto out_free2;
  1388. r = 0;
  1389. out_free2:
  1390. kfree(kvm_regs);
  1391. break;
  1392. }
  1393. case KVM_GET_SREGS: {
  1394. kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
  1395. r = -ENOMEM;
  1396. if (!kvm_sregs)
  1397. goto out;
  1398. r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
  1399. if (r)
  1400. goto out;
  1401. r = -EFAULT;
  1402. if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
  1403. goto out;
  1404. r = 0;
  1405. break;
  1406. }
  1407. case KVM_SET_SREGS: {
  1408. kvm_sregs = kmalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
  1409. r = -ENOMEM;
  1410. if (!kvm_sregs)
  1411. goto out;
  1412. r = -EFAULT;
  1413. if (copy_from_user(kvm_sregs, argp, sizeof(struct kvm_sregs)))
  1414. goto out;
  1415. r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
  1416. if (r)
  1417. goto out;
  1418. r = 0;
  1419. break;
  1420. }
  1421. case KVM_GET_MP_STATE: {
  1422. struct kvm_mp_state mp_state;
  1423. r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
  1424. if (r)
  1425. goto out;
  1426. r = -EFAULT;
  1427. if (copy_to_user(argp, &mp_state, sizeof mp_state))
  1428. goto out;
  1429. r = 0;
  1430. break;
  1431. }
  1432. case KVM_SET_MP_STATE: {
  1433. struct kvm_mp_state mp_state;
  1434. r = -EFAULT;
  1435. if (copy_from_user(&mp_state, argp, sizeof mp_state))
  1436. goto out;
  1437. r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
  1438. if (r)
  1439. goto out;
  1440. r = 0;
  1441. break;
  1442. }
  1443. case KVM_TRANSLATE: {
  1444. struct kvm_translation tr;
  1445. r = -EFAULT;
  1446. if (copy_from_user(&tr, argp, sizeof tr))
  1447. goto out;
  1448. r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
  1449. if (r)
  1450. goto out;
  1451. r = -EFAULT;
  1452. if (copy_to_user(argp, &tr, sizeof tr))
  1453. goto out;
  1454. r = 0;
  1455. break;
  1456. }
  1457. case KVM_SET_GUEST_DEBUG: {
  1458. struct kvm_guest_debug dbg;
  1459. r = -EFAULT;
  1460. if (copy_from_user(&dbg, argp, sizeof dbg))
  1461. goto out;
  1462. r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
  1463. if (r)
  1464. goto out;
  1465. r = 0;
  1466. break;
  1467. }
  1468. case KVM_SET_SIGNAL_MASK: {
  1469. struct kvm_signal_mask __user *sigmask_arg = argp;
  1470. struct kvm_signal_mask kvm_sigmask;
  1471. sigset_t sigset, *p;
  1472. p = NULL;
  1473. if (argp) {
  1474. r = -EFAULT;
  1475. if (copy_from_user(&kvm_sigmask, argp,
  1476. sizeof kvm_sigmask))
  1477. goto out;
  1478. r = -EINVAL;
  1479. if (kvm_sigmask.len != sizeof sigset)
  1480. goto out;
  1481. r = -EFAULT;
  1482. if (copy_from_user(&sigset, sigmask_arg->sigset,
  1483. sizeof sigset))
  1484. goto out;
  1485. p = &sigset;
  1486. }
  1487. r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
  1488. break;
  1489. }
  1490. case KVM_GET_FPU: {
  1491. fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
  1492. r = -ENOMEM;
  1493. if (!fpu)
  1494. goto out;
  1495. r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
  1496. if (r)
  1497. goto out;
  1498. r = -EFAULT;
  1499. if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
  1500. goto out;
  1501. r = 0;
  1502. break;
  1503. }
  1504. case KVM_SET_FPU: {
  1505. fpu = kmalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
  1506. r = -ENOMEM;
  1507. if (!fpu)
  1508. goto out;
  1509. r = -EFAULT;
  1510. if (copy_from_user(fpu, argp, sizeof(struct kvm_fpu)))
  1511. goto out;
  1512. r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
  1513. if (r)
  1514. goto out;
  1515. r = 0;
  1516. break;
  1517. }
  1518. default:
  1519. r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
  1520. }
  1521. out:
  1522. kfree(fpu);
  1523. kfree(kvm_sregs);
  1524. return r;
  1525. }
  1526. static long kvm_vm_ioctl(struct file *filp,
  1527. unsigned int ioctl, unsigned long arg)
  1528. {
  1529. struct kvm *kvm = filp->private_data;
  1530. void __user *argp = (void __user *)arg;
  1531. int r;
  1532. if (kvm->mm != current->mm)
  1533. return -EIO;
  1534. switch (ioctl) {
  1535. case KVM_CREATE_VCPU:
  1536. r = kvm_vm_ioctl_create_vcpu(kvm, arg);
  1537. if (r < 0)
  1538. goto out;
  1539. break;
  1540. case KVM_SET_USER_MEMORY_REGION: {
  1541. struct kvm_userspace_memory_region kvm_userspace_mem;
  1542. r = -EFAULT;
  1543. if (copy_from_user(&kvm_userspace_mem, argp,
  1544. sizeof kvm_userspace_mem))
  1545. goto out;
  1546. r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 1);
  1547. if (r)
  1548. goto out;
  1549. break;
  1550. }
  1551. case KVM_GET_DIRTY_LOG: {
  1552. struct kvm_dirty_log log;
  1553. r = -EFAULT;
  1554. if (copy_from_user(&log, argp, sizeof log))
  1555. goto out;
  1556. r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
  1557. if (r)
  1558. goto out;
  1559. break;
  1560. }
  1561. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  1562. case KVM_REGISTER_COALESCED_MMIO: {
  1563. struct kvm_coalesced_mmio_zone zone;
  1564. r = -EFAULT;
  1565. if (copy_from_user(&zone, argp, sizeof zone))
  1566. goto out;
  1567. r = -ENXIO;
  1568. r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
  1569. if (r)
  1570. goto out;
  1571. r = 0;
  1572. break;
  1573. }
  1574. case KVM_UNREGISTER_COALESCED_MMIO: {
  1575. struct kvm_coalesced_mmio_zone zone;
  1576. r = -EFAULT;
  1577. if (copy_from_user(&zone, argp, sizeof zone))
  1578. goto out;
  1579. r = -ENXIO;
  1580. r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
  1581. if (r)
  1582. goto out;
  1583. r = 0;
  1584. break;
  1585. }
  1586. #endif
  1587. #ifdef KVM_CAP_DEVICE_ASSIGNMENT
  1588. case KVM_ASSIGN_PCI_DEVICE: {
  1589. struct kvm_assigned_pci_dev assigned_dev;
  1590. r = -EFAULT;
  1591. if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
  1592. goto out;
  1593. r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
  1594. if (r)
  1595. goto out;
  1596. break;
  1597. }
  1598. case KVM_ASSIGN_IRQ: {
  1599. struct kvm_assigned_irq assigned_irq;
  1600. r = -EFAULT;
  1601. if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
  1602. goto out;
  1603. r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
  1604. if (r)
  1605. goto out;
  1606. break;
  1607. }
  1608. #endif
  1609. #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
  1610. case KVM_DEASSIGN_PCI_DEVICE: {
  1611. struct kvm_assigned_pci_dev assigned_dev;
  1612. r = -EFAULT;
  1613. if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
  1614. goto out;
  1615. r = kvm_vm_ioctl_deassign_device(kvm, &assigned_dev);
  1616. if (r)
  1617. goto out;
  1618. break;
  1619. }
  1620. #endif
  1621. #ifdef KVM_CAP_IRQ_ROUTING
  1622. case KVM_SET_GSI_ROUTING: {
  1623. struct kvm_irq_routing routing;
  1624. struct kvm_irq_routing __user *urouting;
  1625. struct kvm_irq_routing_entry *entries;
  1626. r = -EFAULT;
  1627. if (copy_from_user(&routing, argp, sizeof(routing)))
  1628. goto out;
  1629. r = -EINVAL;
  1630. if (routing.nr >= KVM_MAX_IRQ_ROUTES)
  1631. goto out;
  1632. if (routing.flags)
  1633. goto out;
  1634. r = -ENOMEM;
  1635. entries = vmalloc(routing.nr * sizeof(*entries));
  1636. if (!entries)
  1637. goto out;
  1638. r = -EFAULT;
  1639. urouting = argp;
  1640. if (copy_from_user(entries, urouting->entries,
  1641. routing.nr * sizeof(*entries)))
  1642. goto out_free_irq_routing;
  1643. r = kvm_set_irq_routing(kvm, entries, routing.nr,
  1644. routing.flags);
  1645. out_free_irq_routing:
  1646. vfree(entries);
  1647. break;
  1648. }
  1649. #endif
  1650. default:
  1651. r = kvm_arch_vm_ioctl(filp, ioctl, arg);
  1652. }
  1653. out:
  1654. return r;
  1655. }
  1656. static int kvm_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1657. {
  1658. struct page *page[1];
  1659. unsigned long addr;
  1660. int npages;
  1661. gfn_t gfn = vmf->pgoff;
  1662. struct kvm *kvm = vma->vm_file->private_data;
  1663. addr = gfn_to_hva(kvm, gfn);
  1664. if (kvm_is_error_hva(addr))
  1665. return VM_FAULT_SIGBUS;
  1666. npages = get_user_pages(current, current->mm, addr, 1, 1, 0, page,
  1667. NULL);
  1668. if (unlikely(npages != 1))
  1669. return VM_FAULT_SIGBUS;
  1670. vmf->page = page[0];
  1671. return 0;
  1672. }
  1673. static struct vm_operations_struct kvm_vm_vm_ops = {
  1674. .fault = kvm_vm_fault,
  1675. };
  1676. static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
  1677. {
  1678. vma->vm_ops = &kvm_vm_vm_ops;
  1679. return 0;
  1680. }
  1681. static struct file_operations kvm_vm_fops = {
  1682. .release = kvm_vm_release,
  1683. .unlocked_ioctl = kvm_vm_ioctl,
  1684. .compat_ioctl = kvm_vm_ioctl,
  1685. .mmap = kvm_vm_mmap,
  1686. };
  1687. static int kvm_dev_ioctl_create_vm(void)
  1688. {
  1689. int fd;
  1690. struct kvm *kvm;
  1691. kvm = kvm_create_vm();
  1692. if (IS_ERR(kvm))
  1693. return PTR_ERR(kvm);
  1694. fd = anon_inode_getfd("kvm-vm", &kvm_vm_fops, kvm, 0);
  1695. if (fd < 0)
  1696. kvm_put_kvm(kvm);
  1697. return fd;
  1698. }
  1699. static long kvm_dev_ioctl_check_extension_generic(long arg)
  1700. {
  1701. switch (arg) {
  1702. case KVM_CAP_USER_MEMORY:
  1703. case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
  1704. case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
  1705. return 1;
  1706. #ifdef CONFIG_HAVE_KVM_IRQCHIP
  1707. case KVM_CAP_IRQ_ROUTING:
  1708. return KVM_MAX_IRQ_ROUTES;
  1709. #endif
  1710. default:
  1711. break;
  1712. }
  1713. return kvm_dev_ioctl_check_extension(arg);
  1714. }
  1715. static long kvm_dev_ioctl(struct file *filp,
  1716. unsigned int ioctl, unsigned long arg)
  1717. {
  1718. long r = -EINVAL;
  1719. switch (ioctl) {
  1720. case KVM_GET_API_VERSION:
  1721. r = -EINVAL;
  1722. if (arg)
  1723. goto out;
  1724. r = KVM_API_VERSION;
  1725. break;
  1726. case KVM_CREATE_VM:
  1727. r = -EINVAL;
  1728. if (arg)
  1729. goto out;
  1730. r = kvm_dev_ioctl_create_vm();
  1731. break;
  1732. case KVM_CHECK_EXTENSION:
  1733. r = kvm_dev_ioctl_check_extension_generic(arg);
  1734. break;
  1735. case KVM_GET_VCPU_MMAP_SIZE:
  1736. r = -EINVAL;
  1737. if (arg)
  1738. goto out;
  1739. r = PAGE_SIZE; /* struct kvm_run */
  1740. #ifdef CONFIG_X86
  1741. r += PAGE_SIZE; /* pio data page */
  1742. #endif
  1743. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  1744. r += PAGE_SIZE; /* coalesced mmio ring page */
  1745. #endif
  1746. break;
  1747. case KVM_TRACE_ENABLE:
  1748. case KVM_TRACE_PAUSE:
  1749. case KVM_TRACE_DISABLE:
  1750. r = kvm_trace_ioctl(ioctl, arg);
  1751. break;
  1752. default:
  1753. return kvm_arch_dev_ioctl(filp, ioctl, arg);
  1754. }
  1755. out:
  1756. return r;
  1757. }
  1758. static struct file_operations kvm_chardev_ops = {
  1759. .unlocked_ioctl = kvm_dev_ioctl,
  1760. .compat_ioctl = kvm_dev_ioctl,
  1761. };
  1762. static struct miscdevice kvm_dev = {
  1763. KVM_MINOR,
  1764. "kvm",
  1765. &kvm_chardev_ops,
  1766. };
  1767. static void hardware_enable(void *junk)
  1768. {
  1769. int cpu = raw_smp_processor_id();
  1770. if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
  1771. return;
  1772. cpumask_set_cpu(cpu, cpus_hardware_enabled);
  1773. kvm_arch_hardware_enable(NULL);
  1774. }
  1775. static void hardware_disable(void *junk)
  1776. {
  1777. int cpu = raw_smp_processor_id();
  1778. if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
  1779. return;
  1780. cpumask_clear_cpu(cpu, cpus_hardware_enabled);
  1781. kvm_arch_hardware_disable(NULL);
  1782. }
  1783. static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
  1784. void *v)
  1785. {
  1786. int cpu = (long)v;
  1787. val &= ~CPU_TASKS_FROZEN;
  1788. switch (val) {
  1789. case CPU_DYING:
  1790. printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
  1791. cpu);
  1792. hardware_disable(NULL);
  1793. break;
  1794. case CPU_UP_CANCELED:
  1795. printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
  1796. cpu);
  1797. smp_call_function_single(cpu, hardware_disable, NULL, 1);
  1798. break;
  1799. case CPU_ONLINE:
  1800. printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
  1801. cpu);
  1802. smp_call_function_single(cpu, hardware_enable, NULL, 1);
  1803. break;
  1804. }
  1805. return NOTIFY_OK;
  1806. }
  1807. asmlinkage void kvm_handle_fault_on_reboot(void)
  1808. {
  1809. if (kvm_rebooting)
  1810. /* spin while reset goes on */
  1811. while (true)
  1812. ;
  1813. /* Fault while not rebooting. We want the trace. */
  1814. BUG();
  1815. }
  1816. EXPORT_SYMBOL_GPL(kvm_handle_fault_on_reboot);
  1817. static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
  1818. void *v)
  1819. {
  1820. if (val == SYS_RESTART) {
  1821. /*
  1822. * Some (well, at least mine) BIOSes hang on reboot if
  1823. * in vmx root mode.
  1824. */
  1825. printk(KERN_INFO "kvm: exiting hardware virtualization\n");
  1826. kvm_rebooting = true;
  1827. on_each_cpu(hardware_disable, NULL, 1);
  1828. }
  1829. return NOTIFY_OK;
  1830. }
  1831. static struct notifier_block kvm_reboot_notifier = {
  1832. .notifier_call = kvm_reboot,
  1833. .priority = 0,
  1834. };
  1835. void kvm_io_bus_init(struct kvm_io_bus *bus)
  1836. {
  1837. memset(bus, 0, sizeof(*bus));
  1838. }
  1839. void kvm_io_bus_destroy(struct kvm_io_bus *bus)
  1840. {
  1841. int i;
  1842. for (i = 0; i < bus->dev_count; i++) {
  1843. struct kvm_io_device *pos = bus->devs[i];
  1844. kvm_iodevice_destructor(pos);
  1845. }
  1846. }
  1847. struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus,
  1848. gpa_t addr, int len, int is_write)
  1849. {
  1850. int i;
  1851. for (i = 0; i < bus->dev_count; i++) {
  1852. struct kvm_io_device *pos = bus->devs[i];
  1853. if (pos->in_range(pos, addr, len, is_write))
  1854. return pos;
  1855. }
  1856. return NULL;
  1857. }
  1858. void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
  1859. {
  1860. BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
  1861. bus->devs[bus->dev_count++] = dev;
  1862. }
  1863. static struct notifier_block kvm_cpu_notifier = {
  1864. .notifier_call = kvm_cpu_hotplug,
  1865. .priority = 20, /* must be > scheduler priority */
  1866. };
  1867. static int vm_stat_get(void *_offset, u64 *val)
  1868. {
  1869. unsigned offset = (long)_offset;
  1870. struct kvm *kvm;
  1871. *val = 0;
  1872. spin_lock(&kvm_lock);
  1873. list_for_each_entry(kvm, &vm_list, vm_list)
  1874. *val += *(u32 *)((void *)kvm + offset);
  1875. spin_unlock(&kvm_lock);
  1876. return 0;
  1877. }
  1878. DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, NULL, "%llu\n");
  1879. static int vcpu_stat_get(void *_offset, u64 *val)
  1880. {
  1881. unsigned offset = (long)_offset;
  1882. struct kvm *kvm;
  1883. struct kvm_vcpu *vcpu;
  1884. int i;
  1885. *val = 0;
  1886. spin_lock(&kvm_lock);
  1887. list_for_each_entry(kvm, &vm_list, vm_list)
  1888. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  1889. vcpu = kvm->vcpus[i];
  1890. if (vcpu)
  1891. *val += *(u32 *)((void *)vcpu + offset);
  1892. }
  1893. spin_unlock(&kvm_lock);
  1894. return 0;
  1895. }
  1896. DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, NULL, "%llu\n");
  1897. static struct file_operations *stat_fops[] = {
  1898. [KVM_STAT_VCPU] = &vcpu_stat_fops,
  1899. [KVM_STAT_VM] = &vm_stat_fops,
  1900. };
  1901. static void kvm_init_debug(void)
  1902. {
  1903. struct kvm_stats_debugfs_item *p;
  1904. kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
  1905. for (p = debugfs_entries; p->name; ++p)
  1906. p->dentry = debugfs_create_file(p->name, 0444, kvm_debugfs_dir,
  1907. (void *)(long)p->offset,
  1908. stat_fops[p->kind]);
  1909. }
  1910. static void kvm_exit_debug(void)
  1911. {
  1912. struct kvm_stats_debugfs_item *p;
  1913. for (p = debugfs_entries; p->name; ++p)
  1914. debugfs_remove(p->dentry);
  1915. debugfs_remove(kvm_debugfs_dir);
  1916. }
  1917. static int kvm_suspend(struct sys_device *dev, pm_message_t state)
  1918. {
  1919. hardware_disable(NULL);
  1920. return 0;
  1921. }
  1922. static int kvm_resume(struct sys_device *dev)
  1923. {
  1924. hardware_enable(NULL);
  1925. return 0;
  1926. }
  1927. static struct sysdev_class kvm_sysdev_class = {
  1928. .name = "kvm",
  1929. .suspend = kvm_suspend,
  1930. .resume = kvm_resume,
  1931. };
  1932. static struct sys_device kvm_sysdev = {
  1933. .id = 0,
  1934. .cls = &kvm_sysdev_class,
  1935. };
  1936. struct page *bad_page;
  1937. pfn_t bad_pfn;
  1938. static inline
  1939. struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
  1940. {
  1941. return container_of(pn, struct kvm_vcpu, preempt_notifier);
  1942. }
  1943. static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
  1944. {
  1945. struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
  1946. kvm_arch_vcpu_load(vcpu, cpu);
  1947. }
  1948. static void kvm_sched_out(struct preempt_notifier *pn,
  1949. struct task_struct *next)
  1950. {
  1951. struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
  1952. kvm_arch_vcpu_put(vcpu);
  1953. }
  1954. int kvm_init(void *opaque, unsigned int vcpu_size,
  1955. struct module *module)
  1956. {
  1957. int r;
  1958. int cpu;
  1959. kvm_init_debug();
  1960. r = kvm_arch_init(opaque);
  1961. if (r)
  1962. goto out_fail;
  1963. bad_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  1964. if (bad_page == NULL) {
  1965. r = -ENOMEM;
  1966. goto out;
  1967. }
  1968. bad_pfn = page_to_pfn(bad_page);
  1969. if (!alloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
  1970. r = -ENOMEM;
  1971. goto out_free_0;
  1972. }
  1973. r = kvm_arch_hardware_setup();
  1974. if (r < 0)
  1975. goto out_free_0a;
  1976. for_each_online_cpu(cpu) {
  1977. smp_call_function_single(cpu,
  1978. kvm_arch_check_processor_compat,
  1979. &r, 1);
  1980. if (r < 0)
  1981. goto out_free_1;
  1982. }
  1983. on_each_cpu(hardware_enable, NULL, 1);
  1984. r = register_cpu_notifier(&kvm_cpu_notifier);
  1985. if (r)
  1986. goto out_free_2;
  1987. register_reboot_notifier(&kvm_reboot_notifier);
  1988. r = sysdev_class_register(&kvm_sysdev_class);
  1989. if (r)
  1990. goto out_free_3;
  1991. r = sysdev_register(&kvm_sysdev);
  1992. if (r)
  1993. goto out_free_4;
  1994. /* A kmem cache lets us meet the alignment requirements of fx_save. */
  1995. kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
  1996. __alignof__(struct kvm_vcpu),
  1997. 0, NULL);
  1998. if (!kvm_vcpu_cache) {
  1999. r = -ENOMEM;
  2000. goto out_free_5;
  2001. }
  2002. kvm_chardev_ops.owner = module;
  2003. kvm_vm_fops.owner = module;
  2004. kvm_vcpu_fops.owner = module;
  2005. r = misc_register(&kvm_dev);
  2006. if (r) {
  2007. printk(KERN_ERR "kvm: misc device register failed\n");
  2008. goto out_free;
  2009. }
  2010. kvm_preempt_ops.sched_in = kvm_sched_in;
  2011. kvm_preempt_ops.sched_out = kvm_sched_out;
  2012. #ifndef CONFIG_X86
  2013. msi2intx = 0;
  2014. #endif
  2015. return 0;
  2016. out_free:
  2017. kmem_cache_destroy(kvm_vcpu_cache);
  2018. out_free_5:
  2019. sysdev_unregister(&kvm_sysdev);
  2020. out_free_4:
  2021. sysdev_class_unregister(&kvm_sysdev_class);
  2022. out_free_3:
  2023. unregister_reboot_notifier(&kvm_reboot_notifier);
  2024. unregister_cpu_notifier(&kvm_cpu_notifier);
  2025. out_free_2:
  2026. on_each_cpu(hardware_disable, NULL, 1);
  2027. out_free_1:
  2028. kvm_arch_hardware_unsetup();
  2029. out_free_0a:
  2030. free_cpumask_var(cpus_hardware_enabled);
  2031. out_free_0:
  2032. __free_page(bad_page);
  2033. out:
  2034. kvm_arch_exit();
  2035. kvm_exit_debug();
  2036. out_fail:
  2037. return r;
  2038. }
  2039. EXPORT_SYMBOL_GPL(kvm_init);
  2040. void kvm_exit(void)
  2041. {
  2042. kvm_trace_cleanup();
  2043. misc_deregister(&kvm_dev);
  2044. kmem_cache_destroy(kvm_vcpu_cache);
  2045. sysdev_unregister(&kvm_sysdev);
  2046. sysdev_class_unregister(&kvm_sysdev_class);
  2047. unregister_reboot_notifier(&kvm_reboot_notifier);
  2048. unregister_cpu_notifier(&kvm_cpu_notifier);
  2049. on_each_cpu(hardware_disable, NULL, 1);
  2050. kvm_arch_hardware_unsetup();
  2051. kvm_arch_exit();
  2052. kvm_exit_debug();
  2053. free_cpumask_var(cpus_hardware_enabled);
  2054. __free_page(bad_page);
  2055. }
  2056. EXPORT_SYMBOL_GPL(kvm_exit);