Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [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 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #if 0 |
| 21 | /* NOTE: THIS CODE MUST REMAIN POSITION INDEPENDENT |
| 22 | * IT SHOULDN'T USE THE STACK |
| 23 | * AND IN GENERAL EXPECT NOTHING BUT RAM TO WORK |
| 24 | */ |
| 25 | .code32 |
| 26 | .data |
Curt Brune | 3c12cb0 | 2014-08-29 10:43:36 -0700 | [diff] [blame] | 27 | |
| 28 | #include "linux_trampoline.h" |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 29 | #define HEADER_SIG 0x4f49424c // LBIO little endian |
| 30 | #define CB_TAG_FORWARD 0x11 |
| 31 | #define CB_TAG_MEMORY 0x1 |
| 32 | #define CB_TAG_FRAMEBUFFER 0x12 |
| 33 | |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 34 | #define E820_NR_OFFSET 0x1e8 |
| 35 | #define LINUX_ENTRY_OFFSET 0x214 |
| 36 | #define E820_OFFSET 0x2d0 |
| 37 | |
| 38 | .trampoline_start: |
| 39 | |
| 40 | cld |
| 41 | xor %edx, %edx |
| 42 | mov $0, %ecx |
| 43 | |
| 44 | .headerSearch: |
| 45 | mov $0x10000, %ebx |
| 46 | add %ecx, %ebx |
| 47 | mov (%ecx), %eax |
| 48 | cmp $HEADER_SIG, %eax |
| 49 | je .headerSearchDone // found the header |
| 50 | add $16, %ecx |
| 51 | cmp %ecx, %ebx |
| 52 | jne .headerSearch |
| 53 | |
| 54 | .headerSearchDone: |
| 55 | cmp %ecx, %ebx // reached the end == not found anything? |
| 56 | je 2f // give up |
| 57 | |
| 58 | // we assume the checksum is okay, no test |
| 59 | mov 4(%ecx), %ebx |
| 60 | add %ecx, %ebx // ebx = cb_header + header_bytes |
| 61 | mov 20(%ecx), %ecx // ecx = table_entries |
| 62 | |
| 63 | .tableScan: |
| 64 | cmp $CB_TAG_FORWARD, (%ebx) |
| 65 | jne .testMemory |
| 66 | |
| 67 | /* forward tag: assume 32bit pointer */ |
| 68 | mov 8(%ebx), %ecx |
| 69 | jmp .headerSearch |
| 70 | |
| 71 | .testMemory: |
| 72 | cmp $CB_TAG_MEMORY, (%ebx) |
| 73 | jne .testFramebuffer |
| 74 | |
| 75 | /* memory tag: copy e820 map and entry count. also determine alt_mem_k */ |
| 76 | mov 4(%ebx), %eax |
| 77 | sub $8, %eax |
| 78 | shr $2, %eax /* eax = number of dwords of e820 data */ |
| 79 | cmp $(32 * 5), %eax /* linux wants at most 32 entries of 5 dwords */ |
| 80 | jng 1f |
| 81 | mov $(32 * 5), %eax /* only copy 32 entries */ |
| 82 | 1: |
| 83 | mov %eax, %esi |
| 84 | mov $5, %edi |
| 85 | div %edi |
| 86 | mov %eax, (LINUX_PARAM_LOC + E820_NR_OFFSET) |
| 87 | mov %esi, %eax |
| 88 | xchg %eax, %ecx |
| 89 | lea 8(%ebx), %esi /* e820 data source */ |
| 90 | mov $(LINUX_PARAM_LOC + E820_OFFSET), %edi |
| 91 | rep movsl |
| 92 | xchg %eax, %ecx |
| 93 | jmp .endScan |
| 94 | |
| 95 | .testFramebuffer: |
| 96 | cmp $CB_TAG_FRAMEBUFFER, (%ebx) |
| 97 | jne .endScan |
| 98 | /* TODO: handle framebuffer tag */ |
| 99 | |
| 100 | .endScan: |
| 101 | add 4(%ebx), %ebx |
| 102 | dec %ecx |
| 103 | jnz .tableScan |
| 104 | |
Curt Brune | 3c12cb0 | 2014-08-29 10:43:36 -0700 | [diff] [blame] | 105 | /* Setup basic code and data segment selectors for Linux |
| 106 | ** |
| 107 | ** Flat code segment descriptor: |
| 108 | ** selector: 0x10 |
| 109 | ** base : 0x00000000 |
| 110 | ** limit : 0xFFFFFFFF |
| 111 | ** type : code, execute, read |
| 112 | ** |
| 113 | ** Flat data segment descriptor: |
| 114 | ** selector: 0x18 |
| 115 | ** base : 0x00000000 |
| 116 | ** limit : 0xFFFFFFFF |
| 117 | ** type : data, read/write |
| 118 | ** |
| 119 | ** Use TRAMPOLINE_ENTRY_LOC as a scratchpad. |
| 120 | */ |
| 121 | mov $TRAMPOLINE_ENTRY_LOC, %eax |
| 122 | sgdt (%eax) |
| 123 | mov 2(%eax), %ebx |
| 124 | movl $0x0000ffff, 16(%ebx) |
| 125 | movl $0x00cf9b00, 20(%ebx) |
| 126 | movl $0x0000ffff, 24(%ebx) |
| 127 | movl $0x00cf9300, 28(%ebx) |
| 128 | |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 129 | /* finally: jump to kernel */ |
| 130 | mov $LINUX_PARAM_LOC, %esi |
| 131 | jmp *(LINUX_PARAM_LOC + LINUX_ENTRY_OFFSET) |
| 132 | |
| 133 | |
| 134 | 2: |
| 135 | hlt |
| 136 | jmp 2b |
| 137 | |
| 138 | .trampoline_end: |
| 139 | |
| 140 | .global trampoline_start, trampoline_size |
| 141 | trampoline_start: |
| 142 | .long .trampoline_start |
| 143 | trampoline_size: |
| 144 | .long .trampoline_end - .trampoline_start |
| 145 | #endif |
| 146 | |
| 147 | /* The code above is hand-crafted to fit various contraints. |
| 148 | * To simplify porting, the below matches the above. |
| 149 | * When changing any code in here, compile the above as a .S |
| 150 | * file, objcopy it to binary and paste the result below (minus |
| 151 | * the last 8 bytes which are trampoline_start and trampoline_size). |
| 152 | */ |
| 153 | const unsigned char trampoline[] = { |
| 154 | 0xfc, 0x31, 0xd2, 0xb9, 0x00, 0x00, 0x00, 0x00, 0xbb, 0x00, 0x00, 0x01, 0x00, 0x01, 0xcb, 0x8b, |
| 155 | 0x01, 0x3d, 0x4c, 0x42, 0x49, 0x4f, 0x74, 0x07, 0x83, 0xc1, 0x10, 0x39, 0xcb, 0x75, 0xe9, 0x39, |
Curt Brune | 3c12cb0 | 2014-08-29 10:43:36 -0700 | [diff] [blame] | 156 | 0xcb, 0x0f, 0x84, 0x85, 0x00, 0x00, 0x00, 0x8b, 0x59, 0x04, 0x01, 0xcb, 0x8b, 0x49, 0x14, 0x83, |
| 157 | 0x3b, 0x11, 0x75, 0x05, 0x8b, 0x4b, 0x08, 0xeb, 0xcf, 0x83, 0x3b, 0x01, 0x75, 0x33, 0x8b, 0x43, |
| 158 | 0x04, 0x83, 0xe8, 0x08, 0xc1, 0xe8, 0x02, 0x3d, 0xa0, 0x00, 0x00, 0x00, 0x7e, 0x05, 0xb8, 0xa0, |
| 159 | 0x00, 0x00, 0x00, 0x89, 0xc6, 0xbf, 0x05, 0x00, 0x00, 0x00, 0xf7, 0xf7, 0xa3, 0xe8, 0x01, 0x09, |
| 160 | 0x00, 0x89, 0xf0, 0x91, 0x8d, 0x73, 0x08, 0xbf, 0xd0, 0x02, 0x09, 0x00, 0xf3, 0xa5, 0x91, 0xeb, |
| 161 | 0x05, 0x83, 0x3b, 0x12, 0x75, 0x00, 0x03, 0x5b, 0x04, 0x49, 0x75, 0xb3, 0xb8, 0x00, 0x00, 0x04, |
| 162 | 0x00, 0x0f, 0x01, 0x00, 0x8b, 0x58, 0x02, 0xc7, 0x43, 0x10, 0xff, 0xff, 0x00, 0x00, 0xc7, 0x43, |
| 163 | 0x14, 0x00, 0x9b, 0xcf, 0x00, 0xc7, 0x43, 0x18, 0xff, 0xff, 0x00, 0x00, 0xc7, 0x43, 0x1c, 0x00, |
| 164 | 0x93, 0xcf, 0x00, 0xbe, 0x00, 0x00, 0x09, 0x00, 0xff, 0x25, 0x14, 0x02, 0x09, 0x00, 0xf4, 0xeb, |
| 165 | 0xfd |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 166 | }; |
| 167 | |
Alexandru Gagniuc | ae45a98 | 2014-01-29 14:27:52 -0600 | [diff] [blame] | 168 | const void * const trampoline_start = &trampoline; |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 169 | const unsigned long trampoline_size = sizeof trampoline; |