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