kvm_main.c 96 KB

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