From f86c78f497c6f2662f17ce9ac38371380e924862 Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Sat, 5 Oct 2019 16:06:45 +0900 Subject: [PATCH] Start implementation of a statically linked standard C library: - Add implementation of network-related syscalls --- libc/Makefile | 16 +++++ libc/syscall.S | 188 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 204 insertions(+) create mode 100644 libc/Makefile create mode 100644 libc/syscall.S diff --git a/libc/Makefile b/libc/Makefile new file mode 100644 index 0000000..5029bad --- /dev/null +++ b/libc/Makefile @@ -0,0 +1,16 @@ +OUTPUT = libc.a +OBJECTS = syscall.o +PHONY = clean +INCLUDES = -I../include +CFLAGS = -m32 -Wall -nostdlib -nodefaultlibs -nostartfiles -ffreestanding $(INCLUDES) +ASFLAGS = $(CFLAGS) + +all: $(OUTPUT) + +$(OUTPUT): $(OBJECTS) + ar -rc $@ $^ + +clean: + rm -rf $(OUTPUT) $(OBJECTS) + +.PHONY: $(PHONY) diff --git a/libc/syscall.S b/libc/syscall.S new file mode 100644 index 0000000..f1321ab --- /dev/null +++ b/libc/syscall.S @@ -0,0 +1,188 @@ +#define __ASSEMBLY_SOURCE + +#include + +.global socket +socket: + pushl %ebx + + movl $SYS_SOCKET, %eax + movl 8(%esp), %ebx + movl 12(%esp), %ecx + movl 16(%esp), %edx + int $SYSCALL_CXNET + + popl %ebx + ret + +.global close +close: + pushl %ebx + + movl $SYS_CLOSE, %eax + movl 8(%esp), %ebx + int $SYSCALL_CXNET + + popl %ebx + ret + +.global sendto +sendto: + /* + * sendto() and recvfrom() have 6 arguments, so we also have + * to preserve the ebp register in order to repurpose it + */ + pushl %ebx + pushl %ebp + + movl $SYS_SENDTO, %eax + movl 12(%esp), %ebx + movl 16(%esp), %ecx + movl 20(%esp), %edx + movl 24(%esp), %esi + movl 28(%esp), %edi + movl 32(%esp), %ebp + int $SYSCALL_CXNET + + popl %ebp + popl %ebx + ret + +.global recvfrom +recvfrom: + pushl %ebx + pushl %ebp + + movl $SYS_RECVFROM, %eax + movl 12(%esp), %ebx + movl 16(%esp), %ecx + movl 20(%esp), %edx + movl 24(%esp), %esi + movl 28(%esp), %edi + movl 32(%esp), %ebp + int $SYSCALL_CXNET + + popl %ebp + popl %ebx + ret + +.global connect +connect: + pushl %ebx + + movl $SYS_CONNECT, %eax + movl 8(%esp), %ebx + movl 12(%esp), %ecx + movl 16(%esp), %edx + int $SYSCALL_CXNET + + popl %ebx + ret + +.global listen +listen: + pushl %ebx + + movl $SYS_LISTEN, %eax + movl 8(%esp), %ebx + movl 12(%esp), %ecx + int $SYSCALL_CXNET + + popl %ebx + ret + +.global accept +accept: + pushl %ebx + + movl $SYS_ACCEPT, %eax + movl 8(%esp), %ebx + movl 12(%esp), %ecx + movl 16(%esp), %edx + int $SYSCALL_CXNET + + popl %ebx + ret + +.global setsockopt +setsockopt: + pushl %ebx + + movl $SYS_SETSOCKOPT, %eax + movl 8(%esp), %ebx + movl 12(%esp), %ecx + movl 16(%esp), %edx + movl 20(%esp), %esi + movl 24(%esp), %edi + int $SYSCALL_CXNET + + popl %ebx + ret + +.global getsockopt +getsockopt: + pushl %ebx + + movl $SYS_GETSOCKOPT, %eax + movl 8(%esp), %ebx + movl 12(%esp), %ecx + movl 16(%esp), %edx + movl 20(%esp), %esi + movl 24(%esp), %edi + int $SYSCALL_CXNET + + popl %ebx + ret + +.global shutdown +shutdown: + pushl %ebx + + movl $SYS_SHUTDOWN, %eax + movl 8(%esp), %ebx + movl 12(%esp), %ecx + int $SYSCALL_CXNET + + popl %ebx + ret + +.global select +select: + pushl %ebx + + movl $SYS_SELECT, %eax + movl 8(%esp), %ebx + movl 12(%esp), %ecx + movl 16(%esp), %edx + movl 20(%esp), %esi + movl 24(%esp), %edi + int $SYSCALL_CXNET + + popl %ebx + ret + +.global poll +poll: + pushl %ebx + + movl $SYS_POLL, %eax + movl 8(%esp), %ebx + movl 12(%esp), %ecx + movl 16(%esp), %edx + int $SYSCALL_CXNET + + popl %ebx + ret + +.global bind +bind: + pushl %ebx + + movl $SYS_BIND, %eax + movl 8(%esp), %ebx + movl 12(%esp), %ecx + movl 16(%esp), %edx + int $SYSCALL_CXNET + + popl %ebx + ret -- 2.47.3