aint_init.c 28.9 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
/*
 * 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 reading and parsing the text section and managing the memory
 * for an address space
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <time.h>
#include <sys/utsname.h>
#include <string.h>

/* All global variables are defined in this file */
#define MAIN

#include "simtypes.h"
#include "gamma.h"

#include "thread.h"
#include "event.h"
#include "opcodes.h"
#include "globals.h"
#include "alpha_regs.h"
#include "memory.h"
#include "subst.h"
#include "protos.h"

#include "sim_error.h"

void aint_stats ();
void read_hdrs (char *exec_filename, thread_ptr pthread);
void read_text (char *path, thread_ptr pthread);
#ifdef gone 
void decode_opnum (icode_ptr picode, unsigned instr);
void decode_instr (icode_ptr *itext, icode_ptr picode, unsigned instr,
		   ulong text_start);
#endif
static void create_initial_addr_space ();
void create_addr_space(thread_ptr pthread);
void init_all_queues ();
void copy_argv (thread_ptr pthread, int arg_start, int argc, char **argv,
		char **envp);
void init_thread (thread_ptr pthread, event_ptr pevent, int enqueue);
static void save_args (int argc, char **argv);
static void parse_args (int argc, char **argv);
void copy_addr_space (thread_ptr parent, thread_ptr child);
static void usage ();

/* The file pointer corresponding to the executable */
FILE *executable_fp;

static int aint_argc;
char **aint_argv;


/* Useful statistics */
static time_t start_time;
static time_t finish_time;
static char start_date[40];
static char finish_date[40];
static ulong mem_size_start;
static ulong mem_size_finish;
static struct rusage rusage;
static double total_elapsed_time;
static double total_cpu_time;

struct timeval start_real_time;
struct timezone tz;

#ifdef ITRACE
FILE *itrace;
#endif

/*
 * Does all the initialization, reads in the object file, creates address
 * spaces, stacks at the appropriate virtual addresses.
 */
void
aint_init (int argc, char **argv, char **envp)
{
    int next_arg;

#ifdef ITRACE
    if ((itrace = fopen("/udir/sharma/db_trace/aint/itrace", "w")) == 0)
	perror("fopen");
#endif
    
    /* Note the current time */
    start_time = time (NULL);

    /* To fool gettimeofday system call */
    if (gettimeofday(&start_real_time, &tz))
	perror("gettimeofday");
    
    /* Store the end of the data segment */
    mem_size_start = (ulong) sbrk (0);
    
    /* Yes, we want to be environmentally friendly */
    recycle_threads = 0;

    /* handle the arguments */
    sim_name = argv[0];
    /* Make a copy of the args */
    save_args (argc, argv);
    /* gobble up the aint args */
    parse_args (argc, argv);

    next_arg = 1;

    /* check for the executable name */
    if (next_arg >= argc) {
	fatal("Missing object file\n");
	usage ();
	exit (1);
    }
    exec_name = aint_argv[next_arg];

    /* initialize all the queues */
    init_all_queues ();

    /* initialize the main thread */
    init_main_thread ();

    /* read the headers */
    read_hdrs (exec_name,&threads[0]);

#ifdef gone
    /* Read the text section */
    read_text (exec_name,&threads[0]);
#endif
    /* Allocate memory and initialize address space */
    create_initial_addr_space ();

    /* Copy argc, argv, envp onto the process stack */
    copy_argv (&threads[0], next_arg, argc, argv, envp);

    /* Set up the substitutes for syscalls */
    subst_init ();

}


#ifndef DEBUG
#define USAGE "\nUsage: %s [aint options] [-- simulator options]\
 objfile [objfile options]\n\n\
 aint options:\n\
        [-C interval]        checkpoint interval\n\
        [-p procs]           number of per-process regions, default %d\n\
        [-t i]    trace instructions \n\
        [-t n]    trace nothing \n\
        [-t r]    trace memory references \n\
        [-t s]    trace shared references \n\
        [-t p]    trace private references \n\
        [-t x]    trace synchronization events \n\
        [-t a]    trace references AND synchs (default) \n\
                  (Only the last of multiple -t options holds) \
        [-s shmem_size]      shared memory size, default: %ld (0x%lx) \n\
        [-h heap_size]       heap size in bytes, default: %ld (0x%lx)\n\
        [-k stack_size]      stack size in bytes, default: %ld (0x%lx)\n\
        [-x inst_addr]       instr (hex addr) to flag: used for debugging \n"
#else
#define USAGE "\nUsage: %s [-p procs] [-t i|n|r|s|p|x|a]\
 [-- sim_opts] objfile\n"
#endif 

static void
usage ()
{
    fprintf (stderr, USAGE, sim_name, HEAP_SIZE, HEAP_SIZE, STACK_SIZE,
	     STACK_SIZE, MAX_NPROCS, SHMEM_SIZE(0), SHMEM_SIZE(0));
}

static void
save_args (int argc, char **argv)
{
    int i;

    /* copy the args */
    aint_argc = argc;
    aint_argv = (char **) malloc ((argc + 1) * sizeof (char *));
    if (aint_argv == NULL)
	fatal ("save_args: cannot allocate 0x%x bytes for aint_argv.\n",
	       (argc + 1) * sizeof (char *));
    for (i = 0; i < argc; i++) {
	aint_argv[i] = (char *) malloc (strlen (argv[i]) + 1);
	if (aint_argv[i] == NULL)
	    fatal ("save_args: cannot allocate 0x%x bytes for "
		   "aint_argv[%d].\n",
		   strlen (argv[i]) + 1, i);
	strcpy (aint_argv[i], argv[i]);
    }
    aint_argv[argc] = NULL;
}

/*
 * Print out the statistics and do the backend specified cleanup at the end.
 */
void
aint_done ()
{
    /* flush out any output from the simulated program */
    fflush(stdout);

#ifdef ITRACE
    fclose(itrace);
#endif ITRACE
    
    aint_stats ();

    return;
}


/* Prints out some statistics */
void
aint_stats ()
{
    int i;
    unsigned int elapsed, minutes, seconds;
    unsigned int user_sec, user_min, user_usec, sys_sec, sys_min, sys_usec;
    unsigned int total_sec, total_min, total_usec;
    double user_fsec, sys_fsec, total_fsec, cpu_seconds;
    int mem_used;
    char *machine_name;
    struct utsname utsname;

    finish_time = time (NULL);

    getrusage (RUSAGE_SELF, &rusage);

    fprintf (stderr, "\nCommand line: ");
    for (i = 0; i < aint_argc; i++)
	fprintf (stderr, " %s", aint_argv[i]);
    fprintf (stderr, "\n");

    strcpy (start_date, ctime (&start_time));

    /* remove trailing newline */
    start_date[strlen (start_date) - 1] = 0;
    fprintf (stderr, "\nStarted: %s\n", start_date);

    strcpy (finish_date, ctime (&finish_time));

    /* remove trailing newline */
    finish_date[strlen (finish_date) - 1] = 0;
    fprintf (stderr, "\nFinished: %s\n", finish_date);

    elapsed = finish_time - start_time;
    minutes = elapsed / 60;
    seconds = elapsed % 60;
    if (uname (&utsname) < 0)
	machine_name = "(unknown)";
    else
	machine_name = utsname.nodename;

    fprintf (stderr, "Elapsed time for simulation: %u:%02u on %s\n",
	     minutes, seconds, machine_name);

    user_sec = rusage.ru_utime.tv_sec;
    user_usec = rusage.ru_utime.tv_usec;

    user_min = user_sec / 60;
    user_sec = user_sec % 60;
    user_fsec = user_sec + (double) user_usec / 1.0e+6;

    sys_sec = rusage.ru_stime.tv_sec;
    sys_usec = rusage.ru_stime.tv_usec;

    sys_min = sys_sec / 60;
    sys_sec = sys_sec % 60;
    sys_fsec = sys_sec + (double) sys_usec / 1.0e+6;

    total_min = user_min + sys_min;
    total_sec = user_sec + sys_sec;
    total_usec = user_usec + sys_usec;

    if (total_usec >= 1000000) {
	total_sec++;
	total_usec -= 1000000;
    }
    if (total_sec >= 60) {
	total_min++;
	total_sec -= 60;
    }
    total_fsec = total_sec + (double) total_usec / 1.0e+6;
    cpu_seconds = total_min * 60 + total_fsec;

    fprintf (stderr,
	     "CPU time: user: %u:%05.2f, system: %u:%05.2f"
	     "total: %u:%05.2f (%.2f sec)\n", user_min, user_fsec, sys_min,
	     sys_fsec, total_min, total_fsec, cpu_seconds); 

    mem_size_finish = (ulong) sbrk (0);
    mem_used = mem_size_finish - mem_size_start;
    mem_used = (mem_used + 1023) / 1024;
    total_elapsed_time = threads[0].time;
    total_cpu_time = threads[0].cpu_time + threads[0].child_cpu;
    fprintf (stderr, "Space used by malloc: %dK\n", mem_used);

    fprintf (stderr, "\nElapsed simulated cycles: %.0f,"
	     "cpu cycles: %.0f\n", total_elapsed_time, total_cpu_time);

    fprintf (stderr, "Processors used = %d, average cpu_time/proc"
	     "= %.1f\n\n", max_pid + 1, total_cpu_time / (max_pid + 1));
}

/* Parse command line arguments */
static void
parse_args (int argc, char **argv)
{
    int c, errflag;
    extern char *optarg;
    extern int optind;

    /* Set up default values */
    default_heap_size = HEAP_SIZE;
    default_stack_size = STACK_SIZE;
    
    shmem_size = SHMEM_SIZE(0);
    ckpoint_freq = DEFAULT_CKPOINT_FREQ;
    max_nprocs = MAX_NPROCS;
    trace_option = TRACE_DEFAULT;	/* Defined in globals.h */


    /* ObjHook is used in next_event() to insert a call to the procedure
     * ObjHookProc when the PC value equals ObjHook. The default value is 0.
     *
     * Note: the code for inserting the hook is commented out in exec.c
     * to speed-up execution. To use, uncomment it and rebuild aint.
     */

    obj_hook = 0;

    errflag = 0;
    while ((c = getopt (argc, argv, "C:h:k:p:s:x:t:")) != -1) {
	switch (c) {
	    case 'C':
		ckpoint_freq = strtol (optarg, NULL, 0);
		break;
	    case 'h':
		default_heap_size = strtol (optarg, NULL, 0);
		break;
	    case 'k':
		default_stack_size = strtol (optarg, NULL, 0);
		break;
	    case 'p':
		max_nprocs = strtol (optarg, NULL, 0);
		break;
	    case 's':
		shmem_size = strtol (optarg, NULL, 0);
		fprintf (stderr, "parse_args(): shmem size %ld bytes\n",
			 shmem_size); 
		break;
	    case 'x':
		sscanf (optarg, "%lx", &obj_hook);
		fprintf (stderr, "Flagging instruction at address 0x%lx",
			 obj_hook); 
		break;

	    case 't':
		switch (*optarg) {
		    case 'i':
			trace_option = TRACE_INST;
			break;
		    case 'x':
			trace_option = TRACE_SYNC;
			break;
		    case 'n':
			trace_option = TRACE_NONE;
			break;
		    case 'r':
			trace_option = TRACE_REFS;
			break;
		    case 's':
			trace_option = TRACE_SHARED;
			break;
		    case 'p':
			trace_option = TRACE_PRIVATE;
			break;
		    case 'd':
		    default:
			trace_option = TRACE_DEFAULT;
			break;
		}
		break;

	    default:
		errflag = 1;
		break;
	}
    }

    if (errflag) {
	usage ();
	exit (1);
    }
}

/* 
 * Reads the text section of the object file and creates the linked list
 * of icode structures
 */
#ifdef gone
void
read_text (char *path, thread_ptr pthread)
{
    int i, err;
    icode_ptr picode, *pitext, pcopy;
    ulong addr;
    unsigned int instr;
    interpreter_t simfunc;
    ulong text_size, text_start;

    /*
     * Allocate space for pointers to icode.
     * Text_size is number of instructions
     */

    pitext = (icode_ptr *) aint_malloc ((pthread->headerp->text_size + 1)
					   * sizeof (icode_ptr));
    pthread->itext = pitext;

    
    /* Allocate space for the icode structures */
    picode = (icode_ptr) aint_malloc (pthread->headerp->text_size
				      * sizeof (struct icode));


    /* no need to error check after aint_malloc */
    
    /* Assign each pointer to its corresponding icode, and link each icode
     * to point to the next one in the array
     */
    text_size = pthread->headerp->text_size;
    
    for (i = 0; i < text_size; i++) {
	pitext[i] = &picode[i];
	picode[i].next = &picode[i + 1];
    }
    pitext[text_size] = NULL;


    /* seek to the beginning of text and start reading instructions */
    fseek (executable_fp, pthread->headerp->text_seek, SEEK_SET);

    picode = pitext[0];

    /* set the thread's picode to the first executable instruction */
    pthread->picode = pitext[0];

    text_start = pthread->headerp->text_start;
    addr = text_start;
    
    for (i = 0; i < text_size; i++, addr += 4, picode = picode->next) {
	err = fread (&instr, sizeof (int), 1, executable_fp);
	if (err < 1)
	    fatal ("read_text: end of file reading text section\n");
	picode->addr = addr;
	picode->cycles = 1;
	decode_instr (pthread->itext, picode, instr,text_start);

	/*
	 * At this time, we know the opnum, so we can access information
         * stored in the op_desc desc_table. Information desired is the
         * function pointer, the instruction type etc.
	 */
	picode->func = desc_table[picode->opnum].func;
	picode->iflags = desc_table[picode->opnum].iflags;
#ifdef DEBUG
	picode->opname = desc_table[picode->opnum].opname;
#endif	
	/*
	 * Check to see if this is the address of the sim_user
	 * procedure entry, and if so, insert a call to the sim_user
	 * event
	 */
	if (picode->addr == sim_user_addr) {
	    pcopy = (icode_ptr) malloc (sizeof (struct icode));
	    memcpy (pcopy, picode, sizeof (struct icode));
	    picode->next = pcopy;
	    picode->func = event_sim_user;
	    picode->cycles = 0;
	    picode = picode->next;
	}


	simfunc = (interpreter_t) NULL;
	if (picode->iflags & E_READ)
	    simfunc = event_read;
	else if (picode->iflags & E_WRITE)
	    simfunc = event_write;
	else if (picode->iflags & E_LD_L)
	    simfunc = event_load_locked;
	else if (picode->iflags & E_ST_C)
	    simfunc = event_store_conditional;
	else if (picode->iflags & E_BARRIER)
	    simfunc = event_memory_barrier;
	else if (trace_option & TRACE_INST)
	    simfunc = event_inst;

	if (trace_option & TRACE_REFS) {
	    picode->trace_private = trace_option & TRACE_PRIVATE;
	    picode->trace_shared = trace_option & TRACE_SHARED;
	}

	/* for all the load and store ops, record size and ufunc in icode */

	if (picode->iflags & (E_MEM_REF | E_LOCK)) {

	    switch (picode->opnum) {
		case ldl_opn:
		case stl_opn:
		case ldl_l_opn:
		case stl_c_opn:
		case ldf_opn:
		case lds_opn:
		case stf_opn:
		case sts_opn:
		    picode->size = 4;
		    break;
		case ldq_u_opn:
		case ldq_opn:
		case ldg_opn:
		case ldt_opn:
		case stq_u_opn:
		case stq_opn:
		case stg_opn:
		case stt_opn:
		case ldq_l_opn:
		case stq_c_opn:
		    picode->size = 8;
		    break;
		default:
		    break;
	    }
	}

	/* Make a copy of icode if we need to flag this inst */
	if (simfunc) {
	    /* Make copy of icode */
	    pcopy = (icode_ptr) malloc (sizeof (struct icode));
	    if (pcopy == NULL)
		perror("read_text");
	    memcpy (pcopy, picode, sizeof (struct icode));

	    picode->next = pcopy;
	    picode->func = simfunc;
	    picode->cycles = 0;
	    picode = picode->next;
	}
    }
}

#endif

/* Decode the given instruction: determine the aint opnum for the inst. */

#ifdef gone
void
decode_opnum (icode_ptr picode, unsigned instr)
{
    int i, opcode;
    union alpha_instruction uinst;

    picode->instr = instr;

    uinst = *(union alpha_instruction *) &instr;

    opcode = uinst.common.opcode;

    /*
     * Now, use the opcode and other fields to zero in on the actual
     * instruction.
     */
 
    switch (opcode) {
	case op_call_pal:
	    for (i = 0; pal_group[i].opkey != uinst.pal_format.function; i++);
	    picode->opnum = pal_group[i].opnum;
	    break;
	case op_inta:
	    for (i = 0; inta_group[i].opkey != uinst.o_format.function; i++);
	    picode->opnum = inta_group[i].opnum;
	    break;
	case op_intl:
	    for (i = 0; intl_group[i].opkey != uinst.o_format.function; i++);
	    picode->opnum = intl_group[i].opnum;
	    break;
	case op_intm:
	    for (i = 0; intm_group[i].opkey != uinst.o_format.function; i++);
	    picode->opnum = intm_group[i].opnum;
	    break;
	case op_ints:
	    for (i = 0; ints_group[i].opkey != uinst.o_format.function; i++);
	    picode->opnum = ints_group[i].opnum;
	    break;
	case op_fltv:
	    for (i = 0; fltv_group[i].opkey != uinst.f_format.function; i++);
	    picode->opnum = fltv_group[i].opnum;
	    break;
	case op_flti:
	    for (i = 0; flti_group[i].opkey != uinst.f_format.function; i++);
	    picode->opnum = flti_group[i].opnum;
	    break;
	case op_fltl:
	    for (i = 0; fltl_group[i].opkey != uinst.f_format.function; i++);
	    picode->opnum = fltl_group[i].opnum;
	    break;

	case op_misc:
	    for (i = 0;
		 misc_group[i].opkey != uinst.m_format.memory_displacement;
		 i++); 
	    picode->opnum = misc_group[i].opnum;
	    break;

	case op_jsr:
	    for (i = 0; jsr_group[i].opkey != uinst.j_format.function; i++);
	    picode->opnum = jsr_group[i].opnum;
	    break;

	default:
	    for (i = 0; defgroup[i].opkey != uinst.common.opcode; i++);
	    picode->opnum = defgroup[i].opnum;
    }
}
#endif

/* Decode an instruction: store information in an icode struct */

#ifdef gone
void
decode_instr (icode_ptr *itext,icode_ptr picode, unsigned instr,
	      ulong text_start)
{
    int opcode;
    union alpha_instruction uinst;

    picode->instr = instr;
    uinst = *(union alpha_instruction *) &instr;

    /* Get the aint operation number */

    opcode = uinst.common.opcode;
    decode_opnum (picode, instr);

    /*
     * Decode instruction arguments
     */

    switch (op_format[opcode]) {
	case PAL:
	    picode->immed = uinst.pal_format.function;
	    break;

	case reserved:
	    break;

	case memory:
	    picode->immed = uinst.m_format.memory_displacement;
	    /*
	     * Sign-extend the immed field if opcode is not op_misc
	     */
	    if (opcode != op_misc) {
		picode->immed = (picode->immed |
				 ((picode->immed >> 15) ? ~0xffff : 0));
		if (picode->opnum == ldah_opn)
		    picode->immed = picode->immed << 16;
	    }
	    picode->args[RB] = uinst.m_format.rb;
	    /*
	     * If this is a load, redirect loads into the zero reg.
	     * Also, the m_format is being used to handle j_format,
	     * so look for those...
	     */
	    switch (uinst.m_format.opcode) {
		case op_ldq_u:
		case op_ldf:
		case op_ldg:
		case op_lds:
		case op_ldt:
		case op_ldl:
		case op_ldq:
		case op_ldl_l:
		case op_ldq_l:
		case op_jsr:
		    picode->args[RA] = ZERO_REDIRECT (uinst.m_format.ra);
		    break;
		default:
		    picode->args[RA] = uinst.m_format.ra;
		    break;
	    }
	    break;

	case operate:
	    if (!uinst.o_format.form) {
		picode->args[RA] = uinst.o_format.ra;
		picode->args[RB] = uinst.o_format.rb;
		picode->args[RC] = ZERO_REDIRECT (uinst.o_format.rc);
		picode->literal = 0xffff;
		picode->immed = uinst.o_format.function;
	    }
	    else {
		picode->args[RA] = uinst.l_format.ra;
		picode->args[RC] = ZERO_REDIRECT (uinst.o_format.rc);
		picode->literal = uinst.l_format.literal;
		picode->immed = uinst.l_format.function;
	    }
	    break;
	case branch:
	    picode->immed = uinst.b_format.branch_displacement;
	    picode->immed = (picode->immed | ((picode->immed >> 20) ?
					      ~0x1fffff : 0)); 
	    picode->target = IV2R (picode->addr + (picode->immed << 2) + 4);
	    switch (uinst.b_format.opcode) {
		case op_br:
		case op_bsr:
		    picode->args[RA] = ZERO_REDIRECT (uinst.b_format.ra);
		    break;
		default:
		    picode->args[RA] = uinst.b_format.ra;
		    break;
	    }
	    break;
	case floating:
	    picode->args[RA] = uinst.f_format.fa;
	    picode->args[RB] = uinst.f_format.fb;
	    picode->args[RC] = ZERO_REDIRECT (uinst.f_format.fc);
	    picode->immed = uinst.f_format.function;
	    break;
	default:
	    break;
    }
}
#endif

static void
create_initial_addr_space()
{
    create_addr_space(&threads[0]);
}

void
create_addr_space (thread_ptr pthread)
{
    ulong  total_size, data_size, dwords;
    ulong read_only_size;
    struct header *h = pthread->headerp;

    data_size = h->bss_size + h->data_size + HEAP_SIZE + STACK_SIZE;
    data_size = ALIGN(data_size, M_ALIGN);

    read_only_size = h->pdata_size + h->rdata_size + h->rconst_size;
    read_only_size = ALIGN(read_only_size, M_ALIGN);

    h->total_data_size = data_size;
    
    shmem_start = SP_SHMAT_START;
    shmem_end = shmem_start;


    if (h->pdata_size > 0) {
	int dw;
	dwords = h->pdata_size / sizeof (int);
	fseek (executable_fp, h->data_seek, SEEK_SET);
	for (dw = 0; dw < dwords; dw++) {
	    int *pos = (int *) addr2phys (pthread, ((long) h->pdata_start)
					  + (dw << 2)); 

	    if (fread ((caddr_t) pos, sizeof (int), 1, executable_fp) < 1) {
		fatal ("create_addr_space: EOF reading Pdata section.\n");
	    }
	}
    }

    /* If the Rdata section comes before Data, read it first */
    if (h->rdata_size > 0) {
	int dw;
	dwords = h->rdata_size / sizeof (int);
	fseek (executable_fp, h->rdata_seek, SEEK_SET);
	for (dw = 0; dw < dwords; dw++) {
	    int *pos = (int *) addr2phys (pthread, ((long) h->rdata_start)
					  + (dw << 2));

	    if (fread ((caddr_t) pos, sizeof (int), 1, executable_fp) < 1) {
		fatal ("create_addr_space: EOF reading Rdata section.\n");
	    }
	}
    }
    /*
     * New (gamma-SOLO)
     */
    if (h->text_size > 0) {
	int dw;
	dwords = h->text_size;
	fseek (executable_fp, h->text_seek, SEEK_SET);
	for (dw = 0; dw < dwords; dw++) {
	   long addr = (long) h->text_start + (dw << 2);
	    int *pos = (int *) text_addr2phys (pthread, addr);
	    ASSERT(pos);
	    if (fread ((caddr_t) pos, sizeof (int), 1, executable_fp) < 1) {
		fatal ("create_addr_space: EOF reading text section.\n");
	    }
	}
    }

    if (h->rconst_size > 0) {
	int dw;

	dwords = h->rconst_size / sizeof (int);
	fseek (executable_fp, h->rconst_seek, SEEK_SET);
	for (dw = 0; dw < dwords; dw++) {
	    int *pos = (int *) addr2phys (pthread, ((long) h->rconst_start)
					  + (dw << 2));

	    if (fread ((caddr_t) pos, sizeof (int), 1, executable_fp) < 1) {
		fatal ("create_addr_space: EOF reading Rconst section.\n");
	    }
	}
    }

    /* Read in the initialized global variable space at the beginning of the
     * Private space
     */
    if (h->data_size > 0) {
	int dw;

	dwords = h->data_size / sizeof (int);
	fseek (executable_fp, h->data_seek, SEEK_SET);
	for (dw = 0; dw < dwords; dw++) {
	    int *pos = (int *) addr2phys (pthread, ((long) h->data_start)
					  + (dw << 2));
	    if (fread ((caddr_t) pos, sizeof (int),
		       1, executable_fp) < 1) {
		fatal ("create_addr_space: end-of-file"
		       " reading data section\n");
	    }
	}
    }

    /* set up the stack pointer */
    pthread->st.reg[REG_SP] = h->data_start + h->total_data_size;
	
    pthread->stacktop = pthread->st.reg[REG_SP] - default_stack_size;

    /* Set the gp register */
    pthread->st.reg[REG_GP] = h->gp; 

    pthread->st.PC = h->text_start;

    /* we're done */
    fclose(executable_fp);

}

void
copy_argv (thread_ptr pthread, int arg_start, int argc, char **argv,
	   char **envp)
{
    int i, size, nenv;
    long *sp;
    int argc_obj;
    char **argv_obj, **eptr, **envp_obj;

    /* add up sizes of all parameters */
    size = 0;
    for (i = arg_start; i < argc; i++) {
	size += (strlen (argv[i]) + 1);
    }


    nenv = 0;
    for (eptr = envp; *eptr; nenv++, eptr++)
	size += strlen (*eptr) + 1;

    argc_obj = argc - arg_start;

    /* add in space for the argv and envp pointers, including NULLs */
    size += (argc_obj + nenv + 2) * sizeof (char *);

    size += sizeof (long);	/* for argc */

    /* Round up size to quad word boundary */
    size = (size + 0x7) & (~0x7);

    /* Allocate space on stack for the args */
    pthread->st.reg[REG_SP] -= size;
    /* sp = (long *) MAP(pthread->st.reg[30]); */
    sp = (long *) pthread->st.reg[REG_SP];

    /* First item is argc */
#ifdef DEBUG_ARGV
    printf ("Writing argc=%d at 0x%lx\n", argc_obj, (long) sp);
#endif

    /* *sp++ = argc_obj; */
    *(long *) addr2phys (pthread, (long) sp) = argc_obj;
    sp++;

    /* Next comes the array of pointers argv */
    argv_obj = (char **) sp;

    /* Leave space for the argv array, including the NULL pointer */
    sp += (argc_obj + 1);

    /* Pointers for the environment vars */
    envp_obj = (char **) sp;
    sp += (nenv + 1);

    /* Copy the args to the stack of the main thread */
    for (i = arg_start; i < argc; i++) {
	int j;

#ifdef DEBUG_ARGV
	printf ("Storing arg %s at 0x%lx\n", argv[i], (long) sp);
#endif

	/* strcpy((char *) addr2phys(pthread, (long)sp), argv[i]); */
	for (j = 0; j <= strlen (argv[i]); j++) {
	    *(char *) addr2phys (pthread, (long) sp + j) = (argv[i])[j];
	}
	*(long *) addr2phys (pthread, (long) (argv_obj + i - arg_start)) =
	    ((long) sp); 
	sp = (long *) ((long) sp + strlen (argv[i]) + 1);
    }
    *(long **) addr2phys (pthread, (long) (argv_obj + argc - arg_start)) =
	NULL; 



    /* Copy the environment variables to the stack of the main thread */
    for (i = 0, eptr = envp; i < nenv; i++, eptr++) {

	int j;

#ifdef DEBUG_ARGV
	printf ("Storing envstr %s at 0x%lx\n", *eptr, (long) sp);
#endif

	for (j = 0; j <= strlen (*eptr); j++) {
	    *(char *) addr2phys (pthread, (long) sp + j) = (*eptr)[j];
	}

	/* strcpy((char *)addr2phys(pthread, (long)sp), *eptr); */

#ifdef DEBUG_ARGV
	printf ("storing addr of envstr, 0x%lx at 0x%lx (0x%lx)\n", (long)
		PUNMAP ((long) sp), 
		(long) &envp_obj[i], PUNMAP ((long) &envp_obj[i]));
#endif

	*(long *) addr2phys (pthread, (long) (envp_obj + i)) = ((long) sp);
	sp = (long *) ((long) sp + strlen (*eptr) + 1);
    }
    *(char **) addr2phys (pthread, (long) (envp_obj + nenv)) = NULL;

    /* Debug: print the values of things on stack. */
#ifdef DEBUG_ARGV
    for (i = 0; i < 30; i++) {
	printf ("0x%lx (0x%lx): 0x%lx\n", pthread->st.reg[REG_SP] + 8 * i,
		PMAP (pthread->st.reg[REG_SP] + 8 * i),
		*(long *) (PMAP (pthread->st.reg[REG_SP] + 8 * i))); 
    }
#endif
}

/*
 * Copy the parent's address space to the child's. This also sets up
 * all the mapping fields in the child's thread structure.
 */
void
copy_addr_space (thread_ptr parent, thread_ptr child)
{
    int i, pid;
    void *tb_page_freelist, *next_free_page;
    int tbkey;
    struct shm_descriptor *parent_shm_ds, *child_shm_ds;

    /* The address space has already been allocated */
    pid = child->pid;

    /*
     * for each page-table entry, duplicate the entry, and the page from
     * parent
     */ 
    tb_page_freelist = (void *) mmap ((caddr_t) NULL,
				      parent->num_private * TB_PAGESIZE,
				      PROT_READ | PROT_WRITE,
				      MAP_ANONYMOUS | MAP_VARIABLE
				      | MAP_PRIVATE, -1, 0);
    if (tb_page_freelist == (caddr_t) (-1))
	fatal ("Copy_addr_space: Cannot allocate %d pages for child\n",
	       parent->num_private);
    next_free_page = tb_page_freelist;
    
    for (tbkey = 0; tbkey < TB_SIZE; tbkey++) {
	page_t *parent_pg, *child_pg;

	parent_pg = parent->page_bucket[tbkey];
	if (parent_pg) {
	    page_t *pg;
	    pg = (page_t *) malloc (sizeof (page_t));  
	    if (pg == NULL)
		fatal("aint_fork: no space for the page table\n");
	    else {
		child->page_bucket[tbkey] = pg;
		child->page_bucket[tbkey]->lookaside = pg;
	    }
	}
	child_pg = child->page_bucket[tbkey];

	while (parent_pg) {
	    if (parent_pg->next)
		child_pg->next = (page_t *) malloc (sizeof(page_t));
	    else
		child_pg->next = NULL;

	    child_pg->tag = parent_pg->tag;

	    child_pg->flags = parent_pg->flags;

	    /* DONT COPY IF PAGE IS SHARED!!! */
	    if (parent_pg->flags & PAGE_PRIVATE) {
		child_pg->page = next_free_page;
		next_free_page = (void *) ((long) next_free_page
					   + TB_PAGESIZE);
		memcpy (child_pg->page, parent_pg->page, TB_PAGESIZE);
	    }
	    else {
		/* This is a shared page simply put in a link */
		child_pg->page = parent_pg->page;
	    }

	    child_pg = child_pg->next;
	    parent_pg = parent_pg->next;

	}
    }
    child->num_pages = parent->num_pages;
    child->num_private = parent->num_private;


    /* Need to copy the Shared-regions list... Currently working under
       *  the assumption that parent won't attach segments before a fork
     */
    parent_shm_ds = parent->shmem_regions;
    if (parent_shm_ds) {
	child->shmem_regions = (struct shm_descriptor *)
	    malloc (sizeof (struct shm_descriptor));
	child_shm_ds = child->shmem_regions;
	if (child_shm_ds == NULL)
	    fatal ("copy_addr_space: cannot allocate"
		   " shmem descriptor (local)\n");
    }
    for (; parent_shm_ds; parent_shm_ds = parent_shm_ds->next) {
	/* Copy contents */
	child_shm_ds->shmid = parent_shm_ds->shmid;
	child_shm_ds->size = parent_shm_ds->size;
	child_shm_ds->addr = parent_shm_ds->addr;
	if (parent_shm_ds->next) {
	    child_shm_ds->next = (struct shm_descriptor *)
		malloc (sizeof (struct shm_descriptor));
	    if (child_shm_ds->next == NULL)
		fatal ("copy_addr_space: cannot allocate"
		       " shmem descriptor (local)\n");
	}
	else
	    child_shm_ds->next = NULL;

	child_shm_ds = child_shm_ds->next;
    }

    /* Copy the value of the next shmat map to use */
    child->unsp_shmat_current = parent->unsp_shmat_current;

    /* Copy all the registers */
    for (i = 0; i < 33; i++) {
	child->st.reg[i] = parent->st.reg[i];
	child->st.fp[i] = parent->st.fp[i];
    }
    child->st.fpcr = parent->st.fpcr;

}

/*
 * Frees the address space of a thread.
 */
free_addr_space(thread_ptr pthread)
{
    ulong tb_key, vaddr;

    for(tb_key=0; tb_key < TB_SIZE; tb_key++) {
	page_t *entry = pthread->page_bucket[tb_key];

	for(; entry ; ) {
	    page_t *tmp = entry;

	    /* recompute the virtual address */
	    vaddr = (entry->tag << TB_LISTIDX_LENGTH) + tb_key;
	    vaddr <<= TB_OFFSET_LENGTH;
	    
	    /* unmap the page, if it is not shared */
	    if (!IS_SHARED(vaddr))
		if ( munmap(entry->page, TB_PAGESIZE) == -1 )
		    perror("munmap");

	    /* move on */
	    entry = entry->next;

	    /* release the memory for the entry */
	    free(tmp);
	}

	pthread->page_bucket[tb_key] = NULL;
    }
}

static text_cache_t text_mapping[MAX_EXECUTABLES];
static int num_entries = 0;