Created the Malloc function; first stage of memory management implemented.

This commit is contained in:
2019-01-06 20:38:45 -06:00
parent dfe852f69b
commit 67bf4a355d
7 changed files with 69 additions and 149 deletions
+1
View File
@@ -1,3 +1,4 @@
obj/
iso/
kernel.iso
build.number
-1
View File
@@ -1 +0,0 @@
3
-11
View File
@@ -1,11 +0,0 @@
# Create an auto-incrementing build number.
BUILD_NUMBER_LDFLAGS = --defsym __BUILD_DATE=$$(date +'%Y%m%d')
BUILD_NUMBER_LDFLAGS += --defsym __BUILD_NUMBER=$$(cat $(BUILD_NUMBER_FILE))
# Build number file. Increment if any object file changes.
$(BUILD_NUMBER_FILE): $(OBJECTS)
if ! test -f build-number.txt; then echo 0 > build-number.txt; fi
echo $(($(cat build-number.txt) + 1)) > build-number.txt
echo __BUILD_DATE
echo __BUILD_NUMBER
+6
View File
@@ -2,15 +2,21 @@
#define __SYSTEM_H
/* Primitives */
typedef enum { false, true } bool;
typedef unsigned int size_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
/* MAIN.C */
extern unsigned char *memcpy(unsigned char *dest, const unsigned char *src, int count);
extern unsigned char *memset(unsigned char *dest, unsigned char val, int count);
extern unsigned short *memsetw(unsigned short *dest, unsigned short val, int count);
extern int strlen(const char *str);
unsigned char strconcat(unsigned char *dest, unsigned char* src);
extern unsigned char inportb (unsigned short _port);
extern void outportb (unsigned short _port, unsigned char _data);
extern bool strcmp(char* str1, char* str2);
extern void idle();
char* int_to_string(int value, int base);
/* ISRS.C */
void isrs_install();
/* This defines what the stack looks like after an ISR was running */
+15 -8
View File
@@ -1,11 +1,10 @@
GCCPARAMS = -m32 -fno-pie -nostdlib -fstrength-reduce -fomit-frame-pointer -finline-functions -fno-builtin -nostdinc
GCCPARAMS = -m32 -fno-pie -nostdlib -fstrength-reduce -fomit-frame-pointer -finline-functions -fno-builtin -nostdinc -fno-stack-protector
ASPARAMS = -f elf32
LDPARAMS = -m elf_i386
#BUILD_NUMBER_FILE=build-number.txt
INCBULDNUM := sh make_buildnum.sh
objects = obj/start.o \
obj/main.o \
obj/kernel.o \
obj/scrn.o \
obj/gdt.o \
obj/idt.o \
@@ -13,9 +12,12 @@ objects = obj/start.o \
obj/irq.o \
obj/timer.o \
obj/kb.o \
obj/shell.o
obj/shell.o \
obj/memorymanager.o \
obj/pci.o \
obj/port.o
run: kernel.iso
run:kernel.iso
qemu-system-x86_64 -cdrom kernel.iso &
obj/%.o: src/%.c
@@ -27,10 +29,11 @@ obj/%.o: src/%.asm
nasm $(ASPARAMS) -o $@ $<
kernel.bin: linker.ld $(objects)
#Crap way to increment the build number, but will do for now
#thanks to my limited knowledge of Makefiles.
sh make_buildnum.sh
mkdir -p obj/bin
ld $(LDPARAMS) -T $< -o obj/bin/$@ $(objects)
#https://www.linuxjournal.com/content/add-auto-incrementing-build-number-your-build-process
#include buildnumber.mak
kernel.iso: kernel.bin
mkdir iso
@@ -47,3 +50,7 @@ kernel.iso: kernel.bin
.PHONY: clean
clean:
rm -rf obj kernel.iso
.PHONY: resetbuild
resetbuild:
echo 0 > build.number
-121
View File
@@ -1,121 +0,0 @@
#include "../include/system.h"
#include "../include/gdt.h"
#include "../include/idt.h"
#include "../include/scrn.h"
#include "../include/shell.h"
unsigned char *memcpy(unsigned char *dest, const unsigned char *src, int count)
{
/* Add code here to copy 'count' bytes of data from 'src' to
* 'dest', finally return 'dest' */
const char *sp = (const char *)src;
char *dp = (char *)dest;
for(; count != 0; count--) *dp++ = *sp++;
return dest;
}
unsigned char *memset(unsigned char *dest, unsigned char val, int count)
{
/* Add code here to set 'count' bytes in 'dest' to 'val'.
* Again, return 'dest' */
char *temp = (char *)dest;
for( ; count != 0; count--) *temp++ = val;
return dest;
}
unsigned short *memsetw(unsigned short *dest, unsigned short val, int count)
{
/* Same as above, but this time, we're working with a 16-bit
* 'val' and dest pointer. Your code can be an exact copy of
* the above, provided that your local variables if any, are
* unsigned short */
unsigned short *temp = (unsigned short *)dest;
for( ; count != 0; count--) *temp++ = val;
return dest;
}
int strlen(const char *str)
{
/* This loops through character array 'str', returning how
* many characters it needs to check before it finds a 0.
* In simple words, it returns the length in bytes of a string */
//size_t retval;
unsigned int retval;
for(retval = 0; *str != '\0'; str++) retval++;
return retval;
}
bool strcmp(char* str1, char* str2)
{
int len1 = strlen(str1);
int len2 = strlen(str2);
if(len1 != len2)
{
return false;
}
for(int i = len1; i >= 0; i--)
{
if(str1[i] != str2[i]) return false;
}
return true;
}
/* We will use this later on for reading from the I/O ports to get data
* from devices such as the keyboard. We are using what is called
* 'inline assembly' in these routines to actually do the work */
unsigned char inportb (unsigned short _port)
{
unsigned char rv;
__asm__ __volatile__ ("inb %1, %0" : "=a" (rv) : "dN" (_port));
return rv;
}
/* We will use this to write to I/O ports to send bytes to devices. This
* will be used in the next tutorial for changing the textmode cursor
* position. Again, we use some inline assembly for the stuff that simply
* cannot be done in C */
void outportb (unsigned short _port, unsigned char _data)
{
__asm__ __volatile__ ("outb %1, %0" : : "dN" (_port), "a" (_data));
}
//extern char __BUILD_DATE;
//extern char __BUILD_NUMBER;
/* This is a very simple main() function. All it does is sit in an
* infinite loop. This will be like our 'idle' loop */
void main()
{
/* You would add commands after here */
init_video();
puts("Video initialized.\n");
gdt_install();
puts("Global Descriptor Table initialized.\n");
idt_install();
puts("Interrupt Descriptor Table initialized.\n");
isrs_install();
puts("Interrupt Service Routines installed.\n");
irq_install();
__asm__ __volatile__ ("sti");
puts("Interrupt Requests now allowed.\n");
//timer_install();
//puts("Installed Timer.\n");
kb_install();
puts("Keyboard installed.\n");
puts("SDOS version 0.0.0.1 initialized.\n");
run_shell();
//puts(&__BUILD_DATE);
//puts(&__BUILD_NUMBER);
//puts("Divide by zero check:\n");
//puts(5 / 0);
/* ...and leave this loop in. There is an endless loop in
* 'start.asm' also, if you accidentally delete this next line */
for (;;) idle();
}
void idle()
{
__asm__ __volatile__ ("hlt");
}
+45 -6
View File
@@ -3,6 +3,8 @@
0.0.0.1: Intial Release
0.0.0.2: Added idle loop to main function.
0.0.1.2: Added support for the backspace key.
0.0.2.0: Fixed a bug allowing the user to delete all screen
@@ -12,13 +14,15 @@
#include "../include/scrn.h"
#include "../include/system.h"
static char* Prompt = "\n#>";
unsigned char* commandbuffer;
void run_shell()
{
puts("Starting interactive shell...\n");
puts("Starting interactive shell...");
commandbuffer[0] = '\0';
puts("#>");
puts(Prompt);
for(;;) idle();
}
@@ -50,14 +54,15 @@ void key_pressed(unsigned char* character)
void execmd()
{
proccess_command(commandbuffer);
if(strlen(commandbuffer) == 0)
{
puts("\n#>");
puts(Prompt);
return;
}
if(strcmp(commandbuffer, "version"))
{
puts("\nInteractive Shell Version 0.0.2.0\n");
puts("\nInteractive Shell Version 0.0.2.0");
}
else if(strcmp(commandbuffer,"cls"))
{
@@ -67,8 +72,42 @@ void execmd()
{
puts("\nUnknown command: ");
puts(commandbuffer);
puts(".\n");
puts(".");
}
puts("#>");
puts(Prompt);
commandbuffer[0] = '\0';
}
void proccess_command(char *text)
{
int length = strlen(text);
int currentArrayIndex = 0;
if(length == 0) return;
unsigned char *array[length];
unsigned char *temp[length];
int tempIndex = 0;
for(int i = 0; i < length; i++)
{
if(text[i] != ' ')
{
temp[tempIndex] = text[i];
tempIndex++;
}
else
{
tempIndex++;
temp[tempIndex] = '\0';
int_to_string(tempIndex, 10);
memcpy(array[currentArrayIndex], *temp, tempIndex);
currentArrayIndex++;
tempIndex = 0;
}
}
for(int j = 0; j < currentArrayIndex; j++)
{
puts('\n');
puts(array[currentArrayIndex]);
}
}