A Road Map Through Nachos——User-Level Processes

User-Level Processes
用户级进程
    Nachos runs user programs in their own private address space.
    Nachos运行用户程序在其私有的地址空间。
    Nachos can run any MIPS binary, assuming that it restricts itself to only making system calls that Nachos understands.
    Nachos可以运行任何MIPS二进制文件并假设用户程序只使用了Nachos可以理解的系统调用。
    In Unix, ``a.out'' files are stored in ``coff'' format. Nachos requires that executables be in the simpler ``Noff'' format.To convert binaries of one format to the other, use the coff2noff program.
    在Unix中,“a.out”文件以“coff”格式进行存储。Nachos需要以“Noff”格式进行运行。使用coff2noff程序将“coff”格式转化为“Noff”格式。
    Noff-format files consist of four parts. The first part, the Noff header, describes the contents of the rest of the file, giving information about the program's instructions, initialized variables and uninitialized variables.
    Noff格式文件包括四个部分。第一个部分为Noff头用于描述文件其它部分的内容。包括程序运行指令,初始化或未初始化的变量。
    
    The Noff header resides at the very start of the file and contains pointers to the remaining sections. Specifically, the Noff header contains:
    Noff头在文件的开始处并包括指向剩余节的指针。特别的,noff头还包括:
        noffMagic:
            A reserved ``magic'' number that indicates that the file is in Noff format. The magic number is stored in the first four bytes of the file.
            一个保留的魔数用于指示该文件是Noff格式。该魔数存储在文件的前四个字节。
            Before attempting to execute a user-program, Nachos checks the magic number to be sure that the file about to be executed is actually a Nachos executable.
            在尝试运行一个用户程序之前,Nachos检查魔数以确保该文件是Nachos可以执行的。
            
    For each of the remaining sections, Nachos maintains the following information:
    在剩余的每个节中,Nachos管理着以下信息:
        virtualAddr:
        虚拟地址:
            What virtual address that segment begins at (normally zero).
            每段开始的虚拟地址(通常从零开始)。
        inFileAddr:
        文件中地址:
            Pointer within the Noff file where that section actually begins (so that Nachos can read it into memory before execution begins).
            指向文件中段真正的开始地址(这样可以在执行之前Nachos将其读入到内存中)。
        size:
        大小:
            The size (in bytes) of that segment.
            在字节为单位的段大小。
    
    When executing a program, Nachos creates an address space and copies the contents of the instruction and initialized variable segments into the address space.
    当开始执行一个程序,Nachos创建一个地址空间并拷贝运行文件的指令及初始化的变量段到地址空间。
    Note that the uninitialized variable section does not need to be read from the file.
    注:未初始化的变量段并不需要从文件中读取。
    Since it is defined to contain all zeros, Nachos simply allocates memory for it within the address space of the Nachos process and zeros it out.
    因为其都是包含零,Nachos只是简单的在地址空间中为其分配内存并将其初始化为零。
    
Process Creation
创建进程
    Nachos processes are formed by creating an address space, allocating physical memory for the address space, loading the contents of the executable into physical memory, initializing registers and address translation tables, and then invoking machine::Run() to start execution.
    Nachos进程创建地址空间,为其分配物理内存,加载可执行指令到物理内存,初始化寄存器和地址转换表,然后调用machine::Run()开始执行。
    Run() simply ``turns on'' the simulated MIPS machine, having it enter an infinite loop that executes instructions one at a time).
    Run()只是简单的打开模拟的MIPS机器,当其进入到无限循环时开始执行程序指令。
    
    Stock Nachos assumes that only a single user program exists at a given time.
    Nachos假设在一个给定的时间只有一个用户程序存在。
    Thus, when an address space is created, Nachos assumes that no one else is using physical memory and simply zeros out all of physical memory (e.g., the mainMemory character array).
    因此,当地址空间创建后,Nachos假设没有其它的资源正在使用物理内存并将其初始化为零。
    Nachos then reads the binary into physical memory starting at location mainMemory and initializes the translation tables to do a one-to-one mapping between virtual and physical addresses (e.g., so that any virtual address N maps directly into the physical address N).
    然后Nachos将二进制可执行文件读入到从mainMemory开始的物理内存并初始化转换表用于将虚拟地址与物理地址进行一对一的映射(如:任意虚拟地址N都映射到物理地址N)。
    Initialization of registers consists of zeroing them all out, setting PCReg and NextPCReg to 0 and 4 respectively, and setting the stackpointer to the largest virtual address of the process (the stack grows downward towards the heap and text).
    初始化寄存器包括将所有的寄存器都置零,设置PCReg和NextPCReg的值为零和4,设置栈指针指向进程的最大虚拟地址(栈是从高地址向低地址扩展)。
    Nachos assumes that execution of user-programs begins at the first instruction in the text segment (e.g., virtual address 0).
    Nachos假设用户程序从text段的第一条指令开始执行(虚拟地址为零)。
    
    When support for multiple user processes has been added, two other Nachos routines are necessary for process switching.
    当支持多用户进程时,两个Nachos例程将需要进行进程切换。
    Whenever the current processes is suspended (e.g., preempted or put to sleep), the scheduler invokes the routine AddrSpace::SaveUserState(), in order to properly save address-space related state that the low-level thread switching routines do not know about.
    将当前进程被暂停(抢占或让其休眠),调度器将调用AddrSpace::SaveUserState()例程,为了合理的保存与底层线程状态相关的地址空间。
    This becomes necessary when using virtual memory; when switching from one process to another, a new set of address translation tables needs to be loaded.
    当使用虚拟内存时这一步是必须的,当从一个进程切换到另一个时,一组新的地址转换表需要被加载。
    The Nachos scheduler calls SaveUserState() whenever it is about to preempt one thread and switch to another.
    Nachos调度器抢占一个线程并切换到另一个时将调用SaveUserState()。
    Likewise, before switching to a new thread, the Nachos scheduler invokes AddrSpace::RestoreUserState.
    同样的,在切换一个新线程之前,Nachos调度器调用AddrSpace::RestoreUserState。
    RestoreUserState() insures that the proper address translation tables are loaded before execution resumes.
    RestoreUserState()确保在恢复执行前合适的地址转换表被加载。
    
Creating a Noff Binary
创建Noff二进制文件
    Nachos is capable of executing a program containing arbitrary MIPS instructions.
    Nachos可运行任何包含MIPS指令的程序。
    For example, C programs in the test directory are compiled using gcc on a MIPS machine to create ``.o'' files.
    例如,在test目录中的C程序使用MIPS机器的gcc编译成“.o”文件。
    To create an a.out binary file, the loader prepends the instructions in test/start.s before the code of the user program.
    要创建一个a.out二进制文件,加载器将test/start.s中的指令加入到用户程序的代码之前。
    File start.s contains initialization code that needs to be executed before the user's main program.
    start.s文件包括需要在用户main程序之前运行的初始化代码。
    Specifically, the very first instruction in start.s calls the user-supplied main routine, whereas the second instruction invokes the Nachos Exit system call, insuring that user processes terminate properly when their main program returns.
    特别地,在start.s最前面的指令调用用户提供的main例程,然而,第二个指令用于调用Nachos系统调用Exit来保证当用户main返回时同时也中止用户进程。
    In addition, start.s contains stub modules for invoking system calls (described below).
    除此之外,start.s还包括了调用系统调用的桩模块。
    
System Calls and Exception Handling
系统调用及异常处理
    User programs invoke system calls by executing the MIPS ``syscall'' instruction, which generates a hardware trap into the Nachos kernel.
    用户程序通过执行MIPS的“syscall”指令来完成系统调用,该指令产生一个硬件自陷进入Nachos内核。
    The Nachos/MIPS simulator implements traps by invoking the Routine RaiseException(), passing it a arguments indicating the exact cause of the trap.
    Nachos/MIPS模拟器调用调用RaiseException()例程来申明一个自陷,调用时传入参数用于指示什么原因引发自陷。
    RaiseException, in turn, calls ExceptionHandler to take care of the specific problem.
    RaiseException将调用再转给ExceptionHandler来处理具体的问题。
    ExceptionHandler is passed a single argument indicating the precise cause of the trap.
    ExceptionHandler调用时传入参数用于指示引发自陷的具体原因。
    
    The ``syscall'' instruction indicates a system call is requested, but doesn't indicate which system call to perform.
    “syscall”指令指示了一个系统调用,便并不指示具体调用了何种系统调用。
    By convention, user programs place the code indicating the particular system call desired in register r2 before executing the ``syscall'' instruction.
    为了方便,用户程序将在“syscall”指令之前将指示具体系统调用的代码放置到r2寄存器中。
    Additional arguments to the system call (when appropriate) can be found in registers r4-r7, following the standard C procedure call linkage conventions.
    如果系统调用需要参数,则参数放置于下r4-r7寄存器,按标准的C调用机制进行放置。
    Function (and system call) return values are expected to be in register r2 on return.
    函数(和系统调用)的返回值将放置在r2寄存器中。
    
    Warning: When accessing user memory from within the exception handler (or within Nachos in general), user-level addresses cannot be referenced directly.
    注意:当在异常处理(或Nachos代码中)访问用户内存时,用户级的地址不能直接引用。
    Recall that user-level processes execute in their own private address spaces, which the kernel cannot reference directly.
    因为用户级进程在其自己私有的地址空间执行,内核将不能直接引用。
    Attempts to dereference pointers passed as arguments to system calls will likely lead to problems (e.g., segmentation faults) if referenced directly.
    试图将作为直接引用的指针作为参数传递给系统调用并对其解引用可能会导致问题(如,段错误)。
    Use ReadMem and WriteMem to dereference pointers passed as arguments to system calls.

    使用ReadMem和WriteMem对传递给系统调用的指针进行解引用。



你可能感兴趣的:(exception,user,System,initialization,translation,Pointers)