From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from integral2.. (unknown [182.2.70.161]) by gnuweeb.org (Postfix) with ESMTPSA id 151CE7E734; Tue, 29 Mar 2022 10:18:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1648549085; bh=DJZFv4JotEMo5L83nTqipusJFM8ZYgW9v7ot8diz0vk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VQtJiwB00c4DkarcuHtgJpo5lMxauXI6ZY2BAAz027atQbihyJahtmO5ASq05SpoE ER3x8AqZZW9Y9A3fvGB15CFuVCrQV4XyqQnmfHXEJ1FURvektiK4j2H45gOiTdEBYG IimqHvHsTCdJqTS+3pShi5ZkhSbvAXYehNuUwpjVu0FLR7MxmD5DSd8O3Z/3u+VsEB A9/hbEPCnBmGEWuWkStjGKZ4GmjbArtmA/2E4Fux7IB+7JP9kR/txZuXAktrIcbSlu B36r1OxolWe0Sb7enkTZcTd44Ys++bsNJC/MVcDpSZnhaHFHOseDbsbNhWaqE4FzNM RT15qfrBhKR1Q== From: Ammar Faizi To: Willy Tarreau Cc: Ammar Faizi , Alviro Iskandar Setiawan , Nugraha , "Paul E. McKenney" , Linux Kernel Mailing List , GNU/Weeb Mailing List Subject: [PATCH v2 5/9] tools/nolibc/sys: Implement `mmap()` and `munmap()` Date: Tue, 29 Mar 2022 17:17:33 +0700 Message-Id: <20220329101737.58985-6-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220329101737.58985-1-ammarfaizi2@gnuweeb.org> References: <20220329101737.58985-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Implement mmap() and munmap(). Currently, they are only available for architecures that have my_syscall6 macro. For architectures that don't have, this function will return -1 with errno set to ENOSYS (Function not implemented). This has been tested on x86 and i386. Notes for i386: 1) The common mmap() syscall implementation uses __NR_mmap2 instead of __NR_mmap. 2) The offset must be shifted-right by 12-bit. Signed-off-by: Ammar Faizi === @@ Changelog: Link v1: https://lore.gnuweeb.org/gwml/20220324073039.140946-8-ammarfaizi2@gnuweeb.org v1 -> v2: * No changes * Link RFC v2: https://lore.kernel.org/lkml/20220322102115.186179-5-ammarfaizi2@gnuweeb.org RFC v2 -> v1: * No changes * Link RFC v1: https://lore.kernel.org/lkml/20220320093750.159991-5-ammarfaizi2@gnuweeb.org RFC v1 -> RFC v2: * No changes * --- tools/include/nolibc/sys.h | 62 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 4d4308d5d111..08491070387b 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -14,6 +14,7 @@ #include #include // for SIGCHLD #include +#include #include #include #include @@ -675,6 +676,67 @@ int mknod(const char *path, mode_t mode, dev_t dev) return ret; } +#ifndef MAP_SHARED +#define MAP_SHARED 0x01 /* Share changes */ +#define MAP_PRIVATE 0x02 /* Changes are private */ +#define MAP_SHARED_VALIDATE 0x03 /* share + validate extension flags */ +#endif + +#ifndef MAP_FAILED +#define MAP_FAILED ((void *)-1) +#endif + +static __attribute__((unused)) +void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd, + off_t offset) +{ +#ifndef my_syscall6 + /* Function not implemented. */ + return -ENOSYS; +#else + + int n; + +#if defined(__i386__) + n = __NR_mmap2; + offset >>= 12; +#else + n = __NR_mmap; +#endif + + return (void *)my_syscall6(n, addr, length, prot, flags, fd, offset); +#endif +} + +static __attribute__((unused)) +void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) +{ + void *ret = sys_mmap(addr, length, prot, flags, fd, offset); + + if ((unsigned long)ret >= -4095UL) { + SET_ERRNO(-(long)ret); + ret = MAP_FAILED; + } + return ret; +} + +static __attribute__((unused)) +int sys_munmap(void *addr, size_t length) +{ + return my_syscall2(__NR_munmap, addr, length); +} + +static __attribute__((unused)) +int munmap(void *addr, size_t length) +{ + int ret = sys_munmap(addr, length); + + if (ret < 0) { + SET_ERRNO(-ret); + ret = -1; + } + return ret; +} /* * int mount(const char *source, const char *target, -- Ammar Faizi