exec.c 24.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
/*
 * 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 the main execution loop, thread queues, and all the event
 * generating functions
 */

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

#include "gamma.h"
#include "aint.h"
#include "globals.h"
#include "opcodes.h"
#include "protos.h"
#include "wheel.h"
#include "event.h"
#include "alpha_regs.h"
#include "sim_error.h"


#ifdef gone
/* icode structs for the cleanup functions */
static icode_t idone1;		
static icode_t idone2;		
static icode_t idone3;	
#endif	

void init_all_queues ();
void init_thread (thread_ptr pthread, event_ptr pevent, int enqueue);
void init_event (thread_ptr pthread, event_ptr pevent);
int next_event (task_ptr);

#ifdef ITRACE
extern FILE *itrace;
#endif

#ifdef CALL_TRACE
extern int call_trace_enable;
#endif

void
init_all_queues ()
{
    int pid, i;

    /* init the queue head nodes */
    INLINE_INIT_Q (&run_q);
    INLINE_INIT_Q (&done_q);
    INLINE_INIT_Q (&free_q);
    INLINE_INIT_Q (&sleep_q);
    INLINE_INIT_Q (&read_pipe_q);
    INLINE_INIT_Q (&write_pipe_q);
    
    /* Allocate the array of threads */
    threads = (thread_ptr) calloc (max_nprocs, sizeof (thread_t));
    /* zero it out for heaven's sake */
    bzero((char *) threads, max_nprocs * sizeof(thread_t));
    
    if (threads == NULL) {
	fatal ("init_all_queues: cannot allocate 0x%x bytes for threads.\n",
	       max_nprocs * sizeof (thread_t));
    }

    for (pid = 0; pid < max_nprocs; pid++) {
	int i;

	threads[pid].pid = pid;
	INLINE_ENQUEUE (&free_q, &threads[pid]);

	threads[pid].fd = (int *) malloc (MAX_FDNUM * sizeof (int));
	if (threads[pid].fd == NULL)
	    fatal ("init_all_queues: cannot allocate"
		   "0x%x bytes for thread fds.\n", 
		   MAX_FDNUM * sizeof (char));

	threads[pid].num_pages = 0;
	threads[pid].num_private = 0;

	for (i = 0; i < TB_SIZE; i++)
	    threads[pid].page_bucket[i] = NULL;

	threads[pid].shmem_regions = (struct shm_descriptor *) NULL;
	threads[pid].unsp_shmat_current = UNSP_SHMAT_START;

	threads[pid].sigv = (struct my_sigvec *) malloc (MAX_SIGNALS *
						 sizeof (struct my_sigvec));
	if (threads[pid].sigv == NULL)
	    fatal ("init_all_queues: cannot allocate"
		   "0x%x bytes for thread sigv.\n",
		   MAX_SIGNALS * sizeof (struct my_sigvec));

    }

    /* Initialize the global shared-memory page table to empty lists */
    for (i = 0; i < TB_SIZE; i++)
	shmem_page_table[i] = (page_t *) NULL;
}

/*
 * Initializes the thread. The enqueue parameter is normally zero. But when
 * called from execve, it is non-zero. The relevant thread is not enqueued.
 */
void
init_thread (thread_ptr pthread, event_ptr pevent, int enqueue)
{
    int i;

    /* zero some of the field */
    pthread->st.reg[zero] = 0;
    pthread->st.fp[zero] = 0;
    pthread->cpu_time = 0;
    pthread->child_cpu = 0;
    pthread->is_zombie = 0;
    pthread->errno = 0;
    pthread->ufunc = NULL;
	
    /* pthread->stall_addr = NULL; */
    pthread->runstate = R_RUN;
    pthread->youngest = NULL;

    /* put the thread on the run queue */
    if (enqueue) {
	INLINE_ENQUEUE (&run_q, pthread);
    }
    
    /* map in the global errno */
    pthread->perrno = &pthread->errno;

    /* initialize the event structure associated with this thread */
    init_event (pthread, pevent);
#ifdef gone
    pthread->psave = NULL;
#endif
    pthread->sigpending = 0;

    pthread->mmap_size = 0;
}

/*
 * Does some initializations that need to be done, only for the first
 * thread.
 */
void
init_main_thread ()
{
    int i;
    thread_ptr pthread;
    event_ptr pevent;

    max_pid = 0;
    nprocs = 1;
    pthread = &threads[0];

    INLINE_REMOVE (pthread);	/* From free_q */
    NEW_ITEM (event_free, sizeof (event_t), pevent, "init_main_thread");

    init_thread (pthread, pevent, 1);

    /*
     * The following initialization is done only once, for the main thread.
     */


    /* init the signal handler vectors */
    for (i = 0; i < MAX_SIGNALS; i++) {
	pthread->sigv[i].sv_handler = (sig_handler_t) SIG_DFL;
	pthread->sigv[i].sv_mask = 0;
	pthread->sigv[i].sv_flags = 0;
    }
    pthread->sigblocked = 0;

    /* initialize all file descriptors to "closed" */
    for (i = 0; i < MAX_FDNUM; i++)
	pthread->fd[i] = -1;
    
    pthread->fd[0] = 0;
    pthread->fd[1] = 1;
    pthread->fd[2] = 2;
    
    pthread->time = 0;
    pthread->parent = NULL;
    pthread->sibling = NULL;

    pthread->st.fpcr = 0;

    pthread->st.reg[zero] = 0;
    pthread->st.fp[zero] = 0;

#ifdef gone
    /* Icodes for the terminator functions go here */
    idone1.func = cleanup1;
    idone1.cycles = 0;

    idone2.func = cleanup2;
    idone2.cycles = 0;

    idone3.func = cleanup3;
    idone3.cycles = 0;
#endif
}

#ifdef ITRACE
int itrace_enable = 0;
#endif

void 
init_event (thread_ptr pthread, event_ptr pevent)
{
    pthread->pevent = pevent;
    pevent->cpu_time = &pthread->cpu_time;
    pevent->pid = pthread->pid;
    pevent->next = NULL;
}

/*
 * Execute the thread specified by the task, till  a new event is
 * generated. This is the function that actually advances a thread, by
 * executing pieces of code, that do not have any interesting events.
 */
int
next_event (task_ptr ptask)
{
#ifdef SOLO
   ASSERT(0);
#else
    thread_ptr pthread;
    icode_ptr picode;
    int icycles;

    pthread = &threads[ptask->pid];
    pthread->time = ptask->time;
    picode = pthread->picode;

    /*
     * for the system call simulation to run correctly, we'll have to switch
     * the effective uid and gid, everytime we switch threads
     */

    /* Am I the almighty ? */
#ifdef ORACLE
    if (geteuid() == 0) {

	/* Yes I am! */
	if (setruid(pthread->uid) == -1)
	    perror("setuid");
	
	if (setrgid(pthread->gid) == -1)
	    perror("setgid");

    }
    WAKEUP (pthread);
#endif
    
    icycles = 0;

    /* the interpretation loop */
    while (pthread->runstate == R_RUN) {

	/* account for the current instruction */
	icycles += picode->cycles;

#ifdef ITRACE
	if (pthread->pid == 3)
	    if (itrace_enable)
		fprintf(itrace, "%lx\n", picode->addr);
#endif
	
	/* call the function that interprets the current instruction */
	picode = (picode->func) (picode, pthread);
    }

    /* update the thread to reflect the code that has been executed */
    pthread->picode = picode;
    pthread->time += icycles;
    pthread->cpu_time += icycles;

    /* set up a task to continue the thread */
    ptask->ufunc = pthread->ufunc;
    ptask->time = pthread->time;

    /*
     * Reset the pevent pointer in the task structure in case
     * the back end changed the pid for the task
     */

    ptask->pevent = pthread->pevent;
    pthread->pevent->time = pthread->time;
    ptask->priority = DEF_PRIORITY;
    ptask->tstack = NULL;
    task_insert (ptask);

    return T_YIELD;
#endif
}



/*
 * schedule_cleanup is called when the thread calls _exit(). T1 generates 
 * an event so that the accumulated time for this thread (as calculated
 * in the inner execution loop) will be added to its time value before it
 * reaps its children in T2.
 */
#ifdef gone
icode_ptr
schedule_cleanup(icode_ptr picode, thread_ptr pthread)
{
    event_yield (picode, pthread);
    /* idone2 causes terminate_thr() to be called. */
    return &idone1;
}

#endif
#ifdef gone
/* initiate the termination of the thread */
icode_ptr
cleanup1(icode_ptr picode, thread_ptr pthread)
{
    /* Generate an E_DONE event, and wait for the simulator to finish
     * executing all the tasks associated with this process.
     */
    event_terminate (pthread);

    /* idone3 is an icode that will cause terminator2()
     * to be called.
     */
    return &idone2;
}
#endif

#ifdef gone
icode_ptr
cleanup2(icode_ptr picode, thread_ptr pthread)
{
    thread_ptr parent;
    aint_time_t duration;
    event_ptr pevent;

    /* this is not the main thread */
    parent = pthread->parent;

    /* if the parent is waiting on us, then wake him up */
    if (parent) {
	if (parent->runstate == R_WAIT) {
	    /* advance the parent forward in time */
	    duration = pthread->time + picode->cycles - parent->time;
	    if (duration < 0) {
		/*
		 * In reality, this is an error. But the simulation
		 * algorithm, still allows this condition to occur.
		 */
	    } else {
		parent->time += duration;
	    }

	    wakeup_thr (parent);
	    pevent = event_unblock (pthread, parent);
	    pevent->duration = duration;
	    pevent->utype = E_WAIT;
	} else {
	    pthread->is_zombie = 1;
	}
    } else { /* this was thread 0 */
	cleanup3 (picode, pthread);
	/* Return value not used */
	return NULL;		
    }

    /* Next and final operation is to move this thread to the free queue */
    return &idone3;

}
#endif

/*
 * This is the last function called by a thread. It moves the thread
 * to the free queue if the parent has already waited on it.
 */
#ifdef gone
icode_ptr
cleanup3(icode_ptr picode, thread_ptr pthread)
{
    /*
     * Generate an event so that the task scheduler will decrement Nprocs.
     */
    event_done (pthread);

    /* free it's address space */
    free_addr_space(pthread); 
    
    /*
     * If parent has already waited on this thread, then we can move
     * this thread to the free queue. Otherwise, move this thread to
     * the done queue and the parent will move
     * this thread to the free queue when it waits on this thread.
     */
    if (pthread->parent == NULL) {
	INLINE_REMOVE (pthread);
	if (recycle_threads)
	    INLINE_INSERT_AFTER (&free_q, pthread);
	pthread->runstate = R_FREE;
    }
    else {
	INLINE_REMOVE (pthread);
	INLINE_ENQUEUE (&done_q, pthread);
	pthread->runstate = R_DONE;
    }

    return NULL;		/* return value not used */
}

#endif

/*
 * This routine is called when a process terminates in orer to wait for
 * all its children to complete. This guarantees that the elapsed time 
 * of the parent is at least as great as all of  its children.
 *
 * Return values:
 *   -1 if all children have finished
 *    0 if some child has not finished
 */

int
wait_for_children (thread_ptr pthread)
{
    int childpid;

    while ((childpid = wait_for_child (pthread)) > 0);

    return childpid;
}

/*
 * This routine is called by wait to wait for one child. If there is
 * more than one terminated child, the child that terminated first
 * is the one that is waited on.
 *
 * Return values:
 *   -1   no children exist
 *    0   no child has terminated yet
 *    pid the child pid of the earliest terminated child process
 */
int
wait_for_child (thread_ptr pthread)
{
    thread_ptr child;
    thread_ptr prev;
    aint_time_t min_time;

    child = pthread->youngest;

    /* if there are no children, return -1 */
    if (child == NULL)
	return -1;

    min_time = MAXTIME;
    /* search the list of children for one that has finished */
    for (; child; child = child->sibling) {
	if (child->runstate == R_DONE || child->is_zombie) {
	    /* find the earliest terminated child */
	    if (min_time > child->time)
		min_time = child->time;
	}
    }

    /* if no child has terminated, then return 0 */
    if (min_time == MAXTIME)
	return 0;

    prev = pthread;

    /* search the list of children again for the earliest one */
    for (child = pthread->youngest; child; child = child->sibling) {
	if (child->time == min_time
	    && (child->runstate == R_DONE || child->is_zombie)) {
	    /* disown child */
	    if (prev == pthread)
		pthread->youngest = child->sibling;
	    else
		prev->sibling = child->sibling;

	    /* collect cpu time of child */
	    pthread->child_cpu += child->cpu_time + child->child_cpu;

	    /* update parent's elapsed time to that of the child */
	    if (pthread->time < child->time)
		pthread->time = child->time;

	    /* Set child's parent pointer to NULL so that the child knows
	     * the parent waited on it already, and it can put itself
	     * on the free queue.
	     */
	    child->parent = NULL;

	    /* If child is not a zombie, then move it to the free pool.
	     * We can't move a zombie child to the free pool, since it
	     * still has to send an E_DONE event, so it is not yet
	     * free to be reallocated. A zombie child will move itself
	     * to the free pool if the parent has waited on it already.
	     */
	    if (child->runstate == R_DONE) {
		/* move child to free pool */
		INLINE_REMOVE (child);
		if (recycle_threads)
		    INLINE_INSERT_AFTER (&free_q, child);
		child->runstate = R_FREE;
	    }

	    return child->pid;
	}
	else
	    prev = child;
    }

    /* if we get here, it's an error */
    fatal ("wait_for_child: can't find earliest dead child\n");
    return 0;
}

/* Puts a thread to sleep */
void
sleep_thr (thread_ptr pthread)
{
#ifdef SOLO
   ASSERT(0);
#else
    INLINE_REMOVE (pthread);
    INLINE_ENQUEUE (&sleep_q, pthread);
    pthread->runstate = R_SLEEP;
#endif
}

/* wakes up a thread */
void
wakeup_thr (thread_ptr pthread)
{
#ifdef SOLO
   GammaUnstall(pthread);
   pthread->runstate = R_RUN;
   return;
#else
    INLINE_REMOVE (pthread);
    INLINE_ENQUEUE (&run_q, pthread);
    pthread->runstate = R_RUN;
#endif
}

/* wake up all threads on the queue */
void
wakeup_all (qnode_ptr q, thread_ptr pthread, int cycles, int subtype)
{
    thread_ptr pthr, tnext;
    aint_time_t duration, wake_time;
    event_ptr pevent;
#ifdef SOLO
    ASSERT(0);
#else
    wake_time = pthread->time + cycles;
    pthr = (thread_ptr) q->next;
    while (pthr != (thread_ptr) q) {
	/* save the pointer to the next thread on the queue */
	tnext = (thread_ptr) pthr->next;
	if (pthr->runstate != R_BLOCK)
	    fatal ("wakeup_all: runstate (%d) not R_BLOCK for thread %d\n",
		   pthr->runstate, pthr->pid);

	/* wake up this thread */
	wakeup_thr (pthr);
	duration = wake_time - pthr->time;
	if (duration < 0)
	    fatal ("wakeup_all: negative wait duration (%.1f) at time %.0f\n",
		   duration, wake_time);
	pthr->time += duration;
	pevent = event_unblock_list (pthread, pthr);
	pevent->duration = duration;
	pevent->utype = subtype;
	pthr = tnext;
    }
#endif
}

/*
 * Block the thread and put it on the specified wait queue.
 */
void
block_thr (thread_ptr q, thread_ptr pthread)
{
#ifdef SOLO
   ASSERT(0);
#else
    INLINE_REMOVE (pthread);
    INLINE_ENQUEUE (q, pthread);
    pthread->runstate = R_BLOCK;
#endif 
}

/* dequeue the first waiting thread and make it runnable right now */
thread_ptr
unblock_thr (thread_ptr q)
{
    thread_ptr pthread;
#ifdef SOLO
   ASSERT(0);
#else
    INLINE_DEQUEUE (q, pthread);
    if (pthread) {
	INLINE_ENQUEUE (&run_q, pthread);
	pthread->runstate = R_RUN;
    }
    return pthread;
#endif
}

/* schedule tasks to wakeup all the threads on the queue */
void
unblock_list (thread_ptr waker, thread_ptr q)
{
    thread_ptr pthread;

    while (!Q_EMPTY(q)) {
	INLINE_DEQUEUE(q, pthread);
	event_unblock(waker, pthread);
    }
}

#if gone
icode_ptr
event_read(icode_ptr picode, thread_ptr pthread)
{
    event_ptr pevent;
    long vaddr, paddr;

    /*
     * The vaddr calculated is the address passed on by the instruction
     * decoder.  The actual address referenced may be different, as in case
     * of unaligned loads (and stores)
     */
    vaddr = REG (RB) + picode->immed;
    pevent = pthread->pevent;

    pevent->vaddr = vaddr;
    pthread->vaddr = vaddr;

    paddr = PMAP (vaddr);

    if (IS_SHARED (vaddr)) {
	pevent->type = picode->iflags | E_SHARED;
#ifdef DEBUG
	if (vaddr == 0x801b30)
	    fprintf(stderr,"%d(%lx): %lx (%lx) ->",
		    pthread->pid,
		    picode->addr,
		    vaddr,
		    paddr);
#endif
    }
    else {
	pevent->type = picode->iflags;
    }
    if (picode->opnum == ldq_u_opn)
	paddr = (paddr & ~7);

    pevent->paddr = paddr;
    pthread->paddr = paddr;

    /* Get the data item and store it in pevent->data */
    if (paddr) {
	if (picode->size == 4)
	    pevent->data = *(int *) paddr;
	else {
	    if (paddr & 7)
		printf ("event_read: unaligned access pid %d paddr"
			"0x%lx vaddr 0x%lx pc 0x%lx inst %lx\n",
			pthread->pid, paddr, vaddr, picode->addr,
			picode->instr);
	    pevent->data = *(long *) paddr;
	}
    }

#ifdef DEBUG
    if (IS_SHARED(vaddr)) 
	if (vaddr == 0x801b30)
	    fprintf(stderr, " %lx\n", pevent->data);
#endif
    
    pevent->iaddr = picode->addr;
    pthread->ufunc = sim_null;	/* Does nothing - just returns T_ADVANCE */
    pevent->size = picode->size;

    pevent->picode = picode;

    /* Flag only if we need to trace. Always flag if addr invalid */

    if ((picode->trace_shared && IS_SHARED (vaddr)) ||
	(picode->trace_private && !IS_SHARED (vaddr)) ||
	((void *) paddr == NULL)) {

	pthread->ufunc = sim_read;
    }

    SLEEP (pthread);
    return (picode->next);
}

icode_ptr
event_write(icode_ptr picode, thread_ptr pthread)
{
    event_ptr pevent;
    long vaddr, paddr;

    vaddr = REG (RB) + picode->immed;
    pevent = pthread->pevent;
    pevent->vaddr = vaddr;
    pthread->vaddr = vaddr;

    paddr = PMAP (vaddr);

    if (IS_SHARED (vaddr)) {
	pevent->type = picode->iflags | E_SHARED;

#ifdef DEBUG
	if (vaddr == 0x801b30) {
#ifdef CALL_TRACE
	    if (pthread->pid == 1)
		call_trace_enable = 1;
#endif
	    fprintf(stderr,"%d(%lx): %lx (%lx) <-",
		    pthread->pid,
		    picode->addr,
		    vaddr,
		    paddr);
	}
#endif
    }
    else {
	pevent->type = picode->iflags;
    }

    if (picode->opnum == stq_u_opn)
	paddr = (paddr & ~7);

    /* Convert register value to memory word */
    if (picode->iflags & E_FLOAT) {
	aint_cvt_fp_mem (picode->opnum, FP (RA), &pevent->data);
	pevent->type |= E_FLOAT;
    }
    else
	pevent->data = REG (RA);

#ifdef DEBUG
    if (IS_SHARED(vaddr))
	if (vaddr == 0x801b30)
	    fprintf(stderr," %lx\n", pevent->data);
#endif
    
    pevent->paddr = paddr;
    pthread->paddr = paddr;

    pevent->iaddr = picode->addr;
    pthread->ufunc = sim_null;
    pevent->size = picode->size;

    pevent->picode = picode;

    /* Don't execute ufunc if not tracing it */
    if ((IS_SHARED (vaddr) && picode->trace_shared) ||
	(!IS_SHARED (vaddr) && picode->trace_private) ||
	((void *) paddr == NULL)) {
	pthread->ufunc = sim_write;
    }

    SLEEP (pthread);
    return (picode->next);
}

icode_ptr
event_load_locked(icode_ptr picode, thread_ptr pthread)
{
    event_ptr pevent;
    long vaddr, paddr;

    vaddr = REG (RB) + picode->immed;
    pevent = pthread->pevent;
    pevent->vaddr = vaddr;
    pthread->vaddr = vaddr;

    paddr = PMAP (vaddr);

    if (IS_SHARED (vaddr)) {
	pevent->type = picode->iflags | E_SHARED;
    }
    else {
	pevent->type = picode->iflags;
    }

    pevent->paddr = paddr;
    pthread->paddr = paddr;

    pevent->iaddr = picode->addr;
    pthread->ufunc = sim_load_locked;
    pevent->size = picode->size;

    pevent->picode = picode;

    SLEEP (pthread);
    return (picode->next);
}

icode_ptr
event_store_conditional(icode_ptr picode, thread_ptr pthread)
{
    event_ptr pevent;
    long vaddr, paddr;

    vaddr = REG (RB) + picode->immed;
    pevent = pthread->pevent;
    pevent->vaddr = vaddr;
    pthread->vaddr = vaddr;
    
    paddr = PMAP (vaddr);

    if (IS_SHARED (vaddr)) {
	pevent->type = picode->iflags | E_SHARED;
    }
    else {
	pevent->type = picode->iflags;
    }

    pevent->paddr = paddr;
    pthread->paddr = paddr;

    pevent->iaddr = picode->addr;
    pthread->ufunc = sim_store_conditional;
    pevent->size = picode->size;

    pevent->picode = picode;

    SLEEP (pthread);
    return (picode->next);
}


icode_ptr
event_memory_barrier(icode_ptr picode, thread_ptr pthread)
{
    event_ptr pevent;

    pevent = pthread->pevent;

    pevent->iaddr = picode->addr;
    pthread->ufunc = sim_memory_barrier;
    /*
     * This forces a reschedule, so that the mem-barrier semantics are
     * preserved 
     */
    pevent->picode = picode;

    SLEEP (pthread);
    return (picode->next);
}

icode_ptr
event_sim_user(icode_ptr picode, thread_ptr pthread)
{
    event_ptr pevent;

    pevent = pthread->pevent;
    pevent->iaddr = picode->addr;
    pevent->arg1 = pthread->st.reg[16];
    pevent->arg2 = pthread->st.reg[17];
    pevent->arg3 = pthread->st.reg[18];
    pevent->arg4 = pthread->st.reg[19];
    pthread->ufunc = sim_user;
    SLEEP (pthread);
    return (picode->next);
}

icode_ptr
event_inst(icode_ptr picode, thread_ptr pthread)
{
    event_ptr pevent;

    pevent = pthread->pevent;
    pevent->iaddr = picode->addr;
    pevent->picode = picode;
    pevent->type = picode->iflags;

    pthread->ufunc = sim_inst;

    /* Force a reschedule here since we want sim_inst to execute */
    SLEEP (pthread);
    return (picode->next);
}

void
event_terminate (thread_ptr pthread)
{
    event_ptr pevent;

    pevent = pthread->pevent;
    pevent->type = E_DONE;
    pthread->ufunc = task_terminate;
    SLEEP (pthread);
}

/*
 * Calling this functions has the effect of "generating" an event, that'll
 * block the thread
 */
void
event_block (thread_ptr pthread, int subtype)
{
    event_ptr pevent;

    pevent = pthread->pevent;
    pevent->type = E_BLOCK;
    pthread->ufunc =task_block;
    pevent->utype = subtype;
    SLEEP (pthread);
}

/*
 * Calling this functions has the effect of "generating" an event, that'll
 * block the thread
 */
void
event_timed_block (thread_ptr pthread, int subtype, ulong time)
{
    event_ptr pevent;

    pevent = pthread->pevent;
    pevent->type = E_BLOCK;
    pevent->arg1 = time;
    pthread->ufunc = task_timed_block;
    pevent->utype = subtype;
    SLEEP (pthread);
}

event_ptr
event_unblock (thread_ptr pthread1, thread_ptr pthread2)
{
    event_ptr pevent;

    pevent = pthread1->pevent;
    pevent->type = E_UNBLOCK;

    pevent->pevent = pthread2->pevent;
    /*
     * Set the pevent field in the event structure for the unblocked thread
     * to point to the unblocking thread's event structure so that sim_block()
     * can figure out who did the unblocking.
     */
    pthread2->pevent->pevent = pevent;
    pthread1->ufunc = task_unblock;

    /* Can't figure out why the first thread needs to sleep */
    SLEEP(pthread1);

    return pthread2->pevent;
}

/*
 * This function is called to force the scheduler out of its run-until-block
 * loop. It has the side-effect of updating the time accumulated in the
 * scheduler loop. This is only used internally in MINT. The back end
 * never sees these events.
 */
icode_ptr
event_yield(icode_ptr picode, thread_ptr pthread)
{
    event_ptr pevent;

    pevent = pthread->pevent;
    pevent->type = E_YIELD;
    pthread->ufunc = task_yield;
    SLEEP (pthread);
    return picode->next;
}



/*
 * This function is similar to event_unblock() except that it is
 * called when several threads are unblocked by a single event,
 * such as a barrier being released. This function is called once
 * for each unblocked thread. Each time this function is called
 * it adds the event structure for the unblocked thread to a list.
 * 
 * The first argument, pthread, is a pointer to the unblocking thread.
 * The second argument, pthr, is a pointer to the unblocked thread.
 * A pointer to the event structure for the unblocked thread is returned
 * so that the duration and utype fields can be set by the caller.
 */
event_ptr
event_unblock_list (thread_ptr pthread, thread_ptr pthr)
{
    event_ptr pevent;

    pevent = pthread->pevent;

    /* if the next field is NULL, then this is the first event in the list */
    if (pevent->next == NULL) {
	pevent->type = E_UNBLOCK;
	pevent->next = pthr->pevent;
	pthr->pevent->next = NULL;

	/* Set the pevent field in the event structure for the unblocked
	 * thread to point to the unblocking thread's event structure so
	 * that sim_block() can figure out who did the unblocking.
	 */
	pthr->pevent->pevent = pevent;
	pthread->ufunc = task_unblock_list;
	SLEEP (pthread);
	return pthr->pevent;
    }
    /* If we get here, then this is not the first event in the list,
     * so add it.
     */
    pthr->pevent->next = pevent->next;
    pevent->next = pthr->pevent;
    return pthr->pevent;
}

/* generates an exit event */
void
event_exit (thread_ptr pthread)
{
    event_ptr pevent;

    pevent = pthread->pevent;
    pevent->type = E_EXIT;
    pthread->ufunc = task_exit;
    SLEEP (pthread);
}


/* generates a done event */
void
event_done (thread_ptr pthread)
{
    pthread->ufunc = task_done;
    SLEEP (pthread);
}


icode_ptr
event_fork(icode_ptr picode, thread_ptr pthread)
{
    event_ptr pevent;

    pevent = pthread->pevent;

    pevent->type = E_FORK;
    pevent->pevent = pthread->youngest->pevent;
    pthread->ufunc = task_fork;
    SLEEP (pthread);

    return (picode->next);
}
#endif /* gone */

/*
 * This function is called from within next_event() when the feature is
 * enabled. next_event() will compare the address of the instruction to
 * be executed with the value of obj_hook, and call obj_hook_proc if they
 * are equal. This is used mainly for debugging. A sim_objhook()
 * back-end function could be useful here
 */

#ifdef gone

int
obj_hook_proc (icode_ptr picode, thread_ptr pthread)
{
    return 0;
}
#endif