Split uthread code into its own source file
[akaros.git] / tests / mproctests.c
1 #include <arch/arch.h>
2 #include <parlib.h>
3 #include <rassert.h>
4 #include <stdlib.h>
5 #include <vcore.h>
6 #include <ros/mman.h>
7 #include <ros/resource.h>
8 #include <rstdio.h>
9 #include <timing.h>
10 #include <uthread.h>
11
12 #ifdef __sparc_v8__
13 # define udelay(x) udelay((x)/2000)
14 #endif
15
16 #define TEST_MMAP                                        1
17 #define TEST_ONE_CORE                            2
18 #define TEST_ASK_FOR_TOO_MANY_CORES      3
19 #define TEST_INCREMENTAL_CHANGES         4
20 #define TEST_YIELD_OUT_OF_ORDER          5
21 #define TEST_YIELD_0_OUT_OF_ORDER        6
22 #define TEST_YIELD_ALL               7
23 #define TEST_SWITCH_TO_RUNNABLE_S        8
24 #define TEST_CRAZY_YIELDS                        9
25 #define TEST_CONCURRENT_SYSCALLS        10
26
27 int test = TEST_SWITCH_TO_RUNNABLE_S;
28
29 static void global_tests(uint32_t vcoreid);
30
31 void ghetto_vcore_entry(void);
32 struct schedule_ops ghetto_sched_ops = {
33         0, /* init, */
34         ghetto_vcore_entry,
35         0, /* thread_create, */
36         0, /* thread_runnable, */
37         0, /* thread_yield, */
38         0, /* thread_exit, */
39         0, /* preempt_pending, */
40         0, /* spawn_thread, */
41 };
42 struct schedule_ops *sched_ops = &ghetto_sched_ops;
43
44 int main(int argc, char** argv)
45 {
46         uint32_t vcoreid;
47         int retval;
48         vcore_init();
49
50         if ((vcoreid = vcore_id())) {
51                 printf("Should never see me! (from vcore %d)\n", vcoreid);
52         } else { // core 0
53                 printf("Hello from else vcore 0\n");
54                 printf("Multi-Goodbye, world, from PID: %d!\n", sys_getpid());
55                 switch (test) {
56                         case TEST_MMAP:
57                                 printf("Testing MMAP\n");
58                                 void *CT(8*PGSIZE) addr;
59                                 addr = sys_mmap((void*SNT)USTACKTOP - 20*PGSIZE, 8*PGSIZE, 3,
60                                                 MAP_FIXED | MAP_ANONYMOUS, -1, 0);
61                                 printf("got addr = 0x%08x\n", addr);
62                                 *(int*)addr = 0xdeadbeef;
63                                 *(int*)(addr + 3*PGSIZE) = 0xcafebabe;
64                                 // these should work
65                                 printf("reading addr: 0x%08x\n", *(int*)addr);
66                                 printf("reading addr+3pg: 0x%08x\n", *(int*)(addr + 3*PGSIZE));
67                                 // this should fault
68                                 printf("Should page fault and die now.\n");
69                                 { TRUSTEDBLOCK
70                                 *(int*)(addr - 3*PGSIZE) = 0xdeadbeef;
71                                 }
72                                 printf("Should not see me!!!!!!!!!!!!!!!!!!\n");
73                                 while(1);
74                         case TEST_ONE_CORE:
75                                 retval = vcore_request(1);
76                                 printf("One core test's core0's retval: %d\n", retval);
77                                 printf("Check to see it's on a worker core.\n");
78                                 while(1);
79                         case TEST_ASK_FOR_TOO_MANY_CORES:
80                                 retval = vcore_request(12);
81                                 printf("Asked for too many, retval: %d\n", retval);
82                                 return 0;
83                         case TEST_INCREMENTAL_CHANGES:
84                                 retval = vcore_request(4);
85                                 break;
86                         default:
87                                 retval = vcore_request(5);
88                 }
89                 if (retval)
90                         panic("failed to allocate cores!");
91                 printf("Should see me if you want to relocate core0's context "
92                         "when moving from RUNNING_S\n");
93         }
94
95         // vcore0 only below here
96         switch (test) {
97                 case TEST_YIELD_OUT_OF_ORDER:
98                         udelay(10000000);
99                         printf("Core 2 should have yielded, asking for another\n");
100                         retval = vcore_request(5);
101                         break;
102                 case TEST_YIELD_0_OUT_OF_ORDER:
103                         udelay(5000000);
104                         printf("Core %d yielding\n", vcoreid);
105                         sys_yield(0);
106                         printf("Core 0 came back where it left off in RUNNING_M!!!\n");
107                         break;
108         }
109         global_tests(vcoreid);
110         printf("Vcore %d Done!\n", vcoreid);
111         while (1);
112         return 0;
113 }
114
115 void ghetto_vcore_entry(void)
116 {
117         uint32_t vcoreid;
118         static int first_time = 1; // used by vcore2
119         int retval;
120
121         vcoreid = vcore_id();
122         printf("Hello from vcore_entry in vcore %d\n", vcoreid);
123
124         if ((vcoreid == 2) && first_time) {
125                 first_time = 0;
126                 switch (test) {
127                         case TEST_INCREMENTAL_CHANGES:
128                                 // Testing asking for less than we already have
129                                 udelay(1000000);
130                                 printf("Asking for too few:\n");
131                                 retval = vcore_request(2);
132                                 printf("Should be -EINVAL(7): %d\n", retval);
133                                 // Testing getting more while running
134                                 printf("Asking for more while running:\n");
135                                 udelay(1000000);
136                                 retval = vcore_request(5);
137                                 printf("core2's retval: %d\n", retval);
138                                 break;
139                         case TEST_YIELD_OUT_OF_ORDER:
140                                 printf("Core %d yielding\n", vcoreid);
141                                 sys_yield(0);
142                                 break;
143                         case TEST_YIELD_0_OUT_OF_ORDER:
144                                 udelay(7500000);
145                                 printf("Core 0 should have yielded, asking for another\n");
146                                 retval = vcore_request(5);
147                 }
148         }
149         global_tests(vcoreid);
150         printf("Vcore %d Done!\n", vcoreid);
151 }
152
153 static void global_tests(uint32_t vcoreid)
154 {
155         int retval;
156         switch (test) {
157                 case TEST_YIELD_ALL:
158                         printf("Core %d yielding\n", vcoreid);
159                         sys_yield(0);
160                         // should be RUNNABLE_M now, amt_wanted == 1
161                         while(1);
162                 case TEST_SWITCH_TO_RUNNABLE_S:
163                         if (vcoreid == 2) {
164                                 printf("Core %d trying to request 0/ switch to _S\n", vcoreid);
165                                 udelay(3000000);
166                                 retval = vcore_request(0);
167                                 // will only see this if we are scheduled()
168                                 printf("Core %d back up! (retval:%d)\n", vcoreid, retval);
169                                 printf("And exiting\n");
170                                 exit(0);
171                         } 
172                         while(1);
173                 case TEST_CRAZY_YIELDS:
174                         udelay(300000*vcoreid);
175                         vcore_request(5);
176                         sys_yield(0);
177                         printf("should  never see me, unless you slip into *_S\n");
178                         break;
179                 case TEST_CONCURRENT_SYSCALLS:
180                         for (int i = 0; i < 10; i++) {
181                                 for (int j = 0; j < 100; j++)
182                                         sys_null();
183                                 printf("Hello from vcore %d, iteration %d\n", vcoreid, i);
184                         }
185                         break;
186         }
187 }