Subversion Repositories HelenOS-historic

Rev

Rev 1250 | Rev 1339 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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.  
  29. #include "version.h"
  30. #include <ipc.h>
  31. #include <stdio.h>
  32. #include <unistd.h>
  33. #include <stdlib.h>
  34. #include <ns.h>
  35. #include <thread.h>
  36. #include <task.h>
  37. #include <psthread.h>
  38. #include <futex.h>
  39. #include <as.h>
  40. #include <ddi.h>
  41.  
  42. int a;
  43. atomic_t ftx;
  44.  
  45. int __thread stage;
  46.  
  47. extern void utest(void *arg);
  48. void utest(void *arg)
  49. {
  50.     printf("Uspace thread started.\n");
  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.    
  58.     for (;;)
  59.         ;
  60. }
  61.  
  62. /* test different parameters types and modifiers */
  63. static void test_printf(void)
  64. {
  65.     printf("Simple text.\n");
  66.     printf("Now insert '%s' string.\n","this");
  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" );
  76.    
  77.     printf("Thats all, folks!\n");
  78. }
  79.  
  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");
  87.  
  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.  
  95. extern char _heap;
  96. static void test_mremap(void)
  97. {
  98.     printf("Writing to good memory\n");
  99.     as_area_resize(&_heap, 120000, 0);
  100.     printf("%P\n", ((char *)&_heap));
  101.     printf("%P\n", ((char *)&_heap) + 80000);
  102.     *(((char *)&_heap) + 80000) = 10;
  103.     printf("Making small\n");
  104.     as_area_resize(&_heap, 16000, 0);
  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.  
  139.  
  140. static void test_ping(void)
  141. {
  142.     ipcarg_t result;
  143.     int retval;
  144.  
  145.     printf("Pinging\n");
  146.     retval = ipc_call_sync(PHONE_NS, NS_PING, 0xbeef,&result);
  147.     printf("Retval: %d - received: %P\n", retval, result);
  148. }
  149.  
  150. static void got_answer(void *private, int retval, ipc_call_t *data)
  151. {
  152.     printf("Retval: %d...%s...%zX, %zX\n", retval, private,
  153.            IPC_GET_ARG1(*data), IPC_GET_ARG2(*data));
  154. }
  155. static void test_async_ipc(void)
  156. {
  157.     ipc_call_t data;
  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.  
  181.  
  182. static void got_answer_2(void *private, int retval, ipc_call_t *data)
  183. {
  184.     printf("Pong\n");
  185. }
  186. static void test_advanced_ipc(void)
  187. {
  188.     int res;
  189.     ipcarg_t phonead;
  190.     ipc_callid_t callid;
  191.     ipc_call_t data;
  192.     int i;
  193.  
  194.     printf("Asking 0 to connect to me...\n");
  195.     res = ipc_connect_to_me(0, 1, 2, &phonead);
  196.     printf("Result: %d - phonead: %llu\n", res, phonead);
  197.     for (i=0; i < 100; i++) {
  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);
  204.     }
  205. //  callid = ipc_wait_for_call(&data, NULL);
  206. }
  207.  
  208. static void test_connection_ipc(void)
  209. {
  210.     int res;
  211.     ipcarg_t result;
  212.     int phoneid;
  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);
  219.     printf("Retval: %d - received: %X\n", res, result);
  220.    
  221. }
  222.  
  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.  
  276. static int ptest(void *arg)
  277. {
  278.     stage = 1;
  279.     printf("Pseudo thread stage%d.\n", stage);
  280.     stage++;
  281.     psthread_schedule_next();
  282.     printf("Pseudo thread stage%d.\n", stage);
  283.     stage++;
  284.     psthread_schedule_next();
  285.     printf("Pseudo thread stage%d.\n", stage);
  286.     psthread_schedule_next();
  287.     stage++;
  288.     printf("Pseudo thread stage%d.\n", stage);
  289.     psthread_schedule_next();
  290.     printf("Pseudo thread exiting.\n");
  291.     return 0;  
  292. }
  293.  
  294. int main(int argc, char *argv[])
  295. {
  296.     pstid_t ptid;
  297.     int tid;
  298.    
  299.     version_print();
  300.  
  301. //  test_printf();
  302. //  test_printf2();
  303. //  test_ping();
  304. //  test_async_ipc();
  305. //  test_advanced_ipc();
  306. //  test_connection_ipc();
  307. //  test_hangup();
  308. //  test_slam();
  309.    
  310.     printf("Userspace task, taskid=%llX\n", task_get_id());
  311.  
  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");
  317.  
  318.     if (futex_down(&ftx) < 0)
  319.         printf("Futex failed.\n");
  320.  
  321.     if ((tid = thread_create(utest, NULL, "utest")) != -1) {
  322.         printf("Created thread tid=%d\n", tid);
  323.     }
  324.  
  325.     if ((tid = thread_create(utest, NULL, "utest")) != -1) {
  326.         printf("Created thread tid=%d\n", tid);
  327.     }
  328.  
  329.     int i;
  330.    
  331.     for (i = 0; i < 50000000; i++)
  332.         ;
  333.        
  334.     if (futex_up(&ftx) < 0)
  335.         printf("Futex failed.\n");
  336.  
  337.  
  338.     printf("Creating pseudo thread.\n");
  339.     stage = 1;
  340.     ptid = psthread_create(ptest, NULL);
  341.     printf("Main thread stage%d.\n", stage);
  342.     stage++;
  343.     psthread_schedule_next();;
  344.     printf("Main thread stage%d.\n", stage);
  345.     stage++;
  346.     psthread_schedule_next();;
  347.     printf("Main thread stage%d.\n", stage);
  348.  
  349.     psthread_join(ptid);
  350.  
  351.     printf("Main thread exiting.\n");
  352.     return 0;
  353. }
  354.