Browse Source

[Pal/FreeBSD] Remove FreeBSD PAL

FreeBSD PAL is not maintained anymore, is neither built nor tested in
our CI and most likely stopped working long time ago. Currently it only
confuses people implementing changes in PALs (should they also fix bugs
in FreeBSD PAL if they can't test it?) and gives a false impression that
we support FreeBSD hosts.
Michał Kowalczyk 4 years ago
parent
commit
786b10a00f
40 changed files with 8 additions and 7963 deletions
  1. 0 1
      .ci/prfilter
  2. 0 1
      .ci/run-shellcheck
  3. 0 1
      .github/PULL_REQUEST_TEMPLATE.md
  4. 4 3
      Documentation/oldwiki/Introduction-to-Graphene.md
  5. 2 2
      Documentation/oldwiki/PAL-Host-ABI.md
  6. 1 3
      Pal/src/Makefile.Host
  7. 0 57
      Pal/src/host/FreeBSD/Makefile
  8. 0 21
      Pal/src/host/FreeBSD/Makefile.am
  9. 0 439
      Pal/src/host/FreeBSD/db_devices.c
  10. 0 144
      Pal/src/host/FreeBSD/db_events.c
  11. 0 595
      Pal/src/host/FreeBSD/db_exception.c
  12. 0 457
      Pal/src/host/FreeBSD/db_exception2.c
  13. 0 554
      Pal/src/host/FreeBSD/db_files.c
  14. 0 419
      Pal/src/host/FreeBSD/db_main.c
  15. 0 111
      Pal/src/host/FreeBSD/db_memory.c
  16. 0 189
      Pal/src/host/FreeBSD/db_misc.c
  17. 0 230
      Pal/src/host/FreeBSD/db_mutex.c
  18. 0 233
      Pal/src/host/FreeBSD/db_object.c
  19. 0 510
      Pal/src/host/FreeBSD/db_pipes.c
  20. 0 510
      Pal/src/host/FreeBSD/db_process.c
  21. 0 168
      Pal/src/host/FreeBSD/db_rtld.c
  22. 0 1477
      Pal/src/host/FreeBSD/db_sockets.c
  23. 0 460
      Pal/src/host/FreeBSD/db_streams.c
  24. 0 137
      Pal/src/host/FreeBSD/db_threading.c
  25. 0 40
      Pal/src/host/FreeBSD/elf-x86_64.h
  26. 0 8
      Pal/src/host/FreeBSD/host_endian.h
  27. 0 90
      Pal/src/host/FreeBSD/include/sigset.h
  28. 0 111
      Pal/src/host/FreeBSD/include/sysdep.h
  29. 0 21
      Pal/src/host/FreeBSD/pal-gdb.template
  30. 0 11
      Pal/src/host/FreeBSD/pal.gdb
  31. 0 83
      Pal/src/host/FreeBSD/pal.lds
  32. 0 8
      Pal/src/host/FreeBSD/pal.map.template
  33. 0 170
      Pal/src/host/FreeBSD/pal_freebsd.h
  34. 0 11
      Pal/src/host/FreeBSD/pal_freebsd_defs.h
  35. 0 43
      Pal/src/host/FreeBSD/pal_freebsd_error.h
  36. 0 283
      Pal/src/host/FreeBSD/pal_host.h
  37. 0 78
      Pal/src/host/FreeBSD/pal_security.h
  38. 0 106
      Pal/src/host/FreeBSD/rfork_thread.S
  39. 0 177
      Pal/src/host/FreeBSD/sysdep-x86_64.h
  40. 1 1
      README.rst

+ 0 - 1
.ci/prfilter

@@ -43,7 +43,6 @@ THE_BIG_LIST_OF_NAUGHTY_FILES = list(map(pathlib.Path, [
     'LibOS/shim/test/apps/common_tools/benchmark-http.sh',
     'LibOS/shim/test/apps/python-simple/run-tests.sh',
     'LibOS/shim/test/native',
-    'Pal/src/host/FreeBSD/pal-gdb.template',
     'Pal/src/host/Linux-SGX/debugger/gdb',
     'Pal/src/host/Linux-SGX/sgx-driver/load.sh',
     'Runtime/pal_loader',

+ 0 - 1
.ci/run-shellcheck

@@ -9,7 +9,6 @@ shellcheck "$@" \
     LibOS/shim/test/apps/common_tools/benchmark-http.sh \
     LibOS/shim/test/apps/python-simple/run-tests.sh \
     LibOS/shim/test/native/*.sh \
-    Pal/src/host/FreeBSD/pal-gdb.template \
     Pal/src/host/Linux-SGX/debugger/gdb \
     Pal/src/host/Linux-SGX/sgx-driver/load.sh \
     Runtime/pal_loader \

+ 0 - 1
.github/PULL_REQUEST_TEMPLATE.md

@@ -5,7 +5,6 @@
 - [ ] README and global configuration
 - [ ] Linux PAL
 - [ ] SGX PAL
-- [ ] FreeBSD PAL
 - [ ] Common PAL code
 - [ ] Library OS (i.e., SHIM), including GLIBC
 

+ 4 - 3
Documentation/oldwiki/Introduction-to-Graphene.md

@@ -22,9 +22,10 @@ Graphene was developed to encapsulate all host-specific code in one layer, calle
 Adaptation Layer, or PAL. Thus, if there is a PAL for a given host, the library OS and applications
 will "just work".
 
-Porting Graphene to a new host only requires porting a PAL, by implementing the [PAL Host ABI](PAL-Host-ABI.md)
-using OS features of the host. To date, we ported Graphene to FreeBSD and Linux (the latter also
-with Intel SGX support). Support for more hosts is expected in the future.
+Porting Graphene to a new host only requires porting a PAL, by implementing the
+[PAL Host ABI](PAL-Host-ABI.md) using OS features of the host. To date, we ported Graphene to
+FreeBSD (this port is not maintained anymore) and Linux (the latter also with Intel SGX support).
+Support for more hosts is expected in the future.
 
 #### Check out Application Test Cases
 

+ 2 - 2
Documentation/oldwiki/PAL-Host-ABI.md

@@ -35,8 +35,8 @@ based on the file name or content.
 
 Precisely, the loading rules for the manifest and executable are as follows:
 
-1. The first argument given to the PAL loader (e.g., `pal-Linux`, `pal-Linux-SGX`, `pal-FreeBSD`,
-or the cross-platform wrapper, `pal-loader`) can be either a manifest file or an executable.
+1. The first argument given to the PAL loader (e.g., `pal-Linux` or `pal-Linux-SGX`, or the
+cross-platform wrapper, `pal-loader`) can be either a manifest file or an executable.
 2. If an executable is given to the command line, the loader will search for the manifest in the
 following order: the same file name as the executable with a `.manifest` or `.manifest.sgx` extension,
 a `manifest` file without any extension, or no manifest at all.

+ 1 - 3
Pal/src/Makefile.Host

@@ -1,11 +1,9 @@
-all_hosts = Skeleton Linux Linux-SGX FreeBSD
+all_hosts = Skeleton Linux Linux-SGX
 
 SYS ?= $(shell gcc -dumpmachine)
 ifeq ($(PAL_HOST),)
 ifeq ($(findstring x86_64,$(SYS))$(findstring linux,$(SYS)),x86_64linux)
 PAL_HOST := Linux
-else ifeq ($(findstring freebsd,$(SYS)),freebsd)
-PAL_HOST := FreeBSD
 else
 $(error Unsupported platform: $(SYS))
 endif

+ 0 - 57
Pal/src/host/FreeBSD/Makefile

@@ -1,57 +0,0 @@
-include ../../../../Makefile.configs
-include Makefile.am
-
-CFLAGS	+= -I. -Iinclude -I../.. -I../../../include -I../../../lib \
-	   -I../../../linux-kernel/graphene
-ASFLAGS += -I. -Iinclude -I../.. -I../../../include
-
-host_files = libpal-FreeBSD.a pal.map ../../pal-gdb
-
-defs	= -DIN_PAL -DPAL_DIR="$(PAL_DIR)"
-CFLAGS += $(defs)
-ASFLAGS += $(defs)
-objs	= $(addprefix db_,files devices pipes sockets streams memory threading \
-	    mutex events process object main rtld misc \
-	    exception2) rfork_thread
-graphene_lib = .lib/graphene-lib.a
-headers	= $(wildcard *.h) $(wildcard ../../*.h) $(wildcard ../../../lib/*.h)
-
-.PHONY: all
-all: $(host_files)
-
-ifeq ($(DEBUG),1)
-CC += -g -gdwarf-2 -gstrict-dwarf
-CFLAGS += -DDEBUG
-export DEBUG
-endif
-
-pal-gdb: pal-gdb.template
-	sed -e 's:\$$(PAL_DIR):$(PWD):g' $< > $@
-	chmod 755 $@
-
-libpal-FreeBSD.a: $(addsuffix .o,$(objs)) $(graphene_lib)
-	$(call cmd,ar_a_o)
-
-%.o: %.c $(headers)
-	$(call cmd,cc_o_c)
-
-%.i: %.c $(headers)
-	$(call cmd,cpp_i_c)
-
-%.s: %.c $(headers)
-	$(call cmd,cc_s_c)
-
-%.o: %.S $(headers)
-	$(call cmd,as_o_S)
-
-%.s: %.S $(headers)
-	$(call cmd,cpp_s_S)
-
-
-include ../../../../Makefile.rules
-
-CLEAN_FILES += $(notdir $(pal_static) $(pal_lib) $(pal_sec) $(pal_loader))
-
-.PHONY: clean
-clean:
-	rm -f $(addsuffix .o,$(objs)) $(host_files) $(CLEAN_FILES)

+ 0 - 21
Pal/src/host/FreeBSD/Makefile.am

@@ -1,21 +0,0 @@
-# Add host-specific compilation rules here
-HOST_DIR = host/$(PAL_HOST)
-
-CFLAGS	= -Wall -fPIC -O2 -std=gnu99 -fgnu89-inline -U_FORTIFY_SOURCE \
-	  -fno-omit-frame-pointer \
-	  -fno-stack-protector -fno-builtin -Wtrampolines
-ASFLAGS = -DPIC -DSHARED -fPIC -DASSEMBLER -Wa,--noexecstack \
-	  -x assembler-with-cpp
-LDFLAGS	= -shared -nostdlib -z combreloc -z defs \
-	  --version-script $(HOST_DIR)/pal.map -T $(HOST_DIR)/pal.lds
-ARFLAGS	=
-
-ifeq ($(WERROR),1)
-CFLAGS += -Werror
-endif
-
-pal_loader = $(HOST_DIR)/libpal.so
-pal_lib = $(HOST_DIR)/libpal.so
-pal_lib_deps = pal-symbols $(HOST_DIR)/pal.map.template $(HOST_DIR)/pal.lds
-pal_lib_post =
-pal_static = $(HOST_DIR)/libpal.a

+ 0 - 439
Pal/src/host/FreeBSD/db_devices.c

@@ -1,439 +0,0 @@
-/* Copyright (C) 2014 Stony Brook University
-   This file is part of Graphene Library OS.
-
-   Graphene Library OS is free software: you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public License
-   as published by the Free Software Foundation, either version 3 of the
-   License, or (at your option) any later version.
-
-   Graphene Library OS is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
-
-/*
- * db_device.c
- *
- * This file contains operands to handle streams with URIs that start with
- * "dev:".
- */
-
-#include "pal_defs.h"
-#include "pal_freebsd_defs.h"
-#include "pal.h"
-#include "pal_internal.h"
-#include "pal_freebsd.h"
-#include "pal_debug.h"
-#include "pal_error.h"
-#include "api.h"
-
-#include <sys/types.h>
-typedef __kernel_pid_t pid_t;
-#include <sys/stat.h>
-#include <errno.h>
-
-#define DEVICE_OPS(handle)                              \
-    ({ int _type = (handle)->dev.dev_type;              \
-       (_type <= 0 || _type >= PAL_DEVICE_TYPE_BOUND) ? \
-       NULL : pal_device_ops[_type];                    \
-     })
-
-enum {
-    device_type_none = 0,
-    device_type_term,
-    PAL_DEVICE_TYPE_BOUND,
-};
-
-static struct handle_ops term_ops;
-
-static const struct handle_ops * pal_device_ops [PAL_DEVICE_TYPE_BOUND] = {
-            NULL,
-            &term_ops,
-        };
-
-/* parse_device_uri scans the uri, parses the prefix of the uri and searches
-   for stream handler wich will open or access the device. */
-static int parse_device_uri(const char ** uri, char ** type, struct handle_ops ** ops)
-{
-    struct handle_ops * dops = NULL;
-    const char * p, * u = (*uri);
-
-    for (p = u ; (*p) && (*p) != ',' && (*p) != '/' ; p++);
-
-    if (strstartswith_static(u, "tty"))
-        dops = &term_ops;
-
-    if (!dops)
-        return -PAL_ERROR_NOTSUPPORT;
-
-    *uri = (*p) ? p + 1 : p;
-    if (type) {
-        *type = malloc_copy(u, p - u + 1);
-        if (!*type)
-            return -PAL_ERROR_NOMEM;
-        (*type)[p - u] = '\0';
-    }
-    if (ops)
-        *ops = dops;
-    return 0;
-}
-
-static inline void
-dev_attrcopy (PAL_STREAM_ATTR * attr, struct stat * stat);
-
-static int char_read (PAL_HANDLE handle, int offset, int count, void * buffer);
-static int char_write (PAL_HANDLE handle, int offset, int count,
-                       const void * buffer);
-static int term_attrquery (const char * type, const char * uri,
-                           PAL_STREAM_ATTR * attr);
-static int term_attrquerybyhdl (PAL_HANDLE hdl,
-                                PAL_STREAM_ATTR * attr);
-
-/* Method to open standard terminal */
-static int open_standard_term (PAL_HANDLE * handle, const char * param,
-                               int access)
-{
-    if (param)
-        return -PAL_ERROR_NOTIMPLEMENTED;
-
-    PAL_HANDLE hdl = malloc(HANDLE_SIZE(dev));
-    SET_HANDLE_TYPE(hdl, dev);
-    hdl->dev.dev_type = device_type_term;
-
-    if (!(access & PAL_ACCESS_WRONLY)) {
-        hdl->hdr.flags |= RFD(0);
-        hdl->dev.fd_in = 0;
-    }
-
-    if (access & (PAL_ACCESS_WRONLY|PAL_ACCESS_RDWR)) {
-        hdl->hdr.flags |= WFD(1);
-        hdl->dev.fd_out = 1;
-    }
-
-    *handle = hdl;
-    return 0;
-}
-
-/* 'open' operation for terminal stream */
-static int term_open (PAL_HANDLE *handle, const char * type, const char * uri,
-                      int access, int share, int create, int options)
-{
-    const char * term = NULL;
-    const char * param = NULL;
-
-    const char * tmp = uri;
-    while (*tmp) {
-        if (!term && *tmp == '/')
-            term = tmp + 1;
-        if (*tmp == ',') {
-            param = param + 1;
-            break;
-        }
-        tmp++;
-    }
-
-    if (term)
-        return -PAL_ERROR_NOTIMPLEMENTED;
-
-    return open_standard_term(handle, param, access);
-}
-
-static int term_close (PAL_HANDLE handle)
-{
-    return 0;
-}
-
-/* 'attrquery' operation for terminal stream */
-static int term_attrquery (const char * type, const char * uri,
-                           PAL_STREAM_ATTR * attr)
-{
-    attr->handle_type = pal_type_dev;
-    attr->readable = PAL_TRUE;
-    attr->writable = PAL_TRUE;
-    attr->runnable = PAL_FALSE;
-    attr->pending_size = 0;
-    return 0;
-}
-
-/* 'attrquery' operation for terminal stream */
-static int term_attrquerybyhdl (PAL_HANDLE hdl,
-                                PAL_STREAM_ATTR * attr)
-{
-    attr->handle_type = pal_type_dev;
-    attr->readable = (hdl->dev.fd_in  != PAL_IDX_POISON);
-    attr->writable = (hdl->dev.fd_out != PAL_IDX_POISON);
-    attr->runnable = PAL_FALSE;
-    attr->pending_size = 0;
-    return 0;
-}
-
-static struct handle_ops term_ops = {
-        .open           = &term_open,
-        .close          = &term_close,
-        .read           = &char_read,
-        .write          = &char_write,
-        .attrquery      = &term_attrquery,
-        .attrquerybyhdl = &term_attrquerybyhdl,
-    };
-
-/* 'read' operation for character streams. */
-static int char_read (PAL_HANDLE handle, int offset, int size, void * buffer)
-{
-    int fd = handle->dev.fd_in;
-
-    if (fd == PAL_IDX_POISON)
-        return -PAL_ERROR_DENIED;
-
-    int bytes = INLINE_SYSCALL(read, 3, fd, buffer, size);
-
-    if (IS_ERR(bytes))
-        return unix_to_pal_error(ERRNO(bytes));
-
-    return bytes;
-}
-
-/* 'write' operation for character streams. */
-static int char_write (PAL_HANDLE handle, int offset, int size,
-                      const void * buffer)
-{
-    int fd = handle->dev.fd_out;
-
-    if (fd == PAL_IDX_POISON)
-        return -PAL_ERROR_DENIED;
-
-    int bytes = INLINE_SYSCALL(write, 3, fd, buffer, size);
-
-    if (IS_ERR(bytes))
-        return unix_to_pal_error(ERRNO(bytes));
-
-    return bytes;
-}
-
-/* 'open' operation for device streams */
-static int dev_open (PAL_HANDLE * handle, const char * type, const char * uri,
-                     int access, int share, int create, int options)
-{
-    struct handle_ops * ops = NULL;
-    char * dev_type = NULL;
-    int ret = 0;
-
-    ret = parse_device_uri(&uri, &dev_type, &ops);
-
-    if (ret < 0)
-        return ret;
-
-    if (!ops->open)
-            return -PAL_ERROR_NOTSUPPORT;
-
-    PAL_HANDLE hdl = malloc(HANDLE_SIZE(dev));
-    hdl->dev.fd_in  = PAL_IDX_POISON;
-    hdl->dev.fd_out = PAL_IDX_POISON;
-    *handle = hdl;
-
-    ret = ops->open(handle, dev_type, uri,
-                    access, share, create, options);
-    free(dev_type);
-    return ret;
-}
-
-/* 'read' operation for device stream */
-static int dev_read (PAL_HANDLE handle, int offset, int size, void * buffer)
-{
-    const struct handle_ops * ops = DEVICE_OPS(handle);
-
-    if (!ops || !ops->read)
-        return -PAL_ERROR_NOTSUPPORT;
-
-    return ops->read(handle, offset, size, buffer);
-}
-
-/* 'write' operation for device stream */
-static int dev_write (PAL_HANDLE handle, int offset, int size,
-                      const void * buffer)
-{
-    const struct handle_ops * ops = DEVICE_OPS(handle);
-
-    if (!ops || !ops->write)
-        return -PAL_ERROR_NOTSUPPORT;
-
-    return ops->write(handle, offset, size, buffer);
-}
-
-/* 'close' operation for device streams */
-static int dev_close (PAL_HANDLE handle)
-{
-    const struct handle_ops * ops = DEVICE_OPS(handle);
-
-    if (ops && ops->close)
-        return ops->close(handle);
-
-    if (handle->dev.fd_in != PAL_IDX_POISON) {
-        int fd = handle->dev.fd_in;
-
-        int ret = INLINE_SYSCALL(close, 1, fd);
-
-        if (IS_ERR(ret)) {
-            if (ERRNO(ret) != EBADF && ERRNO(ret) != EINVAL)
-                return unix_to_pal_error(ERRNO(ret));
-        }
-    }
-
-    if (handle->dev.fd_out != PAL_IDX_POISON) {
-        int fd = handle->dev.fd_out;
-
-        int ret = INLINE_SYSCALL(close, 1, fd);
-
-        if (IS_ERR(ret)) {
-            if (ERRNO(ret) != EBADF && ERRNO(ret) != EINVAL)
-                return unix_to_pal_error(ERRNO(ret));
-        }
-    }
-
-    if (handle->file.realpath)
-        free((void *) handle->file.realpath);
-
-    return 0;
-}
-
-/* 'delete' operation for device streams */
-static int dev_delete (PAL_HANDLE handle, int access)
-{
-    const struct handle_ops * ops = DEVICE_OPS(handle);
-
-    if (!ops || !ops->delete)
-        return -PAL_ERROR_DENIED;
-
-    int ret = dev_close(handle);
-
-    if (ret < 0)
-        return ret;
-
-    return ops->delete(handle, access);
-}
-
-/* 'flush' operation for device streams */
-static int dev_flush (PAL_HANDLE handle)
-{
-    const struct handle_ops * ops = DEVICE_OPS(handle);
-
-    if (ops && ops->flush)
-        return ops->flush(handle);
-
-    /* try to flush input stream */
-    if (handle->dev.fd_in != PAL_IDX_POISON) {
-        int fd = handle->dev.fd_in;
-
-        int ret = INLINE_SYSCALL(fsync, 1, fd);
-
-        if (IS_ERR(ret)) {
-            if (ERRNO(ret) == EBADF || ERRNO(ret) == EINVAL)
-                return -PAL_ERROR_BADHANDLE;
-            else
-                return unix_to_pal_error(ERRNO(ret));
-        }
-    }
-
-    /* if output stream exists and does not equal to input stream,
-       flush output stream as well */
-    if (handle->dev.fd_out != PAL_IDX_POISON &&
-        handle->dev.fd_out != handle->dev.fd_in) {
-        int fd = handle->dev.fd_out;
-
-        int ret = INLINE_SYSCALL(fsync, 1, fd);
-
-        if (IS_ERR(ret)) {
-            if (ERRNO(ret) == EBADF || ERRNO(ret) == EINVAL)
-                return -PAL_ERROR_BADHANDLE;
-            else
-                return unix_to_pal_error(ERRNO(ret));
-        }
-    }
-
-    return 0;
-}
-
-static inline void
-dev_attrcopy (PAL_STREAM_ATTR * attr, struct stat * stat)
-{
-    attr->handle_type = pal_type_dev;
-	/* readable, writable and runnable are decied by euidstataccess */
-    attr->readable = stataccess(stat, ACCESS_R);
-    attr->writable = stataccess(stat, ACCESS_W);
-    attr->runnable = stataccess(stat, ACCESS_X);
-    attr->pending_size = stat->st_size;
-}
-
-/* 'attrquery' operation for device streams */
-static int dev_attrquery (const char * type, const char * uri,
-                          PAL_STREAM_ATTR * attr)
-{
-    struct handle_ops * ops = NULL;
-    const char * dev_type = NULL;
-    int ret = 0;
-
-    ret = parse_device_uri(&uri, &dev_type, &ops);
-
-    if (ret < 0)
-        return ret;
-
-    if (!ops || !ops->attrquery)
-        return -PAL_ERROR_NOTSUPPORT;
-
-    return ops->attrquery(dev_type, uri, attr);
-}
-
-/* 'attrquerybyhdl' operation for device stream */
-static int dev_attrquerybyhdl (PAL_HANDLE handle,
-                               PAL_STREAM_ATTR * attr)
-{
-    const struct handle_ops * ops = DEVICE_OPS(handle);
-
-    if (ops && ops->attrquerybyhdl)
-        return ops->attrquerybyhdl(handle, attr);
-
-    struct stat stat_buf, * stat_in = NULL, * stat_out = NULL;
-    int ret;
-
-    attr->handle_type = pal_type_dev;
-
-    if (handle->dev.fd_in != PAL_IDX_POISON) {
-        ret = INLINE_SYSCALL(fstat, 2, handle->dev.fd_in, &stat_buf);
-
-        if (!IS_ERR(ret))
-            stat_in = &stat_buf;
-    }
-
-    if (handle->dev.fd_in != PAL_IDX_POISON) {
-        ret = INLINE_SYSCALL(fstat, 2, handle->dev.fd_in, &stat_buf);
-
-        if (!IS_ERR(ret))
-            stat_out = &stat_buf;
-    }
-
-    attr->readable = (stat_in  && stataccess(stat_in,  ACCESS_R));
-    attr->runnable = (stat_in  && stataccess(stat_in,  ACCESS_X));
-    attr->writable = (stat_out && stataccess(stat_out, ACCESS_W));
-    attr->pending_size = stat_in ? stat_in->st_size :
-                         (stat_out ? stat_out->st_size : 0);
-    return 0;
-}
-
-static const char * dev_getrealpath (PAL_HANDLE handle)
-{
-    return handle->dev.realpath;
-}
-
-struct handle_ops dev_ops = {
-        .getrealpath        = &dev_getrealpath,
-        .open               = &dev_open,
-        .read               = &dev_read,
-        .write              = &dev_write,
-        .close              = &dev_close,
-        .delete             = &dev_delete,
-        .flush              = &dev_flush,
-        .attrquery          = &dev_attrquery,
-        .attrquerybyhdl     = &dev_attrquerybyhdl,
-    };

+ 0 - 144
Pal/src/host/FreeBSD/db_events.c

@@ -1,144 +0,0 @@
-/* Copyright (C) 2014 Stony Brook University
-   This file is part of Graphene Library OS.
-
-   Graphene Library OS is free software: you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public License
-   as published by the Free Software Foundation, either version 3 of the
-   License, or (at your option) any later version.
-
-   Graphene Library OS is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
-
-/*
- * db_event.c
- *
- * This file contains implementation of Drawbridge event synchronization APIs.
- */
-
-#include <atomic.h>
-#include <errno.h>
-#include <sys/time.h>
-
-#include "api.h"
-#include "pal.h"
-#include "pal_defs.h"
-#include "pal_error.h"
-#include "pal_freebsd.h"
-#include "pal_freebsd_defs.h"
-#include "pal_internal.h"
-
-int _DkEventCreate(PAL_HANDLE* event, bool initialState, bool isnotification) {
-    PAL_HANDLE ev = malloc(HANDLE_SIZE(event));
-    SET_HANDLE_TYPE(ev, event);
-    ev->event.isnotification = isnotification;
-    atomic_set(&ev->event.signaled, initialState ? 1 : 0);
-    atomic_set(&ev->event.nwaiters, 0);
-    *event = ev;
-    return 0;
-}
-
-void _DkEventDestroy(PAL_HANDLE handle) {
-    free(handle);
-}
-
-int _DkEventSet(PAL_HANDLE event, int wakeup) {
-    int ret = 0;
-    if (event->event.isnotification) {
-        /* Leave it signaled, wake all */
-        if (atomic_cmpxchg(&event->event.signaled, 0, 1) == 0) {
-            int nwaiters = atomic_read(&event->event.nwaiters);
-            if (nwaiters) {
-                if (wakeup != -1 && nwaiters > wakeup)
-                    nwaiters = wakeup;
-
-                ret = INLINE_SYSCALL(_umtx_op, 5, &event->event.signaled, UMTX_OP_WAKE, nwaiters,
-                                     NULL, NULL);
-                if (IS_ERR(ret))
-                    atomic_set(&event->event.signaled, 0);
-            }
-        }
-    } else {
-        /* Only one thread wakes up, leave unsignaled */
-        ret = INLINE_SYSCALL(_umtx_op, 5, &event->event.signaled, UMTX_OP_WAKE, 1, NULL, NULL);
-    }
-
-    return IS_ERR(ret) ? PAL_ERROR_TRYAGAIN : ret;
-}
-
-int _DkEventWaitTimeout(PAL_HANDLE event, uint64_t timeout) {
-    int ret = 0;
-    if (!event->event.isnotification || !atomic_read(&event->event.signaled)) {
-        struct timespec waittime;
-        unsigned long sec      = timeout / 1000000UL;
-        unsigned long microsec = timeout - (sec * 1000000UL);
-        waittime.tv_sec        = sec;
-        waittime.tv_nsec       = microsec * 1000;
-
-        atomic_inc(&event->event.nwaiters);
-
-        do {
-            ret = INLINE_SYSCALL(_umtx_op, 5, &event->event.signaled, UMTX_OP_WAIT_UINT, 0, NULL,
-                                 &waittime);
-            if (IS_ERR(ret)) {
-                if (ERRNO(ret) == EWOULDBLOCK) {
-                    ret = 0;
-                } else {
-                    ret = unix_to_pal_error(ERRNO(ret));
-                    break;
-                }
-            }
-        } while (event->event.isnotification && !atomic_read(&event->event.signaled));
-
-        atomic_dec(&event->event.nwaiters);
-    }
-
-    return ret;
-}
-
-int _DkEventWait(PAL_HANDLE event) {
-    int ret = 0;
-
-    if (!event->event.isnotification || !atomic_read(&event->event.signaled)) {
-        atomic_inc(&event->event.nwaiters);
-
-        do {
-            ret = INLINE_SYSCALL(_umtx_op, 5, &event->event.signaled, UMTX_OP_WAIT, 0, NULL, NULL);
-            if (IS_ERR(ret)) {
-                if (ERRNO(ret) == EWOULDBLOCK) {
-                    ret = 0;
-                } else {
-                    ret = unix_to_pal_error(ERRNO(ret));
-                    break;
-                }
-            }
-        } while (event->event.isnotification && !atomic_read(&event->event.signaled));
-
-        atomic_dec(&event->event.nwaiters);
-    }
-
-    return ret;
-}
-
-int _DkEventClear(PAL_HANDLE event) {
-    atomic_set(&event->event.signaled, 0);
-    return 0;
-}
-
-static int event_close(PAL_HANDLE handle) {
-    _DkEventSet(handle, -1);
-    return 0;
-}
-
-static int event_wait(PAL_HANDLE handle, uint64_t timeout) {
-    return timeout == NO_TIMEOUT ? _DkEventWait(handle) : _DkEventWaitTimeout(handle, timeout);
-}
-
-struct handle_ops event_ops = {
-    .close = &event_close,
-    .wait  = &event_wait,
-};

+ 0 - 595
Pal/src/host/FreeBSD/db_exception.c

@@ -1,595 +0,0 @@
-/* Copyright (C) 2014 Stony Brook University
-   This file is part of Graphene Library OS.
-
-   Graphene Library OS is free software: you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public License
-   as published by the Free Software Foundation, either version 3 of the
-   License, or (at your option) any later version.
-
-   Graphene Library OS is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
-
-/*
- * db_signal.c
- *
- * This file contains APIs to set up handlers of exceptions issued by the
- * host, and the methods to pass the exceptions to the upcalls.
- */
-
-#include "pal_defs.h"
-#include "pal_freebsd_defs.h"
-#include "pal.h"
-#include "pal_internal.h"
-#include "pal_freebsd.h"
-#include "pal_error.h"
-#include "pal_security.h"
-#include "api.h"
-#include "list.h"
-
-#include <atomic.h>
-#include <sigset.h>
-#include "signal.h"
-#include <ucontext.h>
-#include <errno.h>
-
-typedef void (*PAL_UPCALL) (PAL_PTR, PAL_NUM, PAL_CONTEXT *);
-
-int set_sighandler (int * sigs, int nsig, void * handler)
-{
-    struct sigaction action;
-    action.sa_handler = (void (*)(int)) handler;
-    action.sa_flags = SA_SIGINFO;
-
-    __sigemptyset((_sigset_t *) &action.sa_mask);
-    __sigaddset((_sigset_t *) &action.sa_mask, SIGCONT);
-
-    for (int i = 0 ; i < nsig ; i++) {
-        if (sigs[i] == SIGCHLD)
-            action.sa_flags |= SA_NOCLDSTOP|SA_NOCLDWAIT;
-
-#if defined(__i386__)
-        int ret = INLINE_SYSCALL(sigaction, 3, sigs[i], &action, NULL)
-#else
-        int ret = INLINE_SYSCALL(sigaction, 4, sigs[i], &action, NULL,
-                                 sizeof(sigset_t));
-#endif
-        if (IS_ERR(ret))
-            return -PAL_ERROR_DENIED;
-
-        action.sa_flags &= ~(SA_NOCLDSTOP|SA_NOCLDWAIT);
-    }
-
-    bool maskset = false;
-    int ret = 0;
-    _sigset_t mask;
-    __sigemptyset(&mask);
-    for (int i = 0 ; i < nsig ; i++)
-        if (__sigismember(&bsd_state.sigset, sigs[i])) {
-            __sigdelset(&bsd_state.sigset, sigs[i]);
-            __sigaddset(&mask, sigs[i]);
-            maskset = true;
-        }
-
-    if (maskset) {
-#if defined(__i386__)
-        ret = INLINE_SYSCALL(sigprocmask, 3, SIG_UNBLOCK, &mask, NULL)
-#else
-        ret = INLINE_SYSCALL(sigprocmask, 4, SIG_UNBLOCK, &mask, NULL,
-                             sizeof(sigset_t));
-#endif
-    }
-
-    if (IS_ERR(ret))
-        return -PAL_ERROR_DENIED;
-
-    return 0;
-}
-
-int block_signals (int * sigs, int nsig)
-{
-    bool maskset = false;
-    int ret = 0;
-    _sigset_t mask;
-    __sigemptyset(&mask);
-    for (int i = 0 ; i < nsig ; i++)
-        if (!__sigismember(&bsd_state.sigset, sigs[i])) {
-            __sigaddset(&bsd_state.sigset, sigs[i]);
-            __sigaddset(&mask, sigs[i]);
-            maskset = true;
-        }
-
-    if (maskset) {
-#if defined(__i386__)
-        ret = INLINE_SYSCALL(sigprocmask, 3, SIG_BLOCK, &mask, NULL)
-#else
-        ret = INLINE_SYSCALL(sigprocmask, 4, SIG_BLOCK, &mask, NULL,
-                             sizeof(sigset_t));
-#endif
-    }
-
-    if (IS_ERR(ret))
-        return -PAL_ERROR_DENIED;
-
-    return 0;
-}
-
-int unblock_signals (int * sigs, int nsig)
-{
-    bool maskset = false;
-    int ret = 0;
-    _sigset_t mask;
-    __sigemptyset(&mask);
-    for (int i = 0 ; i < nsig ; i++)
-        if (__sigismember(&bsd_state.sigset, sigs[i])) {
-            __sigdelset(&bsd_state.sigset, sigs[i]);
-            __sigaddset(&mask, sigs[i]);
-            maskset = true;
-        }
-
-    if (maskset) {
-#if defined(__i386__)
-        ret = INLINE_SYSCALL(sigprocmask, 3, SIG_UNBLOCK, &mask, NULL)
-#else
-        ret = INLINE_SYSCALL(sigprocmask, 4, SIG_UNBLOCK, &mask, NULL,
-                             sizeof(sigset_t));
-#endif
-    }
-
-    if (IS_ERR(ret))
-        return -PAL_ERROR_DENIED;
-
-    return 0;
-}
-
-int unset_sighandler (int * sigs, int nsig)
-{
-    for (int i = 0 ; i < nsig ; i++) {
-#if defined(__i386__)
-        int ret = INLINE_SYSCALL(sigaction, 4, sigs[i], SIG_DFL, NULL)
-#else
-        int ret = INLINE_SYSCALL(sigaction, 4, sigs[i], SIG_DFL, NULL,
-                                 sizeof(_sigset_t));
-#endif
-        if (IS_ERR(ret))
-            return -PAL_ERROR_DENIED;
-    }
-    return 0;
-}
-
-struct exception_handler {
-    struct mutex_handle lock;
-    int flags;
-    PAL_UPCALL upcall;
-
-} __attribute__((aligned(sizeof(int))));
-
-struct exception_event {
-
-    int event_num;
-    int flags;
-    PAL_CONTEXT context;
-    ucontext_t * uc;
-    void * eframe;
-
-};
-
-#define DECLARE_HANDLER_HEAD(event)                         \
-    static struct exception_handler handler_##event =       \
-        {  .lock = MUTEX_HANDLE_INIT,                       \
-           .upcall = NULL,                                  \
-           .flags = 0, };
-
-DECLARE_HANDLER_HEAD(DivZero);
-DECLARE_HANDLER_HEAD(MemFault);
-DECLARE_HANDLER_HEAD(Illegal);
-DECLARE_HANDLER_HEAD(Quit);
-DECLARE_HANDLER_HEAD(Suspend);
-DECLARE_HANDLER_HEAD(Resume);
-DECLARE_HANDLER_HEAD(Failure);
-
-struct exception_handler * pal_handlers [PAL_EVENT_NUM_BOUND] = {
-        NULL, /* reserved */
-        &handler_DivZero,
-        &handler_MemFault,
-        &handler_Illegal,
-        &handler_Quit,
-        &handler_Suspend,
-        &handler_Resume,
-        &handler_Failure,
-    };
-
-static int get_event_num (int signum)
-{
-    switch(signum) {
-        case SIGFPE:                return PAL_EVENT_ARITHMETIC_ERROR;
-        case SIGSEGV: case SIGBUS:  return PAL_EVENT_MEMFAULT;
-        case SIGILL:                return PAL_EVENT_ILLEGAL;
-        case SIGTERM:               return PAL_EVENT_QUIT;
-        case SIGINT:                return PAL_EVENT_SUSPEND;
-        case SIGCONT:               return PAL_EVENT_RESUME;
-        default: return -1;
-    }
-}
-
-static void _DkGenericEventTrigger (int event_num, PAL_UPCALL upcall,
-                                    int flags, PAL_NUM arg,
-                                    struct pal_frame * frame,
-                                    ucontext_t * uc, void * eframe)
-{
-    struct exception_event event;
-
-    event.event_num = event_num;
-    event.flags = flags;
-    if (uc) {
-        event.context.r8  = uc->uc_mcontext.mc_r8;
-        event.context.r9  = uc->uc_mcontext.mc_r9;
-        event.context.r10 = uc->uc_mcontext.mc_r10;
-        event.context.r11 = uc->uc_mcontext.mc_r11;
-        event.context.r12 = uc->uc_mcontext.mc_r12;
-        event.context.r13 = uc->uc_mcontext.mc_r13;
-        event.context.r14 = uc->uc_mcontext.mc_r14;
-        event.context.r15 = uc->uc_mcontext.mc_r15;
-        event.context.rdi = uc->uc_mcontext.mc_rdi;
-        event.context.rsi = uc->uc_mcontext.mc_rsi;
-        event.context.rbp = uc->uc_mcontext.mc_rbp;
-        event.context.rbx = uc->uc_mcontext.mc_rbx;
-        event.context.rdx = uc->uc_mcontext.mc_rdx;
-        event.context.rax = uc->uc_mcontext.mc_rax;
-        event.context.rcx = uc->uc_mcontext.mc_rcx;
-        event.context.rsp = uc->uc_mcontext.mc_rsp;
-        event.context.rip = uc->uc_mcontext.mc_rip;
-        event.context.efl = uc->uc_mcontext.mc_flags;
-        event.context.err = uc->uc_mcontext.mc_err;
-        event.context.trapno = uc->uc_mcontext.mc_trapno;
-        event.context.csgsfs  = 0;
-        event.context.oldmask = 0;
-        event.context.cr2     = 0;
-    }
-
-    if (frame) {
-        event.context.r15 = frame->arch.r15;
-        event.context.r14 = frame->arch.r14;
-        event.context.r13 = frame->arch.r13;
-        event.context.r12 = frame->arch.r12;
-        event.context.rdi = frame->arch.rdi;
-        event.context.rsi = frame->arch.rsi;
-        event.context.rbx = frame->arch.rbx;
-        /* find last frame */
-        event.context.rsp = frame->arch.rbp + sizeof(unsigned long) * 2;
-        event.context.rbp = ((unsigned long *) frame->arch.rbp)[0];
-        event.context.rip = ((unsigned long *) frame->arch.rbp)[1];
-    }
-
-    event.uc = uc;
-    event.eframe = eframe;
-
-    (*upcall) (&event, arg, &event.context);
-}
-
-static bool _DkGenericSignalHandle (int event_num, siginfo_t * info,
-                                    struct pal_frame * frame,
-                                    ucontext_t * uc, void * eframe)
-{
-    struct exception_handler * handler = pal_handlers[event_num];
-
-    _DkMutexLock(&handler->lock);
-    PAL_UPCALL upcall = handler->upcall;
-    int flags = handler->flags;
-    _DkMutexUnlock(&handler->lock);
-
-    if (upcall) {
-        PAL_NUM arg = 0;
-
-
-        if (event_num == PAL_EVENT_ARITHMETIC_ERROR ||
-            event_num == PAL_EVENT_MEMFAULT ||
-            event_num == PAL_EVENT_ILLEGAL)
-            arg = (PAL_NUM) (info ? info->si_addr : 0);
-
-        _DkGenericEventTrigger(event_num, upcall, flags, arg, frame,
-                               uc, eframe);
-        return true;
-    }
-
-    return false;
-}
-
-#define ADDR_IN_PAL(addr) \
-        ((void*)(addr) > TEXT_START && (void*)(addr) < TEXT_END)
-
-static struct pal_frame * get_frame (ucontext_t * uc)
-{
-    unsigned long rip = uc->uc_mcontext.mc_rip;
-    unsigned long rbp = uc->uc_mcontext.mc_rbp;
-    unsigned long last_rbp = rbp - 1024;
-
-    if (!ADDR_IN_PAL(rip))
-        return NULL;
-
-    while (ADDR_IN_PAL(((unsigned long *) rbp)[1])) {
-        last_rbp = rbp;
-        rbp = *(unsigned long *) rbp;
-    }
-
-    /* search frame record in the top frame of PAL */
-    for (unsigned long ptr = rbp - sizeof(unsigned long) ;
-         ptr > last_rbp ; ptr -= 8) {
-        struct pal_frame * frame = (struct pal_frame *) ptr;
-        if (frame->identifier == PAL_FRAME_IDENTIFIER)
-            return frame;
-    }
-
-    return NULL;
-}
-
-static void return_frame (struct pal_frame * frame, int err)
-{
-    if (err)
-        _DkRaiseFailure(err);
-
-    __clear_frame(frame);
-    arch_restore_frame(&frame->arch);
-    asm volatile ("xor %%rax, %%rax\r\n"
-                  "leaveq\r\n"
-                  "retq\r\n" ::: "memory");
-}
-
-static void _DkGenericSighandler (int signum, siginfo_t * info,
-                                  ucontext_t * uc)
-{
-#if 0
-    /* resurrect this code if signal handler is giving segmentation fault */
-
-    if (signum == SIGSEGV) {
-        int pid = INLINE_SYSCALL(getpid, 0);
-        char msg[24] = "--- SIGSEGV --- [     ]\n";
-        msg[17] = '0' + pid / 10000;
-        msg[18] = '0' + (pid / 1000) % 10;
-        msg[19] = '0' + (pid / 100) % 10;
-        msg[20] = '0' + (pid / 10) % 10;
-        msg[21] = '0' + pid % 10;
-        INLINE_SYSCALL(write, 3, 1, msg, 24);
-
-    }
-#endif
-
-    struct pal_frame * frame = get_frame(uc);
-    void * eframe;
-
-    asm volatile ("movq %%rbp, %0" : "=r"(eframe));
-
-    if (frame && frame->func != &_DkGenericSighandler &&
-        signum != SIGCONT &&
-        signum != SIGINT  &&
-        signum != SIGTERM) {
-        return_frame(frame, PAL_ERROR_BADADDR);
-        return;
-    }
-
-    int event_num = get_event_num(signum);
-    if (event_num == -1)
-        return;
-
-    _DkGenericSignalHandle(event_num, info, frame, uc, eframe);
-}
-
-static void _DkTerminateSighandler (int signum, siginfo_t * info,
-                                    ucontext_t * uc)
-{
-    struct pal_frame * frame = get_frame(uc);
-    void * eframe;
-
-    asm volatile ("movq %%rbp, %0" : "=r"(eframe));
-
-    int event_num = get_event_num(signum);
-    if (event_num == -1)
-        return;
-
-    if (!_DkGenericSignalHandle(event_num, NULL, frame, uc, eframe))
-        _DkThreadExit(/*clear_child_tid=*/NULL);
-}
-
-static void _DkPipeSighandler (int signum, siginfo_t * info,
-                               ucontext_t * uc)
-{
-    struct pal_frame * frame = get_frame(uc);
-    if (frame)
-        return_frame(frame, PAL_ERROR_CONNFAILED);
-}
-
-void _DkRaiseFailure (int error)
-{
-    _DkMutexLock(&handler_Failure.lock);
-    PAL_UPCALL upcall = handler_Failure.upcall;
-    int flags = handler_Failure.flags;
-    _DkMutexUnlock(&handler_Failure.lock);
-
-    if (upcall)
-        _DkGenericEventTrigger(PAL_EVENT_FAILURE, upcall, flags, error,
-                               NULL, NULL, NULL);
-}
-
-struct signal_ops {
-    int signum[3];
-    void (*handler) (int signum, siginfo_t * info, ucontext_t * uc);
-
-};
-
-struct signal_ops on_signals[PAL_EVENT_NUM_BOUND] = {
-        /* DivZero     */ { .signum = { SIGFPE, 0 },
-                            .handler = _DkGenericSighandler },
-        /* MemFault    */ { .signum = { SIGSEGV, SIGBUS, 0 },
-                            .handler = _DkGenericSighandler },
-        /* Illegal     */ { .signum = { SIGILL, 0 },
-                            .handler = _DkGenericSighandler },
-        /* Quit        */ { .signum = { SIGTERM, 0, 0 },
-                            .handler = _DkTerminateSighandler },
-        /* Suspend     */ { .signum = { SIGINT, 0 },
-                            .handler = _DkTerminateSighandler },
-        /* Resume      */ { .signum = { SIGCONT, 0 },
-                            .handler = _DkGenericSighandler },
-    };
-
-static int _DkPersistentSighandlerSetup (int event_num)
-{
-    int nsigs, * sigs = on_signals[event_num].signum;
-    for (nsigs = 0 ; sigs[nsigs] ; nsigs++);
-
-    void * sighandler = on_signals[event_num].handler;
-
-    int ret = set_sighandler (sigs, nsigs, sighandler);
-    if (ret < 0)
-        return ret;
-
-    return 0;
-}
-
-static int _DkPersistentEventUpcall (int event_num, PAL_UPCALL upcall,
-                                     int flags)
-{
-    struct exception_handler * handler = pal_handlers[event_num];
-
-    _DkMutexLock(&handler->lock);
-    handler->upcall = upcall;
-    handler->flags = flags;
-    _DkMutexUnlock(&handler->lock);
-
-    return _DkPersistentSighandlerSetup(event_num);
-}
-
-static int _DkGenericEventUpcall (int event_num, PAL_UPCALL upcall,
-                                  int flags)
-{
-    int nsigs, * sigs = on_signals[event_num].signum;
-    for (nsigs = 0 ; sigs[nsigs] ; nsigs++);
-
-    void * sighandler = on_signals[event_num].handler;
-    struct exception_handler * handler = pal_handlers[event_num];
-    int ret = 0;
-
-    _DkMutexLock(&handler->lock);
-    handler->upcall = upcall;
-    handler->flags = flags;
-    _DkMutexUnlock(&handler->lock);
-
-    if (upcall)
-        ret = set_sighandler (sigs, nsigs, sighandler);
-    else
-        ret = block_signals (sigs, nsigs);
-
-    return ret;
-}
-
-static int _DkDummyEventUpcall (int event_num, PAL_UPCALL upcall,
-                                int flags)
-{
-    struct exception_handler * handler = pal_handlers[event_num];
-
-    _DkMutexLock(&handler->lock);
-    handler->upcall = upcall;
-    handler->flags = flags;
-    _DkMutexUnlock(&handler->lock);
-
-    return 0;
-}
-
-typedef void (*PAL_UPCALL) (PAL_PTR, PAL_NUM, PAL_CONTEXT *);
-
-int (*_DkExceptionHandlers[PAL_EVENT_NUM_BOUND])
-    (int, PAL_UPCALL, int) = {
-        /* reserved   */ NULL,
-        /* DivZero    */ &_DkPersistentEventUpcall,
-        /* MemFault   */ &_DkPersistentEventUpcall,
-        /* Illegal    */ &_DkPersistentEventUpcall,
-        /* Quit       */ &_DkGenericEventUpcall,
-        /* Suspend    */ &_DkGenericEventUpcall,
-        /* Resume     */ &_DkGenericEventUpcall,
-        /* Failure    */ &_DkDummyEventUpcall,
-    };
-
-static void _DkCompatibilitySighandler (int signum, siginfo_t * info,
-                                        ucontext_t * uc)
-{
-    /* not implemented */
-}
-
-void signal_setup (void)
-{
-    int ret, sig;
-    __sigemptyset(&bsd_state.sigset);
-
-    struct sigaction action;
-    action.sa_handler = (void (*)(int)) SIG_IGN;
-    action.sa_flags = 0;
-
-#if defined(__i386__)
-    ret = INLINE_SYSCALL(sigaction, 3, SIGCHLD, &action, NULL)
-#else
-    ret = INLINE_SYSCALL(sigaction, 4, SIGCHLD, &action, NULL,
-                         sizeof(sigset_t));
-#endif
-    if (IS_ERR(ret)) {
-        ret = -PAL_ERROR_DENIED;
-        goto err;
-    }
-
-    if ((ret = _DkPersistentEventUpcall(PAL_EVENT_ARITHMETIC_ERROR,  NULL, 0)) < 0)
-        goto err;
-
-    if ((ret = _DkPersistentEventUpcall(PAL_EVENT_MEMFAULT,  NULL, 0)) < 0)
-        goto err;
-
-    if ((ret = _DkPersistentEventUpcall(PAL_EVENT_ILLEGAL,  NULL, 0)) < 0)
-        goto err;
-
-    sig = SIGPIPE;
-    if ((ret = set_sighandler(&sig, 1, &_DkPipeSighandler)) < 0)
-        goto err;
-
-    sig = SIGSYS;
-    if ((ret = set_sighandler(&sig, 1, &_DkCompatibilitySighandler)) < 0)
-        goto err;
-
-    return;
-
-err:
-    INIT_FAIL(-ret, "cannot setup signal handlers");
-}
-
-void _DkExceptionReturn (void * event)
-{
-    const struct exception_event * e = (const struct exception_event *) event;
-    if (e->uc) {
-        /* copy the context back to ucontext */
-        e->uc->uc_mcontext.mc_r8 = e->context.r8;
-        e->uc->uc_mcontext.mc_r9 = e->context.r9;
-        e->uc->uc_mcontext.mc_r10 = e->context.r10;
-        e->uc->uc_mcontext.mc_r11 = e->context.r11;
-        e->uc->uc_mcontext.mc_r12 = e->context.r12;
-        e->uc->uc_mcontext.mc_r13 = e->context.r13;
-        e->uc->uc_mcontext.mc_r14 = e->context.r14;
-        e->uc->uc_mcontext.mc_r15 = e->context.r15;
-        e->uc->uc_mcontext.mc_rdi = e->context.rdi;
-        e->uc->uc_mcontext.mc_rsi = e->context.rsi;
-        e->uc->uc_mcontext.mc_rbp = e->context.rbp;
-        e->uc->uc_mcontext.mc_rbx = e->context.rbx;
-        e->uc->uc_mcontext.mc_rdx = e->context.rdx;
-        e->uc->uc_mcontext.mc_rax = e->context.rax;
-        e->uc->uc_mcontext.mc_rcx = e->context.rcx;
-        e->uc->uc_mcontext.mc_rsp = e->context.rsp;
-        e->uc->uc_mcontext.mc_rip = e->context.rip;
-        e->uc->uc_mcontext.mc_flags = e->context.efl;
-        e->uc->uc_mcontext.mc_err = e->context.err;
-        e->uc->uc_mcontext.mc_trapno = e->context.trapno;
-
-        /* return to the frame of exception handler */
-        asm volatile ("movq %0, %%rbp\r\n"
-                      "leaveq\r\n"
-                      "retq\r\n" :: "r"(e->eframe) : "memory");
-    }
-}

+ 0 - 457
Pal/src/host/FreeBSD/db_exception2.c

@@ -1,457 +0,0 @@
-/* Copyright (C) 2014 Stony Brook University
-   This file is part of Graphene Library OS.
-
-   Graphene Library OS is free software: you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public License
-   as published by the Free Software Foundation, either version 3 of the
-   License, or (at your option) any later version.
-
-   Graphene Library OS is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
-
-/*
- * db_signal.c
- *
- * This file contains APIs to set up handlers of exceptions issued by the
- * host, and the methods to pass the exceptions to the upcalls.
- */
-
-#include "pal_defs.h"
-#include "pal_freebsd_defs.h"
-#include "pal.h"
-#include "pal_internal.h"
-#include "pal_freebsd.h"
-#include "pal_error.h"
-#include "pal_security.h"
-#include "api.h"
-
-#include <atomic.h>
-#include <sigset.h>
-#include <ucontext.h>
-#include <errno.h>
-
-#if !defined(__i386__)
-/* In x86_64 kernels, sigaction is required to have a user-defined
- * restorer. Also, they not yet support SA_INFO. The reference:
- * http://lxr.linux.no/linux+v2.6.35/arch/x86/kernel/signal.c#L448
- *
- *     / * x86-64 should always use SA_RESTORER. * /
- *     if (ka->sa.sa_flags & SA_RESTORER) {
- *             put_user_ex(ka->sa.sa_restorer, &frame->pretcode);
- *     } else {
- *             / * could use a vstub here * /
- *             err |= -EFAULT;
- *     }
- */
-
-void restore_rt (void) asm ("__restore_rt");
-
-#ifndef SA_RESTORER
-#define SA_RESTORER  0x04000000
-#endif
-
-#define DEFINE_RESTORE_RT(syscall) DEFINE_RESTORE_RT2(syscall)
-# define DEFINE_RESTORE_RT2(syscall)                \
-    asm (                                           \
-         "    nop\n"                                \
-         ".align 16\n"                              \
-         ".LSTART_restore_rt:\n"                    \
-         "    .type __restore_rt,@function\n"       \
-         "__restore_rt:\n"                          \
-         "    movq $" #syscall ", %rax\n"           \
-         "    syscall\n");
-
-#endif
-
-int set_sighandler (int * sigs, int nsig, void * handler)
-{
-    struct sigaction action;
-
-    if (handler) {
-        action.sa_handler = (void (*)(int)) handler;
-        action.sa_flags = SA_SIGINFO;
-#if !defined(__i386__)
-        //action.sa_flags |= SA_RESTORER;
-        //action.sa_restorer = restore_rt;
-#endif
-    } else {
-        action.sa_handler = SIG_IGN;
-    }
-
-#ifdef DEBUG
-    if (!linux_state.in_gdb)
-#endif
-        action.sa_flags |= SA_NOCLDWAIT;
-
-    __sigemptyset((__sigset_t *) &action.sa_mask);
-    __sigaddset((__sigset_t *) &action.sa_mask, SIGCONT);
-
-    for (int i = 0 ; i < nsig ; i++) {
-#if defined(__i386__)
-        int ret = INLINE_SYSCALL(sigaction, 3, sigs[i], &action, NULL)
-#else
-        int ret = INLINE_SYSCALL(sigaction, 4, sigs[i], &action, NULL,
-                                 sizeof(sigset_t));
-#endif
-        if (IS_ERR(ret))
-            return -PAL_ERROR_DENIED;
-    }
-
-    return 0;
-}
-
-typedef struct {
-    PAL_IDX         event_num;
-    PAL_CONTEXT     context;
-    ucontext_t *    uc;
-    PAL_PTR         eframe;
-} PAL_EVENT;
-
-#define SIGNAL_MASK_TIME 1000
-
-static int get_event_num (int signum)
-{
-    switch(signum) {
-        case SIGFPE:                return PAL_EVENT_ARITHMETIC_ERROR;
-        case SIGSEGV: case SIGBUS:  return PAL_EVENT_MEMFAULT;
-        case SIGILL:  case SIGSYS:  return PAL_EVENT_ILLEGAL;
-        case SIGTERM:               return PAL_EVENT_QUIT;
-        case SIGINT:                return PAL_EVENT_SUSPEND;
-        case SIGCONT:               return PAL_EVENT_RESUME;
-        default: return -1;
-    }
-}
-
-static void _DkGenericEventTrigger (PAL_IDX event_num, PAL_EVENT_HANDLER upcall,
-                                    PAL_NUM arg, struct pal_frame * frame,
-                                    ucontext_t * uc, void * eframe)
-{
-    PAL_EVENT event;
-    event.event_num = event_num;
-
-    if (uc) {
-        event.context.r8  = uc->uc_mcontext.mc_r8;
-        event.context.r9  = uc->uc_mcontext.mc_r9;
-        event.context.r10 = uc->uc_mcontext.mc_r10;
-        event.context.r11 = uc->uc_mcontext.mc_r11;
-        event.context.r12 = uc->uc_mcontext.mc_r12;
-        event.context.r13 = uc->uc_mcontext.mc_r13;
-        event.context.r14 = uc->uc_mcontext.mc_r14;
-        event.context.r15 = uc->uc_mcontext.mc_r15;
-        event.context.rdi = uc->uc_mcontext.mc_rdi;
-        event.context.rsi = uc->uc_mcontext.mc_rsi;
-        event.context.rbp = uc->uc_mcontext.mc_rbp;
-        event.context.rbx = uc->uc_mcontext.mc_rbx;
-        event.context.rdx = uc->uc_mcontext.mc_rdx;
-        event.context.rax = uc->uc_mcontext.mc_rax;
-        event.context.rcx = uc->uc_mcontext.mc_rcx;
-        event.context.rsp = uc->uc_mcontext.mc_rsp;
-        event.context.rip = uc->uc_mcontext.mc_rip;
-        event.context.efl = uc->uc_mcontext.mc_flags;
-        event.context.err = uc->uc_mcontext.mc_err;
-        event.context.trapno = uc->uc_mcontext.mc_trapno;
-        event.context.csgsfs  = 0;
-        event.context.oldmask = 0;
-        event.context.cr2     = 0;
-    }
-
-    if (frame) {
-        event.context.r15 = frame->arch.r15;
-        event.context.r14 = frame->arch.r14;
-        event.context.r13 = frame->arch.r13;
-        event.context.r12 = frame->arch.r12;
-        event.context.rdi = frame->arch.rdi;
-        event.context.rsi = frame->arch.rsi;
-        event.context.rbx = frame->arch.rbx;
-        /* find last frame */
-        event.context.rsp = frame->arch.rbp + sizeof(unsigned long) * 2;
-        event.context.rbp = ((unsigned long *) frame->arch.rbp)[0];
-        event.context.rip = ((unsigned long *) frame->arch.rbp)[1];
-        /* making rax = 0 to tell the caller that this PAL call failed */
-        event.context.rax = 0;
-    }
-
-    event.uc = uc;
-    event.eframe = eframe;
-
-    (*upcall) ((PAL_PTR) &event, arg, &event.context);
-}
-
-static bool _DkGenericSignalHandle (int event_num, siginfo_t * info,
-                                    struct pal_frame * frame,
-                                    ucontext_t * uc, void * eframe)
-{
-    PAL_EVENT_HANDLER upcall = _DkGetExceptionHandler(event_num);
-
-    if (upcall) {
-        PAL_NUM arg = 0;
-
-        if (event_num == PAL_EVENT_ARITHMETIC_ERROR ||
-            event_num == PAL_EVENT_MEMFAULT ||
-            event_num == PAL_EVENT_ILLEGAL)
-            arg = (PAL_NUM) (info ? info->si_addr : 0);
-
-        _DkGenericEventTrigger(event_num, upcall, arg, frame, uc, eframe);
-        return true;
-    }
-
-    return false;
-}
-
-#define ADDR_IN_PAL(addr) \
-        ((void*)(addr) > TEXT_START && (void*)(addr) < TEXT_END)
-
-/* This function walks the stack to find the PAL_FRAME
- * that was saved upon entry to the PAL, if an exception/interrupt
- * comes in during a PAL call.  This is needed to support the behavior that an
- * exception in the PAL has Unix-style, EAGAIN semantics.
- *
- * The PAL_FRAME is supposed to be in the first PAL frame, and we look for
- * it by matching a special magic number, that should only appear on the stack
- * once.
- *
- * If an exception comes in while we are not in the PAL, this PAL_FRAME won't
- * exist, and it is ok to return NULL.
- */
-static struct pal_frame * get_frame (ucontext_t * uc)
-{
-    unsigned long rip = uc->uc_mcontext.mc_rip;
-    unsigned long rbp = uc->uc_mcontext.mc_rbp;
-    unsigned long last_rbp = rbp - 64;
-
-    if (!ADDR_IN_PAL(rip))
-        return NULL;
-
-    while (ADDR_IN_PAL(((unsigned long *) rbp)[1])) {
-        last_rbp = rbp;
-        rbp = *(unsigned long *) rbp;
-    }
-
-    /* search frame record in the top frame of PAL */
-    for (unsigned long ptr = rbp - sizeof(unsigned long) ;
-         ptr > last_rbp ; ptr -= 8) {
-        struct pal_frame * frame = (struct pal_frame *) ptr;
-        if (frame->identifier == PAL_FRAME_IDENTIFIER)
-            return frame;
-    }
-
-    return NULL;
-}
-
-static void return_frame (struct pal_frame * frame, int err)
-{
-    if (err)
-        _DkRaiseFailure(err);
-
-    __clear_frame(frame);
-    arch_restore_frame(&frame->arch);
-
-    asm volatile ("xor %rax, %rax\r\n"
-                  "leaveq\r\n"
-                  "retq\r\n");
-}
-
-#if BLOCK_SIGFAULT == 1
-static char exception_msg[24] = "--- SIGSEGV --- [     ]\n";
-static volatile bool cont_exec = false;
-#endif
-
-static void _DkGenericSighandler (int signum, siginfo_t * info,
-                                  struct ucontext * uc)
-{
-#if BLOCK_SIGFUALT == 1
-    /* resurrect this code if signal handler is giving segmentation fault */
-    if (signum == SIGSEGV) {
-        int pid = INLINE_SYSCALL(getpid, 0);
-        exception_msg[17] = '0' + pid / 10000;
-        exception_msg[18] = '0' + (pid / 1000) % 10;
-        exception_msg[19] = '0' + (pid / 100) % 10;
-        exception_msg[20] = '0' + (pid / 10) % 10;
-        exception_msg[21] = '0' + pid % 10;
-        INLINE_SYSCALL(write, 3, 1, exception_msg, 24);
-        while(!cont_exec);
-    }
-#endif
-
-    struct pal_frame * frame = get_frame(uc);
-    void * eframe;
-
-    if (signum == SIGCONT && frame && frame->func == DkObjectsWaitAny)
-        return;
-
-    asm volatile ("movq %%rbp, %0" : "=r"(eframe));
-
-    if (frame && frame->func != &_DkGenericSighandler &&
-        signum != SIGCONT &&
-        signum != SIGINT  &&
-        signum != SIGTERM) {
-        return_frame(frame, PAL_ERROR_BADADDR);
-        return;
-    }
-
-    int event_num = get_event_num(signum);
-    if (event_num == -1)
-        return;
-
-    _DkGenericSignalHandle(event_num, info, frame, uc, eframe);
-}
-
-static void _DkTerminateSighandler (int signum, siginfo_t * info,
-                                    struct ucontext * uc)
-{
-    struct pal_frame * frame = get_frame(uc);
-    void * eframe;
-
-    asm volatile ("movq %%rbp, %0" : "=r"(eframe));
-
-    int event_num = get_event_num(signum);
-    if (event_num == -1)
-        return;
-
-    if (!_DkGenericSignalHandle(event_num, NULL, frame, uc, eframe))
-        _DkThreadExit(/*clear_child_tid=*/NULL);
-}
-
-static void _DkPipeSighandler (int signum, siginfo_t * info,
-                               struct ucontext * uc)
-{
-    return;
-}
-
-void _DkRaiseFailure (int error)
-{
-    PAL_EVENT_HANDLER upcall = _DkGetExceptionHandler(PAL_EVENT_FAILURE);
-
-    if (!upcall)
-        return;
-
-    PAL_EVENT event;
-    event.event_num = PAL_EVENT_FAILURE;
-    event.uc = NULL;
-    event.eframe = NULL;
-
-    (*upcall) ((PAL_PTR) &event, error, NULL);
-}
-
-struct signal_ops {
-    int signum[3];
-    void (*handler) (int signum, siginfo_t * info, ucontext_t * uc);
-};
-
-struct signal_ops on_signals[] = {
-        [PAL_EVENT_ARITHMETIC_ERROR] = { .signum = { SIGFPE, 0 },
-                                         .handler = _DkGenericSighandler },
-        [PAL_EVENT_MEMFAULT]         = { .signum = { SIGSEGV, SIGBUS, 0 },
-                                         .handler = _DkGenericSighandler },
-        [PAL_EVENT_ILLEGAL]          = { .signum = { SIGILL,  SIGSYS, 0 },
-                                         .handler = _DkGenericSighandler },
-        [PAL_EVENT_QUIT]             = { .signum = { SIGTERM, 0, 0 },
-                                         .handler = _DkTerminateSighandler },
-        [PAL_EVENT_SUSPEND]          = { .signum = { SIGINT, 0 },
-                                         .handler = _DkTerminateSighandler },
-        [PAL_EVENT_RESUME]           = { .signum = { SIGCONT, 0 },
-                                         .handler = _DkGenericSighandler },
-    };
-
-static int _DkPersistentSighandlerSetup (int event_num)
-{
-    int nsigs, * sigs = on_signals[event_num].signum;
-    for (nsigs = 0 ; sigs[nsigs] ; nsigs++);
-
-    int ret = set_sighandler(sigs, nsigs, on_signals[event_num].handler);
-    if (ret < 0)
-        return ret;
-
-    return 0;
-}
-
-void signal_setup (void)
-{
-    int ret, sig = SIGCHLD;
-
-#ifdef DEBUG
-    if (!linux_state.in_gdb)
-#endif
-        set_sighandler(&sig, 1, NULL);
-
-    sig = SIGPIPE;
-    if ((ret = set_sighandler(&sig, 1, &_DkPipeSighandler)) < 0)
-        goto err;
-
-    int events[] = {
-        PAL_EVENT_ARITHMETIC_ERROR,
-        PAL_EVENT_MEMFAULT,
-        PAL_EVENT_ILLEGAL,
-        PAL_EVENT_QUIT,
-        PAL_EVENT_SUSPEND,
-        PAL_EVENT_RESUME,
-    };
-
-    for (int e = 0; e < ARRAY_SIZE(events); e++)
-        if ((ret = _DkPersistentSighandlerSetup(events[e])) < 0)
-            goto err;
-
-    return;
-err:
-    INIT_FAIL(-ret, "cannot setup signal handlers");
-}
-
-void _DkExceptionReturn (void * event)
-{
-    PAL_EVENT * e = event;
-
-    if (e->eframe) {
-        struct pal_frame * frame = (struct pal_frame *) e->eframe;
-        int err = 0;
-
-        switch (e->event_num) {
-            case PAL_EVENT_MEMFAULT:
-                err = PAL_ERROR_BADADDR;
-                break;
-            case PAL_EVENT_QUIT:
-            case PAL_EVENT_SUSPEND:
-            case PAL_EVENT_RESUME:
-                err = PAL_ERROR_INTERRUPTED;
-                break;
-        }
-
-        if (err)
-            _DkRaiseFailure(err);
-
-        __clear_frame(frame);
-    }
-
-    if (e->uc) {
-        /* copy the context back to ucontext */
-        e->uc->uc_mcontext.mc_r8 = e->context.r8;
-        e->uc->uc_mcontext.mc_r9 = e->context.r9;
-        e->uc->uc_mcontext.mc_r10 = e->context.r10;
-        e->uc->uc_mcontext.mc_r11 = e->context.r11;
-        e->uc->uc_mcontext.mc_r12 = e->context.r12;
-        e->uc->uc_mcontext.mc_r13 = e->context.r13;
-        e->uc->uc_mcontext.mc_r14 = e->context.r14;
-        e->uc->uc_mcontext.mc_r15 = e->context.r15;
-        e->uc->uc_mcontext.mc_rdi = e->context.rdi;
-        e->uc->uc_mcontext.mc_rsi = e->context.rsi;
-        e->uc->uc_mcontext.mc_rbp = e->context.rbp;
-        e->uc->uc_mcontext.mc_rbx = e->context.rbx;
-        e->uc->uc_mcontext.mc_rdx = e->context.rdx;
-        e->uc->uc_mcontext.mc_rax = e->context.rax;
-        e->uc->uc_mcontext.mc_rcx = e->context.rcx;
-        e->uc->uc_mcontext.mc_rsp = e->context.rsp;
-        e->uc->uc_mcontext.mc_rip = e->context.rip;
-        e->uc->uc_mcontext.mc_flags = e->context.efl;
-        e->uc->uc_mcontext.mc_err = e->context.err;
-        e->uc->uc_mcontext.mc_trapno = e->context.trapno;
-
-        /* return to the frame of exception handler */
-        asm volatile ("movq %0, %%rbp\r\n"
-                      "leaveq\r\n"
-                      "retq\r\n" :: "r"(e->eframe) : "memory");
-    }
-}

+ 0 - 554
Pal/src/host/FreeBSD/db_files.c

@@ -1,554 +0,0 @@
-/* Copyright (C) 2014 Stony Brook University
-   This file is part of Graphene Library OS.
-
-   Graphene Library OS is free software: you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public License
-   as published by the Free Software Foundation, either version 3 of the
-   License, or (at your option) any later version.
-
-   Graphene Library OS is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
-
-/*
- * db_files.c
- *
- * This file contains operands to handle streams with URIs that start with
- * "file:" or "dir:".
- */
-#undef __GLIBC__
-
-#include "pal_defs.h"
-#include "pal_freebsd_defs.h"
-#include "pal.h"
-#include "pal_internal.h"
-#include "pal_freebsd.h"
-#include "pal_debug.h"
-#include "pal_error.h"
-#include "api.h"
-
-#include <sys/stat.h>
-#include <sys/fcntl.h>
-#include <errno.h>
-
-#include <sys/types.h>
-typedef __kernel_pid_t pid_t;
-
-/* 'open' operation for file streams */
-static int file_open (PAL_HANDLE * handle, const char * type, const char * uri,
-                      int access, int share, int create, int options)
-{
-    int flags = HOST_FILE_OPEN(access, create, options);
-    int mode = HOST_PERM(share);
-
-    /* try to do the real open */
-    int ret = INLINE_SYSCALL(open, 3, uri, flags|O_CLOEXEC, mode);
-
-    if (IS_ERR(ret))
-        return unix_to_pal_error(ERRNO(ret));
-
-    /* if try_create_path succeeded, prepare for the file handle */
-    int len = strlen(uri);
-    PAL_HANDLE hdl = malloc(HANDLE_SIZE(file) + len + 1);
-    SET_HANDLE_TYPE(hdl, file);
-    hdl->hdr.flags |= RFD(0)|WFD(0)|WRITABLE(0);
-    hdl->file.fd = ret;
-    hdl->file.offset = 0;
-    hdl->file.append = 0;
-    hdl->file.pass = 0;
-    char * path = (void *) hdl + HANDLE_SIZE(file);
-    memcpy(path, uri, len + 1);
-    hdl->file.realpath = path;
-    *handle = hdl;
-    return 0;
-}
-
-#ifndef SEEK_SET
-# define SEEK_SET 0
-#endif
-
-/* 'read' operation for file streams. */
-static int file_read (PAL_HANDLE handle, int offset, int count,
-                      void * buffer)
-{
-    int fd = handle->file.fd;
-    int ret;
-    if (handle->file.offset != offset) {
-        ret = INLINE_SYSCALL(lseek, 3, fd, offset, SEEK_SET);
-
-        if (IS_ERR(ret))
-            return -PAL_ERROR_DENIED;
-
-        handle->file.offset = offset;
-    }
-
-    ret = INLINE_SYSCALL(read, 3, fd, buffer, count);
-
-    if (IS_ERR(ret))
-        return unix_to_pal_error(ERRNO(ret));
-
-    handle->file.offset = offset + ret;
-    return ret;
-}
-
-/* 'write' operation for file streams. */
-static int file_write (PAL_HANDLE handle, int offset, int count,
-                       const void * buffer)
-{
-    int fd = handle->file.fd;
-    int ret;
-
-    if (handle->file.offset != offset) {
-        ret = INLINE_SYSCALL(lseek, 3, fd, offset, SEEK_SET);
-        if (IS_ERR(ret))
-            return -PAL_ERROR_DENIED;
-
-        handle->file.offset = offset;
-    }
-
-    ret = INLINE_SYSCALL(write, 3, fd, buffer, count);
-
-    if (IS_ERR(ret))
-        return unix_to_pal_error(ERRNO(ret));
-
-    handle->file.offset = offset + ret;
-    return ret;
-}
-
-/* 'close' operation for file streams. In this case, it will only
-   close the file withou deleting it. */
-static int file_close (PAL_HANDLE handle)
-{
-    int fd = handle->file.fd;
-
-    int ret = INLINE_SYSCALL(close, 1, fd);
-
-    /* initial realpath is part of handle object and will be freed with it */
-    if (handle->file.realpath &&
-        handle->file.realpath != (void *) handle + HANDLE_SIZE(file)) {
-        free((void *) handle->file.realpath);
-    }
-    return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : 0;
-}
-
-/* 'delete' operation for file streams. It will actually delete
-   the file if we can successfully close it. */
-static int file_delete (PAL_HANDLE handle, int access)
-{
-    if (access)
-        return -PAL_ERROR_INVAL;
-
-    INLINE_SYSCALL(unlink, 1, handle->file.realpath);
-    return 0;
-}
-
-/* 'map' operation for file stream. */
-static int file_map (PAL_HANDLE handle, void ** addr, int prot,
-                     int offset, int size)
-{
-    int fd = handle->file.fd;
-    void * mem = *addr;
-    int flags = MAP_FILE|HOST_FLAGS(0, prot)|(mem ? MAP_FIXED : 0);
-    prot = HOST_PROT(prot);
-
-    /* The memory will always allocated with flag MAP_PRIVATE
-       and MAP_FILE */
-
-    mem = (void *) ARCH_MMAP(mem, size, prot, flags, fd, offset);
-
-    if (IS_ERR_P(mem))
-        return -PAL_ERROR_DENIED;
-
-    *addr = mem;
-    return 0;
-}
-
-/* 'setlength' operation for file stream. */
-static int file_setlength (PAL_HANDLE handle, int length)
-{
-    int ret = INLINE_SYSCALL(ftruncate, 2, handle->file.fd, length);
-
-    if (IS_ERR(ret))
-        return (ERRNO(ret) == EINVAL || ERRNO(ret) == EBADF) ?
-               -PAL_ERROR_BADHANDLE : -PAL_ERROR_DENIED;
-
-    return length;
-}
-
-/* 'flush' operation for file stream. */
-static int file_flush (PAL_HANDLE handle)
-{
-    int ret = INLINE_SYSCALL(fsync, 1, handle->file.fd);
-
-    if (IS_ERR(ret))
-        return (ERRNO(ret) == EINVAL || ERRNO(ret) == EBADF) ?
-               -PAL_ERROR_BADHANDLE : -PAL_ERROR_DENIED;
-
-    return 0;
-}
-
-static inline int file_stat_type (struct stat * stat)
-{
-    if (S_ISREG(stat->st_mode))
-        return pal_type_file;
-    if (S_ISDIR(stat->st_mode))
-        return pal_type_dir;
-    if (S_ISCHR(stat->st_mode))
-        return pal_type_dev;
-    if (S_ISFIFO(stat->st_mode))
-        return pal_type_pipe;
-    if (S_ISSOCK(stat->st_mode))
-        return pal_type_dev;
-
-    return 0;
-}
-
-/* copy attr content from POSIX stat struct to PAL_STREAM_ATTR */
-static inline void
-file_attrcopy (PAL_STREAM_ATTR * attr, struct stat * stat)
-{
-    attr->handle_type  = file_stat_type(stat);
-    attr->disconnected = PAL_FALSE;
-    attr->nonblocking  = PAL_FALSE;
-    attr->readable     = stataccess(stat, ACCESS_R);
-    attr->writable     = stataccess(stat, ACCESS_W);
-    attr->runnable     = stataccess(stat, ACCESS_X);
-    attr->share_flags  = stat->st_mode;
-    attr->pending_size = stat->st_size;
-}
-
-/* 'attrquery' operation for file streams */
-static int file_attrquery (const char * type, const char * uri,
-                           PAL_STREAM_ATTR * attr)
-{
-    struct stat stat_buf;
-    /* try to do the real open */
-    int ret = INLINE_SYSCALL(stat, 2, uri, &stat_buf);
-
-    /* if it failed, return the right error code */
-    if (IS_ERR(ret))
-        return unix_to_pal_error(ERRNO(ret));
-
-    file_attrcopy(attr, &stat_buf);
-    return 0;
-}
-
-/* 'attrquerybyhdl' operation for file streams */
-static int file_attrquerybyhdl (PAL_HANDLE handle,
-                                PAL_STREAM_ATTR * attr)
-{
-    int fd = handle->hdr.fds[0];
-    struct stat stat_buf;
-
-    int ret = INLINE_SYSCALL(fstat, 2, fd, &stat_buf);
-
-    if (IS_ERR(ret))
-        return unix_to_pal_error(ERRNO(ret));
-
-    file_attrcopy(attr, &stat_buf);
-    return 0;
-}
-
-static int file_attrsetbyhdl (PAL_HANDLE handle,
-                              PAL_STREAM_ATTR * attr)
-{
-    int fd = handle->hdr.fds[0], ret;
-
-    ret = INLINE_SYSCALL(fchmod, 2, fd, attr->share_flags);
-    if (IS_ERR(ret))
-        return unix_to_pal_error(ERRNO(ret));
-
-    return 0;
-}
-
-static int file_rename (PAL_HANDLE handle, const char * type,
-                        const char * uri)
-{
-    char* tmp = strdup(uri);
-    if (!tmp)
-        return -PAL_ERROR_NOMEM;
-
-    int ret = INLINE_SYSCALL(rename, 2, handle->file.realpath, uri);
-    if (IS_ERR(ret)) {
-        free(tmp);
-        return unix_to_pal_error(ERRNO(ret));
-    }
-
-    /* initial realpath is part of handle object and will be freed with it */
-    if (handle->file.realpath &&
-            handle->file.realpath != (void *) handle + HANDLE_SIZE(file)) {
-        free((void *) handle->file.realpath);
-    }
-
-    handle->file.realpath = tmp;
-    return 0;
-}
-
-static int file_getname (PAL_HANDLE handle, char * buffer, int count)
-{
-    if (!handle->file.realpath)
-        return 0;
-
-    int len = strlen(handle->file.realpath);
-    char * tmp = strcpy_static(buffer, URI_PREFIX_FILE, count);
-
-    if (!tmp || buffer + count < tmp + len + 1)
-        return -PAL_ERROR_TOOLONG;
-
-    memcpy(tmp, handle->file.realpath, len + 1);
-    return tmp + len - buffer;
-}
-
-const char * file_getrealpath (PAL_HANDLE handle)
-{
-    return handle->file.realpath;
-}
-
-struct handle_ops file_ops = {
-        .getname            = &file_getname,
-        .getrealpath        = &file_getrealpath,
-        .open               = &file_open,
-        .read               = &file_read,
-        .write              = &file_write,
-        .close              = &file_close,
-        .delete             = &file_delete,
-        .map                = &file_map,
-        .setlength          = &file_setlength,
-        .flush              = &file_flush,
-        .attrquery          = &file_attrquery,
-        .attrquerybyhdl     = &file_attrquerybyhdl,
-        .attrsetbyhdl       = &file_attrsetbyhdl,
-        .rename             = &file_rename,
-    };
-
-/* 'open' operation for directory stream. Directory stream does not have a
-   specific type prefix, its URI looks the same file streams, plus it
-   ended with slashes. dir_open will be called by file_open. */
-static int dir_open (PAL_HANDLE * handle, const char * type, const char * uri,
-                     int access, int share, int create, int options)
-{
-    int ret;
-    int mode = HOST_PERM(share);
-
-    if (create & PAL_CREATE_TRY) {
-        ret = INLINE_SYSCALL(mkdir, 2, uri, mode);
-
-        if (IS_ERR(ret) && ERRNO(ret) == EEXIST &&
-            create & PAL_CREATE_ALWAYS)
-            return -PAL_ERROR_STREAMEXIST;
-    }
-
-    ret = INLINE_SYSCALL(open, 3, uri, O_DIRECTORY|options|O_CLOEXEC, 0);
-
-    if (IS_ERR(ret))
-        return unix_to_pal_error(ERRNO(ret));
-
-    int len = strlen(uri);
-    PAL_HANDLE hdl = malloc(HANDLE_SIZE(dir) + len + 1);
-    SET_HANDLE_TYPE(hdl, dir);
-    hdl->hdr.flags |= RFD(0);
-    hdl->dir.fd = ret;
-    char * path = (void *) hdl + HANDLE_SIZE(dir);
-    memcpy(path, uri, len + 1);
-    hdl->dir.realpath = path;
-    hdl->dir.buf = NULL;
-    hdl->dir.ptr = NULL;
-    hdl->dir.end = NULL;
-    hdl->dir.endofstream = false;
-    *handle = hdl;
-    return 0;
-}
-
-struct dirent {
-        __uint32_t d_fileno;            /* file number of entry */
-        __uint16_t d_reclen;            /* length of this record */
-        __uint8_t  d_type;              /* file type, see below */
-        __uint8_t  d_namlen;            /* length of string in d_name */
-        char    d_name[255 + 1];        /* name must be no longer than this */
-};
-
-#define DT_UNKNOWN      0
-#define DT_FIFO         1
-#define DT_CHR          2
-#define DT_DIR          4
-#define DT_BLK          6
-#define DT_REG          8
-#define DT_LNK          10
-#define DT_SOCK         12
-#define DT_WHT          14
-
-#define DIRBUF_SIZE     1024
-
-/* 'read' operation for directory stream. Directory stream will not
-   need a 'write' operation. */
-int dir_read (PAL_HANDLE handle, int offset, int count, void * buf)
-{
-    void * dent_buf = handle->dir.buf ? : __alloca(DIRBUF_SIZE);
-    void * ptr = handle->dir.ptr;
-    void * end = handle->dir.end;
-    int bytes = 0;
-
-    if (ptr && ptr < end)
-        goto output;
-
-    do {
-        if (handle->dir.endofstream)
-            break;
-
-        int size = INLINE_SYSCALL(getdents, 3, handle->dir.fd, dent_buf,
-                                  DIRBUF_SIZE);
-
-        if (IS_ERR(size))
-            return -PAL_ERROR_DENIED;
-
-        if (size == 0) {
-            handle->dir.endofstream = PAL_TRUE;
-            break;
-        }
-
-        ptr = dent_buf;
-        end = dent_buf + size;
-
-output:
-        while (ptr < end) {
-            struct dirent * d = (struct dirent *) ptr;
-
-            if (d->d_name[0] == '.' &&
-                (!d->d_name[1] || d->d_name[1] == '.'))
-                goto next;
-
-            bool isdir = (d->d_type == DT_DIR);
-            int len = d->d_namlen;
-            if (len + (isdir ? 2 : 1) > count)
-                break;
-
-            memcpy(buf, d->d_name, len);
-            if (isdir)
-                ((char *) buf)[len++] = '/';
-            ((char *) buf)[len++] = '\0';
-
-            bytes += len;
-            buf += len;
-            count -= len;
-next:
-            ptr += d->d_reclen;
-        }
-    } while (ptr == end);
-
-    if (ptr < end) {
-        if (!handle->dir.buf)
-            handle->dir.buf = malloc(DIRBUF_SIZE);
-
-        if (handle->dir.buf != ptr) {
-            memmove(handle->dir.buf, ptr, end - ptr);
-            end = handle->dir.buf + (end - ptr);
-            ptr = handle->dir.buf;
-        }
-
-        if (!bytes)
-            return -PAL_ERROR_OVERFLOW;
-    }
-
-    return bytes ? : -PAL_ERROR_ENDOFSTREAM;
-}
-
-/* 'close' operation of directory streams */
-static int dir_close (PAL_HANDLE handle)
-{
-    int fd = handle->dir.fd;
-
-    int ret = INLINE_SYSCALL(close, 1, fd);
-
-    if (handle->dir.buf) {
-        free(handle->dir.buf);
-        handle->dir.buf = handle->dir.ptr = handle->dir.end = NULL;
-    }
-
-    /* initial realpath is part of handle object and will be freed with it */
-    if (handle->dir.realpath &&
-        handle->dir.realpath != (void *) handle + HANDLE_SIZE(dir)) {
-        free((void *) handle->dir.realpath);
-    }
-
-    if (IS_ERR(ret))
-        return -PAL_ERROR_BADHANDLE;
-
-    return 0;
-}
-
-/* 'delete' operation of directoy streams */
-static int dir_delete (PAL_HANDLE handle, int access)
-{
-    if (access)
-        return -PAL_ERROR_INVAL;
-
-    int ret = dir_close(handle);
-
-    if (ret < 0)
-        return ret;
-
-    ret = INLINE_SYSCALL(rmdir, 1, handle->dir.realpath);
-
-    return (IS_ERR(ret) && ERRNO(ret) != ENOENT) ?
-           -PAL_ERROR_DENIED : 0;
-}
-
-static int dir_rename (PAL_HANDLE handle, const char * type,
-                       const char * uri)
-{
-    char* tmp = strdup(uri);
-    if (!tmp)
-        return -PAL_ERROR_NOMEM;
-
-    int ret = INLINE_SYSCALL(rename, 2, handle->dir.realpath, uri);
-    if (IS_ERR(ret)) {
-        free(tmp);
-        return unix_to_pal_error(ERRNO(ret));
-    }
-
-    /* initial realpath is part of handle object and will be freed with it */
-    if (handle->dir.realpath &&
-            handle->dir.realpath != (void *) handle + HANDLE_SIZE(dir)) {
-        free((void *) handle->dir.realpath);
-    }
-
-    handle->dir.realpath = tmp;
-    return 0;
-}
-
-static int dir_getname (PAL_HANDLE handle, char * buffer, int count)
-{
-    if (!handle->dir.realpath)
-        return 0;
-
-    int len = strlen(handle->dir.realpath);
-    char * tmp = strcpy_static(buffer, URI_PREFIX_FILE, count);
-
-    if (!tmp || buffer + count < tmp + len + 1)
-        return -PAL_ERROR_TOOLONG;
-
-    memcpy(tmp, handle->dir.realpath, len + 1);
-    return tmp + len - buffer;
-}
-
-static const char * dir_getrealpath (PAL_HANDLE handle)
-{
-    return handle->dir.realpath;
-}
-
-struct handle_ops dir_ops = {
-        .getname            = &dir_getname,
-        .getrealpath        = &dir_getrealpath,
-        .open               = &dir_open,
-        .read               = &dir_read,
-        .close              = &dir_close,
-        .delete             = &dir_delete,
-        .attrquery          = &file_attrquery,
-        .attrquerybyhdl     = &file_attrquerybyhdl,
-        .attrsetbyhdl       = &file_attrsetbyhdl,
-        .rename             = &dir_rename,
-    };

+ 0 - 419
Pal/src/host/FreeBSD/db_main.c

@@ -1,419 +0,0 @@
-/* Copyright (C) 2014 Stony Brook University
-   This file is part of Graphene Library OS.
-
-   Graphene Library OS is free software: you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public License
-   as published by the Free Software Foundation, either version 3 of the
-   License, or (at your option) any later version.
-
-   Graphene Library OS is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
-
-/*
- * db_main.c
- *
- * This file contains the main function of the PAL loader, which loads and
- * processes environment, arguments and manifest.
- */
-
-#include "pal_defs.h"
-#include "pal_freebsd_defs.h"
-#include "pal.h"
-#include "pal_internal.h"
-#include "pal_freebsd.h"
-#include "pal_debug.h"
-#include "pal_error.h"
-#include "pal_security.h"
-#include "api.h"
-
-#include <sys/mman.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <elf/elf.h>
-#include <sysdeps/generic/ldsodefs.h>
-#include <sys/types.h>
-
-/* At the begining of entry point, rsp starts at argc, then argvs,
-   envps and auxvs. Here we store rsp to rdi, so it will not be
-   messed up by function calls */
-asm (".global pal_start \n"
-     "  .type pal_start,@function \n"
-     "pal_start: \n "
-     "  movq %rsp, %rdi \n"
-     "  call pal_bsd_main@PLT \n");
-
-#define RTLD_BOOTSTRAP
-
-/* pal_start is the entry point of libpal.so, which calls pal_main */
-#define _ENTRY pal_start
-
-struct pal_bsd_state bsd_state;
-struct pal_sec pal_sec;
-
-static size_t g_page_size = PRESET_PAGESIZE;
-static uid_t uid;
-static gid_t gid;
-
-static void pal_init_bootstrap (void * args, const char ** pal_name,
-                                int * pargc,
-                                const char *** pargv,
-                                const char *** penvp,
-                                ElfW(Addr) * baseaddr)
-{
-    /*
-     * fetch arguments and environment variables, the previous stack
-     * pointer is in rdi (arg). The stack structure starting at rdi
-     * will look like:
-     *            auxv[m - 1] = AT_NULL
-     *            ...
-     *            auxv[0]
-     *            envp[n - 1] = NULL
-     *            ...
-     *            envp[0]
-     *            argv[argc] = NULL
-     *            argv[argc - 1]
-     *            ...
-     *            argv[0]
-     *            argc
-     *       ---------------------------------------
-     *            user stack
-     */
-    const char ** all_args = (const char **) args;
-
-    /* Workaround because sometimes BSD misaligns arguments on stack */
-    if (all_args[0] == 0)
-        all_args++;
-
-    int argc = (uintptr_t) all_args[0];
-    const char ** argv = &all_args[1];
-    const char ** envp = argv + argc + 1;
-
-    /* fetch environment information from aux vectors */
-    void ** auxv = (void **) envp + 1;
-    for (; *(auxv - 1); auxv++);
-    ElfW(auxv_t) *av;
-    ElfW(Addr) base = 0;
-    for (av = (ElfW(auxv_t) *)auxv ; av->a_type != AT_NULL ; av++)
-        switch (av->a_type) {
-            case AT_PAGESZ:
-                g_page_size = av->a_un.a_val;
-                break;
-            case AT_UID:
-            case AT_EUID:
-                uid ^= av->a_un.a_val;
-                break;
-            case AT_GID:
-            case AT_EGID:
-                gid ^= av->a_un.a_val;
-                break;
-            case AT_BASE:
-                base = (ElfW(Addr)) av->a_un.a_val;
-                break;
-        }
-
-    *pal_name = argv[0];
-    argv++;
-    argc--;
-    *pargc = argc;
-    *pargv = argv;
-    *penvp = envp;
-    *baseaddr = base;
-}
-
-unsigned long _DkGetPagesize (void)
-{
-    return g_page_size;
-}
-
-unsigned long _DkGetAllocationAlignment (void)
-{
-    return g_page_size;
-}
-
-void _DkGetAvailableUserAddressRange (PAL_PTR * start, PAL_PTR * end,
-                                      PAL_PTR * hole_start, PAL_PTR * hole_end)
-{
-    void * end_addr, * start_addr;
-
-    if ((void *) TEXT_START - (void *) USER_ADDRESS_LOWEST >
-        (void *) USER_ADDRESS_HIGHEST - (void *) DATA_END){
-        end_addr = (void*)ALLOC_ALIGN_DOWN(TEXT_START);
-        start_addr = pal_sec.user_addr_base ? :
-            (void *) USER_ADDRESS_LOWEST;
-    } else {
-        end_addr = (void*)USER_ADDRESS_HIGHEST;
-        start_addr = (void*)ALLOC_ALIGN_UP(DATA_END);
-    }
-
-    assert(IS_ALLOC_ALIGNED(start_addr) && IS_ALLOC_ALIGNED(end_addr));
-
-    while (1) {
-        if (start_addr >= end_addr)
-            INIT_FAIL(PAL_ERROR_NOMEM, "no user memory available");
-
-        void * mem = (void *) ARCH_MMAP(start_addr,
-                                        pal_state.alloc_align,
-                                        PROT_NONE,
-                                        MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE,
-                                        -1, 0);
-        if (!IS_ERR_P(mem)) {
-            INLINE_SYSCALL(munmap, 2, mem, pal_state.alloc_align);
-            if (mem == start_addr)
-                break;
-        }
-
-        start_addr = (void *) ((unsigned long) start_addr << 1);
-    }
-
-    *end   = (PAL_PTR) end_addr;
-    *start = (PAL_PTR) start_addr;
-
-    // Not used, so set it to an empty range.
-    *hole_start = start_addr;
-    *hole_end = start_addr;
-}
-
-PAL_NUM _DkGetProcessId (void)
-{
-    return (bsd_state.start_time & (~0xffff)) | bsd_state.pid;
-}
-
-PAL_NUM _DkGetHostId (void)
-{
-    return 0;
-}
-
-#include "dynamic_link.h"
-
-void setup_pal_map (struct link_map * map);
-
-static struct link_map pal_map;
-
-#ifdef __x86_64__
-# include "elf-x86_64.h"
-#endif
-
-void pal_bsd_main (void * args)
-{
-    const char * pal_name = NULL;
-    PAL_HANDLE parent = NULL, exec = NULL, manifest = NULL;
-    const char ** argv, ** envp;
-    int argc;
-
-    struct timeval time;
-    INLINE_SYSCALL(gettimeofday, 2, &time, NULL);
-
-    /* parse argc, argv, envp and auxv */
-    pal_init_bootstrap(args, &pal_name, &argc, &argv, &envp, &pal_map.l_addr);
-    pal_map.l_name = pal_name;
-    elf_get_dynamic_info((void *) pal_map.l_addr + elf_machine_dynamic(),
-                         pal_map.l_info, pal_map.l_addr);
-    ELF_DYNAMIC_RELOCATE(&pal_map);
-
-    init_slab_mgr(g_page_size);
-    setup_pal_map(&pal_map);
-
-    bsd_state.start_time = 1000000ULL * time.tv_sec + time.tv_usec;
-    bsd_state.pid = INLINE_SYSCALL(getpid, 0);
-    bsd_state.uid = uid;
-    bsd_state.gid = gid;
-
-    if (!bsd_state.parent_pid)
-        bsd_state.parent_pid = bsd_state.pid;
-
-    PAL_HANDLE first_thread = malloc(HANDLE_SIZE(thread));
-    SET_HANDLE_TYPE(first_thread, thread);
-    first_thread->thread.tid = bsd_state.pid;
-
-    init_child_process(&parent, &exec, &manifest);
-    if (parent)
-        goto done_init;
-
-    int fd = INLINE_SYSCALL(open, 3, argv[0], O_RDONLY|O_CLOEXEC, 0);
-    if (IS_ERR(fd))
-        goto done_init;
-
-
-    size_t len = strlen(argv[0]) + 1;
-    PAL_HANDLE file = malloc(HANDLE_SIZE(file) + len);
-    SET_HANDLE_TYPE(file, file);
-    file->hdr.flags |= RFD(0)|WFD(0)|WRITABLE(0);
-    file->file.fd = fd;
-
-    char * path = (void *) file + HANDLE_SIZE(file);
-    int ret = get_norm_path(argv[0], path, &len);
-    if (ret < 0) {
-        printf("Could not normalize path (%s): %s\n", argv[0], pal_strerror(ret));
-        goto done_init;
-    }
-    file->file.realpath = path;
-
-    if (!check_elf_object(file)) {
-        exec = file;
-        goto done_init;
-
-    }
-    manifest = file;
-
-done_init:
-    if (!parent && !exec && !manifest) {
-        printf("USAGE: %s [executable|manifest] args ...\n", pal_name);
-        _DkProcessExit(0);
-        return;
-    }
-    signal_setup();
-
-    /* jump to main function */
-    pal_main(bsd_state.parent_pid, manifest, exec, NULL, parent, first_thread, argv, envp);
-}
-
-/* the following code is borrowed from CPUID */
-
-static void cpuid (int cpuid_fd, unsigned int reg,
-                   unsigned int words[], unsigned int ecx)
-{
-   asm("cpuid"
-       : "=a" (words[PAL_CPUID_WORD_EAX]),
-         "=b" (words[PAL_CPUID_WORD_EBX]),
-         "=c" (words[PAL_CPUID_WORD_ECX]),
-         "=d" (words[PAL_CPUID_WORD_EDX])
-       : "a" (reg),
-         "c" (ecx));
-}
-
-#define FOUR_CHARS_VALUE(s, w)      \
-    (s)[0] = (w) & 0xff;            \
-    (s)[1] = ((w) >>  8) & 0xff;    \
-    (s)[2] = ((w) >> 16) & 0xff;    \
-    (s)[3] = ((w) >> 24) & 0xff;
-
-#define BPI  32
-#define POWER2(power) (1ULL << (power))
-#define RIGHTMASK(width) \
-   (((unsigned long)(width) >= BPI) ? ~0ULL : POWER2(width) - 1ULL)
-
-#define BIT_EXTRACT_LE(value, start, after) \
-   (((unsigned long)(value) & RIGHTMASK(after)) >> (start))
-
-
-static char * cpu_flags[]
-       = { "fpu",    // "x87 FPU on chip"
-           "vme",    // "virtual-8086 mode enhancement"
-           "de",     // "debugging extensions"
-           "pse",    // "page size extensions"
-           "tsc",    // "time stamp counter"
-           "msr",    // "RDMSR and WRMSR support"
-           "pae",    // "physical address extensions"
-           "mce",    // "machine check exception"
-           "cx8",    // "CMPXCHG8B inst."
-           "apic",   // "APIC on chip"
-           NULL,
-           "sep",    // "SYSENTER and SYSEXIT"
-           "mtrr",   // "memory type range registers"
-           "pge",    // "PTE global bit"
-           "mca",    // "machine check architecture"
-           "cmov",   // "conditional move/compare instruction"
-           "pat",    // "page attribute table"
-           "pse36",  // "page size extension"
-           "pn",     // "processor serial number"
-           "clflush",    // "CLFLUSH instruction"
-           NULL,
-           "dts"     // "debug store"
-           "tm",     // "thermal monitor and clock ctrl"
-           "mmx",    // "MMX Technology"
-           "fxsr",   // "FXSAVE/FXRSTOR"
-           "sse",    // "SSE extensions"
-           "sse2",   // "SSE2 extensions"
-           "ss",     // "self snoop"
-           "ht",     // "hyper-threading / multi-core supported"
-           "tm",     // "therm. monitor"
-           "ia64",   // "IA64"
-           "pbe",    // "pending break event"
-        };
-
-/*
- * Returns the number of online CPUs read from sysctl hw.ncpus, -errno on failure.
- */
-int get_cpu_count(void) {
-    int mib[2], cores;
-    size_t len = sizeof(cores);
-    mib[0] = CTL_HW;
-    mib[1] = HW_NCPU;
-
-    int rv = sysctl(mib, 2, &cores, &len, NULL, 0);
-    if (rv < 0)
-        return unix_to_pal_error(ERRNO(rv));
-
-    return cores;
-}
-
-int _DkGetCPUInfo (PAL_CPU_INFO * ci)
-{
-    unsigned int words[PAL_CPUID_WORD_NUM];
-
-    const size_t VENDOR_ID_SIZE = 13;
-    char* vendor_id = malloc(VENDOR_ID_SIZE);
-    cpuid(2, 0, words, 0);
-
-    FOUR_CHARS_VALUE(&vendor_id[0], words[PAL_CPUID_WORD_EBX]);
-    FOUR_CHARS_VALUE(&vendor_id[4], words[PAL_CPUID_WORD_EDX]);
-    FOUR_CHARS_VALUE(&vendor_id[8], words[PAL_CPUID_WORD_ECX]);
-    vendor_id[VENDOR_ID_SIZE - 1] = '\0';
-    ci->cpu_vendor = vendor_id;
-
-    const size_t BRAND_SIZE = 49;
-    char* brand = malloc(BRAND_SIZE);
-    cpuid(-2, 0x80000002, words, 0);
-    memcpy(&brand[ 0], words, sizeof(unsigned int) * PAL_CPUID_WORD_NUM);
-    cpuid(-2, 0x80000003, words, 0);
-    memcpy(&brand[16], words, sizeof(unsigned int) * PAL_CPUID_WORD_NUM);
-    cpuid(-2, 0x80000004, words, 0);
-    memcpy(&brand[32], words, sizeof(unsigned int) * PAL_CPUID_WORD_NUM);
-    brand[BRAND_SIZE - 1] = '\0';
-    ci->cpu_brand = brand;
-
-    cpuid(2, 1, words, 0);
-    ci->cpu_family   = BIT_EXTRACT_LE(words[PAL_CPUID_WORD_EAX],  8, 12) + 1;
-    ci->cpu_model    = BIT_EXTRACT_LE(words[PAL_CPUID_WORD_EAX],  4,  8);
-    ci->cpu_stepping = BIT_EXTRACT_LE(words[PAL_CPUID_WORD_EAX],  0,  4);
-
-    /* we cannot use CPUID(0xb) because it counts even disabled-by-BIOS cores (e.g. HT cores);
-     * instead we extract info on number of online CPUs by parsing sysfs pseudo-files */
-    int cores = get_cpu_count();
-    if (cores < 0) {
-        free(brand);
-        free(vendor_id);
-        return cores;
-    }
-    ci->cpu_num = cores;
-
-    int flen = 0, fmax = 80;
-    char * flags = malloc(fmax);
-
-    for (int i = 0 ; i < 32 ; i++) {
-        if (!cpu_flags[i])
-            break;
-
-        if (BIT_EXTRACT_LE(words[PAL_CPUID_WORD_EDX], i, i + 1)) {
-            int len = strlen(cpu_flags[i]);
-            if (flen + len + 1 > fmax) {
-                char * new_flags = malloc(fmax * 2);
-                memcpy(new_flags, flags, flen);
-                free(flags);
-                fmax *= 2;
-                flags = new_flags;
-            }
-            memcpy(flags + flen, cpu_flags[i], len);
-            flen += len;
-            flags[flen++] = ' ';
-        }
-    }
-
-    flags[flen ? flen - 1 : 0] = 0;
-    ci->cpu_flags = flags;
-}

+ 0 - 111
Pal/src/host/FreeBSD/db_memory.c

@@ -1,111 +0,0 @@
-/* Copyright (C) 2014 Stony Brook University
-   This file is part of Graphene Library OS.
-
-   Graphene Library OS is free software: you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public License
-   as published by the Free Software Foundation, either version 3 of the
-   License, or (at your option) any later version.
-
-   Graphene Library OS is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
-
-/*
- * db_memory.c
- *
- * This files contains APIs that allocate, free or protect virtual memory.
- */
-
-#include <sys/mman.h>
-#include <sys/sysctl.h>
-#include <sys/vmmeter.h>
-
-#include "api.h"
-#include "pal.h"
-#include "pal_debug.h"
-#include "pal_defs.h"
-#include "pal_error.h"
-#include "pal_freebsd.h"
-#include "pal_freebsd_defs.h"
-#include "pal_internal.h"
-
-bool _DkCheckMemoryMappable(const void* addr, size_t size) {
-    return (addr <= DATA_END && addr + size >= TEXT_START);
-}
-int _DkVirtualMemoryAlloc(void** paddr, uint64_t size, int alloc_type, int prot) {
-    void* addr = *paddr;
-    void* mem = addr;
-
-    int flags = HOST_FLAGS(alloc_type, prot | PAL_PROT_WRITECOPY);
-    prot      = HOST_PROT(prot);
-
-    flags |= MAP_ANONYMOUS | (addr ? MAP_FIXED : 0);
-    mem = (void*)ARCH_MMAP(addr, size, prot, flags, -1, 0);
-
-    if (IS_ERR_P(mem))
-        return unix_to_pal_error(ERRNO_P(mem));
-
-    *paddr = mem;
-    return 0;
-}
-
-int _DkVirtualMemoryFree(void* addr, uint64_t size) {
-    int ret = INLINE_SYSCALL(munmap, 2, addr, size);
-
-    return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : 0;
-}
-
-int _DkVirtualMemoryProtect(void* addr, uint64_t size, int prot) {
-    int ret = INLINE_SYSCALL(mprotect, 3, addr, size, HOST_PROT(prot));
-
-    return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : 0;
-}
-
-#define MEM_TOTAL 1
-#define MEM_FREE  2
-
-#define VM_TOTAL 1
-
-static int get_meminfo(int key, unsigned long* val) {
-    int mib[2], len;
-    struct vmtotal vm;
-
-    int ret = -PAL_ERROR_DENIED;
-
-    switch (key) {
-        case MEM_TOTAL:
-            mib[0] = CTL_HW;
-            mib[1] = HW_REALMEM;
-            len    = sizeof(val);
-            ret    = INLINE_SYSCALL(__sysctl, 6, mib, 2, val, &len, NULL, 0);
-            break;
-        case MEM_FREE:
-            mib[0] = CTL_VM;
-            mib[1] = VM_TOTAL;
-            len    = sizeof(vm);
-            ret    = INLINE_SYSCALL(__sysctl, 6, mib, 2, &vm, &len, NULL, 0);
-            *val   = vm.t_free * 4096;
-            break;
-    }
-    return ret;
-}
-
-unsigned long _DkMemoryQuota(void) {
-    if (bsd_state.memory_quota)
-        return bsd_state.memory_quota;
-    unsigned long quota = 0;
-    if (get_meminfo(MEM_TOTAL, &quota) < 0)
-        return 0;
-    return quota * 1024;
-}
-
-unsigned long _DkMemoryAvailableQuota(void) {
-    unsigned long quota = 0;
-    if (get_meminfo(MEM_FREE, &quota) < 0)
-        return 0;
-    return quota * 1024;
-}

+ 0 - 189
Pal/src/host/FreeBSD/db_misc.c

@@ -1,189 +0,0 @@
-/* Copyright (C) 2014 Stony Brook University
-   This file is part of Graphene Library OS.
-
-   Graphene Library OS is free software: you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public License
-   as published by the Free Software Foundation, either version 3 of the
-   License, or (at your option) any later version.
-
-   Graphene Library OS is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
-
-/*
- * db_misc.c
- *
- * This file contains APIs for miscellaneous use.
- */
-
-#include "pal_defs.h"
-#include "pal_freebsd_defs.h"
-#include "pal.h"
-#include "pal_internal.h"
-#include "pal_freebsd.h"
-#include "pal_error.h"
-#include "pal_security.h"
-#include "api.h"
-
-#include <sys/time.h>
-#include <fcntl.h>
-
-unsigned long _DkSystemTimeQuery (void)
-{
-#if USE_CLOCK_GETTIME == 1
-    struct timespec time;
-    int ret;
-
-        ret = INLINE_SYSCALL(clock_gettime, 2, CLOCK_MONOTONIC, &time);
-
-    /* Come on, gettimeofday mostly never fails */
-    if (IS_ERR(ret))
-        return 0;
-
-    /* in microseconds */
-    return 1000000ULL * time.tv_sec + time.tv_nsec / 1000;
-#else
-    struct timeval time;
-    int ret;
-
-        ret = INLINE_SYSCALL(gettimeofday, 2, &time, NULL);
-
-    /* Come on, gettimeofday mostly never fails */
-    if (IS_ERR(ret))
-        return 0;
-
-    /* in microseconds */
-    return 1000000ULL * time.tv_sec + time.tv_usec;
-#endif
-}
-
-#if USE_ARCH_RDRAND == 1
-int _DkRandomBitsRead (void * buffer, int size)
-{
-    int total_bytes = 0;
-    do {
-        unsigned long rand;
-        asm volatile (".Lretry: rdrand %%rax\r\n jnc .Lretry\r\n"
-                      : "=a"(rand) :: "memory");
-
-        if (total_bytes + sizeof(rand) <= size) {
-            *(unsigned long *) (buffer + total_bytes) = rand;
-            total_bytes += sizeof(rand);
-        } else {
-            for (int i = 0 ; i < size - total_bytes ; i++)
-                *(unsigned char *) (buffer + total_bytes + i) = ((unsigned char *) &rand)[i];
-            total_bytes = size;
-        }
-    } while (total_bytes < size);
-    return 0;
-}
-#else
-int _DkRandomBitsRead (void * buffer, int size)
-{
-    if (!pal_sec.rand_gen) {
-        int rand = INLINE_SYSCALL(open, 3, "/dev/urandom", O_RDONLY, 0);
-        if (IS_ERR(rand))
-            return -PAL_ERROR_DENIED;
-
-        pal_sec.rand_gen = rand;
-    }
-
-    int total_bytes = 0;
-    do {
-        int bytes = INLINE_SYSCALL(read, 3, pal_sec.rand_gen,
-                                   buffer + total_bytes, size - total_bytes);
-        if (IS_ERR(bytes))
-            return -PAL_ERROR_DENIED;
-
-        total_bytes += bytes;
-    } while (total_bytes < size);
-
-    return 0;
-}
-#endif
-
-#if defined(__i386__)
-#include <ldt.h>
-#else
-#include <x86/sysarch.h>
-#endif
-
-int _DkSegmentRegisterSet (int reg, const void * addr)
-{
-    int ret = 0;
-
-#if defined(__i386__)
-    struct user_desc u_info;
-
-    ret = INLINE_SYSCALL(sysarch, 2, I386_GET_FSBASE, &u_info);
-
-    if (IS_ERR(ret))
-        return NULL;
-
-    u_info->entry_number = -1;
-    u_info->base_addr = (unsigned int) addr;
-
-    ret = INLINE_SYSCALL(sysarch, 2, I386_SET_FSBASE, &u_info);
-#else
-    if (reg == PAL_SEGMENT_FS) {
-        ret = INLINE_SYSCALL(sysarch, 2, AMD64_SET_FSBASE, &addr);
-    } else if (reg == PAL_SEGMENT_GS) {
-        ret = INLINE_SYSCALL(sysarch, 2, AMD64_SET_GSBASE, &addr);
-    } else {
-        return -PAL_ERROR_INVAL;
-    }
-#endif
-    if (IS_ERR(ret))
-        return -PAL_ERROR_DENIED;
-
-    return 0;
-}
-
-int _DkSegmentRegisterGet (int reg, void ** addr)
-{
-    int ret;
-
-#if defined(__i386__)
-    struct user_desc u_info;
-
-    ret = INLINE_SYSCALL(sysarch, 2, I386_GET_FSBASE, &u_info);
-
-    if (IS_ERR(ret))
-        return -PAL_ERROR_DENIED;
-
-    *addr = (void *) u_info->base_addr;
-#else
-    unsigned long ret_addr;
-
-    if (reg == PAL_SEGMENT_FS) {
-        ret = INLINE_SYSCALL(sysarch, 2, AMD64_GET_FSBASE, &ret_addr);
-    } else if (reg == PAL_SEGMENT_GS) {
-        ret = INLINE_SYSCALL(sysarch, 2, AMD64_GET_GSBASE, &ret_addr);
-    } else {
-        return -PAL_ERROR_INVAL;
-
-    }
-
-    if (IS_ERR(ret))
-        return -PAL_ERROR_DENIED;
-
-    *addr = (void *) ret_addr;
-#endif
-
-    return 0;
-}
-
-int _DkInstructionCacheFlush (const void * addr, int size)
-{
-    return -PAL_ERROR_NOTIMPLEMENTED;
-}
-
-int _DkCpuIdRetrieve (unsigned int leaf, unsigned int subleaf,
-                      unsigned int values[4])
-{
-    return -PAL_ERROR_NOTIMPLEMENTED;
-}

+ 0 - 230
Pal/src/host/FreeBSD/db_mutex.c

@@ -1,230 +0,0 @@
-/* Copyright (C) 2014 Stony Brook University
-   This file is part of Graphene Library OS.
-
-   Graphene Library OS is free software: you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public License
-   as published by the Free Software Foundation, either version 3 of the
-   License, or (at your option) any later version.
-
-   Graphene Library OS is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
-
-/*
- * db_mutex.c
- *
- * This file contains APIs that provide operations of (futex based) mutexes.
- * Based on "Mutexes and Condition Variables using Futexes"
- * (http://locklessinc.com/articles/mutex_cv_futex)
- */
-
-#include "pal_defs.h"
-#include "pal_freebsd_defs.h"
-#include "pal.h"
-#include "pal_internal.h"
-#include "pal_freebsd.h"
-#include "pal_error.h"
-#include "api.h"
-
-#include <limits.h>
-#include <atomic.h>
-#include <errno.h>
-#include <sys/time.h>
-#include <unistd.h>
-
-#if defined(__i386__)
-#define RMB()           asm volatile("lock; addl $0,0(%%esp)" ::: "memory")
-#define CPU_RELAX()     asm volatile("rep; nop" ::: "memory");
-#endif
-
-#if defined(__x86_64__)
-#include <unistd.h>
-#define RMB()           asm volatile("lfence" ::: "memory")
-#define CPU_RELAX()     asm volatile("rep; nop" ::: "memory");
-#endif
-
-#define MUTEX_SPINLOCK_TIMES    20
-
-
-int _DkMutexLockTimeout (struct mutex_handle * mut, int timeout)
-{
-    int i, c = 0;
-
-    if (timeout == -1)
-        return -_DkMutexLock(mut);
-
-    struct atomic_int * m = &mut->value;
-
-    /* Spin and try to take lock */
-    for (i = 0 ; i < MUTEX_SPINLOCK_TIMES ; i++)
-    {
-        c = atomic_dec_and_test(m);
-        if (c)
-            goto success;
-        CPU_RELAX();
-    }
-
-    /* The lock is now contended */
-
-    int ret;
-
-    if (timeout == 0) {
-        ret = c ? 0 : -PAL_ERROR_TRYAGAIN;
-        goto out;
-    }
-
-    while (!c) {
-        int val = atomic_read(m);
-        if (val == 1)
-            goto again;
-
-        struct timespec waittime;
-        long sec = timeout / 1000000;
-        long microsec = timeout - (sec * 1000000);
-        waittime.tv_sec = sec;
-        waittime.tv_nsec = microsec * 1000;
-
-        ret = INLINE_SYSCALL(_umtx_op, 5, m, UMTX_OP_WAIT_UINT, val,
-                             NULL, &waittime);
-
-        if (IS_ERR(ret) && ERRNO(ret) != EWOULDBLOCK) {
-            ret = unix_to_pal_error(ERRNO(ret));
-            goto out;
-        }
-
-#ifdef DEBUG_MUTEX
-        if (IS_ERR(ret))
-            printf("mutex held by thread %d\n", mut->owner);
-#endif
-
-again:
-        /* Upon wakeup, we still need to check whether mutex is unlocked or
-         * someone else took it.
-         * If c==0 upon return from xchg (i.e., the older value of m==0), we
-         * will exit the loop. Else, we sleep again (through a futex call).
-         */
-        c = atomic_dec_and_test(m);
-    }
-
-success:
-#ifdef DEBUG_MUTEX
-    mut->owner = INLINE_SYSCALL(gettid, 0);
-#endif
-    ret = 0;
-out:
-    return ret;
-}
-int _DkMutexLock (struct mutex_handle * mut)
-{
-    int i, c = 0;
-    int ret;
-    struct atomic_int * m = &mut->value;
-
-    /* Spin and try to take lock */
-    for (i = 0; i < MUTEX_SPINLOCK_TIMES; i++) {
-        c = atomic_dec_and_test(m);
-        if (c)
-            goto success;
-        CPU_RELAX();
-    }
-
-    /* The lock is now contended */
-
-    while (!c) {
-        int val = atomic_read(m);
-        if (val == 1)
-            goto again;
-
-        ret = INLINE_SYSCALL(_umtx_op, 5, m, UMTX_OP_WAIT, val, NULL, NULL);
-
-        if (IS_ERR(ret) && ERRNO(ret) != EWOULDBLOCK) {
-            ret = unix_to_pal_error(ERRNO(ret));
-            goto out;
-        }
-
-#ifdef DEBUG_MUTEX
-        if (IS_ERR(ret))
-            printf("mutex held by thread %d\n", mut->owner);
-#endif
-
-again:
-        /* Upon wakeup, we still need to check whether mutex is unlocked or
-         * someone else took it.
-         * If c==0 upon return from xchg (i.e., the older value of m==0), we
-         * will exit the loop. Else, we sleep again (through a futex call).
-         */
-        c = atomic_dec_and_test(m);
-    }
-
-success:
-#ifdef DEBUG_MUTEX
-    mut->owner = INLINE_SYSCALL(gettid, 0);
-#endif
-    ret = 0;
-out:
-    return ret;
-}
-
-int _DkMutexUnlock (struct mutex_handle * mut)
-{
-    struct atomic_int * m = &mut->value;
-    int ret = 0;
-    int must_wake = 0;
-
-#ifdef DEBUG_MUTEX
-    mut->owner = 0;
-#endif
-
-    /* Unlock, and if not contended then exit. */
-    if (atomic_read(m) < 0)
-        must_wake = 1;
-
-    atomic_set(m, 1);
-
-     if (must_wake) {
-         /* We need to wake someone up */
-         ret = INLINE_SYSCALL(_umtx_op, 5, m, UMTX_OP_WAKE, 1,
-                              NULL, NULL);
-    }
-    if (IS_ERR(ret)) {
-        ret = -PAL_ERROR_TRYAGAIN;
-        goto out;
-    }
-
-    ret = 0;
-out:
-    return ret;
-}
-
-int _DkMutexAcquireTimeout (PAL_HANDLE handle, int timeout)
-{
-    return _DkMutexLockTimeout(&handle->mutex.mut, timeout);
-}
-
-
-void _DkMutexRelease (PAL_HANDLE handle)
-{
-    _DkMutexUnlock(&handle->mutex.mut);
-    return;
-}
-
-static int mutex_wait (PAL_HANDLE handle, uint64_t timeout)
-{
-    return _DkMutexAcquireTimeout(handle, timeout);
-}
-
-struct handle_ops mutex_ops = {
-        .wait               = &mutex_wait,
-};
-
-int _DkMutexCreate (PAL_HANDLE *handle, int count) {
-     PAL_HANDLE mut = malloc(HANDLE_SIZE(mutex));
-     SET_HANDLE_TYPE(mut, mutex);
-     atomic_set(&mut->mutex.mut.value, 0);
-     *handle = mut;
-     return 0;
-}

+ 0 - 233
Pal/src/host/FreeBSD/db_object.c

@@ -1,233 +0,0 @@
-/* Copyright (C) 2014 Stony Brook University
-   This file is part of Graphene Library OS.
-
-   Graphene Library OS is free software: you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public License
-   as published by the Free Software Foundation, either version 3 of the
-   License, or (at your option) any later version.
-
-   Graphene Library OS is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
-
-/*
- * db_object.c
- *
- * This file contains APIs for closing or polling PAL handles.
- */
-
-#include "pal_defs.h"
-#include "pal_freebsd_defs.h"
-#include "pal.h"
-#include "pal_internal.h"
-#include "pal_freebsd.h"
-#include "pal_error.h"
-#include "pal_debug.h"
-#include "api.h"
-
-#include <time.h>
-#include <sys/poll.h>
-#include <sys/wait.h>
-#include <atomic.h>
-#include <errno.h>
-
-#define DEFAULT_QUANTUM 500
-
-/* internally to wait for one object. Also used as a shortcut to wait
-   on events and semaphores */
-static int _DkObjectWaitOne (PAL_HANDLE handle, int timeout)
-{
-    /* only for all these handle which has a file descriptor, or
-       a eventfd. events and semaphores will skip this part */
-    if (handle->hdr.flags & HAS_FDS) {
-        struct pollfd fds[MAX_FDS];
-        int off[MAX_FDS];
-        int nfds = 0;
-        for (int i = 0 ; i < MAX_FDS ; i++) {
-            int events = 0;
-
-            if ((handle->hdr.flags & RFD(i)) &&
-                !(handle->hdr.flags & ERROR(i)))
-                events |= POLLIN;
-
-            if ((handle->hdr.flags & WFD(i)) &&
-                !(handle->hdr.flags & WRITABLE(i)) &&
-                !(handle->hdr.flags & ERROR(i)))
-                events |= POLLOUT;
-
-            if (events) {
-                fds[nfds].fd = handle->hdr.fds[i];
-                fds[nfds].events = events|POLLHUP|POLLERR;
-                fds[nfds].revents = 0;
-                off[nfds] = i;
-                nfds++;
-            }
-        }
-
-        if (!nfds)
-            return -PAL_ERROR_TRYAGAIN;
-
-        int ret = INLINE_SYSCALL(poll, 3, &fds, nfds,
-                                 timeout ? timeout : -1);
-
-        if (IS_ERR(ret))
-            switch (ERRNO(ret)) {
-                case EINTR:
-                    return -PAL_ERROR_INTERRUPTED;
-                default:
-                    return unix_to_pal_error(ERRNO(ret));
-            }
-
-        if (!ret)
-            return -PAL_ERROR_TRYAGAIN;
-
-        for (int i = 0 ; i < nfds ; i++) {
-            if (!fds[i].revents)
-                continue;
-            if (fds[i].revents & POLLOUT)
-                handle->hdr.flags |= WRITABLE(off[i]);
-            if (fds[i].revents & (POLLHUP|POLLERR))
-                handle->hdr.flags |= ERROR(off[i]);
-        }
-
-        return 0;
-    }
-
-    const struct handle_ops * ops = HANDLE_OPS(handle);
-
-    if (!ops)
-        return -PAL_ERROR_BADHANDLE;
-
-    if (!ops->wait)
-        return -PAL_ERROR_NOTSUPPORT;
-
-    return ops->wait(handle, timeout);
-}
-
-/* _DkObjectsWaitAny for internal use. The function wait for any of the handle
-   in the handle array. timeout can be set for the wait. */
-int _DkObjectsWaitAny (int count, PAL_HANDLE * handleArray, uint64_t timeout,
-                       PAL_HANDLE * polled)
-{
-    if (count <= 0)
-        return 0;
-
-    if (count == 1) {
-        *polled = handleArray[0];
-        return _DkObjectWaitOne(handleArray[0], timeout);
-    }
-
-    int i, j, ret, maxfds = 0, nfds = 0;
-
-    /* we are not gonna to allow any polling on muliple synchronous
-       objects, doing this is simply violating the division of
-       labor between PAL and library OS */
-    for (i = 0 ; i < count ; i++) {
-        PAL_HANDLE hdl = handleArray[i];
-
-        if (!hdl)
-            continue;
-
-        if (!(hdl->hdr.flags & HAS_FDS))
-            return -PAL_ERROR_NOTSUPPORT;
-
-        /* eliminate repeated entries */
-        for (j = 0 ; j < i ; j++)
-            if (hdl == handleArray[j])
-                break;
-        if (j == i) {
-            for (j = 0 ; j < MAX_FDS ; j++)
-                if (hdl->hdr.flags & (RFD(j)|WFD(j)))
-                    maxfds++;
-        }
-    }
-
-    struct pollfd * fds = __alloca(sizeof(struct pollfd) * maxfds);
-    PAL_HANDLE * hdls = __alloca(sizeof(PAL_HANDLE) * maxfds);
-
-    for (i = 0 ; i < count ; i++) {
-        PAL_HANDLE hdl = handleArray[i];
-
-        if (!hdl)
-            continue;
-
-        for (j = 0 ; j < i ; j++)
-            if (hdl == handleArray[j])
-                break;
-        if (j < i)
-            continue;
-
-        for (j = 0 ; j < MAX_FDS ; j++) {
-            int events = 0;
-
-            if ((hdl->hdr.flags & RFD(j)) &&
-                !(hdl->hdr.flags & ERROR(j)))
-                events |= POLLIN;
-
-            if ((hdl->hdr.flags & WFD(j)) &&
-                !(hdl->hdr.flags & WRITABLE(j)) &&
-                !(hdl->hdr.flags & ERROR(j)))
-                events |= POLLOUT;
-
-            if (events && hdl->hdr.fds[j] != PAL_IDX_POISON) {
-                fds[nfds].fd = hdl->hdr.fds[j];
-                fds[nfds].events = events|POLLHUP|POLLERR;
-                fds[nfds].revents = 0;
-                hdls[nfds] = hdl;
-                nfds++;
-            }
-        }
-    }
-
-    if (!nfds)
-        return -PAL_ERROR_TRYAGAIN;
-
-    ret = INLINE_SYSCALL(poll, 3, fds, nfds, timeout ? timeout : -1);
-
-    if (IS_ERR(ret))
-        switch (ERRNO(ret)) {
-            case EINTR:
-                return -PAL_ERROR_INTERRUPTED;
-            default:
-                return unix_to_pal_error(ERRNO(ret));
-        }
-
-    if (!ret)
-        return -PAL_ERROR_TRYAGAIN;
-
-    PAL_HANDLE polled_hdl = NULL;
-
-    for (i = 0 ; i < nfds ; i++) {
-        if (!fds[i].revents)
-            continue;
-
-        PAL_HANDLE hdl = hdls[i];
-
-        if (polled_hdl) {
-            if (hdl != polled_hdl)
-                continue;
-        } else {
-            polled_hdl = hdl;
-        }
-
-        for (j = 0 ; j < MAX_FDS ; j++)
-            if ((hdl->hdr.flags & (RFD(j)|WFD(j))) &&
-                hdl->hdr.fds[j] == fds[i].fd)
-                break;
-
-        if (j == MAX_FDS)
-            continue;
-
-        if (fds[i].revents & POLLOUT)
-            hdl->hdr.flags |= WRITABLE(j);
-        if (fds[i].revents & (POLLHUP|POLLERR))
-            hdl->hdr.flags |= ERROR(j);
-    }
-
-    *polled = polled_hdl;
-    return polled_hdl ? 0 : -PAL_ERROR_TRYAGAIN;
-}

+ 0 - 510
Pal/src/host/FreeBSD/db_pipes.c

@@ -1,510 +0,0 @@
-/* Copyright (C) 2014 Stony Brook University
-   This file is part of Graphene Library OS.
-
-   Graphene Library OS is free software: you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public License
-   as published by the Free Software Foundation, either version 3 of the
-   License, or (at your option) any later version.
-
-   Graphene Library OS is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
-
-/*
- * db_pipes.c
- *
- * This file contains oeprands to handle streams with URIs that start with
- * "pipe:" or "pipe.srv:".
- */
-
-#include "pal_defs.h"
-#include "pal_freebsd_defs.h"
-#include "pal.h"
-#include "pal_internal.h"
-#include "pal_freebsd.h"
-#include "pal_error.h"
-#include "pal_security.h"
-#include "pal_debug.h"
-#include "api.h"
-
-#include <sys/types.h>
-typedef __kernel_pid_t pid_t;
-#include <fcntl.h>
-#include <poll.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <errno.h>
-#include <sys/filio.h>
-
-static int pipe_path (int pipeid, char * path, int len)
-{
-    /* use abstract UNIX sockets for pipes */
-    memset(path, 0, len);
-    if (pal_sec.pipe_prefix)
-        return snprintf(path, len, GRAPHENE_PIPEDIR "/%08x/%s%08x",
-                        pal_sec.domain_id,
-                        pal_sec.pipe_prefix, pipeid);
-    else
-        return snprintf(path, len, GRAPHENE_PIPEDIR "/%08x/%08x",
-                        pal_sec.domain_id, pipeid);
-}
-
-static int pipe_addr (int pipeid, struct sockaddr_un * addr)
-{
-    addr->sun_family = AF_UNIX;
-    return pipe_path(pipeid, (char *) addr->sun_path, sizeof(addr->sun_path));
-}
-
-static int pipe_listen (PAL_HANDLE * handle, PAL_NUM pipeid, int options)
-{
-    int ret, fd;
-    options = HOST_SOCKET_OPTIONS(options);
-
-    fd = INLINE_SYSCALL(socket, 3, AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|options,
-                        0);
-    if (IS_ERR(fd))
-        return -PAL_ERROR_DENIED;
-
-    struct sockaddr_un addr;
-
-    if ((ret = pipe_addr(pipeid, &addr)) < 0)
-        return ret;
-
-    ret = INLINE_SYSCALL(bind, 3, fd, &addr, sizeof(addr.sun_path) - 1);
-
-    if (IS_ERR(ret)) {
-        INLINE_SYSCALL(close, 1, fd);
-
-        switch(ERRNO(ret)) {
-            case EINVAL:
-                return -PAL_ERROR_INVAL;
-            case EADDRINUSE:
-                return -PAL_ERROR_STREAMEXIST;
-            default:
-                return -PAL_ERROR_DENIED;
-        }
-    }
-
-    ret = INLINE_SYSCALL(listen, 2, fd, 1);
-    if (IS_ERR(ret))
-        return -PAL_ERROR_DENIED;
-
-    PAL_HANDLE hdl = malloc(HANDLE_SIZE(pipe));
-    SET_HANDLE_TYPE(hdl, pipesrv);
-    hdl->hdr.flags |= RFD(0);
-    hdl->pipe.fd = fd;
-    hdl->pipe.pipeid = pipeid;
-    hdl->pipe.nonblocking = options & O_NONBLOCK ?
-                            PAL_TRUE : PAL_FALSE;
-    *handle = hdl;
-    return 0;
-}
-
-static int pipe_waitforclient (PAL_HANDLE handle, PAL_HANDLE * client)
-{
-    if (!IS_HANDLE_TYPE(handle, pipesrv))
-        return -PAL_ERROR_NOTSERVER;
-
-    if (handle->pipe.fd == PAL_IDX_POISON)
-        return -PAL_ERROR_DENIED;
-
-    int newfd = INLINE_SYSCALL(accept4, 4, handle->pipe.fd, NULL, NULL,
-                               SOCK_CLOEXEC);
-    if (IS_ERR(newfd))
-        switch (ERRNO(newfd)) {
-            case EWOULDBLOCK:
-                return -PAL_ERROR_TRYAGAIN;
-            case ECONNABORTED:
-                return -PAL_ERROR_CONNFAILED;
-            default:
-                return -PAL_ERROR_DENIED;
-        }
-
-    PAL_HANDLE clnt = malloc(HANDLE_SIZE(pipe));
-    SET_HANDLE_TYPE(clnt, pipecli);
-    clnt->hdr.flags |= RFD(0)|WFD(0)|WRITABLE(0);
-    clnt->pipe.fd = newfd;
-    clnt->pipe.nonblocking = PAL_FALSE;
-    clnt->pipe.pipeid = handle->pipe.pipeid;
-    *client = clnt;
-
-    return 0;
-}
-
-static int pipe_connect (PAL_HANDLE * handle, PAL_NUM pipeid, int options)
-{
-    int ret, fd;
-
-    options = HOST_SOCKET_OPTIONS(options);
-
-    fd = INLINE_SYSCALL(socket, 3, AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|options,
-                        0);
-
-    if (IS_ERR(fd))
-        return -PAL_ERROR_DENIED;
-
-    struct sockaddr_un addr;
-
-    if ((ret = pipe_addr(pipeid, &addr)) < 0)
-        return ret;
-
-    ret = INLINE_SYSCALL(connect, 3, fd, &addr, sizeof(addr.sun_path) - 1);
-    if (IS_ERR(ret)) {
-        INLINE_SYSCALL(close, 1, fd);
-        switch (ERRNO(ret)) {
-            case ECONNREFUSED:
-                return -PAL_ERROR_STREAMNOTEXIST;
-            case EINTR:
-                return -PAL_ERROR_TRYAGAIN;
-            default:
-                return -PAL_ERROR_DENIED;
-        }
-    }
-
-    PAL_HANDLE hdl = malloc(HANDLE_SIZE(pipe));
-    SET_HANDLE_TYPE(hdl, pipe);
-    hdl->hdr.flags |= RFD(0)|WFD(0)|WRITABLE(0);
-    hdl->pipe.fd = fd;
-    hdl->pipe.pipeid = pipeid;
-    hdl->pipe.nonblocking = (options & O_NONBLOCK) ?
-                            PAL_TRUE : PAL_FALSE;
-    *handle = hdl;
-
-    return 0;
-}
-
-static int pipe_private (PAL_HANDLE * handle, int options)
-{
-    int ret, fds[2];
-
-    options = HOST_SOCKET_OPTIONS(options);
-    ret = INLINE_SYSCALL(socketpair, 4, AF_UNIX,
-                         SOCK_STREAM|SOCK_CLOEXEC|options, 0, fds);
-    if (IS_ERR(ret))
-        return -PAL_ERROR_DENIED;
-
-    PAL_HANDLE hdl = malloc(HANDLE_SIZE(pipeprv));
-    SET_HANDLE_TYPE(hdl, pipeprv);
-    hdl->hdr.flags |= RFD(0)|WFD(1)|WRITABLE(1);
-    hdl->pipeprv.fds[0] = fds[0];
-    hdl->pipeprv.fds[1] = fds[1];
-    hdl->pipeprv.nonblocking = (options & O_NONBLOCK) ?
-                                PAL_TRUE : PAL_FALSE;
-    *handle = hdl;
-    return 0;
-}
-
-/* 'open' operation of pipe stream. For each pipe stream, it is
-   identified by a decimal number in URI. There could be two
-   types: pipe and pipe.srv. They behave pretty much the same,
-   except they are two ends of the pipe. */
-static int pipe_open (PAL_HANDLE *handle, const char * type, const char * uri,
-                      int access, int share, int create, int options)
-{
-    if (!strcmp_static(type, URI_TYPE_PIPE) && !*uri)
-        return pipe_private(handle, options);
-
-    char * endptr;
-    PAL_NUM pipeid = strtol(uri, &endptr, 10);
-
-    if (*endptr)
-        return -PAL_ERROR_INVAL;
-
-    options = HOST_OPTIONS(options & PAL_OPTION_MASK);
-
-    if (!strcmp_static(type, URI_TYPE_PIPE_SRV))
-        return pipe_listen(handle, pipeid, options);
-
-    if (!strcmp_static(type, URI_TYPE_PIPE))
-        return pipe_connect(handle, pipeid, options);
-
-    return -PAL_ERROR_INVAL;
-}
-
-/* 'read' operation of pipe stream. offset does not apply here. */
-static int pipe_read (PAL_HANDLE handle, int offset, int len,
-                      void * buffer)
-{
-    if (!IS_HANDLE_TYPE(handle, pipecli) &&
-        !IS_HANDLE_TYPE(handle, pipeprv) &&
-        !IS_HANDLE_TYPE(handle, pipe))
-        return -PAL_ERROR_NOTCONNECTION;
-
-    int fd = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.fds[0] :
-             handle->pipe.fd;
-    int bytes = 0;
-
-    struct msghdr hdr;
-    struct iovec iov;
-
-    iov.iov_base = buffer;
-    iov.iov_len = len;
-    hdr.msg_name = NULL;
-    hdr.msg_namelen = 0;
-    hdr.msg_iov = &iov;
-    hdr.msg_iovlen = 1;
-    hdr.msg_control = NULL;
-    hdr.msg_controllen = 0;
-    hdr.msg_flags = 0;
-
-    bytes = INLINE_SYSCALL(recvmsg, 3, fd, &hdr, 0);
-
-    if (IS_ERR(bytes))
-        switch(ERRNO(bytes)) {
-            case EWOULDBLOCK:
-                return-PAL_ERROR_TRYAGAIN;
-            case EINTR:
-                return -PAL_ERROR_INTERRUPTED;
-            default:
-                return -PAL_ERROR_DENIED;
-        }
-
-    if (!bytes)
-        return -PAL_ERROR_ENDOFSTREAM;
-
-    return bytes;
-}
-
-/* 'write' operation of pipe stream. offset does not apply here. */
-static int pipe_write (PAL_HANDLE handle, int offset, int len,
-                       const void * buffer)
-{
-    if (!IS_HANDLE_TYPE(handle, pipecli) &&
-        !IS_HANDLE_TYPE(handle, pipeprv) &&
-        !IS_HANDLE_TYPE(handle, pipe))
-        return -PAL_ERROR_NOTCONNECTION;
-
-    int fd = IS_HANDLE_TYPE(handle, pipeprv) ? handle->pipeprv.fds[1] :
-             handle->pipe.fd;
-    int bytes = 0;
-
-    struct msghdr hdr;
-    struct iovec iov;
-
-    iov.iov_base = (void *) buffer;
-    iov.iov_len = len;
-    hdr.msg_name = NULL;
-    hdr.msg_namelen = 0;
-    hdr.msg_iov = &iov;
-    hdr.msg_iovlen = 1;
-    hdr.msg_control = NULL;
-    hdr.msg_controllen = 0;
-    hdr.msg_flags = 0;
-
-    bytes = INLINE_SYSCALL(sendmsg, 3, fd, &hdr, MSG_NOSIGNAL);
-
-    PAL_FLG writable = IS_HANDLE_TYPE(handle, pipeprv) ? WRITABLE(1) :
-                       WRITABLE(0);
-
-    if (IS_ERR(bytes))
-        switch(ERRNO(bytes)) {
-            case EWOULDBLOCK:
-                handle->hdr.flags &= ~writable;
-                return-PAL_ERROR_TRYAGAIN;
-            case EINTR:
-                return -PAL_ERROR_INTERRUPTED;
-            default:
-                return -PAL_ERROR_DENIED;
-        }
-
-    if (bytes == len)
-        handle->hdr.flags |= writable;
-    else
-        handle->hdr.flags &= ~writable;
-
-    return bytes;
-}
-
-/* 'close' operation of pipe stream. */
-static int pipe_close (PAL_HANDLE handle)
-{
-    if (IS_HANDLE_TYPE(handle, pipeprv)) {
-        if (handle->pipeprv.fds[0] != PAL_IDX_POISON) {
-            INLINE_SYSCALL(close, 1, handle->pipeprv.fds[0]);
-            handle->pipeprv.fds[0] = PAL_IDX_POISON;
-        }
-        if (handle->pipeprv.fds[1] != PAL_IDX_POISON) {
-            INLINE_SYSCALL(close, 1, handle->pipeprv.fds[1]);
-            handle->pipeprv.fds[1] = PAL_IDX_POISON;
-        }
-        return 0;
-    }
-
-    if (handle->pipe.fd != PAL_IDX_POISON) {
-        INLINE_SYSCALL(close, 1, handle->pipe.fd);
-        handle->pipe.fd = PAL_IDX_POISON;
-    }
-
-    return 0;
-}
-
-/* 'delete' operation of pipe stream. */
-static int pipe_delete (PAL_HANDLE handle, int access)
-{
-    if (IS_HANDLE_TYPE(handle, pipeprv)) {
-        switch (access) {
-            case 0:
-                if (handle->pipeprv.fds[0] != PAL_IDX_POISON) {
-                    INLINE_SYSCALL(close, 1, handle->pipeprv.fds[0]);
-                    handle->pipeprv.fds[0] = PAL_IDX_POISON;
-                }
-                if (handle->pipeprv.fds[1] != PAL_IDX_POISON) {
-                    INLINE_SYSCALL(close, 1, handle->pipeprv.fds[1]);
-                    handle->pipeprv.fds[1] = PAL_IDX_POISON;
-                }
-                break;
-            case PAL_DELETE_RD:
-                if (handle->pipeprv.fds[0] != PAL_IDX_POISON) {
-                    INLINE_SYSCALL(close, 1, handle->pipeprv.fds[0]);
-                    handle->pipeprv.fds[0] = PAL_IDX_POISON;
-                }
-                break;
-            case PAL_DELETE_WR:
-                if (handle->pipeprv.fds[1] != PAL_IDX_POISON) {
-                    INLINE_SYSCALL(close, 1, handle->pipeprv.fds[1]);
-                    handle->pipeprv.fds[1] = PAL_IDX_POISON;
-                }
-                break;
-            default:
-                return -PAL_ERROR_INVAL;
-        }
-    }
-
-    if (handle->pipe.fd == PAL_IDX_POISON)
-        return 0;
-
-    int shutdown;
-    switch (access) {
-        case 0:
-            shutdown = SHUT_RDWR;
-            break;
-        case PAL_DELETE_RD:
-            shutdown = SHUT_RD;
-            break;
-        case PAL_DELETE_WR:
-            shutdown = SHUT_WR;
-            break;
-        default:
-            return -PAL_ERROR_INVAL;
-    }
-
-    INLINE_SYSCALL(shutdown, 2, handle->pipe.fd, shutdown);
-
-    return 0;
-}
-
-static int pipe_attrquerybyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
-{
-    int ret, val;
-
-    if (handle->hdr.fds[0] == PAL_IDX_POISON)
-        return -PAL_ERROR_BADHANDLE;
-
-    ret = INLINE_SYSCALL(ioctl, 3, handle->hdr.fds[0], FIONREAD, &val);
-    if (IS_ERR(ret))
-        return unix_to_pal_error(ERRNO(ret));
-
-    attr->handle_type  = pal_type_pipe;
-    attr->disconnected = handle->hdr.flags & ERROR(0);
-    attr->nonblocking  = (handle->hdr.type == pal_type_pipeprv) ?
-                         handle->pipeprv.nonblocking : handle->pipe.nonblocking;
-    attr->readable     = val > 0;
-    if (PAL_GET_TYPE(handle) == pal_type_pipeprv)
-        attr->writable = handle->hdr.flags & WRITABLE(1);
-    else
-        attr->writable = handle->hdr.flags & WRITABLE(0);
-    attr->pending_size = val;
-    return 0;
-}
-
-static int pipe_attrsetbyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
-{
-    if (handle->hdr.fds[0] == PAL_IDX_POISON)
-        return -PAL_ERROR_BADHANDLE;
-
-    int ret;
-    PAL_BOL * nonblocking = (handle->hdr.type == pal_type_pipeprv) ?
-                            &handle->pipeprv.nonblocking :
-                            &handle->pipe.nonblocking;
-
-    if (attr->nonblocking != *nonblocking) {
-        ret = INLINE_SYSCALL(fcntl, 3, handle->hdr.fds[0], F_SETFL,
-                             *nonblocking ? O_NONBLOCK : 0);
-
-        if (IS_ERR(ret))
-            return unix_to_pal_error(ERRNO(ret));
-
-        *nonblocking = attr->nonblocking;
-    }
-
-    return 0;
-}
-
-static int pipe_getname (PAL_HANDLE handle, char * buffer, int count)
-{
-    int old_count = count;
-    int ret;
-
-    const char * prefix = NULL;
-    int prefix_len = 0;
-
-    switch (PAL_GET_TYPE(handle)) {
-        case pal_type_pipesrv:
-        case pal_type_pipecli:
-            prefix_len = static_strlen(URI_TYPE_PIPE_SRV);
-            prefix = URI_TYPE_PIPE_SRV;
-            break;
-        case pal_type_pipe:
-            prefix_len = static_strlen(URI_TYPE_PIPE);
-            prefix = URI_TYPE_PIPE;
-            break;
-        case pal_type_pipeprv:
-        default:
-            return -PAL_ERROR_INVAL;
-    }
-
-    if (prefix_len >= count)
-        return -PAL_ERROR_OVERFLOW;
-
-    memcpy(buffer, prefix, prefix_len);
-    buffer[prefix_len] = ':';
-    buffer += prefix_len + 1;
-    count  -= prefix_len + 1;
-
-    ret = snprintf(buffer, count, "%lu\n", handle->pipe.pipeid);
-
-    if (buffer[ret - 1] != '\n') {
-        memset(buffer, 0, count);
-        return -PAL_ERROR_OVERFLOW;
-    }
-
-    buffer[ret - 1] = 0;
-    buffer += ret - 1;
-    count  -= ret - 1;
-    return old_count - count;
-}
-
-struct handle_ops pipe_ops = {
-        .getname            = &pipe_getname,
-        .open               = &pipe_open,
-        .waitforclient      = &pipe_waitforclient,
-        .read               = &pipe_read,
-        .write              = &pipe_write,
-        .close              = &pipe_close,
-        .delete             = &pipe_delete,
-        .attrquerybyhdl     = &pipe_attrquerybyhdl,
-        .attrsetbyhdl       = &pipe_attrsetbyhdl,
-    };
-
-struct handle_ops pipeprv_ops = {
-        .open               = &pipe_open,
-        .read               = &pipe_read,
-        .write              = &pipe_write,
-        .close              = &pipe_close,
-        .attrquerybyhdl     = &pipe_attrquerybyhdl,
-        .attrsetbyhdl       = &pipe_attrsetbyhdl,
-    };

+ 0 - 510
Pal/src/host/FreeBSD/db_process.c

@@ -1,510 +0,0 @@
-/* Copyright (C) 2014 Stony Brook University
-   This file is part of Graphene Library OS.
-
-   Graphene Library OS is free software: you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public License
-   as published by the Free Software Foundation, either version 3 of the
-   License, or (at your option) any later version.
-
-   Graphene Library OS is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
-
-/*
- * db_process.c
- *
- * This source file contains functions to create a child process and terminate
- * the running process. Child does not inherit any objects or memory from its
- * parent pricess. A Parent process may not modify the execution of its
- * children. It can wait for a child to exit using its handle. Also, parent and
- * child may communicate through I/O streams provided by the parent to the child
- * at creation.
- */
-
-#include "pal_defs.h"
-#include "pal_freebsd_defs.h"
-#include "pal.h"
-#include "pal_internal.h"
-#include "pal_freebsd.h"
-#include "pal_debug.h"
-#include "pal_error.h"
-#include "pal_security.h"
-#include "api.h"
-
-#include <sched.h>
-#include <sys/types.h>
-typedef __kernel_pid_t pid_t;
-#include <fcntl.h>
-#include <sys/socket.h>
-#include <errno.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <sys/filio.h>
-#ifndef SEEK_SET
-# define SEEK_SET 0
-#endif
-
-static inline int create_process_handle (PAL_HANDLE * parent,
-                                         PAL_HANDLE * child)
-{
-    PAL_HANDLE phdl = NULL, chdl = NULL;
-    int fds[4] = { -1, -1, -1, -1 };
-    int socktype = SOCK_STREAM | SOCK_CLOEXEC;
-    int ret;
-
-    if (IS_ERR((ret = INLINE_SYSCALL(socketpair, 4, AF_UNIX, socktype, 0, &fds[0]))) ||
-        IS_ERR((ret = INLINE_SYSCALL(socketpair, 4, AF_UNIX, socktype, 0, &fds[2])))) {
-        ret = -PAL_ERROR_DENIED;
-        goto out;
-    }
-
-    phdl = malloc(HANDLE_SIZE(process));
-    if (!phdl) {
-        ret = -PAL_ERROR_NOMEM;
-        goto out;
-    }
-
-    SET_HANDLE_TYPE(phdl, process);
-    phdl->hdr.flags |= RFD(0)|WFD(0)|RFD(1)|WFD(1)|WRITABLE(0)|WRITABLE(1);
-    phdl->process.stream      = fds[0];
-    phdl->process.cargo       = fds[2];
-    phdl->process.pid         = bsd_state.pid;
-    phdl->process.nonblocking = PAL_FALSE;
-
-    chdl = malloc(HANDLE_SIZE(process));
-    if (!chdl) {
-        ret = -PAL_ERROR_NOMEM;
-        goto out;
-    }
-
-    SET_HANDLE_TYPE(chdl, process);
-    chdl->hdr.flags |= RFD(0)|WFD(0)|RFD(1)|WFD(1)|WRITABLE(0)|WRITABLE(1);
-    chdl->process.stream      = fds[1];
-    chdl->process.cargo       = fds[3];
-    chdl->process.pid         = 0; /* unknown yet */
-    chdl->process.nonblocking = PAL_FALSE;
-
-    *parent = phdl;
-    *child  = chdl;
-    ret = 0;
-out:
-    if (ret < 0) {
-        if (phdl)
-            _DkObjectClose(phdl);
-        if (chdl)
-            _DkObjectClose(chdl);
-        for (int i = 0; i < 4; i++)
-            if (fds[i] != -1)
-                INLINE_SYSCALL(close, 1, fds[i]);
-    }
-    return ret;
-}
-
-struct proc_param {
-    PAL_HANDLE parent;
-    PAL_HANDLE exec;
-    PAL_HANDLE manifest;
-    const char ** argv;
-};
-
-struct proc_args {
-    PAL_NUM parent_process_id;
-    struct pal_sec  pal_sec;
-    unsigned long   memory_quota;
-    unsigned int    parent_data_size;
-    unsigned int    exec_data_size;
-    unsigned int    manifest_data_size;
-};
-
-static int child_process (void * param)
-{
-    struct proc_param * proc_param = param;
-    int ret;
-
-    ret = INLINE_SYSCALL(dup2, 2, proc_param->parent->process.stream, PROC_INIT_FD);
-    if (IS_ERR(ret))
-        goto failed;
-
-    if (proc_param->parent)
-        handle_set_cloexec(proc_param->parent,   false);
-    if (proc_param->exec)
-        handle_set_cloexec(proc_param->exec,     false);
-    if (proc_param->manifest)
-        handle_set_cloexec(proc_param->manifest, false);
-
-    INLINE_SYSCALL(execve, 3, PAL_LOADER, proc_param->argv, NULL);
-
-failed:
-    /* fail is it gets here */
-    return -PAL_ERROR_DENIED;
-}
-
-int _DkProcessCreate (PAL_HANDLE * handle, const char * uri, const char ** args)
-{
-    PAL_HANDLE exec = NULL;
-    PAL_HANDLE parent_handle = NULL, child_handle = NULL;
-    int ret;
-
-    /* step 1: open uri and check whether it is an executable */
-
-    if (uri) {
-        if ((ret = _DkStreamOpen(&exec, uri, PAL_ACCESS_RDONLY, 0, 0, 0)) < 0)
-            return ret;
-
-        handle_set_cloexec(exec, true);
-    }
-
-    /* step 2: create parant and child process handle */
-
-    struct proc_param param;
-
-    ret = create_process_handle(&parent_handle, &child_handle);
-    if (ret < 0)
-        goto out;
-
-
-    param.parent = parent_handle;
-    param.exec = exec;
-    param.manifest = pal_state.manifest_handle;
-
-    /* step 3: compose process parameter */
-
-    int parent_datasz = 0, exec_datasz = 0, manifest_datasz = 0;
-    void * parent_data = NULL;
-    void * exec_data = NULL;
-    void * manifest_data = NULL;
-
-    ret = handle_serialize(parent_handle, &parent_data);
-    if (ret < 0)
-        goto out;
-    parent_datasz = ret;
-
-    if (exec) {
-        ret = handle_serialize(exec, &exec_data);
-        if (ret < 0) {
-            free(parent_data);
-            goto out;
-        }
-        exec_datasz = ret;
-    }
-
-    if (pal_state.manifest_handle) {
-        ret = handle_serialize(pal_state.manifest_handle, &manifest_data);
-        if (ret < 0) {
-            free(parent_data);
-            free(exec_data);
-            goto out;
-        }
-        manifest_datasz = ret;
-    }
-
-    unsigned int datasz = parent_datasz + exec_datasz + manifest_datasz;
-    struct proc_args * proc_args =
-            __alloca(sizeof(struct proc_args) + datasz);
-
-    proc_args->parent_process_id = bsd_state.parent_pid;
-    memcpy(&proc_args->pal_sec, &pal_sec, sizeof(struct pal_sec));
-    proc_args->pal_sec.r_debug_state = NULL;
-    proc_args->pal_sec.r_debug = NULL;
-    proc_args->memory_quota = bsd_state.memory_quota;
-
-    void * data = (void *) (proc_args + 1);
-
-    memcpy(data, parent_data, parent_datasz);
-    data += (proc_args->parent_data_size = parent_datasz);
-    free(parent_data);
-
-    if (exec_data) {
-        memcpy(data, exec_data, exec_datasz);
-        data += (proc_args->exec_data_size = exec_datasz);
-        free(exec_data);
-    } else {
-        proc_args->exec_data_size = 0;
-    }
-
-    if (manifest_data) {
-        memcpy(data, manifest_data, manifest_datasz);
-        data += (proc_args->manifest_data_size = manifest_datasz);
-        free(manifest_data);
-    } else {
-        proc_args->manifest_data_size = 0;
-    }
-
-    /* step 4: create a child thread which will execve in the future */
-
-    /* the first arguement must be the PAL */
-    int argc = 0;
-    if (args)
-        for (; args[argc] ; argc++);
-    param.argv = __alloca(sizeof(const char *) * (argc + 2));
-    param.argv[0] = PAL_LOADER;
-    if (args)
-        memcpy(&param.argv[1], args, sizeof(const char *) * argc);
-    param.argv[argc + 1] = NULL;
-
-    ret = INLINE_SYSCALL(vfork, 0);
-
-    if (IS_ERR(ret)) {
-        ret = -PAL_ERROR_DENIED;
-        goto out;
-    }
-
-    if (!ret) {
-        ret = child_process(&param);
-        goto out; /* if child_process returned, there was a failure */
-    }
-
-    child_handle->process.pid = ret;
-
-    /* step 4: send parameters over the process handle */
-
-    ret = INLINE_SYSCALL(write, 3,
-                         child_handle->process.stream,
-                         proc_args,
-                         sizeof(struct proc_args) + datasz);
-
-    if (IS_ERR(ret) ||
-        ret < sizeof(struct proc_args) + datasz) {
-        ret = -PAL_ERROR_DENIED;
-        goto out;
-    }
-
-    *handle = child_handle;
-    ret = 0;
-out:
-    if (parent_handle)
-        _DkObjectClose(parent_handle);
-    if (ret < 0) {
-        if (child_handle)
-            _DkObjectClose(child_handle);
-        if (exec)
-            _DkObjectClose(exec);
-    }
-    return ret;
-}
-
-#define LARGE_PROC_ARGS 4096
-
-void init_child_process (PAL_HANDLE * parent_handle,
-                         PAL_HANDLE * exec_handle,
-                         PAL_HANDLE * manifest_handle)
-{
-    int ret = 0;
-
-    /* try to do a very large reading, so it doesn't have to be read for the
-       second time */
-    struct proc_args * proc_args = __alloca(sizeof(struct proc_args));
-    struct proc_args * new_proc_args;
-
-    int bytes = INLINE_SYSCALL(read, 3, PROC_INIT_FD, proc_args,
-                               sizeof(*proc_args));
-
-    if (IS_ERR(bytes)) {
-        if (ERRNO(bytes) != EBADF)
-            INIT_FAIL(PAL_ERROR_DENIED, "communication fail with parent");
-
-        /* in the first process */
-        /* occupy PROC_INIT_FD so no one will use it */
-        INLINE_SYSCALL(dup2, 2, 0, PROC_INIT_FD);
-        return;
-    }
-
-    /* a child must have parent handle and an executable */
-    if (!proc_args->parent_data_size)
-        INIT_FAIL(PAL_ERROR_INVAL, "invalid process created");
-
-    int datasz = proc_args->parent_data_size + proc_args->exec_data_size +
-                 proc_args->manifest_data_size;
-
-    if (!datasz)
-        goto no_data;
-
-    new_proc_args = __alloca(sizeof(*proc_args) + datasz);
-    memcpy(new_proc_args, proc_args, sizeof(*proc_args));
-    proc_args = new_proc_args;
-    void * data = (void *) (proc_args + 1);
-
-    bytes = INLINE_SYSCALL(read, 3, PROC_INIT_FD, data, datasz);
-    if (IS_ERR(bytes))
-        INIT_FAIL(PAL_ERROR_DENIED, "communication fail with parent");
-
-    /* now deserialize the parent_handle */
-    PAL_HANDLE parent = NULL;
-    ret = handle_deserialize(&parent, data, proc_args->parent_data_size);
-    if (ret < 0)
-        INIT_FAIL(-ret, "cannot deseilaize parent process handle");
-    data += proc_args->parent_data_size;
-    *parent_handle = parent;
-
-    /* deserialize the executable handle */
-    if (proc_args->exec_data_size) {
-        PAL_HANDLE exec = NULL;
-
-        ret = handle_deserialize(&exec, data,
-                                 proc_args->exec_data_size);
-        if (ret < 0)
-            INIT_FAIL(-ret, "cannot deserialize executable handle");
-
-        data += proc_args->exec_data_size;
-        *exec_handle = exec;
-    }
-
-    /* deserialize the manifest handle, if there is one */
-    if (proc_args->manifest_data_size) {
-        PAL_HANDLE manifest = NULL;
-
-        ret = handle_deserialize(&manifest, data,
-                                 proc_args->manifest_data_size);
-        if (ret < 0)
-            INIT_FAIL(-ret, "cannot deserialize manifest handle");
-
-        data += proc_args->manifest_data_size;
-        *manifest_handle = manifest;
-    }
-
-no_data:
-    bsd_state.memory_quota = proc_args->memory_quota;
-    memcpy(&pal_sec, &proc_args->pal_sec, sizeof(struct pal_sec));
-}
-
-noreturn void _DkProcessExit (int exitcode)
-{
-    INLINE_SYSCALL(exit, 1, exitcode);
-}
-
-static int proc_read (PAL_HANDLE handle, int offset, int count,
-                          void * buffer)
-{
-    int bytes = INLINE_SYSCALL(read, 3, handle->process.stream, buffer, count);
-
-    if (IS_ERR(bytes))
-        switch(ERRNO(bytes)) {
-            case EWOULDBLOCK:
-                return-PAL_ERROR_TRYAGAIN;
-            case EINTR:
-                return -PAL_ERROR_INTERRUPTED;
-            default:
-                return -PAL_ERROR_DENIED;
-        }
-
-    return bytes;
-}
-
-static int proc_write (PAL_HANDLE handle, int offset, int count,
-                       const void * buffer)
-{
-    int bytes = INLINE_SYSCALL(write, 3, handle->process.stream, buffer, count);
-
-    if (IS_ERR(bytes))
-        switch(ERRNO(bytes)) {
-            case EWOULDBLOCK:
-                handle->hdr.flags &= ~WRITABLE(0);
-                return-PAL_ERROR_TRYAGAIN;
-            case EINTR:
-                return -PAL_ERROR_INTERRUPTED;
-            default:
-                return -PAL_ERROR_DENIED;
-        }
-
-    if (bytes == count)
-        handle->hdr.flags |= WRITABLE(0);
-    else
-        handle->hdr.flags &= ~WRITABLE(0);
-
-    return bytes;
-}
-
-static int proc_close (PAL_HANDLE handle)
-{
-    if (handle->process.stream != PAL_IDX_POISON) {
-        INLINE_SYSCALL(close, 1, handle->process.stream);
-        handle->process.stream = PAL_IDX_POISON;
-    }
-
-    if (handle->process.cargo != PAL_IDX_POISON) {
-        INLINE_SYSCALL(close, 1, handle->process.cargo);
-        handle->process.cargo = PAL_IDX_POISON;
-    }
-
-    return 0;
-}
-
-static int proc_delete (PAL_HANDLE handle, int access)
-{
-    int shutdown;
-    switch (access) {
-        case 0:
-            shutdown = SHUT_RDWR;
-            break;
-        case PAL_DELETE_RD:
-            shutdown = SHUT_RD;
-            break;
-        case PAL_DELETE_WR:
-            shutdown = SHUT_WR;
-            break;
-        default:
-            return -PAL_ERROR_INVAL;
-    }
-
-    if (handle->process.stream != PAL_IDX_POISON)
-        INLINE_SYSCALL(shutdown, 2, handle->process.stream, shutdown);
-
-    if (handle->process.cargo != PAL_IDX_POISON)
-        INLINE_SYSCALL(shutdown, 2, handle->process.cargo, shutdown);
-
-    return 0;
-}
-
-static int proc_attrquerybyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
-{
-    int ret, val;
-
-    if (handle->process.stream == PAL_IDX_POISON)
-        return -PAL_ERROR_BADHANDLE;
-
-    ret = INLINE_SYSCALL(ioctl, 3, handle->process.stream, FIONREAD, &val);
-    if (IS_ERR(ret))
-        return unix_to_pal_error(ERRNO(ret));
-
-    attr->handle_type  = pal_type_process;
-    attr->nonblocking  = handle->process.nonblocking;
-    attr->disconnected = handle->hdr.flags & ERROR(0);
-    attr->readable     = !!val;
-    attr->writable     = handle->hdr.flags & WRITABLE(0);
-    attr->runnable     = PAL_FALSE;
-    attr->pending_size = val;
-
-    return 0;
-}
-
-static int proc_attrsetbyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
-{
-    if (handle->process.stream == PAL_IDX_POISON)
-        return -PAL_ERROR_BADHANDLE;
-
-    int ret;
-    if (attr->nonblocking != handle->process.nonblocking) {
-        ret = INLINE_SYSCALL(fcntl, 3, handle->process.stream, F_SETFL,
-                             handle->process.nonblocking ? O_NONBLOCK : 0);
-
-        if (IS_ERR(ret))
-            return unix_to_pal_error(ERRNO(ret));
-
-        handle->process.nonblocking = attr->nonblocking;
-    }
-
-    return 0;
-}
-
-struct handle_ops proc_ops = {
-        .read           = &proc_read,
-        .write          = &proc_write,
-        .close          = &proc_close,
-        .delete         = &proc_delete,
-        .attrquerybyhdl = &proc_attrquerybyhdl,
-        .attrsetbyhdl   = &proc_attrsetbyhdl,
-    };

+ 0 - 168
Pal/src/host/FreeBSD/db_rtld.c

@@ -1,168 +0,0 @@
-/* Copyright (C) 2014 Stony Brook University
-   This file is part of Graphene Library OS.
-
-   Graphene Library OS is free software: you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public License
-   as published by the Free Software Foundation, either version 3 of the
-   License, or (at your option) any later version.
-
-   Graphene Library OS is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
-
-/*
- * db_rtld.c
- *
- * This file contains utilities to load ELF binaries into the memory
- * and link them against each other.
- * The source code in this file is imported and modified from the GNU C
- * Library.
- */
-
-#include "pal_defs.h"
-#include "pal_freebsd_defs.h"
-#include "pal.h"
-#include "pal_internal.h"
-#include "pal_freebsd.h"
-#include "pal_debug.h"
-#include "pal_error.h"
-#include "pal_security.h"
-#include "pal_rtld.h"
-#include "api.h"
-
-#include <sysdeps/generic/ldsodefs.h>
-#include <elf/elf.h>
-#include <dlfcn.h>
-
-#include "elf-x86_64.h"
-
-/* This structure communicates dl state to the debugger.  The debugger
-   normally finds it via the DT_DEBUG entry in the dynamic section, but in
-   a statically-linked program there is no dynamic section for the debugger
-   to examine and it looks for this particular symbol name.  */
-struct r_debug pal_r_debug =
-        { 1, NULL, &pal_r_debug_state, RT_CONSISTENT, };
-
-/* This function exists solely to have a breakpoint set on it by the
-   debugger.  The debugger is supposed to find this function's address by
-   examining the r_brk member of struct r_debug, but GDB 4.15 in fact looks
-   for this particular symbol name in the PT_INTERP file.  */
-
-/* The special symbol name is set as breakpoint in gdb */
-void __attribute__((noinline))
-pal_r_debug_state (struct r_debug * rd, struct link_gdb_map * map)
-{
-    if (pal_sec.r_debug_state)
-        pal_sec.r_debug_state(rd, map);
-
-    asm volatile ("" ::: "memory");
-}
-
-extern __typeof(pal_r_debug_state) r_debug_state
-    __attribute ((alias ("pal_r_debug_state")));
-
-void _DkDebugAddMap (struct link_map * map)
-{
-    struct r_debug * dbg = pal_sec.r_debug ? : &pal_r_debug;
-    int len = map->l_name ? strlen(map->l_name) + 1 : 0;
-
-    struct link_map ** prev = &dbg->r_map, * last = NULL,
-                    * tmp = *prev;
-    while (tmp) {
-        if (tmp->l_addr == map->l_addr &&
-            tmp->l_ld == map->l_ld &&
-            !memcmp(tmp->l_name, map->l_name, len))
-            return;
-
-        last = tmp;
-        tmp = *(prev = &last->l_next);
-    }
-
-    struct link_gdb_map * m = malloc(sizeof(struct link_gdb_map) + len);
-    if (!m)
-        return;
-
-    if (len) {
-        m->l_name = (char *) m + sizeof(struct link_gdb_map);
-        memcpy((void *) m->l_name, map->l_name, len);
-    } else {
-        m->l_name = NULL;
-    }
-
-    m->l_addr = map->l_addr;
-    m->l_ld   = map->l_real_ld;
-
-    dbg->r_state = RT_ADD;
-    pal_r_debug_state(dbg, m);
-
-    *prev = (struct link_map *) m;
-    m->l_prev = last;
-    m->l_next = NULL;
-
-    dbg->r_state = RT_CONSISTENT;
-    pal_r_debug_state(dbg, NULL);
-}
-
-void _DkDebugDelMap (struct link_map * map)
-{
-    struct r_debug * dbg = pal_sec.r_debug ? : &pal_r_debug;
-    int len = map->l_name ? strlen(map->l_name) + 1 : 0;
-
-    struct link_gdb_map ** prev = (void *) &dbg->r_map;
-    struct link_gdb_map * last = NULL;
-    struct link_gdb_map * t = (void *) dbg->r_map, * m = NULL;
-
-    while (t) {
-        if (t->l_addr == map->l_addr &&
-            t->l_ld == map->l_ld &&
-            !memcmp(t->l_name, map->l_name, len)) {
-            m = t;
-            break;
-        }
-
-        last = t;
-        prev = (void *) last->l_next;
-        t = *prev;
-    }
-
-    if (!m)
-        return;
-
-    dbg->r_state = RT_DELETE;
-    pal_r_debug_state(dbg, m);
-
-    if (last)
-        last->l_next = m->l_next;
-    else
-        dbg->r_map = m->l_next;
-
-    if (m->l_next)
-        m->l_next->l_prev = (void *) last;
-
-    free(m);
-
-    dbg->r_state = RT_CONSISTENT;
-    pal_r_debug_state(dbg, NULL);
-}
-
-extern void setup_elf_hash (struct link_map *map);
-
-void setup_pal_map (struct link_map * pal_map)
-{
-    const ElfW(Ehdr) * header = (void *) pal_map->l_addr;
-
-    pal_map->l_real_ld = pal_map->l_ld = (void *) elf_machine_dynamic();
-    pal_map->l_type = OBJECT_RTLD;
-    pal_map->l_entry = header->e_entry;
-    pal_map->l_phdr  = (void *) (pal_map->l_addr + header->e_phoff);
-    pal_map->l_phnum = header->e_phnum;
-    setup_elf_hash(pal_map);
-
-    _DkDebugAddMap(pal_map);
-    pal_map->l_prev = pal_map->l_next = NULL;
-    loaded_maps = pal_map;
-}

+ 0 - 1477
Pal/src/host/FreeBSD/db_sockets.c

@@ -1,1477 +0,0 @@
-/* Copyright (C) 2014 Stony Brook University
-   This file is part of Graphene Library OS.
-
-   Graphene Library OS is free software: you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public License
-   as published by the Free Software Foundation, either version 3 of the
-   License, or (at your option) any later version.
-
-   Graphene Library OS is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
-
-/*
- * db_socket.c
- *
- * This file contains operands for streams with URIs that start with
- * "tcp:", "tcp.srv:", "udp:", "udp.srv:".
- */
-
-#include "pal_defs.h"
-#include "pal_freebsd_defs.h"
-#include "pal.h"
-#include "pal_internal.h"
-#include "pal_freebsd.h"
-#include "pal_debug.h"
-#include "pal_security.h"
-#include "pal_error.h"
-#include "api.h"
-
-#include <sys/types.h>
-#include <poll.h>
-typedef __kernel_pid_t pid_t;
-#include <fcntl.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <netinet/tcp.h>
-#include <errno.h>
-#include <sys/filio.h>
-
-/* FreeBSD doesn't have addr_check_any. */
-#undef ALLOW_BIND_ANY
-#define ALLOW_BIND_ANY 1
-
-/* 96 bytes is the minimal size of buffer to store a IPv4/IPv6
-   address */
-#define PAL_SOCKADDR_SIZE   96
-
-typedef uint16_t __be16;
-
-#define SOL_TCP			6
-#define TCP_CORK	TCP_NOPUSH
-
-static size_t addr_size(const struct sockaddr* addr) {
-    switch (addr->sa_family) {
-        case AF_INET:
-            return sizeof(struct sockaddr_in);
-        case AF_INET6:
-            return sizeof(struct sockaddr_in6);
-        default:
-            return 0;
-    }
-}
-
-/* parsing the string of uri, and fill in the socket address structure.
-   the latest pointer of uri, length of socket address are returned. */
-static int inet_parse_uri (char ** uri, struct sockaddr * addr, int * addrlen)
-{
-    char * tmp = *uri, * end;
-    char * addr_str = NULL, * port_str;
-    int af;
-    void * addr_buf;
-    int addr_len;
-    __be16 * port_buf;
-    int slen;
-
-    if (tmp[0] == '[') {
-        /* for IPv6, the address will be in the form of
-           "[xx:xx:xx:xx:xx:xx:xx:xx]:port". */
-        struct sockaddr_in6 * addr_in6 = (struct sockaddr_in6 *) addr;
-
-        slen = sizeof(struct sockaddr_in6);
-        memset(addr, 0, slen);
-
-        end = strchr(tmp + 1, ']');
-        if (!end || *(end + 1) != ':')
-            goto inval;
-
-        addr_str = tmp + 1;
-        addr_len = end - tmp + 1;
-        port_str = end + 2;
-        for (end = port_str ; *end >= '0' && *end <= '9' ; end++);
-        addr_in6->sin6_family = af = AF_INET6;
-        addr_buf = &addr_in6->sin6_addr.s6_addr;
-        port_buf = &addr_in6->sin6_port;
-    } else {
-        /* for IP, the address will be in the form of "x.x.x.x:port". */
-        struct sockaddr_in * addr_in = (struct sockaddr_in *) addr;
-
-        slen = sizeof(struct sockaddr_in);
-        memset(addr, 0, slen);
-
-        end = strchr(tmp, ':');
-        if (!end)
-            goto inval;
-
-        addr_str = tmp;
-        addr_len = end - tmp;
-        port_str = end + 1;
-        for (end = port_str ; *end >= '0' && *end <= '9' ; end++);
-        addr_in->sin_family = af = AF_INET;
-        addr_buf = &addr_in->sin_addr.s_addr;
-        port_buf = &addr_in->sin_port;
-    }
-
-    if (af == AF_INET) {
-        if (inet_pton4(addr_str, addr_len, addr_buf) < 0)
-            goto inval;
-    } else {
-        if (inet_pton6(addr_str, addr_len, addr_buf) < 0)
-            goto inval;
-    }
-
-    *port_buf = __htons(atoi(port_str));
-    *uri = *end ? end + 1 : NULL;
-
-    if (addrlen)
-        *addrlen = slen;
-
-    return 0;
-
-inval:
-    return -PAL_ERROR_INVAL;
-}
-
-/* create the string of uri from the given socket address */
-static int inet_create_uri (char * uri, int count, struct sockaddr * addr,
-                            int addrlen)
-{
-    int len = 0;
-
-    if (addr->sa_family == AF_INET) {
-        if (addrlen != sizeof(struct sockaddr_in))
-            return PAL_ERROR_INVAL;
-
-        struct sockaddr_in * addr_in = (struct sockaddr_in *) addr;
-        char * addr = (char *) &addr_in->sin_addr.s_addr;
-
-        /* for IP, the address will be in the form of "x.x.x.x:port". */
-        len = snprintf(uri, count, "%u.%u.%u.%u:%u",
-                       (unsigned char) addr[0],
-                       (unsigned char) addr[1],
-                       (unsigned char) addr[2],
-                       (unsigned char) addr[3],
-                       __ntohs(addr_in->sin_port));
-    } else if (addr->sa_family == AF_INET6) {
-        if (addrlen != sizeof(struct sockaddr_in6))
-            return PAL_ERROR_INVAL;
-
-        struct sockaddr_in6 * addr_in6 = (struct sockaddr_in6 *) addr;
-        short * addr = (short *) &addr_in6->sin6_addr.s6_addr;
-
-        /* for IPv6, the address will be in the form of
-           "[xx:xx:xx:xx:xx:xx:xx:xx]:port". */
-        len = snprintf(uri, count, "[%x:%x:%x:%x:%x:%x:%x:%x]:%u",
-                       addr[0], addr[1], addr[2], addr[3],
-                       addr[4], addr[5], addr[6], addr[7],
-                       __ntohs(addr_in6->sin6_port));
-    } else {
-        return -PAL_ERROR_INVAL;
-    }
-
-    return len;
-}
-
-/* parse the uri for a socket stream. The uri might have both binding
-   address and connecting address, or connecting address only. The form
-   of uri will be either "bind-addr:bind-port:connect-addr:connect-port"
-   or "addr:port". */
-static int socket_parse_uri (char * uri,
-                             struct sockaddr ** bind_addr, int * bind_addrlen,
-                             struct sockaddr ** dest_addr, int * dest_addrlen)
-{
-    int ret;
-
-    if (!bind_addr && !dest_addr)
-        return 0;
-
-    if (!uri || !(*uri)) {
-        if (bind_addr)
-            *bind_addr = NULL;
-        if (dest_addr)
-            *dest_addr = NULL;
-        return 0;
-    }
-
-    /* at least parse uri once */
-    if ((ret = inet_parse_uri(&uri, bind_addr ? *bind_addr : *dest_addr,
-                              bind_addr ? bind_addrlen : dest_addrlen)) < 0)
-        return ret;
-
-    if (!(bind_addr && dest_addr))
-        return 0;
-
-    /* if you reach here, it can only be connection address */
-    if (!uri || (ret = inet_parse_uri(&uri, *dest_addr, dest_addrlen)) < 0) {
-        *dest_addr = *bind_addr;
-        *dest_addrlen = *bind_addrlen;
-        *bind_addr = NULL;
-        *bind_addrlen = 0;
-    }
-
-    return 0;
-}
-
-/* fill in the PAL handle based on the file descriptors and address given. */
-static inline
-PAL_HANDLE socket_create_handle (int type, int fd, int options,
-                                 struct sockaddr * bind_addr, int bind_addrlen,
-                                 struct sockaddr * dest_addr, int dest_addrlen)
-{
-    PAL_HANDLE hdl = malloc(HANDLE_SIZE(sock) + (bind_addr ? bind_addrlen : 0) +
-                            (dest_addr ? dest_addrlen : 0));
-
-    if (!hdl)
-        return NULL;
-
-    options = HOST_SOCKET_OPTIONS(options);
-
-    memset(hdl, 0, sizeof(union pal_handle));
-    PAL_GET_TYPE(hdl) = type;
-    hdl->hdr.flags |= RFD(0)|(type != pal_type_tcpsrv ? WFD(0) : 0);
-    hdl->sock.fd = fd;
-    void * addr = (void *) hdl + HANDLE_SIZE(sock);
-    if (bind_addr) {
-        hdl->sock.bind = addr;
-        memcpy(addr, bind_addr, bind_addrlen);
-        addr += bind_addrlen;
-    } else {
-        hdl->sock.bind = NULL;
-    }
-    if (dest_addr) {
-        hdl->sock.conn = addr;
-        memcpy(addr, dest_addr, dest_addrlen);
-        addr += dest_addrlen;
-    } else {
-        hdl->sock.conn = NULL;
-    }
-
-    hdl->sock.nonblocking   = (options & SOCK_NONBLOCK) ?
-                              PAL_TRUE : PAL_FALSE;
-
-    hdl->sock.linger        = 0;
-
-    if (type == pal_type_tcpsrv) {
-        hdl->sock.receivebuf     = 0;
-        hdl->sock.sendbuf        = 0;
-    } else {
-        int ret, val, len = sizeof(int);
-
-        ret = INLINE_SYSCALL(getsockopt, 5, fd, SOL_SOCKET, SO_RCVBUF,
-                             &val, &len);
-        hdl->sock.receivebuf = IS_ERR(ret) ? 0 : val;
-
-        ret = INLINE_SYSCALL(getsockopt, 5, fd, SOL_SOCKET, SO_SNDBUF,
-                             &val, &len);
-        hdl->sock.sendbuf = IS_ERR(ret) ? 0 : val;
-    }
-
-    hdl->sock.receivetimeout = 0;
-    hdl->sock.sendtimeout    = 0;
-    hdl->sock.tcp_cork       = PAL_FALSE;
-    hdl->sock.tcp_keepalive  = PAL_FALSE;
-    hdl->sock.tcp_nodelay    = PAL_FALSE;
-    return hdl;
-}
-
-#if ALLOW_BIND_ANY == 0
-static bool check_zero (void * mem, size_t size)
-{
-    void * p = mem, * q = mem + size;
-
-    while (p < q) {
-        if (p <= q - sizeof(long)) {
-            if (*(long *) p)
-                return false;
-            p += sizeof(long);
-        } else if (p <= q - sizeof(int)) {
-            if (*(int *) p)
-                return false;
-            p += sizeof(int);
-        } else if (p <= q - sizeof(short)) {
-            if (*(short *) p)
-                return false;
-            p += sizeof(short);
-        } else {
-            if (*(char *) p)
-                return false;
-            p++;
-        }
-    }
-
-    return true;
-}
-
-/* check if an address is "Any" */
-static bool check_any_addr (struct sockaddr * addr)
-{
-    if (addr->sa_family == AF_INET) {
-        struct sockaddr_in * addr_in =
-                        (struct sockaddr_in *) addr;
-
-        return addr_in->sin_port == 0 &&
-               check_zero(&addr_in->sin_addr,
-                          sizeof(addr_in->sin_addr));
-    } else if (addr->sa_family == AF_INET6) {
-        struct sockaddr_in6 * addr_in6 =
-                        (struct sockaddr_in6 *) addr;
-
-        return addr_in6->sin6_port == 0 &&
-               check_zero(&addr_in6->sin6_addr,
-                          sizeof(addr_in6->sin6_addr));
-    }
-
-    return false;
-}
-#endif
-
-/* listen on a tcp socket */
-static int tcp_listen (PAL_HANDLE * handle, char * uri, int options)
-{
-    struct sockaddr buffer, * bind_addr = &buffer;
-    int bind_addrlen;
-    int ret, fd = -1;
-
-    if ((ret = socket_parse_uri(uri, &bind_addr, &bind_addrlen,
-                                NULL, NULL)) < 0)
-        return ret;
-
-#if ALLOW_BIND_ANY == 0
-    /* the socket need to have a binding address, a null address or an
-       any address is not allowed */
-    if (addr_check_any(bind_addr))
-       return -PAL_ERROR_INVAL;
-#endif
-
-    options = HOST_SOCKET_OPTIONS(options);
-    /* the socket need to have a binding address, a null address or an
-       any address is not allowed */
-    if (!bind_addr == 0)
-        return -PAL_ERROR_INVAL;
-
-    fd = INLINE_SYSCALL(socket, 3, bind_addr->sa_family,
-                        SOCK_STREAM|SOCK_CLOEXEC|options, 0);
-
-    if (IS_ERR(fd))
-        return -PAL_ERROR_DENIED;
-
-    /* must set the socket to be reuseable */
-    int reuseaddr = 1;
-    INLINE_SYSCALL(setsockopt, 5, fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr,
-                   sizeof(int)); // maybe SO_REUSEPORT
-
-    ret = INLINE_SYSCALL(bind, 3, fd, bind_addr, bind_addrlen);
-
-    if (IS_ERR(ret)) {
-        switch(ERRNO(ret)) {
-            case EINVAL:
-                ret = -PAL_ERROR_INVAL;
-                goto failed;
-            case EADDRINUSE:
-                ret = -PAL_ERROR_STREAMEXIST;
-                goto failed;
-            default:
-                ret = -PAL_ERROR_DENIED;
-                goto failed;
-        }
-    }
-
-    ret = INLINE_SYSCALL(listen, 2, fd, DEFAULT_BACKLOG);
-    if (IS_ERR(ret))
-        return -PAL_ERROR_DENIED;
-
-    *handle = socket_create_handle(pal_type_tcpsrv, fd, options,
-                                   bind_addr, bind_addrlen, NULL, 0);
-    if (!(*handle)) {
-        ret = -PAL_ERROR_NOMEM;
-        goto failed;
-    }
-
-    return 0;
-
-failed:
-    INLINE_SYSCALL(close, 1, fd);
-    return ret;
-}
-
-/* accept a tcp connection */
-static int tcp_accept (PAL_HANDLE handle, PAL_HANDLE * client)
-{
-    if (!IS_HANDLE_TYPE(handle, tcpsrv) ||
-        !handle->sock.bind || handle->sock.conn)
-        return -PAL_ERROR_NOTSERVER;
-
-    if (handle->sock.fd == PAL_IDX_POISON)
-        return -PAL_ERROR_BADHANDLE;
-
-    struct sockaddr * bind_addr = (struct sockaddr *) handle->sock.bind;
-    int bind_addrlen = addr_size(bind_addr);
-    struct sockaddr buffer;
-    socklen_t addrlen = sizeof(struct sockaddr);
-    int ret = 0;
-
-    int newfd = INLINE_SYSCALL(accept4, 4, handle->sock.fd, &buffer,
-                               &addrlen, SOCK_CLOEXEC);
-
-    if (IS_ERR(newfd))
-        switch(ERRNO(newfd)) {
-            case EWOULDBLOCK:
-                return -PAL_ERROR_TRYAGAIN;
-            case ECONNABORTED:
-                return -PAL_ERROR_STREAMNOTEXIST;
-            default:
-                return unix_to_pal_error(ERRNO(newfd));
-        }
-
-    struct sockaddr * dest_addr = &buffer;
-    int dest_addrlen = addrlen;
-
-    *client = socket_create_handle(pal_type_tcp, newfd, 0,
-                                   bind_addr, bind_addrlen,
-                                   dest_addr, dest_addrlen);
-
-    if (!(*client)) {
-        ret = -PAL_ERROR_NOMEM;
-        goto failed;
-    }
-
-    return 0;
-
-failed:
-    INLINE_SYSCALL(close, 1, newfd);
-    return ret;
-}
-
-/* connect on a tcp socket */
-static int tcp_connect (PAL_HANDLE * handle, char * uri, int options)
-{
-    struct sockaddr buffer[3];
-    struct sockaddr * bind_addr = buffer, * dest_addr = buffer + 1;
-    int bind_addrlen, dest_addrlen;
-    int ret, fd = -1;
-
-    options = HOST_SOCKET_OPTIONS(options);
-
-    /* accepting two kind of different uri:
-       dest-ip:dest-port or bind-ip:bind-port:dest-ip:dest-port */
-    if ((ret = socket_parse_uri(uri, &bind_addr, &bind_addrlen,
-                                &dest_addr, &dest_addrlen)) < 0)
-        return ret;
-
-    if (!dest_addr)
-        return -PAL_ERROR_INVAL;
-
-    if (bind_addr && bind_addr->sa_family != dest_addr->sa_family)
-        return -PAL_ERROR_INVAL;
-
-#if ALLOW_BIND_ANY == 0
-    /* the socket need to have a binding address, a null address or an
-       any address is not allowed */
-    if (bind_addr && addr_check_any(bind_addr))
-       return -PAL_ERROR_INVAL;
-#endif
-
-    fd = INLINE_SYSCALL(socket, 3, dest_addr->sa_family,
-                        SOCK_STREAM|SOCK_CLOEXEC|options, 0);
-    if (IS_ERR(fd))
-        return -PAL_ERROR_DENIED;
-
-    if (bind_addr) {
-        if (IS_ERR(ret)) {
-            INLINE_SYSCALL(close, 1, fd);
-            switch (ERRNO(ret)) {
-                case EADDRINUSE:
-                    ret = -PAL_ERROR_STREAMEXIST;
-                    goto failed;
-                case EADDRNOTAVAIL:
-                    ret = -PAL_ERROR_ADDRNOTEXIST;
-                    goto failed;
-                default:
-                    ret = unix_to_pal_error(ERRNO(ret));
-                    goto failed;
-            }
-        }
-    }
-
-    ret = INLINE_SYSCALL(connect, 3, fd, dest_addr, dest_addrlen);
-
-    if (IS_ERR(ret)) {
-        ret = unix_to_pal_error(ERRNO(ret));
-        goto failed;
-    }
-
-    if (!bind_addr) {
-        /* save some space to get socket address */
-        bind_addr = buffer + 2;
-        bind_addrlen = sizeof(struct sockaddr);
-
-        /* call getsockname to get socket address */
-        if ((ret = INLINE_SYSCALL(getsockname, 3, fd,
-                                  bind_addr, &bind_addrlen)) < 0)
-            bind_addr = NULL;
-    }
-
-    *handle = socket_create_handle(pal_type_tcp, fd, options,
-                                   bind_addr, bind_addrlen,
-                                   dest_addr, dest_addrlen);
-
-    if (!(*handle)) {
-        ret = -PAL_ERROR_NOMEM;
-        goto failed;
-    }
-
-    return 0;
-
-failed:
-    INLINE_SYSCALL(close, 1, fd);
-    return ret;
-}
-
-/* 'open' operation of tcp stream */
-static int tcp_open (PAL_HANDLE *handle, const char * type, const char * uri,
-                     int access, int share, int create, int options)
-{
-    int uri_len = strlen(uri) + 1;
-
-    if (uri_len > PAL_SOCKADDR_SIZE)
-        return -PAL_ERROR_TOOLONG;
-
-    char uri_buf[PAL_SOCKADDR_SIZE];
-    memcpy(uri_buf, uri, uri_len);
-
-    if (!strcmp_static(type, URI_TYPE_TCP_SRV))
-        return tcp_listen(handle, uri_buf, options);
-
-    if (!strcmp_static(type, URI_TYPE_TCP))
-        return tcp_connect(handle, uri_buf, options);
-
-    return -PAL_ERROR_NOTSUPPORT;
-}
-
-/* 'read' operation of tcp stream */
-static int tcp_read (PAL_HANDLE handle, int offset, int len, void * buf)
-{
-    if (!IS_HANDLE_TYPE(handle, tcp) || !handle->sock.conn)
-        return -PAL_ERROR_NOTCONNECTION;
-
-    if (handle->sock.fd == PAL_IDX_POISON)
-        return -PAL_ERROR_ENDOFSTREAM;
-
-    struct msghdr hdr;
-    struct iovec iov;
-    iov.iov_base = buf;
-    iov.iov_len = len;
-    hdr.msg_name = NULL;
-    hdr.msg_namelen = 0;
-    hdr.msg_iov = &iov;
-    hdr.msg_iovlen = 1;
-    hdr.msg_control = NULL;
-    hdr.msg_controllen = 0;
-    hdr.msg_flags = 0;
-
-    int bytes = INLINE_SYSCALL(recvmsg, 3, handle->sock.fd, &hdr, 0);
-
-    if (IS_ERR(bytes))
-        switch (ERRNO(bytes)) {
-            case EWOULDBLOCK:
-                return -PAL_ERROR_TRYAGAIN;
-            default:
-                return unix_to_pal_error(ERRNO(bytes));
-        }
-
-    if (!bytes)
-        return -PAL_ERROR_ENDOFSTREAM;
-
-    return bytes;
-}
-
-/* write' operation of tcp stream */
-static int tcp_write (PAL_HANDLE handle, int offset, int len, const void * buf)
-{
-    if (!IS_HANDLE_TYPE(handle, tcp) || !handle->sock.conn)
-        return -PAL_ERROR_NOTCONNECTION;
-
-    if (handle->sock.fd == PAL_IDX_POISON)
-        return -PAL_ERROR_CONNFAILED;
-
-    struct msghdr hdr;
-    struct iovec iov;
-    iov.iov_base = (void *) buf;
-    iov.iov_len = len;
-    hdr.msg_name = NULL;
-    hdr.msg_namelen = 0;
-    hdr.msg_iov = &iov;
-    hdr.msg_iovlen = 1;
-    hdr.msg_control = NULL;
-    hdr.msg_controllen = 0;
-    hdr.msg_flags = 0;
-
-    int bytes = INLINE_SYSCALL(sendmsg, 3, handle->sock.fd, &hdr, MSG_NOSIGNAL);
-
-    if (IS_ERR(bytes))
-        switch(ERRNO(bytes)) {
-            case ECONNRESET:
-            case EPIPE:
-                return -PAL_ERROR_CONNFAILED;
-            case EWOULDBLOCK:
-                handle->hdr.flags &= ~WRITABLE(0);
-                return -PAL_ERROR_TRYAGAIN;
-            default:
-                return unix_to_pal_error(ERRNO(bytes));
-        }
-
-    if (bytes == len)
-        handle->hdr.flags |= WRITABLE(0);
-    else
-        handle->hdr.flags &= ~WRITABLE(0);
-
-    return bytes;
-}
-
-/* used by 'open' operation of tcp stream for bound socket */
-static int udp_bind (PAL_HANDLE * handle, char * uri, int options)
-{
-    struct sockaddr buffer, * bind_addr = &buffer;
-    int bind_addrlen;
-    int ret = 0, fd = -1;
-
-    if ((ret = socket_parse_uri(uri, &bind_addr, &bind_addrlen,
-                                NULL, NULL)) < 0)
-        return ret;
-
-    assert(bind_addr);
-    assert(bind_addrlen == addr_size(bind_addr));
-
-#if ALLOW_BIND_ANY == 0
-    /* the socket need to have a binding address, a null address or an
-       any address is not allowed */
-    if (addr_check_any(bind_addr))
-       return -PAL_ERROR_INVAL;
-#endif
-
-    options = HOST_SOCKET_OPTIONS(options);
-    fd = INLINE_SYSCALL(socket, 3, bind_addr->sa_family,
-                        SOCK_DGRAM|SOCK_CLOEXEC|options, 0);
-
-    if (IS_ERR(fd))
-        return -PAL_ERROR_DENIED;
-
-    ret = INLINE_SYSCALL(bind, 3, fd, bind_addr, bind_addrlen);
-
-    if (IS_ERR(ret)) {
-        switch (ERRNO(ret)) {
-            case EADDRINUSE:
-                ret = -PAL_ERROR_STREAMEXIST;
-                goto failed;
-            case EADDRNOTAVAIL:
-                ret = -PAL_ERROR_ADDRNOTEXIST;
-                goto failed;
-            default:
-                ret = unix_to_pal_error(ERRNO(ret));
-                goto failed;
-        }
-    }
-
-    *handle = socket_create_handle(pal_type_udpsrv, fd, options,
-                                   bind_addr, bind_addrlen, NULL, 0);
-
-    if (!(*handle)) {
-        ret = -ENOMEM;
-        goto failed;
-    }
-
-    return 0;
-
-failed:
-    INLINE_SYSCALL(close, 1, fd);
-    return ret;
-}
-
-/* used by 'open' operation of tcp stream for connected socket */
-static int udp_connect (PAL_HANDLE * handle, char * uri, int options)
-{
-    struct sockaddr buffer[2];
-    struct sockaddr * bind_addr = buffer, * dest_addr = buffer + 1;
-    int bind_addrlen, dest_addrlen;
-    int ret, fd = -1;
-
-    if ((ret = socket_parse_uri(uri, &bind_addr, &bind_addrlen,
-                                &dest_addr, &dest_addrlen)) < 0)
-        return ret;
-
-#if ALLOW_BIND_ANY == 0
-    /* the socket need to have a binding address, a null address or an
-       any address is not allowed */
-    if (bind_addr && addr_check_any(bind_addr))
-       return -PAL_ERROR_INVAL;
-#endif
-
-    options = HOST_SOCKET_OPTIONS(options);
-
-    fd = INLINE_SYSCALL(socket, 3, dest_addr ? dest_addr->sa_family : AF_INET,
-                        SOCK_DGRAM|SOCK_CLOEXEC|options, 0);
-
-    if (IS_ERR(fd))
-        return -PAL_ERROR_DENIED;
-
-    if (bind_addr) {
-        ret = INLINE_SYSCALL(bind, 3, fd, bind_addr, bind_addrlen);
-
-        if (IS_ERR(ret)) {
-            switch (ERRNO(ret)) {
-                case EADDRINUSE:
-                    ret = -PAL_ERROR_STREAMEXIST;
-                    goto failed;
-                case EADDRNOTAVAIL:
-                    ret = -PAL_ERROR_ADDRNOTEXIST;
-                    goto failed;
-                default:
-                    ret = unix_to_pal_error(ERRNO(ret));
-                    goto failed;
-            }
-        }
-    }
-
-    *handle = socket_create_handle(dest_addr ? pal_type_udp : pal_type_udpsrv,
-                                   fd, options,
-                                   bind_addr, bind_addrlen,
-                                   dest_addr, dest_addrlen);
-
-    if (!(*handle)) {
-        ret = -ENOMEM;
-        goto failed;
-    }
-
-    return 0;
-
-failed:
-    INLINE_SYSCALL(close, 1, fd);
-    return ret;
-}
-
-static int udp_open (PAL_HANDLE *hdl, const char * type, const char * uri,
-                     int access, int share, int create, int options)
-{
-    char buf[PAL_SOCKADDR_SIZE];
-    int len = strlen(uri);
-
-    if (len >= PAL_SOCKADDR_SIZE)
-        return -PAL_ERROR_TOOLONG;
-
-    memcpy(buf, uri, len + 1);
-    options &= PAL_OPTION_MASK;
-
-    if (!strcmp_static(type, URI_TYPE_UDP_SRV))
-        return udp_bind(hdl, buf, options);
-
-    if (!strcmp_static(type, URI_TYPE_UDP))
-        return udp_connect(hdl, buf, options);
-
-    return -PAL_ERROR_NOTSUPPORT;
-}
-
-static int udp_receive (PAL_HANDLE handle, int offset, int len, void * buf)
-{
-    if (!IS_HANDLE_TYPE(handle, udp))
-        return -PAL_ERROR_NOTCONNECTION;
-
-    if (handle->sock.fd == PAL_IDX_POISON)
-        return -PAL_ERROR_BADHANDLE;
-
-    struct msghdr hdr;
-    struct iovec iov;
-    iov.iov_base = buf;
-    iov.iov_len = len;
-    hdr.msg_name = NULL;
-    hdr.msg_namelen = 0;
-    hdr.msg_iov = &iov;
-    hdr.msg_iovlen = 1;
-    hdr.msg_control = NULL;
-    hdr.msg_controllen = 0;
-    hdr.msg_flags = 0;
-
-    int bytes = INLINE_SYSCALL(recvmsg, 3, handle->sock.fd, &hdr, 0);
-
-    if (IS_ERR(bytes))
-        switch(ERRNO(bytes)) {
-            case EWOULDBLOCK:
-                return -PAL_ERROR_TRYAGAIN;
-            case EINTR:
-                return -PAL_ERROR_INTERRUPTED;
-            default:
-                return unix_to_pal_error(ERRNO(bytes));
-        }
-
-    return bytes;
-}
-
-static int udp_receivebyaddr (PAL_HANDLE handle, int offset, int len,
-                              void * buf, char * addr, int addrlen)
-{
-    if (!IS_HANDLE_TYPE(handle, udpsrv))
-        return -PAL_ERROR_NOTCONNECTION;
-
-    if (handle->sock.fd == PAL_IDX_POISON)
-        return -PAL_ERROR_BADHANDLE;
-
-    struct sockaddr conn_addr;
-    socklen_t conn_addrlen = sizeof(struct sockaddr);
-
-    struct msghdr hdr;
-    struct iovec iov;
-    iov.iov_base = buf;
-    iov.iov_len = len;
-    hdr.msg_name = &conn_addr;
-    hdr.msg_namelen = conn_addrlen;
-    hdr.msg_iov = &iov;
-    hdr.msg_iovlen = 1;
-    hdr.msg_control = NULL;
-    hdr.msg_controllen = 0;
-    hdr.msg_flags = 0;
-
-    int bytes = INLINE_SYSCALL(recvmsg, 3, handle->sock.fd, &hdr, 0);
-
-    if (IS_ERR(bytes))
-        switch(ERRNO(bytes)) {
-            case EWOULDBLOCK:
-                return -PAL_ERROR_TRYAGAIN;
-            case EINTR:
-                return -PAL_ERROR_INTERRUPTED;
-            case ECONNREFUSED:
-                return -PAL_ERROR_STREAMNOTEXIST;
-            default:
-                return unix_to_pal_error(ERRNO(bytes));
-        }
-
-    char * tmp = strcpy_static(addr, URI_PREFIX_UDP, addrlen);
-    if (!tmp)
-        return -PAL_ERROR_OVERFLOW;
-
-    inet_create_uri(tmp, addr + addrlen - tmp, &conn_addr, hdr.msg_namelen);
-
-    return bytes;
-}
-
-static int udp_send (PAL_HANDLE handle, int offset, int len, const void * buf)
-{
-    if (!IS_HANDLE_TYPE(handle, udp))
-        return -PAL_ERROR_NOTCONNECTION;
-
-    if (handle->sock.fd == PAL_IDX_POISON)
-        return -PAL_ERROR_BADHANDLE;
-
-    struct msghdr hdr;
-    struct iovec iov;
-    iov.iov_base = (void *) buf;
-    iov.iov_len = len;
-    hdr.msg_name = handle->sock.conn;
-    hdr.msg_namelen = addr_size(handle->sock.conn);
-    hdr.msg_iov = &iov;
-    hdr.msg_iovlen = 1;
-    hdr.msg_control = NULL;
-    hdr.msg_controllen = 0;
-    hdr.msg_flags = 0;
-
-    int bytes = INLINE_SYSCALL(sendmsg, 3, handle->sock.fd, &hdr, MSG_NOSIGNAL);
-
-    if (IS_ERR(bytes))
-        switch(ERRNO(bytes)) {
-            case EAGAIN:
-                handle->hdr.flags &= ~WRITABLE(0);
-                return -PAL_ERROR_TRYAGAIN;
-            case ECONNRESET:
-            case EPIPE:
-                return -PAL_ERROR_CONNFAILED;
-            default:
-                return unix_to_pal_error(ERRNO(bytes));
-        }
-
-    if (bytes == len)
-        handle->hdr.flags |= WRITABLE(0);
-    else
-        handle->hdr.flags &= ~WRITABLE(0);
-
-    return bytes;
-}
-
-static int udp_sendbyaddr (PAL_HANDLE handle, int offset, int len,
-                           const void * buf, const char * addr, int addrlen)
-{
-    if (!IS_HANDLE_TYPE(handle, udpsrv))
-        return -PAL_ERROR_NOTCONNECTION;
-
-    if (handle->sock.fd == PAL_IDX_POISON)
-        return -PAL_ERROR_BADHANDLE;
-
-    if (!strstartswith_static(addr, URI_PREFIX_UDP))
-        return -PAL_ERROR_INVAL;
-
-    addr += static_strlen(URI_PREFIX_UDP);
-    addrlen -= static_strlen(URI_PREFIX_UDP);
-    char * addrbuf = __alloca(addrlen + 1);
-    memcpy(addrbuf, addr, addrlen + 1);
-
-    struct sockaddr conn_addr;
-    int conn_addrlen;
-
-    int ret = inet_parse_uri(&addrbuf, &conn_addr, &conn_addrlen);
-    if (ret < 0)
-        return ret;
-
-    struct msghdr hdr;
-    struct iovec iov;
-    iov.iov_base = (void *) buf;
-    iov.iov_len = len;
-    hdr.msg_name = &conn_addr;
-    hdr.msg_namelen = conn_addrlen;
-    hdr.msg_iov = &iov;
-    hdr.msg_iovlen = 1;
-    hdr.msg_control = NULL;
-    hdr.msg_controllen = 0;
-    hdr.msg_flags = 0;
-
-    int bytes = INLINE_SYSCALL(sendmsg, 3, handle->sock.fd, &hdr, MSG_NOSIGNAL);
-
-    if (IS_ERR(bytes))
-        switch(ERRNO(bytes)) {
-            case ECONNRESET:
-            case EPIPE:
-                return -PAL_ERROR_CONNFAILED;
-            case EAGAIN:
-                handle->hdr.flags &= ~WRITABLE(0);
-            default:
-                return unix_to_pal_error(ERRNO(bytes));
-        }
-
-    if (bytes == len)
-        handle->hdr.flags |= WRITABLE(0);
-    else
-        handle->hdr.flags &= ~WRITABLE(0);
-
-    return bytes;
-}
-
-static int socket_delete (PAL_HANDLE handle, int access)
-{
-    if (handle->sock.fd == PAL_IDX_POISON)
-        return 0;
-
-    if (!IS_HANDLE_TYPE(handle, tcp) && access)
-        return -PAL_ERROR_INVAL;
-
-    if (IS_HANDLE_TYPE(handle, tcp) || IS_HANDLE_TYPE(handle, tcpsrv)) {
-        int shutdown;
-        switch (access) {
-            case 0:
-                shutdown = SHUT_RDWR;
-                break;
-            case PAL_DELETE_RD:
-                shutdown = SHUT_RD;
-                break;
-            case PAL_DELETE_WR:
-                shutdown = SHUT_WR;
-                break;
-            default:
-                return -PAL_ERROR_INVAL;
-        }
-
-        INLINE_SYSCALL(shutdown, 2, handle->sock.fd, shutdown);
-    }
-
-    return 0;
-}
-
-static int socket_close (PAL_HANDLE handle)
-{
-    if (handle->sock.fd != PAL_IDX_POISON) {
-        INLINE_SYSCALL(close, 1, handle->sock.fd);
-        handle->sock.fd = PAL_IDX_POISON;
-    }
-
-    if (handle->sock.bind)
-        handle->sock.bind = NULL;
-
-    if (handle->sock.conn)
-        handle->sock.conn = NULL;
-
-    return 0;
-}
-
-static int socket_attrquerybyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR  * attr)
-{
-    if (handle->sock.fd == PAL_IDX_POISON)
-        return -PAL_ERROR_BADHANDLE;
-
-    int fd = handle->sock.fd, ret, val;
-
-    if (handle->sock.conn) {
-        /* try use ioctl FIONEAD to get the size of socket */
-        ret = INLINE_SYSCALL(ioctl, 3, fd, FIONREAD, &val);
-
-        if (IS_ERR(ret))
-            return unix_to_pal_error(ERRNO(ret));
-
-        attr->pending_size = val;
-        attr->readable = !!attr->pending_size > 0;
-    } else {
-        attr->readable = !attr->disconnected;
-    }
-
-    attr->handle_type           = handle->hdr.type;
-    attr->disconnected          = handle->hdr.flags & ERROR(0);
-    attr->nonblocking           = handle->sock.nonblocking;
-    attr->writable              = handle->hdr.flags & WRITABLE(0);
-    attr->socket.linger         = handle->sock.linger;
-    attr->socket.receivebuf     = handle->sock.receivebuf;
-    attr->socket.sendbuf        = handle->sock.sendbuf;
-    attr->socket.receivetimeout = handle->sock.receivetimeout;
-    attr->socket.sendtimeout    = handle->sock.sendtimeout;
-    attr->socket.tcp_cork       = handle->sock.tcp_cork;
-    attr->socket.tcp_keepalive  = handle->sock.tcp_keepalive;
-    attr->socket.tcp_nodelay    = handle->sock.tcp_nodelay;
-    return 0;
-}
-
-static int socket_attrsetbyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR  * attr)
-{
-    if (handle->sock.fd == PAL_IDX_POISON)
-        return -PAL_ERROR_BADHANDLE;
-
-    int fd = handle->sock.fd, ret, val;
-
-    if (attr->nonblocking != handle->sock.nonblocking) {
-        ret = INLINE_SYSCALL(fcntl, 3, fd, F_SETFL,
-                             attr->nonblocking ? O_NONBLOCK : 0);
-
-        if (IS_ERR(ret))
-            return unix_to_pal_error(ERRNO(ret));
-
-        handle->sock.nonblocking = attr->nonblocking;
-    }
-
-    if (IS_HANDLE_TYPE(handle, tcpsrv)) {
-
-        struct __kernel_linger {
-            int l_onoff;
-            int l_linger;
-        };
-
-        if (attr->socket.linger != handle->sock.linger) {
-
-            struct __kernel_linger l;
-            l.l_onoff = attr->socket.linger ? 1 : 0;
-            l.l_linger = attr->socket.linger;
-            ret = INLINE_SYSCALL(setsockopt, 5, fd, SOL_SOCKET, SO_LINGER,
-                                 &l, sizeof(struct __kernel_linger));
-
-            if (IS_ERR(ret))
-                return unix_to_pal_error(ERRNO(ret));
-
-            handle->sock.linger = attr->socket.linger;
-        }
-
-        if (attr->socket.receivebuf != handle->sock.receivebuf) {
-            int val = attr->socket.receivebuf;
-            ret = INLINE_SYSCALL(setsockopt, 5, fd, SOL_SOCKET, SO_RCVBUF,
-                                 &val, sizeof(int));
-
-            if (IS_ERR(ret))
-                return unix_to_pal_error(ERRNO(ret));
-
-            handle->sock.receivebuf = attr->socket.receivebuf;
-        }
-
-        if (attr->socket.sendbuf != handle->sock.sendbuf) {
-            int val = attr->socket.sendbuf;
-            ret = INLINE_SYSCALL(setsockopt, 5, fd, SOL_SOCKET, SO_SNDBUF,
-                                 &val, sizeof(int));
-
-            if (IS_ERR(ret))
-                return unix_to_pal_error(ERRNO(ret));
-
-            handle->sock.sendbuf = attr->socket.sendbuf;
-        }
-
-        if (attr->socket.receivetimeout != handle->sock.receivetimeout) {
-            int val = attr->socket.receivetimeout;
-            ret = INLINE_SYSCALL(setsockopt, 5, fd, SOL_SOCKET, SO_RCVTIMEO,
-                                 &val, sizeof(int));
-
-            if (IS_ERR(ret))
-                return unix_to_pal_error(ERRNO(ret));
-
-            handle->sock.receivetimeout = attr->socket.receivetimeout;
-        }
-
-        if (attr->socket.sendtimeout != handle->sock.sendtimeout) {
-            int val = attr->socket.sendtimeout;
-            ret = INLINE_SYSCALL(setsockopt, 5, fd, SOL_SOCKET, SO_SNDTIMEO,
-                                 &val, sizeof(int));
-
-            if (IS_ERR(ret))
-                return unix_to_pal_error(ERRNO(ret));
-
-            handle->sock.sendtimeout = attr->socket.sendtimeout;
-        }
-    }
-
-    if (IS_HANDLE_TYPE(handle, tcp) || IS_HANDLE_TYPE(handle, tcpsrv)) {
-
-        if (attr->socket.tcp_cork != handle->sock.tcp_cork) {
-            val = attr->socket.tcp_cork ? 1 : 0;
-            ret = INLINE_SYSCALL(setsockopt, 5, fd, SOL_TCP, TCP_CORK,
-                                 &val, sizeof(int));
-
-            if (IS_ERR(ret))
-                return unix_to_pal_error(ERRNO(ret));
-
-            handle->sock.tcp_cork = attr->socket.tcp_cork;
-        }
-
-        if (attr->socket.tcp_keepalive != handle->sock.tcp_keepalive) {
-            val = attr->socket.tcp_keepalive ? 1 : 0;
-            ret = INLINE_SYSCALL(setsockopt, 5, fd, SOL_SOCKET, SO_KEEPALIVE,
-                                 &val, sizeof(int));
-
-            if (IS_ERR(ret))
-                return unix_to_pal_error(ERRNO(ret));
-
-            handle->sock.tcp_keepalive = attr->socket.tcp_keepalive;
-        }
-
-        if (attr->socket.tcp_nodelay != handle->sock.tcp_nodelay) {
-            val = attr->socket.tcp_nodelay ? 1 : 0;
-            ret = INLINE_SYSCALL(setsockopt, 5, fd, SOL_TCP, TCP_NODELAY,
-                                 &val, sizeof(int));
-
-            if (IS_ERR(ret))
-                return unix_to_pal_error(ERRNO(ret));
-
-            handle->sock.tcp_nodelay = attr->socket.tcp_nodelay;
-        }
-    }
-
-    return 0;
-}
-static int socket_getname (PAL_HANDLE handle, char * buffer, int count)
-{
-    int old_count = count;
-    int ret;
-
-    const char * prefix = NULL;
-    int prefix_len = 0;
-    struct sockaddr * bind_addr = NULL, * dest_addr = NULL;
-
-    switch (PAL_GET_TYPE(handle)) {
-        case pal_type_tcpsrv:
-            prefix_len = static_strlen(URI_TYPE_TCP_SRV);
-            prefix = URI_TYPE_TCP_SRV;
-            bind_addr = handle->sock.bind;
-            break;
-        case pal_type_tcp:
-            prefix_len = static_strlen(URI_TYPE_TCP);
-            prefix = URI_TYPE_TCP;
-            bind_addr = handle->sock.bind;
-            dest_addr = handle->sock.conn;
-            break;
-        case pal_type_udpsrv:
-            prefix_len = static_strlen(URI_TYPE_UDP_SRV);
-            prefix = URI_TYPE_UDP_SRV;
-            bind_addr = handle->sock.bind;
-            break;
-        case pal_type_udp:
-            prefix_len = static_strlen(URI_TYPE_UDP);
-            prefix = URI_TYPE_UDP;
-            bind_addr = handle->sock.bind;
-            dest_addr = handle->sock.conn;
-            break;
-        default:
-            return -PAL_ERROR_INVAL;
-    }
-
-    if (prefix_len >= count)
-        return -PAL_ERROR_OVERFLOW;
-
-    memcpy(buffer, prefix, prefix_len + 1);
-    buffer += prefix_len;
-    count -= prefix_len;
-
-    for (int i = 0 ; i < 2 ; i++) {
-        struct sockaddr * addr = i ? dest_addr : bind_addr;
-        if (addr) {
-            if (count <= 1)
-                return -PAL_ERROR_OVERFLOW;
-
-            buffer[0] = ':';
-            buffer[1] = 0;
-            buffer++;
-            count--;
-
-            if ((ret = inet_create_uri(buffer, count, addr,
-                                       addr_size(addr))) < 0)
-                return ret;
-
-            buffer += ret;
-            count -= ret;
-        }
-    }
-
-    return old_count - count;
-}
-
-struct handle_ops tcp_ops = {
-        .getname        = &socket_getname,
-        .open           = &tcp_open,
-        .waitforclient  = &tcp_accept,
-        .read           = &tcp_read,
-        .write          = &tcp_write,
-        .delete         = &socket_delete,
-        .close          = &socket_close,
-        .attrquerybyhdl = &socket_attrquerybyhdl,
-        .attrsetbyhdl   = &socket_attrsetbyhdl,
-    };
-
-struct handle_ops udp_ops = {
-        .getname        = &socket_getname,
-        .open           = &udp_open,
-        .read           = &udp_receive,
-        .write          = &udp_send,
-        .delete         = &socket_delete,
-        .close          = &socket_close,
-        .attrquerybyhdl = &socket_attrquerybyhdl,
-        .attrsetbyhdl   = &socket_attrsetbyhdl,
-    };
-
-struct handle_ops udpsrv_ops = {
-        .getname        = &socket_getname,
-        .open           = &udp_open,
-        .readbyaddr     = &udp_receivebyaddr,
-        .writebyaddr    = &udp_sendbyaddr,
-        .delete         = &socket_delete,
-        .close          = &socket_close,
-        .attrquerybyhdl = &socket_attrquerybyhdl,
-        .attrsetbyhdl   = &socket_attrsetbyhdl,
-    };
-
-static int mcast_s (PAL_HANDLE handle, int port)
-{
-    handle->mcast.srv = PAL_IDX_POISON;
-    int ret = 0;
-
-    int fd = INLINE_SYSCALL(socket, 3, AF_INET, SOCK_DGRAM, 0);
-
-    if (IS_ERR(fd))
-        return -PAL_ERROR_DENIED;
-
-    struct in_addr local;
-    local.s_addr  = INADDR_ANY;
-    ret = INLINE_SYSCALL(setsockopt, 5, fd, IPPROTO_IP, IP_MULTICAST_IF,
-                         &local, sizeof(local));
-    if (IS_ERR(ret))
-        return -PAL_ERROR_DENIED;
-
-    handle->hdr.flags |= WFD(1)|WRITABLE(1);
-    handle->mcast.srv = fd;
-    return 0;
-}
-
-static int mcast_c (PAL_HANDLE handle, int port)
-{
-    handle->mcast.cli = PAL_IDX_POISON;
-    int ret = 0;
-
-    int fd = INLINE_SYSCALL(socket, 3, AF_INET, SOCK_DGRAM, 0);
-
-    if (IS_ERR(fd))
-        return -PAL_ERROR_DENIED;
-
-    int reuse = 1;
-    INLINE_SYSCALL(setsockopt, 5, fd, SOL_SOCKET, SO_REUSEPORT,
-                   &reuse, sizeof(reuse));
-
-    struct sockaddr_in addr;
-    addr.sin_family = AF_INET;
-    addr.sin_addr.s_addr = htonl(INADDR_ANY);
-    addr.sin_port = htons(port);
-    ret = INLINE_SYSCALL(bind, 3, fd, &addr, sizeof(addr));
-
-    if (IS_ERR(ret))
-        return -PAL_ERROR_DENIED;
-
-    struct in_addr local;
-    local.s_addr = INADDR_ANY;
-    ret = INLINE_SYSCALL(setsockopt, 5, fd, IPPROTO_IP, IP_MULTICAST_IF,
-                         &local, sizeof(local));
-    if (IS_ERR(ret))
-        return -PAL_ERROR_DENIED;
-
-    struct ip_mreq group;
-    inet_pton4(GRAPHENE_MCAST_GROUP, sizeof(GRAPHENE_MCAST_GROUP) - 1,
-               &group.imr_multiaddr.s_addr);
-    group.imr_interface.s_addr = htonl(INADDR_ANY);
-    ret = INLINE_SYSCALL(setsockopt, 5, fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
-                         &group, sizeof(group));
-    if (IS_ERR(ret))
-        return -PAL_ERROR_DENIED;
-
-    handle->mcast.cli = fd;
-    handle->mcast.nonblocking = PAL_FALSE;
-    return 0;
-}
-
-PAL_HANDLE _DkBroadcastStreamOpen (void)
-{
-    if (!pal_sec.mcast_port) {
-        unsigned short mcast_port;
-        _DkFastRandomBitsRead(&mcast_port, sizeof(unsigned short));
-        if (mcast_port < 1024)
-            mcast_port += 1024;
-        pal_sec.mcast_port = mcast_port > 1024 ? mcast_port : mcast_port + 1204;
-    }
-
-    PAL_HANDLE hdl = malloc(HANDLE_SIZE(mcast));
-    SET_HANDLE_TYPE(hdl, mcast);
-    mcast_s(hdl, pal_sec.mcast_port);
-    mcast_c(hdl, pal_sec.mcast_port);
-    hdl->mcast.port = pal_sec.mcast_port;
-    return hdl;
-}
-
-static int mcast_send (PAL_HANDLE handle, int offset, int size,
-                       const void * buf)
-{
-    if (handle->mcast.srv == PAL_IDX_POISON)
-        return -PAL_ERROR_BADHANDLE;
-
-    struct sockaddr_in addr;
-    addr.sin_family = AF_INET;
-    inet_pton4(GRAPHENE_MCAST_GROUP, sizeof(GRAPHENE_MCAST_GROUP) - 1,
-               &addr.sin_addr.s_addr);
-    addr.sin_port = htons(handle->mcast.port);
-
-    struct msghdr hdr;
-    struct iovec iov;
-    iov.iov_base = (void *) buf;
-    iov.iov_len = size;
-    hdr.msg_name = &addr;
-    hdr.msg_namelen = sizeof(addr);
-    hdr.msg_iov = &iov;
-    hdr.msg_iovlen = 1;
-    hdr.msg_control = NULL;
-    hdr.msg_controllen = 0;
-    hdr.msg_flags = 0;
-
-    int bytes = INLINE_SYSCALL(sendmsg, 3, handle->mcast.srv, &hdr,
-                               MSG_NOSIGNAL);
-
-    if (IS_ERR(bytes))
-        switch(ERRNO(bytes)) {
-            case ECONNRESET:
-            case EPIPE:
-                return -PAL_ERROR_CONNFAILED;
-            case EAGAIN:
-                handle->hdr.flags &= ~WRITABLE(1);
-                /* fallthrough */
-            default:
-                return unix_to_pal_error(ERRNO(bytes));
-        }
-
-    if (bytes == size)
-        handle->hdr.flags |= WRITABLE(1);
-    else
-        handle->hdr.flags &= ~WRITABLE(1);
-
-    return bytes;
-}
-
-static int mcast_receive (PAL_HANDLE handle, int offset, int size, void * buf)
-{
-    if (handle->mcast.cli == PAL_IDX_POISON)
-        return -PAL_ERROR_BADHANDLE;
-
-    struct msghdr hdr;
-    struct iovec iov;
-    iov.iov_base = buf;
-    iov.iov_len = size;
-    hdr.msg_name = NULL;
-    hdr.msg_namelen = 0;
-    hdr.msg_iov = &iov;
-    hdr.msg_iovlen = 1;
-    hdr.msg_control = NULL;
-    hdr.msg_controllen = 0;
-    hdr.msg_flags = 0;
-
-    int bytes = INLINE_SYSCALL(recvmsg, 3, handle->mcast.cli, &hdr, 0);
-
-    if (IS_ERR(bytes))
-        return -PAL_ERROR_DENIED;
-
-    return bytes;
-}
-
-static int mcast_close (PAL_HANDLE handle)
-{
-    if (handle->mcast.srv != PAL_IDX_POISON) {
-        INLINE_SYSCALL(close, 1, handle->mcast.srv);
-        handle->mcast.srv = PAL_IDX_POISON;
-    }
-    if (handle->mcast.cli != PAL_IDX_POISON) {
-        INLINE_SYSCALL(close, 1, handle->mcast.cli);
-        handle->mcast.cli = PAL_IDX_POISON;
-    }
-    return 0;
-}
-
-static int mcast_attrquerybyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
-{
-    int ret, val;
-
-    if (handle->mcast.cli == PAL_IDX_POISON)
-        return -PAL_ERROR_BADHANDLE;
-    ret = INLINE_SYSCALL(ioctl, 3, handle->mcast.cli, FIONREAD, &val);
-    if (IS_ERR(ret))
-        return unix_to_pal_error(ERRNO(ret));
-
-    attr->handle_type  = pal_type_mcast;
-    attr->disconnected = handle->hdr.flags & (ERROR(0)|ERROR(1));
-    attr->nonblocking  = handle->mcast.nonblocking;
-    attr->readable     = !!val;
-    attr->writable     = handle->hdr.flags & WRITABLE(1);
-    attr->runnable     = PAL_FALSE;
-    attr->pending_size = val;
-    return 0;
-}
-
-static int mcast_attrsetbyhdl (PAL_HANDLE handle, PAL_STREAM_ATTR * attr)
-{
-    if (handle->mcast.cli == PAL_IDX_POISON)
-        return -PAL_ERROR_BADHANDLE;
-
-    int ret;
-    PAL_BOL * nonblocking = &handle->mcast.nonblocking;
-
-    if (attr->nonblocking != *nonblocking) {
-        ret = INLINE_SYSCALL(fcntl, 3, handle->mcast.cli, F_SETFL,
-                             *nonblocking ? O_NONBLOCK : 0);
-
-        if (IS_ERR(ret))
-            return unix_to_pal_error(ERRNO(ret));
-
-        *nonblocking = attr->nonblocking;
-    }
-
-    return 0;
-}
-
-struct handle_ops mcast_ops = {
-        .write              = &mcast_send,
-        .read               = &mcast_receive,
-        .close              = &mcast_close,
-        .attrquerybyhdl     = &mcast_attrquerybyhdl,
-        .attrsetbyhdl       = &mcast_attrsetbyhdl,
-    };

+ 0 - 460
Pal/src/host/FreeBSD/db_streams.c

@@ -1,460 +0,0 @@
-/* Copyright (C) 2014 Stony Brook University
-   This file is part of Graphene Library OS.
-
-   Graphene Library OS is free software: you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public License
-   as published by the Free Software Foundation, either version 3 of the
-   License, or (at your option) any later version.
-
-   Graphene Library OS is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
-
-/*
- * db_stream.c
- *
- * This file contains APIs to open, read, write and get attribute of
- * streams.
- */
-
-#include "pal_defs.h"
-#include "pal_freebsd_defs.h"
-#include "pal.h"
-#include "pal_internal.h"
-#include "pal_freebsd.h"
-#include "pal_debug.h"
-#include "pal_error.h"
-#include "api.h"
-
-#include <sys/types.h>
-typedef __kernel_pid_t pid_t;
-#include <sys/msg.h>
-#include <sys/socket.h>
-#include <sys/wait.h>
-#include <sys/stat.h>
-#include <poll.h>
-#include <sys/signal.h>
-#include <netinet/in.h>
-#include <errno.h>
-
-void _DkPrintConsole (const void * buf, int size)
-{
-    INLINE_SYSCALL(write, 3, 2, buf, size);
-}
-
-bool stataccess (struct stat * stat, int acc)
-{
-    mode_t mode = stat->st_mode;
-
-    if (bsd_state.uid && bsd_state.uid == stat->st_uid) {
-        mode >>= 6;
-        goto out;
-    }
-
-    if (bsd_state.gid && bsd_state.gid == stat->st_gid) {
-        mode >>= 3;
-        goto out;
-    }
-
-    if (!bsd_state.uid)
-        mode >>= 6;
-
-out:
-    return (mode & acc);
-}
-
-int handle_set_cloexec (PAL_HANDLE handle, bool enable)
-{
-    for (int i = 0 ; i < MAX_FDS ; i++)
-        if (handle->hdr.flags & (RFD(i)|WFD(i))) {
-            long flags = enable ? FD_CLOEXEC : 0;
-            int ret = INLINE_SYSCALL(fcntl, 3,
-                                     handle->hdr.fds[i], F_SETFD,
-                                     flags);
-            if (IS_ERR(ret) && ERRNO(ret) != EBADF)
-                return -PAL_ERROR_DENIED;
-        }
-
-    return 0;
-}
-
-/* _DkStreamUnmap for internal use. Unmap stream at certain memory address.
-   The memory is unmapped as a whole.*/
-int _DkStreamUnmap (void * addr, uint64_t size)
-{
-    /* Just let the kernel tell us if the mapping isn't good. */
-    int ret = INLINE_SYSCALL(munmap, 2, addr, size);
-
-    if (IS_ERR(ret))
-        return -PAL_ERROR_DENIED;
-
-    return 0;
-}
-
-static size_t addr_size(const struct sockaddr* addr) {
-    switch (addr->sa_family) {
-        case AF_INET:
-            return sizeof(struct sockaddr_in);
-        case AF_INET6:
-            return sizeof(struct sockaddr_in6);
-        default:
-            return 0;
-    }
-}
-
-int handle_serialize (PAL_HANDLE handle, void ** data)
-{
-    int hdlsz = handle_size(handle);
-    const void * d1, * d2;
-    int dsz1 = 0, dsz2 = 0;
-
-    // ~ Check cargo PAL_HANDLE - is allowed to be sent (White List checking
-    // of cargo type)
-    // ~ Also, Initialize common parameter formessage passing
-    // Channel between parent and child
-    switch(PAL_GET_TYPE(handle)) {
-        case pal_type_file:
-            d1 = handle->file.realpath;
-            dsz1 = strlen(handle->file.realpath) + 1;
-            break;
-        case pal_type_pipe:
-        case pal_type_pipesrv:
-        case pal_type_pipecli:
-        case pal_type_pipeprv:
-            break;
-        case pal_type_dev:
-            if (handle->dev.realpath) {
-                d1 = handle->dev.realpath;
-                dsz1 = strlen(handle->dev.realpath) + 1;
-
-            }
-            break;
-        case pal_type_dir:
-            if (handle->dir.realpath) {
-                d1 = handle->dir.realpath;
-                dsz1 = strlen(handle->dir.realpath) + 1;
-            }
-            break;
-        case pal_type_tcp:
-        case pal_type_tcpsrv:
-        case pal_type_udp:
-        case pal_type_udpsrv:
-            if (handle->sock.bind) {
-                d1 = handle->sock.bind;
-                dsz1 = addr_size(handle->sock.bind);
-            }
-            if (handle->sock.conn) {
-                d2 = handle->sock.conn;
-                dsz2 = addr_size(handle->sock.conn);
-            }
-            break;
-        case pal_type_process:
-            break;
-        default:
-            return -PAL_ERROR_INVAL;
-    }
-
-    void * buffer = malloc(hdlsz + dsz1 + dsz2);
-    if (!buffer)
-        return -PAL_ERROR_NOMEM;
-
-    memcpy(buffer, handle, hdlsz);
-    if (dsz1)
-        memcpy(buffer + hdlsz, d1, dsz1);
-    if (dsz2)
-        memcpy(buffer + hdlsz + dsz1, d2, dsz2);
-
-    *data = buffer;
-    return hdlsz + dsz1 + dsz2;
-}
-
-#ifndef SEEK_SET
-# define SEEK_SET 0
-#endif
-
-int handle_deserialize (PAL_HANDLE * handle, const void * data, int size)
-{
-    PAL_HANDLE hdl_data = (void *) data, hdl = NULL;
-    int hdlsz = handle_size(hdl_data), ret = -PAL_ERROR_NOMEM;
-
-    data += hdlsz;
-    size -= hdlsz;
-
-    // recreate PAL_HANDLE based on type
-    switch(PAL_GET_TYPE(hdl_data)) {
-        case pal_type_file: {
-            int l = strlen((const char *) data) + 1;
-            hdl = malloc(hdlsz + l);
-            if (!hdl)
-                break;
-            memcpy(hdl, hdl_data, hdlsz);
-            memcpy((void *) hdl + hdlsz, data, l);
-            hdl->file.realpath = (void *) hdl + hdlsz;
-            break;
-        }
-        case pal_type_pipe:
-        case pal_type_pipesrv:
-        case pal_type_pipecli:
-        case pal_type_pipeprv:
-            hdl = malloc_copy(hdl_data, hdlsz);
-            break;
-        case pal_type_dev: {
-            int l = hdl_data->dev.realpath ? strlen((const char *) data) + 1 : 0;
-            hdl = malloc(hdlsz + l);
-            if (!hdl)
-                break;
-            memcpy(hdl, hdl_data, hdlsz);
-            if (l) {
-                memcpy((void *) hdl + hdlsz, data, l);
-                hdl->dev.realpath = (void *) hdl + hdlsz;
-            }
-            break;
-        }
-        case pal_type_dir: {
-            int l = hdl_data->dir.realpath ? strlen((const char *) data) + 1 : 0;
-            hdl = malloc(hdlsz + l);
-            if (!hdl)
-                break;
-            memcpy(hdl, hdl_data, hdlsz);
-            if (l) {
-                memcpy((void *) hdl + hdlsz, data, l);
-                hdl->dir.realpath = (void *) hdl + hdlsz;
-            }
-            break;
-        }
-        case pal_type_tcp:
-        case pal_type_tcpsrv:
-        case pal_type_udp:
-        case pal_type_udpsrv: {
-            int s1 = 0, s2 = 0;
-            if (hdl_data->sock.bind)
-                s1 = addr_size(data);
-            if (hdl_data->sock.conn)
-                s2 = addr_size(data + s1);
-            hdl = malloc(hdlsz + s1 + s2);
-            if (!hdl)
-                break;
-            memcpy(hdl, hdl_data, hdlsz);
-            if (s1) {
-                memcpy((void *) hdl + hdlsz, data, s1);
-                hdl->sock.bind = (void *) hdl + hdlsz;
-            }
-            if (s2) {
-                memcpy((void *) hdl + hdlsz + s1, data + s1, s2);
-                hdl->sock.conn = (void *) hdl + hdlsz + s2;
-            }
-            break;
-        }
-        case pal_type_process:
-            hdl = malloc_copy(hdl_data, hdlsz);
-            break;
-        default :
-            return -PAL_ERROR_BADHANDLE;
-    }
-
-    if (!hdl)
-        return ret;
-
-    *handle = hdl;
-    return 0;
-}
-
-// Header for DkSendHandle and DkRecvHandle
-struct hdl_header {
-    unsigned short fds:(MAX_FDS);
-    unsigned short data_size:(16-(MAX_FDS));
-};
-
-/* _DkSendHandle for internal use. Send a Pal Handle over the given
-   process handle. Return 1 if success else return negative error code */
-int _DkSendHandle (PAL_HANDLE hdl, PAL_HANDLE cargo)
-{
-    struct hdl_header hdl_hdr;
-    void * hdl_data;
-    int ret = handle_serialize(cargo, &hdl_data);
-    if (ret < 0)
-        return ret;
-
-    hdl_hdr.fds = 0;
-    hdl_hdr.data_size = ret;
-    int fds[MAX_FDS];
-    int nfds = 0;
-    for (int i = 0 ; i < MAX_FDS ; i++)
-        if (cargo->hdr.flags & (RFD(i)|WFD(i))) {
-            hdl_hdr.fds |= 1U << i;
-            fds[nfds++] = cargo->hdr.fds[i];
-        }
-    // ~ Initialize common parameter formessage passing
-    // Channel between parent and child
-    int ch = hdl->process.cargo;
-
-    // Declare variables required for sending the message
-    struct msghdr hdr; // message header
-    struct cmsghdr * chdr; //control message header
-    struct iovec iov[1]; // IO Vector
-    iov[0].iov_base = &hdl_hdr;
-    iov[0].iov_len = sizeof(struct hdl_header);
-    hdr.msg_name = NULL;
-    hdr.msg_namelen = 0;
-    hdr.msg_iov = iov;
-    hdr.msg_iovlen = 1;
-    hdr.msg_control = NULL;
-    hdr.msg_controllen = 0;
-    hdr.msg_flags = 0;
-
-    ret = INLINE_SYSCALL(sendmsg, 3, ch, &hdr, MSG_NOSIGNAL);
-    // Unlock is error
-    if (IS_ERR(ret)) {
-        free(hdl_data);
-        return -PAL_ERROR_DENIED;
-    }
-
-    /* Message Body Composition:
-       IOVEC[0]: PAL_HANDLE
-       IOVEC[1..n]: Additional handle member follow
-       Control Message: file descriptors */
-
-    // Control message buffer with added space for 2 fds (ie. max size
-    // that it will have)
-    char cbuf[CMSG_LEN(nfds * sizeof(int))];
-    memset(cbuf, 0, sizeof(cbuf));
-    // Initialize iovec[0] with struct PAL_HANDLE
-    iov[0].iov_base = hdl_data;
-    iov[0].iov_len = hdl_hdr.data_size;
-
-    hdr.msg_iov = iov;
-
-    hdr.msg_iovlen = 1;
-    hdr.msg_control = cbuf; // Control Message Buffer
-    hdr.msg_controllen = CMSG_LEN(sizeof(int) * nfds);
-    // Fill control message infomation for the file descriptors
-    // Check hdr.msg_controllen >= sizeof(struct cmsghdr) to point to
-    // cbuf, which is redundant based on the above code as we have
-    // statically allocated memory.
-    // or (struct cmsghdr*) cbuf
-    chdr = CMSG_FIRSTHDR(&hdr); // Pointer to msg_control
-    chdr->cmsg_level = SOL_SOCKET; // Originating Protocol
-    chdr->cmsg_type = SCM_RIGHTS; // Protocol Specific Type
-    // Length of control message = sizeof(struct cmsghdr) + nfds
-    chdr->cmsg_len = CMSG_LEN(sizeof(int) * nfds);
-
-    // Copy the fds below control header
-    memcpy(CMSG_DATA(chdr), fds, sizeof(int) * nfds);
-
-    // Also, Update main header with control message length (duplicate)
-    hdr.msg_controllen = chdr->cmsg_len;
-    //  Send message
-    ret = INLINE_SYSCALL(sendmsg, 3, ch, &hdr, 0);
-    free(hdl_data);
-    return IS_ERR(ret) ? -PAL_ERROR_DENIED : 0;
-}
-
-/* _DkRecvHandle for internal use. Receive and return a PAL_HANDLE over the
-   given PAL_HANDLE else return negative value. */
-int _DkReceiveHandle(PAL_HANDLE hdl, PAL_HANDLE * cargo)
-{
-    struct hdl_header hdl_hdr;
-
-    // ~ Check connection PAL_HANDLE - is of process type for sending handle
-    // else fail
-    if (!IS_HANDLE_TYPE(hdl, process))
-        return -PAL_ERROR_BADHANDLE;
-
-    // ~ Initialize common parameter for message passing
-    // Channel between parent and child
-    int ch = hdl->process.cargo;
-
-    struct msghdr hdr;
-    struct iovec iov[1];
-
-    iov[0].iov_base = &hdl_hdr;
-    iov[0].iov_len = sizeof(struct hdl_header);
-    hdr.msg_name = NULL;
-    hdr.msg_namelen = 0;
-    hdr.msg_iov = iov;
-    hdr.msg_iovlen = 1;
-    hdr.msg_control = NULL;
-    hdr.msg_controllen = 0;
-    hdr.msg_flags = 0;
-
-    int ret = INLINE_SYSCALL(recvmsg, 3, ch, &hdr, 0);
-    if (IS_ERR(ret))
-        return unix_to_pal_error(ERRNO(ret));
-    if (ret < sizeof(struct hdl_header)) {
-        /*
-         * This code block is just in case to cover all the possibilities.
-         * We know that the file descriptor is an unix domain socket with
-         * blocking mode and that the sender, _DkSendHandle() above, sends the
-         * header with single sendmsg syscall which transfers message atomically.
-         *
-         * read size == 0: return error for the caller to try again.
-         *                 It should result in EINTR.
-         *
-         * read size > 0: return error for the caller to give up this file
-         *                descriptor.
-         *                If the header can't be send atomically for some
-         *                reason, the sender should get EMSGSIZE.
-         */
-        if (!ret)
-            return -PAL_ERROR_TRYAGAIN;
-        return -PAL_ERROR_DENIED;
-    }
-
-    // initialize variables to get body
-    void * buffer = __alloca(hdl_hdr.data_size);
-    int nfds = 0;
-
-    for (int i = 0 ; i < MAX_FDS ; i++)
-        if (hdl_hdr.fds & (1U << i))
-            nfds++;
-    // receive PAL_HANDLE contents in the body
-    char cbuf[CMSG_LEN(nfds * sizeof(int))];
-    memset(cbuf, 0, sizeof(cbuf));
-    // initialize iovec[0] with struct PAL_HANDLE
-    iov[0].iov_base = buffer;
-    iov[0].iov_len = hdl_hdr.data_size;
-
-    // set message header values
-    hdr.msg_iov = iov;
-    hdr.msg_iovlen = 1;
-    hdr.msg_control = cbuf;
-    hdr.msg_controllen = sizeof(cbuf);
-    ret = INLINE_SYSCALL(recvmsg, 3, ch, &hdr, 0);
-    // if error was returned
-    if (IS_ERR(ret) && ERRNO(ret) != EINTR && ERRNO(ret) != ERESTART)
-        return -ERRNO(ret);
-
-    struct cmsghdr * chdr = CMSG_FIRSTHDR(&hdr);
-    if (!chdr || chdr->cmsg_type != SCM_RIGHTS)
-        return -PAL_ERROR_INVAL;
-
-    PAL_HANDLE handle = NULL;
-    ret = handle_deserialize(&handle, buffer, hdl_hdr.data_size);
-    if (ret < 0)
-        return ret;
-
-    int total_fds = (hdr.msg_controllen - sizeof(struct cmsghdr)) /
-                    sizeof(int);
-    int n = 0;
-    for (int i = 0 ; i < MAX_FDS ; i++)
-        if (hdl_hdr.fds & (1U << i)) {
-            if (n < total_fds) {
-                handle->hdr.fds[i] = ((int *) CMSG_DATA(chdr))[n++];
-            } else {
-                handle->hdr.flags &= ~(RFD(i)|WFD(i));
-            }
-        }
-
-    if (IS_HANDLE_TYPE(handle, file)) {
-        ret = INLINE_SYSCALL(lseek, 3, handle->file.fd, 0, SEEK_SET);
-        if (!IS_ERR(ret))
-            handle->file.offset = ret;
-    }
-
-    *cargo = handle;
-    return 0;
-}

+ 0 - 137
Pal/src/host/FreeBSD/db_threading.c

@@ -1,137 +0,0 @@
-/* Copyright (C) 2014 Stony Brook University
-   This file is part of Graphene Library OS.
-
-   Graphene Library OS is free software: you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public License
-   as published by the Free Software Foundation, either version 3 of the
-   License, or (at your option) any later version.
-
-   Graphene Library OS is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
-
-/*
- * db_threading.c
- *
- * This file contain APIs to create, exit and yield a thread.
- */
-
-#include "pal_defs.h"
-#include "pal_freebsd_defs.h"
-#include "pal.h"
-#include "pal_internal.h"
-#include "pal_freebsd.h"
-#include "pal_error.h"
-#include "pal_debug.h"
-#include "api.h"
-
-#include <errno.h>
-#include <signal.h>
-#include <sys/mman.h>
-#include <sched.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <sys/timespec.h>
-#include <unistd.h>
-#define THREAD_STACK_SIZE   (pal_state.alloc_align)
-
-/* _DkThreadCreate for internal use. Create an internal thread
-   inside the current process. The arguments callback and param
-   specify the starting function and parameters */
-int _DkThreadCreate (PAL_HANDLE * handle, int (*callback) (void *),
-                     const void * param)
-{
-   void * child_stack = NULL;
-
-    if (_DkVirtualMemoryAlloc(&child_stack, THREAD_STACK_SIZE, 0,
-                              PAL_PROT_READ|PAL_PROT_WRITE) < 0)
-        return -PAL_ERROR_NOMEM;
-
-    // move child_stack to the top of stack.
-    child_stack += THREAD_STACK_SIZE;
-
-    // align child_stack to 16
-    child_stack = ALIGN_DOWN_PTR(child_stack, 16);
-
-    flags &= PAL_THREAD_MASK;
-
-    assert(!flags); //FreeBSD does not support any more flags for rfork!
-
-    int ret = rfork_thread(
-                    RFPROC|RFNOWAIT|RFSIGSHARE|RFMEM,
-                    child_stack,
-                    callback,
-                    (void *)param);
-
-    if (IS_ERR(ret))
-        return -PAL_ERROR_DENIED;
-
-    PAL_HANDLE hdl = malloc(HANDLE_SIZE(thread));
-    SET_HANDLE_TYPE(hdl, thread);
-    hdl->thread.tid = ret;
-    *handle = hdl;
-    return 0;
-}
-
-int _DkThreadDelayExecution (unsigned long * duration)
-{
-    struct timespec sleeptime;
-    struct timespec remainingtime;
-
-    const long VERY_LONG_TIME_IN_US = 1000000L * 60 * 60 * 24 * 365 * 128;
-    if (*duration > VERY_LONG_TIME_IN_US) {
-        /* avoid overflow with time_t */
-        sleeptime.tv_sec  = VERY_LONG_TIME_IN_US / 1000000;
-        sleeptime.tv_nsec = 0;
-    } else {
-        sleeptime.tv_sec = *duration / 1000000;
-        sleeptime.tv_nsec = (*duration - sleeptime.tv_sec * 1000000) * 1000;
-    }
-
-    int ret = INLINE_SYSCALL(nanosleep, 2, &sleeptime, &remainingtime);
-
-    if (IS_ERR(ret)) {
-        PAL_NUM remaining = remainingtime.tv_sec * 1000000 +
-                            remainingtime.tv_nsec / 1000;
-
-        *duration -= remaining;
-        return -PAL_ERROR_INTERRUPTED;
-    }
-
-    return 0;
-}
-
-/* PAL call DkThreadYieldExecution. Yield the execution
-   of the current thread. */
-void _DkThreadYieldExecution (void)
-{
-    INLINE_SYSCALL(sched_yield, 0);
-}
-
-/* _DkThreadExit for internal use: Thread exiting */
-noreturn void _DkThreadExit(int* clear_child_tid) {
-    if (clear_child_tid) {
-        /* thread is ready to exit, must inform LibOS by setting *clear_child_tid to 0;
-         * async helper thread in LibOS is waiting on this to wake up parent */
-        __atomic_store_n(clear_child_tid, 0, __ATOMIC_RELAXED);
-    }
-    INLINE_SYSCALL(exit, 1, 0);
-}
-
-int _DkThreadResume (PAL_HANDLE threadHandle)
-{
-    int ret = INLINE_SYSCALL(kill, 2, threadHandle->thread.tid, SIGCONT);
-
-    if (IS_ERR(ret))
-        return -PAL_ERROR_DENIED;
-
-    return 0;
-}
-
-struct handle_ops thread_ops = {
-    /* nothing */
-};

+ 0 - 40
Pal/src/host/FreeBSD/elf-x86_64.h

@@ -1,40 +0,0 @@
-/* Copyright (C) 2014 Stony Brook University
-   This file is part of Graphene Library OS.
-
-   Graphene Library OS is free software: you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public License
-   as published by the Free Software Foundation, either version 3 of the
-   License, or (at your option) any later version.
-
-   Graphene Library OS is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
-
-/*
- * dl-machine-x86_64.h
- *
- * This files contain architecture-specific implementation of ELF dynamic
- * relocation function.
- * The source code is imported and modified from the GNU C Library.
- */
-
-#define ELF_MACHINE_NAME "x86_64"
-
-#include <sys/param.h>
-#include <sysdep.h>
-#include <sysdeps/generic/ldsodefs.h>
-
-#include "pal_internal.h"
-
-/* Return the link-time address of _DYNAMIC.  Conveniently, this is the
-   first element of the GOT.  This must be inlined in a function which
-   uses global data.  */
-static inline Elf64_Addr __attribute__((unused)) elf_machine_dynamic(void) {
-    Elf64_Addr addr;
-    addr = (Elf64_Addr)&_DYNAMIC;
-    return addr;
-}

+ 0 - 8
Pal/src/host/FreeBSD/host_endian.h

@@ -1,8 +0,0 @@
-#include <sys/endian.h>
-#undef __htonl
-#undef __ntohl
-#undef __htons
-#undef __ntohs
-#define __bswap_16 __bswap16
-#define __bswap_32 __bswap32
-#define __bswap_64 __bswap64

+ 0 - 90
Pal/src/host/FreeBSD/include/sigset.h

@@ -1,90 +0,0 @@
-#ifndef __SIGSET_H__
-#define __SIGSET_H__
-
-/* __sig_atomic_t, _sigset_t, and related definitions.  Linux version.
-   Copyright (C) 1991, 1992, 1994, 1996, 1997, 2007
-   Free Software Foundation, Inc.
-   This file is part of the GNU C Library.
-
-   The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 2.1 of the License, or (at your option) any later version.
-
-   The GNU C Library is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-   02111-1307 USA.  */
-
-#define _SIGSET_H_types 1
-#define _SIGSET_H_fns   1
-
-#define ____sigset_t_defined
-
-typedef int __sig_atomic_t;
-
-/* A `sigset_t' has a bit for each signal.  */
-
-#define _SIGSET_NWORDS (64 / (8 * sizeof(unsigned long int)))
-
-typedef struct {
-    unsigned long int __val[_SIGSET_NWORDS];
-} _sigset_t;
-
-/* Return a mask that includes the bit for SIG only.  */
-#define __sigmask(sig) (((unsigned long int)1) << (((sig) - 1) % (8 * sizeof(unsigned long int))))
-
-/* Return the word index for SIG.  */
-#define __sigword(sig) (((sig) - 1) / (8 * sizeof(unsigned long int)))
-
-#define __sigemptyset(set)                 \
-    ({                                     \
-        int __cnt        = _SIGSET_NWORDS; \
-        _sigset_t* __set = (set);          \
-        while (--__cnt >= 0) {             \
-            __set->__val[__cnt] = 0;       \
-        }                                  \
-        0;                                 \
-    })
-#define __sigfillset(set)                  \
-    ({                                     \
-        int __cnt        = _SIGSET_NWORDS; \
-        _sigset_t* __set = (set);          \
-        while (--__cnt >= 0) {             \
-            __set->__val[__cnt] = ~0UL;    \
-        }                                  \
-        0;                                 \
-    })
-#define __sigcopyset(set, src)                         \
-    ({                                                 \
-        int __cnt        = _SIGSET_NWORDS;             \
-        _sigset_t* __set = (set);                      \
-        _sigset_t* __src = (src);                      \
-        while (--__cnt >= 0) {                         \
-            __set->__val[__cnt] = __src->__val[__cnt]; \
-        }                                              \
-        0;                                             \
-    })
-
-/* These functions needn't check for a bogus signal number -- error
-   checking is done in the non __ versions.  */
-
-#define __SIGSETFN(NAME, BODY, CONST)                           \
-    static inline int NAME(CONST _sigset_t* __set, int __sig) { \
-        unsigned long int __mask = __sigmask(__sig);            \
-        unsigned long int __word = __sigword(__sig);            \
-        return BODY;                                            \
-    }
-
-__SIGSETFN(__sigismember, (__set->__val[__word] & __mask) ? 1 : 0, __const)
-__SIGSETFN(__sigaddset, ((__set->__val[__word] |= __mask), 0), )
-__SIGSETFN(__sigdelset, ((__set->__val[__word] &= ~__mask), 0), )
-
-#undef __SIGSETFN
-
-#endif /* __SIGSET_H__  */

+ 0 - 111
Pal/src/host/FreeBSD/include/sysdep.h

@@ -1,111 +0,0 @@
-/* Assembler macros for x86-64.
-   Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
-   This file is part of the GNU C Library.
-
-   The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 2.1 of the License, or (at your option) any later version.
-
-   The GNU C Library is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-   02111-1307 USA.  */
-
-#ifdef __ASSEMBLER__
-
-/* Syntactic details of assembler.  */
-
-#ifdef HAVE_ELF
-
-/* ELF uses byte-counts for .align, most others use log2 of count of bytes.  */
-#define ALIGNARG(log2) (1 << (log2))
-/* For ELF we need the `.type' directive to make shared libs work right.  */
-#define ASM_TYPE_DIRECTIVE(name, typearg) .type name,typearg;
-#define ASM_SIZE_DIRECTIVE(name)          .size name,.-name;
-
-/* In ELF C symbols are asm symbols.  */
-#undef NO_UNDERSCORES
-#define NO_UNDERSCORES
-
-#else
-
-#define ALIGNARG(log2)                 (log2)
-#define ASM_TYPE_DIRECTIVE(name, type) /* Nothing is specified.  */
-#define ASM_SIZE_DIRECTIVE(name)       /* Nothing is specified.  */
-
-#endif
-
-/* Define an entry point visible from C.  */
-#define ENTRY(name)                                     \
-    ASM_GLOBAL_DIRECTIVE C_SYMBOL_NAME(name);           \
-    ASM_TYPE_DIRECTIVE(C_SYMBOL_NAME(name), @function); \
-    .align ALIGNARG(4);                                 \
-    C_LABEL(name);                                      \
-    cfi_startproc;                                      \
-    CALL_MCOUNT
-
-#undef END
-#define END(name) \
-    cfi_endproc;  \
-    ASM_SIZE_DIRECTIVE(name)
-
-/* If compiled for profiling, call `mcount' at the start of each function.  */
-#ifdef PROF
-/* The mcount code relies on a normal frame pointer being on the stack
-   to locate our caller, so push one just for its benefit.  */
-#define CALL_MCOUNT              \
-    pushq %rbp;                  \
-    cfi_adjust_cfa_offset(8);    \
-    movq %rsp, %rbp;             \
-    cfi_def_cfa_register(%rbp);  \
-    call JUMPTARGET(mcount);     \
-    popq %rbp;                   \
-    cfi_def_cfa(rsp, 8);
-#else
-#define CALL_MCOUNT /* Do nothing.  */
-#endif
-
-#ifdef NO_UNDERSCORES
-/* Since C identifiers are not normally prefixed with an underscore
-   on this system, the asm identifier `syscall_error' intrudes on the
-   C name space.  Make sure we use an innocuous name.  */
-#define syscall_error __syscall_error
-#define mcount        _mcount
-#endif
-
-// commented by GP
-#define PSEUDO(name, syscall_name, args) \
-    lose:                                \
-    jmp JUMPTARGET(syscall_error);       \
-    .globl syscall_error;                \
-    ENTRY(name);                         \
-    DO_CALL(syscall_name, args);         \
-    jb lose
-
-#undef PSEUDO_END
-#define PSEUDO_END(name) END(name)
-
-#undef JUMPTARGET
-#ifdef PIC
-#define JUMPTARGET(name) name##@PLT
-#else
-#define JUMPTARGET(name) name
-#endif
-
-/* Local label name for asm code. */
-#ifndef L
-#ifdef HAVE_ELF
-/* ELF-like local names start with `.L'.  */
-#define L(name) .L##name
-#else
-#define L(name) name
-#endif
-#endif
-
-#endif /* __ASSEMBLER__ */

+ 0 - 21
Pal/src/host/FreeBSD/pal-gdb.template

@@ -1,21 +0,0 @@
-#!/bin/bash
-
-BASE=0x1021000
-PAL=$(PAL_DIR)/pal
-PALGDBINIT=$(PAL_DIR)/pal.gdbinit
-
-if [ ! -f $PALGDBINIT ] || [ $PAL -nt $PALGDBINIT ]; then
-	echo "symbol-file" > $PALGDBINIT
-	readelf -WS $PAL | (
-	SECTIONS=""
-	TEXT=""
-	while read -r line
-	do
-		echo ${line:5} | awk '{if ($3 ~ /^0/) { v = sprintf("%d", "0x" $3); if (v != 0) printf("section %s 0x%x\n", $1, sprintf("%d", "'$BASE'") + v) }}' >> $PALGDBINIT
-		TEXT+=$(echo ${line:5} | awk '{if ($1 == ".text") { v = sprintf("%d", "0x" $3); printf("0x%x", sprintf("%d", "'$BASE'") + v) }}')
-		SECTIONS+=$(echo ${line:5} | awk '{if ($1 != ".text" && $3 ~ /^0/) { v = sprintf("%d", "0x" $3); if (v != 0) printf(" -s %s 0x%x", $1, sprintf("%d", "'$BASE'") + v) }}')
-	done
-	echo add-symbol-file $PAL $TEXT $SECTIONS >> $PALGDBINIT)
-fi
-
-exec gdb -q -ex "source $PALGDBINIT" -ex "source $(PAL_DIR)/pal.gdb" --args $PAL "$@"

+ 0 - 11
Pal/src/host/FreeBSD/pal.gdb

@@ -1,11 +0,0 @@
-echo Graphene GDB Script loaded\n
-
-set env IN_GDB = 1
-
-handle SIGCONT pass noprint nostop
-
-#set disable-randomization off
-set detach-on-fork off
-set schedule-multiple on
-set follow-exec-mode same
-set follow-fork-mode child

+ 0 - 83
Pal/src/host/FreeBSD/pal.lds

@@ -1,83 +0,0 @@
-OUTPUT_FORMAT("elf64-x86-64-freebsd", "elf64-x86-64-freebsd", "elf64-x86-64-freebsd")
-OUTPUT_ARCH(i386:x86-64)
-ENTRY(pal_start);
-
-SECTIONS
-{
-  /* Read-only sections, merged into text segment; */
-  __text_start = .;
-  . = SEGMENT_START("text-segment", 0) + SIZEOF_HEADERS;
-  .note.gnu.build-id : { *(.note.gnu.build-id) }
-  .hash          : { *(.hash) }
-  .gnu.hash      : { *(.gnu.hash) }
-  .dynsym        : { *(.dynsym) }
-  .dynstr        : { *(.dynstr) }
-  .gnu.version   : { *(.gnu.version) }
-  .gnu.version_d : { *(.gnu.version_d) }
-  .gnu.version_r : { *(.gnu.version_r) }
-  .rela.dyn       :
-     {
-      *(.rela.init)
-      *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)
-      *(.rela.fini)
-      *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)
-      *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)
-      *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)
-      *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)
-      *(.rela.ctors)
-      *(.rela.dtors)
-      *(.rela.got)
-      *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)
-      *(.rela.ldata .rela.ldata.* .rela.gnu.linkonce.l.*)
-      *(.rela.lbss .rela.lbss.* .rela.gnu.linkonce.lb.*)
-      *(.rela.lrodata .rela.lrodata.* .rela.gnu.linkonce.lr.*)
-      *(.rela.ifunc)
-    }
-  .rela.plt      :
-    {
-      *(.rela.plt)
-      *(.rela.iplt)
-    }
-  .init          : { *(.init) }
-  .plt           : { *(.plt) *(.iplt) }
-  .text :
-  {
-    /* the rest of text segments */
-    *(.text .stub .text.*);
-  }
-  .fini          : { *(.fini) }
-  .rodata        :
-  {
-    /* the rest of rodata */
-    *(.rodata .rodata.*)
-  }
-  .rodata1       : { *(.rodata1) }
-  .eh_frame_hdr  : { *(.eh_frame_hdr) }
-  .eh_frame      : ONLY_IF_RO { *(.eh_frame) }
-  __text_end = .;
-
-  /* now adjust the address for the data segment */
-  . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE));
-  __data_start = .;
-  .eh_frame      : ONLY_IF_RW { *(.eh_frame) }
-  .data.rel.ro   : { *(.data.rel.ro) }
-  .ctors         : { *(.ctors) }
-  .dtors         : { *(.dtors) }
-  .jcr           : { *(.jcr) }
-  .dynamic       : { *(.dynamic) }
-  .got           : { *(.got) *(.igot) }
-  .got.plt       : { *(.got.plt) *(.igot.plt) }
-  .data :
-  {
-    /* the rest of data segment */
-    *(.data .data.*);
-  }
-  .bss            :
-  {
-    *(.bss .bss.*)
-    *(COMMON)
-  }
-  . = DATA_SEGMENT_END(.);
-  __data_end = .;
-}
-

+ 0 - 8
Pal/src/host/FreeBSD/pal.map.template

@@ -1,8 +0,0 @@
-PAL {
-    global: $(PAL_SYMBOLS) r_debug;
-    local: *;
-};
-PAL_PRIVATE {
-    global:
-        r_debug_state;
-};

+ 0 - 170
Pal/src/host/FreeBSD/pal_freebsd.h

@@ -1,170 +0,0 @@
-/* Copyright (C) 2014 Stony Brook University
-   This file is part of Graphene Library OS.
-
-   Graphene Library OS is free software: you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public License
-   as published by the Free Software Foundation, either version 3 of the
-   License, or (at your option) any later version.
-
-   Graphene Library OS is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
-
-#ifndef PAL_FREEBSD_H
-#define PAL_FREEBSD_H
-
-#include "pal.h"
-#include "pal_defs.h"
-#include "pal_freebsd_defs.h"
-#include "pal_freebsd_error.h"
-
-typedef int __kernel_pid_t;
-
-#include <sigset.h>
-#include <sys/syscall.h>
-#include <unistd.h>
-
-#undef __htonl
-#undef __ntohl
-#undef __htons
-#undef __ntohs
-
-#ifdef __x86_64__
-#include "sysdep-x86_64.h"
-#endif
-
-#define PAL_LOADER XSTRINGIFY(PAL_LOADER_PATH)
-
-#define IS_ERR   INTERNAL_SYSCALL_ERROR
-#define IS_ERR_P INTERNAL_SYSCALL_ERROR_P
-#define ERRNO    INTERNAL_SYSCALL_ERRNO
-#define ERRNO_P  INTERNAL_SYSCALL_ERRNO_P
-
-struct timespec;
-struct timeval;
-extern struct pal_bsd_state {
-    /* state */
-    unsigned long start_time;
-
-    /* credentails */
-    unsigned int pid;
-    unsigned int uid, gid;
-    unsigned int parent_pid;
-    /* currently enabled signals */
-    _sigset_t sigset;
-
-    unsigned long memory_quota;
-} bsd_state;
-
-#include <sys/mman.h>
-#ifdef INLINE_SYSCALL
-#ifdef __i386__
-#define ARCH_MMAP(addr, len, prot, flags, fd, offset) \
-    ({                                                \
-        struct mmap_arg_struct {                      \
-            unsigned long addr;                       \
-            unsigned long len;                        \
-            unsigned long prot;                       \
-            unsigned long flags;                      \
-            unsigned long fd;                         \
-            unsigned long offset;                     \
-        } args = {                                    \
-            .addr   = (unsigned long)(addr),          \
-            .len    = (unsigned long)(len),           \
-            .prot   = (unsigned long)(prot),          \
-            .flags  = (unsigned long)(flags),         \
-            .fd     = (unsigned long)(fd),            \
-            .offset = (unsigned long)(offset),        \
-        };                                            \
-        INLINE_SYSCALL(mmap, 1, &args);               \
-    })
-#else
-#define ARCH_MMAP(addr, len, prot, flags, fd, offset) \
-    INLINE_SYSCALL(mmap, 6, (addr), (len), (prot), (flags), (fd), (offset))
-#endif
-#else
-#error "INLINE_SYSCALL not supported"
-#endif
-
-#define PRESET_PAGESIZE (1 << 12)
-
-#define DEFAULT_BACKLOG 2048
-
-static inline int HOST_FLAGS(int alloc_type, int prot) {
-    return (prot & PAL_PROT_WRITECOPY) ? MAP_PRIVATE : MAP_SHARED;
-}
-
-static inline int HOST_PROT(int prot) {
-    return prot & (PAL_PROT_READ | PAL_PROT_WRITE | PAL_PROT_EXEC);
-}
-
-int __clone(int (*__fn)(void* __arg), void* __child_stack, int __flags, const void* __arg, ...);
-
-/* set/unset CLOEXEC flags of all fds in a handle */
-int handle_set_cloexec(PAL_HANDLE handle, bool enable);
-
-/* serialize/deserialize a handle into/from a malloc'ed buffer */
-int handle_serialize(PAL_HANDLE handle, void** data);
-int handle_deserialize(PAL_HANDLE* handle, const void* data, int size);
-#define ACCESS_R 4
-#define ACCESS_W 2
-#define ACCESS_X 1
-
-struct stat;
-bool stataccess(struct stat* stats, int acc);
-
-#include <sys/fcntl.h>
-
-static inline int HOST_FILE_OPEN(int access_type, int create_type, int options) {
-    return ((access_type) | (create_type & PAL_CREATE_TRY ? O_CREAT : 0) |
-            (create_type & PAL_CREATE_ALWAYS ? O_EXCL : 0) | (options));
-}
-
-#include <sys/stat.h>
-
-static inline int HOST_PERM(int share_type) {
-    return (share_type & PAL_SHARE_GLOBAL_X ? S_IXUSR | S_IXGRP | S_IXOTH : 0) |
-           (share_type & PAL_SHARE_GLOBAL_W ? S_IWUSR | S_IWGRP | S_IWOTH : 0) |
-           (share_type & PAL_SHARE_GLOBAL_R ? S_IRUSR | S_IRGRP | S_IROTH : 0) |
-           (share_type & PAL_SHARE_GROUP_X ? S_IXGRP : 0) |
-           (share_type & PAL_SHARE_GROUP_W ? S_IWGRP : 0) |
-           (share_type & PAL_SHARE_GROUP_R ? S_IRGRP : 0) |
-           (share_type & PAL_SHARE_OWNER_X ? S_IXUSR : 0) |
-           (share_type & PAL_SHARE_OWNER_W ? S_IWUSR : 0) |
-           (share_type & PAL_SHARE_OWNER_R ? S_IRUSR : 0);
-}
-
-static inline int HOST_OPTIONS(int options) {
-    return (options & PAL_OPTION_NONBLOCK ? O_NONBLOCK : 0);
-}
-
-#include <sys/socket.h>
-
-static inline int HOST_SOCKET_OPTIONS(int options) {
-    return (options & PAL_OPTION_NONBLOCK ? SOCK_NONBLOCK : 0);
-}
-
-/* Locking and unlocking of Mutexes */
-int _DkMutexLock(struct mutex_handle* mut);
-int _DkMutexLockTimeout(struct mutex_handle* mut, int timeout);
-int _DkMutexUnlock(struct mutex_handle* mut);
-
-/*UMTX constants*/
-#define UMTX_OP_WAIT      2
-#define UMTX_OP_WAKE      3
-#define UMTX_OP_WAIT_UINT 11
-
-void init_child_process(PAL_HANDLE* parent, PAL_HANDLE* exec, PAL_HANDLE* manifest);
-void signal_setup(void);
-
-extern char __text_start, __text_end, __data_start, __data_end;
-#define TEXT_START ((void*)(&__text_start))
-#define TEXT_END   ((void*)(&__text_end))
-#define DATA_START ((void*)(&__text_start))
-#define DATA_END   ((void*)(&__text_end))
-
-#endif /* PAL_FREEBSD_H */

+ 0 - 11
Pal/src/host/FreeBSD/pal_freebsd_defs.h

@@ -1,11 +0,0 @@
-#ifndef PAL_FREEBSD_DEFS_H
-#define PAL_FREEBSD_DEFS_H
-
-#define USER_ADDRESS_LOWEST  0x10000
-#define USER_ADDRESS_HIGHEST 0x80000000
-
-#define USE_CLOCK_GETTIME 0
-
-#define USE_ARCH_RDRAND 0
-
-#endif /* PAL_FREEBSD_DEFS_H */

+ 0 - 43
Pal/src/host/FreeBSD/pal_freebsd_error.h

@@ -1,43 +0,0 @@
-#ifndef PAL_FREEBSD_ERROR_H
-#define PAL_FREEBSD_ERROR_H
-
-#ifdef IN_PAL
-
-#include <errno.h>
-#include <pal_error.h>
-
-static inline __attribute__((unused)) int unix_to_pal_error(int unix_errno) {
-    switch (unix_errno) {
-        case ENOENT:
-            return -PAL_ERROR_STREAMNOTEXIST;
-        case EINTR:
-            return -PAL_ERROR_INTERRUPTED;
-        case EBADF:
-            return -PAL_ERROR_BADHANDLE;
-        case ETIMEDOUT:
-        case EAGAIN:
-            return -PAL_ERROR_TRYAGAIN;
-        case ENOMEM:
-            return -PAL_ERROR_NOMEM;
-        case EFAULT:
-            return -PAL_ERROR_BADADDR;
-        case EEXIST:
-            return -PAL_ERROR_STREAMEXIST;
-        case ENOTDIR:
-            return -PAL_ERROR_STREAMISFILE;
-        case EINVAL:
-            return -PAL_ERROR_INVAL;
-        case ENAMETOOLONG:
-            return -PAL_ERROR_TOOLONG;
-        case EISDIR:
-            return -PAL_ERROR_STREAMISDIR;
-        default:
-            return -PAL_ERROR_DENIED;
-    }
-}
-
-#define ERESTART (-1) /* restart syscall */
-
-#endif /* IN_PAL */
-
-#endif /* PAL_FREEBSD_ERROR_H */

+ 0 - 283
Pal/src/host/FreeBSD/pal_host.h

@@ -1,283 +0,0 @@
-/* Copyright (C) 2014 Stony Brook University
-   This file is part of Graphene Library OS.
-
-   Graphene Library OS is free software: you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public License
-   as published by the Free Software Foundation, either version 3 of the
-   License, or (at your option) any later version.
-
-   Graphene Library OS is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
-
-/*
- * pal_host.h
- *
- * This file contains definition of PAL host ABI.
- */
-
-#ifndef PAL_HOST_H
-#define PAL_HOST_H
-
-#ifndef IN_PAL
-# error "cannot be included outside PAL"
-#endif
-
-/* internal Mutex design, the structure has to align at integer boundary
-   because it is required by futex call. If DEBUG_MUTEX is defined,
-   mutex_handle will record the owner of mutex locking. */
-typedef struct mutex_handle {
-    struct atomic_int value;
-#ifdef DEBUG_MUTEX
-    int owner;
-#endif
-} PAL_LOCK;
-
-/* Initializer of Mutexes */
-#define MUTEX_HANDLE_INIT    { .value = { .counter = 1 } }
-#define INIT_MUTEX_HANDLE(mut)  \
-    do { atomic_set(&(mut)->value, 1); } while (0)
-
-#define LOCK_INIT MUTEX_HANDLE_INIT
-#define INIT_LOCK(lock) INIT_MUTEX_HANDLE(lock);
-
-#define _DkInternalLock _DkMutexLock
-#define _DkInternalUnlock _DkMutexUnlock
-
-typedef union pal_handle
-{
-    /* TSAI: Here we define the internal types of PAL_HANDLE
-     * in PAL design, user has not to access the content inside the
-     * handle, also there is no need to allocate the internal
-     * handles, so we hide the type name of these handles on purpose.
-     */
-    struct {
-        PAL_HDR hdr;
-        struct mutex_handle mut;
-    } mutex;
-
-    struct {
-        PAL_IDX type;
-        PAL_FLG flags;
-        PAL_REF ref;
-        PAL_IDX fds[];
-    } hdr;
-
-    struct {
-        PAL_HDR hdr;
-        PAL_IDX fd;
-        PAL_NUM offset;
-        PAL_BOL append;
-        PAL_BOL pass;
-        PAL_STR realpath;
-    } file;
-
-    struct {
-        PAL_HDR hdr;
-        PAL_IDX fd;
-        PAL_NUM pipeid;
-        PAL_BOL nonblocking;
-    } pipe;
-
-    struct {
-        PAL_HDR hdr;
-        PAL_IDX fds[MAX_FDS];
-        PAL_BOL nonblocking;
-    } pipeprv;
-
-    struct {
-        PAL_HDR hdr;
-        PAL_IDX fd_in, fd_out;
-        PAL_IDX dev_type;
-        PAL_BOL destroy;
-        PAL_STR realpath;
-    } dev;
-
-    struct {
-        PAL_HDR hdr;
-        PAL_IDX fd;
-        PAL_STR realpath;
-        PAL_PTR buf;
-        PAL_PTR ptr;
-        PAL_PTR end;
-        PAL_BOL endofstream;
-    } dir;
-
-    struct {
-        PAL_HDR hdr;
-        PAL_IDX fd;
-        PAL_PTR bind;
-        PAL_PTR conn;
-        PAL_BOL nonblocking;
-        PAL_BOL reuseaddr;
-        PAL_NUM linger;
-        PAL_NUM receivebuf;
-        PAL_NUM sendbuf;
-        PAL_NUM receivetimeout;
-        PAL_NUM sendtimeout;
-        PAL_BOL tcp_cork;
-        PAL_BOL tcp_keepalive;
-        PAL_BOL tcp_nodelay;
-    } sock;
-
-    struct {
-        PAL_HDR hdr;
-        PAL_IDX stream;
-        PAL_IDX cargo;
-        PAL_IDX pid;
-        PAL_BOL nonblocking;
-    } process;
-
-    struct {
-        PAL_HDR hdr;
-        PAL_IDX cli;
-        PAL_IDX srv;
-        PAL_IDX port;
-        PAL_BOL nonblocking;
-    } mcast;
-
-    struct {
-        PAL_HDR hdr;
-        PAL_IDX tid;
-    } thread;
-
-    struct {
-        PAL_HDR hdr;
-        struct atomic_int nwaiters;
-        PAL_NUM max_value;
-        union {
-            struct mutex_handle mut;
-            struct atomic_int i;
-        } value;
-    } semaphore;
-
-    struct {
-        PAL_HDR hdr;
-        struct atomic_int signaled;
-        struct atomic_int nwaiters;
-        PAL_BOL isnotification;
-    } event;
-} * PAL_HANDLE;
-
-#define RFD(n)          (1 << (MAX_FDS*0 + (n)))
-#define WFD(n)          (1 << (MAX_FDS*1 + (n)))
-#define WRITABLE(n)     (1 << (MAX_FDS*2 + (n)))
-#define ERROR(n)        (1 << (MAX_FDS*3 + (n)))
-#define HAS_FDS         ((1 << MAX_FDS*2) - 1)
-
-#define HANDLE_TYPE(handle)  ((handle)->hdr.type)
-struct arch_frame {
-#ifdef __x86_64__
-    uint64_t rsp, rbp, rbx, rsi, rdi, r12, r13, r14, r15;
-#else
-# error "unsupported architecture"
-#endif
-};
-
-#ifdef __x86_64__
-# define store_register(reg, var)     \
-    asm volatile ("movq %%" #reg ", %0" : "=a" (var) :: "memory");
-
-# define store_register_in_frame(reg, f)     store_register(reg, (f)->reg)
-
-# define arch_store_frame(f)                     \
-    store_register_in_frame(rsp, f)              \
-    store_register_in_frame(rbp, f)              \
-    store_register_in_frame(rbx, f)              \
-    store_register_in_frame(rsi, f)              \
-    store_register_in_frame(rdi, f)              \
-    store_register_in_frame(r12, f)              \
-    store_register_in_frame(r13, f)              \
-    store_register_in_frame(r14, f)              \
-    store_register_in_frame(r15, f)
-
-# define restore_register(reg, var, clobber...)  \
-    asm volatile ("movq %0, %%" #reg :: "g" (var) : "memory", ##clobber);
-
-# define restore_register_in_frame(reg, f)       \
-    restore_register(reg, (f)->reg,              \
-                     "r15", "r14", "r13", "r12", "rdi", "rsi", "rbx")
-
-# define arch_restore_frame(f)                   \
-    restore_register_in_frame(r15, f)            \
-    restore_register_in_frame(r14, f)            \
-    restore_register_in_frame(r13, f)            \
-    restore_register_in_frame(r12, f)            \
-    restore_register_in_frame(rdi, f)            \
-    restore_register_in_frame(rsi, f)            \
-    restore_register_in_frame(rbx, f)            \
-    restore_register_in_frame(rbp, f)            \
-    restore_register_in_frame(rsp, f)
-#else /* __x86_64__ */
-# error "unsupported architecture"
-#endif
-
-#define PAL_FRAME_IDENTIFIER    (0xdeaddeadbeefbeef)
-
-struct pal_frame {
-    volatile uint64_t           identifier;
-    void *                      func;
-    const char *                funcname;
-    struct arch_frame           arch;
-};
-
-/* When a PAL call is issued, a special PAL_FRAME is placed on the stack.
- * This stores both a magic identifier, debugging information,
- * as well as callee-saved state.  This is used as a way to deal
- * with PAL-internal failures where the goal is to exit the PAL and return a
- * failure.
- *
- * Arguably, an alternative is to unwind the stack and handle error cases at
- * each stage.  In general, this is probably more robust, but would take work
- * in the short term.  The one exception where the current strategy is
- * probably better is when the PAL gets in a state where the code is
- * unrecoverable, but ideally, this shouldn't happen.
- */
-
-/* DEP 12/25/17: This frame storage thing is important to mark volatile.
- * The compiler should not optimize out any of these changes, and
- * because some accesses can happen during an exception, these are not
- * visible to the compiler in an otherwise stack-local variable (so the
- * compiler will try to optimize out these assignments.
- */
-static inline
-void __store_frame (volatile struct pal_frame * frame,
-                    void * func, const char * funcname)
-{
-    arch_store_frame(&frame->arch)
-    frame->func = func;
-    frame->funcname = funcname;
-    asm volatile ("nop" ::: "memory");
-    frame->identifier = PAL_FRAME_IDENTIFIER;
-}
-
-#define ENTER_PAL_CALL(name)                \
-    struct pal_frame frame;                 \
-    __store_frame(&frame, &(name), #name)
-
-
-static inline
-void __clear_frame (volatile struct pal_frame * frame)
-{
-    if (frame->identifier == PAL_FRAME_IDENTIFIER) {
-        asm volatile ("nop" ::: "memory");
-        frame->identifier = 0;
-    }
-}
-
-#define LEAVE_PAL_CALL()                    \
-    do {                                    \
-        __clear_frame(&frame);              \
-    } while (0)
-
-#define LEAVE_PAL_CALL_RETURN(retval)       \
-    do {                                    \
-        __clear_frame(&frame);              \
-        return (retval);                    \
-} while (0)
-
-#endif /* PAL_HOST_H */

+ 0 - 78
Pal/src/host/FreeBSD/pal_security.h

@@ -1,78 +0,0 @@
-/* Copyright (C) 2014 Stony Brook University
-   This file is part of Graphene Library OS.
-
-   Graphene Library OS is free software: you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public License
-   as published by the Free Software Foundation, either version 3 of the
-   License, or (at your option) any later version.
-
-   Graphene Library OS is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
-
-#ifndef PAL_SECURITY_H
-#define PAL_SECURITY_H
-
-#include "pal.h"
-
-#define PATH_MAX    80
-#define PIPE_MAX    32
-
-struct link_gdb_map;
-
-/* Rendezvous structure used by the run-time dynamic linker to communicate
-   details of shared object loading to the debugger.  If the executable's
-   dynamic section has a DT_DEBUG element, the run-time linker sets that
-   element's value to the address where this structure can be found.  */
-struct r_debug {
-    int r_version;           /* Version number for this protocol.  */
-
-    struct link_map * r_map; /* Head of the chain of loaded objects.  */
-
-    /* This is the address of a function internal to the run-time linker,
-       that will always be called when the linker begins to map in a
-       library or unmap it, and again when the mapping change is complete.
-       The debugger can set a breakpoint at this address if it wants to
-       notice shared object mapping changes.  */
-    void (*r_brk) (struct r_debug *, struct link_gdb_map *);
-    enum {
-        /* This state value describes the mapping change taking place when
-           the `r_brk' address is called.  */
-        RT_CONSISTENT,  /* Mapping change is complete.  */
-        RT_ADD,         /* Beginning to add a new object.  */
-        RT_DELETE       /* Beginning to remove an object mapping.  */
-    } r_state;
-};
-
-void pal_r_debug_state (struct r_debug *, struct link_gdb_map *);
-
-/* This structure communicates dl state to the debugger.  The debugger
-   normally finds it via the DT_DEBUG entry in the dynamic section, but in
-   a statically-linked program there is no dynamic section for the debugger
-   to examine and it looks for this particular symbol name.  */
-extern struct r_debug pal_r_debug;
-symbol_version_default(pal_r_debug, r_debug, PAL);
-
-extern struct pal_sec {
-    unsigned int        domain_id;
-    char                pipe_prefix[PIPE_MAX];
-    void *              user_addr_base;
-    int                 rand_gen;
-    unsigned short      mcast_port;
-    void                (*r_debug_state) (struct r_debug *,
-                                          struct link_gdb_map *);
-    struct r_debug *    r_debug;
-} pal_sec;
-
-#define GRAPHENE_TEMPDIR        "/tmp/graphene"
-#define GRAPHENE_PIPEDIR        (GRAPHENE_TEMPDIR "/pipes")
-
-#define PROC_INIT_FD    255
-
-#define GRAPHENE_MCAST_GROUP "239.0.0.1"
-
-#endif /* PAL_SECURITY_H */

+ 0 - 106
Pal/src/host/FreeBSD/rfork_thread.S

@@ -1,106 +0,0 @@
-/*-
- * Copyright (c) 2000 Peter Wemm <peter@FreeBSD.org>
- * Copyright (c) 2003 Alan L. Cox <alc@cs.rice.edu>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <machine/asm.h>
-#include <sys/syscall.h>
-
-/*
- * With thanks to John Dyson for the original version of this.
- */
-
-/*
- *              %edi   %rsi        %rdx       %rcx
- * rfork_thread(flags, stack_addr, start_fnc, start_arg);
- *
- * flags:		Flags to rfork system call.  See rfork(2).
- * stack_addr:		Top of stack for thread.
- * start_fnc:		Address of thread function to call in child.
- * start_arg:		Argument to pass to the thread function in child.
- */
-
-ENTRY(rfork_thread)
-	pushq	%rbx
-	pushq	%r12
-	movq	%rdx, %rbx
-	movq	%rcx, %r12
-
-	/*
-	 * Prepare and execute the thread creation syscall
-	 */
-	movq	$SYS_rfork, %rax
-	int	$0x80
-	jb 	2f
-
-	/*
-	 * Check to see if we are in the parent or child
-	 */
-	cmpl	$0, %edx
-	jnz	1f
-	popq	%r12
-	popq	%rbx
-	ret
-
-	/*
-	 * If we are in the child (new thread), then
-	 * set-up the call to the internal subroutine.  If it
-	 * returns, then call __exit.
-	 */
-1:
-	movq	%rsi, %rsp
-	movq	%r12, %rdi
-	call	*%rbx
-	movl	%eax, %edi
-
-	/*
-	 * Exit system call
-	 */
-#ifdef SYS_exit
-	movq	$SYS_exit, %rax
-#else
-	movq	$SYS_sys_exit, %rax
-#endif
-	int	$0x80
-
-	/*
-	 * Branch here if the thread creation fails:
-	 */
-2:
-	popq	%r12
-	popq	%rbx
-	/*
-	* Exit if error during fork
-	*/
-#ifdef SYS_exit
-	movq	$SYS_exit, %rax
-#else
-	movq	$SYS_sys_exit, %rax
-#endif
-	int	$0x80
-
-END(rfork_thread)
-
-	.section .note.GNU-stack,"",%progbits

+ 0 - 177
Pal/src/host/FreeBSD/sysdep-x86_64.h

@@ -1,177 +0,0 @@
-/* Copyright (C) 2014 Stony Brook University
-   This file is part of Graphene Library OS.
-
-   Graphene Library OS is free software: you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public License
-   as published by the Free Software Foundation, either version 3 of the
-   License, or (at your option) any later version.
-
-   Graphene Library OS is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
-
-/* This file is imported and modified from the GNU C Library */
-
-/* This file was copied directly from the Linux implementation and modified to work on FreeBSD.
-    It may contain a lot of unnecessary code. */
-#ifndef _LINUX_X86_64_SYSDEP_H
-#define _LINUX_X86_64_SYSDEP_H 1
-
-#include <sysdeps/generic/sysdep.h>
-#include <sys/syscall.h>
-
-/* BSD useds SYS_* sys call names, so we can use these to map syscall names to syscall numbers from syscall.h */
-#define SYS_ifyBSD(syscall_name) SYS_##syscall_name
-
-#ifdef __ASSEMBLER__
-
-/* ELF uses byte-counts for .align, most others use log2 of count of bytes.  */
-#define ALIGNARG(log2) (1 << (log2))
-#define ASM_GLOBAL_DIRECTIVE .global
-/* For ELF we need the `.type' directive to make shared libs work right.  */
-#define ASM_TYPE_DIRECTIVE(name,typearg) .type name,typearg;
-#define ASM_SIZE_DIRECTIVE(name) .size name,.-name;
-
-/* Define an entry point visible from C.  */
-#define ENTRY(name)                                            \
-  ASM_GLOBAL_DIRECTIVE name;                                   \
-  ASM_TYPE_DIRECTIVE (name,@function)                          \
-  .align ALIGNARG(4);                                          \
-  name:                                                        \
-  cfi_startproc;
-
-#undef  END
-#define END(name)                                              \
-  cfi_endproc;                                                 \
-  ASM_SIZE_DIRECTIVE(name)
-
-#undef  DO_CALL
-#define DO_CALL(syscall_name, args)             \
-    DOARGS_##args                               \
-    movl $SYS_ify (syscall_name), %eax;         \
-    syscall;
-
-#define DOARGS_0 /* nothing */
-#define DOARGS_1 /* nothing */
-#define DOARGS_2 /* nothing */
-#define DOARGS_3 /* nothing */
-#define DOARGS_4 movq %rcx, %r10;
-#define DOARGS_5 DOARGS_4
-#define DOARGS_6 DOARGS_5
-
-#else    /* !__ASSEMBLER__ */
-/* Define a macro which expands inline into the wrapper code for a system
-   call.  */
-#undef INLINE_SYSCALL
-
-#define INLINE_SYSCALL(name, nr, args...) INTERNAL_SYSCALL(name, , nr, args)
-
-#undef INTERNAL_SYSCALL_DECL
-#define INTERNAL_SYSCALL_DECL(err) do { } while (0)
-
-#define INTERNAL_SYSCALL_NCS(name, err, nr, args...)    \
-  ({                                                    \
-    LOAD_ARGS_##nr (args)                               \
-    LOAD_REGS_##nr                                      \
-    unsigned long resultvar;                            \
-    asm volatile (                                      \
-    "int $0x80\n\t"                                     \
-    : "=a" (resultvar)                                  \
-    : "0" (name) ASM_ARGS_##nr                          \
-    : "memory", "cc");                                  \
-    (long) IS_SYSCALL_ERROR(resultvar);                 \
-   })
-
-#undef INTERNAL_SYSCALL
-#define INTERNAL_SYSCALL(name, err, nr, args...)        \
-INTERNAL_SYSCALL_NCS(SYS_ifyBSD(name), err, nr, ##args)
-
-#undef INTERNAL_SYSCALL_ERROR
-#define INTERNAL_SYSCALL_ERROR(val) ((val) < 0)
-
-#undef INTERNAL_SYSCALL_ERROR_P
-#define INTERNAL_SYSCALL_ERROR_P(val) \
-  ((unsigned long) (val) >= -4095L)
-
-#undef INTERNAL_SYSCALL_ERRNO
-#define INTERNAL_SYSCALL_ERRNO(val) (-(val))
-
-#undef INTERNAL_SYSCALL_ERRNO_P
-#define INTERNAL_SYSCALL_ERRNO_P(val) (-((long) val))
-
-/*
-If a syscall fails, it generally sets the carry flag and returns the error code in rax.
-To simplify matters and reuse a lot of the Linux code, we change rax to negative after checking the carry flag.
-*/
-
-#define IS_SYSCALL_ERROR(val)                       \
-    ({                                              \
-    int carry;                                      \
-    asm volatile("mov $0, %0\n\t"                   \
-          "adc $0, %0\n\t"                          \
-          : "=b"(carry)                             \
-          :                                         \
-          : "cc", "memory", "eax");                 \
-     (carry) ? -(val) : (val); })
-
-#define LOAD_ARGS_0()
-#define LOAD_REGS_0
-#define ASM_ARGS_0
-
-#define LOAD_ARGS_1(a1)                             \
-  long int __arg1 = (long)(a1);                     \
-  LOAD_ARGS_0()
-#define LOAD_REGS_1                                 \
-  register long int _a1 asm ("rdi") = __arg1;       \
-  LOAD_REGS_0
-
-#define ASM_ARGS_1    ASM_ARGS_0, "r" (_a1)
-
-#define LOAD_ARGS_2(a1, a2)                         \
-  long int __arg2 = (long)(a2);                     \
-  LOAD_ARGS_1(a1)
-#define LOAD_REGS_2                                 \
-  register long int _a2 asm ("rsi") = __arg2;       \
-  LOAD_REGS_1
-#define ASM_ARGS_2    ASM_ARGS_1, "r" (_a2)
-
-#define LOAD_ARGS_3(a1, a2, a3)                     \
-  long int __arg3 = (long)(a3);                     \
-  LOAD_ARGS_2(a1, a2)
-#define LOAD_REGS_3                                 \
-  register long int _a3 asm ("rdx") = __arg3;       \
-  LOAD_REGS_2
-
-#define ASM_ARGS_3    ASM_ARGS_2, "r" (_a3)
-
-#define LOAD_ARGS_4(a1, a2, a3, a4)                 \
-  long int __arg4 = (long)(a4);                     \
-  LOAD_ARGS_3(a1, a2, a3)
-#define LOAD_REGS_4                                 \
-  register long int _a4 asm ("rcx") = __arg4;       \
-  LOAD_REGS_3
-#define ASM_ARGS_4    ASM_ARGS_3, "r" (_a4)
-
-#define LOAD_ARGS_5(a1, a2, a3, a4, a5)             \
-  long int __arg5 = (long)(a5);                     \
-  LOAD_ARGS_4(a1, a2, a3, a4)
-#define LOAD_REGS_5                                 \
-  register long int _a5 asm ("r8") = __arg5;        \
-  LOAD_REGS_4
-#define ASM_ARGS_5    ASM_ARGS_4, "r" (_a5)
-
-#define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6)         \
-  long int __arg6 = (long)(a6);                     \
-  LOAD_ARGS_5(a1, a2, a3, a4, a5)
-#define LOAD_REGS_6                                 \
-register long int _a6 asm ("r9") = __arg6;          \
-  LOAD_REGS_5
-#define ASM_ARGS_6    ASM_ARGS_5, "r" (_a6)
-
-#endif    /* __ASSEMBLER__ */
-
-#endif /* linux/x86_64/sysdep.h */

+ 1 - 1
README.rst

@@ -22,7 +22,7 @@ running a complete OS in a virtual machine -- including guest customization, eas
 different OSes, and process migration.
 
 Graphene supports native, unmodified Linux applications on any platform. Currently, Graphene runs on
-Linux, FreeBSD and Intel SGX enclaves on Linux platforms.
+Linux and Intel SGX enclaves on Linux platforms.
 
 With Intel SGX support, Graphene can secure a |_| critical application in a |_| hardware-encrypted
 memory region. Graphene can protect applications from a |_| malicious system stack with minimal