diff options
Diffstat (limited to 'lib/libc')
42 files changed, 2603 insertions, 8 deletions
diff --git a/lib/libc/Makefile b/lib/libc/Makefile index 49e7329..b5bd01e 100644 --- a/lib/libc/Makefile +++ b/lib/libc/Makefile @@ -1,6 +1,6 @@ -CFLAGS = -c -fno-stack-protector -nostdlib -static -Iinclude/ +CFLAGS = -c -fno-stack-protector -nostdlib -static -Iinclude/ -D_OLIBC LIBC_CFILES = $(shell find src/ -name "*.c") -LIBC_ASMFILES = $(shell find src/ -name "*.S") +LIBC_ASMFILES = $(shell find src/arch/$(ARCH) -name "*.S") LIBC_OBJ = $(LIBC_CFILES:.c=.o) LIBC_ASMOBJ = $(LIBC_ASMFILES:.S=.S.o) diff --git a/lib/libc/include/arch/aarch64/syscall.h b/lib/libc/include/arch/aarch64/syscall.h new file mode 100644 index 0000000..84a51e0 --- /dev/null +++ b/lib/libc/include/arch/aarch64/syscall.h @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _MACHINE_SYSCALL_H_ +#define _MACHINE_SYSCALL_H_ + +#if !defined(__ASSEMBLER__) +__always_inline static inline long +syscall0(uint64_t code) +{ + return 0; +} + +__always_inline static inline long +syscall1(uint64_t code, uint64_t arg0) +{ + return 0; +} + +__always_inline static long inline +syscall2(uint64_t code, uint64_t arg0, uint64_t arg1) +{ + return 0; +} + +__always_inline static inline long +syscall3(uint64_t code, uint64_t arg0, uint64_t arg1, uint64_t arg2) +{ + return 0; +} + +__always_inline static inline long +syscall4(uint64_t code, uint64_t arg0, uint64_t arg1, uint64_t arg2, uint64_t arg3) +{ + return 0; +} + +__always_inline static inline long +syscall5(uint64_t code, uint64_t arg0, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4) +{ + return 0; +} + +__always_inline static inline long +syscall6(uint64_t code, uint64_t arg0, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5) +{ + return 0; +} + +#define _SYSCALL_N(a0, a1, a2, a3, a4, a5, a6, name, ...) \ + name + +#define syscall(...) \ +_SYSCALL_N(__VA_ARGS__, syscall6, syscall5, \ + syscall4, syscall3, syscall2, syscall1, \ + syscall0)(__VA_ARGS__) + +#endif /* !__ASSEMBLER__ */ +#endif /* !_MACHINE_SYSCALL_H_ */ diff --git a/lib/libc/include/stdarg.h b/lib/libc/include/stdarg.h new file mode 100644 index 0000000..dc75475 --- /dev/null +++ b/lib/libc/include/stdarg.h @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _STDARG_H +#define _STDARG_H 1 + +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L +#define __STDC_VERSION_STDARG_H__ 202311L +#endif + +/* Determine which definitions are needed */ +#if !defined(__need___va_list) && !defined(__need_va_list) && \ + !defined(__need_va_arg) && \ + !defined(__need___va_copy) && !defined(__need_va_copy) +#define __need___va_list +#define __need_va_list +#define __need_va_arg +#define __need___va_copy +#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || (defined(__cplusplus) && __cplusplus >= 201103L) +#define __need_va_copy +#endif +#endif + +/* __gnuc_va_list type */ +#ifdef __need___va_list +#ifndef __GNUC_VA_LIST +#define __GNUC_VA_LIST +typedef __builtin_va_list __gnuc_va_list; +#endif /* !__GNUC_VA_LIST */ +#undef __need___va_list +#endif /* __need___va_list */ + +/* va_list type */ +#ifdef __need_va_list +#ifndef _VA_LIST +#define _VA_LIST +typedef __builtin_va_list va_list; +#endif /* !_VA_LIST */ +#undef __need_va_list +#endif /* __need_va_list */ + +/* va_start(), va_end(), and va_arg() macros */ +#ifdef __need_va_arg +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L +#define va_start(ap, ...) __builtin_va_start(ap, 0) +#else +#define va_start(ap, arg) __builtin_va_start(ap, arg) +#endif +#define va_end(ap) __builtin_va_end(ap) +#define va_arg(ap, type) __builtin_va_arg(ap, type) +#undef __need_va_arg +#endif /* __need_va_arg */ + +/* __va_copy() macro */ +#ifdef __need___va_copy +#define __va_copy(dest, src) __builtin_va_copy(dest, src) +#undef __need___va_copy +#endif /* __need___va_copy */ + +/* va_copy() macro */ +#ifdef __need_va_copy +#define va_copy(dest, src) __builtin_va_copy(dest, src) +#undef __need_va_copy +#endif /* __need_va_copy */ + +#endif /* !_STDARG_H */ diff --git a/lib/libc/include/stdatomic.h b/lib/libc/include/stdatomic.h new file mode 100644 index 0000000..3f80270 --- /dev/null +++ b/lib/libc/include/stdatomic.h @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _STDATOMIC_H +#define _STDATOMIC_H 1 + +#include <stddef.h> +#include <stdint.h> + +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L +#define __STDC_VERSION_STDATOMIC_H__ 202311L +#endif + +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L + +#define kill_dependency(y) (y) + +#if (defined(__STDC_VERSION__) && __STDC_VERSION__ < 202311L) || defined(__cplusplus) +/* Deprecated in C17, removed in C23 */ +#define ATOMIC_VAR_INIT(value) (value) +#endif + +#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) || defined(__cplusplus) +#define ATOMIC_FLAG_INIT { false } +typedef _Atomic(bool) atomic_bool; +#else +#define ATOMIC_FLAG_INIT { 0 } +typedef _Atomic(_Bool) atomic_bool; +#endif + +typedef _Atomic(signed char) atomic_schar; + +typedef _Atomic(char) atomic_char; +typedef _Atomic(short) atomic_short; +typedef _Atomic(int) atomic_int; +typedef _Atomic(long) atomic_long; +typedef _Atomic(long long) atomic_llong; + +typedef _Atomic(unsigned char) atomic_uchar; +typedef _Atomic(unsigned short) atomic_ushort; +typedef _Atomic(unsigned int) atomic_uint; +typedef _Atomic(unsigned long) atomic_ulong; +typedef _Atomic(unsigned long long) atomic_ullong; + +typedef _Atomic(uintptr_t) atomic_uintptr_t; +typedef _Atomic(size_t) atomic_size_t; + +typedef struct atomic_flag { + atomic_bool _Value; +} atomic_flag; + +typedef enum memory_order { + memory_order_relaxed = __ATOMIC_RELAXED, + memory_order_consume = __ATOMIC_CONSUME, + memory_order_acquire = __ATOMIC_ACQUIRE, + memory_order_release = __ATOMIC_RELEASE, + memory_order_acq_rel = __ATOMIC_ACQ_REL, + memory_order_seq_cst = __ATOMIC_SEQ_CST +} memory_order; + +#define atomic_is_lock_free(obj) __atomic_is_lock_free(sizeof(*(obj)), 0) +#define atomic_flag_test_and_set(obj) __atomic_test_and_set((obj), memory_order_seq_cst) +#define atomic_flag_clear(obj) __atomic_clear((obj), memory_order_seq_cst) +#define atomic_load(obj) __atomic_load_n((obj), memory_order_seq_cst) +#define atomic_store(obj, desired) __atomic_store_n((obj), (desired), memory_order_seq_cst) +#define atomic_exchange(obj, desired) __atomic_exchange_n((obj), (desired), memory_order_seq_cst) +#define atomic_fetch_add(obj, arg) __atomic_fetch_add((obj), (arg), memory_order_seq_cst) +#define atomic_fetch_sub(obj, arg) __atomic_fetch_sub((obj), (arg), memory_order_seq_cst) +#define atomic_fetch_and(obj, arg) __atomic_fetch_and((obj), (arg), memory_order_seq_cst) +#define atomic_fetch_or(obj, arg) __atomic_fetch_or((obj), (arg), memory_order_seq_cst) +#define atomic_fetch_xor(obj, arg) __atomic_fetch_xor((obj), (arg), memory_order_seq_cst) + +#define atomic_signal_fence __atomic_signal_fence +#define atomic_thread_fence __atomic_thread_fence +#define atomic_flag_test_and_set_explicit __atomic_test_and_set +#define atomic_flag_clear_explicit __atomic_clear +#define atomic_load_explicit __atomic_load_n +#define atomic_store_explicit __atomic_store_n +#define atomic_exchange_explicit __atomic_exchange_n +#define atomic_fetch_add_explicit __atomic_fetch_add +#define atomic_fetch_sub_explicit __atomic_fetch_sub +#define atomic_fetch_and_explicit __atomic_fetch_and +#define atomic_fetch_or_explicit __atomic_fetch_or +#define atomic_fetch_xor_explicit __atomic_fetch_xor + +#define atomic_compare_exchange_strong(obj, expected, desired) __atomic_compare_exchange_n((obj), (expected), (desired), false, memory_order_seq_cst, memory_order_seq_cst) +#define atomic_compare_exchange_weak(obj, expected, desired) __atomic_compare_exchange_n((obj), (expected), (desired), true, memory_order_seq_cst, memory_order_seq_cst) +#define atomic_compare_exchange_strong_explicit __atomic_compare_exchange_n +#define atomic_compare_exchange_weak_explicit __atomic_compare_exchange_n + +#endif + +#endif /* !_STDATOMIC_H */ diff --git a/lib/libc/include/stddef.h b/lib/libc/include/stddef.h index 557f69b..642f773 100644 --- a/lib/libc/include/stddef.h +++ b/lib/libc/include/stddef.h @@ -28,18 +28,157 @@ */ #ifndef _STDDEF_H -#define _STDDEF_H +#define _STDDEF_H 1 -#include <sys/types.h> +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L +#define __STDC_VERSION_STDDEF_H__ 202311L +#endif + +/* Determine which definitions are needed */ +#if !defined(__need_NULL) && !defined(__need_nullptr_t) && \ + !defined(__need_size_t) && !defined(__need_rsize_t) && \ + !defined(__need_wchar_t) && !defined(__need_wint_t) && \ + !defined(__need_ptrdiff_t) && !defined(__need_max_align_t) && \ + !defined(__need_offsetof) && !defined(__need_unreachable) +#define __need_NULL +#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) || defined(__cplusplus) +#define __need_nullptr_t +#endif +#define __need_ptrdiff_t +#define __need_size_t +#if defined(__STDC_WANT_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__ >= 1 +#define __need_rsize_t +#endif +#define __need_wchar_t +#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || (defined(__cplusplus) && __cplusplus >= 201103L) +#define __need_max_align_t +#endif +#define __need_offsetof +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L +#define __need_unreachable +#endif +#endif +/* NULL pointer constant */ +#ifdef __need_NULL +#ifdef __cplusplus #if __cplusplus >= 201103L #define NULL nullptr -#elif defined(__cplusplus) +#else #define NULL 0L +#endif /* __cplusplus >= 201103L */ #else -#define NULL ((void *) 0) +#define NULL ((void *) 0) +#endif /* __cplusplus */ +#undef __need_NULL +#endif /* __need_NULL */ + +/* nullptr_t type */ +#ifdef __need_nullptr_t +#ifndef _NULLPTR_T +#define _NULLPTR_T +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L +typedef typeof(nullptr) nullptr_t; #endif +#endif /* !_NULLPTR_T */ +#undef __need_nullptr_t +#endif /* __need_nullptr_t */ + +/* size_t type */ +#ifdef __need_size_t +#ifndef _SIZE_T +#define _SIZE_T +#ifdef __SIZE_TYPE__ +typedef __SIZE_TYPE__ size_t; +#else +typedef long unsigned int size_t; +#endif /* __SIZE_TYPE__ */ +#endif /* !_SIZE_T */ +#undef __need_size_t +#endif /* __need_size_t */ + +/* rsize_t type */ +#ifdef __need_rsize_t +#ifndef _RSIZE_T +#define _RSIZE_T +#ifdef __SIZE_TYPE__ +typedef __SIZE_TYPE__ rsize_t; +#else +typedef long unsigned int rsize_t; +#endif /* __SIZE_TYPE__ */ +#endif /* !_RSIZE_T */ +#undef __need_rsize_t +#endif /* __need_rsize_t */ -typedef __size_t size_t; +/* wchar_t type */ +#ifdef __need_wchar_t +#ifndef _WCHAR_T +#define _WCHAR_T +#ifdef __WCHAR_TYPE__ +typedef __WCHAR_TYPE__ wchar_t; +#else +typedef int wchar_t; +#endif /* __WCHAR_TYPE__ */ +#endif /* !_WCHAR_T */ +#undef __need_wchar_t +#endif /* __need_wchar_t */ + +/* wint_t type */ +#ifdef __need_wint_t +#ifndef _WINT_T +#define _WINT_T +#ifdef __WINT_TYPE__ +typedef __WINT_TYPE__ wint_t; +#else +typedef unsigned int wint_t; +#endif /* __WINT_TYPE__ */ +#endif /* !_WINT_T */ +#undef __need_wint_t +#endif /* __need_wint_t */ + +/* ptrdiff_t type */ +#ifdef __need_ptrdiff_t +#ifndef _PTRDIFF_T +#define _PTRDIFF_T +#ifdef __PTRDIFF_TYPE__ +typedef __PTRDIFF_TYPE__ ptrdiff_t; +#else +typedef long int ptrdiff_t; +#endif /* __PTRDIFF_TYPE__ */ +#endif /* !_PTRDIFF_T */ +#undef __need_ptrdiff_t +#endif /* __need_ptrdiff_t */ + +/* max_align_t type */ +#ifdef __need_max_align_t +#if defined (_MSC_VER) +typedef double max_align_t; +#elif defined(__APPLE__) +typedef long double max_align_t; +#else +typedef struct { + long long __longlong __attribute__((__aligned__(__alignof__(long long)))); + long double __longdouble __attribute__((__aligned__(__alignof__(long double)))); +} max_align_t; +#endif +#undef __need_max_align_t +#endif /* __need_max_align_t */ + +/* offsetof() macro */ +#ifdef __need_offsetof +#ifndef offsetof +#define offsetof(type, member) __builtin_offsetof(type, member) +#endif +#undef __need_offsetof +#endif /* __need_offsetof */ + +/* unreachable() macro */ +#ifdef __need_unreachable +/* C++ has std::unreachable() */ +#if !defined(__cplusplus) && !defined(unreachable) +#define unreachable() __builtin_unreachable() +#endif +#undef __need_unreachable +#endif /* __need_unreachable */ #endif /* !_STDDEF_H */ diff --git a/lib/libc/include/stdio.h b/lib/libc/include/stdio.h new file mode 100644 index 0000000..7d8279d --- /dev/null +++ b/lib/libc/include/stdio.h @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _STDIO_H +#define _STDIO_H 1 + +#include <sys/cdefs.h> +#define __need_NULL +#define __need_size_t +#include <stddef.h> +#define __need_va_list +#include <stdarg.h> + +#if __STDC_VERSION__ >= 202311L +#define __STDC_VERSION_STDIO_H__ 202311L +#endif + +/* Buffering modes */ +#define _IOFBF 0 /* Fully buffered */ +#define _IOLBF 1 /* Line buffered */ +#define _IONBF 2 /* Unbuffered */ + +/* Default buffer size */ +#define BUFSIZ 256 + +/* End-Of-File indicator */ +#define EOF (-1) + +/* Spec says these should be defined as macros */ +#define stdin stdin +#define stdout stdout +#define stderr stderr + +/* File structure */ +typedef struct _IO_FILE { + int fd; + int buf_mode; +} FILE; + +extern FILE *stdin; +extern FILE *stdout; +extern FILE *stderr; + +#define putc(c, stream) fputc((c), (stream)) +#define getc(stream) fgetc((stream)) + +__BEGIN_DECLS + +size_t fread(void *__restrict ptr, size_t size, size_t n, FILE *__restrict stream); +size_t fwrite(const void *__restrict ptr, size_t size, size_t n, FILE *__restrict stream); +char *fgets(char *__restrict s, int size, FILE *__restrict stream); + +FILE *fopen(const char *__restrict path, const char *__restrict mode); +int fclose(FILE *stream); + +int vsnprintf(char *s, size_t size, const char *fmt, va_list ap); +int snprintf(char *s, size_t size, const char *fmt, ...); + +int printf(const char *__restrict fmt, ...); +int fileno(FILE *stream); +int fputc(int c, FILE *stream); + +int putchar(int c); +int fgetc(FILE *stream); +int getchar(void); + +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L +int fputs(const char *__restrict s, FILE *__restrict stream); +#else +int fputs(const char *s, FILE *stream); +#endif +int puts(const char *s); + +__END_DECLS + +#endif /* !_STDIO_H */ diff --git a/lib/libc/include/stdlib.h b/lib/libc/include/stdlib.h new file mode 100644 index 0000000..b1de3f3 --- /dev/null +++ b/lib/libc/include/stdlib.h @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _STDLIB_H +#define _STDLIB_H 1 + +/* For __dead */ +#include <sys/cdefs.h> + +/* Get specific definitions from stddef.h */ +#define __need_NULL +#define __need_size_t +#define __need_wchar_t +#include <stddef.h> + +#if __STDC_VERSION__ >= 202311L +#define __STDC_VERSION_STDLIB_H__ 202311L +#endif + +#define EXIT_SUCCESS 0 +#define EXIT_FAILURE 1 + +typedef struct { + int quot; + int rem; +} div_t; + +#ifndef __ldiv_t_defined +typedef struct { + long int quot; + long int rem; +} ldiv_t; +#define __ldiv_t_defined 1 +#endif /* !__ldiv_t_defined */ + +#ifndef __lldiv_t_defined +typedef struct { + long long int quot; + long long int rem; +} lldiv_t; +#define __lldiv_t_defined 1 +#endif /* !__lldiv_t_defined */ + +__BEGIN_DECLS + +#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) || (defined(__cplusplus) && __cplusplus >= 201103L) +[[noreturn]] void abort(void); +[[noreturn]] void exit(int status); +[[noreturn]] void _Exit(int status); +#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L +_Noreturn void abort(void); +_Noreturn void exit(int status); +_Noreturn void _Exit(int status); +#else +__dead void abort(void); +__dead void exit(int status); +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L +__dead void _Exit(int status); +#endif +#endif + +void *malloc(size_t size); +void *realloc(void *ptr, size_t size); +void free(void *ptr); + +void srand(unsigned int r); +int rand(void); + +__END_DECLS + +#endif /* !_STDLIB_H */ diff --git a/lib/libc/include/string.h b/lib/libc/include/string.h index d1613e4..4ac384b 100644 --- a/lib/libc/include/string.h +++ b/lib/libc/include/string.h @@ -31,9 +31,15 @@ #define _STRING_H_ 1 #include <stddef.h> +#include <stdint.h> size_t strlen(const char *s); +void *memset(void *dst, int c, size_t n); int memcmp(const void *s1, const void *s2, size_t n); + +char *itoa(int64_t value, char *buf, int base); +void *memcpy(void *dest, const void *src, size_t n); int strcmp(const char *s1, const char *s2); +int atoi(char *s); #endif /* !_STRING_H_ */ diff --git a/lib/libc/include/time.h b/lib/libc/include/time.h new file mode 100644 index 0000000..afb8adc --- /dev/null +++ b/lib/libc/include/time.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _TIME_H_ +#define _TIME_H_ 1 + +#include <sys/time.h> + +int sleep(struct timespec *__restrict tsp, struct timespec *__restrict remp); + +#endif /* !_TIME_H_ */ diff --git a/lib/libc/include/unistd.h b/lib/libc/include/unistd.h index 0a8c429..8c82eb8 100644 --- a/lib/libc/include/unistd.h +++ b/lib/libc/include/unistd.h @@ -30,15 +30,27 @@ #ifndef _UNISTD_H #define _UNISTD_H +#include <sys/exec.h> #include <sys/types.h> #include <sys/cdefs.h> #include <stddef.h> +#define F_OK 0 + +/* lseek whence, follows Hyra ABI */ +#define SEEK_SET 0 +#define SEEK_CUR 1 +#define SEEK_END 2 + __BEGIN_DECLS +int sysconf(int name); ssize_t read(int fd, void *buf, size_t count); ssize_t write(int fd, const void *buf, size_t count); + int close(int fd); +int access(const char *path, int mode); +off_t lseek(int fildes, off_t offset, int whence); __END_DECLS diff --git a/lib/libc/src/arch/aarch64/crti.S b/lib/libc/src/arch/aarch64/crti.S new file mode 100644 index 0000000..d95220b --- /dev/null +++ b/lib/libc/src/arch/aarch64/crti.S @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + + .text + .globl _start +_start: + mov fp, xzr + mov x0, sp +1: b 1b diff --git a/lib/libc/src/hyra/inject.c b/lib/libc/src/hyra/inject.c new file mode 100644 index 0000000..b1fd7dc --- /dev/null +++ b/lib/libc/src/hyra/inject.c @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/syscall.h> +#include <sys/errno.h> +#include <sys/krq.h> +#include <stdlib.h> + +/* + * Inject a kernel runtime quantum + * + * @path: NULL for all builtin but deferrable drivers. + * They are not initialized more than once but + * can be send up through this routine. + */ +int +inject(const char *path) +{ + /* TODO: Support this */ + if (path != NULL) { + return -EINVAL; + } + + return syscall(SYS_inject, (uintptr_t)path); +} diff --git a/lib/libc/src/hyra/mmap.c b/lib/libc/src/hyra/mmap.c new file mode 100644 index 0000000..6870c75 --- /dev/null +++ b/lib/libc/src/hyra/mmap.c @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/syscall.h> +#include <sys/mman.h> +#include <sys/types.h> + +void * +mmap(void *addr, size_t len, int prot, int flags, + int fildes, off_t off) + +{ + return (void *)syscall(SYS_mmap, (uintptr_t)addr, len, + prot, flags, fildes, off); +} + +int +munmap(void *addr, size_t len) +{ + return syscall(SYS_munmap, (uintptr_t)addr, len); +} diff --git a/lib/libc/src/hyra/reboot.c b/lib/libc/src/hyra/reboot.c new file mode 100644 index 0000000..1bb8faa --- /dev/null +++ b/lib/libc/src/hyra/reboot.c @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/reboot.h> +#include <sys/syscall.h> + +/* + * Reboot or poweroff the machine. + */ +void +cpu_reboot(int method) +{ + syscall(SYS_reboot, method); +} diff --git a/lib/libc/src/hyra/sleep.c b/lib/libc/src/hyra/sleep.c new file mode 100644 index 0000000..14830f6 --- /dev/null +++ b/lib/libc/src/hyra/sleep.c @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/syscall.h> +#include <time.h> + +/* + * Sleep using a timespec value + * + * @tsp: Timespec to sleep with + * @remp: Remaining time if interrupted during sleep + */ +int +sleep(struct timespec *__restrict tsp, struct timespec *__restrict remp) +{ + return syscall(SYS_sleep, (uintptr_t)tsp, (uintptr_t)remp); +} diff --git a/lib/libc/src/hyra/spawn.c b/lib/libc/src/hyra/spawn.c new file mode 100644 index 0000000..b4c92ef --- /dev/null +++ b/lib/libc/src/hyra/spawn.c @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/spawn.h> +#include <sys/syscall.h> +#include <sys/types.h> + +/* + * Spawn a process + * + * @pathname: Path to executable. + * @argv: Argument vector + * @envp: Environment vector + * @flags: Spawn flags. + */ +pid_t +spawn(const char *pathname, char **argv, char **envp, int flags) +{ + return syscall(SYS_spawn, (uintptr_t)pathname, (uintptr_t)argv, + (uintptr_t)envp, flags); +} diff --git a/lib/libc/src/hyra/stat.c b/lib/libc/src/hyra/stat.c new file mode 100644 index 0000000..42a353b --- /dev/null +++ b/lib/libc/src/hyra/stat.c @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/stat.h> +#include <sys/syscall.h> + +int +stat(const char *path, struct stat *buf) +{ + return syscall(SYS_stat, (uintptr_t)path, (uintptr_t)buf); +} diff --git a/lib/libc/src/main.c b/lib/libc/src/main.c index 1154b21..02a648b 100644 --- a/lib/libc/src/main.c +++ b/lib/libc/src/main.c @@ -27,13 +27,55 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#include <sys/exec.h> #include <stdint.h> #include <stddef.h> +extern int __libc_stdio_init(void); +extern int __malloc_mem_init(void); +uint64_t __libc_auxv[_AT_MAX]; + int main(int argc, char **argv); +struct auxv_entry { + uint64_t tag; + uint64_t val; +}; + int __libc_entry(uint64_t *ctx) { - return main(0, NULL); + const struct auxv_entry *auxvp; + int status; + uint64_t argc, envc, tag; + char **argv; + char **envp; + + argc = *ctx; + argv = (char **)(ctx + 1); + envp = (char **)(argv + argc + 1); + + envc = 0; + while (envp[envc] != NULL) { + ++envc; + } + + auxvp = (void *)(envp + envc + 1); + for (int i = 0; i < _AT_MAX; ++i) { + if (auxvp->tag == AT_NULL) { + break; + } + if (auxvp->tag < _AT_MAX) { + __libc_auxv[auxvp->tag] = auxvp->val; + } + + ++auxvp; + } + + if ((status = __libc_stdio_init()) != 0) { + return status; + } + + __malloc_mem_init(); + return main(argc, argv); } diff --git a/lib/libc/src/stdio/fclose.c b/lib/libc/src/stdio/fclose.c new file mode 100644 index 0000000..7628973 --- /dev/null +++ b/lib/libc/src/stdio/fclose.c @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/types.h> +#include <sys/errno.h> +#include <stdlib.h> +#include <stdio.h> +#include <unistd.h> + +int +fclose(FILE *stream) +{ + int retval; + + if (stream == NULL) { + return -EBADF; + } + + retval = close(stream->fd); + free(stream); + return retval; +} diff --git a/lib/libc/src/stdio/fgetc.c b/lib/libc/src/stdio/fgetc.c new file mode 100644 index 0000000..2ed8496 --- /dev/null +++ b/lib/libc/src/stdio/fgetc.c @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdio.h> +#include <stdint.h> + +extern size_t __stdio_read(void *__restrict ptr, size_t size, FILE *__restrict stream); + +int +fgetc(FILE *stream) +{ + uint16_t val; + + if (stream == NULL) { + return EOF; + } + + if (__stdio_read(&val, sizeof(val), stream) != sizeof(val)) { + return EOF; + } + + return (int)val; +} + +int +getchar(void) +{ + return fgetc(stdin); +} diff --git a/lib/libc/src/stdio/fgets.c b/lib/libc/src/stdio/fgets.c new file mode 100644 index 0000000..fdaad84 --- /dev/null +++ b/lib/libc/src/stdio/fgets.c @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdio.h> +#include <stdint.h> + +extern size_t __stdio_read(void *__restrict ptr, size_t size, FILE *__restrict stream); + +char * +fgets(char *__restrict s, int size, FILE *__restrict stream) +{ + size_t count; + uint8_t tmp; + int idx = 0; + char c; + + if (stream == NULL) { + return NULL; + } + + for (;;) { + count = __stdio_read(&tmp, sizeof(tmp), stream); + if (count <= 0 && idx == 0) { + s[idx] = '\0'; + return NULL; + } + if (count <= 0 && idx > 0) { + s[idx] = '\0'; + break; + } + + c = (char)tmp; + s[idx++] = c; + + if (c == '\n') { + s[idx] = '\0'; + break; + } + + /* Did we read the entire line? */ + if (idx >= size - 1) { + return s; + } + } + + return s; +} diff --git a/lib/libc/src/stdio/file.c b/lib/libc/src/stdio/file.c new file mode 100644 index 0000000..5c84c35 --- /dev/null +++ b/lib/libc/src/stdio/file.c @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdio.h> + +int +fileno(FILE *stream) +{ + return stream->fd; +} diff --git a/lib/libc/src/stdio/fopen.c b/lib/libc/src/stdio/fopen.c new file mode 100644 index 0000000..c15dace --- /dev/null +++ b/lib/libc/src/stdio/fopen.c @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/types.h> +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <fcntl.h> + +FILE * +fopen(const char *__restrict path, const char *__restrict mode) +{ + FILE *fhand; + int fd; + mode_t seal = 0; + + if (path == NULL || mode == NULL) { + return NULL; + } + + /* Create the seal from mode string */ + if (strcmp(mode, "r") == 0) { + seal |= O_RDONLY; + } else if (strcmp(mode, "w") == 0) { + seal |= (O_WRONLY | O_CREAT); + } else if (strcmp(mode, "r+") == 0) { + seal |= O_RDWR; + } else { + return NULL; + } + + /* Try to open the file descriptor */ + fd = open(path, seal); + if (fd < 0) { + return NULL; + } + + fhand = malloc(sizeof(*fhand)); + if (fhand == NULL) { + return NULL; + } + + fhand->fd = fd; + fhand->buf_mode = _IONBF; + return fhand; +} diff --git a/lib/libc/src/stdio/fputc.c b/lib/libc/src/stdio/fputc.c new file mode 100644 index 0000000..6ac7aac --- /dev/null +++ b/lib/libc/src/stdio/fputc.c @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdio.h> + +extern size_t __stdio_write(const void *__restrict ptr, size_t size, FILE *__restrict stream); + +int +fputc(int c, FILE *stream) +{ + unsigned char val; + + if (stream == NULL) { + return EOF; + } + + val = (unsigned char)c; + if (__stdio_write(&val, sizeof(val), stream) != sizeof(val)) { + return EOF; + } + + return (int)val; +} + +int +putchar(int c) +{ + return fputc(c, stdout); +} diff --git a/lib/libc/src/stdio/fputs.c b/lib/libc/src/stdio/fputs.c new file mode 100644 index 0000000..357fd52 --- /dev/null +++ b/lib/libc/src/stdio/fputs.c @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdio.h> +#include <string.h> + +extern size_t __stdio_write(const void *__restrict ptr, size_t size, FILE *__restrict stream); + +int +fputs(const char *__restrict s, FILE *__restrict stream) +{ + size_t len; + + if (s == NULL || stream == NULL) { + return EOF; + } + + len = strlen(s); + if (len < 1) { + return 0; + } + + if (__stdio_write(s, sizeof(char) * len, stream) != (sizeof(char) * len)) { + return EOF; + } + + return 0; +} + +int +puts(const char *s) +{ + if (fputs(s, stdout) < 0) { + return EOF; + } + + if (fputc('\n', stdout) != '\n') { + return EOF; + } + + return 0; +} diff --git a/lib/libc/src/stdio/fread.c b/lib/libc/src/stdio/fread.c new file mode 100644 index 0000000..036962d --- /dev/null +++ b/lib/libc/src/stdio/fread.c @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdio.h> +#include <unistd.h> + +size_t +__stdio_read(void *__restrict ptr, size_t size, FILE *__restrict stream) +{ + ssize_t count; + + if (stream->buf_mode == _IONBF) { + if ((count = read(stream->fd, ptr, size)) < 0) { + return 0; + } + + return count; + } + + /* + * TODO: Implement more buffering modes. + */ + + return 0; +} + +size_t +fread(void *__restrict ptr, size_t size, size_t n, FILE *__restrict stream) +{ + if (ptr == NULL || stream == NULL || (size * n) == 0) { + return 0; + } + + return __stdio_read(ptr, size * n, stream); +} diff --git a/lib/libc/src/stdio/fwrite.c b/lib/libc/src/stdio/fwrite.c new file mode 100644 index 0000000..660034e --- /dev/null +++ b/lib/libc/src/stdio/fwrite.c @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdio.h> +#include <unistd.h> + +size_t +__stdio_write(const void *__restrict ptr, size_t size, FILE *__restrict stream) +{ + ssize_t count; + + if (stream->buf_mode == _IONBF) { + if ((count = write(stream->fd, ptr, size)) < 0) { + return 0; + } + + return count; + } + + /* + * TODO: Implement more buffering modes. + */ + + return 0; +} + +size_t +fwrite(const void *__restrict ptr, size_t size, size_t n, FILE *__restrict stream) +{ + if (ptr == NULL || stream == NULL || (size * n) == 0) { + return 0; + } + + return __stdio_write(ptr, size * n, stream); +} diff --git a/lib/libc/src/stdio/init.c b/lib/libc/src/stdio/init.c new file mode 100644 index 0000000..5982d59 --- /dev/null +++ b/lib/libc/src/stdio/init.c @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdio.h> +#include <fcntl.h> + +FILE *stdin; +FILE *stdout; +FILE *stderr; + +static FILE cin, cout, cerr; + +int +__libc_stdio_init(void) +{ + int cfd; + + if ((cfd = open("/dev/console", O_RDWR)) < 0) { + return cfd; + } + + cin.buf_mode = _IONBF; + cin.fd = cfd; + + cout.buf_mode = _IONBF; + cout.fd = cfd; + + cerr.buf_mode = _IONBF; + cerr.fd = cfd; + + stdout = &cout; + stdin = &cin; + stderr = &cerr; + + return 0; +} diff --git a/lib/libc/src/stdio/snprintf.c b/lib/libc/src/stdio/snprintf.c new file mode 100644 index 0000000..2387950 --- /dev/null +++ b/lib/libc/src/stdio/snprintf.c @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/types.h> +#include <stdio.h> +#include <stddef.h> +#include <unistd.h> + +/* TODO FIXME: Use stdarg.h */ +#define __va_start(ap, fmt) __builtin_va_start(ap, fmt) +#define __va_end(ap) __builtin_va_end(ap) + +int +snprintf(char *s, size_t size, const char *fmt, ...) +{ + va_list ap; + int ret; + + __va_start(ap, fmt); + ret = vsnprintf(s, size, fmt, ap); + __va_end(ap); + return ret; +} + +int +printf(const char *__restrict fmt, ...) +{ + char buf[512]; + va_list ap; + int ret; + + __va_start(ap, fmt); + ret = vsnprintf(buf, sizeof(buf), fmt, ap); + write(stdout->fd, buf, ret); + __va_end(ap); + return ret; +} diff --git a/lib/libc/src/stdio/vsnprintf.c b/lib/libc/src/stdio/vsnprintf.c new file mode 100644 index 0000000..24b2df6 --- /dev/null +++ b/lib/libc/src/stdio/vsnprintf.c @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/types.h> +#include <stdio.h> +#include <stddef.h> +#include <string.h> + +/* TODO FIXME: Use stdarg.h */ +#define __va_arg(ap, type) __builtin_va_arg(ap, type) + +static inline void +printc(char *buf, size_t size, size_t *off, char c) +{ + if (*off < size - 1) { + buf[(*off)++] = c; + } + buf[*off] = 0; +} + +static void +printstr(char *buf, size_t size, size_t *off, const char *s) +{ + while (*off < size - 1 && *s != '\0') { + buf[(*off)++] = *(s)++; + } + buf[*off] = 0; +} + +int +vsnprintf(char *s, size_t size, const char *fmt, va_list ap) +{ + size_t tmp_len, num_len, off = 0; + ssize_t num = 0; + char c, c1, num_buf[256] = {0}; + const char *tmp_str; + uint8_t pad_width = 0; + + while (off < (size - 1)) { + while (*fmt && *fmt != '%') { + printc(s, size, &off, *fmt++); + } + if (*(fmt)++ == '\0' || off == size - 1) { + return off; + } + + /* + * Handle a case where we have "%0N". For example: + * "%04d" + */ + if (*fmt == '0') { + ++fmt; + while (*fmt >= '0' && *fmt <= '9') { + pad_width = pad_width * 10 + (*fmt - '0'); + ++fmt; + } + } + + c = *fmt++; + switch (c) { + case 'c': + c1 = (char )__va_arg(ap, int); + printc(s, size, &off, c1); + break; + case 'd': + num = __va_arg(ap, int); + itoa(num, num_buf, 10); + + if (pad_width > 0) { + num_len = strlen(num_buf); + for (size_t i = num_len; i < pad_width; ++i) + printc(s, size, &off, '0'); + pad_width = 0; + } + printstr(s, size, &off, num_buf); + break; + case 'p': + num = __va_arg(ap, uint64_t); + itoa(num, num_buf, 16); + tmp_len = strlen(num_buf); + + /* Add '0x' prefix */ + printc(s, size, &off, '0'); + printc(s, size, &off, 'x'); + /* + * Now we pad this. + * + * XXX TODO: This assumes 64-bits, should be + * cleaned up. + */ + for (size_t i = 0; i < 18 - tmp_len; ++i) { + printc(s, size, &off, '0'); + } + printstr(s, size, &off, num_buf + 2); + break; + case 'x': + num = __va_arg(ap, uint64_t); + itoa(num, num_buf, 16); + tmp_len = strlen(num_buf); + if (pad_width > 0) { + num_len = strlen(num_buf); + for (size_t i = num_len; i < pad_width; ++i) + printc(s, size, &off, '0'); + pad_width = 0; + } + printstr(s, size, &off, num_buf + 2); + break; + case 's': + tmp_str = __va_arg(ap, const char *); + printstr(s, size, &off, tmp_str); + break; + } + } + + return 0; +} diff --git a/lib/libc/src/stdlib/_Exit.c b/lib/libc/src/stdlib/_Exit.c new file mode 100644 index 0000000..e975381 --- /dev/null +++ b/lib/libc/src/stdlib/_Exit.c @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/syscall.h> +#include <stdlib.h> + +__dead void +_Exit(int status) +{ + syscall(SYS_exit, status); + __builtin_unreachable(); +} diff --git a/lib/libc/src/stdlib/abort.c b/lib/libc/src/stdlib/abort.c new file mode 100644 index 0000000..bc0a491 --- /dev/null +++ b/lib/libc/src/stdlib/abort.c @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdlib.h> + +__dead void +abort(void) +{ + /* + * TODO: Call SIGABRT handler here. + */ + + _Exit(EXIT_FAILURE); +} diff --git a/lib/libc/src/stdlib/exit.c b/lib/libc/src/stdlib/exit.c new file mode 100644 index 0000000..e5adfac --- /dev/null +++ b/lib/libc/src/stdlib/exit.c @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdlib.h> + +__dead void +exit(int status) +{ + /* + * TODO: Call atexit() handlers and do cleanup here. + */ + + _Exit(status); +} diff --git a/lib/libc/src/stdlib/malloc.c b/lib/libc/src/stdlib/malloc.c new file mode 100644 index 0000000..4f25c24 --- /dev/null +++ b/lib/libc/src/stdlib/malloc.c @@ -0,0 +1,219 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/types.h> +#include <sys/mman.h> +#include <sys/param.h> +#include <sys/cdefs.h> +#include <stdlib.h> +#include <stddef.h> +#include <stdatomic.h> +#include <stdio.h> +#include <stdbool.h> +#include <string.h> + +#define HEAP_SIZE 0x1001A8 +#define HEAP_MAGIC 0x05306A /* "OSMORA" :~) */ +#define HEAP_ALIGN 4 +#define HEAP_PROT PROT_READ | PROT_WRITE +#define BYTE_PTR(PTR) ((char *)(PTR)) +#define HEAP_NEXT(BLOCKP, SIZE) \ + PTR_OFFSET((BLOCKP), sizeof(struct mem_block) + (SIZE)) + +struct __aligned(HEAP_ALIGN) mem_block { + uint32_t magic; + uint32_t size; + uint8_t allocated : 1; + struct mem_block *next; +}; + +static struct mem_block *mem_head; +static struct mem_block *mem_tail; + +/* + * The size of the heap including data on the heap + * as well as the sizes of their respective block + * headers. + */ +static ssize_t heap_len = 0; +static off_t heap_pos = 0; + +/* + * During the initial state of malloc() when the C library + * first starts up. We can assume that there is zero fragmentation + * in our heap pool. This allows us to initially allocate memory + * by bumping a pointer which is O(1). During this state, even after + * any calls to free(), we can assume that there is more memory ahead + * of us that is free (due to the initial zero-fragmentation). However, + * once we've reached the end of the pool, if any memory has been previously + * freed (indicated by heap_len > 0), we can wrap the tail and start allocating + * in a best-fit fashion as we can assume that the heap is now fragmented. + */ +static bool wrap = false; + +void __malloc_mem_init(void); + +/* + * Terminate program abnormally due to any heap + * errors. + * + * TODO: Raise SIGABRT instead of using _Exit() + */ +__dead static void +__heap_abort(const char *str) +{ + printf(str); + _Exit(1); + __builtin_unreachable(); +} + +/* + * Find a free block + * + * TODO: This is currently a first-fit style + * routine. This should be best-fit instead + * as it doesn't waste memory. + */ +static struct mem_block * +malloc_find_free(size_t size) +{ + struct mem_block *cur = mem_head; + + while (cur != NULL) { + if (cur->size >= size) { + return cur; + } + + cur = cur->next; + } + + return NULL; +} + +void * +malloc(size_t size) +{ + struct mem_block *next_block; + struct mem_block *tail; + size_t inc_len = 0; + + size = ALIGN_UP(size, HEAP_ALIGN); + inc_len = sizeof(*next_block) + size; + + if (heap_len < 0) { + heap_len = 0; + } + + if (heap_len < 0) { + heap_len = 0; + return NULL; + } + + /* Any memory left to allocate? */ + if ((heap_len + inc_len) >= HEAP_SIZE) { + return NULL; + } + + if (heap_pos >= HEAP_SIZE - inc_len) { + wrap = true; + mem_tail = mem_head; + } + + tail = wrap ? malloc_find_free(size) : mem_tail; + if (tail == NULL) { + return NULL; + } + + next_block = mem_tail; + mem_tail = HEAP_NEXT(mem_tail, size); + mem_tail->next = NULL; + + next_block->next = mem_tail; + next_block->size = size; + next_block->allocated = 1; + next_block->magic = HEAP_MAGIC; + + heap_len += inc_len; + heap_pos += inc_len; + return PTR_OFFSET(next_block, sizeof(*next_block)); +} + +void * +realloc(void *ptr, size_t size) +{ + struct mem_block *blk; + void *new_buf; + + blk = PTR_NOFFSET(ptr, sizeof(*blk)); + if (blk->magic != HEAP_MAGIC) { + __heap_abort("realloc: bad realloc block detected\n"); + } + if (!blk->allocated) { + __heap_abort("realloc: bad realloc\n"); + } + + new_buf = malloc(size); + memcpy(new_buf, ptr, blk->size); + free(ptr); + return new_buf; +} + +void +free(void *ptr) +{ + struct mem_block *blk; + + blk = PTR_NOFFSET(ptr, sizeof(*blk)); + if (blk->magic != HEAP_MAGIC) { + __heap_abort("free: bad free block detected\n"); + } + if (!blk->allocated) { + __heap_abort("free: double free detected\n"); + } + + blk->allocated = 0; + heap_len -= (blk->size + sizeof(*blk)); + if (heap_len < 0) { + heap_len = 0; + } +} + +void +__malloc_mem_init(void) +{ + mem_head = mmap(NULL, HEAP_SIZE, HEAP_PROT, MAP_ANON, 0, 0); + if (mem_head == NULL) { + __heap_abort("__malloc_mem_init: mem_head is NULL, out of memory\n"); + } + + mem_head->size = 0; + mem_head->next = NULL; + mem_head->allocated = 0; + mem_tail = mem_head; +} diff --git a/lib/libc/src/stdlib/rand.c b/lib/libc/src/stdlib/rand.c new file mode 100644 index 0000000..838ba5f --- /dev/null +++ b/lib/libc/src/stdlib/rand.c @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdlib.h> + +static unsigned int r_seed = 0x9E3779B9; + +void +srand(unsigned int r) +{ + r_seed = r; +} + +int +rand(void) +{ + r_seed = (r_seed >> 0x01U) ^ (-(r_seed & 0x01U) & 0xB400U); + return r_seed; +} diff --git a/lib/libc/src/string/atoi.c b/lib/libc/src/string/atoi.c new file mode 100644 index 0000000..24943da --- /dev/null +++ b/lib/libc/src/string/atoi.c @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <string.h> + +#define IS_DIGIT(C) ((C >= '0' && C <= '9')) + +int +atoi(char *s) +{ + int n, sign; + + while (*s == ' ') { + ++s; + } + + sign = (*s == '-') ? -1 : 1; + if (*s == '+' || *s == '-') { + s++; + } + for (n = 0; IS_DIGIT(*s); ++s) { + n = 10 * n + (*s - '0'); + } + return sign * n; +} diff --git a/lib/libc/src/string/itoa.c b/lib/libc/src/string/itoa.c new file mode 100644 index 0000000..cfce406 --- /dev/null +++ b/lib/libc/src/string/itoa.c @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/types.h> +#include <string.h> +#include <stdbool.h> + +static char * +itoa_base10_convert(int64_t value, char *buf) +{ + size_t i; + uint8_t tmp; + bool is_negative; + + i = 0; + is_negative = false; + + if (value == 0) { + buf[i++] = '0'; + buf[i++] = '\0'; + return buf; + } + + if (value < 0) { + /* Easier to handle positive numbers */ + value *= -1; + is_negative = true; + } + + while (value > 0) { + buf[i++] = '0' + (value % 10); + value /= 10; + } + + if (is_negative) { + buf[i++] = '-'; + } + + buf[i--] = '\0'; + + /* Result is in reverse */ + for (int j = 0; j < i; ++j, --i) { + tmp = buf[j]; + buf[j] = buf[i]; + buf[i] = tmp; + } + + return buf; +} + +static char * +itoa_convert_base16(uint64_t n, char *buffer) +{ + bool pad; + uint8_t nibble; + uint8_t i, j, tmp; + const char *ascii_nums = "0123456789ABCDEF"; + + i = 0; + pad = false; + + if (n == 0) { + /* Zero, no need to parse */ + memcpy(buffer, "0x00\0", 5); + return buffer; + } + /* If one digit, pad out 2 later */ + if (n < 0x10) { + pad = true; + } + + while (n > 0) { + nibble = (uint8_t)n & 0x0F; + nibble = ascii_nums[nibble]; + buffer[i++] = nibble; + n >>= 4; /* Fetch next nibble */ + } + + if (pad) { + buffer[i++] = '0'; + } + + /* Add "0x" prefix */ + buffer[i++] = 'x'; + buffer[i++] = '0'; + buffer[i--] = '\0'; + + /* Unreverse the result */ + for (j = 0; j < i; ++j, --i) { + tmp = buffer[j]; + buffer[j] = buffer[i]; + buffer[i] = tmp; + } + return buffer; +} + +char * +itoa(int64_t value, char *buf, int base) +{ + switch (base) { + case 10: + return itoa_base10_convert(value, buf); + case 16: + return itoa_convert_base16(value, buf); + default: + return NULL; + } +} diff --git a/lib/libc/src/string/memcpy.c b/lib/libc/src/string/memcpy.c new file mode 100644 index 0000000..a9bcbe9 --- /dev/null +++ b/lib/libc/src/string/memcpy.c @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <string.h> + +void * +memcpy(void *dest, const void *src, size_t n) +{ + for (size_t i = 0; i < n; ++i) { + ((char *)dest)[i] = ((char *)src)[i]; + } + + return dest; +} diff --git a/lib/libc/src/string/memset.c b/lib/libc/src/string/memset.c new file mode 100644 index 0000000..c95029c --- /dev/null +++ b/lib/libc/src/string/memset.c @@ -0,0 +1,45 @@ + +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <string.h> + +void * +memset(void *dst, int c, size_t n) +{ + char *p = dst; + + if (n != 0) { + do { + *p++ = (unsigned char)c; + } while (--n != 0); + } + + return dst; +} diff --git a/lib/libc/src/unistd/access.c b/lib/libc/src/unistd/access.c new file mode 100644 index 0000000..0aeb030 --- /dev/null +++ b/lib/libc/src/unistd/access.c @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/syscall.h> +#include <unistd.h> + +int +access(const char *path, int mode) +{ + return syscall(SYS_access, (uintptr_t)path, mode); +} diff --git a/lib/libc/src/unistd/lseek.c b/lib/libc/src/unistd/lseek.c new file mode 100644 index 0000000..d99b0f0 --- /dev/null +++ b/lib/libc/src/unistd/lseek.c @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/syscall.h> +#include <unistd.h> + +off_t +lseek(int fildes, off_t offset, int whence) +{ + return syscall(SYS_lseek, fildes, offset, whence); +} diff --git a/lib/libc/src/unistd/sysconf.c b/lib/libc/src/unistd/sysconf.c new file mode 100644 index 0000000..43fab01 --- /dev/null +++ b/lib/libc/src/unistd/sysconf.c @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <unistd.h> + +extern uint64_t __libc_auxv[_AT_MAX]; + +int +sysconf(int name) +{ + if (name >= _AT_MAX) { + return -1; + } + + return __libc_auxv[name]; +} |