Subversion Repositories HelenOS-historic

Rev

Rev 1279 | Rev 1343 | 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. #include <string.h>
  42.  
  43. int a;
  44. atomic_t ftx;
  45.  
  46. int __thread stage;
  47.  
  48. extern void utest(void *arg);
  49. void utest(void *arg)
  50. {
  51.     printf("Uspace thread started.\n");
  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.    
  59.     for (;;)
  60.         ;
  61. }
  62.  
  63. /* test different parameters types and modifiers */
  64. static void test_printf(void)
  65. {
  66.     printf("Simple text.\n");
  67.     printf("Now insert '%s' string.\n","this");
  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" );
  77.    
  78.     printf("Thats all, folks!\n");
  79. }
  80.  
  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");
  88.  
  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.  
  96. extern char _heap;
  97. static void test_mremap(void)
  98. {
  99.     printf("Writing to good memory\n");
  100.     as_area_resize(&_heap, 120000, 0);
  101.     printf("%P\n", ((char *)&_heap));
  102.     printf("%P\n", ((char *)&_heap) + 80000);
  103.     *(((char *)&_heap) + 80000) = 10;
  104.     printf("Making small\n");
  105.     as_area_resize(&_heap, 16000, 0);
  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.  
  140.  
  141. static void test_ping(void)
  142. {
  143.     ipcarg_t result;
  144.     int retval;
  145.  
  146.     printf("Pinging\n");
  147.     retval = ipc_call_sync(PHONE_NS, NS_PING, 0xbeef,&result);
  148.     printf("Retval: %d - received: %P\n", retval, result);
  149. }
  150.  
  151. static void got_answer(void *private, int retval, ipc_call_t *data)
  152. {
  153.     printf("Retval: %d...%s...%zX, %zX\n", retval, private,
  154.            IPC_GET_ARG1(*data), IPC_GET_ARG2(*data));
  155. }
  156. static void test_async_ipc(void)
  157. {
  158.     ipc_call_t data;
  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.  
  182.  
  183. static void got_answer_2(void *private, int retval, ipc_call_t *data)
  184. {
  185.     printf("Pong\n");
  186. }
  187. static void test_advanced_ipc(void)
  188. {
  189.     int res;
  190.     ipcarg_t phonead;
  191.     ipc_callid_t callid;
  192.     ipc_call_t data;
  193.     int i;
  194.  
  195.     printf("Asking 0 to connect to me...\n");
  196.     res = ipc_connect_to_me(0, 1, 2, &phonead);
  197.     printf("Result: %d - phonead: %llu\n", res, phonead);
  198.     for (i=0; i < 100; i++) {
  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);
  205.     }
  206. //  callid = ipc_wait_for_call(&data, NULL);
  207. }
  208.  
  209. static void test_connection_ipc(void)
  210. {
  211.     int res;
  212.     ipcarg_t result;
  213.     int phoneid;
  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);
  220.     printf("Retval: %d - received: %X\n", res, result);
  221.    
  222. }
  223.  
  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.  
  277. static int ptest(void *arg)
  278. {
  279.     stage = 1;
  280.     printf("Pseudo thread stage%d.\n", stage);
  281.     stage++;
  282.     psthread_schedule_next();
  283.     printf("Pseudo thread stage%d.\n", stage);
  284.     stage++;
  285.     psthread_schedule_next();
  286.     printf("Pseudo thread stage%d.\n", stage);
  287.     psthread_schedule_next();
  288.     stage++;
  289.     printf("Pseudo thread stage%d.\n", stage);
  290.     psthread_schedule_next();
  291.     printf("Pseudo thread exiting.\n");
  292.     return 0;  
  293. }
  294.  
  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.  
  318. int main(int argc, char *argv[])
  319. {
  320.     pstid_t ptid;
  321.     int tid;
  322.    
  323.     version_print();
  324.  
  325. //  test_printf();
  326. //  test_printf2();
  327. //  test_ping();
  328. //  test_async_ipc();
  329. //  test_advanced_ipc();
  330. //  test_connection_ipc();
  331. //  test_hangup();
  332. //  test_slam();
  333.     test_as_send();
  334. /* 
  335.     printf("Userspace task, taskid=%llX\n", task_get_id());
  336.  
  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");
  342.  
  343.     if (futex_down(&ftx) < 0)
  344.         printf("Futex failed.\n");
  345.  
  346.     if ((tid = thread_create(utest, NULL, "utest")) != -1) {
  347.         printf("Created thread tid=%d\n", tid);
  348.     }
  349.  
  350.     if ((tid = thread_create(utest, NULL, "utest")) != -1) {
  351.         printf("Created thread tid=%d\n", tid);
  352.     }
  353.  
  354.     int i;
  355.    
  356.     for (i = 0; i < 50000000; i++)
  357.         ;
  358.        
  359.     if (futex_up(&ftx) < 0)
  360.         printf("Futex failed.\n");
  361.  
  362.  
  363.     printf("Creating pseudo thread.\n");
  364.     stage = 1;
  365.     ptid = psthread_create(ptest, NULL);
  366.     printf("Main thread stage%d.\n", stage);
  367.     stage++;
  368.     psthread_schedule_next();;
  369.     printf("Main thread stage%d.\n", stage);
  370.     stage++;
  371.     psthread_schedule_next();;
  372.     printf("Main thread stage%d.\n", stage);
  373.  
  374.     psthread_join(ptid);
  375.  
  376.     printf("Main thread exiting.\n");
  377. */
  378.     return 0;
  379. }
  380.