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 |
Arthur Heymans | efdcb46 | 2022-03-03 22:31:56 +0100 | [diff] [blame] | 16 | #define CB_TAG_ACPI_RSDP 0x43 |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 17 | |
Arthur Heymans | efdcb46 | 2022-03-03 22:31:56 +0100 | [diff] [blame] | 18 | #define ACPI_RSDP_ADDR 0x70 |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 19 | #define E820_NR_OFFSET 0x1e8 |
| 20 | #define LINUX_ENTRY_OFFSET 0x214 |
| 21 | #define E820_OFFSET 0x2d0 |
| 22 | |
| 23 | .trampoline_start: |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 24 | cld |
| 25 | xor %edx, %edx |
| 26 | mov $0, %ecx |
| 27 | |
| 28 | .headerSearch: |
| 29 | mov $0x10000, %ebx |
| 30 | add %ecx, %ebx |
| 31 | mov (%ecx), %eax |
| 32 | cmp $HEADER_SIG, %eax |
| 33 | je .headerSearchDone // found the header |
| 34 | add $16, %ecx |
| 35 | cmp %ecx, %ebx |
| 36 | jne .headerSearch |
| 37 | |
| 38 | .headerSearchDone: |
| 39 | cmp %ecx, %ebx // reached the end == not found anything? |
| 40 | je 2f // give up |
| 41 | |
| 42 | // we assume the checksum is okay, no test |
| 43 | mov 4(%ecx), %ebx |
| 44 | add %ecx, %ebx // ebx = cb_header + header_bytes |
| 45 | mov 20(%ecx), %ecx // ecx = table_entries |
| 46 | |
| 47 | .tableScan: |
| 48 | cmp $CB_TAG_FORWARD, (%ebx) |
| 49 | jne .testMemory |
| 50 | |
| 51 | /* forward tag: assume 32bit pointer */ |
| 52 | mov 8(%ebx), %ecx |
| 53 | jmp .headerSearch |
| 54 | |
| 55 | .testMemory: |
| 56 | cmp $CB_TAG_MEMORY, (%ebx) |
Arthur Heymans | efdcb46 | 2022-03-03 22:31:56 +0100 | [diff] [blame] | 57 | jne .testAcpiRsdp |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 58 | |
| 59 | /* memory tag: copy e820 map and entry count. also determine alt_mem_k */ |
| 60 | mov 4(%ebx), %eax |
| 61 | sub $8, %eax |
| 62 | shr $2, %eax /* eax = number of dwords of e820 data */ |
| 63 | cmp $(32 * 5), %eax /* linux wants at most 32 entries of 5 dwords */ |
| 64 | jng 1f |
| 65 | mov $(32 * 5), %eax /* only copy 32 entries */ |
| 66 | 1: |
| 67 | mov %eax, %esi |
| 68 | mov $5, %edi |
| 69 | div %edi |
| 70 | mov %eax, (LINUX_PARAM_LOC + E820_NR_OFFSET) |
| 71 | mov %esi, %eax |
| 72 | xchg %eax, %ecx |
| 73 | lea 8(%ebx), %esi /* e820 data source */ |
| 74 | mov $(LINUX_PARAM_LOC + E820_OFFSET), %edi |
| 75 | rep movsl |
| 76 | xchg %eax, %ecx |
Arthur Heymans | 71971c9 | 2021-05-29 20:07:42 +0200 | [diff] [blame] | 77 | /* e820 and LB_TAG_MEMORY type don't fully match: remap unknown type to 2, reserved memory */ |
| 78 | mov (LINUX_PARAM_LOC + E820_NR_OFFSET), %eax |
| 79 | mov $(LINUX_PARAM_LOC + E820_OFFSET), %edi |
| 80 | .test_e820_entry: |
| 81 | cmp $0, %eax |
| 82 | je .endScan |
| 83 | cmp $12, 16(%edi) /* type */ |
| 84 | jng .next_e820_entry |
| 85 | /* Fixup the type to 2, reserved memory */ |
| 86 | mov $2, 16(%edi) |
| 87 | .next_e820_entry: |
| 88 | dec %eax |
| 89 | add $20, %edi |
| 90 | jmp .test_e820_entry |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 91 | |
Arthur Heymans | efdcb46 | 2022-03-03 22:31:56 +0100 | [diff] [blame] | 92 | .testAcpiRsdp: |
| 93 | cmp $CB_TAG_ACPI_RSDP, (%ebx) |
| 94 | jne .testFramebuffer |
| 95 | |
| 96 | mov 8(%ebx), %eax |
| 97 | mov %eax, (LINUX_PARAM_LOC + ACPI_RSDP_ADDR) |
| 98 | mov 12(%ebx), %eax |
| 99 | mov %eax, (LINUX_PARAM_LOC + ACPI_RSDP_ADDR + 4) |
| 100 | jmp .endScan |
| 101 | |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 102 | .testFramebuffer: |
| 103 | cmp $CB_TAG_FRAMEBUFFER, (%ebx) |
| 104 | jne .endScan |
| 105 | /* TODO: handle framebuffer tag */ |
| 106 | |
| 107 | .endScan: |
| 108 | add 4(%ebx), %ebx |
| 109 | dec %ecx |
| 110 | jnz .tableScan |
| 111 | |
| 112 | /* Setup basic code and data segment selectors for Linux |
| 113 | ** |
| 114 | ** Flat code segment descriptor: |
| 115 | ** selector: 0x10 |
| 116 | ** base : 0x00000000 |
| 117 | ** limit : 0xFFFFFFFF |
| 118 | ** type : code, execute, read |
| 119 | ** |
| 120 | ** Flat data segment descriptor: |
| 121 | ** selector: 0x18 |
| 122 | ** base : 0x00000000 |
| 123 | ** limit : 0xFFFFFFFF |
| 124 | ** type : data, read/write |
| 125 | ** |
| 126 | ** Use TRAMPOLINE_ENTRY_LOC as a scratchpad. |
| 127 | */ |
| 128 | mov $TRAMPOLINE_ENTRY_LOC, %eax |
Ronald G. Minnich | eeb83b6 | 2018-07-18 07:19:30 -0700 | [diff] [blame] | 129 | movl $0x0000ffff, 16(%eax) // Set up the 2 new descriptors |
| 130 | movl $0x00cf9b00, 20(%eax) |
| 131 | movl $0x0000ffff, 24(%eax) |
| 132 | movl $0x00cf9300, 28(%eax) |
| 133 | movb $0x2b, 0(%eax) // Set the size |
| 134 | movl %eax, 2(%eax) // Set pointer to new GDT |
| 135 | lgdt (%eax) // Load it |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 136 | |
| 137 | /* finally: jump to kernel */ |
| 138 | mov $LINUX_PARAM_LOC, %esi |
| 139 | jmp *(LINUX_PARAM_LOC + LINUX_ENTRY_OFFSET) |
| 140 | |
| 141 | |
| 142 | 2: |
| 143 | hlt |
| 144 | jmp 2b |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 145 | .trampoline_end: |