Skip to content

Commit 3fb077b

Browse files
committed
AppleM1: Support Apple Silicon M1(aarch64), #30
1 parent 6610914 commit 3fb077b

File tree

14 files changed

+284
-13
lines changed

14 files changed

+284
-13
lines changed

Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,10 @@ EXTRA_OBJS = $(TARGETDIR)/md_darwin.o
100100
LD = cc
101101
SFLAGS = -fPIC -fno-common
102102
DSO_SUFFIX = dylib
103-
CFLAGS += -arch x86_64
104-
LDFLAGS += -arch x86_64
103+
CPU_ARCHS = $(shell g++ -dM -E - </dev/null |grep -q '__x86_64' && echo x86_64)
104+
CPU_ARCHS += $(shell g++ -dM -E - </dev/null |grep -q '__aarch64' && echo arm64)
105+
CFLAGS += -arch $(CPU_ARCHS)
106+
LDFLAGS += -arch $(CPU_ARCHS)
105107
LDFLAGS += -dynamiclib -install_name /sw/lib/libst.$(MAJOR).$(DSO_SUFFIX) -compatibility_version $(MAJOR) -current_version $(VERSION)
106108
OTHER_FLAGS = -Wall
107109
DEFINES += -DMD_HAVE_KQUEUE -DMD_HAVE_SELECT

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ The branch [srs](https://github.com/ossrs/state-threads/tree/srs) will be patche
113113
- [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).
114114
- [x] RISCV: Support RISCV for RISCV CPU, [#24](https://github.com/ossrs/state-threads/pull/28).
115115
- [x] MIPS: Support Linux/MIPS64 for loongson 3A4000/3B3000, [#21](https://github.com/ossrs/state-threads/pull/21).
116+
- [x] AppleM1: Support Apple Silicon M1(aarch64), [#30](https://github.com/ossrs/state-threads/issues/30).
116117
- [ ] IDE: Support CLion for debugging and learning.
117118
- [ ] System: Support sendmmsg for UDP, [#12](https://github.com/ossrs/state-threads/issues/12).
118119

md_darwin.S

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,127 @@
7474

7575
/****************************************************************/
7676

77-
#endif
77+
78+
79+
80+
81+
82+
83+
84+
85+
86+
87+
#elif defined(__aarch64__)
88+
89+
/****************************************************************/
90+
/* See https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms */
91+
/* See https://developer.arm.com/documentation/102374/0100/Function-calls */
92+
/* See https://developer.arm.com/documentation/102374/0100/Procedure-Call-Standard */
93+
/* See https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst#machine-registers */
94+
/* See https://wiki.cdot.senecacollege.ca/wiki/AArch64_Register_and_Instruction_Quick_Start */
95+
/*
96+
* See setjmp.h of Darwin.
97+
*
98+
* _JBLEN is the number of ints required to save the following:
99+
* r21-r29, sp, fp, lr == 12 registers, 8 bytes each. d8-d15
100+
* are another 8 registers, each 8 bytes long. (aapcs64 specifies
101+
* that only 64-bit versions of FP registers need to be saved).
102+
* Finally, two 8-byte fields for signal handling purposes.
103+
*/
104+
105+
/* The called routine is expected to preserve r19-r28 *** These registers are generally
106+
safe to use in your program. */
107+
#define JB_X19 0
108+
#define JB_X20 1
109+
#define JB_X21 2
110+
#define JB_X22 3
111+
#define JB_X23 4
112+
#define JB_X24 5
113+
#define JB_X25 6
114+
#define JB_X26 7
115+
#define JB_X27 8
116+
#define JB_X28 9
117+
/* r29 and r30 are used as the frame register and link register (avoid) */
118+
#define JB_X29 10
119+
#define JB_LR 11
120+
/* Register '31' is one of two registers depending on the instruction context:
121+
For instructions dealing with the stack, it is the stack pointer, named rsp */
122+
#define JB_SP 13
123+
124+
/* FP registers */
125+
#define JB_D8 14
126+
#define JB_D9 15
127+
#define JB_D10 16
128+
#define JB_D11 17
129+
#define JB_D12 18
130+
#define JB_D13 19
131+
#define JB_D14 20
132+
#define JB_D15 21
133+
134+
.file "md.S"
135+
.text
136+
137+
/* _st_md_cxt_save(__jmp_buf env) */
138+
.globl __st_md_cxt_save
139+
.align 4
140+
__st_md_cxt_save:
141+
stp x19, x20, [x0, #JB_X19<<3]
142+
stp x21, x22, [x0, #JB_X21<<3]
143+
stp x23, x24, [x0, #JB_X23<<3]
144+
stp x25, x26, [x0, #JB_X25<<3]
145+
stp x27, x28, [x0, #JB_X27<<3]
146+
stp x29, x30, [x0, #JB_X29<<3]
147+
148+
stp d8, d9, [x0, #JB_D8<<3]
149+
stp d10, d11, [x0, #JB_D10<<3]
150+
stp d12, d13, [x0, #JB_D12<<3]
151+
stp d14, d15, [x0, #JB_D14<<3]
152+
mov x2, sp
153+
str x2, [x0, #JB_SP<<3]
154+
155+
mov x0, #0
156+
ret
157+
158+
/****************************************************************/
159+
160+
/* _st_md_cxt_restore(__jmp_buf env, int val) */
161+
.globl __st_md_cxt_restore
162+
.align 4
163+
__st_md_cxt_restore:
164+
ldp x19, x20, [x0, #JB_X19<<3]
165+
ldp x21, x22, [x0, #JB_X21<<3]
166+
ldp x23, x24, [x0, #JB_X23<<3]
167+
ldp x25, x26, [x0, #JB_X25<<3]
168+
ldp x27, x28, [x0, #JB_X27<<3]
169+
170+
ldp x29, x30, [x0, #JB_X29<<3]
171+
172+
ldp d8, d9, [x0, #JB_D8<<3]
173+
ldp d10, d11, [x0, #JB_D10<<3]
174+
ldp d12, d13, [x0, #JB_D12<<3]
175+
ldp d14, d15, [x0, #JB_D14<<3]
176+
177+
ldr x5, [x0, #JB_SP<<3]
178+
mov sp, x5
179+
180+
cmp x1, #0
181+
mov x0, #1
182+
csel x0, x1, x0, ne
183+
/* Use br instead of ret because ret is guaranteed to mispredict */
184+
br x30
185+
186+
/****************************************************************/
187+
188+
189+
190+
191+
192+
193+
194+
195+
196+
78197

79198
#endif
199+
200+
#endif

md_linux.S

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,15 @@
175175
#elif defined(__aarch64__)
176176

177177
/****************************************************************/
178-
/* https://github.com/ossrs/srs/issues/1282#issuecomment-445539513 */
179-
178+
/* See https://developer.arm.com/documentation/102374/0100/Function-calls */
179+
/* See https://developer.arm.com/documentation/102374/0100/Procedure-Call-Standard */
180+
/* See https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst#machine-registers */
181+
/* See https://wiki.cdot.senecacollege.ca/wiki/AArch64_Register_and_Instruction_Quick_Start */
182+
/* See https://chromium.googlesource.com/native_client/nacl-glibc/+/glibc-2.21/sysdeps/aarch64/__longjmp.S */
183+
/* See https://chromium.googlesource.com/native_client/nacl-glibc/+/glibc-2.21/sysdeps/aarch64/setjmp.S */
184+
185+
/* The called routine is expected to preserve r19-r28 *** These registers are generally
186+
safe to use in your program. */
180187
#define JB_X19 0
181188
#define JB_X20 1
182189
#define JB_X21 2
@@ -187,10 +194,14 @@
187194
#define JB_X26 7
188195
#define JB_X27 8
189196
#define JB_X28 9
197+
/* r29 and r30 are used as the frame register and link register (avoid) */
190198
#define JB_X29 10
191199
#define JB_LR 11
200+
/* Register '31' is one of two registers depending on the instruction context:
201+
For instructions dealing with the stack, it is the stack pointer, named rsp */
192202
#define JB_SP 13
193203

204+
/* FP registers */
194205
#define JB_D8 14
195206
#define JB_D9 15
196207
#define JB_D10 16

tools/helloworld/Makefile

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,22 @@
33
LDLIBS=../../obj/libst.a
44
CFLAGS=-g -O0 -I../../obj
55

6-
./helloworld: helloworld.c $(LDLIBS)
6+
OS_NAME = $(shell uname -s)
7+
ST_TARGET = linux-debug
8+
ifeq ($(OS_NAME), Darwin)
9+
ST_TARGET = darwin-debug
10+
CPU_ARCHS = $(shell g++ -dM -E - </dev/null |grep -q '__x86_64' && echo x86_64)
11+
CPU_ARCHS += $(shell g++ -dM -E - </dev/null |grep -q '__aarch64' && echo arm64)
12+
CFLAGS += -arch $(CPU_ARCHS)
13+
endif
14+
15+
./helloworld: helloworld.c $(LDLIBS)
716
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -Wall -o helloworld helloworld.c $(LDLIBS)
817

918
clean:
10-
rm -f helloworld
19+
cd ../.. && make clean
20+
rm -rf helloworld helloworld.dSYM
21+
22+
$(LDLIBS):
23+
cd ../.. && make $(ST_TARGET)
1124

tools/jmpbuf/Makefile

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
1-
.PHONY: default clean pre
1+
.PHONY: default clean
22

33
CFLAGS=-g -O0
44

5-
default: ./jmpbuf pre
5+
OS_NAME = $(shell uname -s)
6+
ST_TARGET = linux-debug
7+
ifeq ($(OS_NAME), Darwin)
8+
ST_TARGET = darwin-debug
9+
CPU_ARCHS = $(shell g++ -dM -E - </dev/null |grep -q '__x86_64' && echo x86_64)
10+
CPU_ARCHS += $(shell g++ -dM -E - </dev/null |grep -q '__aarch64' && echo arm64)
11+
CFLAGS += -arch $(CPU_ARCHS)
12+
endif
13+
14+
default: ./jmpbuf ./jmpbuf.E.c
615

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

10-
pre: jmpbuf.c
19+
./jmpbuf.E.c: jmpbuf.c
1120
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -Wall -E -o jmpbuf.E.c $^ $(LDLIBS)
1221

1322
clean:
14-
rm -f jmpbuf jmpbuf.E.c
23+
rm -rf jmpbuf jmpbuf.E.c jmpbuf.dSYM
1524

tools/pcs/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pcs
2+

tools/pcs/Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.PHONY: clean
2+
3+
CFLAGS=-g -O0
4+
5+
OS_NAME = $(shell uname -s)
6+
ST_TARGET = linux-debug
7+
ifeq ($(OS_NAME), Darwin)
8+
ST_TARGET = darwin-debug
9+
CPU_ARCHS = $(shell g++ -dM -E - </dev/null |grep -q '__x86_64' && echo x86_64)
10+
CPU_ARCHS += $(shell g++ -dM -E - </dev/null |grep -q '__aarch64' && echo arm64)
11+
CFLAGS += -arch $(CPU_ARCHS)
12+
endif
13+
14+
./pcs: pcs.c
15+
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -Wall -o $@ $^ $(LDLIBS)
16+
17+
clean:
18+
rm -rf pcs pcs.dSYM
19+

tools/pcs/pcs.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* SPDX-License-Identifier: MIT */
2+
/* Copyright (c) 2022 Winlin */
3+
4+
void foo() {
5+
}
6+
7+
void foo2(char a) {
8+
}
9+
10+
void foo3(int a) {
11+
}
12+
13+
void foo4(long a) {
14+
}
15+
16+
void foo5(long long a) {
17+
}
18+
19+
long foo6(long a) {
20+
return a + 1;
21+
}
22+
23+
int main(int argc, char** argv)
24+
{
25+
foo();
26+
foo2('s');
27+
foo3(0x7);
28+
foo4(0x7);
29+
foo5(0x7);
30+
foo6(0x7);
31+
return 0;
32+
}
33+

tools/porting/Makefile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,18 @@
22

33
CFLAGS=-g -O0
44

5+
OS_NAME = $(shell uname -s)
6+
ST_TARGET = linux-debug
7+
ifeq ($(OS_NAME), Darwin)
8+
ST_TARGET = darwin-debug
9+
CPU_ARCHS = $(shell g++ -dM -E - </dev/null |grep -q '__x86_64' && echo x86_64)
10+
CPU_ARCHS += $(shell g++ -dM -E - </dev/null |grep -q '__aarch64' && echo arm64)
11+
CFLAGS += -arch $(CPU_ARCHS)
12+
endif
13+
514
./porting: porting.c
615
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -Wall -o $@ $^ $(LDLIBS)
716

817
clean:
9-
rm -f porting
18+
rm -rf porting porting.dSYM
1019

tools/stack/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
stack
2+

tools/stack/Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.PHONY: clean
2+
3+
CFLAGS=-g -O0
4+
5+
OS_NAME = $(shell uname -s)
6+
ST_TARGET = linux-debug
7+
ifeq ($(OS_NAME), Darwin)
8+
ST_TARGET = darwin-debug
9+
CPU_ARCHS = $(shell g++ -dM -E - </dev/null |grep -q '__x86_64' && echo x86_64)
10+
CPU_ARCHS += $(shell g++ -dM -E - </dev/null |grep -q '__aarch64' && echo arm64)
11+
CFLAGS += -arch $(CPU_ARCHS)
12+
endif
13+
14+
./stack: stack.c
15+
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -Wall -o $@ $^ $(LDLIBS)
16+
17+
clean:
18+
rm -rf stack stack.dSYM
19+

tools/stack/stack.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* SPDX-License-Identifier: MIT */
2+
/* Copyright (c) 2022 Winlin */
3+
4+
long foo() {
5+
char c;
6+
int i;
7+
long l;
8+
long long ll;
9+
return c + i + l + ll;
10+
}
11+
12+
int main(int argc, char** argv)
13+
{
14+
foo();
15+
return 0;
16+
}
17+

tools/verify/Makefile

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,22 @@
33
LDLIBS=../../obj/libst.a
44
CFLAGS=-g -O0
55

6+
OS_NAME = $(shell uname -s)
7+
ST_TARGET = linux-debug
8+
ifeq ($(OS_NAME), Darwin)
9+
ST_TARGET = darwin-debug
10+
CPU_ARCHS = $(shell g++ -dM -E - </dev/null |grep -q '__x86_64' && echo x86_64)
11+
CPU_ARCHS += $(shell g++ -dM -E - </dev/null |grep -q '__aarch64' && echo arm64)
12+
CFLAGS += -arch $(CPU_ARCHS)
13+
endif
14+
615
./verify: verify.c $(LDLIBS)
716
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -Wall -o verify verify.c $(LDLIBS)
817

918
clean:
10-
rm -f verify
19+
cd ../.. && make clean
20+
rm -rf verify verify.dSYM
21+
22+
$(LDLIBS):
23+
cd ../.. && make $(ST_TARGET)
1124

0 commit comments

Comments
 (0)