From 5d73ba1059771f3226c1f45d41a9e3f52d68ee3d Mon Sep 17 00:00:00 2001 From: Jon Santmyer Date: Mon, 15 Sep 2025 15:51:13 -0400 Subject: begin init staging refactor --- apps/init/stage1/initrd.h | 5 +++++ apps/init/stage1/kprint.c | 15 +++++++++++++++ apps/init/stage1/kprint.h | 7 +++++++ apps/init/stage1/main.c | 24 ++++++++++++++++++++++++ apps/init/stage1/memory.h | 6 ++++++ apps/init/stage1/stage1.h | 12 ++++++++++++ apps/init/stage1/x86_64/syscall.c | 19 +++++++++++++++++++ 7 files changed, 88 insertions(+) create mode 100644 apps/init/stage1/initrd.h create mode 100644 apps/init/stage1/kprint.c create mode 100644 apps/init/stage1/kprint.h create mode 100644 apps/init/stage1/main.c create mode 100644 apps/init/stage1/memory.h create mode 100644 apps/init/stage1/stage1.h create mode 100644 apps/init/stage1/x86_64/syscall.c (limited to 'apps/init/stage1') diff --git a/apps/init/stage1/initrd.h b/apps/init/stage1/initrd.h new file mode 100644 index 0000000..b4bec79 --- /dev/null +++ b/apps/init/stage1/initrd.h @@ -0,0 +1,5 @@ +#ifndef INIT_STAGE1_INITRD_H +#define INIT_STAGE1_INITRD_H 1 + + +#endif diff --git a/apps/init/stage1/kprint.c b/apps/init/stage1/kprint.c new file mode 100644 index 0000000..57e10f5 --- /dev/null +++ b/apps/init/stage1/kprint.c @@ -0,0 +1,15 @@ +#include "stage1.h" +#include + +void +kputc(char c) +{ + _syscall_payload[0] = c; + syscall_invoke_noret(SYSCALL_DEBUG_PUTC); +} + +void +kputs(const char *s) +{ + for(; *s; s++) kputc(*s); +} diff --git a/apps/init/stage1/kprint.h b/apps/init/stage1/kprint.h new file mode 100644 index 0000000..ea71012 --- /dev/null +++ b/apps/init/stage1/kprint.h @@ -0,0 +1,7 @@ +#ifndef INIT_STAGE1_KPRINT_H +#define INIT_STAGE1_KPRINT_H 1 + +void kputc(char c); +void kputs(const char *s); + +#endif diff --git a/apps/init/stage1/main.c b/apps/init/stage1/main.c new file mode 100644 index 0000000..90e663d --- /dev/null +++ b/apps/init/stage1/main.c @@ -0,0 +1,24 @@ +#include "stage1.h" +#include "kprint.h" +#include + +uint8_t *_syscall_payload; +uint8_t _syscall_message; + +void main(void *init_message_ptr) +{ + _syscall_payload = init_message_ptr; + _syscall_message = INIT_OBJECT_MESSAGE; + + kputs("Hello, Userland!\n"); + for(;;); +} + +__attribute__((naked)) +void +_start(void) +{ + __asm__ volatile("\ + popq %rdi; \ + jmp main"); +} diff --git a/apps/init/stage1/memory.h b/apps/init/stage1/memory.h new file mode 100644 index 0000000..72db624 --- /dev/null +++ b/apps/init/stage1/memory.h @@ -0,0 +1,6 @@ +#ifndef INIT_STAGE1_MEMORY_H +#define INIT_STAGE1_MEMORY_H 1 + + + +#endif diff --git a/apps/init/stage1/stage1.h b/apps/init/stage1/stage1.h new file mode 100644 index 0000000..d6c3e90 --- /dev/null +++ b/apps/init/stage1/stage1.h @@ -0,0 +1,12 @@ +#ifndef INIT_STAGE1_H +#define INIT_STAGE1_H 1 + +#include + +extern uint8_t *_syscall_payload; +extern uint8_t _syscall_message; + +void syscall_invoke_noret(uint64_t calltype); +intmax_t syscall_invoke_ret(uint64_t calltype); + +#endif diff --git a/apps/init/stage1/x86_64/syscall.c b/apps/init/stage1/x86_64/syscall.c new file mode 100644 index 0000000..d63be52 --- /dev/null +++ b/apps/init/stage1/x86_64/syscall.c @@ -0,0 +1,19 @@ +#include "stage1/stage1.h" + +void +syscall_invoke_noret(uint64_t calltype) +{ + __asm__ volatile("syscall":: "S"(calltype), "D"(_syscall_message): "memory"); +} + +int64_t +syscall_invoke_ret(uint64_t calltype) +{ + int rax = 0; \ + __asm__ volatile("syscall": "=a"(rax) \ + : "S"(calltype), "D"(_syscall_message) \ + : "memory"); + return rax; + +} + -- cgit v1.2.1