Split up source and added basic support for different targets.

This commit is contained in:
2024-01-12 17:19:27 +01:00
parent 219a48c616
commit 408e06b2e0
29 changed files with 559 additions and 36 deletions

17
targets/_any/SConscript Normal file
View File

@@ -0,0 +1,17 @@
Import('env')
any_target_sources = Split('''
src/app/main.cpp
src/os/panic.cpp
src/os/tty.cpp
src/cstdlib/assert.cpp
src/cstdlib/stdio.cpp
src/cstdlib/stdlib.cpp
src/cstdlib/string.cpp
''')
env.Append(KERNEL_SOURCES = [env.File(f) for f in any_target_sources])
Return('env')

View File

@@ -0,0 +1,27 @@
#pragma once
#if !defined(BAD_APPLE_OS_ASSERT_H_INCLUDED)
#define BAD_APPLE_OS_ASSERT_H_INCLUDED
#include "./detail/common.h"
BA_EXTERN_C_BEGIN
#if defined(NDEBUG)
#define assert(condition) ((void) 0)
#else
#define assert(condition) \
if (!(condition)) \
{ \
__ba__assert_fail(__FILE__, __LINE__, #condition); \
}
#endif
#if !defined(NDEBUG)
BA_CXX_NORETURN void __ba__assert_fail(const char* filename, int line, const char* condition) BA_CXX_NOEXCEPT;
#endif
BA_EXTERN_C_END
#endif // !defined(BAD_APPLE_OS_ASSERT_H_INCLUDED)

View File

@@ -0,0 +1,21 @@
#pragma once
#if !defined(BAD_APPLE_OS_DETAIL_ATTRIBUTES_H_INCLUDED)
#define BAD_APPLE_OS_DETAIL_ATTRIBUTES_H_INCLUDED
#if defined(__cplusplus)
#define BA_CXX_NODISCARD [[nodiscard]]
#define BA_CXX_NORETURN [[noreturn]]
#define BA_CXX_NOEXCEPT noexcept
#define BA_EXTERN_C_BEGIN extern "C" {
#define BA_EXTERN_C_END }
#else
#define BA_CXX_NODISCARD
#define BA_CXX_NORETURN
#define BA_CXX_NOEXCEPT
#define BA_EXTERN_C_BEGIN
#define BA_EXTERN_C_END
#endif
#endif // !defined(BAD_APPLE_OS_DETAIL_ATTRIBUTES_H_INCLUDED)

View File

@@ -0,0 +1,10 @@
#pragma once
#if !defined(BAD_APPLE_OS_PANIC_HPP_INCLUDED)
#define BAD_APPLE_OS_PANIC_HPP_INCLUDED
[[noreturn]] void panic(const char* message) noexcept;
[[noreturn]] void panicf(const char* format, ...) noexcept;
#endif // !defined(BAD_APPLE_OS_PANIC_HPP_INCLUDED)

View File

@@ -0,0 +1,46 @@
#pragma once
#if !defined(OS_TTY_HPP_INCLUDED)
#define OS_TTY_HPP_INCLUDED 1
#include <stddef.h>
#include <stdint.h>
namespace tty
{
enum class VgaColor : uint8_t
{
BLACK = 0,
BLUE = 1,
GREEN = 2,
CYAN = 3,
RED = 4,
MAGENTA = 5,
BROWN = 6,
LIGHT_GREY = 7,
DARK_GREY = 8,
LIGHT_BLUE = 9,
LIGHT_GREEN = 10,
LIGHT_CYAN = 11,
LIGHT_RED = 12,
LIGHT_MAGENTA = 13,
LIGHT_BROWN = 14,
WHITE = 15
};
struct VgaDoubleColor
{
VgaColor foreground : 4 = VgaColor::LIGHT_GREY;
VgaColor background : 4 = VgaColor::BLACK;
};
void initialize();
void setColor(VgaDoubleColor color);
void putEntryAt(char chr, VgaDoubleColor color, size_t posX, size_t posY);
void putChar(char chr);
void write(const char* data, size_t size);
void write(const char* data);
} // namespace tty
#endif // OS_TTY_HPP_INCLUDED

View File

@@ -0,0 +1,19 @@
#pragma once
#if !defined(BAD_APPLE_OS_STDIO_H_INCLUDED)
#define BAD_APPLE_OS_STDIO_H_INCLUDED
#include <stdarg.h>
#include "./detail/common.h"
BA_EXTERN_C_BEGIN
int putchar(int chr) BA_CXX_NOEXCEPT;
int puts(const char* str) BA_CXX_NOEXCEPT;
int printf(const char* format, ...) BA_CXX_NOEXCEPT;
int vprintf(const char* format, va_list vlist) BA_CXX_NOEXCEPT;
BA_EXTERN_C_END
#endif // !defined(BAD_APPLE_OS_STDIO_H_INCLUDED)

View File

@@ -0,0 +1,18 @@
#pragma once
#if !defined(BAD_APPLE_OS_STDLIB_H_INCLUDED)
#define BAD_APPLE_OS_STDLIB_H_INCLUDED
#include <stddef.h>
#include "./detail/common.h"
BA_EXTERN_C_BEGIN
BA_CXX_NORETURN void abort();
BA_CXX_NODISCARD void* malloc(size_t size) BA_CXX_NOEXCEPT;
void free(void* ptr) BA_CXX_NOEXCEPT;
BA_EXTERN_C_END
#endif // !defined(BAD_APPLE_OS_STDLIB_H_INCLUDED)

View File

@@ -0,0 +1,21 @@
#pragma once
#if !defined(BAD_APPLE_OS_STRING_H_INCLUDED)
#define BAD_APPLE_OS_STRING_H_INCLUDED
#include <stddef.h>
#include "./detail/common.h"
BA_EXTERN_C_BEGIN
BA_CXX_NODISCARD int memcmp(const void* lhs, const void* rhs, size_t count) BA_CXX_NOEXCEPT;
void memcpy(void* dest, const void* src, size_t count) BA_CXX_NOEXCEPT;
void memmove(void* dest, const void* src, size_t count) BA_CXX_NOEXCEPT;
void* memset(void* dest, int value, size_t count) BA_CXX_NOEXCEPT;
BA_CXX_NODISCARD size_t strlen(const char* str) BA_CXX_NOEXCEPT;
BA_EXTERN_C_END
#endif // !defined(BAD_APPLE_OS_STRING_H_INCLUDED)

View File

@@ -0,0 +1,4 @@
extern "C" void main()
{
}

View File

@@ -0,0 +1,20 @@
#include <assert.h>
#include <cstdio>
#include <cstdlib>
extern "C"
{
#if !defined(NDEBUG)
void __ba__assert_fail(const char* filename, int line, const char* condition) noexcept
{
std::printf(R"(
Assertion failed:
File: %s
Line: %d
Condition: %s)" "\n", filename, line, condition);
std::abort();
}
#endif
} // extern "C"

View File

@@ -0,0 +1,208 @@
#include <stdio.h>
#include <cassert>
#include <cstring>
#include <stdarg.h>
#include "os/tty.hpp"
extern "C"
{
namespace
{
int printInt(int value)
{
if (value == 0)
{
putchar('0');
return 1;
}
if (value < 0)
{
putchar('-');
return printInt(-value) + 1;
}
char digits[10]; // 2147483647 has 10 digits
char* pos = &digits[0];
while (value > 0)
{
*pos = static_cast<char>('0' + (value % 10));
value /= 10;
++pos;
}
for (char* chr = pos - 1; chr >= digits; --chr)
{
putchar(*chr);
}
return pos - digits;
}
int printUInt(uint64_t value)
{
if (value == 0)
{
putchar('0');
return 1;
}
char digits[20]; // 18,446,744,073,709,551,615 has 20 digits
char* pos = &digits[0];
while (value > 0)
{
*pos = static_cast<char>('0' + (value % 10));
value /= 10;
++pos;
}
for (char* chr = pos - 1; chr >= digits; --chr)
{
putchar(*chr);
}
return pos - digits;
}
int printHexInt(uint64_t value)
{
if (value == 0)
{
putchar('0');
return 1;
}
char digits[16]; // FFFFFFFFFFFFFFFF has 16 digits
char* pos = &digits[0];
while (value > 0)
{
const uint64_t digit = (value % 16);
if (digit < 10)
{
*pos = static_cast<char>('0' + digit);
}
else
{
*pos = static_cast<char>('A' + digit - 10);
}
value /= 16;
++pos;
}
for (char* chr = pos - 1; chr >= digits; --chr)
{
putchar(*chr);
}
return pos - digits;
}
int printPointer(void* ptr)
{
return printHexInt(reinterpret_cast<uint64_t>(ptr));
}
int printString(const char* str)
{
const size_t len = std::strlen(str);
for (size_t idx = 0; idx < len; ++idx)
{
putchar(str[idx]);
}
return static_cast<int>(len);
}
int printByteSize(size_t bytes)
{
const char* suffixes[] = {
"B", "KiB", "MiB", "GiB", "TiB"
};
int suffixIdx = 0;
for (; suffixIdx < sizeof(suffixes) / sizeof(suffixes[0]); ++suffixIdx)
{
if (bytes < 1024) {
break;
}
bytes /= 1024;
}
return printUInt(bytes) + printString(suffixes[suffixIdx]);
}
}
int putchar(int chr) noexcept
{
tty::putChar(static_cast<char>(chr));
return 0;
}
int puts(const char* str) noexcept
{
tty::write(str);
tty::putChar('\n');
return 0;
}
int printf(const char* format, ...) noexcept
{
va_list parameters;
va_start(parameters, format);
const int result = vprintf(format, parameters);
va_end(parameters);
return result;
}
int vprintf(const char* format, va_list vlist) BA_CXX_NOEXCEPT
{
int result = 0;
const char* pos = format;
while (*pos != '\0')
{
if (*pos == '%')
{
++pos;
switch (*pos)
{
case '%':
putchar('%');
++result;
break;
case '\0':
// TODO: invalid format string, do something
assert(!"Invalid format string, ends with %.");
return -1;
case 'b':
result += printByteSize(va_arg(vlist, size_t));
break;
case 'c':
{
const char chr = static_cast<char>(va_arg(vlist, int));
putchar(chr);
++result;
break;
}
case 's':
result += printString(va_arg(vlist, const char*));
break;
case 'd':
result += printInt(va_arg(vlist, int));
break;
case 'p':
result += printPointer(va_arg(vlist, void*));
break;
case 'X':
result += printHexInt(va_arg(vlist, unsigned));
break;
default:
assert(!"Invalid format string, unknown format identifier.");
return -1;
}
} else
{
putchar(*pos);
}
++pos;
}
++result;
return result;
}
} // extern "C"

View File

@@ -0,0 +1,105 @@
#include <stdlib.h>
#include <stddef.h>
#include "os/tty.hpp"
namespace
{
inline const size_t INIT_MALLOC_SPACE_ELEMENTS = 1024;
inline const size_t INIT_MALLOC_SPACE_BYTES = INIT_MALLOC_SPACE_ELEMENTS * sizeof(max_align_t);
max_align_t gInitMallocSpace[INIT_MALLOC_SPACE_ELEMENTS];
struct MallocBlock
{
size_t elements;
MallocBlock* nextBlock;
};
struct AllocInfo
{
size_t elements;
};
static_assert(sizeof(MallocBlock) <= sizeof(max_align_t));
static_assert(sizeof(AllocInfo) <= sizeof(max_align_t));
MallocBlock* gNextBlock = []()
{
MallocBlock* initialBlock = reinterpret_cast<MallocBlock*>(gInitMallocSpace);
initialBlock->elements = INIT_MALLOC_SPACE_ELEMENTS;
initialBlock->nextBlock = nullptr;
return initialBlock;
}();
}
void __ba_registerAllocatableMemory(void* memory, size_t size) noexcept
{
if (size < sizeof(max_align_t))
{
return;
}
MallocBlock* newBlock = static_cast<MallocBlock*>(memory);
newBlock->nextBlock = gNextBlock;
newBlock->elements = size / sizeof(max_align_t);
gNextBlock = newBlock;
}
extern "C"
{
void abort()
{
tty::write("Abort called!");
__asm__ volatile("hlt");
while(true) {};
__builtin_unreachable();
}
void* malloc(size_t size) noexcept
{
if (size == 0)
{
return nullptr;
}
const size_t requiredElements = (size + sizeof(max_align_t) - 1) / sizeof(max_align_t) + 1;
MallocBlock* prevBlock = nullptr;
for (MallocBlock* block = gNextBlock; block != nullptr; block = block->nextBlock)
{
if (block->elements >= requiredElements)
{
MallocBlock* newBlock = nullptr;
if (block->elements > requiredElements)
{
newBlock = reinterpret_cast<MallocBlock*>(reinterpret_cast<max_align_t*>(block) + requiredElements);
newBlock->nextBlock = block->nextBlock;
newBlock->elements = block->elements - requiredElements;
}
else
{
newBlock = block->nextBlock;
}
if (prevBlock != nullptr)
{
prevBlock->nextBlock = newBlock;
}
AllocInfo* allocInfo = reinterpret_cast<AllocInfo*>(block);
allocInfo->elements = requiredElements;
return reinterpret_cast<max_align_t*>(block) + 1;
}
prevBlock = block;
}
return nullptr;
}
void free(void* memory) noexcept
{
if (memory == nullptr)
{
return;
}
MallocBlock* block = reinterpret_cast<MallocBlock*>(static_cast<max_align_t*>(memory) - 1);
block->nextBlock = gNextBlock;
gNextBlock = block;
}
} // extern "C"

View File

@@ -0,0 +1,65 @@
#include <string.h>
#include <stdint.h>
extern "C"
{
int memcmp(const void* lhs, const void* rhs, size_t count) noexcept
{
for (size_t pos = 0; pos < count; ++pos)
{
const unsigned char left = *(static_cast<const unsigned char*>(lhs) + pos);
const unsigned char right = *(static_cast<const unsigned char*>(rhs) + pos);
if (left != right) {
return left - right;
}
}
return 0;
}
void memcpy(void* dest, const void* src, size_t count) noexcept
{
for (size_t pos = 0; pos < count; ++pos)
{
*(static_cast<uint8_t*>(dest) + pos) = *(static_cast<const uint8_t*>(src) + pos);
}
}
void memmove(void* dest, const void* src, size_t count) noexcept
{
if (dest < src)
{
for (size_t pos = 0; pos < count; ++pos)
{
*(static_cast<uint8_t*>(dest) + pos) = *(static_cast<const uint8_t*>(src) + pos);
}
}
else
{
for (size_t pos = 0; pos < count; ++pos)
{
*(static_cast<uint8_t*>(dest) + count - pos - 1) = *(static_cast<const uint8_t*>(src) + count - pos - 1);
}
}
}
void* memset(void* dest, int value, size_t count) noexcept
{
for (size_t pos = 0; pos < count; ++pos)
{
*(static_cast<unsigned char*>(dest) + pos) = static_cast<unsigned char>(value);
}
return dest;
}
size_t strlen(const char* str) noexcept
{
size_t len = 0;
while (str[len] != '\0') {
++len;
}
return len;
}
}

View File

@@ -0,0 +1,26 @@
#include "os/panic.hpp"
#include <stdarg.h>
#include <cstdio>
#include <cstdlib>
#include "os/tty.hpp"
void panic(const char* message) noexcept
{
tty::write("Kernel panic!\n");
tty::write(message);
tty::putChar('\n');
std::abort();
}
void panicf(const char* format, ...) noexcept
{
tty::write("Kernel panic!\n");
va_list parameters;
va_start(parameters, format);
std::vprintf(format, parameters);
va_end(parameters);
tty::putChar('\n');
std::abort();
}

View File

@@ -0,0 +1,93 @@
#include "os/tty.hpp"
#include <cstring>
namespace tty
{
namespace
{
inline const size_t VGA_WIDTH = 80;
inline const size_t VGA_HEIGHT = 25;
size_t gTerminalRow = 0;
size_t gTerminalColumn = 0;
VgaDoubleColor gTerminalColor = VgaDoubleColor();
uint16_t* gTerminalBuffer = reinterpret_cast<uint16_t*>(0xB8000);
constexpr uint8_t vgaEntryColor(VgaDoubleColor color)
{
return static_cast<uint8_t>(color.foreground) | static_cast<uint8_t>(color.background) << 4;
}
constexpr uint16_t vgaEntry(const unsigned char chr, const VgaDoubleColor color)
{
return static_cast<uint16_t>(chr) | static_cast<uint16_t>(vgaEntryColor(color) << 8);
}
void newline()
{
gTerminalColumn = 0;
if (gTerminalRow < VGA_HEIGHT - 1)
{
++gTerminalRow;
}
else
{
// scrolling up
std::memmove(&gTerminalBuffer[0], &gTerminalBuffer[VGA_WIDTH], VGA_WIDTH * (VGA_HEIGHT - 1) * sizeof(uint16_t));
}
}
}
void initialize()
{
for (size_t y = 0; y < VGA_HEIGHT; y++)
{
for (size_t x = 0; x < VGA_WIDTH; x++)
{
const size_t index = y * VGA_WIDTH + x;
gTerminalBuffer[index] = vgaEntry(' ', gTerminalColor);
}
}
}
void setColor(VgaDoubleColor color)
{
gTerminalColor = color;
}
void putEntryAt(char chr, VgaDoubleColor color, size_t posX, size_t posY)
{
const size_t index = posY * VGA_WIDTH + posX;
gTerminalBuffer[index] = vgaEntry(chr, color);
}
void putChar(char chr)
{
if (chr == '\n')
{
newline();
return;
}
putEntryAt(chr, gTerminalColor, gTerminalColumn, gTerminalRow);
if (++gTerminalColumn == VGA_WIDTH)
{
newline();
}
}
void write(const char* data, size_t size)
{
for (size_t idx = 0; idx < size; idx++)
{
putChar(data[idx]);
}
}
void write(const char* data)
{
write(data, std::strlen(data));
}
}