Patrick Georgi | ea063cb | 2020-05-08 19:28:13 +0200 | [diff] [blame] | 1 | /* linux_trampoline */ |
Patrick Georgi | 7333a11 | 2020-05-08 20:48:04 +0200 | [diff] [blame] | 2 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 3 | |
| 4 | /* NOTE: THIS CODE MUST REMAIN POSITION INDEPENDENT |
| 5 | * IT SHOULDN'T USE THE STACK |
| 6 | * AND IN GENERAL EXPECT NOTHING BUT RAM TO WORK |
| 7 | */ |
| 8 | .code32 |
| 9 | .data |
| 10 | |
| 11 | #include "linux_trampoline.h" |
| 12 | #define HEADER_SIG 0x4f49424c // LBIO little endian |
| 13 | #define CB_TAG_FORWARD 0x11 |
| 14 | #define CB_TAG_MEMORY 0x1 |
| 15 | #define CB_TAG_FRAMEBUFFER 0x12 |
| 16 | |
| 17 | #define E820_NR_OFFSET 0x1e8 |
| 18 | #define LINUX_ENTRY_OFFSET 0x214 |
| 19 | #define E820_OFFSET 0x2d0 |
| 20 | |
| 21 | .trampoline_start: |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 22 | cld |
| 23 | xor %edx, %edx |
| 24 | mov $0, %ecx |
| 25 | |
| 26 | .headerSearch: |
| 27 | mov $0x10000, %ebx |
| 28 | add %ecx, %ebx |
| 29 | mov (%ecx), %eax |
| 30 | cmp $HEADER_SIG, %eax |
| 31 | je .headerSearchDone // found the header |
| 32 | add $16, %ecx |
| 33 | cmp %ecx, %ebx |
| 34 | jne .headerSearch |
| 35 | |
| 36 | .headerSearchDone: |
| 37 | cmp %ecx, %ebx // reached the end == not found anything? |
| 38 | je 2f // give up |
| 39 | |
| 40 | // we assume the checksum is okay, no test |
| 41 | mov 4(%ecx), %ebx |
| 42 | add %ecx, %ebx // ebx = cb_header + header_bytes |
| 43 | mov 20(%ecx), %ecx // ecx = table_entries |
| 44 | |
| 45 | .tableScan: |
| 46 | cmp $CB_TAG_FORWARD, (%ebx) |
| 47 | jne .testMemory |
| 48 | |
| 49 | /* forward tag: assume 32bit pointer */ |
| 50 | mov 8(%ebx), %ecx |
| 51 | jmp .headerSearch |
| 52 | |
| 53 | .testMemory: |
| 54 | cmp $CB_TAG_MEMORY, (%ebx) |
| 55 | jne .testFramebuffer |
| 56 | |
| 57 | /* memory tag: copy e820 map and entry count. also determine alt_mem_k */ |
| 58 | mov 4(%ebx), %eax |
| 59 | sub $8, %eax |
| 60 | shr $2, %eax /* eax = number of dwords of e820 data */ |
| 61 | cmp $(32 * 5), %eax /* linux wants at most 32 entries of 5 dwords */ |
| 62 | jng 1f |
| 63 | mov $(32 * 5), %eax /* only copy 32 entries */ |
| 64 | 1: |
| 65 | mov %eax, %esi |
| 66 | mov $5, %edi |
| 67 | div %edi |
| 68 | mov %eax, (LINUX_PARAM_LOC + E820_NR_OFFSET) |
| 69 | mov %esi, %eax |
| 70 | xchg %eax, %ecx |
| 71 | lea 8(%ebx), %esi /* e820 data source */ |
| 72 | mov $(LINUX_PARAM_LOC + E820_OFFSET), %edi |
| 73 | rep movsl |
| 74 | xchg %eax, %ecx |
| 75 | jmp .endScan |
| 76 | |
| 77 | .testFramebuffer: |
| 78 | cmp $CB_TAG_FRAMEBUFFER, (%ebx) |
| 79 | jne .endScan |
| 80 | /* TODO: handle framebuffer tag */ |
| 81 | |
| 82 | .endScan: |
| 83 | add 4(%ebx), %ebx |
| 84 | dec %ecx |
| 85 | jnz .tableScan |
| 86 | |
| 87 | /* Setup basic code and data segment selectors for Linux |
| 88 | ** |
| 89 | ** Flat code segment descriptor: |
| 90 | ** selector: 0x10 |
| 91 | ** base : 0x00000000 |
| 92 | ** limit : 0xFFFFFFFF |
| 93 | ** type : code, execute, read |
| 94 | ** |
| 95 | ** Flat data segment descriptor: |
| 96 | ** selector: 0x18 |
| 97 | ** base : 0x00000000 |
| 98 | ** limit : 0xFFFFFFFF |
| 99 | ** type : data, read/write |
| 100 | ** |
| 101 | ** Use TRAMPOLINE_ENTRY_LOC as a scratchpad. |
| 102 | */ |
| 103 | mov $TRAMPOLINE_ENTRY_LOC, %eax |
Ronald G. Minnich | eeb83b6 | 2018-07-18 07:19:30 -0700 | [diff] [blame] | 104 | movl $0x0000ffff, 16(%eax) // Set up the 2 new descriptors |
| 105 | movl $0x00cf9b00, 20(%eax) |
| 106 | movl $0x0000ffff, 24(%eax) |
| 107 | movl $0x00cf9300, 28(%eax) |
| 108 | movb $0x2b, 0(%eax) // Set the size |
| 109 | movl %eax, 2(%eax) // Set pointer to new GDT |
| 110 | lgdt (%eax) // Load it |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 111 | |
| 112 | /* finally: jump to kernel */ |
| 113 | mov $LINUX_PARAM_LOC, %esi |
| 114 | jmp *(LINUX_PARAM_LOC + LINUX_ENTRY_OFFSET) |
| 115 | |
| 116 | |
| 117 | 2: |
| 118 | hlt |
| 119 | jmp 2b |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 120 | .trampoline_end: |