Started working on Magenta support

This commit is contained in:
Max Wash
2020-04-30 17:44:54 +01:00
parent 23e6519090
commit bb1f32f334
14 changed files with 376 additions and 6 deletions

View File

@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.5) cmake_minimum_required(VERSION 3.5)
project(photon C ASM-ATT) #project(photon C ASM-ATT)
macro(subdirlist result curdir) macro(subdirlist result curdir)
file(GLOB children RELATIVE ${curdir} ${curdir}/*) file(GLOB children RELATIVE ${curdir} ${curdir}/*)
@@ -13,7 +13,7 @@ macro(subdirlist result curdir)
endmacro() endmacro()
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/platform.cmake) include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/platform.cmake)
platform_config(${TARGET}) platform_config(${PHOTON_TARGET})
message(STATUS "Target: ${machine}-${platform}") 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) ${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/sys/${platform}/machine/${machine}/crt*.s)
add_library(crt OBJECT ${photon_libc_crt}) add_library(crt OBJECT ${photon_libc_crt})
target_compile_options(crt PRIVATE -c)
file(GLOB platform_sources file(GLOB platform_sources
${CMAKE_CURRENT_SOURCE_DIR}/photon/libc/sys/${platform}/*.c ${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_sources ${photon_libc_sources} ${malloc_sources} ${platform_sources})
set(photon_libc_headers ${photon_libc_headers} ${platform_headers}) set(photon_libc_headers ${photon_libc_headers} ${platform_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}) add_library(c SHARED ${photon_libc_sources} ${photon_libc_headers})
endif ()
target_compile_options(c PRIVATE -ffreestanding -nostdlib) target_compile_options(c PRIVATE -ffreestanding -nostdlib)
target_link_libraries(c crt) 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_SOURCE_DIR}/photon/libc/sys/${platform}/)
target_include_directories(c PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/sysroot/usr/include) 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} DEPENDS ${photon_libc_headers}
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/scripts/create-sysroot.sh COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/scripts/create-sysroot.sh
${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}
${platform} ${machine}) ${platform} ${machine})
add_dependencies(c sysroot) add_dependencies(c local-root)
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_link_libraries(c gcc) target_link_libraries(c gcc)
endif () endif ()
if (PHOTON_TESTS EQUAL 1)
add_subdirectory(tests) add_subdirectory(tests)
endif ()

View File

@@ -231,6 +231,14 @@
/* #define WIN32 */ /* #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 #ifdef WIN32
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN

View File

@@ -0,0 +1,28 @@
#ifndef SYS_LINUX___FIO_H_
#define SYS_LINUX___FIO_H_
#include <magenta.h>
#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

View 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

View 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

View File

@@ -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;
}

View File

@@ -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

View 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;
}

View 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

View 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

View 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

View 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

View File

@@ -0,0 +1,3 @@
#include "sys/syscall.h"
#include "__syscall.h"

View File

@@ -0,0 +1,7 @@
#include "unistd.h"
_Noreturn void __exit(int code)
{
_exit(code);
while (1) {}
}