From: Rhett Date: Mon, 2 Sep 2013 00:14:18 +0000 (-0700) Subject: Minimal user library and app for testing system call X-Git-Url: https://git.kitaultman.com/?a=commitdiff_plain;h=93a9ac87d041b21a48fb58e2bc252fb37bfc9277;p=arcanos.git Minimal user library and app for testing system call --- diff --git a/GNUmakefile b/GNUmakefile index 29875d5..e6e0d43 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -36,7 +36,7 @@ PERL := perl # Compiler flags # -fno-builtin is required to avoid refs to undefined functions in the kernel. # Only optimize to -O1 to discourage inlining, which complicates backtraces. -CFLAGS := $(CFLAGS) $(DEFS) $(LABDEFS) -O -fno-builtin -I$(TOP) -MD -Wall -Wno-format -Wno-unused -Werror -gstabs -fno-stack-protector -std=gnu99 -m32 +CFLAGS := $(CFLAGS) $(DEFS) $(LABDEFS) -O -fno-builtin -I$(TOP) -MD -Wall -Wno-format -Wno-unused -Werror -gstabs -fno-stack-protector -std=gnu99 -m32 -nostdlib # Lists that the */Makefrag makefile fragments will add to OBJDIRS := @@ -55,14 +55,15 @@ all: $(OBJDIR)/lib/%.o $(OBJDIR)/fs/%.o $(OBJDIR)/user/%.o KERN_CFLAGS := $(CFLAGS) -DARCANOS_KERNEL -gstabs -USER_CFLAGS := $(CFLAGS) -DARCANOS_USER -gstabs +USER_CFLAGS := $(CFLAGS) -I usr/inc -DARCANOS_USER -gstabs # Include Makefrags for subdirectories include kern/Makefrag - +include usr/lib/Makefrag +include usr/app/Makefrag IMAGES = $(OBJDIR)/kern/bochs.img diff --git a/usr/app/Makefrag b/usr/app/Makefrag new file mode 100644 index 0000000..efa9120 --- /dev/null +++ b/usr/app/Makefrag @@ -0,0 +1,32 @@ +OBJDIRS += usr/app + +USRAPP_LIBFLAGS := -L obj/usr/lib -l usrlib + +USRAPP_SRCFILES := usr/app/try_syscall.c \ + +# Only build files if they exist. +USRAPP_SRCFILES := $(wildcard $(USRAPP_SRCFILES)) + +USRAPP_OBJFILES := $(patsubst %.c, $(OBJDIR)/%.o, $(USRAPP_SRCFILES)) +USRAPP_OBJFILES := $(patsubst %.S, $(OBJDIR)/%.o, $(USRAPP_OBJFILES)) + +# How to build app object files +$(OBJDIR)/usr/app/%.o: usr/app/%.c + @echo + cc $< + @mkdir -p $(@D) + $(V)$(CC) -nostdinc $(USER_CFLAGS) -c -o $@ $< + +$(OBJDIR)/usr/app/%.o: usr/app/%.S + @echo + as $< + @mkdir -p $(@D) + $(V)$(CC) -nostdinc $(USER_CFLAGS) -c -o $@ $< + +# How to build the app itself +$(OBJDIR)/usr/app/try_syscall: $(USRAPP_OBJFILES) + @echo + cc $@ + $(V)$(CC) $(USER_CFLAGS) -o $@ $(USRAPP_OBJFILES) $(USRAPP_LIBFLAGS) + $(V)$(OBJDUMP) -S $@ > $@.asm + $(V)$(NM) -n $@ > $@.sym + +all: $(OBJDIR)/usr/app/try_syscall + diff --git a/usr/app/a.out b/usr/app/a.out new file mode 100755 index 0000000..0c29ad3 Binary files /dev/null and b/usr/app/a.out differ diff --git a/usr/app/try_syscall.c b/usr/app/try_syscall.c new file mode 100644 index 0000000..8d100a5 --- /dev/null +++ b/usr/app/try_syscall.c @@ -0,0 +1,9 @@ +#include + +int main (int argc, char **argv) +{ + while (1) + { + syscall(); + } +} diff --git a/usr/inc/syscall.h b/usr/inc/syscall.h new file mode 100644 index 0000000..c4510aa --- /dev/null +++ b/usr/inc/syscall.h @@ -0,0 +1 @@ +void syscall(); diff --git a/usr/lib/Makefrag b/usr/lib/Makefrag new file mode 100644 index 0000000..20c23b9 --- /dev/null +++ b/usr/lib/Makefrag @@ -0,0 +1,33 @@ +OBJDIRS += usr/lib + +USRLIB_LDFLAGS := $(LDFLAGS) -melf_i386 -nostdlib + +USRLIB_SRCFILES := usr/lib/syscall.c \ + usr/lib/entry.S \ + +# Only build files if they exist. +USRLIB_SRCFILES := $(wildcard $(USRLIB_SRCFILES)) + +USRLIB_OBJFILES := $(patsubst %.c, $(OBJDIR)/%.o, $(USRLIB_SRCFILES)) +USRLIB_OBJFILES := $(patsubst %.S, $(OBJDIR)/%.o, $(USRLIB_OBJFILES)) + +# How to build usrlib object files +$(OBJDIR)/usr/lib/%.o: usr/lib/%.c + @echo + cc $< + @mkdir -p $(@D) + $(V)$(CC) -nostdinc $(USER_CFLAGS) -c -o $@ $< + +$(OBJDIR)/usr/lib/%.o: usr/lib/%.S + @echo + as $< + @mkdir -p $(@D) + $(V)$(CC) -nostdinc $(USER_CFLAGS) -c -o $@ $< + +# How to build the usrlib itself +$(OBJDIR)/usr/lib/libusrlib.a: $(USRLIB_OBJFILES) + @echo + ld $@ + $(V)$(AR) rcs $@ $(USRLIB_OBJFILES) + $(V)$(OBJDUMP) -S $@ > $@.asm + $(V)$(NM) -n $@ > $@.sym + +all: $(OBJDIR)/usr/lib/libusrlib.a + diff --git a/usr/lib/entry.S b/usr/lib/entry.S new file mode 100644 index 0000000..bfd9daf --- /dev/null +++ b/usr/lib/entry.S @@ -0,0 +1,21 @@ +.global _start +.extern main +_start: + +## here you might want to get the argc/argv pairs somehow and then push +## them onto the stack... + +## right now, though, we have no concept of a "command line" and thus no +## args that could be pushed +pushl $0 +pushl $0 + +# call the user's function +call main + +# once it exists, call a syscall to terminate the process + +# loop in case we haven't yet rescheduled +lp: +hlt +jmp lp diff --git a/usr/lib/syscall.c b/usr/lib/syscall.c new file mode 100644 index 0000000..8a7cade --- /dev/null +++ b/usr/lib/syscall.c @@ -0,0 +1,5 @@ +#include + +void syscall() { + __asm__("int $0x80"); +}