diff --git a/Kernel/drivers/time.c b/Kernel/drivers/time.c index ef5c01f..65fcdbe 100755 --- a/Kernel/drivers/time.c +++ b/Kernel/drivers/time.c @@ -12,7 +12,7 @@ int ticks_elapsed() { int seconds_elapsed() { // return ticks / 18; - return ticks; + return ticks / 18; } int getTime(char option) { @@ -33,6 +33,11 @@ int getTime(char option) { } } +// TODO void wait(long seconds) { - while (seconds_elapsed() < seconds); + // int initialSeconds = getTime(SECONDS); + // while (initialSeconds - seconds != initialSeconds) { + + // } + // while (seconds_elapsed() < seconds); } \ No newline at end of file diff --git a/Kernel/interruptions/exceptions.c b/Kernel/interruptions/exceptions.c index 9187172..2eb7400 100755 --- a/Kernel/interruptions/exceptions.c +++ b/Kernel/interruptions/exceptions.c @@ -74,15 +74,21 @@ void printRegs() { *rspValueA = (uint64_t) getSampleRSP(); } +void exitProcess(); static void startOver() { - unsigned char key = 0; - while (key != '\n') { - _sti(); - haltcpu(); - key = getKeyFromBuffer(); - } - clear(); + exitProcess(); + + // TODO : hacer wait con getTimegen + + // unsigned char key = 0; + // while (key != '\n') { + // _sti(); + // haltcpu(); + // key = getKeyFromBuffer(); + // } + + // clear(); // cleanProcesses(); // moveToWindowVideo(1); //((fn)sampleCodeAddress)(); diff --git a/Kernel/interruptions/systemCallsDispatcher.c b/Kernel/interruptions/systemCallsDispatcher.c index 479d254..4e18486 100644 --- a/Kernel/interruptions/systemCallsDispatcher.c +++ b/Kernel/interruptions/systemCallsDispatcher.c @@ -2,6 +2,9 @@ #include "systemCalls.h" void exitProcess(); +// void setFn(uint64_t, uint64_t, uint64_t); +char * processes(); +void enqueueProcess(uint64_t, uint64_t, uint64_t, uint64_t); uint64_t systemCallsDispatcher(uint64_t rdi, uint64_t rsi, uint64_t rdx, uint64_t rcx, uint64_t r8) { switch (rdi) { @@ -18,6 +21,11 @@ uint64_t systemCallsDispatcher(uint64_t rdi, uint64_t rsi, uint64_t rdx, uint64_ case 4: exitProcess(); break; + case 5: + // setFn(rsi, rdx, rcx); + break; + case 6: + return (uint64_t) processes(); default: return -1; } diff --git a/Kernel/utils/scheduler.c b/Kernel/utils/scheduler.c index 382d36c..aa4e655 100644 --- a/Kernel/utils/scheduler.c +++ b/Kernel/utils/scheduler.c @@ -63,7 +63,7 @@ uint64_t nextProcess() { if (currentProcess->next != NULL) currentProcess = currentProcess->next; else { - // ncPrint("Una venezolana para el socio biza"); + // ncPrint("Una colombiana para el socio biza"); // wait(4); currentProcess = firstReady; } @@ -81,6 +81,11 @@ void initScheduler() { enqueueProcess(idle, 0, 1, argv); } +// void setFn(void (*fn) (int, char **), int argc, char *argv[]) { +// currentProcess->rsp = (currentProcess->rbp) - 20; + // _initialize_stack_frame(fn, currentProcess->rbp, argc, argv); +// } + void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[]) { if (firstReady != NULL && firstReady->pid == INIT_PID) block(INIT_PID); @@ -313,50 +318,72 @@ char getProcessData(char * out, processCDT * proc) { char written = 0; char flag = 0; - for (int j = 0; j < MAX_ATTR_SIZE; j++) { - if (proc->name[j] != 0) + for (int j = 0; j < MAX_NAME_SIZE; j++) { + if (!flag && proc->name[j] == 0) flag = 1; - if (flag){ + else if (flag) out += addSpaces(out, 1); - } else + else *out++ = proc->name[j]; } - written += MAX_ATTR_SIZE; + written += MAX_NAME_SIZE; out += addSpaces(out, 2); written += 2; char buffer[10]; - written += strcpy(out, itoa(proc->pid, buffer, 10, 10)); + char copied = strcpy(out, itoa(proc->pid, buffer, 10, 10)); + out += copied; + out += addSpaces(out, MAX_ATTR_SIZE - copied); + written += MAX_ATTR_SIZE - copied; out += addSpaces(out, 2); + written += copied + 2; - buffer[0] = 0; - written += strcpy(out, itoa(proc->priority, buffer, 10, 2)); + // buffer = itoa(proc->priority, buffer, 10, 2); + copied = strcpy(out, itoa(proc->priority, buffer, 10, 2)); + out += copied; + out += addSpaces(out, MAX_ATTR_SIZE - copied); + written += MAX_ATTR_SIZE - copied; out += addSpaces(out, 2); + written += copied + 2; - buffer[0] = 0; - written += strcpy(out, itoa(proc->rsp, buffer, 16, 10)); + copied = strcpy(out, itoa(proc->rsp, buffer, 16, 10)); + out += copied; + out += addSpaces(out, MAX_NAME_SIZE - copied); + written += MAX_NAME_SIZE - copied; out += addSpaces(out, 2); + written += copied + 2; - buffer[0] = 0; - written += strcpy(out, itoa(proc->rbp, buffer, 16, 10)); + copied = strcpy(out, itoa(proc->rbp, buffer, 16, 10)); + out += copied; + out += addSpaces(out, MAX_NAME_SIZE - copied); + written += MAX_NAME_SIZE - copied; out += addSpaces(out, 2); + written += copied + 2; - buffer[0] = 0; - written += strcpy(out, (proc->foreground == 1) ? "F" : "B"); + copied = strcpy(out, (proc->foreground == 1) ? "F" : "B"); + out += copied; + out += addSpaces(out, MAX_ATTR_SIZE - copied); + written += MAX_ATTR_SIZE - copied; + out += addSpaces(out, 2); + written += copied + 2; // out += addSpaces(out, 2); + // *out++; + // *out = '\0'; return written; } char * processes(){ char * ans = pvPortMalloc(pids * PROCESS_DATA_MAX_SIZE); + char * ret = ans; - int lastPid = getPid(); - if (lastPid == EXIT_FAILURE) - return NULL; + char * info = "name pid prio rsp rbp fore\n"; + strcpy(ans, info); + ans += 56; + processCDT * aux = firstReady; - while (aux != NULL){ + while (aux != NULL) { char writtenChars = getProcessData(ans, aux); if (writtenChars == EXIT_FAILURE) return NULL; @@ -371,7 +398,8 @@ char * processes(){ aux = aux->next; } *ans = 0; - return ans; + + return ret; } /* diff --git a/Kernel/utils/semaphore.c b/Kernel/utils/semaphore.c index 1e355e2..90e6bc4 100644 --- a/Kernel/utils/semaphore.c +++ b/Kernel/utils/semaphore.c @@ -1,4 +1,8 @@ #include +#include +#include +#include "libc.h" +#include "scheduler.h" #define MAX_SEM 100 #define MAX_NAME 100 @@ -11,8 +15,15 @@ typedef struct sem { unsigned int value; char name[MAX_NAME]; // cola FIFO + pid_t * entering; + pid_t * last; } sem_t; +typedef struct pid_t { + int pid; + struct pid_t * next; +} pid_t; + // Podemos manejarnos con indices. static sem_t semaphores[MAX_SEM]; /* @@ -31,37 +42,63 @@ 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); - // + + sem_t * sem = pvPortMalloc(sizeof(sem_t)); + strcpy(sem->name, name); + sem->value = value; + leave_region(&semLock); } -int semClose(sem_t * semaphore) { +int semClose(sem_t * sem) { + if (sem == NULL) + return EXIT_FAILURE; + enter_region(&semLock); - // + + pid_t * pid = sem->entering; + while (pid != NULL) { + pid_t * aux = pid; + pid = pid->next; + vPortFree(aux); + } + vPortFree(sem); + leave_region(&semLock); } -int semWait(sem_t * semaphore) { +int semWait(sem_t * sem) { enter_region(&semLock); - if (semaphore->value > 0) { - semaphore->value--; + if (sem->value > 0) { + sem->value--; } else { - // leave_region(&semLock); - // enter_region(&semLock); - // semaphore->value--; + leave_region(&semLock); - // mandarlo a noni. es decir bloquearlo y solo despertarlo cuando alguien hace post - // block(con el pid) - // + pid_t * curr = pvPortMalloc(sizeof(pid_t)); + curr->pid = getPid(); + curr->next = NULL; + sem->last->next = curr; + sem->last = curr; + block(curr->pid); + + enter_region(&semLock); + sem->value--; } leave_region(&semLock); } -int semPost(sem_t * semaphore) { +int semPost(sem_t * sem) { enter_region(&semLock); - // + + sem->value++; + + pid_t * aux = sem->entering; + sem->entering = sem->entering->next; + unblock(sem->entering->pid); + pvPortFree(aux); + leave_region(&semLock); } \ No newline at end of file diff --git a/Userland/SampleCodeModule/asm/libasm.asm b/Userland/SampleCodeModule/asm/libasm.asm index e6dc7bd..7184d3c 100644 --- a/Userland/SampleCodeModule/asm/libasm.asm +++ b/Userland/SampleCodeModule/asm/libasm.asm @@ -3,7 +3,7 @@ GLOBAL _getMem, sys_loadProcess GLOBAL raiseOpcodeExc GLOBAL _getRegs, sys_switchContext GLOBAL cpu_id, cpu_id_support -GLOBAL sys_exit +GLOBAL sys_exit, sys_exec, sys_fork, sys_ps section .text @@ -70,6 +70,66 @@ sys_exit: pop rbp ret +sys_fork: + push rbp + mov rbp, rsp + + push rdi + push rsi + push rdx + + mov rdx, rbp + mov rsi, rsp + mov rdi, 3 + int 80h + + pop rdx + pop rsi + pop rdi + + mov rsp, rbp + pop rbp + ret + +sys_exec: + push rbp + mov rbp, rsp + + push rdi + push rsi + push rdx + push rcx + + mov rcx, rdx + mov rdx, rsi + mov rsi, rdi + mov rdi, 5 + int 80h + + pop rcx + pop rdx + pop rsi + pop rdi + + mov rsp, rbp + pop rbp + ret + +sys_ps: + push rbp + mov rbp, rsp + + push rdi + + mov rdi, 6 + int 80h + + pop rdi + + mov rsp, rbp + pop rbp + ret + _getMem: push rbp mov rbp, rsp diff --git a/Userland/SampleCodeModule/shell/commands/inforeg.c b/Userland/SampleCodeModule/shell/commands/inforeg.c index 779fda1..fed08ee 100644 --- a/Userland/SampleCodeModule/shell/commands/inforeg.c +++ b/Userland/SampleCodeModule/shell/commands/inforeg.c @@ -16,12 +16,12 @@ void inforeg(char * window, int * offset) { char bufferAux[20]; for (int i = 0; i < 17; i++) { - if (i % 3 == 0 && i != 0) + if (i % 5 == 0 && i != 0) substractLine(window, offset); addText(regsNames[i], window, offset); addText("0x", window, offset); addText(gtoa(regs[i], bufferAux, 16, 20), window, offset); - if (i != 14 && i % 3 != 2) + if (i != 14 && i % 5 != 4) addText(" - ", window, offset); } printWindow(window); diff --git a/Userland/SampleCodeModule/shell/commands/ps.c b/Userland/SampleCodeModule/shell/commands/ps.c new file mode 100644 index 0000000..21f3553 --- /dev/null +++ b/Userland/SampleCodeModule/shell/commands/ps.c @@ -0,0 +1,11 @@ +#include "libc.h" +#include "shell.h" + +char * sys_ps(); + +void ps(char * window, int * offset) { + char * output = sys_ps(); + addText(output, window, offset); + printWindow(window); + substractLine(window, offset); +} \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/include/ps.h b/Userland/SampleCodeModule/shell/include/ps.h new file mode 100644 index 0000000..0b1df69 --- /dev/null +++ b/Userland/SampleCodeModule/shell/include/ps.h @@ -0,0 +1,6 @@ +#ifndef PS_LIB +#define PS_LIB + +void ps(char * window, int * offset); + +#endif \ No newline at end of file diff --git a/Userland/SampleCodeModule/shell/shell.c b/Userland/SampleCodeModule/shell/shell.c index d4ead73..c82ed01 100644 --- a/Userland/SampleCodeModule/shell/shell.c +++ b/Userland/SampleCodeModule/shell/shell.c @@ -10,6 +10,7 @@ #include "quadratic.h" #include "cpu_id.h" #include "change.h" +#include "ps.h" #define SIZE 100 #define MAX_ARGS 5 @@ -18,8 +19,8 @@ #define ROWS 25 const int len = 8; -char *commands_void[] = {"help", "time", "inforeg", "excdiv", "excop", "clear", "cpufeatures", "change"}; -void (*func []) (char *, int *) = {help, time, inforeg, excdiv, excop, clear, cpufeatures, change}; +char *commands_void[] = {"help", "time", "inforeg", "excdiv", "excop", "clear", "cpufeatures", "ps"}; +void (*func []) (char *, int *) = {help, time, inforeg, excdiv, excop, clear, cpufeatures, ps}; void substractLine(char * window, int * offset) { for (int i = 0; i < ROWS - 1; i++) { @@ -36,7 +37,11 @@ void substractLine(char * window, int * offset) { void addText(char * buffer, char * window, int * offset) { while (*buffer != 0) { - if (*offset == ROWS * COLS - 1) substractLine(window, offset); + if (*offset == ROWS * COLS - 1) substractLine(window, offset); + if (*buffer == '\n') { + buffer++; + substractLine(window, offset); + } window[(*offset)++] = *buffer++; } }