All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] implement-system-call-mini-HOWTO.txt
@ 2006-11-13  6:14 Amit Choudhary
  2006-11-13  8:04 ` Arjan van de Ven
  2006-11-13  9:23 ` Jan Engelhardt
  0 siblings, 2 replies; 3+ messages in thread
From: Amit Choudhary @ 2006-11-13  6:14 UTC (permalink / raw)
  To: Linux Kernel

Description: A mini HOWTO on implementing a new system call. Although, I think that implementing new system calls is discouraged, many people do end up in implementing proprietary system calls. This HOWTO can help those people, and also those who are inquisitive (like me). Would it be a good idea to include this in the kernel tree, in the Documentation directory? If you feel that this document should be improved before it goes in the tree, please offer me your suggestions.

Signed-off-by: Amit Choudhary <amit2030@gmail.com>

diff -Nru b/implement-system-call-mini-HOWTO.txt a/implement-system-call-mini-HOWTO.txt
--- b/implement-system-call-mini-HOWTO.txt	1969-12-31 16:00:00.000000000 -0800
+++ a/implement-system-call-mini-HOWTO.txt	2006-11-12 22:02:25.000000000 -0800
@@ -0,0 +1,165 @@
+Implementing a System Call on Linux 2.6 for i386.
+Amit Choudhary
+http://amit-choudhary.blogspot.com
+amit2030@gmail.com
+
+System Call
+----------
+*What is a system call?
+--A mechanism by which a normal user program can request services from the operating system. --It is executed in the kernel mode.
+*How is it invoked?
+--A library routine issues a trap to the OS by executing INT 0x80 assembly instruction.
+--The system call number is passed through the EAX register.
+--The arguments are passed through registers  EBX, ECX, etc.
+
+Files to be modified
+(Assume your linux source base directory is "/usr/src/linux")
+--------------------
+*/usr/src/linux/arch/i386/kernel/syscall_table.S
+*/usr/src/linux/include/asm-i386/unistd.h
+*/usr/src/linux/include/linux/syscalls.h
+*/usr/src/linux/Makefile
+*/boot/grub/grub.conf
+
+New files/directories
+---------------------
+*Directory that will contain the source and the Makefile (You can also implement your
+ system call in an existing file) [/usr/src/linux/mycall]
+*Source file containing our system call code [/usr/src/linux/mycall/mycall.c]
+*Makefile [[/usr/src/linux/mycall/Makefile]
+*User space header file and source file to test the system call.
+
+syscall_table.S
+---------------
+*/usr/src/linux/arch/i386/kernel/syscall_table.S
+--This file contains system call names.
+--Add a line to the end of this file (Let's assume that the name of our system call is mycall)
+--Add ".long sys_mycall" at the end of the list.
+
+unistd.h
+--------
+*/usr/src/linux/include/asm-i386/unistd.h
+--This file contains the system call number that is passed to the kernel through a register (EAX), when a system call is invoked.
+--Add "#define __NR_mycall <Last_System_Call_Num + 1>" at the end of the list.
+--If the last system call defined here is:
+	"#define __NR_vmsplice		316", then add:
+	"#define __NR_mycall		317" at the end of the list.
+--Increment the "NR_syscalls" by 1. So, if NR_syscalls is defined as:
+	"#define NR_syscalls 317", then change it to:
+	"#define NR_syscalls 318"
+
+syscalls.h
+----------
+*/usr/src/linux/include/linux/syscalls.h
+--This file contain the declarations for system calls.
+--Add the following line at the end of the file:
+	"asmlinkage long sys_mycall(int i);"
+
+Source file
+-----------
+*Create a new directory in /usr/src/linux
+--Let's name it as "mycall"
+*Create a source file named "mycall.c" in dir "mycall" that will have the code for our system call
+*The definition of the system call in the source file would be
+--asmlinkage long sys_mycall(...){...}
+*It should include the file linux/linkage.h
+
+*So, the file "mycall.c" will look like:
+
+/*---Start of mycall.c----*/
+#include<linux/linkage.h>
+
+asmlinkage long sys_mycall(int i)
+{
+        return i+10;
+}
+/*---End of mycall.c------*/
+
+*What is asmlinkage?
+--Look for the arguments on the kernel stack
+
+Makefile for our system call
+----------------------------
+*The Makefile in dir "mycall" will have only one line:
+
+#####Makefile Start#####
+obj-y := mycall.o
+#####Makefile End#######
+
+Top level makefile
+------------------
+*Edit /usr/src/linux/Makefile
+*Add mycall/ to core-y (Search for regex: core-y.*+=)
+
+User space header file	
+----------------------
+*Create a header file called testmycall.h
+*This header file should be included by any program calling our system call.
+*Add three lines to it
+
+--Line 1:
+	#include<linux/unistd.h>
+	This is needed because we need the definition of _syscall1.
+--Line 2:
+	#define __NR_mycall 317
+	This is needed because we need the number of our system call.
+--Line 3:
+	_syscall1(long, mycall, int, i)
+	This is needed for system calls with 1 argument. It is explained in detail
+        below.
+*So, our user header file looks like:
+
+/*---Start of header file------*/
+#include<linux/unistd.h>
+#define __NR_mycall 317
+_syscall1(long, mycall, int, i)
+/*---End of header file--------*/
+
+User space C program to test our system call
+--------------------------------------------
+*Create a C file called testmycall.c in the same directory as
+ testmycall.h
+*The C file will look like:
+
+/*---Start of C file------*/
+#include<stdio.h>
+#include "testmycall.h"
+
+main()
+{
+        printf("%d\n", mycall(15));
+}
+/*---End of C file------*/
+
+_syscallN macro
+---------------
+*_syscall0(int,mycall) indicates that:
+--The name of the system call is mycall
+--It takes zero arguments
+--It returns an int
+*_syscall1(int,mycall,int,number) indicates that:
+--The name of the system call is mycall
+--It takes one argument
+--The argument is an int named number
+--It returns an int
+*Lets expand _syscall1(long,mycall,int,i)
+long mycall(int i)
+{
+	return syscall(__NR_mycall, i);
+}
+*But the definition of _syscallN macros are different in the kernel.
+ You can look at /usr/src/linux/include/asm-i386/unistd.h for the
+ definition.
+
+Recompiling the kernel
+----------------------
+*Now you have to recompile and install the new kernel.
+
+Recompiling the user space C file
+---------------------------------
+*Compile and execute the user space C file that we created above.
+*You should see the output as 25. This has been tested on kernel 2.6.17.13.
+
+Questions And Suggestions
+-------------------------
+*Please send a mail to amit2030@gmail.com (Amit Choudhary).

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] implement-system-call-mini-HOWTO.txt
  2006-11-13  6:14 [PATCH] implement-system-call-mini-HOWTO.txt Amit Choudhary
@ 2006-11-13  8:04 ` Arjan van de Ven
  2006-11-13  9:23 ` Jan Engelhardt
  1 sibling, 0 replies; 3+ messages in thread
From: Arjan van de Ven @ 2006-11-13  8:04 UTC (permalink / raw)
  To: Amit Choudhary; +Cc: Linux Kernel

On Sun, 2006-11-12 at 22:14 -0800, Amit Choudhary wrote:
> Description: A mini HOWTO on implementing a new system call. 

> Although, I think that implementing new system calls is discouraged, many people do end up in implementing proprietary system calls. 



what do you mean by that? Do you mean open source-but-forked ? Yes that
is bad, because applications written to use that system call will not
work later on when the upstream kernel uses your system call number for
something else....




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] implement-system-call-mini-HOWTO.txt
  2006-11-13  6:14 [PATCH] implement-system-call-mini-HOWTO.txt Amit Choudhary
  2006-11-13  8:04 ` Arjan van de Ven
@ 2006-11-13  9:23 ` Jan Engelhardt
  1 sibling, 0 replies; 3+ messages in thread
From: Jan Engelhardt @ 2006-11-13  9:23 UTC (permalink / raw)
  To: Amit Choudhary; +Cc: Linux Kernel


On Nov 12 2006 22:14, Amit Choudhary wrote:
>+*How is it invoked?
>+--A library routine issues a trap to the OS by executing INT 0x80 assembly instruction.
>+--The system call number is passed through the EAX register.
>+--The arguments are passed through registers  EBX, ECX, etc.

A nice short description, however, not all of the world runs x86 and may 
not know E?X. Just mention that you will describe the process based on 
x86.

>+*The Makefile in dir "mycall" will have only one line:
>+
>+#####Makefile Start#####
>+obj-y := mycall.o
>+#####Makefile End#######

I am tempted to use  obj-y += mycall.o

Generally, one does not need a new directory for a new syscall, or a new 
source file. You can edit kernel/sys.c if you like.

>+--Line 3:
>+	_syscall1(long, mycall, int, i)
>+	This is needed for system calls with 1 argument. It is explained in detail
>+        below.

This was deprecated in favor of syscall().

>+#include<stdio.h>
>+#include "testmycall.h"
>+
>+main()
>+{
>+        printf("%d\n", mycall(15));
>+}

Care to use ANSI C?


	-`J'
-- 

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2006-11-13  9:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-11-13  6:14 [PATCH] implement-system-call-mini-HOWTO.txt Amit Choudhary
2006-11-13  8:04 ` Arjan van de Ven
2006-11-13  9:23 ` Jan Engelhardt

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.