Skip to content

Commit

Permalink
[Pal] Refactor simple types, part 1 (bools, enums, strings)
Browse files Browse the repository at this point in the history
There's no reason for PAL API to have custom types for these. This
commit replaces them with proper C types.

The rest of the types will be changed separately.

In order to make the PAL API more regular, creating eventfds is now done
with two calls on the host: eventfd() with the initial value of 0, and
write() with the desired initial value.

Also, DkSegmentRegister{Get,Set} were renamed to DkSegmentBase{Get,Set}.

Signed-off-by: Michał Kowalczyk <[email protected]>
  • Loading branch information
mkow committed Nov 12, 2021
1 parent 7ec3aab commit 16c91f5
Show file tree
Hide file tree
Showing 100 changed files with 805 additions and 760 deletions.
2 changes: 2 additions & 0 deletions Documentation/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ def setup(app):
('c:type', 'uint32_t'),
('c:type', 'uint64_t'),
('c:type', 'union'),
('c:type', 'enum'), # parsing of these seems to be broken:
# WARNING: c:type reference target not found: enum
]

manpages_url = 'https://manpages.debian.org/{path}'
Expand Down
12 changes: 5 additions & 7 deletions Documentation/devel/new-syscall.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ and cannot be emulated inside the library OS. Therefore, you may have to add
a |~| few new PAL calls to the existing interface.

To add a |~| new PAL call, first modify :file:`Pal/include/pal/pal.h`. Define
the PAL call in a |~| platform-independent way::
the PAL call::

PAL_BOL DkThreadSetCPUAffinity(PAL_NUM cpu_num, PAL_IDX* cpu_indexes);
bool DkThreadSetCPUAffinity(PAL_NUM cpu_num, PAL_IDX* cpu_indexes);

Make sure you use the PAL-specific data types, including :type:`PAL_BOL`,
:type:`PAL_NUM`, :type:`PAL_PTR`, :type:`PAL_FLG`, :type:`PAL_IDX`, and
:type:`PAL_STR`. The naming convention of a |~| PAL call is to start functions
with the ``Dk`` prefix, followed by a comprehensive name describing the purpose
of the PAL call.
The naming convention of a |~| PAL call is to start functions with the ``Dk``
prefix, followed by a comprehensive name describing the purpose of the PAL
call.

4. Export new PAL calls from PAL binaries (optional)
----------------------------------------------------
Expand Down
37 changes: 14 additions & 23 deletions Documentation/pal/host-abi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,10 @@ Basic types

.. doxygentypedef:: PAL_NUM
:project: pal
.. doxygentypedef:: PAL_FLG
:project: pal
.. doxygentypedef:: PAL_PTR
:project: pal
.. doxygentypedef:: PAL_STR
:project: pal
.. doxygentypedef:: PAL_IDX
:project: pal
.. doxygentypedef:: PAL_BOL
:project: pal

.. doxygendefine:: PAL_TRUE
:project: pal
.. doxygendefine:: PAL_FALSE
:project: pal


.. doxygentypedef:: PAL_PTR_RANGE
Expand Down Expand Up @@ -145,9 +134,9 @@ memory.
.. doxygenfunction:: DkVirtualMemoryFree
:project: pal

.. doxygenenum:: PAL_ALLOC
.. doxygentypedef:: pal_alloc_flags_t
:project: pal
.. doxygenenum:: PAL_PROT
.. doxygentypedef:: pal_prot_flags_t
:project: pal

.. doxygenfunction:: DkVirtualMemoryProtect
Expand Down Expand Up @@ -247,21 +236,23 @@ applications.
Flags used for stream manipulation
""""""""""""""""""""""""""""""""""

.. doxygenenum:: PAL_ACCESS
.. doxygenenum:: pal_access
:project: pal

.. doxygenenum:: PAL_SHARE
.. doxygentypedef:: pal_share_flags_t
:project: pal

.. doxygenenum:: PAL_CREATE
.. doxygenenum:: pal_create_mode
:project: pal

.. doxygenenum:: PAL_OPTION
.. doxygentypedef:: pal_stream_options_t
:project: pal

.. doxygenenum:: PAL_DELETE
.. doxygenenum:: pal_delete_mode
:project: pal

.. doxygentypedef:: pal_wait_flags_t
:project: pal

Thread creation
^^^^^^^^^^^^^^^
Expand All @@ -286,7 +277,7 @@ seven calls to create, signal, and block on synchronization objects.
Exception handling
^^^^^^^^^^^^^^^^^^

.. doxygenenum:: PAL_EVENT
.. doxygenenum:: pal_event
:project: pal

.. doxygentypedef:: PAL_CONTEXT
Expand All @@ -295,7 +286,7 @@ Exception handling
:project: pal
:members:

.. doxygentypedef:: PAL_EVENT_HANDLER
.. doxygentypedef:: pal_event_handler_t
:project: pal

.. doxygenfunction:: DkSetExceptionHandler
Expand Down Expand Up @@ -349,13 +340,13 @@ and to obtain an attestation report and quote.
.. doxygenfunction:: DkRandomBitsRead
:project: pal

.. doxygenfunction:: DkSegmentRegisterGet
.. doxygenfunction:: DkSegmentBaseGet
:project: pal

.. doxygenfunction:: DkSegmentRegisterSet
.. doxygenfunction:: DkSegmentBaseSet
:project: pal

.. doxygenenum:: PAL_SEGMENT
.. doxygenenum:: pal_segment_reg
:project: pal

.. doxygenfunction:: DkMemoryAvailableQuota
Expand Down
4 changes: 2 additions & 2 deletions LibOS/shim/include/arch/x86_64/shim_tcb-arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ struct shim_xstate {
} while (0)

static inline void set_tls(unsigned long tls) {
DkSegmentRegisterSet(PAL_SEGMENT_FS, (PAL_PTR)tls);
DkSegmentBaseSet(PAL_SEGMENT_FS, (PAL_PTR)tls);
}

static inline void set_default_tls(void) {
Expand All @@ -153,7 +153,7 @@ static inline void set_default_tls(void) {

static inline unsigned long get_tls(void) {
void* addr = NULL;
(void)DkSegmentRegisterGet(PAL_SEGMENT_FS, &addr);
(void)DkSegmentBaseGet(PAL_SEGMENT_FS, &addr);
return (unsigned long)addr;
}

Expand Down
2 changes: 1 addition & 1 deletion LibOS/shim/include/shim_checkpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct shim_mem_entry {
struct shim_mem_entry* next;
void* addr;
size_t size;
int prot; /* combination of PAL_PROT_* flags */
pal_prot_flags_t prot; /* combination of PAL_PROT_* flags */
};

struct shim_palhdl_entry {
Expand Down
25 changes: 9 additions & 16 deletions LibOS/shim/include/shim_flags_conv.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "pal.h"
#include "shim_internal.h"

static inline int LINUX_PROT_TO_PAL(int prot, int map_flags) {
static inline pal_prot_flags_t LINUX_PROT_TO_PAL(int prot, int map_flags) {
assert(WITHIN_MASK(prot, PROT_NONE | PROT_READ | PROT_WRITE | PROT_EXEC
| PROT_GROWSDOWN | PROT_GROWSUP));
return (prot & PROT_READ ? PAL_PROT_READ : 0) |
Expand All @@ -31,36 +31,29 @@ static inline int LINUX_PROT_TO_PAL(int prot, int map_flags) {
(map_flags & MAP_PRIVATE ? PAL_PROT_WRITECOPY : 0);
}

static inline int LINUX_OPEN_FLAGS_TO_PAL_ACCESS(int access) {
int ret = 0;
static inline enum pal_access LINUX_OPEN_FLAGS_TO_PAL_ACCESS(int access) {
/* FIXME: Currently PAL does not support appending, so O_APPEND is ignored. */
switch (access & O_ACCMODE) {
case O_RDONLY:
ret = PAL_ACCESS_RDONLY;
break;
return PAL_ACCESS_RDONLY;
case O_WRONLY:
ret = PAL_ACCESS_WRONLY;
break;
return PAL_ACCESS_WRONLY;
case O_RDWR:
ret = PAL_ACCESS_RDWR;
break;
return PAL_ACCESS_RDWR;
default:
BUG();
}
if (access & O_APPEND) {
/* FIXME: Currently PAL does not support appending. */
}
return ret;
}

static inline int LINUX_OPEN_FLAGS_TO_PAL_CREATE(int flags) {
static inline enum pal_create_mode LINUX_OPEN_FLAGS_TO_PAL_CREATE(int flags) {
if (WITHIN_MASK(O_CREAT | O_EXCL, flags))
return PAL_CREATE_ALWAYS;
if (flags & O_CREAT)
return PAL_CREATE_TRY;
return 0;
return PAL_CREATE_NEVER;
}

static inline int LINUX_OPEN_FLAGS_TO_PAL_OPTIONS(int flags) {
static inline pal_stream_options_t LINUX_OPEN_FLAGS_TO_PAL_OPTIONS(int flags) {
return (flags & O_CLOEXEC ? PAL_OPTION_CLOEXEC : 0) |
(flags & O_NONBLOCK ? PAL_OPTION_NONBLOCK : 0);
}
Expand Down
7 changes: 4 additions & 3 deletions LibOS/shim/include/shim_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ void debug_print_syscall_after(unsigned long sysno, ...);
* Note that using `clear_event` probably requires external locking to avoid races.
*/
static inline int create_event(AEVENTTYPE* e) {
return pal_to_unix_errno(DkStreamOpen(URI_PREFIX_PIPE, PAL_ACCESS_RDWR, 0, 0, 0, &e->event));
return pal_to_unix_errno(DkStreamOpen(URI_PREFIX_PIPE, PAL_ACCESS_RDWR, /*share_flags=*/0,
PAL_CREATE_IGNORED, /*options=*/0, &e->event));
}

static inline PAL_HANDLE event_handle(AEVENTTYPE* e) {
Expand Down Expand Up @@ -230,8 +231,8 @@ static inline int clear_event(AEVENTTYPE* e) {

while (1) {
PAL_HANDLE handle = e->event;
PAL_FLG ievent = PAL_WAIT_READ;
PAL_FLG revent = 0;
pal_wait_flags_t ievent = PAL_WAIT_READ;
pal_wait_flags_t revent = 0;

int ret = DkStreamsWaitEvents(1, &handle, &ievent, &revent, /*timeout=*/0);
if (ret < 0) {
Expand Down
2 changes: 1 addition & 1 deletion LibOS/shim/src/arch/x86_64/shim_arch_prctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ long shim_do_arch_prctl(int code, void* addr) {
return 0;

case ARCH_GET_FS:
return pal_to_unix_errno(DkSegmentRegisterGet(PAL_SEGMENT_FS, addr));
return pal_to_unix_errno(DkSegmentBaseGet(PAL_SEGMENT_FS, addr));

default:
log_warning("Not supported flag (0x%x) passed to arch_prctl", code);
Expand Down
2 changes: 1 addition & 1 deletion LibOS/shim/src/bookkeep/shim_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ int alloc_thread_libos_stack(struct shim_thread* thread) {
need_mem_free = true;

/* Create a stack guard page. */
ret = DkVirtualMemoryProtect(addr, PAGE_SIZE, PAL_PROT_NONE);
ret = DkVirtualMemoryProtect(addr, PAGE_SIZE, /*prot=*/0);
if (ret < 0) {
ret = pal_to_unix_errno(ret);
goto unmap;
Expand Down
20 changes: 10 additions & 10 deletions LibOS/shim/src/fs/chroot/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ static int chroot_temp_open(struct shim_dentry* dent, mode_t type, PAL_HANDLE* o
if (ret < 0)
return ret;

ret = DkStreamOpen(uri, PAL_ACCESS_RDONLY, /*share_flags=*/0, /*create=*/0, /*options=*/0,
out_palhdl);
ret = DkStreamOpen(uri, PAL_ACCESS_RDONLY, /*share_flags=*/0, PAL_CREATE_NEVER,
/*options=*/0, out_palhdl);
free(uri);
return pal_to_unix_errno(ret);
}
Expand All @@ -236,9 +236,9 @@ static int chroot_do_open(struct shim_handle* hdl, struct shim_dentry* dent, mod
return ret;

PAL_HANDLE palhdl;
int access = LINUX_OPEN_FLAGS_TO_PAL_ACCESS(flags);
int create = LINUX_OPEN_FLAGS_TO_PAL_CREATE(flags);
int options = LINUX_OPEN_FLAGS_TO_PAL_OPTIONS(flags);
enum pal_access access = LINUX_OPEN_FLAGS_TO_PAL_ACCESS(flags);
enum pal_create_mode create = LINUX_OPEN_FLAGS_TO_PAL_CREATE(flags);
pal_stream_options_t options = LINUX_OPEN_FLAGS_TO_PAL_OPTIONS(flags);
mode_t host_perm = HOST_PERM(perm);
ret = DkStreamOpen(uri, access, host_perm, create, options, &palhdl);
if (ret < 0) {
Expand Down Expand Up @@ -478,7 +478,7 @@ static int chroot_mmap(struct shim_handle* hdl, void** addr, size_t size, int pr
uint64_t offset) {
assert(hdl->type == TYPE_CHROOT);

int pal_prot = LINUX_PROT_TO_PAL(prot, flags);
pal_prot_flags_t pal_prot = LINUX_PROT_TO_PAL(prot, flags);

if (flags & MAP_ANONYMOUS)
return -EINVAL;
Expand Down Expand Up @@ -599,7 +599,7 @@ static int chroot_unlink(struct shim_dentry* dir, struct shim_dentry* dent) {
if (ret < 0)
goto out;

ret = DkStreamDelete(palhdl, /*access=*/0);
ret = DkStreamDelete(palhdl, PAL_DELETE_ALL);
DkObjectClose(palhdl);
if (ret < 0) {
ret = pal_to_unix_errno(ret);
Expand Down Expand Up @@ -716,9 +716,9 @@ static int chroot_reopen(struct shim_handle* hdl, PAL_HANDLE* out_palhdl) {
PAL_HANDLE palhdl;

mode_t mode = 0;
int access = LINUX_OPEN_FLAGS_TO_PAL_ACCESS(hdl->flags);
int create = 0;
int options = LINUX_OPEN_FLAGS_TO_PAL_OPTIONS(hdl->flags);
enum pal_access access = LINUX_OPEN_FLAGS_TO_PAL_ACCESS(hdl->flags);
enum pal_create_mode create = PAL_CREATE_NEVER;
pal_stream_options_t options = LINUX_OPEN_FLAGS_TO_PAL_OPTIONS(hdl->flags);
int ret = DkStreamOpen(uri, access, mode, create, options, &palhdl);
if (ret < 0)
return pal_to_unix_errno(ret);
Expand Down
4 changes: 2 additions & 2 deletions LibOS/shim/src/fs/pipe/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ static int pipe_setflags(struct shim_handle* hdl, int flags) {
if (flags & O_NONBLOCK)
return 0;

attr.nonblocking = PAL_FALSE;
attr.nonblocking = false;
} else {
if (!(flags & O_NONBLOCK))
return 0;

attr.nonblocking = PAL_TRUE;
attr.nonblocking = true;
}

ret = DkStreamAttributesSetByHandle(hdl->pal_handle, &attr);
Expand Down
4 changes: 2 additions & 2 deletions LibOS/shim/src/fs/socket/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,12 @@ static int socket_setflags(struct shim_handle* hdl, int flags) {
if (flags & O_NONBLOCK)
return 0;

attr.nonblocking = PAL_FALSE;
attr.nonblocking = false;
} else {
if (!(flags & O_NONBLOCK))
return 0;

attr.nonblocking = PAL_TRUE;
attr.nonblocking = true;
}

ret = DkStreamAttributesSetByHandle(hdl->pal_handle, &attr);
Expand Down
3 changes: 2 additions & 1 deletion LibOS/shim/src/ipc/shim_ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ static int ipc_connect(IDTYPE dest, struct shim_ipc_connection** conn_ptr) {
log_error("buffer for IPC pipe URI too small");
BUG();
}
ret = DkStreamOpen(uri, 0, 0, 0, 0, &conn->handle);
ret = DkStreamOpen(uri, PAL_ACCESS_RDONLY, /*share_flags=*/0, PAL_CREATE_IGNORED,
/*options=*/0, &conn->handle);
if (ret < 0) {
ret = pal_to_unix_errno(ret);
goto out;
Expand Down
4 changes: 2 additions & 2 deletions LibOS/shim/src/ipc/shim_ipc_worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ static noreturn void ipc_worker_main(void) {
* be rare). */
struct shim_ipc_connection** connections = NULL;
PAL_HANDLE* handles = NULL;
PAL_FLG* events = NULL;
PAL_FLG* ret_events = NULL;
pal_wait_flags_t* events = NULL;
pal_wait_flags_t* ret_events = NULL;
size_t prev_items_cnt = 0;

while (1) {
Expand Down
12 changes: 6 additions & 6 deletions LibOS/shim/src/shim_async.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,13 @@ static void shim_async_worker(void* arg) {
goto out_err;
}

/* allocate one memory region to hold two PAL_FLG arrays: events and revents */
PAL_FLG* pal_events = malloc(sizeof(*pal_events) * (1 + pals_max_cnt) * 2);
/* allocate one memory region to hold two pal_wait_flags_t arrays: events and revents */
pal_wait_flags_t* pal_events = malloc(sizeof(*pal_events) * (1 + pals_max_cnt) * 2);
if (!pal_events) {
log_error("Allocation of pal_events failed");
goto out_err;
}
PAL_FLG* ret_events = pal_events + 1 + pals_max_cnt;
pal_wait_flags_t* ret_events = pal_events + 1 + pals_max_cnt;

PAL_HANDLE install_new_event_pal = event_handle(&install_new_event);
pals[0] = install_new_event_pal;
Expand Down Expand Up @@ -222,13 +222,13 @@ static void shim_async_worker(void* arg) {
log_error("tmp_pals allocation failed");
goto out_err_unlock;
}
PAL_FLG* tmp_pal_events =
pal_wait_flags_t* tmp_pal_events =
malloc(sizeof(*tmp_pal_events) * (2 + pals_max_cnt * 4));
if (!tmp_pal_events) {
log_error("tmp_pal_events allocation failed");
goto out_err_unlock;
}
PAL_FLG* tmp_ret_events = tmp_pal_events + 1 + pals_max_cnt * 2;
pal_wait_flags_t* tmp_ret_events = tmp_pal_events + 1 + pals_max_cnt * 2;

memcpy(tmp_pals, pals, sizeof(*tmp_pals) * (1 + pals_max_cnt));
memcpy(tmp_pal_events, pal_events,
Expand Down Expand Up @@ -290,7 +290,7 @@ static void shim_async_worker(void* arg) {
log_error("DkStreamsWaitEvents failed with: %d", ret);
goto out_err;
}
PAL_BOL polled = ret == 0;
bool polled = ret == 0;

ret = DkSystemTimeQuery(&now);
if (ret < 0) {
Expand Down
Loading

0 comments on commit 16c91f5

Please sign in to comment.