kvm_main.c 57 KB

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