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