160129dafb7cef29d8605cd6c68f7e56824d7e13
[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 #include <ros/timer.h>
35 #include <ros/arch/membar.h>
36
37 /*
38  * Currently, if you leave this function by way of proc_run (process_workqueue
39  * that proc_runs), you will never come back to where you left off, and the
40  * function will start from the top.  Hence the hack 'progress'.
41  */
42 void manager(void)
43 {
44         #ifndef DEVELOPER_NAME
45                 #define DEVELOPER_NAME brho
46         #endif
47
48         // LoL
49         #define PASTE(s1,s2) s1 ## s2
50         #define MANAGER_FUNC(dev) PASTE(manager_,dev)
51
52         void MANAGER_FUNC(DEVELOPER_NAME)(void);
53         MANAGER_FUNC(DEVELOPER_NAME)();
54 }
55
56 char *p_argv[] = {0, 0, 0};
57 char *p_envp[] = {"LD_LIBRARY_PATH=/lib", 0};
58 /* Helper macro for quickly running a process.  Pass it a string, *file, and a
59  * *proc. */
60 #define quick_proc_run(x, p, f)                                                  \
61         (f) = do_file_open((x), 0, 0);                                               \
62         assert((f));                                                                 \
63         p_argv[0] = file_name((f));                                                  \
64         (p) = proc_create((f), p_argv, p_envp);                                      \
65         kref_put(&(f)->f_kref);                                                      \
66         spin_lock(&(p)->proc_lock);                                                  \
67         __proc_set_state((p), PROC_RUNNABLE_S);                                      \
68         spin_unlock(&(p)->proc_lock);                                                \
69         proc_run((p));                                                               \
70         kref_put(&(p)->kref);
71
72 #define quick_proc_create(x, p, f)                                               \
73         (f) = do_file_open((x), 0, 0);                                               \
74         assert((f));                                                                 \
75         p_argv[0] = file_name((f));                                                  \
76         (p) = proc_create((f), p_argv, p_envp);                                      \
77         kref_put(&(f)->f_kref);                                                      \
78         spin_lock(&(p)->proc_lock);                                                  \
79         __proc_set_state((p), PROC_RUNNABLE_S);                                      \
80         spin_unlock(&(p)->proc_lock);
81
82 #define quick_proc_color_run(x, p, c, f)                                         \
83         (f) = do_file_open((x), 0, 0);                                               \
84         assert((f));                                                                 \
85         p_argv[0] = file_name((f));                                                  \
86         (p) = proc_create((f), p_argv, p_envp);                                      \
87         kref_put(&(f)->f_kref);                                                      \
88         spin_lock(&(p)->proc_lock);                                                  \
89         __proc_set_state((p), PROC_RUNNABLE_S);                                      \
90         spin_unlock(&(p)->proc_lock);                                                \
91         p->cache_colors_map = cache_colors_map_alloc();                              \
92         for (int i = 0; i < (c); i++)                                                \
93                 cache_color_alloc(llc_cache, p->cache_colors_map);                       \
94         proc_run((p));                                                               \
95         kref_put(&(p)->kref);
96
97 #define quick_proc_color_create(x, p, c, f)                                      \
98         (f) = do_file_open((x), 0, 0);                                               \
99         assert((f));                                                                 \
100         p_argv[0] = file_name((f));                                                  \
101         (p) = proc_create((f), p_argv, p_envp);                                      \
102         kref_put(&(f)->f_kref);                                                      \
103         spin_lock(&(p)->proc_lock);                                                  \
104         __proc_set_state((p), PROC_RUNNABLE_S);                                      \
105         spin_unlock(&(p)->proc_lock);                                                \
106         p->cache_colors_map = cache_colors_map_alloc();                              \
107         for (int i = 0; i < (c); i++)                                                \
108                 cache_color_alloc(llc_cache, p->cache_colors_map);
109
110 void manager_brho(void)
111 {
112         static uint8_t RACY progress = 0;       /* this will wrap around. */
113         static struct proc *p;
114         struct file *temp_f;
115
116         /* I usually want this */
117         schedule();
118         printk("No work to do (schedule returned)\n");
119         monitor(0);
120
121         // for testing taking cores, check in case 1 for usage
122         uint32_t corelist[MAX_NUM_CPUS];
123         uint32_t num = 3;
124
125         switch (progress++) {
126                 case 0:
127                         printk("Top of the manager to ya!\n");
128                         /* 124 is half of the available boxboro colors (with the kernel
129                          * getting 8) */
130                         //quick_proc_color_run("msr_dumb_while", p, 124, temp_f);
131                         quick_proc_run("/bin/hello", p, temp_f);
132                         #if 0
133                         // this is how you can transition to a parallel process manually
134                         // make sure you don't proc run first
135                         __proc_set_state(p, PROC_RUNNING_S);
136                         __proc_set_state(p, PROC_RUNNABLE_M);
137                         p->resources[RES_CORES].amt_wanted = 5;
138                         spin_unlock(&p->proc_lock);
139                         core_request(p);
140                         panic("This is okay");
141                         #endif
142                         break;
143                 case 1:
144                         #if 0
145                         udelay(10000000);
146                         // this is a ghetto way to test restarting an _M
147                                 printk("\nattempting to ghetto preempt...\n");
148                                 spin_lock(&p->proc_lock);
149                                 proc_take_allcores(p, __death);
150                                 __proc_set_state(p, PROC_RUNNABLE_M);
151                                 spin_unlock(&p->proc_lock);
152                                 udelay(5000000);
153                                 printk("\nattempting to restart...\n");
154                                 core_request(p); // proc still wants the cores
155                         panic("This is okay");
156                         // this tests taking some cores, and later killing an _M
157                                 printk("taking 3 cores from p\n");
158                                 for (int i = 0; i < num; i++)
159                                         corelist[i] = 7-i; // 7, 6, and 5
160                                 spin_lock(&p->proc_lock);
161                                 proc_take_cores(p, corelist, &num, __death);
162                                 spin_unlock(&p->proc_lock);
163                                 udelay(5000000);
164                                 printk("Killing p\n");
165                                 proc_destroy(p);
166                                 printk("Killed p\n");
167                         panic("This is okay");
168
169                         envs[0] = kfs_proc_create(kfs_lookup_path("roslib_hello"));
170                         __proc_set_state(envs[0], PROC_RUNNABLE_S);
171                         proc_run(envs[0]);
172                         break;
173                         #endif
174                 case 2:
175                         /*
176                         test_smp_call_functions();
177                         test_checklists();
178                         test_barrier();
179                         test_print_info();
180                         test_lapic_status_bit();
181                         test_ipi_sending();
182                         test_pit();
183                         */
184                 default:
185                         printd("Manager Progress: %d\n", progress);
186                         // delay if you want to test rescheduling an MCP that yielded
187                         //udelay(15000000);
188                         schedule();
189         }
190         panic("If you see me, then you probably screwed up");
191         monitor(0);
192
193         /*
194         printk("Servicing syscalls from Core 0:\n\n");
195         while (1) {
196                 process_generic_syscalls(&envs[0], 1);
197                 cpu_relax();
198         }
199         */
200         return;
201 }
202
203 void manager_klueska()
204 {
205         static struct proc *envs[256];
206         static volatile uint8_t progress = 0;
207
208         if (progress == 0) {
209                 progress++;
210                 panic("what do you want to do?");
211                 //envs[0] = kfs_proc_create(kfs_lookup_path("fillmeup"));
212                 __proc_set_state(envs[0], PROC_RUNNABLE_S);
213                 proc_run(envs[0]);
214         }
215         schedule();
216
217         panic("DON'T PANIC");
218 }
219
220 struct elf_info
221 {
222         long entry;
223         long phdr;
224         int phnum;
225         int dynamic;
226         char interp[256];
227 };
228
229 void manager_waterman()
230 {
231         static int init = 0;
232         if(!init)
233         {
234                 warn("Old file creation might not work");
235                 init = 1;
236                 struct proc *p;
237                 proc_alloc(&p, 0);
238
239                 char* argv[] = {"/bin/sh","-l",0};
240                 char* envp[] = {"LD_LIBRARY_PATH=/lib",0};
241                 procinfo_pack_args(p->procinfo,argv,envp);
242
243                 struct file* f = file_open("/bin/busybox",0,0);
244                 assert(f != NULL);
245                 assert(load_elf(p,f) == 0);
246                 file_decref(f);
247
248                 __proc_set_state(p, PROC_RUNNABLE_S);
249                 proc_run(p);
250         }
251         schedule();
252 }
253
254 void manager_pearce()
255 {
256         static struct proc *envs[256];
257         static volatile uint8_t progress = 0;
258
259         if (progress == 0) {
260                 progress++;
261                 panic("what do you want to do?");
262                 //envs[0] = kfs_proc_create(kfs_lookup_path("parlib_httpserver_integrated"));
263                 //envs[0] = kfs_proc_create(kfs_lookup_path("parlib_lock_test"));
264                 __proc_set_state(envs[0], PROC_RUNNABLE_S);
265                 proc_run(envs[0]);
266         }
267         schedule();
268
269         panic("DON'T PANIC");
270
271 }
272
273 void manager_yuzhu()
274 {
275         
276         static uint8_t RACY progress = 0;
277         static struct proc *p;
278
279         // for testing taking cores, check in case 1 for usage
280         uint32_t corelist[MAX_NUM_CPUS];
281         uint32_t num = 3;
282
283         //create_server(init_num_cores, loop);
284
285         monitor(0);
286
287         // quick_proc_run("hello", p);
288
289 }
290
291 #ifdef __sparc_v8__
292
293 static char*
294 itoa(int num, char* buf0, size_t base)
295 {
296         if(base > 16)
297                 return NULL;
298
299         char* buf = buf0;
300         int len = 0, i;
301
302         if(num < 0)
303         {
304                 *buf++ = '-';
305                 num = -num;
306         }
307
308         do {
309                 buf[len++] = "0123456789abcdef"[num%base];
310                 num /= base;
311         } while(num);
312
313         for(i = 0; i < len/2; i++)
314         {
315                 char temp = buf[i];
316                 buf[i] = buf[len-i-1];
317                 buf[len-i-1] = temp;
318         }
319         buf[len] = 0;
320
321         return buf0;
322 }
323
324 void gsf_set_frame_cycles(int cycles)
325 {
326         store_alternate(26*4,2,cycles);
327 }
328
329 void gsf_set_partition_credits(int partition, int credits)
330 {
331         store_alternate((32+partition)*4,2,credits);
332 }
333
334 void gsf_set_core_partition(int core, int partition)
335 {
336         store_alternate((64+core)*4,2,partition);
337 }
338
339 #endif
340