blob: 3b4e730b33473db0be6e671ed76c2d12d73898f6 [file] [log] [blame]
Stefan Reinauer0316e1a2015-11-20 17:58:59 +01001/*
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 Reinauer0316e1a2015-11-20 17:58:59 +010034cld
35xor %edx, %edx
36mov $0, %ecx
37
38.headerSearch:
39mov $0x10000, %ebx
40add %ecx, %ebx
41mov (%ecx), %eax
42cmp $HEADER_SIG, %eax
43je .headerSearchDone // found the header
44add $16, %ecx
45cmp %ecx, %ebx
46jne .headerSearch
47
48.headerSearchDone:
49cmp %ecx, %ebx // reached the end == not found anything?
50je 2f // give up
51
52// we assume the checksum is okay, no test
53mov 4(%ecx), %ebx
54add %ecx, %ebx // ebx = cb_header + header_bytes
55mov 20(%ecx), %ecx // ecx = table_entries
56
57.tableScan:
58cmp $CB_TAG_FORWARD, (%ebx)
59jne .testMemory
60
61/* forward tag: assume 32bit pointer */
62mov 8(%ebx), %ecx
63jmp .headerSearch
64
65.testMemory:
66cmp $CB_TAG_MEMORY, (%ebx)
67jne .testFramebuffer
68
69/* memory tag: copy e820 map and entry count. also determine alt_mem_k */
70mov 4(%ebx), %eax
71sub $8, %eax
72shr $2, %eax /* eax = number of dwords of e820 data */
73cmp $(32 * 5), %eax /* linux wants at most 32 entries of 5 dwords */
74jng 1f
75mov $(32 * 5), %eax /* only copy 32 entries */
761:
77mov %eax, %esi
78mov $5, %edi
79div %edi
80mov %eax, (LINUX_PARAM_LOC + E820_NR_OFFSET)
81mov %esi, %eax
82xchg %eax, %ecx
83lea 8(%ebx), %esi /* e820 data source */
84mov $(LINUX_PARAM_LOC + E820_OFFSET), %edi
85rep movsl
86xchg %eax, %ecx
87jmp .endScan
88
89.testFramebuffer:
90cmp $CB_TAG_FRAMEBUFFER, (%ebx)
91jne .endScan
92/* TODO: handle framebuffer tag */
93
94.endScan:
95add 4(%ebx), %ebx
96dec %ecx
97jnz .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*/
115mov $TRAMPOLINE_ENTRY_LOC, %eax
Ronald G. Minnicheeb83b62018-07-18 07:19:30 -0700116movl $0x0000ffff, 16(%eax) // Set up the 2 new descriptors
117movl $0x00cf9b00, 20(%eax)
118movl $0x0000ffff, 24(%eax)
119movl $0x00cf9300, 28(%eax)
120movb $0x2b, 0(%eax) // Set the size
121movl %eax, 2(%eax) // Set pointer to new GDT
122lgdt (%eax) // Load it
Stefan Reinauer0316e1a2015-11-20 17:58:59 +0100123
124/* finally: jump to kernel */
125mov $LINUX_PARAM_LOC, %esi
126jmp *(LINUX_PARAM_LOC + LINUX_ENTRY_OFFSET)
127
128
1292:
130hlt
131jmp 2b
Stefan Reinauer0316e1a2015-11-20 17:58:59 +0100132.trampoline_end: