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