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