From 8e2953292e2d7ca9e9b1385896b814f8d144fc35 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Fri, 17 Apr 2020 11:19:30 +0100 Subject: [PATCH] Implemented lots of Linux-specific headers --- photon/libc/sys/linux/__system.h | 14 +++ photon/libc/sys/linux/fcntl.h | 56 ++++++++++++ photon/libc/sys/linux/sbrk.c | 22 +++++ photon/libc/sys/linux/sys/mman.h | 131 ++++++++++++++++++++++++++++ photon/libc/sys/linux/sys/param.h | 85 ++++++++++++++++++ photon/libc/sys/linux/sys/syscall.h | 2 + photon/libc/sys/linux/sys/types.h | 8 ++ photon/libc/sys/linux/syscall.c | 9 ++ photon/libc/sys/linux/system.c | 7 ++ photon/libc/sys/linux/unistd.h | 3 + 10 files changed, 337 insertions(+) create mode 100644 photon/libc/sys/linux/__system.h create mode 100644 photon/libc/sys/linux/fcntl.h create mode 100644 photon/libc/sys/linux/sbrk.c create mode 100644 photon/libc/sys/linux/sys/mman.h create mode 100644 photon/libc/sys/linux/sys/param.h create mode 100644 photon/libc/sys/linux/system.c diff --git a/photon/libc/sys/linux/__system.h b/photon/libc/sys/linux/__system.h new file mode 100644 index 0000000..e93ba18 --- /dev/null +++ b/photon/libc/sys/linux/__system.h @@ -0,0 +1,14 @@ +#ifndef SYS_LINUX___SYSTEM_H_ +#define SYS_LINUX___SYSTEM_H_ + +#if defined(__cplusplus) +extern "C" { +#endif + +extern _Noreturn void __exit(int code); + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/photon/libc/sys/linux/fcntl.h b/photon/libc/sys/linux/fcntl.h new file mode 100644 index 0000000..0be1c7a --- /dev/null +++ b/photon/libc/sys/linux/fcntl.h @@ -0,0 +1,56 @@ +#ifndef SYS_LINUX_FCNTL_H_ +#define SYS_LINUX_FCNTL_H_ + +#include + +struct flock +{ + short int l_type; /* Type of lock: F_RDLCK, F_WRLCK, or F_UNLCK. */ + short int l_whence; /* Where `l_start' is relative to (like `lseek'). */ + off_t l_start; /* Offset where the lock begins. */ + off_t l_len; /* Size of the locked area; zero means until EOF. */ + pid_t l_pid; /* Process holding the lock. */ +}; + +struct flock64 +{ + short int l_type; /* Type of lock: F_RDLCK, F_WRLCK, or F_UNLCK. */ + short int l_whence; /* Where `l_start' is relative to (like `lseek'). */ + off64_t l_start; /* Offset where the lock begins. */ + off64_t l_len; /* Size of the locked area; zero means until EOF. */ + pid_t l_pid; /* Process holding the lock. */ +}; + +#define O_ACCMODE 0003 +#define O_RDONLY 00 +#define O_WRONLY 01 +#define O_RDWR 02 +#define O_CREAT 0100 /* Not fcntl. */ +#define O_EXCL 0200 /* Not fcntl. */ +#define O_NOCTTY 0400 /* Not fcntl. */ +#define O_TRUNC 01000 /* Not fcntl. */ +#define O_APPEND 02000 +#define O_NONBLOCK 04000 +#define O_NDELAY O_NONBLOCK +#define O_SYNC 04010000 +#define O_FSYNC O_SYNC +#define O_ASYNC 020000 +#define O_LARGEFILE 0100000 + +#define O_DIRECTORY 0200000 +#define O_NOFOLLOW 0400000 +#define O_CLOEXEC 02000000 +#define O_DIRECT 040000 +#define O_NOATIME 01000000 +#define O_PATH 010000000 +#define O_DSYNC 010000 +#define O_TMPFILE (020000000 | __O_DIRECTORY) + +#define F_GETLK 5 /* Get record locking info. */ +#define F_SETLK 6 /* Set record locking info (non-blocking). */ +#define F_SETLKW 7 /* Set record locking info (blocking). */ +#define F_GETLK64 12 /* Get record locking info. */ +#define F_SETLK64 13 /* Set record locking info (non-blocking). */ +#define F_SETLKW64 14 /* Set record locking info (blocking). */ + +#endif diff --git a/photon/libc/sys/linux/sbrk.c b/photon/libc/sys/linux/sbrk.c new file mode 100644 index 0000000..bedec70 --- /dev/null +++ b/photon/libc/sys/linux/sbrk.c @@ -0,0 +1,22 @@ +#include "unistd.h" +#include +#include + +void *__curr_brk = NULL; + +void *sbrk(intptr_t increment) +{ + if (!__curr_brk) { + brk(NULL); + } + + uintptr_t end = (uintptr_t)__curr_brk + increment; + void *start = __curr_brk; + int res = brk((void *)end); + + if (res == -1) { + return NULL; + } + + return start; +} diff --git a/photon/libc/sys/linux/sys/mman.h b/photon/libc/sys/linux/sys/mman.h new file mode 100644 index 0000000..bfe9674 --- /dev/null +++ b/photon/libc/sys/linux/sys/mman.h @@ -0,0 +1,131 @@ +#ifndef SYS_LINUX_SYS_MMAN_H_ +#define SYS_LINUX_SYS_MMAN_H_ + +#include +#include + +#define MAP_GROWSDOWN 0x00100 +#define MAP_DENYWRITE 0x00800 +#define MAP_EXECUTABLE 0x01000 +#define MAP_LOCKED 0x02000 +#define MAP_NORESERVE 0x04000 +#define MAP_POPULATE 0x08000 +#define MAP_NONBLOCK 0x10000 +#define MAP_STACK 0x20000 +#define MAP_HUGETLB 0x40000 +#define MAP_SYNC 0x80000 +#define MAP_FIXED_NOREPLACE 0x100000 + +#define PROT_READ 0x1 +#define PROT_WRITE 0x2 +#define PROT_EXEC 0x4 +#define PROT_NONE 0x0 +#define PROT_GROWSDOWN 0x01000000 +#define PROT_GROWSUP 0x02000000 + +#define MAP_SHARED 0x01 +#define MAP_PRIVATE 0x02 +#define MAP_SHARED_VALIDATE 0x03 +#define MAP_TYPE 0x0f + +#define MAP_FIXED 0x10 +#define MAP_FILE 0 +#define MAP_ANONYMOUS 0x20 +#define MAP_ANON MAP_ANONYMOUS +#define MAP_HUGE_SHIFT 26 +#define MAP_HUGE_MASK 0x3f + +#define MS_ASYNC 1 +#define MS_SYNC 4 +#define MS_INVALIDATE 2 + +#define MADV_NORMAL 0 +#define MADV_RANDOM 1 +#define MADV_SEQUENTIAL 2 +#define MADV_WILLNEED 3 +#define MADV_DONTNEED 4 +#define MADV_FREE 8 +#define MADV_REMOVE 9 +#define MADV_DONTFORK 10 +#define MADV_DOFORK 11 +#define MADV_MERGEABLE 12 +#define MADV_UNMERGEABLE 13 +#define MADV_HUGEPAGE 14 +#define MADV_NOHUGEPAGE 15 +#define MADV_DONTDUMP 16 +#define MADV_DODUMP 17 +#define MADV_WIPEONFORK 18 +#define MADV_KEEPONFORK 19 +#define MADV_COLD 20 +#define MADV_PAGEOUT 21 +#define MADV_HWPOISON 100 + +#define MCL_CURRENT 1 +#define MCL_FUTURE 2 +#define MCL_ONFAULT 4 +#define PROT_READ 0x1 +#define PROT_WRITE 0x2 +#define PROT_EXEC 0x4 +#define PROT_NONE 0x0 +#define PROT_GROWSDOWN 0x01000000 +#define PROT_GROWSUP 0x02000000 + +#define MAP_SHARED 0x01 +#define MAP_PRIVATE 0x02 +#define MAP_SHARED_VALIDATE 0x03 +#define MAP_TYPE 0x0f + +#define MAP_FIXED 0x10 +#define MAP_FILE 0 +#define MAP_ANONYMOUS 0x20 +#define MAP_ANON MAP_ANONYMOUS +#define MAP_HUGE_SHIFT 26 +#define MAP_HUGE_MASK 0x3f + +#define MS_ASYNC 1 +#define MS_SYNC 4 +#define MS_INVALIDATE 2 + +#define MADV_NORMAL 0 +#define MADV_RANDOM 1 +#define MADV_SEQUENTIAL 2 +#define MADV_WILLNEED 3 +#define MADV_DONTNEED 4 +#define MADV_FREE 8 +#define MADV_REMOVE 9 +#define MADV_DONTFORK 10 +#define MADV_DOFORK 11 +#define MADV_MERGEABLE 12 +#define MADV_UNMERGEABLE 13 +#define MADV_HUGEPAGE 14 +#define MADV_NOHUGEPAGE 15 +#define MADV_DONTDUMP 16 +#define MADV_DODUMP 17 +#define MADV_WIPEONFORK 18 +#define MADV_KEEPONFORK 19 +#define MADV_COLD 20 +#define MADV_PAGEOUT 21 +#define MADV_HWPOISON 100 + +#define MCL_CURRENT 1 +#define MCL_FUTURE 2 +#define MCL_ONFAULT 4 + +#define MAP_FAILED ((void *) -1) + +extern void *mmap (void *addr, size_t len, int prot, + int flags, int fd, off_t offset); + +extern int munmap (void *addr, size_t len); +extern int mprotect (void *addr, size_t len, int prot) ; +extern int msync (void *addr, size_t len, int flags); + +extern int madvise (void *addr, size_t len, int advice) ; +extern int mlock (const void *addr, size_t len) ; +extern int munlock (const void *addr, size_t len) ; +extern int mlockall (int flags) ; +extern int munlockall (void) ; + +extern int mincore (void *start, size_t len, unsigned char *vec); + +#endif diff --git a/photon/libc/sys/linux/sys/param.h b/photon/libc/sys/linux/sys/param.h new file mode 100644 index 0000000..780fb5e --- /dev/null +++ b/photon/libc/sys/linux/sys/param.h @@ -0,0 +1,85 @@ +#ifndef SYS_LINUX_SYS_PARAM_H_ +#define SYS_LINUX_SYS_PARAM_H_ + +#include + +#include + +#ifndef ARG_MAX +# define __undef_ARG_MAX +#endif + +#include +#include + +#ifdef __undef_ARG_MAX +#undef ARG_MAX +#undef __undef_ARG_MAX +#endif + +#define MAXSYMLINKS 20 + +#define NOFILE 256 +#define NCARGS 131072 + +#define NBBY CHAR_BIT + +#if !defined NGROUPS && defined NGROUPS_MAX +#define NGROUPS NGROUPS_MAX +#endif +#if !defined MAXSYMLINKS && defined SYMLOOP_MAX +#define MAXSYMLINKS SYMLOOP_MAX +#endif +#if !defined CANBSIZ && defined MAX_CANON +#define CANBSIZ MAX_CANON +#endif +#if !defined MAXPATHLEN && defined PATH_MAX +#define MAXPATHLEN PATH_MAX +#endif +#if !defined NOFILE && defined OPEN_MAX +#define NOFILE OPEN_MAX +#endif +#if !defined MAXHOSTNAMELEN && defined HOST_NAME_MAX +#define MAXHOSTNAMELEN HOST_NAME_MAX +#endif +#ifndef NCARGS +#ifdef ARG_MAX +#define NCARGS ARG_MAX +#else +#define NCARGS INT_MAX +#endif +#endif + +#ifndef NOGROUP +#define NOGROUP 65535 /* Marker for empty group set member. */ +#endif +#ifndef NODEV +#define NODEV ((dev_t) -1) /* Non-existent device. */ +#endif + +#ifndef DEV_BSIZE +#define DEV_BSIZE 512 +#endif + +#define setbit(a,i) ((a)[(i)/NBBY] |= 1<<((i)%NBBY)) +#define clrbit(a,i) ((a)[(i)/NBBY] &= ~(1<<((i)%NBBY))) +#define isset(a,i) ((a)[(i)/NBBY] & (1<<((i)%NBBY))) +#define isclr(a,i) (((a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0) + +#ifndef howmany +#define howmany(x, y) (((x) + ((y) - 1)) / (y)) +#endif +#ifdef __GNUC__ +#define roundup(x, y) (__builtin_constant_p (y) && powerof2 (y) \ + ? (((x) + (y) - 1) & ~((y) - 1)) \ + : ((((x) + ((y) - 1)) / (y)) * (y))) +#else +#define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) +#endif +#define powerof2(x) ((((x) - 1) & (x)) == 0) + +/* Macros for min/max. */ +#define MIN(a,b) (((a)<(b))?(a):(b)) +#define MAX(a,b) (((a)>(b))?(a):(b)) + +#endif diff --git a/photon/libc/sys/linux/sys/syscall.h b/photon/libc/sys/linux/sys/syscall.h index 02511cd..764395f 100644 --- a/photon/libc/sys/linux/sys/syscall.h +++ b/photon/libc/sys/linux/sys/syscall.h @@ -18,5 +18,7 @@ #define SYS_rt_sigprocmask 14 #define SYS_rt_sigreturn 15 #define SYS_ioctl 16 +#define SYS_mremap 25 +#define SYS__exit 60 #endif diff --git a/photon/libc/sys/linux/sys/types.h b/photon/libc/sys/linux/sys/types.h index 2676d28..d0fcbfb 100644 --- a/photon/libc/sys/linux/sys/types.h +++ b/photon/libc/sys/linux/sys/types.h @@ -1,6 +1,8 @@ #ifndef SYS_LINUX_SYS_TYPES_H_ #define SYS_LINUX_SYS_TYPES_H_ +#include + typedef signed long long blkcnt_t; typedef signed long long blksize_t; typedef unsigned long long clock_t; @@ -14,6 +16,7 @@ typedef unsigned int key_t; typedef unsigned int mode_t; typedef unsigned long long nlink_t; typedef signed long long off_t; +typedef __int64_t off64_t; typedef signed int pid_t; typedef unsigned long long time_t; typedef unsigned int timer_t; @@ -21,4 +24,9 @@ typedef unsigned int uid_t; typedef unsigned long useconds_t; typedef unsigned long long dev_t; +#ifndef __SIZE_T_DEFINED +typedef __uintptr_t size_t; +#define __SIZE_T_DEFINED +#endif + #endif diff --git a/photon/libc/sys/linux/syscall.c b/photon/libc/sys/linux/syscall.c index d696e3a..c7e21a9 100644 --- a/photon/libc/sys/linux/syscall.c +++ b/photon/libc/sys/linux/syscall.c @@ -3,6 +3,8 @@ #include "__syscall.h" #include "unistd.h" +extern void *__curr_brk; + ssize_t read(int fd, void *buf, size_t count) { intptr_t res = __syscall3(SYS_read, (uintptr_t)fd, (uintptr_t)buf, @@ -140,6 +142,8 @@ int brk(void *addr) return -1; } + __curr_brk = (void *)res; + return (int)res; } @@ -153,3 +157,8 @@ int ioctl(int fd, unsigned long req, void *arg) return (int)res; } + +void _exit(int code) +{ + __syscall1(SYS__exit, code); +} diff --git a/photon/libc/sys/linux/system.c b/photon/libc/sys/linux/system.c new file mode 100644 index 0000000..d9025ad --- /dev/null +++ b/photon/libc/sys/linux/system.c @@ -0,0 +1,7 @@ +#include "unistd.h" + +_Noreturn void __exit(int code) +{ + _exit(code); + while (1) {} +} diff --git a/photon/libc/sys/linux/unistd.h b/photon/libc/sys/linux/unistd.h index 18310e1..1b19572 100644 --- a/photon/libc/sys/linux/unistd.h +++ b/photon/libc/sys/linux/unistd.h @@ -2,6 +2,7 @@ #define SYS_LINUX_UNISTD_H_ #include +#include #include struct stat; @@ -22,6 +23,8 @@ extern void *mmap(void *addr, size_t length, int prot, int flags, extern int mprotect(void *addr, size_t len, int prot); extern int munmap(void *addr, size_t length); extern int brk(void *addr); +extern void *sbrk(intptr_t increment); extern int ioctl(int fd, unsigned long request, void *arg); +extern void _exit(int code); #endif