refactor: Use C18 features

master
Sean McBride 4 years ago
parent 96fbac9b30
commit ebf6dc2e84

@ -7,7 +7,7 @@ endif
# Compiler Settings
CC=clang
CC_OPTIONS = -O3 -flto -g -pthread -D_GNU_SOURCE
CC_OPTIONS = -O3 -flto -g -pthread -D_GNU_SOURCE -std=c18
# CC_OPTIONS for Debugging
# CC_OPTIONS = -O0 -g -pthread -D_GNU_SOURCE

@ -79,33 +79,33 @@ arch_context_switch(struct arch_context *a, struct arch_context *b)
reg_t *a_registers = a->regs, *b_registers = b->regs;
assert(a_registers && b_registers);
asm volatile("mov x0, sp\n\t"
"adr x1, reset%=\n\t"
"str x1, [%[a], 8]\n\t"
"str x0, [%[a]]\n\t"
"mov x0, #1\n\t"
"str x0, [%[av]]\n\t"
"ldr x1, [%[bv]]\n\t"
"sub x1, x1, #2\n\t"
"cbz x1, slow%=\n\t"
"ldr x0, [%[b]]\n\t"
"ldr x1, [%[b], 8]\n\t"
"mov sp, x0\n\t"
"br x1\n\t"
"slow%=:\n\t"
"br %[slowpath]\n\t"
".align 8\n\t"
"reset%=:\n\t"
"mov x1, #3\n\t"
"str x1, [%[bv]]\n\t"
".align 8\n\t"
"exit%=:\n\t"
:
: [a] "r"(a_registers), [b] "r"(b_registers), [av] "r"(&a->variant), [bv] "r"(&b->variant),
[slowpath] "r"(&arch_context_restore_preempted)
: "memory", "cc", "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11", "x12",
"x13", "x14", "x15", "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23", "x24", "d8", "d9",
"d10", "d11", "d12", "d13", "d14", "d15");
__asm__ volatile("mov x0, sp\n\t"
"adr x1, reset%=\n\t"
"str x1, [%[a], 8]\n\t"
"str x0, [%[a]]\n\t"
"mov x0, #1\n\t"
"str x0, [%[av]]\n\t"
"ldr x1, [%[bv]]\n\t"
"sub x1, x1, #2\n\t"
"cbz x1, slow%=\n\t"
"ldr x0, [%[b]]\n\t"
"ldr x1, [%[b], 8]\n\t"
"mov sp, x0\n\t"
"br x1\n\t"
"slow%=:\n\t"
"br %[slowpath]\n\t"
".align 8\n\t"
"reset%=:\n\t"
"mov x1, #3\n\t"
"str x1, [%[bv]]\n\t"
".align 8\n\t"
"exit%=:\n\t"
:
: [a] "r"(a_registers), [b] "r"(b_registers), [av] "r"(&a->variant), [bv] "r"(&b->variant),
[slowpath] "r"(&arch_context_restore_preempted)
: "memory", "cc", "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11",
"x12", "x13", "x14", "x15", "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23", "x24",
"d8", "d9", "d10", "d11", "d12", "d13", "d14", "d15");
return 0;
}

@ -3,6 +3,7 @@
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdnoreturn.h>
#include <string.h>
#include <ucontext.h>
#include <unistd.h>
@ -30,4 +31,4 @@
*/
/* Cannot be inlined because called in assembly */
void __attribute__((noinline)) __attribute__((noreturn)) arch_context_restore_preempted(void);
noreturn void __attribute__((noinline)) arch_context_restore_preempted(void);

@ -31,14 +31,14 @@ arch_context_init(struct arch_context *actx, reg_t ip, reg_t sp)
* to push the stack pointer sp to the top of its own stack.
* This acts as the base pointer
*/
asm volatile("movq %%rsp, %%rbx\n\t" /* Temporarily save pointer of active stack to B */
"movq %%rax, %%rsp\n\t" /* Set active stack to stack pointer in A(C variable sp) */
"pushq %%rax\n\t" /* Push A register (C variable sp) onto the stack at sp */
"movq %%rsp, %%rax\n\t" /* Write the incremented stack pointer to A(C variable sp) */
"movq %%rbx, %%rsp\n\t" /* Restore original stack saved in B */
: "=a"(sp)
: "a"(sp)
: "memory", "cc", "rbx");
__asm__ volatile("movq %%rsp, %%rbx\n\t" /* Temporarily save pointer of active stack to B */
"movq %%rax, %%rsp\n\t" /* Set active stack to stack pointer in A(C variable sp) */
"pushq %%rax\n\t" /* Push A register (C variable sp) onto the stack at sp */
"movq %%rsp, %%rax\n\t" /* Write the incremented stack pointer to A(C variable sp) */
"movq %%rbx, %%rsp\n\t" /* Restore original stack saved in B */
: "=a"(sp)
: "a"(sp)
: "memory", "cc", "rbx");
}
// FIXME: Is the klobber list correct? Issue #129
// : "memory", "cc", "rbx", "rsi", "rdi");
@ -109,7 +109,7 @@ arch_context_switch(struct arch_context *a, struct arch_context *b)
reg_t *a_registers = a->regs, *b_registers = b->regs;
assert(a_registers && b_registers);
asm volatile(
__asm__ volatile(
/* Create a new stack frame */
"pushq %%rbp\n\t" /* stack[stack_len++] = base_pointer */
"movq %%rsp, %%rbp\n\t" /* base_pointer = stack_pointer. Start new Frame */

@ -1,11 +1,13 @@
#pragma once
#include <threads.h>
#include "sandbox_types.h"
/* current sandbox that is active.. */
extern __thread struct sandbox *worker_thread_current_sandbox;
extern thread_local struct sandbox *worker_thread_current_sandbox;
extern __thread struct sandbox_context_cache local_sandbox_context_cache;
extern thread_local struct sandbox_context_cache local_sandbox_context_cache;
void current_sandbox_start(void);

@ -1,10 +1,11 @@
#pragma once
#include <stdint.h>
#include <threads.h>
extern __thread uint64_t generic_thread_lock_duration;
extern __thread uint64_t generic_thread_lock_longest;
extern __thread uint64_t generic_thread_start_timestamp;
extern thread_local uint64_t generic_thread_lock_duration;
extern thread_local uint64_t generic_thread_lock_longest;
extern thread_local uint64_t generic_thread_start_timestamp;
void generic_thread_dump_lock_overhead(void);
void generic_thread_initialize(void);

@ -1,6 +1,7 @@
#pragma once
#include <stdbool.h>
#include <stdnoreturn.h>
#include "generic_thread.h"
#include "module.h"
@ -9,9 +10,9 @@
extern pthread_t listener_thread_id;
void listener_thread_initialize(void);
__attribute__((noreturn)) void *listener_thread_main(void *dummy);
int listener_thread_register_module(struct module *mod);
void listener_thread_initialize(void);
noreturn void *listener_thread_main(void *dummy);
int listener_thread_register_module(struct module *mod);
/**
* Used to determine if running in the context of a listener thread

@ -8,6 +8,7 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <threads.h>
#include "debuglog.h"
#include "runtime.h"
@ -17,8 +18,8 @@
* Externs *
***********/
extern _Atomic __thread volatile sig_atomic_t software_interrupt_deferred_sigalrm;
extern _Atomic volatile sig_atomic_t * software_interrupt_deferred_sigalrm_max;
extern _Atomic thread_local volatile sig_atomic_t software_interrupt_deferred_sigalrm;
extern _Atomic volatile sig_atomic_t * software_interrupt_deferred_sigalrm_max;
/*************************
* Public Static Inlines *

@ -2,6 +2,7 @@
#include <stdint.h>
#include <stdio.h>
#include <threads.h>
#include "wasm_types.h"
@ -33,4 +34,4 @@ struct sandbox_context_cache {
struct indirect_table_entry *module_indirect_table;
};
extern __thread struct sandbox_context_cache local_sandbox_context_cache;
extern thread_local struct sandbox_context_cache local_sandbox_context_cache;

@ -1,11 +1,13 @@
#pragma once
#include <threads.h>
#include "generic_thread.h"
#include "runtime.h"
extern __thread struct arch_context worker_thread_base_context;
extern __thread int worker_thread_epoll_file_descriptor;
extern __thread int worker_thread_idx;
extern thread_local struct arch_context worker_thread_base_context;
extern thread_local int worker_thread_epoll_file_descriptor;
extern thread_local int worker_thread_idx;
void *worker_thread_main(void *return_code);

@ -6,7 +6,7 @@ unsigned long long int
__getcycles(void)
{
unsigned long long virtual_timer_value;
asm volatile("mrs %0, cntvct_el0" : "=r"(virtual_timer_value));
__asm__ volatile("mrs %0, cntvct_el0" : "=r"(virtual_timer_value));
return virtual_timer_value;
}

@ -2,6 +2,7 @@
#include <signal.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdnoreturn.h>
#include "panic.h"
@ -11,7 +12,7 @@
* then update the registers we should return to, then sigreturn (by returning from the handler). This returns to the
* control flow restored from the mcontext
*/
void __attribute__((noinline)) __attribute__((noreturn)) arch_context_restore_preempted(void)
noreturn void __attribute__((noinline)) arch_context_restore_preempted(void)
{
pthread_kill(pthread_self(), SIGUSR1);
panic("Unexpectedly reached code after sending self SIGUSR1\n");

@ -1,3 +1,5 @@
#include <threads.h>
#include "current_sandbox.h"
#include "sandbox_functions.h"
#include "sandbox_receive_request.h"
@ -8,9 +10,9 @@
#include "scheduler.h"
#include "software_interrupt.h"
__thread struct sandbox *worker_thread_current_sandbox = NULL;
thread_local struct sandbox *worker_thread_current_sandbox = NULL;
__thread struct sandbox_context_cache local_sandbox_context_cache = {
thread_local struct sandbox_context_cache local_sandbox_context_cache = {
.memory = {
.start = NULL,
.size = 0,

@ -1,13 +1,14 @@
#include <stdint.h>
#include <threads.h>
#include "arch/getcycles.h"
#include "debuglog.h"
/* Implemented by listener and workers */
__thread uint64_t generic_thread_lock_duration = 0;
__thread uint64_t generic_thread_lock_longest = 0;
__thread uint64_t generic_thread_start_timestamp = 0;
thread_local uint64_t generic_thread_lock_duration = 0;
thread_local uint64_t generic_thread_lock_longest = 0;
thread_local uint64_t generic_thread_start_timestamp = 0;
void
generic_thread_initialize()

@ -1,20 +1,22 @@
#include <stdnoreturn.h>
#include "global_request_scheduler.h"
#include "panic.h"
/* Default uninitialized implementations of the polymorphic interface */
__attribute__((noreturn)) static struct sandbox_request *
noreturn static struct sandbox_request *
uninitialized_add(void *arg)
{
panic("Global Request Scheduler Add was called before initialization\n");
}
__attribute__((noreturn)) static int
noreturn static int
uninitialized_remove(struct sandbox_request **arg)
{
panic("Global Request Scheduler Remove was called before initialization\n");
}
__attribute__((noreturn)) static uint64_t
noreturn static uint64_t
uninitialized_peek()
{
panic("Global Request Scheduler Peek was called before initialization\n");

@ -73,7 +73,7 @@ listener_thread_register_module(struct module *mod)
* listener_thread_epoll_file_descriptor - the epoll file descriptor
*
*/
__attribute__((noreturn)) void *
noreturn void *
listener_thread_main(void *dummy)
{
struct epoll_event epoll_events[RUNTIME_MAX_EPOLL_EVENTS];

@ -1,7 +1,9 @@
#include <threads.h>
#include "local_completion_queue.h"
#include "sandbox_functions.h"
__thread static struct ps_list_head local_completion_queue;
thread_local static struct ps_list_head local_completion_queue;
void

@ -2,13 +2,14 @@
#ifdef LOG_LOCAL_RUNQUEUE
#include <stdint.h>
#endif
#include <threads.h>
#include "local_runqueue.h"
static struct local_runqueue_config local_runqueue;
#ifdef LOG_LOCAL_RUNQUEUE
__thread uint32_t local_runqueue_count = 0;
thread_local uint32_t local_runqueue_count = 0;
#endif
/* Initializes a concrete implementation of the sandbox request scheduler interface */

@ -1,3 +1,5 @@
#include <threads.h>
#include "client_socket.h"
#include "current_sandbox.h"
#include "global_request_scheduler.h"
@ -5,7 +7,7 @@
#include "local_runqueue.h"
#include "sandbox_functions.h"
__thread static struct ps_list_head local_runqueue_list;
thread_local static struct ps_list_head local_runqueue_list;
bool
local_runqueue_list_is_empty()

@ -1,4 +1,5 @@
#include <stdint.h>
#include <threads.h>
#include "arch/context.h"
#include "client_socket.h"
@ -12,7 +13,7 @@
#include "sandbox_functions.h"
#include "runtime.h"
__thread static struct priority_queue *local_runqueue_minheap;
thread_local static struct priority_queue *local_runqueue_minheap;
/**
* Checks if the run queue is empty

@ -5,6 +5,7 @@
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <threads.h>
#include <unistd.h>
#include <ucontext.h>
@ -31,11 +32,11 @@ static uint64_t software_interrupt_interval_duration_in_cycles;
* Thread Globals *
*****************/
__thread _Atomic static volatile sig_atomic_t software_interrupt_SIGALRM_kernel_count = 0;
__thread _Atomic static volatile sig_atomic_t software_interrupt_SIGALRM_thread_count = 0;
__thread _Atomic static volatile sig_atomic_t software_interrupt_SIGUSR_count = 0;
__thread _Atomic volatile sig_atomic_t software_interrupt_deferred_sigalrm = 0;
__thread _Atomic volatile sig_atomic_t software_interrupt_signal_depth = 0;
thread_local _Atomic static volatile sig_atomic_t software_interrupt_SIGALRM_kernel_count = 0;
thread_local _Atomic static volatile sig_atomic_t software_interrupt_SIGALRM_thread_count = 0;
thread_local _Atomic static volatile sig_atomic_t software_interrupt_SIGUSR_count = 0;
thread_local _Atomic volatile sig_atomic_t software_interrupt_deferred_sigalrm = 0;
thread_local _Atomic volatile sig_atomic_t software_interrupt_signal_depth = 0;
_Atomic volatile sig_atomic_t *software_interrupt_deferred_sigalrm_max;

@ -4,6 +4,7 @@
#include <signal.h>
#include <sched.h>
#include <stdlib.h>
#include <threads.h>
#include "current_sandbox.h"
#include "local_completion_queue.h"
@ -21,12 +22,12 @@
**************************/
/* context of the runtime thread before running sandboxes or to resume its "main". */
__thread struct arch_context worker_thread_base_context;
thread_local struct arch_context worker_thread_base_context;
__thread int worker_thread_epoll_file_descriptor;
thread_local int worker_thread_epoll_file_descriptor;
/* Used to index into global arguments and deadlines arrays */
__thread int worker_thread_idx;
thread_local int worker_thread_idx;
/***********************
* Worker Thread Logic *

Loading…
Cancel
Save