kvm_main.c 53 KB

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