Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 1 | /* |
| 2 | * linux_trampoline |
| 3 | * |
| 4 | * Copyright (C) 2013 Patrick Georgi <patrick@georgi-clan.de> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | /* NOTE: THIS CODE MUST REMAIN POSITION INDEPENDENT |
| 17 | * IT SHOULDN'T USE THE STACK |
| 18 | * AND IN GENERAL EXPECT NOTHING BUT RAM TO WORK |
| 19 | */ |
| 20 | .code32 |
| 21 | .data |
| 22 | |
| 23 | #include "linux_trampoline.h" |
| 24 | #define HEADER_SIG 0x4f49424c // LBIO little endian |
| 25 | #define CB_TAG_FORWARD 0x11 |
| 26 | #define CB_TAG_MEMORY 0x1 |
| 27 | #define CB_TAG_FRAMEBUFFER 0x12 |
| 28 | |
| 29 | #define E820_NR_OFFSET 0x1e8 |
| 30 | #define LINUX_ENTRY_OFFSET 0x214 |
| 31 | #define E820_OFFSET 0x2d0 |
| 32 | |
| 33 | .trampoline_start: |
| 34 | |
| 35 | cld |
| 36 | xor %edx, %edx |
| 37 | mov $0, %ecx |
| 38 | |
| 39 | .headerSearch: |
| 40 | mov $0x10000, %ebx |
| 41 | add %ecx, %ebx |
| 42 | mov (%ecx), %eax |
| 43 | cmp $HEADER_SIG, %eax |
| 44 | je .headerSearchDone // found the header |
| 45 | add $16, %ecx |
| 46 | cmp %ecx, %ebx |
| 47 | jne .headerSearch |
| 48 | |
| 49 | .headerSearchDone: |
| 50 | cmp %ecx, %ebx // reached the end == not found anything? |
| 51 | je 2f // give up |
| 52 | |
| 53 | // we assume the checksum is okay, no test |
| 54 | mov 4(%ecx), %ebx |
| 55 | add %ecx, %ebx // ebx = cb_header + header_bytes |
| 56 | mov 20(%ecx), %ecx // ecx = table_entries |
| 57 | |
| 58 | .tableScan: |
| 59 | cmp $CB_TAG_FORWARD, (%ebx) |
| 60 | jne .testMemory |
| 61 | |
| 62 | /* forward tag: assume 32bit pointer */ |
| 63 | mov 8(%ebx), %ecx |
| 64 | jmp .headerSearch |
| 65 | |
| 66 | .testMemory: |
| 67 | cmp $CB_TAG_MEMORY, (%ebx) |
| 68 | jne .testFramebuffer |
| 69 | |
| 70 | /* memory tag: copy e820 map and entry count. also determine alt_mem_k */ |
| 71 | mov 4(%ebx), %eax |
| 72 | sub $8, %eax |
| 73 | shr $2, %eax /* eax = number of dwords of e820 data */ |
| 74 | cmp $(32 * 5), %eax /* linux wants at most 32 entries of 5 dwords */ |
| 75 | jng 1f |
| 76 | mov $(32 * 5), %eax /* only copy 32 entries */ |
| 77 | 1: |
| 78 | mov %eax, %esi |
| 79 | mov $5, %edi |
| 80 | div %edi |
| 81 | mov %eax, (LINUX_PARAM_LOC + E820_NR_OFFSET) |
| 82 | mov %esi, %eax |
| 83 | xchg %eax, %ecx |
| 84 | lea 8(%ebx), %esi /* e820 data source */ |
| 85 | mov $(LINUX_PARAM_LOC + E820_OFFSET), %edi |
| 86 | rep movsl |
| 87 | xchg %eax, %ecx |
| 88 | jmp .endScan |
| 89 | |
| 90 | .testFramebuffer: |
| 91 | cmp $CB_TAG_FRAMEBUFFER, (%ebx) |
| 92 | jne .endScan |
| 93 | /* TODO: handle framebuffer tag */ |
| 94 | |
| 95 | .endScan: |
| 96 | add 4(%ebx), %ebx |
| 97 | dec %ecx |
| 98 | jnz .tableScan |
| 99 | |
| 100 | /* Setup basic code and data segment selectors for Linux |
| 101 | ** |
| 102 | ** Flat code segment descriptor: |
| 103 | ** selector: 0x10 |
| 104 | ** base : 0x00000000 |
| 105 | ** limit : 0xFFFFFFFF |
| 106 | ** type : code, execute, read |
| 107 | ** |
| 108 | ** Flat data segment descriptor: |
| 109 | ** selector: 0x18 |
| 110 | ** base : 0x00000000 |
| 111 | ** limit : 0xFFFFFFFF |
| 112 | ** type : data, read/write |
| 113 | ** |
| 114 | ** Use TRAMPOLINE_ENTRY_LOC as a scratchpad. |
| 115 | */ |
| 116 | mov $TRAMPOLINE_ENTRY_LOC, %eax |
| 117 | sgdt (%eax) |
| 118 | mov 2(%eax), %ebx |
| 119 | movl $0x0000ffff, 16(%ebx) |
| 120 | movl $0x00cf9b00, 20(%ebx) |
| 121 | movl $0x0000ffff, 24(%ebx) |
| 122 | movl $0x00cf9300, 28(%ebx) |
| 123 | |
| 124 | /* finally: jump to kernel */ |
| 125 | mov $LINUX_PARAM_LOC, %esi |
| 126 | jmp *(LINUX_PARAM_LOC + LINUX_ENTRY_OFFSET) |
| 127 | |
| 128 | |
| 129 | 2: |
| 130 | hlt |
| 131 | jmp 2b |
| 132 | |
| 133 | .trampoline_end: |