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