18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: LGPL-2.1 OR MIT */ 28c2ecf20Sopenharmony_ci/* nolibc.h 38c2ecf20Sopenharmony_ci * Copyright (C) 2017-2018 Willy Tarreau <w@1wt.eu> 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci/* 78c2ecf20Sopenharmony_ci * This file is designed to be used as a libc alternative for minimal programs 88c2ecf20Sopenharmony_ci * with very limited requirements. It consists of a small number of syscall and 98c2ecf20Sopenharmony_ci * type definitions, and the minimal startup code needed to call main(). 108c2ecf20Sopenharmony_ci * All syscalls are declared as static functions so that they can be optimized 118c2ecf20Sopenharmony_ci * away by the compiler when not used. 128c2ecf20Sopenharmony_ci * 138c2ecf20Sopenharmony_ci * Syscalls are split into 3 levels: 148c2ecf20Sopenharmony_ci * - The lower level is the arch-specific syscall() definition, consisting in 158c2ecf20Sopenharmony_ci * assembly code in compound expressions. These are called my_syscall0() to 168c2ecf20Sopenharmony_ci * my_syscall6() depending on the number of arguments. The MIPS 178c2ecf20Sopenharmony_ci * implementation is limited to 5 arguments. All input arguments are cast 188c2ecf20Sopenharmony_ci * to a long stored in a register. These expressions always return the 198c2ecf20Sopenharmony_ci * syscall's return value as a signed long value which is often either a 208c2ecf20Sopenharmony_ci * pointer or the negated errno value. 218c2ecf20Sopenharmony_ci * 228c2ecf20Sopenharmony_ci * - The second level is mostly architecture-independent. It is made of 238c2ecf20Sopenharmony_ci * static functions called sys_<name>() which rely on my_syscallN() 248c2ecf20Sopenharmony_ci * depending on the syscall definition. These functions are responsible 258c2ecf20Sopenharmony_ci * for exposing the appropriate types for the syscall arguments (int, 268c2ecf20Sopenharmony_ci * pointers, etc) and for setting the appropriate return type (often int). 278c2ecf20Sopenharmony_ci * A few of them are architecture-specific because the syscalls are not all 288c2ecf20Sopenharmony_ci * mapped exactly the same among architectures. For example, some archs do 298c2ecf20Sopenharmony_ci * not implement select() and need pselect6() instead, so the sys_select() 308c2ecf20Sopenharmony_ci * function will have to abstract this. 318c2ecf20Sopenharmony_ci * 328c2ecf20Sopenharmony_ci * - The third level is the libc call definition. It exposes the lower raw 338c2ecf20Sopenharmony_ci * sys_<name>() calls in a way that looks like what a libc usually does, 348c2ecf20Sopenharmony_ci * takes care of specific input values, and of setting errno upon error. 358c2ecf20Sopenharmony_ci * There can be minor variations compared to standard libc calls. For 368c2ecf20Sopenharmony_ci * example the open() call always takes 3 args here. 378c2ecf20Sopenharmony_ci * 388c2ecf20Sopenharmony_ci * The errno variable is declared static and unused. This way it can be 398c2ecf20Sopenharmony_ci * optimized away if not used. However this means that a program made of 408c2ecf20Sopenharmony_ci * multiple C files may observe different errno values (one per C file). For 418c2ecf20Sopenharmony_ci * the type of programs this project targets it usually is not a problem. The 428c2ecf20Sopenharmony_ci * resulting program may even be reduced by defining the NOLIBC_IGNORE_ERRNO 438c2ecf20Sopenharmony_ci * macro, in which case the errno value will never be assigned. 448c2ecf20Sopenharmony_ci * 458c2ecf20Sopenharmony_ci * Some stdint-like integer types are defined. These are valid on all currently 468c2ecf20Sopenharmony_ci * supported architectures, because signs are enforced, ints are assumed to be 478c2ecf20Sopenharmony_ci * 32 bits, longs the size of a pointer and long long 64 bits. If more 488c2ecf20Sopenharmony_ci * architectures have to be supported, this may need to be adapted. 498c2ecf20Sopenharmony_ci * 508c2ecf20Sopenharmony_ci * Some macro definitions like the O_* values passed to open(), and some 518c2ecf20Sopenharmony_ci * structures like the sys_stat struct depend on the architecture. 528c2ecf20Sopenharmony_ci * 538c2ecf20Sopenharmony_ci * The definitions start with the architecture-specific parts, which are picked 548c2ecf20Sopenharmony_ci * based on what the compiler knows about the target architecture, and are 558c2ecf20Sopenharmony_ci * completed with the generic code. Since it is the compiler which sets the 568c2ecf20Sopenharmony_ci * target architecture, cross-compiling normally works out of the box without 578c2ecf20Sopenharmony_ci * having to specify anything. 588c2ecf20Sopenharmony_ci * 598c2ecf20Sopenharmony_ci * Finally some very common libc-level functions are provided. It is the case 608c2ecf20Sopenharmony_ci * for a few functions usually found in string.h, ctype.h, or stdlib.h. Nothing 618c2ecf20Sopenharmony_ci * is currently provided regarding stdio emulation. 628c2ecf20Sopenharmony_ci * 638c2ecf20Sopenharmony_ci * The macro NOLIBC is always defined, so that it is possible for a program to 648c2ecf20Sopenharmony_ci * check this macro to know if it is being built against and decide to disable 658c2ecf20Sopenharmony_ci * some features or simply not to include some standard libc files. 668c2ecf20Sopenharmony_ci * 678c2ecf20Sopenharmony_ci * Ideally this file should be split in multiple files for easier long term 688c2ecf20Sopenharmony_ci * maintenance, but provided as a single file as it is now, it's quite 698c2ecf20Sopenharmony_ci * convenient to use. Maybe some variations involving a set of includes at the 708c2ecf20Sopenharmony_ci * top could work. 718c2ecf20Sopenharmony_ci * 728c2ecf20Sopenharmony_ci * A simple static executable may be built this way : 738c2ecf20Sopenharmony_ci * $ gcc -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \ 748c2ecf20Sopenharmony_ci * -static -include nolibc.h -lgcc -o hello hello.c 758c2ecf20Sopenharmony_ci * 768c2ecf20Sopenharmony_ci * A very useful calling convention table may be found here : 778c2ecf20Sopenharmony_ci * http://man7.org/linux/man-pages/man2/syscall.2.html 788c2ecf20Sopenharmony_ci * 798c2ecf20Sopenharmony_ci * This doc is quite convenient though not necessarily up to date : 808c2ecf20Sopenharmony_ci * https://w3challs.com/syscalls/ 818c2ecf20Sopenharmony_ci * 828c2ecf20Sopenharmony_ci */ 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci/* Some archs (at least aarch64) don't expose the regular syscalls anymore by 858c2ecf20Sopenharmony_ci * default, either because they have an "_at" replacement, or because there are 868c2ecf20Sopenharmony_ci * more modern alternatives. For now we'd rather still use them. 878c2ecf20Sopenharmony_ci */ 888c2ecf20Sopenharmony_ci#define __ARCH_WANT_SYSCALL_NO_AT 898c2ecf20Sopenharmony_ci#define __ARCH_WANT_SYSCALL_NO_FLAGS 908c2ecf20Sopenharmony_ci#define __ARCH_WANT_SYSCALL_DEPRECATED 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci#include <asm/unistd.h> 938c2ecf20Sopenharmony_ci#include <asm/ioctls.h> 948c2ecf20Sopenharmony_ci#include <asm/errno.h> 958c2ecf20Sopenharmony_ci#include <linux/fs.h> 968c2ecf20Sopenharmony_ci#include <linux/loop.h> 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci#define NOLIBC 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci/* this way it will be removed if unused */ 1018c2ecf20Sopenharmony_cistatic int errno; 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci#ifndef NOLIBC_IGNORE_ERRNO 1048c2ecf20Sopenharmony_ci#define SET_ERRNO(v) do { errno = (v); } while (0) 1058c2ecf20Sopenharmony_ci#else 1068c2ecf20Sopenharmony_ci#define SET_ERRNO(v) do { } while (0) 1078c2ecf20Sopenharmony_ci#endif 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci/* errno codes all ensure that they will not conflict with a valid pointer 1108c2ecf20Sopenharmony_ci * because they all correspond to the highest addressable memry page. 1118c2ecf20Sopenharmony_ci */ 1128c2ecf20Sopenharmony_ci#define MAX_ERRNO 4095 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci/* Declare a few quite common macros and types that usually are in stdlib.h, 1158c2ecf20Sopenharmony_ci * stdint.h, ctype.h, unistd.h and a few other common locations. 1168c2ecf20Sopenharmony_ci */ 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci#define NULL ((void *)0) 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci/* stdint types */ 1218c2ecf20Sopenharmony_citypedef unsigned char uint8_t; 1228c2ecf20Sopenharmony_citypedef signed char int8_t; 1238c2ecf20Sopenharmony_citypedef unsigned short uint16_t; 1248c2ecf20Sopenharmony_citypedef signed short int16_t; 1258c2ecf20Sopenharmony_citypedef unsigned int uint32_t; 1268c2ecf20Sopenharmony_citypedef signed int int32_t; 1278c2ecf20Sopenharmony_citypedef unsigned long long uint64_t; 1288c2ecf20Sopenharmony_citypedef signed long long int64_t; 1298c2ecf20Sopenharmony_citypedef unsigned long size_t; 1308c2ecf20Sopenharmony_citypedef signed long ssize_t; 1318c2ecf20Sopenharmony_citypedef unsigned long uintptr_t; 1328c2ecf20Sopenharmony_citypedef signed long intptr_t; 1338c2ecf20Sopenharmony_citypedef signed long ptrdiff_t; 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci/* for stat() */ 1368c2ecf20Sopenharmony_citypedef unsigned int dev_t; 1378c2ecf20Sopenharmony_citypedef unsigned long ino_t; 1388c2ecf20Sopenharmony_citypedef unsigned int mode_t; 1398c2ecf20Sopenharmony_citypedef signed int pid_t; 1408c2ecf20Sopenharmony_citypedef unsigned int uid_t; 1418c2ecf20Sopenharmony_citypedef unsigned int gid_t; 1428c2ecf20Sopenharmony_citypedef unsigned long nlink_t; 1438c2ecf20Sopenharmony_citypedef signed long off_t; 1448c2ecf20Sopenharmony_citypedef signed long blksize_t; 1458c2ecf20Sopenharmony_citypedef signed long blkcnt_t; 1468c2ecf20Sopenharmony_citypedef signed long time_t; 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci/* for poll() */ 1498c2ecf20Sopenharmony_cistruct pollfd { 1508c2ecf20Sopenharmony_ci int fd; 1518c2ecf20Sopenharmony_ci short int events; 1528c2ecf20Sopenharmony_ci short int revents; 1538c2ecf20Sopenharmony_ci}; 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci/* for select() */ 1568c2ecf20Sopenharmony_cistruct timeval { 1578c2ecf20Sopenharmony_ci long tv_sec; 1588c2ecf20Sopenharmony_ci long tv_usec; 1598c2ecf20Sopenharmony_ci}; 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci/* for pselect() */ 1628c2ecf20Sopenharmony_cistruct timespec { 1638c2ecf20Sopenharmony_ci long tv_sec; 1648c2ecf20Sopenharmony_ci long tv_nsec; 1658c2ecf20Sopenharmony_ci}; 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci/* for gettimeofday() */ 1688c2ecf20Sopenharmony_cistruct timezone { 1698c2ecf20Sopenharmony_ci int tz_minuteswest; 1708c2ecf20Sopenharmony_ci int tz_dsttime; 1718c2ecf20Sopenharmony_ci}; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci/* for getdents64() */ 1748c2ecf20Sopenharmony_cistruct linux_dirent64 { 1758c2ecf20Sopenharmony_ci uint64_t d_ino; 1768c2ecf20Sopenharmony_ci int64_t d_off; 1778c2ecf20Sopenharmony_ci unsigned short d_reclen; 1788c2ecf20Sopenharmony_ci unsigned char d_type; 1798c2ecf20Sopenharmony_ci char d_name[]; 1808c2ecf20Sopenharmony_ci}; 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci/* commonly an fd_set represents 256 FDs */ 1838c2ecf20Sopenharmony_ci#define FD_SETSIZE 256 1848c2ecf20Sopenharmony_citypedef struct { uint32_t fd32[FD_SETSIZE/32]; } fd_set; 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci/* needed by wait4() */ 1878c2ecf20Sopenharmony_cistruct rusage { 1888c2ecf20Sopenharmony_ci struct timeval ru_utime; 1898c2ecf20Sopenharmony_ci struct timeval ru_stime; 1908c2ecf20Sopenharmony_ci long ru_maxrss; 1918c2ecf20Sopenharmony_ci long ru_ixrss; 1928c2ecf20Sopenharmony_ci long ru_idrss; 1938c2ecf20Sopenharmony_ci long ru_isrss; 1948c2ecf20Sopenharmony_ci long ru_minflt; 1958c2ecf20Sopenharmony_ci long ru_majflt; 1968c2ecf20Sopenharmony_ci long ru_nswap; 1978c2ecf20Sopenharmony_ci long ru_inblock; 1988c2ecf20Sopenharmony_ci long ru_oublock; 1998c2ecf20Sopenharmony_ci long ru_msgsnd; 2008c2ecf20Sopenharmony_ci long ru_msgrcv; 2018c2ecf20Sopenharmony_ci long ru_nsignals; 2028c2ecf20Sopenharmony_ci long ru_nvcsw; 2038c2ecf20Sopenharmony_ci long ru_nivcsw; 2048c2ecf20Sopenharmony_ci}; 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci/* stat flags (WARNING, octal here) */ 2078c2ecf20Sopenharmony_ci#define S_IFDIR 0040000 2088c2ecf20Sopenharmony_ci#define S_IFCHR 0020000 2098c2ecf20Sopenharmony_ci#define S_IFBLK 0060000 2108c2ecf20Sopenharmony_ci#define S_IFREG 0100000 2118c2ecf20Sopenharmony_ci#define S_IFIFO 0010000 2128c2ecf20Sopenharmony_ci#define S_IFLNK 0120000 2138c2ecf20Sopenharmony_ci#define S_IFSOCK 0140000 2148c2ecf20Sopenharmony_ci#define S_IFMT 0170000 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci#define S_ISDIR(mode) (((mode) & S_IFDIR) == S_IFDIR) 2178c2ecf20Sopenharmony_ci#define S_ISCHR(mode) (((mode) & S_IFCHR) == S_IFCHR) 2188c2ecf20Sopenharmony_ci#define S_ISBLK(mode) (((mode) & S_IFBLK) == S_IFBLK) 2198c2ecf20Sopenharmony_ci#define S_ISREG(mode) (((mode) & S_IFREG) == S_IFREG) 2208c2ecf20Sopenharmony_ci#define S_ISFIFO(mode) (((mode) & S_IFIFO) == S_IFIFO) 2218c2ecf20Sopenharmony_ci#define S_ISLNK(mode) (((mode) & S_IFLNK) == S_IFLNK) 2228c2ecf20Sopenharmony_ci#define S_ISSOCK(mode) (((mode) & S_IFSOCK) == S_IFSOCK) 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci#define DT_UNKNOWN 0 2258c2ecf20Sopenharmony_ci#define DT_FIFO 1 2268c2ecf20Sopenharmony_ci#define DT_CHR 2 2278c2ecf20Sopenharmony_ci#define DT_DIR 4 2288c2ecf20Sopenharmony_ci#define DT_BLK 6 2298c2ecf20Sopenharmony_ci#define DT_REG 8 2308c2ecf20Sopenharmony_ci#define DT_LNK 10 2318c2ecf20Sopenharmony_ci#define DT_SOCK 12 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci/* all the *at functions */ 2348c2ecf20Sopenharmony_ci#ifndef AT_FDWCD 2358c2ecf20Sopenharmony_ci#define AT_FDCWD -100 2368c2ecf20Sopenharmony_ci#endif 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci/* lseek */ 2398c2ecf20Sopenharmony_ci#define SEEK_SET 0 2408c2ecf20Sopenharmony_ci#define SEEK_CUR 1 2418c2ecf20Sopenharmony_ci#define SEEK_END 2 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci/* reboot */ 2448c2ecf20Sopenharmony_ci#define LINUX_REBOOT_MAGIC1 0xfee1dead 2458c2ecf20Sopenharmony_ci#define LINUX_REBOOT_MAGIC2 0x28121969 2468c2ecf20Sopenharmony_ci#define LINUX_REBOOT_CMD_HALT 0xcdef0123 2478c2ecf20Sopenharmony_ci#define LINUX_REBOOT_CMD_POWER_OFF 0x4321fedc 2488c2ecf20Sopenharmony_ci#define LINUX_REBOOT_CMD_RESTART 0x01234567 2498c2ecf20Sopenharmony_ci#define LINUX_REBOOT_CMD_SW_SUSPEND 0xd000fce2 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_ci/* The format of the struct as returned by the libc to the application, which 2538c2ecf20Sopenharmony_ci * significantly differs from the format returned by the stat() syscall flavours. 2548c2ecf20Sopenharmony_ci */ 2558c2ecf20Sopenharmony_cistruct stat { 2568c2ecf20Sopenharmony_ci dev_t st_dev; /* ID of device containing file */ 2578c2ecf20Sopenharmony_ci ino_t st_ino; /* inode number */ 2588c2ecf20Sopenharmony_ci mode_t st_mode; /* protection */ 2598c2ecf20Sopenharmony_ci nlink_t st_nlink; /* number of hard links */ 2608c2ecf20Sopenharmony_ci uid_t st_uid; /* user ID of owner */ 2618c2ecf20Sopenharmony_ci gid_t st_gid; /* group ID of owner */ 2628c2ecf20Sopenharmony_ci dev_t st_rdev; /* device ID (if special file) */ 2638c2ecf20Sopenharmony_ci off_t st_size; /* total size, in bytes */ 2648c2ecf20Sopenharmony_ci blksize_t st_blksize; /* blocksize for file system I/O */ 2658c2ecf20Sopenharmony_ci blkcnt_t st_blocks; /* number of 512B blocks allocated */ 2668c2ecf20Sopenharmony_ci time_t st_atime; /* time of last access */ 2678c2ecf20Sopenharmony_ci time_t st_mtime; /* time of last modification */ 2688c2ecf20Sopenharmony_ci time_t st_ctime; /* time of last status change */ 2698c2ecf20Sopenharmony_ci}; 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_ci#define WEXITSTATUS(status) (((status) & 0xff00) >> 8) 2728c2ecf20Sopenharmony_ci#define WIFEXITED(status) (((status) & 0x7f) == 0) 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci/* Below comes the architecture-specific code. For each architecture, we have 2768c2ecf20Sopenharmony_ci * the syscall declarations and the _start code definition. This is the only 2778c2ecf20Sopenharmony_ci * global part. On all architectures the kernel puts everything in the stack 2788c2ecf20Sopenharmony_ci * before jumping to _start just above us, without any return address (_start 2798c2ecf20Sopenharmony_ci * is not a function but an entry pint). So at the stack pointer we find argc. 2808c2ecf20Sopenharmony_ci * Then argv[] begins, and ends at the first NULL. Then we have envp which 2818c2ecf20Sopenharmony_ci * starts and ends with a NULL as well. So envp=argv+argc+1. 2828c2ecf20Sopenharmony_ci */ 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci#if defined(__x86_64__) 2858c2ecf20Sopenharmony_ci/* Syscalls for x86_64 : 2868c2ecf20Sopenharmony_ci * - registers are 64-bit 2878c2ecf20Sopenharmony_ci * - syscall number is passed in rax 2888c2ecf20Sopenharmony_ci * - arguments are in rdi, rsi, rdx, r10, r8, r9 respectively 2898c2ecf20Sopenharmony_ci * - the system call is performed by calling the syscall instruction 2908c2ecf20Sopenharmony_ci * - syscall return comes in rax 2918c2ecf20Sopenharmony_ci * - rcx and r8..r11 may be clobbered, others are preserved. 2928c2ecf20Sopenharmony_ci * - the arguments are cast to long and assigned into the target registers 2938c2ecf20Sopenharmony_ci * which are then simply passed as registers to the asm code, so that we 2948c2ecf20Sopenharmony_ci * don't have to experience issues with register constraints. 2958c2ecf20Sopenharmony_ci * - the syscall number is always specified last in order to allow to force 2968c2ecf20Sopenharmony_ci * some registers before (gcc refuses a %-register at the last position). 2978c2ecf20Sopenharmony_ci */ 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ci#define my_syscall0(num) \ 3008c2ecf20Sopenharmony_ci({ \ 3018c2ecf20Sopenharmony_ci long _ret; \ 3028c2ecf20Sopenharmony_ci register long _num asm("rax") = (num); \ 3038c2ecf20Sopenharmony_ci \ 3048c2ecf20Sopenharmony_ci asm volatile ( \ 3058c2ecf20Sopenharmony_ci "syscall\n" \ 3068c2ecf20Sopenharmony_ci : "=a" (_ret) \ 3078c2ecf20Sopenharmony_ci : "0"(_num) \ 3088c2ecf20Sopenharmony_ci : "rcx", "r8", "r9", "r10", "r11", "memory", "cc" \ 3098c2ecf20Sopenharmony_ci ); \ 3108c2ecf20Sopenharmony_ci _ret; \ 3118c2ecf20Sopenharmony_ci}) 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_ci#define my_syscall1(num, arg1) \ 3148c2ecf20Sopenharmony_ci({ \ 3158c2ecf20Sopenharmony_ci long _ret; \ 3168c2ecf20Sopenharmony_ci register long _num asm("rax") = (num); \ 3178c2ecf20Sopenharmony_ci register long _arg1 asm("rdi") = (long)(arg1); \ 3188c2ecf20Sopenharmony_ci \ 3198c2ecf20Sopenharmony_ci asm volatile ( \ 3208c2ecf20Sopenharmony_ci "syscall\n" \ 3218c2ecf20Sopenharmony_ci : "=a" (_ret) \ 3228c2ecf20Sopenharmony_ci : "r"(_arg1), \ 3238c2ecf20Sopenharmony_ci "0"(_num) \ 3248c2ecf20Sopenharmony_ci : "rcx", "r8", "r9", "r10", "r11", "memory", "cc" \ 3258c2ecf20Sopenharmony_ci ); \ 3268c2ecf20Sopenharmony_ci _ret; \ 3278c2ecf20Sopenharmony_ci}) 3288c2ecf20Sopenharmony_ci 3298c2ecf20Sopenharmony_ci#define my_syscall2(num, arg1, arg2) \ 3308c2ecf20Sopenharmony_ci({ \ 3318c2ecf20Sopenharmony_ci long _ret; \ 3328c2ecf20Sopenharmony_ci register long _num asm("rax") = (num); \ 3338c2ecf20Sopenharmony_ci register long _arg1 asm("rdi") = (long)(arg1); \ 3348c2ecf20Sopenharmony_ci register long _arg2 asm("rsi") = (long)(arg2); \ 3358c2ecf20Sopenharmony_ci \ 3368c2ecf20Sopenharmony_ci asm volatile ( \ 3378c2ecf20Sopenharmony_ci "syscall\n" \ 3388c2ecf20Sopenharmony_ci : "=a" (_ret) \ 3398c2ecf20Sopenharmony_ci : "r"(_arg1), "r"(_arg2), \ 3408c2ecf20Sopenharmony_ci "0"(_num) \ 3418c2ecf20Sopenharmony_ci : "rcx", "r8", "r9", "r10", "r11", "memory", "cc" \ 3428c2ecf20Sopenharmony_ci ); \ 3438c2ecf20Sopenharmony_ci _ret; \ 3448c2ecf20Sopenharmony_ci}) 3458c2ecf20Sopenharmony_ci 3468c2ecf20Sopenharmony_ci#define my_syscall3(num, arg1, arg2, arg3) \ 3478c2ecf20Sopenharmony_ci({ \ 3488c2ecf20Sopenharmony_ci long _ret; \ 3498c2ecf20Sopenharmony_ci register long _num asm("rax") = (num); \ 3508c2ecf20Sopenharmony_ci register long _arg1 asm("rdi") = (long)(arg1); \ 3518c2ecf20Sopenharmony_ci register long _arg2 asm("rsi") = (long)(arg2); \ 3528c2ecf20Sopenharmony_ci register long _arg3 asm("rdx") = (long)(arg3); \ 3538c2ecf20Sopenharmony_ci \ 3548c2ecf20Sopenharmony_ci asm volatile ( \ 3558c2ecf20Sopenharmony_ci "syscall\n" \ 3568c2ecf20Sopenharmony_ci : "=a" (_ret) \ 3578c2ecf20Sopenharmony_ci : "r"(_arg1), "r"(_arg2), "r"(_arg3), \ 3588c2ecf20Sopenharmony_ci "0"(_num) \ 3598c2ecf20Sopenharmony_ci : "rcx", "r8", "r9", "r10", "r11", "memory", "cc" \ 3608c2ecf20Sopenharmony_ci ); \ 3618c2ecf20Sopenharmony_ci _ret; \ 3628c2ecf20Sopenharmony_ci}) 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci#define my_syscall4(num, arg1, arg2, arg3, arg4) \ 3658c2ecf20Sopenharmony_ci({ \ 3668c2ecf20Sopenharmony_ci long _ret; \ 3678c2ecf20Sopenharmony_ci register long _num asm("rax") = (num); \ 3688c2ecf20Sopenharmony_ci register long _arg1 asm("rdi") = (long)(arg1); \ 3698c2ecf20Sopenharmony_ci register long _arg2 asm("rsi") = (long)(arg2); \ 3708c2ecf20Sopenharmony_ci register long _arg3 asm("rdx") = (long)(arg3); \ 3718c2ecf20Sopenharmony_ci register long _arg4 asm("r10") = (long)(arg4); \ 3728c2ecf20Sopenharmony_ci \ 3738c2ecf20Sopenharmony_ci asm volatile ( \ 3748c2ecf20Sopenharmony_ci "syscall\n" \ 3758c2ecf20Sopenharmony_ci : "=a" (_ret), "=r"(_arg4) \ 3768c2ecf20Sopenharmony_ci : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \ 3778c2ecf20Sopenharmony_ci "0"(_num) \ 3788c2ecf20Sopenharmony_ci : "rcx", "r8", "r9", "r11", "memory", "cc" \ 3798c2ecf20Sopenharmony_ci ); \ 3808c2ecf20Sopenharmony_ci _ret; \ 3818c2ecf20Sopenharmony_ci}) 3828c2ecf20Sopenharmony_ci 3838c2ecf20Sopenharmony_ci#define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 3848c2ecf20Sopenharmony_ci({ \ 3858c2ecf20Sopenharmony_ci long _ret; \ 3868c2ecf20Sopenharmony_ci register long _num asm("rax") = (num); \ 3878c2ecf20Sopenharmony_ci register long _arg1 asm("rdi") = (long)(arg1); \ 3888c2ecf20Sopenharmony_ci register long _arg2 asm("rsi") = (long)(arg2); \ 3898c2ecf20Sopenharmony_ci register long _arg3 asm("rdx") = (long)(arg3); \ 3908c2ecf20Sopenharmony_ci register long _arg4 asm("r10") = (long)(arg4); \ 3918c2ecf20Sopenharmony_ci register long _arg5 asm("r8") = (long)(arg5); \ 3928c2ecf20Sopenharmony_ci \ 3938c2ecf20Sopenharmony_ci asm volatile ( \ 3948c2ecf20Sopenharmony_ci "syscall\n" \ 3958c2ecf20Sopenharmony_ci : "=a" (_ret), "=r"(_arg4), "=r"(_arg5) \ 3968c2ecf20Sopenharmony_ci : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 3978c2ecf20Sopenharmony_ci "0"(_num) \ 3988c2ecf20Sopenharmony_ci : "rcx", "r9", "r11", "memory", "cc" \ 3998c2ecf20Sopenharmony_ci ); \ 4008c2ecf20Sopenharmony_ci _ret; \ 4018c2ecf20Sopenharmony_ci}) 4028c2ecf20Sopenharmony_ci 4038c2ecf20Sopenharmony_ci#define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 4048c2ecf20Sopenharmony_ci({ \ 4058c2ecf20Sopenharmony_ci long _ret; \ 4068c2ecf20Sopenharmony_ci register long _num asm("rax") = (num); \ 4078c2ecf20Sopenharmony_ci register long _arg1 asm("rdi") = (long)(arg1); \ 4088c2ecf20Sopenharmony_ci register long _arg2 asm("rsi") = (long)(arg2); \ 4098c2ecf20Sopenharmony_ci register long _arg3 asm("rdx") = (long)(arg3); \ 4108c2ecf20Sopenharmony_ci register long _arg4 asm("r10") = (long)(arg4); \ 4118c2ecf20Sopenharmony_ci register long _arg5 asm("r8") = (long)(arg5); \ 4128c2ecf20Sopenharmony_ci register long _arg6 asm("r9") = (long)(arg6); \ 4138c2ecf20Sopenharmony_ci \ 4148c2ecf20Sopenharmony_ci asm volatile ( \ 4158c2ecf20Sopenharmony_ci "syscall\n" \ 4168c2ecf20Sopenharmony_ci : "=a" (_ret), "=r"(_arg4), "=r"(_arg5) \ 4178c2ecf20Sopenharmony_ci : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 4188c2ecf20Sopenharmony_ci "r"(_arg6), "0"(_num) \ 4198c2ecf20Sopenharmony_ci : "rcx", "r11", "memory", "cc" \ 4208c2ecf20Sopenharmony_ci ); \ 4218c2ecf20Sopenharmony_ci _ret; \ 4228c2ecf20Sopenharmony_ci}) 4238c2ecf20Sopenharmony_ci 4248c2ecf20Sopenharmony_ci/* startup code */ 4258c2ecf20Sopenharmony_ci/* 4268c2ecf20Sopenharmony_ci * x86-64 System V ABI mandates: 4278c2ecf20Sopenharmony_ci * 1) %rsp must be 16-byte aligned right before the function call. 4288c2ecf20Sopenharmony_ci * 2) The deepest stack frame should be zero (the %rbp). 4298c2ecf20Sopenharmony_ci * 4308c2ecf20Sopenharmony_ci */ 4318c2ecf20Sopenharmony_ciasm(".section .text\n" 4328c2ecf20Sopenharmony_ci ".global _start\n" 4338c2ecf20Sopenharmony_ci "_start:\n" 4348c2ecf20Sopenharmony_ci "pop %rdi\n" // argc (first arg, %rdi) 4358c2ecf20Sopenharmony_ci "mov %rsp, %rsi\n" // argv[] (second arg, %rsi) 4368c2ecf20Sopenharmony_ci "lea 8(%rsi,%rdi,8),%rdx\n" // then a NULL then envp (third arg, %rdx) 4378c2ecf20Sopenharmony_ci "xor %ebp, %ebp\n" // zero the stack frame 4388c2ecf20Sopenharmony_ci "and $-16, %rsp\n" // x86 ABI : esp must be 16-byte aligned before call 4398c2ecf20Sopenharmony_ci "call main\n" // main() returns the status code, we'll exit with it. 4408c2ecf20Sopenharmony_ci "mov %eax, %edi\n" // retrieve exit code (32 bit) 4418c2ecf20Sopenharmony_ci "mov $60, %rax\n" // NR_exit == 60 4428c2ecf20Sopenharmony_ci "syscall\n" // really exit 4438c2ecf20Sopenharmony_ci "hlt\n" // ensure it does not return 4448c2ecf20Sopenharmony_ci ""); 4458c2ecf20Sopenharmony_ci 4468c2ecf20Sopenharmony_ci/* fcntl / open */ 4478c2ecf20Sopenharmony_ci#define O_RDONLY 0 4488c2ecf20Sopenharmony_ci#define O_WRONLY 1 4498c2ecf20Sopenharmony_ci#define O_RDWR 2 4508c2ecf20Sopenharmony_ci#define O_CREAT 0x40 4518c2ecf20Sopenharmony_ci#define O_EXCL 0x80 4528c2ecf20Sopenharmony_ci#define O_NOCTTY 0x100 4538c2ecf20Sopenharmony_ci#define O_TRUNC 0x200 4548c2ecf20Sopenharmony_ci#define O_APPEND 0x400 4558c2ecf20Sopenharmony_ci#define O_NONBLOCK 0x800 4568c2ecf20Sopenharmony_ci#define O_DIRECTORY 0x10000 4578c2ecf20Sopenharmony_ci 4588c2ecf20Sopenharmony_ci/* The struct returned by the stat() syscall, equivalent to stat64(). The 4598c2ecf20Sopenharmony_ci * syscall returns 116 bytes and stops in the middle of __unused. 4608c2ecf20Sopenharmony_ci */ 4618c2ecf20Sopenharmony_cistruct sys_stat_struct { 4628c2ecf20Sopenharmony_ci unsigned long st_dev; 4638c2ecf20Sopenharmony_ci unsigned long st_ino; 4648c2ecf20Sopenharmony_ci unsigned long st_nlink; 4658c2ecf20Sopenharmony_ci unsigned int st_mode; 4668c2ecf20Sopenharmony_ci unsigned int st_uid; 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ci unsigned int st_gid; 4698c2ecf20Sopenharmony_ci unsigned int __pad0; 4708c2ecf20Sopenharmony_ci unsigned long st_rdev; 4718c2ecf20Sopenharmony_ci long st_size; 4728c2ecf20Sopenharmony_ci long st_blksize; 4738c2ecf20Sopenharmony_ci 4748c2ecf20Sopenharmony_ci long st_blocks; 4758c2ecf20Sopenharmony_ci unsigned long st_atime; 4768c2ecf20Sopenharmony_ci unsigned long st_atime_nsec; 4778c2ecf20Sopenharmony_ci unsigned long st_mtime; 4788c2ecf20Sopenharmony_ci 4798c2ecf20Sopenharmony_ci unsigned long st_mtime_nsec; 4808c2ecf20Sopenharmony_ci unsigned long st_ctime; 4818c2ecf20Sopenharmony_ci unsigned long st_ctime_nsec; 4828c2ecf20Sopenharmony_ci long __unused[3]; 4838c2ecf20Sopenharmony_ci}; 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_ci#elif defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) 4868c2ecf20Sopenharmony_ci/* Syscalls for i386 : 4878c2ecf20Sopenharmony_ci * - mostly similar to x86_64 4888c2ecf20Sopenharmony_ci * - registers are 32-bit 4898c2ecf20Sopenharmony_ci * - syscall number is passed in eax 4908c2ecf20Sopenharmony_ci * - arguments are in ebx, ecx, edx, esi, edi, ebp respectively 4918c2ecf20Sopenharmony_ci * - all registers are preserved (except eax of course) 4928c2ecf20Sopenharmony_ci * - the system call is performed by calling int $0x80 4938c2ecf20Sopenharmony_ci * - syscall return comes in eax 4948c2ecf20Sopenharmony_ci * - the arguments are cast to long and assigned into the target registers 4958c2ecf20Sopenharmony_ci * which are then simply passed as registers to the asm code, so that we 4968c2ecf20Sopenharmony_ci * don't have to experience issues with register constraints. 4978c2ecf20Sopenharmony_ci * - the syscall number is always specified last in order to allow to force 4988c2ecf20Sopenharmony_ci * some registers before (gcc refuses a %-register at the last position). 4998c2ecf20Sopenharmony_ci * 5008c2ecf20Sopenharmony_ci * Also, i386 supports the old_select syscall if newselect is not available 5018c2ecf20Sopenharmony_ci */ 5028c2ecf20Sopenharmony_ci#define __ARCH_WANT_SYS_OLD_SELECT 5038c2ecf20Sopenharmony_ci 5048c2ecf20Sopenharmony_ci#define my_syscall0(num) \ 5058c2ecf20Sopenharmony_ci({ \ 5068c2ecf20Sopenharmony_ci long _ret; \ 5078c2ecf20Sopenharmony_ci register long _num asm("eax") = (num); \ 5088c2ecf20Sopenharmony_ci \ 5098c2ecf20Sopenharmony_ci asm volatile ( \ 5108c2ecf20Sopenharmony_ci "int $0x80\n" \ 5118c2ecf20Sopenharmony_ci : "=a" (_ret) \ 5128c2ecf20Sopenharmony_ci : "0"(_num) \ 5138c2ecf20Sopenharmony_ci : "memory", "cc" \ 5148c2ecf20Sopenharmony_ci ); \ 5158c2ecf20Sopenharmony_ci _ret; \ 5168c2ecf20Sopenharmony_ci}) 5178c2ecf20Sopenharmony_ci 5188c2ecf20Sopenharmony_ci#define my_syscall1(num, arg1) \ 5198c2ecf20Sopenharmony_ci({ \ 5208c2ecf20Sopenharmony_ci long _ret; \ 5218c2ecf20Sopenharmony_ci register long _num asm("eax") = (num); \ 5228c2ecf20Sopenharmony_ci register long _arg1 asm("ebx") = (long)(arg1); \ 5238c2ecf20Sopenharmony_ci \ 5248c2ecf20Sopenharmony_ci asm volatile ( \ 5258c2ecf20Sopenharmony_ci "int $0x80\n" \ 5268c2ecf20Sopenharmony_ci : "=a" (_ret) \ 5278c2ecf20Sopenharmony_ci : "r"(_arg1), \ 5288c2ecf20Sopenharmony_ci "0"(_num) \ 5298c2ecf20Sopenharmony_ci : "memory", "cc" \ 5308c2ecf20Sopenharmony_ci ); \ 5318c2ecf20Sopenharmony_ci _ret; \ 5328c2ecf20Sopenharmony_ci}) 5338c2ecf20Sopenharmony_ci 5348c2ecf20Sopenharmony_ci#define my_syscall2(num, arg1, arg2) \ 5358c2ecf20Sopenharmony_ci({ \ 5368c2ecf20Sopenharmony_ci long _ret; \ 5378c2ecf20Sopenharmony_ci register long _num asm("eax") = (num); \ 5388c2ecf20Sopenharmony_ci register long _arg1 asm("ebx") = (long)(arg1); \ 5398c2ecf20Sopenharmony_ci register long _arg2 asm("ecx") = (long)(arg2); \ 5408c2ecf20Sopenharmony_ci \ 5418c2ecf20Sopenharmony_ci asm volatile ( \ 5428c2ecf20Sopenharmony_ci "int $0x80\n" \ 5438c2ecf20Sopenharmony_ci : "=a" (_ret) \ 5448c2ecf20Sopenharmony_ci : "r"(_arg1), "r"(_arg2), \ 5458c2ecf20Sopenharmony_ci "0"(_num) \ 5468c2ecf20Sopenharmony_ci : "memory", "cc" \ 5478c2ecf20Sopenharmony_ci ); \ 5488c2ecf20Sopenharmony_ci _ret; \ 5498c2ecf20Sopenharmony_ci}) 5508c2ecf20Sopenharmony_ci 5518c2ecf20Sopenharmony_ci#define my_syscall3(num, arg1, arg2, arg3) \ 5528c2ecf20Sopenharmony_ci({ \ 5538c2ecf20Sopenharmony_ci long _ret; \ 5548c2ecf20Sopenharmony_ci register long _num asm("eax") = (num); \ 5558c2ecf20Sopenharmony_ci register long _arg1 asm("ebx") = (long)(arg1); \ 5568c2ecf20Sopenharmony_ci register long _arg2 asm("ecx") = (long)(arg2); \ 5578c2ecf20Sopenharmony_ci register long _arg3 asm("edx") = (long)(arg3); \ 5588c2ecf20Sopenharmony_ci \ 5598c2ecf20Sopenharmony_ci asm volatile ( \ 5608c2ecf20Sopenharmony_ci "int $0x80\n" \ 5618c2ecf20Sopenharmony_ci : "=a" (_ret) \ 5628c2ecf20Sopenharmony_ci : "r"(_arg1), "r"(_arg2), "r"(_arg3), \ 5638c2ecf20Sopenharmony_ci "0"(_num) \ 5648c2ecf20Sopenharmony_ci : "memory", "cc" \ 5658c2ecf20Sopenharmony_ci ); \ 5668c2ecf20Sopenharmony_ci _ret; \ 5678c2ecf20Sopenharmony_ci}) 5688c2ecf20Sopenharmony_ci 5698c2ecf20Sopenharmony_ci#define my_syscall4(num, arg1, arg2, arg3, arg4) \ 5708c2ecf20Sopenharmony_ci({ \ 5718c2ecf20Sopenharmony_ci long _ret; \ 5728c2ecf20Sopenharmony_ci register long _num asm("eax") = (num); \ 5738c2ecf20Sopenharmony_ci register long _arg1 asm("ebx") = (long)(arg1); \ 5748c2ecf20Sopenharmony_ci register long _arg2 asm("ecx") = (long)(arg2); \ 5758c2ecf20Sopenharmony_ci register long _arg3 asm("edx") = (long)(arg3); \ 5768c2ecf20Sopenharmony_ci register long _arg4 asm("esi") = (long)(arg4); \ 5778c2ecf20Sopenharmony_ci \ 5788c2ecf20Sopenharmony_ci asm volatile ( \ 5798c2ecf20Sopenharmony_ci "int $0x80\n" \ 5808c2ecf20Sopenharmony_ci : "=a" (_ret) \ 5818c2ecf20Sopenharmony_ci : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \ 5828c2ecf20Sopenharmony_ci "0"(_num) \ 5838c2ecf20Sopenharmony_ci : "memory", "cc" \ 5848c2ecf20Sopenharmony_ci ); \ 5858c2ecf20Sopenharmony_ci _ret; \ 5868c2ecf20Sopenharmony_ci}) 5878c2ecf20Sopenharmony_ci 5888c2ecf20Sopenharmony_ci#define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 5898c2ecf20Sopenharmony_ci({ \ 5908c2ecf20Sopenharmony_ci long _ret; \ 5918c2ecf20Sopenharmony_ci register long _num asm("eax") = (num); \ 5928c2ecf20Sopenharmony_ci register long _arg1 asm("ebx") = (long)(arg1); \ 5938c2ecf20Sopenharmony_ci register long _arg2 asm("ecx") = (long)(arg2); \ 5948c2ecf20Sopenharmony_ci register long _arg3 asm("edx") = (long)(arg3); \ 5958c2ecf20Sopenharmony_ci register long _arg4 asm("esi") = (long)(arg4); \ 5968c2ecf20Sopenharmony_ci register long _arg5 asm("edi") = (long)(arg5); \ 5978c2ecf20Sopenharmony_ci \ 5988c2ecf20Sopenharmony_ci asm volatile ( \ 5998c2ecf20Sopenharmony_ci "int $0x80\n" \ 6008c2ecf20Sopenharmony_ci : "=a" (_ret) \ 6018c2ecf20Sopenharmony_ci : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 6028c2ecf20Sopenharmony_ci "0"(_num) \ 6038c2ecf20Sopenharmony_ci : "memory", "cc" \ 6048c2ecf20Sopenharmony_ci ); \ 6058c2ecf20Sopenharmony_ci _ret; \ 6068c2ecf20Sopenharmony_ci}) 6078c2ecf20Sopenharmony_ci 6088c2ecf20Sopenharmony_ci/* startup code */ 6098c2ecf20Sopenharmony_ci/* 6108c2ecf20Sopenharmony_ci * i386 System V ABI mandates: 6118c2ecf20Sopenharmony_ci * 1) last pushed argument must be 16-byte aligned. 6128c2ecf20Sopenharmony_ci * 2) The deepest stack frame should be set to zero 6138c2ecf20Sopenharmony_ci * 6148c2ecf20Sopenharmony_ci */ 6158c2ecf20Sopenharmony_ciasm(".section .text\n" 6168c2ecf20Sopenharmony_ci ".global _start\n" 6178c2ecf20Sopenharmony_ci "_start:\n" 6188c2ecf20Sopenharmony_ci "pop %eax\n" // argc (first arg, %eax) 6198c2ecf20Sopenharmony_ci "mov %esp, %ebx\n" // argv[] (second arg, %ebx) 6208c2ecf20Sopenharmony_ci "lea 4(%ebx,%eax,4),%ecx\n" // then a NULL then envp (third arg, %ecx) 6218c2ecf20Sopenharmony_ci "xor %ebp, %ebp\n" // zero the stack frame 6228c2ecf20Sopenharmony_ci "and $-16, %esp\n" // x86 ABI : esp must be 16-byte aligned before 6238c2ecf20Sopenharmony_ci "sub $4, %esp\n" // the call instruction (args are aligned) 6248c2ecf20Sopenharmony_ci "push %ecx\n" // push all registers on the stack so that we 6258c2ecf20Sopenharmony_ci "push %ebx\n" // support both regparm and plain stack modes 6268c2ecf20Sopenharmony_ci "push %eax\n" 6278c2ecf20Sopenharmony_ci "call main\n" // main() returns the status code in %eax 6288c2ecf20Sopenharmony_ci "mov %eax, %ebx\n" // retrieve exit code (32-bit int) 6298c2ecf20Sopenharmony_ci "movl $1, %eax\n" // NR_exit == 1 6308c2ecf20Sopenharmony_ci "int $0x80\n" // exit now 6318c2ecf20Sopenharmony_ci "hlt\n" // ensure it does not 6328c2ecf20Sopenharmony_ci ""); 6338c2ecf20Sopenharmony_ci 6348c2ecf20Sopenharmony_ci/* fcntl / open */ 6358c2ecf20Sopenharmony_ci#define O_RDONLY 0 6368c2ecf20Sopenharmony_ci#define O_WRONLY 1 6378c2ecf20Sopenharmony_ci#define O_RDWR 2 6388c2ecf20Sopenharmony_ci#define O_CREAT 0x40 6398c2ecf20Sopenharmony_ci#define O_EXCL 0x80 6408c2ecf20Sopenharmony_ci#define O_NOCTTY 0x100 6418c2ecf20Sopenharmony_ci#define O_TRUNC 0x200 6428c2ecf20Sopenharmony_ci#define O_APPEND 0x400 6438c2ecf20Sopenharmony_ci#define O_NONBLOCK 0x800 6448c2ecf20Sopenharmony_ci#define O_DIRECTORY 0x10000 6458c2ecf20Sopenharmony_ci 6468c2ecf20Sopenharmony_ci/* The struct returned by the stat() syscall, 32-bit only, the syscall returns 6478c2ecf20Sopenharmony_ci * exactly 56 bytes (stops before the unused array). 6488c2ecf20Sopenharmony_ci */ 6498c2ecf20Sopenharmony_cistruct sys_stat_struct { 6508c2ecf20Sopenharmony_ci unsigned long st_dev; 6518c2ecf20Sopenharmony_ci unsigned long st_ino; 6528c2ecf20Sopenharmony_ci unsigned short st_mode; 6538c2ecf20Sopenharmony_ci unsigned short st_nlink; 6548c2ecf20Sopenharmony_ci unsigned short st_uid; 6558c2ecf20Sopenharmony_ci unsigned short st_gid; 6568c2ecf20Sopenharmony_ci 6578c2ecf20Sopenharmony_ci unsigned long st_rdev; 6588c2ecf20Sopenharmony_ci unsigned long st_size; 6598c2ecf20Sopenharmony_ci unsigned long st_blksize; 6608c2ecf20Sopenharmony_ci unsigned long st_blocks; 6618c2ecf20Sopenharmony_ci 6628c2ecf20Sopenharmony_ci unsigned long st_atime; 6638c2ecf20Sopenharmony_ci unsigned long st_atime_nsec; 6648c2ecf20Sopenharmony_ci unsigned long st_mtime; 6658c2ecf20Sopenharmony_ci unsigned long st_mtime_nsec; 6668c2ecf20Sopenharmony_ci 6678c2ecf20Sopenharmony_ci unsigned long st_ctime; 6688c2ecf20Sopenharmony_ci unsigned long st_ctime_nsec; 6698c2ecf20Sopenharmony_ci unsigned long __unused[2]; 6708c2ecf20Sopenharmony_ci}; 6718c2ecf20Sopenharmony_ci 6728c2ecf20Sopenharmony_ci#elif defined(__ARM_EABI__) 6738c2ecf20Sopenharmony_ci/* Syscalls for ARM in ARM or Thumb modes : 6748c2ecf20Sopenharmony_ci * - registers are 32-bit 6758c2ecf20Sopenharmony_ci * - stack is 8-byte aligned 6768c2ecf20Sopenharmony_ci * ( http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka4127.html) 6778c2ecf20Sopenharmony_ci * - syscall number is passed in r7 6788c2ecf20Sopenharmony_ci * - arguments are in r0, r1, r2, r3, r4, r5 6798c2ecf20Sopenharmony_ci * - the system call is performed by calling svc #0 6808c2ecf20Sopenharmony_ci * - syscall return comes in r0. 6818c2ecf20Sopenharmony_ci * - only lr is clobbered. 6828c2ecf20Sopenharmony_ci * - the arguments are cast to long and assigned into the target registers 6838c2ecf20Sopenharmony_ci * which are then simply passed as registers to the asm code, so that we 6848c2ecf20Sopenharmony_ci * don't have to experience issues with register constraints. 6858c2ecf20Sopenharmony_ci * - the syscall number is always specified last in order to allow to force 6868c2ecf20Sopenharmony_ci * some registers before (gcc refuses a %-register at the last position). 6878c2ecf20Sopenharmony_ci * 6888c2ecf20Sopenharmony_ci * Also, ARM supports the old_select syscall if newselect is not available 6898c2ecf20Sopenharmony_ci */ 6908c2ecf20Sopenharmony_ci#define __ARCH_WANT_SYS_OLD_SELECT 6918c2ecf20Sopenharmony_ci 6928c2ecf20Sopenharmony_ci#define my_syscall0(num) \ 6938c2ecf20Sopenharmony_ci({ \ 6948c2ecf20Sopenharmony_ci register long _num asm("r7") = (num); \ 6958c2ecf20Sopenharmony_ci register long _arg1 asm("r0"); \ 6968c2ecf20Sopenharmony_ci \ 6978c2ecf20Sopenharmony_ci asm volatile ( \ 6988c2ecf20Sopenharmony_ci "svc #0\n" \ 6998c2ecf20Sopenharmony_ci : "=r"(_arg1) \ 7008c2ecf20Sopenharmony_ci : "r"(_num) \ 7018c2ecf20Sopenharmony_ci : "memory", "cc", "lr" \ 7028c2ecf20Sopenharmony_ci ); \ 7038c2ecf20Sopenharmony_ci _arg1; \ 7048c2ecf20Sopenharmony_ci}) 7058c2ecf20Sopenharmony_ci 7068c2ecf20Sopenharmony_ci#define my_syscall1(num, arg1) \ 7078c2ecf20Sopenharmony_ci({ \ 7088c2ecf20Sopenharmony_ci register long _num asm("r7") = (num); \ 7098c2ecf20Sopenharmony_ci register long _arg1 asm("r0") = (long)(arg1); \ 7108c2ecf20Sopenharmony_ci \ 7118c2ecf20Sopenharmony_ci asm volatile ( \ 7128c2ecf20Sopenharmony_ci "svc #0\n" \ 7138c2ecf20Sopenharmony_ci : "=r"(_arg1) \ 7148c2ecf20Sopenharmony_ci : "r"(_arg1), \ 7158c2ecf20Sopenharmony_ci "r"(_num) \ 7168c2ecf20Sopenharmony_ci : "memory", "cc", "lr" \ 7178c2ecf20Sopenharmony_ci ); \ 7188c2ecf20Sopenharmony_ci _arg1; \ 7198c2ecf20Sopenharmony_ci}) 7208c2ecf20Sopenharmony_ci 7218c2ecf20Sopenharmony_ci#define my_syscall2(num, arg1, arg2) \ 7228c2ecf20Sopenharmony_ci({ \ 7238c2ecf20Sopenharmony_ci register long _num asm("r7") = (num); \ 7248c2ecf20Sopenharmony_ci register long _arg1 asm("r0") = (long)(arg1); \ 7258c2ecf20Sopenharmony_ci register long _arg2 asm("r1") = (long)(arg2); \ 7268c2ecf20Sopenharmony_ci \ 7278c2ecf20Sopenharmony_ci asm volatile ( \ 7288c2ecf20Sopenharmony_ci "svc #0\n" \ 7298c2ecf20Sopenharmony_ci : "=r"(_arg1) \ 7308c2ecf20Sopenharmony_ci : "r"(_arg1), "r"(_arg2), \ 7318c2ecf20Sopenharmony_ci "r"(_num) \ 7328c2ecf20Sopenharmony_ci : "memory", "cc", "lr" \ 7338c2ecf20Sopenharmony_ci ); \ 7348c2ecf20Sopenharmony_ci _arg1; \ 7358c2ecf20Sopenharmony_ci}) 7368c2ecf20Sopenharmony_ci 7378c2ecf20Sopenharmony_ci#define my_syscall3(num, arg1, arg2, arg3) \ 7388c2ecf20Sopenharmony_ci({ \ 7398c2ecf20Sopenharmony_ci register long _num asm("r7") = (num); \ 7408c2ecf20Sopenharmony_ci register long _arg1 asm("r0") = (long)(arg1); \ 7418c2ecf20Sopenharmony_ci register long _arg2 asm("r1") = (long)(arg2); \ 7428c2ecf20Sopenharmony_ci register long _arg3 asm("r2") = (long)(arg3); \ 7438c2ecf20Sopenharmony_ci \ 7448c2ecf20Sopenharmony_ci asm volatile ( \ 7458c2ecf20Sopenharmony_ci "svc #0\n" \ 7468c2ecf20Sopenharmony_ci : "=r"(_arg1) \ 7478c2ecf20Sopenharmony_ci : "r"(_arg1), "r"(_arg2), "r"(_arg3), \ 7488c2ecf20Sopenharmony_ci "r"(_num) \ 7498c2ecf20Sopenharmony_ci : "memory", "cc", "lr" \ 7508c2ecf20Sopenharmony_ci ); \ 7518c2ecf20Sopenharmony_ci _arg1; \ 7528c2ecf20Sopenharmony_ci}) 7538c2ecf20Sopenharmony_ci 7548c2ecf20Sopenharmony_ci#define my_syscall4(num, arg1, arg2, arg3, arg4) \ 7558c2ecf20Sopenharmony_ci({ \ 7568c2ecf20Sopenharmony_ci register long _num asm("r7") = (num); \ 7578c2ecf20Sopenharmony_ci register long _arg1 asm("r0") = (long)(arg1); \ 7588c2ecf20Sopenharmony_ci register long _arg2 asm("r1") = (long)(arg2); \ 7598c2ecf20Sopenharmony_ci register long _arg3 asm("r2") = (long)(arg3); \ 7608c2ecf20Sopenharmony_ci register long _arg4 asm("r3") = (long)(arg4); \ 7618c2ecf20Sopenharmony_ci \ 7628c2ecf20Sopenharmony_ci asm volatile ( \ 7638c2ecf20Sopenharmony_ci "svc #0\n" \ 7648c2ecf20Sopenharmony_ci : "=r"(_arg1) \ 7658c2ecf20Sopenharmony_ci : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \ 7668c2ecf20Sopenharmony_ci "r"(_num) \ 7678c2ecf20Sopenharmony_ci : "memory", "cc", "lr" \ 7688c2ecf20Sopenharmony_ci ); \ 7698c2ecf20Sopenharmony_ci _arg1; \ 7708c2ecf20Sopenharmony_ci}) 7718c2ecf20Sopenharmony_ci 7728c2ecf20Sopenharmony_ci#define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 7738c2ecf20Sopenharmony_ci({ \ 7748c2ecf20Sopenharmony_ci register long _num asm("r7") = (num); \ 7758c2ecf20Sopenharmony_ci register long _arg1 asm("r0") = (long)(arg1); \ 7768c2ecf20Sopenharmony_ci register long _arg2 asm("r1") = (long)(arg2); \ 7778c2ecf20Sopenharmony_ci register long _arg3 asm("r2") = (long)(arg3); \ 7788c2ecf20Sopenharmony_ci register long _arg4 asm("r3") = (long)(arg4); \ 7798c2ecf20Sopenharmony_ci register long _arg5 asm("r4") = (long)(arg5); \ 7808c2ecf20Sopenharmony_ci \ 7818c2ecf20Sopenharmony_ci asm volatile ( \ 7828c2ecf20Sopenharmony_ci "svc #0\n" \ 7838c2ecf20Sopenharmony_ci : "=r" (_arg1) \ 7848c2ecf20Sopenharmony_ci : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 7858c2ecf20Sopenharmony_ci "r"(_num) \ 7868c2ecf20Sopenharmony_ci : "memory", "cc", "lr" \ 7878c2ecf20Sopenharmony_ci ); \ 7888c2ecf20Sopenharmony_ci _arg1; \ 7898c2ecf20Sopenharmony_ci}) 7908c2ecf20Sopenharmony_ci 7918c2ecf20Sopenharmony_ci/* startup code */ 7928c2ecf20Sopenharmony_ciasm(".section .text\n" 7938c2ecf20Sopenharmony_ci ".global _start\n" 7948c2ecf20Sopenharmony_ci "_start:\n" 7958c2ecf20Sopenharmony_ci#if defined(__THUMBEB__) || defined(__THUMBEL__) 7968c2ecf20Sopenharmony_ci /* We enter here in 32-bit mode but if some previous functions were in 7978c2ecf20Sopenharmony_ci * 16-bit mode, the assembler cannot know, so we need to tell it we're in 7988c2ecf20Sopenharmony_ci * 32-bit now, then switch to 16-bit (is there a better way to do it than 7998c2ecf20Sopenharmony_ci * adding 1 by hand ?) and tell the asm we're now in 16-bit mode so that 8008c2ecf20Sopenharmony_ci * it generates correct instructions. Note that we do not support thumb1. 8018c2ecf20Sopenharmony_ci */ 8028c2ecf20Sopenharmony_ci ".code 32\n" 8038c2ecf20Sopenharmony_ci "add r0, pc, #1\n" 8048c2ecf20Sopenharmony_ci "bx r0\n" 8058c2ecf20Sopenharmony_ci ".code 16\n" 8068c2ecf20Sopenharmony_ci#endif 8078c2ecf20Sopenharmony_ci "pop {%r0}\n" // argc was in the stack 8088c2ecf20Sopenharmony_ci "mov %r1, %sp\n" // argv = sp 8098c2ecf20Sopenharmony_ci "add %r2, %r1, %r0, lsl #2\n" // envp = argv + 4*argc ... 8108c2ecf20Sopenharmony_ci "add %r2, %r2, $4\n" // ... + 4 8118c2ecf20Sopenharmony_ci "and %r3, %r1, $-8\n" // AAPCS : sp must be 8-byte aligned in the 8128c2ecf20Sopenharmony_ci "mov %sp, %r3\n" // callee, an bl doesn't push (lr=pc) 8138c2ecf20Sopenharmony_ci "bl main\n" // main() returns the status code, we'll exit with it. 8148c2ecf20Sopenharmony_ci "movs r7, $1\n" // NR_exit == 1 8158c2ecf20Sopenharmony_ci "svc $0x00\n" 8168c2ecf20Sopenharmony_ci ""); 8178c2ecf20Sopenharmony_ci 8188c2ecf20Sopenharmony_ci/* fcntl / open */ 8198c2ecf20Sopenharmony_ci#define O_RDONLY 0 8208c2ecf20Sopenharmony_ci#define O_WRONLY 1 8218c2ecf20Sopenharmony_ci#define O_RDWR 2 8228c2ecf20Sopenharmony_ci#define O_CREAT 0x40 8238c2ecf20Sopenharmony_ci#define O_EXCL 0x80 8248c2ecf20Sopenharmony_ci#define O_NOCTTY 0x100 8258c2ecf20Sopenharmony_ci#define O_TRUNC 0x200 8268c2ecf20Sopenharmony_ci#define O_APPEND 0x400 8278c2ecf20Sopenharmony_ci#define O_NONBLOCK 0x800 8288c2ecf20Sopenharmony_ci#define O_DIRECTORY 0x4000 8298c2ecf20Sopenharmony_ci 8308c2ecf20Sopenharmony_ci/* The struct returned by the stat() syscall, 32-bit only, the syscall returns 8318c2ecf20Sopenharmony_ci * exactly 56 bytes (stops before the unused array). In big endian, the format 8328c2ecf20Sopenharmony_ci * differs as devices are returned as short only. 8338c2ecf20Sopenharmony_ci */ 8348c2ecf20Sopenharmony_cistruct sys_stat_struct { 8358c2ecf20Sopenharmony_ci#if defined(__ARMEB__) 8368c2ecf20Sopenharmony_ci unsigned short st_dev; 8378c2ecf20Sopenharmony_ci unsigned short __pad1; 8388c2ecf20Sopenharmony_ci#else 8398c2ecf20Sopenharmony_ci unsigned long st_dev; 8408c2ecf20Sopenharmony_ci#endif 8418c2ecf20Sopenharmony_ci unsigned long st_ino; 8428c2ecf20Sopenharmony_ci unsigned short st_mode; 8438c2ecf20Sopenharmony_ci unsigned short st_nlink; 8448c2ecf20Sopenharmony_ci unsigned short st_uid; 8458c2ecf20Sopenharmony_ci unsigned short st_gid; 8468c2ecf20Sopenharmony_ci#if defined(__ARMEB__) 8478c2ecf20Sopenharmony_ci unsigned short st_rdev; 8488c2ecf20Sopenharmony_ci unsigned short __pad2; 8498c2ecf20Sopenharmony_ci#else 8508c2ecf20Sopenharmony_ci unsigned long st_rdev; 8518c2ecf20Sopenharmony_ci#endif 8528c2ecf20Sopenharmony_ci unsigned long st_size; 8538c2ecf20Sopenharmony_ci unsigned long st_blksize; 8548c2ecf20Sopenharmony_ci unsigned long st_blocks; 8558c2ecf20Sopenharmony_ci unsigned long st_atime; 8568c2ecf20Sopenharmony_ci unsigned long st_atime_nsec; 8578c2ecf20Sopenharmony_ci unsigned long st_mtime; 8588c2ecf20Sopenharmony_ci unsigned long st_mtime_nsec; 8598c2ecf20Sopenharmony_ci unsigned long st_ctime; 8608c2ecf20Sopenharmony_ci unsigned long st_ctime_nsec; 8618c2ecf20Sopenharmony_ci unsigned long __unused[2]; 8628c2ecf20Sopenharmony_ci}; 8638c2ecf20Sopenharmony_ci 8648c2ecf20Sopenharmony_ci#elif defined(__aarch64__) 8658c2ecf20Sopenharmony_ci/* Syscalls for AARCH64 : 8668c2ecf20Sopenharmony_ci * - registers are 64-bit 8678c2ecf20Sopenharmony_ci * - stack is 16-byte aligned 8688c2ecf20Sopenharmony_ci * - syscall number is passed in x8 8698c2ecf20Sopenharmony_ci * - arguments are in x0, x1, x2, x3, x4, x5 8708c2ecf20Sopenharmony_ci * - the system call is performed by calling svc 0 8718c2ecf20Sopenharmony_ci * - syscall return comes in x0. 8728c2ecf20Sopenharmony_ci * - the arguments are cast to long and assigned into the target registers 8738c2ecf20Sopenharmony_ci * which are then simply passed as registers to the asm code, so that we 8748c2ecf20Sopenharmony_ci * don't have to experience issues with register constraints. 8758c2ecf20Sopenharmony_ci * 8768c2ecf20Sopenharmony_ci * On aarch64, select() is not implemented so we have to use pselect6(). 8778c2ecf20Sopenharmony_ci */ 8788c2ecf20Sopenharmony_ci#define __ARCH_WANT_SYS_PSELECT6 8798c2ecf20Sopenharmony_ci 8808c2ecf20Sopenharmony_ci#define my_syscall0(num) \ 8818c2ecf20Sopenharmony_ci({ \ 8828c2ecf20Sopenharmony_ci register long _num asm("x8") = (num); \ 8838c2ecf20Sopenharmony_ci register long _arg1 asm("x0"); \ 8848c2ecf20Sopenharmony_ci \ 8858c2ecf20Sopenharmony_ci asm volatile ( \ 8868c2ecf20Sopenharmony_ci "svc #0\n" \ 8878c2ecf20Sopenharmony_ci : "=r"(_arg1) \ 8888c2ecf20Sopenharmony_ci : "r"(_num) \ 8898c2ecf20Sopenharmony_ci : "memory", "cc" \ 8908c2ecf20Sopenharmony_ci ); \ 8918c2ecf20Sopenharmony_ci _arg1; \ 8928c2ecf20Sopenharmony_ci}) 8938c2ecf20Sopenharmony_ci 8948c2ecf20Sopenharmony_ci#define my_syscall1(num, arg1) \ 8958c2ecf20Sopenharmony_ci({ \ 8968c2ecf20Sopenharmony_ci register long _num asm("x8") = (num); \ 8978c2ecf20Sopenharmony_ci register long _arg1 asm("x0") = (long)(arg1); \ 8988c2ecf20Sopenharmony_ci \ 8998c2ecf20Sopenharmony_ci asm volatile ( \ 9008c2ecf20Sopenharmony_ci "svc #0\n" \ 9018c2ecf20Sopenharmony_ci : "=r"(_arg1) \ 9028c2ecf20Sopenharmony_ci : "r"(_arg1), \ 9038c2ecf20Sopenharmony_ci "r"(_num) \ 9048c2ecf20Sopenharmony_ci : "memory", "cc" \ 9058c2ecf20Sopenharmony_ci ); \ 9068c2ecf20Sopenharmony_ci _arg1; \ 9078c2ecf20Sopenharmony_ci}) 9088c2ecf20Sopenharmony_ci 9098c2ecf20Sopenharmony_ci#define my_syscall2(num, arg1, arg2) \ 9108c2ecf20Sopenharmony_ci({ \ 9118c2ecf20Sopenharmony_ci register long _num asm("x8") = (num); \ 9128c2ecf20Sopenharmony_ci register long _arg1 asm("x0") = (long)(arg1); \ 9138c2ecf20Sopenharmony_ci register long _arg2 asm("x1") = (long)(arg2); \ 9148c2ecf20Sopenharmony_ci \ 9158c2ecf20Sopenharmony_ci asm volatile ( \ 9168c2ecf20Sopenharmony_ci "svc #0\n" \ 9178c2ecf20Sopenharmony_ci : "=r"(_arg1) \ 9188c2ecf20Sopenharmony_ci : "r"(_arg1), "r"(_arg2), \ 9198c2ecf20Sopenharmony_ci "r"(_num) \ 9208c2ecf20Sopenharmony_ci : "memory", "cc" \ 9218c2ecf20Sopenharmony_ci ); \ 9228c2ecf20Sopenharmony_ci _arg1; \ 9238c2ecf20Sopenharmony_ci}) 9248c2ecf20Sopenharmony_ci 9258c2ecf20Sopenharmony_ci#define my_syscall3(num, arg1, arg2, arg3) \ 9268c2ecf20Sopenharmony_ci({ \ 9278c2ecf20Sopenharmony_ci register long _num asm("x8") = (num); \ 9288c2ecf20Sopenharmony_ci register long _arg1 asm("x0") = (long)(arg1); \ 9298c2ecf20Sopenharmony_ci register long _arg2 asm("x1") = (long)(arg2); \ 9308c2ecf20Sopenharmony_ci register long _arg3 asm("x2") = (long)(arg3); \ 9318c2ecf20Sopenharmony_ci \ 9328c2ecf20Sopenharmony_ci asm volatile ( \ 9338c2ecf20Sopenharmony_ci "svc #0\n" \ 9348c2ecf20Sopenharmony_ci : "=r"(_arg1) \ 9358c2ecf20Sopenharmony_ci : "r"(_arg1), "r"(_arg2), "r"(_arg3), \ 9368c2ecf20Sopenharmony_ci "r"(_num) \ 9378c2ecf20Sopenharmony_ci : "memory", "cc" \ 9388c2ecf20Sopenharmony_ci ); \ 9398c2ecf20Sopenharmony_ci _arg1; \ 9408c2ecf20Sopenharmony_ci}) 9418c2ecf20Sopenharmony_ci 9428c2ecf20Sopenharmony_ci#define my_syscall4(num, arg1, arg2, arg3, arg4) \ 9438c2ecf20Sopenharmony_ci({ \ 9448c2ecf20Sopenharmony_ci register long _num asm("x8") = (num); \ 9458c2ecf20Sopenharmony_ci register long _arg1 asm("x0") = (long)(arg1); \ 9468c2ecf20Sopenharmony_ci register long _arg2 asm("x1") = (long)(arg2); \ 9478c2ecf20Sopenharmony_ci register long _arg3 asm("x2") = (long)(arg3); \ 9488c2ecf20Sopenharmony_ci register long _arg4 asm("x3") = (long)(arg4); \ 9498c2ecf20Sopenharmony_ci \ 9508c2ecf20Sopenharmony_ci asm volatile ( \ 9518c2ecf20Sopenharmony_ci "svc #0\n" \ 9528c2ecf20Sopenharmony_ci : "=r"(_arg1) \ 9538c2ecf20Sopenharmony_ci : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \ 9548c2ecf20Sopenharmony_ci "r"(_num) \ 9558c2ecf20Sopenharmony_ci : "memory", "cc" \ 9568c2ecf20Sopenharmony_ci ); \ 9578c2ecf20Sopenharmony_ci _arg1; \ 9588c2ecf20Sopenharmony_ci}) 9598c2ecf20Sopenharmony_ci 9608c2ecf20Sopenharmony_ci#define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 9618c2ecf20Sopenharmony_ci({ \ 9628c2ecf20Sopenharmony_ci register long _num asm("x8") = (num); \ 9638c2ecf20Sopenharmony_ci register long _arg1 asm("x0") = (long)(arg1); \ 9648c2ecf20Sopenharmony_ci register long _arg2 asm("x1") = (long)(arg2); \ 9658c2ecf20Sopenharmony_ci register long _arg3 asm("x2") = (long)(arg3); \ 9668c2ecf20Sopenharmony_ci register long _arg4 asm("x3") = (long)(arg4); \ 9678c2ecf20Sopenharmony_ci register long _arg5 asm("x4") = (long)(arg5); \ 9688c2ecf20Sopenharmony_ci \ 9698c2ecf20Sopenharmony_ci asm volatile ( \ 9708c2ecf20Sopenharmony_ci "svc #0\n" \ 9718c2ecf20Sopenharmony_ci : "=r" (_arg1) \ 9728c2ecf20Sopenharmony_ci : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 9738c2ecf20Sopenharmony_ci "r"(_num) \ 9748c2ecf20Sopenharmony_ci : "memory", "cc" \ 9758c2ecf20Sopenharmony_ci ); \ 9768c2ecf20Sopenharmony_ci _arg1; \ 9778c2ecf20Sopenharmony_ci}) 9788c2ecf20Sopenharmony_ci 9798c2ecf20Sopenharmony_ci#define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 9808c2ecf20Sopenharmony_ci({ \ 9818c2ecf20Sopenharmony_ci register long _num asm("x8") = (num); \ 9828c2ecf20Sopenharmony_ci register long _arg1 asm("x0") = (long)(arg1); \ 9838c2ecf20Sopenharmony_ci register long _arg2 asm("x1") = (long)(arg2); \ 9848c2ecf20Sopenharmony_ci register long _arg3 asm("x2") = (long)(arg3); \ 9858c2ecf20Sopenharmony_ci register long _arg4 asm("x3") = (long)(arg4); \ 9868c2ecf20Sopenharmony_ci register long _arg5 asm("x4") = (long)(arg5); \ 9878c2ecf20Sopenharmony_ci register long _arg6 asm("x5") = (long)(arg6); \ 9888c2ecf20Sopenharmony_ci \ 9898c2ecf20Sopenharmony_ci asm volatile ( \ 9908c2ecf20Sopenharmony_ci "svc #0\n" \ 9918c2ecf20Sopenharmony_ci : "=r" (_arg1) \ 9928c2ecf20Sopenharmony_ci : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 9938c2ecf20Sopenharmony_ci "r"(_arg6), "r"(_num) \ 9948c2ecf20Sopenharmony_ci : "memory", "cc" \ 9958c2ecf20Sopenharmony_ci ); \ 9968c2ecf20Sopenharmony_ci _arg1; \ 9978c2ecf20Sopenharmony_ci}) 9988c2ecf20Sopenharmony_ci 9998c2ecf20Sopenharmony_ci/* startup code */ 10008c2ecf20Sopenharmony_ciasm(".section .text\n" 10018c2ecf20Sopenharmony_ci ".global _start\n" 10028c2ecf20Sopenharmony_ci "_start:\n" 10038c2ecf20Sopenharmony_ci "ldr x0, [sp]\n" // argc (x0) was in the stack 10048c2ecf20Sopenharmony_ci "add x1, sp, 8\n" // argv (x1) = sp 10058c2ecf20Sopenharmony_ci "lsl x2, x0, 3\n" // envp (x2) = 8*argc ... 10068c2ecf20Sopenharmony_ci "add x2, x2, 8\n" // + 8 (skip null) 10078c2ecf20Sopenharmony_ci "add x2, x2, x1\n" // + argv 10088c2ecf20Sopenharmony_ci "and sp, x1, -16\n" // sp must be 16-byte aligned in the callee 10098c2ecf20Sopenharmony_ci "bl main\n" // main() returns the status code, we'll exit with it. 10108c2ecf20Sopenharmony_ci "mov x8, 93\n" // NR_exit == 93 10118c2ecf20Sopenharmony_ci "svc #0\n" 10128c2ecf20Sopenharmony_ci ""); 10138c2ecf20Sopenharmony_ci 10148c2ecf20Sopenharmony_ci/* fcntl / open */ 10158c2ecf20Sopenharmony_ci#define O_RDONLY 0 10168c2ecf20Sopenharmony_ci#define O_WRONLY 1 10178c2ecf20Sopenharmony_ci#define O_RDWR 2 10188c2ecf20Sopenharmony_ci#define O_CREAT 0x40 10198c2ecf20Sopenharmony_ci#define O_EXCL 0x80 10208c2ecf20Sopenharmony_ci#define O_NOCTTY 0x100 10218c2ecf20Sopenharmony_ci#define O_TRUNC 0x200 10228c2ecf20Sopenharmony_ci#define O_APPEND 0x400 10238c2ecf20Sopenharmony_ci#define O_NONBLOCK 0x800 10248c2ecf20Sopenharmony_ci#define O_DIRECTORY 0x4000 10258c2ecf20Sopenharmony_ci 10268c2ecf20Sopenharmony_ci/* The struct returned by the newfstatat() syscall. Differs slightly from the 10278c2ecf20Sopenharmony_ci * x86_64's stat one by field ordering, so be careful. 10288c2ecf20Sopenharmony_ci */ 10298c2ecf20Sopenharmony_cistruct sys_stat_struct { 10308c2ecf20Sopenharmony_ci unsigned long st_dev; 10318c2ecf20Sopenharmony_ci unsigned long st_ino; 10328c2ecf20Sopenharmony_ci unsigned int st_mode; 10338c2ecf20Sopenharmony_ci unsigned int st_nlink; 10348c2ecf20Sopenharmony_ci unsigned int st_uid; 10358c2ecf20Sopenharmony_ci unsigned int st_gid; 10368c2ecf20Sopenharmony_ci 10378c2ecf20Sopenharmony_ci unsigned long st_rdev; 10388c2ecf20Sopenharmony_ci unsigned long __pad1; 10398c2ecf20Sopenharmony_ci long st_size; 10408c2ecf20Sopenharmony_ci int st_blksize; 10418c2ecf20Sopenharmony_ci int __pad2; 10428c2ecf20Sopenharmony_ci 10438c2ecf20Sopenharmony_ci long st_blocks; 10448c2ecf20Sopenharmony_ci long st_atime; 10458c2ecf20Sopenharmony_ci unsigned long st_atime_nsec; 10468c2ecf20Sopenharmony_ci long st_mtime; 10478c2ecf20Sopenharmony_ci 10488c2ecf20Sopenharmony_ci unsigned long st_mtime_nsec; 10498c2ecf20Sopenharmony_ci long st_ctime; 10508c2ecf20Sopenharmony_ci unsigned long st_ctime_nsec; 10518c2ecf20Sopenharmony_ci unsigned int __unused[2]; 10528c2ecf20Sopenharmony_ci}; 10538c2ecf20Sopenharmony_ci 10548c2ecf20Sopenharmony_ci#elif defined(__mips__) && defined(_ABIO32) 10558c2ecf20Sopenharmony_ci/* Syscalls for MIPS ABI O32 : 10568c2ecf20Sopenharmony_ci * - WARNING! there's always a delayed slot! 10578c2ecf20Sopenharmony_ci * - WARNING again, the syntax is different, registers take a '$' and numbers 10588c2ecf20Sopenharmony_ci * do not. 10598c2ecf20Sopenharmony_ci * - registers are 32-bit 10608c2ecf20Sopenharmony_ci * - stack is 8-byte aligned 10618c2ecf20Sopenharmony_ci * - syscall number is passed in v0 (starts at 0xfa0). 10628c2ecf20Sopenharmony_ci * - arguments are in a0, a1, a2, a3, then the stack. The caller needs to 10638c2ecf20Sopenharmony_ci * leave some room in the stack for the callee to save a0..a3 if needed. 10648c2ecf20Sopenharmony_ci * - Many registers are clobbered, in fact only a0..a2 and s0..s8 are 10658c2ecf20Sopenharmony_ci * preserved. See: https://www.linux-mips.org/wiki/Syscall as well as 10668c2ecf20Sopenharmony_ci * scall32-o32.S in the kernel sources. 10678c2ecf20Sopenharmony_ci * - the system call is performed by calling "syscall" 10688c2ecf20Sopenharmony_ci * - syscall return comes in v0, and register a3 needs to be checked to know 10698c2ecf20Sopenharmony_ci * if an error occured, in which case errno is in v0. 10708c2ecf20Sopenharmony_ci * - the arguments are cast to long and assigned into the target registers 10718c2ecf20Sopenharmony_ci * which are then simply passed as registers to the asm code, so that we 10728c2ecf20Sopenharmony_ci * don't have to experience issues with register constraints. 10738c2ecf20Sopenharmony_ci */ 10748c2ecf20Sopenharmony_ci 10758c2ecf20Sopenharmony_ci#define my_syscall0(num) \ 10768c2ecf20Sopenharmony_ci({ \ 10778c2ecf20Sopenharmony_ci register long _num asm("v0") = (num); \ 10788c2ecf20Sopenharmony_ci register long _arg4 asm("a3"); \ 10798c2ecf20Sopenharmony_ci \ 10808c2ecf20Sopenharmony_ci asm volatile ( \ 10818c2ecf20Sopenharmony_ci "addiu $sp, $sp, -32\n" \ 10828c2ecf20Sopenharmony_ci "syscall\n" \ 10838c2ecf20Sopenharmony_ci "addiu $sp, $sp, 32\n" \ 10848c2ecf20Sopenharmony_ci : "=r"(_num), "=r"(_arg4) \ 10858c2ecf20Sopenharmony_ci : "r"(_num) \ 10868c2ecf20Sopenharmony_ci : "memory", "cc", "at", "v1", "hi", "lo", \ 10878c2ecf20Sopenharmony_ci "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \ 10888c2ecf20Sopenharmony_ci ); \ 10898c2ecf20Sopenharmony_ci _arg4 ? -_num : _num; \ 10908c2ecf20Sopenharmony_ci}) 10918c2ecf20Sopenharmony_ci 10928c2ecf20Sopenharmony_ci#define my_syscall1(num, arg1) \ 10938c2ecf20Sopenharmony_ci({ \ 10948c2ecf20Sopenharmony_ci register long _num asm("v0") = (num); \ 10958c2ecf20Sopenharmony_ci register long _arg1 asm("a0") = (long)(arg1); \ 10968c2ecf20Sopenharmony_ci register long _arg4 asm("a3"); \ 10978c2ecf20Sopenharmony_ci \ 10988c2ecf20Sopenharmony_ci asm volatile ( \ 10998c2ecf20Sopenharmony_ci "addiu $sp, $sp, -32\n" \ 11008c2ecf20Sopenharmony_ci "syscall\n" \ 11018c2ecf20Sopenharmony_ci "addiu $sp, $sp, 32\n" \ 11028c2ecf20Sopenharmony_ci : "=r"(_num), "=r"(_arg4) \ 11038c2ecf20Sopenharmony_ci : "0"(_num), \ 11048c2ecf20Sopenharmony_ci "r"(_arg1) \ 11058c2ecf20Sopenharmony_ci : "memory", "cc", "at", "v1", "hi", "lo", \ 11068c2ecf20Sopenharmony_ci "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \ 11078c2ecf20Sopenharmony_ci ); \ 11088c2ecf20Sopenharmony_ci _arg4 ? -_num : _num; \ 11098c2ecf20Sopenharmony_ci}) 11108c2ecf20Sopenharmony_ci 11118c2ecf20Sopenharmony_ci#define my_syscall2(num, arg1, arg2) \ 11128c2ecf20Sopenharmony_ci({ \ 11138c2ecf20Sopenharmony_ci register long _num asm("v0") = (num); \ 11148c2ecf20Sopenharmony_ci register long _arg1 asm("a0") = (long)(arg1); \ 11158c2ecf20Sopenharmony_ci register long _arg2 asm("a1") = (long)(arg2); \ 11168c2ecf20Sopenharmony_ci register long _arg4 asm("a3"); \ 11178c2ecf20Sopenharmony_ci \ 11188c2ecf20Sopenharmony_ci asm volatile ( \ 11198c2ecf20Sopenharmony_ci "addiu $sp, $sp, -32\n" \ 11208c2ecf20Sopenharmony_ci "syscall\n" \ 11218c2ecf20Sopenharmony_ci "addiu $sp, $sp, 32\n" \ 11228c2ecf20Sopenharmony_ci : "=r"(_num), "=r"(_arg4) \ 11238c2ecf20Sopenharmony_ci : "0"(_num), \ 11248c2ecf20Sopenharmony_ci "r"(_arg1), "r"(_arg2) \ 11258c2ecf20Sopenharmony_ci : "memory", "cc", "at", "v1", "hi", "lo", \ 11268c2ecf20Sopenharmony_ci "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \ 11278c2ecf20Sopenharmony_ci ); \ 11288c2ecf20Sopenharmony_ci _arg4 ? -_num : _num; \ 11298c2ecf20Sopenharmony_ci}) 11308c2ecf20Sopenharmony_ci 11318c2ecf20Sopenharmony_ci#define my_syscall3(num, arg1, arg2, arg3) \ 11328c2ecf20Sopenharmony_ci({ \ 11338c2ecf20Sopenharmony_ci register long _num asm("v0") = (num); \ 11348c2ecf20Sopenharmony_ci register long _arg1 asm("a0") = (long)(arg1); \ 11358c2ecf20Sopenharmony_ci register long _arg2 asm("a1") = (long)(arg2); \ 11368c2ecf20Sopenharmony_ci register long _arg3 asm("a2") = (long)(arg3); \ 11378c2ecf20Sopenharmony_ci register long _arg4 asm("a3"); \ 11388c2ecf20Sopenharmony_ci \ 11398c2ecf20Sopenharmony_ci asm volatile ( \ 11408c2ecf20Sopenharmony_ci "addiu $sp, $sp, -32\n" \ 11418c2ecf20Sopenharmony_ci "syscall\n" \ 11428c2ecf20Sopenharmony_ci "addiu $sp, $sp, 32\n" \ 11438c2ecf20Sopenharmony_ci : "=r"(_num), "=r"(_arg4) \ 11448c2ecf20Sopenharmony_ci : "0"(_num), \ 11458c2ecf20Sopenharmony_ci "r"(_arg1), "r"(_arg2), "r"(_arg3) \ 11468c2ecf20Sopenharmony_ci : "memory", "cc", "at", "v1", "hi", "lo", \ 11478c2ecf20Sopenharmony_ci "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \ 11488c2ecf20Sopenharmony_ci ); \ 11498c2ecf20Sopenharmony_ci _arg4 ? -_num : _num; \ 11508c2ecf20Sopenharmony_ci}) 11518c2ecf20Sopenharmony_ci 11528c2ecf20Sopenharmony_ci#define my_syscall4(num, arg1, arg2, arg3, arg4) \ 11538c2ecf20Sopenharmony_ci({ \ 11548c2ecf20Sopenharmony_ci register long _num asm("v0") = (num); \ 11558c2ecf20Sopenharmony_ci register long _arg1 asm("a0") = (long)(arg1); \ 11568c2ecf20Sopenharmony_ci register long _arg2 asm("a1") = (long)(arg2); \ 11578c2ecf20Sopenharmony_ci register long _arg3 asm("a2") = (long)(arg3); \ 11588c2ecf20Sopenharmony_ci register long _arg4 asm("a3") = (long)(arg4); \ 11598c2ecf20Sopenharmony_ci \ 11608c2ecf20Sopenharmony_ci asm volatile ( \ 11618c2ecf20Sopenharmony_ci "addiu $sp, $sp, -32\n" \ 11628c2ecf20Sopenharmony_ci "syscall\n" \ 11638c2ecf20Sopenharmony_ci "addiu $sp, $sp, 32\n" \ 11648c2ecf20Sopenharmony_ci : "=r" (_num), "=r"(_arg4) \ 11658c2ecf20Sopenharmony_ci : "0"(_num), \ 11668c2ecf20Sopenharmony_ci "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4) \ 11678c2ecf20Sopenharmony_ci : "memory", "cc", "at", "v1", "hi", "lo", \ 11688c2ecf20Sopenharmony_ci "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \ 11698c2ecf20Sopenharmony_ci ); \ 11708c2ecf20Sopenharmony_ci _arg4 ? -_num : _num; \ 11718c2ecf20Sopenharmony_ci}) 11728c2ecf20Sopenharmony_ci 11738c2ecf20Sopenharmony_ci#define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 11748c2ecf20Sopenharmony_ci({ \ 11758c2ecf20Sopenharmony_ci register long _num asm("v0") = (num); \ 11768c2ecf20Sopenharmony_ci register long _arg1 asm("a0") = (long)(arg1); \ 11778c2ecf20Sopenharmony_ci register long _arg2 asm("a1") = (long)(arg2); \ 11788c2ecf20Sopenharmony_ci register long _arg3 asm("a2") = (long)(arg3); \ 11798c2ecf20Sopenharmony_ci register long _arg4 asm("a3") = (long)(arg4); \ 11808c2ecf20Sopenharmony_ci register long _arg5 = (long)(arg5); \ 11818c2ecf20Sopenharmony_ci \ 11828c2ecf20Sopenharmony_ci asm volatile ( \ 11838c2ecf20Sopenharmony_ci "addiu $sp, $sp, -32\n" \ 11848c2ecf20Sopenharmony_ci "sw %7, 16($sp)\n" \ 11858c2ecf20Sopenharmony_ci "syscall\n " \ 11868c2ecf20Sopenharmony_ci "addiu $sp, $sp, 32\n" \ 11878c2ecf20Sopenharmony_ci : "=r" (_num), "=r"(_arg4) \ 11888c2ecf20Sopenharmony_ci : "0"(_num), \ 11898c2ecf20Sopenharmony_ci "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5) \ 11908c2ecf20Sopenharmony_ci : "memory", "cc", "at", "v1", "hi", "lo", \ 11918c2ecf20Sopenharmony_ci "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9" \ 11928c2ecf20Sopenharmony_ci ); \ 11938c2ecf20Sopenharmony_ci _arg4 ? -_num : _num; \ 11948c2ecf20Sopenharmony_ci}) 11958c2ecf20Sopenharmony_ci 11968c2ecf20Sopenharmony_ci/* startup code, note that it's called __start on MIPS */ 11978c2ecf20Sopenharmony_ciasm(".section .text\n" 11988c2ecf20Sopenharmony_ci ".set nomips16\n" 11998c2ecf20Sopenharmony_ci ".global __start\n" 12008c2ecf20Sopenharmony_ci ".set noreorder\n" 12018c2ecf20Sopenharmony_ci ".option pic0\n" 12028c2ecf20Sopenharmony_ci ".ent __start\n" 12038c2ecf20Sopenharmony_ci "__start:\n" 12048c2ecf20Sopenharmony_ci "lw $a0,($sp)\n" // argc was in the stack 12058c2ecf20Sopenharmony_ci "addiu $a1, $sp, 4\n" // argv = sp + 4 12068c2ecf20Sopenharmony_ci "sll $a2, $a0, 2\n" // a2 = argc * 4 12078c2ecf20Sopenharmony_ci "add $a2, $a2, $a1\n" // envp = argv + 4*argc ... 12088c2ecf20Sopenharmony_ci "addiu $a2, $a2, 4\n" // ... + 4 12098c2ecf20Sopenharmony_ci "li $t0, -8\n" 12108c2ecf20Sopenharmony_ci "and $sp, $sp, $t0\n" // sp must be 8-byte aligned 12118c2ecf20Sopenharmony_ci "addiu $sp,$sp,-16\n" // the callee expects to save a0..a3 there! 12128c2ecf20Sopenharmony_ci "jal main\n" // main() returns the status code, we'll exit with it. 12138c2ecf20Sopenharmony_ci "nop\n" // delayed slot 12148c2ecf20Sopenharmony_ci "move $a0, $v0\n" // retrieve 32-bit exit code from v0 12158c2ecf20Sopenharmony_ci "li $v0, 4001\n" // NR_exit == 4001 12168c2ecf20Sopenharmony_ci "syscall\n" 12178c2ecf20Sopenharmony_ci ".end __start\n" 12188c2ecf20Sopenharmony_ci ""); 12198c2ecf20Sopenharmony_ci 12208c2ecf20Sopenharmony_ci/* fcntl / open */ 12218c2ecf20Sopenharmony_ci#define O_RDONLY 0 12228c2ecf20Sopenharmony_ci#define O_WRONLY 1 12238c2ecf20Sopenharmony_ci#define O_RDWR 2 12248c2ecf20Sopenharmony_ci#define O_APPEND 0x0008 12258c2ecf20Sopenharmony_ci#define O_NONBLOCK 0x0080 12268c2ecf20Sopenharmony_ci#define O_CREAT 0x0100 12278c2ecf20Sopenharmony_ci#define O_TRUNC 0x0200 12288c2ecf20Sopenharmony_ci#define O_EXCL 0x0400 12298c2ecf20Sopenharmony_ci#define O_NOCTTY 0x0800 12308c2ecf20Sopenharmony_ci#define O_DIRECTORY 0x10000 12318c2ecf20Sopenharmony_ci 12328c2ecf20Sopenharmony_ci/* The struct returned by the stat() syscall. 88 bytes are returned by the 12338c2ecf20Sopenharmony_ci * syscall. 12348c2ecf20Sopenharmony_ci */ 12358c2ecf20Sopenharmony_cistruct sys_stat_struct { 12368c2ecf20Sopenharmony_ci unsigned int st_dev; 12378c2ecf20Sopenharmony_ci long st_pad1[3]; 12388c2ecf20Sopenharmony_ci unsigned long st_ino; 12398c2ecf20Sopenharmony_ci unsigned int st_mode; 12408c2ecf20Sopenharmony_ci unsigned int st_nlink; 12418c2ecf20Sopenharmony_ci unsigned int st_uid; 12428c2ecf20Sopenharmony_ci unsigned int st_gid; 12438c2ecf20Sopenharmony_ci unsigned int st_rdev; 12448c2ecf20Sopenharmony_ci long st_pad2[2]; 12458c2ecf20Sopenharmony_ci long st_size; 12468c2ecf20Sopenharmony_ci long st_pad3; 12478c2ecf20Sopenharmony_ci long st_atime; 12488c2ecf20Sopenharmony_ci long st_atime_nsec; 12498c2ecf20Sopenharmony_ci long st_mtime; 12508c2ecf20Sopenharmony_ci long st_mtime_nsec; 12518c2ecf20Sopenharmony_ci long st_ctime; 12528c2ecf20Sopenharmony_ci long st_ctime_nsec; 12538c2ecf20Sopenharmony_ci long st_blksize; 12548c2ecf20Sopenharmony_ci long st_blocks; 12558c2ecf20Sopenharmony_ci long st_pad4[14]; 12568c2ecf20Sopenharmony_ci}; 12578c2ecf20Sopenharmony_ci 12588c2ecf20Sopenharmony_ci#elif defined(__riscv) 12598c2ecf20Sopenharmony_ci 12608c2ecf20Sopenharmony_ci#if __riscv_xlen == 64 12618c2ecf20Sopenharmony_ci#define PTRLOG "3" 12628c2ecf20Sopenharmony_ci#define SZREG "8" 12638c2ecf20Sopenharmony_ci#elif __riscv_xlen == 32 12648c2ecf20Sopenharmony_ci#define PTRLOG "2" 12658c2ecf20Sopenharmony_ci#define SZREG "4" 12668c2ecf20Sopenharmony_ci#endif 12678c2ecf20Sopenharmony_ci 12688c2ecf20Sopenharmony_ci/* Syscalls for RISCV : 12698c2ecf20Sopenharmony_ci * - stack is 16-byte aligned 12708c2ecf20Sopenharmony_ci * - syscall number is passed in a7 12718c2ecf20Sopenharmony_ci * - arguments are in a0, a1, a2, a3, a4, a5 12728c2ecf20Sopenharmony_ci * - the system call is performed by calling ecall 12738c2ecf20Sopenharmony_ci * - syscall return comes in a0 12748c2ecf20Sopenharmony_ci * - the arguments are cast to long and assigned into the target 12758c2ecf20Sopenharmony_ci * registers which are then simply passed as registers to the asm code, 12768c2ecf20Sopenharmony_ci * so that we don't have to experience issues with register constraints. 12778c2ecf20Sopenharmony_ci */ 12788c2ecf20Sopenharmony_ci 12798c2ecf20Sopenharmony_ci#define my_syscall0(num) \ 12808c2ecf20Sopenharmony_ci({ \ 12818c2ecf20Sopenharmony_ci register long _num asm("a7") = (num); \ 12828c2ecf20Sopenharmony_ci register long _arg1 asm("a0"); \ 12838c2ecf20Sopenharmony_ci \ 12848c2ecf20Sopenharmony_ci asm volatile ( \ 12858c2ecf20Sopenharmony_ci "ecall\n\t" \ 12868c2ecf20Sopenharmony_ci : "=r"(_arg1) \ 12878c2ecf20Sopenharmony_ci : "r"(_num) \ 12888c2ecf20Sopenharmony_ci : "memory", "cc" \ 12898c2ecf20Sopenharmony_ci ); \ 12908c2ecf20Sopenharmony_ci _arg1; \ 12918c2ecf20Sopenharmony_ci}) 12928c2ecf20Sopenharmony_ci 12938c2ecf20Sopenharmony_ci#define my_syscall1(num, arg1) \ 12948c2ecf20Sopenharmony_ci({ \ 12958c2ecf20Sopenharmony_ci register long _num asm("a7") = (num); \ 12968c2ecf20Sopenharmony_ci register long _arg1 asm("a0") = (long)(arg1); \ 12978c2ecf20Sopenharmony_ci \ 12988c2ecf20Sopenharmony_ci asm volatile ( \ 12998c2ecf20Sopenharmony_ci "ecall\n" \ 13008c2ecf20Sopenharmony_ci : "+r"(_arg1) \ 13018c2ecf20Sopenharmony_ci : "r"(_num) \ 13028c2ecf20Sopenharmony_ci : "memory", "cc" \ 13038c2ecf20Sopenharmony_ci ); \ 13048c2ecf20Sopenharmony_ci _arg1; \ 13058c2ecf20Sopenharmony_ci}) 13068c2ecf20Sopenharmony_ci 13078c2ecf20Sopenharmony_ci#define my_syscall2(num, arg1, arg2) \ 13088c2ecf20Sopenharmony_ci({ \ 13098c2ecf20Sopenharmony_ci register long _num asm("a7") = (num); \ 13108c2ecf20Sopenharmony_ci register long _arg1 asm("a0") = (long)(arg1); \ 13118c2ecf20Sopenharmony_ci register long _arg2 asm("a1") = (long)(arg2); \ 13128c2ecf20Sopenharmony_ci \ 13138c2ecf20Sopenharmony_ci asm volatile ( \ 13148c2ecf20Sopenharmony_ci "ecall\n" \ 13158c2ecf20Sopenharmony_ci : "+r"(_arg1) \ 13168c2ecf20Sopenharmony_ci : "r"(_arg2), \ 13178c2ecf20Sopenharmony_ci "r"(_num) \ 13188c2ecf20Sopenharmony_ci : "memory", "cc" \ 13198c2ecf20Sopenharmony_ci ); \ 13208c2ecf20Sopenharmony_ci _arg1; \ 13218c2ecf20Sopenharmony_ci}) 13228c2ecf20Sopenharmony_ci 13238c2ecf20Sopenharmony_ci#define my_syscall3(num, arg1, arg2, arg3) \ 13248c2ecf20Sopenharmony_ci({ \ 13258c2ecf20Sopenharmony_ci register long _num asm("a7") = (num); \ 13268c2ecf20Sopenharmony_ci register long _arg1 asm("a0") = (long)(arg1); \ 13278c2ecf20Sopenharmony_ci register long _arg2 asm("a1") = (long)(arg2); \ 13288c2ecf20Sopenharmony_ci register long _arg3 asm("a2") = (long)(arg3); \ 13298c2ecf20Sopenharmony_ci \ 13308c2ecf20Sopenharmony_ci asm volatile ( \ 13318c2ecf20Sopenharmony_ci "ecall\n\t" \ 13328c2ecf20Sopenharmony_ci : "+r"(_arg1) \ 13338c2ecf20Sopenharmony_ci : "r"(_arg2), "r"(_arg3), \ 13348c2ecf20Sopenharmony_ci "r"(_num) \ 13358c2ecf20Sopenharmony_ci : "memory", "cc" \ 13368c2ecf20Sopenharmony_ci ); \ 13378c2ecf20Sopenharmony_ci _arg1; \ 13388c2ecf20Sopenharmony_ci}) 13398c2ecf20Sopenharmony_ci 13408c2ecf20Sopenharmony_ci#define my_syscall4(num, arg1, arg2, arg3, arg4) \ 13418c2ecf20Sopenharmony_ci({ \ 13428c2ecf20Sopenharmony_ci register long _num asm("a7") = (num); \ 13438c2ecf20Sopenharmony_ci register long _arg1 asm("a0") = (long)(arg1); \ 13448c2ecf20Sopenharmony_ci register long _arg2 asm("a1") = (long)(arg2); \ 13458c2ecf20Sopenharmony_ci register long _arg3 asm("a2") = (long)(arg3); \ 13468c2ecf20Sopenharmony_ci register long _arg4 asm("a3") = (long)(arg4); \ 13478c2ecf20Sopenharmony_ci \ 13488c2ecf20Sopenharmony_ci asm volatile ( \ 13498c2ecf20Sopenharmony_ci "ecall\n" \ 13508c2ecf20Sopenharmony_ci : "+r"(_arg1) \ 13518c2ecf20Sopenharmony_ci : "r"(_arg2), "r"(_arg3), "r"(_arg4), \ 13528c2ecf20Sopenharmony_ci "r"(_num) \ 13538c2ecf20Sopenharmony_ci : "memory", "cc" \ 13548c2ecf20Sopenharmony_ci ); \ 13558c2ecf20Sopenharmony_ci _arg1; \ 13568c2ecf20Sopenharmony_ci}) 13578c2ecf20Sopenharmony_ci 13588c2ecf20Sopenharmony_ci#define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 13598c2ecf20Sopenharmony_ci({ \ 13608c2ecf20Sopenharmony_ci register long _num asm("a7") = (num); \ 13618c2ecf20Sopenharmony_ci register long _arg1 asm("a0") = (long)(arg1); \ 13628c2ecf20Sopenharmony_ci register long _arg2 asm("a1") = (long)(arg2); \ 13638c2ecf20Sopenharmony_ci register long _arg3 asm("a2") = (long)(arg3); \ 13648c2ecf20Sopenharmony_ci register long _arg4 asm("a3") = (long)(arg4); \ 13658c2ecf20Sopenharmony_ci register long _arg5 asm("a4") = (long)(arg5); \ 13668c2ecf20Sopenharmony_ci \ 13678c2ecf20Sopenharmony_ci asm volatile ( \ 13688c2ecf20Sopenharmony_ci "ecall\n" \ 13698c2ecf20Sopenharmony_ci : "+r"(_arg1) \ 13708c2ecf20Sopenharmony_ci : "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 13718c2ecf20Sopenharmony_ci "r"(_num) \ 13728c2ecf20Sopenharmony_ci : "memory", "cc" \ 13738c2ecf20Sopenharmony_ci ); \ 13748c2ecf20Sopenharmony_ci _arg1; \ 13758c2ecf20Sopenharmony_ci}) 13768c2ecf20Sopenharmony_ci 13778c2ecf20Sopenharmony_ci#define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 13788c2ecf20Sopenharmony_ci({ \ 13798c2ecf20Sopenharmony_ci register long _num asm("a7") = (num); \ 13808c2ecf20Sopenharmony_ci register long _arg1 asm("a0") = (long)(arg1); \ 13818c2ecf20Sopenharmony_ci register long _arg2 asm("a1") = (long)(arg2); \ 13828c2ecf20Sopenharmony_ci register long _arg3 asm("a2") = (long)(arg3); \ 13838c2ecf20Sopenharmony_ci register long _arg4 asm("a3") = (long)(arg4); \ 13848c2ecf20Sopenharmony_ci register long _arg5 asm("a4") = (long)(arg5); \ 13858c2ecf20Sopenharmony_ci register long _arg6 asm("a5") = (long)(arg6); \ 13868c2ecf20Sopenharmony_ci \ 13878c2ecf20Sopenharmony_ci asm volatile ( \ 13888c2ecf20Sopenharmony_ci "ecall\n" \ 13898c2ecf20Sopenharmony_ci : "+r"(_arg1) \ 13908c2ecf20Sopenharmony_ci : "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), "r"(_arg6), \ 13918c2ecf20Sopenharmony_ci "r"(_num) \ 13928c2ecf20Sopenharmony_ci : "memory", "cc" \ 13938c2ecf20Sopenharmony_ci ); \ 13948c2ecf20Sopenharmony_ci _arg1; \ 13958c2ecf20Sopenharmony_ci}) 13968c2ecf20Sopenharmony_ci 13978c2ecf20Sopenharmony_ci/* startup code */ 13988c2ecf20Sopenharmony_ciasm(".section .text\n" 13998c2ecf20Sopenharmony_ci ".global _start\n" 14008c2ecf20Sopenharmony_ci "_start:\n" 14018c2ecf20Sopenharmony_ci ".option push\n" 14028c2ecf20Sopenharmony_ci ".option norelax\n" 14038c2ecf20Sopenharmony_ci "lla gp, __global_pointer$\n" 14048c2ecf20Sopenharmony_ci ".option pop\n" 14058c2ecf20Sopenharmony_ci "ld a0, 0(sp)\n" // argc (a0) was in the stack 14068c2ecf20Sopenharmony_ci "add a1, sp, "SZREG"\n" // argv (a1) = sp 14078c2ecf20Sopenharmony_ci "slli a2, a0, "PTRLOG"\n" // envp (a2) = SZREG*argc ... 14088c2ecf20Sopenharmony_ci "add a2, a2, "SZREG"\n" // + SZREG (skip null) 14098c2ecf20Sopenharmony_ci "add a2,a2,a1\n" // + argv 14108c2ecf20Sopenharmony_ci "andi sp,a1,-16\n" // sp must be 16-byte aligned 14118c2ecf20Sopenharmony_ci "call main\n" // main() returns the status code, we'll exit with it. 14128c2ecf20Sopenharmony_ci "li a7, 93\n" // NR_exit == 93 14138c2ecf20Sopenharmony_ci "ecall\n" 14148c2ecf20Sopenharmony_ci ""); 14158c2ecf20Sopenharmony_ci 14168c2ecf20Sopenharmony_ci/* fcntl / open */ 14178c2ecf20Sopenharmony_ci#define O_RDONLY 0 14188c2ecf20Sopenharmony_ci#define O_WRONLY 1 14198c2ecf20Sopenharmony_ci#define O_RDWR 2 14208c2ecf20Sopenharmony_ci#define O_CREAT 0x100 14218c2ecf20Sopenharmony_ci#define O_EXCL 0x200 14228c2ecf20Sopenharmony_ci#define O_NOCTTY 0x400 14238c2ecf20Sopenharmony_ci#define O_TRUNC 0x1000 14248c2ecf20Sopenharmony_ci#define O_APPEND 0x2000 14258c2ecf20Sopenharmony_ci#define O_NONBLOCK 0x4000 14268c2ecf20Sopenharmony_ci#define O_DIRECTORY 0x200000 14278c2ecf20Sopenharmony_ci 14288c2ecf20Sopenharmony_cistruct sys_stat_struct { 14298c2ecf20Sopenharmony_ci unsigned long st_dev; /* Device. */ 14308c2ecf20Sopenharmony_ci unsigned long st_ino; /* File serial number. */ 14318c2ecf20Sopenharmony_ci unsigned int st_mode; /* File mode. */ 14328c2ecf20Sopenharmony_ci unsigned int st_nlink; /* Link count. */ 14338c2ecf20Sopenharmony_ci unsigned int st_uid; /* User ID of the file's owner. */ 14348c2ecf20Sopenharmony_ci unsigned int st_gid; /* Group ID of the file's group. */ 14358c2ecf20Sopenharmony_ci unsigned long st_rdev; /* Device number, if device. */ 14368c2ecf20Sopenharmony_ci unsigned long __pad1; 14378c2ecf20Sopenharmony_ci long st_size; /* Size of file, in bytes. */ 14388c2ecf20Sopenharmony_ci int st_blksize; /* Optimal block size for I/O. */ 14398c2ecf20Sopenharmony_ci int __pad2; 14408c2ecf20Sopenharmony_ci long st_blocks; /* Number 512-byte blocks allocated. */ 14418c2ecf20Sopenharmony_ci long st_atime; /* Time of last access. */ 14428c2ecf20Sopenharmony_ci unsigned long st_atime_nsec; 14438c2ecf20Sopenharmony_ci long st_mtime; /* Time of last modification. */ 14448c2ecf20Sopenharmony_ci unsigned long st_mtime_nsec; 14458c2ecf20Sopenharmony_ci long st_ctime; /* Time of last status change. */ 14468c2ecf20Sopenharmony_ci unsigned long st_ctime_nsec; 14478c2ecf20Sopenharmony_ci unsigned int __unused4; 14488c2ecf20Sopenharmony_ci unsigned int __unused5; 14498c2ecf20Sopenharmony_ci}; 14508c2ecf20Sopenharmony_ci 14518c2ecf20Sopenharmony_ci#endif 14528c2ecf20Sopenharmony_ci 14538c2ecf20Sopenharmony_ci 14548c2ecf20Sopenharmony_ci/* Below are the C functions used to declare the raw syscalls. They try to be 14558c2ecf20Sopenharmony_ci * architecture-agnostic, and return either a success or -errno. Declaring them 14568c2ecf20Sopenharmony_ci * static will lead to them being inlined in most cases, but it's still possible 14578c2ecf20Sopenharmony_ci * to reference them by a pointer if needed. 14588c2ecf20Sopenharmony_ci */ 14598c2ecf20Sopenharmony_cistatic __attribute__((unused)) 14608c2ecf20Sopenharmony_civoid *sys_brk(void *addr) 14618c2ecf20Sopenharmony_ci{ 14628c2ecf20Sopenharmony_ci return (void *)my_syscall1(__NR_brk, addr); 14638c2ecf20Sopenharmony_ci} 14648c2ecf20Sopenharmony_ci 14658c2ecf20Sopenharmony_cistatic __attribute__((noreturn,unused)) 14668c2ecf20Sopenharmony_civoid sys_exit(int status) 14678c2ecf20Sopenharmony_ci{ 14688c2ecf20Sopenharmony_ci my_syscall1(__NR_exit, status & 255); 14698c2ecf20Sopenharmony_ci while(1); // shut the "noreturn" warnings. 14708c2ecf20Sopenharmony_ci} 14718c2ecf20Sopenharmony_ci 14728c2ecf20Sopenharmony_cistatic __attribute__((unused)) 14738c2ecf20Sopenharmony_ciint sys_chdir(const char *path) 14748c2ecf20Sopenharmony_ci{ 14758c2ecf20Sopenharmony_ci return my_syscall1(__NR_chdir, path); 14768c2ecf20Sopenharmony_ci} 14778c2ecf20Sopenharmony_ci 14788c2ecf20Sopenharmony_cistatic __attribute__((unused)) 14798c2ecf20Sopenharmony_ciint sys_chmod(const char *path, mode_t mode) 14808c2ecf20Sopenharmony_ci{ 14818c2ecf20Sopenharmony_ci#ifdef __NR_fchmodat 14828c2ecf20Sopenharmony_ci return my_syscall4(__NR_fchmodat, AT_FDCWD, path, mode, 0); 14838c2ecf20Sopenharmony_ci#else 14848c2ecf20Sopenharmony_ci return my_syscall2(__NR_chmod, path, mode); 14858c2ecf20Sopenharmony_ci#endif 14868c2ecf20Sopenharmony_ci} 14878c2ecf20Sopenharmony_ci 14888c2ecf20Sopenharmony_cistatic __attribute__((unused)) 14898c2ecf20Sopenharmony_ciint sys_chown(const char *path, uid_t owner, gid_t group) 14908c2ecf20Sopenharmony_ci{ 14918c2ecf20Sopenharmony_ci#ifdef __NR_fchownat 14928c2ecf20Sopenharmony_ci return my_syscall5(__NR_fchownat, AT_FDCWD, path, owner, group, 0); 14938c2ecf20Sopenharmony_ci#else 14948c2ecf20Sopenharmony_ci return my_syscall3(__NR_chown, path, owner, group); 14958c2ecf20Sopenharmony_ci#endif 14968c2ecf20Sopenharmony_ci} 14978c2ecf20Sopenharmony_ci 14988c2ecf20Sopenharmony_cistatic __attribute__((unused)) 14998c2ecf20Sopenharmony_ciint sys_chroot(const char *path) 15008c2ecf20Sopenharmony_ci{ 15018c2ecf20Sopenharmony_ci return my_syscall1(__NR_chroot, path); 15028c2ecf20Sopenharmony_ci} 15038c2ecf20Sopenharmony_ci 15048c2ecf20Sopenharmony_cistatic __attribute__((unused)) 15058c2ecf20Sopenharmony_ciint sys_close(int fd) 15068c2ecf20Sopenharmony_ci{ 15078c2ecf20Sopenharmony_ci return my_syscall1(__NR_close, fd); 15088c2ecf20Sopenharmony_ci} 15098c2ecf20Sopenharmony_ci 15108c2ecf20Sopenharmony_cistatic __attribute__((unused)) 15118c2ecf20Sopenharmony_ciint sys_dup(int fd) 15128c2ecf20Sopenharmony_ci{ 15138c2ecf20Sopenharmony_ci return my_syscall1(__NR_dup, fd); 15148c2ecf20Sopenharmony_ci} 15158c2ecf20Sopenharmony_ci 15168c2ecf20Sopenharmony_cistatic __attribute__((unused)) 15178c2ecf20Sopenharmony_ciint sys_dup2(int old, int new) 15188c2ecf20Sopenharmony_ci{ 15198c2ecf20Sopenharmony_ci return my_syscall2(__NR_dup2, old, new); 15208c2ecf20Sopenharmony_ci} 15218c2ecf20Sopenharmony_ci 15228c2ecf20Sopenharmony_cistatic __attribute__((unused)) 15238c2ecf20Sopenharmony_ciint sys_execve(const char *filename, char *const argv[], char *const envp[]) 15248c2ecf20Sopenharmony_ci{ 15258c2ecf20Sopenharmony_ci return my_syscall3(__NR_execve, filename, argv, envp); 15268c2ecf20Sopenharmony_ci} 15278c2ecf20Sopenharmony_ci 15288c2ecf20Sopenharmony_cistatic __attribute__((unused)) 15298c2ecf20Sopenharmony_cipid_t sys_fork(void) 15308c2ecf20Sopenharmony_ci{ 15318c2ecf20Sopenharmony_ci return my_syscall0(__NR_fork); 15328c2ecf20Sopenharmony_ci} 15338c2ecf20Sopenharmony_ci 15348c2ecf20Sopenharmony_cistatic __attribute__((unused)) 15358c2ecf20Sopenharmony_ciint sys_fsync(int fd) 15368c2ecf20Sopenharmony_ci{ 15378c2ecf20Sopenharmony_ci return my_syscall1(__NR_fsync, fd); 15388c2ecf20Sopenharmony_ci} 15398c2ecf20Sopenharmony_ci 15408c2ecf20Sopenharmony_cistatic __attribute__((unused)) 15418c2ecf20Sopenharmony_ciint sys_getdents64(int fd, struct linux_dirent64 *dirp, int count) 15428c2ecf20Sopenharmony_ci{ 15438c2ecf20Sopenharmony_ci return my_syscall3(__NR_getdents64, fd, dirp, count); 15448c2ecf20Sopenharmony_ci} 15458c2ecf20Sopenharmony_ci 15468c2ecf20Sopenharmony_cistatic __attribute__((unused)) 15478c2ecf20Sopenharmony_cipid_t sys_getpgrp(void) 15488c2ecf20Sopenharmony_ci{ 15498c2ecf20Sopenharmony_ci return my_syscall0(__NR_getpgrp); 15508c2ecf20Sopenharmony_ci} 15518c2ecf20Sopenharmony_ci 15528c2ecf20Sopenharmony_cistatic __attribute__((unused)) 15538c2ecf20Sopenharmony_cipid_t sys_getpid(void) 15548c2ecf20Sopenharmony_ci{ 15558c2ecf20Sopenharmony_ci return my_syscall0(__NR_getpid); 15568c2ecf20Sopenharmony_ci} 15578c2ecf20Sopenharmony_ci 15588c2ecf20Sopenharmony_cistatic __attribute__((unused)) 15598c2ecf20Sopenharmony_ciint sys_gettimeofday(struct timeval *tv, struct timezone *tz) 15608c2ecf20Sopenharmony_ci{ 15618c2ecf20Sopenharmony_ci return my_syscall2(__NR_gettimeofday, tv, tz); 15628c2ecf20Sopenharmony_ci} 15638c2ecf20Sopenharmony_ci 15648c2ecf20Sopenharmony_cistatic __attribute__((unused)) 15658c2ecf20Sopenharmony_ciint sys_ioctl(int fd, unsigned long req, void *value) 15668c2ecf20Sopenharmony_ci{ 15678c2ecf20Sopenharmony_ci return my_syscall3(__NR_ioctl, fd, req, value); 15688c2ecf20Sopenharmony_ci} 15698c2ecf20Sopenharmony_ci 15708c2ecf20Sopenharmony_cistatic __attribute__((unused)) 15718c2ecf20Sopenharmony_ciint sys_kill(pid_t pid, int signal) 15728c2ecf20Sopenharmony_ci{ 15738c2ecf20Sopenharmony_ci return my_syscall2(__NR_kill, pid, signal); 15748c2ecf20Sopenharmony_ci} 15758c2ecf20Sopenharmony_ci 15768c2ecf20Sopenharmony_cistatic __attribute__((unused)) 15778c2ecf20Sopenharmony_ciint sys_link(const char *old, const char *new) 15788c2ecf20Sopenharmony_ci{ 15798c2ecf20Sopenharmony_ci#ifdef __NR_linkat 15808c2ecf20Sopenharmony_ci return my_syscall5(__NR_linkat, AT_FDCWD, old, AT_FDCWD, new, 0); 15818c2ecf20Sopenharmony_ci#else 15828c2ecf20Sopenharmony_ci return my_syscall2(__NR_link, old, new); 15838c2ecf20Sopenharmony_ci#endif 15848c2ecf20Sopenharmony_ci} 15858c2ecf20Sopenharmony_ci 15868c2ecf20Sopenharmony_cistatic __attribute__((unused)) 15878c2ecf20Sopenharmony_cioff_t sys_lseek(int fd, off_t offset, int whence) 15888c2ecf20Sopenharmony_ci{ 15898c2ecf20Sopenharmony_ci return my_syscall3(__NR_lseek, fd, offset, whence); 15908c2ecf20Sopenharmony_ci} 15918c2ecf20Sopenharmony_ci 15928c2ecf20Sopenharmony_cistatic __attribute__((unused)) 15938c2ecf20Sopenharmony_ciint sys_mkdir(const char *path, mode_t mode) 15948c2ecf20Sopenharmony_ci{ 15958c2ecf20Sopenharmony_ci#ifdef __NR_mkdirat 15968c2ecf20Sopenharmony_ci return my_syscall3(__NR_mkdirat, AT_FDCWD, path, mode); 15978c2ecf20Sopenharmony_ci#else 15988c2ecf20Sopenharmony_ci return my_syscall2(__NR_mkdir, path, mode); 15998c2ecf20Sopenharmony_ci#endif 16008c2ecf20Sopenharmony_ci} 16018c2ecf20Sopenharmony_ci 16028c2ecf20Sopenharmony_cistatic __attribute__((unused)) 16038c2ecf20Sopenharmony_cilong sys_mknod(const char *path, mode_t mode, dev_t dev) 16048c2ecf20Sopenharmony_ci{ 16058c2ecf20Sopenharmony_ci#ifdef __NR_mknodat 16068c2ecf20Sopenharmony_ci return my_syscall4(__NR_mknodat, AT_FDCWD, path, mode, dev); 16078c2ecf20Sopenharmony_ci#else 16088c2ecf20Sopenharmony_ci return my_syscall3(__NR_mknod, path, mode, dev); 16098c2ecf20Sopenharmony_ci#endif 16108c2ecf20Sopenharmony_ci} 16118c2ecf20Sopenharmony_ci 16128c2ecf20Sopenharmony_cistatic __attribute__((unused)) 16138c2ecf20Sopenharmony_ciint sys_mount(const char *src, const char *tgt, const char *fst, 16148c2ecf20Sopenharmony_ci unsigned long flags, const void *data) 16158c2ecf20Sopenharmony_ci{ 16168c2ecf20Sopenharmony_ci return my_syscall5(__NR_mount, src, tgt, fst, flags, data); 16178c2ecf20Sopenharmony_ci} 16188c2ecf20Sopenharmony_ci 16198c2ecf20Sopenharmony_cistatic __attribute__((unused)) 16208c2ecf20Sopenharmony_ciint sys_open(const char *path, int flags, mode_t mode) 16218c2ecf20Sopenharmony_ci{ 16228c2ecf20Sopenharmony_ci#ifdef __NR_openat 16238c2ecf20Sopenharmony_ci return my_syscall4(__NR_openat, AT_FDCWD, path, flags, mode); 16248c2ecf20Sopenharmony_ci#else 16258c2ecf20Sopenharmony_ci return my_syscall3(__NR_open, path, flags, mode); 16268c2ecf20Sopenharmony_ci#endif 16278c2ecf20Sopenharmony_ci} 16288c2ecf20Sopenharmony_ci 16298c2ecf20Sopenharmony_cistatic __attribute__((unused)) 16308c2ecf20Sopenharmony_ciint sys_pivot_root(const char *new, const char *old) 16318c2ecf20Sopenharmony_ci{ 16328c2ecf20Sopenharmony_ci return my_syscall2(__NR_pivot_root, new, old); 16338c2ecf20Sopenharmony_ci} 16348c2ecf20Sopenharmony_ci 16358c2ecf20Sopenharmony_cistatic __attribute__((unused)) 16368c2ecf20Sopenharmony_ciint sys_poll(struct pollfd *fds, int nfds, int timeout) 16378c2ecf20Sopenharmony_ci{ 16388c2ecf20Sopenharmony_ci return my_syscall3(__NR_poll, fds, nfds, timeout); 16398c2ecf20Sopenharmony_ci} 16408c2ecf20Sopenharmony_ci 16418c2ecf20Sopenharmony_cistatic __attribute__((unused)) 16428c2ecf20Sopenharmony_cissize_t sys_read(int fd, void *buf, size_t count) 16438c2ecf20Sopenharmony_ci{ 16448c2ecf20Sopenharmony_ci return my_syscall3(__NR_read, fd, buf, count); 16458c2ecf20Sopenharmony_ci} 16468c2ecf20Sopenharmony_ci 16478c2ecf20Sopenharmony_cistatic __attribute__((unused)) 16488c2ecf20Sopenharmony_cissize_t sys_reboot(int magic1, int magic2, int cmd, void *arg) 16498c2ecf20Sopenharmony_ci{ 16508c2ecf20Sopenharmony_ci return my_syscall4(__NR_reboot, magic1, magic2, cmd, arg); 16518c2ecf20Sopenharmony_ci} 16528c2ecf20Sopenharmony_ci 16538c2ecf20Sopenharmony_cistatic __attribute__((unused)) 16548c2ecf20Sopenharmony_ciint sys_sched_yield(void) 16558c2ecf20Sopenharmony_ci{ 16568c2ecf20Sopenharmony_ci return my_syscall0(__NR_sched_yield); 16578c2ecf20Sopenharmony_ci} 16588c2ecf20Sopenharmony_ci 16598c2ecf20Sopenharmony_cistatic __attribute__((unused)) 16608c2ecf20Sopenharmony_ciint sys_select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout) 16618c2ecf20Sopenharmony_ci{ 16628c2ecf20Sopenharmony_ci#if defined(__ARCH_WANT_SYS_OLD_SELECT) && !defined(__NR__newselect) 16638c2ecf20Sopenharmony_ci struct sel_arg_struct { 16648c2ecf20Sopenharmony_ci unsigned long n; 16658c2ecf20Sopenharmony_ci fd_set *r, *w, *e; 16668c2ecf20Sopenharmony_ci struct timeval *t; 16678c2ecf20Sopenharmony_ci } arg = { .n = nfds, .r = rfds, .w = wfds, .e = efds, .t = timeout }; 16688c2ecf20Sopenharmony_ci return my_syscall1(__NR_select, &arg); 16698c2ecf20Sopenharmony_ci#elif defined(__ARCH_WANT_SYS_PSELECT6) && defined(__NR_pselect6) 16708c2ecf20Sopenharmony_ci struct timespec t; 16718c2ecf20Sopenharmony_ci 16728c2ecf20Sopenharmony_ci if (timeout) { 16738c2ecf20Sopenharmony_ci t.tv_sec = timeout->tv_sec; 16748c2ecf20Sopenharmony_ci t.tv_nsec = timeout->tv_usec * 1000; 16758c2ecf20Sopenharmony_ci } 16768c2ecf20Sopenharmony_ci return my_syscall6(__NR_pselect6, nfds, rfds, wfds, efds, timeout ? &t : NULL, NULL); 16778c2ecf20Sopenharmony_ci#else 16788c2ecf20Sopenharmony_ci#ifndef __NR__newselect 16798c2ecf20Sopenharmony_ci#define __NR__newselect __NR_select 16808c2ecf20Sopenharmony_ci#endif 16818c2ecf20Sopenharmony_ci return my_syscall5(__NR__newselect, nfds, rfds, wfds, efds, timeout); 16828c2ecf20Sopenharmony_ci#endif 16838c2ecf20Sopenharmony_ci} 16848c2ecf20Sopenharmony_ci 16858c2ecf20Sopenharmony_cistatic __attribute__((unused)) 16868c2ecf20Sopenharmony_ciint sys_setpgid(pid_t pid, pid_t pgid) 16878c2ecf20Sopenharmony_ci{ 16888c2ecf20Sopenharmony_ci return my_syscall2(__NR_setpgid, pid, pgid); 16898c2ecf20Sopenharmony_ci} 16908c2ecf20Sopenharmony_ci 16918c2ecf20Sopenharmony_cistatic __attribute__((unused)) 16928c2ecf20Sopenharmony_cipid_t sys_setsid(void) 16938c2ecf20Sopenharmony_ci{ 16948c2ecf20Sopenharmony_ci return my_syscall0(__NR_setsid); 16958c2ecf20Sopenharmony_ci} 16968c2ecf20Sopenharmony_ci 16978c2ecf20Sopenharmony_cistatic __attribute__((unused)) 16988c2ecf20Sopenharmony_ciint sys_stat(const char *path, struct stat *buf) 16998c2ecf20Sopenharmony_ci{ 17008c2ecf20Sopenharmony_ci struct sys_stat_struct stat; 17018c2ecf20Sopenharmony_ci long ret; 17028c2ecf20Sopenharmony_ci 17038c2ecf20Sopenharmony_ci#ifdef __NR_newfstatat 17048c2ecf20Sopenharmony_ci /* only solution for arm64 */ 17058c2ecf20Sopenharmony_ci ret = my_syscall4(__NR_newfstatat, AT_FDCWD, path, &stat, 0); 17068c2ecf20Sopenharmony_ci#else 17078c2ecf20Sopenharmony_ci ret = my_syscall2(__NR_stat, path, &stat); 17088c2ecf20Sopenharmony_ci#endif 17098c2ecf20Sopenharmony_ci buf->st_dev = stat.st_dev; 17108c2ecf20Sopenharmony_ci buf->st_ino = stat.st_ino; 17118c2ecf20Sopenharmony_ci buf->st_mode = stat.st_mode; 17128c2ecf20Sopenharmony_ci buf->st_nlink = stat.st_nlink; 17138c2ecf20Sopenharmony_ci buf->st_uid = stat.st_uid; 17148c2ecf20Sopenharmony_ci buf->st_gid = stat.st_gid; 17158c2ecf20Sopenharmony_ci buf->st_rdev = stat.st_rdev; 17168c2ecf20Sopenharmony_ci buf->st_size = stat.st_size; 17178c2ecf20Sopenharmony_ci buf->st_blksize = stat.st_blksize; 17188c2ecf20Sopenharmony_ci buf->st_blocks = stat.st_blocks; 17198c2ecf20Sopenharmony_ci buf->st_atime = stat.st_atime; 17208c2ecf20Sopenharmony_ci buf->st_mtime = stat.st_mtime; 17218c2ecf20Sopenharmony_ci buf->st_ctime = stat.st_ctime; 17228c2ecf20Sopenharmony_ci return ret; 17238c2ecf20Sopenharmony_ci} 17248c2ecf20Sopenharmony_ci 17258c2ecf20Sopenharmony_ci 17268c2ecf20Sopenharmony_cistatic __attribute__((unused)) 17278c2ecf20Sopenharmony_ciint sys_symlink(const char *old, const char *new) 17288c2ecf20Sopenharmony_ci{ 17298c2ecf20Sopenharmony_ci#ifdef __NR_symlinkat 17308c2ecf20Sopenharmony_ci return my_syscall3(__NR_symlinkat, old, AT_FDCWD, new); 17318c2ecf20Sopenharmony_ci#else 17328c2ecf20Sopenharmony_ci return my_syscall2(__NR_symlink, old, new); 17338c2ecf20Sopenharmony_ci#endif 17348c2ecf20Sopenharmony_ci} 17358c2ecf20Sopenharmony_ci 17368c2ecf20Sopenharmony_cistatic __attribute__((unused)) 17378c2ecf20Sopenharmony_cimode_t sys_umask(mode_t mode) 17388c2ecf20Sopenharmony_ci{ 17398c2ecf20Sopenharmony_ci return my_syscall1(__NR_umask, mode); 17408c2ecf20Sopenharmony_ci} 17418c2ecf20Sopenharmony_ci 17428c2ecf20Sopenharmony_cistatic __attribute__((unused)) 17438c2ecf20Sopenharmony_ciint sys_umount2(const char *path, int flags) 17448c2ecf20Sopenharmony_ci{ 17458c2ecf20Sopenharmony_ci return my_syscall2(__NR_umount2, path, flags); 17468c2ecf20Sopenharmony_ci} 17478c2ecf20Sopenharmony_ci 17488c2ecf20Sopenharmony_cistatic __attribute__((unused)) 17498c2ecf20Sopenharmony_ciint sys_unlink(const char *path) 17508c2ecf20Sopenharmony_ci{ 17518c2ecf20Sopenharmony_ci#ifdef __NR_unlinkat 17528c2ecf20Sopenharmony_ci return my_syscall3(__NR_unlinkat, AT_FDCWD, path, 0); 17538c2ecf20Sopenharmony_ci#else 17548c2ecf20Sopenharmony_ci return my_syscall1(__NR_unlink, path); 17558c2ecf20Sopenharmony_ci#endif 17568c2ecf20Sopenharmony_ci} 17578c2ecf20Sopenharmony_ci 17588c2ecf20Sopenharmony_cistatic __attribute__((unused)) 17598c2ecf20Sopenharmony_cipid_t sys_wait4(pid_t pid, int *status, int options, struct rusage *rusage) 17608c2ecf20Sopenharmony_ci{ 17618c2ecf20Sopenharmony_ci return my_syscall4(__NR_wait4, pid, status, options, rusage); 17628c2ecf20Sopenharmony_ci} 17638c2ecf20Sopenharmony_ci 17648c2ecf20Sopenharmony_cistatic __attribute__((unused)) 17658c2ecf20Sopenharmony_cipid_t sys_waitpid(pid_t pid, int *status, int options) 17668c2ecf20Sopenharmony_ci{ 17678c2ecf20Sopenharmony_ci return sys_wait4(pid, status, options, 0); 17688c2ecf20Sopenharmony_ci} 17698c2ecf20Sopenharmony_ci 17708c2ecf20Sopenharmony_cistatic __attribute__((unused)) 17718c2ecf20Sopenharmony_cipid_t sys_wait(int *status) 17728c2ecf20Sopenharmony_ci{ 17738c2ecf20Sopenharmony_ci return sys_waitpid(-1, status, 0); 17748c2ecf20Sopenharmony_ci} 17758c2ecf20Sopenharmony_ci 17768c2ecf20Sopenharmony_cistatic __attribute__((unused)) 17778c2ecf20Sopenharmony_cissize_t sys_write(int fd, const void *buf, size_t count) 17788c2ecf20Sopenharmony_ci{ 17798c2ecf20Sopenharmony_ci return my_syscall3(__NR_write, fd, buf, count); 17808c2ecf20Sopenharmony_ci} 17818c2ecf20Sopenharmony_ci 17828c2ecf20Sopenharmony_ci 17838c2ecf20Sopenharmony_ci/* Below are the libc-compatible syscalls which return x or -1 and set errno. 17848c2ecf20Sopenharmony_ci * They rely on the functions above. Similarly they're marked static so that it 17858c2ecf20Sopenharmony_ci * is possible to assign pointers to them if needed. 17868c2ecf20Sopenharmony_ci */ 17878c2ecf20Sopenharmony_ci 17888c2ecf20Sopenharmony_cistatic __attribute__((unused)) 17898c2ecf20Sopenharmony_ciint brk(void *addr) 17908c2ecf20Sopenharmony_ci{ 17918c2ecf20Sopenharmony_ci void *ret = sys_brk(addr); 17928c2ecf20Sopenharmony_ci 17938c2ecf20Sopenharmony_ci if (!ret) { 17948c2ecf20Sopenharmony_ci SET_ERRNO(ENOMEM); 17958c2ecf20Sopenharmony_ci return -1; 17968c2ecf20Sopenharmony_ci } 17978c2ecf20Sopenharmony_ci return 0; 17988c2ecf20Sopenharmony_ci} 17998c2ecf20Sopenharmony_ci 18008c2ecf20Sopenharmony_cistatic __attribute__((noreturn,unused)) 18018c2ecf20Sopenharmony_civoid exit(int status) 18028c2ecf20Sopenharmony_ci{ 18038c2ecf20Sopenharmony_ci sys_exit(status); 18048c2ecf20Sopenharmony_ci} 18058c2ecf20Sopenharmony_ci 18068c2ecf20Sopenharmony_cistatic __attribute__((unused)) 18078c2ecf20Sopenharmony_ciint chdir(const char *path) 18088c2ecf20Sopenharmony_ci{ 18098c2ecf20Sopenharmony_ci int ret = sys_chdir(path); 18108c2ecf20Sopenharmony_ci 18118c2ecf20Sopenharmony_ci if (ret < 0) { 18128c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 18138c2ecf20Sopenharmony_ci ret = -1; 18148c2ecf20Sopenharmony_ci } 18158c2ecf20Sopenharmony_ci return ret; 18168c2ecf20Sopenharmony_ci} 18178c2ecf20Sopenharmony_ci 18188c2ecf20Sopenharmony_cistatic __attribute__((unused)) 18198c2ecf20Sopenharmony_ciint chmod(const char *path, mode_t mode) 18208c2ecf20Sopenharmony_ci{ 18218c2ecf20Sopenharmony_ci int ret = sys_chmod(path, mode); 18228c2ecf20Sopenharmony_ci 18238c2ecf20Sopenharmony_ci if (ret < 0) { 18248c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 18258c2ecf20Sopenharmony_ci ret = -1; 18268c2ecf20Sopenharmony_ci } 18278c2ecf20Sopenharmony_ci return ret; 18288c2ecf20Sopenharmony_ci} 18298c2ecf20Sopenharmony_ci 18308c2ecf20Sopenharmony_cistatic __attribute__((unused)) 18318c2ecf20Sopenharmony_ciint chown(const char *path, uid_t owner, gid_t group) 18328c2ecf20Sopenharmony_ci{ 18338c2ecf20Sopenharmony_ci int ret = sys_chown(path, owner, group); 18348c2ecf20Sopenharmony_ci 18358c2ecf20Sopenharmony_ci if (ret < 0) { 18368c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 18378c2ecf20Sopenharmony_ci ret = -1; 18388c2ecf20Sopenharmony_ci } 18398c2ecf20Sopenharmony_ci return ret; 18408c2ecf20Sopenharmony_ci} 18418c2ecf20Sopenharmony_ci 18428c2ecf20Sopenharmony_cistatic __attribute__((unused)) 18438c2ecf20Sopenharmony_ciint chroot(const char *path) 18448c2ecf20Sopenharmony_ci{ 18458c2ecf20Sopenharmony_ci int ret = sys_chroot(path); 18468c2ecf20Sopenharmony_ci 18478c2ecf20Sopenharmony_ci if (ret < 0) { 18488c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 18498c2ecf20Sopenharmony_ci ret = -1; 18508c2ecf20Sopenharmony_ci } 18518c2ecf20Sopenharmony_ci return ret; 18528c2ecf20Sopenharmony_ci} 18538c2ecf20Sopenharmony_ci 18548c2ecf20Sopenharmony_cistatic __attribute__((unused)) 18558c2ecf20Sopenharmony_ciint close(int fd) 18568c2ecf20Sopenharmony_ci{ 18578c2ecf20Sopenharmony_ci int ret = sys_close(fd); 18588c2ecf20Sopenharmony_ci 18598c2ecf20Sopenharmony_ci if (ret < 0) { 18608c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 18618c2ecf20Sopenharmony_ci ret = -1; 18628c2ecf20Sopenharmony_ci } 18638c2ecf20Sopenharmony_ci return ret; 18648c2ecf20Sopenharmony_ci} 18658c2ecf20Sopenharmony_ci 18668c2ecf20Sopenharmony_cistatic __attribute__((unused)) 18678c2ecf20Sopenharmony_ciint dup2(int old, int new) 18688c2ecf20Sopenharmony_ci{ 18698c2ecf20Sopenharmony_ci int ret = sys_dup2(old, new); 18708c2ecf20Sopenharmony_ci 18718c2ecf20Sopenharmony_ci if (ret < 0) { 18728c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 18738c2ecf20Sopenharmony_ci ret = -1; 18748c2ecf20Sopenharmony_ci } 18758c2ecf20Sopenharmony_ci return ret; 18768c2ecf20Sopenharmony_ci} 18778c2ecf20Sopenharmony_ci 18788c2ecf20Sopenharmony_cistatic __attribute__((unused)) 18798c2ecf20Sopenharmony_ciint execve(const char *filename, char *const argv[], char *const envp[]) 18808c2ecf20Sopenharmony_ci{ 18818c2ecf20Sopenharmony_ci int ret = sys_execve(filename, argv, envp); 18828c2ecf20Sopenharmony_ci 18838c2ecf20Sopenharmony_ci if (ret < 0) { 18848c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 18858c2ecf20Sopenharmony_ci ret = -1; 18868c2ecf20Sopenharmony_ci } 18878c2ecf20Sopenharmony_ci return ret; 18888c2ecf20Sopenharmony_ci} 18898c2ecf20Sopenharmony_ci 18908c2ecf20Sopenharmony_cistatic __attribute__((unused)) 18918c2ecf20Sopenharmony_cipid_t fork(void) 18928c2ecf20Sopenharmony_ci{ 18938c2ecf20Sopenharmony_ci pid_t ret = sys_fork(); 18948c2ecf20Sopenharmony_ci 18958c2ecf20Sopenharmony_ci if (ret < 0) { 18968c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 18978c2ecf20Sopenharmony_ci ret = -1; 18988c2ecf20Sopenharmony_ci } 18998c2ecf20Sopenharmony_ci return ret; 19008c2ecf20Sopenharmony_ci} 19018c2ecf20Sopenharmony_ci 19028c2ecf20Sopenharmony_cistatic __attribute__((unused)) 19038c2ecf20Sopenharmony_ciint fsync(int fd) 19048c2ecf20Sopenharmony_ci{ 19058c2ecf20Sopenharmony_ci int ret = sys_fsync(fd); 19068c2ecf20Sopenharmony_ci 19078c2ecf20Sopenharmony_ci if (ret < 0) { 19088c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 19098c2ecf20Sopenharmony_ci ret = -1; 19108c2ecf20Sopenharmony_ci } 19118c2ecf20Sopenharmony_ci return ret; 19128c2ecf20Sopenharmony_ci} 19138c2ecf20Sopenharmony_ci 19148c2ecf20Sopenharmony_cistatic __attribute__((unused)) 19158c2ecf20Sopenharmony_ciint getdents64(int fd, struct linux_dirent64 *dirp, int count) 19168c2ecf20Sopenharmony_ci{ 19178c2ecf20Sopenharmony_ci int ret = sys_getdents64(fd, dirp, count); 19188c2ecf20Sopenharmony_ci 19198c2ecf20Sopenharmony_ci if (ret < 0) { 19208c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 19218c2ecf20Sopenharmony_ci ret = -1; 19228c2ecf20Sopenharmony_ci } 19238c2ecf20Sopenharmony_ci return ret; 19248c2ecf20Sopenharmony_ci} 19258c2ecf20Sopenharmony_ci 19268c2ecf20Sopenharmony_cistatic __attribute__((unused)) 19278c2ecf20Sopenharmony_cipid_t getpgrp(void) 19288c2ecf20Sopenharmony_ci{ 19298c2ecf20Sopenharmony_ci pid_t ret = sys_getpgrp(); 19308c2ecf20Sopenharmony_ci 19318c2ecf20Sopenharmony_ci if (ret < 0) { 19328c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 19338c2ecf20Sopenharmony_ci ret = -1; 19348c2ecf20Sopenharmony_ci } 19358c2ecf20Sopenharmony_ci return ret; 19368c2ecf20Sopenharmony_ci} 19378c2ecf20Sopenharmony_ci 19388c2ecf20Sopenharmony_cistatic __attribute__((unused)) 19398c2ecf20Sopenharmony_cipid_t getpid(void) 19408c2ecf20Sopenharmony_ci{ 19418c2ecf20Sopenharmony_ci pid_t ret = sys_getpid(); 19428c2ecf20Sopenharmony_ci 19438c2ecf20Sopenharmony_ci if (ret < 0) { 19448c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 19458c2ecf20Sopenharmony_ci ret = -1; 19468c2ecf20Sopenharmony_ci } 19478c2ecf20Sopenharmony_ci return ret; 19488c2ecf20Sopenharmony_ci} 19498c2ecf20Sopenharmony_ci 19508c2ecf20Sopenharmony_cistatic __attribute__((unused)) 19518c2ecf20Sopenharmony_ciint gettimeofday(struct timeval *tv, struct timezone *tz) 19528c2ecf20Sopenharmony_ci{ 19538c2ecf20Sopenharmony_ci int ret = sys_gettimeofday(tv, tz); 19548c2ecf20Sopenharmony_ci 19558c2ecf20Sopenharmony_ci if (ret < 0) { 19568c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 19578c2ecf20Sopenharmony_ci ret = -1; 19588c2ecf20Sopenharmony_ci } 19598c2ecf20Sopenharmony_ci return ret; 19608c2ecf20Sopenharmony_ci} 19618c2ecf20Sopenharmony_ci 19628c2ecf20Sopenharmony_cistatic __attribute__((unused)) 19638c2ecf20Sopenharmony_ciint ioctl(int fd, unsigned long req, void *value) 19648c2ecf20Sopenharmony_ci{ 19658c2ecf20Sopenharmony_ci int ret = sys_ioctl(fd, req, value); 19668c2ecf20Sopenharmony_ci 19678c2ecf20Sopenharmony_ci if (ret < 0) { 19688c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 19698c2ecf20Sopenharmony_ci ret = -1; 19708c2ecf20Sopenharmony_ci } 19718c2ecf20Sopenharmony_ci return ret; 19728c2ecf20Sopenharmony_ci} 19738c2ecf20Sopenharmony_ci 19748c2ecf20Sopenharmony_cistatic __attribute__((unused)) 19758c2ecf20Sopenharmony_ciint kill(pid_t pid, int signal) 19768c2ecf20Sopenharmony_ci{ 19778c2ecf20Sopenharmony_ci int ret = sys_kill(pid, signal); 19788c2ecf20Sopenharmony_ci 19798c2ecf20Sopenharmony_ci if (ret < 0) { 19808c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 19818c2ecf20Sopenharmony_ci ret = -1; 19828c2ecf20Sopenharmony_ci } 19838c2ecf20Sopenharmony_ci return ret; 19848c2ecf20Sopenharmony_ci} 19858c2ecf20Sopenharmony_ci 19868c2ecf20Sopenharmony_cistatic __attribute__((unused)) 19878c2ecf20Sopenharmony_ciint link(const char *old, const char *new) 19888c2ecf20Sopenharmony_ci{ 19898c2ecf20Sopenharmony_ci int ret = sys_link(old, new); 19908c2ecf20Sopenharmony_ci 19918c2ecf20Sopenharmony_ci if (ret < 0) { 19928c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 19938c2ecf20Sopenharmony_ci ret = -1; 19948c2ecf20Sopenharmony_ci } 19958c2ecf20Sopenharmony_ci return ret; 19968c2ecf20Sopenharmony_ci} 19978c2ecf20Sopenharmony_ci 19988c2ecf20Sopenharmony_cistatic __attribute__((unused)) 19998c2ecf20Sopenharmony_cioff_t lseek(int fd, off_t offset, int whence) 20008c2ecf20Sopenharmony_ci{ 20018c2ecf20Sopenharmony_ci off_t ret = sys_lseek(fd, offset, whence); 20028c2ecf20Sopenharmony_ci 20038c2ecf20Sopenharmony_ci if (ret < 0) { 20048c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 20058c2ecf20Sopenharmony_ci ret = -1; 20068c2ecf20Sopenharmony_ci } 20078c2ecf20Sopenharmony_ci return ret; 20088c2ecf20Sopenharmony_ci} 20098c2ecf20Sopenharmony_ci 20108c2ecf20Sopenharmony_cistatic __attribute__((unused)) 20118c2ecf20Sopenharmony_ciint mkdir(const char *path, mode_t mode) 20128c2ecf20Sopenharmony_ci{ 20138c2ecf20Sopenharmony_ci int ret = sys_mkdir(path, mode); 20148c2ecf20Sopenharmony_ci 20158c2ecf20Sopenharmony_ci if (ret < 0) { 20168c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 20178c2ecf20Sopenharmony_ci ret = -1; 20188c2ecf20Sopenharmony_ci } 20198c2ecf20Sopenharmony_ci return ret; 20208c2ecf20Sopenharmony_ci} 20218c2ecf20Sopenharmony_ci 20228c2ecf20Sopenharmony_cistatic __attribute__((unused)) 20238c2ecf20Sopenharmony_ciint mknod(const char *path, mode_t mode, dev_t dev) 20248c2ecf20Sopenharmony_ci{ 20258c2ecf20Sopenharmony_ci int ret = sys_mknod(path, mode, dev); 20268c2ecf20Sopenharmony_ci 20278c2ecf20Sopenharmony_ci if (ret < 0) { 20288c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 20298c2ecf20Sopenharmony_ci ret = -1; 20308c2ecf20Sopenharmony_ci } 20318c2ecf20Sopenharmony_ci return ret; 20328c2ecf20Sopenharmony_ci} 20338c2ecf20Sopenharmony_ci 20348c2ecf20Sopenharmony_cistatic __attribute__((unused)) 20358c2ecf20Sopenharmony_ciint mount(const char *src, const char *tgt, 20368c2ecf20Sopenharmony_ci const char *fst, unsigned long flags, 20378c2ecf20Sopenharmony_ci const void *data) 20388c2ecf20Sopenharmony_ci{ 20398c2ecf20Sopenharmony_ci int ret = sys_mount(src, tgt, fst, flags, data); 20408c2ecf20Sopenharmony_ci 20418c2ecf20Sopenharmony_ci if (ret < 0) { 20428c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 20438c2ecf20Sopenharmony_ci ret = -1; 20448c2ecf20Sopenharmony_ci } 20458c2ecf20Sopenharmony_ci return ret; 20468c2ecf20Sopenharmony_ci} 20478c2ecf20Sopenharmony_ci 20488c2ecf20Sopenharmony_cistatic __attribute__((unused)) 20498c2ecf20Sopenharmony_ciint open(const char *path, int flags, mode_t mode) 20508c2ecf20Sopenharmony_ci{ 20518c2ecf20Sopenharmony_ci int ret = sys_open(path, flags, mode); 20528c2ecf20Sopenharmony_ci 20538c2ecf20Sopenharmony_ci if (ret < 0) { 20548c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 20558c2ecf20Sopenharmony_ci ret = -1; 20568c2ecf20Sopenharmony_ci } 20578c2ecf20Sopenharmony_ci return ret; 20588c2ecf20Sopenharmony_ci} 20598c2ecf20Sopenharmony_ci 20608c2ecf20Sopenharmony_cistatic __attribute__((unused)) 20618c2ecf20Sopenharmony_ciint pivot_root(const char *new, const char *old) 20628c2ecf20Sopenharmony_ci{ 20638c2ecf20Sopenharmony_ci int ret = sys_pivot_root(new, old); 20648c2ecf20Sopenharmony_ci 20658c2ecf20Sopenharmony_ci if (ret < 0) { 20668c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 20678c2ecf20Sopenharmony_ci ret = -1; 20688c2ecf20Sopenharmony_ci } 20698c2ecf20Sopenharmony_ci return ret; 20708c2ecf20Sopenharmony_ci} 20718c2ecf20Sopenharmony_ci 20728c2ecf20Sopenharmony_cistatic __attribute__((unused)) 20738c2ecf20Sopenharmony_ciint poll(struct pollfd *fds, int nfds, int timeout) 20748c2ecf20Sopenharmony_ci{ 20758c2ecf20Sopenharmony_ci int ret = sys_poll(fds, nfds, timeout); 20768c2ecf20Sopenharmony_ci 20778c2ecf20Sopenharmony_ci if (ret < 0) { 20788c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 20798c2ecf20Sopenharmony_ci ret = -1; 20808c2ecf20Sopenharmony_ci } 20818c2ecf20Sopenharmony_ci return ret; 20828c2ecf20Sopenharmony_ci} 20838c2ecf20Sopenharmony_ci 20848c2ecf20Sopenharmony_cistatic __attribute__((unused)) 20858c2ecf20Sopenharmony_cissize_t read(int fd, void *buf, size_t count) 20868c2ecf20Sopenharmony_ci{ 20878c2ecf20Sopenharmony_ci ssize_t ret = sys_read(fd, buf, count); 20888c2ecf20Sopenharmony_ci 20898c2ecf20Sopenharmony_ci if (ret < 0) { 20908c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 20918c2ecf20Sopenharmony_ci ret = -1; 20928c2ecf20Sopenharmony_ci } 20938c2ecf20Sopenharmony_ci return ret; 20948c2ecf20Sopenharmony_ci} 20958c2ecf20Sopenharmony_ci 20968c2ecf20Sopenharmony_cistatic __attribute__((unused)) 20978c2ecf20Sopenharmony_ciint reboot(int cmd) 20988c2ecf20Sopenharmony_ci{ 20998c2ecf20Sopenharmony_ci int ret = sys_reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, cmd, 0); 21008c2ecf20Sopenharmony_ci 21018c2ecf20Sopenharmony_ci if (ret < 0) { 21028c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 21038c2ecf20Sopenharmony_ci ret = -1; 21048c2ecf20Sopenharmony_ci } 21058c2ecf20Sopenharmony_ci return ret; 21068c2ecf20Sopenharmony_ci} 21078c2ecf20Sopenharmony_ci 21088c2ecf20Sopenharmony_cistatic __attribute__((unused)) 21098c2ecf20Sopenharmony_civoid *sbrk(intptr_t inc) 21108c2ecf20Sopenharmony_ci{ 21118c2ecf20Sopenharmony_ci void *ret; 21128c2ecf20Sopenharmony_ci 21138c2ecf20Sopenharmony_ci /* first call to find current end */ 21148c2ecf20Sopenharmony_ci if ((ret = sys_brk(0)) && (sys_brk(ret + inc) == ret + inc)) 21158c2ecf20Sopenharmony_ci return ret + inc; 21168c2ecf20Sopenharmony_ci 21178c2ecf20Sopenharmony_ci SET_ERRNO(ENOMEM); 21188c2ecf20Sopenharmony_ci return (void *)-1; 21198c2ecf20Sopenharmony_ci} 21208c2ecf20Sopenharmony_ci 21218c2ecf20Sopenharmony_cistatic __attribute__((unused)) 21228c2ecf20Sopenharmony_ciint sched_yield(void) 21238c2ecf20Sopenharmony_ci{ 21248c2ecf20Sopenharmony_ci int ret = sys_sched_yield(); 21258c2ecf20Sopenharmony_ci 21268c2ecf20Sopenharmony_ci if (ret < 0) { 21278c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 21288c2ecf20Sopenharmony_ci ret = -1; 21298c2ecf20Sopenharmony_ci } 21308c2ecf20Sopenharmony_ci return ret; 21318c2ecf20Sopenharmony_ci} 21328c2ecf20Sopenharmony_ci 21338c2ecf20Sopenharmony_cistatic __attribute__((unused)) 21348c2ecf20Sopenharmony_ciint select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout) 21358c2ecf20Sopenharmony_ci{ 21368c2ecf20Sopenharmony_ci int ret = sys_select(nfds, rfds, wfds, efds, timeout); 21378c2ecf20Sopenharmony_ci 21388c2ecf20Sopenharmony_ci if (ret < 0) { 21398c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 21408c2ecf20Sopenharmony_ci ret = -1; 21418c2ecf20Sopenharmony_ci } 21428c2ecf20Sopenharmony_ci return ret; 21438c2ecf20Sopenharmony_ci} 21448c2ecf20Sopenharmony_ci 21458c2ecf20Sopenharmony_cistatic __attribute__((unused)) 21468c2ecf20Sopenharmony_ciint setpgid(pid_t pid, pid_t pgid) 21478c2ecf20Sopenharmony_ci{ 21488c2ecf20Sopenharmony_ci int ret = sys_setpgid(pid, pgid); 21498c2ecf20Sopenharmony_ci 21508c2ecf20Sopenharmony_ci if (ret < 0) { 21518c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 21528c2ecf20Sopenharmony_ci ret = -1; 21538c2ecf20Sopenharmony_ci } 21548c2ecf20Sopenharmony_ci return ret; 21558c2ecf20Sopenharmony_ci} 21568c2ecf20Sopenharmony_ci 21578c2ecf20Sopenharmony_cistatic __attribute__((unused)) 21588c2ecf20Sopenharmony_cipid_t setsid(void) 21598c2ecf20Sopenharmony_ci{ 21608c2ecf20Sopenharmony_ci pid_t ret = sys_setsid(); 21618c2ecf20Sopenharmony_ci 21628c2ecf20Sopenharmony_ci if (ret < 0) { 21638c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 21648c2ecf20Sopenharmony_ci ret = -1; 21658c2ecf20Sopenharmony_ci } 21668c2ecf20Sopenharmony_ci return ret; 21678c2ecf20Sopenharmony_ci} 21688c2ecf20Sopenharmony_ci 21698c2ecf20Sopenharmony_cistatic __attribute__((unused)) 21708c2ecf20Sopenharmony_ciunsigned int sleep(unsigned int seconds) 21718c2ecf20Sopenharmony_ci{ 21728c2ecf20Sopenharmony_ci struct timeval my_timeval = { seconds, 0 }; 21738c2ecf20Sopenharmony_ci 21748c2ecf20Sopenharmony_ci if (sys_select(0, 0, 0, 0, &my_timeval) < 0) 21758c2ecf20Sopenharmony_ci return my_timeval.tv_sec + !!my_timeval.tv_usec; 21768c2ecf20Sopenharmony_ci else 21778c2ecf20Sopenharmony_ci return 0; 21788c2ecf20Sopenharmony_ci} 21798c2ecf20Sopenharmony_ci 21808c2ecf20Sopenharmony_cistatic __attribute__((unused)) 21818c2ecf20Sopenharmony_ciint stat(const char *path, struct stat *buf) 21828c2ecf20Sopenharmony_ci{ 21838c2ecf20Sopenharmony_ci int ret = sys_stat(path, buf); 21848c2ecf20Sopenharmony_ci 21858c2ecf20Sopenharmony_ci if (ret < 0) { 21868c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 21878c2ecf20Sopenharmony_ci ret = -1; 21888c2ecf20Sopenharmony_ci } 21898c2ecf20Sopenharmony_ci return ret; 21908c2ecf20Sopenharmony_ci} 21918c2ecf20Sopenharmony_ci 21928c2ecf20Sopenharmony_cistatic __attribute__((unused)) 21938c2ecf20Sopenharmony_ciint symlink(const char *old, const char *new) 21948c2ecf20Sopenharmony_ci{ 21958c2ecf20Sopenharmony_ci int ret = sys_symlink(old, new); 21968c2ecf20Sopenharmony_ci 21978c2ecf20Sopenharmony_ci if (ret < 0) { 21988c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 21998c2ecf20Sopenharmony_ci ret = -1; 22008c2ecf20Sopenharmony_ci } 22018c2ecf20Sopenharmony_ci return ret; 22028c2ecf20Sopenharmony_ci} 22038c2ecf20Sopenharmony_ci 22048c2ecf20Sopenharmony_cistatic __attribute__((unused)) 22058c2ecf20Sopenharmony_ciint tcsetpgrp(int fd, pid_t pid) 22068c2ecf20Sopenharmony_ci{ 22078c2ecf20Sopenharmony_ci return ioctl(fd, TIOCSPGRP, &pid); 22088c2ecf20Sopenharmony_ci} 22098c2ecf20Sopenharmony_ci 22108c2ecf20Sopenharmony_cistatic __attribute__((unused)) 22118c2ecf20Sopenharmony_cimode_t umask(mode_t mode) 22128c2ecf20Sopenharmony_ci{ 22138c2ecf20Sopenharmony_ci return sys_umask(mode); 22148c2ecf20Sopenharmony_ci} 22158c2ecf20Sopenharmony_ci 22168c2ecf20Sopenharmony_cistatic __attribute__((unused)) 22178c2ecf20Sopenharmony_ciint umount2(const char *path, int flags) 22188c2ecf20Sopenharmony_ci{ 22198c2ecf20Sopenharmony_ci int ret = sys_umount2(path, flags); 22208c2ecf20Sopenharmony_ci 22218c2ecf20Sopenharmony_ci if (ret < 0) { 22228c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 22238c2ecf20Sopenharmony_ci ret = -1; 22248c2ecf20Sopenharmony_ci } 22258c2ecf20Sopenharmony_ci return ret; 22268c2ecf20Sopenharmony_ci} 22278c2ecf20Sopenharmony_ci 22288c2ecf20Sopenharmony_cistatic __attribute__((unused)) 22298c2ecf20Sopenharmony_ciint unlink(const char *path) 22308c2ecf20Sopenharmony_ci{ 22318c2ecf20Sopenharmony_ci int ret = sys_unlink(path); 22328c2ecf20Sopenharmony_ci 22338c2ecf20Sopenharmony_ci if (ret < 0) { 22348c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 22358c2ecf20Sopenharmony_ci ret = -1; 22368c2ecf20Sopenharmony_ci } 22378c2ecf20Sopenharmony_ci return ret; 22388c2ecf20Sopenharmony_ci} 22398c2ecf20Sopenharmony_ci 22408c2ecf20Sopenharmony_cistatic __attribute__((unused)) 22418c2ecf20Sopenharmony_cipid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage) 22428c2ecf20Sopenharmony_ci{ 22438c2ecf20Sopenharmony_ci pid_t ret = sys_wait4(pid, status, options, rusage); 22448c2ecf20Sopenharmony_ci 22458c2ecf20Sopenharmony_ci if (ret < 0) { 22468c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 22478c2ecf20Sopenharmony_ci ret = -1; 22488c2ecf20Sopenharmony_ci } 22498c2ecf20Sopenharmony_ci return ret; 22508c2ecf20Sopenharmony_ci} 22518c2ecf20Sopenharmony_ci 22528c2ecf20Sopenharmony_cistatic __attribute__((unused)) 22538c2ecf20Sopenharmony_cipid_t waitpid(pid_t pid, int *status, int options) 22548c2ecf20Sopenharmony_ci{ 22558c2ecf20Sopenharmony_ci pid_t ret = sys_waitpid(pid, status, options); 22568c2ecf20Sopenharmony_ci 22578c2ecf20Sopenharmony_ci if (ret < 0) { 22588c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 22598c2ecf20Sopenharmony_ci ret = -1; 22608c2ecf20Sopenharmony_ci } 22618c2ecf20Sopenharmony_ci return ret; 22628c2ecf20Sopenharmony_ci} 22638c2ecf20Sopenharmony_ci 22648c2ecf20Sopenharmony_cistatic __attribute__((unused)) 22658c2ecf20Sopenharmony_cipid_t wait(int *status) 22668c2ecf20Sopenharmony_ci{ 22678c2ecf20Sopenharmony_ci pid_t ret = sys_wait(status); 22688c2ecf20Sopenharmony_ci 22698c2ecf20Sopenharmony_ci if (ret < 0) { 22708c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 22718c2ecf20Sopenharmony_ci ret = -1; 22728c2ecf20Sopenharmony_ci } 22738c2ecf20Sopenharmony_ci return ret; 22748c2ecf20Sopenharmony_ci} 22758c2ecf20Sopenharmony_ci 22768c2ecf20Sopenharmony_cistatic __attribute__((unused)) 22778c2ecf20Sopenharmony_cissize_t write(int fd, const void *buf, size_t count) 22788c2ecf20Sopenharmony_ci{ 22798c2ecf20Sopenharmony_ci ssize_t ret = sys_write(fd, buf, count); 22808c2ecf20Sopenharmony_ci 22818c2ecf20Sopenharmony_ci if (ret < 0) { 22828c2ecf20Sopenharmony_ci SET_ERRNO(-ret); 22838c2ecf20Sopenharmony_ci ret = -1; 22848c2ecf20Sopenharmony_ci } 22858c2ecf20Sopenharmony_ci return ret; 22868c2ecf20Sopenharmony_ci} 22878c2ecf20Sopenharmony_ci 22888c2ecf20Sopenharmony_ci/* some size-optimized reimplementations of a few common str* and mem* 22898c2ecf20Sopenharmony_ci * functions. They're marked static, except memcpy() and raise() which are used 22908c2ecf20Sopenharmony_ci * by libgcc on ARM, so they are marked weak instead in order not to cause an 22918c2ecf20Sopenharmony_ci * error when building a program made of multiple files (not recommended). 22928c2ecf20Sopenharmony_ci */ 22938c2ecf20Sopenharmony_ci 22948c2ecf20Sopenharmony_cistatic __attribute__((unused)) 22958c2ecf20Sopenharmony_civoid *memmove(void *dst, const void *src, size_t len) 22968c2ecf20Sopenharmony_ci{ 22978c2ecf20Sopenharmony_ci ssize_t pos = (dst <= src) ? -1 : (long)len; 22988c2ecf20Sopenharmony_ci void *ret = dst; 22998c2ecf20Sopenharmony_ci 23008c2ecf20Sopenharmony_ci while (len--) { 23018c2ecf20Sopenharmony_ci pos += (dst <= src) ? 1 : -1; 23028c2ecf20Sopenharmony_ci ((char *)dst)[pos] = ((char *)src)[pos]; 23038c2ecf20Sopenharmony_ci } 23048c2ecf20Sopenharmony_ci return ret; 23058c2ecf20Sopenharmony_ci} 23068c2ecf20Sopenharmony_ci 23078c2ecf20Sopenharmony_cistatic __attribute__((unused)) 23088c2ecf20Sopenharmony_civoid *memset(void *dst, int b, size_t len) 23098c2ecf20Sopenharmony_ci{ 23108c2ecf20Sopenharmony_ci char *p = dst; 23118c2ecf20Sopenharmony_ci 23128c2ecf20Sopenharmony_ci while (len--) 23138c2ecf20Sopenharmony_ci *(p++) = b; 23148c2ecf20Sopenharmony_ci return dst; 23158c2ecf20Sopenharmony_ci} 23168c2ecf20Sopenharmony_ci 23178c2ecf20Sopenharmony_cistatic __attribute__((unused)) 23188c2ecf20Sopenharmony_ciint memcmp(const void *s1, const void *s2, size_t n) 23198c2ecf20Sopenharmony_ci{ 23208c2ecf20Sopenharmony_ci size_t ofs = 0; 23218c2ecf20Sopenharmony_ci int c1 = 0; 23228c2ecf20Sopenharmony_ci 23238c2ecf20Sopenharmony_ci while (ofs < n && !(c1 = ((unsigned char *)s1)[ofs] - ((unsigned char *)s2)[ofs])) { 23248c2ecf20Sopenharmony_ci ofs++; 23258c2ecf20Sopenharmony_ci } 23268c2ecf20Sopenharmony_ci return c1; 23278c2ecf20Sopenharmony_ci} 23288c2ecf20Sopenharmony_ci 23298c2ecf20Sopenharmony_cistatic __attribute__((unused)) 23308c2ecf20Sopenharmony_cichar *strcpy(char *dst, const char *src) 23318c2ecf20Sopenharmony_ci{ 23328c2ecf20Sopenharmony_ci char *ret = dst; 23338c2ecf20Sopenharmony_ci 23348c2ecf20Sopenharmony_ci while ((*dst++ = *src++)); 23358c2ecf20Sopenharmony_ci return ret; 23368c2ecf20Sopenharmony_ci} 23378c2ecf20Sopenharmony_ci 23388c2ecf20Sopenharmony_cistatic __attribute__((unused)) 23398c2ecf20Sopenharmony_cichar *strchr(const char *s, int c) 23408c2ecf20Sopenharmony_ci{ 23418c2ecf20Sopenharmony_ci while (*s) { 23428c2ecf20Sopenharmony_ci if (*s == (char)c) 23438c2ecf20Sopenharmony_ci return (char *)s; 23448c2ecf20Sopenharmony_ci s++; 23458c2ecf20Sopenharmony_ci } 23468c2ecf20Sopenharmony_ci return NULL; 23478c2ecf20Sopenharmony_ci} 23488c2ecf20Sopenharmony_ci 23498c2ecf20Sopenharmony_cistatic __attribute__((unused)) 23508c2ecf20Sopenharmony_cichar *strrchr(const char *s, int c) 23518c2ecf20Sopenharmony_ci{ 23528c2ecf20Sopenharmony_ci const char *ret = NULL; 23538c2ecf20Sopenharmony_ci 23548c2ecf20Sopenharmony_ci while (*s) { 23558c2ecf20Sopenharmony_ci if (*s == (char)c) 23568c2ecf20Sopenharmony_ci ret = s; 23578c2ecf20Sopenharmony_ci s++; 23588c2ecf20Sopenharmony_ci } 23598c2ecf20Sopenharmony_ci return (char *)ret; 23608c2ecf20Sopenharmony_ci} 23618c2ecf20Sopenharmony_ci 23628c2ecf20Sopenharmony_cistatic __attribute__((unused)) 23638c2ecf20Sopenharmony_cisize_t nolibc_strlen(const char *str) 23648c2ecf20Sopenharmony_ci{ 23658c2ecf20Sopenharmony_ci size_t len; 23668c2ecf20Sopenharmony_ci 23678c2ecf20Sopenharmony_ci for (len = 0; str[len]; len++); 23688c2ecf20Sopenharmony_ci return len; 23698c2ecf20Sopenharmony_ci} 23708c2ecf20Sopenharmony_ci 23718c2ecf20Sopenharmony_ci#define strlen(str) ({ \ 23728c2ecf20Sopenharmony_ci __builtin_constant_p((str)) ? \ 23738c2ecf20Sopenharmony_ci __builtin_strlen((str)) : \ 23748c2ecf20Sopenharmony_ci nolibc_strlen((str)); \ 23758c2ecf20Sopenharmony_ci}) 23768c2ecf20Sopenharmony_ci 23778c2ecf20Sopenharmony_cistatic __attribute__((unused)) 23788c2ecf20Sopenharmony_ciint isdigit(int c) 23798c2ecf20Sopenharmony_ci{ 23808c2ecf20Sopenharmony_ci return (unsigned int)(c - '0') <= 9; 23818c2ecf20Sopenharmony_ci} 23828c2ecf20Sopenharmony_ci 23838c2ecf20Sopenharmony_cistatic __attribute__((unused)) 23848c2ecf20Sopenharmony_cilong atol(const char *s) 23858c2ecf20Sopenharmony_ci{ 23868c2ecf20Sopenharmony_ci unsigned long ret = 0; 23878c2ecf20Sopenharmony_ci unsigned long d; 23888c2ecf20Sopenharmony_ci int neg = 0; 23898c2ecf20Sopenharmony_ci 23908c2ecf20Sopenharmony_ci if (*s == '-') { 23918c2ecf20Sopenharmony_ci neg = 1; 23928c2ecf20Sopenharmony_ci s++; 23938c2ecf20Sopenharmony_ci } 23948c2ecf20Sopenharmony_ci 23958c2ecf20Sopenharmony_ci while (1) { 23968c2ecf20Sopenharmony_ci d = (*s++) - '0'; 23978c2ecf20Sopenharmony_ci if (d > 9) 23988c2ecf20Sopenharmony_ci break; 23998c2ecf20Sopenharmony_ci ret *= 10; 24008c2ecf20Sopenharmony_ci ret += d; 24018c2ecf20Sopenharmony_ci } 24028c2ecf20Sopenharmony_ci 24038c2ecf20Sopenharmony_ci return neg ? -ret : ret; 24048c2ecf20Sopenharmony_ci} 24058c2ecf20Sopenharmony_ci 24068c2ecf20Sopenharmony_cistatic __attribute__((unused)) 24078c2ecf20Sopenharmony_ciint atoi(const char *s) 24088c2ecf20Sopenharmony_ci{ 24098c2ecf20Sopenharmony_ci return atol(s); 24108c2ecf20Sopenharmony_ci} 24118c2ecf20Sopenharmony_ci 24128c2ecf20Sopenharmony_cistatic __attribute__((unused)) 24138c2ecf20Sopenharmony_ciconst char *ltoa(long in) 24148c2ecf20Sopenharmony_ci{ 24158c2ecf20Sopenharmony_ci /* large enough for -9223372036854775808 */ 24168c2ecf20Sopenharmony_ci static char buffer[21]; 24178c2ecf20Sopenharmony_ci char *pos = buffer + sizeof(buffer) - 1; 24188c2ecf20Sopenharmony_ci int neg = in < 0; 24198c2ecf20Sopenharmony_ci unsigned long n = neg ? -in : in; 24208c2ecf20Sopenharmony_ci 24218c2ecf20Sopenharmony_ci *pos-- = '\0'; 24228c2ecf20Sopenharmony_ci do { 24238c2ecf20Sopenharmony_ci *pos-- = '0' + n % 10; 24248c2ecf20Sopenharmony_ci n /= 10; 24258c2ecf20Sopenharmony_ci if (pos < buffer) 24268c2ecf20Sopenharmony_ci return pos + 1; 24278c2ecf20Sopenharmony_ci } while (n); 24288c2ecf20Sopenharmony_ci 24298c2ecf20Sopenharmony_ci if (neg) 24308c2ecf20Sopenharmony_ci *pos-- = '-'; 24318c2ecf20Sopenharmony_ci return pos + 1; 24328c2ecf20Sopenharmony_ci} 24338c2ecf20Sopenharmony_ci 24348c2ecf20Sopenharmony_ci__attribute__((weak,unused)) 24358c2ecf20Sopenharmony_civoid *memcpy(void *dst, const void *src, size_t len) 24368c2ecf20Sopenharmony_ci{ 24378c2ecf20Sopenharmony_ci return memmove(dst, src, len); 24388c2ecf20Sopenharmony_ci} 24398c2ecf20Sopenharmony_ci 24408c2ecf20Sopenharmony_ci/* needed by libgcc for divide by zero */ 24418c2ecf20Sopenharmony_ci__attribute__((weak,unused)) 24428c2ecf20Sopenharmony_ciint raise(int signal) 24438c2ecf20Sopenharmony_ci{ 24448c2ecf20Sopenharmony_ci return kill(getpid(), signal); 24458c2ecf20Sopenharmony_ci} 24468c2ecf20Sopenharmony_ci 24478c2ecf20Sopenharmony_ci/* Here come a few helper functions */ 24488c2ecf20Sopenharmony_ci 24498c2ecf20Sopenharmony_cistatic __attribute__((unused)) 24508c2ecf20Sopenharmony_civoid FD_ZERO(fd_set *set) 24518c2ecf20Sopenharmony_ci{ 24528c2ecf20Sopenharmony_ci memset(set, 0, sizeof(*set)); 24538c2ecf20Sopenharmony_ci} 24548c2ecf20Sopenharmony_ci 24558c2ecf20Sopenharmony_cistatic __attribute__((unused)) 24568c2ecf20Sopenharmony_civoid FD_SET(int fd, fd_set *set) 24578c2ecf20Sopenharmony_ci{ 24588c2ecf20Sopenharmony_ci if (fd < 0 || fd >= FD_SETSIZE) 24598c2ecf20Sopenharmony_ci return; 24608c2ecf20Sopenharmony_ci set->fd32[fd / 32] |= 1 << (fd & 31); 24618c2ecf20Sopenharmony_ci} 24628c2ecf20Sopenharmony_ci 24638c2ecf20Sopenharmony_ci/* WARNING, it only deals with the 4096 first majors and 256 first minors */ 24648c2ecf20Sopenharmony_cistatic __attribute__((unused)) 24658c2ecf20Sopenharmony_cidev_t makedev(unsigned int major, unsigned int minor) 24668c2ecf20Sopenharmony_ci{ 24678c2ecf20Sopenharmony_ci return ((major & 0xfff) << 8) | (minor & 0xff); 24688c2ecf20Sopenharmony_ci} 2469