Implemented a proper libc

This commit is contained in:
2024-10-05 13:34:24 +02:00
parent a6445ebcb6
commit 5a5ea4ea59
28 changed files with 258 additions and 123 deletions
+44 -27
View File
@@ -3,8 +3,8 @@ CC := i686-elf-gcc
LD := i686-elf-ld
AS := nasm
CFLAGS := -g -std=c17 -ffreestanding -fno-builtin -fno-stack-protector -fno-pie \
-Os -nostdinc -mgeneral-regs-only -m32 -march=i386 -Wall -Iinclude/c
CCFLAGS := -g -std=c17 -ffreestanding -fno-builtin -fno-stack-protector -fno-pie \
-Os -nostdinc -mgeneral-regs-only -m32 -march=i386 -Wall
LDFLAGS := -z notext -melf_i386 -nostdlib
ASFLAGS :=
@@ -12,61 +12,78 @@ include make.config
SRC := ./src
BIN := ./bin
LIBK_INC := ./include
LIBC_INC := ./include/libc
LIBC_DIR := ./libc
LIBK_DIR := ./libk
C_FILES := calc edit
FONTS := testfont term16n
BOOTLOADER := $(BIN)/stage1.bin $(BIN)/stage2.bin $(BIN)/stage3.bin
KERNEL := $(BIN)/kernel.bin
LIBC := $(BIN)/libc.o
# TODO: LIBK := $(BIN)/libk.o
ISO := kern.iso
.PHONY: os clean run
# Main compilation target
os: $(BIN) bootloader kernel $(C_FILES) $(FONTS)
@cc ./makedisk.c -o ./makedisk
@./makedisk # TODO: List of files
os: $(ISO)
$(ISO): $(BIN) $(BOOTLOADER) $(KERNEL) $(C_FILES) $(FONTS)
cc ./makedisk.c -o ./makedisk
./makedisk # TODO: List of files
@echo "\nDone!\n"
$(BIN):
@[ -d $(BIN) ] || mkdir $(BIN)
@rm -rf $(BIN)/*
[ -d $(BIN) ] || mkdir $(BIN)
rm -rf $(BIN)/*
# Bootloader: This target is responsible for creating stage?.bin (1,2,3)
bootloader: $(SRC)/stage1.asm $(SRC)/stage2.asm
$(BOOTLOADER): $(SRC)/stage1.asm $(SRC)/stage2.asm $(SRC)/stage3.c $(LIBC)
@echo "Assembling $(SRC)/stage1.asm..."
@$(AS) -fbin -o $(BIN)/stage1.bin $(SRC)/stage1.asm $(ASFLAGS)
$(AS) -fbin -o $(BIN)/stage1.bin $(SRC)/stage1.asm $(ASFLAGS)
@echo "Assembling $(SRC)/stage2.asm..."
@$(AS) -fbin -o $(BIN)/stage2.bin $(SRC)/stage2.asm $(ASFLAGS)
$(AS) -fbin -o $(BIN)/stage2.bin $(SRC)/stage2.asm $(ASFLAGS)
@echo "Compiling $(SRC)/stage3.c..."
@$(CC) $(CFLAGS) -Iinclude -o $(BIN)/stage3.o -c $(SRC)/stage3.c
@$(LD) $(LDFLAGS) -T$(SRC)/stage3.ld --oformat binary -o $(BIN)/stage3.bin $(BIN)/stage3.o
@rm $(BIN)/stage3.o
$(CC) $(CCFLAGS) -I$(LIBK_INC) -I$(LIBC_INC) -o $(BIN)/stage3.o -c $(SRC)/stage3.c
$(LD) $(LDFLAGS) -T$(SRC)/stage3.ld --oformat binary -o $(BIN)/stage3.bin $(BIN)/stage3.o $(LIBC)
rm $(BIN)/stage3.o
# Kernel: This target is responsible for creating kernel.bin
kernel: $(SRC)/kernel.c
$(KERNEL): $(SRC)/kernel.c $(LIBC) #TODO: $(LIBK)
@echo "Compiling $(SRC)/kernel.c..."
@$(CC) $(CFLAGS) -Iinclude -o $(BIN)/kernel.o -c $(SRC)/kernel.c
@$(LD) $(LDFLAGS) -T$(SRC)/kernel.ld --oformat binary -o $(BIN)/kernel.bin $(BIN)/kernel.o
@rm $(BIN)/kernel.o
$(CC) $(CCFLAGS) -I$(LIBK_INC) -I$(LIBC_INC) -o $(BIN)/kernel.o -c $(SRC)/kernel.c
$(LD) $(LDFLAGS) -T$(SRC)/kernel.ld --oformat binary -o $(BIN)/kernel.bin $(BIN)/kernel.o $(LIBC) #TODO: LIBK
rm $(BIN)/kernel.o
# This target compiles *.c (except kernel.c)
$(C_FILES):
$(C_FILES): $(LIBC)
@echo "Compiling $(SRC)/$@.c..."
@$(CC) $(CFLAGS) -o $(BIN)/$@.o -c $(SRC)/$@.c
@$(LD) $(LDFLAGS) -T$(SRC)/$@.ld --oformat binary -o $(BIN)/$@.bin $(BIN)/$@.o
@rm $(BIN)/$@.o
$(CC) $(CCFLAGS) -I$(LIBC_INC) -o $(BIN)/$@.o -c $(SRC)/$@.c
$(LD) $(LDFLAGS) -T$(SRC)/$@.ld --oformat binary -o $(BIN)/$@.bin $(BIN)/$@.o $(LIBC)
rm $(BIN)/$@.o
# This target builds all fonts inside src/fonts
$(LIBC): $(LIBC_DIR)/**.c
@echo "Compiling $(LIBC)..."
mkdir -p $(BIN)/libc
for f in libc/*.c; do $(CC) $(CCFLAGS) -I$(LIBC_INC) -o $(BIN)/$$f.o -c $$f; done
$(LD) $(LDFLAGS) -r -o $(LIBC) $(BIN)/libc/**.o
# This target retrieves all fonts inside src/fonts
$(FONTS):
@cp $(SRC)/fonts/$@.fnt $(BIN)/$@.fnt
cp $(SRC)/fonts/$@.fnt $(BIN)/$@.fnt
# Run QEMU
run:
qemu-system-x86_64\
-drive format=raw,file="./kern.iso",index=0,media=disk\
-drive format=raw,file=$(ISO),index=0,media=disk\
-m 256M -accel tcg -monitor stdio\
-rtc base=localtime,clock=host,driftfix=slew\
-audiodev coreaudio,id=audio0 -machine pcspk-audiodev=audio0
# Clean all build files
clean:
@rm -rf $(BIN)
@#rm -f kern.iso
@rm -f makedisk
rm -rf $(BIN)
rm -f makedisk
+15
View File
@@ -0,0 +1,15 @@
#ifndef COLOR_H
#define COLOR_H
#define BLACK 0x00000000 // 0
#define RED 0x00ff0000 // 1
#define GREEN 0x0000ff00 // 2
#define YELLOW 0x00ffff00 // 3
#define BLUE 0x000000ff // 4
#define MAGENTA 0x00ff00ff // 5
#define CYAN 0x0000ffff // 6
#define WHITE 0x00ffffff // 7
uint32_t codeToColor(uint8_t code);
#endif
+41
View File
@@ -0,0 +1,41 @@
#ifndef STDIO_H
#define STDIO_H
#include <stddef.h>
// Nobody should have to interface with the internals of this structure
typedef struct _iobuf { // 24B
unsigned char* _ptr; // File buffer base
unsigned int _size; // Size of buffer
int _offset; // Offset into file
unsigned short _flag; // Open flags
int _file; // File descriptor
char __reserved[6];
} FILE;
// Standard file pointers
extern FILE* stdin;
extern FILE* stdout;
extern FILE* stderr;
/* File IO */
FILE* fopen(const char* filename, const char* mode);
int fclose(FILE* stream);
size_t fread(void* ptr, size_t size, size_t count, FILE* stream);
size_t fwrite(const void* ptr, size_t size, size_t count, FILE* stream);
int fseek(FILE* stream, long int origin, int offset);
long int ftell(FILE* stream);
/* Terminal IO */
int printf(const char* fmt, ...);
int sprintf(char* str, const char* fmt, ...);
int snprintf(char* str, size_t n, const char* fmt, ...);
int fprintf(FILE* stream, const char* fmt, ...);
void putc(char c);
char getc();
void print(const char* str);
void clear();
void reads(char* str);
#endif
+10
View File
@@ -0,0 +1,10 @@
#ifndef STDLIB_H
#define STDLIB_H
#include <stdint.h>
#include <syscall.h>
void* malloc(const uint32_t size);
void free(const void* ptr);
#endif
+23
View File
@@ -0,0 +1,23 @@
#ifndef STRING_H
#define STRING_H
#include <stdint.h>
void* memcpy(void* src, void* dst, uint32_t length);
void* memcpy32(void* src, void* dst, uint32_t length);
void* memset(void* dst, uint8_t value, uint32_t length);
char* itoa(uint32_t val, uint8_t base);
uint32_t atoi(const char* str);
int isSpace(char c);
char* strcpy(char* str1, const char* str2);
char* strncpy(char* str1, const char* str2, size_t n);
char* strcat(char* str1, const char* str2);
char* strncat(char* str1, const char* str2, size_t n);
char* strchr(const char* str, char c);
char* strrchr(const char* str, char c);
int strcmp(const char* str1, const char* str2);
int strncmp(const char* str1, const char* str2, size_t n);
uint32_t strlen(const char* str);
#endif
+38
View File
@@ -0,0 +1,38 @@
#ifndef SYSCALL_H
#define SYSCALL_H
#include <stddef.h>
#define MAX_SYSCALLS 9
typedef enum {
SYS_RESERVED,
SYS_SLEEP,
SYS_READ,
SYS_WRITE,
SYS_OPEN,
SYS_CLOSE,
SYS_MALLOC,
SYS_FREE,
SYS_SEEK,
} SYSCALL_NUMBERS;
typedef enum {
O_CREAT = 0x01, // Create if does not exist
O_RDONLY = 0x02, // Read only
O_WRONLY = 0x04, // Write only
O_RDWR = 0x08, // Read and write
O_APPEND = 0x10, // Always writes at the end (appends)
O_TRUNC = 0x20, // Truncate file size and position to 0 on Open
O_BIN = 0x40, // Read bytes, not text
} OFLAGS;
int syscall(int nr, int arg0, int arg1, int arg2);
/* Syscall wrappers */
int read(int fd, void* ptr, size_t count);
int write(int fd, const void* ptr, size_t count);
int open(const char* path, int oflag);
int close(int fd);
#endif
+8
View File
@@ -0,0 +1,8 @@
#ifndef TIME_H
#define TIME_H
#include <stdint.h>
void sleep(uint32_t ms);
#endif
BIN
View File
Binary file not shown.
+1 -20
View File
@@ -1,25 +1,8 @@
#ifndef _FILEIO_H
#define _FILEIO_H
#include <stdio.h>
#include <syscall.h>
#include <stdlib.h>
#include <stddef.h>
// Nobody should have to interface with the internals of this structure
typedef struct _iobuf { // 24B
unsigned char* _ptr; // File buffer base
unsigned int _size; // Size of buffer
int _offset; // Offset into file
unsigned short _flag; // Open flags
int _file; // File descriptor
char __reserved[6];
} FILE;
FILE* stdin;
FILE* stdout;
FILE* stderr;
FILE* fopen(const char* filename, const char* mode) {
int oflag = O_RDWR | O_CREAT | O_BIN; // TODO!
@@ -79,5 +62,3 @@ long int ftell(FILE* stream) {
return -1;
}
#endif
+2 -7
View File
@@ -1,14 +1,11 @@
#ifndef _PRINTF_H
#define _PRINTF_H
#include <stdio.h>
#include <syscall.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <_fileio.h>
// Warning: returns a allocated buffer, must be freed!
// Warning: returns an allocated buffer, must be freed!
char* _format(const char* fmt, va_list args) {
static char* buffer;
size_t len = 0;
@@ -126,5 +123,3 @@ int printf(const char* fmt, ...) {
return len;
}
#endif
+2 -12
View File
@@ -1,14 +1,5 @@
#ifndef COLOR_H
#define COLOR_H
#define BLACK 0x00000000 // 0
#define RED 0x00ff0000 // 1
#define GREEN 0x0000ff00 // 2
#define YELLOW 0x00ffff00 // 3
#define BLUE 0x000000ff // 4
#define MAGENTA 0x00ff00ff // 5
#define CYAN 0x0000ffff // 6
#define WHITE 0x00ffffff // 7
#include <stdint.h>
#include <color.h>
uint32_t codeToColor(uint8_t code) {
switch (code) {
@@ -24,4 +15,3 @@ uint32_t codeToColor(uint8_t code) {
}
}
#endif
+16
View File
@@ -0,0 +1,16 @@
#ifndef MATH_H
#define MATH_H
// Constants
#define PI 3.141592653589 // Should be good enough
#define E 2.718281828459
// Macros
#define abs(x) ( (x) > 0 ? (x) : -(x) )
#define sign(x) ( (x) == 0 ? 0 : ((x) < 0 ? -1 : 1 ) )
#define floor(x) ( (int)(x) ) // C floors floats when converting to ints
#define round(x) ( (int)((x) + 0.5) ) // Normal rounding
#define ceil(x) ( (int)((x) + 1) ) // Always rounds up
#endif
+10
View File
@@ -0,0 +1,10 @@
#ifndef STDARG_H
#define STDARG_H
typedef char* va_list;
#define va_start(ap, paramn) (void)((ap) = (char*)(&(paramn) + 1))
#define va_end(ap) (void)((ap) = 0)
#define va_arg(ap, type) (((type*)((ap) = ((ap) + sizeof(type))))[-1])
#endif
+8
View File
@@ -0,0 +1,8 @@
#ifndef BOOL_H
#define BOOL_H
#define bool _Bool
#define true 1
#define false 0
#endif
+9
View File
@@ -0,0 +1,9 @@
#ifndef STDDEF_H
#define STDDEF_H
#define NULL ((void*)0)
typedef unsigned int size_t;
typedef signed int ssize_t;
#endif
+20
View File
@@ -0,0 +1,20 @@
#ifndef STDINT_H
#define STDINT_H
#include <stddef.h>
// Signed ints
typedef signed char int8_t;
typedef signed short int16_t;
typedef signed int int32_t;
typedef signed long long int64_t;
// Unsigned ints
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
// TODO: floats
#endif
+4 -13
View File
@@ -1,20 +1,12 @@
#ifndef STDIO_H
#define STDIO_H
#include <stdio.h>
#include <syscall.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
/* Declarations */
void putc(char);
char getc();
int printf(const char*, ...);
/* STDIO Include */
#include <_fileio.h>
#include <_printf.h>
FILE* stdin;
FILE* stdout;
FILE* stderr;
void putc(char c) {
// write(stdout, &c, 1); TODO: This doesn't work and we need to use a static variable
@@ -59,4 +51,3 @@ void reads(char* buffer) {
buffer[len] = '\0'; // Null terminate
}
#endif
+1 -4
View File
@@ -1,8 +1,6 @@
#ifndef STDLIB_H
#define STDLIB_H
#include <stdint.h>
#include <syscall.h>
#include <stdlib.h>
void* malloc(const uint32_t size) {
return (void*)syscall(SYS_MALLOC, size, 0, 0);
@@ -12,4 +10,3 @@ void free(const void* ptr) {
syscall(SYS_FREE, (int)ptr, 0, 0);
}
#endif
+2 -9
View File
@@ -1,12 +1,6 @@
#ifndef STRING_H
#define STRING_H
#include <string.h>
#include <stdint.h>
void* memcpy(void* src, void* dst, uint32_t length);
void* memcpy32(void* src, void* dst, uint32_t length);
void* memset(void* dst, uint8_t value, uint32_t length);
// Source: http://www.strudel.org.uk/itoa/
char* itoa(uint32_t val, uint8_t base) {
static char buf[32] = { 0 };
@@ -84,7 +78,7 @@ char* strchr(const char* str, char c) {
return (*s == c) ? (char*)s : NULL;
}
// char* strrchr(const char* str, char c);
char* strrchr(const char* str, char c) { return NULL; } // TODO
int strcmp(const char* str1, const char* str2) {
while (*str1 != '\0' && *str1 == *str2) {
@@ -140,4 +134,3 @@ void* memset(void* dst, uint8_t value, uint32_t length) {
return dst;
}
#endif
+1 -26
View File
@@ -1,32 +1,8 @@
#ifndef SYSCALL_H
#define SYSCALL_H
#include <syscall.h>
#include <stddef.h>
#define MAX_SYSCALLS 9
typedef enum {
SYS_RESERVED,
SYS_SLEEP,
SYS_READ,
SYS_WRITE,
SYS_OPEN,
SYS_CLOSE,
SYS_MALLOC,
SYS_FREE,
SYS_SEEK,
} SYSCALL_NUMBERS;
typedef enum {
O_CREAT = 0x01, // Create if does not exist
O_RDONLY = 0x02, // Read only
O_WRONLY = 0x04, // Write only
O_RDWR = 0x08, // Read and write
O_APPEND = 0x10, // Always writes at the end (appends)
O_TRUNC = 0x20, // Truncate file size and position to 0 on Open
O_BIN = 0x40, // Read bytes, not text
} OFLAGS;
int syscall(int nr, int arg0, int arg1, int arg2) {
int ret = 0;
__asm__ volatile ("int $0x80" : "=a"(ret) : "a"(nr), "b"(arg0), "c"(arg1), "d"(arg2));
@@ -54,4 +30,3 @@ int close(int fd) {
return syscall(SYS_CLOSE, fd, 0, 0);
}
#endif
+1 -5
View File
@@ -1,11 +1,7 @@
#ifndef TIME_H
#define TIME_H
#include <time.h>
#include <syscall.h>
#include <stdint.h>
void sleep(uint32_t ms) {
syscall(SYS_SLEEP, ms, 0, 0);
}
#endif
+1
View File
@@ -93,6 +93,7 @@ void main() {
// Run Interactive Shell Program
// TODO: Make it in another file
while (1) {
printf("\e[6M\x7f\x80\n");
printf("\e[6M\e[8N%s \e[2M%s \e[8M", cwd, PROMPT);
char input[256];
char* inputPtr = input;
+1
View File
@@ -6,6 +6,7 @@
#include <fs/fs.h>
#include <screen/gfxmode.h>
#include <error/bigerr.h>
#include <stdio.h>
void bigError();