Moving certain aspects into their own static library

Problem:
	As our kernel grows we need more complex datastructures and functions these would come from
	the standard C/C++ library with normal programs.
	The kernel is a freestanding programme and has no access to standard libraries.

Solution:
	We build a mini version of the standard C/C++ library which will contain the
	datastructures and functions we want. This library can then be statically linked
	into our kernel binary.

	Making it a statically linked library also gives more structure to the project.
	Keeping these random functions and datastructures in the kernel just clutters the
	kernel source code with less relevant source code.
dev
Nigel Barink 2023-02-19 23:38:32 +01:00
parent 94a2de3847
commit b07b4f0d38
32 changed files with 255 additions and 224 deletions

View File

@ -1,11 +1,10 @@
EMULATOR = qemu-system-i386
AS = ${HOME}/opt/cross/bin/i686-elf-as
CC = ${HOME}/opt/cross/bin/i686-elf-gcc
CPP = ${HOME}/opt/cross/bin/i686-elf-g++
CFLAGS = -ffreestanding -Og -ggdb -Wall -Wextra
CFLAGS = -ffreestanding -Og -ggdb -Wall -Wextra -I source/CoreLib/build/include
OFILES =$(BUILD_DIR)/boot.o $(BUILD_DIR)/Path.o $(BUILD_DIR)/kterm.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/memory.o $(BUILD_DIR)/paging.o $(BUILD_DIR)/VFS.o $(BUILD_DIR)/pit.o $(BUILD_DIR)/time.o $(BUILD_DIR)/keyboard.o $(BUILD_DIR)/io.o $(BUILD_DIR)/processor.o $(BUILD_DIR)/gdtc.o $(BUILD_DIR)/idt.o $(BUILD_DIR)/pic.o $(BUILD_DIR)/sv-terminal.o $(BUILD_DIR)/string.o $(BUILD_DIR)/prekernel.o $(BUILD_DIR)/KHeap.o $(BUILD_DIR)/pci.o $(BUILD_DIR)/pcidevice.o $(BUILD_DIR)/atapiDevice.o $(BUILD_DIR)/ataDevice.o $(BUILD_DIR)/rsdp.o $(BUILD_DIR)/acpi.o
OFILES =$(BUILD_DIR)/boot.o $(BUILD_DIR)/kterm.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/memory.o $(BUILD_DIR)/paging.o $(BUILD_DIR)/VFS.o $(BUILD_DIR)/pit.o $(BUILD_DIR)/time.o $(BUILD_DIR)/keyboard.o $(BUILD_DIR)/io.o $(BUILD_DIR)/processor.o $(BUILD_DIR)/gdtc.o $(BUILD_DIR)/idt.o $(BUILD_DIR)/pic.o $(BUILD_DIR)/sv-terminal.o $(BUILD_DIR)/prekernel.o $(BUILD_DIR)/KHeap.o $(BUILD_DIR)/pci.o $(BUILD_DIR)/pcidevice.o $(BUILD_DIR)/atapiDevice.o $(BUILD_DIR)/ataDevice.o $(BUILD_DIR)/rsdp.o $(BUILD_DIR)/acpi.o
SRC_DIR = source
BUILD_DIR = build
@ -53,8 +52,7 @@ test_disk: all
build_kernel: $(OBJ_LINK_LIST)
$(CC) -T $(SRC_DIR)/kernel//linker.ld -o $(BUILD_DIR)/myos.bin \
-ffreestanding -ggdb -Og -nostdlib $(OBJ_LINK_LIST) -lgcc
$(CPP) -T $(SRC_DIR)/kernel/linker.ld -o $(BUILD_DIR)/myos.bin -ffreestanding -ggdb -Og -nostdlib $(OBJ_LINK_LIST) -lgcc -L source/CoreLib/build -lCoreLib
build_x86_64:
$(AS) $(SRC_DIR)/cgc/x86_64/crti.s -o $(BUILD_DIR)/crti_64.o
@ -71,7 +69,7 @@ $(BUILD_DIR)/kterm.o:
$(CPP) -c $(SRC_DIR)/kernel/terminal/kterm.cpp -o $(BUILD_DIR)/kterm.o $(CFLAGS) -fno-exceptions -fno-rtti
$(BUILD_DIR)/io.o:
$(CPP) -c $(SRC_DIR)/kernel/drivers/io/io.cpp -o $(BUILD_DIR)/io.o $(CFLAGS) -fno-exceptions -fno-rtti
$(CPP) -c $(SRC_DIR)/kernel/drivers/io/io.cpp -o $(BUILD_DIR)/io.o $(CFLAGS) -fno-exceptions -fno-rtti
$(BUILD_DIR)/idt.o:
$(CPP) -c $(SRC_DIR)/kernel/interrupts/idt.cpp -o $(BUILD_DIR)/idt.o $(CFLAGS) -fno-exceptions -fno-rtti
@ -82,9 +80,6 @@ $(BUILD_DIR)/gdtc.o:
$(BUILD_DIR)/pic.o:
$(CPP) -c $(SRC_DIR)/kernel/drivers/pic/pic.cpp -o $(BUILD_DIR)/pic.o $(CFLAGS) -fno-exceptions -fno-rtti
$(BUILD_DIR)/string.o:
$(CC) -c $(SRC_DIR)/lib/include/string.c -o $(BUILD_DIR)/string.o $(CFLAGS) -std=gnu99
$(BUILD_DIR)/PhysicalMemoryManager.o:
$(CPP) -c $(SRC_DIR)/kernel/memory/PhysicalMemoryManager.cpp -o $(BUILD_DIR)/PhysicalMemoryManager.o $(CFLAGS) -fno-exceptions -fno-rtti
@ -136,9 +131,6 @@ $(BUILD_DIR)/prekernel.o:
$(BUILD_DIR)/processor.o:
$(CPP) -c $(SRC_DIR)/kernel/i386/processor.cpp -o $(BUILD_DIR)/processor.o $(CFLAGS) -fno-exceptions -fno-rtti
$(BUILD_DIR)/Path.o:
$(CPP) -c $(SRC_DIR)/kernel/vfs/Path.cpp -o $(BUILD_DIR)/Path.o $(CFLAGS) -fno-exceptions -fno-rtti
# Assembly -> Object files
$(BUILD_DIR)/boot.o:
$(AS) $(SRC_DIR)/kernel/boot/boot.s -o $(BUILD_DIR)/boot.o

33
source/CoreLib/Makefile Normal file
View File

@ -0,0 +1,33 @@
CPP = ${HOME}/opt/cross/bin/i686-elf-g++
CFLAGS = -ffreestanding -Og -ggdb -Wall -Wextra
SRC_DIR = .
BUILD_DIR = build
OBJ_FOLDER = bin
OUTPUTFILE = $(BUILD_DIR)/libCoreLib.a
OFILES = $(OBJ_FOLDER)/memory.o $(OBJ_FOLDER)/path.o $(OBJ_FOLDER)/stack.o $(OBJ_FOLDER)/string.o $(OBJ_FOLDER)/stringview.o
.phony: all
all: $(OUTPUTFILE)
cp *.h build/include/
$(OUTPUTFILE): $(OFILES)
ar -rc $(OUTPUTFILE) $(OFILES)
$(OBJ_FOLDER)/memory.o: Memory.cpp
$(CPP) -c Memory.cpp -o $(OBJ_FOLDER)/memory.o $(CFLAGS)
$(OBJ_FOLDER)/path.o: Path.cpp
$(CPP) -c Path.cpp -o $(OBJ_FOLDER)/path.o $(CFLAGS)
$(OBJ_FOLDER)/stack.o: Stack.cpp
$(CPP) -c Stack.cpp -o $(OBJ_FOLDER)/stack.o $(CFLAGS)
$(OBJ_FOLDER)/string.o: String.cpp
$(CPP) -c String.cpp -o $(OBJ_FOLDER)/string.o $(CFLAGS)
$(OBJ_FOLDER)/stringview.o: StringView.cpp
$(CPP) -c StringView.cpp -o $(OBJ_FOLDER)/stringview.o $(CFLAGS)

58
source/CoreLib/Memory.cpp Normal file
View File

@ -0,0 +1,58 @@
//
// Created by nigel on 19/02/23.
//
#include "Memory.h"
void* memset (void* ptr, int value, size_t num)
{
for( int i = 0; i < num; i++ )
{
unsigned char* data = (unsigned char*)ptr+ i;
*data = (unsigned char)value;
}
return ptr;
}
int memcmp( const void* ptr1, const void* ptr2, size_t num)
{
const unsigned char * cs = (const unsigned char*) ptr1;
const unsigned char * ct = (const unsigned char*) ptr2;
for (int i = 0 ; i < num ; i++, cs++, ct++ ){
if( *cs < *ct){
return -1;
} else if( *cs > *ct){
return 1;
}
}
return 0;
}
size_t strlen(const char* str) {
size_t len = 0;
while(str[len]){
len++;
}
return len;
}
int strncmp ( const char* str1, const char* str2, size_t num ){
for( int i = 0; i < num ; i++){
if( str1[i] < str2[i]){
return -1;
}
if( str1[i] > str2[i] ){
return 1;
}
}
return 0;
}

12
source/CoreLib/Memory.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
#include <stddef.h>
#include <stdint.h>
void* memset (void* ptr, int value, size_t num);
int memcmp( const void* ptr1, const void* ptr2, size_t num);
size_t strlen(const char* str);
int strncmp ( const char* str1, const char* str2, size_t num );

24
source/CoreLib/Path.cpp Normal file
View File

@ -0,0 +1,24 @@
//
// Created by nigel on 19/02/23.
//
#include "Path.h"
Path::Path(String path)
: path(path)
{
}
StringView Path::getbasename()
{
unsigned int path_length = path.length();
int i = path_length;
while (path[i] != '/')
i--;
return {path,static_cast<unsigned int>(i +1), path_length};
}
char* Path::str() {
return path.str();
}

22
source/CoreLib/Path.h Normal file
View File

@ -0,0 +1,22 @@
//
// Created by nigel on 19/02/23.
//
#pragma once
#include <stddef.h>
#include "StringView.h"
class Path{
public:
explicit Path(String path);
StringView getbasename();
char* str();
private:
String path;
};

3
source/CoreLib/Stack.cpp Normal file
View File

@ -0,0 +1,3 @@
//
// Created by nigel on 19/02/23.
//

View File

@ -1,5 +1,5 @@
#pragma once
#include "../../kernel/memory/KernelHeap.h"
#include "../kernel/memory/KernelHeap.h"
#include <stdint.h>
template <typename T>

39
source/CoreLib/String.cpp Normal file
View File

@ -0,0 +1,39 @@
#include "String.h"
#include <stdint.h>
#include <stddef.h>
String::String(char* characters)
: chars(characters)
{
}
char* String::str(){
return chars;
}
unsigned int String::length ()
{
int i = 0;
while ( chars[i] != '\0'){
i++;
}
return i;
}
// Returns a null character if size exceeds limits
char String::operator[] (size_t idx)
{
if( idx > this->length())
return '\0';
return chars[idx];
}
const char String::operator[](size_t idx) const {
return (const char) chars[idx];
}

18
source/CoreLib/String.h Normal file
View File

@ -0,0 +1,18 @@
#pragma once
#include <stddef.h>
class String {
public:
String(char* characters);
String(String&) = default;
unsigned int length();
char* str ();
char operator[](size_t index) ;
const char operator[](size_t idx) const;
protected:
char* chars;
};

View File

@ -0,0 +1,14 @@
//
// Created by nigel on 19/02/23.
//
#include "StringView.h"
StringView::StringView(String string, unsigned int start, unsigned int end)
: String(string), begin(start), end(end)
{
}
char* StringView::str(){
//TODO: Not implemented
}

View File

@ -0,0 +1,14 @@
//
// Created by nigel on 19/02/23.
//
#pragma once
#include "String.h"
class StringView : String {
public:
StringView(String string, unsigned int start, unsigned int end );
char* str ();
private:
unsigned int begin;
unsigned int end;
};

BIN
source/CoreLib/bin/memory.o Normal file

Binary file not shown.

BIN
source/CoreLib/bin/path.o Normal file

Binary file not shown.

BIN
source/CoreLib/bin/stack.o Normal file

Binary file not shown.

BIN
source/CoreLib/bin/string.o Normal file

Binary file not shown.

Binary file not shown.

View File

@ -1,7 +1,8 @@
#pragma once
#include <stdint.h>
#include "./../../terminal/kterm.h"
#include "../../../lib/include/mem.h"
#include <CoreLib/Memory.h>
struct RSDPTR {
char signature[8];
uint8_t Checksum ;

View File

@ -12,7 +12,6 @@ void ATA_DEVICE::Soft_Reset(uint8_t DEVICE_CHANNEL,DEVICE_DRIVE drive){
}
void ATA_DEVICE::Identify(uint16_t DEVICE_CHANNEL,DEVICE_DRIVE drive ){
// lets ignore which port we actually want to check for now !

View File

@ -1,10 +1,6 @@
/*
Copyright © Nigel Barink 2023
*/
extern "C"{
#include "../lib/include/string.h"
}
#include "memory/memory.h"
#include "memory/KernelHeap.h"
#include "memory/gdt/gdtc.h"

View File

@ -1,8 +1,9 @@
#pragma once
#include <stddef.h>
#include <CoreLib/Memory.h>
#include "../prekernel/bootstructure.h"
#include "../terminal/kterm.h"
#include "../../lib/include/mem.h"
#include "../bitmap.h"
#define BLOCK_SIZE 4092

View File

@ -1,6 +1,6 @@
#pragma once
#include "gdt/gdtc.h"
#include "../../lib/include/string.h"
#include <CoreLib/Memory.h>
struct TaskStateSegment {
uint32_t prev_tss;

View File

@ -5,7 +5,8 @@
#include "memoryinfo.h"
#include "../prekernel/multiboot.h"
#include "../terminal/kterm.h"
#include "../../lib/include/mem.h"
#include <CoreLib/Memory.h>
#include "../bitmap.h"
#define BLOCK_SIZE 4092

View File

@ -4,8 +4,6 @@
#include "../drivers/pit/pit.h"
#include "../drivers/ps-2/keyboard.h"
#include "../memory/PhysicalMemoryManager.h"
extern "C" {
#include "../../lib/include/string.h"
}
#include <CoreLib/Memory.h>
extern "C" void startSuperVisorTerminal();

View File

@ -5,10 +5,7 @@
#include "../drivers/vga/colors.h"
#include "../drivers/io/io.h"
extern "C" {
#include "../../lib/include/string.h"
}
#include "CoreLib/Memory.h"
void kterm_init();

View File

@ -1,83 +0,0 @@
//
// Created by nigel on 19/02/23.
//
#include "Path.h"
#include "../memory/KernelHeap.h"
String::String(char* characters)
: chars(characters)
{
}
char* String::str(){
return chars;
}
unsigned int String::length ()
{
int i = 0;
while ( chars[i] != '\0'){
i++;
}
return i;
}
// Returns a null character if size exceeds limits
char String::operator[] (size_t idx)
{
if( idx > this->length())
return '\0';
return chars[idx];
}
const char String::operator[](size_t idx) const {
return (const char) chars[idx];
}
StringView::StringView(String &string, unsigned int start, unsigned int end)
: String(string), begin(start), end(end)
{
}
char* StringView::str(){
char* str = (char*) malloc((this->length() * sizeof(char))+1);
int index = 0;
for ( int i = begin; i < end ; i++){
str[index] = chars[i];
index++;
}
chars[index+1] ='\0';
return str;
}
Path::Path(char *path)
: path(String(path))
{
}
Path::Path(String &path)
: path(path)
{
}
StringView Path::getbasename()
{
unsigned int path_length = path.length();
int i = path_length;
while (path[i] != '/')
i--;
return StringView(path,i +1, path_length);
}
char* Path::str() {
return path.str();
}

View File

@ -1,46 +0,0 @@
//
// Created by nigel on 19/02/23.
//
#pragma once
#include <stddef.h>
class String {
public:
String(char* characters);
String(String&) = default;
unsigned int length();
char* str ();
char operator[](size_t index) ;
const char operator[](size_t idx) const;
protected:
char* chars;
};
class StringView : String {
public:
StringView(String& string, unsigned int start, unsigned int end );
char* str ();
private:
unsigned int begin;
unsigned int end;
};
class Path{
public:
Path(String& path);
Path(char* path);
StringView getbasename();
char* str();
private:
String path;
};

View File

@ -4,7 +4,7 @@
#include "../drivers/ata/ataDevice.h"
#include "../partitiontable/mbr/MasterBootRecord.h"
#include "../memory/KernelHeap.h"
#include "Path.h"
#include "../../CoreLib/Memory.h"
#include "../filesystem/FAT/DirectoryEntry.h"
MOUNT_INFO mountInfo;
@ -389,6 +389,8 @@ char* FindNextEntryName (char* path )
void FileSystem::ResolvePath(Path &path)
{
// See reference material (1) https://man7.org/linux/man-pages/man7/path_resolution.7.html
char* string_path = path.str();
void* cpy = string_path;
@ -410,9 +412,6 @@ void FileSystem::ResolvePath(Path &path)
skip = strlen(entry_name);
free(entry_name);
free(cpy);
}

View File

@ -1,6 +1,6 @@
#pragma once
#include <stdint.h>
#include "Path.h"
#include "../../CoreLib/Path.h"
#define FS_FILE 0
#define FS_DIRECTORY 1

View File

@ -1,31 +0,0 @@
#pragma once
// NOTE: These should not be inline
inline void* memset (void* ptr, int value, size_t num)
{
for( int i = 0; i < num; i++ )
{
unsigned char* data = (unsigned char*)ptr+ i;
*data = (unsigned char)value;
}
return ptr;
}
inline int memcmp( const void* ptr1, const void* ptr2, size_t num)
{
const unsigned char * cs = (const unsigned char*) ptr1;
const unsigned char * ct = (const unsigned char*) ptr2;
for (int i = 0 ; i < num ; i++, cs++, ct++ ){
if( *cs < *ct){
return -1;
} else if( *cs > *ct){
return 1;
}
}
return 0;
}

View File

@ -1,26 +0,0 @@
#include "string.h"
size_t strlen(const char* str) {
size_t len = 0;
while(str[len]){
len++;
}
return len;
}
int strncmp ( const char* str1, const char* str2, size_t num ){
for( int i = 0; i < num ; i++){
if( str1[i] < str2[i]){
return -1;
}
if( str1[i] > str2[i] ){
return 1;
}
}
return 0;
}

View File

@ -1,4 +0,0 @@
#pragma once
#include <stddef.h>
size_t strlen(const char* str);
int strncmp ( const char* str1, const char* str2, size_t num );