Idenfity devices by name, not by char [1/3]
[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 #include <ros/common.h>
9 #include <smp.h>
10 #include <arch/init.h>
11 #include <mm.h>
12 #include <elf.h>
13 #include <frontend.h>
14
15 #include <kmalloc.h>
16 #include <assert.h>
17 #include <manager.h>
18 #include <process.h>
19 #include <schedule.h>
20 #include <syscall.h>
21 #include <ktest.h>
22 #include <kfs.h>
23 #include <stdio.h>
24 #include <time.h>
25 #include <monitor.h>
26 #include <colored_caches.h>
27 #include <string.h>
28 #include <pmap.h>
29 #include <arch/console.h>
30 #include <time.h>
31 #include <ros/arch/membar.h>
32
33 /*
34  * Currently, if you leave this function by way of proc_run (process_workqueue
35  * that proc_runs), you will never come back to where you left off, and the
36  * function will start from the top.  Hence the hack 'progress'.
37  */
38 void manager(void)
39 {
40         // LoL
41         #define PASTE(s1,s2) s1 ## s2
42         #define MANAGER_FUNC(dev) PASTE(manager_,dev)
43
44         #if !defined(DEVELOPER_NAME) && \
45             (defined(CONFIG_KERNEL_POSTBOOT_TESTING) || \
46              defined(CONFIG_USERSPACE_TESTING))
47                 #define DEVELOPER_NAME jenkins
48         #endif
49
50         #ifndef DEVELOPER_NAME
51                 #define DEVELOPER_NAME brho
52         #endif
53
54         void MANAGER_FUNC(DEVELOPER_NAME)(void);
55         MANAGER_FUNC(DEVELOPER_NAME)();
56 }
57
58 char *p_argv[] = {0, 0, 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), O_READ, 0);                                          \
63         assert((f));                                                                 \
64         p_argv[0] = file_name((f));                                                  \
65         (p) = proc_create((f), p_argv, NULL);                                        \
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), O_READ, 0);                                          \
75         assert((f));                                                                 \
76         p_argv[0] = file_name((f));                                                  \
77         (p) = proc_create((f), p_argv, NULL);                                        \
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), O_READ, 0);                                          \
85         assert((f));                                                                 \
86         p_argv[0] = file_name((f));                                                  \
87         (p) = proc_create((f), p_argv, NULL);                                        \
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), O_READ, 0);                                          \
100         assert((f));                                                                 \
101         p_argv[0] = file_name((f));                                                  \
102         (p) = proc_create((f), p_argv, NULL);                                        \
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("*** IRQs must be enabled for input emergency codes ***\n");
118                 #ifdef CONFIG_X86
119                 printk("*** Hit ctrl-g to enter the monitor. ***\n");
120                 printk("*** Hit ctrl-q to force-enter the monitor. ***\n");
121                 printk("*** Hit ctrl-b for a backtrace of core 0 ***\n");
122                 #else
123                 printk("*** Hit ctrl-g to enter the monitor. ***\n");
124                 #warning "***** ctrl-g untested on riscv, check k/a/r/trap.c *****"
125                 #endif
126                 first = FALSE;
127         }
128         /* just idle, and deal with things via interrupts.  or via face. */
129         smp_idle();
130         /* whatever we do in the manager, keep in mind that we need to not do
131          * anything too soon (like make processes), since we'll drop in here during
132          * boot if the boot sequence required any I/O (like EXT2), and we need to
133          * PRKM() */
134         assert(0);
135
136 #if 0 /* ancient tests below: (keeping around til we ditch the manager) */
137         // for testing taking cores, check in case 1 for usage
138         uint32_t corelist[MAX_NUM_CORES];
139         uint32_t num = 3;
140         struct file *temp_f;
141         static struct proc *p;
142
143         static uint8_t RACY progress = 0;       /* this will wrap around. */
144         switch (progress++) {
145                 case 0:
146                         printk("Top of the manager to ya!\n");
147                         /* 124 is half of the available boxboro colors (with the kernel
148                          * getting 8) */
149                         //quick_proc_color_run("msr_dumb_while", p, 124, temp_f);
150                         quick_proc_run("/bin/hello", p, temp_f);
151                         #if 0
152                         // this is how you can transition to a parallel process manually
153                         // make sure you don't proc run first
154                         __proc_set_state(p, PROC_RUNNING_S);
155                         __proc_set_state(p, PROC_RUNNABLE_M);
156                         p->resources[RES_CORES].amt_wanted = 5;
157                         spin_unlock(&p->proc_lock);
158                         core_request(p);
159                         panic("This is okay");
160                         #endif
161                         break;
162                 case 1:
163                         #if 0
164                         udelay(10000000);
165                         // this is a ghetto way to test restarting an _M
166                                 printk("\nattempting to ghetto preempt...\n");
167                                 spin_lock(&p->proc_lock);
168                                 proc_take_allcores(p, __death);
169                                 __proc_set_state(p, PROC_RUNNABLE_M);
170                                 spin_unlock(&p->proc_lock);
171                                 udelay(5000000);
172                                 printk("\nattempting to restart...\n");
173                                 core_request(p); // proc still wants the cores
174                         panic("This is okay");
175                         // this tests taking some cores, and later killing an _M
176                                 printk("taking 3 cores from p\n");
177                                 for (int i = 0; i < num; i++)
178                                         corelist[i] = 7-i; // 7, 6, and 5
179                                 spin_lock(&p->proc_lock);
180                                 proc_take_cores(p, corelist, &num, __death);
181                                 spin_unlock(&p->proc_lock);
182                                 udelay(5000000);
183                                 printk("Killing p\n");
184                                 enable_irq();
185                                 proc_destroy(p);
186                                 printk("Killed p\n");
187                         panic("This is okay");
188
189                         envs[0] = kfs_proc_create(kfs_lookup_path("roslib_hello"));
190                         __proc_set_state(envs[0], PROC_RUNNABLE_S);
191                         proc_run(envs[0]);
192                         warn("DEPRECATED");
193                         break;
194                         #endif
195                 case 2:
196                         /*
197                         test_smp_call_functions();
198                         test_checklists();
199                         test_barrier();
200                         test_print_info();
201                         test_lapic_status_bit();
202                         test_ipi_sending();
203                         test_pit();
204                         */
205                 default:
206                         printd("Manager Progress: %d\n", progress);
207                         // delay if you want to test rescheduling an MCP that yielded
208                         //udelay(15000000);
209                         run_scheduler();
210         }
211         panic("If you see me, then you probably screwed up");
212         monitor(0);
213
214         /*
215         printk("Servicing syscalls from Core 0:\n\n");
216         while (1) {
217                 process_generic_syscalls(&envs[0], 1);
218                 cpu_relax();
219         }
220         */
221         return;
222 #endif
223 }
224
225 void manager_jenkins()
226
227         #ifdef CONFIG_KERNEL_TESTING
228                 printk("<-- BEGIN_KERNEL_TESTS -->\n");
229                 run_registered_ktest_suites();
230                 printk("<-- END_KERNEL_TESTS -->\n");
231         #endif
232
233         // Run userspace tests (from config specified path).
234         #ifdef CONFIG_USERSPACE_TESTING
235         if (strlen(CONFIG_USERSPACE_TESTING_SCRIPT) != 0) {
236                 char exec[] = "/bin/ash";
237                 char *p_argv[] = {exec, CONFIG_USERSPACE_TESTING_SCRIPT, 0};
238
239                 struct file *program = do_file_open(exec, O_READ, 0);
240                 struct proc *p = proc_create(program, p_argv, NULL);
241                 proc_wakeup(p);
242                 proc_decref(p); /* let go of the reference created in proc_create() */
243                 kref_put(&program->f_kref);
244                 run_scheduler();
245             // Need a way to wait for p to finish
246         } else {
247                 printk("No user-space launcher file specified.\n");
248         }
249         #endif
250         smp_idle();
251         assert(0);
252 }
253
254 void manager_klueska()
255 {
256         static struct proc *envs[256];
257         static volatile uint8_t progress = 0;
258
259         if (progress == 0) {
260                 progress++;
261                 panic("what do you want to do?");
262                 //envs[0] = kfs_proc_create(kfs_lookup_path("fillmeup"));
263                 __proc_set_state(envs[0], PROC_RUNNABLE_S);
264                 proc_run_s(envs[0]);
265                 warn("DEPRECATED");
266         }
267         run_scheduler();
268
269         panic("DON'T PANIC");
270 }
271
272 void manager_waterman()
273 {
274         static bool first = true;
275         if (first)
276                 mon_bb(0, 0, 0);
277         smp_idle();
278         assert(0);
279 }
280
281 void manager_yuzhu()
282 {
283         
284         static uint8_t progress = 0;
285         static struct proc *p;
286
287         // for testing taking cores, check in case 1 for usage
288         uint32_t corelist[MAX_NUM_CORES];
289         uint32_t num = 3;
290
291         //create_server(init_num_cores, loop);
292
293         monitor(0);
294
295         // quick_proc_run("hello", p);
296
297 }