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/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>
25 #include <xen/interface/xen.h>
26 #include <xen/interface/physdev.h>
27 #include <xen/interface/vcpu.h>
28 #include <xen/features.h>
31 #include <asm/paravirt.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>
39 #include <asm/pgtable.h>
43 #include "multicalls.h"
45 EXPORT_SYMBOL_GPL(hypercall_page
);
47 DEFINE_PER_CPU(enum paravirt_lazy_mode
, xen_lazy_mode
);
49 DEFINE_PER_CPU(struct vcpu_info
*, xen_vcpu
);
50 DEFINE_PER_CPU(struct vcpu_info
, xen_vcpu_info
);
51 DEFINE_PER_CPU(unsigned long, xen_cr3
);
53 struct start_info
*xen_start_info
;
54 EXPORT_SYMBOL_GPL(xen_start_info
);
56 static void xen_vcpu_setup(int cpu
)
58 per_cpu(xen_vcpu
, cpu
) = &HYPERVISOR_shared_info
->vcpu_info
[cpu
];
61 static void __init
xen_banner(void)
63 printk(KERN_INFO
"Booting paravirtualized kernel on %s\n",
65 printk(KERN_INFO
"Hypervisor signature: %s\n", xen_start_info
->magic
);
68 static void xen_cpuid(unsigned int *eax
, unsigned int *ebx
,
69 unsigned int *ecx
, unsigned int *edx
)
71 unsigned maskedx
= ~0;
74 * Mask out inconvenient features, to try and disable as many
75 * unsupported kernel subsystems as possible.
78 maskedx
= ~((1 << X86_FEATURE_APIC
) | /* disable APIC */
79 (1 << X86_FEATURE_ACPI
) | /* disable ACPI */
80 (1 << X86_FEATURE_ACC
)); /* thermal monitoring */
82 asm(XEN_EMULATE_PREFIX
"cpuid"
87 : "0" (*eax
), "2" (*ecx
));
91 static void xen_set_debugreg(int reg
, unsigned long val
)
93 HYPERVISOR_set_debugreg(reg
, val
);
96 static unsigned long xen_get_debugreg(int reg
)
98 return HYPERVISOR_get_debugreg(reg
);
101 static unsigned long xen_save_fl(void)
103 struct vcpu_info
*vcpu
;
107 vcpu
= x86_read_percpu(xen_vcpu
);
108 /* flag has opposite sense of mask */
109 flags
= !vcpu
->evtchn_upcall_mask
;
112 /* convert to IF type flag
116 return (-flags
) & X86_EFLAGS_IF
;
119 static void xen_restore_fl(unsigned long flags
)
121 struct vcpu_info
*vcpu
;
125 /* convert from IF type flag */
126 flags
= !(flags
& X86_EFLAGS_IF
);
127 vcpu
= x86_read_percpu(xen_vcpu
);
128 vcpu
->evtchn_upcall_mask
= flags
;
131 /* Unmask then check (avoid races). We're only protecting
132 against updates by this CPU, so there's no need for
133 anything stronger. */
136 if (unlikely(vcpu
->evtchn_upcall_pending
))
137 force_evtchn_callback();
140 preempt_enable_no_resched();
143 static void xen_irq_disable(void)
145 struct vcpu_info
*vcpu
;
147 vcpu
= x86_read_percpu(xen_vcpu
);
148 vcpu
->evtchn_upcall_mask
= 1;
149 preempt_enable_no_resched();
152 static void xen_irq_enable(void)
154 struct vcpu_info
*vcpu
;
157 vcpu
= x86_read_percpu(xen_vcpu
);
158 vcpu
->evtchn_upcall_mask
= 0;
160 /* Unmask then check (avoid races). We're only protecting
161 against updates by this CPU, so there's no need for
162 anything stronger. */
165 if (unlikely(vcpu
->evtchn_upcall_pending
))
166 force_evtchn_callback();
170 static void xen_safe_halt(void)
172 /* Blocking includes an implicit local_irq_enable(). */
173 if (HYPERVISOR_sched_op(SCHEDOP_block
, 0) != 0)
177 static void xen_halt(void)
180 HYPERVISOR_vcpu_op(VCPUOP_down
, smp_processor_id(), NULL
);
185 static void xen_set_lazy_mode(enum paravirt_lazy_mode mode
)
188 case PARAVIRT_LAZY_NONE
:
189 BUG_ON(x86_read_percpu(xen_lazy_mode
) == PARAVIRT_LAZY_NONE
);
192 case PARAVIRT_LAZY_MMU
:
193 case PARAVIRT_LAZY_CPU
:
194 BUG_ON(x86_read_percpu(xen_lazy_mode
) != PARAVIRT_LAZY_NONE
);
197 case PARAVIRT_LAZY_FLUSH
:
198 /* flush if necessary, but don't change state */
199 if (x86_read_percpu(xen_lazy_mode
) != PARAVIRT_LAZY_NONE
)
205 x86_write_percpu(xen_lazy_mode
, mode
);
208 static unsigned long xen_store_tr(void)
213 static void xen_set_ldt(const void *addr
, unsigned entries
)
215 unsigned long linear_addr
= (unsigned long)addr
;
216 struct mmuext_op
*op
;
217 struct multicall_space mcs
= xen_mc_entry(sizeof(*op
));
220 op
->cmd
= MMUEXT_SET_LDT
;
222 /* ldt my be vmalloced, use arbitrary_virt_to_machine */
224 maddr
= arbitrary_virt_to_machine((unsigned long)addr
);
225 linear_addr
= (unsigned long)maddr
.maddr
;
227 op
->arg1
.linear_addr
= linear_addr
;
228 op
->arg2
.nr_ents
= entries
;
230 MULTI_mmuext_op(mcs
.mc
, op
, 1, NULL
, DOMID_SELF
);
232 xen_mc_issue(PARAVIRT_LAZY_CPU
);
235 static void xen_load_gdt(const struct Xgt_desc_struct
*dtr
)
237 unsigned long *frames
;
238 unsigned long va
= dtr
->address
;
239 unsigned int size
= dtr
->size
+ 1;
240 unsigned pages
= (size
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
242 struct multicall_space mcs
;
244 /* A GDT can be up to 64k in size, which corresponds to 8192
245 8-byte entries, or 16 4k pages.. */
247 BUG_ON(size
> 65536);
248 BUG_ON(va
& ~PAGE_MASK
);
250 mcs
= xen_mc_entry(sizeof(*frames
) * pages
);
253 for (f
= 0; va
< dtr
->address
+ size
; va
+= PAGE_SIZE
, f
++) {
254 frames
[f
] = virt_to_mfn(va
);
255 make_lowmem_page_readonly((void *)va
);
258 MULTI_set_gdt(mcs
.mc
, frames
, size
/ sizeof(struct desc_struct
));
260 xen_mc_issue(PARAVIRT_LAZY_CPU
);
263 static void load_TLS_descriptor(struct thread_struct
*t
,
264 unsigned int cpu
, unsigned int i
)
266 struct desc_struct
*gdt
= get_cpu_gdt_table(cpu
);
267 xmaddr_t maddr
= virt_to_machine(&gdt
[GDT_ENTRY_TLS_MIN
+i
]);
268 struct multicall_space mc
= __xen_mc_entry(0);
270 MULTI_update_descriptor(mc
.mc
, maddr
.maddr
, t
->tls_array
[i
]);
273 static void xen_load_tls(struct thread_struct
*t
, unsigned int cpu
)
277 load_TLS_descriptor(t
, cpu
, 0);
278 load_TLS_descriptor(t
, cpu
, 1);
279 load_TLS_descriptor(t
, cpu
, 2);
281 xen_mc_issue(PARAVIRT_LAZY_CPU
);
284 static void xen_write_ldt_entry(struct desc_struct
*dt
, int entrynum
,
287 unsigned long lp
= (unsigned long)&dt
[entrynum
];
288 xmaddr_t mach_lp
= virt_to_machine(lp
);
289 u64 entry
= (u64
)high
<< 32 | low
;
292 if (HYPERVISOR_update_descriptor(mach_lp
.maddr
, entry
))
296 static int cvt_gate_to_trap(int vector
, u32 low
, u32 high
,
297 struct trap_info
*info
)
301 type
= (high
>> 8) & 0x1f;
302 dpl
= (high
>> 13) & 3;
304 if (type
!= 0xf && type
!= 0xe)
307 info
->vector
= vector
;
308 info
->address
= (high
& 0xffff0000) | (low
& 0x0000ffff);
309 info
->cs
= low
>> 16;
311 /* interrupt gates clear IF */
318 /* Locations of each CPU's IDT */
319 static DEFINE_PER_CPU(struct Xgt_desc_struct
, idt_desc
);
321 /* Set an IDT entry. If the entry is part of the current IDT, then
323 static void xen_write_idt_entry(struct desc_struct
*dt
, int entrynum
,
327 int cpu
= smp_processor_id();
328 unsigned long p
= (unsigned long)&dt
[entrynum
];
329 unsigned long start
= per_cpu(idt_desc
, cpu
).address
;
330 unsigned long end
= start
+ per_cpu(idt_desc
, cpu
).size
+ 1;
334 write_dt_entry(dt
, entrynum
, low
, high
);
336 if (p
>= start
&& (p
+ 8) <= end
) {
337 struct trap_info info
[2];
341 if (cvt_gate_to_trap(entrynum
, low
, high
, &info
[0]))
342 if (HYPERVISOR_set_trap_table(info
))
347 /* Load a new IDT into Xen. In principle this can be per-CPU, so we
348 hold a spinlock to protect the static traps[] array (static because
349 it avoids allocation, and saves stack space). */
350 static void xen_load_idt(const struct Xgt_desc_struct
*desc
)
352 static DEFINE_SPINLOCK(lock
);
353 static struct trap_info traps
[257];
355 int cpu
= smp_processor_id();
356 unsigned in
, out
, count
;
358 per_cpu(idt_desc
, cpu
) = *desc
;
360 count
= (desc
->size
+1) / 8;
364 for (in
= out
= 0; in
< count
; in
++) {
365 const u32
*entry
= (u32
*)(desc
->address
+ in
* 8);
367 if (cvt_gate_to_trap(in
, entry
[0], entry
[1], &traps
[out
]))
370 traps
[out
].address
= 0;
373 if (HYPERVISOR_set_trap_table(traps
))
379 /* Write a GDT descriptor entry. Ignore LDT descriptors, since
380 they're handled differently. */
381 static void xen_write_gdt_entry(struct desc_struct
*dt
, int entry
,
384 switch ((high
>> 8) & 0xff) {
391 xmaddr_t maddr
= virt_to_machine(&dt
[entry
]);
392 u64 desc
= (u64
)high
<< 32 | low
;
395 if (HYPERVISOR_update_descriptor(maddr
.maddr
, desc
))
402 static void xen_load_esp0(struct tss_struct
*tss
,
403 struct thread_struct
*thread
)
405 struct multicall_space mcs
= xen_mc_entry(0);
406 MULTI_stack_switch(mcs
.mc
, __KERNEL_DS
, thread
->esp0
);
407 xen_mc_issue(PARAVIRT_LAZY_CPU
);
410 static void xen_set_iopl_mask(unsigned mask
)
412 struct physdev_set_iopl set_iopl
;
414 /* Force the change at ring 0. */
415 set_iopl
.iopl
= (mask
== 0) ? 1 : (mask
>> 12) & 3;
416 HYPERVISOR_physdev_op(PHYSDEVOP_set_iopl
, &set_iopl
);
419 static void xen_io_delay(void)
423 #ifdef CONFIG_X86_LOCAL_APIC
424 static unsigned long xen_apic_read(unsigned long reg
)
430 static void xen_flush_tlb(void)
434 op
.cmd
= MMUEXT_TLB_FLUSH_LOCAL
;
435 if (HYPERVISOR_mmuext_op(&op
, 1, NULL
, DOMID_SELF
))
439 static void xen_flush_tlb_single(unsigned long addr
)
443 op
.cmd
= MMUEXT_INVLPG_LOCAL
;
444 op
.arg1
.linear_addr
= addr
& PAGE_MASK
;
445 if (HYPERVISOR_mmuext_op(&op
, 1, NULL
, DOMID_SELF
))
449 static unsigned long xen_read_cr2(void)
451 return x86_read_percpu(xen_vcpu
)->arch
.cr2
;
454 static void xen_write_cr4(unsigned long cr4
)
456 /* never allow TSC to be disabled */
457 native_write_cr4(cr4
& ~X86_CR4_TSD
);
461 * Page-directory addresses above 4GB do not fit into architectural %cr3.
462 * When accessing %cr3, or equivalent field in vcpu_guest_context, guests
463 * must use the following accessor macros to pack/unpack valid MFNs.
465 * Note that Xen is using the fact that the pagetable base is always
466 * page-aligned, and putting the 12 MSB of the address into the 12 LSB
469 #define xen_pfn_to_cr3(pfn) (((unsigned)(pfn) << 12) | ((unsigned)(pfn) >> 20))
470 #define xen_cr3_to_pfn(cr3) (((unsigned)(cr3) >> 12) | ((unsigned)(cr3) << 20))
472 static unsigned long xen_read_cr3(void)
474 return x86_read_percpu(xen_cr3
);
477 static void xen_write_cr3(unsigned long cr3
)
479 if (cr3
== x86_read_percpu(xen_cr3
)) {
480 /* just a simple tlb flush */
485 x86_write_percpu(xen_cr3
, cr3
);
489 struct mmuext_op
*op
;
490 struct multicall_space mcs
= xen_mc_entry(sizeof(*op
));
491 unsigned long mfn
= pfn_to_mfn(PFN_DOWN(cr3
));
494 op
->cmd
= MMUEXT_NEW_BASEPTR
;
497 MULTI_mmuext_op(mcs
.mc
, op
, 1, NULL
, DOMID_SELF
);
499 xen_mc_issue(PARAVIRT_LAZY_CPU
);
503 static void xen_alloc_pt(struct mm_struct
*mm
, u32 pfn
)
505 /* XXX pfn isn't necessarily a lowmem page */
506 make_lowmem_page_readonly(__va(PFN_PHYS(pfn
)));
509 static void xen_alloc_pd(u32 pfn
)
511 make_lowmem_page_readonly(__va(PFN_PHYS(pfn
)));
514 static void xen_release_pd(u32 pfn
)
516 make_lowmem_page_readwrite(__va(PFN_PHYS(pfn
)));
519 static void xen_release_pt(u32 pfn
)
521 make_lowmem_page_readwrite(__va(PFN_PHYS(pfn
)));
524 static void xen_alloc_pd_clone(u32 pfn
, u32 clonepfn
,
525 u32 start
, u32 count
)
530 static __init
void xen_pagetable_setup_start(pgd_t
*base
)
532 pgd_t
*xen_pgd
= (pgd_t
*)xen_start_info
->pt_base
;
536 * copy top-level of Xen-supplied pagetable into place. For
537 * !PAE we can use this as-is, but for PAE it is a stand-in
538 * while we copy the pmd pages.
540 memcpy(base
, xen_pgd
, PTRS_PER_PGD
* sizeof(pgd_t
));
542 if (PTRS_PER_PMD
> 1) {
545 * For PAE, need to allocate new pmds, rather than
546 * share Xen's, since Xen doesn't like pmd's being
547 * shared between address spaces.
549 for (i
= 0; i
< PTRS_PER_PGD
; i
++) {
550 if (pgd_val_ma(xen_pgd
[i
]) & _PAGE_PRESENT
) {
551 pmd_t
*pmd
= (pmd_t
*)alloc_bootmem_low_pages(PAGE_SIZE
);
553 memcpy(pmd
, (void *)pgd_page_vaddr(xen_pgd
[i
]),
556 xen_alloc_pd(PFN_DOWN(__pa(pmd
)));
558 set_pgd(&base
[i
], __pgd(1 + __pa(pmd
)));
564 /* make sure zero_page is mapped RO so we can use it in pagetables */
565 make_lowmem_page_readonly(empty_zero_page
);
566 make_lowmem_page_readonly(base
);
568 * Switch to new pagetable. This is done before
569 * pagetable_init has done anything so that the new pages
570 * added to the table can be prepared properly for Xen.
572 xen_write_cr3(__pa(base
));
575 static __init
void xen_pagetable_setup_done(pgd_t
*base
)
577 if (!xen_feature(XENFEAT_auto_translated_physmap
)) {
579 * Create a mapping for the shared info page.
580 * Should be set_fixmap(), but shared_info is a machine
581 * address with no corresponding pseudo-phys address.
583 set_pte_mfn(fix_to_virt(FIX_PARAVIRT_BOOTMAP
),
584 PFN_DOWN(xen_start_info
->shared_info
),
587 HYPERVISOR_shared_info
=
588 (struct shared_info
*)fix_to_virt(FIX_PARAVIRT_BOOTMAP
);
591 HYPERVISOR_shared_info
=
592 (struct shared_info
*)__va(xen_start_info
->shared_info
);
596 xen_vcpu_setup(smp_processor_id());
599 static const struct paravirt_ops xen_paravirt_ops __initdata
= {
600 .paravirt_enabled
= 1,
601 .shared_kernel_pmd
= 0,
604 .banner
= xen_banner
,
606 .patch
= paravirt_patch_default
,
608 .memory_setup
= xen_memory_setup
,
609 .arch_setup
= xen_arch_setup
,
613 .set_debugreg
= xen_set_debugreg
,
614 .get_debugreg
= xen_get_debugreg
,
618 .read_cr0
= native_read_cr0
,
619 .write_cr0
= native_write_cr0
,
621 .read_cr2
= xen_read_cr2
,
622 .write_cr2
= native_write_cr2
,
624 .read_cr3
= xen_read_cr3
,
625 .write_cr3
= xen_write_cr3
,
627 .read_cr4
= native_read_cr4
,
628 .read_cr4_safe
= native_read_cr4_safe
,
629 .write_cr4
= xen_write_cr4
,
631 .save_fl
= xen_save_fl
,
632 .restore_fl
= xen_restore_fl
,
633 .irq_disable
= xen_irq_disable
,
634 .irq_enable
= xen_irq_enable
,
635 .safe_halt
= xen_safe_halt
,
637 .wbinvd
= native_wbinvd
,
639 .read_msr
= native_read_msr_safe
,
640 .write_msr
= native_write_msr_safe
,
641 .read_tsc
= native_read_tsc
,
642 .read_pmc
= native_read_pmc
,
644 .iret
= (void *)&hypercall_page
[__HYPERVISOR_iret
],
645 .irq_enable_sysexit
= NULL
, /* never called */
647 .load_tr_desc
= paravirt_nop
,
648 .set_ldt
= xen_set_ldt
,
649 .load_gdt
= xen_load_gdt
,
650 .load_idt
= xen_load_idt
,
651 .load_tls
= xen_load_tls
,
653 .store_gdt
= native_store_gdt
,
654 .store_idt
= native_store_idt
,
655 .store_tr
= xen_store_tr
,
657 .write_ldt_entry
= xen_write_ldt_entry
,
658 .write_gdt_entry
= xen_write_gdt_entry
,
659 .write_idt_entry
= xen_write_idt_entry
,
660 .load_esp0
= xen_load_esp0
,
662 .set_iopl_mask
= xen_set_iopl_mask
,
663 .io_delay
= xen_io_delay
,
665 #ifdef CONFIG_X86_LOCAL_APIC
666 .apic_write
= paravirt_nop
,
667 .apic_write_atomic
= paravirt_nop
,
668 .apic_read
= xen_apic_read
,
669 .setup_boot_clock
= paravirt_nop
,
670 .setup_secondary_clock
= paravirt_nop
,
671 .startup_ipi_hook
= paravirt_nop
,
674 .flush_tlb_user
= xen_flush_tlb
,
675 .flush_tlb_kernel
= xen_flush_tlb
,
676 .flush_tlb_single
= xen_flush_tlb_single
,
678 .pte_update
= paravirt_nop
,
679 .pte_update_defer
= paravirt_nop
,
681 .pagetable_setup_start
= xen_pagetable_setup_start
,
682 .pagetable_setup_done
= xen_pagetable_setup_done
,
684 .alloc_pt
= xen_alloc_pt
,
685 .alloc_pd
= xen_alloc_pd
,
686 .alloc_pd_clone
= xen_alloc_pd_clone
,
687 .release_pd
= xen_release_pd
,
688 .release_pt
= xen_release_pt
,
690 .set_pte
= xen_set_pte
,
691 .set_pte_at
= xen_set_pte_at
,
692 .set_pmd
= xen_set_pmd
,
694 .pte_val
= xen_pte_val
,
695 .pgd_val
= xen_pgd_val
,
697 .make_pte
= xen_make_pte
,
698 .make_pgd
= xen_make_pgd
,
700 #ifdef CONFIG_X86_PAE
701 .set_pte_atomic
= xen_set_pte_atomic
,
702 .set_pte_present
= xen_set_pte_at
,
703 .set_pud
= xen_set_pud
,
704 .pte_clear
= xen_pte_clear
,
705 .pmd_clear
= xen_pmd_clear
,
707 .make_pmd
= xen_make_pmd
,
708 .pmd_val
= xen_pmd_val
,
711 .activate_mm
= xen_activate_mm
,
712 .dup_mmap
= xen_dup_mmap
,
713 .exit_mmap
= xen_exit_mmap
,
715 .set_lazy_mode
= xen_set_lazy_mode
,
718 /* First C function to be called on Xen boot */
719 asmlinkage
void __init
xen_start_kernel(void)
726 BUG_ON(memcmp(xen_start_info
->magic
, "xen-3.0", 7) != 0);
728 /* Install Xen paravirt ops */
729 paravirt_ops
= xen_paravirt_ops
;
731 xen_setup_features();
734 if (!xen_feature(XENFEAT_auto_translated_physmap
))
735 phys_to_machine_mapping
= (unsigned long *)xen_start_info
->mfn_list
;
737 pgd
= (pgd_t
*)xen_start_info
->pt_base
;
739 init_pg_tables_end
= __pa(pgd
) + xen_start_info
->nr_pt_frames
*PAGE_SIZE
;
741 init_mm
.pgd
= pgd
; /* use the Xen pagetables to start */
743 /* keep using Xen gdt for now; no urgent need to change it */
745 x86_write_percpu(xen_cr3
, __pa(pgd
));
748 paravirt_ops
.kernel_rpl
= 1;
749 if (xen_feature(XENFEAT_supervisor_mode_kernel
))
750 paravirt_ops
.kernel_rpl
= 0;
752 /* set the limit of our address space */
753 reserve_top_address(-HYPERVISOR_VIRT_START
+ 2 * PAGE_SIZE
);
755 /* set up basic CPUID stuff */
756 cpu_detect(&new_cpu_data
);
757 new_cpu_data
.hard_math
= 1;
758 new_cpu_data
.x86_capability
[0] = cpuid_edx(1);
760 /* Poke various useful things into boot_params */
761 LOADER_TYPE
= (9 << 4) | 0;
762 INITRD_START
= xen_start_info
->mod_start
? __pa(xen_start_info
->mod_start
) : 0;
763 INITRD_SIZE
= xen_start_info
->mod_len
;
765 /* Start the world */