Guts the old KFS!
[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 /* Helper macro for quickly running something out of KFS.  Pass it a string and
57  * a proc pointer. */
58 #define quick_proc_run(x, p)                                                     \
59         (p) = kfs_proc_create(kfs_lookup_path((x)));                                 \
60         spin_lock(&(p)->proc_lock);                                                  \
61         __proc_set_state((p), PROC_RUNNABLE_S);                                      \
62         spin_unlock(&(p)->proc_lock);                                                \
63         proc_run((p));                                                               \
64         proc_decref((p), 1);
65
66 #define quick_proc_create(x, p)                                                  \
67         (p) = kfs_proc_create(kfs_lookup_path((x)));                                 \
68         spin_lock(&(p)->proc_lock);                                                  \
69         __proc_set_state((p), PROC_RUNNABLE_S);                                      \
70         spin_unlock(&(p)->proc_lock);
71
72 #define quick_proc_color_run(x, p, c)                                            \
73         (p) = kfs_proc_create(kfs_lookup_path((x)));                                 \
74         spin_lock(&(p)->proc_lock);                                                  \
75         __proc_set_state((p), PROC_RUNNABLE_S);                                      \
76         spin_unlock(&(p)->proc_lock);                                                \
77         p->cache_colors_map = cache_colors_map_alloc();                              \
78         for (int i = 0; i < (c); i++)                                                \
79                 cache_color_alloc(llc_cache, p->cache_colors_map);                       \
80         proc_run((p));                                                               \
81         proc_decref((p), 1);
82
83 #define quick_proc_color_create(x, p, c)                                         \
84         (p) = kfs_proc_create(kfs_lookup_path((x)));                                 \
85         spin_lock(&(p)->proc_lock);                                                  \
86         __proc_set_state((p), PROC_RUNNABLE_S);                                      \
87         spin_unlock(&(p)->proc_lock);                                                \
88         p->cache_colors_map = cache_colors_map_alloc();                              \
89         for (int i = 0; i < (c); i++)                                                \
90                 cache_color_alloc(llc_cache, p->cache_colors_map);
91
92 void manager_brho(void)
93 {
94         static uint8_t RACY progress = 0;
95         static struct proc *p;
96
97         // for testing taking cores, check in case 1 for usage
98         uint32_t corelist[MAX_NUM_CPUS];
99         uint32_t num = 3;
100
101         switch (progress++) {
102                 case 0:
103                         monitor(0);
104                         /* 124 is half of the available boxboro colors (with the kernel
105                          * getting 8) */
106                         //quick_proc_color_run("msr_dumb_while", p, 124);
107                         //quick_proc_run("mhello", p);
108                         #if 0
109                         // this is how you can transition to a parallel process manually
110                         // make sure you don't proc run first
111                         __proc_set_state(p, PROC_RUNNING_S);
112                         __proc_set_state(p, PROC_RUNNABLE_M);
113                         p->resources[RES_CORES].amt_wanted = 5;
114                         spin_unlock(&p->proc_lock);
115                         core_request(p);
116                         panic("This is okay");
117                         #endif
118                         break;
119                 case 1:
120                         #if 0
121                         udelay(10000000);
122                         // this is a ghetto way to test restarting an _M
123                                 printk("\nattempting to ghetto preempt...\n");
124                                 spin_lock(&p->proc_lock);
125                                 proc_take_allcores(p, __death);
126                                 __proc_set_state(p, PROC_RUNNABLE_M);
127                                 spin_unlock(&p->proc_lock);
128                                 udelay(5000000);
129                                 printk("\nattempting to restart...\n");
130                                 core_request(p); // proc still wants the cores
131                         panic("This is okay");
132                         // this tests taking some cores, and later killing an _M
133                                 printk("taking 3 cores from p\n");
134                                 for (int i = 0; i < num; i++)
135                                         corelist[i] = 7-i; // 7, 6, and 5
136                                 spin_lock(&p->proc_lock);
137                                 proc_take_cores(p, corelist, &num, __death);
138                                 spin_unlock(&p->proc_lock);
139                                 udelay(5000000);
140                                 printk("Killing p\n");
141                                 proc_destroy(p);
142                                 printk("Killed p\n");
143                         panic("This is okay");
144
145                         envs[0] = kfs_proc_create(kfs_lookup_path("roslib_hello"));
146                         __proc_set_state(envs[0], PROC_RUNNABLE_S);
147                         proc_run(envs[0]);
148                         break;
149                         #endif
150                 case 2:
151                         /*
152                         test_smp_call_functions();
153                         test_checklists();
154                         test_barrier();
155                         test_print_info();
156                         test_lapic_status_bit();
157                         test_ipi_sending();
158                         test_pit();
159                         */
160                 default:
161                         printd("Manager Progress: %d\n", progress);
162                         // delay if you want to test rescheduling an MCP that yielded
163                         //udelay(15000000);
164                         schedule();
165         }
166         panic("If you see me, then you probably screwed up");
167         monitor(0);
168
169         /*
170         printk("Servicing syscalls from Core 0:\n\n");
171         while (1) {
172                 process_generic_syscalls(&envs[0], 1);
173                 cpu_relax();
174         }
175         */
176         return;
177 }
178
179 void manager_klueska()
180 {
181         static struct proc *envs[256];
182         static volatile uint8_t progress = 0;
183
184         if (progress == 0) {
185                 progress++;
186                 panic("what do you want to do?");
187                 //envs[0] = kfs_proc_create(kfs_lookup_path("fillmeup"));
188                 __proc_set_state(envs[0], PROC_RUNNABLE_S);
189                 proc_run(envs[0]);
190         }
191         schedule();
192
193         panic("DON'T PANIC");
194 }
195
196 struct elf_info
197 {
198         long entry;
199         long phdr;
200         int phnum;
201         int dynamic;
202         char interp[256];
203 };
204
205 void manager_waterman()
206 {
207         static int init = 0;
208         if(!init)
209         {
210                 init = 1;
211                 struct proc* p = proc_create(NULL,0);
212
213                 char* argv[] = {"/bin/sh","-l",0};
214                 char* envp[] = {"LD_LIBRARY_PATH=/lib",0};
215                 procinfo_pack_args(p->procinfo,argv,envp);
216
217                 struct file* f = file_open("/bin/busybox",0,0);
218                 assert(f != NULL);
219                 assert(load_elf(p,f) == 0);
220                 file_decref(f);
221
222                 __proc_set_state(p, PROC_RUNNABLE_S);
223                 proc_run(p);
224         }
225         schedule();
226 }
227
228 void manager_pearce()
229 {
230         static struct proc *envs[256];
231         static volatile uint8_t progress = 0;
232
233         if (progress == 0) {
234                 progress++;
235                 panic("what do you want to do?");
236                 //envs[0] = kfs_proc_create(kfs_lookup_path("parlib_httpserver_integrated"));
237                 //envs[0] = kfs_proc_create(kfs_lookup_path("parlib_lock_test"));
238                 __proc_set_state(envs[0], PROC_RUNNABLE_S);
239                 proc_run(envs[0]);
240         }
241         schedule();
242
243         panic("DON'T PANIC");
244
245 }
246
247 #ifdef __sparc_v8__
248
249 static char*
250 itoa(int num, char* buf0, size_t base)
251 {
252         if(base > 16)
253                 return NULL;
254
255         char* buf = buf0;
256         int len = 0, i;
257
258         if(num < 0)
259         {
260                 *buf++ = '-';
261                 num = -num;
262         }
263
264         do {
265                 buf[len++] = "0123456789abcdef"[num%base];
266                 num /= base;
267         } while(num);
268
269         for(i = 0; i < len/2; i++)
270         {
271                 char temp = buf[i];
272                 buf[i] = buf[len-i-1];
273                 buf[len-i-1] = temp;
274         }
275         buf[len] = 0;
276
277         return buf0;
278 }
279
280 void gsf_set_frame_cycles(int cycles)
281 {
282         store_alternate(26*4,2,cycles);
283 }
284
285 void gsf_set_partition_credits(int partition, int credits)
286 {
287         store_alternate((32+partition)*4,2,credits);
288 }
289
290 void gsf_set_core_partition(int core, int partition)
291 {
292         store_alternate((64+core)*4,2,partition);
293 }
294
295 #endif
296