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