blob: 726852804ff2d6e5eb90a5fcc2987b4821283bae [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
Arthur Heymans71971c92021-05-29 20:07:42 +020075/* e820 and LB_TAG_MEMORY type don't fully match: remap unknown type to 2, reserved memory */
76mov (LINUX_PARAM_LOC + E820_NR_OFFSET), %eax
77mov $(LINUX_PARAM_LOC + E820_OFFSET), %edi
78.test_e820_entry:
79cmp $0, %eax
80je .endScan
81cmp $12, 16(%edi) /* type */
82jng .next_e820_entry
83/* Fixup the type to 2, reserved memory */
84mov $2, 16(%edi)
85.next_e820_entry:
86dec %eax
87add $20, %edi
88jmp .test_e820_entry
Stefan Reinauer0316e1a2015-11-20 17:58:59 +010089
90.testFramebuffer:
91cmp $CB_TAG_FRAMEBUFFER, (%ebx)
92jne .endScan
93/* TODO: handle framebuffer tag */
94
95.endScan:
96add 4(%ebx), %ebx
97dec %ecx
98jnz .tableScan
99
100/* Setup basic code and data segment selectors for Linux
101**
102** Flat code segment descriptor:
103** selector: 0x10
104** base : 0x00000000
105** limit : 0xFFFFFFFF
106** type : code, execute, read
107**
108** Flat data segment descriptor:
109** selector: 0x18
110** base : 0x00000000
111** limit : 0xFFFFFFFF
112** type : data, read/write
113**
114** Use TRAMPOLINE_ENTRY_LOC as a scratchpad.
115*/
116mov $TRAMPOLINE_ENTRY_LOC, %eax
Ronald G. Minnicheeb83b62018-07-18 07:19:30 -0700117movl $0x0000ffff, 16(%eax) // Set up the 2 new descriptors
118movl $0x00cf9b00, 20(%eax)
119movl $0x0000ffff, 24(%eax)
120movl $0x00cf9300, 28(%eax)
121movb $0x2b, 0(%eax) // Set the size
122movl %eax, 2(%eax) // Set pointer to new GDT
123lgdt (%eax) // Load it
Stefan Reinauer0316e1a2015-11-20 17:58:59 +0100124
125/* finally: jump to kernel */
126mov $LINUX_PARAM_LOC, %esi
127jmp *(LINUX_PARAM_LOC + LINUX_ENTRY_OFFSET)
128
129
1302:
131hlt
132jmp 2b
Stefan Reinauer0316e1a2015-11-20 17:58:59 +0100133.trampoline_end: