x86.c 111 KB

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