A road map through Nachos -- user-level processes

Source: Internet
Author: User

User-level processes
User-level process
Nachos runs user programs in their own private address space.
Nachos runs the user program in its private address space.
Nachos can run any MIPS binary, assuming that it restricts itself to only making system cballs that nachos understands.
Nachos can run any MIPS binary file and assume that the user program only uses system calls that nachos can understand.
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.
In UNIX, the "A. Out" file is stored in the "coff" format. Nachos needs to run in "noff" format. Use the coff2noff program to convert "coff" format to "noff" format.
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.

The noff format file consists of four parts. The first part is the noff header used to describe the content of other parts of the file. Including running commands, initialization or uninitialized variables.

The noff header resides at the very start of the file and contains pointers to the remaining sections. Specifically, the noff header contains:

The noff header is at the beginning of the file and contains a pointer to the remaining section. Specifically, the noff header also includes:
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.
A reserved magic number indicates that the file is in noff format. The magic number is stored in the first four bytes of the file.
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.

Before trying to run a user program, nachos checks the magic number to ensure that the file is executable by nachos.

For each of the remaining sections, nachos maintains the following information:

In each of the remaining sections, nachos manages the following information:
Virtualaddr:
Virtual Address:
What virtual address that segment begins at (normally zero ).
The starting virtual address of each segment (usually starting from scratch ).
Infileaddr:
File address:
Pointer within the noff file where that section actually begins (so that nachos can read it into memory before execution begins ).

Point to the real start address in the middle part of the file (so that nachos can read it into the memory before execution ).
Size:
Size:
The size (in bytes) of that segment.
The segment size in bytes.

When executing a program, nachos creates an address space and copies the contents of the instruction and initialized variable segments into the address space.

When a program is started, nachos creates an address space and copies the commands of the running file and initialized variable segments to the address space.
Note that the uninitialized Variable Section does not need to be read from the file.

Note: uninitialized variable segments do 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.

Because they all contain zero, nachos simply allocates memory for it in the address space and initializes it to zero.

Process Creation
Create Process
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.
The nachos process creates an address space, allocates physical memory for it, loads executable commands to the physical memory, initializes registers and address conversion tables, and then calls machine: Run () to start execution.
Run () simply '''turns on ''' the simulated MIPS machine, having it enter an infinite loop that executes instructions one at a time ).

Run () is simply to open the simulation MIPS machine, and when it enters the infinite loop, it starts to execute program commands.

Stock nachos assumes that only a single user program exists at a given time.
Nachos assumes that only one user program exists at a given time.
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 ).
Therefore, after the address space is created, nachos assumes that no other resources are using the physical memory and initialized to zero.
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 ).
Then nachos reads the binary executable file to the physical memory starting from mainmemory and initializes the conversion table to map the virtual address to the physical address one to one (for example: any virtual address N is mapped to the physical address 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 ).

The initialization register includes setting all registers to zero and setting the values of pcreg and nextpcreg to zero and 4, set the maximum virtual address (Stack is extended from high address to low address) that the stack pointer points to the process ).
Nachos assumes that execution of user-programs begins at the first instruction in the Text Segment (e.g., virtual address 0 ).

Nachos assumes that the user program runs from the first command in the Text Segment (the virtual address is zero ).

When support for multiple user processes has been added, two other nachos routines are necessary for process switching.

When multi-user processes are supported, process switching is required for two nachos routines.
Whenever the current processes is suincluded (e.g ., preempted or put to sleep), The schedinvoinvokes the routine addrspace: saveuserstate (), in order to properly save address-space related state that the low-level thread switching routines do not know about.

When the current process is paused (preemptible or sleep), The scheduler calls the addrspace: saveuserstate () routine to properly store the address space related to the underlying thread status.
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.
This step is required when virtual memory is used. When switching from one process to another, a new set of address translation tables need to be loaded.
The nachos scheduler CILS saveuserstate () whenever it is about to preempt one thread and switch to another.
The nachos scheduler will call saveuserstate () to seize one thread and switch to another ().
Likewise, before switching to a new thread, the nachos scheduler invokes addrspace: restoreuserstate.

Similarly, before switching to a new thread, the nachos scheduler calls addrspace: restoreuserstate.
Restoreuserstate () insures that the proper address translation tables are loaded before execution resumes.

Restoreuserstate () ensures that the appropriate address translation table is loaded before the restoration.

Creating a noff binary
Create a noff binary file
Nachos is capable of executing a program containing arbitrary MIPS instructions.

Nachos can run any program containing MIPS commands.
For example, C Programs in the test directory are compiled using GCC on a MIPS machine to create ''. o' files.
For example, the C program in the test directory uses the GCC of the MIPs machine to compile it into a ". O" file.
To create an A. Out binary file, the loader prepends the instructions in test/start. s before the code of the user program.
To create a A. Out binary file, the loader adds the commands in test/start. s to the user program code.
File start. S contains initialization code that needs to be executed before the user's main program.

The START. s file contains the initialization code that needs to be run before the user's main program.
Specifically, the very first instruction in start. s callthe 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.
In particular. s's first command calls the main routine provided by the user. However, the second command is used to call the nachos system to call exit to ensure that the user's process is also aborted when the user main returns.
In addition, start. S contains stub modules for invoking system CILS (described below ).

In addition, start. s also includes the pile module that calls the system call.

System cballs and Exception Handling
System Call and Exception Handling
User Programs invoke system cballs by executing the MIPs ''syscall'' instruction, which generates a hardware trap into the nachos kernel.
The user program calls the system by executing the "syscall" command of MIPS. This command generates a hardware trap into the nachos kernel.
The nachos/MIPS simulator implements traps by invoking the routine raiseexception (), passing it a arguments indicating the exact cause of the trap.
The nachos/MIPS simulator calls the raiseexception () routine to declare a self-traps. The input parameters are used to indicate the cause of self-traps during the call.
Raiseexception, in turn, callexceptionhandler to take care of the specific problem.

Raiseexception transfers the call to exceptionhandler to solve the specific problem.
Exceptionhandler is passed a single argument indicating the precise cause of the trap.
When the exceptionhandler is called, input parameters are used to indicate the specific cause of Self-trap.

The ''syscall'' Instruction indicates a system call is requested, but doesn' t indicate which system call to perform.
The "syscall" command indicates a system call and does not indicate the specific system call.
By convention, user programs place the code indicating the participating system call desired in register R2 before executing the ''syscall'' instruction.
For convenience, your program will place the code that instructs the specific system to call in the R2 register before the "syscall" command.
Additional arguments to the System Call (when appropriate) can be found in registers r4-r7, following the standard C procedure call linkage conventions.

If a system call requires a parameter, the parameter is placed in the lower r4-r7 register and placed according to the standard C call mechanism.
Function (and System Call) return values are expected to be in register R2 on return.

The return values of functions (and system calls) are placed in the R2 register.

Warning: when accessing user memory from within the exception handler (or within nachos in general), user-level addresses cannot be referenced directly.

Note: When an exception handling (or nachos code) accesses the user memory, user-level addresses cannot be directly referenced.
Recall that user-level processes execute in their own private address spaces, which the kernel cannot reference directly.
Because user-level processes are executed in their own private address space, the kernel cannot be directly referenced.
Attempts to dereference pointers passed as arguments to system CILS will likely lead to problems (e.g., segmentation faults) If referenced directly.
An attempt to pass the pointer directly referenced as a parameter to the system call and unreference it may cause problems (such as segment errors ).
Use readmem and writemem to dereference pointers passed as arguments to system CILS.

Use readmem and writemem to dereference the pointer passed to the system call.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.