Below in the implementation section, there are implementation chapters for the user-space side and kernel side respectively, where I have provided the exact GitHub commit link, which you can access.
I have also included program source code files and record, record-time, and syscall_diff.txt files there.
In the user-space program, you write a C program to access your custom newly added syscall by using its syscall number. In my case, it was 548 for my 64-bit base syscall named uddhav5204_64. I also created Makefile to compile and in the same user-space directory, I will later store git diff file, record, and record-times.
upgautamvt@upg-vt-lab:~/CLionProjects/KernelWithBpfPrograms/user-space$ ls
access-syscall access-syscall.c Makefile record record-time syscall_diff.txt
That was everything.
Below, I have provided all implementation that tells what files added or modified and where.
User-space directory
Makefile
CC = gcc
CFLAGS = -Wall -Werror
SRCS = $(wildcard *.c)
TARGETS = $(patsubst %.c,%, $(SRCS))
all: $(TARGETS)
%: %.c
$(CC) $(CFLAGS) -o $@ $<
clean:
rm -f $(TARGETS)
access-syscall.c
#include <stdio.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <errno.h>
#define __NR_uddhav5204_64 548
int main() {
long result;
*// Test 64-bit syscall*
result = syscall(__NR_uddhav5204_64);
if (result == -1) {
perror("64-bit syscall failed");
} else {
printf("%ldn", result);
}
return 0;
}
548 64 common sys_uddhav5204_64 //add this at the end
asmlinkage long sys_uddhav5204_64(void); //add this at the end
#ifndef __NR_uddhav5204_64
#define __NR_uddhav5204_64 548
#endif
#include <linux/kernel.h>
#include <linux/syscalls.h>
*// 64-bit syscall definition*
asmlinkage long sys_uddhav5204_64(void) {
return 1000; *// Always return 1000*
}
*//DEINEX is a macro, X tells number of arguments*
SYSCALL_DEFINE0(uddhav5204_64)
{
return sys_uddhav5204_64();
}
### kernel/Makefile
obj-y += sys_uddhav5204_64.o //add this at the end
diff --git a/arch/x86/entry/syscalls/syscall_64.tbl b/arch/x86/entry/syscalls/syscall_64.tbl
index 7e8d46f4147f..e15e3c190ea6 100644
--- a/arch/x86/entry/syscalls/syscall_64.tbl
+++ b/arch/x86/entry/syscalls/syscall_64.tbl
@@ -426,5 +426,6 @@
545 x32 execveat compat_sys_execveat
546 x32 preadv2 compat_sys_preadv64v2
547 x32 pwritev2 compat_sys_pwritev64v2
+548 64 common sys_uddhav5204_64 //add this at the end
# This is the end of the legacy x32 range. Numbers 548 and above are
# not special and are not to be used for x32-specific syscalls.
diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h
index 77eb9b0e7685..05f3fb914b54 100644
--- a/include/linux/syscalls.h
+++ b/include/linux/syscalls.h
@@ -1295,3 +1295,4 @@ int __sys_getsockopt(int fd, int level, int optname, char __user *optval,
int __sys_setsockopt(int fd, int level, int optname, char __user *optval,
int optlen);
#endif
+asmlinkage long sys_uddhav5204_64(void); //add this at the end
\ No newline at end of file
diff --git a/kernel/Makefile b/kernel/Makefile
index ce105a5558fc..803f7c8b6548 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -159,3 +159,4 @@ $(obj)/kheaders_data.tar.xz: FORCE
$(call cmd,genikh)
clean-files := kheaders_data.tar.xz kheaders.md5
+obj-y += sys_uddhav5204_64.o //add this at the end
\ No newline at end of file
diff --git a/kernel/sys_uddhav5204_64.c b/kernel/sys_uddhav5204_64.c
new file mode 100644
index 000000000000..2fada3bf3d18
--- /dev/null
+++ b/kernel/sys_uddhav5204_64.c
@@ -0,0 +1,13 @@
+#include <linux/kernel.h>
+#include <linux/syscalls.h>
+
+// 64-bit syscall definition
+asmlinkage long sys_uddhav5204_64(void) {
+ return 1000; // Always return 1000
+}
+
+//DEINEX is a macro, X tells number of arguments
+SYSCALL_DEFINE0(uddhav5204_64)
+{
+ return sys_uddhav5204_64();
+}
diff --git a/tools/arch/x86/include/uapi/asm/unistd_64.h b/tools/arch/x86/include/uapi/asm/unistd_64.h
index d0f2043d7132..001570df7d88 100644
--- a/tools/arch/x86/include/uapi/asm/unistd_64.h
+++ b/tools/arch/x86/include/uapi/asm/unistd_64.h
@@ -29,3 +29,6 @@
#ifndef __NR_seccomp
#define __NR_seccomp 317
#endif
+#ifndef __NR_uddhav5204_64
+#define __NR_uddhav5204_64 548
+#endif