Results 1 to 10 of 10
i want to implement a system call in linux for my new idea in linux kernel and i try it like following :
//--------------------------------------------------------------------------------------------------------------------
Full path of the file - ...
- 07-23-2011 #1Just Joined!
- Join Date
- Jul 2011
- Posts
- 7
system call problem
i want to implement a system call in linux for my new idea in linux kernel and i try it like following :
//--------------------------------------------------------------------------------------------------------------------
Full path of the file - /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.
--------------------------------------------------------------------------------
6. unistd.h
Full path of the file - /usr/src/linux/include/asm-i386/unistd.h
This file contains the system call number that is passed to the kernel through the 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_vmsplice316", then add:
"#define __NR_mycall317" 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"
--------------------------------------------------------------------------------
7. syscalls.h
Full path of the file - /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);"
--------------------------------------------------------------------------------
8. Makefile
Full path of the file - /usr/src/linux/Makefile
Add mycall/ to core-y (Search for regex: core-y.*+=). You will be creating this directory. This directory will contain the source file, header file and the Makefile for our system call.
--------------------------------------------------------------------------------
9. New kernel files/directories to be created
--------------------------------------------------------------------------------
10. mycall
Full path of the file - /usr/src/linux/mycall
Create a new directory in /usr/src/linux and name it "mycall".
--------------------------------------------------------------------------------
11. mycall.c
Full path of the file - /usr/src/linux/mycall/mycall.c
Create a source file named "mycall.c" in dir "mycall". mycall.c 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?
Asmlinkage is used to look for the arguments on the kernel stack.
--------------------------------------------------------------------------------
12. Makefile
Full path of the file - /usr/src/linux/mycall/Makefile
The Makefile in dir "mycall" will have only one line:
#####Makefile Start#####
obj-y := mycall.o
#####Makefile End#######
--------------------------------------------------------------------------------
13. New user space files, to be created, to test our system call
--------------------------------------------------------------------------------
14. testmycall.h (new user space header file to be created)
testmycall.h
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: This is needed because we need the definition of _syscall1.
#include<linux/unistd.h>
Line 2: This is needed because we need the number of our system call.
#define __NR_mycall 317
Line 3: This is needed for system calls with 1 argument. It is explained in detail below.
_syscall1(long, mycall, int, i)
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--------*/
--------------------------------------------------------------------------------
15. testmycall.c (new user space source file to be created)
testmycall.c
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"
int main(void)
{
printf("%d\n", mycall(15));
}
/*---End of C file------*/
//---------------------------------------------------------------------------------------------------------------------------------------
but it's not working properly i dont know what is the problem and its very important for me to solve it
when i compile testmycall its print -1 or a number like 4956566123 and dont print any thing to /var/log/messages
im working with 2.6.34-12 Linux kernel
Please help me .
thanks a lot .
Regards,Last edited by sherko; 07-23-2011 at 07:59 PM.
- 07-23-2011 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,961
You built the kernel side as a kernel module? Did you install the mycall.ko file? If so, how did you do so? If so, can you see it with the "lsmod" command?
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 07-24-2011 #3Just Joined!
- Join Date
- Jul 2011
- Posts
- 7
no its a system call that use linkage.h in the toturials that i used there isnt any comment about .ko
i use make command in root of my kernel :
usr/src/linux/make
and mycall.o and built-in.o and modules.built-in and modules.order created automatically by kernel then i use this command : gcc testmycall.c -o testmycall to create testmycall.o and then use ./testmycall.o to execute it
- 07-24-2011 #4Just Joined!
- Join Date
- Jul 2011
- Posts
- 7
in the toturial says you should edit : usr/src/linux/arch/i386/kernel/syscall_table.s
but in my linux in the i386 ther isnt any kernel directory and my arch is i686 and i conclude that i should go to x86 directory and edit usr/src/linux/arch/x86/kernel/syscall_table_32.s
and for usr/src/linux/include/asm-i386/unistd.h too there isnt any asm-i386 directory then i have to go to asm-general
is there anyone that can help me ? is my work true or not? i think i should install a patch for my kernel to use syscall_table.s
- 07-24-2011 #5Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,961
As far as I know, if you are writing your own system call, then you will have to write kernel code to handle it.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 07-27-2011 #6Just Joined!
- Join Date
- Jul 2011
- Posts
- 7
I need a system call sample that working without any problem on linux kernel 2.6.34.12 with any method that u know.
i tryed to implement it like some tutorials at the internet but its not working properly becuase ther is n't syscall_table exported to use in kernel 2.6
- 08-01-2011 #7Just Joined!
- Join Date
- Jul 2011
- Posts
- 7
rulingminds and etc
i do it like rulingminds too but i have some problem with it :
1- in syscall_table_32.S i have 338 syscall entry but in unistd.h i have 244 entry i dont know when i write #define to my syscall , 244 is correct or 338
/usr/src/linux/arch/x86/kernel/syscall_table_32.S:
.long sys_rt_tgsigqueueinfo /* 335 */
.long sys_perf_event_open
.long sys_recvmmsg
.long sys_msg /*338* My syscall/
and
/usr/src/linux/include/asm-generic/unistd.h
#define __NR_recvmmsg 243
__SYSCALL(__NR_recvmmsg, sys_recvmmsg)
#define __NR_msg 244
#undef __NR_syscalls
#define __NR_syscalls 245
2- in syscalls.h i write it like this:
#ifndef _LINUX_SYSCALLS_H
#define _LINUX_SYSCALLS_H
.....
asmlinkage long sys_old_mmap(struct mmap_arg_struct __user *arg);
#endif
asmlinkage long sys_msg(const char *message);
***i dont khow it should be befor endif or after it
3- when i do make modoule_install there is some warning in allocating memory and when i compile my kernel to register this syscall and boot it there is some Error like this lib/modules/linux.2.6.39.3/modules.dep is not exist
- 08-05-2011 #8Just Joined!
- Join Date
- Jul 2011
- Posts
- 16
You should use arch/<arch>/include/asm/unistd.h. If urs is x86 use
arch/x86/include/asm/unistd_32.h or unistd_64.h
-Rulingminds
- 08-08-2011 #9Just Joined!
- Join Date
- Oct 2008
- Posts
- 4
Good day sherko. I think that the kernel part you did ok. The problem in user sp.
Try next source files.
/* testmycall.c ---Start---*/
#include<stdio.h>
extern long mycall(int);
int main(){
int k=0;
k= mycall(10);
printf("%d\n",k);
}
/* ---end---*/
-------------------------------------------
# test.s ---Start---
.text
.globl mycall
mycall:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %ebx
movl $317, %eax
int $0x80
movl %ebx, 8(%ebp)
popl %ebp
ret
# test.s ---end--
In order to compile this source files run next command:
gcc testmycall.c test.s -o testmycall
Here we use two C and Assembler files: testmycall.c, test.s
I hope that'll work. Good lucks.
More information about assembler proqramming and system functions
please refer to Programming From Groung Up tutorial of Jonathan Bartlett.
- 08-17-2011 #10Just Joined!
- Join Date
- Aug 2011
- Posts
- 3
i am sorry ,and who can help me? it is my ploblem.


Reply With Quote

