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