enlighten.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761
  1. /*
  2. * Core of Xen paravirt_ops implementation.
  3. *
  4. * This file contains the xen_paravirt_ops structure itself, and the
  5. * implementations for:
  6. * - privileged instructions
  7. * - interrupt flags
  8. * - segment operations
  9. * - booting and setup
  10. *
  11. * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/smp.h>
  16. #include <linux/preempt.h>
  17. #include <linux/hardirq.h>
  18. #include <linux/percpu.h>
  19. #include <linux/delay.h>
  20. #include <linux/start_kernel.h>
  21. #include <linux/sched.h>
  22. #include <linux/bootmem.h>
  23. #include <linux/module.h>
  24. #include <linux/mm.h>
  25. #include <linux/page-flags.h>
  26. #include <linux/highmem.h>
  27. #include <linux/console.h>
  28. #include <xen/interface/xen.h>
  29. #include <xen/interface/physdev.h>
  30. #include <xen/interface/vcpu.h>
  31. #include <xen/interface/sched.h>
  32. #include <xen/features.h>
  33. #include <xen/page.h>
  34. #include <xen/hvc-console.h>
  35. #include <asm/paravirt.h>
  36. #include <asm/page.h>
  37. #include <asm/xen/hypercall.h>
  38. #include <asm/xen/hypervisor.h>
  39. #include <asm/fixmap.h>
  40. #include <asm/processor.h>
  41. #include <asm/msr-index.h>
  42. #include <asm/setup.h>
  43. #include <asm/desc.h>
  44. #include <asm/pgtable.h>
  45. #include <asm/tlbflush.h>
  46. #include <asm/reboot.h>
  47. #include "xen-ops.h"
  48. #include "mmu.h"
  49. #include "multicalls.h"
  50. EXPORT_SYMBOL_GPL(hypercall_page);
  51. DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
  52. DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
  53. /*
  54. * Identity map, in addition to plain kernel map. This needs to be
  55. * large enough to allocate page table pages to allocate the rest.
  56. * Each page can map 2MB.
  57. */
  58. static pte_t level1_ident_pgt[PTRS_PER_PTE * 4] __page_aligned_bss;
  59. #ifdef CONFIG_X86_64
  60. /* l3 pud for userspace vsyscall mapping */
  61. static pud_t level3_user_vsyscall[PTRS_PER_PUD] __page_aligned_bss;
  62. #endif /* CONFIG_X86_64 */
  63. /*
  64. * Note about cr3 (pagetable base) values:
  65. *
  66. * xen_cr3 contains the current logical cr3 value; it contains the
  67. * last set cr3. This may not be the current effective cr3, because
  68. * its update may be being lazily deferred. However, a vcpu looking
  69. * at its own cr3 can use this value knowing that it everything will
  70. * be self-consistent.
  71. *
  72. * xen_current_cr3 contains the actual vcpu cr3; it is set once the
  73. * hypercall to set the vcpu cr3 is complete (so it may be a little
  74. * out of date, but it will never be set early). If one vcpu is
  75. * looking at another vcpu's cr3 value, it should use this variable.
  76. */
  77. DEFINE_PER_CPU(unsigned long, xen_cr3); /* cr3 stored as physaddr */
  78. DEFINE_PER_CPU(unsigned long, xen_current_cr3); /* actual vcpu cr3 */
  79. struct start_info *xen_start_info;
  80. EXPORT_SYMBOL_GPL(xen_start_info);
  81. struct shared_info xen_dummy_shared_info;
  82. /*
  83. * Point at some empty memory to start with. We map the real shared_info
  84. * page as soon as fixmap is up and running.
  85. */
  86. struct shared_info *HYPERVISOR_shared_info = (void *)&xen_dummy_shared_info;
  87. /*
  88. * Flag to determine whether vcpu info placement is available on all
  89. * VCPUs. We assume it is to start with, and then set it to zero on
  90. * the first failure. This is because it can succeed on some VCPUs
  91. * and not others, since it can involve hypervisor memory allocation,
  92. * or because the guest failed to guarantee all the appropriate
  93. * constraints on all VCPUs (ie buffer can't cross a page boundary).
  94. *
  95. * Note that any particular CPU may be using a placed vcpu structure,
  96. * but we can only optimise if the all are.
  97. *
  98. * 0: not available, 1: available
  99. */
  100. static int have_vcpu_info_placement = 1;
  101. static void xen_vcpu_setup(int cpu)
  102. {
  103. struct vcpu_register_vcpu_info info;
  104. int err;
  105. struct vcpu_info *vcpup;
  106. BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
  107. per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
  108. if (!have_vcpu_info_placement)
  109. return; /* already tested, not available */
  110. vcpup = &per_cpu(xen_vcpu_info, cpu);
  111. info.mfn = virt_to_mfn(vcpup);
  112. info.offset = offset_in_page(vcpup);
  113. printk(KERN_DEBUG "trying to map vcpu_info %d at %p, mfn %llx, offset %d\n",
  114. cpu, vcpup, info.mfn, info.offset);
  115. /* Check to see if the hypervisor will put the vcpu_info
  116. structure where we want it, which allows direct access via
  117. a percpu-variable. */
  118. err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
  119. if (err) {
  120. printk(KERN_DEBUG "register_vcpu_info failed: err=%d\n", err);
  121. have_vcpu_info_placement = 0;
  122. } else {
  123. /* This cpu is using the registered vcpu info, even if
  124. later ones fail to. */
  125. per_cpu(xen_vcpu, cpu) = vcpup;
  126. printk(KERN_DEBUG "cpu %d using vcpu_info at %p\n",
  127. cpu, vcpup);
  128. }
  129. }
  130. /*
  131. * On restore, set the vcpu placement up again.
  132. * If it fails, then we're in a bad state, since
  133. * we can't back out from using it...
  134. */
  135. void xen_vcpu_restore(void)
  136. {
  137. if (have_vcpu_info_placement) {
  138. int cpu;
  139. for_each_online_cpu(cpu) {
  140. bool other_cpu = (cpu != smp_processor_id());
  141. if (other_cpu &&
  142. HYPERVISOR_vcpu_op(VCPUOP_down, cpu, NULL))
  143. BUG();
  144. xen_vcpu_setup(cpu);
  145. if (other_cpu &&
  146. HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL))
  147. BUG();
  148. }
  149. BUG_ON(!have_vcpu_info_placement);
  150. }
  151. }
  152. static void __init xen_banner(void)
  153. {
  154. unsigned version = HYPERVISOR_xen_version(XENVER_version, NULL);
  155. struct xen_extraversion extra;
  156. HYPERVISOR_xen_version(XENVER_extraversion, &extra);
  157. printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
  158. pv_info.name);
  159. printk(KERN_INFO "Xen version: %d.%d%s%s\n",
  160. version >> 16, version & 0xffff, extra.extraversion,
  161. xen_feature(XENFEAT_mmu_pt_update_preserve_ad) ? " (preserve-AD)" : "");
  162. }
  163. static void xen_cpuid(unsigned int *ax, unsigned int *bx,
  164. unsigned int *cx, unsigned int *dx)
  165. {
  166. unsigned maskedx = ~0;
  167. /*
  168. * Mask out inconvenient features, to try and disable as many
  169. * unsupported kernel subsystems as possible.
  170. */
  171. if (*ax == 1)
  172. maskedx = ~((1 << X86_FEATURE_APIC) | /* disable APIC */
  173. (1 << X86_FEATURE_ACPI) | /* disable ACPI */
  174. (1 << X86_FEATURE_MCE) | /* disable MCE */
  175. (1 << X86_FEATURE_MCA) | /* disable MCA */
  176. (1 << X86_FEATURE_ACC)); /* thermal monitoring */
  177. asm(XEN_EMULATE_PREFIX "cpuid"
  178. : "=a" (*ax),
  179. "=b" (*bx),
  180. "=c" (*cx),
  181. "=d" (*dx)
  182. : "0" (*ax), "2" (*cx));
  183. *dx &= maskedx;
  184. }
  185. static void xen_set_debugreg(int reg, unsigned long val)
  186. {
  187. HYPERVISOR_set_debugreg(reg, val);
  188. }
  189. static unsigned long xen_get_debugreg(int reg)
  190. {
  191. return HYPERVISOR_get_debugreg(reg);
  192. }
  193. static unsigned long xen_save_fl(void)
  194. {
  195. struct vcpu_info *vcpu;
  196. unsigned long flags;
  197. vcpu = x86_read_percpu(xen_vcpu);
  198. /* flag has opposite sense of mask */
  199. flags = !vcpu->evtchn_upcall_mask;
  200. /* convert to IF type flag
  201. -0 -> 0x00000000
  202. -1 -> 0xffffffff
  203. */
  204. return (-flags) & X86_EFLAGS_IF;
  205. }
  206. static void xen_restore_fl(unsigned long flags)
  207. {
  208. struct vcpu_info *vcpu;
  209. /* convert from IF type flag */
  210. flags = !(flags & X86_EFLAGS_IF);
  211. /* There's a one instruction preempt window here. We need to
  212. make sure we're don't switch CPUs between getting the vcpu
  213. pointer and updating the mask. */
  214. preempt_disable();
  215. vcpu = x86_read_percpu(xen_vcpu);
  216. vcpu->evtchn_upcall_mask = flags;
  217. preempt_enable_no_resched();
  218. /* Doesn't matter if we get preempted here, because any
  219. pending event will get dealt with anyway. */
  220. if (flags == 0) {
  221. preempt_check_resched();
  222. barrier(); /* unmask then check (avoid races) */
  223. if (unlikely(vcpu->evtchn_upcall_pending))
  224. force_evtchn_callback();
  225. }
  226. }
  227. static void xen_irq_disable(void)
  228. {
  229. /* There's a one instruction preempt window here. We need to
  230. make sure we're don't switch CPUs between getting the vcpu
  231. pointer and updating the mask. */
  232. preempt_disable();
  233. x86_read_percpu(xen_vcpu)->evtchn_upcall_mask = 1;
  234. preempt_enable_no_resched();
  235. }
  236. static void xen_irq_enable(void)
  237. {
  238. struct vcpu_info *vcpu;
  239. /* We don't need to worry about being preempted here, since
  240. either a) interrupts are disabled, so no preemption, or b)
  241. the caller is confused and is trying to re-enable interrupts
  242. on an indeterminate processor. */
  243. vcpu = x86_read_percpu(xen_vcpu);
  244. vcpu->evtchn_upcall_mask = 0;
  245. /* Doesn't matter if we get preempted here, because any
  246. pending event will get dealt with anyway. */
  247. barrier(); /* unmask then check (avoid races) */
  248. if (unlikely(vcpu->evtchn_upcall_pending))
  249. force_evtchn_callback();
  250. }
  251. static void xen_safe_halt(void)
  252. {
  253. /* Blocking includes an implicit local_irq_enable(). */
  254. if (HYPERVISOR_sched_op(SCHEDOP_block, NULL) != 0)
  255. BUG();
  256. }
  257. static void xen_halt(void)
  258. {
  259. if (irqs_disabled())
  260. HYPERVISOR_vcpu_op(VCPUOP_down, smp_processor_id(), NULL);
  261. else
  262. xen_safe_halt();
  263. }
  264. static void xen_leave_lazy(void)
  265. {
  266. paravirt_leave_lazy(paravirt_get_lazy_mode());
  267. xen_mc_flush();
  268. }
  269. static unsigned long xen_store_tr(void)
  270. {
  271. return 0;
  272. }
  273. static void xen_set_ldt(const void *addr, unsigned entries)
  274. {
  275. struct mmuext_op *op;
  276. struct multicall_space mcs = xen_mc_entry(sizeof(*op));
  277. op = mcs.args;
  278. op->cmd = MMUEXT_SET_LDT;
  279. op->arg1.linear_addr = (unsigned long)addr;
  280. op->arg2.nr_ents = entries;
  281. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  282. xen_mc_issue(PARAVIRT_LAZY_CPU);
  283. }
  284. static void xen_load_gdt(const struct desc_ptr *dtr)
  285. {
  286. unsigned long *frames;
  287. unsigned long va = dtr->address;
  288. unsigned int size = dtr->size + 1;
  289. unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
  290. int f;
  291. struct multicall_space mcs;
  292. /* A GDT can be up to 64k in size, which corresponds to 8192
  293. 8-byte entries, or 16 4k pages.. */
  294. BUG_ON(size > 65536);
  295. BUG_ON(va & ~PAGE_MASK);
  296. mcs = xen_mc_entry(sizeof(*frames) * pages);
  297. frames = mcs.args;
  298. for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
  299. frames[f] = virt_to_mfn(va);
  300. make_lowmem_page_readonly((void *)va);
  301. }
  302. MULTI_set_gdt(mcs.mc, frames, size / sizeof(struct desc_struct));
  303. xen_mc_issue(PARAVIRT_LAZY_CPU);
  304. }
  305. static void load_TLS_descriptor(struct thread_struct *t,
  306. unsigned int cpu, unsigned int i)
  307. {
  308. struct desc_struct *gdt = get_cpu_gdt_table(cpu);
  309. xmaddr_t maddr = virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
  310. struct multicall_space mc = __xen_mc_entry(0);
  311. MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
  312. }
  313. static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
  314. {
  315. /*
  316. * XXX sleazy hack: If we're being called in a lazy-cpu zone,
  317. * it means we're in a context switch, and %gs has just been
  318. * saved. This means we can zero it out to prevent faults on
  319. * exit from the hypervisor if the next process has no %gs.
  320. * Either way, it has been saved, and the new value will get
  321. * loaded properly. This will go away as soon as Xen has been
  322. * modified to not save/restore %gs for normal hypercalls.
  323. *
  324. * On x86_64, this hack is not used for %gs, because gs points
  325. * to KERNEL_GS_BASE (and uses it for PDA references), so we
  326. * must not zero %gs on x86_64
  327. *
  328. * For x86_64, we need to zero %fs, otherwise we may get an
  329. * exception between the new %fs descriptor being loaded and
  330. * %fs being effectively cleared at __switch_to().
  331. */
  332. if (paravirt_get_lazy_mode() == PARAVIRT_LAZY_CPU) {
  333. #ifdef CONFIG_X86_32
  334. loadsegment(gs, 0);
  335. #else
  336. loadsegment(fs, 0);
  337. #endif
  338. }
  339. xen_mc_batch();
  340. load_TLS_descriptor(t, cpu, 0);
  341. load_TLS_descriptor(t, cpu, 1);
  342. load_TLS_descriptor(t, cpu, 2);
  343. xen_mc_issue(PARAVIRT_LAZY_CPU);
  344. }
  345. #ifdef CONFIG_X86_64
  346. static void xen_load_gs_index(unsigned int idx)
  347. {
  348. if (HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, idx))
  349. BUG();
  350. }
  351. #endif
  352. static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
  353. const void *ptr)
  354. {
  355. unsigned long lp = (unsigned long)&dt[entrynum];
  356. xmaddr_t mach_lp = virt_to_machine(lp);
  357. u64 entry = *(u64 *)ptr;
  358. preempt_disable();
  359. xen_mc_flush();
  360. if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
  361. BUG();
  362. preempt_enable();
  363. }
  364. static int cvt_gate_to_trap(int vector, const gate_desc *val,
  365. struct trap_info *info)
  366. {
  367. if (val->type != 0xf && val->type != 0xe)
  368. return 0;
  369. info->vector = vector;
  370. info->address = gate_offset(*val);
  371. info->cs = gate_segment(*val);
  372. info->flags = val->dpl;
  373. /* interrupt gates clear IF */
  374. if (val->type == 0xe)
  375. info->flags |= 4;
  376. return 1;
  377. }
  378. /* Locations of each CPU's IDT */
  379. static DEFINE_PER_CPU(struct desc_ptr, idt_desc);
  380. /* Set an IDT entry. If the entry is part of the current IDT, then
  381. also update Xen. */
  382. static void xen_write_idt_entry(gate_desc *dt, int entrynum, const gate_desc *g)
  383. {
  384. unsigned long p = (unsigned long)&dt[entrynum];
  385. unsigned long start, end;
  386. preempt_disable();
  387. start = __get_cpu_var(idt_desc).address;
  388. end = start + __get_cpu_var(idt_desc).size + 1;
  389. xen_mc_flush();
  390. native_write_idt_entry(dt, entrynum, g);
  391. if (p >= start && (p + 8) <= end) {
  392. struct trap_info info[2];
  393. info[1].address = 0;
  394. if (cvt_gate_to_trap(entrynum, g, &info[0]))
  395. if (HYPERVISOR_set_trap_table(info))
  396. BUG();
  397. }
  398. preempt_enable();
  399. }
  400. static void xen_convert_trap_info(const struct desc_ptr *desc,
  401. struct trap_info *traps)
  402. {
  403. unsigned in, out, count;
  404. count = (desc->size+1) / sizeof(gate_desc);
  405. BUG_ON(count > 256);
  406. for (in = out = 0; in < count; in++) {
  407. gate_desc *entry = (gate_desc*)(desc->address) + in;
  408. if (cvt_gate_to_trap(in, entry, &traps[out]))
  409. out++;
  410. }
  411. traps[out].address = 0;
  412. }
  413. void xen_copy_trap_info(struct trap_info *traps)
  414. {
  415. const struct desc_ptr *desc = &__get_cpu_var(idt_desc);
  416. xen_convert_trap_info(desc, traps);
  417. }
  418. /* Load a new IDT into Xen. In principle this can be per-CPU, so we
  419. hold a spinlock to protect the static traps[] array (static because
  420. it avoids allocation, and saves stack space). */
  421. static void xen_load_idt(const struct desc_ptr *desc)
  422. {
  423. static DEFINE_SPINLOCK(lock);
  424. static struct trap_info traps[257];
  425. spin_lock(&lock);
  426. __get_cpu_var(idt_desc) = *desc;
  427. xen_convert_trap_info(desc, traps);
  428. xen_mc_flush();
  429. if (HYPERVISOR_set_trap_table(traps))
  430. BUG();
  431. spin_unlock(&lock);
  432. }
  433. /* Write a GDT descriptor entry. Ignore LDT descriptors, since
  434. they're handled differently. */
  435. static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
  436. const void *desc, int type)
  437. {
  438. preempt_disable();
  439. switch (type) {
  440. case DESC_LDT:
  441. case DESC_TSS:
  442. /* ignore */
  443. break;
  444. default: {
  445. xmaddr_t maddr = virt_to_machine(&dt[entry]);
  446. xen_mc_flush();
  447. if (HYPERVISOR_update_descriptor(maddr.maddr, *(u64 *)desc))
  448. BUG();
  449. }
  450. }
  451. preempt_enable();
  452. }
  453. static void xen_load_sp0(struct tss_struct *tss,
  454. struct thread_struct *thread)
  455. {
  456. struct multicall_space mcs = xen_mc_entry(0);
  457. MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->sp0);
  458. xen_mc_issue(PARAVIRT_LAZY_CPU);
  459. }
  460. static void xen_set_iopl_mask(unsigned mask)
  461. {
  462. struct physdev_set_iopl set_iopl;
  463. /* Force the change at ring 0. */
  464. set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
  465. HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
  466. }
  467. static void xen_io_delay(void)
  468. {
  469. }
  470. #ifdef CONFIG_X86_LOCAL_APIC
  471. static u32 xen_apic_read(unsigned long reg)
  472. {
  473. return 0;
  474. }
  475. static void xen_apic_write(unsigned long reg, u32 val)
  476. {
  477. /* Warn to see if there's any stray references */
  478. WARN_ON(1);
  479. }
  480. #endif
  481. static void xen_flush_tlb(void)
  482. {
  483. struct mmuext_op *op;
  484. struct multicall_space mcs;
  485. preempt_disable();
  486. mcs = xen_mc_entry(sizeof(*op));
  487. op = mcs.args;
  488. op->cmd = MMUEXT_TLB_FLUSH_LOCAL;
  489. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  490. xen_mc_issue(PARAVIRT_LAZY_MMU);
  491. preempt_enable();
  492. }
  493. static void xen_flush_tlb_single(unsigned long addr)
  494. {
  495. struct mmuext_op *op;
  496. struct multicall_space mcs;
  497. preempt_disable();
  498. mcs = xen_mc_entry(sizeof(*op));
  499. op = mcs.args;
  500. op->cmd = MMUEXT_INVLPG_LOCAL;
  501. op->arg1.linear_addr = addr & PAGE_MASK;
  502. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  503. xen_mc_issue(PARAVIRT_LAZY_MMU);
  504. preempt_enable();
  505. }
  506. static void xen_flush_tlb_others(const cpumask_t *cpus, struct mm_struct *mm,
  507. unsigned long va)
  508. {
  509. struct {
  510. struct mmuext_op op;
  511. cpumask_t mask;
  512. } *args;
  513. cpumask_t cpumask = *cpus;
  514. struct multicall_space mcs;
  515. /*
  516. * A couple of (to be removed) sanity checks:
  517. *
  518. * - current CPU must not be in mask
  519. * - mask must exist :)
  520. */
  521. BUG_ON(cpus_empty(cpumask));
  522. BUG_ON(cpu_isset(smp_processor_id(), cpumask));
  523. BUG_ON(!mm);
  524. /* If a CPU which we ran on has gone down, OK. */
  525. cpus_and(cpumask, cpumask, cpu_online_map);
  526. if (cpus_empty(cpumask))
  527. return;
  528. mcs = xen_mc_entry(sizeof(*args));
  529. args = mcs.args;
  530. args->mask = cpumask;
  531. args->op.arg2.vcpumask = &args->mask;
  532. if (va == TLB_FLUSH_ALL) {
  533. args->op.cmd = MMUEXT_TLB_FLUSH_MULTI;
  534. } else {
  535. args->op.cmd = MMUEXT_INVLPG_MULTI;
  536. args->op.arg1.linear_addr = va;
  537. }
  538. MULTI_mmuext_op(mcs.mc, &args->op, 1, NULL, DOMID_SELF);
  539. xen_mc_issue(PARAVIRT_LAZY_MMU);
  540. }
  541. static void xen_clts(void)
  542. {
  543. struct multicall_space mcs;
  544. mcs = xen_mc_entry(0);
  545. MULTI_fpu_taskswitch(mcs.mc, 0);
  546. xen_mc_issue(PARAVIRT_LAZY_CPU);
  547. }
  548. static void xen_write_cr0(unsigned long cr0)
  549. {
  550. struct multicall_space mcs;
  551. /* Only pay attention to cr0.TS; everything else is
  552. ignored. */
  553. mcs = xen_mc_entry(0);
  554. MULTI_fpu_taskswitch(mcs.mc, (cr0 & X86_CR0_TS) != 0);
  555. xen_mc_issue(PARAVIRT_LAZY_CPU);
  556. }
  557. static void xen_write_cr2(unsigned long cr2)
  558. {
  559. x86_read_percpu(xen_vcpu)->arch.cr2 = cr2;
  560. }
  561. static unsigned long xen_read_cr2(void)
  562. {
  563. return x86_read_percpu(xen_vcpu)->arch.cr2;
  564. }
  565. static unsigned long xen_read_cr2_direct(void)
  566. {
  567. return x86_read_percpu(xen_vcpu_info.arch.cr2);
  568. }
  569. static void xen_write_cr4(unsigned long cr4)
  570. {
  571. cr4 &= ~X86_CR4_PGE;
  572. cr4 &= ~X86_CR4_PSE;
  573. native_write_cr4(cr4);
  574. }
  575. static unsigned long xen_read_cr3(void)
  576. {
  577. return x86_read_percpu(xen_cr3);
  578. }
  579. static void set_current_cr3(void *v)
  580. {
  581. x86_write_percpu(xen_current_cr3, (unsigned long)v);
  582. }
  583. static void __xen_write_cr3(bool kernel, unsigned long cr3)
  584. {
  585. struct mmuext_op *op;
  586. struct multicall_space mcs;
  587. unsigned long mfn;
  588. if (cr3)
  589. mfn = pfn_to_mfn(PFN_DOWN(cr3));
  590. else
  591. mfn = 0;
  592. WARN_ON(mfn == 0 && kernel);
  593. mcs = __xen_mc_entry(sizeof(*op));
  594. op = mcs.args;
  595. op->cmd = kernel ? MMUEXT_NEW_BASEPTR : MMUEXT_NEW_USER_BASEPTR;
  596. op->arg1.mfn = mfn;
  597. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  598. if (kernel) {
  599. x86_write_percpu(xen_cr3, cr3);
  600. /* Update xen_current_cr3 once the batch has actually
  601. been submitted. */
  602. xen_mc_callback(set_current_cr3, (void *)cr3);
  603. }
  604. }
  605. static void xen_write_cr3(unsigned long cr3)
  606. {
  607. BUG_ON(preemptible());
  608. xen_mc_batch(); /* disables interrupts */
  609. /* Update while interrupts are disabled, so its atomic with
  610. respect to ipis */
  611. x86_write_percpu(xen_cr3, cr3);
  612. __xen_write_cr3(true, cr3);
  613. #ifdef CONFIG_X86_64
  614. {
  615. pgd_t *user_pgd = xen_get_user_pgd(__va(cr3));
  616. if (user_pgd)
  617. __xen_write_cr3(false, __pa(user_pgd));
  618. else
  619. __xen_write_cr3(false, 0);
  620. }
  621. #endif
  622. xen_mc_issue(PARAVIRT_LAZY_CPU); /* interrupts restored */
  623. }
  624. static int xen_write_msr_safe(unsigned int msr, unsigned low, unsigned high)
  625. {
  626. int ret;
  627. ret = 0;
  628. switch(msr) {
  629. #ifdef CONFIG_X86_64
  630. unsigned which;
  631. u64 base;
  632. case MSR_FS_BASE: which = SEGBASE_FS; goto set;
  633. case MSR_KERNEL_GS_BASE: which = SEGBASE_GS_USER; goto set;
  634. case MSR_GS_BASE: which = SEGBASE_GS_KERNEL; goto set;
  635. set:
  636. base = ((u64)high << 32) | low;
  637. if (HYPERVISOR_set_segment_base(which, base) != 0)
  638. ret = -EFAULT;
  639. break;
  640. #endif
  641. default:
  642. ret = native_write_msr_safe(msr, low, high);
  643. }
  644. return ret;
  645. }
  646. /* Early in boot, while setting up the initial pagetable, assume
  647. everything is pinned. */
  648. static __init void xen_alloc_pte_init(struct mm_struct *mm, unsigned long pfn)
  649. {
  650. #ifdef CONFIG_FLATMEM
  651. BUG_ON(mem_map); /* should only be used early */
  652. #endif
  653. make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
  654. }
  655. /* Early release_pte assumes that all pts are pinned, since there's
  656. only init_mm and anything attached to that is pinned. */
  657. static void xen_release_pte_init(unsigned long pfn)
  658. {
  659. make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
  660. }
  661. static void pin_pagetable_pfn(unsigned cmd, unsigned long pfn)
  662. {
  663. struct mmuext_op op;
  664. op.cmd = cmd;
  665. op.arg1.mfn = pfn_to_mfn(pfn);
  666. if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF))
  667. BUG();
  668. }
  669. /* This needs to make sure the new pte page is pinned iff its being
  670. attached to a pinned pagetable. */
  671. static void xen_alloc_ptpage(struct mm_struct *mm, unsigned long pfn, unsigned level)
  672. {
  673. struct page *page = pfn_to_page(pfn);
  674. if (PagePinned(virt_to_page(mm->pgd))) {
  675. SetPagePinned(page);
  676. if (!PageHighMem(page)) {
  677. make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
  678. if (level == PT_PTE)
  679. pin_pagetable_pfn(MMUEXT_PIN_L1_TABLE, pfn);
  680. } else
  681. /* make sure there are no stray mappings of
  682. this page */
  683. kmap_flush_unused();
  684. }
  685. }
  686. static void xen_alloc_pte(struct mm_struct *mm, unsigned long pfn)
  687. {
  688. xen_alloc_ptpage(mm, pfn, PT_PTE);
  689. }
  690. static void xen_alloc_pmd(struct mm_struct *mm, unsigned long pfn)
  691. {
  692. xen_alloc_ptpage(mm, pfn, PT_PMD);
  693. }
  694. static int xen_pgd_alloc(struct mm_struct *mm)
  695. {
  696. pgd_t *pgd = mm->pgd;
  697. int ret = 0;
  698. BUG_ON(PagePinned(virt_to_page(pgd)));
  699. #ifdef CONFIG_X86_64
  700. {
  701. struct page *page = virt_to_page(pgd);
  702. pgd_t *user_pgd;
  703. BUG_ON(page->private != 0);
  704. ret = -ENOMEM;
  705. user_pgd = (pgd_t *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
  706. page->private = (unsigned long)user_pgd;
  707. if (user_pgd != NULL) {
  708. user_pgd[pgd_index(VSYSCALL_START)] =
  709. __pgd(__pa(level3_user_vsyscall) | _PAGE_TABLE);
  710. ret = 0;
  711. }
  712. BUG_ON(PagePinned(virt_to_page(xen_get_user_pgd(pgd))));
  713. }
  714. #endif
  715. return ret;
  716. }
  717. static void xen_pgd_free(struct mm_struct *mm, pgd_t *pgd)
  718. {
  719. #ifdef CONFIG_X86_64
  720. pgd_t *user_pgd = xen_get_user_pgd(pgd);
  721. if (user_pgd)
  722. free_page((unsigned long)user_pgd);
  723. #endif
  724. }
  725. /* This should never happen until we're OK to use struct page */
  726. static void xen_release_ptpage(unsigned long pfn, unsigned level)
  727. {
  728. struct page *page = pfn_to_page(pfn);
  729. if (PagePinned(page)) {
  730. if (!PageHighMem(page)) {
  731. if (level == PT_PTE)
  732. pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, pfn);
  733. make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
  734. }
  735. ClearPagePinned(page);
  736. }
  737. }
  738. static void xen_release_pte(unsigned long pfn)
  739. {
  740. xen_release_ptpage(pfn, PT_PTE);
  741. }
  742. static void xen_release_pmd(unsigned long pfn)
  743. {
  744. xen_release_ptpage(pfn, PT_PMD);
  745. }
  746. #if PAGETABLE_LEVELS == 4
  747. static void xen_alloc_pud(struct mm_struct *mm, unsigned long pfn)
  748. {
  749. xen_alloc_ptpage(mm, pfn, PT_PUD);
  750. }
  751. static void xen_release_pud(unsigned long pfn)
  752. {
  753. xen_release_ptpage(pfn, PT_PUD);
  754. }
  755. #endif
  756. #ifdef CONFIG_HIGHPTE
  757. static void *xen_kmap_atomic_pte(struct page *page, enum km_type type)
  758. {
  759. pgprot_t prot = PAGE_KERNEL;
  760. if (PagePinned(page))
  761. prot = PAGE_KERNEL_RO;
  762. if (0 && PageHighMem(page))
  763. printk("mapping highpte %lx type %d prot %s\n",
  764. page_to_pfn(page), type,
  765. (unsigned long)pgprot_val(prot) & _PAGE_RW ? "WRITE" : "READ");
  766. return kmap_atomic_prot(page, type, prot);
  767. }
  768. #endif
  769. static __init pte_t mask_rw_pte(pte_t *ptep, pte_t pte)
  770. {
  771. /* If there's an existing pte, then don't allow _PAGE_RW to be set */
  772. if (pte_val_ma(*ptep) & _PAGE_PRESENT)
  773. pte = __pte_ma(((pte_val_ma(*ptep) & _PAGE_RW) | ~_PAGE_RW) &
  774. pte_val_ma(pte));
  775. return pte;
  776. }
  777. /* Init-time set_pte while constructing initial pagetables, which
  778. doesn't allow RO pagetable pages to be remapped RW */
  779. static __init void xen_set_pte_init(pte_t *ptep, pte_t pte)
  780. {
  781. pte = mask_rw_pte(ptep, pte);
  782. xen_set_pte(ptep, pte);
  783. }
  784. static __init void xen_pagetable_setup_start(pgd_t *base)
  785. {
  786. }
  787. void xen_setup_shared_info(void)
  788. {
  789. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  790. set_fixmap(FIX_PARAVIRT_BOOTMAP,
  791. xen_start_info->shared_info);
  792. HYPERVISOR_shared_info =
  793. (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
  794. } else
  795. HYPERVISOR_shared_info =
  796. (struct shared_info *)__va(xen_start_info->shared_info);
  797. #ifndef CONFIG_SMP
  798. /* In UP this is as good a place as any to set up shared info */
  799. xen_setup_vcpu_info_placement();
  800. #endif
  801. xen_setup_mfn_list_list();
  802. }
  803. static __init void xen_pagetable_setup_done(pgd_t *base)
  804. {
  805. xen_setup_shared_info();
  806. }
  807. static __init void xen_post_allocator_init(void)
  808. {
  809. pv_mmu_ops.set_pte = xen_set_pte;
  810. pv_mmu_ops.set_pmd = xen_set_pmd;
  811. pv_mmu_ops.set_pud = xen_set_pud;
  812. #if PAGETABLE_LEVELS == 4
  813. pv_mmu_ops.set_pgd = xen_set_pgd;
  814. #endif
  815. /* This will work as long as patching hasn't happened yet
  816. (which it hasn't) */
  817. pv_mmu_ops.alloc_pte = xen_alloc_pte;
  818. pv_mmu_ops.alloc_pmd = xen_alloc_pmd;
  819. pv_mmu_ops.release_pte = xen_release_pte;
  820. pv_mmu_ops.release_pmd = xen_release_pmd;
  821. #if PAGETABLE_LEVELS == 4
  822. pv_mmu_ops.alloc_pud = xen_alloc_pud;
  823. pv_mmu_ops.release_pud = xen_release_pud;
  824. #endif
  825. #ifdef CONFIG_X86_64
  826. SetPagePinned(virt_to_page(level3_user_vsyscall));
  827. #endif
  828. xen_mark_init_mm_pinned();
  829. }
  830. /* This is called once we have the cpu_possible_map */
  831. void xen_setup_vcpu_info_placement(void)
  832. {
  833. int cpu;
  834. for_each_possible_cpu(cpu)
  835. xen_vcpu_setup(cpu);
  836. /* xen_vcpu_setup managed to place the vcpu_info within the
  837. percpu area for all cpus, so make use of it */
  838. #ifdef CONFIG_X86_32
  839. if (have_vcpu_info_placement) {
  840. printk(KERN_INFO "Xen: using vcpu_info placement\n");
  841. pv_irq_ops.save_fl = xen_save_fl_direct;
  842. pv_irq_ops.restore_fl = xen_restore_fl_direct;
  843. pv_irq_ops.irq_disable = xen_irq_disable_direct;
  844. pv_irq_ops.irq_enable = xen_irq_enable_direct;
  845. pv_mmu_ops.read_cr2 = xen_read_cr2_direct;
  846. }
  847. #endif
  848. }
  849. static unsigned xen_patch(u8 type, u16 clobbers, void *insnbuf,
  850. unsigned long addr, unsigned len)
  851. {
  852. char *start, *end, *reloc;
  853. unsigned ret;
  854. start = end = reloc = NULL;
  855. #define SITE(op, x) \
  856. case PARAVIRT_PATCH(op.x): \
  857. if (have_vcpu_info_placement) { \
  858. start = (char *)xen_##x##_direct; \
  859. end = xen_##x##_direct_end; \
  860. reloc = xen_##x##_direct_reloc; \
  861. } \
  862. goto patch_site
  863. switch (type) {
  864. #ifdef CONFIG_X86_32
  865. SITE(pv_irq_ops, irq_enable);
  866. SITE(pv_irq_ops, irq_disable);
  867. SITE(pv_irq_ops, save_fl);
  868. SITE(pv_irq_ops, restore_fl);
  869. #endif /* CONFIG_X86_32 */
  870. #undef SITE
  871. patch_site:
  872. if (start == NULL || (end-start) > len)
  873. goto default_patch;
  874. ret = paravirt_patch_insns(insnbuf, len, start, end);
  875. /* Note: because reloc is assigned from something that
  876. appears to be an array, gcc assumes it's non-null,
  877. but doesn't know its relationship with start and
  878. end. */
  879. if (reloc > start && reloc < end) {
  880. int reloc_off = reloc - start;
  881. long *relocp = (long *)(insnbuf + reloc_off);
  882. long delta = start - (char *)addr;
  883. *relocp += delta;
  884. }
  885. break;
  886. default_patch:
  887. default:
  888. ret = paravirt_patch_default(type, clobbers, insnbuf,
  889. addr, len);
  890. break;
  891. }
  892. return ret;
  893. }
  894. static void xen_set_fixmap(unsigned idx, unsigned long phys, pgprot_t prot)
  895. {
  896. pte_t pte;
  897. phys >>= PAGE_SHIFT;
  898. switch (idx) {
  899. case FIX_BTMAP_END ... FIX_BTMAP_BEGIN:
  900. #ifdef CONFIG_X86_F00F_BUG
  901. case FIX_F00F_IDT:
  902. #endif
  903. #ifdef CONFIG_X86_32
  904. case FIX_WP_TEST:
  905. case FIX_VDSO:
  906. # ifdef CONFIG_HIGHMEM
  907. case FIX_KMAP_BEGIN ... FIX_KMAP_END:
  908. # endif
  909. #else
  910. case VSYSCALL_LAST_PAGE ... VSYSCALL_FIRST_PAGE:
  911. #endif
  912. #ifdef CONFIG_X86_LOCAL_APIC
  913. case FIX_APIC_BASE: /* maps dummy local APIC */
  914. #endif
  915. pte = pfn_pte(phys, prot);
  916. break;
  917. default:
  918. pte = mfn_pte(phys, prot);
  919. break;
  920. }
  921. __native_set_fixmap(idx, pte);
  922. #ifdef CONFIG_X86_64
  923. /* Replicate changes to map the vsyscall page into the user
  924. pagetable vsyscall mapping. */
  925. if (idx >= VSYSCALL_LAST_PAGE && idx <= VSYSCALL_FIRST_PAGE) {
  926. unsigned long vaddr = __fix_to_virt(idx);
  927. set_pte_vaddr_pud(level3_user_vsyscall, vaddr, pte);
  928. }
  929. #endif
  930. }
  931. static const struct pv_info xen_info __initdata = {
  932. .paravirt_enabled = 1,
  933. .shared_kernel_pmd = 0,
  934. .name = "Xen",
  935. };
  936. static const struct pv_init_ops xen_init_ops __initdata = {
  937. .patch = xen_patch,
  938. .banner = xen_banner,
  939. .memory_setup = xen_memory_setup,
  940. .arch_setup = xen_arch_setup,
  941. .post_allocator_init = xen_post_allocator_init,
  942. };
  943. static const struct pv_time_ops xen_time_ops __initdata = {
  944. .time_init = xen_time_init,
  945. .set_wallclock = xen_set_wallclock,
  946. .get_wallclock = xen_get_wallclock,
  947. .get_tsc_khz = xen_tsc_khz,
  948. .sched_clock = xen_sched_clock,
  949. };
  950. static const struct pv_cpu_ops xen_cpu_ops __initdata = {
  951. .cpuid = xen_cpuid,
  952. .set_debugreg = xen_set_debugreg,
  953. .get_debugreg = xen_get_debugreg,
  954. .clts = xen_clts,
  955. .read_cr0 = native_read_cr0,
  956. .write_cr0 = xen_write_cr0,
  957. .read_cr4 = native_read_cr4,
  958. .read_cr4_safe = native_read_cr4_safe,
  959. .write_cr4 = xen_write_cr4,
  960. .wbinvd = native_wbinvd,
  961. .read_msr = native_read_msr_safe,
  962. .write_msr = xen_write_msr_safe,
  963. .read_tsc = native_read_tsc,
  964. .read_pmc = native_read_pmc,
  965. .iret = xen_iret,
  966. .irq_enable_sysexit = xen_sysexit,
  967. #ifdef CONFIG_X86_64
  968. .usergs_sysret32 = xen_sysret32,
  969. .usergs_sysret64 = xen_sysret64,
  970. #endif
  971. .load_tr_desc = paravirt_nop,
  972. .set_ldt = xen_set_ldt,
  973. .load_gdt = xen_load_gdt,
  974. .load_idt = xen_load_idt,
  975. .load_tls = xen_load_tls,
  976. #ifdef CONFIG_X86_64
  977. .load_gs_index = xen_load_gs_index,
  978. #endif
  979. .store_gdt = native_store_gdt,
  980. .store_idt = native_store_idt,
  981. .store_tr = xen_store_tr,
  982. .write_ldt_entry = xen_write_ldt_entry,
  983. .write_gdt_entry = xen_write_gdt_entry,
  984. .write_idt_entry = xen_write_idt_entry,
  985. .load_sp0 = xen_load_sp0,
  986. .set_iopl_mask = xen_set_iopl_mask,
  987. .io_delay = xen_io_delay,
  988. /* Xen takes care of %gs when switching to usermode for us */
  989. .swapgs = paravirt_nop,
  990. .lazy_mode = {
  991. .enter = paravirt_enter_lazy_cpu,
  992. .leave = xen_leave_lazy,
  993. },
  994. };
  995. static void __init __xen_init_IRQ(void)
  996. {
  997. #ifdef CONFIG_X86_64
  998. int i;
  999. /* Create identity vector->irq map */
  1000. for(i = 0; i < NR_VECTORS; i++) {
  1001. int cpu;
  1002. for_each_possible_cpu(cpu)
  1003. per_cpu(vector_irq, cpu)[i] = i;
  1004. }
  1005. #endif /* CONFIG_X86_64 */
  1006. xen_init_IRQ();
  1007. }
  1008. static const struct pv_irq_ops xen_irq_ops __initdata = {
  1009. .init_IRQ = __xen_init_IRQ,
  1010. .save_fl = xen_save_fl,
  1011. .restore_fl = xen_restore_fl,
  1012. .irq_disable = xen_irq_disable,
  1013. .irq_enable = xen_irq_enable,
  1014. .safe_halt = xen_safe_halt,
  1015. .halt = xen_halt,
  1016. #ifdef CONFIG_X86_64
  1017. .adjust_exception_frame = xen_adjust_exception_frame,
  1018. #endif
  1019. };
  1020. static const struct pv_apic_ops xen_apic_ops __initdata = {
  1021. #ifdef CONFIG_X86_LOCAL_APIC
  1022. .apic_write = xen_apic_write,
  1023. .apic_read = xen_apic_read,
  1024. .setup_boot_clock = paravirt_nop,
  1025. .setup_secondary_clock = paravirt_nop,
  1026. .startup_ipi_hook = paravirt_nop,
  1027. #endif
  1028. };
  1029. static const struct pv_mmu_ops xen_mmu_ops __initdata = {
  1030. .pagetable_setup_start = xen_pagetable_setup_start,
  1031. .pagetable_setup_done = xen_pagetable_setup_done,
  1032. .read_cr2 = xen_read_cr2,
  1033. .write_cr2 = xen_write_cr2,
  1034. .read_cr3 = xen_read_cr3,
  1035. .write_cr3 = xen_write_cr3,
  1036. .flush_tlb_user = xen_flush_tlb,
  1037. .flush_tlb_kernel = xen_flush_tlb,
  1038. .flush_tlb_single = xen_flush_tlb_single,
  1039. .flush_tlb_others = xen_flush_tlb_others,
  1040. .pte_update = paravirt_nop,
  1041. .pte_update_defer = paravirt_nop,
  1042. .pgd_alloc = xen_pgd_alloc,
  1043. .pgd_free = xen_pgd_free,
  1044. .alloc_pte = xen_alloc_pte_init,
  1045. .release_pte = xen_release_pte_init,
  1046. .alloc_pmd = xen_alloc_pte_init,
  1047. .alloc_pmd_clone = paravirt_nop,
  1048. .release_pmd = xen_release_pte_init,
  1049. #ifdef CONFIG_HIGHPTE
  1050. .kmap_atomic_pte = xen_kmap_atomic_pte,
  1051. #endif
  1052. #ifdef CONFIG_X86_64
  1053. .set_pte = xen_set_pte,
  1054. #else
  1055. .set_pte = xen_set_pte_init,
  1056. #endif
  1057. .set_pte_at = xen_set_pte_at,
  1058. .set_pmd = xen_set_pmd_hyper,
  1059. .ptep_modify_prot_start = __ptep_modify_prot_start,
  1060. .ptep_modify_prot_commit = __ptep_modify_prot_commit,
  1061. .pte_val = xen_pte_val,
  1062. .pte_flags = native_pte_flags,
  1063. .pgd_val = xen_pgd_val,
  1064. .make_pte = xen_make_pte,
  1065. .make_pgd = xen_make_pgd,
  1066. #ifdef CONFIG_X86_PAE
  1067. .set_pte_atomic = xen_set_pte_atomic,
  1068. .set_pte_present = xen_set_pte_at,
  1069. .pte_clear = xen_pte_clear,
  1070. .pmd_clear = xen_pmd_clear,
  1071. #endif /* CONFIG_X86_PAE */
  1072. .set_pud = xen_set_pud_hyper,
  1073. .make_pmd = xen_make_pmd,
  1074. .pmd_val = xen_pmd_val,
  1075. #if PAGETABLE_LEVELS == 4
  1076. .pud_val = xen_pud_val,
  1077. .make_pud = xen_make_pud,
  1078. .set_pgd = xen_set_pgd_hyper,
  1079. .alloc_pud = xen_alloc_pte_init,
  1080. .release_pud = xen_release_pte_init,
  1081. #endif /* PAGETABLE_LEVELS == 4 */
  1082. .activate_mm = xen_activate_mm,
  1083. .dup_mmap = xen_dup_mmap,
  1084. .exit_mmap = xen_exit_mmap,
  1085. .lazy_mode = {
  1086. .enter = paravirt_enter_lazy_mmu,
  1087. .leave = xen_leave_lazy,
  1088. },
  1089. .set_fixmap = xen_set_fixmap,
  1090. };
  1091. static void xen_reboot(int reason)
  1092. {
  1093. struct sched_shutdown r = { .reason = reason };
  1094. #ifdef CONFIG_SMP
  1095. smp_send_stop();
  1096. #endif
  1097. if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
  1098. BUG();
  1099. }
  1100. static void xen_restart(char *msg)
  1101. {
  1102. xen_reboot(SHUTDOWN_reboot);
  1103. }
  1104. static void xen_emergency_restart(void)
  1105. {
  1106. xen_reboot(SHUTDOWN_reboot);
  1107. }
  1108. static void xen_machine_halt(void)
  1109. {
  1110. xen_reboot(SHUTDOWN_poweroff);
  1111. }
  1112. static void xen_crash_shutdown(struct pt_regs *regs)
  1113. {
  1114. xen_reboot(SHUTDOWN_crash);
  1115. }
  1116. static const struct machine_ops __initdata xen_machine_ops = {
  1117. .restart = xen_restart,
  1118. .halt = xen_machine_halt,
  1119. .power_off = xen_machine_halt,
  1120. .shutdown = xen_machine_halt,
  1121. .crash_shutdown = xen_crash_shutdown,
  1122. .emergency_restart = xen_emergency_restart,
  1123. };
  1124. static void __init xen_reserve_top(void)
  1125. {
  1126. #ifdef CONFIG_X86_32
  1127. unsigned long top = HYPERVISOR_VIRT_START;
  1128. struct xen_platform_parameters pp;
  1129. if (HYPERVISOR_xen_version(XENVER_platform_parameters, &pp) == 0)
  1130. top = pp.virt_start;
  1131. reserve_top_address(-top + 2 * PAGE_SIZE);
  1132. #endif /* CONFIG_X86_32 */
  1133. }
  1134. /*
  1135. * Like __va(), but returns address in the kernel mapping (which is
  1136. * all we have until the physical memory mapping has been set up.
  1137. */
  1138. static void *__ka(phys_addr_t paddr)
  1139. {
  1140. #ifdef CONFIG_X86_64
  1141. return (void *)(paddr + __START_KERNEL_map);
  1142. #else
  1143. return __va(paddr);
  1144. #endif
  1145. }
  1146. /* Convert a machine address to physical address */
  1147. static unsigned long m2p(phys_addr_t maddr)
  1148. {
  1149. phys_addr_t paddr;
  1150. maddr &= PTE_PFN_MASK;
  1151. paddr = mfn_to_pfn(maddr >> PAGE_SHIFT) << PAGE_SHIFT;
  1152. return paddr;
  1153. }
  1154. /* Convert a machine address to kernel virtual */
  1155. static void *m2v(phys_addr_t maddr)
  1156. {
  1157. return __ka(m2p(maddr));
  1158. }
  1159. #ifdef CONFIG_X86_64
  1160. static void walk(pgd_t *pgd, unsigned long addr)
  1161. {
  1162. unsigned l4idx = pgd_index(addr);
  1163. unsigned l3idx = pud_index(addr);
  1164. unsigned l2idx = pmd_index(addr);
  1165. unsigned l1idx = pte_index(addr);
  1166. pgd_t l4;
  1167. pud_t l3;
  1168. pmd_t l2;
  1169. pte_t l1;
  1170. xen_raw_printk("walk %p, %lx -> %d %d %d %d\n",
  1171. pgd, addr, l4idx, l3idx, l2idx, l1idx);
  1172. l4 = pgd[l4idx];
  1173. xen_raw_printk(" l4: %016lx\n", l4.pgd);
  1174. xen_raw_printk(" %016lx\n", pgd_val(l4));
  1175. l3 = ((pud_t *)(m2v(l4.pgd)))[l3idx];
  1176. xen_raw_printk(" l3: %016lx\n", l3.pud);
  1177. xen_raw_printk(" %016lx\n", pud_val(l3));
  1178. l2 = ((pmd_t *)(m2v(l3.pud)))[l2idx];
  1179. xen_raw_printk(" l2: %016lx\n", l2.pmd);
  1180. xen_raw_printk(" %016lx\n", pmd_val(l2));
  1181. l1 = ((pte_t *)(m2v(l2.pmd)))[l1idx];
  1182. xen_raw_printk(" l1: %016lx\n", l1.pte);
  1183. xen_raw_printk(" %016lx\n", pte_val(l1));
  1184. }
  1185. #endif
  1186. static void set_page_prot(void *addr, pgprot_t prot)
  1187. {
  1188. unsigned long pfn = __pa(addr) >> PAGE_SHIFT;
  1189. pte_t pte = pfn_pte(pfn, prot);
  1190. xen_raw_printk("addr=%p pfn=%lx mfn=%lx prot=%016llx pte=%016llx\n",
  1191. addr, pfn, get_phys_to_machine(pfn),
  1192. pgprot_val(prot), pte.pte);
  1193. if (HYPERVISOR_update_va_mapping((unsigned long)addr, pte, 0))
  1194. BUG();
  1195. }
  1196. static __init void xen_map_identity_early(pmd_t *pmd, unsigned long max_pfn)
  1197. {
  1198. unsigned pmdidx, pteidx;
  1199. unsigned ident_pte;
  1200. unsigned long pfn;
  1201. ident_pte = 0;
  1202. pfn = 0;
  1203. for(pmdidx = 0; pmdidx < PTRS_PER_PMD && pfn < max_pfn; pmdidx++) {
  1204. pte_t *pte_page;
  1205. /* Reuse or allocate a page of ptes */
  1206. if (pmd_present(pmd[pmdidx]))
  1207. pte_page = m2v(pmd[pmdidx].pmd);
  1208. else {
  1209. /* Check for free pte pages */
  1210. if (ident_pte == ARRAY_SIZE(level1_ident_pgt))
  1211. break;
  1212. pte_page = &level1_ident_pgt[ident_pte];
  1213. ident_pte += PTRS_PER_PTE;
  1214. pmd[pmdidx] = __pmd(__pa(pte_page) | _PAGE_TABLE);
  1215. }
  1216. /* Install mappings */
  1217. for(pteidx = 0; pteidx < PTRS_PER_PTE; pteidx++, pfn++) {
  1218. pte_t pte;
  1219. if (pfn > max_pfn_mapped)
  1220. max_pfn_mapped = pfn;
  1221. if (!pte_none(pte_page[pteidx]))
  1222. continue;
  1223. pte = pfn_pte(pfn, PAGE_KERNEL_EXEC);
  1224. pte_page[pteidx] = pte;
  1225. }
  1226. }
  1227. for(pteidx = 0; pteidx < ident_pte; pteidx += PTRS_PER_PTE)
  1228. set_page_prot(&level1_ident_pgt[pteidx], PAGE_KERNEL_RO);
  1229. set_page_prot(pmd, PAGE_KERNEL_RO);
  1230. }
  1231. #ifdef CONFIG_X86_64
  1232. static void convert_pfn_mfn(void *v)
  1233. {
  1234. pte_t *pte = v;
  1235. int i;
  1236. /* All levels are converted the same way, so just treat them
  1237. as ptes. */
  1238. for(i = 0; i < PTRS_PER_PTE; i++)
  1239. pte[i] = xen_make_pte(pte[i].pte);
  1240. }
  1241. /*
  1242. * Set up the inital kernel pagetable.
  1243. *
  1244. * We can construct this by grafting the Xen provided pagetable into
  1245. * head_64.S's preconstructed pagetables. We copy the Xen L2's into
  1246. * level2_ident_pgt, level2_kernel_pgt and level2_fixmap_pgt. This
  1247. * means that only the kernel has a physical mapping to start with -
  1248. * but that's enough to get __va working. We need to fill in the rest
  1249. * of the physical mapping once some sort of allocator has been set
  1250. * up.
  1251. */
  1252. static __init pgd_t *xen_setup_kernel_pagetable(pgd_t *pgd, unsigned long max_pfn)
  1253. {
  1254. pud_t *l3;
  1255. pmd_t *l2;
  1256. /* Zap identity mapping */
  1257. init_level4_pgt[0] = __pgd(0);
  1258. /* Pre-constructed entries are in pfn, so convert to mfn */
  1259. convert_pfn_mfn(init_level4_pgt);
  1260. convert_pfn_mfn(level3_ident_pgt);
  1261. convert_pfn_mfn(level3_kernel_pgt);
  1262. l3 = m2v(pgd[pgd_index(__START_KERNEL_map)].pgd);
  1263. l2 = m2v(l3[pud_index(__START_KERNEL_map)].pud);
  1264. memcpy(level2_ident_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD);
  1265. memcpy(level2_kernel_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD);
  1266. l3 = m2v(pgd[pgd_index(__START_KERNEL_map + PMD_SIZE)].pgd);
  1267. l2 = m2v(l3[pud_index(__START_KERNEL_map + PMD_SIZE)].pud);
  1268. memcpy(level2_fixmap_pgt, l2, sizeof(pmd_t) * PTRS_PER_PMD);
  1269. /* Set up identity map */
  1270. xen_map_identity_early(level2_ident_pgt, max_pfn);
  1271. /* Make pagetable pieces RO */
  1272. set_page_prot(init_level4_pgt, PAGE_KERNEL_RO);
  1273. set_page_prot(level3_ident_pgt, PAGE_KERNEL_RO);
  1274. set_page_prot(level3_kernel_pgt, PAGE_KERNEL_RO);
  1275. set_page_prot(level3_user_vsyscall, PAGE_KERNEL_RO);
  1276. set_page_prot(level2_kernel_pgt, PAGE_KERNEL_RO);
  1277. set_page_prot(level2_fixmap_pgt, PAGE_KERNEL_RO);
  1278. /* Pin down new L4 */
  1279. pin_pagetable_pfn(MMUEXT_PIN_L4_TABLE,
  1280. PFN_DOWN(__pa_symbol(init_level4_pgt)));
  1281. /* Unpin Xen-provided one */
  1282. pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd)));
  1283. /* Switch over */
  1284. pgd = init_level4_pgt;
  1285. /*
  1286. * At this stage there can be no user pgd, and no page
  1287. * structure to attach it to, so make sure we just set kernel
  1288. * pgd.
  1289. */
  1290. xen_mc_batch();
  1291. __xen_write_cr3(true, __pa(pgd));
  1292. xen_mc_issue(PARAVIRT_LAZY_CPU);
  1293. reserve_early(__pa(xen_start_info->pt_base),
  1294. __pa(xen_start_info->pt_base +
  1295. xen_start_info->nr_pt_frames * PAGE_SIZE),
  1296. "XEN PAGETABLES");
  1297. return pgd;
  1298. }
  1299. #else /* !CONFIG_X86_64 */
  1300. static pmd_t level2_kernel_pgt[PTRS_PER_PMD] __page_aligned_bss;
  1301. static __init pgd_t *xen_setup_kernel_pagetable(pgd_t *pgd, unsigned long max_pfn)
  1302. {
  1303. pmd_t *kernel_pmd;
  1304. init_pg_tables_start = __pa(pgd);
  1305. init_pg_tables_end = __pa(pgd) + xen_start_info->nr_pt_frames*PAGE_SIZE;
  1306. max_pfn_mapped = PFN_DOWN(init_pg_tables_end + 512*1024);
  1307. kernel_pmd = m2v(pgd[KERNEL_PGD_BOUNDARY].pgd);
  1308. memcpy(level2_kernel_pgt, kernel_pmd, sizeof(pmd_t) * PTRS_PER_PMD);
  1309. xen_map_identity_early(level2_kernel_pgt, max_pfn);
  1310. memcpy(swapper_pg_dir, pgd, sizeof(pgd_t) * PTRS_PER_PGD);
  1311. set_pgd(&swapper_pg_dir[KERNEL_PGD_BOUNDARY],
  1312. __pgd(__pa(level2_kernel_pgt) | _PAGE_PRESENT));
  1313. set_page_prot(level2_kernel_pgt, PAGE_KERNEL_RO);
  1314. set_page_prot(swapper_pg_dir, PAGE_KERNEL_RO);
  1315. set_page_prot(empty_zero_page, PAGE_KERNEL_RO);
  1316. pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, PFN_DOWN(__pa(pgd)));
  1317. xen_write_cr3(__pa(swapper_pg_dir));
  1318. pin_pagetable_pfn(MMUEXT_PIN_L3_TABLE, PFN_DOWN(__pa(swapper_pg_dir)));
  1319. return swapper_pg_dir;
  1320. }
  1321. #endif /* CONFIG_X86_64 */
  1322. /* First C function to be called on Xen boot */
  1323. asmlinkage void __init xen_start_kernel(void)
  1324. {
  1325. pgd_t *pgd;
  1326. if (!xen_start_info)
  1327. return;
  1328. BUG_ON(memcmp(xen_start_info->magic, "xen-3", 5) != 0);
  1329. xen_setup_features();
  1330. /* Install Xen paravirt ops */
  1331. pv_info = xen_info;
  1332. pv_init_ops = xen_init_ops;
  1333. pv_time_ops = xen_time_ops;
  1334. pv_cpu_ops = xen_cpu_ops;
  1335. pv_irq_ops = xen_irq_ops;
  1336. pv_apic_ops = xen_apic_ops;
  1337. pv_mmu_ops = xen_mmu_ops;
  1338. if (xen_feature(XENFEAT_mmu_pt_update_preserve_ad)) {
  1339. pv_mmu_ops.ptep_modify_prot_start = xen_ptep_modify_prot_start;
  1340. pv_mmu_ops.ptep_modify_prot_commit = xen_ptep_modify_prot_commit;
  1341. }
  1342. machine_ops = xen_machine_ops;
  1343. #ifdef CONFIG_X86_64
  1344. /* Disable until direct per-cpu data access. */
  1345. have_vcpu_info_placement = 0;
  1346. x86_64_init_pda();
  1347. #endif
  1348. xen_smp_init();
  1349. /* Get mfn list */
  1350. if (!xen_feature(XENFEAT_auto_translated_physmap))
  1351. xen_build_dynamic_phys_to_machine();
  1352. pgd = (pgd_t *)xen_start_info->pt_base;
  1353. /* Prevent unwanted bits from being set in PTEs. */
  1354. __supported_pte_mask &= ~_PAGE_GLOBAL;
  1355. if (!is_initial_xendomain())
  1356. __supported_pte_mask &= ~(_PAGE_PWT | _PAGE_PCD);
  1357. /* Don't do the full vcpu_info placement stuff until we have a
  1358. possible map and a non-dummy shared_info. */
  1359. per_cpu(xen_vcpu, 0) = &HYPERVISOR_shared_info->vcpu_info[0];
  1360. xen_raw_console_write("mapping kernel into physical memory\n");
  1361. pgd = xen_setup_kernel_pagetable(pgd, xen_start_info->nr_pages);
  1362. init_mm.pgd = pgd;
  1363. /* keep using Xen gdt for now; no urgent need to change it */
  1364. pv_info.kernel_rpl = 1;
  1365. if (xen_feature(XENFEAT_supervisor_mode_kernel))
  1366. pv_info.kernel_rpl = 0;
  1367. /* set the limit of our address space */
  1368. xen_reserve_top();
  1369. #ifdef CONFIG_X86_32
  1370. /* set up basic CPUID stuff */
  1371. cpu_detect(&new_cpu_data);
  1372. new_cpu_data.hard_math = 1;
  1373. new_cpu_data.x86_capability[0] = cpuid_edx(1);
  1374. #endif
  1375. /* Poke various useful things into boot_params */
  1376. boot_params.hdr.type_of_loader = (9 << 4) | 0;
  1377. boot_params.hdr.ramdisk_image = xen_start_info->mod_start
  1378. ? __pa(xen_start_info->mod_start) : 0;
  1379. boot_params.hdr.ramdisk_size = xen_start_info->mod_len;
  1380. boot_params.hdr.cmd_line_ptr = __pa(xen_start_info->cmd_line);
  1381. if (!is_initial_xendomain()) {
  1382. add_preferred_console("xenboot", 0, NULL);
  1383. add_preferred_console("tty", 0, NULL);
  1384. add_preferred_console("hvc", 0, NULL);
  1385. }
  1386. xen_raw_console_write("about to get started...\n");
  1387. #if 0
  1388. xen_raw_printk("&boot_params=%p __pa(&boot_params)=%lx __va(__pa(&boot_params))=%lx\n",
  1389. &boot_params, __pa_symbol(&boot_params),
  1390. __va(__pa_symbol(&boot_params)));
  1391. walk(pgd, &boot_params);
  1392. walk(pgd, __va(__pa(&boot_params)));
  1393. #endif
  1394. /* Start the world */
  1395. #ifdef CONFIG_X86_32
  1396. i386_start_kernel();
  1397. #else
  1398. x86_64_start_reservations((char *)__pa_symbol(&boot_params));
  1399. #endif
  1400. }