lguest: populate initial_page_table
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / x86 / lguest / i386_head.S
1 #include <linux/linkage.h>
2 #include <linux/lguest.h>
3 #include <asm/lguest_hcall.h>
4 #include <asm/asm-offsets.h>
5 #include <asm/thread_info.h>
6 #include <asm/processor-flags.h>
7 #include <asm/pgtable.h>
8
9 /*G:020
10 * Our story starts with the kernel booting into startup_32 in
11 * 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.
19 *
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
23 * data without remembering to subtract __PAGE_OFFSET!
24 *
25 * The .section line puts this code in .init.text so it will be discarded after
26 * boot.
27 */
28 .section .init.text, "ax", @progbits
29 ENTRY(lguest_entry)
30 /*
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 */
34 movl $LHCALL_LGUEST_INIT, %eax
35 movl $lguest_data - __PAGE_OFFSET, %ebx
36 int $LGUEST_TRAP_ENTRY
37
38 /* Set up the initial stack so we can run C code. */
39 movl $(init_thread_union+THREAD_SIZE),%esp
40
41 call init_pagetables
42
43 /* Jumps are relative: we're running __PAGE_OFFSET too low. */
44 jmp lguest_init+__PAGE_OFFSET
45
46 /*
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 */
56 init_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 */
65 MAPPING_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
85 10:
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
91 11:
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
105 1:
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
116 page_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
121 10:
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
127 11:
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
148 /*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 */
153 .text
154 #define LGUEST_PATCH(name, insns...) \
155 lgstart_##name: insns; lgend_##name:; \
156 .globl lgstart_##name; .globl lgend_##name
157
158 LGUEST_PATCH(cli, movl $0, lguest_data+LGUEST_DATA_irq_enabled)
159 LGUEST_PATCH(pushf, movl lguest_data+LGUEST_DATA_irq_enabled, %eax)
160
161 /*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.
166 *
167 * I skipped over our first piece of assembler, but this one is worth studying
168 * in a bit more detail so I'll describe in easy stages. First, the routine to
169 * enable interrupts:
170 */
171 ENTRY(lg_irq_enable)
172 /*
173 * The reverse of irq_disable, this sets lguest_data.irq_enabled to
174 * X86_EFLAGS_IF (ie. "Interrupts enabled").
175 */
176 movl $X86_EFLAGS_IF, lguest_data+LGUEST_DATA_irq_enabled
177 /*
178 * But now we need to check if the Host wants to know: there might have
179 * 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
181 * jump to send_interrupts, otherwise we're done.
182 */
183 testl $0, lguest_data+LGUEST_DATA_irq_pending
184 jnz send_interrupts
185 /*
186 * One cool thing about x86 is that you can do many things without using
187 * a register. In this case, the normal path hasn't needed to save or
188 * restore any registers at all!
189 */
190 ret
191 send_interrupts:
192 /*
193 * OK, now we need a register: eax is used for the hypercall number,
194 * 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
200 * way.
201 */
202 pushl %eax
203 movl $LHCALL_SEND_INTERRUPTS, %eax
204 /*
205 * This is a vmcall instruction (same thing that KVM uses). Older
206 * assembler versions might not know the "vmcall" instruction, so we
207 * create one manually here.
208 */
209 .byte 0x0f,0x01,0xc1 /* KVM_HYPERCALL */
210 /* Put eax back the way we found it. */
211 popl %eax
212 ret
213
214 /*
215 * Finally, the "popf" or "restore flags" routine. The %eax register holds the
216 * flags (in practice, either X86_EFLAGS_IF or 0): if it's X86_EFLAGS_IF we're
217 * enabling interrupts again, if it's 0 we're leaving them off.
218 */
219 ENTRY(lg_restore_fl)
220 /* This is just "lguest_data.irq_enabled = flags;" */
221 movl %eax, lguest_data+LGUEST_DATA_irq_enabled
222 /*
223 * Now, if the %eax value has enabled interrupts and
224 * 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
228 * jump to send_interrupts.
229 */
230 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
234 /*:*/
235
236 /* These demark the EIP range where host should never deliver interrupts. */
237 .global lguest_noirq_start
238 .global lguest_noirq_end
239
240 /*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).
247 *
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
252 * we'll never get to this iret!
253 :*/
254
255 /*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*!
258 *
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
281 * enabled.
282 */
283 ENTRY(lguest_iret)
284 pushl %eax
285 movl 12(%esp), %eax
286 lguest_noirq_start:
287 /*
288 * Note the %ss: segment prefix here. Normal data accesses use the
289 * "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:
291 * prefix makes sure we use the stack segment, which is still valid.
292 */
293 movl %eax,%ss:lguest_data+LGUEST_DATA_irq_enabled
294 popl %eax
295 iret
296 lguest_noirq_end: