LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 05-23-2011, 03:33 PM   #1
kalyan.m
LQ Newbie
 
Registered: May 2011
Posts: 6

Rep: Reputation: Disabled
Adding a system Call to Kernel


I am able to add a system call to my kernel, but i am unable to see that it is working.

2.6.32-31-generic ;i686 GNU/Linux

Version is 10.04.1; 10.04.2, 10.10, I tried on all of this versions

Please help me with this,

Regards,
Kalyan
 
Old 05-23-2011, 11:47 PM   #2
bsat
Member
 
Registered: Feb 2009
Posts: 347

Rep: Reputation: 72
How are you testing it ? Just calling it from a "c" program will not work. You will have to add it to the gcc library also.
If you have access to the book
http://www.amazon.com/Linux-Kernel-D.../dp/0672325128

It gives you details how you can do the call from user space.
 
Old 05-24-2011, 11:15 PM   #3
kalyan.m
LQ Newbie
 
Registered: May 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
@bsat :
I tried it but no use, I studied that book also
 
Old 05-25-2011, 11:15 PM   #4
bsat
Member
 
Registered: Feb 2009
Posts: 347

Rep: Reputation: 72
what is the problem you are facing.... ?
 
Old 06-05-2011, 04:02 AM   #5
kalyan.m
LQ Newbie
 
Registered: May 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
@bsat: Procedure I used to add system call, Can you spot me what is wrong in this?

•The directory/file naming convention may include either i386 (older versions) or x86, or x64 as welll as suffixes _32 or _64.
•Add new call to syscall table
◦gedit /usr/src/mylinux/arch/x86/kernel/syscall_table_32.S &
◦Add “.long sys_test_call” to the bottom of the file. Pay attention to use of spaces and TABs as white space in this and all other files that you are going to edit.
•Add new call to unistd.h
◦gedit /usr/src/mylinux/include/asm-generic/unistd.h &
◦Find and Increment NR_syscalls by one, i.e. if the number was 242, it should be now 243
◦Just above that line there should be a line contaning __NR_XXXX 241 that was one less than the original value of NR_syscalls.
Insert an additional entry shown below. If the last define is number in this section was 241 then use 242, which is one less than NR_syscalls.
/* test_call/. HOMEWORK only */
#define __NR_test_call 242
__SYSCALL(__NR_test_call, sys_test_call)
•Add new call prototype to syscalls.h
◦gedit /usr/src/mylinux/include/linux/syscalls.h &
◦Add at the end of the file asmlinkage long sys_test_call(unsigned short);
•Add test_call to core-y in Makefile
◦gedit /usr/src/mylinux/Makefile &
◦Search for core-y to find the line “core-y += kernel/ mm/ fs/ ipc/ security/ crypto/ block/”
◦Add “test_call/” to the end of the line
•Create a new directory for the call
◦mkdir test_call
◦cd test_call
•Create a new file in the test_call directory.
◦gedit /usr/src/mylinux/test_call/test_call.c &
◦Enter the following text
/*----------Start of test_call.c----------*/#include <linux/linkage.h>
#include <linux/kernel.h>

asmlinkage long sys_test_call(unsigned short n)
{
unsigned short i;
for (i=0; i<n; i++)
printk(KERN_EMERG "Kernel Hello World! by <insert your name here>\n");
return n;
}
/*-----------End of test_call.c-----------*/◦Save file as test_call.c in the test_call directory
•Create a Makefile in the test_call directory.
◦gedit /usr/src/mylinux/test_call/Makefile &
◦Enter the following text
########## Start of Makefile ##########
obj-y := test_call.o
########## End of Makefile ##########◦Save file as Makefile in the test_call directory
 
Old 06-05-2011, 04:03 AM   #6
kalyan.m
LQ Newbie
 
Registered: May 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
spot the error in above procedure

I am following the above procedure for adding a system call to kernel, Can you say me what is the error in the above procedure,
I am doing it on Ubuntu 10.04.2
 
Old 06-05-2011, 07:43 AM   #7
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Quote:
Originally Posted by kalyan.m View Post
I am following the above procedure for adding a system call to kernel, Can you say me what is the error in the above procedure,
I am doing it on Ubuntu 10.04.2
You need to tell us what error you are getting when you do this. Does the kernel compile with no warnings? Does the test code succeed? How do you know its not working?
 
Old 06-05-2011, 03:45 PM   #8
kalyan.m
LQ Newbie
 
Registered: May 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
@small pond

It is compiling but the code is never called.
 
Old 06-06-2011, 07:19 PM   #9
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Quote:
Originally Posted by kalyan.m View Post
It is compiling but the code is never called.
OK. So where is the code that attempts to call it and what does it return?
It should look like this:

Code:
       #define _GNU_SOURCE
       #include <unistd.h>
       #include <sys/syscall.h>
       #include <sys/types.h>
       #include <errno.h>

       main(int argc, char *argv[])
       {
           int res;
           errno = 0;
           res = syscall(__NR_test_call);
           printf("Result of syscall is %d.  errno = %d\n", res, errno);
       }
 
Old 06-06-2011, 08:46 PM   #10
kalyan.m
LQ Newbie
 
Registered: May 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Code I have used is

/*----------Start of test_call_run.c----------*/
#include <stdio.h>
#include <unistd.h> // to use syscall

// Reminding that sys_test_call function is filed under the system call 242
#define __NR_test_call 242

// Calling a macro to define a system call with N parameters that returns a long
long test_call()
{
return( syscall(__NR_test_call, 3) );
}

int main()
{
long status = test_call();
printf("Our test system call was called and returned status %ld\n", status);
return(0);
}
/*-----------End of test_call_run.c-----------*/
 
Old 06-07-2011, 08:46 AM   #11
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Your types are all wrong. syscall returns an int so that should not compile without a warning.
In your kernel code you use an unsigned short. Why?

Why didn't you say what it prints when you run it? Is it a secret?
 
Old 06-08-2011, 01:01 AM   #12
bsat
Member
 
Registered: Feb 2009
Posts: 347

Rep: Reputation: 72
Are you sure you are booting the newly compiled kernel.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reg : Adding a simple system call to the kernel kiranm Linux - Kernel 2 01-20-2011 09:03 AM
After adding a system call in the kernel, an error occured. corone Programming 1 08-26-2009 04:47 AM
adding a new system call to kernel in LFS lfs_rocks Linux From Scratch 1 05-03-2008 10:12 PM
adding a new system call to kernel in LFS lfs_rocks Linux - Kernel 1 04-28-2008 09:17 AM
Adding new System Call to Kernel kurt_ram Linux - General 1 10-22-2002 03:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 06:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration