kvm_main.c 69 KB

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