This commit is contained in:
2026-04-20 23:09:38 +08:00
parent dbd88505e6
commit 16855e4b98
3 changed files with 121 additions and 20 deletions

3
.gitmodules vendored
View File

@@ -1,3 +1,6 @@
[submodule "limine"] [submodule "limine"]
path = limine path = limine
url = https://github.com/limine-bootloader/limine.git url = https://github.com/limine-bootloader/limine.git
[submodule "cleonos/third-party/doomgeneric"]
path = cleonos/third-party/doomgeneric
url = https://github.com/ozkl/doomgeneric.git

View File

@@ -21,6 +21,9 @@
struct dg_alloc_hdr { struct dg_alloc_hdr {
size_t size; size_t size;
int free;
struct dg_alloc_hdr *next;
struct dg_alloc_hdr *prev;
}; };
struct dg_mem_fd { struct dg_mem_fd {
@@ -42,7 +45,8 @@ struct dg_stream {
}; };
static unsigned char g_dg_heap[DG_HEAP_SIZE]; static unsigned char g_dg_heap[DG_HEAP_SIZE];
static size_t g_dg_heap_pos = 0U; static struct dg_alloc_hdr *g_dg_heap_head = (struct dg_alloc_hdr *)0;
static int g_dg_heap_ready = 0;
static struct dg_mem_fd g_dg_fds[DG_MAX_MEM_FD]; static struct dg_mem_fd g_dg_fds[DG_MAX_MEM_FD];
static struct dg_stream g_dg_stdin_stream = {DG_STDIO_MAGIC, 0, 0, 0}; static struct dg_stream g_dg_stdin_stream = {DG_STDIO_MAGIC, 0, 0, 0};
static struct dg_stream g_dg_stdout_stream = {DG_STDIO_MAGIC, 1, 0, 0}; static struct dg_stream g_dg_stdout_stream = {DG_STDIO_MAGIC, 1, 0, 0};
@@ -382,30 +386,87 @@ static int dg_fd_flush_slot(struct dg_mem_fd *file) {
} }
void *malloc(size_t size) { void *malloc(size_t size) {
struct dg_alloc_hdr *hdr; struct dg_alloc_hdr *cur;
size_t aligned = (size + 15U) & ~(size_t)15U; size_t aligned;
size_t need = aligned + sizeof(struct dg_alloc_hdr);
if (size == 0U) { if (size == 0U) {
size = 1U; size = 1U;
aligned = (size + 15U) & ~(size_t)15U;
need = aligned + sizeof(struct dg_alloc_hdr);
} }
if (need > DG_HEAP_SIZE || g_dg_heap_pos > DG_HEAP_SIZE - need) { aligned = (size + 15U) & ~(size_t)15U;
errno = ENOMEM;
return (void *)0; if (g_dg_heap_ready == 0) {
if (DG_HEAP_SIZE <= sizeof(struct dg_alloc_hdr) + 16U) {
errno = ENOMEM;
return (void *)0;
}
g_dg_heap_head = (struct dg_alloc_hdr *)(void *)g_dg_heap;
g_dg_heap_head->size = DG_HEAP_SIZE - sizeof(struct dg_alloc_hdr);
g_dg_heap_head->free = 1;
g_dg_heap_head->next = (struct dg_alloc_hdr *)0;
g_dg_heap_head->prev = (struct dg_alloc_hdr *)0;
g_dg_heap_ready = 1;
} }
hdr = (struct dg_alloc_hdr *)(void *)(g_dg_heap + g_dg_heap_pos); cur = g_dg_heap_head;
hdr->size = aligned;
g_dg_heap_pos += need;
return (void *)(hdr + 1); while (cur != (struct dg_alloc_hdr *)0) {
if (cur->free != 0 && cur->size >= aligned) {
size_t remain = cur->size - aligned;
if (remain > sizeof(struct dg_alloc_hdr) + 16U) {
struct dg_alloc_hdr *tail =
(struct dg_alloc_hdr *)(void *)((unsigned char *)(void *)(cur + 1) + aligned);
tail->size = remain - sizeof(struct dg_alloc_hdr);
tail->free = 1;
tail->next = cur->next;
tail->prev = cur;
if (cur->next != (struct dg_alloc_hdr *)0) {
cur->next->prev = tail;
}
cur->next = tail;
cur->size = aligned;
}
cur->free = 0;
return (void *)(cur + 1);
}
cur = cur->next;
}
errno = ENOMEM;
return (void *)0;
} }
void free(void *ptr) { void free(void *ptr) {
(void)ptr; struct dg_alloc_hdr *hdr;
if (ptr == (void *)0) {
return;
}
hdr = ((struct dg_alloc_hdr *)ptr) - 1;
hdr->free = 1;
if (hdr->next != (struct dg_alloc_hdr *)0 && hdr->next->free != 0) {
struct dg_alloc_hdr *next = hdr->next;
hdr->size += sizeof(struct dg_alloc_hdr) + next->size;
hdr->next = next->next;
if (next->next != (struct dg_alloc_hdr *)0) {
next->next->prev = hdr;
}
}
if (hdr->prev != (struct dg_alloc_hdr *)0 && hdr->prev->free != 0) {
struct dg_alloc_hdr *prev = hdr->prev;
prev->size += sizeof(struct dg_alloc_hdr) + hdr->size;
prev->next = hdr->next;
if (hdr->next != (struct dg_alloc_hdr *)0) {
hdr->next->prev = prev;
}
}
} }
void *calloc(size_t nmemb, size_t size) { void *calloc(size_t nmemb, size_t size) {
@@ -446,12 +507,28 @@ void *realloc(void *ptr, size_t size) {
old_hdr = ((struct dg_alloc_hdr *)ptr) - 1; old_hdr = ((struct dg_alloc_hdr *)ptr) - 1;
old_size = old_hdr->size; old_size = old_hdr->size;
if (old_size >= size) {
return ptr;
}
if (old_hdr->next != (struct dg_alloc_hdr *)0 && old_hdr->next->free != 0 &&
old_size + sizeof(struct dg_alloc_hdr) + old_hdr->next->size >= size) {
struct dg_alloc_hdr *next = old_hdr->next;
old_hdr->size += sizeof(struct dg_alloc_hdr) + next->size;
old_hdr->next = next->next;
if (old_hdr->next != (struct dg_alloc_hdr *)0) {
old_hdr->next->prev = old_hdr;
}
return ptr;
}
new_ptr = malloc(size); new_ptr = malloc(size);
if (new_ptr == (void *)0) { if (new_ptr == (void *)0) {
return (void *)0; return (void *)0;
} }
memcpy(new_ptr, ptr, (old_size < size) ? old_size : size); memcpy(new_ptr, ptr, (old_size < size) ? old_size : size);
free(ptr);
return new_ptr; return new_ptr;
} }
@@ -656,6 +733,7 @@ int rename(const char *old_path, const char *new_path) {
u64 file_size; u64 file_size;
char *buf; char *buf;
u64 read_len; u64 read_len;
int rc = -1;
if (old_path == (const char *)0 || new_path == (const char *)0) { if (old_path == (const char *)0 || new_path == (const char *)0) {
errno = EINVAL; errno = EINVAL;
@@ -684,20 +762,23 @@ int rename(const char *old_path, const char *new_path) {
read_len = cleonos_sys_fs_read(src, buf, file_size); read_len = cleonos_sys_fs_read(src, buf, file_size);
if (read_len != file_size) { if (read_len != file_size) {
errno = EIO; errno = EIO;
return -1; goto done;
} }
if (cleonos_sys_fs_write(dst, buf, read_len) != read_len) { if (cleonos_sys_fs_write(dst, buf, read_len) != read_len) {
errno = EIO; errno = EIO;
return -1; goto done;
} }
if (cleonos_sys_fs_remove(src) == 0ULL) { if (cleonos_sys_fs_remove(src) == 0ULL) {
errno = EIO; errno = EIO;
return -1; goto done;
} }
return 0; rc = 0;
done:
free(buf);
return rc;
} }
int open(const char *path, int flags, ...) { int open(const char *path, int flags, ...) {
@@ -739,12 +820,18 @@ int open(const char *path, int flags, ...) {
} }
} else if (size > 0ULL) { } else if (size > 0ULL) {
if (dg_fd_ensure_cap(file, (size_t)size) != 0) { if (dg_fd_ensure_cap(file, (size_t)size) != 0) {
free(file->data);
file->used = 0; file->used = 0;
file->data = (unsigned char *)0;
file->cap = 0U;
return -1; return -1;
} }
if (cleonos_sys_fs_read(resolved, (char *)file->data, size) != size) { if (cleonos_sys_fs_read(resolved, (char *)file->data, size) != size) {
free(file->data);
file->used = 0; file->used = 0;
file->data = (unsigned char *)0;
file->cap = 0U;
errno = EIO; errno = EIO;
return -1; return -1;
} }
@@ -783,6 +870,7 @@ int close(int fd) {
return -1; return -1;
} }
free(file->data);
file->used = 0; file->used = 0;
file->writable = 0; file->writable = 0;
file->dirty = 0; file->dirty = 0;

View File

@@ -13,7 +13,7 @@
typedef u64 (*clks_exec_entry_fn)(void); typedef u64 (*clks_exec_entry_fn)(void);
#define CLKS_EXEC_RUN_STACK_BYTES (64ULL * 1024ULL) #define CLKS_EXEC_RUN_STACK_BYTES (1024ULL * 1024ULL)
#define CLKS_EXEC_MAX_PROCS 64U #define CLKS_EXEC_MAX_PROCS 64U
#define CLKS_EXEC_MAX_DEPTH 16U #define CLKS_EXEC_MAX_DEPTH 16U
#define CLKS_EXEC_PATH_MAX 192U #define CLKS_EXEC_PATH_MAX 192U
@@ -160,6 +160,11 @@ struct clks_exec_dynlib_slot {
#if defined(CLKS_ARCH_X86_64) #if defined(CLKS_ARCH_X86_64)
extern u64 clks_exec_call_on_stack_x86_64(void *entry_ptr, void *stack_top); extern u64 clks_exec_call_on_stack_x86_64(void *entry_ptr, void *stack_top);
extern void clks_exec_abort_to_caller_x86_64(void); extern void clks_exec_abort_to_caller_x86_64(void);
static u64 clks_exec_read_cr2(void) {
u64 value = 0ULL;
__asm__ volatile("mov %%cr2, %0" : "=r"(value));
return value;
}
#endif #endif
static u64 clks_exec_requests = 0ULL; static u64 clks_exec_requests = 0ULL;
@@ -2471,6 +2476,11 @@ clks_bool clks_exec_handle_exception(u64 vector, u64 error_code, u64 rip, u64 *i
clks_exec_log_hex_serial("VECTOR", vector); clks_exec_log_hex_serial("VECTOR", vector);
clks_exec_log_hex_serial("ERROR", error_code); clks_exec_log_hex_serial("ERROR", error_code);
clks_exec_log_hex_serial("RIP", rip); clks_exec_log_hex_serial("RIP", rip);
#if defined(CLKS_ARCH_X86_64)
if (vector == 14ULL) {
clks_exec_log_hex_serial("CR2", clks_exec_read_cr2());
}
#endif
#if defined(CLKS_ARCH_X86_64) #if defined(CLKS_ARCH_X86_64)
if (io_rip == CLKS_NULL || io_rdi == CLKS_NULL || io_rsi == CLKS_NULL) { if (io_rip == CLKS_NULL || io_rdi == CLKS_NULL || io_rsi == CLKS_NULL) {