From f3e1b876c78eb8d8430a76ebe1a6ae5f6d97b958 Mon Sep 17 00:00:00 2001 From: osy <50960678+osy@users.noreply.github.com> Date: Sat, 2 Jan 2021 21:07:29 -0800 Subject: [PATCH 1/3] project: move freestanding include headers This is needed because in libucontext.h we include "libucontext/bits.h". We therefore need to have bits.h in somepath/libucontext/bits.h. In the Makefile, somepath/freestanding/bits.h was copied to the right path but in the meson build system, the include copy happens at the end. --- Makefile | 10 +++++----- .../{freestanding => include/libucontext}/bits.h | 0 arch/arm/{freestanding => include/libucontext}/bits.h | 0 arch/common/{ => include/libucontext}/bits.h | 0 arch/m68k/{freestanding => include/libucontext}/bits.h | 0 arch/mips/{freestanding => include/libucontext}/bits.h | 0 .../{freestanding => include/libucontext}/bits.h | 0 .../{freestanding => include/libucontext}/bits.h | 0 .../s390x/{freestanding => include/libucontext}/bits.h | 0 arch/sh/{freestanding => include/libucontext}/bits.h | 0 arch/x86/{freestanding => include/libucontext}/bits.h | 0 .../{freestanding => include/libucontext}/bits.h | 0 meson.build | 6 ++++-- 13 files changed, 9 insertions(+), 7 deletions(-) rename arch/aarch64/{freestanding => include/libucontext}/bits.h (100%) rename arch/arm/{freestanding => include/libucontext}/bits.h (100%) rename arch/common/{ => include/libucontext}/bits.h (100%) rename arch/m68k/{freestanding => include/libucontext}/bits.h (100%) rename arch/mips/{freestanding => include/libucontext}/bits.h (100%) rename arch/mips64/{freestanding => include/libucontext}/bits.h (100%) rename arch/riscv64/{freestanding => include/libucontext}/bits.h (100%) rename arch/s390x/{freestanding => include/libucontext}/bits.h (100%) rename arch/sh/{freestanding => include/libucontext}/bits.h (100%) rename arch/x86/{freestanding => include/libucontext}/bits.h (100%) rename arch/x86_64/{freestanding => include/libucontext}/bits.h (100%) diff --git a/Makefile b/Makefile index c95d3ca..cce1686 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ EXPORT_UNPREFIXED := yes FREESTANDING := no ifeq ($(FREESTANDING),yes) - CFLAGS += -DFREESTANDING -isystem arch/${ARCH}/freestanding + CFLAGS += -DFREESTANDING EXPORT_UNPREFIXED = no endif @@ -215,13 +215,13 @@ examples/cooperative_threading: examples/cooperative_threading.c ${LIBUCONTEXT_N ifeq ($(FREESTANDING),no) -include/libucontext/bits.h: arch/common/bits.h - cp arch/common/bits.h $@ +include/libucontext/bits.h: arch/common/include/libucontext/bits.h + cp $< $@ else -include/libucontext/bits.h: arch/${ARCH}/freestanding/bits.h - cp arch/${ARCH}/freestanding/bits.h $@ +include/libucontext/bits.h: arch/${ARCH}/include/libucontext/bits.h + cp $< $@ endif diff --git a/arch/aarch64/freestanding/bits.h b/arch/aarch64/include/libucontext/bits.h similarity index 100% rename from arch/aarch64/freestanding/bits.h rename to arch/aarch64/include/libucontext/bits.h diff --git a/arch/arm/freestanding/bits.h b/arch/arm/include/libucontext/bits.h similarity index 100% rename from arch/arm/freestanding/bits.h rename to arch/arm/include/libucontext/bits.h diff --git a/arch/common/bits.h b/arch/common/include/libucontext/bits.h similarity index 100% rename from arch/common/bits.h rename to arch/common/include/libucontext/bits.h diff --git a/arch/m68k/freestanding/bits.h b/arch/m68k/include/libucontext/bits.h similarity index 100% rename from arch/m68k/freestanding/bits.h rename to arch/m68k/include/libucontext/bits.h diff --git a/arch/mips/freestanding/bits.h b/arch/mips/include/libucontext/bits.h similarity index 100% rename from arch/mips/freestanding/bits.h rename to arch/mips/include/libucontext/bits.h diff --git a/arch/mips64/freestanding/bits.h b/arch/mips64/include/libucontext/bits.h similarity index 100% rename from arch/mips64/freestanding/bits.h rename to arch/mips64/include/libucontext/bits.h diff --git a/arch/riscv64/freestanding/bits.h b/arch/riscv64/include/libucontext/bits.h similarity index 100% rename from arch/riscv64/freestanding/bits.h rename to arch/riscv64/include/libucontext/bits.h diff --git a/arch/s390x/freestanding/bits.h b/arch/s390x/include/libucontext/bits.h similarity index 100% rename from arch/s390x/freestanding/bits.h rename to arch/s390x/include/libucontext/bits.h diff --git a/arch/sh/freestanding/bits.h b/arch/sh/include/libucontext/bits.h similarity index 100% rename from arch/sh/freestanding/bits.h rename to arch/sh/include/libucontext/bits.h diff --git a/arch/x86/freestanding/bits.h b/arch/x86/include/libucontext/bits.h similarity index 100% rename from arch/x86/freestanding/bits.h rename to arch/x86/include/libucontext/bits.h diff --git a/arch/x86_64/freestanding/bits.h b/arch/x86_64/include/libucontext/bits.h similarity index 100% rename from arch/x86_64/freestanding/bits.h rename to arch/x86_64/include/libucontext/bits.h diff --git a/meson.build b/meson.build index 948b071..e863780 100644 --- a/meson.build +++ b/meson.build @@ -70,9 +70,11 @@ if freestanding build_args += '-DFREESTANDING' build_posix = false export_unprefixed = false - project_headers += ['arch' / cpu / 'freestanding/bits.h'] + project_headers += ['arch' / cpu / 'include/libucontext/bits.h'] + project_includes += ['arch' / cpu / 'include'] else - project_headers += ['arch/common/bits.h'] + project_headers += ['arch/common/include/libucontext/bits.h'] + project_includes += ['arch/common/include'] endif if export_unprefixed From 5d66c194a7dc56a1b1e9552e69c45fcdd4808804 Mon Sep 17 00:00:00 2001 From: osy <50960678+osy@users.noreply.github.com> Date: Mon, 8 Mar 2021 14:02:58 -0800 Subject: [PATCH 2/3] x86_64: define reg names if not defined _GNU_SOURCE can be defined without defining REG_X macros so it is not a good indicator. (QEMU for example defines it in the build system.) x86 and x86_64 should have consistant looking defs.h so ifndef guards are added to each REG_X definition. --- arch/x86/defs.h | 2 -- arch/x86_64/defs.h | 68 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/arch/x86/defs.h b/arch/x86/defs.h index 0bf73ba..9370869 100644 --- a/arch/x86/defs.h +++ b/arch/x86/defs.h @@ -1,7 +1,6 @@ #ifndef __ARCH_X86_DEFS_H #define __ARCH_X86_DEFS_H -#ifndef _GNU_SOURCE #ifndef REG_GS # define REG_GS (0) #endif @@ -53,7 +52,6 @@ #ifndef REG_EIP # define REG_EIP (14) #endif -#endif #define REG_SZ (4) diff --git a/arch/x86_64/defs.h b/arch/x86_64/defs.h index c5fd126..1ebf3a9 100644 --- a/arch/x86_64/defs.h +++ b/arch/x86_64/defs.h @@ -1,29 +1,95 @@ #ifndef __ARCH_X86_64_DEFS_H #define __ARCH_X86_64_DEFS_H -#ifndef _GNU_SOURCE +#ifndef REG_R8 # define REG_R8 (0) +#endif + +#ifndef REG_R9 # define REG_R9 (1) +#endif + +#ifndef REG_R10 # define REG_R10 (2) +#endif + +#ifndef REG_R11 # define REG_R11 (3) +#endif + +#ifndef REG_R12 # define REG_R12 (4) +#endif + +#ifndef REG_R13 # define REG_R13 (5) +#endif + +#ifndef REG_R14 # define REG_R14 (6) +#endif + +#ifndef REG_R15 # define REG_R15 (7) +#endif + +#ifndef REG_RDI # define REG_RDI (8) +#endif + +#ifndef REG_RSI # define REG_RSI (9) +#endif + +#ifndef REG_RBP # define REG_RBP (10) +#endif + +#ifndef REG_RBX # define REG_RBX (11) +#endif + +#ifndef REG_RDX # define REG_RDX (12) +#endif + +#ifndef REG_RAX # define REG_RAX (13) +#endif + +#ifndef REG_RCX # define REG_RCX (14) +#endif + +#ifndef REG_RSP # define REG_RSP (15) +#endif + +#ifndef REG_RIP # define REG_RIP (16) +#endif + +#ifndef REG_EFL # define REG_EFL (17) +#endif + +#ifndef REG_CSGSFS # define REG_CSGSFS (18) +#endif + +#ifndef REG_ERR # define REG_ERR (19) +#endif + +#ifndef REG_TRAPNO # define REG_TRAPNO (20) +#endif + +#ifndef REG_OLDMASK # define REG_OLDMASK (21) +#endif + +#ifndef REG_CR2 # define REG_CR2 (22) #endif From 9b1d8f01a6e99166f9808c79966abe10786de8b6 Mon Sep 17 00:00:00 2001 From: osy <50960678+osy@users.noreply.github.com> Date: Sun, 3 Jan 2021 21:24:24 -0800 Subject: [PATCH 3/3] aarch64: remove usage of FUNC macro for clang Clang's arm64 assembler does not support multiple directives on a single line. C macros ALWAYS expand to a single line. --- arch/aarch64/getcontext.S | 6 +++++- arch/aarch64/setcontext.S | 6 +++++- arch/aarch64/swapcontext.S | 6 +++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/arch/aarch64/getcontext.S b/arch/aarch64/getcontext.S index 2a1c4d7..1098706 100644 --- a/arch/aarch64/getcontext.S +++ b/arch/aarch64/getcontext.S @@ -15,7 +15,11 @@ ALIAS(getcontext, libucontext_getcontext) ALIAS(__getcontext, libucontext_getcontext) -FUNC(libucontext_getcontext) + .global PROC_NAME(libucontext_getcontext); + .align 2; + TYPE(libucontext_getcontext) + ENT(libucontext_getcontext) +PROC_NAME(libucontext_getcontext): str xzr, [x0, #REG_OFFSET(0)] /* save GPRs */ diff --git a/arch/aarch64/setcontext.S b/arch/aarch64/setcontext.S index 994652b..3d455a5 100644 --- a/arch/aarch64/setcontext.S +++ b/arch/aarch64/setcontext.S @@ -15,7 +15,11 @@ ALIAS(setcontext, libucontext_setcontext) ALIAS(__setcontext, libucontext_setcontext) -FUNC(libucontext_setcontext) + .global PROC_NAME(libucontext_setcontext); + .align 2; + TYPE(libucontext_setcontext) + ENT(libucontext_setcontext) +PROC_NAME(libucontext_setcontext): /* restore GPRs */ ldp x18, x19, [x0, #REG_OFFSET(18)] ldp x20, x21, [x0, #REG_OFFSET(20)] diff --git a/arch/aarch64/swapcontext.S b/arch/aarch64/swapcontext.S index d5bad2c..fddfa41 100644 --- a/arch/aarch64/swapcontext.S +++ b/arch/aarch64/swapcontext.S @@ -15,7 +15,11 @@ ALIAS(swapcontext, libucontext_swapcontext) ALIAS(__swapcontext, libucontext_swapcontext) -FUNC(libucontext_swapcontext) + .global PROC_NAME(libucontext_swapcontext); + .align 2; + TYPE(libucontext_swapcontext) + ENT(libucontext_swapcontext) +PROC_NAME(libucontext_swapcontext): str xzr, [x0, #REG_OFFSET(0)] /* save GPRs */