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(void) {
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 u32 bkup_ss, bkup_esp;
45 // Backup ss/esp / set esp to flat stack location
52 // Transition to 32bit mode, call func, return to 16bit
53 " pushl $(" __stringify(BUILD_BIOS_ADDR) " + 1f)\n"
58 " jmp transition16big\n"
65 : "=&r" (bkup_ss), "=&r" (bkup_esp)
67 : "eax", "ecx", "edx", "cc", "memory");
69 // Restore gdt and fs/gs
74 // Restore cmos index register
75 outb(cmosindex, PORT_CMOS_INDEX);
81 /****************************************************************
83 ****************************************************************/
85 // Switch to the extra stack in ebda and call a function.
87 stack_hop(u32 eax, u32 edx, void *func)
90 u16 ebda_seg = get_ebda_seg(), bkup_ss;
93 // Backup current %ss/%esp values.
96 // Copy ebda seg to %ds/%ss and set %esp
102 // Restore segments and stack
106 : "+a" (eax), "+d" (edx), "+c" (func), "=&r" (bkup_ss), "=&r" (bkup_esp)
107 : "i" (EBDA_OFFSET_TOP_STACK), "r" (ebda_seg)
113 /****************************************************************
115 ****************************************************************/
117 #define THREADSTACKSIZE 4096
120 struct thread_info *next;
124 struct thread_info VAR16VISIBLE MainThread;
125 int VAR16VISIBLE CanPreempt;
130 MainThread.next = &MainThread;
131 MainThread.stackpos = NULL;
135 // Return the 'struct thread_info' for the currently running thread.
140 if (esp <= BUILD_STACK_ADDR)
142 return (void*)ALIGN_DOWN(esp, THREADSTACKSIZE);
145 // Switch to next thread stack.
147 switch_next(struct thread_info *cur)
149 struct thread_info *next = cur->next;
154 " pushl $1f\n" // store return pc
155 " pushl %%ebp\n" // backup %ebp
156 " movl %%esp, 4(%%eax)\n" // cur->stackpos = %esp
157 " movl 4(%%ecx), %%esp\n" // %esp = next->stackpos
158 " popl %%ebp\n" // restore %ebp
159 " retl\n" // restore pc
161 : "+a"(cur), "+c"(next)
163 : "ebx", "edx", "esi", "edi", "cc", "memory");
166 // Briefly permit irqs to occur.
170 if (MODESEGMENT || !CONFIG_THREADS) {
171 // Just directly check irqs.
175 struct thread_info *cur = getCurThread();
176 if (cur == &MainThread)
177 // Permit irqs to fire
180 // Switch to the next thread
184 // Last thing called from a thread (called on "next" stack).
186 __end_thread(struct thread_info *old)
188 struct thread_info *pos = &MainThread;
189 while (pos->next != old)
191 pos->next = old->next;
193 dprintf(DEBUG_thread, "\\%08x/ End thread\n", (u32)old);
196 // Create a new thread and start executing 'func' in it.
198 run_thread(void (*func)(void*), void *data)
201 if (! CONFIG_THREADS)
203 struct thread_info *thread;
204 thread = memalign_tmphigh(THREADSTACKSIZE, THREADSTACKSIZE);
208 thread->stackpos = (void*)thread + THREADSTACKSIZE;
209 struct thread_info *cur = getCurThread();
210 thread->next = cur->next;
213 dprintf(DEBUG_thread, "/%08x\\ Start thread\n", (u32)thread);
216 " pushl $1f\n" // store return pc
217 " pushl %%ebp\n" // backup %ebp
218 " movl %%esp, 4(%%edx)\n" // cur->stackpos = %esp
219 " movl 4(%%ebx), %%esp\n" // %esp = thread->stackpos
220 " calll *%%ecx\n" // Call func
223 " movl (%%ebx), %%ecx\n" // %ecx = thread->next
224 " movl 4(%%ecx), %%esp\n" // %esp = next->stackpos
225 " movl %%ebx, %%eax\n"
226 " calll %4\n" // call __end_thread(thread)
227 " popl %%ebp\n" // restore %ebp
228 " retl\n" // restore pc
230 : "+a"(data), "+c"(func), "+b"(thread), "+d"(cur)
231 : "m"(*(u8*)__end_thread)
232 : "esi", "edi", "cc", "memory");
239 // Wait for all threads (other than the main thread) to complete.
244 if (! CONFIG_THREADS)
246 while (MainThread.next != &MainThread)
251 mutex_lock(struct mutex_s *mutex)
254 if (! CONFIG_THREADS)
256 while (mutex->isLocked)
262 mutex_unlock(struct mutex_s *mutex)
265 if (! CONFIG_THREADS)
271 /****************************************************************
273 ****************************************************************/
275 static u32 PreemptCount;
277 // Turn on RTC irqs and arrange for them to check the 32bit threads.
281 if (! CONFIG_THREADS || ! CONFIG_THREAD_OPTIONROMS)
288 // Turn off RTC irqs / stop checking for thread execution.
292 if (! CONFIG_THREADS || ! CONFIG_THREAD_OPTIONROMS)
296 dprintf(1, "Done preempt - %d checks\n", PreemptCount);
299 // Check if preemption is on, and wait for it to complete if so.
303 if (MODESEGMENT || !CONFIG_THREADS || !CONFIG_THREAD_OPTIONROMS
311 extern void yield_preempt(void);
313 // Try to execute 32bit threads.
318 switch_next(&MainThread);
322 // 16bit code that checks if threads are pending and executes them if so.
326 if (! CONFIG_THREADS || ! CONFIG_THREAD_OPTIONROMS
327 || !GET_GLOBAL(CanPreempt)
328 || GET_GLOBAL(MainThread.next) == &MainThread)
331 call32(yield_preempt);