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