C userspace programs

This commit is contained in:
2025-09-04 23:20:30 +02:00
parent afa4d383e0
commit 90266f044b
51 changed files with 259 additions and 174 deletions

1
ulib/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.o

33
ulib/Makefile Normal file
View File

@ -0,0 +1,33 @@
include $(ROOT)/mk/grabsrc.mk
include $(ROOT)/mk/arch/toolchain-$(ARCH).mk
include $(ROOT)/mk/user/$(ARCH).mk
.PHONY: all clean
SRCFILES := $(call GRABSRC, \
. \
syscall \
string \
system \
)
CFLAGS += -isystem $(ROOT)/ulib -isystem $(ROOT)/std/include
ASFILES := $(call GET_ASFILES, $(SRCFILES))
CFILES := $(call GET_CFILES, $(SRCFILES))
OBJ := $(call GET_OBJ, $(SRCFILES))
%.o: %.S
$(CC) $(CFLAGS) -c $< -o $@
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
libulib.a: $(OBJ)
$(AR) rcs libulib.a $(OBJ)
all: libulib.a
clean:
rm -f $(OBJ)
rm -f libulib.a

22
ulib/_premain.c Normal file
View File

@ -0,0 +1,22 @@
#include <stdint.h>
#include <stddef.h>
extern void main(void);
extern uint8_t _bss_start[];
extern uint8_t _bss_end[];
void bss_clear(void) {
uint8_t *p = _bss_start;
while (p != _bss_end) {
*p = 0;
p++;
}
}
// ulib initialization goes here
void _premain(void) {
bss_clear();
main();
}

10
ulib/_start.S Normal file
View File

@ -0,0 +1,10 @@
#include <syscall/syscall.h>
.extern _premain
.global _start
_start:
call _premain
mov $SYS_QUITPROC, %rax
int $0x80

BIN
ulib/libulib.a Normal file

Binary file not shown.

11
ulib/string/string.c Normal file
View File

@ -0,0 +1,11 @@
#include <stddef.h>
#include <string/string.h>
size_t string_len(const char *s) {
size_t l = 0;
while (*s != '\0') {
l++;
s++;
}
return l;
}

8
ulib/string/string.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef ULIB_STRING_STRING_H_
#define ULIB_STRING_STRING_H_
#include <stddef.h>
size_t string_len(const char *s);
#endif // ULIB_STRING_STRING_H_

27
ulib/syscall/syscall.c Normal file
View File

@ -0,0 +1,27 @@
#include <stdint.h>
uint64_t syscall(uint64_t num, uint64_t arg1, uint64_t arg2,
uint64_t arg3, uint64_t arg4, uint64_t arg5, uint64_t arg6) {
uint64_t ret = 0;
asm volatile (
"mov %[SYSNUM], %%rax\n"
"mov %[ARG1], %%rdi\n"
"mov %[ARG2], %%rsi\n"
"mov %[ARG3], %%rdx\n"
"mov %[ARG4], %%r10\n"
"mov %[ARG5], %%r8\n"
"mov %[ARG6], %%r9\n"
"int $0x80\n"
"mov %%rax, %[RESULT]\n"
: [RESULT]"=r"(ret)
: [SYSNUM]"r"(num),
[ARG1]"r"(arg1),
[ARG2]"r"(arg2),
[ARG3]"r"(arg3),
[ARG4]"r"(arg4),
[ARG5]"r"(arg5),
[ARG6]"r"(arg6)
: "%rax", "%rdi", "%rsi", "%rdx", "%r10", "%r8", "%r9", "memory"
);
return ret;
}

14
ulib/syscall/syscall.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef ULIB_SYSCALL_SYSCALL_H_
#define ULIB_SYSCALL_SYSCALL_H_
#define SYS_DEBUGPRINT 1
#define SYS_QUITPROC 2
#if !defined(__ASSEMBLER__)
uint64_t syscall(uint64_t num, uint64_t arg1, uint64_t arg2,
uint64_t arg3, uint64_t arg4, uint64_t arg5, uint64_t arg6);
#endif // ! __ASSEMBLER__
#endif // ULIB_SYSCALL_SYSCALL_H_

9
ulib/system/system.c Normal file
View File

@ -0,0 +1,9 @@
#include <stdint.h>
#include <system/system.h>
#include <syscall/syscall.h>
void sys_debugprint(const char *string) {
syscall(SYS_DEBUGPRINT, (uint64_t)string, 0, 0, 0, 0, 0);
}

6
ulib/system/system.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef ULIB_SYSTEM_SYSTEM_H_
#define ULIB_SYSTEM_SYSTEM_H_
void sys_debugprint(const char *string);
#endif // ULIB_SYSTEM_SYSTEM_H_