Use clang-format

This commit is contained in:
2025-12-21 22:53:25 +01:00
parent 8794a61073
commit b2d8294b12
36 changed files with 925 additions and 842 deletions

37
.editorconfig Normal file
View File

@@ -0,0 +1,37 @@
root = true
# Default for all files
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
# C / header files
[*.{c,h}]
indent_style = space
indent_size = 2
tab_width = 2
max_line_length = 80
# Assembly (if present; usually tab-sensitive)
[*.S]
indent_style = tab
tab_width = 8
trim_trailing_whitespace = false
# Makefiles (MUST use tabs)
[Makefile]
indent_style = tab
tab_width = 8
trim_trailing_whitespace = false
[*.mk]
indent_style = tab
tab_width = 8
trim_trailing_whitespace = false
# Markdown (avoid wrapping conflicts)
[*.md]
trim_trailing_whitespace = false
max_line_length = off

View File

@@ -6,4 +6,7 @@ all_kernel:
clean_kernel:
make -C kernel platform=$(platform) clean
.PHONY: all_kernel clean_kernel
format_kernel:
make -C kernel platform=$(platform) format
.PHONY: all_kernel clean_kernel format_kernel

57
kernel/.clang-format Normal file
View File

@@ -0,0 +1,57 @@
BasedOnStyle: LLVM
Language: C
# Indentation
IndentWidth: 2
TabWidth: 2
UseTab: Never
# Braces and blocks
BreakBeforeBraces: Attach
BraceWrapping:
AfterFunction: false
AfterControlStatement: false
AfterStruct: false
AfterEnum: false
AfterUnion: false
BeforeElse: false
# Control statements
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortBlocksOnASingleLine: Never
# Line breaking
ColumnLimit: 80
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakStringLiterals: false
# Spacing
SpaceBeforeParens: Always
SpaceBeforeAssignmentOperators: true
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
# Pointer alignment
PointerAlignment: Left
DerivePointerAlignment: false
# Alignment
AlignAfterOpenBracket: DontAlign
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignOperands: false
# Includes
SortIncludes: true
# Comments
ReflowComments: false
CommentPragmas: '^ IWYU pragma:'
# Misc
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 1

View File

@@ -23,4 +23,13 @@ build/kernel.elf: $(o)
clean:
rm -f $(o) build/kernel.elf
.PHONY: all clean
format:
clang-format -i $$(git ls-files '*.c' '*.h' \
':!limine/limine.h' \
':!c_headers/include/**' \
':!uACPI/source/**' \
':!uACPI/include/**' \
':!uACPI/tests/**' \
':!libk/printf*')
.PHONY: all clean format

View File

@@ -1,23 +1,26 @@
#include <limine/limine.h>
#include <amd64/init.h>
#include <sys/debug.h>
#include <mm/pmm.h>
#include <limine/limine.h>
#include <mm/liballoc.h>
#include <mm/pmm.h>
#include <sys/debug.h>
#include <uacpi/uacpi.h>
#define UACPI_MEMORY_BUFFER_MAX 4096
__attribute__((aligned(16))) static uint8_t uacpi_memory_buffer[UACPI_MEMORY_BUFFER_MAX];
__attribute__ ((
aligned (16))) static uint8_t uacpi_memory_buffer[UACPI_MEMORY_BUFFER_MAX];
void bootmain (void) {
amd64_init ();
pmm_init ();
uacpi_setup_early_table_access((void *)uacpi_memory_buffer, sizeof(uacpi_memory_buffer));
uacpi_setup_early_table_access (
(void*)uacpi_memory_buffer, sizeof (uacpi_memory_buffer));
int* a = malloc (sizeof (int));
*a = 6969;
DEBUG ("a=%p, *a=%d\n", a, *a);
for (;;);
for (;;)
;
}

View File

@@ -1,9 +1,9 @@
#include <libk/std.h>
#include <libk/string.h>
#include <libk/printf.h>
#include <sys/debug.h>
#include <amd64/debug.h>
#include <amd64/io.h>
#include <libk/printf.h>
#include <libk/std.h>
#include <libk/string.h>
#include <sys/debug.h>
#define PORT_COM1 0x03F8
#define BUFFER_SIZE 1024
@@ -13,7 +13,8 @@ static bool amd64_debug_serial_tx_empty(void) {
}
static void amd64_debug_serial_write (char x) {
while (!amd64_debug_serial_tx_empty());
while (!amd64_debug_serial_tx_empty ())
;
amd64_io_outb (PORT_COM1, (uint8_t)x);
}

View File

@@ -1,9 +1,9 @@
#include <amd64/debug.h>
#include <amd64/init.h>
#include <amd64/intr.h>
#include <amd64/tss.h>
#include <libk/std.h>
#include <libk/string.h>
#include <amd64/init.h>
#include <amd64/tss.h>
#include <amd64/debug.h>
#include <amd64/intr.h>
#define GDT_KCODE 0x08
#define GDT_KDATA 0x10
@@ -36,8 +36,10 @@ struct gdt_extended {
struct gdt_entry tsshigh;
} __attribute__ ((packed));
/* clang-format off */
__attribute__ ((aligned (16))) static volatile uint8_t kernel_stack[KSTACK_SIZE];
__attribute__ ((aligned (16))) static volatile struct gdt_extended gdt;
/* clang-format on */
static void amd64_gdt_set (volatile struct gdt_entry* ent, uint32_t base,
uint32_t limit, uint8_t acc, uint8_t gran) {
@@ -67,7 +69,8 @@ static void amd64_gdt_init(void) {
amd64_gdt_set (&gdt.old[2], 0, 0xFFFFF, 0x92, 0xC0);
amd64_gdt_set (&gdt.old[3], 0, 0xFFFFF, 0xFA, 0xA0);
amd64_gdt_set (&gdt.old[4], 0, 0xFFFFF, 0xF2, 0xC0);
amd64_gdt_set(&gdt.tsslow, (tssbase & 0xFFFFFFFF), tsslimit, TSS_PRESENT | TSS, 0);
amd64_gdt_set (
&gdt.tsslow, (tssbase & 0xFFFFFFFF), tsslimit, TSS_PRESENT | TSS, 0);
uint32_t tssbasehigh = (tssbase >> 32);
gdt.tsshigh.limitlow = (tssbasehigh & 0xFFFF);
@@ -82,8 +85,7 @@ static void amd64_gdt_init(void) {
gdtr.base = (uint64_t)&gdt;
__asm__ volatile ("lgdt %0" ::"m"(gdtr) : "memory");
__asm__ volatile(
"pushq %[kcode]\n"
__asm__ volatile ("pushq %[kcode]\n"
"lea 1f(%%rip), %%rax\n"
"pushq %%rax\n"
"lretq\n"
@@ -94,8 +96,7 @@ static void amd64_gdt_init(void) {
"movw %%ax, %%ss\n"
:
: [kcode] "i"(GDT_KCODE), [kdata] "i"(GDT_KDATA)
: "rax", "memory"
);
: "rax", "memory");
__asm__ volatile ("ltr %0" ::"r"((uint16_t)GDT_TSS));
}

View File

@@ -1,8 +1,8 @@
#include <amd64/intr.h>
#include <amd64/io.h>
#include <libk/std.h>
#include <libk/string.h>
#include <sys/debug.h>
#include <amd64/intr.h>
#include <amd64/io.h>
/* 8259 PIC defs. */
#define PIC1 0x20
@@ -46,14 +46,17 @@ struct idt {
uint64_t base;
} __attribute__ ((packed));
__attribute__((aligned(16))) static volatile struct idt_entry idt_entries[IDT_ENTRIES_MAX];
__attribute__ ((aligned (
16))) static volatile struct idt_entry idt_entries[IDT_ENTRIES_MAX];
static volatile struct idt idt;
extern void amd64_spin (void);
/* Remaps and disables old 8259 PIC, since we'll be using APIC. */
static void amd64_init_pic (void) {
#define IO_OP(fn, ...) fn(__VA_ARGS__); amd64_io_wait()
#define IO_OP(fn, ...) \
fn (__VA_ARGS__); \
amd64_io_wait ()
IO_OP (amd64_io_outb, PIC1_CMD, (ICW1_INIT | ICW1_ICW4));
IO_OP (amd64_io_outb, PIC2_CMD, (ICW1_INIT | ICW1_ICW4));
@@ -74,7 +77,8 @@ static void amd64_init_pic(void) {
#undef IO_OP
}
static void amd64_idt_set(volatile struct idt_entry *ent, uint64_t handler, uint8_t flags) {
static void amd64_idt_set (
volatile struct idt_entry* ent, uint64_t handler, uint8_t flags) {
ent->intrlow = (handler & 0xFFFF);
ent->kernel_cs = 0x08; // GDT_KCODE (init.c)
ent->ist = 0;
@@ -90,18 +94,54 @@ static void amd64_idt_init(void) {
#define IDT_ENTRY(n) \
extern void amd64_intr##n (void); \
amd64_idt_set (&idt_entries[(n)], (uint64_t)&amd64_intr##n, 0x8E)
IDT_ENTRY(0); IDT_ENTRY(1); IDT_ENTRY(2); IDT_ENTRY(3);
IDT_ENTRY(4); IDT_ENTRY(5); IDT_ENTRY(6); IDT_ENTRY(7);
IDT_ENTRY(8); IDT_ENTRY(9); IDT_ENTRY(10); IDT_ENTRY(11);
IDT_ENTRY(12); IDT_ENTRY(13); IDT_ENTRY(14); IDT_ENTRY(15);
IDT_ENTRY(16); IDT_ENTRY(17); IDT_ENTRY(18); IDT_ENTRY(19);
IDT_ENTRY(20); IDT_ENTRY(21); IDT_ENTRY(22); IDT_ENTRY(23);
IDT_ENTRY(24); IDT_ENTRY(25); IDT_ENTRY(26); IDT_ENTRY(27);
IDT_ENTRY(28); IDT_ENTRY(29); IDT_ENTRY(30); IDT_ENTRY(31);
IDT_ENTRY(32); IDT_ENTRY(33); IDT_ENTRY(34); IDT_ENTRY(35);
IDT_ENTRY(36); IDT_ENTRY(37); IDT_ENTRY(38); IDT_ENTRY(39);
IDT_ENTRY(40); IDT_ENTRY(41); IDT_ENTRY(42); IDT_ENTRY(43);
IDT_ENTRY(44); IDT_ENTRY(45); IDT_ENTRY(46); IDT_ENTRY(47);
IDT_ENTRY (0);
IDT_ENTRY (1);
IDT_ENTRY (2);
IDT_ENTRY (3);
IDT_ENTRY (4);
IDT_ENTRY (5);
IDT_ENTRY (6);
IDT_ENTRY (7);
IDT_ENTRY (8);
IDT_ENTRY (9);
IDT_ENTRY (10);
IDT_ENTRY (11);
IDT_ENTRY (12);
IDT_ENTRY (13);
IDT_ENTRY (14);
IDT_ENTRY (15);
IDT_ENTRY (16);
IDT_ENTRY (17);
IDT_ENTRY (18);
IDT_ENTRY (19);
IDT_ENTRY (20);
IDT_ENTRY (21);
IDT_ENTRY (22);
IDT_ENTRY (23);
IDT_ENTRY (24);
IDT_ENTRY (25);
IDT_ENTRY (26);
IDT_ENTRY (27);
IDT_ENTRY (28);
IDT_ENTRY (29);
IDT_ENTRY (30);
IDT_ENTRY (31);
IDT_ENTRY (32);
IDT_ENTRY (33);
IDT_ENTRY (34);
IDT_ENTRY (35);
IDT_ENTRY (36);
IDT_ENTRY (37);
IDT_ENTRY (38);
IDT_ENTRY (39);
IDT_ENTRY (40);
IDT_ENTRY (41);
IDT_ENTRY (42);
IDT_ENTRY (43);
IDT_ENTRY (44);
IDT_ENTRY (45);
IDT_ENTRY (46);
IDT_ENTRY (47);
#undef IDT_ENTRY
idt.limit = sizeof (idt_entries) - 1;
@@ -119,8 +159,7 @@ static void amd64_intr_exception(struct saved_regs *regs) {
uint64_t cr3;
__asm__ volatile ("movq %%cr3, %0" : "=r"(cr3));
debugprintf(
"r15=%016lx r14=%016lx r13=%016lx\n"
debugprintf ("r15=%016lx r14=%016lx r13=%016lx\n"
"r12=%016lx r11=%016lx r10=%016lx\n"
"r9 =%016lx r8 =%016lx rbp=%016lx\n"
"rdi=%016lx rsi=%016lx rdx=%016lx\n"
@@ -128,15 +167,10 @@ static void amd64_intr_exception(struct saved_regs *regs) {
"err=%016lx rip=%016lx cs =%016lx\n"
"rfl=%016lx rsp=%016lx ss =%016lx\n"
"cr2=%016lx cr3=%016lx rbx=%016lx\n",
regs->r15, regs->r14, regs->r13,
regs->r12, regs->r11, regs->r10,
regs->r9, regs->r8, regs->rbp,
regs->rdi, regs->rsi, regs->rdx,
regs->rcx, regs->rax, regs->trap,
regs->error, regs->rip, regs->cs,
regs->rflags, regs->rsp, regs->ss,
cr2, cr3, regs->rbx
);
regs->r15, regs->r14, regs->r13, regs->r12, regs->r11, regs->r10,
regs->r9, regs->r8, regs->rbp, regs->rdi, regs->rsi, regs->rdx, regs->rcx,
regs->rax, regs->trap, regs->error, regs->rip, regs->cs, regs->rflags,
regs->rsp, regs->ss, cr2, cr3, regs->rbx);
amd64_spin ();
}

View File

@@ -1,5 +1,5 @@
#include <libk/std.h>
#include <amd64/io.h>
#include <libk/std.h>
void amd64_io_outb (uint16_t port, uint8_t v) {
__asm__ volatile ("outb %1, %0" ::"dN"(port), "a"(v));
@@ -14,12 +14,10 @@ void amd64_io_outl(uint16_t port, uint32_t v) {
}
void amd64_io_outsw (uint16_t port, const void* addr, int cnt) {
__asm__ volatile(
"cld; rep outsw"
__asm__ volatile ("cld; rep outsw"
: "+S"(addr), "+c"(cnt)
: "d"(port)
: "memory", "cc"
);
: "memory", "cc");
}
uint8_t amd64_io_inb (uint16_t port) {
@@ -41,14 +39,10 @@ uint32_t amd64_io_inl(uint16_t port) {
}
void amd64_io_insw (uint16_t port, void* addr, int cnt) {
__asm__ volatile(
"cld; rep insw"
__asm__ volatile ("cld; rep insw"
: "+D"(addr), "+c"(cnt)
: "d"(port)
: "memory", "cc"
);
: "memory", "cc");
}
void amd64_io_wait(void) {
amd64_io_outb(0x80, 0);
}
void amd64_io_wait (void) { amd64_io_outb (0x80, 0); }

View File

@@ -1,5 +1,3 @@
#include <sys/spin_lock.h>
void spin_lock_relax(void) {
__asm__ volatile("pause");
}
void spin_lock_relax (void) { __asm__ volatile ("pause"); }

View File

@@ -1,8 +1,6 @@
#include <libk/std.h>
#include <amd64/tss.h>
#include <libk/std.h>
__attribute__ ((aligned (16))) static volatile struct tss tss;
volatile struct tss *amd64_get_tss(void) {
return &tss;
}
volatile struct tss* amd64_get_tss (void) { return &tss; }

View File

@@ -1,5 +1,5 @@
#include <libk/std.h>
#include <libk/bm.h>
#include <libk/std.h>
#include <libk/string.h>
void bm_init (struct bm* bm, uint8_t* base, size_t nbits) {

View File

@@ -1,3 +1,2 @@
void putchar_ (char x) { (void)x; }

View File

@@ -4,10 +4,10 @@
#include <limits.h>
#include <stdalign.h>
#include <stdarg.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdnoreturn.h>
#include <stdatomic.h>
#endif // _KERNEL_LIBK_STD_H

View File

@@ -21,12 +21,14 @@ size_t memcpy(void *dst, const void *src, size_t n) {
// SOURCE: https://stackoverflow.com/a/48967408
void strncpy (char* dst, const char* src, size_t n) {
size_t i = 0;
while(i++ != n && (*dst++ = *src++));
while (i++ != n && (*dst++ = *src++))
;
}
size_t strlen (const char* str) {
const char* s;
for (s = str; *s; ++s);
for (s = str; *s; ++s)
;
return (s - str);
}

View File

@@ -1,20 +1,20 @@
#include <limine/limine.h>
#define DECL_REQ(small, big) \
__attribute__((used, section(".limine_requests"))) \
struct limine_ ## small ## _request limine_ ## small ## _request = { \
.id = LIMINE_ ## big ## _REQUEST_ID, \
.revision = 4 \
}
__attribute__ (( \
used, section (".limine_requests"))) struct limine_##small##_request \
limine_##small##_request = { \
.id = LIMINE_##big##_REQUEST_ID, .revision = 4}
__attribute__((used, section(".limine_requests")))
volatile uint64_t limine_base_revision[] = LIMINE_BASE_REVISION(4);
__attribute__ ((used,
section (".limine_requests"))) volatile uint64_t limine_base_revision[] =
LIMINE_BASE_REVISION (4);
__attribute__((used, section(".limine_requests_start")))
volatile uint64_t limine_requests_start_marker[] = LIMINE_REQUESTS_START_MARKER;
__attribute__ ((used, section (".limine_requests_start"))) volatile uint64_t
limine_requests_start_marker[] = LIMINE_REQUESTS_START_MARKER;
__attribute__((used, section(".limine_requests_end")))
volatile uint64_t limine_requests_end_marker[] = LIMINE_REQUESTS_END_MARKER;
__attribute__ ((used, section (".limine_requests_end"))) volatile uint64_t
limine_requests_end_marker[] = LIMINE_REQUESTS_END_MARKER;
DECL_REQ (hhdm, HHDM);
DECL_REQ (memmap, MEMMAP);

View File

@@ -1,11 +1,11 @@
/* liballoc breaks when optimized too aggressively, for eg. clang's -Oz */
#pragma clang optimize off
#include <limine/requests.h>
#include <mm/liballoc.h>
#include <mm/pmm.h>
#include <mm/types.h>
#include <sync/spin_lock.h>
#include <limine/requests.h>
/* Porting */
spin_lock_t _liballoc_lock = SPIN_LOCK_INIT;
@@ -59,57 +59,49 @@ int liballoc_free(void *ptr, int pages) {
#include <stdio.h>
#endif
struct boundary_tag* l_freePages[MAXEXP]; //< Allowing for 2^MAXEXP blocks
int l_completePages[MAXEXP]; //< Allowing for 2^MAXEXP blocks
#ifdef DEBUG
unsigned int l_allocated = 0; //< The real amount of memory allocated.
unsigned int l_inuse = 0; //< The amount of memory in use (malloc'ed).
#endif
static int l_initialized = 0; //< Flag to indicate initialization.
static int l_pageSize = 4096; //< Individual page size
static int l_pageCount = 16; //< Minimum number of pages to allocate.
// *********** HELPER FUNCTIONS *******************************
/** Returns the exponent required to manage 'size' amount of memory.
*
* Returns n where 2^n <= size < 2^(n+1)
*/
static inline int getexp( unsigned int size )
{
if ( size < (1<<MINEXP) )
{
static inline int getexp (unsigned int size) {
if (size < (1 << MINEXP)) {
#ifdef DEBUG
printf ("getexp returns -1 for %i less than MINEXP\n", size);
#endif
return -1; // Smaller than the quantum.
}
int shift = MINEXP;
while ( shift < MAXEXP )
{
if ( (1<<shift) > size ) break;
while (shift < MAXEXP) {
if ((1 << shift) > size)
break;
shift += 1;
}
#ifdef DEBUG
printf("getexp returns %i (%i bytes) for %i size\n", shift - 1, (1<<(shift -1)), size );
printf ("getexp returns %i (%i bytes) for %i size\n", shift - 1,
(1 << (shift - 1)), size);
#endif
return shift - 1;
}
static void* liballoc_memset(void* s, int c, size_t n)
{
static void* liballoc_memset (void* s, int c, size_t n) {
size_t i;
for (i = 0; i < n; i++)
((char*)s)[i] = c;
@@ -117,15 +109,13 @@ static void* liballoc_memset(void* s, int c, size_t n)
return s;
}
static void* liballoc_memcpy(void* s1, const void* s2, size_t n)
{
static void* liballoc_memcpy (void* s1, const void* s2, size_t n) {
char* cdest;
char* csrc;
unsigned int* ldest = (unsigned int*)s1;
unsigned int* lsrc = (unsigned int*)s2;
while ( n >= sizeof(unsigned int) )
{
while (n >= sizeof (unsigned int)) {
*ldest++ = *lsrc++;
n -= sizeof (unsigned int);
}
@@ -133,8 +123,7 @@ static void* liballoc_memcpy(void* s1, const void* s2, size_t n)
cdest = (char*)ldest;
csrc = (char*)lsrc;
while ( n > 0 )
{
while (n > 0) {
*cdest++ = *csrc++;
n -= 1;
}
@@ -142,11 +131,8 @@ static void* liballoc_memcpy(void* s1, const void* s2, size_t n)
return s1;
}
#ifdef DEBUG
static void dump_array()
{
static void dump_array () {
int i = 0;
struct boundary_tag* tag = NULL;
@@ -154,16 +140,16 @@ static void dump_array()
printf ("System memory allocated: %i\n", l_allocated);
printf ("Memory in used (malloc'ed): %i\n", l_inuse);
for ( i = 0; i < MAXEXP; i++ )
{
for (i = 0; i < MAXEXP; i++) {
printf ("%.2i(%i): ", i, l_completePages[i]);
tag = l_freePages[i];
while ( tag != NULL )
{
if ( tag->split_left != NULL ) printf("*");
while (tag != NULL) {
if (tag->split_left != NULL)
printf ("*");
printf ("%i", tag->real_size);
if ( tag->split_right != NULL ) printf("*");
if (tag->split_right != NULL)
printf ("*");
printf (" ");
tag = tag->next;
@@ -176,24 +162,19 @@ static void dump_array()
}
#endif
static inline void insert_tag( struct boundary_tag *tag, int index )
{
static inline void insert_tag (struct boundary_tag* tag, int index) {
int realIndex;
if ( index < 0 )
{
if (index < 0) {
realIndex = getexp (tag->real_size - sizeof (struct boundary_tag));
if ( realIndex < MINEXP ) realIndex = MINEXP;
}
else
if (realIndex < MINEXP)
realIndex = MINEXP;
} else
realIndex = index;
tag->index = realIndex;
if ( l_freePages[ realIndex ] != NULL )
{
if (l_freePages[realIndex] != NULL) {
l_freePages[realIndex]->prev = tag;
tag->next = l_freePages[realIndex];
}
@@ -201,34 +182,33 @@ static inline void insert_tag( struct boundary_tag *tag, int index )
l_freePages[realIndex] = tag;
}
static inline void remove_tag( struct boundary_tag *tag )
{
if ( l_freePages[ tag->index ] == tag ) l_freePages[ tag->index ] = tag->next;
static inline void remove_tag (struct boundary_tag* tag) {
if (l_freePages[tag->index] == tag)
l_freePages[tag->index] = tag->next;
if ( tag->prev != NULL ) tag->prev->next = tag->next;
if ( tag->next != NULL ) tag->next->prev = tag->prev;
if (tag->prev != NULL)
tag->prev->next = tag->next;
if (tag->next != NULL)
tag->next->prev = tag->prev;
tag->next = NULL;
tag->prev = NULL;
tag->index = -1;
}
static inline struct boundary_tag* melt_left( struct boundary_tag *tag )
{
static inline struct boundary_tag* melt_left (struct boundary_tag* tag) {
struct boundary_tag* left = tag->split_left;
left->real_size += tag->real_size;
left->split_right = tag->split_right;
if ( tag->split_right != NULL ) tag->split_right->split_left = left;
if (tag->split_right != NULL)
tag->split_right->split_left = left;
return left;
}
static inline struct boundary_tag* absorb_right( struct boundary_tag *tag )
{
static inline struct boundary_tag* absorb_right (struct boundary_tag* tag) {
struct boundary_tag* right = tag->split_right;
remove_tag (right); // Remove right from free pages.
@@ -242,12 +222,12 @@ static inline struct boundary_tag* absorb_right( struct boundary_tag *tag )
return tag;
}
static inline struct boundary_tag* split_tag( struct boundary_tag* tag )
{
unsigned int remainder = tag->real_size - sizeof(struct boundary_tag) - tag->size;
static inline struct boundary_tag* split_tag (struct boundary_tag* tag) {
unsigned int remainder =
tag->real_size - sizeof (struct boundary_tag) - tag->size;
struct boundary_tag *new_tag =
(struct boundary_tag*)((uintptr_t)tag + sizeof(struct boundary_tag) + tag->size);
struct boundary_tag* new_tag = (struct boundary_tag*)((uintptr_t)tag +
sizeof (struct boundary_tag) + tag->size);
new_tag->magic = LIBALLOC_MAGIC;
new_tag->real_size = remainder;
@@ -258,7 +238,8 @@ static inline struct boundary_tag* split_tag( struct boundary_tag* tag )
new_tag->split_left = tag;
new_tag->split_right = tag->split_right;
if (new_tag->split_right != NULL) new_tag->split_right->split_left = new_tag;
if (new_tag->split_right != NULL)
new_tag->split_right->split_left = new_tag;
tag->split_right = new_tag;
tag->real_size -= new_tag->real_size;
@@ -268,14 +249,9 @@ static inline struct boundary_tag* split_tag( struct boundary_tag* tag )
return new_tag;
}
// ***************************************************************
static struct boundary_tag* allocate_new_tag( unsigned int size )
{
static struct boundary_tag* allocate_new_tag (unsigned int size) {
unsigned int pages;
unsigned int usage;
struct boundary_tag* tag;
@@ -285,14 +261,17 @@ static struct boundary_tag* allocate_new_tag( unsigned int size )
// Perfect amount of space
pages = usage / l_pageSize;
if ( (usage % l_pageSize) != 0 ) pages += 1;
if ((usage % l_pageSize) != 0)
pages += 1;
// Make sure it's >= the minimum size.
if ( pages < (unsigned int)l_pageCount ) pages = l_pageCount;
if (pages < (unsigned int)l_pageCount)
pages = l_pageCount;
tag = (struct boundary_tag*)liballoc_alloc (pages);
if ( tag == NULL ) return NULL; // uh oh, we ran out of memory.
if (tag == NULL)
return NULL; // uh oh, we ran out of memory.
tag->magic = LIBALLOC_MAGIC;
tag->size = size;
@@ -304,9 +283,9 @@ static struct boundary_tag* allocate_new_tag( unsigned int size )
tag->split_left = NULL;
tag->split_right = NULL;
#ifdef DEBUG
printf("Resource allocated %x of %i pages (%i bytes) for %i size.\n", tag, pages, pages * l_pageSize, size );
printf ("Resource allocated %x of %i pages (%i bytes) for %i size.\n", tag,
pages, pages * l_pageSize, size);
l_allocated += pages * l_pageSize;
@@ -316,23 +295,18 @@ static struct boundary_tag* allocate_new_tag( unsigned int size )
return tag;
}
void *malloc(size_t size)
{
void* malloc (size_t size) {
int index;
void* ptr;
struct boundary_tag* tag = NULL;
liballoc_lock ();
if ( l_initialized == 0 )
{
if (l_initialized == 0) {
#ifdef DEBUG
printf ("%s\n", "liballoc initializing.");
#endif
for ( index = 0; index < MAXEXP; index++ )
{
for (index = 0; index < MAXEXP; index++) {
l_freePages[index] = NULL;
l_completePages[index] = 0;
}
@@ -340,19 +314,19 @@ void *malloc(size_t size)
}
index = getexp (size) + MODE;
if ( index < MINEXP ) index = MINEXP;
if (index < MINEXP)
index = MINEXP;
// Find one big enough.
tag = l_freePages[index]; // Start at the front of the list.
while ( tag != NULL )
{
while (tag != NULL) {
// If there's enough space in this tag.
if ( (tag->real_size - sizeof(struct boundary_tag))
>= (size + sizeof(struct boundary_tag) ) )
{
if ((tag->real_size - sizeof (struct boundary_tag)) >=
(size + sizeof (struct boundary_tag))) {
#ifdef DEBUG
printf("Tag search found %i >= %i\n",(tag->real_size - sizeof(struct boundary_tag)), (size + sizeof(struct boundary_tag) ) );
printf ("Tag search found %i >= %i\n",
(tag->real_size - sizeof (struct boundary_tag)),
(size + sizeof (struct boundary_tag)));
#endif
break;
}
@@ -360,20 +334,15 @@ void *malloc(size_t size)
tag = tag->next;
}
// No page found. Make one.
if ( tag == NULL )
{
if ( (tag = allocate_new_tag( size )) == NULL )
{
if (tag == NULL) {
if ((tag = allocate_new_tag (size)) == NULL) {
liballoc_unlock ();
return NULL;
}
index = getexp (tag->real_size - sizeof (struct boundary_tag));
}
else
{
} else {
remove_tag (tag);
if ((tag->split_left == NULL) && (tag->split_right == NULL))
@@ -387,19 +356,23 @@ void *malloc(size_t size)
// Removed... see if we can re-use the excess space.
#ifdef DEBUG
printf("Found tag with %i bytes available (requested %i bytes, leaving %i), which has exponent: %i (%i bytes)\n", tag->real_size - sizeof(struct boundary_tag), size, tag->real_size - size - sizeof(struct boundary_tag), index, 1<<index );
printf (
"Found tag with %i bytes available (requested %i bytes, leaving %i), which has exponent: %i (%i bytes)\n",
tag->real_size - sizeof (struct boundary_tag), size,
tag->real_size - size - sizeof (struct boundary_tag), index, 1 << index);
#endif
unsigned int remainder = tag->real_size - size - sizeof( struct boundary_tag ) * 2; // Support a new tag + remainder
unsigned int remainder = tag->real_size - size -
sizeof (struct boundary_tag) * 2; // Support a new tag + remainder
if ( ((int)(remainder) > 0) /*&& ( (tag->real_size - remainder) >= (1<<MINEXP))*/ )
{
if (((int)(remainder) >
0) /*&& ( (tag->real_size - remainder) >= (1<<MINEXP))*/) {
int childIndex = getexp (remainder);
if ( childIndex >= 0 )
{
if (childIndex >= 0) {
#ifdef DEBUG
printf("Seems to be splittable: %i >= 2^%i .. %i\n", remainder, childIndex, (1<<childIndex) );
printf ("Seems to be splittable: %i >= 2^%i .. %i\n", remainder,
childIndex, (1 << childIndex));
#endif
struct boundary_tag* new_tag = split_tag (tag);
@@ -407,93 +380,85 @@ void *malloc(size_t size)
(void)new_tag;
#ifdef DEBUG
printf("Old tag has become %i bytes, new tag is now %i bytes (%i exp)\n", tag->real_size, new_tag->real_size, new_tag->index );
printf ("Old tag has become %i bytes, new tag is now %i bytes (%i exp)\n",
tag->real_size, new_tag->real_size, new_tag->index);
#endif
}
}
ptr = (void*)((uintptr_t)tag + sizeof (struct boundary_tag));
#ifdef DEBUG
l_inuse += size;
printf("malloc: %x, %i, %i\n", ptr, (int)l_inuse / 1024, (int)l_allocated / 1024 );
printf ("malloc: %x, %i, %i\n", ptr, (int)l_inuse / 1024,
(int)l_allocated / 1024);
dump_array ();
#endif
liballoc_unlock ();
return ptr;
}
void free(void *ptr)
{
void free (void* ptr) {
int index;
struct boundary_tag* tag;
if ( ptr == NULL ) return;
if (ptr == NULL)
return;
liballoc_lock ();
tag = (struct boundary_tag*)((uintptr_t)ptr - sizeof (struct boundary_tag));
if ( tag->magic != LIBALLOC_MAGIC )
{
if (tag->magic != LIBALLOC_MAGIC) {
liballoc_unlock (); // release the lock
return;
}
#ifdef DEBUG
l_inuse -= tag->size;
printf("free: %x, %i, %i\n", ptr, (int)l_inuse / 1024, (int)l_allocated / 1024 );
printf (
"free: %x, %i, %i\n", ptr, (int)l_inuse / 1024, (int)l_allocated / 1024);
#endif
// MELT LEFT...
while ( (tag->split_left != NULL) && (tag->split_left->index >= 0) )
{
while ((tag->split_left != NULL) && (tag->split_left->index >= 0)) {
#ifdef DEBUG
printf("Melting tag left into available memory. Left was %i, becomes %i (%i)\n", tag->split_left->real_size, tag->split_left->real_size + tag->real_size, tag->split_left->real_size );
printf (
"Melting tag left into available memory. Left was %i, becomes %i (%i)\n",
tag->split_left->real_size, tag->split_left->real_size + tag->real_size,
tag->split_left->real_size);
#endif
tag = melt_left (tag);
remove_tag (tag);
}
// MELT RIGHT...
while ( (tag->split_right != NULL) && (tag->split_right->index >= 0) )
{
while ((tag->split_right != NULL) && (tag->split_right->index >= 0)) {
#ifdef DEBUG
printf("Melting tag right into available memory. This was was %i, becomes %i (%i)\n", tag->real_size, tag->split_right->real_size + tag->real_size, tag->split_right->real_size );
printf (
"Melting tag right into available memory. This was was %i, becomes %i (%i)\n",
tag->real_size, tag->split_right->real_size + tag->real_size,
tag->split_right->real_size);
#endif
tag = absorb_right (tag);
}
// Where is it going back to?
index = getexp (tag->real_size - sizeof (struct boundary_tag));
if ( index < MINEXP ) index = MINEXP;
if (index < MINEXP)
index = MINEXP;
// A whole, empty block?
if ( (tag->split_left == NULL) && (tag->split_right == NULL) )
{
if ( l_completePages[ index ] == MAXCOMPLETE )
{
if ((tag->split_left == NULL) && (tag->split_right == NULL)) {
if (l_completePages[index] == MAXCOMPLETE) {
// Too many standing by to keep. Free this one.
unsigned int pages = tag->real_size / l_pageSize;
if ( (tag->real_size % l_pageSize) != 0 ) pages += 1;
if ( pages < (unsigned int)l_pageCount ) pages = l_pageCount;
if ((tag->real_size % l_pageSize) != 0)
pages += 1;
if (pages < (unsigned int)l_pageCount)
pages = l_pageCount;
liballoc_free (tag, pages);
@@ -507,29 +472,24 @@ void free(void *ptr)
return;
}
l_completePages[index] += 1; // Increase the count of complete pages.
}
// ..........
insert_tag (tag, index);
#ifdef DEBUG
printf("Returning tag with %i bytes (requested %i bytes), which has exponent: %i\n", tag->real_size, tag->size, index );
printf (
"Returning tag with %i bytes (requested %i bytes), which has exponent: %i\n",
tag->real_size, tag->size, index);
dump_array ();
#endif
liballoc_unlock ();
}
void* calloc(size_t nobj, size_t size)
{
void* calloc (size_t nobj, size_t size) {
int real_size;
void* p;
@@ -542,27 +502,27 @@ void* calloc(size_t nobj, size_t size)
return p;
}
void* realloc(void *p, size_t size)
{
void* realloc (void* p, size_t size) {
void* ptr;
struct boundary_tag* tag;
int real_size;
if ( size == 0 )
{
if (size == 0) {
free (p);
return NULL;
}
if ( p == NULL ) return malloc( size );
if (p == NULL)
return malloc (size);
if ( &liballoc_lock != NULL ) liballoc_lock(); // lockit
if (&liballoc_lock != NULL)
liballoc_lock (); // lockit
tag = (struct boundary_tag*)((uintptr_t)p - sizeof (struct boundary_tag));
real_size = tag->size;
if ( &liballoc_unlock != NULL ) liballoc_unlock();
if (&liballoc_unlock != NULL)
liballoc_unlock ();
if ( (size_t)real_size > size ) real_size = size;
if ((size_t)real_size > size)
real_size = size;
ptr = malloc (size);
liballoc_memcpy (ptr, p, real_size);
@@ -570,6 +530,3 @@ void* realloc(void *p, size_t size)
return ptr;
}

View File

@@ -12,7 +12,6 @@
typedef unsigned int size_t;
#endif
#ifndef NULL
#define NULL 0
#endif
@@ -23,14 +22,12 @@ typedef unsigned int size_t;
extern "C" {
#endif
/** This is a boundary tag which is prepended to the
* page or section of a page which we have allocated. It is
* used to identify valid memory blocks that the
* application is trying to free.
*/
struct boundary_tag
{
struct boundary_tag {
unsigned int magic; //< It's a kind of ...
unsigned int size; //< Requested size.
unsigned int real_size; //< Actual size.
@@ -43,9 +40,6 @@ struct boundary_tag
struct boundary_tag* prev; //< Linked list info.
};
/** This function is supposed to lock the memory data structures. It
* could be as simple as disabling interrupts or acquiring a spinlock.
* It's up to you to decide.
@@ -82,18 +76,13 @@ extern void* liballoc_alloc(int);
*/
extern int liballoc_free (void*, int);
void* malloc (size_t); //< The standard function.
void* realloc (void*, size_t); //< The standard function.
void* calloc (size_t, size_t); //< The standard function.
void free (void*); //< The standard function.
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -1,14 +1,14 @@
#include <libk/std.h>
#include <libk/bm.h>
#include <libk/string.h>
#include <libk/align.h>
#include <sys/mm.h>
#include <sys/debug.h>
#include <sync/spin_lock.h>
#include <mm/types.h>
#include <mm/pmm.h>
#include <libk/bm.h>
#include <libk/std.h>
#include <libk/string.h>
#include <limine/limine.h>
#include <limine/requests.h>
#include <mm/pmm.h>
#include <mm/types.h>
#include <sync/spin_lock.h>
#include <sys/debug.h>
#include <sys/mm.h>
static struct pmm pmm;
@@ -21,13 +21,12 @@ void pmm_init(void) {
size_t region = 0;
for (size_t i = 0; i < memmap->entry_count; i++) {
struct limine_memmap_entry* entry = memmap->entries[i];
static const char *entry_strings[] = {
"usable", "reserved", "acpi reclaimable", "acpi nvs",
"bad memory", "bootloader reclaimable", "executable and modules",
"framebuffer", "acpi tables"
};
static const char* entry_strings[] = {"usable", "reserved",
"acpi reclaimable", "acpi nvs", "bad memory", "bootloader reclaimable",
"executable and modules", "framebuffer", "acpi tables"};
DEBUG("memmap entry: %-25s %p (%zu bytes)\n", entry_strings[entry->type], entry->base, entry->length);
DEBUG ("memmap entry: %-25s %p (%zu bytes)\n", entry_strings[entry->type],
entry->base, entry->length);
if (entry->type == LIMINE_MEMMAP_USABLE && region < PMM_REGIONS_MAX) {
struct pmm_region* pmm_region = &pmm.regions[region];
@@ -82,7 +81,8 @@ void pmm_init(void) {
* Find free space for a block range. For every bit of the bitmap, we test nblks bits forward.
* bm_test_region helps us out, because it automatically does range checks. See comments there.
*/
static size_t pmm_find_free_space(struct pmm_region *pmm_region, size_t nblks) {
static size_t pmm_find_free_space (
struct pmm_region* pmm_region, size_t nblks) {
for (size_t bit = 0; bit < pmm_region->bm.nbits; bit++) {
if (bm_test_region (&pmm_region->bm, bit, nblks)) {
continue;
@@ -134,7 +134,8 @@ void pmm_free(physaddr_t p_addr, size_t nblks) {
continue;
/* If aligned_p_addr is within the range if this region, it belongs to it. */
if (aligned_p_addr >= pmm_region->membase && aligned_p_addr < pmm_region->size) {
if (aligned_p_addr >= pmm_region->membase &&
aligned_p_addr < pmm_region->size) {
physaddr_t addr = aligned_p_addr - pmm_region->membase;
size_t bit = div_align_up (addr, PAGE_SIZE);

View File

@@ -1,10 +1,10 @@
#ifndef _KERNEL_MM_PMM_H
#define _KERNEL_MM_PMM_H
#include <libk/std.h>
#include <libk/bm.h>
#include <sync/spin_lock.h>
#include <libk/std.h>
#include <mm/types.h>
#include <sync/spin_lock.h>
#define PMM_ALLOC_ERR ((physaddr_t) - 1)

View File

@@ -1,6 +1,6 @@
#include <libk/std.h>
#include <sys/spin_lock.h>
#include <sync/spin_lock.h>
#include <sys/spin_lock.h>
void spin_lock (spin_lock_t* sl) {
while (atomic_flag_test_and_set_explicit (sl, memory_order_acquire))

View File

@@ -3,7 +3,8 @@
void debugprintf (const char* fmt, ...);
#define DEBUG(fmt, ...) do { \
#define DEBUG(fmt, ...) \
do { \
debugprintf ("%s: " fmt, __func__, ##__VA_ARGS__); \
} while (0)

View File

@@ -1,7 +1,7 @@
#include <libk/std.h>
#include <sys/debug.h>
#include <mm/liballoc.h>
#include <limine/requests.h>
#include <mm/liballoc.h>
#include <sys/debug.h>
#include <uacpi/kernel_api.h>
#include <uacpi/status.h>
@@ -9,7 +9,8 @@ uacpi_status uacpi_kernel_get_rsdp(uacpi_phys_addr *out_rsdp_address) {
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
struct limine_rsdp_response* rsdp = limine_rsdp_request.response;
*out_rsdp_address = (uacpi_phys_addr)((uintptr_t)rsdp->address - (uintptr_t)hhdm->offset);
*out_rsdp_address =
(uacpi_phys_addr)((uintptr_t)rsdp->address - (uintptr_t)hhdm->offset);
return UACPI_STATUS_OK;
}
@@ -20,9 +21,7 @@ void *uacpi_kernel_map(uacpi_phys_addr addr, uacpi_size len) {
return (void*)((uintptr_t)hhdm->offset + (uintptr_t)addr);
}
void uacpi_kernel_unmap(void *addr, uacpi_size len) {
(void)addr, (void)len;
}
void uacpi_kernel_unmap (void* addr, uacpi_size len) { (void)addr, (void)len; }
void uacpi_kernel_log (uacpi_log_level level, const uacpi_char* msg) {
const char* prefix = NULL;