Adds chaninfo()
[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 <testing.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         #ifndef DEVELOPER_NAME
45                 #define DEVELOPER_NAME brho
46         #endif
47
48         // LoL
49         #define PASTE(s1,s2) s1 ## s2
50         #define MANAGER_FUNC(dev) PASTE(manager_,dev)
51
52         void MANAGER_FUNC(DEVELOPER_NAME)(void);
53         MANAGER_FUNC(DEVELOPER_NAME)();
54 }
55
56 char *p_argv[] = {0, 0, 0};
57 char *p_envp[] = {"LD_LIBRARY_PATH=/lib", 0};
58 /* Helper macro for quickly running a process.  Pass it a string, *file, and a
59  * *proc. */
60 #define quick_proc_run(x, p, f)                                                  \
61         (f) = do_file_open((x), 0, 0);                                               \
62         assert((f));                                                                 \
63         p_argv[0] = file_name((f));                                                  \
64         (p) = proc_create((f), p_argv, p_envp);                                      \
65         kref_put(&(f)->f_kref);                                                      \
66         spin_lock(&(p)->proc_lock);                                                  \
67         __proc_set_state((p), PROC_RUNNABLE_S);                                      \
68         spin_unlock(&(p)->proc_lock);                                                \
69         proc_run_s((p));                                                             \
70         proc_decref((p));
71
72 #define quick_proc_create(x, p, f)                                               \
73         (f) = do_file_open((x), 0, 0);                                               \
74         assert((f));                                                                 \
75         p_argv[0] = file_name((f));                                                  \
76         (p) = proc_create((f), p_argv, p_envp);                                      \
77         kref_put(&(f)->f_kref);                                                      \
78         spin_lock(&(p)->proc_lock);                                                  \
79         __proc_set_state((p), PROC_RUNNABLE_S);                                      \
80         spin_unlock(&(p)->proc_lock);
81
82 #define quick_proc_color_run(x, p, c, f)                                         \
83         (f) = do_file_open((x), 0, 0);                                               \
84         assert((f));                                                                 \
85         p_argv[0] = file_name((f));                                                  \
86         (p) = proc_create((f), p_argv, p_envp);                                      \
87         kref_put(&(f)->f_kref);                                                      \
88         spin_lock(&(p)->proc_lock);                                                  \
89         __proc_set_state((p), PROC_RUNNABLE_S);                                      \
90         spin_unlock(&(p)->proc_lock);                                                \
91         p->cache_colors_map = cache_colors_map_alloc();                              \
92         for (int i = 0; i < (c); i++)                                                \
93                 cache_color_alloc(llc_cache, p->cache_colors_map);                       \
94         proc_run_s((p));                                                             \
95         proc_decref((p));
96
97 #define quick_proc_color_create(x, p, c, f)                                      \
98         (f) = do_file_open((x), 0, 0);                                               \
99         assert((f));                                                                 \
100         p_argv[0] = file_name((f));                                                  \
101         (p) = proc_create((f), p_argv, p_envp);                                      \
102         kref_put(&(f)->f_kref);                                                      \
103         spin_lock(&(p)->proc_lock);                                                  \
104         __proc_set_state((p), PROC_RUNNABLE_S);                                      \
105         spin_unlock(&(p)->proc_lock);                                                \
106         p->cache_colors_map = cache_colors_map_alloc();                              \
107         for (int i = 0; i < (c); i++)                                                \
108                 cache_color_alloc(llc_cache, p->cache_colors_map);
109
110 void manager_brho(void)
111 {
112         static bool first = TRUE;
113         struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
114
115         if (first) {    
116                 printk("*** IRQs must be enabled for input emergency codes ***\n");
117                 #ifdef CONFIG_X86
118                 printk("*** Hit ctrl-g or shift-g to enter the monitor. ***\n");
119                 printk("*** Hit ctrl-q to force-enter the monitor. ***\n");
120                 printk("*** Hit ctrl-b for a backtrace of core 0 ***\n");
121                 #else
122                 printk("*** Hit shift-g to enter the monitor. ***\n");
123                 #endif
124                 first = FALSE;
125         }
126         /* just idle, and deal with things via interrupts.  or via face. */
127         smp_idle();
128         /* whatever we do in the manager, keep in mind that we need to not do
129          * anything too soon (like make processes), since we'll drop in here during
130          * boot if the boot sequence required any I/O (like EXT2), and we need to
131          * PRKM() */
132         assert(0);
133
134 #if 0 /* ancient tests below: (keeping around til we ditch the manager) */
135         // for testing taking cores, check in case 1 for usage
136         uint32_t corelist[MAX_NUM_CPUS];
137         uint32_t num = 3;
138         struct file *temp_f;
139         static struct proc *p;
140
141         static uint8_t RACY progress = 0;       /* this will wrap around. */
142         switch (progress++) {
143                 case 0:
144                         printk("Top of the manager to ya!\n");
145                         /* 124 is half of the available boxboro colors (with the kernel
146                          * getting 8) */
147                         //quick_proc_color_run("msr_dumb_while", p, 124, temp_f);
148                         quick_proc_run("/bin/hello", p, temp_f);
149                         #if 0
150                         // this is how you can transition to a parallel process manually
151                         // make sure you don't proc run first
152                         __proc_set_state(p, PROC_RUNNING_S);
153                         __proc_set_state(p, PROC_RUNNABLE_M);
154                         p->resources[RES_CORES].amt_wanted = 5;
155                         spin_unlock(&p->proc_lock);
156                         core_request(p);
157                         panic("This is okay");
158                         #endif
159                         break;
160                 case 1:
161                         #if 0
162                         udelay(10000000);
163                         // this is a ghetto way to test restarting an _M
164                                 printk("\nattempting to ghetto preempt...\n");
165                                 spin_lock(&p->proc_lock);
166                                 proc_take_allcores(p, __death);
167                                 __proc_set_state(p, PROC_RUNNABLE_M);
168                                 spin_unlock(&p->proc_lock);
169                                 udelay(5000000);
170                                 printk("\nattempting to restart...\n");
171                                 core_request(p); // proc still wants the cores
172                         panic("This is okay");
173                         // this tests taking some cores, and later killing an _M
174                                 printk("taking 3 cores from p\n");
175                                 for (int i = 0; i < num; i++)
176                                         corelist[i] = 7-i; // 7, 6, and 5
177                                 spin_lock(&p->proc_lock);
178                                 proc_take_cores(p, corelist, &num, __death);
179                                 spin_unlock(&p->proc_lock);
180                                 udelay(5000000);
181                                 printk("Killing p\n");
182                                 enable_irq();
183                                 proc_destroy(p);
184                                 printk("Killed p\n");
185                         panic("This is okay");
186
187                         envs[0] = kfs_proc_create(kfs_lookup_path("roslib_hello"));
188                         __proc_set_state(envs[0], PROC_RUNNABLE_S);
189                         proc_run(envs[0]);
190                         warn("DEPRECATED");
191                         break;
192                         #endif
193                 case 2:
194                         /*
195                         test_smp_call_functions();
196                         test_checklists();
197                         test_barrier();
198                         test_print_info();
199                         test_lapic_status_bit();
200                         test_ipi_sending();
201                         test_pit();
202                         */
203                 default:
204                         printd("Manager Progress: %d\n", progress);
205                         // delay if you want to test rescheduling an MCP that yielded
206                         //udelay(15000000);
207                         run_scheduler();
208         }
209         panic("If you see me, then you probably screwed up");
210         monitor(0);
211
212         /*
213         printk("Servicing syscalls from Core 0:\n\n");
214         while (1) {
215                 process_generic_syscalls(&envs[0], 1);
216                 cpu_relax();
217         }
218         */
219         return;
220 #endif
221 }
222
223 void manager_klueska()
224 {
225         static struct proc *envs[256];
226         static volatile uint8_t progress = 0;
227
228         if (progress == 0) {
229                 progress++;
230                 panic("what do you want to do?");
231                 //envs[0] = kfs_proc_create(kfs_lookup_path("fillmeup"));
232                 __proc_set_state(envs[0], PROC_RUNNABLE_S);
233                 proc_run_s(envs[0]);
234                 warn("DEPRECATED");
235         }
236         run_scheduler();
237
238         panic("DON'T PANIC");
239 }
240
241 void manager_waterman()
242 {
243         static bool first = true;
244         if (first)
245                 mon_bb(0, 0, 0);
246         smp_idle();
247         assert(0);
248 }
249
250 void manager_yuzhu()
251 {
252         
253         static uint8_t RACY progress = 0;
254         static struct proc *p;
255
256         // for testing taking cores, check in case 1 for usage
257         uint32_t corelist[MAX_NUM_CPUS];
258         uint32_t num = 3;
259
260         //create_server(init_num_cores, loop);
261
262         monitor(0);
263
264         // quick_proc_run("hello", p);
265
266 }