nachos实验一代码_第1页
nachos实验一代码_第2页
nachos实验一代码_第3页
nachos实验一代码_第4页
nachos实验一代码_第5页
已阅读5页,还剩18页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Test文件下: shell.c: #include syscall.hintmain() SpaceId newProc; OpenFileId input = ConsoleInput; OpenFileId output = ConsoleOutput; char ch, buffer60; int i;Write( ttn, 34, output); while( 1 ) Write( :, 20, output);i = 0;do Read(&bufferi, 1, input); while( bufferi+ != n );buffer-i = 0; if(Strncmp(buf

2、fer,exit,4)=0) Halt(); if( i 0 ) newProc = Exec(buffer);Join(newProc); Start.s :/* Start.s *Assembly language assist for user programs running on top of Nachos. * *Since we dont want to pull in the entire C library, we define *what we need for a user program here, namely Start and the system *calls.

3、 */#define IN_ASM#include syscall.h .text .align 2/* - * _start *Initialize running a C program, by calling main. * * NOTE: This has to be first, so that it gets loaded at location 0. *The Nachos kernel always starts a program by jumping to location 0. * - */.globl _start.ent_start_start:jalmainmove

4、$4,$0jalExit /* if we return from main, exit(0) */.end _start/* - * System call stubs: *Assembly language assist to make system calls to the Nachos kernel. *There is one stub per system call, that places the code for the *system call into register r2, and leaves the arguments to the *system call alo

5、ne (in other words, arg1 is in r4, arg2 is *in r5, arg3 is in r6, arg4 is in r7) * * The return value is in r2. This follows the standard C calling * convention on the MIPS. * - */.globl Halt.entHaltHalt:addiu $2,$0,SC_Haltsyscallj$31.end Halt.globl Add.entAddAdd:addiu $2,$0,SC_Addsyscallj $31.end A

6、dd.globl Exit.entExitExit:addiu $2,$0,SC_Exitsyscallj$31.end Exit.globl Exec.entExecExec:addiu $2,$0,SC_Execsyscallj$31.end Exec.globl ExecV.entExecVExecV:addiu $2,$0,SC_ExecVsyscallj$31.end ExecV.globl Join.entJoinJoin:addiu $2,$0,SC_Joinsyscallj$31.end Join.globl Create.entCreateCreate:addiu $2,$0

7、,SC_Createsyscallj$31.end Create.globl Remove.entRemoveRemove:addiu $2,$0,SC_Removesyscallj$31.end Remove.globl Open.entOpenOpen:addiu $2,$0,SC_Opensyscallj$31.end Open.globl Read.entReadRead:addiu $2,$0,SC_Readsyscallj$31.end Read.globl Write.entWriteWrite:addiu $2,$0,SC_Writesyscallj$31.end Write.

8、globl Close.entCloseClose:addiu $2,$0,SC_Closesyscallj$31.end Close.globl Seek.entSeekSeek:addiu $2,$0,SC_Seeksyscallj$31.end Seek.globl Delete.entDeleteDelete: addiu $2,$0,SC_Deletesyscallj$31.end Delete .globl ThreadFork .ent ThreadForkThreadFork: addiu $2,$0,SC_ThreadFork syscall j $31 .end Threa

9、dFork .globl ThreadYield .ent ThreadYieldThreadYield: addiu $2,$0,SC_ThreadYield syscall j $31 .end ThreadYield.globl ThreadExit.ent ThreadExitThreadExit:addiu $2, $0, SC_ThreadExitsyscallj $31.end ThreadExit.globl ThreadJoin.ent ThreadJoinThreadJoin:addiu $2, $0, SC_ThreadJoinsyscallj $31.end Threa

10、dJoin.globl getSpaceID.entgetSpaceIDgetSpaceID:addiu $2,$0,SC_getSpaceIDsyscallj$31.end getSpaceID.globl getThreadID.entgetThreadIDgetThreadID:addiu $2,$0,SC_getThreadIDsyscallj$31.end getThreadID.globl Ipc.ent IpcIpc: addiu $2,$0,SC_Ipcsyscallj $31.end Ipc.globl Clock.ent ClockClock:addiu $2,$0,SC_

11、Clocksyscallj $31.end Clock/* dummy function to keep gcc happy */ .globl _main .ent _main_main: j $31 .end _main.globl Strncmp.ent StrncmpStrncmp: addiu $2,$0,SC_Strncmp syscall j $31 .end Strncmp .globl Exit .ent ExitUserprog文件夹下Exception.cc: / exception.cc /Entry point into the Nachos kernel from

12、user programs./There are two kinds of things that can cause control to/transfer back to here from user code:/syscall - The user code explicitly requests to call a procedure/in the Nachos kernel. Right now, the only function we support is/Halt./exceptions - The user code does something that the CPU c

13、ant handle./For instance, accessing memory that doesnt exist, arithmetic errors,/etc. /Interrupts (which can also cause control to transfer from user/code into the Nachos kernel) are handled elsewhere./ For now, this only handles the Halt() system call./ Everything else core dumps./ Copyright (c) 19

14、92-1996 The Regents of the University of California./ All rights reserved. See copyright.h for copyright notice and limitation / of liability and disclaimer of warranty provisions.#include copyright.h#include main.h#include syscall.h#include ksyscall.h/-/ ExceptionHandler/ Entry point into the Nacho

15、s kernel. Called when a user program/is executing, and either does a syscall, or generates an addressing/or arithmetic exception./ For system calls, the following is the calling convention:/ system call code - r2/arg1 - r4/arg2 - r5/arg3 - r6/arg4 - r7/The result of the system call, if any, must be

16、put back into r2. / If you are handling a system call, dont forget to increment the pc/ before returning. (Or else youll loop making the same system call forever!)/which is the kind of exception. The list of possible exceptions /is in machine.h./-voidExceptionHandler(ExceptionType which) int type =

17、kernel-machine-ReadRegister(2); DEBUG(dbgSys, Received Exception which type: type n); switch (which) case SyscallException: switch(type) case SC_Halt:DEBUG(dbgSys, Shutdown, initiated by user program.n);SysHalt();ASSERTNOTREACHED();break;case SC_Write:DEBUG(dbgSys,Write from buffer to consoleOutputm

18、achine-ReadRegister(4)machine-ReadRegister(5)machine-ReadRegister(6)machine-ReadRegister(5);OpenFileId printOut;printOut= (OpenFileId)kernel-machine-ReadRegister(6);int addressOne;addressOne=(int)kernel-machine-ReadRegister(4);int i;i=0;dokernel-machine-ReadMem(addressOne+,1,(int*)&wbufferi+); while

19、(imachine-WriteRegister(2,n1);kernel-machine-WriteRegister(PrevPCReg,kernel-machine-ReadRegister(PCReg);kernel-machine-WriteRegister(PCReg, kernel-machine-ReadRegister(PCReg) +4);kernel-machine-WriteRegister(NextPCReg,kernel-machine-ReadRegister(PCReg)+4); return;ASSERTNOTREACHED();break;case SC_Rea

20、d:DEBUG(dbgSys, read from buffer machine-ReadRegister(4) machine-ReadRegister(5)machine-ReadRegister(6) machine-ReadRegister(5);OpenFileId enterIn;enterIn = (OpenFileId)kernel-machine-ReadRegister(6);char addressTwo;addressTwo = (char)kernel-machine-ReadRegister(4);int local ;local= (int)kernel-mach

21、ine-ReadRegister(4);int n2 ;n2= SysRead(&addressTwo,rsize,enterIn);kernel-machine-WriteMem(local,1,(int)addressTwo);kernel-machine-WriteRegister(2,n2); kernel-machine-WriteRegister(PrevPCReg,kernel-machine-ReadRegister(PCReg);kernel-machine-WriteRegister(PCReg, kernel-machine-ReadRegister(PCReg) +4)

22、;kernel-machine-WriteRegister(NextPCReg,kernel-machine-ReadRegister(PCReg)+4);return;ASSERTNOTREACHED();break;case SC_Exec:DEBUG(dbgSys,execute a commandmachine-ReadRegister(4)machine-ReadRegister(4);int c;c = 0;dokernel-machine-ReadMem(addressThree+,1,(int*)&ebufferc+); while(cmachine-WriteRegister

23、(2,SysExec(ebuffer);kernel-machine-WriteRegister(PrevPCReg,kernel-machine-ReadRegister(PCReg);kernel-machine-WriteRegister(PCReg, kernel-machine-ReadRegister(PCReg) +4);kernel-machine-WriteRegister(NextPCReg,kernel-machine-ReadRegister(PCReg)+4);return;ASSERTNOTREACHED();break;case SC_Join:DEBUG(dbg

24、Sys,jion machine-ReadRegister(4)machine-WriteRegister(2,SysJoin(SpaceId)kernel-machine-ReadRegister(4);kernel-machine-WriteRegister(PrevPCReg,kernel-machine-ReadRegister(PCReg);kernel-machine-WriteRegister(PCReg, kernel-machine-ReadRegister(PCReg) +4);kernel-machine-WriteRegister(NextPCReg,kernel-ma

25、chine-ReadRegister(PCReg)+4);return;ASSERTNOTREACHED();break;case SC_Strncmp:DEBUG(dbgSys, compare two string machine-ReadRegister(4) machine-ReadRegister(5) machine-ReadRegister(6) machine-ReadRegister(4);int addressFive;addressFive = kernel-machine-ReadRegister(5);char str1buffer60;char str2buffer

26、60;int d,e;d= 0;e=0;dokernel-machine-ReadMem(addressFour+,1,(int*)&str1bufferd+); while(dmachine-ReadMem(addressFive+,1,(int*)&str2buffere+);while(emachine-ReadRegister(6);kernel-machine-WriteRegister(2,n3);kernel-machine-WriteRegister(PrevPCReg,kernel-machine-ReadRegister(PCReg);kernel-machine-Writ

27、eRegister(PCReg, kernel-machine-ReadRegister(PCReg) +4);kernel-machine-WriteRegister(NextPCReg,kernel-machine-ReadRegister(PCReg)+4);return;ASSERTNOTREACHED();break; case SC_Add:DEBUG(dbgSys, Add machine-ReadRegister(4) + machine-ReadRegister(5) machine-ReadRegister(4),/* int op2 */(int)kernel-machi

28、ne-ReadRegister(5);DEBUG(dbgSys, Add returning with result machine-WriteRegister(2, (int)result);/* Modify return point */ /* set previous programm counter (debugging only)*/ kernel-machine-WriteRegister(PrevPCReg, kernel-machine-ReadRegister(PCReg); /* set programm counter to next instruction (all

29、Instructions are 4 byte wide)*/ kernel-machine-WriteRegister(PCReg, kernel-machine-ReadRegister(PCReg) + 4); /* set next programm counter for brach execution */ kernel-machine-WriteRegister(NextPCReg, kernel-machine-ReadRegister(PCReg)+4);return;ASSERTNOTREACHED();break; default:cerr Unexpected syst

30、em call type n;break; break; default: cerr Unexpected user mode exception (int)which n; break; ASSERTNOTREACHED();Ksyscall.h : /* * * userprog/ksyscall.h * * Kernel interface for systemcalls * * by Marcus Voelp (c) Universitaet Karlsruhe * */#ifndef _USERPROG_KSYSCALL_H_ #define _USERPROG_KSYSCALL_H

31、_ #include kernel.h#include #include #include #include #include #include #include #include #include #include #include #include #include #define SHELL /bin/sh int SysWrite(char*buffer ,int size,OpenFileId id) return write(id,buffer,(size_t)size);int SysRead(char*buffer ,int size,OpenFileId id) return

32、 read(id, buffer, (size_t)size);SpaceId SysExec(char* exec_name)pid_t child;child = vfork();if(child = 0)execl (SHELL, SHELL, -c, exec_name, NULL); _exit (EXIT_FAILURE);else if(child interrupt-Halt();int SysAdd(int op1, int op2) return op1 + op2;#endif /* ! _USERPROG_KSYSCALL_H_ */Syscall.h: /* sysc

33、alls.h * Nachos system call interface. These are Nachos kernel operations * that can be invoked from user programs, by trapping to the kernel *via the syscall instruction. * *This file is included by user programs and by the Nachos kernel. * * Copyright (c) 1992-1993 The Regents of the University of

34、 California. * All rights reserved. See copyright.h for copyright notice and limitation * of liability and disclaimer of warranty provisions. */#ifndef SYSCALLS_H#define SYSCALLS_H#include copyright.h#include errno.h/* system call codes - used by the stubs to tell the kernel which system call * is b

35、eing asked for */#define SC_Halt0#define SC_Exit1#define SC_Exec2#define SC_Join3#define SC_Create4#define SC_Remove 5#define SC_Open6#define SC_Read7#define SC_Write8#define SC_Seek 9#define SC_Close10#define SC_Delete 11#define SC_ThreadFork12#define SC_ThreadYield13#define SC_ExecV14#define SC_Th

36、readExit 15#define SC_ThreadJoin 16#define SC_getSpaceID 17#define SC_getThreadID 18#define SC_Ipc 19#define SC_Clock 20#define SC_Add42#define SC_Strncmp 43#ifndef IN_ASM/* The system call interface. These are the operations the Nachos * kernel needs to support, to be able to run user programs. * *

37、 Each of these is invoked by a user program by simply calling the * procedure; an assembly language stub stuffs the system call code * into a register, and traps to the kernel. The kernel procedures * are then invoked in the Nachos kernel, after appropriate error checking, * from the system call ent

38、ry point in exception.cc. */* Stop Nachos, and print out performance stats */void Halt();int Strncmp(char *buffer,char*str,int size); /* * Add the two operants and return the result */ int Add(int op1, int op2);/* Address space control operations: Exit, Exec, Execv, and Join */* This user program is

39、 done (status = 0 means exited normally). */void Exit(int status);/* A unique identifier for an executing user program (address space) */typedef int SpaceId;/* A unique identifier for a thread within a task */typedef int ThreadId;/* Run the specified executable, with no args */* This can be implemen

40、ted as a call to ExecV. */ SpaceId Exec(char* exec_name);/* Run the executable, stored in the Nachos file argv0, with * parameters stored in argv1.argc-1 and return the * address space identifier */SpaceId ExecV(int argc, char* argv); /* Only return once the user program id has finished. * Return th

41、e exit status. */int Join(SpaceId id); /* File system operations: Create, Remove, Open, Read, Write, Close * These functions are patterned after UNIX - files represent * both files *and* hardware I/O devices. * * Note that the Nachos file system has a stub implementation, which * can be used to supp

42、ort these system calls if the regular Nachos * file system has not been implemented. */ /* A unique identifier for an open Nachos file. */typedef int OpenFileId;/* when an address space starts up, it has two open files, representing * keyboard input and display output (in UNIX terms, stdin and stdou

43、t). * Read and Write can be used directly on these, without first opening * the console device. */#define ConsoleInput0 #define ConsoleOutput1 /* Create a Nachos file, with name name */* Note: Create does not open the file. */* Return 1 on success, negative error code on failure */int Create(char *n

44、ame);/* Remove a Nachos file, with name name */int Remove(char *name);/* Open the Nachos file name, and return an OpenFileId that can * be used to read and write to the file. mode gives the requested * operation mode for this file. */#define RO 1#define RW 2#define APPEND 3OpenFileId Open(char *name

45、, int mode);/* Write size bytes from buffer to the open file. * Return the number of bytes actually read on success. * On failure, a negative error code is returned. */int Write(char *buffer, int size, OpenFileId id);/* Read size bytes from the open file into buffer. * Return the number of bytes actually read - if the open file isnt * long enough, or if it is an I/O device, and there arent enough * characters to read, return whatever is available (for I/O devices, * you should always wait until you can return at least one c

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论