how to add syscall on x86_64

http://blog.zhangsen.org/2008/12/how-to-add-syscall-on-x8664.html


There is little documentation about how to add a system call to Linux kernel on x86_64. This is how I do it.


Of course adding new system call is hardly a good idea. But my teacher simply gives us the homework ;-)

Things are different now in these aspects:

1. Entry.S no longer contains the syscall table; those tutorial mentioning entry.S don't apply to 2.6.

2. Now the i386 and x86_64 arch are merged into one: x86. (See arch and include directories.) However, some files, like syscall_table_32.S, don't have corresponding ones under x86_64.

For i386, here's a list of files to be edited, (see this tldp page):

1. arch/i386/kernel/syscall_table.S,
( now arch/x86/kernel/syscall_table_32.S)
2. include/asm-i386/unistd.h,
(now include/asm-x86/unistd_32.h)
3. include/linux/syscalls.h

For x86_64 arch, no corresponding syscall_table_64.S exists. To add new system calls to x86_64 kernel, edit these files:

1. include/asm-x86/unistd_64.h,

Add lines like:

#define __NR_my_call 288
__SYSCALL(__NR_my_call, sys_my_call)


2. include/linux/syscalls.h
Add a declaration here:

asmlinkage long sys_my_call(int n);

Of course, you can write the implementation in kernel/sys.c or another newfoo.c file and then writeMakefiles.



http://www.gossamer-threads.com/lists/linux/kernel/772035?do=post_view_threaded


On Sat, 19 May 2007 04:55:12 -0700 kernel coder wrote:

> hi,
>
> I'm trying to implement a system call for x86_64. Mine processor is
> dual core opetron.There is very little material on web for
> implementing system calls for x86_64 processor for 2.6 series kernel.I
> tried to implement a new system call by observing the existing
> implementation but to no success.Following are files names and changes
> made.

Your example is very CPU-independent, i.e., not x86_64-specific,
so following examples of recently-added syscalls should be good enough.

I used your "patch" below (with a few small modifications) on
2.6.22-rc2 and it worked fine.

Linux unicorn 2.6.22-rc2 #2 SMP Sun May 20 22:22:36 PDT 2007 x86_64 x86_64 x86_64 GNU/Linux
...
[ 98.369454] new system call


> //
> file-> include/asm-x86_64/unistd.h
>
> #define __NR_newcall 273
> __SYSCALL(__NR_newcall, sys_newcall)
>
> #define __NR_syscall_max __NR_newcall

syscall_max is no longer used.

>
> //
> file-> include/linux/syscalls.h
>
> asmlinkage unsigned long sys_newcall(char __user *buf);

not unsigned.

>
> /
> file--> fs/read_write.c
>
> asmlinkage unsigned long sys_newcall(char __user * buf){

not unsigned.

>
> printk("new system call \n");
> ret 0;

return 0;

> }
>
> EXPORT_SYMBOL_GPL(sys_write)

EXPORT_SYMBOL_GPL(sys_newcall);

> Please let me know where i'm doing wrong .Following is program which
> is calling mine system call
>
>
> #include <stdlib.h>
> #include <stdio.h>
> #include <sys/unistd.h>

#include <unistd.h>

> #include <sys/syscall.h>
>
> long int ret;
> int num = 243;
> char buffer=[20];

eh? does not compile.

>
> int main() {
>
>
> asm ("syscall;"
> : "=a" (ret)
> : "0" (num),
> "D" (buffer),
> );

I just used the syscall() glibc interface instead of asm:

ret = syscall(__NR_newcall);

> return ret;
> }
>
> When i call this ,nothing gets printed in file /var/log/messages.Am i
> missing something ?

Mostly typos...

> Actually i wana pass a pointer to kernel from user space.Later on data
> will be copied to that memory location .i am thinking of using
> copy_to_user for copying data.Buffer passed through system call will
> be used by kernel function as circular ring.And portions of this ring
> will get updated frequently even after system call has returned.
>
> Is there any better way to do this?

Sounds mostly OK to me.
Where are the ring head, tail, size, etc. maintained?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值