Esempio n. 1
0
//****************************************************************************
PCB* CreateProcess(char* name, int type, ProcessEntry entry, int priority, 
    long* dstID, long* dstErr)
{
    // Global process ID number is assigned to each process created.
    static long gCurrentProcessID = 0;

    // Create a PCB for the new process.
    PCB* pcb = (PCB*)ALLOC(PCB);
    pcb->type = type;
    pcb->entry = entry;
    pcb->priority = priority;
    pcb->readyQueueKey = priority;
    size_t len = strlen(name);
    pcb->name = (char*)malloc(len + 1);
    strcpy(pcb->name, name);
    pcb->name[len] = 0;
    pcb->processID = ++gCurrentProcessID;
    pcb->messages = (List*)ALLOC(List);
    pcb->trackTable = calloc(VIRTUAL_MEM_PAGES, sizeof(TrackTableItem));

    // Return process id to the caller.
    if( dstID )
    {
        *dstID = pcb->processID;
    }
    if( dstErr )
    {
        *dstErr = ERR_SUCCESS;
    }

    // Only add user process to global list and queue.
    if( type == PROCESS_TYPE_USER )
    {
        // Add to global process list.
        ListNode* pcbNode = (ListNode*)ALLOC(ListNode);
        pcbNode->data = (void*)pcb;
        ListAttach(gGlobalProcessList, pcbNode);

        // Add to ready queue.
        PushToReadyQueue(pcb);
    }

    // Create hardware context for the process.
    Z502MakeContext(&pcb->context, (void*)entry, USER_MODE);

    return pcb;
}
Esempio n. 2
0
void os_create_process( char* process_name, void* scontext, long Prio ) {
    void                *next_context;
    PCB *pcb = (PCB *)malloc(sizeof(PCB));
    strcpy(pcb->Processname , process_name);
    pcb->status = 0;
    pcb->Priority = Prio;
    pcb->context = scontext;
    pcb->pid = Pid;
    //readyfront = pcb;
    //readyrear = pcb;
    pcb->next=NULL;
    pcb->status=CURRENT_RUNNING;
    Running = pcb;
    Pid++;
    Z502MakeContext( &next_context, pcb->context, USER_MODE );
    Z502SwitchContext( SWITCH_CONTEXT_SAVE_MODE, &next_context );
}
Esempio n. 3
0
void sample_code(void) {
	INT32 i, j, k; /* Index & counters       */
	long Value;

	INT32 disk_id, sector; /* Used for disk requests */
	char disk_buffer_write[PGSIZE ];
	char disk_buffer_read[PGSIZE ];
	char physical_memory_write[PGSIZE ];
	char physical_memory_read[PGSIZE ];

	void *context_pointer; /* Used for context commands*/
	void *starting_address;
	BOOL kernel_or_user;
	short random_buckets[NUM_RAND_BUCKETS];
	INT32 LockResult;

	INT32 Status;
	INT32 Temp, Temp1;

	/*********************************************************************
	 Show the interface to the delay timer.
	 Eventually the timer will interrupt ( in base.c there's a handler for
	 this ), but here in sample_code.c we're merely showing the interface
	 to start the call.
	 *********************************************************************/
	MEM_READ(Z502TimerStatus, &Status);
	if (Status == DEVICE_FREE)
		printf("Got expected result for Status of Timer\n");
	else
		printf("Got erroneous result for Status of Timer\n");

	Temp = 777; /* You pick the time units */
	MEM_WRITE(Z502TimerStart, &Temp);
	MEM_READ(Z502TimerStatus, &Status);
	if (Status == DEVICE_IN_USE)
		printf("Got expected result for Status of Timer\n");
	else
		printf("Got erroneous result for Status of Timer\n");
	printf("The next output from the Interrupt Handler should report that \n");
	printf("   interrupt of device 4 has occurred with no error.\n");
	Z502Idle();                //  Let the interrupt for this timer occur

	//  Now we're going to try an illegal time and ensure that the fault
	// handler reports an illegal status.
	Temp = -77; /* You pick the time units */
	printf("The next output from the Interrupt Handler should report that \n");
	printf(
			"   interrupt of device 4 has occurred with an ERR_BAD_PARAM = 1.\n");
	MEM_WRITE(Z502TimerStart, &Temp);
	MEM_READ(Z502TimerStatus, &Status);
	if (Status == DEVICE_FREE)          // Bogus value shouldn't start timer
		printf("Got expected result for Status of Timer\n");
	else
		printf("Got erroneous result for Status of Timer\n");
	fflush(stdout);
	//   ZCALL( Z502_IDLE() );           //  Let the interrupt for this timer occur

	/*  The interrupt handler will have exercised the code doing
	 Z502InterruptDevice,  Z502InterruptStatus, and Z502InterruptClear.
	 But they will have been tested only for "correct" usage.
	 Let's try a few erroneous/illegal operations.          */

	// Set an illegal device as target of our query
	Temp = LARGEST_STAT_VECTOR_INDEX + 1;
	MEM_WRITE(Z502InterruptDevice, &Temp);
	// Now read the status of this device
	MEM_READ(Z502InterruptStatus, &Status);
	if (Status == ERR_BAD_DEVICE_ID)
		printf("Got expected result for Status of Illegal Device\n");
	else
		printf("Got erroneous result for Status of Illegal Device\n");

	/*********************************************************************
	 Show the interface to the Z502_CLOCK.
	 This is easy - all we're going to do is read it twice and make sure
	 the time is incrementing.
	 *********************************************************************/

	MEM_READ(Z502ClockStatus, &Temp);
	MEM_READ(Z502ClockStatus, &Temp1);
	if (Temp1 > Temp)
		printf("The clock time incremented correctly - %d1, %d2\n", Temp,
				Temp1);
	else
		printf("The clock time did NOT increment correctly - %d1, %d2\n", Temp,
				Temp1);

	/*********************************************************************
	 Show the interface to the disk read and write.
	 Eventually the disk will interrupt ( in base.c there's a handler for
	 this ), but here in sample_code.c we're merely showing the interface
	 to start the call.
	 *********************************************************************/

	disk_id = 1; /* Pick arbitrary disk location             */
	sector = 3;
	/* Put data into the buffer being written   */
	strncpy(disk_buffer_write, "123456789abcdef", 15);
	/* Do the hardware call to put data on disk */
	MEM_WRITE(Z502DiskSetID, &disk_id);
	MEM_READ(Z502DiskStatus, &Temp);
	if (Temp == DEVICE_FREE)        // Disk hasn't been used - should be free
		printf("Got expected result for Disk Status\n");
	else
		printf("Got erroneous result for Disk Status - Device not free.\n");
	MEM_WRITE(Z502DiskSetSector, &sector);
	MEM_WRITE(Z502DiskSetBuffer, (INT32 * )disk_buffer_write);
	Temp = 1;                        // Specify a write
	MEM_WRITE(Z502DiskSetAction, &Temp);
	Temp = 0;                        // Must be set to 0
	MEM_WRITE(Z502DiskStart, &Temp);
	// Disk should now be started - let's see
	MEM_WRITE(Z502DiskSetID, &disk_id);
	MEM_READ(Z502DiskStatus, &Temp);
	if (Temp == DEVICE_IN_USE)        // Disk should report being used
		printf("Got expected result for Disk Status\n");
	else
		printf("Got erroneous result for Disk Status\n");

	/* Wait until the disk "finishes" the write. the write is an
	 "unpended-io", meaning the call returns before the work is
	 completed.  By doing the IDLE here, we wait for the disk
	 action to complete.    */
	MEM_WRITE(Z502DiskSetID, &disk_id);
	MEM_READ(Z502DiskStatus, &Temp);
	while (Temp != DEVICE_FREE) {
		Z502Idle();
		MEM_READ(Z502DiskStatus, &Temp);
	}
	/* Now we read the data back from the disk.  If we're lucky,
	 we'll read the same thing we wrote!                     */

	MEM_WRITE(Z502DiskSetSector, &sector);
	MEM_WRITE(Z502DiskSetBuffer, (INT32 * )disk_buffer_read);
	Temp = 0;                        // Specify a read
	MEM_WRITE(Z502DiskSetAction, &Temp);
	Temp = 0;                        // Must be set to 0
	MEM_WRITE(Z502DiskStart, &Temp);

	/* wait for the disk action to complete.  */
	MEM_WRITE(Z502DiskSetID, &disk_id);
	MEM_READ(Z502DiskStatus, &Temp);
	while (Temp != DEVICE_FREE) {
		Z502Idle();
		MEM_READ(Z502DiskStatus, &Temp);
	}

	printf("\n\nThe disk data written is: %s\n", disk_buffer_write);
	printf("The disk data read    is: %s\n", disk_buffer_read);

	/*********************************************************************
	 Let's try some intentional errors to see what happens
	 *********************************************************************/
	Temp = 0;                        // Must be set to 0
	MEM_WRITE(Z502DiskStart, &Temp);
	// Try reading the status without setting an ID
	MEM_READ(Z502DiskStatus, &Temp);
	if (Temp == ERR_BAD_DEVICE_ID)
		printf("Got expected result for Disk Status when using no ID\n");
	else
		printf("Got erroneous result for Disk Status when using no ID\n");

	// Try entering a bad ID and then reading the status
	disk_id = 999;
	MEM_WRITE(Z502DiskSetID, &disk_id);
	MEM_READ(Z502DiskStatus, &Temp);
	if (Temp == ERR_BAD_DEVICE_ID)
		printf("Got expected result for Disk Status when using bad ID\n");
	else
		printf("Got erroneous result for Disk Status when using bad ID\n");

	//  Try doing everything right EXCEPT entering the buffer address,
	disk_id = 1; /* Pick arbitrary disk location             */
	sector = 3;

	MEM_WRITE(Z502DiskSetID, &disk_id);
	MEM_WRITE(Z502DiskSetSector, &sector);
// Don't do this ->    MEM_WRITE( Z502DiskSetBuffer, (INT32 *)disk_buffer_write );
	Temp = 1;                        // Specify a write
	MEM_WRITE(Z502DiskSetAction, &Temp);
	Temp = 0;                        // Must be set to 0
	MEM_WRITE(Z502DiskStart, &Temp);
	// Disk should now not be started - it was missing vital info
	MEM_WRITE(Z502DiskSetID, &disk_id);
	MEM_READ(Z502DiskStatus, &Temp);
	if (Temp == DEVICE_FREE)        // Disk should report being free
		printf("Got expected result for Disk Status when missing data\n");
	else
		printf("Got erroneous result for Disk Status when missing data\n");
	/*********************************************************************
	 Some of the tests put thousands of pages of data on the disk.  Let's
	 see if we can do that here.   The pages ARE being written to the disk,
	 but the interrupt handler doesn't show all of them happening because
	 it's not catching multiple interrupts.
	 *********************************************************************/

	disk_id = 1;
	sector = 0;
	printf("The following section will take a few seconds\n");
	for (j = 0; j < VIRTUAL_MEM_PGS + 100 /* arbitrary # */; j++) {
		MEM_WRITE(Z502DiskSetID, &disk_id);
		MEM_WRITE(Z502DiskSetSector, &sector);
		MEM_WRITE(Z502DiskSetBuffer, (INT32 * )disk_buffer_write);
		Temp = 1;                        // Specify a write
		MEM_WRITE(Z502DiskSetAction, &Temp);
		Temp = 0;                        // Start the disk
		MEM_WRITE(Z502DiskStart, &Temp);
		MEM_WRITE(Z502DiskSetID, &disk_id);
		MEM_READ(Z502DiskStatus, &Temp);
		while (Temp == DEVICE_IN_USE)        // Disk should report being used
		{
			//printf( "Got erroneous result for Disk Status when writing lots of blocks\n" );
			Temp = 5; /* You pick the time units */
			MEM_WRITE(Z502TimerStart, &Temp);
			MEM_READ(Z502DiskStatus, &Temp);
		}
		sector++;
		if (sector >= NUM_LOGICAL_SECTORS) {
			sector = 0;
			disk_id++;
		}
	}
	/*********************************************************************
	 Do a physical memory access to check out that it works.
	 *********************************************************************/
	printf("\nStarting test of physical memory write and read.\n");
	for (i = 0; i < PGSIZE ; i++) {
		physical_memory_write[i] = i;
	}
	Z502WritePhysicalMemory(17, (char *) physical_memory_write);
	Z502ReadPhysicalMemory(17, (char *) physical_memory_read);
	for (i = 0; i < PGSIZE ; i++) {
		if (physical_memory_write[i] != physical_memory_read[i])
			printf("Error in Physical Memory Access\n");
	}
	printf("Completed test of physical memory write and read.\n");
	/*********************************************************************
	 Start all of the disks at the same time and see what happens.
	 *********************************************************************/
	/*
	 sector   = 0;
	 for ( disk_id = 1; disk_id <= MAX_NUMBER_OF_DISKS  ; disk_id++ )
	 {
	 MEM_WRITE( Z502DiskSetID, &disk_id );
	 MEM_WRITE( Z502DiskSetSector, &sector );
	 MEM_WRITE( Z502DiskSetBuffer, (INT32 *)disk_buffer_write );
	 Temp = 1;                        // Specify a write
	 MEM_WRITE( Z502DiskSetAction, &Temp );
	 Temp = 0;                        // Start the disk
	 MEM_WRITE( Z502DiskStart, &Temp );
	 sector++;
	 }
	 //  Now wait until all disks have finished
	 for ( disk_id = 1; disk_id <= MAX_NUMBER_OF_DISKS  ; disk_id++ )
	 {
	 MEM_WRITE( Z502DiskSetID, &disk_id );
	 MEM_READ( Z502DiskStatus, &Temp );
	 while ( Temp == DEVICE_IN_USE )        // Disk should report being used
	 {
	 //ZCALL( Z502_IDLE() );
	 DoSleep(50);
	 }
	 }
	 */
	printf("Disk multiple-block test is complete\n\n");

	/*********************************************************************
	 Show the interface to read and write of real memory
	 It turns out, that though these are hardware calls, the Z502
	 assumes the calls are being made in user mode.  Because the
	 process we're running here in "sample" is in kernel mode,
	 the calls don't work correctly.  For working examples of
	 these calls, see test2a.
	 *********************************************************************/

	Z502_PAGE_TBL_LENGTH = 64;
	Z502_PAGE_TBL_ADDR = (UINT16 *) calloc(sizeof(UINT16),
			Z502_PAGE_TBL_LENGTH);
	i = PTBL_VALID_BIT;
	Z502_PAGE_TBL_ADDR[0] = (UINT16) i;
	i = 73;
	MEM_WRITE(0, &i);
	MEM_READ(0, &j);
	/*  WE expect the data read back to be the same as what we wrote  */
	if (i == j)
		printf("Memory write and read completed successfully\n");
	else
		printf("Memory write and read were NOT successful.\n");

	/*********************************************************************
	 This is the interface to the locking mechanism.  These are hardware
	 interlocks.  We need to test that they work here.  This is the
	 interface we'll be using.

	 void    Z502_READ_MODIFY( INT32 VirtualAddress, INT32 NewLockValue,
	 INT32 Suspend, INT32 *LockResult )

	 We've defined these above to help remember them.
	 #define                  DO_LOCK                     1
	 #define                  DO_UNLOCK                   0
	 #define                  SUSPEND_UNTIL_LOCKED        TRUE
	 #define                  DO_NOT_SUSPEND              FALSE

	 *********************************************************************/

	printf("++++  Starting Hardware Interlock Testing   ++++\n");

	//  TRY A SERIES OF CALLS AS DESCRIBED HERE
	printf("These tests map into the matrix describing the Z502_READ_MODIFY\n");
	printf("     described in Appendix A\n\n");

	printf(
			"1.  Start State = Unlocked:  Action (Thread 1) = Lock: End State = Locked\n");
	READ_MODIFY(MEMORY_INTERLOCK_BASE, DO_LOCK, SUSPEND_UNTIL_LOCKED,
			&LockResult);
	printf("%s\n", &(Success[SPART * LockResult]));

	printf(
			"2.  Start State = locked(1): Action (Thread 1) = unLock: End State = UnLocked\n");
	READ_MODIFY(MEMORY_INTERLOCK_BASE, DO_UNLOCK, SUSPEND_UNTIL_LOCKED,
			&LockResult);
	printf("%s\n", &(Success[SPART * LockResult]));

	printf(
			"3.  Start State = Unlocked:  Action (Thread 1) = unLock: End State = UnLocked\n");
	printf("    An Error is Expected\n");
	READ_MODIFY(MEMORY_INTERLOCK_BASE, DO_UNLOCK, SUSPEND_UNTIL_LOCKED,
			&LockResult);
	printf("%s\n", &(Success[SPART * LockResult]));

	printf(
			"4.  Start State = unlocked:  Action (Thread 1) = tryLock: End State = Locked\n");
	READ_MODIFY(MEMORY_INTERLOCK_BASE, DO_LOCK, DO_NOT_SUSPEND, &LockResult);
	printf("%s\n", &(Success[SPART * LockResult]));

	printf(
			"5.  Start State = Locked(1): Action (Thread 1) = tryLock: End State = Locked\n");
	READ_MODIFY(MEMORY_INTERLOCK_BASE, DO_LOCK, DO_NOT_SUSPEND, &LockResult);
	printf("%s\n", &(Success[SPART * LockResult]));

	printf(
			"6.  Start State = locked(1): Action (Thread 1) = unLock: End State = UnLocked\n");
	READ_MODIFY(MEMORY_INTERLOCK_BASE, DO_UNLOCK, SUSPEND_UNTIL_LOCKED,
			&LockResult);
	printf("%s\n", &(Success[SPART * LockResult]));

	printf(
			"7.  Start State = Unlocked:  Action (Thread 1) = Lock: End State = Locked\n");
	READ_MODIFY(MEMORY_INTERLOCK_BASE, DO_LOCK, SUSPEND_UNTIL_LOCKED,
			&LockResult);
	printf("%s\n", &(Success[SPART * LockResult]));

	//  A thread that locks an item it has already locked will succeed
	printf(
			"8.  Start State = locked(1): Action (Thread 1) = Lock: End State = Locked\n");
	READ_MODIFY(MEMORY_INTERLOCK_BASE, DO_LOCK, SUSPEND_UNTIL_LOCKED,
			&LockResult);
	printf("%s\n", &(Success[SPART * LockResult]));

	// Locking a thread that's already locked - but do it with a thread
	// other than the one that did the locking.
	printf(
			"9.  Start State = Locked(1): Action (Thread 2) = tryLock: End State = Locked\n");
	printf("    An Error is Expected\n");
	Status = CreateAThread((int *) DoOnelock, &Temp);
	DoSleep(100); /*  Wait for that thread to finish   */

	// Unlock a thread that's already locked - but unlock it with a thread
	// other than the one that did the locking.
	printf(
			"10. Start State = Locked(1): Action (Thread 2) = unLock: End State = Locked\n");
	printf("    An Error is Expected\n");
	Status = CreateAThread((int *) DoOneUnlock, &Temp);
	DoSleep(100); /*  Wait for that thread to finish   */

	// The second thread will try to get the lock held by the first thread.  This is OK
	// but the second thread will suspend until the first thread releases the lock.
	printf(
			"11. Start State = Locked(1): Action (Thread 2) = Lock: End State = Locked\n");
	Status = CreateAThread((int *) DoOneTrylock, &Temp);
	DoSleep(100); /*  Wait for that thread to finish   */

	//  The first thread does an unlock.  This means the second thread is now able to get
	//  the lock so there is a "relock" when thread two succeeds.
	printf(
			"12. Start State = locked(1): Action (Thread 1) = unLock: End State = Locked(by 2)\n");
	READ_MODIFY(MEMORY_INTERLOCK_BASE, DO_UNLOCK, SUSPEND_UNTIL_LOCKED,
			&LockResult);
	printf("%s\n", &(Success[SPART * LockResult]));
	DoSleep(100); /*  Wait for locking action of 2nd thread to finish   */

	printf(
			"13. Start State = Locked(2): Action (Thread 3) = unLock: End State = Locked(2)\n");
	printf("    An Error is Expected\n");
	Status = CreateAThread((int *) DoOneUnlock, &Temp);
	DoSleep(100); /*  Wait for that thread to finish   */

	printf(
			"14. Start State = Locked(2): Action (Thread 1) = tryLock: End State = Locked(2)\n");
	READ_MODIFY(MEMORY_INTERLOCK_BASE, DO_LOCK, DO_NOT_SUSPEND, &LockResult);
	printf("%s\n", &(Success[SPART * LockResult]));

	printf(
			"15. Start State = locked(2): Action (Thread 1) = unLock: End State = Locked(2)\n");
	printf("    An Error is Expected\n");
	READ_MODIFY(MEMORY_INTERLOCK_BASE, DO_UNLOCK, SUSPEND_UNTIL_LOCKED,
			&LockResult);
	printf("%s\n", &(Success[SPART * LockResult]));

	printf("++++  END of hardware interlock code  ++++\n\n");

	/*********************************************************************
	 Show the interface to the CONTEXT calls.   We aren't going to do a
	 SWITCH_CONTEXT here, because that would cause us to start a
	 process in a strange place and we might never return here.

	 But we do all the setup required.
	 *********************************************************************/

	/* The context_pointer is returned by the MAKE_CONTEXT call.        */

	starting_address = (void *) starting_point_for_new_context;
	kernel_or_user = USER_MODE;
	Z502MakeContext(&context_pointer, starting_address, kernel_or_user);
	Z502DestroyContext(&context_pointer);

	/*********************************************************************
	 Show the interface to the scheduler printer.
	 *********************************************************************/

	CALL(SP_setup( SP_TIME_MODE, 99999 ));
	CALL(SP_setup_action( SP_ACTION_MODE, "CREATE" ));
	CALL(SP_setup( SP_TARGET_MODE, 99L ));
	CALL(SP_setup( SP_RUNNING_MODE, 99L ));
	for (j = 0; j < SP_MAX_NUMBER_OF_PIDS ; j++)
		CALL(SP_setup( SP_READY_MODE, j ));
	for (j = 0; j < SP_MAX_NUMBER_OF_PIDS ; j++)
		CALL(SP_setup( SP_WAITING_MODE, j+20 ));
	for (j = 0; j < SP_MAX_NUMBER_OF_PIDS ; j++)
		CALL(SP_setup( SP_SUSPENDED_MODE, j+40 ));
	for (j = 0; j < SP_MAX_NUMBER_OF_PIDS ; j++)
		CALL(SP_setup( SP_SWAPPED_MODE, j+60 ));
	for (j = 0; j < SP_MAX_NUMBER_OF_PIDS ; j++)
		CALL(SP_setup( SP_TERMINATED_MODE, j+80 ));
	for (j = 0; j < SP_MAX_NUMBER_OF_PIDS ; j++)
		CALL(SP_setup( SP_NEW_MODE, j+50 ));
	CALL(SP_print_header());
	CALL(SP_print_line());

	/*********************************************************************
	 Show the interface to the memory_printer.
	 *********************************************************************/

	for (j = 0; j < 64; j = j + 2) {
		MP_setup((INT32) (j), (INT32) (j / 2) % 10, (INT32) j * 16 + 10,
				(INT32) (j / 2) % 8);
	}
	MP_print_line();

	/*********************************************************************
	 Show how the skewed random numbers work on this platform.
	 *********************************************************************/

	for (j = 0; j < NUM_RAND_BUCKETS; j++)
		random_buckets[j] = 0;

	for (j = 0; j < 100000; j++) {
		get_skewed_random_number(&Value, NUM_RAND_BUCKETS);
		random_buckets[Value]++;
	}
	printf("\nTesting that your platform produces correctly skewed random\n");
	printf("numbers.  Each row should have higher count than the previous.\n");
	printf("    Range:   Counts in this range.\n");
	for (j = 0; j < NUM_RAND_BUCKETS; j = j + 8) {
		k = 0;
		for (i = j; i < j + 8; i++)
			k += random_buckets[i];
		printf("%3d - %3d:  %d\n", j, j + 7, k);
	}

	/*********************************************************************
	 Show the interface to the Z502Halt.
	 Note that the program will end NOW, since we don't return
	 from the command.
	 *********************************************************************/

	Z502Halt();

}
Esempio n. 4
0
File: base.c Progetto: ShunYao/OS502
void    osInit( int argc, char *argv[]  ) {
    void                *next_context;
    INT32               i;
	void         		*process_ptr;
	INT32               j;

    /* Demonstrates how calling arguments are passed thru to here       */

    printf( "Program called with %d arguments:", argc );
    for ( i = 0; i < argc; i++ )
        printf( " %s", argv[i] );
    printf( "\n" );
    printf( "Calling with argument 'sample' executes the sample program.\n" );

    /*          Setup so handlers will come to code in base.c           */

    TO_VECTOR[TO_VECTOR_INT_HANDLER_ADDR]   = (void *)interrupt_handler;
    TO_VECTOR[TO_VECTOR_FAULT_HANDLER_ADDR] = (void *)fault_handler;
    TO_VECTOR[TO_VECTOR_TRAP_HANDLER_ADDR]  = (void *)svc;

    /*  Determine if the switch was set, and if so go to demo routine.  */

    if (( argc > 1 ) && ( strcmp( argv[1], "sample" ) == 0 ) ) {
        Z502MakeContext( &next_context, (void *)sample_code, KERNEL_MODE );
        Z502SwitchContext( SWITCH_CONTEXT_KILL_MODE, &next_context );
    }                   /* This routine should never return!!           */
	
    /*  This should be done by a "os_make_process" routine, so that
        test0 runs on a process recognized by the operating system.    */
	else if(( argc > 1 ) && ( strcmp( argv[1], "test1a" ) == 0 ) )
		{
			process_ptr = test1a;
			os_create_process("test1a", process_ptr, LEGAL,&j, &j,RUN);
		} 
	
	else if(( argc > 1 ) && ( strcmp( argv[1], "test1b" ) == 0 ) )
		{
			process_ptr = test1b;
			os_create_process("test1b", process_ptr, LEGAL ,&j, &j,RUN);

		}
	else if(( argc > 1 ) && ( strcmp( argv[1], "test1c" ) == 0 ) )
		{
		
			process_ptr = test1c;
			os_create_process("test1c", process_ptr, LEGAL ,&i, &j,RUN);
		}
	else if(( argc > 1 ) && ( strcmp( argv[1], "test1d" ) == 0 ) )
		{
			process_ptr = test1d;
			os_create_process("test1d", process_ptr, LEGAL ,&i, &j,RUN);
		}
	else if(( argc > 1 ) && ( strcmp( argv[1], "test1e" ) == 0 ) )
	{
		process_ptr = test1e;
		os_create_process("test1e", process_ptr, LEGAL ,&i, &j,RUN);
	}
	else if(( argc > 1 ) && ( strcmp( argv[1], "test1f" ) == 0 ) )
	{
		process_ptr = test1f;
		os_create_process("test1f", process_ptr, LEGAL ,&i, &j,RUN);
	}
	else if(( argc > 1 ) && ( strcmp( argv[1], "test1g" ) == 0 ) )
	{
		process_ptr = test1g;
		os_create_process("test1g", process_ptr, LEGAL ,&i, &j,RUN);
	}
	else if(( argc > 1 ) && ( strcmp( argv[1], "test1h" ) == 0 ) )
	{
		process_ptr = test1h;
		os_create_process("test1h", process_ptr, LEGAL ,&i, &j,RUN);
	}
	else if(( argc > 1 ) && ( strcmp( argv[1], "test1i" ) == 0 ) )
	{
		process_ptr = test1i;
		os_create_process("test1i", process_ptr, LEGAL ,&i, &j,RUN);
	}
	else if(( argc > 1 ) && ( strcmp( argv[1], "test1j" ) == 0 ) )
	{
		process_ptr = test1j;
		os_create_process("test1j", process_ptr, LEGAL ,&i, &j,RUN);
	}
	else 
	{
		process_ptr = test1l;
		os_create_process("test1l", process_ptr, LEGAL ,&i, &j,RUN);
	}
}
Esempio n. 5
0
File: base.c Progetto: ShunYao/OS502
void make_context (S_PCB *PCB_PRT, void *procPTR )
{

	CALL(Z502MakeContext(&PCB_PRT->context, procPTR, USER_MODE ));
}
Esempio n. 6
0
void    osInit( int argc, char *argv[]  ) {
    void                *next_context;
    INT32               i;
    void                *scontext = (void *)test1a;
    int                 Prio = 8;
    int                 j,frameindex = 0;
    PCB *pcb = (PCB *)malloc(sizeof(PCB));
    char name[20] ="testbase";
    char test[20];
    Frame *frame;

    /*let's just initialize all the frames here*/
    for(j=0; j<64; j++) {
        frame = (Frame *)malloc(sizeof(Frame));
        frame->framenumber = frameindex;
        frame->framestatus = 0;
        frame->next = NULL;

        if(framefront == NULL) {
            framefront = frame;
            framerear = frame;
        }

        else {
            framerear->next = frame;
            framerear = frame;
        }
        frameindex++;
    }

    shadowinit();
    frameinit();
    /* Demonstrates how calling arguments are passed thru to here       */

    printf( "Program called with %d arguments:", argc );
    for ( i = 0; i < argc; i++ )
        printf( " %s", argv[i] );
    printf( "\n" );
    printf( "Calling with argument 'sample' executes the sample program.\n" );

    /*          Setup so handlers will come to code in base.c           */

    TO_VECTOR[TO_VECTOR_INT_HANDLER_ADDR]   = (void *)interrupt_handler;
    TO_VECTOR[TO_VECTOR_FAULT_HANDLER_ADDR] = (void *)fault_handler;
    TO_VECTOR[TO_VECTOR_TRAP_HANDLER_ADDR]  = (void *)svc;

    /*  Determine if the switch was set, and if so go to demo routine.  */

    if (( argc > 1 ) && ( strcmp( argv[1], "sample" ) == 0 ) ) {
        Z502MakeContext( &next_context, (void *)sample_code, KERNEL_MODE );
        Z502SwitchContext( SWITCH_CONTEXT_KILL_MODE, &next_context );
    }                   /* This routine should never return!!           */
    //os_create_process("testbase", scontext, 10);
    /*  This should be done by a "os_make_process" routine, so that
        test0 runs on a process recognized by the operating system.    */
    strcpy(pcb->Processname , name);
    pcb->status = 0;
    pcb->Priority = Prio;

    pcb->pid = Pid;
    //readyfront = pcb;
    //readyrear = pcb;
    pcb->next=NULL;
    //pcb->status=CURRENT_RUNNING;
    Running = pcb;
    Pid++;

    printf("please input the test you want to enter:\n");
    scanf("%s",&test);
    if(strcmp(test,"test1a")==0) {
        Z502MakeContext( &next_context, (void *)test1a, USER_MODE );
    }
    else if(strcmp(test,"test1b")==0) {
        Z502MakeContext( &next_context, (void *)test1b, USER_MODE );
    }
    else if(strcmp(test,"test1c")==0) {
        Z502MakeContext( &next_context, (void *)test1c, USER_MODE );
    }
    else if(strcmp(test,"test1d")==0) {
        Z502MakeContext( &next_context, (void *)test1d, USER_MODE );
    }
    else if(strcmp(test,"test1e")==0) {
        Z502MakeContext( &next_context, (void *)test1e, USER_MODE );
    }
    else if(strcmp(test,"test1f")==0) {
        Z502MakeContext( &next_context, (void *)test1f, USER_MODE );
    }
    else if(strcmp(test,"test1g")==0) {
        Z502MakeContext( &next_context, (void *)test1g, USER_MODE );
    }
    else if(strcmp(test,"test1h")==0) {
        Z502MakeContext( &next_context, (void *)test1h, USER_MODE );
    }
    else if(strcmp(test,"test1i")==0) {
        Z502MakeContext( &next_context, (void *)test1i, USER_MODE );
    }
    else if(strcmp(test,"test1j")==0) {
        Z502MakeContext( &next_context, (void *)test1j, USER_MODE );
    }
    else if(strcmp(test,"test1k")==0) {
        Z502MakeContext( &next_context, (void *)test1k, USER_MODE );
    }
    else if(strcmp(test,"test2a")==0) {
        Z502MakeContext( &next_context, (void *)test2a, USER_MODE );
    }
    else if(strcmp(test,"test2b")==0) {
        Z502MakeContext( &next_context, (void *)test2b, USER_MODE );
    }
    else if(strcmp(test,"test2c")==0) {
        Z502MakeContext( &next_context, (void *)test2c, USER_MODE );
    }
    else if(strcmp(test,"test2d")==0) {
        Z502MakeContext( &next_context, (void *)test2d, USER_MODE );
    }
    else if(strcmp(test,"test2e")==0) {
        Z502MakeContext( &next_context, (void *)test2e, USER_MODE );
    }
    else if(strcmp(test,"test2f")==0) {
        Z502MakeContext( &next_context, (void *)test2f, USER_MODE );
    }
    else if(strcmp(test,"test2g")==0) {
        Z502MakeContext( &next_context, (void *)test2g, USER_MODE );
    }
    //Z502MakeContext( &next_context, (void *)test1e, USER_MODE );
    pcb->context = next_context;
    Z502SwitchContext( SWITCH_CONTEXT_SAVE_MODE, &next_context );
}                                               // End of osInit
Esempio n. 7
0
void    svc( SYSTEM_CALL_DATA *SystemCallData ) {
    short               call_type;
    static short        do_print = 10;
    short               i;
    int                 Time;
    int                 ID;
    int                 k=0,m=0,n=0;
    int                 PR;
    int                 sid,tid,mlen;
    int                 actual_length;
    long                tmp;
    INT32               diskstatus;
    long                tmpid;
    void                *next_context;
    char                *tmpmsg;
    PCB                 *head;
    PCB                 *head1;
    PCB *pcb = (PCB *)malloc(sizeof(PCB));


    call_type = (short)SystemCallData->SystemCallNumber;
    if ( do_print > 0 ) {
        printf( "SVC handler: %s\n", call_names[call_type]);
        for (i = 0; i < SystemCallData->NumberOfArguments - 1; i++ ) {
            //Value = (long)*SystemCallData->Argument[i];
            printf( "Arg %d: Contents = (Decimal) %8ld,  (Hex) %8lX\n", i,
                    (unsigned long )SystemCallData->Argument[i],
                    (unsigned long )SystemCallData->Argument[i]);
        }
    }
    switch(call_type) {
    case SYSNUM_GET_TIME_OF_DAY:
        MEM_READ(Z502ClockStatus, &Time);
        //Z502_REG1=Time;
        *SystemCallData->Argument[0]=Time;
        break;

    case SYSNUM_TERMINATE_PROCESS:
        tmpid = (long)SystemCallData->Argument[0];
        if(tmpid>=0) {
            os_delete_process_ready(tmpid);
            Z502_REG9 = ERR_SUCCESS;
        }
        else if(tmpid == -1)
        {
            head =Running;
            Running = NULL;
            while(readyfront==NULL&&timerfront==NULL) {
                Z502Halt();
            }
            //free(head);
            dispatcher();
            Z502SwitchContext( SWITCH_CONTEXT_SAVE_MODE, &(Running->context) );
        }
        else
            Z502Halt();
        break;
    //the execution of sleep();
    case SYSNUM_SLEEP:
        start_timer( (int)SystemCallData->Argument[0] );

        break;
    case SYSNUM_GET_PROCESS_ID:
        *SystemCallData->Argument[1] = os_get_process_id((char *)SystemCallData->Argument[0]);
        break;
    case SYSNUM_CREATE_PROCESS:
        strcpy(pcb->Processname , (char *)SystemCallData->Argument[0]);
        pcb->Priority = (long)SystemCallData->Argument[2];
        head = readyfront;
        head1 = readyfront;
        if(Pid < 20) {
            if(pcb->Priority >0) {
                if(readyfront == NULL&&readyrear == NULL) {
                    readyfront = pcb;
                    readyrear = pcb;
                    //*SystemCallData->Argument[4] = ERR_SUCCESS;
                    Z502_REG9 = ERR_SUCCESS;
                    pcb->pid = Pid;
                    pcb->next=NULL;
                    Toppriority = pcb->Priority;
                    *SystemCallData->Argument[3] = Pid;
                    //pcb->context = (void *)SystemCallData->Argument[1];
                    Z502MakeContext( &next_context, (void *)SystemCallData->Argument[1], USER_MODE );
                    pcb->context = next_context;
                    Pid++;
                }
                else if(readyfront!=NULL&&readyrear!=NULL) {

                    if(checkPCBname(pcb) == 1) {

                        if(pcb->Priority < Toppriority) {
                            Z502_REG9 = ERR_SUCCESS;
                            pcb->next = readyfront;
                            readyfront = pcb;
                            pcb->pid = Pid;
                            pcb->next=NULL;
                            Z502MakeContext( &next_context, (void *)SystemCallData->Argument[1], USER_MODE );
                            pcb->context = next_context;
                            *SystemCallData->Argument[3] = Pid;
                            Pid++;
                        }
                        else {
                            while(head->next!=NULL) {
                                if(pcb->Priority < head->Priority)
                                    break;
                                else
                                    head = head->next;
                            }

                            if(head->next!=NULL) {
                                while(head1->next!=head)
                                {
                                    head1=head1->next;
                                }
                                Z502_REG9 = ERR_SUCCESS;
                                head1->next = pcb;
                                pcb->next=head;
                                pcb->pid = Pid;
                                Z502MakeContext( &next_context, (void *)SystemCallData->Argument[1], USER_MODE );
                                pcb->context = next_context;
                                *SystemCallData->Argument[3] = Pid;
                                Pid++;
                            }
                            else {
                                if(pcb->Priority < head->Priority) {
                                    while(head1->next!=head)
                                    {
                                        head1=head1->next;
                                    }
                                    Z502_REG9 = ERR_SUCCESS;
                                    head1->next=pcb;
                                    pcb->next=head;
                                    pcb->pid = Pid;
                                    Z502MakeContext( &next_context, (void *)SystemCallData->Argument[1], USER_MODE );
                                    pcb->context = next_context;
                                    *SystemCallData->Argument[3] = Pid;
                                    Pid++;
                                }
                                else {
                                    Z502_REG9 = ERR_SUCCESS;
                                    head->next = pcb;
                                    readyrear = pcb;
                                    pcb->next=NULL;
                                    pcb->pid = Pid;
                                    Z502MakeContext( &next_context, (void *)SystemCallData->Argument[1], USER_MODE );
                                    pcb->context = next_context;
                                    *SystemCallData->Argument[3] = Pid;
                                    Pid++;
                                }
                            }

                        }
                    }
                    else free(pcb);
                }
            } else free(pcb);
        }
        else {
            Z502_REG9++;
            free(pcb);
        }
        break;

    case SYSNUM_SUSPEND_PROCESS:
        ID = (int)SystemCallData->Argument[0];
        head = suspendfront;
        while(head!=NULL) {
            if(head->pid == ID) {
                n = 1;
                break;
            }
            else
                head = head->next;
        }

        if(n!=1) {
            head = readyfront;
            while(head!=NULL) {
                if(head->pid == ID) {
                    Z502_REG9 = ERR_SUCCESS;
                    suspend_process_ready(head);
                    k = 1;
                    break;
                }
                else
                    head = head->next;
            }

            if(k == 0) {
                head = timerfront;
                while(head!=NULL) {
                    if(head->pid == ID) {
                        Z502_REG9 = ERR_SUCCESS;
                        suspend_process_timer(head);
                        m = 1;
                        break;
                    }
                    else
                        head=head->next;
                }
                if(m == 0&&k == 0) {
                    printf("illegal PID\n");
                }
            }
        }
        if(n == 1) {
            printf("can not suspend suspended process\n");
        }
        break;

    case SYSNUM_RESUME_PROCESS:
        ID = (int)SystemCallData->Argument[0];
        head = suspendfront;
        while(head!=NULL) {
            if(head->pid == ID) {
                k=1;
                break;
            }
            else
                head=head->next;
        }
        if(k==1)
            resume_process(head);
        else
            printf("error\n");
        break;
    case SYSNUM_CHANGE_PRIORITY:
        ID = (int)SystemCallData->Argument[0];
        PR = (int)SystemCallData->Argument[1];
        if(ID == -1) {
            Running->Priority = PR;
            Z502_REG9 = ERR_SUCCESS;
        }
        else {
            if(PR < 100) {
                if(checkReady(ID) == 1) {
                    head = readyfront;
                    while(head->pid != ID)
                        head=head->next;
                    change_priority_ready(head,PR);
                    Z502_REG9 = ERR_SUCCESS;
                }
                else if(checkTimer(ID) == 1) {
                    head = timerfront;
                    while(head->pid != ID)
                        head=head->next;
                    change_priority_timer(head,PR);
                    Z502_REG9 = ERR_SUCCESS;
                }
                else if(checkSuspend(ID) == 1) {
                    head = suspendfront;
                    while(head->pid != ID)
                        head = head->next;
                    change_priority_suspend(head,PR);
                    Z502_REG9 = ERR_SUCCESS;
                }
                else {
                    printf("ID ERROR!\n");
                }
            }
            else {
                printf("illegal Priority");
            }
        }
        break;
    case SYSNUM_SEND_MESSAGE:
        sid = Running->pid;
        tid = (int)SystemCallData->Argument[0];
        tmpmsg =(char *)SystemCallData->Argument[1];
        mlen = (int)SystemCallData->Argument[2];
        if(maxbuffer < 8) {
            if(tid < 100) {
                if(mlen < 100)
                {
                    if(tid>0)
                    {   send_message(sid,tid,mlen,tmpmsg);
                        maxbuffer++;
                    }
                    else if(tid == -1) {
                        send_message_to_all(sid,mlen,tmpmsg);
                        maxbuffer++;
                    }
                }
                else {
                    printf("illegal length!\n");
                }
            } else
                printf("illegal id!\n");
        }
        else
        {   printf("no space!\n");
            Z502_REG9++;
        }
        break;
    case  SYSNUM_RECEIVE_MESSAGE:
        sid = (int)SystemCallData->Argument[0];
        mlen = (int)SystemCallData->Argument[2];

        if(sid < 100) {
            if(mlen < 100) {
                if(sid == -1) {

                    receive_message_fromall();
                    if(msgnum>0) {
                        actual_length = strlen(checkmsg->msg_buffer);
                        if(mlen >actual_length) {
                            msg_out_queue(checkmsg);
                            *SystemCallData->Argument[3] = actual_length;
                            *SystemCallData->Argument[4] = checkmsg->source_pid;
                            strcpy((char *)SystemCallData->Argument[1] ,checkmsg->msg_buffer);
                            Z502_REG9 = ERR_SUCCESS;
                        }
                        else {
                            printf("small buffer!\n");
                        }
                    }
                }
                else {
                    receive_message_fromone(sid);
                    if(msgnum>0) {
                        actual_length = strlen(checkmsg->msg_buffer);
                        if(mlen >actual_length) {
                            msg_out_queue(checkmsg);
                            *SystemCallData->Argument[3] = actual_length;
                            *SystemCallData->Argument[4] = checkmsg->source_pid;
                            strcpy((char *)SystemCallData->Argument[1], checkmsg->msg_buffer);
                            Z502_REG9 = ERR_SUCCESS;
                        }
                        else {
                            printf("small buffer!\n");
                        }
                    }
                }
            }
            else
                printf("illegal length!\n");
        }
        else
            printf("illegal id!\n");
        break;
    case  SYSNUM_DISK_READ:
        disk_read(SystemCallData->Argument[0],SystemCallData->Argument[1],SystemCallData->Argument[2]);

        break;
    case SYSNUM_DISK_WRITE:
        disk_write(SystemCallData->Argument[0],SystemCallData->Argument[1],SystemCallData->Argument[2]);
        break;
    default:
        printf("call_type %d cannot be recognized\n",call_type);
        break;
    }

    do_print--;
}
Esempio n. 8
0
void    osInit( int argc, char *argv[]  ) {
    void                *next_context;
    INT32               i;

    if( argc == 1 )
    {
        printf("Please enter a test you want to run (such as test1c).\n");
        return;
    }

    /* Demonstrates how calling arguments are passed thru to here       */

    printf( "Program called with %d arguments:", argc );
    for ( i = 0; i < argc; i++ )
        printf( " %s", argv[i] );
    printf( "\n" );
    printf( "Calling with argument 'sample' executes the sample program.\n" );

    /*          Setup so handlers will come to code in base.c           */

    TO_VECTOR[TO_VECTOR_INT_HANDLER_ADDR]   = (void *)interrupt_handler;
    TO_VECTOR[TO_VECTOR_FAULT_HANDLER_ADDR] = (void *)fault_handler;
    TO_VECTOR[TO_VECTOR_TRAP_HANDLER_ADDR]  = (void *)svc;

    /*  Determine if the switch was set, and if so go to demo routine.  */

    if (( argc > 1 ) && ( strcmp( argv[1], "sample" ) == 0 ) ) {
        Z502MakeContext( &next_context, (void *)sample_code, KERNEL_MODE );
        Z502SwitchContext( SWITCH_CONTEXT_KILL_MODE, &next_context );
    }                   /* This routine should never return!!           */

    // Figure out which test we need to run.
    int entry = 0;
    char* entryName = "";
    if( argc > 1 )
    {
        if( argv[1][0] == 't' &&
            argv[1][1] == 'e' &&
            argv[1][2] == 's' &&
            argv[1][3] == 't' )
        {
            if( argv[1][4] == '1' || argv[1][4] == '2' )
            {
                entry = argv[1][5] - 'a';
                entryName = &argv[1][0];
                if( entry > 11 )
                {
                    entry = 11;
                }

                if( argv[1][4] == '2' )
                {
                    entry += 12;
                }
            }
        }
    }

    // Initialize global managers.
    ProcessManagerInitialize();
    SchedulerInitialize();
    MemoryManagerInitialize();
    DiskManagerInitialize();

    // Create main user process.
    PCB* pcb = gProcessManager->CreateProcess(entryName, 1, tests[entry], 20, 0, 0);
    gScheduler->Dispatch();
}                                               // End of osInit