Removed the workqueue
[akaros.git] / kern / src / manager.c
1 /*
2  * Copyright (c) 2009 The Regents of the University of California
3  * Barret Rhoden <brho@cs.berkeley.edu>
4  * See LICENSE for details.
5  */
6
7
8 #ifdef __SHARC__
9 #pragma nosharc
10 #endif
11
12 #include <ros/common.h>
13 #include <smp.h>
14 #include <arch/init.h>
15 #include <mm.h>
16 #include <elf.h>
17 #include <frontend.h>
18
19 #include <kmalloc.h>
20 #include <assert.h>
21 #include <manager.h>
22 #include <process.h>
23 #include <schedule.h>
24 #include <syscall.h>
25 #include <testing.h>
26 #include <kfs.h>
27 #include <stdio.h>
28 #include <timing.h>
29 #include <resource.h>
30 #include <monitor.h>
31 #include <colored_caches.h>
32 #include <string.h>
33 #include <pmap.h>
34
35 /*
36  * Currently, if you leave this function by way of proc_run (process_workqueue
37  * that proc_runs), you will never come back to where you left off, and the
38  * function will start from the top.  Hence the hack 'progress'.
39  */
40 void manager(void)
41 {
42         #ifndef DEVELOPER_NAME
43                 #define DEVELOPER_NAME brho
44         #endif
45
46         // LoL
47         #define PASTE(s1,s2) s1 ## s2
48         #define MANAGER_FUNC(dev) PASTE(manager_,dev)
49
50         void MANAGER_FUNC(DEVELOPER_NAME)(void);
51         MANAGER_FUNC(DEVELOPER_NAME)();
52 }
53
54 void manager_brho(void)
55 {
56         static uint8_t RACY progress = 0;
57
58         static struct proc *envs[256];
59         static struct proc *p ;
60
61         // for testing taking cores, check in case 1 for usage
62         uint32_t corelist[MAX_NUM_CPUS];
63         uint32_t num = 3;
64
65         switch (progress++) {
66                 case 0:
67                         // TODO: need to store the pid for future manager runs, not the *p
68                         p = kfs_proc_create(kfs_lookup_path("mhello"));
69                         //p = kfs_proc_create(kfs_lookup_path("roslib_mhello"));
70                         //p = kfs_proc_create(kfs_lookup_path("roslib_mproctests"));
71                         //p = kfs_proc_create(kfs_lookup_path("roslib_spawn"));
72                         // being proper and all:
73                         spin_lock_irqsave(&p->proc_lock);
74                         __proc_set_state(p, PROC_RUNNABLE_S);
75                         // normal single-cored way
76                         spin_unlock_irqsave(&p->proc_lock);
77                         proc_run(p);
78                         proc_decref(p, 1);
79                         #if 0
80                         // this is how you can transition to a parallel process manually
81                         // make sure you don't proc run first
82                         __proc_set_state(p, PROC_RUNNING_S);
83                         __proc_set_state(p, PROC_RUNNABLE_M);
84                         p->resources[RES_CORES].amt_wanted = 5;
85                         spin_unlock_irqsave(&p->proc_lock);
86                         core_request(p);
87                         panic("This is okay");
88                         #endif
89                         break;
90                 case 1:
91                         monitor(0);
92                         #if 0
93                         udelay(10000000);
94                         // this is a ghetto way to test restarting an _M
95                                 printk("\nattempting to ghetto preempt...\n");
96                                 spin_lock_irqsave(&p->proc_lock);
97                                 proc_take_allcores(p, __death);
98                                 __proc_set_state(p, PROC_RUNNABLE_M);
99                                 spin_unlock_irqsave(&p->proc_lock);
100                                 udelay(5000000);
101                                 printk("\nattempting to restart...\n");
102                                 core_request(p); // proc still wants the cores
103                         panic("This is okay");
104                         // this tests taking some cores, and later killing an _M
105                                 printk("taking 3 cores from p\n");
106                                 for (int i = 0; i < num; i++)
107                                         corelist[i] = 7-i; // 7, 6, and 5
108                                 spin_lock_irqsave(&p->proc_lock);
109                                 proc_take_cores(p, corelist, &num, __death);
110                                 spin_unlock_irqsave(&p->proc_lock);
111                                 udelay(5000000);
112                                 printk("Killing p\n");
113                                 proc_destroy(p);
114                                 printk("Killed p\n");
115                         panic("This is okay");
116
117                         envs[0] = kfs_proc_create(kfs_lookup_path("roslib_hello"));
118                         __proc_set_state(envs[0], PROC_RUNNABLE_S);
119                         proc_run(envs[0]);
120                         break;
121                         #endif
122                 case 2:
123                         /*
124                         test_smp_call_functions();
125                         test_checklists();
126                         test_barrier();
127                         test_print_info();
128                         test_lapic_status_bit();
129                         test_ipi_sending();
130                         test_pit();
131                         */
132                 default:
133                         printk("Manager Progress: %d\n", progress);
134                         // delay if you want to test rescheduling an MCP that yielded
135                         //udelay(15000000);
136                         schedule();
137         }
138         panic("If you see me, then you probably screwed up");
139
140         /*
141         printk("Servicing syscalls from Core 0:\n\n");
142         while (1) {
143                 process_generic_syscalls(&envs[0], 1);
144                 cpu_relax();
145         }
146         */
147         return;
148 }
149
150 void manager_klueska()
151 {
152         static struct proc *envs[256];
153         static volatile uint8_t progress = 0;
154
155         if (progress == 0) {
156                 progress++;
157                 envs[0] = kfs_proc_create(kfs_lookup_path("parlib_matrix"));
158                 __proc_set_state(envs[0], PROC_RUNNABLE_S);
159                 proc_run(envs[0]);
160         }
161         schedule();
162
163         panic("DON'T PANIC");
164 }
165
166 struct elf_info
167 {
168         long entry;
169         long phdr;
170         int phnum;
171         int dynamic;
172         char interp[256];
173 };
174
175 void manager_waterman()
176 {
177         static int init = 0;
178         if(!init)
179         {
180                 init = 1;
181                 struct proc* p = proc_create(NULL,0);
182
183                 char* argv[] = {"/bin/sh","-l",0};
184                 char* envp[] = {"LD_LIBRARY_PATH=/lib",0};
185                 procinfo_pack_args(p->procinfo,argv,envp);
186
187                 struct file* f = file_open("/bin/busybox",0,0);
188                 assert(f != NULL);
189                 assert(load_elf(p,f) == 0);
190                 file_decref(f);
191
192                 __proc_set_state(p, PROC_RUNNABLE_S);
193                 proc_run(p);
194         }
195         schedule();
196 }
197
198 void manager_pearce()
199 {
200         static struct proc *envs[256];
201         static volatile uint8_t progress = 0;
202
203         if (progress == 0) {
204                 progress++;
205                 envs[0] = kfs_proc_create(kfs_lookup_path("parlib_httpserver"));
206                 //envs[0] = kfs_proc_create(kfs_lookup_path("parlib_lock_test"));
207                 __proc_set_state(envs[0], PROC_RUNNABLE_S);
208                 proc_run(envs[0]);
209         }
210         schedule();
211
212         panic("DON'T PANIC");
213
214 }
215
216
217 #ifdef __sparc_v8__
218
219 static char*
220 itoa(int num, char* buf0, size_t base)
221 {
222         if(base > 16)
223                 return NULL;
224
225         char* buf = buf0;
226         int len = 0, i;
227
228         if(num < 0)
229         {
230                 *buf++ = '-';
231                 num = -num;
232         }
233
234         do {
235                 buf[len++] = "0123456789abcdef"[num%base];
236                 num /= base;
237         } while(num);
238
239         for(i = 0; i < len/2; i++)
240         {
241                 char temp = buf[i];
242                 buf[i] = buf[len-i-1];
243                 buf[len-i-1] = temp;
244         }
245         buf[len] = 0;
246
247         return buf0;
248 }
249
250 void gsf_set_frame_cycles(int cycles)
251 {
252         store_alternate(26*4,2,cycles);
253 }
254
255 void gsf_set_partition_credits(int partition, int credits)
256 {
257         store_alternate((32+partition)*4,2,credits);
258 }
259
260 void gsf_set_core_partition(int core, int partition)
261 {
262         store_alternate((64+core)*4,2,partition);
263 }
264
265 #endif
266