Scheduler is now selectable in make menuconfig in kernel hacking.
[projects/modsched/linux.git] / kernel / sched / modsched / framework / os / bertos / os.c
1 #include "../os.h"
2
3 #include <stdarg.h>
4
5 #include <cfg/debug.h>
6
7 #include <cpu/pgm.h>
8 #include <cpu/irq.h>
9 #include <cpu/types.h>
10 #include <cpu/frame.h>
11
12 #include <struct/heap.h>
13
14 #include <kern/proc.h>
15
16
17 HEAP_DEFINE_BUF(os_heap_buf, 1024);
18 Heap os_heap;
19
20 fw_task_t *current_task = NULL;
21
22 void os_set_current_task(fw_task_t *task){
23         current_task = task;
24 }
25
26 void *os_malloc(fw_size_t size){
27         return heap_malloc(&os_heap, (size_t) size);
28 }
29
30 void os_free(void* ptr)
31 {
32         heap_free(&os_heap, ptr);
33 }
34
35 void os_create_task(fw_task_t *task)
36 {
37         (void) task;
38 }
39
40 void os_dispatch(fw_task_t *task)
41 {
42         cpu_stack_t *dummy;
43
44         fw_task_t *old_task = current_task;
45         current_task = task;
46
47
48         /*
49          * If there is no old process, we save the old stack pointer into a
50          * dummy variable that we ignore.  In fact, this happens only when the
51          * old process has just exited.
52          */
53         //os_printf("switching %x (%x) and %x (%x)\n", ((Process*)(task->real_task))->stack, &((Process*)(task->real_task))->stack, ((Process*)(current_task->real_task))->stack, &((Process*)(current_task->real_task))->stack);
54
55
56         asm_switch_context(&((Process*)(current_task->real_task))->stack, old_task ? &((Process*)(old_task->real_task))->stack : &dummy);
57 }
58
59 void os_dispatch_mp(fw_task_t *task, int cpu)
60 {
61         (void) cpu;
62         os_dispatch(task);
63 }
64
65 int os_init_os(){
66         kprintf("Initializing for BeRTOS...");
67
68         heap_init(&os_heap, os_heap_buf, sizeof(os_heap_buf));
69
70         kprintf("done!\n");
71         return 0;
72 }
73
74 inline void os_spinlock(fw_spinlock_t *lock)
75 {
76         cpu_flags_t __flags; \
77         IRQ_SAVE_DISABLE(__flags); \
78         while(*lock){
79                 IRQ_RESTORE(__flags);
80                 CPU_IDLE;
81                 MEMORY_BARRIER;
82                 IRQ_SAVE_DISABLE(__flags);
83         }
84         *lock = 1;
85         IRQ_RESTORE(__flags);
86 }
87
88 inline void os_spinunlock(fw_spinlock_t *lock)
89 {
90         cpu_flags_t __flags; \
91         IRQ_SAVE_DISABLE(__flags); \
92         *lock = 0;
93         IRQ_RESTORE(__flags);
94 }
95
96 const char *os_task_info(fw_task_t *task){
97         return task->real_task ? proc_name(task->real_task) : "Invalid Task";
98 }