1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
// Code for manipulating stack locations.
//
// Copyright (C) 2009 Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU LGPLv3 license.
#include "biosvar.h" // get_ebda_seg
#include "util.h" // dprintf
#include "bregs.h" // CR0_PE
static inline u32 getcr0(void) {
u32 cr0;
asm("movl %%cr0, %0" : "=r"(cr0));
return cr0;
}
static inline void sgdt(struct descloc_s *desc) {
asm("sgdtl %0" : "=m"(*desc));
}
static inline void lgdt(struct descloc_s *desc) {
asm("lgdtl %0" : : "m"(*desc) : "memory");
}
// Call a 32bit SeaBIOS function from a 16bit SeaBIOS function.
static inline int
call32(void *func)
{
ASSERT16();
u32 cr0 = getcr0();
if (cr0 & CR0_PE)
// Called in 16bit protected mode?!
return -1;
// Backup cmos index register and disable nmi
u8 cmosindex = inb(PORT_CMOS_INDEX);
outb(cmosindex | NMI_DISABLE_BIT, PORT_CMOS_INDEX);
inb(PORT_CMOS_DATA);
// Backup fs/gs and gdt
u16 fs = GET_SEG(FS), gs = GET_SEG(GS);
struct descloc_s gdt;
sgdt(&gdt);
func -= BUILD_BIOS_ADDR;
u32 bkup_ss, bkup_esp;
asm volatile(
// Backup ss/esp / set esp to flat stack location
" movl %%ss, %0\n"
" movl %%esp, %1\n"
" shll $4, %0\n"
" addl %0, %%esp\n"
" movl %%ss, %0\n"
// Transition to 32bit mode, call func, return to 16bit
" pushl $(" __stringify(BUILD_BIOS_ADDR) " + 1f)\n"
" jmp transition32\n"
" .code32\n"
"1:calll %2\n"
" pushl $2f\n"
" jmp transition16big\n"
// Restore ds/ss/esp
" .code16gcc\n"
"2:movl %0, %%ds\n"
" movl %0, %%ss\n"
" movl %1, %%esp\n"
: "=&r" (bkup_ss), "=&r" (bkup_esp)
: "m" (*(u8*)func)
: "eax", "ecx", "edx", "cc", "memory");
// Restore gdt and fs/gs
lgdt(&gdt);
SET_SEG(FS, fs);
SET_SEG(GS, gs);
// Restore cmos index register
outb(cmosindex, PORT_CMOS_INDEX);
inb(PORT_CMOS_DATA);
return 0;
}
/****************************************************************
* Stack in EBDA
****************************************************************/
// Switch to the extra stack in ebda and call a function.
inline u32
stack_hop(u32 eax, u32 edx, u32 ecx, void *func)
{
ASSERT16();
u16 ebda_seg = get_ebda_seg(), bkup_ss;
u32 bkup_esp;
asm volatile(
// Backup current %ss/%esp values.
"movw %%ss, %w3\n"
"movl %%esp, %4\n"
// Copy ebda seg to %ds/%ss and set %esp
"movw %w6, %%ds\n"
"movw %w6, %%ss\n"
"movl %5, %%esp\n"
// Call func
"calll %7\n"
// Restore segments and stack
"movw %w3, %%ds\n"
"movw %w3, %%ss\n"
"movl %4, %%esp"
: "+a" (eax), "+d" (edx), "+c" (ecx), "=&r" (bkup_ss), "=&r" (bkup_esp)
: "i" (EBDA_OFFSET_TOP_STACK), "r" (ebda_seg), "m" (*(u8*)func)
: "cc", "memory");
return eax;
}
/****************************************************************
* Threads
****************************************************************/
#define THREADSTACKSIZE 4096
struct thread_info {
struct thread_info *next;
void *stackpos;
};
struct thread_info VAR16VISIBLE MainThread;
int VAR16VISIBLE CanPreempt;
void
thread_setup(void)
{
MainThread.next = &MainThread;
MainThread.stackpos = NULL;
CanPreempt = 0;
}
// Return the 'struct thread_info' for the currently running thread.
struct thread_info *
getCurThread(void)
{
u32 esp = getesp();
if (esp <= BUILD_STACK_ADDR)
return &MainThread;
return (void*)ALIGN_DOWN(esp, THREADSTACKSIZE);
}
// Switch to next thread stack.
static void
switch_next(struct thread_info *cur)
{
struct thread_info *next = cur->next;
if (cur == next)
// Nothing to do.
return;
asm volatile(
" pushl $1f\n" // store return pc
" pushl %%ebp\n" // backup %ebp
" movl %%esp, 4(%%eax)\n" // cur->stackpos = %esp
" movl 4(%%ecx), %%esp\n" // %esp = next->stackpos
" popl %%ebp\n" // restore %ebp
" retl\n" // restore pc
"1:\n"
: "+a"(cur), "+c"(next)
:
: "ebx", "edx", "esi", "edi", "cc", "memory");
}
// Briefly permit irqs to occur.
void
yield(void)
{
if (MODESEGMENT || !CONFIG_THREADS) {
// Just directly check irqs.
check_irqs();
return;
}
struct thread_info *cur = getCurThread();
if (cur == &MainThread)
// Permit irqs to fire
check_irqs();
// Switch to the next thread
switch_next(cur);
}
// Last thing called from a thread (called on "next" stack).
static void
__end_thread(struct thread_info *old)
{
struct thread_info *pos = &MainThread;
while (pos->next != old)
pos = pos->next;
pos->next = old->next;
free(old);
dprintf(DEBUG_thread, "\\%08x/ End thread\n", (u32)old);
}
// Create a new thread and start executing 'func' in it.
void
run_thread(void (*func)(void*), void *data)
{
ASSERT32FLAT();
if (! CONFIG_THREADS)
goto fail;
struct thread_info *thread;
thread = memalign_tmphigh(THREADSTACKSIZE, THREADSTACKSIZE);
if (!thread)
goto fail;
thread->stackpos = (void*)thread + THREADSTACKSIZE;
struct thread_info *cur = getCurThread();
thread->next = cur->next;
cur->next = thread;
dprintf(DEBUG_thread, "/%08x\\ Start thread\n", (u32)thread);
asm volatile(
// Start thread
" pushl $1f\n" // store return pc
" pushl %%ebp\n" // backup %ebp
" movl %%esp, 4(%%edx)\n" // cur->stackpos = %esp
" movl 4(%%ebx), %%esp\n" // %esp = thread->stackpos
" calll *%%ecx\n" // Call func
// End thread
" movl (%%ebx), %%ecx\n" // %ecx = thread->next
" movl 4(%%ecx), %%esp\n" // %esp = next->stackpos
" movl %%ebx, %%eax\n"
" calll %4\n" // call __end_thread(thread)
" popl %%ebp\n" // restore %ebp
" retl\n" // restore pc
"1:\n"
: "+a"(data), "+c"(func), "+b"(thread), "+d"(cur)
: "m"(*(u8*)__end_thread)
: "esi", "edi", "cc", "memory");
return;
fail:
func(data);
}
// Wait for all threads (other than the main thread) to complete.
void
wait_threads(void)
{
ASSERT32FLAT();
if (! CONFIG_THREADS)
return;
while (MainThread.next != &MainThread)
yield();
}
/****************************************************************
* Thread preemption
****************************************************************/
static u32 PreemptCount;
// Turn on RTC irqs and arrange for them to check the 32bit threads.
void
start_preempt(void)
{
if (! CONFIG_THREADS || ! CONFIG_THREAD_OPTIONROMS)
return;
CanPreempt = 1;
PreemptCount = 0;
useRTC();
}
// Turn off RTC irqs / stop checking for thread execution.
void
finish_preempt(void)
{
if (! CONFIG_THREADS || ! CONFIG_THREAD_OPTIONROMS)
return;
CanPreempt = 0;
releaseRTC();
dprintf(1, "Done preempt - %d checks\n", PreemptCount);
}
extern void yield_preempt(void);
#if MODESEGMENT == 0
// Try to execute 32bit threads.
void VISIBLE32FLAT
yield_preempt(void)
{
PreemptCount++;
switch_next(&MainThread);
}
#endif
// 16bit code that checks if threads are pending and executes them if so.
void
check_preempt(void)
{
if (! CONFIG_THREADS || ! CONFIG_THREAD_OPTIONROMS
|| !GET_GLOBAL(CanPreempt)
|| GET_GLOBAL(MainThread.next) == &MainThread)
return;
call32(yield_preempt);
}
|