From e15065fe45f8004adcc8b69bbbe680ced0d530a1 Mon Sep 17 00:00:00 2001
From: Anton Kling <anton@kling.gg>
Date: Wed, 26 Jun 2024 21:34:40 +0200
Subject: Kernel: Move all syscalls into one file

---
 kernel/Makefile                 |   2 +-
 kernel/cpu/syscall.c            | 372 +++++++++++++++++++++++++++++++++++++++-
 kernel/cpu/syscall.h            |  54 ------
 kernel/includes/syscalls.h      | 124 --------------
 kernel/syscalls/accept.c        |   5 -
 kernel/syscalls/bind.c          |   5 -
 kernel/syscalls/chdir.c         |   6 -
 kernel/syscalls/clock_gettime.c |  10 --
 kernel/syscalls/fstat.c         |  11 --
 kernel/syscalls/ftruncate.c     |   6 -
 kernel/syscalls/getcwd.c        |  10 --
 kernel/syscalls/isatty.c        |  14 --
 kernel/syscalls/kill.c          |  15 --
 kernel/syscalls/lseek.c         |  34 ----
 kernel/syscalls/mkdir.c         |   5 -
 kernel/syscalls/mmap.c          |   7 -
 kernel/syscalls/msleep.c        |   9 -
 kernel/syscalls/munmap.c        |   6 -
 kernel/syscalls/open.c          |  14 --
 kernel/syscalls/open_process.c  |  26 ---
 kernel/syscalls/ppoll.c         |  11 --
 kernel/syscalls/pwrite.c        |   6 -
 kernel/syscalls/randomfill.c    |   9 -
 kernel/syscalls/recvfrom.c      |  60 -------
 kernel/syscalls/sendto.c        |  23 ---
 kernel/syscalls/shm.c           |   6 -
 kernel/syscalls/sigaction.c     |   9 -
 kernel/syscalls/socket.c        |   5 -
 kernel/syscalls/uptime.c        |   6 -
 kernel/syscalls/write.c         |  13 --
 30 files changed, 372 insertions(+), 511 deletions(-)
 delete mode 100644 kernel/includes/syscalls.h
 delete mode 100644 kernel/syscalls/accept.c
 delete mode 100644 kernel/syscalls/bind.c
 delete mode 100644 kernel/syscalls/chdir.c
 delete mode 100644 kernel/syscalls/clock_gettime.c
 delete mode 100644 kernel/syscalls/fstat.c
 delete mode 100644 kernel/syscalls/ftruncate.c
 delete mode 100644 kernel/syscalls/getcwd.c
 delete mode 100644 kernel/syscalls/isatty.c
 delete mode 100644 kernel/syscalls/kill.c
 delete mode 100644 kernel/syscalls/lseek.c
 delete mode 100644 kernel/syscalls/mkdir.c
 delete mode 100644 kernel/syscalls/mmap.c
 delete mode 100644 kernel/syscalls/msleep.c
 delete mode 100644 kernel/syscalls/munmap.c
 delete mode 100644 kernel/syscalls/open.c
 delete mode 100644 kernel/syscalls/open_process.c
 delete mode 100644 kernel/syscalls/ppoll.c
 delete mode 100644 kernel/syscalls/pwrite.c
 delete mode 100644 kernel/syscalls/randomfill.c
 delete mode 100644 kernel/syscalls/recvfrom.c
 delete mode 100644 kernel/syscalls/sendto.c
 delete mode 100644 kernel/syscalls/shm.c
 delete mode 100644 kernel/syscalls/sigaction.c
 delete mode 100644 kernel/syscalls/socket.c
 delete mode 100644 kernel/syscalls/uptime.c
 delete mode 100644 kernel/syscalls/write.c

diff --git a/kernel/Makefile b/kernel/Makefile
index 0bc1e6b..45e898e 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -1,6 +1,6 @@
 CC="i686-sb-gcc"
 AS="i686-sb-as"
-OBJ = arch/i386/boot.o init/kernel.o cpu/gdt.o cpu/reload_gdt.o cpu/idt.o cpu/io.o libc/stdio/print.o drivers/keyboard.o log.o drivers/pit.o libc/string/memcpy.o libc/string/strlen.o libc/string/memcmp.o drivers/ata.o libc/string/memset.o cpu/syscall.o  read_eip.o libc/exit/assert.o process.o libc/string/strcpy.o arch/i386/mmu.o kmalloc.o fs/ext2.o fs/vfs.o fs/devfs.o cpu/spinlock.o random.o libc/string/strcmp.o crypto/ChaCha20/chacha20.o crypto/SHA1/sha1.o fs/tmpfs.o libc/string/isequal.o drivers/pst.o syscalls/ppoll.o syscalls/ftruncate.o kubsan.o syscalls/mmap.o drivers/serial.o syscalls/accept.o syscalls/bind.o syscalls/socket.o socket.o poll.o fs/fifo.o hashmap/hashmap.o fs/shm.o syscalls/shm.o elf.o ksbrk.o sched/scheduler.o syscalls/fstat.o libc/string/copy.o drivers/mouse.o libc/string/strlcpy.o libc/string/strcat.o drivers/vbe.o syscalls/msleep.o syscalls/uptime.o syscalls/mkdir.o drivers/pci.o drivers/rtl8139.o network/ethernet.o network/arp.o network/bytes.o network/ipv4.o network/udp.o syscalls/recvfrom.o math.o syscalls/sendto.o signal.o syscalls/kill.o syscalls/sigaction.o network/tcp.o drivers/ahci.o crypto/xoshiro256plusplus/xoshiro256plusplus.o syscalls/chdir.o syscalls/getcwd.o syscalls/isatty.o syscalls/randomfill.o syscalls/open.o syscalls/write.o syscalls/pwrite.o arch/i386/interrupts.o cpu/isr.o lib/stack.o lib/buffered_write.o lib/list.o cpu/arch_inst.o cpu/int_syscall.o syscalls/munmap.o syscalls/open_process.o syscalls/lseek.o lib/ringbuffer.o lib/relist.o
+OBJ = arch/i386/boot.o init/kernel.o cpu/gdt.o cpu/reload_gdt.o cpu/idt.o cpu/io.o libc/stdio/print.o drivers/keyboard.o log.o drivers/pit.o libc/string/memcpy.o libc/string/strlen.o libc/string/memcmp.o drivers/ata.o libc/string/memset.o cpu/syscall.o  read_eip.o libc/exit/assert.o process.o libc/string/strcpy.o arch/i386/mmu.o kmalloc.o fs/ext2.o fs/vfs.o fs/devfs.o cpu/spinlock.o random.o libc/string/strcmp.o crypto/ChaCha20/chacha20.o crypto/SHA1/sha1.o fs/tmpfs.o libc/string/isequal.o drivers/pst.o kubsan.o drivers/serial.o socket.o poll.o fs/fifo.o hashmap/hashmap.o fs/shm.o elf.o ksbrk.o sched/scheduler.o libc/string/copy.o drivers/mouse.o libc/string/strlcpy.o libc/string/strcat.o drivers/vbe.o drivers/pci.o drivers/rtl8139.o network/ethernet.o network/arp.o network/bytes.o network/ipv4.o network/udp.o math.o signal.o network/tcp.o drivers/ahci.o crypto/xoshiro256plusplus/xoshiro256plusplus.o arch/i386/interrupts.o cpu/isr.o lib/stack.o lib/buffered_write.o lib/list.o cpu/arch_inst.o cpu/int_syscall.o lib/ringbuffer.o lib/relist.o
 CFLAGS = -std=c99 -O0 -fsanitize=vla-bound,shift-exponent,pointer-overflow,shift,signed-integer-overflow,bounds -ggdb -ffreestanding -Wall -Wextra -Wno-int-conversion -Wno-unused-parameter -Werror -mgeneral-regs-only -Wimplicit-fallthrough -I./libc/include/ -I. -Wno-pointer-sign -DKERNEL
 LDFLAGS=
 INCLUDE=-I./includes/ -I../include/ -I./libc/include/
diff --git a/kernel/cpu/syscall.c b/kernel/cpu/syscall.c
index 4a446d5..6d2b150 100644
--- a/kernel/cpu/syscall.c
+++ b/kernel/cpu/syscall.c
@@ -2,22 +2,392 @@
 // valid.
 #include <assert.h>
 #include <cpu/syscall.h>
+#include <drivers/pit.h>
 #include <drivers/pst.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <fs/shm.h>
 #include <fs/tmpfs.h>
 #include <fs/vfs.h>
 #include <interrupts.h>
 #include <kmalloc.h>
+#include <math.h>
 #include <network/ethernet.h>
 #include <network/tcp.h>
+#include <network/udp.h>
+#include <poll.h>
+#include <random.h>
 #include <socket.h>
 #include <string.h>
-#include <syscalls.h>
 #include <typedefs.h>
 
+typedef struct SYS_CLOCK_GETTIME_PARAMS {
+  clockid_t clk;
+  struct timespec *ts;
+} __attribute__((packed)) SYS_CLOCK_GETTIME_PARAMS;
+
+struct two_args {
+  u32 a;
+  u32 b;
+};
+
+typedef struct SYS_OPEN_PARAMS {
+  const char *file;
+  int flags;
+  int mode;
+} __attribute__((packed)) SYS_OPEN_PARAMS;
+
+typedef struct SYS_PREAD_PARAMS {
+  int fd;
+  void *buf;
+  size_t count;
+  size_t offset;
+} __attribute__((packed)) SYS_PREAD_PARAMS;
+
+typedef struct SYS_READ_PARAMS {
+  int fd;
+  void *buf;
+  size_t count;
+} __attribute__((packed)) SYS_READ_PARAMS;
+
+typedef struct SYS_PWRITE_PARAMS {
+  int fd;
+  const void *buf;
+  size_t count;
+  size_t offset;
+} __attribute__((packed)) SYS_PWRITE_PARAMS;
+
+typedef struct SYS_WRITE_PARAMS {
+  int fd;
+  const void *buf;
+  size_t count;
+} __attribute__((packed)) SYS_WRITE_PARAMS;
+
+typedef struct SYS_EXEC_PARAMS {
+  const char *path;
+  char **argv;
+} __attribute__((packed)) SYS_EXEC_PARAMS;
+
+typedef struct SYS_DUP2_PARAMS {
+  int org_fd;
+  int new_fd;
+} __attribute__((packed)) SYS_DUP2_PARAMS;
+
+typedef struct SYS_OPENPTY_PARAMS {
+  int *amaster;
+  int *aslave;
+  char *name;
+  /*const struct termios*/ void *termp;
+  /*const struct winsize*/ void *winp;
+} __attribute__((packed)) SYS_OPENPTY_PARAMS;
+
+typedef struct SYS_POLL_PARAMS {
+  struct pollfd *fds;
+  size_t nfds;
+  int timeout;
+} __attribute__((packed)) SYS_POLL_PARAMS;
+
+typedef struct SYS_MMAP_PARAMS {
+  void *addr;
+  size_t length;
+  int prot;
+  int flags;
+  int fd;
+  size_t offset;
+} __attribute__((packed)) SYS_MMAP_PARAMS;
+
+typedef struct SYS_SOCKET_PARAMS {
+  int domain;
+  int type;
+  int protocol;
+} __attribute__((packed)) SYS_SOCKET_PARAMS;
+
+typedef struct SYS_BIND_PARAMS {
+  int sockfd;
+  const struct sockaddr *addr;
+  socklen_t addrlen;
+} __attribute__((packed)) SYS_BIND_PARAMS;
+
+typedef struct SYS_ACCEPT_PARAMS {
+  int socket;
+  struct sockaddr *address;
+  socklen_t *address_len;
+} __attribute__((packed)) SYS_ACCEPT_PARAMS;
+
+typedef struct SYS_SHM_OPEN_PARAMS {
+  const char *name;
+  int oflag;
+  mode_t mode;
+} __attribute__((packed)) SYS_SHM_OPEN_PARAMS;
+
+typedef struct SYS_FTRUNCATE_PARAMS {
+  int fildes;
+  size_t length;
+} __attribute__((packed)) SYS_FTRUNCATE_PARAMS;
+
 #pragma GCC diagnostic ignored "-Wpedantic"
 
+int syscall_accept(SYS_ACCEPT_PARAMS *args) {
+  return accept(args->socket, args->address, args->address_len);
+}
+
+int syscall_bind(SYS_BIND_PARAMS *args) {
+  return bind(args->sockfd, args->addr, args->addrlen);
+}
+
+int syscall_chdir(const char *path) {
+  return vfs_chdir(path);
+}
+
+int syscall_clock_gettime(SYS_CLOCK_GETTIME_PARAMS *args) {
+  // FIXME: Actually implement this
+  if (args->ts) {
+    args->ts->tv_sec = 0;
+    args->ts->tv_nsec = 0;
+  }
+  return 0;
+}
+
+int syscall_fstat(int fd, struct stat *buf) {
+  if (!mmu_is_valid_userpointer(buf, sizeof(struct stat))) {
+    return -EPERM; // TODO: Is this correct? The spec says nothing about
+                   // this case.
+  }
+  return vfs_fstat(fd, buf);
+}
+
+int syscall_ftruncate(int fd, size_t length) {
+  return vfs_ftruncate(fd, length);
+}
+
+char *syscall_getcwd(char *buf, size_t size) {
+  const char *cwd = current_task->current_working_directory;
+  size_t len = min(size, strlen(cwd));
+  strlcpy(buf, current_task->current_working_directory, len);
+  return buf;
+}
+
+int syscall_isatty(int fd) {
+  vfs_fd_t *fd_ptr = get_vfs_fd(fd, NULL);
+  if (!fd_ptr) {
+    return -EBADF;
+  }
+  if (!fd_ptr->is_tty) {
+    return -ENOTTY;
+  }
+  return 1;
+}
+
+int syscall_kill(int fd, int sig) {
+  vfs_fd_t *fd_ptr = get_vfs_fd(fd, NULL);
+  if (!fd_ptr) {
+    return -EBADF;
+  }
+  if (!fd_ptr->inode->send_signal) {
+    return -EBADF;
+  }
+  return process_signal(fd_ptr, sig);
+}
+
+// FIXME: These should be in a shared header file with libc
+#define SEEK_SET 0
+#define SEEK_CUR 1
+#define SEEK_END 2
+
+int syscall_lseek(int fd, int offset, int whence) {
+  vfs_fd_t *fd_ptr = get_vfs_fd(fd, NULL);
+  if (!fd_ptr) {
+    return -EBADF;
+  }
+
+  off_t ret_offset = fd_ptr->offset;
+  switch (whence) {
+  case SEEK_SET:
+    ret_offset = offset;
+    break;
+  case SEEK_CUR:
+    ret_offset += offset;
+    break;
+  case SEEK_END:
+    assert(fd_ptr->inode);
+    ret_offset = fd_ptr->inode->file_size + offset;
+    break;
+  default:
+    return -EINVAL;
+    break;
+  }
+  fd_ptr->offset = ret_offset;
+  return ret_offset;
+}
+
+int syscall_mkdir(const char *path, int mode) {
+  return vfs_mkdir(path, mode);
+}
+
+void *syscall_mmap(SYS_MMAP_PARAMS *args) {
+  return mmap(args->addr, args->length, args->prot, args->flags, args->fd,
+              args->offset);
+}
+
+void syscall_msleep(u32 ms) {
+  current_task->sleep_until = pit_num_ms() + ms;
+  switch_task();
+}
+
+int syscall_munmap(void *addr, size_t length) {
+  return munmap(addr, length);
+}
+
+int syscall_open(const char *file, int flags, mode_t mode) {
+  const char *_file = copy_and_allocate_user_string(file);
+  if (!_file) {
+    return -EFAULT;
+  }
+  int _flags = flags;
+  int _mode = mode;
+  int rc = vfs_open(_file, _flags, _mode);
+  kfree((void *)_file);
+  return rc;
+}
+
+int syscall_open_process(int pid) {
+  // TODO: Permission check
+  process_t *process = (process_t *)ready_queue;
+  for (; process; process = process->next) {
+    if (pid == process->pid) {
+      break;
+    }
+  }
+  if (!process) {
+    return -ESRCH;
+  }
+
+  vfs_inode_t *inode = vfs_create_inode(
+      process->pid, 0 /*type*/, 0 /*has_data*/, 0 /*can_write*/, 1 /*is_open*/,
+      process /*internal_object*/, 0 /*file_size*/, NULL /*open*/,
+      NULL /*create_file*/, NULL /*read*/, NULL /*write*/, NULL /*close*/,
+      NULL /*create_directory*/, NULL /*get_vm_object*/, NULL /*truncate*/,
+      NULL /*stat*/, process_signal, NULL /*connect*/);
+  int rc = vfs_create_fd(0, 0, 0, inode, NULL);
+  assert(rc >= 0);
+  return rc;
+}
+
+int syscall_poll(SYS_POLL_PARAMS *args) {
+  struct pollfd *fds = args->fds;
+  size_t nfds = args->nfds;
+  int timeout = args->timeout;
+  return poll(fds, nfds, timeout);
+}
+
+int syscall_pwrite(int fd, const char *buf, size_t count, size_t offset) {
+  return vfs_pwrite(fd, (char *)buf, count, offset);
+}
+
+void syscall_randomfill(void *buffer, u32 size) {
+  get_random(buffer, size);
+}
+
+size_t syscall_recvfrom(
+    int socket, void *buffer, size_t length, int flags,
+    struct two_args
+        *extra_args /*struct sockaddr *address, socklen_t *address_len*/) {
+
+  struct sockaddr *address = (struct sockaddr *)extra_args->a;
+  socklen_t *address_len = (socklen_t *)extra_args->b;
+
+  if (flags & MSG_WAITALL) {
+    struct pollfd fds[1];
+    fds[0].fd = socket;
+    fds[0].events = POLLIN;
+    poll(fds, 1, 0);
+  }
+
+  u16 data_length;
+  socklen_t tmp_socklen;
+  vfs_pread(socket, &tmp_socklen, sizeof(socklen_t), 0);
+  if (address_len) {
+    *address_len = tmp_socklen;
+  }
+  if (address) {
+    vfs_pread(socket, address, tmp_socklen, 0);
+  } else {
+    // We still have to throwaway the data.
+    char devnull[100];
+    for (; tmp_socklen;) {
+      int rc = vfs_pread(socket, devnull, min(tmp_socklen, 100), 0);
+      assert(rc >= 0);
+      tmp_socklen -= rc;
+    }
+  }
+
+  vfs_pread(socket, &data_length, sizeof(data_length), 0);
+  // If it is reading less than the packet length that could cause
+  // problems as the next read will not be put at a new header. Luckily
+  // it seems as if other UNIX systems can discard the rest of the
+  // packet if not read.
+
+  // Read in the data requested
+  int read_len = min(length, data_length);
+  int rc = vfs_pread(socket, buffer, read_len, 0);
+  // Discard the rest of the packet
+  int rest = data_length - read_len;
+  char devnull[100];
+  for (; rest;) {
+    int rc = vfs_pread(socket, devnull, 100, 0);
+    assert(rc >= 0);
+    rest -= rc;
+  }
+  return rc;
+}
+
+size_t syscall_sendto(int socket, const void *message, size_t length,
+           int flags, struct two_args *extra_args /*
+	   const struct sockaddr *dest_addr,
+           socklen_t dest_len*/) {
+  const struct sockaddr *dest_addr = (const struct sockaddr *)extra_args->a;
+  socklen_t dest_len = (socklen_t)extra_args->b;
+  (void)dest_len;
+  vfs_fd_t *fd = get_vfs_fd(socket, NULL);
+  assert(fd);
+  SOCKET *s = (SOCKET *)fd->inode->internal_object;
+  OPEN_INET_SOCKET *inet = s->child;
+  assert(inet);
+  struct sockaddr_in in;
+  in.sin_addr.s_addr = inet->address;
+  in.sin_port = inet->port;
+  send_udp_packet(&in, (const struct sockaddr_in *)dest_addr, message, length);
+  return length; // FIXME: This is probably not true.
+}
+
+int syscall_shm_open(SYS_SHM_OPEN_PARAMS *args) {
+  return shm_open(args->name, args->oflag, args->mode);
+}
+
+int syscall_sigaction(int sig, const struct sigaction *restrict act,
+                      struct sigaction *restrict oact) {
+  set_signal_handler(sig, act->sa_handler);
+  return 0;
+}
+
+int syscall_socket(SYS_SOCKET_PARAMS *args) {
+  return socket(args->domain, args->type, args->protocol);
+}
+
+u32 syscall_uptime(void) {
+  return (u32)pit_num_ms();
+}
+
+int syscall_write(int fd, const char *buf, size_t count) {
+  vfs_fd_t *fd_ptr = get_vfs_fd(fd, NULL);
+  if (!fd_ptr) {
+    return -EBADF;
+  }
+  int rc = vfs_pwrite(fd, (char *)buf, count, fd_ptr->offset);
+  fd_ptr->offset += rc;
+  return rc;
+}
+
 int syscall_exec(SYS_EXEC_PARAMS *args) {
   const char *filename = copy_and_allocate_user_string(args->path);
 
diff --git a/kernel/cpu/syscall.h b/kernel/cpu/syscall.h
index 3568e51..7dd7229 100644
--- a/kernel/cpu/syscall.h
+++ b/kernel/cpu/syscall.h
@@ -3,57 +3,3 @@
 #include <typedefs.h>
 
 void syscalls_init(void);
-
-typedef struct SYS_OPEN_PARAMS {
-  char *file;
-  int flags;
-  int mode;
-} __attribute__((packed)) SYS_OPEN_PARAMS;
-
-typedef struct SYS_PREAD_PARAMS {
-  int fd;
-  void *buf;
-  size_t count;
-  size_t offset;
-} __attribute__((packed)) SYS_PREAD_PARAMS;
-
-typedef struct SYS_READ_PARAMS {
-  int fd;
-  void *buf;
-  size_t count;
-} __attribute__((packed)) SYS_READ_PARAMS;
-
-typedef struct SYS_PWRITE_PARAMS {
-  int fd;
-  void *buf;
-  size_t count;
-  size_t offset;
-} __attribute__((packed)) SYS_PWRITE_PARAMS;
-
-typedef struct SYS_WRITE_PARAMS {
-  int fd;
-  void *buf;
-  size_t count;
-} __attribute__((packed)) SYS_WRITE_PARAMS;
-
-typedef struct SYS_EXEC_PARAMS {
-  char *path;
-  char **argv;
-} __attribute__((packed)) SYS_EXEC_PARAMS;
-
-typedef struct SYS_WAIT_PARAMS {
-  int *status;
-} __attribute__((packed)) SYS_WAIT_PARAMS;
-
-typedef struct SYS_DUP2_PARAMS {
-  int org_fd;
-  int new_fd;
-} __attribute__((packed)) SYS_DUP2_PARAMS;
-
-typedef struct SYS_OPENPTY_PARAMS {
-  int *amaster;
-  int *aslave;
-  char *name;
-  /*const struct termios*/ void *termp;
-  /*const struct winsize*/ void *winp;
-} __attribute__((packed)) SYS_OPENPTY_PARAMS;
diff --git a/kernel/includes/syscalls.h b/kernel/includes/syscalls.h
deleted file mode 100644
index a7ebfe0..0000000
--- a/kernel/includes/syscalls.h
+++ /dev/null
@@ -1,124 +0,0 @@
-#include <fs/vfs.h>
-#include <signal.h>
-#include <socket.h>
-#include <stddef.h>
-#include <sys/types.h>
-#include <time.h>
-#include <typedefs.h>
-#include <types.h>
-
-void syscall_outw(u16 port, u16 word);
-u32 syscall_inl(u16 port);
-void syscall_outl(u16 port, u32 l);
-int syscall_open_process(int pid);
-
-int syscall_ipc_register_endpoint(u32 endpoint);
-int syscall_ipc_read(u8 *buffer, u32 length, u32 *sender_pid);
-int syscall_ipc_write(int ipc_id, u8 *buffer, u32 length);
-int syscall_ipc_write_to_process(int pid, u8 *buffer, u32 length);
-u32 syscall_map_frames(u32 address, u32 size);
-u32 syscall_virtual_to_physical(u32 virtual);
-int syscall_install_irq(void (*irq_handler)(), u8 irq);
-
-typedef struct SYS_ACCEPT_PARAMS {
-  int socket;
-  struct sockaddr *address;
-  socklen_t *address_len;
-} __attribute__((packed)) SYS_ACCEPT_PARAMS;
-
-int syscall_accept(SYS_ACCEPT_PARAMS *args);
-int syscall_open(const char *file, int flags, mode_t mode);
-int syscall_write(int fd, const char *buf, size_t count);
-int syscall_pwrite(int fd, const char *buf, size_t count, size_t offset);
-
-void syscall_randomfill(void *buffer, u32 size);
-
-typedef struct SYS_BIND_PARAMS {
-  int sockfd;
-  const struct sockaddr *addr;
-  socklen_t addrlen;
-} __attribute__((packed)) SYS_BIND_PARAMS;
-
-int syscall_bind(SYS_BIND_PARAMS *args);
-int syscall_chdir(const char *path);
-
-typedef struct SYS_CLOCK_GETTIME_PARAMS {
-  clockid_t clk;
-  struct timespec *ts;
-} __attribute__((packed)) SYS_CLOCK_GETTIME_PARAMS;
-
-int syscall_clock_gettime(SYS_CLOCK_GETTIME_PARAMS *args);
-int syscall_ftruncate(int fd, size_t length);
-
-char *syscall_getcwd(char *buf, size_t size);
-int syscall_kill(pid_t pid, int sig);
-int syscall_mkdir(const char *path, int mode);
-
-typedef struct SYS_MMAP_PARAMS {
-  void *addr;
-  size_t length;
-  int prot;
-  int flags;
-  int fd;
-  size_t offset;
-} __attribute__((packed)) SYS_MMAP_PARAMS;
-
-void *syscall_mmap(SYS_MMAP_PARAMS *args);
-int syscall_munmap(void *addr, size_t length);
-#ifndef MSLEEP_H
-#define MSLEEP_H
-void syscall_msleep(u32 ms);
-#endif
-
-typedef struct SYS_POLL_PARAMS {
-  struct pollfd *fds;
-  size_t nfds;
-  int timeout;
-} __attribute__((packed)) SYS_POLL_PARAMS;
-
-int syscall_poll(SYS_POLL_PARAMS *args);
-
-struct two_args {
-  u32 a;
-  u32 b;
-};
-
-size_t syscall_recvfrom(
-    int socket, void *buffer, size_t length, int flags,
-    struct two_args
-        *extra_args /*struct sockaddr *address, socklen_t *address_len*/);
-
-struct t_two_args {
-  u32 a;
-  u32 b;
-};
-size_t syscall_sendto(int socket, const void *message, size_t length,
-           int flags, struct t_two_args *extra_args /*
-	   const struct sockaddr *dest_addr,
-           socklen_t dest_len*/);
-#ifndef SYS_SHM_H
-#define SYS_SHM_H
-
-typedef struct SYS_SHM_OPEN_PARAMS {
-  const char *name;
-  int oflag;
-  mode_t mode;
-} __attribute__((packed)) SYS_SHM_OPEN_PARAMS;
-
-int syscall_shm_open(SYS_SHM_OPEN_PARAMS *args);
-#endif
-int syscall_sigaction(int sig, const struct sigaction *restrict act,
-                      struct sigaction *restrict oact);
-
-typedef struct SYS_SOCKET_PARAMS {
-  int domain;
-  int type;
-  int protocol;
-} __attribute__((packed)) SYS_SOCKET_PARAMS;
-
-int syscall_socket(SYS_SOCKET_PARAMS *args);
-
-int syscall_fstat(int fd, struct stat *buf);
-u32 syscall_uptime(void);
-int syscall_isatty(int fd);
-int syscall_lseek(int fd, int offset, int whence);
diff --git a/kernel/syscalls/accept.c b/kernel/syscalls/accept.c
deleted file mode 100644
index 0be578c..0000000
--- a/kernel/syscalls/accept.c
+++ /dev/null
@@ -1,5 +0,0 @@
-#include <syscalls.h>
-
-int syscall_accept(SYS_ACCEPT_PARAMS *args) {
-  return accept(args->socket, args->address, args->address_len);
-}
diff --git a/kernel/syscalls/bind.c b/kernel/syscalls/bind.c
deleted file mode 100644
index 53eeb79..0000000
--- a/kernel/syscalls/bind.c
+++ /dev/null
@@ -1,5 +0,0 @@
-#include <syscalls.h>
-
-int syscall_bind(SYS_BIND_PARAMS *args) {
-  return bind(args->sockfd, args->addr, args->addrlen);
-}
diff --git a/kernel/syscalls/chdir.c b/kernel/syscalls/chdir.c
deleted file mode 100644
index 4b1f714..0000000
--- a/kernel/syscalls/chdir.c
+++ /dev/null
@@ -1,6 +0,0 @@
-#include <fs/vfs.h>
-#include <syscalls.h>
-
-int syscall_chdir(const char *path) {
-  return vfs_chdir(path);
-}
diff --git a/kernel/syscalls/clock_gettime.c b/kernel/syscalls/clock_gettime.c
deleted file mode 100644
index c0696d7..0000000
--- a/kernel/syscalls/clock_gettime.c
+++ /dev/null
@@ -1,10 +0,0 @@
-#include <syscalls.h>
-
-int syscall_clock_gettime(SYS_CLOCK_GETTIME_PARAMS *args) {
-  // FIXME: Actually implement this
-  if (args->ts) {
-    args->ts->tv_sec = 0;
-    args->ts->tv_nsec = 0;
-  }
-  return 0;
-}
diff --git a/kernel/syscalls/fstat.c b/kernel/syscalls/fstat.c
deleted file mode 100644
index 38346e3..0000000
--- a/kernel/syscalls/fstat.c
+++ /dev/null
@@ -1,11 +0,0 @@
-#include <errno.h>
-#include <fs/vfs.h>
-#include <syscalls.h>
-
-int syscall_fstat(int fd, struct stat *buf) {
-  if (!mmu_is_valid_userpointer(buf, sizeof(struct stat))) {
-    return -EPERM; // TODO: Is this correct? The spec says nothing about
-                   // this case.
-  }
-  return vfs_fstat(fd, buf);
-}
diff --git a/kernel/syscalls/ftruncate.c b/kernel/syscalls/ftruncate.c
deleted file mode 100644
index 2ed5ccc..0000000
--- a/kernel/syscalls/ftruncate.c
+++ /dev/null
@@ -1,6 +0,0 @@
-#include <fs/vfs.h>
-#include <syscalls.h>
-
-int syscall_ftruncate(int fd, size_t length) {
-  return vfs_ftruncate(fd, length);
-}
diff --git a/kernel/syscalls/getcwd.c b/kernel/syscalls/getcwd.c
deleted file mode 100644
index bd0025d..0000000
--- a/kernel/syscalls/getcwd.c
+++ /dev/null
@@ -1,10 +0,0 @@
-#include <math.h>
-#include <sched/scheduler.h>
-#include <syscalls.h>
-
-char *syscall_getcwd(char *buf, size_t size) {
-  const char *cwd = current_task->current_working_directory;
-  size_t len = min(size, strlen(cwd));
-  strlcpy(buf, current_task->current_working_directory, len);
-  return buf;
-}
diff --git a/kernel/syscalls/isatty.c b/kernel/syscalls/isatty.c
deleted file mode 100644
index de3e918..0000000
--- a/kernel/syscalls/isatty.c
+++ /dev/null
@@ -1,14 +0,0 @@
-#include <errno.h>
-#include <fs/vfs.h>
-#include <syscalls.h>
-
-int syscall_isatty(int fd) {
-  vfs_fd_t *fd_ptr = get_vfs_fd(fd, NULL);
-  if (!fd_ptr) {
-    return -EBADF;
-  }
-  if (!fd_ptr->is_tty) {
-    return -ENOTTY;
-  }
-  return 1;
-}
diff --git a/kernel/syscalls/kill.c b/kernel/syscalls/kill.c
deleted file mode 100644
index affade6..0000000
--- a/kernel/syscalls/kill.c
+++ /dev/null
@@ -1,15 +0,0 @@
-#include <errno.h>
-#include <sched/scheduler.h>
-#include <signal.h>
-#include <syscalls.h>
-
-int syscall_kill(int fd, int sig) {
-  vfs_fd_t *fd_ptr = get_vfs_fd(fd, NULL);
-  if (!fd_ptr) {
-    return -EBADF;
-  }
-  if (!fd_ptr->inode->send_signal) {
-    return -EBADF;
-  }
-  return process_signal(fd_ptr, sig);
-}
diff --git a/kernel/syscalls/lseek.c b/kernel/syscalls/lseek.c
deleted file mode 100644
index e85d4c1..0000000
--- a/kernel/syscalls/lseek.c
+++ /dev/null
@@ -1,34 +0,0 @@
-#include <assert.h>
-#include <errno.h>
-#include <fs/vfs.h>
-
-// FIXME: These should be in a shared header file with libc
-#define SEEK_SET 0
-#define SEEK_CUR 1
-#define SEEK_END 2
-
-int syscall_lseek(int fd, int offset, int whence) {
-  vfs_fd_t *fd_ptr = get_vfs_fd(fd, NULL);
-  if (!fd_ptr) {
-    return -EBADF;
-  }
-
-  off_t ret_offset = fd_ptr->offset;
-  switch (whence) {
-  case SEEK_SET:
-    ret_offset = offset;
-    break;
-  case SEEK_CUR:
-    ret_offset += offset;
-    break;
-  case SEEK_END:
-    assert(fd_ptr->inode);
-    ret_offset = fd_ptr->inode->file_size + offset;
-    break;
-  default:
-    return -EINVAL;
-    break;
-  }
-  fd_ptr->offset = ret_offset;
-  return ret_offset;
-}
diff --git a/kernel/syscalls/mkdir.c b/kernel/syscalls/mkdir.c
deleted file mode 100644
index e7dec0b..0000000
--- a/kernel/syscalls/mkdir.c
+++ /dev/null
@@ -1,5 +0,0 @@
-#include <syscalls.h>
-
-int syscall_mkdir(const char *path, int mode) {
-  return vfs_mkdir(path, mode);
-}
diff --git a/kernel/syscalls/mmap.c b/kernel/syscalls/mmap.c
deleted file mode 100644
index 85b32a3..0000000
--- a/kernel/syscalls/mmap.c
+++ /dev/null
@@ -1,7 +0,0 @@
-#include <sched/scheduler.h>
-#include <syscalls.h>
-
-void *syscall_mmap(SYS_MMAP_PARAMS *args) {
-  return mmap(args->addr, args->length, args->prot, args->flags, args->fd,
-              args->offset);
-}
diff --git a/kernel/syscalls/msleep.c b/kernel/syscalls/msleep.c
deleted file mode 100644
index d2c8b59..0000000
--- a/kernel/syscalls/msleep.c
+++ /dev/null
@@ -1,9 +0,0 @@
-#include <drivers/pit.h>
-#include <sched/scheduler.h>
-#include <stdio.h>
-#include <syscalls.h>
-
-void syscall_msleep(u32 ms) {
-  current_task->sleep_until = pit_num_ms() + ms;
-  switch_task();
-}
diff --git a/kernel/syscalls/munmap.c b/kernel/syscalls/munmap.c
deleted file mode 100644
index d57ef36..0000000
--- a/kernel/syscalls/munmap.c
+++ /dev/null
@@ -1,6 +0,0 @@
-#include <sched/scheduler.h>
-#include <syscalls.h>
-
-int syscall_munmap(void *addr, size_t length) {
-  return munmap(addr, length);
-}
diff --git a/kernel/syscalls/open.c b/kernel/syscalls/open.c
deleted file mode 100644
index ef5297b..0000000
--- a/kernel/syscalls/open.c
+++ /dev/null
@@ -1,14 +0,0 @@
-#include <errno.h>
-#include <syscalls.h>
-
-int syscall_open(const char *file, int flags, mode_t mode) {
-  const char *_file = copy_and_allocate_user_string(file);
-  if (!_file) {
-    return -EFAULT;
-  }
-  int _flags = flags;
-  int _mode = mode;
-  int rc = vfs_open(_file, _flags, _mode);
-  kfree((void *)_file);
-  return rc;
-}
diff --git a/kernel/syscalls/open_process.c b/kernel/syscalls/open_process.c
deleted file mode 100644
index 2acc744..0000000
--- a/kernel/syscalls/open_process.c
+++ /dev/null
@@ -1,26 +0,0 @@
-#include <assert.h>
-#include <errno.h>
-#include <sched/scheduler.h>
-
-int syscall_open_process(int pid) {
-  // TODO: Permission check
-  process_t *process = (process_t *)ready_queue;
-  for (; process; process = process->next) {
-    if (pid == process->pid) {
-      break;
-    }
-  }
-  if (!process) {
-    return -ESRCH;
-  }
-
-  vfs_inode_t *inode = vfs_create_inode(
-      process->pid, 0 /*type*/, 0 /*has_data*/, 0 /*can_write*/, 1 /*is_open*/,
-      process /*internal_object*/, 0 /*file_size*/, NULL /*open*/,
-      NULL /*create_file*/, NULL /*read*/, NULL /*write*/, NULL /*close*/,
-      NULL /*create_directory*/, NULL /*get_vm_object*/, NULL /*truncate*/,
-      NULL /*stat*/, process_signal, NULL /*connect*/);
-  int rc = vfs_create_fd(0, 0, 0, inode, NULL);
-  assert(rc >= 0);
-  return rc;
-}
diff --git a/kernel/syscalls/ppoll.c b/kernel/syscalls/ppoll.c
deleted file mode 100644
index 7c5befa..0000000
--- a/kernel/syscalls/ppoll.c
+++ /dev/null
@@ -1,11 +0,0 @@
-#include <fs/vfs.h>
-#include <poll.h>
-#include <sched/scheduler.h>
-#include <syscalls.h>
-
-int syscall_poll(SYS_POLL_PARAMS *args) {
-  struct pollfd *fds = args->fds;
-  size_t nfds = args->nfds;
-  int timeout = args->timeout;
-  return poll(fds, nfds, timeout);
-}
diff --git a/kernel/syscalls/pwrite.c b/kernel/syscalls/pwrite.c
deleted file mode 100644
index b251dbb..0000000
--- a/kernel/syscalls/pwrite.c
+++ /dev/null
@@ -1,6 +0,0 @@
-#include <fs/vfs.h>
-#include <syscalls.h>
-
-int syscall_pwrite(int fd, const char *buf, size_t count, size_t offset) {
-  return vfs_pwrite(fd, (char *)buf, count, offset);
-}
diff --git a/kernel/syscalls/randomfill.c b/kernel/syscalls/randomfill.c
deleted file mode 100644
index 3808009..0000000
--- a/kernel/syscalls/randomfill.c
+++ /dev/null
@@ -1,9 +0,0 @@
-#include <random.h>
-#include <typedefs.h>
-
-// This syscall will never fail. At worst a page fault will occur but if
-// the syscall returns the buffer will have been filled with random
-// data.
-void syscall_randomfill(void *buffer, u32 size) {
-  get_random(buffer, size);
-}
diff --git a/kernel/syscalls/recvfrom.c b/kernel/syscalls/recvfrom.c
deleted file mode 100644
index c35dcfa..0000000
--- a/kernel/syscalls/recvfrom.c
+++ /dev/null
@@ -1,60 +0,0 @@
-#include <assert.h>
-#include <fs/vfs.h>
-#include <math.h>
-#include <poll.h>
-#include <syscalls.h>
-
-size_t syscall_recvfrom(
-    int socket, void *buffer, size_t length, int flags,
-    struct two_args
-        *extra_args /*struct sockaddr *address, socklen_t *address_len*/) {
-
-  struct sockaddr *address = (struct sockaddr *)extra_args->a;
-  socklen_t *address_len = (socklen_t *)extra_args->b;
-  kprintf("address: %x\n", address);
-  kprintf("address_len: %x\n", address_len);
-
-  if (flags & MSG_WAITALL) {
-    struct pollfd fds[1];
-    fds[0].fd = socket;
-    fds[0].events = POLLIN;
-    poll(fds, 1, 0);
-  }
-
-  u16 data_length;
-  socklen_t tmp_socklen;
-  vfs_pread(socket, &tmp_socklen, sizeof(socklen_t), 0);
-  if (address_len) {
-    *address_len = tmp_socklen;
-  }
-  if (address) {
-    vfs_pread(socket, address, tmp_socklen, 0);
-  } else {
-    // We still have to throwaway the data.
-    char devnull[100];
-    for (; tmp_socklen;) {
-      int rc = vfs_pread(socket, devnull, min(tmp_socklen, 100), 0);
-      assert(rc >= 0);
-      tmp_socklen -= rc;
-    }
-  }
-
-  vfs_pread(socket, &data_length, sizeof(data_length), 0);
-  // If it is reading less than the packet length that could cause
-  // problems as the next read will not be put at a new header. Luckily
-  // it seems as if other UNIX systems can discard the rest of the
-  // packet if not read.
-
-  // Read in the data requested
-  int read_len = min(length, data_length);
-  int rc = vfs_pread(socket, buffer, read_len, 0);
-  // Discard the rest of the packet
-  int rest = data_length - read_len;
-  char devnull[100];
-  for (; rest;) {
-    int rc = vfs_pread(socket, devnull, 100, 0);
-    assert(rc >= 0);
-    rest -= rc;
-  }
-  return rc;
-}
diff --git a/kernel/syscalls/sendto.c b/kernel/syscalls/sendto.c
deleted file mode 100644
index 678b5b3..0000000
--- a/kernel/syscalls/sendto.c
+++ /dev/null
@@ -1,23 +0,0 @@
-#include <assert.h>
-#include <network/bytes.h>
-#include <network/udp.h>
-#include <syscalls.h>
-
-size_t syscall_sendto(int socket, const void *message, size_t length,
-           int flags, struct t_two_args *extra_args /*
-	   const struct sockaddr *dest_addr,
-           socklen_t dest_len*/) {
-  const struct sockaddr *dest_addr = (const struct sockaddr *)extra_args->a;
-  socklen_t dest_len = (socklen_t)extra_args->b;
-  (void)dest_len;
-  vfs_fd_t *fd = get_vfs_fd(socket, NULL);
-  assert(fd);
-  SOCKET *s = (SOCKET *)fd->inode->internal_object;
-  OPEN_INET_SOCKET *inet = s->child;
-  assert(inet);
-  struct sockaddr_in in;
-  in.sin_addr.s_addr = inet->address;
-  in.sin_port = inet->port;
-  send_udp_packet(&in, (const struct sockaddr_in *)dest_addr, message, length);
-  return length; // FIXME: This is probably not true.
-}
diff --git a/kernel/syscalls/shm.c b/kernel/syscalls/shm.c
deleted file mode 100644
index 39e272e..0000000
--- a/kernel/syscalls/shm.c
+++ /dev/null
@@ -1,6 +0,0 @@
-#include <fs/shm.h>
-#include <syscalls.h>
-
-int syscall_shm_open(SYS_SHM_OPEN_PARAMS *args) {
-  return shm_open(args->name, args->oflag, args->mode);
-}
diff --git a/kernel/syscalls/sigaction.c b/kernel/syscalls/sigaction.c
deleted file mode 100644
index 9882192..0000000
--- a/kernel/syscalls/sigaction.c
+++ /dev/null
@@ -1,9 +0,0 @@
-#include <sched/scheduler.h>
-#include <signal.h>
-#include <syscalls.h>
-
-int syscall_sigaction(int sig, const struct sigaction *restrict act,
-                      struct sigaction *restrict oact) {
-  set_signal_handler(sig, act->sa_handler);
-  return 0;
-}
diff --git a/kernel/syscalls/socket.c b/kernel/syscalls/socket.c
deleted file mode 100644
index b5c8000..0000000
--- a/kernel/syscalls/socket.c
+++ /dev/null
@@ -1,5 +0,0 @@
-#include <syscalls.h>
-
-int syscall_socket(SYS_SOCKET_PARAMS *args) {
-  return socket(args->domain, args->type, args->protocol);
-}
diff --git a/kernel/syscalls/uptime.c b/kernel/syscalls/uptime.c
deleted file mode 100644
index cb32db5..0000000
--- a/kernel/syscalls/uptime.c
+++ /dev/null
@@ -1,6 +0,0 @@
-#include <drivers/pit.h>
-#include <syscalls.h>
-
-u32 syscall_uptime(void) {
-  return (u32)pit_num_ms();
-}
diff --git a/kernel/syscalls/write.c b/kernel/syscalls/write.c
deleted file mode 100644
index f88215d..0000000
--- a/kernel/syscalls/write.c
+++ /dev/null
@@ -1,13 +0,0 @@
-#include <errno.h>
-#include <fs/vfs.h>
-#include <syscalls.h>
-
-int syscall_write(int fd, const char *buf, size_t count) {
-  vfs_fd_t *fd_ptr = get_vfs_fd(fd, NULL);
-  if (!fd_ptr) {
-    return -EBADF;
-  }
-  int rc = vfs_pwrite(fd, (char *)buf, count, fd_ptr->offset);
-  fd_ptr->offset += rc;
-  return rc;
-}
-- 
cgit v1.2.3