From 6b4a4cc2fdd4e0edba9758c8dff479459661e2e1 Mon Sep 17 00:00:00 2001 From: WuZheng Date: Thu, 26 Dec 2024 13:09:54 +0800 Subject: [PATCH] remove `ruxlibc` from ulibc. --- .github/workflows/build.yml | 6 +- .github/workflows/docs.yml | 2 +- .github/workflows/test.yml | 4 +- Cargo.lock | 18 +- Cargo.toml | 1 - Makefile | 10 +- README.md | 2 +- api/arceos_api/Cargo.toml | 2 + api/arceos_api/src/imp/mod.rs | 2 + .../arceos_api/src/imp/trap.rs | 15 +- api/ruxos_posix_api/Cargo.toml | 2 +- api/ruxos_posix_api/build.rs | 6 +- api/ruxos_posix_api/src/imp/fs.rs | 2 +- api/ruxos_posix_api/src/imp/mmap/api.rs | 2 +- api/ruxos_posix_api/src/imp/mmap/trap.rs | 6 +- api/ruxos_posix_api/src/imp/mmap/utils.rs | 21 +- api/ruxos_posix_api/src/imp/pipe.rs | 1 - api/ruxos_posix_api/src/imp/pthread/mod.rs | 57 +- api/ruxos_posix_api/src/imp/task.rs | 70 +- api/ruxos_posix_api/src/lib.rs | 4 +- apps/c/envtest/expect_info.out | 1 - apps/c/httpclient/features.txt | 2 + apps/c/memtest/expect_trace.out | 55 +- .../c/pthread/basic/expect_info_smp4_fifo.out | 1 - apps/c/pthread/basic/features.txt | 1 + .../parallel/expect_info_smp4_fifo.out | 26 - .../pthread/parallel/expect_info_smp4_rr.out | 27 - apps/c/pthread/parallel/features.txt | 1 + apps/c/pthread/parallel/test_cmd | 4 +- apps/c/pthread/pipe/expect_info_smp4_fifo.out | 1 - apps/c/pthread/pipe/features.txt | 1 + .../c/pthread/sleep/expect_info_smp4_fifo.out | 1 - apps/c/pthread/sleep/features.txt | 1 + apps/c/pthread/tsd/expect_info_smp4_fifo.out | 1 - apps/c/pthread/tsd/features.txt | 1 + modules/ruxdriver/src/virtio.rs | 19 +- modules/ruxfs/src/root.rs | 23 +- modules/ruxhal/Cargo.toml | 4 +- modules/ruxhal/src/arch/x86_64/mod.rs | 1 - modules/ruxhal/src/mem.rs | 2 + modules/ruxhal/src/platform/x86_pc/rtc.rs | 6 + modules/ruxmm/src/lib.rs | 3 + modules/ruxmm/src/mem.rs | 2 + modules/ruxnet/src/lwip_impl/addr.rs | 13 +- modules/ruxnet/src/lwip_impl/dns.rs | 2 +- modules/ruxnet/src/lwip_impl/driver.rs | 19 +- modules/ruxnet/src/lwip_impl/mod.rs | 4 +- modules/ruxnet/src/lwip_impl/tcp.rs | 6 +- modules/ruxnet/src/lwip_impl/udp.rs | 4 +- modules/ruxnet/src/unix.rs | 9 +- modules/ruxruntime/Cargo.toml | 20 +- .../resources/create_test_img.sh | 0 .../{ruxfs => ruxruntime}/resources/fat16.img | Bin .../tests/test_common/mod.rs | 0 .../{ruxfs => ruxruntime}/tests/test_fatfs.rs | 0 .../{ruxfs => ruxruntime}/tests/test_ramfs.rs | 1 + modules/ruxtask/Cargo.toml | 9 +- modules/ruxtask/src/api.rs | 2 +- modules/ruxtask/src/fs.rs | 73 +- modules/ruxtask/src/lib.rs | 2 +- modules/ruxtask/src/run_queue.rs | 16 +- modules/ruxtask/src/signal.rs | 7 +- modules/ruxtask/src/task.rs | 68 +- modules/ruxtask/src/vma.rs | 11 + scripts/make/build.mk | 6 +- scripts/make/build_c.mk | 4 +- scripts/make/cargo.mk | 2 +- scripts/make/features.mk | 15 +- scripts/make/test.mk | 6 +- scripts/test/app_test.sh | 2 +- ulib/axstd/Cargo.toml | 2 +- ulib/include/.gitignore | 2 + ulib/{ruxlibc => }/include/arpa/inet.h | 0 ulib/{ruxlibc => }/include/assert.h | 0 ulib/{ruxlibc => }/include/byteswap.h | 0 ulib/{ruxlibc => }/include/crypt.h | 0 ulib/{ruxlibc => }/include/ctype.h | 0 ulib/{ruxlibc => }/include/dirent.h | 0 ulib/{ruxlibc => }/include/dlfcn.h | 0 ulib/{ruxlibc => }/include/endian.h | 0 ulib/{ruxlibc => }/include/errno.h | 0 ulib/{ruxlibc => }/include/fcntl.h | 0 ulib/{ruxlibc => }/include/features.h | 0 ulib/{ruxlibc => }/include/float.h | 0 ulib/{ruxlibc => }/include/fnmatch.h | 0 ulib/{ruxlibc => }/include/glob.h | 0 ulib/{ruxlibc => }/include/grp.h | 0 ulib/{ruxlibc => }/include/inttypes.h | 0 ulib/{ruxlibc => }/include/ksigaction.h | 0 ulib/{ruxlibc => }/include/langinfo.h | 0 ulib/{ruxlibc => }/include/libgen.h | 0 ulib/{ruxlibc => }/include/limits.h | 0 ulib/{ruxlibc => }/include/locale.h | 0 ulib/{ruxlibc => }/include/malloc.h | 0 ulib/{ruxlibc => }/include/math.h | 0 ulib/{ruxlibc => }/include/memory.h | 0 ulib/{ruxlibc => }/include/netdb.h | 0 ulib/{ruxlibc => }/include/netinet/in.h | 0 ulib/{ruxlibc => }/include/netinet/tcp.h | 0 ulib/{ruxlibc => }/include/nscd.h | 0 ulib/{ruxlibc => }/include/poll.h | 0 ulib/{ruxlibc => }/include/pthread.h | 0 ulib/{ruxlibc => }/include/pwd.h | 0 ulib/{ruxlibc => }/include/regex.h | 0 ulib/{ruxlibc => }/include/sched.h | 0 ulib/{ruxlibc => }/include/semaphore.h | 0 ulib/{ruxlibc => }/include/setjmp.h | 0 ulib/{ruxlibc => }/include/signal.h | 0 ulib/{ruxlibc => }/include/stdarg.h | 0 ulib/{ruxlibc => }/include/stdbool.h | 0 ulib/{ruxlibc => }/include/stddef.h | 0 ulib/{ruxlibc => }/include/stdint.h | 0 ulib/{ruxlibc => }/include/stdio.h | 0 ulib/{ruxlibc => }/include/stdlib.h | 0 ulib/{ruxlibc => }/include/string.h | 0 ulib/{ruxlibc => }/include/strings.h | 0 ulib/{ruxlibc => }/include/sys/epoll.h | 0 ulib/{ruxlibc => }/include/sys/eventfd.h | 0 ulib/{ruxlibc => }/include/sys/file.h | 0 ulib/{ruxlibc => }/include/sys/ioctl.h | 0 ulib/{ruxlibc => }/include/sys/mman.h | 0 ulib/{ruxlibc => }/include/sys/param.h | 0 ulib/{ruxlibc => }/include/sys/prctl.h | 0 ulib/{ruxlibc => }/include/sys/random.h | 0 ulib/{ruxlibc => }/include/sys/resource.h | 0 ulib/{ruxlibc => }/include/sys/select.h | 0 ulib/{ruxlibc => }/include/sys/sendfile.h | 0 ulib/{ruxlibc => }/include/sys/socket.h | 0 ulib/{ruxlibc => }/include/sys/stat.h | 0 ulib/{ruxlibc => }/include/sys/statfs.h | 0 ulib/{ruxlibc => }/include/sys/statvfs.h | 0 ulib/{ruxlibc => }/include/sys/syscall.h | 0 ulib/{ruxlibc => }/include/sys/sysinfo.h | 0 ulib/{ruxlibc => }/include/sys/time.h | 0 ulib/{ruxlibc => }/include/sys/types.h | 0 ulib/{ruxlibc => }/include/sys/uio.h | 0 ulib/{ruxlibc => }/include/sys/un.h | 0 ulib/{ruxlibc => }/include/sys/utsname.h | 0 ulib/{ruxlibc => }/include/sys/vfs.h | 0 ulib/{ruxlibc => }/include/sys/wait.h | 0 ulib/{ruxlibc => }/include/syslog.h | 0 ulib/{ruxlibc => }/include/termios.h | 0 ulib/{ruxlibc => }/include/time.h | 0 ulib/{ruxlibc => }/include/unistd.h | 0 ulib/ruxlibc/.gitignore | 4 - ulib/ruxlibc/Cargo.toml | 67 - ulib/ruxlibc/build.rs | 33 - ulib/ruxlibc/c/assert.c | 17 - ulib/ruxlibc/c/ctype.c | 26 - ulib/ruxlibc/c/dirent.c | 107 -- ulib/ruxlibc/c/dlfcn.c | 48 - ulib/ruxlibc/c/fcntl.c | 71 - ulib/ruxlibc/c/flock.c | 18 - ulib/ruxlibc/c/fnmatch.c | 304 ---- ulib/ruxlibc/c/glob.c | 338 ---- ulib/ruxlibc/c/grp.c | 46 - ulib/ruxlibc/c/ioctl.c | 27 - ulib/ruxlibc/c/libgen.c | 42 - ulib/ruxlibc/c/libm.c | 26 - ulib/ruxlibc/c/libm.h | 242 --- ulib/ruxlibc/c/locale.c | 51 - ulib/ruxlibc/c/log.c | 404 ----- ulib/ruxlibc/c/math.c | 580 ------- ulib/ruxlibc/c/network.c | 243 --- ulib/ruxlibc/c/nscd_query.c | 103 -- ulib/ruxlibc/c/pow.c | 824 --------- ulib/ruxlibc/c/prctl.c | 19 - ulib/ruxlibc/c/printf.c | 1491 ----------------- ulib/ruxlibc/c/printf.h | 224 --- ulib/ruxlibc/c/printf_config.h | 23 - ulib/ruxlibc/c/pthread.c | 80 - ulib/ruxlibc/c/pwd.c | 52 - ulib/ruxlibc/c/resource.c | 27 - ulib/ruxlibc/c/sched.c | 24 - ulib/ruxlibc/c/select.c | 26 - ulib/ruxlibc/c/semaphore.c | 39 - ulib/ruxlibc/c/sendfile.c | 16 - ulib/ruxlibc/c/signal.c | 101 -- ulib/ruxlibc/c/socket.c | 94 -- ulib/ruxlibc/c/stat.c | 40 - ulib/ruxlibc/c/statfs.c | 18 - ulib/ruxlibc/c/stdio.c | 469 ------ ulib/ruxlibc/c/stdlib.c | 691 -------- ulib/ruxlibc/c/string.c | 473 ------ ulib/ruxlibc/c/syslog.c | 25 - ulib/ruxlibc/c/time.c | 216 --- ulib/ruxlibc/c/uio.c | 32 - ulib/ruxlibc/c/unistd.c | 236 --- ulib/ruxlibc/c/utsname.c | 18 - ulib/ruxlibc/c/wait.c | 26 - ulib/ruxlibc/ctypes.h | 11 - ulib/ruxlibc/src/env.rs | 140 -- ulib/ruxlibc/src/errno.rs | 48 - ulib/ruxlibc/src/fd_ops.rs | 63 - ulib/ruxlibc/src/fs.rs | 109 -- ulib/ruxlibc/src/io.rs | 54 - ulib/ruxlibc/src/io_mpx.rs | 76 - ulib/ruxlibc/src/lib.rs | 152 -- ulib/ruxlibc/src/malloc.rs | 65 - ulib/ruxlibc/src/mktime.rs | 67 - ulib/ruxlibc/src/mmap.rs | 64 - ulib/ruxlibc/src/net.rs | 198 --- ulib/ruxlibc/src/pipe.rs | 23 - ulib/ruxlibc/src/pthread.rs | 157 -- ulib/ruxlibc/src/rand.rs | 39 - ulib/ruxlibc/src/resource.rs | 29 - ulib/ruxlibc/src/setjmp.rs | 247 --- ulib/ruxlibc/src/signal.rs | 79 - ulib/ruxlibc/src/strftime.rs | 263 --- ulib/ruxlibc/src/string.rs | 29 - ulib/ruxlibc/src/strtod.rs | 140 -- ulib/ruxlibc/src/sys.rs | 49 - ulib/ruxlibc/src/time.rs | 87 - ulib/ruxlibc/src/unistd.rs | 87 - ulib/ruxmusl/src/aarch64/mod.rs | 4 + ulib/ruxmusl/src/aarch64/syscall_id.rs | 2 + ulib/ruxmusl/src/trap.rs | 3 + 217 files changed, 435 insertions(+), 10653 deletions(-) rename ulib/ruxlibc/src/utils.rs => api/arceos_api/src/imp/trap.rs (62%) rename modules/{ruxfs => ruxruntime}/resources/create_test_img.sh (100%) rename modules/{ruxfs => ruxruntime}/resources/fat16.img (100%) rename modules/{ruxfs => ruxruntime}/tests/test_common/mod.rs (100%) rename modules/{ruxfs => ruxruntime}/tests/test_fatfs.rs (100%) rename modules/{ruxfs => ruxruntime}/tests/test_ramfs.rs (99%) create mode 100644 ulib/include/.gitignore rename ulib/{ruxlibc => }/include/arpa/inet.h (100%) rename ulib/{ruxlibc => }/include/assert.h (100%) rename ulib/{ruxlibc => }/include/byteswap.h (100%) rename ulib/{ruxlibc => }/include/crypt.h (100%) rename ulib/{ruxlibc => }/include/ctype.h (100%) rename ulib/{ruxlibc => }/include/dirent.h (100%) rename ulib/{ruxlibc => }/include/dlfcn.h (100%) rename ulib/{ruxlibc => }/include/endian.h (100%) rename ulib/{ruxlibc => }/include/errno.h (100%) rename ulib/{ruxlibc => }/include/fcntl.h (100%) rename ulib/{ruxlibc => }/include/features.h (100%) rename ulib/{ruxlibc => }/include/float.h (100%) rename ulib/{ruxlibc => }/include/fnmatch.h (100%) rename ulib/{ruxlibc => }/include/glob.h (100%) rename ulib/{ruxlibc => }/include/grp.h (100%) rename ulib/{ruxlibc => }/include/inttypes.h (100%) rename ulib/{ruxlibc => }/include/ksigaction.h (100%) rename ulib/{ruxlibc => }/include/langinfo.h (100%) rename ulib/{ruxlibc => }/include/libgen.h (100%) rename ulib/{ruxlibc => }/include/limits.h (100%) rename ulib/{ruxlibc => }/include/locale.h (100%) rename ulib/{ruxlibc => }/include/malloc.h (100%) rename ulib/{ruxlibc => }/include/math.h (100%) rename ulib/{ruxlibc => }/include/memory.h (100%) rename ulib/{ruxlibc => }/include/netdb.h (100%) rename ulib/{ruxlibc => }/include/netinet/in.h (100%) rename ulib/{ruxlibc => }/include/netinet/tcp.h (100%) rename ulib/{ruxlibc => }/include/nscd.h (100%) rename ulib/{ruxlibc => }/include/poll.h (100%) rename ulib/{ruxlibc => }/include/pthread.h (100%) rename ulib/{ruxlibc => }/include/pwd.h (100%) rename ulib/{ruxlibc => }/include/regex.h (100%) rename ulib/{ruxlibc => }/include/sched.h (100%) rename ulib/{ruxlibc => }/include/semaphore.h (100%) rename ulib/{ruxlibc => }/include/setjmp.h (100%) rename ulib/{ruxlibc => }/include/signal.h (100%) rename ulib/{ruxlibc => }/include/stdarg.h (100%) rename ulib/{ruxlibc => }/include/stdbool.h (100%) rename ulib/{ruxlibc => }/include/stddef.h (100%) rename ulib/{ruxlibc => }/include/stdint.h (100%) rename ulib/{ruxlibc => }/include/stdio.h (100%) rename ulib/{ruxlibc => }/include/stdlib.h (100%) rename ulib/{ruxlibc => }/include/string.h (100%) rename ulib/{ruxlibc => }/include/strings.h (100%) rename ulib/{ruxlibc => }/include/sys/epoll.h (100%) rename ulib/{ruxlibc => }/include/sys/eventfd.h (100%) rename ulib/{ruxlibc => }/include/sys/file.h (100%) rename ulib/{ruxlibc => }/include/sys/ioctl.h (100%) rename ulib/{ruxlibc => }/include/sys/mman.h (100%) rename ulib/{ruxlibc => }/include/sys/param.h (100%) rename ulib/{ruxlibc => }/include/sys/prctl.h (100%) rename ulib/{ruxlibc => }/include/sys/random.h (100%) rename ulib/{ruxlibc => }/include/sys/resource.h (100%) rename ulib/{ruxlibc => }/include/sys/select.h (100%) rename ulib/{ruxlibc => }/include/sys/sendfile.h (100%) rename ulib/{ruxlibc => }/include/sys/socket.h (100%) rename ulib/{ruxlibc => }/include/sys/stat.h (100%) rename ulib/{ruxlibc => }/include/sys/statfs.h (100%) rename ulib/{ruxlibc => }/include/sys/statvfs.h (100%) rename ulib/{ruxlibc => }/include/sys/syscall.h (100%) rename ulib/{ruxlibc => }/include/sys/sysinfo.h (100%) rename ulib/{ruxlibc => }/include/sys/time.h (100%) rename ulib/{ruxlibc => }/include/sys/types.h (100%) rename ulib/{ruxlibc => }/include/sys/uio.h (100%) rename ulib/{ruxlibc => }/include/sys/un.h (100%) rename ulib/{ruxlibc => }/include/sys/utsname.h (100%) rename ulib/{ruxlibc => }/include/sys/vfs.h (100%) rename ulib/{ruxlibc => }/include/sys/wait.h (100%) rename ulib/{ruxlibc => }/include/syslog.h (100%) rename ulib/{ruxlibc => }/include/termios.h (100%) rename ulib/{ruxlibc => }/include/time.h (100%) rename ulib/{ruxlibc => }/include/unistd.h (100%) delete mode 100644 ulib/ruxlibc/.gitignore delete mode 100644 ulib/ruxlibc/Cargo.toml delete mode 100644 ulib/ruxlibc/build.rs delete mode 100644 ulib/ruxlibc/c/assert.c delete mode 100644 ulib/ruxlibc/c/ctype.c delete mode 100644 ulib/ruxlibc/c/dirent.c delete mode 100644 ulib/ruxlibc/c/dlfcn.c delete mode 100644 ulib/ruxlibc/c/fcntl.c delete mode 100644 ulib/ruxlibc/c/flock.c delete mode 100644 ulib/ruxlibc/c/fnmatch.c delete mode 100644 ulib/ruxlibc/c/glob.c delete mode 100644 ulib/ruxlibc/c/grp.c delete mode 100644 ulib/ruxlibc/c/ioctl.c delete mode 100644 ulib/ruxlibc/c/libgen.c delete mode 100644 ulib/ruxlibc/c/libm.c delete mode 100644 ulib/ruxlibc/c/libm.h delete mode 100644 ulib/ruxlibc/c/locale.c delete mode 100644 ulib/ruxlibc/c/log.c delete mode 100644 ulib/ruxlibc/c/math.c delete mode 100644 ulib/ruxlibc/c/network.c delete mode 100644 ulib/ruxlibc/c/nscd_query.c delete mode 100644 ulib/ruxlibc/c/pow.c delete mode 100644 ulib/ruxlibc/c/prctl.c delete mode 100644 ulib/ruxlibc/c/printf.c delete mode 100644 ulib/ruxlibc/c/printf.h delete mode 100644 ulib/ruxlibc/c/printf_config.h delete mode 100644 ulib/ruxlibc/c/pthread.c delete mode 100644 ulib/ruxlibc/c/pwd.c delete mode 100644 ulib/ruxlibc/c/resource.c delete mode 100644 ulib/ruxlibc/c/sched.c delete mode 100644 ulib/ruxlibc/c/select.c delete mode 100644 ulib/ruxlibc/c/semaphore.c delete mode 100644 ulib/ruxlibc/c/sendfile.c delete mode 100644 ulib/ruxlibc/c/signal.c delete mode 100644 ulib/ruxlibc/c/socket.c delete mode 100644 ulib/ruxlibc/c/stat.c delete mode 100644 ulib/ruxlibc/c/statfs.c delete mode 100644 ulib/ruxlibc/c/stdio.c delete mode 100644 ulib/ruxlibc/c/stdlib.c delete mode 100644 ulib/ruxlibc/c/string.c delete mode 100644 ulib/ruxlibc/c/syslog.c delete mode 100644 ulib/ruxlibc/c/time.c delete mode 100644 ulib/ruxlibc/c/uio.c delete mode 100644 ulib/ruxlibc/c/unistd.c delete mode 100644 ulib/ruxlibc/c/utsname.c delete mode 100644 ulib/ruxlibc/c/wait.c delete mode 100644 ulib/ruxlibc/ctypes.h delete mode 100644 ulib/ruxlibc/src/env.rs delete mode 100644 ulib/ruxlibc/src/errno.rs delete mode 100644 ulib/ruxlibc/src/fd_ops.rs delete mode 100644 ulib/ruxlibc/src/fs.rs delete mode 100644 ulib/ruxlibc/src/io.rs delete mode 100644 ulib/ruxlibc/src/io_mpx.rs delete mode 100644 ulib/ruxlibc/src/lib.rs delete mode 100644 ulib/ruxlibc/src/malloc.rs delete mode 100644 ulib/ruxlibc/src/mktime.rs delete mode 100644 ulib/ruxlibc/src/mmap.rs delete mode 100644 ulib/ruxlibc/src/net.rs delete mode 100644 ulib/ruxlibc/src/pipe.rs delete mode 100644 ulib/ruxlibc/src/pthread.rs delete mode 100644 ulib/ruxlibc/src/rand.rs delete mode 100644 ulib/ruxlibc/src/resource.rs delete mode 100644 ulib/ruxlibc/src/setjmp.rs delete mode 100644 ulib/ruxlibc/src/signal.rs delete mode 100644 ulib/ruxlibc/src/strftime.rs delete mode 100644 ulib/ruxlibc/src/string.rs delete mode 100644 ulib/ruxlibc/src/strtod.rs delete mode 100644 ulib/ruxlibc/src/sys.rs delete mode 100644 ulib/ruxlibc/src/time.rs delete mode 100644 ulib/ruxlibc/src/unistd.rs diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c68842bf9..6a9c40a02 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -7,7 +7,7 @@ env: jobs: clippy: - runs-on: ubuntu-latest + runs-on: ubuntu-22.04 strategy: fail-fast: false steps: @@ -37,7 +37,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-latest] + os: [ubuntu-22.04] arch: [x86_64, riscv64, aarch64] steps: - uses: actions/checkout@v3 @@ -94,7 +94,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-latest] + os: [ubuntu-22.04] arch: [x86_64] steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 68e607319..3e0558e18 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -7,7 +7,7 @@ env: jobs: doc: - runs-on: ubuntu-latest + runs-on: ubuntu-22.04 strategy: fail-fast: false permissions: diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b78448eb3..014375d5e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -8,7 +8,7 @@ env: jobs: unit-test: - runs-on: ubuntu-latest + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v3 with: @@ -26,7 +26,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-latest] + os: [ubuntu-22.04] arch: [x86_64, riscv64, aarch64] steps: - uses: actions/checkout@v3 diff --git a/Cargo.lock b/Cargo.lock index 175ee3a34..06591d4bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -98,11 +98,13 @@ dependencies = [ "axerrno", "axio", "axlog", + "crate_interface", "ruxconfig", "ruxdisplay", "ruxfeat", "ruxfs", "ruxhal", + "ruxmm", "ruxnet", "ruxruntime", "ruxtask", @@ -1733,17 +1735,6 @@ dependencies = [ "x86_64", ] -[[package]] -name = "ruxlibc" -version = "0.1.0" -dependencies = [ - "axerrno", - "axio", - "bindgen 0.66.1", - "ruxfeat", - "ruxos_posix_api", -] - [[package]] name = "ruxmm" version = "0.1.0" @@ -1873,13 +1864,18 @@ name = "ruxruntime" version = "0.1.0" dependencies = [ "axalloc", + "axfs_ramfs", + "axfs_vfs", + "axio", "axlog", "axsync", "cfg-if", "crate_interface", + "driver_block", "dtb", "kernel_guard", "lazy_init", + "log", "percpu", "rux9p", "ruxconfig", diff --git a/Cargo.toml b/Cargo.toml index fda7d6d92..303a9acdd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -59,7 +59,6 @@ members = [ "api/ruxos_posix_api", "ulib/axstd", - "ulib/ruxlibc", "ulib/ruxmusl", "apps/display/basic_painting", diff --git a/Makefile b/Makefile index e00e726e0..2d76c7945 100644 --- a/Makefile +++ b/Makefile @@ -47,7 +47,7 @@ V ?= # App options A ?= apps/c/helloworld APP ?= $(A) -FEATURES ?= multitask paging fs +FEATURES ?= APP_FEATURES ?= # QEMU options @@ -79,7 +79,7 @@ ARGS ?= ENVS ?= # Libc options -MUSL ?= n +MUSL ?= y # App type ifeq ($(wildcard $(APP)),) @@ -250,9 +250,6 @@ doc_check_missing: fmt: cargo fmt --all -fmt_c: - @clang-format --style=file -i $(shell find ulib/ruxlibc -iname '*.c' -o -iname '*.h') - test: $(call app_test) @@ -274,12 +271,11 @@ clean: clean_c clean_musl cargo clean clean_c:: - rm -rf ulib/ruxlibc/build_* rm -rf $(app-objs) clean_musl: rm -rf ulib/ruxmusl/build_* rm -rf ulib/ruxmusl/install -.PHONY: all build disasm run justrun debug clippy fmt fmt_c test test_no_fail_fast clean clean_c\ +.PHONY: all build disasm run justrun debug clippy fmt fmt_c test test_no_fail_fast clean \ clean_musl doc disk_image debug_no_attach prebuild _force diff --git a/README.md b/README.md index 006ca3a32..686c6ff21 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ The currently supported applications and programming languages, as well as their | Language | Description | |- | - | -| C | Run C apps by RuxOS ruxlibc or standard musl libc supported by ruxmusl. Evaluated by libc-bench. | +| C | Run C apps by standard musl libc supported by ruxmusl. Evaluated by libc-bench. | | C++ | Run C++ apps by c++ static library provided by musl libc. Passed c++ benchmark. Evaluated by c++ benchmark. | | [Perl](https://github.com/syswonder/rux-perl) | Run Perl standard library by musl libc. Evaluated by Perl benchmark. | | [Python](https://github.com/syswonder/rux-python3) | Run Python apps by dynamically loading Python modules. Evaluated by Python benchmark. | diff --git a/api/arceos_api/Cargo.toml b/api/arceos_api/Cargo.toml index b65cb69d9..616ce9ffd 100644 --- a/api/arceos_api/Cargo.toml +++ b/api/arceos_api/Cargo.toml @@ -28,9 +28,11 @@ myfs = ["ruxfeat/myfs"] dummy-if-not-enabled = [] [dependencies] +crate_interface = "0.1.1" ruxfeat = { path = "../ruxfeat" } ruxruntime = { path = "../../modules/ruxruntime" } ruxconfig = { path = "../../modules/ruxconfig" } +ruxmm = {path = "../../modules/ruxmm"} axlog = { path = "../../modules/axlog" } axio = { path = "../../crates/axio" } axerrno = { path = "../../crates/axerrno" } diff --git a/api/arceos_api/src/imp/mod.rs b/api/arceos_api/src/imp/mod.rs index c4d2e01ec..7a34fe345 100644 --- a/api/arceos_api/src/imp/mod.rs +++ b/api/arceos_api/src/imp/mod.rs @@ -9,6 +9,8 @@ mod mem; mod task; +#[cfg(feature = "paging")] +mod trap; cfg_fs! { mod fs; diff --git a/ulib/ruxlibc/src/utils.rs b/api/arceos_api/src/imp/trap.rs similarity index 62% rename from ulib/ruxlibc/src/utils.rs rename to api/arceos_api/src/imp/trap.rs index 14d05e5cf..bb90964ee 100644 --- a/ulib/ruxlibc/src/utils.rs +++ b/api/arceos_api/src/imp/trap.rs @@ -7,13 +7,14 @@ * See the Mulan PSL v2 for more details. */ -use core::ffi::c_int; +use ruxhal::trap::{PageFaultCause, TrapHandler}; -pub fn e(ret: c_int) -> c_int { - if ret < 0 { - crate::errno::set_errno(ret.abs()); - -1 - } else { - ret as _ +struct TrapHandlerImpl; + +#[crate_interface::impl_interface] +impl TrapHandler for TrapHandlerImpl { + fn handle_page_fault(vaddr: usize, cause: PageFaultCause) -> bool { + // TODO: handle page fault + panic!("Page fault at {:#x} with cause {:?}.", vaddr, cause); } } diff --git a/api/ruxos_posix_api/Cargo.toml b/api/ruxos_posix_api/Cargo.toml index 4fe38279f..3ff20b170 100644 --- a/api/ruxos_posix_api/Cargo.toml +++ b/api/ruxos_posix_api/Cargo.toml @@ -47,7 +47,7 @@ ruxfdtable = { path = "../../modules/ruxfdtable" } ruxmm = { path = "../../modules/ruxmm", optional = true } ruxfutex = { path = "../../modules/ruxfutex", optional = true } axalloc = { path = "../../modules/axalloc", optional = true } -ruxtask = { path = "../../modules/ruxtask", optional = true } +ruxtask = { path = "../../modules/ruxtask", features = ["notest"], optional = true } ruxfs = { path = "../../modules/ruxfs", optional = true } ruxnet = { path = "../../modules/ruxnet", optional = true } diff --git a/api/ruxos_posix_api/build.rs b/api/ruxos_posix_api/build.rs index 0529c64e6..76c151157 100644 --- a/api/ruxos_posix_api/build.rs +++ b/api/ruxos_posix_api/build.rs @@ -148,7 +148,7 @@ typedef struct {{ let mut builder = bindgen::Builder::default() .header(in_file) - .clang_arg("-I./../../ulib/ruxlibc/include") + .clang_arg("-I./../../ulib/include") .parse_callbacks(Box::new(MyCallbacks)) .derive_default(true) .size_t_is_usize(false) @@ -167,7 +167,7 @@ typedef struct {{ .expect("Couldn't write bindings!"); } - gen_pthread_mutex("../../ulib/ruxlibc/include/ax_pthread_mutex.h").unwrap(); - gen_pthread_cond("../../ulib/ruxlibc/include/ax_pthread_cond.h").unwrap(); + gen_pthread_mutex("../../ulib/include/ax_pthread_mutex.h").unwrap(); + gen_pthread_cond("../../ulib/include/ax_pthread_cond.h").unwrap(); gen_c_to_rust_bindings("ctypes.h", "src/ctypes_gen.rs"); } diff --git a/api/ruxos_posix_api/src/imp/fs.rs b/api/ruxos_posix_api/src/imp/fs.rs index 745c8d5a4..3b3e399aa 100644 --- a/api/ruxos_posix_api/src/imp/fs.rs +++ b/api/ruxos_posix_api/src/imp/fs.rs @@ -28,7 +28,7 @@ struct InitFsImpl; #[crate_interface::impl_interface] impl ruxtask::fs::InitFs for InitFsImpl { - fn init(fs: &mut ruxtask::fs::FileSystem) { + fn add_stdios_to_fd_table(fs: &mut ruxtask::fs::FileSystem) { debug!("init initial process's fd_table"); let fd_table = &mut fs.fd_table; fd_table.add_at(0, Arc::new(stdin()) as _).unwrap(); // stdin diff --git a/api/ruxos_posix_api/src/imp/mmap/api.rs b/api/ruxos_posix_api/src/imp/mmap/api.rs index e70acca02..447a63ad0 100644 --- a/api/ruxos_posix_api/src/imp/mmap/api.rs +++ b/api/ruxos_posix_api/src/imp/mmap/api.rs @@ -307,7 +307,7 @@ pub fn sys_msync(start: *mut c_void, len: ctypes::size_t, flags: c_int) -> c_int for (&vaddr, page_info) in current().mm.mem_map.lock().range(start..end) { if let Some(FileInfo { file, offset, size }) = &page_info.mapping_file { let src = vaddr as *mut u8; - write_into(&file, src, *offset as u64, *size); + write_into(file, src, *offset as u64, *size); } } } diff --git a/api/ruxos_posix_api/src/imp/mmap/trap.rs b/api/ruxos_posix_api/src/imp/mmap/trap.rs index c8fb1d693..cfa059757 100644 --- a/api/ruxos_posix_api/src/imp/mmap/trap.rs +++ b/api/ruxos_posix_api/src/imp/mmap/trap.rs @@ -205,10 +205,10 @@ impl ruxhal::trap::TrapHandler for TrapHandlerImpl { dst.copy_from(vaddr as *mut u8, size); } let paddr = direct_virt_to_phys(fake_vaddr); - let mapping_file = memory_map.get(&vaddr.into()).unwrap().mapping_file.clone(); - memory_map.remove(&vaddr.into()); + let mapping_file = memory_map.get(&vaddr).unwrap().mapping_file.clone(); + memory_map.remove(&vaddr); memory_map.insert( - vaddr.into(), + vaddr, Arc::new(PageInfo { paddr, #[cfg(feature = "fs")] diff --git a/api/ruxos_posix_api/src/imp/mmap/utils.rs b/api/ruxos_posix_api/src/imp/mmap/utils.rs index 582a1dcdf..89ee4a9a2 100644 --- a/api/ruxos_posix_api/src/imp/mmap/utils.rs +++ b/api/ruxos_posix_api/src/imp/mmap/utils.rs @@ -212,7 +212,7 @@ pub(crate) fn release_pages_mapped(start: usize, end: usize) { #[cfg(feature = "fs")] if let Some(FileInfo { file, offset, size }) = &page_info.mapping_file { let src = vaddr as *mut u8; - write_into(&file, src, *offset as u64, *size); + write_into(file, src, *offset as u64, *size); } if pte_unmap_page(VirtAddr::from(vaddr)).is_err() { panic!("Release page failed when munmapping!"); @@ -294,7 +294,7 @@ pub(crate) fn shift_mapped_page(start: usize, end: usize, vma_offset: usize, cop used_fs! { let mut opt_buffer = Vec::new(); - for (&start, &ref off_in_swap) in swaped_map.range(start..end) { + for (&start, off_in_swap) in swaped_map.range(start..end) { opt_buffer.push((start, off_in_swap.clone())); } for (start, swap_info) in opt_buffer { @@ -334,7 +334,7 @@ pub(crate) fn preload_page_with_swap( // For file mapping, the mapped content will be written directly to the original file. Some(FileInfo { file, offset, size }) => { let offset = *offset as u64; - write_into(&file, vaddr_swapped as *mut u8, offset, *size); + write_into(file, vaddr_swapped as *mut u8, offset, *size); pte_swap_preload(VirtAddr::from(vaddr_swapped)).unwrap() } // For anonymous mapping, you need to save the mapped memory to the prepared swap file, @@ -354,21 +354,6 @@ pub(crate) fn preload_page_with_swap( } } } - // For anonymous mapping, you need to save the mapped memory to the prepared swap file, - // and record the memory address and its offset in the swap file. - // Some((vaddr_swapped, PageInfo{paddr:_, mapping_file:Some(FileInfo{file, offset, size})})) => { - // let offset_get = off_pool.pop(); - // let offset = offset_get.unwrap(); - // swaped_map.insert(vaddr_swapped, Arc::new(offset)); - - // write_into( - // &SWAP_FILE, - // vaddr_swapped as *mut u8, - // offset as u64, - // PAGE_SIZE_4K, - // ); - // pte_swap_preload(VirtAddr::from(vaddr_swapped)).unwrap() - // } _ => panic!("No memory for mmap, check if huge memory leaky exists"), }, diff --git a/api/ruxos_posix_api/src/imp/pipe.rs b/api/ruxos_posix_api/src/imp/pipe.rs index bb0ab0184..afc56c613 100644 --- a/api/ruxos_posix_api/src/imp/pipe.rs +++ b/api/ruxos_posix_api/src/imp/pipe.rs @@ -117,7 +117,6 @@ impl Pipe { pub fn write_end_close(&self) -> bool { let write_end_count = Arc::weak_count(&self.buffer); - // error!("Pipe::write_end_close <= buffer: {:#?} {:#?}", write_end_count, Arc::as_ptr(&self.buffer)); write_end_count == 0 } } diff --git a/api/ruxos_posix_api/src/imp/pthread/mod.rs b/api/ruxos_posix_api/src/imp/pthread/mod.rs index ce79a2be2..3ea951f5a 100644 --- a/api/ruxos_posix_api/src/imp/pthread/mod.rs +++ b/api/ruxos_posix_api/src/imp/pthread/mod.rs @@ -260,10 +260,7 @@ unsafe impl Send for ForceSendSync {} unsafe impl Sync for ForceSendSync {} /// Create new thread by `sys_clone`, return new thread ID -#[cfg(all( - feature = "musl", - any(target_arch = "aarch64", target_arch = "riscv64") -))] +#[cfg(all(feature = "musl", target_arch = "aarch64"))] pub unsafe fn sys_clone( flags: c_int, stack: *mut c_void, @@ -332,6 +329,58 @@ pub unsafe fn sys_clone( }) } +/// Create new thread by `sys_clone`, return new thread ID +#[cfg(all(feature = "musl", target_arch = "riscv64"))] +pub unsafe fn sys_clone( + flags: c_int, + stack: *mut c_void, + ptid: *mut ctypes::pid_t, + tls: *mut c_void, + ctid: *mut ctypes::pid_t, +) -> c_int { + debug!( + "sys_clone <= flags: {:x}, stack: {:p}, ctid: {:x}", + flags, stack, ctid as usize + ); + + syscall_body!(sys_clone, { + if (flags as u32 & ctypes::CLONE_THREAD) != 0 { + let func = unsafe { + core::mem::transmute::<*const (), extern "C" fn(arg: *mut c_void) -> *mut c_void>( + (*(stack as *mut usize)) as *const (), + ) + }; + let args = unsafe { *((stack as usize + 8) as *mut usize) } as *mut c_void; + + let set_tid = if (flags as u32 & ctypes::CLONE_CHILD_SETTID) != 0 { + core::sync::atomic::AtomicU64::new(ctid as _) + } else { + core::sync::atomic::AtomicU64::new(0) + }; + + let (tid, task_inner) = Pthread::pcreate( + core::ptr::null(), + func, + args, + tls, + set_tid, + core::sync::atomic::AtomicU64::from(ctid as u64), + )?; + + // write tid to ptid + if (flags as u32 & ctypes::CLONE_PARENT_SETTID) != 0 { + unsafe { *ptid = tid as c_int }; + } + ruxtask::put_task(task_inner); + + return Ok(tid); + } else { + debug!("ONLY support CLONE_THREAD and SIGCHLD"); + return Err(LinuxError::EINVAL); + } + }) +} + /// Create new thread by `sys_clone`, return new thread ID #[cfg(all(feature = "musl", target_arch = "x86_64"))] pub unsafe fn sys_clone( diff --git a/api/ruxos_posix_api/src/imp/task.rs b/api/ruxos_posix_api/src/imp/task.rs index ae07be44b..a81854bac 100644 --- a/api/ruxos_posix_api/src/imp/task.rs +++ b/api/ruxos_posix_api/src/imp/task.rs @@ -7,10 +7,13 @@ * See the Mulan PSL v2 for more details. */ -use crate::ctypes; use core::ffi::c_int; -use ruxtask::{task::PROCESS_MAP, yield_now}; +#[cfg(feature = "multitask")] +use { + crate::ctypes, + ruxtask::{task::PROCESS_MAP, yield_now}, +}; /// Relinquish the CPU, and switches to another task. /// @@ -31,31 +34,52 @@ pub fn sys_sched_yield() -> c_int { /// Get current thread ID. pub fn sys_gettid() -> c_int { syscall_body!(sys_gettid, - #[cfg(feature = "multitask")] - { - Ok(ruxtask::current().id().as_u64() as c_int) - } - #[cfg(not(feature = "multitask"))] - { + #[cfg(not(feature = "multitask"))]{ Ok(2) // `main` task ID } + #[cfg(feature = "multitask")]{ + Ok(ruxtask::current().id().as_u64() as c_int) + } ) } /// Get current process ID. pub fn sys_getpid() -> c_int { - syscall_body!(sys_getpid, Ok(ruxtask::current().id().as_u64() as c_int)) + #[cfg(not(feature = "multitask"))] + { + syscall_body!(sys_getpid, Ok(1)) + } + + #[cfg(feature = "multitask")] + { + syscall_body!(sys_getpid, Ok(ruxtask::current().id().as_u64() as c_int)) + } } /// Get parent process's ID. pub fn sys_getppid() -> c_int { - syscall_body!(sys_getppid, Ok(1)) + #[cfg(not(feature = "multitask"))] + { + syscall_body!(sys_getppid, Ok(1)) + } + + #[cfg(feature = "multitask")] + { + syscall_body!(sys_getppid, { + if let Some(parent_taskid) = ruxtask::current().parent_process() { + Ok(parent_taskid.id().as_u64() as c_int) + } else { + Ok(0) // `init` process ID + } + }) + } } /// Wait for a child process to exit and return its status. /// /// TODO: part of options, and rusage are not implemented yet. -pub fn sys_wait4( +#[cfg(feature = "multitask")] +pub unsafe fn sys_wait4( pid: c_int, wstatus: *mut c_int, options: c_int, @@ -101,18 +125,18 @@ pub fn sys_wait4( .filter(|(_, task)| task.parent_process().is_some()) { let parent_pid = task.parent_process().unwrap().id().as_u64(); - if parent_pid == ruxtask::current().id().as_u64() { - if task.state() == ruxtask::task::TaskState::Exited { - // add to to_remove list - unsafe { - // lower 8 bits of exit_code is the signal number, while upper 8 bits of exit_code is the exit status - // according to "bits/waitstatus.h" in glibc source code. - // TODO: add signal number to wstatus - wstatus.write(task.exit_code() << 8); - } - let _ = to_remove.insert(*child_pid); - break; + if parent_pid == ruxtask::current().id().as_u64() + && task.state() == ruxtask::task::TaskState::Exited + { + // add to to_remove list + unsafe { + // lower 8 bits of exit_code is the signal number, while upper 8 bits of exit_code is the exit status + // according to "bits/waitstatus.h" in glibc source code. + // TODO: add signal number to wstatus + wstatus.write(task.exit_code() << 8); } + let _ = to_remove.insert(*child_pid); + break; } } if options & WNOHANG != 0 { @@ -121,7 +145,7 @@ pub fn sys_wait4( // drop lock before yielding to other tasks drop(process_map); // check if the condition is meet - if !to_remove.is_none() { + if to_remove.is_some() { break; } // for single-cpu system, we must yield to other tasks instead of dead-looping here. diff --git a/api/ruxos_posix_api/src/lib.rs b/api/ruxos_posix_api/src/lib.rs index 9e0e69ce6..72e3f2c05 100644 --- a/api/ruxos_posix_api/src/lib.rs +++ b/api/ruxos_posix_api/src/lib.rs @@ -56,7 +56,9 @@ pub use imp::stat::{ }; pub use imp::sys::{sys_sysinfo, sys_uname}; pub use imp::sys_invalid; -pub use imp::task::{sys_exit, sys_getpid, sys_getppid, sys_gettid, sys_sched_yield, sys_wait4}; +#[cfg(feature = "multitask")] +pub use imp::task::sys_wait4; +pub use imp::task::{sys_exit, sys_getpid, sys_getppid, sys_gettid, sys_sched_yield}; pub use imp::time::{ sys_clock_gettime, sys_clock_nanosleep, sys_clock_settime, sys_gettimeofday, sys_nanosleep, sys_times, diff --git a/apps/c/envtest/expect_info.out b/apps/c/envtest/expect_info.out index cd1e5927d..5c11ea248 100644 --- a/apps/c/envtest/expect_info.out +++ b/apps/c/envtest/expect_info.out @@ -11,7 +11,6 @@ Found physcial memory regions: .bss (READ | WRITE | RESERVED) free memory (READ | WRITE | EXECUTE | FREE) Initialize global memory allocator... - use TLSF allocator. Initialize kernel page table... Initialize platform devices... Primary CPU 0 init OK. diff --git a/apps/c/httpclient/features.txt b/apps/c/httpclient/features.txt index 25ca64f38..af0b03c96 100644 --- a/apps/c/httpclient/features.txt +++ b/apps/c/httpclient/features.txt @@ -1,3 +1,5 @@ alloc paging net +poll +rtc diff --git a/apps/c/memtest/expect_trace.out b/apps/c/memtest/expect_trace.out index 3bdabf61c..52e341aa0 100644 --- a/apps/c/memtest/expect_trace.out +++ b/apps/c/memtest/expect_trace.out @@ -1,43 +1,14 @@ -smp = 1 -build_mode = release -log_level = trace - -Primary CPU 0 started, -Found physcial memory regions: - .text (READ | EXECUTE | RESERVED) - .rodata (READ | RESERVED) - .data .tdata .tbss .percpu (READ | WRITE | RESERVED) - .percpu (READ | WRITE | RESERVED) - boot stack (READ | WRITE | RESERVED) - .bss (READ | WRITE | RESERVED) - free memory (READ | WRITE | EXECUTE | FREE) -Initialize global memory allocator... - use TLSF allocator. -initialize global allocator at: \[0x[0-9a-f]\+, 0x[0-9a-f]\+) -Initialize kernel page table... -Initialize platform devices... -map_region(PA:0x[0-9a-f]\+): \[VA:0x[0-9a-f]\+, VA:0x[0-9a-f]\+) -> \[PA:0x[0-9a-f]\+, PA:0x[0-9a-f]\+) MappingFlags(READ | EXECUTE) -map_region(PA:0x[0-9a-f]\+): \[VA:0x[0-9a-f]\+, VA:0x[0-9a-f]\+) -> \[PA:0x[0-9a-f]\+, PA:0x[0-9a-f]\+) MappingFlags(READ) -map_region(PA:0x[0-9a-f]\+): \[VA:0x[0-9a-f]\+, VA:0x[0-9a-f]\+) -> \[PA:0x[0-9a-f]\+, PA:0x[0-9a-f]\+) MappingFlags(READ | WRITE) -map_region(PA:0x[0-9a-f]\+): \[VA:0x[0-9a-f]\+, VA:0x[0-9a-f]\+) -> \[PA:0x[0-9a-f]\+, PA:0x[0-9a-f]\+) MappingFlags(READ | WRITE) -map_region(PA:0x[0-9a-f]\+): \[VA:0x[0-9a-f]\+, VA:0x[0-9a-f]\+) -> \[PA:0x[0-9a-f]\+, PA:0x[0-9a-f]\+) MappingFlags(READ | WRITE) -map_region(PA:0x[0-9a-f]\+): \[VA:0x[0-9a-f]\+, VA:0x[0-9a-f]\+) -> \[PA:0x[0-9a-f]\+, PA:0x[0-9a-f]\+) MappingFlags(READ | WRITE) -map_region(PA:0x[0-9a-f]\+): \[VA:0x[0-9a-f]\+, VA:0x[0-9a-f]\+) -> \[PA:0x[0-9a-f]\+, PA:0x[0-9a-f]\+) MappingFlags(READ | WRITE | DEVICE) -map_region(PA:0x[0-9a-f]\+): \[VA:0x[0-9a-f]\+, VA:0x[0-9a-f]\+) -> \[PA:0x[0-9a-f]\+, PA:0x[0-9a-f]\+) MappingFlags(READ | WRITE) -set page table root: PA:0x[0-9a-f]\+ => PA:0x[0-9a-f]\+ -Primary CPU 0 init OK. Running memory tests... -top of heap=0x[0-9a-f]\{16\} -72(+8)Byte allocated: p=0x[0-9a-f]\{16\} -allocate 8(+8)Byte for 9 times: -allocated addr=0x[0-9a-f]\{16\} -allocated addr=0x[0-9a-f]\{16\} -allocated addr=0x[0-9a-f]\{16\} -allocated addr=0x[0-9a-f]\{16\} -allocated addr=0x[0-9a-f]\{16\} -allocated addr=0x[0-9a-f]\{16\} -allocated addr=0x[0-9a-f]\{16\} -allocated addr=0x[0-9a-f]\{16\} -allocated addr=0x[0-9a-f]\{16\} -Memory tests run OK! -Shutting down... \ No newline at end of file +top of heap= +72(+8)Byte allocated: p= +allocate 8(+8)Byte for 9 +allocated addr= +allocated addr= +allocated addr= +allocated addr= +allocated addr= +allocated addr= +allocated addr= +allocated addr= +allocated addr= +Memory tests run OK! \ No newline at end of file diff --git a/apps/c/pthread/basic/expect_info_smp4_fifo.out b/apps/c/pthread/basic/expect_info_smp4_fifo.out index 3b75dedc8..115be8878 100644 --- a/apps/c/pthread/basic/expect_info_smp4_fifo.out +++ b/apps/c/pthread/basic/expect_info_smp4_fifo.out @@ -15,7 +15,6 @@ Initialize global memory allocator... Initialize kernel page table... Initialize platform devices... Initialize scheduling... - use FIFO scheduler. Pass NULL argument Recieve: Main thread pass message test_create_join: Child thread return message diff --git a/apps/c/pthread/basic/features.txt b/apps/c/pthread/basic/features.txt index 035f5582a..86e9e458a 100644 --- a/apps/c/pthread/basic/features.txt +++ b/apps/c/pthread/basic/features.txt @@ -2,3 +2,4 @@ alloc paging multitask irq +signal diff --git a/apps/c/pthread/parallel/expect_info_smp4_fifo.out b/apps/c/pthread/parallel/expect_info_smp4_fifo.out index b11eb07ca..c0e92224f 100644 --- a/apps/c/pthread/parallel/expect_info_smp4_fifo.out +++ b/apps/c/pthread/parallel/expect_info_smp4_fifo.out @@ -1,28 +1,3 @@ -smp = 4 -build_mode = release -log_level = info - -CPU 0 started -Found physcial memory regions: - .text (READ | EXECUTE | RESERVED) - .rodata (READ | RESERVED) - .data .tdata .tbss .percpu (READ | WRITE | RESERVED) - .percpu (READ | WRITE | RESERVED) - boot stack (READ | WRITE | RESERVED) - .bss (READ | WRITE | RESERVED) - free memory (READ | WRITE | EXECUTE | FREE) -Initialize global memory allocator... -Initialize kernel page table... -Initialize platform devices... -Initialize scheduling... - use FIFO scheduler -CPU 1 started -CPU 1 init OK -CPU 2 started -CPU 2 init OK -CPU 3 started -CPU 3 init OK -CPU 0 init OK part 0: \[0, 125000) part 1: \[125000, 250000) part 2: \[250000, 375000) @@ -56,4 +31,3 @@ part 13 finished part 14 finished part 15 finished (C)Pthread parallel run OK! -Shutting down... diff --git a/apps/c/pthread/parallel/expect_info_smp4_rr.out b/apps/c/pthread/parallel/expect_info_smp4_rr.out index 12efc08ad..c0e92224f 100644 --- a/apps/c/pthread/parallel/expect_info_smp4_rr.out +++ b/apps/c/pthread/parallel/expect_info_smp4_rr.out @@ -1,29 +1,3 @@ -smp = 4 -build_mode = release -log_level = info - -CPU 0 started -Found physcial memory regions: - .text (READ | EXECUTE | RESERVED) - .rodata (READ | RESERVED) - .data .tdata .tbss .percpu (READ | WRITE | RESERVED) - .percpu (READ | WRITE | RESERVED) - boot stack (READ | WRITE | RESERVED) - .bss (READ | WRITE | RESERVED) - free memory (READ | WRITE | EXECUTE | FREE) -Initialize global memory allocator... -Initialize kernel page table... -Initialize platform devices... -Initialize scheduling... - use Round-robin scheduler -CPU 1 started -CPU 1 init OK -CPU 2 started -CPU 2 init OK -CPU 3 started -CPU 3 init OK -Initialize interrupt handlers... -CPU 0 init OK part 0: \[0, 125000) part 1: \[125000, 250000) part 2: \[250000, 375000) @@ -57,4 +31,3 @@ part 13 finished part 14 finished part 15 finished (C)Pthread parallel run OK! -Shutting down... diff --git a/apps/c/pthread/parallel/features.txt b/apps/c/pthread/parallel/features.txt index 6ca9b0c5f..56be374d4 100644 --- a/apps/c/pthread/parallel/features.txt +++ b/apps/c/pthread/parallel/features.txt @@ -1,3 +1,4 @@ alloc paging multitask +signal diff --git a/apps/c/pthread/parallel/test_cmd b/apps/c/pthread/parallel/test_cmd index b0d44ae37..e6d65df42 100644 --- a/apps/c/pthread/parallel/test_cmd +++ b/apps/c/pthread/parallel/test_cmd @@ -1,3 +1,3 @@ -test_one "SMP=4 LOG=info" "expect_info_smp4_fifo.out" -test_one "SMP=4 LOG=info FEATURES=sched_rr" "expect_info_smp4_rr.out" +test_one "SMP=4 LOG=off" "expect_info_smp4_fifo.out" +test_one "SMP=4 LOG=off FEATURES=sched_rr" "expect_info_smp4_rr.out" rm -f $APP/*.o diff --git a/apps/c/pthread/pipe/expect_info_smp4_fifo.out b/apps/c/pthread/pipe/expect_info_smp4_fifo.out index fefc8cc0c..213c1d841 100644 --- a/apps/c/pthread/pipe/expect_info_smp4_fifo.out +++ b/apps/c/pthread/pipe/expect_info_smp4_fifo.out @@ -15,7 +15,6 @@ Initialize global memory allocator... Initialize kernel page table... Initialize platform devices... Initialize scheduling... - use FIFO scheduler. Child thread send message(1) Child thread send message(2) Main thread recieve (1): I am child(1)! diff --git a/apps/c/pthread/pipe/features.txt b/apps/c/pthread/pipe/features.txt index be558434d..1bcdfc264 100644 --- a/apps/c/pthread/pipe/features.txt +++ b/apps/c/pthread/pipe/features.txt @@ -2,3 +2,4 @@ paging alloc multitask pipe +signal diff --git a/apps/c/pthread/sleep/expect_info_smp4_fifo.out b/apps/c/pthread/sleep/expect_info_smp4_fifo.out index 6446cd725..b3b99198f 100644 --- a/apps/c/pthread/sleep/expect_info_smp4_fifo.out +++ b/apps/c/pthread/sleep/expect_info_smp4_fifo.out @@ -15,7 +15,6 @@ Initialize global memory allocator... Initialize kernel page table... Initialize platform devices... Initialize scheduling... - use FIFO scheduler. Initialize interrupt handlers... Hello, main task! main task sleep for 1\.[0-9]\+s diff --git a/apps/c/pthread/sleep/features.txt b/apps/c/pthread/sleep/features.txt index efe7876d3..a80f84ac6 100644 --- a/apps/c/pthread/sleep/features.txt +++ b/apps/c/pthread/sleep/features.txt @@ -2,3 +2,4 @@ paging alloc multitask irq +signal diff --git a/apps/c/pthread/tsd/expect_info_smp4_fifo.out b/apps/c/pthread/tsd/expect_info_smp4_fifo.out index 28018797a..480302205 100644 --- a/apps/c/pthread/tsd/expect_info_smp4_fifo.out +++ b/apps/c/pthread/tsd/expect_info_smp4_fifo.out @@ -15,7 +15,6 @@ Initialize global memory allocator... Initialize kernel page table... Initialize platform devices... Initialize scheduling... - use FIFO scheduler. max_keys = 1024, got No.0 destr_func, *arg = 0x1234 destr_func, *arg = 0x5678 diff --git a/apps/c/pthread/tsd/features.txt b/apps/c/pthread/tsd/features.txt index 035f5582a..86e9e458a 100644 --- a/apps/c/pthread/tsd/features.txt +++ b/apps/c/pthread/tsd/features.txt @@ -2,3 +2,4 @@ alloc paging multitask irq +signal diff --git a/modules/ruxdriver/src/virtio.rs b/modules/ruxdriver/src/virtio.rs index 2c36297a4..eed4d7da8 100644 --- a/modules/ruxdriver/src/virtio.rs +++ b/modules/ruxdriver/src/virtio.rs @@ -7,12 +7,21 @@ * See the Mulan PSL v2 for more details. */ +//! A driver for VirtIO devices. + use crate::{drivers::DriverProbe, AxDeviceEnum}; use cfg_if::cfg_if; use core::marker::PhantomData; use driver_common::{BaseDriverOps, DevResult, DeviceType}; #[cfg(bus = "mmio")] use ruxhal::mem::phys_to_virt; +#[cfg(any( + feature = "virtio-net", + feature = "virtio-blk", + feature = "virtio-gpu", + feature = "virtio-9p", + feature = "pci" +))] use ruxhal::virtio::virtio_hal::VirtIoHalImpl; cfg_if! { @@ -26,16 +35,21 @@ cfg_if! { /// A trait for VirtIO device meta information. pub trait VirtIoDevMeta { + /// The device type of the VirtIO device. const DEVICE_TYPE: DeviceType; + /// The device type of the VirtIO device. type Device: BaseDriverOps; + /// The driver for the VirtIO device. type Driver = VirtIoDriver; + /// Try to create a new instance of the VirtIO device. fn try_new(transport: VirtIoTransport) -> DevResult; } cfg_if! { if #[cfg(net_dev = "virtio-net")] { + /// A VirtIO network device. pub struct VirtIoNet; impl VirtIoDevMeta for VirtIoNet { @@ -51,6 +65,7 @@ cfg_if! { cfg_if! { if #[cfg(block_dev = "virtio-blk")] { + /// A VirtIO block device. pub struct VirtIoBlk; impl VirtIoDevMeta for VirtIoBlk { @@ -66,6 +81,7 @@ cfg_if! { cfg_if! { if #[cfg(display_dev = "virtio-gpu")] { + /// A VirtIO GPU device. pub struct VirtIoGpu; impl VirtIoDevMeta for VirtIoGpu { @@ -81,6 +97,7 @@ cfg_if! { cfg_if! { if #[cfg(_9p_dev = "virtio-9p")] { + /// A VirtIO 9P device. pub struct VirtIo9p; impl VirtIoDevMeta for VirtIo9p { @@ -94,7 +111,7 @@ cfg_if! { } } -/// A common driver for all VirtIO devices that implements [`DriverProbe`]. +/// A common driver for all VirtIO devices that implements DriverProbe. pub struct VirtIoDriver(PhantomData); impl DriverProbe for VirtIoDriver { diff --git a/modules/ruxfs/src/root.rs b/modules/ruxfs/src/root.rs index 117dca0b3..4e5593a3b 100644 --- a/modules/ruxfs/src/root.rs +++ b/modules/ruxfs/src/root.rs @@ -19,7 +19,9 @@ use crate::api::FileType; /// mount point information pub struct MountPoint { + /// mount point path pub path: &'static str, + /// mounted filesystem pub fs: Arc, } @@ -230,9 +232,16 @@ pub(crate) fn remove_dir(dir: Option<&VfsNodeRef>, path: &str) -> AxResult { { return ax_err!(InvalidInput); } - // if ROOT_DIR.contains(&absolute_path(path)?) { - // return ax_err!(PermissionDenied); - // } + + // TODO: judge if the path is a mount point, + // and return PermissionDenied if it is.(not allow to remove mount points) + // but it's not necessary to do this in this function. + // to meet multi-process requirement, some checks were commented out. + if crate_interface::call_interface!(CurrentWorkingDirectoryOps::root_dir,) + .contains(&absolute_path(path)?) + { + return ax_err!(PermissionDenied); + } let node = lookup(dir, path)?; let attr = node.get_attr()?; @@ -254,12 +263,20 @@ pub(crate) fn rename(old: &str, new: &str) -> AxResult { } #[crate_interface::def_interface] +/// Current working directory operations. pub trait CurrentWorkingDirectoryOps { + /// Initializes the root filesystem with the specified mount points. fn init_rootfs(mount_points: Vec); + /// Returns the parent node of the specified path. fn parent_node_of(dir: Option<&VfsNodeRef>, path: &str) -> VfsNodeRef; + /// Returns the absolute path of the specified path. fn absolute_path(path: &str) -> AxResult; + /// Returns the current working directory. fn current_dir() -> AxResult; + /// Sets the current working directory. fn set_current_dir(path: &str) -> AxResult; + /// get the root directory of the filesystem + fn root_dir() -> Arc; } pub(crate) fn parent_node_of(dir: Option<&VfsNodeRef>, path: &str) -> VfsNodeRef { diff --git a/modules/ruxhal/Cargo.toml b/modules/ruxhal/Cargo.toml index bc6e143f1..c12254a7a 100644 --- a/modules/ruxhal/Cargo.toml +++ b/modules/ruxhal/Cargo.toml @@ -23,7 +23,7 @@ tls = ["alloc"] default = [] musl = [] signal = [] -virtio_console = ["driver_console", "driver_virtio", "driver_virtio/console", "driver_common", "virtio-drivers", "axalloc", "lazy_static", "alloc", "virtio"] +virtio_console = ["driver_console", "driver_virtio", "driver_virtio/console", "driver_common", "virtio-drivers", "axalloc", "alloc", "virtio"] [dependencies] @@ -46,7 +46,7 @@ driver_console = { path = "../../crates/driver_console", optional = true } driver_virtio = { path = "../../crates/driver_virtio", optional = true } driver_common = { path = "../../crates/driver_common", optional = true } virtio-drivers = { git = "https://github.com/syswonder/virtio-drivers.git", rev = "62dbe5a", optional = true } -lazy_static = { version = "1.4", features = ["spin_no_std"], optional = true } +lazy_static = { version = "1.4", features = ["spin_no_std"] } memory_addr = "0.1.0" handler_table = "0.1.0" crate_interface = "0.1.1" diff --git a/modules/ruxhal/src/arch/x86_64/mod.rs b/modules/ruxhal/src/arch/x86_64/mod.rs index 973ff5cf6..a934c5b18 100644 --- a/modules/ruxhal/src/arch/x86_64/mod.rs +++ b/modules/ruxhal/src/arch/x86_64/mod.rs @@ -174,7 +174,6 @@ pub fn flush_tlb(vaddr: Option) { #[inline] #[cfg(all(feature = "irq", feature = "paging", feature = "smp"))] pub(crate) fn flush_tlb_ipi_handler() { - // error!("flush TLB entry in IPI handler"); let guard = kernel_guard::NoPreempt::new(); unsafe { let mut flushing_addresses = FLUSHING_ADDRESSES[this_cpu_id()].lock(); diff --git a/modules/ruxhal/src/mem.rs b/modules/ruxhal/src/mem.rs index 6c45d12eb..3475d9874 100644 --- a/modules/ruxhal/src/mem.rs +++ b/modules/ruxhal/src/mem.rs @@ -56,11 +56,13 @@ pub struct MemRegion { /// A trait for address translation. #[crate_interface::def_interface] pub trait AddressTranslate { + /// Translates a virtual address to a physical address. fn virt_to_phys(vaddr: VirtAddr) -> Option { Some(direct_virt_to_phys(vaddr).into()) } } +/// translates a virtual address to a physical address. pub fn address_translate(vaddr: VirtAddr) -> Option { crate_interface::call_interface!(AddressTranslate::virt_to_phys, vaddr) } diff --git a/modules/ruxhal/src/platform/x86_pc/rtc.rs b/modules/ruxhal/src/platform/x86_pc/rtc.rs index 6744bb7d1..cdc002d07 100644 --- a/modules/ruxhal/src/platform/x86_pc/rtc.rs +++ b/modules/ruxhal/src/platform/x86_pc/rtc.rs @@ -141,6 +141,12 @@ pub struct Rtc { nmi: bool, } +impl Default for Rtc { + fn default() -> Self { + Self::new() + } +} + impl Rtc { /// Create new empty RTC pub fn new() -> Self { diff --git a/modules/ruxmm/src/lib.rs b/modules/ruxmm/src/lib.rs index 76a09e7b2..31ce9993a 100644 --- a/modules/ruxmm/src/lib.rs +++ b/modules/ruxmm/src/lib.rs @@ -7,6 +7,8 @@ * See the Mulan PSL v2 for more details. */ +//! memory management module for RuxOS. + #![no_std] #![feature(asm_const)] #![feature(naked_functions)] @@ -18,4 +20,5 @@ pub mod mem; #[cfg(feature = "paging")] +/// A module for paging operations. pub mod paging; diff --git a/modules/ruxmm/src/mem.rs b/modules/ruxmm/src/mem.rs index fe9f599b8..6ada172a6 100644 --- a/modules/ruxmm/src/mem.rs +++ b/modules/ruxmm/src/mem.rs @@ -7,6 +7,8 @@ * See the Mulan PSL v2 for more details. */ +//! This module provides functions to translate virtual addresses to physical addresses. + #[cfg(feature = "paging")] use crate::paging::pte_query; use ruxhal::mem::{direct_virt_to_phys, AddressTranslate, PhysAddr, VirtAddr}; diff --git a/modules/ruxnet/src/lwip_impl/addr.rs b/modules/ruxnet/src/lwip_impl/addr.rs index 0cf41a737..2359d4323 100644 --- a/modules/ruxnet/src/lwip_impl/addr.rs +++ b/modules/ruxnet/src/lwip_impl/addr.rs @@ -3,14 +3,9 @@ use core::{ str::FromStr, }; -use lwip_rust::bindings::{ - ip4_addr_t, ip_addr_t, lwip_ip_addr_type_IPADDR_TYPE_V4, lwip_ip_addr_type_IPADDR_TYPE_V6, -}; +use lwip_rust::bindings::ip_addr_t; -use core::net::{ - Ipv4Addr as CoreIpv4Addr, Ipv6Addr as CoreIpv6Addr, SocketAddr as CoreSocketAddr, SocketAddrV4, - SocketAddrV6, -}; +use core::net::{Ipv4Addr as CoreIpv4Addr, SocketAddr as CoreSocketAddr, SocketAddrV4}; /// Mac Address #[derive(Clone, Copy, Debug, Default)] @@ -73,7 +68,7 @@ impl IpAddr { /// Get the IP Address as a byte array pub fn as_bytes(&self) -> &[u8] { match self { - IpAddr::Ipv4(Ipv4Addr(addr)) => unsafe { &addr[..] }, + IpAddr::Ipv4(Ipv4Addr(addr)) => &addr[..], _ => panic!("IPv6 not supported"), } } @@ -120,7 +115,7 @@ impl From for ip_addr_t { impl From for IpAddr { #[allow(non_upper_case_globals)] fn from(addr: ip_addr_t) -> IpAddr { - IpAddr::Ipv4(Ipv4Addr(unsafe { addr.addr.to_be_bytes() })) + IpAddr::Ipv4(Ipv4Addr(addr.addr.to_be_bytes())) } } diff --git a/modules/ruxnet/src/lwip_impl/dns.rs b/modules/ruxnet/src/lwip_impl/dns.rs index 829775745..626ec99cc 100644 --- a/modules/ruxnet/src/lwip_impl/dns.rs +++ b/modules/ruxnet/src/lwip_impl/dns.rs @@ -98,7 +98,7 @@ pub fn resolve_socket_addr(name: &str) -> AxResult> { } /// Public function for DNS query. -pub fn dns_query(name: &str) -> AxResult> { +pub fn dns_query(_name: &str) -> AxResult> { let empty_vec = alloc::vec::Vec::new(); Ok(empty_vec) } diff --git a/modules/ruxnet/src/lwip_impl/driver.rs b/modules/ruxnet/src/lwip_impl/driver.rs index 70c546bdd..72b521669 100644 --- a/modules/ruxnet/src/lwip_impl/driver.rs +++ b/modules/ruxnet/src/lwip_impl/driver.rs @@ -3,12 +3,12 @@ use crate::{ net_impl::addr::{mask_to_prefix, MacAddr}, IpAddr, }; -use alloc::{boxed::Box, collections::VecDeque, sync::Arc, vec}; +use alloc::{boxed::Box, collections::VecDeque, sync::Arc}; #[cfg(feature = "irq")] use axdriver::register_interrupt_handler; use axsync::Mutex; use core::{cell::RefCell, ffi::c_void}; -use driver_net::{DevError, NetBuf, NetBufBox, NetBufPool, NetBufPtr}; +use driver_net::{DevError, NetBuf, NetBufBox}; use lazy_init::LazyInit; use lwip_rust::bindings::{ err_enum_t_ERR_MEM, err_enum_t_ERR_OK, err_t, etharp_output, ethernet_input, ip4_addr_t, @@ -21,9 +21,6 @@ use ruxdriver::prelude::*; const RX_BUF_QUEUE_SIZE: usize = 64; -const NET_BUF_LEN: usize = 1526; -const NET_BUF_POOL_SIZE: usize = 128; - struct NetifWrapper(netif); unsafe impl Send for NetifWrapper {} @@ -164,7 +161,7 @@ extern "C" fn ethif_output(netif: *mut netif, p: *mut pbuf) -> err_t { if dev.can_transmit() { unsafe { - let tot_len = unsafe { (*p).tot_len }; + let tot_len = (*p).tot_len; let mut tx_buf = *NetBuf::from_buf_ptr(dev.alloc_tx_buffer(tot_len.into()).unwrap()); dev.prepare_tx_buffer(&mut tx_buf, tot_len.into()).unwrap(); @@ -172,12 +169,12 @@ extern "C" fn ethif_output(netif: *mut netif, p: *mut pbuf) -> err_t { let mut offset = 0; let mut q = p; while !q.is_null() { - let len = unsafe { (*q).len } as usize; - let payload = unsafe { (*q).payload }; - let payload = unsafe { core::slice::from_raw_parts(payload as *const u8, len) }; + let len = (*q).len as usize; + let payload = (*q).payload; + let payload = core::slice::from_raw_parts(payload as *const u8, len); tx_buf.packet_mut()[offset..offset + len].copy_from_slice(payload); offset += len; - q = unsafe { (*q).next }; + q = (*q).next; } trace!( @@ -217,7 +214,7 @@ fn ip4_addr_gen(a: u8, b: u8, c: u8, d: u8) -> ip4_addr_t { } pub fn init() {} -pub fn init_netdev(mut net_dev: AxNetDevice) { +pub fn init_netdev(net_dev: AxNetDevice) { match net_dev.device_name() { "loopback" => { info!("use lwip netif loopback"); diff --git a/modules/ruxnet/src/lwip_impl/mod.rs b/modules/ruxnet/src/lwip_impl/mod.rs index 87084c607..907701d00 100644 --- a/modules/ruxnet/src/lwip_impl/mod.rs +++ b/modules/ruxnet/src/lwip_impl/mod.rs @@ -1,3 +1,5 @@ +#![allow(dead_code)] + mod addr; mod dns; mod driver; @@ -5,7 +7,7 @@ mod tcp; mod udp; pub use self::addr::{IpAddr, Ipv4Addr, SocketAddr}; -pub use self::dns::{dns_query, resolve_socket_addr}; +pub use self::dns::dns_query; pub use self::driver::{init, init_netdev, poll_interfaces}; pub use self::tcp::TcpSocket; pub use self::udp::UdpSocket; diff --git a/modules/ruxnet/src/lwip_impl/tcp.rs b/modules/ruxnet/src/lwip_impl/tcp.rs index c3abfe3ee..5f8691c21 100644 --- a/modules/ruxnet/src/lwip_impl/tcp.rs +++ b/modules/ruxnet/src/lwip_impl/tcp.rs @@ -7,7 +7,7 @@ use axerrno::{ax_err, AxError, AxResult}; use axio::PollState; use axsync::Mutex; use core::cell::UnsafeCell; -use core::sync::atomic::{AtomicBool, AtomicU8, Ordering}; +use core::sync::atomic::{AtomicBool, Ordering}; use core::{ffi::c_void, pin::Pin, ptr::null_mut}; use lwip_rust::bindings::{ err_enum_t_ERR_MEM, err_enum_t_ERR_OK, err_enum_t_ERR_USE, err_enum_t_ERR_VAL, err_t, @@ -375,7 +375,7 @@ impl TcpSocket { } /// Receives data from the socket, stores it in the given buffer. - pub fn recv(&self, buf: &mut [u8], flags: i32) -> AxResult { + pub fn recv(&self, buf: &mut [u8], _flags: i32) -> AxResult { loop { if self.inner.remote_closed { return Ok(0); @@ -470,7 +470,6 @@ impl TcpSocket { trace!("poll pcbstate: {:?}", unsafe { (*self.pcb.get()).state }); lwip_loop_once(); if unsafe { (*self.pcb.get()).state } == tcp_state_LISTEN { - let test = self.inner.accept_queue.lock().len(); // listener Ok(PollState { readable: self.inner.accept_queue.lock().len() != 0, @@ -478,7 +477,6 @@ impl TcpSocket { pollhup: false, }) } else { - let test = self.inner.recv_queue.lock().len(); // stream Ok(PollState { readable: self.inner.recv_queue.lock().len() != 0, diff --git a/modules/ruxnet/src/lwip_impl/udp.rs b/modules/ruxnet/src/lwip_impl/udp.rs index 0c5046517..d1c54f820 100644 --- a/modules/ruxnet/src/lwip_impl/udp.rs +++ b/modules/ruxnet/src/lwip_impl/udp.rs @@ -6,7 +6,7 @@ use alloc::{boxed::Box, collections::VecDeque}; use axerrno::{ax_err, AxError, AxResult}; use axio::PollState; use axsync::Mutex; -use core::sync::atomic::{AtomicBool, AtomicU8, Ordering}; +use core::sync::atomic::{AtomicBool, Ordering}; use core::{ffi::c_void, pin::Pin, ptr::null_mut}; use lwip_rust::bindings::{ err_enum_t_ERR_MEM, err_enum_t_ERR_OK, err_enum_t_ERR_RTE, err_enum_t_ERR_USE, @@ -253,7 +253,7 @@ impl UdpSocket { drop(guard); } - Ok((copy_len, addr.into())) + Ok((copy_len, addr)) }; drop(recv_queue); match res { diff --git a/modules/ruxnet/src/unix.rs b/modules/ruxnet/src/unix.rs index 5378241fb..53c6c7165 100644 --- a/modules/ruxnet/src/unix.rs +++ b/modules/ruxnet/src/unix.rs @@ -66,7 +66,7 @@ impl<'a> UnixSocketInner<'a> { } pub fn get_addr(&self) -> SocketAddrUnix { - self.addr.lock().clone() + *self.addr.lock() } pub fn get_peersocket(&self) -> Option { @@ -460,7 +460,7 @@ impl UnixSocket { let writable = { let mut binding = UNIX_TABLE.write(); let mut socket_inner = binding.get_mut(self.get_sockethandle()).unwrap().lock(); - if !socket_inner.get_peersocket().is_none() { + if socket_inner.get_peersocket().is_some() { socket_inner.set_state(UnixSocketStatus::Connected); true } else { @@ -694,10 +694,7 @@ impl UnixSocket { /// Checks if the socket is in a listening state. pub fn is_listening(&self) -> bool { let now_state = self.get_state(); - match now_state { - UnixSocketStatus::Listening => true, - _ => false, - } + matches!(now_state, UnixSocketStatus::Listening) } /// Returns the socket type of the `UnixSocket`. diff --git a/modules/ruxruntime/Cargo.toml b/modules/ruxruntime/Cargo.toml index d6823ee9e..3cbdb7615 100644 --- a/modules/ruxruntime/Cargo.toml +++ b/modules/ruxruntime/Cargo.toml @@ -20,7 +20,7 @@ smp = ["ruxhal/smp"] irq = ["ruxhal/irq", "ruxtask?/irq", "percpu", "kernel_guard"] tls = ["ruxhal/tls", "ruxtask?/tls"] alloc = ["axalloc", "dtb"] -paging = ["ruxhal/paging", "lazy_init", "ruxmm"] +paging = ["ruxhal/paging", "ruxmm/paging", "lazy_init", "ruxtask/paging"] rtc = ["ruxhal/rtc"] virtio_console = [] @@ -32,16 +32,18 @@ virtio-9p = ["fs", "rux9p"] net-9p = ["fs", "rux9p"] net = ["ruxdriver", "ruxnet"] display = ["ruxdriver", "ruxdisplay"] -signal = ["ruxhal/signal"] +signal = ["ruxhal/signal", "ruxtask/signal"] musl = ["dep:ruxfutex"] +# for testing +myfs = ["fs", "multitask", "alloc", "ruxfs/myfs", "ruxtask/test"] [dependencies] cfg-if = "1.0" ruxhal = { path = "../ruxhal" } axlog = { path = "../axlog" } -ruxconfig = { path = "../ruxconfig" } +ruxconfig = { path = "../ruxconfig"} axalloc = { path = "../axalloc", optional = true } ruxdriver = { path = "../ruxdriver", optional = true } ruxfs = { path = "../ruxfs", optional = true } @@ -49,7 +51,7 @@ rux9p = { path = "../rux9p", optional = true } ruxnet = { path = "../ruxnet", optional = true } ruxdisplay = { path = "../ruxdisplay", optional = true } ruxtask = { path = "../ruxtask", optional = true } -ruxmm = { path = "../ruxmm", optional = true } +ruxmm = { path = "../ruxmm" } axsync = { path = "../axsync", optional = true } ruxfutex = { path = "../ruxfutex", optional = true } ruxrand = { path = "../ruxrand", optional = true } @@ -61,3 +63,13 @@ lazy_init = { path = "../../crates/lazy_init", optional = true } dtb = { path = "../../crates/dtb", optional = true } tty = { path = "../../crates/tty", optional = true } + +[dev-dependencies] +log = "0.4" +ruxdriver = { path = "../ruxdriver", features = ["block", "ramdisk", "dyn"] } +driver_block = { path = "../../crates/driver_block", features = ["ramdisk"] } +axio = { path = "../../crates/axio", features = ["alloc"] } +ruxfs = { path = "../ruxfs" } +ruxtask = { path = "../ruxtask" } +axfs_vfs = { path = "../../crates/axfs_vfs" } +axfs_ramfs = { path = "../../crates/axfs_ramfs" } diff --git a/modules/ruxfs/resources/create_test_img.sh b/modules/ruxruntime/resources/create_test_img.sh similarity index 100% rename from modules/ruxfs/resources/create_test_img.sh rename to modules/ruxruntime/resources/create_test_img.sh diff --git a/modules/ruxfs/resources/fat16.img b/modules/ruxruntime/resources/fat16.img similarity index 100% rename from modules/ruxfs/resources/fat16.img rename to modules/ruxruntime/resources/fat16.img diff --git a/modules/ruxfs/tests/test_common/mod.rs b/modules/ruxruntime/tests/test_common/mod.rs similarity index 100% rename from modules/ruxfs/tests/test_common/mod.rs rename to modules/ruxruntime/tests/test_common/mod.rs diff --git a/modules/ruxfs/tests/test_fatfs.rs b/modules/ruxruntime/tests/test_fatfs.rs similarity index 100% rename from modules/ruxfs/tests/test_fatfs.rs rename to modules/ruxruntime/tests/test_fatfs.rs diff --git a/modules/ruxfs/tests/test_ramfs.rs b/modules/ruxruntime/tests/test_ramfs.rs similarity index 99% rename from modules/ruxfs/tests/test_ramfs.rs rename to modules/ruxruntime/tests/test_ramfs.rs index 61c57a45d..778de9938 100644 --- a/modules/ruxfs/tests/test_ramfs.rs +++ b/modules/ruxruntime/tests/test_ramfs.rs @@ -56,6 +56,7 @@ fn test_ramfs() { ruxtask::init_scheduler(); // call this to use `axsync::Mutex`. // By default, mount_points[0] will be rootfs + let mut mount_points: Vec = Vec::new(); // setup and initialize blkfs as one mountpoint for rootfs mount_points.push(ruxfs::init_blkfs(AxDeviceContainer::from_one(Box::new( diff --git a/modules/ruxtask/Cargo.toml b/modules/ruxtask/Cargo.toml index 386912fbb..cc1a9ba46 100644 --- a/modules/ruxtask/Cargo.toml +++ b/modules/ruxtask/Cargo.toml @@ -12,25 +12,26 @@ homepage = "https://github.com/syswonder/ruxos" repository = "https://github.com/syswonder/ruxos/tree/main/modules/ruxtask" [features] -default = [] +default = ["fs"] multitask = [ "dep:ruxconfig", "dep:percpu", "dep:spinlock", "dep:lazy_init", "dep:memory_addr", - "dep:scheduler", "dep:timer_list", "kernel_guard", "dep:crate_interface", + "dep:scheduler", "dep:timer_list", "kernel_guard", "dep:crate_interface" ] irq = [] tls = ["ruxhal/tls"] musl = [] preempt = ["irq", "percpu?/preempt", "kernel_guard/preempt"] -paging = [] +paging = ["ruxhal/paging"] fs = [] -signal = [] +signal = ["ruxhal/signal"] sched_fifo = ["multitask"] sched_rr = ["multitask", "preempt"] sched_cfs = ["multitask", "preempt"] test = ["percpu?/sp-naive"] +notest = [] [dependencies] cfg-if = "1.0" diff --git a/modules/ruxtask/src/api.rs b/modules/ruxtask/src/api.rs index 3981e3477..c4aa8f894 100644 --- a/modules/ruxtask/src/api.rs +++ b/modules/ruxtask/src/api.rs @@ -129,7 +129,7 @@ where } // temporarily only support aarch64 -#[cfg(target_arch = "aarch64")] +#[cfg(all(target_arch = "aarch64", feature = "paging", feature = "fs"))] pub fn fork_task() -> Option { use core::mem::ManuallyDrop; diff --git a/modules/ruxtask/src/fs.rs b/modules/ruxtask/src/fs.rs index be03efa29..b5eb30c7a 100644 --- a/modules/ruxtask/src/fs.rs +++ b/modules/ruxtask/src/fs.rs @@ -7,6 +7,10 @@ * See the Mulan PSL v2 for more details. */ +//! File system related functions. + +#![cfg(feature = "fs")] + use crate::current; use alloc::{format, string::String, sync::Arc, vec::Vec}; use axerrno::{ax_err, AxResult}; @@ -24,32 +28,48 @@ use ruxfdtable::RuxStat; use spin::RwLock; #[crate_interface::def_interface] +/// The interface for initializing the file system. pub trait InitFs { - fn init(task_inner: &mut FileSystem); + /// Initializes the file system. + fn add_stdios_to_fd_table(task_inner: &mut FileSystem); +} + +#[cfg(not(feature = "notest"))] +struct InitFsDefaultImpl; + +#[cfg(not(feature = "notest"))] +#[crate_interface::impl_interface] +impl InitFs for InitFsDefaultImpl { + fn add_stdios_to_fd_table(_task_inner: &mut FileSystem) { + // do nothing + } } +/// Initializes the file system. pub fn get_file_like(fd: i32) -> LinuxResult> { // let _exec = *MUST_EXEC; let binding_task = current(); let mut binding_fs = binding_task.fs.lock(); - let fd_table = &mut binding_fs.as_mut().unwrap().fd_table; - fd_table - .get(fd as usize) - .cloned() - .ok_or(LinuxError::EBADF) - .clone() + if let Some(fs) = binding_fs.as_mut() { + fs.fd_table + .get(fd as usize) + .cloned() + .ok_or(LinuxError::EBADF) + } else { + Err(LinuxError::EBADF) + } } +/// Adds a file like object to the file descriptor table and returns the file descriptor. pub fn add_file_like(f: Arc) -> LinuxResult { - // let _exec = *MUST_EXEC; let binding_task = current(); let mut binding_fs = binding_task.fs.lock(); - let fd_table = &mut binding_fs.as_mut().unwrap().fd_table; + let fd_table = &mut binding_fs.as_mut().expect("No fd table found").fd_table; Ok(fd_table.add(f).ok_or(LinuxError::EMFILE)? as i32) } +/// Removes a file like object from the file descriptor table. pub fn close_file_like(fd: i32) -> LinuxResult { - // let _exec = *MUST_EXEC; let binding_task = current(); let mut binding_fs = binding_task.fs.lock(); let fd_table = &mut binding_fs.as_mut().unwrap().fd_table; @@ -57,21 +77,26 @@ pub fn close_file_like(fd: i32) -> LinuxResult { Ok(()) } +/// A struct representing a file object. pub struct File { + /// The inner file object. pub inner: RwLock, } impl File { + /// Creates a new file object with the given inner file object. pub fn new(inner: ruxfs::fops::File) -> Self { Self { inner: RwLock::new(inner), } } + /// Adds the file object to the file descriptor table and returns the file descriptor. pub fn add_to_fd_table(self) -> LinuxResult { add_file_like(Arc::new(self)) } + /// Creates a new file object from the given file descriptor. pub fn from_fd(fd: i32) -> LinuxResult> { let f = get_file_like(fd)?; f.into_any() @@ -136,21 +161,26 @@ impl FileLike for File { } } +/// A struct representing a directory object. pub struct Directory { + /// The inner directory object. pub inner: RwLock, } impl Directory { + /// Creates a new directory object with the given inner directory object. pub fn new(inner: ruxfs::fops::Directory) -> Self { Self { inner: RwLock::new(inner), } } + /// Adds the directory object to the file descriptor table and returns the file descriptor. pub fn add_to_fd_table(self) -> LinuxResult { add_file_like(Arc::new(self)) } + /// Creates a new directory object from the given file descriptor. pub fn from_fd(fd: i32) -> LinuxResult> { let f = get_file_like(fd)?; f.into_any() @@ -212,13 +242,18 @@ pub const RUX_FILE_LIMIT: usize = 1024; /// A struct representing a file system object. pub struct FileSystem { + /// The file descriptor table. pub fd_table: FlattenObjects, RUX_FILE_LIMIT>, + /// The current working directory. pub current_path: String, + /// The current directory. pub current_dir: VfsNodeRef, + /// The root directory. pub root_dir: Arc, } impl FileSystem { + /// Closes all file objects in the file descriptor table. pub fn close_all_files(&mut self) { for fd in 0..self.fd_table.capacity() { if self.fd_table.get(fd).is_some() { @@ -248,6 +283,7 @@ impl Clone for FileSystem { } } +/// Initializes the file system. pub fn init_rootfs(mount_points: Vec) { let main_fs = mount_points .first() @@ -272,8 +308,11 @@ pub fn init_rootfs(mount_points: Vec) { current_dir: root_dir_arc.clone(), root_dir: root_dir_arc.clone(), }; + + // TODO: make a more clear interface for adding stdios to fd table when not in unit tests let fs_mutable = &mut fs; - crate_interface::call_interface!(InitFs::init, fs_mutable); + crate_interface::call_interface!(InitFs::add_stdios_to_fd_table, fs_mutable); + current().fs.lock().replace(fs); } @@ -286,6 +325,7 @@ fn parent_node_of(dir: Option<&VfsNodeRef>, path: &str) -> VfsNodeRef { } } +/// Returns the absolute path of the given path. pub fn absolute_path(path: &str) -> AxResult { if path.starts_with('/') { Ok(axfs_vfs::path::canonicalize(path)) @@ -295,10 +335,12 @@ pub fn absolute_path(path: &str) -> AxResult { } } +/// Returns the current directory. pub fn current_dir() -> AxResult { Ok(current().fs.lock().as_mut().unwrap().current_path.clone()) } +/// Sets the current directory. pub fn set_current_dir(path: &str) -> AxResult { let mut abs_path = absolute_path(path)?; if !abs_path.ends_with('/') { @@ -343,4 +385,13 @@ impl CurrentWorkingDirectoryOps for CurrentWorkingDirectoryImpl { fn set_current_dir(path: &str) -> AxResult { set_current_dir(path) } + fn root_dir() -> Arc { + current() + .fs + .lock() + .as_mut() + .expect("No filesystem found") + .root_dir + .clone() + } } diff --git a/modules/ruxtask/src/lib.rs b/modules/ruxtask/src/lib.rs index 8b1d5060f..038dbfa3a 100644 --- a/modules/ruxtask/src/lib.rs +++ b/modules/ruxtask/src/lib.rs @@ -54,7 +54,7 @@ cfg_if::cfg_if! { pub mod signal; #[cfg(feature = "paging")] pub mod vma; - // #[cfg(feature = "fs")] + #[cfg(feature = "fs")] pub mod fs; #[cfg(feature = "irq")] /// load average diff --git a/modules/ruxtask/src/run_queue.rs b/modules/ruxtask/src/run_queue.rs index 796ac1bf5..7da23165f 100644 --- a/modules/ruxtask/src/run_queue.rs +++ b/modules/ruxtask/src/run_queue.rs @@ -7,8 +7,8 @@ * See the Mulan PSL v2 for more details. */ -use crate::fs::get_file_like; -use crate::fs::RUX_FILE_LIMIT; +#[cfg(feature = "fs")] +use crate::fs::{get_file_like, RUX_FILE_LIMIT}; use alloc::collections::VecDeque; use alloc::sync::Arc; use axerrno::LinuxResult; @@ -245,6 +245,7 @@ impl AxRunQueue { } } +#[cfg(feature = "fs")] fn gc_flush_file(fd: usize) -> LinuxResult { trace!("gc task flush: {}", fd); get_file_like(fd as i32)?.flush() @@ -253,10 +254,13 @@ fn gc_flush_file(fd: usize) -> LinuxResult { fn gc_entry() { let mut now_file_fd: usize = 3; loop { - let _ = gc_flush_file(now_file_fd); - now_file_fd += 1; - if now_file_fd >= RUX_FILE_LIMIT { - now_file_fd = 3; + #[cfg(feature = "fs")] + { + let _ = gc_flush_file(now_file_fd); + now_file_fd += 1; + if now_file_fd >= RUX_FILE_LIMIT { + now_file_fd = 3; + } } // Drop all exited tasks and recycle resources. let n = EXITED_TASKS.lock().len(); diff --git a/modules/ruxtask/src/signal.rs b/modules/ruxtask/src/signal.rs index 4864fe136..b1d16f742 100644 --- a/modules/ruxtask/src/signal.rs +++ b/modules/ruxtask/src/signal.rs @@ -7,6 +7,8 @@ * See the Mulan PSL v2 for more details. */ +//! Signal module for RuxOS. + use crate::current; #[cfg(feature = "irq")] use core::sync::atomic::AtomicI64; @@ -15,7 +17,6 @@ use core::{ time::Duration, }; -use crate_interface::impl_interface; use ruxhal::trap::TrapHandler; /// sigaction in kernel @@ -60,9 +61,9 @@ unsafe extern "C" fn default_handler(signum: c_int) { #[cfg(feature = "signal")] struct SignalHandler; -#[impl_interface] +#[cfg(feature = "signal")] +#[crate_interface::impl_interface] impl TrapHandler for SignalHandler { - #[cfg(feature = "signal")] fn handle_signal() { let signal = Signal::signal(-1, true).unwrap(); for signum in 0..32 { diff --git a/modules/ruxtask/src/task.rs b/modules/ruxtask/src/task.rs index 304379656..889334e33 100644 --- a/modules/ruxtask/src/task.rs +++ b/modules/ruxtask/src/task.rs @@ -7,6 +7,8 @@ * See the Mulan PSL v2 for more details. */ +//! implementation of task structure and related functions. +#[cfg(feature = "fs")] use crate::fs::FileSystem; use alloc::collections::BTreeMap; use alloc::{ @@ -92,6 +94,7 @@ pub struct TaskInner { tsd: TSD, #[cfg(feature = "signal")] + /// The signal to be sent to the task. pub signal_if: Arc>, // set tid @@ -101,11 +104,13 @@ pub struct TaskInner { #[cfg(feature = "musl")] tl: AtomicU64, #[cfg(feature = "paging")] - // The page table of the task. + /// The page table of the task. pub pagetable: Arc>, - // file system + /// file system + #[cfg(feature = "fs")] pub fs: Arc>>, - // memory management + #[cfg(feature = "paging")] + /// memory management pub mm: Arc, } @@ -210,6 +215,7 @@ impl TaskInner { } } +/// map task id into task. pub static PROCESS_MAP: SpinNoIrq>> = SpinNoIrq::new(BTreeMap::new()); // private methods @@ -254,7 +260,9 @@ impl TaskInner { tl: AtomicU64::new(0), #[cfg(feature = "paging")] pagetable: current().pagetable.clone(), + #[cfg(feature = "fs")] fs: current().fs.clone(), + #[cfg(feature = "paging")] mm: current().mm.clone(), } } @@ -298,15 +306,19 @@ impl TaskInner { tl, #[cfg(feature = "paging")] pagetable: current().pagetable.clone(), + #[cfg(feature = "fs")] fs: current().fs.clone(), + #[cfg(feature = "paging")] mm: current().mm.clone(), } } + /// Create a new idle task. pub fn stack_top(&self) -> VirtAddr { self.kstack.lock().as_ref().as_ref().unwrap().top() } + /// Set the stack top and size for the task. pub fn set_stack_top(&self, begin: usize, size: usize) { debug!("set_stack_top: begin={:#x}, size={:#x}", begin, size); *self.stack_map_addr.lock() = VirtAddr::from(begin); @@ -375,7 +387,7 @@ impl TaskInner { Arc::new(AxTask::new(t)) } - #[cfg(target_arch = "aarch64")] + #[cfg(all(target_arch = "aarch64", feature = "paging", feature = "fs"))] /// only support for aarch64 pub fn fork() -> AxTaskRef { use crate::alloc::string::ToString; @@ -515,7 +527,9 @@ impl TaskInner { tl: AtomicU64::new(0), #[cfg(feature = "paging")] pagetable: Arc::new(SpinNoIrq::new(cloned_page_table)), + #[cfg(feature = "fs")] fs: Arc::new(SpinNoIrq::new(current_task.fs.lock().clone())), + #[cfg(feature = "paging")] mm: Arc::new(cloned_mm), }; @@ -557,8 +571,9 @@ impl TaskInner { /// /// And there is no need to set the `entry`, `kstack` or `tls` fields, as /// they will be filled automatically when the task is switches out. + #[allow(unused_mut)] pub(crate) fn new_init(name: String) -> AxTaskRef { - let mut t = Self { + let mut t: TaskInner = Self { parent_process: None, process_task: Weak::new(), id: TaskId::new(), @@ -593,22 +608,28 @@ impl TaskInner { pagetable: Arc::new(SpinNoIrq::new( PageTable::try_new().expect("failed to create page table"), )), + #[cfg(feature = "fs")] fs: Arc::new(SpinNoIrq::new(None)), + #[cfg(feature = "paging")] mm: Arc::new(MmapStruct::new()), }; debug!("new init task: {}", t.id_name()); - #[cfg(feature = "tls")] - let tls = VirtAddr::from(t.tls.tls_ptr() as usize); - #[cfg(not(feature = "tls"))] - let tls = VirtAddr::from(0); + #[cfg(feature = "notest")] + { + #[cfg(feature = "tls")] + let tls = VirtAddr::from(t.tls.tls_ptr() as usize); + #[cfg(not(feature = "tls"))] + let tls = VirtAddr::from(0); + + t.set_stack_top(boot_stack as usize, ruxconfig::TASK_STACK_SIZE); + t.ctx.get_mut().init( + task_entry as usize, + VirtAddr::from(boot_stack as usize), + tls, + ); + } - t.set_stack_top(boot_stack as usize, ruxconfig::TASK_STACK_SIZE); - t.ctx.get_mut().init( - task_entry as usize, - VirtAddr::from(boot_stack as usize), - tls, - ); let task_ref = Arc::new(AxTask::new(t)); PROCESS_MAP .lock() @@ -616,6 +637,7 @@ impl TaskInner { task_ref } + /// Create a new idle task. pub fn new_idle(name: String) -> AxTaskRef { const IDLE_STACK_SIZE: usize = 4096; let bindings = PROCESS_MAP.lock(); @@ -656,7 +678,9 @@ impl TaskInner { tl: AtomicU64::new(0), #[cfg(feature = "paging")] pagetable: task_ref.pagetable.clone(), + #[cfg(feature = "fs")] fs: task_ref.fs.clone(), + #[cfg(feature = "paging")] mm: task_ref.mm.clone(), }; @@ -825,12 +849,15 @@ impl fmt::Debug for TaskInner { } #[derive(Debug)] +/// A wrapper of TaskStack to provide a safe interface for allocating and +/// deallocating task stacks. pub struct TaskStack { ptr: NonNull, layout: Layout, } impl TaskStack { + /// Allocate a new task stack with the given size. pub fn alloc(size: usize) -> Self { let layout = Layout::from_size_align(size, 8).unwrap(); Self { @@ -839,19 +866,28 @@ impl TaskStack { } } + /// Deallocate the task stack. pub const fn top(&self) -> VirtAddr { unsafe { core::mem::transmute(self.ptr.as_ptr().add(self.layout.size())) } } + /// Deallocate the task stack. pub const fn end(&self) -> VirtAddr { unsafe { core::mem::transmute(self.ptr.as_ptr()) } } + /// Deallocate the task stack. pub fn size(&self) -> usize { self.layout.size() } } +impl Drop for TaskStack { + fn drop(&mut self) { + unsafe { alloc::alloc::dealloc(self.ptr.as_ptr(), self.layout) } + } +} + /// A wrapper of [`AxTaskRef`] as the current task. pub struct CurrentTask(ManuallyDrop); @@ -874,6 +910,7 @@ impl CurrentTask { &self.0 } + /// clone [`CurrentTask`] as [`AxTaskRef`]. pub fn clone_as_taskref(&self) -> AxTaskRef { self.0.deref().clone() } @@ -923,6 +960,7 @@ extern "C" fn task_entry() -> ! { crate::exit(0); } +#[cfg(feature = "notest")] extern "C" { fn boot_stack(); } diff --git a/modules/ruxtask/src/vma.rs b/modules/ruxtask/src/vma.rs index d0fe4184b..48e302f97 100644 --- a/modules/ruxtask/src/vma.rs +++ b/modules/ruxtask/src/vma.rs @@ -46,9 +46,12 @@ used_fs! { // pub(crate) const SWAP_MAX: usize = 1024 * 1024 * 1024; pub(crate) const SWAP_MAX: usize = 0; pub(crate) const SWAP_PATH: &str = "swap.raw\0"; + /// record the mapping of swapped out pages. pub static SWAPED_MAP: SpinNoIrq>> = SpinNoIrq::new(BTreeMap::new()); // Vaddr => (page_size, offset_at_swaped) lazy_static::lazy_static! { + /// swap file for swapping out pages. pub static ref SWAP_FILE: Arc = open_swap_file(SWAP_PATH); + /// bitmap for free pages in swap file. pub static ref BITMAP_FREE: SpinNoIrq> = SpinNoIrq::new((0..SWAP_MAX).step_by(PAGE_SIZE_4K).collect()); } } @@ -69,21 +72,27 @@ fn open_swap_file(filename: &str) -> Arc { /// Data structure for file mapping. #[derive(Clone)] pub struct FileInfo { + /// file that the mapping is backed by pub file: Arc, + /// offset in the file pub offset: usize, + /// size of the mapping pub size: usize, } /// Data structure for information of mapping. pub struct PageInfo { + /// physical address of the page pub paddr: PhysAddr, #[cfg(feature = "fs")] + /// file that the mapping is backed by pub mapping_file: Option, } /// Data structure for swaping out a page in a file. #[derive(Debug, Clone)] pub struct SwapInfo { + /// offset in the swap file pub offset: usize, } @@ -162,6 +171,7 @@ impl MmapStruct { /// Impl for Vma. impl Vma { + /// Create a new `Vma` instance. pub fn new(_fid: i32, offset: usize, prot: u32, flags: u32) -> Self { // #[cfg(feature = "fs")] let file = if _fid < 0 { @@ -187,6 +197,7 @@ impl Vma { } } + /// Clone a new `Vma` instance. pub fn clone_from(vma: &Vma, start_addr: usize, end_addr: usize) -> Self { Vma { start_addr, diff --git a/scripts/make/build.mk b/scripts/make/build.mk index aee2098af..3672ff47c 100644 --- a/scripts/make/build.mk +++ b/scripts/make/build.mk @@ -41,11 +41,7 @@ ifeq ($(APP_TYPE), rust) $(call cargo_build,--manifest-path $(APP)/Cargo.toml,$(RUX_FEAT) $(LIB_FEAT) $(APP_FEAT)) @cp $(rust_elf) $(OUT_ELF) else ifeq ($(APP_TYPE), c) - ifeq ($(MUSL), y) - $(call cargo_build,-p ruxmusl,$(RUX_FEAT) $(LIB_FEAT)) - else - $(call cargo_build,-p ruxlibc,$(RUX_FEAT) $(LIB_FEAT)) - endif + $(call cargo_build,-p ruxmusl,$(RUX_FEAT) $(LIB_FEAT)) endif $(OUT_DIR): diff --git a/scripts/make/build_c.mk b/scripts/make/build_c.mk index d1522f98a..148de9c82 100644 --- a/scripts/make/build_c.mk +++ b/scripts/make/build_c.mk @@ -1,7 +1,7 @@ -rust_lib_name := ruxlibc +rust_lib_name := ruxmusl rust_lib := target/$(TARGET)/$(MODE)/lib$(rust_lib_name).a -ulib_dir := ulib/ruxlibc +ulib_dir := ulib/ruxmusl src_dir := $(ulib_dir)/c obj_dir := $(ulib_dir)/build_$(ARCH) inc_dir := $(ulib_dir)/include diff --git a/scripts/make/cargo.mk b/scripts/make/cargo.mk index 308896d27..d2d84c2fd 100644 --- a/scripts/make/cargo.mk +++ b/scripts/make/cargo.mk @@ -40,7 +40,7 @@ endef all_packages := \ $(shell ls $(CURDIR)/crates) \ $(shell ls $(CURDIR)/modules) \ - ruxfeat arceos_api axstd ruxlibc ruxmusl + ruxfeat arceos_api axstd ruxmusl define cargo_doc $(call run_cmd,cargo doc,--no-deps --all-features --workspace --exclude "arceos-*" --exclude "ruxos-*" --exclude "ruxos-*" $(verbose)) diff --git a/scripts/make/features.mk b/scripts/make/features.mk index 4a07c7e5c..a79d5ac8d 100644 --- a/scripts/make/features.mk +++ b/scripts/make/features.mk @@ -3,21 +3,17 @@ # Inputs: # - `FEATURES`: a list of features to be enabled split by spaces or commas. # The features can be selected from the crate `ruxfeat` or the user library -# (crate `axstd` or `ruxlibc`). +# (crate `axstd` or `ruxmusl`). # - `APP_FEATURES`: a list of features to be enabled for the Rust app. # # Outputs: # - `RUX_FEAT`: features to be enabled for Ruxos modules (crate `ruxfeat`). -# - `LIB_FEAT`: features to be enabled for the user library (crate `axstd`, `ruxlibc`, `ruxmusl`). +# - `LIB_FEAT`: features to be enabled for the user library (crate `axstd`, `ruxmusl`, `ruxmusl`). # - `APP_FEAT`: features to be enabled for the Rust app. ifeq ($(APP_TYPE),c) ax_feat_prefix := ruxfeat/ - ifeq ($(MUSL), y) - lib_feat_prefix := ruxmusl/ - else - lib_feat_prefix := ruxlibc/ - endif + lib_feat_prefix := ruxmusl/ lib_features := fp_simd alloc irq sched_rr paging multitask fs net fd pipe select poll epoll random-hw signal else # TODO: it's better to use `ruxfeat/` as `ax_feat_prefix`, but all apps need to have `ruxfeat` as a dependency @@ -32,6 +28,11 @@ ifeq ($(APP_TYPE),c) endif override FEATURES := $(shell echo $(FEATURES) | tr ',' ' ') +# TODO: remove below features when it is possible +override FEATURES += multitask +override FEATURES += fs +override FEATURES += paging +override FEATURES += alloc ifeq ($(APP_TYPE), c) ifneq ($(wildcard $(APP)/features.txt),) # check features.txt exists diff --git a/scripts/make/test.mk b/scripts/make/test.mk index b132654a6..8dcde7b4f 100644 --- a/scripts/make/test.mk +++ b/scripts/make/test.mk @@ -1,8 +1,10 @@ # Test scripts +# for unit tests, try to run the tests + define unit_test - $(call run_cmd,cargo test,-p percpu $(1) -- --nocapture) - $(call run_cmd,cargo test,-p ruxfs $(1) --features "myfs" -- --nocapture) + $(call run_cmd,cargo test,-p percpu $(1) -- --nocapture) + $(call run_cmd,cargo test,-p ruxruntime $(1) --features "myfs" -- --nocapture) $(call run_cmd,cargo test,--workspace --exclude lwip_rust --exclude "arceos-*" --exclude "ruxos-*" $(1) -- --nocapture) endef diff --git a/scripts/test/app_test.sh b/scripts/test/app_test.sh index b81ae0885..598ffcf53 100755 --- a/scripts/test/app_test.sh +++ b/scripts/test/app_test.sh @@ -108,8 +108,8 @@ if [ -z "$1" ]; then test_list=( "apps/c/helloworld" "apps/c/memtest" - "apps/c/sqlite3" "apps/c/httpclient" + "apps/c/sqlite3" "apps/c/pthread/basic" "apps/c/pthread/sleep" "apps/c/pthread/pipe" diff --git a/ulib/axstd/Cargo.toml b/ulib/axstd/Cargo.toml index 7c604d674..c583baf4a 100644 --- a/ulib/axstd/Cargo.toml +++ b/ulib/axstd/Cargo.toml @@ -36,7 +36,7 @@ alloc = ["arceos_api/alloc", "ruxfeat/alloc", "axio/alloc"] alloc-tlsf = ["ruxfeat/alloc-tlsf"] alloc-slab = ["ruxfeat/alloc-slab"] alloc-buddy = ["ruxfeat/alloc-buddy"] -paging = ["ruxfeat/paging"] +paging = ["ruxfeat/paging", "arceos_api/paging"] tls = ["ruxfeat/tls"] # Multi-threading and scheduler diff --git a/ulib/include/.gitignore b/ulib/include/.gitignore new file mode 100644 index 000000000..9a162c2b8 --- /dev/null +++ b/ulib/include/.gitignore @@ -0,0 +1,2 @@ +ax_pthread_cond.h +ax_pthread_mutex.h diff --git a/ulib/ruxlibc/include/arpa/inet.h b/ulib/include/arpa/inet.h similarity index 100% rename from ulib/ruxlibc/include/arpa/inet.h rename to ulib/include/arpa/inet.h diff --git a/ulib/ruxlibc/include/assert.h b/ulib/include/assert.h similarity index 100% rename from ulib/ruxlibc/include/assert.h rename to ulib/include/assert.h diff --git a/ulib/ruxlibc/include/byteswap.h b/ulib/include/byteswap.h similarity index 100% rename from ulib/ruxlibc/include/byteswap.h rename to ulib/include/byteswap.h diff --git a/ulib/ruxlibc/include/crypt.h b/ulib/include/crypt.h similarity index 100% rename from ulib/ruxlibc/include/crypt.h rename to ulib/include/crypt.h diff --git a/ulib/ruxlibc/include/ctype.h b/ulib/include/ctype.h similarity index 100% rename from ulib/ruxlibc/include/ctype.h rename to ulib/include/ctype.h diff --git a/ulib/ruxlibc/include/dirent.h b/ulib/include/dirent.h similarity index 100% rename from ulib/ruxlibc/include/dirent.h rename to ulib/include/dirent.h diff --git a/ulib/ruxlibc/include/dlfcn.h b/ulib/include/dlfcn.h similarity index 100% rename from ulib/ruxlibc/include/dlfcn.h rename to ulib/include/dlfcn.h diff --git a/ulib/ruxlibc/include/endian.h b/ulib/include/endian.h similarity index 100% rename from ulib/ruxlibc/include/endian.h rename to ulib/include/endian.h diff --git a/ulib/ruxlibc/include/errno.h b/ulib/include/errno.h similarity index 100% rename from ulib/ruxlibc/include/errno.h rename to ulib/include/errno.h diff --git a/ulib/ruxlibc/include/fcntl.h b/ulib/include/fcntl.h similarity index 100% rename from ulib/ruxlibc/include/fcntl.h rename to ulib/include/fcntl.h diff --git a/ulib/ruxlibc/include/features.h b/ulib/include/features.h similarity index 100% rename from ulib/ruxlibc/include/features.h rename to ulib/include/features.h diff --git a/ulib/ruxlibc/include/float.h b/ulib/include/float.h similarity index 100% rename from ulib/ruxlibc/include/float.h rename to ulib/include/float.h diff --git a/ulib/ruxlibc/include/fnmatch.h b/ulib/include/fnmatch.h similarity index 100% rename from ulib/ruxlibc/include/fnmatch.h rename to ulib/include/fnmatch.h diff --git a/ulib/ruxlibc/include/glob.h b/ulib/include/glob.h similarity index 100% rename from ulib/ruxlibc/include/glob.h rename to ulib/include/glob.h diff --git a/ulib/ruxlibc/include/grp.h b/ulib/include/grp.h similarity index 100% rename from ulib/ruxlibc/include/grp.h rename to ulib/include/grp.h diff --git a/ulib/ruxlibc/include/inttypes.h b/ulib/include/inttypes.h similarity index 100% rename from ulib/ruxlibc/include/inttypes.h rename to ulib/include/inttypes.h diff --git a/ulib/ruxlibc/include/ksigaction.h b/ulib/include/ksigaction.h similarity index 100% rename from ulib/ruxlibc/include/ksigaction.h rename to ulib/include/ksigaction.h diff --git a/ulib/ruxlibc/include/langinfo.h b/ulib/include/langinfo.h similarity index 100% rename from ulib/ruxlibc/include/langinfo.h rename to ulib/include/langinfo.h diff --git a/ulib/ruxlibc/include/libgen.h b/ulib/include/libgen.h similarity index 100% rename from ulib/ruxlibc/include/libgen.h rename to ulib/include/libgen.h diff --git a/ulib/ruxlibc/include/limits.h b/ulib/include/limits.h similarity index 100% rename from ulib/ruxlibc/include/limits.h rename to ulib/include/limits.h diff --git a/ulib/ruxlibc/include/locale.h b/ulib/include/locale.h similarity index 100% rename from ulib/ruxlibc/include/locale.h rename to ulib/include/locale.h diff --git a/ulib/ruxlibc/include/malloc.h b/ulib/include/malloc.h similarity index 100% rename from ulib/ruxlibc/include/malloc.h rename to ulib/include/malloc.h diff --git a/ulib/ruxlibc/include/math.h b/ulib/include/math.h similarity index 100% rename from ulib/ruxlibc/include/math.h rename to ulib/include/math.h diff --git a/ulib/ruxlibc/include/memory.h b/ulib/include/memory.h similarity index 100% rename from ulib/ruxlibc/include/memory.h rename to ulib/include/memory.h diff --git a/ulib/ruxlibc/include/netdb.h b/ulib/include/netdb.h similarity index 100% rename from ulib/ruxlibc/include/netdb.h rename to ulib/include/netdb.h diff --git a/ulib/ruxlibc/include/netinet/in.h b/ulib/include/netinet/in.h similarity index 100% rename from ulib/ruxlibc/include/netinet/in.h rename to ulib/include/netinet/in.h diff --git a/ulib/ruxlibc/include/netinet/tcp.h b/ulib/include/netinet/tcp.h similarity index 100% rename from ulib/ruxlibc/include/netinet/tcp.h rename to ulib/include/netinet/tcp.h diff --git a/ulib/ruxlibc/include/nscd.h b/ulib/include/nscd.h similarity index 100% rename from ulib/ruxlibc/include/nscd.h rename to ulib/include/nscd.h diff --git a/ulib/ruxlibc/include/poll.h b/ulib/include/poll.h similarity index 100% rename from ulib/ruxlibc/include/poll.h rename to ulib/include/poll.h diff --git a/ulib/ruxlibc/include/pthread.h b/ulib/include/pthread.h similarity index 100% rename from ulib/ruxlibc/include/pthread.h rename to ulib/include/pthread.h diff --git a/ulib/ruxlibc/include/pwd.h b/ulib/include/pwd.h similarity index 100% rename from ulib/ruxlibc/include/pwd.h rename to ulib/include/pwd.h diff --git a/ulib/ruxlibc/include/regex.h b/ulib/include/regex.h similarity index 100% rename from ulib/ruxlibc/include/regex.h rename to ulib/include/regex.h diff --git a/ulib/ruxlibc/include/sched.h b/ulib/include/sched.h similarity index 100% rename from ulib/ruxlibc/include/sched.h rename to ulib/include/sched.h diff --git a/ulib/ruxlibc/include/semaphore.h b/ulib/include/semaphore.h similarity index 100% rename from ulib/ruxlibc/include/semaphore.h rename to ulib/include/semaphore.h diff --git a/ulib/ruxlibc/include/setjmp.h b/ulib/include/setjmp.h similarity index 100% rename from ulib/ruxlibc/include/setjmp.h rename to ulib/include/setjmp.h diff --git a/ulib/ruxlibc/include/signal.h b/ulib/include/signal.h similarity index 100% rename from ulib/ruxlibc/include/signal.h rename to ulib/include/signal.h diff --git a/ulib/ruxlibc/include/stdarg.h b/ulib/include/stdarg.h similarity index 100% rename from ulib/ruxlibc/include/stdarg.h rename to ulib/include/stdarg.h diff --git a/ulib/ruxlibc/include/stdbool.h b/ulib/include/stdbool.h similarity index 100% rename from ulib/ruxlibc/include/stdbool.h rename to ulib/include/stdbool.h diff --git a/ulib/ruxlibc/include/stddef.h b/ulib/include/stddef.h similarity index 100% rename from ulib/ruxlibc/include/stddef.h rename to ulib/include/stddef.h diff --git a/ulib/ruxlibc/include/stdint.h b/ulib/include/stdint.h similarity index 100% rename from ulib/ruxlibc/include/stdint.h rename to ulib/include/stdint.h diff --git a/ulib/ruxlibc/include/stdio.h b/ulib/include/stdio.h similarity index 100% rename from ulib/ruxlibc/include/stdio.h rename to ulib/include/stdio.h diff --git a/ulib/ruxlibc/include/stdlib.h b/ulib/include/stdlib.h similarity index 100% rename from ulib/ruxlibc/include/stdlib.h rename to ulib/include/stdlib.h diff --git a/ulib/ruxlibc/include/string.h b/ulib/include/string.h similarity index 100% rename from ulib/ruxlibc/include/string.h rename to ulib/include/string.h diff --git a/ulib/ruxlibc/include/strings.h b/ulib/include/strings.h similarity index 100% rename from ulib/ruxlibc/include/strings.h rename to ulib/include/strings.h diff --git a/ulib/ruxlibc/include/sys/epoll.h b/ulib/include/sys/epoll.h similarity index 100% rename from ulib/ruxlibc/include/sys/epoll.h rename to ulib/include/sys/epoll.h diff --git a/ulib/ruxlibc/include/sys/eventfd.h b/ulib/include/sys/eventfd.h similarity index 100% rename from ulib/ruxlibc/include/sys/eventfd.h rename to ulib/include/sys/eventfd.h diff --git a/ulib/ruxlibc/include/sys/file.h b/ulib/include/sys/file.h similarity index 100% rename from ulib/ruxlibc/include/sys/file.h rename to ulib/include/sys/file.h diff --git a/ulib/ruxlibc/include/sys/ioctl.h b/ulib/include/sys/ioctl.h similarity index 100% rename from ulib/ruxlibc/include/sys/ioctl.h rename to ulib/include/sys/ioctl.h diff --git a/ulib/ruxlibc/include/sys/mman.h b/ulib/include/sys/mman.h similarity index 100% rename from ulib/ruxlibc/include/sys/mman.h rename to ulib/include/sys/mman.h diff --git a/ulib/ruxlibc/include/sys/param.h b/ulib/include/sys/param.h similarity index 100% rename from ulib/ruxlibc/include/sys/param.h rename to ulib/include/sys/param.h diff --git a/ulib/ruxlibc/include/sys/prctl.h b/ulib/include/sys/prctl.h similarity index 100% rename from ulib/ruxlibc/include/sys/prctl.h rename to ulib/include/sys/prctl.h diff --git a/ulib/ruxlibc/include/sys/random.h b/ulib/include/sys/random.h similarity index 100% rename from ulib/ruxlibc/include/sys/random.h rename to ulib/include/sys/random.h diff --git a/ulib/ruxlibc/include/sys/resource.h b/ulib/include/sys/resource.h similarity index 100% rename from ulib/ruxlibc/include/sys/resource.h rename to ulib/include/sys/resource.h diff --git a/ulib/ruxlibc/include/sys/select.h b/ulib/include/sys/select.h similarity index 100% rename from ulib/ruxlibc/include/sys/select.h rename to ulib/include/sys/select.h diff --git a/ulib/ruxlibc/include/sys/sendfile.h b/ulib/include/sys/sendfile.h similarity index 100% rename from ulib/ruxlibc/include/sys/sendfile.h rename to ulib/include/sys/sendfile.h diff --git a/ulib/ruxlibc/include/sys/socket.h b/ulib/include/sys/socket.h similarity index 100% rename from ulib/ruxlibc/include/sys/socket.h rename to ulib/include/sys/socket.h diff --git a/ulib/ruxlibc/include/sys/stat.h b/ulib/include/sys/stat.h similarity index 100% rename from ulib/ruxlibc/include/sys/stat.h rename to ulib/include/sys/stat.h diff --git a/ulib/ruxlibc/include/sys/statfs.h b/ulib/include/sys/statfs.h similarity index 100% rename from ulib/ruxlibc/include/sys/statfs.h rename to ulib/include/sys/statfs.h diff --git a/ulib/ruxlibc/include/sys/statvfs.h b/ulib/include/sys/statvfs.h similarity index 100% rename from ulib/ruxlibc/include/sys/statvfs.h rename to ulib/include/sys/statvfs.h diff --git a/ulib/ruxlibc/include/sys/syscall.h b/ulib/include/sys/syscall.h similarity index 100% rename from ulib/ruxlibc/include/sys/syscall.h rename to ulib/include/sys/syscall.h diff --git a/ulib/ruxlibc/include/sys/sysinfo.h b/ulib/include/sys/sysinfo.h similarity index 100% rename from ulib/ruxlibc/include/sys/sysinfo.h rename to ulib/include/sys/sysinfo.h diff --git a/ulib/ruxlibc/include/sys/time.h b/ulib/include/sys/time.h similarity index 100% rename from ulib/ruxlibc/include/sys/time.h rename to ulib/include/sys/time.h diff --git a/ulib/ruxlibc/include/sys/types.h b/ulib/include/sys/types.h similarity index 100% rename from ulib/ruxlibc/include/sys/types.h rename to ulib/include/sys/types.h diff --git a/ulib/ruxlibc/include/sys/uio.h b/ulib/include/sys/uio.h similarity index 100% rename from ulib/ruxlibc/include/sys/uio.h rename to ulib/include/sys/uio.h diff --git a/ulib/ruxlibc/include/sys/un.h b/ulib/include/sys/un.h similarity index 100% rename from ulib/ruxlibc/include/sys/un.h rename to ulib/include/sys/un.h diff --git a/ulib/ruxlibc/include/sys/utsname.h b/ulib/include/sys/utsname.h similarity index 100% rename from ulib/ruxlibc/include/sys/utsname.h rename to ulib/include/sys/utsname.h diff --git a/ulib/ruxlibc/include/sys/vfs.h b/ulib/include/sys/vfs.h similarity index 100% rename from ulib/ruxlibc/include/sys/vfs.h rename to ulib/include/sys/vfs.h diff --git a/ulib/ruxlibc/include/sys/wait.h b/ulib/include/sys/wait.h similarity index 100% rename from ulib/ruxlibc/include/sys/wait.h rename to ulib/include/sys/wait.h diff --git a/ulib/ruxlibc/include/syslog.h b/ulib/include/syslog.h similarity index 100% rename from ulib/ruxlibc/include/syslog.h rename to ulib/include/syslog.h diff --git a/ulib/ruxlibc/include/termios.h b/ulib/include/termios.h similarity index 100% rename from ulib/ruxlibc/include/termios.h rename to ulib/include/termios.h diff --git a/ulib/ruxlibc/include/time.h b/ulib/include/time.h similarity index 100% rename from ulib/ruxlibc/include/time.h rename to ulib/include/time.h diff --git a/ulib/ruxlibc/include/unistd.h b/ulib/include/unistd.h similarity index 100% rename from ulib/ruxlibc/include/unistd.h rename to ulib/include/unistd.h diff --git a/ulib/ruxlibc/.gitignore b/ulib/ruxlibc/.gitignore deleted file mode 100644 index 792f33c1e..000000000 --- a/ulib/ruxlibc/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -src/libctypes_gen.rs -include/ax_pthread_mutex.h -include/ax_pthread_cond.h -build_* diff --git a/ulib/ruxlibc/Cargo.toml b/ulib/ruxlibc/Cargo.toml deleted file mode 100644 index 8b00b811f..000000000 --- a/ulib/ruxlibc/Cargo.toml +++ /dev/null @@ -1,67 +0,0 @@ -[package] -name = "ruxlibc" -version = "0.1.0" -edition = "2021" -authors = [ - "Yuekai Jia ", - "yanjuguang ", - "wudashuai ", - "yfblock <321353225@qq.com>", - "scPointer ", - "Shiping Yuan ", -] -description = "Ruxos user program library for C apps" -license = "GPL-3.0-or-later OR Apache-2.0" -homepage = "https://github.com/syswonder/ruxos" -repository = "https://github.com/syswonder/ruxos/tree/main/ulib/ruxlibc" - -[lib] -crate-type = ["staticlib"] - -[features] -default = [] - -# Multicore -smp = ["ruxos_posix_api/smp"] - -# Floating point/SIMD -fp_simd = ["ruxfeat/fp_simd"] - -# Memory -alloc = ["ruxos_posix_api/alloc"] -paging = ["alloc", "ruxos_posix_api/paging"] -tls = ["alloc", "ruxfeat/tls"] - -# Multi-task -multitask = ["ruxos_posix_api/multitask"] - -# File system -fs = ["ruxos_posix_api/fs", "fd"] - -# Networking -net = ["ruxos_posix_api/net", "fd"] - -# Signal -signal = ["ruxos_posix_api/signal"] - -# Libc features -fd = [] -pipe = ["ruxos_posix_api/pipe"] -select = ["ruxos_posix_api/select"] -poll = ["ruxos_posix_api/poll"] -epoll = ["ruxos_posix_api/epoll"] -random-hw = ["ruxos_posix_api/random-hw"] - -# Interrupts -irq = ["ruxos_posix_api/irq", "ruxfeat/irq"] - -sched_rr = ["irq", "ruxfeat/sched_rr"] - -[dependencies] -ruxfeat = { path = "../../api/ruxfeat" } -ruxos_posix_api = { path = "../../api/ruxos_posix_api" } -axio = { path = "../../crates/axio" } -axerrno = { path = "../../crates/axerrno" } - -[build-dependencies] -bindgen ={ version = "0.66" } diff --git a/ulib/ruxlibc/build.rs b/ulib/ruxlibc/build.rs deleted file mode 100644 index 7a8501edd..000000000 --- a/ulib/ruxlibc/build.rs +++ /dev/null @@ -1,33 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -fn main() { - fn gen_c_to_rust_bindings(in_file: &str, out_file: &str) { - println!("cargo:rerun-if-changed={in_file}"); - - let allow_types = ["tm", "jmp_buf"]; - let mut builder = bindgen::Builder::default() - .header(in_file) - .clang_arg("-isystem./include") - .derive_default(true) - .size_t_is_usize(false) - .use_core(); - for ty in allow_types { - builder = builder.allowlist_type(ty); - } - - builder - .generate() - .expect("Unable to generate c->rust bindings") - .write_to_file(out_file) - .expect("Couldn't write bindings!"); - } - - gen_c_to_rust_bindings("ctypes.h", "src/libctypes_gen.rs"); -} diff --git a/ulib/ruxlibc/c/assert.c b/ulib/ruxlibc/c/assert.c deleted file mode 100644 index 4d9060c17..000000000 --- a/ulib/ruxlibc/c/assert.c +++ /dev/null @@ -1,17 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include - -_Noreturn void __assert_fail(const char *expr, const char *file, int line, const char *func) -{ - fprintf(stderr, "Assertion failed: %s (%s: %s: %d)\n", expr, file, func, line); - abort(); -} diff --git a/ulib/ruxlibc/c/ctype.c b/ulib/ruxlibc/c/ctype.c deleted file mode 100644 index 3ec36db30..000000000 --- a/ulib/ruxlibc/c/ctype.c +++ /dev/null @@ -1,26 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include -#include - -int tolower(int c) -{ - if (isupper(c)) - return c | 32; - return c; -} - -int toupper(int c) -{ - if (islower(c)) - return c & 0x5f; - return c; -} diff --git a/ulib/ruxlibc/c/dirent.c b/ulib/ruxlibc/c/dirent.c deleted file mode 100644 index da9fc05e8..000000000 --- a/ulib/ruxlibc/c/dirent.c +++ /dev/null @@ -1,107 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#ifdef RUX_CONFIG_FS - -#include -#include -#include -#include -#include -#include -#include -#include - -int closedir(DIR *dir) -{ - int ret = close(dir->fd); - free(dir); - return ret; -} - -DIR *fdopendir(int fd) -{ - DIR *dir; - struct stat st; - - if (fstat(fd, &st) < 0) { - return 0; - } - if (fcntl(fd, F_GETFL) & O_PATH) { - errno = EBADF; - return 0; - } - if (!S_ISDIR(st.st_mode)) { - errno = ENOTDIR; - return 0; - } - if (!(dir = calloc(1, sizeof(*dir)))) { - return 0; - } - - fcntl(fd, F_SETFD, FD_CLOEXEC); - dir->fd = fd; - return dir; -} - -int dirfd(DIR *d) -{ - return d->fd; -} - -// TODO -DIR *opendir(const char *__name) -{ - unimplemented(); - return NULL; -} - -// TODO -struct dirent *readdir(DIR *__dirp) -{ - unimplemented(); - return NULL; -} - -// TODO -int readdir_r(DIR *restrict dir, struct dirent *restrict buf, struct dirent **restrict result) -{ - struct dirent *de; - int errno_save = errno; - int ret; - - // LOCK(dir->lock); - errno = 0; - de = readdir(dir); - if ((ret = errno)) { - // UNLOCK(dir->lock); - return ret; - } - errno = errno_save; - if (de) - memcpy(buf, de, de->d_reclen); - else - buf = NULL; - - // UNLOCK(dir->lock); - *result = buf; - return 0; -} - -// TODO -void rewinddir(DIR *dir) -{ - // LOCK(dir->lock); - lseek(dir->fd, 0, SEEK_SET); - dir->buf_pos = dir->buf_end = 0; - dir->tell = 0; - // UNLOCK(dir->lock); -} - -#endif // RUX_CONFIG_FS diff --git a/ulib/ruxlibc/c/dlfcn.c b/ulib/ruxlibc/c/dlfcn.c deleted file mode 100644 index 5fc088d02..000000000 --- a/ulib/ruxlibc/c/dlfcn.c +++ /dev/null @@ -1,48 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include -#include - -// TODO -int dladdr(const void *__address, Dl_info *__info) -{ - unimplemented(); - return 0; -} - -// TODO -void *dlopen(const char *__file, int __mode) -{ - unimplemented(); - return NULL; -} - -// TODO -char *dlerror() -{ - unimplemented(); - return NULL; -} - -// TODO -void *dlsym(void *__restrict__ __handle, const char *__restrict__ __name) -{ - - unimplemented(); - return NULL; -} - -// TODO -int dlclose(void *p) -{ - unimplemented(); - return 0; -} diff --git a/ulib/ruxlibc/c/fcntl.c b/ulib/ruxlibc/c/fcntl.c deleted file mode 100644 index 1c2aeedad..000000000 --- a/ulib/ruxlibc/c/fcntl.c +++ /dev/null @@ -1,71 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include -#include - -#ifdef RUX_CONFIG_FD - -// TODO: remove this function in future work -int ax_fcntl(int fd, int cmd, size_t arg); - -int fcntl(int fd, int cmd, ... /* arg */) -{ - unsigned long arg; - va_list ap; - va_start(ap, cmd); - arg = va_arg(ap, unsigned long); - va_end(ap); - - return ax_fcntl(fd, cmd, arg); -} - -#endif // RUX_CONFIG_FD - -#ifdef RUX_CONFIG_FS - -// TODO: remove this function in future work -int ax_open(const char *filename, int flags, mode_t mode); - -int open(const char *filename, int flags, ...) -{ - mode_t mode = 0; - - if ((flags & O_CREAT) || (flags & O_TMPFILE) == O_TMPFILE) { - va_list ap; - va_start(ap, flags); - mode = va_arg(ap, mode_t); - va_end(ap); - } - - return ax_open(filename, flags, mode); -} - -// TODO -int posix_fadvise(int __fd, unsigned long __offset, unsigned long __len, int __advise) -{ - unimplemented(); - return 0; -} - -// TODO -int sync_file_range(int fd, off_t pos, off_t len, unsigned flags) -{ - unimplemented(); - return 0; -} - -int openat(int dirfd, const char *pathname, int flags, ...) -{ - unimplemented(); - return 0; -} - -#endif // RUX_CONFIG_FS diff --git a/ulib/ruxlibc/c/flock.c b/ulib/ruxlibc/c/flock.c deleted file mode 100644 index 36859d25c..000000000 --- a/ulib/ruxlibc/c/flock.c +++ /dev/null @@ -1,18 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include - -// TODO -int flock(int __fd, int __operation) -{ - unimplemented(); - return 0; -} diff --git a/ulib/ruxlibc/c/fnmatch.c b/ulib/ruxlibc/c/fnmatch.c deleted file mode 100644 index 7e88e5970..000000000 --- a/ulib/ruxlibc/c/fnmatch.c +++ /dev/null @@ -1,304 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include -#include -#include - -#define END 0 -#define UNMATCHABLE -2 -#define BRACKET -3 -#define QUESTION -4 -#define STAR -5 - -// TODO: When wide characters implemented, remove annotations according to musl. - -static int str_next(const char *str, size_t n, size_t *step) -{ - if (!n) { - *step = 0; - return 0; - } - // TODO: When wide characters implemented, remove annotation - // if (str[0] >= 128U) { - // wchar_t wc; - // int k = mbtowc(&wc, str, n); - // if (k<0) { - // *step = 1; - // return -1; - // } - // *step = k; - // return wc; - // } - *step = 1; - return str[0]; -} - -static int pat_next(const char *pat, size_t m, size_t *step, int flags) -{ - if (!m || !*pat) { - *step = 0; - return END; - } - *step = 1; - if (pat[0]=='\\' && pat[1] && !(flags & FNM_NOESCAPE)) { - *step = 2; - pat++; - goto escaped; - } - if (pat[0]=='[') { - size_t k = 1; - if (k= 128U) { - // wchar_t wc; - // int k = mbtowc(&wc, pat, m); - // if (k<0) { - // *step = 0; - // return UNMATCHABLE; - // } - // *step = k + esc; - // return wc; - // } - return pat[0]; -} - -static int casefold(int k) -{ - int c = toupper(k); - return c == k ? tolower(k) : c; - -} - -static int match_bracket(const char *p, int k, int kfold) -{ - // TODO: When wide characters implemented, remove annotation and delete next line - // wchar_t wc; - char wc; - int inv = 0; - p++; - if (*p=='^' || *p=='!') { - inv = 1; - p++; - } - if (*p==']') { - if (k==']') return !inv; - p++; - } else if (*p=='-') { - if (k=='-') return !inv; - p++; - } - wc = p[-1]; - for (; *p != ']'; p++) { - if (p[0]=='-' && p[1]!=']') { - // TODO: When wide characters implemented, remove annotation - // wchar_t wc2; - // int l = mbtowc(&wc2, p+1, 4); - // if (l < 0) return 0; - char wc2 = p[1]; - if (wc <= wc2) - if ((unsigned)k-wc <= wc2-wc || - (unsigned)kfold-wc <= wc2-wc) - return !inv; - // TODO: When wide characters implemented, remove annotation and delete next line - // p += l-1; - p += 1; - continue; - } - if (*p < 128U) { - wc = (unsigned char)*p; - } - if (wc==k || wc==kfold) return !inv; - } - return inv; -} - -static int fnmatch_internal(const char *pat, size_t m, const char *str, size_t n, int flags) -{ - const char *p, *ptail, *endpat; - const char *s, *stail, *endstr; - size_t pinc, sinc, tailcnt=0; - int c, k, kfold; - - if (flags & FNM_PERIOD) { - if (*str == '.' && *pat != '.') - return FNM_NOMATCH; - } - for (;;) { - switch ((c = pat_next(pat, m, &pinc, flags))) { - case UNMATCHABLE: - return FNM_NOMATCH; - case STAR: - pat++; - m--; - break; - default: - k = str_next(str, n, &sinc); - if (k <= 0) - return (c==END) ? 0 : FNM_NOMATCH; - str += sinc; - n -= sinc; - kfold = flags & FNM_CASEFOLD ? casefold(k) : k; - if (c == BRACKET) { - if (!match_bracket(pat, k, kfold)) - return FNM_NOMATCH; - } else if (c != QUESTION && k != c && kfold != c) { - return FNM_NOMATCH; - } - pat+=pinc; - m-=pinc; - continue; - } - break; - } - - /* Compute real pat length if it was initially unknown/-1 */ - m = strnlen(pat, m); - endpat = pat + m; - - /* Find the last * in pat and count chars needed after it */ - for (p=ptail=pat; pstr && tailcnt; tailcnt--) { - // TODO: When wide characters implemented, remove annotation - // if (s[-1] < 128U || MB_CUR_MAX==1) s--; - // else while ((unsigned char)*--s-0x80U<0x40 && s>str); - s--; - } - if (tailcnt) return FNM_NOMATCH; - stail = s; - - /* Check that the pat and str tails match */ - p = ptail; - for (;;) { - c = pat_next(p, endpat-p, &pinc, flags); - p += pinc; - if ((k = str_next(s, endstr-s, &sinc)) <= 0) { - if (c != END) return FNM_NOMATCH; - break; - } - s += sinc; - kfold = flags & FNM_CASEFOLD ? casefold(k) : k; - if (c == BRACKET) { - if (!match_bracket(p-pinc, k, kfold)) - return FNM_NOMATCH; - } else if (c != QUESTION && k != c && kfold != c) { - return FNM_NOMATCH; - } - } - - /* We're all done with the tails now, so throw them out */ - endstr = stail; - endpat = ptail; - - /* Match pattern components until there are none left */ - while (pat 0) str += sinc; - else for (str++; str_next(str, endstr-str, &sinc)<0; str++); - } - - return 0; -} - -int fnmatch(const char *pat, const char *str, int flags) -{ - const char *s, *p; - size_t inc; - int c; - if (flags & FNM_PATHNAME) for (;;) { - for (s=str; *s && *s!='/'; s++); - for (p=pat; (c=pat_next(p, -1, &inc, flags))!=END && c!='/'; p+=inc); - if (c!=*s && (!*s || !(flags & FNM_LEADING_DIR))) - return FNM_NOMATCH; - if (fnmatch_internal(pat, p-pat, str, s-str, flags)) - return FNM_NOMATCH; - if (!c) return 0; - str = s+1; - pat = p+inc; - } else if (flags & FNM_LEADING_DIR) { - for (s=str; *s; s++) { - if (*s != '/') continue; - if (!fnmatch_internal(pat, -1, str, s-str, flags)) - return 0; - } - } - return fnmatch_internal(pat, -1, str, -1, flags); -} diff --git a/ulib/ruxlibc/c/glob.c b/ulib/ruxlibc/c/glob.c deleted file mode 100644 index 6b6137424..000000000 --- a/ulib/ruxlibc/c/glob.c +++ /dev/null @@ -1,338 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#ifdef RUX_CONFIG_FS - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -struct match { - struct match *next; - char name[]; -}; - -static int append(struct match **tail, const char *name, size_t len, int mark) -{ - struct match *new = malloc(sizeof(struct match) + len + 2); - if (!new) - return -1; - (*tail)->next = new; - new->next = NULL; - memcpy(new->name, name, len + 1); - if (mark && len && name[len - 1] != '/') { - new->name[len] = '/'; - new->name[len + 1] = 0; - } - *tail = new; - return 0; -} - -static int do_glob(char *buf, size_t pos, int type, char *pat, int flags, - int (*errfunc)(const char *path, int err), struct match **tail) -{ - /* If GLOB_MARK is unused, we don't care about type. */ - if (!type && !(flags & GLOB_MARK)) - type = DT_REG; - - /* Special-case the remaining pattern being all slashes, in - * which case we can use caller-passed type if it's a dir. */ - if (*pat && type != DT_DIR) - type = 0; - while (pos + 1 < PATH_MAX && *pat == '/') buf[pos++] = *pat++; - - /* Consume maximal [escaped-]literal prefix of pattern, copying - * and un-escaping it to the running buffer as we go. */ - long i = 0, j = 0; - int in_bracket = 0, overflow = 0; - for (; pat[i] != '*' && pat[i] != '?' && (!in_bracket || pat[i] != ']'); i++) { - if (!pat[i]) { - if (overflow) - return 0; - pat += i; - pos += j; - i = j = 0; - break; - } else if (pat[i] == '[') { - in_bracket = 1; - } else if (pat[i] == '\\' && !(flags & GLOB_NOESCAPE)) { - /* Backslashes inside a bracket are (at least by - * our interpretation) non-special, so if next - * char is ']' we have a complete expression. */ - if (in_bracket && pat[i + 1] == ']') - break; - /* Unpaired final backslash never matches. */ - if (!pat[i + 1]) - return 0; - i++; - } - if (pat[i] == '/') { - if (overflow) - return 0; - in_bracket = 0; - pat += i + 1; - i = -1; - pos += j + 1; - j = -1; - } - /* Only store a character if it fits in the buffer, but if - * a potential bracket expression is open, the overflow - * must be remembered and handled later only if the bracket - * is unterminated (and thereby a literal), so as not to - * disallow long bracket expressions with short matches. */ - if (pos + (j + 1) < PATH_MAX) { - buf[pos + j++] = pat[i]; - } else if (in_bracket) { - overflow = 1; - } else { - return 0; - } - /* If we consume any new components, the caller-passed type - * or dummy type from above is no longer valid. */ - type = 0; - } - buf[pos] = 0; - if (!*pat) { - /* If we consumed any components above, or if GLOB_MARK is - * requested and we don't yet know if the match is a dir, - * we must confirm the file exists and/or determine its type. - * - * If marking dirs, symlink type is inconclusive; we need the - * type for the symlink target, and therefore must try stat - * first unless type is known not to be a symlink. Otherwise, - * or if that fails, use lstat for determining existence to - * avoid false negatives in the case of broken symlinks. */ - struct stat st; - if ((flags & GLOB_MARK) && (!type || type == DT_LNK) && !stat(buf, &st)) { - if (S_ISDIR(st.st_mode)) - type = DT_DIR; - else - type = DT_REG; - } - if (!type && lstat(buf, &st)) { - if (errno != ENOENT && (errfunc(buf, errno) || (flags & GLOB_ERR))) - return GLOB_ABORTED; - return 0; - } - if (append(tail, buf, pos, (flags & GLOB_MARK) && type == DT_DIR)) - return GLOB_NOSPACE; - return 0; - } - char *p2 = strchr(pat, '/'), saved_sep = '/'; - /* Check if the '/' was escaped and, if so, remove the escape char - * so that it will not be unpaired when passed to fnmatch. */ - if (p2 && !(flags & GLOB_NOESCAPE)) { - char *p; - for (p = p2; p > pat && p[-1] == '\\'; p--) - ; - if ((p2 - p) % 2) { - p2--; - saved_sep = '\\'; - } - } - DIR *dir = opendir(pos ? buf : "."); - if (!dir) { - if (errfunc(buf, errno) || (flags & GLOB_ERR)) - return GLOB_ABORTED; - return 0; - } - int old_errno = errno; - struct dirent *de; - while (errno = 0, de = readdir(dir)) { - /* Quickly skip non-directories when there's pattern left. */ - if (p2 && de->d_type && de->d_type != DT_DIR && de->d_type != DT_LNK) - continue; - - size_t l = strlen(de->d_name); - if (l >= PATH_MAX - pos) - continue; - - if (p2) - *p2 = 0; - - int fnm_flags = ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0) | - ((!(flags & GLOB_PERIOD)) ? FNM_PERIOD : 0); - - if (fnmatch(pat, de->d_name, fnm_flags)) - continue; - - /* With GLOB_PERIOD, don't allow matching . or .. unless - * fnmatch would match them with FNM_PERIOD rules in effect. */ - if (p2 && (flags & GLOB_PERIOD) && de->d_name[0] == '.' && - (!de->d_name[1] || (de->d_name[1] == '.' && !de->d_name[2])) && - fnmatch(pat, de->d_name, fnm_flags | FNM_PERIOD)) - continue; - - memcpy(buf + pos, de->d_name, l + 1); - if (p2) - *p2 = saved_sep; - int r = do_glob(buf, pos + l, de->d_type, p2 ? p2 : "", flags, errfunc, tail); - if (r) { - closedir(dir); - return r; - } - } - int readerr = errno; - if (p2) - *p2 = saved_sep; - closedir(dir); - if (readerr && (errfunc(buf, errno) || (flags & GLOB_ERR))) - return GLOB_ABORTED; - errno = old_errno; - return 0; -} - -static int ignore_err(const char *path, int err) -{ - return 0; -} - -static void freelist(struct match *head) -{ - struct match *match, *next; - for (match = head->next; match; match = next) { - next = match->next; - free(match); - } -} - -static int sort(const void *a, const void *b) -{ - return strcmp(*(const char **)a, *(const char **)b); -} - -static int expand_tilde(char **pat, char *buf, size_t *pos) -{ - char *p = *pat + 1; - size_t i = 0; - - char delim, *name_end = strchrnul(p, '/'); - if ((delim = *name_end)) - *name_end++ = 0; - *pat = name_end; - - char *home = *p ? NULL : getenv("HOME"); - if (!home) { - struct passwd pw, *res; - switch (*p ? getpwnam_r(p, &pw, buf, PATH_MAX, &res) - : getpwuid_r(getuid(), &pw, buf, PATH_MAX, &res)) { - case ENOMEM: - return GLOB_NOSPACE; - case 0: - if (!res) - default: - return GLOB_NOMATCH; - } - home = pw.pw_dir; - } - while (i < PATH_MAX - 2 && *home) buf[i++] = *home++; - if (*home) - return GLOB_NOMATCH; - if ((buf[i] = delim)) - buf[++i] = 0; - *pos = i; - return 0; -} - -int glob(const char *restrict pat, int flags, int (*errfunc)(const char *path, int err), - glob_t *restrict g) -{ - struct match head = {.next = NULL}, *tail = &head; - size_t cnt, i; - size_t offs = (flags & GLOB_DOOFFS) ? g->gl_offs : 0; - int error = 0; - char buf[PATH_MAX]; - - if (!errfunc) - errfunc = ignore_err; - - if (!(flags & GLOB_APPEND)) { - g->gl_offs = offs; - g->gl_pathc = 0; - g->gl_pathv = NULL; - } - - if (*pat) { - char *p = strdup(pat); - if (!p) - return GLOB_NOSPACE; - buf[0] = 0; - size_t pos = 0; - char *s = p; - if ((flags & (GLOB_TILDE | GLOB_TILDE_CHECK)) && *p == '~') - error = expand_tilde(&s, buf, &pos); - if (!error) - error = do_glob(buf, pos, 0, s, flags, errfunc, &tail); - free(p); - } - - if (error == GLOB_NOSPACE) { - freelist(&head); - return error; - } - - for (cnt = 0, tail = head.next; tail; tail = tail->next, cnt++) - ; - if (!cnt) { - if (flags & GLOB_NOCHECK) { - tail = &head; - if (append(&tail, pat, strlen(pat), 0)) - return GLOB_NOSPACE; - cnt++; - } else - return GLOB_NOMATCH; - } - - if (flags & GLOB_APPEND) { - char **pathv = realloc(g->gl_pathv, (offs + g->gl_pathc + cnt + 1) * sizeof(char *)); - if (!pathv) { - freelist(&head); - return GLOB_NOSPACE; - } - g->gl_pathv = pathv; - offs += g->gl_pathc; - } else { - g->gl_pathv = malloc((offs + cnt + 1) * sizeof(char *)); - if (!g->gl_pathv) { - freelist(&head); - return GLOB_NOSPACE; - } - for (i = 0; i < offs; i++) g->gl_pathv[i] = NULL; - } - for (i = 0, tail = head.next; i < cnt; tail = tail->next, i++) - g->gl_pathv[offs + i] = tail->name; - g->gl_pathv[offs + i] = NULL; - g->gl_pathc += cnt; - - if (!(flags & GLOB_NOSORT)) - qsort(g->gl_pathv + offs, cnt, sizeof(char *), sort); - - return error; -} - -void globfree(glob_t *g) -{ - size_t i; - for (i = 0; i < g->gl_pathc; i++) - free(g->gl_pathv[g->gl_offs + i] - offsetof(struct match, name)); - free(g->gl_pathv); - g->gl_pathc = 0; - g->gl_pathv = NULL; -} - -#endif // RUX_CONFIG_FS diff --git a/ulib/ruxlibc/c/grp.c b/ulib/ruxlibc/c/grp.c deleted file mode 100644 index eb858607c..000000000 --- a/ulib/ruxlibc/c/grp.c +++ /dev/null @@ -1,46 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include -#include -#include -#include - -/* Group members */ -static char *g_members__[] = { RUX_DEFAULT_USER, NULL }; - -/* Default group */ -static struct group g__ = { - .gr_name = RUX_DEFAULT_GROUP, - .gr_passwd = RUX_DEFAULT_PASS, - .gr_gid = RUX_DEFAULT_GID, - .gr_mem = g_members__, -}; - -// TODO -int initgroups(const char *user, gid_t group) -{ - unimplemented(); - return 0; -} - -struct group *getgrnam(const char *name) -{ - struct group *res; - - if (name && !strcmp(name, g__.gr_name)) - res = &g__; - else { - res = NULL; - errno = ENOENT; - } - - return res; -} diff --git a/ulib/ruxlibc/c/ioctl.c b/ulib/ruxlibc/c/ioctl.c deleted file mode 100644 index 578622a46..000000000 --- a/ulib/ruxlibc/c/ioctl.c +++ /dev/null @@ -1,27 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A - * PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. - */ - -#include -#include -#include - -int rux_ioctl(int fd, int cmd, size_t arg); - -// TODO -int ioctl(int fd, int request, ...) -{ - unsigned long arg; - va_list ap; - va_start(ap, request); - arg = va_arg(ap, unsigned long); - va_end(ap); - - return rux_ioctl(fd, request, arg); -} diff --git a/ulib/ruxlibc/c/libgen.c b/ulib/ruxlibc/c/libgen.c deleted file mode 100644 index d0ffcdced..000000000 --- a/ulib/ruxlibc/c/libgen.c +++ /dev/null @@ -1,42 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include - -char *dirname(char *s) -{ - size_t i; - if (!s || !*s) - return "."; - i = strlen(s) - 1; - for (; s[i] == '/'; i--) - if (!i) - return "/"; - for (; s[i] != '/'; i--) - if (!i) - return "."; - for (; s[i] == '/'; i--) - if (!i) - return "/"; - s[i + 1] = 0; - return s; -} - -char *basename(char *s) -{ - size_t i; - if (!s || !*s) - return "."; - i = strlen(s) - 1; - for (; i && s[i] == '/'; i--) s[i] = 0; - for (; i && s[i - 1] != '/'; i--) - ; - return s + i; -} diff --git a/ulib/ruxlibc/c/libm.c b/ulib/ruxlibc/c/libm.c deleted file mode 100644 index 26269c3ed..000000000 --- a/ulib/ruxlibc/c/libm.c +++ /dev/null @@ -1,26 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#ifdef RUX_CONFIG_FP_SIMD - -#include - -#include "libm.h" - -double __math_divzero(uint32_t sign) -{ - return fp_barrier(sign ? -1.0 : 1.0) / 0.0; -} - -double __math_invalid(double x) -{ - return (x - x) / (x - x); -} - -#endif // RUX_CONFIG_FP_SIMD diff --git a/ulib/ruxlibc/c/libm.h b/ulib/ruxlibc/c/libm.h deleted file mode 100644 index 51046ba8c..000000000 --- a/ulib/ruxlibc/c/libm.h +++ /dev/null @@ -1,242 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#ifndef _LIBM_H -#define _LIBM_H - -#if RUX_CONFIG_FP_SIMD - -#include -#include -#include - -#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 -#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __LITTLE_ENDIAN -union ldshape { - long double f; - struct { - uint64_t m; - uint16_t se; - } i; -}; -#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __BIG_ENDIAN -/* This is the m68k variant of 80-bit long double, and this definition only works - * on archs where the alignment requirement of uint64_t is <= 4. */ -union ldshape { - long double f; - struct { - uint16_t se; - uint16_t pad; - uint64_t m; - } i; -}; -#elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __LITTLE_ENDIAN -union ldshape { - long double f; - struct { - uint64_t lo; - uint32_t mid; - uint16_t top; - uint16_t se; - } i; - struct { - uint64_t lo; - uint64_t hi; - } i2; -}; -#elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 && __BYTE_ORDER == __BIG_ENDIAN -union ldshape { - long double f; - struct { - uint16_t se; - uint16_t top; - uint32_t mid; - uint64_t lo; - } i; - struct { - uint64_t hi; - uint64_t lo; - } i2; -}; -#else -#error Unsupported long double representation -#endif - -/* Support non-nearest rounding mode. */ -#define WANT_ROUNDING 1 -/* Support signaling NaNs. */ -#define WANT_SNAN 0 - -#if WANT_SNAN -#error SNaN is unsupported -#else -#define issignalingf_inline(x) 0 -#define issignaling_inline(x) 0 -#endif - -#ifndef TOINT_INTRINSICS -#define TOINT_INTRINSICS 0 -#endif - -#if TOINT_INTRINSICS -/* Round x to nearest int in all rounding modes, ties have to be rounded - consistently with converttoint so the results match. If the result - would be outside of [-2^31, 2^31-1] then the semantics is unspecified. */ -static double_t roundtoint(double_t); - -/* Convert x to nearest int in all rounding modes, ties have to be rounded - consistently with roundtoint. If the result is not representible in an - int32_t then the semantics is unspecified. */ -static int32_t converttoint(double_t); -#endif - -/* Helps static branch prediction so hot path can be better optimized. */ -#ifdef __GNUC__ -#define predict_true(x) __builtin_expect(!!(x), 1) -#define predict_false(x) __builtin_expect(x, 0) -#else -#define predict_true(x) (x) -#define predict_false(x) (x) -#endif - -/* Evaluate an expression as the specified type. With standard excess - precision handling a type cast or assignment is enough (with - -ffloat-store an assignment is required, in old compilers argument - passing and return statement may not drop excess precision). */ - -static inline float eval_as_float(float x) -{ - float y = x; - return y; -} - -static inline double eval_as_double(double x) -{ - double y = x; - return y; -} - -/* fp_barrier returns its input, but limits code transformations - as if it had a side-effect (e.g. observable io) and returned - an arbitrary value. */ - -#ifndef fp_barrierf -#define fp_barrierf fp_barrierf -static inline float fp_barrierf(float x) -{ - volatile float y = x; - return y; -} -#endif - -#ifndef fp_barrier -#define fp_barrier fp_barrier -static inline double fp_barrier(double x) -{ - volatile double y = x; - return y; -} -#endif - -#ifndef fp_barrierl -#define fp_barrierl fp_barrierl -static inline long double fp_barrierl(long double x) -{ - volatile long double y = x; - return y; -} -#endif - -/* fp_force_eval ensures that the input value is computed when that's - otherwise unused. To prevent the constant folding of the input - expression, an additional fp_barrier may be needed or a compilation - mode that does so (e.g. -frounding-math in gcc). Then it can be - used to evaluate an expression for its fenv side-effects only. */ - -#ifndef fp_force_evalf -#define fp_force_evalf fp_force_evalf -static inline void fp_force_evalf(float x) -{ - volatile float y; - y = x; -} -#endif - -#ifndef fp_force_eval -#define fp_force_eval fp_force_eval -static inline void fp_force_eval(double x) -{ - volatile double y; - y = x; -} -#endif - -#ifndef fp_force_evall -#define fp_force_evall fp_force_evall -static inline void fp_force_evall(long double x) -{ - volatile long double y; - y = x; -} -#endif - -#define FORCE_EVAL(x) \ - do { \ - if (sizeof(x) == sizeof(float)) { \ - fp_force_evalf(x); \ - } else if (sizeof(x) == sizeof(double)) { \ - fp_force_eval(x); \ - } else { \ - fp_force_evall(x); \ - } \ - } while (0) - -#define asuint(f) \ - ((union { \ - float _f; \ - uint32_t _i; \ - }){f}) \ - ._i -#define asfloat(i) \ - ((union { \ - uint32_t _i; \ - float _f; \ - }){i}) \ - ._f -#define asuint64(f) \ - ((union { \ - double _f; \ - uint64_t _i; \ - }){f}) \ - ._i -#define asdouble(i) \ - ((union { \ - uint64_t _i; \ - double _f; \ - }){i}) \ - ._f - -/* error handling functions */ -float __math_xflowf(uint32_t, float); -float __math_uflowf(uint32_t); -float __math_oflowf(uint32_t); -float __math_divzerof(uint32_t); -float __math_invalidf(float); -double __math_xflow(uint32_t, double); -double __math_uflow(uint32_t); -double __math_oflow(uint32_t); -double __math_divzero(uint32_t); -double __math_invalid(double); -#if LDBL_MANT_DIG != DBL_MANT_DIG -long double __math_invalidl(long double); -#endif - -#endif // RUX_CONFIG_FP_SIMD - -#endif // _LIBM_H diff --git a/ulib/ruxlibc/c/locale.c b/ulib/ruxlibc/c/locale.c deleted file mode 100644 index 5cbc619ea..000000000 --- a/ulib/ruxlibc/c/locale.c +++ /dev/null @@ -1,51 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include -#include - -// TODO -char *setlocale(int __category, const char *__locale) -{ - unimplemented(); - return NULL; -} - -static const struct lconv posix_lconv = { - .decimal_point = ".", - .thousands_sep = "", - .grouping = "", - .int_curr_symbol = "", - .currency_symbol = "", - .mon_decimal_point = "", - .mon_thousands_sep = "", - .mon_grouping = "", - .positive_sign = "", - .negative_sign = "", - .int_frac_digits = CHAR_MAX, - .frac_digits = CHAR_MAX, - .p_cs_precedes = CHAR_MAX, - .p_sep_by_space = CHAR_MAX, - .n_cs_precedes = CHAR_MAX, - .n_sep_by_space = CHAR_MAX, - .p_sign_posn = CHAR_MAX, - .n_sign_posn = CHAR_MAX, - .int_p_cs_precedes = CHAR_MAX, - .int_p_sep_by_space = CHAR_MAX, - .int_n_cs_precedes = CHAR_MAX, - .int_n_sep_by_space = CHAR_MAX, - .int_p_sign_posn = CHAR_MAX, - .int_n_sign_posn = CHAR_MAX, -}; - -struct lconv *localeconv(void) -{ - return (void *)&posix_lconv; -} diff --git a/ulib/ruxlibc/c/log.c b/ulib/ruxlibc/c/log.c deleted file mode 100644 index 2dc35dad5..000000000 --- a/ulib/ruxlibc/c/log.c +++ /dev/null @@ -1,404 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#ifdef RUX_CONFIG_FP_SIMD - -#include -#include -#include -#include - -#include "libm.h" - -struct log_data { - double ln2hi; - double ln2lo; - double poly[LOG_POLY_ORDER - 1]; - double poly1[LOG_POLY1_ORDER - 1]; - struct { - double invc, logc; - } tab[1 << LOG_TABLE_BITS]; -#if !__FP_FAST_FMA - struct { - double chi, clo; - } tab2[1 << LOG_TABLE_BITS]; -#endif -}; - -const struct log_data __log_data = { - .ln2hi = 0x1.62e42fefa3800p-1, - .ln2lo = 0x1.ef35793c76730p-45, - .poly1 = - { - -0x1p-1, - 0x1.5555555555577p-2, - -0x1.ffffffffffdcbp-3, - 0x1.999999995dd0cp-3, - -0x1.55555556745a7p-3, - 0x1.24924a344de3p-3, - -0x1.fffffa4423d65p-4, - 0x1.c7184282ad6cap-4, - -0x1.999eb43b068ffp-4, - 0x1.78182f7afd085p-4, - -0x1.5521375d145cdp-4, - }, - .poly = - { - -0x1.0000000000001p-1, - 0x1.555555551305bp-2, - -0x1.fffffffeb459p-3, - 0x1.999b324f10111p-3, - -0x1.55575e506c89fp-3, - }, - .tab = - { - {0x1.734f0c3e0de9fp+0, -0x1.7cc7f79e69000p-2}, - {0x1.713786a2ce91fp+0, -0x1.76feec20d0000p-2}, - {0x1.6f26008fab5a0p+0, -0x1.713e31351e000p-2}, - {0x1.6d1a61f138c7dp+0, -0x1.6b85b38287800p-2}, - {0x1.6b1490bc5b4d1p+0, -0x1.65d5590807800p-2}, - {0x1.69147332f0cbap+0, -0x1.602d076180000p-2}, - {0x1.6719f18224223p+0, -0x1.5a8ca86909000p-2}, - {0x1.6524f99a51ed9p+0, -0x1.54f4356035000p-2}, - {0x1.63356aa8f24c4p+0, -0x1.4f637c36b4000p-2}, - {0x1.614b36b9ddc14p+0, -0x1.49da7fda85000p-2}, - {0x1.5f66452c65c4cp+0, -0x1.445923989a800p-2}, - {0x1.5d867b5912c4fp+0, -0x1.3edf439b0b800p-2}, - {0x1.5babccb5b90dep+0, -0x1.396ce448f7000p-2}, - {0x1.59d61f2d91a78p+0, -0x1.3401e17bda000p-2}, - {0x1.5805612465687p+0, -0x1.2e9e2ef468000p-2}, - {0x1.56397cee76bd3p+0, -0x1.2941b3830e000p-2}, - {0x1.54725e2a77f93p+0, -0x1.23ec58cda8800p-2}, - {0x1.52aff42064583p+0, -0x1.1e9e129279000p-2}, - {0x1.50f22dbb2bddfp+0, -0x1.1956d2b48f800p-2}, - {0x1.4f38f4734ded7p+0, -0x1.141679ab9f800p-2}, - {0x1.4d843cfde2840p+0, -0x1.0edd094ef9800p-2}, - {0x1.4bd3ec078a3c8p+0, -0x1.09aa518db1000p-2}, - {0x1.4a27fc3e0258ap+0, -0x1.047e65263b800p-2}, - {0x1.4880524d48434p+0, -0x1.feb224586f000p-3}, - {0x1.46dce1b192d0bp+0, -0x1.f474a7517b000p-3}, - {0x1.453d9d3391854p+0, -0x1.ea4443d103000p-3}, - {0x1.43a2744b4845ap+0, -0x1.e020d44e9b000p-3}, - {0x1.420b54115f8fbp+0, -0x1.d60a22977f000p-3}, - {0x1.40782da3ef4b1p+0, -0x1.cc00104959000p-3}, - {0x1.3ee8f5d57fe8fp+0, -0x1.c202956891000p-3}, - {0x1.3d5d9a00b4ce9p+0, -0x1.b81178d811000p-3}, - {0x1.3bd60c010c12bp+0, -0x1.ae2c9ccd3d000p-3}, - {0x1.3a5242b75dab8p+0, -0x1.a45402e129000p-3}, - {0x1.38d22cd9fd002p+0, -0x1.9a877681df000p-3}, - {0x1.3755bc5847a1cp+0, -0x1.90c6d69483000p-3}, - {0x1.35dce49ad36e2p+0, -0x1.87120a645c000p-3}, - {0x1.34679984dd440p+0, -0x1.7d68fb4143000p-3}, - {0x1.32f5cceffcb24p+0, -0x1.73cb83c627000p-3}, - {0x1.3187775a10d49p+0, -0x1.6a39a9b376000p-3}, - {0x1.301c8373e3990p+0, -0x1.60b3154b7a000p-3}, - {0x1.2eb4ebb95f841p+0, -0x1.5737d76243000p-3}, - {0x1.2d50a0219a9d1p+0, -0x1.4dc7b8fc23000p-3}, - {0x1.2bef9a8b7fd2ap+0, -0x1.4462c51d20000p-3}, - {0x1.2a91c7a0c1babp+0, -0x1.3b08abc830000p-3}, - {0x1.293726014b530p+0, -0x1.31b996b490000p-3}, - {0x1.27dfa5757a1f5p+0, -0x1.2875490a44000p-3}, - {0x1.268b39b1d3bbfp+0, -0x1.1f3b9f879a000p-3}, - {0x1.2539d838ff5bdp+0, -0x1.160c8252ca000p-3}, - {0x1.23eb7aac9083bp+0, -0x1.0ce7f57f72000p-3}, - {0x1.22a012ba940b6p+0, -0x1.03cdc49fea000p-3}, - {0x1.2157996cc4132p+0, -0x1.f57bdbc4b8000p-4}, - {0x1.201201dd2fc9bp+0, -0x1.e370896404000p-4}, - {0x1.1ecf4494d480bp+0, -0x1.d17983ef94000p-4}, - {0x1.1d8f5528f6569p+0, -0x1.bf9674ed8a000p-4}, - {0x1.1c52311577e7cp+0, -0x1.adc79202f6000p-4}, - {0x1.1b17c74cb26e9p+0, -0x1.9c0c3e7288000p-4}, - {0x1.19e010c2c1ab6p+0, -0x1.8a646b372c000p-4}, - {0x1.18ab07bb670bdp+0, -0x1.78d01b3ac0000p-4}, - {0x1.1778a25efbcb6p+0, -0x1.674f145380000p-4}, - {0x1.1648d354c31dap+0, -0x1.55e0e6d878000p-4}, - {0x1.151b990275fddp+0, -0x1.4485cdea1e000p-4}, - {0x1.13f0ea432d24cp+0, -0x1.333d94d6aa000p-4}, - {0x1.12c8b7210f9dap+0, -0x1.22079f8c56000p-4}, - {0x1.11a3028ecb531p+0, -0x1.10e4698622000p-4}, - {0x1.107fbda8434afp+0, -0x1.ffa6c6ad20000p-5}, - {0x1.0f5ee0f4e6bb3p+0, -0x1.dda8d4a774000p-5}, - {0x1.0e4065d2a9fcep+0, -0x1.bbcece4850000p-5}, - {0x1.0d244632ca521p+0, -0x1.9a1894012c000p-5}, - {0x1.0c0a77ce2981ap+0, -0x1.788583302c000p-5}, - {0x1.0af2f83c636d1p+0, -0x1.5715e67d68000p-5}, - {0x1.09ddb98a01339p+0, -0x1.35c8a49658000p-5}, - {0x1.08cabaf52e7dfp+0, -0x1.149e364154000p-5}, - {0x1.07b9f2f4e28fbp+0, -0x1.e72c082eb8000p-6}, - {0x1.06ab58c358f19p+0, -0x1.a55f152528000p-6}, - {0x1.059eea5ecf92cp+0, -0x1.63d62cf818000p-6}, - {0x1.04949cdd12c90p+0, -0x1.228fb8caa0000p-6}, - {0x1.038c6c6f0ada9p+0, -0x1.c317b20f90000p-7}, - {0x1.02865137932a9p+0, -0x1.419355daa0000p-7}, - {0x1.0182427ea7348p+0, -0x1.81203c2ec0000p-8}, - {0x1.008040614b195p+0, -0x1.0040979240000p-9}, - {0x1.fe01ff726fa1ap-1, 0x1.feff384900000p-9}, - {0x1.fa11cc261ea74p-1, 0x1.7dc41353d0000p-7}, - {0x1.f6310b081992ep-1, 0x1.3cea3c4c28000p-6}, - {0x1.f25f63ceeadcdp-1, 0x1.b9fc114890000p-6}, - {0x1.ee9c8039113e7p-1, 0x1.1b0d8ce110000p-5}, - {0x1.eae8078cbb1abp-1, 0x1.58a5bd001c000p-5}, - {0x1.e741aa29d0c9bp-1, 0x1.95c8340d88000p-5}, - {0x1.e3a91830a99b5p-1, 0x1.d276aef578000p-5}, - {0x1.e01e009609a56p-1, 0x1.07598e598c000p-4}, - {0x1.dca01e577bb98p-1, 0x1.253f5e30d2000p-4}, - {0x1.d92f20b7c9103p-1, 0x1.42edd8b380000p-4}, - {0x1.d5cac66fb5ccep-1, 0x1.606598757c000p-4}, - {0x1.d272caa5ede9dp-1, 0x1.7da76356a0000p-4}, - {0x1.cf26e3e6b2ccdp-1, 0x1.9ab434e1c6000p-4}, - {0x1.cbe6da2a77902p-1, 0x1.b78c7bb0d6000p-4}, - {0x1.c8b266d37086dp-1, 0x1.d431332e72000p-4}, - {0x1.c5894bd5d5804p-1, 0x1.f0a3171de6000p-4}, - {0x1.c26b533bb9f8cp-1, 0x1.067152b914000p-3}, - {0x1.bf583eeece73fp-1, 0x1.147858292b000p-3}, - {0x1.bc4fd75db96c1p-1, 0x1.2266ecdca3000p-3}, - {0x1.b951e0c864a28p-1, 0x1.303d7a6c55000p-3}, - {0x1.b65e2c5ef3e2cp-1, 0x1.3dfc33c331000p-3}, - {0x1.b374867c9888bp-1, 0x1.4ba366b7a8000p-3}, - {0x1.b094b211d304ap-1, 0x1.5933928d1f000p-3}, - {0x1.adbe885f2ef7ep-1, 0x1.66acd2418f000p-3}, - {0x1.aaf1d31603da2p-1, 0x1.740f8ec669000p-3}, - {0x1.a82e63fd358a7p-1, 0x1.815c0f51af000p-3}, - {0x1.a5740ef09738bp-1, 0x1.8e92954f68000p-3}, - {0x1.a2c2a90ab4b27p-1, 0x1.9bb3602f84000p-3}, - {0x1.a01a01393f2d1p-1, 0x1.a8bed1c2c0000p-3}, - {0x1.9d79f24db3c1bp-1, 0x1.b5b515c01d000p-3}, - {0x1.9ae2505c7b190p-1, 0x1.c2967ccbcc000p-3}, - {0x1.9852ef297ce2fp-1, 0x1.cf635d5486000p-3}, - {0x1.95cbaeea44b75p-1, 0x1.dc1bd3446c000p-3}, - {0x1.934c69de74838p-1, 0x1.e8c01b8cfe000p-3}, - {0x1.90d4f2f6752e6p-1, 0x1.f5509c0179000p-3}, - {0x1.8e6528effd79dp-1, 0x1.00e6c121fb800p-2}, - {0x1.8bfce9fcc007cp-1, 0x1.071b80e93d000p-2}, - {0x1.899c0dabec30ep-1, 0x1.0d46b9e867000p-2}, - {0x1.87427aa2317fbp-1, 0x1.13687334bd000p-2}, - {0x1.84f00acb39a08p-1, 0x1.1980d67234800p-2}, - {0x1.82a49e8653e55p-1, 0x1.1f8ffe0cc8000p-2}, - {0x1.8060195f40260p-1, 0x1.2595fd7636800p-2}, - {0x1.7e22563e0a329p-1, 0x1.2b9300914a800p-2}, - {0x1.7beb377dcb5adp-1, 0x1.3187210436000p-2}, - {0x1.79baa679725c2p-1, 0x1.377266dec1800p-2}, - {0x1.77907f2170657p-1, 0x1.3d54ffbaf3000p-2}, - {0x1.756cadbd6130cp-1, 0x1.432eee32fe000p-2}, - }, -#if !__FP_FAST_FMA - .tab2 = - { - {0x1.61000014fb66bp-1, 0x1.e026c91425b3cp-56}, - {0x1.63000034db495p-1, 0x1.dbfea48005d41p-55}, - {0x1.650000d94d478p-1, 0x1.e7fa786d6a5b7p-55}, - {0x1.67000074e6fadp-1, 0x1.1fcea6b54254cp-57}, - {0x1.68ffffedf0faep-1, -0x1.c7e274c590efdp-56}, - {0x1.6b0000763c5bcp-1, -0x1.ac16848dcda01p-55}, - {0x1.6d0001e5cc1f6p-1, 0x1.33f1c9d499311p-55}, - {0x1.6efffeb05f63ep-1, -0x1.e80041ae22d53p-56}, - {0x1.710000e86978p-1, 0x1.bff6671097952p-56}, - {0x1.72ffffc67e912p-1, 0x1.c00e226bd8724p-55}, - {0x1.74fffdf81116ap-1, -0x1.e02916ef101d2p-57}, - {0x1.770000f679c9p-1, -0x1.7fc71cd549c74p-57}, - {0x1.78ffffa7ec835p-1, 0x1.1bec19ef50483p-55}, - {0x1.7affffe20c2e6p-1, -0x1.07e1729cc6465p-56}, - {0x1.7cfffed3fc9p-1, -0x1.08072087b8b1cp-55}, - {0x1.7efffe9261a76p-1, 0x1.dc0286d9df9aep-55}, - {0x1.81000049ca3e8p-1, 0x1.97fd251e54c33p-55}, - {0x1.8300017932c8fp-1, -0x1.afee9b630f381p-55}, - {0x1.850000633739cp-1, 0x1.9bfbf6b6535bcp-55}, - {0x1.87000204289c6p-1, -0x1.bbf65f3117b75p-55}, - {0x1.88fffebf57904p-1, -0x1.9006ea23dcb57p-55}, - {0x1.8b00022bc04dfp-1, -0x1.d00df38e04b0ap-56}, - {0x1.8cfffe50c1b8ap-1, -0x1.8007146ff9f05p-55}, - {0x1.8effffc918e43p-1, 0x1.3817bd07a7038p-55}, - {0x1.910001efa5fc7p-1, 0x1.93e9176dfb403p-55}, - {0x1.9300013467bb9p-1, 0x1.f804e4b980276p-56}, - {0x1.94fffe6ee076fp-1, -0x1.f7ef0d9ff622ep-55}, - {0x1.96fffde3c12d1p-1, -0x1.082aa962638bap-56}, - {0x1.98ffff4458a0dp-1, -0x1.7801b9164a8efp-55}, - {0x1.9afffdd982e3ep-1, -0x1.740e08a5a9337p-55}, - {0x1.9cfffed49fb66p-1, 0x1.fce08c19bep-60}, - {0x1.9f00020f19c51p-1, -0x1.a3faa27885b0ap-55}, - {0x1.a10001145b006p-1, 0x1.4ff489958da56p-56}, - {0x1.a300007bbf6fap-1, 0x1.cbeab8a2b6d18p-55}, - {0x1.a500010971d79p-1, 0x1.8fecadd78793p-55}, - {0x1.a70001df52e48p-1, -0x1.f41763dd8abdbp-55}, - {0x1.a90001c593352p-1, -0x1.ebf0284c27612p-55}, - {0x1.ab0002a4f3e4bp-1, -0x1.9fd043cff3f5fp-57}, - {0x1.acfffd7ae1ed1p-1, -0x1.23ee7129070b4p-55}, - {0x1.aefffee510478p-1, 0x1.a063ee00edea3p-57}, - {0x1.b0fffdb650d5bp-1, 0x1.a06c8381f0ab9p-58}, - {0x1.b2ffffeaaca57p-1, -0x1.9011e74233c1dp-56}, - {0x1.b4fffd995badcp-1, -0x1.9ff1068862a9fp-56}, - {0x1.b7000249e659cp-1, 0x1.aff45d0864f3ep-55}, - {0x1.b8ffff987164p-1, 0x1.cfe7796c2c3f9p-56}, - {0x1.bafffd204cb4fp-1, -0x1.3ff27eef22bc4p-57}, - {0x1.bcfffd2415c45p-1, -0x1.cffb7ee3bea21p-57}, - {0x1.beffff86309dfp-1, -0x1.14103972e0b5cp-55}, - {0x1.c0fffe1b57653p-1, 0x1.bc16494b76a19p-55}, - {0x1.c2ffff1fa57e3p-1, -0x1.4feef8d30c6edp-57}, - {0x1.c4fffdcbfe424p-1, -0x1.43f68bcec4775p-55}, - {0x1.c6fffed54b9f7p-1, 0x1.47ea3f053e0ecp-55}, - {0x1.c8fffeb998fd5p-1, 0x1.383068df992f1p-56}, - {0x1.cb0002125219ap-1, -0x1.8fd8e64180e04p-57}, - {0x1.ccfffdd94469cp-1, 0x1.e7ebe1cc7ea72p-55}, - {0x1.cefffeafdc476p-1, 0x1.ebe39ad9f88fep-55}, - {0x1.d1000169af82bp-1, 0x1.57d91a8b95a71p-56}, - {0x1.d30000d0ff71dp-1, 0x1.9c1906970c7dap-55}, - {0x1.d4fffea790fc4p-1, -0x1.80e37c558fe0cp-58}, - {0x1.d70002edc87e5p-1, -0x1.f80d64dc10f44p-56}, - {0x1.d900021dc82aap-1, -0x1.47c8f94fd5c5cp-56}, - {0x1.dafffd86b0283p-1, 0x1.c7f1dc521617ep-55}, - {0x1.dd000296c4739p-1, 0x1.8019eb2ffb153p-55}, - {0x1.defffe54490f5p-1, 0x1.e00d2c652cc89p-57}, - {0x1.e0fffcdabf694p-1, -0x1.f8340202d69d2p-56}, - {0x1.e2fffdb52c8ddp-1, 0x1.b00c1ca1b0864p-56}, - {0x1.e4ffff24216efp-1, 0x1.2ffa8b094ab51p-56}, - {0x1.e6fffe88a5e11p-1, -0x1.7f673b1efbe59p-58}, - {0x1.e9000119eff0dp-1, -0x1.4808d5e0bc801p-55}, - {0x1.eafffdfa51744p-1, 0x1.80006d54320b5p-56}, - {0x1.ed0001a127fa1p-1, -0x1.002f860565c92p-58}, - {0x1.ef00007babcc4p-1, -0x1.540445d35e611p-55}, - {0x1.f0ffff57a8d02p-1, -0x1.ffb3139ef9105p-59}, - {0x1.f30001ee58ac7p-1, 0x1.a81acf2731155p-55}, - {0x1.f4ffff5823494p-1, 0x1.a3f41d4d7c743p-55}, - {0x1.f6ffffca94c6bp-1, -0x1.202f41c987875p-57}, - {0x1.f8fffe1f9c441p-1, 0x1.77dd1f477e74bp-56}, - {0x1.fafffd2e0e37ep-1, -0x1.f01199a7ca331p-57}, - {0x1.fd0001c77e49ep-1, 0x1.181ee4bceacb1p-56}, - {0x1.feffff7e0c331p-1, -0x1.e05370170875ap-57}, - {0x1.00ffff465606ep+0, -0x1.a7ead491c0adap-55}, - {0x1.02ffff3867a58p+0, -0x1.77f69c3fcb2ep-54}, - {0x1.04ffffdfc0d17p+0, 0x1.7bffe34cb945bp-54}, - {0x1.0700003cd4d82p+0, 0x1.20083c0e456cbp-55}, - {0x1.08ffff9f2cbe8p+0, -0x1.dffdfbe37751ap-57}, - {0x1.0b000010cda65p+0, -0x1.13f7faee626ebp-54}, - {0x1.0d00001a4d338p+0, 0x1.07dfa79489ff7p-55}, - {0x1.0effffadafdfdp+0, -0x1.7040570d66bcp-56}, - {0x1.110000bbafd96p+0, 0x1.e80d4846d0b62p-55}, - {0x1.12ffffae5f45dp+0, 0x1.dbffa64fd36efp-54}, - {0x1.150000dd59ad9p+0, 0x1.a0077701250aep-54}, - {0x1.170000f21559ap+0, 0x1.dfdf9e2e3deeep-55}, - {0x1.18ffffc275426p+0, 0x1.10030dc3b7273p-54}, - {0x1.1b000123d3c59p+0, 0x1.97f7980030188p-54}, - {0x1.1cffff8299eb7p+0, -0x1.5f932ab9f8c67p-57}, - {0x1.1effff48ad4p+0, 0x1.37fbf9da75bebp-54}, - {0x1.210000c8b86a4p+0, 0x1.f806b91fd5b22p-54}, - {0x1.2300003854303p+0, 0x1.3ffc2eb9fbf33p-54}, - {0x1.24fffffbcf684p+0, 0x1.601e77e2e2e72p-56}, - {0x1.26ffff52921d9p+0, 0x1.ffcbb767f0c61p-56}, - {0x1.2900014933a3cp+0, -0x1.202ca3c02412bp-56}, - {0x1.2b00014556313p+0, -0x1.2808233f21f02p-54}, - {0x1.2cfffebfe523bp+0, -0x1.8ff7e384fdcf2p-55}, - {0x1.2f0000bb8ad96p+0, -0x1.5ff51503041c5p-55}, - {0x1.30ffffb7ae2afp+0, -0x1.10071885e289dp-55}, - {0x1.32ffffeac5f7fp+0, -0x1.1ff5d3fb7b715p-54}, - {0x1.350000ca66756p+0, 0x1.57f82228b82bdp-54}, - {0x1.3700011fbf721p+0, 0x1.000bac40dd5ccp-55}, - {0x1.38ffff9592fb9p+0, -0x1.43f9d2db2a751p-54}, - {0x1.3b00004ddd242p+0, 0x1.57f6b707638e1p-55}, - {0x1.3cffff5b2c957p+0, 0x1.a023a10bf1231p-56}, - {0x1.3efffeab0b418p+0, 0x1.87f6d66b152bp-54}, - {0x1.410001532aff4p+0, 0x1.7f8375f198524p-57}, - {0x1.4300017478b29p+0, 0x1.301e672dc5143p-55}, - {0x1.44fffe795b463p+0, 0x1.9ff69b8b2895ap-55}, - {0x1.46fffe80475ep+0, -0x1.5c0b19bc2f254p-54}, - {0x1.48fffef6fc1e7p+0, 0x1.b4009f23a2a72p-54}, - {0x1.4afffe5bea704p+0, -0x1.4ffb7bf0d7d45p-54}, - {0x1.4d000171027dep+0, -0x1.9c06471dc6a3dp-54}, - {0x1.4f0000ff03ee2p+0, 0x1.77f890b85531cp-54}, - {0x1.5100012dc4bd1p+0, 0x1.004657166a436p-57}, - {0x1.530001605277ap+0, -0x1.6bfcece233209p-54}, - {0x1.54fffecdb704cp+0, -0x1.902720505a1d7p-55}, - {0x1.56fffef5f54a9p+0, 0x1.bbfe60ec96412p-54}, - {0x1.5900017e61012p+0, 0x1.87ec581afef9p-55}, - {0x1.5b00003c93e92p+0, -0x1.f41080abf0ccp-54}, - {0x1.5d0001d4919bcp+0, -0x1.8812afb254729p-54}, - {0x1.5efffe7b87a89p+0, -0x1.47eb780ed6904p-54}, - }, -#endif -}; - -#define T __log_data.tab -#define T2 __log_data.tab2 -#define B __log_data.poly1 -#define A __log_data.poly -#define Ln2hi __log_data.ln2hi -#define Ln2lo __log_data.ln2lo -#define N (1 << LOG_TABLE_BITS) -#define OFF 0x3fe6000000000000 - -static inline uint32_t top16(double x) -{ - return asuint64(x) >> 48; -} - -double log(double x) -{ - double_t w, z, r, r2, r3, y, invc, logc, kd, hi, lo; - uint64_t ix, iz, tmp; - uint32_t top; - int k, i; - - ix = asuint64(x); - top = top16(x); -#define LO asuint64(1.0 - 0x1p-4) -#define HI asuint64(1.0 + 0x1.09p-4) - if (predict_false(ix - LO < HI - LO)) { - if (WANT_ROUNDING && predict_false(ix == asuint64(1.0))) - return 0; - r = x - 1.0; - r2 = r * r; - r3 = r * r2; - y = r3 * - (B[1] + r * B[2] + r2 * B[3] + - r3 * (B[4] + r * B[5] + r2 * B[6] + r3 * (B[7] + r * B[8] + r2 * B[9] + r3 * B[10]))); - w = r * 0x1p27; - double_t rhi = r + w - w; - double_t rlo = r - rhi; - w = rhi * rhi * B[0]; - hi = r + w; - lo = r - hi + w; - lo += B[0] * rlo * (rhi + r); - y += lo; - y += hi; - return eval_as_double(y); - } - if (predict_false(top - 0x0010 >= 0x7ff0 - 0x0010)) { - if (ix * 2 == 0) - return __math_divzero(1); - if (ix == asuint64(INFINITY)) - return x; - if ((top & 0x8000) || (top & 0x7ff0) == 0x7ff0) - return __math_invalid(x); - ix = asuint64(x * 0x1p52); - ix -= 52ULL << 52; - } - - tmp = ix - OFF; - i = (tmp >> (52 - LOG_TABLE_BITS)) % N; - k = (int64_t)tmp >> 52; - iz = ix - (tmp & 0xfffULL << 52); - invc = T[i].invc; - logc = T[i].logc; - z = asdouble(iz); - -#if __FP_FAST_FMA - r = __builtin_fma(z, invc, -1.0); -#else - r = (z - T2[i].chi - T2[i].clo) * invc; -#endif - - kd = (double_t)k; - w = kd * Ln2hi + logc; - hi = w + r; - lo = w - hi + r + kd * Ln2lo; - r2 = r * r; - y = lo + r2 * A[0] + r * r2 * (A[1] + r * A[2] + r2 * (A[3] + r * A[4])) + hi; - return eval_as_double(y); -} - -#endif // RUX_CONFIG_FP_SIMD diff --git a/ulib/ruxlibc/c/math.c b/ulib/ruxlibc/c/math.c deleted file mode 100644 index 1f5266dfd..000000000 --- a/ulib/ruxlibc/c/math.c +++ /dev/null @@ -1,580 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#ifdef RUX_CONFIG_FP_SIMD - -#include -#include -#include -#include - -#include "libm.h" - -int __fpclassify(double x) -{ - union { - double f; - uint64_t i; - } u = {x}; - int e = u.i >> 52 & 0x7ff; - if (!e) - return u.i << 1 ? FP_SUBNORMAL : FP_ZERO; - if (e == 0x7ff) - return u.i << 12 ? FP_NAN : FP_INFINITE; - return FP_NORMAL; -} - -int __fpclassifyf(float x) -{ - union { - float f; - uint32_t i; - } u = {x}; - int e = u.i >> 23 & 0xff; - if (!e) - return u.i << 1 ? FP_SUBNORMAL : FP_ZERO; - if (e == 0xff) - return u.i << 9 ? FP_NAN : FP_INFINITE; - return FP_NORMAL; -} - -#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 -int __fpclassifyl(long double x) -{ - return __fpclassify(x); -} -#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 -int __fpclassifyl(long double x) -{ - union ldshape u = {x}; - int e = u.i.se & 0x7fff; - int msb = u.i.m >> 63; - if (!e && !msb) - return u.i.m ? FP_SUBNORMAL : FP_ZERO; - if (e == 0x7fff) { - /* The x86 variant of 80-bit extended precision only admits - * one representation of each infinity, with the mantissa msb - * necessarily set. The version with it clear is invalid/nan. - * The m68k variant, however, allows either, and tooling uses - * the version with it clear. */ - if (__BYTE_ORDER == __LITTLE_ENDIAN && !msb) - return FP_NAN; - return u.i.m << 1 ? FP_NAN : FP_INFINITE; - } - if (!msb) - return FP_NAN; - return FP_NORMAL; -} -#elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384 -int __fpclassifyl(long double x) -{ - union ldshape u = {x}; - int e = u.i.se & 0x7fff; - u.i.se = 0; - if (!e) - return u.i2.lo | u.i2.hi ? FP_SUBNORMAL : FP_ZERO; - if (e == 0x7fff) - return u.i2.lo | u.i2.hi ? FP_NAN : FP_INFINITE; - return FP_NORMAL; -} -#endif - -double fabs(double x) -{ - union { - double f; - uint64_t i; - } u = {x}; - u.i &= -1ULL / 2; - return u.f; -} - -static const double toint = 1 / DBL_EPSILON; - -double floor(double x) -{ - union { - double f; - uint64_t i; - } u = {x}; - int e = u.i >> 52 & 0x7ff; - double y; - - if (e >= 0x3ff + 52 || x == 0) - return x; - /* y = int(x) - x, where int(x) is an integer neighbor of x */ - if (u.i >> 63) - y = x - toint + toint - x; - else - y = x + toint - toint - x; - /* special case because of non-nearest rounding modes */ - if (e <= 0x3ff - 1) { - FORCE_EVAL(y); - return u.i >> 63 ? -1 : 0; - } - if (y > 0) - return x + y - 1; - return x + y; -} - -double rint(double x) -{ - unimplemented(); - return 0; -} - -long long llrint(double x) -{ - return rint(x); -} - -double sqrt(double x) -{ - unimplemented(); - return 0; -} - -double round(double x) -{ - unimplemented(); - return x; -} - -long double roundl(long double x) -{ - unimplemented(); - return x; -} - -long long llroundl(long double x) -{ - unimplemented(); - return x; -} - -double cos(double __x) -{ - unimplemented(); - return 0; -} - -double ceil(double x) -{ - union { - double f; - uint64_t i; - } u = {x}; - int e = u.i >> 52 & 0x7ff; - double_t y; - - if (e >= 0x3ff + 52 || x == 0) - return x; - if (u.i >> 63) - y = x - toint + toint - x; - else - y = x + toint - toint - x; - if (e <= 0x3ff - 1) { - FORCE_EVAL(y); - return u.i >> 63 ? -0.0 : 1; - } - if (y < 0) - return x + y + 1; - return x + y; -} - -// TODO -double sin(double __x) -{ - unimplemented(); - return 0; -} - -// TODO -double asin(double __x) -{ - unimplemented(); - return 0; -} - -long double ceill(long double x) -{ - unimplemented(); - return x; -} - -double acos(double x) -{ - unimplemented(); - return 0; -} - -// TODO -double atan(double x) -{ - unimplemented(); - return 0; -} - -// TODO -double atan2(double y, double x) -{ - unimplemented(); - return 0; -} - -double cosh(double x) -{ - unimplemented(); - return 0; -} - -// TODO -double exp(double x) -{ - unimplemented(); - return 0; -} - -// TODO -double frexp(double x, int *e) -{ - unimplemented(); - return 0; -} - -double ldexp(double x, int n) -{ - unimplemented(); - return 0; -} - -// TODO -double log10(double x) -{ - unimplemented(); - return 0; -} - -// TODO -double modf(double x, double *iptr) -{ - unimplemented(); - return 0; -} - -double sinh(double x) -{ - unimplemented(); - return 0; -} - -// TODO -double tan(double x) -{ - unimplemented(); - return 0; -} - -// TODO -double tanh(double x) -{ - unimplemented(); - return 0; -} - -double copysign(double x, double y) -{ - union { - double f; - uint64_t i; - } ux = {x}, uy = {y}; - ux.i &= -1ULL / 2; - ux.i |= uy.i & 1ULL << 63; - return ux.f; -} - -#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 -long double copysignl(long double x, long double y) -{ - return copysign(x, y); -} -#elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 -long double copysignl(long double x, long double y) -{ - union ldshape ux = {x}, uy = {y}; - ux.i.se &= 0x7fff; - ux.i.se |= uy.i.se & 0x8000; - return ux.f; -} -#endif - -double scalbn(double x, int n) -{ - union { - double f; - uint64_t i; - } u; - double_t y = x; - - if (n > 1023) { - y *= 0x1p1023; - n -= 1023; - if (n > 1023) { - y *= 0x1p1023; - n -= 1023; - if (n > 1023) - n = 1023; - } - } else if (n < -1022) { - /* make sure final n < -53 to avoid double - rounding in the subnormal range */ - y *= 0x1p-1022 * 0x1p53; - n += 1022 - 53; - if (n < -1022) { - y *= 0x1p-1022 * 0x1p53; - n += 1022 - 53; - if (n < -1022) - n = -1022; - } - } - u.i = (uint64_t)(0x3ff + n) << 52; - x = y * u.f; - return x; -} - -#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 -long double scalbnl(long double x, int n) -{ - return scalbn(x, n); -} -#elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 -long double scalbnl(long double x, int n) -{ - union ldshape u; - - if (n > 16383) { - x *= 0x1p16383L; - n -= 16383; - if (n > 16383) { - x *= 0x1p16383L; - n -= 16383; - if (n > 16383) - n = 16383; - } - } else if (n < -16382) { - x *= 0x1p-16382L * 0x1p113L; - n += 16382 - 113; - if (n < -16382) { - x *= 0x1p-16382L * 0x1p113L; - n += 16382 - 113; - if (n < -16382) - n = -16382; - } - } - u.f = 1.0; - u.i.se = 0x3fff + n; - return x * u.f; -} -#endif - -double fmod(double x, double y) -{ - union { - double f; - uint64_t i; - } ux = {x}, uy = {y}; - int ex = ux.i >> 52 & 0x7ff; - int ey = uy.i >> 52 & 0x7ff; - int sx = ux.i >> 63; - uint64_t i; - - /* in the followings uxi should be ux.i, but then gcc wrongly adds */ - /* float load/store to inner loops ruining performance and code size */ - uint64_t uxi = ux.i; - - if (uy.i << 1 == 0 || isnan(y) || ex == 0x7ff) - return (x * y) / (x * y); - if (uxi << 1 <= uy.i << 1) { - if (uxi << 1 == uy.i << 1) - return 0 * x; - return x; - } - - /* normalize x and y */ - if (!ex) { - for (i = uxi << 12; i >> 63 == 0; ex--, i <<= 1) - ; - uxi <<= -ex + 1; - } else { - uxi &= -1ULL >> 12; - uxi |= 1ULL << 52; - } - if (!ey) { - for (i = uy.i << 12; i >> 63 == 0; ey--, i <<= 1) - ; - uy.i <<= -ey + 1; - } else { - uy.i &= -1ULL >> 12; - uy.i |= 1ULL << 52; - } - - /* x mod y */ - for (; ex > ey; ex--) { - i = uxi - uy.i; - if (i >> 63 == 0) { - if (i == 0) - return 0 * x; - uxi = i; - } - uxi <<= 1; - } - i = uxi - uy.i; - if (i >> 63 == 0) { - if (i == 0) - return 0 * x; - uxi = i; - } - for (; uxi >> 52 == 0; uxi <<= 1, ex--) - ; - - /* scale result */ - if (ex > 0) { - uxi -= 1ULL << 52; - uxi |= (uint64_t)ex << 52; - } else { - uxi >>= -ex + 1; - } - uxi |= (uint64_t)sx << 63; - ux.i = uxi; - return ux.f; -} - -// x86_64 has specific implementation -#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 -long double fmodl(long double x, long double y) -{ - return fmod(x, y); -} -#elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 -long double fmodl(long double x, long double y) -{ - union ldshape ux = {x}, uy = {y}; - int ex = ux.i.se & 0x7fff; - int ey = uy.i.se & 0x7fff; - int sx = ux.i.se & 0x8000; - - if (y == 0 || isnan(y) || ex == 0x7fff) - return (x * y) / (x * y); - ux.i.se = ex; - uy.i.se = ey; - if (ux.f <= uy.f) { - if (ux.f == uy.f) - return 0 * x; - return x; - } - - /* normalize x and y */ - if (!ex) { - ux.f *= 0x1p120f; - ex = ux.i.se - 120; - } - if (!ey) { - uy.f *= 0x1p120f; - ey = uy.i.se - 120; - } - - /* x mod y */ -#if LDBL_MANT_DIG == 64 - uint64_t i, mx, my; - mx = ux.i.m; - my = uy.i.m; - for (; ex > ey; ex--) { - i = mx - my; - if (mx >= my) { - if (i == 0) - return 0 * x; - mx = 2 * i; - } else if (2 * mx < mx) { - mx = 2 * mx - my; - } else { - mx = 2 * mx; - } - } - i = mx - my; - if (mx >= my) { - if (i == 0) - return 0 * x; - mx = i; - } - for (; mx >> 63 == 0; mx *= 2, ex--) - ; - ux.i.m = mx; -#elif LDBL_MANT_DIG == 113 - uint64_t hi, lo, xhi, xlo, yhi, ylo; - xhi = (ux.i2.hi & -1ULL >> 16) | 1ULL << 48; - yhi = (uy.i2.hi & -1ULL >> 16) | 1ULL << 48; - xlo = ux.i2.lo; - ylo = uy.i2.lo; - for (; ex > ey; ex--) { - hi = xhi - yhi; - lo = xlo - ylo; - if (xlo < ylo) - hi -= 1; - if (hi >> 63 == 0) { - if ((hi | lo) == 0) - return 0 * x; - xhi = 2 * hi + (lo >> 63); - xlo = 2 * lo; - } else { - xhi = 2 * xhi + (xlo >> 63); - xlo = 2 * xlo; - } - } - hi = xhi - yhi; - lo = xlo - ylo; - if (xlo < ylo) - hi -= 1; - if (hi >> 63 == 0) { - if ((hi | lo) == 0) - return 0 * x; - xhi = hi; - xlo = lo; - } - for (; xhi >> 48 == 0; xhi = 2 * xhi + (xlo >> 63), xlo = 2 * xlo, ex--) - ; - ux.i2.hi = xhi; - ux.i2.lo = xlo; -#endif - - /* scale result */ - if (ex <= 0) { - ux.i.se = (ex + 120) | sx; - ux.f *= 0x1p-120f; - } else - ux.i.se = ex | sx; - return ux.f; -} -#endif - -#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 -long double fabsl(long double x) -{ - return fabs(x); -} -#elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 -long double fabsl(long double x) -{ - union ldshape u = {x}; - - u.i.se &= 0x7fff; - return u.f; -} -#endif - -#endif // RUX_CONFIG_FP_SIMD diff --git a/ulib/ruxlibc/c/network.c b/ulib/ruxlibc/c/network.c deleted file mode 100644 index 0cd8c1bd2..000000000 --- a/ulib/ruxlibc/c/network.c +++ /dev/null @@ -1,243 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A - * PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. - */ - -#ifdef RUX_CONFIG_NET - -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -int h_errno; - -static const char gai_msgs[] = "Invalid flags\0" - "Name does not resolve\0" - "Try again\0" - "Non-recoverable error\0" - "Unknown error\0" - "Unrecognized address family or invalid length\0" - "Unrecognized socket type\0" - "Unrecognized service\0" - "Unknown error\0" - "Out of memory\0" - "System error\0" - "Overflow\0" - "\0Unknown error"; - -const char *gai_strerror(int ecode) -{ - const char *s; - for (s = gai_msgs, ecode++; ecode && *s; ecode++, s++) - for (; *s; s++) - ; - if (!*s) - s++; - return s; -} - -static const char msgs[] = "Host not found\0" - "Try again\0" - "Non-recoverable error\0" - "Address not available\0" - "\0Unknown error"; - -const char *hstrerror(int ecode) -{ - const char *s; - for (s = msgs, ecode--; ecode && *s; ecode--, s++) - for (; *s; s++) - ; - if (!*s) - s++; - return s; -} - -static __inline uint16_t __bswap_16(uint16_t __x) -{ - return __x << 8 | __x >> 8; -} - -static __inline uint32_t __bswap_32(uint32_t __x) -{ - return __x >> 24 | (__x >> 8 & 0xff00) | (__x << 8 & 0xff0000) | __x << 24; -} - -uint32_t htonl(uint32_t n) -{ - union { - int i; - char c; - } u = {1}; - return u.c ? __bswap_32(n) : n; -} - -uint16_t htons(uint16_t n) -{ - union { - int i; - char c; - } u = {1}; - return u.c ? __bswap_16(n) : n; -} - -uint32_t ntohl(uint32_t n) -{ - union { - int i; - char c; - } u = {1}; - return u.c ? __bswap_32(n) : n; -} - -uint16_t ntohs(uint16_t n) -{ - union { - int i; - char c; - } u = {1}; - return u.c ? __bswap_16(n) : n; -} - -static int hexval(unsigned c) -{ - if (c - '0' < 10) - return c - '0'; - c |= 32; - if (c - 'a' < 6) - return c - 'a' + 10; - return -1; -} - -int inet_pton(int af, const char *__restrict s, void *__restrict a0) -{ - uint16_t ip[8]; - unsigned char *a = a0; - int i, j, v, d, brk = -1, need_v4 = 0; - - if (af == AF_INET) { - for (i = 0; i < 4; i++) { - for (v = j = 0; j < 3 && isdigit(s[j]); j++) v = 10 * v + s[j] - '0'; - if (j == 0 || (j > 1 && s[0] == '0') || v > 255) - return 0; - a[i] = v; - if (s[j] == 0 && i == 3) - return 1; - if (s[j] != '.') - return 0; - s += j + 1; - } - return 0; - } else if (af != AF_INET6) { - errno = EAFNOSUPPORT; - return -1; - } - - if (*s == ':' && *++s != ':') - return 0; - - for (i = 0;; i++) { - if (s[0] == ':' && brk < 0) { - brk = i; - ip[i & 7] = 0; - if (!*++s) - break; - if (i == 7) - return 0; - continue; - } - for (v = j = 0; j < 4 && (d = hexval(s[j])) >= 0; j++) v = 16 * v + d; - if (j == 0) - return 0; - ip[i & 7] = v; - if (!s[j] && (brk >= 0 || i == 7)) - break; - if (i == 7) - return 0; - if (s[j] != ':') { - if (s[j] != '.' || (i < 6 && brk < 0)) - return 0; - need_v4 = 1; - i++; - break; - } - s += j + 1; - } - if (brk >= 0) { - for (j = 0; j < 7 - i; j++) ip[brk + j] = 0; - memmove(ip + brk + 7 - i, ip + brk, 2 * (i + 1 - brk)); - } - for (j = 0; j < 8; j++) { - *a++ = ip[j] >> 8; - *a++ = ip[j]; - } - if (need_v4 && inet_pton(AF_INET, (void *)s, a - 4) <= 0) - return 0; - return 1; -} - -const char *inet_ntop(int af, const void *__restrict a0, char *__restrict s, socklen_t l) -{ - const unsigned char *a = a0; - int i, j, max, best; - char buf[100]; - - switch (af) { - case AF_INET: - if (snprintf(s, l, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]) < l) - return s; - break; - case AF_INET6: - if (memcmp(a, "\0\0\0\0\0\0\0\0\0\0\377\377", 12)) - snprintf(buf, sizeof buf, "%x:%x:%x:%x:%x:%x:%x:%x", 256 * a[0] + a[1], - 256 * a[2] + a[3], 256 * a[4] + a[5], 256 * a[6] + a[7], 256 * a[8] + a[9], - 256 * a[10] + a[11], 256 * a[12] + a[13], 256 * a[14] + a[15]); - else - snprintf(buf, sizeof buf, "%x:%x:%x:%x:%x:%x:%d.%d.%d.%d", 256 * a[0] + a[1], - 256 * a[2] + a[3], 256 * a[4] + a[5], 256 * a[6] + a[7], 256 * a[8] + a[9], - 256 * a[10] + a[11], a[12], a[13], a[14], a[15]); - /* Replace longest /(^0|:)[:0]{2,}/ with "::" */ - for (i = best = 0, max = 2; buf[i]; i++) { - if (i && buf[i] != ':') - continue; - j = strspn(buf + i, ":0"); - if (j > max) - best = i, max = j; - } - if (max > 3) { - buf[best] = buf[best + 1] = ':'; - memmove(buf + best + 2, buf + best + max, i - best - max + 1); - } - if (strlen(buf) < l) { - strcpy(s, buf); - return s; - } - break; - default: - errno = EAFNOSUPPORT; - return 0; - } - errno = ENOSPC; - return 0; -} - -// TODO -struct hostent *gethostbyname(const char *name) -{ - unimplemented(); - return 0; -} - -#endif // RUX_CONFIG_NET diff --git a/ulib/ruxlibc/c/nscd_query.c b/ulib/ruxlibc/c/nscd_query.c deleted file mode 100644 index 8e7f85aaf..000000000 --- a/ulib/ruxlibc/c/nscd_query.c +++ /dev/null @@ -1,103 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A - * PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. - */ - -#include "nscd.h" -#include -#include -#include -#include -#include -#include -#include -#include - -static const struct { - short sun_family; - char sun_path[21]; -} addr = {AF_UNIX, "/var/run/nscd/socket"}; - -FILE *__nscd_query(int32_t req, const char *key, int32_t *buf, size_t len, int *swap) -{ - size_t i; - int fd; - FILE *f = 0; - int32_t req_buf[REQ_LEN] = {NSCDVERSION, req, strnlen(key, LOGIN_NAME_MAX) + 1}; - struct msghdr msg = { - .msg_iov = (struct iovec[]){{&req_buf, sizeof(req_buf)}, {(char *)key, strlen(key) + 1}}, - .msg_iovlen = 2}; - int errno_save = errno; - - *swap = 0; -retry: - memset(buf, 0, len); - buf[0] = NSCDVERSION; - - fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); - if (fd < 0) { - if (errno == EAFNOSUPPORT) { - f = fopen("/dev/null", "re"); - if (f) - errno = errno_save; - return f; - } - return 0; - } - - if (!(f = fdopen(fd, "r"))) { - close(fd); - return 0; - } - - if (req_buf[2] > LOGIN_NAME_MAX) - return f; - - if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { - if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT) { - errno = errno_save; - return f; - } - goto error; - } - - if (sendmsg(fd, &msg, MSG_NOSIGNAL) < 0) - goto error; - - if (!fread(buf, len, 1, f)) { - if (ferror(f)) - goto error; - if (!*swap) { - fclose(f); - for (i = 0; i < sizeof(req_buf) / sizeof(req_buf[0]); i++) { - req_buf[i] = bswap_32(req_buf[i]); - } - *swap = 1; - goto retry; - } else { - errno = EIO; - goto error; - } - } - - if (*swap) { - for (i = 0; i < len / sizeof(buf[0]); i++) { - buf[i] = bswap_32(buf[i]); - } - } - - if (buf[0] != NSCDVERSION) { - errno = EIO; - goto error; - } - - return f; -error: - fclose(f); - return 0; -} diff --git a/ulib/ruxlibc/c/pow.c b/ulib/ruxlibc/c/pow.c deleted file mode 100644 index 7e5da7055..000000000 --- a/ulib/ruxlibc/c/pow.c +++ /dev/null @@ -1,824 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#if defined(RUX_CONFIG_FP_SIMD) - -#include -#include -#include -#include -#include - -#include "libm.h" - -#define OFF 0x3fe6955500000000 - -#define POW_LOG_TABLE_BITS 7 -#define POW_LOG_POLY_ORDER 8 -#define N (1 << POW_LOG_TABLE_BITS) -struct pow_log_data { - double ln2hi; - double ln2lo; - double poly[POW_LOG_POLY_ORDER - 1]; /* First coefficient is 1. */ - /* Note: the pad field is unused, but allows slightly faster indexing. */ - struct { - double invc, pad, logc, logctail; - } tab[1 << POW_LOG_TABLE_BITS]; -}; - -const struct - pow_log_data - __pow_log_data = - { - .ln2hi = 0x1.62e42fefa3800p-1, - .ln2lo = 0x1.ef35793c76730p-45, - .poly = - { - -0x1p-1, - 0x1.555555555556p-2 * -2, - -0x1.0000000000006p-2 * -2, - 0x1.999999959554ep-3 * 4, - -0x1.555555529a47ap-3 * 4, - 0x1.2495b9b4845e9p-3 * -8, - -0x1.0002b8b263fc3p-3 * -8, - }, - .tab = - { -#define A(a, b, c) {a, 0, b, c}, - A(0x1.6a00000000000p+0, -0x1.62c82f2b9c800p-2, 0x1.ab42428375680p-48) - A(0x1.6800000000000p+0, -0x1.5d1bdbf580800p-2, -0x1.ca508d8e0f720p-46) - A(0x1.6600000000000p+0, -0x1.5767717455800p-2, - -0x1.362a4d5b6506dp-45) - A(0x1.6400000000000p+0, -0x1.51aad872df800p-2, - -0x1.684e49eb067d5p-49) A(0x1.6200000000000p+0, - -0x1.4be5f95777800p-2, - -0x1.41b6993293ee0p-47) A(0x1.6000000000000p+0, -0x1.4618bc21c6000p-2, 0x1.3d82f484c84ccp-46) A(0x1.5e00000000000p+0, -0x1.404308686a800p-2, 0x1.c42f3ed820b3ap-50) A(0x1.5c00000000000p+0, -0x1.3a64c55694800p-2, 0x1.0b1c686519460p-45) A(0x1.5a00000000000p+0, -0x1.347dd9a988000p-2, 0x1.5594dd4c58092p-45) A(0x1.5800000000000p+0, -0x1.2e8e2bae12000p-2, 0x1.67b1e99b72bd8p-45) A(0x1.5600000000000p+0, - -0x1.2895a13de8800p-2, 0x1.5ca14b6cfb03fp-46) A(0x1.5600000000000p+0, - -0x1.2895a13de8800p-2, 0x1.5ca14b6cfb03fp-46) A(0x1.5400000000000p+0, - -0x1.22941fbcf7800p-2, - -0x1.65a242853da76p-46) A(0x1.5200000000000p+0, - -0x1.1c898c1699800p-2, - -0x1.fafbc68e75404p-46) A(0x1.5000000000000p+0, - -0x1.1675cababa800p-2, 0x1.f1fc63382a8f0p-46) A(0x1.4e00000000000p+0, - -0x1.1058bf9ae4800p-2, - -0x1.6a8c4fd055a66p-45) A(0x1.4c00000000000p+0, - -0x1.0a324e2739000p-2, -0x1.c6bee7ef4030ep-47) A(0x1.4a00000000000p+0, - -0x1.0402594b4d000p-2, -0x1.036b89ef42d7fp-48) A(0x1.4a00000000000p+0, - -0x1.0402594b4d000p-2, -0x1.036b89ef42d7fp-48) A(0x1.4800000000000p+0, - -0x1.fb9186d5e4000p-3, - 0x1.d572aab993c87p-47) A(0x1.4600000000000p+0, - -0x1.ef0adcbdc6000p-3, - 0x1.b26b79c86af24p-45) A(0x1.4400000000000p+0, - -0x1.e27076e2af000p-3, -0x1.72f4f543fff10p-46) A(0x1.4200000000000p+0, - -0x1.d5c216b4fc000p-3, 0x1.1ba91bbca681bp-45) A(0x1.4000000000000p+0, -0x1.c8ff7c79aa000p-3, 0x1.7794f689f8434p-45) A(0x1.4000000000000p+0, -0x1.c8ff7c79aa000p-3, 0x1.7794f689f8434p-45) A(0x1.3e00000000000p+0, - -0x1.bc286742d9000p-3, 0x1.94eb0318bb78fp-46) A(0x1.3c00000000000p+0, - -0x1.af3c94e80c000p-3, 0x1.a4e633fcd9066p-52) A(0x1.3a00000000000p+0, - -0x1.a23bc1fe2b000p-3, - -0x1.58c64dc46c1eap-45) A(0x1.3a00000000000p+0, - -0x1.a23bc1fe2b000p-3, -0x1.58c64dc46c1eap-45) A(0x1.3800000000000p+0, - -0x1.9525a9cf45000p-3, -0x1.ad1d904c1d4e3p-45) A(0x1.3600000000000p+0, - -0x1.87fa06520d000p-3, 0x1.bbdbf7fdbfa09p-45) A(0x1.3400000000000p+0, - -0x1.7ab890210e000p-3, 0x1.bdb9072534a58p-45) A(0x1.3400000000000p+0, - -0x1.7ab890210e000p-3, 0x1.bdb9072534a58p-45) A(0x1.3200000000000p+0, -0x1.6d60fe719d000p-3, -0x1.0e46aa3b2e266p-46) A(0x1.3000000000000p+0, - -0x1.5ff3070a79000p-3, -0x1.e9e439f105039p-46) A(0x1.3000000000000p+0, - -0x1.5ff3070a79000p-3, -0x1.e9e439f105039p-46) A(0x1.2e00000000000p+0, - -0x1.526e5e3a1b000p-3, -0x1.0de8b90075b8fp-45) A(0x1.2c00000000000p+0, - -0x1.44d2b6ccb8000p-3, 0x1.70cc16135783cp-46) A(0x1.2c00000000000p+0, -0x1.44d2b6ccb8000p-3, 0x1.70cc16135783cp-46) A(0x1.2a00000000000p+0, - -0x1.371fc201e9000p-3, 0x1.178864d27543ap-48) A(0x1.2800000000000p+0, - -0x1.29552f81ff000p-3, -0x1.48d301771c408p-45) A(0x1.2600000000000p+0, -0x1.1b72ad52f6000p-3, -0x1.e80a41811a396p-45) A(0x1.2600000000000p+0, -0x1.1b72ad52f6000p-3, -0x1.e80a41811a396p-45) A(0x1.2400000000000p+0, - -0x1.0d77e7cd09000p-3, - 0x1.a699688e85bf4p-47) A(0x1.2400000000000p+0, -0x1.0d77e7cd09000p-3, 0x1.a699688e85bf4p-47) A(0x1.2200000000000p+0, -0x1.fec9131dbe000p-4, -0x1.575545ca333f2p-45) A(0x1.2000000000000p+0, - -0x1.e27076e2b0000p-4, 0x1.a342c2af0003cp-45) A(0x1.2000000000000p+0, -0x1.e27076e2b0000p-4, 0x1.a342c2af0003cp-45) A(0x1.1e00000000000p+0, -0x1.c5e548f5bc000p-4, -0x1.d0c57585fbe06p-46) A(0x1.1c00000000000p+0, - -0x1.a926d3a4ae000p-4, 0x1.53935e85baac8p-45) A(0x1.1c00000000000p+0, - -0x1.a926d3a4ae000p-4, 0x1.53935e85baac8p-45) A(0x1.1a00000000000p+0, -0x1.8c345d631a000p-4, 0x1.37c294d2f5668p-46) A(0x1.1a00000000000p+0, - -0x1.8c345d631a000p-4, - 0x1.37c294d2f5668p-46) A(0x1.1800000000000p+0, -0x1.6f0d28ae56000p-4, - -0x1.69737c93373dap-45) A(0x1.1600000000000p+0, - -0x1.51b073f062000p-4, 0x1.f025b61c65e57p-46) A(0x1.1600000000000p+0, - -0x1.51b073f062000p-4, 0x1.f025b61c65e57p-46) A(0x1.1400000000000p+0, -0x1.341d7961be000p-4, 0x1.c5edaccf913dfp-45) A(0x1.1400000000000p+0, - -0x1.341d7961be000p-4, 0x1.c5edaccf913dfp-45) A(0x1.1200000000000p+0, -0x1.16536eea38000p-4, 0x1.47c5e768fa309p-46) A(0x1.1000000000000p+0, - -0x1.f0a30c0118000p-5, 0x1.d599e83368e91p-45) A(0x1.1000000000000p+0, -0x1.f0a30c0118000p-5, - 0x1.d599e83368e91p-45) A(0x1.0e00000000000p+0, - -0x1.b42dd71198000p-5, 0x1.c827ae5d6704cp-46) A(0x1.0e00000000000p+0, - -0x1.b42dd71198000p-5, 0x1.c827ae5d6704cp-46) A(0x1.0c00000000000p+0, - -0x1.77458f632c000p-5, -0x1.cfc4634f2a1eep-45) A(0x1.0c00000000000p+0, -0x1.77458f632c000p-5, -0x1.cfc4634f2a1eep-45) A(0x1.0a00000000000p+0, -0x1.39e87b9fec000p-5, 0x1.502b7f526feaap-48) A(0x1.0a00000000000p+0, - -0x1.39e87b9fec000p-5, 0x1.502b7f526feaap-48) A(0x1.0800000000000p+0, -0x1.f829b0e780000p-6, -0x1.980267c7e09e4p-45) A(0x1.0800000000000p+0, -0x1.f829b0e780000p-6, -0x1.980267c7e09e4p-45) A(0x1.0600000000000p+0, - -0x1.7b91b07d58000p-6, -0x1.88d5493faa639p-45) A(0x1.0400000000000p+0, - -0x1.fc0a8b0fc0000p-7, -0x1.f1e7cf6d3a69cp-50) A(0x1.0400000000000p+0, -0x1.fc0a8b0fc0000p-7, -0x1.f1e7cf6d3a69cp-50) A(0x1.0200000000000p+0, -0x1.fe02a6b100000p-8, -0x1.9e23f0dda40e4p-46) A(0x1.0200000000000p+0, - -0x1.fe02a6b100000p-8, - -0x1.9e23f0dda40e4p-46) A(0x1.0000000000000p+0, - 0x0.0000000000000p+0, 0x0.0000000000000p+0) A(0x1.0000000000000p+0, - 0x0.0000000000000p+0, - 0x0.0000000000000p+0) A(0x1.fc00000000000p-1, - 0x1.0101575890000p-7, -0x1.0c76b999d2be8p-46) A(0x1.f800000000000p-1, - 0x1.0205658938000p-6, -0x1.3dc5b06e2f7d2p-45) A(0x1.f400000000000p-1, - 0x1.8492528c90000p-6, - -0x1.aa0ba325a0c34p-45) A(0x1.f000000000000p-1, - 0x1.0415d89e74000p-5, - 0x1.111c05cf1d753p-47) A(0x1.ec00000000000p-1, 0x1.466aed42e0000p-5, -0x1.c167375bdfd28p-45) A(0x1.e800000000000p-1, - 0x1.894aa149fc000p-5, - -0x1.97995d05a267dp-46) A(0x1.e400000000000p-1, - 0x1.ccb73cdddc000p-5, -0x1.a68f247d82807p-46) A(0x1.e200000000000p-1, - 0x1.eea31c006c000p-5, - -0x1.e113e4fc93b7bp-47) A(0x1.de00000000000p-1, - 0x1.1973bd1466000p-4, - -0x1.5325d560d9e9bp-45) A(0x1.da00000000000p-1, 0x1.3bdf5a7d1e000p-4, 0x1.cc85ea5db4ed7p-45) A(0x1.d600000000000p-1, 0x1.5e95a4d97a000p-4, -0x1.c69063c5d1d1ep-45) A(0x1.d400000000000p-1, 0x1.700d30aeac000p-4, 0x1.c1e8da99ded32p-49) A(0x1.d000000000000p-1, - 0x1.9335e5d594000p-4, - 0x1.3115c3abd47dap-45) A(0x1.cc00000000000p-1, - 0x1.b6ac88dad6000p-4, - -0x1.390802bf768e5p-46) A(0x1.ca00000000000p-1, - 0x1.c885801bc4000p-4, - 0x1.646d1c65aacd3p-45) A(0x1.c600000000000p-1, - 0x1.ec739830a2000p-4, - -0x1.dc068afe645e0p-45) A(0x1.c400000000000p-1, - 0x1.fe89139dbe000p-4, - -0x1.534d64fa10afdp-45) A(0x1.c000000000000p-1, 0x1.1178e8227e000p-3, 0x1.1ef78ce2d07f2p-45) A(0x1.be00000000000p-1, 0x1.1aa2b7e23f000p-3, 0x1.ca78e44389934p-45) A(0x1.ba00000000000p-1, 0x1.2d1610c868000p-3, 0x1.39d6ccb81b4a1p-47) A(0x1.b800000000000p-1, 0x1.365fcb0159000p-3, 0x1.62fa8234b7289p-51) A(0x1.b400000000000p-1, 0x1.4913d8333b000p-3, 0x1.5837954fdb678p-45) A(0x1.b200000000000p-1, 0x1.527e5e4a1b000p-3, - 0x1.633e8e5697dc7p-45) A(0x1.ae00000000000p-1, - 0x1.6574ebe8c1000p-3, - 0x1.9cf8b2c3c2e78p-46) A(0x1.ac00000000000p-1, - 0x1.6f0128b757000p-3, -0x1.5118de59c21e1p-45) A(0x1.aa00000000000p-1, 0x1.7898d85445000p-3, -0x1.c661070914305p-46) A(0x1.a600000000000p-1, - 0x1.8beafeb390000p-3, -0x1.73d54aae92cd1p-47) A(0x1.a400000000000p-1, 0x1.95a5adcf70000p-3, 0x1.7f22858a0ff6fp-47) A(0x1.a000000000000p-1, 0x1.a93ed3c8ae000p-3, -0x1.8724350562169p-45) A(0x1.9e00000000000p-1, 0x1.b31d8575bd000p-3, -0x1.c358d4eace1aap-47) A(0x1.9c00000000000p-1, 0x1.bd087383be000p-3, -0x1.d4bc4595412b6p-45) A(0x1.9a00000000000p-1, 0x1.c6ffbc6f01000p-3, -0x1.1ec72c5962bd2p-48) A(0x1.9600000000000p-1, 0x1.db13db0d49000p-3, -0x1.aff2af715b035p-45) A(0x1.9400000000000p-1, - 0x1.e530effe71000p-3, - 0x1.212276041f430p-51) A(0x1.9200000000000p-1, 0x1.ef5ade4dd0000p-3, -0x1.a211565bb8e11p-51) A(0x1.9000000000000p-1, 0x1.f991c6cb3b000p-3, 0x1.bcbecca0cdf30p-46) A(0x1.8c00000000000p-1, 0x1.07138604d5800p-2, 0x1.89cdb16ed4e91p-48) A(0x1.8a00000000000p-1, - 0x1.0c42d67616000p-2, - 0x1.7188b163ceae9p-45) A(0x1.8800000000000p-1, 0x1.1178e8227e800p-2, -0x1.c210e63a5f01cp-45) A(0x1.8600000000000p-1, - 0x1.16b5ccbacf800p-2, - 0x1.b9acdf7a51681p-45) A(0x1.8400000000000p-1, - 0x1.1bf99635a6800p-2, - 0x1.ca6ed5147bdb7p-45) A(0x1.8200000000000p-1, - 0x1.214456d0eb800p-2, - 0x1.a87deba46baeap-47) A(0x1.7e00000000000p-1, - 0x1.2bef07cdc9000p-2, 0x1.a9cfa4a5004f4p-45) A(0x1.7c00000000000p-1, 0x1.314f1e1d36000p-2, -0x1.8e27ad3213cb8p-45) A(0x1.7a00000000000p-1, 0x1.36b6776be1000p-2, 0x1.16ecdb0f177c8p-46) A(0x1.7800000000000p-1, - 0x1.3c25277333000p-2, - 0x1.83b54b606bd5cp-46) A(0x1.7600000000000p-1, - 0x1.419b423d5e800p-2, - 0x1.8e436ec90e09dp-47) A(0x1.7400000000000p-1, 0x1.4718dc271c800p-2, -0x1.f27ce0967d675p-45) A(0x1.7200000000000p-1, 0x1.4c9e09e173000p-2, -0x1.e20891b0ad8a4p-45) A(0x1.7000000000000p-1, - 0x1.522ae0738a000p-2, - 0x1.ebe708164c759p-45) A(0x1.6e00000000000p-1, 0x1.57bf753c8d000p-2, 0x1.fadedee5d40efp-46) A(0x1.6c00000000000p-1, - 0x1.5d5bddf596000p-2, - -0x1.a0b2a08a465dcp-47)}, -}; - -#define T __pow_log_data.tab -#undef A -#define A __pow_log_data.poly -#define Ln2hi __pow_log_data.ln2hi -#define Ln2lo __pow_log_data.ln2lo - -/* Top 12 bits of a double (sign and exponent bits). */ -static inline uint32_t top12(double x) -{ - return asuint64(x) >> 52; -} - -/* Compute y+TAIL = log(x) where the rounded result is y and TAIL has about - additional 15 bits precision. IX is the bit representation of x, but - normalized in the subnormal range using the sign bit for the exponent. */ -static inline double_t log_inline(uint64_t ix, double_t *tail) -{ - /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ - double_t z, r, y, invc, logc, logctail, kd, hi, t1, t2, lo, lo1, lo2, p; - uint64_t iz, tmp; - int k, i; - - /* x = 2^k z; where z is in range [OFF,2*OFF) and exact. - The range is split into N subintervals. - The ith subinterval contains z and c is near its center. */ - tmp = ix - OFF; - i = (tmp >> (52 - POW_LOG_TABLE_BITS)) % N; - k = (int64_t)tmp >> 52; /* arithmetic shift */ - iz = ix - (tmp & 0xfffULL << 52); - z = asdouble(iz); - kd = (double_t)k; - - /* log(x) = k*Ln2 + log(c) + log1p(z/c-1). */ - invc = T[i].invc; - logc = T[i].logc; - logctail = T[i].logctail; - - /* Note: 1/c is j/N or j/N/2 where j is an integer in [N,2N) and - |z/c - 1| < 1/N, so r = z/c - 1 is exactly representible. */ -#if __FP_FAST_FMA - r = __builtin_fma(z, invc, -1.0); -#else - /* Split z such that rhi, rlo and rhi*rhi are exact and |rlo| <= |r|. */ - double_t zhi = asdouble((iz + (1ULL << 31)) & (-1ULL << 32)); - double_t zlo = z - zhi; - double_t rhi = zhi * invc - 1.0; - double_t rlo = zlo * invc; - r = rhi + rlo; -#endif - - /* k*Ln2 + log(c) + r. */ - t1 = kd * Ln2hi + logc; - t2 = t1 + r; - lo1 = kd * Ln2lo + logctail; - lo2 = t1 - t2 + r; - - /* Evaluation is optimized assuming superscalar pipelined execution. */ - double_t ar, ar2, ar3, lo3, lo4; - ar = A[0] * r; /* A[0] = -0.5. */ - ar2 = r * ar; - ar3 = r * ar2; - /* k*Ln2 + log(c) + r + A[0]*r*r. */ -#if __FP_FAST_FMA - hi = t2 + ar2; - lo3 = __builtin_fma(ar, r, -ar2); - lo4 = t2 - hi + ar2; -#else - double_t arhi = A[0] * rhi; - double_t arhi2 = rhi * arhi; - hi = t2 + arhi2; - lo3 = rlo * (ar + arhi); - lo4 = t2 - hi + arhi2; -#endif - /* p = log1p(r) - r - A[0]*r*r. */ - p = (ar3 * (A[1] + r * A[2] + ar2 * (A[3] + r * A[4] + ar2 * (A[5] + r * A[6])))); - lo = lo1 + lo2 + lo3 + lo4 + p; - y = hi + lo; - *tail = hi - y + lo; - return y; -} - -#undef N -#undef T -#define EXP_TABLE_BITS 7 -#define EXP_POLY_ORDER 5 -#define EXP_USE_TOINT_NARROW 0 -#define EXP2_POLY_ORDER 5 -struct exp_data { - double invln2N; - double shift; - double negln2hiN; - double negln2loN; - double poly[4]; /* Last four coefficients. */ - double exp2_shift; - double exp2_poly[EXP2_POLY_ORDER]; - uint64_t tab[2 * (1 << EXP_TABLE_BITS)]; -}; -#define N (1 << EXP_TABLE_BITS) - -const struct exp_data __exp_data = { - // N/ln2 - .invln2N = 0x1.71547652b82fep0 * N, - // -ln2/N - .negln2hiN = -0x1.62e42fefa0000p-8, - .negln2loN = -0x1.cf79abc9e3b3ap-47, -// Used for rounding when !TOINT_INTRINSICS -#if EXP_USE_TOINT_NARROW - .shift = 0x1800000000.8p0, -#else - .shift = 0x1.8p52, -#endif - // exp polynomial coefficients. - .poly = - { - // abs error: 1.555*2^-66 - // ulp error: 0.509 (0.511 without fma) - // if |x| < ln2/256+eps - // abs error if |x| < ln2/256+0x1p-15: 1.09*2^-65 - // abs error if |x| < ln2/128: 1.7145*2^-56 - 0x1.ffffffffffdbdp-2, - 0x1.555555555543cp-3, - 0x1.55555cf172b91p-5, - 0x1.1111167a4d017p-7, - }, - .exp2_shift = 0x1.8p52 / N, - // exp2 polynomial coefficients. - .exp2_poly = - { - // abs error: 1.2195*2^-65 - // ulp error: 0.507 (0.511 without fma) - // if |x| < 1/256 - // abs error if |x| < 1/128: 1.9941*2^-56 - 0x1.62e42fefa39efp-1, - 0x1.ebfbdff82c424p-3, - 0x1.c6b08d70cf4b5p-5, - 0x1.3b2abd24650ccp-7, - 0x1.5d7e09b4e3a84p-10, - }, - // 2^(k/N) ~= H[k]*(1 + T[k]) for int k in [0,N) - // tab[2*k] = asuint64(T[k]) - // tab[2*k+1] = asuint64(H[k]) - (k << 52)/N - .tab = - { - 0x0, - 0x3ff0000000000000, - 0x3c9b3b4f1a88bf6e, - 0x3feff63da9fb3335, - 0xbc7160139cd8dc5d, - 0x3fefec9a3e778061, - 0xbc905e7a108766d1, - 0x3fefe315e86e7f85, - 0x3c8cd2523567f613, - 0x3fefd9b0d3158574, - 0xbc8bce8023f98efa, - 0x3fefd06b29ddf6de, - 0x3c60f74e61e6c861, - 0x3fefc74518759bc8, - 0x3c90a3e45b33d399, - 0x3fefbe3ecac6f383, - 0x3c979aa65d837b6d, - 0x3fefb5586cf9890f, - 0x3c8eb51a92fdeffc, - 0x3fefac922b7247f7, - 0x3c3ebe3d702f9cd1, - 0x3fefa3ec32d3d1a2, - 0xbc6a033489906e0b, - 0x3fef9b66affed31b, - 0xbc9556522a2fbd0e, - 0x3fef9301d0125b51, - 0xbc5080ef8c4eea55, - 0x3fef8abdc06c31cc, - 0xbc91c923b9d5f416, - 0x3fef829aaea92de0, - 0x3c80d3e3e95c55af, - 0x3fef7a98c8a58e51, - 0xbc801b15eaa59348, - 0x3fef72b83c7d517b, - 0xbc8f1ff055de323d, - 0x3fef6af9388c8dea, - 0x3c8b898c3f1353bf, - 0x3fef635beb6fcb75, - 0xbc96d99c7611eb26, - 0x3fef5be084045cd4, - 0x3c9aecf73e3a2f60, - 0x3fef54873168b9aa, - 0xbc8fe782cb86389d, - 0x3fef4d5022fcd91d, - 0x3c8a6f4144a6c38d, - 0x3fef463b88628cd6, - 0x3c807a05b0e4047d, - 0x3fef3f49917ddc96, - 0x3c968efde3a8a894, - 0x3fef387a6e756238, - 0x3c875e18f274487d, - 0x3fef31ce4fb2a63f, - 0x3c80472b981fe7f2, - 0x3fef2b4565e27cdd, - 0xbc96b87b3f71085e, - 0x3fef24dfe1f56381, - 0x3c82f7e16d09ab31, - 0x3fef1e9df51fdee1, - 0xbc3d219b1a6fbffa, - 0x3fef187fd0dad990, - 0x3c8b3782720c0ab4, - 0x3fef1285a6e4030b, - 0x3c6e149289cecb8f, - 0x3fef0cafa93e2f56, - 0x3c834d754db0abb6, - 0x3fef06fe0a31b715, - 0x3c864201e2ac744c, - 0x3fef0170fc4cd831, - 0x3c8fdd395dd3f84a, - 0x3feefc08b26416ff, - 0xbc86a3803b8e5b04, - 0x3feef6c55f929ff1, - 0xbc924aedcc4b5068, - 0x3feef1a7373aa9cb, - 0xbc9907f81b512d8e, - 0x3feeecae6d05d866, - 0xbc71d1e83e9436d2, - 0x3feee7db34e59ff7, - 0xbc991919b3ce1b15, - 0x3feee32dc313a8e5, - 0x3c859f48a72a4c6d, - 0x3feedea64c123422, - 0xbc9312607a28698a, - 0x3feeda4504ac801c, - 0xbc58a78f4817895b, - 0x3feed60a21f72e2a, - 0xbc7c2c9b67499a1b, - 0x3feed1f5d950a897, - 0x3c4363ed60c2ac11, - 0x3feece086061892d, - 0x3c9666093b0664ef, - 0x3feeca41ed1d0057, - 0x3c6ecce1daa10379, - 0x3feec6a2b5c13cd0, - 0x3c93ff8e3f0f1230, - 0x3feec32af0d7d3de, - 0x3c7690cebb7aafb0, - 0x3feebfdad5362a27, - 0x3c931dbdeb54e077, - 0x3feebcb299fddd0d, - 0xbc8f94340071a38e, - 0x3feeb9b2769d2ca7, - 0xbc87deccdc93a349, - 0x3feeb6daa2cf6642, - 0xbc78dec6bd0f385f, - 0x3feeb42b569d4f82, - 0xbc861246ec7b5cf6, - 0x3feeb1a4ca5d920f, - 0x3c93350518fdd78e, - 0x3feeaf4736b527da, - 0x3c7b98b72f8a9b05, - 0x3feead12d497c7fd, - 0x3c9063e1e21c5409, - 0x3feeab07dd485429, - 0x3c34c7855019c6ea, - 0x3feea9268a5946b7, - 0x3c9432e62b64c035, - 0x3feea76f15ad2148, - 0xbc8ce44a6199769f, - 0x3feea5e1b976dc09, - 0xbc8c33c53bef4da8, - 0x3feea47eb03a5585, - 0xbc845378892be9ae, - 0x3feea34634ccc320, - 0xbc93cedd78565858, - 0x3feea23882552225, - 0x3c5710aa807e1964, - 0x3feea155d44ca973, - 0xbc93b3efbf5e2228, - 0x3feea09e667f3bcd, - 0xbc6a12ad8734b982, - 0x3feea012750bdabf, - 0xbc6367efb86da9ee, - 0x3fee9fb23c651a2f, - 0xbc80dc3d54e08851, - 0x3fee9f7df9519484, - 0xbc781f647e5a3ecf, - 0x3fee9f75e8ec5f74, - 0xbc86ee4ac08b7db0, - 0x3fee9f9a48a58174, - 0xbc8619321e55e68a, - 0x3fee9feb564267c9, - 0x3c909ccb5e09d4d3, - 0x3feea0694fde5d3f, - 0xbc7b32dcb94da51d, - 0x3feea11473eb0187, - 0x3c94ecfd5467c06b, - 0x3feea1ed0130c132, - 0x3c65ebe1abd66c55, - 0x3feea2f336cf4e62, - 0xbc88a1c52fb3cf42, - 0x3feea427543e1a12, - 0xbc9369b6f13b3734, - 0x3feea589994cce13, - 0xbc805e843a19ff1e, - 0x3feea71a4623c7ad, - 0xbc94d450d872576e, - 0x3feea8d99b4492ed, - 0x3c90ad675b0e8a00, - 0x3feeaac7d98a6699, - 0x3c8db72fc1f0eab4, - 0x3feeace5422aa0db, - 0xbc65b6609cc5e7ff, - 0x3feeaf3216b5448c, - 0x3c7bf68359f35f44, - 0x3feeb1ae99157736, - 0xbc93091fa71e3d83, - 0x3feeb45b0b91ffc6, - 0xbc5da9b88b6c1e29, - 0x3feeb737b0cdc5e5, - 0xbc6c23f97c90b959, - 0x3feeba44cbc8520f, - 0xbc92434322f4f9aa, - 0x3feebd829fde4e50, - 0xbc85ca6cd7668e4b, - 0x3feec0f170ca07ba, - 0x3c71affc2b91ce27, - 0x3feec49182a3f090, - 0x3c6dd235e10a73bb, - 0x3feec86319e32323, - 0xbc87c50422622263, - 0x3feecc667b5de565, - 0x3c8b1c86e3e231d5, - 0x3feed09bec4a2d33, - 0xbc91bbd1d3bcbb15, - 0x3feed503b23e255d, - 0x3c90cc319cee31d2, - 0x3feed99e1330b358, - 0x3c8469846e735ab3, - 0x3feede6b5579fdbf, - 0xbc82dfcd978e9db4, - 0x3feee36bbfd3f37a, - 0x3c8c1a7792cb3387, - 0x3feee89f995ad3ad, - 0xbc907b8f4ad1d9fa, - 0x3feeee07298db666, - 0xbc55c3d956dcaeba, - 0x3feef3a2b84f15fb, - 0xbc90a40e3da6f640, - 0x3feef9728de5593a, - 0xbc68d6f438ad9334, - 0x3feeff76f2fb5e47, - 0xbc91eee26b588a35, - 0x3fef05b030a1064a, - 0x3c74ffd70a5fddcd, - 0x3fef0c1e904bc1d2, - 0xbc91bdfbfa9298ac, - 0x3fef12c25bd71e09, - 0x3c736eae30af0cb3, - 0x3fef199bdd85529c, - 0x3c8ee3325c9ffd94, - 0x3fef20ab5fffd07a, - 0x3c84e08fd10959ac, - 0x3fef27f12e57d14b, - 0x3c63cdaf384e1a67, - 0x3fef2f6d9406e7b5, - 0x3c676b2c6c921968, - 0x3fef3720dcef9069, - 0xbc808a1883ccb5d2, - 0x3fef3f0b555dc3fa, - 0xbc8fad5d3ffffa6f, - 0x3fef472d4a07897c, - 0xbc900dae3875a949, - 0x3fef4f87080d89f2, - 0x3c74a385a63d07a7, - 0x3fef5818dcfba487, - 0xbc82919e2040220f, - 0x3fef60e316c98398, - 0x3c8e5a50d5c192ac, - 0x3fef69e603db3285, - 0x3c843a59ac016b4b, - 0x3fef7321f301b460, - 0xbc82d52107b43e1f, - 0x3fef7c97337b9b5f, - 0xbc892ab93b470dc9, - 0x3fef864614f5a129, - 0x3c74b604603a88d3, - 0x3fef902ee78b3ff6, - 0x3c83c5ec519d7271, - 0x3fef9a51fbc74c83, - 0xbc8ff7128fd391f0, - 0x3fefa4afa2a490da, - 0xbc8dae98e223747d, - 0x3fefaf482d8e67f1, - 0x3c8ec3bc41aa2008, - 0x3fefba1bee615a27, - 0x3c842b94c3a9eb32, - 0x3fefc52b376bba97, - 0x3c8a64a931d185ee, - 0x3fefd0765b6e4540, - 0xbc8e37bae43be3ed, - 0x3fefdbfdad9cbe14, - 0x3c77893b4d91cd9d, - 0x3fefe7c1819e90d8, - 0x3c5305c14160cc89, - 0x3feff3c22b8f71f1, - }, -}; - -#define InvLn2N __exp_data.invln2N -#define NegLn2hiN __exp_data.negln2hiN -#define NegLn2loN __exp_data.negln2loN -#define Shift __exp_data.shift -#define T __exp_data.tab -#define C2 __exp_data.poly[5 - EXP_POLY_ORDER] -#define C3 __exp_data.poly[6 - EXP_POLY_ORDER] -#define C4 __exp_data.poly[7 - EXP_POLY_ORDER] -#define C5 __exp_data.poly[8 - EXP_POLY_ORDER] -#define C6 __exp_data.poly[9 - EXP_POLY_ORDER] - -static inline double specialcase(double_t tmp, uint64_t sbits, uint64_t ki) -{ - double_t scale, y; - - if ((ki & 0x80000000) == 0) { - /* k > 0, the exponent of scale might have overflowed by <= 460. */ - sbits -= 1009ull << 52; - scale = asdouble(sbits); - y = 0x1p1009 * (scale + scale * tmp); - return eval_as_double(y); - } - /* k < 0, need special care in the subnormal range. */ - sbits += 1022ull << 52; - /* Note: sbits is signed scale. */ - scale = asdouble(sbits); - y = scale + scale * tmp; - if (fabs(y) < 1.0) { - /* Round y to the right precision before scaling it into the subnormal - range to avoid double rounding that can cause 0.5+E/2 ulp error where - E is the worst-case ulp error outside the subnormal range. So this - is only useful if the goal is better than 1 ulp worst-case error. */ - double_t hi, lo, one = 1.0; - if (y < 0.0) - one = -1.0; - lo = scale - y + scale * tmp; - hi = one + y; - lo = one - hi + y + lo; - y = eval_as_double(hi + lo) - one; - /* Fix the sign of 0. */ - if (y == 0.0) - y = asdouble(sbits & 0x8000000000000000); - /* The underflow exception needs to be signaled explicitly. */ - fp_force_eval(fp_barrier(0x1p-1022) * 0x1p-1022); - } - y = 0x1p-1022 * y; - return eval_as_double(y); -} - -#define SIGN_BIAS (0x800 << EXP_TABLE_BITS) - -double __math_xflow(uint32_t sign, double y) -{ - return eval_as_double(fp_barrier(sign ? -y : y) * y); -} - -double __math_uflow(uint32_t sign) -{ - return __math_xflow(sign, 0x1p-767); -} - -double __math_oflow(uint32_t sign) -{ - return __math_xflow(sign, 0x1p769); -} - -/* Computes sign*exp(x+xtail) where |xtail| < 2^-8/N and |xtail| <= |x|. - The sign_bias argument is SIGN_BIAS or 0 and sets the sign to -1 or 1. */ -static inline double exp_inline(double_t x, double_t xtail, uint32_t sign_bias) -{ - uint32_t abstop; - uint64_t ki, idx, top, sbits; - /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ - double_t kd, z, r, r2, scale, tail, tmp; - - abstop = top12(x) & 0x7ff; - if (predict_false(abstop - top12(0x1p-54) >= top12(512.0) - top12(0x1p-54))) { - if (abstop - top12(0x1p-54) >= 0x80000000) { - /* Avoid spurious underflow for tiny x. */ - /* Note: 0 is common input. */ - double_t one = WANT_ROUNDING ? 1.0 + x : 1.0; - return sign_bias ? -one : one; - } - if (abstop >= top12(1024.0)) { - /* Note: inf and nan are already handled. */ - if (asuint64(x) >> 63) - return __math_uflow(sign_bias); - else - return __math_oflow(sign_bias); - } - /* Large x is special cased below. */ - abstop = 0; - } - - /* exp(x) = 2^(k/N) * exp(r), with exp(r) in [2^(-1/2N),2^(1/2N)]. */ - /* x = ln2/N*k + r, with int k and r in [-ln2/2N, ln2/2N]. */ - z = InvLn2N * x; -#if TOINT_INTRINSICS - kd = roundtoint(z); - ki = converttoint(z); -#elif EXP_USE_TOINT_NARROW - /* z - kd is in [-0.5-2^-16, 0.5] in all rounding modes. */ - kd = eval_as_double(z + Shift); - ki = asuint64(kd) >> 16; - kd = (double_t)(int32_t)ki; -#else - /* z - kd is in [-1, 1] in non-nearest rounding modes. */ - kd = eval_as_double(z + Shift); - ki = asuint64(kd); - kd -= Shift; -#endif - r = x + kd * NegLn2hiN + kd * NegLn2loN; - /* The code assumes 2^-200 < |xtail| < 2^-8/N. */ - r += xtail; - /* 2^(k/N) ~= scale * (1 + tail). */ - idx = 2 * (ki % N); - top = (ki + sign_bias) << (52 - EXP_TABLE_BITS); - tail = asdouble(T[idx]); - /* This is only a valid scale when -1023*N < k < 1024*N. */ - sbits = T[idx + 1] + top; - /* exp(x) = 2^(k/N) * exp(r) ~= scale + scale * (tail + exp(r) - 1). */ - /* Evaluation is optimized assuming superscalar pipelined execution. */ - r2 = r * r; - /* Without fma the worst case error is 0.25/N ulp larger. */ - /* Worst case error is less than 0.5+1.11/N+(abs poly error * 2^53) ulp. */ - tmp = tail + r + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5); - if (predict_false(abstop == 0)) - return specialcase(tmp, sbits, ki); - scale = asdouble(sbits); - /* Note: tmp == 0 or |tmp| > 2^-200 and scale > 2^-739, so there - is no spurious underflow here even without fma. */ - return eval_as_double(scale + scale * tmp); -} - -/* Returns 0 if not int, 1 if odd int, 2 if even int. The argument is - the bit representation of a non-zero finite floating-point value. */ -static inline int checkint(uint64_t iy) -{ - int e = iy >> 52 & 0x7ff; - if (e < 0x3ff) - return 0; - if (e > 0x3ff + 52) - return 2; - if (iy & ((1ULL << (0x3ff + 52 - e)) - 1)) - return 0; - if (iy & (1ULL << (0x3ff + 52 - e))) - return 1; - return 2; -} - -#if 100 * __GNUC__ + __GNUC_MINOR__ >= 303 -#define NAN __builtin_nanf("") -#define INFINITY __builtin_inff() -#else -#define NAN (0.0f / 0.0f) -#define INFINITY 1e5000f -#endif - -static inline int zeroinfnan(uint64_t i) -{ - return 2 * i - 1 >= 2 * asuint64(INFINITY) - 1; -} - -#if WANT_SNAN -#error SNaN is unsupported -#else -#define issignalingf_inline(x) 0 -#define issignaling_inline(x) 0 -#endif - -double pow(double x, double y) -{ - uint32_t sign_bias = 0; - uint64_t ix, iy; - uint32_t topx, topy; - - ix = asuint64(x); - iy = asuint64(y); - topx = top12(x); - topy = top12(y); - if (predict_false(topx - 0x001 >= 0x7ff - 0x001 || (topy & 0x7ff) - 0x3be >= 0x43e - 0x3be)) { - /* Note: if |y| > 1075 * ln2 * 2^53 ~= 0x1.749p62 then pow(x,y) = inf/0 - and if |y| < 2^-54 / 1075 ~= 0x1.e7b6p-65 then pow(x,y) = +-1. */ - /* Special cases: (x < 0x1p-126 or inf or nan) or - (|y| < 0x1p-65 or |y| >= 0x1p63 or nan). */ - if (predict_false(zeroinfnan(iy))) { - if (2 * iy == 0) - return issignaling_inline(x) ? x + y : 1.0; - if (ix == asuint64(1.0)) - return issignaling_inline(y) ? x + y : 1.0; - if (2 * ix > 2 * asuint64(INFINITY) || 2 * iy > 2 * asuint64(INFINITY)) - return x + y; - if (2 * ix == 2 * asuint64(1.0)) - return 1.0; - if ((2 * ix < 2 * asuint64(1.0)) == !(iy >> 63)) - return 0.0; /* |x|<1 && y==inf or |x|>1 && y==-inf. */ - return y * y; - } - if (predict_false(zeroinfnan(ix))) { - double_t x2 = x * x; - if (ix >> 63 && checkint(iy) == 1) - x2 = -x2; - /* Without the barrier some versions of clang hoist the 1/x2 and - thus division by zero exception can be signaled spuriously. */ - return iy >> 63 ? fp_barrier(1 / x2) : x2; - } - /* Here x and y are non-zero finite. */ - if (ix >> 63) { - /* Finite x < 0. */ - int yint = checkint(iy); - if (yint == 0) - return __math_invalid(x); - if (yint == 1) - sign_bias = SIGN_BIAS; - ix &= 0x7fffffffffffffff; - topx &= 0x7ff; - } - if ((topy & 0x7ff) - 0x3be >= 0x43e - 0x3be) { - /* Note: sign_bias == 0 here because y is not odd. */ - if (ix == asuint64(1.0)) - return 1.0; - if ((topy & 0x7ff) < 0x3be) { - /* |y| < 2^-65, x^y ~= 1 + y*log(x). */ - if (WANT_ROUNDING) - return ix > asuint64(1.0) ? 1.0 + y : 1.0 - y; - else - return 1.0; - } - return (ix > asuint64(1.0)) == (topy < 0x800) ? __math_oflow(0) : __math_uflow(0); - } - if (topx == 0) { - /* Normalize subnormal x so exponent becomes negative. */ - ix = asuint64(x * 0x1p52); - ix &= 0x7fffffffffffffff; - ix -= 52ULL << 52; - } - } - - double_t lo; - double_t hi = log_inline(ix, &lo); - double_t ehi, elo; -#if __FP_FAST_FMA - ehi = y * hi; - elo = y * lo + __builtin_fma(y, hi, -ehi); -#else - double_t yhi = asdouble(iy & -1ULL << 27); - double_t ylo = y - yhi; - double_t lhi = asdouble(asuint64(hi) & -1ULL << 27); - double_t llo = hi - lhi + lo; - ehi = yhi * lhi; - elo = ylo * lhi + y * llo; /* |elo| < |ehi| * 2^-25. */ -#endif - return exp_inline(ehi, elo, sign_bias); -} -#endif diff --git a/ulib/ruxlibc/c/prctl.c b/ulib/ruxlibc/c/prctl.c deleted file mode 100644 index 7f0f1ee2c..000000000 --- a/ulib/ruxlibc/c/prctl.c +++ /dev/null @@ -1,19 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A - * PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. - */ - -#include -#include - -// TODO -int prctl(int option, ...) -{ - unimplemented(); - return 0; -} diff --git a/ulib/ruxlibc/c/printf.c b/ulib/ruxlibc/c/printf.c deleted file mode 100644 index 128cfdb66..000000000 --- a/ulib/ruxlibc/c/printf.c +++ /dev/null @@ -1,1491 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -/** - * @author (c) Eyal Rozenberg - * 2021-2022, Haifa, Palestine/Israel - * @author (c) Marco Paland (info@paland.com) - * 2014-2019, PALANDesign Hannover, Germany - * - * @note Others have made smaller contributions to this file: see the - * contributors page at https://github.com/eyalroz/printf/graphs/contributors - * or ask one of the authors. The original code for exponential specifiers was - * contributed by Martijn Jasperse . - * - * @brief Small stand-alone implementation of the printf family of functions - * (`(v)printf`, `(v)s(n)printf` etc., geared towards use on embedded systems with - * a very limited resources. - * - * @note the implementations are thread-safe; re-entrant; use no functions from - * the standard library; and do not dynamically allocate any memory. - * - * @license The MIT License (MIT) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -// Define this globally (e.g. gcc -DPRINTF_INCLUDE_CONFIG_H=1 ...) to include the -// printf_config.h header file -#define PRINTF_INCLUDE_CONFIG_H 1 - -#if PRINTF_INCLUDE_CONFIG_H -#include "printf_config.h" -#endif - -#include "printf.h" - -#ifdef __cplusplus -#include -#include -#else -#include -#include -#include -#include -#endif // __cplusplus - -#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES -#define printf_ printf -#define sprintf_ sprintf -#define vsprintf_ vsprintf -#define snprintf_ snprintf -#define vsnprintf_ vsnprintf -#define vprintf_ vprintf -#endif - -// 'ntoa' conversion buffer size, this must be big enough to hold one converted -// numeric number including padded zeros (dynamically created on stack) -#ifndef PRINTF_INTEGER_BUFFER_SIZE -#define PRINTF_INTEGER_BUFFER_SIZE 32 -#endif - -// size of the fixed (on-stack) buffer for printing individual decimal numbers. -// this must be big enough to hold one converted floating-point value including -// padded zeros. -#ifndef PRINTF_DECIMAL_BUFFER_SIZE -#define PRINTF_DECIMAL_BUFFER_SIZE 32 -#endif - -// Support for the decimal notation floating point conversion specifiers (%f, %F) -#ifndef PRINTF_SUPPORT_DECIMAL_SPECIFIERS -#define PRINTF_SUPPORT_DECIMAL_SPECIFIERS 1 -#endif - -// Support for the exponential notation floating point conversion specifiers (%e, %g, %E, %G) -#ifndef PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS -#define PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS 1 -#endif - -// Support for the length write-back specifier (%n) -#ifndef PRINTF_SUPPORT_WRITEBACK_SPECIFIER -#define PRINTF_SUPPORT_WRITEBACK_SPECIFIER 1 -#endif - -// Default precision for the floating point conversion specifiers (the C standard sets this at 6) -#ifndef PRINTF_DEFAULT_FLOAT_PRECISION -#define PRINTF_DEFAULT_FLOAT_PRECISION 6 -#endif - -// According to the C languages standard, printf() and related functions must be able to print any -// integral number in floating-point notation, regardless of length, when using the %f specifier - -// possibly hundreds of characters, potentially overflowing your buffers. In this implementation, -// all values beyond this threshold are switched to exponential notation. -#ifndef PRINTF_MAX_INTEGRAL_DIGITS_FOR_DECIMAL -#define PRINTF_MAX_INTEGRAL_DIGITS_FOR_DECIMAL 9 -#endif - -// Support for the long long integral types (with the ll, z and t length modifiers for specifiers -// %d,%i,%o,%x,%X,%u, and with the %p specifier). Note: 'L' (long double) is not supported. -#ifndef PRINTF_SUPPORT_LONG_LONG -#define PRINTF_SUPPORT_LONG_LONG 1 -#endif - -// The number of terms in a Taylor series expansion of log_10(x) to -// use for approximation - including the power-zero term (i.e. the -// value at the point of expansion). -#ifndef PRINTF_LOG10_TAYLOR_TERMS -#define PRINTF_LOG10_TAYLOR_TERMS 4 -#endif - -#if PRINTF_LOG10_TAYLOR_TERMS <= 1 -#error "At least one non-constant Taylor expansion is necessary for the log10() calculation" -#endif - -// Be extra-safe, and don't assume format specifiers are completed correctly -// before the format string end. -#ifndef PRINTF_CHECK_FOR_NUL_IN_FORMAT_SPECIFIER -#define PRINTF_CHECK_FOR_NUL_IN_FORMAT_SPECIFIER 1 -#endif - -#define PRINTF_PREFER_DECIMAL false -#define PRINTF_PREFER_EXPONENTIAL true - -/////////////////////////////////////////////////////////////////////////////// - -// The following will convert the number-of-digits into an exponential-notation literal -#define PRINTF_CONCATENATE(s1, s2) s1##s2 -#define PRINTF_EXPAND_THEN_CONCATENATE(s1, s2) PRINTF_CONCATENATE(s1, s2) -#define PRINTF_FLOAT_NOTATION_THRESHOLD \ - PRINTF_EXPAND_THEN_CONCATENATE(1e, PRINTF_MAX_INTEGRAL_DIGITS_FOR_DECIMAL) - -// internal flag definitions -#define FLAGS_ZEROPAD (1U << 0U) -#define FLAGS_LEFT (1U << 1U) -#define FLAGS_PLUS (1U << 2U) -#define FLAGS_SPACE (1U << 3U) -#define FLAGS_HASH (1U << 4U) -#define FLAGS_UPPERCASE (1U << 5U) -#define FLAGS_CHAR (1U << 6U) -#define FLAGS_SHORT (1U << 7U) -#define FLAGS_INT (1U << 8U) -// Only used with PRINTF_SUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS -#define FLAGS_LONG (1U << 9U) -#define FLAGS_LONG_LONG (1U << 10U) -#define FLAGS_PRECISION (1U << 11U) -#define FLAGS_ADAPT_EXP (1U << 12U) -#define FLAGS_POINTER (1U << 13U) -// Note: Similar, but not identical, effect as FLAGS_HASH -#define FLAGS_SIGNED (1U << 14U) -// Only used with PRINTF_SUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS - -#ifdef PRINTF_SUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS - -#define FLAGS_INT8 FLAGS_CHAR - -#if (SHRT_MAX == 32767LL) -#define FLAGS_INT16 FLAGS_SHORT -#elif (INT_MAX == 32767LL) -#define FLAGS_INT16 FLAGS_INT -#elif (LONG_MAX == 32767LL) -#define FLAGS_INT16 FLAGS_LONG -#elif (LLONG_MAX == 32767LL) -#define FLAGS_INT16 FLAGS_LONG_LONG -#else -#error "No basic integer type has a size of 16 bits exactly" -#endif - -#if (SHRT_MAX == 2147483647LL) -#define FLAGS_INT32 FLAGS_SHORT -#elif (INT_MAX == 2147483647LL) -#define FLAGS_INT32 FLAGS_INT -#elif (LONG_MAX == 2147483647LL) -#define FLAGS_INT32 FLAGS_LONG -#elif (LLONG_MAX == 2147483647LL) -#define FLAGS_INT32 FLAGS_LONG_LONG -#else -#error "No basic integer type has a size of 32 bits exactly" -#endif - -#if (SHRT_MAX == 9223372036854775807LL) -#define FLAGS_INT64 FLAGS_SHORT -#elif (INT_MAX == 9223372036854775807LL) -#define FLAGS_INT64 FLAGS_INT -#elif (LONG_MAX == 9223372036854775807LL) -#define FLAGS_INT64 FLAGS_LONG -#elif (LLONG_MAX == 9223372036854775807LL) -#define FLAGS_INT64 FLAGS_LONG_LONG -#else -#error "No basic integer type has a size of 64 bits exactly" -#endif - -#endif // PRINTF_SUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS - -typedef unsigned int printf_flags_t; - -#define BASE_BINARY 2 -#define BASE_OCTAL 8 -#define BASE_DECIMAL 10 -#define BASE_HEX 16 - -typedef uint8_t numeric_base_t; - -#if PRINTF_SUPPORT_LONG_LONG -typedef unsigned long long printf_unsigned_value_t; -typedef long long printf_signed_value_t; -#else -typedef unsigned long printf_unsigned_value_t; -typedef long printf_signed_value_t; -#endif - -// The printf()-family functions return an `int`; it is therefore -// unnecessary/inappropriate to use size_t - often larger than int -// in practice - for non-negative related values, such as widths, -// precisions, offsets into buffers used for printing and the sizes -// of these buffers. instead, we use: -typedef unsigned int printf_size_t; -#define PRINTF_MAX_POSSIBLE_BUFFER_SIZE INT_MAX -// If we were to nitpick, this would actually be INT_MAX + 1, -// since INT_MAX is the maximum return value, which excludes the -// trailing '\0'. - -#if (PRINTF_SUPPORT_DECIMAL_SPECIFIERS || PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS) -#include -#if FLT_RADIX != 2 -#error "Non-binary-radix floating-point types are unsupported." -#endif - -#if DBL_MANT_DIG == 24 - -#define DOUBLE_SIZE_IN_BITS 32 -typedef uint32_t double_uint_t; -#define DOUBLE_EXPONENT_MASK 0xFFU -#define DOUBLE_BASE_EXPONENT 127 -#define DOUBLE_MAX_SUBNORMAL_EXPONENT_OF_10 -38 -#define DOUBLE_MAX_SUBNORMAL_POWER_OF_10 1e-38 - -#elif DBL_MANT_DIG == 53 - -#define DOUBLE_SIZE_IN_BITS 64 -typedef uint64_t double_uint_t; -#define DOUBLE_EXPONENT_MASK 0x7FFU -#define DOUBLE_BASE_EXPONENT 1023 -#define DOUBLE_MAX_SUBNORMAL_EXPONENT_OF_10 -308 -#define DOUBLE_MAX_SUBNORMAL_POWER_OF_10 1e-308 - -#else -#error "Unsupported double type configuration" -#endif -#define DOUBLE_STORED_MANTISSA_BITS (DBL_MANT_DIG - 1) - -typedef union { - double_uint_t U; - double F; -} double_with_bit_access; - -// This is unnecessary in C99, since compound initializers can be used, -// but: -// 1. Some compilers are finicky about this; -// 2. Some people may want to convert this to C89; -// 3. If you try to use it as C++, only C++20 supports compound literals -static inline double_with_bit_access get_bit_access(double x) -{ - double_with_bit_access dwba; - dwba.F = x; - return dwba; -} - -static inline int get_sign_bit(double x) -{ - // The sign is stored in the highest bit - return (int)(get_bit_access(x).U >> (DOUBLE_SIZE_IN_BITS - 1)); -} - -static inline int get_exp2(double_with_bit_access x) -{ - // The exponent in an IEEE-754 floating-point number occupies a contiguous - // sequence of bits (e.g. 52..62 for 64-bit doubles), but with a non-trivial representation: An - // unsigned offset from some negative value (with the extremal offset values reserved for - // special use). - return (int)((x.U >> DOUBLE_STORED_MANTISSA_BITS) & DOUBLE_EXPONENT_MASK) - - DOUBLE_BASE_EXPONENT; -} -#define PRINTF_ABS(_x) ((_x) > 0 ? (_x) : -(_x)) - -#endif // (PRINTF_SUPPORT_DECIMAL_SPECIFIERS || PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS) - -// Note in particular the behavior here on LONG_MIN or LLONG_MIN; it is valid -// and well-defined, but if you're not careful you can easily trigger undefined -// behavior with -LONG_MIN or -LLONG_MIN -#define ABS_FOR_PRINTING(_x) \ - ((printf_unsigned_value_t)((_x) > 0 ? (_x) : -((printf_signed_value_t)_x))) - -// wrapper (used as buffer) for output function type -// -// One of the following must hold: -// 1. max_chars is 0 -// 2. buffer is non-null -// 3. function is non-null -// -// ... otherwise bad things will happen. -typedef struct { - void (*function)(char c, void *extra_arg); - void *extra_function_arg; - char *buffer; - printf_size_t pos; - printf_size_t max_chars; -} output_gadget_t; - -// Note: This function currently assumes it is not passed a '\0' c, -// or alternatively, that '\0' can be passed to the function in the output -// gadget. The former assumption holds within the printf library. It also -// assumes that the output gadget has been properly initialized. -static inline void putchar_via_gadget(output_gadget_t *gadget, char c) -{ - printf_size_t write_pos = gadget->pos++; - // We're _always_ increasing pos, so as to count how may characters - // _would_ have been written if not for the max_chars limitation - if (write_pos >= gadget->max_chars) { - return; - } - if (gadget->function != NULL) { - // No check for c == '\0' . - gadget->function(c, gadget->extra_function_arg); - } else { - // it must be the case that gadget->buffer != NULL , due to the constraint - // on output_gadget_t ; and note we're relying on write_pos being non-negative. - gadget->buffer[write_pos] = c; - } -} - -// Possibly-write the string-terminating '\0' character -static inline void append_termination_with_gadget(output_gadget_t *gadget) -{ - if (gadget->function != NULL || gadget->max_chars == 0) { - return; - } - if (gadget->buffer == NULL) { - return; - } - printf_size_t null_char_pos = - gadget->pos < gadget->max_chars ? gadget->pos : gadget->max_chars - 1; - gadget->buffer[null_char_pos] = '\0'; -} - -// We can't use putchar_ as is, since our output gadget -// only takes pointers to functions with an extra argument -// static inline void putchar_wrapper(char c, void *unused) -// { -// (void)unused; -// putchar_(c); -// } - -static inline output_gadget_t discarding_gadget(void) -{ - output_gadget_t gadget; - gadget.function = NULL; - gadget.extra_function_arg = NULL; - gadget.buffer = NULL; - gadget.pos = 0; - gadget.max_chars = 0; - return gadget; -} - -static inline output_gadget_t buffer_gadget(char *buffer, size_t buffer_size) -{ - printf_size_t usable_buffer_size = (buffer_size > PRINTF_MAX_POSSIBLE_BUFFER_SIZE) - ? PRINTF_MAX_POSSIBLE_BUFFER_SIZE - : (printf_size_t)buffer_size; - output_gadget_t result = discarding_gadget(); - if (buffer != NULL) { - result.buffer = buffer; - result.max_chars = usable_buffer_size; - } - return result; -} - -static inline output_gadget_t function_gadget(void (*function)(char, void *), void *extra_arg) -{ - output_gadget_t result = discarding_gadget(); - result.function = function; - result.extra_function_arg = extra_arg; - result.max_chars = PRINTF_MAX_POSSIBLE_BUFFER_SIZE; - return result; -} - -// static inline output_gadget_t extern_putchar_gadget(void) -// { -// return function_gadget(putchar_wrapper, NULL); -// } - -// internal secure strlen -// @return The length of the string (excluding the terminating 0) limited by 'maxsize' -// @note strlen uses size_t, but wes only use this function with printf_size_t -// variables - hence the signature. -static inline printf_size_t strnlen_s_(const char *str, printf_size_t maxsize) -{ - const char *s; - for (s = str; *s && maxsize--; ++s) - ; - return (printf_size_t)(s - str); -} - -// internal test if char is a digit (0-9) -// @return true if char is a digit -static inline bool is_digit_(char ch) -{ - return (ch >= '0') && (ch <= '9'); -} - -// internal ASCII string to printf_size_t conversion -static printf_size_t atou_(const char **str) -{ - printf_size_t i = 0U; - while (is_digit_(**str)) { - i = i * 10U + (printf_size_t)(*((*str)++) - '0'); - } - return i; -} - -// output the specified string in reverse, taking care of any zero-padding -static void out_rev_(output_gadget_t *output, const char *buf, printf_size_t len, - printf_size_t width, printf_flags_t flags) -{ - const printf_size_t start_pos = output->pos; - - // pad spaces up to given width - if (!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) { - for (printf_size_t i = len; i < width; i++) { - putchar_via_gadget(output, ' '); - } - } - - // reverse string - while (len) { - putchar_via_gadget(output, buf[--len]); - } - - // append pad spaces up to given width - if (flags & FLAGS_LEFT) { - while (output->pos - start_pos < width) { - putchar_via_gadget(output, ' '); - } - } -} - -// Invoked by print_integer after the actual number has been printed, performing necessary -// work on the number's prefix (as the number is initially printed in reverse order) -static void print_integer_finalization(output_gadget_t *output, char *buf, printf_size_t len, - bool negative, numeric_base_t base, printf_size_t precision, - printf_size_t width, printf_flags_t flags) -{ - printf_size_t unpadded_len = len; - - // pad with leading zeros - { - if (!(flags & FLAGS_LEFT)) { - if (width && (flags & FLAGS_ZEROPAD) && - (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) { - width--; - } - while ((flags & FLAGS_ZEROPAD) && (len < width) && (len < PRINTF_INTEGER_BUFFER_SIZE)) { - buf[len++] = '0'; - } - } - - while ((len < precision) && (len < PRINTF_INTEGER_BUFFER_SIZE)) { - buf[len++] = '0'; - } - - if (base == BASE_OCTAL && (len > unpadded_len)) { - // Since we've written some zeros, we've satisfied the alternative format leading space - // requirement - flags &= ~FLAGS_HASH; - } - } - - // handle hash - if (flags & (FLAGS_HASH | FLAGS_POINTER)) { - if (!(flags & FLAGS_PRECISION) && len && ((len == precision) || (len == width))) { - // Let's take back some padding digits to fit in what will eventually - // be the format-specific prefix - if (unpadded_len < len) { - len--; // This should suffice for BASE_OCTAL - } - if (len && (base == BASE_HEX || base == BASE_BINARY) && (unpadded_len < len)) { - len--; // ... and an extra one for 0x or 0b - } - } - if ((base == BASE_HEX) && !(flags & FLAGS_UPPERCASE) && - (len < PRINTF_INTEGER_BUFFER_SIZE)) { - buf[len++] = 'x'; - } else if ((base == BASE_HEX) && (flags & FLAGS_UPPERCASE) && - (len < PRINTF_INTEGER_BUFFER_SIZE)) { - buf[len++] = 'X'; - } else if ((base == BASE_BINARY) && (len < PRINTF_INTEGER_BUFFER_SIZE)) { - buf[len++] = 'b'; - } - if (len < PRINTF_INTEGER_BUFFER_SIZE) { - buf[len++] = '0'; - } - } - - if (len < PRINTF_INTEGER_BUFFER_SIZE) { - if (negative) { - buf[len++] = '-'; - } else if (flags & FLAGS_PLUS) { - buf[len++] = '+'; // ignore the space if the '+' exists - } else if (flags & FLAGS_SPACE) { - buf[len++] = ' '; - } - } - - out_rev_(output, buf, len, width, flags); -} - -// An internal itoa-like function -static void print_integer(output_gadget_t *output, printf_unsigned_value_t value, bool negative, - numeric_base_t base, printf_size_t precision, printf_size_t width, - printf_flags_t flags) -{ - char buf[PRINTF_INTEGER_BUFFER_SIZE]; - printf_size_t len = 0U; - - if (!value) { - if (!(flags & FLAGS_PRECISION)) { - buf[len++] = '0'; - flags &= ~FLAGS_HASH; - // We drop this flag this since either the alternative and regular modes of the - // specifier don't differ on 0 values, or (in the case of octal) we've already provided - // the special handling for this mode. - } else if (base == BASE_HEX) { - flags &= ~FLAGS_HASH; - // We drop this flag this since either the alternative and regular modes of the - // specifier don't differ on 0 values - } - } else { - do { - const char digit = (char)(value % base); - buf[len++] = (char)(digit < 10 ? '0' + digit - : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10); - value /= base; - } while (value && (len < PRINTF_INTEGER_BUFFER_SIZE)); - } - - print_integer_finalization(output, buf, len, negative, base, precision, width, flags); -} - -#if (PRINTF_SUPPORT_DECIMAL_SPECIFIERS || PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS) - -// Stores a fixed-precision representation of a double relative -// to a fixed precision (which cannot be determined by examining this structure) -struct double_components { - int_fast64_t integral; - int_fast64_t fractional; - // ... truncation of the actual fractional part of the double value, scaled - // by the precision value - bool is_negative; -}; - -#define NUM_DECIMAL_DIGITS_IN_INT64_T 18 -#define PRINTF_MAX_PRECOMPUTED_POWER_OF_10 NUM_DECIMAL_DIGITS_IN_INT64_T -static const double powers_of_10[NUM_DECIMAL_DIGITS_IN_INT64_T] = { - 1e00, 1e01, 1e02, 1e03, 1e04, 1e05, 1e06, 1e07, 1e08, - 1e09, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17}; - -#define PRINTF_MAX_SUPPORTED_PRECISION NUM_DECIMAL_DIGITS_IN_INT64_T - 1 - -// Break up a double number - which is known to be a finite non-negative number - -// into its base-10 parts: integral - before the decimal point, and fractional - after it. -// Taken the precision into account, but does not change it even internally. -static struct double_components get_components(double number, printf_size_t precision) -{ - struct double_components number_; - number_.is_negative = get_sign_bit(number); - double abs_number = (number_.is_negative) ? -number : number; - number_.integral = (int_fast64_t)abs_number; - double remainder = (abs_number - (double)number_.integral) * powers_of_10[precision]; - number_.fractional = (int_fast64_t)remainder; - - remainder -= (double)number_.fractional; - - if (remainder > 0.5) { - ++number_.fractional; - // handle rollover, e.g. case 0.99 with precision 1 is 1.0 - if ((double)number_.fractional >= powers_of_10[precision]) { - number_.fractional = 0; - ++number_.integral; - } - } else if ((remainder == 0.5) && ((number_.fractional == 0U) || (number_.fractional & 1U))) { - // if halfway, round up if odd OR if last digit is 0 - ++number_.fractional; - } - - if (precision == 0U) { - remainder = abs_number - (double)number_.integral; - if ((!(remainder < 0.5) || (remainder > 0.5)) && (number_.integral & 1)) { - // exactly 0.5 and ODD, then round up - // 1.5 -> 2, but 2.5 -> 2 - ++number_.integral; - } - } - return number_; -} - -#if PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS -struct scaling_factor { - double raw_factor; - bool multiply; // if true, need to multiply by raw_factor; otherwise need to divide by it -}; - -static double apply_scaling(double num, struct scaling_factor normalization) -{ - return normalization.multiply ? num * normalization.raw_factor : num / normalization.raw_factor; -} - -static double unapply_scaling(double normalized, struct scaling_factor normalization) -{ -#ifdef __GNUC__ -// accounting for a static analysis bug in GCC 6.x and earlier -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" -#endif - return normalization.multiply ? normalized / normalization.raw_factor - : normalized * normalization.raw_factor; -#ifdef __GNUC__ -#pragma GCC diagnostic pop -#endif -} - -static struct scaling_factor update_normalization(struct scaling_factor sf, - double extra_multiplicative_factor) -{ - struct scaling_factor result; - if (sf.multiply) { - result.multiply = true; - result.raw_factor = sf.raw_factor * extra_multiplicative_factor; - } else { - int factor_exp2 = get_exp2(get_bit_access(sf.raw_factor)); - int extra_factor_exp2 = get_exp2(get_bit_access(extra_multiplicative_factor)); - - // Divide the larger-exponent raw raw_factor by the smaller - if (PRINTF_ABS(factor_exp2) > PRINTF_ABS(extra_factor_exp2)) { - result.multiply = false; - result.raw_factor = sf.raw_factor / extra_multiplicative_factor; - } else { - result.multiply = true; - result.raw_factor = extra_multiplicative_factor / sf.raw_factor; - } - } - return result; -} - -static struct double_components get_normalized_components(bool negative, printf_size_t precision, - double non_normalized, - struct scaling_factor normalization, - int floored_exp10) -{ - struct double_components components; - components.is_negative = negative; - double scaled = apply_scaling(non_normalized, normalization); - - bool close_to_representation_extremum = - ((-floored_exp10 + (int)precision) >= DBL_MAX_10_EXP - 1); - if (close_to_representation_extremum) { - // We can't have a normalization factor which also accounts for the precision, i.e. moves - // some decimal digits into the mantissa, since it's unrepresentable, or nearly - // unrepresentable. So, we'll give up early on getting extra precision... - return get_components(negative ? -scaled : scaled, precision); - } - components.integral = (int_fast64_t)scaled; - double remainder = non_normalized - unapply_scaling((double)components.integral, normalization); - double prec_power_of_10 = powers_of_10[precision]; - struct scaling_factor account_for_precision = - update_normalization(normalization, prec_power_of_10); - double scaled_remainder = apply_scaling(remainder, account_for_precision); - double rounding_threshold = 0.5; - - components.fractional = - (int_fast64_t)scaled_remainder; // when precision == 0, the assigned value should be 0 - scaled_remainder -= - (double)components.fractional; // when precision == 0, this will not change scaled_remainder - - components.fractional += (scaled_remainder >= rounding_threshold); - if (scaled_remainder == rounding_threshold) { - // banker's rounding: Round towards the even number (making the mean error 0) - components.fractional &= ~((int_fast64_t)0x1); - } - // handle rollover, e.g. the case of 0.99 with precision 1 becoming (0,100), - // and must then be corrected into (1, 0). - // Note: for precision = 0, this will "translate" the rounding effect from - // the fractional part to the integral part where it should actually be - // felt (as prec_power_of_10 is 1) - if ((double)components.fractional >= prec_power_of_10) { - components.fractional = 0; - ++components.integral; - } - return components; -} -#endif // PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS - -static void print_broken_up_decimal(struct double_components number_, output_gadget_t *output, - printf_size_t precision, printf_size_t width, - printf_flags_t flags, char *buf, printf_size_t len) -{ - if (precision != 0U) { - // do fractional part, as an unsigned number - - printf_size_t count = precision; - - // %g/%G mandates we skip the trailing 0 digits... - if ((flags & FLAGS_ADAPT_EXP) && !(flags & FLAGS_HASH) && (number_.fractional > 0)) { - while (true) { - int_fast64_t digit = number_.fractional % 10U; - if (digit != 0) { - break; - } - --count; - number_.fractional /= 10U; - } - // ... and even the decimal point if there are no - // non-zero fractional part digits (see below) - } - - if (number_.fractional > 0 || !(flags & FLAGS_ADAPT_EXP) || (flags & FLAGS_HASH)) { - while (len < PRINTF_DECIMAL_BUFFER_SIZE) { - --count; - buf[len++] = (char)('0' + number_.fractional % 10U); - if (!(number_.fractional /= 10U)) { - break; - } - } - // add extra 0s - while ((len < PRINTF_DECIMAL_BUFFER_SIZE) && (count > 0U)) { - buf[len++] = '0'; - --count; - } - if (len < PRINTF_DECIMAL_BUFFER_SIZE) { - buf[len++] = '.'; - } - } - } else { - if ((flags & FLAGS_HASH) && (len < PRINTF_DECIMAL_BUFFER_SIZE)) { - buf[len++] = '.'; - } - } - - // Write the integer part of the number (it comes after the fractional - // since the character order is reversed) - while (len < PRINTF_DECIMAL_BUFFER_SIZE) { - buf[len++] = (char)('0' + (number_.integral % 10)); - if (!(number_.integral /= 10)) { - break; - } - } - - // pad leading zeros - if (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD)) { - if (width && (number_.is_negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) { - width--; - } - while ((len < width) && (len < PRINTF_DECIMAL_BUFFER_SIZE)) { - buf[len++] = '0'; - } - } - - if (len < PRINTF_DECIMAL_BUFFER_SIZE) { - if (number_.is_negative) { - buf[len++] = '-'; - } else if (flags & FLAGS_PLUS) { - buf[len++] = '+'; // ignore the space if the '+' exists - } else if (flags & FLAGS_SPACE) { - buf[len++] = ' '; - } - } - - out_rev_(output, buf, len, width, flags); -} - -// internal ftoa for fixed decimal floating point -static void print_decimal_number(output_gadget_t *output, double number, printf_size_t precision, - printf_size_t width, printf_flags_t flags, char *buf, - printf_size_t len) -{ - struct double_components value_ = get_components(number, precision); - print_broken_up_decimal(value_, output, precision, width, flags, buf, len); -} - -#if PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS - -// A floor function - but one which only works for numbers whose -// floor value is representable by an int. -static int bastardized_floor(double x) -{ - if (x >= 0) { - return (int)x; - } - int n = (int)x; - return (((double)n) == x) ? n : n - 1; -} - -// Computes the base-10 logarithm of the input number - which must be an actual -// positive number (not infinity or NaN, nor a sub-normal) -static double log10_of_positive(double positive_number) -{ - // The implementation follows David Gay (https://www.ampl.com/netlib/fp/dtoa.c). - // - // Since log_10 ( M * 2^x ) = log_10(M) + x , we can separate the components of - // our input number, and need only solve log_10(M) for M between 1 and 2 (as - // the base-2 mantissa is always 1-point-something). In that limited range, a - // Taylor series expansion of log10(x) should serve us well enough; and we'll - // take the mid-point, 1.5, as the point of expansion. - - double_with_bit_access dwba = get_bit_access(positive_number); - // based on the algorithm by David Gay (https://www.ampl.com/netlib/fp/dtoa.c) - int exp2 = get_exp2(dwba); - // drop the exponent, so dwba.F comes into the range [1,2) - dwba.U = (dwba.U & (((double_uint_t)(1) << DOUBLE_STORED_MANTISSA_BITS) - 1U)) | - ((double_uint_t)DOUBLE_BASE_EXPONENT << DOUBLE_STORED_MANTISSA_BITS); - double z = (dwba.F - 1.5); - return ( - // Taylor expansion around 1.5: - 0.1760912590556812420 // Expansion term 0: ln(1.5) / ln(10) - + z * 0.2895296546021678851 // Expansion term 1: (M - 1.5) * 2/3 / ln(10) -#if PRINTF_LOG10_TAYLOR_TERMS > 2 - - z * z * 0.0965098848673892950 // Expansion term 2: (M - 1.5)^2 * 2/9 / ln(10) -#if PRINTF_LOG10_TAYLOR_TERMS > 3 - + z * z * z * 0.0428932821632841311 // Expansion term 2: (M - 1.5)^3 * 8/81 / ln(10) -#endif -#endif - // exact log_2 of the exponent x, with logarithm base change - + exp2 * 0.30102999566398119521 // = exp2 * log_10(2) = exp2 * ln(2)/ln(10) - ); -} - -static double pow10_of_int(int floored_exp10) -{ - // A crude hack for avoiding undesired behavior with barely-normal or slightly-subnormal values. - if (floored_exp10 == DOUBLE_MAX_SUBNORMAL_EXPONENT_OF_10) { - return DOUBLE_MAX_SUBNORMAL_POWER_OF_10; - } - // Compute 10^(floored_exp10) but (try to) make sure that doesn't overflow - double_with_bit_access dwba; - int exp2 = bastardized_floor(floored_exp10 * 3.321928094887362 + 0.5); - const double z = floored_exp10 * 2.302585092994046 - exp2 * 0.6931471805599453; - const double z2 = z * z; - dwba.U = ((double_uint_t)(exp2) + DOUBLE_BASE_EXPONENT) << DOUBLE_STORED_MANTISSA_BITS; - // compute exp(z) using continued fractions, - // see https://en.wikipedia.org/wiki/Exponential_function#Continued_fractions_for_ex - dwba.F *= 1 + 2 * z / (2 - z + (z2 / (6 + (z2 / (10 + z2 / 14))))); - return dwba.F; -} - -static void print_exponential_number(output_gadget_t *output, double number, - printf_size_t precision, printf_size_t width, - printf_flags_t flags, char *buf, printf_size_t len) -{ - const bool negative = get_sign_bit(number); - // This number will decrease gradually (by factors of 10) as we "extract" the exponent out of it - double abs_number = negative ? -number : number; - - int floored_exp10; - bool abs_exp10_covered_by_powers_table; - struct scaling_factor normalization; - - // Determine the decimal exponent - if (abs_number == 0.0) { - // TODO: This is a special-case for 0.0 (and -0.0); but proper handling is required for - // denormals more generally. - floored_exp10 = - 0; // ... and no need to set a normalization factor or check the powers table - } else { - double exp10 = log10_of_positive(abs_number); - floored_exp10 = bastardized_floor(exp10); - double p10 = pow10_of_int(floored_exp10); - // correct for rounding errors - if (abs_number < p10) { - floored_exp10--; - p10 /= 10; - } - abs_exp10_covered_by_powers_table = - PRINTF_ABS(floored_exp10) < PRINTF_MAX_PRECOMPUTED_POWER_OF_10; - normalization.raw_factor = - abs_exp10_covered_by_powers_table ? powers_of_10[PRINTF_ABS(floored_exp10)] : p10; - } - - // We now begin accounting for the widths of the two parts of our printed field: - // the decimal part after decimal exponent extraction, and the base-10 exponent part. - // For both of these, the value of 0 has a special meaning, but not the same one: - // a 0 exponent-part width means "don't print the exponent"; a 0 decimal-part width - // means "use as many characters as necessary". - - bool fall_back_to_decimal_only_mode = false; - if (flags & FLAGS_ADAPT_EXP) { - int required_significant_digits = (precision == 0) ? 1 : (int)precision; - // Should we want to fall-back to "%f" mode, and only print the decimal part? - fall_back_to_decimal_only_mode = - (floored_exp10 >= -4 && floored_exp10 < required_significant_digits); - // Now, let's adjust the precision - // This also decided how we adjust the precision value - as in "%g" mode, - // "precision" is the number of _significant digits_, and this is when we "translate" - // the precision value to an actual number of decimal digits. - int precision_ = - fall_back_to_decimal_only_mode - ? (int)precision - 1 - floored_exp10 - : (int)precision - 1; // the presence of the exponent ensures only one significant - // digit comes before the decimal point - precision = (precision_ > 0 ? (unsigned)precision_ : 0U); - flags |= FLAGS_PRECISION; // make sure print_broken_up_decimal respects our choice above - } - - normalization.multiply = (floored_exp10 < 0 && abs_exp10_covered_by_powers_table); - bool should_skip_normalization = (fall_back_to_decimal_only_mode || floored_exp10 == 0); - struct double_components decimal_part_components = - should_skip_normalization ? get_components(negative ? -abs_number : abs_number, precision) - : get_normalized_components(negative, precision, abs_number, - normalization, floored_exp10); - - // Account for roll-over, e.g. rounding from 9.99 to 100.0 - which effects - // the exponent and may require additional tweaking of the parts - if (fall_back_to_decimal_only_mode) { - if ((flags & FLAGS_ADAPT_EXP) && floored_exp10 >= -1 && - decimal_part_components.integral == powers_of_10[floored_exp10 + 1]) { - floored_exp10++; // Not strictly necessary, since floored_exp10 is no longer really used - precision--; - // ... and it should already be the case that decimal_part_components.fractional == 0 - } - // TODO: What about rollover strictly within the fractional part? - } else { - if (decimal_part_components.integral >= 10) { - floored_exp10++; - decimal_part_components.integral = 1; - decimal_part_components.fractional = 0; - } - } - - // the floored_exp10 format is "E%+03d" and largest possible floored_exp10 value for a 64-bit - // double is "307" (for 2^1023), so we set aside 4-5 characters overall - printf_size_t exp10_part_width = fall_back_to_decimal_only_mode ? 0U - : (PRINTF_ABS(floored_exp10) < 100) ? 4U - : 5U; - - printf_size_t decimal_part_width = - ((flags & FLAGS_LEFT) && exp10_part_width) - ? - // We're padding on the right, so the width constraint is the exponent part's - // problem, not the decimal part's, so we'll use as many characters as we need: - 0U - : - // We're padding on the left; so the width constraint is the decimal part's - // problem. Well, can both the decimal part and the exponent part fit within our overall - // width? - ((width > exp10_part_width) - ? - // Yes, so we limit our decimal part's width. - // (Note this is trivially valid even if we've fallen back to "%f" mode) - width - exp10_part_width - : - // No; we just give up on any restriction on the decimal part and use as many - // characters as we need - 0U); - - const printf_size_t printed_exponential_start_pos = output->pos; - print_broken_up_decimal(decimal_part_components, output, precision, decimal_part_width, flags, - buf, len); - - if (!fall_back_to_decimal_only_mode) { - putchar_via_gadget(output, (flags & FLAGS_UPPERCASE) ? 'E' : 'e'); - print_integer(output, ABS_FOR_PRINTING(floored_exp10), floored_exp10 < 0, 10, 0, - exp10_part_width - 1, FLAGS_ZEROPAD | FLAGS_PLUS); - if (flags & FLAGS_LEFT) { - // We need to right-pad with spaces to meet the width requirement - while (output->pos - printed_exponential_start_pos < width) { - putchar_via_gadget(output, ' '); - } - } - } -} -#endif // PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS - -static void print_floating_point(output_gadget_t *output, double value, printf_size_t precision, - printf_size_t width, printf_flags_t flags, bool prefer_exponential) -{ - char buf[PRINTF_DECIMAL_BUFFER_SIZE]; - printf_size_t len = 0U; - - // test for special values - if (value != value) { - out_rev_(output, "nan", 3, width, flags); - return; - } - if (value < -DBL_MAX) { - out_rev_(output, "fni-", 4, width, flags); - return; - } - if (value > DBL_MAX) { - out_rev_(output, (flags & FLAGS_PLUS) ? "fni+" : "fni", (flags & FLAGS_PLUS) ? 4U : 3U, - width, flags); - return; - } - - if (!prefer_exponential && - ((value > PRINTF_FLOAT_NOTATION_THRESHOLD) || (value < -PRINTF_FLOAT_NOTATION_THRESHOLD))) { - // The required behavior of standard printf is to print _every_ integral-part digit -- which - // could mean printing hundreds of characters, overflowing any fixed internal buffer and - // necessitating a more complicated implementation. -#if PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS - print_exponential_number(output, value, precision, width, flags, buf, len); -#endif - return; - } - - // set default precision, if not set explicitly - if (!(flags & FLAGS_PRECISION)) { - precision = PRINTF_DEFAULT_FLOAT_PRECISION; - } - - // limit precision so that our integer holding the fractional part does not overflow - while ((len < PRINTF_DECIMAL_BUFFER_SIZE) && (precision > PRINTF_MAX_SUPPORTED_PRECISION)) { - buf[len++] = '0'; // This respects the precision in terms of result length only - precision--; - } - -#if PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS - if (prefer_exponential) - print_exponential_number(output, value, precision, width, flags, buf, len); - else -#endif - print_decimal_number(output, value, precision, width, flags, buf, len); -} - -#endif // (PRINTF_SUPPORT_DECIMAL_SPECIFIERS || PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS) - -// Advances the format pointer past the flags, and returns the parsed flags -// due to the characters passed -static printf_flags_t parse_flags(const char **format) -{ - printf_flags_t flags = 0U; - do { - switch (**format) { - case '0': - flags |= FLAGS_ZEROPAD; - (*format)++; - break; - case '-': - flags |= FLAGS_LEFT; - (*format)++; - break; - case '+': - flags |= FLAGS_PLUS; - (*format)++; - break; - case ' ': - flags |= FLAGS_SPACE; - (*format)++; - break; - case '#': - flags |= FLAGS_HASH; - (*format)++; - break; - default: - return flags; - } - } while (true); -} - -static inline void format_string_loop(output_gadget_t *output, const char *format, va_list args) -{ -#if PRINTF_CHECK_FOR_NUL_IN_FORMAT_SPECIFIER -#define ADVANCE_IN_FORMAT_STRING(cptr_) \ - do { \ - (cptr_)++; \ - if (!*(cptr_)) \ - return; \ - } while (0) -#else -#define ADVANCE_IN_FORMAT_STRING(cptr_) (cptr_)++ -#endif - - while (*format) { - if (*format != '%') { - // A regular content character - putchar_via_gadget(output, *format); - format++; - continue; - } - // We're parsing a format specifier: %[flags][width][.precision][length] - ADVANCE_IN_FORMAT_STRING(format); - - printf_flags_t flags = parse_flags(&format); - - // evaluate width field - printf_size_t width = 0U; - if (is_digit_(*format)) { - width = (printf_size_t)atou_(&format); - } else if (*format == '*') { - const int w = va_arg(args, int); - if (w < 0) { - flags |= FLAGS_LEFT; // reverse padding - width = (printf_size_t)-w; - } else { - width = (printf_size_t)w; - } - ADVANCE_IN_FORMAT_STRING(format); - } - - // evaluate precision field - printf_size_t precision = 0U; - if (*format == '.') { - flags |= FLAGS_PRECISION; - ADVANCE_IN_FORMAT_STRING(format); - if (is_digit_(*format)) { - precision = (printf_size_t)atou_(&format); - } else if (*format == '*') { - const int precision_ = va_arg(args, int); - precision = precision_ > 0 ? (printf_size_t)precision_ : 0U; - ADVANCE_IN_FORMAT_STRING(format); - } - } - - // evaluate length field - switch (*format) { -#ifdef PRINTF_SUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS - case 'I': { - ADVANCE_IN_FORMAT_STRING(format); - // Greedily parse for size in bits: 8, 16, 32 or 64 - switch (*format) { - case '8': - flags |= FLAGS_INT8; - ADVANCE_IN_FORMAT_STRING(format); - break; - case '1': - ADVANCE_IN_FORMAT_STRING(format); - if (*format == '6') { - format++; - flags |= FLAGS_INT16; - } - break; - case '3': - ADVANCE_IN_FORMAT_STRING(format); - if (*format == '2') { - ADVANCE_IN_FORMAT_STRING(format); - flags |= FLAGS_INT32; - } - break; - case '6': - ADVANCE_IN_FORMAT_STRING(format); - if (*format == '4') { - ADVANCE_IN_FORMAT_STRING(format); - flags |= FLAGS_INT64; - } - break; - default: - break; - } - break; - } -#endif - case 'l': - flags |= FLAGS_LONG; - ADVANCE_IN_FORMAT_STRING(format); - if (*format == 'l') { - flags |= FLAGS_LONG_LONG; - ADVANCE_IN_FORMAT_STRING(format); - } - break; - case 'h': - flags |= FLAGS_SHORT; - ADVANCE_IN_FORMAT_STRING(format); - if (*format == 'h') { - flags |= FLAGS_CHAR; - ADVANCE_IN_FORMAT_STRING(format); - } - break; - case 't': - flags |= (sizeof(ptrdiff_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG); - ADVANCE_IN_FORMAT_STRING(format); - break; - case 'j': - flags |= (sizeof(intmax_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG); - ADVANCE_IN_FORMAT_STRING(format); - break; - case 'z': - flags |= (sizeof(size_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG); - ADVANCE_IN_FORMAT_STRING(format); - break; - default: - break; - } - - // evaluate specifier - switch (*format) { - case 'd': - case 'i': - case 'u': - case 'x': - case 'X': - case 'o': - case 'b': { - - if (*format == 'd' || *format == 'i') { - flags |= FLAGS_SIGNED; - } - - numeric_base_t base; - if (*format == 'x' || *format == 'X') { - base = BASE_HEX; - } else if (*format == 'o') { - base = BASE_OCTAL; - } else if (*format == 'b') { - base = BASE_BINARY; - } else { - base = BASE_DECIMAL; - flags &= ~FLAGS_HASH; // decimal integers have no alternative presentation - } - - if (*format == 'X') { - flags |= FLAGS_UPPERCASE; - } - - format++; - // ignore '0' flag when precision is given - if (flags & FLAGS_PRECISION) { - flags &= ~FLAGS_ZEROPAD; - } - - if (flags & FLAGS_SIGNED) { - // A signed specifier: d, i or possibly I + bit size if enabled - - if (flags & FLAGS_LONG_LONG) { -#if PRINTF_SUPPORT_LONG_LONG - const long long value = va_arg(args, long long); - print_integer(output, ABS_FOR_PRINTING(value), value < 0, base, precision, - width, flags); -#endif - } else if (flags & FLAGS_LONG) { - const long value = va_arg(args, long); - print_integer(output, ABS_FOR_PRINTING(value), value < 0, base, precision, - width, flags); - } else { - // We never try to interpret the argument as something potentially-smaller than - // int, due to integer promotion rules: Even if the user passed a short int, - // short unsigned etc. - these will come in after promotion, as int's (or - // unsigned for the case of short unsigned when it has the same size as int) - const int value = (flags & FLAGS_CHAR) ? (signed char)va_arg(args, int) - : (flags & FLAGS_SHORT) ? (short int)va_arg(args, int) - : va_arg(args, int); - print_integer(output, ABS_FOR_PRINTING(value), value < 0, base, precision, - width, flags); - } - } else { - // An unsigned specifier: u, x, X, o, b - - flags &= ~(FLAGS_PLUS | FLAGS_SPACE); - - if (flags & FLAGS_LONG_LONG) { -#if PRINTF_SUPPORT_LONG_LONG - print_integer(output, (printf_unsigned_value_t)va_arg(args, unsigned long long), - false, base, precision, width, flags); -#endif - } else if (flags & FLAGS_LONG) { - print_integer(output, (printf_unsigned_value_t)va_arg(args, unsigned long), - false, base, precision, width, flags); - } else { - const unsigned int value = - (flags & FLAGS_CHAR) ? (unsigned char)va_arg(args, unsigned int) - : (flags & FLAGS_SHORT) ? (unsigned short int)va_arg(args, unsigned int) - : va_arg(args, unsigned int); - print_integer(output, (printf_unsigned_value_t)value, false, base, precision, - width, flags); - } - } - break; - } -#if PRINTF_SUPPORT_DECIMAL_SPECIFIERS - case 'f': - case 'F': - if (*format == 'F') - flags |= FLAGS_UPPERCASE; - print_floating_point(output, va_arg(args, double), precision, width, flags, - PRINTF_PREFER_DECIMAL); - format++; - break; -#endif -#if PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS - case 'e': - case 'E': - case 'g': - case 'G': - if ((*format == 'g') || (*format == 'G')) - flags |= FLAGS_ADAPT_EXP; - if ((*format == 'E') || (*format == 'G')) - flags |= FLAGS_UPPERCASE; - print_floating_point(output, va_arg(args, double), precision, width, flags, - PRINTF_PREFER_EXPONENTIAL); - format++; - break; -#endif // PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS - case 'c': { - printf_size_t l = 1U; - // pre padding - if (!(flags & FLAGS_LEFT)) { - while (l++ < width) { - putchar_via_gadget(output, ' '); - } - } - // char output - putchar_via_gadget(output, (char)va_arg(args, int)); - // post padding - if (flags & FLAGS_LEFT) { - while (l++ < width) { - putchar_via_gadget(output, ' '); - } - } - format++; - break; - } - - case 's': { - const char *p = va_arg(args, char *); - if (p == NULL) { - out_rev_(output, ")llun(", 6, width, flags); - } else { - printf_size_t l = - strnlen_s_(p, precision ? precision : PRINTF_MAX_POSSIBLE_BUFFER_SIZE); - // pre padding - if (flags & FLAGS_PRECISION) { - l = (l < precision ? l : precision); - } - if (!(flags & FLAGS_LEFT)) { - while (l++ < width) { - putchar_via_gadget(output, ' '); - } - } - // string output - while ((*p != 0) && (!(flags & FLAGS_PRECISION) || precision)) { - putchar_via_gadget(output, *(p++)); - --precision; - } - // post padding - if (flags & FLAGS_LEFT) { - while (l++ < width) { - putchar_via_gadget(output, ' '); - } - } - } - format++; - break; - } - - case 'p': { - width = sizeof(void *) * 2U + 2; // 2 hex chars per byte + the "0x" prefix - flags |= FLAGS_ZEROPAD | FLAGS_POINTER; - uintptr_t value = (uintptr_t)va_arg(args, void *); - (value == (uintptr_t)NULL) ? out_rev_(output, ")lin(", 5, width, flags) - : print_integer(output, (printf_unsigned_value_t)value, - false, BASE_HEX, precision, width, flags); - format++; - break; - } - - case '%': - putchar_via_gadget(output, '%'); - format++; - break; - - // Many people prefer to disable support for %n, as it lets the caller - // engineer a write to an arbitrary location, of a value the caller - // effectively controls - which could be a security concern in some cases. -#if PRINTF_SUPPORT_WRITEBACK_SPECIFIER - case 'n': { - if (flags & FLAGS_CHAR) - *(va_arg(args, char *)) = (char)output->pos; - else if (flags & FLAGS_SHORT) - *(va_arg(args, short *)) = (short)output->pos; - else if (flags & FLAGS_LONG) - *(va_arg(args, long *)) = (long)output->pos; -#if PRINTF_SUPPORT_LONG_LONG - else if (flags & FLAGS_LONG_LONG) - *(va_arg(args, long long *)) = (long long int)output->pos; -#endif // PRINTF_SUPPORT_LONG_LONG - else - *(va_arg(args, int *)) = (int)output->pos; - format++; - break; - } -#endif // PRINTF_SUPPORT_WRITEBACK_SPECIFIER - - default: - putchar_via_gadget(output, *format); - format++; - break; - } - } -} - -// internal vsnprintf - used for implementing _all library functions -static int vsnprintf_impl(output_gadget_t *output, const char *format, va_list args) -{ - // Note: The library only calls vsnprintf_impl() with output->pos being 0. However, it is - // possible to call this function with a non-zero pos value for some "remedial printing". - format_string_loop(output, format, args); - - // termination - append_termination_with_gadget(output); - - // return written chars without terminating \0 - return (int)output->pos; -} - -/////////////////////////////////////////////////////////////////////////////// - -// int vprintf_(const char *format, va_list arg) -// { -// output_gadget_t gadget = extern_putchar_gadget(); -// return vsnprintf_impl(&gadget, format, arg); -// } - -int vsnprintf_(char *s, size_t n, const char *format, va_list arg) -{ - output_gadget_t gadget = buffer_gadget(s, n); - return vsnprintf_impl(&gadget, format, arg); -} - -int vsprintf_(char *s, const char *format, va_list arg) -{ - return vsnprintf_(s, PRINTF_MAX_POSSIBLE_BUFFER_SIZE, format, arg); -} - -int vfctprintf(void (*out)(char c, void *extra_arg), void *extra_arg, const char *format, - va_list arg) -{ - output_gadget_t gadget = function_gadget(out, extra_arg); - return vsnprintf_impl(&gadget, format, arg); -} - -// int printf_(const char *format, ...) -// { -// va_list args; -// va_start(args, format); -// const int ret = vprintf_(format, args); -// va_end(args); -// return ret; -// } - -int sprintf_(char *s, const char *format, ...) -{ - va_list args; - va_start(args, format); - const int ret = vsprintf_(s, format, args); - va_end(args); - return ret; -} - -int snprintf_(char *s, size_t n, const char *format, ...) -{ - va_list args; - va_start(args, format); - const int ret = vsnprintf_(s, n, format, args); - va_end(args); - return ret; -} - -int fctprintf(void (*out)(char c, void *extra_arg), void *extra_arg, const char *format, ...) -{ - va_list args; - va_start(args, format); - const int ret = vfctprintf(out, extra_arg, format, args); - va_end(args); - return ret; -} diff --git a/ulib/ruxlibc/c/printf.h b/ulib/ruxlibc/c/printf.h deleted file mode 100644 index 45a0750b0..000000000 --- a/ulib/ruxlibc/c/printf.h +++ /dev/null @@ -1,224 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -/** - * @author (c) Eyal Rozenberg - * 2021-2022, Haifa, Palestine/Israel - * @author (c) Marco Paland (info@paland.com) - * 2014-2019, PALANDesign Hannover, Germany - * - * @note Others have made smaller contributions to this file: see the - * contributors page at https://github.com/eyalroz/printf/graphs/contributors - * or ask one of the authors. - * - * @brief Small stand-alone implementation of the printf family of functions - * (`(v)printf`, `(v)s(n)printf` etc., geared towards use on embedded systems with - * a very limited resources. - * - * @note the implementations are thread-safe; re-entrant; use no functions from - * the standard library; and do not dynamically allocate any memory. - * - * @license The MIT License (MIT) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef PRINTF_H_ -#define PRINTF_H_ - -#ifdef __cplusplus -#include -#include -extern "C" { -#else -#include -#include -#endif - -#ifdef __GNUC__ -#if ((__GNUC__ == 4 && __GNUC_MINOR__ >= 4) || __GNUC__ > 4) -#define ATTR_PRINTF(one_based_format_index, first_arg) \ - __attribute__((format(gnu_printf, (one_based_format_index), (first_arg)))) -#else -#define ATTR_PRINTF(one_based_format_index, first_arg) \ - __attribute__((format(printf, (one_based_format_index), (first_arg)))) -#endif -#define ATTR_VPRINTF(one_based_format_index) ATTR_PRINTF((one_based_format_index), 0) -#else -#define ATTR_PRINTF(one_based_format_index, first_arg) -#define ATTR_VPRINTF(one_based_format_index) -#endif - -#ifndef PRINTF_ALIAS_STANDARD_FUNCTION_NAMES -#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES 0 -#endif - -#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD -#define printf_ printf -#define sprintf_ sprintf -#define vsprintf_ vsprintf -#define snprintf_ snprintf -#define vsnprintf_ vsnprintf -#define vprintf_ vprintf -#endif - -// If you want to include this implementation file directly rather than -// link against, this will let you control the functions' visibility, -// e.g. make them static so as not to clash with other objects also -// using them. -#ifndef PRINTF_VISIBILITY -#define PRINTF_VISIBILITY -#endif - -/** - * Prints/send a single character to some opaque output entity - * - * @note This function is not implemented by the library, only declared; you must provide an - * implementation if you wish to use the @ref printf / @ref vprintf function (and possibly - * for linking against the library, if your toolchain does not support discarding unused functions) - * - * @note The output could be as simple as a wrapper for the `write()` system call on a Unix-like - * system, or even libc's @ref putchar , for replicating actual functionality of libc's @ref printf - * function; but on an embedded system it may involve interaction with a special output device, - * like a UART, etc. - * - * @note in libc's @ref putchar, the parameter type is an int; this was intended to support the - * representation of either a proper character or EOF in a variable - but this is really not - * meaningful to pass into @ref putchar and is discouraged today. See further discussion in: - * @link https://stackoverflow.com/q/17452847/1593077 - * - * @param c the single character to print - */ -// PRINTF_VISIBILITY -// void putchar_(char c); - -/** - * An implementation of the C standard's printf/vprintf - * - * @note you must implement a @ref putchar_ function for using this function - it invokes @ref - * putchar_ rather than directly performing any I/O (which insulates it from any dependence on the - * operating system and external libraries). - * - * @param format A string specifying the format of the output, with %-marked specifiers of how to - * interpret additional arguments. - * @param arg Additional arguments to the function, one for each %-specifier in @p format string - * @return The number of characters written into @p s, not counting the terminating null character - */ -///@{ -// PRINTF_VISIBILITY -// int printf_(const char* format, ...) ATTR_PRINTF(1, 2); -// PRINTF_VISIBILITY -// int vprintf_(const char* format, va_list arg) ATTR_VPRINTF(1); -///@} - -/** - * An implementation of the C standard's sprintf/vsprintf - * - * @note For security considerations (the potential for exceeding the buffer bounds), please - * consider using the size-constrained variant, @ref snprintf / @ref vsnprintf , instead. - * - * @param s An array in which to store the formatted string. It must be large enough to fit the - * formatted output! - * @param format A string specifying the format of the output, with %-marked specifiers of how to - * interpret additional arguments. - * @param arg Additional arguments to the function, one for each specifier in @p format - * @return The number of characters written into @p s, not counting the terminating null character - */ -///@{ -PRINTF_VISIBILITY -int sprintf_(char *s, const char *format, ...) ATTR_PRINTF(2, 3); -PRINTF_VISIBILITY -int vsprintf_(char *s, const char *format, va_list arg) ATTR_VPRINTF(2); -///@} - -/** - * An implementation of the C standard's snprintf/vsnprintf - * - * @param s An array in which to store the formatted string. It must be large enough to fit either - * the entire formatted output, or at least @p n characters. Alternatively, it can be NULL, in which - * case nothing will be printed, and only the number of characters which _could_ have been printed - * is tallied and returned. - * @param n The maximum number of characters to write to the array, including a terminating null - * character - * @param format A string specifying the format of the output, with %-marked specifiers of how to - * interpret additional arguments. - * @param arg Additional arguments to the function, one for each specifier in @p format - * @return The number of characters that COULD have been written into @p s, not counting the - * terminating null character. A value equal or larger than @p n indicates truncation. Only when the - * returned value is non-negative and less than @p n, the null-terminated string has been fully and - * successfully printed. - */ -///@{ -PRINTF_VISIBILITY -int snprintf_(char *s, size_t count, const char *format, ...) ATTR_PRINTF(3, 4); -PRINTF_VISIBILITY -int vsnprintf_(char *s, size_t count, const char *format, va_list arg) ATTR_VPRINTF(3); -///@} - -/** - * printf/vprintf with user-specified output function - * - * An alternative to @ref printf_, in which the output function is specified dynamically - * (rather than @ref putchar_ being used) - * - * @param out An output function which takes one character and a type-erased additional parameters - * @param extra_arg The type-erased argument to pass to the output function @p out with each call - * @param format A string specifying the format of the output, with %-marked specifiers of how to - * interpret additional arguments. - * @param arg Additional arguments to the function, one for each specifier in @p format - * @return The number of characters for which the output f unction was invoked, not counting the - * terminating null character - * - */ -PRINTF_VISIBILITY -int fctprintf(void (*out)(char c, void *extra_arg), void *extra_arg, const char *format, ...) - ATTR_PRINTF(3, 4); -PRINTF_VISIBILITY -int vfctprintf(void (*out)(char c, void *extra_arg), void *extra_arg, const char *format, - va_list arg) ATTR_VPRINTF(3); - -#ifdef __cplusplus -} // extern "C" -#endif - -#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD -#undef printf_ -#undef sprintf_ -#undef vsprintf_ -#undef snprintf_ -#undef vsnprintf_ -#undef vprintf_ -#else -#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT -#define printf printf_ -#define sprintf sprintf_ -#define vsprintf vsprintf_ -#define snprintf snprintf_ -#define vsnprintf vsnprintf_ -#define vprintf vprintf_ -#endif -#endif - -#endif // PRINTF_H_ diff --git a/ulib/ruxlibc/c/printf_config.h b/ulib/ruxlibc/c/printf_config.h deleted file mode 100644 index 2086f9276..000000000 --- a/ulib/ruxlibc/c/printf_config.h +++ /dev/null @@ -1,23 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#ifndef PRINTF_CONFIG_H -#define PRINTF_CONFIG_H - -#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES 1 - -#ifndef RUX_CONFIG_FP_SIMD - -#define PRINTF_SUPPORT_DECIMAL_SPECIFIERS 0 - -#define PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERS 0 - -#endif // RUX_CONFIG_FP_SIMD - -#endif // PRINTF_CONFIG_H diff --git a/ulib/ruxlibc/c/pthread.c b/ulib/ruxlibc/c/pthread.c deleted file mode 100644 index 363f02d99..000000000 --- a/ulib/ruxlibc/c/pthread.c +++ /dev/null @@ -1,80 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#ifdef RUX_CONFIG_MULTITASK - -#include -#include -#include -#include -#include - -int pthread_setcancelstate(int new, int *old) -{ - unimplemented(); - return 0; -} - -int pthread_setcanceltype(int new, int *old) -{ - unimplemented(); - return 0; -} - -// TODO -void pthread_testcancel(void) -{ - unimplemented(); - return; -} - -// TODO -int pthread_cancel(pthread_t t) -{ - unimplemented(); - return 0; -} - -// TODO -int pthread_setname_np(pthread_t thread, const char *name) -{ - unimplemented(); - return 0; -} - -#define DEFAULT_STACK_SIZE 131072 -#define DEFAULT_GUARD_SIZE 8192 - -// TODO -int pthread_attr_init(pthread_attr_t *a) -{ - *a = (pthread_attr_t){0}; - // __acquire_ptc(); - a->_a_stacksize = DEFAULT_STACK_SIZE; - a->_a_guardsize = DEFAULT_GUARD_SIZE; - // __release_ptc(); - return 0; -} - -int pthread_attr_getstacksize(const pthread_attr_t *restrict a, size_t *restrict size) -{ - *size = a->_a_stacksize; - return 0; -} - -int pthread_attr_setstacksize(pthread_attr_t *a, size_t size) -{ - if (size - PTHREAD_STACK_MIN > SIZE_MAX / 4) - return EINVAL; - a->_a_stackaddr = 0; - a->_a_stacksize = size; - return 0; -} - -#endif // RUX_CONFIG_MULTITASK diff --git a/ulib/ruxlibc/c/pwd.c b/ulib/ruxlibc/c/pwd.c deleted file mode 100644 index 0b0df0e1a..000000000 --- a/ulib/ruxlibc/c/pwd.c +++ /dev/null @@ -1,52 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A - * PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. - */ - -#include -#include -#include -#include -#include -#include - -static struct passwd pw__ = { - .pw_name = RUX_DEFAULT_USER, - .pw_passwd = RUX_DEFAULT_PASS, - .pw_uid = RUX_DEFAULT_UID, - .pw_gid = RUX_DEFAULT_GID, - .pw_gecos = RUX_DEFAULT_USER, - .pw_dir = "/", - .pw_shell = "", -}; - -int getpwnam_r(const char *name, struct passwd *pw, char *buf, size_t size, struct passwd **res) -{ - unimplemented(); - return 0; -} - -int getpwuid_r(uid_t uid, struct passwd *pw, char *buf, size_t size, struct passwd **res) -{ - unimplemented(); - return 0; -} - -struct passwd *getpwnam(const char *name) -{ - struct passwd *pwd; - - if (name && !strcmp(name, pw__.pw_name)) - pwd = &pw__; - else { - pwd = NULL; - errno = ENOENT; - } - - return pwd; -} diff --git a/ulib/ruxlibc/c/resource.c b/ulib/ruxlibc/c/resource.c deleted file mode 100644 index a50b640e2..000000000 --- a/ulib/ruxlibc/c/resource.c +++ /dev/null @@ -1,27 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A - * PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. - */ - -#include -#include -#include - -// TODO -int getrusage(int __who, struct rusage *__usage) -{ - unimplemented(); - return 0; -} - -int setpriority(int which, id_t who, int prio) -{ - unimplemented(); - return 0; -} - diff --git a/ulib/ruxlibc/c/sched.c b/ulib/ruxlibc/c/sched.c deleted file mode 100644 index ad24fa270..000000000 --- a/ulib/ruxlibc/c/sched.c +++ /dev/null @@ -1,24 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include - -// TODO -int sched_setaffinity(pid_t __pid, size_t __cpusetsize, const cpu_set_t *__cpuset) -{ - unimplemented(); - return 0; -} - -int sched_yield(void) -{ - unimplemented(); - return 0; -} diff --git a/ulib/ruxlibc/c/select.c b/ulib/ruxlibc/c/select.c deleted file mode 100644 index 555b21739..000000000 --- a/ulib/ruxlibc/c/select.c +++ /dev/null @@ -1,26 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#ifdef RUX_CONFIG_SELECT - -#include -#include -#include -#include -#include - -int pselect(int n, fd_set *restrict rfds, fd_set *restrict wfds, fd_set *restrict efds, - const struct timespec *restrict ts, const sigset_t *restrict mask) -{ - struct timeval tv = {ts->tv_sec, ts->tv_nsec / 1000}; - select(n, rfds, wfds, efds, &tv); - return 0; -} - -#endif // RUX_CONFIG_SELECT diff --git a/ulib/ruxlibc/c/semaphore.c b/ulib/ruxlibc/c/semaphore.c deleted file mode 100644 index 970ea656a..000000000 --- a/ulib/ruxlibc/c/semaphore.c +++ /dev/null @@ -1,39 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include - -// TODO -int sem_destroy(sem_t *sem) -{ - unimplemented(); - return 0; -} - -// TODO -int sem_init(sem_t *sem, int pshared, unsigned int value) -{ - unimplemented(); - return 0; -} - -// TODO -int sem_post(sem_t *sem) -{ - unimplemented(); - return 0; -} - -// TODO -int sem_wait(sem_t *sem) -{ - unimplemented(); - return 0; -} diff --git a/ulib/ruxlibc/c/sendfile.c b/ulib/ruxlibc/c/sendfile.c deleted file mode 100644 index 1a7f1f2ed..000000000 --- a/ulib/ruxlibc/c/sendfile.c +++ /dev/null @@ -1,16 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include - -ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count){ - unimplemented(); - return 0; -} diff --git a/ulib/ruxlibc/c/signal.c b/ulib/ruxlibc/c/signal.c deleted file mode 100644 index 6c94a5da5..000000000 --- a/ulib/ruxlibc/c/signal.c +++ /dev/null @@ -1,101 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A - * PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. - */ - -#include -#include -#include -#include - -extern int sigaction_inner(int, const struct sigaction *, struct sigaction *); - -void (*signal(int signum, void (*handler)(int)))(int) -{ - struct sigaction old; - struct sigaction act = { - .sa_handler = handler, .sa_flags = SA_RESTART, /* BSD signal semantics */ - }; - - if (sigaction_inner(signum, &act, &old) < 0) - return SIG_ERR; - - return (old.sa_flags & SA_SIGINFO) ? NULL : old.sa_handler; -} - -int sigaction(int sig, const struct sigaction *restrict act, struct sigaction *restrict oact) -{ - return sigaction_inner(sig, act, oact); -} - -// TODO -int kill(pid_t __pid, int __sig) -{ - unimplemented(); - return 0; -} - -int sigemptyset(sigset_t *set) -{ - set->__bits[0] = 0; - if (sizeof(long) == 4 || _NSIG > 65) - set->__bits[1] = 0; - if (sizeof(long) == 4 && _NSIG > 65) { - set->__bits[2] = 0; - set->__bits[3] = 0; - } - return 0; -} - -// TODO -int raise(int __sig) -{ - unimplemented(); - return 0; -} - -int sigaddset(sigset_t *set, int sig) -{ - unsigned s = sig - 1; - if (s >= _NSIG - 1 || sig - 32U < 3) { - errno = EINVAL; - return -1; - } - set->__bits[s / 8 / sizeof *set->__bits] |= 1UL << (s & (8 * sizeof *set->__bits - 1)); - return 0; -} - -// TODO -int pthread_sigmask(int __how, const sigset_t *restrict __newmask, sigset_t *restrict __oldmask) -{ - unimplemented(); - return 0; -} - -// TODO -int sigprocmask(int how, const sigset_t *__restrict set, sigset_t *__restrict oldset) -{ - unimplemented(); - return 0; -} - -// TODO -int sigsuspend(const sigset_t *mask) -{ - unimplemented(); - return 0; -} - -#ifdef RUX_CONFIG_MULTITASK -// TODO -int pthread_kill(pthread_t t, int sig) -{ - unimplemented(); - return 0; -} -#endif diff --git a/ulib/ruxlibc/c/socket.c b/ulib/ruxlibc/c/socket.c deleted file mode 100644 index 9b60da7d3..000000000 --- a/ulib/ruxlibc/c/socket.c +++ /dev/null @@ -1,94 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#ifdef RUX_CONFIG_NET - -#include -#include -#include -#include -#include -#include - -int accept4(int fd, struct sockaddr *restrict addr, socklen_t *restrict len, int flg) -{ - if (!flg) - return accept(fd, addr, len); - if (flg & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) { - errno = EINVAL; - return -1; - } - int ret = accept(fd, addr, len); - if (ret < 0) - return ret; - if (flg & SOCK_CLOEXEC) - fcntl(ret, F_SETFD, FD_CLOEXEC); - if (flg & SOCK_NONBLOCK) - fcntl(ret, F_SETFL, O_NONBLOCK); - return ret; -} - -int getsockopt(int fd, int level, int optname, void *restrict optval, socklen_t *restrict optlen) -{ - unimplemented(); - return -1; -} - -int setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen) -{ - unimplemented("fd: %d, level: %d, optname: %d, optval: %d, optlen: %d", fd, level, optname, - *(int *)optval, optlen); - return 0; -} - -// TODO: remove this function in future work -ssize_t ax_sendmsg(int fd, const struct msghdr *msg, int flags); - -ssize_t sendmsg(int fd, const struct msghdr *msg, int flags) -{ -#if LONG_MAX > INT_MAX - struct msghdr h; - /* Kernels before 2.6.38 set SCM_MAX_FD to 255, allocate enough - * space to support an SCM_RIGHTS ancillary message with 255 fds. - * Kernels since 2.6.38 set SCM_MAX_FD to 253. */ - struct cmsghdr chbuf[CMSG_SPACE(255*sizeof(int))/sizeof(struct cmsghdr)+1], *c; - if (msg) { - h = *msg; - h.__pad1 = h.__pad2 = 0; - msg = &h; - if (h.msg_controllen) { - if (h.msg_controllen > sizeof chbuf) { - errno = ENOMEM; - return -1; - } - memcpy(chbuf, h.msg_control, h.msg_controllen); - h.msg_control = chbuf; - for (c=CMSG_FIRSTHDR(&h); c; c=CMSG_NXTHDR(&h,c)) - c->__pad1 = 0; - } - } -#endif - return ax_sendmsg(fd, msg, flags); -} - -// TODO -ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags) -{ - unimplemented(); - return 0; -} - -// TODO -int socketpair(int domain, int type, int protocol, int sv[2]) -{ - unimplemented(); - return 0; -} - -#endif // RUX_CONFIG_NET diff --git a/ulib/ruxlibc/c/stat.c b/ulib/ruxlibc/c/stat.c deleted file mode 100644 index 9b93f816b..000000000 --- a/ulib/ruxlibc/c/stat.c +++ /dev/null @@ -1,40 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include -#include - -// TODO: -int fchmod(int fd, mode_t mode) -{ - unimplemented(); - return 0; -} - -// TODO -int chmod(const char *path, mode_t mode) -{ - unimplemented(); - return 0; -} - -// TODO -mode_t umask(mode_t mask) -{ - unimplemented("mask: %d", mask); - return 0; -} - -// TODO -int fstatat(int fd, const char *restrict path, struct stat *restrict st, int flag) -{ - unimplemented(); - return 0; -} diff --git a/ulib/ruxlibc/c/statfs.c b/ulib/ruxlibc/c/statfs.c deleted file mode 100644 index 84d773818..000000000 --- a/ulib/ruxlibc/c/statfs.c +++ /dev/null @@ -1,18 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include - -// TODO -int statfs(const char *path, struct statfs *buf) -{ - unimplemented(); - return 0; -} diff --git a/ulib/ruxlibc/c/stdio.c b/ulib/ruxlibc/c/stdio.c deleted file mode 100644 index 5122b9b85..000000000 --- a/ulib/ruxlibc/c/stdio.c +++ /dev/null @@ -1,469 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include "printf.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -// LOCK used by `puts()` -#ifdef RUX_CONFIG_MULTITASK -#include -static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; -#endif - -#define MAX(a, b) ((a) > (b) ? (a) : (b)) -#define MIN(a, b) ((a) < (b) ? (a) : (b)) - -FILE __stdin_FILE = {.fd = 0, .buffer_len = 0}; - -FILE __stdout_FILE = {.fd = 1, .buffer_len = 0}; - -FILE __stderr_FILE = {.fd = 2, .buffer_len = 0}; - -FILE *const stdin = &__stdin_FILE; -FILE *const stdout = &__stdout_FILE; -FILE *const stderr = &__stderr_FILE; - -// Returns: number of chars written, negative for failure -// Warn: buffer_len[f] will not be changed -static int __write_buffer(FILE *f) -{ - int r = 0; - if (f->buffer_len == 0) - return 0; - r = write(f->fd, f->buf, f->buffer_len); - return r; -} - -// Clear buffer_len[f] -static void __clear_buffer(FILE *f) -{ - f->buffer_len = 0; -} - -static int __fflush(FILE *f) -{ - int r = __write_buffer(f); - __clear_buffer(f); - return r >= 0 ? 0 : r; -} - -static int out(FILE *f, const char *s, size_t l) -{ - int ret = 0; - for (size_t i = 0; i < l; i++) { - char c = s[i]; - f->buf[f->buffer_len++] = c; - if (f->buffer_len == FILE_BUF_SIZE || c == '\n') { - int r = __write_buffer(f); - __clear_buffer(f); - if (r < 0) - return r; - if (r < f->buffer_len) - return ret + r; - ret += r; - } - } - return ret; -} - -int getchar(void) -{ - unimplemented(); - return 0; -} - -int fflush(FILE *f) -{ - return __fflush(f); -} - -static inline int do_putc(int c, FILE *f) -{ - char byte = c; - return out(f, &byte, 1); -} - -int fputc(int c, FILE *f) -{ - return do_putc(c, f); -} - -int putc(int c, FILE *f) -{ - return do_putc(c, f); -} - -int putchar(int c) -{ - return do_putc(c, stdout); -} - -int puts(const char *s) -{ -#ifdef RUX_CONFIG_MULTITASK - pthread_mutex_lock(&lock); -#endif - - int r = write(1, (const void *)s, strlen(s)); - char brk[1] = {'\n'}; - write(1, (const void *)brk, 1); - -#ifdef RUX_CONFIG_MULTITASK - pthread_mutex_unlock(&lock); -#endif - - return r; -} - -void perror(const char *msg) -{ - FILE *f = stderr; - char *errstr = strerror(errno); - - if (msg && *msg) { - out(f, msg, strlen(msg)); - out(f, ": ", 2); - } - out(f, errstr, strlen(errstr)); - out(f, "\n", 1); -} - -static void __out_wrapper(char c, void *arg) -{ - out(arg, &c, 1); -} - -int printf(const char *restrict fmt, ...) -{ - int ret; - va_list ap; - va_start(ap, fmt); - ret = vfprintf(stdout, fmt, ap); - va_end(ap); - return ret; -} - -int fprintf(FILE *restrict f, const char *restrict fmt, ...) -{ - int ret; - va_list ap; - va_start(ap, fmt); - ret = vfprintf(f, fmt, ap); - va_end(ap); - return ret; -} - -int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap) -{ - return vfctprintf(__out_wrapper, f, fmt, ap); -} - -// TODO -int sscanf(const char *restrict __s, const char *restrict __format, ...) -{ - unimplemented(); - return 0; -} - -#ifdef RUX_CONFIG_FS - -int __fmodeflags(const char *mode) -{ - int flags; - if (strchr(mode, '+')) - flags = O_RDWR; - else if (*mode == 'r') - flags = O_RDONLY; - else - flags = O_WRONLY; - if (strchr(mode, 'x')) - flags |= O_EXCL; - if (strchr(mode, 'e')) - flags |= O_CLOEXEC; - if (*mode != 'r') - flags |= O_CREAT; - if (*mode == 'w') - flags |= O_TRUNC; - if (*mode == 'a') - flags |= O_APPEND; - return flags; -} - -FILE *fopen(const char *filename, const char *mode) -{ - FILE *f; - int flags; - - if (!strchr("rwa", *mode)) { - errno = EINVAL; - return 0; - } - - f = (FILE *)malloc(sizeof(FILE)); - - flags = __fmodeflags(mode); - // TODO: currently mode is unused in ax_open - int fd = open(filename, flags, 0666); - if (fd < 0) - return NULL; - f->fd = fd; - - return f; -} - -char *fgets(char *restrict s, int n, FILE *restrict f) -{ - if (n == 0) - return NULL; - if (n == 1) { - *s = '\0'; - return s; - } - - int cnt = 0; - while (cnt < n - 1) { - char c; - if (read(f->fd, (void *)&c, 1) > 0) { - if (c != '\n') - s[cnt++] = c; - else{ - s[cnt++] = c; - break; - } - - } else - break; - } - if(cnt==0){ - return NULL; - } - s[cnt] = '\0'; - return s; -} - -size_t fread(void *restrict destv, size_t size, size_t nmemb, FILE *restrict f) -{ - size_t total = size * nmemb; - size_t read_len = 0; - size_t len = 0; - do { - len = read(f->fd, destv + read_len, total - read_len); - if (len < 0) - break; - read_len += len; - } while (len > 0); - return read_len == size * nmemb ? nmemb : read_len / size; -} - -size_t fwrite(const void *restrict src, size_t size, size_t nmemb, FILE *restrict f) -{ - size_t total = size * nmemb; - size_t write_len = 0; - size_t len = 0; - do { - len = write(f->fd, src + write_len, total - write_len); - if (len < 0) - break; - write_len += len; - } while (len > 0); - return write_len == size * nmemb ? nmemb : write_len / size; -} - -int fputs(const char *restrict s, FILE *restrict f) -{ - size_t l = strlen(s); - return (fwrite(s, 1, l, f) == l) - 1; -} - -int fclose(FILE *f) -{ - return close(f->fd); -} - -int fileno(FILE *f) -{ - return f->fd; -} - -int feof(FILE *f) -{ - unimplemented(); - return 0; -} - -// TODO -int fseek(FILE *__stream, long __off, int __whence) -{ - unimplemented(); - return 0; -} - -// TODO -off_t ftello(FILE *__stream) -{ - unimplemented(); - return 0; -} - -// TODO -char *tmpnam(char *buf) -{ - unimplemented(); - return 0; -} - -// TODO -void clearerr(FILE *f) -{ - unimplemented(); -} - -// TODO -int ferror(FILE *f) -{ - unimplemented(); - return 0; -} - - -FILE *freopen(const char *restrict filename, const char *restrict mode, FILE *restrict f) -{ - int fl = __fmodeflags(mode); - FILE *f2; - - fflush(f); - - if (!filename) { - if (fl&O_CLOEXEC) - fcntl(f->fd, F_SETFD, FD_CLOEXEC); - fl &= ~(O_CREAT|O_EXCL|O_CLOEXEC); - if(fcntl(f->fd, F_SETFL, fl) < 0) - goto fail; - } else { - f2 = fopen(filename, mode); - if (!f2) goto fail; - if (f2->fd == f->fd) f2->fd = -1; /* avoid closing in fclose */ - else if (dup3(f2->fd, f->fd, fl&O_CLOEXEC)<0) goto fail2; - fclose(f2); - } - return f; - -fail2: - fclose(f2); -fail: - fclose(f); - return NULL; -} - -// TODO -int fscanf(FILE *restrict f, const char *restrict fmt, ...) -{ - unimplemented(); - return 0; -} - -// TODO -long ftell(FILE *f) -{ - unimplemented(); - return 0; -} - -int getc(FILE *f) -{ - unimplemented(); - return 0; -} - -int remove(const char *path) -{ - if(unlink(path) < 0) { - return rmdir(path); - } - return 0; -} - -// TODO -int setvbuf(FILE *restrict f, char *restrict buf, int type, size_t size) -{ - unimplemented(); - return 0; -} - -// TODO -FILE *tmpfile(void) -{ - unimplemented(); - return NULL; -} - -int ungetc(int c, FILE *f) -{ - unimplemented(); - return 0; -} - -ssize_t getdelim(char **restrict s, size_t *restrict n, int delim, FILE *restrict f) -{ - unimplemented(); - return 0; -} - -ssize_t getline(char **restrict s, size_t *restrict n, FILE *restrict f) -{ - return getdelim(s, n, '\n', f); -} - -int __uflow(FILE *f) -{ - unimplemented(); - return 0; -} - -int getc_unlocked(FILE *f) -{ - unimplemented(); - return 0; -} - -FILE *fdopen(int fd, const char *mode) -{ - FILE *f; - if (!strchr("rwa", *mode)) { - errno = EINVAL; - return 0; - } - - if (!(f=malloc(sizeof *f))) return 0; - f->buffer_len = 0; - - /* Apply close-on-exec flag */ - if (strchr(mode, 'e')) fcntl(fd, F_SETFD, FD_CLOEXEC); - - /* Set append mode on fd if opened for append */ - if (*mode == 'a') { - int flags = fcntl(fd, F_GETFL); - if (!(flags & O_APPEND)) - fcntl(fd, F_SETFL, flags | O_APPEND); - } - f->fd = fd; - return f; -} - -#endif // RUX_CONFIG_FS diff --git a/ulib/ruxlibc/c/stdlib.c b/ulib/ruxlibc/c/stdlib.c deleted file mode 100644 index ea14d5772..000000000 --- a/ulib/ruxlibc/c/stdlib.c +++ /dev/null @@ -1,691 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A - * PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -char *program_invocation_short_name = "dummy"; -char *program_invocation_name = "dummy"; - -#define __DECONST(type, var) ((type)(uintptr_t)(const void *)(var)) - -void srandom(unsigned int s) -{ - srand(s); -} - -#ifdef RUX_CONFIG_ALLOC - -void *calloc(size_t m, size_t n) -{ - void *mem = malloc(m * n); - - return memset(mem, 0, n * m); -} - -void *realloc(void *memblock, size_t size) -{ - if (!memblock) - return malloc(size); - - size_t o_size = *(size_t *)(memblock - 8); - - void *mem = malloc(size); - - for (int i = 0; i < (o_size < size ? o_size : size); i++) - ((char *)mem)[i] = ((char *)memblock)[i]; - - free(memblock); - return mem; -} - -#endif // RUX_CONFIG_ALLOC - -long long llabs(long long a) -{ - return a > 0 ? a : -a; -} - -int abs(int a) -{ - return a > 0 ? a : -a; -} - -long long atoll(const char *s) -{ - long long n = 0; - int neg = 0; - while (isspace(*s)) s++; - switch (*s) { - case '-': - neg = 1; - case '+': - s++; - } - /* Compute n as a negative number to avoid overflow on LLONG_MIN */ - while (isdigit(*s)) n = 10 * n - (*s++ - '0'); - return neg ? n : -n; -} - -long strtol(const char *restrict nptr, char **restrict endptr, int base) -{ - const char *s; - unsigned long acc; - unsigned char c; - unsigned long qbase, cutoff; - int neg, any, cutlim; - - s = nptr; - if (base < 0 || base == 1 || base > 36) { - errno = EINVAL; - any = 0; - acc = 0; - goto exit; - } - - do { - c = *s++; - } while (isspace(c)); - if (c == '-') { - neg = 1; - c = *s++; - } else { - neg = 0; - if (c == '+') - c = *s++; - } - if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) { - c = s[1]; - s += 2; - base = 16; - } - if (base == 0) - base = c == '0' ? 8 : 10; - - qbase = (unsigned int)base; - cutoff = neg ? (unsigned long)LONG_MAX - (unsigned long)(LONG_MIN + LONG_MAX) : LONG_MAX; - cutlim = cutoff % qbase; - cutoff /= qbase; - for (acc = 0, any = 0;; c = *s++) { - if (!isascii(c)) - break; - if (isdigit(c)) - c -= '0'; - else if (isalpha(c)) - c -= isupper(c) ? 'A' - 10 : 'a' - 10; - else - break; - if (c >= base) - break; - if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) - any = -1; - else { - any = 1; - acc *= qbase; - acc += c; - } - } - - if (any < 0) { - acc = neg ? LONG_MIN : LONG_MAX; - errno = ERANGE; - } else if (neg) - acc = -acc; - -exit: - if (endptr != 0) - *endptr = __DECONST(char *, any ? s - 1 : nptr); - return acc; -} - -unsigned long strtoul(const char *nptr, char **endptr, int base) -{ - const char *s = nptr; - unsigned long acc; - unsigned char c; - unsigned long cutoff; - int neg = 0, any, cutlim; - - if (base < 0 || base == 1 || base > 36) { - errno = EINVAL; - any = 0; - acc = 0; - goto exit; - } - - do { - c = *s++; - } while (isspace(c)); - if (c == '-') { - neg = 1; - c = *s++; - } else if (c == '+') - c = *s++; - if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) { - c = s[1]; - s += 2; - base = 16; - } - if (base == 0) - base = c == '0' ? 8 : 10; - cutoff = (unsigned long)ULONG_MAX / (unsigned long)base; - cutlim = (unsigned long)ULONG_MAX % (unsigned long)base; - - for (acc = 0, any = 0;; c = *s++) { - if (!isascii(c)) - break; - if (isdigit(c)) - c -= '0'; - else if (isalpha(c)) - c -= isupper(c) ? 'A' - 10 : 'a' - 10; - else - break; - if (c >= base) - break; - if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) - any = -1; - else { - any = 1; - acc *= base; - acc += c; - } - } - if (any < 0) { - acc = ULONG_MAX; - errno = ERANGE; - } else if (neg) - acc = -acc; -exit: - if (endptr != 0) - *endptr = __DECONST(char *, any ? s - 1 : nptr); - return acc; -} - -long long strtoll(const char *nptr, char **endptr, int base) -{ - const char *s; - unsigned long long acc; - unsigned char c; - unsigned long long qbase, cutoff; - int neg, any, cutlim; - - s = nptr; - if (base < 0 || base == 1 || base > 36) { - errno = EINVAL; - any = 0; - acc = 0; - goto exit; - } - - do { - c = *s++; - } while (isspace(c)); - if (c == '-') { - neg = 1; - c = *s++; - } else { - neg = 0; - if (c == '+') - c = *s++; - } - if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) { - c = s[1]; - s += 2; - base = 16; - } - if (base == 0) - base = c == '0' ? 8 : 10; - - qbase = (unsigned int)base; - cutoff = neg ? (unsigned long long)LLONG_MAX - (unsigned long long)(LLONG_MIN + LLONG_MAX) - : LLONG_MAX; - cutlim = cutoff % qbase; - cutoff /= qbase; - for (acc = 0, any = 0;; c = *s++) { - if (!isascii(c)) - break; - if (isdigit(c)) - c -= '0'; - else if (isalpha(c)) - c -= isupper(c) ? 'A' - 10 : 'a' - 10; - else - break; - if (c >= base) - break; - if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) - any = -1; - else { - any = 1; - acc *= qbase; - acc += c; - } - } - - if (any < 0) { - errno = ERANGE; - acc = neg ? LLONG_MIN : LLONG_MAX; - } else if (neg) - acc = -acc; - -exit: - if (endptr != 0) - *endptr = __DECONST(char *, any ? s - 1 : nptr); - return acc; -} - -unsigned long long strtoull(const char *nptr, char **endptr, int base) -{ - const char *s = nptr; - unsigned long long acc; - unsigned char c; - unsigned long long qbase, cutoff; - int neg, any, cutlim; - - if (base < 0 || base == 1 || base > 36) { - errno = EINVAL; - any = 0; - acc = 0; - goto exit; - } - - do { - c = *s++; - } while (isspace(c)); - if (c == '-') { - neg = 1; - c = *s++; - } else { - neg = 0; - if (c == '+') - c = *s++; - } - if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) { - c = s[1]; - s += 2; - base = 16; - } - if (base == 0) - base = c == '0' ? 8 : 10; - - qbase = (unsigned int)base; - cutoff = (unsigned long long)ULLONG_MAX / qbase; - cutlim = (unsigned long long)ULLONG_MAX % qbase; - for (acc = 0, any = 0;; c = *s++) { - if (!isascii(c)) - break; - if (isdigit(c)) - c -= '0'; - else if (isalpha(c)) - c -= isupper(c) ? 'A' - 10 : 'a' - 10; - else - break; - if (c >= base) - break; - if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) - any = -1; - else { - any = 1; - acc *= qbase; - acc += c; - } - } - if (any < 0) { - errno = ERANGE; - acc = ULLONG_MAX; - } else if (neg) - acc = -acc; - -exit: - if (endptr != 0) - *endptr = __DECONST(char *, any ? s - 1 : nptr); - return acc; -} - -#ifdef RUX_CONFIG_FP_SIMD - -// TODO: precision may not be enough -long double strtold(const char *restrict s, char **restrict p) -{ - return (long double)strtod(s, p); -} - -#endif // RUX_CONFIG_FP_SIMD - -typedef int (*cmpfun_)(const void *, const void *); - -static int wrapper_cmp(const void *v1, const void *v2, void *cmp) -{ - return ((cmpfun_)cmp)(v1, v2); -} - -typedef int (*cmpfun)(const void *, const void *, void *); - -static inline int a_ctz_32(uint32_t x) -{ - static const char debruijn32[32] = {0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, - 11, 20, 8, 4, 13, 31, 22, 28, 18, 26, 10, - 7, 12, 21, 17, 9, 6, 16, 5, 15, 14}; - return debruijn32[(x & -x) * 0x076be629 >> 27]; -} - -static inline int a_ctz_64(uint64_t x) -{ - static const char debruijn64[64] = { - 0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28, 62, 5, 39, 46, 44, 42, - 22, 9, 24, 35, 59, 56, 49, 18, 29, 11, 63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, - 23, 58, 17, 10, 51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12}; - if (sizeof(long) < 8) { - uint32_t y = x; - if (!y) { - y = x >> 32; - return 32 + a_ctz_32(y); - } - return a_ctz_32(y); - } - return debruijn64[(x & -x) * 0x022fdd63cc95386dull >> 58]; -} - -static inline int a_ctz_l(unsigned long x) -{ - return (sizeof(long) < 8) ? a_ctz_32(x) : a_ctz_64(x); -} - -#define ntz(x) a_ctz_l((x)) - -static inline int pntz(size_t p[2]) -{ - int r = ntz(p[0] - 1); - if (r != 0 || (r = 8 * sizeof(size_t) + ntz(p[1])) != 8 * sizeof(size_t)) { - return r; - } - return 0; -} - -static void cycle(size_t width, unsigned char *ar[], int n) -{ - unsigned char tmp[256]; - size_t l; - int i; - - if (n < 2) { - return; - } - - ar[n] = tmp; - while (width) { - l = sizeof(tmp) < width ? sizeof(tmp) : width; - memcpy(ar[n], ar[0], l); - for (i = 0; i < n; i++) { - memcpy(ar[i], ar[i + 1], l); - ar[i] += l; - } - width -= l; - } -} - -/* shl() and shr() need n > 0 */ -static inline void shl(size_t p[2], int n) -{ - if (n >= 8 * sizeof(size_t)) { - n -= 8 * sizeof(size_t); - p[1] = p[0]; - p[0] = 0; - } - p[1] <<= n; - p[1] |= p[0] >> (sizeof(size_t) * 8 - n); - p[0] <<= n; -} - -static inline void shr(size_t p[2], int n) -{ - if (n >= 8 * sizeof(size_t)) { - n -= 8 * sizeof(size_t); - p[0] = p[1]; - p[1] = 0; - } - p[0] >>= n; - p[0] |= p[1] << (sizeof(size_t) * 8 - n); - p[1] >>= n; -} - -static void sift(unsigned char *head, size_t width, cmpfun cmp, void *arg, int pshift, size_t lp[]) -{ - unsigned char *rt, *lf; - unsigned char *ar[14 * sizeof(size_t) + 1]; - int i = 1; - - ar[0] = head; - while (pshift > 1) { - rt = head - width; - lf = head - width - lp[pshift - 2]; - - if (cmp(ar[0], lf, arg) >= 0 && cmp(ar[0], rt, arg) >= 0) { - break; - } - if (cmp(lf, rt, arg) >= 0) { - ar[i++] = lf; - head = lf; - pshift -= 1; - } else { - ar[i++] = rt; - head = rt; - pshift -= 2; - } - } - cycle(width, ar, i); -} - -static void trinkle(unsigned char *head, size_t width, cmpfun cmp, void *arg, size_t pp[2], - int pshift, int trusty, size_t lp[]) -{ - unsigned char *stepson, *rt, *lf; - size_t p[2]; - unsigned char *ar[14 * sizeof(size_t) + 1]; - int i = 1; - int trail; - - p[0] = pp[0]; - p[1] = pp[1]; - - ar[0] = head; - while (p[0] != 1 || p[1] != 0) { - stepson = head - lp[pshift]; - if (cmp(stepson, ar[0], arg) <= 0) { - break; - } - if (!trusty && pshift > 1) { - rt = head - width; - lf = head - width - lp[pshift - 2]; - if (cmp(rt, stepson, arg) >= 0 || cmp(lf, stepson, arg) >= 0) { - break; - } - } - - ar[i++] = stepson; - head = stepson; - trail = pntz(p); - shr(p, trail); - pshift += trail; - trusty = 0; - } - if (!trusty) { - cycle(width, ar, i); - sift(head, width, cmp, arg, pshift, lp); - } -} - -void __qsort_r(void *base, size_t nel, size_t width, cmpfun cmp, void *arg) -{ - size_t lp[12 * sizeof(size_t)]; - size_t i, size = width * nel; - unsigned char *head, *high; - size_t p[2] = {1, 0}; - int pshift = 1; - int trail; - - if (!size) - return; - - head = base; - high = head + size - width; - - /* Precompute Leonardo numbers, scaled by element width */ - for (lp[0] = lp[1] = width, i = 2; (lp[i] = lp[i - 2] + lp[i - 1] + width) < size; i++) - ; - - while (head < high) { - if ((p[0] & 3) == 3) { - sift(head, width, cmp, arg, pshift, lp); - shr(p, 2); - pshift += 2; - } else { - if (lp[pshift - 1] >= high - head) { - trinkle(head, width, cmp, arg, p, pshift, 0, lp); - } else { - sift(head, width, cmp, arg, pshift, lp); - } - - if (pshift == 1) { - shl(p, 1); - pshift = 0; - } else { - shl(p, pshift - 1); - pshift = 1; - } - } - - p[0] |= 1; - head += width; - } - - trinkle(head, width, cmp, arg, p, pshift, 0, lp); - - while (pshift != 1 || p[0] != 1 || p[1] != 0) { - if (pshift <= 1) { - trail = pntz(p); - shr(p, trail); - pshift += trail; - } else { - shl(p, 2); - pshift -= 2; - p[0] ^= 7; - shr(p, 1); - trinkle(head - lp[pshift] - width, width, cmp, arg, p, pshift + 1, 1, lp); - shl(p, 1); - p[0] |= 1; - trinkle(head - width, width, cmp, arg, p, pshift, 1, lp); - } - head -= width; - } -} - -void qsort(void *base, size_t nel, size_t width, cmpfun_ cmp) -{ - __qsort_r(base, nel, width, wrapper_cmp, (void *)cmp); -} - -// TODO -int mkstemp(char *__template) -{ - unimplemented(); - return 0; -} - -// TODO -int mkostemp(char *__template, int __flags) -{ - unimplemented(); - return 0; -} - -// TODO -int system(const char *cmd) -{ - unimplemented(); - return 0; -} - -// TODO -char *realpath(const char *restrict path, char *restrict resolved_path) -{ - unimplemented(); - return 0; -} - -struct chunk { - size_t psize, csize; - struct chunk *next, *prev; -}; - -void __bin_chunk(struct chunk *) -{ - unimplemented(); - return; -} - -void *aligned_alloc(size_t align, size_t len) -{ - unsigned char *mem, *new; - - if ((align & -align) != align) { - errno = EINVAL; - return 0; - } - - if (len > SIZE_MAX - align) { - errno = ENOMEM; - return 0; - } - - if (align <= SIZE_ALIGN) - return malloc(len); - - if (!(mem = malloc(len + align - 1))) - return 0; - - new = (void *)((uintptr_t)mem + align - 1 & -align); - if (new == mem) - return mem; - - struct chunk *c = MEM_TO_CHUNK(mem); - struct chunk *n = MEM_TO_CHUNK(new); - - if (IS_MMAPPED(c)) { - n->psize = c->psize + (new - mem); - n->csize = c->csize - (new - mem); - return new; - } - - struct chunk *t = NEXT_CHUNK(c); - - n->psize = c->csize = C_INUSE | (new - mem); - n->csize = t->psize -= new - mem; - - __bin_chunk(c); - return new; -} - -// TODO -int posix_memalign(void **res, size_t align, size_t len) -{ - if (align < sizeof(void *)) - return EINVAL; - void *mem = aligned_alloc(align, len); - if (!mem) - return errno; - *res = mem; - return 0; -} diff --git a/ulib/ruxlibc/c/string.c b/ulib/ruxlibc/c/string.c deleted file mode 100644 index d712cc90c..000000000 --- a/ulib/ruxlibc/c/string.c +++ /dev/null @@ -1,473 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include -#include -#include -#include -#include - -int atoi(const char *s) -{ - int n = 0, neg = 0; - while (isspace(*s)) s++; - switch (*s) { - case '-': - neg = 1; - case '+': - s++; - } - /* Compute n as a negative number to avoid overflow on INT_MIN */ - while (isdigit(*s)) n = 10 * n - (*s++ - '0'); - return neg ? n : -n; -} - -void *memchr(const void *src, int c, size_t n) -{ - const unsigned char *s = src; - c = (unsigned char)c; - for (; n && *s != c; s++, n--) - ; - return n ? (void *)s : 0; -} - -void *memset(void *dest, int c, size_t n) -{ - unsigned char *s = dest; - size_t k; - - /* Fill head and tail with minimal branching. Each - * conditional ensures that all the subsequently used - * offsets are well-defined and in the dest region. */ - - if (!n) - return dest; - s[0] = c; - s[n - 1] = c; - if (n <= 2) - return dest; - s[1] = c; - s[2] = c; - s[n - 2] = c; - s[n - 3] = c; - if (n <= 6) - return dest; - s[3] = c; - s[n - 4] = c; - if (n <= 8) - return dest; - - /* Advance pointer to align it at a 4-byte boundary, - * and truncate n to a multiple of 4. The previous code - * already took care of any head/tail that get cut off - * by the alignment. */ - - k = -(uintptr_t)s & 3; - s += k; - n -= k; - n &= -4; - - /* Pure C fallback with no aliasing violations. */ - for (; n; n--, s++) *s = c; - - return dest; -} - -char *strcpy(char *restrict d, const char *restrict s) -{ - for (; (*d = *s); s++, d++) - ; - return d; -} - -char *strncpy(char *restrict d, const char *restrict s, size_t n) -{ - for (; n && (*d = *s); n--, s++, d++) - ; - return d; -} - -char *strcat(char *restrict d, const char *restrict s) -{ - strcpy(d + strlen(d), s); - return d; -} - -char *strncat(char *restrict d, const char *restrict s, size_t n) -{ - char *a = d; - d += strlen(d); - while (n && *s) n--, *d++ = *s++; - *d++ = 0; - return a; -} - -int strcmp(const char *l, const char *r) -{ - for (; *l == *r && *l; l++, r++) - ; - return *(unsigned char *)l - *(unsigned char *)r; -} - -int strncmp(const char *_l, const char *_r, size_t n) -{ - const unsigned char *l = (void *)_l, *r = (void *)_r; - if (!n--) - return 0; - for (; *l && *r && n && *l == *r; l++, r++, n--) - ; - return *l - *r; -} - -int strcoll(const char *l, const char *r) -{ - return strcmp(l, r); -} - -#define BITOP(a, b, op) \ - ((a)[(size_t)(b) / (8 * sizeof *(a))] op(size_t) 1 << ((size_t)(b) % (8 * sizeof *(a)))) -#define MAX(a, b) ((a) > (b) ? (a) : (b)) -#define MIN(a, b) ((a) < (b) ? (a) : (b)) - -size_t strcspn(const char *s1, const char *s2) -{ - const char *a = s1; - size_t byteset[32 / sizeof(size_t)]; - - if (!s2[0] || !s2[1]) { - for (; *s1 != *s2; s1++) return s1 - a; - } - memset(byteset, 0, sizeof byteset); - - for (; *s2 != '\0'; s2++) BITOP(byteset, *(unsigned char *)s2, |=); - for (; *s1 && !(BITOP(byteset, *(unsigned char *)s1, &)); s1++) - ; - - return s1 - a; -} - -size_t strspn(const char *s, const char *c) -{ - const char *a = s; - size_t byteset[32 / sizeof(size_t)] = {0}; - - if (!c[0]) - return 0; - if (!c[1]) { - for (; *s == *c; s++) - ; - return s - a; - } - - for (; *c && BITOP(byteset, *(unsigned char *)c, |=); c++) - ; - for (; *s && BITOP(byteset, *(unsigned char *)s, &); s++) - ; - return s - a; -} - -char *strpbrk(const char *s, const char *b) -{ - s += strcspn(s, b); - return *s ? (char *)s : 0; -} - -char *strchrnul(const char *s, int c) -{ - c = (unsigned char)c; - if (!c) - return (char *)s + strlen(s); - - for (; *s && *(unsigned char *)s != c; s++) - ; - return (char *)s; -} - -char *strchr(const char *s, int c) -{ - while (*s != c && *s != '\0') s++; - - if (*s == c) { - return (char *)s; - } else { - return NULL; - } -} - -char *strrchr(const char *s, int c) -{ - char *isCharFind = NULL; - if (s != NULL) { - do { - if (*s == (char)c) { - isCharFind = (char *)s; - } - } while (*s++); - } - return isCharFind; -} - -int strerror_r(int err, char *buf, size_t buflen) -{ - char *msg = strerror(err); - size_t l = strlen(msg); - if (l >= buflen) { - if (buflen) { - memcpy(buf, msg, buflen - 1); - buf[buflen - 1] = 0; - } - return ERANGE; - } - memcpy(buf, msg, l + 1); - return 0; -} - -void *memcpy(void *restrict dest, const void *restrict src, size_t n) -{ - unsigned char *d = dest; - const unsigned char *s = src; - for (; n; n--) *d++ = *s++; - return dest; -} - -void *memmove(void *dest, const void *src, size_t n) -{ - char *d = dest; - const char *s = src; - - if (d == s) - return d; - if ((uintptr_t)s - (uintptr_t)d - n <= -2 * n) - return memcpy(d, s, n); - - if (d < s) { - for (; n; n--) *d++ = *s++; - } else { - while (n) n--, d[n] = s[n]; - } - - return dest; -} - -int memcmp(const void *vl, const void *vr, size_t n) -{ - const unsigned char *l = vl, *r = vr; - for (; n && *l == *r; n--, l++, r++) - ; - return n ? *l - *r : 0; -} - -int strcasecmp(const char *_l, const char *_r) -{ - const unsigned char *l = (void *)_l, *r = (void *)_r; - for (; *l && *r && (*l == *r || tolower(*l) == tolower(*r)); l++, r++) - ; - return tolower(*l) - tolower(*r); -} - -int strncasecmp(const char *_l, const char *_r, size_t n) -{ - const unsigned char *l = (void *)_l, *r = (void *)_r; - if (!n--) - return 0; - for (; *l && *r && n && (*l == *r || tolower(*l) == tolower(*r)); l++, r++, n--) - ; - return tolower(*l) - tolower(*r); -} - -// `strstr` helper function -static char *twobyte_strstr(const unsigned char *h, const unsigned char *n) -{ - uint16_t nw = n[0] << 8 | n[1], hw = h[0] << 8 | h[1]; - for (h++; *h && hw != nw; hw = hw << 8 | *++h) - ; - return *h ? (char *)h - 1 : 0; -} - -// `strstr` helper function -static char *threebyte_strstr(const unsigned char *h, const unsigned char *n) -{ - uint32_t nw = (uint32_t)n[0] << 24 | n[1] << 16 | n[2] << 8; - uint32_t hw = (uint32_t)h[0] << 24 | h[1] << 16 | h[2] << 8; - for (h += 2; *h && hw != nw; hw = (hw | *++h) << 8) - ; - return *h ? (char *)h - 2 : 0; -} - -// `strstr` helper function -static char *fourbyte_strstr(const unsigned char *h, const unsigned char *n) -{ - uint32_t nw = (uint32_t)n[0] << 24 | n[1] << 16 | n[2] << 8 | n[3]; - uint32_t hw = (uint32_t)h[0] << 24 | h[1] << 16 | h[2] << 8 | h[3]; - for (h += 3; *h && hw != nw; hw = hw << 8 | *++h) - ; - return *h ? (char *)h - 3 : 0; -} - -// `strstr` helper function -static char *twoway_strstr(const unsigned char *h, const unsigned char *n) -{ - const unsigned char *z; - size_t l, ip, jp, k, p, ms, p0, mem, mem0; - size_t byteset[32 / sizeof(size_t)] = {0}; - size_t shift[256]; - - /* Computing length of needle and fill shift table */ - for (l = 0; n[l] && h[l]; l++) BITOP(byteset, n[l], |=), shift[n[l]] = l + 1; - if (n[l]) - return 0; /* hit the end of h */ - - /* Compute maximal suffix */ - ip = -1; - jp = 0; - k = p = 1; - while (jp + k < l) { - if (n[ip + k] == n[jp + k]) { - if (k == p) { - jp += p; - k = 1; - } else - k++; - } else if (n[ip + k] > n[jp + k]) { - jp += k; - k = 1; - p = jp - ip; - } else { - ip = jp++; - k = p = 1; - } - } - ms = ip; - p0 = p; - - /* And with the opposite comparison */ - ip = -1; - jp = 0; - k = p = 1; - while (jp + k < l) { - if (n[ip + k] == n[jp + k]) { - if (k == p) { - jp += p; - k = 1; - } else - k++; - } else if (n[ip + k] < n[jp + k]) { - jp += k; - k = 1; - p = jp - ip; - } else { - ip = jp++; - k = p = 1; - } - } - if (ip + 1 > ms + 1) - ms = ip; - else - p = p0; - - /* Periodic needle? */ - if (memcmp(n, n + p, ms + 1)) { - mem0 = 0; - p = MAX(ms, l - ms - 1) + 1; - } else - mem0 = l - p; - mem = 0; - - /* Initialize incremental end-of-haystack pointer */ - z = h; - - /* Search loop */ - for (;;) { - /* Update incremental end-of-haystack pointer */ - if (z - h < l) { - /* Fast estimate for MAX(l,63) */ - size_t grow = l | 63; - const unsigned char *z2 = memchr(z, 0, grow); - if (z2) { - z = z2; - if (z - h < l) - return 0; - } else - z += grow; - } - - /* Check last byte first; advance by shift on mismatch */ - if (BITOP(byteset, h[l - 1], &)) { - k = l - shift[h[l - 1]]; - if (k) { - if (k < mem) - k = mem; - h += k; - mem = 0; - continue; - } - } else { - h += l; - mem = 0; - continue; - } - - /* Compare right half */ - for (k = MAX(ms + 1, mem); n[k] && n[k] == h[k]; k++) - ; - if (n[k]) { - h += k - ms; - mem = 0; - continue; - } - /* Compare left half */ - for (k = ms + 1; k > mem && n[k - 1] == h[k - 1]; k--) - ; - if (k <= mem) - return (char *)h; - h += p; - mem = mem0; - } -} - -char *strstr(const char *h, const char *n) -{ - /* Return immediately on empty needle */ - if (!n[0]) - return (char *)h; - - /* Use faster algorithms for short needles */ - h = strchr(h, *n); - if (!h || !n[1]) - return (char *)h; - if (!h[1]) - return 0; - if (!n[2]) - return twobyte_strstr((void *)h, (void *)n); - if (!h[2]) - return 0; - if (!n[3]) - return threebyte_strstr((void *)h, (void *)n); - if (!h[3]) - return 0; - if (!n[4]) - return fourbyte_strstr((void *)h, (void *)n); - - return twoway_strstr((void *)h, (void *)n); -} - -#ifdef RUX_CONFIG_ALLOC - -#include -char *strdup(const char *s) -{ - size_t l = strlen(s); - char *d = malloc(l + 1); - if (!d) - return NULL; - return memcpy(d, s, l + 1); -} - -#endif // RUX_CONFIG_ALLOC diff --git a/ulib/ruxlibc/c/syslog.c b/ulib/ruxlibc/c/syslog.c deleted file mode 100644 index d47afadeb..000000000 --- a/ulib/ruxlibc/c/syslog.c +++ /dev/null @@ -1,25 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include - -// TODO -void syslog(int __pri, const char *__fmt, ...) -{ - unimplemented(); - return; -} - -// TODO -void openlog(const char *__ident, int __option, int __facility) -{ - unimplemented(); - return; -} diff --git a/ulib/ruxlibc/c/time.c b/ulib/ruxlibc/c/time.c deleted file mode 100644 index b3e05a13f..000000000 --- a/ulib/ruxlibc/c/time.c +++ /dev/null @@ -1,216 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A - * PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. - */ - -#include -#include -#include -#include -#include -#include - -long timezone = 0; -const char __utc[] = "UTC"; - -const int SEC_PER_MIN = 60; -const int SEC_PER_HOUR = 3600; -const int MIN_PER_HOUR = 60; -const int HOUR_PER_DAY = 24; - -/* 2000-03-01 (mod 400 year, immediately after feb29 */ -#define LEAPOCH (946684800LL + 86400 * (31 + 29)) -#define DAYS_PER_400Y (365 * 400 + 97) -#define DAYS_PER_100Y (365 * 100 + 24) -#define DAYS_PER_4Y (365 * 4 + 1) - -int __secs_to_tm(long long t, struct tm *tm) -{ - long long days, secs, years; - int remdays, remsecs, remyears; - int qc_cycles, c_cycles, q_cycles; - int months; - int wday, yday, leap; - static const char days_in_month[] = {31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 29}; - - /* Reject time_t values whose year would overflow int */ - if (t < INT_MIN * 31622400LL || t > INT_MAX * 31622400LL) - return -1; - - secs = t - LEAPOCH; - days = secs / 86400; - remsecs = secs % 86400; - if (remsecs < 0) { - remsecs += 86400; - days--; - } - - wday = (3 + days) % 7; - if (wday < 0) - wday += 7; - - qc_cycles = days / DAYS_PER_400Y; - remdays = days % DAYS_PER_400Y; - if (remdays < 0) { - remdays += DAYS_PER_400Y; - qc_cycles--; - } - - c_cycles = remdays / DAYS_PER_100Y; - if (c_cycles == 4) - c_cycles--; - remdays -= c_cycles * DAYS_PER_100Y; - - q_cycles = remdays / DAYS_PER_4Y; - if (q_cycles == 25) - q_cycles--; - remdays -= q_cycles * DAYS_PER_4Y; - - remyears = remdays / 365; - if (remyears == 4) - remyears--; - remdays -= remyears * 365; - - leap = !remyears && (q_cycles || !c_cycles); - yday = remdays + 31 + 28 + leap; - if (yday >= 365 + leap) - yday -= 365 + leap; - - years = remyears + 4 * q_cycles + 100 * c_cycles + 400LL * qc_cycles; - - for (months = 0; days_in_month[months] <= remdays; months++) remdays -= days_in_month[months]; - - if (months >= 10) { - months -= 12; - years++; - } - - if (years + 100 > INT_MAX || years + 100 < INT_MIN) - return -1; - - tm->tm_year = years + 100; - tm->tm_mon = months + 2; - tm->tm_mday = remdays + 1; - tm->tm_wday = wday; - tm->tm_yday = yday; - - tm->tm_hour = remsecs / 3600; - tm->tm_min = remsecs / 60 % 60; - tm->tm_sec = remsecs % 60; - - return 0; -} - -struct tm *gmtime_r(const time_t *restrict t, struct tm *restrict tm) -{ - if (__secs_to_tm(*t, tm) < 0) { - errno = EOVERFLOW; - return 0; - } - tm->tm_isdst = 0; - tm->__tm_gmtoff = 0; - tm->__tm_zone = __utc; - return tm; -} - -struct tm *gmtime(const time_t *timer) -{ - static struct tm tm; - return gmtime_r(timer, &tm); -} - -struct tm *localtime_r(const time_t *restrict t, struct tm *restrict tm) -{ - if (*t < INT_MIN * 31622400LL || *t > INT_MAX * 31622400LL) { - errno = EOVERFLOW; - return 0; - } - - if (__secs_to_tm(*t, tm) < 0) { - errno = EOVERFLOW; - return 0; - } - - tm->tm_isdst = 0; - tm->__tm_gmtoff = 0; - tm->__tm_zone = __utc; - - return tm; -} - -struct tm *localtime(const time_t *timep) -{ - static struct tm tm; - return localtime_r(timep, &tm); -} - -time_t time(time_t *t) -{ - struct timespec ts; - clock_gettime(CLOCK_REALTIME, &ts); - time_t ret = ts.tv_sec; - if (t) - *t = ret; - return ret; -} - -int gettimeofday(struct timeval *tv, struct timezone *tz) -{ - struct timespec ts; - if (!tv) - return 0; - clock_gettime(CLOCK_REALTIME, &ts); - tv->tv_sec = ts.tv_sec; - tv->tv_usec = (int)ts.tv_nsec / 1000; - return 0; -} - -int settimeofday(const struct timeval *tv, const struct timezone *_tz) -{ - if (!tv) - return 0; - if (tv->tv_usec >= 1000000ULL) - return -EINVAL; - return clock_settime(CLOCK_REALTIME, - &((struct timespec){.tv_sec = tv->tv_sec, .tv_nsec = tv->tv_usec * 1000})); -} - -// TODO: -int utimes(const char *filename, const struct timeval times[2]) -{ - unimplemented(); - return 0; -} - -// TODO -void tzset() -{ - unimplemented(); - return; -} - -// TODO -char *ctime_r(const time_t *t, char *buf) -{ - unimplemented(); - return NULL; -} - -// TODO -clock_t clock(void) -{ - unimplemented(); - return 0; -} - -#ifdef RUX_CONFIG_FP_SIMD -double difftime(time_t t1, time_t t0) -{ - return t1 - t0; -} -#endif diff --git a/ulib/ruxlibc/c/uio.c b/ulib/ruxlibc/c/uio.c deleted file mode 100644 index a1e597b3c..000000000 --- a/ulib/ruxlibc/c/uio.c +++ /dev/null @@ -1,32 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include - -// TODO -ssize_t readv(int fd, const struct iovec *iov, int iovcnt) -{ - unimplemented(); - return 0; -} - -// TODO -ssize_t preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset) -{ - unimplemented(); - return 0; -} - -// TODO -ssize_t pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset) -{ - unimplemented(); - return 0; -} diff --git a/ulib/ruxlibc/c/unistd.c b/ulib/ruxlibc/c/unistd.c deleted file mode 100644 index a559144c9..000000000 --- a/ulib/ruxlibc/c/unistd.c +++ /dev/null @@ -1,236 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A - * PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -// TODO: -pid_t getppid(void) -{ - unimplemented(); - return 0; -} - -// TODO: -uid_t geteuid(void) -{ - unimplemented(); - return 0; -} - -// TODO -uid_t getuid(void) -{ - unimplemented(); - return 0; -} - -// TODO -int setuid(uid_t __uid) -{ - unimplemented(); - return 0; -} - -// TODO -pid_t setsid(void) -{ - unimplemented(); - return 0; -} - -// TODO -int setgid(gid_t gid) -{ - unimplemented(); - return 0; -} - -// TODO -int isatty(int fd) -{ - unimplemented(); - return 0; -} - -// TODO -int getpagesize(void) -{ - unimplemented(); - return 0; -} - -// TODO -ssize_t pread(int fd, void *buf, size_t count, off_t offset) -{ - unimplemented(); - return 0; -} - -// TODO -ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset) -{ - unimplemented(); - return 0; -} - -// TODO -int gethostname(char *name, size_t len) -{ - size_t i; - struct utsname uts; - if (uname(&uts)) - return -1; - if (len > sizeof uts.nodename) - len = sizeof uts.nodename; - for (i = 0; i < len && (name[i] = uts.nodename[i]); i++) - ; - if (i && i == len) - name[i - 1] = 0; - return 0; -} - -// TODO -int chown(const char *path, uid_t owner, gid_t group) -{ - unimplemented(); - return 0; -} - -unsigned int sleep(unsigned int seconds) -{ - struct timespec ts; - - ts.tv_sec = seconds; - ts.tv_nsec = 0; - if (nanosleep(&ts, &ts)) - return ts.tv_sec; - - return 0; -} - -int usleep(unsigned useconds) -{ - struct timespec tv = {.tv_sec = useconds / 1000000, .tv_nsec = (useconds % 1000000) * 1000}; - return nanosleep(&tv, &tv); -} - -#ifdef RUX_CONFIG_FS - -// TODO: -int access(const char *pathname, int mode) -{ - unimplemented(); - return 0; -} - -// TODO: -ssize_t readlink(const char *path, char *buf, size_t bufsiz) -{ - unimplemented(); - return 0; -} - -// TODO: -int fsync(int fd) -{ - unimplemented(); - return 0; -} - -// TODO -int fdatasync(int __fildes) -{ - unimplemented(); - return 0; -} - -// TODO: -int fchown(int fd, uid_t owner, gid_t group) -{ - unimplemented("owner: %x group: %x", owner, group); - return 0; -} - -// TODO: -int ftruncate(int fd, off_t length) -{ - unimplemented(); - return 0; -} - -// TODO -int chdir(const char *__path) -{ - unimplemented(); - return 0; -} - -// TODO -int truncate(const char *path, off_t length) -{ - unimplemented(); - return 0; -} - -#endif // RUX_CONFIG_FS - -#ifdef RUX_CONFIG_PIPE - -int pipe2(int fd[2], int flag) -{ - if (!flag) - return pipe(fd); - if (flag & ~(O_CLOEXEC | O_NONBLOCK)) - return -EINVAL; - - int res = pipe(fd); - if (res != 0) - return res; - - if (flag & O_CLOEXEC) { - fcntl(fd[0], F_SETFD, FD_CLOEXEC); - fcntl(fd[1], F_SETFD, FD_CLOEXEC); - } - if (flag & O_NONBLOCK) { - fcntl(fd[0], F_SETFL, O_NONBLOCK); - fcntl(fd[1], F_SETFL, O_NONBLOCK); - } - - return 0; -} - -#endif // RUX_CONFIG_PIPE - -// TODO -_Noreturn void _exit(int status) -{ - exit(status); -} - -// TODO -int execve(const char *__path, char *const *__argv, char *const *__envp) -{ - unimplemented(); - return 0; -} - -// TODO -pid_t fork(void) -{ - unimplemented(); - return -1; -} diff --git a/ulib/ruxlibc/c/utsname.c b/ulib/ruxlibc/c/utsname.c deleted file mode 100644 index 7aeecd9e6..000000000 --- a/ulib/ruxlibc/c/utsname.c +++ /dev/null @@ -1,18 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include - -// TODO -int uname(struct utsname *a) -{ - unimplemented(); - return 0; -} diff --git a/ulib/ruxlibc/c/wait.c b/ulib/ruxlibc/c/wait.c deleted file mode 100644 index 90add3e59..000000000 --- a/ulib/ruxlibc/c/wait.c +++ /dev/null @@ -1,26 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include -#include - -// TODO -pid_t waitpid(pid_t pid, int *status, int options) -{ - unimplemented(); - return 0; -} - -// TODO -pid_t wait3(int *status, int _options, struct rusage *usage) -{ - unimplemented(); - return 0; -} diff --git a/ulib/ruxlibc/ctypes.h b/ulib/ruxlibc/ctypes.h deleted file mode 100644 index f1c1cc8b1..000000000 --- a/ulib/ruxlibc/ctypes.h +++ /dev/null @@ -1,11 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -#include -#include diff --git a/ulib/ruxlibc/src/env.rs b/ulib/ruxlibc/src/env.rs deleted file mode 100644 index eb0ca4668..000000000 --- a/ulib/ruxlibc/src/env.rs +++ /dev/null @@ -1,140 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ -use core::ffi::{c_char, c_int, c_void}; -use ruxos_posix_api::{environ, environ_iter, RUX_ENVIRON}; - -use crate::malloc::{free, malloc}; -use crate::string::strlen; -unsafe fn find_env(search: *const c_char) -> Option<(usize, *mut c_char)> { - for (i, mut item) in environ_iter().enumerate() { - let mut search = search; - loop { - let end_of_query = *search == 0 || *search == b'=' as c_char; - assert_ne!(*item, 0, "environ has an item without value"); - if *item == b'=' as c_char || end_of_query { - if *item == b'=' as c_char && end_of_query { - // Both keys env here - return Some((i, item.add(1))); - } else { - break; - } - } - - if *item != *search { - break; - } - - item = item.add(1); - search = search.add(1); - } - } - None -} - -unsafe fn put_new_env(insert: *mut c_char) { - // XXX: Another problem is that `environ` can be set to any pointer, which means there is a - // chance of a memory leak. But we can check if it was the same as before, like musl does. - if environ == RUX_ENVIRON.as_mut_ptr() { - *RUX_ENVIRON.last_mut().unwrap() = insert; - RUX_ENVIRON.push(core::ptr::null_mut()); - // Likely a no-op but is needed due to Stacked Borrows. - environ = RUX_ENVIRON.as_mut_ptr(); - } else { - RUX_ENVIRON.clear(); - RUX_ENVIRON.extend(environ_iter()); - RUX_ENVIRON.push(insert); - RUX_ENVIRON.push(core::ptr::null_mut()); - environ = RUX_ENVIRON.as_mut_ptr(); - } -} - -unsafe fn copy_kv( - existing: *mut c_char, - key: *const c_char, - value: *const c_char, - key_len: usize, - value_len: usize, -) { - core::ptr::copy_nonoverlapping(key, existing, key_len); - core::ptr::write(existing.add(key_len), b'=' as c_char); - core::ptr::copy_nonoverlapping(value, existing.add(key_len + 1), value_len); - core::ptr::write(existing.add(key_len + 1 + value_len), 0); -} - -/// set an environ variable -#[no_mangle] -pub unsafe extern "C" fn setenv( - key: *const c_char, - value: *const c_char, - overwrite: c_int, -) -> c_int { - let key_len = strlen(key); - let value_len = strlen(value); - if let Some((i, existing)) = find_env(key) { - if overwrite == 0 { - return 0; - } - - let existing_len = strlen(existing); - if existing_len >= value_len { - // Reuse existing element's allocation - core::ptr::copy_nonoverlapping(value, existing, value_len); - core::ptr::write(existing.add(value_len), 0); - } else { - // Reuse environ slot, but allocate a new pointer. - let ptr = malloc(key_len + 1 + value_len + 1) as *mut c_char; - copy_kv(ptr, key, value, key_len, value_len); - environ.add(i).write(ptr); - } - } else { - // Expand environ and allocate a new pointer. - let ptr = malloc(key_len + 1 + value_len + 1) as *mut c_char; - copy_kv(ptr, key, value, key_len, value_len); - put_new_env(ptr); - } - 0 -} - -/// unset an environ variable -#[no_mangle] -pub unsafe extern "C" fn unsetenv(key: *const c_char) -> c_int { - if let Some((i, _)) = find_env(key) { - if environ == RUX_ENVIRON.as_mut_ptr() { - // No need to worry about updating the pointer, this does not - // reallocate in any way. And the final null is already shifted back. - let rm = RUX_ENVIRON.remove(i); - free(rm as *mut c_void); - // My UB paranoia. - environ = RUX_ENVIRON.as_mut_ptr(); - } else { - let len = RUX_ENVIRON.len(); - for _ in 0..len { - let rm = RUX_ENVIRON.pop().unwrap(); - free(rm as *mut c_void); - } - RUX_ENVIRON.extend( - environ_iter() - .enumerate() - .filter(|&(j, _)| j != i) - .map(|(_, v)| v), - ); - RUX_ENVIRON.push(core::ptr::null_mut()); - environ = RUX_ENVIRON.as_mut_ptr(); - } - } - 0 -} - -/// get the corresponding environ variable -#[no_mangle] -pub unsafe extern "C" fn getenv(name: *const c_char) -> *mut c_char { - find_env(name) - .map(|val| val.1) - .unwrap_or(core::ptr::null_mut()) -} diff --git a/ulib/ruxlibc/src/errno.rs b/ulib/ruxlibc/src/errno.rs deleted file mode 100644 index 6bfd13677..000000000 --- a/ulib/ruxlibc/src/errno.rs +++ /dev/null @@ -1,48 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use axerrno::LinuxError; -use core::ffi::{c_char, c_int}; - -/// The global errno variable. -#[cfg_attr(feature = "tls", thread_local)] -#[no_mangle] -#[allow(non_upper_case_globals)] -pub static mut errno: c_int = 0; - -pub fn set_errno(code: i32) { - unsafe { - errno = code; - } -} - -/// Returns a pointer to the global errno variable. -#[no_mangle] -pub unsafe extern "C" fn __errno_location() -> *mut c_int { - &mut errno -} - -/// Returns a pointer to the string representation of the given error code. -#[no_mangle] -pub unsafe extern "C" fn strerror(e: c_int) -> *mut c_char { - #[allow(non_upper_case_globals)] - static mut strerror_buf: [u8; 256] = [0; 256]; // TODO: thread safe - - let err_str = if e == 0 { - "Success" - } else { - LinuxError::try_from(e) - .map(|e| e.as_str()) - .unwrap_or("Unknown error") - }; - unsafe { - strerror_buf[..err_str.len()].copy_from_slice(err_str.as_bytes()); - strerror_buf.as_mut_ptr() as *mut c_char - } -} diff --git a/ulib/ruxlibc/src/fd_ops.rs b/ulib/ruxlibc/src/fd_ops.rs deleted file mode 100644 index 21518e5e6..000000000 --- a/ulib/ruxlibc/src/fd_ops.rs +++ /dev/null @@ -1,63 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use crate::{ctypes, utils::e}; -use axerrno::LinuxError; -use core::ffi::c_int; -use ruxos_posix_api::{sys_close, sys_dup, sys_dup2, sys_fcntl}; - -/// Close a file by `fd`. -#[no_mangle] -pub unsafe extern "C" fn close(fd: c_int) -> c_int { - e(sys_close(fd)) -} - -/// Duplicate a file descriptor. -#[no_mangle] -pub unsafe extern "C" fn dup(old_fd: c_int) -> c_int { - e(sys_dup(old_fd)) -} - -/// Duplicate a file descriptor, use file descriptor specified in `new_fd`. -#[no_mangle] -pub unsafe extern "C" fn dup2(old_fd: c_int, new_fd: c_int) -> c_int { - e(sys_dup2(old_fd, new_fd)) -} - -/// Duplicate a file descriptor, the caller can force the close-on-exec flag to -/// be set for the new file descriptor by specifying `O_CLOEXEC` in flags. -/// -/// If oldfd equals newfd, then `dup3()` fails with the error `EINVAL`. -#[no_mangle] -pub unsafe extern "C" fn dup3(old_fd: c_int, new_fd: c_int, flags: c_int) -> c_int { - if old_fd == new_fd { - return e((LinuxError::EINVAL as c_int).wrapping_neg()); - } - let r = e(sys_dup2(old_fd, new_fd)); - if r < 0 { - r - } else { - if flags as u32 & ctypes::O_CLOEXEC != 0 { - e(sys_fcntl( - new_fd, - ctypes::F_SETFD as c_int, - ctypes::FD_CLOEXEC as usize, - )); - } - new_fd - } -} - -/// Manipulate file descriptor. -/// -/// TODO: `SET/GET` command is ignored -#[no_mangle] -pub unsafe extern "C" fn ax_fcntl(fd: c_int, cmd: c_int, arg: usize) -> c_int { - e(sys_fcntl(fd, cmd, arg)) -} diff --git a/ulib/ruxlibc/src/fs.rs b/ulib/ruxlibc/src/fs.rs deleted file mode 100644 index a396e4ae4..000000000 --- a/ulib/ruxlibc/src/fs.rs +++ /dev/null @@ -1,109 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use core::ffi::{c_char, c_int}; - -use ruxos_posix_api::{ - sys_fstat, sys_getcwd, sys_lseek, sys_lstat, sys_mkdir, sys_open, sys_rename, sys_rmdir, - sys_stat, sys_unlink, -}; - -use crate::{ctypes, utils::e}; - -/// Open a file by `filename` and insert it into the file descriptor table. -/// -/// Return its index in the file table (`fd`). Return `EMFILE` if it already -/// has the maximum number of files open. -#[no_mangle] -pub unsafe extern "C" fn ax_open( - filename: *const c_char, - flags: c_int, - mode: ctypes::mode_t, -) -> c_int { - e(sys_open(filename, flags, mode)) -} - -/// Set the position of the file indicated by `fd`. -/// -/// Return its position after seek. -#[no_mangle] -pub unsafe extern "C" fn lseek(fd: c_int, offset: ctypes::off_t, whence: c_int) -> ctypes::off_t { - e(sys_lseek(fd, offset, whence) as _) as _ -} - -/// Get the file metadata by `path` and write into `buf`. -/// -/// Return 0 if success. -#[no_mangle] -pub unsafe extern "C" fn stat(path: *const c_char, buf: *mut ctypes::stat) -> c_int { - e(sys_stat(path, buf as _)) -} - -/// Get file metadata by `fd` and write into `buf`. -/// -/// Return 0 if success. -#[no_mangle] -pub unsafe extern "C" fn fstat(fd: c_int, buf: *mut ctypes::stat) -> c_int { - e(sys_fstat(fd, buf as *mut core::ffi::c_void)) -} - -/// Get the metadata of the symbolic link and write into `buf`. -/// -/// Return 0 if success. -#[no_mangle] -pub unsafe extern "C" fn lstat(path: *const c_char, buf: *mut ctypes::stat) -> c_int { - e(sys_lstat(path, buf) as _) -} - -/// Get the path of the current directory. -#[no_mangle] -pub unsafe extern "C" fn getcwd(buf: *mut c_char, size: usize) -> *mut c_char { - if buf.is_null() && size != 0 { - crate::errno::set_errno(axerrno::LinuxError::EINVAL as _); - return core::ptr::null_mut() as *mut c_char; - } - let e = sys_getcwd(buf, size); - if e < 0 { - return core::ptr::null_mut() as *mut c_char; - } - if e == 0 || buf.read() != '/' as _ { - crate::errno::set_errno(axerrno::LinuxError::ENOENT as _); - return core::ptr::null_mut() as *mut c_char; - } - buf -} - -/// Rename `old` to `new` -/// If new exists, it is first removed. -/// -/// Return 0 if the operation succeeds, otherwise return -1. -#[no_mangle] -pub unsafe extern "C" fn rename(old: *const c_char, new: *const c_char) -> c_int { - e(sys_rename(old, new)) -} - -/// Remove a directory, which must be empty -/// -/// Return 0 if the operation succeeds, otherwise return -1. -#[no_mangle] -pub unsafe extern "C" fn rmdir(pathname: *const c_char) -> c_int { - e(sys_rmdir(pathname)) -} - -/// Removes a file from the filesystem. -#[no_mangle] -pub unsafe extern "C" fn unlink(pathname: *const c_char) -> c_int { - e(sys_unlink(pathname)) -} - -/// Creates a new directory -#[no_mangle] -pub unsafe extern "C" fn mkdir(pathname: *const c_char, mode: ctypes::mode_t) -> c_int { - e(sys_mkdir(pathname, mode)) -} diff --git a/ulib/ruxlibc/src/io.rs b/ulib/ruxlibc/src/io.rs deleted file mode 100644 index 2c7450195..000000000 --- a/ulib/ruxlibc/src/io.rs +++ /dev/null @@ -1,54 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use core::ffi::{c_int, c_void}; -#[cfg(feature = "fd")] -use ruxos_posix_api::sys_ioctl; - -#[cfg(not(test))] -use ruxos_posix_api::sys_write; -use ruxos_posix_api::{sys_read, sys_writev}; - -use crate::{ctypes, utils::e}; - -/// Read data from the file indicated by `fd`. -/// -/// Return the read size if success. -#[no_mangle] -pub unsafe extern "C" fn read(fd: c_int, buf: *mut c_void, count: usize) -> ctypes::ssize_t { - e(sys_read(fd, buf, count) as _) as _ -} - -/// Write data to the file indicated by `fd`. -/// -/// Return the written size if success. -#[no_mangle] -#[cfg(not(test))] -pub unsafe extern "C" fn write(fd: c_int, buf: *const c_void, count: usize) -> ctypes::ssize_t { - e(sys_write(fd, buf, count) as _) as _ -} - -/// Write a vector. -#[no_mangle] -pub unsafe extern "C" fn writev( - fd: c_int, - iov: *const ctypes::iovec, - iocnt: c_int, -) -> ctypes::ssize_t { - e(sys_writev(fd, iov, iocnt) as _) as _ -} - -/// Manipulate file descriptor. -/// -/// TODO: `SET/GET` command is ignored -#[cfg(feature = "fd")] -#[no_mangle] -pub unsafe extern "C" fn rux_ioctl(fd: c_int, req: c_int, arg: usize) -> c_int { - e(sys_ioctl(fd, req.try_into().unwrap(), arg)) -} diff --git a/ulib/ruxlibc/src/io_mpx.rs b/ulib/ruxlibc/src/io_mpx.rs deleted file mode 100644 index 15cb297b2..000000000 --- a/ulib/ruxlibc/src/io_mpx.rs +++ /dev/null @@ -1,76 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use crate::{ctypes, utils::e}; - -use core::ffi::c_int; - -#[cfg(feature = "poll")] -use ruxos_posix_api::sys_poll; -#[cfg(feature = "select")] -use ruxos_posix_api::sys_select; -#[cfg(feature = "epoll")] -use ruxos_posix_api::{sys_epoll_create, sys_epoll_ctl, sys_epoll_wait}; - -/// Creates a new epoll instance. -/// -/// It returns a file descriptor referring to the new epoll instance. -#[cfg(feature = "epoll")] -#[no_mangle] -pub unsafe extern "C" fn epoll_create(size: c_int) -> c_int { - e(sys_epoll_create(size)) -} - -/// Control interface for an epoll file descriptor -#[cfg(feature = "epoll")] -#[no_mangle] -pub unsafe extern "C" fn epoll_ctl( - epfd: c_int, - op: c_int, - fd: c_int, - event: *mut ctypes::epoll_event, -) -> c_int { - e(sys_epoll_ctl(epfd, op, fd, event)) -} - -/// Waits for events on the epoll instance referred to by the file descriptor epfd. -#[cfg(feature = "epoll")] -#[no_mangle] -pub unsafe extern "C" fn epoll_wait( - epfd: c_int, - events: *mut ctypes::epoll_event, - maxevents: c_int, - timeout: c_int, -) -> c_int { - e(sys_epoll_wait(epfd, events, maxevents, timeout)) -} - -/// Monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" for some class of I/O operation -#[cfg(feature = "select")] -#[no_mangle] -pub unsafe extern "C" fn select( - nfds: c_int, - readfds: *mut ctypes::fd_set, - writefds: *mut ctypes::fd_set, - exceptfds: *mut ctypes::fd_set, - timeout: *mut ctypes::timeval, -) -> c_int { - e(sys_select(nfds, readfds, writefds, exceptfds, timeout)) -} - -/// Monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" for some class of I/O operation -#[cfg(feature = "poll")] -#[no_mangle] -pub unsafe extern "C" fn poll( - fds: *mut ctypes::pollfd, - nfds: ctypes::nfds_t, - timeout: c_int, -) -> c_int { - e(sys_poll(fds, nfds, timeout)) -} diff --git a/ulib/ruxlibc/src/lib.rs b/ulib/ruxlibc/src/lib.rs deleted file mode 100644 index 160566cff..000000000 --- a/ulib/ruxlibc/src/lib.rs +++ /dev/null @@ -1,152 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -//! [Ruxos] user program library for C apps. -//! -//! ## Cargo Features -//! -//! - CPU -//! - `smp`: Enable SMP (symmetric multiprocessing) support. -//! - `fp_simd`: Enable floating point and SIMD support. -//! - Interrupts: -//! - `irq`: Enable interrupt handling support. -//! - Memory -//! - `alloc`: Enable dynamic memory allocation. -//! - `tls`: Enable thread-local storage. -//! - Task management -//! - `multitask`: Enable multi-threading support. -//! - Upperlayer stacks -//! - `fs`: Enable file system support. -//! - `net`: Enable networking support. -//! - `signal`: Enable signal support. -//! - Lib C functions -//! - `fd`: Enable file descriptor table. -//! - `pipe`: Enable pipe support. -//! - `select`: Enable synchronous I/O multiplexing ([select]) support. -//! - `epoll`: Enable event polling ([epoll]) support. -//! -//! [Ruxos]: https://github.com/syswonder/ruxos -//! [select]: https://man7.org/linux/man-pages/man2/select.2.html -//! [epoll]: https://man7.org/linux/man-pages/man7/epoll.7.html - -#![cfg_attr(all(not(test), not(doc)), no_std)] -#![feature(doc_cfg)] -#![feature(doc_auto_cfg)] -#![feature(naked_functions)] -#![feature(thread_local)] -#![allow(clippy::missing_safety_doc)] - -#[cfg(feature = "alloc")] -extern crate alloc; -#[cfg(feature = "alloc")] -mod env; -#[path = "."] -mod ctypes { - #[rustfmt::skip] - #[path = "libctypes_gen.rs"] - #[allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals, clippy::upper_case_acronyms)] - mod libctypes; - - pub use libctypes::*; - pub use ruxos_posix_api::ctypes::*; -} - -#[macro_use] -mod utils; - -#[cfg(feature = "fd")] -mod fd_ops; -#[cfg(feature = "fs")] -mod fs; -#[cfg(any(feature = "select", feature = "poll", feature = "epoll"))] -mod io_mpx; -#[cfg(feature = "alloc")] -mod malloc; -#[cfg(feature = "alloc")] -mod mmap; -#[cfg(feature = "net")] -mod net; -#[cfg(feature = "pipe")] -mod pipe; -#[cfg(feature = "multitask")] -mod pthread; -#[cfg(feature = "alloc")] -mod strftime; -#[cfg(feature = "fp_simd")] -mod strtod; - -mod errno; -mod io; -mod mktime; -mod rand; -mod resource; -mod setjmp; -mod signal; -mod string; -mod sys; -mod time; -mod unistd; - -#[cfg(not(test))] -pub use self::io::write; -pub use self::io::{read, writev}; - -pub use self::errno::strerror; -pub use self::mktime::mktime; -pub use self::rand::{getrandom, rand, random, srand}; -pub use self::resource::{getrlimit, setrlimit}; -pub use self::setjmp::{longjmp, setjmp}; -pub use self::string::{strlen, strnlen}; -pub use self::sys::sysconf; -pub use self::time::{clock_gettime, nanosleep}; -pub use self::unistd::{abort, exit, getpid}; - -#[cfg(feature = "alloc")] -pub use self::env::{getenv, setenv, unsetenv}; -#[cfg(feature = "fd")] -pub use self::fd_ops::{ax_fcntl, close, dup, dup2, dup3}; -#[cfg(feature = "fs")] -pub use self::fs::{ax_open, fstat, getcwd, lseek, lstat, mkdir, rename, rmdir, stat, unlink}; -#[cfg(feature = "fd")] -pub use self::io::rux_ioctl; -#[cfg(feature = "poll")] -pub use self::io_mpx::poll; -#[cfg(feature = "select")] -pub use self::io_mpx::select; -#[cfg(feature = "epoll")] -pub use self::io_mpx::{epoll_create, epoll_ctl, epoll_wait}; -#[cfg(feature = "alloc")] -pub use self::malloc::{free, malloc}; -#[cfg(feature = "alloc")] -pub use self::mmap::{mmap, munmap}; -#[cfg(feature = "net")] -pub use self::net::{ - accept, ax_sendmsg, bind, connect, freeaddrinfo, getaddrinfo, getpeername, getsockname, listen, - recv, recvfrom, send, sendto, shutdown, socket, -}; -#[cfg(feature = "pipe")] -pub use self::pipe::pipe; -#[cfg(feature = "multitask")] -pub use self::pthread::{ - pthread_cond_broadcast, pthread_cond_init, pthread_cond_signal, pthread_cond_wait, -}; -#[cfg(feature = "multitask")] -pub use self::pthread::{pthread_create, pthread_exit, pthread_join, pthread_self}; -#[cfg(feature = "multitask")] -pub use self::pthread::{ - pthread_mutex_init, pthread_mutex_lock, pthread_mutex_trylock, pthread_mutex_unlock, -}; -#[cfg(feature = "alloc")] -pub use self::strftime::strftime; -#[cfg(feature = "fp_simd")] -pub use self::strtod::{strtod, strtof}; -#[cfg(feature = "signal")] -pub use self::time::{getitimer, setitimer}; -#[cfg(feature = "signal")] -pub use self::unistd::{alarm, ualarm}; diff --git a/ulib/ruxlibc/src/malloc.rs b/ulib/ruxlibc/src/malloc.rs deleted file mode 100644 index 4b2cb25c5..000000000 --- a/ulib/ruxlibc/src/malloc.rs +++ /dev/null @@ -1,65 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -//! Provides the corresponding malloc(size_t) and free(size_t) when using the C user program. -//! -//! The normal malloc(size_t) and free(size_t) are provided by the library malloc.h, and -//! sys_brk is used internally to apply for memory from the kernel. But in a unikernel like -//! `Ruxos`, we noticed that the heap of the Rust user program is shared with the kernel. In -//! order to maintain consistency, C user programs also choose to share the kernel heap, -//! skipping the sys_brk step. - -use alloc::alloc::{alloc, dealloc}; -use core::alloc::Layout; -use core::ffi::c_void; - -use crate::ctypes; - -struct MemoryControlBlock { - size: usize, -} - -const CTRL_BLK_SIZE: usize = core::mem::size_of::(); - -/// Allocate memory and return the memory address. -/// -/// Returns 0 on failure (the current implementation does not trigger an exception) -#[no_mangle] -pub unsafe extern "C" fn malloc(size: ctypes::size_t) -> *mut c_void { - // Allocate `(actual length) + 8`. The lowest 8 Bytes are stored in the actual allocated space size. - // This is because free(uintptr_t) has only one parameter representing the address, - // So we need to save in advance to know the size of the memory space that needs to be released - let layout = Layout::from_size_align(size + CTRL_BLK_SIZE, 8).unwrap(); - unsafe { - let ptr = alloc(layout).cast::(); - assert!(!ptr.is_null(), "malloc failed"); - ptr.write(MemoryControlBlock { size }); - ptr.add(1).cast() - } -} - -/// Deallocate memory. -/// -/// (WARNING) If the address to be released does not match the allocated address, an error should -/// occur, but it will NOT be checked out. This is due to the global allocator `Buddy_system` -/// (currently used) does not check the validity of address to be released. -#[no_mangle] -pub unsafe extern "C" fn free(ptr: *mut c_void) { - if ptr.is_null() { - return; - } - let ptr = ptr.cast::(); - assert!(ptr as usize > CTRL_BLK_SIZE, "free a null pointer"); - unsafe { - let ptr = ptr.sub(1); - let size = ptr.read().size; - let layout = Layout::from_size_align(size + CTRL_BLK_SIZE, 8).unwrap(); - dealloc(ptr.cast(), layout) - } -} diff --git a/ulib/ruxlibc/src/mktime.rs b/ulib/ruxlibc/src/mktime.rs deleted file mode 100644 index fadc1cfb5..000000000 --- a/ulib/ruxlibc/src/mktime.rs +++ /dev/null @@ -1,67 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use core::ffi::c_int; - -use crate::ctypes; - -const MONTH_DAYS: [[c_int; 12]; 2] = [ - // Non-leap years: - [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], - // Leap years: - [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], -]; - -#[inline(always)] -fn leap_year(year: c_int) -> bool { - year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) -} - -/// Convert broken-down time into time since the Epoch. -#[no_mangle] -pub unsafe extern "C" fn mktime(t: *mut ctypes::tm) -> ctypes::time_t { - let mut year = (*t).tm_year + 1900; - let mut month = (*t).tm_mon; - let mut day = (*t).tm_mday as i64 - 1; - - let leap = if leap_year(year) { 1 } else { 0 }; - - if year < 1970 { - day = MONTH_DAYS[if leap_year(year) { 1 } else { 0 }][(*t).tm_mon as usize] as i64 - day; - - while year < 1969 { - year += 1; - day += if leap_year(year) { 366 } else { 365 }; - } - - while month < 11 { - month += 1; - day += MONTH_DAYS[leap][month as usize] as i64; - } - - (-(day * (60 * 60 * 24) - - (((*t).tm_hour as i64) * (60 * 60) + ((*t).tm_min as i64) * 60 + (*t).tm_sec as i64))) - as ctypes::time_t - } else { - while year > 1970 { - year -= 1; - day += if leap_year(year) { 366 } else { 365 }; - } - - while month > 0 { - month -= 1; - day += MONTH_DAYS[leap][month as usize] as i64; - } - - (day * (60 * 60 * 24) - + ((*t).tm_hour as i64) * (60 * 60) - + ((*t).tm_min as i64) * 60 - + (*t).tm_sec as i64) as ctypes::time_t - } -} diff --git a/ulib/ruxlibc/src/mmap.rs b/ulib/ruxlibc/src/mmap.rs deleted file mode 100644 index 4573e2cfc..000000000 --- a/ulib/ruxlibc/src/mmap.rs +++ /dev/null @@ -1,64 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use crate::ctypes; -use core::ffi::{c_int, c_void}; - -use ruxos_posix_api::{sys_madvise, sys_mmap, sys_mprotect, sys_mremap, sys_msync, sys_munmap}; - -/// Map a file or device into virtual memory. -#[no_mangle] -pub unsafe extern "C" fn mmap( - addr: *mut c_void, - len: ctypes::size_t, - prot: c_int, - flags: c_int, - fid: c_int, - offset: ctypes::off_t, -) -> *mut c_void { - sys_mmap(addr, len, prot, flags, fid, offset) -} - -/// Unmap a range address of memory. -#[no_mangle] -pub unsafe extern "C" fn munmap(addr: *mut c_void, len: ctypes::size_t) -> c_int { - sys_munmap(addr, len) -} - -/// Sync pages mapped in memory to file. -#[no_mangle] -pub unsafe extern "C" fn msync(addr: *mut c_void, len: ctypes::size_t, flags: c_int) -> c_int { - sys_msync(addr, len, flags) -} - -/// Remap the address for already mapped memory. -#[no_mangle] -pub unsafe extern "C" fn mremap( - old_addr: *mut c_void, - old_size: ctypes::size_t, - new_size: ctypes::size_t, - flags: c_int, - new_addr: *mut c_void, -) -> *mut c_void { - sys_mremap(old_addr, old_size, new_size, flags, new_addr) -} - -/// Change the accessiblity for already mapped memory. -#[no_mangle] -pub unsafe extern "C" fn mprotect(addr: *mut c_void, len: ctypes::size_t, flags: c_int) -> c_int { - sys_mprotect(addr, len, flags) -} - -/// Advise the operating system about the expected behavior of a specific region of memory. -/// -/// Note: Unimplement yet. -#[no_mangle] -pub unsafe extern "C" fn madvise(addr: *mut c_void, len: ctypes::size_t, advice: c_int) -> c_int { - sys_madvise(addr, len, advice) -} diff --git a/ulib/ruxlibc/src/net.rs b/ulib/ruxlibc/src/net.rs deleted file mode 100644 index 6ae7d237c..000000000 --- a/ulib/ruxlibc/src/net.rs +++ /dev/null @@ -1,198 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use core::ffi::{c_char, c_int, c_void}; -use ruxos_posix_api as api; - -use crate::{ctypes, utils::e}; - -/// Create an socket for communication. -/// -/// Return the socket file descriptor. -#[no_mangle] -pub unsafe extern "C" fn socket(domain: c_int, socktype: c_int, protocol: c_int) -> c_int { - e(api::sys_socket(domain, socktype, protocol)) -} - -/// Bind a address to a socket. -/// -/// Return 0 if success. -#[no_mangle] -pub unsafe extern "C" fn bind( - socket_fd: c_int, - socket_addr: *const ctypes::sockaddr, - addrlen: ctypes::socklen_t, -) -> c_int { - e(api::sys_bind(socket_fd, socket_addr, addrlen)) -} - -/// Connects the socket to the address specified. -/// -/// Return 0 if success. -#[no_mangle] -pub unsafe extern "C" fn connect( - socket_fd: c_int, - socket_addr: *const ctypes::sockaddr, - addrlen: ctypes::socklen_t, -) -> c_int { - e(api::sys_connect(socket_fd, socket_addr, addrlen)) -} - -/// Send a message on a socket to the address specified. -/// -/// Return the number of bytes sent if success. -#[no_mangle] -pub unsafe extern "C" fn sendto( - socket_fd: c_int, - buf_ptr: *const c_void, - len: ctypes::size_t, - flag: c_int, // currently not used - socket_addr: *const ctypes::sockaddr, - addrlen: ctypes::socklen_t, -) -> ctypes::ssize_t { - if socket_addr.is_null() && addrlen == 0 { - return e(api::sys_send(socket_fd, buf_ptr, len, flag) as _) as _; - } - e(api::sys_sendto(socket_fd, buf_ptr, len, flag, socket_addr, addrlen) as _) as _ -} - -/// Send a message on a socket to the address connected. -/// -/// Return the number of bytes sent if success. -#[no_mangle] -pub unsafe extern "C" fn send( - socket_fd: c_int, - buf_ptr: *const c_void, - len: ctypes::size_t, - flag: c_int, // currently not used -) -> ctypes::ssize_t { - e(api::sys_send(socket_fd, buf_ptr, len, flag) as _) as _ -} - -/// Receive a message on a socket and get its source address. -/// -/// Return the number of bytes received if success. -#[no_mangle] -pub unsafe extern "C" fn recvfrom( - socket_fd: c_int, - buf_ptr: *mut c_void, - len: ctypes::size_t, - flag: c_int, // currently not used - socket_addr: *mut ctypes::sockaddr, - addrlen: *mut ctypes::socklen_t, -) -> ctypes::ssize_t { - if socket_addr.is_null() { - return e(api::sys_recv(socket_fd, buf_ptr, len, flag) as _) as _; - } - e(api::sys_recvfrom(socket_fd, buf_ptr, len, flag, socket_addr, addrlen) as _) as _ -} - -/// Receive a message on a socket. -/// -/// Return the number of bytes received if success. -#[no_mangle] -pub unsafe extern "C" fn recv( - socket_fd: c_int, - buf_ptr: *mut c_void, - len: ctypes::size_t, - flag: c_int, // currently not used -) -> ctypes::ssize_t { - e(api::sys_recv(socket_fd, buf_ptr, len, flag) as _) as _ -} - -/// Listen for connections on a socket -/// -/// Return 0 if success. -#[no_mangle] -pub unsafe extern "C" fn listen( - socket_fd: c_int, - backlog: c_int, // currently not used -) -> c_int { - e(api::sys_listen(socket_fd, backlog)) -} - -/// Accept for connections on a socket -/// -/// Return file descriptor for the accepted socket if success. -#[no_mangle] -pub unsafe extern "C" fn accept( - socket_fd: c_int, - socket_addr: *mut ctypes::sockaddr, - socket_len: *mut ctypes::socklen_t, -) -> c_int { - e(api::sys_accept(socket_fd, socket_addr, socket_len)) -} - -/// Shut down a full-duplex connection. -/// -/// Return 0 if success. -#[no_mangle] -pub unsafe extern "C" fn shutdown( - socket_fd: c_int, - flag: c_int, // currently not used -) -> c_int { - e(api::sys_shutdown(socket_fd, flag)) -} - -/// Query addresses for a domain name. -/// -/// Return address number if success. -#[no_mangle] -pub unsafe extern "C" fn getaddrinfo( - nodename: *const c_char, - servname: *const c_char, - hints: *const ctypes::addrinfo, - res: *mut *mut ctypes::addrinfo, -) -> c_int { - let ret = e(api::sys_getaddrinfo(nodename, servname, hints, res)); - match ret { - r if r < 0 => ctypes::EAI_FAIL, - 0 => ctypes::EAI_NONAME, - _ => 0, - } -} - -/// Free queried `addrinfo` struct -#[no_mangle] -pub unsafe extern "C" fn freeaddrinfo(res: *mut ctypes::addrinfo) { - api::sys_freeaddrinfo(res); -} - -/// Get current address to which the socket sockfd is bound. -#[no_mangle] -pub unsafe extern "C" fn getsockname( - sock_fd: c_int, - addr: *mut ctypes::sockaddr, - addrlen: *mut ctypes::socklen_t, -) -> c_int { - e(api::sys_getsockname(sock_fd, addr, addrlen)) -} - -/// Get peer address to which the socket sockfd is connected. -#[no_mangle] -pub unsafe extern "C" fn getpeername( - sock_fd: c_int, - addr: *mut ctypes::sockaddr, - addrlen: *mut ctypes::socklen_t, -) -> c_int { - e(api::sys_getpeername(sock_fd, addr, addrlen)) -} - -/// Send a message on a socket to the address connected. -/// The message is pointed to by the elements of the array msg.msg_iov. -/// -/// Return the number of bytes sent if success. -#[no_mangle] -pub unsafe extern "C" fn ax_sendmsg( - socket_fd: c_int, - msg: *const ctypes::msghdr, - flags: c_int, -) -> ctypes::ssize_t { - e(api::sys_sendmsg(socket_fd, msg, flags) as _) as _ -} diff --git a/ulib/ruxlibc/src/pipe.rs b/ulib/ruxlibc/src/pipe.rs deleted file mode 100644 index d4d853ca6..000000000 --- a/ulib/ruxlibc/src/pipe.rs +++ /dev/null @@ -1,23 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use core::ffi::c_int; - -use ruxos_posix_api::sys_pipe; - -use crate::utils::e; - -/// Create a pipe -/// -/// Return 0 if succeed -#[no_mangle] -pub unsafe extern "C" fn pipe(fd: *mut c_int) -> c_int { - let fds = unsafe { core::slice::from_raw_parts_mut(fd, 2) }; - e(sys_pipe(fds)) -} diff --git a/ulib/ruxlibc/src/pthread.rs b/ulib/ruxlibc/src/pthread.rs deleted file mode 100644 index 8df293acc..000000000 --- a/ulib/ruxlibc/src/pthread.rs +++ /dev/null @@ -1,157 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use crate::{ctypes, utils::e}; -use core::ffi::{c_int, c_void}; -use ruxos_posix_api as api; - -/// Returns the `pthread` struct of current thread. -#[no_mangle] -pub unsafe extern "C" fn pthread_self() -> ctypes::pthread_t { - api::sys_pthread_self() -} - -/// Create a new thread with the given entry point and argument. -/// -/// If successful, it stores the pointer to the newly created `struct __pthread` -/// in `res` and returns 0. -#[no_mangle] -pub unsafe extern "C" fn pthread_create( - res: *mut ctypes::pthread_t, - attr: *const ctypes::pthread_attr_t, - start_routine: extern "C" fn(arg: *mut c_void) -> *mut c_void, - arg: *mut c_void, -) -> c_int { - e(api::sys_pthread_create(res, attr, start_routine, arg)) -} - -/// Exits the current thread. The value `retval` will be returned to the joiner. -#[no_mangle] -pub unsafe extern "C" fn pthread_exit(retval: *mut c_void) -> ! { - api::sys_pthread_exit(retval) -} - -/// Waits for the given thread to exit, and stores the return value in `retval`. -#[no_mangle] -pub unsafe extern "C" fn pthread_join( - thread: ctypes::pthread_t, - retval: *mut *mut c_void, -) -> c_int { - e(api::sys_pthread_join(thread, retval)) -} - -/// Initialize a mutex. -#[no_mangle] -pub unsafe extern "C" fn pthread_mutex_init( - mutex: *mut ctypes::pthread_mutex_t, - attr: *const ctypes::pthread_mutexattr_t, -) -> c_int { - e(api::sys_pthread_mutex_init(mutex, attr)) -} - -/// Destroy a mutex. -#[no_mangle] -pub unsafe extern "C" fn pthread_mutex_destroy(mutex: *mut ctypes::pthread_mutex_t) -> c_int { - e(api::sys_pthread_mutex_destroy(mutex)) -} - -/// Lock the given mutex. -#[no_mangle] -pub unsafe extern "C" fn pthread_mutex_lock(mutex: *mut ctypes::pthread_mutex_t) -> c_int { - e(api::sys_pthread_mutex_lock(mutex)) -} - -/// Lock the given mutex. If the mutex is already locked, it returns immediatly with the error -/// code EBUSY. -#[no_mangle] -pub unsafe extern "C" fn pthread_mutex_trylock(mutex: *mut ctypes::pthread_mutex_t) -> c_int { - e(api::sys_pthread_mutex_trylock(mutex)) -} - -/// Unlock the given mutex. -#[no_mangle] -pub unsafe extern "C" fn pthread_mutex_unlock(mutex: *mut ctypes::pthread_mutex_t) -> c_int { - e(api::sys_pthread_mutex_unlock(mutex)) -} - -/// Initialize a condition variable -#[no_mangle] -pub unsafe extern "C" fn pthread_cond_init( - condvar: *mut ctypes::pthread_cond_t, - attr: *mut ctypes::pthread_condattr_t, -) -> c_int { - e(api::sys_pthread_cond_init(condvar, attr)) -} - -/// Destroy a condition variable -#[no_mangle] -pub unsafe extern "C" fn pthread_cond_destroy(condvar: *mut ctypes::pthread_cond_t) -> c_int { - e(api::sys_pthread_cond_destroy(condvar)) -} - -#[no_mangle] -/// Wait for the condition variable to be signaled or timeout -pub unsafe extern "C" fn pthread_cond_timedwait( - condvar: *mut ctypes::pthread_cond_t, - mutex: *mut ctypes::pthread_mutex_t, - abstime: *const ctypes::timespec, -) -> c_int { - e(api::sys_pthread_cond_timedwait(condvar, mutex, abstime)) -} - -/// Wait for the condition variable to be signaled -#[no_mangle] -pub unsafe extern "C" fn pthread_cond_wait( - condvar: *mut ctypes::pthread_cond_t, - mutex: *mut ctypes::pthread_mutex_t, -) -> c_int { - e(api::sys_pthread_cond_wait(condvar, mutex)) -} - -/// Restarts one of the threads that are waiting on the condition variable. -#[no_mangle] -pub unsafe extern "C" fn pthread_cond_signal(condvar: *mut ctypes::pthread_cond_t) -> c_int { - e(api::sys_pthread_cond_signal(condvar)) -} - -/// Restarts all the threads that are waiting on the condition variable. -#[no_mangle] -pub unsafe extern "C" fn pthread_cond_broadcast(condvar: *mut ctypes::pthread_cond_t) -> c_int { - e(api::sys_pthread_cond_broadcast(condvar)) -} - -/// Initialize a thread-specific data key -#[no_mangle] -pub unsafe extern "C" fn pthread_key_create( - key: *mut ctypes::pthread_key_t, - dtor: Option, -) -> c_int { - e(api::sys_pthread_key_create(key, dtor)) -} - -/// Destroy a thread-specific data key -#[no_mangle] -pub unsafe extern "C" fn pthread_key_delete(key: ctypes::pthread_key_t) -> c_int { - e(api::sys_pthread_key_delete(key)) -} - -/// Get the value of a thread-specific data key -#[no_mangle] -pub unsafe extern "C" fn pthread_getspecific(key: ctypes::pthread_key_t) -> *mut c_void { - api::sys_pthread_getspecific(key) -} - -/// Set the value of a thread-specific data key -#[no_mangle] -pub unsafe extern "C" fn pthread_setspecific( - key: ctypes::pthread_key_t, - value: *const c_void, -) -> c_int { - e(api::sys_pthread_setspecific(key, value)) -} diff --git a/ulib/ruxlibc/src/rand.rs b/ulib/ruxlibc/src/rand.rs deleted file mode 100644 index 07eefdd60..000000000 --- a/ulib/ruxlibc/src/rand.rs +++ /dev/null @@ -1,39 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use crate::ctypes::size_t; -use core::ffi::{c_int, c_long, c_uint, c_void}; - -use ruxos_posix_api::{sys_getrandom, sys_rand, sys_random, sys_srand}; - -use crate::utils::e; - -/// srand -#[no_mangle] -pub unsafe extern "C" fn srand(seed: c_uint) { - sys_srand(seed); -} - -/// rand -#[no_mangle] -pub unsafe extern "C" fn rand() -> c_int { - e(sys_rand() as c_int) -} - -/// random -#[no_mangle] -pub unsafe extern "C" fn random() -> c_long { - e(sys_random().try_into().unwrap()) as _ -} - -/// Get random -#[no_mangle] -pub unsafe extern "C" fn getrandom(buf: *mut c_void, buflen: size_t, flags: c_int) -> size_t { - e(sys_getrandom(buf, buflen, flags).try_into().unwrap()) as _ -} diff --git a/ulib/ruxlibc/src/resource.rs b/ulib/ruxlibc/src/resource.rs deleted file mode 100644 index 2f55ea5e9..000000000 --- a/ulib/ruxlibc/src/resource.rs +++ /dev/null @@ -1,29 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use core::ffi::c_int; - -use ruxos_posix_api::{sys_getrlimit, sys_setrlimit}; - -use crate::utils::e; - -/// Get resource limitations -#[no_mangle] -pub unsafe extern "C" fn getrlimit(resource: c_int, rlimits: *mut crate::ctypes::rlimit) -> c_int { - e(sys_getrlimit(resource, rlimits)) -} - -/// Set resource limitations -#[no_mangle] -pub unsafe extern "C" fn setrlimit( - resource: c_int, - rlimits: *const crate::ctypes::rlimit, -) -> c_int { - e(sys_setrlimit(resource, rlimits)) -} diff --git a/ulib/ruxlibc/src/setjmp.rs b/ulib/ruxlibc/src/setjmp.rs deleted file mode 100644 index bf310415c..000000000 --- a/ulib/ruxlibc/src/setjmp.rs +++ /dev/null @@ -1,247 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use core::ffi::c_int; - -use crate::ctypes; - -/// `setjmp` implementation -#[naked] -#[no_mangle] -pub unsafe extern "C" fn setjmp(_buf: *mut ctypes::__jmp_buf_tag) { - #[cfg(all(target_arch = "aarch64", feature = "fp_simd"))] - core::arch::asm!( - " - stp x19, x20, [x0,#0] - stp x21, x22, [x0,#16] - stp x23, x24, [x0,#32] - stp x25, x26, [x0,#48] - stp x27, x28, [x0,#64] - stp x29, x30, [x0,#80] - mov x2, sp - str x2, [x0,#104] - stp d8, d9, [x0,#112] - stp d10, d11, [x0,#128] - stp d12, d13, [x0,#144] - stp d14, d15, [x0,#160] - mov x0, #0 - ret", - options(noreturn), - ); - #[cfg(all(target_arch = "aarch64", not(feature = "fp_simd")))] - core::arch::asm!( - " - stp x19, x20, [x0,#0] - stp x21, x22, [x0,#16] - stp x23, x24, [x0,#32] - stp x25, x26, [x0,#48] - stp x27, x28, [x0,#64] - stp x29, x30, [x0,#80] - mov x2, sp - str x2, [x0,#104] - mov x0, #0 - ret", - options(noreturn), - ); - #[cfg(target_arch = "x86_64")] - core::arch::asm!( - "mov [rdi], rbx - mov [rdi + 8], rbp - mov [rdi + 16], r12 - mov [rdi + 24], r13 - mov [rdi + 32], r14 - mov [rdi + 40], r15 - lea rdx, [rsp + 8] - mov [rdi + 48], rdx - mov rdx, [rsp] - mov [rdi + 56], rdx - xor rax, rax - ret", - options(noreturn), - ); - #[cfg(all(target_arch = "riscv64", feature = "fp_simd"))] - core::arch::asm!( - "sd s0, 0(a0) - sd s1, 8(a0) - sd s2, 16(a0) - sd s3, 24(a0) - sd s4, 32(a0) - sd s5, 40(a0) - sd s6, 48(a0) - sd s7, 56(a0) - sd s8, 64(a0) - sd s9, 72(a0) - sd s10, 80(a0) - sd s11, 88(a0) - sd sp, 96(a0) - sd ra, 104(a0) - - fsd fs0, 112(a0) - fsd fs1, 120(a0) - fsd fs2, 128(a0) - fsd fs3, 136(a0) - fsd fs4, 144(a0) - fsd fs5, 152(a0) - fsd fs6, 160(a0) - fsd fs7, 168(a0) - fsd fs8, 176(a0) - fsd fs9, 184(a0) - fsd fs10, 192(a0) - fsd fs11, 200(a0) - - li a0, 0 - ret", - options(noreturn), - ); - #[cfg(all(target_arch = "riscv64", not(feature = "fp_simd")))] - core::arch::asm!( - "sd s0, 0(a0) - sd s1, 8(a0) - sd s2, 16(a0) - sd s3, 24(a0) - sd s4, 32(a0) - sd s5, 40(a0) - sd s6, 48(a0) - sd s7, 56(a0) - sd s8, 64(a0) - sd s9, 72(a0) - sd s10, 80(a0) - sd s11, 88(a0) - sd sp, 96(a0) - sd ra, 104(a0) - - li a0, 0 - ret", - options(noreturn), - ); - #[cfg(not(any( - target_arch = "aarch64", - target_arch = "x86_64", - target_arch = "riscv64" - )))] - core::arch::asm!("ret", options(noreturn)) -} - -/// `longjmp` implementation -#[naked] -#[no_mangle] -pub unsafe extern "C" fn longjmp(_buf: *mut ctypes::__jmp_buf_tag, _val: c_int) -> ! { - #[cfg(all(target_arch = "aarch64", feature = "fp_simd"))] - core::arch::asm!( - "ldp x19, x20, [x0,#0] - ldp x21, x22, [x0,#16] - ldp x23, x24, [x0,#32] - ldp x25, x26, [x0,#48] - ldp x27, x28, [x0,#64] - ldp x29, x30, [x0,#80] - ldr x2, [x0,#104] - mov sp, x2 - ldp d8 , d9, [x0,#112] - ldp d10, d11, [x0,#128] - ldp d12, d13, [x0,#144] - ldp d14, d15, [x0,#160] - - cmp w1, 0 - csinc w0, w1, wzr, ne - br x30", - options(noreturn), - ); - #[cfg(all(target_arch = "aarch64", not(feature = "fp_simd")))] - core::arch::asm!( - "ldp x19, x20, [x0,#0] - ldp x21, x22, [x0,#16] - ldp x23, x24, [x0,#32] - ldp x25, x26, [x0,#48] - ldp x27, x28, [x0,#64] - ldp x29, x30, [x0,#80] - ldr x2, [x0,#104] - mov sp, x2 - - cmp w1, 0 - csinc w0, w1, wzr, ne - br x30", - options(noreturn), - ); - #[cfg(target_arch = "x86_64")] - core::arch::asm!( - "mov rax,rsi - test rax,rax - jnz 1f - inc rax - 1: - mov rbx, [rdi] - mov rbp, [rdi + 8] - mov r12, [rdi + 16] - mov r13, [rdi + 24] - mov r14, [rdi + 32] - mov r15, [rdi + 40] - mov rdx, [rdi + 48] - mov rsp, rdx - mov rdx, [rdi + 56] - jmp rdx", - options(noreturn), - ); - #[cfg(all(target_arch = "riscv64", feature = "fp_simd"))] - core::arch::asm!( - "ld s0, 0(a0) - ld s1, 8(a0) - ld s2, 16(a0) - ld s3, 24(a0) - ld s4, 32(a0) - ld s5, 40(a0) - ld s6, 48(a0) - ld s7, 56(a0) - ld s8, 64(a0) - ld s9, 72(a0) - ld s10, 80(a0) - ld s11, 88(a0) - ld sp, 96(a0) - ld ra, 104(a0) - - fld fs0, 112(a0) - fld fs1, 120(a0) - fld fs2, 128(a0) - fld fs3, 136(a0) - fld fs4, 144(a0) - fld fs5, 152(a0) - fld fs6, 160(a0) - fld fs7, 168(a0) - fld fs8, 176(a0) - fld fs9, 184(a0) - fld fs10, 192(a0) - fld fs11, 200(a0) - - seqz a0, a1 - add a0, a0, a1 - ret", - options(noreturn), - ); - #[cfg(all(target_arch = "riscv64", not(feature = "fp_simd")))] - core::arch::asm!( - "ld s0, 0(a0) - ld s1, 8(a0) - ld s2, 16(a0) - ld s3, 24(a0) - ld s4, 32(a0) - ld s5, 40(a0) - ld s6, 48(a0) - ld s7, 56(a0) - ld s8, 64(a0) - ld s9, 72(a0) - ld s10, 80(a0) - ld s11, 88(a0) - ld sp, 96(a0) - ld ra, 104(a0) - - seqz a0, a1 - add a0, a0, a1 - ret", - options(noreturn), - ); -} diff --git a/ulib/ruxlibc/src/signal.rs b/ulib/ruxlibc/src/signal.rs deleted file mode 100644 index 4b3aec0bc..000000000 --- a/ulib/ruxlibc/src/signal.rs +++ /dev/null @@ -1,79 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use core::ffi::c_int; - -#[cfg(feature = "signal")] -use crate::ctypes::k_sigaction; -use crate::ctypes::{sigaction, EINVAL, SIGKILL, SIGSTOP}; -#[cfg(feature = "signal")] -use ruxos_posix_api::sys_sigaction; - -#[cfg(feature = "signal")] -unsafe extern "C" fn ignore_handler(_: c_int) {} - -#[no_mangle] -pub unsafe extern "C" fn sigaction_inner( - signum: c_int, - _act: *const sigaction, - oldact: *mut sigaction, -) -> c_int { - if signum >= 32 || signum == SIGKILL as _ || signum == SIGSTOP as _ { - return -(EINVAL as c_int); - } - #[cfg(feature = "signal")] - { - let k_act = { - if _act.is_null() { - None - } else { - let mut sh = (*_act).__sa_handler.sa_handler; - if let Some(h) = sh { - if h as usize == crate::ctypes::SIGIGN as usize { - sh = Some(ignore_handler as unsafe extern "C" fn(c_int)); - } - } - k_sigaction { - handler: sh, - flags: (*_act).sa_flags as _, - restorer: (*_act).sa_restorer, - mask: Default::default(), - } - } - }; - let mut k_oldact = k_sigaction::default(); - sys_sigaction( - signum as u8, - Some(&k_act), - if oldact.is_null() { - None - } else { - Some(&mut k_oldact) - }, - ); - if !oldact.is_null() { - (*oldact).__sa_handler.sa_handler = k_oldact.handler; - (*oldact).sa_flags = k_oldact.flags as _; - (*oldact).sa_restorer = k_oldact.restorer; - // Not support mask - // (*oldact).sa_mask = k_oldact.mask; - } - } - #[cfg(not(feature = "signal"))] - { - if !oldact.is_null() { - // set to 0 - (*oldact).__sa_handler.sa_handler = None; - (*oldact).sa_flags = 0; - (*oldact).sa_restorer = None; - (*oldact).sa_mask = Default::default(); - } - } - 0 -} diff --git a/ulib/ruxlibc/src/strftime.rs b/ulib/ruxlibc/src/strftime.rs deleted file mode 100644 index c839b5edf..000000000 --- a/ulib/ruxlibc/src/strftime.rs +++ /dev/null @@ -1,263 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use alloc::string::String; -use core::{ffi::c_char, fmt}; - -use axio::Write; - -use crate::ctypes; - -pub trait WriteByte: fmt::Write { - fn write_u8(&mut self, byte: u8) -> fmt::Result; -} - -struct StringWriter(pub *mut u8, pub usize); - -impl Write for StringWriter { - fn write(&mut self, buf: &[u8]) -> axerrno::AxResult { - if self.1 > 1 { - let copy_size = buf.len().min(self.1 - 1); - unsafe { - core::ptr::copy_nonoverlapping(buf.as_ptr(), self.0, copy_size); - self.1 -= copy_size; - - self.0 = self.0.add(copy_size); - *self.0 = 0; - } - } - Ok(buf.len()) - } - fn flush(&mut self) -> axerrno::AxResult { - Ok(()) - } -} - -impl fmt::Write for StringWriter { - fn write_str(&mut self, s: &str) -> fmt::Result { - // can't fail - self.write(s.as_bytes()).unwrap(); - Ok(()) - } -} - -impl WriteByte for StringWriter { - fn write_u8(&mut self, byte: u8) -> fmt::Result { - // can't fail - self.write(&[byte]).unwrap(); - Ok(()) - } -} - -struct CountingWriter { - pub inner: T, - pub written: usize, -} - -impl CountingWriter { - pub fn new(writer: T) -> Self { - Self { - inner: writer, - written: 0, - } - } -} - -impl fmt::Write for CountingWriter { - fn write_str(&mut self, s: &str) -> fmt::Result { - self.written += s.len(); - self.inner.write_str(s) - } -} - -impl WriteByte for CountingWriter { - fn write_u8(&mut self, byte: u8) -> fmt::Result { - self.written += 1; - self.inner.write_u8(byte) - } -} - -impl Write for CountingWriter { - fn write(&mut self, buf: &[u8]) -> axerrno::AxResult { - let res = self.inner.write(buf); - if let Ok(written) = res { - self.written += written; - } - res - } - - fn write_all(&mut self, buf: &[u8]) -> axerrno::AxResult { - match self.inner.write_all(buf) { - Ok(()) => (), - Err(err) => return Err(err), - } - self.written += buf.len(); - Ok(()) - } - - fn flush(&mut self) -> axerrno::AxResult { - self.inner.flush() - } -} - -unsafe fn strftime_inner( - w: W, - format: *const c_char, - t: *const ctypes::tm, -) -> ctypes::size_t { - pub unsafe fn inner_strftime( - w: &mut W, - mut format: *const c_char, - t: *const ctypes::tm, - ) -> bool { - macro_rules! w { - (byte $b:expr) => {{ - if w.write_u8($b).is_err() { - return false; - } - }}; - (char $chr:expr) => {{ - if w.write_char($chr).is_err() { - return false; - } - }}; - (recurse $fmt:expr) => {{ - let mut fmt = String::with_capacity($fmt.len() + 1); - fmt.push_str($fmt); - fmt.push('\0'); - - if !inner_strftime(w, fmt.as_ptr() as *mut c_char, t) { - return false; - } - }}; - ($str:expr) => {{ - if w.write_str($str).is_err() { - return false; - } - }}; - ($fmt:expr, $($args:expr),+) => {{ - // Would use write!() if I could get the length written - if write!(w, $fmt, $($args),+).is_err() { - return false; - } - }}; - } - const WDAYS: [&str; 7] = [ - "Sunday", - "Monday", - "Tuesday", - "Wednesday", - "Thursday", - "Friday", - "Saturday", - ]; - const MONTHS: [&str; 12] = [ - "January", - "Febuary", - "March", - "April", - "May", - "June", - "July", - "August", - "September", - "October", - "November", - "December", - ]; - - while *format != 0 { - if *format as u8 != b'%' { - w!(byte * format as u8); - format = format.offset(1); - continue; - } - - format = format.offset(1); - - if *format as u8 == b'E' || *format as u8 == b'O' { - // Ignore because these do nothing without locale - format = format.offset(1); - } - - match *format as u8 { - b'%' => w!(byte b'%'), - b'n' => w!(byte b'\n'), - b't' => w!(byte b'\t'), - b'a' => w!(&WDAYS[(*t).tm_wday as usize][..3]), - b'A' => w!(WDAYS[(*t).tm_wday as usize]), - b'b' | b'h' => w!(&MONTHS[(*t).tm_mon as usize][..3]), - b'B' => w!(MONTHS[(*t).tm_mon as usize]), - b'C' => { - let mut year = (*t).tm_year / 100; - // Round up - if (*t).tm_year % 100 != 0 { - year += 1; - } - w!("{:02}", year + 19); - } - b'd' => w!("{:02}", (*t).tm_mday), - b'D' => w!(recurse "%m/%d/%y"), - b'e' => w!("{:2}", (*t).tm_mday), - b'F' => w!(recurse "%Y-%m-%d"), - b'H' => w!("{:02}", (*t).tm_hour), - b'I' => w!("{:02}", ((*t).tm_hour + 12 - 1) % 12 + 1), - b'j' => w!("{:03}", (*t).tm_yday), - b'k' => w!("{:2}", (*t).tm_hour), - b'l' => w!("{:2}", ((*t).tm_hour + 12 - 1) % 12 + 1), - b'm' => w!("{:02}", (*t).tm_mon + 1), - b'M' => w!("{:02}", (*t).tm_min), - b'p' => w!(if (*t).tm_hour < 12 { "AM" } else { "PM" }), - b'P' => w!(if (*t).tm_hour < 12 { "am" } else { "pm" }), - b'r' => w!(recurse "%I:%M:%S %p"), - b'R' => w!(recurse "%H:%M"), - // Nothing is modified in mktime, but the C standard of course requires a mutable pointer ._. - b's' => w!("{}", super::mktime(t as *mut ctypes::tm)), - b'S' => w!("{:02}", (*t).tm_sec), - b'T' => w!(recurse "%H:%M:%S"), - b'u' => w!("{}", ((*t).tm_wday + 7 - 1) % 7 + 1), - b'U' => w!("{}", ((*t).tm_yday + 7 - (*t).tm_wday) / 7), - b'w' => w!("{}", (*t).tm_wday), - b'W' => w!("{}", ((*t).tm_yday + 7 - ((*t).tm_wday + 6) % 7) / 7), - b'y' => w!("{:02}", (*t).tm_year % 100), - b'Y' => w!("{}", (*t).tm_year + 1900), - b'z' => w!("+0000"), // TODO - b'Z' => w!("UTC"), // TODO - b'+' => w!(recurse "%a %b %d %T %Z %Y"), - _ => return false, - } - - format = format.offset(1); - } - true - } - - let mut w: CountingWriter = CountingWriter::new(w); - if !inner_strftime(&mut w, format, t) { - return 0; - } - - w.written -} - -/// Convert date and time to a string. -#[no_mangle] -pub unsafe extern "C" fn strftime( - buf: *mut c_char, - size: ctypes::size_t, - format: *const c_char, - timeptr: *const ctypes::tm, -) -> ctypes::size_t { - let ret = strftime_inner(StringWriter(buf as *mut u8, size), format, timeptr); - if ret < size { - ret - } else { - 0 - } -} diff --git a/ulib/ruxlibc/src/string.rs b/ulib/ruxlibc/src/string.rs deleted file mode 100644 index 5cbbc7740..000000000 --- a/ulib/ruxlibc/src/string.rs +++ /dev/null @@ -1,29 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use crate::ctypes; -use core::ffi::c_char; -/// calculate the length of a string, excluding the terminating null byte -#[no_mangle] -pub unsafe extern "C" fn strlen(s: *const c_char) -> ctypes::size_t { - strnlen(s, ctypes::size_t::MAX) -} - -/// calculate the length of a string like strlen, but at most maxlen. -#[no_mangle] -pub unsafe extern "C" fn strnlen(s: *const c_char, size: ctypes::size_t) -> ctypes::size_t { - let mut i = 0; - while i < size { - if *s.add(i) == 0 { - break; - } - i += 1; - } - i -} diff --git a/ulib/ruxlibc/src/strtod.rs b/ulib/ruxlibc/src/strtod.rs deleted file mode 100644 index 2e1c3db41..000000000 --- a/ulib/ruxlibc/src/strtod.rs +++ /dev/null @@ -1,140 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use core::ffi::{c_char, c_double, c_float, c_int}; - -macro_rules! strto_float_impl { - ($type:ident, $s:expr, $endptr:expr) => {{ - let mut s = $s; - let endptr = $endptr; - - // TODO: Handle named floats: NaN, Inf... - - while isspace(*s as c_int) { - s = s.offset(1); - } - - let mut result: $type = 0.0; - let mut radix = 10; - - let result_sign = match *s as u8 { - b'-' => { - s = s.offset(1); - -1.0 - } - b'+' => { - s = s.offset(1); - 1.0 - } - _ => 1.0, - }; - - if *s as u8 == b'0' && *s.offset(1) as u8 == b'x' { - s = s.offset(2); - radix = 16; - } - - while let Some(digit) = (*s as u8 as char).to_digit(radix) { - result *= radix as $type; - result += digit as $type; - s = s.offset(1); - } - - if *s as u8 == b'.' { - s = s.offset(1); - - let mut i = 1.0; - while let Some(digit) = (*s as u8 as char).to_digit(radix) { - i *= radix as $type; - result += digit as $type / i; - s = s.offset(1); - } - } - - let s_before_exponent = s; - - let exponent = match (*s as u8, radix) { - (b'e' | b'E', 10) | (b'p' | b'P', 16) => { - s = s.offset(1); - - let is_exponent_positive = match *s as u8 { - b'-' => { - s = s.offset(1); - false - } - b'+' => { - s = s.offset(1); - true - } - _ => true, - }; - - // Exponent digits are always in base 10. - if (*s as u8 as char).is_digit(10) { - let mut exponent_value = 0; - - while let Some(digit) = (*s as u8 as char).to_digit(10) { - exponent_value *= 10; - exponent_value += digit; - s = s.offset(1); - } - - let exponent_base = match radix { - 10 => 10u128, - 16 => 2u128, - _ => unreachable!(), - }; - - if is_exponent_positive { - Some(exponent_base.pow(exponent_value) as $type) - } else { - Some(1.0 / (exponent_base.pow(exponent_value) as $type)) - } - } else { - // Exponent had no valid digits after 'e'/'p' and '+'/'-', rollback - s = s_before_exponent; - None - } - } - _ => None, - }; - - // Return pointer should be *mut - if !endptr.is_null() { - *endptr = s as *mut _; - } - - if let Some(exponent) = exponent { - result_sign * result * exponent - } else { - result_sign * result - } - }}; -} - -fn isspace(c: c_int) -> bool { - c == c_int::from(b' ') - || c == c_int::from(b'\t') - || c == c_int::from(b'\n') - || c == c_int::from(b'\r') - || c == 0x0b - || c == 0x0c -} - -/// Convert a string to a double-precision number. -#[no_mangle] -pub unsafe extern "C" fn strtod(s: *const c_char, endptr: *mut *mut c_char) -> c_double { - strto_float_impl!(c_double, s, endptr) -} - -/// Convert a string to a float number. -#[no_mangle] -pub unsafe extern "C" fn strtof(s: *const c_char, endptr: *mut *mut c_char) -> c_float { - strto_float_impl!(c_float, s, endptr) -} diff --git a/ulib/ruxlibc/src/sys.rs b/ulib/ruxlibc/src/sys.rs deleted file mode 100644 index 1ec89e423..000000000 --- a/ulib/ruxlibc/src/sys.rs +++ /dev/null @@ -1,49 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use crate::ctypes; -use core::ffi::{c_int, c_long}; -use ruxos_posix_api::{config, sys_getrlimit}; - -/// Return system configuration infomation -/// -/// Notice: currently only support what unikraft covers -#[no_mangle] -pub unsafe extern "C" fn sysconf(name: c_int) -> c_long { - match name as u32 { - // Maximum process number - ctypes::_SC_CHILD_MAX => { - let mut rl: ctypes::rlimit = core::mem::zeroed(); - sys_getrlimit(ctypes::RLIMIT_NPROC.try_into().unwrap(), &mut rl); - rl.rlim_max as c_long - } - // Page size - ctypes::_SC_PAGE_SIZE => config::PAGE_SIZE_4K as c_long, - // Total physical pages - ctypes::_SC_PHYS_PAGES => (config::PHYS_MEMORY_SIZE / config::PAGE_SIZE_4K) as c_long, - // Number of processors in use - ctypes::_SC_NPROCESSORS_ONLN => config::SMP as c_long, - // Avaliable physical pages - ctypes::_SC_AVPHYS_PAGES => { - let mut info: ctypes::sysinfo = core::mem::zeroed(); - ruxos_posix_api::sys_sysinfo(&mut info); - (info.freeram / config::PAGE_SIZE_4K as u64) as c_long - } - // Maximum number of files per process - #[cfg(feature = "fd")] - ctypes::_SC_OPEN_MAX => { - let mut rl: ctypes::rlimit = core::mem::zeroed(); - sys_getrlimit(ctypes::RLIMIT_NOFILE.try_into().unwrap(), &mut rl); - rl.rlim_max as c_long - } - // Maximum number of keys per thread - ctypes::_SC_THREAD_KEYS_MAX => config::PTHREAD_KEY_MAX as c_long, - _ => 0, - } -} diff --git a/ulib/ruxlibc/src/time.rs b/ulib/ruxlibc/src/time.rs deleted file mode 100644 index ff26e5ace..000000000 --- a/ulib/ruxlibc/src/time.rs +++ /dev/null @@ -1,87 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use core::ffi::c_int; -use ruxos_posix_api::{sys_clock_gettime, sys_clock_nanosleep, sys_clock_settime, sys_nanosleep}; -#[cfg(feature = "signal")] -use ruxos_posix_api::{sys_getitimer, sys_setitimer}; - -use crate::{ctypes, utils::e}; - -/// Get clock time since booting -#[no_mangle] -pub unsafe extern "C" fn clock_gettime(clk: ctypes::clockid_t, ts: *mut ctypes::timespec) -> c_int { - e(sys_clock_gettime(clk, ts)) -} - -/// Set clock time since booting -#[no_mangle] -pub unsafe extern "C" fn clock_settime(clk: ctypes::clockid_t, ts: *mut ctypes::timespec) -> c_int { - e(sys_clock_settime(clk, ts)) -} - -/// Sleep until some nanoseconds -/// -/// TODO: should be woken by signals, and set errno -#[no_mangle] -pub unsafe extern "C" fn clock_nanosleep( - which_clock: ctypes::clockid_t, - flags: c_int, - req: *const ctypes::timespec, - rem: *mut ctypes::timespec, -) -> c_int { - e(sys_clock_nanosleep(which_clock, flags, req, rem)) -} - -/// Sleep some nanoseconds -/// -/// TODO: should be woken by signals, and set errno -#[no_mangle] -pub unsafe extern "C" fn nanosleep( - req: *const ctypes::timespec, - rem: *mut ctypes::timespec, -) -> c_int { - e(sys_nanosleep(req, rem)) -} - -/// Set timer to send signal after some time -#[no_mangle] -pub unsafe extern "C" fn setitimer( - _which: c_int, - _new: *const ctypes::itimerval, - _old: *mut ctypes::itimerval, -) -> c_int { - #[cfg(feature = "signal")] - { - if !_old.is_null() { - let res = e(sys_getitimer(_which, _old)); - if res != 0 { - return res; - } - } - e(sys_setitimer(_which, _new)) - } - #[cfg(not(feature = "signal"))] - { - e(0) - } -} - -/// Set timer to send signal after some time -#[no_mangle] -pub unsafe extern "C" fn getitimer(_which: c_int, _curr_value: *mut ctypes::itimerval) -> c_int { - #[cfg(feature = "signal")] - { - e(sys_getitimer(_which, _curr_value)) - } - #[cfg(not(feature = "signal"))] - { - e(0) - } -} diff --git a/ulib/ruxlibc/src/unistd.rs b/ulib/ruxlibc/src/unistd.rs deleted file mode 100644 index 51d53625f..000000000 --- a/ulib/ruxlibc/src/unistd.rs +++ /dev/null @@ -1,87 +0,0 @@ -/* Copyright (c) [2023] [Syswonder Community] - * [Ruxos] is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -use core::ffi::c_int; -use ruxos_posix_api::{sys_exit, sys_getpid, sys_gettid}; -#[cfg(feature = "signal")] -use { - crate::getitimer, - crate::{ctypes, utils::e}, - core::ffi::c_uint, - ruxos_posix_api::sys_setitimer, -}; - -/// Get current thread ID. -#[no_mangle] -pub unsafe extern "C" fn getpid() -> c_int { - sys_getpid() -} - -/// Get current thread ID. -#[no_mangle] -pub unsafe extern "C" fn gettid() -> c_int { - sys_gettid() -} - -/// Abort the current process. -#[no_mangle] -pub unsafe extern "C" fn abort() -> ! { - panic!() -} - -/// Exits the current thread. -#[no_mangle] -pub unsafe extern "C" fn exit(exit_code: c_int) -> ! { - sys_exit(exit_code) -} - -/// Set an alarm clock for delivery of a signal -#[cfg(feature = "signal")] -#[no_mangle] -pub unsafe extern "C" fn alarm(seconds: c_uint) -> c_uint { - let it = ctypes::itimerval { - it_interval: ctypes::timeval { - tv_sec: 0, - tv_usec: 0, - }, - it_value: ctypes::timeval { - tv_sec: seconds as i64, - tv_usec: 0, - }, - }; - let mut old = ctypes::itimerval::default(); - if getitimer(ctypes::ITIMER_REAL as c_int, &mut old) < 0 { - e(sys_setitimer(ctypes::ITIMER_REAL as c_int, &it)) as c_uint - } else { - old.it_value.tv_sec as c_uint - } -} - -/// Schedule signal after given number of microseconds -#[cfg(feature = "signal")] -#[no_mangle] -pub unsafe extern "C" fn ualarm(useconds: c_uint, interval: c_uint) -> c_uint { - let it = ctypes::itimerval { - it_interval: ctypes::timeval { - tv_sec: 0, - tv_usec: interval as i64, - }, - it_value: ctypes::timeval { - tv_sec: 0, - tv_usec: useconds as i64, - }, - }; - let mut old = ctypes::itimerval::default(); - if getitimer(ctypes::ITIMER_REAL as i32, &mut old) < 0 { - e(sys_setitimer(ctypes::ITIMER_REAL as i32, &it)); - 0 - } else { - core::time::Duration::from(old.it_value).as_micros() as c_uint - } -} diff --git a/ulib/ruxmusl/src/aarch64/mod.rs b/ulib/ruxmusl/src/aarch64/mod.rs index 51fc08f11..b8553a6ef 100644 --- a/ulib/ruxmusl/src/aarch64/mod.rs +++ b/ulib/ruxmusl/src/aarch64/mod.rs @@ -1,3 +1,5 @@ +#![allow(unused_imports)] + pub mod syscall_id; use core::ffi::{c_char, c_int}; @@ -201,6 +203,7 @@ pub fn syscall(syscall_id: SyscallId, args: [usize; 6]) -> isize { SyscallId::EXIT => { ruxos_posix_api::sys_pthread_exit(args[0] as *mut core::ffi::c_void) as _ } + #[cfg(feature = "multitask")] SyscallId::EXIT_GROUP => ruxos_posix_api::sys_exit_group(args[0] as c_int), #[cfg(feature = "multitask")] SyscallId::SET_TID_ADDRESS => ruxos_posix_api::sys_set_tid_address(args[0]) as _, @@ -415,6 +418,7 @@ pub fn syscall(syscall_id: SyscallId, args: [usize; 6]) -> isize { args[1] as ctypes::size_t, args[2] as c_int, ) as _, + #[cfg(feature = "multitask")] SyscallId::WAIT4 => ruxos_posix_api::sys_wait4( args[0] as ctypes::pid_t, args[1] as *mut c_int, diff --git a/ulib/ruxmusl/src/aarch64/syscall_id.rs b/ulib/ruxmusl/src/aarch64/syscall_id.rs index b9b9b18dc..237cf88c2 100644 --- a/ulib/ruxmusl/src/aarch64/syscall_id.rs +++ b/ulib/ruxmusl/src/aarch64/syscall_id.rs @@ -73,6 +73,7 @@ pub enum SyscallId { FDATASYNC = 83, CAP_GET = 90, EXIT = 93, + #[cfg(feature = "multitask")] EXIT_GROUP = 94, #[cfg(feature = "multitask")] SET_TID_ADDRESS = 96, @@ -153,6 +154,7 @@ pub enum SyscallId { MSYNC = 227, #[cfg(feature = "alloc")] MADVISE = 233, + #[cfg(feature = "multitask")] WAIT4 = 260, PRLIMIT64 = 261, GETRANDOM = 278, diff --git a/ulib/ruxmusl/src/trap.rs b/ulib/ruxmusl/src/trap.rs index 17fb9aecd..c0cf757f4 100644 --- a/ulib/ruxmusl/src/trap.rs +++ b/ulib/ruxmusl/src/trap.rs @@ -21,6 +21,9 @@ impl ruxhal::trap::TrapHandler for TrapHandlerImpl { #[cfg(feature = "musl")] fn handle_syscall(syscall_id: usize, args: [usize; 6]) -> isize { let id = SyscallId::try_from(syscall_id).unwrap_or(SyscallId::INVALID); + if id == SyscallId::INVALID { + info!("Invalid syscall id: {}", syscall_id); + } crate::syscall(id, args) } }