blob: 649bb64b0e680723ea7cb6bf9013267e61a74c82 [file] [log] [blame]
Patrick Georgide36d332013-08-27 20:22:21 +02001/*
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 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
18 */
19
20#if 0
21/* NOTE: THIS CODE MUST REMAIN POSITION INDEPENDENT
22 * IT SHOULDN'T USE THE STACK
23 * AND IN GENERAL EXPECT NOTHING BUT RAM TO WORK
24 */
25.code32
26.data
Curt Brune3c12cb02014-08-29 10:43:36 -070027
28#include "linux_trampoline.h"
Patrick Georgide36d332013-08-27 20:22:21 +020029#define HEADER_SIG 0x4f49424c // LBIO little endian
30#define CB_TAG_FORWARD 0x11
31#define CB_TAG_MEMORY 0x1
32#define CB_TAG_FRAMEBUFFER 0x12
33
Patrick Georgide36d332013-08-27 20:22:21 +020034#define E820_NR_OFFSET 0x1e8
35#define LINUX_ENTRY_OFFSET 0x214
36#define E820_OFFSET 0x2d0
37
38.trampoline_start:
39
40cld
41xor %edx, %edx
42mov $0, %ecx
43
44.headerSearch:
45mov $0x10000, %ebx
46add %ecx, %ebx
47mov (%ecx), %eax
48cmp $HEADER_SIG, %eax
49je .headerSearchDone // found the header
50add $16, %ecx
51cmp %ecx, %ebx
52jne .headerSearch
53
54.headerSearchDone:
55cmp %ecx, %ebx // reached the end == not found anything?
56je 2f // give up
57
58// we assume the checksum is okay, no test
59mov 4(%ecx), %ebx
60add %ecx, %ebx // ebx = cb_header + header_bytes
61mov 20(%ecx), %ecx // ecx = table_entries
62
63.tableScan:
64cmp $CB_TAG_FORWARD, (%ebx)
65jne .testMemory
66
67/* forward tag: assume 32bit pointer */
68mov 8(%ebx), %ecx
69jmp .headerSearch
70
71.testMemory:
72cmp $CB_TAG_MEMORY, (%ebx)
73jne .testFramebuffer
74
75/* memory tag: copy e820 map and entry count. also determine alt_mem_k */
76mov 4(%ebx), %eax
77sub $8, %eax
78shr $2, %eax /* eax = number of dwords of e820 data */
79cmp $(32 * 5), %eax /* linux wants at most 32 entries of 5 dwords */
80jng 1f
81mov $(32 * 5), %eax /* only copy 32 entries */
821:
83mov %eax, %esi
84mov $5, %edi
85div %edi
86mov %eax, (LINUX_PARAM_LOC + E820_NR_OFFSET)
87mov %esi, %eax
88xchg %eax, %ecx
89lea 8(%ebx), %esi /* e820 data source */
90mov $(LINUX_PARAM_LOC + E820_OFFSET), %edi
91rep movsl
92xchg %eax, %ecx
93jmp .endScan
94
95.testFramebuffer:
96cmp $CB_TAG_FRAMEBUFFER, (%ebx)
97jne .endScan
98/* TODO: handle framebuffer tag */
99
100.endScan:
101add 4(%ebx), %ebx
102dec %ecx
103jnz .tableScan
104
Curt Brune3c12cb02014-08-29 10:43:36 -0700105/* Setup basic code and data segment selectors for Linux
106**
107** Flat code segment descriptor:
108** selector: 0x10
109** base : 0x00000000
110** limit : 0xFFFFFFFF
111** type : code, execute, read
112**
113** Flat data segment descriptor:
114** selector: 0x18
115** base : 0x00000000
116** limit : 0xFFFFFFFF
117** type : data, read/write
118**
119** Use TRAMPOLINE_ENTRY_LOC as a scratchpad.
120*/
121mov $TRAMPOLINE_ENTRY_LOC, %eax
122sgdt (%eax)
123mov 2(%eax), %ebx
124movl $0x0000ffff, 16(%ebx)
125movl $0x00cf9b00, 20(%ebx)
126movl $0x0000ffff, 24(%ebx)
127movl $0x00cf9300, 28(%ebx)
128
Patrick Georgide36d332013-08-27 20:22:21 +0200129/* finally: jump to kernel */
130mov $LINUX_PARAM_LOC, %esi
131jmp *(LINUX_PARAM_LOC + LINUX_ENTRY_OFFSET)
132
133
1342:
135hlt
136jmp 2b
137
138.trampoline_end:
139
140.global trampoline_start, trampoline_size
141trampoline_start:
142.long .trampoline_start
143trampoline_size:
144.long .trampoline_end - .trampoline_start
145#endif
146
147/* The code above is hand-crafted to fit various contraints.
148 * To simplify porting, the below matches the above.
149 * When changing any code in here, compile the above as a .S
150 * file, objcopy it to binary and paste the result below (minus
151 * the last 8 bytes which are trampoline_start and trampoline_size).
152 */
153const unsigned char trampoline[] = {
1540xfc, 0x31, 0xd2, 0xb9, 0x00, 0x00, 0x00, 0x00, 0xbb, 0x00, 0x00, 0x01, 0x00, 0x01, 0xcb, 0x8b,
1550x01, 0x3d, 0x4c, 0x42, 0x49, 0x4f, 0x74, 0x07, 0x83, 0xc1, 0x10, 0x39, 0xcb, 0x75, 0xe9, 0x39,
Curt Brune3c12cb02014-08-29 10:43:36 -07001560xcb, 0x0f, 0x84, 0x85, 0x00, 0x00, 0x00, 0x8b, 0x59, 0x04, 0x01, 0xcb, 0x8b, 0x49, 0x14, 0x83,
1570x3b, 0x11, 0x75, 0x05, 0x8b, 0x4b, 0x08, 0xeb, 0xcf, 0x83, 0x3b, 0x01, 0x75, 0x33, 0x8b, 0x43,
1580x04, 0x83, 0xe8, 0x08, 0xc1, 0xe8, 0x02, 0x3d, 0xa0, 0x00, 0x00, 0x00, 0x7e, 0x05, 0xb8, 0xa0,
1590x00, 0x00, 0x00, 0x89, 0xc6, 0xbf, 0x05, 0x00, 0x00, 0x00, 0xf7, 0xf7, 0xa3, 0xe8, 0x01, 0x09,
1600x00, 0x89, 0xf0, 0x91, 0x8d, 0x73, 0x08, 0xbf, 0xd0, 0x02, 0x09, 0x00, 0xf3, 0xa5, 0x91, 0xeb,
1610x05, 0x83, 0x3b, 0x12, 0x75, 0x00, 0x03, 0x5b, 0x04, 0x49, 0x75, 0xb3, 0xb8, 0x00, 0x00, 0x04,
1620x00, 0x0f, 0x01, 0x00, 0x8b, 0x58, 0x02, 0xc7, 0x43, 0x10, 0xff, 0xff, 0x00, 0x00, 0xc7, 0x43,
1630x14, 0x00, 0x9b, 0xcf, 0x00, 0xc7, 0x43, 0x18, 0xff, 0xff, 0x00, 0x00, 0xc7, 0x43, 0x1c, 0x00,
1640x93, 0xcf, 0x00, 0xbe, 0x00, 0x00, 0x09, 0x00, 0xff, 0x25, 0x14, 0x02, 0x09, 0x00, 0xf4, 0xeb,
1650xfd
Patrick Georgide36d332013-08-27 20:22:21 +0200166};
167
Alexandru Gagniucae45a982014-01-29 14:27:52 -0600168const void * const trampoline_start = &trampoline;
Patrick Georgide36d332013-08-27 20:22:21 +0200169const unsigned long trampoline_size = sizeof trampoline;