x86.c 106 KB

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