From 07853cbcd3606a06edc56420d8dc2282f2b339b8 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Wed, 8 Dec 2021 01:30:52 +0100 Subject: [PATCH] TD3:II.1 First working version of ForkExec But need to implement Process exit... --- code/machine/machine.h | 2 +- code/test/start.S | 10 ++++++++- code/test/userpages0 | Bin 808 -> 936 bytes code/threads/system.h | 2 +- code/userprog/exception.cc | 8 ++++++++ code/userprog/syscall.h | 2 ++ code/userprog/userthread.cc | 40 +++++++++++++++++++++++++++++++++++- code/userprog/userthread.h | 1 + 8 files changed, 61 insertions(+), 4 deletions(-) diff --git a/code/machine/machine.h b/code/machine/machine.h index 5422e22..f58b667 100644 --- a/code/machine/machine.h +++ b/code/machine/machine.h @@ -36,7 +36,7 @@ // the disk sector size, for // simplicity -#define NumPhysPages 64 // Increase this as necessary! +#define NumPhysPages 128 // Increase this as necessary! #define MemorySize (NumPhysPages * PageSize) #define TLBSize 4 // if there is a TLB, make it small diff --git a/code/test/start.S b/code/test/start.S index d79b2f4..353097d 100644 --- a/code/test/start.S +++ b/code/test/start.S @@ -213,7 +213,15 @@ ThreadExit: syscall j $31 .end ThreadExit - + + + .globl ForkExec + .ent ForkExec +ForkExec: + addiu $2,$0,SC_ForkExec + syscall + j $31 + .end ForkExec #endif /* dummy function to keep gcc happy */ diff --git a/code/test/userpages0 b/code/test/userpages0 index b6243774929b274e918c76bbd09bf73cdf608b9e..ce0fec15db07a466d644fb9626f4de2e6cbb7e75 100644 GIT binary patch delta 115 zcmZ3%wt`)3?fqR0AfN#vm?2n$d7{`P#;%Pk-5D8$CqHCNw<=>`;1OWhu%3ZIz~%v? vfEoh>s|^G5Qk5*AxS|3B6Nt}(!e_Aw1IlqQPi|sTn`oOo(SddH3Z@1C8n_lc delta 106 zcmZ3%zJg6`?fqR0AfN%n4NMTcf@z}IB*wOlE8Q6 ofq~VgfoZ8q7EoMKfdQzA)usi7&tek>lReadRegister(4); + char * command = new char[MAX_STRING_SIZE]; + int size = copyStringFromMachine(s, command, MAX_STRING_SIZE); + do_ForkExec(command); + break; + } #endif default: { diff --git a/code/userprog/syscall.h b/code/userprog/syscall.h index 94c598a..3b930b3 100644 --- a/code/userprog/syscall.h +++ b/code/userprog/syscall.h @@ -40,6 +40,7 @@ #define SC_GetInt 16 #define SC_ThreadCreate 17 #define SC_ThreadExit 18 +#define SC_ForkExec 19 #endif #ifdef IN_USER_MODE @@ -148,6 +149,7 @@ void PutInt(int n); void GetInt( int * n); int ThreadCreate(void f(void * args), void * args); void ThreadExit(void); +int ForkExec ( const char * s); #endif // CHANGED diff --git a/code/userprog/userthread.cc b/code/userprog/userthread.cc index 9267df8..f93039e 100644 --- a/code/userprog/userthread.cc +++ b/code/userprog/userthread.cc @@ -88,8 +88,46 @@ void do_ThreadExit(){ if ( currentThread->space->threads == 0 ){ // No threads remains, desallocate addrspace delete currentThread->space; - Exit(0); + currentThread->space = NULL; } currentThread->Finish(); } + +void StartUserProc( void * args ){ + currentThread->space->InitRegisters(); + currentThread->space->RestoreState(); + machine->DumpMem("memory.svg"); + machine->Run(); +} + +int do_ForkExec( const char * c){ + DEBUG('x', "Enter ForkExec\n"); + AddrSpace * space; + Thread * fork; + OpenFile * file; + + file = fileSystem->Open(c); + + if (file == NULL) { + printf("Unable to open file %s\n", file); + return -1; + } + + try { + space = new AddrSpace(file); + }catch(const std::bad_alloc& e) { + printf("==> BAD ALLOC ERROR: no empty space\n"); + delete space; + ASSERT(false); + } + + fork = new Thread("forkThread"); + fork->space = space; + fork->Start(StartUserProc, NULL); + + delete file; + + return 0; +} + #endif diff --git a/code/userprog/userthread.h b/code/userprog/userthread.h index e49b51b..42028f8 100644 --- a/code/userprog/userthread.h +++ b/code/userprog/userthread.h @@ -11,4 +11,5 @@ typedef struct { extern int do_ThreadCreate(int f, int arg); extern void do_ThreadExit(); +extern int do_ForkExec( const char * s); #endif