/* **************************************************************************** * (C) 2005 - Grzegorz Milos - Intel Research Cambridge **************************************************************************** * * File: sched.c * Author: Grzegorz Milos * Changes: * * Date: Aug 2005 * * Environment: Xen Minimal OS * Description: simple scheduler for Mini-Os * * The scheduler is non-preemptive (cooperative), and schedules according * to Round Robin algorithm. * **************************************************************************** * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to * deal in the Software without restriction, including without limitation the * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or * sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ #include #include #include #include #include #include #include #include #include #include #ifdef SCHED_DEBUG #define DEBUG(_f, _a...) \ printk("MINI_OS(file=sched.c, line=%d) " _f "\n", __LINE__, ## _a) #else #define DEBUG(_f, _a...) ((void)0) #endif #define RUNNABLE_FLAG 0x00000001 #define is_runnable(_thread) (_thread->flags & RUNNABLE_FLAG) #define set_runnable(_thread) (_thread->flags |= RUNNABLE_FLAG) #define clear_runnable(_thread) (_thread->flags &= ~RUNNABLE_FLAG) struct thread *idle_thread = NULL; LIST_HEAD(exited_threads); void idle_thread_fn(void *unused); void dump_stack(struct thread *thread) { unsigned long *bottom = (unsigned long *)(thread->stack + 2*4*1024); unsigned long *pointer = (unsigned long *)thread->sp; int count; if(thread == current) { #ifdef __i386__ asm("movl %%esp,%0" : "=r"(pointer)); #else asm("movq %%rsp,%0" : "=r"(pointer)); #endif } printk("The stack for \"%s\"\n", thread->name); for(count = 0; count < 25 && pointer < bottom; count ++) { printk("[0x%lx] 0x%lx\n", pointer, *pointer); pointer++; } if(pointer < bottom) printk(" ... continues.\n"); } #ifdef __i386__ #define switch_threads(prev, next) do { \ unsigned long esi,edi; \ __asm__ __volatile__("pushfl\n\t"

alising scheduler.\n"); BUG(); } local_irq_restore(flags); return thread; } void block(struct thread *thread) { clear_runnable(thread); } void wake(struct thread *thread) { set_runnable(thread); } void idle_thread_fn(void *unused) { for(;;) { schedule(); block_domain(10000); } } void run_idle_thread(void) { /* Switch stacks and run the thread */ #if defined(__i386__) __asm__ __volatile__("mov %0,%%esp\n\t" "push %1\n\t" "ret" :"=m" (idle_thread->sp) :"m" (idle_thread->ip)); #elif defined(__x86_64__) __asm__ __volatile__("mov %0,%%rsp\n\t" "push %1\n\t" "ret" :"=m" (idle_thread->sp) :"m" (idle_thread->ip)); #endif } DECLARE_MUTEX(mutex); void th_f1(void *data) { struct timeval tv1, tv2; for(;;) { down(&mutex); printk("Thread \"%s\" got semaphore, runnable %d\n", current->name, is_runnable(current)); schedule(); printk("Thread \"%s\" releases the semaphore\n", current->name); up(&mutex); gettimeofday(&tv1); for(;;) { gettimeofday(&tv2); if(tv2.tv_sec - tv1.tv_sec > 2) break; } schedule(); } } void th_f2(void *data) { for(;;) { printk("Thread OTHER executing, data 0x%lx\n", data); schedule(); } } void init_sched(void) { printk("Initialising scheduler\n"); idle_thread = create_thread("Idle", idle_thread_fn, NULL); INIT_LIST_HEAD(&idle_thread->thread_list); }