{"id":167,"date":"2024-04-23T09:29:12","date_gmt":"2024-04-23T01:29:12","guid":{"rendered":"https:\/\/yewpo.top\/?p=167"},"modified":"2024-04-23T09:29:12","modified_gmt":"2024-04-23T01:29:12","slug":"linux-%e6%b7%bb%e5%8a%a0%e7%b3%bb%e7%bb%9f%e8%b0%83%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.yewpo.top\/index.php\/167\/","title":{"rendered":"Linux \u6dfb\u52a0\u7cfb\u7edf\u8c03\u7528"},"content":{"rendered":"<blockquote><p>\u65e2\u7136\u9700\u8981\u6dfb\u52a0\u7cfb\u7edf\u8c03\u7528\uff0c\u5c31\u9700\u8981\u51c6\u5907\u597d Linux \u7684\u6e90\u7801\uff08\u4e00\u53e5\u5e9f\u8bdd\uff1a\uff09\u3002\u8fd9\u91cc\u662f\u57fa\u4e8e <a href=\"https:\/\/cdn.kernel.org\/pub\/linux\/kernel\/v5.x\/patch-5.10.215.xz\">5.10.215<\/a> \u7684\u5185\u6838\u7248\u672c\u4fee\u6539\u7684\u3002<\/p><\/blockquote>\n<h2>1 Linux \u6e90\u7801\u521d\u6b65\u63a2\u7d22<\/h2>\n<blockquote><p>\u521d\u6b21\uff08\u4e5f\u4e0d\u5b8c\u5168\u662f\u521d\u6b21\uff09\u9605\u8bfb Linux \u6e90\u7801\uff0c\u76ee\u6807\u6bd4\u8f83\u660e\u786e\uff0c\u76f4\u63a5\u51b2 <code>sysycall<\/code>\uff01<\/p><\/blockquote>\n<p>\u81ea\u5df1\u672c\u8eab\u5bf9 RISC-V \u67b6\u6784\u6bd4\u8f83\u719f\u6089\uff0c\u76f4\u63a5\u5728 <code>arch\/riscv<\/code> \u76ee\u5f55\u4e0b\u627e\u6587\u4ef6\u540d\u4e2d\u5e26 <code>syscall<\/code> \u7684\uff0c\u7ed3\u679c\u627e\u5230\u4e86\u4e00\u4e2a <code>riscv\/kernel\/syscall_table.c<\/code> \u7684\u4ee3\u7801\u3002\u8fd9\u4efd\u4ee3\u7801\u975e\u5e38\u7b80\u5355\uff0c\uff08\u8ddf\u6ca1\u5199\u6ca1\u5565\u533a\u522b\uff0c\uff09\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-c\">\/\/ SPDX-License-Identifier: GPL-2.0-only\n\/*\n * Copyright (C) 2009 Arnd Bergmann &lt;arnd@arndb.de&gt;\n * Copyright (C) 2012 Regents of the University of California\n *\/\n\n#include &lt;linux\/linkage.h&gt;\n#include &lt;linux\/syscalls.h&gt;\n#include &lt;asm-generic\/syscalls.h&gt;\n#include &lt;asm\/vdso.h&gt;\n#include &lt;asm\/syscall.h&gt;\n\n#undef __SYSCALL\n#define __SYSCALL(nr, call) [nr] = (call),\n\nvoid *sys_call_table[__NR_syscalls] = {\n    [0 ... __NR_syscalls - 1] = sys_ni_syscall,\n#include &lt;asm\/unistd.h&gt;\n};<\/code><\/pre>\n<p>\u8fd9\u4efd\u4ee3\u7801\u6700\u5173\u952e\u7684\u90e8\u5206\u5c31\u662f <code>sys_call_table<\/code> \u8fd9\u4e2a\u51fd\u6570\u6307\u9488\u6570\u7ec4\u4e86\uff0c\u518d\u5f15\u5165\u4e86\u4e00\u4e2a <code>asm\/unistd<\/code> \u7684\u5934\u6587\u4ef6\uff1b\u8fd8\u6709\u4e00\u70b9\uff0c\u5c31\u662f\u8fd9\u4e2a <code>__SYSCALL<\/code> \u5b8f\u4e86\u3002\u6b64\u5916\uff0c\u8fd8\u6709\u51e0\u4e2a\u5934\u6587\u4ef6\uff0c\u90a3\u6211\u4eec\u5c31\u4ece\u8fd9\u4e9b\u5934\u6587\u4ef6\u5165\u624b\u3002\u5148\u6765\u770b\u770b <code>asm\/syscall.h<\/code> \u7684\u4ee3\u7801\u3002<\/p>\n<p>\u76ee\u524d\u4e3a\u6b62\uff0c\u8fd8\u4e0d\u6e05\u695a <code>asm<\/code> \u662f\u90a3\u4e2a\u5355\u8bcd\u7684\u7f29\u5199\uff0c\u4f46\u662f\u53ef\u4ee5\u8bf4\u7684\u662f\uff0c<code>asm<\/code> \u7edd\u4e0d\u5728\u4e3b\u76ee\u5f55\u4e0b\u7684 <code>include<\/code> \u7684\u6587\u4ef6\u5939\u91cc\uff0c\u800c\u662f\u5728 <code>arch\/$ISA\/include<\/code> \u7684\u6587\u4ef6\u5939\u91cc\u3002<code>asm\/syscall.h<\/code> \u5b9a\u4e49\u4e86\u4e00\u4e9b\u67e5\u770b\u7cfb\u7edf\u8c03\u7528\u72b6\u6001\u7684\u51fd\u6570\uff0c\u4f8b\u5982\u83b7\u53d6\u8fd4\u56de\u503c <code>get_return_value<\/code>\u3001\u83b7\u53d6\u53c2\u6570 <code>get_arguments<\/code> \u7b49\u7cfb\u7edf\u8c03\u7528\u3002<\/p>\n<p>\u5728 <code>asm\/vdso.h<\/code> \u4e2d\uff0c\u5b9a\u4e49\u4e86 <code>vdso<\/code> \u7684\u6570\u636e\u7ed3\u6784\u4f53\u548c <code>vdso<\/code> \u7684\u6807\u8bc6\u5b8f\u3002\u6b64\u5916\uff0c\u8fd8\u5b9a\u4e49\u4e86\u4e00\u4e2a <code>riscv_flush_icache<\/code> \u7684\u7cfb\u7edf\u8c03\u7528\u58f0\u660e\u3002<code>vDSO<\/code> \u7528\u4e8e\u4e00\u4e9b\u5feb\u901f\u7684\u7cfb\u7edf\u8c03\u7528\uff0c\u800c\u4e0d\u9700\u8981\u5207\u6362\u5230\u5185\u6838\u6001\u3002<\/p>\n<blockquote><p>vDSO (virtual dynamic shared object) \u4e5f\u662f\u4e00\u79cd\u7cfb\u7edf\u8c03\u7528\u52a0\u901f\u673a\u5236\u3002 vDSO \u548c vsyscall \u7684\u57fa\u672c\u539f\u7406\u7c7b\u4f3c\uff0c\u90fd\u662f\u901a\u8fc7\u6620\u5c04\u5230\u7528\u6237\u7a7a\u95f4\u7684\u4ee3\u7801\u548c\u6570\u636e\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\uff0c\u6765\u8fbe\u5230\u52a0\u901f\u7684\u76ee\u7684\u3002 \u800c\u5b83\u4eec\u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\uff1a vDSO \u662f\u4e00\u4e2aELF \u683c\u5f0f\u7684\u52a8\u6001\u5e93\uff0c\u62e5\u6709\u5b8c\u6574\u7684\u7b26\u53f7\u8868\u4fe1\u606f\u3002<\/p><\/blockquote>\n<p>\u63a5\u4e0b\u6765\u7684\u4e09\u4e2a\u5934\u6587\u4ef6\uff0c\u90fd\u662f\u5728 <code>linux<\/code> \u6e90\u7801\u7684\u4e3b\u76ee\u5f55\u4e0b\u7684 <code>include<\/code> \u7684\u6587\u4ef6\u5939\u91cc\u3002\u5148\u6765\u770b\u4e00\u4e0b <code>asm-generic\/syscalls.h<\/code> \u7684\u4ee3\u7801\uff0c\u5728\u8fd9\u4e2a\u4ee3\u7801\u91cc\u9762\u58f0\u660e\u4e86\u4e09\u4e2a\u7cfb\u7edf\u8c03\u7528\uff0c\u5206\u522b\u662f <code>mmap2<\/code>\u3001<code>mmap<\/code>\u3001<code>rt_sigreturn<\/code> \u7cfb\u7edf\u8c03\u7528\u3002\u603b\u7ed3\u662f\u4e00\u4e9b\u67b6\u6784\u76f8\u5173\u7684\u7cfb\u7edf\u8c03\u7528\u3002<\/p>\n<p><code>linux\/syscalls.h<\/code> \u662f\u6700\u5173\u952e\u7684\u4e00\u4e2a\u5934\u6587\u4ef6\u4e86\u3002\u5148\u662f\uff0c\u58f0\u660e\u4e86\u5168\u90e8\u5185\u6838\u4e2d\u7684\u7cfb\u7edf\u8c03\u7528\u51fd\u6570\uff08<strong>\u5982\u679c\u6539\u67b6\u6784\u6ca1\u6709\u5b9e\u73b0 <code>syscall_wrapper<\/code><\/strong>\uff09\uff0c\u8fd8\u6709\u4e0e\u4e4b\u76f8\u5173\u7684\u7ed3\u6784\u4f53\u3002\u4e3a\u4e86\u4f7f\u8fd9\u4e9b\u7cfb\u7edf\u8c03\u7528\u548c\u67b6\u6784\u5206\u79bb\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u901a\u7528\u7684\u7cfb\u7edf\u8c03\u7528\u7684\u5b8f\uff08SYSCALL_DEFINEx\uff09\uff0c\u4ee5\u53ca\u76f8\u5173\u8f85\u52a9\u7cfb\u7edf\u8c03\u7528\u5b8f\u62c6\u89e3\u7684\u5b8f\uff08MAP\uff0cSC_DECL \u7b49\uff09\u3002\u66f4\u4e3a\u7ec6\u8282\u7684\u62c6\u89e3\u5b8f\uff0c\u5c31\u5728\u67b6\u6784\u4e2d\u7684\u76f8\u5173\u5b8f\u8fdb\u884c\u62c6\u89e3\uff08\u4f8b\u5982 x86 \u7684 <code>asm\/syscall_wrapper.h<\/code>\uff09\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u751f\u6210\u67b6\u6784\u76f8\u5173\u7684\u7cfb\u7edf\u8c03\u7528\u4e86\u3002\u4e86\u89e3\u5230\u8fd9\u4e2a\u5c42\u6b21\u5c31\u77e5\u9053\u90a3\u4e9b\u4ee3\u7801\u662f\u6bd4\u8f83\u91cd\u8981\u7684\u7684\u4e86\u3002<\/p>\n<p>\u6700\u540e\u4e00\u4e2a\u5934\u6587\u4ef6\u5c31\u662f <code>linux\/linkage.h<\/code>\uff0c\u4e00\u4e9b\u4e3a\u4e0d\u540c\u7f16\u8bd1\u5668\u63d0\u4f9b\u5b9a\u5236\u7684\u5b8f\uff0c\u53ef\u80fd\u7528\u4e8e\u6c47\u7f16\u4ee3\u7801\u6587\u4ef6\u4e2d\u3002\u5728\u6b64\u5904\u7406\u89e3 syscall\uff0c\u8fd9\u90e8\u5206\u4ee3\u7801\u4e0d\u662f\u7279\u522b\u91cd\u8981\u3002<\/p>\n<h2>2 x86 \u7684\u7cfb\u7edf\u8c03\u7528<\/h2>\n<p>\u4e3a\u4e86\u627e\u5230\u7cfb\u7edf\u8c03\u7528\u662f\u600e\u4e48\u8c03\u7528\u7684\uff0c\u4e0d\u59a8\u5148\u53bb\u627e\u4e00\u4e0b\u4e00\u4e2a\u7cfb\u7edf\u8c03\u7528\u7684\u5b9e\u73b0\u51fd\u6570\uff0c\u4f8b\u5982 <code>sys_read<\/code>\u3002\u901a\u8fc7\u547d\u4ee4\u884c\u67e5\u627e\uff0c\u4f46\u4f3c\u4e4e\u5e76\u6ca1\u6709\u627e\u5230\uff1f<\/p>\n<p>\u770b\u4e86\u4ee5\u4e0b\u7f51\u4e0a\u7684 Linux \u5185\u6838\u63ed\u79d8\uff0c\u7ed3\u679c\u53d1\u73b0\uff0c\u5e76\u4e0d\u662f\u7b80\u5355\u5730\u58f0\u660e <code>sys_read<\/code> \u4e86\u5f97\uff0c\u800c\u662f\u4f7f\u7528\u4e86\u4f7f\u7528\u4e86 <code>SYSCALL_DEFINEx<\/code> \u7684\u5b8f\u3002\u6309\u7167\u8fd9\u4e2a\u65b9\u5f0f\u4e00\u641c\uff0c\u8fd8\u771f\u641c\u5230\u4e86\u3002\u8fd9\u91cc\u4ee5 <code>chmod<\/code> \u7cfb\u7edf\u8c03\u7528\u4e3a\u4f8b\u5b50\u4e86\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-c\">SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode)\n{\n    return do_fchmodat(AT_FDCWD, filename, mode);\n}<\/code><\/pre>\n<p>\u4e3a\u4e86\u7406\u89e3\u4ee3\u7801\uff0c\u6211\u4eec\u9700\u8981\u62c6\u89e3\u8fd9\u4e2a <code>SYSCALL_DEFINE2<\/code> \u7684\u5b8f\u3002\u5982 1 \u8282\u5f53\u4e2d\u6240\u8ff0\uff0c\u5728 <code>linux\/syscall.h<\/code> \u4e2d\uff0c\u5b9a\u4e49\u4e86\u8fd9\u4e2a\u5b8f\uff0c\u4ee3\u7801\u4e3a <code>#define SYSCALL_DEFINE2(name, ...) SYSCALL_DEFINEx(2, _##name, __VA_ARGS__)<\/code>\uff1b\u5b8f\u91cc\u9762\u53c8\u5d4c\u5957\u4e86\u4e00\u4e2a <code>SYSCALL_DEFINEx<\/code> \u5b8f\uff0c\u8fd9\u4e2a\u5b8f\u5c31\u5728\u521a\u624d\u4ee3\u7801\u7684\u9644\u8fd1\u4e86\uff0c<strong>\u4f46\u662f\u8fd9\u91cc\u5e76\u6ca1\u6709\u4f7f\u7528\u8fd9\u4e2a\u5b8f\u5b9a\u4e49\uff0c\u5e94\u4e3a x86 \u67b6\u6784\u6709 syscall_wrap.h<\/strong>\u3002\u518d\u6765\u770b\u770b <code>SYSCALL_DEFINEx<\/code> \u5b8f\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-c\">#define __SYSCALL_DEFINEx(x, name, ...)                 \\\n    static long __se_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__)); \\\n    static inline long __do_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__));\\\n    __X64_SYS_STUBx(x, name, __VA_ARGS__)               \\\n    __IA32_SYS_STUBx(x, name, __VA_ARGS__)              \\\n    static long __se_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__))  \\\n    {                               \\\n        long ret = __do_sys##name(__MAP(x,__SC_CAST,__VA_ARGS__));\\\n        __MAP(x,__SC_TEST,__VA_ARGS__);             \\\n        __PROTECT(x, ret,__MAP(x,__SC_ARGS,__VA_ARGS__));   \\\n        return ret;                     \\\n    }                               \\\n    static inline long __do_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__))<\/code><\/pre>\n<p>\u4f7f\u7528 <code>name<\/code> \u6709\u70b9\u62bd\u8c61\uff0c\u51b5\u4e14\u540e\u9762\u8fd8\u6709 <code>sname<\/code>\uff0c\u6211\u4eec\u628a <code>name<\/code> \u66ff\u6362\u6210 <code>_##read<\/code>\uff0c\u5e76\u5256\u51fa\u4e0d\u662f\u7279\u522b\u91cd\u8981\u7684\u90e8\u5206\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-C\">#define __SYSCALL_DEFINEx(x, name, ...) \\\n    static long __se_sys_read(...); \\\n    static inline long __do_sys_read(...); \\\n    __X64_SYS_STUBx(x, name, __VA_ARGS__) \\\n    __IA32_SYS_STUBx(x, name, __VA_ARGS__) \\\n    static long __se_sys_read(...) { __do_sys_read(); } \\\n    static inline long __do_sys_read(...)<\/code><\/pre>\n<p>\u6700\u540e\u4e00\u884c\u7684 <code>__do_sys_read<\/code> \u7684\u51fd\u6570\u4e3b\u4f53\u90e8\u5206\u5c31\u5728\u5b9e\u73b0\u7cfb\u7edf\u8c03\u7528\u7684\u5730\u65b9\u4e86\u3002\u5728\u6b64\u5904\uff0c\u53ef\u4ee5\u660e\u663e\u7684\u770b\u51fa\u4e00\u4e2a\u8c03\u7528\u5173\u7cfb\uff0c<code>__se_sys_read<\/code> \u8c03\u7528\u4e86<code>__do_sys_read<\/code>\u3002\u63a5\u4e0b\u6765\u9700\u8981\u89e3\u5bc6\u7684\u662f <code>__X64_SYS_STUBx<\/code> \u548c <code>__IA32_SYS_STUBx<\/code>\u3002\u8fd9\u4e2a\u4e24\u4e2a\u5206\u522b\u5bf9\u5e94\u7684\u662f 64 \u4f4d\u67b6\u6784\u548c 32 \u4f4d\u67b6\u6784\uff0c\u6709\u4e14\u53ea\u6709\u4e00\u4e2a\u4f1a\u5b9e\u73b0\u3002\u8fd9\u91cc\u9009\u62e9\u7684\u662f 64 \u4f4d\u67b6\u6784\uff0c\u5c31\u53ea\u770b <code>__X64_SYS_STUBx(x, name, __VA_ARGS__)<\/code> \u7684\u5b8f\u3002<\/p>\n<pre><code class=\"language-c\">#define __X64_SYS_STUBx(x, name, ...)                   \\\n    __SYS_STUBx(x64, sys##name,                 \\\n            SC_X86_64_REGS_TO_ARGS(x, __VA_ARGS__))<\/code><\/pre>\n<p>\u7ee7\u7eed\uff0c\u5957\u7528\u4e86 <code>__SYS_STUBx<\/code> \u7684\u5b8f\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-c\">#define __SYS_STUBx(abi, name, ...)                 \\\n    long __##abi##_##name(const struct pt_regs *regs);      \\\n    ALLOW_ERROR_INJECTION(__##abi##_##name, ERRNO);         \\\n    long __##abi##_##name(const struct pt_regs *regs)       \\\n    {                               \\\n        return __se_##name(__VA_ARGS__);            \\\n    }<\/code><\/pre>\n<p>\u5316\u7b80\u4e4b\u540e\uff0c\u8fd9\u91cc\u5c31\u662f\u58f0\u660e\u4e86\u4e00\u4e2a <code>__x64_sys_read<\/code> \u51fd\u6570\uff0c\u800c\u4e14\u8fd9\u4e2a\u51fd\u6570\u8c03\u7528\u4e86 <code>__se_sys_read<\/code>\uff01\u5b8c\u6574\u7684\u8c03\u7528\u94fe\u6761\u5df2\u7ecf\u627e\u5230\u4e86\uff0c\u73b0\u5728\u5c31\u662f\u9700\u8981\u627e\u5230\uff0c\u4ec0\u4e48\u5730\u65b9\u8c03\u7528\u4e86\u8fd9\u4e2a <code>__x64_sys_read<\/code> \u7684\u51fd\u6570\u3002\u6b64\u65f6\uff0c\u60f3\u5230\u4e86\u5728\u7b2c 1 \u8282\u4e2d\u8bb2\u5230\u7684 <code>syscall_table<\/code>\u3002<\/p>\n<p>x86 \u7684 <code>syscall_table<\/code> \u662f\u5728 <code>arch\/x86\/entry\/syscall_64.c<\/code> \u6587\u4ef6\u4e2d\u58f0\u660e\u7684\uff0c\u4ee3\u7801\u683c\u5f0f\u548c <code>rsicv<\/code> \u7684\u5927\u540c\u5c0f\u5f02\u3002\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\uff0criscv \u5f15\u7528\u7684\u662f <code>asm\/unistd.h<\/code> \u7684\u5934\u6587\u4ef6\uff0c\u800c x86 \u4f7f\u7528\u7684\u662f <code>asm\/syscalls_64.h<\/code> \u7684\u5934\u6587\u4ef6\u3002\u4f46\u95ee\u9898\u662f\uff0c\u6211\u4eec\u5e76\u4e0d\u80fd\u5728 x86 \u7684 include \u4e0b\u9762\u627e\u5230\u8fd9\u4e2a\u5934\u6587\u4ef6\uff0c\u4f46\u662f\u53ef\u4ee5\u5728 <code>generated<\/code> \u7684\u6587\u4ef6\u5939\u4e2d\u627e\u5230\u3002\u53ef\u4ee5\u731c\u6d4b\u8fd9\u90e8\u5206\u662f\u81ea\u52a8\u751f\u6210\u7684\u3002\u518d\u770b\u770b <code>entry\/syscall<\/code> \u76ee\u5f55\uff0c\u6709\u4e00\u4e2a <code>syscall_64.tbl<\/code> \u6587\u4ef6\uff0c\u6587\u4ef6\u5185\u5bb9\u7684\u5927\u6982\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-c\">0   common  read            sys_read\n1   common  write           sys_write\n2   common  open            sys_open\n3   common  close           sys_close<\/code><\/pre>\n<p>\u6700\u540e\u751f\u6210\u7684\u4ee3\u7801\u6587\u4ef6\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-C\">__SYSCALL_COMMON(0, sys_read)\n__SYSCALL_COMMON(1, sys_write)\n__SYSCALL_COMMON(2, sys_open)\n__SYSCALL_COMMON(3, sys_close)<\/code><\/pre>\n<p><code>__SYSCALL_COMMON<\/code> \u7684\u5b8f\uff0c\u662f\u5728 <code>syscall_64.c<\/code> \u4e2d\u58f0\u660e\u7684\uff0c\u4f1a\u5c06\u5176\u8f6c\u5316\u4e3a <code>__SYSCALL_64<\/code> \u5b8f\uff0c<code>__SYSCALL_64<\/code> \u5b8f\u53c8\u751f\u6210 <code>__x64_sys_read<\/code> \u7684\u683c\u5f0f\u3002\u8fd9\u4e0b\uff0c\u4e5f\u5c31\u6e05\u695a <code>__x64_sys_read<\/code> \u662f\u5728\u54ea\u91cc\u8c03\u7528\u7684\u4e86\u3002<\/p>\n<h2>3 \u7cfb\u7edf\u8c03\u7528\u521d\u59cb\u5316<\/h2>\n<p>\u5728\u7b2c 2 \u8282\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u4e86\u89e3\u4e86\u7cfb\u7edf\u8c03\u7528\u7684\u58f0\u660e\u65b9\u5f0f\u548c\u5982\u4f55\u89e3\u5b8f\u5230\u67b6\u6784\u76f8\u5173\u7684\u4ee3\u7801\u3002\u90a3\u4e48\u5728\u7b2c 3 \u8282\uff0c\u6211\u4eec\u63a2\u7d22\u5e94\u7528\u7a0b\u5e8f\u89e6\u53d1\u7cfb\u7edf\u8c03\u7528\u7684\u65f6\u5019\u5982\u4f55\u8fdb\u5165\u5230\u7cfb\u7edf\u8c03\u7528\u7684\u5904\u7406\u51fd\u6570\u7684\u3002<\/p>\n<p>\u5728 <code>init\/main.c<\/code> \u7684\u4ee3\u7801\u4e2d\uff0c\u6709\u8d77\u59cb\u51fd\u6570 <code>start_kernel()<\/code>\u3002\u5185\u6838\u8fdb\u5165\u5230 <code>start_kernel<\/code> \u540e\uff0c\u4f1a\u8c03\u7528 <code>trap_init()<\/code> \u51fd\u6570\u5bf9\u9677\u5165\u8fdb\u884c\u521d\u59cb\u5316\u3002\u7136\u800c <code>trap_init()<\/code> \u7684\u58f0\u660e\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<pre><code class=\"language-c\">void __init __weak trap_init() {};<\/code><\/pre>\n<p>\u8fd9\u91cc\u7684 <code>__init<\/code> \u8868\u793a\u8be5\u51fd\u6570\u53ea\u4f1a\u5728\u7cfb\u7edf\u7cfb\u7edf\u7684\u65f6\u5019\u77ed\u6682\u5730\u88ab\u4f7f\u7528\uff1b\u800c <code>__weak<\/code> \u8868\u793a\u82e5\u6709\u5176\u4ed6\u540c\u540d\u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u88ab\u5ffd\u7565\u3002<\/p>\n<p><code>trap_init()<\/code> \u53ef\u80fd\u5c31\u662f\u4e00\u4e2a\u67b6\u6784\u76f8\u5173\u7684\u51fd\u6570\u3002\u5bf9\u4e0e x86 \u6765\u8bf4\uff0c\u5728 <code>arch\/x86\/kernel\/traps.c<\/code> \u4e2d\u58f0\u660e\u4e86 <code>trap_init()<\/code> \u51fd\u6570\uff0c\u51fd\u6570\u7684\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-C\">void __init trap_init(void)\n{\n    \/* Init cpu_entry_area before IST entries are set up *\/\n    setup_cpu_entry_areas();\n\n    \/* Init GHCB memory pages when running as an SEV-ES guest *\/\n    sev_es_init_vc_handling();\n\n    \/* Initialize TSS before setting up traps so ISTs work *\/\n    cpu_init_exception_handling();\n    \/* Setup traps as cpu_init() might #GP *\/\n    idt_setup_traps();\n    cpu_init();\n}<\/code><\/pre>\n<p>\u5173\u952e\u4e4b\u5904\u5728\u4e8e\u5728\u6700\u540e\u8c03\u7528\u4e86 <code>cpu_init()<\/code> \u7684\u51fd\u6570\u3002<code>cpu_init()<\/code> \u5728 <code>arch\/x86\/kernel\/cpu\/common.c<\/code> \u7684\u6587\u4ef6\u4e2d\uff0c\u5b83\u53c8\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u8c03\u7528\u4e86 <code>syscall_init()<\/code> \u51fd\u6570\u3002\u8be5\u51fd\u6570\u4e5f\u540c\u6837\u4f4d\u4e8e\u8be5\u6587\u4ef6\u4e2d\u3002<code>syscall_init()<\/code> \u51fd\u6570\u5b8c\u6210\u4e86\u4e24\u4ef6\u5f88\u91cd\u8981\u7684\u4e8b\u60c5\uff0c\u4e00\u4e2a\u662f\u5199 <code>MSR_STAR<\/code> \u5bc4\u5b58\u5668\uff0c\u53e6\u4e00\u4e2a\u662f\u5199 <code>MSR_LSTAR<\/code> \u5bc4\u5b58\u5668\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-C\">    wrmsr(MSR_STAR, 0, (__USER32_CS &lt;&lt; 16) | __KERNEL_CS);\n    wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64);<\/code><\/pre>\n<p>\u8fd9\u4e24\u4e2a\u5bc4\u5b58\u5668\u5199\u5165\u4e86\u7cfb\u7edf\u8c03\u7528\u7684\u5165\u53e3\u5730\u5740\u3002<\/p>\n<h2>4 \u6dfb\u52a0\u7cfb\u7edf\u8c03\u7528<\/h2>\n<p>\u4e24\u4e2a\u6b65\u9aa4\uff0c\u58f0\u660e\u52a0\u5b9e\u73b0\u3002<\/p>\n<h3>4.1 \u58f0\u660e<\/h3>\n<p>x86 \u662f\u811a\u672c\u751f\u6210\u76f8\u5173\u4ee3\u7801\uff0c\u6545\u53ea\u9700\u8981\u5728 <code>arch\/x86\/entry\/syscalls\/syscall_64.tbl<\/code> \u4eff\u7167\u5199\u4e00\u4e2a\u5373\u53ef\u3002\u8fd9\u91cc\u6211\u9009\u62e9\u7cfb\u7edf\u8c03\u7528\u53f7 441\u3002\u5bf9\u4e8e\u5176\u4ed6\u67b6\u6784\u7684\u58f0\u660e\uff0c\u4f8b\u5982 riscv\uff0c\u56e0\u4e3a\u6ca1\u6709 wrapper\uff0c\u6240\u4ee5\u9700\u8981\u5728 <code>include\/linux\/syscalls.h<\/code> \u4e2d\u58f0\u660e\u3002<\/p>\n<h3>4.2 \u5b9e\u73b0<\/h3>\n<p>\u9009\u62e9\u5728\u4e00\u4e2a\u6587\u4ef6\u5939\u4e2d\u5b9e\u73b0\u5373\u53ef\uff0c\u4f7f\u7528 <code>SYSCALL_DEFINE<\/code> \u76f8\u5173\u5b8f\u3002\u5728\u7cfb\u7edf\u8c03\u7528\u4e2d\uff0c\u8f93\u51fa\u4e00\u6761 <code>Hello, myself!<\/code> \u7684\u4fe1\u606f\uff0c\u5e76\u4e14\u8fd4\u56de 441\u3002<\/p>\n<h3>4.3 \u6d4b\u8bd5<\/h3>\n<p>\u7f16\u8bd1\u5185\u6838\uff01<\/p>\n<p>\u7f16\u5199\u4e00\u4e2a\u7528\u6237\u7a0b\u5e8f\uff0c\u4f7f\u7528\u5185\u8054\u6c47\u7f16\u89e6\u53d1\u7cfb\u7edf\u8c03\u7528\u3002x86 \u7684 64 \u4f4d\u5185\u6838\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 <code>syscall<\/code> \u547d\u4ee4\uff0c\u5728 <code>rax<\/code> \u5bc4\u5b58\u5668\u4e2d\u4f20\u9012\u7cfb\u7edf\u8c03\u7528\u53f7 441\u3002\u68c0\u67e5\u7cfb\u7edf\u8c03\u7528\u7684\u8fd4\u56de\u7ed3\u679c\uff0c\u5982\u679c\u662f 441\uff0c\u5219\u8bf4\u660e\u8c03\u7528\u6210\u529f\u3002<\/p>\n<p>\u6d4b\u8bd5\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n\nint main() {\n  int res;\n  asm volatile(\n      \"mov $441, %%rax\\n\"\n      \"syscall\\n\"\n      : \"=a\"(res)\n  );\n\n  if (res == 441) {\n    printf(\"Successfully call sys_myself system call!\\n\");\n  } else {\n    printf(\"Failed to call sys_myself system call!\\n\");\n  }\n\n  return 0;\n}<\/code><\/pre>\n<h3>4.4 patch<\/h3>\n<p>\u4fee\u6539\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-diff\">diff --git a\/arch\/x86\/entry\/syscalls\/syscall_64.tbl b\/arch\/x86\/entry\/syscalls\/syscall_64.tbl\nindex 3798192..969fdec 100644\n--- a\/arch\/x86\/entry\/syscalls\/syscall_64.tbl\n+++ b\/arch\/x86\/entry\/syscalls\/syscall_64.tbl\n@@ -362,6 +362,7 @@\n 438    common  pidfd_getfd     sys_pidfd_getfd\n 439    common  faccessat2      sys_faccessat2\n 440    common  process_madvise     sys_process_madvise\n+441 common  myself      sys_myself\n\n #\n # Due to a historical design error, certain syscalls are numbered differently\ndiff --git a\/fs\/open.c b\/fs\/open.c\nindex 83f62cf..044b43f 100644\n--- a\/fs\/open.c\n+++ b\/fs\/open.c\n@@ -140,6 +140,11 @@ long do_sys_truncate(const char __user *pathname, loff_t length)\n    return error;\n }\n\n+SYSCALL_DEFINE0(myself) {\n+  printk(\"Hello, myself!\\n\");\n+  return 441;\n+}\n+\n SYSCALL_DEFINE2(truncate, const char __user *, path, long, length)\n {\n    return do_sys_truncate(path, length);<\/code><\/pre>\n<h3>4.5 \u8fd0\u884c<\/h3>\n<p>\u542f\u52a8 Linux\uff0c\u5e76\u4e14\u5728\u8be5\u73af\u5883\u4e2d\u7f16\u8bd1\u6d4b\u8bd5\u4ee3\u7801 <code>gcc myprog.c -o myprog<\/code>\uff0c\u751f\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u8fd0\u884c\u5b83\u3002\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/raw.githubusercontent.com\/YEWPO\/yewpoblogonlinePic\/main\/image-20240418114043716.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" style=\"zoom: 50%;\" data-original=\"https:\/\/raw.githubusercontent.com\/YEWPO\/yewpoblogonlinePic\/main\/image-20240418114043716.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20240418114043716\" \/><\/div><\/p>\n<h2>5 \u5728 ARM \u67b6\u6784\u7684\u7cfb\u7edf\u8c03\u7528\u6dfb\u52a0<\/h2>\n<p>ARM \u67b6\u6784\u4e0b\u6dfb\u52a0\u7cfb\u7edf\u8c03\u7528\uff08\u9488\u5bf9\u662f Linux 6.6.7\uff09\u6bd4\u8f83\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5728 <code>include\/uapi\/asm-generic\/unistd.h<\/code>; \u6dfb\u52a0\u7cfb\u7edf\u8c03\u7528\u4fe1\u606f\uff0c\u5982\uff1a<\/p>\n<pre class=\"code\">#define __NR_mysyscall 454\n__SYSCALL(__NR_mysyscall, sys_mysyscall)<\/pre>\n<p>\u7136\u540e\u5c06 <code>__NR_syscalls<\/code> \u7684\u503c\u6539\u6210 455\u3002\u5728 Linux \u7684\u5176\u4ed6\u4f4d\u7f6e\u4e0a\uff0c\u6dfb\u52a0\u5408\u9002\u7684\u7cfb\u7edf\u8c03\u7528\u51fd\u6570\u5373\u53ef\u3002<\/p>\n<h2>\u53c2\u8003\u8d44\u6599<\/h2>\n<p><a href=\"https:\/\/zhuanlan.zhihu.com\/p\/436454953\">Linux vDSO\u6982\u8ff0<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/hust-open-atom-club\/linux-insides-zh\/blob\/master\/SysCall\/linux-syscall-1.md\">linux syscall 1<\/a><\/p>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/2535989\/what-are-the-calling-conventions-for-unix-linux-system-calls-and-user-space-f\">i386 \u548c x64 \u5185\u8054\u6c47\u7f16 syscall<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Linux 5.10 \u7248\u672c\u4e0a\u6dfb\u52a0\u7cfb\u7edf\u8c03\u7528\u5e76\u8bc1\u660e\u53ef\u4ee5\u88ab\u7528\u6237\u5e94\u7528\u8c03\u7528\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[10],"class_list":["post-167","post","type-post","status-publish","format-standard","hentry","category-os","tag-linux"],"_links":{"self":[{"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/posts\/167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/comments?post=167"}],"version-history":[{"count":0,"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/posts\/167\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/media?parent=167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/categories?post=167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yewpo.top\/index.php\/wp-json\/wp\/v2\/tags?post=167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}