kvm_main.c 68 KB

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