From 08c4b250306be8580942c67617520d17b066b042 Mon Sep 17 00:00:00 2001 From: Steven Liu Date: Wed, 20 Jul 2022 21:53:39 +0800 Subject: [PATCH] RISCV: Patch ST and libsrtp. #3115 --- trunk/3rdparty/libsrtp-2-fit/config.guess | 6 - .../patches/srtp/config.guess-02.patch | 7 + trunk/3rdparty/st-srs/README.md | 129 +++++++++++++----- trunk/3rdparty/st-srs/md.h | 4 + trunk/3rdparty/st-srs/md_linux.S | 76 +++++++++++ trunk/auto/depends.sh | 1 + 6 files changed, 186 insertions(+), 37 deletions(-) create mode 100644 trunk/3rdparty/patches/srtp/config.guess-02.patch diff --git a/trunk/3rdparty/libsrtp-2-fit/config.guess b/trunk/3rdparty/libsrtp-2-fit/config.guess index 5cfafce41b..c4bd827a7b 100755 --- a/trunk/3rdparty/libsrtp-2-fit/config.guess +++ b/trunk/3rdparty/libsrtp-2-fit/config.guess @@ -1032,12 +1032,6 @@ EOF ppcle:Linux:*:*) echo powerpcle-unknown-linux-${LIBC} exit ;; - riscv:Linux:*:*) - echo riscv-unknown-linux-${LIBC} - exit ;; - riscv64:Linux:*:*) - echo riscv64-unknown-linux-${LIBC} - exit ;; s390:Linux:*:* | s390x:Linux:*:*) echo ${UNAME_MACHINE}-ibm-linux-${LIBC} exit ;; diff --git a/trunk/3rdparty/patches/srtp/config.guess-02.patch b/trunk/3rdparty/patches/srtp/config.guess-02.patch new file mode 100644 index 0000000000..827e96ac22 --- /dev/null +++ b/trunk/3rdparty/patches/srtp/config.guess-02.patch @@ -0,0 +1,7 @@ +1034a1035,1040 +> riscv:Linux:*:*) +> echo riscv-unknown-linux-${LIBC} +> exit ;; +> riscv64:Linux:*:*) +> echo riscv64-unknown-linux-${LIBC} +> exit ;; diff --git a/trunk/3rdparty/st-srs/README.md b/trunk/3rdparty/st-srs/README.md index 551a02b7c2..91c2a449da 100644 --- a/trunk/3rdparty/st-srs/README.md +++ b/trunk/3rdparty/st-srs/README.md @@ -1,9 +1,9 @@ # state-threads ![](http://ossrs.net:8000/gif/v1/sls.gif?site=github.com&path=/srs/srsst) -[![](https://circleci.com/gh/ossrs/state-threads/tree/srs.svg?style=svg&circle-token=1ef1d5b5b0cde6c8c282ed856a18199f9e8f85a9)](https://circleci.com/gh/ossrs/state-threads/tree/srs) +[![](https://github.com/ossrs/state-threads/actions/workflows/test.yml/badge.svg?branch=srs)](https://github.com/ossrs/state-threads/actions?query=workflow%3ATest+branch%3Asrs) [![](https://codecov.io/gh/ossrs/state-threads/branch/srs/graph/badge.svg)](https://codecov.io/gh/ossrs/state-threads/branch/srs) -[![](https://cloud.githubusercontent.com/assets/2777660/22814959/c51cbe72-ef92-11e6-81cc-32b657b285d5.png)](https://github.com/ossrs/srs/wiki/v1_CN_Contact#wechat) +[![](https://cloud.githubusercontent.com/assets/2777660/22814959/c51cbe72-ef92-11e6-81cc-32b657b285d5.png)](https://github.com/ossrs/srs/wiki/Contact#wechat) Fork from http://sourceforge.net/projects/state-threads, patched for [SRS](https://github.com/ossrs/srs/tree/2.0release). @@ -15,7 +15,7 @@ For original ST without any changes, checkout the [ST master branch](https://git [state-threads](https://github.com/ossrs/state-threads/blob/srs/README#L68) is licenced under [MPL or GPLv2](https://github.com/ossrs/srs/wiki/LicenseMixing#state-threads). -## Usage +## Linux: Usage Get code: @@ -29,18 +29,6 @@ For Linux: make linux-debug ``` -For OSX: - -```bash -make darwin-debug -``` - -For Cygwin(Windows): - -``` -make cygwin64-debug -``` - For Linux aarch64, which fail with `Unknown CPU architecture`: ```bash @@ -61,13 +49,43 @@ Linux with valgrind and epoll: make linux-debug EXTRA_CFLAGS="-DMD_HAVE_EPOLL -DMD_VALGRIND" ``` +## Mac: Usage + +Get code: + +```bash +git clone -b srs https://github.com/ossrs/state-threads.git +``` + +For OSX: + +```bash +make darwin-debug +``` + For OSX, user must specifies the valgrind header files: ```bash make darwin-debug EXTRA_CFLAGS="-DMD_HAVE_KQUEUE -DMD_VALGRIND -I/usr/local/include" ``` -> Remark: Latest OSX does not support ST, please use docker to run ST. +> Remark: M1 is unsupported by ST, please use docker to run, please read [SRS#2747](https://github.com/ossrs/srs/issues/2747). + +## Windows: Usage + +Get code: + +```bash +git clone -b srs https://github.com/ossrs/state-threads.git +``` + +For Cygwin(Windows): + +``` +make cygwin64-debug +``` + +> Remark: Windows native build is unsupported right now. ## Branch SRS @@ -89,7 +107,10 @@ The branch [srs](https://github.com/ossrs/state-threads/tree/srs) will be patche - [x] System: Improve the performance of timer. [9fe8cfe5b](https://github.com/ossrs/state-threads/commit/9fe8cfe5b1c9741a2e671a46215184f267fba400), [7879c2b](https://github.com/ossrs/state-threads/commit/7879c2b), [387cddb](https://github.com/ossrs/state-threads/commit/387cddb) - [x] Windows: Support Windows 64bits. [#20](https://github.com/ossrs/state-threads/issues/20). - [x] MIPS: Support Linux/MIPS for OpenWRT, [#21](https://github.com/ossrs/state-threads/issues/21). -- [ ] 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] LOONGARCH: Support loongarch for loongson CPU, [#24](https://github.com/ossrs/state-threads/issues/24). +- [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). +- [ ] IDE: Support CLion for debugging and learning. - [ ] System: Support sendmmsg for UDP, [#12](https://github.com/ossrs/state-threads/issues/12). ## GDB Tools @@ -110,23 +131,51 @@ Important cli options: 1. `--track-origins= [default: no]`, Controls whether Memcheck tracks the origin of uninitialised values. By default, it does not, which means that although it can tell you that an uninitialised value is being used in a dangerous way, it cannot tell you where the uninitialised value came from. This often makes it difficult to track down the root problem. 1. `--show-reachable= , --show-possibly-lost=`, to show the using memory. -## UTest and Coverage +## Linux: UTest + +> Note: We use [Google test](https://github.com/google/googletest/releases/tag/release-1.11.0) in `utest/gtest-fit`. + +To make ST with utest and run it: + +```bash +make linux-debug-utest && ./obj/st_utest +``` + +Note that the gcc(4.8) of CentOS is too old, please use docker(`ossrs/srs:dev-gcc7`) to run: + +```bash +docker run --rm -it -v $(pwd):/state-threads -w /state-threads \ + registry.cn-hangzhou.aliyuncs.com/ossrs/srs:dev-gcc7 \ + bash -c 'make linux-debug-utest && ./obj/st_utest' +``` + +## Mac: UTest -First of all, download [google test](https://github.com/google/googletest/releases/tag/release-1.6.0) to `utest/gtest`, check by: +> Note: We use [Google test](https://github.com/google/googletest/releases/tag/release-1.11.0) in `utest/gtest-fit`. + +To make ST with utest and run it: ```bash -ls -lh utest/gtest/include/gtest/gtest.h >/dev/null && echo yes +make darwin-debug-utest && ./obj/st_utest ``` +## Linux: Coverage + +> Note: We use [Google test](https://github.com/google/googletest/releases/tag/release-1.11.0) in `utest/gtest-fit`. + To make ST with utest and run it: ```bash make linux-debug-gcov && ./obj/st_utest ``` -> For macOS: `make darwin-debug-gcov && ./obj/st_utest` +Note that the gcc(4.8) of CentOS is too old, please use docker(`ossrs/srs:dev-gcc7`) to run: -> Run utest without coverage: `make darwin-debug-utest && ./obj/st_utest` +```bash +docker run --rm -it -v $(pwd):/state-threads -w /state-threads \ + registry.cn-hangzhou.aliyuncs.com/ossrs/srs:dev-gcc7 \ + bash -c 'make linux-debug-gcov && ./obj/st_utest' +``` Then, install [gcovr](https://gcovr.com/en/stable/guide.html) for coverage: @@ -135,19 +184,29 @@ yum install -y python2-pip && pip install lxml && pip install gcovr ``` -> For macOS: `pip3 install gcovr` +Finally, run test and get the report: -Finally, run test and get the report +```bash +bash auto/coverage.sh +``` + +## Mac: Coverage + +> Note: We use [Google test](https://github.com/google/googletest/releases/tag/release-1.11.0) in `utest/gtest-fit`. + +To make ST with utest and run it: ```bash -mkdir -p coverage && -gcovr -r . -e LINUX -e DARWIN -e examples --html --html-details -o coverage/st.html && -open coverage/st.html +make darwin-debug-gcov && ./obj/st_utest ``` -> Note: We ignore `LINUX*` and `DARWIN*` which is `obj` actually. +Then, install [gcovr](https://gcovr.com/en/stable/guide.html) for coverage: + +```bash +pip install gcovr +``` -Or just run locally: +Finally, run test and get the report: ```bash bash auto/coverage.sh @@ -160,11 +219,19 @@ bash auto/coverage.sh * Programming notes: http://ossrs.github.io/state-threads/docs/notes.html * [How to porting ST to other OS/CPU?](https://github.com/ossrs/state-threads/issues/22) -* About setjmp and longjmp, read [setjmp](https://ossrs.net/wiki/images/st-setjmp.jpg). -* About the stack structure, read [stack](https://ossrs.net/wiki/images/st-stack.jpg) +* About setjmp and longjmp, read [setjmp](https://gitee.com/winlinvip/srs-wiki/raw/master/images/st-setjmp.jpg). +* About the stack structure, read [stack](https://gitee.com/winlinvip/srs-wiki/raw/master/images/st-stack.jpg) * About asm code comments, read [#91d530e](https://github.com/ossrs/state-threads/commit/91d530e#diff-ed9428b14ff6afda0e9ab04cc91d4445R25). * About the scheduler, read [#13-scheduler](https://github.com/ossrs/state-threads/issues/13#issuecomment-616025527). * About the IO event system, read [#13-IO](https://github.com/ossrs/state-threads/issues/13#issuecomment-616096568). * Code analysis, please read [#15](https://github.com/ossrs/state-threads/issues/15). +## CLion + +Use [CLion](https://www.jetbrains.com/clion/) to open directory state-threads. + +Then, open `ide/st_clion/CMakeLists.txt` and click `Load CMake project`. + +Finally, select a configuration to run or debug. + Winlin 2016 diff --git a/trunk/3rdparty/st-srs/md.h b/trunk/3rdparty/st-srs/md.h index 51e8b25752..703e783de0 100644 --- a/trunk/3rdparty/st-srs/md.h +++ b/trunk/3rdparty/st-srs/md.h @@ -186,6 +186,10 @@ /* https://github.com/ossrs/state-threads/issues/21 */ #define MD_USE_BUILTIN_SETJMP #define MD_GET_SP(_t) *((long *)&((_t)->context[0].__jb[0])) + #elif defined(__riscv) + /* https://github.com/ossrs/state-threads/pull/28 */ + #define MD_USE_BUILTIN_SETJMP + #define MD_GET_SP(_t) *((long *)&((_t)->context[0].__jmpbuf[0])) #elif defined(__loongarch__) /* https://github.com/ossrs/state-threads/issues/24 */ diff --git a/trunk/3rdparty/st-srs/md_linux.S b/trunk/3rdparty/st-srs/md_linux.S index adcee5a189..2c666183fe 100644 --- a/trunk/3rdparty/st-srs/md_linux.S +++ b/trunk/3rdparty/st-srs/md_linux.S @@ -352,6 +352,82 @@ +#elif defined(__riscv) + + /****************************************************************/ + /* + * Internal __jmp_buf layout + * riscv-asm: https://github.com/riscv/riscv-asm-manual/blob/master/riscv-asm.md + */ + #define JB_SP 0 /* A0, SP, Stack pointer */ + #define JB_RA 1 /* RA, Return address */ + #define JB_FP 2 /* FP/S0 Frame pointer */ + #define JB_S1 3 /* S1 Saved register*/ + #define JB_S2 4 /* S2-S11, Saved register */ + #define JB_S3 5 /* S2-S11, Saved register */ + #define JB_S4 6 /* S2-S11, Saved register */ + #define JB_S5 7 /* S2-S11, Saved register */ + #define JB_S6 8 /* S2-S11, Saved register */ + #define JB_S7 9 /* S2-S11, Saved register */ + #define JB_S8 10 /* S2-S11, Saved register */ + #define JB_S9 11 /* S2-S11, Saved register */ + #define JB_S10 12 /* S2-S11, Saved register */ + #define JB_S11 13 /* S2-S11, Saved register */ + + + .file "md_linux.S" + .text + + /* _st_md_cxt_save(__jmp_buf env) */ /* The env is $a0, https://en.wikipedia.org/wiki/RISC-V#Register_sets */ + .globl _st_md_cxt_save + .type _st_md_cxt_save, %function + .align 2 + _st_md_cxt_save: + sd sp, JB_SP * 8(a0) + sd ra, JB_RA * 8(a0) + sd s0, JB_FP * 8(a0) + sd s1, JB_S1 * 8(a0) + sd s2, JB_S2 * 8(a0) + sd s3, JB_S3 * 8(a0) + sd s4, JB_S4 * 8(a0) + sd s5, JB_S5 * 8(a0) + sd s6, JB_S6 * 8(a0) + sd s7, JB_S7 * 8(a0) + sd s8, JB_S8 * 8(a0) + sd s9, JB_S9 * 8(a0) + sd s10, JB_S10 * 8(a0) + sd s11, JB_S11 * 8(a0) + li a0, 0 + jr ra + .size _st_md_cxt_save, .-_st_md_cxt_save + + /****************************************************************/ + + /* _st_md_cxt_restore(__jmp_buf env, int val) */ + .globl _st_md_cxt_restore + .type _st_md_cxt_restore, %function + .align 2 + _st_md_cxt_restore: + ld sp, JB_SP * 8(a0) + ld ra, JB_RA * 8(a0) + ld s0, JB_FP * 8(a0) + ld s1, JB_S1 * 8(a0) + ld s2, JB_S2 * 8(a0) + ld s3, JB_S3 * 8(a0) + ld s4, JB_S4 * 8(a0) + ld s5, JB_S5 * 8(a0) + ld s6, JB_S6 * 8(a0) + ld s7, JB_S7 * 8(a0) + ld s8, JB_S8 * 8(a0) + ld s9, JB_S9 * 8(a0) + ld s10, JB_S10 * 8(a0) + ld s11, JB_S11 * 8(a0) + li a0, 1 + jr ra + .size _st_md_cxt_restore, .-_st_md_cxt_restore + + /****************************************************************/ + diff --git a/trunk/auto/depends.sh b/trunk/auto/depends.sh index 05c8a6f9d9..8b0480f0b1 100755 --- a/trunk/auto/depends.sh +++ b/trunk/auto/depends.sh @@ -635,6 +635,7 @@ if [[ $SRS_RTC == YES ]]; then rm -rf ${SRS_OBJS}/srtp2 && cd ${SRS_OBJS}/${SRS_PLATFORM} && rm -rf libsrtp-2-fit && cp -R ../../3rdparty/libsrtp-2-fit . && cd libsrtp-2-fit && patch -p0 crypto/math/datatypes.c ../../../3rdparty/patches/srtp/gcc10-01.patch && + patch -p0 config.guess ../../../3rdparty/patches/srtp/config.guess-02.patch && $SRTP_CONFIGURE ${SRTP_OPTIONS} --prefix=`pwd`/_release && make ${SRS_JOBS} && make install && cd .. && rm -rf srtp2 && ln -sf libsrtp-2-fit/_release srtp2