Rev 1250 | Rev 1330 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
447 | decky | 1 | /* |
2 | * Copyright (C) 2005 Martin Decky |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * - The name of the author may not be used to endorse or promote products |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
713 | decky | 29 | #include "version.h" |
954 | palkovsky | 30 | #include <ipc.h> |
988 | palkovsky | 31 | #include <stdio.h> |
32 | #include <unistd.h> |
||
33 | #include <stdlib.h> |
||
1028 | palkovsky | 34 | #include <ns.h> |
1065 | jermar | 35 | #include <thread.h> |
1175 | jermar | 36 | #include <task.h> |
1113 | palkovsky | 37 | #include <psthread.h> |
1111 | jermar | 38 | #include <futex.h> |
1250 | jermar | 39 | #include <as.h> |
1279 | palkovsky | 40 | #include <ddi.h> |
999 | palkovsky | 41 | |
1089 | palkovsky | 42 | int a; |
1111 | jermar | 43 | atomic_t ftx; |
1089 | palkovsky | 44 | |
1152 | jermar | 45 | int __thread stage; |
1129 | palkovsky | 46 | |
1065 | jermar | 47 | extern void utest(void *arg); |
48 | void utest(void *arg) |
||
49 | { |
||
1081 | jermar | 50 | printf("Uspace thread started.\n"); |
1111 | jermar | 51 | if (futex_down(&ftx) < 0) |
52 | printf("Futex failed.\n"); |
||
53 | if (futex_up(&ftx) < 0) |
||
54 | printf("Futex failed.\n"); |
||
55 | |||
56 | printf("%s in good condition.\n", __FUNCTION__); |
||
57 | |||
1065 | jermar | 58 | for (;;) |
59 | ; |
||
60 | } |
||
1049 | cejka | 61 | |
1197 | cejka | 62 | /* test different parameters types and modifiers */ |
995 | cejka | 63 | static void test_printf(void) |
64 | { |
||
65 | printf("Simple text.\n"); |
||
66 | printf("Now insert '%s' string.\n","this"); |
||
1073 | cejka | 67 | printf("Signed formats on uns. numbers: '%d', '%+d', '% d', '%u' (,+, ,u)\n", 321, 321, 321, 321); |
68 | printf("Signed formats on sig. numbers: '%d', '%+d', '% d', '%u' (,+, ,u)\n", -321, -321, -321, -321); |
||
69 | printf("Signed with different sized: '%hhd', '%hd', '%d', '%ld', %lld;\n", -3, -32, -321, -32101l, -3210123ll); |
||
70 | printf("And now... '%hhd' byte! '%hd' word! '%d' int! \n", 11, 11111, 1111111111); |
||
71 | printf("Different bases: %#hx, %#hu, %#ho and %#hb\n", 123, 123, 123, 123); |
||
72 | printf("Different bases signed: %#hx, %#hu, %#ho and %#hb\n", -123, -123, -123, -123); |
||
73 | printf("'%llX' llX! Another '%llx' llx! \n", 0x1234567887654321ll, 0x1234567887654321ll); |
||
74 | printf("'%llX' with 64bit value and '%x' with 32 bit value. \n", 0x1234567887654321ll, 0x12345678 ); |
||
75 | printf("'%llx' 64bit, '%x' 32bit, '%hhx' 8bit, '%hx' 16bit, '%llX' 64bit and '%s' string.\n", 0x1234567887654321ll, 0x12345678, 0x12, 0x1234, 0x1234567887654321ull, "Lovely string" ); |
||
1049 | cejka | 76 | |
995 | cejka | 77 | printf("Thats all, folks!\n"); |
78 | } |
||
999 | palkovsky | 79 | |
1197 | cejka | 80 | /* test width and precision modifiers */ |
81 | static void test_printf2(void) |
||
82 | { |
||
83 | printf(" text 10.8s %*.*s \n", 5, 3, "text"); |
||
84 | printf(" very long text 10.8s %10.8s \n", "very long text"); |
||
85 | printf(" text 8.10s %8.10s \n", "text"); |
||
86 | printf(" very long text 8.10s %8.10s \n", "very long text"); |
||
1049 | cejka | 87 | |
1197 | cejka | 88 | printf(" char: c '%c', 3.2c '%3.2c', -3.2c '%-3.2c', 2.3c '%2.3c', -2.3c '%-2.3c' \n",'a', 'b', 'c', 'd', 'e' ); |
89 | printf(" int: d '%d', 3.2d '%3.2d', -3.2d '%-3.2d', 2.3d '%2.3d', -2.3d '%-2.3d' \n",1, 1, 1, 1, 1 ); |
||
90 | printf(" -int: d '%d', 3.2d '%3.2d', -3.2d '%-3.2d', 2.3d '%2.3d', -2.3d '%-2.3d' \n",-1, -1, -1, -1, -1 ); |
||
91 | printf(" 0xint: x '%x', 5.3x '%#5.3x', -5.3x '%#-5.3x', 3.5x '%#3.5x', -3.5x '%#-3.5x' \n",17, 17, 17, 17, 17 ); |
||
92 | |||
93 | } |
||
94 | |||
1028 | palkovsky | 95 | extern char _heap; |
994 | jermar | 96 | static void test_mremap(void) |
988 | palkovsky | 97 | { |
98 | printf("Writing to good memory\n"); |
||
1228 | jermar | 99 | as_area_resize(&_heap, 120000, 0); |
988 | palkovsky | 100 | printf("%P\n", ((char *)&_heap)); |
101 | printf("%P\n", ((char *)&_heap) + 80000); |
||
102 | *(((char *)&_heap) + 80000) = 10; |
||
103 | printf("Making small\n"); |
||
1228 | jermar | 104 | as_area_resize(&_heap, 16000, 0); |
988 | palkovsky | 105 | printf("Failing..\n"); |
106 | *((&_heap) + 80000) = 10; |
||
107 | |||
108 | printf("memory done\n"); |
||
109 | } |
||
110 | /* |
||
111 | static void test_sbrk(void) |
||
112 | { |
||
113 | printf("Writing to good memory\n"); |
||
114 | printf("Got: %P\n", sbrk(120000)); |
||
115 | printf("%P\n", ((char *)&_heap)); |
||
116 | printf("%P\n", ((char *)&_heap) + 80000); |
||
117 | *(((char *)&_heap) + 80000) = 10; |
||
118 | printf("Making small, got: %P\n",sbrk(-120000)); |
||
119 | printf("Testing access to heap\n"); |
||
120 | _heap = 10; |
||
121 | printf("Failing..\n"); |
||
122 | *((&_heap) + 80000) = 10; |
||
123 | |||
124 | printf("memory done\n"); |
||
125 | } |
||
126 | */ |
||
127 | /* |
||
128 | static void test_malloc(void) |
||
129 | { |
||
130 | char *data; |
||
131 | |||
132 | data = malloc(10); |
||
133 | printf("Heap: %P, data: %P\n", &_heap, data); |
||
134 | data[0] = 'a'; |
||
135 | free(data); |
||
136 | } |
||
137 | */ |
||
138 | |||
1028 | palkovsky | 139 | |
140 | static void test_ping(void) |
||
141 | { |
||
142 | ipcarg_t result; |
||
143 | int retval; |
||
144 | |||
1091 | palkovsky | 145 | printf("Pinging\n"); |
1028 | palkovsky | 146 | retval = ipc_call_sync(PHONE_NS, NS_PING, 0xbeef,&result); |
147 | printf("Retval: %d - received: %P\n", retval, result); |
||
148 | } |
||
149 | |||
1091 | palkovsky | 150 | static void got_answer(void *private, int retval, ipc_call_t *data) |
999 | palkovsky | 151 | { |
1073 | cejka | 152 | printf("Retval: %d...%s...%zX, %zX\n", retval, private, |
999 | palkovsky | 153 | IPC_GET_ARG1(*data), IPC_GET_ARG2(*data)); |
154 | } |
||
155 | static void test_async_ipc(void) |
||
156 | { |
||
1028 | palkovsky | 157 | ipc_call_t data; |
999 | palkovsky | 158 | int i; |
159 | |||
160 | printf("Sending ping\n"); |
||
161 | ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2, |
||
162 | "Pong1", got_answer); |
||
163 | ipc_call_async_2(PHONE_NS, NS_PING, 2, 0xbeefbee4, |
||
164 | "Pong2", got_answer); |
||
165 | ipc_call_async_2(PHONE_NS, NS_PING, 3, 0xbeefbee4, |
||
166 | "Pong3", got_answer); |
||
167 | ipc_call_async_2(PHONE_NS, NS_PING, 4, 0xbeefbee4, |
||
168 | "Pong4", got_answer); |
||
169 | ipc_call_async_2(PHONE_NS, NS_PING, 5, 0xbeefbee4, |
||
170 | "Pong5", got_answer); |
||
171 | ipc_call_async_2(PHONE_NS, NS_PING, 6, 0xbeefbee4, |
||
172 | "Pong6", got_answer); |
||
173 | printf("Waiting forever...\n"); |
||
174 | for (i=0; i<100;i++) |
||
175 | printf("."); |
||
176 | printf("\n"); |
||
177 | ipc_wait_for_call(&data, NULL); |
||
178 | printf("Received call???\n"); |
||
179 | } |
||
180 | |||
1028 | palkovsky | 181 | |
1091 | palkovsky | 182 | static void got_answer_2(void *private, int retval, ipc_call_t *data) |
1028 | palkovsky | 183 | { |
184 | printf("Pong\n"); |
||
185 | } |
||
186 | static void test_advanced_ipc(void) |
||
187 | { |
||
188 | int res; |
||
1091 | palkovsky | 189 | ipcarg_t phonead; |
1028 | palkovsky | 190 | ipc_callid_t callid; |
191 | ipc_call_t data; |
||
1061 | palkovsky | 192 | int i; |
1028 | palkovsky | 193 | |
194 | printf("Asking 0 to connect to me...\n"); |
||
1091 | palkovsky | 195 | res = ipc_connect_to_me(0, 1, 2, &phonead); |
196 | printf("Result: %d - phonead: %llu\n", res, phonead); |
||
1061 | palkovsky | 197 | for (i=0; i < 100; i++) { |
1028 | palkovsky | 198 | printf("----------------\n"); |
199 | ipc_call_async(PHONE_NS, NS_PING_SVC, 0, "prov", |
||
200 | got_answer_2); |
||
201 | callid = ipc_wait_for_call(&data, NULL); |
||
202 | printf("Received ping\n"); |
||
203 | ipc_answer(callid, 0, 0, 0); |
||
1061 | palkovsky | 204 | } |
1089 | palkovsky | 205 | // callid = ipc_wait_for_call(&data, NULL); |
1028 | palkovsky | 206 | } |
207 | |||
1061 | palkovsky | 208 | static void test_connection_ipc(void) |
209 | { |
||
210 | int res; |
||
211 | ipcarg_t result; |
||
1091 | palkovsky | 212 | int phoneid; |
1061 | palkovsky | 213 | |
214 | printf("Starting connect...\n"); |
||
215 | res = ipc_connect_me_to(PHONE_NS, 10, 20); |
||
216 | printf("Connected: %d\n", res); |
||
217 | printf("pinging.\n"); |
||
218 | res = ipc_call_sync(res, NS_PING, 0xbeef,&result); |
||
1089 | palkovsky | 219 | printf("Retval: %d - received: %X\n", res, result); |
1061 | palkovsky | 220 | |
221 | } |
||
222 | |||
1089 | palkovsky | 223 | static void test_hangup(void) |
224 | { |
||
225 | int phoneid; |
||
226 | ipc_call_t data; |
||
227 | ipc_callid_t callid; |
||
228 | int i; |
||
229 | |||
230 | printf("Starting connect...\n"); |
||
231 | phoneid = ipc_connect_me_to(PHONE_NS, 10, 20); |
||
232 | printf("Phoneid: %d, pinging\n", phoneid); |
||
233 | ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2, |
||
234 | "Pong1", got_answer); |
||
235 | printf("Hangin up\n"); |
||
236 | ipc_hangup(phoneid); |
||
237 | printf("Connecting\n"); |
||
238 | phoneid = ipc_connect_me_to(PHONE_NS, 10, 20); |
||
239 | printf("Newphid: %d\n", phoneid); |
||
240 | for (i=0; i < 1000; i++) { |
||
241 | if ((callid=ipc_wait_for_call(&data, IPC_WAIT_NONBLOCKING))) |
||
242 | printf("callid: %d\n"); |
||
243 | } |
||
244 | printf("New new phoneid: %d\n", ipc_connect_me_to(PHONE_NS, 10, 20)); |
||
245 | } |
||
246 | |||
247 | static void test_slam(void) |
||
248 | { |
||
249 | int i; |
||
250 | ipc_call_t data; |
||
251 | ipc_callid_t callid; |
||
252 | |||
253 | printf("ping"); |
||
254 | ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2, |
||
255 | "Pong1", got_answer); |
||
256 | printf("slam"); |
||
257 | ipc_call_async_2(PHONE_NS, NS_HANGUP, 1, 0xbeefbee2, |
||
258 | "Hang", got_answer); |
||
259 | printf("ping2\n"); |
||
260 | ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2, |
||
261 | "Ping2", got_answer); |
||
262 | |||
263 | for (i=0; i < 1000; i++) { |
||
264 | if ((callid=ipc_wait_for_call(&data, IPC_WAIT_NONBLOCKING))) |
||
265 | printf("callid: %d\n"); |
||
266 | } |
||
267 | ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2, |
||
268 | "Pong1", got_answer); |
||
269 | printf("Closing file\n"); |
||
270 | ipc_hangup(PHONE_NS); |
||
271 | ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2, |
||
272 | "Pong1", got_answer); |
||
273 | ipc_wait_for_call(&data, 0); |
||
274 | } |
||
275 | |||
1113 | palkovsky | 276 | static int ptest(void *arg) |
277 | { |
||
1152 | jermar | 278 | stage = 1; |
279 | printf("Pseudo thread stage%d.\n", stage); |
||
280 | stage++; |
||
1128 | jermar | 281 | psthread_schedule_next(); |
1152 | jermar | 282 | printf("Pseudo thread stage%d.\n", stage); |
283 | stage++; |
||
1128 | jermar | 284 | psthread_schedule_next(); |
1152 | jermar | 285 | printf("Pseudo thread stage%d.\n", stage); |
1128 | jermar | 286 | psthread_schedule_next(); |
1152 | jermar | 287 | stage++; |
288 | printf("Pseudo thread stage%d.\n", stage); |
||
1128 | jermar | 289 | psthread_schedule_next(); |
290 | printf("Pseudo thread exiting.\n"); |
||
1113 | palkovsky | 291 | return 0; |
292 | } |
||
1111 | jermar | 293 | |
447 | decky | 294 | int main(int argc, char *argv[]) |
295 | { |
||
1113 | palkovsky | 296 | pstid_t ptid; |
1065 | jermar | 297 | int tid; |
1197 | cejka | 298 | |
713 | decky | 299 | version_print(); |
954 | palkovsky | 300 | |
1125 | jermar | 301 | // test_printf(); |
1197 | cejka | 302 | // test_printf2(); |
1028 | palkovsky | 303 | // test_ping(); |
304 | // test_async_ipc(); |
||
1061 | palkovsky | 305 | // test_advanced_ipc(); |
1089 | palkovsky | 306 | // test_connection_ipc(); |
307 | // test_hangup(); |
||
1091 | palkovsky | 308 | // test_slam(); |
1111 | jermar | 309 | |
1228 | jermar | 310 | printf("Userspace task, taskid=%llX\n", task_get_id()); |
1175 | jermar | 311 | |
1111 | jermar | 312 | futex_initialize(&ftx, 1); |
313 | if (futex_down(&ftx) < 0) |
||
314 | printf("Futex failed.\n"); |
||
315 | if (futex_up(&ftx) < 0) |
||
316 | printf("Futex failed.\n"); |
||
1089 | palkovsky | 317 | |
1111 | jermar | 318 | if (futex_down(&ftx) < 0) |
319 | printf("Futex failed.\n"); |
||
1152 | jermar | 320 | |
1125 | jermar | 321 | if ((tid = thread_create(utest, NULL, "utest")) != -1) { |
1091 | palkovsky | 322 | printf("Created thread tid=%d\n", tid); |
323 | } |
||
1111 | jermar | 324 | |
1125 | jermar | 325 | if ((tid = thread_create(utest, NULL, "utest")) != -1) { |
326 | printf("Created thread tid=%d\n", tid); |
||
327 | } |
||
1152 | jermar | 328 | |
1111 | jermar | 329 | int i; |
330 | |||
1125 | jermar | 331 | for (i = 0; i < 50000000; i++) |
1111 | jermar | 332 | ; |
333 | |||
334 | if (futex_up(&ftx) < 0) |
||
335 | printf("Futex failed.\n"); |
||
336 | |||
1129 | palkovsky | 337 | |
1152 | jermar | 338 | printf("Creating pseudo thread.\n"); |
339 | stage = 1; |
||
1113 | palkovsky | 340 | ptid = psthread_create(ptest, NULL); |
1152 | jermar | 341 | printf("Main thread stage%d.\n", stage); |
342 | stage++; |
||
1128 | jermar | 343 | psthread_schedule_next();; |
1152 | jermar | 344 | printf("Main thread stage%d.\n", stage); |
345 | stage++; |
||
1128 | jermar | 346 | psthread_schedule_next();; |
1152 | jermar | 347 | printf("Main thread stage%d.\n", stage); |
1113 | palkovsky | 348 | |
1128 | jermar | 349 | psthread_join(ptid); |
1113 | palkovsky | 350 | |
1111 | jermar | 351 | printf("Main thread exiting.\n"); |
447 | decky | 352 | return 0; |
353 | } |