From a2b410fca3f09ec4db5b8a979eef374a5535417a Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Sun, 24 Oct 2021 18:30:09 -0300 Subject: [PATCH] Finish scheduler implementation Co-authored-by: Ezequiel Bellver Co-authored-by: Juan Barmasch --- Kernel/drivers/video.c | 64 ++++----- Kernel/include/scheduler.h | 2 +- Kernel/interruptions/exceptions.c | 4 +- Kernel/kernel.c | 1 + Kernel/utils/memManagerFRT4.c | 2 +- Kernel/utils/scheduler.c | 136 ++++++++++++------- Kernel/utils/semaphore.c | 3 + Userland/SampleCodeModule/include/libc.h | 1 + Userland/SampleCodeModule/libc.c | 4 + Userland/SampleCodeModule/sampleCodeModule.c | 2 +- Userland/SampleCodeModule/shell/shell.c | 7 +- 11 files changed, 137 insertions(+), 89 deletions(-) diff --git a/Kernel/drivers/video.c b/Kernel/drivers/video.c index bda9491..1cf280d 100644 --- a/Kernel/drivers/video.c +++ b/Kernel/drivers/video.c @@ -11,38 +11,38 @@ static int currentY = 0; int limitX[2] = {0, 80}; int limitY[2] = {0, 25}; -char windowVideo = 1; - -void changeWindow() { - windowVideo = 1 - windowVideo; - moveToWindowVideo(windowVideo); -} - -void moveToWindowVideo(char window) { - if (window == -1) { - windowVideo = -1; - limitX[0] = 0; - limitX[1] = 80; - limitY[0] = 0; - limitY[1] = 25; - } - if (window == 1) { - windowVideo = 1; - limitX[0] = 40; - limitX[1] = 80; - limitY[0] = 0; - limitY[1] = 25; - } - if (window == 0) { - windowVideo = 0; - limitX[0] = 0; - limitX[1] = 40; - limitY[0] = 0; - limitY[1] = 25; - } - currentX = limitX[0]; - currentY = limitY[0]; -} +// char windowVideo = 1; +// +// void changeWindow() { +// windowVideo = 1 - windowVideo; +// moveToWindowVideo(windowVideo); +// } +// +// void moveToWindowVideo(char window) { +// if (window == -1) { +// windowVideo = -1; +// limitX[0] = 0; +// limitX[1] = 80; +// limitY[0] = 0; +// limitY[1] = 25; +// } +// if (window == 1) { +// windowVideo = 1; +// limitX[0] = 40; +// limitX[1] = 80; +// limitY[0] = 0; +// limitY[1] = 25; +// } +// if (window == 0) { +// windowVideo = 0; +// limitX[0] = 0; +// limitX[1] = 40; +// limitY[0] = 0; +// limitY[1] = 25; +// } +// currentX = limitX[0]; +// currentY = limitY[0]; +// } void increment() { currentX++; diff --git a/Kernel/include/scheduler.h b/Kernel/include/scheduler.h index d29bfac..a2c17f7 100644 --- a/Kernel/include/scheduler.h +++ b/Kernel/include/scheduler.h @@ -11,7 +11,7 @@ void forceTimer(); void haltcpu(); -#define STACK_SIZE 1024 +#define STACK_SIZE 1024 * 512 #define MAX_PRIORITY 40 // Maximum number for a priority #define MIN_PRIORITY 0 // Minimum number for a priority (yet maximum level of priority) // me gusta #define DEF_PRIORITY 0 diff --git a/Kernel/interruptions/exceptions.c b/Kernel/interruptions/exceptions.c index ccbacfd..9187172 100755 --- a/Kernel/interruptions/exceptions.c +++ b/Kernel/interruptions/exceptions.c @@ -84,12 +84,12 @@ static void startOver() { clear(); // cleanProcesses(); - moveToWindowVideo(1); + // moveToWindowVideo(1); //((fn)sampleCodeAddress)(); } static void genericException(char * string, int len) { - moveToWindowVideo(-1); + // moveToWindowVideo(-1); clear(); printStringLen(15, string, len); printRegs(); diff --git a/Kernel/kernel.c b/Kernel/kernel.c index b14f395..f15e98e 100644 --- a/Kernel/kernel.c +++ b/Kernel/kernel.c @@ -178,6 +178,7 @@ int main() { // ((EntryPoint)sampleCodeModuleAddress)(); char * argv[] = {"SampleCode"}; enqueueProcess(sampleCodeModuleAddress, 1, 1, argv); + clear(); // haltcpu(); _sti(); // forceTimer(); diff --git a/Kernel/utils/memManagerFRT4.c b/Kernel/utils/memManagerFRT4.c index 99042c8..367b3a4 100644 --- a/Kernel/utils/memManagerFRT4.c +++ b/Kernel/utils/memManagerFRT4.c @@ -22,7 +22,7 @@ typedef unsigned long UBaseType_t; #define configSUPPORT_STATIC_ALLOCATION 1 #define configSUPPORT_DYNAMIC_ALLOCATION 1 -#define configTOTAL_HEAP_SIZE 1024 * 1024 +#define configTOTAL_HEAP_SIZE 1024 * 1024 * 100 #define configAPPLICATION_ALLOCATED_HEAP 0 #define portBYTE_ALIGNMENT 8 #define portBYTE_ALIGNMENT_MASK ( 0x0007 ) // 8 diff --git a/Kernel/utils/scheduler.c b/Kernel/utils/scheduler.c index 9470b31..382d36c 100644 --- a/Kernel/utils/scheduler.c +++ b/Kernel/utils/scheduler.c @@ -1,4 +1,6 @@ #include "scheduler.h" +#define INIT_PID 1 + void _initialize_stack_frame(void *, void *, int, char**); enum states {READY = 0, BLOCKED}; @@ -25,33 +27,46 @@ int readyLen = 0; int blockedLen = 0; static processCDT * currentProcess = NULL; -int pids = 0; +static int pids = INIT_PID; +static char update = 1; #include "naiveConsole.h" #include "time.h" + + uint64_t nextProcess() { + update = 1; if (currentProcess == NULL) { - ncClear(); - ncPrint("Una cubana para el socio biza"); - ncPrint(firstReady->name); - // ncPrintDec(firstReady->pid); - ncPrintHex(firstReady->rsp); - ncPrintHex(firstReady->rbp); + // ncClear(); + // ncPrint("Una cubana para el socio biza"); + // ncPrint(firstReady->name); + // // ncPrintDec(firstReady->pid); + // ncPrintHex(firstReady->rsp); + // ncPrintHex(firstReady->rbp); // wait(4); + if (firstReady == NULL) + unblock(INIT_PID); + currentProcess = firstReady; return firstReady->rsp; } if (currentProcess->executions < MAX_PRIORITY - currentProcess->priority + 1) { currentProcess->executions++; - ncClear(); - ncPrint("Hola"); + // ncClear(); + // ncPrint("Hola"); // ncPrintDec(firstReady->pid); // wait(4); return currentProcess->rsp; } - ncPrint("Chau"); + // ncPrint("Chau"); currentProcess->executions = 0; - currentProcess = currentProcess->next; + if (currentProcess->next != NULL) + currentProcess = currentProcess->next; + else { + // ncPrint("Una venezolana para el socio biza"); + // wait(4); + currentProcess = firstReady; + } return currentProcess->rsp; } @@ -67,8 +82,12 @@ void initScheduler() { } void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[]) { + if (firstReady != NULL && firstReady->pid == INIT_PID) + block(INIT_PID); + processADT process = pvPortMalloc(sizeof(processCDT)); - uint64_t * rbp = STACK_SIZE + pvPortMalloc(STACK_SIZE); + uint64_t * auxi = pvPortMalloc(STACK_SIZE); + uint64_t * rbp = STACK_SIZE + auxi; uint64_t * rsp = rbp - 20; //22 char priority = (foreground == 1) ? DEF_PRIORITY : MAX_PRIORITY/2; @@ -94,41 +113,40 @@ void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char * _initialize_stack_frame(fn, rbp, argc, argv); - if (firstReady == NULL) { + if (firstReady == NULL) firstReady = process; - lastReady = firstReady; - } - else + else lastReady->next = process; + lastReady = process; - ncClear(); - ncPrint(argv[0]); - // proc->name = argv[0]; - ncPrint(process->name); - ncPrintDec(process->pid); - ncPrintHex(process->rsp); - ncPrintHex(process->rbp); + // ncClear(); + // ncPrint(argv[0]); + // // proc->name = argv[0]; + // ncPrint(process->name); + // ncPrintDec(process->pid); + // ncPrintHex(process->rsp); + // ncPrintHex(process->rbp); // wait(3); return; } void newProcess(processADT process, char * name, char priority, char foreground, uint64_t rsp, uint64_t rbp) { - char aux[MAX_NAME_SIZE]; - int j; - for (j = 0; j < MAX_NAME_SIZE - 1 && name[j] != 0; j++) { - aux[j] = name[j]; - } - aux[j] = '\0'; + // char aux[MAX_NAME_SIZE]; + // int j; + // for (j = 0; j < MAX_NAME_SIZE - 1 && name[j] != 0; j++) { + // aux[j] = name[j]; + // } + // aux[j] = '\0'; - process->name = aux; - process->pid = pids++; - process->ppid = currentProcess->pid; - process->priority = priority; - process->rsp = rsp; - process->rbp = rbp; - process->executions = 0; - process->foreground = foreground; - process->state = READY; + // process->name = aux; + // process->pid = pids++; + // process->ppid = currentProcess->pid; + // process->priority = priority; + // process->rsp = rsp; + // process->rbp = rbp; + // process->executions = 0; + // process->foreground = foreground; + // process->state = READY; } // void loader(int argc, char * argv[], void (*fn) (int, char **)) { @@ -169,11 +187,20 @@ char block(int pid) { firstReady = del->next; } - lastBlocked->next = del; + processCDT * next = del->next; del->next = NULL; + + if (lastBlocked != NULL) + lastBlocked->next = del; + else + firstBlocked = del; lastBlocked = del; - forceTimer(); + if (pid == currentProcess->pid) { + update = 0; + currentProcess = next; + forceTimer(); + } return EXIT_SUCCESS; } @@ -188,10 +215,13 @@ char unblock(int pid) { prev->next = del->next; else firstBlocked = del->next; + del->next = NULL; + if (lastReady != NULL) + lastReady->next = del; + else + firstReady = del; + lastReady = del; } - - if (pid == del->pid) - forceTimer(); return EXIT_SUCCESS; } @@ -217,12 +247,17 @@ char kill(int pid) { firstReady = del->next; } - if (pid == del->pid) - forceTimer(); - - vPortFree((void *) del->rsp); + processCDT * next = del->next; + + vPortFree((void *) del->rbp - STACK_SIZE); vPortFree((void *) del); + if (pid == currentProcess->pid) { + update = 0; + currentProcess = next; + forceTimer(); + } + return EXIT_SUCCESS; } @@ -239,12 +274,13 @@ char nice(char offset) { void updateRSP(uint64_t newRsp) { if (currentProcess == NULL) { - ncClear(); - ncPrint("ES NULL"); + // ncClear(); + // ncPrint("ES NULL"); // wait(4); return; } - currentProcess->rsp = newRsp; + if (update) + currentProcess->rsp = newRsp; } int getPid() { diff --git a/Kernel/utils/semaphore.c b/Kernel/utils/semaphore.c index 9761b3d..1e355e2 100644 --- a/Kernel/utils/semaphore.c +++ b/Kernel/utils/semaphore.c @@ -31,11 +31,13 @@ int semPost(char semaphore) {} // o pasando la estructura de sem. esta es la q hace posix sem_t * semOpen(char * name, unsigned int value) { enter_region(&semLock); + // leave_region(&semLock); } int semClose(sem_t * semaphore) { enter_region(&semLock); + // leave_region(&semLock); } @@ -60,5 +62,6 @@ int semWait(sem_t * semaphore) { int semPost(sem_t * semaphore) { enter_region(&semLock); + // leave_region(&semLock); } \ No newline at end of file diff --git a/Userland/SampleCodeModule/include/libc.h b/Userland/SampleCodeModule/include/libc.h index 417176a..1e4596a 100644 --- a/Userland/SampleCodeModule/include/libc.h +++ b/Userland/SampleCodeModule/include/libc.h @@ -6,6 +6,7 @@ void winClear(); void printString(char * string); void printStringError(char * string); +void printStringLen(char * string, int len); void new_line(); char getChar(); int abs(); diff --git a/Userland/SampleCodeModule/libc.c b/Userland/SampleCodeModule/libc.c index 6aeebb3..1786bd1 100644 --- a/Userland/SampleCodeModule/libc.c +++ b/Userland/SampleCodeModule/libc.c @@ -5,6 +5,10 @@ void printString(char * string) { sys_write(1, string, len); } +void printStringLen(char * string, int len) { + sys_write(1, string, len); +} + void printStringError(char * string) { int len = strlen(string); sys_write(2, string, len); diff --git a/Userland/SampleCodeModule/sampleCodeModule.c b/Userland/SampleCodeModule/sampleCodeModule.c index 7d04867..5ebbbc9 100644 --- a/Userland/SampleCodeModule/sampleCodeModule.c +++ b/Userland/SampleCodeModule/sampleCodeModule.c @@ -15,5 +15,5 @@ int main(int argc, char *argv[]) { // sys_switchContext(); sys_exit(); - return 1; + return 1; } diff --git a/Userland/SampleCodeModule/shell/shell.c b/Userland/SampleCodeModule/shell/shell.c index 0f9eb45..d4ead73 100644 --- a/Userland/SampleCodeModule/shell/shell.c +++ b/Userland/SampleCodeModule/shell/shell.c @@ -14,7 +14,7 @@ #define SIZE 100 #define MAX_ARGS 5 -#define COLS 40 +#define COLS 80 #define ROWS 25 const int len = 8; @@ -42,7 +42,7 @@ void addText(char * buffer, char * window, int * offset) { } void printWindow(char * window) { - printString(window); + printStringLen(window, ROWS * COLS); } void scanfNoPrint(char * buffer, int maxSize, char * window, int * offset) { @@ -80,6 +80,9 @@ void clearWindow(char * window, int * offset) { printWindow(window); } +// static char window[ROWS * COLS + 1] = {[0 ... ROWS * COLS - 1] = ' ', 0}; +// static char buffer[SIZE] = {0}; +// static char *tokens[SIZE] = {0}; void shell(int argc, char *argv[]) { char window[ROWS * COLS + 1] = {[0 ... ROWS * COLS - 1] = ' ', 0}; int offset = (ROWS - 1) * COLS;