2 * Core of Xen paravirt_ops implementation.
4 * This file contains the xen_paravirt_ops structure itself, and the
6 * - privileged instructions
11 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/smp.h>
17 #include <linux/preempt.h>
18 #include <linux/hardirq.h>
19 #include <linux/percpu.h>
20 #include <linux/delay.h>
21 #include <linux/start_kernel.h>
22 #include <linux/sched.h>
23 #include <linux/bootmem.h>
24 #include <linux/module.h>
26 #include <linux/page-flags.h>
27 #include <linux/highmem.h>
28 #include <linux/smp.h>
30 #include <xen/interface/xen.h>
31 #include <xen/interface/physdev.h>
32 #include <xen/interface/vcpu.h>
33 #include <xen/interface/sched.h>
34 #include <xen/features.h>
37 #include <asm/paravirt.h>
39 #include <asm/xen/hypercall.h>
40 #include <asm/xen/hypervisor.h>
41 #include <asm/fixmap.h>
42 #include <asm/processor.h>
43 #include <asm/setup.h>
45 #include <asm/pgtable.h>
46 #include <asm/tlbflush.h>
47 #include <asm/reboot.h>
51 #include "multicalls.h"
53 EXPORT_SYMBOL_GPL(hypercall_page
);
55 DEFINE_PER_CPU(enum paravirt_lazy_mode
, xen_lazy_mode
);
57 DEFINE_PER_CPU(struct vcpu_info
*, xen_vcpu
);
58 DEFINE_PER_CPU(struct vcpu_info
, xen_vcpu_info
);
59 DEFINE_PER_CPU(unsigned long, xen_cr3
);
61 struct start_info
*xen_start_info
;
62 EXPORT_SYMBOL_GPL(xen_start_info
);
64 static /* __initdata */ struct shared_info dummy_shared_info
;
67 * Point at some empty memory to start with. We map the real shared_info
68 * page as soon as fixmap is up and running.
70 struct shared_info
*HYPERVISOR_shared_info
= (void *)&dummy_shared_info
;
73 * Flag to determine whether vcpu info placement is available on all
74 * VCPUs. We assume it is to start with, and then set it to zero on
75 * the first failure. This is because it can succeed on some VCPUs
76 * and not others, since it can involve hypervisor memory allocation,
77 * or because the guest failed to guarantee all the appropriate
78 * constraints on all VCPUs (ie buffer can't cross a page boundary).
80 * Note that any particular CPU may be using a placed vcpu structure,
81 * but we can only optimise if the all are.
83 * 0: not available, 1: available
85 static int have_vcpu_info_placement
= 1;
87 static void __init
xen_vcpu_setup(int cpu
)
89 struct vcpu_register_vcpu_info info
;
91 struct vcpu_info
*vcpup
;
93 per_cpu(xen_vcpu
, cpu
) = &HYPERVISOR_shared_info
->vcpu_info
[cpu
];
95 if (!have_vcpu_info_placement
)
96 return; /* already tested, not available */
98 vcpup
= &per_cpu(xen_vcpu_info
, cpu
);
100 info
.mfn
= virt_to_mfn(vcpup
);
101 info
.offset
= offset_in_page(vcpup
);
103 printk(KERN_DEBUG
"trying to map vcpu_info %d at %p, mfn %x, offset %d\n",
104 cpu
, vcpup
, info
.mfn
, info
.offset
);
106 /* Check to see if the hypervisor will put the vcpu_info
107 structure where we want it, which allows direct access via
108 a percpu-variable. */
109 err
= HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info
, cpu
, &info
);
112 printk(KERN_DEBUG
"register_vcpu_info failed: err=%d\n", err
);
113 have_vcpu_info_placement
= 0;
115 /* This cpu is using the registered vcpu info, even if
116 later ones fail to. */
117 per_cpu(xen_vcpu
, cpu
) = vcpup
;
118 printk(KERN_DEBUG
"cpu %d using vcpu_info at %p\n",
123 static void __init
xen_banner(void)
125 printk(KERN_INFO
"Booting paravirtualized kernel on %s\n",
127 printk(KERN_INFO
"Hypervisor signature: %s\n", xen_start_info
->magic
);
130 static void xen_cpuid(unsigned int *eax
, unsigned int *ebx
,
131 unsigned int *ecx
, unsigned int *edx
)
133 unsigned maskedx
= ~0;
136 * Mask out inconvenient features, to try and disable as many
137 * unsupported kernel subsystems as possible.
140 maskedx
= ~((1 << X86_FEATURE_APIC
) | /* disable APIC */
141 (1 << X86_FEATURE_ACPI
) | /* disable ACPI */
142 (1 << X86_FEATURE_ACC
)); /* thermal monitoring */
144 asm(XEN_EMULATE_PREFIX
"cpuid"
149 : "0" (*eax
), "2" (*ecx
));
153 static void xen_set_debugreg(int reg
, unsigned long val
)
155 HYPERVISOR_set_debugreg(reg
, val
);
158 static unsigned long xen_get_debugreg(int reg
)
160 return HYPERVISOR_get_debugreg(reg
);
163 static unsigned long xen_save_fl(void)
165 struct vcpu_info
*vcpu
;
168 vcpu
= x86_read_percpu(xen_vcpu
);
170 /* flag has opposite sense of mask */
171 flags
= !vcpu
->evtchn_upcall_mask
;
173 /* convert to IF type flag
177 return (-flags
) & X86_EFLAGS_IF
;
180 static unsigned long xen_save_fl_direct(void)
184 /* flag has opposite sense of mask */
185 flags
= !x86_read_percpu(xen_vcpu_info
.evtchn_upcall_mask
);
187 /* convert to IF type flag
191 return (-flags
) & X86_EFLAGS_IF
;
194 static void xen_restore_fl(unsigned long flags
)
196 struct vcpu_info
*vcpu
;
198 /* convert from IF type flag */
199 flags
= !(flags
& X86_EFLAGS_IF
);
201 /* There's a one instruction preempt window here. We need to
202 make sure we're don't switch CPUs between getting the vcpu
203 pointer and updating the mask. */
205 vcpu
= x86_read_percpu(xen_vcpu
);
206 vcpu
->evtchn_upcall_mask
= flags
;
207 preempt_enable_no_resched();
209 /* Doesn't matter if we get preempted here, because any
210 pending event will get dealt with anyway. */
213 preempt_check_resched();
214 barrier(); /* unmask then check (avoid races) */
215 if (unlikely(vcpu
->evtchn_upcall_pending
))
216 force_evtchn_callback();
220 static void xen_restore_fl_direct(unsigned long flags
)
222 /* convert from IF type flag */
223 flags
= !(flags
& X86_EFLAGS_IF
);
225 /* This is an atomic update, so no need to worry about
227 x86_write_percpu(xen_vcpu_info
.evtchn_upcall_mask
, flags
);
229 /* If we get preempted here, then any pending event will be
233 barrier(); /* unmask then check (avoid races) */
234 if (unlikely(x86_read_percpu(xen_vcpu_info
.evtchn_upcall_pending
)))
235 force_evtchn_callback();
239 static void xen_irq_disable(void)
241 /* There's a one instruction preempt window here. We need to
242 make sure we're don't switch CPUs between getting the vcpu
243 pointer and updating the mask. */
245 x86_read_percpu(xen_vcpu
)->evtchn_upcall_mask
= 1;
246 preempt_enable_no_resched();
249 static void xen_irq_disable_direct(void)
251 /* Atomic update, so preemption not a concern. */
252 x86_write_percpu(xen_vcpu_info
.evtchn_upcall_mask
, 1);
255 static void xen_irq_enable(void)
257 struct vcpu_info
*vcpu
;
259 /* There's a one instruction preempt window here. We need to
260 make sure we're don't switch CPUs between getting the vcpu
261 pointer and updating the mask. */
263 vcpu
= x86_read_percpu(xen_vcpu
);
264 vcpu
->evtchn_upcall_mask
= 0;
265 preempt_enable_no_resched();
267 /* Doesn't matter if we get preempted here, because any
268 pending event will get dealt with anyway. */
270 barrier(); /* unmask then check (avoid races) */
271 if (unlikely(vcpu
->evtchn_upcall_pending
))
272 force_evtchn_callback();
275 static void xen_irq_enable_direct(void)
277 /* Atomic update, so preemption not a concern. */
278 x86_write_percpu(xen_vcpu_info
.evtchn_upcall_mask
, 0);
280 /* Doesn't matter if we get preempted here, because any
281 pending event will get dealt with anyway. */
283 barrier(); /* unmask then check (avoid races) */
284 if (unlikely(x86_read_percpu(xen_vcpu_info
.evtchn_upcall_pending
)))
285 force_evtchn_callback();
288 static void xen_safe_halt(void)
290 /* Blocking includes an implicit local_irq_enable(). */
291 if (HYPERVISOR_sched_op(SCHEDOP_block
, 0) != 0)
295 static void xen_halt(void)
298 HYPERVISOR_vcpu_op(VCPUOP_down
, smp_processor_id(), NULL
);
303 static void xen_set_lazy_mode(enum paravirt_lazy_mode mode
)
305 BUG_ON(preemptible());
308 case PARAVIRT_LAZY_NONE
:
309 BUG_ON(x86_read_percpu(xen_lazy_mode
) == PARAVIRT_LAZY_NONE
);
312 case PARAVIRT_LAZY_MMU
:
313 case PARAVIRT_LAZY_CPU
:
314 BUG_ON(x86_read_percpu(xen_lazy_mode
) != PARAVIRT_LAZY_NONE
);
317 case PARAVIRT_LAZY_FLUSH
:
318 /* flush if necessary, but don't change state */
319 if (x86_read_percpu(xen_lazy_mode
) != PARAVIRT_LAZY_NONE
)
325 x86_write_percpu(xen_lazy_mode
, mode
);
328 static unsigned long xen_store_tr(void)
333 static void xen_set_ldt(const void *addr
, unsigned entries
)
335 unsigned long linear_addr
= (unsigned long)addr
;
336 struct mmuext_op
*op
;
337 struct multicall_space mcs
= xen_mc_entry(sizeof(*op
));
340 op
->cmd
= MMUEXT_SET_LDT
;
342 /* ldt my be vmalloced, use arbitrary_virt_to_machine */
344 maddr
= arbitrary_virt_to_machine((unsigned long)addr
);
345 linear_addr
= (unsigned long)maddr
.maddr
;
347 op
->arg1
.linear_addr
= linear_addr
;
348 op
->arg2
.nr_ents
= entries
;
350 MULTI_mmuext_op(mcs
.mc
, op
, 1, NULL
, DOMID_SELF
);
352 xen_mc_issue(PARAVIRT_LAZY_CPU
);
355 static void xen_load_gdt(const struct Xgt_desc_struct
*dtr
)
357 unsigned long *frames
;
358 unsigned long va
= dtr
->address
;
359 unsigned int size
= dtr
->size
+ 1;
360 unsigned pages
= (size
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
362 struct multicall_space mcs
;
364 /* A GDT can be up to 64k in size, which corresponds to 8192
365 8-byte entries, or 16 4k pages.. */
367 BUG_ON(size
> 65536);
368 BUG_ON(va
& ~PAGE_MASK
);
370 mcs
= xen_mc_entry(sizeof(*frames
) * pages
);
373 for (f
= 0; va
< dtr
->address
+ size
; va
+= PAGE_SIZE
, f
++) {
374 frames
[f
] = virt_to_mfn(va
);
375 make_lowmem_page_readonly((void *)va
);
378 MULTI_set_gdt(mcs
.mc
, frames
, size
/ sizeof(struct desc_struct
));
380 xen_mc_issue(PARAVIRT_LAZY_CPU
);
383 static void load_TLS_descriptor(struct thread_struct
*t
,
384 unsigned int cpu
, unsigned int i
)
386 struct desc_struct
*gdt
= get_cpu_gdt_table(cpu
);
387 xmaddr_t maddr
= virt_to_machine(&gdt
[GDT_ENTRY_TLS_MIN
+i
]);
388 struct multicall_space mc
= __xen_mc_entry(0);
390 MULTI_update_descriptor(mc
.mc
, maddr
.maddr
, t
->tls_array
[i
]);
393 static void xen_load_tls(struct thread_struct
*t
, unsigned int cpu
)
397 load_TLS_descriptor(t
, cpu
, 0);
398 load_TLS_descriptor(t
, cpu
, 1);
399 load_TLS_descriptor(t
, cpu
, 2);
401 xen_mc_issue(PARAVIRT_LAZY_CPU
);
404 * XXX sleazy hack: If we're being called in a lazy-cpu zone,
405 * it means we're in a context switch, and %gs has just been
406 * saved. This means we can zero it out to prevent faults on
407 * exit from the hypervisor if the next process has no %gs.
408 * Either way, it has been saved, and the new value will get
409 * loaded properly. This will go away as soon as Xen has been
410 * modified to not save/restore %gs for normal hypercalls.
412 if (xen_get_lazy_mode() == PARAVIRT_LAZY_CPU
)
416 static void xen_write_ldt_entry(struct desc_struct
*dt
, int entrynum
,
419 unsigned long lp
= (unsigned long)&dt
[entrynum
];
420 xmaddr_t mach_lp
= virt_to_machine(lp
);
421 u64 entry
= (u64
)high
<< 32 | low
;
426 if (HYPERVISOR_update_descriptor(mach_lp
.maddr
, entry
))
432 static int cvt_gate_to_trap(int vector
, u32 low
, u32 high
,
433 struct trap_info
*info
)
437 type
= (high
>> 8) & 0x1f;
438 dpl
= (high
>> 13) & 3;
440 if (type
!= 0xf && type
!= 0xe)
443 info
->vector
= vector
;
444 info
->address
= (high
& 0xffff0000) | (low
& 0x0000ffff);
445 info
->cs
= low
>> 16;
447 /* interrupt gates clear IF */
454 /* Locations of each CPU's IDT */
455 static DEFINE_PER_CPU(struct Xgt_desc_struct
, idt_desc
);
457 /* Set an IDT entry. If the entry is part of the current IDT, then
459 static void xen_write_idt_entry(struct desc_struct
*dt
, int entrynum
,
462 unsigned long p
= (unsigned long)&dt
[entrynum
];
463 unsigned long start
, end
;
467 start
= __get_cpu_var(idt_desc
).address
;
468 end
= start
+ __get_cpu_var(idt_desc
).size
+ 1;
472 write_dt_entry(dt
, entrynum
, low
, high
);
474 if (p
>= start
&& (p
+ 8) <= end
) {
475 struct trap_info info
[2];
479 if (cvt_gate_to_trap(entrynum
, low
, high
, &info
[0]))
480 if (HYPERVISOR_set_trap_table(info
))
487 static void xen_convert_trap_info(const struct Xgt_desc_struct
*desc
,
488 struct trap_info
*traps
)
490 unsigned in
, out
, count
;
492 count
= (desc
->size
+1) / 8;
495 for (in
= out
= 0; in
< count
; in
++) {
496 const u32
*entry
= (u32
*)(desc
->address
+ in
* 8);
498 if (cvt_gate_to_trap(in
, entry
[0], entry
[1], &traps
[out
]))
501 traps
[out
].address
= 0;
504 void xen_copy_trap_info(struct trap_info
*traps
)
506 const struct Xgt_desc_struct
*desc
= &__get_cpu_var(idt_desc
);
508 xen_convert_trap_info(desc
, traps
);
511 /* Load a new IDT into Xen. In principle this can be per-CPU, so we
512 hold a spinlock to protect the static traps[] array (static because
513 it avoids allocation, and saves stack space). */
514 static void xen_load_idt(const struct Xgt_desc_struct
*desc
)
516 static DEFINE_SPINLOCK(lock
);
517 static struct trap_info traps
[257];
521 __get_cpu_var(idt_desc
) = *desc
;
523 xen_convert_trap_info(desc
, traps
);
526 if (HYPERVISOR_set_trap_table(traps
))
532 /* Write a GDT descriptor entry. Ignore LDT descriptors, since
533 they're handled differently. */
534 static void xen_write_gdt_entry(struct desc_struct
*dt
, int entry
,
539 switch ((high
>> 8) & 0xff) {
546 xmaddr_t maddr
= virt_to_machine(&dt
[entry
]);
547 u64 desc
= (u64
)high
<< 32 | low
;
550 if (HYPERVISOR_update_descriptor(maddr
.maddr
, desc
))
559 static void xen_load_esp0(struct tss_struct
*tss
,
560 struct thread_struct
*thread
)
562 struct multicall_space mcs
= xen_mc_entry(0);
563 MULTI_stack_switch(mcs
.mc
, __KERNEL_DS
, thread
->esp0
);
564 xen_mc_issue(PARAVIRT_LAZY_CPU
);
567 static void xen_set_iopl_mask(unsigned mask
)
569 struct physdev_set_iopl set_iopl
;
571 /* Force the change at ring 0. */
572 set_iopl
.iopl
= (mask
== 0) ? 1 : (mask
>> 12) & 3;
573 HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl
, &set_iopl
);
576 static void xen_io_delay(void)
580 #ifdef CONFIG_X86_LOCAL_APIC
581 static unsigned long xen_apic_read(unsigned long reg
)
586 static void xen_apic_write(unsigned long reg
, unsigned long val
)
588 /* Warn to see if there's any stray references */
593 static void xen_flush_tlb(void)
595 struct mmuext_op
*op
;
596 struct multicall_space mcs
= xen_mc_entry(sizeof(*op
));
599 op
->cmd
= MMUEXT_TLB_FLUSH_LOCAL
;
600 MULTI_mmuext_op(mcs
.mc
, op
, 1, NULL
, DOMID_SELF
);
602 xen_mc_issue(PARAVIRT_LAZY_MMU
);
605 static void xen_flush_tlb_single(unsigned long addr
)
607 struct mmuext_op
*op
;
608 struct multicall_space mcs
= xen_mc_entry(sizeof(*op
));
611 op
->cmd
= MMUEXT_INVLPG_LOCAL
;
612 op
->arg1
.linear_addr
= addr
& PAGE_MASK
;
613 MULTI_mmuext_op(mcs
.mc
, op
, 1, NULL
, DOMID_SELF
);
615 xen_mc_issue(PARAVIRT_LAZY_MMU
);
618 static void xen_flush_tlb_others(const cpumask_t
*cpus
, struct mm_struct
*mm
,
625 cpumask_t cpumask
= *cpus
;
626 struct multicall_space mcs
;
629 * A couple of (to be removed) sanity checks:
631 * - current CPU must not be in mask
632 * - mask must exist :)
634 BUG_ON(cpus_empty(cpumask
));
635 BUG_ON(cpu_isset(smp_processor_id(), cpumask
));
638 /* If a CPU which we ran on has gone down, OK. */
639 cpus_and(cpumask
, cpumask
, cpu_online_map
);
640 if (cpus_empty(cpumask
))
643 mcs
= xen_mc_entry(sizeof(*args
));
645 args
->mask
= cpumask
;
646 args
->op
.arg2
.vcpumask
= &args
->mask
;
648 if (va
== TLB_FLUSH_ALL
) {
649 args
->op
.cmd
= MMUEXT_TLB_FLUSH_MULTI
;
651 args
->op
.cmd
= MMUEXT_INVLPG_MULTI
;
652 args
->op
.arg1
.linear_addr
= va
;
655 MULTI_mmuext_op(mcs
.mc
, &args
->op
, 1, NULL
, DOMID_SELF
);
657 xen_mc_issue(PARAVIRT_LAZY_MMU
);
660 static void xen_write_cr2(unsigned long cr2
)
662 x86_read_percpu(xen_vcpu
)->arch
.cr2
= cr2
;
665 static unsigned long xen_read_cr2(void)
667 return x86_read_percpu(xen_vcpu
)->arch
.cr2
;
670 static unsigned long xen_read_cr2_direct(void)
672 return x86_read_percpu(xen_vcpu_info
.arch
.cr2
);
675 static void xen_write_cr4(unsigned long cr4
)
677 /* never allow TSC to be disabled */
678 native_write_cr4(cr4
& ~X86_CR4_TSD
);
681 static unsigned long xen_read_cr3(void)
683 return x86_read_percpu(xen_cr3
);
686 static void xen_write_cr3(unsigned long cr3
)
688 BUG_ON(preemptible());
690 if (cr3
== x86_read_percpu(xen_cr3
)) {
691 /* just a simple tlb flush */
696 x86_write_percpu(xen_cr3
, cr3
);
700 struct mmuext_op
*op
;
701 struct multicall_space mcs
= xen_mc_entry(sizeof(*op
));
702 unsigned long mfn
= pfn_to_mfn(PFN_DOWN(cr3
));
705 op
->cmd
= MMUEXT_NEW_BASEPTR
;
708 MULTI_mmuext_op(mcs
.mc
, op
, 1, NULL
, DOMID_SELF
);
710 xen_mc_issue(PARAVIRT_LAZY_CPU
);
714 /* Early in boot, while setting up the initial pagetable, assume
715 everything is pinned. */
716 static __init
void xen_alloc_pt_init(struct mm_struct
*mm
, u32 pfn
)
718 BUG_ON(mem_map
); /* should only be used early */
719 make_lowmem_page_readonly(__va(PFN_PHYS(pfn
)));
722 /* This needs to make sure the new pte page is pinned iff its being
723 attached to a pinned pagetable. */
724 static void xen_alloc_pt(struct mm_struct
*mm
, u32 pfn
)
726 struct page
*page
= pfn_to_page(pfn
);
728 if (PagePinned(virt_to_page(mm
->pgd
))) {
731 if (!PageHighMem(page
))
732 make_lowmem_page_readonly(__va(PFN_PHYS(pfn
)));
734 /* make sure there are no stray mappings of
740 /* This should never happen until we're OK to use struct page */
741 static void xen_release_pt(u32 pfn
)
743 struct page
*page
= pfn_to_page(pfn
);
745 if (PagePinned(page
)) {
746 if (!PageHighMem(page
))
747 make_lowmem_page_readwrite(__va(PFN_PHYS(pfn
)));
751 #ifdef CONFIG_HIGHPTE
752 static void *xen_kmap_atomic_pte(struct page
*page
, enum km_type type
)
754 pgprot_t prot
= PAGE_KERNEL
;
756 if (PagePinned(page
))
757 prot
= PAGE_KERNEL_RO
;
759 if (0 && PageHighMem(page
))
760 printk("mapping highpte %lx type %d prot %s\n",
761 page_to_pfn(page
), type
,
762 (unsigned long)pgprot_val(prot
) & _PAGE_RW
? "WRITE" : "READ");
764 return kmap_atomic_prot(page
, type
, prot
);
768 static __init pte_t
mask_rw_pte(pte_t
*ptep
, pte_t pte
)
770 /* If there's an existing pte, then don't allow _PAGE_RW to be set */
771 if (pte_val_ma(*ptep
) & _PAGE_PRESENT
)
772 pte
= __pte_ma(((pte_val_ma(*ptep
) & _PAGE_RW
) | ~_PAGE_RW
) &
778 /* Init-time set_pte while constructing initial pagetables, which
779 doesn't allow RO pagetable pages to be remapped RW */
780 static __init
void xen_set_pte_init(pte_t
*ptep
, pte_t pte
)
782 pte
= mask_rw_pte(ptep
, pte
);
784 xen_set_pte(ptep
, pte
);
787 static __init
void xen_pagetable_setup_start(pgd_t
*base
)
789 pgd_t
*xen_pgd
= (pgd_t
*)xen_start_info
->pt_base
;
791 /* special set_pte for pagetable initialization */
792 paravirt_ops
.set_pte
= xen_set_pte_init
;
796 * copy top-level of Xen-supplied pagetable into place. For
797 * !PAE we can use this as-is, but for PAE it is a stand-in
798 * while we copy the pmd pages.
800 memcpy(base
, xen_pgd
, PTRS_PER_PGD
* sizeof(pgd_t
));
802 if (PTRS_PER_PMD
> 1) {
805 * For PAE, need to allocate new pmds, rather than
806 * share Xen's, since Xen doesn't like pmd's being
807 * shared between address spaces.
809 for (i
= 0; i
< PTRS_PER_PGD
; i
++) {
810 if (pgd_val_ma(xen_pgd
[i
]) & _PAGE_PRESENT
) {
811 pmd_t
*pmd
= (pmd_t
*)alloc_bootmem_low_pages(PAGE_SIZE
);
813 memcpy(pmd
, (void *)pgd_page_vaddr(xen_pgd
[i
]),
816 make_lowmem_page_readonly(pmd
);
818 set_pgd(&base
[i
], __pgd(1 + __pa(pmd
)));
824 /* make sure zero_page is mapped RO so we can use it in pagetables */
825 make_lowmem_page_readonly(empty_zero_page
);
826 make_lowmem_page_readonly(base
);
828 * Switch to new pagetable. This is done before
829 * pagetable_init has done anything so that the new pages
830 * added to the table can be prepared properly for Xen.
832 xen_write_cr3(__pa(base
));
835 static __init
void xen_pagetable_setup_done(pgd_t
*base
)
837 /* This will work as long as patching hasn't happened yet
839 paravirt_ops
.alloc_pt
= xen_alloc_pt
;
840 paravirt_ops
.set_pte
= xen_set_pte
;
842 if (!xen_feature(XENFEAT_auto_translated_physmap
)) {
844 * Create a mapping for the shared info page.
845 * Should be set_fixmap(), but shared_info is a machine
846 * address with no corresponding pseudo-phys address.
848 set_pte_mfn(fix_to_virt(FIX_PARAVIRT_BOOTMAP
),
849 PFN_DOWN(xen_start_info
->shared_info
),
852 HYPERVISOR_shared_info
=
853 (struct shared_info
*)fix_to_virt(FIX_PARAVIRT_BOOTMAP
);
856 HYPERVISOR_shared_info
=
857 (struct shared_info
*)__va(xen_start_info
->shared_info
);
859 /* Actually pin the pagetable down, but we can't set PG_pinned
860 yet because the page structures don't exist yet. */
863 #ifdef CONFIG_X86_PAE
864 op
.cmd
= MMUEXT_PIN_L3_TABLE
;
866 op
.cmd
= MMUEXT_PIN_L3_TABLE
;
868 op
.arg1
.mfn
= pfn_to_mfn(PFN_DOWN(__pa(base
)));
869 if (HYPERVISOR_mmuext_op(&op
, 1, NULL
, DOMID_SELF
))
874 /* This is called once we have the cpu_possible_map */
875 void __init
xen_setup_vcpu_info_placement(void)
879 for_each_possible_cpu(cpu
)
882 /* xen_vcpu_setup managed to place the vcpu_info within the
883 percpu area for all cpus, so make use of it */
884 if (have_vcpu_info_placement
) {
885 printk(KERN_INFO
"Xen: using vcpu_info placement\n");
887 paravirt_ops
.save_fl
= xen_save_fl_direct
;
888 paravirt_ops
.restore_fl
= xen_restore_fl_direct
;
889 paravirt_ops
.irq_disable
= xen_irq_disable_direct
;
890 paravirt_ops
.irq_enable
= xen_irq_enable_direct
;
891 paravirt_ops
.read_cr2
= xen_read_cr2_direct
;
895 static const struct paravirt_ops xen_paravirt_ops __initdata
= {
896 .paravirt_enabled
= 1,
897 .shared_kernel_pmd
= 0,
900 .banner
= xen_banner
,
902 .patch
= paravirt_patch_default
,
904 .memory_setup
= xen_memory_setup
,
905 .arch_setup
= xen_arch_setup
,
906 .init_IRQ
= xen_init_IRQ
,
907 .post_allocator_init
= xen_mark_init_mm_pinned
,
909 .time_init
= xen_time_init
,
910 .set_wallclock
= xen_set_wallclock
,
911 .get_wallclock
= xen_get_wallclock
,
912 .get_cpu_khz
= xen_cpu_khz
,
913 .sched_clock
= xen_sched_clock
,
917 .set_debugreg
= xen_set_debugreg
,
918 .get_debugreg
= xen_get_debugreg
,
922 .read_cr0
= native_read_cr0
,
923 .write_cr0
= native_write_cr0
,
925 .read_cr2
= xen_read_cr2
,
926 .write_cr2
= xen_write_cr2
,
928 .read_cr3
= xen_read_cr3
,
929 .write_cr3
= xen_write_cr3
,
931 .read_cr4
= native_read_cr4
,
932 .read_cr4_safe
= native_read_cr4_safe
,
933 .write_cr4
= xen_write_cr4
,
935 .save_fl
= xen_save_fl
,
936 .restore_fl
= xen_restore_fl
,
937 .irq_disable
= xen_irq_disable
,
938 .irq_enable
= xen_irq_enable
,
939 .safe_halt
= xen_safe_halt
,
941 .wbinvd
= native_wbinvd
,
943 .read_msr
= native_read_msr_safe
,
944 .write_msr
= native_write_msr_safe
,
945 .read_tsc
= native_read_tsc
,
946 .read_pmc
= native_read_pmc
,
948 .iret
= (void *)&hypercall_page
[__HYPERVISOR_iret
],
949 .irq_enable_sysexit
= NULL
, /* never called */
951 .load_tr_desc
= paravirt_nop
,
952 .set_ldt
= xen_set_ldt
,
953 .load_gdt
= xen_load_gdt
,
954 .load_idt
= xen_load_idt
,
955 .load_tls
= xen_load_tls
,
957 .store_gdt
= native_store_gdt
,
958 .store_idt
= native_store_idt
,
959 .store_tr
= xen_store_tr
,
961 .write_ldt_entry
= xen_write_ldt_entry
,
962 .write_gdt_entry
= xen_write_gdt_entry
,
963 .write_idt_entry
= xen_write_idt_entry
,
964 .load_esp0
= xen_load_esp0
,
966 .set_iopl_mask
= xen_set_iopl_mask
,
967 .io_delay
= xen_io_delay
,
969 #ifdef CONFIG_X86_LOCAL_APIC
970 .apic_write
= xen_apic_write
,
971 .apic_write_atomic
= xen_apic_write
,
972 .apic_read
= xen_apic_read
,
973 .setup_boot_clock
= paravirt_nop
,
974 .setup_secondary_clock
= paravirt_nop
,
975 .startup_ipi_hook
= paravirt_nop
,
978 .flush_tlb_user
= xen_flush_tlb
,
979 .flush_tlb_kernel
= xen_flush_tlb
,
980 .flush_tlb_single
= xen_flush_tlb_single
,
981 .flush_tlb_others
= xen_flush_tlb_others
,
983 .pte_update
= paravirt_nop
,
984 .pte_update_defer
= paravirt_nop
,
986 .pagetable_setup_start
= xen_pagetable_setup_start
,
987 .pagetable_setup_done
= xen_pagetable_setup_done
,
989 .alloc_pt
= xen_alloc_pt_init
,
990 .release_pt
= xen_release_pt
,
991 .alloc_pd
= paravirt_nop
,
992 .alloc_pd_clone
= paravirt_nop
,
993 .release_pd
= paravirt_nop
,
995 #ifdef CONFIG_HIGHPTE
996 .kmap_atomic_pte
= xen_kmap_atomic_pte
,
999 .set_pte
= NULL
, /* see xen_pagetable_setup_* */
1000 .set_pte_at
= xen_set_pte_at
,
1001 .set_pmd
= xen_set_pmd
,
1003 .pte_val
= xen_pte_val
,
1004 .pgd_val
= xen_pgd_val
,
1006 .make_pte
= xen_make_pte
,
1007 .make_pgd
= xen_make_pgd
,
1009 #ifdef CONFIG_X86_PAE
1010 .set_pte_atomic
= xen_set_pte_atomic
,
1011 .set_pte_present
= xen_set_pte_at
,
1012 .set_pud
= xen_set_pud
,
1013 .pte_clear
= xen_pte_clear
,
1014 .pmd_clear
= xen_pmd_clear
,
1016 .make_pmd
= xen_make_pmd
,
1017 .pmd_val
= xen_pmd_val
,
1020 .activate_mm
= xen_activate_mm
,
1021 .dup_mmap
= xen_dup_mmap
,
1022 .exit_mmap
= xen_exit_mmap
,
1024 .set_lazy_mode
= xen_set_lazy_mode
,
1028 static const struct smp_ops xen_smp_ops __initdata
= {
1029 .smp_prepare_boot_cpu
= xen_smp_prepare_boot_cpu
,
1030 .smp_prepare_cpus
= xen_smp_prepare_cpus
,
1031 .cpu_up
= xen_cpu_up
,
1032 .smp_cpus_done
= xen_smp_cpus_done
,
1034 .smp_send_stop
= xen_smp_send_stop
,
1035 .smp_send_reschedule
= xen_smp_send_reschedule
,
1036 .smp_call_function_mask
= xen_smp_call_function_mask
,
1038 #endif /* CONFIG_SMP */
1040 static void xen_reboot(int reason
)
1046 if (HYPERVISOR_sched_op(SCHEDOP_shutdown
, reason
))
1050 static void xen_restart(char *msg
)
1052 xen_reboot(SHUTDOWN_reboot
);
1055 static void xen_emergency_restart(void)
1057 xen_reboot(SHUTDOWN_reboot
);
1060 static void xen_machine_halt(void)
1062 xen_reboot(SHUTDOWN_poweroff
);
1065 static void xen_crash_shutdown(struct pt_regs
*regs
)
1067 xen_reboot(SHUTDOWN_crash
);
1070 static const struct machine_ops __initdata xen_machine_ops
= {
1071 .restart
= xen_restart
,
1072 .halt
= xen_machine_halt
,
1073 .power_off
= xen_machine_halt
,
1074 .shutdown
= xen_machine_halt
,
1075 .crash_shutdown
= xen_crash_shutdown
,
1076 .emergency_restart
= xen_emergency_restart
,
1079 /* First C function to be called on Xen boot */
1080 asmlinkage
void __init
xen_start_kernel(void)
1084 if (!xen_start_info
)
1087 BUG_ON(memcmp(xen_start_info
->magic
, "xen-3.0", 7) != 0);
1089 /* Install Xen paravirt ops */
1090 paravirt_ops
= xen_paravirt_ops
;
1091 machine_ops
= xen_machine_ops
;
1094 smp_ops
= xen_smp_ops
;
1097 xen_setup_features();
1100 if (!xen_feature(XENFEAT_auto_translated_physmap
))
1101 phys_to_machine_mapping
= (unsigned long *)xen_start_info
->mfn_list
;
1103 pgd
= (pgd_t
*)xen_start_info
->pt_base
;
1105 init_pg_tables_end
= __pa(pgd
) + xen_start_info
->nr_pt_frames
*PAGE_SIZE
;
1107 init_mm
.pgd
= pgd
; /* use the Xen pagetables to start */
1109 /* keep using Xen gdt for now; no urgent need to change it */
1111 x86_write_percpu(xen_cr3
, __pa(pgd
));
1114 /* Don't do the full vcpu_info placement stuff until we have a
1116 per_cpu(xen_vcpu
, 0) = &HYPERVISOR_shared_info
->vcpu_info
[0];
1118 /* May as well do it now, since there's no good time to call
1120 xen_setup_vcpu_info_placement();
1123 paravirt_ops
.kernel_rpl
= 1;
1124 if (xen_feature(XENFEAT_supervisor_mode_kernel
))
1125 paravirt_ops
.kernel_rpl
= 0;
1127 /* set the limit of our address space */
1128 reserve_top_address(-HYPERVISOR_VIRT_START
+ 2 * PAGE_SIZE
);
1130 /* set up basic CPUID stuff */
1131 cpu_detect(&new_cpu_data
);
1132 new_cpu_data
.hard_math
= 1;
1133 new_cpu_data
.x86_capability
[0] = cpuid_edx(1);
1135 /* Poke various useful things into boot_params */
1136 LOADER_TYPE
= (9 << 4) | 0;
1137 INITRD_START
= xen_start_info
->mod_start
? __pa(xen_start_info
->mod_start
) : 0;
1138 INITRD_SIZE
= xen_start_info
->mod_len
;
1140 /* Start the world */