Skip to content

Commit 5ead97c

Browse files
Jeremy Fitzhardingejsgf
authored andcommitted
xen: Core Xen implementation
This patch is a rollup of all the core pieces of the Xen implementation, including: - booting and setup - pagetable setup - privileged instructions - segmentation - interrupt flags - upcalls - multicall batching BOOTING AND SETUP The vmlinux image is decorated with ELF notes which tell the Xen domain builder what the kernel's requirements are; the domain builder then constructs the address space accordingly and starts the kernel. Xen has its own entrypoint for the kernel (contained in an ELF note). The ELF notes are set up by xen-head.S, which is included into head.S. In principle it could be linked separately, but it seems to provoke lots of binutils bugs. Because the domain builder starts the kernel in a fairly sane state (32-bit protected mode, paging enabled, flat segments set up), there's not a lot of setup needed before starting the kernel proper. The main steps are: 1. Install the Xen paravirt_ops, which is simply a matter of a structure assignment. 2. Set init_mm to use the Xen-supplied pagetables (analogous to the head.S generated pagetables in a native boot). 3. Reserve address space for Xen, since it takes a chunk at the top of the address space for its own use. 4. Call start_kernel() PAGETABLE SETUP Once we hit the main kernel boot sequence, it will end up calling back via paravirt_ops to set up various pieces of Xen specific state. One of the critical things which requires a bit of extra care is the construction of the initial init_mm pagetable. Because Xen places tight constraints on pagetables (an active pagetable must always be valid, and must always be mapped read-only to the guest domain), we need to be careful when constructing the new pagetable to keep these constraints in mind. It turns out that the easiest way to do this is use the initial Xen-provided pagetable as a template, and then just insert new mappings for memory where a mapping doesn't already exist. This means that during pagetable setup, it uses a special version of xen_set_pte which ignores any attempt to remap a read-only page as read-write (since Xen will map its own initial pagetable as RO), but lets other changes to the ptes happen, so that things like NX are set properly. PRIVILEGED INSTRUCTIONS AND SEGMENTATION When the kernel runs under Xen, it runs in ring 1 rather than ring 0. This means that it is more privileged than user-mode in ring 3, but it still can't run privileged instructions directly. Non-performance critical instructions are dealt with by taking a privilege exception and trapping into the hypervisor and emulating the instruction, but more performance-critical instructions have their own specific paravirt_ops. In many cases we can avoid having to do any hypercalls for these instructions, or the Xen implementation is quite different from the normal native version. The privileged instructions fall into the broad classes of: Segmentation: setting up the GDT and the GDT entries, LDT, TLS and so on. Xen doesn't allow the GDT to be directly modified; all GDT updates are done via hypercalls where the new entries can be validated. This is important because Xen uses segment limits to prevent the guest kernel from damaging the hypervisor itself. Traps and exceptions: Xen uses a special format for trap entrypoints, so when the kernel wants to set an IDT entry, it needs to be converted to the form Xen expects. Xen sets int 0x80 up specially so that the trap goes straight from userspace into the guest kernel without going via the hypervisor. sysenter isn't supported. Kernel stack: The esp0 entry is extracted from the tss and provided to Xen. TLB operations: the various TLB calls are mapped into corresponding Xen hypercalls. Control registers: all the control registers are privileged. The most important is cr3, which points to the base of the current pagetable, and we handle it specially. Another instruction we treat specially is CPUID, even though its not privileged. We want to control what CPU features are visible to the rest of the kernel, and so CPUID ends up going into a paravirt_op. Xen implements this mainly to disable the ACPI and APIC subsystems. INTERRUPT FLAGS Xen maintains its own separate flag for masking events, which is contained within the per-cpu vcpu_info structure. Because the guest kernel runs in ring 1 and not 0, the IF flag in EFLAGS is completely ignored (and must be, because even if a guest domain disables interrupts for itself, it can't disable them overall). (A note on terminology: "events" and interrupts are effectively synonymous. However, rather than using an "enable flag", Xen uses a "mask flag", which blocks event delivery when it is non-zero.) There are paravirt_ops for each of cli/sti/save_fl/restore_fl, which are implemented to manage the Xen event mask state. The only thing worth noting is that when events are unmasked, we need to explicitly see if there's a pending event and call into the hypervisor to make sure it gets delivered. UPCALLS Xen needs a couple of upcall (or callback) functions to be implemented by each guest. One is the event upcalls, which is how events (interrupts, effectively) are delivered to the guests. The other is the failsafe callback, which is used to report errors in either reloading a segment register, or caused by iret. These are implemented in i386/kernel/entry.S so they can jump into the normal iret_exc path when necessary. MULTICALL BATCHING Xen provides a multicall mechanism, which allows multiple hypercalls to be issued at once in order to mitigate the cost of trapping into the hypervisor. This is particularly useful for context switches, since the 4-5 hypercalls they would normally need (reload cr3, update TLS, maybe update LDT) can be reduced to one. This patch implements a generic batching mechanism for hypercalls, which gets used in many places in the Xen code. Signed-off-by: Jeremy Fitzhardinge <[email protected]> Signed-off-by: Chris Wright <[email protected]> Cc: Ian Pratt <[email protected]> Cc: Christian Limpach <[email protected]> Cc: Adrian Bunk <[email protected]>
1 parent a42089d commit 5ead97c

File tree

16 files changed

+1373
-1
lines changed

16 files changed

+1373
-1
lines changed

arch/i386/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ mflags-$(CONFIG_X86_ES7000) := -Iinclude/asm-i386/mach-es7000
9393
mcore-$(CONFIG_X86_ES7000) := mach-default
9494
core-$(CONFIG_X86_ES7000) := arch/i386/mach-es7000/
9595

96+
# Xen paravirtualization support
97+
core-$(CONFIG_XEN) += arch/i386/xen/
98+
9699
# default subarch .h files
97100
mflags-y += -Iinclude/asm-i386/mach-default
98101

arch/i386/kernel/entry.S

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,77 @@ ENTRY(kernel_thread_helper)
10231023
CFI_ENDPROC
10241024
ENDPROC(kernel_thread_helper)
10251025

1026+
#ifdef CONFIG_XEN
1027+
ENTRY(xen_hypervisor_callback)
1028+
CFI_STARTPROC
1029+
pushl $0
1030+
CFI_ADJUST_CFA_OFFSET 4
1031+
SAVE_ALL
1032+
TRACE_IRQS_OFF
1033+
mov %esp, %eax
1034+
call xen_evtchn_do_upcall
1035+
jmp ret_from_intr
1036+
CFI_ENDPROC
1037+
ENDPROC(xen_hypervisor_callback)
1038+
1039+
# Hypervisor uses this for application faults while it executes.
1040+
# We get here for two reasons:
1041+
# 1. Fault while reloading DS, ES, FS or GS
1042+
# 2. Fault while executing IRET
1043+
# Category 1 we fix up by reattempting the load, and zeroing the segment
1044+
# register if the load fails.
1045+
# Category 2 we fix up by jumping to do_iret_error. We cannot use the
1046+
# normal Linux return path in this case because if we use the IRET hypercall
1047+
# to pop the stack frame we end up in an infinite loop of failsafe callbacks.
1048+
# We distinguish between categories by maintaining a status value in EAX.
1049+
ENTRY(xen_failsafe_callback)
1050+
CFI_STARTPROC
1051+
pushl %eax
1052+
CFI_ADJUST_CFA_OFFSET 4
1053+
movl $1,%eax
1054+
1: mov 4(%esp),%ds
1055+
2: mov 8(%esp),%es
1056+
3: mov 12(%esp),%fs
1057+
4: mov 16(%esp),%gs
1058+
testl %eax,%eax
1059+
popl %eax
1060+
CFI_ADJUST_CFA_OFFSET -4
1061+
lea 16(%esp),%esp
1062+
CFI_ADJUST_CFA_OFFSET -16
1063+
jz 5f
1064+
addl $16,%esp
1065+
jmp iret_exc # EAX != 0 => Category 2 (Bad IRET)
1066+
5: pushl $0 # EAX == 0 => Category 1 (Bad segment)
1067+
CFI_ADJUST_CFA_OFFSET 4
1068+
SAVE_ALL
1069+
jmp ret_from_exception
1070+
CFI_ENDPROC
1071+
1072+
.section .fixup,"ax"
1073+
6: xorl %eax,%eax
1074+
movl %eax,4(%esp)
1075+
jmp 1b
1076+
7: xorl %eax,%eax
1077+
movl %eax,8(%esp)
1078+
jmp 2b
1079+
8: xorl %eax,%eax
1080+
movl %eax,12(%esp)
1081+
jmp 3b
1082+
9: xorl %eax,%eax
1083+
movl %eax,16(%esp)
1084+
jmp 4b
1085+
.previous
1086+
.section __ex_table,"a"
1087+
.align 4
1088+
.long 1b,6b
1089+
.long 2b,7b
1090+
.long 3b,8b
1091+
.long 4b,9b
1092+
.previous
1093+
ENDPROC(xen_failsafe_callback)
1094+
1095+
#endif /* CONFIG_XEN */
1096+
10261097
.section .rodata,"a"
10271098
#include "syscall_table.S"
10281099

arch/i386/kernel/head.S

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,8 @@ ENTRY(_stext)
510510
/*
511511
* BSS section
512512
*/
513-
.section ".bss.page_aligned","w"
513+
.section ".bss.page_aligned","wa"
514+
.align PAGE_SIZE_asm
514515
ENTRY(swapper_pg_dir)
515516
.fill 1024,4,0
516517
ENTRY(swapper_pg_pmd)
@@ -538,6 +539,8 @@ fault_msg:
538539
.ascii "Int %d: CR2 %p err %p EIP %p CS %p flags %p\n"
539540
.asciz "Stack: %p %p %p %p %p %p %p %p\n"
540541

542+
#include "../xen/xen-head.S"
543+
541544
/*
542545
* The IDT and GDT 'descriptors' are a strange 48-bit object
543546
* only used by the lidt and lgdt instructions. They are not

arch/i386/kernel/vmlinux.lds.S

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ SECTIONS
8888

8989
. = ALIGN(4096);
9090
.data.page_aligned : AT(ADDR(.data.page_aligned) - LOAD_OFFSET) {
91+
*(.data.page_aligned)
9192
*(.data.idt)
9293
}
9394

arch/i386/xen/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
obj-y := enlighten.o setup.o features.o multicalls.o

0 commit comments

Comments
 (0)