ipc.c 18.8 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
/*
 * 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.
 *
 */


/*
 * Routines for supporting System V semaphores and shared memory.
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>   
#include <sys/shm.h>
#include <errno.h>

#include "gamma.h"
#include "opcodes.h"
#include "globals.h"
#include "ipc.h"
#include "task.h"
#include "protos.h"
#include "alpha_regs.h"
#include "sim_error.h"
#include "aint.h"
#ifdef ITRACE
extern FILE *itrace;
#endif


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



static struct sysv_semid_ds sem_set[MAX_SEMASET];
int shm_max_inuse = -1;

static void alloc_semset (struct sysv_semid_ds *, int, int, int);
static void free_semset (struct sysv_semid_ds *);


/* Support for semaphores */

/*
 * int semget (
 *         key_t key,
 *         int nsems,
 *	   int semflg );
 */
SyscallStatus aint_semget( thread_ptr pthread)
{
    int key, nsems, flags;
    int i;

    key = pthread->st.reg[_arg1];
    nsems = pthread->st.reg[_arg2];
    flags = pthread->st.reg[_arg3];

    if (key == 0) {
	/* search for a free slot */
	for (i = 0; i < MAX_SEMASET; i++)
	    if (sem_set[i].inuse == 0)
		break;

	/* out of semaphore sets, return an error */
	if (i == MAX_SEMASET) {
	    pthread->st.reg[REG_V0] = -1;
	    *pthread->perrno = ENOSPC;
	    pthread->st.reg[_arg4] = ENOSPC;
	    return SYSCALL_NEXT;
	}

	/* found a free slot */
	alloc_semset (&sem_set[i], key, nsems, pthread->pid);

	/* return the semid */
	pthread->st.reg[REG_V0] = i;
	pthread->st.reg[_arg4] = 0;
	return SYSCALL_NEXT;
    }

    /* search for a matching key */
    for (i = 0; i < MAX_SEMASET; i++)
	if (sem_set[i].sem_perm.key == key)
	    break;
    if (sem_set[i].sem_perm.key == key) {
	/* if they wanted a new exclusive semaphore, then it's an error */
	if ((flags & (IPC_EXCL | IPC_CREAT)) == (IPC_EXCL | IPC_CREAT)) {
	    pthread->st.reg[REG_V0] = -1;
	    pthread->st.reg[_arg4] = *pthread->perrno = EEXIST;
	    return SYSCALL_NEXT;
	}
	/* return the semid */
	pthread->st.reg[REG_V0] = i;
	pthread->st.reg[_arg4] = 0;
	return SYSCALL_NEXT;
    }

    /* semid matching key not found */
    if (flags & IPC_CREAT) {
	/* search for a free slot */
	for (i = 0; i < MAX_SEMASET; i++)
	    if (sem_set[i].inuse == 0)
		break;

	/* out of semaphore sets, return an error */
	if (i == MAX_SEMASET) {
	    pthread->st.reg[REG_V0] = -1;
	    pthread->st.reg[_arg4] = *pthread->perrno = ENOSPC;
	    return SYSCALL_NEXT;
	}

	/* found a free slot */
	alloc_semset (&sem_set[i], key, nsems, pthread->pid);

	/* return the semid */
	pthread->st.reg[REG_V0] = i;
	pthread->st.reg[_arg4] = 0;
	return SYSCALL_NEXT;
    }

    /* no match and IPC_CREAT not set, so it's an error */
    pthread->st.reg[REG_V0] = -1;
    pthread->st.reg[_arg4] = *pthread->perrno = ENOENT;
    return SYSCALL_NEXT;
}

static void
alloc_semset (struct sysv_semid_ds *psemset, int key, int nsems, int pid)
{
    int i;
    struct sysv_sem *psem;

    psemset->inuse = 1;
    psemset->sem_perm.key = key;
    psemset->sem_nsems = nsems;
    psem = (struct sysv_sem *) malloc (nsems * sizeof (struct sysv_sem));
    if (psem == NULL)
	fatal ("alloc_semset: cannot allocate 0x%x bytes for semaphores.\n",
	       nsems * sizeof (struct sysv_sem));
    psemset->sem_base = psem;

    for (i = 0; i < nsems; i++) {
	psem[i].semval = 0;
	psem[i].sempid = pid;
	psem[i].semncnt = 0;
	psem[i].semzcnt = 0;
	INLINE_INIT_Q (&psem[i].nwait_q);
	INLINE_INIT_Q (&psem[i].zwait_q);
    }
}

static void
free_semset (struct sysv_semid_ds *psemset)
{
    psemset->inuse = 0;
    psemset->sem_perm.key = 0;
    free (psemset->sem_base);
}

void
semcpin (struct sysv_semid_ds *psemset, struct sysv_semid_ds *buf)
{
    int mode;

    psemset->sem_perm.uid = buf->sem_perm.uid;
    psemset->sem_perm.gid = buf->sem_perm.gid;

    mode = buf->sem_perm.mode & 0777;
    psemset->sem_perm.mode = (psemset->sem_perm.mode & ~0777) | mode;
}

void
semcpout (struct sysv_semid_ds *buf, struct sysv_semid_ds *psemset)
{
    buf->sem_perm.uid = psemset->sem_perm.uid;
    buf->sem_perm.gid = psemset->sem_perm.gid;
    buf->sem_perm.cuid = psemset->sem_perm.cuid;
    buf->sem_perm.cgid = psemset->sem_perm.cgid;
    buf->sem_perm.mode = psemset->sem_perm.mode & 0777;
    buf->sem_perm.seq = psemset->sem_perm.seq;
    buf->sem_perm.key = psemset->sem_perm.key;
    buf->sem_nsems = psemset->sem_nsems;
    buf->sem_otime = psemset->sem_otime;
    buf->sem_ctime = psemset->sem_ctime;
}

/*
 * int semop(
 *   int semid,
 *   struct sembuf *sops,
 *   u_int nsops);
 */
SyscallStatus aint_semop( thread_ptr pthread)
{
    int i, sem_num, sem_op, sem_flg;
    int semid, r5, nsops;
    struct sembuf *sops;
    struct sysv_semid_ds *psemset;
    struct sysv_sem *psem;
    thread_ptr pthr, tnext;
    aint_time_t duration;
    event_ptr pevent;

    semid = pthread->st.reg[_arg1];
    r5 = pthread->st.reg[_arg2];
    nsops = pthread->st.reg[_arg3];

    sops = (struct sembuf *) (long) MAP (r5);
    psemset = &sem_set[semid];

    /*
     * First loop through all the semaphores to check that all requested
     * operations can be performed.
     */
    for (i = 0; i < nsops; i++) {
	sem_num = sops[i].sem_num;
	sem_op = sops[i].sem_op;
	sem_flg = sops[i].sem_flg;
	if (sem_flg & SEM_UNDO)
	    fatal ("mint_semop: SEM_UNDO not supported yet.\n");
	psem = &psemset->sem_base[sem_num];
	if (sem_op < 0) {
	    if (psem->semval < -sem_op) {
		if (sem_flg & IPC_NOWAIT) {
		    pthread->st.reg[REG_V0] = -1;
		    pthread->st.reg[_arg4] = *pthread->perrno = EAGAIN;
		    return SYSCALL_NEXT;
		}
		/* need to suspend this process */
		pthread->semval = -sem_op;
		psem->semncnt++;
		event_block (pthread, E_PSEMA_ATTEMPT);
		block_thr ( (thread_ptr) &psem->nwait_q, pthread);
		/* return to this routine to try the semaphores again */
		return SYSCALL_SAME;
	    }
	}
	else if (sem_op == 0) {
	    if (psem->semval) {
		if (sem_flg & IPC_NOWAIT) {
		    pthread->st.reg[REG_V0] = -1;
		    pthread->st.reg[_arg4] = *pthread->perrno = EAGAIN;
		    return SYSCALL_NEXT;
		}
		/* need to suspend this process */
		psem->semzcnt++;
		event_block (pthread, E_PSEMA_ATTEMPT);
		block_thr ( (thread_ptr) &psem->zwait_q, pthread);
		/* return to this routine to try the semaphores again */
		return SYSCALL_SAME;
	    }
	}
    }

    /*
     * Loop through the operations again, this time performing them,
     * since we know that none will block.
     */
    for (i = 0; i < nsops; i++) {
	sem_num = sops[i].sem_num;
	sem_op = sops[i].sem_op;
	sem_flg = sops[i].sem_flg;
	psem = &psemset->sem_base[sem_num];
	/* need to check for blocked processes */
	if (sem_op < 0)
	    psem->semval += sem_op;
	else if (sem_op > 0) {
	    psem->semval += sem_op;
	    /*
	     * If there are processes waiting for this semaphore,
	     * we need to check if they should be awakened.
	     */
	    if (psem->semval > 0 && psem->semncnt) {
		/*
		 * Wake up everyone waiting for this to be large enough.
		 * We don't know who will be able to proceed since they
		 * may have to atomically change other semaphores, so we
		 * wake them all up and let them fight it out.
		 */
		pthr = (thread_ptr) psem->nwait_q.next;
		while (pthr != (thread_ptr) & psem->nwait_q) {
		    tnext = (thread_ptr) pthr->next;
		    if (pthr->runstate != R_BLOCK)
			fatal ("mint_semop: runstate (%d) "
			       "not R_BLOCK for thread %d\n",
			       pthr->runstate, pthr->pid);
		    if (psem->semval >= pthr->semval) {
			/* wake up this thread */
			wakeup_thr (pthr);
#ifdef SOLO
			ASSERT(0); duration = 1;
#else
			duration = pthread->time + picode->cycles - pthr->time;
			if (duration < 0)
			    fatal ("mint_usvsema: negative wait "
				   "duration (%.1f) at time %.0f\n",
				   duration, pthread->time + picode->cycles);
			pthr->time += duration;
			pevent = event_unblock_list (pthread, pthr);
			pevent->duration = duration;
			pevent->utype = E_PSEMA_ATTEMPT;
			psem->semncnt--;
#endif

		    }
		    pthr = tnext;
		}
	    }
	}

	/*
	 * If this semaphore is now zero, and there are waiting processes,
	 * then wake them up.
	 */
	if (psem->semval == 0 || psem->semzcnt) {
	    /* wake up everyone waiting for this to be zero */
	    for (i = 0; i < psem->semzcnt; i++) {
		pthr = unblock_thr ((thread_ptr) & psem->zwait_q);
#ifdef SOLO
		ASSERT(0); duration = 1;
#else
		duration = pthread->time + picode->cycles - pthr->time;
		if (duration < 0)
		    fatal ("mint_usvsema: negative wait "
			   "duration (%.1f) at time %.0f\n",
			   duration, pthread->time + picode->cycles);
		pthr->time += duration;
		pevent = event_unblock_list (pthread, pthr);
		pevent->duration = duration;
		pevent->utype = E_PSEMA_ATTEMPT;
#endif

	    }
	    psem->semzcnt = 0;
	}
    }

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

/*
 *  int semctl(
 *	    int semid,
 *	    int semnum,
 *	    int cmd,
 *	    union semun {
 *		    int val;
 *		    struct semid_ds *buf;
 *		    u_short *array;
 *	    } arg );
 */
SyscallStatus aint_semctl( thread_ptr pthread)
{
    int i;
    int semid, semnum, cmd;
    long arg;			/* really "union semun" */
    ushort *array;
    struct semid_ds *axpbuf;
    struct sysv_semid_ds *buf;
    struct sysv_semid_ds *psemset;
    struct sysv_sem *psem;

    semid = pthread->st.reg[_arg1];
    semnum = pthread->st.reg[_arg2];
    cmd = pthread->st.reg[_arg3];
    arg = pthread->st.reg[_arg4];

    /* check for invalid semid */
    if (semid >= MAX_SEMASET || sem_set[semid].inuse == 0) {
	pthread->st.reg[REG_V0] = -1;
	pthread->st.reg[_arg4] = *pthread->perrno = EINVAL;
	return SYSCALL_NEXT;
    }
    psemset = &sem_set[semid];
    if (semnum < 0 || semnum >= psemset->sem_nsems) {
	pthread->st.reg[REG_V0] = -1;
	pthread->st.reg[_arg4] = *pthread->perrno = EINVAL;
	return SYSCALL_NEXT;
    }
    psem = &psemset->sem_base[semnum];

    switch (cmd) {
	case GETVAL:
	    pthread->st.reg[REG_V0] = psem->semval;
	    pthread->st.reg[_arg4] = 0;
	    break;
	case SETVAL:
	    psem->semval = (int) arg;
	    pthread->st.reg[REG_V0] = pthread->st.reg[_arg4] = 0;
	    break;
	case GETPID:
	    pthread->st.reg[REG_V0] = psem->sempid;
	    pthread->st.reg[_arg4] = 0;
	    break;
	case GETNCNT:
	    pthread->st.reg[REG_V0] = psem->semncnt;
	    pthread->st.reg[_arg4] = 0;
	    break;
	case GETZCNT:
	    pthread->st.reg[REG_V0] = psem->semzcnt;
	    break;
	case GETALL:
	    array = (ushort *) (long) MAP (arg);
	    for (i = 0; i < psemset->sem_nsems; i++)
		array[i] = psemset->sem_base[i].semval;
	    pthread->st.reg[REG_V0] = pthread->st.reg[_arg4] = 0;
	    break;
	case SETALL:
	    array = (ushort *) (long) MAP (arg);
	    for (i = 0; i < psemset->sem_nsems; i++)
		psemset->sem_base[i].semval = array[i];
	    pthread->st.reg[REG_V0] = pthread->st.reg[_arg4] = 0;
	    break;
	case IPC_STAT:
	    buf = (struct sysv_semid_ds *) (long) MAP (arg);
	    semcpout (buf, psemset);
	    pthread->st.reg[REG_V0] = pthread->st.reg[_arg4] = 0;
	    break;
	case IPC_SET:
	    buf = (struct sysv_semid_ds *) (long) MAP (arg);
	    semcpin (psemset, buf);
	    pthread->st.reg[REG_V0] = pthread->st.reg[_arg4] = 0;
	    break;
	case IPC_RMID:
	    /*
	     * Need to check here if any process is blocked on a semaphore
	     * in this set.
	     */
	    free_semset (psemset);
	    pthread->st.reg[REG_V0] = pthread->st.reg[_arg4] = 0;
	    break;
	default:
	    pthread->st.reg[REG_V0] = -1;
	    pthread->st.reg[_arg4] = *pthread->perrno = EINVAL;
	    break;
    }

    return SYSCALL_NEXT;
}

/* Support for shared memory */

/*
 * int shmget (
 *	  key_t	key,
 *	  size_t size,
 *	  int flags );
 */
SyscallStatus aint_shmget( thread_ptr pthread)
{
    int i;
    key_t key;
    size_t size;
    int flags;

    key = pthread->st.reg[_arg1];
    size = pthread->st.reg[_arg2];
    flags = pthread->st.reg[_arg3];

#ifdef DEBUG
    fprintf(stderr, "%d: shmget: key = %lx, size = %lx, flags = %x\n",
	    pthread->pid,
	    key, size, flags);
#endif
    
    /* round size up to a page boundary */
    size = (size + TB_PAGESIZE - 1) & ~(TB_PAGESIZE - 1);

    /* If a non-zero key was specified, then we have to search for a
     * segment with a matching key.
     */
    if (key) {
	/* search for a matching key */
	for (i = 0; i < MAX_SHMSEG; i++) {
	    if (shm_seg[i].shm_perm.key == key) {
		/* If this process requested a new exclusive copy,
		 * then it's an error.
		 */
		if ((flags & (IPC_EXCL | IPC_CREAT))
		    == (IPC_EXCL | IPC_CREAT)) {
		    /* return an error */
#ifdef DEBUG
		    fprintf(stderr, "aint_shmget: Requesting a new exclusive"
			    " copy of a segment in use\n");
#endif
		    pthread->st.reg[REG_V0] = -1;
		    pthread->st.reg[_arg4] = *pthread->perrno = EEXIST;
		    return SYSCALL_NEXT;
		}
		else {
		    /* return the shmid */
		    pthread->st.reg[REG_V0] = i;
		    pthread->st.reg[_arg4] = 0;
		    return SYSCALL_NEXT;
		}
	    }
	}

	/* if we get here then we did not find a matching key */
	if ((flags & IPC_CREAT) == 0) {
#ifdef DEBUG
	    fprintf(stderr, "aint_shmget: Key %d not found and IPC_CREAT not"
		    " specified\n", key);
	    fprintf(stderr, "flags: %x\n", flags);
#endif
	    pthread->st.reg[REG_V0] = -1;
	    pthread->st.reg[_arg4] = *pthread->perrno = ENOENT;
	    return SYSCALL_NEXT;
	}
    }

    /* search for an unused shmid */
    for (i = 0; i < MAX_SHMSEG; i++)
	if (shm_seg[i].inuse == 0)
	    break;

    /* Out of shared memory segments, return an error */
    if (i == MAX_SHMSEG) {

#ifdef DEBUG
	fprintf(stderr, "aint_shmget: No more shared memory segments\n");
#endif
	pthread->st.reg[REG_V0] = -1;
	pthread->st.reg[_arg4] = *pthread->perrno = ENOSPC;
	return SYSCALL_NEXT;
    }
    /* the return value is the shmid */
    pthread->st.reg[REG_V0] = i;
    pthread->st.reg[_arg4] = 0;

    if (i > shm_max_inuse)
	shm_max_inuse = i;

    /* make sure there is enough space */
    if (shmem_end + size > shmem_start + shmem_size)
	fatal ("aint_shmget: not enough shared memory "
	       "(requested size = 0x%x, used = 0x%x)\n",
	       size, shmem_end - shmem_start);

    /* initialize the shared memory segment */

    shm_seg[i].addr = shmem_end;
    shm_seg[i].size = size;
    shm_seg[i].inuse = 1;
    shm_seg[i].shm_perm.key = key;

    /* Update size of used shared-space */
    shmem_end += size;

    return SYSCALL_NEXT;
}

/*
 * void *shmat(
 *	  int shmid,
 *	  void *shmaddr,
 *	  int shmflg);
 */
SyscallStatus aint_shmat( thread_ptr pthread)
{
    int shmid, flags;
    int id, size;
    long addr, real_addr;
    struct shm_descriptor *new_shm_ds;

    shmid = pthread->st.reg[_arg1];
    addr = pthread->st.reg[_arg2];
    flags = pthread->st.reg[_arg3];

#ifdef DEBUG
    fprintf(stderr, "%d: shmat: shmid = %lx, addr = %lx, flags = %x\n",
	    pthread->pid,
	    shmid, addr, flags);
#endif

    /* check that the shmid is a valid index */
    if ((shmid < 0) || (shmid > MAX_SHMSEG)) {

#ifdef DEBUG
	fprintf(stderr, "aint_shmat: Invalid shmid\n");
#endif
	pthread->st.reg[REG_V0] = -1;
	pthread->st.reg[_arg4] = *pthread->perrno = EINVAL;
	return SYSCALL_NEXT;
    }

    /* check that this shared memory segment has been allocated */
    if (shm_seg[shmid].inuse == 0) {

#ifdef DEBUG
	fprintf(stderr, "aint_shmat: Segment has not been allocated\n");
#endif	
	pthread->st.reg[REG_V0] = -1;
	pthread->st.reg[_arg4] = *pthread->perrno = EINVAL;
	return SYSCALL_NEXT;
    }

    size = shm_seg[shmid].size;

    /*
     * If addr is zero, get the next valid addr: unsp_shmat_current upto
     * unsp_shmat_current+size
     */ 
    if (addr == 0) {
	addr = pthread->unsp_shmat_current;
	pthread->unsp_shmat_current += ALIGN(size, TB_PAGESIZE);
	if (pthread->unsp_shmat_current >= UNSP_SHMAT_END) {
	    /* Cannot attach - full */

#ifdef DEBUG
	    fprintf(stderr, "aint_shmat: Address space full\n");
#endif	    
	    pthread->st.reg[REG_V0] = -1;
	    pthread->st.reg[_arg4] = *pthread->perrno = ENOMEM;
	    return SYSCALL_NEXT;
	}
	/* Plug in the address in the per-process table */
	new_shm_ds = (struct shm_descriptor *)
	    malloc (sizeof (struct shm_descriptor));
	new_shm_ds->shmid = shmid;
	new_shm_ds->size = size;
	new_shm_ds->addr = addr;
	/* Enter new_shm_ds in the per-process shared-region descriptor */
	new_shm_ds->next = pthread->shmem_regions;
	pthread->shmem_regions = new_shm_ds;

	/* return values */
	pthread->st.reg[REG_V0] = addr;
	pthread->st.reg[_arg4] = 0;

	return SYSCALL_NEXT;
    }
    else {
	/*
	 * If the address was specified, it should be within SP_SHMAT_START
         * and SP_SHMAT_END
	 */
	addr = (addr + TB_PAGESIZE - 1) & ~(TB_PAGESIZE - 1);
	if ((addr < SP_SHMAT_START) || ((addr + size) >= SP_SHMAT_END)) {
#ifdef DEBUG
	    fprintf(stderr, "aint_shmat: Address specified, %ld,"
		    " not in the correct range\n", addr);
#endif
	    
	    pthread->st.reg[REG_V0] = -1;
	    pthread->st.reg[_arg4] = EINVAL;
	    return SYSCALL_NEXT;
	}
    }
    /* Address is valid. Allocate a per-process shared-region descriptor */
    new_shm_ds = (struct shm_descriptor *)
	malloc (sizeof (struct shm_descriptor));
    new_shm_ds->shmid = shmid;
    new_shm_ds->size = size;
    new_shm_ds->addr = addr;
    new_shm_ds->next = pthread->shmem_regions;
    pthread->shmem_regions = new_shm_ds;

    /* return values */
    pthread->st.reg[REG_V0] = addr;
    pthread->st.reg[_arg4] = 0;

    return SYSCALL_NEXT;
}


/*
 * int shmdt(
 *	  const	void *addr);
 *
 */
SyscallStatus aint_shmdt( thread_ptr pthread)
{
    struct shm_descriptor *sd, *last;
    ulong addr = pthread->st.reg[_arg1];
    size_t size;
    ulong num_pages;
    int i;
    
#ifdef DEBUG
    fprintf(stderr, "%d: shmdt: addr = %lx\n",
	    pthread->pid,
	    pthread->st.reg[_arg1]);
#endif
    
    /* first locate the descriptor corresponding to the region */
    sd = last = pthread->shmem_regions;

    /* while there are more descriptors */
    while(sd) {
	if (sd->addr == addr)
	    break;

	/* traverse the list */
	last = sd;
	sd = sd->next;
    }

    if (sd->addr != addr) {
	/* didn't find a shmem region at the specified address */
	pthread->st.reg[REG_V0] = -1;
#ifdef DEBUG
	fprintf(stderr, "shmdt: no shmem region at %lx\n", addr);
#endif
    }

    /* note the size down */
    size = sd->size;
    
    /* now remove the descriptor from the list */
    if (sd == last) {
	/* it was the first element */
	pthread->shmem_regions = sd->next;
	/* free the node */
	free(sd);
    }

    /* unmap all the pages */
    
    /* round up the length to the next multiple of the page size */
    num_pages = size / TB_PAGESIZE;

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

    /* all clear */
    pthread->st.reg[REG_V0] = pthread->st.reg[_arg4] = 0;

    return SYSCALL_NEXT;
}

/*
 * int shmctl(
 *	  int shmid,
 *	  int cmd,
 *	  struct shmid_ds *buf);
 *
 * Does nothing right now.
 */
SyscallStatus aint_shmctl( thread_ptr pthread)
{
    pthread->st.reg[_arg4] = pthread->st.reg[REG_V0] = 0;
    return SYSCALL_NEXT;
}