kvm_main.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224
  1. /*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * This module enables machines with Intel VT-x extensions to run virtual
  5. * machines without emulation or binary translation.
  6. *
  7. * Copyright (C) 2006 Qumranet, Inc.
  8. *
  9. * Authors:
  10. * Avi Kivity <avi@qumranet.com>
  11. * Yaniv Kamay <yaniv@qumranet.com>
  12. *
  13. * This work is licensed under the terms of the GNU GPL, version 2. See
  14. * the COPYING file in the top-level directory.
  15. *
  16. */
  17. #include "kvm.h"
  18. #include "x86_emulate.h"
  19. #include "segment_descriptor.h"
  20. #include <linux/kvm.h>
  21. #include <linux/module.h>
  22. #include <linux/errno.h>
  23. #include <linux/percpu.h>
  24. #include <linux/gfp.h>
  25. #include <linux/mm.h>
  26. #include <linux/miscdevice.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/reboot.h>
  29. #include <linux/debugfs.h>
  30. #include <linux/highmem.h>
  31. #include <linux/file.h>
  32. #include <linux/sysdev.h>
  33. #include <linux/cpu.h>
  34. #include <linux/sched.h>
  35. #include <linux/cpumask.h>
  36. #include <linux/smp.h>
  37. #include <linux/anon_inodes.h>
  38. #include <asm/processor.h>
  39. #include <asm/msr.h>
  40. #include <asm/io.h>
  41. #include <asm/uaccess.h>
  42. #include <asm/desc.h>
  43. MODULE_AUTHOR("Qumranet");
  44. MODULE_LICENSE("GPL");
  45. static DEFINE_SPINLOCK(kvm_lock);
  46. static LIST_HEAD(vm_list);
  47. static cpumask_t cpus_hardware_enabled;
  48. struct kvm_arch_ops *kvm_arch_ops;
  49. static void hardware_disable(void *ignored);
  50. #define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x)
  51. static struct kvm_stats_debugfs_item {
  52. const char *name;
  53. int offset;
  54. struct dentry *dentry;
  55. } debugfs_entries[] = {
  56. { "pf_fixed", STAT_OFFSET(pf_fixed) },
  57. { "pf_guest", STAT_OFFSET(pf_guest) },
  58. { "tlb_flush", STAT_OFFSET(tlb_flush) },
  59. { "invlpg", STAT_OFFSET(invlpg) },
  60. { "exits", STAT_OFFSET(exits) },
  61. { "io_exits", STAT_OFFSET(io_exits) },
  62. { "mmio_exits", STAT_OFFSET(mmio_exits) },
  63. { "signal_exits", STAT_OFFSET(signal_exits) },
  64. { "irq_window", STAT_OFFSET(irq_window_exits) },
  65. { "halt_exits", STAT_OFFSET(halt_exits) },
  66. { "request_irq", STAT_OFFSET(request_irq_exits) },
  67. { "irq_exits", STAT_OFFSET(irq_exits) },
  68. { "light_exits", STAT_OFFSET(light_exits) },
  69. { "efer_reload", STAT_OFFSET(efer_reload) },
  70. { NULL }
  71. };
  72. static struct dentry *debugfs_dir;
  73. #define MAX_IO_MSRS 256
  74. #define CR0_RESEVED_BITS 0xffffffff1ffaffc0ULL
  75. #define LMSW_GUEST_MASK 0x0eULL
  76. #define CR4_RESEVED_BITS (~((1ULL << 11) - 1))
  77. #define CR8_RESEVED_BITS (~0x0fULL)
  78. #define EFER_RESERVED_BITS 0xfffffffffffff2fe
  79. #ifdef CONFIG_X86_64
  80. // LDT or TSS descriptor in the GDT. 16 bytes.
  81. struct segment_descriptor_64 {
  82. struct segment_descriptor s;
  83. u32 base_higher;
  84. u32 pad_zero;
  85. };
  86. #endif
  87. static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
  88. unsigned long arg);
  89. unsigned long segment_base(u16 selector)
  90. {
  91. struct descriptor_table gdt;
  92. struct segment_descriptor *d;
  93. unsigned long table_base;
  94. typedef unsigned long ul;
  95. unsigned long v;
  96. if (selector == 0)
  97. return 0;
  98. asm ("sgdt %0" : "=m"(gdt));
  99. table_base = gdt.base;
  100. if (selector & 4) { /* from ldt */
  101. u16 ldt_selector;
  102. asm ("sldt %0" : "=g"(ldt_selector));
  103. table_base = segment_base(ldt_selector);
  104. }
  105. d = (struct segment_descriptor *)(table_base + (selector & ~7));
  106. v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
  107. #ifdef CONFIG_X86_64
  108. if (d->system == 0
  109. && (d->type == 2 || d->type == 9 || d->type == 11))
  110. v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
  111. #endif
  112. return v;
  113. }
  114. EXPORT_SYMBOL_GPL(segment_base);
  115. static inline int valid_vcpu(int n)
  116. {
  117. return likely(n >= 0 && n < KVM_MAX_VCPUS);
  118. }
  119. int kvm_read_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
  120. void *dest)
  121. {
  122. unsigned char *host_buf = dest;
  123. unsigned long req_size = size;
  124. while (size) {
  125. hpa_t paddr;
  126. unsigned now;
  127. unsigned offset;
  128. hva_t guest_buf;
  129. paddr = gva_to_hpa(vcpu, addr);
  130. if (is_error_hpa(paddr))
  131. break;
  132. guest_buf = (hva_t)kmap_atomic(
  133. pfn_to_page(paddr >> PAGE_SHIFT),
  134. KM_USER0);
  135. offset = addr & ~PAGE_MASK;
  136. guest_buf |= offset;
  137. now = min(size, PAGE_SIZE - offset);
  138. memcpy(host_buf, (void*)guest_buf, now);
  139. host_buf += now;
  140. addr += now;
  141. size -= now;
  142. kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
  143. }
  144. return req_size - size;
  145. }
  146. EXPORT_SYMBOL_GPL(kvm_read_guest);
  147. int kvm_write_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
  148. void *data)
  149. {
  150. unsigned char *host_buf = data;
  151. unsigned long req_size = size;
  152. while (size) {
  153. hpa_t paddr;
  154. unsigned now;
  155. unsigned offset;
  156. hva_t guest_buf;
  157. gfn_t gfn;
  158. paddr = gva_to_hpa(vcpu, addr);
  159. if (is_error_hpa(paddr))
  160. break;
  161. gfn = vcpu->mmu.gva_to_gpa(vcpu, addr) >> PAGE_SHIFT;
  162. mark_page_dirty(vcpu->kvm, gfn);
  163. guest_buf = (hva_t)kmap_atomic(
  164. pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0);
  165. offset = addr & ~PAGE_MASK;
  166. guest_buf |= offset;
  167. now = min(size, PAGE_SIZE - offset);
  168. memcpy((void*)guest_buf, host_buf, now);
  169. host_buf += now;
  170. addr += now;
  171. size -= now;
  172. kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
  173. }
  174. return req_size - size;
  175. }
  176. EXPORT_SYMBOL_GPL(kvm_write_guest);
  177. void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
  178. {
  179. if (!vcpu->fpu_active || vcpu->guest_fpu_loaded)
  180. return;
  181. vcpu->guest_fpu_loaded = 1;
  182. fx_save(vcpu->host_fx_image);
  183. fx_restore(vcpu->guest_fx_image);
  184. }
  185. EXPORT_SYMBOL_GPL(kvm_load_guest_fpu);
  186. void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
  187. {
  188. if (!vcpu->guest_fpu_loaded)
  189. return;
  190. vcpu->guest_fpu_loaded = 0;
  191. fx_save(vcpu->guest_fx_image);
  192. fx_restore(vcpu->host_fx_image);
  193. }
  194. EXPORT_SYMBOL_GPL(kvm_put_guest_fpu);
  195. /*
  196. * Switches to specified vcpu, until a matching vcpu_put()
  197. */
  198. static void vcpu_load(struct kvm_vcpu *vcpu)
  199. {
  200. mutex_lock(&vcpu->mutex);
  201. kvm_arch_ops->vcpu_load(vcpu);
  202. }
  203. static void vcpu_put(struct kvm_vcpu *vcpu)
  204. {
  205. kvm_arch_ops->vcpu_put(vcpu);
  206. mutex_unlock(&vcpu->mutex);
  207. }
  208. static void ack_flush(void *_completed)
  209. {
  210. atomic_t *completed = _completed;
  211. atomic_inc(completed);
  212. }
  213. void kvm_flush_remote_tlbs(struct kvm *kvm)
  214. {
  215. int i, cpu, needed;
  216. cpumask_t cpus;
  217. struct kvm_vcpu *vcpu;
  218. atomic_t completed;
  219. atomic_set(&completed, 0);
  220. cpus_clear(cpus);
  221. needed = 0;
  222. for (i = 0; i < kvm->nvcpus; ++i) {
  223. vcpu = &kvm->vcpus[i];
  224. if (test_and_set_bit(KVM_TLB_FLUSH, &vcpu->requests))
  225. continue;
  226. cpu = vcpu->cpu;
  227. if (cpu != -1 && cpu != raw_smp_processor_id())
  228. if (!cpu_isset(cpu, cpus)) {
  229. cpu_set(cpu, cpus);
  230. ++needed;
  231. }
  232. }
  233. /*
  234. * We really want smp_call_function_mask() here. But that's not
  235. * available, so ipi all cpus in parallel and wait for them
  236. * to complete.
  237. */
  238. for (cpu = first_cpu(cpus); cpu != NR_CPUS; cpu = next_cpu(cpu, cpus))
  239. smp_call_function_single(cpu, ack_flush, &completed, 1, 0);
  240. while (atomic_read(&completed) != needed) {
  241. cpu_relax();
  242. barrier();
  243. }
  244. }
  245. static struct kvm *kvm_create_vm(void)
  246. {
  247. struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
  248. int i;
  249. if (!kvm)
  250. return ERR_PTR(-ENOMEM);
  251. kvm_io_bus_init(&kvm->pio_bus);
  252. spin_lock_init(&kvm->lock);
  253. INIT_LIST_HEAD(&kvm->active_mmu_pages);
  254. kvm_io_bus_init(&kvm->mmio_bus);
  255. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  256. struct kvm_vcpu *vcpu = &kvm->vcpus[i];
  257. mutex_init(&vcpu->mutex);
  258. vcpu->cpu = -1;
  259. vcpu->kvm = kvm;
  260. vcpu->mmu.root_hpa = INVALID_PAGE;
  261. }
  262. spin_lock(&kvm_lock);
  263. list_add(&kvm->vm_list, &vm_list);
  264. spin_unlock(&kvm_lock);
  265. return kvm;
  266. }
  267. static int kvm_dev_open(struct inode *inode, struct file *filp)
  268. {
  269. return 0;
  270. }
  271. /*
  272. * Free any memory in @free but not in @dont.
  273. */
  274. static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
  275. struct kvm_memory_slot *dont)
  276. {
  277. int i;
  278. if (!dont || free->phys_mem != dont->phys_mem)
  279. if (free->phys_mem) {
  280. for (i = 0; i < free->npages; ++i)
  281. if (free->phys_mem[i])
  282. __free_page(free->phys_mem[i]);
  283. vfree(free->phys_mem);
  284. }
  285. if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
  286. vfree(free->dirty_bitmap);
  287. free->phys_mem = NULL;
  288. free->npages = 0;
  289. free->dirty_bitmap = NULL;
  290. }
  291. static void kvm_free_physmem(struct kvm *kvm)
  292. {
  293. int i;
  294. for (i = 0; i < kvm->nmemslots; ++i)
  295. kvm_free_physmem_slot(&kvm->memslots[i], NULL);
  296. }
  297. static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
  298. {
  299. int i;
  300. for (i = 0; i < 2; ++i)
  301. if (vcpu->pio.guest_pages[i]) {
  302. __free_page(vcpu->pio.guest_pages[i]);
  303. vcpu->pio.guest_pages[i] = NULL;
  304. }
  305. }
  306. static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
  307. {
  308. if (!vcpu->vmcs)
  309. return;
  310. vcpu_load(vcpu);
  311. kvm_mmu_unload(vcpu);
  312. vcpu_put(vcpu);
  313. }
  314. static void kvm_free_vcpu(struct kvm_vcpu *vcpu)
  315. {
  316. if (!vcpu->vmcs)
  317. return;
  318. vcpu_load(vcpu);
  319. kvm_mmu_destroy(vcpu);
  320. vcpu_put(vcpu);
  321. kvm_arch_ops->vcpu_free(vcpu);
  322. free_page((unsigned long)vcpu->run);
  323. vcpu->run = NULL;
  324. free_page((unsigned long)vcpu->pio_data);
  325. vcpu->pio_data = NULL;
  326. free_pio_guest_pages(vcpu);
  327. }
  328. static void kvm_free_vcpus(struct kvm *kvm)
  329. {
  330. unsigned int i;
  331. /*
  332. * Unpin any mmu pages first.
  333. */
  334. for (i = 0; i < KVM_MAX_VCPUS; ++i)
  335. kvm_unload_vcpu_mmu(&kvm->vcpus[i]);
  336. for (i = 0; i < KVM_MAX_VCPUS; ++i)
  337. kvm_free_vcpu(&kvm->vcpus[i]);
  338. }
  339. static int kvm_dev_release(struct inode *inode, struct file *filp)
  340. {
  341. return 0;
  342. }
  343. static void kvm_destroy_vm(struct kvm *kvm)
  344. {
  345. spin_lock(&kvm_lock);
  346. list_del(&kvm->vm_list);
  347. spin_unlock(&kvm_lock);
  348. kvm_io_bus_destroy(&kvm->pio_bus);
  349. kvm_io_bus_destroy(&kvm->mmio_bus);
  350. kvm_free_vcpus(kvm);
  351. kvm_free_physmem(kvm);
  352. kfree(kvm);
  353. }
  354. static int kvm_vm_release(struct inode *inode, struct file *filp)
  355. {
  356. struct kvm *kvm = filp->private_data;
  357. kvm_destroy_vm(kvm);
  358. return 0;
  359. }
  360. static void inject_gp(struct kvm_vcpu *vcpu)
  361. {
  362. kvm_arch_ops->inject_gp(vcpu, 0);
  363. }
  364. /*
  365. * Load the pae pdptrs. Return true is they are all valid.
  366. */
  367. static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
  368. {
  369. gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
  370. unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
  371. int i;
  372. u64 pdpte;
  373. u64 *pdpt;
  374. int ret;
  375. struct page *page;
  376. spin_lock(&vcpu->kvm->lock);
  377. page = gfn_to_page(vcpu->kvm, pdpt_gfn);
  378. /* FIXME: !page - emulate? 0xff? */
  379. pdpt = kmap_atomic(page, KM_USER0);
  380. ret = 1;
  381. for (i = 0; i < 4; ++i) {
  382. pdpte = pdpt[offset + i];
  383. if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull)) {
  384. ret = 0;
  385. goto out;
  386. }
  387. }
  388. for (i = 0; i < 4; ++i)
  389. vcpu->pdptrs[i] = pdpt[offset + i];
  390. out:
  391. kunmap_atomic(pdpt, KM_USER0);
  392. spin_unlock(&vcpu->kvm->lock);
  393. return ret;
  394. }
  395. void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  396. {
  397. if (cr0 & CR0_RESEVED_BITS) {
  398. printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
  399. cr0, vcpu->cr0);
  400. inject_gp(vcpu);
  401. return;
  402. }
  403. if ((cr0 & CR0_NW_MASK) && !(cr0 & CR0_CD_MASK)) {
  404. printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
  405. inject_gp(vcpu);
  406. return;
  407. }
  408. if ((cr0 & CR0_PG_MASK) && !(cr0 & CR0_PE_MASK)) {
  409. printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
  410. "and a clear PE flag\n");
  411. inject_gp(vcpu);
  412. return;
  413. }
  414. if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK)) {
  415. #ifdef CONFIG_X86_64
  416. if ((vcpu->shadow_efer & EFER_LME)) {
  417. int cs_db, cs_l;
  418. if (!is_pae(vcpu)) {
  419. printk(KERN_DEBUG "set_cr0: #GP, start paging "
  420. "in long mode while PAE is disabled\n");
  421. inject_gp(vcpu);
  422. return;
  423. }
  424. kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  425. if (cs_l) {
  426. printk(KERN_DEBUG "set_cr0: #GP, start paging "
  427. "in long mode while CS.L == 1\n");
  428. inject_gp(vcpu);
  429. return;
  430. }
  431. } else
  432. #endif
  433. if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
  434. printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
  435. "reserved bits\n");
  436. inject_gp(vcpu);
  437. return;
  438. }
  439. }
  440. kvm_arch_ops->set_cr0(vcpu, cr0);
  441. vcpu->cr0 = cr0;
  442. spin_lock(&vcpu->kvm->lock);
  443. kvm_mmu_reset_context(vcpu);
  444. spin_unlock(&vcpu->kvm->lock);
  445. return;
  446. }
  447. EXPORT_SYMBOL_GPL(set_cr0);
  448. void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
  449. {
  450. set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
  451. }
  452. EXPORT_SYMBOL_GPL(lmsw);
  453. void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  454. {
  455. if (cr4 & CR4_RESEVED_BITS) {
  456. printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
  457. inject_gp(vcpu);
  458. return;
  459. }
  460. if (is_long_mode(vcpu)) {
  461. if (!(cr4 & CR4_PAE_MASK)) {
  462. printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
  463. "in long mode\n");
  464. inject_gp(vcpu);
  465. return;
  466. }
  467. } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & CR4_PAE_MASK)
  468. && !load_pdptrs(vcpu, vcpu->cr3)) {
  469. printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
  470. inject_gp(vcpu);
  471. }
  472. if (cr4 & CR4_VMXE_MASK) {
  473. printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
  474. inject_gp(vcpu);
  475. return;
  476. }
  477. kvm_arch_ops->set_cr4(vcpu, cr4);
  478. spin_lock(&vcpu->kvm->lock);
  479. kvm_mmu_reset_context(vcpu);
  480. spin_unlock(&vcpu->kvm->lock);
  481. }
  482. EXPORT_SYMBOL_GPL(set_cr4);
  483. void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  484. {
  485. if (is_long_mode(vcpu)) {
  486. if (cr3 & CR3_L_MODE_RESEVED_BITS) {
  487. printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
  488. inject_gp(vcpu);
  489. return;
  490. }
  491. } else {
  492. if (cr3 & CR3_RESEVED_BITS) {
  493. printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
  494. inject_gp(vcpu);
  495. return;
  496. }
  497. if (is_paging(vcpu) && is_pae(vcpu) &&
  498. !load_pdptrs(vcpu, cr3)) {
  499. printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
  500. "reserved bits\n");
  501. inject_gp(vcpu);
  502. return;
  503. }
  504. }
  505. vcpu->cr3 = cr3;
  506. spin_lock(&vcpu->kvm->lock);
  507. /*
  508. * Does the new cr3 value map to physical memory? (Note, we
  509. * catch an invalid cr3 even in real-mode, because it would
  510. * cause trouble later on when we turn on paging anyway.)
  511. *
  512. * A real CPU would silently accept an invalid cr3 and would
  513. * attempt to use it - with largely undefined (and often hard
  514. * to debug) behavior on the guest side.
  515. */
  516. if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
  517. inject_gp(vcpu);
  518. else
  519. vcpu->mmu.new_cr3(vcpu);
  520. spin_unlock(&vcpu->kvm->lock);
  521. }
  522. EXPORT_SYMBOL_GPL(set_cr3);
  523. void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
  524. {
  525. if ( cr8 & CR8_RESEVED_BITS) {
  526. printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
  527. inject_gp(vcpu);
  528. return;
  529. }
  530. vcpu->cr8 = cr8;
  531. }
  532. EXPORT_SYMBOL_GPL(set_cr8);
  533. void fx_init(struct kvm_vcpu *vcpu)
  534. {
  535. struct __attribute__ ((__packed__)) fx_image_s {
  536. u16 control; //fcw
  537. u16 status; //fsw
  538. u16 tag; // ftw
  539. u16 opcode; //fop
  540. u64 ip; // fpu ip
  541. u64 operand;// fpu dp
  542. u32 mxcsr;
  543. u32 mxcsr_mask;
  544. } *fx_image;
  545. fx_save(vcpu->host_fx_image);
  546. fpu_init();
  547. fx_save(vcpu->guest_fx_image);
  548. fx_restore(vcpu->host_fx_image);
  549. fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
  550. fx_image->mxcsr = 0x1f80;
  551. memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
  552. 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
  553. }
  554. EXPORT_SYMBOL_GPL(fx_init);
  555. /*
  556. * Allocate some memory and give it an address in the guest physical address
  557. * space.
  558. *
  559. * Discontiguous memory is allowed, mostly for framebuffers.
  560. */
  561. static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
  562. struct kvm_memory_region *mem)
  563. {
  564. int r;
  565. gfn_t base_gfn;
  566. unsigned long npages;
  567. unsigned long i;
  568. struct kvm_memory_slot *memslot;
  569. struct kvm_memory_slot old, new;
  570. int memory_config_version;
  571. r = -EINVAL;
  572. /* General sanity checks */
  573. if (mem->memory_size & (PAGE_SIZE - 1))
  574. goto out;
  575. if (mem->guest_phys_addr & (PAGE_SIZE - 1))
  576. goto out;
  577. if (mem->slot >= KVM_MEMORY_SLOTS)
  578. goto out;
  579. if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
  580. goto out;
  581. memslot = &kvm->memslots[mem->slot];
  582. base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
  583. npages = mem->memory_size >> PAGE_SHIFT;
  584. if (!npages)
  585. mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
  586. raced:
  587. spin_lock(&kvm->lock);
  588. memory_config_version = kvm->memory_config_version;
  589. new = old = *memslot;
  590. new.base_gfn = base_gfn;
  591. new.npages = npages;
  592. new.flags = mem->flags;
  593. /* Disallow changing a memory slot's size. */
  594. r = -EINVAL;
  595. if (npages && old.npages && npages != old.npages)
  596. goto out_unlock;
  597. /* Check for overlaps */
  598. r = -EEXIST;
  599. for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
  600. struct kvm_memory_slot *s = &kvm->memslots[i];
  601. if (s == memslot)
  602. continue;
  603. if (!((base_gfn + npages <= s->base_gfn) ||
  604. (base_gfn >= s->base_gfn + s->npages)))
  605. goto out_unlock;
  606. }
  607. /*
  608. * Do memory allocations outside lock. memory_config_version will
  609. * detect any races.
  610. */
  611. spin_unlock(&kvm->lock);
  612. /* Deallocate if slot is being removed */
  613. if (!npages)
  614. new.phys_mem = NULL;
  615. /* Free page dirty bitmap if unneeded */
  616. if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
  617. new.dirty_bitmap = NULL;
  618. r = -ENOMEM;
  619. /* Allocate if a slot is being created */
  620. if (npages && !new.phys_mem) {
  621. new.phys_mem = vmalloc(npages * sizeof(struct page *));
  622. if (!new.phys_mem)
  623. goto out_free;
  624. memset(new.phys_mem, 0, npages * sizeof(struct page *));
  625. for (i = 0; i < npages; ++i) {
  626. new.phys_mem[i] = alloc_page(GFP_HIGHUSER
  627. | __GFP_ZERO);
  628. if (!new.phys_mem[i])
  629. goto out_free;
  630. set_page_private(new.phys_mem[i],0);
  631. }
  632. }
  633. /* Allocate page dirty bitmap if needed */
  634. if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
  635. unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
  636. new.dirty_bitmap = vmalloc(dirty_bytes);
  637. if (!new.dirty_bitmap)
  638. goto out_free;
  639. memset(new.dirty_bitmap, 0, dirty_bytes);
  640. }
  641. spin_lock(&kvm->lock);
  642. if (memory_config_version != kvm->memory_config_version) {
  643. spin_unlock(&kvm->lock);
  644. kvm_free_physmem_slot(&new, &old);
  645. goto raced;
  646. }
  647. r = -EAGAIN;
  648. if (kvm->busy)
  649. goto out_unlock;
  650. if (mem->slot >= kvm->nmemslots)
  651. kvm->nmemslots = mem->slot + 1;
  652. *memslot = new;
  653. ++kvm->memory_config_version;
  654. kvm_mmu_slot_remove_write_access(kvm, mem->slot);
  655. kvm_flush_remote_tlbs(kvm);
  656. spin_unlock(&kvm->lock);
  657. kvm_free_physmem_slot(&old, &new);
  658. return 0;
  659. out_unlock:
  660. spin_unlock(&kvm->lock);
  661. out_free:
  662. kvm_free_physmem_slot(&new, &old);
  663. out:
  664. return r;
  665. }
  666. /*
  667. * Get (and clear) the dirty memory log for a memory slot.
  668. */
  669. static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
  670. struct kvm_dirty_log *log)
  671. {
  672. struct kvm_memory_slot *memslot;
  673. int r, i;
  674. int n;
  675. unsigned long any = 0;
  676. spin_lock(&kvm->lock);
  677. /*
  678. * Prevent changes to guest memory configuration even while the lock
  679. * is not taken.
  680. */
  681. ++kvm->busy;
  682. spin_unlock(&kvm->lock);
  683. r = -EINVAL;
  684. if (log->slot >= KVM_MEMORY_SLOTS)
  685. goto out;
  686. memslot = &kvm->memslots[log->slot];
  687. r = -ENOENT;
  688. if (!memslot->dirty_bitmap)
  689. goto out;
  690. n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
  691. for (i = 0; !any && i < n/sizeof(long); ++i)
  692. any = memslot->dirty_bitmap[i];
  693. r = -EFAULT;
  694. if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
  695. goto out;
  696. spin_lock(&kvm->lock);
  697. kvm_mmu_slot_remove_write_access(kvm, log->slot);
  698. kvm_flush_remote_tlbs(kvm);
  699. memset(memslot->dirty_bitmap, 0, n);
  700. spin_unlock(&kvm->lock);
  701. r = 0;
  702. out:
  703. spin_lock(&kvm->lock);
  704. --kvm->busy;
  705. spin_unlock(&kvm->lock);
  706. return r;
  707. }
  708. /*
  709. * Set a new alias region. Aliases map a portion of physical memory into
  710. * another portion. This is useful for memory windows, for example the PC
  711. * VGA region.
  712. */
  713. static int kvm_vm_ioctl_set_memory_alias(struct kvm *kvm,
  714. struct kvm_memory_alias *alias)
  715. {
  716. int r, n;
  717. struct kvm_mem_alias *p;
  718. r = -EINVAL;
  719. /* General sanity checks */
  720. if (alias->memory_size & (PAGE_SIZE - 1))
  721. goto out;
  722. if (alias->guest_phys_addr & (PAGE_SIZE - 1))
  723. goto out;
  724. if (alias->slot >= KVM_ALIAS_SLOTS)
  725. goto out;
  726. if (alias->guest_phys_addr + alias->memory_size
  727. < alias->guest_phys_addr)
  728. goto out;
  729. if (alias->target_phys_addr + alias->memory_size
  730. < alias->target_phys_addr)
  731. goto out;
  732. spin_lock(&kvm->lock);
  733. p = &kvm->aliases[alias->slot];
  734. p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
  735. p->npages = alias->memory_size >> PAGE_SHIFT;
  736. p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
  737. for (n = KVM_ALIAS_SLOTS; n > 0; --n)
  738. if (kvm->aliases[n - 1].npages)
  739. break;
  740. kvm->naliases = n;
  741. kvm_mmu_zap_all(kvm);
  742. spin_unlock(&kvm->lock);
  743. return 0;
  744. out:
  745. return r;
  746. }
  747. static gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
  748. {
  749. int i;
  750. struct kvm_mem_alias *alias;
  751. for (i = 0; i < kvm->naliases; ++i) {
  752. alias = &kvm->aliases[i];
  753. if (gfn >= alias->base_gfn
  754. && gfn < alias->base_gfn + alias->npages)
  755. return alias->target_gfn + gfn - alias->base_gfn;
  756. }
  757. return gfn;
  758. }
  759. static struct kvm_memory_slot *__gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
  760. {
  761. int i;
  762. for (i = 0; i < kvm->nmemslots; ++i) {
  763. struct kvm_memory_slot *memslot = &kvm->memslots[i];
  764. if (gfn >= memslot->base_gfn
  765. && gfn < memslot->base_gfn + memslot->npages)
  766. return memslot;
  767. }
  768. return NULL;
  769. }
  770. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
  771. {
  772. gfn = unalias_gfn(kvm, gfn);
  773. return __gfn_to_memslot(kvm, gfn);
  774. }
  775. struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
  776. {
  777. struct kvm_memory_slot *slot;
  778. gfn = unalias_gfn(kvm, gfn);
  779. slot = __gfn_to_memslot(kvm, gfn);
  780. if (!slot)
  781. return NULL;
  782. return slot->phys_mem[gfn - slot->base_gfn];
  783. }
  784. EXPORT_SYMBOL_GPL(gfn_to_page);
  785. void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
  786. {
  787. int i;
  788. struct kvm_memory_slot *memslot;
  789. unsigned long rel_gfn;
  790. for (i = 0; i < kvm->nmemslots; ++i) {
  791. memslot = &kvm->memslots[i];
  792. if (gfn >= memslot->base_gfn
  793. && gfn < memslot->base_gfn + memslot->npages) {
  794. if (!memslot->dirty_bitmap)
  795. return;
  796. rel_gfn = gfn - memslot->base_gfn;
  797. /* avoid RMW */
  798. if (!test_bit(rel_gfn, memslot->dirty_bitmap))
  799. set_bit(rel_gfn, memslot->dirty_bitmap);
  800. return;
  801. }
  802. }
  803. }
  804. static int emulator_read_std(unsigned long addr,
  805. void *val,
  806. unsigned int bytes,
  807. struct x86_emulate_ctxt *ctxt)
  808. {
  809. struct kvm_vcpu *vcpu = ctxt->vcpu;
  810. void *data = val;
  811. while (bytes) {
  812. gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
  813. unsigned offset = addr & (PAGE_SIZE-1);
  814. unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
  815. unsigned long pfn;
  816. struct page *page;
  817. void *page_virt;
  818. if (gpa == UNMAPPED_GVA)
  819. return X86EMUL_PROPAGATE_FAULT;
  820. pfn = gpa >> PAGE_SHIFT;
  821. page = gfn_to_page(vcpu->kvm, pfn);
  822. if (!page)
  823. return X86EMUL_UNHANDLEABLE;
  824. page_virt = kmap_atomic(page, KM_USER0);
  825. memcpy(data, page_virt + offset, tocopy);
  826. kunmap_atomic(page_virt, KM_USER0);
  827. bytes -= tocopy;
  828. data += tocopy;
  829. addr += tocopy;
  830. }
  831. return X86EMUL_CONTINUE;
  832. }
  833. static int emulator_write_std(unsigned long addr,
  834. const void *val,
  835. unsigned int bytes,
  836. struct x86_emulate_ctxt *ctxt)
  837. {
  838. printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
  839. addr, bytes);
  840. return X86EMUL_UNHANDLEABLE;
  841. }
  842. static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
  843. gpa_t addr)
  844. {
  845. /*
  846. * Note that its important to have this wrapper function because
  847. * in the very near future we will be checking for MMIOs against
  848. * the LAPIC as well as the general MMIO bus
  849. */
  850. return kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
  851. }
  852. static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
  853. gpa_t addr)
  854. {
  855. return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
  856. }
  857. static int emulator_read_emulated(unsigned long addr,
  858. void *val,
  859. unsigned int bytes,
  860. struct x86_emulate_ctxt *ctxt)
  861. {
  862. struct kvm_vcpu *vcpu = ctxt->vcpu;
  863. struct kvm_io_device *mmio_dev;
  864. gpa_t gpa;
  865. if (vcpu->mmio_read_completed) {
  866. memcpy(val, vcpu->mmio_data, bytes);
  867. vcpu->mmio_read_completed = 0;
  868. return X86EMUL_CONTINUE;
  869. } else if (emulator_read_std(addr, val, bytes, ctxt)
  870. == X86EMUL_CONTINUE)
  871. return X86EMUL_CONTINUE;
  872. gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
  873. if (gpa == UNMAPPED_GVA)
  874. return X86EMUL_PROPAGATE_FAULT;
  875. /*
  876. * Is this MMIO handled locally?
  877. */
  878. mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
  879. if (mmio_dev) {
  880. kvm_iodevice_read(mmio_dev, gpa, bytes, val);
  881. return X86EMUL_CONTINUE;
  882. }
  883. vcpu->mmio_needed = 1;
  884. vcpu->mmio_phys_addr = gpa;
  885. vcpu->mmio_size = bytes;
  886. vcpu->mmio_is_write = 0;
  887. return X86EMUL_UNHANDLEABLE;
  888. }
  889. static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
  890. const void *val, int bytes)
  891. {
  892. struct page *page;
  893. void *virt;
  894. unsigned offset = offset_in_page(gpa);
  895. if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
  896. return 0;
  897. page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
  898. if (!page)
  899. return 0;
  900. mark_page_dirty(vcpu->kvm, gpa >> PAGE_SHIFT);
  901. virt = kmap_atomic(page, KM_USER0);
  902. kvm_mmu_pte_write(vcpu, gpa, virt + offset, val, bytes);
  903. memcpy(virt + offset_in_page(gpa), val, bytes);
  904. kunmap_atomic(virt, KM_USER0);
  905. return 1;
  906. }
  907. static int emulator_write_emulated_onepage(unsigned long addr,
  908. const void *val,
  909. unsigned int bytes,
  910. struct x86_emulate_ctxt *ctxt)
  911. {
  912. struct kvm_vcpu *vcpu = ctxt->vcpu;
  913. struct kvm_io_device *mmio_dev;
  914. gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
  915. if (gpa == UNMAPPED_GVA) {
  916. kvm_arch_ops->inject_page_fault(vcpu, addr, 2);
  917. return X86EMUL_PROPAGATE_FAULT;
  918. }
  919. if (emulator_write_phys(vcpu, gpa, val, bytes))
  920. return X86EMUL_CONTINUE;
  921. /*
  922. * Is this MMIO handled locally?
  923. */
  924. mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
  925. if (mmio_dev) {
  926. kvm_iodevice_write(mmio_dev, gpa, bytes, val);
  927. return X86EMUL_CONTINUE;
  928. }
  929. vcpu->mmio_needed = 1;
  930. vcpu->mmio_phys_addr = gpa;
  931. vcpu->mmio_size = bytes;
  932. vcpu->mmio_is_write = 1;
  933. memcpy(vcpu->mmio_data, val, bytes);
  934. return X86EMUL_CONTINUE;
  935. }
  936. static int emulator_write_emulated(unsigned long addr,
  937. const void *val,
  938. unsigned int bytes,
  939. struct x86_emulate_ctxt *ctxt)
  940. {
  941. /* Crossing a page boundary? */
  942. if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
  943. int rc, now;
  944. now = -addr & ~PAGE_MASK;
  945. rc = emulator_write_emulated_onepage(addr, val, now, ctxt);
  946. if (rc != X86EMUL_CONTINUE)
  947. return rc;
  948. addr += now;
  949. val += now;
  950. bytes -= now;
  951. }
  952. return emulator_write_emulated_onepage(addr, val, bytes, ctxt);
  953. }
  954. static int emulator_cmpxchg_emulated(unsigned long addr,
  955. const void *old,
  956. const void *new,
  957. unsigned int bytes,
  958. struct x86_emulate_ctxt *ctxt)
  959. {
  960. static int reported;
  961. if (!reported) {
  962. reported = 1;
  963. printk(KERN_WARNING "kvm: emulating exchange as write\n");
  964. }
  965. return emulator_write_emulated(addr, new, bytes, ctxt);
  966. }
  967. static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
  968. {
  969. return kvm_arch_ops->get_segment_base(vcpu, seg);
  970. }
  971. int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
  972. {
  973. return X86EMUL_CONTINUE;
  974. }
  975. int emulate_clts(struct kvm_vcpu *vcpu)
  976. {
  977. unsigned long cr0;
  978. cr0 = vcpu->cr0 & ~CR0_TS_MASK;
  979. kvm_arch_ops->set_cr0(vcpu, cr0);
  980. return X86EMUL_CONTINUE;
  981. }
  982. int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
  983. {
  984. struct kvm_vcpu *vcpu = ctxt->vcpu;
  985. switch (dr) {
  986. case 0 ... 3:
  987. *dest = kvm_arch_ops->get_dr(vcpu, dr);
  988. return X86EMUL_CONTINUE;
  989. default:
  990. printk(KERN_DEBUG "%s: unexpected dr %u\n",
  991. __FUNCTION__, dr);
  992. return X86EMUL_UNHANDLEABLE;
  993. }
  994. }
  995. int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
  996. {
  997. unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
  998. int exception;
  999. kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
  1000. if (exception) {
  1001. /* FIXME: better handling */
  1002. return X86EMUL_UNHANDLEABLE;
  1003. }
  1004. return X86EMUL_CONTINUE;
  1005. }
  1006. static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
  1007. {
  1008. static int reported;
  1009. u8 opcodes[4];
  1010. unsigned long rip = ctxt->vcpu->rip;
  1011. unsigned long rip_linear;
  1012. rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
  1013. if (reported)
  1014. return;
  1015. emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
  1016. printk(KERN_ERR "emulation failed but !mmio_needed?"
  1017. " rip %lx %02x %02x %02x %02x\n",
  1018. rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
  1019. reported = 1;
  1020. }
  1021. struct x86_emulate_ops emulate_ops = {
  1022. .read_std = emulator_read_std,
  1023. .write_std = emulator_write_std,
  1024. .read_emulated = emulator_read_emulated,
  1025. .write_emulated = emulator_write_emulated,
  1026. .cmpxchg_emulated = emulator_cmpxchg_emulated,
  1027. };
  1028. int emulate_instruction(struct kvm_vcpu *vcpu,
  1029. struct kvm_run *run,
  1030. unsigned long cr2,
  1031. u16 error_code)
  1032. {
  1033. struct x86_emulate_ctxt emulate_ctxt;
  1034. int r;
  1035. int cs_db, cs_l;
  1036. vcpu->mmio_fault_cr2 = cr2;
  1037. kvm_arch_ops->cache_regs(vcpu);
  1038. kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  1039. emulate_ctxt.vcpu = vcpu;
  1040. emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
  1041. emulate_ctxt.cr2 = cr2;
  1042. emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
  1043. ? X86EMUL_MODE_REAL : cs_l
  1044. ? X86EMUL_MODE_PROT64 : cs_db
  1045. ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
  1046. if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
  1047. emulate_ctxt.cs_base = 0;
  1048. emulate_ctxt.ds_base = 0;
  1049. emulate_ctxt.es_base = 0;
  1050. emulate_ctxt.ss_base = 0;
  1051. } else {
  1052. emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
  1053. emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
  1054. emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
  1055. emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
  1056. }
  1057. emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
  1058. emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
  1059. vcpu->mmio_is_write = 0;
  1060. r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
  1061. if ((r || vcpu->mmio_is_write) && run) {
  1062. run->mmio.phys_addr = vcpu->mmio_phys_addr;
  1063. memcpy(run->mmio.data, vcpu->mmio_data, 8);
  1064. run->mmio.len = vcpu->mmio_size;
  1065. run->mmio.is_write = vcpu->mmio_is_write;
  1066. }
  1067. if (r) {
  1068. if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
  1069. return EMULATE_DONE;
  1070. if (!vcpu->mmio_needed) {
  1071. report_emulation_failure(&emulate_ctxt);
  1072. return EMULATE_FAIL;
  1073. }
  1074. return EMULATE_DO_MMIO;
  1075. }
  1076. kvm_arch_ops->decache_regs(vcpu);
  1077. kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
  1078. if (vcpu->mmio_is_write) {
  1079. vcpu->mmio_needed = 0;
  1080. return EMULATE_DO_MMIO;
  1081. }
  1082. return EMULATE_DONE;
  1083. }
  1084. EXPORT_SYMBOL_GPL(emulate_instruction);
  1085. int kvm_emulate_halt(struct kvm_vcpu *vcpu)
  1086. {
  1087. if (vcpu->irq_summary)
  1088. return 1;
  1089. vcpu->run->exit_reason = KVM_EXIT_HLT;
  1090. ++vcpu->stat.halt_exits;
  1091. return 0;
  1092. }
  1093. EXPORT_SYMBOL_GPL(kvm_emulate_halt);
  1094. int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
  1095. {
  1096. unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
  1097. kvm_arch_ops->cache_regs(vcpu);
  1098. ret = -KVM_EINVAL;
  1099. #ifdef CONFIG_X86_64
  1100. if (is_long_mode(vcpu)) {
  1101. nr = vcpu->regs[VCPU_REGS_RAX];
  1102. a0 = vcpu->regs[VCPU_REGS_RDI];
  1103. a1 = vcpu->regs[VCPU_REGS_RSI];
  1104. a2 = vcpu->regs[VCPU_REGS_RDX];
  1105. a3 = vcpu->regs[VCPU_REGS_RCX];
  1106. a4 = vcpu->regs[VCPU_REGS_R8];
  1107. a5 = vcpu->regs[VCPU_REGS_R9];
  1108. } else
  1109. #endif
  1110. {
  1111. nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
  1112. a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
  1113. a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
  1114. a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
  1115. a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
  1116. a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
  1117. a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
  1118. }
  1119. switch (nr) {
  1120. default:
  1121. run->hypercall.args[0] = a0;
  1122. run->hypercall.args[1] = a1;
  1123. run->hypercall.args[2] = a2;
  1124. run->hypercall.args[3] = a3;
  1125. run->hypercall.args[4] = a4;
  1126. run->hypercall.args[5] = a5;
  1127. run->hypercall.ret = ret;
  1128. run->hypercall.longmode = is_long_mode(vcpu);
  1129. kvm_arch_ops->decache_regs(vcpu);
  1130. return 0;
  1131. }
  1132. vcpu->regs[VCPU_REGS_RAX] = ret;
  1133. kvm_arch_ops->decache_regs(vcpu);
  1134. return 1;
  1135. }
  1136. EXPORT_SYMBOL_GPL(kvm_hypercall);
  1137. static u64 mk_cr_64(u64 curr_cr, u32 new_val)
  1138. {
  1139. return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
  1140. }
  1141. void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  1142. {
  1143. struct descriptor_table dt = { limit, base };
  1144. kvm_arch_ops->set_gdt(vcpu, &dt);
  1145. }
  1146. void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  1147. {
  1148. struct descriptor_table dt = { limit, base };
  1149. kvm_arch_ops->set_idt(vcpu, &dt);
  1150. }
  1151. void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
  1152. unsigned long *rflags)
  1153. {
  1154. lmsw(vcpu, msw);
  1155. *rflags = kvm_arch_ops->get_rflags(vcpu);
  1156. }
  1157. unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
  1158. {
  1159. kvm_arch_ops->decache_cr4_guest_bits(vcpu);
  1160. switch (cr) {
  1161. case 0:
  1162. return vcpu->cr0;
  1163. case 2:
  1164. return vcpu->cr2;
  1165. case 3:
  1166. return vcpu->cr3;
  1167. case 4:
  1168. return vcpu->cr4;
  1169. default:
  1170. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
  1171. return 0;
  1172. }
  1173. }
  1174. void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
  1175. unsigned long *rflags)
  1176. {
  1177. switch (cr) {
  1178. case 0:
  1179. set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
  1180. *rflags = kvm_arch_ops->get_rflags(vcpu);
  1181. break;
  1182. case 2:
  1183. vcpu->cr2 = val;
  1184. break;
  1185. case 3:
  1186. set_cr3(vcpu, val);
  1187. break;
  1188. case 4:
  1189. set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
  1190. break;
  1191. default:
  1192. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
  1193. }
  1194. }
  1195. /*
  1196. * Register the para guest with the host:
  1197. */
  1198. static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
  1199. {
  1200. struct kvm_vcpu_para_state *para_state;
  1201. hpa_t para_state_hpa, hypercall_hpa;
  1202. struct page *para_state_page;
  1203. unsigned char *hypercall;
  1204. gpa_t hypercall_gpa;
  1205. printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
  1206. printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
  1207. /*
  1208. * Needs to be page aligned:
  1209. */
  1210. if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
  1211. goto err_gp;
  1212. para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
  1213. printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
  1214. if (is_error_hpa(para_state_hpa))
  1215. goto err_gp;
  1216. mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
  1217. para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
  1218. para_state = kmap_atomic(para_state_page, KM_USER0);
  1219. printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
  1220. printk(KERN_DEBUG ".... size: %d\n", para_state->size);
  1221. para_state->host_version = KVM_PARA_API_VERSION;
  1222. /*
  1223. * We cannot support guests that try to register themselves
  1224. * with a newer API version than the host supports:
  1225. */
  1226. if (para_state->guest_version > KVM_PARA_API_VERSION) {
  1227. para_state->ret = -KVM_EINVAL;
  1228. goto err_kunmap_skip;
  1229. }
  1230. hypercall_gpa = para_state->hypercall_gpa;
  1231. hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
  1232. printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
  1233. if (is_error_hpa(hypercall_hpa)) {
  1234. para_state->ret = -KVM_EINVAL;
  1235. goto err_kunmap_skip;
  1236. }
  1237. printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
  1238. vcpu->para_state_page = para_state_page;
  1239. vcpu->para_state_gpa = para_state_gpa;
  1240. vcpu->hypercall_gpa = hypercall_gpa;
  1241. mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
  1242. hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
  1243. KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
  1244. kvm_arch_ops->patch_hypercall(vcpu, hypercall);
  1245. kunmap_atomic(hypercall, KM_USER1);
  1246. para_state->ret = 0;
  1247. err_kunmap_skip:
  1248. kunmap_atomic(para_state, KM_USER0);
  1249. return 0;
  1250. err_gp:
  1251. return 1;
  1252. }
  1253. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1254. {
  1255. u64 data;
  1256. switch (msr) {
  1257. case 0xc0010010: /* SYSCFG */
  1258. case 0xc0010015: /* HWCR */
  1259. case MSR_IA32_PLATFORM_ID:
  1260. case MSR_IA32_P5_MC_ADDR:
  1261. case MSR_IA32_P5_MC_TYPE:
  1262. case MSR_IA32_MC0_CTL:
  1263. case MSR_IA32_MCG_STATUS:
  1264. case MSR_IA32_MCG_CAP:
  1265. case MSR_IA32_MC0_MISC:
  1266. case MSR_IA32_MC0_MISC+4:
  1267. case MSR_IA32_MC0_MISC+8:
  1268. case MSR_IA32_MC0_MISC+12:
  1269. case MSR_IA32_MC0_MISC+16:
  1270. case MSR_IA32_UCODE_REV:
  1271. case MSR_IA32_PERF_STATUS:
  1272. case MSR_IA32_EBL_CR_POWERON:
  1273. /* MTRR registers */
  1274. case 0xfe:
  1275. case 0x200 ... 0x2ff:
  1276. data = 0;
  1277. break;
  1278. case 0xcd: /* fsb frequency */
  1279. data = 3;
  1280. break;
  1281. case MSR_IA32_APICBASE:
  1282. data = vcpu->apic_base;
  1283. break;
  1284. case MSR_IA32_MISC_ENABLE:
  1285. data = vcpu->ia32_misc_enable_msr;
  1286. break;
  1287. #ifdef CONFIG_X86_64
  1288. case MSR_EFER:
  1289. data = vcpu->shadow_efer;
  1290. break;
  1291. #endif
  1292. default:
  1293. printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
  1294. return 1;
  1295. }
  1296. *pdata = data;
  1297. return 0;
  1298. }
  1299. EXPORT_SYMBOL_GPL(kvm_get_msr_common);
  1300. /*
  1301. * Reads an msr value (of 'msr_index') into 'pdata'.
  1302. * Returns 0 on success, non-0 otherwise.
  1303. * Assumes vcpu_load() was already called.
  1304. */
  1305. int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  1306. {
  1307. return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
  1308. }
  1309. #ifdef CONFIG_X86_64
  1310. static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
  1311. {
  1312. if (efer & EFER_RESERVED_BITS) {
  1313. printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
  1314. efer);
  1315. inject_gp(vcpu);
  1316. return;
  1317. }
  1318. if (is_paging(vcpu)
  1319. && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
  1320. printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
  1321. inject_gp(vcpu);
  1322. return;
  1323. }
  1324. kvm_arch_ops->set_efer(vcpu, efer);
  1325. efer &= ~EFER_LMA;
  1326. efer |= vcpu->shadow_efer & EFER_LMA;
  1327. vcpu->shadow_efer = efer;
  1328. }
  1329. #endif
  1330. int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  1331. {
  1332. switch (msr) {
  1333. #ifdef CONFIG_X86_64
  1334. case MSR_EFER:
  1335. set_efer(vcpu, data);
  1336. break;
  1337. #endif
  1338. case MSR_IA32_MC0_STATUS:
  1339. printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
  1340. __FUNCTION__, data);
  1341. break;
  1342. case MSR_IA32_MCG_STATUS:
  1343. printk(KERN_WARNING "%s: MSR_IA32_MCG_STATUS 0x%llx, nop\n",
  1344. __FUNCTION__, data);
  1345. break;
  1346. case MSR_IA32_UCODE_REV:
  1347. case MSR_IA32_UCODE_WRITE:
  1348. case 0x200 ... 0x2ff: /* MTRRs */
  1349. break;
  1350. case MSR_IA32_APICBASE:
  1351. vcpu->apic_base = data;
  1352. break;
  1353. case MSR_IA32_MISC_ENABLE:
  1354. vcpu->ia32_misc_enable_msr = data;
  1355. break;
  1356. /*
  1357. * This is the 'probe whether the host is KVM' logic:
  1358. */
  1359. case MSR_KVM_API_MAGIC:
  1360. return vcpu_register_para(vcpu, data);
  1361. default:
  1362. printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
  1363. return 1;
  1364. }
  1365. return 0;
  1366. }
  1367. EXPORT_SYMBOL_GPL(kvm_set_msr_common);
  1368. /*
  1369. * Writes msr value into into the appropriate "register".
  1370. * Returns 0 on success, non-0 otherwise.
  1371. * Assumes vcpu_load() was already called.
  1372. */
  1373. int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  1374. {
  1375. return kvm_arch_ops->set_msr(vcpu, msr_index, data);
  1376. }
  1377. void kvm_resched(struct kvm_vcpu *vcpu)
  1378. {
  1379. if (!need_resched())
  1380. return;
  1381. vcpu_put(vcpu);
  1382. cond_resched();
  1383. vcpu_load(vcpu);
  1384. }
  1385. EXPORT_SYMBOL_GPL(kvm_resched);
  1386. void load_msrs(struct vmx_msr_entry *e, int n)
  1387. {
  1388. int i;
  1389. for (i = 0; i < n; ++i)
  1390. wrmsrl(e[i].index, e[i].data);
  1391. }
  1392. EXPORT_SYMBOL_GPL(load_msrs);
  1393. void save_msrs(struct vmx_msr_entry *e, int n)
  1394. {
  1395. int i;
  1396. for (i = 0; i < n; ++i)
  1397. rdmsrl(e[i].index, e[i].data);
  1398. }
  1399. EXPORT_SYMBOL_GPL(save_msrs);
  1400. void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
  1401. {
  1402. int i;
  1403. u32 function;
  1404. struct kvm_cpuid_entry *e, *best;
  1405. kvm_arch_ops->cache_regs(vcpu);
  1406. function = vcpu->regs[VCPU_REGS_RAX];
  1407. vcpu->regs[VCPU_REGS_RAX] = 0;
  1408. vcpu->regs[VCPU_REGS_RBX] = 0;
  1409. vcpu->regs[VCPU_REGS_RCX] = 0;
  1410. vcpu->regs[VCPU_REGS_RDX] = 0;
  1411. best = NULL;
  1412. for (i = 0; i < vcpu->cpuid_nent; ++i) {
  1413. e = &vcpu->cpuid_entries[i];
  1414. if (e->function == function) {
  1415. best = e;
  1416. break;
  1417. }
  1418. /*
  1419. * Both basic or both extended?
  1420. */
  1421. if (((e->function ^ function) & 0x80000000) == 0)
  1422. if (!best || e->function > best->function)
  1423. best = e;
  1424. }
  1425. if (best) {
  1426. vcpu->regs[VCPU_REGS_RAX] = best->eax;
  1427. vcpu->regs[VCPU_REGS_RBX] = best->ebx;
  1428. vcpu->regs[VCPU_REGS_RCX] = best->ecx;
  1429. vcpu->regs[VCPU_REGS_RDX] = best->edx;
  1430. }
  1431. kvm_arch_ops->decache_regs(vcpu);
  1432. kvm_arch_ops->skip_emulated_instruction(vcpu);
  1433. }
  1434. EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
  1435. static int pio_copy_data(struct kvm_vcpu *vcpu)
  1436. {
  1437. void *p = vcpu->pio_data;
  1438. void *q;
  1439. unsigned bytes;
  1440. int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
  1441. kvm_arch_ops->vcpu_put(vcpu);
  1442. q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
  1443. PAGE_KERNEL);
  1444. if (!q) {
  1445. kvm_arch_ops->vcpu_load(vcpu);
  1446. free_pio_guest_pages(vcpu);
  1447. return -ENOMEM;
  1448. }
  1449. q += vcpu->pio.guest_page_offset;
  1450. bytes = vcpu->pio.size * vcpu->pio.cur_count;
  1451. if (vcpu->pio.in)
  1452. memcpy(q, p, bytes);
  1453. else
  1454. memcpy(p, q, bytes);
  1455. q -= vcpu->pio.guest_page_offset;
  1456. vunmap(q);
  1457. kvm_arch_ops->vcpu_load(vcpu);
  1458. free_pio_guest_pages(vcpu);
  1459. return 0;
  1460. }
  1461. static int complete_pio(struct kvm_vcpu *vcpu)
  1462. {
  1463. struct kvm_pio_request *io = &vcpu->pio;
  1464. long delta;
  1465. int r;
  1466. kvm_arch_ops->cache_regs(vcpu);
  1467. if (!io->string) {
  1468. if (io->in)
  1469. memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
  1470. io->size);
  1471. } else {
  1472. if (io->in) {
  1473. r = pio_copy_data(vcpu);
  1474. if (r) {
  1475. kvm_arch_ops->cache_regs(vcpu);
  1476. return r;
  1477. }
  1478. }
  1479. delta = 1;
  1480. if (io->rep) {
  1481. delta *= io->cur_count;
  1482. /*
  1483. * The size of the register should really depend on
  1484. * current address size.
  1485. */
  1486. vcpu->regs[VCPU_REGS_RCX] -= delta;
  1487. }
  1488. if (io->down)
  1489. delta = -delta;
  1490. delta *= io->size;
  1491. if (io->in)
  1492. vcpu->regs[VCPU_REGS_RDI] += delta;
  1493. else
  1494. vcpu->regs[VCPU_REGS_RSI] += delta;
  1495. }
  1496. kvm_arch_ops->decache_regs(vcpu);
  1497. io->count -= io->cur_count;
  1498. io->cur_count = 0;
  1499. if (!io->count)
  1500. kvm_arch_ops->skip_emulated_instruction(vcpu);
  1501. return 0;
  1502. }
  1503. void kernel_pio(struct kvm_io_device *pio_dev, struct kvm_vcpu *vcpu)
  1504. {
  1505. /* TODO: String I/O for in kernel device */
  1506. if (vcpu->pio.in)
  1507. kvm_iodevice_read(pio_dev, vcpu->pio.port,
  1508. vcpu->pio.size,
  1509. vcpu->pio_data);
  1510. else
  1511. kvm_iodevice_write(pio_dev, vcpu->pio.port,
  1512. vcpu->pio.size,
  1513. vcpu->pio_data);
  1514. }
  1515. int kvm_setup_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  1516. int size, unsigned long count, int string, int down,
  1517. gva_t address, int rep, unsigned port)
  1518. {
  1519. unsigned now, in_page;
  1520. int i;
  1521. int nr_pages = 1;
  1522. struct page *page;
  1523. struct kvm_io_device *pio_dev;
  1524. vcpu->run->exit_reason = KVM_EXIT_IO;
  1525. vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
  1526. vcpu->run->io.size = size;
  1527. vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
  1528. vcpu->run->io.count = count;
  1529. vcpu->run->io.port = port;
  1530. vcpu->pio.count = count;
  1531. vcpu->pio.cur_count = count;
  1532. vcpu->pio.size = size;
  1533. vcpu->pio.in = in;
  1534. vcpu->pio.port = port;
  1535. vcpu->pio.string = string;
  1536. vcpu->pio.down = down;
  1537. vcpu->pio.guest_page_offset = offset_in_page(address);
  1538. vcpu->pio.rep = rep;
  1539. pio_dev = vcpu_find_pio_dev(vcpu, port);
  1540. if (!string) {
  1541. kvm_arch_ops->cache_regs(vcpu);
  1542. memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
  1543. kvm_arch_ops->decache_regs(vcpu);
  1544. if (pio_dev) {
  1545. kernel_pio(pio_dev, vcpu);
  1546. complete_pio(vcpu);
  1547. return 1;
  1548. }
  1549. return 0;
  1550. }
  1551. /* TODO: String I/O for in kernel device */
  1552. if (pio_dev)
  1553. printk(KERN_ERR "kvm_setup_pio: no string io support\n");
  1554. if (!count) {
  1555. kvm_arch_ops->skip_emulated_instruction(vcpu);
  1556. return 1;
  1557. }
  1558. now = min(count, PAGE_SIZE / size);
  1559. if (!down)
  1560. in_page = PAGE_SIZE - offset_in_page(address);
  1561. else
  1562. in_page = offset_in_page(address) + size;
  1563. now = min(count, (unsigned long)in_page / size);
  1564. if (!now) {
  1565. /*
  1566. * String I/O straddles page boundary. Pin two guest pages
  1567. * so that we satisfy atomicity constraints. Do just one
  1568. * transaction to avoid complexity.
  1569. */
  1570. nr_pages = 2;
  1571. now = 1;
  1572. }
  1573. if (down) {
  1574. /*
  1575. * String I/O in reverse. Yuck. Kill the guest, fix later.
  1576. */
  1577. printk(KERN_ERR "kvm: guest string pio down\n");
  1578. inject_gp(vcpu);
  1579. return 1;
  1580. }
  1581. vcpu->run->io.count = now;
  1582. vcpu->pio.cur_count = now;
  1583. for (i = 0; i < nr_pages; ++i) {
  1584. spin_lock(&vcpu->kvm->lock);
  1585. page = gva_to_page(vcpu, address + i * PAGE_SIZE);
  1586. if (page)
  1587. get_page(page);
  1588. vcpu->pio.guest_pages[i] = page;
  1589. spin_unlock(&vcpu->kvm->lock);
  1590. if (!page) {
  1591. inject_gp(vcpu);
  1592. free_pio_guest_pages(vcpu);
  1593. return 1;
  1594. }
  1595. }
  1596. if (!vcpu->pio.in)
  1597. return pio_copy_data(vcpu);
  1598. return 0;
  1599. }
  1600. EXPORT_SYMBOL_GPL(kvm_setup_pio);
  1601. static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  1602. {
  1603. int r;
  1604. sigset_t sigsaved;
  1605. vcpu_load(vcpu);
  1606. if (vcpu->sigset_active)
  1607. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  1608. /* re-sync apic's tpr */
  1609. vcpu->cr8 = kvm_run->cr8;
  1610. if (vcpu->pio.cur_count) {
  1611. r = complete_pio(vcpu);
  1612. if (r)
  1613. goto out;
  1614. }
  1615. if (vcpu->mmio_needed) {
  1616. memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
  1617. vcpu->mmio_read_completed = 1;
  1618. vcpu->mmio_needed = 0;
  1619. r = emulate_instruction(vcpu, kvm_run,
  1620. vcpu->mmio_fault_cr2, 0);
  1621. if (r == EMULATE_DO_MMIO) {
  1622. /*
  1623. * Read-modify-write. Back to userspace.
  1624. */
  1625. kvm_run->exit_reason = KVM_EXIT_MMIO;
  1626. r = 0;
  1627. goto out;
  1628. }
  1629. }
  1630. if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
  1631. kvm_arch_ops->cache_regs(vcpu);
  1632. vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
  1633. kvm_arch_ops->decache_regs(vcpu);
  1634. }
  1635. r = kvm_arch_ops->run(vcpu, kvm_run);
  1636. out:
  1637. if (vcpu->sigset_active)
  1638. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1639. vcpu_put(vcpu);
  1640. return r;
  1641. }
  1642. static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
  1643. struct kvm_regs *regs)
  1644. {
  1645. vcpu_load(vcpu);
  1646. kvm_arch_ops->cache_regs(vcpu);
  1647. regs->rax = vcpu->regs[VCPU_REGS_RAX];
  1648. regs->rbx = vcpu->regs[VCPU_REGS_RBX];
  1649. regs->rcx = vcpu->regs[VCPU_REGS_RCX];
  1650. regs->rdx = vcpu->regs[VCPU_REGS_RDX];
  1651. regs->rsi = vcpu->regs[VCPU_REGS_RSI];
  1652. regs->rdi = vcpu->regs[VCPU_REGS_RDI];
  1653. regs->rsp = vcpu->regs[VCPU_REGS_RSP];
  1654. regs->rbp = vcpu->regs[VCPU_REGS_RBP];
  1655. #ifdef CONFIG_X86_64
  1656. regs->r8 = vcpu->regs[VCPU_REGS_R8];
  1657. regs->r9 = vcpu->regs[VCPU_REGS_R9];
  1658. regs->r10 = vcpu->regs[VCPU_REGS_R10];
  1659. regs->r11 = vcpu->regs[VCPU_REGS_R11];
  1660. regs->r12 = vcpu->regs[VCPU_REGS_R12];
  1661. regs->r13 = vcpu->regs[VCPU_REGS_R13];
  1662. regs->r14 = vcpu->regs[VCPU_REGS_R14];
  1663. regs->r15 = vcpu->regs[VCPU_REGS_R15];
  1664. #endif
  1665. regs->rip = vcpu->rip;
  1666. regs->rflags = kvm_arch_ops->get_rflags(vcpu);
  1667. /*
  1668. * Don't leak debug flags in case they were set for guest debugging
  1669. */
  1670. if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
  1671. regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
  1672. vcpu_put(vcpu);
  1673. return 0;
  1674. }
  1675. static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
  1676. struct kvm_regs *regs)
  1677. {
  1678. vcpu_load(vcpu);
  1679. vcpu->regs[VCPU_REGS_RAX] = regs->rax;
  1680. vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
  1681. vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
  1682. vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
  1683. vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
  1684. vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
  1685. vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
  1686. vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
  1687. #ifdef CONFIG_X86_64
  1688. vcpu->regs[VCPU_REGS_R8] = regs->r8;
  1689. vcpu->regs[VCPU_REGS_R9] = regs->r9;
  1690. vcpu->regs[VCPU_REGS_R10] = regs->r10;
  1691. vcpu->regs[VCPU_REGS_R11] = regs->r11;
  1692. vcpu->regs[VCPU_REGS_R12] = regs->r12;
  1693. vcpu->regs[VCPU_REGS_R13] = regs->r13;
  1694. vcpu->regs[VCPU_REGS_R14] = regs->r14;
  1695. vcpu->regs[VCPU_REGS_R15] = regs->r15;
  1696. #endif
  1697. vcpu->rip = regs->rip;
  1698. kvm_arch_ops->set_rflags(vcpu, regs->rflags);
  1699. kvm_arch_ops->decache_regs(vcpu);
  1700. vcpu_put(vcpu);
  1701. return 0;
  1702. }
  1703. static void get_segment(struct kvm_vcpu *vcpu,
  1704. struct kvm_segment *var, int seg)
  1705. {
  1706. return kvm_arch_ops->get_segment(vcpu, var, seg);
  1707. }
  1708. static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  1709. struct kvm_sregs *sregs)
  1710. {
  1711. struct descriptor_table dt;
  1712. vcpu_load(vcpu);
  1713. get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  1714. get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  1715. get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  1716. get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  1717. get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  1718. get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  1719. get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  1720. get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  1721. kvm_arch_ops->get_idt(vcpu, &dt);
  1722. sregs->idt.limit = dt.limit;
  1723. sregs->idt.base = dt.base;
  1724. kvm_arch_ops->get_gdt(vcpu, &dt);
  1725. sregs->gdt.limit = dt.limit;
  1726. sregs->gdt.base = dt.base;
  1727. kvm_arch_ops->decache_cr4_guest_bits(vcpu);
  1728. sregs->cr0 = vcpu->cr0;
  1729. sregs->cr2 = vcpu->cr2;
  1730. sregs->cr3 = vcpu->cr3;
  1731. sregs->cr4 = vcpu->cr4;
  1732. sregs->cr8 = vcpu->cr8;
  1733. sregs->efer = vcpu->shadow_efer;
  1734. sregs->apic_base = vcpu->apic_base;
  1735. memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
  1736. sizeof sregs->interrupt_bitmap);
  1737. vcpu_put(vcpu);
  1738. return 0;
  1739. }
  1740. static void set_segment(struct kvm_vcpu *vcpu,
  1741. struct kvm_segment *var, int seg)
  1742. {
  1743. return kvm_arch_ops->set_segment(vcpu, var, seg);
  1744. }
  1745. static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  1746. struct kvm_sregs *sregs)
  1747. {
  1748. int mmu_reset_needed = 0;
  1749. int i;
  1750. struct descriptor_table dt;
  1751. vcpu_load(vcpu);
  1752. dt.limit = sregs->idt.limit;
  1753. dt.base = sregs->idt.base;
  1754. kvm_arch_ops->set_idt(vcpu, &dt);
  1755. dt.limit = sregs->gdt.limit;
  1756. dt.base = sregs->gdt.base;
  1757. kvm_arch_ops->set_gdt(vcpu, &dt);
  1758. vcpu->cr2 = sregs->cr2;
  1759. mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
  1760. vcpu->cr3 = sregs->cr3;
  1761. vcpu->cr8 = sregs->cr8;
  1762. mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
  1763. #ifdef CONFIG_X86_64
  1764. kvm_arch_ops->set_efer(vcpu, sregs->efer);
  1765. #endif
  1766. vcpu->apic_base = sregs->apic_base;
  1767. kvm_arch_ops->decache_cr4_guest_bits(vcpu);
  1768. mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
  1769. kvm_arch_ops->set_cr0(vcpu, sregs->cr0);
  1770. mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
  1771. kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
  1772. if (!is_long_mode(vcpu) && is_pae(vcpu))
  1773. load_pdptrs(vcpu, vcpu->cr3);
  1774. if (mmu_reset_needed)
  1775. kvm_mmu_reset_context(vcpu);
  1776. memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
  1777. sizeof vcpu->irq_pending);
  1778. vcpu->irq_summary = 0;
  1779. for (i = 0; i < NR_IRQ_WORDS; ++i)
  1780. if (vcpu->irq_pending[i])
  1781. __set_bit(i, &vcpu->irq_summary);
  1782. set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  1783. set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  1784. set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  1785. set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  1786. set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  1787. set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  1788. set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  1789. set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  1790. vcpu_put(vcpu);
  1791. return 0;
  1792. }
  1793. /*
  1794. * List of msr numbers which we expose to userspace through KVM_GET_MSRS
  1795. * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
  1796. *
  1797. * This list is modified at module load time to reflect the
  1798. * capabilities of the host cpu.
  1799. */
  1800. static u32 msrs_to_save[] = {
  1801. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  1802. MSR_K6_STAR,
  1803. #ifdef CONFIG_X86_64
  1804. MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
  1805. #endif
  1806. MSR_IA32_TIME_STAMP_COUNTER,
  1807. };
  1808. static unsigned num_msrs_to_save;
  1809. static u32 emulated_msrs[] = {
  1810. MSR_IA32_MISC_ENABLE,
  1811. };
  1812. static __init void kvm_init_msr_list(void)
  1813. {
  1814. u32 dummy[2];
  1815. unsigned i, j;
  1816. for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
  1817. if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
  1818. continue;
  1819. if (j < i)
  1820. msrs_to_save[j] = msrs_to_save[i];
  1821. j++;
  1822. }
  1823. num_msrs_to_save = j;
  1824. }
  1825. /*
  1826. * Adapt set_msr() to msr_io()'s calling convention
  1827. */
  1828. static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
  1829. {
  1830. return kvm_set_msr(vcpu, index, *data);
  1831. }
  1832. /*
  1833. * Read or write a bunch of msrs. All parameters are kernel addresses.
  1834. *
  1835. * @return number of msrs set successfully.
  1836. */
  1837. static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
  1838. struct kvm_msr_entry *entries,
  1839. int (*do_msr)(struct kvm_vcpu *vcpu,
  1840. unsigned index, u64 *data))
  1841. {
  1842. int i;
  1843. vcpu_load(vcpu);
  1844. for (i = 0; i < msrs->nmsrs; ++i)
  1845. if (do_msr(vcpu, entries[i].index, &entries[i].data))
  1846. break;
  1847. vcpu_put(vcpu);
  1848. return i;
  1849. }
  1850. /*
  1851. * Read or write a bunch of msrs. Parameters are user addresses.
  1852. *
  1853. * @return number of msrs set successfully.
  1854. */
  1855. static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
  1856. int (*do_msr)(struct kvm_vcpu *vcpu,
  1857. unsigned index, u64 *data),
  1858. int writeback)
  1859. {
  1860. struct kvm_msrs msrs;
  1861. struct kvm_msr_entry *entries;
  1862. int r, n;
  1863. unsigned size;
  1864. r = -EFAULT;
  1865. if (copy_from_user(&msrs, user_msrs, sizeof msrs))
  1866. goto out;
  1867. r = -E2BIG;
  1868. if (msrs.nmsrs >= MAX_IO_MSRS)
  1869. goto out;
  1870. r = -ENOMEM;
  1871. size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
  1872. entries = vmalloc(size);
  1873. if (!entries)
  1874. goto out;
  1875. r = -EFAULT;
  1876. if (copy_from_user(entries, user_msrs->entries, size))
  1877. goto out_free;
  1878. r = n = __msr_io(vcpu, &msrs, entries, do_msr);
  1879. if (r < 0)
  1880. goto out_free;
  1881. r = -EFAULT;
  1882. if (writeback && copy_to_user(user_msrs->entries, entries, size))
  1883. goto out_free;
  1884. r = n;
  1885. out_free:
  1886. vfree(entries);
  1887. out:
  1888. return r;
  1889. }
  1890. /*
  1891. * Translate a guest virtual address to a guest physical address.
  1892. */
  1893. static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  1894. struct kvm_translation *tr)
  1895. {
  1896. unsigned long vaddr = tr->linear_address;
  1897. gpa_t gpa;
  1898. vcpu_load(vcpu);
  1899. spin_lock(&vcpu->kvm->lock);
  1900. gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
  1901. tr->physical_address = gpa;
  1902. tr->valid = gpa != UNMAPPED_GVA;
  1903. tr->writeable = 1;
  1904. tr->usermode = 0;
  1905. spin_unlock(&vcpu->kvm->lock);
  1906. vcpu_put(vcpu);
  1907. return 0;
  1908. }
  1909. static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
  1910. struct kvm_interrupt *irq)
  1911. {
  1912. if (irq->irq < 0 || irq->irq >= 256)
  1913. return -EINVAL;
  1914. vcpu_load(vcpu);
  1915. set_bit(irq->irq, vcpu->irq_pending);
  1916. set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
  1917. vcpu_put(vcpu);
  1918. return 0;
  1919. }
  1920. static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
  1921. struct kvm_debug_guest *dbg)
  1922. {
  1923. int r;
  1924. vcpu_load(vcpu);
  1925. r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
  1926. vcpu_put(vcpu);
  1927. return r;
  1928. }
  1929. static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
  1930. unsigned long address,
  1931. int *type)
  1932. {
  1933. struct kvm_vcpu *vcpu = vma->vm_file->private_data;
  1934. unsigned long pgoff;
  1935. struct page *page;
  1936. *type = VM_FAULT_MINOR;
  1937. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  1938. if (pgoff == 0)
  1939. page = virt_to_page(vcpu->run);
  1940. else if (pgoff == KVM_PIO_PAGE_OFFSET)
  1941. page = virt_to_page(vcpu->pio_data);
  1942. else
  1943. return NOPAGE_SIGBUS;
  1944. get_page(page);
  1945. return page;
  1946. }
  1947. static struct vm_operations_struct kvm_vcpu_vm_ops = {
  1948. .nopage = kvm_vcpu_nopage,
  1949. };
  1950. static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
  1951. {
  1952. vma->vm_ops = &kvm_vcpu_vm_ops;
  1953. return 0;
  1954. }
  1955. static int kvm_vcpu_release(struct inode *inode, struct file *filp)
  1956. {
  1957. struct kvm_vcpu *vcpu = filp->private_data;
  1958. fput(vcpu->kvm->filp);
  1959. return 0;
  1960. }
  1961. static struct file_operations kvm_vcpu_fops = {
  1962. .release = kvm_vcpu_release,
  1963. .unlocked_ioctl = kvm_vcpu_ioctl,
  1964. .compat_ioctl = kvm_vcpu_ioctl,
  1965. .mmap = kvm_vcpu_mmap,
  1966. };
  1967. /*
  1968. * Allocates an inode for the vcpu.
  1969. */
  1970. static int create_vcpu_fd(struct kvm_vcpu *vcpu)
  1971. {
  1972. int fd, r;
  1973. struct inode *inode;
  1974. struct file *file;
  1975. r = anon_inode_getfd(&fd, &inode, &file,
  1976. "kvm-vcpu", &kvm_vcpu_fops, vcpu);
  1977. if (r)
  1978. return r;
  1979. atomic_inc(&vcpu->kvm->filp->f_count);
  1980. return fd;
  1981. }
  1982. /*
  1983. * Creates some virtual cpus. Good luck creating more than one.
  1984. */
  1985. static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
  1986. {
  1987. int r;
  1988. struct kvm_vcpu *vcpu;
  1989. struct page *page;
  1990. r = -EINVAL;
  1991. if (!valid_vcpu(n))
  1992. goto out;
  1993. vcpu = &kvm->vcpus[n];
  1994. mutex_lock(&vcpu->mutex);
  1995. if (vcpu->vmcs) {
  1996. mutex_unlock(&vcpu->mutex);
  1997. return -EEXIST;
  1998. }
  1999. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  2000. r = -ENOMEM;
  2001. if (!page)
  2002. goto out_unlock;
  2003. vcpu->run = page_address(page);
  2004. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  2005. r = -ENOMEM;
  2006. if (!page)
  2007. goto out_free_run;
  2008. vcpu->pio_data = page_address(page);
  2009. vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
  2010. FX_IMAGE_ALIGN);
  2011. vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
  2012. vcpu->cr0 = 0x10;
  2013. r = kvm_arch_ops->vcpu_create(vcpu);
  2014. if (r < 0)
  2015. goto out_free_vcpus;
  2016. r = kvm_mmu_create(vcpu);
  2017. if (r < 0)
  2018. goto out_free_vcpus;
  2019. kvm_arch_ops->vcpu_load(vcpu);
  2020. r = kvm_mmu_setup(vcpu);
  2021. if (r >= 0)
  2022. r = kvm_arch_ops->vcpu_setup(vcpu);
  2023. vcpu_put(vcpu);
  2024. if (r < 0)
  2025. goto out_free_vcpus;
  2026. r = create_vcpu_fd(vcpu);
  2027. if (r < 0)
  2028. goto out_free_vcpus;
  2029. spin_lock(&kvm_lock);
  2030. if (n >= kvm->nvcpus)
  2031. kvm->nvcpus = n + 1;
  2032. spin_unlock(&kvm_lock);
  2033. return r;
  2034. out_free_vcpus:
  2035. kvm_free_vcpu(vcpu);
  2036. out_free_run:
  2037. free_page((unsigned long)vcpu->run);
  2038. vcpu->run = NULL;
  2039. out_unlock:
  2040. mutex_unlock(&vcpu->mutex);
  2041. out:
  2042. return r;
  2043. }
  2044. static void cpuid_fix_nx_cap(struct kvm_vcpu *vcpu)
  2045. {
  2046. u64 efer;
  2047. int i;
  2048. struct kvm_cpuid_entry *e, *entry;
  2049. rdmsrl(MSR_EFER, efer);
  2050. entry = NULL;
  2051. for (i = 0; i < vcpu->cpuid_nent; ++i) {
  2052. e = &vcpu->cpuid_entries[i];
  2053. if (e->function == 0x80000001) {
  2054. entry = e;
  2055. break;
  2056. }
  2057. }
  2058. if (entry && (entry->edx & (1 << 20)) && !(efer & EFER_NX)) {
  2059. entry->edx &= ~(1 << 20);
  2060. printk(KERN_INFO "kvm: guest NX capability removed\n");
  2061. }
  2062. }
  2063. static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
  2064. struct kvm_cpuid *cpuid,
  2065. struct kvm_cpuid_entry __user *entries)
  2066. {
  2067. int r;
  2068. r = -E2BIG;
  2069. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  2070. goto out;
  2071. r = -EFAULT;
  2072. if (copy_from_user(&vcpu->cpuid_entries, entries,
  2073. cpuid->nent * sizeof(struct kvm_cpuid_entry)))
  2074. goto out;
  2075. vcpu->cpuid_nent = cpuid->nent;
  2076. cpuid_fix_nx_cap(vcpu);
  2077. return 0;
  2078. out:
  2079. return r;
  2080. }
  2081. static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
  2082. {
  2083. if (sigset) {
  2084. sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
  2085. vcpu->sigset_active = 1;
  2086. vcpu->sigset = *sigset;
  2087. } else
  2088. vcpu->sigset_active = 0;
  2089. return 0;
  2090. }
  2091. /*
  2092. * fxsave fpu state. Taken from x86_64/processor.h. To be killed when
  2093. * we have asm/x86/processor.h
  2094. */
  2095. struct fxsave {
  2096. u16 cwd;
  2097. u16 swd;
  2098. u16 twd;
  2099. u16 fop;
  2100. u64 rip;
  2101. u64 rdp;
  2102. u32 mxcsr;
  2103. u32 mxcsr_mask;
  2104. u32 st_space[32]; /* 8*16 bytes for each FP-reg = 128 bytes */
  2105. #ifdef CONFIG_X86_64
  2106. u32 xmm_space[64]; /* 16*16 bytes for each XMM-reg = 256 bytes */
  2107. #else
  2108. u32 xmm_space[32]; /* 8*16 bytes for each XMM-reg = 128 bytes */
  2109. #endif
  2110. };
  2111. static int kvm_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  2112. {
  2113. struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
  2114. vcpu_load(vcpu);
  2115. memcpy(fpu->fpr, fxsave->st_space, 128);
  2116. fpu->fcw = fxsave->cwd;
  2117. fpu->fsw = fxsave->swd;
  2118. fpu->ftwx = fxsave->twd;
  2119. fpu->last_opcode = fxsave->fop;
  2120. fpu->last_ip = fxsave->rip;
  2121. fpu->last_dp = fxsave->rdp;
  2122. memcpy(fpu->xmm, fxsave->xmm_space, sizeof fxsave->xmm_space);
  2123. vcpu_put(vcpu);
  2124. return 0;
  2125. }
  2126. static int kvm_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
  2127. {
  2128. struct fxsave *fxsave = (struct fxsave *)vcpu->guest_fx_image;
  2129. vcpu_load(vcpu);
  2130. memcpy(fxsave->st_space, fpu->fpr, 128);
  2131. fxsave->cwd = fpu->fcw;
  2132. fxsave->swd = fpu->fsw;
  2133. fxsave->twd = fpu->ftwx;
  2134. fxsave->fop = fpu->last_opcode;
  2135. fxsave->rip = fpu->last_ip;
  2136. fxsave->rdp = fpu->last_dp;
  2137. memcpy(fxsave->xmm_space, fpu->xmm, sizeof fxsave->xmm_space);
  2138. vcpu_put(vcpu);
  2139. return 0;
  2140. }
  2141. static long kvm_vcpu_ioctl(struct file *filp,
  2142. unsigned int ioctl, unsigned long arg)
  2143. {
  2144. struct kvm_vcpu *vcpu = filp->private_data;
  2145. void __user *argp = (void __user *)arg;
  2146. int r = -EINVAL;
  2147. switch (ioctl) {
  2148. case KVM_RUN:
  2149. r = -EINVAL;
  2150. if (arg)
  2151. goto out;
  2152. r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
  2153. break;
  2154. case KVM_GET_REGS: {
  2155. struct kvm_regs kvm_regs;
  2156. memset(&kvm_regs, 0, sizeof kvm_regs);
  2157. r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
  2158. if (r)
  2159. goto out;
  2160. r = -EFAULT;
  2161. if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
  2162. goto out;
  2163. r = 0;
  2164. break;
  2165. }
  2166. case KVM_SET_REGS: {
  2167. struct kvm_regs kvm_regs;
  2168. r = -EFAULT;
  2169. if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
  2170. goto out;
  2171. r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
  2172. if (r)
  2173. goto out;
  2174. r = 0;
  2175. break;
  2176. }
  2177. case KVM_GET_SREGS: {
  2178. struct kvm_sregs kvm_sregs;
  2179. memset(&kvm_sregs, 0, sizeof kvm_sregs);
  2180. r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
  2181. if (r)
  2182. goto out;
  2183. r = -EFAULT;
  2184. if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
  2185. goto out;
  2186. r = 0;
  2187. break;
  2188. }
  2189. case KVM_SET_SREGS: {
  2190. struct kvm_sregs kvm_sregs;
  2191. r = -EFAULT;
  2192. if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
  2193. goto out;
  2194. r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
  2195. if (r)
  2196. goto out;
  2197. r = 0;
  2198. break;
  2199. }
  2200. case KVM_TRANSLATE: {
  2201. struct kvm_translation tr;
  2202. r = -EFAULT;
  2203. if (copy_from_user(&tr, argp, sizeof tr))
  2204. goto out;
  2205. r = kvm_vcpu_ioctl_translate(vcpu, &tr);
  2206. if (r)
  2207. goto out;
  2208. r = -EFAULT;
  2209. if (copy_to_user(argp, &tr, sizeof tr))
  2210. goto out;
  2211. r = 0;
  2212. break;
  2213. }
  2214. case KVM_INTERRUPT: {
  2215. struct kvm_interrupt irq;
  2216. r = -EFAULT;
  2217. if (copy_from_user(&irq, argp, sizeof irq))
  2218. goto out;
  2219. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  2220. if (r)
  2221. goto out;
  2222. r = 0;
  2223. break;
  2224. }
  2225. case KVM_DEBUG_GUEST: {
  2226. struct kvm_debug_guest dbg;
  2227. r = -EFAULT;
  2228. if (copy_from_user(&dbg, argp, sizeof dbg))
  2229. goto out;
  2230. r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
  2231. if (r)
  2232. goto out;
  2233. r = 0;
  2234. break;
  2235. }
  2236. case KVM_GET_MSRS:
  2237. r = msr_io(vcpu, argp, kvm_get_msr, 1);
  2238. break;
  2239. case KVM_SET_MSRS:
  2240. r = msr_io(vcpu, argp, do_set_msr, 0);
  2241. break;
  2242. case KVM_SET_CPUID: {
  2243. struct kvm_cpuid __user *cpuid_arg = argp;
  2244. struct kvm_cpuid cpuid;
  2245. r = -EFAULT;
  2246. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  2247. goto out;
  2248. r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
  2249. if (r)
  2250. goto out;
  2251. break;
  2252. }
  2253. case KVM_SET_SIGNAL_MASK: {
  2254. struct kvm_signal_mask __user *sigmask_arg = argp;
  2255. struct kvm_signal_mask kvm_sigmask;
  2256. sigset_t sigset, *p;
  2257. p = NULL;
  2258. if (argp) {
  2259. r = -EFAULT;
  2260. if (copy_from_user(&kvm_sigmask, argp,
  2261. sizeof kvm_sigmask))
  2262. goto out;
  2263. r = -EINVAL;
  2264. if (kvm_sigmask.len != sizeof sigset)
  2265. goto out;
  2266. r = -EFAULT;
  2267. if (copy_from_user(&sigset, sigmask_arg->sigset,
  2268. sizeof sigset))
  2269. goto out;
  2270. p = &sigset;
  2271. }
  2272. r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
  2273. break;
  2274. }
  2275. case KVM_GET_FPU: {
  2276. struct kvm_fpu fpu;
  2277. memset(&fpu, 0, sizeof fpu);
  2278. r = kvm_vcpu_ioctl_get_fpu(vcpu, &fpu);
  2279. if (r)
  2280. goto out;
  2281. r = -EFAULT;
  2282. if (copy_to_user(argp, &fpu, sizeof fpu))
  2283. goto out;
  2284. r = 0;
  2285. break;
  2286. }
  2287. case KVM_SET_FPU: {
  2288. struct kvm_fpu fpu;
  2289. r = -EFAULT;
  2290. if (copy_from_user(&fpu, argp, sizeof fpu))
  2291. goto out;
  2292. r = kvm_vcpu_ioctl_set_fpu(vcpu, &fpu);
  2293. if (r)
  2294. goto out;
  2295. r = 0;
  2296. break;
  2297. }
  2298. default:
  2299. ;
  2300. }
  2301. out:
  2302. return r;
  2303. }
  2304. static long kvm_vm_ioctl(struct file *filp,
  2305. unsigned int ioctl, unsigned long arg)
  2306. {
  2307. struct kvm *kvm = filp->private_data;
  2308. void __user *argp = (void __user *)arg;
  2309. int r = -EINVAL;
  2310. switch (ioctl) {
  2311. case KVM_CREATE_VCPU:
  2312. r = kvm_vm_ioctl_create_vcpu(kvm, arg);
  2313. if (r < 0)
  2314. goto out;
  2315. break;
  2316. case KVM_SET_MEMORY_REGION: {
  2317. struct kvm_memory_region kvm_mem;
  2318. r = -EFAULT;
  2319. if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
  2320. goto out;
  2321. r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
  2322. if (r)
  2323. goto out;
  2324. break;
  2325. }
  2326. case KVM_GET_DIRTY_LOG: {
  2327. struct kvm_dirty_log log;
  2328. r = -EFAULT;
  2329. if (copy_from_user(&log, argp, sizeof log))
  2330. goto out;
  2331. r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
  2332. if (r)
  2333. goto out;
  2334. break;
  2335. }
  2336. case KVM_SET_MEMORY_ALIAS: {
  2337. struct kvm_memory_alias alias;
  2338. r = -EFAULT;
  2339. if (copy_from_user(&alias, argp, sizeof alias))
  2340. goto out;
  2341. r = kvm_vm_ioctl_set_memory_alias(kvm, &alias);
  2342. if (r)
  2343. goto out;
  2344. break;
  2345. }
  2346. default:
  2347. ;
  2348. }
  2349. out:
  2350. return r;
  2351. }
  2352. static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
  2353. unsigned long address,
  2354. int *type)
  2355. {
  2356. struct kvm *kvm = vma->vm_file->private_data;
  2357. unsigned long pgoff;
  2358. struct page *page;
  2359. *type = VM_FAULT_MINOR;
  2360. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  2361. page = gfn_to_page(kvm, pgoff);
  2362. if (!page)
  2363. return NOPAGE_SIGBUS;
  2364. get_page(page);
  2365. return page;
  2366. }
  2367. static struct vm_operations_struct kvm_vm_vm_ops = {
  2368. .nopage = kvm_vm_nopage,
  2369. };
  2370. static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
  2371. {
  2372. vma->vm_ops = &kvm_vm_vm_ops;
  2373. return 0;
  2374. }
  2375. static struct file_operations kvm_vm_fops = {
  2376. .release = kvm_vm_release,
  2377. .unlocked_ioctl = kvm_vm_ioctl,
  2378. .compat_ioctl = kvm_vm_ioctl,
  2379. .mmap = kvm_vm_mmap,
  2380. };
  2381. static int kvm_dev_ioctl_create_vm(void)
  2382. {
  2383. int fd, r;
  2384. struct inode *inode;
  2385. struct file *file;
  2386. struct kvm *kvm;
  2387. kvm = kvm_create_vm();
  2388. if (IS_ERR(kvm))
  2389. return PTR_ERR(kvm);
  2390. r = anon_inode_getfd(&fd, &inode, &file, "kvm-vm", &kvm_vm_fops, kvm);
  2391. if (r) {
  2392. kvm_destroy_vm(kvm);
  2393. return r;
  2394. }
  2395. kvm->filp = file;
  2396. return fd;
  2397. }
  2398. static long kvm_dev_ioctl(struct file *filp,
  2399. unsigned int ioctl, unsigned long arg)
  2400. {
  2401. void __user *argp = (void __user *)arg;
  2402. long r = -EINVAL;
  2403. switch (ioctl) {
  2404. case KVM_GET_API_VERSION:
  2405. r = -EINVAL;
  2406. if (arg)
  2407. goto out;
  2408. r = KVM_API_VERSION;
  2409. break;
  2410. case KVM_CREATE_VM:
  2411. r = -EINVAL;
  2412. if (arg)
  2413. goto out;
  2414. r = kvm_dev_ioctl_create_vm();
  2415. break;
  2416. case KVM_GET_MSR_INDEX_LIST: {
  2417. struct kvm_msr_list __user *user_msr_list = argp;
  2418. struct kvm_msr_list msr_list;
  2419. unsigned n;
  2420. r = -EFAULT;
  2421. if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
  2422. goto out;
  2423. n = msr_list.nmsrs;
  2424. msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
  2425. if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
  2426. goto out;
  2427. r = -E2BIG;
  2428. if (n < num_msrs_to_save)
  2429. goto out;
  2430. r = -EFAULT;
  2431. if (copy_to_user(user_msr_list->indices, &msrs_to_save,
  2432. num_msrs_to_save * sizeof(u32)))
  2433. goto out;
  2434. if (copy_to_user(user_msr_list->indices
  2435. + num_msrs_to_save * sizeof(u32),
  2436. &emulated_msrs,
  2437. ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
  2438. goto out;
  2439. r = 0;
  2440. break;
  2441. }
  2442. case KVM_CHECK_EXTENSION:
  2443. /*
  2444. * No extensions defined at present.
  2445. */
  2446. r = 0;
  2447. break;
  2448. case KVM_GET_VCPU_MMAP_SIZE:
  2449. r = -EINVAL;
  2450. if (arg)
  2451. goto out;
  2452. r = 2 * PAGE_SIZE;
  2453. break;
  2454. default:
  2455. ;
  2456. }
  2457. out:
  2458. return r;
  2459. }
  2460. static struct file_operations kvm_chardev_ops = {
  2461. .open = kvm_dev_open,
  2462. .release = kvm_dev_release,
  2463. .unlocked_ioctl = kvm_dev_ioctl,
  2464. .compat_ioctl = kvm_dev_ioctl,
  2465. };
  2466. static struct miscdevice kvm_dev = {
  2467. KVM_MINOR,
  2468. "kvm",
  2469. &kvm_chardev_ops,
  2470. };
  2471. static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
  2472. void *v)
  2473. {
  2474. if (val == SYS_RESTART) {
  2475. /*
  2476. * Some (well, at least mine) BIOSes hang on reboot if
  2477. * in vmx root mode.
  2478. */
  2479. printk(KERN_INFO "kvm: exiting hardware virtualization\n");
  2480. on_each_cpu(hardware_disable, NULL, 0, 1);
  2481. }
  2482. return NOTIFY_OK;
  2483. }
  2484. static struct notifier_block kvm_reboot_notifier = {
  2485. .notifier_call = kvm_reboot,
  2486. .priority = 0,
  2487. };
  2488. /*
  2489. * Make sure that a cpu that is being hot-unplugged does not have any vcpus
  2490. * cached on it.
  2491. */
  2492. static void decache_vcpus_on_cpu(int cpu)
  2493. {
  2494. struct kvm *vm;
  2495. struct kvm_vcpu *vcpu;
  2496. int i;
  2497. spin_lock(&kvm_lock);
  2498. list_for_each_entry(vm, &vm_list, vm_list)
  2499. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  2500. vcpu = &vm->vcpus[i];
  2501. /*
  2502. * If the vcpu is locked, then it is running on some
  2503. * other cpu and therefore it is not cached on the
  2504. * cpu in question.
  2505. *
  2506. * If it's not locked, check the last cpu it executed
  2507. * on.
  2508. */
  2509. if (mutex_trylock(&vcpu->mutex)) {
  2510. if (vcpu->cpu == cpu) {
  2511. kvm_arch_ops->vcpu_decache(vcpu);
  2512. vcpu->cpu = -1;
  2513. }
  2514. mutex_unlock(&vcpu->mutex);
  2515. }
  2516. }
  2517. spin_unlock(&kvm_lock);
  2518. }
  2519. static void hardware_enable(void *junk)
  2520. {
  2521. int cpu = raw_smp_processor_id();
  2522. if (cpu_isset(cpu, cpus_hardware_enabled))
  2523. return;
  2524. cpu_set(cpu, cpus_hardware_enabled);
  2525. kvm_arch_ops->hardware_enable(NULL);
  2526. }
  2527. static void hardware_disable(void *junk)
  2528. {
  2529. int cpu = raw_smp_processor_id();
  2530. if (!cpu_isset(cpu, cpus_hardware_enabled))
  2531. return;
  2532. cpu_clear(cpu, cpus_hardware_enabled);
  2533. decache_vcpus_on_cpu(cpu);
  2534. kvm_arch_ops->hardware_disable(NULL);
  2535. }
  2536. static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
  2537. void *v)
  2538. {
  2539. int cpu = (long)v;
  2540. switch (val) {
  2541. case CPU_DYING:
  2542. case CPU_DYING_FROZEN:
  2543. printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
  2544. cpu);
  2545. hardware_disable(NULL);
  2546. break;
  2547. case CPU_UP_CANCELED:
  2548. case CPU_UP_CANCELED_FROZEN:
  2549. printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
  2550. cpu);
  2551. smp_call_function_single(cpu, hardware_disable, NULL, 0, 1);
  2552. break;
  2553. case CPU_ONLINE:
  2554. case CPU_ONLINE_FROZEN:
  2555. printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
  2556. cpu);
  2557. smp_call_function_single(cpu, hardware_enable, NULL, 0, 1);
  2558. break;
  2559. }
  2560. return NOTIFY_OK;
  2561. }
  2562. void kvm_io_bus_init(struct kvm_io_bus *bus)
  2563. {
  2564. memset(bus, 0, sizeof(*bus));
  2565. }
  2566. void kvm_io_bus_destroy(struct kvm_io_bus *bus)
  2567. {
  2568. int i;
  2569. for (i = 0; i < bus->dev_count; i++) {
  2570. struct kvm_io_device *pos = bus->devs[i];
  2571. kvm_iodevice_destructor(pos);
  2572. }
  2573. }
  2574. struct kvm_io_device *kvm_io_bus_find_dev(struct kvm_io_bus *bus, gpa_t addr)
  2575. {
  2576. int i;
  2577. for (i = 0; i < bus->dev_count; i++) {
  2578. struct kvm_io_device *pos = bus->devs[i];
  2579. if (pos->in_range(pos, addr))
  2580. return pos;
  2581. }
  2582. return NULL;
  2583. }
  2584. void kvm_io_bus_register_dev(struct kvm_io_bus *bus, struct kvm_io_device *dev)
  2585. {
  2586. BUG_ON(bus->dev_count > (NR_IOBUS_DEVS-1));
  2587. bus->devs[bus->dev_count++] = dev;
  2588. }
  2589. static struct notifier_block kvm_cpu_notifier = {
  2590. .notifier_call = kvm_cpu_hotplug,
  2591. .priority = 20, /* must be > scheduler priority */
  2592. };
  2593. static u64 stat_get(void *_offset)
  2594. {
  2595. unsigned offset = (long)_offset;
  2596. u64 total = 0;
  2597. struct kvm *kvm;
  2598. struct kvm_vcpu *vcpu;
  2599. int i;
  2600. spin_lock(&kvm_lock);
  2601. list_for_each_entry(kvm, &vm_list, vm_list)
  2602. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  2603. vcpu = &kvm->vcpus[i];
  2604. total += *(u32 *)((void *)vcpu + offset);
  2605. }
  2606. spin_unlock(&kvm_lock);
  2607. return total;
  2608. }
  2609. static void stat_set(void *offset, u64 val)
  2610. {
  2611. }
  2612. DEFINE_SIMPLE_ATTRIBUTE(stat_fops, stat_get, stat_set, "%llu\n");
  2613. static __init void kvm_init_debug(void)
  2614. {
  2615. struct kvm_stats_debugfs_item *p;
  2616. debugfs_dir = debugfs_create_dir("kvm", NULL);
  2617. for (p = debugfs_entries; p->name; ++p)
  2618. p->dentry = debugfs_create_file(p->name, 0444, debugfs_dir,
  2619. (void *)(long)p->offset,
  2620. &stat_fops);
  2621. }
  2622. static void kvm_exit_debug(void)
  2623. {
  2624. struct kvm_stats_debugfs_item *p;
  2625. for (p = debugfs_entries; p->name; ++p)
  2626. debugfs_remove(p->dentry);
  2627. debugfs_remove(debugfs_dir);
  2628. }
  2629. static int kvm_suspend(struct sys_device *dev, pm_message_t state)
  2630. {
  2631. hardware_disable(NULL);
  2632. return 0;
  2633. }
  2634. static int kvm_resume(struct sys_device *dev)
  2635. {
  2636. hardware_enable(NULL);
  2637. return 0;
  2638. }
  2639. static struct sysdev_class kvm_sysdev_class = {
  2640. set_kset_name("kvm"),
  2641. .suspend = kvm_suspend,
  2642. .resume = kvm_resume,
  2643. };
  2644. static struct sys_device kvm_sysdev = {
  2645. .id = 0,
  2646. .cls = &kvm_sysdev_class,
  2647. };
  2648. hpa_t bad_page_address;
  2649. int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
  2650. {
  2651. int r;
  2652. if (kvm_arch_ops) {
  2653. printk(KERN_ERR "kvm: already loaded the other module\n");
  2654. return -EEXIST;
  2655. }
  2656. if (!ops->cpu_has_kvm_support()) {
  2657. printk(KERN_ERR "kvm: no hardware support\n");
  2658. return -EOPNOTSUPP;
  2659. }
  2660. if (ops->disabled_by_bios()) {
  2661. printk(KERN_ERR "kvm: disabled by bios\n");
  2662. return -EOPNOTSUPP;
  2663. }
  2664. kvm_arch_ops = ops;
  2665. r = kvm_arch_ops->hardware_setup();
  2666. if (r < 0)
  2667. goto out;
  2668. on_each_cpu(hardware_enable, NULL, 0, 1);
  2669. r = register_cpu_notifier(&kvm_cpu_notifier);
  2670. if (r)
  2671. goto out_free_1;
  2672. register_reboot_notifier(&kvm_reboot_notifier);
  2673. r = sysdev_class_register(&kvm_sysdev_class);
  2674. if (r)
  2675. goto out_free_2;
  2676. r = sysdev_register(&kvm_sysdev);
  2677. if (r)
  2678. goto out_free_3;
  2679. kvm_chardev_ops.owner = module;
  2680. r = misc_register(&kvm_dev);
  2681. if (r) {
  2682. printk (KERN_ERR "kvm: misc device register failed\n");
  2683. goto out_free;
  2684. }
  2685. return r;
  2686. out_free:
  2687. sysdev_unregister(&kvm_sysdev);
  2688. out_free_3:
  2689. sysdev_class_unregister(&kvm_sysdev_class);
  2690. out_free_2:
  2691. unregister_reboot_notifier(&kvm_reboot_notifier);
  2692. unregister_cpu_notifier(&kvm_cpu_notifier);
  2693. out_free_1:
  2694. on_each_cpu(hardware_disable, NULL, 0, 1);
  2695. kvm_arch_ops->hardware_unsetup();
  2696. out:
  2697. kvm_arch_ops = NULL;
  2698. return r;
  2699. }
  2700. void kvm_exit_arch(void)
  2701. {
  2702. misc_deregister(&kvm_dev);
  2703. sysdev_unregister(&kvm_sysdev);
  2704. sysdev_class_unregister(&kvm_sysdev_class);
  2705. unregister_reboot_notifier(&kvm_reboot_notifier);
  2706. unregister_cpu_notifier(&kvm_cpu_notifier);
  2707. on_each_cpu(hardware_disable, NULL, 0, 1);
  2708. kvm_arch_ops->hardware_unsetup();
  2709. kvm_arch_ops = NULL;
  2710. }
  2711. static __init int kvm_init(void)
  2712. {
  2713. static struct page *bad_page;
  2714. int r;
  2715. r = kvm_mmu_module_init();
  2716. if (r)
  2717. goto out4;
  2718. kvm_init_debug();
  2719. kvm_init_msr_list();
  2720. if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
  2721. r = -ENOMEM;
  2722. goto out;
  2723. }
  2724. bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
  2725. memset(__va(bad_page_address), 0, PAGE_SIZE);
  2726. return 0;
  2727. out:
  2728. kvm_exit_debug();
  2729. kvm_mmu_module_exit();
  2730. out4:
  2731. return r;
  2732. }
  2733. static __exit void kvm_exit(void)
  2734. {
  2735. kvm_exit_debug();
  2736. __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
  2737. kvm_mmu_module_exit();
  2738. }
  2739. module_init(kvm_init)
  2740. module_exit(kvm_exit)
  2741. EXPORT_SYMBOL_GPL(kvm_init_arch);
  2742. EXPORT_SYMBOL_GPL(kvm_exit_arch);