From 2a3198c949e76db09a1e7321ecc9d4d22aad13ec Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Fri, 15 Oct 2021 02:04:09 +0200 Subject: [PATCH] Update all syscall And add PutInt() and GetInt() --- code/Makefile.dep | 4 +-- code/test/start.S | 18 +++++++++++ code/threads/threadtest.cc | 1 - code/userprog/consoledriver.cc | 32 +++++++++++++++---- code/userprog/consoledriver.h | 2 ++ code/userprog/exception.cc | 58 +++++++++++++++++++++++----------- code/userprog/syscall.h | 11 +++++-- 7 files changed, 95 insertions(+), 31 deletions(-) diff --git a/code/Makefile.dep b/code/Makefile.dep index e42a43b..1678db3 100644 --- a/code/Makefile.dep +++ b/code/Makefile.dep @@ -41,8 +41,8 @@ CFLAGS += -fsanitize=undefined LDFLAGS += -fsanitize=undefined # décommenté TD1 - partie V -#CFLAGS += -fsanitize=address -#LDFLAGS += -fsanitize=address -lpthread +CFLAGS += -fsanitize=address +LDFLAGS += -fsanitize=address -lpthread endif ifeq ($(NACHOS_SYS),MAC_OS_SYS) diff --git a/code/test/start.S b/code/test/start.S index 54d4d82..52911f8 100644 --- a/code/test/start.S +++ b/code/test/start.S @@ -181,6 +181,24 @@ GetString: syscall j $31 .end GetString + + .globl PutInt + .ent PutInt +PutInt: + addiu $2,$0,SC_PutInt + syscall + j $31 + .end PutInt + + .globl GetInt + .ent GetInt +GetInt: + addiu $2,$0,SC_GetInt + syscall + j $31 + .end GetInt + + #endif /* dummy function to keep gcc happy */ diff --git a/code/threads/threadtest.cc b/code/threads/threadtest.cc index b4b4fde..a169b18 100644 --- a/code/threads/threadtest.cc +++ b/code/threads/threadtest.cc @@ -11,7 +11,6 @@ #include "copyright.h" #include "system.h" - //---------------------------------------------------------------------- // SimpleThread // Loop 10 times, yielding the CPU to another ready thread diff --git a/code/userprog/consoledriver.cc b/code/userprog/consoledriver.cc index dd4aec4..50a58b3 100644 --- a/code/userprog/consoledriver.cc +++ b/code/userprog/consoledriver.cc @@ -37,22 +37,22 @@ int ConsoleDriver::GetChar() return ch; } -void ConsoleDriver::PutString(const char s[MAX_STRING_SIZE]) +void ConsoleDriver::PutString(const char s[]) { - int i; - for (i = 0; i < MAX_STRING_SIZE; i++){ - if (*(s+i) =='\0'){ - return; - } + int i = 0; + while (*(s+i) != '\0') + { PutChar(*(s+i)); + i++; } } void ConsoleDriver::GetString(char * s, int n) { int i; - for ( i = 0; i < n - 1; i++){ + for ( i = 0; i < n; i++){ char c = GetChar(); + DEBUG('s', "consoledriver->getstring keycode:%d, position:%d\n", (int)c, i); if ( c == '\0' || c == '\n' || c == EOF) { break; } @@ -61,4 +61,22 @@ void ConsoleDriver::GetString(char * s, int n) *(s+i) = '\0'; } +void ConsoleDriver::PutInt(int n) +{ + char *buffer = new char[MAX_STRING_SIZE]; + snprintf(buffer,MAX_STRING_SIZE,"%d",n); + DEBUG('s', "consoledriver->PutInt buffer:%s\n", buffer); + PutString(buffer); + delete [] buffer; +} + +int ConsoleDriver::GetInt() +{ + int n; + char *buffer = new char[MAX_STRING_SIZE]; + GetString(buffer,MAX_STRING_SIZE); + sscanf(buffer, "%d", &n); + delete [] buffer; + return n; +} #endif // CHANGED diff --git a/code/userprog/consoledriver.h b/code/userprog/consoledriver.h index 9f49ad5..a313f77 100644 --- a/code/userprog/consoledriver.h +++ b/code/userprog/consoledriver.h @@ -18,6 +18,8 @@ class ConsoleDriver:dontcopythis { void PutString(const char *s); // Behaves like fputs(3S) void GetString(char *s, int n); // Behaves like fgets(3S) + void PutInt(int n); + int GetInt(); private: Console *console; }; diff --git a/code/userprog/exception.cc b/code/userprog/exception.cc index 1827a5e..8da8f1f 100644 --- a/code/userprog/exception.cc +++ b/code/userprog/exception.cc @@ -69,7 +69,8 @@ int copyStringFromMachine(int from, char *to, unsigned size) int res; // Need to read size-1 to put final /0 if needed - while((iReadMem(from+i,1,&res))){ + while((i < size) && (machine->ReadMem(from+i,1,&res))){ + DEBUG('s', "\n ->copyFromMachine keycode:%d", res); *(to+i) = (char)res; if ((char)res == '\0'){ return i; @@ -83,7 +84,8 @@ int copyStringFromMachine(int from, char *to, unsigned size) int copyStringToMachine(int to, char* from, unsigned size) { unsigned i; - for (i=0; i < size - 1; i++) { + for (i=0; i < size ; i++) { + DEBUG('s', "\n ->copyToMachine keycode:%d", (int)from[i]); if (from[i] == '\0') { break; } @@ -134,14 +136,11 @@ ExceptionHandler (ExceptionType which) int readsize; int addr_start = machine->ReadRegister(4); do { - char* read = new char[MAX_STRING_SIZE]; + char *read = new char[MAX_STRING_SIZE + 1]; readsize = copyStringFromMachine( addr_start, read, MAX_STRING_SIZE); + DEBUG('s', "\nRead buffer:%s, size:%d\n", read, readsize); consoledriver->PutString(read); - // beware, the last element of our buffer was added by - // copyStringFromMachine(), we need to remove one memory case - // or the first char of our next read will be - // forgotten - addr_start += readsize - 1; + addr_start += readsize ; delete [] read; } while(readsize == MAX_STRING_SIZE); break; @@ -151,32 +150,55 @@ ExceptionHandler (ExceptionType which) DEBUG ('s', "GetChar.\n"); // Get the char input an write it to the register n.2 int c = consoledriver->GetChar(); - machine->WriteRegister((2), c); + machine->WriteRegister(2, c); break; } case SC_GetString: { - DEBUG ('s', "GetString.\n"); + DEBUG('s', "Begin GetString.\n"); int writesize = 0; int addr = machine->ReadRegister(4); int size = machine->ReadRegister(5); - DEBUG ('s', "size:%d\n", size); + DEBUG('s', "size:%d\n", size); do { - char* write = new char[MAX_STRING_SIZE]; - consoledriver->GetString(write, MAX_STRING_SIZE); - DEBUG('s', "write:%s:\n", write); - writesize = copyStringToMachine(addr, write, MAX_STRING_SIZE); + // Do not process char before size + // but for now we don't have any method to determine if + // string passed to our function est bigger than expected (size) + int n_char = ( size < MAX_STRING_SIZE) ? size : MAX_STRING_SIZE; + + // allocate on byte more than buffer to put our \0 if needed + char *write = new char[n_char + 1]; + consoledriver->GetString(write, n_char); + writesize = copyStringToMachine(addr, write, n_char); addr += writesize; - DEBUG('s', "Writed:%d\n", writesize); + size = size - writesize; + DEBUG('s', "writed:%s, size:%d, remain:%d\n", write, writesize, size); delete [] write; - } while( writesize == MAX_STRING_SIZE - 1 ); + } while( size > 0 && writesize == MAX_STRING_SIZE ); + DEBUG('s', "End GetString\n"); break; } + case SC_PutInt: + { + DEBUG('s', "PutInt.\n"); + int n = machine->ReadRegister(4); + DEBUG('s', "PutInt parameter: %d\n", n); + consoledriver->PutInt(n); + } + break; + case SC_GetInt: + { + DEBUG('s', "GetInt\n"); + int n = consoledriver->GetInt(); + DEBUG('s', "Number entered: %d\n", n); + machine->WriteRegister(2,n); + break; + } #endif default: { - printf("Unimplemented system call %d\n", type); + printf("Unimplemented system call %d\n", type); ASSERT(FALSE); } } diff --git a/code/userprog/syscall.h b/code/userprog/syscall.h index a072008..4ba2c36 100644 --- a/code/userprog/syscall.h +++ b/code/userprog/syscall.h @@ -36,6 +36,8 @@ #define SC_PutString 12 #define SC_GetChar 13 #define SC_GetString 14 +#define SC_PutInt 15 +#define SC_GetInt 16 #endif #ifdef IN_USER_MODE @@ -137,9 +139,12 @@ void Yield (); #ifdef CHANGED void PutChar (char c); -void PutString (char * s); -void GetChar(char c); -void GetString(char * s, int n ); +void PutString (char *s); +char GetChar(); +void GetString(char *s, int n ); +void PutInt(int n); +int GetInt(); + #endif // CHANGED #endif // IN_USER_MODE