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