Add a system call on x86_32 arch

來源:互聯網
上載者:User

diff --git a/Makefile b/Makefile
index a43733d..9e3bfef 100644
--- a/Makefile
+++ b/Makefile
@@ -708,7 +708,7 @@ export mod_strip_cmd
 
 
 ifeq ($(KBUILD_EXTMOD),)
-core-y        += kernel/ mm/ fs/ ipc/ security/ crypto/ block/
+core-y        += kernel/ mm/ fs/ ipc/ security/ crypto/ block/ mycall/
 
 vmlinux-dirs    := $(patsubst %/,%,$(filter %/, $(init-y) $(init-m) \
              $(core-y) $(core-m) $(drivers-y) $(drivers-m) \
diff --git a/arch/x86/kernel/syscall_table_32.S b/arch/x86/kernel/syscall_table_32.S
index 9a0e312..f208f64 100644
--- a/arch/x86/kernel/syscall_table_32.S
+++ b/arch/x86/kernel/syscall_table_32.S
@@ -348,3 +348,5 @@ ENTRY(sys_call_table)
     .long sys_setns
     .long sys_process_vm_readv
     .long sys_process_vm_writev
+    .long sys_mycall
+    .long sys_sillycopy             /* 350 */
diff --git a/mycall/Makefile b/mycall/Makefile
new file mode 100644
index 0000000..60ba8c8
--- /dev/null
+++ b/mycall/Makefile
@@ -0,0 +1 @@
+obj-y := mycall.o
diff --git a/mycall/mycall.c b/mycall/mycall.c
new file mode 100644
index 0000000..d071b72
--- /dev/null
+++ b/mycall/mycall.c
@@ -0,0 +1,39 @@
+/*
+ * =====================================================================================
+ *
+ *       Filename:  mycall.c
+ *
+ *    Description:  
+ *
+ *        Version:  1.0
+ *        Created:  12/19/2011 08:05:03 AM
+ *       Revision:  none
+ *       Compiler:  gcc
+ *
+ *         Author:  YOUR NAME (),
+ *        Company:  
+ *
+ * =====================================================================================
+ */
+#include <linux/syscalls.h>
+
+SYSCALL_DEFINE1(mycall, int, i)
+{
+    return i+10;
+}
+
+SYSCALL_DEFINE3(sillycopy,  
+        unsigned long *, src,
+        unsigned long *, dst,
+        unsigned long  , len)
+{
+    unsigned long buf[1024];
+
+    if (copy_from_user(buf, src, len))
+        return -EFAULT;
+
+    if (copy_to_user(dst, buf, len))
+        return -EFAULT;
+
+    return len;
+}
diff --git a/mycall/mycalltest.c b/mycall/mycalltest.c
new file mode 100644
index 0000000..71497ab
--- /dev/null
+++ b/mycall/mycalltest.c
@@ -0,0 +1,61 @@
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/syscall.h>
+#include <sys/types.h>
+
+int main()
+{
+    pid_t tid;
+    char buf1[20] = {"abcd"};
+    char buf2[20] = {0};
+    int  len = 5;
+
+    tid = syscall(SYS_gettid);
+    printf("sys_gettid call number is %d\n", SYS_gettid);
+    printf("sys_gettid return              : %d\n", tid);
+
+    /*call sys_gettid in assembly*/
+    __asm__(
+        "mov $224, %%eax\n"
+        "int $0x80\n"
+        "mov %%eax, %0"
+        :"=r"(tid)
+        );
+    printf("sys_gettid return from assembly: %d\n", tid);
+
+
+
+    tid = syscall(349, 15);
+    printf("sys_mycall return            : %d\n", tid);
+
+    /*call sys_mycall in assembly*/
+    __asm__(
+        "mov $349, %%eax\n"
+        "mov $15, %%ebx\n"
+        "int $0x80\n"
+        "mov %%eax, %0"
+        :"=r"(tid)
+        );
+    printf("sys_mycall in assembly return: %d\n", tid);
+
+    printf("buf2 is %s\n", buf2);
+    tid = syscall(350, buf1, buf2, 5);
+    printf("buf2 is %s\n", buf2);
+
+    buf2[0] = 'e';
+    printf("buf2 is %s\n", buf2);
+    /*call sys_sillycopy in assembly*/
+    __asm__(
+        "mov $350, %%eax\n"
+        "mov %1, %%ebx\n"
+        "mov %2, %%ecx\n"
+        "mov %3, %%edx\n"
+        "int $0x80\n"
+        "mov %%eax, %0"
+        :"=r"(tid)
+        :"r"(buf1), "r"(buf2), "r"(len)
+        :
+        );
+    printf("buf2 is %s\n", buf2);
+}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.