Add MM to kernel
Co-authored-by: Ezequiel Bellver <ebellver@itba.edu.ar> Co-authored-by: Juan Barmasch <jbarmasch@itba.edu.ar>
This commit is contained in:
parent
2135068a23
commit
c8eec8ee92
|
@ -4,6 +4,7 @@
|
|||
.config/*
|
||||
index.html
|
||||
.bashrc
|
||||
.vscode/*
|
||||
|
||||
#Binary Files
|
||||
*.bin
|
||||
|
@ -16,4 +17,4 @@ index.html
|
|||
*.elf
|
||||
|
||||
#Object files
|
||||
*.o
|
||||
*.o
|
||||
|
|
|
@ -28,7 +28,7 @@ $(KERNEL_ELF): $(STATICLIBS) $(ALL_OBJECTS)
|
|||
$(LD) $(LDFLAGS) -T kernel.ld --oformat=elf64-x86-64 -o $@ $^
|
||||
|
||||
%.o: %.c
|
||||
$(GCC) $(GCCFLAGS) -I./include -I./drivers -I./interruptions -c $< -o $@
|
||||
$(GCC) $(GCCFLAGS) -I./include -I./drivers -I./interruptions $(MFLAG) -c $< -o $@
|
||||
|
||||
%.o : %.asm
|
||||
$(ASM) $(ASMFLAGS) $< -o $@
|
||||
|
|
|
@ -5,8 +5,14 @@
|
|||
|
||||
typedef struct MemoryManagerCDT * MemoryManagerADT;
|
||||
|
||||
MemoryManagerADT createMemoryManager(void *const restrict memoryForMemoryManager, void *const restrict managedMemory);
|
||||
// MemoryManagerADT createMemoryManager(void *const restrict memoryForMemoryManager, void *const restrict managedMemory);
|
||||
// void *allocMemory(MemoryManagerADT const restrict memoryManager, const size_t memoryToAllocate);
|
||||
|
||||
void *allocMemory(MemoryManagerADT const restrict memoryManager, const size_t memoryToAllocate);
|
||||
char initMemoryManager(void *const restrict memoryForMemoryManager, void *const restrict managedMemory);
|
||||
void * memMalloc(const size_t memoryToAllocate);
|
||||
|
||||
// SACAR DPS
|
||||
char testOne();
|
||||
char testTwo();
|
||||
|
||||
#endif
|
|
@ -7,6 +7,7 @@
|
|||
#include "keyboard.h"
|
||||
#include "time.h"
|
||||
#include "pcb.h"
|
||||
#include "memManager.h"
|
||||
|
||||
extern uint8_t text;
|
||||
extern uint8_t rodata;
|
||||
|
@ -19,10 +20,10 @@ static const uint64_t PageSize = 0x1000;
|
|||
|
||||
static void * const sampleCodeModuleAddress = (void*)0x400000;
|
||||
static void * const sampleDataModuleAddress = (void*)0x500000;
|
||||
static void * const memoryModuleAddress = (void*)0x600000;
|
||||
|
||||
typedef int (*EntryPoint)();
|
||||
|
||||
|
||||
void clearBSS(void * bssAddress, uint64_t bssSize) {
|
||||
memset(bssAddress, 0, bssSize);
|
||||
}
|
||||
|
@ -49,10 +50,34 @@ void * initializeKernelBinary() {
|
|||
|
||||
void load_idt();
|
||||
uint64_t getRSP();
|
||||
void printBottlerAndWait();
|
||||
|
||||
int main() {
|
||||
load_idt();
|
||||
|
||||
if (initMemoryManager(memoryModuleAddress, memoryModuleAddress + sizeof(void *)) == -1) {
|
||||
printStringLen(13, "createMemoryManager() -- ERROR", 31);
|
||||
new_line();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
#ifndef BUDDY
|
||||
// SACAR DESPUÉS! ES SOLO PARA TESTEO... CORRER DE A UNO!
|
||||
if (testOne() == EXIT_FAILURE)
|
||||
return EXIT_FAILURE;
|
||||
// if (testTwo() == EXIT_FAILURE)
|
||||
// return EXIT_FAILURE;
|
||||
#endif
|
||||
|
||||
saveSampleRSP(getRSP());
|
||||
|
||||
printBottlerAndWait();
|
||||
|
||||
((EntryPoint)sampleCodeModuleAddress)();
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void printBottlerAndWait() {
|
||||
printStringLen(4, " ", 80); new_line();
|
||||
printStringLen(4, " (%( ", 80); new_line();
|
||||
printStringLen(15, " Welcome to", 17);
|
||||
|
@ -90,9 +115,4 @@ int main() {
|
|||
wait(3);
|
||||
|
||||
clear();
|
||||
|
||||
saveSampleRSP(getRSP());
|
||||
|
||||
((EntryPoint)sampleCodeModuleAddress)();
|
||||
return 0;
|
||||
}
|
|
@ -1,89 +0,0 @@
|
|||
#include "include/memManager.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#define MANAGED_MEMORY_SIZE 20480
|
||||
|
||||
char mem[MANAGED_MEMORY_SIZE];
|
||||
char mem2[MANAGED_MEMORY_SIZE];
|
||||
|
||||
typedef struct MemoryManagerCDT {
|
||||
char *nextAddress;
|
||||
char *lastAddress;
|
||||
char *initialAddress;
|
||||
} MemoryManagerCDT;
|
||||
|
||||
MemoryManagerADT createMemoryManager(void *const restrict memoryForMemoryManager, void *const restrict managedMemory) {
|
||||
MemoryManagerADT memoryManager = (MemoryManagerADT) memoryForMemoryManager;
|
||||
memoryManager->nextAddress = managedMemory;
|
||||
char * aux = managedMemory;
|
||||
memoryManager->lastAddress = aux - 1;
|
||||
memoryManager->initialAddress = managedMemory;
|
||||
|
||||
return memoryManager;
|
||||
}
|
||||
|
||||
void *allocMemory(MemoryManagerADT const restrict memoryManager, const size_t memoryToAllocate) {
|
||||
char *allocation = memoryManager->nextAddress;
|
||||
if (memoryToAllocate + memoryManager->nextAddress > memoryManager->initialAddress + MANAGED_MEMORY_SIZE){
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memoryManager->nextAddress += memoryToAllocate;
|
||||
if (!(memoryManager->nextAddress -1 == (memoryManager->lastAddress += memoryToAllocate)))
|
||||
return NULL;
|
||||
|
||||
return (void *) allocation;
|
||||
}
|
||||
|
||||
int main() {
|
||||
static MemoryManagerADT memoryManager;
|
||||
static MemoryManagerADT memoryManager2;
|
||||
|
||||
void * memoryForMemoryManager = (void *) 0x700000;
|
||||
// void * memoryForMemoryManager = malloc(sizeof(void *));
|
||||
if (memoryForMemoryManager == NULL) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
void *managedMemory = (void *) 0x700016;// malloc(MANAGED_MEMORY_SIZE);
|
||||
if (managedMemory == NULL) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
void * memoryForMemoryManager2 = (void *) 0x700008;
|
||||
// void * memoryForMemoryManager2 = malloc(sizeof(void *));
|
||||
memoryManager = createMemoryManager(memoryForMemoryManager, managedMemory);
|
||||
|
||||
void * alloc1 = allocMemory(memoryManager, 100);
|
||||
void * alloc2 = allocMemory(memoryManager, 200);
|
||||
void * alloc3 = allocMemory(memoryManager, 300);
|
||||
|
||||
memset(alloc1, 1, 100);
|
||||
memset(alloc2, 2, 200);
|
||||
memset(alloc3, 3, 300);
|
||||
|
||||
for (int i = 0; i < 600; i++) {
|
||||
if (i < 100) {
|
||||
assert(*((char *) alloc1+i) == 1);
|
||||
} else if (i < 300) {
|
||||
assert(*((char *) alloc1+i) == 2);
|
||||
} else if (i < 600) {
|
||||
assert(*((char *) alloc1+i) == 3);
|
||||
}
|
||||
}
|
||||
|
||||
managedMemory = (void *) (0x700016 + 600);
|
||||
memoryManager2 = createMemoryManager(memoryForMemoryManager2, managedMemory);
|
||||
|
||||
void * ptr;
|
||||
while (ptr != NULL){
|
||||
ptr = allocMemory(memoryManager2, (rand() % 2000) + 1);
|
||||
assert ((char *) memoryManager2->nextAddress >= mem2);
|
||||
assert ((char *) memoryManager2->nextAddress <= mem2 + MANAGED_MEMORY_SIZE);
|
||||
assert ((char *) memoryManager2->lastAddress >= mem2);
|
||||
assert ((char *) memoryManager2->lastAddress <= mem2 + MANAGED_MEMORY_SIZE);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
#ifdef BUDDY
|
||||
|
||||
#include "memManager.h"
|
||||
|
||||
char initMemoryManager(void *const restrict memoryForMemoryManager, void *const restrict managedMemory) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
void * memMalloc(const size_t memoryToAllocate) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,2 @@
|
|||
#ifndef BUDDY
|
||||
#endif
|
|
@ -0,0 +1,176 @@
|
|||
#ifndef BUDDY
|
||||
|
||||
#include "memManager.h"
|
||||
// #include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include "video.h"
|
||||
|
||||
#define MANAGED_MEMORY_SIZE 1024 * 1024 * 64
|
||||
|
||||
// char mem[MANAGED_MEMORY_SIZE];
|
||||
// char mem2[MANAGED_MEMORY_SIZE];
|
||||
|
||||
typedef struct MemoryManagerCDT {
|
||||
char *nextAddress;
|
||||
// char *lastAddress;
|
||||
char *initialAddress;
|
||||
} MemoryManagerCDT;
|
||||
|
||||
MemoryManagerADT createMemoryManager(void *const restrict memoryForMemoryManager, void *const restrict managedMemory) {
|
||||
MemoryManagerADT memoryManager = (MemoryManagerADT) memoryForMemoryManager;
|
||||
memoryManager->nextAddress = managedMemory;
|
||||
// char * aux = managedMemory;
|
||||
// memoryManager->lastAddress = aux - 1;
|
||||
memoryManager->initialAddress = managedMemory;
|
||||
|
||||
return memoryManager;
|
||||
}
|
||||
|
||||
void *allocMemory(MemoryManagerADT const restrict memoryManager, const size_t memoryToAllocate) {
|
||||
char *allocation = memoryManager->nextAddress;
|
||||
if (memoryToAllocate + memoryManager->nextAddress > memoryManager->initialAddress + MANAGED_MEMORY_SIZE){
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memoryManager->nextAddress += memoryToAllocate;
|
||||
// if (!(memoryManager->nextAddress -1 == (memoryManager->lastAddress += memoryToAllocate)))
|
||||
// return NULL;
|
||||
|
||||
return (void *) allocation;
|
||||
}
|
||||
|
||||
// Esto es mejor para independizarnos del puntero a memoryManager! Y poder llamar desde distintos lugares (sin la referencia del memManager) a malloc...
|
||||
|
||||
static MemoryManagerADT memoryManager;
|
||||
char initMemoryManager(void *const restrict memoryForMemoryManager, void *const restrict managedMemory) {
|
||||
return ((memoryManager = createMemoryManager(memoryForMemoryManager, managedMemory)) == NULL) ? EXIT_FAILURE : EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void * memMalloc(const size_t memoryToAllocate) {
|
||||
return allocMemory(memoryManager, memoryToAllocate);
|
||||
}
|
||||
|
||||
|
||||
// SACAR DESPUÉS! ES SOLO PARA TESTEO...
|
||||
|
||||
char testOne() {
|
||||
void * alloc1 = memMalloc(100);
|
||||
void * alloc2 = memMalloc(200);
|
||||
void * alloc3 = memMalloc(300);
|
||||
|
||||
memset(alloc1, 1, 100);
|
||||
memset(alloc2, 2, 200);
|
||||
memset(alloc3, 3, 300);
|
||||
|
||||
for (int i = 0; i < 600; i++) {
|
||||
if (i < 100) {
|
||||
if (!(*((char *) alloc1+i) == 1)) {
|
||||
printStringLen(13, "alloc1 -- ERROR", 31);
|
||||
// printStringLen(13, *((char *) alloc1+i), 1);
|
||||
new_line();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
} else if (i < 300) {
|
||||
if (!(*((char *) alloc1+i) == 2)) {
|
||||
printStringLen(13, "alloc2 -- ERROR", 31);
|
||||
new_line();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
} else if (i < 600) {
|
||||
if (!(*((char *) alloc1+i) == 3)) {
|
||||
printStringLen(13, "alloc3 -- ERROR", 31);
|
||||
new_line();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
static unsigned long int next = 1;
|
||||
int rand(void) // RAND_MAX assumed to be 32767
|
||||
{
|
||||
next = next * 1103515245 + 12345;
|
||||
return (unsigned int)(next/65536) % 32768;
|
||||
}
|
||||
|
||||
char testTwo() {
|
||||
void * ptr;
|
||||
while (ptr != NULL){
|
||||
ptr = memMalloc((rand() % 2000) + 1);
|
||||
if (!((char *) memoryManager->nextAddress >= memoryManager->initialAddress)) {
|
||||
printStringLen(13, "allocRand1 -- ERROR", 31);
|
||||
new_line();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (!((char *) memoryManager->nextAddress <= memoryManager->initialAddress + MANAGED_MEMORY_SIZE)) {
|
||||
printStringLen(13, "allocRand2 -- ERROR", 31);
|
||||
new_line();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
// if (!((char *) memoryManager->lastAddress >= memoryManager->initialAddress)) {
|
||||
// }
|
||||
// if (!((char *) memoryManager->lastAddress <= memoryManager->initialAddress + MANAGED_MEMORY_SIZE)) {
|
||||
// }
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
int main() {
|
||||
static MemoryManagerADT memoryManager;
|
||||
static MemoryManagerADT memoryManager2;
|
||||
|
||||
void * memoryForMemoryManager = (void *) 0x700000;
|
||||
// void * memoryForMemoryManager = malloc(sizeof(void *));
|
||||
if (memoryForMemoryManager == NULL) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
void *managedMemory = (void *) 0x700016;// malloc(MANAGED_MEMORY_SIZE);
|
||||
if (managedMemory == NULL) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
void * memoryForMemoryManager2 = (void *) 0x700008;
|
||||
// void * memoryForMemoryManager2 = malloc(sizeof(void *));
|
||||
memoryManager = createMemoryManager(memoryForMemoryManager, managedMemory);
|
||||
|
||||
void * alloc1 = allocMemory(memoryManager, 100);
|
||||
void * alloc2 = allocMemory(memoryManager, 200);
|
||||
void * alloc3 = allocMemory(memoryManager, 300);
|
||||
|
||||
memset(alloc1, 1, 100);
|
||||
memset(alloc2, 2, 200);
|
||||
memset(alloc3, 3, 300);
|
||||
|
||||
for (int i = 0; i < 600; i++) {
|
||||
if (i < 100) {
|
||||
assert(*((char *) alloc1+i) == 1);
|
||||
} else if (i < 300) {
|
||||
assert(*((char *) alloc1+i) == 2);
|
||||
} else if (i < 600) {
|
||||
assert(*((char *) alloc1+i) == 3);
|
||||
}
|
||||
}
|
||||
|
||||
managedMemory = (void *) (0x700016 + 600);
|
||||
memoryManager2 = createMemoryManager(memoryForMemoryManager2, managedMemory);
|
||||
|
||||
void * ptr;
|
||||
while (ptr != NULL){
|
||||
ptr = allocMemory(memoryManager2, (rand() % 2000) + 1);
|
||||
assert ((char *) memoryManager2->nextAddress >= mem2);
|
||||
assert ((char *) memoryManager2->nextAddress <= mem2 + MANAGED_MEMORY_SIZE);
|
||||
assert ((char *) memoryManager2->lastAddress >= mem2);
|
||||
assert ((char *) memoryManager2->lastAddress <= mem2 + MANAGED_MEMORY_SIZE);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue