Skip to content

Commit

Permalink
AppleM1: Support Apple Silicon M1(aarch64), #30
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Aug 14, 2022
1 parent 6610914 commit 3fb077b
Show file tree
Hide file tree
Showing 14 changed files with 284 additions and 13 deletions.
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ EXTRA_OBJS = $(TARGETDIR)/md_darwin.o
LD = cc
SFLAGS = -fPIC -fno-common
DSO_SUFFIX = dylib
CFLAGS += -arch x86_64
LDFLAGS += -arch x86_64
CPU_ARCHS = $(shell g++ -dM -E - </dev/null |grep -q '__x86_64' && echo x86_64)
CPU_ARCHS += $(shell g++ -dM -E - </dev/null |grep -q '__aarch64' && echo arm64)
CFLAGS += -arch $(CPU_ARCHS)
LDFLAGS += -arch $(CPU_ARCHS)
LDFLAGS += -dynamiclib -install_name /sw/lib/libst.$(MAJOR).$(DSO_SUFFIX) -compatibility_version $(MAJOR) -current_version $(VERSION)
OTHER_FLAGS = -Wall
DEFINES += -DMD_HAVE_KQUEUE -DMD_HAVE_SELECT
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ The branch [srs](https://github.com/ossrs/state-threads/tree/srs) will be patche
- [x] System: Support Multiple Threads for Linux and Darwin. [#19](https://github.com/ossrs/state-threads/issues/19), [srs#2188](https://github.com/ossrs/srs/issues/2188).
- [x] RISCV: Support RISCV for RISCV CPU, [#24](https://github.com/ossrs/state-threads/pull/28).
- [x] MIPS: Support Linux/MIPS64 for loongson 3A4000/3B3000, [#21](https://github.com/ossrs/state-threads/pull/21).
- [x] AppleM1: Support Apple Silicon M1(aarch64), [#30](https://github.com/ossrs/state-threads/issues/30).
- [ ] IDE: Support CLion for debugging and learning.
- [ ] System: Support sendmmsg for UDP, [#12](https://github.com/ossrs/state-threads/issues/12).

Expand Down
123 changes: 122 additions & 1 deletion md_darwin.S
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,127 @@

/****************************************************************/

#endif










#elif defined(__aarch64__)

/****************************************************************/
/* See https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms */
/* See https://developer.arm.com/documentation/102374/0100/Function-calls */
/* See https://developer.arm.com/documentation/102374/0100/Procedure-Call-Standard */
/* See https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst#machine-registers */
/* See https://wiki.cdot.senecacollege.ca/wiki/AArch64_Register_and_Instruction_Quick_Start */
/*
* See setjmp.h of Darwin.
*
* _JBLEN is the number of ints required to save the following:
* r21-r29, sp, fp, lr == 12 registers, 8 bytes each. d8-d15
* are another 8 registers, each 8 bytes long. (aapcs64 specifies
* that only 64-bit versions of FP registers need to be saved).
* Finally, two 8-byte fields for signal handling purposes.
*/

/* The called routine is expected to preserve r19-r28 *** These registers are generally
safe to use in your program. */
#define JB_X19 0
#define JB_X20 1
#define JB_X21 2
#define JB_X22 3
#define JB_X23 4
#define JB_X24 5
#define JB_X25 6
#define JB_X26 7
#define JB_X27 8
#define JB_X28 9
/* r29 and r30 are used as the frame register and link register (avoid) */
#define JB_X29 10
#define JB_LR 11
/* Register '31' is one of two registers depending on the instruction context:
For instructions dealing with the stack, it is the stack pointer, named rsp */
#define JB_SP 13

/* FP registers */
#define JB_D8 14
#define JB_D9 15
#define JB_D10 16
#define JB_D11 17
#define JB_D12 18
#define JB_D13 19
#define JB_D14 20
#define JB_D15 21

.file "md.S"
.text

/* _st_md_cxt_save(__jmp_buf env) */
.globl __st_md_cxt_save
.align 4
__st_md_cxt_save:
stp x19, x20, [x0, #JB_X19<<3]
stp x21, x22, [x0, #JB_X21<<3]
stp x23, x24, [x0, #JB_X23<<3]
stp x25, x26, [x0, #JB_X25<<3]
stp x27, x28, [x0, #JB_X27<<3]
stp x29, x30, [x0, #JB_X29<<3]

stp d8, d9, [x0, #JB_D8<<3]
stp d10, d11, [x0, #JB_D10<<3]
stp d12, d13, [x0, #JB_D12<<3]
stp d14, d15, [x0, #JB_D14<<3]
mov x2, sp
str x2, [x0, #JB_SP<<3]

mov x0, #0
ret

/****************************************************************/

/* _st_md_cxt_restore(__jmp_buf env, int val) */
.globl __st_md_cxt_restore
.align 4
__st_md_cxt_restore:
ldp x19, x20, [x0, #JB_X19<<3]
ldp x21, x22, [x0, #JB_X21<<3]
ldp x23, x24, [x0, #JB_X23<<3]
ldp x25, x26, [x0, #JB_X25<<3]
ldp x27, x28, [x0, #JB_X27<<3]

ldp x29, x30, [x0, #JB_X29<<3]

ldp d8, d9, [x0, #JB_D8<<3]
ldp d10, d11, [x0, #JB_D10<<3]
ldp d12, d13, [x0, #JB_D12<<3]
ldp d14, d15, [x0, #JB_D14<<3]

ldr x5, [x0, #JB_SP<<3]
mov sp, x5

cmp x1, #0
mov x0, #1
csel x0, x1, x0, ne
/* Use br instead of ret because ret is guaranteed to mispredict */
br x30

/****************************************************************/











#endif

#endif
15 changes: 13 additions & 2 deletions md_linux.S
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,15 @@
#elif defined(__aarch64__)

/****************************************************************/
/* https://github.com/ossrs/srs/issues/1282#issuecomment-445539513 */

/* See https://developer.arm.com/documentation/102374/0100/Function-calls */
/* See https://developer.arm.com/documentation/102374/0100/Procedure-Call-Standard */
/* See https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst#machine-registers */
/* See https://wiki.cdot.senecacollege.ca/wiki/AArch64_Register_and_Instruction_Quick_Start */
/* See https://chromium.googlesource.com/native_client/nacl-glibc/+/glibc-2.21/sysdeps/aarch64/__longjmp.S */
/* See https://chromium.googlesource.com/native_client/nacl-glibc/+/glibc-2.21/sysdeps/aarch64/setjmp.S */

/* The called routine is expected to preserve r19-r28 *** These registers are generally
safe to use in your program. */
#define JB_X19 0
#define JB_X20 1
#define JB_X21 2
Expand All @@ -187,10 +194,14 @@
#define JB_X26 7
#define JB_X27 8
#define JB_X28 9
/* r29 and r30 are used as the frame register and link register (avoid) */
#define JB_X29 10
#define JB_LR 11
/* Register '31' is one of two registers depending on the instruction context:
For instructions dealing with the stack, it is the stack pointer, named rsp */
#define JB_SP 13

/* FP registers */
#define JB_D8 14
#define JB_D9 15
#define JB_D10 16
Expand Down
17 changes: 15 additions & 2 deletions tools/helloworld/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,22 @@
LDLIBS=../../obj/libst.a
CFLAGS=-g -O0 -I../../obj

./helloworld: helloworld.c $(LDLIBS)
OS_NAME = $(shell uname -s)
ST_TARGET = linux-debug
ifeq ($(OS_NAME), Darwin)
ST_TARGET = darwin-debug
CPU_ARCHS = $(shell g++ -dM -E - </dev/null |grep -q '__x86_64' && echo x86_64)
CPU_ARCHS += $(shell g++ -dM -E - </dev/null |grep -q '__aarch64' && echo arm64)
CFLAGS += -arch $(CPU_ARCHS)
endif

./helloworld: helloworld.c $(LDLIBS)
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -Wall -o helloworld helloworld.c $(LDLIBS)

clean:
rm -f helloworld
cd ../.. && make clean
rm -rf helloworld helloworld.dSYM

$(LDLIBS):
cd ../.. && make $(ST_TARGET)

17 changes: 13 additions & 4 deletions tools/jmpbuf/Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
.PHONY: default clean pre
.PHONY: default clean

CFLAGS=-g -O0

default: ./jmpbuf pre
OS_NAME = $(shell uname -s)
ST_TARGET = linux-debug
ifeq ($(OS_NAME), Darwin)
ST_TARGET = darwin-debug
CPU_ARCHS = $(shell g++ -dM -E - </dev/null |grep -q '__x86_64' && echo x86_64)
CPU_ARCHS += $(shell g++ -dM -E - </dev/null |grep -q '__aarch64' && echo arm64)
CFLAGS += -arch $(CPU_ARCHS)
endif

default: ./jmpbuf ./jmpbuf.E.c

./jmpbuf: jmpbuf.c
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -Wall -o $@ $^ $(LDLIBS)

pre: jmpbuf.c
./jmpbuf.E.c: jmpbuf.c
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -Wall -E -o jmpbuf.E.c $^ $(LDLIBS)

clean:
rm -f jmpbuf jmpbuf.E.c
rm -rf jmpbuf jmpbuf.E.c jmpbuf.dSYM

2 changes: 2 additions & 0 deletions tools/pcs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pcs

19 changes: 19 additions & 0 deletions tools/pcs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.PHONY: clean

CFLAGS=-g -O0

OS_NAME = $(shell uname -s)
ST_TARGET = linux-debug
ifeq ($(OS_NAME), Darwin)
ST_TARGET = darwin-debug
CPU_ARCHS = $(shell g++ -dM -E - </dev/null |grep -q '__x86_64' && echo x86_64)
CPU_ARCHS += $(shell g++ -dM -E - </dev/null |grep -q '__aarch64' && echo arm64)
CFLAGS += -arch $(CPU_ARCHS)
endif

./pcs: pcs.c
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -Wall -o $@ $^ $(LDLIBS)

clean:
rm -rf pcs pcs.dSYM

33 changes: 33 additions & 0 deletions tools/pcs/pcs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* SPDX-License-Identifier: MIT */
/* Copyright (c) 2022 Winlin */

void foo() {
}

void foo2(char a) {
}

void foo3(int a) {
}

void foo4(long a) {
}

void foo5(long long a) {
}

long foo6(long a) {
return a + 1;
}

int main(int argc, char** argv)
{
foo();
foo2('s');
foo3(0x7);
foo4(0x7);
foo5(0x7);
foo6(0x7);
return 0;
}

11 changes: 10 additions & 1 deletion tools/porting/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@

CFLAGS=-g -O0

OS_NAME = $(shell uname -s)
ST_TARGET = linux-debug
ifeq ($(OS_NAME), Darwin)
ST_TARGET = darwin-debug
CPU_ARCHS = $(shell g++ -dM -E - </dev/null |grep -q '__x86_64' && echo x86_64)
CPU_ARCHS += $(shell g++ -dM -E - </dev/null |grep -q '__aarch64' && echo arm64)
CFLAGS += -arch $(CPU_ARCHS)
endif

./porting: porting.c
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -Wall -o $@ $^ $(LDLIBS)

clean:
rm -f porting
rm -rf porting porting.dSYM

2 changes: 2 additions & 0 deletions tools/stack/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
stack

19 changes: 19 additions & 0 deletions tools/stack/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.PHONY: clean

CFLAGS=-g -O0

OS_NAME = $(shell uname -s)
ST_TARGET = linux-debug
ifeq ($(OS_NAME), Darwin)
ST_TARGET = darwin-debug
CPU_ARCHS = $(shell g++ -dM -E - </dev/null |grep -q '__x86_64' && echo x86_64)
CPU_ARCHS += $(shell g++ -dM -E - </dev/null |grep -q '__aarch64' && echo arm64)
CFLAGS += -arch $(CPU_ARCHS)
endif

./stack: stack.c
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -Wall -o $@ $^ $(LDLIBS)

clean:
rm -rf stack stack.dSYM

17 changes: 17 additions & 0 deletions tools/stack/stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* SPDX-License-Identifier: MIT */
/* Copyright (c) 2022 Winlin */

long foo() {
char c;
int i;
long l;
long long ll;
return c + i + l + ll;
}

int main(int argc, char** argv)
{
foo();
return 0;
}

15 changes: 14 additions & 1 deletion tools/verify/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,22 @@
LDLIBS=../../obj/libst.a
CFLAGS=-g -O0

OS_NAME = $(shell uname -s)
ST_TARGET = linux-debug
ifeq ($(OS_NAME), Darwin)
ST_TARGET = darwin-debug
CPU_ARCHS = $(shell g++ -dM -E - </dev/null |grep -q '__x86_64' && echo x86_64)
CPU_ARCHS += $(shell g++ -dM -E - </dev/null |grep -q '__aarch64' && echo arm64)
CFLAGS += -arch $(CPU_ARCHS)
endif

./verify: verify.c $(LDLIBS)
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -Wall -o verify verify.c $(LDLIBS)

clean:
rm -f verify
cd ../.. && make clean
rm -rf verify verify.dSYM

$(LDLIBS):
cd ../.. && make $(ST_TARGET)

0 comments on commit 3fb077b

Please sign in to comment.