blob: e7d5382ef26344534b0883a11d02b19a82d4a9cd [file] [log] [blame]
Rusty Russell07ad1572007-07-19 01:49:22 -07001#include <linux/linkage.h>
2#include <linux/lguest.h>
Rusty Russell47436aa2007-10-22 11:03:36 +10003#include <asm/lguest_hcall.h>
Rusty Russell07ad1572007-07-19 01:49:22 -07004#include <asm/asm-offsets.h>
5#include <asm/thread_info.h>
Rusty Russell876be9d2007-07-20 22:12:56 +10006#include <asm/processor-flags.h>
Rusty Russellda32dac2010-12-16 17:03:15 -06007#include <asm/pgtable.h>
Rusty Russell07ad1572007-07-19 01:49:22 -07008
Rusty Russell2e04ef72009-07-30 16:03:45 -06009/*G:020
10 * Our story starts with the kernel booting into startup_32 in
Rusty Russella6bd8e12008-03-28 11:05:53 -050011 * arch/x86/kernel/head_32.S. It expects a boot header, which is created by
12 * the bootloader (the Launcher in our case).
13 *
14 * The startup_32 function does very little: it clears the uninitialized global
15 * C variables which we expect to be zero (ie. BSS) and then copies the boot
16 * header and kernel command line somewhere safe. Finally it checks the
17 * 'hardware_subarch' field. This was introduced in 2.6.24 for lguest and Xen:
18 * if it's set to '1' (lguest's assigned number), then it calls us here.
Rusty Russell47436aa2007-10-22 11:03:36 +100019 *
20 * WARNING: be very careful here! We're running at addresses equal to physical
21 * addesses (around 0), not above PAGE_OFFSET as most code expectes
22 * (eg. 0xC0000000). Jumps are relative, so they're OK, but we can't touch any
Rusty Russella6bd8e12008-03-28 11:05:53 -050023 * data without remembering to subtract __PAGE_OFFSET!
Rusty Russell07ad1572007-07-19 01:49:22 -070024 *
Rusty Russellb2b47c22007-07-26 10:41:02 -070025 * The .section line puts this code in .init.text so it will be discarded after
Rusty Russell2e04ef72009-07-30 16:03:45 -060026 * boot.
27 */
Rusty Russell07ad1572007-07-19 01:49:22 -070028.section .init.text, "ax", @progbits
Rusty Russell814a0e52007-10-22 11:29:44 +100029ENTRY(lguest_entry)
Rusty Russell2e04ef72009-07-30 16:03:45 -060030 /*
31 * We make the "initialization" hypercall now to tell the Host about
32 * us, and also find out where it put our page tables.
33 */
Rusty Russell47436aa2007-10-22 11:03:36 +100034 movl $LHCALL_LGUEST_INIT, %eax
Matias Zabaljauregui4cd8b5e2009-03-14 13:37:52 -020035 movl $lguest_data - __PAGE_OFFSET, %ebx
Rusty Russell091ebf02010-04-14 21:43:54 -060036 int $LGUEST_TRAP_ENTRY
Rusty Russell47436aa2007-10-22 11:03:36 +100037
Rusty Russell47436aa2007-10-22 11:03:36 +100038 /* Set up the initial stack so we can run C code. */
39 movl $(init_thread_union+THREAD_SIZE),%esp
40
Rusty Russellda32dac2010-12-16 17:03:15 -060041 call init_pagetables
42
Rusty Russell2e04ef72009-07-30 16:03:45 -060043 /* Jumps are relative: we're running __PAGE_OFFSET too low. */
Rusty Russell47436aa2007-10-22 11:03:36 +100044 jmp lguest_init+__PAGE_OFFSET
Rusty Russell07ad1572007-07-19 01:49:22 -070045
Rusty Russellda32dac2010-12-16 17:03:15 -060046/*
47 * Initialize page tables. This creates a PDE and a set of page
48 * tables, which are located immediately beyond __brk_base. The variable
49 * _brk_end is set up to point to the first "safe" location.
50 * Mappings are created both at virtual address 0 (identity mapping)
51 * and PAGE_OFFSET for up to _end.
52 *
53 * FIXME: This code is taken verbatim from arch/x86/kernel/head_32.S: they
54 * don't have a stack at this point, so we can't just use call and ret.
55 */
56init_pagetables:
57#if PTRS_PER_PMD > 1
58#define PAGE_TABLE_SIZE(pages) (((pages) / PTRS_PER_PMD) + PTRS_PER_PGD)
59#else
60#define PAGE_TABLE_SIZE(pages) ((pages) / PTRS_PER_PGD)
61#endif
62#define pa(X) ((X) - __PAGE_OFFSET)
63
64/* Enough space to fit pagetables for the low memory linear map */
65MAPPING_BEYOND_END = \
66 PAGE_TABLE_SIZE(((1<<32) - __PAGE_OFFSET) >> PAGE_SHIFT) << PAGE_SHIFT
67#ifdef CONFIG_X86_PAE
68
69 /*
70 * In PAE mode initial_page_table is statically defined to contain
71 * enough entries to cover the VMSPLIT option (that is the top 1, 2 or 3
72 * entries). The identity mapping is handled by pointing two PGD entries
73 * to the first kernel PMD.
74 *
75 * Note the upper half of each PMD or PTE are always zero at this stage.
76 */
77
78#define KPMDS (((-__PAGE_OFFSET) >> 30) & 3) /* Number of kernel PMDs */
79
80 xorl %ebx,%ebx /* %ebx is kept at zero */
81
82 movl $pa(__brk_base), %edi
83 movl $pa(initial_pg_pmd), %edx
84 movl $PTE_IDENT_ATTR, %eax
8510:
86 leal PDE_IDENT_ATTR(%edi),%ecx /* Create PMD entry */
87 movl %ecx,(%edx) /* Store PMD entry */
88 /* Upper half already zero */
89 addl $8,%edx
90 movl $512,%ecx
9111:
92 stosl
93 xchgl %eax,%ebx
94 stosl
95 xchgl %eax,%ebx
96 addl $0x1000,%eax
97 loop 11b
98
99 /*
100 * End condition: we must map up to the end + MAPPING_BEYOND_END.
101 */
102 movl $pa(_end) + MAPPING_BEYOND_END + PTE_IDENT_ATTR, %ebp
103 cmpl %ebp,%eax
104 jb 10b
1051:
106 addl $__PAGE_OFFSET, %edi
107 movl %edi, pa(_brk_end)
108 shrl $12, %eax
109 movl %eax, pa(max_pfn_mapped)
110
111 /* Do early initialization of the fixmap area */
112 movl $pa(initial_pg_fixmap)+PDE_IDENT_ATTR,%eax
113 movl %eax,pa(initial_pg_pmd+0x1000*KPMDS-8)
114#else /* Not PAE */
115
116page_pde_offset = (__PAGE_OFFSET >> 20);
117
118 movl $pa(__brk_base), %edi
119 movl $pa(initial_page_table), %edx
120 movl $PTE_IDENT_ATTR, %eax
12110:
122 leal PDE_IDENT_ATTR(%edi),%ecx /* Create PDE entry */
123 movl %ecx,(%edx) /* Store identity PDE entry */
124 movl %ecx,page_pde_offset(%edx) /* Store kernel PDE entry */
125 addl $4,%edx
126 movl $1024, %ecx
12711:
128 stosl
129 addl $0x1000,%eax
130 loop 11b
131 /*
132 * End condition: we must map up to the end + MAPPING_BEYOND_END.
133 */
134 movl $pa(_end) + MAPPING_BEYOND_END + PTE_IDENT_ATTR, %ebp
135 cmpl %ebp,%eax
136 jb 10b
137 addl $__PAGE_OFFSET, %edi
138 movl %edi, pa(_brk_end)
139 shrl $12, %eax
140 movl %eax, pa(max_pfn_mapped)
141
142 /* Do early initialization of the fixmap area */
143 movl $pa(initial_pg_fixmap)+PDE_IDENT_ATTR,%eax
144 movl %eax,pa(initial_page_table+0xffc)
145#endif
146 ret
147
Rusty Russell2e04ef72009-07-30 16:03:45 -0600148/*G:055
149 * We create a macro which puts the assembler code between lgstart_ and lgend_
150 * markers. These templates are put in the .text section: they can't be
151 * discarded after boot as we may need to patch modules, too.
152 */
Rusty Russellbbbd2bf2007-09-24 21:24:44 -0700153.text
Rusty Russell07ad1572007-07-19 01:49:22 -0700154#define LGUEST_PATCH(name, insns...) \
155 lgstart_##name: insns; lgend_##name:; \
156 .globl lgstart_##name; .globl lgend_##name
157
158LGUEST_PATCH(cli, movl $0, lguest_data+LGUEST_DATA_irq_enabled)
Rusty Russell07ad1572007-07-19 01:49:22 -0700159LGUEST_PATCH(pushf, movl lguest_data+LGUEST_DATA_irq_enabled, %eax)
Rusty Russell61f4bc82009-06-12 22:27:03 -0600160
Rusty Russell2e04ef72009-07-30 16:03:45 -0600161/*G:033
162 * But using those wrappers is inefficient (we'll see why that doesn't matter
163 * for save_fl and irq_disable later). If we write our routines carefully in
164 * assembler, we can avoid clobbering any registers and avoid jumping through
165 * the wrapper functions.
Rusty Russell61f4bc82009-06-12 22:27:03 -0600166 *
167 * I skipped over our first piece of assembler, but this one is worth studying
Rusty Russell2e04ef72009-07-30 16:03:45 -0600168 * in a bit more detail so I'll describe in easy stages. First, the routine to
169 * enable interrupts:
170 */
Rusty Russell61f4bc82009-06-12 22:27:03 -0600171ENTRY(lg_irq_enable)
Rusty Russell2e04ef72009-07-30 16:03:45 -0600172 /*
173 * The reverse of irq_disable, this sets lguest_data.irq_enabled to
174 * X86_EFLAGS_IF (ie. "Interrupts enabled").
175 */
Rusty Russell61f4bc82009-06-12 22:27:03 -0600176 movl $X86_EFLAGS_IF, lguest_data+LGUEST_DATA_irq_enabled
Rusty Russell2e04ef72009-07-30 16:03:45 -0600177 /*
178 * But now we need to check if the Host wants to know: there might have
Rusty Russell61f4bc82009-06-12 22:27:03 -0600179 * been interrupts waiting to be delivered, in which case it will have
180 * set lguest_data.irq_pending to X86_EFLAGS_IF. If it's not zero, we
Rusty Russell2e04ef72009-07-30 16:03:45 -0600181 * jump to send_interrupts, otherwise we're done.
182 */
Rusty Russell61f4bc82009-06-12 22:27:03 -0600183 testl $0, lguest_data+LGUEST_DATA_irq_pending
184 jnz send_interrupts
Rusty Russell2e04ef72009-07-30 16:03:45 -0600185 /*
186 * One cool thing about x86 is that you can do many things without using
Rusty Russell61f4bc82009-06-12 22:27:03 -0600187 * a register. In this case, the normal path hasn't needed to save or
Rusty Russell2e04ef72009-07-30 16:03:45 -0600188 * restore any registers at all!
189 */
Rusty Russell61f4bc82009-06-12 22:27:03 -0600190 ret
191send_interrupts:
Rusty Russell2e04ef72009-07-30 16:03:45 -0600192 /*
193 * OK, now we need a register: eax is used for the hypercall number,
Rusty Russell61f4bc82009-06-12 22:27:03 -0600194 * which is LHCALL_SEND_INTERRUPTS.
195 *
196 * We used not to bother with this pending detection at all, which was
197 * much simpler. Sooner or later the Host would realize it had to
198 * send us an interrupt. But that turns out to make performance 7
199 * times worse on a simple tcp benchmark. So now we do this the hard
Rusty Russell2e04ef72009-07-30 16:03:45 -0600200 * way.
201 */
Rusty Russell61f4bc82009-06-12 22:27:03 -0600202 pushl %eax
203 movl $LHCALL_SEND_INTERRUPTS, %eax
Rusty Russell2e04ef72009-07-30 16:03:45 -0600204 /*
205 * This is a vmcall instruction (same thing that KVM uses). Older
Rusty Russell61f4bc82009-06-12 22:27:03 -0600206 * assembler versions might not know the "vmcall" instruction, so we
Rusty Russell2e04ef72009-07-30 16:03:45 -0600207 * create one manually here.
208 */
Rusty Russell61f4bc82009-06-12 22:27:03 -0600209 .byte 0x0f,0x01,0xc1 /* KVM_HYPERCALL */
Rusty Russella91d74a2009-07-30 16:03:45 -0600210 /* Put eax back the way we found it. */
Rusty Russell61f4bc82009-06-12 22:27:03 -0600211 popl %eax
212 ret
213
Rusty Russell2e04ef72009-07-30 16:03:45 -0600214/*
215 * Finally, the "popf" or "restore flags" routine. The %eax register holds the
Rusty Russell61f4bc82009-06-12 22:27:03 -0600216 * flags (in practice, either X86_EFLAGS_IF or 0): if it's X86_EFLAGS_IF we're
Rusty Russell2e04ef72009-07-30 16:03:45 -0600217 * enabling interrupts again, if it's 0 we're leaving them off.
218 */
Rusty Russell61f4bc82009-06-12 22:27:03 -0600219ENTRY(lg_restore_fl)
220 /* This is just "lguest_data.irq_enabled = flags;" */
221 movl %eax, lguest_data+LGUEST_DATA_irq_enabled
Rusty Russell2e04ef72009-07-30 16:03:45 -0600222 /*
223 * Now, if the %eax value has enabled interrupts and
Rusty Russell61f4bc82009-06-12 22:27:03 -0600224 * lguest_data.irq_pending is set, we want to tell the Host so it can
225 * deliver any outstanding interrupts. Fortunately, both values will
226 * be X86_EFLAGS_IF (ie. 512) in that case, and the "testl"
227 * instruction will AND them together for us. If both are set, we
Rusty Russell2e04ef72009-07-30 16:03:45 -0600228 * jump to send_interrupts.
229 */
Rusty Russell61f4bc82009-06-12 22:27:03 -0600230 testl lguest_data+LGUEST_DATA_irq_pending, %eax
231 jnz send_interrupts
232 /* Again, the normal path has used no extra registers. Clever, huh? */
233 ret
Rusty Russella91d74a2009-07-30 16:03:45 -0600234/*:*/
Rusty Russell07ad1572007-07-19 01:49:22 -0700235
Rusty Russell07ad1572007-07-19 01:49:22 -0700236/* These demark the EIP range where host should never deliver interrupts. */
237.global lguest_noirq_start
238.global lguest_noirq_end
239
Rusty Russell2e04ef72009-07-30 16:03:45 -0600240/*M:004
241 * When the Host reflects a trap or injects an interrupt into the Guest, it
242 * sets the eflags interrupt bit on the stack based on lguest_data.irq_enabled,
243 * so the Guest iret logic does the right thing when restoring it. However,
244 * when the Host sets the Guest up for direct traps, such as system calls, the
245 * processor is the one to push eflags onto the stack, and the interrupt bit
246 * will be 1 (in reality, interrupts are always enabled in the Guest).
Rusty Russellf56a3842007-07-26 10:41:05 -0700247 *
248 * This turns out to be harmless: the only trap which should happen under Linux
249 * with interrupts disabled is Page Fault (due to our lazy mapping of vmalloc
250 * regions), which has to be reflected through the Host anyway. If another
251 * trap *does* go off when interrupts are disabled, the Guest will panic, and
Rusty Russell2e04ef72009-07-30 16:03:45 -0600252 * we'll never get to this iret!
253:*/
Rusty Russellf56a3842007-07-26 10:41:05 -0700254
Rusty Russell2e04ef72009-07-30 16:03:45 -0600255/*G:045
256 * There is one final paravirt_op that the Guest implements, and glancing at it
257 * you can see why I left it to last. It's *cool*! It's in *assembler*!
Rusty Russellb2b47c22007-07-26 10:41:02 -0700258 *
259 * The "iret" instruction is used to return from an interrupt or trap. The
260 * stack looks like this:
261 * old address
262 * old code segment & privilege level
263 * old processor flags ("eflags")
264 *
265 * The "iret" instruction pops those values off the stack and restores them all
266 * at once. The only problem is that eflags includes the Interrupt Flag which
267 * the Guest can't change: the CPU will simply ignore it when we do an "iret".
268 * So we have to copy eflags from the stack to lguest_data.irq_enabled before
269 * we do the "iret".
270 *
271 * There are two problems with this: firstly, we need to use a register to do
272 * the copy and secondly, the whole thing needs to be atomic. The first
273 * problem is easy to solve: push %eax on the stack so we can use it, and then
274 * restore it at the end just before the real "iret".
275 *
276 * The second is harder: copying eflags to lguest_data.irq_enabled will turn
277 * interrupts on before we're finished, so we could be interrupted before we
278 * return to userspace or wherever. Our solution to this is to surround the
279 * code with lguest_noirq_start: and lguest_noirq_end: labels. We tell the
280 * Host that it is *never* to interrupt us there, even if interrupts seem to be
Rusty Russell2e04ef72009-07-30 16:03:45 -0600281 * enabled.
282 */
Rusty Russell07ad1572007-07-19 01:49:22 -0700283ENTRY(lguest_iret)
284 pushl %eax
285 movl 12(%esp), %eax
286lguest_noirq_start:
Rusty Russell2e04ef72009-07-30 16:03:45 -0600287 /*
288 * Note the %ss: segment prefix here. Normal data accesses use the
Rusty Russellb2b47c22007-07-26 10:41:02 -0700289 * "ds" segment, but that will have already been restored for whatever
290 * we're returning to (such as userspace): we can't trust it. The %ss:
Rusty Russell2e04ef72009-07-30 16:03:45 -0600291 * prefix makes sure we use the stack segment, which is still valid.
292 */
Rusty Russell07ad1572007-07-19 01:49:22 -0700293 movl %eax,%ss:lguest_data+LGUEST_DATA_irq_enabled
294 popl %eax
295 iret
296lguest_noirq_end: