Subversion Repositories HelenOS

Rev

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