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