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