Export epoch time via proc_global_info (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 #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_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                                 proc_destroy(p);
185                                 printk("Killed p\n");
186                         panic("This is okay");
187
188                         envs[0] = kfs_proc_create(kfs_lookup_path("roslib_hello"));
189                         __proc_set_state(envs[0], PROC_RUNNABLE_S);
190                         proc_run(envs[0]);
191                         warn("DEPRECATED");
192                         break;
193                         #endif
194                 case 2:
195                         /*
196                         test_smp_call_functions();
197                         test_checklists();
198                         test_barrier();
199                         test_print_info();
200                         test_lapic_status_bit();
201                         test_ipi_sending();
202                         test_pit();
203                         */
204                 default:
205                         printd("Manager Progress: %d\n", progress);
206                         // delay if you want to test rescheduling an MCP that yielded
207                         //udelay(15000000);
208                         run_scheduler();
209         }
210         panic("If you see me, then you probably screwed up");
211         monitor(0);
212
213         /*
214         printk("Servicing syscalls from Core 0:\n\n");
215         while (1) {
216                 process_generic_syscalls(&envs[0], 1);
217                 cpu_relax();
218         }
219         */
220         return;
221 #endif
222 }
223
224 void manager_jenkins()
225 {
226         #ifdef CONFIG_KERNEL_TESTING
227                 printk("<-- BEGIN_KERNEL_TESTS -->\n");
228                 run_registered_ktest_suites();
229                 printk("<-- END_KERNEL_TESTS -->\n");
230         #endif
231
232         // Run userspace tests (from config specified path).
233         #ifdef CONFIG_USERSPACE_TESTING
234         if (strlen(CONFIG_USERSPACE_TESTING_SCRIPT) != 0) {
235                 char exec[] = "/bin/ash";
236                 char *p_argv[] = {exec, CONFIG_USERSPACE_TESTING_SCRIPT, 0};
237
238                 struct file *program = do_file_open(exec, O_READ, 0);
239                 struct proc *p = proc_create(program, p_argv, NULL);
240                 proc_wakeup(p);
241                 proc_decref(p); /* let go of the reference created in proc_create() */
242                 kref_put(&program->f_kref);
243                 run_scheduler();
244             // Need a way to wait for p to finish
245         } else {
246                 printk("No user-space launcher file specified.\n");
247         }
248         #endif
249         smp_idle();
250         assert(0);
251 }
252
253 void manager_klueska()
254 {
255         static struct proc *envs[256];
256         static volatile uint8_t progress = 0;
257
258         if (progress == 0) {
259                 progress++;
260                 panic("what do you want to do?");
261                 //envs[0] = kfs_proc_create(kfs_lookup_path("fillmeup"));
262                 __proc_set_state(envs[0], PROC_RUNNABLE_S);
263                 proc_run_s(envs[0]);
264                 warn("DEPRECATED");
265         }
266         run_scheduler();
267
268         panic("DON'T PANIC");
269 }
270
271 void manager_waterman()
272 {
273         static bool first = true;
274         if (first)
275                 mon_shell(0, 0, 0);
276         smp_idle();
277         assert(0);
278 }
279
280 void manager_yuzhu()
281 {
282
283         static uint8_t progress = 0;
284         static struct proc *p;
285
286         // for testing taking cores, check in case 1 for usage
287         uint32_t corelist[MAX_NUM_CORES];
288         uint32_t num = 3;
289
290         //create_server(init_num_cores, loop);
291
292         monitor(0);
293
294         // quick_proc_run("hello", p);
295
296 }