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