1 | /* |
---|
2 | **************************************************************************** |
---|
3 | * (C) 2005 - Grzegorz Milos - Intel Research Cambridge |
---|
4 | **************************************************************************** |
---|
5 | * |
---|
6 | * File: sched.c |
---|
7 | * Author: Grzegorz Milos |
---|
8 | * Changes: Robert Kaiser |
---|
9 | * |
---|
10 | * Date: Aug 2005 |
---|
11 | * |
---|
12 | * Environment: Xen Minimal OS |
---|
13 | * Description: simple scheduler for Mini-Os |
---|
14 | * |
---|
15 | * The scheduler is non-preemptive (cooperative), and schedules according |
---|
16 | * to Round Robin algorithm. |
---|
17 | * |
---|
18 | **************************************************************************** |
---|
19 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
20 | * of this software and associated documentation files (the "Software"), to |
---|
21 | * deal in the Software without restriction, including without limitation the |
---|
22 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
---|
23 | * sell copies of the Software, and to permit persons to whom the Software is |
---|
24 | * furnished to do so, subject to the following conditions: |
---|
25 | * |
---|
26 | * The above copyright notice and this permission notice shall be included in |
---|
27 | * all copies or substantial portions of the Software. |
---|
28 | * |
---|
29 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
32 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
33 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
34 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | * DEALINGS IN THE SOFTWARE. |
---|
36 | */ |
---|
37 | |
---|
38 | #include <os.h> |
---|
39 | #include <hypervisor.h> |
---|
40 | #include <time.h> |
---|
41 | #include <mm.h> |
---|
42 | #include <types.h> |
---|
43 | #include <lib.h> |
---|
44 | #include <xmalloc.h> |
---|
45 | #include <list.h> |
---|
46 | #include <sched.h> |
---|
47 | #include <semaphore.h> |
---|
48 | |
---|
49 | |
---|
50 | #ifdef SCHED_DEBUG |
---|
51 | #define DEBUG(_f, _a...) \ |
---|
52 | printk("MINI_OS(file=sched.c, line=%d) " _f "\n", __LINE__, ## _a) |
---|
53 | #else |
---|
54 | #define DEBUG(_f, _a...) ((void)0) |
---|
55 | #endif |
---|
56 | |
---|
57 | |
---|
58 | void dump_stack(struct thread *thread) |
---|
59 | { |
---|
60 | unsigned long *bottom = (unsigned long *)(thread->stack + 2*4*1024); |
---|
61 | unsigned long *pointer = (unsigned long *)thread->sp; |
---|
62 | int count; |
---|
63 | if(thread == current) |
---|
64 | { |
---|
65 | #ifdef __i386__ |
---|
66 | asm("movl %%esp,%0" |
---|
67 | : "=r"(pointer)); |
---|
68 | #else |
---|
69 | asm("movq %%rsp,%0" |
---|
70 | : "=r"(pointer)); |
---|
71 | #endif |
---|
72 | } |
---|
73 | printk("The stack for \"%s\"\n", thread->name); |
---|
74 | for(count = 0; count < 25 && pointer < bottom; count ++) |
---|
75 | { |
---|
76 | printk("[0x%lx] 0x%lx\n", pointer, *pointer); |
---|
77 | pointer++; |
---|
78 | } |
---|
79 | |
---|
80 | if(pointer < bottom) printk(" ... continues.\n"); |
---|
81 | } |
---|
82 | |
---|
83 | /* Gets run when a new thread is scheduled the first time ever, |
---|
84 | defined in x86_[32/64].S */ |
---|
85 | extern void thread_starter(void); |
---|
86 | |
---|
87 | /* Pushes the specified value onto the stack of the specified thread */ |
---|
88 | static void stack_push(struct thread *thread, unsigned long value) |
---|
89 | { |
---|
90 | thread->sp -= sizeof(unsigned long); |
---|
91 | *((unsigned long *)thread->sp) = value; |
---|
92 | } |
---|
93 | |
---|
94 | /* Architecture specific setup of thread creation */ |
---|
95 | struct thread* arch_create_thread(char *name, void (*function)(void *), |
---|
96 | void *data) |
---|
97 | { |
---|
98 | struct thread *thread; |
---|
99 | |
---|
100 | thread = xmalloc(struct thread); |
---|
101 | /* Allocate 2 pages for stack, stack will be 2pages aligned */ |
---|
102 | thread->stack = (char *)alloc_pages(1); |
---|
103 | thread->name = name; |
---|
104 | printk("Thread \"%s\": pointer: 0x%lx, stack: 0x%lx\n", name, thread, |
---|
105 | thread->stack); |
---|
106 | |
---|
107 | thread->sp = (unsigned long)thread->stack + 4096 * 2; |
---|
108 | /* Save pointer to the thread on the stack, used by current macro */ |
---|
109 | *((unsigned long *)thread->stack) = (unsigned long)thread; |
---|
110 | |
---|
111 | stack_push(thread, (unsigned long) function); |
---|
112 | stack_push(thread, (unsigned long) data); |
---|
113 | thread->ip = (unsigned long) thread_starter; |
---|
114 | return thread; |
---|
115 | } |
---|
116 | |
---|
117 | void run_idle_thread(void) |
---|
118 | { |
---|
119 | /* Switch stacks and run the thread */ |
---|
120 | #if defined(__i386__) |
---|
121 | __asm__ __volatile__("mov %0,%%esp\n\t" |
---|
122 | "push %1\n\t" |
---|
123 | "ret" |
---|
124 | :"=m" (idle_thread->sp) |
---|
125 | :"m" (idle_thread->ip)); |
---|
126 | #elif defined(__x86_64__) |
---|
127 | __asm__ __volatile__("mov %0,%%rsp\n\t" |
---|
128 | "push %1\n\t" |
---|
129 | "ret" |
---|
130 | :"=m" (idle_thread->sp) |
---|
131 | :"m" (idle_thread->ip)); |
---|
132 | #endif |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | |
---|