Removed CONFIG_OSDI and EXPER_TRADPROC
[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 <timing.h>
29 #include <resource.h>
30 #include <monitor.h>
31 #include <colored_caches.h>
32 #include <string.h>
33 #include <pmap.h>
34 #include <ros/timer.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 /* Helper macro for quickly running something out of KFS.  Pass it a string and
57  * a proc pointer. */
58 #define quick_proc_run(x, p)                                                     \
59         (p) = kfs_proc_create(kfs_lookup_path((x)));                                 \
60         spin_lock(&(p)->proc_lock);                                                  \
61         __proc_set_state((p), PROC_RUNNABLE_S);                                      \
62         spin_unlock(&(p)->proc_lock);                                                \
63         proc_run((p));                                                               \
64         proc_decref((p), 1);
65
66 #define quick_proc_create(x, p)                                                  \
67         (p) = kfs_proc_create(kfs_lookup_path((x)));                                 \
68         spin_lock(&(p)->proc_lock);                                                  \
69         __proc_set_state((p), PROC_RUNNABLE_S);                                      \
70         spin_unlock(&(p)->proc_lock);
71
72 #define quick_proc_color_run(x, p, c)                                            \
73         (p) = kfs_proc_create(kfs_lookup_path((x)));                                 \
74         spin_lock(&(p)->proc_lock);                                                  \
75         __proc_set_state((p), PROC_RUNNABLE_S);                                      \
76         spin_unlock(&(p)->proc_lock);                                                \
77         p->cache_colors_map = cache_colors_map_alloc();                              \
78         for (int i = 0; i < (c); i++)                                                \
79                 cache_color_alloc(llc_cache, p->cache_colors_map);                       \
80         proc_run((p));                                                               \
81         proc_decref((p), 1);
82
83 #define quick_proc_color_create(x, p, c)                                         \
84         (p) = kfs_proc_create(kfs_lookup_path((x)));                                 \
85         spin_lock(&(p)->proc_lock);                                                  \
86         __proc_set_state((p), PROC_RUNNABLE_S);                                      \
87         spin_unlock(&(p)->proc_lock);                                                \
88         p->cache_colors_map = cache_colors_map_alloc();                              \
89         for (int i = 0; i < (c); i++)                                                \
90                 cache_color_alloc(llc_cache, p->cache_colors_map);
91
92 void manager_brho(void)
93 {
94         static uint8_t RACY progress = 0;
95         static struct proc *p;
96
97         // for testing taking cores, check in case 1 for usage
98         uint32_t corelist[MAX_NUM_CPUS];
99         uint32_t num = 3;
100
101         switch (progress++) {
102                 case 0:
103                         /* 124 is half of the available boxboro colors (with the kernel
104                          * getting 8) */
105                         //quick_proc_color_run("msr_dumb_while", p, 124);
106                         quick_proc_run("mhello", p);
107                         #if 0
108                         // this is how you can transition to a parallel process manually
109                         // make sure you don't proc run first
110                         __proc_set_state(p, PROC_RUNNING_S);
111                         __proc_set_state(p, PROC_RUNNABLE_M);
112                         p->resources[RES_CORES].amt_wanted = 5;
113                         spin_unlock(&p->proc_lock);
114                         core_request(p);
115                         panic("This is okay");
116                         #endif
117                         break;
118                 case 1:
119                         #if 0
120                         udelay(10000000);
121                         // this is a ghetto way to test restarting an _M
122                                 printk("\nattempting to ghetto preempt...\n");
123                                 spin_lock(&p->proc_lock);
124                                 proc_take_allcores(p, __death);
125                                 __proc_set_state(p, PROC_RUNNABLE_M);
126                                 spin_unlock(&p->proc_lock);
127                                 udelay(5000000);
128                                 printk("\nattempting to restart...\n");
129                                 core_request(p); // proc still wants the cores
130                         panic("This is okay");
131                         // this tests taking some cores, and later killing an _M
132                                 printk("taking 3 cores from p\n");
133                                 for (int i = 0; i < num; i++)
134                                         corelist[i] = 7-i; // 7, 6, and 5
135                                 spin_lock(&p->proc_lock);
136                                 proc_take_cores(p, corelist, &num, __death);
137                                 spin_unlock(&p->proc_lock);
138                                 udelay(5000000);
139                                 printk("Killing p\n");
140                                 proc_destroy(p);
141                                 printk("Killed p\n");
142                         panic("This is okay");
143
144                         envs[0] = kfs_proc_create(kfs_lookup_path("roslib_hello"));
145                         __proc_set_state(envs[0], PROC_RUNNABLE_S);
146                         proc_run(envs[0]);
147                         break;
148                         #endif
149                 case 2:
150                         /*
151                         test_smp_call_functions();
152                         test_checklists();
153                         test_barrier();
154                         test_print_info();
155                         test_lapic_status_bit();
156                         test_ipi_sending();
157                         test_pit();
158                         */
159                 default:
160                         printd("Manager Progress: %d\n", progress);
161                         // delay if you want to test rescheduling an MCP that yielded
162                         //udelay(15000000);
163                         schedule();
164         }
165         panic("If you see me, then you probably screwed up");
166         monitor(0);
167
168         /*
169         printk("Servicing syscalls from Core 0:\n\n");
170         while (1) {
171                 process_generic_syscalls(&envs[0], 1);
172                 cpu_relax();
173         }
174         */
175         return;
176 }
177
178 void manager_klueska()
179 {
180         static struct proc *envs[256];
181         static volatile uint8_t progress = 0;
182
183         if (progress == 0) {
184                 progress++;
185                 envs[0] = kfs_proc_create(kfs_lookup_path("fillmeup"));
186                 __proc_set_state(envs[0], PROC_RUNNABLE_S);
187                 proc_run(envs[0]);
188         }
189         schedule();
190
191         panic("DON'T PANIC");
192 }
193
194 struct elf_info
195 {
196         long entry;
197         long phdr;
198         int phnum;
199         int dynamic;
200         char interp[256];
201 };
202
203 void manager_waterman()
204 {
205         static int init = 0;
206         if(!init)
207         {
208                 init = 1;
209                 struct proc* p = proc_create(NULL,0);
210
211                 char* argv[] = {"/bin/sh","-l",0};
212                 char* envp[] = {"LD_LIBRARY_PATH=/lib",0};
213                 procinfo_pack_args(p->procinfo,argv,envp);
214
215                 struct file* f = file_open("/bin/busybox",0,0);
216                 assert(f != NULL);
217                 assert(load_elf(p,f) == 0);
218                 file_decref(f);
219
220                 __proc_set_state(p, PROC_RUNNABLE_S);
221                 proc_run(p);
222         }
223         schedule();
224 }
225
226 void manager_pearce()
227 {
228         static struct proc *envs[256];
229         static volatile uint8_t progress = 0;
230
231         if (progress == 0) {
232                 progress++;
233                 envs[0] = kfs_proc_create(kfs_lookup_path("parlib_httpserver_integrated"));
234                 //envs[0] = kfs_proc_create(kfs_lookup_path("parlib_lock_test"));
235                 __proc_set_state(envs[0], PROC_RUNNABLE_S);
236                 proc_run(envs[0]);
237         }
238         schedule();
239
240         panic("DON'T PANIC");
241
242 }
243
244 #ifdef __sparc_v8__
245
246 static char*
247 itoa(int num, char* buf0, size_t base)
248 {
249         if(base > 16)
250                 return NULL;
251
252         char* buf = buf0;
253         int len = 0, i;
254
255         if(num < 0)
256         {
257                 *buf++ = '-';
258                 num = -num;
259         }
260
261         do {
262                 buf[len++] = "0123456789abcdef"[num%base];
263                 num /= base;
264         } while(num);
265
266         for(i = 0; i < len/2; i++)
267         {
268                 char temp = buf[i];
269                 buf[i] = buf[len-i-1];
270                 buf[len-i-1] = temp;
271         }
272         buf[len] = 0;
273
274         return buf0;
275 }
276
277 void gsf_set_frame_cycles(int cycles)
278 {
279         store_alternate(26*4,2,cycles);
280 }
281
282 void gsf_set_partition_credits(int partition, int credits)
283 {
284         store_alternate((32+partition)*4,2,credits);
285 }
286
287 void gsf_set_core_partition(int core, int partition)
288 {
289         store_alternate((64+core)*4,2,partition);
290 }
291
292 #endif
293