x86.c 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * derived from drivers/kvm/kvm_main.c
  5. *
  6. * Copyright (C) 2006 Qumranet, Inc.
  7. * Copyright (C) 2008 Qumranet, Inc.
  8. * Copyright IBM Corporation, 2008
  9. *
  10. * Authors:
  11. * Avi Kivity <avi@qumranet.com>
  12. * Yaniv Kamay <yaniv@qumranet.com>
  13. * Amit Shah <amit.shah@qumranet.com>
  14. * Ben-Ami Yassour <benami@il.ibm.com>
  15. *
  16. * This work is licensed under the terms of the GNU GPL, version 2. See
  17. * the COPYING file in the top-level directory.
  18. *
  19. */
  20. #include <linux/kvm_host.h>
  21. #include "irq.h"
  22. #include "mmu.h"
  23. #include "i8254.h"
  24. #include "tss.h"
  25. #include "kvm_cache_regs.h"
  26. #include "x86.h"
  27. #include <linux/clocksource.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/kvm.h>
  30. #include <linux/fs.h>
  31. #include <linux/pci.h>
  32. #include <linux/vmalloc.h>
  33. #include <linux/module.h>
  34. #include <linux/mman.h>
  35. #include <linux/highmem.h>
  36. #include <linux/intel-iommu.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/msr.h>
  39. #include <asm/desc.h>
  40. #define MAX_IO_MSRS 256
  41. #define CR0_RESERVED_BITS \
  42. (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
  43. | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
  44. | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
  45. #define CR4_RESERVED_BITS \
  46. (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
  47. | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
  48. | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
  49. | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
  50. #define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
  51. /* EFER defaults:
  52. * - enable syscall per default because its emulated by KVM
  53. * - enable LME and LMA per default on 64 bit KVM
  54. */
  55. #ifdef CONFIG_X86_64
  56. static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffafeULL;
  57. #else
  58. static u64 __read_mostly efer_reserved_bits = 0xfffffffffffffffeULL;
  59. #endif
  60. #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
  61. #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
  62. static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
  63. struct kvm_cpuid_entry2 __user *entries);
  64. struct kvm_x86_ops *kvm_x86_ops;
  65. EXPORT_SYMBOL_GPL(kvm_x86_ops);
  66. struct kvm_stats_debugfs_item debugfs_entries[] = {
  67. { "pf_fixed", VCPU_STAT(pf_fixed) },
  68. { "pf_guest", VCPU_STAT(pf_guest) },
  69. { "tlb_flush", VCPU_STAT(tlb_flush) },
  70. { "invlpg", VCPU_STAT(invlpg) },
  71. { "exits", VCPU_STAT(exits) },
  72. { "io_exits", VCPU_STAT(io_exits) },
  73. { "mmio_exits", VCPU_STAT(mmio_exits) },
  74. { "signal_exits", VCPU_STAT(signal_exits) },
  75. { "irq_window", VCPU_STAT(irq_window_exits) },
  76. { "nmi_window", VCPU_STAT(nmi_window_exits) },
  77. { "halt_exits", VCPU_STAT(halt_exits) },
  78. { "halt_wakeup", VCPU_STAT(halt_wakeup) },
  79. { "hypercalls", VCPU_STAT(hypercalls) },
  80. { "request_irq", VCPU_STAT(request_irq_exits) },
  81. { "irq_exits", VCPU_STAT(irq_exits) },
  82. { "host_state_reload", VCPU_STAT(host_state_reload) },
  83. { "efer_reload", VCPU_STAT(efer_reload) },
  84. { "fpu_reload", VCPU_STAT(fpu_reload) },
  85. { "insn_emulation", VCPU_STAT(insn_emulation) },
  86. { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
  87. { "irq_injections", VCPU_STAT(irq_injections) },
  88. { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
  89. { "mmu_pte_write", VM_STAT(mmu_pte_write) },
  90. { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
  91. { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
  92. { "mmu_flooded", VM_STAT(mmu_flooded) },
  93. { "mmu_recycled", VM_STAT(mmu_recycled) },
  94. { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
  95. { "mmu_unsync", VM_STAT(mmu_unsync) },
  96. { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
  97. { "largepages", VM_STAT(lpages) },
  98. { NULL }
  99. };
  100. static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
  101. int assigned_dev_id)
  102. {
  103. struct list_head *ptr;
  104. struct kvm_assigned_dev_kernel *match;
  105. list_for_each(ptr, head) {
  106. match = list_entry(ptr, struct kvm_assigned_dev_kernel, list);
  107. if (match->assigned_dev_id == assigned_dev_id)
  108. return match;
  109. }
  110. return NULL;
  111. }
  112. static void kvm_assigned_dev_interrupt_work_handler(struct work_struct *work)
  113. {
  114. struct kvm_assigned_dev_kernel *assigned_dev;
  115. assigned_dev = container_of(work, struct kvm_assigned_dev_kernel,
  116. interrupt_work);
  117. /* This is taken to safely inject irq inside the guest. When
  118. * the interrupt injection (or the ioapic code) uses a
  119. * finer-grained lock, update this
  120. */
  121. mutex_lock(&assigned_dev->kvm->lock);
  122. kvm_set_irq(assigned_dev->kvm,
  123. assigned_dev->guest_irq, 1);
  124. mutex_unlock(&assigned_dev->kvm->lock);
  125. kvm_put_kvm(assigned_dev->kvm);
  126. }
  127. /* FIXME: Implement the OR logic needed to make shared interrupts on
  128. * this line behave properly
  129. */
  130. static irqreturn_t kvm_assigned_dev_intr(int irq, void *dev_id)
  131. {
  132. struct kvm_assigned_dev_kernel *assigned_dev =
  133. (struct kvm_assigned_dev_kernel *) dev_id;
  134. kvm_get_kvm(assigned_dev->kvm);
  135. schedule_work(&assigned_dev->interrupt_work);
  136. disable_irq_nosync(irq);
  137. return IRQ_HANDLED;
  138. }
  139. /* Ack the irq line for an assigned device */
  140. static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
  141. {
  142. struct kvm_assigned_dev_kernel *dev;
  143. if (kian->gsi == -1)
  144. return;
  145. dev = container_of(kian, struct kvm_assigned_dev_kernel,
  146. ack_notifier);
  147. kvm_set_irq(dev->kvm, dev->guest_irq, 0);
  148. enable_irq(dev->host_irq);
  149. }
  150. static void kvm_free_assigned_device(struct kvm *kvm,
  151. struct kvm_assigned_dev_kernel
  152. *assigned_dev)
  153. {
  154. if (irqchip_in_kernel(kvm) && assigned_dev->irq_requested)
  155. free_irq(assigned_dev->host_irq, (void *)assigned_dev);
  156. kvm_unregister_irq_ack_notifier(kvm, &assigned_dev->ack_notifier);
  157. if (cancel_work_sync(&assigned_dev->interrupt_work))
  158. /* We had pending work. That means we will have to take
  159. * care of kvm_put_kvm.
  160. */
  161. kvm_put_kvm(kvm);
  162. pci_release_regions(assigned_dev->dev);
  163. pci_disable_device(assigned_dev->dev);
  164. pci_dev_put(assigned_dev->dev);
  165. list_del(&assigned_dev->list);
  166. kfree(assigned_dev);
  167. }
  168. static void kvm_free_all_assigned_devices(struct kvm *kvm)
  169. {
  170. struct list_head *ptr, *ptr2;
  171. struct kvm_assigned_dev_kernel *assigned_dev;
  172. list_for_each_safe(ptr, ptr2, &kvm->arch.assigned_dev_head) {
  173. assigned_dev = list_entry(ptr,
  174. struct kvm_assigned_dev_kernel,
  175. list);
  176. kvm_free_assigned_device(kvm, assigned_dev);
  177. }
  178. }
  179. static int kvm_vm_ioctl_assign_irq(struct kvm *kvm,
  180. struct kvm_assigned_irq
  181. *assigned_irq)
  182. {
  183. int r = 0;
  184. struct kvm_assigned_dev_kernel *match;
  185. mutex_lock(&kvm->lock);
  186. match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  187. assigned_irq->assigned_dev_id);
  188. if (!match) {
  189. mutex_unlock(&kvm->lock);
  190. return -EINVAL;
  191. }
  192. if (match->irq_requested) {
  193. match->guest_irq = assigned_irq->guest_irq;
  194. match->ack_notifier.gsi = assigned_irq->guest_irq;
  195. mutex_unlock(&kvm->lock);
  196. return 0;
  197. }
  198. INIT_WORK(&match->interrupt_work,
  199. kvm_assigned_dev_interrupt_work_handler);
  200. if (irqchip_in_kernel(kvm)) {
  201. if (!capable(CAP_SYS_RAWIO)) {
  202. r = -EPERM;
  203. goto out_release;
  204. }
  205. if (assigned_irq->host_irq)
  206. match->host_irq = assigned_irq->host_irq;
  207. else
  208. match->host_irq = match->dev->irq;
  209. match->guest_irq = assigned_irq->guest_irq;
  210. match->ack_notifier.gsi = assigned_irq->guest_irq;
  211. match->ack_notifier.irq_acked = kvm_assigned_dev_ack_irq;
  212. kvm_register_irq_ack_notifier(kvm, &match->ack_notifier);
  213. /* Even though this is PCI, we don't want to use shared
  214. * interrupts. Sharing host devices with guest-assigned devices
  215. * on the same interrupt line is not a happy situation: there
  216. * are going to be long delays in accepting, acking, etc.
  217. */
  218. if (request_irq(match->host_irq, kvm_assigned_dev_intr, 0,
  219. "kvm_assigned_device", (void *)match)) {
  220. r = -EIO;
  221. goto out_release;
  222. }
  223. }
  224. match->irq_requested = true;
  225. mutex_unlock(&kvm->lock);
  226. return r;
  227. out_release:
  228. mutex_unlock(&kvm->lock);
  229. kvm_free_assigned_device(kvm, match);
  230. return r;
  231. }
  232. static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
  233. struct kvm_assigned_pci_dev *assigned_dev)
  234. {
  235. int r = 0;
  236. struct kvm_assigned_dev_kernel *match;
  237. struct pci_dev *dev;
  238. mutex_lock(&kvm->lock);
  239. match = kvm_find_assigned_dev(&kvm->arch.assigned_dev_head,
  240. assigned_dev->assigned_dev_id);
  241. if (match) {
  242. /* device already assigned */
  243. r = -EINVAL;
  244. goto out;
  245. }
  246. match = kzalloc(sizeof(struct kvm_assigned_dev_kernel), GFP_KERNEL);
  247. if (match == NULL) {
  248. printk(KERN_INFO "%s: Couldn't allocate memory\n",
  249. __func__);
  250. r = -ENOMEM;
  251. goto out;
  252. }
  253. dev = pci_get_bus_and_slot(assigned_dev->busnr,
  254. assigned_dev->devfn);
  255. if (!dev) {
  256. printk(KERN_INFO "%s: host device not found\n", __func__);
  257. r = -EINVAL;
  258. goto out_free;
  259. }
  260. if (pci_enable_device(dev)) {
  261. printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
  262. r = -EBUSY;
  263. goto out_put;
  264. }
  265. r = pci_request_regions(dev, "kvm_assigned_device");
  266. if (r) {
  267. printk(KERN_INFO "%s: Could not get access to device regions\n",
  268. __func__);
  269. goto out_disable;
  270. }
  271. match->assigned_dev_id = assigned_dev->assigned_dev_id;
  272. match->host_busnr = assigned_dev->busnr;
  273. match->host_devfn = assigned_dev->devfn;
  274. match->dev = dev;
  275. match->kvm = kvm;
  276. list_add(&match->list, &kvm->arch.assigned_dev_head);
  277. if (assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU) {
  278. r = kvm_iommu_map_guest(kvm, match);
  279. if (r)
  280. goto out_list_del;
  281. }
  282. out:
  283. mutex_unlock(&kvm->lock);
  284. return r;
  285. out_list_del:
  286. list_del(&match->list);
  287. pci_release_regions(dev);
  288. out_disable:
  289. pci_disable_device(dev);
  290. out_put:
  291. pci_dev_put(dev);
  292. out_free:
  293. kfree(match);
  294. mutex_unlock(&kvm->lock);
  295. return r;
  296. }
  297. unsigned long segment_base(u16 selector)
  298. {
  299. struct descriptor_table gdt;
  300. struct desc_struct *d;
  301. unsigned long table_base;
  302. unsigned long v;
  303. if (selector == 0)
  304. return 0;
  305. asm("sgdt %0" : "=m"(gdt));
  306. table_base = gdt.base;
  307. if (selector & 4) { /* from ldt */
  308. u16 ldt_selector;
  309. asm("sldt %0" : "=g"(ldt_selector));
  310. table_base = segment_base(ldt_selector);
  311. }
  312. d = (struct desc_struct *)(table_base + (selector & ~7));
  313. v = d->base0 | ((unsigned long)d->base1 << 16) |
  314. ((unsigned long)d->base2 << 24);
  315. #ifdef CONFIG_X86_64
  316. if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
  317. v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
  318. #endif
  319. return v;
  320. }
  321. EXPORT_SYMBOL_GPL(segment_base);
  322. u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
  323. {
  324. if (irqchip_in_kernel(vcpu->kvm))
  325. return vcpu->arch.apic_base;
  326. else
  327. return vcpu->arch.apic_base;
  328. }
  329. EXPORT_SYMBOL_GPL(kvm_get_apic_base);
  330. void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data)
  331. {
  332. /* TODO: reserve bits check */
  333. if (irqchip_in_kernel(vcpu->kvm))
  334. kvm_lapic_set_base(vcpu, data);
  335. else
  336. vcpu->arch.apic_base = data;
  337. }
  338. EXPORT_SYMBOL_GPL(kvm_set_apic_base);
  339. void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
  340. {
  341. WARN_ON(vcpu->arch.exception.pending);
  342. vcpu->arch.exception.pending = true;
  343. vcpu->arch.exception.has_error_code = false;
  344. vcpu->arch.exception.nr = nr;
  345. }
  346. EXPORT_SYMBOL_GPL(kvm_queue_exception);
  347. void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long addr,
  348. u32 error_code)
  349. {
  350. ++vcpu->stat.pf_guest;
  351. if (vcpu->arch.exception.pending) {
  352. if (vcpu->arch.exception.nr == PF_VECTOR) {
  353. printk(KERN_DEBUG "kvm: inject_page_fault:"
  354. " double fault 0x%lx\n", addr);
  355. vcpu->arch.exception.nr = DF_VECTOR;
  356. vcpu->arch.exception.error_code = 0;
  357. } else if (vcpu->arch.exception.nr == DF_VECTOR) {
  358. /* triple fault -> shutdown */
  359. set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
  360. }
  361. return;
  362. }
  363. vcpu->arch.cr2 = addr;
  364. kvm_queue_exception_e(vcpu, PF_VECTOR, error_code);
  365. }
  366. void kvm_inject_nmi(struct kvm_vcpu *vcpu)
  367. {
  368. vcpu->arch.nmi_pending = 1;
  369. }
  370. EXPORT_SYMBOL_GPL(kvm_inject_nmi);
  371. void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
  372. {
  373. WARN_ON(vcpu->arch.exception.pending);
  374. vcpu->arch.exception.pending = true;
  375. vcpu->arch.exception.has_error_code = true;
  376. vcpu->arch.exception.nr = nr;
  377. vcpu->arch.exception.error_code = error_code;
  378. }
  379. EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
  380. static void __queue_exception(struct kvm_vcpu *vcpu)
  381. {
  382. kvm_x86_ops->queue_exception(vcpu, vcpu->arch.exception.nr,
  383. vcpu->arch.exception.has_error_code,
  384. vcpu->arch.exception.error_code);
  385. }
  386. /*
  387. * Load the pae pdptrs. Return true is they are all valid.
  388. */
  389. int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
  390. {
  391. gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
  392. unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
  393. int i;
  394. int ret;
  395. u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
  396. ret = kvm_read_guest_page(vcpu->kvm, pdpt_gfn, pdpte,
  397. offset * sizeof(u64), sizeof(pdpte));
  398. if (ret < 0) {
  399. ret = 0;
  400. goto out;
  401. }
  402. for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
  403. if ((pdpte[i] & 1) && (pdpte[i] & 0xfffffff0000001e6ull)) {
  404. ret = 0;
  405. goto out;
  406. }
  407. }
  408. ret = 1;
  409. memcpy(vcpu->arch.pdptrs, pdpte, sizeof(vcpu->arch.pdptrs));
  410. out:
  411. return ret;
  412. }
  413. EXPORT_SYMBOL_GPL(load_pdptrs);
  414. static bool pdptrs_changed(struct kvm_vcpu *vcpu)
  415. {
  416. u64 pdpte[ARRAY_SIZE(vcpu->arch.pdptrs)];
  417. bool changed = true;
  418. int r;
  419. if (is_long_mode(vcpu) || !is_pae(vcpu))
  420. return false;
  421. r = kvm_read_guest(vcpu->kvm, vcpu->arch.cr3 & ~31u, pdpte, sizeof(pdpte));
  422. if (r < 0)
  423. goto out;
  424. changed = memcmp(pdpte, vcpu->arch.pdptrs, sizeof(pdpte)) != 0;
  425. out:
  426. return changed;
  427. }
  428. void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  429. {
  430. if (cr0 & CR0_RESERVED_BITS) {
  431. printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
  432. cr0, vcpu->arch.cr0);
  433. kvm_inject_gp(vcpu, 0);
  434. return;
  435. }
  436. if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) {
  437. printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
  438. kvm_inject_gp(vcpu, 0);
  439. return;
  440. }
  441. if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) {
  442. printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
  443. "and a clear PE flag\n");
  444. kvm_inject_gp(vcpu, 0);
  445. return;
  446. }
  447. if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
  448. #ifdef CONFIG_X86_64
  449. if ((vcpu->arch.shadow_efer & EFER_LME)) {
  450. int cs_db, cs_l;
  451. if (!is_pae(vcpu)) {
  452. printk(KERN_DEBUG "set_cr0: #GP, start paging "
  453. "in long mode while PAE is disabled\n");
  454. kvm_inject_gp(vcpu, 0);
  455. return;
  456. }
  457. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  458. if (cs_l) {
  459. printk(KERN_DEBUG "set_cr0: #GP, start paging "
  460. "in long mode while CS.L == 1\n");
  461. kvm_inject_gp(vcpu, 0);
  462. return;
  463. }
  464. } else
  465. #endif
  466. if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
  467. printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
  468. "reserved bits\n");
  469. kvm_inject_gp(vcpu, 0);
  470. return;
  471. }
  472. }
  473. kvm_x86_ops->set_cr0(vcpu, cr0);
  474. vcpu->arch.cr0 = cr0;
  475. kvm_mmu_reset_context(vcpu);
  476. return;
  477. }
  478. EXPORT_SYMBOL_GPL(kvm_set_cr0);
  479. void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
  480. {
  481. kvm_set_cr0(vcpu, (vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f));
  482. KVMTRACE_1D(LMSW, vcpu,
  483. (u32)((vcpu->arch.cr0 & ~0x0ful) | (msw & 0x0f)),
  484. handler);
  485. }
  486. EXPORT_SYMBOL_GPL(kvm_lmsw);
  487. void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  488. {
  489. if (cr4 & CR4_RESERVED_BITS) {
  490. printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
  491. kvm_inject_gp(vcpu, 0);
  492. return;
  493. }
  494. if (is_long_mode(vcpu)) {
  495. if (!(cr4 & X86_CR4_PAE)) {
  496. printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
  497. "in long mode\n");
  498. kvm_inject_gp(vcpu, 0);
  499. return;
  500. }
  501. } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & X86_CR4_PAE)
  502. && !load_pdptrs(vcpu, vcpu->arch.cr3)) {
  503. printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
  504. kvm_inject_gp(vcpu, 0);
  505. return;
  506. }
  507. if (cr4 & X86_CR4_VMXE) {
  508. printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
  509. kvm_inject_gp(vcpu, 0);
  510. return;
  511. }
  512. kvm_x86_ops->set_cr4(vcpu, cr4);
  513. vcpu->arch.cr4 = cr4;
  514. kvm_mmu_reset_context(vcpu);
  515. }
  516. EXPORT_SYMBOL_GPL(kvm_set_cr4);
  517. void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  518. {
  519. if (cr3 == vcpu->arch.cr3 && !pdptrs_changed(vcpu)) {
  520. kvm_mmu_sync_roots(vcpu);
  521. kvm_mmu_flush_tlb(vcpu);
  522. return;
  523. }
  524. if (is_long_mode(vcpu)) {
  525. if (cr3 & CR3_L_MODE_RESERVED_BITS) {
  526. printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
  527. kvm_inject_gp(vcpu, 0);
  528. return;
  529. }
  530. } else {
  531. if (is_pae(vcpu)) {
  532. if (cr3 & CR3_PAE_RESERVED_BITS) {
  533. printk(KERN_DEBUG
  534. "set_cr3: #GP, reserved bits\n");
  535. kvm_inject_gp(vcpu, 0);
  536. return;
  537. }
  538. if (is_paging(vcpu) && !load_pdptrs(vcpu, cr3)) {
  539. printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
  540. "reserved bits\n");
  541. kvm_inject_gp(vcpu, 0);
  542. return;
  543. }
  544. }
  545. /*
  546. * We don't check reserved bits in nonpae mode, because
  547. * this isn't enforced, and VMware depends on this.
  548. */
  549. }
  550. /*
  551. * Does the new cr3 value map to physical memory? (Note, we
  552. * catch an invalid cr3 even in real-mode, because it would
  553. * cause trouble later on when we turn on paging anyway.)
  554. *
  555. * A real CPU would silently accept an invalid cr3 and would
  556. * attempt to use it - with largely undefined (and often hard
  557. * to debug) behavior on the guest side.
  558. */
  559. if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
  560. kvm_inject_gp(vcpu, 0);
  561. else {
  562. vcpu->arch.cr3 = cr3;
  563. vcpu->arch.mmu.new_cr3(vcpu);
  564. }
  565. }
  566. EXPORT_SYMBOL_GPL(kvm_set_cr3);
  567. void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
  568. {
  569. if (cr8 & CR8_RESERVED_BITS) {
  570. printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
  571. kvm_inject_gp(vcpu, 0);
  572. return;
  573. }
  574. if (irqchip_in_kernel(vcpu->kvm))
  575. kvm_lapic_set_tpr(vcpu, cr8);
  576. else
  577. vcpu->arch.cr8 = cr8;
  578. }
  579. EXPORT_SYMBOL_GPL(kvm_set_cr8);
  580. unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
  581. {
  582. if (irqchip_in_kernel(vcpu->kvm))
  583. return kvm_lapic_get_cr8(vcpu);
  584. else
  585. return vcpu->arch.cr8;
  586. }
  587. EXPORT_SYMBOL_GPL(kvm_get_cr8);
  588. /*
  589. * List of msr numbers which we expose to userspace through KVM_GET_MSRS
  590. * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
  591. *
  592. * This list is modified at module load time to reflect the
  593. * capabilities of the host cpu.
  594. */
  595. static u32 msrs_to_save[] = {
  596. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  597. MSR_K6_STAR,
  598. #ifdef CONFIG_X86_64
  599. MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
  600. #endif
  601. MSR_IA32_TIME_STAMP_COUNTER, MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
  602. MSR_IA32_PERF_STATUS,
  603. };
  604. static unsigned num_msrs_to_save;
  605. static u32 emulated_msrs[] = {
  606. MSR_IA32_MISC_ENABLE,
  607. };
  608. static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
  609. {
  610. if (efer & efer_reserved_bits) {
  611. printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
  612. efer);
  613. kvm_inject_gp(vcpu, 0);
  614. return;
  615. }
  616. if (is_paging(vcpu)
  617. && (vcpu->arch.shadow_efer & EFER_LME) != (efer & EFER_LME)) {
  618. printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
  619. kvm_inject_gp(vcpu, 0);
  620. return;
  621. }
  622. kvm_x86_ops->set_efer(vcpu, efer);
  623. efer &= ~EFER_LMA;
  624. efer |= vcpu->arch.shadow_efer & EFER_LMA;
  625. vcpu->arch.shadow_efer = efer;
  626. }
  627. void kvm_enable_efer_bits(u64 mask)
  628. {
  629. efer_reserved_bits &= ~mask;
  630. }
  631. EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
  632. /*
  633. * Writes msr value into into the appropriate "register".
  634. * Returns 0 on success, non-0 otherwise.
  635. * Assumes vcpu_load() was already called.
  636. */
  637. int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  638. {
  639. return kvm_x86_ops->set_msr(vcpu, msr_index, data);
  640. }
  641. /*
  642. * Adapt set_msr() to msr_io()'s calling convention
  643. */
  644. static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
  645. {
  646. return kvm_set_msr(vcpu, index, *data);
  647. }
  648. static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
  649. {
  650. static int version;
  651. struct pvclock_wall_clock wc;
  652. struct timespec now, sys, boot;
  653. if (!wall_clock)
  654. return;
  655. version++;
  656. kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
  657. /*
  658. * The guest calculates current wall clock time by adding
  659. * system time (updated by kvm_write_guest_time below) to the
  660. * wall clock specified here. guest system time equals host
  661. * system time for us, thus we must fill in host boot time here.
  662. */
  663. now = current_kernel_time();
  664. ktime_get_ts(&sys);
  665. boot = ns_to_timespec(timespec_to_ns(&now) - timespec_to_ns(&sys));
  666. wc.sec = boot.tv_sec;
  667. wc.nsec = boot.tv_nsec;
  668. wc.version = version;
  669. kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
  670. version++;
  671. kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
  672. }
  673. static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
  674. {
  675. uint32_t quotient, remainder;
  676. /* Don't try to replace with do_div(), this one calculates
  677. * "(dividend << 32) / divisor" */
  678. __asm__ ( "divl %4"
  679. : "=a" (quotient), "=d" (remainder)
  680. : "0" (0), "1" (dividend), "r" (divisor) );
  681. return quotient;
  682. }
  683. static void kvm_set_time_scale(uint32_t tsc_khz, struct pvclock_vcpu_time_info *hv_clock)
  684. {
  685. uint64_t nsecs = 1000000000LL;
  686. int32_t shift = 0;
  687. uint64_t tps64;
  688. uint32_t tps32;
  689. tps64 = tsc_khz * 1000LL;
  690. while (tps64 > nsecs*2) {
  691. tps64 >>= 1;
  692. shift--;
  693. }
  694. tps32 = (uint32_t)tps64;
  695. while (tps32 <= (uint32_t)nsecs) {
  696. tps32 <<= 1;
  697. shift++;
  698. }
  699. hv_clock->tsc_shift = shift;
  700. hv_clock->tsc_to_system_mul = div_frac(nsecs, tps32);
  701. pr_debug("%s: tsc_khz %u, tsc_shift %d, tsc_mul %u\n",
  702. __FUNCTION__, tsc_khz, hv_clock->tsc_shift,
  703. hv_clock->tsc_to_system_mul);
  704. }
  705. static void kvm_write_guest_time(struct kvm_vcpu *v)
  706. {
  707. struct timespec ts;
  708. unsigned long flags;
  709. struct kvm_vcpu_arch *vcpu = &v->arch;
  710. void *shared_kaddr;
  711. if ((!vcpu->time_page))
  712. return;
  713. if (unlikely(vcpu->hv_clock_tsc_khz != tsc_khz)) {
  714. kvm_set_time_scale(tsc_khz, &vcpu->hv_clock);
  715. vcpu->hv_clock_tsc_khz = tsc_khz;
  716. }
  717. /* Keep irq disabled to prevent changes to the clock */
  718. local_irq_save(flags);
  719. kvm_get_msr(v, MSR_IA32_TIME_STAMP_COUNTER,
  720. &vcpu->hv_clock.tsc_timestamp);
  721. ktime_get_ts(&ts);
  722. local_irq_restore(flags);
  723. /* With all the info we got, fill in the values */
  724. vcpu->hv_clock.system_time = ts.tv_nsec +
  725. (NSEC_PER_SEC * (u64)ts.tv_sec);
  726. /*
  727. * The interface expects us to write an even number signaling that the
  728. * update is finished. Since the guest won't see the intermediate
  729. * state, we just increase by 2 at the end.
  730. */
  731. vcpu->hv_clock.version += 2;
  732. shared_kaddr = kmap_atomic(vcpu->time_page, KM_USER0);
  733. memcpy(shared_kaddr + vcpu->time_offset, &vcpu->hv_clock,
  734. sizeof(vcpu->hv_clock));
  735. kunmap_atomic(shared_kaddr, KM_USER0);
  736. mark_page_dirty(v->kvm, vcpu->time >> PAGE_SHIFT);
  737. }
  738. static bool msr_mtrr_valid(unsigned msr)
  739. {
  740. switch (msr) {
  741. case 0x200 ... 0x200 + 2 * KVM_NR_VAR_MTRR - 1:
  742. case MSR_MTRRfix64K_00000:
  743. case MSR_MTRRfix16K_80000:
  744. case MSR_MTRRfix16K_A0000:
  745. case MSR_MTRRfix4K_C0000:
  746. case MSR_MTRRfix4K_C8000:
  747. case MSR_MTRRfix4K_D0000:
  748. case MSR_MTRRfix4K_D8000:
  749. case MSR_MTRRfix4K_E0000:
  750. case MSR_MTRRfix4K_E8000:
  751. case MSR_MTRRfix4K_F0000:
  752. case MSR_MTRRfix4K_F8000:
  753. case MSR_MTRRdefType:
  754. case MSR_IA32_CR_PAT:
  755. return true;
  756. case 0x2f8:
  757. return true;
  758. }
  759. return false;
  760. }
  761. static int set_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  762. {
  763. if (!msr_mtrr_valid(msr))
  764. return 1;
  765. vcpu->arch.mtrr[msr - 0x200] = data;
  766. return 0;
  767. }
  768. int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  769. {
  770. switch (msr) {
  771. case MSR_EFER:
  772. set_efer(vcpu, data);
  773. break;
  774. case MSR_IA32_MC0_STATUS:
  775. pr_unimpl(vcpu, "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
  776. __func__, data);
  777. break;
  778. case MSR_IA32_MCG_STATUS:
  779. pr_unimpl(vcpu, "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
  780. __func__, data);
  781. break;
  782. case MSR_IA32_MCG_CTL:
  783. pr_unimpl(vcpu, "%s: MSR_IA32_MCG_CTL 0x%llx, nop\n",
  784. __func__, data);
  785. break;
  786. case MSR_IA32_DEBUGCTLMSR:
  787. if (!data) {
  788. /* We support the non-activated case already */
  789. break;
  790. } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
  791. /* Values other than LBR and BTF are vendor-specific,
  792. thus reserved and should throw a #GP */
  793. return 1;
  794. }
  795. pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
  796. __func__, data);
  797. break;
  798. case MSR_IA32_UCODE_REV:
  799. case MSR_IA32_UCODE_WRITE:
  800. break;
  801. case 0x200 ... 0x2ff:
  802. return set_msr_mtrr(vcpu, msr, data);
  803. case MSR_IA32_APICBASE:
  804. kvm_set_apic_base(vcpu, data);
  805. break;
  806. case MSR_IA32_MISC_ENABLE:
  807. vcpu->arch.ia32_misc_enable_msr = data;
  808. break;
  809. case MSR_KVM_WALL_CLOCK:
  810. vcpu->kvm->arch.wall_clock = data;
  811. kvm_write_wall_clock(vcpu->kvm, data);
  812. break;
  813. case MSR_KVM_SYSTEM_TIME: {
  814. if (vcpu->arch.time_page) {
  815. kvm_release_page_dirty(vcpu->arch.time_page);
  816. vcpu->arch.time_page = NULL;
  817. }
  818. vcpu->arch.time = data;
  819. /* we verify if the enable bit is set... */
  820. if (!(data & 1))
  821. break;
  822. /* ...but clean it before doing the actual write */
  823. vcpu->arch.time_offset = data & ~(PAGE_MASK | 1);
  824. vcpu->arch.time_page =
  825. gfn_to_page(vcpu->kvm, data >> PAGE_SHIFT);
  826. if (is_error_page(vcpu->arch.time_page)) {
  827. kvm_release_page_clean(vcpu->arch.time_page);
  828. vcpu->arch.time_page = NULL;
  829. }
  830. kvm_write_guest_time(vcpu);
  831. break;
  832. }
  833. default:
  834. pr_unimpl(vcpu, "unhandled wrmsr: 0x%x data %llx\n", msr, data);
  835. return 1;
  836. }
  837. return 0;
  838. }
  839. EXPORT_SYMBOL_GPL(kvm_set_msr_common);
  840. /*
  841. * Reads an msr value (of 'msr_index') into 'pdata'.
  842. * Returns 0 on success, non-0 otherwise.
  843. * Assumes vcpu_load() was already called.
  844. */
  845. int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  846. {
  847. return kvm_x86_ops->get_msr(vcpu, msr_index, pdata);
  848. }
  849. static int get_msr_mtrr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  850. {
  851. if (!msr_mtrr_valid(msr))
  852. return 1;
  853. *pdata = vcpu->arch.mtrr[msr - 0x200];
  854. return 0;
  855. }
  856. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  857. {
  858. u64 data;
  859. switch (msr) {
  860. case 0xc0010010: /* SYSCFG */
  861. case 0xc0010015: /* HWCR */
  862. case MSR_IA32_PLATFORM_ID:
  863. case MSR_IA32_P5_MC_ADDR:
  864. case MSR_IA32_P5_MC_TYPE:
  865. case MSR_IA32_MC0_CTL:
  866. case MSR_IA32_MCG_STATUS:
  867. case MSR_IA32_MCG_CAP:
  868. case MSR_IA32_MCG_CTL:
  869. case MSR_IA32_MC0_MISC:
  870. case MSR_IA32_MC0_MISC+4:
  871. case MSR_IA32_MC0_MISC+8:
  872. case MSR_IA32_MC0_MISC+12:
  873. case MSR_IA32_MC0_MISC+16:
  874. case MSR_IA32_MC0_MISC+20:
  875. case MSR_IA32_UCODE_REV:
  876. case MSR_IA32_EBL_CR_POWERON:
  877. case MSR_IA32_DEBUGCTLMSR:
  878. case MSR_IA32_LASTBRANCHFROMIP:
  879. case MSR_IA32_LASTBRANCHTOIP:
  880. case MSR_IA32_LASTINTFROMIP:
  881. case MSR_IA32_LASTINTTOIP:
  882. data = 0;
  883. break;
  884. case MSR_MTRRcap:
  885. data = 0x500 | KVM_NR_VAR_MTRR;
  886. break;
  887. case 0x200 ... 0x2ff:
  888. return get_msr_mtrr(vcpu, msr, pdata);
  889. case 0xcd: /* fsb frequency */
  890. data = 3;
  891. break;
  892. case MSR_IA32_APICBASE:
  893. data = kvm_get_apic_base(vcpu);
  894. break;
  895. case MSR_IA32_MISC_ENABLE:
  896. data = vcpu->arch.ia32_misc_enable_msr;
  897. break;
  898. case MSR_IA32_PERF_STATUS:
  899. /* TSC increment by tick */
  900. data = 1000ULL;
  901. /* CPU multiplier */
  902. data |= (((uint64_t)4ULL) << 40);
  903. break;
  904. case MSR_EFER:
  905. data = vcpu->arch.shadow_efer;
  906. break;
  907. case MSR_KVM_WALL_CLOCK:
  908. data = vcpu->kvm->arch.wall_clock;
  909. break;
  910. case MSR_KVM_SYSTEM_TIME:
  911. data = vcpu->arch.time;
  912. break;
  913. default:
  914. pr_unimpl(vcpu, "unhandled rdmsr: 0x%x\n", msr);
  915. return 1;
  916. }
  917. *pdata = data;
  918. return 0;
  919. }
  920. EXPORT_SYMBOL_GPL(kvm_get_msr_common);
  921. /*
  922. * Read or write a bunch of msrs. All parameters are kernel addresses.
  923. *
  924. * @return number of msrs set successfully.
  925. */
  926. static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
  927. struct kvm_msr_entry *entries,
  928. int (*do_msr)(struct kvm_vcpu *vcpu,
  929. unsigned index, u64 *data))
  930. {
  931. int i;
  932. vcpu_load(vcpu);
  933. down_read(&vcpu->kvm->slots_lock);
  934. for (i = 0; i < msrs->nmsrs; ++i)
  935. if (do_msr(vcpu, entries[i].index, &entries[i].data))
  936. break;
  937. up_read(&vcpu->kvm->slots_lock);
  938. vcpu_put(vcpu);
  939. return i;
  940. }
  941. /*
  942. * Read or write a bunch of msrs. Parameters are user addresses.
  943. *
  944. * @return number of msrs set successfully.
  945. */
  946. static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
  947. int (*do_msr)(struct kvm_vcpu *vcpu,
  948. unsigned index, u64 *data),
  949. int writeback)
  950. {
  951. struct kvm_msrs msrs;
  952. struct kvm_msr_entry *entries;
  953. int r, n;
  954. unsigned size;
  955. r = -EFAULT;
  956. if (copy_from_user(&msrs, user_msrs, sizeof msrs))
  957. goto out;
  958. r = -E2BIG;
  959. if (msrs.nmsrs >= MAX_IO_MSRS)
  960. goto out;
  961. r = -ENOMEM;
  962. size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
  963. entries = vmalloc(size);
  964. if (!entries)
  965. goto out;
  966. r = -EFAULT;
  967. if (copy_from_user(entries, user_msrs->entries, size))
  968. goto out_free;
  969. r = n = __msr_io(vcpu, &msrs, entries, do_msr);
  970. if (r < 0)
  971. goto out_free;
  972. r = -EFAULT;
  973. if (writeback && copy_to_user(user_msrs->entries, entries, size))
  974. goto out_free;
  975. r = n;
  976. out_free:
  977. vfree(entries);
  978. out:
  979. return r;
  980. }
  981. int kvm_dev_ioctl_check_extension(long ext)
  982. {
  983. int r;
  984. switch (ext) {
  985. case KVM_CAP_IRQCHIP:
  986. case KVM_CAP_HLT:
  987. case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
  988. case KVM_CAP_USER_MEMORY:
  989. case KVM_CAP_SET_TSS_ADDR:
  990. case KVM_CAP_EXT_CPUID:
  991. case KVM_CAP_CLOCKSOURCE:
  992. case KVM_CAP_PIT:
  993. case KVM_CAP_NOP_IO_DELAY:
  994. case KVM_CAP_MP_STATE:
  995. case KVM_CAP_SYNC_MMU:
  996. r = 1;
  997. break;
  998. case KVM_CAP_COALESCED_MMIO:
  999. r = KVM_COALESCED_MMIO_PAGE_OFFSET;
  1000. break;
  1001. case KVM_CAP_VAPIC:
  1002. r = !kvm_x86_ops->cpu_has_accelerated_tpr();
  1003. break;
  1004. case KVM_CAP_NR_VCPUS:
  1005. r = KVM_MAX_VCPUS;
  1006. break;
  1007. case KVM_CAP_NR_MEMSLOTS:
  1008. r = KVM_MEMORY_SLOTS;
  1009. break;
  1010. case KVM_CAP_PV_MMU:
  1011. r = !tdp_enabled;
  1012. break;
  1013. case KVM_CAP_IOMMU:
  1014. r = intel_iommu_found();
  1015. break;
  1016. default:
  1017. r = 0;
  1018. break;
  1019. }
  1020. return r;
  1021. }
  1022. long kvm_arch_dev_ioctl(struct file *filp,
  1023. unsigned int ioctl, unsigned long arg)
  1024. {
  1025. void __user *argp = (void __user *)arg;
  1026. long r;
  1027. switch (ioctl) {
  1028. case KVM_GET_MSR_INDEX_LIST: {
  1029. struct kvm_msr_list __user *user_msr_list = argp;
  1030. struct kvm_msr_list msr_list;
  1031. unsigned n;
  1032. r = -EFAULT;
  1033. if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
  1034. goto out;
  1035. n = msr_list.nmsrs;
  1036. msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
  1037. if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
  1038. goto out;
  1039. r = -E2BIG;
  1040. if (n < num_msrs_to_save)
  1041. goto out;
  1042. r = -EFAULT;
  1043. if (copy_to_user(user_msr_list->indices, &msrs_to_save,
  1044. num_msrs_to_save * sizeof(u32)))
  1045. goto out;
  1046. if (copy_to_user(user_msr_list->indices
  1047. + num_msrs_to_save * sizeof(u32),
  1048. &emulated_msrs,
  1049. ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
  1050. goto out;
  1051. r = 0;
  1052. break;
  1053. }
  1054. case KVM_GET_SUPPORTED_CPUID: {
  1055. struct kvm_cpuid2 __user *cpuid_arg = argp;
  1056. struct kvm_cpuid2 cpuid;
  1057. r = -EFAULT;
  1058. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1059. goto out;
  1060. r = kvm_dev_ioctl_get_supported_cpuid(&cpuid,
  1061. cpuid_arg->entries);
  1062. if (r)
  1063. goto out;
  1064. r = -EFAULT;
  1065. if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
  1066. goto out;
  1067. r = 0;
  1068. break;
  1069. }
  1070. default:
  1071. r = -EINVAL;
  1072. }
  1073. out:
  1074. return r;
  1075. }
  1076. void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
  1077. {
  1078. kvm_x86_ops->vcpu_load(vcpu, cpu);
  1079. kvm_write_guest_time(vcpu);
  1080. }
  1081. void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
  1082. {
  1083. kvm_x86_ops->vcpu_put(vcpu);
  1084. kvm_put_guest_fpu(vcpu);
  1085. }
  1086. static int is_efer_nx(void)
  1087. {
  1088. u64 efer;
  1089. rdmsrl(MSR_EFER, efer);
  1090. return efer & EFER_NX;
  1091. }
  1092. static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
  1093. {
  1094. int i;
  1095. struct kvm_cpuid_entry2 *e, *entry;
  1096. entry = NULL;
  1097. for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
  1098. e = &vcpu->arch.cpuid_entries[i];
  1099. if (e->function == 0x80000001) {
  1100. entry = e;
  1101. break;
  1102. }
  1103. }
  1104. if (entry && (entry->edx & (1 << 20)) && !is_efer_nx()) {
  1105. entry->edx &= ~(1 << 20);
  1106. printk(KERN_INFO "kvm: guest NX capability removed\n");
  1107. }
  1108. }
  1109. /* when an old userspace process fills a new kernel module */
  1110. static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
  1111. struct kvm_cpuid *cpuid,
  1112. struct kvm_cpuid_entry __user *entries)
  1113. {
  1114. int r, i;
  1115. struct kvm_cpuid_entry *cpuid_entries;
  1116. r = -E2BIG;
  1117. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  1118. goto out;
  1119. r = -ENOMEM;
  1120. cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry) * cpuid->nent);
  1121. if (!cpuid_entries)
  1122. goto out;
  1123. r = -EFAULT;
  1124. if (copy_from_user(cpuid_entries, entries,
  1125. cpuid->nent * sizeof(struct kvm_cpuid_entry)))
  1126. goto out_free;
  1127. for (i = 0; i < cpuid->nent; i++) {
  1128. vcpu->arch.cpuid_entries[i].function = cpuid_entries[i].function;
  1129. vcpu->arch.cpuid_entries[i].eax = cpuid_entries[i].eax;
  1130. vcpu->arch.cpuid_entries[i].ebx = cpuid_entries[i].ebx;
  1131. vcpu->arch.cpuid_entries[i].ecx = cpuid_entries[i].ecx;
  1132. vcpu->arch.cpuid_entries[i].edx = cpuid_entries[i].edx;
  1133. vcpu->arch.cpuid_entries[i].index = 0;
  1134. vcpu->arch.cpuid_entries[i].flags = 0;
  1135. vcpu->arch.cpuid_entries[i].padding[0] = 0;
  1136. vcpu->arch.cpuid_entries[i].padding[1] = 0;
  1137. vcpu->arch.cpuid_entries[i].padding[2] = 0;
  1138. }
  1139. vcpu->arch.cpuid_nent = cpuid->nent;
  1140. cpuid_fix_nx_cap(vcpu);
  1141. r = 0;
  1142. out_free:
  1143. vfree(cpuid_entries);
  1144. out:
  1145. return r;
  1146. }
  1147. static int kvm_vcpu_ioctl_set_cpuid2(struct kvm_vcpu *vcpu,
  1148. struct kvm_cpuid2 *cpuid,
  1149. struct kvm_cpuid_entry2 __user *entries)
  1150. {
  1151. int r;
  1152. r = -E2BIG;
  1153. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  1154. goto out;
  1155. r = -EFAULT;
  1156. if (copy_from_user(&vcpu->arch.cpuid_entries, entries,
  1157. cpuid->nent * sizeof(struct kvm_cpuid_entry2)))
  1158. goto out;
  1159. vcpu->arch.cpuid_nent = cpuid->nent;
  1160. return 0;
  1161. out:
  1162. return r;
  1163. }
  1164. static int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
  1165. struct kvm_cpuid2 *cpuid,
  1166. struct kvm_cpuid_entry2 __user *entries)
  1167. {
  1168. int r;
  1169. r = -E2BIG;
  1170. if (cpuid->nent < vcpu->arch.cpuid_nent)
  1171. goto out;
  1172. r = -EFAULT;
  1173. if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
  1174. vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
  1175. goto out;
  1176. return 0;
  1177. out:
  1178. cpuid->nent = vcpu->arch.cpuid_nent;
  1179. return r;
  1180. }
  1181. static inline u32 bit(int bitno)
  1182. {
  1183. return 1 << (bitno & 31);
  1184. }
  1185. static void do_cpuid_1_ent(struct kvm_cpuid_entry2 *entry, u32 function,
  1186. u32 index)
  1187. {
  1188. entry->function = function;
  1189. entry->index = index;
  1190. cpuid_count(entry->function, entry->index,
  1191. &entry->eax, &entry->ebx, &entry->ecx, &entry->edx);
  1192. entry->flags = 0;
  1193. }
  1194. static void do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
  1195. u32 index, int *nent, int maxnent)
  1196. {
  1197. const u32 kvm_supported_word0_x86_features = bit(X86_FEATURE_FPU) |
  1198. bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
  1199. bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
  1200. bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
  1201. bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
  1202. bit(X86_FEATURE_SEP) | bit(X86_FEATURE_PGE) |
  1203. bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
  1204. bit(X86_FEATURE_CLFLSH) | bit(X86_FEATURE_MMX) |
  1205. bit(X86_FEATURE_FXSR) | bit(X86_FEATURE_XMM) |
  1206. bit(X86_FEATURE_XMM2) | bit(X86_FEATURE_SELFSNOOP);
  1207. const u32 kvm_supported_word1_x86_features = bit(X86_FEATURE_FPU) |
  1208. bit(X86_FEATURE_VME) | bit(X86_FEATURE_DE) |
  1209. bit(X86_FEATURE_PSE) | bit(X86_FEATURE_TSC) |
  1210. bit(X86_FEATURE_MSR) | bit(X86_FEATURE_PAE) |
  1211. bit(X86_FEATURE_CX8) | bit(X86_FEATURE_APIC) |
  1212. bit(X86_FEATURE_PGE) |
  1213. bit(X86_FEATURE_CMOV) | bit(X86_FEATURE_PSE36) |
  1214. bit(X86_FEATURE_MMX) | bit(X86_FEATURE_FXSR) |
  1215. bit(X86_FEATURE_SYSCALL) |
  1216. (bit(X86_FEATURE_NX) && is_efer_nx()) |
  1217. #ifdef CONFIG_X86_64
  1218. bit(X86_FEATURE_LM) |
  1219. #endif
  1220. bit(X86_FEATURE_MMXEXT) |
  1221. bit(X86_FEATURE_3DNOWEXT) |
  1222. bit(X86_FEATURE_3DNOW);
  1223. const u32 kvm_supported_word3_x86_features =
  1224. bit(X86_FEATURE_XMM3) | bit(X86_FEATURE_CX16);
  1225. const u32 kvm_supported_word6_x86_features =
  1226. bit(X86_FEATURE_LAHF_LM) | bit(X86_FEATURE_CMP_LEGACY);
  1227. /* all func 2 cpuid_count() should be called on the same cpu */
  1228. get_cpu();
  1229. do_cpuid_1_ent(entry, function, index);
  1230. ++*nent;
  1231. switch (function) {
  1232. case 0:
  1233. entry->eax = min(entry->eax, (u32)0xb);
  1234. break;
  1235. case 1:
  1236. entry->edx &= kvm_supported_word0_x86_features;
  1237. entry->ecx &= kvm_supported_word3_x86_features;
  1238. break;
  1239. /* function 2 entries are STATEFUL. That is, repeated cpuid commands
  1240. * may return different values. This forces us to get_cpu() before
  1241. * issuing the first command, and also to emulate this annoying behavior
  1242. * in kvm_emulate_cpuid() using KVM_CPUID_FLAG_STATE_READ_NEXT */
  1243. case 2: {
  1244. int t, times = entry->eax & 0xff;
  1245. entry->flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
  1246. for (t = 1; t < times && *nent < maxnent; ++t) {
  1247. do_cpuid_1_ent(&entry[t], function, 0);
  1248. entry[t].flags |= KVM_CPUID_FLAG_STATEFUL_FUNC;
  1249. ++*nent;
  1250. }
  1251. break;
  1252. }
  1253. /* function 4 and 0xb have additional index. */
  1254. case 4: {
  1255. int i, cache_type;
  1256. entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  1257. /* read more entries until cache_type is zero */
  1258. for (i = 1; *nent < maxnent; ++i) {
  1259. cache_type = entry[i - 1].eax & 0x1f;
  1260. if (!cache_type)
  1261. break;
  1262. do_cpuid_1_ent(&entry[i], function, i);
  1263. entry[i].flags |=
  1264. KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  1265. ++*nent;
  1266. }
  1267. break;
  1268. }
  1269. case 0xb: {
  1270. int i, level_type;
  1271. entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  1272. /* read more entries until level_type is zero */
  1273. for (i = 1; *nent < maxnent; ++i) {
  1274. level_type = entry[i - 1].ecx & 0xff;
  1275. if (!level_type)
  1276. break;
  1277. do_cpuid_1_ent(&entry[i], function, i);
  1278. entry[i].flags |=
  1279. KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
  1280. ++*nent;
  1281. }
  1282. break;
  1283. }
  1284. case 0x80000000:
  1285. entry->eax = min(entry->eax, 0x8000001a);
  1286. break;
  1287. case 0x80000001:
  1288. entry->edx &= kvm_supported_word1_x86_features;
  1289. entry->ecx &= kvm_supported_word6_x86_features;
  1290. break;
  1291. }
  1292. put_cpu();
  1293. }
  1294. static int kvm_dev_ioctl_get_supported_cpuid(struct kvm_cpuid2 *cpuid,
  1295. struct kvm_cpuid_entry2 __user *entries)
  1296. {
  1297. struct kvm_cpuid_entry2 *cpuid_entries;
  1298. int limit, nent = 0, r = -E2BIG;
  1299. u32 func;
  1300. if (cpuid->nent < 1)
  1301. goto out;
  1302. r = -ENOMEM;
  1303. cpuid_entries = vmalloc(sizeof(struct kvm_cpuid_entry2) * cpuid->nent);
  1304. if (!cpuid_entries)
  1305. goto out;
  1306. do_cpuid_ent(&cpuid_entries[0], 0, 0, &nent, cpuid->nent);
  1307. limit = cpuid_entries[0].eax;
  1308. for (func = 1; func <= limit && nent < cpuid->nent; ++func)
  1309. do_cpuid_ent(&cpuid_entries[nent], func, 0,
  1310. &nent, cpuid->nent);
  1311. r = -E2BIG;
  1312. if (nent >= cpuid->nent)
  1313. goto out_free;
  1314. do_cpuid_ent(&cpuid_entries[nent], 0x80000000, 0, &nent, cpuid->nent);
  1315. limit = cpuid_entries[nent - 1].eax;
  1316. for (func = 0x80000001; func <= limit && nent < cpuid->nent; ++func)
  1317. do_cpuid_ent(&cpuid_entries[nent], func, 0,
  1318. &nent, cpuid->nent);
  1319. r = -EFAULT;
  1320. if (copy_to_user(entries, cpuid_entries,
  1321. nent * sizeof(struct kvm_cpuid_entry2)))
  1322. goto out_free;
  1323. cpuid->nent = nent;
  1324. r = 0;
  1325. out_free:
  1326. vfree(cpuid_entries);
  1327. out:
  1328. return r;
  1329. }
  1330. static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
  1331. struct kvm_lapic_state *s)
  1332. {
  1333. vcpu_load(vcpu);
  1334. memcpy(s->regs, vcpu->arch.apic->regs, sizeof *s);
  1335. vcpu_put(vcpu);
  1336. return 0;
  1337. }
  1338. static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
  1339. struct kvm_lapic_state *s)
  1340. {
  1341. vcpu_load(vcpu);
  1342. memcpy(vcpu->arch.apic->regs, s->regs, sizeof *s);
  1343. kvm_apic_post_state_restore(vcpu);
  1344. vcpu_put(vcpu);
  1345. return 0;
  1346. }
  1347. static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
  1348. struct kvm_interrupt *irq)
  1349. {
  1350. if (irq->irq < 0 || irq->irq >= 256)
  1351. return -EINVAL;
  1352. if (irqchip_in_kernel(vcpu->kvm))
  1353. return -ENXIO;
  1354. vcpu_load(vcpu);
  1355. set_bit(irq->irq, vcpu->arch.irq_pending);
  1356. set_bit(irq->irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
  1357. vcpu_put(vcpu);
  1358. return 0;
  1359. }
  1360. static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
  1361. struct kvm_tpr_access_ctl *tac)
  1362. {
  1363. if (tac->flags)
  1364. return -EINVAL;
  1365. vcpu->arch.tpr_access_reporting = !!tac->enabled;
  1366. return 0;
  1367. }
  1368. long kvm_arch_vcpu_ioctl(struct file *filp,
  1369. unsigned int ioctl, unsigned long arg)
  1370. {
  1371. struct kvm_vcpu *vcpu = filp->private_data;
  1372. void __user *argp = (void __user *)arg;
  1373. int r;
  1374. struct kvm_lapic_state *lapic = NULL;
  1375. switch (ioctl) {
  1376. case KVM_GET_LAPIC: {
  1377. lapic = kzalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
  1378. r = -ENOMEM;
  1379. if (!lapic)
  1380. goto out;
  1381. r = kvm_vcpu_ioctl_get_lapic(vcpu, lapic);
  1382. if (r)
  1383. goto out;
  1384. r = -EFAULT;
  1385. if (copy_to_user(argp, lapic, sizeof(struct kvm_lapic_state)))
  1386. goto out;
  1387. r = 0;
  1388. break;
  1389. }
  1390. case KVM_SET_LAPIC: {
  1391. lapic = kmalloc(sizeof(struct kvm_lapic_state), GFP_KERNEL);
  1392. r = -ENOMEM;
  1393. if (!lapic)
  1394. goto out;
  1395. r = -EFAULT;
  1396. if (copy_from_user(lapic, argp, sizeof(struct kvm_lapic_state)))
  1397. goto out;
  1398. r = kvm_vcpu_ioctl_set_lapic(vcpu, lapic);
  1399. if (r)
  1400. goto out;
  1401. r = 0;
  1402. break;
  1403. }
  1404. case KVM_INTERRUPT: {
  1405. struct kvm_interrupt irq;
  1406. r = -EFAULT;
  1407. if (copy_from_user(&irq, argp, sizeof irq))
  1408. goto out;
  1409. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  1410. if (r)
  1411. goto out;
  1412. r = 0;
  1413. break;
  1414. }
  1415. case KVM_SET_CPUID: {
  1416. struct kvm_cpuid __user *cpuid_arg = argp;
  1417. struct kvm_cpuid cpuid;
  1418. r = -EFAULT;
  1419. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1420. goto out;
  1421. r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
  1422. if (r)
  1423. goto out;
  1424. break;
  1425. }
  1426. case KVM_SET_CPUID2: {
  1427. struct kvm_cpuid2 __user *cpuid_arg = argp;
  1428. struct kvm_cpuid2 cpuid;
  1429. r = -EFAULT;
  1430. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1431. goto out;
  1432. r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
  1433. cpuid_arg->entries);
  1434. if (r)
  1435. goto out;
  1436. break;
  1437. }
  1438. case KVM_GET_CPUID2: {
  1439. struct kvm_cpuid2 __user *cpuid_arg = argp;
  1440. struct kvm_cpuid2 cpuid;
  1441. r = -EFAULT;
  1442. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1443. goto out;
  1444. r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
  1445. cpuid_arg->entries);
  1446. if (r)
  1447. goto out;
  1448. r = -EFAULT;
  1449. if (copy_to_user(cpuid_arg, &cpuid, sizeof cpuid))
  1450. goto out;
  1451. r = 0;
  1452. break;
  1453. }
  1454. case KVM_GET_MSRS:
  1455. r = msr_io(vcpu, argp, kvm_get_msr, 1);
  1456. break;
  1457. case KVM_SET_MSRS:
  1458. r = msr_io(vcpu, argp, do_set_msr, 0);
  1459. break;
  1460. case KVM_TPR_ACCESS_REPORTING: {
  1461. struct kvm_tpr_access_ctl tac;
  1462. r = -EFAULT;
  1463. if (copy_from_user(&tac, argp, sizeof tac))
  1464. goto out;
  1465. r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
  1466. if (r)
  1467. goto out;
  1468. r = -EFAULT;
  1469. if (copy_to_user(argp, &tac, sizeof tac))
  1470. goto out;
  1471. r = 0;
  1472. break;
  1473. };
  1474. case KVM_SET_VAPIC_ADDR: {
  1475. struct kvm_vapic_addr va;
  1476. r = -EINVAL;
  1477. if (!irqchip_in_kernel(vcpu->kvm))
  1478. goto out;
  1479. r = -EFAULT;
  1480. if (copy_from_user(&va, argp, sizeof va))
  1481. goto out;
  1482. r = 0;
  1483. kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
  1484. break;
  1485. }
  1486. default:
  1487. r = -EINVAL;
  1488. }
  1489. out:
  1490. if (lapic)
  1491. kfree(lapic);
  1492. return r;
  1493. }
  1494. static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
  1495. {
  1496. int ret;
  1497. if (addr > (unsigned int)(-3 * PAGE_SIZE))
  1498. return -1;
  1499. ret = kvm_x86_ops->set_tss_addr(kvm, addr);
  1500. return ret;
  1501. }
  1502. static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
  1503. u32 kvm_nr_mmu_pages)
  1504. {
  1505. if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
  1506. return -EINVAL;
  1507. down_write(&kvm->slots_lock);
  1508. kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
  1509. kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
  1510. up_write(&kvm->slots_lock);
  1511. return 0;
  1512. }
  1513. static int kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
  1514. {
  1515. return kvm->arch.n_alloc_mmu_pages;
  1516. }
  1517. gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
  1518. {
  1519. int i;
  1520. struct kvm_mem_alias *alias;
  1521. for (i = 0; i < kvm->arch.naliases; ++i) {
  1522. alias = &kvm->arch.aliases[i];
  1523. if (gfn >= alias->base_gfn
  1524. && gfn < alias->base_gfn + alias->npages)
  1525. return alias->target_gfn + gfn - alias->base_gfn;
  1526. }
  1527. return gfn;
  1528. }
  1529. /*
  1530. * Set a new alias region. Aliases map a portion of physical memory into
  1531. * another portion. This is useful for memory windows, for example the PC
  1532. * VGA region.
  1533. */
  1534. static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
  1535. struct kvm_memory_alias *alias)
  1536. {
  1537. int r, n;
  1538. struct kvm_mem_alias *p;
  1539. r = -EINVAL;
  1540. /* General sanity checks */
  1541. if (alias->memory_size & (PAGE_SIZE - 1))
  1542. goto out;
  1543. if (alias->guest_phys_addr & (PAGE_SIZE - 1))
  1544. goto out;
  1545. if (alias->slot >= KVM_ALIAS_SLOTS)
  1546. goto out;
  1547. if (alias->guest_phys_addr + alias->memory_size
  1548. < alias->guest_phys_addr)
  1549. goto out;
  1550. if (alias->target_phys_addr + alias->memory_size
  1551. < alias->target_phys_addr)
  1552. goto out;
  1553. down_write(&kvm->slots_lock);
  1554. spin_lock(&kvm->mmu_lock);
  1555. p = &kvm->arch.aliases[alias->slot];
  1556. p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
  1557. p->npages = alias->memory_size >> PAGE_SHIFT;
  1558. p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
  1559. for (n = KVM_ALIAS_SLOTS; n > 0; --n)
  1560. if (kvm->arch.aliases[n - 1].npages)
  1561. break;
  1562. kvm->arch.naliases = n;
  1563. spin_unlock(&kvm->mmu_lock);
  1564. kvm_mmu_zap_all(kvm);
  1565. up_write(&kvm->slots_lock);
  1566. return 0;
  1567. out:
  1568. return r;
  1569. }
  1570. static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  1571. {
  1572. int r;
  1573. r = 0;
  1574. switch (chip->chip_id) {
  1575. case KVM_IRQCHIP_PIC_MASTER:
  1576. memcpy(&chip->chip.pic,
  1577. &pic_irqchip(kvm)->pics[0],
  1578. sizeof(struct kvm_pic_state));
  1579. break;
  1580. case KVM_IRQCHIP_PIC_SLAVE:
  1581. memcpy(&chip->chip.pic,
  1582. &pic_irqchip(kvm)->pics[1],
  1583. sizeof(struct kvm_pic_state));
  1584. break;
  1585. case KVM_IRQCHIP_IOAPIC:
  1586. memcpy(&chip->chip.ioapic,
  1587. ioapic_irqchip(kvm),
  1588. sizeof(struct kvm_ioapic_state));
  1589. break;
  1590. default:
  1591. r = -EINVAL;
  1592. break;
  1593. }
  1594. return r;
  1595. }
  1596. static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
  1597. {
  1598. int r;
  1599. r = 0;
  1600. switch (chip->chip_id) {
  1601. case KVM_IRQCHIP_PIC_MASTER:
  1602. memcpy(&pic_irqchip(kvm)->pics[0],
  1603. &chip->chip.pic,
  1604. sizeof(struct kvm_pic_state));
  1605. break;
  1606. case KVM_IRQCHIP_PIC_SLAVE:
  1607. memcpy(&pic_irqchip(kvm)->pics[1],
  1608. &chip->chip.pic,
  1609. sizeof(struct kvm_pic_state));
  1610. break;
  1611. case KVM_IRQCHIP_IOAPIC:
  1612. memcpy(ioapic_irqchip(kvm),
  1613. &chip->chip.ioapic,
  1614. sizeof(struct kvm_ioapic_state));
  1615. break;
  1616. default:
  1617. r = -EINVAL;
  1618. break;
  1619. }
  1620. kvm_pic_update_irq(pic_irqchip(kvm));
  1621. return r;
  1622. }
  1623. static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
  1624. {
  1625. int r = 0;
  1626. memcpy(ps, &kvm->arch.vpit->pit_state, sizeof(struct kvm_pit_state));
  1627. return r;
  1628. }
  1629. static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
  1630. {
  1631. int r = 0;
  1632. memcpy(&kvm->arch.vpit->pit_state, ps, sizeof(struct kvm_pit_state));
  1633. kvm_pit_load_count(kvm, 0, ps->channels[0].count);
  1634. return r;
  1635. }
  1636. /*
  1637. * Get (and clear) the dirty memory log for a memory slot.
  1638. */
  1639. int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
  1640. struct kvm_dirty_log *log)
  1641. {
  1642. int r;
  1643. int n;
  1644. struct kvm_memory_slot *memslot;
  1645. int is_dirty = 0;
  1646. down_write(&kvm->slots_lock);
  1647. r = kvm_get_dirty_log(kvm, log, &is_dirty);
  1648. if (r)
  1649. goto out;
  1650. /* If nothing is dirty, don't bother messing with page tables. */
  1651. if (is_dirty) {
  1652. kvm_mmu_slot_remove_write_access(kvm, log->slot);
  1653. kvm_flush_remote_tlbs(kvm);
  1654. memslot = &kvm->memslots[log->slot];
  1655. n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
  1656. memset(memslot->dirty_bitmap, 0, n);
  1657. }
  1658. r = 0;
  1659. out:
  1660. up_write(&kvm->slots_lock);
  1661. return r;
  1662. }
  1663. long kvm_arch_vm_ioctl(struct file *filp,
  1664. unsigned int ioctl, unsigned long arg)
  1665. {
  1666. struct kvm *kvm = filp->private_data;
  1667. void __user *argp = (void __user *)arg;
  1668. int r = -EINVAL;
  1669. /*
  1670. * This union makes it completely explicit to gcc-3.x
  1671. * that these two variables' stack usage should be
  1672. * combined, not added together.
  1673. */
  1674. union {
  1675. struct kvm_pit_state ps;
  1676. struct kvm_memory_alias alias;
  1677. } u;
  1678. switch (ioctl) {
  1679. case KVM_SET_TSS_ADDR:
  1680. r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
  1681. if (r < 0)
  1682. goto out;
  1683. break;
  1684. case KVM_SET_MEMORY_REGION: {
  1685. struct kvm_memory_region kvm_mem;
  1686. struct kvm_userspace_memory_region kvm_userspace_mem;
  1687. r = -EFAULT;
  1688. if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
  1689. goto out;
  1690. kvm_userspace_mem.slot = kvm_mem.slot;
  1691. kvm_userspace_mem.flags = kvm_mem.flags;
  1692. kvm_userspace_mem.guest_phys_addr = kvm_mem.guest_phys_addr;
  1693. kvm_userspace_mem.memory_size = kvm_mem.memory_size;
  1694. r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem, 0);
  1695. if (r)
  1696. goto out;
  1697. break;
  1698. }
  1699. case KVM_SET_NR_MMU_PAGES:
  1700. r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
  1701. if (r)
  1702. goto out;
  1703. break;
  1704. case KVM_GET_NR_MMU_PAGES:
  1705. r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
  1706. break;
  1707. case KVM_SET_MEMORY_ALIAS:
  1708. r = -EFAULT;
  1709. if (copy_from_user(&u.alias, argp, sizeof(struct kvm_memory_alias)))
  1710. goto out;
  1711. r = kvm_vm_ioctl_set_memory_alias(kvm, &u.alias);
  1712. if (r)
  1713. goto out;
  1714. break;
  1715. case KVM_CREATE_IRQCHIP:
  1716. r = -ENOMEM;
  1717. kvm->arch.vpic = kvm_create_pic(kvm);
  1718. if (kvm->arch.vpic) {
  1719. r = kvm_ioapic_init(kvm);
  1720. if (r) {
  1721. kfree(kvm->arch.vpic);
  1722. kvm->arch.vpic = NULL;
  1723. goto out;
  1724. }
  1725. } else
  1726. goto out;
  1727. break;
  1728. case KVM_CREATE_PIT:
  1729. r = -ENOMEM;
  1730. kvm->arch.vpit = kvm_create_pit(kvm);
  1731. if (kvm->arch.vpit)
  1732. r = 0;
  1733. break;
  1734. case KVM_IRQ_LINE: {
  1735. struct kvm_irq_level irq_event;
  1736. r = -EFAULT;
  1737. if (copy_from_user(&irq_event, argp, sizeof irq_event))
  1738. goto out;
  1739. if (irqchip_in_kernel(kvm)) {
  1740. mutex_lock(&kvm->lock);
  1741. kvm_set_irq(kvm, irq_event.irq, irq_event.level);
  1742. mutex_unlock(&kvm->lock);
  1743. r = 0;
  1744. }
  1745. break;
  1746. }
  1747. case KVM_GET_IRQCHIP: {
  1748. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  1749. struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
  1750. r = -ENOMEM;
  1751. if (!chip)
  1752. goto out;
  1753. r = -EFAULT;
  1754. if (copy_from_user(chip, argp, sizeof *chip))
  1755. goto get_irqchip_out;
  1756. r = -ENXIO;
  1757. if (!irqchip_in_kernel(kvm))
  1758. goto get_irqchip_out;
  1759. r = kvm_vm_ioctl_get_irqchip(kvm, chip);
  1760. if (r)
  1761. goto get_irqchip_out;
  1762. r = -EFAULT;
  1763. if (copy_to_user(argp, chip, sizeof *chip))
  1764. goto get_irqchip_out;
  1765. r = 0;
  1766. get_irqchip_out:
  1767. kfree(chip);
  1768. if (r)
  1769. goto out;
  1770. break;
  1771. }
  1772. case KVM_SET_IRQCHIP: {
  1773. /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
  1774. struct kvm_irqchip *chip = kmalloc(sizeof(*chip), GFP_KERNEL);
  1775. r = -ENOMEM;
  1776. if (!chip)
  1777. goto out;
  1778. r = -EFAULT;
  1779. if (copy_from_user(chip, argp, sizeof *chip))
  1780. goto set_irqchip_out;
  1781. r = -ENXIO;
  1782. if (!irqchip_in_kernel(kvm))
  1783. goto set_irqchip_out;
  1784. r = kvm_vm_ioctl_set_irqchip(kvm, chip);
  1785. if (r)
  1786. goto set_irqchip_out;
  1787. r = 0;
  1788. set_irqchip_out:
  1789. kfree(chip);
  1790. if (r)
  1791. goto out;
  1792. break;
  1793. }
  1794. case KVM_ASSIGN_PCI_DEVICE: {
  1795. struct kvm_assigned_pci_dev assigned_dev;
  1796. r = -EFAULT;
  1797. if (copy_from_user(&assigned_dev, argp, sizeof assigned_dev))
  1798. goto out;
  1799. r = kvm_vm_ioctl_assign_device(kvm, &assigned_dev);
  1800. if (r)
  1801. goto out;
  1802. break;
  1803. }
  1804. case KVM_ASSIGN_IRQ: {
  1805. struct kvm_assigned_irq assigned_irq;
  1806. r = -EFAULT;
  1807. if (copy_from_user(&assigned_irq, argp, sizeof assigned_irq))
  1808. goto out;
  1809. r = kvm_vm_ioctl_assign_irq(kvm, &assigned_irq);
  1810. if (r)
  1811. goto out;
  1812. break;
  1813. }
  1814. case KVM_GET_PIT: {
  1815. r = -EFAULT;
  1816. if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
  1817. goto out;
  1818. r = -ENXIO;
  1819. if (!kvm->arch.vpit)
  1820. goto out;
  1821. r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
  1822. if (r)
  1823. goto out;
  1824. r = -EFAULT;
  1825. if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
  1826. goto out;
  1827. r = 0;
  1828. break;
  1829. }
  1830. case KVM_SET_PIT: {
  1831. r = -EFAULT;
  1832. if (copy_from_user(&u.ps, argp, sizeof u.ps))
  1833. goto out;
  1834. r = -ENXIO;
  1835. if (!kvm->arch.vpit)
  1836. goto out;
  1837. r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
  1838. if (r)
  1839. goto out;
  1840. r = 0;
  1841. break;
  1842. }
  1843. default:
  1844. ;
  1845. }
  1846. out:
  1847. return r;
  1848. }
  1849. static void kvm_init_msr_list(void)
  1850. {
  1851. u32 dummy[2];
  1852. unsigned i, j;
  1853. for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
  1854. if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
  1855. continue;
  1856. if (j < i)
  1857. msrs_to_save[j] = msrs_to_save[i];
  1858. j++;
  1859. }
  1860. num_msrs_to_save = j;
  1861. }
  1862. /*
  1863. * Only apic need an MMIO device hook, so shortcut now..
  1864. */
  1865. static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
  1866. gpa_t addr, int len,
  1867. int is_write)
  1868. {
  1869. struct kvm_io_device *dev;
  1870. if (vcpu->arch.apic) {
  1871. dev = &vcpu->arch.apic->dev;
  1872. if (dev->in_range(dev, addr, len, is_write))
  1873. return dev;
  1874. }
  1875. return NULL;
  1876. }
  1877. static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
  1878. gpa_t addr, int len,
  1879. int is_write)
  1880. {
  1881. struct kvm_io_device *dev;
  1882. dev = vcpu_find_pervcpu_dev(vcpu, addr, len, is_write);
  1883. if (dev == NULL)
  1884. dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr, len,
  1885. is_write);
  1886. return dev;
  1887. }
  1888. int emulator_read_std(unsigned long addr,
  1889. void *val,
  1890. unsigned int bytes,
  1891. struct kvm_vcpu *vcpu)
  1892. {
  1893. void *data = val;
  1894. int r = X86EMUL_CONTINUE;
  1895. while (bytes) {
  1896. gpa_t gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
  1897. unsigned offset = addr & (PAGE_SIZE-1);
  1898. unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
  1899. int ret;
  1900. if (gpa == UNMAPPED_GVA) {
  1901. r = X86EMUL_PROPAGATE_FAULT;
  1902. goto out;
  1903. }
  1904. ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
  1905. if (ret < 0) {
  1906. r = X86EMUL_UNHANDLEABLE;
  1907. goto out;
  1908. }
  1909. bytes -= tocopy;
  1910. data += tocopy;
  1911. addr += tocopy;
  1912. }
  1913. out:
  1914. return r;
  1915. }
  1916. EXPORT_SYMBOL_GPL(emulator_read_std);
  1917. static int emulator_read_emulated(unsigned long addr,
  1918. void *val,
  1919. unsigned int bytes,
  1920. struct kvm_vcpu *vcpu)
  1921. {
  1922. struct kvm_io_device *mmio_dev;
  1923. gpa_t gpa;
  1924. if (vcpu->mmio_read_completed) {
  1925. memcpy(val, vcpu->mmio_data, bytes);
  1926. vcpu->mmio_read_completed = 0;
  1927. return X86EMUL_CONTINUE;
  1928. }
  1929. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
  1930. /* For APIC access vmexit */
  1931. if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  1932. goto mmio;
  1933. if (emulator_read_std(addr, val, bytes, vcpu)
  1934. == X86EMUL_CONTINUE)
  1935. return X86EMUL_CONTINUE;
  1936. if (gpa == UNMAPPED_GVA)
  1937. return X86EMUL_PROPAGATE_FAULT;
  1938. mmio:
  1939. /*
  1940. * Is this MMIO handled locally?
  1941. */
  1942. mutex_lock(&vcpu->kvm->lock);
  1943. mmio_dev = vcpu_find_mmio_dev(vcpu, gpa, bytes, 0);
  1944. if (mmio_dev) {
  1945. kvm_iodevice_read(mmio_dev, gpa, bytes, val);
  1946. mutex_unlock(&vcpu->kvm->lock);
  1947. return X86EMUL_CONTINUE;
  1948. }
  1949. mutex_unlock(&vcpu->kvm->lock);
  1950. vcpu->mmio_needed = 1;
  1951. vcpu->mmio_phys_addr = gpa;
  1952. vcpu->mmio_size = bytes;
  1953. vcpu->mmio_is_write = 0;
  1954. return X86EMUL_UNHANDLEABLE;
  1955. }
  1956. int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
  1957. const void *val, int bytes)
  1958. {
  1959. int ret;
  1960. ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
  1961. if (ret < 0)
  1962. return 0;
  1963. kvm_mmu_pte_write(vcpu, gpa, val, bytes);
  1964. return 1;
  1965. }
  1966. static int emulator_write_emulated_onepage(unsigned long addr,
  1967. const void *val,
  1968. unsigned int bytes,
  1969. struct kvm_vcpu *vcpu)
  1970. {
  1971. struct kvm_io_device *mmio_dev;
  1972. gpa_t gpa;
  1973. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
  1974. if (gpa == UNMAPPED_GVA) {
  1975. kvm_inject_page_fault(vcpu, addr, 2);
  1976. return X86EMUL_PROPAGATE_FAULT;
  1977. }
  1978. /* For APIC access vmexit */
  1979. if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  1980. goto mmio;
  1981. if (emulator_write_phys(vcpu, gpa, val, bytes))
  1982. return X86EMUL_CONTINUE;
  1983. mmio:
  1984. /*
  1985. * Is this MMIO handled locally?
  1986. */
  1987. mutex_lock(&vcpu->kvm->lock);
  1988. mmio_dev = vcpu_find_mmio_dev(vcpu, gpa, bytes, 1);
  1989. if (mmio_dev) {
  1990. kvm_iodevice_write(mmio_dev, gpa, bytes, val);
  1991. mutex_unlock(&vcpu->kvm->lock);
  1992. return X86EMUL_CONTINUE;
  1993. }
  1994. mutex_unlock(&vcpu->kvm->lock);
  1995. vcpu->mmio_needed = 1;
  1996. vcpu->mmio_phys_addr = gpa;
  1997. vcpu->mmio_size = bytes;
  1998. vcpu->mmio_is_write = 1;
  1999. memcpy(vcpu->mmio_data, val, bytes);
  2000. return X86EMUL_CONTINUE;
  2001. }
  2002. int emulator_write_emulated(unsigned long addr,
  2003. const void *val,
  2004. unsigned int bytes,
  2005. struct kvm_vcpu *vcpu)
  2006. {
  2007. /* Crossing a page boundary? */
  2008. if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
  2009. int rc, now;
  2010. now = -addr & ~PAGE_MASK;
  2011. rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
  2012. if (rc != X86EMUL_CONTINUE)
  2013. return rc;
  2014. addr += now;
  2015. val += now;
  2016. bytes -= now;
  2017. }
  2018. return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
  2019. }
  2020. EXPORT_SYMBOL_GPL(emulator_write_emulated);
  2021. static int emulator_cmpxchg_emulated(unsigned long addr,
  2022. const void *old,
  2023. const void *new,
  2024. unsigned int bytes,
  2025. struct kvm_vcpu *vcpu)
  2026. {
  2027. static int reported;
  2028. if (!reported) {
  2029. reported = 1;
  2030. printk(KERN_WARNING "kvm: emulating exchange as write\n");
  2031. }
  2032. #ifndef CONFIG_X86_64
  2033. /* guests cmpxchg8b have to be emulated atomically */
  2034. if (bytes == 8) {
  2035. gpa_t gpa;
  2036. struct page *page;
  2037. char *kaddr;
  2038. u64 val;
  2039. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, addr);
  2040. if (gpa == UNMAPPED_GVA ||
  2041. (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
  2042. goto emul_write;
  2043. if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
  2044. goto emul_write;
  2045. val = *(u64 *)new;
  2046. page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
  2047. kaddr = kmap_atomic(page, KM_USER0);
  2048. set_64bit((u64 *)(kaddr + offset_in_page(gpa)), val);
  2049. kunmap_atomic(kaddr, KM_USER0);
  2050. kvm_release_page_dirty(page);
  2051. }
  2052. emul_write:
  2053. #endif
  2054. return emulator_write_emulated(addr, new, bytes, vcpu);
  2055. }
  2056. static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
  2057. {
  2058. return kvm_x86_ops->get_segment_base(vcpu, seg);
  2059. }
  2060. int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
  2061. {
  2062. kvm_mmu_invlpg(vcpu, address);
  2063. return X86EMUL_CONTINUE;
  2064. }
  2065. int emulate_clts(struct kvm_vcpu *vcpu)
  2066. {
  2067. KVMTRACE_0D(CLTS, vcpu, handler);
  2068. kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 & ~X86_CR0_TS);
  2069. return X86EMUL_CONTINUE;
  2070. }
  2071. int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
  2072. {
  2073. struct kvm_vcpu *vcpu = ctxt->vcpu;
  2074. switch (dr) {
  2075. case 0 ... 3:
  2076. *dest = kvm_x86_ops->get_dr(vcpu, dr);
  2077. return X86EMUL_CONTINUE;
  2078. default:
  2079. pr_unimpl(vcpu, "%s: unexpected dr %u\n", __func__, dr);
  2080. return X86EMUL_UNHANDLEABLE;
  2081. }
  2082. }
  2083. int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
  2084. {
  2085. unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
  2086. int exception;
  2087. kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
  2088. if (exception) {
  2089. /* FIXME: better handling */
  2090. return X86EMUL_UNHANDLEABLE;
  2091. }
  2092. return X86EMUL_CONTINUE;
  2093. }
  2094. void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
  2095. {
  2096. u8 opcodes[4];
  2097. unsigned long rip = kvm_rip_read(vcpu);
  2098. unsigned long rip_linear;
  2099. if (!printk_ratelimit())
  2100. return;
  2101. rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
  2102. emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
  2103. printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
  2104. context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
  2105. }
  2106. EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
  2107. static struct x86_emulate_ops emulate_ops = {
  2108. .read_std = emulator_read_std,
  2109. .read_emulated = emulator_read_emulated,
  2110. .write_emulated = emulator_write_emulated,
  2111. .cmpxchg_emulated = emulator_cmpxchg_emulated,
  2112. };
  2113. static void cache_all_regs(struct kvm_vcpu *vcpu)
  2114. {
  2115. kvm_register_read(vcpu, VCPU_REGS_RAX);
  2116. kvm_register_read(vcpu, VCPU_REGS_RSP);
  2117. kvm_register_read(vcpu, VCPU_REGS_RIP);
  2118. vcpu->arch.regs_dirty = ~0;
  2119. }
  2120. int emulate_instruction(struct kvm_vcpu *vcpu,
  2121. struct kvm_run *run,
  2122. unsigned long cr2,
  2123. u16 error_code,
  2124. int emulation_type)
  2125. {
  2126. int r;
  2127. struct decode_cache *c;
  2128. kvm_clear_exception_queue(vcpu);
  2129. vcpu->arch.mmio_fault_cr2 = cr2;
  2130. /*
  2131. * TODO: fix x86_emulate.c to use guest_read/write_register
  2132. * instead of direct ->regs accesses, can save hundred cycles
  2133. * on Intel for instructions that don't read/change RSP, for
  2134. * for example.
  2135. */
  2136. cache_all_regs(vcpu);
  2137. vcpu->mmio_is_write = 0;
  2138. vcpu->arch.pio.string = 0;
  2139. if (!(emulation_type & EMULTYPE_NO_DECODE)) {
  2140. int cs_db, cs_l;
  2141. kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  2142. vcpu->arch.emulate_ctxt.vcpu = vcpu;
  2143. vcpu->arch.emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
  2144. vcpu->arch.emulate_ctxt.mode =
  2145. (vcpu->arch.emulate_ctxt.eflags & X86_EFLAGS_VM)
  2146. ? X86EMUL_MODE_REAL : cs_l
  2147. ? X86EMUL_MODE_PROT64 : cs_db
  2148. ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
  2149. r = x86_decode_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
  2150. /* Reject the instructions other than VMCALL/VMMCALL when
  2151. * try to emulate invalid opcode */
  2152. c = &vcpu->arch.emulate_ctxt.decode;
  2153. if ((emulation_type & EMULTYPE_TRAP_UD) &&
  2154. (!(c->twobyte && c->b == 0x01 &&
  2155. (c->modrm_reg == 0 || c->modrm_reg == 3) &&
  2156. c->modrm_mod == 3 && c->modrm_rm == 1)))
  2157. return EMULATE_FAIL;
  2158. ++vcpu->stat.insn_emulation;
  2159. if (r) {
  2160. ++vcpu->stat.insn_emulation_fail;
  2161. if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
  2162. return EMULATE_DONE;
  2163. return EMULATE_FAIL;
  2164. }
  2165. }
  2166. r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
  2167. if (vcpu->arch.pio.string)
  2168. return EMULATE_DO_MMIO;
  2169. if ((r || vcpu->mmio_is_write) && run) {
  2170. run->exit_reason = KVM_EXIT_MMIO;
  2171. run->mmio.phys_addr = vcpu->mmio_phys_addr;
  2172. memcpy(run->mmio.data, vcpu->mmio_data, 8);
  2173. run->mmio.len = vcpu->mmio_size;
  2174. run->mmio.is_write = vcpu->mmio_is_write;
  2175. }
  2176. if (r) {
  2177. if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
  2178. return EMULATE_DONE;
  2179. if (!vcpu->mmio_needed) {
  2180. kvm_report_emulation_failure(vcpu, "mmio");
  2181. return EMULATE_FAIL;
  2182. }
  2183. return EMULATE_DO_MMIO;
  2184. }
  2185. kvm_x86_ops->set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
  2186. if (vcpu->mmio_is_write) {
  2187. vcpu->mmio_needed = 0;
  2188. return EMULATE_DO_MMIO;
  2189. }
  2190. return EMULATE_DONE;
  2191. }
  2192. EXPORT_SYMBOL_GPL(emulate_instruction);
  2193. static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
  2194. {
  2195. int i;
  2196. for (i = 0; i < ARRAY_SIZE(vcpu->arch.pio.guest_pages); ++i)
  2197. if (vcpu->arch.pio.guest_pages[i]) {
  2198. kvm_release_page_dirty(vcpu->arch.pio.guest_pages[i]);
  2199. vcpu->arch.pio.guest_pages[i] = NULL;
  2200. }
  2201. }
  2202. static int pio_copy_data(struct kvm_vcpu *vcpu)
  2203. {
  2204. void *p = vcpu->arch.pio_data;
  2205. void *q;
  2206. unsigned bytes;
  2207. int nr_pages = vcpu->arch.pio.guest_pages[1] ? 2 : 1;
  2208. q = vmap(vcpu->arch.pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
  2209. PAGE_KERNEL);
  2210. if (!q) {
  2211. free_pio_guest_pages(vcpu);
  2212. return -ENOMEM;
  2213. }
  2214. q += vcpu->arch.pio.guest_page_offset;
  2215. bytes = vcpu->arch.pio.size * vcpu->arch.pio.cur_count;
  2216. if (vcpu->arch.pio.in)
  2217. memcpy(q, p, bytes);
  2218. else
  2219. memcpy(p, q, bytes);
  2220. q -= vcpu->arch.pio.guest_page_offset;
  2221. vunmap(q);
  2222. free_pio_guest_pages(vcpu);
  2223. return 0;
  2224. }
  2225. int complete_pio(struct kvm_vcpu *vcpu)
  2226. {
  2227. struct kvm_pio_request *io = &vcpu->arch.pio;
  2228. long delta;
  2229. int r;
  2230. unsigned long val;
  2231. if (!io->string) {
  2232. if (io->in) {
  2233. val = kvm_register_read(vcpu, VCPU_REGS_RAX);
  2234. memcpy(&val, vcpu->arch.pio_data, io->size);
  2235. kvm_register_write(vcpu, VCPU_REGS_RAX, val);
  2236. }
  2237. } else {
  2238. if (io->in) {
  2239. r = pio_copy_data(vcpu);
  2240. if (r)
  2241. return r;
  2242. }
  2243. delta = 1;
  2244. if (io->rep) {
  2245. delta *= io->cur_count;
  2246. /*
  2247. * The size of the register should really depend on
  2248. * current address size.
  2249. */
  2250. val = kvm_register_read(vcpu, VCPU_REGS_RCX);
  2251. val -= delta;
  2252. kvm_register_write(vcpu, VCPU_REGS_RCX, val);
  2253. }
  2254. if (io->down)
  2255. delta = -delta;
  2256. delta *= io->size;
  2257. if (io->in) {
  2258. val = kvm_register_read(vcpu, VCPU_REGS_RDI);
  2259. val += delta;
  2260. kvm_register_write(vcpu, VCPU_REGS_RDI, val);
  2261. } else {
  2262. val = kvm_register_read(vcpu, VCPU_REGS_RSI);
  2263. val += delta;
  2264. kvm_register_write(vcpu, VCPU_REGS_RSI, val);
  2265. }
  2266. }
  2267. io->count -= io->cur_count;
  2268. io->cur_count = 0;
  2269. return 0;
  2270. }
  2271. static void kernel_pio(struct kvm_io_device *pio_dev,
  2272. struct kvm_vcpu *vcpu,
  2273. void *pd)
  2274. {
  2275. /* TODO: String I/O for in kernel device */
  2276. mutex_lock(&vcpu->kvm->lock);
  2277. if (vcpu->arch.pio.in)
  2278. kvm_iodevice_read(pio_dev, vcpu->arch.pio.port,
  2279. vcpu->arch.pio.size,
  2280. pd);
  2281. else
  2282. kvm_iodevice_write(pio_dev, vcpu->arch.pio.port,
  2283. vcpu->arch.pio.size,
  2284. pd);
  2285. mutex_unlock(&vcpu->kvm->lock);
  2286. }
  2287. static void pio_string_write(struct kvm_io_device *pio_dev,
  2288. struct kvm_vcpu *vcpu)
  2289. {
  2290. struct kvm_pio_request *io = &vcpu->arch.pio;
  2291. void *pd = vcpu->arch.pio_data;
  2292. int i;
  2293. mutex_lock(&vcpu->kvm->lock);
  2294. for (i = 0; i < io->cur_count; i++) {
  2295. kvm_iodevice_write(pio_dev, io->port,
  2296. io->size,
  2297. pd);
  2298. pd += io->size;
  2299. }
  2300. mutex_unlock(&vcpu->kvm->lock);
  2301. }
  2302. static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
  2303. gpa_t addr, int len,
  2304. int is_write)
  2305. {
  2306. return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr, len, is_write);
  2307. }
  2308. int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  2309. int size, unsigned port)
  2310. {
  2311. struct kvm_io_device *pio_dev;
  2312. unsigned long val;
  2313. vcpu->run->exit_reason = KVM_EXIT_IO;
  2314. vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
  2315. vcpu->run->io.size = vcpu->arch.pio.size = size;
  2316. vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
  2317. vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = 1;
  2318. vcpu->run->io.port = vcpu->arch.pio.port = port;
  2319. vcpu->arch.pio.in = in;
  2320. vcpu->arch.pio.string = 0;
  2321. vcpu->arch.pio.down = 0;
  2322. vcpu->arch.pio.guest_page_offset = 0;
  2323. vcpu->arch.pio.rep = 0;
  2324. if (vcpu->run->io.direction == KVM_EXIT_IO_IN)
  2325. KVMTRACE_2D(IO_READ, vcpu, vcpu->run->io.port, (u32)size,
  2326. handler);
  2327. else
  2328. KVMTRACE_2D(IO_WRITE, vcpu, vcpu->run->io.port, (u32)size,
  2329. handler);
  2330. val = kvm_register_read(vcpu, VCPU_REGS_RAX);
  2331. memcpy(vcpu->arch.pio_data, &val, 4);
  2332. kvm_x86_ops->skip_emulated_instruction(vcpu);
  2333. pio_dev = vcpu_find_pio_dev(vcpu, port, size, !in);
  2334. if (pio_dev) {
  2335. kernel_pio(pio_dev, vcpu, vcpu->arch.pio_data);
  2336. complete_pio(vcpu);
  2337. return 1;
  2338. }
  2339. return 0;
  2340. }
  2341. EXPORT_SYMBOL_GPL(kvm_emulate_pio);
  2342. int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  2343. int size, unsigned long count, int down,
  2344. gva_t address, int rep, unsigned port)
  2345. {
  2346. unsigned now, in_page;
  2347. int i, ret = 0;
  2348. int nr_pages = 1;
  2349. struct page *page;
  2350. struct kvm_io_device *pio_dev;
  2351. vcpu->run->exit_reason = KVM_EXIT_IO;
  2352. vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
  2353. vcpu->run->io.size = vcpu->arch.pio.size = size;
  2354. vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
  2355. vcpu->run->io.count = vcpu->arch.pio.count = vcpu->arch.pio.cur_count = count;
  2356. vcpu->run->io.port = vcpu->arch.pio.port = port;
  2357. vcpu->arch.pio.in = in;
  2358. vcpu->arch.pio.string = 1;
  2359. vcpu->arch.pio.down = down;
  2360. vcpu->arch.pio.guest_page_offset = offset_in_page(address);
  2361. vcpu->arch.pio.rep = rep;
  2362. if (vcpu->run->io.direction == KVM_EXIT_IO_IN)
  2363. KVMTRACE_2D(IO_READ, vcpu, vcpu->run->io.port, (u32)size,
  2364. handler);
  2365. else
  2366. KVMTRACE_2D(IO_WRITE, vcpu, vcpu->run->io.port, (u32)size,
  2367. handler);
  2368. if (!count) {
  2369. kvm_x86_ops->skip_emulated_instruction(vcpu);
  2370. return 1;
  2371. }
  2372. if (!down)
  2373. in_page = PAGE_SIZE - offset_in_page(address);
  2374. else
  2375. in_page = offset_in_page(address) + size;
  2376. now = min(count, (unsigned long)in_page / size);
  2377. if (!now) {
  2378. /*
  2379. * String I/O straddles page boundary. Pin two guest pages
  2380. * so that we satisfy atomicity constraints. Do just one
  2381. * transaction to avoid complexity.
  2382. */
  2383. nr_pages = 2;
  2384. now = 1;
  2385. }
  2386. if (down) {
  2387. /*
  2388. * String I/O in reverse. Yuck. Kill the guest, fix later.
  2389. */
  2390. pr_unimpl(vcpu, "guest string pio down\n");
  2391. kvm_inject_gp(vcpu, 0);
  2392. return 1;
  2393. }
  2394. vcpu->run->io.count = now;
  2395. vcpu->arch.pio.cur_count = now;
  2396. if (vcpu->arch.pio.cur_count == vcpu->arch.pio.count)
  2397. kvm_x86_ops->skip_emulated_instruction(vcpu);
  2398. for (i = 0; i < nr_pages; ++i) {
  2399. page = gva_to_page(vcpu, address + i * PAGE_SIZE);
  2400. vcpu->arch.pio.guest_pages[i] = page;
  2401. if (!page) {
  2402. kvm_inject_gp(vcpu, 0);
  2403. free_pio_guest_pages(vcpu);
  2404. return 1;
  2405. }
  2406. }
  2407. pio_dev = vcpu_find_pio_dev(vcpu, port,
  2408. vcpu->arch.pio.cur_count,
  2409. !vcpu->arch.pio.in);
  2410. if (!vcpu->arch.pio.in) {
  2411. /* string PIO write */
  2412. ret = pio_copy_data(vcpu);
  2413. if (ret >= 0 && pio_dev) {
  2414. pio_string_write(pio_dev, vcpu);
  2415. complete_pio(vcpu);
  2416. if (vcpu->arch.pio.count == 0)
  2417. ret = 1;
  2418. }
  2419. } else if (pio_dev)
  2420. pr_unimpl(vcpu, "no string pio read support yet, "
  2421. "port %x size %d count %ld\n",
  2422. port, size, count);
  2423. return ret;
  2424. }
  2425. EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
  2426. int kvm_arch_init(void *opaque)
  2427. {
  2428. int r;
  2429. struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
  2430. if (kvm_x86_ops) {
  2431. printk(KERN_ERR "kvm: already loaded the other module\n");
  2432. r = -EEXIST;
  2433. goto out;
  2434. }
  2435. if (!ops->cpu_has_kvm_support()) {
  2436. printk(KERN_ERR "kvm: no hardware support\n");
  2437. r = -EOPNOTSUPP;
  2438. goto out;
  2439. }
  2440. if (ops->disabled_by_bios()) {
  2441. printk(KERN_ERR "kvm: disabled by bios\n");
  2442. r = -EOPNOTSUPP;
  2443. goto out;
  2444. }
  2445. r = kvm_mmu_module_init();
  2446. if (r)
  2447. goto out;
  2448. kvm_init_msr_list();
  2449. kvm_x86_ops = ops;
  2450. kvm_mmu_set_nonpresent_ptes(0ull, 0ull);
  2451. kvm_mmu_set_base_ptes(PT_PRESENT_MASK);
  2452. kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
  2453. PT_DIRTY_MASK, PT64_NX_MASK, 0);
  2454. return 0;
  2455. out:
  2456. return r;
  2457. }
  2458. void kvm_arch_exit(void)
  2459. {
  2460. kvm_x86_ops = NULL;
  2461. kvm_mmu_module_exit();
  2462. }
  2463. int kvm_emulate_halt(struct kvm_vcpu *vcpu)
  2464. {
  2465. ++vcpu->stat.halt_exits;
  2466. KVMTRACE_0D(HLT, vcpu, handler);
  2467. if (irqchip_in_kernel(vcpu->kvm)) {
  2468. vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
  2469. return 1;
  2470. } else {
  2471. vcpu->run->exit_reason = KVM_EXIT_HLT;
  2472. return 0;
  2473. }
  2474. }
  2475. EXPORT_SYMBOL_GPL(kvm_emulate_halt);
  2476. static inline gpa_t hc_gpa(struct kvm_vcpu *vcpu, unsigned long a0,
  2477. unsigned long a1)
  2478. {
  2479. if (is_long_mode(vcpu))
  2480. return a0;
  2481. else
  2482. return a0 | ((gpa_t)a1 << 32);
  2483. }
  2484. int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
  2485. {
  2486. unsigned long nr, a0, a1, a2, a3, ret;
  2487. int r = 1;
  2488. nr = kvm_register_read(vcpu, VCPU_REGS_RAX);
  2489. a0 = kvm_register_read(vcpu, VCPU_REGS_RBX);
  2490. a1 = kvm_register_read(vcpu, VCPU_REGS_RCX);
  2491. a2 = kvm_register_read(vcpu, VCPU_REGS_RDX);
  2492. a3 = kvm_register_read(vcpu, VCPU_REGS_RSI);
  2493. KVMTRACE_1D(VMMCALL, vcpu, (u32)nr, handler);
  2494. if (!is_long_mode(vcpu)) {
  2495. nr &= 0xFFFFFFFF;
  2496. a0 &= 0xFFFFFFFF;
  2497. a1 &= 0xFFFFFFFF;
  2498. a2 &= 0xFFFFFFFF;
  2499. a3 &= 0xFFFFFFFF;
  2500. }
  2501. switch (nr) {
  2502. case KVM_HC_VAPIC_POLL_IRQ:
  2503. ret = 0;
  2504. break;
  2505. case KVM_HC_MMU_OP:
  2506. r = kvm_pv_mmu_op(vcpu, a0, hc_gpa(vcpu, a1, a2), &ret);
  2507. break;
  2508. default:
  2509. ret = -KVM_ENOSYS;
  2510. break;
  2511. }
  2512. kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
  2513. ++vcpu->stat.hypercalls;
  2514. return r;
  2515. }
  2516. EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
  2517. int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
  2518. {
  2519. char instruction[3];
  2520. int ret = 0;
  2521. unsigned long rip = kvm_rip_read(vcpu);
  2522. /*
  2523. * Blow out the MMU to ensure that no other VCPU has an active mapping
  2524. * to ensure that the updated hypercall appears atomically across all
  2525. * VCPUs.
  2526. */
  2527. kvm_mmu_zap_all(vcpu->kvm);
  2528. kvm_x86_ops->patch_hypercall(vcpu, instruction);
  2529. if (emulator_write_emulated(rip, instruction, 3, vcpu)
  2530. != X86EMUL_CONTINUE)
  2531. ret = -EFAULT;
  2532. return ret;
  2533. }
  2534. static u64 mk_cr_64(u64 curr_cr, u32 new_val)
  2535. {
  2536. return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
  2537. }
  2538. void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  2539. {
  2540. struct descriptor_table dt = { limit, base };
  2541. kvm_x86_ops->set_gdt(vcpu, &dt);
  2542. }
  2543. void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  2544. {
  2545. struct descriptor_table dt = { limit, base };
  2546. kvm_x86_ops->set_idt(vcpu, &dt);
  2547. }
  2548. void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
  2549. unsigned long *rflags)
  2550. {
  2551. kvm_lmsw(vcpu, msw);
  2552. *rflags = kvm_x86_ops->get_rflags(vcpu);
  2553. }
  2554. unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
  2555. {
  2556. unsigned long value;
  2557. kvm_x86_ops->decache_cr4_guest_bits(vcpu);
  2558. switch (cr) {
  2559. case 0:
  2560. value = vcpu->arch.cr0;
  2561. break;
  2562. case 2:
  2563. value = vcpu->arch.cr2;
  2564. break;
  2565. case 3:
  2566. value = vcpu->arch.cr3;
  2567. break;
  2568. case 4:
  2569. value = vcpu->arch.cr4;
  2570. break;
  2571. case 8:
  2572. value = kvm_get_cr8(vcpu);
  2573. break;
  2574. default:
  2575. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
  2576. return 0;
  2577. }
  2578. KVMTRACE_3D(CR_READ, vcpu, (u32)cr, (u32)value,
  2579. (u32)((u64)value >> 32), handler);
  2580. return value;
  2581. }
  2582. void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
  2583. unsigned long *rflags)
  2584. {
  2585. KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr, (u32)val,
  2586. (u32)((u64)val >> 32), handler);
  2587. switch (cr) {
  2588. case 0:
  2589. kvm_set_cr0(vcpu, mk_cr_64(vcpu->arch.cr0, val));
  2590. *rflags = kvm_x86_ops->get_rflags(vcpu);
  2591. break;
  2592. case 2:
  2593. vcpu->arch.cr2 = val;
  2594. break;
  2595. case 3:
  2596. kvm_set_cr3(vcpu, val);
  2597. break;
  2598. case 4:
  2599. kvm_set_cr4(vcpu, mk_cr_64(vcpu->arch.cr4, val));
  2600. break;
  2601. case 8:
  2602. kvm_set_cr8(vcpu, val & 0xfUL);
  2603. break;
  2604. default:
  2605. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __func__, cr);
  2606. }
  2607. }
  2608. static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
  2609. {
  2610. struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
  2611. int j, nent = vcpu->arch.cpuid_nent;
  2612. e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
  2613. /* when no next entry is found, the current entry[i] is reselected */
  2614. for (j = i + 1; j == i; j = (j + 1) % nent) {
  2615. struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
  2616. if (ej->function == e->function) {
  2617. ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
  2618. return j;
  2619. }
  2620. }
  2621. return 0; /* silence gcc, even though control never reaches here */
  2622. }
  2623. /* find an entry with matching function, matching index (if needed), and that
  2624. * should be read next (if it's stateful) */
  2625. static int is_matching_cpuid_entry(struct kvm_cpuid_entry2 *e,
  2626. u32 function, u32 index)
  2627. {
  2628. if (e->function != function)
  2629. return 0;
  2630. if ((e->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX) && e->index != index)
  2631. return 0;
  2632. if ((e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC) &&
  2633. !(e->flags & KVM_CPUID_FLAG_STATE_READ_NEXT))
  2634. return 0;
  2635. return 1;
  2636. }
  2637. void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
  2638. {
  2639. int i;
  2640. u32 function, index;
  2641. struct kvm_cpuid_entry2 *e, *best;
  2642. function = kvm_register_read(vcpu, VCPU_REGS_RAX);
  2643. index = kvm_register_read(vcpu, VCPU_REGS_RCX);
  2644. kvm_register_write(vcpu, VCPU_REGS_RAX, 0);
  2645. kvm_register_write(vcpu, VCPU_REGS_RBX, 0);
  2646. kvm_register_write(vcpu, VCPU_REGS_RCX, 0);
  2647. kvm_register_write(vcpu, VCPU_REGS_RDX, 0);
  2648. best = NULL;
  2649. for (i = 0; i < vcpu->arch.cpuid_nent; ++i) {
  2650. e = &vcpu->arch.cpuid_entries[i];
  2651. if (is_matching_cpuid_entry(e, function, index)) {
  2652. if (e->flags & KVM_CPUID_FLAG_STATEFUL_FUNC)
  2653. move_to_next_stateful_cpuid_entry(vcpu, i);
  2654. best = e;
  2655. break;
  2656. }
  2657. /*
  2658. * Both basic or both extended?
  2659. */
  2660. if (((e->function ^ function) & 0x80000000) == 0)
  2661. if (!best || e->function > best->function)
  2662. best = e;
  2663. }
  2664. if (best) {
  2665. kvm_register_write(vcpu, VCPU_REGS_RAX, best->eax);
  2666. kvm_register_write(vcpu, VCPU_REGS_RBX, best->ebx);
  2667. kvm_register_write(vcpu, VCPU_REGS_RCX, best->ecx);
  2668. kvm_register_write(vcpu, VCPU_REGS_RDX, best->edx);
  2669. }
  2670. kvm_x86_ops->skip_emulated_instruction(vcpu);
  2671. KVMTRACE_5D(CPUID, vcpu, function,
  2672. (u32)kvm_register_read(vcpu, VCPU_REGS_RAX),
  2673. (u32)kvm_register_read(vcpu, VCPU_REGS_RBX),
  2674. (u32)kvm_register_read(vcpu, VCPU_REGS_RCX),
  2675. (u32)kvm_register_read(vcpu, VCPU_REGS_RDX), handler);
  2676. }
  2677. EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
  2678. /*
  2679. * Check if userspace requested an interrupt window, and that the
  2680. * interrupt window is open.
  2681. *
  2682. * No need to exit to userspace if we already have an interrupt queued.
  2683. */
  2684. static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
  2685. struct kvm_run *kvm_run)
  2686. {
  2687. return (!vcpu->arch.irq_summary &&
  2688. kvm_run->request_interrupt_window &&
  2689. vcpu->arch.interrupt_window_open &&
  2690. (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
  2691. }
  2692. static void post_kvm_run_save(struct kvm_vcpu *vcpu,
  2693. struct kvm_run *kvm_run)
  2694. {
  2695. kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
  2696. kvm_run->cr8 = kvm_get_cr8(vcpu);
  2697. kvm_run->apic_base = kvm_get_apic_base(vcpu);
  2698. if (irqchip_in_kernel(vcpu->kvm))
  2699. kvm_run->ready_for_interrupt_injection = 1;
  2700. else
  2701. kvm_run->ready_for_interrupt_injection =
  2702. (vcpu->arch.interrupt_window_open &&
  2703. vcpu->arch.irq_summary == 0);
  2704. }
  2705. static void vapic_enter(struct kvm_vcpu *vcpu)
  2706. {
  2707. struct kvm_lapic *apic = vcpu->arch.apic;
  2708. struct page *page;
  2709. if (!apic || !apic->vapic_addr)
  2710. return;
  2711. page = gfn_to_page(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
  2712. vcpu->arch.apic->vapic_page = page;
  2713. }
  2714. static void vapic_exit(struct kvm_vcpu *vcpu)
  2715. {
  2716. struct kvm_lapic *apic = vcpu->arch.apic;
  2717. if (!apic || !apic->vapic_addr)
  2718. return;
  2719. down_read(&vcpu->kvm->slots_lock);
  2720. kvm_release_page_dirty(apic->vapic_page);
  2721. mark_page_dirty(vcpu->kvm, apic->vapic_addr >> PAGE_SHIFT);
  2722. up_read(&vcpu->kvm->slots_lock);
  2723. }
  2724. static int vcpu_enter_guest(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2725. {
  2726. int r;
  2727. if (vcpu->requests)
  2728. if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
  2729. kvm_mmu_unload(vcpu);
  2730. r = kvm_mmu_reload(vcpu);
  2731. if (unlikely(r))
  2732. goto out;
  2733. if (vcpu->requests) {
  2734. if (test_and_clear_bit(KVM_REQ_MIGRATE_TIMER, &vcpu->requests))
  2735. __kvm_migrate_timers(vcpu);
  2736. if (test_and_clear_bit(KVM_REQ_MMU_SYNC, &vcpu->requests))
  2737. kvm_mmu_sync_roots(vcpu);
  2738. if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
  2739. kvm_x86_ops->tlb_flush(vcpu);
  2740. if (test_and_clear_bit(KVM_REQ_REPORT_TPR_ACCESS,
  2741. &vcpu->requests)) {
  2742. kvm_run->exit_reason = KVM_EXIT_TPR_ACCESS;
  2743. r = 0;
  2744. goto out;
  2745. }
  2746. if (test_and_clear_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests)) {
  2747. kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
  2748. r = 0;
  2749. goto out;
  2750. }
  2751. }
  2752. clear_bit(KVM_REQ_PENDING_TIMER, &vcpu->requests);
  2753. kvm_inject_pending_timer_irqs(vcpu);
  2754. preempt_disable();
  2755. kvm_x86_ops->prepare_guest_switch(vcpu);
  2756. kvm_load_guest_fpu(vcpu);
  2757. local_irq_disable();
  2758. if (vcpu->requests || need_resched() || signal_pending(current)) {
  2759. local_irq_enable();
  2760. preempt_enable();
  2761. r = 1;
  2762. goto out;
  2763. }
  2764. if (vcpu->guest_debug.enabled)
  2765. kvm_x86_ops->guest_debug_pre(vcpu);
  2766. vcpu->guest_mode = 1;
  2767. /*
  2768. * Make sure that guest_mode assignment won't happen after
  2769. * testing the pending IRQ vector bitmap.
  2770. */
  2771. smp_wmb();
  2772. if (vcpu->arch.exception.pending)
  2773. __queue_exception(vcpu);
  2774. else if (irqchip_in_kernel(vcpu->kvm))
  2775. kvm_x86_ops->inject_pending_irq(vcpu);
  2776. else
  2777. kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
  2778. kvm_lapic_sync_to_vapic(vcpu);
  2779. up_read(&vcpu->kvm->slots_lock);
  2780. kvm_guest_enter();
  2781. KVMTRACE_0D(VMENTRY, vcpu, entryexit);
  2782. kvm_x86_ops->run(vcpu, kvm_run);
  2783. vcpu->guest_mode = 0;
  2784. local_irq_enable();
  2785. ++vcpu->stat.exits;
  2786. /*
  2787. * We must have an instruction between local_irq_enable() and
  2788. * kvm_guest_exit(), so the timer interrupt isn't delayed by
  2789. * the interrupt shadow. The stat.exits increment will do nicely.
  2790. * But we need to prevent reordering, hence this barrier():
  2791. */
  2792. barrier();
  2793. kvm_guest_exit();
  2794. preempt_enable();
  2795. down_read(&vcpu->kvm->slots_lock);
  2796. /*
  2797. * Profile KVM exit RIPs:
  2798. */
  2799. if (unlikely(prof_on == KVM_PROFILING)) {
  2800. unsigned long rip = kvm_rip_read(vcpu);
  2801. profile_hit(KVM_PROFILING, (void *)rip);
  2802. }
  2803. if (vcpu->arch.exception.pending && kvm_x86_ops->exception_injected(vcpu))
  2804. vcpu->arch.exception.pending = false;
  2805. kvm_lapic_sync_from_vapic(vcpu);
  2806. r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
  2807. out:
  2808. return r;
  2809. }
  2810. static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2811. {
  2812. int r;
  2813. if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED)) {
  2814. printk("vcpu %d received sipi with vector # %x\n",
  2815. vcpu->vcpu_id, vcpu->arch.sipi_vector);
  2816. kvm_lapic_reset(vcpu);
  2817. r = kvm_x86_ops->vcpu_reset(vcpu);
  2818. if (r)
  2819. return r;
  2820. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  2821. }
  2822. down_read(&vcpu->kvm->slots_lock);
  2823. vapic_enter(vcpu);
  2824. r = 1;
  2825. while (r > 0) {
  2826. if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
  2827. r = vcpu_enter_guest(vcpu, kvm_run);
  2828. else {
  2829. up_read(&vcpu->kvm->slots_lock);
  2830. kvm_vcpu_block(vcpu);
  2831. down_read(&vcpu->kvm->slots_lock);
  2832. if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests))
  2833. if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
  2834. vcpu->arch.mp_state =
  2835. KVM_MP_STATE_RUNNABLE;
  2836. if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE)
  2837. r = -EINTR;
  2838. }
  2839. if (r > 0) {
  2840. if (dm_request_for_irq_injection(vcpu, kvm_run)) {
  2841. r = -EINTR;
  2842. kvm_run->exit_reason = KVM_EXIT_INTR;
  2843. ++vcpu->stat.request_irq_exits;
  2844. }
  2845. if (signal_pending(current)) {
  2846. r = -EINTR;
  2847. kvm_run->exit_reason = KVM_EXIT_INTR;
  2848. ++vcpu->stat.signal_exits;
  2849. }
  2850. if (need_resched()) {
  2851. up_read(&vcpu->kvm->slots_lock);
  2852. kvm_resched(vcpu);
  2853. down_read(&vcpu->kvm->slots_lock);
  2854. }
  2855. }
  2856. }
  2857. up_read(&vcpu->kvm->slots_lock);
  2858. post_kvm_run_save(vcpu, kvm_run);
  2859. vapic_exit(vcpu);
  2860. return r;
  2861. }
  2862. int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  2863. {
  2864. int r;
  2865. sigset_t sigsaved;
  2866. vcpu_load(vcpu);
  2867. if (vcpu->sigset_active)
  2868. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  2869. if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
  2870. kvm_vcpu_block(vcpu);
  2871. clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
  2872. r = -EAGAIN;
  2873. goto out;
  2874. }
  2875. /* re-sync apic's tpr */
  2876. if (!irqchip_in_kernel(vcpu->kvm))
  2877. kvm_set_cr8(vcpu, kvm_run->cr8);
  2878. if (vcpu->arch.pio.cur_count) {
  2879. r = complete_pio(vcpu);
  2880. if (r)
  2881. goto out;
  2882. }
  2883. #if CONFIG_HAS_IOMEM
  2884. if (vcpu->mmio_needed) {
  2885. memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
  2886. vcpu->mmio_read_completed = 1;
  2887. vcpu->mmio_needed = 0;
  2888. down_read(&vcpu->kvm->slots_lock);
  2889. r = emulate_instruction(vcpu, kvm_run,
  2890. vcpu->arch.mmio_fault_cr2, 0,
  2891. EMULTYPE_NO_DECODE);
  2892. up_read(&vcpu->kvm->slots_lock);
  2893. if (r == EMULATE_DO_MMIO) {
  2894. /*
  2895. * Read-modify-write. Back to userspace.
  2896. */
  2897. r = 0;
  2898. goto out;
  2899. }
  2900. }
  2901. #endif
  2902. if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL)
  2903. kvm_register_write(vcpu, VCPU_REGS_RAX,
  2904. kvm_run->hypercall.ret);
  2905. r = __vcpu_run(vcpu, kvm_run);
  2906. out:
  2907. if (vcpu->sigset_active)
  2908. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  2909. vcpu_put(vcpu);
  2910. return r;
  2911. }
  2912. int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  2913. {
  2914. vcpu_load(vcpu);
  2915. regs->rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
  2916. regs->rbx = kvm_register_read(vcpu, VCPU_REGS_RBX);
  2917. regs->rcx = kvm_register_read(vcpu, VCPU_REGS_RCX);
  2918. regs->rdx = kvm_register_read(vcpu, VCPU_REGS_RDX);
  2919. regs->rsi = kvm_register_read(vcpu, VCPU_REGS_RSI);
  2920. regs->rdi = kvm_register_read(vcpu, VCPU_REGS_RDI);
  2921. regs->rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  2922. regs->rbp = kvm_register_read(vcpu, VCPU_REGS_RBP);
  2923. #ifdef CONFIG_X86_64
  2924. regs->r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
  2925. regs->r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
  2926. regs->r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
  2927. regs->r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
  2928. regs->r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
  2929. regs->r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
  2930. regs->r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
  2931. regs->r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
  2932. #endif
  2933. regs->rip = kvm_rip_read(vcpu);
  2934. regs->rflags = kvm_x86_ops->get_rflags(vcpu);
  2935. /*
  2936. * Don't leak debug flags in case they were set for guest debugging
  2937. */
  2938. if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
  2939. regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
  2940. vcpu_put(vcpu);
  2941. return 0;
  2942. }
  2943. int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
  2944. {
  2945. vcpu_load(vcpu);
  2946. kvm_register_write(vcpu, VCPU_REGS_RAX, regs->rax);
  2947. kvm_register_write(vcpu, VCPU_REGS_RBX, regs->rbx);
  2948. kvm_register_write(vcpu, VCPU_REGS_RCX, regs->rcx);
  2949. kvm_register_write(vcpu, VCPU_REGS_RDX, regs->rdx);
  2950. kvm_register_write(vcpu, VCPU_REGS_RSI, regs->rsi);
  2951. kvm_register_write(vcpu, VCPU_REGS_RDI, regs->rdi);
  2952. kvm_register_write(vcpu, VCPU_REGS_RSP, regs->rsp);
  2953. kvm_register_write(vcpu, VCPU_REGS_RBP, regs->rbp);
  2954. #ifdef CONFIG_X86_64
  2955. kvm_register_write(vcpu, VCPU_REGS_R8, regs->r8);
  2956. kvm_register_write(vcpu, VCPU_REGS_R9, regs->r9);
  2957. kvm_register_write(vcpu, VCPU_REGS_R10, regs->r10);
  2958. kvm_register_write(vcpu, VCPU_REGS_R11, regs->r11);
  2959. kvm_register_write(vcpu, VCPU_REGS_R12, regs->r12);
  2960. kvm_register_write(vcpu, VCPU_REGS_R13, regs->r13);
  2961. kvm_register_write(vcpu, VCPU_REGS_R14, regs->r14);
  2962. kvm_register_write(vcpu, VCPU_REGS_R15, regs->r15);
  2963. #endif
  2964. kvm_rip_write(vcpu, regs->rip);
  2965. kvm_x86_ops->set_rflags(vcpu, regs->rflags);
  2966. vcpu->arch.exception.pending = false;
  2967. vcpu_put(vcpu);
  2968. return 0;
  2969. }
  2970. void kvm_get_segment(struct kvm_vcpu *vcpu,
  2971. struct kvm_segment *var, int seg)
  2972. {
  2973. kvm_x86_ops->get_segment(vcpu, var, seg);
  2974. }
  2975. void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
  2976. {
  2977. struct kvm_segment cs;
  2978. kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
  2979. *db = cs.db;
  2980. *l = cs.l;
  2981. }
  2982. EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
  2983. int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  2984. struct kvm_sregs *sregs)
  2985. {
  2986. struct descriptor_table dt;
  2987. int pending_vec;
  2988. vcpu_load(vcpu);
  2989. kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  2990. kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  2991. kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  2992. kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  2993. kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  2994. kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  2995. kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  2996. kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  2997. kvm_x86_ops->get_idt(vcpu, &dt);
  2998. sregs->idt.limit = dt.limit;
  2999. sregs->idt.base = dt.base;
  3000. kvm_x86_ops->get_gdt(vcpu, &dt);
  3001. sregs->gdt.limit = dt.limit;
  3002. sregs->gdt.base = dt.base;
  3003. kvm_x86_ops->decache_cr4_guest_bits(vcpu);
  3004. sregs->cr0 = vcpu->arch.cr0;
  3005. sregs->cr2 = vcpu->arch.cr2;
  3006. sregs->cr3 = vcpu->arch.cr3;
  3007. sregs->cr4 = vcpu->arch.cr4;
  3008. sregs->cr8 = kvm_get_cr8(vcpu);
  3009. sregs->efer = vcpu->arch.shadow_efer;
  3010. sregs->apic_base = kvm_get_apic_base(vcpu);
  3011. if (irqchip_in_kernel(vcpu->kvm)) {
  3012. memset(sregs->interrupt_bitmap, 0,
  3013. sizeof sregs->interrupt_bitmap);
  3014. pending_vec = kvm_x86_ops->get_irq(vcpu);
  3015. if (pending_vec >= 0)
  3016. set_bit(pending_vec,
  3017. (unsigned long *)sregs->interrupt_bitmap);
  3018. } else
  3019. memcpy(sregs->interrupt_bitmap, vcpu->arch.irq_pending,
  3020. sizeof sregs->interrupt_bitmap);
  3021. vcpu_put(vcpu);
  3022. return 0;
  3023. }
  3024. int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
  3025. struct kvm_mp_state *mp_state)
  3026. {
  3027. vcpu_load(vcpu);
  3028. mp_state->mp_state = vcpu->arch.mp_state;
  3029. vcpu_put(vcpu);
  3030. return 0;
  3031. }
  3032. int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
  3033. struct kvm_mp_state *mp_state)
  3034. {
  3035. vcpu_load(vcpu);
  3036. vcpu->arch.mp_state = mp_state->mp_state;
  3037. vcpu_put(vcpu);
  3038. return 0;
  3039. }
  3040. static void kvm_set_segment(struct kvm_vcpu *vcpu,
  3041. struct kvm_segment *var, int seg)
  3042. {
  3043. kvm_x86_ops->set_segment(vcpu, var, seg);
  3044. }
  3045. static void seg_desct_to_kvm_desct(struct desc_struct *seg_desc, u16 selector,
  3046. struct kvm_segment *kvm_desct)
  3047. {
  3048. kvm_desct->base = seg_desc->base0;
  3049. kvm_desct->base |= seg_desc->base1 << 16;
  3050. kvm_desct->base |= seg_desc->base2 << 24;
  3051. kvm_desct->limit = seg_desc->limit0;
  3052. kvm_desct->limit |= seg_desc->limit << 16;
  3053. if (seg_desc->g) {
  3054. kvm_desct->limit <<= 12;
  3055. kvm_desct->limit |= 0xfff;
  3056. }
  3057. kvm_desct->selector = selector;
  3058. kvm_desct->type = seg_desc->type;
  3059. kvm_desct->present = seg_desc->p;
  3060. kvm_desct->dpl = seg_desc->dpl;
  3061. kvm_desct->db = seg_desc->d;
  3062. kvm_desct->s = seg_desc->s;
  3063. kvm_desct->l = seg_desc->l;
  3064. kvm_desct->g = seg_desc->g;
  3065. kvm_desct->avl = seg_desc->avl;
  3066. if (!selector)
  3067. kvm_desct->unusable = 1;
  3068. else
  3069. kvm_desct->unusable = 0;
  3070. kvm_desct->padding = 0;
  3071. }
  3072. static void get_segment_descritptor_dtable(struct kvm_vcpu *vcpu,
  3073. u16 selector,
  3074. struct descriptor_table *dtable)
  3075. {
  3076. if (selector & 1 << 2) {
  3077. struct kvm_segment kvm_seg;
  3078. kvm_get_segment(vcpu, &kvm_seg, VCPU_SREG_LDTR);
  3079. if (kvm_seg.unusable)
  3080. dtable->limit = 0;
  3081. else
  3082. dtable->limit = kvm_seg.limit;
  3083. dtable->base = kvm_seg.base;
  3084. }
  3085. else
  3086. kvm_x86_ops->get_gdt(vcpu, dtable);
  3087. }
  3088. /* allowed just for 8 bytes segments */
  3089. static int load_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
  3090. struct desc_struct *seg_desc)
  3091. {
  3092. gpa_t gpa;
  3093. struct descriptor_table dtable;
  3094. u16 index = selector >> 3;
  3095. get_segment_descritptor_dtable(vcpu, selector, &dtable);
  3096. if (dtable.limit < index * 8 + 7) {
  3097. kvm_queue_exception_e(vcpu, GP_VECTOR, selector & 0xfffc);
  3098. return 1;
  3099. }
  3100. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, dtable.base);
  3101. gpa += index * 8;
  3102. return kvm_read_guest(vcpu->kvm, gpa, seg_desc, 8);
  3103. }
  3104. /* allowed just for 8 bytes segments */
  3105. static int save_guest_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
  3106. struct desc_struct *seg_desc)
  3107. {
  3108. gpa_t gpa;
  3109. struct descriptor_table dtable;
  3110. u16 index = selector >> 3;
  3111. get_segment_descritptor_dtable(vcpu, selector, &dtable);
  3112. if (dtable.limit < index * 8 + 7)
  3113. return 1;
  3114. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, dtable.base);
  3115. gpa += index * 8;
  3116. return kvm_write_guest(vcpu->kvm, gpa, seg_desc, 8);
  3117. }
  3118. static u32 get_tss_base_addr(struct kvm_vcpu *vcpu,
  3119. struct desc_struct *seg_desc)
  3120. {
  3121. u32 base_addr;
  3122. base_addr = seg_desc->base0;
  3123. base_addr |= (seg_desc->base1 << 16);
  3124. base_addr |= (seg_desc->base2 << 24);
  3125. return vcpu->arch.mmu.gva_to_gpa(vcpu, base_addr);
  3126. }
  3127. static u16 get_segment_selector(struct kvm_vcpu *vcpu, int seg)
  3128. {
  3129. struct kvm_segment kvm_seg;
  3130. kvm_get_segment(vcpu, &kvm_seg, seg);
  3131. return kvm_seg.selector;
  3132. }
  3133. static int load_segment_descriptor_to_kvm_desct(struct kvm_vcpu *vcpu,
  3134. u16 selector,
  3135. struct kvm_segment *kvm_seg)
  3136. {
  3137. struct desc_struct seg_desc;
  3138. if (load_guest_segment_descriptor(vcpu, selector, &seg_desc))
  3139. return 1;
  3140. seg_desct_to_kvm_desct(&seg_desc, selector, kvm_seg);
  3141. return 0;
  3142. }
  3143. static int kvm_load_realmode_segment(struct kvm_vcpu *vcpu, u16 selector, int seg)
  3144. {
  3145. struct kvm_segment segvar = {
  3146. .base = selector << 4,
  3147. .limit = 0xffff,
  3148. .selector = selector,
  3149. .type = 3,
  3150. .present = 1,
  3151. .dpl = 3,
  3152. .db = 0,
  3153. .s = 1,
  3154. .l = 0,
  3155. .g = 0,
  3156. .avl = 0,
  3157. .unusable = 0,
  3158. };
  3159. kvm_x86_ops->set_segment(vcpu, &segvar, seg);
  3160. return 0;
  3161. }
  3162. int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
  3163. int type_bits, int seg)
  3164. {
  3165. struct kvm_segment kvm_seg;
  3166. if (!(vcpu->arch.cr0 & X86_CR0_PE))
  3167. return kvm_load_realmode_segment(vcpu, selector, seg);
  3168. if (load_segment_descriptor_to_kvm_desct(vcpu, selector, &kvm_seg))
  3169. return 1;
  3170. kvm_seg.type |= type_bits;
  3171. if (seg != VCPU_SREG_SS && seg != VCPU_SREG_CS &&
  3172. seg != VCPU_SREG_LDTR)
  3173. if (!kvm_seg.s)
  3174. kvm_seg.unusable = 1;
  3175. kvm_set_segment(vcpu, &kvm_seg, seg);
  3176. return 0;
  3177. }
  3178. static void save_state_to_tss32(struct kvm_vcpu *vcpu,
  3179. struct tss_segment_32 *tss)
  3180. {
  3181. tss->cr3 = vcpu->arch.cr3;
  3182. tss->eip = kvm_rip_read(vcpu);
  3183. tss->eflags = kvm_x86_ops->get_rflags(vcpu);
  3184. tss->eax = kvm_register_read(vcpu, VCPU_REGS_RAX);
  3185. tss->ecx = kvm_register_read(vcpu, VCPU_REGS_RCX);
  3186. tss->edx = kvm_register_read(vcpu, VCPU_REGS_RDX);
  3187. tss->ebx = kvm_register_read(vcpu, VCPU_REGS_RBX);
  3188. tss->esp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  3189. tss->ebp = kvm_register_read(vcpu, VCPU_REGS_RBP);
  3190. tss->esi = kvm_register_read(vcpu, VCPU_REGS_RSI);
  3191. tss->edi = kvm_register_read(vcpu, VCPU_REGS_RDI);
  3192. tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
  3193. tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
  3194. tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
  3195. tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
  3196. tss->fs = get_segment_selector(vcpu, VCPU_SREG_FS);
  3197. tss->gs = get_segment_selector(vcpu, VCPU_SREG_GS);
  3198. tss->ldt_selector = get_segment_selector(vcpu, VCPU_SREG_LDTR);
  3199. tss->prev_task_link = get_segment_selector(vcpu, VCPU_SREG_TR);
  3200. }
  3201. static int load_state_from_tss32(struct kvm_vcpu *vcpu,
  3202. struct tss_segment_32 *tss)
  3203. {
  3204. kvm_set_cr3(vcpu, tss->cr3);
  3205. kvm_rip_write(vcpu, tss->eip);
  3206. kvm_x86_ops->set_rflags(vcpu, tss->eflags | 2);
  3207. kvm_register_write(vcpu, VCPU_REGS_RAX, tss->eax);
  3208. kvm_register_write(vcpu, VCPU_REGS_RCX, tss->ecx);
  3209. kvm_register_write(vcpu, VCPU_REGS_RDX, tss->edx);
  3210. kvm_register_write(vcpu, VCPU_REGS_RBX, tss->ebx);
  3211. kvm_register_write(vcpu, VCPU_REGS_RSP, tss->esp);
  3212. kvm_register_write(vcpu, VCPU_REGS_RBP, tss->ebp);
  3213. kvm_register_write(vcpu, VCPU_REGS_RSI, tss->esi);
  3214. kvm_register_write(vcpu, VCPU_REGS_RDI, tss->edi);
  3215. if (kvm_load_segment_descriptor(vcpu, tss->ldt_selector, 0, VCPU_SREG_LDTR))
  3216. return 1;
  3217. if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
  3218. return 1;
  3219. if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
  3220. return 1;
  3221. if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
  3222. return 1;
  3223. if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
  3224. return 1;
  3225. if (kvm_load_segment_descriptor(vcpu, tss->fs, 1, VCPU_SREG_FS))
  3226. return 1;
  3227. if (kvm_load_segment_descriptor(vcpu, tss->gs, 1, VCPU_SREG_GS))
  3228. return 1;
  3229. return 0;
  3230. }
  3231. static void save_state_to_tss16(struct kvm_vcpu *vcpu,
  3232. struct tss_segment_16 *tss)
  3233. {
  3234. tss->ip = kvm_rip_read(vcpu);
  3235. tss->flag = kvm_x86_ops->get_rflags(vcpu);
  3236. tss->ax = kvm_register_read(vcpu, VCPU_REGS_RAX);
  3237. tss->cx = kvm_register_read(vcpu, VCPU_REGS_RCX);
  3238. tss->dx = kvm_register_read(vcpu, VCPU_REGS_RDX);
  3239. tss->bx = kvm_register_read(vcpu, VCPU_REGS_RBX);
  3240. tss->sp = kvm_register_read(vcpu, VCPU_REGS_RSP);
  3241. tss->bp = kvm_register_read(vcpu, VCPU_REGS_RBP);
  3242. tss->si = kvm_register_read(vcpu, VCPU_REGS_RSI);
  3243. tss->di = kvm_register_read(vcpu, VCPU_REGS_RDI);
  3244. tss->es = get_segment_selector(vcpu, VCPU_SREG_ES);
  3245. tss->cs = get_segment_selector(vcpu, VCPU_SREG_CS);
  3246. tss->ss = get_segment_selector(vcpu, VCPU_SREG_SS);
  3247. tss->ds = get_segment_selector(vcpu, VCPU_SREG_DS);
  3248. tss->ldt = get_segment_selector(vcpu, VCPU_SREG_LDTR);
  3249. tss->prev_task_link = get_segment_selector(vcpu, VCPU_SREG_TR);
  3250. }
  3251. static int load_state_from_tss16(struct kvm_vcpu *vcpu,
  3252. struct tss_segment_16 *tss)
  3253. {
  3254. kvm_rip_write(vcpu, tss->ip);
  3255. kvm_x86_ops->set_rflags(vcpu, tss->flag | 2);
  3256. kvm_register_write(vcpu, VCPU_REGS_RAX, tss->ax);
  3257. kvm_register_write(vcpu, VCPU_REGS_RCX, tss->cx);
  3258. kvm_register_write(vcpu, VCPU_REGS_RDX, tss->dx);
  3259. kvm_register_write(vcpu, VCPU_REGS_RBX, tss->bx);
  3260. kvm_register_write(vcpu, VCPU_REGS_RSP, tss->sp);
  3261. kvm_register_write(vcpu, VCPU_REGS_RBP, tss->bp);
  3262. kvm_register_write(vcpu, VCPU_REGS_RSI, tss->si);
  3263. kvm_register_write(vcpu, VCPU_REGS_RDI, tss->di);
  3264. if (kvm_load_segment_descriptor(vcpu, tss->ldt, 0, VCPU_SREG_LDTR))
  3265. return 1;
  3266. if (kvm_load_segment_descriptor(vcpu, tss->es, 1, VCPU_SREG_ES))
  3267. return 1;
  3268. if (kvm_load_segment_descriptor(vcpu, tss->cs, 9, VCPU_SREG_CS))
  3269. return 1;
  3270. if (kvm_load_segment_descriptor(vcpu, tss->ss, 1, VCPU_SREG_SS))
  3271. return 1;
  3272. if (kvm_load_segment_descriptor(vcpu, tss->ds, 1, VCPU_SREG_DS))
  3273. return 1;
  3274. return 0;
  3275. }
  3276. static int kvm_task_switch_16(struct kvm_vcpu *vcpu, u16 tss_selector,
  3277. u32 old_tss_base,
  3278. struct desc_struct *nseg_desc)
  3279. {
  3280. struct tss_segment_16 tss_segment_16;
  3281. int ret = 0;
  3282. if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
  3283. sizeof tss_segment_16))
  3284. goto out;
  3285. save_state_to_tss16(vcpu, &tss_segment_16);
  3286. if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_16,
  3287. sizeof tss_segment_16))
  3288. goto out;
  3289. if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
  3290. &tss_segment_16, sizeof tss_segment_16))
  3291. goto out;
  3292. if (load_state_from_tss16(vcpu, &tss_segment_16))
  3293. goto out;
  3294. ret = 1;
  3295. out:
  3296. return ret;
  3297. }
  3298. static int kvm_task_switch_32(struct kvm_vcpu *vcpu, u16 tss_selector,
  3299. u32 old_tss_base,
  3300. struct desc_struct *nseg_desc)
  3301. {
  3302. struct tss_segment_32 tss_segment_32;
  3303. int ret = 0;
  3304. if (kvm_read_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
  3305. sizeof tss_segment_32))
  3306. goto out;
  3307. save_state_to_tss32(vcpu, &tss_segment_32);
  3308. if (kvm_write_guest(vcpu->kvm, old_tss_base, &tss_segment_32,
  3309. sizeof tss_segment_32))
  3310. goto out;
  3311. if (kvm_read_guest(vcpu->kvm, get_tss_base_addr(vcpu, nseg_desc),
  3312. &tss_segment_32, sizeof tss_segment_32))
  3313. goto out;
  3314. if (load_state_from_tss32(vcpu, &tss_segment_32))
  3315. goto out;
  3316. ret = 1;
  3317. out:
  3318. return ret;
  3319. }
  3320. int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason)
  3321. {
  3322. struct kvm_segment tr_seg;
  3323. struct desc_struct cseg_desc;
  3324. struct desc_struct nseg_desc;
  3325. int ret = 0;
  3326. u32 old_tss_base = get_segment_base(vcpu, VCPU_SREG_TR);
  3327. u16 old_tss_sel = get_segment_selector(vcpu, VCPU_SREG_TR);
  3328. old_tss_base = vcpu->arch.mmu.gva_to_gpa(vcpu, old_tss_base);
  3329. /* FIXME: Handle errors. Failure to read either TSS or their
  3330. * descriptors should generate a pagefault.
  3331. */
  3332. if (load_guest_segment_descriptor(vcpu, tss_selector, &nseg_desc))
  3333. goto out;
  3334. if (load_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc))
  3335. goto out;
  3336. if (reason != TASK_SWITCH_IRET) {
  3337. int cpl;
  3338. cpl = kvm_x86_ops->get_cpl(vcpu);
  3339. if ((tss_selector & 3) > nseg_desc.dpl || cpl > nseg_desc.dpl) {
  3340. kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
  3341. return 1;
  3342. }
  3343. }
  3344. if (!nseg_desc.p || (nseg_desc.limit0 | nseg_desc.limit << 16) < 0x67) {
  3345. kvm_queue_exception_e(vcpu, TS_VECTOR, tss_selector & 0xfffc);
  3346. return 1;
  3347. }
  3348. if (reason == TASK_SWITCH_IRET || reason == TASK_SWITCH_JMP) {
  3349. cseg_desc.type &= ~(1 << 1); //clear the B flag
  3350. save_guest_segment_descriptor(vcpu, old_tss_sel, &cseg_desc);
  3351. }
  3352. if (reason == TASK_SWITCH_IRET) {
  3353. u32 eflags = kvm_x86_ops->get_rflags(vcpu);
  3354. kvm_x86_ops->set_rflags(vcpu, eflags & ~X86_EFLAGS_NT);
  3355. }
  3356. kvm_x86_ops->skip_emulated_instruction(vcpu);
  3357. if (nseg_desc.type & 8)
  3358. ret = kvm_task_switch_32(vcpu, tss_selector, old_tss_base,
  3359. &nseg_desc);
  3360. else
  3361. ret = kvm_task_switch_16(vcpu, tss_selector, old_tss_base,
  3362. &nseg_desc);
  3363. if (reason == TASK_SWITCH_CALL || reason == TASK_SWITCH_GATE) {
  3364. u32 eflags = kvm_x86_ops->get_rflags(vcpu);
  3365. kvm_x86_ops->set_rflags(vcpu, eflags | X86_EFLAGS_NT);
  3366. }
  3367. if (reason != TASK_SWITCH_IRET) {
  3368. nseg_desc.type |= (1 << 1);
  3369. save_guest_segment_descriptor(vcpu, tss_selector,
  3370. &nseg_desc);
  3371. }
  3372. kvm_x86_ops->set_cr0(vcpu, vcpu->arch.cr0 | X86_CR0_TS);
  3373. seg_desct_to_kvm_desct(&nseg_desc, tss_selector, &tr_seg);
  3374. tr_seg.type = 11;
  3375. kvm_set_segment(vcpu, &tr_seg, VCPU_SREG_TR);
  3376. out:
  3377. return ret;
  3378. }
  3379. EXPORT_SYMBOL_GPL(kvm_task_switch);
  3380. int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  3381. struct kvm_sregs *sregs)
  3382. {
  3383. int mmu_reset_needed = 0;
  3384. int i, pending_vec, max_bits;
  3385. struct descriptor_table dt;
  3386. vcpu_load(vcpu);
  3387. dt.limit = sregs->idt.limit;
  3388. dt.base = sregs->idt.base;
  3389. kvm_x86_ops->set_idt(vcpu, &dt);
  3390. dt.limit = sregs->gdt.limit;
  3391. dt.base = sregs->gdt.base;
  3392. kvm_x86_ops->set_gdt(vcpu, &dt);
  3393. vcpu->arch.cr2 = sregs->cr2;
  3394. mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3;
  3395. vcpu->arch.cr3 = sregs->cr3;
  3396. kvm_set_cr8(vcpu, sregs->cr8);
  3397. mmu_reset_needed |= vcpu->arch.shadow_efer != sregs->efer;
  3398. kvm_x86_ops->set_efer(vcpu, sregs->efer);
  3399. kvm_set_apic_base(vcpu, sregs->apic_base);
  3400. kvm_x86_ops->decache_cr4_guest_bits(vcpu);
  3401. mmu_reset_needed |= vcpu->arch.cr0 != sregs->cr0;
  3402. kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
  3403. vcpu->arch.cr0 = sregs->cr0;
  3404. mmu_reset_needed |= vcpu->arch.cr4 != sregs->cr4;
  3405. kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
  3406. if (!is_long_mode(vcpu) && is_pae(vcpu))
  3407. load_pdptrs(vcpu, vcpu->arch.cr3);
  3408. if (mmu_reset_needed)
  3409. kvm_mmu_reset_context(vcpu);
  3410. if (!irqchip_in_kernel(vcpu->kvm)) {
  3411. memcpy(vcpu->arch.irq_pending, sregs->interrupt_bitmap,
  3412. sizeof vcpu->arch.irq_pending);
  3413. vcpu->arch.irq_summary = 0;
  3414. for (i = 0; i < ARRAY_SIZE(vcpu->arch.irq_pending); ++i)
  3415. if (vcpu->arch.irq_pending[i])
  3416. __set_bit(i, &vcpu->arch.irq_summary);
  3417. } else {
  3418. max_bits = (sizeof sregs->interrupt_bitmap) << 3;
  3419. pending_vec = find_first_bit(
  3420. (const unsigned long *)sregs->interrupt_bitmap,
  3421. max_bits);
  3422. /* Only pending external irq is handled here */
  3423. if (pending_vec < max_bits) {
  3424. kvm_x86_ops->set_irq(vcpu, pending_vec);
  3425. pr_debug("Set back pending irq %d\n",
  3426. pending_vec);
  3427. }
  3428. kvm_pic_clear_isr_ack(vcpu->kvm);
  3429. }
  3430. kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  3431. kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  3432. kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  3433. kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  3434. kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  3435. kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  3436. kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  3437. kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  3438. /* Older userspace won't unhalt the vcpu on reset. */
  3439. if (vcpu->vcpu_id == 0 && kvm_rip_read(vcpu) == 0xfff0 &&
  3440. sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
  3441. !(vcpu->arch.cr0 & X86_CR0_PE))
  3442. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  3443. vcpu_put(vcpu);
  3444. return 0;
  3445. }
  3446. int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
  3447. struct kvm_debug_guest *dbg)
  3448. {
  3449. int r;
  3450. vcpu_load(vcpu);
  3451. r = kvm_x86_ops->set_guest_debug(vcpu, dbg);
  3452. vcpu_put(vcpu);
  3453. return r;
  3454. }
  3455. /*
  3456. * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
  3457. * we have asm/x86/processor.h
  3458. */
  3459. struct fxsave {
  3460. u16 cwd;
  3461. u16 swd;
  3462. u16 twd;
  3463. u16 fop;
  3464. u64 rip;
  3465. u64 rdp;
  3466. u32 mxcsr;
  3467. u32 mxcsr_mask;
  3468. u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
  3469. #ifdef CONFIG_X86_64
  3470. u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
  3471. #else
  3472. u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
  3473. #endif
  3474. };
  3475. /*
  3476. * Translate a guest virtual address to a guest physical address.
  3477. */
  3478. int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  3479. struct kvm_translation *tr)
  3480. {
  3481. unsigned long vaddr = tr->linear_address;
  3482. gpa_t gpa;
  3483. vcpu_load(vcpu);
  3484. down_read(&vcpu->kvm->slots_lock);
  3485. gpa = vcpu->arch.mmu.gva_to_gpa(vcpu, vaddr);
  3486. up_read(&vcpu->kvm->slots_lock);
  3487. tr->physical_address = gpa;
  3488. tr->valid = gpa != UNMAPPED_GVA;
  3489. tr->writeable = 1;
  3490. tr->usermode = 0;
  3491. vcpu_put(vcpu);
  3492. return 0;
  3493. }
  3494. int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  3495. {
  3496. struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
  3497. vcpu_load(vcpu);
  3498. memcpy(fpu->fpr, fxsave->st_space, 128);
  3499. fpu->fcw = fxsave->cwd;
  3500. fpu->fsw = fxsave->swd;
  3501. fpu->ftwx = fxsave->twd;
  3502. fpu->last_opcode = fxsave->fop;
  3503. fpu->last_ip = fxsave->rip;
  3504. fpu->last_dp = fxsave->rdp;
  3505. memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
  3506. vcpu_put(vcpu);
  3507. return 0;
  3508. }
  3509. int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  3510. {
  3511. struct fxsave *fxsave = (struct fxsave *)&vcpu->arch.guest_fx_image;
  3512. vcpu_load(vcpu);
  3513. memcpy(fxsave->st_space, fpu->fpr, 128);
  3514. fxsave->cwd = fpu->fcw;
  3515. fxsave->swd = fpu->fsw;
  3516. fxsave->twd = fpu->ftwx;
  3517. fxsave->fop = fpu->last_opcode;
  3518. fxsave->rip = fpu->last_ip;
  3519. fxsave->rdp = fpu->last_dp;
  3520. memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
  3521. vcpu_put(vcpu);
  3522. return 0;
  3523. }
  3524. void fx_init(struct kvm_vcpu *vcpu)
  3525. {
  3526. unsigned after_mxcsr_mask;
  3527. /*
  3528. * Touch the fpu the first time in non atomic context as if
  3529. * this is the first fpu instruction the exception handler
  3530. * will fire before the instruction returns and it'll have to
  3531. * allocate ram with GFP_KERNEL.
  3532. */
  3533. if (!used_math())
  3534. kvm_fx_save(&vcpu->arch.host_fx_image);
  3535. /* Initialize guest FPU by resetting ours and saving into guest's */
  3536. preempt_disable();
  3537. kvm_fx_save(&vcpu->arch.host_fx_image);
  3538. kvm_fx_finit();
  3539. kvm_fx_save(&vcpu->arch.guest_fx_image);
  3540. kvm_fx_restore(&vcpu->arch.host_fx_image);
  3541. preempt_enable();
  3542. vcpu->arch.cr0 |= X86_CR0_ET;
  3543. after_mxcsr_mask = offsetof(struct i387_fxsave_struct, st_space);
  3544. vcpu->arch.guest_fx_image.mxcsr = 0x1f80;
  3545. memset((void *)&vcpu->arch.guest_fx_image + after_mxcsr_mask,
  3546. 0, sizeof(struct i387_fxsave_struct) - after_mxcsr_mask);
  3547. }
  3548. EXPORT_SYMBOL_GPL(fx_init);
  3549. void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
  3550. {
  3551. if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
  3552. return;
  3553. vcpu->guest_fpu_loaded = 1;
  3554. kvm_fx_save(&vcpu->arch.host_fx_image);
  3555. kvm_fx_restore(&vcpu->arch.guest_fx_image);
  3556. }
  3557. EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
  3558. void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
  3559. {
  3560. if (!vcpu->guest_fpu_loaded)
  3561. return;
  3562. vcpu->guest_fpu_loaded = 0;
  3563. kvm_fx_save(&vcpu->arch.guest_fx_image);
  3564. kvm_fx_restore(&vcpu->arch.host_fx_image);
  3565. ++vcpu->stat.fpu_reload;
  3566. }
  3567. EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
  3568. void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
  3569. {
  3570. kvm_x86_ops->vcpu_free(vcpu);
  3571. }
  3572. struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
  3573. unsigned int id)
  3574. {
  3575. return kvm_x86_ops->vcpu_create(kvm, id);
  3576. }
  3577. int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
  3578. {
  3579. int r;
  3580. /* We do fxsave: this must be aligned. */
  3581. BUG_ON((unsigned long)&vcpu->arch.host_fx_image & 0xF);
  3582. vcpu_load(vcpu);
  3583. r = kvm_arch_vcpu_reset(vcpu);
  3584. if (r == 0)
  3585. r = kvm_mmu_setup(vcpu);
  3586. vcpu_put(vcpu);
  3587. if (r < 0)
  3588. goto free_vcpu;
  3589. return 0;
  3590. free_vcpu:
  3591. kvm_x86_ops->vcpu_free(vcpu);
  3592. return r;
  3593. }
  3594. void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
  3595. {
  3596. vcpu_load(vcpu);
  3597. kvm_mmu_unload(vcpu);
  3598. vcpu_put(vcpu);
  3599. kvm_x86_ops->vcpu_free(vcpu);
  3600. }
  3601. int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
  3602. {
  3603. return kvm_x86_ops->vcpu_reset(vcpu);
  3604. }
  3605. void kvm_arch_hardware_enable(void *garbage)
  3606. {
  3607. kvm_x86_ops->hardware_enable(garbage);
  3608. }
  3609. void kvm_arch_hardware_disable(void *garbage)
  3610. {
  3611. kvm_x86_ops->hardware_disable(garbage);
  3612. }
  3613. int kvm_arch_hardware_setup(void)
  3614. {
  3615. return kvm_x86_ops->hardware_setup();
  3616. }
  3617. void kvm_arch_hardware_unsetup(void)
  3618. {
  3619. kvm_x86_ops->hardware_unsetup();
  3620. }
  3621. void kvm_arch_check_processor_compat(void *rtn)
  3622. {
  3623. kvm_x86_ops->check_processor_compatibility(rtn);
  3624. }
  3625. int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
  3626. {
  3627. struct page *page;
  3628. struct kvm *kvm;
  3629. int r;
  3630. BUG_ON(vcpu->kvm == NULL);
  3631. kvm = vcpu->kvm;
  3632. vcpu->arch.mmu.root_hpa = INVALID_PAGE;
  3633. if (!irqchip_in_kernel(kvm) || vcpu->vcpu_id == 0)
  3634. vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
  3635. else
  3636. vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
  3637. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  3638. if (!page) {
  3639. r = -ENOMEM;
  3640. goto fail;
  3641. }
  3642. vcpu->arch.pio_data = page_address(page);
  3643. r = kvm_mmu_create(vcpu);
  3644. if (r < 0)
  3645. goto fail_free_pio_data;
  3646. if (irqchip_in_kernel(kvm)) {
  3647. r = kvm_create_lapic(vcpu);
  3648. if (r < 0)
  3649. goto fail_mmu_destroy;
  3650. }
  3651. return 0;
  3652. fail_mmu_destroy:
  3653. kvm_mmu_destroy(vcpu);
  3654. fail_free_pio_data:
  3655. free_page((unsigned long)vcpu->arch.pio_data);
  3656. fail:
  3657. return r;
  3658. }
  3659. void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
  3660. {
  3661. kvm_free_lapic(vcpu);
  3662. down_read(&vcpu->kvm->slots_lock);
  3663. kvm_mmu_destroy(vcpu);
  3664. up_read(&vcpu->kvm->slots_lock);
  3665. free_page((unsigned long)vcpu->arch.pio_data);
  3666. }
  3667. struct kvm *kvm_arch_create_vm(void)
  3668. {
  3669. struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
  3670. if (!kvm)
  3671. return ERR_PTR(-ENOMEM);
  3672. INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
  3673. INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
  3674. return kvm;
  3675. }
  3676. static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
  3677. {
  3678. vcpu_load(vcpu);
  3679. kvm_mmu_unload(vcpu);
  3680. vcpu_put(vcpu);
  3681. }
  3682. static void kvm_free_vcpus(struct kvm *kvm)
  3683. {
  3684. unsigned int i;
  3685. /*
  3686. * Unpin any mmu pages first.
  3687. */
  3688. for (i = 0; i < KVM_MAX_VCPUS; ++i)
  3689. if (kvm->vcpus[i])
  3690. kvm_unload_vcpu_mmu(kvm->vcpus[i]);
  3691. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  3692. if (kvm->vcpus[i]) {
  3693. kvm_arch_vcpu_free(kvm->vcpus[i]);
  3694. kvm->vcpus[i] = NULL;
  3695. }
  3696. }
  3697. }
  3698. void kvm_arch_destroy_vm(struct kvm *kvm)
  3699. {
  3700. kvm_iommu_unmap_guest(kvm);
  3701. kvm_free_all_assigned_devices(kvm);
  3702. kvm_free_pit(kvm);
  3703. kfree(kvm->arch.vpic);
  3704. kfree(kvm->arch.vioapic);
  3705. kvm_free_vcpus(kvm);
  3706. kvm_free_physmem(kvm);
  3707. if (kvm->arch.apic_access_page)
  3708. put_page(kvm->arch.apic_access_page);
  3709. if (kvm->arch.ept_identity_pagetable)
  3710. put_page(kvm->arch.ept_identity_pagetable);
  3711. kfree(kvm);
  3712. }
  3713. int kvm_arch_set_memory_region(struct kvm *kvm,
  3714. struct kvm_userspace_memory_region *mem,
  3715. struct kvm_memory_slot old,
  3716. int user_alloc)
  3717. {
  3718. int npages = mem->memory_size >> PAGE_SHIFT;
  3719. struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
  3720. /*To keep backward compatibility with older userspace,
  3721. *x86 needs to hanlde !user_alloc case.
  3722. */
  3723. if (!user_alloc) {
  3724. if (npages && !old.rmap) {
  3725. unsigned long userspace_addr;
  3726. down_write(&current->mm->mmap_sem);
  3727. userspace_addr = do_mmap(NULL, 0,
  3728. npages * PAGE_SIZE,
  3729. PROT_READ | PROT_WRITE,
  3730. MAP_PRIVATE | MAP_ANONYMOUS,
  3731. 0);
  3732. up_write(&current->mm->mmap_sem);
  3733. if (IS_ERR((void *)userspace_addr))
  3734. return PTR_ERR((void *)userspace_addr);
  3735. /* set userspace_addr atomically for kvm_hva_to_rmapp */
  3736. spin_lock(&kvm->mmu_lock);
  3737. memslot->userspace_addr = userspace_addr;
  3738. spin_unlock(&kvm->mmu_lock);
  3739. } else {
  3740. if (!old.user_alloc && old.rmap) {
  3741. int ret;
  3742. down_write(&current->mm->mmap_sem);
  3743. ret = do_munmap(current->mm, old.userspace_addr,
  3744. old.npages * PAGE_SIZE);
  3745. up_write(&current->mm->mmap_sem);
  3746. if (ret < 0)
  3747. printk(KERN_WARNING
  3748. "kvm_vm_ioctl_set_memory_region: "
  3749. "failed to munmap memory\n");
  3750. }
  3751. }
  3752. }
  3753. if (!kvm->arch.n_requested_mmu_pages) {
  3754. unsigned int nr_mmu_pages = kvm_mmu_calculate_mmu_pages(kvm);
  3755. kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
  3756. }
  3757. kvm_mmu_slot_remove_write_access(kvm, mem->slot);
  3758. kvm_flush_remote_tlbs(kvm);
  3759. return 0;
  3760. }
  3761. void kvm_arch_flush_shadow(struct kvm *kvm)
  3762. {
  3763. kvm_mmu_zap_all(kvm);
  3764. }
  3765. int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
  3766. {
  3767. return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
  3768. || vcpu->arch.mp_state == KVM_MP_STATE_SIPI_RECEIVED;
  3769. }
  3770. static void vcpu_kick_intr(void *info)
  3771. {
  3772. #ifdef DEBUG
  3773. struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
  3774. printk(KERN_DEBUG "vcpu_kick_intr %p \n", vcpu);
  3775. #endif
  3776. }
  3777. void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
  3778. {
  3779. int ipi_pcpu = vcpu->cpu;
  3780. int cpu = get_cpu();
  3781. if (waitqueue_active(&vcpu->wq)) {
  3782. wake_up_interruptible(&vcpu->wq);
  3783. ++vcpu->stat.halt_wakeup;
  3784. }
  3785. /*
  3786. * We may be called synchronously with irqs disabled in guest mode,
  3787. * So need not to call smp_call_function_single() in that case.
  3788. */
  3789. if (vcpu->guest_mode && vcpu->cpu != cpu)
  3790. smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0);
  3791. put_cpu();
  3792. }