Sanitize vcoreid from untrusted sources
[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
29 /*
30  * Currently, if you leave this function by way of proc_run (process_workqueue
31  * that proc_runs), you will never come back to where you left off, and the
32  * function will start from the top.  Hence the hack 'progress'.
33  */
34 void manager(void)
35 {
36         // LoL
37         #define MANAGER_FUNC(dev) PASTE(manager_,dev)
38
39         #if !defined(DEVELOPER_NAME) && \
40             (defined(CONFIG_KERNEL_TESTING) || \
41              defined(CONFIG_USERSPACE_TESTING))
42                 #define DEVELOPER_NAME jenkins
43         #endif
44
45         #ifndef DEVELOPER_NAME
46                 #define DEVELOPER_NAME brho
47         #endif
48
49         void MANAGER_FUNC(DEVELOPER_NAME)(void);
50         MANAGER_FUNC(DEVELOPER_NAME)();
51 }
52
53 char *p_argv[] = {0, 0, 0};
54 /* Helper macro for quickly running a process.  Pass it a string, *file, and a
55  * *proc. */
56 #define quick_proc_run(x, p, f)                                              \
57         (f) = do_file_open((x), O_READ, 0);                                  \
58         assert((f));                                                         \
59         p_argv[0] = file_name((f));                                          \
60         (p) = proc_create((f), p_argv, NULL);                                \
61         kref_put(&(f)->f_kref);                                              \
62         spin_lock(&(p)->proc_lock);                                          \
63         __proc_set_state((p), PROC_RUNNABLE_S);                              \
64         spin_unlock(&(p)->proc_lock);                                        \
65         proc_run_s((p));                                                     \
66         proc_decref((p));
67
68 #define quick_proc_create(x, p, f)                                           \
69         (f) = do_file_open((x), O_READ, 0);                                  \
70         assert((f));                                                         \
71         p_argv[0] = file_name((f));                                          \
72         (p) = proc_create((f), p_argv, NULL);                                \
73         kref_put(&(f)->f_kref);                                              \
74         spin_lock(&(p)->proc_lock);                                          \
75         __proc_set_state((p), PROC_RUNNABLE_S);                              \
76         spin_unlock(&(p)->proc_lock);
77
78 #define quick_proc_color_run(x, p, c, f)                                     \
79         (f) = do_file_open((x), O_READ, 0);                                  \
80         assert((f));                                                         \
81         p_argv[0] = file_name((f));                                          \
82         (p) = proc_create((f), p_argv, NULL);                                \
83         kref_put(&(f)->f_kref);                                              \
84         spin_lock(&(p)->proc_lock);                                          \
85         __proc_set_state((p), PROC_RUNNABLE_S);                              \
86         spin_unlock(&(p)->proc_lock);                                        \
87         p->cache_colors_map = cache_colors_map_alloc();                      \
88         for (int i = 0; i < (c); i++)                                        \
89                 cache_color_alloc(llc_cache, p->cache_colors_map);           \
90         proc_run_s((p));                                                     \
91         proc_decref((p));
92
93 #define quick_proc_color_create(x, p, c, f)                                  \
94         (f) = do_file_open((x), O_READ, 0);                                  \
95         assert((f));                                                         \
96         p_argv[0] = file_name((f));                                          \
97         (p) = proc_create((f), p_argv, NULL);                                \
98         kref_put(&(f)->f_kref);                                              \
99         spin_lock(&(p)->proc_lock);                                          \
100         __proc_set_state((p), PROC_RUNNABLE_S);                              \
101         spin_unlock(&(p)->proc_lock);                                        \
102         p->cache_colors_map = cache_colors_map_alloc();                      \
103         for (int i = 0; i < (c); i++)                                        \
104                 cache_color_alloc(llc_cache, p->cache_colors_map);
105
106 void manager_brho(void)
107 {
108         static bool first = TRUE;
109         struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
110
111         if (first) {
112                 printk("*** IRQs must be enabled for input emergency codes ***\n");
113                 #ifdef CONFIG_X86
114                 printk("*** Hit ctrl-g to enter the monitor. ***\n");
115                 printk("*** Hit ctrl-q to force-enter the monitor. ***\n");
116                 printk("*** Hit ctrl-b for a backtrace of core 0 ***\n");
117                 #else
118                 printk("*** Hit ctrl-g to enter the monitor. ***\n");
119                 #warning "***** ctrl-g untested on riscv, check k/a/r/trap.c *****"
120                 #endif
121                 first = FALSE;
122         }
123         /* just idle, and deal with things via interrupts.  or via face. */
124         smp_idle();
125         /* whatever we do in the manager, keep in mind that we need to not do
126          * anything too soon (like make processes), since we'll drop in here
127          * during boot if the boot sequence required any I/O (like EXT2), and we
128          * need to PRKM() */
129         assert(0);
130
131 }
132
133 void manager_jenkins()
134 {
135         #ifdef CONFIG_KERNEL_TESTING
136                 printk("<-- BEGIN_KERNEL_TESTS -->\n");
137                 run_registered_ktest_suites();
138                 printk("<-- END_KERNEL_TESTS -->\n");
139         #endif
140
141         // Run userspace tests (from config specified path).
142         #ifdef CONFIG_USERSPACE_TESTING
143         if (strlen(CONFIG_USERSPACE_TESTING_SCRIPT) != 0) {
144                 char exec[] = "/bin/ash";
145                 char *p_argv[] = {exec, CONFIG_USERSPACE_TESTING_SCRIPT, 0};
146
147                 struct file *program = do_file_open(exec, O_READ, 0);
148                 struct proc *p = proc_create(program, p_argv, NULL);
149                 proc_wakeup(p);
150                 /* let go of the reference created in proc_create() */
151                 proc_decref(p);
152                 kref_put(&program->f_kref);
153                 run_scheduler();
154             // Need a way to wait for p to finish
155         } else {
156                 printk("No user-space launcher file specified.\n");
157         }
158         #endif
159         smp_idle();
160         assert(0);
161 }
162
163 void manager_klueska()
164 {
165         static struct proc *envs[256];
166         static volatile uint8_t progress = 0;
167
168         if (progress == 0) {
169                 progress++;
170                 panic("what do you want to do?");
171                 //envs[0] = kfs_proc_create(kfs_lookup_path("fillmeup"));
172                 __proc_set_state(envs[0], PROC_RUNNABLE_S);
173                 proc_run_s(envs[0]);
174                 warn("DEPRECATED");
175         }
176         run_scheduler();
177
178         panic("DON'T PANIC");
179 }
180
181 void manager_waterman()
182 {
183         static bool first = true;
184
185         if (first)
186                 mon_shell(0, 0, 0);
187         smp_idle();
188         assert(0);
189 }
190
191 void manager_yuzhu()
192 {
193
194         static uint8_t progress = 0;
195         static struct proc *p;
196
197         // for testing taking cores, check in case 1 for usage
198         uint32_t corelist[MAX_NUM_CORES];
199         uint32_t num = 3;
200
201         //create_server(init_num_cores, loop);
202
203         monitor(0);
204
205         // quick_proc_run("hello", p);
206
207 }