Minimal user library and app for testing system call
authorRhett <rhett.aultman@meraki.net>
Mon, 2 Sep 2013 00:14:18 +0000 (17:14 -0700)
committerRhett <rhett.aultman@meraki.net>
Mon, 2 Sep 2013 00:14:18 +0000 (17:14 -0700)
GNUmakefile
usr/app/Makefrag [new file with mode: 0644]
usr/app/a.out [new file with mode: 0755]
usr/app/try_syscall.c [new file with mode: 0644]
usr/inc/syscall.h [new file with mode: 0644]
usr/lib/Makefrag [new file with mode: 0644]
usr/lib/entry.S [new file with mode: 0644]
usr/lib/syscall.c [new file with mode: 0644]

index 29875d5781699bdd6d0a5ddae51c7783e9346411..e6e0d4352ad6f4b180b9e2508c411b229261dd59 100644 (file)
@@ -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 (file)
index 0000000..efa9120
--- /dev/null
@@ -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 (executable)
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 (file)
index 0000000..8d100a5
--- /dev/null
@@ -0,0 +1,9 @@
+#include <syscall.h>
+
+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 (file)
index 0000000..c4510aa
--- /dev/null
@@ -0,0 +1 @@
+void syscall();
diff --git a/usr/lib/Makefrag b/usr/lib/Makefrag
new file mode 100644 (file)
index 0000000..20c23b9
--- /dev/null
@@ -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 (file)
index 0000000..bfd9daf
--- /dev/null
@@ -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 (file)
index 0000000..8a7cade
--- /dev/null
@@ -0,0 +1,5 @@
+#include <syscall.h>
+
+void syscall() {
+  __asm__("int $0x80");
+}