diff --git a/CMakeLists.txt b/CMakeLists.txt index 364ec16..84457e5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.5) -project(photon C ASM-ATT) +#project(photon C ASM-ATT) macro(subdirlist result curdir) file(GLOB children RELATIVE ${curdir} ${curdir}/*) @@ -13,7 +13,7 @@ macro(subdirlist result curdir) endmacro() include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/platform.cmake) -platform_config(${TARGET}) +platform_config(${PHOTON_TARGET}) message(STATUS "Target: ${machine}-${platform}") @@ -40,6 +40,7 @@ file(GLOB photon_libc_crt ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/sys/${platform}/machine/${machine}/crt*.s) add_library(crt OBJECT ${photon_libc_crt}) +target_compile_options(crt PRIVATE -c) file(GLOB platform_sources ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/sys/${platform}/*.c @@ -57,7 +58,12 @@ file(GLOB platform_headers set(photon_libc_sources ${photon_libc_sources} ${malloc_sources} ${platform_sources}) set(photon_libc_headers ${photon_libc_headers} ${platform_headers}) -add_library(c SHARED ${photon_libc_sources} ${photon_libc_headers}) +if (PHOTON_STATIC EQUAL 1) + add_library(c STATIC ${photon_libc_sources} ${photon_libc_headers}) +else () + add_library(c SHARED ${photon_libc_sources} ${photon_libc_headers}) +endif () + target_compile_options(c PRIVATE -ffreestanding -nostdlib) target_link_libraries(c crt) @@ -65,17 +71,19 @@ target_include_directories(c PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/inc target_include_directories(c PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/sys/${platform}/) target_include_directories(c PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/sysroot/usr/include) -add_custom_target(sysroot +add_custom_target(local-root DEPENDS ${photon_libc_headers} COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/scripts/create-sysroot.sh ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} ${platform} ${machine}) -add_dependencies(c sysroot) +add_dependencies(c local-root) if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") target_link_libraries(c gcc) endif () -add_subdirectory(tests) +if (PHOTON_TESTS EQUAL 1) + add_subdirectory(tests) +endif () diff --git a/photon/libc/stdlib/dlmalloc/malloc.c b/photon/libc/stdlib/dlmalloc/malloc.c index 5e2a87b..d38f1c6 100644 --- a/photon/libc/stdlib/dlmalloc/malloc.c +++ b/photon/libc/stdlib/dlmalloc/malloc.c @@ -231,6 +231,14 @@ /* #define WIN32 */ +#ifdef __magenta__ +#define LACKS_UNISTD_H +#define LACKS_SYS_PARAM_H +#define LACKS_SYS_MMAN_H + +#define HAVE_MMAP 0 +#endif + #ifdef WIN32 #define WIN32_LEAN_AND_MEAN diff --git a/photon/libc/sys/magenta/__fio.h b/photon/libc/sys/magenta/__fio.h new file mode 100644 index 0000000..d99db9f --- /dev/null +++ b/photon/libc/sys/magenta/__fio.h @@ -0,0 +1,28 @@ +#ifndef SYS_LINUX___FIO_H_ +#define SYS_LINUX___FIO_H_ + +#include + +#define __FIO_BUFSZ 512 + +#if defined(__cplusplus) +extern "C" { +#endif + +struct __io_file { + char buf[__FIO_BUFSZ]; + unsigned int buf_idx; + mx_handle_t handle; + char err; +}; + +extern int __fileno(struct __io_file *f); +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); + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/photon/libc/sys/magenta/__syscall.h b/photon/libc/sys/magenta/__syscall.h new file mode 100644 index 0000000..49121c6 --- /dev/null +++ b/photon/libc/sys/magenta/__syscall.h @@ -0,0 +1,17 @@ +#ifndef SYS_LINUX___SYSCALL_H_ +#define SYS_LINUX___SYSCALL_H_ + +#include + +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 diff --git a/photon/libc/sys/magenta/__system.h b/photon/libc/sys/magenta/__system.h new file mode 100644 index 0000000..e93ba18 --- /dev/null +++ b/photon/libc/sys/magenta/__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/magenta/fio.c b/photon/libc/sys/magenta/fio.c new file mode 100644 index 0000000..c85b78b --- /dev/null +++ b/photon/libc/sys/magenta/fio.c @@ -0,0 +1,53 @@ +#include "__fio.h" +#include "__syscall.h" +#include "unistd.h" + +struct __io_file __stdin = {}; +struct __io_file __stdout = {}; +struct __io_file __stderr = {}; + +struct __io_file *stdin = &__stdin; +struct __io_file *stdout = &__stdout; +struct __io_file *stderr = &__stderr; + +void __fio_init() +{ +} + +int __fileno(struct __io_file *f) +{ + return 0; +} + +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 = 0;//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; +} diff --git a/photon/libc/sys/magenta/machine/x86_64/crt0.s b/photon/libc/sys/magenta/machine/x86_64/crt0.s index e69de29..6941741 100644 --- a/photon/libc/sys/magenta/machine/x86_64/crt0.s +++ b/photon/libc/sys/magenta/machine/x86_64/crt0.s @@ -0,0 +1,19 @@ +.global _start +.type _start, @function + +.extern __fio_init +.type __fio_init, @function + +.extern main +.type main, @function + +_start: + pop %rbp + pop %rdi + mov %rsp, %rsi + andq $-16, %rsp + call __fio_init + call main + movq %rax, %rdi + movq $60, %rax + syscall diff --git a/photon/libc/sys/magenta/machine/x86_64/syscall.c b/photon/libc/sys/magenta/machine/x86_64/syscall.c new file mode 100644 index 0000000..dc54634 --- /dev/null +++ b/photon/libc/sys/magenta/machine/x86_64/syscall.c @@ -0,0 +1,108 @@ +#include + +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; +} + diff --git a/photon/libc/sys/magenta/sys/_errno.h b/photon/libc/sys/magenta/sys/_errno.h new file mode 100644 index 0000000..0955e02 --- /dev/null +++ b/photon/libc/sys/magenta/sys/_errno.h @@ -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 diff --git a/photon/libc/sys/magenta/sys/_fconst.h b/photon/libc/sys/magenta/sys/_fconst.h new file mode 100644 index 0000000..1733158 --- /dev/null +++ b/photon/libc/sys/magenta/sys/_fconst.h @@ -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 diff --git a/photon/libc/sys/magenta/sys/syscall.h b/photon/libc/sys/magenta/sys/syscall.h new file mode 100644 index 0000000..764395f --- /dev/null +++ b/photon/libc/sys/magenta/sys/syscall.h @@ -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 diff --git a/photon/libc/sys/magenta/sys/types.h b/photon/libc/sys/magenta/sys/types.h new file mode 100644 index 0000000..d0fcbfb --- /dev/null +++ b/photon/libc/sys/magenta/sys/types.h @@ -0,0 +1,32 @@ +#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; +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 diff --git a/photon/libc/sys/magenta/syscall.c b/photon/libc/sys/magenta/syscall.c new file mode 100644 index 0000000..750fd0f --- /dev/null +++ b/photon/libc/sys/magenta/syscall.c @@ -0,0 +1,3 @@ +#include "sys/syscall.h" +#include "__syscall.h" + diff --git a/photon/libc/sys/magenta/system.c b/photon/libc/sys/magenta/system.c new file mode 100644 index 0000000..d9025ad --- /dev/null +++ b/photon/libc/sys/magenta/system.c @@ -0,0 +1,7 @@ +#include "unistd.h" + +_Noreturn void __exit(int code) +{ + _exit(code); + while (1) {} +}