From 00b31986fb658ebd9a4693214544ad743e5fff0f Mon Sep 17 00:00:00 2001 From: Jean-Pierre Miceli Date: Tue, 7 Oct 2025 13:51:51 +0200 Subject: [PATCH 1/6] Add 'writev' syscall Signed-off-by: Jean-Pierre Miceli --- so3/arch/arm32/include/asm/syscall_number.h | 2 + so3/arch/arm64/include/asm/syscall_number.h | 2 + so3/devices/serial/pl011.c | 2 +- so3/fs/elf.c | 8 +-- so3/fs/vfs.c | 69 ++++++++++++++------- so3/include/net/lwip/sockets.h | 12 ++-- so3/include/syscall.h | 10 +-- so3/include/vfs.h | 6 ++ so3/kernel/process.c | 4 +- so3/kernel/syscalls.c | 1 + so3/kernel/thread.c | 2 +- so3/net/net.c | 4 +- 12 files changed, 80 insertions(+), 42 deletions(-) diff --git a/so3/arch/arm32/include/asm/syscall_number.h b/so3/arch/arm32/include/asm/syscall_number.h index 7474f8f2e..3a5167332 100644 --- a/so3/arch/arm32/include/asm/syscall_number.h +++ b/so3/arch/arm32/include/asm/syscall_number.h @@ -78,4 +78,6 @@ #define SYSCALL_SETSOCKOPT 110 #define SYSCALL_RECVFROM 111 +#define SYSCALL_WRITEV 146 + #endif /* ARCH_ARM32_SYSCALL_NUMBER_H */ diff --git a/so3/arch/arm64/include/asm/syscall_number.h b/so3/arch/arm64/include/asm/syscall_number.h index d81a0858a..c443e2fae 100644 --- a/so3/arch/arm64/include/asm/syscall_number.h +++ b/so3/arch/arm64/include/asm/syscall_number.h @@ -71,6 +71,8 @@ #define SYSCALL_MUTEX_LOCK 60 #define SYSCALL_MUTEX_UNLOCK 61 +#define SYSCALL_WRITEV 66 + #define SYSCALL_NANOSLEEP 70 #define SYSCALL_SYSINFO 99 diff --git a/so3/devices/serial/pl011.c b/so3/devices/serial/pl011.c index 6bd6bee18..4e3f101bd 100644 --- a/so3/devices/serial/pl011.c +++ b/so3/devices/serial/pl011.c @@ -119,7 +119,7 @@ static irq_return_t pl011_int(int irq, void *dummy) #ifdef CONFIG_IPC_SIGNAL if (current()->pcb != NULL) - do_kill(current()->pcb->pid, SIGINT); + __do_kill(current()->pcb->pid, SIGINT); #endif } diff --git a/so3/fs/elf.c b/so3/fs/elf.c index dce824587..cc2965c2c 100644 --- a/so3/fs/elf.c +++ b/so3/fs/elf.c @@ -35,12 +35,12 @@ uint8_t *elf_load_buffer(const char *filename) struct stat st; /* open and read file */ - fd = do_open(filename, O_RDONLY); + fd = __do_open(filename, O_RDONLY); if (fd < 0) return NULL; - if (do_stat(filename, &st)) + if (__do_stat(filename, &st)) return NULL; if (!st.st_size) @@ -52,9 +52,9 @@ uint8_t *elf_load_buffer(const char *filename) return NULL; } - do_read(fd, buffer, st.st_size); + __do_read(fd, buffer, st.st_size); - do_close(fd); + __do_close(fd); return buffer; } diff --git a/so3/fs/vfs.c b/so3/fs/vfs.c index 3f1ac96a2..fc06f015b 100644 --- a/so3/fs/vfs.c +++ b/so3/fs/vfs.c @@ -380,7 +380,8 @@ int vfs_clone_fd(int *fd_src, int *fd_dst) /**************************** Syscall implementation ****************************/ -SYSCALL_DEFINE3(read, int, fd, void *, buffer, int, count) +/* Low Level write */ +static int do_write(int fd, const void *buffer, int count) { int gfd; int ret; @@ -395,6 +396,11 @@ SYSCALL_DEFINE3(read, int, fd, void *, buffer, int, count) gfd = vfs_get_gfd(fd); + if (!vfs_is_valid_gfd(gfd)) { + mutex_unlock(&vfs_lock); + return -EINVAL; + } + /* FIXME: As for now the do_read/do_open only * support regular file VFS_TYPE_FILE, VFS_TYPE_IO and pipes */ @@ -403,28 +409,19 @@ SYSCALL_DEFINE3(read, int, fd, void *, buffer, int, count) return -EISDIR; } - if (!vfs_is_valid_gfd(gfd)) { - mutex_unlock(&vfs_lock); - return -EINVAL; - } - - if (!open_fds[gfd]->fops->read) { - LOG_ERROR("No fops read\n"); + if (!open_fds[gfd]->fops->write) { mutex_unlock(&vfs_lock); return -EBADF; } mutex_unlock(&vfs_lock); - ret = open_fds[gfd]->fops->read(gfd, buffer, count); + ret = open_fds[gfd]->fops->write(gfd, buffer, count); return ret; } -/** - * @brief This function writes a REGULAR FILE/FOLDER. It only support regular file, dirs and pipes - */ -SYSCALL_DEFINE3(write, int, fd, const void *, buffer, int, count) +SYSCALL_DEFINE3(read, int, fd, void *, buffer, int, count) { int gfd; int ret; @@ -439,11 +436,6 @@ SYSCALL_DEFINE3(write, int, fd, const void *, buffer, int, count) gfd = vfs_get_gfd(fd); - if (!vfs_is_valid_gfd(gfd)) { - mutex_unlock(&vfs_lock); - return -EINVAL; - } - /* FIXME: As for now the do_read/do_open only * support regular file VFS_TYPE_FILE, VFS_TYPE_IO and pipes */ @@ -452,18 +444,32 @@ SYSCALL_DEFINE3(write, int, fd, const void *, buffer, int, count) return -EISDIR; } - if (!open_fds[gfd]->fops->write) { + if (!vfs_is_valid_gfd(gfd)) { + mutex_unlock(&vfs_lock); + return -EINVAL; + } + + if (!open_fds[gfd]->fops->read) { + LOG_ERROR("No fops read\n"); mutex_unlock(&vfs_lock); return -EBADF; } mutex_unlock(&vfs_lock); - ret = open_fds[gfd]->fops->write(gfd, buffer, count); + ret = open_fds[gfd]->fops->read(gfd, buffer, count); return ret; } +/** + * @brief This function writes a REGULAR FILE/FOLDER. It only support regular file, dirs and pipes + */ +SYSCALL_DEFINE3(write, int, fd, const void *, buffer, int, count) +{ + return do_write(fd, buffer, count); +} + /** * @brief This function opens a file. Not all file types are supported. */ @@ -655,7 +661,7 @@ SYSCALL_DEFINE2(dup2, int, oldfd, int, newfd) } if (vfs_get_gfd(oldfd) != vfs_get_gfd(newfd)) - do_close(newfd); + __do_close(newfd); vfs_link_fd(newfd, vfs_get_gfd(oldfd)); @@ -824,6 +830,27 @@ SYSCALL_DEFINE3(fcntl, int, fd, unsigned long, cmd, unsigned long, args) return 0; } +/* + * Implementation of the writev syscall + */ +SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec *, vec, unsigned long, vlen) +{ + int i; + int ret = -1; + int total = 0; + + for (i = 0; i < vlen; i++) { + ret = do_write(fd, (const void *)vec[i].iov_base, vec[i].iov_len); + if (ret < 0) + break; + + total += ret; + } + + return total; +} + + static void vfs_gfd_init(void) { memset(open_fds, 0, MAX_FDS * sizeof(struct fd *)); diff --git a/so3/include/net/lwip/sockets.h b/so3/include/net/lwip/sockets.h index afe097b42..5ea356ca8 100644 --- a/so3/include/net/lwip/sockets.h +++ b/so3/include/net/lwip/sockets.h @@ -123,12 +123,12 @@ typedef u32_t socklen_t; #error "IOV_MAX larger than supported by LwIP" #endif /* IOV_MAX */ -#if !defined(iovec) -struct iovec { - void *iov_base; - size_t iov_len; -}; -#endif +// #if !defined(iovec) +// struct iovec { +// void *iov_base; +// size_t iov_len; +// }; +// #endif typedef int msg_iovlen_t; diff --git a/so3/include/syscall.h b/so3/include/syscall.h index eaf3ed73b..68231865e 100644 --- a/so3/include/syscall.h +++ b/so3/include/syscall.h @@ -90,14 +90,14 @@ */ #define SYSCALL_DECLARE(name, ...) \ long __sys_##name(syscall_args_t *args); \ - inline long do_##name(__VA_ARGS__); + inline long __do_##name(__VA_ARGS__); #define SYSCALL_DEFINE0(name) \ long __sys_##name(syscall_args_t *unused) \ { \ - return do_##name(); \ + return __do_##name(); \ } \ - inline long do_##name(void) + inline long __do_##name(void) #define SYSCALL_DEFINE1(...) __SYSCALL_DEFINEx(1, __VA_ARGS__) #define SYSCALL_DEFINE2(...) __SYSCALL_DEFINEx(2, __VA_ARGS__) #define SYSCALL_DEFINE3(...) __SYSCALL_DEFINEx(3, __VA_ARGS__) @@ -108,9 +108,9 @@ #define __SYSCALL_DEFINEx(x, name, ...) \ long __sys_##name(syscall_args_t *args) \ { \ - return do_##name(__MAP_ARGS(x, args->args, __VA_ARGS__)); \ + return __do_##name(__MAP_ARGS(x, args->args, __VA_ARGS__)); \ } \ - inline long do_##name(__MAP(x, __M_DECL, __VA_ARGS__)) + inline long __do_##name(__MAP(x, __M_DECL, __VA_ARGS__)) typedef struct { unsigned long args[6]; diff --git a/so3/include/vfs.h b/so3/include/vfs.h index a6956db23..e30837a3b 100644 --- a/so3/include/vfs.h +++ b/so3/include/vfs.h @@ -103,6 +103,11 @@ #include +struct iovec { + void *iov_base; + size_t iov_len; +}; + struct file_operations { int (*open)(int fd, const char *path); int (*close)(int fd); @@ -162,6 +167,7 @@ SYSCALL_DECLARE(mmap, addr_t start, size_t length, int prot, int fd, off_t offse SYSCALL_DECLARE(ioctl, int fd, unsigned long cmd, unsigned long args); SYSCALL_DECLARE(fcntl, int fd, unsigned long cmd, unsigned long args); SYSCALL_DECLARE(lseek, int fd, off_t off, int whence); +SYSCALL_DECLARE(writev, unsigned long fd, const struct iovec *vec, unsigned long vlen); /* VFS common interface */ diff --git a/so3/kernel/process.c b/so3/kernel/process.c index bc065224a..a5e4d471a 100644 --- a/so3/kernel/process.c +++ b/so3/kernel/process.c @@ -925,7 +925,7 @@ SYSCALL_DEFINE1(exit, int, exit_status) * locking in the low layers. */ for (i = 0; i < FD_MAX; i++) - do_close(i); + __do_close(i); local_irq_disable(); @@ -944,7 +944,7 @@ SYSCALL_DEFINE1(exit, int, exit_status) #ifdef CONFIG_IPC_SIGNAL /* Send the SIGCHLD signal to the parent */ - do_kill(pcb->parent->pid, SIGCHLD); + __do_kill(pcb->parent->pid, SIGCHLD); #endif /* CONFIG_IPC_SIGNAL */ diff --git a/so3/kernel/syscalls.c b/so3/kernel/syscalls.c index 4c61b0b16..7367092f4 100644 --- a/so3/kernel/syscalls.c +++ b/so3/kernel/syscalls.c @@ -60,6 +60,7 @@ static const syscall_fn_t syscall_table[NR_SYSCALLS] = { [SYSCALL_IOCTL] = __sys_ioctl, [SYSCALL_FCNTL] = __sys_fcntl, [SYSCALL_LSEEK] = __sys_lseek, + [SYSCALL_WRITEV] = __sys_writev, #ifdef CONFIG_IPC_PIPE [SYSCALL_PIPE] = __sys_pipe, #endif diff --git a/so3/kernel/thread.c b/so3/kernel/thread.c index 868c31835..17faefed0 100644 --- a/so3/kernel/thread.c +++ b/so3/kernel/thread.c @@ -272,7 +272,7 @@ void thread_exit(int *exit_status) remove_tcb_from_pcb(current()); #ifdef CONFIG_PROC_ENV - do_exit(0); + __do_exit(0); #endif } else { diff --git a/so3/net/net.c b/so3/net/net.c index 7ca28cb3f..19c006efb 100644 --- a/so3/net/net.c +++ b/so3/net/net.c @@ -413,7 +413,7 @@ SYSCALL_DEFINE3(socket, int, domain, int, type, int, protocol) lwip_fd = lwip_socket(domain, type, protocol); if (lwip_fd < 0) { - do_close(fd); + __do_close(fd); return lwip_return(lwip_fd); } @@ -505,7 +505,7 @@ SYSCALL_DEFINE3(accept, int, sockfd, struct sockaddr *, addr, socklen_t *, addrl lwip_bind_fd = lwip_accept(lwip_fd, addr_ptr, addrlen); if (lwip_bind_fd < 0) { - do_close(fd); + __do_close(fd); return lwip_return(lwip_bind_fd); } From cdaaf6d28d15964205555dd51bf008248229a69b Mon Sep 17 00:00:00 2001 From: Jean-Pierre Miceli Date: Tue, 7 Oct 2025 14:14:48 +0200 Subject: [PATCH 2/6] writev - Fix error in error case Signed-off-by: Jean-Pierre Miceli --- so3/fs/vfs.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/so3/fs/vfs.c b/so3/fs/vfs.c index fc06f015b..2d3d364e9 100644 --- a/so3/fs/vfs.c +++ b/so3/fs/vfs.c @@ -836,13 +836,17 @@ SYSCALL_DEFINE3(fcntl, int, fd, unsigned long, cmd, unsigned long, args) SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec *, vec, unsigned long, vlen) { int i; - int ret = -1; + int ret; int total = 0; for (i = 0; i < vlen; i++) { ret = do_write(fd, (const void *)vec[i].iov_base, vec[i].iov_len); - if (ret < 0) + if (ret < 0) { + /* Error on the fist loop --> return error code */ + if (i == 0) + total = -1; break; + } total += ret; } @@ -850,7 +854,6 @@ SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec *, vec, unsigned l return total; } - static void vfs_gfd_init(void) { memset(open_fds, 0, MAX_FDS * sizeof(struct fd *)); From 99a14299e688d4780cdb36af4f81955fa9e591c7 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Miceli Date: Tue, 7 Oct 2025 17:51:57 +0200 Subject: [PATCH 3/6] Add 'readv' syscall Signed-off-by: Jean-Pierre Miceli --- so3/arch/arm32/include/asm/syscall_number.h | 1 + so3/arch/arm64/include/asm/syscall_number.h | 1 + so3/fs/vfs.c | 66 +++++++++++++++++++++ so3/include/vfs.h | 1 + 4 files changed, 69 insertions(+) diff --git a/so3/arch/arm32/include/asm/syscall_number.h b/so3/arch/arm32/include/asm/syscall_number.h index 3a5167332..1da6a2f71 100644 --- a/so3/arch/arm32/include/asm/syscall_number.h +++ b/so3/arch/arm32/include/asm/syscall_number.h @@ -78,6 +78,7 @@ #define SYSCALL_SETSOCKOPT 110 #define SYSCALL_RECVFROM 111 +#define SYSCALL_READV 145 #define SYSCALL_WRITEV 146 #endif /* ARCH_ARM32_SYSCALL_NUMBER_H */ diff --git a/so3/arch/arm64/include/asm/syscall_number.h b/so3/arch/arm64/include/asm/syscall_number.h index c443e2fae..a91dd9e65 100644 --- a/so3/arch/arm64/include/asm/syscall_number.h +++ b/so3/arch/arm64/include/asm/syscall_number.h @@ -71,6 +71,7 @@ #define SYSCALL_MUTEX_LOCK 60 #define SYSCALL_MUTEX_UNLOCK 61 +#define SYSCALL_READV 66 #define SYSCALL_WRITEV 66 #define SYSCALL_NANOSLEEP 70 diff --git a/so3/fs/vfs.c b/so3/fs/vfs.c index 2d3d364e9..772ec6207 100644 --- a/so3/fs/vfs.c +++ b/so3/fs/vfs.c @@ -380,6 +380,50 @@ int vfs_clone_fd(int *fd_src, int *fd_dst) /**************************** Syscall implementation ****************************/ +/* Low Level read */ +static int do_read(int fd, void *buffer, int count) +{ + int gfd; + int ret; + + /* FIXME Max size of buffer */ + if (!buffer || count < 0) { + LOG_ERROR("Invalid inputs\n"); + return -EINVAL; + } + + mutex_lock(&vfs_lock); + + gfd = vfs_get_gfd(fd); + + /* FIXME: As for now the do_read/do_open only + * support regular file VFS_TYPE_FILE, VFS_TYPE_IO and pipes + */ + if (open_fds[gfd]->type == VFS_TYPE_DIR) { + mutex_unlock(&vfs_lock); + return -EISDIR; + } + + if (!vfs_is_valid_gfd(gfd)) { + mutex_unlock(&vfs_lock); + return -EINVAL; + } + + if (!open_fds[gfd]->fops->read) { + LOG_ERROR("No fops read\n"); + mutex_unlock(&vfs_lock); + return -EBADF; + } + + mutex_unlock(&vfs_lock); + + ret = open_fds[gfd]->fops->read(gfd, buffer, count); + + return ret; +} + + + /* Low Level write */ static int do_write(int fd, const void *buffer, int count) { @@ -854,6 +898,28 @@ SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec *, vec, unsigned l return total; } +SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec *, vec, + unsigned long, vlen) +{ + int i; + int ret; + int total = 0; + + for (i = 0; i < vlen; i++) { + ret = do_read(fd, vec[i].iov_base, vec[i].iov_len); + if (ret < 0) { + /* Error on the fist loop --> return error code */ + if (i == 0) + total = -1; + break; + } + + total += ret; + } + + return total; +} + static void vfs_gfd_init(void) { memset(open_fds, 0, MAX_FDS * sizeof(struct fd *)); diff --git a/so3/include/vfs.h b/so3/include/vfs.h index e30837a3b..745dd7335 100644 --- a/so3/include/vfs.h +++ b/so3/include/vfs.h @@ -168,6 +168,7 @@ SYSCALL_DECLARE(ioctl, int fd, unsigned long cmd, unsigned long args); SYSCALL_DECLARE(fcntl, int fd, unsigned long cmd, unsigned long args); SYSCALL_DECLARE(lseek, int fd, off_t off, int whence); SYSCALL_DECLARE(writev, unsigned long fd, const struct iovec *vec, unsigned long vlen); +SYSCALL_DECLARE(readv, unsigned long fd, const struct iovec *vec, unsigned long vlen); /* VFS common interface */ From 291932167931ea421f07f5ecfd9ac5dc0512643e Mon Sep 17 00:00:00 2001 From: Jean-Pierre Miceli Date: Wed, 8 Oct 2025 16:16:08 +0200 Subject: [PATCH 4/6] [syscall] Update readv & writev from comments in MR #207 Signed-off-by: Jean-Pierre Miceli --- so3/arch/arm64/include/asm/syscall_number.h | 2 +- so3/devices/serial/pl011.c | 2 +- so3/fs/elf.c | 8 +-- so3/fs/vfs.c | 63 ++++++--------------- so3/include/net/lwip/sockets.h | 12 ++-- so3/include/syscall.h | 12 ++-- so3/include/vfs.h | 3 + so3/kernel/process.c | 4 +- so3/kernel/syscalls.c | 1 + so3/kernel/thread.c | 2 +- so3/net/net.c | 4 +- 11 files changed, 44 insertions(+), 69 deletions(-) diff --git a/so3/arch/arm64/include/asm/syscall_number.h b/so3/arch/arm64/include/asm/syscall_number.h index a91dd9e65..2cbbea82d 100644 --- a/so3/arch/arm64/include/asm/syscall_number.h +++ b/so3/arch/arm64/include/asm/syscall_number.h @@ -71,7 +71,7 @@ #define SYSCALL_MUTEX_LOCK 60 #define SYSCALL_MUTEX_UNLOCK 61 -#define SYSCALL_READV 66 +#define SYSCALL_READV 65 #define SYSCALL_WRITEV 66 #define SYSCALL_NANOSLEEP 70 diff --git a/so3/devices/serial/pl011.c b/so3/devices/serial/pl011.c index 4e3f101bd..601be88f2 100644 --- a/so3/devices/serial/pl011.c +++ b/so3/devices/serial/pl011.c @@ -119,7 +119,7 @@ static irq_return_t pl011_int(int irq, void *dummy) #ifdef CONFIG_IPC_SIGNAL if (current()->pcb != NULL) - __do_kill(current()->pcb->pid, SIGINT); + sys_do_kill(current()->pcb->pid, SIGINT); #endif } diff --git a/so3/fs/elf.c b/so3/fs/elf.c index cc2965c2c..0489e7de5 100644 --- a/so3/fs/elf.c +++ b/so3/fs/elf.c @@ -35,12 +35,12 @@ uint8_t *elf_load_buffer(const char *filename) struct stat st; /* open and read file */ - fd = __do_open(filename, O_RDONLY); + fd = sys_do_open(filename, O_RDONLY); if (fd < 0) return NULL; - if (__do_stat(filename, &st)) + if (sys_do_stat(filename, &st)) return NULL; if (!st.st_size) @@ -52,9 +52,9 @@ uint8_t *elf_load_buffer(const char *filename) return NULL; } - __do_read(fd, buffer, st.st_size); + sys_do_read(fd, buffer, st.st_size); - __do_close(fd); + sys_do_close(fd); return buffer; } diff --git a/so3/fs/vfs.c b/so3/fs/vfs.c index 772ec6207..2f5343e20 100644 --- a/so3/fs/vfs.c +++ b/so3/fs/vfs.c @@ -467,43 +467,7 @@ static int do_write(int fd, const void *buffer, int count) SYSCALL_DEFINE3(read, int, fd, void *, buffer, int, count) { - int gfd; - int ret; - - /* FIXME Max size of buffer */ - if (!buffer || count < 0) { - LOG_ERROR("Invalid inputs\n"); - return -EINVAL; - } - - mutex_lock(&vfs_lock); - - gfd = vfs_get_gfd(fd); - - /* FIXME: As for now the do_read/do_open only - * support regular file VFS_TYPE_FILE, VFS_TYPE_IO and pipes - */ - if (open_fds[gfd]->type == VFS_TYPE_DIR) { - mutex_unlock(&vfs_lock); - return -EISDIR; - } - - if (!vfs_is_valid_gfd(gfd)) { - mutex_unlock(&vfs_lock); - return -EINVAL; - } - - if (!open_fds[gfd]->fops->read) { - LOG_ERROR("No fops read\n"); - mutex_unlock(&vfs_lock); - return -EBADF; - } - - mutex_unlock(&vfs_lock); - - ret = open_fds[gfd]->fops->read(gfd, buffer, count); - - return ret; + return do_read(fd, buffer, count); } /** @@ -705,7 +669,7 @@ SYSCALL_DEFINE2(dup2, int, oldfd, int, newfd) } if (vfs_get_gfd(oldfd) != vfs_get_gfd(newfd)) - __do_close(newfd); + sys_do_close(newfd); vfs_link_fd(newfd, vfs_get_gfd(oldfd)); @@ -885,17 +849,21 @@ SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec *, vec, unsigned l for (i = 0; i < vlen; i++) { ret = do_write(fd, (const void *)vec[i].iov_base, vec[i].iov_len); + ret = do_read(fd, vec[i].iov_base, vec[i].iov_len); if (ret < 0) { - /* Error on the fist loop --> return error code */ - if (i == 0) - total = -1; + break; + } else if ((ret >= 0) && (ret < vec[i].iov_len)) { + total += ret; break; } total += ret; } - return total; + if (total == 0) + return ret; + else + return total; } SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec *, vec, @@ -908,16 +876,19 @@ SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec *, vec, for (i = 0; i < vlen; i++) { ret = do_read(fd, vec[i].iov_base, vec[i].iov_len); if (ret < 0) { - /* Error on the fist loop --> return error code */ - if (i == 0) - total = -1; + break; + } else if ((ret >= 0) && (ret < vec[i].iov_len)) { + total += ret; break; } total += ret; } - return total; + if (total == 0) + return ret; + else + return total; } static void vfs_gfd_init(void) diff --git a/so3/include/net/lwip/sockets.h b/so3/include/net/lwip/sockets.h index 5ea356ca8..afe097b42 100644 --- a/so3/include/net/lwip/sockets.h +++ b/so3/include/net/lwip/sockets.h @@ -123,12 +123,12 @@ typedef u32_t socklen_t; #error "IOV_MAX larger than supported by LwIP" #endif /* IOV_MAX */ -// #if !defined(iovec) -// struct iovec { -// void *iov_base; -// size_t iov_len; -// }; -// #endif +#if !defined(iovec) +struct iovec { + void *iov_base; + size_t iov_len; +}; +#endif typedef int msg_iovlen_t; diff --git a/so3/include/syscall.h b/so3/include/syscall.h index 68231865e..2ddfbd18e 100644 --- a/so3/include/syscall.h +++ b/so3/include/syscall.h @@ -84,20 +84,20 @@ * __sys_SYSCALL_NAME taking syscall_args_t as arguments allowing for * a common interface between all syscall to put them in an array. * - * do_SYSCALL_NAME actual function with the syscall implementation with + * sys_do_SYSCALL_NAME actual function with the syscall implementation with * arguments define like a normal function. That function is automatically * called by __sys_SYCALL_NAME with the correct argument number and cast. */ #define SYSCALL_DECLARE(name, ...) \ long __sys_##name(syscall_args_t *args); \ - inline long __do_##name(__VA_ARGS__); + inline long sys_do_##name(__VA_ARGS__); #define SYSCALL_DEFINE0(name) \ long __sys_##name(syscall_args_t *unused) \ { \ - return __do_##name(); \ + return sys_do_##name(); \ } \ - inline long __do_##name(void) + inline long sys_do_##name(void) #define SYSCALL_DEFINE1(...) __SYSCALL_DEFINEx(1, __VA_ARGS__) #define SYSCALL_DEFINE2(...) __SYSCALL_DEFINEx(2, __VA_ARGS__) #define SYSCALL_DEFINE3(...) __SYSCALL_DEFINEx(3, __VA_ARGS__) @@ -108,9 +108,9 @@ #define __SYSCALL_DEFINEx(x, name, ...) \ long __sys_##name(syscall_args_t *args) \ { \ - return __do_##name(__MAP_ARGS(x, args->args, __VA_ARGS__)); \ + return sys_do_##name(__MAP_ARGS(x, args->args, __VA_ARGS__)); \ } \ - inline long __do_##name(__MAP(x, __M_DECL, __VA_ARGS__)) + inline long sys_do_##name(__MAP(x, __M_DECL, __VA_ARGS__)) typedef struct { unsigned long args[6]; diff --git a/so3/include/vfs.h b/so3/include/vfs.h index 745dd7335..671f80001 100644 --- a/so3/include/vfs.h +++ b/so3/include/vfs.h @@ -108,6 +108,9 @@ struct iovec { size_t iov_len; }; +#define iovec iovec + + struct file_operations { int (*open)(int fd, const char *path); int (*close)(int fd); diff --git a/so3/kernel/process.c b/so3/kernel/process.c index a5e4d471a..ff283bc79 100644 --- a/so3/kernel/process.c +++ b/so3/kernel/process.c @@ -925,7 +925,7 @@ SYSCALL_DEFINE1(exit, int, exit_status) * locking in the low layers. */ for (i = 0; i < FD_MAX; i++) - __do_close(i); + sys_do_close(i); local_irq_disable(); @@ -944,7 +944,7 @@ SYSCALL_DEFINE1(exit, int, exit_status) #ifdef CONFIG_IPC_SIGNAL /* Send the SIGCHLD signal to the parent */ - __do_kill(pcb->parent->pid, SIGCHLD); + sys_do_kill(pcb->parent->pid, SIGCHLD); #endif /* CONFIG_IPC_SIGNAL */ diff --git a/so3/kernel/syscalls.c b/so3/kernel/syscalls.c index 7367092f4..68e15083f 100644 --- a/so3/kernel/syscalls.c +++ b/so3/kernel/syscalls.c @@ -60,6 +60,7 @@ static const syscall_fn_t syscall_table[NR_SYSCALLS] = { [SYSCALL_IOCTL] = __sys_ioctl, [SYSCALL_FCNTL] = __sys_fcntl, [SYSCALL_LSEEK] = __sys_lseek, + [SYSCALL_READV] = __sys_readv, [SYSCALL_WRITEV] = __sys_writev, #ifdef CONFIG_IPC_PIPE [SYSCALL_PIPE] = __sys_pipe, diff --git a/so3/kernel/thread.c b/so3/kernel/thread.c index 17faefed0..363ec1520 100644 --- a/so3/kernel/thread.c +++ b/so3/kernel/thread.c @@ -272,7 +272,7 @@ void thread_exit(int *exit_status) remove_tcb_from_pcb(current()); #ifdef CONFIG_PROC_ENV - __do_exit(0); + sys_do_exit(0); #endif } else { diff --git a/so3/net/net.c b/so3/net/net.c index 19c006efb..d638f0797 100644 --- a/so3/net/net.c +++ b/so3/net/net.c @@ -413,7 +413,7 @@ SYSCALL_DEFINE3(socket, int, domain, int, type, int, protocol) lwip_fd = lwip_socket(domain, type, protocol); if (lwip_fd < 0) { - __do_close(fd); + sys_do_close(fd); return lwip_return(lwip_fd); } @@ -505,7 +505,7 @@ SYSCALL_DEFINE3(accept, int, sockfd, struct sockaddr *, addr, socklen_t *, addrl lwip_bind_fd = lwip_accept(lwip_fd, addr_ptr, addrlen); if (lwip_bind_fd < 0) { - __do_close(fd); + sys_do_close(fd); return lwip_return(lwip_bind_fd); } From 580e00fa5e6351328d6a8e6228b3e9bf777e6aba Mon Sep 17 00:00:00 2001 From: Jean-Pierre Miceli Date: Thu, 9 Oct 2025 08:18:33 +0200 Subject: [PATCH 5/6] [syscall] readv & writev clean-up - MR #207 Signed-off-by: Jean-Pierre Miceli --- so3/fs/vfs.c | 12 ++++-------- so3/include/syscall.h | 10 +++++----- so3/include/vfs.h | 1 - 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/so3/fs/vfs.c b/so3/fs/vfs.c index 2f5343e20..1ba8de20f 100644 --- a/so3/fs/vfs.c +++ b/so3/fs/vfs.c @@ -422,8 +422,6 @@ static int do_read(int fd, void *buffer, int count) return ret; } - - /* Low Level write */ static int do_write(int fd, const void *buffer, int count) { @@ -844,12 +842,11 @@ SYSCALL_DEFINE3(fcntl, int, fd, unsigned long, cmd, unsigned long, args) SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec *, vec, unsigned long, vlen) { int i; - int ret; + int ret = 0; int total = 0; for (i = 0; i < vlen; i++) { - ret = do_write(fd, (const void *)vec[i].iov_base, vec[i].iov_len); - ret = do_read(fd, vec[i].iov_base, vec[i].iov_len); + ret = do_write(fd, (const void *) vec[i].iov_base, vec[i].iov_len); if (ret < 0) { break; } else if ((ret >= 0) && (ret < vec[i].iov_len)) { @@ -866,11 +863,10 @@ SYSCALL_DEFINE3(writev, unsigned long, fd, const struct iovec *, vec, unsigned l return total; } -SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec *, vec, - unsigned long, vlen) +SYSCALL_DEFINE3(readv, unsigned long, fd, const struct iovec *, vec, unsigned long, vlen) { int i; - int ret; + int ret = 0; int total = 0; for (i = 0; i < vlen; i++) { diff --git a/so3/include/syscall.h b/so3/include/syscall.h index 2ddfbd18e..1c1031576 100644 --- a/so3/include/syscall.h +++ b/so3/include/syscall.h @@ -95,7 +95,7 @@ #define SYSCALL_DEFINE0(name) \ long __sys_##name(syscall_args_t *unused) \ { \ - return sys_do_##name(); \ + return sys_do_##name(); \ } \ inline long sys_do_##name(void) #define SYSCALL_DEFINE1(...) __SYSCALL_DEFINEx(1, __VA_ARGS__) @@ -105,11 +105,11 @@ #define SYSCALL_DEFINE5(...) __SYSCALL_DEFINEx(5, __VA_ARGS__) #define SYSCALL_DEFINE6(...) __SYSCALL_DEFINEx(6, __VA_ARGS__) -#define __SYSCALL_DEFINEx(x, name, ...) \ - long __sys_##name(syscall_args_t *args) \ - { \ +#define __SYSCALL_DEFINEx(x, name, ...) \ + long __sys_##name(syscall_args_t *args) \ + { \ return sys_do_##name(__MAP_ARGS(x, args->args, __VA_ARGS__)); \ - } \ + } \ inline long sys_do_##name(__MAP(x, __M_DECL, __VA_ARGS__)) typedef struct { diff --git a/so3/include/vfs.h b/so3/include/vfs.h index 671f80001..b75836586 100644 --- a/so3/include/vfs.h +++ b/so3/include/vfs.h @@ -110,7 +110,6 @@ struct iovec { #define iovec iovec - struct file_operations { int (*open)(int fd, const char *path); int (*close)(int fd); From 0c97a7f604c61c56a9c86d9473e660ca62d9be2a Mon Sep 17 00:00:00 2001 From: Jean-Pierre Miceli Date: Thu, 9 Oct 2025 08:21:26 +0200 Subject: [PATCH 6/6] Run format checking for branch 144-support-musl Signed-off-by: Jean-Pierre Miceli --- .github/workflows/style.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/style.yml b/.github/workflows/style.yml index 770233583..3268b2146 100644 --- a/.github/workflows/style.yml +++ b/.github/workflows/style.yml @@ -10,7 +10,7 @@ on: push: branches: ["main"] pull_request: - branches: ["main"] + branches: ["main", "144-support-musl"] jobs: formatting-check: