kvm_main.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762
  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. run->hypercall.args[0] = a0;
  1016. run->hypercall.args[1] = a1;
  1017. run->hypercall.args[2] = a2;
  1018. run->hypercall.args[3] = a3;
  1019. run->hypercall.args[4] = a4;
  1020. run->hypercall.args[5] = a5;
  1021. run->hypercall.ret = ret;
  1022. run->hypercall.longmode = is_long_mode(vcpu);
  1023. kvm_arch_ops->decache_regs(vcpu);
  1024. return 0;
  1025. }
  1026. vcpu->regs[VCPU_REGS_RAX] = ret;
  1027. kvm_arch_ops->decache_regs(vcpu);
  1028. return 1;
  1029. }
  1030. EXPORT_SYMBOL_GPL(kvm_hypercall);
  1031. static u64 mk_cr_64(u64 curr_cr, u32 new_val)
  1032. {
  1033. return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
  1034. }
  1035. void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  1036. {
  1037. struct descriptor_table dt = { limit, base };
  1038. kvm_arch_ops->set_gdt(vcpu, &dt);
  1039. }
  1040. void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
  1041. {
  1042. struct descriptor_table dt = { limit, base };
  1043. kvm_arch_ops->set_idt(vcpu, &dt);
  1044. }
  1045. void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
  1046. unsigned long *rflags)
  1047. {
  1048. lmsw(vcpu, msw);
  1049. *rflags = kvm_arch_ops->get_rflags(vcpu);
  1050. }
  1051. unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
  1052. {
  1053. kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
  1054. switch (cr) {
  1055. case 0:
  1056. return vcpu->cr0;
  1057. case 2:
  1058. return vcpu->cr2;
  1059. case 3:
  1060. return vcpu->cr3;
  1061. case 4:
  1062. return vcpu->cr4;
  1063. default:
  1064. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
  1065. return 0;
  1066. }
  1067. }
  1068. void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
  1069. unsigned long *rflags)
  1070. {
  1071. switch (cr) {
  1072. case 0:
  1073. set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
  1074. *rflags = kvm_arch_ops->get_rflags(vcpu);
  1075. break;
  1076. case 2:
  1077. vcpu->cr2 = val;
  1078. break;
  1079. case 3:
  1080. set_cr3(vcpu, val);
  1081. break;
  1082. case 4:
  1083. set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
  1084. break;
  1085. default:
  1086. vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
  1087. }
  1088. }
  1089. /*
  1090. * Register the para guest with the host:
  1091. */
  1092. static int vcpu_register_para(struct kvm_vcpu *vcpu, gpa_t para_state_gpa)
  1093. {
  1094. struct kvm_vcpu_para_state *para_state;
  1095. hpa_t para_state_hpa, hypercall_hpa;
  1096. struct page *para_state_page;
  1097. unsigned char *hypercall;
  1098. gpa_t hypercall_gpa;
  1099. printk(KERN_DEBUG "kvm: guest trying to enter paravirtual mode\n");
  1100. printk(KERN_DEBUG ".... para_state_gpa: %08Lx\n", para_state_gpa);
  1101. /*
  1102. * Needs to be page aligned:
  1103. */
  1104. if (para_state_gpa != PAGE_ALIGN(para_state_gpa))
  1105. goto err_gp;
  1106. para_state_hpa = gpa_to_hpa(vcpu, para_state_gpa);
  1107. printk(KERN_DEBUG ".... para_state_hpa: %08Lx\n", para_state_hpa);
  1108. if (is_error_hpa(para_state_hpa))
  1109. goto err_gp;
  1110. mark_page_dirty(vcpu->kvm, para_state_gpa >> PAGE_SHIFT);
  1111. para_state_page = pfn_to_page(para_state_hpa >> PAGE_SHIFT);
  1112. para_state = kmap_atomic(para_state_page, KM_USER0);
  1113. printk(KERN_DEBUG ".... guest version: %d\n", para_state->guest_version);
  1114. printk(KERN_DEBUG ".... size: %d\n", para_state->size);
  1115. para_state->host_version = KVM_PARA_API_VERSION;
  1116. /*
  1117. * We cannot support guests that try to register themselves
  1118. * with a newer API version than the host supports:
  1119. */
  1120. if (para_state->guest_version > KVM_PARA_API_VERSION) {
  1121. para_state->ret = -KVM_EINVAL;
  1122. goto err_kunmap_skip;
  1123. }
  1124. hypercall_gpa = para_state->hypercall_gpa;
  1125. hypercall_hpa = gpa_to_hpa(vcpu, hypercall_gpa);
  1126. printk(KERN_DEBUG ".... hypercall_hpa: %08Lx\n", hypercall_hpa);
  1127. if (is_error_hpa(hypercall_hpa)) {
  1128. para_state->ret = -KVM_EINVAL;
  1129. goto err_kunmap_skip;
  1130. }
  1131. printk(KERN_DEBUG "kvm: para guest successfully registered.\n");
  1132. vcpu->para_state_page = para_state_page;
  1133. vcpu->para_state_gpa = para_state_gpa;
  1134. vcpu->hypercall_gpa = hypercall_gpa;
  1135. mark_page_dirty(vcpu->kvm, hypercall_gpa >> PAGE_SHIFT);
  1136. hypercall = kmap_atomic(pfn_to_page(hypercall_hpa >> PAGE_SHIFT),
  1137. KM_USER1) + (hypercall_hpa & ~PAGE_MASK);
  1138. kvm_arch_ops->patch_hypercall(vcpu, hypercall);
  1139. kunmap_atomic(hypercall, KM_USER1);
  1140. para_state->ret = 0;
  1141. err_kunmap_skip:
  1142. kunmap_atomic(para_state, KM_USER0);
  1143. return 0;
  1144. err_gp:
  1145. return 1;
  1146. }
  1147. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
  1148. {
  1149. u64 data;
  1150. switch (msr) {
  1151. case 0xc0010010: /* SYSCFG */
  1152. case 0xc0010015: /* HWCR */
  1153. case MSR_IA32_PLATFORM_ID:
  1154. case MSR_IA32_P5_MC_ADDR:
  1155. case MSR_IA32_P5_MC_TYPE:
  1156. case MSR_IA32_MC0_CTL:
  1157. case MSR_IA32_MCG_STATUS:
  1158. case MSR_IA32_MCG_CAP:
  1159. case MSR_IA32_MC0_MISC:
  1160. case MSR_IA32_MC0_MISC+4:
  1161. case MSR_IA32_MC0_MISC+8:
  1162. case MSR_IA32_MC0_MISC+12:
  1163. case MSR_IA32_MC0_MISC+16:
  1164. case MSR_IA32_UCODE_REV:
  1165. case MSR_IA32_PERF_STATUS:
  1166. /* MTRR registers */
  1167. case 0xfe:
  1168. case 0x200 ... 0x2ff:
  1169. data = 0;
  1170. break;
  1171. case 0xcd: /* fsb frequency */
  1172. data = 3;
  1173. break;
  1174. case MSR_IA32_APICBASE:
  1175. data = vcpu->apic_base;
  1176. break;
  1177. case MSR_IA32_MISC_ENABLE:
  1178. data = vcpu->ia32_misc_enable_msr;
  1179. break;
  1180. #ifdef CONFIG_X86_64
  1181. case MSR_EFER:
  1182. data = vcpu->shadow_efer;
  1183. break;
  1184. #endif
  1185. default:
  1186. printk(KERN_ERR "kvm: unhandled rdmsr: 0x%x\n", msr);
  1187. return 1;
  1188. }
  1189. *pdata = data;
  1190. return 0;
  1191. }
  1192. EXPORT_SYMBOL_GPL(kvm_get_msr_common);
  1193. /*
  1194. * Reads an msr value (of 'msr_index') into 'pdata'.
  1195. * Returns 0 on success, non-0 otherwise.
  1196. * Assumes vcpu_load() was already called.
  1197. */
  1198. static int get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
  1199. {
  1200. return kvm_arch_ops->get_msr(vcpu, msr_index, pdata);
  1201. }
  1202. #ifdef CONFIG_X86_64
  1203. static void set_efer(struct kvm_vcpu *vcpu, u64 efer)
  1204. {
  1205. if (efer & EFER_RESERVED_BITS) {
  1206. printk(KERN_DEBUG "set_efer: 0x%llx #GP, reserved bits\n",
  1207. efer);
  1208. inject_gp(vcpu);
  1209. return;
  1210. }
  1211. if (is_paging(vcpu)
  1212. && (vcpu->shadow_efer & EFER_LME) != (efer & EFER_LME)) {
  1213. printk(KERN_DEBUG "set_efer: #GP, change LME while paging\n");
  1214. inject_gp(vcpu);
  1215. return;
  1216. }
  1217. kvm_arch_ops->set_efer(vcpu, efer);
  1218. efer &= ~EFER_LMA;
  1219. efer |= vcpu->shadow_efer & EFER_LMA;
  1220. vcpu->shadow_efer = efer;
  1221. }
  1222. #endif
  1223. int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data)
  1224. {
  1225. switch (msr) {
  1226. #ifdef CONFIG_X86_64
  1227. case MSR_EFER:
  1228. set_efer(vcpu, data);
  1229. break;
  1230. #endif
  1231. case MSR_IA32_MC0_STATUS:
  1232. printk(KERN_WARNING "%s: MSR_IA32_MC0_STATUS 0x%llx, nop\n",
  1233. __FUNCTION__, data);
  1234. break;
  1235. case MSR_IA32_UCODE_REV:
  1236. case MSR_IA32_UCODE_WRITE:
  1237. case 0x200 ... 0x2ff: /* MTRRs */
  1238. break;
  1239. case MSR_IA32_APICBASE:
  1240. vcpu->apic_base = data;
  1241. break;
  1242. case MSR_IA32_MISC_ENABLE:
  1243. vcpu->ia32_misc_enable_msr = data;
  1244. break;
  1245. /*
  1246. * This is the 'probe whether the host is KVM' logic:
  1247. */
  1248. case MSR_KVM_API_MAGIC:
  1249. return vcpu_register_para(vcpu, data);
  1250. default:
  1251. printk(KERN_ERR "kvm: unhandled wrmsr: 0x%x\n", msr);
  1252. return 1;
  1253. }
  1254. return 0;
  1255. }
  1256. EXPORT_SYMBOL_GPL(kvm_set_msr_common);
  1257. /*
  1258. * Writes msr value into into the appropriate "register".
  1259. * Returns 0 on success, non-0 otherwise.
  1260. * Assumes vcpu_load() was already called.
  1261. */
  1262. static int set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
  1263. {
  1264. return kvm_arch_ops->set_msr(vcpu, msr_index, data);
  1265. }
  1266. void kvm_resched(struct kvm_vcpu *vcpu)
  1267. {
  1268. vcpu_put(vcpu);
  1269. cond_resched();
  1270. vcpu_load(vcpu);
  1271. }
  1272. EXPORT_SYMBOL_GPL(kvm_resched);
  1273. void load_msrs(struct vmx_msr_entry *e, int n)
  1274. {
  1275. int i;
  1276. for (i = 0; i < n; ++i)
  1277. wrmsrl(e[i].index, e[i].data);
  1278. }
  1279. EXPORT_SYMBOL_GPL(load_msrs);
  1280. void save_msrs(struct vmx_msr_entry *e, int n)
  1281. {
  1282. int i;
  1283. for (i = 0; i < n; ++i)
  1284. rdmsrl(e[i].index, e[i].data);
  1285. }
  1286. EXPORT_SYMBOL_GPL(save_msrs);
  1287. void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
  1288. {
  1289. int i;
  1290. u32 function;
  1291. struct kvm_cpuid_entry *e, *best;
  1292. kvm_arch_ops->cache_regs(vcpu);
  1293. function = vcpu->regs[VCPU_REGS_RAX];
  1294. vcpu->regs[VCPU_REGS_RAX] = 0;
  1295. vcpu->regs[VCPU_REGS_RBX] = 0;
  1296. vcpu->regs[VCPU_REGS_RCX] = 0;
  1297. vcpu->regs[VCPU_REGS_RDX] = 0;
  1298. best = NULL;
  1299. for (i = 0; i < vcpu->cpuid_nent; ++i) {
  1300. e = &vcpu->cpuid_entries[i];
  1301. if (e->function == function) {
  1302. best = e;
  1303. break;
  1304. }
  1305. /*
  1306. * Both basic or both extended?
  1307. */
  1308. if (((e->function ^ function) & 0x80000000) == 0)
  1309. if (!best || e->function > best->function)
  1310. best = e;
  1311. }
  1312. if (best) {
  1313. vcpu->regs[VCPU_REGS_RAX] = best->eax;
  1314. vcpu->regs[VCPU_REGS_RBX] = best->ebx;
  1315. vcpu->regs[VCPU_REGS_RCX] = best->ecx;
  1316. vcpu->regs[VCPU_REGS_RDX] = best->edx;
  1317. }
  1318. kvm_arch_ops->decache_regs(vcpu);
  1319. kvm_arch_ops->skip_emulated_instruction(vcpu);
  1320. }
  1321. EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
  1322. static void complete_pio(struct kvm_vcpu *vcpu)
  1323. {
  1324. struct kvm_io *io = &vcpu->run->io;
  1325. long delta;
  1326. kvm_arch_ops->cache_regs(vcpu);
  1327. if (!io->string) {
  1328. if (io->direction == KVM_EXIT_IO_IN)
  1329. memcpy(&vcpu->regs[VCPU_REGS_RAX], &io->value,
  1330. io->size);
  1331. } else {
  1332. delta = 1;
  1333. if (io->rep) {
  1334. delta *= io->count;
  1335. /*
  1336. * The size of the register should really depend on
  1337. * current address size.
  1338. */
  1339. vcpu->regs[VCPU_REGS_RCX] -= delta;
  1340. }
  1341. if (io->string_down)
  1342. delta = -delta;
  1343. delta *= io->size;
  1344. if (io->direction == KVM_EXIT_IO_IN)
  1345. vcpu->regs[VCPU_REGS_RDI] += delta;
  1346. else
  1347. vcpu->regs[VCPU_REGS_RSI] += delta;
  1348. }
  1349. vcpu->pio_pending = 0;
  1350. vcpu->run->io_completed = 0;
  1351. kvm_arch_ops->decache_regs(vcpu);
  1352. kvm_arch_ops->skip_emulated_instruction(vcpu);
  1353. }
  1354. static int kvm_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
  1355. {
  1356. int r;
  1357. sigset_t sigsaved;
  1358. vcpu_load(vcpu);
  1359. if (vcpu->sigset_active)
  1360. sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
  1361. /* re-sync apic's tpr */
  1362. vcpu->cr8 = kvm_run->cr8;
  1363. if (kvm_run->io_completed) {
  1364. if (vcpu->pio_pending)
  1365. complete_pio(vcpu);
  1366. else {
  1367. memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
  1368. vcpu->mmio_read_completed = 1;
  1369. }
  1370. }
  1371. vcpu->mmio_needed = 0;
  1372. if (kvm_run->exit_reason == KVM_EXIT_HYPERCALL) {
  1373. kvm_arch_ops->cache_regs(vcpu);
  1374. vcpu->regs[VCPU_REGS_RAX] = kvm_run->hypercall.ret;
  1375. kvm_arch_ops->decache_regs(vcpu);
  1376. }
  1377. r = kvm_arch_ops->run(vcpu, kvm_run);
  1378. if (vcpu->sigset_active)
  1379. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1380. vcpu_put(vcpu);
  1381. return r;
  1382. }
  1383. static int kvm_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu,
  1384. struct kvm_regs *regs)
  1385. {
  1386. vcpu_load(vcpu);
  1387. kvm_arch_ops->cache_regs(vcpu);
  1388. regs->rax = vcpu->regs[VCPU_REGS_RAX];
  1389. regs->rbx = vcpu->regs[VCPU_REGS_RBX];
  1390. regs->rcx = vcpu->regs[VCPU_REGS_RCX];
  1391. regs->rdx = vcpu->regs[VCPU_REGS_RDX];
  1392. regs->rsi = vcpu->regs[VCPU_REGS_RSI];
  1393. regs->rdi = vcpu->regs[VCPU_REGS_RDI];
  1394. regs->rsp = vcpu->regs[VCPU_REGS_RSP];
  1395. regs->rbp = vcpu->regs[VCPU_REGS_RBP];
  1396. #ifdef CONFIG_X86_64
  1397. regs->r8 = vcpu->regs[VCPU_REGS_R8];
  1398. regs->r9 = vcpu->regs[VCPU_REGS_R9];
  1399. regs->r10 = vcpu->regs[VCPU_REGS_R10];
  1400. regs->r11 = vcpu->regs[VCPU_REGS_R11];
  1401. regs->r12 = vcpu->regs[VCPU_REGS_R12];
  1402. regs->r13 = vcpu->regs[VCPU_REGS_R13];
  1403. regs->r14 = vcpu->regs[VCPU_REGS_R14];
  1404. regs->r15 = vcpu->regs[VCPU_REGS_R15];
  1405. #endif
  1406. regs->rip = vcpu->rip;
  1407. regs->rflags = kvm_arch_ops->get_rflags(vcpu);
  1408. /*
  1409. * Don't leak debug flags in case they were set for guest debugging
  1410. */
  1411. if (vcpu->guest_debug.enabled && vcpu->guest_debug.singlestep)
  1412. regs->rflags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
  1413. vcpu_put(vcpu);
  1414. return 0;
  1415. }
  1416. static int kvm_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu,
  1417. struct kvm_regs *regs)
  1418. {
  1419. vcpu_load(vcpu);
  1420. vcpu->regs[VCPU_REGS_RAX] = regs->rax;
  1421. vcpu->regs[VCPU_REGS_RBX] = regs->rbx;
  1422. vcpu->regs[VCPU_REGS_RCX] = regs->rcx;
  1423. vcpu->regs[VCPU_REGS_RDX] = regs->rdx;
  1424. vcpu->regs[VCPU_REGS_RSI] = regs->rsi;
  1425. vcpu->regs[VCPU_REGS_RDI] = regs->rdi;
  1426. vcpu->regs[VCPU_REGS_RSP] = regs->rsp;
  1427. vcpu->regs[VCPU_REGS_RBP] = regs->rbp;
  1428. #ifdef CONFIG_X86_64
  1429. vcpu->regs[VCPU_REGS_R8] = regs->r8;
  1430. vcpu->regs[VCPU_REGS_R9] = regs->r9;
  1431. vcpu->regs[VCPU_REGS_R10] = regs->r10;
  1432. vcpu->regs[VCPU_REGS_R11] = regs->r11;
  1433. vcpu->regs[VCPU_REGS_R12] = regs->r12;
  1434. vcpu->regs[VCPU_REGS_R13] = regs->r13;
  1435. vcpu->regs[VCPU_REGS_R14] = regs->r14;
  1436. vcpu->regs[VCPU_REGS_R15] = regs->r15;
  1437. #endif
  1438. vcpu->rip = regs->rip;
  1439. kvm_arch_ops->set_rflags(vcpu, regs->rflags);
  1440. kvm_arch_ops->decache_regs(vcpu);
  1441. vcpu_put(vcpu);
  1442. return 0;
  1443. }
  1444. static void get_segment(struct kvm_vcpu *vcpu,
  1445. struct kvm_segment *var, int seg)
  1446. {
  1447. return kvm_arch_ops->get_segment(vcpu, var, seg);
  1448. }
  1449. static int kvm_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
  1450. struct kvm_sregs *sregs)
  1451. {
  1452. struct descriptor_table dt;
  1453. vcpu_load(vcpu);
  1454. get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  1455. get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  1456. get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  1457. get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  1458. get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  1459. get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  1460. get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  1461. get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  1462. kvm_arch_ops->get_idt(vcpu, &dt);
  1463. sregs->idt.limit = dt.limit;
  1464. sregs->idt.base = dt.base;
  1465. kvm_arch_ops->get_gdt(vcpu, &dt);
  1466. sregs->gdt.limit = dt.limit;
  1467. sregs->gdt.base = dt.base;
  1468. kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
  1469. sregs->cr0 = vcpu->cr0;
  1470. sregs->cr2 = vcpu->cr2;
  1471. sregs->cr3 = vcpu->cr3;
  1472. sregs->cr4 = vcpu->cr4;
  1473. sregs->cr8 = vcpu->cr8;
  1474. sregs->efer = vcpu->shadow_efer;
  1475. sregs->apic_base = vcpu->apic_base;
  1476. memcpy(sregs->interrupt_bitmap, vcpu->irq_pending,
  1477. sizeof sregs->interrupt_bitmap);
  1478. vcpu_put(vcpu);
  1479. return 0;
  1480. }
  1481. static void set_segment(struct kvm_vcpu *vcpu,
  1482. struct kvm_segment *var, int seg)
  1483. {
  1484. return kvm_arch_ops->set_segment(vcpu, var, seg);
  1485. }
  1486. static int kvm_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
  1487. struct kvm_sregs *sregs)
  1488. {
  1489. int mmu_reset_needed = 0;
  1490. int i;
  1491. struct descriptor_table dt;
  1492. vcpu_load(vcpu);
  1493. set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
  1494. set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
  1495. set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
  1496. set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
  1497. set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
  1498. set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
  1499. set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
  1500. set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
  1501. dt.limit = sregs->idt.limit;
  1502. dt.base = sregs->idt.base;
  1503. kvm_arch_ops->set_idt(vcpu, &dt);
  1504. dt.limit = sregs->gdt.limit;
  1505. dt.base = sregs->gdt.base;
  1506. kvm_arch_ops->set_gdt(vcpu, &dt);
  1507. vcpu->cr2 = sregs->cr2;
  1508. mmu_reset_needed |= vcpu->cr3 != sregs->cr3;
  1509. vcpu->cr3 = sregs->cr3;
  1510. vcpu->cr8 = sregs->cr8;
  1511. mmu_reset_needed |= vcpu->shadow_efer != sregs->efer;
  1512. #ifdef CONFIG_X86_64
  1513. kvm_arch_ops->set_efer(vcpu, sregs->efer);
  1514. #endif
  1515. vcpu->apic_base = sregs->apic_base;
  1516. kvm_arch_ops->decache_cr0_cr4_guest_bits(vcpu);
  1517. mmu_reset_needed |= vcpu->cr0 != sregs->cr0;
  1518. kvm_arch_ops->set_cr0_no_modeswitch(vcpu, sregs->cr0);
  1519. mmu_reset_needed |= vcpu->cr4 != sregs->cr4;
  1520. kvm_arch_ops->set_cr4(vcpu, sregs->cr4);
  1521. if (!is_long_mode(vcpu) && is_pae(vcpu))
  1522. load_pdptrs(vcpu, vcpu->cr3);
  1523. if (mmu_reset_needed)
  1524. kvm_mmu_reset_context(vcpu);
  1525. memcpy(vcpu->irq_pending, sregs->interrupt_bitmap,
  1526. sizeof vcpu->irq_pending);
  1527. vcpu->irq_summary = 0;
  1528. for (i = 0; i < NR_IRQ_WORDS; ++i)
  1529. if (vcpu->irq_pending[i])
  1530. __set_bit(i, &vcpu->irq_summary);
  1531. vcpu_put(vcpu);
  1532. return 0;
  1533. }
  1534. /*
  1535. * List of msr numbers which we expose to userspace through KVM_GET_MSRS
  1536. * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
  1537. *
  1538. * This list is modified at module load time to reflect the
  1539. * capabilities of the host cpu.
  1540. */
  1541. static u32 msrs_to_save[] = {
  1542. MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
  1543. MSR_K6_STAR,
  1544. #ifdef CONFIG_X86_64
  1545. MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
  1546. #endif
  1547. MSR_IA32_TIME_STAMP_COUNTER,
  1548. };
  1549. static unsigned num_msrs_to_save;
  1550. static u32 emulated_msrs[] = {
  1551. MSR_IA32_MISC_ENABLE,
  1552. };
  1553. static __init void kvm_init_msr_list(void)
  1554. {
  1555. u32 dummy[2];
  1556. unsigned i, j;
  1557. for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
  1558. if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
  1559. continue;
  1560. if (j < i)
  1561. msrs_to_save[j] = msrs_to_save[i];
  1562. j++;
  1563. }
  1564. num_msrs_to_save = j;
  1565. }
  1566. /*
  1567. * Adapt set_msr() to msr_io()'s calling convention
  1568. */
  1569. static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
  1570. {
  1571. return set_msr(vcpu, index, *data);
  1572. }
  1573. /*
  1574. * Read or write a bunch of msrs. All parameters are kernel addresses.
  1575. *
  1576. * @return number of msrs set successfully.
  1577. */
  1578. static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
  1579. struct kvm_msr_entry *entries,
  1580. int (*do_msr)(struct kvm_vcpu *vcpu,
  1581. unsigned index, u64 *data))
  1582. {
  1583. int i;
  1584. vcpu_load(vcpu);
  1585. for (i = 0; i < msrs->nmsrs; ++i)
  1586. if (do_msr(vcpu, entries[i].index, &entries[i].data))
  1587. break;
  1588. vcpu_put(vcpu);
  1589. return i;
  1590. }
  1591. /*
  1592. * Read or write a bunch of msrs. Parameters are user addresses.
  1593. *
  1594. * @return number of msrs set successfully.
  1595. */
  1596. static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
  1597. int (*do_msr)(struct kvm_vcpu *vcpu,
  1598. unsigned index, u64 *data),
  1599. int writeback)
  1600. {
  1601. struct kvm_msrs msrs;
  1602. struct kvm_msr_entry *entries;
  1603. int r, n;
  1604. unsigned size;
  1605. r = -EFAULT;
  1606. if (copy_from_user(&msrs, user_msrs, sizeof msrs))
  1607. goto out;
  1608. r = -E2BIG;
  1609. if (msrs.nmsrs >= MAX_IO_MSRS)
  1610. goto out;
  1611. r = -ENOMEM;
  1612. size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
  1613. entries = vmalloc(size);
  1614. if (!entries)
  1615. goto out;
  1616. r = -EFAULT;
  1617. if (copy_from_user(entries, user_msrs->entries, size))
  1618. goto out_free;
  1619. r = n = __msr_io(vcpu, &msrs, entries, do_msr);
  1620. if (r < 0)
  1621. goto out_free;
  1622. r = -EFAULT;
  1623. if (writeback && copy_to_user(user_msrs->entries, entries, size))
  1624. goto out_free;
  1625. r = n;
  1626. out_free:
  1627. vfree(entries);
  1628. out:
  1629. return r;
  1630. }
  1631. /*
  1632. * Translate a guest virtual address to a guest physical address.
  1633. */
  1634. static int kvm_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
  1635. struct kvm_translation *tr)
  1636. {
  1637. unsigned long vaddr = tr->linear_address;
  1638. gpa_t gpa;
  1639. vcpu_load(vcpu);
  1640. spin_lock(&vcpu->kvm->lock);
  1641. gpa = vcpu->mmu.gva_to_gpa(vcpu, vaddr);
  1642. tr->physical_address = gpa;
  1643. tr->valid = gpa != UNMAPPED_GVA;
  1644. tr->writeable = 1;
  1645. tr->usermode = 0;
  1646. spin_unlock(&vcpu->kvm->lock);
  1647. vcpu_put(vcpu);
  1648. return 0;
  1649. }
  1650. static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
  1651. struct kvm_interrupt *irq)
  1652. {
  1653. if (irq->irq < 0 || irq->irq >= 256)
  1654. return -EINVAL;
  1655. vcpu_load(vcpu);
  1656. set_bit(irq->irq, vcpu->irq_pending);
  1657. set_bit(irq->irq / BITS_PER_LONG, &vcpu->irq_summary);
  1658. vcpu_put(vcpu);
  1659. return 0;
  1660. }
  1661. static int kvm_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
  1662. struct kvm_debug_guest *dbg)
  1663. {
  1664. int r;
  1665. vcpu_load(vcpu);
  1666. r = kvm_arch_ops->set_guest_debug(vcpu, dbg);
  1667. vcpu_put(vcpu);
  1668. return r;
  1669. }
  1670. static struct page *kvm_vcpu_nopage(struct vm_area_struct *vma,
  1671. unsigned long address,
  1672. int *type)
  1673. {
  1674. struct kvm_vcpu *vcpu = vma->vm_file->private_data;
  1675. unsigned long pgoff;
  1676. struct page *page;
  1677. *type = VM_FAULT_MINOR;
  1678. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  1679. if (pgoff != 0)
  1680. return NOPAGE_SIGBUS;
  1681. page = virt_to_page(vcpu->run);
  1682. get_page(page);
  1683. return page;
  1684. }
  1685. static struct vm_operations_struct kvm_vcpu_vm_ops = {
  1686. .nopage = kvm_vcpu_nopage,
  1687. };
  1688. static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
  1689. {
  1690. vma->vm_ops = &kvm_vcpu_vm_ops;
  1691. return 0;
  1692. }
  1693. static int kvm_vcpu_release(struct inode *inode, struct file *filp)
  1694. {
  1695. struct kvm_vcpu *vcpu = filp->private_data;
  1696. fput(vcpu->kvm->filp);
  1697. return 0;
  1698. }
  1699. static struct file_operations kvm_vcpu_fops = {
  1700. .release = kvm_vcpu_release,
  1701. .unlocked_ioctl = kvm_vcpu_ioctl,
  1702. .compat_ioctl = kvm_vcpu_ioctl,
  1703. .mmap = kvm_vcpu_mmap,
  1704. };
  1705. /*
  1706. * Allocates an inode for the vcpu.
  1707. */
  1708. static int create_vcpu_fd(struct kvm_vcpu *vcpu)
  1709. {
  1710. int fd, r;
  1711. struct inode *inode;
  1712. struct file *file;
  1713. atomic_inc(&vcpu->kvm->filp->f_count);
  1714. inode = kvmfs_inode(&kvm_vcpu_fops);
  1715. if (IS_ERR(inode)) {
  1716. r = PTR_ERR(inode);
  1717. goto out1;
  1718. }
  1719. file = kvmfs_file(inode, vcpu);
  1720. if (IS_ERR(file)) {
  1721. r = PTR_ERR(file);
  1722. goto out2;
  1723. }
  1724. r = get_unused_fd();
  1725. if (r < 0)
  1726. goto out3;
  1727. fd = r;
  1728. fd_install(fd, file);
  1729. return fd;
  1730. out3:
  1731. fput(file);
  1732. out2:
  1733. iput(inode);
  1734. out1:
  1735. fput(vcpu->kvm->filp);
  1736. return r;
  1737. }
  1738. /*
  1739. * Creates some virtual cpus. Good luck creating more than one.
  1740. */
  1741. static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, int n)
  1742. {
  1743. int r;
  1744. struct kvm_vcpu *vcpu;
  1745. struct page *page;
  1746. r = -EINVAL;
  1747. if (!valid_vcpu(n))
  1748. goto out;
  1749. vcpu = &kvm->vcpus[n];
  1750. mutex_lock(&vcpu->mutex);
  1751. if (vcpu->vmcs) {
  1752. mutex_unlock(&vcpu->mutex);
  1753. return -EEXIST;
  1754. }
  1755. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  1756. r = -ENOMEM;
  1757. if (!page)
  1758. goto out_unlock;
  1759. vcpu->run = page_address(page);
  1760. vcpu->host_fx_image = (char*)ALIGN((hva_t)vcpu->fx_buf,
  1761. FX_IMAGE_ALIGN);
  1762. vcpu->guest_fx_image = vcpu->host_fx_image + FX_IMAGE_SIZE;
  1763. r = kvm_arch_ops->vcpu_create(vcpu);
  1764. if (r < 0)
  1765. goto out_free_vcpus;
  1766. r = kvm_mmu_create(vcpu);
  1767. if (r < 0)
  1768. goto out_free_vcpus;
  1769. kvm_arch_ops->vcpu_load(vcpu);
  1770. r = kvm_mmu_setup(vcpu);
  1771. if (r >= 0)
  1772. r = kvm_arch_ops->vcpu_setup(vcpu);
  1773. vcpu_put(vcpu);
  1774. if (r < 0)
  1775. goto out_free_vcpus;
  1776. r = create_vcpu_fd(vcpu);
  1777. if (r < 0)
  1778. goto out_free_vcpus;
  1779. return r;
  1780. out_free_vcpus:
  1781. kvm_free_vcpu(vcpu);
  1782. out_unlock:
  1783. mutex_unlock(&vcpu->mutex);
  1784. out:
  1785. return r;
  1786. }
  1787. static int kvm_vcpu_ioctl_set_cpuid(struct kvm_vcpu *vcpu,
  1788. struct kvm_cpuid *cpuid,
  1789. struct kvm_cpuid_entry __user *entries)
  1790. {
  1791. int r;
  1792. r = -E2BIG;
  1793. if (cpuid->nent > KVM_MAX_CPUID_ENTRIES)
  1794. goto out;
  1795. r = -EFAULT;
  1796. if (copy_from_user(&vcpu->cpuid_entries, entries,
  1797. cpuid->nent * sizeof(struct kvm_cpuid_entry)))
  1798. goto out;
  1799. vcpu->cpuid_nent = cpuid->nent;
  1800. return 0;
  1801. out:
  1802. return r;
  1803. }
  1804. static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
  1805. {
  1806. if (sigset) {
  1807. sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1808. vcpu->sigset_active = 1;
  1809. vcpu->sigset = *sigset;
  1810. } else
  1811. vcpu->sigset_active = 0;
  1812. return 0;
  1813. }
  1814. static long kvm_vcpu_ioctl(struct file *filp,
  1815. unsigned int ioctl, unsigned long arg)
  1816. {
  1817. struct kvm_vcpu *vcpu = filp->private_data;
  1818. void __user *argp = (void __user *)arg;
  1819. int r = -EINVAL;
  1820. switch (ioctl) {
  1821. case KVM_RUN:
  1822. r = kvm_vcpu_ioctl_run(vcpu, vcpu->run);
  1823. break;
  1824. case KVM_GET_REGS: {
  1825. struct kvm_regs kvm_regs;
  1826. memset(&kvm_regs, 0, sizeof kvm_regs);
  1827. r = kvm_vcpu_ioctl_get_regs(vcpu, &kvm_regs);
  1828. if (r)
  1829. goto out;
  1830. r = -EFAULT;
  1831. if (copy_to_user(argp, &kvm_regs, sizeof kvm_regs))
  1832. goto out;
  1833. r = 0;
  1834. break;
  1835. }
  1836. case KVM_SET_REGS: {
  1837. struct kvm_regs kvm_regs;
  1838. r = -EFAULT;
  1839. if (copy_from_user(&kvm_regs, argp, sizeof kvm_regs))
  1840. goto out;
  1841. r = kvm_vcpu_ioctl_set_regs(vcpu, &kvm_regs);
  1842. if (r)
  1843. goto out;
  1844. r = 0;
  1845. break;
  1846. }
  1847. case KVM_GET_SREGS: {
  1848. struct kvm_sregs kvm_sregs;
  1849. memset(&kvm_sregs, 0, sizeof kvm_sregs);
  1850. r = kvm_vcpu_ioctl_get_sregs(vcpu, &kvm_sregs);
  1851. if (r)
  1852. goto out;
  1853. r = -EFAULT;
  1854. if (copy_to_user(argp, &kvm_sregs, sizeof kvm_sregs))
  1855. goto out;
  1856. r = 0;
  1857. break;
  1858. }
  1859. case KVM_SET_SREGS: {
  1860. struct kvm_sregs kvm_sregs;
  1861. r = -EFAULT;
  1862. if (copy_from_user(&kvm_sregs, argp, sizeof kvm_sregs))
  1863. goto out;
  1864. r = kvm_vcpu_ioctl_set_sregs(vcpu, &kvm_sregs);
  1865. if (r)
  1866. goto out;
  1867. r = 0;
  1868. break;
  1869. }
  1870. case KVM_TRANSLATE: {
  1871. struct kvm_translation tr;
  1872. r = -EFAULT;
  1873. if (copy_from_user(&tr, argp, sizeof tr))
  1874. goto out;
  1875. r = kvm_vcpu_ioctl_translate(vcpu, &tr);
  1876. if (r)
  1877. goto out;
  1878. r = -EFAULT;
  1879. if (copy_to_user(argp, &tr, sizeof tr))
  1880. goto out;
  1881. r = 0;
  1882. break;
  1883. }
  1884. case KVM_INTERRUPT: {
  1885. struct kvm_interrupt irq;
  1886. r = -EFAULT;
  1887. if (copy_from_user(&irq, argp, sizeof irq))
  1888. goto out;
  1889. r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
  1890. if (r)
  1891. goto out;
  1892. r = 0;
  1893. break;
  1894. }
  1895. case KVM_DEBUG_GUEST: {
  1896. struct kvm_debug_guest dbg;
  1897. r = -EFAULT;
  1898. if (copy_from_user(&dbg, argp, sizeof dbg))
  1899. goto out;
  1900. r = kvm_vcpu_ioctl_debug_guest(vcpu, &dbg);
  1901. if (r)
  1902. goto out;
  1903. r = 0;
  1904. break;
  1905. }
  1906. case KVM_GET_MSRS:
  1907. r = msr_io(vcpu, argp, get_msr, 1);
  1908. break;
  1909. case KVM_SET_MSRS:
  1910. r = msr_io(vcpu, argp, do_set_msr, 0);
  1911. break;
  1912. case KVM_SET_CPUID: {
  1913. struct kvm_cpuid __user *cpuid_arg = argp;
  1914. struct kvm_cpuid cpuid;
  1915. r = -EFAULT;
  1916. if (copy_from_user(&cpuid, cpuid_arg, sizeof cpuid))
  1917. goto out;
  1918. r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
  1919. if (r)
  1920. goto out;
  1921. break;
  1922. }
  1923. case KVM_SET_SIGNAL_MASK: {
  1924. struct kvm_signal_mask __user *sigmask_arg = argp;
  1925. struct kvm_signal_mask kvm_sigmask;
  1926. sigset_t sigset, *p;
  1927. p = NULL;
  1928. if (argp) {
  1929. r = -EFAULT;
  1930. if (copy_from_user(&kvm_sigmask, argp,
  1931. sizeof kvm_sigmask))
  1932. goto out;
  1933. r = -EINVAL;
  1934. if (kvm_sigmask.len != sizeof sigset)
  1935. goto out;
  1936. r = -EFAULT;
  1937. if (copy_from_user(&sigset, sigmask_arg->sigset,
  1938. sizeof sigset))
  1939. goto out;
  1940. p = &sigset;
  1941. }
  1942. r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
  1943. break;
  1944. }
  1945. default:
  1946. ;
  1947. }
  1948. out:
  1949. return r;
  1950. }
  1951. static long kvm_vm_ioctl(struct file *filp,
  1952. unsigned int ioctl, unsigned long arg)
  1953. {
  1954. struct kvm *kvm = filp->private_data;
  1955. void __user *argp = (void __user *)arg;
  1956. int r = -EINVAL;
  1957. switch (ioctl) {
  1958. case KVM_CREATE_VCPU:
  1959. r = kvm_vm_ioctl_create_vcpu(kvm, arg);
  1960. if (r < 0)
  1961. goto out;
  1962. break;
  1963. case KVM_SET_MEMORY_REGION: {
  1964. struct kvm_memory_region kvm_mem;
  1965. r = -EFAULT;
  1966. if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
  1967. goto out;
  1968. r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_mem);
  1969. if (r)
  1970. goto out;
  1971. break;
  1972. }
  1973. case KVM_GET_DIRTY_LOG: {
  1974. struct kvm_dirty_log log;
  1975. r = -EFAULT;
  1976. if (copy_from_user(&log, argp, sizeof log))
  1977. goto out;
  1978. r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
  1979. if (r)
  1980. goto out;
  1981. break;
  1982. }
  1983. default:
  1984. ;
  1985. }
  1986. out:
  1987. return r;
  1988. }
  1989. static struct page *kvm_vm_nopage(struct vm_area_struct *vma,
  1990. unsigned long address,
  1991. int *type)
  1992. {
  1993. struct kvm *kvm = vma->vm_file->private_data;
  1994. unsigned long pgoff;
  1995. struct kvm_memory_slot *slot;
  1996. struct page *page;
  1997. *type = VM_FAULT_MINOR;
  1998. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  1999. slot = gfn_to_memslot(kvm, pgoff);
  2000. if (!slot)
  2001. return NOPAGE_SIGBUS;
  2002. page = gfn_to_page(slot, pgoff);
  2003. if (!page)
  2004. return NOPAGE_SIGBUS;
  2005. get_page(page);
  2006. return page;
  2007. }
  2008. static struct vm_operations_struct kvm_vm_vm_ops = {
  2009. .nopage = kvm_vm_nopage,
  2010. };
  2011. static int kvm_vm_mmap(struct file *file, struct vm_area_struct *vma)
  2012. {
  2013. vma->vm_ops = &kvm_vm_vm_ops;
  2014. return 0;
  2015. }
  2016. static struct file_operations kvm_vm_fops = {
  2017. .release = kvm_vm_release,
  2018. .unlocked_ioctl = kvm_vm_ioctl,
  2019. .compat_ioctl = kvm_vm_ioctl,
  2020. .mmap = kvm_vm_mmap,
  2021. };
  2022. static int kvm_dev_ioctl_create_vm(void)
  2023. {
  2024. int fd, r;
  2025. struct inode *inode;
  2026. struct file *file;
  2027. struct kvm *kvm;
  2028. inode = kvmfs_inode(&kvm_vm_fops);
  2029. if (IS_ERR(inode)) {
  2030. r = PTR_ERR(inode);
  2031. goto out1;
  2032. }
  2033. kvm = kvm_create_vm();
  2034. if (IS_ERR(kvm)) {
  2035. r = PTR_ERR(kvm);
  2036. goto out2;
  2037. }
  2038. file = kvmfs_file(inode, kvm);
  2039. if (IS_ERR(file)) {
  2040. r = PTR_ERR(file);
  2041. goto out3;
  2042. }
  2043. kvm->filp = file;
  2044. r = get_unused_fd();
  2045. if (r < 0)
  2046. goto out4;
  2047. fd = r;
  2048. fd_install(fd, file);
  2049. return fd;
  2050. out4:
  2051. fput(file);
  2052. out3:
  2053. kvm_destroy_vm(kvm);
  2054. out2:
  2055. iput(inode);
  2056. out1:
  2057. return r;
  2058. }
  2059. static long kvm_dev_ioctl(struct file *filp,
  2060. unsigned int ioctl, unsigned long arg)
  2061. {
  2062. void __user *argp = (void __user *)arg;
  2063. int r = -EINVAL;
  2064. switch (ioctl) {
  2065. case KVM_GET_API_VERSION:
  2066. r = KVM_API_VERSION;
  2067. break;
  2068. case KVM_CREATE_VM:
  2069. r = kvm_dev_ioctl_create_vm();
  2070. break;
  2071. case KVM_GET_MSR_INDEX_LIST: {
  2072. struct kvm_msr_list __user *user_msr_list = argp;
  2073. struct kvm_msr_list msr_list;
  2074. unsigned n;
  2075. r = -EFAULT;
  2076. if (copy_from_user(&msr_list, user_msr_list, sizeof msr_list))
  2077. goto out;
  2078. n = msr_list.nmsrs;
  2079. msr_list.nmsrs = num_msrs_to_save + ARRAY_SIZE(emulated_msrs);
  2080. if (copy_to_user(user_msr_list, &msr_list, sizeof msr_list))
  2081. goto out;
  2082. r = -E2BIG;
  2083. if (n < num_msrs_to_save)
  2084. goto out;
  2085. r = -EFAULT;
  2086. if (copy_to_user(user_msr_list->indices, &msrs_to_save,
  2087. num_msrs_to_save * sizeof(u32)))
  2088. goto out;
  2089. if (copy_to_user(user_msr_list->indices
  2090. + num_msrs_to_save * sizeof(u32),
  2091. &emulated_msrs,
  2092. ARRAY_SIZE(emulated_msrs) * sizeof(u32)))
  2093. goto out;
  2094. r = 0;
  2095. break;
  2096. }
  2097. case KVM_CHECK_EXTENSION:
  2098. /*
  2099. * No extensions defined at present.
  2100. */
  2101. r = 0;
  2102. break;
  2103. default:
  2104. ;
  2105. }
  2106. out:
  2107. return r;
  2108. }
  2109. static struct file_operations kvm_chardev_ops = {
  2110. .open = kvm_dev_open,
  2111. .release = kvm_dev_release,
  2112. .unlocked_ioctl = kvm_dev_ioctl,
  2113. .compat_ioctl = kvm_dev_ioctl,
  2114. };
  2115. static struct miscdevice kvm_dev = {
  2116. KVM_MINOR,
  2117. "kvm",
  2118. &kvm_chardev_ops,
  2119. };
  2120. static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
  2121. void *v)
  2122. {
  2123. if (val == SYS_RESTART) {
  2124. /*
  2125. * Some (well, at least mine) BIOSes hang on reboot if
  2126. * in vmx root mode.
  2127. */
  2128. printk(KERN_INFO "kvm: exiting hardware virtualization\n");
  2129. on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
  2130. }
  2131. return NOTIFY_OK;
  2132. }
  2133. static struct notifier_block kvm_reboot_notifier = {
  2134. .notifier_call = kvm_reboot,
  2135. .priority = 0,
  2136. };
  2137. /*
  2138. * Make sure that a cpu that is being hot-unplugged does not have any vcpus
  2139. * cached on it.
  2140. */
  2141. static void decache_vcpus_on_cpu(int cpu)
  2142. {
  2143. struct kvm *vm;
  2144. struct kvm_vcpu *vcpu;
  2145. int i;
  2146. spin_lock(&kvm_lock);
  2147. list_for_each_entry(vm, &vm_list, vm_list)
  2148. for (i = 0; i < KVM_MAX_VCPUS; ++i) {
  2149. vcpu = &vm->vcpus[i];
  2150. /*
  2151. * If the vcpu is locked, then it is running on some
  2152. * other cpu and therefore it is not cached on the
  2153. * cpu in question.
  2154. *
  2155. * If it's not locked, check the last cpu it executed
  2156. * on.
  2157. */
  2158. if (mutex_trylock(&vcpu->mutex)) {
  2159. if (vcpu->cpu == cpu) {
  2160. kvm_arch_ops->vcpu_decache(vcpu);
  2161. vcpu->cpu = -1;
  2162. }
  2163. mutex_unlock(&vcpu->mutex);
  2164. }
  2165. }
  2166. spin_unlock(&kvm_lock);
  2167. }
  2168. static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val,
  2169. void *v)
  2170. {
  2171. int cpu = (long)v;
  2172. switch (val) {
  2173. case CPU_DOWN_PREPARE:
  2174. case CPU_UP_CANCELED:
  2175. printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
  2176. cpu);
  2177. decache_vcpus_on_cpu(cpu);
  2178. smp_call_function_single(cpu, kvm_arch_ops->hardware_disable,
  2179. NULL, 0, 1);
  2180. break;
  2181. case CPU_ONLINE:
  2182. printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
  2183. cpu);
  2184. smp_call_function_single(cpu, kvm_arch_ops->hardware_enable,
  2185. NULL, 0, 1);
  2186. break;
  2187. }
  2188. return NOTIFY_OK;
  2189. }
  2190. static struct notifier_block kvm_cpu_notifier = {
  2191. .notifier_call = kvm_cpu_hotplug,
  2192. .priority = 20, /* must be > scheduler priority */
  2193. };
  2194. static __init void kvm_init_debug(void)
  2195. {
  2196. struct kvm_stats_debugfs_item *p;
  2197. debugfs_dir = debugfs_create_dir("kvm", NULL);
  2198. for (p = debugfs_entries; p->name; ++p)
  2199. p->dentry = debugfs_create_u32(p->name, 0444, debugfs_dir,
  2200. p->data);
  2201. }
  2202. static void kvm_exit_debug(void)
  2203. {
  2204. struct kvm_stats_debugfs_item *p;
  2205. for (p = debugfs_entries; p->name; ++p)
  2206. debugfs_remove(p->dentry);
  2207. debugfs_remove(debugfs_dir);
  2208. }
  2209. static int kvm_suspend(struct sys_device *dev, pm_message_t state)
  2210. {
  2211. decache_vcpus_on_cpu(raw_smp_processor_id());
  2212. on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
  2213. return 0;
  2214. }
  2215. static int kvm_resume(struct sys_device *dev)
  2216. {
  2217. on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
  2218. return 0;
  2219. }
  2220. static struct sysdev_class kvm_sysdev_class = {
  2221. set_kset_name("kvm"),
  2222. .suspend = kvm_suspend,
  2223. .resume = kvm_resume,
  2224. };
  2225. static struct sys_device kvm_sysdev = {
  2226. .id = 0,
  2227. .cls = &kvm_sysdev_class,
  2228. };
  2229. hpa_t bad_page_address;
  2230. static int kvmfs_get_sb(struct file_system_type *fs_type, int flags,
  2231. const char *dev_name, void *data, struct vfsmount *mnt)
  2232. {
  2233. return get_sb_pseudo(fs_type, "kvm:", NULL, KVMFS_SUPER_MAGIC, mnt);
  2234. }
  2235. static struct file_system_type kvm_fs_type = {
  2236. .name = "kvmfs",
  2237. .get_sb = kvmfs_get_sb,
  2238. .kill_sb = kill_anon_super,
  2239. };
  2240. int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
  2241. {
  2242. int r;
  2243. if (kvm_arch_ops) {
  2244. printk(KERN_ERR "kvm: already loaded the other module\n");
  2245. return -EEXIST;
  2246. }
  2247. if (!ops->cpu_has_kvm_support()) {
  2248. printk(KERN_ERR "kvm: no hardware support\n");
  2249. return -EOPNOTSUPP;
  2250. }
  2251. if (ops->disabled_by_bios()) {
  2252. printk(KERN_ERR "kvm: disabled by bios\n");
  2253. return -EOPNOTSUPP;
  2254. }
  2255. kvm_arch_ops = ops;
  2256. r = kvm_arch_ops->hardware_setup();
  2257. if (r < 0)
  2258. goto out;
  2259. on_each_cpu(kvm_arch_ops->hardware_enable, NULL, 0, 1);
  2260. r = register_cpu_notifier(&kvm_cpu_notifier);
  2261. if (r)
  2262. goto out_free_1;
  2263. register_reboot_notifier(&kvm_reboot_notifier);
  2264. r = sysdev_class_register(&kvm_sysdev_class);
  2265. if (r)
  2266. goto out_free_2;
  2267. r = sysdev_register(&kvm_sysdev);
  2268. if (r)
  2269. goto out_free_3;
  2270. kvm_chardev_ops.owner = module;
  2271. r = misc_register(&kvm_dev);
  2272. if (r) {
  2273. printk (KERN_ERR "kvm: misc device register failed\n");
  2274. goto out_free;
  2275. }
  2276. return r;
  2277. out_free:
  2278. sysdev_unregister(&kvm_sysdev);
  2279. out_free_3:
  2280. sysdev_class_unregister(&kvm_sysdev_class);
  2281. out_free_2:
  2282. unregister_reboot_notifier(&kvm_reboot_notifier);
  2283. unregister_cpu_notifier(&kvm_cpu_notifier);
  2284. out_free_1:
  2285. on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
  2286. kvm_arch_ops->hardware_unsetup();
  2287. out:
  2288. kvm_arch_ops = NULL;
  2289. return r;
  2290. }
  2291. void kvm_exit_arch(void)
  2292. {
  2293. misc_deregister(&kvm_dev);
  2294. sysdev_unregister(&kvm_sysdev);
  2295. sysdev_class_unregister(&kvm_sysdev_class);
  2296. unregister_reboot_notifier(&kvm_reboot_notifier);
  2297. unregister_cpu_notifier(&kvm_cpu_notifier);
  2298. on_each_cpu(kvm_arch_ops->hardware_disable, NULL, 0, 1);
  2299. kvm_arch_ops->hardware_unsetup();
  2300. kvm_arch_ops = NULL;
  2301. }
  2302. static __init int kvm_init(void)
  2303. {
  2304. static struct page *bad_page;
  2305. int r;
  2306. r = register_filesystem(&kvm_fs_type);
  2307. if (r)
  2308. goto out3;
  2309. kvmfs_mnt = kern_mount(&kvm_fs_type);
  2310. r = PTR_ERR(kvmfs_mnt);
  2311. if (IS_ERR(kvmfs_mnt))
  2312. goto out2;
  2313. kvm_init_debug();
  2314. kvm_init_msr_list();
  2315. if ((bad_page = alloc_page(GFP_KERNEL)) == NULL) {
  2316. r = -ENOMEM;
  2317. goto out;
  2318. }
  2319. bad_page_address = page_to_pfn(bad_page) << PAGE_SHIFT;
  2320. memset(__va(bad_page_address), 0, PAGE_SIZE);
  2321. return 0;
  2322. out:
  2323. kvm_exit_debug();
  2324. mntput(kvmfs_mnt);
  2325. out2:
  2326. unregister_filesystem(&kvm_fs_type);
  2327. out3:
  2328. return r;
  2329. }
  2330. static __exit void kvm_exit(void)
  2331. {
  2332. kvm_exit_debug();
  2333. __free_page(pfn_to_page(bad_page_address >> PAGE_SHIFT));
  2334. mntput(kvmfs_mnt);
  2335. unregister_filesystem(&kvm_fs_type);
  2336. }
  2337. module_init(kvm_init)
  2338. module_exit(kvm_exit)
  2339. EXPORT_SYMBOL_GPL(kvm_init_arch);
  2340. EXPORT_SYMBOL_GPL(kvm_exit_arch);