1 // Code for manipulating stack locations.
3 // Copyright (C) 2009-2010 Kevin O'Connor <kevin@koconnor.net>
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
7 #include "biosvar.h" // get_ebda_seg
8 #include "util.h" // dprintf
9 #include "bregs.h" // CR0_PE
11 // Thread info - stored at bottom of each thread stack - don't change
12 // without also updating the inline assembler below.
14 struct thread_info *next;
16 struct thread_info **pprev;
18 struct thread_info VAR32FLATVISIBLE MainThread = {
19 &MainThread, NULL, &MainThread.next
23 /****************************************************************
25 ****************************************************************/
27 static inline u32 getcr0(void) {
29 asm("movl %%cr0, %0" : "=r"(cr0));
32 static inline void sgdt(struct descloc_s *desc) {
33 asm("sgdtl %0" : "=m"(*desc));
35 static inline void lgdt(struct descloc_s *desc) {
36 asm("lgdtl %0" : : "m"(*desc) : "memory");
39 // Call a 32bit SeaBIOS function from a 16bit SeaBIOS function.
46 // Called in 16bit protected mode?!
49 // Backup cmos index register and disable nmi
50 u8 cmosindex = inb(PORT_CMOS_INDEX);
51 outb(cmosindex | NMI_DISABLE_BIT, PORT_CMOS_INDEX);
54 // Backup fs/gs and gdt
55 u16 fs = GET_SEG(FS), gs = GET_SEG(GS);
59 u32 bkup_ss, bkup_esp;
61 // Backup ss/esp / set esp to flat stack location
68 // Transition to 32bit mode, call func, return to 16bit
69 " pushl $(" __stringify(BUILD_BIOS_ADDR) " + 1f)\n"
74 " jmp transition16big\n"
81 : "=&r" (bkup_ss), "=&r" (bkup_esp)
83 : "eax", "ecx", "edx", "cc", "memory");
85 // Restore gdt and fs/gs
90 // Restore cmos index register
91 outb(cmosindex, PORT_CMOS_INDEX);
96 // 16bit trampoline for enabling irqs from 32bit mode.
98 " .global trampoline_checkirqs\n"
99 "trampoline_checkirqs:\n"
117 extern void trampoline_checkirqs();
120 br.code.seg = SEG_BIOS;
121 br.code.offset = (u32)&trampoline_checkirqs;
125 // 16bit trampoline for waiting for an irq from 32bit mode.
127 " .global trampoline_waitirq\n"
128 "trampoline_waitirq:\n"
134 // Wait for next irq to occur.
139 asm volatile("sti ; hlt ; cli ; cld": : :"memory");
142 if (CONFIG_THREADS && MainThread.next != &MainThread) {
143 // Threads still active - do a yield instead.
147 extern void trampoline_waitirq();
150 br.code.seg = SEG_BIOS;
151 br.code.offset = (u32)&trampoline_waitirq;
156 /****************************************************************
158 ****************************************************************/
160 // Switch to the extra stack in ebda and call a function.
162 stack_hop(u32 eax, u32 edx, void *func)
165 u16 ebda_seg = get_ebda_seg(), bkup_ss;
168 // Backup current %ss/%esp values.
171 // Copy ebda seg to %ds/%ss and set %esp
177 // Restore segments and stack
181 : "+a" (eax), "+d" (edx), "+c" (func), "=&r" (bkup_ss), "=&r" (bkup_esp)
182 : "i" (EBDA_OFFSET_TOP_STACK), "r" (ebda_seg)
188 /****************************************************************
190 ****************************************************************/
192 #define THREADSTACKSIZE 4096
193 int VAR16VISIBLE CanPreempt;
195 // Return the 'struct thread_info' for the currently running thread.
200 if (esp <= BUILD_STACK_ADDR)
202 return (void*)ALIGN_DOWN(esp, THREADSTACKSIZE);
205 // Switch to next thread stack.
207 switch_next(struct thread_info *cur)
209 struct thread_info *next = cur->next;
214 " pushl $1f\n" // store return pc
215 " pushl %%ebp\n" // backup %ebp
216 " movl %%esp, 4(%%eax)\n" // cur->stackpos = %esp
217 " movl 4(%%ecx), %%esp\n" // %esp = next->stackpos
218 " popl %%ebp\n" // restore %ebp
219 " retl\n" // restore pc
221 : "+a"(cur), "+c"(next)
223 : "ebx", "edx", "esi", "edi", "cc", "memory");
226 // Briefly permit irqs to occur.
230 if (MODESEGMENT || !CONFIG_THREADS) {
231 // Just directly check irqs.
235 struct thread_info *cur = getCurThread();
236 if (cur == &MainThread)
237 // Permit irqs to fire
240 // Switch to the next thread
244 // Last thing called from a thread (called on "next" stack).
246 __end_thread(struct thread_info *old)
248 old->next->pprev = old->pprev;
249 *old->pprev = old->next;
251 dprintf(DEBUG_thread, "\\%08x/ End thread\n", (u32)old);
252 if (MainThread.next == &MainThread)
253 dprintf(1, "All threads complete.\n");
256 // Create a new thread and start executing 'func' in it.
258 run_thread(void (*func)(void*), void *data)
261 if (! CONFIG_THREADS)
263 struct thread_info *thread;
264 thread = memalign_tmphigh(THREADSTACKSIZE, THREADSTACKSIZE);
268 thread->stackpos = (void*)thread + THREADSTACKSIZE;
269 struct thread_info *cur = getCurThread();
271 thread->pprev = cur->pprev;
272 cur->pprev = &thread->next;
273 *thread->pprev = thread;
275 dprintf(DEBUG_thread, "/%08x\\ Start thread\n", (u32)thread);
278 " pushl $1f\n" // store return pc
279 " pushl %%ebp\n" // backup %ebp
280 " movl %%esp, 4(%%edx)\n" // cur->stackpos = %esp
281 " movl 4(%%ebx), %%esp\n" // %esp = thread->stackpos
282 " calll *%%ecx\n" // Call func
285 " movl (%%ebx), %%ecx\n" // %ecx = thread->next
286 " movl 4(%%ecx), %%esp\n" // %esp = next->stackpos
287 " movl %%ebx, %%eax\n"
288 " calll %4\n" // call __end_thread(thread)
289 " popl %%ebp\n" // restore %ebp
290 " retl\n" // restore pc
292 : "+a"(data), "+c"(func), "+b"(thread), "+d"(cur)
293 : "m"(*(u8*)__end_thread)
294 : "esi", "edi", "cc", "memory");
301 // Wait for all threads (other than the main thread) to complete.
306 if (! CONFIG_THREADS)
308 while (MainThread.next != &MainThread)
313 mutex_lock(struct mutex_s *mutex)
316 if (! CONFIG_THREADS)
318 while (mutex->isLocked)
324 mutex_unlock(struct mutex_s *mutex)
327 if (! CONFIG_THREADS)
333 /****************************************************************
335 ****************************************************************/
337 static u32 PreemptCount;
339 // Turn on RTC irqs and arrange for them to check the 32bit threads.
343 if (! CONFIG_THREADS || ! CONFIG_THREAD_OPTIONROMS)
350 // Turn off RTC irqs / stop checking for thread execution.
354 if (! CONFIG_THREADS || ! CONFIG_THREAD_OPTIONROMS) {
360 dprintf(9, "Done preempt - %d checks\n", PreemptCount);
364 // Check if preemption is on, and wait for it to complete if so.
368 if (MODESEGMENT || !CONFIG_THREADS || !CONFIG_THREAD_OPTIONROMS
376 extern void yield_preempt(void);
378 // Try to execute 32bit threads.
383 switch_next(&MainThread);
387 // 16bit code that checks if threads are pending and executes them if so.
391 if (! CONFIG_THREADS || ! CONFIG_THREAD_OPTIONROMS
392 || !GET_GLOBAL(CanPreempt)
393 || GET_FLATPTR(MainThread.next) == &MainThread)
396 call32(yield_preempt);