VFS: mounts KFS, shell functions
[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                 envs[0] = kfs_proc_create(kfs_lookup_path("fillmeup"));
187                 __proc_set_state(envs[0], PROC_RUNNABLE_S);
188                 proc_run(envs[0]);
189         }
190         schedule();
191
192         panic("DON'T PANIC");
193 }
194
195 struct elf_info
196 {
197         long entry;
198         long phdr;
199         int phnum;
200         int dynamic;
201         char interp[256];
202 };
203
204 void manager_waterman()
205 {
206         static int init = 0;
207         if(!init)
208         {
209                 init = 1;
210                 struct proc* p = proc_create(NULL,0);
211
212                 char* argv[] = {"/bin/sh","-l",0};
213                 char* envp[] = {"LD_LIBRARY_PATH=/lib",0};
214                 procinfo_pack_args(p->procinfo,argv,envp);
215
216                 struct file* f = file_open("/bin/busybox",0,0);
217                 assert(f != NULL);
218                 assert(load_elf(p,f) == 0);
219                 file_decref(f);
220
221                 __proc_set_state(p, PROC_RUNNABLE_S);
222                 proc_run(p);
223         }
224         schedule();
225 }
226
227 void manager_pearce()
228 {
229         static struct proc *envs[256];
230         static volatile uint8_t progress = 0;
231
232         if (progress == 0) {
233                 progress++;
234                 envs[0] = kfs_proc_create(kfs_lookup_path("parlib_httpserver_integrated"));
235                 //envs[0] = kfs_proc_create(kfs_lookup_path("parlib_lock_test"));
236                 __proc_set_state(envs[0], PROC_RUNNABLE_S);
237                 proc_run(envs[0]);
238         }
239         schedule();
240
241         panic("DON'T PANIC");
242
243 }
244
245 #ifdef __sparc_v8__
246
247 static char*
248 itoa(int num, char* buf0, size_t base)
249 {
250         if(base > 16)
251                 return NULL;
252
253         char* buf = buf0;
254         int len = 0, i;
255
256         if(num < 0)
257         {
258                 *buf++ = '-';
259                 num = -num;
260         }
261
262         do {
263                 buf[len++] = "0123456789abcdef"[num%base];
264                 num /= base;
265         } while(num);
266
267         for(i = 0; i < len/2; i++)
268         {
269                 char temp = buf[i];
270                 buf[i] = buf[len-i-1];
271                 buf[len-i-1] = temp;
272         }
273         buf[len] = 0;
274
275         return buf0;
276 }
277
278 void gsf_set_frame_cycles(int cycles)
279 {
280         store_alternate(26*4,2,cycles);
281 }
282
283 void gsf_set_partition_credits(int partition, int credits)
284 {
285         store_alternate((32+partition)*4,2,credits);
286 }
287
288 void gsf_set_core_partition(int core, int partition)
289 {
290         store_alternate((64+core)*4,2,partition);
291 }
292
293 #endif
294