kvm_main.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558
  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 <linux/kvm.h>
  19. #include <linux/module.h>
  20. #include <linux/errno.h>
  21. #include <asm/processor.h>
  22. #include <linux/percpu.h>
  23. #include <linux/gfp.h>
  24. #include <asm/msr.h>
  25. #include <linux/mm.h>
  26. #include <linux/miscdevice.h>
  27. #include <linux/vmalloc.h>
  28. #include <asm/uaccess.h>
  29. #include <linux/reboot.h>
  30. #include <asm/io.h>
  31. #include <linux/debugfs.h>
  32. #include <linux/highmem.h>
  33. #include <linux/file.h>
  34. #include <asm/desc.h>
  35. #include <linux/sysdev.h>
  36. #include <linux/cpu.h>
  37. #include <linux/file.h>
  38. #include <linux/fs.h>
  39. #include <linux/mount.h>
  40. #include "x86_emulate.h"
  41. #include "segment_descriptor.h"
  42. MODULE_AUTHOR("Qumranet");
  43. MODULE_LICENSE("GPL");
  44. static DEFINE_SPINLOCK(kvm_lock);
  45. static LIST_HEAD(vm_list);
  46. struct kvm_arch_ops *kvm_arch_ops;
  47. struct kvm_stat kvm_stat;
  48. EXPORT_SYMBOL_GPL(kvm_stat);
  49. static struct kvm_stats_debugfs_item {
  50. const char *name;
  51. u32 *data;
  52. struct dentry *dentry;
  53. } debugfs_entries[] = {
  54. { "pf_fixed", &kvm_stat.pf_fixed },
  55. { "pf_guest", &kvm_stat.pf_guest },
  56. { "tlb_flush", &kvm_stat.tlb_flush },
  57. { "invlpg", &kvm_stat.invlpg },
  58. { "exits", &kvm_stat.exits },
  59. { "io_exits", &kvm_stat.io_exits },
  60. { "mmio_exits", &kvm_stat.mmio_exits },
  61. { "signal_exits", &kvm_stat.signal_exits },
  62. { "irq_window", &kvm_stat.irq_window_exits },
  63. { "halt_exits", &kvm_stat.halt_exits },
  64. { "request_irq", &kvm_stat.request_irq_exits },
  65. { "irq_exits", &kvm_stat.irq_exits },
  66. { NULL, NULL }
  67. };
  68. static struct dentry *debugfs_dir;
  69. #define KVMFS_MAGIC 0x19700426
  70. struct vfsmount *kvmfs_mnt;
  71. #define MAX_IO_MSRS 256
  72. #define CR0_RESEVED_BITS 0xffffffff1ffaffc0ULL
  73. #define LMSW_GUEST_MASK 0x0eULL
  74. #define CR4_RESEVED_BITS (~((1ULL << 11) - 1))
  75. #define CR8_RESEVED_BITS (~0x0fULL)
  76. #define EFER_RESERVED_BITS 0xfffffffffffff2fe
  77. #ifdef CONFIG_X86_64
  78. // LDT or TSS descriptor in the GDT. 16 bytes.
  79. struct segment_descriptor_64 {
  80. struct segment_descriptor s;
  81. u32 base_higher;
  82. u32 pad_zero;
  83. };
  84. #endif
  85. static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
  86. unsigned long arg);
  87. static struct inode *kvmfs_inode(struct file_operations *fops)
  88. {
  89. int error = -ENOMEM;
  90. struct inode *inode = new_inode(kvmfs_mnt->mnt_sb);
  91. if (!inode)
  92. goto eexit_1;
  93. inode->i_fop = fops;
  94. /*
  95. * Mark the inode dirty from the very beginning,
  96. * that way it will never be moved to the dirty
  97. * list because mark_inode_dirty() will think
  98. * that it already _is_ on the dirty list.
  99. */
  100. inode->i_state = I_DIRTY;
  101. inode->i_mode = S_IRUSR | S_IWUSR;
  102. inode->i_uid = current->fsuid;
  103. inode->i_gid = current->fsgid;
  104. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  105. return inode;
  106. eexit_1:
  107. return ERR_PTR(error);
  108. }
  109. static struct file *kvmfs_file(struct inode *inode, void *private_data)
  110. {
  111. struct file *file = get_empty_filp();
  112. if (!file)
  113. return ERR_PTR(-ENFILE);
  114. file->f_path.mnt = mntget(kvmfs_mnt);
  115. file->f_path.dentry = d_alloc_anon(inode);
  116. if (!file->f_path.dentry)
  117. return ERR_PTR(-ENOMEM);
  118. file->f_mapping = inode->i_mapping;
  119. file->f_pos = 0;
  120. file->f_flags = O_RDWR;
  121. file->f_op = inode->i_fop;
  122. file->f_mode = FMODE_READ | FMODE_WRITE;
  123. file->f_version = 0;
  124. file->private_data = private_data;
  125. return file;
  126. }
  127. unsigned long segment_base(u16 selector)
  128. {
  129. struct descriptor_table gdt;
  130. struct segment_descriptor *d;
  131. unsigned long table_base;
  132. typedef unsigned long ul;
  133. unsigned long v;
  134. if (selector == 0)
  135. return 0;
  136. asm ("sgdt %0" : "=m"(gdt));
  137. table_base = gdt.base;
  138. if (selector & 4) { /* from ldt */
  139. u16 ldt_selector;
  140. asm ("sldt %0" : "=g"(ldt_selector));
  141. table_base = segment_base(ldt_selector);
  142. }
  143. d = (struct segment_descriptor *)(table_base + (selector & ~7));
  144. v = d->base_low | ((ul)d->base_mid << 16) | ((ul)d->base_high << 24);
  145. #ifdef CONFIG_X86_64
  146. if (d->system == 0
  147. && (d->type == 2 || d->type == 9 || d->type == 11))
  148. v |= ((ul)((struct segment_descriptor_64 *)d)->base_higher) << 32;
  149. #endif
  150. return v;
  151. }
  152. EXPORT_SYMBOL_GPL(segment_base);
  153. static inline int valid_vcpu(int n)
  154. {
  155. return likely(n >= 0 && n < KVM_MAX_VCPUS);
  156. }
  157. int kvm_read_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
  158. void *dest)
  159. {
  160. unsigned char *host_buf = dest;
  161. unsigned long req_size = size;
  162. while (size) {
  163. hpa_t paddr;
  164. unsigned now;
  165. unsigned offset;
  166. hva_t guest_buf;
  167. paddr = gva_to_hpa(vcpu, addr);
  168. if (is_error_hpa(paddr))
  169. break;
  170. guest_buf = (hva_t)kmap_atomic(
  171. pfn_to_page(paddr >> PAGE_SHIFT),
  172. KM_USER0);
  173. offset = addr & ~PAGE_MASK;
  174. guest_buf |= offset;
  175. now = min(size, PAGE_SIZE - offset);
  176. memcpy(host_buf, (void*)guest_buf, now);
  177. host_buf += now;
  178. addr += now;
  179. size -= now;
  180. kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
  181. }
  182. return req_size - size;
  183. }
  184. EXPORT_SYMBOL_GPL(kvm_read_guest);
  185. int kvm_write_guest(struct kvm_vcpu *vcpu, gva_t addr, unsigned long size,
  186. void *data)
  187. {
  188. unsigned char *host_buf = data;
  189. unsigned long req_size = size;
  190. while (size) {
  191. hpa_t paddr;
  192. unsigned now;
  193. unsigned offset;
  194. hva_t guest_buf;
  195. paddr = gva_to_hpa(vcpu, addr);
  196. if (is_error_hpa(paddr))
  197. break;
  198. guest_buf = (hva_t)kmap_atomic(
  199. pfn_to_page(paddr >> PAGE_SHIFT), KM_USER0);
  200. offset = addr & ~PAGE_MASK;
  201. guest_buf |= offset;
  202. now = min(size, PAGE_SIZE - offset);
  203. memcpy((void*)guest_buf, host_buf, now);
  204. host_buf += now;
  205. addr += now;
  206. size -= now;
  207. kunmap_atomic((void *)(guest_buf & PAGE_MASK), KM_USER0);
  208. }
  209. return req_size - size;
  210. }
  211. EXPORT_SYMBOL_GPL(kvm_write_guest);
  212. /*
  213. * Switches to specified vcpu, until a matching vcpu_put()
  214. */
  215. static void vcpu_load(struct kvm_vcpu *vcpu)
  216. {
  217. mutex_lock(&vcpu->mutex);
  218. kvm_arch_ops->vcpu_load(vcpu);
  219. }
  220. /*
  221. * Switches to specified vcpu, until a matching vcpu_put(). Will return NULL
  222. * if the slot is not populated.
  223. */
  224. static struct kvm_vcpu *vcpu_load_slot(struct kvm *kvm, int slot)
  225. {
  226. struct kvm_vcpu *vcpu = &kvm->vcpus[slot];
  227. mutex_lock(&vcpu->mutex);
  228. if (!vcpu->vmcs) {
  229. mutex_unlock(&vcpu->mutex);
  230. return NULL;
  231. }
  232. kvm_arch_ops->vcpu_load(vcpu);
  233. return vcpu;
  234. }
  235. static void vcpu_put(struct kvm_vcpu *vcpu)
  236. {
  237. kvm_arch_ops->vcpu_put(vcpu);
  238. mutex_unlock(&vcpu->mutex);
  239. }
  240. static struct kvm *kvm_create_vm(void)
  241. {
  242. struct kvm *kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
  243. int i;
  244. if (!kvm)
  245. return ERR_PTR(-ENOMEM);
  246. spin_lock_init(&kvm->lock);
  247. INIT_LIST_HEAD(&kvm->active_mmu_pages);
  248. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  249. struct kvm_vcpu *vcpu = &kvm->vcpus[i];
  250. mutex_init(&vcpu->mutex);
  251. vcpu->cpu = -1;
  252. vcpu->kvm = kvm;
  253. vcpu->mmu.root_hpa = INVALID_PAGE;
  254. INIT_LIST_HEAD(&vcpu->free_pages);
  255. spin_lock(&kvm_lock);
  256. list_add(&kvm->vm_list, &vm_list);
  257. spin_unlock(&kvm_lock);
  258. }
  259. return kvm;
  260. }
  261. static int kvm_dev_open(struct inode *inode, struct file *filp)
  262. {
  263. return 0;
  264. }
  265. /*
  266. * Free any memory in @free but not in @dont.
  267. */
  268. static void kvm_free_physmem_slot(struct kvm_memory_slot *free,
  269. struct kvm_memory_slot *dont)
  270. {
  271. int i;
  272. if (!dont || free->phys_mem != dont->phys_mem)
  273. if (free->phys_mem) {
  274. for (i = 0; i < free->npages; ++i)
  275. if (free->phys_mem[i])
  276. __free_page(free->phys_mem[i]);
  277. vfree(free->phys_mem);
  278. }
  279. if (!dont || free->dirty_bitmap != dont->dirty_bitmap)
  280. vfree(free->dirty_bitmap);
  281. free->phys_mem = NULL;
  282. free->npages = 0;
  283. free->dirty_bitmap = NULL;
  284. }
  285. static void kvm_free_physmem(struct kvm *kvm)
  286. {
  287. int i;
  288. for (i = 0; i < kvm->nmemslots; ++i)
  289. kvm_free_physmem_slot(&kvm->memslots[i], NULL);
  290. }
  291. static void kvm_free_vcpu(struct kvm_vcpu *vcpu)
  292. {
  293. if (!vcpu->vmcs)
  294. return;
  295. vcpu_load(vcpu);
  296. kvm_mmu_destroy(vcpu);
  297. vcpu_put(vcpu);
  298. kvm_arch_ops->vcpu_free(vcpu);
  299. }
  300. static void kvm_free_vcpus(struct kvm *kvm)
  301. {
  302. unsigned int i;
  303. for (i = 0; i < KVM_MAX_VCPUS; ++i)
  304. kvm_free_vcpu(&kvm->vcpus[i]);
  305. }
  306. static int kvm_dev_release(struct inode *inode, struct file *filp)
  307. {
  308. return 0;
  309. }
  310. static void kvm_destroy_vm(struct kvm *kvm)
  311. {
  312. spin_lock(&kvm_lock);
  313. list_del(&kvm->vm_list);
  314. spin_unlock(&kvm_lock);
  315. kvm_free_vcpus(kvm);
  316. kvm_free_physmem(kvm);
  317. kfree(kvm);
  318. }
  319. static int kvm_vm_release(struct inode *inode, struct file *filp)
  320. {
  321. struct kvm *kvm = filp->private_data;
  322. kvm_destroy_vm(kvm);
  323. return 0;
  324. }
  325. static void inject_gp(struct kvm_vcpu *vcpu)
  326. {
  327. kvm_arch_ops->inject_gp(vcpu, 0);
  328. }
  329. /*
  330. * Load the pae pdptrs. Return true is they are all valid.
  331. */
  332. static int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)
  333. {
  334. gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
  335. unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
  336. int i;
  337. u64 pdpte;
  338. u64 *pdpt;
  339. int ret;
  340. struct kvm_memory_slot *memslot;
  341. spin_lock(&vcpu->kvm->lock);
  342. memslot = gfn_to_memslot(vcpu->kvm, pdpt_gfn);
  343. /* FIXME: !memslot - emulate? 0xff? */
  344. pdpt = kmap_atomic(gfn_to_page(memslot, pdpt_gfn), KM_USER0);
  345. ret = 1;
  346. for (i = 0; i < 4; ++i) {
  347. pdpte = pdpt[offset + i];
  348. if ((pdpte & 1) && (pdpte & 0xfffffff0000001e6ull)) {
  349. ret = 0;
  350. goto out;
  351. }
  352. }
  353. for (i = 0; i < 4; ++i)
  354. vcpu->pdptrs[i] = pdpt[offset + i];
  355. out:
  356. kunmap_atomic(pdpt, KM_USER0);
  357. spin_unlock(&vcpu->kvm->lock);
  358. return ret;
  359. }
  360. void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  361. {
  362. if (cr0 & CR0_RESEVED_BITS) {
  363. printk(KERN_DEBUG "set_cr0: 0x%lx #GP, reserved bits 0x%lx\n",
  364. cr0, vcpu->cr0);
  365. inject_gp(vcpu);
  366. return;
  367. }
  368. if ((cr0 & CR0_NW_MASK) && !(cr0 & CR0_CD_MASK)) {
  369. printk(KERN_DEBUG "set_cr0: #GP, CD == 0 && NW == 1\n");
  370. inject_gp(vcpu);
  371. return;
  372. }
  373. if ((cr0 & CR0_PG_MASK) && !(cr0 & CR0_PE_MASK)) {
  374. printk(KERN_DEBUG "set_cr0: #GP, set PG flag "
  375. "and a clear PE flag\n");
  376. inject_gp(vcpu);
  377. return;
  378. }
  379. if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK)) {
  380. #ifdef CONFIG_X86_64
  381. if ((vcpu->shadow_efer & EFER_LME)) {
  382. int cs_db, cs_l;
  383. if (!is_pae(vcpu)) {
  384. printk(KERN_DEBUG "set_cr0: #GP, start paging "
  385. "in long mode while PAE is disabled\n");
  386. inject_gp(vcpu);
  387. return;
  388. }
  389. kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  390. if (cs_l) {
  391. printk(KERN_DEBUG "set_cr0: #GP, start paging "
  392. "in long mode while CS.L == 1\n");
  393. inject_gp(vcpu);
  394. return;
  395. }
  396. } else
  397. #endif
  398. if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->cr3)) {
  399. printk(KERN_DEBUG "set_cr0: #GP, pdptrs "
  400. "reserved bits\n");
  401. inject_gp(vcpu);
  402. return;
  403. }
  404. }
  405. kvm_arch_ops->set_cr0(vcpu, cr0);
  406. vcpu->cr0 = cr0;
  407. spin_lock(&vcpu->kvm->lock);
  408. kvm_mmu_reset_context(vcpu);
  409. spin_unlock(&vcpu->kvm->lock);
  410. return;
  411. }
  412. EXPORT_SYMBOL_GPL(set_cr0);
  413. void lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
  414. {
  415. kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
  416. set_cr0(vcpu, (vcpu->cr0 & ~0x0ful) | (msw & 0x0f));
  417. }
  418. EXPORT_SYMBOL_GPL(lmsw);
  419. void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
  420. {
  421. if (cr4 & CR4_RESEVED_BITS) {
  422. printk(KERN_DEBUG "set_cr4: #GP, reserved bits\n");
  423. inject_gp(vcpu);
  424. return;
  425. }
  426. if (is_long_mode(vcpu)) {
  427. if (!(cr4 & CR4_PAE_MASK)) {
  428. printk(KERN_DEBUG "set_cr4: #GP, clearing PAE while "
  429. "in long mode\n");
  430. inject_gp(vcpu);
  431. return;
  432. }
  433. } else if (is_paging(vcpu) && !is_pae(vcpu) && (cr4 & CR4_PAE_MASK)
  434. && !load_pdptrs(vcpu, vcpu->cr3)) {
  435. printk(KERN_DEBUG "set_cr4: #GP, pdptrs reserved bits\n");
  436. inject_gp(vcpu);
  437. }
  438. if (cr4 & CR4_VMXE_MASK) {
  439. printk(KERN_DEBUG "set_cr4: #GP, setting VMXE\n");
  440. inject_gp(vcpu);
  441. return;
  442. }
  443. kvm_arch_ops->set_cr4(vcpu, cr4);
  444. spin_lock(&vcpu->kvm->lock);
  445. kvm_mmu_reset_context(vcpu);
  446. spin_unlock(&vcpu->kvm->lock);
  447. }
  448. EXPORT_SYMBOL_GPL(set_cr4);
  449. void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
  450. {
  451. if (is_long_mode(vcpu)) {
  452. if (cr3 & CR3_L_MODE_RESEVED_BITS) {
  453. printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
  454. inject_gp(vcpu);
  455. return;
  456. }
  457. } else {
  458. if (cr3 & CR3_RESEVED_BITS) {
  459. printk(KERN_DEBUG "set_cr3: #GP, reserved bits\n");
  460. inject_gp(vcpu);
  461. return;
  462. }
  463. if (is_paging(vcpu) && is_pae(vcpu) &&
  464. !load_pdptrs(vcpu, cr3)) {
  465. printk(KERN_DEBUG "set_cr3: #GP, pdptrs "
  466. "reserved bits\n");
  467. inject_gp(vcpu);
  468. return;
  469. }
  470. }
  471. vcpu->cr3 = cr3;
  472. spin_lock(&vcpu->kvm->lock);
  473. /*
  474. * Does the new cr3 value map to physical memory? (Note, we
  475. * catch an invalid cr3 even in real-mode, because it would
  476. * cause trouble later on when we turn on paging anyway.)
  477. *
  478. * A real CPU would silently accept an invalid cr3 and would
  479. * attempt to use it - with largely undefined (and often hard
  480. * to debug) behavior on the guest side.
  481. */
  482. if (unlikely(!gfn_to_memslot(vcpu->kvm, cr3 >> PAGE_SHIFT)))
  483. inject_gp(vcpu);
  484. else
  485. vcpu->mmu.new_cr3(vcpu);
  486. spin_unlock(&vcpu->kvm->lock);
  487. }
  488. EXPORT_SYMBOL_GPL(set_cr3);
  489. void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
  490. {
  491. if ( cr8 & CR8_RESEVED_BITS) {
  492. printk(KERN_DEBUG "set_cr8: #GP, reserved bits 0x%lx\n", cr8);
  493. inject_gp(vcpu);
  494. return;
  495. }
  496. vcpu->cr8 = cr8;
  497. }
  498. EXPORT_SYMBOL_GPL(set_cr8);
  499. void fx_init(struct kvm_vcpu *vcpu)
  500. {
  501. struct __attribute__ ((__packed__)) fx_image_s {
  502. u16 control; //fcw
  503. u16 status; //fsw
  504. u16 tag; // ftw
  505. u16 opcode; //fop
  506. u64 ip; // fpu ip
  507. u64 operand;// fpu dp
  508. u32 mxcsr;
  509. u32 mxcsr_mask;
  510. } *fx_image;
  511. fx_save(vcpu->host_fx_image);
  512. fpu_init();
  513. fx_save(vcpu->guest_fx_image);
  514. fx_restore(vcpu->host_fx_image);
  515. fx_image = (struct fx_image_s *)vcpu->guest_fx_image;
  516. fx_image->mxcsr = 0x1f80;
  517. memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
  518. 0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
  519. }
  520. EXPORT_SYMBOL_GPL(fx_init);
  521. /*
  522. * Allocate some memory and give it an address in the guest physical address
  523. * space.
  524. *
  525. * Discontiguous memory is allowed, mostly for framebuffers.
  526. */
  527. static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
  528. struct kvm_memory_region *mem)
  529. {
  530. int r;
  531. gfn_t base_gfn;
  532. unsigned long npages;
  533. unsigned long i;
  534. struct kvm_memory_slot *memslot;
  535. struct kvm_memory_slot old, new;
  536. int memory_config_version;
  537. r = -EINVAL;
  538. /* General sanity checks */
  539. if (mem->memory_size & (PAGE_SIZE - 1))
  540. goto out;
  541. if (mem->guest_phys_addr & (PAGE_SIZE - 1))
  542. goto out;
  543. if (mem->slot >= KVM_MEMORY_SLOTS)
  544. goto out;
  545. if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
  546. goto out;
  547. memslot = &kvm->memslots[mem->slot];
  548. base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
  549. npages = mem->memory_size >> PAGE_SHIFT;
  550. if (!npages)
  551. mem->flags &= ~KVM_MEM_LOG_DIRTY_PAGES;
  552. raced:
  553. spin_lock(&kvm->lock);
  554. memory_config_version = kvm->memory_config_version;
  555. new = old = *memslot;
  556. new.base_gfn = base_gfn;
  557. new.npages = npages;
  558. new.flags = mem->flags;
  559. /* Disallow changing a memory slot's size. */
  560. r = -EINVAL;
  561. if (npages && old.npages && npages != old.npages)
  562. goto out_unlock;
  563. /* Check for overlaps */
  564. r = -EEXIST;
  565. for (i = 0; i < KVM_MEMORY_SLOTS; ++i) {
  566. struct kvm_memory_slot *s = &kvm->memslots[i];
  567. if (s == memslot)
  568. continue;
  569. if (!((base_gfn + npages <= s->base_gfn) ||
  570. (base_gfn >= s->base_gfn + s->npages)))
  571. goto out_unlock;
  572. }
  573. /*
  574. * Do memory allocations outside lock. memory_config_version will
  575. * detect any races.
  576. */
  577. spin_unlock(&kvm->lock);
  578. /* Deallocate if slot is being removed */
  579. if (!npages)
  580. new.phys_mem = NULL;
  581. /* Free page dirty bitmap if unneeded */
  582. if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
  583. new.dirty_bitmap = NULL;
  584. r = -ENOMEM;
  585. /* Allocate if a slot is being created */
  586. if (npages && !new.phys_mem) {
  587. new.phys_mem = vmalloc(npages * sizeof(struct page *));
  588. if (!new.phys_mem)
  589. goto out_free;
  590. memset(new.phys_mem, 0, npages * sizeof(struct page *));
  591. for (i = 0; i < npages; ++i) {
  592. new.phys_mem[i] = alloc_page(GFP_HIGHUSER
  593. | __GFP_ZERO);
  594. if (!new.phys_mem[i])
  595. goto out_free;
  596. set_page_private(new.phys_mem[i],0);
  597. }
  598. }
  599. /* Allocate page dirty bitmap if needed */
  600. if ((new.flags & KVM_MEM_LOG_DIRTY_PAGES) && !new.dirty_bitmap) {
  601. unsigned dirty_bytes = ALIGN(npages, BITS_PER_LONG) / 8;
  602. new.dirty_bitmap = vmalloc(dirty_bytes);
  603. if (!new.dirty_bitmap)
  604. goto out_free;
  605. memset(new.dirty_bitmap, 0, dirty_bytes);
  606. }
  607. spin_lock(&kvm->lock);
  608. if (memory_config_version != kvm->memory_config_version) {
  609. spin_unlock(&kvm->lock);
  610. kvm_free_physmem_slot(&new, &old);
  611. goto raced;
  612. }
  613. r = -EAGAIN;
  614. if (kvm->busy)
  615. goto out_unlock;
  616. if (mem->slot >= kvm->nmemslots)
  617. kvm->nmemslots = mem->slot + 1;
  618. *memslot = new;
  619. ++kvm->memory_config_version;
  620. spin_unlock(&kvm->lock);
  621. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  622. struct kvm_vcpu *vcpu;
  623. vcpu = vcpu_load_slot(kvm, i);
  624. if (!vcpu)
  625. continue;
  626. kvm_mmu_reset_context(vcpu);
  627. vcpu_put(vcpu);
  628. }
  629. kvm_free_physmem_slot(&old, &new);
  630. return 0;
  631. out_unlock:
  632. spin_unlock(&kvm->lock);
  633. out_free:
  634. kvm_free_physmem_slot(&new, &old);
  635. out:
  636. return r;
  637. }
  638. static void do_remove_write_access(struct kvm_vcpu *vcpu, int slot)
  639. {
  640. spin_lock(&vcpu->kvm->lock);
  641. kvm_mmu_slot_remove_write_access(vcpu, slot);
  642. spin_unlock(&vcpu->kvm->lock);
  643. }
  644. /*
  645. * Get (and clear) the dirty memory log for a memory slot.
  646. */
  647. static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
  648. struct kvm_dirty_log *log)
  649. {
  650. struct kvm_memory_slot *memslot;
  651. int r, i;
  652. int n;
  653. int cleared;
  654. unsigned long any = 0;
  655. spin_lock(&kvm->lock);
  656. /*
  657. * Prevent changes to guest memory configuration even while the lock
  658. * is not taken.
  659. */
  660. ++kvm->busy;
  661. spin_unlock(&kvm->lock);
  662. r = -EINVAL;
  663. if (log->slot >= KVM_MEMORY_SLOTS)
  664. goto out;
  665. memslot = &kvm->memslots[log->slot];
  666. r = -ENOENT;
  667. if (!memslot->dirty_bitmap)
  668. goto out;
  669. n = ALIGN(memslot->npages, 8) / 8;
  670. for (i = 0; !any && i < n; ++i)
  671. any = memslot->dirty_bitmap[i];
  672. r = -EFAULT;
  673. if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
  674. goto out;
  675. if (any) {
  676. cleared = 0;
  677. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  678. struct kvm_vcpu *vcpu;
  679. vcpu = vcpu_load_slot(kvm, i);
  680. if (!vcpu)
  681. continue;
  682. if (!cleared) {
  683. do_remove_write_access(vcpu, log->slot);
  684. memset(memslot->dirty_bitmap, 0, n);
  685. cleared = 1;
  686. }
  687. kvm_arch_ops->tlb_flush(vcpu);
  688. vcpu_put(vcpu);
  689. }
  690. }
  691. r = 0;
  692. out:
  693. spin_lock(&kvm->lock);
  694. --kvm->busy;
  695. spin_unlock(&kvm->lock);
  696. return r;
  697. }
  698. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
  699. {
  700. int i;
  701. for (i = 0; i < kvm->nmemslots; ++i) {
  702. struct kvm_memory_slot *memslot = &kvm->memslots[i];
  703. if (gfn >= memslot->base_gfn
  704. && gfn < memslot->base_gfn + memslot->npages)
  705. return memslot;
  706. }
  707. return NULL;
  708. }
  709. EXPORT_SYMBOL_GPL(gfn_to_memslot);
  710. void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
  711. {
  712. int i;
  713. struct kvm_memory_slot *memslot = NULL;
  714. unsigned long rel_gfn;
  715. for (i = 0; i < kvm->nmemslots; ++i) {
  716. memslot = &kvm->memslots[i];
  717. if (gfn >= memslot->base_gfn
  718. && gfn < memslot->base_gfn + memslot->npages) {
  719. if (!memslot || !memslot->dirty_bitmap)
  720. return;
  721. rel_gfn = gfn - memslot->base_gfn;
  722. /* avoid RMW */
  723. if (!test_bit(rel_gfn, memslot->dirty_bitmap))
  724. set_bit(rel_gfn, memslot->dirty_bitmap);
  725. return;
  726. }
  727. }
  728. }
  729. static int emulator_read_std(unsigned long addr,
  730. unsigned long *val,
  731. unsigned int bytes,
  732. struct x86_emulate_ctxt *ctxt)
  733. {
  734. struct kvm_vcpu *vcpu = ctxt->vcpu;
  735. void *data = val;
  736. while (bytes) {
  737. gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
  738. unsigned offset = addr & (PAGE_SIZE-1);
  739. unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
  740. unsigned long pfn;
  741. struct kvm_memory_slot *memslot;
  742. void *page;
  743. if (gpa == UNMAPPED_GVA)
  744. return X86EMUL_PROPAGATE_FAULT;
  745. pfn = gpa >> PAGE_SHIFT;
  746. memslot = gfn_to_memslot(vcpu->kvm, pfn);
  747. if (!memslot)
  748. return X86EMUL_UNHANDLEABLE;
  749. page = kmap_atomic(gfn_to_page(memslot, pfn), KM_USER0);
  750. memcpy(data, page + offset, tocopy);
  751. kunmap_atomic(page, KM_USER0);
  752. bytes -= tocopy;
  753. data += tocopy;
  754. addr += tocopy;
  755. }
  756. return X86EMUL_CONTINUE;
  757. }
  758. static int emulator_write_std(unsigned long addr,
  759. unsigned long val,
  760. unsigned int bytes,
  761. struct x86_emulate_ctxt *ctxt)
  762. {
  763. printk(KERN_ERR "emulator_write_std: addr %lx n %d\n",
  764. addr, bytes);
  765. return X86EMUL_UNHANDLEABLE;
  766. }
  767. static int emulator_read_emulated(unsigned long addr,
  768. unsigned long *val,
  769. unsigned int bytes,
  770. struct x86_emulate_ctxt *ctxt)
  771. {
  772. struct kvm_vcpu *vcpu = ctxt->vcpu;
  773. if (vcpu->mmio_read_completed) {
  774. memcpy(val, vcpu->mmio_data, bytes);
  775. vcpu->mmio_read_completed = 0;
  776. return X86EMUL_CONTINUE;
  777. } else if (emulator_read_std(addr, val, bytes, ctxt)
  778. == X86EMUL_CONTINUE)
  779. return X86EMUL_CONTINUE;
  780. else {
  781. gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
  782. if (gpa == UNMAPPED_GVA)
  783. return X86EMUL_PROPAGATE_FAULT;
  784. vcpu->mmio_needed = 1;
  785. vcpu->mmio_phys_addr = gpa;
  786. vcpu->mmio_size = bytes;
  787. vcpu->mmio_is_write = 0;
  788. return X86EMUL_UNHANDLEABLE;
  789. }
  790. }
  791. static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
  792. unsigned long val, int bytes)
  793. {
  794. struct kvm_memory_slot *m;
  795. struct page *page;
  796. void *virt;
  797. if (((gpa + bytes - 1) >> PAGE_SHIFT) != (gpa >> PAGE_SHIFT))
  798. return 0;
  799. m = gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT);
  800. if (!m)
  801. return 0;
  802. page = gfn_to_page(m, gpa >> PAGE_SHIFT);
  803. kvm_mmu_pre_write(vcpu, gpa, bytes);
  804. virt = kmap_atomic(page, KM_USER0);
  805. memcpy(virt + offset_in_page(gpa), &val, bytes);
  806. kunmap_atomic(virt, KM_USER0);
  807. kvm_mmu_post_write(vcpu, gpa, bytes);
  808. return 1;
  809. }
  810. static int emulator_write_emulated(unsigned long addr,
  811. unsigned long val,
  812. unsigned int bytes,
  813. struct x86_emulate_ctxt *ctxt)
  814. {
  815. struct kvm_vcpu *vcpu = ctxt->vcpu;
  816. gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
  817. if (gpa == UNMAPPED_GVA)
  818. return X86EMUL_PROPAGATE_FAULT;
  819. if (emulator_write_phys(vcpu, gpa, val, bytes))
  820. return X86EMUL_CONTINUE;
  821. vcpu->mmio_needed = 1;
  822. vcpu->mmio_phys_addr = gpa;
  823. vcpu->mmio_size = bytes;
  824. vcpu->mmio_is_write = 1;
  825. memcpy(vcpu->mmio_data, &val, bytes);
  826. return X86EMUL_CONTINUE;
  827. }
  828. static int emulator_cmpxchg_emulated(unsigned long addr,
  829. unsigned long old,
  830. unsigned long new,
  831. unsigned int bytes,
  832. struct x86_emulate_ctxt *ctxt)
  833. {
  834. static int reported;
  835. if (!reported) {
  836. reported = 1;
  837. printk(KERN_WARNING "kvm: emulating exchange as write\n");
  838. }
  839. return emulator_write_emulated(addr, new, bytes, ctxt);
  840. }
  841. #ifdef CONFIG_X86_32
  842. static int emulator_cmpxchg8b_emulated(unsigned long addr,
  843. unsigned long old_lo,
  844. unsigned long old_hi,
  845. unsigned long new_lo,
  846. unsigned long new_hi,
  847. struct x86_emulate_ctxt *ctxt)
  848. {
  849. static int reported;
  850. int r;
  851. if (!reported) {
  852. reported = 1;
  853. printk(KERN_WARNING "kvm: emulating exchange8b as write\n");
  854. }
  855. r = emulator_write_emulated(addr, new_lo, 4, ctxt);
  856. if (r != X86EMUL_CONTINUE)
  857. return r;
  858. return emulator_write_emulated(addr+4, new_hi, 4, ctxt);
  859. }
  860. #endif
  861. static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
  862. {
  863. return kvm_arch_ops->get_segment_base(vcpu, seg);
  864. }
  865. int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
  866. {
  867. return X86EMUL_CONTINUE;
  868. }
  869. int emulate_clts(struct kvm_vcpu *vcpu)
  870. {
  871. unsigned long cr0;
  872. kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
  873. cr0 = vcpu->cr0 & ~CR0_TS_MASK;
  874. kvm_arch_ops->set_cr0(vcpu, cr0);
  875. return X86EMUL_CONTINUE;
  876. }
  877. int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr, unsigned long *dest)
  878. {
  879. struct kvm_vcpu *vcpu = ctxt->vcpu;
  880. switch (dr) {
  881. case 0 ... 3:
  882. *dest = kvm_arch_ops->get_dr(vcpu, dr);
  883. return X86EMUL_CONTINUE;
  884. default:
  885. printk(KERN_DEBUG "%s: unexpected dr %u\n",
  886. __FUNCTION__, dr);
  887. return X86EMUL_UNHANDLEABLE;
  888. }
  889. }
  890. int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
  891. {
  892. unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
  893. int exception;
  894. kvm_arch_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
  895. if (exception) {
  896. /* FIXME: better handling */
  897. return X86EMUL_UNHANDLEABLE;
  898. }
  899. return X86EMUL_CONTINUE;
  900. }
  901. static void report_emulation_failure(struct x86_emulate_ctxt *ctxt)
  902. {
  903. static int reported;
  904. u8 opcodes[4];
  905. unsigned long rip = ctxt->vcpu->rip;
  906. unsigned long rip_linear;
  907. rip_linear = rip + get_segment_base(ctxt->vcpu, VCPU_SREG_CS);
  908. if (reported)
  909. return;
  910. emulator_read_std(rip_linear, (void *)opcodes, 4, ctxt);
  911. printk(KERN_ERR "emulation failed but !mmio_needed?"
  912. " rip %lx %02x %02x %02x %02x\n",
  913. rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
  914. reported = 1;
  915. }
  916. struct x86_emulate_ops emulate_ops = {
  917. .read_std = emulator_read_std,
  918. .write_std = emulator_write_std,
  919. .read_emulated = emulator_read_emulated,
  920. .write_emulated = emulator_write_emulated,
  921. .cmpxchg_emulated = emulator_cmpxchg_emulated,
  922. #ifdef CONFIG_X86_32
  923. .cmpxchg8b_emulated = emulator_cmpxchg8b_emulated,
  924. #endif
  925. };
  926. int emulate_instruction(struct kvm_vcpu *vcpu,
  927. struct kvm_run *run,
  928. unsigned long cr2,
  929. u16 error_code)
  930. {
  931. struct x86_emulate_ctxt emulate_ctxt;
  932. int r;
  933. int cs_db, cs_l;
  934. kvm_arch_ops->cache_regs(vcpu);
  935. kvm_arch_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
  936. emulate_ctxt.vcpu = vcpu;
  937. emulate_ctxt.eflags = kvm_arch_ops->get_rflags(vcpu);
  938. emulate_ctxt.cr2 = cr2;
  939. emulate_ctxt.mode = (emulate_ctxt.eflags & X86_EFLAGS_VM)
  940. ? X86EMUL_MODE_REAL : cs_l
  941. ? X86EMUL_MODE_PROT64 : cs_db
  942. ? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
  943. if (emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
  944. emulate_ctxt.cs_base = 0;
  945. emulate_ctxt.ds_base = 0;
  946. emulate_ctxt.es_base = 0;
  947. emulate_ctxt.ss_base = 0;
  948. } else {
  949. emulate_ctxt.cs_base = get_segment_base(vcpu, VCPU_SREG_CS);
  950. emulate_ctxt.ds_base = get_segment_base(vcpu, VCPU_SREG_DS);
  951. emulate_ctxt.es_base = get_segment_base(vcpu, VCPU_SREG_ES);
  952. emulate_ctxt.ss_base = get_segment_base(vcpu, VCPU_SREG_SS);
  953. }
  954. emulate_ctxt.gs_base = get_segment_base(vcpu, VCPU_SREG_GS);
  955. emulate_ctxt.fs_base = get_segment_base(vcpu, VCPU_SREG_FS);
  956. vcpu->mmio_is_write = 0;
  957. r = x86_emulate_memop(&emulate_ctxt, &emulate_ops);
  958. if ((r || vcpu->mmio_is_write) && run) {
  959. run->mmio.phys_addr = vcpu->mmio_phys_addr;
  960. memcpy(run->mmio.data, vcpu->mmio_data, 8);
  961. run->mmio.len = vcpu->mmio_size;
  962. run->mmio.is_write = vcpu->mmio_is_write;
  963. }
  964. if (r) {
  965. if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
  966. return EMULATE_DONE;
  967. if (!vcpu->mmio_needed) {
  968. report_emulation_failure(&emulate_ctxt);
  969. return EMULATE_FAIL;
  970. }
  971. return EMULATE_DO_MMIO;
  972. }
  973. kvm_arch_ops->decache_regs(vcpu);
  974. kvm_arch_ops->set_rflags(vcpu, emulate_ctxt.eflags);
  975. if (vcpu->mmio_is_write)
  976. return EMULATE_DO_MMIO;
  977. return EMULATE_DONE;
  978. }
  979. EXPORT_SYMBOL_GPL(emulate_instruction);
  980. int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run)
  981. {
  982. unsigned long nr, a0, a1, a2, a3, a4, a5, ret;
  983. kvm_arch_ops->decache_regs(vcpu);
  984. ret = -KVM_EINVAL;
  985. #ifdef CONFIG_X86_64
  986. if (is_long_mode(vcpu)) {
  987. nr = vcpu->regs[VCPU_REGS_RAX];
  988. a0 = vcpu->regs[VCPU_REGS_RDI];
  989. a1 = vcpu->regs[VCPU_REGS_RSI];
  990. a2 = vcpu->regs[VCPU_REGS_RDX];
  991. a3 = vcpu->regs[VCPU_REGS_RCX];
  992. a4 = vcpu->regs[VCPU_REGS_R8];
  993. a5 = vcpu->regs[VCPU_REGS_R9];
  994. } else
  995. #endif
  996. {
  997. nr = vcpu->regs[VCPU_REGS_RBX] & -1u;
  998. a0 = vcpu->regs[VCPU_REGS_RAX] & -1u;
  999. a1 = vcpu->regs[VCPU_REGS_RCX] & -1u;
  1000. a2 = vcpu->regs[VCPU_REGS_RDX] & -1u;
  1001. a3 = vcpu->regs[VCPU_REGS_RSI] & -1u;
  1002. a4 = vcpu->regs[VCPU_REGS_RDI] & -1u;
  1003. a5 = vcpu->regs[VCPU_REGS_RBP] & -1u;
  1004. }
  1005. switch (nr) {
  1006. default:
  1007. ;
  1008. }
  1009. vcpu->regs[VCPU_REGS_RAX] = ret;
  1010. kvm_arch_ops->cache_regs(vcpu);
  1011. return 1;
  1012. }
  1013. EXPORT_SYMBOL_GPL(kvm_hypercall);
  1014. static u64 mk_cr_64(u64 curr_cr, u32 new_val)
  1015. {
  1016. return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
  1017. }
  1018. void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  1019. {
  1020. struct descriptor_table dt = { limit, base };
  1021. kvm_arch_ops->set_gdt(vcpu, &dt);
  1022. }
  1023. void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  1024. {
  1025. struct descriptor_table dt = { limit, base };
  1026. kvm_arch_ops->set_idt(vcpu, &dt);
  1027. }
  1028. void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
  1029. unsigned long *rflags)
  1030. {
  1031. lmsw(vcpu, msw);
  1032. *rflags = kvm_arch_ops->get_rflags(vcpu);
  1033. }
  1034. unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
  1035. {
  1036. kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
  1037. switch (cr) {
  1038. case 0:
  1039. return vcpu->cr0;
  1040. case 2:
  1041. return vcpu->cr2;
  1042. case 3:
  1043. return vcpu->cr3;
  1044. case 4:
  1045. return vcpu->cr4;
  1046. default:
  1047. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
  1048. return 0;
  1049. }
  1050. }
  1051. void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
  1052. unsigned long *rflags)
  1053. {
  1054. switch (cr) {
  1055. case 0:
  1056. set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
  1057. *rflags = kvm_arch_ops->get_rflags(vcpu);
  1058. break;
  1059. case 2:
  1060. vcpu->cr2 = val;
  1061. break;
  1062. case 3:
  1063. set_cr3(vcpu, val);
  1064. break;
  1065. case 4:
  1066. set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
  1067. break;
  1068. default:
  1069. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
  1070. }
  1071. }
  1072. /*
  1073. * Register the para guest with the host:
  1074. */
  1075. static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
  1076. {
  1077. struct kvm_vcpu_para_state *para_state;
  1078. hpa_t para_state_hpa, hypercall_hpa;
  1079. struct page *para_state_page;
  1080. unsigned char *hypercall;
  1081. gpa_t hypercall_gpa;
  1082. printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
  1083. printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
  1084. /*
  1085. * Needs to be page aligned:
  1086. */
  1087. if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
  1088. goto err_gp;
  1089. para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
  1090. printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
  1091. if (is_error_hpa(para_state_hpa))
  1092. goto err_gp;
  1093. para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
  1094. para_state = kmap_atomic(para_state_page, KM_USER0);
  1095. printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
  1096. printk(KERN_DEBUG ".... size: %d\n", para_state->size);
  1097. para_state->host_version = KVM_PARA_API_VERSION;
  1098. /*
  1099. * We cannot support guests that try to register themselves
  1100. * with a newer API version than the host supports:
  1101. */
  1102. if (para_state->guest_version > KVM_PARA_API_VERSION) {
  1103. para_state->ret = -KVM_EINVAL;
  1104. goto err_kunmap_skip;
  1105. }
  1106. hypercall_gpa = para_state->hypercall_gpa;
  1107. hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
  1108. printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
  1109. if (is_error_hpa(hypercall_hpa)) {
  1110. para_state->ret = -KVM_EINVAL;
  1111. goto err_kunmap_skip;
  1112. }
  1113. printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
  1114. vcpu->para_state_page = para_state_page;
  1115. vcpu->para_state_gpa = para_state_gpa;
  1116. vcpu->hypercall_gpa = hypercall_gpa;
  1117. hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
  1118. KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
  1119. kvm_arch_ops->patch_hypercall(vcpu, hypercall);
  1120. kunmap_atomic(hypercall, KM_USER1);
  1121. para_state->ret = 0;
  1122. err_kunmap_skip:
  1123. kunmap_atomic(para_state, KM_USER0);
  1124. return 0;
  1125. err_gp:
  1126. return 1;
  1127. }
  1128. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1129. {
  1130. u64 data;
  1131. switch (msr) {
  1132. case 0xc0010010: /* SYSCFG */
  1133. case 0xc0010015: /* HWCR */
  1134. case MSR_IA32_PLATFORM_ID:
  1135. case MSR_IA32_P5_MC_ADDR:
  1136. case MSR_IA32_P5_MC_TYPE:
  1137. case MSR_IA32_MC0_CTL:
  1138. case MSR_IA32_MCG_STATUS:
  1139. case MSR_IA32_MCG_CAP:
  1140. case MSR_IA32_MC0_MISC:
  1141. case MSR_IA32_MC0_MISC+4:
  1142. case MSR_IA32_MC0_MISC+8:
  1143. case MSR_IA32_MC0_MISC+12:
  1144. case MSR_IA32_MC0_MISC+16:
  1145. case MSR_IA32_UCODE_REV:
  1146. case MSR_IA32_PERF_STATUS:
  1147. /* MTRR registers */
  1148. case 0xfe:
  1149. case 0x200 ... 0x2ff:
  1150. data = 0;
  1151. break;
  1152. case 0xcd: /* fsb frequency */
  1153. data = 3;
  1154. break;
  1155. case MSR_IA32_APICBASE:
  1156. data = vcpu->apic_base;
  1157. break;
  1158. case MSR_IA32_MISC_ENABLE:
  1159. data = vcpu->ia32_misc_enable_msr;
  1160. break;
  1161. #ifdef CONFIG_X86_64
  1162. case MSR_EFER:
  1163. data = vcpu->shadow_efer;
  1164. break;
  1165. #endif
  1166. default:
  1167. printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
  1168. return 1;
  1169. }
  1170. *pdata = data;
  1171. return 0;
  1172. }
  1173. EXPORT_SYMBOL_GPL(kvm_get_msr_common);
  1174. /*
  1175. * Reads an msr value (of 'msr_index') into 'pdata'.
  1176. * Returns 0 on success, non-0 otherwise.
  1177. * Assumes vcpu_load() was already called.
  1178. */
  1179. static int get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  1180. {
  1181. return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
  1182. }
  1183. #ifdef CONFIG_X86_64
  1184. static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
  1185. {
  1186. if (efer & EFER_RESERVED_BITS) {
  1187. printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
  1188. efer);
  1189. inject_gp(vcpu);
  1190. return;
  1191. }
  1192. if (is_paging(vcpu)
  1193. && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
  1194. printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
  1195. inject_gp(vcpu);
  1196. return;
  1197. }
  1198. kvm_arch_ops->set_efer(vcpu, efer);
  1199. efer &= ~EFER_LMA;
  1200. efer |= vcpu->shadow_efer & EFER_LMA;
  1201. vcpu->shadow_efer = efer;
  1202. }
  1203. #endif
  1204. int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  1205. {
  1206. switch (msr) {
  1207. #ifdef CONFIG_X86_64
  1208. case MSR_EFER:
  1209. set_efer(vcpu, data);
  1210. break;
  1211. #endif
  1212. case MSR_IA32_MC0_STATUS:
  1213. printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
  1214. __FUNCTION__, data);
  1215. break;
  1216. case MSR_IA32_UCODE_REV:
  1217. case MSR_IA32_UCODE_WRITE:
  1218. case 0x200 ... 0x2ff: /* MTRRs */
  1219. break;
  1220. case MSR_IA32_APICBASE:
  1221. vcpu->apic_base = data;
  1222. break;
  1223. case MSR_IA32_MISC_ENABLE:
  1224. vcpu->ia32_misc_enable_msr = data;
  1225. break;
  1226. /*
  1227. * This is the 'probe whether the host is KVM' logic:
  1228. */
  1229. case MSR_KVM_API_MAGIC:
  1230. return vcpu_register_para(vcpu, data);
  1231. default:
  1232. printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
  1233. return 1;
  1234. }
  1235. return 0;
  1236. }
  1237. EXPORT_SYMBOL_GPL(kvm_set_msr_common);
  1238. /*
  1239. * Writes msr value into into the appropriate "register".
  1240. * Returns 0 on success, non-0 otherwise.
  1241. * Assumes vcpu_load() was already called.
  1242. */
  1243. static int set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  1244. {
  1245. return kvm_arch_ops->set_msr(vcpu, msr_index, data);
  1246. }
  1247. void kvm_resched(struct kvm_vcpu *vcpu)
  1248. {
  1249. vcpu_put(vcpu);
  1250. cond_resched();
  1251. vcpu_load(vcpu);
  1252. }
  1253. EXPORT_SYMBOL_GPL(kvm_resched);
  1254. void load_msrs(struct vmx_msr_entry *e, int n)
  1255. {
  1256. int i;
  1257. for (i = 0; i < n; ++i)
  1258. wrmsrl(e[i].index, e[i].data);
  1259. }
  1260. EXPORT_SYMBOL_GPL(load_msrs);
  1261. void save_msrs(struct vmx_msr_entry *e, int n)
  1262. {
  1263. int i;
  1264. for (i = 0; i < n; ++i)
  1265. rdmsrl(e[i].index, e[i].data);
  1266. }
  1267. EXPORT_SYMBOL_GPL(save_msrs);
  1268. static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  1269. {
  1270. int r;
  1271. vcpu_load(vcpu);
  1272. /* re-sync apic's tpr */
  1273. vcpu->cr8 = kvm_run->cr8;
  1274. if (kvm_run->emulated) {
  1275. kvm_arch_ops->skip_emulated_instruction(vcpu);
  1276. kvm_run->emulated = 0;
  1277. }
  1278. if (kvm_run->mmio_completed) {
  1279. memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
  1280. vcpu->mmio_read_completed = 1;
  1281. }
  1282. vcpu->mmio_needed = 0;
  1283. r = kvm_arch_ops->run(vcpu, kvm_run);
  1284. vcpu_put(vcpu);
  1285. return r;
  1286. }
  1287. static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
  1288. struct kvm_regs *regs)
  1289. {
  1290. vcpu_load(vcpu);
  1291. kvm_arch_ops->cache_regs(vcpu);
  1292. regs->rax = vcpu->regs[VCPU_REGS_RAX];
  1293. regs->rbx = vcpu->regs[VCPU_REGS_RBX];
  1294. regs->rcx = vcpu->regs[VCPU_REGS_RCX];
  1295. regs->rdx = vcpu->regs[VCPU_REGS_RDX];
  1296. regs->rsi = vcpu->regs[VCPU_REGS_RSI];
  1297. regs->rdi = vcpu->regs[VCPU_REGS_RDI];
  1298. regs->rsp = vcpu->regs[VCPU_REGS_RSP];
  1299. regs->rbp = vcpu->regs[VCPU_REGS_RBP];
  1300. #ifdef CONFIG_X86_64
  1301. regs->r8 = vcpu->regs[VCPU_REGS_R8];
  1302. regs->r9 = vcpu->regs[VCPU_REGS_R9];
  1303. regs->r10 = vcpu->regs[VCPU_REGS_R10];
  1304. regs->r11 = vcpu->regs[VCPU_REGS_R11];
  1305. regs->r12 = vcpu->regs[VCPU_REGS_R12];
  1306. regs->r13 = vcpu->regs[VCPU_REGS_R13];
  1307. regs->r14 = vcpu->regs[VCPU_REGS_R14];
  1308. regs->r15 = vcpu->regs[VCPU_REGS_R15];
  1309. #endif
  1310. regs->rip = vcpu->rip;
  1311. regs->rflags = kvm_arch_ops->get_rflags(vcpu);
  1312. /*
  1313. * Don't leak debug flags in case they were set for guest debugging
  1314. */
  1315. if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
  1316. regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
  1317. vcpu_put(vcpu);
  1318. return 0;
  1319. }
  1320. static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
  1321. struct kvm_regs *regs)
  1322. {
  1323. vcpu_load(vcpu);
  1324. vcpu->regs[VCPU_REGS_RAX] = regs->rax;
  1325. vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
  1326. vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
  1327. vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
  1328. vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
  1329. vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
  1330. vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
  1331. vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
  1332. #ifdef CONFIG_X86_64
  1333. vcpu->regs[VCPU_REGS_R8] = regs->r8;
  1334. vcpu->regs[VCPU_REGS_R9] = regs->r9;
  1335. vcpu->regs[VCPU_REGS_R10] = regs->r10;
  1336. vcpu->regs[VCPU_REGS_R11] = regs->r11;
  1337. vcpu->regs[VCPU_REGS_R12] = regs->r12;
  1338. vcpu->regs[VCPU_REGS_R13] = regs->r13;
  1339. vcpu->regs[VCPU_REGS_R14] = regs->r14;
  1340. vcpu->regs[VCPU_REGS_R15] = regs->r15;
  1341. #endif
  1342. vcpu->rip = regs->rip;
  1343. kvm_arch_ops->set_rflags(vcpu, regs->rflags);
  1344. kvm_arch_ops->decache_regs(vcpu);
  1345. vcpu_put(vcpu);
  1346. return 0;
  1347. }
  1348. static void get_segment(struct kvm_vcpu *vcpu,
  1349. struct kvm_segment *var, int seg)
  1350. {
  1351. return kvm_arch_ops->get_segment(vcpu, var, seg);
  1352. }
  1353. static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  1354. struct kvm_sregs *sregs)
  1355. {
  1356. struct descriptor_table dt;
  1357. vcpu_load(vcpu);
  1358. get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  1359. get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  1360. get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  1361. get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  1362. get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  1363. get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  1364. get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  1365. get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  1366. kvm_arch_ops->get_idt(vcpu, &dt);
  1367. sregs->idt.limit = dt.limit;
  1368. sregs->idt.base = dt.base;
  1369. kvm_arch_ops->get_gdt(vcpu, &dt);
  1370. sregs->gdt.limit = dt.limit;
  1371. sregs->gdt.base = dt.base;
  1372. kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
  1373. sregs->cr0 = vcpu->cr0;
  1374. sregs->cr2 = vcpu->cr2;
  1375. sregs->cr3 = vcpu->cr3;
  1376. sregs->cr4 = vcpu->cr4;
  1377. sregs->cr8 = vcpu->cr8;
  1378. sregs->efer = vcpu->shadow_efer;
  1379. sregs->apic_base = vcpu->apic_base;
  1380. memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
  1381. sizeof sregs->interrupt_bitmap);
  1382. vcpu_put(vcpu);
  1383. return 0;
  1384. }
  1385. static void set_segment(struct kvm_vcpu *vcpu,
  1386. struct kvm_segment *var, int seg)
  1387. {
  1388. return kvm_arch_ops->set_segment(vcpu, var, seg);
  1389. }
  1390. static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  1391. struct kvm_sregs *sregs)
  1392. {
  1393. int mmu_reset_needed = 0;
  1394. int i;
  1395. struct descriptor_table dt;
  1396. vcpu_load(vcpu);
  1397. set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  1398. set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  1399. set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  1400. set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  1401. set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  1402. set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  1403. set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  1404. set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  1405. dt.limit = sregs->idt.limit;
  1406. dt.base = sregs->idt.base;
  1407. kvm_arch_ops->set_idt(vcpu, &dt);
  1408. dt.limit = sregs->gdt.limit;
  1409. dt.base = sregs->gdt.base;
  1410. kvm_arch_ops->set_gdt(vcpu, &dt);
  1411. vcpu->cr2 = sregs->cr2;
  1412. mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
  1413. vcpu->cr3 = sregs->cr3;
  1414. vcpu->cr8 = sregs->cr8;
  1415. mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
  1416. #ifdef CONFIG_X86_64
  1417. kvm_arch_ops->set_efer(vcpu, sregs->efer);
  1418. #endif
  1419. vcpu->apic_base = sregs->apic_base;
  1420. kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
  1421. mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
  1422. kvm_arch_ops->set_cr0_no_modeswitch(vcpu, sregs->cr0);
  1423. mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
  1424. kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
  1425. if (!is_long_mode(vcpu) && is_pae(vcpu))
  1426. load_pdptrs(vcpu, vcpu->cr3);
  1427. if (mmu_reset_needed)
  1428. kvm_mmu_reset_context(vcpu);
  1429. memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
  1430. sizeof vcpu->irq_pending);
  1431. vcpu->irq_summary = 0;
  1432. for (i = 0; i < NR_IRQ_WORDS; ++i)
  1433. if (vcpu->irq_pending[i])
  1434. __set_bit(i, &vcpu->irq_summary);
  1435. vcpu_put(vcpu);
  1436. return 0;
  1437. }
  1438. /*
  1439. * List of msr numbers which we expose to userspace through KVM_GET_MSRS
  1440. * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
  1441. *
  1442. * This list is modified at module load time to reflect the
  1443. * capabilities of the host cpu.
  1444. */
  1445. static u32 msrs_to_save[] = {
  1446. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  1447. MSR_K6_STAR,
  1448. #ifdef CONFIG_X86_64
  1449. MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
  1450. #endif
  1451. MSR_IA32_TIME_STAMP_COUNTER,
  1452. };
  1453. static unsigned num_msrs_to_save;
  1454. static u32 emulated_msrs[] = {
  1455. MSR_IA32_MISC_ENABLE,
  1456. };
  1457. static __init void kvm_init_msr_list(void)
  1458. {
  1459. u32 dummy[2];
  1460. unsigned i, j;
  1461. for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
  1462. if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
  1463. continue;
  1464. if (j < i)
  1465. msrs_to_save[j] = msrs_to_save[i];
  1466. j++;
  1467. }
  1468. num_msrs_to_save = j;
  1469. }
  1470. /*
  1471. * Adapt set_msr() to msr_io()'s calling convention
  1472. */
  1473. static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
  1474. {
  1475. return set_msr(vcpu, index, *data);
  1476. }
  1477. /*
  1478. * Read or write a bunch of msrs. All parameters are kernel addresses.
  1479. *
  1480. * @return number of msrs set successfully.
  1481. */
  1482. static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
  1483. struct kvm_msr_entry *entries,
  1484. int (*do_msr)(struct kvm_vcpu *vcpu,
  1485. unsigned index, u64 *data))
  1486. {
  1487. int i;
  1488. vcpu_load(vcpu);
  1489. for (i = 0; i < msrs->nmsrs; ++i)
  1490. if (do_msr(vcpu, entries[i].index, &entries[i].data))
  1491. break;
  1492. vcpu_put(vcpu);
  1493. return i;
  1494. }
  1495. /*
  1496. * Read or write a bunch of msrs. Parameters are user addresses.
  1497. *
  1498. * @return number of msrs set successfully.
  1499. */
  1500. static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
  1501. int (*do_msr)(struct kvm_vcpu *vcpu,
  1502. unsigned index, u64 *data),
  1503. int writeback)
  1504. {
  1505. struct kvm_msrs msrs;
  1506. struct kvm_msr_entry *entries;
  1507. int r, n;
  1508. unsigned size;
  1509. r = -EFAULT;
  1510. if (copy_from_user(&msrs, user_msrs, sizeof msrs))
  1511. goto out;
  1512. r = -E2BIG;
  1513. if (msrs.nmsrs >= MAX_IO_MSRS)
  1514. goto out;
  1515. r = -ENOMEM;
  1516. size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
  1517. entries = vmalloc(size);
  1518. if (!entries)
  1519. goto out;
  1520. r = -EFAULT;
  1521. if (copy_from_user(entries, user_msrs->entries, size))
  1522. goto out_free;
  1523. r = n = __msr_io(vcpu, &msrs, entries, do_msr);
  1524. if (r < 0)
  1525. goto out_free;
  1526. r = -EFAULT;
  1527. if (writeback && copy_to_user(user_msrs->entries, entries, size))
  1528. goto out_free;
  1529. r = n;
  1530. out_free:
  1531. vfree(entries);
  1532. out:
  1533. return r;
  1534. }
  1535. /*
  1536. * Translate a guest virtual address to a guest physical address.
  1537. */
  1538. static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  1539. struct kvm_translation *tr)
  1540. {
  1541. unsigned long vaddr = tr->linear_address;
  1542. gpa_t gpa;
  1543. vcpu_load(vcpu);
  1544. spin_lock(&vcpu->kvm->lock);
  1545. gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
  1546. tr->physical_address = gpa;
  1547. tr->valid = gpa != UNMAPPED_GVA;
  1548. tr->writeable = 1;
  1549. tr->usermode = 0;
  1550. spin_unlock(&vcpu->kvm->lock);
  1551. vcpu_put(vcpu);
  1552. return 0;
  1553. }
  1554. static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
  1555. struct kvm_interrupt *irq)
  1556. {
  1557. if (irq->irq < 0 || irq->irq >= 256)
  1558. return -EINVAL;
  1559. vcpu_load(vcpu);
  1560. set_bit(irq->irq, vcpu->irq_pending);
  1561. set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
  1562. vcpu_put(vcpu);
  1563. return 0;
  1564. }
  1565. static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
  1566. struct kvm_debug_guest *dbg)
  1567. {
  1568. int r;
  1569. vcpu_load(vcpu);
  1570. r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
  1571. vcpu_put(vcpu);
  1572. return r;
  1573. }
  1574. static int kvm_vcpu_release(struct inode *inode, struct file *filp)
  1575. {
  1576. struct kvm_vcpu *vcpu = filp->private_data;
  1577. fput(vcpu->kvm->filp);
  1578. return 0;
  1579. }
  1580. static struct file_operations kvm_vcpu_fops = {
  1581. .release = kvm_vcpu_release,
  1582. .unlocked_ioctl = kvm_vcpu_ioctl,
  1583. .compat_ioctl = kvm_vcpu_ioctl,
  1584. };
  1585. /*
  1586. * Allocates an inode for the vcpu.
  1587. */
  1588. static int create_vcpu_fd(struct kvm_vcpu *vcpu)
  1589. {
  1590. int fd, r;
  1591. struct inode *inode;
  1592. struct file *file;
  1593. atomic_inc(&vcpu->kvm->filp->f_count);
  1594. inode = kvmfs_inode(&kvm_vcpu_fops);
  1595. if (IS_ERR(inode)) {
  1596. r = PTR_ERR(inode);
  1597. goto out1;
  1598. }
  1599. file = kvmfs_file(inode, vcpu);
  1600. if (IS_ERR(file)) {
  1601. r = PTR_ERR(file);
  1602. goto out2;
  1603. }
  1604. r = get_unused_fd();
  1605. if (r < 0)
  1606. goto out3;
  1607. fd = r;
  1608. fd_install(fd, file);
  1609. return fd;
  1610. out3:
  1611. fput(file);
  1612. out2:
  1613. iput(inode);
  1614. out1:
  1615. fput(vcpu->kvm->filp);
  1616. return r;
  1617. }
  1618. /*
  1619. * Creates some virtual cpus. Good luck creating more than one.
  1620. */
  1621. static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
  1622. {
  1623. int r;
  1624. struct kvm_vcpu *vcpu;
  1625. r = -EINVAL;
  1626. if (!valid_vcpu(n))
  1627. goto out;
  1628. vcpu = &kvm->vcpus[n];
  1629. mutex_lock(&vcpu->mutex);
  1630. if (vcpu->vmcs) {
  1631. mutex_unlock(&vcpu->mutex);
  1632. return -EEXIST;
  1633. }
  1634. vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
  1635. FX_IMAGE_ALIGN);
  1636. vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
  1637. r = kvm_arch_ops->vcpu_create(vcpu);
  1638. if (r < 0)
  1639. goto out_free_vcpus;
  1640. r = kvm_mmu_create(vcpu);
  1641. if (r < 0)
  1642. goto out_free_vcpus;
  1643. kvm_arch_ops->vcpu_load(vcpu);
  1644. r = kvm_mmu_setup(vcpu);
  1645. if (r >= 0)
  1646. r = kvm_arch_ops->vcpu_setup(vcpu);
  1647. vcpu_put(vcpu);
  1648. if (r < 0)
  1649. goto out_free_vcpus;
  1650. r = create_vcpu_fd(vcpu);
  1651. if (r < 0)
  1652. goto out_free_vcpus;
  1653. return r;
  1654. out_free_vcpus:
  1655. kvm_free_vcpu(vcpu);
  1656. mutex_unlock(&vcpu->mutex);
  1657. out:
  1658. return r;
  1659. }
  1660. static long kvm_vcpu_ioctl(struct file *filp,
  1661. unsigned int ioctl, unsigned long arg)
  1662. {
  1663. struct kvm_vcpu *vcpu = filp->private_data;
  1664. void __user *argp = (void __user *)arg;
  1665. int r = -EINVAL;
  1666. switch (ioctl) {
  1667. case KVM_RUN: {
  1668. struct kvm_run kvm_run;
  1669. r = -EFAULT;
  1670. if (copy_from_user(&kvm_run, argp, sizeof kvm_run))
  1671. goto out;
  1672. r = kvm_vcpu_ioctl_run(vcpu, &kvm_run);
  1673. if (r < 0 && r != -EINTR)
  1674. goto out;
  1675. if (copy_to_user(argp, &kvm_run, sizeof kvm_run)) {
  1676. r = -EFAULT;
  1677. goto out;
  1678. }
  1679. break;
  1680. }
  1681. case KVM_GET_REGS: {
  1682. struct kvm_regs kvm_regs;
  1683. memset(&kvm_regs, 0, sizeof kvm_regs);
  1684. r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
  1685. if (r)
  1686. goto out;
  1687. r = -EFAULT;
  1688. if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
  1689. goto out;
  1690. r = 0;
  1691. break;
  1692. }
  1693. case KVM_SET_REGS: {
  1694. struct kvm_regs kvm_regs;
  1695. r = -EFAULT;
  1696. if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
  1697. goto out;
  1698. r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
  1699. if (r)
  1700. goto out;
  1701. r = 0;
  1702. break;
  1703. }
  1704. case KVM_GET_SREGS: {
  1705. struct kvm_sregs kvm_sregs;
  1706. memset(&kvm_sregs, 0, sizeof kvm_sregs);
  1707. r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
  1708. if (r)
  1709. goto out;
  1710. r = -EFAULT;
  1711. if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
  1712. goto out;
  1713. r = 0;
  1714. break;
  1715. }
  1716. case KVM_SET_SREGS: {
  1717. struct kvm_sregs kvm_sregs;
  1718. r = -EFAULT;
  1719. if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
  1720. goto out;
  1721. r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
  1722. if (r)
  1723. goto out;
  1724. r = 0;
  1725. break;
  1726. }
  1727. case KVM_TRANSLATE: {
  1728. struct kvm_translation tr;
  1729. r = -EFAULT;
  1730. if (copy_from_user(&tr, argp, sizeof tr))
  1731. goto out;
  1732. r = kvm_vcpu_ioctl_translate(vcpu, &tr);
  1733. if (r)
  1734. goto out;
  1735. r = -EFAULT;
  1736. if (copy_to_user(argp, &tr, sizeof tr))
  1737. goto out;
  1738. r = 0;
  1739. break;
  1740. }
  1741. case KVM_INTERRUPT: {
  1742. struct kvm_interrupt irq;
  1743. r = -EFAULT;
  1744. if (copy_from_user(&irq, argp, sizeof irq))
  1745. goto out;
  1746. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  1747. if (r)
  1748. goto out;
  1749. r = 0;
  1750. break;
  1751. }
  1752. case KVM_DEBUG_GUEST: {
  1753. struct kvm_debug_guest dbg;
  1754. r = -EFAULT;
  1755. if (copy_from_user(&dbg, argp, sizeof dbg))
  1756. goto out;
  1757. r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
  1758. if (r)
  1759. goto out;
  1760. r = 0;
  1761. break;
  1762. }
  1763. case KVM_GET_MSRS:
  1764. r = msr_io(vcpu, argp, get_msr, 1);
  1765. break;
  1766. case KVM_SET_MSRS:
  1767. r = msr_io(vcpu, argp, do_set_msr, 0);
  1768. break;
  1769. default:
  1770. ;
  1771. }
  1772. out:
  1773. return r;
  1774. }
  1775. static long kvm_vm_ioctl(struct file *filp,
  1776. unsigned int ioctl, unsigned long arg)
  1777. {
  1778. struct kvm *kvm = filp->private_data;
  1779. void __user *argp = (void __user *)arg;
  1780. int r = -EINVAL;
  1781. switch (ioctl) {
  1782. case KVM_CREATE_VCPU:
  1783. r = kvm_vm_ioctl_create_vcpu(kvm, arg);
  1784. if (r < 0)
  1785. goto out;
  1786. break;
  1787. case KVM_SET_MEMORY_REGION: {
  1788. struct kvm_memory_region kvm_mem;
  1789. r = -EFAULT;
  1790. if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
  1791. goto out;
  1792. r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
  1793. if (r)
  1794. goto out;
  1795. break;
  1796. }
  1797. case KVM_GET_DIRTY_LOG: {
  1798. struct kvm_dirty_log log;
  1799. r = -EFAULT;
  1800. if (copy_from_user(&log, argp, sizeof log))
  1801. goto out;
  1802. r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
  1803. if (r)
  1804. goto out;
  1805. break;
  1806. }
  1807. default:
  1808. ;
  1809. }
  1810. out:
  1811. return r;
  1812. }
  1813. static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
  1814. unsigned long address,
  1815. int *type)
  1816. {
  1817. struct kvm *kvm = vma->vm_file->private_data;
  1818. unsigned long pgoff;
  1819. struct kvm_memory_slot *slot;
  1820. struct page *page;
  1821. *type = VM_FAULT_MINOR;
  1822. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  1823. slot = gfn_to_memslot(kvm, pgoff);
  1824. if (!slot)
  1825. return NOPAGE_SIGBUS;
  1826. page = gfn_to_page(slot, pgoff);
  1827. if (!page)
  1828. return NOPAGE_SIGBUS;
  1829. get_page(page);
  1830. return page;
  1831. }
  1832. static struct vm_operations_struct kvm_vm_vm_ops = {
  1833. .nopage = kvm_vm_nopage,
  1834. };
  1835. static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
  1836. {
  1837. vma->vm_ops = &kvm_vm_vm_ops;
  1838. return 0;
  1839. }
  1840. static struct file_operations kvm_vm_fops = {
  1841. .release = kvm_vm_release,
  1842. .unlocked_ioctl = kvm_vm_ioctl,
  1843. .compat_ioctl = kvm_vm_ioctl,
  1844. .mmap = kvm_vm_mmap,
  1845. };
  1846. static int kvm_dev_ioctl_create_vm(void)
  1847. {
  1848. int fd, r;
  1849. struct inode *inode;
  1850. struct file *file;
  1851. struct kvm *kvm;
  1852. inode = kvmfs_inode(&kvm_vm_fops);
  1853. if (IS_ERR(inode)) {
  1854. r = PTR_ERR(inode);
  1855. goto out1;
  1856. }
  1857. kvm = kvm_create_vm();
  1858. if (IS_ERR(kvm)) {
  1859. r = PTR_ERR(kvm);
  1860. goto out2;
  1861. }
  1862. file = kvmfs_file(inode, kvm);
  1863. if (IS_ERR(file)) {
  1864. r = PTR_ERR(file);
  1865. goto out3;
  1866. }
  1867. kvm->filp = file;
  1868. r = get_unused_fd();
  1869. if (r < 0)
  1870. goto out4;
  1871. fd = r;
  1872. fd_install(fd, file);
  1873. return fd;
  1874. out4:
  1875. fput(file);
  1876. out3:
  1877. kvm_destroy_vm(kvm);
  1878. out2:
  1879. iput(inode);
  1880. out1:
  1881. return r;
  1882. }
  1883. static long kvm_dev_ioctl(struct file *filp,
  1884. unsigned int ioctl, unsigned long arg)
  1885. {
  1886. void __user *argp = (void __user *)arg;
  1887. int r = -EINVAL;
  1888. switch (ioctl) {
  1889. case KVM_GET_API_VERSION:
  1890. r = KVM_API_VERSION;
  1891. break;
  1892. case KVM_CREATE_VM:
  1893. r = kvm_dev_ioctl_create_vm();
  1894. break;
  1895. case KVM_GET_MSR_INDEX_LIST: {
  1896. struct kvm_msr_list __user *user_msr_list = argp;
  1897. struct kvm_msr_list msr_list;
  1898. unsigned n;
  1899. r = -EFAULT;
  1900. if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
  1901. goto out;
  1902. n = msr_list.nmsrs;
  1903. msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
  1904. if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
  1905. goto out;
  1906. r = -E2BIG;
  1907. if (n < num_msrs_to_save)
  1908. goto out;
  1909. r = -EFAULT;
  1910. if (copy_to_user(user_msr_list->indices, &msrs_to_save,
  1911. num_msrs_to_save * sizeof(u32)))
  1912. goto out;
  1913. if (copy_to_user(user_msr_list->indices
  1914. + num_msrs_to_save * sizeof(u32),
  1915. &emulated_msrs,
  1916. ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
  1917. goto out;
  1918. r = 0;
  1919. break;
  1920. }
  1921. default:
  1922. ;
  1923. }
  1924. out:
  1925. return r;
  1926. }
  1927. static struct file_operations kvm_chardev_ops = {
  1928. .open = kvm_dev_open,
  1929. .release = kvm_dev_release,
  1930. .unlocked_ioctl = kvm_dev_ioctl,
  1931. .compat_ioctl = kvm_dev_ioctl,
  1932. };
  1933. static struct miscdevice kvm_dev = {
  1934. MISC_DYNAMIC_MINOR,
  1935. "kvm",
  1936. &kvm_chardev_ops,
  1937. };
  1938. static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
  1939. void *v)
  1940. {
  1941. if (val == SYS_RESTART) {
  1942. /*
  1943. * Some (well, at least mine) BIOSes hang on reboot if
  1944. * in vmx root mode.
  1945. */
  1946. printk(KERN_INFO "kvm: exiting hardware virtualization\n");
  1947. on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
  1948. }
  1949. return NOTIFY_OK;
  1950. }
  1951. static struct notifier_block kvm_reboot_notifier = {
  1952. .notifier_call = kvm_reboot,
  1953. .priority = 0,
  1954. };
  1955. /*
  1956. * Make sure that a cpu that is being hot-unplugged does not have any vcpus
  1957. * cached on it.
  1958. */
  1959. static void decache_vcpus_on_cpu(int cpu)
  1960. {
  1961. struct kvm *vm;
  1962. struct kvm_vcpu *vcpu;
  1963. int i;
  1964. spin_lock(&kvm_lock);
  1965. list_for_each_entry(vm, &vm_list, vm_list)
  1966. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  1967. vcpu = &vm->vcpus[i];
  1968. /*
  1969. * If the vcpu is locked, then it is running on some
  1970. * other cpu and therefore it is not cached on the
  1971. * cpu in question.
  1972. *
  1973. * If it's not locked, check the last cpu it executed
  1974. * on.
  1975. */
  1976. if (mutex_trylock(&vcpu->mutex)) {
  1977. if (vcpu->cpu == cpu) {
  1978. kvm_arch_ops->vcpu_decache(vcpu);
  1979. vcpu->cpu = -1;
  1980. }
  1981. mutex_unlock(&vcpu->mutex);
  1982. }
  1983. }
  1984. spin_unlock(&kvm_lock);
  1985. }
  1986. static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
  1987. void *v)
  1988. {
  1989. int cpu = (long)v;
  1990. switch (val) {
  1991. case CPU_DOWN_PREPARE:
  1992. case CPU_UP_CANCELED:
  1993. printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
  1994. cpu);
  1995. decache_vcpus_on_cpu(cpu);
  1996. smp_call_function_single(cpu, kvm_arch_ops->hardware_disable,
  1997. NULL, 0, 1);
  1998. break;
  1999. case CPU_ONLINE:
  2000. printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
  2001. cpu);
  2002. smp_call_function_single(cpu, kvm_arch_ops->hardware_enable,
  2003. NULL, 0, 1);
  2004. break;
  2005. }
  2006. return NOTIFY_OK;
  2007. }
  2008. static struct notifier_block kvm_cpu_notifier = {
  2009. .notifier_call = kvm_cpu_hotplug,
  2010. .priority = 20, /* must be > scheduler priority */
  2011. };
  2012. static __init void kvm_init_debug(void)
  2013. {
  2014. struct kvm_stats_debugfs_item *p;
  2015. debugfs_dir = debugfs_create_dir("kvm", NULL);
  2016. for (p = debugfs_entries; p->name; ++p)
  2017. p->dentry = debugfs_create_u32(p->name, 0444, debugfs_dir,
  2018. p->data);
  2019. }
  2020. static void kvm_exit_debug(void)
  2021. {
  2022. struct kvm_stats_debugfs_item *p;
  2023. for (p = debugfs_entries; p->name; ++p)
  2024. debugfs_remove(p->dentry);
  2025. debugfs_remove(debugfs_dir);
  2026. }
  2027. static int kvm_suspend(struct sys_device *dev, pm_message_t state)
  2028. {
  2029. decache_vcpus_on_cpu(raw_smp_processor_id());
  2030. on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
  2031. return 0;
  2032. }
  2033. static int kvm_resume(struct sys_device *dev)
  2034. {
  2035. on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
  2036. return 0;
  2037. }
  2038. static struct sysdev_class kvm_sysdev_class = {
  2039. set_kset_name("kvm"),
  2040. .suspend = kvm_suspend,
  2041. .resume = kvm_resume,
  2042. };
  2043. static struct sys_device kvm_sysdev = {
  2044. .id = 0,
  2045. .cls = &kvm_sysdev_class,
  2046. };
  2047. hpa_t bad_page_address;
  2048. static int kvmfs_get_sb(struct file_system_type *fs_type, int flags,
  2049. const char *dev_name, void *data, struct vfsmount *mnt)
  2050. {
  2051. return get_sb_pseudo(fs_type, "kvm:", NULL, KVMFS_MAGIC, mnt);
  2052. }
  2053. static struct file_system_type kvm_fs_type = {
  2054. .name = "kvmfs",
  2055. .get_sb = kvmfs_get_sb,
  2056. .kill_sb = kill_anon_super,
  2057. };
  2058. int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
  2059. {
  2060. int r;
  2061. if (kvm_arch_ops) {
  2062. printk(KERN_ERR "kvm: already loaded the other module\n");
  2063. return -EEXIST;
  2064. }
  2065. if (!ops->cpu_has_kvm_support()) {
  2066. printk(KERN_ERR "kvm: no hardware support\n");
  2067. return -EOPNOTSUPP;
  2068. }
  2069. if (ops->disabled_by_bios()) {
  2070. printk(KERN_ERR "kvm: disabled by bios\n");
  2071. return -EOPNOTSUPP;
  2072. }
  2073. kvm_arch_ops = ops;
  2074. r = kvm_arch_ops->hardware_setup();
  2075. if (r < 0)
  2076. return r;
  2077. on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
  2078. r = register_cpu_notifier(&kvm_cpu_notifier);
  2079. if (r)
  2080. goto out_free_1;
  2081. register_reboot_notifier(&kvm_reboot_notifier);
  2082. r = sysdev_class_register(&kvm_sysdev_class);
  2083. if (r)
  2084. goto out_free_2;
  2085. r = sysdev_register(&kvm_sysdev);
  2086. if (r)
  2087. goto out_free_3;
  2088. kvm_chardev_ops.owner = module;
  2089. r = misc_register(&kvm_dev);
  2090. if (r) {
  2091. printk (KERN_ERR "kvm: misc device register failed\n");
  2092. goto out_free;
  2093. }
  2094. return r;
  2095. out_free:
  2096. sysdev_unregister(&kvm_sysdev);
  2097. out_free_3:
  2098. sysdev_class_unregister(&kvm_sysdev_class);
  2099. out_free_2:
  2100. unregister_reboot_notifier(&kvm_reboot_notifier);
  2101. unregister_cpu_notifier(&kvm_cpu_notifier);
  2102. out_free_1:
  2103. on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
  2104. kvm_arch_ops->hardware_unsetup();
  2105. return r;
  2106. }
  2107. void kvm_exit_arch(void)
  2108. {
  2109. misc_deregister(&kvm_dev);
  2110. sysdev_unregister(&kvm_sysdev);
  2111. sysdev_class_unregister(&kvm_sysdev_class);
  2112. unregister_reboot_notifier(&kvm_reboot_notifier);
  2113. unregister_cpu_notifier(&kvm_cpu_notifier);
  2114. on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
  2115. kvm_arch_ops->hardware_unsetup();
  2116. kvm_arch_ops = NULL;
  2117. }
  2118. static __init int kvm_init(void)
  2119. {
  2120. static struct page *bad_page;
  2121. int r;
  2122. r = register_filesystem(&kvm_fs_type);
  2123. if (r)
  2124. goto out3;
  2125. kvmfs_mnt = kern_mount(&kvm_fs_type);
  2126. r = PTR_ERR(kvmfs_mnt);
  2127. if (IS_ERR(kvmfs_mnt))
  2128. goto out2;
  2129. kvm_init_debug();
  2130. kvm_init_msr_list();
  2131. if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
  2132. r = -ENOMEM;
  2133. goto out;
  2134. }
  2135. bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
  2136. memset(__va(bad_page_address), 0, PAGE_SIZE);
  2137. return r;
  2138. out:
  2139. kvm_exit_debug();
  2140. mntput(kvmfs_mnt);
  2141. out2:
  2142. unregister_filesystem(&kvm_fs_type);
  2143. out3:
  2144. return r;
  2145. }
  2146. static __exit void kvm_exit(void)
  2147. {
  2148. kvm_exit_debug();
  2149. __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
  2150. mntput(kvmfs_mnt);
  2151. unregister_filesystem(&kvm_fs_type);
  2152. }
  2153. module_init(kvm_init)
  2154. module_exit(kvm_exit)
  2155. EXPORT_SYMBOL_GPL(kvm_init_arch);
  2156. EXPORT_SYMBOL_GPL(kvm_exit_arch);