kvm_main.c 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033
  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. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  9. *
  10. * Authors:
  11. * Avi Kivity <avi@qumranet.com>
  12. * Yaniv Kamay <yaniv@qumranet.com>
  13. *
  14. * This work is licensed under the terms of the GNU GPL, version 2. See
  15. * the COPYING file in the top-level directory.
  16. *
  17. */
  18. #include <kvm/iodev.h>
  19. #include <linux/kvm_host.h>
  20. #include <linux/kvm.h>
  21. #include <linux/module.h>
  22. #include <linux/errno.h>
  23. #include <linux/percpu.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/syscore_ops.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 <linux/bitops.h>
  43. #include <linux/spinlock.h>
  44. #include <linux/compat.h>
  45. #include <linux/srcu.h>
  46. #include <linux/hugetlb.h>
  47. #include <linux/slab.h>
  48. #include <linux/sort.h>
  49. #include <linux/bsearch.h>
  50. #include <asm/processor.h>
  51. #include <asm/io.h>
  52. #include <asm/ioctl.h>
  53. #include <asm/uaccess.h>
  54. #include <asm/pgtable.h>
  55. #include "coalesced_mmio.h"
  56. #include "async_pf.h"
  57. #include "vfio.h"
  58. #define CREATE_TRACE_POINTS
  59. #include <trace/events/kvm.h>
  60. /* Worst case buffer size needed for holding an integer. */
  61. #define ITOA_MAX_LEN 12
  62. MODULE_AUTHOR("Qumranet");
  63. MODULE_LICENSE("GPL");
  64. /* Architectures should define their poll value according to the halt latency */
  65. unsigned int halt_poll_ns = KVM_HALT_POLL_NS_DEFAULT;
  66. module_param(halt_poll_ns, uint, S_IRUGO | S_IWUSR);
  67. EXPORT_SYMBOL_GPL(halt_poll_ns);
  68. /* Default doubles per-vcpu halt_poll_ns. */
  69. unsigned int halt_poll_ns_grow = 2;
  70. module_param(halt_poll_ns_grow, uint, S_IRUGO | S_IWUSR);
  71. EXPORT_SYMBOL_GPL(halt_poll_ns_grow);
  72. /* Default resets per-vcpu halt_poll_ns . */
  73. unsigned int halt_poll_ns_shrink;
  74. module_param(halt_poll_ns_shrink, uint, S_IRUGO | S_IWUSR);
  75. EXPORT_SYMBOL_GPL(halt_poll_ns_shrink);
  76. /*
  77. * Ordering of locks:
  78. *
  79. * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
  80. */
  81. DEFINE_SPINLOCK(kvm_lock);
  82. static DEFINE_RAW_SPINLOCK(kvm_count_lock);
  83. LIST_HEAD(vm_list);
  84. static cpumask_var_t cpus_hardware_enabled;
  85. static int kvm_usage_count;
  86. static atomic_t hardware_enable_failed;
  87. struct kmem_cache *kvm_vcpu_cache;
  88. EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
  89. static __read_mostly struct preempt_ops kvm_preempt_ops;
  90. struct dentry *kvm_debugfs_dir;
  91. EXPORT_SYMBOL_GPL(kvm_debugfs_dir);
  92. static int kvm_debugfs_num_entries;
  93. static const struct file_operations *stat_fops_per_vm[];
  94. static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
  95. unsigned long arg);
  96. #ifdef CONFIG_KVM_COMPAT
  97. static long kvm_vcpu_compat_ioctl(struct file *file, unsigned int ioctl,
  98. unsigned long arg);
  99. #endif
  100. static int hardware_enable_all(void);
  101. static void hardware_disable_all(void);
  102. static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
  103. static void kvm_release_pfn_dirty(kvm_pfn_t pfn);
  104. static void mark_page_dirty_in_slot(struct kvm_memory_slot *memslot, gfn_t gfn);
  105. __visible bool kvm_rebooting;
  106. EXPORT_SYMBOL_GPL(kvm_rebooting);
  107. static bool largepages_enabled = true;
  108. bool kvm_is_reserved_pfn(kvm_pfn_t pfn)
  109. {
  110. if (pfn_valid(pfn))
  111. return PageReserved(pfn_to_page(pfn));
  112. return true;
  113. }
  114. /*
  115. * Switches to specified vcpu, until a matching vcpu_put()
  116. */
  117. int vcpu_load(struct kvm_vcpu *vcpu)
  118. {
  119. int cpu;
  120. if (mutex_lock_killable(&vcpu->mutex))
  121. return -EINTR;
  122. cpu = get_cpu();
  123. preempt_notifier_register(&vcpu->preempt_notifier);
  124. kvm_arch_vcpu_load(vcpu, cpu);
  125. put_cpu();
  126. return 0;
  127. }
  128. EXPORT_SYMBOL_GPL(vcpu_load);
  129. void vcpu_put(struct kvm_vcpu *vcpu)
  130. {
  131. preempt_disable();
  132. kvm_arch_vcpu_put(vcpu);
  133. preempt_notifier_unregister(&vcpu->preempt_notifier);
  134. preempt_enable();
  135. mutex_unlock(&vcpu->mutex);
  136. }
  137. EXPORT_SYMBOL_GPL(vcpu_put);
  138. static void ack_flush(void *_completed)
  139. {
  140. }
  141. bool kvm_make_all_cpus_request(struct kvm *kvm, unsigned int req)
  142. {
  143. int i, cpu, me;
  144. cpumask_var_t cpus;
  145. bool called = true;
  146. struct kvm_vcpu *vcpu;
  147. zalloc_cpumask_var(&cpus, GFP_ATOMIC);
  148. me = get_cpu();
  149. kvm_for_each_vcpu(i, vcpu, kvm) {
  150. kvm_make_request(req, vcpu);
  151. cpu = vcpu->cpu;
  152. /* Set ->requests bit before we read ->mode. */
  153. smp_mb__after_atomic();
  154. if (cpus != NULL && cpu != -1 && cpu != me &&
  155. kvm_vcpu_exiting_guest_mode(vcpu) != OUTSIDE_GUEST_MODE)
  156. cpumask_set_cpu(cpu, cpus);
  157. }
  158. if (unlikely(cpus == NULL))
  159. smp_call_function_many(cpu_online_mask, ack_flush, NULL, 1);
  160. else if (!cpumask_empty(cpus))
  161. smp_call_function_many(cpus, ack_flush, NULL, 1);
  162. else
  163. called = false;
  164. put_cpu();
  165. free_cpumask_var(cpus);
  166. return called;
  167. }
  168. #ifndef CONFIG_HAVE_KVM_ARCH_TLB_FLUSH_ALL
  169. void kvm_flush_remote_tlbs(struct kvm *kvm)
  170. {
  171. /*
  172. * Read tlbs_dirty before setting KVM_REQ_TLB_FLUSH in
  173. * kvm_make_all_cpus_request.
  174. */
  175. long dirty_count = smp_load_acquire(&kvm->tlbs_dirty);
  176. /*
  177. * We want to publish modifications to the page tables before reading
  178. * mode. Pairs with a memory barrier in arch-specific code.
  179. * - x86: smp_mb__after_srcu_read_unlock in vcpu_enter_guest
  180. * and smp_mb in walk_shadow_page_lockless_begin/end.
  181. * - powerpc: smp_mb in kvmppc_prepare_to_enter.
  182. *
  183. * There is already an smp_mb__after_atomic() before
  184. * kvm_make_all_cpus_request() reads vcpu->mode. We reuse that
  185. * barrier here.
  186. */
  187. if (kvm_make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
  188. ++kvm->stat.remote_tlb_flush;
  189. cmpxchg(&kvm->tlbs_dirty, dirty_count, 0);
  190. }
  191. EXPORT_SYMBOL_GPL(kvm_flush_remote_tlbs);
  192. #endif
  193. void kvm_reload_remote_mmus(struct kvm *kvm)
  194. {
  195. kvm_make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
  196. }
  197. int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
  198. {
  199. struct page *page;
  200. int r;
  201. mutex_init(&vcpu->mutex);
  202. vcpu->cpu = -1;
  203. vcpu->kvm = kvm;
  204. vcpu->vcpu_id = id;
  205. vcpu->pid = NULL;
  206. init_swait_queue_head(&vcpu->wq);
  207. kvm_async_pf_vcpu_init(vcpu);
  208. vcpu->pre_pcpu = -1;
  209. INIT_LIST_HEAD(&vcpu->blocked_vcpu_list);
  210. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  211. if (!page) {
  212. r = -ENOMEM;
  213. goto fail;
  214. }
  215. vcpu->run = page_address(page);
  216. kvm_vcpu_set_in_spin_loop(vcpu, false);
  217. kvm_vcpu_set_dy_eligible(vcpu, false);
  218. vcpu->preempted = false;
  219. r = kvm_arch_vcpu_init(vcpu);
  220. if (r < 0)
  221. goto fail_free_run;
  222. return 0;
  223. fail_free_run:
  224. free_page((unsigned long)vcpu->run);
  225. fail:
  226. return r;
  227. }
  228. EXPORT_SYMBOL_GPL(kvm_vcpu_init);
  229. void kvm_vcpu_uninit(struct kvm_vcpu *vcpu)
  230. {
  231. put_pid(vcpu->pid);
  232. kvm_arch_vcpu_uninit(vcpu);
  233. free_page((unsigned long)vcpu->run);
  234. }
  235. EXPORT_SYMBOL_GPL(kvm_vcpu_uninit);
  236. #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
  237. static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
  238. {
  239. return container_of(mn, struct kvm, mmu_notifier);
  240. }
  241. static void kvm_mmu_notifier_invalidate_page(struct mmu_notifier *mn,
  242. struct mm_struct *mm,
  243. unsigned long address)
  244. {
  245. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  246. int need_tlb_flush, idx;
  247. /*
  248. * When ->invalidate_page runs, the linux pte has been zapped
  249. * already but the page is still allocated until
  250. * ->invalidate_page returns. So if we increase the sequence
  251. * here the kvm page fault will notice if the spte can't be
  252. * established because the page is going to be freed. If
  253. * instead the kvm page fault establishes the spte before
  254. * ->invalidate_page runs, kvm_unmap_hva will release it
  255. * before returning.
  256. *
  257. * The sequence increase only need to be seen at spin_unlock
  258. * time, and not at spin_lock time.
  259. *
  260. * Increasing the sequence after the spin_unlock would be
  261. * unsafe because the kvm page fault could then establish the
  262. * pte after kvm_unmap_hva returned, without noticing the page
  263. * is going to be freed.
  264. */
  265. idx = srcu_read_lock(&kvm->srcu);
  266. spin_lock(&kvm->mmu_lock);
  267. kvm->mmu_notifier_seq++;
  268. need_tlb_flush = kvm_unmap_hva(kvm, address) | kvm->tlbs_dirty;
  269. /* we've to flush the tlb before the pages can be freed */
  270. if (need_tlb_flush)
  271. kvm_flush_remote_tlbs(kvm);
  272. spin_unlock(&kvm->mmu_lock);
  273. kvm_arch_mmu_notifier_invalidate_page(kvm, address);
  274. srcu_read_unlock(&kvm->srcu, idx);
  275. }
  276. static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
  277. struct mm_struct *mm,
  278. unsigned long address,
  279. pte_t pte)
  280. {
  281. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  282. int idx;
  283. idx = srcu_read_lock(&kvm->srcu);
  284. spin_lock(&kvm->mmu_lock);
  285. kvm->mmu_notifier_seq++;
  286. kvm_set_spte_hva(kvm, address, pte);
  287. spin_unlock(&kvm->mmu_lock);
  288. srcu_read_unlock(&kvm->srcu, idx);
  289. }
  290. static void kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
  291. struct mm_struct *mm,
  292. unsigned long start,
  293. unsigned long end)
  294. {
  295. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  296. int need_tlb_flush = 0, idx;
  297. idx = srcu_read_lock(&kvm->srcu);
  298. spin_lock(&kvm->mmu_lock);
  299. /*
  300. * The count increase must become visible at unlock time as no
  301. * spte can be established without taking the mmu_lock and
  302. * count is also read inside the mmu_lock critical section.
  303. */
  304. kvm->mmu_notifier_count++;
  305. need_tlb_flush = kvm_unmap_hva_range(kvm, start, end);
  306. need_tlb_flush |= kvm->tlbs_dirty;
  307. /* we've to flush the tlb before the pages can be freed */
  308. if (need_tlb_flush)
  309. kvm_flush_remote_tlbs(kvm);
  310. spin_unlock(&kvm->mmu_lock);
  311. srcu_read_unlock(&kvm->srcu, idx);
  312. }
  313. static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
  314. struct mm_struct *mm,
  315. unsigned long start,
  316. unsigned long end)
  317. {
  318. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  319. spin_lock(&kvm->mmu_lock);
  320. /*
  321. * This sequence increase will notify the kvm page fault that
  322. * the page that is going to be mapped in the spte could have
  323. * been freed.
  324. */
  325. kvm->mmu_notifier_seq++;
  326. smp_wmb();
  327. /*
  328. * The above sequence increase must be visible before the
  329. * below count decrease, which is ensured by the smp_wmb above
  330. * in conjunction with the smp_rmb in mmu_notifier_retry().
  331. */
  332. kvm->mmu_notifier_count--;
  333. spin_unlock(&kvm->mmu_lock);
  334. BUG_ON(kvm->mmu_notifier_count < 0);
  335. }
  336. static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
  337. struct mm_struct *mm,
  338. unsigned long start,
  339. unsigned long end)
  340. {
  341. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  342. int young, idx;
  343. idx = srcu_read_lock(&kvm->srcu);
  344. spin_lock(&kvm->mmu_lock);
  345. young = kvm_age_hva(kvm, start, end);
  346. if (young)
  347. kvm_flush_remote_tlbs(kvm);
  348. spin_unlock(&kvm->mmu_lock);
  349. srcu_read_unlock(&kvm->srcu, idx);
  350. return young;
  351. }
  352. static int kvm_mmu_notifier_clear_young(struct mmu_notifier *mn,
  353. struct mm_struct *mm,
  354. unsigned long start,
  355. unsigned long end)
  356. {
  357. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  358. int young, idx;
  359. idx = srcu_read_lock(&kvm->srcu);
  360. spin_lock(&kvm->mmu_lock);
  361. /*
  362. * Even though we do not flush TLB, this will still adversely
  363. * affect performance on pre-Haswell Intel EPT, where there is
  364. * no EPT Access Bit to clear so that we have to tear down EPT
  365. * tables instead. If we find this unacceptable, we can always
  366. * add a parameter to kvm_age_hva so that it effectively doesn't
  367. * do anything on clear_young.
  368. *
  369. * Also note that currently we never issue secondary TLB flushes
  370. * from clear_young, leaving this job up to the regular system
  371. * cadence. If we find this inaccurate, we might come up with a
  372. * more sophisticated heuristic later.
  373. */
  374. young = kvm_age_hva(kvm, start, end);
  375. spin_unlock(&kvm->mmu_lock);
  376. srcu_read_unlock(&kvm->srcu, idx);
  377. return young;
  378. }
  379. static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,
  380. struct mm_struct *mm,
  381. unsigned long address)
  382. {
  383. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  384. int young, idx;
  385. idx = srcu_read_lock(&kvm->srcu);
  386. spin_lock(&kvm->mmu_lock);
  387. young = kvm_test_age_hva(kvm, address);
  388. spin_unlock(&kvm->mmu_lock);
  389. srcu_read_unlock(&kvm->srcu, idx);
  390. return young;
  391. }
  392. static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
  393. struct mm_struct *mm)
  394. {
  395. struct kvm *kvm = mmu_notifier_to_kvm(mn);
  396. int idx;
  397. idx = srcu_read_lock(&kvm->srcu);
  398. kvm_arch_flush_shadow_all(kvm);
  399. srcu_read_unlock(&kvm->srcu, idx);
  400. }
  401. static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
  402. .invalidate_page = kvm_mmu_notifier_invalidate_page,
  403. .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
  404. .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
  405. .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
  406. .clear_young = kvm_mmu_notifier_clear_young,
  407. .test_young = kvm_mmu_notifier_test_young,
  408. .change_pte = kvm_mmu_notifier_change_pte,
  409. .release = kvm_mmu_notifier_release,
  410. };
  411. static int kvm_init_mmu_notifier(struct kvm *kvm)
  412. {
  413. kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
  414. return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
  415. }
  416. #else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
  417. static int kvm_init_mmu_notifier(struct kvm *kvm)
  418. {
  419. return 0;
  420. }
  421. #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
  422. static struct kvm_memslots *kvm_alloc_memslots(void)
  423. {
  424. int i;
  425. struct kvm_memslots *slots;
  426. slots = kvm_kvzalloc(sizeof(struct kvm_memslots));
  427. if (!slots)
  428. return NULL;
  429. /*
  430. * Init kvm generation close to the maximum to easily test the
  431. * code of handling generation number wrap-around.
  432. */
  433. slots->generation = -150;
  434. for (i = 0; i < KVM_MEM_SLOTS_NUM; i++)
  435. slots->id_to_index[i] = slots->memslots[i].id = i;
  436. return slots;
  437. }
  438. static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
  439. {
  440. if (!memslot->dirty_bitmap)
  441. return;
  442. kvfree(memslot->dirty_bitmap);
  443. memslot->dirty_bitmap = NULL;
  444. }
  445. /*
  446. * Free any memory in @free but not in @dont.
  447. */
  448. static void kvm_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
  449. struct kvm_memory_slot *dont)
  450. {
  451. if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
  452. kvm_destroy_dirty_bitmap(free);
  453. kvm_arch_free_memslot(kvm, free, dont);
  454. free->npages = 0;
  455. }
  456. static void kvm_free_memslots(struct kvm *kvm, struct kvm_memslots *slots)
  457. {
  458. struct kvm_memory_slot *memslot;
  459. if (!slots)
  460. return;
  461. kvm_for_each_memslot(memslot, slots)
  462. kvm_free_memslot(kvm, memslot, NULL);
  463. kvfree(slots);
  464. }
  465. static void kvm_destroy_vm_debugfs(struct kvm *kvm)
  466. {
  467. int i;
  468. if (!kvm->debugfs_dentry)
  469. return;
  470. debugfs_remove_recursive(kvm->debugfs_dentry);
  471. if (kvm->debugfs_stat_data) {
  472. for (i = 0; i < kvm_debugfs_num_entries; i++)
  473. kfree(kvm->debugfs_stat_data[i]);
  474. kfree(kvm->debugfs_stat_data);
  475. }
  476. }
  477. static int kvm_create_vm_debugfs(struct kvm *kvm, int fd)
  478. {
  479. char dir_name[ITOA_MAX_LEN * 2];
  480. struct kvm_stat_data *stat_data;
  481. struct kvm_stats_debugfs_item *p;
  482. if (!debugfs_initialized())
  483. return 0;
  484. snprintf(dir_name, sizeof(dir_name), "%d-%d", task_pid_nr(current), fd);
  485. kvm->debugfs_dentry = debugfs_create_dir(dir_name,
  486. kvm_debugfs_dir);
  487. if (!kvm->debugfs_dentry)
  488. return -ENOMEM;
  489. kvm->debugfs_stat_data = kcalloc(kvm_debugfs_num_entries,
  490. sizeof(*kvm->debugfs_stat_data),
  491. GFP_KERNEL);
  492. if (!kvm->debugfs_stat_data)
  493. return -ENOMEM;
  494. for (p = debugfs_entries; p->name; p++) {
  495. stat_data = kzalloc(sizeof(*stat_data), GFP_KERNEL);
  496. if (!stat_data)
  497. return -ENOMEM;
  498. stat_data->kvm = kvm;
  499. stat_data->offset = p->offset;
  500. kvm->debugfs_stat_data[p - debugfs_entries] = stat_data;
  501. if (!debugfs_create_file(p->name, 0644,
  502. kvm->debugfs_dentry,
  503. stat_data,
  504. stat_fops_per_vm[p->kind]))
  505. return -ENOMEM;
  506. }
  507. return 0;
  508. }
  509. static struct kvm *kvm_create_vm(unsigned long type)
  510. {
  511. int r, i;
  512. struct kvm *kvm = kvm_arch_alloc_vm();
  513. if (!kvm)
  514. return ERR_PTR(-ENOMEM);
  515. spin_lock_init(&kvm->mmu_lock);
  516. atomic_inc(&current->mm->mm_count);
  517. kvm->mm = current->mm;
  518. kvm_eventfd_init(kvm);
  519. mutex_init(&kvm->lock);
  520. mutex_init(&kvm->irq_lock);
  521. mutex_init(&kvm->slots_lock);
  522. atomic_set(&kvm->users_count, 1);
  523. INIT_LIST_HEAD(&kvm->devices);
  524. r = kvm_arch_init_vm(kvm, type);
  525. if (r)
  526. goto out_err_no_disable;
  527. r = hardware_enable_all();
  528. if (r)
  529. goto out_err_no_disable;
  530. #ifdef CONFIG_HAVE_KVM_IRQFD
  531. INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
  532. #endif
  533. BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX);
  534. r = -ENOMEM;
  535. for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
  536. kvm->memslots[i] = kvm_alloc_memslots();
  537. if (!kvm->memslots[i])
  538. goto out_err_no_srcu;
  539. }
  540. if (init_srcu_struct(&kvm->srcu))
  541. goto out_err_no_srcu;
  542. if (init_srcu_struct(&kvm->irq_srcu))
  543. goto out_err_no_irq_srcu;
  544. for (i = 0; i < KVM_NR_BUSES; i++) {
  545. kvm->buses[i] = kzalloc(sizeof(struct kvm_io_bus),
  546. GFP_KERNEL);
  547. if (!kvm->buses[i])
  548. goto out_err;
  549. }
  550. r = kvm_init_mmu_notifier(kvm);
  551. if (r)
  552. goto out_err;
  553. spin_lock(&kvm_lock);
  554. list_add(&kvm->vm_list, &vm_list);
  555. spin_unlock(&kvm_lock);
  556. preempt_notifier_inc();
  557. return kvm;
  558. out_err:
  559. cleanup_srcu_struct(&kvm->irq_srcu);
  560. out_err_no_irq_srcu:
  561. cleanup_srcu_struct(&kvm->srcu);
  562. out_err_no_srcu:
  563. hardware_disable_all();
  564. out_err_no_disable:
  565. for (i = 0; i < KVM_NR_BUSES; i++)
  566. kfree(kvm->buses[i]);
  567. for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++)
  568. kvm_free_memslots(kvm, kvm->memslots[i]);
  569. kvm_arch_free_vm(kvm);
  570. mmdrop(current->mm);
  571. return ERR_PTR(r);
  572. }
  573. /*
  574. * Avoid using vmalloc for a small buffer.
  575. * Should not be used when the size is statically known.
  576. */
  577. void *kvm_kvzalloc(unsigned long size)
  578. {
  579. if (size > PAGE_SIZE)
  580. return vzalloc(size);
  581. else
  582. return kzalloc(size, GFP_KERNEL);
  583. }
  584. static void kvm_destroy_devices(struct kvm *kvm)
  585. {
  586. struct kvm_device *dev, *tmp;
  587. /*
  588. * We do not need to take the kvm->lock here, because nobody else
  589. * has a reference to the struct kvm at this point and therefore
  590. * cannot access the devices list anyhow.
  591. */
  592. list_for_each_entry_safe(dev, tmp, &kvm->devices, vm_node) {
  593. list_del(&dev->vm_node);
  594. dev->ops->destroy(dev);
  595. }
  596. }
  597. static void kvm_destroy_vm(struct kvm *kvm)
  598. {
  599. int i;
  600. struct mm_struct *mm = kvm->mm;
  601. kvm_destroy_vm_debugfs(kvm);
  602. kvm_arch_sync_events(kvm);
  603. spin_lock(&kvm_lock);
  604. list_del(&kvm->vm_list);
  605. spin_unlock(&kvm_lock);
  606. kvm_free_irq_routing(kvm);
  607. for (i = 0; i < KVM_NR_BUSES; i++)
  608. kvm_io_bus_destroy(kvm->buses[i]);
  609. kvm_coalesced_mmio_free(kvm);
  610. #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
  611. mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
  612. #else
  613. kvm_arch_flush_shadow_all(kvm);
  614. #endif
  615. kvm_arch_destroy_vm(kvm);
  616. kvm_destroy_devices(kvm);
  617. for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++)
  618. kvm_free_memslots(kvm, kvm->memslots[i]);
  619. cleanup_srcu_struct(&kvm->irq_srcu);
  620. cleanup_srcu_struct(&kvm->srcu);
  621. kvm_arch_free_vm(kvm);
  622. preempt_notifier_dec();
  623. hardware_disable_all();
  624. mmdrop(mm);
  625. }
  626. void kvm_get_kvm(struct kvm *kvm)
  627. {
  628. atomic_inc(&kvm->users_count);
  629. }
  630. EXPORT_SYMBOL_GPL(kvm_get_kvm);
  631. void kvm_put_kvm(struct kvm *kvm)
  632. {
  633. if (atomic_dec_and_test(&kvm->users_count))
  634. kvm_destroy_vm(kvm);
  635. }
  636. EXPORT_SYMBOL_GPL(kvm_put_kvm);
  637. static int kvm_vm_release(struct inode *inode, struct file *filp)
  638. {
  639. struct kvm *kvm = filp->private_data;
  640. kvm_irqfd_release(kvm);
  641. kvm_put_kvm(kvm);
  642. return 0;
  643. }
  644. /*
  645. * Allocation size is twice as large as the actual dirty bitmap size.
  646. * See x86's kvm_vm_ioctl_get_dirty_log() why this is needed.
  647. */
  648. static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot)
  649. {
  650. unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
  651. memslot->dirty_bitmap = kvm_kvzalloc(dirty_bytes);
  652. if (!memslot->dirty_bitmap)
  653. return -ENOMEM;
  654. return 0;
  655. }
  656. /*
  657. * Insert memslot and re-sort memslots based on their GFN,
  658. * so binary search could be used to lookup GFN.
  659. * Sorting algorithm takes advantage of having initially
  660. * sorted array and known changed memslot position.
  661. */
  662. static void update_memslots(struct kvm_memslots *slots,
  663. struct kvm_memory_slot *new)
  664. {
  665. int id = new->id;
  666. int i = slots->id_to_index[id];
  667. struct kvm_memory_slot *mslots = slots->memslots;
  668. WARN_ON(mslots[i].id != id);
  669. if (!new->npages) {
  670. WARN_ON(!mslots[i].npages);
  671. if (mslots[i].npages)
  672. slots->used_slots--;
  673. } else {
  674. if (!mslots[i].npages)
  675. slots->used_slots++;
  676. }
  677. while (i < KVM_MEM_SLOTS_NUM - 1 &&
  678. new->base_gfn <= mslots[i + 1].base_gfn) {
  679. if (!mslots[i + 1].npages)
  680. break;
  681. mslots[i] = mslots[i + 1];
  682. slots->id_to_index[mslots[i].id] = i;
  683. i++;
  684. }
  685. /*
  686. * The ">=" is needed when creating a slot with base_gfn == 0,
  687. * so that it moves before all those with base_gfn == npages == 0.
  688. *
  689. * On the other hand, if new->npages is zero, the above loop has
  690. * already left i pointing to the beginning of the empty part of
  691. * mslots, and the ">=" would move the hole backwards in this
  692. * case---which is wrong. So skip the loop when deleting a slot.
  693. */
  694. if (new->npages) {
  695. while (i > 0 &&
  696. new->base_gfn >= mslots[i - 1].base_gfn) {
  697. mslots[i] = mslots[i - 1];
  698. slots->id_to_index[mslots[i].id] = i;
  699. i--;
  700. }
  701. } else
  702. WARN_ON_ONCE(i != slots->used_slots);
  703. mslots[i] = *new;
  704. slots->id_to_index[mslots[i].id] = i;
  705. }
  706. static int check_memory_region_flags(const struct kvm_userspace_memory_region *mem)
  707. {
  708. u32 valid_flags = KVM_MEM_LOG_DIRTY_PAGES;
  709. #ifdef __KVM_HAVE_READONLY_MEM
  710. valid_flags |= KVM_MEM_READONLY;
  711. #endif
  712. if (mem->flags & ~valid_flags)
  713. return -EINVAL;
  714. return 0;
  715. }
  716. static struct kvm_memslots *install_new_memslots(struct kvm *kvm,
  717. int as_id, struct kvm_memslots *slots)
  718. {
  719. struct kvm_memslots *old_memslots = __kvm_memslots(kvm, as_id);
  720. /*
  721. * Set the low bit in the generation, which disables SPTE caching
  722. * until the end of synchronize_srcu_expedited.
  723. */
  724. WARN_ON(old_memslots->generation & 1);
  725. slots->generation = old_memslots->generation + 1;
  726. rcu_assign_pointer(kvm->memslots[as_id], slots);
  727. synchronize_srcu_expedited(&kvm->srcu);
  728. /*
  729. * Increment the new memslot generation a second time. This prevents
  730. * vm exits that race with memslot updates from caching a memslot
  731. * generation that will (potentially) be valid forever.
  732. */
  733. slots->generation++;
  734. kvm_arch_memslots_updated(kvm, slots);
  735. return old_memslots;
  736. }
  737. /*
  738. * Allocate some memory and give it an address in the guest physical address
  739. * space.
  740. *
  741. * Discontiguous memory is allowed, mostly for framebuffers.
  742. *
  743. * Must be called holding kvm->slots_lock for write.
  744. */
  745. int __kvm_set_memory_region(struct kvm *kvm,
  746. const struct kvm_userspace_memory_region *mem)
  747. {
  748. int r;
  749. gfn_t base_gfn;
  750. unsigned long npages;
  751. struct kvm_memory_slot *slot;
  752. struct kvm_memory_slot old, new;
  753. struct kvm_memslots *slots = NULL, *old_memslots;
  754. int as_id, id;
  755. enum kvm_mr_change change;
  756. r = check_memory_region_flags(mem);
  757. if (r)
  758. goto out;
  759. r = -EINVAL;
  760. as_id = mem->slot >> 16;
  761. id = (u16)mem->slot;
  762. /* General sanity checks */
  763. if (mem->memory_size & (PAGE_SIZE - 1))
  764. goto out;
  765. if (mem->guest_phys_addr & (PAGE_SIZE - 1))
  766. goto out;
  767. /* We can read the guest memory with __xxx_user() later on. */
  768. if ((id < KVM_USER_MEM_SLOTS) &&
  769. ((mem->userspace_addr & (PAGE_SIZE - 1)) ||
  770. !access_ok(VERIFY_WRITE,
  771. (void __user *)(unsigned long)mem->userspace_addr,
  772. mem->memory_size)))
  773. goto out;
  774. if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_MEM_SLOTS_NUM)
  775. goto out;
  776. if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
  777. goto out;
  778. slot = id_to_memslot(__kvm_memslots(kvm, as_id), id);
  779. base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
  780. npages = mem->memory_size >> PAGE_SHIFT;
  781. if (npages > KVM_MEM_MAX_NR_PAGES)
  782. goto out;
  783. new = old = *slot;
  784. new.id = id;
  785. new.base_gfn = base_gfn;
  786. new.npages = npages;
  787. new.flags = mem->flags;
  788. if (npages) {
  789. if (!old.npages)
  790. change = KVM_MR_CREATE;
  791. else { /* Modify an existing slot. */
  792. if ((mem->userspace_addr != old.userspace_addr) ||
  793. (npages != old.npages) ||
  794. ((new.flags ^ old.flags) & KVM_MEM_READONLY))
  795. goto out;
  796. if (base_gfn != old.base_gfn)
  797. change = KVM_MR_MOVE;
  798. else if (new.flags != old.flags)
  799. change = KVM_MR_FLAGS_ONLY;
  800. else { /* Nothing to change. */
  801. r = 0;
  802. goto out;
  803. }
  804. }
  805. } else {
  806. if (!old.npages)
  807. goto out;
  808. change = KVM_MR_DELETE;
  809. new.base_gfn = 0;
  810. new.flags = 0;
  811. }
  812. if ((change == KVM_MR_CREATE) || (change == KVM_MR_MOVE)) {
  813. /* Check for overlaps */
  814. r = -EEXIST;
  815. kvm_for_each_memslot(slot, __kvm_memslots(kvm, as_id)) {
  816. if ((slot->id >= KVM_USER_MEM_SLOTS) ||
  817. (slot->id == id))
  818. continue;
  819. if (!((base_gfn + npages <= slot->base_gfn) ||
  820. (base_gfn >= slot->base_gfn + slot->npages)))
  821. goto out;
  822. }
  823. }
  824. /* Free page dirty bitmap if unneeded */
  825. if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
  826. new.dirty_bitmap = NULL;
  827. r = -ENOMEM;
  828. if (change == KVM_MR_CREATE) {
  829. new.userspace_addr = mem->userspace_addr;
  830. if (kvm_arch_create_memslot(kvm, &new, npages))
  831. goto out_free;
  832. }
  833. /* Allocate page dirty bitmap if needed */
  834. if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
  835. if (kvm_create_dirty_bitmap(&new) < 0)
  836. goto out_free;
  837. }
  838. slots = kvm_kvzalloc(sizeof(struct kvm_memslots));
  839. if (!slots)
  840. goto out_free;
  841. memcpy(slots, __kvm_memslots(kvm, as_id), sizeof(struct kvm_memslots));
  842. if ((change == KVM_MR_DELETE) || (change == KVM_MR_MOVE)) {
  843. slot = id_to_memslot(slots, id);
  844. slot->flags |= KVM_MEMSLOT_INVALID;
  845. old_memslots = install_new_memslots(kvm, as_id, slots);
  846. /* slot was deleted or moved, clear iommu mapping */
  847. kvm_iommu_unmap_pages(kvm, &old);
  848. /* From this point no new shadow pages pointing to a deleted,
  849. * or moved, memslot will be created.
  850. *
  851. * validation of sp->gfn happens in:
  852. * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
  853. * - kvm_is_visible_gfn (mmu_check_roots)
  854. */
  855. kvm_arch_flush_shadow_memslot(kvm, slot);
  856. /*
  857. * We can re-use the old_memslots from above, the only difference
  858. * from the currently installed memslots is the invalid flag. This
  859. * will get overwritten by update_memslots anyway.
  860. */
  861. slots = old_memslots;
  862. }
  863. r = kvm_arch_prepare_memory_region(kvm, &new, mem, change);
  864. if (r)
  865. goto out_slots;
  866. /* actual memory is freed via old in kvm_free_memslot below */
  867. if (change == KVM_MR_DELETE) {
  868. new.dirty_bitmap = NULL;
  869. memset(&new.arch, 0, sizeof(new.arch));
  870. }
  871. update_memslots(slots, &new);
  872. old_memslots = install_new_memslots(kvm, as_id, slots);
  873. kvm_arch_commit_memory_region(kvm, mem, &old, &new, change);
  874. kvm_free_memslot(kvm, &old, &new);
  875. kvfree(old_memslots);
  876. /*
  877. * IOMMU mapping: New slots need to be mapped. Old slots need to be
  878. * un-mapped and re-mapped if their base changes. Since base change
  879. * unmapping is handled above with slot deletion, mapping alone is
  880. * needed here. Anything else the iommu might care about for existing
  881. * slots (size changes, userspace addr changes and read-only flag
  882. * changes) is disallowed above, so any other attribute changes getting
  883. * here can be skipped.
  884. */
  885. if ((change == KVM_MR_CREATE) || (change == KVM_MR_MOVE)) {
  886. r = kvm_iommu_map_pages(kvm, &new);
  887. return r;
  888. }
  889. return 0;
  890. out_slots:
  891. kvfree(slots);
  892. out_free:
  893. kvm_free_memslot(kvm, &new, &old);
  894. out:
  895. return r;
  896. }
  897. EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
  898. int kvm_set_memory_region(struct kvm *kvm,
  899. const struct kvm_userspace_memory_region *mem)
  900. {
  901. int r;
  902. mutex_lock(&kvm->slots_lock);
  903. r = __kvm_set_memory_region(kvm, mem);
  904. mutex_unlock(&kvm->slots_lock);
  905. return r;
  906. }
  907. EXPORT_SYMBOL_GPL(kvm_set_memory_region);
  908. static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
  909. struct kvm_userspace_memory_region *mem)
  910. {
  911. if ((u16)mem->slot >= KVM_USER_MEM_SLOTS)
  912. return -EINVAL;
  913. return kvm_set_memory_region(kvm, mem);
  914. }
  915. int kvm_get_dirty_log(struct kvm *kvm,
  916. struct kvm_dirty_log *log, int *is_dirty)
  917. {
  918. struct kvm_memslots *slots;
  919. struct kvm_memory_slot *memslot;
  920. int r, i, as_id, id;
  921. unsigned long n;
  922. unsigned long any = 0;
  923. r = -EINVAL;
  924. as_id = log->slot >> 16;
  925. id = (u16)log->slot;
  926. if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS)
  927. goto out;
  928. slots = __kvm_memslots(kvm, as_id);
  929. memslot = id_to_memslot(slots, id);
  930. r = -ENOENT;
  931. if (!memslot->dirty_bitmap)
  932. goto out;
  933. n = kvm_dirty_bitmap_bytes(memslot);
  934. for (i = 0; !any && i < n/sizeof(long); ++i)
  935. any = memslot->dirty_bitmap[i];
  936. r = -EFAULT;
  937. if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
  938. goto out;
  939. if (any)
  940. *is_dirty = 1;
  941. r = 0;
  942. out:
  943. return r;
  944. }
  945. EXPORT_SYMBOL_GPL(kvm_get_dirty_log);
  946. #ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
  947. /**
  948. * kvm_get_dirty_log_protect - get a snapshot of dirty pages, and if any pages
  949. * are dirty write protect them for next write.
  950. * @kvm: pointer to kvm instance
  951. * @log: slot id and address to which we copy the log
  952. * @is_dirty: flag set if any page is dirty
  953. *
  954. * We need to keep it in mind that VCPU threads can write to the bitmap
  955. * concurrently. So, to avoid losing track of dirty pages we keep the
  956. * following order:
  957. *
  958. * 1. Take a snapshot of the bit and clear it if needed.
  959. * 2. Write protect the corresponding page.
  960. * 3. Copy the snapshot to the userspace.
  961. * 4. Upon return caller flushes TLB's if needed.
  962. *
  963. * Between 2 and 4, the guest may write to the page using the remaining TLB
  964. * entry. This is not a problem because the page is reported dirty using
  965. * the snapshot taken before and step 4 ensures that writes done after
  966. * exiting to userspace will be logged for the next call.
  967. *
  968. */
  969. int kvm_get_dirty_log_protect(struct kvm *kvm,
  970. struct kvm_dirty_log *log, bool *is_dirty)
  971. {
  972. struct kvm_memslots *slots;
  973. struct kvm_memory_slot *memslot;
  974. int r, i, as_id, id;
  975. unsigned long n;
  976. unsigned long *dirty_bitmap;
  977. unsigned long *dirty_bitmap_buffer;
  978. r = -EINVAL;
  979. as_id = log->slot >> 16;
  980. id = (u16)log->slot;
  981. if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS)
  982. goto out;
  983. slots = __kvm_memslots(kvm, as_id);
  984. memslot = id_to_memslot(slots, id);
  985. dirty_bitmap = memslot->dirty_bitmap;
  986. r = -ENOENT;
  987. if (!dirty_bitmap)
  988. goto out;
  989. n = kvm_dirty_bitmap_bytes(memslot);
  990. dirty_bitmap_buffer = dirty_bitmap + n / sizeof(long);
  991. memset(dirty_bitmap_buffer, 0, n);
  992. spin_lock(&kvm->mmu_lock);
  993. *is_dirty = false;
  994. for (i = 0; i < n / sizeof(long); i++) {
  995. unsigned long mask;
  996. gfn_t offset;
  997. if (!dirty_bitmap[i])
  998. continue;
  999. *is_dirty = true;
  1000. mask = xchg(&dirty_bitmap[i], 0);
  1001. dirty_bitmap_buffer[i] = mask;
  1002. if (mask) {
  1003. offset = i * BITS_PER_LONG;
  1004. kvm_arch_mmu_enable_log_dirty_pt_masked(kvm, memslot,
  1005. offset, mask);
  1006. }
  1007. }
  1008. spin_unlock(&kvm->mmu_lock);
  1009. r = -EFAULT;
  1010. if (copy_to_user(log->dirty_bitmap, dirty_bitmap_buffer, n))
  1011. goto out;
  1012. r = 0;
  1013. out:
  1014. return r;
  1015. }
  1016. EXPORT_SYMBOL_GPL(kvm_get_dirty_log_protect);
  1017. #endif
  1018. bool kvm_largepages_enabled(void)
  1019. {
  1020. return largepages_enabled;
  1021. }
  1022. void kvm_disable_largepages(void)
  1023. {
  1024. largepages_enabled = false;
  1025. }
  1026. EXPORT_SYMBOL_GPL(kvm_disable_largepages);
  1027. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
  1028. {
  1029. return __gfn_to_memslot(kvm_memslots(kvm), gfn);
  1030. }
  1031. EXPORT_SYMBOL_GPL(gfn_to_memslot);
  1032. struct kvm_memory_slot *kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn)
  1033. {
  1034. return __gfn_to_memslot(kvm_vcpu_memslots(vcpu), gfn);
  1035. }
  1036. bool kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
  1037. {
  1038. struct kvm_memory_slot *memslot = gfn_to_memslot(kvm, gfn);
  1039. if (!memslot || memslot->id >= KVM_USER_MEM_SLOTS ||
  1040. memslot->flags & KVM_MEMSLOT_INVALID)
  1041. return false;
  1042. return true;
  1043. }
  1044. EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
  1045. unsigned long kvm_host_page_size(struct kvm *kvm, gfn_t gfn)
  1046. {
  1047. struct vm_area_struct *vma;
  1048. unsigned long addr, size;
  1049. size = PAGE_SIZE;
  1050. addr = gfn_to_hva(kvm, gfn);
  1051. if (kvm_is_error_hva(addr))
  1052. return PAGE_SIZE;
  1053. down_read(&current->mm->mmap_sem);
  1054. vma = find_vma(current->mm, addr);
  1055. if (!vma)
  1056. goto out;
  1057. size = vma_kernel_pagesize(vma);
  1058. out:
  1059. up_read(&current->mm->mmap_sem);
  1060. return size;
  1061. }
  1062. static bool memslot_is_readonly(struct kvm_memory_slot *slot)
  1063. {
  1064. return slot->flags & KVM_MEM_READONLY;
  1065. }
  1066. static unsigned long __gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
  1067. gfn_t *nr_pages, bool write)
  1068. {
  1069. if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
  1070. return KVM_HVA_ERR_BAD;
  1071. if (memslot_is_readonly(slot) && write)
  1072. return KVM_HVA_ERR_RO_BAD;
  1073. if (nr_pages)
  1074. *nr_pages = slot->npages - (gfn - slot->base_gfn);
  1075. return __gfn_to_hva_memslot(slot, gfn);
  1076. }
  1077. static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
  1078. gfn_t *nr_pages)
  1079. {
  1080. return __gfn_to_hva_many(slot, gfn, nr_pages, true);
  1081. }
  1082. unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot,
  1083. gfn_t gfn)
  1084. {
  1085. return gfn_to_hva_many(slot, gfn, NULL);
  1086. }
  1087. EXPORT_SYMBOL_GPL(gfn_to_hva_memslot);
  1088. unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
  1089. {
  1090. return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
  1091. }
  1092. EXPORT_SYMBOL_GPL(gfn_to_hva);
  1093. unsigned long kvm_vcpu_gfn_to_hva(struct kvm_vcpu *vcpu, gfn_t gfn)
  1094. {
  1095. return gfn_to_hva_many(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn, NULL);
  1096. }
  1097. EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_hva);
  1098. /*
  1099. * If writable is set to false, the hva returned by this function is only
  1100. * allowed to be read.
  1101. */
  1102. unsigned long gfn_to_hva_memslot_prot(struct kvm_memory_slot *slot,
  1103. gfn_t gfn, bool *writable)
  1104. {
  1105. unsigned long hva = __gfn_to_hva_many(slot, gfn, NULL, false);
  1106. if (!kvm_is_error_hva(hva) && writable)
  1107. *writable = !memslot_is_readonly(slot);
  1108. return hva;
  1109. }
  1110. unsigned long gfn_to_hva_prot(struct kvm *kvm, gfn_t gfn, bool *writable)
  1111. {
  1112. struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
  1113. return gfn_to_hva_memslot_prot(slot, gfn, writable);
  1114. }
  1115. unsigned long kvm_vcpu_gfn_to_hva_prot(struct kvm_vcpu *vcpu, gfn_t gfn, bool *writable)
  1116. {
  1117. struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
  1118. return gfn_to_hva_memslot_prot(slot, gfn, writable);
  1119. }
  1120. static int get_user_page_nowait(unsigned long start, int write,
  1121. struct page **page)
  1122. {
  1123. int flags = FOLL_NOWAIT | FOLL_HWPOISON;
  1124. if (write)
  1125. flags |= FOLL_WRITE;
  1126. return get_user_pages(start, 1, flags, page, NULL);
  1127. }
  1128. static inline int check_user_page_hwpoison(unsigned long addr)
  1129. {
  1130. int rc, flags = FOLL_HWPOISON | FOLL_WRITE;
  1131. rc = get_user_pages(addr, 1, flags, NULL, NULL);
  1132. return rc == -EHWPOISON;
  1133. }
  1134. /*
  1135. * The atomic path to get the writable pfn which will be stored in @pfn,
  1136. * true indicates success, otherwise false is returned.
  1137. */
  1138. static bool hva_to_pfn_fast(unsigned long addr, bool atomic, bool *async,
  1139. bool write_fault, bool *writable, kvm_pfn_t *pfn)
  1140. {
  1141. struct page *page[1];
  1142. int npages;
  1143. if (!(async || atomic))
  1144. return false;
  1145. /*
  1146. * Fast pin a writable pfn only if it is a write fault request
  1147. * or the caller allows to map a writable pfn for a read fault
  1148. * request.
  1149. */
  1150. if (!(write_fault || writable))
  1151. return false;
  1152. npages = __get_user_pages_fast(addr, 1, 1, page);
  1153. if (npages == 1) {
  1154. *pfn = page_to_pfn(page[0]);
  1155. if (writable)
  1156. *writable = true;
  1157. return true;
  1158. }
  1159. return false;
  1160. }
  1161. /*
  1162. * The slow path to get the pfn of the specified host virtual address,
  1163. * 1 indicates success, -errno is returned if error is detected.
  1164. */
  1165. static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault,
  1166. bool *writable, kvm_pfn_t *pfn)
  1167. {
  1168. struct page *page[1];
  1169. int npages = 0;
  1170. might_sleep();
  1171. if (writable)
  1172. *writable = write_fault;
  1173. if (async) {
  1174. down_read(&current->mm->mmap_sem);
  1175. npages = get_user_page_nowait(addr, write_fault, page);
  1176. up_read(&current->mm->mmap_sem);
  1177. } else {
  1178. unsigned int flags = FOLL_HWPOISON;
  1179. if (write_fault)
  1180. flags |= FOLL_WRITE;
  1181. npages = get_user_pages_unlocked(addr, 1, page, flags);
  1182. }
  1183. if (npages != 1)
  1184. return npages;
  1185. /* map read fault as writable if possible */
  1186. if (unlikely(!write_fault) && writable) {
  1187. struct page *wpage[1];
  1188. npages = __get_user_pages_fast(addr, 1, 1, wpage);
  1189. if (npages == 1) {
  1190. *writable = true;
  1191. put_page(page[0]);
  1192. page[0] = wpage[0];
  1193. }
  1194. npages = 1;
  1195. }
  1196. *pfn = page_to_pfn(page[0]);
  1197. return npages;
  1198. }
  1199. static bool vma_is_valid(struct vm_area_struct *vma, bool write_fault)
  1200. {
  1201. if (unlikely(!(vma->vm_flags & VM_READ)))
  1202. return false;
  1203. if (write_fault && (unlikely(!(vma->vm_flags & VM_WRITE))))
  1204. return false;
  1205. return true;
  1206. }
  1207. static int hva_to_pfn_remapped(struct vm_area_struct *vma,
  1208. unsigned long addr, bool *async,
  1209. bool write_fault, kvm_pfn_t *p_pfn)
  1210. {
  1211. unsigned long pfn;
  1212. int r;
  1213. r = follow_pfn(vma, addr, &pfn);
  1214. if (r) {
  1215. /*
  1216. * get_user_pages fails for VM_IO and VM_PFNMAP vmas and does
  1217. * not call the fault handler, so do it here.
  1218. */
  1219. bool unlocked = false;
  1220. r = fixup_user_fault(current, current->mm, addr,
  1221. (write_fault ? FAULT_FLAG_WRITE : 0),
  1222. &unlocked);
  1223. if (unlocked)
  1224. return -EAGAIN;
  1225. if (r)
  1226. return r;
  1227. r = follow_pfn(vma, addr, &pfn);
  1228. if (r)
  1229. return r;
  1230. }
  1231. /*
  1232. * Get a reference here because callers of *hva_to_pfn* and
  1233. * *gfn_to_pfn* ultimately call kvm_release_pfn_clean on the
  1234. * returned pfn. This is only needed if the VMA has VM_MIXEDMAP
  1235. * set, but the kvm_get_pfn/kvm_release_pfn_clean pair will
  1236. * simply do nothing for reserved pfns.
  1237. *
  1238. * Whoever called remap_pfn_range is also going to call e.g.
  1239. * unmap_mapping_range before the underlying pages are freed,
  1240. * causing a call to our MMU notifier.
  1241. */
  1242. kvm_get_pfn(pfn);
  1243. *p_pfn = pfn;
  1244. return 0;
  1245. }
  1246. /*
  1247. * Pin guest page in memory and return its pfn.
  1248. * @addr: host virtual address which maps memory to the guest
  1249. * @atomic: whether this function can sleep
  1250. * @async: whether this function need to wait IO complete if the
  1251. * host page is not in the memory
  1252. * @write_fault: whether we should get a writable host page
  1253. * @writable: whether it allows to map a writable host page for !@write_fault
  1254. *
  1255. * The function will map a writable host page for these two cases:
  1256. * 1): @write_fault = true
  1257. * 2): @write_fault = false && @writable, @writable will tell the caller
  1258. * whether the mapping is writable.
  1259. */
  1260. static kvm_pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool *async,
  1261. bool write_fault, bool *writable)
  1262. {
  1263. struct vm_area_struct *vma;
  1264. kvm_pfn_t pfn = 0;
  1265. int npages, r;
  1266. /* we can do it either atomically or asynchronously, not both */
  1267. BUG_ON(atomic && async);
  1268. if (hva_to_pfn_fast(addr, atomic, async, write_fault, writable, &pfn))
  1269. return pfn;
  1270. if (atomic)
  1271. return KVM_PFN_ERR_FAULT;
  1272. npages = hva_to_pfn_slow(addr, async, write_fault, writable, &pfn);
  1273. if (npages == 1)
  1274. return pfn;
  1275. down_read(&current->mm->mmap_sem);
  1276. if (npages == -EHWPOISON ||
  1277. (!async && check_user_page_hwpoison(addr))) {
  1278. pfn = KVM_PFN_ERR_HWPOISON;
  1279. goto exit;
  1280. }
  1281. retry:
  1282. vma = find_vma_intersection(current->mm, addr, addr + 1);
  1283. if (vma == NULL)
  1284. pfn = KVM_PFN_ERR_FAULT;
  1285. else if (vma->vm_flags & (VM_IO | VM_PFNMAP)) {
  1286. r = hva_to_pfn_remapped(vma, addr, async, write_fault, &pfn);
  1287. if (r == -EAGAIN)
  1288. goto retry;
  1289. if (r < 0)
  1290. pfn = KVM_PFN_ERR_FAULT;
  1291. } else {
  1292. if (async && vma_is_valid(vma, write_fault))
  1293. *async = true;
  1294. pfn = KVM_PFN_ERR_FAULT;
  1295. }
  1296. exit:
  1297. up_read(&current->mm->mmap_sem);
  1298. return pfn;
  1299. }
  1300. kvm_pfn_t __gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn,
  1301. bool atomic, bool *async, bool write_fault,
  1302. bool *writable)
  1303. {
  1304. unsigned long addr = __gfn_to_hva_many(slot, gfn, NULL, write_fault);
  1305. if (addr == KVM_HVA_ERR_RO_BAD) {
  1306. if (writable)
  1307. *writable = false;
  1308. return KVM_PFN_ERR_RO_FAULT;
  1309. }
  1310. if (kvm_is_error_hva(addr)) {
  1311. if (writable)
  1312. *writable = false;
  1313. return KVM_PFN_NOSLOT;
  1314. }
  1315. /* Do not map writable pfn in the readonly memslot. */
  1316. if (writable && memslot_is_readonly(slot)) {
  1317. *writable = false;
  1318. writable = NULL;
  1319. }
  1320. return hva_to_pfn(addr, atomic, async, write_fault,
  1321. writable);
  1322. }
  1323. EXPORT_SYMBOL_GPL(__gfn_to_pfn_memslot);
  1324. kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
  1325. bool *writable)
  1326. {
  1327. return __gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn, false, NULL,
  1328. write_fault, writable);
  1329. }
  1330. EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
  1331. kvm_pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
  1332. {
  1333. return __gfn_to_pfn_memslot(slot, gfn, false, NULL, true, NULL);
  1334. }
  1335. EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot);
  1336. kvm_pfn_t gfn_to_pfn_memslot_atomic(struct kvm_memory_slot *slot, gfn_t gfn)
  1337. {
  1338. return __gfn_to_pfn_memslot(slot, gfn, true, NULL, true, NULL);
  1339. }
  1340. EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot_atomic);
  1341. kvm_pfn_t gfn_to_pfn_atomic(struct kvm *kvm, gfn_t gfn)
  1342. {
  1343. return gfn_to_pfn_memslot_atomic(gfn_to_memslot(kvm, gfn), gfn);
  1344. }
  1345. EXPORT_SYMBOL_GPL(gfn_to_pfn_atomic);
  1346. kvm_pfn_t kvm_vcpu_gfn_to_pfn_atomic(struct kvm_vcpu *vcpu, gfn_t gfn)
  1347. {
  1348. return gfn_to_pfn_memslot_atomic(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn);
  1349. }
  1350. EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_pfn_atomic);
  1351. kvm_pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
  1352. {
  1353. return gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn);
  1354. }
  1355. EXPORT_SYMBOL_GPL(gfn_to_pfn);
  1356. kvm_pfn_t kvm_vcpu_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn)
  1357. {
  1358. return gfn_to_pfn_memslot(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn);
  1359. }
  1360. EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_pfn);
  1361. int gfn_to_page_many_atomic(struct kvm_memory_slot *slot, gfn_t gfn,
  1362. struct page **pages, int nr_pages)
  1363. {
  1364. unsigned long addr;
  1365. gfn_t entry;
  1366. addr = gfn_to_hva_many(slot, gfn, &entry);
  1367. if (kvm_is_error_hva(addr))
  1368. return -1;
  1369. if (entry < nr_pages)
  1370. return 0;
  1371. return __get_user_pages_fast(addr, nr_pages, 1, pages);
  1372. }
  1373. EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
  1374. static struct page *kvm_pfn_to_page(kvm_pfn_t pfn)
  1375. {
  1376. if (is_error_noslot_pfn(pfn))
  1377. return KVM_ERR_PTR_BAD_PAGE;
  1378. if (kvm_is_reserved_pfn(pfn)) {
  1379. WARN_ON(1);
  1380. return KVM_ERR_PTR_BAD_PAGE;
  1381. }
  1382. return pfn_to_page(pfn);
  1383. }
  1384. struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
  1385. {
  1386. kvm_pfn_t pfn;
  1387. pfn = gfn_to_pfn(kvm, gfn);
  1388. return kvm_pfn_to_page(pfn);
  1389. }
  1390. EXPORT_SYMBOL_GPL(gfn_to_page);
  1391. struct page *kvm_vcpu_gfn_to_page(struct kvm_vcpu *vcpu, gfn_t gfn)
  1392. {
  1393. kvm_pfn_t pfn;
  1394. pfn = kvm_vcpu_gfn_to_pfn(vcpu, gfn);
  1395. return kvm_pfn_to_page(pfn);
  1396. }
  1397. EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_page);
  1398. void kvm_release_page_clean(struct page *page)
  1399. {
  1400. WARN_ON(is_error_page(page));
  1401. kvm_release_pfn_clean(page_to_pfn(page));
  1402. }
  1403. EXPORT_SYMBOL_GPL(kvm_release_page_clean);
  1404. void kvm_release_pfn_clean(kvm_pfn_t pfn)
  1405. {
  1406. if (!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn))
  1407. put_page(pfn_to_page(pfn));
  1408. }
  1409. EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
  1410. void kvm_release_page_dirty(struct page *page)
  1411. {
  1412. WARN_ON(is_error_page(page));
  1413. kvm_release_pfn_dirty(page_to_pfn(page));
  1414. }
  1415. EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
  1416. static void kvm_release_pfn_dirty(kvm_pfn_t pfn)
  1417. {
  1418. kvm_set_pfn_dirty(pfn);
  1419. kvm_release_pfn_clean(pfn);
  1420. }
  1421. void kvm_set_pfn_dirty(kvm_pfn_t pfn)
  1422. {
  1423. if (!kvm_is_reserved_pfn(pfn)) {
  1424. struct page *page = pfn_to_page(pfn);
  1425. if (!PageReserved(page))
  1426. SetPageDirty(page);
  1427. }
  1428. }
  1429. EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
  1430. void kvm_set_pfn_accessed(kvm_pfn_t pfn)
  1431. {
  1432. if (!kvm_is_reserved_pfn(pfn))
  1433. mark_page_accessed(pfn_to_page(pfn));
  1434. }
  1435. EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
  1436. void kvm_get_pfn(kvm_pfn_t pfn)
  1437. {
  1438. if (!kvm_is_reserved_pfn(pfn))
  1439. get_page(pfn_to_page(pfn));
  1440. }
  1441. EXPORT_SYMBOL_GPL(kvm_get_pfn);
  1442. static int next_segment(unsigned long len, int offset)
  1443. {
  1444. if (len > PAGE_SIZE - offset)
  1445. return PAGE_SIZE - offset;
  1446. else
  1447. return len;
  1448. }
  1449. static int __kvm_read_guest_page(struct kvm_memory_slot *slot, gfn_t gfn,
  1450. void *data, int offset, int len)
  1451. {
  1452. int r;
  1453. unsigned long addr;
  1454. addr = gfn_to_hva_memslot_prot(slot, gfn, NULL);
  1455. if (kvm_is_error_hva(addr))
  1456. return -EFAULT;
  1457. r = __copy_from_user(data, (void __user *)addr + offset, len);
  1458. if (r)
  1459. return -EFAULT;
  1460. return 0;
  1461. }
  1462. int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
  1463. int len)
  1464. {
  1465. struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
  1466. return __kvm_read_guest_page(slot, gfn, data, offset, len);
  1467. }
  1468. EXPORT_SYMBOL_GPL(kvm_read_guest_page);
  1469. int kvm_vcpu_read_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, void *data,
  1470. int offset, int len)
  1471. {
  1472. struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
  1473. return __kvm_read_guest_page(slot, gfn, data, offset, len);
  1474. }
  1475. EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_page);
  1476. int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
  1477. {
  1478. gfn_t gfn = gpa >> PAGE_SHIFT;
  1479. int seg;
  1480. int offset = offset_in_page(gpa);
  1481. int ret;
  1482. while ((seg = next_segment(len, offset)) != 0) {
  1483. ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
  1484. if (ret < 0)
  1485. return ret;
  1486. offset = 0;
  1487. len -= seg;
  1488. data += seg;
  1489. ++gfn;
  1490. }
  1491. return 0;
  1492. }
  1493. EXPORT_SYMBOL_GPL(kvm_read_guest);
  1494. int kvm_vcpu_read_guest(struct kvm_vcpu *vcpu, gpa_t gpa, void *data, unsigned long len)
  1495. {
  1496. gfn_t gfn = gpa >> PAGE_SHIFT;
  1497. int seg;
  1498. int offset = offset_in_page(gpa);
  1499. int ret;
  1500. while ((seg = next_segment(len, offset)) != 0) {
  1501. ret = kvm_vcpu_read_guest_page(vcpu, gfn, data, offset, seg);
  1502. if (ret < 0)
  1503. return ret;
  1504. offset = 0;
  1505. len -= seg;
  1506. data += seg;
  1507. ++gfn;
  1508. }
  1509. return 0;
  1510. }
  1511. EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest);
  1512. static int __kvm_read_guest_atomic(struct kvm_memory_slot *slot, gfn_t gfn,
  1513. void *data, int offset, unsigned long len)
  1514. {
  1515. int r;
  1516. unsigned long addr;
  1517. addr = gfn_to_hva_memslot_prot(slot, gfn, NULL);
  1518. if (kvm_is_error_hva(addr))
  1519. return -EFAULT;
  1520. pagefault_disable();
  1521. r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
  1522. pagefault_enable();
  1523. if (r)
  1524. return -EFAULT;
  1525. return 0;
  1526. }
  1527. int kvm_read_guest_atomic(struct kvm *kvm, gpa_t gpa, void *data,
  1528. unsigned long len)
  1529. {
  1530. gfn_t gfn = gpa >> PAGE_SHIFT;
  1531. struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
  1532. int offset = offset_in_page(gpa);
  1533. return __kvm_read_guest_atomic(slot, gfn, data, offset, len);
  1534. }
  1535. EXPORT_SYMBOL_GPL(kvm_read_guest_atomic);
  1536. int kvm_vcpu_read_guest_atomic(struct kvm_vcpu *vcpu, gpa_t gpa,
  1537. void *data, unsigned long len)
  1538. {
  1539. gfn_t gfn = gpa >> PAGE_SHIFT;
  1540. struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
  1541. int offset = offset_in_page(gpa);
  1542. return __kvm_read_guest_atomic(slot, gfn, data, offset, len);
  1543. }
  1544. EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_atomic);
  1545. static int __kvm_write_guest_page(struct kvm_memory_slot *memslot, gfn_t gfn,
  1546. const void *data, int offset, int len)
  1547. {
  1548. int r;
  1549. unsigned long addr;
  1550. addr = gfn_to_hva_memslot(memslot, gfn);
  1551. if (kvm_is_error_hva(addr))
  1552. return -EFAULT;
  1553. r = __copy_to_user((void __user *)addr + offset, data, len);
  1554. if (r)
  1555. return -EFAULT;
  1556. mark_page_dirty_in_slot(memslot, gfn);
  1557. return 0;
  1558. }
  1559. int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn,
  1560. const void *data, int offset, int len)
  1561. {
  1562. struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
  1563. return __kvm_write_guest_page(slot, gfn, data, offset, len);
  1564. }
  1565. EXPORT_SYMBOL_GPL(kvm_write_guest_page);
  1566. int kvm_vcpu_write_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
  1567. const void *data, int offset, int len)
  1568. {
  1569. struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
  1570. return __kvm_write_guest_page(slot, gfn, data, offset, len);
  1571. }
  1572. EXPORT_SYMBOL_GPL(kvm_vcpu_write_guest_page);
  1573. int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
  1574. unsigned long len)
  1575. {
  1576. gfn_t gfn = gpa >> PAGE_SHIFT;
  1577. int seg;
  1578. int offset = offset_in_page(gpa);
  1579. int ret;
  1580. while ((seg = next_segment(len, offset)) != 0) {
  1581. ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
  1582. if (ret < 0)
  1583. return ret;
  1584. offset = 0;
  1585. len -= seg;
  1586. data += seg;
  1587. ++gfn;
  1588. }
  1589. return 0;
  1590. }
  1591. EXPORT_SYMBOL_GPL(kvm_write_guest);
  1592. int kvm_vcpu_write_guest(struct kvm_vcpu *vcpu, gpa_t gpa, const void *data,
  1593. unsigned long len)
  1594. {
  1595. gfn_t gfn = gpa >> PAGE_SHIFT;
  1596. int seg;
  1597. int offset = offset_in_page(gpa);
  1598. int ret;
  1599. while ((seg = next_segment(len, offset)) != 0) {
  1600. ret = kvm_vcpu_write_guest_page(vcpu, gfn, data, offset, seg);
  1601. if (ret < 0)
  1602. return ret;
  1603. offset = 0;
  1604. len -= seg;
  1605. data += seg;
  1606. ++gfn;
  1607. }
  1608. return 0;
  1609. }
  1610. EXPORT_SYMBOL_GPL(kvm_vcpu_write_guest);
  1611. int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  1612. gpa_t gpa, unsigned long len)
  1613. {
  1614. struct kvm_memslots *slots = kvm_memslots(kvm);
  1615. int offset = offset_in_page(gpa);
  1616. gfn_t start_gfn = gpa >> PAGE_SHIFT;
  1617. gfn_t end_gfn = (gpa + len - 1) >> PAGE_SHIFT;
  1618. gfn_t nr_pages_needed = end_gfn - start_gfn + 1;
  1619. gfn_t nr_pages_avail;
  1620. ghc->gpa = gpa;
  1621. ghc->generation = slots->generation;
  1622. ghc->len = len;
  1623. ghc->memslot = gfn_to_memslot(kvm, start_gfn);
  1624. ghc->hva = gfn_to_hva_many(ghc->memslot, start_gfn, NULL);
  1625. if (!kvm_is_error_hva(ghc->hva) && nr_pages_needed <= 1) {
  1626. ghc->hva += offset;
  1627. } else {
  1628. /*
  1629. * If the requested region crosses two memslots, we still
  1630. * verify that the entire region is valid here.
  1631. */
  1632. while (start_gfn <= end_gfn) {
  1633. ghc->memslot = gfn_to_memslot(kvm, start_gfn);
  1634. ghc->hva = gfn_to_hva_many(ghc->memslot, start_gfn,
  1635. &nr_pages_avail);
  1636. if (kvm_is_error_hva(ghc->hva))
  1637. return -EFAULT;
  1638. start_gfn += nr_pages_avail;
  1639. }
  1640. /* Use the slow path for cross page reads and writes. */
  1641. ghc->memslot = NULL;
  1642. }
  1643. return 0;
  1644. }
  1645. EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
  1646. int kvm_write_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  1647. void *data, int offset, unsigned long len)
  1648. {
  1649. struct kvm_memslots *slots = kvm_memslots(kvm);
  1650. int r;
  1651. gpa_t gpa = ghc->gpa + offset;
  1652. BUG_ON(len + offset > ghc->len);
  1653. if (slots->generation != ghc->generation)
  1654. kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa, ghc->len);
  1655. if (unlikely(!ghc->memslot))
  1656. return kvm_write_guest(kvm, gpa, data, len);
  1657. if (kvm_is_error_hva(ghc->hva))
  1658. return -EFAULT;
  1659. r = __copy_to_user((void __user *)ghc->hva + offset, data, len);
  1660. if (r)
  1661. return -EFAULT;
  1662. mark_page_dirty_in_slot(ghc->memslot, gpa >> PAGE_SHIFT);
  1663. return 0;
  1664. }
  1665. EXPORT_SYMBOL_GPL(kvm_write_guest_offset_cached);
  1666. int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  1667. void *data, unsigned long len)
  1668. {
  1669. return kvm_write_guest_offset_cached(kvm, ghc, data, 0, len);
  1670. }
  1671. EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
  1672. int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
  1673. void *data, unsigned long len)
  1674. {
  1675. struct kvm_memslots *slots = kvm_memslots(kvm);
  1676. int r;
  1677. BUG_ON(len > ghc->len);
  1678. if (slots->generation != ghc->generation)
  1679. kvm_gfn_to_hva_cache_init(kvm, ghc, ghc->gpa, ghc->len);
  1680. if (unlikely(!ghc->memslot))
  1681. return kvm_read_guest(kvm, ghc->gpa, data, len);
  1682. if (kvm_is_error_hva(ghc->hva))
  1683. return -EFAULT;
  1684. r = __copy_from_user(data, (void __user *)ghc->hva, len);
  1685. if (r)
  1686. return -EFAULT;
  1687. return 0;
  1688. }
  1689. EXPORT_SYMBOL_GPL(kvm_read_guest_cached);
  1690. int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
  1691. {
  1692. const void *zero_page = (const void *) __va(page_to_phys(ZERO_PAGE(0)));
  1693. return kvm_write_guest_page(kvm, gfn, zero_page, offset, len);
  1694. }
  1695. EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
  1696. int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
  1697. {
  1698. gfn_t gfn = gpa >> PAGE_SHIFT;
  1699. int seg;
  1700. int offset = offset_in_page(gpa);
  1701. int ret;
  1702. while ((seg = next_segment(len, offset)) != 0) {
  1703. ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
  1704. if (ret < 0)
  1705. return ret;
  1706. offset = 0;
  1707. len -= seg;
  1708. ++gfn;
  1709. }
  1710. return 0;
  1711. }
  1712. EXPORT_SYMBOL_GPL(kvm_clear_guest);
  1713. static void mark_page_dirty_in_slot(struct kvm_memory_slot *memslot,
  1714. gfn_t gfn)
  1715. {
  1716. if (memslot && memslot->dirty_bitmap) {
  1717. unsigned long rel_gfn = gfn - memslot->base_gfn;
  1718. set_bit_le(rel_gfn, memslot->dirty_bitmap);
  1719. }
  1720. }
  1721. void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
  1722. {
  1723. struct kvm_memory_slot *memslot;
  1724. memslot = gfn_to_memslot(kvm, gfn);
  1725. mark_page_dirty_in_slot(memslot, gfn);
  1726. }
  1727. EXPORT_SYMBOL_GPL(mark_page_dirty);
  1728. void kvm_vcpu_mark_page_dirty(struct kvm_vcpu *vcpu, gfn_t gfn)
  1729. {
  1730. struct kvm_memory_slot *memslot;
  1731. memslot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
  1732. mark_page_dirty_in_slot(memslot, gfn);
  1733. }
  1734. EXPORT_SYMBOL_GPL(kvm_vcpu_mark_page_dirty);
  1735. static void grow_halt_poll_ns(struct kvm_vcpu *vcpu)
  1736. {
  1737. unsigned int old, val, grow;
  1738. old = val = vcpu->halt_poll_ns;
  1739. grow = READ_ONCE(halt_poll_ns_grow);
  1740. /* 10us base */
  1741. if (val == 0 && grow)
  1742. val = 10000;
  1743. else
  1744. val *= grow;
  1745. if (val > halt_poll_ns)
  1746. val = halt_poll_ns;
  1747. vcpu->halt_poll_ns = val;
  1748. trace_kvm_halt_poll_ns_grow(vcpu->vcpu_id, val, old);
  1749. }
  1750. static void shrink_halt_poll_ns(struct kvm_vcpu *vcpu)
  1751. {
  1752. unsigned int old, val, shrink;
  1753. old = val = vcpu->halt_poll_ns;
  1754. shrink = READ_ONCE(halt_poll_ns_shrink);
  1755. if (shrink == 0)
  1756. val = 0;
  1757. else
  1758. val /= shrink;
  1759. vcpu->halt_poll_ns = val;
  1760. trace_kvm_halt_poll_ns_shrink(vcpu->vcpu_id, val, old);
  1761. }
  1762. static int kvm_vcpu_check_block(struct kvm_vcpu *vcpu)
  1763. {
  1764. if (kvm_arch_vcpu_runnable(vcpu)) {
  1765. kvm_make_request(KVM_REQ_UNHALT, vcpu);
  1766. return -EINTR;
  1767. }
  1768. if (kvm_cpu_has_pending_timer(vcpu))
  1769. return -EINTR;
  1770. if (signal_pending(current))
  1771. return -EINTR;
  1772. return 0;
  1773. }
  1774. /*
  1775. * The vCPU has executed a HLT instruction with in-kernel mode enabled.
  1776. */
  1777. void kvm_vcpu_block(struct kvm_vcpu *vcpu)
  1778. {
  1779. ktime_t start, cur;
  1780. DECLARE_SWAITQUEUE(wait);
  1781. bool waited = false;
  1782. u64 block_ns;
  1783. start = cur = ktime_get();
  1784. if (vcpu->halt_poll_ns) {
  1785. ktime_t stop = ktime_add_ns(ktime_get(), vcpu->halt_poll_ns);
  1786. ++vcpu->stat.halt_attempted_poll;
  1787. do {
  1788. /*
  1789. * This sets KVM_REQ_UNHALT if an interrupt
  1790. * arrives.
  1791. */
  1792. if (kvm_vcpu_check_block(vcpu) < 0) {
  1793. ++vcpu->stat.halt_successful_poll;
  1794. if (!vcpu_valid_wakeup(vcpu))
  1795. ++vcpu->stat.halt_poll_invalid;
  1796. goto out;
  1797. }
  1798. cur = ktime_get();
  1799. } while (single_task_running() && ktime_before(cur, stop));
  1800. }
  1801. kvm_arch_vcpu_blocking(vcpu);
  1802. for (;;) {
  1803. prepare_to_swait(&vcpu->wq, &wait, TASK_INTERRUPTIBLE);
  1804. if (kvm_vcpu_check_block(vcpu) < 0)
  1805. break;
  1806. waited = true;
  1807. schedule();
  1808. }
  1809. finish_swait(&vcpu->wq, &wait);
  1810. cur = ktime_get();
  1811. kvm_arch_vcpu_unblocking(vcpu);
  1812. out:
  1813. block_ns = ktime_to_ns(cur) - ktime_to_ns(start);
  1814. if (!vcpu_valid_wakeup(vcpu))
  1815. shrink_halt_poll_ns(vcpu);
  1816. else if (halt_poll_ns) {
  1817. if (block_ns <= vcpu->halt_poll_ns)
  1818. ;
  1819. /* we had a long block, shrink polling */
  1820. else if (vcpu->halt_poll_ns && block_ns > halt_poll_ns)
  1821. shrink_halt_poll_ns(vcpu);
  1822. /* we had a short halt and our poll time is too small */
  1823. else if (vcpu->halt_poll_ns < halt_poll_ns &&
  1824. block_ns < halt_poll_ns)
  1825. grow_halt_poll_ns(vcpu);
  1826. } else
  1827. vcpu->halt_poll_ns = 0;
  1828. trace_kvm_vcpu_wakeup(block_ns, waited, vcpu_valid_wakeup(vcpu));
  1829. kvm_arch_vcpu_block_finish(vcpu);
  1830. }
  1831. EXPORT_SYMBOL_GPL(kvm_vcpu_block);
  1832. #ifndef CONFIG_S390
  1833. void kvm_vcpu_wake_up(struct kvm_vcpu *vcpu)
  1834. {
  1835. struct swait_queue_head *wqp;
  1836. wqp = kvm_arch_vcpu_wq(vcpu);
  1837. if (swait_active(wqp)) {
  1838. swake_up(wqp);
  1839. ++vcpu->stat.halt_wakeup;
  1840. }
  1841. }
  1842. EXPORT_SYMBOL_GPL(kvm_vcpu_wake_up);
  1843. /*
  1844. * Kick a sleeping VCPU, or a guest VCPU in guest mode, into host kernel mode.
  1845. */
  1846. void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
  1847. {
  1848. int me;
  1849. int cpu = vcpu->cpu;
  1850. kvm_vcpu_wake_up(vcpu);
  1851. me = get_cpu();
  1852. if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
  1853. if (kvm_arch_vcpu_should_kick(vcpu))
  1854. smp_send_reschedule(cpu);
  1855. put_cpu();
  1856. }
  1857. EXPORT_SYMBOL_GPL(kvm_vcpu_kick);
  1858. #endif /* !CONFIG_S390 */
  1859. int kvm_vcpu_yield_to(struct kvm_vcpu *target)
  1860. {
  1861. struct pid *pid;
  1862. struct task_struct *task = NULL;
  1863. int ret = 0;
  1864. rcu_read_lock();
  1865. pid = rcu_dereference(target->pid);
  1866. if (pid)
  1867. task = get_pid_task(pid, PIDTYPE_PID);
  1868. rcu_read_unlock();
  1869. if (!task)
  1870. return ret;
  1871. ret = yield_to(task, 1);
  1872. put_task_struct(task);
  1873. return ret;
  1874. }
  1875. EXPORT_SYMBOL_GPL(kvm_vcpu_yield_to);
  1876. /*
  1877. * Helper that checks whether a VCPU is eligible for directed yield.
  1878. * Most eligible candidate to yield is decided by following heuristics:
  1879. *
  1880. * (a) VCPU which has not done pl-exit or cpu relax intercepted recently
  1881. * (preempted lock holder), indicated by @in_spin_loop.
  1882. * Set at the beiginning and cleared at the end of interception/PLE handler.
  1883. *
  1884. * (b) VCPU which has done pl-exit/ cpu relax intercepted but did not get
  1885. * chance last time (mostly it has become eligible now since we have probably
  1886. * yielded to lockholder in last iteration. This is done by toggling
  1887. * @dy_eligible each time a VCPU checked for eligibility.)
  1888. *
  1889. * Yielding to a recently pl-exited/cpu relax intercepted VCPU before yielding
  1890. * to preempted lock-holder could result in wrong VCPU selection and CPU
  1891. * burning. Giving priority for a potential lock-holder increases lock
  1892. * progress.
  1893. *
  1894. * Since algorithm is based on heuristics, accessing another VCPU data without
  1895. * locking does not harm. It may result in trying to yield to same VCPU, fail
  1896. * and continue with next VCPU and so on.
  1897. */
  1898. static bool kvm_vcpu_eligible_for_directed_yield(struct kvm_vcpu *vcpu)
  1899. {
  1900. #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
  1901. bool eligible;
  1902. eligible = !vcpu->spin_loop.in_spin_loop ||
  1903. vcpu->spin_loop.dy_eligible;
  1904. if (vcpu->spin_loop.in_spin_loop)
  1905. kvm_vcpu_set_dy_eligible(vcpu, !vcpu->spin_loop.dy_eligible);
  1906. return eligible;
  1907. #else
  1908. return true;
  1909. #endif
  1910. }
  1911. void kvm_vcpu_on_spin(struct kvm_vcpu *me)
  1912. {
  1913. struct kvm *kvm = me->kvm;
  1914. struct kvm_vcpu *vcpu;
  1915. int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
  1916. int yielded = 0;
  1917. int try = 3;
  1918. int pass;
  1919. int i;
  1920. kvm_vcpu_set_in_spin_loop(me, true);
  1921. /*
  1922. * We boost the priority of a VCPU that is runnable but not
  1923. * currently running, because it got preempted by something
  1924. * else and called schedule in __vcpu_run. Hopefully that
  1925. * VCPU is holding the lock that we need and will release it.
  1926. * We approximate round-robin by starting at the last boosted VCPU.
  1927. */
  1928. for (pass = 0; pass < 2 && !yielded && try; pass++) {
  1929. kvm_for_each_vcpu(i, vcpu, kvm) {
  1930. if (!pass && i <= last_boosted_vcpu) {
  1931. i = last_boosted_vcpu;
  1932. continue;
  1933. } else if (pass && i > last_boosted_vcpu)
  1934. break;
  1935. if (!ACCESS_ONCE(vcpu->preempted))
  1936. continue;
  1937. if (vcpu == me)
  1938. continue;
  1939. if (swait_active(&vcpu->wq) && !kvm_arch_vcpu_runnable(vcpu))
  1940. continue;
  1941. if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
  1942. continue;
  1943. yielded = kvm_vcpu_yield_to(vcpu);
  1944. if (yielded > 0) {
  1945. kvm->last_boosted_vcpu = i;
  1946. break;
  1947. } else if (yielded < 0) {
  1948. try--;
  1949. if (!try)
  1950. break;
  1951. }
  1952. }
  1953. }
  1954. kvm_vcpu_set_in_spin_loop(me, false);
  1955. /* Ensure vcpu is not eligible during next spinloop */
  1956. kvm_vcpu_set_dy_eligible(me, false);
  1957. }
  1958. EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
  1959. static int kvm_vcpu_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1960. {
  1961. struct kvm_vcpu *vcpu = vma->vm_file->private_data;
  1962. struct page *page;
  1963. if (vmf->pgoff == 0)
  1964. page = virt_to_page(vcpu->run);
  1965. #ifdef CONFIG_X86
  1966. else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
  1967. page = virt_to_page(vcpu->arch.pio_data);
  1968. #endif
  1969. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  1970. else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
  1971. page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
  1972. #endif
  1973. else
  1974. return kvm_arch_vcpu_fault(vcpu, vmf);
  1975. get_page(page);
  1976. vmf->page = page;
  1977. return 0;
  1978. }
  1979. static const struct vm_operations_struct kvm_vcpu_vm_ops = {
  1980. .fault = kvm_vcpu_fault,
  1981. };
  1982. static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
  1983. {
  1984. vma->vm_ops = &kvm_vcpu_vm_ops;
  1985. return 0;
  1986. }
  1987. static int kvm_vcpu_release(struct inode *inode, struct file *filp)
  1988. {
  1989. struct kvm_vcpu *vcpu = filp->private_data;
  1990. debugfs_remove_recursive(vcpu->debugfs_dentry);
  1991. kvm_put_kvm(vcpu->kvm);
  1992. return 0;
  1993. }
  1994. static struct file_operations kvm_vcpu_fops = {
  1995. .release = kvm_vcpu_release,
  1996. .unlocked_ioctl = kvm_vcpu_ioctl,
  1997. #ifdef CONFIG_KVM_COMPAT
  1998. .compat_ioctl = kvm_vcpu_compat_ioctl,
  1999. #endif
  2000. .mmap = kvm_vcpu_mmap,
  2001. .llseek = noop_llseek,
  2002. };
  2003. /*
  2004. * Allocates an inode for the vcpu.
  2005. */
  2006. static int create_vcpu_fd(struct kvm_vcpu *vcpu)
  2007. {
  2008. return anon_inode_getfd("kvm-vcpu", &kvm_vcpu_fops, vcpu, O_RDWR | O_CLOEXEC);
  2009. }
  2010. static int kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
  2011. {
  2012. char dir_name[ITOA_MAX_LEN * 2];
  2013. int ret;
  2014. if (!kvm_arch_has_vcpu_debugfs())
  2015. return 0;
  2016. if (!debugfs_initialized())
  2017. return 0;
  2018. snprintf(dir_name, sizeof(dir_name), "vcpu%d", vcpu->vcpu_id);
  2019. vcpu->debugfs_dentry = debugfs_create_dir(dir_name,
  2020. vcpu->kvm->debugfs_dentry);
  2021. if (!vcpu->debugfs_dentry)
  2022. return -ENOMEM;
  2023. ret = kvm_arch_create_vcpu_debugfs(vcpu);
  2024. if (ret < 0) {
  2025. debugfs_remove_recursive(vcpu->debugfs_dentry);
  2026. return ret;
  2027. }
  2028. return 0;
  2029. }
  2030. /*
  2031. * Creates some virtual cpus. Good luck creating more than one.
  2032. */
  2033. static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
  2034. {
  2035. int r;
  2036. struct kvm_vcpu *vcpu;
  2037. if (id >= KVM_MAX_VCPU_ID)
  2038. return -EINVAL;
  2039. mutex_lock(&kvm->lock);
  2040. if (kvm->created_vcpus == KVM_MAX_VCPUS) {
  2041. mutex_unlock(&kvm->lock);
  2042. return -EINVAL;
  2043. }
  2044. kvm->created_vcpus++;
  2045. mutex_unlock(&kvm->lock);
  2046. vcpu = kvm_arch_vcpu_create(kvm, id);
  2047. if (IS_ERR(vcpu)) {
  2048. r = PTR_ERR(vcpu);
  2049. goto vcpu_decrement;
  2050. }
  2051. preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
  2052. r = kvm_arch_vcpu_setup(vcpu);
  2053. if (r)
  2054. goto vcpu_destroy;
  2055. r = kvm_create_vcpu_debugfs(vcpu);
  2056. if (r)
  2057. goto vcpu_destroy;
  2058. mutex_lock(&kvm->lock);
  2059. if (kvm_get_vcpu_by_id(kvm, id)) {
  2060. r = -EEXIST;
  2061. goto unlock_vcpu_destroy;
  2062. }
  2063. BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
  2064. /* Now it's all set up, let userspace reach it */
  2065. kvm_get_kvm(kvm);
  2066. r = create_vcpu_fd(vcpu);
  2067. if (r < 0) {
  2068. kvm_put_kvm(kvm);
  2069. goto unlock_vcpu_destroy;
  2070. }
  2071. kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
  2072. /*
  2073. * Pairs with smp_rmb() in kvm_get_vcpu. Write kvm->vcpus
  2074. * before kvm->online_vcpu's incremented value.
  2075. */
  2076. smp_wmb();
  2077. atomic_inc(&kvm->online_vcpus);
  2078. mutex_unlock(&kvm->lock);
  2079. kvm_arch_vcpu_postcreate(vcpu);
  2080. return r;
  2081. unlock_vcpu_destroy:
  2082. mutex_unlock(&kvm->lock);
  2083. debugfs_remove_recursive(vcpu->debugfs_dentry);
  2084. vcpu_destroy:
  2085. kvm_arch_vcpu_destroy(vcpu);
  2086. vcpu_decrement:
  2087. mutex_lock(&kvm->lock);
  2088. kvm->created_vcpus--;
  2089. mutex_unlock(&kvm->lock);
  2090. return r;
  2091. }
  2092. static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
  2093. {
  2094. if (sigset) {
  2095. sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
  2096. vcpu->sigset_active = 1;
  2097. vcpu->sigset = *sigset;
  2098. } else
  2099. vcpu->sigset_active = 0;
  2100. return 0;
  2101. }
  2102. static long kvm_vcpu_ioctl(struct file *filp,
  2103. unsigned int ioctl, unsigned long arg)
  2104. {
  2105. struct kvm_vcpu *vcpu = filp->private_data;
  2106. void __user *argp = (void __user *)arg;
  2107. int r;
  2108. struct kvm_fpu *fpu = NULL;
  2109. struct kvm_sregs *kvm_sregs = NULL;
  2110. if (vcpu->kvm->mm != current->mm)
  2111. return -EIO;
  2112. if (unlikely(_IOC_TYPE(ioctl) != KVMIO))
  2113. return -EINVAL;
  2114. #if defined(CONFIG_S390) || defined(CONFIG_PPC) || defined(CONFIG_MIPS)
  2115. /*
  2116. * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
  2117. * so vcpu_load() would break it.
  2118. */
  2119. if (ioctl == KVM_S390_INTERRUPT || ioctl == KVM_S390_IRQ || ioctl == KVM_INTERRUPT)
  2120. return kvm_arch_vcpu_ioctl(filp, ioctl, arg);
  2121. #endif
  2122. r = vcpu_load(vcpu);
  2123. if (r)
  2124. return r;
  2125. switch (ioctl) {
  2126. case KVM_RUN:
  2127. r = -EINVAL;
  2128. if (arg)
  2129. goto out;
  2130. if (unlikely(vcpu->pid != current->pids[PIDTYPE_PID].pid)) {
  2131. /* The thread running this VCPU changed. */
  2132. struct pid *oldpid = vcpu->pid;
  2133. struct pid *newpid = get_task_pid(current, PIDTYPE_PID);
  2134. rcu_assign_pointer(vcpu->pid, newpid);
  2135. if (oldpid)
  2136. synchronize_rcu();
  2137. put_pid(oldpid);
  2138. }
  2139. r = kvm_arch_vcpu_ioctl_run(vcpu, vcpu->run);
  2140. trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
  2141. break;
  2142. case KVM_GET_REGS: {
  2143. struct kvm_regs *kvm_regs;
  2144. r = -ENOMEM;
  2145. kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
  2146. if (!kvm_regs)
  2147. goto out;
  2148. r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
  2149. if (r)
  2150. goto out_free1;
  2151. r = -EFAULT;
  2152. if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
  2153. goto out_free1;
  2154. r = 0;
  2155. out_free1:
  2156. kfree(kvm_regs);
  2157. break;
  2158. }
  2159. case KVM_SET_REGS: {
  2160. struct kvm_regs *kvm_regs;
  2161. r = -ENOMEM;
  2162. kvm_regs = memdup_user(argp, sizeof(*kvm_regs));
  2163. if (IS_ERR(kvm_regs)) {
  2164. r = PTR_ERR(kvm_regs);
  2165. goto out;
  2166. }
  2167. r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
  2168. kfree(kvm_regs);
  2169. break;
  2170. }
  2171. case KVM_GET_SREGS: {
  2172. kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
  2173. r = -ENOMEM;
  2174. if (!kvm_sregs)
  2175. goto out;
  2176. r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
  2177. if (r)
  2178. goto out;
  2179. r = -EFAULT;
  2180. if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
  2181. goto out;
  2182. r = 0;
  2183. break;
  2184. }
  2185. case KVM_SET_SREGS: {
  2186. kvm_sregs = memdup_user(argp, sizeof(*kvm_sregs));
  2187. if (IS_ERR(kvm_sregs)) {
  2188. r = PTR_ERR(kvm_sregs);
  2189. kvm_sregs = NULL;
  2190. goto out;
  2191. }
  2192. r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
  2193. break;
  2194. }
  2195. case KVM_GET_MP_STATE: {
  2196. struct kvm_mp_state mp_state;
  2197. r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
  2198. if (r)
  2199. goto out;
  2200. r = -EFAULT;
  2201. if (copy_to_user(argp, &mp_state, sizeof(mp_state)))
  2202. goto out;
  2203. r = 0;
  2204. break;
  2205. }
  2206. case KVM_SET_MP_STATE: {
  2207. struct kvm_mp_state mp_state;
  2208. r = -EFAULT;
  2209. if (copy_from_user(&mp_state, argp, sizeof(mp_state)))
  2210. goto out;
  2211. r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
  2212. break;
  2213. }
  2214. case KVM_TRANSLATE: {
  2215. struct kvm_translation tr;
  2216. r = -EFAULT;
  2217. if (copy_from_user(&tr, argp, sizeof(tr)))
  2218. goto out;
  2219. r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
  2220. if (r)
  2221. goto out;
  2222. r = -EFAULT;
  2223. if (copy_to_user(argp, &tr, sizeof(tr)))
  2224. goto out;
  2225. r = 0;
  2226. break;
  2227. }
  2228. case KVM_SET_GUEST_DEBUG: {
  2229. struct kvm_guest_debug dbg;
  2230. r = -EFAULT;
  2231. if (copy_from_user(&dbg, argp, sizeof(dbg)))
  2232. goto out;
  2233. r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
  2234. break;
  2235. }
  2236. case KVM_SET_SIGNAL_MASK: {
  2237. struct kvm_signal_mask __user *sigmask_arg = argp;
  2238. struct kvm_signal_mask kvm_sigmask;
  2239. sigset_t sigset, *p;
  2240. p = NULL;
  2241. if (argp) {
  2242. r = -EFAULT;
  2243. if (copy_from_user(&kvm_sigmask, argp,
  2244. sizeof(kvm_sigmask)))
  2245. goto out;
  2246. r = -EINVAL;
  2247. if (kvm_sigmask.len != sizeof(sigset))
  2248. goto out;
  2249. r = -EFAULT;
  2250. if (copy_from_user(&sigset, sigmask_arg->sigset,
  2251. sizeof(sigset)))
  2252. goto out;
  2253. p = &sigset;
  2254. }
  2255. r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
  2256. break;
  2257. }
  2258. case KVM_GET_FPU: {
  2259. fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
  2260. r = -ENOMEM;
  2261. if (!fpu)
  2262. goto out;
  2263. r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
  2264. if (r)
  2265. goto out;
  2266. r = -EFAULT;
  2267. if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
  2268. goto out;
  2269. r = 0;
  2270. break;
  2271. }
  2272. case KVM_SET_FPU: {
  2273. fpu = memdup_user(argp, sizeof(*fpu));
  2274. if (IS_ERR(fpu)) {
  2275. r = PTR_ERR(fpu);
  2276. fpu = NULL;
  2277. goto out;
  2278. }
  2279. r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
  2280. break;
  2281. }
  2282. default:
  2283. r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
  2284. }
  2285. out:
  2286. vcpu_put(vcpu);
  2287. kfree(fpu);
  2288. kfree(kvm_sregs);
  2289. return r;
  2290. }
  2291. #ifdef CONFIG_KVM_COMPAT
  2292. static long kvm_vcpu_compat_ioctl(struct file *filp,
  2293. unsigned int ioctl, unsigned long arg)
  2294. {
  2295. struct kvm_vcpu *vcpu = filp->private_data;
  2296. void __user *argp = compat_ptr(arg);
  2297. int r;
  2298. if (vcpu->kvm->mm != current->mm)
  2299. return -EIO;
  2300. switch (ioctl) {
  2301. case KVM_SET_SIGNAL_MASK: {
  2302. struct kvm_signal_mask __user *sigmask_arg = argp;
  2303. struct kvm_signal_mask kvm_sigmask;
  2304. compat_sigset_t csigset;
  2305. sigset_t sigset;
  2306. if (argp) {
  2307. r = -EFAULT;
  2308. if (copy_from_user(&kvm_sigmask, argp,
  2309. sizeof(kvm_sigmask)))
  2310. goto out;
  2311. r = -EINVAL;
  2312. if (kvm_sigmask.len != sizeof(csigset))
  2313. goto out;
  2314. r = -EFAULT;
  2315. if (copy_from_user(&csigset, sigmask_arg->sigset,
  2316. sizeof(csigset)))
  2317. goto out;
  2318. sigset_from_compat(&sigset, &csigset);
  2319. r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
  2320. } else
  2321. r = kvm_vcpu_ioctl_set_sigmask(vcpu, NULL);
  2322. break;
  2323. }
  2324. default:
  2325. r = kvm_vcpu_ioctl(filp, ioctl, arg);
  2326. }
  2327. out:
  2328. return r;
  2329. }
  2330. #endif
  2331. static int kvm_device_ioctl_attr(struct kvm_device *dev,
  2332. int (*accessor)(struct kvm_device *dev,
  2333. struct kvm_device_attr *attr),
  2334. unsigned long arg)
  2335. {
  2336. struct kvm_device_attr attr;
  2337. if (!accessor)
  2338. return -EPERM;
  2339. if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
  2340. return -EFAULT;
  2341. return accessor(dev, &attr);
  2342. }
  2343. static long kvm_device_ioctl(struct file *filp, unsigned int ioctl,
  2344. unsigned long arg)
  2345. {
  2346. struct kvm_device *dev = filp->private_data;
  2347. switch (ioctl) {
  2348. case KVM_SET_DEVICE_ATTR:
  2349. return kvm_device_ioctl_attr(dev, dev->ops->set_attr, arg);
  2350. case KVM_GET_DEVICE_ATTR:
  2351. return kvm_device_ioctl_attr(dev, dev->ops->get_attr, arg);
  2352. case KVM_HAS_DEVICE_ATTR:
  2353. return kvm_device_ioctl_attr(dev, dev->ops->has_attr, arg);
  2354. default:
  2355. if (dev->ops->ioctl)
  2356. return dev->ops->ioctl(dev, ioctl, arg);
  2357. return -ENOTTY;
  2358. }
  2359. }
  2360. static int kvm_device_release(struct inode *inode, struct file *filp)
  2361. {
  2362. struct kvm_device *dev = filp->private_data;
  2363. struct kvm *kvm = dev->kvm;
  2364. kvm_put_kvm(kvm);
  2365. return 0;
  2366. }
  2367. static const struct file_operations kvm_device_fops = {
  2368. .unlocked_ioctl = kvm_device_ioctl,
  2369. #ifdef CONFIG_KVM_COMPAT
  2370. .compat_ioctl = kvm_device_ioctl,
  2371. #endif
  2372. .release = kvm_device_release,
  2373. };
  2374. struct kvm_device *kvm_device_from_filp(struct file *filp)
  2375. {
  2376. if (filp->f_op != &kvm_device_fops)
  2377. return NULL;
  2378. return filp->private_data;
  2379. }
  2380. static struct kvm_device_ops *kvm_device_ops_table[KVM_DEV_TYPE_MAX] = {
  2381. #ifdef CONFIG_KVM_MPIC
  2382. [KVM_DEV_TYPE_FSL_MPIC_20] = &kvm_mpic_ops,
  2383. [KVM_DEV_TYPE_FSL_MPIC_42] = &kvm_mpic_ops,
  2384. #endif
  2385. #ifdef CONFIG_KVM_XICS
  2386. [KVM_DEV_TYPE_XICS] = &kvm_xics_ops,
  2387. #endif
  2388. };
  2389. int kvm_register_device_ops(struct kvm_device_ops *ops, u32 type)
  2390. {
  2391. if (type >= ARRAY_SIZE(kvm_device_ops_table))
  2392. return -ENOSPC;
  2393. if (kvm_device_ops_table[type] != NULL)
  2394. return -EEXIST;
  2395. kvm_device_ops_table[type] = ops;
  2396. return 0;
  2397. }
  2398. void kvm_unregister_device_ops(u32 type)
  2399. {
  2400. if (kvm_device_ops_table[type] != NULL)
  2401. kvm_device_ops_table[type] = NULL;
  2402. }
  2403. static int kvm_ioctl_create_device(struct kvm *kvm,
  2404. struct kvm_create_device *cd)
  2405. {
  2406. struct kvm_device_ops *ops = NULL;
  2407. struct kvm_device *dev;
  2408. bool test = cd->flags & KVM_CREATE_DEVICE_TEST;
  2409. int ret;
  2410. if (cd->type >= ARRAY_SIZE(kvm_device_ops_table))
  2411. return -ENODEV;
  2412. ops = kvm_device_ops_table[cd->type];
  2413. if (ops == NULL)
  2414. return -ENODEV;
  2415. if (test)
  2416. return 0;
  2417. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  2418. if (!dev)
  2419. return -ENOMEM;
  2420. dev->ops = ops;
  2421. dev->kvm = kvm;
  2422. mutex_lock(&kvm->lock);
  2423. ret = ops->create(dev, cd->type);
  2424. if (ret < 0) {
  2425. mutex_unlock(&kvm->lock);
  2426. kfree(dev);
  2427. return ret;
  2428. }
  2429. list_add(&dev->vm_node, &kvm->devices);
  2430. mutex_unlock(&kvm->lock);
  2431. if (ops->init)
  2432. ops->init(dev);
  2433. ret = anon_inode_getfd(ops->name, &kvm_device_fops, dev, O_RDWR | O_CLOEXEC);
  2434. if (ret < 0) {
  2435. mutex_lock(&kvm->lock);
  2436. list_del(&dev->vm_node);
  2437. mutex_unlock(&kvm->lock);
  2438. ops->destroy(dev);
  2439. return ret;
  2440. }
  2441. kvm_get_kvm(kvm);
  2442. cd->fd = ret;
  2443. return 0;
  2444. }
  2445. static long kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
  2446. {
  2447. switch (arg) {
  2448. case KVM_CAP_USER_MEMORY:
  2449. case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
  2450. case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
  2451. case KVM_CAP_INTERNAL_ERROR_DATA:
  2452. #ifdef CONFIG_HAVE_KVM_MSI
  2453. case KVM_CAP_SIGNAL_MSI:
  2454. #endif
  2455. #ifdef CONFIG_HAVE_KVM_IRQFD
  2456. case KVM_CAP_IRQFD:
  2457. case KVM_CAP_IRQFD_RESAMPLE:
  2458. #endif
  2459. case KVM_CAP_IOEVENTFD_ANY_LENGTH:
  2460. case KVM_CAP_CHECK_EXTENSION_VM:
  2461. return 1;
  2462. #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
  2463. case KVM_CAP_IRQ_ROUTING:
  2464. return KVM_MAX_IRQ_ROUTES;
  2465. #endif
  2466. #if KVM_ADDRESS_SPACE_NUM > 1
  2467. case KVM_CAP_MULTI_ADDRESS_SPACE:
  2468. return KVM_ADDRESS_SPACE_NUM;
  2469. #endif
  2470. case KVM_CAP_MAX_VCPU_ID:
  2471. return KVM_MAX_VCPU_ID;
  2472. default:
  2473. break;
  2474. }
  2475. return kvm_vm_ioctl_check_extension(kvm, arg);
  2476. }
  2477. static long kvm_vm_ioctl(struct file *filp,
  2478. unsigned int ioctl, unsigned long arg)
  2479. {
  2480. struct kvm *kvm = filp->private_data;
  2481. void __user *argp = (void __user *)arg;
  2482. int r;
  2483. if (kvm->mm != current->mm)
  2484. return -EIO;
  2485. switch (ioctl) {
  2486. case KVM_CREATE_VCPU:
  2487. r = kvm_vm_ioctl_create_vcpu(kvm, arg);
  2488. break;
  2489. case KVM_SET_USER_MEMORY_REGION: {
  2490. struct kvm_userspace_memory_region kvm_userspace_mem;
  2491. r = -EFAULT;
  2492. if (copy_from_user(&kvm_userspace_mem, argp,
  2493. sizeof(kvm_userspace_mem)))
  2494. goto out;
  2495. r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem);
  2496. break;
  2497. }
  2498. case KVM_GET_DIRTY_LOG: {
  2499. struct kvm_dirty_log log;
  2500. r = -EFAULT;
  2501. if (copy_from_user(&log, argp, sizeof(log)))
  2502. goto out;
  2503. r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
  2504. break;
  2505. }
  2506. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  2507. case KVM_REGISTER_COALESCED_MMIO: {
  2508. struct kvm_coalesced_mmio_zone zone;
  2509. r = -EFAULT;
  2510. if (copy_from_user(&zone, argp, sizeof(zone)))
  2511. goto out;
  2512. r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
  2513. break;
  2514. }
  2515. case KVM_UNREGISTER_COALESCED_MMIO: {
  2516. struct kvm_coalesced_mmio_zone zone;
  2517. r = -EFAULT;
  2518. if (copy_from_user(&zone, argp, sizeof(zone)))
  2519. goto out;
  2520. r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
  2521. break;
  2522. }
  2523. #endif
  2524. case KVM_IRQFD: {
  2525. struct kvm_irqfd data;
  2526. r = -EFAULT;
  2527. if (copy_from_user(&data, argp, sizeof(data)))
  2528. goto out;
  2529. r = kvm_irqfd(kvm, &data);
  2530. break;
  2531. }
  2532. case KVM_IOEVENTFD: {
  2533. struct kvm_ioeventfd data;
  2534. r = -EFAULT;
  2535. if (copy_from_user(&data, argp, sizeof(data)))
  2536. goto out;
  2537. r = kvm_ioeventfd(kvm, &data);
  2538. break;
  2539. }
  2540. #ifdef CONFIG_HAVE_KVM_MSI
  2541. case KVM_SIGNAL_MSI: {
  2542. struct kvm_msi msi;
  2543. r = -EFAULT;
  2544. if (copy_from_user(&msi, argp, sizeof(msi)))
  2545. goto out;
  2546. r = kvm_send_userspace_msi(kvm, &msi);
  2547. break;
  2548. }
  2549. #endif
  2550. #ifdef __KVM_HAVE_IRQ_LINE
  2551. case KVM_IRQ_LINE_STATUS:
  2552. case KVM_IRQ_LINE: {
  2553. struct kvm_irq_level irq_event;
  2554. r = -EFAULT;
  2555. if (copy_from_user(&irq_event, argp, sizeof(irq_event)))
  2556. goto out;
  2557. r = kvm_vm_ioctl_irq_line(kvm, &irq_event,
  2558. ioctl == KVM_IRQ_LINE_STATUS);
  2559. if (r)
  2560. goto out;
  2561. r = -EFAULT;
  2562. if (ioctl == KVM_IRQ_LINE_STATUS) {
  2563. if (copy_to_user(argp, &irq_event, sizeof(irq_event)))
  2564. goto out;
  2565. }
  2566. r = 0;
  2567. break;
  2568. }
  2569. #endif
  2570. #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
  2571. case KVM_SET_GSI_ROUTING: {
  2572. struct kvm_irq_routing routing;
  2573. struct kvm_irq_routing __user *urouting;
  2574. struct kvm_irq_routing_entry *entries = NULL;
  2575. r = -EFAULT;
  2576. if (copy_from_user(&routing, argp, sizeof(routing)))
  2577. goto out;
  2578. r = -EINVAL;
  2579. if (routing.nr > KVM_MAX_IRQ_ROUTES)
  2580. goto out;
  2581. if (routing.flags)
  2582. goto out;
  2583. if (routing.nr) {
  2584. r = -ENOMEM;
  2585. entries = vmalloc(routing.nr * sizeof(*entries));
  2586. if (!entries)
  2587. goto out;
  2588. r = -EFAULT;
  2589. urouting = argp;
  2590. if (copy_from_user(entries, urouting->entries,
  2591. routing.nr * sizeof(*entries)))
  2592. goto out_free_irq_routing;
  2593. }
  2594. r = kvm_set_irq_routing(kvm, entries, routing.nr,
  2595. routing.flags);
  2596. out_free_irq_routing:
  2597. vfree(entries);
  2598. break;
  2599. }
  2600. #endif /* CONFIG_HAVE_KVM_IRQ_ROUTING */
  2601. case KVM_CREATE_DEVICE: {
  2602. struct kvm_create_device cd;
  2603. r = -EFAULT;
  2604. if (copy_from_user(&cd, argp, sizeof(cd)))
  2605. goto out;
  2606. r = kvm_ioctl_create_device(kvm, &cd);
  2607. if (r)
  2608. goto out;
  2609. r = -EFAULT;
  2610. if (copy_to_user(argp, &cd, sizeof(cd)))
  2611. goto out;
  2612. r = 0;
  2613. break;
  2614. }
  2615. case KVM_CHECK_EXTENSION:
  2616. r = kvm_vm_ioctl_check_extension_generic(kvm, arg);
  2617. break;
  2618. default:
  2619. r = kvm_arch_vm_ioctl(filp, ioctl, arg);
  2620. }
  2621. out:
  2622. return r;
  2623. }
  2624. #ifdef CONFIG_KVM_COMPAT
  2625. struct compat_kvm_dirty_log {
  2626. __u32 slot;
  2627. __u32 padding1;
  2628. union {
  2629. compat_uptr_t dirty_bitmap; /* one bit per page */
  2630. __u64 padding2;
  2631. };
  2632. };
  2633. static long kvm_vm_compat_ioctl(struct file *filp,
  2634. unsigned int ioctl, unsigned long arg)
  2635. {
  2636. struct kvm *kvm = filp->private_data;
  2637. int r;
  2638. if (kvm->mm != current->mm)
  2639. return -EIO;
  2640. switch (ioctl) {
  2641. case KVM_GET_DIRTY_LOG: {
  2642. struct compat_kvm_dirty_log compat_log;
  2643. struct kvm_dirty_log log;
  2644. r = -EFAULT;
  2645. if (copy_from_user(&compat_log, (void __user *)arg,
  2646. sizeof(compat_log)))
  2647. goto out;
  2648. log.slot = compat_log.slot;
  2649. log.padding1 = compat_log.padding1;
  2650. log.padding2 = compat_log.padding2;
  2651. log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
  2652. r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
  2653. break;
  2654. }
  2655. default:
  2656. r = kvm_vm_ioctl(filp, ioctl, arg);
  2657. }
  2658. out:
  2659. return r;
  2660. }
  2661. #endif
  2662. static struct file_operations kvm_vm_fops = {
  2663. .release = kvm_vm_release,
  2664. .unlocked_ioctl = kvm_vm_ioctl,
  2665. #ifdef CONFIG_KVM_COMPAT
  2666. .compat_ioctl = kvm_vm_compat_ioctl,
  2667. #endif
  2668. .llseek = noop_llseek,
  2669. };
  2670. static int kvm_dev_ioctl_create_vm(unsigned long type)
  2671. {
  2672. int r;
  2673. struct kvm *kvm;
  2674. struct file *file;
  2675. kvm = kvm_create_vm(type);
  2676. if (IS_ERR(kvm))
  2677. return PTR_ERR(kvm);
  2678. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  2679. r = kvm_coalesced_mmio_init(kvm);
  2680. if (r < 0) {
  2681. kvm_put_kvm(kvm);
  2682. return r;
  2683. }
  2684. #endif
  2685. r = get_unused_fd_flags(O_CLOEXEC);
  2686. if (r < 0) {
  2687. kvm_put_kvm(kvm);
  2688. return r;
  2689. }
  2690. file = anon_inode_getfile("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
  2691. if (IS_ERR(file)) {
  2692. put_unused_fd(r);
  2693. kvm_put_kvm(kvm);
  2694. return PTR_ERR(file);
  2695. }
  2696. if (kvm_create_vm_debugfs(kvm, r) < 0) {
  2697. put_unused_fd(r);
  2698. fput(file);
  2699. return -ENOMEM;
  2700. }
  2701. fd_install(r, file);
  2702. return r;
  2703. }
  2704. static long kvm_dev_ioctl(struct file *filp,
  2705. unsigned int ioctl, unsigned long arg)
  2706. {
  2707. long r = -EINVAL;
  2708. switch (ioctl) {
  2709. case KVM_GET_API_VERSION:
  2710. if (arg)
  2711. goto out;
  2712. r = KVM_API_VERSION;
  2713. break;
  2714. case KVM_CREATE_VM:
  2715. r = kvm_dev_ioctl_create_vm(arg);
  2716. break;
  2717. case KVM_CHECK_EXTENSION:
  2718. r = kvm_vm_ioctl_check_extension_generic(NULL, arg);
  2719. break;
  2720. case KVM_GET_VCPU_MMAP_SIZE:
  2721. if (arg)
  2722. goto out;
  2723. r = PAGE_SIZE; /* struct kvm_run */
  2724. #ifdef CONFIG_X86
  2725. r += PAGE_SIZE; /* pio data page */
  2726. #endif
  2727. #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
  2728. r += PAGE_SIZE; /* coalesced mmio ring page */
  2729. #endif
  2730. break;
  2731. case KVM_TRACE_ENABLE:
  2732. case KVM_TRACE_PAUSE:
  2733. case KVM_TRACE_DISABLE:
  2734. r = -EOPNOTSUPP;
  2735. break;
  2736. default:
  2737. return kvm_arch_dev_ioctl(filp, ioctl, arg);
  2738. }
  2739. out:
  2740. return r;
  2741. }
  2742. static struct file_operations kvm_chardev_ops = {
  2743. .unlocked_ioctl = kvm_dev_ioctl,
  2744. .compat_ioctl = kvm_dev_ioctl,
  2745. .llseek = noop_llseek,
  2746. };
  2747. static struct miscdevice kvm_dev = {
  2748. KVM_MINOR,
  2749. "kvm",
  2750. &kvm_chardev_ops,
  2751. };
  2752. static void hardware_enable_nolock(void *junk)
  2753. {
  2754. int cpu = raw_smp_processor_id();
  2755. int r;
  2756. if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
  2757. return;
  2758. cpumask_set_cpu(cpu, cpus_hardware_enabled);
  2759. r = kvm_arch_hardware_enable();
  2760. if (r) {
  2761. cpumask_clear_cpu(cpu, cpus_hardware_enabled);
  2762. atomic_inc(&hardware_enable_failed);
  2763. pr_info("kvm: enabling virtualization on CPU%d failed\n", cpu);
  2764. }
  2765. }
  2766. static int kvm_starting_cpu(unsigned int cpu)
  2767. {
  2768. raw_spin_lock(&kvm_count_lock);
  2769. if (kvm_usage_count)
  2770. hardware_enable_nolock(NULL);
  2771. raw_spin_unlock(&kvm_count_lock);
  2772. return 0;
  2773. }
  2774. static void hardware_disable_nolock(void *junk)
  2775. {
  2776. int cpu = raw_smp_processor_id();
  2777. if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
  2778. return;
  2779. cpumask_clear_cpu(cpu, cpus_hardware_enabled);
  2780. kvm_arch_hardware_disable();
  2781. }
  2782. static int kvm_dying_cpu(unsigned int cpu)
  2783. {
  2784. raw_spin_lock(&kvm_count_lock);
  2785. if (kvm_usage_count)
  2786. hardware_disable_nolock(NULL);
  2787. raw_spin_unlock(&kvm_count_lock);
  2788. return 0;
  2789. }
  2790. static void hardware_disable_all_nolock(void)
  2791. {
  2792. BUG_ON(!kvm_usage_count);
  2793. kvm_usage_count--;
  2794. if (!kvm_usage_count)
  2795. on_each_cpu(hardware_disable_nolock, NULL, 1);
  2796. }
  2797. static void hardware_disable_all(void)
  2798. {
  2799. raw_spin_lock(&kvm_count_lock);
  2800. hardware_disable_all_nolock();
  2801. raw_spin_unlock(&kvm_count_lock);
  2802. }
  2803. static int hardware_enable_all(void)
  2804. {
  2805. int r = 0;
  2806. raw_spin_lock(&kvm_count_lock);
  2807. kvm_usage_count++;
  2808. if (kvm_usage_count == 1) {
  2809. atomic_set(&hardware_enable_failed, 0);
  2810. on_each_cpu(hardware_enable_nolock, NULL, 1);
  2811. if (atomic_read(&hardware_enable_failed)) {
  2812. hardware_disable_all_nolock();
  2813. r = -EBUSY;
  2814. }
  2815. }
  2816. raw_spin_unlock(&kvm_count_lock);
  2817. return r;
  2818. }
  2819. static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
  2820. void *v)
  2821. {
  2822. /*
  2823. * Some (well, at least mine) BIOSes hang on reboot if
  2824. * in vmx root mode.
  2825. *
  2826. * And Intel TXT required VMX off for all cpu when system shutdown.
  2827. */
  2828. pr_info("kvm: exiting hardware virtualization\n");
  2829. kvm_rebooting = true;
  2830. on_each_cpu(hardware_disable_nolock, NULL, 1);
  2831. return NOTIFY_OK;
  2832. }
  2833. static struct notifier_block kvm_reboot_notifier = {
  2834. .notifier_call = kvm_reboot,
  2835. .priority = 0,
  2836. };
  2837. static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
  2838. {
  2839. int i;
  2840. for (i = 0; i < bus->dev_count; i++) {
  2841. struct kvm_io_device *pos = bus->range[i].dev;
  2842. kvm_iodevice_destructor(pos);
  2843. }
  2844. kfree(bus);
  2845. }
  2846. static inline int kvm_io_bus_cmp(const struct kvm_io_range *r1,
  2847. const struct kvm_io_range *r2)
  2848. {
  2849. gpa_t addr1 = r1->addr;
  2850. gpa_t addr2 = r2->addr;
  2851. if (addr1 < addr2)
  2852. return -1;
  2853. /* If r2->len == 0, match the exact address. If r2->len != 0,
  2854. * accept any overlapping write. Any order is acceptable for
  2855. * overlapping ranges, because kvm_io_bus_get_first_dev ensures
  2856. * we process all of them.
  2857. */
  2858. if (r2->len) {
  2859. addr1 += r1->len;
  2860. addr2 += r2->len;
  2861. }
  2862. if (addr1 > addr2)
  2863. return 1;
  2864. return 0;
  2865. }
  2866. static int kvm_io_bus_sort_cmp(const void *p1, const void *p2)
  2867. {
  2868. return kvm_io_bus_cmp(p1, p2);
  2869. }
  2870. static int kvm_io_bus_insert_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev,
  2871. gpa_t addr, int len)
  2872. {
  2873. bus->range[bus->dev_count++] = (struct kvm_io_range) {
  2874. .addr = addr,
  2875. .len = len,
  2876. .dev = dev,
  2877. };
  2878. sort(bus->range, bus->dev_count, sizeof(struct kvm_io_range),
  2879. kvm_io_bus_sort_cmp, NULL);
  2880. return 0;
  2881. }
  2882. static int kvm_io_bus_get_first_dev(struct kvm_io_bus *bus,
  2883. gpa_t addr, int len)
  2884. {
  2885. struct kvm_io_range *range, key;
  2886. int off;
  2887. key = (struct kvm_io_range) {
  2888. .addr = addr,
  2889. .len = len,
  2890. };
  2891. range = bsearch(&key, bus->range, bus->dev_count,
  2892. sizeof(struct kvm_io_range), kvm_io_bus_sort_cmp);
  2893. if (range == NULL)
  2894. return -ENOENT;
  2895. off = range - bus->range;
  2896. while (off > 0 && kvm_io_bus_cmp(&key, &bus->range[off-1]) == 0)
  2897. off--;
  2898. return off;
  2899. }
  2900. static int __kvm_io_bus_write(struct kvm_vcpu *vcpu, struct kvm_io_bus *bus,
  2901. struct kvm_io_range *range, const void *val)
  2902. {
  2903. int idx;
  2904. idx = kvm_io_bus_get_first_dev(bus, range->addr, range->len);
  2905. if (idx < 0)
  2906. return -EOPNOTSUPP;
  2907. while (idx < bus->dev_count &&
  2908. kvm_io_bus_cmp(range, &bus->range[idx]) == 0) {
  2909. if (!kvm_iodevice_write(vcpu, bus->range[idx].dev, range->addr,
  2910. range->len, val))
  2911. return idx;
  2912. idx++;
  2913. }
  2914. return -EOPNOTSUPP;
  2915. }
  2916. /* kvm_io_bus_write - called under kvm->slots_lock */
  2917. int kvm_io_bus_write(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
  2918. int len, const void *val)
  2919. {
  2920. struct kvm_io_bus *bus;
  2921. struct kvm_io_range range;
  2922. int r;
  2923. range = (struct kvm_io_range) {
  2924. .addr = addr,
  2925. .len = len,
  2926. };
  2927. bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu);
  2928. r = __kvm_io_bus_write(vcpu, bus, &range, val);
  2929. return r < 0 ? r : 0;
  2930. }
  2931. /* kvm_io_bus_write_cookie - called under kvm->slots_lock */
  2932. int kvm_io_bus_write_cookie(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx,
  2933. gpa_t addr, int len, const void *val, long cookie)
  2934. {
  2935. struct kvm_io_bus *bus;
  2936. struct kvm_io_range range;
  2937. range = (struct kvm_io_range) {
  2938. .addr = addr,
  2939. .len = len,
  2940. };
  2941. bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu);
  2942. /* First try the device referenced by cookie. */
  2943. if ((cookie >= 0) && (cookie < bus->dev_count) &&
  2944. (kvm_io_bus_cmp(&range, &bus->range[cookie]) == 0))
  2945. if (!kvm_iodevice_write(vcpu, bus->range[cookie].dev, addr, len,
  2946. val))
  2947. return cookie;
  2948. /*
  2949. * cookie contained garbage; fall back to search and return the
  2950. * correct cookie value.
  2951. */
  2952. return __kvm_io_bus_write(vcpu, bus, &range, val);
  2953. }
  2954. static int __kvm_io_bus_read(struct kvm_vcpu *vcpu, struct kvm_io_bus *bus,
  2955. struct kvm_io_range *range, void *val)
  2956. {
  2957. int idx;
  2958. idx = kvm_io_bus_get_first_dev(bus, range->addr, range->len);
  2959. if (idx < 0)
  2960. return -EOPNOTSUPP;
  2961. while (idx < bus->dev_count &&
  2962. kvm_io_bus_cmp(range, &bus->range[idx]) == 0) {
  2963. if (!kvm_iodevice_read(vcpu, bus->range[idx].dev, range->addr,
  2964. range->len, val))
  2965. return idx;
  2966. idx++;
  2967. }
  2968. return -EOPNOTSUPP;
  2969. }
  2970. EXPORT_SYMBOL_GPL(kvm_io_bus_write);
  2971. /* kvm_io_bus_read - called under kvm->slots_lock */
  2972. int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
  2973. int len, void *val)
  2974. {
  2975. struct kvm_io_bus *bus;
  2976. struct kvm_io_range range;
  2977. int r;
  2978. range = (struct kvm_io_range) {
  2979. .addr = addr,
  2980. .len = len,
  2981. };
  2982. bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu);
  2983. r = __kvm_io_bus_read(vcpu, bus, &range, val);
  2984. return r < 0 ? r : 0;
  2985. }
  2986. /* Caller must hold slots_lock. */
  2987. int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
  2988. int len, struct kvm_io_device *dev)
  2989. {
  2990. struct kvm_io_bus *new_bus, *bus;
  2991. bus = kvm->buses[bus_idx];
  2992. /* exclude ioeventfd which is limited by maximum fd */
  2993. if (bus->dev_count - bus->ioeventfd_count > NR_IOBUS_DEVS - 1)
  2994. return -ENOSPC;
  2995. new_bus = kmalloc(sizeof(*bus) + ((bus->dev_count + 1) *
  2996. sizeof(struct kvm_io_range)), GFP_KERNEL);
  2997. if (!new_bus)
  2998. return -ENOMEM;
  2999. memcpy(new_bus, bus, sizeof(*bus) + (bus->dev_count *
  3000. sizeof(struct kvm_io_range)));
  3001. kvm_io_bus_insert_dev(new_bus, dev, addr, len);
  3002. rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
  3003. synchronize_srcu_expedited(&kvm->srcu);
  3004. kfree(bus);
  3005. return 0;
  3006. }
  3007. /* Caller must hold slots_lock. */
  3008. int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
  3009. struct kvm_io_device *dev)
  3010. {
  3011. int i, r;
  3012. struct kvm_io_bus *new_bus, *bus;
  3013. bus = kvm->buses[bus_idx];
  3014. r = -ENOENT;
  3015. for (i = 0; i < bus->dev_count; i++)
  3016. if (bus->range[i].dev == dev) {
  3017. r = 0;
  3018. break;
  3019. }
  3020. if (r)
  3021. return r;
  3022. new_bus = kmalloc(sizeof(*bus) + ((bus->dev_count - 1) *
  3023. sizeof(struct kvm_io_range)), GFP_KERNEL);
  3024. if (!new_bus)
  3025. return -ENOMEM;
  3026. memcpy(new_bus, bus, sizeof(*bus) + i * sizeof(struct kvm_io_range));
  3027. new_bus->dev_count--;
  3028. memcpy(new_bus->range + i, bus->range + i + 1,
  3029. (new_bus->dev_count - i) * sizeof(struct kvm_io_range));
  3030. rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
  3031. synchronize_srcu_expedited(&kvm->srcu);
  3032. kfree(bus);
  3033. return r;
  3034. }
  3035. struct kvm_io_device *kvm_io_bus_get_dev(struct kvm *kvm, enum kvm_bus bus_idx,
  3036. gpa_t addr)
  3037. {
  3038. struct kvm_io_bus *bus;
  3039. int dev_idx, srcu_idx;
  3040. struct kvm_io_device *iodev = NULL;
  3041. srcu_idx = srcu_read_lock(&kvm->srcu);
  3042. bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
  3043. dev_idx = kvm_io_bus_get_first_dev(bus, addr, 1);
  3044. if (dev_idx < 0)
  3045. goto out_unlock;
  3046. iodev = bus->range[dev_idx].dev;
  3047. out_unlock:
  3048. srcu_read_unlock(&kvm->srcu, srcu_idx);
  3049. return iodev;
  3050. }
  3051. EXPORT_SYMBOL_GPL(kvm_io_bus_get_dev);
  3052. static int kvm_debugfs_open(struct inode *inode, struct file *file,
  3053. int (*get)(void *, u64 *), int (*set)(void *, u64),
  3054. const char *fmt)
  3055. {
  3056. struct kvm_stat_data *stat_data = (struct kvm_stat_data *)
  3057. inode->i_private;
  3058. /* The debugfs files are a reference to the kvm struct which
  3059. * is still valid when kvm_destroy_vm is called.
  3060. * To avoid the race between open and the removal of the debugfs
  3061. * directory we test against the users count.
  3062. */
  3063. if (!atomic_add_unless(&stat_data->kvm->users_count, 1, 0))
  3064. return -ENOENT;
  3065. if (simple_attr_open(inode, file, get, set, fmt)) {
  3066. kvm_put_kvm(stat_data->kvm);
  3067. return -ENOMEM;
  3068. }
  3069. return 0;
  3070. }
  3071. static int kvm_debugfs_release(struct inode *inode, struct file *file)
  3072. {
  3073. struct kvm_stat_data *stat_data = (struct kvm_stat_data *)
  3074. inode->i_private;
  3075. simple_attr_release(inode, file);
  3076. kvm_put_kvm(stat_data->kvm);
  3077. return 0;
  3078. }
  3079. static int vm_stat_get_per_vm(void *data, u64 *val)
  3080. {
  3081. struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data;
  3082. *val = *(ulong *)((void *)stat_data->kvm + stat_data->offset);
  3083. return 0;
  3084. }
  3085. static int vm_stat_clear_per_vm(void *data, u64 val)
  3086. {
  3087. struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data;
  3088. if (val)
  3089. return -EINVAL;
  3090. *(ulong *)((void *)stat_data->kvm + stat_data->offset) = 0;
  3091. return 0;
  3092. }
  3093. static int vm_stat_get_per_vm_open(struct inode *inode, struct file *file)
  3094. {
  3095. __simple_attr_check_format("%llu\n", 0ull);
  3096. return kvm_debugfs_open(inode, file, vm_stat_get_per_vm,
  3097. vm_stat_clear_per_vm, "%llu\n");
  3098. }
  3099. static const struct file_operations vm_stat_get_per_vm_fops = {
  3100. .owner = THIS_MODULE,
  3101. .open = vm_stat_get_per_vm_open,
  3102. .release = kvm_debugfs_release,
  3103. .read = simple_attr_read,
  3104. .write = simple_attr_write,
  3105. .llseek = generic_file_llseek,
  3106. };
  3107. static int vcpu_stat_get_per_vm(void *data, u64 *val)
  3108. {
  3109. int i;
  3110. struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data;
  3111. struct kvm_vcpu *vcpu;
  3112. *val = 0;
  3113. kvm_for_each_vcpu(i, vcpu, stat_data->kvm)
  3114. *val += *(u64 *)((void *)vcpu + stat_data->offset);
  3115. return 0;
  3116. }
  3117. static int vcpu_stat_clear_per_vm(void *data, u64 val)
  3118. {
  3119. int i;
  3120. struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data;
  3121. struct kvm_vcpu *vcpu;
  3122. if (val)
  3123. return -EINVAL;
  3124. kvm_for_each_vcpu(i, vcpu, stat_data->kvm)
  3125. *(u64 *)((void *)vcpu + stat_data->offset) = 0;
  3126. return 0;
  3127. }
  3128. static int vcpu_stat_get_per_vm_open(struct inode *inode, struct file *file)
  3129. {
  3130. __simple_attr_check_format("%llu\n", 0ull);
  3131. return kvm_debugfs_open(inode, file, vcpu_stat_get_per_vm,
  3132. vcpu_stat_clear_per_vm, "%llu\n");
  3133. }
  3134. static const struct file_operations vcpu_stat_get_per_vm_fops = {
  3135. .owner = THIS_MODULE,
  3136. .open = vcpu_stat_get_per_vm_open,
  3137. .release = kvm_debugfs_release,
  3138. .read = simple_attr_read,
  3139. .write = simple_attr_write,
  3140. .llseek = generic_file_llseek,
  3141. };
  3142. static const struct file_operations *stat_fops_per_vm[] = {
  3143. [KVM_STAT_VCPU] = &vcpu_stat_get_per_vm_fops,
  3144. [KVM_STAT_VM] = &vm_stat_get_per_vm_fops,
  3145. };
  3146. static int vm_stat_get(void *_offset, u64 *val)
  3147. {
  3148. unsigned offset = (long)_offset;
  3149. struct kvm *kvm;
  3150. struct kvm_stat_data stat_tmp = {.offset = offset};
  3151. u64 tmp_val;
  3152. *val = 0;
  3153. spin_lock(&kvm_lock);
  3154. list_for_each_entry(kvm, &vm_list, vm_list) {
  3155. stat_tmp.kvm = kvm;
  3156. vm_stat_get_per_vm((void *)&stat_tmp, &tmp_val);
  3157. *val += tmp_val;
  3158. }
  3159. spin_unlock(&kvm_lock);
  3160. return 0;
  3161. }
  3162. static int vm_stat_clear(void *_offset, u64 val)
  3163. {
  3164. unsigned offset = (long)_offset;
  3165. struct kvm *kvm;
  3166. struct kvm_stat_data stat_tmp = {.offset = offset};
  3167. if (val)
  3168. return -EINVAL;
  3169. spin_lock(&kvm_lock);
  3170. list_for_each_entry(kvm, &vm_list, vm_list) {
  3171. stat_tmp.kvm = kvm;
  3172. vm_stat_clear_per_vm((void *)&stat_tmp, 0);
  3173. }
  3174. spin_unlock(&kvm_lock);
  3175. return 0;
  3176. }
  3177. DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, vm_stat_clear, "%llu\n");
  3178. static int vcpu_stat_get(void *_offset, u64 *val)
  3179. {
  3180. unsigned offset = (long)_offset;
  3181. struct kvm *kvm;
  3182. struct kvm_stat_data stat_tmp = {.offset = offset};
  3183. u64 tmp_val;
  3184. *val = 0;
  3185. spin_lock(&kvm_lock);
  3186. list_for_each_entry(kvm, &vm_list, vm_list) {
  3187. stat_tmp.kvm = kvm;
  3188. vcpu_stat_get_per_vm((void *)&stat_tmp, &tmp_val);
  3189. *val += tmp_val;
  3190. }
  3191. spin_unlock(&kvm_lock);
  3192. return 0;
  3193. }
  3194. static int vcpu_stat_clear(void *_offset, u64 val)
  3195. {
  3196. unsigned offset = (long)_offset;
  3197. struct kvm *kvm;
  3198. struct kvm_stat_data stat_tmp = {.offset = offset};
  3199. if (val)
  3200. return -EINVAL;
  3201. spin_lock(&kvm_lock);
  3202. list_for_each_entry(kvm, &vm_list, vm_list) {
  3203. stat_tmp.kvm = kvm;
  3204. vcpu_stat_clear_per_vm((void *)&stat_tmp, 0);
  3205. }
  3206. spin_unlock(&kvm_lock);
  3207. return 0;
  3208. }
  3209. DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, vcpu_stat_clear,
  3210. "%llu\n");
  3211. static const struct file_operations *stat_fops[] = {
  3212. [KVM_STAT_VCPU] = &vcpu_stat_fops,
  3213. [KVM_STAT_VM] = &vm_stat_fops,
  3214. };
  3215. static int kvm_init_debug(void)
  3216. {
  3217. int r = -EEXIST;
  3218. struct kvm_stats_debugfs_item *p;
  3219. kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
  3220. if (kvm_debugfs_dir == NULL)
  3221. goto out;
  3222. kvm_debugfs_num_entries = 0;
  3223. for (p = debugfs_entries; p->name; ++p, kvm_debugfs_num_entries++) {
  3224. if (!debugfs_create_file(p->name, 0644, kvm_debugfs_dir,
  3225. (void *)(long)p->offset,
  3226. stat_fops[p->kind]))
  3227. goto out_dir;
  3228. }
  3229. return 0;
  3230. out_dir:
  3231. debugfs_remove_recursive(kvm_debugfs_dir);
  3232. out:
  3233. return r;
  3234. }
  3235. static int kvm_suspend(void)
  3236. {
  3237. if (kvm_usage_count)
  3238. hardware_disable_nolock(NULL);
  3239. return 0;
  3240. }
  3241. static void kvm_resume(void)
  3242. {
  3243. if (kvm_usage_count) {
  3244. WARN_ON(raw_spin_is_locked(&kvm_count_lock));
  3245. hardware_enable_nolock(NULL);
  3246. }
  3247. }
  3248. static struct syscore_ops kvm_syscore_ops = {
  3249. .suspend = kvm_suspend,
  3250. .resume = kvm_resume,
  3251. };
  3252. static inline
  3253. struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
  3254. {
  3255. return container_of(pn, struct kvm_vcpu, preempt_notifier);
  3256. }
  3257. static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
  3258. {
  3259. struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
  3260. if (vcpu->preempted)
  3261. vcpu->preempted = false;
  3262. kvm_arch_sched_in(vcpu, cpu);
  3263. kvm_arch_vcpu_load(vcpu, cpu);
  3264. }
  3265. static void kvm_sched_out(struct preempt_notifier *pn,
  3266. struct task_struct *next)
  3267. {
  3268. struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
  3269. if (current->state == TASK_RUNNING)
  3270. vcpu->preempted = true;
  3271. kvm_arch_vcpu_put(vcpu);
  3272. }
  3273. int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
  3274. struct module *module)
  3275. {
  3276. int r;
  3277. int cpu;
  3278. r = kvm_arch_init(opaque);
  3279. if (r)
  3280. goto out_fail;
  3281. /*
  3282. * kvm_arch_init makes sure there's at most one caller
  3283. * for architectures that support multiple implementations,
  3284. * like intel and amd on x86.
  3285. * kvm_arch_init must be called before kvm_irqfd_init to avoid creating
  3286. * conflicts in case kvm is already setup for another implementation.
  3287. */
  3288. r = kvm_irqfd_init();
  3289. if (r)
  3290. goto out_irqfd;
  3291. if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
  3292. r = -ENOMEM;
  3293. goto out_free_0;
  3294. }
  3295. r = kvm_arch_hardware_setup();
  3296. if (r < 0)
  3297. goto out_free_0a;
  3298. for_each_online_cpu(cpu) {
  3299. smp_call_function_single(cpu,
  3300. kvm_arch_check_processor_compat,
  3301. &r, 1);
  3302. if (r < 0)
  3303. goto out_free_1;
  3304. }
  3305. r = cpuhp_setup_state_nocalls(CPUHP_AP_KVM_STARTING, "AP_KVM_STARTING",
  3306. kvm_starting_cpu, kvm_dying_cpu);
  3307. if (r)
  3308. goto out_free_2;
  3309. register_reboot_notifier(&kvm_reboot_notifier);
  3310. /* A kmem cache lets us meet the alignment requirements of fx_save. */
  3311. if (!vcpu_align)
  3312. vcpu_align = __alignof__(struct kvm_vcpu);
  3313. kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size, vcpu_align,
  3314. 0, NULL);
  3315. if (!kvm_vcpu_cache) {
  3316. r = -ENOMEM;
  3317. goto out_free_3;
  3318. }
  3319. r = kvm_async_pf_init();
  3320. if (r)
  3321. goto out_free;
  3322. kvm_chardev_ops.owner = module;
  3323. kvm_vm_fops.owner = module;
  3324. kvm_vcpu_fops.owner = module;
  3325. r = misc_register(&kvm_dev);
  3326. if (r) {
  3327. pr_err("kvm: misc device register failed\n");
  3328. goto out_unreg;
  3329. }
  3330. register_syscore_ops(&kvm_syscore_ops);
  3331. kvm_preempt_ops.sched_in = kvm_sched_in;
  3332. kvm_preempt_ops.sched_out = kvm_sched_out;
  3333. r = kvm_init_debug();
  3334. if (r) {
  3335. pr_err("kvm: create debugfs files failed\n");
  3336. goto out_undebugfs;
  3337. }
  3338. r = kvm_vfio_ops_init();
  3339. WARN_ON(r);
  3340. return 0;
  3341. out_undebugfs:
  3342. unregister_syscore_ops(&kvm_syscore_ops);
  3343. misc_deregister(&kvm_dev);
  3344. out_unreg:
  3345. kvm_async_pf_deinit();
  3346. out_free:
  3347. kmem_cache_destroy(kvm_vcpu_cache);
  3348. out_free_3:
  3349. unregister_reboot_notifier(&kvm_reboot_notifier);
  3350. cpuhp_remove_state_nocalls(CPUHP_AP_KVM_STARTING);
  3351. out_free_2:
  3352. out_free_1:
  3353. kvm_arch_hardware_unsetup();
  3354. out_free_0a:
  3355. free_cpumask_var(cpus_hardware_enabled);
  3356. out_free_0:
  3357. kvm_irqfd_exit();
  3358. out_irqfd:
  3359. kvm_arch_exit();
  3360. out_fail:
  3361. return r;
  3362. }
  3363. EXPORT_SYMBOL_GPL(kvm_init);
  3364. void kvm_exit(void)
  3365. {
  3366. debugfs_remove_recursive(kvm_debugfs_dir);
  3367. misc_deregister(&kvm_dev);
  3368. kmem_cache_destroy(kvm_vcpu_cache);
  3369. kvm_async_pf_deinit();
  3370. unregister_syscore_ops(&kvm_syscore_ops);
  3371. unregister_reboot_notifier(&kvm_reboot_notifier);
  3372. cpuhp_remove_state_nocalls(CPUHP_AP_KVM_STARTING);
  3373. on_each_cpu(hardware_disable_nolock, NULL, 1);
  3374. kvm_arch_hardware_unsetup();
  3375. kvm_arch_exit();
  3376. kvm_irqfd_exit();
  3377. free_cpumask_var(cpus_hardware_enabled);
  3378. kvm_vfio_ops_exit();
  3379. }
  3380. EXPORT_SYMBOL_GPL(kvm_exit);