kvm_main.c 56 KB

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