meta: move photon/libc to root
This commit is contained in:
33
libc/sys/linux/__fio.h
Normal file
33
libc/sys/linux/__fio.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef SYS_LINUX___FIO_H_
|
||||
#define SYS_LINUX___FIO_H_
|
||||
|
||||
#define __FIO_BUFSZ 512
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct __io_file {
|
||||
char buf[__FIO_BUFSZ];
|
||||
unsigned int buf_idx;
|
||||
unsigned int fd;
|
||||
char err;
|
||||
};
|
||||
|
||||
extern int __fileno(struct __io_file *f);
|
||||
extern void __fio_init();
|
||||
extern unsigned int __fio_read(struct __io_file *f, char *buf, unsigned int sz);
|
||||
extern unsigned int __fio_write(struct __io_file *f, const char *buf, unsigned int sz);
|
||||
extern unsigned int __fio_flush(struct __io_file *f);
|
||||
extern int __fio_error(struct __io_file *f);
|
||||
extern int __fio_ungetc(struct __io_file *f, char c);
|
||||
|
||||
extern int __fio_fopen(const char *path, const char *mode, struct __io_file *out);
|
||||
extern int __fio_fclose(struct __io_file *in);
|
||||
extern int __fio_fdopen(int fd, const char *mode, struct __io_file *out);
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
17
libc/sys/linux/__syscall.h
Normal file
17
libc/sys/linux/__syscall.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef SYS_LINUX___SYSCALL_H_
|
||||
#define SYS_LINUX___SYSCALL_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
extern intptr_t __syscall0(uintptr_t id);
|
||||
extern intptr_t __syscall1(uintptr_t id, uintptr_t p0);
|
||||
extern intptr_t __syscall2(uintptr_t id, uintptr_t p0, uintptr_t p1);
|
||||
extern intptr_t __syscall3(uintptr_t id, uintptr_t p0, uintptr_t p1, uintptr_t p2);
|
||||
extern intptr_t __syscall4(uintptr_t id, uintptr_t p0, uintptr_t p1, uintptr_t p2,
|
||||
uintptr_t p3);
|
||||
extern intptr_t __syscall5(uintptr_t id, uintptr_t p0, uintptr_t p1, uintptr_t p2,
|
||||
uintptr_t p3, uintptr_t p4);
|
||||
extern intptr_t __syscall6(uintptr_t id, uintptr_t p0, uintptr_t p1, uintptr_t p2,
|
||||
uintptr_t p3, uintptr_t p4, uintptr_t p5);
|
||||
|
||||
#endif
|
||||
14
libc/sys/linux/__system.h
Normal file
14
libc/sys/linux/__system.h
Normal file
@@ -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
|
||||
0
libc/sys/linux/config.cmake
Normal file
0
libc/sys/linux/config.cmake
Normal file
56
libc/sys/linux/fcntl.h
Normal file
56
libc/sys/linux/fcntl.h
Normal file
@@ -0,0 +1,56 @@
|
||||
#ifndef SYS_LINUX_FCNTL_H_
|
||||
#define SYS_LINUX_FCNTL_H_
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
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
|
||||
49
libc/sys/linux/fio.c
Normal file
49
libc/sys/linux/fio.c
Normal file
@@ -0,0 +1,49 @@
|
||||
#include "__fio.h"
|
||||
#include "__syscall.h"
|
||||
#include "unistd.h"
|
||||
|
||||
struct __io_file __stdin = { .fd = 0 };
|
||||
struct __io_file __stdout = { .fd = 1 };
|
||||
struct __io_file __stderr = { .fd = 2 };
|
||||
|
||||
struct __io_file *stdin = &__stdin;
|
||||
struct __io_file *stdout = &__stdout;
|
||||
struct __io_file *stderr = &__stderr;
|
||||
|
||||
int __fileno(struct __io_file *f)
|
||||
{
|
||||
return f->fd;
|
||||
}
|
||||
|
||||
unsigned int __fio_write(struct __io_file *f, const char *buf, unsigned int sz)
|
||||
{
|
||||
for (unsigned int i = 0; i < sz; i++) {
|
||||
if (f->buf_idx >= __FIO_BUFSZ) {
|
||||
__fio_flush(f);
|
||||
}
|
||||
|
||||
f->buf[f->buf_idx++] = buf[i];
|
||||
if (buf[i] == '\n') {
|
||||
__fio_flush(f);
|
||||
}
|
||||
}
|
||||
|
||||
return sz;
|
||||
}
|
||||
|
||||
unsigned int __fio_flush(struct __io_file *f)
|
||||
{
|
||||
ssize_t res = write(f->fd, f->buf, f->buf_idx);
|
||||
if (res != f->buf_idx) {
|
||||
f->err = 1;
|
||||
}
|
||||
|
||||
size_t flushed = f->buf_idx;
|
||||
f->buf_idx = 0;
|
||||
return flushed;
|
||||
}
|
||||
|
||||
int __fio_error(struct __io_file *f)
|
||||
{
|
||||
return f->err != 0;
|
||||
}
|
||||
20
libc/sys/linux/machine/x86_64/crt0.s
Normal file
20
libc/sys/linux/machine/x86_64/crt0.s
Normal file
@@ -0,0 +1,20 @@
|
||||
.global _start
|
||||
.type _start, @function
|
||||
|
||||
.extern __fio_init
|
||||
.type __fio_init, @function
|
||||
|
||||
.extern main
|
||||
.type main, @function
|
||||
|
||||
_start:
|
||||
xor %ebp, %ebp
|
||||
mov (%rsp), %edi
|
||||
lea 8(%rsp), %rsi
|
||||
lea 16(%rsp, %rdi, 8), %rdx
|
||||
xor %eax, %eax
|
||||
|
||||
call main
|
||||
movq %rax, %rdi
|
||||
movq $60, %rax
|
||||
syscall
|
||||
108
libc/sys/linux/machine/x86_64/syscall.c
Normal file
108
libc/sys/linux/machine/x86_64/syscall.c
Normal file
@@ -0,0 +1,108 @@
|
||||
#include <stdint.h>
|
||||
|
||||
intptr_t __syscall0(uintptr_t id)
|
||||
{
|
||||
intptr_t ret = 0;
|
||||
asm volatile("mov %1, %%rax; syscall; mov %%rax, %0"
|
||||
: "=m" (ret)
|
||||
: "m" (id));
|
||||
return ret;
|
||||
}
|
||||
|
||||
intptr_t __syscall1(uintptr_t id, uintptr_t p0)
|
||||
{
|
||||
intptr_t ret = 0;
|
||||
asm volatile(
|
||||
"mov %1, %%rax;"
|
||||
"mov %2, %%rdi;"
|
||||
"syscall;"
|
||||
"mov %%rax, %0"
|
||||
: "=m" (ret)
|
||||
: "m" (id), "m" (p0));
|
||||
return ret;
|
||||
}
|
||||
|
||||
intptr_t __syscall2(uintptr_t id, uintptr_t p0, uintptr_t p1)
|
||||
{
|
||||
intptr_t ret = 0;
|
||||
asm volatile(
|
||||
"mov %1, %%rax;"
|
||||
"mov %2, %%rdi;"
|
||||
"mov %3, %%rsi;"
|
||||
"syscall;"
|
||||
"mov %%rax, %0"
|
||||
: "=m" (ret)
|
||||
: "m" (id), "m" (p0), "m" (p1));
|
||||
return ret;
|
||||
}
|
||||
|
||||
intptr_t __syscall3(uintptr_t id, uintptr_t p0, uintptr_t p1, uintptr_t p2)
|
||||
{
|
||||
intptr_t ret = 0;
|
||||
asm volatile(
|
||||
"movq %1, %%rax;"
|
||||
"mov %2, %%rdi;"
|
||||
"mov %3, %%rsi;"
|
||||
"mov %4, %%rdx;"
|
||||
"syscall;"
|
||||
"mov %%rax, %0"
|
||||
: "=m" (ret)
|
||||
: "m" (id), "m" (p0), "m" (p1), "m" (p2));
|
||||
return ret;
|
||||
}
|
||||
|
||||
intptr_t __syscall4(uintptr_t id, uintptr_t p0, uintptr_t p1, uintptr_t p2,
|
||||
uintptr_t p3)
|
||||
{
|
||||
intptr_t ret = 0;
|
||||
asm volatile(
|
||||
"mov %1, %%rax;"
|
||||
"mov %2, %%rdi;"
|
||||
"mov %3, %%rsi;"
|
||||
"mov %4, %%rdx;"
|
||||
"mov %5, %%r10;"
|
||||
"syscall;"
|
||||
"mov %%rax, %0"
|
||||
: "=m" (ret)
|
||||
: "m" (id), "m" (p0), "m" (p1), "m" (p2), "m" (p3));
|
||||
return ret;
|
||||
}
|
||||
|
||||
intptr_t __syscall5(uintptr_t id, uintptr_t p0, uintptr_t p1, uintptr_t p2,
|
||||
uintptr_t p3, uintptr_t p4)
|
||||
{
|
||||
intptr_t ret = 0;
|
||||
asm volatile(
|
||||
"mov %1, %%rax;"
|
||||
"mov %2, %%rdi;"
|
||||
"mov %3, %%rsi;"
|
||||
"mov %4, %%rdx;"
|
||||
"mov %5, %%r10;"
|
||||
"mov %6, %%r8;"
|
||||
"syscall;"
|
||||
"mov %%rax, %0"
|
||||
: "=m" (ret)
|
||||
: "m" (id), "m" (p0), "m" (p1), "m" (p2), "m" (p3), "m" (p4));
|
||||
return ret;
|
||||
}
|
||||
|
||||
intptr_t __syscall6(uintptr_t id, uintptr_t p0, uintptr_t p1, uintptr_t p2,
|
||||
uintptr_t p3, uintptr_t p4, uintptr_t p5)
|
||||
{
|
||||
intptr_t ret = 0;
|
||||
asm volatile(
|
||||
"mov %1, %%rax;"
|
||||
"mov %2, %%rdi;"
|
||||
"mov %3, %%rsi;"
|
||||
"mov %4, %%rdx;"
|
||||
"mov %5, %%r10;"
|
||||
"mov %6, %%r8;"
|
||||
"mov %7, %%r9;"
|
||||
"syscall;"
|
||||
"mov %%rax, %0"
|
||||
: "=m" (ret)
|
||||
: "m" (id), "m" (p0), "m" (p1), "m" (p2), "m" (p3), "m" (p4),
|
||||
"m" (p5));
|
||||
return ret;
|
||||
}
|
||||
|
||||
33
libc/sys/linux/poll.h
Normal file
33
libc/sys/linux/poll.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef SYS_LINUX_POLL_H_
|
||||
#define SYS_LINUX_POLL_H_
|
||||
|
||||
#define POLLIN 0x1
|
||||
#define POLLPRI 0x2
|
||||
#define POLLOUT 0x4
|
||||
#define POLLERR 0x8
|
||||
#define POLLHUP 0x10
|
||||
#define POLLNVAL 0x20
|
||||
#define POLLRDNORM 0x40
|
||||
#define POLLRDBAND 0x80
|
||||
#define POLLWRNORM 0x100
|
||||
#define POLLWRBAND 0x200
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef unsigned long long nfds_t;
|
||||
|
||||
struct pollfd {
|
||||
int fd;
|
||||
short events;
|
||||
short revents;
|
||||
};
|
||||
|
||||
extern int poll(struct pollfd *fds, nfds_t nfds, int timeout);
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
22
libc/sys/linux/sbrk.c
Normal file
22
libc/sys/linux/sbrk.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#include "unistd.h"
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
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;
|
||||
}
|
||||
39
libc/sys/linux/sys/_errno.h
Normal file
39
libc/sys/linux/sys/_errno.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#ifndef SYS_MAGENTA_SYS__ERRNO_H_
|
||||
#define SYS_MAGENTA_SYS__ERRNO_H_
|
||||
|
||||
#define EPERM 1 /* Operation not permitted */
|
||||
#define ENOENT 2 /* No such file or directory */
|
||||
#define ESRCH 3 /* No such process */
|
||||
#define EINTR 4 /* Interrupted system call */
|
||||
#define EIO 5 /* I/O error */
|
||||
#define ENXIO 6 /* No such device or address */
|
||||
#define E2BIG 7 /* Argument list too long */
|
||||
#define ENOEXEC 8 /* Exec format error */
|
||||
#define EBADF 9 /* Bad file number */
|
||||
#define ECHILD 10 /* No child processes */
|
||||
#define EAGAIN 11 /* Try again */
|
||||
#define ENOMEM 12 /* Out of memory */
|
||||
#define EACCES 13 /* Permission denied */
|
||||
#define EFAULT 14 /* Bad address */
|
||||
#define ENOTBLK 15 /* Block device required */
|
||||
#define EBUSY 16 /* Device or resource busy */
|
||||
#define EEXIST 17 /* File exists */
|
||||
#define EXDEV 18 /* Cross-device link */
|
||||
#define ENODEV 19 /* No such device */
|
||||
#define ENOTDIR 20 /* Not a directory */
|
||||
#define EISDIR 21 /* Is a directory */
|
||||
#define EINVAL 22 /* Invalid argument */
|
||||
#define ENFILE 23 /* File table overflow */
|
||||
#define EMFILE 24 /* Too many open files */
|
||||
#define ENOTTY 25 /* Not a typewriter */
|
||||
#define ETXTBSY 26 /* Text file busy */
|
||||
#define EFBIG 27 /* File too large */
|
||||
#define ENOSPC 28 /* No space left on device */
|
||||
#define ESPIPE 29 /* Illegal seek */
|
||||
#define EROFS 30 /* Read-only file system */
|
||||
#define EMLINK 31 /* Too many links */
|
||||
#define EPIPE 32 /* Broken pipe */
|
||||
#define EDOM 33 /* Math argument out of domain of func */
|
||||
#define ERANGE 34 /* Math result not representable */
|
||||
|
||||
#endif
|
||||
10
libc/sys/linux/sys/_fconst.h
Normal file
10
libc/sys/linux/sys/_fconst.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef SYS_LINUX_SYS__FCONST_H_
|
||||
#define SYS_LINUX_SYS__FCONST_H_
|
||||
|
||||
#define __FILENAME_MAX 1024
|
||||
|
||||
#define __SEEK_SET 0
|
||||
#define __SEEK_CUR 1
|
||||
#define __SEEK_END 2
|
||||
|
||||
#endif
|
||||
131
libc/sys/linux/sys/mman.h
Normal file
131
libc/sys/linux/sys/mman.h
Normal file
@@ -0,0 +1,131 @@
|
||||
#ifndef SYS_LINUX_SYS_MMAN_H_
|
||||
#define SYS_LINUX_SYS_MMAN_H_
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#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
|
||||
85
libc/sys/linux/sys/param.h
Normal file
85
libc/sys/linux/sys/param.h
Normal file
@@ -0,0 +1,85 @@
|
||||
#ifndef SYS_LINUX_SYS_PARAM_H_
|
||||
#define SYS_LINUX_SYS_PARAM_H_
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#ifndef ARG_MAX
|
||||
# define __undef_ARG_MAX
|
||||
#endif
|
||||
|
||||
#include <linux/limits.h>
|
||||
#include <linux/param.h>
|
||||
|
||||
#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
|
||||
24
libc/sys/linux/sys/stat.h
Normal file
24
libc/sys/linux/sys/stat.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef SYS_LINUX_SYS_STAT_H_
|
||||
#define SYS_LINUX_SYS_STAT_H_
|
||||
|
||||
#include <time.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
struct stat {
|
||||
dev_t st_dev;
|
||||
ino_t st_ino;
|
||||
mode_t st_mode;
|
||||
nlink_t st_nlink;
|
||||
uid_t st_uid;
|
||||
gid_t st_gid;
|
||||
dev_t st_rdev;
|
||||
off_t st_size;
|
||||
blksize_t st_blksize;
|
||||
blkcnt_t st_blocks;
|
||||
|
||||
struct timespec st_atim;
|
||||
struct timespec st_mtim;
|
||||
struct timespec st_ctim;
|
||||
};
|
||||
|
||||
#endif
|
||||
24
libc/sys/linux/sys/syscall.h
Normal file
24
libc/sys/linux/sys/syscall.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef SYS_LINUX_SYSCALL_H_
|
||||
#define SYS_LINUX_SYSCALL_H_
|
||||
|
||||
#define SYS_read 0
|
||||
#define SYS_write 1
|
||||
#define SYS_open 2
|
||||
#define SYS_close 3
|
||||
#define SYS_stat 4
|
||||
#define SYS_fstat 5
|
||||
#define SYS_lstat 6
|
||||
#define SYS_poll 7
|
||||
#define SYS_lseek 8
|
||||
#define SYS_mmap 9
|
||||
#define SYS_mprotect 10
|
||||
#define SYS_munmap 11
|
||||
#define SYS_brk 12
|
||||
#define SYS_rt_sigaction 13
|
||||
#define SYS_rt_sigprocmask 14
|
||||
#define SYS_rt_sigreturn 15
|
||||
#define SYS_ioctl 16
|
||||
#define SYS_mremap 25
|
||||
#define SYS__exit 60
|
||||
|
||||
#endif
|
||||
32
libc/sys/linux/sys/types.h
Normal file
32
libc/sys/linux/sys/types.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef SYS_LINUX_SYS_TYPES_H_
|
||||
#define SYS_LINUX_SYS_TYPES_H_
|
||||
|
||||
#include <machine/_stdint.h>
|
||||
|
||||
typedef signed long long blkcnt_t;
|
||||
typedef signed long long blksize_t;
|
||||
typedef unsigned long long clock_t;
|
||||
typedef unsigned int clockid_t;
|
||||
typedef unsigned long long fsblkcnt_t;
|
||||
typedef unsigned long long fsfilcnt_t;
|
||||
typedef unsigned int gid_t;
|
||||
typedef unsigned int id_t;
|
||||
typedef unsigned long long ino_t;
|
||||
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;
|
||||
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
|
||||
164
libc/sys/linux/syscall.c
Normal file
164
libc/sys/linux/syscall.c
Normal file
@@ -0,0 +1,164 @@
|
||||
#include "poll.h"
|
||||
#include "sys/syscall.h"
|
||||
#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,
|
||||
(uintptr_t)count);
|
||||
if (res < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (ssize_t)res;
|
||||
}
|
||||
|
||||
ssize_t write(int fd, const void *buf, size_t count)
|
||||
{
|
||||
intptr_t res = __syscall3(SYS_write, (uintptr_t)fd, (uintptr_t)buf,
|
||||
(uintptr_t)count);
|
||||
if (res < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (ssize_t)res;
|
||||
}
|
||||
|
||||
int open(const char *pathname, int flags)
|
||||
{
|
||||
intptr_t res = __syscall3(SYS_open, (uintptr_t)pathname,
|
||||
(uintptr_t)flags, 0);
|
||||
if (res < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (int)res;
|
||||
}
|
||||
|
||||
int close(int fd)
|
||||
{
|
||||
intptr_t res = __syscall1(SYS_close, (uintptr_t)fd);
|
||||
if (res < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (int)res;
|
||||
}
|
||||
|
||||
int stat(const char *pathname, struct stat *statbuf)
|
||||
{
|
||||
intptr_t res = __syscall2(SYS_stat, (uintptr_t)pathname,
|
||||
(uintptr_t)statbuf);
|
||||
if (res < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (int)res;
|
||||
}
|
||||
|
||||
int fstat(int fd, struct stat *statbuf)
|
||||
{
|
||||
intptr_t res = __syscall2(SYS_fstat, (uintptr_t)fd,
|
||||
(uintptr_t)statbuf);
|
||||
if (res < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (int)res;
|
||||
}
|
||||
|
||||
int lstat(const char *pathname, struct stat *statbuf)
|
||||
{
|
||||
intptr_t res = __syscall2(SYS_lstat, (uintptr_t)pathname,
|
||||
(uintptr_t)statbuf);
|
||||
if (res < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (int)res;
|
||||
}
|
||||
|
||||
int poll(struct pollfd *fds, nfds_t nfds, int timeout)
|
||||
{
|
||||
intptr_t res = __syscall3(SYS_poll, (uintptr_t)fds,
|
||||
(uintptr_t)nfds, (uintptr_t)timeout);
|
||||
if (res < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (int)res;
|
||||
}
|
||||
|
||||
off_t lseek(int fd, off_t offset, int whence)
|
||||
{
|
||||
intptr_t res = __syscall3(SYS_lseek, (uintptr_t)fd,
|
||||
(uintptr_t)offset, (uintptr_t)whence);
|
||||
if (res < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (int)res;
|
||||
}
|
||||
|
||||
void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t off)
|
||||
{
|
||||
intptr_t res = __syscall6(SYS_mmap, (uintptr_t)addr, (uintptr_t)len,
|
||||
(uintptr_t)prot, (uintptr_t)flags, (uintptr_t)fd, (uintptr_t)off);
|
||||
if (res < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return (void *)res;
|
||||
}
|
||||
|
||||
int mprotect(void *addr, size_t len, int prot)
|
||||
{
|
||||
intptr_t res = __syscall3(SYS_mprotect, (uintptr_t)addr, (uintptr_t)len,
|
||||
(uintptr_t)prot);
|
||||
if (res < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (int)res;
|
||||
}
|
||||
|
||||
int munmap(void *addr, size_t length)
|
||||
{
|
||||
intptr_t res = __syscall2(SYS_munmap, (uintptr_t)addr, (uintptr_t)length);
|
||||
if (res < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (int)res;
|
||||
}
|
||||
|
||||
int brk(void *addr)
|
||||
{
|
||||
intptr_t res = __syscall1(SYS_brk, (uintptr_t)addr);
|
||||
if (res < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
__curr_brk = (void *)res;
|
||||
|
||||
return (int)res;
|
||||
}
|
||||
|
||||
int ioctl(int fd, unsigned long req, void *arg)
|
||||
{
|
||||
intptr_t res = __syscall3(SYS_ioctl, (uintptr_t)fd, (uintptr_t)req,
|
||||
(uintptr_t)arg);
|
||||
if (res < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (int)res;
|
||||
}
|
||||
|
||||
void _exit(int code)
|
||||
{
|
||||
__syscall1(SYS__exit, code);
|
||||
}
|
||||
7
libc/sys/linux/system.c
Normal file
7
libc/sys/linux/system.c
Normal file
@@ -0,0 +1,7 @@
|
||||
#include "unistd.h"
|
||||
|
||||
_Noreturn void __exit(int code)
|
||||
{
|
||||
_exit(code);
|
||||
while (1) {}
|
||||
}
|
||||
30
libc/sys/linux/unistd.h
Normal file
30
libc/sys/linux/unistd.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef SYS_LINUX_UNISTD_H_
|
||||
#define SYS_LINUX_UNISTD_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
struct stat;
|
||||
struct pollfd;
|
||||
|
||||
typedef __intptr_t ssize_t;
|
||||
|
||||
extern ssize_t read(int fd, void *buf, size_t count);
|
||||
extern ssize_t write(int fd, const void *buf, size_t count);
|
||||
extern int open(const char *pathname, int flags);
|
||||
extern int close(int fd);
|
||||
extern int stat(const char *pathname, struct stat *statbuf);
|
||||
extern int fstat(int fd, struct stat *statbuf);
|
||||
extern int lstat(const char *pathname, struct stat *statbuf);
|
||||
extern off_t lseek(int fd, off_t offset, int whence);
|
||||
extern void *mmap(void *addr, size_t length, int prot, int flags,
|
||||
int fd, off_t offset);
|
||||
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
|
||||
Reference in New Issue
Block a user