ia64/xen-unstable

view extras/mini-os/sched.c @ 9788:bdcc838b9a72

Add small memory warning message to domain configuration examples.

Signed-off-by: Daniel Stekloff <dsteklof@us.ibm.com>
author stekloff@dyn9047022152.beaverton.ibm.com
date Wed Apr 19 22:58:24 2006 +0100 (2006-04-19)
parents 8c21c8ea5fff
children f6507937cb7c
line source
1 /*
2 ****************************************************************************
3 * (C) 2005 - Grzegorz Milos - Intel Research Cambridge
4 ****************************************************************************
5 *
6 * File: sched.c
7 * Author: Grzegorz Milos
8 * Changes:
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 */
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>
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
58 #define RUNNABLE_FLAG 0x00000001
60 #define is_runnable(_thread) (_thread->flags & RUNNABLE_FLAG)
61 #define set_runnable(_thread) (_thread->flags |= RUNNABLE_FLAG)
62 #define clear_runnable(_thread) (_thread->flags &= ~RUNNABLE_FLAG)
65 struct thread *idle_thread = NULL;
66 LIST_HEAD(exited_threads);
68 void idle_thread_fn(void *unused);
70 void dump_stack(struct thread *thread)
71 {
72 unsigned long *bottom = (unsigned long *)thread->stack + 2048;
73 unsigned long *pointer = (unsigned long *)thread->eps;
74 int count;
75 printk("The stack for \"%s\"\n", thread->name);
76 for(count = 0; count < 15 && pointer < bottom; count ++)
77 {
78 printk("[0x%lx] 0x%lx\n", pointer, *pointer);
79 pointer++;
80 }
82 if(pointer < bottom) printk("Not the whole stack printed\n");
83 }
85 #ifdef __i386__
86 #define switch_threads(prev, next) do { \
87 unsigned long esi,edi; \
88 __asm__ __volatile__("pushfl\n\t" \
89 "pushl %%ebp\n\t" \
90 "movl %%esp,%0\n\t" /* save ESP */ \
91 "movl %4,%%esp\n\t" /* restore ESP */ \
92 "movl $1f,%1\n\t" /* save EIP */ \
93 "pushl %5\n\t" /* restore EIP */ \
94 "ret\n\t" \
95 "1:\t" \
96 "popl %%ebp\n\t" \
97 "popfl" \
98 :"=m" (prev->eps),"=m" (prev->eip), \
99 "=S" (esi),"=D" (edi) \
100 :"m" (next->eps),"m" (next->eip), \
101 "2" (prev), "d" (next)); \
102 } while (0)
103 #elif __x86_64__
104 /* FIXME */
105 #endif
107 void inline print_runqueue(void)
108 {
109 struct list_head *it;
110 struct thread *th;
111 list_for_each(it, &idle_thread->thread_list)
112 {
113 th = list_entry(it, struct thread, thread_list);
114 printk(" Thread \"%s\", runnable=%d\n", th->name, is_runnable(th));
115 }
116 printk("\n");
117 }
120 void schedule(void)
121 {
122 struct thread *prev, *next, *thread;
123 struct list_head *iterator;
124 unsigned long flags;
125 prev = current;
126 local_irq_save(flags);
127 list_for_each(iterator, &exited_threads)
128 {
129 thread = list_entry(iterator, struct thread, thread_list);
130 if(thread != prev)
131 {
132 list_del(&thread->thread_list);
133 free_pages(thread->stack, 1);
134 xfree(thread);
135 }
136 }
137 next = idle_thread;
138 /* Thread list needs to be protected */
139 list_for_each(iterator, &idle_thread->thread_list)
140 {
141 thread = list_entry(iterator, struct thread, thread_list);
142 if(is_runnable(thread))
143 {
144 next = thread;
145 /* Put this thread on the end of the list */
146 list_del(&thread->thread_list);
147 list_add_tail(&thread->thread_list, &idle_thread->thread_list);
148 break;
149 }
150 }
151 local_irq_restore(flags);
152 /* Interrupting the switch is equivalent to having the next thread
153 inturrupted at the return instruction. And therefore at safe point. */
154 /* The thread switching only works for i386 at the moment */
155 #ifdef __i386__
156 if(prev != next) switch_threads(prev, next);
157 #endif
158 }
162 void exit_thread(struct thread *thread)
163 {
164 unsigned long flags;
165 printk("Thread \"%s\" exited.\n", thread->name);
166 local_irq_save(flags);
167 /* Remove from the thread list */
168 list_del(&thread->thread_list);
169 clear_runnable(thread);
170 /* Put onto exited list */
171 list_add(&thread->thread_list, &exited_threads);
172 local_irq_restore(flags);
173 /* Schedule will free the resources */
174 schedule();
175 }
178 struct thread* create_thread(char *name, void (*function)(void *), void *data)
179 {
180 struct thread *thread;
181 unsigned long flags;
183 thread = xmalloc(struct thread);
184 /* Allocate 2 pages for stack, stack will be 2pages aligned */
185 thread->stack = (char *)alloc_pages(1);
186 thread->name = name;
187 printk("Thread \"%s\": pointer: 0x%lx, stack: 0x%lx\n", name, thread,
188 thread->stack);
190 thread->eps = (unsigned long)thread->stack + 4096 * 2 - 4;
191 /* Save pointer to the thread on the stack, used by current macro */
192 *((unsigned long *)thread->stack) = (unsigned long)thread;
193 *((unsigned long *)thread->eps) = (unsigned long)thread;
194 thread->eps -= 4;
195 *((unsigned long *)thread->eps) = (unsigned long)data;
197 /* No return address */
198 thread->eps -= 4;
199 *((unsigned long *)thread->eps) = (unsigned long)exit_thread;
201 thread->eip = (unsigned long)function;
203 /* Not runable, not exited */
204 thread->flags = 0;
205 set_runnable(thread);
207 local_irq_save(flags);
208 if(idle_thread != NULL) {
209 list_add_tail(&thread->thread_list, &idle_thread->thread_list);
210 } else if(function != idle_thread_fn)
211 {
212 printk("BUG: Not allowed to create thread before initialising scheduler.\n");
213 BUG();
214 }
215 local_irq_restore(flags);
217 return thread;
218 }
221 void block(struct thread *thread)
222 {
223 clear_runnable(thread);
224 }
226 void wake(struct thread *thread)
227 {
228 set_runnable(thread);
229 }
231 void idle_thread_fn(void *unused)
232 {
233 for(;;)
234 {
235 schedule();
236 block_domain(10000);
237 }
238 }
240 void run_idle_thread(void)
241 {
242 /* Switch stacks and run the thread */
243 __asm__ __volatile__("mov %0,%%esp\n\t"
244 "push %1\n\t"
245 "ret"
246 :"=m" (idle_thread->eps)
247 :"m" (idle_thread->eip));
248 }
252 DECLARE_MUTEX(mutex);
254 void th_f1(void *data)
255 {
256 struct timeval tv1, tv2;
258 for(;;)
259 {
260 down(&mutex);
261 printk("Thread \"%s\" got semaphore, runnable %d\n", current->name, is_runnable(current));
262 schedule();
263 printk("Thread \"%s\" releases the semaphore\n", current->name);
264 up(&mutex);
267 gettimeofday(&tv1);
268 for(;;)
269 {
270 gettimeofday(&tv2);
271 if(tv2.tv_sec - tv1.tv_sec > 2) break;
272 }
275 schedule();
276 }
277 }
279 void th_f2(void *data)
280 {
281 for(;;)
282 {
283 printk("Thread OTHER executing, data 0x%lx\n", data);
284 schedule();
285 }
286 }
290 void init_sched(void)
291 {
292 printk("Initialising scheduler, idle_thread %p\n", idle_thread);
294 idle_thread = create_thread("Idle", idle_thread_fn, NULL);
295 INIT_LIST_HEAD(&idle_thread->thread_list);
296 }