subst.c 50.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234
/*
 * Copyright (C) 1998 by the Board of Trustees
 *    of Leland Stanford Junior University.
 * Copyright (C) 1998 Digital Equipment Corporation
 *
 * This file is part of the SimOS distribution.
 * See LICENSE file for terms of the license.
 *
 */

/*
 * Substitute the system calls by AINT equivalents.
 */

#include <stdio.h>
#include <stdlib.h>
#include <syscall.h>
#include <errno.h>
#include <string.h>
#include <sys/resource.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/utsname.h>
#include <sys/sysinfo.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <machine/hal_sysinfo.h>
#include <math.h>

#include "gamma.h"
#include "aint.h"
#include "sim.h"
#include "globals.h"
#include "opcodes.h"
#include "subst.h"
#include "protos.h"
#include "alpha_regs.h"


#define v0 REG_V0
#define a0 REG_A0
#define a1 REG_A1
#define a2 REG_A2
#define a3 REG_A3
#define a4 REG_A4

#define _arg1 16
#define _arg2 17
#define _arg3 18
#define _arg4 19
#define _arg5 20
#define _arg6 21


extern struct timeval start_real_time;
extern struct timezone tz;
aint_time_t virtual_time;


syscall_substitute syscall_table[LAST];

SyscallStatus aint_ioctl( thread_ptr pthread);
SyscallStatus aint_fcntl( thread_ptr pthread);
SyscallStatus aint_statfs( thread_ptr pthread);
SyscallStatus aint_lstat( thread_ptr pthread);
SyscallStatus aint_getdirentries( thread_ptr pthread);
SyscallStatus aint_close( thread_ptr pthread);
SyscallStatus aint_write( thread_ptr pthread);
SyscallStatus aint_read( thread_ptr pthread);
SyscallStatus aint_lseek( thread_ptr pthread);
SyscallStatus aint_exit( thread_ptr pthread);
SyscallStatus aint_fork( thread_ptr pthread);
SyscallStatus aint_open( thread_ptr pthread);
SyscallStatus aint_stat( thread_ptr pthread);
SyscallStatus aint_fstat( thread_ptr pthread);
SyscallStatus aint_brk( thread_ptr pthread);
SyscallStatus aint_getpagesize( thread_ptr pthread);
SyscallStatus aint_gettimeofday( thread_ptr pthread);
SyscallStatus aint_shmget( thread_ptr pthread);
SyscallStatus aint_shmat( thread_ptr pthread);
SyscallStatus aint_shmdt( thread_ptr pthread);
SyscallStatus aint_shmctl( thread_ptr pthread);
SyscallStatus aint_wait4( thread_ptr pthread);
SyscallStatus aint_getdtablesize( thread_ptr pthread);
SyscallStatus aint_getrusage( thread_ptr pthread);
SyscallStatus aint_getpid( thread_ptr pthread);
SyscallStatus aint_getuid( thread_ptr pthread);
SyscallStatus aint_usleep( thread_ptr pthread);
SyscallStatus aint_munmap( thread_ptr pthread);
SyscallStatus aint_mmap( thread_ptr pthread);
SyscallStatus aint_access( thread_ptr pthread);
SyscallStatus aint_gethostname( thread_ptr pthread);
SyscallStatus aint_uname( thread_ptr pthread);
SyscallStatus aint_flock( thread_ptr pthread);
SyscallStatus aint_signal( thread_ptr pthread);
SyscallStatus aint_sigaction( thread_ptr pthread);
SyscallStatus aint_pipe( thread_ptr pthread);
SyscallStatus aint_getsysinfo( thread_ptr pthread);
SyscallStatus aint_setsysinfo( thread_ptr pthread);
SyscallStatus aint_sigprocmask( thread_ptr pthread);
SyscallStatus aint_setrlimit( thread_ptr pthread);
SyscallStatus aint_getrlimit( thread_ptr pthread);
SyscallStatus aint_setsid( thread_ptr pthread);
SyscallStatus aint_chdir( thread_ptr pthread);
SyscallStatus aint_getgid( thread_ptr pthread);
SyscallStatus aint_getdomainname( thread_ptr pthread);
SyscallStatus aint_setuid( thread_ptr pthread);
SyscallStatus aint_madvise( thread_ptr pthread);
SyscallStatus aint_umask( thread_ptr pthread);
SyscallStatus aint_table( thread_ptr pthread);
SyscallStatus aint_unlink( thread_ptr pthread);
SyscallStatus aint_syscall( thread_ptr pthread);
SyscallStatus aint_dup( thread_ptr pthread);
SyscallStatus aint_pid_block( thread_ptr pthread);
SyscallStatus aint_pid_unblock( thread_ptr pthread);
SyscallStatus aint_kill( thread_ptr pthread);
SyscallStatus aint_sigstack( thread_ptr pthread);

int copy_string(thread_ptr pthread, char *buffer, long pointer);
int xaddr_space_strlen(thread_ptr pthread, long pointer);
int free_addr_space(thread_ptr pthread);
void init_thread (thread_ptr pthread, event_ptr pevent, int enqueue);
void copy_argv (thread_ptr pthread, int arg_start, int argc, char **argv,
		char **envp);
void page_unmap(thread_ptr pthread, ulong addr);
SyscallStatus schedule_cleanup( thread_ptr pthread);

/* This function initializes icodes used for implementing exit() */

void
subst_init ()
{

    /*
     * Initialize the system call table for only the supported system calls. 
     */
    syscall_table[SYS_exit] = &aint_exit;
    syscall_table[SYS_ioctl] = &aint_ioctl;
    syscall_table[SYS_fcntl] = &aint_fcntl;
    syscall_table[SYS_statfs] = &aint_statfs;
    syscall_table[SYS_lstat] = &aint_lstat;
    syscall_table[SYS_getdirentries] = &aint_getdirentries;
    syscall_table[SYS_close] = &aint_close;
    syscall_table[SYS_open] = &aint_open;
    syscall_table[SYS_stat] = &aint_stat;
    syscall_table[SYS_fstat] = &aint_fstat;
    syscall_table[SYS_write] = &aint_write;
    syscall_table[SYS_read] = &aint_read;
    syscall_table[SYS_lseek] = &aint_lseek;
    syscall_table[SYS_fork] = &aint_fork;
    syscall_table[SYS_obreak] = &aint_brk;
    syscall_table[SYS_getpagesize] = &aint_getpagesize;
    syscall_table[SYS_gettimeofday] = &aint_gettimeofday;
    syscall_table[SYS_shmget] = &aint_shmget;
    syscall_table[SYS_shmat] = &aint_shmat;
    syscall_table[SYS_shmdt] = &aint_shmdt;
    syscall_table[SYS_shmctl] = &aint_shmctl;
    syscall_table[SYS_wait4] = &aint_wait4;
    syscall_table[SYS_getdtablesize] = &aint_getdtablesize;
    syscall_table[SYS_getrusage] = &aint_getrusage;
    syscall_table[SYS_getpid] = &aint_getpid;
    syscall_table[SYS_getuid] = &aint_getuid;
    syscall_table[SYS_usleep_thread] = &aint_usleep;
    syscall_table[SYS_execve] = &aint_execve;
    syscall_table[SYS_munmap] = &aint_munmap;
    syscall_table[SYS_mmap] = &aint_mmap;
    syscall_table[SYS_access] = &aint_access;
    syscall_table[SYS_gethostname] = &aint_gethostname;
    syscall_table[SYS_uname] = &aint_uname;
    syscall_table[SYS_flock] = &aint_flock;
    syscall_table[SYS_signal] = &aint_signal;
    syscall_table[SYS_sigaction] = &aint_sigaction;
    syscall_table[SYS_pipe] = &aint_pipe;
    syscall_table[SYS_getsysinfo] = &aint_getsysinfo;
    syscall_table[SYS_setsysinfo] = &aint_setsysinfo;
    syscall_table[SYS_sigprocmask] = &aint_sigprocmask;
    syscall_table[SYS_setrlimit] = &aint_setrlimit;
    syscall_table[SYS_getrlimit] = &aint_getrlimit;
    syscall_table[SYS_setsid] = &aint_setsid;
    syscall_table[SYS_chdir] = &aint_chdir;
    syscall_table[SYS_getgid] = &aint_getgid;
    syscall_table[SYS_getdomainname] = &aint_getdomainname;
    syscall_table[SYS_setuid] = &aint_setuid;
    syscall_table[SYS_madvise] = &aint_madvise;
    syscall_table[SYS_umask] = &aint_umask;
    syscall_table[SYS_table] = &aint_table;
    syscall_table[SYS_unlink] = &aint_unlink;
    syscall_table[SYS_syscall] = &aint_syscall;
    syscall_table[SYS_dup] = &aint_dup;
    syscall_table[SYS_pid_block] = &aint_pid_block;
    syscall_table[SYS_pid_unblock] = &aint_pid_unblock;
    syscall_table[SYS_kill] = &aint_kill;
    syscall_table[SYS_sigstack] = &aint_sigstack;
}

/*
 * All the system calls are faked by calling AINT substitues.
 * The calling conventions assumed:
 *          func(arg1,arg2,arg3,...) means that
 *             reg16 = arg1, reg17 = arg2 etc
 */
SyscallStatus
callsys_f( thread_ptr pthread)
{
    syscall_substitute aint_syscall;
    
    /* Reset the process's lock_flag if set */
    pthread->lock_flag = 0;

    /* Lookup the system call in the table */
    aint_syscall = syscall_table[pthread->st.reg[v0]];

#ifdef DEBUG 
    fprintf(stderr, "%d: system call %d\n",
	    pthread->pid, pthread->st.reg[v0]);
#endif

    if (aint_syscall == NULL) {
	fatal("Unsupported system call: %d\n", pthread->st.reg[v0]);
	return SYSCALL_EXIT;
    } else {
	return (*aint_syscall)(pthread);
    }
}

/*
 * ioctl(
 * 	  int d,
 * 	  unsigned long	request,
 * 	  void * arg );
 */
SyscallStatus
aint_ioctl( thread_ptr pthread)
{
    /* The fd is in a0, the command in a1, and the output buf in a2 */
    char mybuf[255];
    int vfd, pfd, len;

    vfd = pthread->st.reg[_arg1];
    pfd = pthread->fd[vfd];

    len = (pthread->st.reg[_arg2] >> 16) & 0x1fff;

    if ((pthread->st.reg[_arg2] >> 28) & 0x2) {
	int i;
	for (i = 0; i < len; i++) {
	    mybuf[i] = *(char *) addr2phys (pthread, pthread->st.reg[_arg3] + i);
	}
    }

    pthread->st.reg[v0] = ioctl (pfd, pthread->st.reg[_arg2], mybuf);

    if (pthread->st.reg[v0] < 0) {
	pthread->st.reg[_arg4] = errno;
    }
    else
	pthread->st.reg[_arg4] = 0;

    /* if out parameter, then fill in the results */
    if ((pthread->st.reg[_arg2] >> 28) && 0x4) {
	int i;
	for (i = 0; i < len; i++) {
	    *(char *) addr2phys (pthread, pthread->st.reg[_arg3] + i) = mybuf[i];
	}
    }

    return SYSCALL_NEXT;

}

/*
 * fcntl(
 * 	  int d,
 * 	  int request [ ,
 *        int argument | struct flock *argument  ]  );
 */
SyscallStatus
aint_fcntl( thread_ptr pthread)
{
    char mybuf[255];
    int vfd, pfd, len;
    struct flock aint_flock;
    char *p, *q, *buffer_p;
    int i;
    
    vfd = pthread->st.reg[_arg1];
    pfd = pthread->fd[vfd];

    switch(pthread->st.reg[_arg2]) {
    case F_GETLK:
    case F_SETLK:
    case F_SETLKW:
	pthread->st.reg[v0] = fcntl (pfd,
				  pthread->st.reg[_arg2],
				  &aint_flock);
	/* copy the result */
	buffer_p = (char *) pthread->st.reg[_arg3];
	q = (char *) &aint_flock;
	for(i=0; i < sizeof(struct flock); i++,q++) {
	    char *p = (char *) addr2phys(pthread, (long) buffer_p+i);
	    *p = *q;
	}
	break;
    default:
	pthread->st.reg[v0] = fcntl (pfd,
				  pthread->st.reg[_arg2],
				  pthread->st.reg[_arg3]);
	break;
    }

    if (pthread->st.reg[v0] < 0) {
	pthread->st.reg[_arg4] = errno;
    }
    else
	pthread->st.reg[_arg4] = 0;

    return (SYSCALL_NEXT);
}

/*
 * int statfs( 
 *           char *path, 
 *           struct statfs *buffer, 
 *           int length ); 
 */
SyscallStatus
aint_statfs( thread_ptr pthread)
{
    char path_buf[MAX_PATH];
    struct statfs aint_buffer;
    char *buffer_p, *q;
    int i;
    
    /* use local variables for efficient access */
    buffer_p = (char *) pthread->st.reg[_arg2];
    q = (char *) &aint_buffer;
    
    /* copy the pointer arguments */
    copy_string(pthread, path_buf, pthread->st.reg[_arg1]);
    /* make the system call */
    pthread->st.reg[v0] = statfs(path_buf, &aint_buffer, pthread->st.reg[_arg3]);
    /* copy the results */
    for(i=0; i < sizeof(struct statfs); i++,q++) {
	char *p = (char *) addr2phys(pthread, (long) buffer_p+i);
	*p = *q;
    }

    if (pthread->st.reg[v0] < 0) {
	pthread->st.reg[_arg4] = errno;
    }
    else
	pthread->st.reg[_arg4] = 0;

    return SYSCALL_NEXT;
}

/*
 * int lstat(
 *         const char *path,
 *         struct stat *buffer );
 */
SyscallStatus
aint_lstat( thread_ptr pthread)
{
    char path_buf[MAX_PATH];
    struct stat aint_buffer;
    char *buffer_p, *q;
    int i;
    
    /* use local variables for efficient access */
    buffer_p = (char *) pthread->st.reg[_arg2];
    q = (char *) &aint_buffer;
    
    /* copy the pointer arguments */
    copy_string(pthread, path_buf, pthread->st.reg[_arg1]);
    /* make the system call */
    pthread->st.reg[v0] = lstat(path_buf, &aint_buffer);
    /* copy the results */
    for(i=0; i < sizeof(struct stat); i++,q++) {
	char *p = (char *) addr2phys(pthread, (long) buffer_p+i);
	*p = *q;
    }

    if (pthread->st.reg[v0] < 0) {
	pthread->st.reg[_arg4] = errno;
    }
    else
	pthread->st.reg[_arg4] = 0;

    return SYSCALL_NEXT;
}

/*
 * int getdirentries(
 *           int fd,
 *           char *buf,
 *           int nbytes,
 *           long *basep );
 */
SyscallStatus
aint_getdirentries( thread_ptr pthread)
{
    int vfd, pfd, nbytes;
    char *aint_buf;
    long base;

    /* get the file descriptor */
    vfd = pthread->st.reg[_arg1];
    pfd = pthread->fd[vfd];

    /* set up the buffer */
    nbytes = pthread->st.reg[_arg3];
    aint_buf = (char *) malloc(nbytes);
    if (aint_buf == NULL)
	fatal("Out of memory\n");
    
    /* set up the base pointer */
    base = * (long *) addr2phys(pthread, pthread->st.reg[_arg4]);

    /* make the call */
    pthread->st.reg[v0] = getdirentries(pfd, aint_buf, nbytes, &base);

    /* copy the output */
    * (long *) addr2phys(pthread, pthread->st.reg[_arg4]) = base;
    
    if (pthread->st.reg[v0] < 0) {
	pthread->st.reg[_arg4] = errno;
    }
    else
	pthread->st.reg[_arg4] = 0;

    return SYSCALL_NEXT;
    
}

/*
 * int close (
 *	  int filedes );
 */
SyscallStatus
aint_close( thread_ptr pthread)
{
    int vfd, pfd;

    vfd = pthread->st.reg[_arg1];
    pfd = pthread->fd[vfd];

#ifdef DEBUG
    fprintf(stderr, "close: v: %d, p: %d\n", vfd, pfd);
#endif /* DEBUG */

    /* nothing can be closed here, since other processes might have it open */
    pthread->st.reg[v0] = 0;

    /*
     * mark the file descriptor free only if greater than 2. We don't want
     * these descriptors viz. 0,1,2 to be reused for other files.
     */
    if (vfd > 2)
	pthread->fd[vfd] = -1;

    /* set the error if any */
    if (pthread->st.reg[v0] < 0) {
	pthread->st.reg[_arg4] = errno;
    }
    else
	pthread->st.reg[_arg4] = 0;

    return SYSCALL_NEXT;
}

/*
 * int open (
 *	  const	char *path,
 *	  int oflag [ ,
 *	  mode_t mode ]	);
 */
SyscallStatus
aint_open( thread_ptr pthread)
{
    int pfd, vfd, oflag;

    oflag = pthread->st.reg[_arg2];
    
    /* Search for an unused fd */
    for (vfd = 0; vfd < MAX_FDNUM; vfd++)
	if (pthread->fd[vfd] == -1)
	    break;

    if (vfd < MAX_FDNUM) {
	char filename[256], *c;
	int i;

	for (i = 0;; i++) {
	    c = (char *) addr2phys (pthread, pthread->st.reg[_arg1] + i);
	    filename[i] = *c;
	    if (*c == '\0')
		break;
	}

	/* open the files in the non blocking mode */
	pfd = open (filename, oflag | O_NONBLOCK,
		    pthread->st.reg[_arg3]);

	/* change the owner and the group to that of the running process */
#ifdef ORACLE 
	if (((oflag & O_WRONLY) || (oflag & O_RDWR)) && (pfd != -1))
	    if (fchown(pfd, pthread->uid, pthread->gid))
		perror("fchown");
#endif
	
	pthread->fd[vfd] = pfd;

	if (pfd == -1) {
	    pthread->st.reg[_arg4] = errno;
	    pthread->st.reg[v0] = -1;
	}
	else {
	    pthread->st.reg[_arg4] = 0;
	    pthread->st.reg[v0] = vfd;
	}

#ifdef DEBUG 
	fprintf(stderr, "%d: open: %s, pfd: %d\n",
		pthread->pid,
		filename, pfd);
#endif
	
    }
    else {
	pthread->st.reg[_arg4] = ENOMEM;
	pthread->st.reg[v0] = -1;
    }

    
    return SYSCALL_NEXT;
}

/*
 * ssize_t write(
 *          int filedes ,
 *          const void *buffer,
 *          size_t nbytes );
 */
SyscallStatus
aint_write( thread_ptr pthread)
{
    int vfd, pfd;
    int i, result;
    void *buffer;
    size_t nbytes;
    char *mybuf;

    vfd = pthread->st.reg[_arg1];
    pfd = pthread->fd[vfd];

#ifdef DEBUG
    fprintf(stderr, "write: v: %d, p: %d\n", vfd, pfd);
#endif /* DEBUG */

    buffer = (void *) pthread->st.reg[_arg2];
    nbytes = pthread->st.reg[_arg3];
    mybuf = (char *) malloc (nbytes + 1);

    for (i = 0; i < nbytes; i++) {
	mybuf[i] = *(char *) addr2phys (pthread, (long) buffer + i);
    }

    result = write (pfd, (caddr_t) mybuf, nbytes);

    /*
     * We assume that a result of -1 is returned only when a process blocks
     * on writing a pipe. In that case, it will be put on a pipe_q and woken
     * up on a subsequent read. The process, then retries the write.
     */

    if (result == -1) {

#ifdef DEBUG
	perror("aint_write");
#endif 
	/* clean up */
	free(mybuf);
	/* block */
	event_block(pthread, E_BLOCK);
	block_thr(&write_pipe_q, pthread);
	return SYSCALL_SAME;
    } else {
	/* wake up everybody on the write queue */
	unblock_list(pthread, &read_pipe_q);
    }

    pthread->st.reg[v0] = result;
    pthread->st.reg[_arg4] = 0;
    free(mybuf);

    return SYSCALL_NEXT;
}

/*
 * ssize_t read(
 *	  int filedes,
 *	  void *buffer,
 *	  size_t nbytes	);
 */
SyscallStatus
aint_read( thread_ptr pthread)
{

    int pfd, vfd;
    void *buffer;
    size_t nbytes;
    char *mybuf;
    int i,result;

    
    vfd = pthread->st.reg[_arg1];
    pfd = pthread->fd[vfd];
    
    buffer = (void *) pthread->st.reg[_arg2];
    nbytes = pthread->st.reg[_arg3];
    mybuf = (char *) malloc (nbytes + 1);

    result = read (pfd, mybuf, nbytes);

    /*
     * We assume that a result of -1 is returned only when a process blocks
     * on reading a pipe. In that case, it will be put on a pipe_q and woken
     * up on a subsequent write. The process, then retries the read.
     */
    if (result == -1) {
	/* clean up */
	free(mybuf);
	/* block */
	event_block(pthread, E_BLOCK);
	block_thr(&read_pipe_q, pthread);

#ifdef DEBUG_BLOCK
    fprintf(stderr, "%d: read: v: %d, p: %d - blocking\n",
	    pthread->pid, vfd, pfd);
#endif /* DEBUG */

	return SYSCALL_SAME;
    } else {
	/* wake up everybody on the write queue */
	unblock_list(pthread, &write_pipe_q);
    }

#ifdef DEBUG
    fprintf(stderr, "read: v: %d, p: %d\n", vfd, pfd);
#endif /* DEBUG */

    for (i = 0; i < result; i++)
	*(char *) addr2phys (pthread, (long) buffer + i)
	    = mybuf[i];

    pthread->st.reg[v0] = result;
    pthread->st.reg[_arg4] = 0;
    free (mybuf);
    return SYSCALL_NEXT;
}

/*
 * off_t lseek (
 *	  int filedes,
 *	  off_t	offset,
 *	  int whence );
 */
SyscallStatus
aint_lseek( thread_ptr pthread)
{

    int vfd, pfd;

    vfd = pthread->st.reg[_arg1];
    pfd = pthread->fd[vfd];

    if (pfd > 2) {
	pthread->st.reg[v0] = lseek (pfd, pthread->st.reg[_arg2],
				  pthread->st.reg[_arg3]);
    }
    else {
	pthread->st.reg[v0] = lseek (pfd, 0, SEEK_CUR);
    }

    pthread->st.reg[_arg4] = 0;

    return SYSCALL_NEXT;
}

/*
 * void exit (
 *	  int status );
 */
SyscallStatus
aint_exit( thread_ptr pthread)
{

#ifdef DEBUG
    fprintf(stderr, "%d: exiting\n", pthread->pid);
#endif /* DEBUG */    

    pthread->exitcode = pthread->st.reg[_arg1];
    return SYSCALL_EXIT;
}

/*
 * pid_t fork ( void );
 */
SyscallStatus
aint_fork( thread_ptr pthread)
{
    int i;
    thread_ptr child;
    int pid;
    event_ptr pevent;
    SyscallStatus pcopy;

    /* allocate a new thread */
    INLINE_DEQUEUE (&free_q, child);

    if (child == NULL)
	fatal ("aint_fork: exceeded process limit (%d)\n", max_nprocs);

    NEW_ITEM (event_free, sizeof (event_t), pevent, "aint_fork");

    /* Ensure that each newly created thread has a unique pid */
    max_pid++;
    child->pid = pid = max_pid;
    nprocs++;

    if (nprocs > max_nprocs)
	fatal ("aint_fork: exceeded process limit (%d)\n", max_nprocs);

    /* copy the parent's effective uid and gid */
    child->uid = pthread->uid;
    child->gid = pthread->gid;
    
    /* copy the parent's address space to the child */
    copy_addr_space (pthread, child);

    /*
     * init_thread() must be called *after* copy_addr_space() since
     * init_thread() uses the mapping fields set up by copy_addr_space().
     */
    init_thread (child, pevent, 1);

    /* the child's start time is the parent's plus the time for the fork */
#ifdef SOLO
    child->time = 1;
#else
    child->time = pthread->time + picode->cycles;
#endif

    child->parent = pthread;

    /* set the header information for the child */
    child->headerp = pthread->headerp;

    /* set the progarm text */
    child->itext = pthread->itext;
    
    /* link in the child to the parent's list of children */
    child->sibling = pthread->youngest;
    pthread->youngest = child;

    /* the first instruction for the child is to return 
       child->picode = T2I(pthread->st.reg[31]); */
#ifndef SOLO
    child->picode = picode->next;
#endif

    /* the return value for the parent is the child's pid */
    pthread->st.reg[v0] = pid + PID_OFFSET;
    pthread->st.reg[_arg4] = 0;

    /* the return value for the child is 0 */
    child->st.reg[v0] = 0;
    child->st.reg[_arg4] = 0;

    /* Set child flags - r20 is 0 for parent and 1 for child */
    pthread->st.reg[_arg5] = 0;
    child->st.reg[_arg5] = 1;

    /* copy the parent's signal handler vectors */
    for (i = 0; i < MAX_SIGNALS; i++) {
	child->sigv[i].sv_handler = pthread->sigv[i].sv_handler;
	child->sigv[i].sv_mask = pthread->sigv[i].sv_mask;
	child->sigv[i].sv_flags = pthread->sigv[i].sv_flags;
    }
    child->sigblocked = pthread->sigblocked;

    /* copy parent's file descriptors */
    for(i = 0; i < MAX_FDNUM; i++) {
	if (i < 3)
	    child->fd[i] = i;
	else {
	    child->fd[i] = pthread->fd[i];
	}
    }

#ifdef ORACLE
    fprintf(stderr, "%d: fork: created %d\n", pthread->pid, child->pid);
#endif /* DEBUG */    

    /*
     * Return from the parent. If generating any events, then first
     * generate a fork event. The picode returned must be the one that
     * calls event_fork...
     */

    /* We first check if an ifork icode has been introduced at this point */

#ifdef SOLO
    child->st.PC = pthread->st.PC + 4;

    GammaNewThread(child);
    return SYSCALL_NEXT;
#else
    if (picode->next->func == event_fork) {
	child->picode = child->picode->next;
	return SYSCALL_NEXT;
    }
    else {
	/* add a copy of ifork here */
	pcopy = (SyscallStatus) malloc (sizeof (struct icode));
	memcpy (pcopy, &ifork, sizeof (struct icode));
	pcopy->next = picode->next;
	pcopy->addr = picode->addr;
	picode->next = pcopy;
	return pcopy;
    }
#endif
}

/*
 * int stat(
 *	  const	char *path,
 *	  struct stat *buffer );
 */
SyscallStatus
aint_stat( thread_ptr pthread)
{
    char filename[256], *c;
    unsigned char *statbuf = (unsigned char *) malloc (sizeof (struct stat));
    int i;

    /* Copy filename */
    for (i = 0;; i++) {
	c = (char *) addr2phys (pthread, pthread->st.reg[_arg1] + i);
	filename[i] = *c;
	if (*c == '\0')
	    break;
    }

    pthread->st.reg[v0] = stat (filename, (struct stat *) statbuf);

#ifdef DEBUG
    fprintf(stderr, "stat: %s = %d\n", filename, pthread->st.reg[v0]);
#endif

    /* Copy struct stat */
    for (i = 0; i < sizeof (struct stat); i++) {
	*(unsigned char *) addr2phys (pthread, pthread->st.reg[_arg2] + i)
	    = statbuf[i];
    }

    pthread->st.reg[_arg4] = pthread->st.reg[v0] ? errno : 0;

    free ((void *) statbuf);

    return SYSCALL_NEXT;
}

/*
 * int fstat(
 *	  int filedes,
 *	  struct stat *buffer );
 */
SyscallStatus
aint_fstat( thread_ptr pthread)
{
    int vfd, pfd;
    unsigned char *statbuf = (unsigned char *) malloc (sizeof (struct stat));
    int i;

    vfd = pthread->st.reg[_arg1];
    pfd = pthread->fd[vfd];

    pthread->st.reg[v0] = fstat (pfd, (struct stat *) statbuf);

    /* Copy struct stat */
    for (i = 0; i < sizeof (struct stat); i++) {
	*(unsigned char *) addr2phys (pthread, pthread->st.reg[_arg2] + i)
	    = statbuf[i];
    }

    pthread->st.reg[_arg4] = 0;

    free ((void *) statbuf);

    return SYSCALL_NEXT;
}

/*
 * int brk(
 *	  void *addr );
 */
SyscallStatus
aint_brk( thread_ptr pthread)
{
    ulong new_brk_address = pthread->st.reg[_arg1];
    
    /* set the new break address */
    pthread->headerp->total_data_size = new_brk_address;

    /* report `all clear' */
    pthread->st.reg[v0] = 0;
    pthread->st.reg[_arg4] = 0;

    return SYSCALL_NEXT;
}

/*
 * int getpagesize ( void );
 */
SyscallStatus
aint_getpagesize( thread_ptr pthread)
{
    pthread->st.reg[v0] = getpagesize ();
    pthread->st.reg[_arg4] = 0;
    return SYSCALL_NEXT;
}

/*
 * int gettimeofday (
 *	  struct timeval *tp,
 *	  struct timezone *tzp );
 *
 * Returns the virtual time, rather than the `real' time.
 */
SyscallStatus
aint_gettimeofday( thread_ptr pthread)
{
    struct timeval tval;
    int i;


    pthread->st.reg[v0] = 0;

    /* copy the start time */
    bcopy((char *) &start_real_time, (char *) &tval, sizeof(struct timeval));

    /* Add the virtual time in seconds */
    tval.tv_sec += nint (virtual_time / CLOCKS_PER_SECOND);
    
    /* Copy the timeval and timezone structs */
    for (i = 0; i < sizeof (struct timeval); i++) {
	*(char *) addr2phys (pthread, pthread->st.reg[_arg1] + i)
	    = *(char *) ((char *) &tval + i);
    }

    if (pthread->st.reg[_arg2] != 0) {
	unsigned long p17 = pthread->st.reg[_arg2];
	for (i = 0; i < sizeof (struct timezone); i++) {
	    *(char *) addr2phys (pthread, p17 + i)
		= *(char *) ((char *) &tz + i);
	}
    }

    if (pthread->st.reg[v0])
	pthread->st.reg[_arg4] = errno;
    else
	pthread->st.reg[_arg4] = 0;

    return SYSCALL_NEXT;
}

/*
 * pid_t wait4 (
 *	  pid_t	process_id
 *	  union	wait *status_location,
 *	  int options,
 *	  struct rusage	*resource_usage	);
 */
SyscallStatus
aint_wait4( thread_ptr pthread)
{
    pid_t pid;
    int *status_location;
    int options;
    struct rusage *resource_usage;
    int childpid;
    static int sync_children = 1;     /* toggled each time */

    /*
     * force an explicit synchronization with the children
     * - this is an imprecise solution. Does not take care of the race
     * conditions arising out of multiple threads calling aint_wait4. For the
     * correct implementation, there should be a sync_children per thread.
     */
#ifndef SOLO
    if (sync_children) {
	sync_children = 0;
	event_yield(picode, pthread);
	return SYSCALL_SAME;
    } else {
	sync_children = 1;
    }
    
#endif
    pid = pthread->st.reg[_arg1];
    if (pid >= 0)
	pid -= PID_OFFSET;
   
    if (pthread->st.reg[_arg2]) {
	status_location = (int *) PMAP(pthread->st.reg[_arg2]);
    } else {
	status_location = (int *) NULL;
    }

    options = pthread->st.reg[_arg3];

    if (pthread->st.reg[_arg4]) {
	resource_usage = (struct rusage *) PMAP(pthread->st.reg[_arg4]);
    } else {
	resource_usage = (struct rusage *) NULL;
    }

    if ((pid == -1) && (options == 0)) {
	childpid = wait_for_child (pthread);
	if (childpid == 0) {
#ifdef SOLO
	   GammaStall(pthread,cpu_blocked);
	   return SYSCALL_NEXT;
#endif
	    event_block (pthread, E_WAIT);
	    sleep_thr (pthread);
	    pthread->runstate = R_WAIT;
	    return SYSCALL_SAME;
	}
	pthread->st.reg[v0] = childpid + PID_OFFSET;
	if (childpid == -1)
	    pthread->st.reg[_arg4] = ECHILD;
	else if (status_location)
	    *status_location = threads[childpid].exitcode;
	return SYSCALL_NEXT;
    }
    else {
	fatal ("aint_wait4(): unimplemented option %d\n", options);
    }
}

/*
 * int getdtablesize(void);
 */
SyscallStatus
aint_getdtablesize( thread_ptr pthread)
{
    pthread->st.reg[v0] = MAX_FDNUM;
    pthread->st.reg[_arg4] = 0;
    return SYSCALL_NEXT;
}

/*
 * int getrusage(
 *         int who,
 *         struct rusage *r_usage );
 */
SyscallStatus
aint_getrusage( thread_ptr pthread)
{
    struct rusage mybuf;
    int i;

    pthread->st.reg[v0] = getrusage (pthread->st.reg[_arg1], &mybuf);

    /* Copy the struct rusage */
    for (i = 0; i < sizeof (struct rusage); i++)
	*(unsigned char *) addr2phys (pthread, pthread->st.reg[_arg2] + i)
	    = *(unsigned char *) ((long) &mybuf + i);

    pthread->st.reg[_arg4] = 0;
    return SYSCALL_NEXT;
}

#ifdef CALL_TRACE
extern int call_trace_enable;
#endif

/*
 *  pid_t getpid(void );
 */
SyscallStatus
aint_getpid( thread_ptr pthread)
{
#ifdef DEBUG
    fprintf(stderr, "%d: getpid\n", pthread->pid);
#endif
    
    /*
     * return pids starting from 3000 - having low pids could be a problem
     * for some applications.
     */
    pthread->st.reg[v0] = PID_OFFSET + pthread->pid;
    pthread->st.reg[_arg4] = 0;
    return SYSCALL_NEXT;
}

/*
 * uid_t getuid(void);
 *
 * Should return uid in v0 and effective uid in a4. Thanks to the hidden
 * API. 
 */
SyscallStatus
aint_getuid( thread_ptr pthread)
{
    pthread->st.reg[v0] = getuid();
    pthread->st.reg[a4] = pthread->uid;

#ifdef DEBUG    
    fprintf(stderr, "uid: %d, effective uid %d\n",
	    pthread->st.reg[v0], pthread->st.reg[a4]);
#endif
    
    pthread->st.reg[_arg4] = 0;
    return SYSCALL_NEXT;
}

/*
 * void usleep(
 *	  unsigned mseconds );
 */	  
SyscallStatus
aint_usleep( thread_ptr pthread)
{
    ulong secs = pthread->st.reg[_arg1] * 1000000;
    
    event_timed_block(pthread, E_BLOCK, secs *CLOCKS_PER_SECOND);
    block_thr(&sleep_q, pthread);

    pthread->st.reg[v0] = 0;
    pthread->st.reg[_arg4] = 0;

    return SYSCALL_NEXT;
}

/*
 * int execve (
 *       const char *path,
 *       char * const argv[ ],
 *       char * const envp[ ] );
 */	  
SyscallStatus
aint_execve( thread_ptr pthread)
{
 
#ifdef SOLO
   CPUError("forget aint_execve for now \n");
   return 0;
#else
   char *path_buf,**argv_buf,**env_buf;
    int i,argc;
    long *pp;
    event_ptr pevent;
    
    long path_ptr = pthread->st.reg[_arg1];
    long *argv_ptr = (long *) pthread->st.reg[_arg2];
    long *env_ptr = (long *) pthread->st.reg[_arg3];
    
    /* allocate memory for the buffers */
    path_buf = (char *) malloc(MAX_PATH);
    argv_buf = (char **) malloc(MAX_ARGV);
    env_buf = (char **) malloc(MAX_ENV);

    
    /* copy the pathname */
    copy_string(pthread, path_buf,path_ptr);

#ifdef DEBUG
    fprintf(stderr, "execve: %s\n", path_buf);
#endif /* DEBUG */

    /* copy argv */
    pp = (long *) addr2phys(pthread,(long) argv_ptr);
    for(i=0; *pp ; i++) {
	/* find out how much space we need */
	argv_buf[i] = malloc(xaddr_space_strlen(pthread, (long) *pp));
	/* copy the ith argument */
	copy_string(pthread, argv_buf[i], (long) *pp);
	pp = (long *) addr2phys(pthread,(long) ++argv_ptr);
    }
    argv_buf[i] = '\0';
    argc = i;
    
    /* copy env */
    pp =  (long *) addr2phys(pthread,(long) env_ptr);
    for(i=0; *pp ; i++) {
	/* find out how much space we need */
	env_buf[i] = malloc(xaddr_space_strlen(pthread, (long) *pp));
	/* copy the ith argument */
	copy_string(pthread, env_buf[i], (long) *pp);
	pp = (long *) addr2phys(pthread, (long) ++env_ptr);
    }
    env_buf[i] = '\0';

    /* free the existing address space */
    free_addr_space(pthread);

    /* get a new event structure */
    NEW_ITEM (event_free, sizeof (event_t), pevent, "aint_fork");
    
    /* initialize the thread, but don't enqueue it */
    init_thread(pthread, pevent, 0);

    /* read the headers from the object file */
    read_hdrs(path_buf, pthread);

    /* read the text */
    read_text(path_buf, pthread);

    /* create address space */
    create_addr_space(pthread);

    /* copy the arguments */
    copy_argv(pthread, 0, argc, argv_buf, env_buf);

    /* The new thread is ready for execution */
    return (pthread->picode);
#endif
}


/*
 * Given a buffer and a pointer in the simulated virtual address
 * space, this copies a string pointed by pointer into buffer in
 * AINT address space
 */
copy_string(thread_ptr pthread, char *buffer, long pointer)
{
    int i;
    char *p;

    /* translate from simulated to AINT address space */
    p = (char *) addr2phys(pthread,pointer);
    for(i=0; *p ; i++) {
	/* store a character */
	buffer[i] = *p;
	/* translate from simulated to AINT address space */
	p = (char *) addr2phys(pthread,++pointer);
    }
    /* put a null character */
    buffer[i] = '\0';
}

/* A cross address space strlen */
xaddr_space_strlen(thread_ptr pthread, long pointer)
{
    int i;
    char *p;
    
    /* translate from simulated to AINT address space */
    p = (char *) addr2phys(pthread,pointer);
    for(i=0; *p ; i++) {
	/* translate from simulated to AINT address space */
	p = (char *) addr2phys(pthread,++pointer);
    }

    /* plus the null character */
    return i+1;
}

/*
 * int munmap (
 *	  caddr_t addr,
 *	  size_t len );
 */
SyscallStatus
aint_munmap( thread_ptr pthread)
{
    int num_pages;
    int i;

    long addr = pthread->st.reg[_arg1];
    long len = pthread->st.reg[_arg2];
    
    /* round up the length to the next multiple of the page size */
    num_pages = len / TB_PAGESIZE;

    for ( i=0; i < num_pages ; i++) {
	page_unmap(pthread, addr + i * TB_PAGESIZE);
    }

    /* indicate that all is well */
    pthread->st.reg[v0] = 0;
    pthread->st.reg[_arg4] = 0;

    return SYSCALL_NEXT;
}

/*
 * caddr_t mmap (
 *	  caddr_t addr,
 *	  size_t len,
 *	  int prot,
 *	  int flags,
 *	  int filedes,
 *	  off_t	off );
 *
 * Only Anonymous pages are supported.
 */
SyscallStatus
aint_mmap( thread_ptr pthread)
{
    caddr_t addr = (caddr_t) pthread->st.reg[_arg1];
    size_t original_size, size = pthread->st.reg[_arg2];
    int prot = pthread->st.reg[_arg3];
    int flags = pthread->st.reg[_arg4];
    int filedes = pthread->st.reg[_arg5];
    off_t offset = pthread->st.reg[_arg6];
    ulong npages;
    int i;

    trace_flag = 1;
    
    /* A few sanity checks */
    if ( (filedes != -1) ^ ((flags & MAP_ANONYMOUS) == 0))  {
	fatal("File descriptor should be -1 for mapping anonymous pages");
    }

    original_size = size;
    size = ALIGN(size, TB_PAGESIZE);
    npages = size / TB_PAGESIZE;

    /*
     * If addr is zero, allocate from the end of the private area, expanding
       it further.
     */ 
    if ((addr == 0) && ((flags & MAP_FIXED) == 0)) {
	addr = (caddr_t) pthread->mmap_size + MMAP_AREA_START;
	pthread->mmap_size += size;
    }

    for(i=0; i < npages; i++) {
	ulong page, key, tag, page_start_addr;
	/* extract the relevant fields from the address */
	page_start_addr = (ulong) addr + TB_PAGESIZE * i;
	page = TB_VPAGE(page_start_addr);
	tag = TB_TAG(page);
	key = TB_KEY(page);	
	
	/* introduce a mapping if not alrady present */
	if (is_mapped(pthread, page_start_addr, page, key, tag)) 
	    page_unmap(pthread, page_start_addr);
	
	if ((flags & MAP_ANONYMOUS) == 0) {
	    int pfd = pthread->fd[filedes];
	    /*
	     * mmap fails because of the rounding up of the size. So let's
	     * keep it exact and manipulate the last call
	     */
	    if (i != npages - 1)
		mmap_tlb_miss(pthread,page_start_addr, page, key, tag,
			      TB_PAGESIZE, prot, pfd,
			      flags, offset+TB_PAGESIZE * i);
	    else
		mmap_tlb_miss(pthread,page_start_addr, page, key, tag,
			      TB_PAGESIZE - (size - original_size),prot,
			      pfd, flags, offset + TB_PAGESIZE * i);
					      
	} else {
	    tlb_miss(pthread, page_start_addr, page, key, tag, 1);
	}
    }
    
    /* put the return value in */
    pthread->st.reg[v0] = (ulong) addr;
    pthread->st.reg[_arg4] = 0;

    return SYSCALL_NEXT;
}

/*
 * int access (
 *           const char *path,
 *           int access_mode) ;
 */ 
SyscallStatus
aint_access( thread_ptr pthread)
{
    char path_buf[MAX_PATH];

    /* copy the pointer arguments */
    copy_string(pthread, path_buf, pthread->st.reg[_arg1]);
    /* make the system call */
    pthread->st.reg[v0] = access(path_buf, pthread->st.reg[_arg2]);
    
    if (pthread->st.reg[v0] < 0) {
	pthread->st.reg[_arg4] = errno;
    }
    else
	pthread->st.reg[_arg4] = 0;

    return SYSCALL_NEXT;
}

/*
 * int gethostname (
 *         char *address,
 *         int address_len );
 */
SyscallStatus
aint_gethostname( thread_ptr pthread)
{
    int address_len;
    int i;
    char *aint_buffer, *p, *q;

    /* set up the buffer */
    address_len = pthread->st.reg[_arg2];
    aint_buffer = (char *) malloc(address_len);
    if (aint_buffer == NULL)
	fatal("Out of memory\n");

    /* make the call */
    pthread->st.reg[v0] = gethostname(aint_buffer, address_len);

    
    /* copy the output */
    q = aint_buffer;
    for(i=0; i < address_len; i++,q++) {
	char *p = (char *) addr2phys(pthread, (long) aint_buffer + i);
	*p = *q;
    }
    
    if (pthread->st.reg[v0] < 0) {
	pthread->st.reg[_arg4] = errno;
    }
    else
	pthread->st.reg[_arg4] = 0;
    
    return SYSCALL_NEXT;
}

/*
 * int uname (
 *          struct utsname *name );
 */
SyscallStatus
aint_uname( thread_ptr pthread)
{
    struct utsname aint_buffer;
    char *buffer_p, *q;
    int i;
    
    /* use local variables for efficient access */
    buffer_p = (char *) pthread->st.reg[_arg1];
    q = (char *) &aint_buffer;
    
    /* make the system call */
    pthread->st.reg[v0] = uname(&aint_buffer);

    /* copy the results */
    for(i=0; i < sizeof(struct utsname); i++,q++) {
	char *p = (char *) addr2phys(pthread, (long) buffer_p+i);
	*p = *q;
    }

    if (pthread->st.reg[v0] < 0) {
	pthread->st.reg[_arg4] = errno;
    }
    else
	pthread->st.reg[_arg4] = 0;

    return SYSCALL_NEXT;
}

/*
 * int flock(                
 *           int filedes,    
 *           int operation );
 */
SyscallStatus
aint_flock( thread_ptr pthread)
{
    int vfd, pfd;

    vfd = pthread->st.reg[_arg1];
    pfd = pthread->fd[vfd];
    
    /* make the system call */
    pthread->st.reg[v0] = flock(pfd, pthread->st.reg[_arg2]);

    if (pthread->st.reg[v0] < 0) {
	pthread->st.reg[_arg4] = errno;
    }
    else
	pthread->st.reg[_arg4] = 0;

    return SYSCALL_NEXT;
}


/*
 * void (*signal (                            
 *           int sig,                       
 *           void (*function) (int))) (int);
 */
SyscallStatus
aint_signal( thread_ptr pthread)
{
    int sig = pthread->st.reg[_arg1];

#ifdef DEBUG
    fprintf(stderr,"signal handler for %d\n",sig);
#endif /* DEBUG */

    /* report `all clear' */
    pthread->st.reg[v0] = 0;
    pthread->st.reg[_arg4] = 0;
    
    return SYSCALL_NEXT;
}

/* 
 *   int sigaction (
 * 	    int signal,
 * 	    const struct sigaction *action,
 *	    struct sigaction *o_action );
 */
SyscallStatus
aint_sigaction( thread_ptr pthread)
{
    int signal = pthread->st.reg[_arg1];

#ifdef DEBUG
    fprintf(stderr,"signal handler for %d\n",signal);
#endif /* DEBUG */

    /* report `all clear' */
    pthread->st.reg[v0] = 0;
    pthread->st.reg[_arg4] = 0;
    
    return SYSCALL_NEXT;
}

/*   
 *   int pipe (
 * 	    int filedes[2] );
 *  After looking into the OSF sources, it became clear that the file
 *  descriptors are passed out in regs v0 and a4 respectively and not in
 *  memory.     
 */
SyscallStatus
aint_pipe( thread_ptr pthread)
{
    int vfd0, vfd1;
    ulong address = pthread->st.reg[_arg1];
    int aint_fd[2];
    
    /* make the system call */
    pthread->st.reg[v0] = pipe(aint_fd);
    /* make them non blocking */
    fcntl(aint_fd[0], F_SETFL, O_NONBLOCK);
    fcntl(aint_fd[1], F_SETFL, O_NONBLOCK);
    
    if (pthread->st.reg[v0] < 0) {
	pthread->st.reg[_arg4] = errno;
    }
    else
	pthread->st.reg[_arg4] = 0;

    /* first look for unused file descriptors */
    for (vfd0 = 0; vfd0 < MAX_FDNUM; vfd0++)
	if (pthread->fd[vfd0] == -1)
	    break;

    /* introduce the pthread fd to AINT fd mapping */
    pthread->fd[vfd0] = aint_fd[0];
    /* copy the results */
    pthread->st.reg[v0] = vfd0;

    /* first look for unused file descriptors */
    for (vfd1 = 0; vfd1 < MAX_FDNUM; vfd1++)
	if (pthread->fd[vfd1] == -1)
	    break;

    /* introduce the pthrad fd to AINT fd mapping */
    pthread->fd[vfd1] = aint_fd[1];

    /* copy the results */
    pthread->st.reg[a4] = vfd1;

#ifdef DEBUG
    fprintf(stderr, "created pipe v: %d %d p: %d %d\n", vfd0, vfd1,
	    aint_fd[0], aint_fd[1]);
#endif /* DEBUG */	    

    
    return SYSCALL_NEXT;
}

/*
 * getsysinfo(op, buffer, nbytes, start, arg) 
 *    unsigned long op;                       
 *    datatype *buffer;                       
 *    unsigned long nbytes;                   
 *    int *start;                             
 *    datatype *arg;                          
 */
  
SyscallStatus
aint_getsysinfo( thread_ptr pthread)
{
    ulong op = pthread->st.reg[_arg1];
    char *buffer = (char *) PMAP(pthread->st.reg[_arg2]);
    ulong nbytes = pthread->st.reg[_arg3];
    int *start;
    char *arg;

    if (pthread->st.reg[_arg4])
	start = (int *) PMAP(pthread->st.reg[_arg4]);
    else
	start = (int *) NULL;

    if (pthread->st.reg[_arg5])
	arg = (char *) PMAP(pthread->st.reg[_arg5]);
    else
	arg = (char *) NULL;

    pthread->st.reg[v0] = getsysinfo(op, buffer, nbytes, start, arg);

    if (pthread->st.reg[v0] == -1) 
	pthread->st.reg[_arg4] = errno;
    else
	pthread->st.reg[_arg4] = 0;
    
    return SYSCALL_NEXT;
}

/*
 * setsysinfo(op, buffer, nbytes, start, arg, flag) 
 *    unsigned long op;                       
 *    datatype *buffer;                       
 *    unsigned long nbytes;                   
 *    int *start;                             
 *    datatype *arg;                          
 *    unsigned long flag;
 */
  
SyscallStatus
aint_setsysinfo( thread_ptr pthread)
{
    ulong op = pthread->st.reg[_arg1];
    char *buffer = (char *) PMAP(pthread->st.reg[_arg2]);
    ulong nbytes = pthread->st.reg[_arg3];
    int *start;
    char *arg;
    ulong flag = pthread->st.reg[_arg6];

    if (pthread->st.reg[_arg5])
	arg = (char *) PMAP(pthread->st.reg[_arg5]);
    else
	arg = (char *) NULL;
    
    pthread->st.reg[v0] = setsysinfo(op, buffer, nbytes, start, arg, flag);

    if (pthread->st.reg[v0] == -1) 
	pthread->st.reg[_arg4] = errno;
    else
	pthread->st.reg[_arg4] = 0;
    
    return SYSCALL_NEXT;
}


/*
 * int sigprocmask(            
 *           int how,          
 *           sigset_t *set,    
 *           sigset_t *o_set );
 */
SyscallStatus
aint_sigprocmask( thread_ptr pthread)
{
    /* report `all clear' */
    pthread->st.reg[v0] = 0;
    pthread->st.reg[_arg4] = 0;
    
    return SYSCALL_NEXT;
}

/*
 * int  setrlimit(
 *           int resource1,
 *           struct rlimit *rlp );
 *
 *   - not implemented yet.
 */
SyscallStatus
aint_setrlimit( thread_ptr pthread)
{
    pthread->st.reg[_arg4] = 0;
    
    return SYSCALL_NEXT;
}

/*
 * int getrlimit (
 *          int resource1,
 *         struct rlimit *rlp );
 */
SyscallStatus
aint_getrlimit( thread_ptr pthread)
{
    int resource1 = pthread->st.reg[_arg1];
    struct rlimit aint_buffer;
    char *buffer_p, *q;
    int i;
    
    /* use local variables for efficient access */
    buffer_p = (char *) pthread->st.reg[_arg2];
    q = (char *) &aint_buffer;
    
    /* make the system call */
    pthread->st.reg[v0] = getrlimit(resource1, &aint_buffer);

    /* copy the results */
    for(i=0; i < sizeof(struct rlimit); i++,q++) {
	char *p = (char *) addr2phys(pthread, (long) buffer_p+i);
	*p = *q;
    }

    if (pthread->st.reg[v0] < 0) {
	pthread->st.reg[_arg4] = errno;
    }
    else
	pthread->st.reg[_arg4] = 0;

    return SYSCALL_NEXT;
}

/*
 * pid_t setsid( void );
 *
 * - not implemented yet.
 */
SyscallStatus
aint_setsid( thread_ptr pthread)
{
    pthread->st.reg[_arg4] = 0;
    
    return SYSCALL_NEXT;
}



/*
 * int chdir (                   
 *           const char *path );
 */
SyscallStatus
aint_chdir( thread_ptr pthread)
{
    int i;
    char *aint_buffer, *path;
    

    /* copy path */
    path = (char *) pthread->st.reg[_arg1];

    /* find out how much space we need */
    if ((aint_buffer = malloc(xaddr_space_strlen(pthread, (long) path)))
	== NULL)
	perror("aint_chdir");
    copy_string(pthread, aint_buffer, (long) path);

    /* make the call */
    pthread->st.reg[v0] = chdir(aint_buffer);
    
    if (pthread->st.reg[v0] < 0) {
	pthread->st.reg[_arg4] = errno;
    }
    else
	pthread->st.reg[_arg4] = 0;
    
    return SYSCALL_NEXT;
}

/*
 * gid_t getgid( void );
 *
 * Should return gid in v0 and effective gid in a4. Thanks to the hidden
 * API. 
 *
 */
SyscallStatus
aint_getgid( thread_ptr pthread)
{

    pthread->st.reg[v0] = getgid();
    pthread->st.reg[a4] = pthread->gid;

#ifdef DEBUG
    fprintf(stderr, "gid: %d, effective gid %d\n",
	    pthread->st.reg[v0], pthread->st.reg[a4]);
#endif
    
    pthread->st.reg[_arg4] = 0;
    
    return SYSCALL_NEXT;
}

/*
 * int getdomainname(name, namelen)
 *  char *name;
 *  int namelen;
 */
SyscallStatus
aint_getdomainname( thread_ptr pthread)
{
    int namelen;
    int i;
    char *aint_buffer, *p, *q, *output_addr;

    /* set up the buffer */
    output_addr = (char *) pthread->st.reg[_arg1];
    namelen = pthread->st.reg[_arg2];
    aint_buffer = (char *) malloc(namelen);
    if (aint_buffer == NULL)
	fatal("Out of memory\n");

    /* make the call */
    pthread->st.reg[v0] = getdomainname(aint_buffer, namelen);
    
    /* copy the output */
    q = aint_buffer;
    for(i=0; i < namelen; i++,q++) {
	char *p = (char *) addr2phys(pthread, (long) output_addr + i);
	*p = *q;
    }
    
    if (pthread->st.reg[v0] < 0) {
	pthread->st.reg[_arg4] = errno;
    }
    else
	pthread->st.reg[_arg4] = 0;
    
    return SYSCALL_NEXT;
}

/*
 * int setuid(uid_t uid);
 *
 */
SyscallStatus
aint_setuid( thread_ptr pthread)
{
    uid_t uid = (uid_t) pthread->st.reg[_arg1];

    pthread->st.reg[v0] = setuid(uid);
    
    if (pthread->st.reg[v0] < 0) {
	pthread->st.reg[_arg4] = errno;
    }
    else
	pthread->st.reg[_arg4] = 0;
    
    return SYSCALL_NEXT;
}

/*
 * int madvise (
 *          caddr_t addr,
 *          size_t len,
 *          int behav );
 * - not implemented 
 */
SyscallStatus
aint_madvise( thread_ptr pthread)
{
    
    pthread->st.reg[v0] = 0;
    pthread->st.reg[_arg4] = 0;
    
    return SYSCALL_NEXT;
}

/*
 * int umask(mode_t cmask)
 *
 */
SyscallStatus
aint_umask( thread_ptr pthread)
{
    mode_t mode = (mode_t) pthread->st.reg[_arg1];

    pthread->st.reg[v0] = umask(mode);
    
    if (pthread->st.reg[v0] < 0) {
	pthread->st.reg[_arg4] = errno;
    }
    else
	pthread->st.reg[_arg4] = 0;
    
    return SYSCALL_NEXT;
}

/*
 * int table(id, index, addr, nel, lel)
 *           int id;
 *           int index;
 *           char *addr;
 *           int nel;
 *           u_int lel;
 *
 * - not sure how well this implementation is working.
 */ 
SyscallStatus
aint_table( thread_ptr pthread)
{
    int id = pthread->st.reg[_arg1];
    int index = pthread->st.reg[_arg2];
    char *addr;
    int nel = pthread->st.reg[_arg4];
    u_int lel = pthread->st.reg[_arg5];
    char *aint_buffer;
    char *p, *q;
    int i;
    
    if (nel > 0) {
	/* the call copies from kernel to user space */
	size_t size = lel * nel;
	
	addr = (char *) pthread->st.reg[_arg3];
	aint_buffer = (char *) malloc(size);

	/* make the call itself */
	pthread->st.reg[v0] = table(id, index, aint_buffer, nel, lel);

	/* copy the results */
	q = aint_buffer;
	for(i=0; i < size; i++,q++) {
	    p = (char *) addr2phys(pthread, (long) addr + i);
	    *p = *q;
	}
    	
    } else {
	/* the call copies from user space to kernel space */

	/*
	 * copy the buffer from the simulated address space to the AINT
	 * address space
	 */
	size_t size = lel * nel;

	addr = (char *) pthread->st.reg[_arg3];
	aint_buffer = (char *) malloc(lel * nel);

	p = aint_buffer;
	for(i=0; i < size; i++,p++) {
	    q = (char *) addr2phys(pthread, (long) addr + i);
	    *p = *q;
	}

	/* make the call itself */
	pthread->st.reg[v0] = table(id, index, aint_buffer, nel, lel);
    }
    
    if (pthread->st.reg[v0] < 0) {
	pthread->st.reg[_arg4] = errno;
    }
    else
	pthread->st.reg[_arg4] = 0;
    
    return SYSCALL_NEXT;
}

/*
 * int unlink (
 *           const char *path );
 */
SyscallStatus
aint_unlink( thread_ptr pthread)
{
    int i;
    char *aint_buffer, *path;
    

    /* copy path */
    path = (char *) pthread->st.reg[_arg1];

    /* find out how much space we need */
    if ((aint_buffer = malloc(xaddr_space_strlen(pthread, (long) path)))
	== NULL)
	perror("aint_chdir");
    copy_string(pthread, aint_buffer, (long) path);

    /* make the call */
    pthread->st.reg[v0] = unlink(aint_buffer);
    
    if (pthread->st.reg[v0] < 0) {
	pthread->st.reg[_arg4] = errno;
    }
    else
	pthread->st.reg[_arg4] = 0;
    
    return SYSCALL_NEXT;
}

/*
 * int syscall(number, arg..);
 *
 *  - indirect system call.
 */
SyscallStatus
aint_syscall( thread_ptr pthread)
{
    int number = pthread->st.reg[_arg1];
    ulong arg1, arg2, arg3, arg4, arg5;
    
    /* some cheating to be done here */
    if (number == 33554449) {
	pthread->st.reg[v0] = 204;
	pthread->st.reg[a3] = 0;
	pthread->st.reg[a4] = 0;
    
	return SYSCALL_NEXT;
    }
    
    arg1 = pthread->st.reg[_arg2];
    arg2 = pthread->st.reg[_arg3];
    arg3 = pthread->st.reg[_arg4];
    arg4 = pthread->st.reg[_arg5];
    arg5 = pthread->st.reg[_arg6];

    /* now fill in the information */
    pthread->st.reg[v0] = number;
    pthread->st.reg[_arg1] = pthread->st.reg[_arg2];
    pthread->st.reg[_arg2] = pthread->st.reg[_arg3];
    pthread->st.reg[_arg3] = pthread->st.reg[_arg4];
    pthread->st.reg[_arg4] = pthread->st.reg[_arg5];
    pthread->st.reg[_arg5] = pthread->st.reg[_arg6];
    
    return callsys_f( pthread);
}

/*
 * int dup(
 *           int filedes );
 */ 
SyscallStatus
aint_dup( thread_ptr pthread)
{
    int pfd, vfd;
    int filedes = pthread->st.reg[_arg1];
    int new_file_descriptor;

    /* get the AINT descriptor */
    pfd = pthread->fd[filedes];

    /* make the actual system call */
    new_file_descriptor = dup(pfd);
    
    /* Search for an unused fd */
    for (vfd = 0; vfd < MAX_FDNUM; vfd++)
	if (pthread->fd[vfd] == -1)
	    break;

    pthread->fd[vfd] = new_file_descriptor;

#ifdef DEBUG
    fprintf(stderr, "dup: vfd1: %d, vfd2: %d\n    pfd1: %d, pfd2: %d\n",
	    filedes, vfd, pfd, new_file_descriptor);
#endif    

    /* return this virtual fd */
    pthread->st.reg[v0] = vfd;
	
    if (pthread->st.reg[v0] < 0) {
	pthread->st.reg[_arg4] = errno;
    }
    else
	pthread->st.reg[_arg4] = 0;
    
    return SYSCALL_NEXT;
}

#ifdef DEBUG
int trace_on = 0;
#endif

/*
 * int pid_block                  
 *           (struct timeval *tp,
 *           long flags);        
 */
SyscallStatus
aint_pid_block( thread_ptr pthread)
{
    int i;
    struct timeval t; 
    char *p;
    ulong tp;
    long flags;
    long secs;
    
    /* do a byte by byte copy */
    tp = pthread->st.reg[_arg1];

#ifdef DEBUG_BLOCK
    fprintf(stderr, "%d: blocking\n", pthread->pid);
#endif

    if (tp) {
	p = (char *) &t;
	for(i=0; i < sizeof(struct timeval); i++, p++) {
	    *p = *(char *) PMAP(tp+i);
	}
	
	flags = pthread->st.reg[_arg2];
	
	secs = t.tv_sec;

	/* block for a specified period */
	if (secs) {
	    
#ifdef DEBUG_BLOCK
    fprintf(stderr, "for %d seconds\n", secs);
#endif
	    event_timed_block(pthread, E_BLOCK, secs *CLOCKS_PER_SECOND);
	    block_thr(&sleep_q, pthread);
	} else {
	    /* Just yield to some other thread */
	    event_yield(picode, pthread);
	}

    } else {

	/* block till woken up */
	event_block(pthread, E_BLOCK);
	block_thr(&sleep_q, pthread);
	
    }
    
    pthread->st.reg[v0] = 0;
    pthread->st.reg[_arg4] = 0;
    
    return SYSCALL_NEXT;
}

/*                      
 * int pid_unblock         
 *	     (pid_t pid   
 *	     long flags); 
 */
SyscallStatus
aint_pid_unblock( thread_ptr pthread)
{
    int i;
    pid_t pid;

    pid = pthread->st.reg[_arg1];
    if (pid >= 0)
	pid -= PID_OFFSET;
    
#ifdef DEBUG
    fprintf(stderr, "%d: unblocking %d\n", pthread->pid, pid);
#endif

    /* wake it up if it is not running */
    if (threads[pid].runstate != R_RUN)
	event_unblock(pthread, &threads[pid]);
    
    pthread->st.reg[v0] = 0;
    pthread->st.reg[_arg4] = 0;
    
    return SYSCALL_NEXT;
}

/*
 * int kill(
 *           pid_t process,
 *           int signal );
 *
 * - implements signal 0 only.
 */
SyscallStatus
aint_kill( thread_ptr pthread)
{
    pid_t pid;
    int signal;

    pid = (pid_t) pthread->st.reg[_arg1];
    signal = (int) pthread->st.reg[_arg2];

#ifdef DEBUG
    fprintf(stderr, "%d: checking for %d\n",
	    pthread->pid,
	    pid);
#endif
    
    if (signal != 0) 
	fatal("aint_kill: unimplemented signal %d", signal);

    pthread->st.reg[v0] = 0;
    pthread->st.reg[_arg4] = 0;
    
    return SYSCALL_NEXT;
}

/*                      
 * int sigstack (                         
 *           struct sigstack *instack,   
 *           struct sigstack *outstack );
 * - unimplemented.
 */
SyscallStatus
aint_sigstack( thread_ptr pthread)
{
    
    pthread->st.reg[v0] = 0;
    pthread->st.reg[_arg4] = 0;
    
    return SYSCALL_NEXT;
}