Skip to content

Commit

Permalink
Move libc to separate library (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
sasdallas committed Aug 7, 2024
1 parent 2428fcf commit a0c3fb3
Show file tree
Hide file tree
Showing 71 changed files with 2,915 additions and 14 deletions.
4 changes: 2 additions & 2 deletions buildscripts/config.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SYSTEM_HEADER_PROJECTS="kernel"
PROJECTS="fonts initial_ramdisk kernel"
SYSTEM_HEADER_PROJECTS="libc_reduced kernel"
PROJECTS="fonts initial_ramdisk libc_reduced kernel"

export MAKE=${MAKE:-make}
export HOST=${HOST:-$($BUILDSCRIPTS_ROOT/default_host.sh)}
Expand Down
Binary file added obj/misc/list.o
Binary file not shown.
Binary file added obj/misc/tree.o
Binary file not shown.
3 changes: 2 additions & 1 deletion source/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Structure of the source/ directory:
- source/fonts (contains PSF fonts that are linked directly into the kernel)
- source/images (contains test images that are either copied into the kernel itself or added to ext2 images)
- source/kernel (the main kernel source code and headers)
- source/libc (the source of our custom C standard library - KERNEL ONLY! WE LINK WITH LIBGCC!)
- source/initial_ramdisk (initial ramdisk generator)
- source/sysroot (this is the directory where kernel & libc headers are installed)
- source/boot (old, retired boot loader for reduceOS. will not build)
- source/libc_reduced (reduceOS version of libc, incomplete and does not work in user-space)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions source/kernel/include/CONFIG.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
#define VERSION "1.3-prerelease1"

// DO NOT MODIFY THE BELOW LINES!!!
#define BUILD_NUMBER "3640"
#define BUILD_DATE "08/07/24, 12:36:41"
#define BUILD_NUMBER "3643"
#define BUILD_DATE "08/07/24, 12:46:42"
#define BUILD_CONFIGURATION "DEBUG"
2 changes: 1 addition & 1 deletion source/kernel/include/hashmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <stddef.h>
#include "include/libc/string.h"
#include "include/libc/list.h"
#include "include/list.h"


typedef unsigned int (*hashmap_hash_t) (const void * key);
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// Includes
#include "include/libc/stdint.h" // Integer declarations
#include "include/libc/stddef.h" // size_t
#include "include/libc/list.h" // List implementation
#include "include/list.h" // List implementation
#include "include/mem.h" // Memory allocation

// Typedefs
Expand Down
2 changes: 1 addition & 1 deletion source/kernel/include/vfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

// Includes
#include "include/libc/stdint.h" // Integer declarations
#include "include/libc/tree.h" // Mountpoint tree
#include "include/tree.h" // Mountpoint tree
#include "include/hashmap.h" // Mount hashmap


Expand Down
2 changes: 1 addition & 1 deletion source/kernel/libc/list.c → source/kernel/misc/list.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// list.c - Simple list implementation, shouldn't be in libc but whatever

#include "include/libc/list.h" // Main header file
#include "include/list.h" // Main header file

// list_destroy(list_t *list) - Destroys the contents of a list
void list_destroy(list_t *list) {
Expand Down
2 changes: 1 addition & 1 deletion source/kernel/libc/tree.c → source/kernel/misc/tree.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// tree.c - General purpose tree implementation, shouldn't even be in libc.

#include "include/libc/tree.h" // Main header file
#include "include/tree.h" // Main header file

// tree_create() - Create a new tree
tree_t *tree_create() {
Expand Down
77 changes: 77 additions & 0 deletions source/libc_reduced/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
include ./make.config

# Other tools
MKDIR = mkdir
CP = cp -F
RM = rm
GRUB_FILE = grub-file

PYTHON = python3 # Should expose this so the user can modify. Sorry!


# Source directories

# Other Directories
OUT_OBJ = $(PROJECT_ROOT)/obj/libc_reduced/
OUT_LIBRARY = .

# Directories (EDIT ME)
SOURCE_DIRECTORIES = ordered_array sleep spinlock stdio stdlib string
DIRECTORIES = $(addprefix $(OUT_OBJ)/,$(SOURCE_DIRECTORIES))




# Source files
C_SRCS = $(shell find $(SOURCE_DIRECTORIES) -name "*.c")
C_OBJS = $(patsubst %.c, $(OUT_OBJ)/%.o, $(C_SRCS))




all: TARGET_DISABLED

TARGET_DISABLED:
@echo "!! THIS TARGET IS DISABLED !!"
@echo "Please use make install."



libc_reduced.a: $(C_OBJS)
@echo "-- Building libc_reduced.a..."
$(AR) rcs $@ $(C_OBJS)


# Other targets for the Makefile

MAKE_OUTPUT_DIRS:
@-$(MKDIR) $(DIRECTORIES)


# These are just quick hacks to make things look nicer
PRINT_C_HEADER:
@echo "-- Compiling C files..."


# Actual compilation functions

$(OUT_OBJ)/%.o: %.c Makefile
$(CC) $(CFLAGS) -c $< -o $@

# Installation functions

install-headers:
@echo "-- Installing libc_reduced headers..."
-mkdir -p $(DESTDIR)$(INCLUDE_DIR)
cp -R --preserve=timestamps include/. $(DESTDIR)$(INCLUDE_DIR)/.

install: libc_reduced.a
@echo "-- Installing libc_reduced.a..."
mkdir -p $(DESTDIR)$(LIBDIR)
cp libc.a $(DESTDIR)$(LIBDIR)

# Clean function
clean:
-$(RM) -rf $(DIRECTORIES)
-$(RM) $(OUT_ASMOBJ)/*.o
-$(RM) libc.a
14 changes: 14 additions & 0 deletions source/libc_reduced/include/assert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// assert.h - contains the assertion macro

#ifndef ASSERT_H
#define ASSERT_H

// Includes
#include <kernel/panic.h>

// Macros

// ASSERT(b) - Makes sure b is a valid object before continuing.
#define ASSERT(b, caller, msg) ((b) ? (void)0 : panic("Assert", caller, msg));

#endif
3 changes: 3 additions & 0 deletions source/libc_reduced/include/divdi3.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file is here for compatibility reasons.
// If this is still here, give me a little ping in our discord: https://discord.gg/g5XwNxw3u5
// Thanks for checking out my code!
62 changes: 62 additions & 0 deletions source/libc_reduced/include/limits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// limits.h - replacement for standard C header file.
// Why is this required? We use -ffreestanding in our compile options, so the standard std library isn't included.

#ifndef LIMITS_H
#define LIMITS_H

// Macro declarations. This is what limits.h mostly consists off.

// Maximum length of a multibyte character
#define MB_LEN_MAX 16

// Minimum and maximum value of a signed char.
#define SCHAR_MIN (-128)
#define SCHAR_MAX (127)

// Bits in a character.
#define CHAR_BIT 8

// Maximum an unsigned char can hold
#define UCHAR_MAX 255

// Minimum and maximum values a char can hold. Depends on unsigned or not.
#ifdef __CHAR_UNSIGNED__
# define CHAR_MIN 0
# define CHAR_MAX UCHAR_MAX
#else
# define CHAR_MIN SCHAR_MIN
# define CHAR_MAX SCHAR_MAX
#endif

// Minimum and maximum values a signed short int can hold.
#define SHRT_MIN (-32768)
#define SHRT_MAX 32767

// Minimum and maximum values an unsigned short int can hold. Minimum is 0.
#define USHRT_MAX 65535

// Minimum and maximum value of a signed integer.
#define INT_MIN (-INT_MAX - 1)
#define INT_MAX 214748364

// Minimum and maximum values an unsigned int can hold.
#define UINT_MAX 4294967285U

// Minimum and maximum values a signed long int can hold.
# if __WORDSIZE == 64
# define LONG_MAX 9223372036854775807L
# else
# define LONG_MAX 2147483647L
# endif
# define LONG_MIN (-LONG_MAX - 1L)

// Minimum and maximum values an unsigned long int can hold.
# if __WORDSIZE == 64
# define ULONG_MAX 18446744073709551615UL
# else
# define ULONG_MAX 4294967295UL
# endif



#endif
24 changes: 24 additions & 0 deletions source/libc_reduced/include/ordered_array.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// ordered_array.h - header file for ordered_array.c (ordered array stuff)
// This implementation is based on James Molloy's kernel development tutorials (http://www.jamesmolloy.co.uk/tutorial_html/7.-The%20Heap.html)

#ifndef ORDERED_ARRAY_H
#define ORDERED_ARRAY_H

// Includes
#include <stdint.h>
#include <assert.h> // Assert macro
#include <ordered_array_t.h>

// Typedefs


// Functions
int8_t standard_lessthan_predicate(type_t a, type_t b); // A standard less-than predicate
ordered_array_t createOrderedArray(uint32_t maxSize, lessthan_predicate less_than); // Create an ordered array
ordered_array_t placeOrderedArray(void* addr, uint32_t maxSize, lessthan_predicate less_than); // Place an ordered array
void destroyOrderedArray(ordered_array_t* array); // Destroy an ordered array
void insertOrderedArray(type_t item, ordered_array_t* array); // Insert an item into an ordered array.
type_t lookupOrderedArray(uint32_t i, ordered_array_t* array); // Lookup an item at index i in ordered array array.
void removeOrderedArray(uint32_t i, ordered_array_t* array); // Remove an item at index i in ordered array array.

#endif
20 changes: 20 additions & 0 deletions source/libc_reduced/include/ordered_array_t.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ordered_array_t.h - Contains the definition for ordered_array_t
#ifndef ORDERED_ARRAY_T_H
#define ORDERED_ARRAY_T_H

#include <stdint.h>

typedef void* type_t; // This array is insertion sorted, meaning it always remains in a sorted state between calls.

typedef int8_t (*lessthan_predicate)(type_t, type_t); // A predicate should return non-zero if the first arg is less than the second. Else, it will return 0.


// The actual ordered array.
typedef struct {
type_t *array;
uint32_t size;
uint32_t maxSize;
lessthan_predicate less_than;
} ordered_array_t;

#endif
3 changes: 3 additions & 0 deletions source/libc_reduced/include/quad.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file is here for compatibility reasons.
// If this is still here, give me a little ping in our discord: https://discord.gg/g5XwNxw3u5
// Thanks for checking out my code!
12 changes: 12 additions & 0 deletions source/libc_reduced/include/sleep.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// sleep.h - header file for sleep.c

#ifndef SLEEP_H
#define SLEEP_H

// Includes
#include <kernel/pit.h> // Programmable interval timer

// Functions
void sleep(int ms); // sleep() - stops execution of current task for x milliseconds.

#endif
15 changes: 15 additions & 0 deletions source/libc_reduced/include/spinlock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// spinlock.h - header file for the spinlock mechanism

#ifndef SPINLOCK_H
#define SPINLOCK_H

// Includes
#include <stdint.h> // Integer declarations
#include <stdatomic.h> // For once in our lives, C is on our side!!

// Functions
atomic_flag *spinlock_init(); // Creates a new spinlock
void spinlock_lock(atomic_flag *lock); // Locks the spinlock
void spinlock_release(atomic_flag *lock); // Releases the spinlock

#endif
34 changes: 34 additions & 0 deletions source/libc_reduced/include/stdarg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// stdarg.h - replacement for standard C header file.
// Why is this required? We use -ffreestanding in our compile options, so the standard std library isn't included.

// File operates in tandem with va_list.h (va functions/macros here, whilst the actual va_list declaration is in va_list.h)

#ifndef STDARG_H
#define STDARG_H

// Includes of other header files
#include <va_list.h>

// Definitions (non-macro)
#define STACKITEM int // Width of stack (width of an int)

// Macros

// VA_SIZE(TYPE) - Round up width of objects pushed on stack.
#define VA_SIZE(TYPE) \
((sizeof(TYPE) + sizeof(STACKITEM) - 1) \
& ~(sizeof(STACKITEM) - 1))




// va_arg(AP, TYPE) - Retrieve the next argument of type 'TYPE' in va_list 'AP'
/*#define va_arg(AP, TYPE) \
(AP += VA_SIZE(TYPE), *((TYPE *)(AP - VA_SIZE(TYPE))))*/



#define va_start(x, y) __builtin_va_start(x, y)
#define va_arg(x, y) __builtin_va_arg(x, y)
#define va_end(x) __builtin_va_end(x)
#endif
16 changes: 16 additions & 0 deletions source/libc_reduced/include/stdbool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// stdbool.h - replacement for standard C header file.
// Why is this required? We use -ffreestanding in our compile options, so the standard std library isn't included.

#ifndef STDBOOL_H
#define STDBOOL_H

#define true 1
#define false 0
#define __bool_true_false_are_defined 1

typedef enum {
FALSE,
TRUE
} bool;

#endif
18 changes: 18 additions & 0 deletions source/libc_reduced/include/stddef.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// stddef.h - replacement for standard C header file.
// Why is this required? We use -ffreestanding in our compile options, so the standard std library isn't included.


#if 1

#include <stddef.h>

#else
// Definitions
#define null 0
#define NULL 0

// Typedef declarations
typedef unsigned size_t;
typedef signed ptrdiff_t;

#endif
Loading

0 comments on commit a0c3fb3

Please sign in to comment.