No longer require LD_LIBRARY_PATH to be set (XCC)
[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 <ktest.h>
26 #include <kfs.h>
27 #include <stdio.h>
28 #include <time.h>
29 #include <monitor.h>
30 #include <colored_caches.h>
31 #include <string.h>
32 #include <pmap.h>
33 #include <arch/console.h>
34 #include <time.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         // LoL
45         #define PASTE(s1,s2) s1 ## s2
46         #define MANAGER_FUNC(dev) PASTE(manager_,dev)
47
48         #if !defined(DEVELOPER_NAME) && \
49             (defined(CONFIG_KERNEL_POSTBOOT_TESTING) || \
50              defined(CONFIG_USERSPACE_TESTING))
51                 #define DEVELOPER_NAME jenkins
52         #endif
53
54         #ifndef DEVELOPER_NAME
55                 #define DEVELOPER_NAME brho
56         #endif
57
58         void MANAGER_FUNC(DEVELOPER_NAME)(void);
59         MANAGER_FUNC(DEVELOPER_NAME)();
60 }
61
62 char *p_argv[] = {0, 0, 0};
63 /* Helper macro for quickly running a process.  Pass it a string, *file, and a
64  * *proc. */
65 #define quick_proc_run(x, p, f)                                                  \
66         (f) = do_file_open((x), 0, 0);                                               \
67         assert((f));                                                                 \
68         p_argv[0] = file_name((f));                                                  \
69         (p) = proc_create((f), p_argv, NULL);                                        \
70         kref_put(&(f)->f_kref);                                                      \
71         spin_lock(&(p)->proc_lock);                                                  \
72         __proc_set_state((p), PROC_RUNNABLE_S);                                      \
73         spin_unlock(&(p)->proc_lock);                                                \
74         proc_run_s((p));                                                             \
75         proc_decref((p));
76
77 #define quick_proc_create(x, p, f)                                               \
78         (f) = do_file_open((x), 0, 0);                                               \
79         assert((f));                                                                 \
80         p_argv[0] = file_name((f));                                                  \
81         (p) = proc_create((f), p_argv, NULL);                                        \
82         kref_put(&(f)->f_kref);                                                      \
83         spin_lock(&(p)->proc_lock);                                                  \
84         __proc_set_state((p), PROC_RUNNABLE_S);                                      \
85         spin_unlock(&(p)->proc_lock);
86
87 #define quick_proc_color_run(x, p, c, f)                                         \
88         (f) = do_file_open((x), 0, 0);                                               \
89         assert((f));                                                                 \
90         p_argv[0] = file_name((f));                                                  \
91         (p) = proc_create((f), p_argv, NULL);                                        \
92         kref_put(&(f)->f_kref);                                                      \
93         spin_lock(&(p)->proc_lock);                                                  \
94         __proc_set_state((p), PROC_RUNNABLE_S);                                      \
95         spin_unlock(&(p)->proc_lock);                                                \
96         p->cache_colors_map = cache_colors_map_alloc();                              \
97         for (int i = 0; i < (c); i++)                                                \
98                 cache_color_alloc(llc_cache, p->cache_colors_map);                       \
99         proc_run_s((p));                                                             \
100         proc_decref((p));
101
102 #define quick_proc_color_create(x, p, c, f)                                      \
103         (f) = do_file_open((x), 0, 0);                                               \
104         assert((f));                                                                 \
105         p_argv[0] = file_name((f));                                                  \
106         (p) = proc_create((f), p_argv, NULL);                                        \
107         kref_put(&(f)->f_kref);                                                      \
108         spin_lock(&(p)->proc_lock);                                                  \
109         __proc_set_state((p), PROC_RUNNABLE_S);                                      \
110         spin_unlock(&(p)->proc_lock);                                                \
111         p->cache_colors_map = cache_colors_map_alloc();                              \
112         for (int i = 0; i < (c); i++)                                                \
113                 cache_color_alloc(llc_cache, p->cache_colors_map);
114
115 void manager_brho(void)
116 {
117         static bool first = TRUE;
118         struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
119
120         if (first) {    
121                 printk("*** IRQs must be enabled for input emergency codes ***\n");
122                 #ifdef CONFIG_X86
123                 printk("*** Hit ctrl-g to enter the monitor. ***\n");
124                 printk("*** Hit ctrl-q to force-enter the monitor. ***\n");
125                 printk("*** Hit ctrl-b for a backtrace of core 0 ***\n");
126                 #else
127                 printk("*** Hit ctrl-g to enter the monitor. ***\n");
128                 #warning "***** ctrl-g untested on riscv, check k/a/r/trap.c *****"
129                 #endif
130                 first = FALSE;
131         }
132         /* just idle, and deal with things via interrupts.  or via face. */
133         smp_idle();
134         /* whatever we do in the manager, keep in mind that we need to not do
135          * anything too soon (like make processes), since we'll drop in here during
136          * boot if the boot sequence required any I/O (like EXT2), and we need to
137          * PRKM() */
138         assert(0);
139
140 #if 0 /* ancient tests below: (keeping around til we ditch the manager) */
141         // for testing taking cores, check in case 1 for usage
142         uint32_t corelist[MAX_NUM_CPUS];
143         uint32_t num = 3;
144         struct file *temp_f;
145         static struct proc *p;
146
147         static uint8_t RACY progress = 0;       /* this will wrap around. */
148         switch (progress++) {
149                 case 0:
150                         printk("Top of the manager to ya!\n");
151                         /* 124 is half of the available boxboro colors (with the kernel
152                          * getting 8) */
153                         //quick_proc_color_run("msr_dumb_while", p, 124, temp_f);
154                         quick_proc_run("/bin/hello", p, temp_f);
155                         #if 0
156                         // this is how you can transition to a parallel process manually
157                         // make sure you don't proc run first
158                         __proc_set_state(p, PROC_RUNNING_S);
159                         __proc_set_state(p, PROC_RUNNABLE_M);
160                         p->resources[RES_CORES].amt_wanted = 5;
161                         spin_unlock(&p->proc_lock);
162                         core_request(p);
163                         panic("This is okay");
164                         #endif
165                         break;
166                 case 1:
167                         #if 0
168                         udelay(10000000);
169                         // this is a ghetto way to test restarting an _M
170                                 printk("\nattempting to ghetto preempt...\n");
171                                 spin_lock(&p->proc_lock);
172                                 proc_take_allcores(p, __death);
173                                 __proc_set_state(p, PROC_RUNNABLE_M);
174                                 spin_unlock(&p->proc_lock);
175                                 udelay(5000000);
176                                 printk("\nattempting to restart...\n");
177                                 core_request(p); // proc still wants the cores
178                         panic("This is okay");
179                         // this tests taking some cores, and later killing an _M
180                                 printk("taking 3 cores from p\n");
181                                 for (int i = 0; i < num; i++)
182                                         corelist[i] = 7-i; // 7, 6, and 5
183                                 spin_lock(&p->proc_lock);
184                                 proc_take_cores(p, corelist, &num, __death);
185                                 spin_unlock(&p->proc_lock);
186                                 udelay(5000000);
187                                 printk("Killing p\n");
188                                 enable_irq();
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                         run_scheduler();
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_jenkins()
230
231         #ifdef CONFIG_KERNEL_TESTING
232                 printk("<-- BEGIN_KERNEL_TESTS -->\n");
233                 run_registered_ktest_suites();
234                 printk("<-- END_KERNEL_TESTS -->\n");
235         #endif
236
237         // Run userspace tests (from config specified path).
238         #ifdef CONFIG_USERSPACE_TESTING
239         if (strlen(CONFIG_USERSPACE_TESTING_SCRIPT) != 0) {
240                 char exec[] = "/bin/ash";
241                 char *p_argv[] = {exec, CONFIG_USERSPACE_TESTING_SCRIPT, 0};
242
243                 struct file *program = do_file_open(exec, 0, 0);
244                 struct proc *p = proc_create(program, p_argv, NULL);
245                 proc_wakeup(p);
246                 proc_decref(p); /* let go of the reference created in proc_create() */
247                 kref_put(&program->f_kref);
248                 run_scheduler();
249             // Need a way to wait for p to finish
250         } else {
251                 printk("No user-space launcher file specified.\n");
252         }
253         #endif
254         smp_idle();
255         assert(0);
256 }
257
258 void manager_klueska()
259 {
260         static struct proc *envs[256];
261         static volatile uint8_t progress = 0;
262
263         if (progress == 0) {
264                 progress++;
265                 panic("what do you want to do?");
266                 //envs[0] = kfs_proc_create(kfs_lookup_path("fillmeup"));
267                 __proc_set_state(envs[0], PROC_RUNNABLE_S);
268                 proc_run_s(envs[0]);
269                 warn("DEPRECATED");
270         }
271         run_scheduler();
272
273         panic("DON'T PANIC");
274 }
275
276 void manager_waterman()
277 {
278         static bool first = true;
279         if (first)
280                 mon_bb(0, 0, 0);
281         smp_idle();
282         assert(0);
283 }
284
285 void manager_yuzhu()
286 {
287         
288         static uint8_t RACY progress = 0;
289         static struct proc *p;
290
291         // for testing taking cores, check in case 1 for usage
292         uint32_t corelist[MAX_NUM_CPUS];
293         uint32_t num = 3;
294
295         //create_server(init_num_cores, loop);
296
297         monitor(0);
298
299         // quick_proc_run("hello", p);
300
301 }