From 1cfbd25dc9eb39467406686c8ae1efbfd5517865 Mon Sep 17 00:00:00 2001 From: Danny Holman Date: Sat, 3 May 2025 13:01:25 -0500 Subject: kernel: x86_64: rename x86 Rename the x86 arch-specific directory to x86_64, reflecting clang's architecture selector flags. Signed-off-by: Danny Holman --- kernel/core/init.c | 26 ++++++++ kernel/core/kprintf.c | 95 ++++++++++++++++++++++++++ kernel/core/panic.c | 26 +++++++- kernel/core/stack_protector.c | 28 ++++++++ kernel/core/string.c | 152 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 324 insertions(+), 3 deletions(-) create mode 100644 kernel/core/init.c create mode 100644 kernel/core/kprintf.c create mode 100644 kernel/core/stack_protector.c create mode 100644 kernel/core/string.c (limited to 'kernel/core') diff --git a/kernel/core/init.c b/kernel/core/init.c new file mode 100644 index 0000000..d999a50 --- /dev/null +++ b/kernel/core/init.c @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2025 Danny Holman + * + * This file is part of BoxOS, a free and open-source Unix-like operating + * system. + * + * BoxOS is free software; you can redistribute it and/or modify under the + * terms of the GNU General Public License as published by the Free Software + * Foundation; either version 2 of the License, or (at your option) any later + * version. + * + * BoxOS is distributed in the hope it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * BoxOS; if not, see . + */ + +#include +#include +#include + +void kernel_main(int argc, char* argv[]) { + kprintf("BoxOS - version %s\n", VERSION); +} diff --git a/kernel/core/kprintf.c b/kernel/core/kprintf.c new file mode 100644 index 0000000..c12a01f --- /dev/null +++ b/kernel/core/kprintf.c @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2025 Danny Holman + * + * This file is part of BoxOS, a free and open-source Unix-like operating + * system. + * + * BoxOS is free software; you can redistribute it and/or modify under the + * terms of the GNU General Public License as published by the Free Software + * Foundation; either version 2 of the License, or (at your option) any later + * version. + * + * BoxOS is distributed in the hope it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * BoxOS; if not, see . + */ + +#include +#include +#include + +char* convert(unsigned int num, int base) { + static char rep[] = "0123456789ABCDEF"; + static char buffer[50]; + char *ptr; + + ptr = &buffer[49]; + *ptr = '\0'; + + do { + *--ptr = rep[num%base]; + num /= base; + } while (num != 0); + + return ptr; +} + +int vkprintf(const char *fmt, va_list args) { + char *s; + int i; + + char buffer[4096]; + memset(buffer, 0, 4096); + for (size_t n = 0; n < strlen(fmt); n++) { + if (fmt[n] != '%') { + buffer[strlen(buffer)] = fmt[n]; + continue; + } else { + n++; + } + + switch (fmt[n]) { + case 'c': + i = va_arg(args, int); + buffer[strlen(buffer)] = i; + break; + case 's': + s = va_arg(args, char*); + strcat(buffer, s); + break; + case 'd': + i = va_arg(args, int); + if (i < 0) { + i = -i; + strcat(buffer, "-"); + } + strcat(buffer, convert(i, 10)); + break; + case 'o': + i = va_arg(args, unsigned int); + strcat(buffer, convert(i, 10)); + break; + case 'x': + i = va_arg(args, unsigned int); + strcat(buffer, convert(i, 16)); + break; + } + } + //tty_write(buffer, strlen(buffer)); + memset(buffer, 0, 4096); + return 0; +} + +int kprintf(const char *fmt, ...) { + va_list args; + int done; + + va_start(args, fmt); + done = vkprintf(fmt, args); + va_end(args); + + return done; +} diff --git a/kernel/core/panic.c b/kernel/core/panic.c index a44a73c..742e71e 100644 --- a/kernel/core/panic.c +++ b/kernel/core/panic.c @@ -1,6 +1,26 @@ -#include -#include -#include +/* + * Copyright (C) 2025 Danny Holman + * + * This file is part of BoxOS, a free and open-source Unix-like operating + * system. + * + * BoxOS is free software; you can redistribute it and/or modify under the + * terms of the GNU General Public License as published by the Free Software + * Foundation; either version 2 of the License, or (at your option) any later + * version. + * + * BoxOS is distributed in the hope it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * BoxOS; if not, see . + */ + +#include +#include +#include +#include #include #include diff --git a/kernel/core/stack_protector.c b/kernel/core/stack_protector.c new file mode 100644 index 0000000..cb15a15 --- /dev/null +++ b/kernel/core/stack_protector.c @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2025 Danny Holman + * + * This file is part of BoxOS, a free and open-source Unix-like operating + * system. + * + * BoxOS is free software; you can redistribute it and/or modify under the + * terms of the GNU General Public License as published by the Free Software + * Foundation; either version 2 of the License, or (at your option) any later + * version. + * + * BoxOS is distributed in the hope it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * BoxOS; if not, see . + */ + +#include +#include + +uintptr_t __stack_chk_guard; + +__attribute__((noreturn)) void __stack_chk_fail(void) { + panic("Detected attempted stack manipulation in kernel"); + while(1); +} diff --git a/kernel/core/string.c b/kernel/core/string.c new file mode 100644 index 0000000..5502051 --- /dev/null +++ b/kernel/core/string.c @@ -0,0 +1,152 @@ +/* + * Copyright (C) 2025 Danny Holman + * + * This file is part of BoxOS, a free and open-source Unix-like operating + * system. + * + * BoxOS is free software; you can redistribute it and/or modify under the + * terms of the GNU General Public License as published by the Free Software + * Foundation; either version 2 of the License, or (at your option) any later + * version. + * + * BoxOS is distributed in the hope it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * BoxOS; if not, see . + */ + +#include + +int memcmp(const void *str1, const void *str2, size_t n) { + unsigned char const *p1 = str1; + unsigned char const *p2 = str2; + int ret = 0; + + if (str1 == str2) + return 0; + + while (n > 0) { + if (*p1 != *p2) { + ret = (*p1 > *p2)?1:-1; + break; + } + n--; + p1++; + p2++; + } + return ret; +} + +void* memcpy(void* __restrict dest, const void* __restrict src, size_t n) { + if (dest == src) + return dest; + + unsigned char *pdest = dest; + unsigned char const *psrc = src; + for (size_t i = 0; i < n; i++) + pdest[i] = psrc[i]; + return dest; +} + +char* strncpy(char* __restrict dest, const char* __restrict src, size_t n) { + for (size_t i = 0; i < n; i++) { + if (src[i] == '\0') + break; + dest[i] = src[i]; + } + return dest; +} + +char* strcpy(char* __restrict dest, const char* __restrict src) { + return (char*)memcpy(dest, src, strlen(src)); +} + +char* strcat(char* __restrict dest, const char* __restrict src) { + return (char*)memcpy(&dest[strlen(dest)], src, strlen(src)); +} + +char* strncat(char* __restrict dest, const char* __restrict src, size_t n) { + return (char*)memcpy(&dest[strlen(dest)], src, n); +} + +void* memmove(void* __restrict dest, const void* __restrict src, size_t n) { + if (dest == src) + return dest; + + unsigned char const *psrc = src; + unsigned char buffer[n]; + + for (size_t i = 0; i < n; i++) + buffer[i] = psrc[i]; + return memcpy(dest, (void*)buffer, n); +} + +void* memset(void *str, int c, size_t n) { + unsigned char *p = str; + for (size_t i = 0; i < n; i++) + p[i] = (unsigned char)c; + return str; +} + +int strncmp(const char *str1, const char *str2, size_t n) { + return memcmp(str1, str2, n); +} + +int strcmp(const char *str1, const char *str2) { + size_t str1_sz = strlen(str1); + size_t str2_sz = strlen(str2); + if (str1_sz > str2_sz) + return memcmp(str1, str2, str2_sz); + return memcmp(str1, str2, str1_sz); +} + +size_t strlen(const char *str) { + size_t i = 0; + while (str[i] != '\0') + i++; + return i; +} + +int _is_delim(char c, const char *delim) { + while (*delim != '\0') { + if (c == *delim) + return 1; + delim++; + } + return 0; +} + +char* strtok(char* __restrict str, const char* __restrict delim) { + static char *old_str; + + if (str == NULL) + str = old_str; + + while (1) { + if (_is_delim(*str, delim)) { + str++; + continue; + } + + if (*str == '\0') + return NULL; + break; + } + + char *ret = str; + while (1) { + if (*str == '\0') { + old_str = str; + return ret; + } + + if (_is_delim(*str, delim)) { + *str = '\0'; + old_str = str + 1; + return ret; + } + str++; + } +} -- cgit v1.2.3