aboutsummaryrefslogtreecommitdiff
path: root/system
diff options
context:
space:
mode:
Diffstat (limited to 'system')
-rw-r--r--system/Files55
-rw-r--r--system/README5
-rw-r--r--system/__alarm.c17
-rw-r--r--system/__environ.c1
-rw-r--r--system/__errno.c1
-rw-r--r--system/__errno_location.c3
-rw-r--r--system/__nice.c8
-rw-r--r--system/__sbrk.c25
-rw-r--r--system/__time.c14
-rw-r--r--system/__waitpid.c11
-rw-r--r--system/features.h7
-rw-r--r--system/i386/Flags1
-rw-r--r--system/i386/Makefile5
-rw-r--r--system/i386/__restore.S12
-rw-r--r--system/i386/__restore_rt.S9
-rw-r--r--system/i386/mmap.S11
-rw-r--r--system/i386/start.S37
-rw-r--r--system/i386/syscalls.h34
-rw-r--r--system/i386/unified.S68
-rw-r--r--system/i386/unified_256.S7
-rw-r--r--system/x86_64/Flags1
-rw-r--r--system/x86_64/Makefile5
-rw-r--r--system/x86_64/__restore_rt.S8
-rw-r--r--system/x86_64/gettimeofday.S18
-rw-r--r--system/x86_64/start.S17
-rw-r--r--system/x86_64/syscalls.h20
-rw-r--r--system/x86_64/unified.S27
-rw-r--r--system/x86_64/waitpid.S10
28 files changed, 437 insertions, 0 deletions
diff --git a/system/Files b/system/Files
new file mode 100644
index 0000000..6b1aaad
--- /dev/null
+++ b/system/Files
@@ -0,0 +1,55 @@
+# -*-Makefile-*-
+CC = gcc
+CFLAGS = -Os -W -Wall
+
+ifeq ($(FLAG_DEBUG),no)
+CCC_ = @echo ' CC $<';
+else
+CCC_ =
+endif
+
+CCC = $(CCC_) $(CC) $(CFLAGS) $(OPTIMIZATION)
+
+NINIT_O=fork.o waitpid.o __waitpid.o wait4.o \
+ nanosleep.o time.o __time.o gettimeofday.o \
+ close.o open.o getpid.o access.o execve.o ioctl.o write.o \
+ dup2.o fcntl.o poll.o chdir.o kill.o read.o lseek.o mmap.o \
+ munmap.o symlink.o rt_sigaction.o rt_sigprocmask.o \
+ SYS_reboot.o SYS_mknod.o
+
+RUN_O=setuid.o setgid.o SYS_setgroups.o \
+ writev.o getppid.o setsid.o \
+ alarm.o __alarm.o settimer.o \
+ readlink.o nice.o __nice.o getpriority.o setpriority.o
+
+RUN_WAIT_O=setrlimit.o getrlimit.o flock.o
+NSVC_O=getuid.o geteuid.o
+OTHER = unlink.o sync.o rename.o pipe.o uname.o \
+ SYS_chown.o chmod.o mkdir.o umask.o fsync.o \
+ __errno.o __environ.o __errno_location.o SYS_brk.o __sbrk.o
+
+ALL = $(NINIT_O) $(RUN_O) $(RUN_WAIT_O) $(NSVC_O) $(OTHER)
+
+start.o: system.a
+
+__%.o: ../__%.c
+ $(CCC) -c -o $@ $<
+
+start.o: start.S
+ $(CCC) -c -include ../features.h $<
+unified.o: unified.S
+ $(CCC) -c -include ../features.h $<
+%.o: %.S
+ $(CCC) -c -include ./syscalls.h $<
+
+SYS_%.S:
+ @( echo '#ifdef __NR_'$*; \
+ echo 'syscall_weak('$*,SYS_$*,$*')'; \
+ echo '#endif' ) > $@
+%.S:
+ @( echo '#ifdef __NR_'$*; \
+ echo 'syscall('$*,$*')'; \
+ echo '#endif' ) > $@
+
+clean:
+ rm -rf *.o *.a SYS_*.S
diff --git a/system/README b/system/README
new file mode 100644
index 0000000..7fda65a
--- /dev/null
+++ b/system/README
@@ -0,0 +1,5 @@
+This is part of dietlibc.
+I wrote only Makefile, Files and *.c.
+*.S are almost copy/paste.
+
+Nikola
diff --git a/system/__alarm.c b/system/__alarm.c
new file mode 100644
index 0000000..49eb8f6
--- /dev/null
+++ b/system/__alarm.c
@@ -0,0 +1,17 @@
+#include <asm/unistd.h>
+#ifndef __NR_alarm
+
+#include <unistd.h>
+#include <sys/time.h>
+
+unsigned int alarm(unsigned int seconds) {
+ struct itimerval old, new;
+ unsigned int ret;
+ new.it_interval.tv_usec=0;
+ new.it_interval.tv_sec=0;
+ new.it_value.tv_usec =0;
+ new.it_value.tv_sec =(long)seconds;
+ if (setitimer(ITIMER_REAL,&new,&old)==-1) return 0;
+ return old.it_value.tv_sec+(old.it_value.tv_usec?1:0);
+}
+#endif
diff --git a/system/__environ.c b/system/__environ.c
new file mode 100644
index 0000000..b6c6a47
--- /dev/null
+++ b/system/__environ.c
@@ -0,0 +1 @@
+char **environ=0;
diff --git a/system/__errno.c b/system/__errno.c
new file mode 100644
index 0000000..3c6cdb0
--- /dev/null
+++ b/system/__errno.c
@@ -0,0 +1 @@
+int errno=0;
diff --git a/system/__errno_location.c b/system/__errno_location.c
new file mode 100644
index 0000000..76900d3
--- /dev/null
+++ b/system/__errno_location.c
@@ -0,0 +1,3 @@
+extern int errno;
+int *__errno_location() { return &errno; }
+
diff --git a/system/__nice.c b/system/__nice.c
new file mode 100644
index 0000000..7898012
--- /dev/null
+++ b/system/__nice.c
@@ -0,0 +1,8 @@
+#include <asm/unistd.h>
+#include <sys/resource.h>
+
+#ifndef __NR_nice
+int nice(int i) {
+ return setpriority(PRIO_PROCESS,0,getpriority(PRIO_PROCESS,0)+i);
+}
+#endif
diff --git a/system/__sbrk.c b/system/__sbrk.c
new file mode 100644
index 0000000..df2a302
--- /dev/null
+++ b/system/__sbrk.c
@@ -0,0 +1,25 @@
+#include <sys/types.h>
+#define N ((void *)-1)
+
+extern void *SYS_brk(void *x);
+static void *cur = N;
+
+void *sbrk(ssize_t incr) {
+ void *t=0, *old=N;
+
+ if (cur == N) {
+ again:
+ cur = SYS_brk(t);
+ if (cur == N) return cur;
+ }
+
+ if (old == N) {
+ old = cur;
+ if (incr) {
+ t = old + incr;
+ goto again;
+ }
+ }
+
+ return old;
+}
diff --git a/system/__time.c b/system/__time.c
new file mode 100644
index 0000000..2ade864
--- /dev/null
+++ b/system/__time.c
@@ -0,0 +1,14 @@
+#include <asm/unistd.h>
+
+#ifndef __NR_time
+
+#include <time.h>
+#include <sys/time.h>
+
+time_t time(time_t *foo) {
+ struct timeval tv;
+ gettimeofday(&tv,0);
+ if (foo) *foo=tv.tv_sec;
+ return tv.tv_sec;
+}
+#endif
diff --git a/system/__waitpid.c b/system/__waitpid.c
new file mode 100644
index 0000000..06f44c1
--- /dev/null
+++ b/system/__waitpid.c
@@ -0,0 +1,11 @@
+#include <asm/unistd.h>
+
+#ifndef __NR_waitpid
+
+#include <sys/types.h>
+#include <sys/wait.h>
+
+pid_t waitpid(pid_t pid, int *status, int options) {
+ return wait4(pid, status, options, 0);
+}
+#endif
diff --git a/system/features.h b/system/features.h
new file mode 100644
index 0000000..507b5ff
--- /dev/null
+++ b/system/features.h
@@ -0,0 +1,7 @@
+/* use __errno_location instead of errno */
+#define WANT_THREAD_SAFE
+
+/* on i386, Linux has an alternate syscall method since 2002/12/16 */
+/* on my Athlon XP, it is twice as fast, but it's only in kernel 2.5 */
+/* 20040118: enabling this breaks User Mode Linux! It's their fault. */
+#define WANT_SYSENTER
diff --git a/system/i386/Flags b/system/i386/Flags
new file mode 100644
index 0000000..77b27ee
--- /dev/null
+++ b/system/i386/Flags
@@ -0,0 +1 @@
+OPTIMIZATION = -fomit-frame-pointer -mpreferred-stack-boundary=2 -falign-functions=1 -falign-jumps=1 -falign-loops=1
diff --git a/system/i386/Makefile b/system/i386/Makefile
new file mode 100644
index 0000000..f02a48f
--- /dev/null
+++ b/system/i386/Makefile
@@ -0,0 +1,5 @@
+include Flags
+include ../Files
+
+system.a: unified.o unified_256.o $(ALL) __restore.o __restore_rt.o
+ ar cr $@ $^
diff --git a/system/i386/__restore.S b/system/i386/__restore.S
new file mode 100644
index 0000000..2c7936a
--- /dev/null
+++ b/system/i386/__restore.S
@@ -0,0 +1,12 @@
+#include "syscalls.h"
+
+.text
+.type __restore,@function
+.global __restore
+.align 8
+__restore:
+ popl %eax
+ movl $__NR_sigreturn,%eax
+ int $0x80
+ hlt /* die if syscall returns */
+.size __restore,.-__restore
diff --git a/system/i386/__restore_rt.S b/system/i386/__restore_rt.S
new file mode 100644
index 0000000..06bb240
--- /dev/null
+++ b/system/i386/__restore_rt.S
@@ -0,0 +1,9 @@
+.text
+.type __restore_rt,@function
+.global __restore_rt
+.align 8
+__restore_rt:
+ movl $__NR_rt_sigreturn,%eax
+ int $0x80
+ hlt /* die if syscall returns */
+.size __restore_rt,.-__restore_rt
diff --git a/system/i386/mmap.S b/system/i386/mmap.S
new file mode 100644
index 0000000..c2b375e
--- /dev/null
+++ b/system/i386/mmap.S
@@ -0,0 +1,11 @@
+.text
+.global mmap
+.type mmap,@function
+mmap:
+ mov $__NR_mmap,%al
+ lea 0x4(%esp,1),%edx
+ push %edx
+ call __unified_syscall
+ pop %ecx
+ ret
+.size mmap,.-mmap
diff --git a/system/i386/start.S b/system/i386/start.S
new file mode 100644
index 0000000..762e807
--- /dev/null
+++ b/system/i386/start.S
@@ -0,0 +1,37 @@
+.text
+.global _start
+_start:
+ popl %ecx /* %ecx = argc */
+ movl %esp,%esi /* %esi = argv */
+ pushl %ecx
+ leal 4(%esi,%ecx,4),%eax /* %eax = envp = (4*ecx)+%esi+4 */
+
+ pushl %eax
+ pushl %esi
+ pushl %ecx
+ movl %eax,environ
+
+#ifdef WANT_SYSENTER
+ movl %eax,%edx
+ xorl %esi,%esi
+1:
+ add $4,%edx
+ cmpl %esi,-4(%edx)
+ jne 1b
+1:
+ movl (%edx),%edi
+ test %edi,%edi
+ jz 1f
+ addl $8,%edx
+ cmpl $32,%edi
+ jne 1b
+ movl -4(%edx),%edi
+ movl %edi,__vsyscall
+1:
+#endif
+
+ call main
+ pushl %eax
+ call exit
+ hlt /* die now ! will ya ... */
+.size _start,.-_start
diff --git a/system/i386/syscalls.h b/system/i386/syscalls.h
new file mode 100644
index 0000000..41c21db
--- /dev/null
+++ b/system/i386/syscalls.h
@@ -0,0 +1,34 @@
+#include <asm/unistd.h>
+
+#define syscall(name,sym) \
+.text; \
+.type sym,@function; \
+.global sym; \
+sym: \
+.ifle __NR_##name-255; \
+ movb $__NR_##name,%al; \
+ jmp __unified_syscall; \
+.else; \
+ movw $__NR_##name,%ax; \
+ jmp __unified_syscall_256; \
+.endif; \
+.Lend##sym: ; \
+.size sym,.Lend##sym-sym
+
+#define syscall_weak(name,wsym,sym) \
+.text; \
+.type wsym,@function; \
+.weak wsym; \
+wsym: ; \
+.type sym,@function; \
+.global sym; \
+sym: \
+.ifle __NR_##name-255; \
+ movb $__NR_##name,%al; \
+ jmp __unified_syscall; \
+.else; \
+ movw $__NR_##name,%ax; \
+ jmp __unified_syscall_256; \
+.endif; \
+.Lend##sym: ; \
+.size sym,.Lend##sym-sym
diff --git a/system/i386/unified.S b/system/i386/unified.S
new file mode 100644
index 0000000..82f8636
--- /dev/null
+++ b/system/i386/unified.S
@@ -0,0 +1,68 @@
+#ifdef WANT_SYSENTER
+.data
+.type __vsyscall,@object
+.global __vsyscall
+__vsyscall:
+.Lvsyscall:
+.long .Lcallint80
+#endif
+
+.text
+.weak exit
+exit:
+.global _exit
+.type _exit,@function
+_exit:
+ movb $1,%al
+.size _exit,.-_exit
+
+.global __unified_syscall
+.type __unified_syscall,@function
+__unified_syscall:
+ movzbl %al, %eax
+.size __unified_syscall,.-__unified_syscall
+
+.global __unified_return
+.type __unified_return,@function
+__unified_return:
+ push %edi
+ push %esi
+ push %ebx
+ movl %esp,%edi
+ /* we use movl instead of pop because otherwise a signal would
+ destroy the stack frame and crash the program, although it
+ would save a few bytes. */
+ movl 0x10(%edi),%ebx
+ movl 0x14(%edi),%ecx
+ movl 0x18(%edi),%edx
+ movl 0x1c(%edi),%esi
+ movl 0x20(%edi),%edi
+#ifdef WANT_SYSENTER
+ call *.Lvsyscall /* 0xffffe000 */
+#else
+ int $0x80
+#endif
+ cmp $-124,%eax
+ jb .Lnoerror
+ neg %eax
+#ifdef WANT_THREAD_SAFE
+ movl %eax,%ebx
+ call __errno_location
+ movl %ebx,(%eax)
+ orl $-1,%eax
+#else
+ mov %eax,errno
+ sbb %eax,%eax # eax = eax - eax - CY = -1
+#endif
+.Lnoerror:
+ pop %ebx
+ pop %esi
+ pop %edi
+ ret
+.size __unified_return,.-__unified_return
+
+#ifdef WANT_SYSENTER
+.Lcallint80:
+ int $0x80
+ ret
+#endif
diff --git a/system/i386/unified_256.S b/system/i386/unified_256.S
new file mode 100644
index 0000000..76436ce
--- /dev/null
+++ b/system/i386/unified_256.S
@@ -0,0 +1,7 @@
+.text
+.global __unified_syscall_256
+.type __unified_syscall_256,@function
+__unified_syscall_256:
+ movzwl %ax,%eax
+ jmp __unified_return
+.size __unified_syscall_256,.-__unified_syscall_256
diff --git a/system/x86_64/Flags b/system/x86_64/Flags
new file mode 100644
index 0000000..af36a53
--- /dev/null
+++ b/system/x86_64/Flags
@@ -0,0 +1 @@
+OPTIMIZATION =
diff --git a/system/x86_64/Makefile b/system/x86_64/Makefile
new file mode 100644
index 0000000..6b11a00
--- /dev/null
+++ b/system/x86_64/Makefile
@@ -0,0 +1,5 @@
+include Flags
+include ../Files
+
+system.a: unified.o $(ALL) __restore_rt.o
+ ar cr $@ $^
diff --git a/system/x86_64/__restore_rt.S b/system/x86_64/__restore_rt.S
new file mode 100644
index 0000000..34a5d3f
--- /dev/null
+++ b/system/x86_64/__restore_rt.S
@@ -0,0 +1,8 @@
+.text
+.align 16
+.global __restore_rt
+.type __restore_rt,@function
+__restore_rt:
+ movq $15, %rax
+ syscall
+ hlt
diff --git a/system/x86_64/gettimeofday.S b/system/x86_64/gettimeofday.S
new file mode 100644
index 0000000..8c2f83b
--- /dev/null
+++ b/system/x86_64/gettimeofday.S
@@ -0,0 +1,18 @@
+.text
+.global gettimeofday
+.type gettimeofday,@function
+gettimeofday:
+ mov $0xffffffffff600000,%rax
+ callq *%rax
+ cmpq $-128, %rax
+ jbe 1f
+ negl %eax
+ pushq %rax
+ call __errno_location
+ popq %rcx
+ movl %ecx,(%rax)
+ orq $-1, %rax
+1:
+ ret
+.Lhere:
+ .size gettimeofday,.Lhere-gettimeofday
diff --git a/system/x86_64/start.S b/system/x86_64/start.S
new file mode 100644
index 0000000..b31b9b1
--- /dev/null
+++ b/system/x86_64/start.S
@@ -0,0 +1,17 @@
+.text
+.global _start
+_start:
+ popq %rdi /* %rdi = argc */
+ movq %rsp,%rsi /* %rsi = argv */
+ pushq %rdi
+
+ leaq 8(%rsi,%rdi,8),%rdx /* %rdx = envp = (8*rdi)+%rsi+8 */
+
+ movq %rdx, environ(%rip)
+ call main
+ movq %rax, %rdi /* return value */
+ call exit
+ hlt
+.Lstart:
+ .size _start,.Lstart-_start
+
diff --git a/system/x86_64/syscalls.h b/system/x86_64/syscalls.h
new file mode 100644
index 0000000..b4f1476
--- /dev/null
+++ b/system/x86_64/syscalls.h
@@ -0,0 +1,20 @@
+#include <asm/unistd.h>
+
+#define syscall_weak(name,wsym,sym) \
+.text; \
+.type wsym,@function; \
+.weak wsym; \
+wsym: ; \
+.type sym,@function; \
+.global sym; \
+sym: \
+ mov $__NR_##name,%al; \
+ jmp __unified_syscall
+
+#define syscall(name,sym) \
+.text; \
+.type sym,@function; \
+.global sym; \
+sym: \
+ mov $__NR_##name,%al; \
+ jmp __unified_syscall
diff --git a/system/x86_64/unified.S b/system/x86_64/unified.S
new file mode 100644
index 0000000..c4dff7d
--- /dev/null
+++ b/system/x86_64/unified.S
@@ -0,0 +1,27 @@
+#define SYS_exit 0x3c
+
+.text
+.weak exit
+exit:
+.global _exit
+_exit:
+ mov $SYS_exit,%al
+
+.global __unified_syscall
+__unified_syscall:
+ movzbl %al, %eax
+ mov %rcx, %r10
+ syscall
+ cmpq $-128, %rax
+ jbe .Lnoerror
+ negl %eax
+ pushq %rax
+ call __errno_location
+ popq %rcx
+ movl %ecx,(%rax)
+ orq $-1, %rax
+.Lnoerror:
+
+ ret
+.Lhere:
+ .size __unified_syscall,.Lhere-__unified_syscall
diff --git a/system/x86_64/waitpid.S b/system/x86_64/waitpid.S
new file mode 100644
index 0000000..2a85491
--- /dev/null
+++ b/system/x86_64/waitpid.S
@@ -0,0 +1,10 @@
+.text
+.type waitpid,@function
+.weak waitpid
+waitpid:
+.type __libc_waitpid,@function
+.global __libc_waitpid
+__libc_waitpid:
+ xor %rcx,%rcx
+ mov $__NR_wait4,%al
+ jmp __unified_syscall