Make a lot of changes in shell and fix some bugs

Co-authored-by: Ezequiel Bellver <ebellver@itba.edu.ar>
Co-authored-by: Juan Barmasch <jbarmasch@itba.edu.ar>
This commit is contained in:
Santiago Lo Coco 2021-10-26 22:18:19 -03:00
parent 86f6f72574
commit af241224c9
44 changed files with 570 additions and 421 deletions

View File

@ -30,6 +30,8 @@ GLOBAL loadProcess
GLOBAL _initialize_stack_frame
GLOBAL _switchContext
EXTERN getFPUaddress, getSSEaddress
SECTION .text
%macro pushState 0
@ -136,9 +138,44 @@ picSlaveMask:
pop rbp
retn
; getFPUaddress:
; push rcx
; mov ecx, [auxi]
; mov ecx, 1
; xor ecx, [auxi]
; cmp ecx, 0
; jne .getSecond
; mov rax, bytesForFPU1
; jmp .finish
; .getSecond:
; mov rax, bytesForFPU2
; .finish:
; mov dword [auxi], ecx
; pop rcx
; ret
; getSSEaddress:
; push rcx
; mov ecx, [auxi]
; cmp ecx, 0
; jne .getSecond
; mov rax, bytesForSSEAligned1
; jmp .finish
; .getSecond:
; mov rax, bytesForSSEAligned2
; .finish:
; pop rcx
; ret
;8254 Timer (Timer Tick)
_irq00Handler:
pushState
; push rax
; call getFPUaddress
; fsave [rax]
; call getSSEaddress
; fxsave [rax]
; pop rax
; fsave [bytesForFPU]
; fxsave [bytesForSSEAligned]
@ -156,9 +193,13 @@ _irq00Handler:
; fxrstor [bytesForSSEAligned]
; frstor [bytesForFPU]
; push rax
; call getFPUaddress
; frstor [rax]
; call getSSEaddress
; fxrstor [rax]
; pop rax
popState
; pop rsi ; argv
; pop rdi ; argc 6004d8
iretq
;Keyboard
@ -236,16 +277,24 @@ _initialize_stack_frame:
push 0x0 ; ss
push rsi ; sp
push 0x202 ; rflags
push 0x08 ; cs -- offset de la GDT
push 0x08 ; cs
push rdi ; IP
; push rdx ; argc
; push rcx ; argv
mov rsi, rcx
mov rdi, rdx
pushState
; mov rdi, rsp
; call newStack
; mov rax, rsp
; fsave [bytesForFPU]
; push rax
; mov dword [auxi], 1
; call getFPUaddress
; call getSSEaddress
; pop rax
; fxsave [bytesForSSEAligned]
; fsave [r8]
; fxsave [r9]
mov rsp, r10
ret
@ -262,41 +311,19 @@ _systemCallsHandler:
popStateNoRax
iretq
; switch Context (int 81h)
; _switchContext:
; pushState
;
; call changeWindow
; mov rdi, rsp
; call preserveStack
;
; mov rsp, rax
; popState
; pop rsi ; argv
; pop rdi ; argc
; iretq
; _switchContext:
; pushState
; mov rdi, rsp
; call updateRSP
; call nextProcess
; mov rsp, rax
; popState
; pop rsi ; argv
; pop rdi ; argc
; iretq
SECTION .data
align 16
bytesForSSEAligned times 512 db 0
; bytesForSSEAligned1 times 512 db 0
; bytesForSSEAligned2 times 512 db 0
; counter dd 1
SECTION .bss
aux resq 1
bytesForSSE resb 512
bytesForFPU resb 108
; bytesForFPU1 resb 108
; bytesForFPU2 resb 108
insPointer resb 8
rspPointer resb 8
rspPointer resb 8
; auxi resb 4

View File

@ -1,8 +1,8 @@
#include <stdint.h>
#include "video.h"
static uint8_t * const video = (uint8_t*)0xB8000;
static uint8_t * currentVideo = (uint8_t*)0xB8000;
static uint8_t * const video = (uint8_t*) 0xB8000;
static uint8_t * currentVideo = (uint8_t*) 0xB8000;
static const int width = 80;
static const int height = 25;
static int currentX = 0;
@ -11,39 +11,6 @@ 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];
// }
void increment() {
currentX++;
if (currentX >= limitX[1]) {
@ -89,7 +56,10 @@ int printStringLen(int color, const char * string, int maxLen) {
if (*string == '\n') {
new_line();
return i;
// return i;
string++;
i++;
continue;
}
else if (*string == '\b') {
backspace();
@ -109,9 +79,7 @@ int printStringLen(int color, const char * string, int maxLen) {
}
void backspace() {
if (currentVideo > video) {
currentVideo -= 2;
*currentVideo = ' ';
if (video + currentY * width * 2 + currentX * 2 > video) {
currentX--;
if (currentX < limitX[0]) {
currentX = limitX[1];
@ -119,6 +87,7 @@ void backspace() {
currentY--;
}
}
*(video + currentY * width * 2 + currentX * 2) = ' ';
}
void new_line() {

View File

@ -12,5 +12,6 @@ int abs(int value);
char * itoa(int value, char * buffer, int base, int length);
void strlen(const char *str, int *len);
int strcpy(char * strDest, const char * strSrc);
char addSpaces(char * str, char qty);
#endif

18
Kernel/include/pipe.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef PIPE_H
#define PIPE_H
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include "lib.h"
#include "scheduler.h"
#include "memManager.h"
#include "pipeLib.h"
#define MAX_SEM 100
#define SEM_NAME "Pipes"
int exists(pipe_t * pipe);
node_t * searchPipe(node_t ** previous, int fd);
#endif

26
Kernel/include/pipeLib.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef PIPELIB_H
#define PIPELIB_H
#include "semLib.h"
#define MAX_NAME 100
#define PIPE_MAX_SIZE 1024
typedef struct pipe_t {
int fd;
char buffer[PIPE_MAX_SIZE];
int current;
char name[MAX_NAME];
sem_t * sem;
} pipe_t;
typedef struct node_t {
pipe_t * pipe;
struct node_t * next;
} node_t;
pipe_t * openPipe(char * name);
void writePipe(int fd, char c);
char readPipe(int fd);
#endif

View File

@ -1,19 +1,18 @@
#ifndef SCHEDULER_H
#define SCHEDULER_H
#include "queue.h"
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include "memManager.h"
#include <stdlib.h>
#include "lib.h"
#include <stddef.h>
#include "schedulerLib.h"
void forceTimer();
void haltcpu();
#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 MIN_PRIORITY 0 // Minimum number for a priority (yet maximum level of priority)
#define DEF_PRIORITY 0
#define PROCESS_DATA_MAX_SIZE 100
#define MAX_ATTR_SIZE 6
@ -21,20 +20,10 @@ void haltcpu();
typedef struct processCDT * processADT;
uint64_t nextProcess();
void idle();
void initScheduler();
void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[]);
void newProcess(processADT process, char * name, char priority, char foreground, uint64_t rsp, uint64_t rbp);
char block(int pid);
char unblock(int pid);
char kill(int pid);
void exitProcess();
char nice(char offset);
uint64_t nextProcess();
void updateRSP(uint64_t newRsp);
int getPid();
char quitCPU();
char getProcessData(char * out, processADT proc);
char * processes();
#endif

View File

@ -0,0 +1,19 @@
#ifndef SCHEDULERLIB_H
#define SCHEDULERLIB_H
#include <stdint.h>
void initScheduler();
void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[]);
char block(int pid);
char unblock(int pid);
char kill(int pid);
void exitProcess();
char nice(char offset);
int getPid();
char quitCPU();
char * processes();
int getFdOut();
int getFdIn();
#endif

18
Kernel/include/sem.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef SEM_H
#define SEM_H
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include "lib.h"
#include "scheduler.h"
#include "memManager.h"
#include "semLib.h"
#define MAX_SEM 100
#define SEM_DATA_MAX_SIZE 100
void enter_region(uint32_t * lock);
void leave_region(uint32_t * lock);
#endif

23
Kernel/include/semLib.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef SEMLIB_H
#define SEMLIB_H
#define MAX_NAME 100
typedef struct pid_t {
int pid;
struct pid_t * next;
} pid_t;
typedef struct sem_t {
unsigned int value;
char name[MAX_NAME];
pid_t * entering;
pid_t * last;
} sem_t;
sem_t * semOpen(char * name, unsigned int value);
int semClose(sem_t * sem);
int semWait(sem_t * sem);
int semPost(sem_t * sem);
#endif

View File

@ -1,9 +0,0 @@
#ifndef SEM_H
#define SEM_H
sem_t * semOpen(char * name, unsigned int value);
int semClose(sem_t * sem);
int semWait(sem_t * sem);
int semPost(sem_t * sem);
#endif

View File

@ -3,7 +3,10 @@
#include "keyboard.h"
#include "time.h"
#include "pcb.h"
#include "pipeLib.h"
#include "schedulerLib.h"
#define STDIN 0
#define STDOUT 1
#define STDERR 2
@ -16,8 +19,14 @@ uint64_t write(uint64_t fd, uint64_t buffer, uint64_t length) {
if (fd == STDOUT)
color = STDOUT_COLOR;
else if (fd == STDERR)
color = STDERR_COLOR;
else {
fd = getFdOut();
int i = 0;
while (bufferAux[i] != '\0' && i++ <= length) {
writePipe(fd, bufferAux[i]);
}
return i;
}
return printStringLen(color, bufferAux, (int) length);
}
@ -26,17 +35,24 @@ uint64_t read(uint64_t fd, uint64_t buffer, uint64_t length) {
char * bufferAux = (char *) buffer;
int readBytes = 0;
while (length-- > 0) {
*bufferAux = getKeyFromBuffer();
if (*bufferAux == 0)
break;
readBytes++;
bufferAux++;
if (fd == STDIN) {
while (length-- > 0) {
*bufferAux = getKeyFromBuffer();
if (*bufferAux == 0)
break;
readBytes++;
bufferAux++;
}
}
else {
fd = getFdIn();
readBytes += strcpy(buffer, readPipe(fd));
}
return readBytes;
}
void createProcess(void (*fn)) {
newProcess(fn);
}
// void createProcess(void (*fn)) {
// newProcess(fn);
// }

View File

@ -66,6 +66,14 @@ char * reverse(char * buffer, int i, int j) {
int abs(int value) {
return value < 0 ? -value : value;
}
char addSpaces(char * str, char qty) {
char aux = qty;
while (qty-- > 0) {
*str++ = ' ';
}
return aux;
}
char * itoa(int value, char * buffer, int base, int length) {
if (base < 2 || base > 32) {

View File

@ -25,7 +25,9 @@ typedef unsigned long UBaseType_t;
#define configTOTAL_HEAP_SIZE 1024 * 1024 * 100
#define configAPPLICATION_ALLOCATED_HEAP 0
#define portBYTE_ALIGNMENT 8
// #define portBYTE_ALIGNMENT 16
#define portBYTE_ALIGNMENT_MASK ( 0x0007 ) // 8
// #define portBYTE_ALIGNMENT_MASK ( 0x000F ) // 16
#define pdFALSE ( ( BaseType_t ) 0 )
#define pdTRUE ( ( BaseType_t ) 1 )
#define portPOINTER_SIZE_TYPE uint32_t
@ -198,12 +200,12 @@ void *pvReturn = NULL;
else {
ncNewline();
ncPrint("MALLOC: ");
ncPrintDec(xFreeBytesRemaining);
ncPrint(" ");
ncPrintDec(xWantedSize);
ncPrint(" ");
ncPrintDec(configADJUSTED_HEAP_SIZE);
// ncPrint("MALLOC: ");
// ncPrintDec(xFreeBytesRemaining);
// ncPrint(" ");
// ncPrintDec(xWantedSize);
// ncPrint(" ");
// ncPrintDec(configADJUSTED_HEAP_SIZE);
ncNewline();
}
}

View File

@ -1,99 +1,65 @@
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include "lib.h"
#include "scheduler.h"
#include "memManager.h"
#include "semaphore.h"
#include "pipe.h"
#define MAX_SEM 100
#define MAX_NAME 100
#define PIPE_MAX_SIZE 1024
#define SEM_NAME "Pipes"
int fds = 2;
typedef struct pipe_t {
char buffer[PIPE_MAX_SIZE];
int current;
char name[MAX_NAME];
sem_t * sem;
} pipe_t;
typedef struct node_t {
pipe_t * pipe;
struct node_t * next;
} node_t;
node_t * first;
// char initPipes() {
// if ((sem = semOpen(SEM_NAME)) == NULL)
// return EXIT_FAILURE;
// return EXIT_SUCCESS;
// }
node_t * firstPipe;
pipe_t * openPipe(char * name) {
// semWait(sem);
pipe_t * pipe = pvPortMalloc(sizeof(pipe_t));
strcpy(pipe->name, name);
if ((pipe->sem = semOpen(SEM_NAME)) == NULL)
pipe->fd = fds++;
if ((pipe->sem = semOpen(SEM_NAME, 1)) == NULL)
return NULL;
return pipe;
// semPost(sem);
}
void writePipe(pipe_t * pipe, char c) {
if (!exists(pipe))
return;
void writePipe(int fd, char c) {
node_t * prev = NULL;
node_t * node = searchPipe(&prev, fd);
semWait(pipe->sem);
semWait(node->pipe->sem);
pipe->buffer[pipe->current++ % PIPE_MAX_SIZE] = c;
node->pipe->buffer[node->pipe->current++ % PIPE_MAX_SIZE] = c;
semPost(pipe->sem);
semPost(node->pipe->sem);
}
char readPipe(pipe_t * pipe) {
if (!exists(pipe))
return -1;
char readPipe(int fd) {
node_t * prev = NULL;
node_t * node = searchPipe(&prev, fd);
semWait(pipe->sem);
semWait(node->pipe->sem);
char c = pipe->buffer[pipe->current-- % PIPE_MAX_SIZE];
char c = node->pipe->buffer[node->pipe->current-- % PIPE_MAX_SIZE];
semPost(pipe->sem);
semPost(node->pipe->sem);
return c;
}
void closePipe(pipe_t * pipe) {
void closePipe(int fd) {
node_t * prev = NULL;
node_t * del = search(&prev, pipe);
node_t * del = searchPipe(&prev, fd);
if (del == NULL)
return;
semWait(pipe->sem);
semWait(del->pipe->sem);
if (prev != NULL)
prev->next = del->next;
else first->next = del->next;
else firstPipe->next = del->next;
vPortFree(pipe);
vPortFree(del->pipe);
vPortFree(del);
semPost(pipe->sem);
semPost(del->pipe->sem);
}
int exists(pipe_t * pipe) {
node_t * prev = NULL;
return search(&prev, pipe) != NULL;
}
node_t * search(node_t ** previous, pipe_t * pipe) {
node_t * curr = first;
node_t * searchPipe(node_t ** previous, int fd) {
node_t * curr = firstPipe;
* previous = NULL;
while (curr != NULL) {
if (curr->pipe == pipe) {
if (curr->pipe->fd == fd) {
break;
}
* previous = curr;
@ -103,7 +69,7 @@ node_t * search(node_t ** previous, pipe_t * pipe) {
* previous = NULL;
return NULL;
}
if (curr == first) {
if (curr == firstPipe) {
* previous = NULL;
return curr;
}

View File

@ -1,6 +1,7 @@
#include "scheduler.h"
#define IDLE_PID 1
// void _initialize_stack_frame(void *, void *, int, char**, void **, void **);
void _initialize_stack_frame(void *, void *, int, char**);
enum states {READY = 0, BLOCKED};
@ -17,6 +18,8 @@ typedef struct processCDT {
char foreground;
enum states state;
int * fd;
void * sseBytes;
void * fpuBytes;
} processCDT;
processCDT * firstReady = NULL;
@ -67,6 +70,7 @@ void initScheduler() {
// }
void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[]) {
// void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[], int * fd[2]) {
if (firstReady != NULL && firstReady->pid == IDLE_PID)
block(IDLE_PID);
@ -95,7 +99,10 @@ void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *
process->executions = 0;
process->foreground = foreground;
process->state = READY;
// process->sseBytes = pvPortMalloc(64);
// process->fpuBytes = pvPortMalloc(14);
// _initialize_stack_frame(fn, rbp, argc, argv, &(process->fpuBytes), &(process->sseBytes));
_initialize_stack_frame(fn, rbp, argc, argv);
if (firstReady == NULL)
@ -115,6 +122,14 @@ void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *
return;
}
void * getSSEaddress() {
return currentProcess->sseBytes;
}
void * getFPUaddress() {
return currentProcess->fpuBytes;
}
void newProcess(processADT process, char * name, char priority, char foreground, uint64_t rsp, uint64_t rbp) {
// char aux[MAX_NAME_SIZE];
// int j;
@ -139,7 +154,7 @@ void newProcess(processADT process, char * name, char priority, char foreground,
// exit();
// }
processADT search(processADT * previous, int pid, processADT first) {
processADT searchProcess(processADT * previous, int pid, processADT first) {
processADT curr = first;
* previous = NULL;
while (curr != NULL) {
@ -162,7 +177,7 @@ processADT search(processADT * previous, int pid, processADT first) {
char block(int pid) {
processADT prev = NULL;
processADT del = search(&prev, pid, firstReady);
processADT del = searchProcess(&prev, pid, firstReady);
if (del == NULL)
return EXIT_FAILURE;
else {
@ -192,7 +207,7 @@ char block(int pid) {
char unblock(int pid) {
processADT prev = NULL;
processADT del = search(&prev, pid, firstBlocked);
processADT del = searchProcess(&prev, pid, firstBlocked);
if (del == NULL)
return EXIT_FAILURE;
else {
@ -213,9 +228,9 @@ char unblock(int pid) {
char kill(int pid) {
processADT prev = NULL;
processADT del = search(&prev, pid, firstReady);
processADT del = searchProcess(&prev, pid, firstReady);
if (del == NULL) {
del = search(&prev, pid, firstBlocked);
del = searchProcess(&prev, pid, firstBlocked);
if (del == NULL)
return EXIT_FAILURE;
else {
@ -246,6 +261,18 @@ char kill(int pid) {
return EXIT_SUCCESS;
}
int getFdOut() {
if (currentProcess == NULL)
return NULL;
return currentProcess->fd[1];
}
int getFdIn() {
if (currentProcess == NULL)
return NULL;
return currentProcess->fd[0];
}
void exitProcess() {
kill(currentProcess->pid);
}
@ -280,14 +307,6 @@ char quitCPU() {
return EXIT_SUCCESS;
}
char addSpaces(char * str, char qty) {
char aux = qty;
while (qty-- > 0) {
*str++ = ' ';
}
return aux;
}
char getGenProcessData(char ** out, char * written, char toAdd, char * in, char isLast) {
char copied = strcpy(*out, in);
*out += copied;

View File

@ -1,37 +1,14 @@
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include "lib.h"
#include "scheduler.h"
#include "memManager.h"
#include "sem.h"
#define MAX_SEM 100
#define MAX_NAME 100
#define SEM_DATA_MAX_SIZE 100
void enter_region(uint32_t * lock);
void leave_region(uint32_t * lock);
static uint32_t semLock;
typedef struct pid_t {
int pid;
struct pid_t * next;
} pid_t;
typedef struct sem_t {
unsigned int value;
char name[MAX_NAME];
pid_t * entering;
pid_t * last;
} sem_t;
typedef struct node_t {
sem_t * sem;
struct node_t * next;
} node_t;
static sem_t semaphores[MAX_SEM];
node_t * first = NULL;
node_t * firstSem = NULL;
static char counter = 0;
sem_t * semOpen(char * name, unsigned int value) {
@ -40,8 +17,8 @@ sem_t * semOpen(char * name, unsigned int value) {
sem_t * sem = pvPortMalloc(sizeof(sem_t));
node_t * node = pvPortMalloc(sizeof(node_t));
node->sem = sem;
node->next = first;
first = node;
node->next = firstSem;
firstSem = node;
strcpy(sem->name, name);
sem->value = value;
counter++;
@ -57,12 +34,12 @@ int semClose(sem_t * sem) {
node_t * del = NULL;
if (first == sem) {
del = first;
first = first->next;
if (firstSem == sem) {
del = firstSem;
firstSem = firstSem->next;
}
else {
node_t * aux = first;
node_t * aux = firstSem;
while (aux != NULL) {
if (aux->next != NULL)
if (aux->next->sem == sem) {
@ -176,7 +153,7 @@ char * getSems() {
ans += strcpy(ans, info);
// ans += 56;
node_t * aux = first;
node_t * aux = firstSem;
while (aux != NULL) {
char writtenChars = getSemaphoresData(ans, aux);
if (writtenChars == EXIT_FAILURE)

View File

@ -8,7 +8,9 @@ void printString(char * string);
void printStringError(char * string);
void printStringLen(char * string, int len);
void new_line();
void backspace();
char getChar();
void putChar(char c);
int abs();
int atoi(char * buffer, int len);
int strlen(const char * s);

View File

@ -30,13 +30,17 @@ int strcmp(const char * s1, const char * s2) {
void putChar(char c){
char buffer = c;
sys_write(1, &buffer, 1);
sys_write(1, &buffer, 0);
}
void new_line() {
putChar('\n');
}
void backspace() {
putChar('\b');
}
void winClear() {
sys_write(1, "\e\f", 2);
}

View File

@ -1,6 +0,0 @@
#include "system.h"
#include "change.h"
void change() {
sys_switchContext();
}

View File

@ -2,6 +2,6 @@
#include "shell.h"
#include "clear.h"
void clear(char * window, int * offset) {
clearWindow(window, offset);
void clear() {
winClear();
}

View File

@ -1,5 +1,4 @@
#include "cpu_id.h"
#include "shell.h"
int cpu_id(int *, int *, int *, int *);
@ -101,12 +100,11 @@ static const int len = 16;
int (* checks[])() = {check_cpuid, check_mmx, check_sse, check_sse2, check_sse3, check_sse41, check_sse42, check_aes, check_pclmulqdq, check_avx, check_vaesni, check_vpclmulqdq, check_f16c, check_fma, check_avx2, check_fpu};
char * supports[] = {"cpuid_support", "mx_support", "sse_support", "sse2_support", "sse3_support", "sse41_support", "sse42_support", "aes_support", "pclmulqdq_support", "avx_support", "vaesni_support", "vpclmulqdq_support", "f16c_support", "fma_support", "avx2_support", "fpu_support"};
void cpufeatures(char * window, int * offset) {
void cpufeatures() {
for (int i = 0; i < len; i++) {
if (checks[i]()) {
addText(supports[i], window, offset);
substractLine(window, offset);
printString(supports[i]);
new_line();
}
}
printWindow(window);
}

View File

@ -1,13 +1,12 @@
#include "libc.h"
#include "shell.h"
static char * info[] = {"change: Change the current working terminal (alternatively, press ALT+F1)", "clear: Clear the shell window", "cpufeatures: Show the features the cpu supports", "excdiv: Throw exception 0 (zero division)", "excop: Throw exception 6 (opcode error)", "help: Print information about commands", "inforeg: Print values of registers","printmem: Prints 32 bytes of memory, starting at a given direction", "quadratic: Receives 3 floats, quadratic coefficients, prints roots if real", "time: Prints date and time in UTC" };
static const int len = 10;
static char * info[] = {"clear: Clear the shell window", "cpufeatures: Show the features the cpu supports", "excdiv: Throw exception 0 (zero division)", "excop: Throw exception 6 (opcode error)", "help: Print information about commands", "inforeg: Print values of registers","printmem: Prints 32 bytes of memory, starting at a given direction", "quadratic: Receives 3 floats, quadratic coefficients, prints roots if real", "time: Prints date and time in UTC" };
static const int len = 9;
void help(char * window, int * offset) {
void help() {
for (int i = 0; i < len; i++) {
addText(info[i], window, offset);
substractLine(window, offset);
printString(info[i]);
new_line();
}
printWindow(window);
}

View File

@ -10,20 +10,19 @@ static char * regsNames[] = {
"R8: ", "R9: ", "R10: ", "R11: ", "R12: ", "R13: ", "R14: ", "R15: ",
"RSP: ", "RIP: "};
void inforeg(char * window, int * offset) {
void inforeg() {
uint64_t * regs = (uint64_t *) _getRegs();
char bufferAux[20];
for (int i = 0; i < 17; i++) {
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);
new_line();
printString(regsNames[i]);
printStringLen("0x", 2);
printString(gtoa(regs[i], bufferAux, 16, 20));
if (i != 14 && i % 5 != 4)
addText(" - ", window, offset);
printStringLen(" - ", 3);
}
printWindow(window);
substractLine(window, offset);
new_line();
}

View File

@ -13,16 +13,15 @@ void getMem(int * buffer, int initialAddress) {
}
}
void printmem(char * window, int * offset, long dir) {
void printmem(long dir) {
int buffer[8];
getMem(buffer, dir);
for (int i = 0; i < 8; i++) {
char bufferAux[8];
addText("0x", window, offset);
addText(itoa(buffer[i], bufferAux, 16), window, offset);
substractLine(window, offset);
printStringLen("0x", 2);
printString(itoa(buffer[i], bufferAux, 16));
new_line();
}
printWindow(window);
}

View File

@ -1,10 +1,9 @@
#include "libc.h"
#include "shell.h"
void ps(char * window, int * offset) {
void ps() {
char * output = sys_ps();
addText(output, window, offset);
printWindow(window);
substractLine(window, offset);
sys_free(output);
printString(output);
new_line();
// sys_free(output);
}

View File

@ -2,41 +2,36 @@
#include "quadratic.h"
#include "shell.h"
void quadratic(char * window, int * offset, double a, double b, double c) {
void quadratic(double a, double b, double c) {
double sol1, sol2;
if (a == 0) {
addText("First argument cannot be 0", window, offset);
printWindow(window);
substractLine(window, offset);
printStringLen("First argument cannot be 0\n", 28);
return;
}
int cond = quadSolver(a, b, c, &sol1, &sol2);
if (cond == 0) {
addText("Not real!", window, offset);
printWindow(window);
substractLine(window, offset);
printStringLen("Not real!\n", 11);
return;
}
char buffer[30] = {0};
addText("Roots for ", window, offset);
printStringLen("Roots for ", 11);
ftoa(a, buffer, 10);
addText(buffer, window, offset);
addText("x^2 + ", window, offset);
printString(buffer);
printStringLen("x^2 + ", 7);
ftoa(b, buffer, 10);
addText(buffer, window, offset);
addText("x + ", window, offset);
printString(buffer);
printString("x + ");
ftoa(c, buffer, 10);
addText(buffer, window, offset);
substractLine(window, offset);
printString(buffer);
new_line();
ftoa(sol1, buffer, 10);
addText(buffer, window, offset);
addText(" and ", window, offset);
printString(buffer);
printStringLen(" and ", 6);
char buffer2[30] = {0};
ftoa(sol2, buffer2, 10);
addText(buffer2, window, offset);
printWindow(window);
substractLine(window, offset);
printString(buffer2);
new_line();
}

View File

@ -1,10 +0,0 @@
#include "libc.h"
#include "shell.h"
void sem(char * window, int * offset) {
char * output = sys_sem();
addText(output, window, offset);
printWindow(window);
substractLine(window, offset);
sys_free(output);
}

View File

@ -0,0 +1,9 @@
#include "libc.h"
#include "shell.h"
void sem() {
char * output = sys_sem();
printString(output);
new_line();
// sys_free(output);
}

View File

@ -27,26 +27,24 @@ int getYear() {
return sys_time(YEAR);
}
void printTime(int number, char * window, int * offset) {
void printTime(int number) {
char buffer[3] = {0};
buffer[0] = number / 10 + '0';
buffer[1] = number % 10 + '0';
buffer[2] = '\0';
addText(buffer, window, offset);
printString(buffer);
}
void time(char * window, int * offset) {
printTime(getDay(), window, offset);
addText("/", window, offset);
printTime(getMonth(), window, offset);
addText("/", window, offset);
printTime(getYear(), window, offset);
addText(" ", window, offset);
printTime(getHours(), window, offset);
addText(":", window, offset);
printTime(getMinutes(), window, offset);
addText(":", window, offset);
printTime(getSeconds(), window, offset);
printWindow(window);
substractLine(window, offset);
printTime(getDay());
putChar('/');
printTime(getMonth());
putChar('/');
printTime(getYear());
putChar(' ');
printTime(getHours());
putChar(':');
printTime(getMinutes());
putChar(':');
printTime(getSeconds());
}

View File

@ -1,6 +0,0 @@
#ifndef CHANGE_H
#define CHANGE_H
void change();
#endif

View File

@ -1,6 +1,6 @@
#ifndef CLEAR_LIB
#define CLEAR_LIB
void clear(char * window, int * offset);
#ifndef CLEAR_LIB
#define CLEAR_LIB
void clear();
#endif

View File

@ -1,5 +1,8 @@
#ifndef CPUID
#define CPUID
#ifndef CPUID_H
#define CPUID_H
#include "libc.h"
#include "shell.h"
// Tabla obtenidad de: https://wiki.osdev.org/CPUID
enum {
@ -68,7 +71,7 @@ enum {
};
void cpufeatures(char * window, int * offset);
void cpufeatures();
int cpu_id_support(void);
#endif

View File

@ -1,6 +1,6 @@
#ifndef HELP_LIB
#define HELP_LIB
void help(char * window, int * offset);
void help();
#endif

View File

@ -1,6 +1,6 @@
#ifndef REG_LIB
#define REG_LIB
void inforeg(char*, int*);
void inforeg();
#endif

View File

@ -1,6 +1,6 @@
#ifndef MEM_LIB
#define MEM_LIB
void printmem(char* window, int* offset, long);
void printmem(long);
#endif

View File

@ -1,6 +1,6 @@
#ifndef PS_LIB
#define PS_LIB
void ps(char * window, int * offset);
void ps();
#endif

View File

@ -2,6 +2,6 @@
#define QUAD_LIB
int quadSolver(double, double, double, double *, double *);
void quadratic(char*, int*, double, double, double);
void quadratic(double, double, double);
#endif

View File

@ -1,6 +0,0 @@
#ifndef SEM_LIB
#define SEM_LIB
void sem(char * window, int * offset);
#endif

View File

@ -0,0 +1,6 @@
#ifndef SEM_LIB
#define SEM_LIB
void sem();
#endif

View File

@ -4,11 +4,7 @@
#include "system.h"
void shell(int argc, char *argv[]);
void printWindow(char * window);
void addText(char * buffer, char * window, int * offset);
void incorrect_comm(char * buffer, char * window, int * offset);
void incorrect_arg(char * command, char * window, int * offset);
void clearWindow(char * window, int * offset);
void substractLine(char * window, int * offset);
void incorrect_comm(char * buffer);
void incorrect_arg(char * command);
#endif

View File

@ -7,7 +7,7 @@ int getHours();
int getDays();
int getMonths();
int getYears();
void time(char * window, int * offset);
void time();
#define SECONDS 0
#define MINUTES 2

View File

@ -0,0 +1,158 @@
/*
#include "libc.h"
#include "help.h"
#include "clear.h"
#include "time.h"
#include "shell.h"
#include "inforeg.h"
#include "printmem.h"
#include "excDiv.h"
#include "excOP.h"
#include "quadratic.h"
#include "cpu_id.h"
#include "change.h"
#include "ps.h"
#include "semCom.h"
#define SIZE 100
#define MAX_ARGS 5
#define COLS 80
#define ROWS 25
const int len = 9;
char *commands_void[] = {"help", "time", "inforeg", "excdiv", "excop", "clear", "cpufeatures", "ps", "sem"};
void (*func []) (char *, int *) = {help, time, inforeg, excdiv, excop, clear, cpufeatures, ps, semCom};
void substractLine(char * window, int * offset) {
for (int i = 0; i < ROWS - 1; i++) {
for (int j = 0; j < COLS; j++) {
window[i * COLS + j] = window[(i + 1) * COLS + j];
}
}
for (int i = 0; i < COLS; i++) {
window[(ROWS - 1) * COLS + i - 1] = ' ';
window[ROWS * COLS] = 0;
}
*offset = (ROWS - 1) * COLS;
}
void addText(char * buffer, char * window, int * offset) {
while (*buffer != 0) {
if (*offset == ROWS * COLS - 1) substractLine(window, offset);
if (*buffer == '\n') {
buffer++;
substractLine(window, offset);
}
window[(*offset)++] = *buffer++;
}
}
void printWindow(char * window) {
printStringLen(window, ROWS * COLS);
}
void scanfNoPrint(char * buffer, int maxSize, char * window, int * offset) {
char c;
int i = 0;
while ((c = getChar()) != '\n' && i < maxSize - 1) {
if (c != -1) {
// if (c == '\v')
// sys_switchContext();
// else if (c == '\b' && i > 0) {
if (c == '\b' && i > 0) {
buffer[--i] = ' ';
window[--(*offset)] = ' ';
printWindow(window);
}
else if (c != 0 && c != '\b') {
buffer[i++] = c;
if (*offset == ROWS * COLS - 1) substractLine(window, offset);
window[(*offset)++] = c;
printWindow(window);
}
}
}
buffer[i] = '\0';
if (*offset == ROWS * COLS - 1) substractLine(window, offset);
window[*offset] = ' ';
}
void clearWindow(char * window, int * offset) {
for (int i = 0; i <= ROWS * COLS; i++) {
window[i] = ' ';
}
window[ROWS * COLS] = 0;
*offset = (ROWS - 1) * COLS;
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;
printWindow(window);
while (1) {
int comm_flag = 0;
addText("$> ", window, &offset);
printWindow(window);
char buffer[SIZE] = {0};
scanfNoPrint(buffer, SIZE, window, &offset);
substractLine(window, &offset);
char* tokens[SIZE] = {0};
tokens[0] = strstrip(buffer, ' ');
for (int i = 1; i < MAX_ARGS; i++) {
tokens[i] = strtok(tokens[i - 1], ' ');
}
for (int i = 0; i < len; i++) {
if (!strcmp(tokens[0], commands_void[i])) {
if (*tokens[1] != 0)
incorrect_arg(tokens[0], window, &offset);
else
(*func[i])(window, &offset);
comm_flag = 1;
}
}
if (!strcmp(tokens[0], "quadratic")) {
if (*tokens[4] != 0 || *tokens[3] == 0)
incorrect_arg(tokens[0], window, &offset);
else if (!isFloat(tokens[1]) || !isFloat(tokens[2]) || !isFloat(tokens[3]))
incorrect_arg(tokens[0], window, &offset);
else
quadratic(window, &offset, atof(tokens[1]), atof(tokens[2]), atof(tokens[3]));
comm_flag = 1;
}
if (!strcmp(tokens[0], "printmem")) {
if (*tokens[2] != 0 || *tokens[1] == 0)
incorrect_arg(tokens[0], window, &offset);
else {
int length = strlen(tokens[1]);
printmem(window, &offset, atoi(tokens[1], length));
}
comm_flag = 1;
}
if (!comm_flag) {
if (*tokens[0] != 0)
incorrect_comm(tokens[0], window, &offset);
}
}
}
void incorrect_comm(char * buffer, char* window, int * offset) {
addText(buffer, window, offset);
addText(" is not a BottlerShell command", window, offset);
printWindow(window);
substractLine(window, offset);
}
void incorrect_arg(char * command, char* window, int * offset) {
addText("Incorrect arguments for command ", window, offset);
addText(command, window, offset);
printWindow(window);
substractLine(window, offset);
}
*/

View File

@ -1,17 +1,16 @@
#include "libc.h"
#include "help.h"
#include "clear.h"
#include "time.h"
#include "shell.h"
#include "clear.h"
#include "inforeg.h"
#include "printmem.h"
#include "excDiv.h"
#include "excOP.h"
#include "quadratic.h"
#include "cpu_id.h"
#include "change.h"
#include "ps.h"
#include "sem.h"
#include "semCom.h"
#define SIZE 100
#define MAX_ARGS 5
@ -21,86 +20,33 @@
const int len = 9;
char *commands_void[] = {"help", "time", "inforeg", "excdiv", "excop", "clear", "cpufeatures", "ps", "sem"};
void (*func []) (char *, int *) = {help, time, inforeg, excdiv, excop, clear, cpufeatures, ps, sem};
void (*func []) () = {help, time, inforeg, excdiv, excop, clear, cpufeatures, ps, sem};
void substractLine(char * window, int * offset) {
for (int i = 0; i < ROWS - 1; i++) {
for (int j = 0; j < COLS; j++) {
window[i * COLS + j] = window[(i + 1) * COLS + j];
}
}
for (int i = 0; i < COLS; i++) {
window[(ROWS - 1) * COLS + i - 1] = ' ';
window[ROWS * COLS] = 0;
}
*offset = (ROWS - 1) * COLS;
}
void addText(char * buffer, char * window, int * offset) {
while (*buffer != 0) {
if (*offset == ROWS * COLS - 1) substractLine(window, offset);
if (*buffer == '\n') {
buffer++;
substractLine(window, offset);
}
window[(*offset)++] = *buffer++;
}
}
void printWindow(char * window) {
printStringLen(window, ROWS * COLS);
}
void scanfNoPrint(char * buffer, int maxSize, char * window, int * offset) {
void scanfNoPrint(char * buffer) {
char c;
int i = 0;
while ((c = getChar()) != '\n' && i < maxSize - 1) {
while ((c = getChar()) != '\n' && i < SIZE - 1) {
if (c != -1) {
// if (c == '\v')
// sys_switchContext();
// else if (c == '\b' && i > 0) {
if (c == '\b' && i > 0) {
buffer[--i] = ' ';
window[--(*offset)] = ' ';
printWindow(window);
backspace();
}
else if (c != 0 && c != '\b') {
buffer[i++] = c;
if (*offset == ROWS * COLS - 1) substractLine(window, offset);
window[(*offset)++] = c;
printWindow(window);
putChar(c);
}
}
}
buffer[i] = '\0';
if (*offset == ROWS * COLS - 1) substractLine(window, offset);
window[*offset] = ' ';
}
void clearWindow(char * window, int * offset) {
for (int i = 0; i <= ROWS * COLS; i++) {
window[i] = ' ';
}
window[ROWS * COLS] = 0;
*offset = (ROWS - 1) * COLS;
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;
printWindow(window);
while (1) {
int comm_flag = 0;
addText("$> ", window, &offset);
printWindow(window);
printStringLen("$> ", 3);
char buffer[SIZE] = {0};
scanfNoPrint(buffer, SIZE, window, &offset);
substractLine(window, &offset);
scanfNoPrint(buffer);
new_line();
char* tokens[SIZE] = {0};
tokens[0] = strstrip(buffer, ' ');
for (int i = 1; i < MAX_ARGS; i++) {
@ -109,48 +55,45 @@ void shell(int argc, char *argv[]) {
for (int i = 0; i < len; i++) {
if (!strcmp(tokens[0], commands_void[i])) {
if (*tokens[1] != 0)
incorrect_arg(tokens[0], window, &offset);
incorrect_arg(tokens[0]);
else
(*func[i])(window, &offset);
(*func[i])();
comm_flag = 1;
}
}
if (!strcmp(tokens[0], "quadratic")) {
if (*tokens[4] != 0 || *tokens[3] == 0)
incorrect_arg(tokens[0], window, &offset);
incorrect_arg(tokens[0]);
else if (!isFloat(tokens[1]) || !isFloat(tokens[2]) || !isFloat(tokens[3]))
incorrect_arg(tokens[0], window, &offset);
incorrect_arg(tokens[0]);
else
quadratic(window, &offset, atof(tokens[1]), atof(tokens[2]), atof(tokens[3]));
quadratic(atof(tokens[1]), atof(tokens[2]), atof(tokens[3]));
comm_flag = 1;
}
if (!strcmp(tokens[0], "printmem")) {
if (*tokens[2] != 0 || *tokens[1] == 0)
incorrect_arg(tokens[0], window, &offset);
incorrect_arg(tokens[0]);
else {
int length = strlen(tokens[1]);
printmem(window, &offset, atoi(tokens[1], length));
printmem(atoi(tokens[1], length));
}
comm_flag = 1;
}
if (!comm_flag) {
if (*tokens[0] != 0)
incorrect_comm(tokens[0], window, &offset);
incorrect_comm(tokens[0]);
}
}
}
void incorrect_comm(char * buffer, char* window, int * offset) {
addText(buffer, window, offset);
addText(" is not a BottlerShell command", window, offset);
printWindow(window);
substractLine(window, offset);
void incorrect_comm(char * buffer) {
printString(buffer);
printStringLen(" is not a BottlerShell command\n", 32);
}
void incorrect_arg(char * command, char* window, int * offset) {
addText("Incorrect arguments for command ", window, offset);
addText(command, window, offset);
printWindow(window);
substractLine(window, offset);
void incorrect_arg(char * command) {
printStringLen("Incorrect arguments for command ", 33);
printString(command);
new_line();
}