Skip to content

Commit

Permalink
Merge pull request #400 from immunant/kkysen/remove-LIBIA2-X86_64-and…
Browse files Browse the repository at this point in the history
…-LIBIA2_AARCH64-defines

runtime: use builtin `__{x86_64, aarch64}__` instead of `LIBIA2_{X86_64,AARCH64}`
  • Loading branch information
kkysen authored Sep 30, 2024
2 parents 0f72587 + 25f1a2f commit 89bac1a
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 31 deletions.
2 changes: 1 addition & 1 deletion cmake/ia2.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
if(LIBIA2_AARCH64)
if (LIBIA2_AARCH64)
set(UBSAN_FLAG "")
else()
set(UBSAN_FLAG "-fsanitize=undefined")
Expand Down
2 changes: 0 additions & 2 deletions runtime/libia2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ add_library(libia2 ia2.c init.c threads.c main.c exit.c)
target_compile_options(libia2 PRIVATE "-fPIC")

if (LIBIA2_AARCH64)
target_compile_definitions(libia2 PUBLIC LIBIA2_AARCH64=1)
target_compile_options(libia2 PUBLIC "-march=armv8.5-a+memtag" "-ffixed-x18")
else()
target_compile_definitions(libia2 PUBLIC LIBIA2_X86_64=1)
endif()

if(LIBIA2_DEBUG)
Expand Down
8 changes: 4 additions & 4 deletions runtime/libia2/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ void _exit(int status);
__asm__(
".global _exit\n"
"_exit:\n"
#if LIBIA2_X86_64
#if defined(__x86_64__)
"jmp exit\n"
#elif LIBIA2_AARCH64
#elif defined(__aarch64__)
"b exit\n"
#endif
);
Expand All @@ -29,7 +29,7 @@ void exit(int status);
__asm__(
".global exit\n"
"exit:\n"
#if LIBIA2_X86_64
#if defined(__x86_64__)
"pushq %rbp\n"
"movq %rsp, %rbp\n"
// Load the stack pointer for the shared compartment's stack.
Expand All @@ -44,7 +44,7 @@ __asm__(
"subq $8, %rsp\n"
// Call the real exit function.
"call call_libc_exit\n"
#elif LIBIA2_AARCH64
#elif defined(__aarch64__)
"stp x29, x30, [sp, #-16]!\n"
// Load the stack pointer for the shared compartment's stack.
"mrs x9, tpidr_el0\n"
Expand Down
4 changes: 2 additions & 2 deletions runtime/libia2/ia2.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "ia2_internal.h"
#include "ia2.h"

#if LIBIA2_X86_64
#if defined(__x86_64__)

__attribute__((__used__)) uint32_t ia2_get_pkru() {
uint32_t pkru = 0;
Expand Down Expand Up @@ -81,7 +81,7 @@ size_t ia2_get_pkey() {
}
size_t ia2_get_tag(void) __attribute__((alias("ia2_get_pkey")));

#elif LIBIA2_AARCH64
#elif defined(__aarch64__)

size_t ia2_get_x18(void) {
size_t x18;
Expand Down
8 changes: 4 additions & 4 deletions runtime/libia2/include/ia2_compartment_init.inc
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ __attribute__((constructor)) static void COMPARTMENT_IDENT(init_pkey)() {
.shared_sections = shared_sections,
};

#if LIBIA2_X86_64
#if defined(__x86_64__)
__asm__ volatile(
/* Set PKRU to the compartment's value */
"xorl %%ecx, %%ecx\n"
Expand All @@ -76,13 +76,13 @@ __attribute__((constructor)) static void COMPARTMENT_IDENT(init_pkey)() {
:
:
: "rax", "rcx", "rdx");
#elif LIBIA2_AARCH64
#elif defined(__aarch64__)
__asm__ volatile("movz_shifted_tag_x18 " XSTR(IA2_COMPARTMENT) "\n");
#endif

dl_iterate_phdr(protect_pages, &args);

#if LIBIA2_X86_64
#if defined(__x86_64__)
__asm__ volatile(
/* Set PKRU to fully untrusted (no access) */
"xorl %%ecx, %%ecx\n"
Expand All @@ -92,7 +92,7 @@ __attribute__((constructor)) static void COMPARTMENT_IDENT(init_pkey)() {
:
:
: "rax", "rcx", "rdx");
#elif LIBIA2_AARCH64
#elif defined(__aarch64__)
__asm__ volatile("movz_shifted_tag_x18 0\n");
#endif

Expand Down
16 changes: 8 additions & 8 deletions runtime/libia2/include/ia2_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ instead of `fn`. */

/* clang-format can't handle inline asm in macros */
/* clang-format off */
#if LIBIA2_X86_64
#if defined(__x86_64__)
#define _IA2_DEFINE_SIGNAL_HANDLER(function, pkey) \
__asm__(".global ia2_sighandler_" #function "\n" \
"ia2_sighandler_" #function ":\n" \
Expand All @@ -88,7 +88,7 @@ instead of `fn`. */
"movq %r11, %rdx\n" \
"movq %r10, %rcx\n" \
"jmp " #function "\n")
#elif LIBIA2_AARCH64
#elif defined(__aarch64__)
#define _IA2_DEFINE_SIGNAL_HANDLER(function, tag) \
__asm__(".global ia2_sighandler_" #function "\n" \
"ia2_sighandler_" #function ":\n" \
Expand Down Expand Up @@ -169,7 +169,7 @@ asm(".macro movz_shifted_tag_x18 tag\n"

#define IA2_ROUND_DOWN(x, y) ((x) & ~((y)-1))

#if LIBIA2_X86_64
#if defined(__x86_64__)
/* clang-format can't handle inline asm in macros */
/* clang-format off */
/* Allocate and protect the stack for this thread's i'th compartment. */
Expand Down Expand Up @@ -228,7 +228,7 @@ asm(".macro movz_shifted_tag_x18 tag\n"
: "rdi", "rcx", "rdx", "r10", "r11", "r12"); \
}
/* clang-format on */
#elif LIBIA2_AARCH64
#elif defined(__aarch64__)
#warning "ALLOCATE_COMPARTMENT_STACK_AND_SETUP_TLS does not do stackptr reinit checking"
#define ALLOCATE_COMPARTMENT_STACK_AND_SETUP_TLS(i) \
{ \
Expand Down Expand Up @@ -268,7 +268,7 @@ asm(".macro movz_shifted_tag_x18 tag\n"
}
#endif

#if LIBIA2_X86_64
#if defined(__x86_64__)
#define return_stackptr_if_compartment(compartment) \
if (pkru == PKRU(compartment)) { \
register void *out asm("rax"); \
Expand All @@ -280,7 +280,7 @@ asm(".macro movz_shifted_tag_x18 tag\n"
:); \
return out; \
}
#elif LIBIA2_AARCH64
#elif defined(__aarch64__)
#warning "libia2 does not implement return_stackptr_if_compartment yet"
#define return_stackptr_if_compartment(compartment)
#endif
Expand All @@ -297,9 +297,9 @@ works as a reasonable signpost no-op. */
void ia2_setup_destructors_##n(void); \
ia2_setup_destructors_##n();

#if LIBIA2_AARCH64
#if defined(__aarch64__)
int ia2_mprotect_with_tag(void *addr, size_t len, int prot, int tag);
#elif LIBIA2_X86_64
#elif defined(__x86_64__)
/* We can't use an alias attribute since this points to a function outside the translation unit */
#define ia2_mprotect_with_tag pkey_mprotect
#endif
Expand Down
4 changes: 2 additions & 2 deletions runtime/libia2/include/scrub_registers.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if LIBIA2_X86_64
#if defined(__x86_64__)
// This file defines the feature specific scrub routines.
//
// This is implemented via the fairly standard fallthrough approach, and
Expand Down Expand Up @@ -71,6 +71,6 @@ asm(".text\n"
"jmp __libia2_scrub_registers_sse\n"
"int3\n"
".previous\n");
#elif LIBIA2_AARCH64
#elif defined(__aarch64__)
#warning "__libia2_scrub_registers is not implemented for aarch64 yet"
#endif
4 changes: 2 additions & 2 deletions runtime/libia2/include/test_fault_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ char *sighandler_sp __attribute__((section("ia2_shared_data"))) =
// This function must be declared naked because it's not necessarily safe for it
// to write to the stack in its prelude (the stack isn't written to when the
// function itself is called because it's only invoked as a signal handler).
#if LIBIA2_X86_64
#if defined(__x86_64__)
__attribute__((naked)) void handle_segfault(int sig) {
// This asm must preserve %rdi which contains the argument since
// print_mpk_message reads it
Expand All @@ -69,7 +69,7 @@ __attribute__((naked)) void handle_segfault(int sig) {
"movq (%rsp), %rsp\n"
"callq print_mpk_message");
}
#elif LIBIA2_AARCH64
#elif defined(__aarch64__)
#warning "Review test_fault_handler implementation after enabling x18 switching"
void print_mpk_message(int sig);
void handle_segfault(int sig) {
Expand Down
2 changes: 1 addition & 1 deletion runtime/libia2/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void verify_tls_padding(void) {

/* Ensure that all required pkeys are allocated or no-op on aarch64. */
void ensure_pkeys_allocated(int *n_to_alloc) {
#if LIBIA2_X86_64
#if defined(__x86_64__)
if (*n_to_alloc != 0) {
for (int pkey = 1; pkey <= *n_to_alloc; pkey++) {
int allocated = pkey_alloc(0, 0);
Expand Down
4 changes: 2 additions & 2 deletions runtime/libia2/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ int __wrap_main(int argc, char **argv);
__asm__(
".global __wrap_main\n"
"__wrap_main:\n"
#if LIBIA2_X86_64
#if defined(__x86_64__)
"pushq %rbp\n"
"movq %rsp, %rbp\n"
// Save the old stack pointer in main_sp.
Expand Down Expand Up @@ -41,7 +41,7 @@ __asm__(
"mov %r10,%rax\n"
"popq %rbp\n"
"ret\n"
#elif LIBIA2_AARCH64
#elif defined(__aarch64__)
// prologue
"stp x29, x30, [sp, #-16]!\n"
"mov x29, sp\n"
Expand Down
4 changes: 2 additions & 2 deletions runtime/libia2/threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void *ia2_thread_begin(void *arg) {
* data. */
/* sigaltstack(&alt_stack, NULL); */

#if LIBIA2_X86_64
#if defined(__x86_64__)
/* Determine the current compartment so know which stack to use. */
uint32_t pkru = 0;
__asm__ volatile(
Expand Down Expand Up @@ -69,7 +69,7 @@ void *ia2_thread_begin(void *arg) {
: "rdi");
/* clang-format on */
return result;
#elif LIBIA2_AARCH64
#elif defined(__aarch64__)
#warning "libia2 does not implement ia2_thread_begin yet"
__builtin_trap();
#endif
Expand Down
2 changes: 1 addition & 1 deletion runtime/partition-alloc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ target_include_directories(partition-alloc_unpadded BEFORE
${EXTERNAL_DIR}/chromium/src)

# Propagage ARM64 define
if(LIBIA2_AARCH64)
if (LIBIA2_AARCH64)
target_compile_options(partition-alloc_unpadded
PRIVATE
"-DARCH_CPU_ARM64"
Expand Down

0 comments on commit 89bac1a

Please sign in to comment.