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 |
Nico Huber | 6e133fa | 2023-07-14 23:15:17 +0200 | [diff] [blame] | 20 | #define PROTOCOL_VERSION 0x206 |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 21 | #define LINUX_ENTRY_OFFSET 0x214 |
| 22 | #define E820_OFFSET 0x2d0 |
| 23 | |
| 24 | .trampoline_start: |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 25 | cld |
| 26 | xor %edx, %edx |
| 27 | mov $0, %ecx |
| 28 | |
| 29 | .headerSearch: |
| 30 | mov $0x10000, %ebx |
| 31 | add %ecx, %ebx |
| 32 | mov (%ecx), %eax |
| 33 | cmp $HEADER_SIG, %eax |
| 34 | je .headerSearchDone // found the header |
| 35 | add $16, %ecx |
| 36 | cmp %ecx, %ebx |
| 37 | jne .headerSearch |
| 38 | |
| 39 | .headerSearchDone: |
| 40 | cmp %ecx, %ebx // reached the end == not found anything? |
| 41 | je 2f // give up |
| 42 | |
| 43 | // we assume the checksum is okay, no test |
| 44 | mov 4(%ecx), %ebx |
| 45 | add %ecx, %ebx // ebx = cb_header + header_bytes |
| 46 | mov 20(%ecx), %ecx // ecx = table_entries |
| 47 | |
| 48 | .tableScan: |
| 49 | cmp $CB_TAG_FORWARD, (%ebx) |
| 50 | jne .testMemory |
| 51 | |
| 52 | /* forward tag: assume 32bit pointer */ |
| 53 | mov 8(%ebx), %ecx |
| 54 | jmp .headerSearch |
| 55 | |
| 56 | .testMemory: |
| 57 | cmp $CB_TAG_MEMORY, (%ebx) |
Arthur Heymans | efdcb46 | 2022-03-03 22:31:56 +0100 | [diff] [blame] | 58 | jne .testAcpiRsdp |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 59 | |
| 60 | /* memory tag: copy e820 map and entry count. also determine alt_mem_k */ |
| 61 | mov 4(%ebx), %eax |
| 62 | sub $8, %eax |
| 63 | shr $2, %eax /* eax = number of dwords of e820 data */ |
Patrick Rudolph | 7d4155e | 2024-02-15 10:28:00 +0100 | [diff] [blame] | 64 | /* |
| 65 | * Historically linux had space for 32 entries. This limit was increased in |
| 66 | * the year 2005 (Linux 2.6.11) to hold up to 128 entries. |
| 67 | * Assume 128 entries when the boot protocol version is 2.04+. |
| 68 | */ |
| 69 | cmpw $0x0204, (LINUX_PARAM_LOC + PROTOCOL_VERSION) |
| 70 | jge .e820big /* protocol version >= 2.04 can handle 128 entries of 5 dwords */ |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 71 | cmp $(32 * 5), %eax /* linux wants at most 32 entries of 5 dwords */ |
| 72 | jng 1f |
| 73 | mov $(32 * 5), %eax /* only copy 32 entries */ |
Patrick Rudolph | 7d4155e | 2024-02-15 10:28:00 +0100 | [diff] [blame] | 74 | jmp 1f |
| 75 | |
| 76 | .e820big: |
| 77 | cmp $(128 * 5), %eax /* linux wants at most 128 entries of 5 dwords */ |
| 78 | jng 1f |
| 79 | mov $(128 * 5), %eax /* only copy 128 entries */ |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 80 | 1: |
| 81 | mov %eax, %esi |
| 82 | mov $5, %edi |
| 83 | div %edi |
| 84 | mov %eax, (LINUX_PARAM_LOC + E820_NR_OFFSET) |
| 85 | mov %esi, %eax |
| 86 | xchg %eax, %ecx |
| 87 | lea 8(%ebx), %esi /* e820 data source */ |
| 88 | mov $(LINUX_PARAM_LOC + E820_OFFSET), %edi |
| 89 | rep movsl |
| 90 | xchg %eax, %ecx |
Arthur Heymans | 71971c9 | 2021-05-29 20:07:42 +0200 | [diff] [blame] | 91 | /* e820 and LB_TAG_MEMORY type don't fully match: remap unknown type to 2, reserved memory */ |
| 92 | mov (LINUX_PARAM_LOC + E820_NR_OFFSET), %eax |
| 93 | mov $(LINUX_PARAM_LOC + E820_OFFSET), %edi |
| 94 | .test_e820_entry: |
| 95 | cmp $0, %eax |
| 96 | je .endScan |
| 97 | cmp $12, 16(%edi) /* type */ |
| 98 | jng .next_e820_entry |
| 99 | /* Fixup the type to 2, reserved memory */ |
Shuo Liu | 740cf98 | 2024-05-21 01:13:35 +0800 | [diff] [blame] | 100 | movl $2, 16(%edi) |
Arthur Heymans | 71971c9 | 2021-05-29 20:07:42 +0200 | [diff] [blame] | 101 | .next_e820_entry: |
| 102 | dec %eax |
| 103 | add $20, %edi |
| 104 | jmp .test_e820_entry |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 105 | |
Arthur Heymans | efdcb46 | 2022-03-03 22:31:56 +0100 | [diff] [blame] | 106 | .testAcpiRsdp: |
| 107 | cmp $CB_TAG_ACPI_RSDP, (%ebx) |
| 108 | jne .testFramebuffer |
| 109 | |
| 110 | mov 8(%ebx), %eax |
| 111 | mov %eax, (LINUX_PARAM_LOC + ACPI_RSDP_ADDR) |
| 112 | mov 12(%ebx), %eax |
| 113 | mov %eax, (LINUX_PARAM_LOC + ACPI_RSDP_ADDR + 4) |
| 114 | jmp .endScan |
| 115 | |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 116 | .testFramebuffer: |
| 117 | cmp $CB_TAG_FRAMEBUFFER, (%ebx) |
| 118 | jne .endScan |
Nico Huber | 295f6bf | 2023-07-14 14:20:39 +0200 | [diff] [blame] | 119 | |
Nico Huber | 6e133fa | 2023-07-14 23:15:17 +0200 | [diff] [blame] | 120 | cmpw $0x020f, (LINUX_PARAM_LOC + PROTOCOL_VERSION) |
| 121 | jge .framebufferSetup /* protocol version >= 2.15 can handle 64-bit address */ |
Nico Huber | 295f6bf | 2023-07-14 14:20:39 +0200 | [diff] [blame] | 122 | cmpl $0, 0x0c(%ebx) /* check if upper 32-bit of framebuffer address are 0 */ |
| 123 | jne .endScan |
| 124 | |
Nico Huber | 6e133fa | 2023-07-14 23:15:17 +0200 | [diff] [blame] | 125 | .framebufferSetup: |
Nico Huber | 295f6bf | 2023-07-14 14:20:39 +0200 | [diff] [blame] | 126 | mov $LINUX_PARAM_LOC, %edi /* translate the framebuffer entry into Linux' struct screen_info */ |
| 127 | mov 0x08(%ebx), %eax /* physical_address */ |
| 128 | mov %eax, 0x18(%edi) /* -> lfb_base */ |
Nico Huber | 6e133fa | 2023-07-14 23:15:17 +0200 | [diff] [blame] | 129 | mov 0x0c(%ebx), %eax /* physical_address */ |
| 130 | mov %eax, 0x3a(%edi) /* -> ext_lfb_base */ |
Nico Huber | 295f6bf | 2023-07-14 14:20:39 +0200 | [diff] [blame] | 131 | mov 0x10(%ebx), %eax /* x_resolution */ |
| 132 | mov %ax, 0x12(%edi) /* -> lfb_width */ |
| 133 | mov 0x14(%ebx), %eax /* y_resolution */ |
| 134 | mov %ax, 0x14(%edi) /* -> lfb_height */ |
| 135 | mov 0x18(%ebx), %edx /* bytes_per_line */ |
| 136 | mov %dx, 0x24(%edi) /* -> lfb_linelength */ |
| 137 | |
| 138 | mul %edx /* bytes_per_line * y_resolution */ |
| 139 | mov %eax, 0x1c(%edi) /* -> lfb_size */ |
| 140 | |
| 141 | movzbw 0x1c(%ebx), %ax /* bits_per_pixel */ |
| 142 | mov %ax, 0x16(%edi) /* -> lfb_depth */ |
| 143 | |
| 144 | mov $4, %esi /* Copy 4 color components' pos and size, each 1 byte. */ |
| 145 | 1: |
| 146 | mov 0x1b(%ebx, %esi, 2), %ax |
| 147 | rol %ax /* Order is reversed for Linux, hence swap. */ |
| 148 | mov %ax, 0x24(%edi, %esi, 2) |
| 149 | dec %esi |
| 150 | jnz 1b |
| 151 | |
Nico Huber | 6e133fa | 2023-07-14 23:15:17 +0200 | [diff] [blame] | 152 | #define VIDEO_CAPABILITY_64BIT_BASE (1 << 1) |
| 153 | movl $VIDEO_CAPABILITY_64BIT_BASE, 0x36(%edi) |
| 154 | |
Nico Huber | 295f6bf | 2023-07-14 14:20:39 +0200 | [diff] [blame] | 155 | #define LFB_EFI_SIMPLE 0x70 /* VIDEO_TYPE_EFI in Linux */ |
| 156 | movb $LFB_EFI_SIMPLE, 0x0f(%edi) /* -> orig_video_isVGA */ |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 157 | |
| 158 | .endScan: |
| 159 | add 4(%ebx), %ebx |
| 160 | dec %ecx |
| 161 | jnz .tableScan |
| 162 | |
| 163 | /* Setup basic code and data segment selectors for Linux |
| 164 | ** |
| 165 | ** Flat code segment descriptor: |
| 166 | ** selector: 0x10 |
| 167 | ** base : 0x00000000 |
| 168 | ** limit : 0xFFFFFFFF |
| 169 | ** type : code, execute, read |
| 170 | ** |
| 171 | ** Flat data segment descriptor: |
| 172 | ** selector: 0x18 |
| 173 | ** base : 0x00000000 |
| 174 | ** limit : 0xFFFFFFFF |
| 175 | ** type : data, read/write |
| 176 | ** |
| 177 | ** Use TRAMPOLINE_ENTRY_LOC as a scratchpad. |
| 178 | */ |
| 179 | mov $TRAMPOLINE_ENTRY_LOC, %eax |
Ronald G. Minnich | eeb83b6 | 2018-07-18 07:19:30 -0700 | [diff] [blame] | 180 | movl $0x0000ffff, 16(%eax) // Set up the 2 new descriptors |
| 181 | movl $0x00cf9b00, 20(%eax) |
| 182 | movl $0x0000ffff, 24(%eax) |
| 183 | movl $0x00cf9300, 28(%eax) |
| 184 | movb $0x2b, 0(%eax) // Set the size |
| 185 | movl %eax, 2(%eax) // Set pointer to new GDT |
| 186 | lgdt (%eax) // Load it |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 187 | |
| 188 | /* finally: jump to kernel */ |
| 189 | mov $LINUX_PARAM_LOC, %esi |
| 190 | jmp *(LINUX_PARAM_LOC + LINUX_ENTRY_OFFSET) |
| 191 | |
| 192 | |
| 193 | 2: |
| 194 | hlt |
| 195 | jmp 2b |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 196 | .trampoline_end: |