diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..b6355cf --- /dev/null +++ b/.clang-format @@ -0,0 +1,292 @@ +AccessModifierOffset: 0 + +AlignAfterOpenBracket: BlockIndent + +AlignArrayOfStructures: Right + +AlignConsecutiveAssignments: + Enabled: true + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: true + PadOperators: false + +AlignConsecutiveBitFields: Consecutive + +AlignConsecutiveDeclarations: + Enabled: true + AcrossEmptyLines: false + AcrossComments: false + AlignFunctionDeclarations: true + AlignFunctionPointers: true + +AlignConsecutiveMacros: Consecutive + +AlignConsecutiveShortCaseStatements: + Enabled: true + AcrossEmptyLines: false + AcrossComments: false + AlignCaseColons: true + +AlignEscapedNewlines: Left + +AlignOperands: AlignAfterOperator + +AlignTrailingComments: + Kind: Always + OverEmptyLines: 1 + +AllowAllArgumentsOnNextLine: true + +AllowAllParametersOfDeclarationOnNextLine: true + +AllowShortBlocksOnASingleLine: Empty + +AllowShortCaseLabelsOnASingleLine: false + +AllowShortEnumsOnASingleLine: false + +AllowShortFunctionsOnASingleLine: All + +AllowShortIfStatementsOnASingleLine: Never + +AllowShortLoopsOnASingleLine: false + +AlwaysBreakBeforeMultilineStrings: false + +AttributeMacros: [] + +BinPackArguments: true + +BinPackParameters: BinPack +#BinPackParameters: true + +BitFieldColonSpacing: Both + +BracedInitializerIndentWidth: 2 + +BraceWrapping: + AfterCaseLabel: false + AfterClass: true + AfterControlStatement: Always #MultiLine + AfterEnum: true + AfterFunction: true + AfterStruct: true + AfterUnion: true + AfterExternBlock: true + BeforeCatch: true + BeforeElse: true + BeforeWhile: false # ('do while' constructs) + IndentBraces: false + SplitEmptyFunction: false + SplitEmptyRecord: false + SplitEmptyNamespace: false + +BreakAdjacentStringLiterals: true + +BreakAfterAttributes: Leave + +BreakAfterReturnType: ExceptShortType + +BreakBeforeBinaryOperators: None # or maybe 'All' ? + +BreakBeforeBraces: Custom + +BreakBeforeInlineASMColon: Always # fixme: 'OnlyMultiLine' not recognised + +BreakBeforeTernaryOperators: false # todo: try true + +BreakBinaryOperations: RespectPrecedence + +BreakFunctionDefinitionParameters: false + +BreakStringLiterals: true + +ColumnLimit: 100 # todo: try smaller values + +CommentPragmas: '^ IWYU pragma:' + +ContinuationIndentWidth: 2 + +DeriveLineEnding: false + +DerivePointerAlignment: false + +DisableFormat: false + +EmptyLineAfterAccessModifier: Never + +EmptyLineBeforeAccessModifier: Always + +ForEachMacros: [ ] + +IfMacros: [ ] + +IncludeBlocks: Regroup + +IncludeCategories: + # Config + - Regex: '^$' + Priority: -20 + # External libraries + - Regex: '^$' + Priority: -2 + - Regex: '^$' + Priority: -2 + - Regex: '^$' + Priority: -2 + - Regex: '^$' + Priority: -2 + # External preprocessor libraries + - Regex: '^$' + Priority: -1 + - Regex: '^$' + Priority: -1 + - Regex: '^$' + Priority: -1 + # Klibc + - Regex: '^$' + Priority: 1 + # Kernel headers + - Regex: '^$' + Priority: 2 + # Machine-specific headers + - Regex: '^$' + Priority: 3 + # Miscellaneous headers + - Regex: '^$' + Priority: 4 + # Standard C headers + - Regex: '^<.+>$' + Priority: -10 + # Local headers + - Regex: '^".+"$' + Priority: 10 + +IncludeIsMainRegex: '([Mm]ain|[Tt]est)' + +IndentAccessModifiers: false + +IndentCaseBlocks: false + +IndentCaseLabels: true + +IndentExternBlock: Indent + +IndentGotoLabels: true + +IndentPPDirectives: BeforeHash + +IndentWidth: 4 + +IndentWrappedFunctionNames: false + +InsertBraces: false + +InsertNewlineAtEOF: true + +InsertTrailingCommas: None + +IntegerLiteralSeparator: + Binary: 4 + BinaryMinDigits: 4 + Decimal: 3 + DecimalMinDigits: 5 + Hex: 2 + HexMinDigits: 6 + +KeepEmptyLines: + AtEndOfFile: false + AtStartOfBlock: false + AtStartOfFile: false + +KeepFormFeed: false + +Language: Cpp + +LineEnding: LF + +MacroBlockBegin: '' + +MacroBlockEnd: '' + +Macros: [ ] + +MainIncludeChar: AngleBracket + +MaxEmptyLinesToKeep: 1 + +PPIndentWidth: 4 + +PointerAlignment: Left + +QualifierAlignment: Leave + +ReflowComments: Always + +RemoveBracesLLVM: false + +RemoveEmptyLinesInUnwrappedLines: true + +RemoveParentheses: Leave + +RemoveSemicolon: false + +SeparateDefinitionBlocks: Leave + +SkipMacroDefinitionBody: false + +SortIncludes: CaseSensitive + +SpaceAfterCStyleCast: false + +SpaceAfterLogicalNot: false + +SpaceAroundPointerQualifiers: Default + +SpaceBeforeAssignmentOperators: true + +SpaceBeforeCaseColon: false + +SpaceBeforeCpp11BracedList: false + +SpaceBeforeParens: ControlStatements # todo: use custom with 'SpaceBeforeParensOptions' + +SpaceBeforeSquareBrackets: false + +SpaceInEmptyBlock: true + +SpacesBeforeTrailingComments: 1 + +SpacesInAngles: Leave + +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: -1 # disable the maximum + +SpacesInParens: Custom + +SpacesInParensOptions: + ExceptDoubleParentheses: true + InConditionalStatements: false + InCStyleCasts: false + InEmptyParentheses: false + Other: false + +SpacesInSquareBrackets: false + +Standard: Latest + +StatementAttributeLikeMacros: [ ] + +StatementMacros: [ ] + +TabWidth: 4 + +TypeNames: [ ] + +TypenameMacros: [ ] + +UseTab: Never + +WhitespaceSensitiveMacros: [ ] \ No newline at end of file diff --git a/ROADMAP.md b/ROADMAP.md index f270f51..089ecae 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -21,4 +21,4 @@ ## 29/12/2024 -- [ ] start writing a task system, with a custom scheduler (?) (based on a multicore priority-based round robin scheduler) \ No newline at end of file +- [ ] start writing a task system, with a custom scheduler (?) (based on a multicore priority-based round robin scheduler) diff --git a/scripts/clang-format b/scripts/clang-format new file mode 100755 index 0000000..64439b0 Binary files /dev/null and b/scripts/clang-format differ diff --git a/zerOS/src/boot/early_printk.c b/zerOS/src/boot/early_printk.c index c053e60..533a1e3 100644 --- a/zerOS/src/boot/early_printk.c +++ b/zerOS/src/boot/early_printk.c @@ -1,27 +1,28 @@ +#include #include #include -#include #include -#include - -#include -#include -#include - -#include -#include - -#include +#include +#include +#include #include #include -#include -#include -#include #include +#include +#include #include -#include +#include + +#include +#include +#include + +#include +#include + +// clang-format off #undef __digit_chars_lower #undef __digit_chars_upper #undef __digit_chars_len @@ -54,6 +55,8 @@ ) \ ) +// clang-format on + #if 0 #ifndef __INTELLISENSE__ static const char lut_lower[] = __digits_tablelookup_build(__digit_chars_lower); @@ -84,7 +87,7 @@ static const int8_t mins8 = INT8_MIN; BOOT_FUNC static inline void* boot_memcpy(void* restrict dest, const void* restrict src, size_t n) { - unsigned char* d = dest; + unsigned char* d = dest; const unsigned char* s = src; while (n--) *d++ = *s++; @@ -92,10 +95,7 @@ static inline void* boot_memcpy(void* restrict dest, const void* restrict src, s } BOOT_FUNC -static bool is_transmit_empty(enum zerOS_serial_port port) -{ - return zerOS_inb(port + 5) & 0x20; -} +static bool is_transmit_empty(enum zerOS_serial_port port) { return zerOS_inb(port + 5) & 0x20; } BOOT_FUNC static void write_serial(enum zerOS_serial_port port, char a) @@ -103,7 +103,8 @@ static void write_serial(enum zerOS_serial_port port, char a) if (!zerOS_in_qemu() || !zerOS_CONFIG_UNDER_QEMU) return; - while (is_transmit_empty(port) == 0); + while (is_transmit_empty(port) == 0) + ; zerOS_outb(port, a); } @@ -150,9 +151,9 @@ extern int zerOS_early_vprintk(const char* str, va_list varargs) { case 'd': { static const char basic_lut[] = "0123456789"; - int64_t value = va_arg(varargs, int64_t); - char buffer[64]; - int i = 0; + int64_t value = va_arg(varargs, int64_t); + int i = 0; + char buffer[64]; if (value < 0) { write_serial(zerOS_SERIAL_DEBUG, '-'); @@ -163,7 +164,7 @@ extern int zerOS_early_vprintk(const char* str, va_list varargs) do { buffer[i++] = basic_lut[value % 10]; - value /= 10; + value /= 10; } while (value); while (i--) { @@ -171,16 +172,17 @@ extern int zerOS_early_vprintk(const char* str, va_list varargs) write_debugcon(buffer[i]); written++; } - } break; + } + break; case 'u': { static const char basic_lut[] = "0123456789"; - uint64_t value = va_arg(varargs, uint64_t); - char buffer[64]; - int i = 0; + uint64_t value = va_arg(varargs, uint64_t); + int i = 0; + char buffer[64]; do { buffer[i++] = basic_lut[value % 10]; - value /= 10; + value /= 10; } while (value); while (i--) { @@ -188,16 +190,17 @@ extern int zerOS_early_vprintk(const char* str, va_list varargs) write_debugcon(buffer[i]); written++; } - } break; + } + break; case 'x': { static const char basic_lut_lower[] = "0123456789abcdef"; - uint64_t value = va_arg(varargs, uint64_t); - char buffer[64]; - int i = 0; + uint64_t value = va_arg(varargs, uint64_t); + int i = 0; + char buffer[64]; do { buffer[i++] = basic_lut_lower[value % 16]; - value /= 16; + value /= 16; } while (value); while (i--) { @@ -205,16 +208,17 @@ extern int zerOS_early_vprintk(const char* str, va_list varargs) write_debugcon(buffer[i]); written++; } - } break; + } + break; case 'X': { static const char basic_lut_upper[] = "0123456789ABCDEF"; - uint64_t value = va_arg(varargs, uint64_t); - char buffer[64]; - int i = 0; + uint64_t value = va_arg(varargs, uint64_t); + int i = 0; + char buffer[64]; do { buffer[i++] = basic_lut_upper[value % 16]; - value /= 16; + value /= 16; } while (value); while (i--) { @@ -222,16 +226,17 @@ extern int zerOS_early_vprintk(const char* str, va_list varargs) write_debugcon(buffer[i]); written++; } - } break; + } + break; case 'p': { static const char basic_lut_lower[] = "0123456789abcdef"; - uintptr_t value = va_arg(varargs, uintptr_t); - char buffer[64]; - int i = 0; + uintptr_t value = va_arg(varargs, uintptr_t); + int i = 0; + char buffer[64]; do { buffer[i++] = basic_lut_lower[value % 16]; - value /= 16; + value /= 16; } while (value); while (i--) { @@ -239,16 +244,17 @@ extern int zerOS_early_vprintk(const char* str, va_list varargs) write_debugcon(buffer[i]); written++; } - } break; + } + break; case 'b': { static const char basic_lut[] = "01"; - uint64_t value = va_arg(varargs, uint64_t); - char buffer[64]; - int i = 0; + uint64_t value = va_arg(varargs, uint64_t); + int i = 0; + char buffer[64]; do { buffer[i++] = basic_lut[value % 2]; - value /= 2; + value /= 2; } while (value); while (i--) { @@ -256,16 +262,17 @@ extern int zerOS_early_vprintk(const char* str, va_list varargs) write_debugcon(buffer[i]); written++; } - } break; + } + break; case 'o': { static const char basic_lut[] = "01234567"; - uint64_t value = va_arg(varargs, uint64_t); - char buffer[64]; - int i = 0; + uint64_t value = va_arg(varargs, uint64_t); + int i = 0; + char buffer[64]; do { buffer[i++] = basic_lut[value % 8]; - value /= 8; + value /= 8; } while (value); while (i--) { @@ -273,13 +280,15 @@ extern int zerOS_early_vprintk(const char* str, va_list varargs) write_debugcon(buffer[i]); written++; } - } break; + } + break; case 'c': { char value = va_arg(varargs, int); write_serial(zerOS_SERIAL_DEBUG, value); write_debugcon(value); written++; - } break; + } + break; case 's': { const char* value = va_arg(varargs, const char*); while (*value) @@ -289,19 +298,22 @@ extern int zerOS_early_vprintk(const char* str, va_list varargs) written++; value++; } - } break; + } + break; case 'n': { - BOOT_FUNC PRINTF_LIKE(1, 2) - extern int zerOS_early_printk(const char* str, ...); + BOOT_FUNC PRINTF_LIKE(1, 2) extern int zerOS_early_printk(const char* str, ...); written += zerOS_early_printk("%d", written); - } break; + } + break; case '%': { write_serial(zerOS_SERIAL_DEBUG, '%'); write_debugcon('%'); written++; - } break; + } + break; - default: break; + default: + break; } } else @@ -326,7 +338,7 @@ BOOT_FUNC */ extern int zerOS_early_printk(const char* str, ...) { - int written = 0; + int written = 0; va_list args; va_start(args, str); @@ -334,4 +346,4 @@ extern int zerOS_early_printk(const char* str, ...) va_end(args); return written; -} \ No newline at end of file +} diff --git a/zerOS/src/boot/limine_requests.c b/zerOS/src/boot/limine_requests.c index a83d528..e463878 100644 --- a/zerOS/src/boot/limine_requests.c +++ b/zerOS/src/boot/limine_requests.c @@ -1,21 +1,22 @@ #include +#include +#include #include #include -#include -#include #include -#include -#include -#include #include +#include +#include +#include #include #include #include +// clang-format off #ifdef LIMINE_REQUESTED_REVISION #error "LIMINE_REQUESTED_REVISION shall not be defined before this point" #endif @@ -30,6 +31,7 @@ #undef __MAX_MEMMAP_ENTRY_COUNT #define __MAX_MEMMAP_ENTRY_COUNT 256 +// clang-format on static struct limine_paging_mode_response paging_response; @@ -54,27 +56,36 @@ BOOT_FUNC static char* limine_entry_type_string(uint64_t type) { static char strings[8][32] = { - [LIMINE_MEMMAP_USABLE] = "USABLE\0", - [LIMINE_MEMMAP_RESERVED] = "RESERVED\0", - [LIMINE_MEMMAP_ACPI_RECLAIMABLE] = "ACPI_RECLAIMABLE\0", - [LIMINE_MEMMAP_ACPI_NVS] = "ACPI_NVS\0", - [LIMINE_MEMMAP_BAD_MEMORY] = "BAD_MEMORY\0", - [LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE] = "BOOTLOADER_RECLAIMABLE\0", - [LIMINE_MEMMAP_KERNEL_AND_MODULES] = "KERNEL_AND_MODULES\0", - [LIMINE_MEMMAP_FRAMEBUFFER] = "FRAMEBUFFER\0" + [LIMINE_MEMMAP_USABLE] = "USABLE\0", + [LIMINE_MEMMAP_RESERVED] = "RESERVED\0", + [LIMINE_MEMMAP_ACPI_RECLAIMABLE] = "ACPI_RECLAIMABLE\0", + [LIMINE_MEMMAP_ACPI_NVS] = "ACPI_NVS\0", + [LIMINE_MEMMAP_BAD_MEMORY] = "BAD_MEMORY\0", + [LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE] = "BOOTLOADER_RECLAIMABLE\0", + [LIMINE_MEMMAP_KERNEL_AND_MODULES] = "KERNEL_AND_MODULES\0", + [LIMINE_MEMMAP_FRAMEBUFFER] = "FRAMEBUFFER\0" }; static char unknown[32] = "UNKNOWN\0"; switch (type) { - case LIMINE_MEMMAP_USABLE: CASE_FALLTHROUGH; - case LIMINE_MEMMAP_RESERVED: CASE_FALLTHROUGH; - case LIMINE_MEMMAP_ACPI_RECLAIMABLE: CASE_FALLTHROUGH; - case LIMINE_MEMMAP_ACPI_NVS: CASE_FALLTHROUGH; - case LIMINE_MEMMAP_BAD_MEMORY: CASE_FALLTHROUGH; - case LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE: CASE_FALLTHROUGH; - case LIMINE_MEMMAP_KERNEL_AND_MODULES: CASE_FALLTHROUGH; - case LIMINE_MEMMAP_FRAMEBUFFER: return strings[type]; - default: return unknown; + case LIMINE_MEMMAP_USABLE: + CASE_FALLTHROUGH; + case LIMINE_MEMMAP_RESERVED: + CASE_FALLTHROUGH; + case LIMINE_MEMMAP_ACPI_RECLAIMABLE: + CASE_FALLTHROUGH; + case LIMINE_MEMMAP_ACPI_NVS: + CASE_FALLTHROUGH; + case LIMINE_MEMMAP_BAD_MEMORY: + CASE_FALLTHROUGH; + case LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE: + CASE_FALLTHROUGH; + case LIMINE_MEMMAP_KERNEL_AND_MODULES: + CASE_FALLTHROUGH; + case LIMINE_MEMMAP_FRAMEBUFFER: + return strings[type]; + default: + return unknown; } }; @@ -86,25 +97,24 @@ static void print_entries(struct limine_memmap_entry* entries, size_t entry_coun for (size_t i = 0; i < entry_count; i++) { struct limine_memmap_entry entry = entries[i]; - zerOS_early_printk("zerOS: entry %u: base = 0x%p, length = 0x%x, type = %s\n", - EPRI_CAST(u, i), - EPRI_CAST(p, entry.base), - EPRI_CAST(x, entry.length), - EPRI_CAST(s, limine_entry_type_string(entry.type)) + zerOS_early_printk( + "zerOS: entry %u: base = 0x%p, length = 0x%x, type = %s\n", EPRI_CAST(u, i), + EPRI_CAST(p, entry.base), EPRI_CAST(x, entry.length), + EPRI_CAST(s, limine_entry_type_string(entry.type)) ); } } else { - struct limine_memmap_response* response = (struct limine_memmap_response*)zerOS_get_limine_data(zerOS_LIMINE_MEMMAP_RESPONSE); + struct limine_memmap_response* response = + (struct limine_memmap_response*)zerOS_get_limine_data(zerOS_LIMINE_MEMMAP_RESPONSE); for (size_t i = 0; i < response->entry_count; i++) { struct limine_memmap_entry* entry = zerOS_get_limine_data(zerOS_LIMINE_MEMMAP_ENTRY, i); - zerOS_early_printk("zerOS: entry %u: base = 0x%p, length = 0x%x, type = %s\n", - EPRI_CAST(u, i), - EPRI_CAST(p, entry->base), - EPRI_CAST(x, entry->length), - EPRI_CAST(s, limine_entry_type_string(entry->type)) + zerOS_early_printk( + "zerOS: entry %u: base = 0x%p, length = 0x%x, type = %s\n", EPRI_CAST(u, i), + EPRI_CAST(p, entry->base), EPRI_CAST(x, entry->length), + EPRI_CAST(s, limine_entry_type_string(entry->type)) ); } } @@ -153,7 +163,7 @@ extern void* zerOS_get_limine_data(enum zerOS_limine_data_request req, ...) va_list args; va_start(args, req); size_t framebuffer_id = va_arg(args, size_t); - size_t video_mode_id = va_arg(args, size_t); + size_t video_mode_id = va_arg(args, size_t); va_end(args); if (framebuffer_id >= framebuffer_response.framebuffer_count || video_mode_id >= framebuffers[framebuffer_id].mode_count) @@ -171,31 +181,29 @@ extern void* zerOS_get_limine_data(enum zerOS_limine_data_request req, ...) return memmap_entry_buf + entry_id; }; - default: return nullptr; + default: + return nullptr; } } BOOT_FUNC static inline void* boot_memcpy(void* restrict dest, const void* restrict src, size_t n) { - unsigned char* d = dest; + unsigned char* d = dest; const unsigned char* s = src; while (n--) *d++ = *s++; return dest; } -#undef __assert_good_response -#define __assert_good_response(request, additionalchecks) \ - if ((request).response == nullptr || \ - (request).revision != LIMINE_REQUESTED_REVISION || \ - !(additionalchecks)) \ - { \ - zerOS_early_printk( \ - "zerOS: no good response for request `%s`\n", \ - EPRI_CAST(s, #request) \ - ); \ - zerOS_hcf(); \ +#undef __assert_good_response +#define __assert_good_response(request, additionalchecks) \ + if ((request).response == nullptr || \ + (request).revision != LIMINE_REQUESTED_REVISION || \ + !(additionalchecks)) \ + { \ + zerOS_early_printk("zerOS: no good response for request `%s`\n", EPRI_CAST(s, #request)); \ + zerOS_hcf(); \ } BOOT_FUNC @@ -204,25 +212,26 @@ static void copy_framebuffers(void) if (framebuffer_response.framebuffer_count > __MAX_FB_COUNT) { zerOS_early_printk( - "zerOS: framebuffer count is too high (%u)\n", - EPRI_CAST(u, framebuffer_response.framebuffer_count) + "zerOS: framebuffer count is too high (%u)\n", + EPRI_CAST(u, framebuffer_response.framebuffer_count) ); zerOS_hcf(); } for (size_t i = 0; i < framebuffer_response.framebuffer_count; i++) { - boot_memcpy(framebuffers + i, *(framebuffer_response.framebuffers + i), sizeof(struct limine_framebuffer)); + boot_memcpy( + framebuffers + i, *(framebuffer_response.framebuffers + i), + sizeof(struct limine_framebuffer) + ); zerOS_early_printk( - "zerOS: framebuffer %u: address = 0x%p\n", - EPRI_CAST(u, i), - EPRI_CAST(p, framebuffers[i].address) + "zerOS: framebuffer %u: address = 0x%p\n", EPRI_CAST(u, i), + EPRI_CAST(p, framebuffers[i].address) ); - + if (framebuffers[i].mode_count > __MAX_FB_VIDEO_MODE_COUNT) { zerOS_early_printk( - "zerOS: video mode count is too high (%u)\n", - EPRI_CAST(u, framebuffers[i].mode_count) + "zerOS: video mode count is too high (%u)\n", EPRI_CAST(u, framebuffers[i].mode_count) ); zerOS_hcf(); } @@ -239,14 +248,15 @@ static void copy_memmap_entries(void) if (memmap_response.entry_count > __MAX_MEMMAP_ENTRY_COUNT) { zerOS_early_printk( - "zerOS: memmap entry count is too high (%u)\n", - EPRI_CAST(u, memmap_response.entry_count) + "zerOS: memmap entry count is too high (%u)\n", EPRI_CAST(u, memmap_response.entry_count) ); zerOS_hcf(); } for (size_t i = 0; i < memmap_response.entry_count; i++) { - boot_memcpy(memmap_entry_buf + i, *(memmap_response.entries + i), sizeof(struct limine_memmap_entry)); + boot_memcpy( + memmap_entry_buf + i, *(memmap_response.entries + i), sizeof(struct limine_memmap_entry) + ); if (memmap_entry_buf[i].type == LIMINE_MEMMAP_USABLE || memmap_entry_buf[i].type == LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE) usable_count++; @@ -259,8 +269,7 @@ static void copy_memmap_entries(void) if (unlikely(usable_count > zerOS_CONFIG_MAX_USABLE_MEMORY_REGIONS)) { zerOS_early_printk( - "zerOS: too many usable memory regions found (%u)\n", - EPRI_CAST(u, usable_count) + "zerOS: too many usable memory regions found (%u)\n", EPRI_CAST(u, usable_count) ); zerOS_hcf(); } @@ -278,27 +287,30 @@ extern void zerOS_copy_limine_requests(void) } __assert_good_response( - lvl5_paging_request, - lvl5_paging_request.response->mode == LIMINE_PAGING_MODE_X86_64_5LVL || - lvl5_paging_request.response->mode == LIMINE_PAGING_MODE_X86_64_4LVL + lvl5_paging_request, lvl5_paging_request.response->mode == LIMINE_PAGING_MODE_X86_64_5LVL || + lvl5_paging_request.response->mode == LIMINE_PAGING_MODE_X86_64_4LVL ); boot_memcpy(&paging_response, lvl5_paging_request.response, sizeof(paging_response)); __assert_good_response( - framebuffer_request, - framebuffer_request.response->framebuffer_count > 0 && - framebuffer_request.response->framebuffers != nullptr + framebuffer_request, framebuffer_request.response->framebuffer_count > 0 && + framebuffer_request.response->framebuffers != nullptr ); boot_memcpy(&framebuffer_response, framebuffer_request.response, sizeof(framebuffer_response)); copy_framebuffers(); __assert_good_response(firmware_type_request, true); - boot_memcpy(&firmware_type_response, firmware_type_request.response, sizeof(firmware_type_response)); + boot_memcpy( + &firmware_type_response, firmware_type_request.response, sizeof(firmware_type_response) + ); __assert_good_response(hhdm_request, true); boot_memcpy(&hhdm_response, hhdm_request.response, sizeof(hhdm_response)); - zerOS_early_printk("zerOS: kernel HHDM = 0x%x\n", - EPRI_CAST(x, ((struct limine_hhdm_response*) zerOS_get_limine_data(zerOS_LIMINE_HHDM_RESPONSE))->offset) + zerOS_early_printk( + "zerOS: kernel HHDM = 0x%x\n", + EPRI_CAST( + x, ((struct limine_hhdm_response*)zerOS_get_limine_data(zerOS_LIMINE_HHDM_RESPONSE))->offset + ) ); __assert_good_response(memmap_request, true); @@ -310,12 +322,18 @@ extern void zerOS_copy_limine_requests(void) boot_memcpy(&efi_memmap_response, efi_memmap_request.response, sizeof(efi_memmap_response)); __assert_good_response(efi_system_table_request, true); - boot_memcpy(&efi_system_table_response, efi_system_table_request.response, sizeof(efi_system_table_response)); + boot_memcpy( + &efi_system_table_response, efi_system_table_request.response, + sizeof(efi_system_table_response) + ); __assert_good_response(kernel_address_request, true); - boot_memcpy(&kernel_address_response, kernel_address_request.response, sizeof(kernel_address_response)); - zerOS_early_printk("zerOS: kernel address base: physical = 0x%p, virtual = 0x%p\n", - EPRI_CAST(p, kernel_address_response.physical_base), - EPRI_CAST(p, kernel_address_response.virtual_base) + boot_memcpy( + &kernel_address_response, kernel_address_request.response, sizeof(kernel_address_response) ); -} \ No newline at end of file + zerOS_early_printk( + "zerOS: kernel address base: physical = 0x%p, virtual = 0x%p\n", + EPRI_CAST(p, kernel_address_response.physical_base), + EPRI_CAST(p, kernel_address_response.virtual_base) + ); +} diff --git a/zerOS/src/kernel/kmain.c b/zerOS/src/kernel/kmain.c index e63a536..89a7852 100644 --- a/zerOS/src/kernel/kmain.c +++ b/zerOS/src/kernel/kmain.c @@ -1,22 +1,20 @@ -#include -#include #include +#include + +#include -#include -#include -#include -#include #include +#include #include -#include +#include #include #include #include +#include +#include +#include -static bool setup_paging(void) -{ - return true; -} +static bool setup_paging(void) { return true; } static bool setup_printk_subsystem(void) { return true; } static bool setup_idt(void) { return true; } @@ -44,16 +42,23 @@ void zerOS_kmain(void) zerOS_hcf(); } - struct limine_framebuffer* framebuffer = (struct limine_framebuffer*) zerOS_get_limine_data(zerOS_LIMINE_FRAMEBUFFER, (size_t)0); + struct limine_framebuffer* framebuffer = + (struct limine_framebuffer*)zerOS_get_limine_data(zerOS_LIMINE_FRAMEBUFFER, (size_t)0); zerOS_early_printk("zerOS: writting to framebuffer to ensure everything is working\n"); - zerOS_early_printk("zerOS: framebuffer virtual address: 0x%p\n", EPRI_CAST(p, framebuffer->address)); - //zerOS_early_printk("zerOS: framebuffer physical address: 0x%p\n", zerOS_virt_to_phys((uintptr_t)framebuffer->address)); - // Note: we assume the framebuffer model is RGB with 32-bit pixels. - for (size_t i = 0; i < 100; i++) { - volatile uint32_t* fb_ptr = framebuffer->address; - fb_ptr[i * (framebuffer->pitch / 4) + i] = 0xffffff; + zerOS_early_printk( + "zerOS: framebuffer virtual address: 0x%p\n", EPRI_CAST(p, framebuffer->address) + ); + // zerOS_early_printk("zerOS: framebuffer physical address: 0x%p\n", + // zerOS_virt_to_phys((uintptr_t)framebuffer->address) + // ); + // Note: we assume the framebuffer model is RGB with 32-bit pixels. + for (size_t i = 0; i < 100; i++) + { + volatile uint32_t* fb_ptr = framebuffer->address; + fb_ptr[i * (framebuffer->pitch / 4) + i] = 0xff'ff'ff; } - while (true) zerOS_hcf(); -} \ No newline at end of file + while (true) + zerOS_hcf(); +} diff --git a/zerOS/src/kernel/memory/paging.c b/zerOS/src/kernel/memory/paging.c index 3e97991..e6f5b33 100644 --- a/zerOS/src/kernel/memory/paging.c +++ b/zerOS/src/kernel/memory/paging.c @@ -1,11 +1,13 @@ #include -#include -#include #include +#include +#include #include +#include + #include #include #include @@ -13,15 +15,17 @@ #include #include -#include - extern bool zerOS_init_early_paging(void) { - struct limine_memmap_response* memmap_resp = (struct limine_memmap_response*) zerOS_get_limine_data(zerOS_LIMINE_MEMMAP_RESPONSE); - struct limine_memmap_entry* memmap = (struct limine_memmap_entry*) zerOS_get_limine_data(zerOS_LIMINE_MEMMAP_ENTRY, (size_t)0); + struct limine_memmap_response* memmap_resp = + (struct limine_memmap_response*)zerOS_get_limine_data(zerOS_LIMINE_MEMMAP_RESPONSE); + struct limine_memmap_entry* memmap = + (struct limine_memmap_entry*)zerOS_get_limine_data(zerOS_LIMINE_MEMMAP_ENTRY, (size_t)0); const size_t memmap_entry_count = memmap_resp->entry_count; - struct limine_hhdm_response* hhdm = (struct limine_hhdm_response*) zerOS_get_limine_data(zerOS_LIMINE_HHDM_RESPONSE); + struct limine_hhdm_response* hhdm = + (struct limine_hhdm_response*)zerOS_get_limine_data(zerOS_LIMINE_HHDM_RESPONSE); - struct limine_kernel_address_response* kernaddr = (struct limine_kernel_address_response*) zerOS_get_limine_data(zerOS_LIMINE_KERNEL_ADDRESS_RESPONSE); -} \ No newline at end of file + struct limine_kernel_address_response* kernaddr = (struct limine_kernel_address_response*) + zerOS_get_limine_data(zerOS_LIMINE_KERNEL_ADDRESS_RESPONSE); +} diff --git a/zerOS/src/kernel/memory/pmm.c b/zerOS/src/kernel/memory/pmm.c index 0c4cef5..3ee6ac2 100644 --- a/zerOS/src/kernel/memory/pmm.c +++ b/zerOS/src/kernel/memory/pmm.c @@ -1,26 +1,26 @@ #include -#include -#include #include +#include +#include #include -#include +#include +#include +#include + +#include +#include #include #include #include -#include -#include +#include #include #include #include -#include -#include -#include - #if 0 // TODO: Make this struct accept size in number of bits (i.e. number of pages) instead of size in number of zerOS_BITSET_UNDERLYING_TYPE elements struct pmm_basic_manager @@ -35,12 +35,13 @@ struct pmm_basic_manager #else struct pmm_basic_manager { - size_t limine_entry_index; ///< The index of the limine entry. - size_t base_page_index; ///< The index of the first page, in physical memory. - bitset_t bitmap; ///< The bitmap. - bitset_t bitmap_physaddr; ///< The physical address of the bitmap. - size_t next_free; ///< The index of the next free page, in the bitmap. - size_t size; ///< The size of the bitmap, in bits (i.e. page_count or size_in_bytes * __CHAR_BIT__). + size_t limine_entry_index; ///< The index of the limine entry. + size_t base_page_index; ///< The index of the first page, in physical memory. + bitset_t bitmap; ///< The bitmap. + bitset_t bitmap_physaddr; ///< The physical address of the bitmap. + size_t next_free; ///< The index of the next free page, in the bitmap. + size_t size; ///< The size of the bitmap, in bits \ + (i.e. page_count or size_in_bytes * __CHAR_BIT__). }; #endif @@ -51,8 +52,8 @@ static inline size_t calc_bitmap_size(size_t page_count) if (page_count % zerOS_fast_uint_bits != 0) { zerOS_early_printk( - "zerOS: unhandled code path: page count " - "is not a multiple of zerOS_fast_uint_bits\n" + "zerOS: unhandled code path: page count " + "is not a multiple of zerOS_fast_uint_bits\n" ); zerOS_hcf(); } @@ -61,14 +62,19 @@ static inline size_t calc_bitmap_size(size_t page_count) } // Returns the index of the memory map entry where the bitmap will be located -static size_t find_proper_bitmaps_loc(size_t* manageable, size_t manageable_count, size_t memory_needs) +static size_t +find_proper_bitmaps_loc(size_t* manageable, size_t manageable_count, size_t memory_needs) { for (size_t i = 0; i < manageable_count; i++) { - struct limine_memmap_entry* entry = (struct limine_memmap_entry*) zerOS_get_limine_data(zerOS_LIMINE_MEMMAP_ENTRY, manageable[i]); + struct limine_memmap_entry* entry = (struct limine_memmap_entry*)zerOS_get_limine_data( + zerOS_LIMINE_MEMMAP_ENTRY, manageable[i] + ); const uint64_t entry_top = entry->base + entry->length; - - if (entry->type == LIMINE_MEMMAP_USABLE && entry->length >= memory_needs && entry_top <= UINT64_C(4) * GiB) + + if (entry->type == LIMINE_MEMMAP_USABLE && + entry->length >= memory_needs && + entry_top <= UINT64_C(4) * GiB) return i; } @@ -79,27 +85,27 @@ static size_t find_proper_bitmaps_loc(size_t* manageable, size_t manageable_coun } static void init_basic_managers( - const uint64_t hhdm_offset, - struct limine_memmap_entry* bitmap_entry, - size_t* manageable, size_t* needs, size_t manageable_count, - size_t needs_sum + const uint64_t hhdm_offset, struct limine_memmap_entry* bitmap_entry, size_t* manageable, + size_t* needs, size_t manageable_count, size_t needs_sum ) { intptr_t current_offset = bitmap_entry->base + bitmap_entry->length; for (size_t i = manageable_count; i != 0; --i) { - const size_t realind = i - 1; - struct limine_memmap_entry* entry = (struct limine_memmap_entry*) zerOS_get_limine_data(zerOS_LIMINE_MEMMAP_ENTRY, manageable[realind]); + const size_t realind = i - 1; + struct limine_memmap_entry* entry = (struct limine_memmap_entry*)zerOS_get_limine_data( + zerOS_LIMINE_MEMMAP_ENTRY, manageable[realind] + ); current_offset -= (intptr_t)(needs[realind]); struct pmm_basic_manager* manager = &pmm_main_managers[realind]; - manager->limine_entry_index = manageable[realind]; - manager->base_page_index = entry->base / zerOS_PAGE_SIZE; - manager->size = needs[realind] * 8; - manager->bitmap_physaddr = (bitset_t)current_offset; - manager->bitmap = (bitset_t)(hhdm_offset + current_offset); - manager->next_free = 0; + manager->limine_entry_index = manageable[realind]; + manager->base_page_index = entry->base / zerOS_PAGE_SIZE; + manager->size = needs[realind] * 8; + manager->bitmap_physaddr = (bitset_t)current_offset; + manager->bitmap = (bitset_t)(hhdm_offset + current_offset); + manager->next_free = 0; if (entry->type == LIMINE_MEMMAP_USABLE) zerOS_bitset_clear_all(manager->bitmap, manager->size); @@ -115,7 +121,8 @@ static void init_basic_managers( zerOS_hcf(); } - const size_t occupied_pages = (needs_sum / zerOS_PAGE_SIZE) + ((needs_sum % zerOS_PAGE_SIZE) != 0 ? 1 : 0); + const size_t occupied_pages = + (needs_sum / zerOS_PAGE_SIZE) + ((needs_sum % zerOS_PAGE_SIZE) != 0 ? 1 : 0); for (size_t j = 0; j < occupied_pages; j++) zerOS_bitset_set(manager->bitmap, j); } @@ -132,54 +139,57 @@ static size_t __static_do_sum(size_t* arr, size_t count) extern bool zerOS_init_pmm(void) { - struct limine_hhdm_response* hhdm = (struct limine_hhdm_response*) zerOS_get_limine_data(zerOS_LIMINE_HHDM_RESPONSE); + struct limine_hhdm_response* hhdm = + (struct limine_hhdm_response*)zerOS_get_limine_data(zerOS_LIMINE_HHDM_RESPONSE); const uint64_t hhdm_offset = hhdm->offset; size_t manageable[zerOS_CONFIG_MAX_USABLE_MEMORY_REGIONS]; size_t manageable_count = 0; - + // in bytes size_t memory_needs[zerOS_CONFIG_MAX_USABLE_MEMORY_REGIONS]; - struct limine_memmap_response* memmap_resp = (struct limine_memmap_response*) zerOS_get_limine_data(zerOS_LIMINE_MEMMAP_RESPONSE); + struct limine_memmap_response* memmap_resp = + (struct limine_memmap_response*)zerOS_get_limine_data(zerOS_LIMINE_MEMMAP_RESPONSE); const size_t memmap_entry_count = memmap_resp->entry_count; for (size_t i = 0; i < memmap_entry_count; i++) { - struct limine_memmap_entry* entry = (struct limine_memmap_entry*) zerOS_get_limine_data(zerOS_LIMINE_MEMMAP_ENTRY, i); - if (entry->type == LIMINE_MEMMAP_USABLE || entry->type == LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE) + struct limine_memmap_entry* entry = + (struct limine_memmap_entry*)zerOS_get_limine_data(zerOS_LIMINE_MEMMAP_ENTRY, i); + if (entry->type == LIMINE_MEMMAP_USABLE || + entry->type == LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE) { const uint64_t div = entry->length / zerOS_PAGE_SIZE, mod = entry->length % zerOS_PAGE_SIZE; if (unlikely(mod != 0)) { - zerOS_early_printk("zerOS: error: memmap entry length is not a multiple of the page size\n"); + zerOS_early_printk( + "zerOS: error: memmap entry length is not a multiple of the page size\n" + ); zerOS_hcf(); } size_t bmsize = calc_bitmap_size(div); - //pmm_main_managers[manageable_count].limine_entry_index = i; - //pmm_main_managers[manageable_count].base_page_index = entry->base / zerOS_PAGE_SIZE; - //pmm_main_managers[manageable_count].size = bmsize / sizeof(zerOS_fast_uint_t); - manageable[manageable_count] = i; + // pmm_main_managers[manageable_count].limine_entry_index = i; + // pmm_main_managers[manageable_count].base_page_index = entry->base / zerOS_PAGE_SIZE; + // pmm_main_managers[manageable_count].size = bmsize / sizeof(zerOS_fast_uint_t); + manageable[manageable_count] = i; memory_needs[manageable_count] = bmsize; ++manageable_count; } } - const size_t sum = __static_do_sum(memory_needs, manageable_count); + const size_t sum = __static_do_sum(memory_needs, manageable_count); const size_t bitmap_loc = find_proper_bitmaps_loc(manageable, manageable_count, sum); - struct limine_memmap_entry* bitmap_entry = (struct limine_memmap_entry*) zerOS_get_limine_data(zerOS_LIMINE_MEMMAP_ENTRY, bitmap_loc); + struct limine_memmap_entry* bitmap_entry = + (struct limine_memmap_entry*)zerOS_get_limine_data(zerOS_LIMINE_MEMMAP_ENTRY, bitmap_loc); - init_basic_managers( - hhdm_offset, bitmap_entry, - manageable, memory_needs, - manageable_count, sum - ); + init_basic_managers(hhdm_offset, bitmap_entry, manageable, memory_needs, manageable_count, sum); } extern bool zerOS_pmm_alloc_frames(uintptr_t* filled, size_t count) { // TODO return false; -} \ No newline at end of file +}