enlighten.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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/smp.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 <asm/paravirt.h>
  35. #include <asm/page.h>
  36. #include <asm/xen/hypercall.h>
  37. #include <asm/xen/hypervisor.h>
  38. #include <asm/fixmap.h>
  39. #include <asm/processor.h>
  40. #include <asm/setup.h>
  41. #include <asm/desc.h>
  42. #include <asm/pgtable.h>
  43. #include <asm/tlbflush.h>
  44. #include <asm/reboot.h>
  45. #include "xen-ops.h"
  46. #include "mmu.h"
  47. #include "multicalls.h"
  48. EXPORT_SYMBOL_GPL(hypercall_page);
  49. DEFINE_PER_CPU(enum paravirt_lazy_mode, xen_lazy_mode);
  50. DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
  51. DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
  52. DEFINE_PER_CPU(unsigned long, xen_cr3);
  53. struct start_info *xen_start_info;
  54. EXPORT_SYMBOL_GPL(xen_start_info);
  55. void xen_vcpu_setup(int cpu)
  56. {
  57. per_cpu(xen_vcpu, cpu) = &HYPERVISOR_shared_info->vcpu_info[cpu];
  58. }
  59. static void __init xen_banner(void)
  60. {
  61. printk(KERN_INFO "Booting paravirtualized kernel on %s\n",
  62. paravirt_ops.name);
  63. printk(KERN_INFO "Hypervisor signature: %s\n", xen_start_info->magic);
  64. }
  65. static void xen_cpuid(unsigned int *eax, unsigned int *ebx,
  66. unsigned int *ecx, unsigned int *edx)
  67. {
  68. unsigned maskedx = ~0;
  69. /*
  70. * Mask out inconvenient features, to try and disable as many
  71. * unsupported kernel subsystems as possible.
  72. */
  73. if (*eax == 1)
  74. maskedx = ~((1 << X86_FEATURE_APIC) | /* disable APIC */
  75. (1 << X86_FEATURE_ACPI) | /* disable ACPI */
  76. (1 << X86_FEATURE_ACC)); /* thermal monitoring */
  77. asm(XEN_EMULATE_PREFIX "cpuid"
  78. : "=a" (*eax),
  79. "=b" (*ebx),
  80. "=c" (*ecx),
  81. "=d" (*edx)
  82. : "0" (*eax), "2" (*ecx));
  83. *edx &= maskedx;
  84. }
  85. static void xen_set_debugreg(int reg, unsigned long val)
  86. {
  87. HYPERVISOR_set_debugreg(reg, val);
  88. }
  89. static unsigned long xen_get_debugreg(int reg)
  90. {
  91. return HYPERVISOR_get_debugreg(reg);
  92. }
  93. static unsigned long xen_save_fl(void)
  94. {
  95. struct vcpu_info *vcpu;
  96. unsigned long flags;
  97. vcpu = x86_read_percpu(xen_vcpu);
  98. /* flag has opposite sense of mask */
  99. flags = !vcpu->evtchn_upcall_mask;
  100. /* convert to IF type flag
  101. -0 -> 0x00000000
  102. -1 -> 0xffffffff
  103. */
  104. return (-flags) & X86_EFLAGS_IF;
  105. }
  106. static void xen_restore_fl(unsigned long flags)
  107. {
  108. struct vcpu_info *vcpu;
  109. /* convert from IF type flag */
  110. flags = !(flags & X86_EFLAGS_IF);
  111. /* There's a one instruction preempt window here. We need to
  112. make sure we're don't switch CPUs between getting the vcpu
  113. pointer and updating the mask. */
  114. preempt_disable();
  115. vcpu = x86_read_percpu(xen_vcpu);
  116. vcpu->evtchn_upcall_mask = flags;
  117. preempt_enable_no_resched();
  118. /* Doesn't matter if we get preempted here, because any
  119. pending event will get dealt with anyway. */
  120. if (flags == 0) {
  121. preempt_check_resched();
  122. barrier(); /* unmask then check (avoid races) */
  123. if (unlikely(vcpu->evtchn_upcall_pending))
  124. force_evtchn_callback();
  125. }
  126. }
  127. static void xen_irq_disable(void)
  128. {
  129. /* There's a one instruction preempt window here. We need to
  130. make sure we're don't switch CPUs between getting the vcpu
  131. pointer and updating the mask. */
  132. preempt_disable();
  133. x86_read_percpu(xen_vcpu)->evtchn_upcall_mask = 1;
  134. preempt_enable_no_resched();
  135. }
  136. static void xen_irq_enable(void)
  137. {
  138. struct vcpu_info *vcpu;
  139. /* There's a one instruction preempt window here. We need to
  140. make sure we're don't switch CPUs between getting the vcpu
  141. pointer and updating the mask. */
  142. preempt_disable();
  143. vcpu = x86_read_percpu(xen_vcpu);
  144. vcpu->evtchn_upcall_mask = 0;
  145. preempt_enable_no_resched();
  146. /* Doesn't matter if we get preempted here, because any
  147. pending event will get dealt with anyway. */
  148. barrier(); /* unmask then check (avoid races) */
  149. if (unlikely(vcpu->evtchn_upcall_pending))
  150. force_evtchn_callback();
  151. }
  152. static void xen_safe_halt(void)
  153. {
  154. /* Blocking includes an implicit local_irq_enable(). */
  155. if (HYPERVISOR_sched_op(SCHEDOP_block, 0) != 0)
  156. BUG();
  157. }
  158. static void xen_halt(void)
  159. {
  160. if (irqs_disabled())
  161. HYPERVISOR_vcpu_op(VCPUOP_down, smp_processor_id(), NULL);
  162. else
  163. xen_safe_halt();
  164. }
  165. static void xen_set_lazy_mode(enum paravirt_lazy_mode mode)
  166. {
  167. BUG_ON(preemptible());
  168. switch (mode) {
  169. case PARAVIRT_LAZY_NONE:
  170. BUG_ON(x86_read_percpu(xen_lazy_mode) == PARAVIRT_LAZY_NONE);
  171. break;
  172. case PARAVIRT_LAZY_MMU:
  173. case PARAVIRT_LAZY_CPU:
  174. BUG_ON(x86_read_percpu(xen_lazy_mode) != PARAVIRT_LAZY_NONE);
  175. break;
  176. case PARAVIRT_LAZY_FLUSH:
  177. /* flush if necessary, but don't change state */
  178. if (x86_read_percpu(xen_lazy_mode) != PARAVIRT_LAZY_NONE)
  179. xen_mc_flush();
  180. return;
  181. }
  182. xen_mc_flush();
  183. x86_write_percpu(xen_lazy_mode, mode);
  184. }
  185. static unsigned long xen_store_tr(void)
  186. {
  187. return 0;
  188. }
  189. static void xen_set_ldt(const void *addr, unsigned entries)
  190. {
  191. unsigned long linear_addr = (unsigned long)addr;
  192. struct mmuext_op *op;
  193. struct multicall_space mcs = xen_mc_entry(sizeof(*op));
  194. op = mcs.args;
  195. op->cmd = MMUEXT_SET_LDT;
  196. if (linear_addr) {
  197. /* ldt my be vmalloced, use arbitrary_virt_to_machine */
  198. xmaddr_t maddr;
  199. maddr = arbitrary_virt_to_machine((unsigned long)addr);
  200. linear_addr = (unsigned long)maddr.maddr;
  201. }
  202. op->arg1.linear_addr = linear_addr;
  203. op->arg2.nr_ents = entries;
  204. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  205. xen_mc_issue(PARAVIRT_LAZY_CPU);
  206. }
  207. static void xen_load_gdt(const struct Xgt_desc_struct *dtr)
  208. {
  209. unsigned long *frames;
  210. unsigned long va = dtr->address;
  211. unsigned int size = dtr->size + 1;
  212. unsigned pages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
  213. int f;
  214. struct multicall_space mcs;
  215. /* A GDT can be up to 64k in size, which corresponds to 8192
  216. 8-byte entries, or 16 4k pages.. */
  217. BUG_ON(size > 65536);
  218. BUG_ON(va & ~PAGE_MASK);
  219. mcs = xen_mc_entry(sizeof(*frames) * pages);
  220. frames = mcs.args;
  221. for (f = 0; va < dtr->address + size; va += PAGE_SIZE, f++) {
  222. frames[f] = virt_to_mfn(va);
  223. make_lowmem_page_readonly((void *)va);
  224. }
  225. MULTI_set_gdt(mcs.mc, frames, size / sizeof(struct desc_struct));
  226. xen_mc_issue(PARAVIRT_LAZY_CPU);
  227. }
  228. static void load_TLS_descriptor(struct thread_struct *t,
  229. unsigned int cpu, unsigned int i)
  230. {
  231. struct desc_struct *gdt = get_cpu_gdt_table(cpu);
  232. xmaddr_t maddr = virt_to_machine(&gdt[GDT_ENTRY_TLS_MIN+i]);
  233. struct multicall_space mc = __xen_mc_entry(0);
  234. MULTI_update_descriptor(mc.mc, maddr.maddr, t->tls_array[i]);
  235. }
  236. static void xen_load_tls(struct thread_struct *t, unsigned int cpu)
  237. {
  238. xen_mc_batch();
  239. load_TLS_descriptor(t, cpu, 0);
  240. load_TLS_descriptor(t, cpu, 1);
  241. load_TLS_descriptor(t, cpu, 2);
  242. xen_mc_issue(PARAVIRT_LAZY_CPU);
  243. /*
  244. * XXX sleazy hack: If we're being called in a lazy-cpu zone,
  245. * it means we're in a context switch, and %gs has just been
  246. * saved. This means we can zero it out to prevent faults on
  247. * exit from the hypervisor if the next process has no %gs.
  248. * Either way, it has been saved, and the new value will get
  249. * loaded properly. This will go away as soon as Xen has been
  250. * modified to not save/restore %gs for normal hypercalls.
  251. */
  252. if (xen_get_lazy_mode() == PARAVIRT_LAZY_CPU)
  253. loadsegment(gs, 0);
  254. }
  255. static void xen_write_ldt_entry(struct desc_struct *dt, int entrynum,
  256. u32 low, u32 high)
  257. {
  258. unsigned long lp = (unsigned long)&dt[entrynum];
  259. xmaddr_t mach_lp = virt_to_machine(lp);
  260. u64 entry = (u64)high << 32 | low;
  261. preempt_disable();
  262. xen_mc_flush();
  263. if (HYPERVISOR_update_descriptor(mach_lp.maddr, entry))
  264. BUG();
  265. preempt_enable();
  266. }
  267. static int cvt_gate_to_trap(int vector, u32 low, u32 high,
  268. struct trap_info *info)
  269. {
  270. u8 type, dpl;
  271. type = (high >> 8) & 0x1f;
  272. dpl = (high >> 13) & 3;
  273. if (type != 0xf && type != 0xe)
  274. return 0;
  275. info->vector = vector;
  276. info->address = (high & 0xffff0000) | (low & 0x0000ffff);
  277. info->cs = low >> 16;
  278. info->flags = dpl;
  279. /* interrupt gates clear IF */
  280. if (type == 0xe)
  281. info->flags |= 4;
  282. return 1;
  283. }
  284. /* Locations of each CPU's IDT */
  285. static DEFINE_PER_CPU(struct Xgt_desc_struct, idt_desc);
  286. /* Set an IDT entry. If the entry is part of the current IDT, then
  287. also update Xen. */
  288. static void xen_write_idt_entry(struct desc_struct *dt, int entrynum,
  289. u32 low, u32 high)
  290. {
  291. unsigned long p = (unsigned long)&dt[entrynum];
  292. unsigned long start, end;
  293. preempt_disable();
  294. start = __get_cpu_var(idt_desc).address;
  295. end = start + __get_cpu_var(idt_desc).size + 1;
  296. xen_mc_flush();
  297. write_dt_entry(dt, entrynum, low, high);
  298. if (p >= start && (p + 8) <= end) {
  299. struct trap_info info[2];
  300. info[1].address = 0;
  301. if (cvt_gate_to_trap(entrynum, low, high, &info[0]))
  302. if (HYPERVISOR_set_trap_table(info))
  303. BUG();
  304. }
  305. preempt_enable();
  306. }
  307. static void xen_convert_trap_info(const struct Xgt_desc_struct *desc,
  308. struct trap_info *traps)
  309. {
  310. unsigned in, out, count;
  311. count = (desc->size+1) / 8;
  312. BUG_ON(count > 256);
  313. for (in = out = 0; in < count; in++) {
  314. const u32 *entry = (u32 *)(desc->address + in * 8);
  315. if (cvt_gate_to_trap(in, entry[0], entry[1], &traps[out]))
  316. out++;
  317. }
  318. traps[out].address = 0;
  319. }
  320. void xen_copy_trap_info(struct trap_info *traps)
  321. {
  322. const struct Xgt_desc_struct *desc = &__get_cpu_var(idt_desc);
  323. xen_convert_trap_info(desc, traps);
  324. }
  325. /* Load a new IDT into Xen. In principle this can be per-CPU, so we
  326. hold a spinlock to protect the static traps[] array (static because
  327. it avoids allocation, and saves stack space). */
  328. static void xen_load_idt(const struct Xgt_desc_struct *desc)
  329. {
  330. static DEFINE_SPINLOCK(lock);
  331. static struct trap_info traps[257];
  332. spin_lock(&lock);
  333. __get_cpu_var(idt_desc) = *desc;
  334. xen_convert_trap_info(desc, traps);
  335. xen_mc_flush();
  336. if (HYPERVISOR_set_trap_table(traps))
  337. BUG();
  338. spin_unlock(&lock);
  339. }
  340. /* Write a GDT descriptor entry. Ignore LDT descriptors, since
  341. they're handled differently. */
  342. static void xen_write_gdt_entry(struct desc_struct *dt, int entry,
  343. u32 low, u32 high)
  344. {
  345. preempt_disable();
  346. switch ((high >> 8) & 0xff) {
  347. case DESCTYPE_LDT:
  348. case DESCTYPE_TSS:
  349. /* ignore */
  350. break;
  351. default: {
  352. xmaddr_t maddr = virt_to_machine(&dt[entry]);
  353. u64 desc = (u64)high << 32 | low;
  354. xen_mc_flush();
  355. if (HYPERVISOR_update_descriptor(maddr.maddr, desc))
  356. BUG();
  357. }
  358. }
  359. preempt_enable();
  360. }
  361. static void xen_load_esp0(struct tss_struct *tss,
  362. struct thread_struct *thread)
  363. {
  364. struct multicall_space mcs = xen_mc_entry(0);
  365. MULTI_stack_switch(mcs.mc, __KERNEL_DS, thread->esp0);
  366. xen_mc_issue(PARAVIRT_LAZY_CPU);
  367. }
  368. static void xen_set_iopl_mask(unsigned mask)
  369. {
  370. struct physdev_set_iopl set_iopl;
  371. /* Force the change at ring 0. */
  372. set_iopl.iopl = (mask == 0) ? 1 : (mask >> 12) & 3;
  373. HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl, &set_iopl);
  374. }
  375. static void xen_io_delay(void)
  376. {
  377. }
  378. #ifdef CONFIG_X86_LOCAL_APIC
  379. static unsigned long xen_apic_read(unsigned long reg)
  380. {
  381. return 0;
  382. }
  383. static void xen_apic_write(unsigned long reg, unsigned long val)
  384. {
  385. /* Warn to see if there's any stray references */
  386. WARN_ON(1);
  387. }
  388. #endif
  389. static void xen_flush_tlb(void)
  390. {
  391. struct mmuext_op *op;
  392. struct multicall_space mcs = xen_mc_entry(sizeof(*op));
  393. op = mcs.args;
  394. op->cmd = MMUEXT_TLB_FLUSH_LOCAL;
  395. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  396. xen_mc_issue(PARAVIRT_LAZY_MMU);
  397. }
  398. static void xen_flush_tlb_single(unsigned long addr)
  399. {
  400. struct mmuext_op *op;
  401. struct multicall_space mcs = xen_mc_entry(sizeof(*op));
  402. op = mcs.args;
  403. op->cmd = MMUEXT_INVLPG_LOCAL;
  404. op->arg1.linear_addr = addr & PAGE_MASK;
  405. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  406. xen_mc_issue(PARAVIRT_LAZY_MMU);
  407. }
  408. static void xen_flush_tlb_others(const cpumask_t *cpus, struct mm_struct *mm,
  409. unsigned long va)
  410. {
  411. struct {
  412. struct mmuext_op op;
  413. cpumask_t mask;
  414. } *args;
  415. cpumask_t cpumask = *cpus;
  416. struct multicall_space mcs;
  417. /*
  418. * A couple of (to be removed) sanity checks:
  419. *
  420. * - current CPU must not be in mask
  421. * - mask must exist :)
  422. */
  423. BUG_ON(cpus_empty(cpumask));
  424. BUG_ON(cpu_isset(smp_processor_id(), cpumask));
  425. BUG_ON(!mm);
  426. /* If a CPU which we ran on has gone down, OK. */
  427. cpus_and(cpumask, cpumask, cpu_online_map);
  428. if (cpus_empty(cpumask))
  429. return;
  430. mcs = xen_mc_entry(sizeof(*args));
  431. args = mcs.args;
  432. args->mask = cpumask;
  433. args->op.arg2.vcpumask = &args->mask;
  434. if (va == TLB_FLUSH_ALL) {
  435. args->op.cmd = MMUEXT_TLB_FLUSH_MULTI;
  436. } else {
  437. args->op.cmd = MMUEXT_INVLPG_MULTI;
  438. args->op.arg1.linear_addr = va;
  439. }
  440. MULTI_mmuext_op(mcs.mc, &args->op, 1, NULL, DOMID_SELF);
  441. xen_mc_issue(PARAVIRT_LAZY_MMU);
  442. }
  443. static unsigned long xen_read_cr2(void)
  444. {
  445. return x86_read_percpu(xen_vcpu)->arch.cr2;
  446. }
  447. static void xen_write_cr4(unsigned long cr4)
  448. {
  449. /* never allow TSC to be disabled */
  450. native_write_cr4(cr4 & ~X86_CR4_TSD);
  451. }
  452. static unsigned long xen_read_cr3(void)
  453. {
  454. return x86_read_percpu(xen_cr3);
  455. }
  456. static void xen_write_cr3(unsigned long cr3)
  457. {
  458. BUG_ON(preemptible());
  459. if (cr3 == x86_read_percpu(xen_cr3)) {
  460. /* just a simple tlb flush */
  461. xen_flush_tlb();
  462. return;
  463. }
  464. x86_write_percpu(xen_cr3, cr3);
  465. {
  466. struct mmuext_op *op;
  467. struct multicall_space mcs = xen_mc_entry(sizeof(*op));
  468. unsigned long mfn = pfn_to_mfn(PFN_DOWN(cr3));
  469. op = mcs.args;
  470. op->cmd = MMUEXT_NEW_BASEPTR;
  471. op->arg1.mfn = mfn;
  472. MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
  473. xen_mc_issue(PARAVIRT_LAZY_CPU);
  474. }
  475. }
  476. /* Early in boot, while setting up the initial pagetable, assume
  477. everything is pinned. */
  478. static __init void xen_alloc_pt_init(struct mm_struct *mm, u32 pfn)
  479. {
  480. BUG_ON(mem_map); /* should only be used early */
  481. make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
  482. }
  483. /* This needs to make sure the new pte page is pinned iff its being
  484. attached to a pinned pagetable. */
  485. static void xen_alloc_pt(struct mm_struct *mm, u32 pfn)
  486. {
  487. struct page *page = pfn_to_page(pfn);
  488. if (PagePinned(virt_to_page(mm->pgd))) {
  489. SetPagePinned(page);
  490. if (!PageHighMem(page))
  491. make_lowmem_page_readonly(__va(PFN_PHYS(pfn)));
  492. else
  493. /* make sure there are no stray mappings of
  494. this page */
  495. kmap_flush_unused();
  496. }
  497. }
  498. /* This should never happen until we're OK to use struct page */
  499. static void xen_release_pt(u32 pfn)
  500. {
  501. struct page *page = pfn_to_page(pfn);
  502. if (PagePinned(page)) {
  503. if (!PageHighMem(page))
  504. make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
  505. }
  506. }
  507. #ifdef CONFIG_HIGHPTE
  508. static void *xen_kmap_atomic_pte(struct page *page, enum km_type type)
  509. {
  510. pgprot_t prot = PAGE_KERNEL;
  511. if (PagePinned(page))
  512. prot = PAGE_KERNEL_RO;
  513. if (0 && PageHighMem(page))
  514. printk("mapping highpte %lx type %d prot %s\n",
  515. page_to_pfn(page), type,
  516. (unsigned long)pgprot_val(prot) & _PAGE_RW ? "WRITE" : "READ");
  517. return kmap_atomic_prot(page, type, prot);
  518. }
  519. #endif
  520. static __init pte_t mask_rw_pte(pte_t *ptep, pte_t pte)
  521. {
  522. /* If there's an existing pte, then don't allow _PAGE_RW to be set */
  523. if (pte_val_ma(*ptep) & _PAGE_PRESENT)
  524. pte = __pte_ma(((pte_val_ma(*ptep) & _PAGE_RW) | ~_PAGE_RW) &
  525. pte_val_ma(pte));
  526. return pte;
  527. }
  528. /* Init-time set_pte while constructing initial pagetables, which
  529. doesn't allow RO pagetable pages to be remapped RW */
  530. static __init void xen_set_pte_init(pte_t *ptep, pte_t pte)
  531. {
  532. pte = mask_rw_pte(ptep, pte);
  533. xen_set_pte(ptep, pte);
  534. }
  535. static __init void xen_pagetable_setup_start(pgd_t *base)
  536. {
  537. pgd_t *xen_pgd = (pgd_t *)xen_start_info->pt_base;
  538. /* special set_pte for pagetable initialization */
  539. paravirt_ops.set_pte = xen_set_pte_init;
  540. init_mm.pgd = base;
  541. /*
  542. * copy top-level of Xen-supplied pagetable into place. For
  543. * !PAE we can use this as-is, but for PAE it is a stand-in
  544. * while we copy the pmd pages.
  545. */
  546. memcpy(base, xen_pgd, PTRS_PER_PGD * sizeof(pgd_t));
  547. if (PTRS_PER_PMD > 1) {
  548. int i;
  549. /*
  550. * For PAE, need to allocate new pmds, rather than
  551. * share Xen's, since Xen doesn't like pmd's being
  552. * shared between address spaces.
  553. */
  554. for (i = 0; i < PTRS_PER_PGD; i++) {
  555. if (pgd_val_ma(xen_pgd[i]) & _PAGE_PRESENT) {
  556. pmd_t *pmd = (pmd_t *)alloc_bootmem_low_pages(PAGE_SIZE);
  557. memcpy(pmd, (void *)pgd_page_vaddr(xen_pgd[i]),
  558. PAGE_SIZE);
  559. make_lowmem_page_readonly(pmd);
  560. set_pgd(&base[i], __pgd(1 + __pa(pmd)));
  561. } else
  562. pgd_clear(&base[i]);
  563. }
  564. }
  565. /* make sure zero_page is mapped RO so we can use it in pagetables */
  566. make_lowmem_page_readonly(empty_zero_page);
  567. make_lowmem_page_readonly(base);
  568. /*
  569. * Switch to new pagetable. This is done before
  570. * pagetable_init has done anything so that the new pages
  571. * added to the table can be prepared properly for Xen.
  572. */
  573. xen_write_cr3(__pa(base));
  574. }
  575. static __init void xen_pagetable_setup_done(pgd_t *base)
  576. {
  577. /* This will work as long as patching hasn't happened yet
  578. (which it hasn't) */
  579. paravirt_ops.alloc_pt = xen_alloc_pt;
  580. paravirt_ops.set_pte = xen_set_pte;
  581. if (!xen_feature(XENFEAT_auto_translated_physmap)) {
  582. /*
  583. * Create a mapping for the shared info page.
  584. * Should be set_fixmap(), but shared_info is a machine
  585. * address with no corresponding pseudo-phys address.
  586. */
  587. set_pte_mfn(fix_to_virt(FIX_PARAVIRT_BOOTMAP),
  588. PFN_DOWN(xen_start_info->shared_info),
  589. PAGE_KERNEL);
  590. HYPERVISOR_shared_info =
  591. (struct shared_info *)fix_to_virt(FIX_PARAVIRT_BOOTMAP);
  592. } else
  593. HYPERVISOR_shared_info =
  594. (struct shared_info *)__va(xen_start_info->shared_info);
  595. /* Actually pin the pagetable down, but we can't set PG_pinned
  596. yet because the page structures don't exist yet. */
  597. {
  598. struct mmuext_op op;
  599. #ifdef CONFIG_X86_PAE
  600. op.cmd = MMUEXT_PIN_L3_TABLE;
  601. #else
  602. op.cmd = MMUEXT_PIN_L3_TABLE;
  603. #endif
  604. op.arg1.mfn = pfn_to_mfn(PFN_DOWN(__pa(base)));
  605. if (HYPERVISOR_mmuext_op(&op, 1, NULL, DOMID_SELF))
  606. BUG();
  607. }
  608. xen_vcpu_setup(smp_processor_id());
  609. }
  610. static const struct paravirt_ops xen_paravirt_ops __initdata = {
  611. .paravirt_enabled = 1,
  612. .shared_kernel_pmd = 0,
  613. .name = "Xen",
  614. .banner = xen_banner,
  615. .patch = paravirt_patch_default,
  616. .memory_setup = xen_memory_setup,
  617. .arch_setup = xen_arch_setup,
  618. .init_IRQ = xen_init_IRQ,
  619. .post_allocator_init = xen_mark_init_mm_pinned,
  620. .time_init = xen_time_init,
  621. .set_wallclock = xen_set_wallclock,
  622. .get_wallclock = xen_get_wallclock,
  623. .get_cpu_khz = xen_cpu_khz,
  624. .sched_clock = xen_sched_clock,
  625. .cpuid = xen_cpuid,
  626. .set_debugreg = xen_set_debugreg,
  627. .get_debugreg = xen_get_debugreg,
  628. .clts = native_clts,
  629. .read_cr0 = native_read_cr0,
  630. .write_cr0 = native_write_cr0,
  631. .read_cr2 = xen_read_cr2,
  632. .write_cr2 = native_write_cr2,
  633. .read_cr3 = xen_read_cr3,
  634. .write_cr3 = xen_write_cr3,
  635. .read_cr4 = native_read_cr4,
  636. .read_cr4_safe = native_read_cr4_safe,
  637. .write_cr4 = xen_write_cr4,
  638. .save_fl = xen_save_fl,
  639. .restore_fl = xen_restore_fl,
  640. .irq_disable = xen_irq_disable,
  641. .irq_enable = xen_irq_enable,
  642. .safe_halt = xen_safe_halt,
  643. .halt = xen_halt,
  644. .wbinvd = native_wbinvd,
  645. .read_msr = native_read_msr_safe,
  646. .write_msr = native_write_msr_safe,
  647. .read_tsc = native_read_tsc,
  648. .read_pmc = native_read_pmc,
  649. .iret = (void *)&hypercall_page[__HYPERVISOR_iret],
  650. .irq_enable_sysexit = NULL, /* never called */
  651. .load_tr_desc = paravirt_nop,
  652. .set_ldt = xen_set_ldt,
  653. .load_gdt = xen_load_gdt,
  654. .load_idt = xen_load_idt,
  655. .load_tls = xen_load_tls,
  656. .store_gdt = native_store_gdt,
  657. .store_idt = native_store_idt,
  658. .store_tr = xen_store_tr,
  659. .write_ldt_entry = xen_write_ldt_entry,
  660. .write_gdt_entry = xen_write_gdt_entry,
  661. .write_idt_entry = xen_write_idt_entry,
  662. .load_esp0 = xen_load_esp0,
  663. .set_iopl_mask = xen_set_iopl_mask,
  664. .io_delay = xen_io_delay,
  665. #ifdef CONFIG_X86_LOCAL_APIC
  666. .apic_write = xen_apic_write,
  667. .apic_write_atomic = xen_apic_write,
  668. .apic_read = xen_apic_read,
  669. .setup_boot_clock = paravirt_nop,
  670. .setup_secondary_clock = paravirt_nop,
  671. .startup_ipi_hook = paravirt_nop,
  672. #endif
  673. .flush_tlb_user = xen_flush_tlb,
  674. .flush_tlb_kernel = xen_flush_tlb,
  675. .flush_tlb_single = xen_flush_tlb_single,
  676. .flush_tlb_others = xen_flush_tlb_others,
  677. .pte_update = paravirt_nop,
  678. .pte_update_defer = paravirt_nop,
  679. .pagetable_setup_start = xen_pagetable_setup_start,
  680. .pagetable_setup_done = xen_pagetable_setup_done,
  681. .alloc_pt = xen_alloc_pt_init,
  682. .release_pt = xen_release_pt,
  683. .alloc_pd = paravirt_nop,
  684. .alloc_pd_clone = paravirt_nop,
  685. .release_pd = paravirt_nop,
  686. #ifdef CONFIG_HIGHPTE
  687. .kmap_atomic_pte = xen_kmap_atomic_pte,
  688. #endif
  689. .set_pte = NULL, /* see xen_pagetable_setup_* */
  690. .set_pte_at = xen_set_pte_at,
  691. .set_pmd = xen_set_pmd,
  692. .pte_val = xen_pte_val,
  693. .pgd_val = xen_pgd_val,
  694. .make_pte = xen_make_pte,
  695. .make_pgd = xen_make_pgd,
  696. #ifdef CONFIG_X86_PAE
  697. .set_pte_atomic = xen_set_pte_atomic,
  698. .set_pte_present = xen_set_pte_at,
  699. .set_pud = xen_set_pud,
  700. .pte_clear = xen_pte_clear,
  701. .pmd_clear = xen_pmd_clear,
  702. .make_pmd = xen_make_pmd,
  703. .pmd_val = xen_pmd_val,
  704. #endif /* PAE */
  705. .activate_mm = xen_activate_mm,
  706. .dup_mmap = xen_dup_mmap,
  707. .exit_mmap = xen_exit_mmap,
  708. .set_lazy_mode = xen_set_lazy_mode,
  709. };
  710. #ifdef CONFIG_SMP
  711. static const struct smp_ops xen_smp_ops __initdata = {
  712. .smp_prepare_boot_cpu = xen_smp_prepare_boot_cpu,
  713. .smp_prepare_cpus = xen_smp_prepare_cpus,
  714. .cpu_up = xen_cpu_up,
  715. .smp_cpus_done = xen_smp_cpus_done,
  716. .smp_send_stop = xen_smp_send_stop,
  717. .smp_send_reschedule = xen_smp_send_reschedule,
  718. .smp_call_function_mask = xen_smp_call_function_mask,
  719. };
  720. #endif /* CONFIG_SMP */
  721. static void xen_reboot(int reason)
  722. {
  723. #ifdef CONFIG_SMP
  724. smp_send_stop();
  725. #endif
  726. if (HYPERVISOR_sched_op(SCHEDOP_shutdown, reason))
  727. BUG();
  728. }
  729. static void xen_restart(char *msg)
  730. {
  731. xen_reboot(SHUTDOWN_reboot);
  732. }
  733. static void xen_emergency_restart(void)
  734. {
  735. xen_reboot(SHUTDOWN_reboot);
  736. }
  737. static void xen_machine_halt(void)
  738. {
  739. xen_reboot(SHUTDOWN_poweroff);
  740. }
  741. static void xen_crash_shutdown(struct pt_regs *regs)
  742. {
  743. xen_reboot(SHUTDOWN_crash);
  744. }
  745. static const struct machine_ops __initdata xen_machine_ops = {
  746. .restart = xen_restart,
  747. .halt = xen_machine_halt,
  748. .power_off = xen_machine_halt,
  749. .shutdown = xen_machine_halt,
  750. .crash_shutdown = xen_crash_shutdown,
  751. .emergency_restart = xen_emergency_restart,
  752. };
  753. /* First C function to be called on Xen boot */
  754. asmlinkage void __init xen_start_kernel(void)
  755. {
  756. pgd_t *pgd;
  757. if (!xen_start_info)
  758. return;
  759. BUG_ON(memcmp(xen_start_info->magic, "xen-3.0", 7) != 0);
  760. /* Install Xen paravirt ops */
  761. paravirt_ops = xen_paravirt_ops;
  762. machine_ops = xen_machine_ops;
  763. #ifdef CONFIG_SMP
  764. smp_ops = xen_smp_ops;
  765. #endif
  766. xen_setup_features();
  767. /* Get mfn list */
  768. if (!xen_feature(XENFEAT_auto_translated_physmap))
  769. phys_to_machine_mapping = (unsigned long *)xen_start_info->mfn_list;
  770. pgd = (pgd_t *)xen_start_info->pt_base;
  771. init_pg_tables_end = __pa(pgd) + xen_start_info->nr_pt_frames*PAGE_SIZE;
  772. init_mm.pgd = pgd; /* use the Xen pagetables to start */
  773. /* keep using Xen gdt for now; no urgent need to change it */
  774. x86_write_percpu(xen_cr3, __pa(pgd));
  775. xen_vcpu_setup(0);
  776. paravirt_ops.kernel_rpl = 1;
  777. if (xen_feature(XENFEAT_supervisor_mode_kernel))
  778. paravirt_ops.kernel_rpl = 0;
  779. /* set the limit of our address space */
  780. reserve_top_address(-HYPERVISOR_VIRT_START + 2 * PAGE_SIZE);
  781. /* set up basic CPUID stuff */
  782. cpu_detect(&new_cpu_data);
  783. new_cpu_data.hard_math = 1;
  784. new_cpu_data.x86_capability[0] = cpuid_edx(1);
  785. /* Poke various useful things into boot_params */
  786. LOADER_TYPE = (9 << 4) | 0;
  787. INITRD_START = xen_start_info->mod_start ? __pa(xen_start_info->mod_start) : 0;
  788. INITRD_SIZE = xen_start_info->mod_len;
  789. /* Start the world */
  790. start_kernel();
  791. }