Skip to content

Commit 82ccd1d

Browse files
authored
Merge pull request #189 from smartobjectoriented/183-refactor-syscall-handling
refactor syscall into an array and move number into arch depend files
2 parents a454a18 + 10f4f5a commit 82ccd1d

File tree

23 files changed

+447
-401
lines changed

23 files changed

+447
-401
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (C) 2025 Clement Dieperink <clement.dieperink@heig-vd.ch>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License version 2 as
6+
* published by the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program; if not, write to the Free Software
15+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16+
*
17+
*/
18+
19+
#ifndef ARCH_ARM32_SYSCALL_NUMBER_H
20+
#define ARCH_ARM32_SYSCALL_NUMBER_H
21+
22+
/*
23+
* Syscall number definition
24+
*/
25+
26+
#define SYSCALL_EXIT 1
27+
#define SYSCALL_EXECVE 2
28+
#define SYSCALL_WAITPID 3
29+
#define SYSCALL_READ 4
30+
#define SYSCALL_WRITE 5
31+
#define SYSCALL_FORK 7
32+
#define SYSCALL_PTRACE 8
33+
#define SYSCALL_READDIR 9
34+
#define SYSCALL_OPEN 14
35+
#define SYSCALL_CLOSE 15
36+
#define SYSCALL_THREAD_CREATE 16
37+
#define SYSCALL_THREAD_JOIN 17
38+
#define SYSCALL_THREAD_EXIT 18
39+
#define SYSCALL_PIPE 19
40+
#define SYSCALL_IOCTL 20
41+
#define SYSCALL_FCNTL 21
42+
#define SYSCALL_DUP 22
43+
#define SYSCALL_DUP2 23
44+
45+
#define SYSCALL_SOCKET 26
46+
#define SYSCALL_BIND 27
47+
#define SYSCALL_LISTEN 28
48+
#define SYSCALL_ACCEPT 29
49+
#define SYSCALL_CONNECT 30
50+
#define SYSCALL_RECV 31
51+
#define SYSCALL_SEND 32
52+
#define SYSCALL_SENDTO 33
53+
54+
#define SYSCALL_STAT 34
55+
#define SYSCALL_MMAP 35
56+
#define SYSCALL_GETPID 37
57+
58+
#define SYSCALL_GETTIMEOFDAY 38
59+
#define SYSCALL_SETTIMEOFDAY 39
60+
#define SYSCALL_CLOCK_GETTIME 40
61+
62+
#define SYSCALL_THREAD_YIELD 43
63+
64+
#define SYSCALL_SBRK 45
65+
#define SYSCALL_SIGACTION 46
66+
#define SYSCALL_KILL 47
67+
#define SYSCALL_SIGRETURN 48
68+
69+
#define SYSCALL_LSEEK 50
70+
71+
#define SYSCALL_MUTEX_LOCK 60
72+
#define SYSCALL_MUTEX_UNLOCK 61
73+
74+
#define SYSCALL_NANOSLEEP 70
75+
76+
#define SYSCALL_SYSINFO 99
77+
78+
#define SYSCALL_SETSOCKOPT 110
79+
#define SYSCALL_RECVFROM 111
80+
81+
#endif /* ARCH_ARM32_SYSCALL_NUMBER_H */
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (C) 2025 Clement Dieperink <clement.dieperink@heig-vd.ch>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License version 2 as
6+
* published by the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program; if not, write to the Free Software
15+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16+
*
17+
*/
18+
19+
#ifndef ARCH_ARM64_SYSCALL_NUMBER_H
20+
#define ARCH_ARM64_SYSCALL_NUMBER_H
21+
22+
/*
23+
* Syscall number definition
24+
*/
25+
26+
#define SYSCALL_EXIT 1
27+
#define SYSCALL_EXECVE 2
28+
#define SYSCALL_WAITPID 3
29+
#define SYSCALL_READ 4
30+
#define SYSCALL_WRITE 5
31+
#define SYSCALL_FORK 7
32+
#define SYSCALL_PTRACE 8
33+
#define SYSCALL_READDIR 9
34+
#define SYSCALL_OPEN 14
35+
#define SYSCALL_CLOSE 15
36+
#define SYSCALL_THREAD_CREATE 16
37+
#define SYSCALL_THREAD_JOIN 17
38+
#define SYSCALL_THREAD_EXIT 18
39+
#define SYSCALL_PIPE 19
40+
#define SYSCALL_IOCTL 20
41+
#define SYSCALL_FCNTL 21
42+
#define SYSCALL_DUP 22
43+
#define SYSCALL_DUP2 23
44+
45+
#define SYSCALL_SOCKET 26
46+
#define SYSCALL_BIND 27
47+
#define SYSCALL_LISTEN 28
48+
#define SYSCALL_ACCEPT 29
49+
#define SYSCALL_CONNECT 30
50+
#define SYSCALL_RECV 31
51+
#define SYSCALL_SEND 32
52+
#define SYSCALL_SENDTO 33
53+
54+
#define SYSCALL_STAT 34
55+
#define SYSCALL_MMAP 35
56+
#define SYSCALL_GETPID 37
57+
58+
#define SYSCALL_GETTIMEOFDAY 38
59+
#define SYSCALL_SETTIMEOFDAY 39
60+
#define SYSCALL_CLOCK_GETTIME 40
61+
62+
#define SYSCALL_THREAD_YIELD 43
63+
64+
#define SYSCALL_SBRK 45
65+
#define SYSCALL_SIGACTION 46
66+
#define SYSCALL_KILL 47
67+
#define SYSCALL_SIGRETURN 48
68+
69+
#define SYSCALL_LSEEK 50
70+
71+
#define SYSCALL_MUTEX_LOCK 60
72+
#define SYSCALL_MUTEX_UNLOCK 61
73+
74+
#define SYSCALL_NANOSLEEP 70
75+
76+
#define SYSCALL_SYSINFO 99
77+
78+
#define SYSCALL_SETSOCKOPT 110
79+
#define SYSCALL_RECVFROM 111
80+
81+
#endif /* ARCH_ARM64_SYSCALL_NUMBER_H */

so3/fs/vfs.c

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ int vfs_clone_fd(int *fd_src, int *fd_dst)
379379

380380
/**************************** Syscall implementation ****************************/
381381

382-
int do_read(int fd, void *buffer, int count)
382+
SYSCALL_DEFINE3(read, int, fd, void *, buffer, int, count)
383383
{
384384
int gfd;
385385
int ret;
@@ -427,7 +427,7 @@ int do_read(int fd, void *buffer, int count)
427427
/**
428428
* @brief This function writes a REGULAR FILE/FOLDER. It only support regular file, dirs and pipes
429429
*/
430-
int do_write(int fd, const void *buffer, int count)
430+
SYSCALL_DEFINE3(write, int, fd, const void *, buffer, int, count)
431431
{
432432
int gfd;
433433
int ret;
@@ -474,7 +474,7 @@ int do_write(int fd, const void *buffer, int count)
474474
/**
475475
* @brief This function opens a file. Not all file types are supported.
476476
*/
477-
int do_open(const char *filename, int flags)
477+
SYSCALL_DEFINE2(open, const char *, filename, int, flags)
478478
{
479479
int fd, gfd, ret = -1;
480480
uint32_t type;
@@ -542,7 +542,7 @@ int do_open(const char *filename, int flags)
542542
* @brief readdir read a directory entry which will be stored in a struct dirent entry
543543
* @param fd This is the file descriptor provided as (DIR *) when doing opendir in the userspace.
544544
*/
545-
int do_readdir(int fd, char *buf, int len)
545+
SYSCALL_DEFINE3(readdir, int, fd, char *, buf, int, len)
546546
{
547547
struct dirent *dirent;
548548
int gfd;
@@ -588,13 +588,13 @@ int do_readdir(int fd, char *buf, int len)
588588
* only when refcount is equal to zero (no more reference on the gfd).
589589
* @param fd This is the local fd from the process' table.
590590
*/
591-
void do_close(int fd)
591+
SYSCALL_DEFINE1(close, int, fd)
592592
{
593593
pcb_t *pcb = current()->pcb;
594594
int gfd;
595595

596596
if ((!pcb) || (fd < 0))
597-
return;
597+
return 0;
598598

599599
mutex_lock(&vfs_lock);
600600

@@ -604,12 +604,12 @@ void do_close(int fd)
604604
if (gfd < 0) {
605605
LOG_DEBUG("Was already freed\n");
606606
mutex_unlock(&vfs_lock);
607-
return;
607+
return 0;
608608
}
609609

610610
if (!open_fds[gfd]) {
611611
mutex_unlock(&vfs_lock);
612-
return;
612+
return 0;
613613
}
614614

615615
/* Decrement reference counter to keep track of open fds */
@@ -640,13 +640,15 @@ void do_close(int fd)
640640
}
641641

642642
mutex_unlock(&vfs_lock);
643+
644+
return 0;
643645
}
644646

645647
/**
646648
* @brief dup2 creates a synonym of oldfd on newfd
647649
*
648650
*/
649-
int do_dup2(int oldfd, int newfd)
651+
SYSCALL_DEFINE2(dup2, int, oldfd, int, newfd)
650652
{
651653
if ((newfd < 0) || (newfd > MAX_FDS))
652654
return -EBADF;
@@ -680,7 +682,7 @@ int do_dup2(int oldfd, int newfd)
680682
* @param File descriptor to copy.
681683
* @return A copy of the file descriptor.
682684
*/
683-
int do_dup(int oldfd)
685+
SYSCALL_DEFINE1(dup, int, oldfd)
684686
{
685687
#ifdef CONFIG_PROC_ENV
686688
int newfd;
@@ -708,7 +710,7 @@ int do_dup(int oldfd)
708710
#endif
709711
}
710712

711-
int do_stat(const char *path, struct stat *st)
713+
SYSCALL_DEFINE2(stat, const char *, path, struct stat *, st)
712714
{
713715
int ret;
714716

@@ -737,7 +739,7 @@ int do_stat(const char *path, struct stat *st)
737739
/**
738740
* An mmap() implementation in VFS.
739741
*/
740-
void *do_mmap(addr_t start, size_t length, int prot, int fd, off_t offset)
742+
SYSCALL_DEFINE5(mmap, addr_t, start, size_t, length, int, prot, int, fd, off_t, offset)
741743
{
742744
int gfd;
743745
uint32_t page_count;
@@ -749,7 +751,7 @@ void *do_mmap(addr_t start, size_t length, int prot, int fd, off_t offset)
749751
if (-1 == gfd) {
750752
printk("%s: could not get global fd.\n", __func__);
751753
set_errno(EBADF);
752-
return MAP_FAILED;
754+
return (long) MAP_FAILED;
753755
}
754756

755757
mutex_lock(&vfs_lock);
@@ -758,7 +760,7 @@ void *do_mmap(addr_t start, size_t length, int prot, int fd, off_t offset)
758760
printk("%s: could not get device fops.\n", __func__);
759761
mutex_unlock(&vfs_lock);
760762
set_errno(EBADF);
761-
return MAP_FAILED;
763+
return (long) MAP_FAILED;
762764
}
763765

764766
mutex_unlock(&vfs_lock);
@@ -772,14 +774,14 @@ void *do_mmap(addr_t start, size_t length, int prot, int fd, off_t offset)
772774
if (!fops->mmap) {
773775
printk("%s: device doesn't support mmap.\n", __func__);
774776
set_errno(EACCES);
775-
return MAP_FAILED;
777+
return (long) MAP_FAILED;
776778
}
777779

778780
/* Call the mmap fops that will do the actual mapping. */
779-
return fops->mmap(fd, start, page_count, offset);
781+
return (long) fops->mmap(fd, start, page_count, offset);
780782
}
781783

782-
int do_ioctl(int fd, unsigned long cmd, unsigned long args)
784+
SYSCALL_DEFINE3(ioctl, int, fd, unsigned long, cmd, unsigned long, args)
783785
{
784786
int rc, gfd;
785787
mutex_lock(&vfs_lock);
@@ -808,7 +810,7 @@ int do_ioctl(int fd, unsigned long cmd, unsigned long args)
808810
* Implementation of standard lseek() syscall. It depends on the underlying
809811
* device operations.
810812
*/
811-
off_t do_lseek(int fd, off_t off, int whence)
813+
SYSCALL_DEFINE3(lseek, int, fd, off_t, off, int, whence)
812814
{
813815
int rc, gfd;
814816
mutex_lock(&vfs_lock);
@@ -834,7 +836,7 @@ off_t do_lseek(int fd, off_t off, int whence)
834836
/*
835837
* Implementation of the fcntl syscall
836838
*/
837-
int do_fcntl(int fd, unsigned long cmd, unsigned long args)
839+
SYSCALL_DEFINE3(fcntl, int, fd, unsigned long, cmd, unsigned long, args)
838840
{
839841
/* Not yet implemented */
840842

so3/include/mutex.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <list.h>
2727
#include <spinlock.h>
2828
#include <thread.h>
29+
#include <syscall.h>
2930

3031
#include <asm/atomic.h>
3132

@@ -58,8 +59,8 @@ void mutex_lock(struct mutex *lock);
5859
void mutex_unlock(struct mutex *lock);
5960
void mutex_init(struct mutex *lock);
6061

61-
int do_mutex_init(void);
62-
int do_mutex_lock(unsigned long number);
63-
int do_mutex_unlock(unsigned long number);
62+
SYSCALL_DECLARE(mutex_init, void);
63+
SYSCALL_DECLARE(mutex_lock, unsigned long number);
64+
SYSCALL_DECLARE(mutex_unlock, unsigned long number);
6465

6566
#endif /* MUTEX_H */

so3/include/net.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define NET_H
2121

2222
#include <vfs.h>
23+
#include <syscall.h>
2324

2425
#include <net/lwip/sockets.h>
2526

@@ -67,15 +68,15 @@
6768

6869
void net_init(void);
6970

70-
int do_socket(int domain, int type, int protocol);
71-
int do_connect(int sockfd, const struct sockaddr *name, socklen_t namelen);
72-
int do_bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
73-
int do_listen(int sockfd, int backlog);
74-
int do_accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
75-
int do_recv(int sockfd, void *mem, size_t len, int flags);
76-
int do_recvfrom(int sockfd, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
77-
int do_send(int sockfd, const void *dataptr, size_t size, int flags);
78-
int do_sendto(int sockfd, const void *dataptr, size_t size, int flags, const struct sockaddr *to, socklen_t tolen);
79-
int do_setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);
71+
SYSCALL_DECLARE(socket, int domain, int type, int protocol);
72+
SYSCALL_DECLARE(connect, int sockfd, const struct sockaddr *name, socklen_t namelen);
73+
SYSCALL_DECLARE(bind, int sockfd, const struct sockaddr *addr, socklen_t addrlen);
74+
SYSCALL_DECLARE(listen, int sockfd, int backlog);
75+
SYSCALL_DECLARE(accept, int sockfd, struct sockaddr *addr, socklen_t *addrlen);
76+
SYSCALL_DECLARE(recv, int sockfd, void *mem, size_t len, int flags);
77+
SYSCALL_DECLARE(recvfrom, int sockfd, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen);
78+
SYSCALL_DECLARE(send, int sockfd, const void *dataptr, size_t size, int flags);
79+
SYSCALL_DECLARE(sendto, int sockfd, const void *dataptr, size_t size, int flags, const struct sockaddr *to, socklen_t tolen);
80+
SYSCALL_DECLARE(setsockopt, int sockfd, int level, int optname, const void *optval, socklen_t optlen);
8081

8182
#endif /* NET_H */

so3/include/pipe.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <memory.h>
2323
#include <mutex.h>
2424
#include <completion.h>
25+
#include <syscall.h>
2526

2627
#define PIPE_READER 0
2728
#define PIPE_WRITER 0
@@ -52,6 +53,6 @@ struct pipe_desc {
5253
};
5354
typedef struct pipe_desc pipe_desc_t;
5455

55-
int do_pipe(int pipefd[2]);
56+
SYSCALL_DECLARE(pipe, int *pipefd);
5657

5758
#endif /* PIPE_H */

0 commit comments

Comments
 (0)