From ee123fbe2fc15d8edccc6ef7f8b144f33c5e94ef Mon Sep 17 00:00:00 2001 From: Richard Campbell Date: Thu, 29 Jul 2021 17:40:01 -0700 Subject: [PATCH 1/4] Added saving/restoring of bottom 64 bits of d8-d15 per AAPCS64 --- arch/.DS_Store | Bin 0 -> 6148 bytes arch/aarch64/.DS_Store | Bin 0 -> 6148 bytes arch/aarch64/defs.h | 1 + arch/aarch64/getcontext.S | 5 ++++- arch/aarch64/include/.DS_Store | Bin 0 -> 6148 bytes arch/aarch64/include/libucontext/bits.h | 3 ++- arch/aarch64/setcontext.S | 5 ++++- arch/aarch64/swapcontext.S | 5 +++++ 8 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 arch/.DS_Store create mode 100644 arch/aarch64/.DS_Store create mode 100644 arch/aarch64/include/.DS_Store diff --git a/arch/.DS_Store b/arch/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..97b33ea8cea4383712469820c4699fa726ea6724 GIT binary patch literal 6148 zcmeHKJE{Uf5bRb#!NkyTt{^;KJcypa3n&U2JQM_tH}YH_&FT-LtR})sq?GyxW$3Sn#*jPIr ddr{OCTjPFBYyzE*ywidF88BUFRN%iAxB*qa6s`aO literal 0 HcmV?d00001 diff --git a/arch/aarch64/.DS_Store b/arch/aarch64/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..105b30b4b44e661ca89e02fd52d46e9a2f09e4a1 GIT binary patch literal 6148 zcmeHK!Ab-%3{Bh>2Nq!u$Xc&f|ir`MOOxARg|_l>B{-R^2< zx~OYsz!`7`oB?OxhYZLC2dQR?uAKpAz!~^tK+cDNCKwHqVm>;catQ#OVmJ$IsU;*Q z7)HaS2oHoc6{x9fB?fCc#)HL0!=$L`#8!N;t^85Ea9$nrha65E6H8pY_PRHO0$^EtGHa*&IV`w%w;T!vWX=a*~i1%tSy0aw5k=vP1z8!Q_cdhH6h0why$1>Yz^Uov4sLHl+zP~Egb%kczMFs(87uJ%a|v9`TOz0>H5n- z>BPCA*RFsoaHzn*x?{=zGyIi0ANhwN-g5<9fq$j|4~uy*#imkj{jojCwE^V{MND|D rG+=0JDh6sQMn}$(a~-ttAZ?8230p&%Mf@5L^oxKB;*~4#3krMzXjneA literal 0 HcmV?d00001 diff --git a/arch/aarch64/include/libucontext/bits.h b/arch/aarch64/include/libucontext/bits.h index e249448..0bc0563 100644 --- a/arch/aarch64/include/libucontext/bits.h +++ b/arch/aarch64/include/libucontext/bits.h @@ -14,7 +14,8 @@ typedef struct sigcontext { unsigned long fault_address; unsigned long regs[31]; unsigned long sp, pc, pstate; - long double __reserved[256]; + unsigned long d8_through_d15[8]; + long double __reserved[252]; } libucontext_mcontext_t; typedef struct { diff --git a/arch/aarch64/setcontext.S b/arch/aarch64/setcontext.S index 3d455a5..cbe1ddc 100644 --- a/arch/aarch64/setcontext.S +++ b/arch/aarch64/setcontext.S @@ -33,7 +33,10 @@ PROC_NAME(libucontext_setcontext): ldr x2, [x0, #SP_OFFSET] mov sp, x2 - /* TODO: SIMD / FPRs */ + ldp d8, d9, [x0, #D8_OFFSET + 0] + ldp d10, d11, [x0, #D8_OFFSET + 16] + ldp d12, d13, [x0, #D8_OFFSET + 32] + ldp d14, d15, [x0, #D8_OFFSET + 48] /* save current program counter in link register */ ldr x16, [x0, #PC_OFFSET] diff --git a/arch/aarch64/swapcontext.S b/arch/aarch64/swapcontext.S index fddfa41..e90f7f1 100644 --- a/arch/aarch64/swapcontext.S +++ b/arch/aarch64/swapcontext.S @@ -49,6 +49,11 @@ PROC_NAME(libucontext_swapcontext): /* save pstate */ str xzr, [x0, #PSTATE_OFFSET] + stp d8, d9, [x0, #D8_OFFSET + 0] + stp d10, d11, [x0, #D8_OFFSET + 16] + stp d12, d13, [x0, #D8_OFFSET + 32] + stp d14, d15, [x0, #D8_OFFSET + 48] + /* context to swap to is in x1 so... we move to x0 and call setcontext */ /* store our link register in x28 */ mov x28, x30 -- 2.41.0 From 06303df1cdc49ac4a4468158d2daf0067c93ef4d Mon Sep 17 00:00:00 2001 From: Richard Campbell Date: Thu, 29 Jul 2021 18:45:48 -0700 Subject: [PATCH 2/4] Added saving/restoring of bottom 64 bits of d8-d15 per AAPCS64 --- arch/aarch64/defs.h | 1 + arch/aarch64/getcontext.S | 5 ++++- arch/aarch64/include/libucontext/bits.h | 3 ++- arch/aarch64/setcontext.S | 5 ++++- arch/aarch64/swapcontext.S | 5 +++++ 5 files changed, 16 insertions(+), 3 deletions(-) diff --git a/arch/aarch64/defs.h b/arch/aarch64/defs.h index ee9fc7f..1282f56 100644 --- a/arch/aarch64/defs.h +++ b/arch/aarch64/defs.h @@ -9,6 +9,7 @@ #define SP_OFFSET 432 #define PC_OFFSET 440 #define PSTATE_OFFSET 448 +#define D8_OFFSET 456 #ifndef FPSIMD_MAGIC # define FPSIMD_MAGIC 0x46508001 diff --git a/arch/aarch64/getcontext.S b/arch/aarch64/getcontext.S index 1098706..a4a8bfa 100644 --- a/arch/aarch64/getcontext.S +++ b/arch/aarch64/getcontext.S @@ -50,7 +50,10 @@ PROC_NAME(libucontext_getcontext): /* save pstate */ str xzr, [x0, #PSTATE_OFFSET] - /* TODO: SIMD / FPRs */ + stp d8, d9, [x0, #D8_OFFSET + 0] + stp d10, d11, [x0, #D8_OFFSET + 16] + stp d12, d13, [x0, #D8_OFFSET + 32] + stp d14, d15, [x0, #D8_OFFSET + 48] mov x0, #0 ret diff --git a/arch/aarch64/include/libucontext/bits.h b/arch/aarch64/include/libucontext/bits.h index e249448..0bc0563 100644 --- a/arch/aarch64/include/libucontext/bits.h +++ b/arch/aarch64/include/libucontext/bits.h @@ -14,7 +14,8 @@ typedef struct sigcontext { unsigned long fault_address; unsigned long regs[31]; unsigned long sp, pc, pstate; - long double __reserved[256]; + unsigned long d8_through_d15[8]; + long double __reserved[252]; } libucontext_mcontext_t; typedef struct { diff --git a/arch/aarch64/setcontext.S b/arch/aarch64/setcontext.S index 3d455a5..cbe1ddc 100644 --- a/arch/aarch64/setcontext.S +++ b/arch/aarch64/setcontext.S @@ -33,7 +33,10 @@ PROC_NAME(libucontext_setcontext): ldr x2, [x0, #SP_OFFSET] mov sp, x2 - /* TODO: SIMD / FPRs */ + ldp d8, d9, [x0, #D8_OFFSET + 0] + ldp d10, d11, [x0, #D8_OFFSET + 16] + ldp d12, d13, [x0, #D8_OFFSET + 32] + ldp d14, d15, [x0, #D8_OFFSET + 48] /* save current program counter in link register */ ldr x16, [x0, #PC_OFFSET] diff --git a/arch/aarch64/swapcontext.S b/arch/aarch64/swapcontext.S index fddfa41..e90f7f1 100644 --- a/arch/aarch64/swapcontext.S +++ b/arch/aarch64/swapcontext.S @@ -49,6 +49,11 @@ PROC_NAME(libucontext_swapcontext): /* save pstate */ str xzr, [x0, #PSTATE_OFFSET] + stp d8, d9, [x0, #D8_OFFSET + 0] + stp d10, d11, [x0, #D8_OFFSET + 16] + stp d12, d13, [x0, #D8_OFFSET + 32] + stp d14, d15, [x0, #D8_OFFSET + 48] + /* context to swap to is in x1 so... we move to x0 and call setcontext */ /* store our link register in x28 */ mov x28, x30 -- 2.41.0 From 1e37bf30f9927d6c55fd91a294f8695cad23eef2 Mon Sep 17 00:00:00 2001 From: Richard Campbell Date: Fri, 30 Jul 2021 12:45:12 -0700 Subject: [PATCH 3/4] Adjusted location within ucontext of saved/restored FP regs for compatibility --- arch/aarch64/defs.h | 2 +- arch/aarch64/getcontext.S | 9 +++++---- arch/aarch64/include/libucontext/bits.h | 3 +-- arch/aarch64/setcontext.S | 9 +++++---- arch/aarch64/swapcontext.S | 9 +++++---- 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/arch/aarch64/defs.h b/arch/aarch64/defs.h index 1282f56..5398adc 100644 --- a/arch/aarch64/defs.h +++ b/arch/aarch64/defs.h @@ -9,7 +9,7 @@ #define SP_OFFSET 432 #define PC_OFFSET 440 #define PSTATE_OFFSET 448 -#define D8_OFFSET 456 +#define FPSIMD_CONTEXT_OFFSET 464 #ifndef FPSIMD_MAGIC # define FPSIMD_MAGIC 0x46508001 diff --git a/arch/aarch64/getcontext.S b/arch/aarch64/getcontext.S index a4a8bfa..37390a6 100644 --- a/arch/aarch64/getcontext.S +++ b/arch/aarch64/getcontext.S @@ -50,10 +50,11 @@ PROC_NAME(libucontext_getcontext): /* save pstate */ str xzr, [x0, #PSTATE_OFFSET] - stp d8, d9, [x0, #D8_OFFSET + 0] - stp d10, d11, [x0, #D8_OFFSET + 16] - stp d12, d13, [x0, #D8_OFFSET + 32] - stp d14, d15, [x0, #D8_OFFSET + 48] + add x2, x0, #FPSIMD_CONTEXT_OFFSET + stp q8, q9, [x2, #144] + stp q10, q11, [x2, #176] + stp q12, q13, [x2, #208] + stp q14, q15, [x2, #240] mov x0, #0 ret diff --git a/arch/aarch64/include/libucontext/bits.h b/arch/aarch64/include/libucontext/bits.h index 0bc0563..e249448 100644 --- a/arch/aarch64/include/libucontext/bits.h +++ b/arch/aarch64/include/libucontext/bits.h @@ -14,8 +14,7 @@ typedef struct sigcontext { unsigned long fault_address; unsigned long regs[31]; unsigned long sp, pc, pstate; - unsigned long d8_through_d15[8]; - long double __reserved[252]; + long double __reserved[256]; } libucontext_mcontext_t; typedef struct { diff --git a/arch/aarch64/setcontext.S b/arch/aarch64/setcontext.S index cbe1ddc..f348360 100644 --- a/arch/aarch64/setcontext.S +++ b/arch/aarch64/setcontext.S @@ -33,10 +33,11 @@ PROC_NAME(libucontext_setcontext): ldr x2, [x0, #SP_OFFSET] mov sp, x2 - ldp d8, d9, [x0, #D8_OFFSET + 0] - ldp d10, d11, [x0, #D8_OFFSET + 16] - ldp d12, d13, [x0, #D8_OFFSET + 32] - ldp d14, d15, [x0, #D8_OFFSET + 48] + add x2, x0, #FPSIMD_CONTEXT_OFFSET + ldp q8, q9, [x2, #144] + ldp q10, q11, [x2, #176] + ldp q12, q13, [x2, #208] + ldp q14, q15, [x2, #240] /* save current program counter in link register */ ldr x16, [x0, #PC_OFFSET] diff --git a/arch/aarch64/swapcontext.S b/arch/aarch64/swapcontext.S index e90f7f1..fc587e3 100644 --- a/arch/aarch64/swapcontext.S +++ b/arch/aarch64/swapcontext.S @@ -49,10 +49,11 @@ PROC_NAME(libucontext_swapcontext): /* save pstate */ str xzr, [x0, #PSTATE_OFFSET] - stp d8, d9, [x0, #D8_OFFSET + 0] - stp d10, d11, [x0, #D8_OFFSET + 16] - stp d12, d13, [x0, #D8_OFFSET + 32] - stp d14, d15, [x0, #D8_OFFSET + 48] + add x2, x0, #FPSIMD_CONTEXT_OFFSET + stp q8, q9, [x2, #144] + stp q10, q11, [x2, #176] + stp q12, q13, [x2, #208] + stp q14, q15, [x2, #240] /* context to swap to is in x1 so... we move to x0 and call setcontext */ /* store our link register in x28 */ -- 2.41.0 From 6ddefbbbb420d295832c570f13e1845371622737 Mon Sep 17 00:00:00 2001 From: Torrekie Gen Date: Sat, 13 Nov 2021 19:10:25 +0800 Subject: [PATCH 4/4] Allow building on Darwin/arm64 Changes: 1. `-soname` should be `-install_name` on Darwin 2. Version suffix should placed before extension suffix (libucontext.so.1 -> libucontext.1.dylib) 3. Check for architecture specific include directory, and include it while building 4. `EXPORT_UNPREFIXED` macro shouldn't be set since Darwin don't support aliases 5. Choose correct subdir for arm64 6. Use `-dynamiclib` instead of `-shared` on Darwin for adding version info (`-current_version` and `-compatibility_version`) --- Makefile | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index cce1686..29c180c 100644 --- a/Makefile +++ b/Makefile @@ -13,13 +13,21 @@ endif ifeq ($(ARCH),$(filter $(ARCH),armv7l)) override ARCH = arm endif +ifeq ($(ARCH),$(filter $(ARCH),arm64)) + override ARCH = aarch64 +endif LIBDIR := /lib INCLUDEDIR := /usr/include PKGCONFIGDIR := /usr/lib/pkgconfig CFLAGS := -ggdb3 -O2 -Wall CPPFLAGS := -Iinclude -Iarch/${ARCH} -Iarch/common -EXPORT_UNPREFIXED := yes +ifneq ($(shell uname),Darwin) + EXPORT_UNPREFIXED := yes +else + # Darwin does not support aliases + EXPORT_UNPREFIXED := no +endif FREESTANDING := no ifeq ($(FREESTANDING),yes) @@ -34,13 +42,30 @@ endif LIBUCONTEXT_C_SRC = $(wildcard arch/${ARCH}/*.c) LIBUCONTEXT_S_SRC = $(wildcard arch/${ARCH}/*.S) +ifeq ($(shell test -d arch/${ARCH}/include; echo $?),0) + CPPFLAGS += -Iarch/${ARCH}/include +endif + LIBUCONTEXT_VERSION := $(shell head -n 1 VERSION) LIBUCONTEXT_OBJ = ${LIBUCONTEXT_C_SRC:.c=.o} ${LIBUCONTEXT_S_SRC:.S=.o} LIBUCONTEXT_SOVERSION = 1 -LIBUCONTEXT_NAME = libucontext.so +ifeq ($(shell uname),Darwin) + LIBUCONTEXT_NAME = libucontext.dylib + LIBUCONTEXT_SONAME = libucontext.${LIBUCONTEXT_SOVERSION}.dylib + LIBUCONTEXT_POSIX_NAME = libucontext_posix.dylib + LIBUCONTEXT_POSIX_SONAME = libucontext_posix.${LIBUCONTEXT_SOVERSION}.dylib + LIBUCONTEXT_LINKER_FLAGS = -Wl,-dynamiclib,-install_name,${LIBUCONTEXT_SONAME},-current_version,${LIBUCONTEXT_SOVERSION},-compatibility_version,${LIBUCONTEXT_SOVERSION} + LIBUCONTEXT_POSIX_LINKER_FLAGS = -Wl,-dynamiclib,-install_name,${LIBUCONTEXT_POSIX_SONAME},-current_version,${LIBUCONTEXT_SOVERSION},-compatibility_version,${LIBUCONTEXT_SOVERSION} +else + LIBUCONTEXT_NAME = libucontext.so + LIBUCONTEXT_SONAME = libucontext.so.${LIBUCONTEXT_SOVERSION} + LIBUCONTEXT_POSIX_NAME = libucontext_posix.so + LIBUCONTEXT_POSIX_SONAME = libucontext_posix.so.${LIBUCONTEXT_SOVERSION} + LIBUCONTEXT_LINKER_FLAGS = -Wl,-shared,-soname,${LIBUCONTEXT_SONAME} + LIBUCONTEXT_POSIX_LINKER_FLAGS = -Wl,-shared,-soname,${LIBUCONTEXT_POSIX_SONAME} +endif LIBUCONTEXT_STATIC_NAME = libucontext.a LIBUCONTEXT_PC = libucontext.pc -LIBUCONTEXT_SONAME = libucontext.so.${LIBUCONTEXT_SOVERSION} LIBUCONTEXT_PATH = ${LIBDIR}/${LIBUCONTEXT_SONAME} LIBUCONTEXT_STATIC_PATH = ${LIBDIR}/${LIBUCONTEXT_STATIC_NAME} LIBUCONTEXT_HEADERS = \ @@ -48,11 +73,9 @@ LIBUCONTEXT_HEADERS = \ include/libucontext/bits.h LIBUCONTEXT_EXAMPLES = \ examples/cooperative_threading -LIBUCONTEXT_POSIX_NAME = libucontext_posix.so LIBUCONTEXT_POSIX_STATIC_NAME = libucontext_posix.a LIBUCONTEXT_POSIX_C_SRC = libucontext_posix.c LIBUCONTEXT_POSIX_OBJ = ${LIBUCONTEXT_POSIX_C_SRC:.c=.o} -LIBUCONTEXT_POSIX_SONAME = libucontext_posix.so.${LIBUCONTEXT_SOVERSION} LIBUCONTEXT_POSIX_PATH = ${LIBDIR}/${LIBUCONTEXT_POSIX_SONAME} LIBUCONTEXT_POSIX_STATIC_PATH = ${LIBDIR}/${LIBUCONTEXT_POSIX_STATIC_NAME} @@ -64,8 +87,7 @@ endif all: ${LIBUCONTEXT_SONAME} ${LIBUCONTEXT_STATIC_NAME} ${LIBUCONTEXT_POSIX_NAME} ${LIBUCONTEXT_POSIX_STATIC_NAME} ${LIBUCONTEXT_PC} ${LIBUCONTEXT_POSIX_NAME}: ${LIBUCONTEXT_NAME} ${LIBUCONTEXT_POSIX_OBJ} - $(CC) -fPIC -o ${LIBUCONTEXT_POSIX_NAME} -Wl,-soname,${LIBUCONTEXT_POSIX_SONAME} \ - -shared ${LIBUCONTEXT_POSIX_OBJ} ${LDFLAGS} + $(CC) -fPIC -o ${LIBUCONTEXT_POSIX_NAME} ${LIBUCONTEXT_POSIX_LINKER_FLAGS} ${LIBUCONTEXT_POSIX_OBJ} ${LDFLAGS} ${LIBUCONTEXT_POSIX_STATIC_NAME}: ${LIBUCONTEXT_STATIC_NAME} ${LIBUCONTEXT_POSIX_OBJ} $(AR) rcs ${LIBUCONTEXT_POSIX_STATIC_NAME} ${LIBUCONTEXT_POSIX_OBJ} @@ -77,8 +99,7 @@ ${LIBUCONTEXT_STATIC_NAME}: ${LIBUCONTEXT_HEADERS} ${LIBUCONTEXT_OBJ} $(AR) rcs ${LIBUCONTEXT_STATIC_NAME} ${LIBUCONTEXT_OBJ} ${LIBUCONTEXT_NAME}: ${LIBUCONTEXT_HEADERS} ${LIBUCONTEXT_OBJ} - $(CC) -fPIC -o ${LIBUCONTEXT_NAME} -Wl,-soname,${LIBUCONTEXT_SONAME} \ - -shared ${LIBUCONTEXT_OBJ} ${LDFLAGS} + $(CC) -fPIC -o ${LIBUCONTEXT_NAME} ${LIBUCONTEXT_LINKER_FLAGS} ${LIBUCONTEXT_OBJ} ${LDFLAGS} ${LIBUCONTEXT_SONAME}: ${LIBUCONTEXT_NAME} ln -sf ${LIBUCONTEXT_NAME} ${LIBUCONTEXT_SONAME} -- 2.41.0