blob: f1619cfb1793adb0b90be691b33b6dbe250514dc [file] [log] [blame]
Patrick Georgiea063cb2020-05-08 19:28:13 +02001/* linux_trampoline */
Patrick Georgi7333a112020-05-08 20:48:04 +02002/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer0316e1a2015-11-20 17:58:59 +01003
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
16
17#define E820_NR_OFFSET 0x1e8
18#define LINUX_ENTRY_OFFSET 0x214
19#define E820_OFFSET 0x2d0
20
21.trampoline_start:
Stefan Reinauer0316e1a2015-11-20 17:58:59 +010022cld
23xor %edx, %edx
24mov $0, %ecx
25
26.headerSearch:
27mov $0x10000, %ebx
28add %ecx, %ebx
29mov (%ecx), %eax
30cmp $HEADER_SIG, %eax
31je .headerSearchDone // found the header
32add $16, %ecx
33cmp %ecx, %ebx
34jne .headerSearch
35
36.headerSearchDone:
37cmp %ecx, %ebx // reached the end == not found anything?
38je 2f // give up
39
40// we assume the checksum is okay, no test
41mov 4(%ecx), %ebx
42add %ecx, %ebx // ebx = cb_header + header_bytes
43mov 20(%ecx), %ecx // ecx = table_entries
44
45.tableScan:
46cmp $CB_TAG_FORWARD, (%ebx)
47jne .testMemory
48
49/* forward tag: assume 32bit pointer */
50mov 8(%ebx), %ecx
51jmp .headerSearch
52
53.testMemory:
54cmp $CB_TAG_MEMORY, (%ebx)
55jne .testFramebuffer
56
57/* memory tag: copy e820 map and entry count. also determine alt_mem_k */
58mov 4(%ebx), %eax
59sub $8, %eax
60shr $2, %eax /* eax = number of dwords of e820 data */
61cmp $(32 * 5), %eax /* linux wants at most 32 entries of 5 dwords */
62jng 1f
63mov $(32 * 5), %eax /* only copy 32 entries */
641:
65mov %eax, %esi
66mov $5, %edi
67div %edi
68mov %eax, (LINUX_PARAM_LOC + E820_NR_OFFSET)
69mov %esi, %eax
70xchg %eax, %ecx
71lea 8(%ebx), %esi /* e820 data source */
72mov $(LINUX_PARAM_LOC + E820_OFFSET), %edi
73rep movsl
74xchg %eax, %ecx
75jmp .endScan
76
77.testFramebuffer:
78cmp $CB_TAG_FRAMEBUFFER, (%ebx)
79jne .endScan
80/* TODO: handle framebuffer tag */
81
82.endScan:
83add 4(%ebx), %ebx
84dec %ecx
85jnz .tableScan
86
87/* Setup basic code and data segment selectors for Linux
88**
89** Flat code segment descriptor:
90** selector: 0x10
91** base : 0x00000000
92** limit : 0xFFFFFFFF
93** type : code, execute, read
94**
95** Flat data segment descriptor:
96** selector: 0x18
97** base : 0x00000000
98** limit : 0xFFFFFFFF
99** type : data, read/write
100**
101** Use TRAMPOLINE_ENTRY_LOC as a scratchpad.
102*/
103mov $TRAMPOLINE_ENTRY_LOC, %eax
Ronald G. Minnicheeb83b62018-07-18 07:19:30 -0700104movl $0x0000ffff, 16(%eax) // Set up the 2 new descriptors
105movl $0x00cf9b00, 20(%eax)
106movl $0x0000ffff, 24(%eax)
107movl $0x00cf9300, 28(%eax)
108movb $0x2b, 0(%eax) // Set the size
109movl %eax, 2(%eax) // Set pointer to new GDT
110lgdt (%eax) // Load it
Stefan Reinauer0316e1a2015-11-20 17:58:59 +0100111
112/* finally: jump to kernel */
113mov $LINUX_PARAM_LOC, %esi
114jmp *(LINUX_PARAM_LOC + LINUX_ENTRY_OFFSET)
115
116
1172:
118hlt
119jmp 2b
Stefan Reinauer0316e1a2015-11-20 17:58:59 +0100120.trampoline_end: