kvm_main.c 69 KB

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