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