November 6, 2011

Practical Part - NachOS 2

PRACTICAL PART - NACHOS 2

To test our programs of paging/swaping algorithms, page table and TLB, we need to make user programs.
To run user programs we need system calls (syscalls are the interface between the OS and user programs), so here is the work that we made to run user programs.

Step 1. Code Assignment

For the system to be able to identify each call is assigned an integer to each.
The statement of system calls is on file: userprog/syscall.h


We add these lines:
#define SC_Cuadrado   22;   /*Obviously the number of syscalls can't repeat*/ 
#define SC_Duplicar 23;

2. Declaration of prototype


To perform the compilation of the user program we must to have the prototype of function to call in the user space.This statement of name, parameters and return of functions is on the file :
userprog/syscall.h

We include the statement of "Cuadrado" and "Duplicar" at the end of file, before the logical end(#endif).

We add these lines:

/* Retorna el cuadrado del valor entregado */
Int Cuadrado(int x);
/* Retorna el cuadrado del valor entregado */
int Duplicar(char* origen, char* destino);


Step 3. Implementation of the trap

In nachOS the trap of the kernel (the passing of control from the user program to the system) must be done in machine language for the MIPS processor simulated.

The file to edit is : test/start.s

We add these lines:

.globl Cuadrado
.ent
Cuadrado
Cuadrado:
addiu $2,$0,SC_Cuadrado
syscall
j
$31
.end Cuadrado
.globl Duplicar
.ent
Duplicar
Duplicar:
addiu $2,$0,SC_Duplicar
syscall
j
$31
.end Duplicar

//Sorry syntax highlighter doesn't work with assembler


Here is an image of the code, cause the spaces in the code are important, so try to copy it exactly like in the image, the large spaces are TAB





In the past lines we declare the implementation in assembler of funtions "Cuadrado" and "Duplicar". This implementation copy to register #2 code of syscall that is calling , then performed over control from the user to the system program to return to the instruction that is next in user program.

(In the MIPS processors the code calls the system is always stored in the register #2 for kernel space recovery)

Step 4. Receiving the syscall in kernel space

NachOS provides a single point of entry to the kernel form user programs. This file is in : userprog/exception.cc in ExceptionHandler function.

This handles syscalls, pagefaults, failures invalid address and instructions.

For the syscall "Cuadrado" and "Duplicar" should add the following code within the switch / case of syscalls:

/* I think that you dont have the switch(which),
i have nachOS 3.4 and this switch doesnt exist so
i implemented it.
*/

void ExceptionHandler(ExceptionType which)
{
int type = machine->ReadRegister(2);
switch(which){
case SyscallException:
switch(type){

case SC_Halt:
DEBUG('a', "Shutdown, initiated by user program.\n");
interrupt->Halt();
break;
case SC_Exec:
vaddr = machine->ReadRegister(4);
DEBUG(dbgSysCall,"System Call: Exec vaddr=" << vaddr);
returnval = ExecHandler(vaddr);
break;

case SC_Cuadrado:
vaddr = machine->ReadRegister(4);
DEBUG(dbgSysCall,"System Call: Cuadrado valor=" << vaddr);
returnval = CuadradoHandler(vaddr);
break;
case SC_Duplicar:
vaddrOrigen = machine->ReadRegister(4);
vaddrDestino = machine->ReadRegister(5);
DEBUG(dbgSysCall,"System Call: Duplicar vaddrOrigen=" <<
vaddrOrigen << " vaddrDestino=" << vaddrDestino );
returnval1 = DuplicarHandler(vaddrOrigen, vaddrDestino);
break;
default:
printf("Unexpected user mode exception %d %d\n", which, type);
ASSERT(false);
}//Fin de 2do switch
}//Fin de 1er switch

}//Fin de ExceptionHandler

In the case SC_Duplicar virtual addresses are recovered where the 2 Strings from the registry #4 and #5 respectively and the handler is invoked.

Step 5. Call Handling implementation

On receipt of the syscall in the past step we make the function calls "CuadradoHandler" and "DuplicarHandler" . These are the functions that actually implement the actions you must perform each syscall.

To CuadradoHandler implementation is:

// Retorna el cuadrado del valor entregado
//--------------------------------------------------------------------
int CuadradoHandler(int numero)
{
return (numero*numero);
}


To DuplicarHandler implementation is:

//DuplicarHandler implementation

int DuplicarHandler(int vOrigen, int vDest)
{
char* origen = new char[MaxStringArgLength];
char* destino = new char[MaxStringArgLength];
int len = 0;
// se limpia el buffer origen y destino
bzero(destino, MaxStringArgLength);
bzero(origen, MaxStringArgLength);
// recuperacion del string desde espacio de usuario a espacio de kernel
if ((len = kernel->currentThread->space->UserStringToKernel(vOrigen,origen)) < 0) {
return -1;
}
// copiamos la primera vez el string
strcpy(destino, origen);
// segunda vez
strcpy(destino+len-1, origen);
// luego debemos pasar el string resultado al espacio de usuario
if (kernel->currentThread->space->KernelToUserBuf(vDest, MaxStringArgLength,
destino)) {
return -1;
}
return MaxStringArgLength;
}

In the tutorial that we followed don't tell us where put these functions("CuadradoHandler and DuplicarHandler"), but we assume that these functions must to be in userprog/exception.cc cause right there calls system are handled :)

Step 6. Inclusion in the user program

The inclusion of syscalls in a user program has 2 parts. The first is the programming, and the second is the modification of test directory makefile to include the headers syscalls.

A basic user program to call both syscalls and print in screen the results is:

#include "syscall.h"
#include "io_lib.h"
int main() {
char destino[256];
printInt(Cuadrado(10));
Duplicar("hola", destino);
print(destino);
Halt();
}


Finally we must to edit the file /test/Makefile to add our program.

Here is a image to guide how you can do it:



This file called Makefile indicates which files must be compiled at moment to do "make".

Now just "make" at .../nachos/code/ and if there aren't errors you can do "./nachos" in /nachos/code/test

We had problems with the function ExceptionHandler so we had 2 errors, here is an image:





Well if you don't have problems the output of execution must be like this:


"(This image is from the tutorial that we followed)"




That's all, all your questions write it in comments please :).

1 comment:

  1. The extras here are what I wanted you all to do as extras during the exam weeks ;) +2 points for this team for including it now.
    Also, another +2 for the LRU/FIFO implementations (rather inefficient, though)
    +1 for slides en English.
    Another +1 for the NachOS memory mapping effort.
    This yields 6 points for N2P for the team.

    ReplyDelete