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 */ |
| 64 | cmp $(32 * 5), %eax /* linux wants at most 32 entries of 5 dwords */ |
| 65 | jng 1f |
| 66 | mov $(32 * 5), %eax /* only copy 32 entries */ |
| 67 | 1: |
| 68 | mov %eax, %esi |
| 69 | mov $5, %edi |
| 70 | div %edi |
| 71 | mov %eax, (LINUX_PARAM_LOC + E820_NR_OFFSET) |
| 72 | mov %esi, %eax |
| 73 | xchg %eax, %ecx |
| 74 | lea 8(%ebx), %esi /* e820 data source */ |
| 75 | mov $(LINUX_PARAM_LOC + E820_OFFSET), %edi |
| 76 | rep movsl |
| 77 | xchg %eax, %ecx |
Arthur Heymans | 71971c9 | 2021-05-29 20:07:42 +0200 | [diff] [blame] | 78 | /* e820 and LB_TAG_MEMORY type don't fully match: remap unknown type to 2, reserved memory */ |
| 79 | mov (LINUX_PARAM_LOC + E820_NR_OFFSET), %eax |
| 80 | mov $(LINUX_PARAM_LOC + E820_OFFSET), %edi |
| 81 | .test_e820_entry: |
| 82 | cmp $0, %eax |
| 83 | je .endScan |
| 84 | cmp $12, 16(%edi) /* type */ |
| 85 | jng .next_e820_entry |
| 86 | /* Fixup the type to 2, reserved memory */ |
| 87 | mov $2, 16(%edi) |
| 88 | .next_e820_entry: |
| 89 | dec %eax |
| 90 | add $20, %edi |
| 91 | jmp .test_e820_entry |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 92 | |
Arthur Heymans | efdcb46 | 2022-03-03 22:31:56 +0100 | [diff] [blame] | 93 | .testAcpiRsdp: |
| 94 | cmp $CB_TAG_ACPI_RSDP, (%ebx) |
| 95 | jne .testFramebuffer |
| 96 | |
| 97 | mov 8(%ebx), %eax |
| 98 | mov %eax, (LINUX_PARAM_LOC + ACPI_RSDP_ADDR) |
| 99 | mov 12(%ebx), %eax |
| 100 | mov %eax, (LINUX_PARAM_LOC + ACPI_RSDP_ADDR + 4) |
| 101 | jmp .endScan |
| 102 | |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 103 | .testFramebuffer: |
| 104 | cmp $CB_TAG_FRAMEBUFFER, (%ebx) |
| 105 | jne .endScan |
Nico Huber | 295f6bf | 2023-07-14 14:20:39 +0200 | [diff] [blame] | 106 | |
Nico Huber | 6e133fa | 2023-07-14 23:15:17 +0200 | [diff] [blame] | 107 | cmpw $0x020f, (LINUX_PARAM_LOC + PROTOCOL_VERSION) |
| 108 | jge .framebufferSetup /* protocol version >= 2.15 can handle 64-bit address */ |
Nico Huber | 295f6bf | 2023-07-14 14:20:39 +0200 | [diff] [blame] | 109 | cmpl $0, 0x0c(%ebx) /* check if upper 32-bit of framebuffer address are 0 */ |
| 110 | jne .endScan |
| 111 | |
Nico Huber | 6e133fa | 2023-07-14 23:15:17 +0200 | [diff] [blame] | 112 | .framebufferSetup: |
Nico Huber | 295f6bf | 2023-07-14 14:20:39 +0200 | [diff] [blame] | 113 | mov $LINUX_PARAM_LOC, %edi /* translate the framebuffer entry into Linux' struct screen_info */ |
| 114 | mov 0x08(%ebx), %eax /* physical_address */ |
| 115 | mov %eax, 0x18(%edi) /* -> lfb_base */ |
Nico Huber | 6e133fa | 2023-07-14 23:15:17 +0200 | [diff] [blame] | 116 | mov 0x0c(%ebx), %eax /* physical_address */ |
| 117 | mov %eax, 0x3a(%edi) /* -> ext_lfb_base */ |
Nico Huber | 295f6bf | 2023-07-14 14:20:39 +0200 | [diff] [blame] | 118 | mov 0x10(%ebx), %eax /* x_resolution */ |
| 119 | mov %ax, 0x12(%edi) /* -> lfb_width */ |
| 120 | mov 0x14(%ebx), %eax /* y_resolution */ |
| 121 | mov %ax, 0x14(%edi) /* -> lfb_height */ |
| 122 | mov 0x18(%ebx), %edx /* bytes_per_line */ |
| 123 | mov %dx, 0x24(%edi) /* -> lfb_linelength */ |
| 124 | |
| 125 | mul %edx /* bytes_per_line * y_resolution */ |
| 126 | mov %eax, 0x1c(%edi) /* -> lfb_size */ |
| 127 | |
| 128 | movzbw 0x1c(%ebx), %ax /* bits_per_pixel */ |
| 129 | mov %ax, 0x16(%edi) /* -> lfb_depth */ |
| 130 | |
| 131 | mov $4, %esi /* Copy 4 color components' pos and size, each 1 byte. */ |
| 132 | 1: |
| 133 | mov 0x1b(%ebx, %esi, 2), %ax |
| 134 | rol %ax /* Order is reversed for Linux, hence swap. */ |
| 135 | mov %ax, 0x24(%edi, %esi, 2) |
| 136 | dec %esi |
| 137 | jnz 1b |
| 138 | |
Nico Huber | 6e133fa | 2023-07-14 23:15:17 +0200 | [diff] [blame] | 139 | #define VIDEO_CAPABILITY_64BIT_BASE (1 << 1) |
| 140 | movl $VIDEO_CAPABILITY_64BIT_BASE, 0x36(%edi) |
| 141 | |
Nico Huber | 295f6bf | 2023-07-14 14:20:39 +0200 | [diff] [blame] | 142 | #define LFB_EFI_SIMPLE 0x70 /* VIDEO_TYPE_EFI in Linux */ |
| 143 | movb $LFB_EFI_SIMPLE, 0x0f(%edi) /* -> orig_video_isVGA */ |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 144 | |
| 145 | .endScan: |
| 146 | add 4(%ebx), %ebx |
| 147 | dec %ecx |
| 148 | jnz .tableScan |
| 149 | |
| 150 | /* Setup basic code and data segment selectors for Linux |
| 151 | ** |
| 152 | ** Flat code segment descriptor: |
| 153 | ** selector: 0x10 |
| 154 | ** base : 0x00000000 |
| 155 | ** limit : 0xFFFFFFFF |
| 156 | ** type : code, execute, read |
| 157 | ** |
| 158 | ** Flat data segment descriptor: |
| 159 | ** selector: 0x18 |
| 160 | ** base : 0x00000000 |
| 161 | ** limit : 0xFFFFFFFF |
| 162 | ** type : data, read/write |
| 163 | ** |
| 164 | ** Use TRAMPOLINE_ENTRY_LOC as a scratchpad. |
| 165 | */ |
| 166 | mov $TRAMPOLINE_ENTRY_LOC, %eax |
Ronald G. Minnich | eeb83b6 | 2018-07-18 07:19:30 -0700 | [diff] [blame] | 167 | movl $0x0000ffff, 16(%eax) // Set up the 2 new descriptors |
| 168 | movl $0x00cf9b00, 20(%eax) |
| 169 | movl $0x0000ffff, 24(%eax) |
| 170 | movl $0x00cf9300, 28(%eax) |
| 171 | movb $0x2b, 0(%eax) // Set the size |
| 172 | movl %eax, 2(%eax) // Set pointer to new GDT |
| 173 | lgdt (%eax) // Load it |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 174 | |
| 175 | /* finally: jump to kernel */ |
| 176 | mov $LINUX_PARAM_LOC, %esi |
| 177 | jmp *(LINUX_PARAM_LOC + LINUX_ENTRY_OFFSET) |
| 178 | |
| 179 | |
| 180 | 2: |
| 181 | hlt |
| 182 | jmp 2b |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 183 | .trampoline_end: |