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