Add phylo and make some changes (but with 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-28 23:36:17 -03:00
parent 7ae51a4849
commit 953f608b4b
32 changed files with 979 additions and 132 deletions

View File

@ -28,9 +28,10 @@ EXTERN nextProcess
GLOBAL switchContext GLOBAL switchContext
GLOBAL loadProcess GLOBAL loadProcess
GLOBAL _initialize_stack_frame GLOBAL _initialize_stack_frame
GLOBAL _switchContext GLOBAL _switchContext, forceTimerAux
EXTERN getFPUaddress, getSSEaddress EXTERN getFPUaddress, getSSEaddress
EXTERN checkSleeping
SECTION .text SECTION .text
@ -109,6 +110,40 @@ SECTION .text
iretq iretq
%endmacro %endmacro
%macro pushStateNoRax 0
push rbx
push rcx
push rdx
push rbp
push rdi
push rsi
push r8
push r9
push r10
push r11
push r12
push r13
push r14
push r15
%endmacro
%macro popStateNoRax 0
pop r15
pop r14
pop r13
pop r12
pop r11
pop r10
pop r9
pop r8
pop rsi
pop rdi
pop rbp
pop rdx
pop rcx
pop rbx
%endmacro
_hlt: _hlt:
sti sti
hlt hlt
@ -167,18 +202,29 @@ picSlaveMask:
; pop rcx ; pop rcx
; ret ; ret
forceTimerAux:
pushStateNoRax
; call checkSleeping
call nextProcess
mov rsp, rax
popStateNoRax
iretq
;8254 Timer (Timer Tick) ;8254 Timer (Timer Tick)
_irq00Handler: _irq00Handler:
pushState pushState
mov rsi, rsp ; mov rsi, rsp
and rsp, -16 ; and rsp, -16
sub rsp, 108 ; sub rsp, 108
fsave [rsp] ; fsave [rsp]
and rsp, -16 ; and rsp, -16
sub rsp, 512 ; sub rsp, 512
fxsave [rsp] ; fxsave [rsp]
push rsi ; push rsi
; push rax ; push rax
; call getFPUaddress ; call getFPUaddress
@ -189,11 +235,14 @@ _irq00Handler:
; fsave [bytesForFPU] ; fsave [bytesForFPU]
; fxsave [bytesForSSEAligned] ; fxsave [bytesForSSEAligned]
mov rdi, 0 call checkSleeping
call irqDispatcher
mov rdi, rsp mov rdi, rsp
call updateRSP call updateRSP
mov r10, rax
mov rdi, 0
call irqDispatcher
call nextProcess call nextProcess
mov rsp, rax mov rsp, rax
@ -210,18 +259,24 @@ _irq00Handler:
; fxrstor [rax] ; fxrstor [rax]
; pop rax ; pop rax
pop rsp ; cmp r10, 0
mov rax, rsp ; je .end
and rsp, -16
sub rsp, 108 ; pop rsp
frstor [rsp] ; mov rax, rsp
and rsp, -16 ; and rsp, -16
sub rsp, 512 ; sub rsp, 108
fxrstor [rsp] ; frstor [rsp]
mov rsp, rax ; and rsp, -16
; sub rsp, 512
; fxrstor [rsp]
; mov rsp, rax
popState popState
iretq iretq
; .end:
; popStateNoRax
; iretq
;Keyboard ;Keyboard
_irq01Handler: _irq01Handler:
@ -257,39 +312,6 @@ haltcpu:
cli cli
ret ret
%macro pushStateNoRax 0
push rbx
push rcx
push rdx
push rbp
push rdi
push rsi
push r8
push r9
push r10
push r11
push r12
push r13
push r14
push r15
%endmacro
%macro popStateNoRax 0
pop r15
pop r14
pop r13
pop r12
pop r11
pop r10
pop r9
pop r8
pop rsi
pop rdi
pop rbp
pop rdx
pop rcx
pop rbx
%endmacro
_initialize_stack_frame: _initialize_stack_frame:
mov r10, rsp mov r10, rsp
@ -306,15 +328,15 @@ _initialize_stack_frame:
pushState pushState
mov rsi, rsp ; mov rsi, rsp
and rsp, -16 ; and rsp, -16
sub rsp, 108 ; sub rsp, 108
fsave [rsp] ; fsave [rsp]
and rsp, -16 ; and rsp, -16
sub rsp, 512 ; sub rsp, 512
fxsave [rsp] ; fxsave [rsp]
push rsi ; push rsi
mov rax, rsp ; mov rax, rsp
; fsave [bytesForFPU] ; fsave [bytesForFPU]
; fxsave [bytesForSSEAligned] ; fxsave [bytesForSSEAligned]
@ -335,13 +357,39 @@ _initialize_stack_frame:
; System calls (int 80h) ; System calls (int 80h)
_systemCallsHandler: _systemCallsHandler:
pushStateNoRax pushStateNoRax
fsave [bytesForFPU] ; fsave [bytesForFPU]
fxsave [bytesForSSEAligned] ; fxsave [bytesForSSEAligned]
; mov [auxRSI], rsi
; mov rsi, rsp
; and rsp, -16
; sub rsp, 108
; fsave [rsp]
; and rsp, -16
; sub rsp, 512
; fxsave [rsp]
; push rsi
; mov [auxRDI], rdi
; mov rdi, rsp
; call updateRSP
; mov rsi, [auxRSI]
; mov rdi, [auxRDI]
call systemCallsDispatcher call systemCallsDispatcher
fxrstor [bytesForSSEAligned] ; fxrstor [bytesForSSEAligned]
frstor [bytesForFPU] ; frstor [bytesForFPU]
; pop rsp
; mov rax, rsp
; and rsp, -16
; sub rsp, 108
; frstor [rsp]
; and rsp, -16
; sub rsp, 512
; fxrstor [rsp]
; mov rsp, rax
popStateNoRax popStateNoRax
iretq iretq
@ -360,4 +408,6 @@ SECTION .bss
; bytesForFPU2 resb 108 ; bytesForFPU2 resb 108
insPointer resb 8 insPointer resb 8
rspPointer resb 8 rspPointer resb 8
auxRSI resb 8
auxRDI resb 8
; auxi resb 4 ; auxi resb 4

View File

@ -6,9 +6,7 @@ forceTimer:
int 20h int 20h
ret ret
; tiene que ser uint32_t el parametro, no soporta más xchg. aunq también podría ser menos
enter_region: enter_region:
;push eax no haría falta pues lo podemos perder...
mov eax, 1 mov eax, 1
xchg eax, [rdi] xchg eax, [rdi]
cmp eax, 0 cmp eax, 0

View File

@ -35,9 +35,43 @@ int getTime(char option) {
// TODO // TODO
void wait(long seconds) { void wait(long seconds) {
// int initialSeconds = getTime(SECONDS); int initialSeconds = getTimeGen(SECONDS);
int initialMinutes = getTimeGen(MINUTES);
int runSeconds = 0;
if (initialSeconds + seconds >= 60) {
}
int newSeconds = 0;
while ((newSeconds = getTimeGen(SECONDS) - initialSeconds) < seconds)
haltcpu();
// while (initialSeconds - seconds != initialSeconds) { // while (initialSeconds - seconds != initialSeconds) {
// } // }
// while (seconds_elapsed() < seconds); // while (seconds_elapsed() < seconds);
} }
static long getTimeFrom2000() {
long years = 00 * 31557600;
long months = 1 * 2629800;
long days = 1 * 86400;
long aux = years + months + days;
return years + months + days;
}
long getTimeOfDay() {
// long years = getTimeGen(YEAR) - 1970;
// obtengo los segundos actuales (sumando todas las variables) y los resto por los de 1970?
long years = getTimeGen(YEAR) * 31557600;
long months = getTimeGen(MONTH) * 2629800;
long days = getTimeGen(DAY) * 86400;
long hours = getTimeGen(HOURS) * 3600;
long minutes = getTimeGen(MINUTES) * 60;
long seconds = getTimeGen(SECONDS);
long total = years + months + days + hours + minutes + seconds;
return total - getTimeFrom2000();
}

View File

@ -4,6 +4,7 @@
#include "memManager.h" #include "memManager.h"
#include <stdlib.h> #include <stdlib.h>
#include "lib.h" #include "lib.h"
#include "time.h"
#include <stddef.h> #include <stddef.h>
#include "schedulerLib.h" #include "schedulerLib.h"
@ -23,7 +24,7 @@ typedef struct processCDT * processADT;
void idle(); void idle();
void newProcess(processADT process, char * name, char priority, char foreground, uint64_t rsp, uint64_t rbp); void newProcess(processADT process, char * name, char priority, char foreground, uint64_t rsp, uint64_t rbp);
uint64_t nextProcess(); uint64_t nextProcess();
void updateRSP(uint64_t newRsp); char updateRSP(uint64_t newRsp);
char getProcessData(char * out, processADT proc); char getProcessData(char * out, processADT proc);
#endif #endif

View File

@ -4,7 +4,7 @@
#include <stdint.h> #include <stdint.h>
void initScheduler(); void initScheduler();
void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[], int * fd); int enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[], int * fd);
char block(int pid); char block(int pid);
char unblock(int pid); char unblock(int pid);
char kill(int pid); char kill(int pid);
@ -15,5 +15,7 @@ char quitCPU();
char * processes(); char * processes();
int getFdOut(); int getFdOut();
int getFdIn(); int getFdIn();
void checkSleeping();
char unblockFirst(int pid);
#endif #endif

View File

@ -15,5 +15,6 @@ int seconds_elapsed();
int getTimeGen(char option); int getTimeGen(char option);
int getTime(char option); int getTime(char option);
void wait(long seconds); void wait(long seconds);
long getTimeOfDay();
#endif #endif

View File

@ -5,9 +5,11 @@
void exitProcess(); void exitProcess();
// void setFn(uint64_t, uint64_t, uint64_t); // void setFn(uint64_t, uint64_t, uint64_t);
char * processes(); char * processes();
void enqueueProcess(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t); // int enqueueProcess(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t);
char openPipe(int *, char *); char openPipe(int *, char *);
char nice(int, char); char nice(int, char);
#include "semLib.h"
#include "schedulerLib.h"
uint64_t systemCallsDispatcher(uint64_t rdi, uint64_t rsi, uint64_t rdx, uint64_t rcx, uint64_t r8, uint64_t r9) { uint64_t systemCallsDispatcher(uint64_t rdi, uint64_t rsi, uint64_t rdx, uint64_t rcx, uint64_t r8, uint64_t r9) {
switch (rdi) { switch (rdi) {
@ -18,9 +20,7 @@ uint64_t systemCallsDispatcher(uint64_t rdi, uint64_t rsi, uint64_t rdx, uint64_
case 2: case 2:
return getTime(rsi, rdx, rcx); return getTime(rsi, rdx, rcx);
case 3: case 3:
// createProcess(rsi); return enqueueProcess(rsi, rdx, rcx, r8, r9);
enqueueProcess(rsi, rdx, rcx, r8, r9);
break;
case 4: case 4:
exitProcess(); exitProcess();
break; break;
@ -32,7 +32,7 @@ uint64_t systemCallsDispatcher(uint64_t rdi, uint64_t rsi, uint64_t rdx, uint64_
case 7: case 7:
return (uint64_t) getSems(); return (uint64_t) getSems();
case 8: case 8:
return pvPortMalloc((void *) rsi); return pvPortMalloc((size_t) rsi);
case 9: case 9:
vPortFree((void *) rsi); vPortFree((void *) rsi);
break; break;
@ -40,6 +40,28 @@ uint64_t systemCallsDispatcher(uint64_t rdi, uint64_t rsi, uint64_t rdx, uint64_
return (uint64_t) openPipe(rsi, rdx); return (uint64_t) openPipe(rsi, rdx);
case 11: case 11:
return nice(rsi, rdx); return nice(rsi, rdx);
case 12:
sleep(rsi);
break;
case 13:
semWait((sem_t *) rsi);
break;
case 14:
semPost((sem_t *) rsi);
break;
case 15:
return semOpen(rsi, rdx);
case 16:
kill(rsi);
break;
case 17:
return getPid();
case 18:
return semClose((sem_t *) rsi);
case 19:
return block(rsi);
case 20:
return unblock(rsi);
default: default:
return -1; return -1;
} }

View File

@ -6,7 +6,8 @@ loader:
call initializeKernelBinary ; Set up the kernel binary, and get thet stack address call initializeKernelBinary ; Set up the kernel binary, and get thet stack address
mov rsp, rax ; Set up the stack with the returned address mov rsp, rax ; Set up the stack with the returned address
call main call main
hang:
cli ; hang:
hlt ; halt machine should kernel return ; cli
jmp hang ; hlt ; halt machine should kernel return
; jmp hang

View File

@ -22,7 +22,7 @@ typedef unsigned long UBaseType_t;
#define configSUPPORT_STATIC_ALLOCATION 1 #define configSUPPORT_STATIC_ALLOCATION 1
#define configSUPPORT_DYNAMIC_ALLOCATION 1 #define configSUPPORT_DYNAMIC_ALLOCATION 1
#define configTOTAL_HEAP_SIZE 1024 * 1024 * 100 #define configTOTAL_HEAP_SIZE 1024 * 1024 * 512
#define configAPPLICATION_ALLOCATED_HEAP 0 #define configAPPLICATION_ALLOCATED_HEAP 0
#define portBYTE_ALIGNMENT 8 #define portBYTE_ALIGNMENT 8
// #define portBYTE_ALIGNMENT 16 // #define portBYTE_ALIGNMENT 16

View File

@ -37,6 +37,13 @@ typedef struct processCDT {
// void * fpuBytes; // void * fpuBytes;
} processCDT; } processCDT;
typedef struct sleepCDT {
int pid;
long time;
long secs;
struct sleepCDT * next;
} sleepCDT;
// typedef union processCDT { // typedef union processCDT {
// struct { // struct {
// struct p * next; // struct p * next;
@ -60,16 +67,20 @@ processCDT * firstReady = NULL;
processCDT * lastReady = NULL; processCDT * lastReady = NULL;
processCDT * firstBlocked = NULL; processCDT * firstBlocked = NULL;
processCDT * lastBlocked = NULL; processCDT * lastBlocked = NULL;
sleepCDT * firstSleep = NULL;
static processCDT * currentProcess = NULL; static processCDT * currentProcess = NULL;
static int pids = IDLE_PID; static int pids = IDLE_PID;
static char update = 1; static char update = 1;
static char idleFlag = 2;
uint64_t nextProcess() { uint64_t nextProcess() {
update = 1; update = 1;
if (currentProcess == NULL) { if (currentProcess == NULL) {
if (firstReady == NULL) if (firstReady == NULL) {
idleFlag = 2;
unblock(IDLE_PID); unblock(IDLE_PID);
}
currentProcess = firstReady; currentProcess = firstReady;
return firstReady->rsp; return firstReady->rsp;
@ -94,7 +105,7 @@ void idle() {
} }
void initScheduler() { void initScheduler() {
char * argv[] = {"Idle"}; char * argv[] = {"idle"};
enqueueProcess(idle, 0, 1, argv, NULL); enqueueProcess(idle, 0, 1, argv, NULL);
} }
@ -104,7 +115,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[]) {
void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[], int * fd) { int enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[], int * fd) {
if (fd == NULL) { if (fd == NULL) {
int * aux = pvPortMalloc(2 * sizeof(int)); int * aux = pvPortMalloc(2 * sizeof(int));
aux[0] = 0; aux[0] = 0;
@ -148,7 +159,8 @@ void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *
// _initialize_stack_frame(fn, rbp, argc, argv, &(process->fpuBytes), &(process->sseBytes)); // _initialize_stack_frame(fn, rbp, argc, argv, &(process->fpuBytes), &(process->sseBytes));
// _initialize_stack_frame(fn, rbp, argc, argv, &(process->bytes->s.sseBytes), &(process->bytes->s.fpuBytes)); // _initialize_stack_frame(fn, rbp, argc, argv, &(process->bytes->s.sseBytes), &(process->bytes->s.fpuBytes));
// _initialize_stack_frame(fn, rbp, argc, argv, process->bytes->s.sseBytes, process->bytes->s.fpuBytes); // _initialize_stack_frame(fn, rbp, argc, argv, process->bytes->s.sseBytes, process->bytes->s.fpuBytes);
process->rsp = _initialize_stack_frame(fn, rbp, argc, argv); // process->rsp = _initialize_stack_frame(fn, rbp, argc, argv);
_initialize_stack_frame(fn, rbp, argc, argv);
if (firstReady == NULL) if (firstReady == NULL)
firstReady = process; firstReady = process;
@ -156,7 +168,59 @@ void enqueueProcess(void (*fn) (int, char **), char foreground, int argc, char *
lastReady->next = process; lastReady->next = process;
lastReady = process; lastReady = process;
return process->pid;
}
void removeProcess(processCDT * del, processCDT * prev, processCDT ** first, processCDT ** last) {
if (prev == NULL) {
*first = del->next;
if (*last == del)
*last = NULL;
}
else {
prev->next = del->next;
if (*last == del)
*last = prev;
}
}
void sleep(int secs) {
if (currentProcess == NULL)
return; return;
sleepCDT * proc = pvPortMalloc(sizeof(sleepCDT));
proc->pid = currentProcess->pid;
proc->secs = secs;
proc->time = getTimeOfDay();
proc->next = firstSleep;
firstSleep = proc;
block(currentProcess->pid);
}
void wakeUp(sleepCDT * wake, sleepCDT * prev) {
if (wake == firstSleep)
firstSleep = wake->next;
else {
prev->next = wake->next;
}
unblockFirst(wake->pid);
vPortFree(wake);
}
void checkSleeping() {
sleepCDT * prev = NULL;
sleepCDT * aux = firstSleep;
while(aux != NULL) {
if (getTimeOfDay() >= aux->time + aux->secs) {
wakeUp(aux, prev);
aux = prev->next;
}
else {
prev = aux;
aux = aux->next;
}
}
} }
// void * getSSEaddress() { // void * getSSEaddress() {
@ -195,10 +259,17 @@ char block(int pid) {
if (del == NULL) if (del == NULL)
return EXIT_FAILURE; return EXIT_FAILURE;
else { else {
if (prev != NULL) removeProcess(del, prev, &firstReady, &lastReady);
prev->next = del->next; // if (prev != NULL) {
else // prev->next = del->next;
firstReady = del->next; // if (lastReady == del)
// lastReady = prev;
// }
// else {
// firstReady = del->next;
// if (del == lastReady)
// lastReady = NULL;
// }
} }
processCDT * next = del->next; processCDT * next = del->next;
@ -225,18 +296,71 @@ char unblock(int pid) {
if (del == NULL) if (del == NULL)
return EXIT_FAILURE; return EXIT_FAILURE;
else { else {
if (prev != NULL) removeProcess(del, prev, &firstBlocked, &lastBlocked);
prev->next = del->next; // if (prev != NULL) {
else // prev->next = del->next;
firstBlocked = del->next; // if (del == lastBlocked)
// lastBlocked = prev;
// }
// else {
// firstBlocked = del->next;
// if (lastBlocked == del)
// lastBlocked = NULL;
// }
}
del->next = NULL; del->next = NULL;
if (lastReady != NULL) if (lastReady != NULL)
lastReady->next = del; lastReady->next = del;
else else
firstReady = del; firstReady = del;
lastReady = del; lastReady = del;
// if (firstReady != NULL && firstReady->pid == IDLE_PID && lastReady->pid != IDLE_PID)
if (idleFlag && !(--idleFlag))
block(IDLE_PID);
return EXIT_SUCCESS;
} }
char unblockFirst(int pid) {
processADT prev = NULL;
processADT del = searchProcess(&prev, pid, firstBlocked);
if (del == NULL)
return EXIT_FAILURE;
else {
removeProcess(del, prev, &firstBlocked, &lastBlocked);
// if (prev != NULL) {
// prev->next = del->next;
// if (lastBlocked == del)
// lastBlocked = prev;
// }
// else {
// firstBlocked = del->next;
// if (lastBlocked == del)
// lastBlocked = NULL;
// }
}
if (currentProcess != NULL) {
del->next = currentProcess->next;
currentProcess->next = del;
if (lastReady == currentProcess)
lastReady = del;
}
else {
if (firstReady != NULL)
del->next = firstReady->next;
else del->next = NULL;
firstReady = del;
if (lastReady == NULL)
lastReady = del;
}
// if (firstReady != NULL && lastReady->pid == IDLE_PID && firstReady->pid != IDLE_PID)
if (idleFlag && !(--idleFlag))
block(IDLE_PID);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -248,23 +372,25 @@ char kill(int pid) {
if (del == NULL) if (del == NULL)
return EXIT_FAILURE; return EXIT_FAILURE;
else { else {
if (prev != NULL) { removeProcess(del, prev, &firstBlocked, &lastBlocked);
prev->next = del->next; // if (prev != NULL) {
if (del == lastBlocked) // prev->next = del->next;
lastBlocked = prev; // if (del == lastBlocked)
} // lastBlocked = prev;
else // }
firstBlocked = del->next; // else
// firstBlocked = del->next;
} }
} }
else { else {
if (prev != NULL) { removeProcess(del, prev, &firstReady, &lastReady);
prev->next = del->next; // if (prev != NULL) {
if (del == lastReady) // prev->next = del->next;
lastReady = prev; // if (del == lastReady)
} // lastReady = prev;
else // }
firstReady = del->next; // else
// firstReady = del->next;
} }
processCDT * next = del->next; processCDT * next = del->next;
@ -317,12 +443,13 @@ char nice(int pid, char offset) {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
void updateRSP(uint64_t newRsp) { char updateRSP(uint64_t newRsp) {
if (currentProcess == NULL) { if (currentProcess == NULL) {
return; return -1;
} }
if (update) if (update)
currentProcess->rsp = newRsp; currentProcess->rsp = newRsp;
return update;
} }
int getPid() { int getPid() {

View File

@ -6,12 +6,14 @@ SOURCES=$(wildcard *.c)
SOURCES_SHELL=$(wildcard shell/*.c) SOURCES_SHELL=$(wildcard shell/*.c)
SOURCES_ASM=$(wildcard asm/*.asm) SOURCES_ASM=$(wildcard asm/*.asm)
SOURCES_PROMPT=$(wildcard shell/commands/*.c) SOURCES_PROMPT=$(wildcard shell/commands/*.c)
SOURCES_BOTTLER=$(wildcard bottler/*.c)
OBJECTS=$(SOURCES:.c=.o) OBJECTS=$(SOURCES:.c=.o)
OBJECTS_SHELL=$(SOURCES_SHELL:.c=.o) OBJECTS_SHELL=$(SOURCES_SHELL:.c=.o)
OBJECTS_PROMPT=$(SOURCES_PROMPT:.c=.o) OBJECTS_PROMPT=$(SOURCES_PROMPT:.c=.o)
OBJECTS_BOTTLER=$(SOURCES_BOTTLER:.c=.o)
OBJECTS_ASM=$(SOURCES_ASM:.asm=.o) OBJECTS_ASM=$(SOURCES_ASM:.asm=.o)
ALL_OBJECTS= $(OBJECTS) $(OBJECTS_ASM) $(OBJECTS_SHELL) $(OBJECTS_COMMANDS) $(OBJECTS_PROMPT) ALL_OBJECTS= $(OBJECTS) $(OBJECTS_ASM) $(OBJECTS_SHELL) $(OBJECTS_COMMANDS) $(OBJECTS_PROMPT) $(OBJECTS_BOTTLER)
STATICLIBS= STATICLIBS=
@ -21,13 +23,13 @@ all: $(MODULE) $(MODULE_ELF) #shellModule
# cd shell; make # cd shell; make
$(MODULE): $(STATICLIBS) $(ALL_OBJECTS) $(MODULE): $(STATICLIBS) $(ALL_OBJECTS)
$(LD) $(LDFLAGS) -T sampleCodeModule.ld $(OBJECTS) $(OBJECTS_SHELL) $(OBJECTS_ASM) $(OBJECTS_PROMPT) -o ../$(MODULE) $(LD) $(LDFLAGS) -T sampleCodeModule.ld $(OBJECTS) $(OBJECTS_SHELL) $(OBJECTS_ASM) $(OBJECTS_PROMPT) $(OBJECTS_BOTTLER) -o ../$(MODULE)
$(MODULE_ELF): $(STATICLIBS) $(ALL_OBJECTS) $(MODULE_ELF): $(STATICLIBS) $(ALL_OBJECTS)
$(LD) $(LDFLAGS) -I./include -I./shell/include -T sampleCodeModule.ld --oformat=elf64-x86-64 $(OBJECTS) $(OBJECTS_SHELL) $(OBJECTS_ASM) $(OBJECTS_PROMPT) -o ../$(MODULE_ELF) $(LD) $(LDFLAGS) -I./include -I./shell/include -I./bottler/include -T sampleCodeModule.ld --oformat=elf64-x86-64 $(OBJECTS) $(OBJECTS_SHELL) $(OBJECTS_ASM) $(OBJECTS_PROMPT) $(OBJECTS_BOTTLER) -o ../$(MODULE_ELF)
%.o:%.c %.o:%.c
$(GCC) $(GCCFLAGS) -T _loader.c -I./include -I./shell/include -c $< -o $@ $(GCC) $(GCCFLAGS) -T _loader.c -I./include -I./shell/include -I./bottler/include -c $< -o $@
%.o:%.asm %.o:%.asm
$(ASM) $(ASMFLAGS) $< -o $@ $(ASM) $(ASMFLAGS) $< -o $@

View File

@ -3,7 +3,8 @@ GLOBAL _getMem, sys_loadProcess
GLOBAL raiseOpcodeExc GLOBAL raiseOpcodeExc
GLOBAL _getRegs, sys_switchContext GLOBAL _getRegs, sys_switchContext
GLOBAL cpu_id, cpu_id_support GLOBAL cpu_id, cpu_id_support
GLOBAL sys_exit, sys_ps, sys_free, sys_malloc, sys_sem, sys_openPipe, sys_nice GLOBAL sys_exit, sys_ps, sys_free, sys_malloc, sys_sem, sys_openPipe, sys_semClose
GLOBAL sys_nice, sys_semWait, sys_semPost, sys_semOpen, sys_sleep, sys_kill, sys_getPid, sys_block, sys_unblock
section .text section .text
@ -81,10 +82,48 @@ sys_exit:
mov rbp, rsp mov rbp, rsp
push rdi push rdi
push rsi
mov rdi, 4 mov rdi, 4
int 80h int 80h
pop rsi
pop rdi
mov rsp, rbp
pop rbp
ret
sys_block:
push rbp
mov rbp, rsp
push rdi
push rsi
mov rsi, rdi
mov rdi, 19
int 80h
pop rsi
pop rdi
mov rsp, rbp
pop rbp
ret
sys_unblock:
push rbp
mov rbp, rsp
push rdi
push rsi
mov rsi, rdi
mov rdi, 20
int 80h
pop rsi
pop rdi pop rdi
mov rsp, rbp mov rsp, rbp
@ -96,10 +135,13 @@ sys_malloc:
mov rbp, rsp mov rbp, rsp
push rdi push rdi
push rsi
mov rsi, rdi
mov rdi, 8 mov rdi, 8
int 80h int 80h
pop rsi
pop rdi pop rdi
mov rsp, rbp mov rsp, rbp
@ -199,6 +241,21 @@ sys_sem:
pop rbp pop rbp
ret ret
sys_getPid:
push rbp
mov rbp, rsp
push rdi
mov rdi, 17
int 80h
pop rdi
mov rsp, rbp
pop rbp
ret
sys_nice: sys_nice:
push rbp push rbp
mov rbp, rsp mov rbp, rsp
@ -220,6 +277,117 @@ sys_nice:
pop rbp pop rbp
ret ret
sys_semOpen:
push rbp
mov rbp, rsp
push rdi
push rsi
push rdx
mov rdx, rsi
mov rsi, rdi
mov rdi, 15
int 80h
pop rdx
pop rsi
pop rdi
mov rsp, rbp
pop rbp
ret
sys_semWait:
push rbp
mov rbp, rsp
push rdi
push rsi
mov rsi, rdi
mov rdi, 13
int 80h
pop rsi
pop rdi
mov rsp, rbp
pop rbp
ret
sys_semPost:
push rbp
mov rbp, rsp
push rdi
push rsi
mov rsi, rdi
mov rdi, 14
int 80h
pop rsi
pop rdi
mov rsp, rbp
pop rbp
ret
sys_semClose:
push rbp
mov rbp, rsp
push rdi
push rsi
mov rsi, rdi
mov rdi, 18
int 80h
pop rsi
pop rdi
mov rsp, rbp
pop rbp
ret
sys_sleep:
push rbp
mov rbp, rsp
push rdi
push rsi
mov rsi, rdi
mov rdi, 12
int 80h
pop rsi
pop rdi
mov rsp, rbp
pop rbp
ret
sys_kill:
push rbp
mov rbp, rsp
push rdi
push rsi
mov rsi, rdi
mov rdi, 16
int 80h
pop rsi
pop rdi
mov rsp, rbp
pop rbp
ret
_getMem: _getMem:
push rbp push rbp
mov rbp, rsp mov rbp, rsp

View File

@ -0,0 +1,41 @@
#include "bottler.h"
void bottler(int argc, char ** argv) {
printStringLen(" ", 80); new_line();
printStringLen(" (%( ", 80); new_line();
printStringLen( " Welcome to", 17);
printStringLen(" %%%%% ", 80); new_line();
printStringLen( " BottlerOS", 18);
printStringLen(" %%% ", 80); new_line();
printStringLen( " %%%%%%%%%%%%% ", 80); new_line();
printStringLen( " %%%%%%%%%%%%%%%%%%%%% ", 80); new_line();
printStringLen( " %%%%%%% %%%%%%% ", 80); new_line();
printStringLen( " %%%%% %%%%% ", 80); new_line();
printStringLen( " %%%%% %%%%% ", 80); new_line();
printStringLen( " %%%%% ", 27);
printStringLen( " %%%% %%%% ", 22);
printStringLen( " %%%%% ", 30); new_line();
printStringLen( " %%%%% ", 28);
printStringLen( " (% %( ", 21);
printStringLen( " %%%%% ", 30); new_line();
printStringLen( " %%%%%%% %%%%%%% ", 80); new_line();
printStringLen(" %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ", 80); new_line();
printStringLen(" %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ", 80); new_line();
printStringLen(" %%%%%%%%%%%%%%%%%%%%%%% ", 41);
printStringLen( " % %* ", 8);
printStringLen(" %%%%%%%%% ", 30); new_line();
printStringLen(" %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ", 80); new_line();
printStringLen(" %%**%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/%% ", 80); new_line();
printStringLen(" %%* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% /%% ", 80); new_line();
printStringLen(" %%* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% /%% ", 80); new_line();
printStringLen(" %%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% ", 80); new_line();
printStringLen( " ,%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%. ", 80); new_line();
printStringLen( " %%. %%%%%%%%%%%%%%%%% .%% ", 80); new_line();
printStringLen( " %%%%%%%%%%%%% ", 80); new_line();
printStringLen( " %%%%%%% ", 80); new_line();
printStringLen( " ", 80); new_line();
// sys_sleep(3);
winClear();
sys_exit();
}

View File

@ -0,0 +1,9 @@
#ifndef BOTTLER_LIB
#define BOTTLER_LIB
#include "libc.h"
#include "system.h"
void bottler(int argc, char *argv[]);
#endif

View File

@ -26,5 +26,7 @@ char * reverse(char * buffer, int i, int j);
int pow(int base, int exponent); int pow(int base, int exponent);
char * strstrip(char * s, char c); char * strstrip(char * s, char c);
void ftoa(double f, char * buf, int precision); void ftoa(double f, char * buf, int precision);
char * strcat(char * dest, const char * src);
int strcpy(char * strDest, const char * strSrc);
#endif #endif

View File

@ -1,8 +1,21 @@
#ifndef SYSTEM_H #ifndef SYSTEM_H
#define SYSTEM_H #define SYSTEM_H
typedef struct pid_t {
int pid;
struct pid_t * next;
} pid_t;
typedef struct sem_t {
unsigned int value;
// char name[MAX_NAME];
char * name;
pid_t * entering;
pid_t * last;
} sem_t;
// void sys_switchContext(); // void sys_switchContext();
void sys_loadProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[], int * fd); int sys_loadProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[], int * fd);
int sys_time(char); int sys_time(char);
void sys_exit(); void sys_exit();
void sys_write(int, char *, int); void sys_write(int, char *, int);
@ -13,5 +26,16 @@ void * sys_malloc(int);
void * sys_free(void *); void * sys_free(void *);
void * sys_openPipe(int *, char *); void * sys_openPipe(int *, char *);
char sys_nice(int, char); char sys_nice(int, char);
void sys_sleep(int);
// void sys_semWait(void *);
void sys_semWait(sem_t *);
void sys_semPost(sem_t *);
// void * sys_semOpen(char *, unsigned int);
sem_t * sys_semOpen(char *, unsigned int);
int sys_getPid();
char sys_semClose(void *);
void sys_kill(int pid);
void sys_block(int pid);
void sys_unblock(int pid);
#endif #endif

View File

@ -54,6 +54,14 @@ void winClear() {
sys_write(1, "\e\f", 2); sys_write(1, "\e\f", 2);
} }
int strcpy(char * strDest, const char * strSrc) {
int i = 0;
while (*strDest++ = *strSrc++) {
i++;
}
return i;
}
char* gtoa(int value, char* buffer, int base, int length) { char* gtoa(int value, char* buffer, int base, int length) {
if (base < 2 || base > 32) { if (base < 2 || base > 32) {
return buffer; return buffer;

View File

@ -1,16 +1,21 @@
/* sampleCodeModule.c */ /* sampleCodeModule.c */
#include "libc.h" #include "libc.h"
#include "shell/include/shell.h" #include "shell/include/shell.h"
#include "bottler/include/bottler.h"
#include <stddef.h> #include <stddef.h>
void sys_loadProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[], int * fd); int sys_loadProcess(void (*fn) (int, char **), char foreground, int argc, char *argv[], int * fd);
void sys_exit(); void sys_exit();
// void sys_switchContext(); // void sys_switchContext();
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
winClear(); winClear();
char * argv2[] = {"BottlerSh"}; char * argv1[] = {"bottler"};
sys_loadProcess(bottler, 1, 1, argv1, NULL);
// sys_sleep(4);
char * argv2[] = {"shell"};
sys_loadProcess(shell, 1, 1, argv2, NULL); sys_loadProcess(shell, 1, 1, argv2, NULL);
// sys_loadProcess(shell); // sys_loadProcess(shell);

View File

@ -0,0 +1,12 @@
#include "block.h"
void block(int argc, char ** argv) {
if (argc != 2) {
printStringLen("block receives a pid\n", 21);
sys_exit();
}
int pid = atoi(argv[1], MAX_LEN);
sys_block(pid);
sys_exit();
}

View File

@ -0,0 +1,12 @@
#include "kill.h"
void kill(int argc, char ** argv) {
if (argc != 2) {
printStringLen("kill receives a pid\n", 21);
sys_exit();
}
int pid = atoi(argv[1], MAX_LEN);
sys_kill(pid);
sys_exit();
}

View File

@ -0,0 +1,13 @@
#include "loop.h"
#define PUAN -1
void loop(int argc, char *argv[]) {
int pid = sys_getPid(), secs = atoi(argv[1], PUAN);
char * str = "Hola, soy el proceso ";
char buffer[MAX_LEN];
str = strcat(str, itoa(pid, buffer, 10));
while (1) {
printString(str);
sys_sleep(secs);
}
}

View File

@ -1,8 +1,10 @@
#include "nice.h" #include "nice.h"
void nice(int argc, char ** argv) { void nice(int argc, char ** argv) {
if (argc != 3) if (argc != 3) {
printStringLen("nice receives a pid and an offset\n", 35); printStringLen("nice receives a pid and an offset\n", 35);
sys_exit();
}
int pid = atoi(argv[1], MAX_LEN); int pid = atoi(argv[1], MAX_LEN);
char offset = (char) atoi(argv[2], MAX_LEN); char offset = (char) atoi(argv[2], MAX_LEN);

View File

@ -0,0 +1,240 @@
#include "phylo.h"
#include <stddef.h>
#include <stdint.h>
#define ARGV_SIZE 2
#define BUFF_SIZE 20
#define MAX_PHILO_SIZE 3
#define MAX_NAME_SIZE 10
#define STARTING 2
// typedef uint64_t sem_t;
// typedef struct pid_t {
// int pid;
// struct pid_t * next;
// } pid_t;
// typedef struct sem_t {
// unsigned int value;
// // char name[MAX_NAME];
// char * name;
// pid_t * entering;
// pid_t * last;
// } sem_t;
int * state;
typedef enum states {EATING = 0, HUNGRY, THINKING} states;
typedef struct philosopher_t {
int debug;
char ** argv;
char * buffer;
sem_t * sem;
int pid;
states state;
struct philosopher_t * left;
struct philosopher_t * right;
} philosopher_t;
philosopher_t * firstPhil;
sem_t * mutex;
int philoCount = STARTING;
void printState() {
philosopher_t * phil = firstPhil;
do {
if (phil->state == EATING)
putChar('E');
else putChar('.');
phil = phil->right;
} while (phil != firstPhil);
putChar('\n');
}
void test(philosopher_t * phil)
{
if (phil->state == HUNGRY && phil->left->state != EATING && phil->right->state != EATING) {
phil->state = EATING;
// sys_sleep(2);
sys_semPost(phil->sem);
}
}
void philosopher(int argc, char ** argv);
void addPhilo() {
philoCount++;
philosopher_t * new = sys_malloc(sizeof(philosopher_t *));
new->argv = sys_malloc(sizeof(char *) * ARGV_SIZE);
new->buffer = sys_malloc(sizeof(char ) * BUFF_SIZE);
new->state = THINKING;
new->sem = sys_semOpen("filosofo", 0);
new->argv[0] = "filosofo";
new->argv[1] = itoa((uint64_t) new, new->argv[1], 10);
new->left = firstPhil->left;
new->right = firstPhil;
firstPhil->left->right = new;
firstPhil->left = new;
new->pid = sys_loadProcess(philosopher, 1, 2, new->argv, NULL);
}
void removePhilo() {
philoCount--;
philosopher_t * del = firstPhil->left;
del->left->right = del->right;
del->right->left = del->left;
sys_semClose(del->sem);
sys_free(del->buffer);
sys_free(del->argv);
sys_free(del);
sys_kill(del->pid);
}
void take_fork(philosopher_t * phil) {
sys_semWait(mutex);
phil->state = HUNGRY;
test(phil);
sys_semPost(mutex);
sys_semWait(phil->sem);
// sys_sleep(1);
}
void put_fork(philosopher_t * phil) {
sys_semWait(mutex);
phil->state = THINKING;
test(phil->left);
test(phil->right);
sys_semPost(mutex);
}
void philosopher(int argc, char ** argv)
{
if (argc != 2) {
sys_exit();
}
philosopher_t * i = (philosopher_t *) ((uint64_t) atoi(argv[1], -1));
while (1) {
// sys_sleep(1);
take_fork(i);
// printState();
// sys_sleep(1);
put_fork(i);
// printState();
}
sys_exit();
}
void phylo(int argc, char ** argv) {
if (argc == 1)
philoCount = STARTING;
else if (argc == 2) {
philoCount = atoi(argv[1], -1);
if (philoCount < 1 || philoCount > MAX_PHILO_SIZE) {
printStringLen("amount of philosofers must be between 1 and 10\n", 48);
sys_exit();
}
}
else sys_exit();
mutex = sys_semOpen("Mutex", 1);
philosopher_t * left = NULL;
for (int i = 0; i < philoCount; i++) {
philosopher_t * phil = sys_malloc(sizeof(philosopher_t));
phil->debug = i;
phil->argv = sys_malloc(sizeof(char *) * 2);
phil->buffer = sys_malloc(20);
phil->state = THINKING;
phil->sem = sys_semOpen("filosofo", 0);
phil->argv[0] = "filosofo";
// phil->argv[1] = itoa((uint64_t) phil, phil->argv[1], 10);
strcpy(phil->buffer, itoa((uint64_t) phil, phil->buffer, 10));
phil->argv[1] = phil->buffer;
if (left != NULL) {
phil->left = left;
phil->left->right = phil;
}
if (firstPhil == NULL) {
firstPhil = phil;
}
left = phil;
}
firstPhil->left = left;
firstPhil->left->right = firstPhil;
philosopher_t * phil = firstPhil;
do {
phil->pid = sys_loadProcess(philosopher, 1, 2, phil->argv, NULL);
phil = phil->right;
} while (phil != firstPhil);
// char c;
// while (1) {
// while ((c = getChar()) != 0 && c != -1) {
// if (c == 'a') {
// addPhilo();
// }
// else if (c == 'r') {
// removePhilo();
// }
// else if (c == 'q') {
// break;
// }
// }
// }
// phil = firstPhil;
// do {
// sys_semClose(phil);
// sys_free(phil);
// sys_free(phil);
// sys_free(phil);
// sys_kill(phil->pid);
// phil = phil->right;
// } while (phil != firstPhil); // PROBLEMA
sys_exit();
}
// La primera es muy similar a:
/*
* Taken from "Operating Systems - Design and Implementation" by Tanenbaum
* Section 2.3, page 91, Figure 2-10
* https://gist.github.com/codelance/4186161
*/

View File

@ -17,7 +17,7 @@ void getMem(int * buffer, int initialAddress) {
void printmem(int argc, char *argv[]) { void printmem(int argc, char *argv[]) {
if (argc != 2) { if (argc != 2) {
printStringLen("printmem receives a memory position\n", 37); printStringLen("printmem receives a memory position\n", 37);
return; sys_exit();
} }
int buffer[8]; int buffer[8];

View File

@ -6,20 +6,20 @@
void quadratic(int argc, char *argv[]) { void quadratic(int argc, char *argv[]) {
if (argc != 4) { if (argc != 4) {
printStringLen("quadratic receives 3 doubles\n", 30); printStringLen("quadratic receives 3 doubles\n", 30);
return; sys_exit();
} }
double sol1, sol2, a = atof(argv[1]), b = atof(argv[2]), c = atof(argv[3]); double sol1, sol2, a = atof(argv[1]), b = atof(argv[2]), c = atof(argv[3]);
if (a == 0) { if (a == 0) {
printStringLen("First argument cannot be 0\n", 28); printStringLen("First argument cannot be 0\n", 28);
return; sys_exit();
} }
int cond = quadSolver(a, b, c, &sol1, &sol2); int cond = quadSolver(a, b, c, &sol1, &sol2);
if (cond == 0) { if (cond == 0) {
printStringLen("Not real!\n", 11); printStringLen("Not real!\n", 11);
return; sys_exit();
} }
char buffer[30] = {0}; char buffer[30] = {0};

View File

@ -0,0 +1,12 @@
#include "unblock.h"
void unblock(int argc, char ** argv) {
if (argc != 2) {
printStringLen("unblock receives a pid\n", 21);
sys_exit();
}
int pid = atoi(argv[1], MAX_LEN);
sys_unblock(pid);
sys_exit();
}

View File

@ -0,0 +1,11 @@
#ifndef BLOCK_LIB
#define BLOCK_LIB
#include "libc.h"
#include "system.h"
#define MAX_LEN 10
void block(int argc, char *argv[]);
#endif

View File

@ -0,0 +1,11 @@
#ifndef KILL_LIB
#define KILL_LIB
#include "libc.h"
#include "system.h"
#define MAX_LEN 10
void kill(int argc, char *argv[]);
#endif

View File

@ -0,0 +1,11 @@
#ifndef LOOP_LIB
#define LOOP_LIB
#include "libc.h"
#include "system.h"
#define MAX_LEN 100
void loop(int argc, char *argv[]);
#endif

View File

@ -0,0 +1,9 @@
#ifndef PHYLO_LIB
#define PHYLO_LIB
#include "libc.h"
#include "system.h"
void phylo(int argc, char *argv[]);
#endif

View File

@ -0,0 +1,11 @@
#ifndef UNBLOCK_LIB
#define UNBLOCK_LIB
#include "libc.h"
#include "system.h"
#define MAX_LEN 10
void unblock(int argc, char *argv[]);
#endif

View File

@ -16,6 +16,10 @@
#include "semCom.h" #include "semCom.h"
#include "stddef.h" #include "stddef.h"
#include "nice.h" #include "nice.h"
#include "phylo.h"
#include "kill.h"
#include "block.h"
#include "unblock.h"
#define EXIT_FAILURE 1 #define EXIT_FAILURE 1
#define EXIT_SUCCESS 0 #define EXIT_SUCCESS 0
@ -31,11 +35,12 @@ typedef struct cmd_t {
char isBuiltIn; char isBuiltIn;
} cmd_t; } cmd_t;
const int len = 15;
cmd_t commands[] = { cmd_t commands[] = {
{ "help", help, 1 }, { "help", help, 1 },
{ "cat", cat, 0 }, { "cat", cat, 0 },
{ "time", time, 1 }, { "time", time, 1 },
{ "block", block, 0 },
{ "unblock", unblock, 0 },
{ "inforeg", inforeg, 1 }, { "inforeg", inforeg, 1 },
{ "excdiv", excdiv, 1 }, { "excdiv", excdiv, 1 },
{ "excop", excop, 1 }, { "excop", excop, 1 },
@ -44,10 +49,13 @@ cmd_t commands[] = {
{ "cpufeatures", cpufeatures, 1 }, { "cpufeatures", cpufeatures, 1 },
{ "nice", nice, 0 }, { "nice", nice, 0 },
{ "ps", ps, 1 }, { "ps", ps, 1 },
{ "kill", kill, 1 },
{ "sem", sem, 1 }, { "sem", sem, 1 },
{ "quadratic", quadratic, 0 }, { "quadratic", quadratic, 0 },
{ "printmem", printmem, 0 }, { "printmem", printmem, 0 },
{ "phylo", phylo, 0 },
{ "wc", wc, 0 }, { "wc", wc, 0 },
{ NULL, NULL, 0}
}; };
int scanfNoPrint(char * buffer) { int scanfNoPrint(char * buffer) {
@ -119,7 +127,7 @@ void processInput(char * input) {
for (int i = 0; i < end; i++) for (int i = 0; i < end; i++)
argv0[i] = tokens[i]; argv0[i] = tokens[i];
} }
for (int i = 0; i < len; i++) { for (int i = 0; commands[i].name != NULL; i++) {
if (!strcmp(tokens[0], commands[i].name)) { if (!strcmp(tokens[0], commands[i].name)) {
comm_flag0 = 1; comm_flag0 = 1;
comm0 = i; comm0 = i;
@ -127,7 +135,7 @@ void processInput(char * input) {
} }
} }
if (comm_flag0 && pipe != -1) { if (comm_flag0 && pipe != -1) {
for (int i = 0; i < len; i++) { for (int i = 0; commands[i].name != NULL; i++) {
if (!strcmp(tokens[pipe + 1], commands[i].name)) { if (!strcmp(tokens[pipe + 1], commands[i].name)) {
comm_flag1 = 1; comm_flag1 = 1;
comm1 = i; comm1 = i;