enlighten.c 19 KB

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