enlighten.c 21 KB

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