From 9c3ba04b2be3a323ba68500455d7fe2a4e886234 Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Sat, 1 Aug 2020 15:08:47 +0900 Subject: [PATCH] sys/test: Add userspace process for libc testing --- sys/Makefile | 6 ++++-- sys/initfs.c | 9 +++++++++ sys/test/Makefile | 26 +++++++++++++++++++++++++ sys/test/main.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 sys/test/Makefile create mode 100644 sys/test/main.c diff --git a/sys/Makefile b/sys/Makefile index d884627..8ae819f 100644 --- a/sys/Makefile +++ b/sys/Makefile @@ -1,4 +1,4 @@ -DEPS = kbd vga io +DEPS = kbd vga io test PHONY = $(DEPS) clean OUTPUT = initfs.a INCLUDES = -I.. -I../include @@ -13,7 +13,9 @@ vga.ko: vga io.ko: io -$(OUTPUT): initfs.o kbd.ko vga.ko io.ko +test.ko: test + +$(OUTPUT): initfs.o kbd.ko vga.ko io.ko test.ko ar -rc $@ $^ updateinitfs: diff --git a/sys/initfs.c b/sys/initfs.c index 13de1c4..a0ef264 100644 --- a/sys/initfs.c +++ b/sys/initfs.c @@ -19,6 +19,10 @@ extern const char _binary_io_start[]; extern const unsigned long _binary_io_size; #endif /* FEATURE(VGA) */ +extern const char _binary_test_end[]; +extern const char _binary_test_start[]; +extern const unsigned long _binary_test_size; + static struct initfs_entry _ifs_entries[] = { #if FEATURE(IO) { @@ -41,6 +45,11 @@ static struct initfs_entry _ifs_entries[] = { .ife_limit = (unsigned long)&_binary_vga_size }, #endif /* FEATURE(VGA) */ + { + .ife_name = "test", + .ife_base = &_binary_test_start, + .ife_limit = (unsigned long)&_binary_test_size + } }; struct initfs initfs = { diff --git a/sys/test/Makefile b/sys/test/Makefile new file mode 100644 index 0000000..5cf9b78 --- /dev/null +++ b/sys/test/Makefile @@ -0,0 +1,26 @@ +OBJECTS = main.o +OUTPUT = test +MODULE = test.ko + +INCLUDES = -I../../include -I../.. +CFLAGS = -m32 -Wall -nostdlib -nodefaultlibs -nostartfiles -ffreestanding \ + -nostdinc -fno-builtin -fno-builtin-memcpy +LIBS = ../../libc/libc.a +CFLAGS += $(INCLUDES) + +ASFLAGS = $(CFLAGS) +PHONY = clean + +all: $(MODULE) + +$(MODULE): $(OUTPUT) + ld -r --oformat=elf32-i386 -m elf_i386 -b binary -o $@ $< + cp $@ ../ + +$(OUTPUT): $(OBJECTS) + gcc -static $(CFLAGS) -o $@ $^ $(LIBS) + +clean: + rm -f $(OBJECTS) $(OUTPUT) + +.PHONY: $(PHONY) diff --git a/sys/test/main.c b/sys/test/main.c new file mode 100644 index 0000000..ecba8fa --- /dev/null +++ b/sys/test/main.c @@ -0,0 +1,48 @@ +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + char buf[128]; + int fds[2]; + int ret_val; + void *abuf; + void *bbuf; + void *heap; + + heap = sbrk(0); + + ret_val = snprintf(buf, sizeof(buf), "Heap base: %p / 0x%08lx\n", heap, heap); + debug(buf, ret_val); + + abuf = sbrk(2048); + + ret_val = snprintf(buf, sizeof(buf), "Allocated 4096B at %p / 0x%08lx\n", abuf, abuf); + debug(buf, ret_val); + + bbuf = malloc(4096); + + ret_val = snprintf(buf, sizeof(buf), "Allocated 4096B at %p / 0x%08x\n", bbuf, buf); + debug(buf, ret_val); + + ret_val = snprintf(buf, sizeof(buf), "Calling pipe()\n"); + debug(buf, ret_val); + + ret_val = pipe(fds); + + ret_val = snprintf(buf, sizeof(buf), "pipe() returned %u\n", ret_val); + debug(buf, ret_val); + + while(1) { + #if 0 + struct cxmsg msg; + + /* guaranteed to cause the process to be suspended */ + cxrecv(PID_IO, &msg); + #endif + } + + return(0); +} -- 2.47.3