鴨子
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    1
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # add syscall to linux kernel (v6.0.6) ### 系統環境 * 作業系統: ubuntu 18.04 * Kernel 版本: 5.4.0-131-generic ### 下載 Kernel Source 到 [Kernel Archive](https://www.kernel.org/) 下載 kernel source,選擇最新的 6.0.6 kernel。 **<font size=4>解壓縮</font>** ``` # 進入 root 模式 sudo su # 把檔案解壓縮到 /usr/src 目錄底下 tar -xvf linux-6.0.6.tar.xz -C /usr/src ``` ### 新增 syscall **<font size=4>建立資料夾</font>** ``` # 把目錄轉到剛剛解壓縮完的 kernel 檔案夾 cd /usr/src/linux-6.0.6 # 在裡面創建一個名叫 hello 的資料夾 mkdir hello # 把目錄轉到 hello 資料夾 cd hello ``` **<font size=4>建立 hello.c</font>** ``` vim hello.c ``` **<font size=4>hello.c 的內容</font>** ``` #include <linux/kernel.h> #include <linux/syscalls.h> SYSCALL_DEFINE0(hello){ printk("Hello world!\n"); return 0; } ``` **<font size=4>於同一個目錄下再建立一個 Makefile</font>** ``` vim Makefile ``` **<font size=4>Makefile 的內容</font>** ``` obj-y := hello.o ``` **<font size=4>接著回上個目錄改 Makefile</font>** ``` # 回上個目錄 cd .. # 打開此目錄下的 Makefile vim Makefile ``` **<font size=4>可以找到 core-y 如下圖</font>** ![](https://i.imgur.com/Chuo6PJ.png) **<font size=4>在最後面補上 hello 這樣 kernel 在編譯時才會到 hello 目錄</font>** ``` core-y += kernel/ certs/ mm/ fs/ ipc/ security/ crypto/ hello/ ``` **<font size=4>接下來要去修改 syscall_64.tbl 檔</font>** ``` cd arch/x86/entry/syscalls vim syscall_64.tbl ``` **<font size=4>在最後一行添加上 system call,然後請把編號記住, 等一下會用</font>** ![](https://i.imgur.com/NCSUFZH.png) ``` 註:會發現下面突然跳到 400 多號,那是放 x32 的東西,我們要擺在 300 多號那邊 ``` **<font size=4>接著編輯 syscalls.h 檔,將 syscall 的原型添加進檔案 (#endif之前)</font>** ``` # 把目錄轉回去 cd /usr/src/linux-6.0.6 cd include/linux vim syscalls.h ``` ![](https://i.imgur.com/lkWyc8U.png) ### 編譯 kernel **<font size=4>先把會用到的套件安裝好</font>** ``` # 這個套件可以幫我們 build 出 kernel-pakage sudo apt-get install fakeroot build-essential kernel-package libncurses5 libncurses5-dev ``` **<font size=4>package configuration (會跳出一個視窗)</font>** ``` 選擇第二個,保持本地版本就好 ``` **<font size=4>複製 kernel 的 config</font>** ``` # 把目錄轉回去 cd /usr/src/linux-6.0.6 cp /boot/config-`uname -r` ./.config ``` **<font size=4>然後可以用 make menuconfig 來設定組態</font>** ``` make menuconfig ``` **<font size=4>會跳出一個畫面可以設定 configuration,直接離開就好,</font>** **<font size=4>然後就可以 compile 你的 kernel 了</font>** ``` # 清理殘留的 package make-kpkg clean # 編譯 kernel fakeroot make-kpkg -j 8 --initrd kernel_image kernel_headers # 等編譯完後會產生 kernel 的 image 和 headers ``` ### 編譯完成 **<font size=4>編譯完成後會在 /usr/src 路徑下產生了這個 kernel 的 image 和 headers, 回到上個目錄安裝它們</font>** ``` # 轉目錄 cd /usr/src # 安裝 kernel image sudo dpkg -i linux-image-6.0.6_6.0.6-10.00.Custom_amd64.deb # 安裝 kernel headers sudo dpkg -i linux-headers-6.0.6_6.0.6-10.00.Custom_amd64.deb # 安裝完後就重開機 reboot ``` **<font size=4>重開機後再看看 kernel 版本有沒有變新</font>** ``` uname -r ``` ![](https://i.imgur.com/7gwDRtu.png) ### 測試 syscall ``` # 創建一個 hello.c vim hello.c ``` **<font size=4>hello.c 的內容如下</font>** ``` #include <linux/kernel.h> #include <unistd.h> #include <sys/syscall.h> #include <stdio.h> int main(){ /* 使用我們剛剛新增的 system call */ long int sys = syscall(335); /* print 出 syscall 的回傳值, 若為 0 則代表成功 */ printf("sys_hello return %ld\n", sys); return 0; } ``` ``` # 編譯 hello.c gcc -o hello hello.c # 執行 ./hello ``` **<font size=4>使用 dmesg 來查看 kernel 內的訊息</font>** ``` dmesg ``` ![](https://i.imgur.com/1DVs02T.png) ### reference [https://wenyuangg.github.io/posts/linux/linux-add-system-call.html](https://wenyuangg.github.io/posts/linux/linux-add-system-call.html) [https://blog.csdn.net/cswhl/article/details/110919572](https://blog.csdn.net/cswhl/article/details/110919572) [https://linuxgazette.net/112/krishnakumar.html](https://linuxgazette.net/112/krishnakumar.html) [https://phoenixnap.com/kb/build-linux-kernel](https://phoenixnap.com/kb/build-linux-kernel) [https://biscuitos.github.io/blog/SYSCALL_PARAMENTER_ZERO/](https://biscuitos.github.io/blog/SYSCALL_PARAMENTER_ZERO/) [https://stackoverflow.com/questions/10949986/whats-meaning-of-obj-y-something-in-linux-kernel-makefile](https://stackoverflow.com/questions/10949986/whats-meaning-of-obj-y-something-in-linux-kernel-makefile)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully