TTIT61: Process Programming and Operating Systems Outline

advertisement
Outline
TTIT61:
Process Programming and
Operating Systems
Alexandru Andrei
alean@ida.liu.se
phone: 282698, room: B 3D:439
ƒ Lab 2 : System Calls
ƒ Introduction to – and – making user programs in Nachos
ƒ User memory vs Kernel memory in Nachos
ƒ File & Console related syscalls
ƒ Synchronizing kernel functions and data structures
ƒ Lab 3 : Memory Management & System Calls
ƒ 3.1 Memory management with linear page tables
¾Multiprogramming - multiple user processes
¾Page tables
¾Process handling
ƒ 3.2 Memory management with software TLB
alean@ida
TTIT61 Lab lesson
-2-
System Calls
Four Components of a Computer System
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
/* System calls are written in bold italic. Type “man 2 sys_call_name” for
info in Linux or “man –s 2 sys_call_name” in Solaris */
int fd, n;
char buf[1024];
fd = open(“datafile.txt”, O_RDWR);
n = read(fd, buf, 1024);
printf(“Have read %d bytes: %s\n”, n, buf);
lseek(fd, 0, SEEK_SET);
write(fd, “Some other text”, strlen(“Some other text”) + 1);
write(fd, buf, n);
close(fd);
alean@ida
-3-
TTIT61 Lab lesson
alean@ida
-4-
TTIT61 Lab lesson
User Programs
System Call Execution
ƒ Located in the Nachos directory: code/test/
ƒ Compiled with cross-compiler to MIPS machine code
(see code/test/Makefile)
ƒ MIPS is a RISC architecture, with delayed loads
ƒ Cross compile – compile in one machine for a
different target machine
ƒ Emulation – runs on MIPS simulator
ƒ Class Machine emulates the MIPS processor
alean@ida
-5-
TTIT61 Lab lesson
alean@ida
-6-
TTIT61 Lab lesson
1
User Program - Example
User Programs (cont.)
ƒ Simple C programs (any C program that doesn’t use
library functions like printf)
ƒ In nachos-3.4/code/test
ƒ For a new test program, add it to the Makefile from
nachos-3.4/code/test
alean@ida
TTIT61 Lab lesson
-7-
Test Program Makefile
ƒIn code/testprogram/Makefile
ƒUse copy paste to add another testprogram
#include ”syscall.h”
void main(void) {
int file_id;
char a[10];
for (i=0;i<=8;i++) a[i]=’a’;
a[9]=’\0’;
Create(a);
file_id = Open(a);
Write(”some text”, 10, file_id);
Close(file_id);
Halt();
}
alean@ida
The User Space Definition of the Syscall Functions
.globl Create
.ent
addiu $2,$0,SC_Create
syscall
j
$31
.end Create
start.o: start.s ../userprog/syscall.h
$(CPP) $(CPPFLAGS) start.s > strt.s
$(AS) $(ASFLAGS) -o start.o strt.s
rm strt.s
TAB
(not spaces)
alean@ida
Create
Create:
all: halt shell matmult sort
halt.o: halt.c
$(CC) $(CFLAGS) -c halt.c
halt: halt.o start.o
$(LD) $(LDFLAGS) start.o halt.o -o halt.coff
../bin/coff2noff halt.coff halt
TTIT61 Lab lesson
-9-
TTIT61 Lab lesson
-8-
code/test/start.s
.globl Open
.ent
Open
Open:
addiu $2,$0,SC_Open
syscall
j
$31
.end Open
alean@ida
TTIT61 Lab lesson
-10-
Program Execution
Under the Hood
ƒThe MIPS processor fetches instructions from memory
and executes them one by one
void Machine::Run() { //
code/machine/mipssim.cc
Instruction *instr = new Instruction;
ƒ How does Nachos execute the binary code
ƒ How does Nachos execute system calls
interrupt->setStatus(UserMode);
for (;;) {
OneInstruction(instr);
interrupt->OneTick();
}
}
alean@ida
-11-
TTIT61 Lab lesson
alean@ida
-12-
TTIT61 Lab lesson
2
OneInstruction() (cont.)
OneInstruction()
void Machine::OneInstruction(Instruction *instr){
int raw;
int nextLoadReg = 0;
int nextLoadValue = 0;
if (!machine->ReadMem(registers[PCReg],4,&raw))
return;
// exception occurred
instr->value = raw;
instr->Decode();
pcAfter = registers[NextPCReg] + 4;
switch (instr->opCode) { Ænextslide
alean@ida
switch
case
case
case
TTIT61 Lab lesson
-13-
(instr->opCode) {
OP_ADD: …
OP_DIV: …
OP_SYSCALL:
RaiseException(SyscallException, 0);
return;
alean@ida
-14-
TTIT61 Lab lesson
Exception Handling: System Calls
RaiseException()
The processor has detected an exception and passes the
control to the operating system for exception handling
void
Machine::RaiseException(//code/machine/machine.cc
ExceptionType which, int badVAddr){
// code/userprog/exception.cc
void ExceptionHandler(ExceptionType which) {
int type = machine->ReadRegister(2);
if ((which == SyscallException)&&(type == SC_Halt)){
DEBUG('m', "Exception: %s\n", exceptionNames[which]);
registers[BadVAddrReg] = badVAddr;
DelayedLoad(0, 0);
// finish anything in progress
DEBUG('a', "Shutdown, initiated by user program.\n");
interrupt->Halt();
}
else
interrupt->setStatus(SystemMode);
ExceptionHandler(which);
//interrupts are enabled at this point
interrupt->setStatus(UserMode);
printf("Unexpected user mode exception %d %d\n",which,type);
}
}
alean@ida
-15-
TTIT61 Lab lesson
alean@ida
Exception Handling: System Calls
void ExceptionHandler(ExceptionType which) {
int type = machine->ReadRegister(2);
if ((which == SyscallException)) {
switch (type) {
case SC_Halt: {
{
break;
break;
break;
-17-
TTIT61 Lab lesson
Exception Handling: System Calls
ƒ The list of the system call ids (the value from register 2)
is in the file code/userprog/syscall.h
DEBUG('a', "Shutdown, initiated by user program.\n");
interrupt->Halt();
}
case SC_Create:
//your code;
}
case SC_Open: {
//your code;
}
case SC_Read: {
//your code;
}
….
} //end of switch
}//end if
}
alean@ida
-16-
TTIT61 Lab lesson
ƒ For example:
#define SC_Halt
#define SC_Exit
#define SC_Exec
#define SC_Join
#define SC_Create
#define SC_Open
#define SC_Read
#define SC_Write
#define SC_Length
#define SC_Close
alean@ida
0
1
2
3
4
5
6
7
8
9
-18-
TTIT61 Lab lesson
3
Exception Handling: System Calls
ƒ ”syscall” machine code instruction
ƒ The system call id is stored in register 2
ƒ The arguments of the syscall are stored in the registers,
starting with register 4
ƒ arg1 -- r4, arg2 -- r5, etc.
ƒ System calls return values in reg. r2
ƒ see …/code/test/start.s
ƒ ExceptionHandler
ƒ see …/code/userprog/exception.cc
¾SC_Halt already implemented
ƒ The list of the system calls is in the file
code/userprog/syscall.h
void Write(char *buffer, int size, OpenFileId id);
int Read(char *buffer, int size, OpenFileId id);
void Close(int fileid);
OpenFileId Open(char *filename);
void Exit(int status);
SpaceId Exec(char *name);
int Join(SpaceId id);
void Halt();
alean@ida
-19-
System Calls and Exception Handling
TTIT61 Lab lesson
alean@ida
Exception Handling: System Calls
-20-
TTIT61 Lab lesson
Two Types of Arguments
void ExceptionHandler(ExceptionType which) {
…
switch (type) {
case SC_Create:
//read from register 4 the argument
//use machine->ReadRegister(4);
//create the file
//does not return any value
//if it would return, it should write
//the return value in register 2
break;
}
…
alean@ida
-21-
TTIT61 Lab lesson
ƒ Arguments passed by value
¾void Exit(int status);
¾case SC_EXIT:
int status=machine->ReadRegister(4);
ƒ Arguments passed by address (pointers)
¾void Create(char *filename);
¾case SC_EXIT:
int address_of_pointer=machine->ReadRegister(4);
alean@ida
-22-
User Programs&User Memory
Process1:
char buffer[10]
Create(buffer);
Process2:
char buffer[10]
Open(buffer);
0000
0004
0008 0000
FFFF
Kernel
Process 1
AAA0 0000
AAA4
FFFF
Process 2
FFFF
Memory
TTIT61 Lab lesson
Important !
ƒ User space vs. Kernel Space
ƒ User address vs. Kernel Address
ƒ Pointer arguments passed to system calls must be
translated to/from user space
ƒ Example:
ƒ Create(char* filename) -> UserToKernel
ƒ Read(char* buf, int size, OpenFileId id) -> KernelToUser
Virtual memory vs. Phisycal memory
alean@ida
-23-
TTIT61 Lab lesson
alean@ida
-24-
TTIT61 Lab lesson
4
UserToKernel
void UserToKernel(void) {
//buffer allocated in kernel space; we copy here
//the content of buffer from the user program
char* kern_buffer = new char[100];
//address
int address = machine->ReadRegister(4);
i=0;
for (;;) {
int data;
machine->ReadMem(address + i, 1, &data);
filename[i] = data;
if (filename[i]==’\0’) break;
i++;
}
}
alean@ida
-25-
machine->ReadMem
ƒmachine->ReadMem(address ,n_of_bytes, &data)
ƒTranslates internally the virtual address address to a
physical address, reads n_of_bytes from that address
and stores them in the variable data
ƒmachine->WriteMem also available in Nachos
TTIT61 Lab lesson
alean@ida
-26-
OpenFile
FileSystem
class FileSystem {
TTIT61 Lab lesson
// code/filesys/filesys.h
public:
class OpenFile {
FileSystem(bool format) {}
// code/filesys/openfile.h
public:
OpenFile(int f) { file = f; currentOffset = 0; }
bool Create(char *name, int initialSize) {
~OpenFile() { Close(file); }
int fileDescriptor = OpenForWrite(name);
if (fileDescriptor == -1) return FALSE;
close(fileDescriptor);
return TRUE;
int Read(char *into, int numBytes) {
…
}
}
OpenFile* Open(char *name) {
int Write(char *from, int numBytes) {
int fileDescriptor = OpenForReadWrite(name, FALSE);
if (fileDescriptor == -1) return NULL;
return new OpenFile(fileDescriptor);
…
}
……
}
}
bool Remove(char *name) { return Unlink(name) == 0; }
};
alean@ida
-27-
TTIT61 Lab lesson
alean@ida
-28-
Exception Handling: System Calls
TTIT61 Lab lesson
Assignment 2
void ExceptionHandler(ExceptionType which) {
…
switch (type) {
case SC_Create:
char k_filename[20];
UserToKernel(k_filename);
fileSystem->Create(k_filename);
break;
}
…
alean@ida
-29-
TTIT61 Lab lesson
ƒ Implement and test the system calls related to file &
console operations
alean@ida
-30-
TTIT61 Lab lesson
5
Why OpenFileID ?
System Calls for File Operations
ƒ File operations (lab 2)
ƒ void Create(char* filename)
¾Create an empty file
ƒ OpenFileId Open(char *filename)
¾open file for read/write
ƒ int Read(char* buf, int size, OpenFileId id)
¾read from an open file
ƒ void Write(char* buf, int size, OpenFileId id)
¾write to an open file
ƒ void Close(int fileid)
¾Close an open file
ƒ See code/userprog/syscall.h for the list
alean@ida
-31-
#include ”syscall.h”
void main(void) {
int file_id;
char a[10];
for (i=0;i<=8;i++) a[i]=’a’;
a[9]=’\0’;
Create(a);
//a is the name of the file
file_id = Open(a); //a is the name of the file
Write(”some text”, 10, file_id); //file_id is the OpenFileId
Close(file_id);
//file_id is the OpenFileId
Halt();
}
TTIT61 Lab lesson
alean@ida
File Related System Calls
ƒ OpenFileId (an integer)
ƒ Global unique id for each open file
ƒ ConsoleInput (o_id==0) and ConsoleOutput (o_id==1)
ƒ The operating system keeps a list of open files
ƒ Read and Write on an open file or on the Console must
be choosen based on the OpenFileId
¾if (o_id>=2) then file; else console;
ƒ The actual file operations are handled by the FileSystem
and OpenFile
alean@ida
-33-
TTIT61 Lab lesson
TTIT61 Lab lesson
-32-
List of Open Files
ƒ Implement in the kernel a list of files that are open
ƒ For each open file, store in the list:
ƒ OpenFileId (this could be the index of your list)
ƒ OpenFile pointer
ƒ Pointer to the thread that opened the file
(currentThread)
ƒ ...
ƒ Open and Close system calls append and remove files
from this list
alean@ida
TTIT61 Lab lesson
-34-
Console
Console Internals
ƒ Console class constructor:
ƒ The Console class handles reading/writing from/to the
console at the char level
ƒ One object with type Console must be used throughout
Nachos
ƒ void PutChar(char ch)
ƒ char GetChar()
ƒ TODO: introduce the possibility to read/write strings
(char* or char[])
ƒ ie. synchronize the console operations
alean@ida
-35-
TTIT61 Lab lesson
Console(
char *readFile, char *writeFile,
VoidFunctionPtr readAvail,
VoidFunctionPtr writeDone,
int callArg
);
alean@ida
-36-
TTIT61 Lab lesson
6
TODO: System Calls for File Operations (Lab2)
Console Internals
ƒ void ReadAvail(int arg) {…}
ƒ void WriteDone(int arg) {…}
ƒ console = new
Console(NULL,NULL,ReadAvail,WriteDone,0);
ƒ An example of a console implementation is given in:
code/userprog/progtest.cc
ƒ Hint: use two semaphores to signal the availability of
an input character (for Read) and the finishing of a
char writing (for Write)
alean@ida
-37-
ƒ File operations (lab 2)
ƒ void Create(char* filename)
ƒ Create an empty file
ƒ OpenFileId Open(char *filename)
ƒ open file for read/write
ƒ int Read(char* buf, int size, OpenFileId id)
ƒ void Write(char* buf, int size, OpenFileId id)
ƒ void Close(int fileid)
ƒ See …/code/userprog/syscall.h for details
ƒ Compile and run nachos always in code/userprog
directory
ƒ Test programs implemented and compiled in code/test
directory
TTIT61 Lab lesson
alean@ida
Nachos Syscalls Info
ƒ Read ”Road Map through Nachos”
ƒ Machine: section 2.1 – 2.4
ƒ Examine exception handling in
¾machine/mipssim.cc (RaiseException)
¾machine.cc (Machine::RaiseException)
¾Userprog/exception.cc (ExceptionHandler)
ƒ System calls ”prototypes” in syscall.h
ƒ FileSystem and OpenFile: 5.2 – 5.3
ƒ User Level Processes: Section 4
alean@ida
-39-
TTIT61 Lab lesson
alean@ida
-41-
TTIT61 Lab lesson
TTIT61 Lab lesson
Nachos Syscalls Info (cont)
ƒ
Source files:
ƒ Working dir. is ../code/userprog
ƒ Central files for assignment
¾”Exception.cc” contains ExceptionHandler function
¾syscall.h - definitions and constants for system calls
ƒ Files necessary for understanding how Nachos works
¾Progtest.cc – test routines, how to load & execute
user prog.
¾Example of running Nachos with a user program:
¾nachos –x <user_program_name>
¾E.g.:
../userprog/nachos –x ../test/halt
¾/machine/machine.* - how MIPS emulator works
alean@ida
-40-
TTIT61 Lab lesson
Lab 3 - Memory Management
Nachos Syscalls Info (cont.)
ƒ In code/userprog: addrspace.h, addrspace.cc
¾data structures to keep track of executing user programs
(address spaces)
ƒ In code/machine:
¾translate.cc
¾data structures for managing translation from virtual
page # -> physical page #
¾used for managing memory on behalf of user programs
¾mipssim.cc (ReadMem, Writemem, Translate, and
OneInstruction)
-38-
ƒ Multiprogramming
ƒ Several processes (user programs) reside in memory at the same
time
¾machine->pageTable contains the pageTable of
the current threads address space
ƒ One address space object for each process
¾ Different page table for each process
¾A pageTable inside the AddrSpace object:
translation of virtual address to physical address
alean@ida
-42-
TTIT61 Lab lesson
7
Lab 3 - Memory Management (cont.)
ƒ Separate OpenFile objects for each process
ƒ Different processes can open the same file
ƒ Each process has it’s own offset into file (i.e.
different OpenFile object)
alean@ida
TTIT61 Lab lesson
-43-
User Programs
ƒ Reside in user address space
ƒ Communication with the kernel
ƒ through system calls (open file, start a new process,
etc.)
ƒ Only one user program in the lab #2
alean@ida
User Level Processes
ƒ When executing a user program,
ƒ Nachos creates an address space
ƒ Copies the content of the instructions’ and initialized
variable segments into the address space.
ƒ Uninitialized variable section are not read from the file (as
it contains all 0’s).
alean@ida
TTIT61 Lab lesson
-45-
TTIT61 Lab lesson
-44-
Process Creation
ƒ Nachos process are formed by:
ƒ Creating an address space
ƒ Allocating physical memory for the AddrSpace
ƒ Load content of executable into the physical memory
ƒ Initialize registers and address translation tables
ƒ Invoke machine::Run() to start executing.
ƒ Run turns on the simulated MIPS machine and enter
into an infinite loop that executes one instruction at a
time.
ƒ progtest.cc to see an example
alean@ida
TTIT61 Lab lesson
-46-
Lab 2 - User Programs
Memory Organization
Memory
ƒ Addresses in Nachos
Address space of a Nachos process
Pages
•Page number
•Same size
•Allocation unit for
a process
machine
machine->mainMemory
Adress space of user
program.
Other machine variables
thread 1
thread 2
thread
variables
thread
variables
(”Physical” memory
alean@ida
of the MIPS machine)
-47-
TTIT61 Lab lesson
ƒMachine.h contains the details regarding the memory size,
number of pages, page size, etc.
alean@ida
-48-
TTIT61 Lab lesson
8
Lab 3 - Page tables in lab 2
Lab 3 - Page tables in lab 3
Running process
Running process
Machine object
Physical
Memory
0
2
Physical
Memory
1
Machine object
n-1
1
pageTable
pageTable
2
0
...
Process1
...
3
n
Process1
ƒ Only one process running
ƒ Every process gets the same physical pages
alean@ida
-49-
TTIT61 Lab lesson
ƒ More than one process
running
ƒ bitMap can be used for
allocation
alean@ida
Process2
-50-
Lab 3 - Address spaces
ƒ Address spaces (AddrSpace object)
ƒ AddrSpace constructor allocates memory pages
ƒ In lab 2 all memory is assigned to one address space
ƒ In lab 3 several address spaces will exist
ƒ Use a BitMap object to keep track of free pages
¾BitMap(int nitems) // total number of items
¾Mark
// mark (allocate) a
position
¾Clear
// clear (free) a position
¾Test
// check if a position is marked
¾Find
// find a free position and mark it
alean@ida
-51-
TTIT61 Lab lesson
Lab 3 - Address spaces
ƒ AddrSpace constructor
ƒ Takes an executable (filename)
ƒ Allocates memory, sets up page table
¾Amount of memory needed is stored in noff-header
ƒ Loads the executable
ƒ Noff binary format, noff header
ƒ NoffHeader object stored in the beginning of the file
ƒ Three Segment objects in the header
alean@ida
-52-
Noff Executable Format
ƒ Noff: Nachos Object File Format
ƒ Compare with coff(Unix), com, exe (DOS)
ƒ Noff consists of four parts
¾Noff header (information concerning segments
below)
¾Code segment – program residence
¾Initialized variables segment
¾Unitialized variables segment
alean@ida
-53-
TTIT61 Lab lesson
TTIT61 Lab lesson
TTIT61 Lab lesson
Noff Format (cont)
ƒ Noff: header
¾information concerning segments below
¾Describe content of the rest of the file, giving
information about programs’s instructions,
initialized and uninitialized variables
ƒ noffMagic: Noff format check (4 bytes)
¾Reserved number indicating that the file is in Noff
format
ƒ For each of the remaining sections Nachos maintains
¾virtualAddr: segment’s starting address in virtual
memory
¾inFileAddr: start of the segment in Noff file
¾Size: size of the segment
alean@ida
-54-
TTIT61 Lab lesson
9
Address Spaces
ƒ Segments:
ƒ code
¾the executable machine code
ƒ initData
¾initialized data (constant strings etc)
ƒ uninitData
¾Declared but unassigned data (empty arrays etc).
¾Not stored in the file.
alean@ida
TTIT61 Lab lesson
-55-
Memory Management
ƒ TODO:
ƒ Allocate/Deallocate physical pages (addrspace)
ƒ Setting translation tables
ƒ Implement Exec, Exit, Join
ƒ Tests:
ƒ Simultaneously running user programs
ƒ Using pre-emptive switches between threads
alean@ida
-57-
TTIT61 Lab lesson
TODO: Lab 3 System Calls for Exit & Join
ƒ int Join(SpaceId id)
ƒ exits the calling user process
ƒ returns ”status” to the parent process
-59-
ƒ The Segment object:
ƒ virtualAddr
¾start address of segment in the address space
ƒ inFileAddr
¾offset of segment in the file
ƒ size
¾size of the segment
alean@ida
-56-
TTIT61 Lab lesson
TODO: Syscalls for Mem. Management. (Lab 3)
ƒ Multiple processes and threads (lab 3)
ƒ SpaceId Exec(char *name)
ƒ Load a program/file from disk into a new AddrSpace
ƒ Create a new Thread for it to execute in (forks and
execute a new program)
ƒ Return a unique global id for that AddrSpace object
(SpaceId of the new program)
alean@ida
-58-
TTIT61 Lab lesson
Lab 3: System Calls for Memory Management
ƒ void Fork(void (* func)()) --optional
ƒ Start a new user level thread in the same address space
ƒ Execute a function func in that thread
ƒ void Yield() --optional
ƒ Switch to another thread, in this address space or in
another (i.e. another process)
ƒ See …/code/userprog/syscall.h
ƒ Roadmap through Nachos, section 4
ƒ Wait for the process with id id to exit
ƒ Return the exit status of that process
ƒ void Exit(int status)
alean@ida
Lab 3 - Address Space
TTIT61 Lab lesson
alean@ida
-60-
TTIT61 Lab lesson
10
Download