Subversion Repositories HelenOS

Rev

Rev 1279 | Rev 1339 | 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>
1330 palkovsky 41
#include <string.h>
999 palkovsky 42
 
1089 palkovsky 43
int a;
1111 jermar 44
atomic_t ftx;
1089 palkovsky 45
 
1152 jermar 46
int __thread stage;
1129 palkovsky 47
 
1065 jermar 48
extern void utest(void *arg);
49
void utest(void *arg)
50
{
1081 jermar 51
	printf("Uspace thread started.\n");
1111 jermar 52
	if (futex_down(&ftx) < 0)
53
		printf("Futex failed.\n");
54
	if (futex_up(&ftx) < 0)
55
		printf("Futex failed.\n");
56
 
57
	printf("%s in good condition.\n", __FUNCTION__);
58
 
1065 jermar 59
	for (;;)
60
		;
61
}
1049 cejka 62
 
1197 cejka 63
/* test different parameters types and modifiers */
995 cejka 64
static void test_printf(void)
65
{
66
	printf("Simple text.\n");
67
	printf("Now insert '%s' string.\n","this");
1073 cejka 68
	printf("Signed formats on uns. numbers: '%d', '%+d', '% d', '%u' (,+, ,u)\n", 321, 321, 321, 321);
69
	printf("Signed formats on sig. numbers: '%d', '%+d', '% d', '%u' (,+, ,u)\n", -321, -321, -321, -321);
70
	printf("Signed with different sized: '%hhd', '%hd', '%d', '%ld', %lld;\n", -3, -32, -321, -32101l, -3210123ll);
71
	printf("And now... '%hhd' byte! '%hd' word! '%d' int! \n", 11, 11111, 1111111111);
72
	printf("Different bases: %#hx, %#hu, %#ho and %#hb\n", 123, 123, 123, 123);
73
	printf("Different bases signed: %#hx, %#hu, %#ho and %#hb\n", -123, -123, -123, -123);
74
	printf("'%llX' llX! Another '%llx' llx! \n", 0x1234567887654321ll, 0x1234567887654321ll);
75
	printf("'%llX' with 64bit value and '%x' with 32 bit value. \n", 0x1234567887654321ll, 0x12345678 );
76
	printf("'%llx' 64bit, '%x' 32bit, '%hhx' 8bit, '%hx' 16bit, '%llX' 64bit and '%s' string.\n", 0x1234567887654321ll, 0x12345678, 0x12, 0x1234, 0x1234567887654321ull, "Lovely string" );
1049 cejka 77
 
995 cejka 78
	printf("Thats all, folks!\n");
79
}
999 palkovsky 80
 
1197 cejka 81
/* test width and precision modifiers */
82
static void test_printf2(void)
83
{
84
	printf(" text 10.8s %*.*s \n", 5, 3, "text");
85
	printf(" very long text 10.8s %10.8s \n", "very long text");
86
	printf(" text 8.10s %8.10s \n", "text");
87
	printf(" very long text 8.10s %8.10s \n", "very long text");
1049 cejka 88
 
1197 cejka 89
	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' );
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(" -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 );
92
	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 );
93
 
94
}
95
 
1028 palkovsky 96
extern char _heap;
994 jermar 97
static void test_mremap(void)
988 palkovsky 98
{
99
	printf("Writing to good memory\n");
1228 jermar 100
	as_area_resize(&_heap, 120000, 0);
988 palkovsky 101
	printf("%P\n", ((char *)&_heap));
102
	printf("%P\n", ((char *)&_heap) + 80000);
103
	*(((char *)&_heap) + 80000) = 10;
104
	printf("Making small\n");
1228 jermar 105
	as_area_resize(&_heap, 16000, 0);
988 palkovsky 106
	printf("Failing..\n");
107
	*((&_heap) + 80000) = 10;
108
 
109
	printf("memory done\n");
110
}
111
/*
112
static void test_sbrk(void)
113
{
114
	printf("Writing to good memory\n");
115
	printf("Got: %P\n", sbrk(120000));
116
	printf("%P\n", ((char *)&_heap));
117
	printf("%P\n", ((char *)&_heap) + 80000);
118
	*(((char *)&_heap) + 80000) = 10;
119
	printf("Making small, got: %P\n",sbrk(-120000));
120
	printf("Testing access to heap\n");
121
	_heap = 10;
122
	printf("Failing..\n");
123
	*((&_heap) + 80000) = 10;
124
 
125
	printf("memory done\n");
126
}
127
*/
128
/*
129
static void test_malloc(void)
130
{
131
	char *data;
132
 
133
	data = malloc(10);
134
	printf("Heap: %P, data: %P\n", &_heap, data);
135
	data[0] = 'a';
136
	free(data);
137
}
138
*/
139
 
1028 palkovsky 140
 
141
static void test_ping(void)
142
{
143
	ipcarg_t result;
144
	int retval;
145
 
1091 palkovsky 146
	printf("Pinging\n");
1028 palkovsky 147
	retval = ipc_call_sync(PHONE_NS, NS_PING, 0xbeef,&result);
148
	printf("Retval: %d - received: %P\n", retval, result);
149
}
150
 
1091 palkovsky 151
static void got_answer(void *private, int retval, ipc_call_t *data)
999 palkovsky 152
{
1073 cejka 153
	printf("Retval: %d...%s...%zX, %zX\n", retval, private,
999 palkovsky 154
	       IPC_GET_ARG1(*data), IPC_GET_ARG2(*data));
155
}
156
static void test_async_ipc(void)
157
{
1028 palkovsky 158
	ipc_call_t data;
999 palkovsky 159
	int i;
160
 
161
	printf("Sending ping\n");
162
	ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2,
163
			 "Pong1", got_answer);
164
	ipc_call_async_2(PHONE_NS, NS_PING, 2, 0xbeefbee4, 
165
			 "Pong2", got_answer);
166
	ipc_call_async_2(PHONE_NS, NS_PING, 3, 0xbeefbee4, 
167
			 "Pong3", got_answer);
168
	ipc_call_async_2(PHONE_NS, NS_PING, 4, 0xbeefbee4, 
169
			 "Pong4", got_answer);
170
	ipc_call_async_2(PHONE_NS, NS_PING, 5, 0xbeefbee4, 
171
			 "Pong5", got_answer);
172
	ipc_call_async_2(PHONE_NS, NS_PING, 6, 0xbeefbee4, 
173
			 "Pong6", got_answer);
174
	printf("Waiting forever...\n");
175
	for (i=0; i<100;i++)
176
		printf(".");
177
	printf("\n");
178
	ipc_wait_for_call(&data, NULL);
179
	printf("Received call???\n");
180
}
181
 
1028 palkovsky 182
 
1091 palkovsky 183
static void got_answer_2(void *private, int retval, ipc_call_t *data)
1028 palkovsky 184
{
185
	printf("Pong\n");
186
}
187
static void test_advanced_ipc(void)
188
{
189
	int res;
1091 palkovsky 190
	ipcarg_t phonead;
1028 palkovsky 191
	ipc_callid_t callid;
192
	ipc_call_t data;
1061 palkovsky 193
	int i;
1028 palkovsky 194
 
195
	printf("Asking 0 to connect to me...\n");
1091 palkovsky 196
	res = ipc_connect_to_me(0, 1, 2, &phonead);
197
	printf("Result: %d - phonead: %llu\n", res, phonead);
1061 palkovsky 198
	for (i=0; i < 100; i++) {
1028 palkovsky 199
		printf("----------------\n");
200
		ipc_call_async(PHONE_NS, NS_PING_SVC, 0, "prov",
201
			       got_answer_2);
202
		callid = ipc_wait_for_call(&data, NULL);
203
		printf("Received ping\n");
204
		ipc_answer(callid, 0, 0, 0);
1061 palkovsky 205
	}
1089 palkovsky 206
//	callid = ipc_wait_for_call(&data, NULL);
1028 palkovsky 207
}
208
 
1061 palkovsky 209
static void test_connection_ipc(void)
210
{
211
	int res;
212
	ipcarg_t result;
1091 palkovsky 213
	int phoneid;
1061 palkovsky 214
 
215
	printf("Starting connect...\n");
216
	res = ipc_connect_me_to(PHONE_NS, 10, 20);
217
	printf("Connected: %d\n", res);
218
	printf("pinging.\n");
219
	res = ipc_call_sync(res, NS_PING, 0xbeef,&result);
1089 palkovsky 220
	printf("Retval: %d - received: %X\n", res, result);
1061 palkovsky 221
 
222
}
223
 
1089 palkovsky 224
static void test_hangup(void)
225
{
226
	int phoneid;
227
	ipc_call_t data;
228
	ipc_callid_t callid;
229
	int i;
230
 
231
	printf("Starting connect...\n");
232
	phoneid = ipc_connect_me_to(PHONE_NS, 10, 20);
233
	printf("Phoneid: %d, pinging\n", phoneid);
234
	ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2,
235
			 "Pong1", got_answer);
236
	printf("Hangin up\n");
237
	ipc_hangup(phoneid);
238
	printf("Connecting\n");
239
	phoneid = ipc_connect_me_to(PHONE_NS, 10, 20);
240
	printf("Newphid: %d\n", phoneid);
241
	for (i=0; i < 1000; i++) {
242
		if ((callid=ipc_wait_for_call(&data, IPC_WAIT_NONBLOCKING)))
243
			printf("callid: %d\n");
244
	}
245
	printf("New new phoneid: %d\n", ipc_connect_me_to(PHONE_NS, 10, 20));
246
}
247
 
248
static void test_slam(void)
249
{
250
	int i;
251
	ipc_call_t data;
252
	ipc_callid_t callid;
253
 
254
	printf("ping");
255
	ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2,
256
			 "Pong1", got_answer);
257
	printf("slam");
258
	ipc_call_async_2(PHONE_NS, NS_HANGUP, 1, 0xbeefbee2,
259
			 "Hang", got_answer);
260
	printf("ping2\n");
261
	ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2,
262
			 "Ping2", got_answer);
263
 
264
	for (i=0; i < 1000; i++) {
265
		if ((callid=ipc_wait_for_call(&data, IPC_WAIT_NONBLOCKING)))
266
			printf("callid: %d\n");
267
	}
268
	ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2,
269
			 "Pong1", got_answer);
270
	printf("Closing file\n");
271
	ipc_hangup(PHONE_NS);
272
	ipc_call_async_2(PHONE_NS, NS_PING, 1, 0xbeefbee2,
273
			 "Pong1", got_answer);
274
	ipc_wait_for_call(&data, 0);
275
}
276
 
1113 palkovsky 277
static int ptest(void *arg)
278
{
1152 jermar 279
	stage = 1;
280
	printf("Pseudo thread stage%d.\n", stage);
281
	stage++;
1128 jermar 282
	psthread_schedule_next();
1152 jermar 283
	printf("Pseudo thread stage%d.\n", stage);
284
	stage++;
1128 jermar 285
	psthread_schedule_next();
1152 jermar 286
	printf("Pseudo thread stage%d.\n", stage);
1128 jermar 287
	psthread_schedule_next();
1152 jermar 288
	stage++;
289
	printf("Pseudo thread stage%d.\n", stage);
1128 jermar 290
	psthread_schedule_next();
291
	printf("Pseudo thread exiting.\n");
1113 palkovsky 292
	return 0;	
293
}
1111 jermar 294
 
1330 palkovsky 295
static int test_as_send()
296
{
297
	char *as;
298
	int retval;
299
	ipcarg_t result;
300
 
301
	as = as_area_create((void *)(1024*1024), 16384, AS_AREA_READ | AS_AREA_WRITE);
302
	if (!as) {
303
		printf("Error creating as.\n");
304
		return 0;
305
	}
306
 
307
	memcpy(as, "Hello world.\n", 14);
308
 
309
	retval = ipc_call_sync_2(PHONE_NS, IPC_M_AS_SEND, 0, (sysarg_t) as,
310
		NULL, NULL);
311
	if (retval) {
312
		printf("AS_SEND failed.\n");
313
		return 0;
314
	}
315
	printf("Done\n");
316
}
317
 
447 decky 318
int main(int argc, char *argv[])
319
{
1113 palkovsky 320
	pstid_t ptid;
1065 jermar 321
	int tid;
1197 cejka 322
 
713 decky 323
	version_print();
954 palkovsky 324
 
1125 jermar 325
//	test_printf();
1197 cejka 326
//	test_printf2();
1028 palkovsky 327
//	test_ping();
328
//	test_async_ipc();
1061 palkovsky 329
//	test_advanced_ipc();
1089 palkovsky 330
//	test_connection_ipc();
331
//	test_hangup();
1091 palkovsky 332
//	test_slam();
1330 palkovsky 333
	test_as_send();
334
/*	
1228 jermar 335
	printf("Userspace task, taskid=%llX\n", task_get_id());
1175 jermar 336
 
1111 jermar 337
	futex_initialize(&ftx, 1);
338
	if (futex_down(&ftx) < 0)
339
		printf("Futex failed.\n");
340
	if (futex_up(&ftx) < 0)
341
		printf("Futex failed.\n");
1089 palkovsky 342
 
1111 jermar 343
	if (futex_down(&ftx) < 0)
344
		printf("Futex failed.\n");
1152 jermar 345
 
1125 jermar 346
	if ((tid = thread_create(utest, NULL, "utest")) != -1) {
1091 palkovsky 347
		printf("Created thread tid=%d\n", tid);
348
	}
1111 jermar 349
 
1125 jermar 350
	if ((tid = thread_create(utest, NULL, "utest")) != -1) {
351
		printf("Created thread tid=%d\n", tid);
352
	}
1152 jermar 353
 
1111 jermar 354
	int i;
355
 
1125 jermar 356
	for (i = 0; i < 50000000; i++)
1111 jermar 357
		;
358
 
359
	if (futex_up(&ftx) < 0)
360
		printf("Futex failed.\n");
361
 
1129 palkovsky 362
 
1152 jermar 363
	printf("Creating pseudo thread.\n");
364
	stage = 1;
1113 palkovsky 365
	ptid = psthread_create(ptest, NULL);
1152 jermar 366
	printf("Main thread stage%d.\n", stage);
367
	stage++;
1128 jermar 368
	psthread_schedule_next();;
1152 jermar 369
	printf("Main thread stage%d.\n", stage);
370
	stage++;
1128 jermar 371
	psthread_schedule_next();;
1152 jermar 372
	printf("Main thread stage%d.\n", stage);
1113 palkovsky 373
 
1128 jermar 374
	psthread_join(ptid);
1113 palkovsky 375
 
1111 jermar 376
	printf("Main thread exiting.\n");
1330 palkovsky 377
*/
447 decky 378
	return 0;
379
}