Rusty Russell | 07ad157 | 2007-07-19 01:49:22 -0700 | [diff] [blame] | 1 | #include <linux/linkage.h> |
| 2 | #include <linux/lguest.h> |
Rusty Russell | 47436aa | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 3 | #include <asm/lguest_hcall.h> |
Rusty Russell | 07ad157 | 2007-07-19 01:49:22 -0700 | [diff] [blame] | 4 | #include <asm/asm-offsets.h> |
| 5 | #include <asm/thread_info.h> |
Rusty Russell | 876be9d | 2007-07-20 22:12:56 +1000 | [diff] [blame] | 6 | #include <asm/processor-flags.h> |
Rusty Russell | da32dac | 2010-12-16 17:03:15 -0600 | [diff] [blame] | 7 | #include <asm/pgtable.h> |
Rusty Russell | 07ad157 | 2007-07-19 01:49:22 -0700 | [diff] [blame] | 8 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 9 | /*G:020 |
| 10 | * Our story starts with the kernel booting into startup_32 in |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 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. |
Rusty Russell | 47436aa | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 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 |
Rusty Russell | a6bd8e1 | 2008-03-28 11:05:53 -0500 | [diff] [blame] | 23 | * data without remembering to subtract __PAGE_OFFSET! |
Rusty Russell | 07ad157 | 2007-07-19 01:49:22 -0700 | [diff] [blame] | 24 | * |
Rusty Russell | b2b47c2 | 2007-07-26 10:41:02 -0700 | [diff] [blame] | 25 | * The .section line puts this code in .init.text so it will be discarded after |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 26 | * boot. |
| 27 | */ |
Rusty Russell | 07ad157 | 2007-07-19 01:49:22 -0700 | [diff] [blame] | 28 | .section .init.text, "ax", @progbits |
Rusty Russell | 814a0e5 | 2007-10-22 11:29:44 +1000 | [diff] [blame] | 29 | ENTRY(lguest_entry) |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 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 | */ |
Rusty Russell | 47436aa | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 34 | movl $LHCALL_LGUEST_INIT, %eax |
Matias Zabaljauregui | 4cd8b5e | 2009-03-14 13:37:52 -0200 | [diff] [blame] | 35 | movl $lguest_data - __PAGE_OFFSET, %ebx |
Rusty Russell | 091ebf0 | 2010-04-14 21:43:54 -0600 | [diff] [blame] | 36 | int $LGUEST_TRAP_ENTRY |
Rusty Russell | 47436aa | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 37 | |
Rusty Russell | 47436aa | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 38 | /* Set up the initial stack so we can run C code. */ |
| 39 | movl $(init_thread_union+THREAD_SIZE),%esp |
| 40 | |
Rusty Russell | da32dac | 2010-12-16 17:03:15 -0600 | [diff] [blame] | 41 | call init_pagetables |
| 42 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 43 | /* Jumps are relative: we're running __PAGE_OFFSET too low. */ |
Rusty Russell | 47436aa | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 44 | jmp lguest_init+__PAGE_OFFSET |
Rusty Russell | 07ad157 | 2007-07-19 01:49:22 -0700 | [diff] [blame] | 45 | |
Rusty Russell | da32dac | 2010-12-16 17:03:15 -0600 | [diff] [blame] | 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 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 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 | */ |
Rusty Russell | bbbd2bf | 2007-09-24 21:24:44 -0700 | [diff] [blame] | 153 | .text |
Rusty Russell | 07ad157 | 2007-07-19 01:49:22 -0700 | [diff] [blame] | 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) |
Rusty Russell | 07ad157 | 2007-07-19 01:49:22 -0700 | [diff] [blame] | 159 | LGUEST_PATCH(pushf, movl lguest_data+LGUEST_DATA_irq_enabled, %eax) |
Rusty Russell | 61f4bc8 | 2009-06-12 22:27:03 -0600 | [diff] [blame] | 160 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 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. |
Rusty Russell | 61f4bc8 | 2009-06-12 22:27:03 -0600 | [diff] [blame] | 166 | * |
| 167 | * I skipped over our first piece of assembler, but this one is worth studying |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 168 | * in a bit more detail so I'll describe in easy stages. First, the routine to |
| 169 | * enable interrupts: |
| 170 | */ |
Rusty Russell | 61f4bc8 | 2009-06-12 22:27:03 -0600 | [diff] [blame] | 171 | ENTRY(lg_irq_enable) |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 172 | /* |
| 173 | * The reverse of irq_disable, this sets lguest_data.irq_enabled to |
| 174 | * X86_EFLAGS_IF (ie. "Interrupts enabled"). |
| 175 | */ |
Rusty Russell | 61f4bc8 | 2009-06-12 22:27:03 -0600 | [diff] [blame] | 176 | movl $X86_EFLAGS_IF, lguest_data+LGUEST_DATA_irq_enabled |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 177 | /* |
| 178 | * But now we need to check if the Host wants to know: there might have |
Rusty Russell | 61f4bc8 | 2009-06-12 22:27:03 -0600 | [diff] [blame] | 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 |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 181 | * jump to send_interrupts, otherwise we're done. |
| 182 | */ |
Rusty Russell | 61f4bc8 | 2009-06-12 22:27:03 -0600 | [diff] [blame] | 183 | testl $0, lguest_data+LGUEST_DATA_irq_pending |
| 184 | jnz send_interrupts |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 185 | /* |
| 186 | * One cool thing about x86 is that you can do many things without using |
Rusty Russell | 61f4bc8 | 2009-06-12 22:27:03 -0600 | [diff] [blame] | 187 | * a register. In this case, the normal path hasn't needed to save or |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 188 | * restore any registers at all! |
| 189 | */ |
Rusty Russell | 61f4bc8 | 2009-06-12 22:27:03 -0600 | [diff] [blame] | 190 | ret |
| 191 | send_interrupts: |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 192 | /* |
| 193 | * OK, now we need a register: eax is used for the hypercall number, |
Rusty Russell | 61f4bc8 | 2009-06-12 22:27:03 -0600 | [diff] [blame] | 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 |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 200 | * way. |
| 201 | */ |
Rusty Russell | 61f4bc8 | 2009-06-12 22:27:03 -0600 | [diff] [blame] | 202 | pushl %eax |
| 203 | movl $LHCALL_SEND_INTERRUPTS, %eax |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 204 | /* |
| 205 | * This is a vmcall instruction (same thing that KVM uses). Older |
Rusty Russell | 61f4bc8 | 2009-06-12 22:27:03 -0600 | [diff] [blame] | 206 | * assembler versions might not know the "vmcall" instruction, so we |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 207 | * create one manually here. |
| 208 | */ |
Rusty Russell | 61f4bc8 | 2009-06-12 22:27:03 -0600 | [diff] [blame] | 209 | .byte 0x0f,0x01,0xc1 /* KVM_HYPERCALL */ |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 210 | /* Put eax back the way we found it. */ |
Rusty Russell | 61f4bc8 | 2009-06-12 22:27:03 -0600 | [diff] [blame] | 211 | popl %eax |
| 212 | ret |
| 213 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 214 | /* |
| 215 | * Finally, the "popf" or "restore flags" routine. The %eax register holds the |
Rusty Russell | 61f4bc8 | 2009-06-12 22:27:03 -0600 | [diff] [blame] | 216 | * flags (in practice, either X86_EFLAGS_IF or 0): if it's X86_EFLAGS_IF we're |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 217 | * enabling interrupts again, if it's 0 we're leaving them off. |
| 218 | */ |
Rusty Russell | 61f4bc8 | 2009-06-12 22:27:03 -0600 | [diff] [blame] | 219 | ENTRY(lg_restore_fl) |
| 220 | /* This is just "lguest_data.irq_enabled = flags;" */ |
| 221 | movl %eax, lguest_data+LGUEST_DATA_irq_enabled |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 222 | /* |
| 223 | * Now, if the %eax value has enabled interrupts and |
Rusty Russell | 61f4bc8 | 2009-06-12 22:27:03 -0600 | [diff] [blame] | 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 |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 228 | * jump to send_interrupts. |
| 229 | */ |
Rusty Russell | 61f4bc8 | 2009-06-12 22:27:03 -0600 | [diff] [blame] | 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 |
Rusty Russell | a91d74a | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 234 | /*:*/ |
Rusty Russell | 07ad157 | 2007-07-19 01:49:22 -0700 | [diff] [blame] | 235 | |
Rusty Russell | 07ad157 | 2007-07-19 01:49:22 -0700 | [diff] [blame] | 236 | /* These demark the EIP range where host should never deliver interrupts. */ |
| 237 | .global lguest_noirq_start |
| 238 | .global lguest_noirq_end |
| 239 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 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). |
Rusty Russell | f56a384 | 2007-07-26 10:41:05 -0700 | [diff] [blame] | 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 |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 252 | * we'll never get to this iret! |
| 253 | :*/ |
Rusty Russell | f56a384 | 2007-07-26 10:41:05 -0700 | [diff] [blame] | 254 | |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 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*! |
Rusty Russell | b2b47c2 | 2007-07-26 10:41:02 -0700 | [diff] [blame] | 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 |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 281 | * enabled. |
| 282 | */ |
Rusty Russell | 07ad157 | 2007-07-19 01:49:22 -0700 | [diff] [blame] | 283 | ENTRY(lguest_iret) |
| 284 | pushl %eax |
| 285 | movl 12(%esp), %eax |
| 286 | lguest_noirq_start: |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 287 | /* |
| 288 | * Note the %ss: segment prefix here. Normal data accesses use the |
Rusty Russell | b2b47c2 | 2007-07-26 10:41:02 -0700 | [diff] [blame] | 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: |
Rusty Russell | 2e04ef7 | 2009-07-30 16:03:45 -0600 | [diff] [blame] | 291 | * prefix makes sure we use the stack segment, which is still valid. |
| 292 | */ |
Rusty Russell | 07ad157 | 2007-07-19 01:49:22 -0700 | [diff] [blame] | 293 | movl %eax,%ss:lguest_data+LGUEST_DATA_irq_enabled |
| 294 | popl %eax |
| 295 | iret |
| 296 | lguest_noirq_end: |