1 // Code for manipulating stack locations.
3 // Copyright (C) 2009 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 static inline u32 getcr0() {
13 asm("movl %%cr0, %0" : "=r"(cr0));
16 static inline void sgdt(struct descloc_s *desc) {
17 asm("sgdtl %0" : "=m"(*desc));
19 static inline void lgdt(struct descloc_s *desc) {
20 asm("lgdtl %0" : : "m"(*desc) : "memory");
23 // Call a 32bit SeaBIOS function from a 16bit SeaBIOS function.
30 // Called in 16bit protected mode?!
33 // Backup cmos index register and disable nmi
34 u8 cmosindex = inb(PORT_CMOS_INDEX);
35 outb(cmosindex | NMI_DISABLE_BIT, PORT_CMOS_INDEX);
38 // Backup fs/gs and gdt
39 u16 fs = GET_SEG(FS), gs = GET_SEG(GS);
43 func -= BUILD_BIOS_ADDR;
44 u32 bkup_ss, bkup_esp;
46 // Backup ss/esp / set esp to flat stack location
53 // Transition to 32bit mode, call func, return to 16bit
54 " pushl $(" __stringify(BUILD_BIOS_ADDR) " + 1f)\n"
59 " jmp transition16big\n"
66 : "=&r" (bkup_ss), "=&r" (bkup_esp)
68 : "eax", "ecx", "edx", "cc", "memory");
70 // Restore gdt and fs/gs
75 // Restore cmos index register
76 outb(cmosindex, PORT_CMOS_INDEX);
82 /****************************************************************
84 ****************************************************************/
86 // Switch to the extra stack in ebda and call a function.
88 stack_hop(u32 eax, u32 edx, u32 ecx, void *func)
91 u16 ebda_seg = get_ebda_seg(), bkup_ss;
94 // Backup current %ss/%esp values.
97 // Copy ebda seg to %ds/%ss and set %esp
103 // Restore segments and stack
107 : "+a" (eax), "+d" (edx), "+c" (ecx), "=&r" (bkup_ss), "=&r" (bkup_esp)
108 : "i" (EBDA_OFFSET_TOP_STACK), "r" (ebda_seg), "m" (*(u8*)func)
114 /****************************************************************
116 ****************************************************************/
118 #define THREADSTACKSIZE 4096
121 struct thread_info *next;
125 struct thread_info VAR16VISIBLE MainThread;
126 int VAR16VISIBLE CanPreempt;
131 MainThread.next = &MainThread;
132 MainThread.stackpos = NULL;
136 // Return the 'struct thread_info' for the currently running thread.
141 if (esp <= BUILD_STACK_ADDR)
143 return (void*)ALIGN_DOWN(esp, THREADSTACKSIZE);
146 // Switch to next thread stack.
148 switch_next(struct thread_info *cur)
150 struct thread_info *next = cur->next;
155 " pushl $1f\n" // store return pc
156 " pushl %%ebp\n" // backup %ebp
157 " movl %%esp, 4(%%eax)\n" // cur->stackpos = %esp
158 " movl 4(%%ecx), %%esp\n" // %esp = next->stackpos
159 " popl %%ebp\n" // restore %ebp
160 " retl\n" // restore pc
162 : "+a"(cur), "+c"(next)
164 : "ebx", "edx", "esi", "edi", "cc", "memory");
167 // Briefly permit irqs to occur.
171 if (MODE16 || !CONFIG_THREADS) {
172 // Just directly check irqs.
176 struct thread_info *cur = getCurThread();
177 if (cur == &MainThread)
178 // Permit irqs to fire
181 // Switch to the next thread
185 // Last thing called from a thread (called on "next" stack).
187 __end_thread(struct thread_info *old)
189 struct thread_info *pos = &MainThread;
190 while (pos->next != old)
192 pos->next = old->next;
194 dprintf(DEBUG_thread, "\\%08x/ End thread\n", (u32)old);
197 // Create a new thread and start executing 'func' in it.
199 run_thread(void (*func)(void*), void *data)
202 if (! CONFIG_THREADS)
204 struct thread_info *thread;
205 thread = memalign_tmphigh(THREADSTACKSIZE, THREADSTACKSIZE);
209 thread->stackpos = (void*)thread + THREADSTACKSIZE;
210 struct thread_info *cur = getCurThread();
211 thread->next = cur->next;
214 dprintf(DEBUG_thread, "/%08x\\ Start thread\n", (u32)thread);
217 " pushl $1f\n" // store return pc
218 " pushl %%ebp\n" // backup %ebp
219 " movl %%esp, 4(%%edx)\n" // cur->stackpos = %esp
220 " movl 4(%%ebx), %%esp\n" // %esp = thread->stackpos
221 " calll *%%ecx\n" // Call func
224 " movl (%%ebx), %%ecx\n" // %ecx = thread->next
225 " movl 4(%%ecx), %%esp\n" // %esp = next->stackpos
226 " movl %%ebx, %%eax\n"
227 " calll %4\n" // call __end_thread(thread)
228 " popl %%ebp\n" // restore %ebp
229 " retl\n" // restore pc
231 : "+a"(data), "+c"(func), "+b"(thread), "+d"(cur)
232 : "m"(*(u8*)__end_thread)
233 : "esi", "edi", "cc", "memory");
240 // Wait for all threads (other than the main thread) to complete.
245 if (! CONFIG_THREADS)
247 while (MainThread.next != &MainThread)
252 /****************************************************************
254 ****************************************************************/
256 static u32 PreemptCount;
258 // Turn on RTC irqs and arrange for them to check the 32bit threads.
262 if (! CONFIG_THREADS || ! CONFIG_THREAD_OPTIONROMS)
269 // Turn off RTC irqs / stop checking for thread execution.
273 if (! CONFIG_THREADS || ! CONFIG_THREAD_OPTIONROMS)
277 dprintf(1, "Done preempt - %d checks\n", PreemptCount);
280 extern void yield_preempt();
282 // Try to execute 32bit threads.
287 switch_next(&MainThread);
291 // 16bit code that checks if threads are pending and executes them if so.
295 if (! CONFIG_THREADS || ! CONFIG_THREAD_OPTIONROMS
296 || !GET_GLOBAL(CanPreempt)
297 || GET_GLOBAL(MainThread.next) == &MainThread)
300 call32(yield_preempt);