Linux kernel add system call __linux

Source: Internet
Author: User

Kernel layer:

1. Kernel Arch/x86/kernel/syscall_table_32.s

. Long Sys_mysyscall/* 285 * * Available

2. Arch/x86/include/asm/unistd_32.h

#define __nr_mysyscall 285

3. Add function code

asmlinkage int Sys_mysyscall (int n)
{
PRINTK ("This are My syscall\n");
return n;
}

User layer:

2.6.15 Times Unistd.h also has the following macros:

_syscall0 (Type,name)

2.6.32 has abolished these macros, changed it to the Syscall function, used man syscall to view the usage, and the extra need to modify sys/syscall.h, which is actually bits/syscall.h.

1. Add #define Sys_mysyscall __nr_mysyscall, you can use Syscall (Sys_mysyscall, ...) To invoke the.

2. Add #define __nr_mysyscall 285 in/usr/include/asm/unistd_32.h.

If you do not do the above two steps, you can only use Syscall (285, ...) To invoke.


#define _GNU_SOURCE/* or _bsd_source or _svid_source * *
#include <unistd.h>
#include <sys/syscall.h>/* for SYS_XXX Definitions * *

int syscall (int number, ...);



The actual system call in the kernel is defined using the Syscall_definex in Include/linux/syscalls.h:


#ifdef Config_ftrace_syscalls
#define SYSCALL_DEFINEX (x, sname, ...) \
static const char *types_# #sname [] = {\
__sc_str_tdecl# #x (__va_args__) \
}; \
static const char *args_# #sname [] = {\
__sc_str_adecl# #x (__va_args__) \
}; \
Syscall_metadata (sname, x); \
__syscall_definex (x, sname, __va_args__)
#else
#define SYSCALL_DEFINEX (x, sname, ...) \
__syscall_definex (x, sname, __va_args__)
#endif

#ifdef config_have_syscall_wrappers

#define SYSCALL_DEFINE (name) static inline long sysc_# #name

#define __SYSCALL_DEFINEX (x, Name, ...) \
Asmlinkage long sys# #name (__sc_decl# #x (__va_args__)); \
Static inline long sysc# #name (__sc_decl# #x (__va_args__)); \
Asmlinkage long sys# #name (__sc_long# #x (__va_args__)) \
{                                                               \
__sc_test# #x (__va_args__); \
Return (long) sysc# #name (__sc_cast# #x (__va_args__)); \
}                                                               \
Syscall_alias (sys# #name, sys# #name); \
Static inline long sysc# #name (__sc_decl# #x (__va_args__))

#else/* config_have_syscall_wrappers * *


Example:


Syscall_define2 (gettimeofday, struct timeval __user *, TV,
struct TimeZone __user *, TZ)
{
if (likely (TV!= NULL)) {
struct Timeval KTV;
Do_gettimeofday (&AMP;KTV);
if (Copy_to_user (TV, &ktv, sizeof (KTV))
Return-efault;
}
if (Unlikely (TZ!= NULL)) {
if (Copy_to_user (TZ, &sys_tz, sizeof (SYS_TZ))
Return-efault;
}
return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.