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 |
Nico Huber | 295f6bf | 2023-07-14 14:20:39 +0200 | [diff] [blame^] | 105 | |
| 106 | cmpl $0, 0x0c(%ebx) /* check if upper 32-bit of framebuffer address are 0 */ |
| 107 | jne .endScan |
| 108 | |
| 109 | mov $LINUX_PARAM_LOC, %edi /* translate the framebuffer entry into Linux' struct screen_info */ |
| 110 | mov 0x08(%ebx), %eax /* physical_address */ |
| 111 | mov %eax, 0x18(%edi) /* -> lfb_base */ |
| 112 | mov 0x10(%ebx), %eax /* x_resolution */ |
| 113 | mov %ax, 0x12(%edi) /* -> lfb_width */ |
| 114 | mov 0x14(%ebx), %eax /* y_resolution */ |
| 115 | mov %ax, 0x14(%edi) /* -> lfb_height */ |
| 116 | mov 0x18(%ebx), %edx /* bytes_per_line */ |
| 117 | mov %dx, 0x24(%edi) /* -> lfb_linelength */ |
| 118 | |
| 119 | mul %edx /* bytes_per_line * y_resolution */ |
| 120 | mov %eax, 0x1c(%edi) /* -> lfb_size */ |
| 121 | |
| 122 | movzbw 0x1c(%ebx), %ax /* bits_per_pixel */ |
| 123 | mov %ax, 0x16(%edi) /* -> lfb_depth */ |
| 124 | |
| 125 | mov $4, %esi /* Copy 4 color components' pos and size, each 1 byte. */ |
| 126 | 1: |
| 127 | mov 0x1b(%ebx, %esi, 2), %ax |
| 128 | rol %ax /* Order is reversed for Linux, hence swap. */ |
| 129 | mov %ax, 0x24(%edi, %esi, 2) |
| 130 | dec %esi |
| 131 | jnz 1b |
| 132 | |
| 133 | #define LFB_EFI_SIMPLE 0x70 /* VIDEO_TYPE_EFI in Linux */ |
| 134 | movb $LFB_EFI_SIMPLE, 0x0f(%edi) /* -> orig_video_isVGA */ |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 135 | |
| 136 | .endScan: |
| 137 | add 4(%ebx), %ebx |
| 138 | dec %ecx |
| 139 | jnz .tableScan |
| 140 | |
| 141 | /* Setup basic code and data segment selectors for Linux |
| 142 | ** |
| 143 | ** Flat code segment descriptor: |
| 144 | ** selector: 0x10 |
| 145 | ** base : 0x00000000 |
| 146 | ** limit : 0xFFFFFFFF |
| 147 | ** type : code, execute, read |
| 148 | ** |
| 149 | ** Flat data segment descriptor: |
| 150 | ** selector: 0x18 |
| 151 | ** base : 0x00000000 |
| 152 | ** limit : 0xFFFFFFFF |
| 153 | ** type : data, read/write |
| 154 | ** |
| 155 | ** Use TRAMPOLINE_ENTRY_LOC as a scratchpad. |
| 156 | */ |
| 157 | mov $TRAMPOLINE_ENTRY_LOC, %eax |
Ronald G. Minnich | eeb83b6 | 2018-07-18 07:19:30 -0700 | [diff] [blame] | 158 | movl $0x0000ffff, 16(%eax) // Set up the 2 new descriptors |
| 159 | movl $0x00cf9b00, 20(%eax) |
| 160 | movl $0x0000ffff, 24(%eax) |
| 161 | movl $0x00cf9300, 28(%eax) |
| 162 | movb $0x2b, 0(%eax) // Set the size |
| 163 | movl %eax, 2(%eax) // Set pointer to new GDT |
| 164 | lgdt (%eax) // Load it |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 165 | |
| 166 | /* finally: jump to kernel */ |
| 167 | mov $LINUX_PARAM_LOC, %esi |
| 168 | jmp *(LINUX_PARAM_LOC + LINUX_ENTRY_OFFSET) |
| 169 | |
| 170 | |
| 171 | 2: |
| 172 | hlt |
| 173 | jmp 2b |
Stefan Reinauer | 0316e1a | 2015-11-20 17:58:59 +0100 | [diff] [blame] | 174 | .trampoline_end: |