鴨子
    • 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Project 2 ### 組員名單 * 110525015 劉松靄 * 111525013 何雋永 * 109502547 楊晴方 ### 系統環境 * 作業系統: ubuntu 18.04 * Kernel 版本: 5.4.0-131-generic ### 新增 syscall 過程 由於篇幅較多,因此另外寫了一篇,在[這裡](https://hackmd.io/7x2suD5FRPuoavelhqUP4Q)。 ### Project 1 有些補充的知識點在 project 1,所以在此留個[傳送門](https://hackmd.io/4SgqE4UARS-VcxfUWyLSjA?both)。 ### kernel space code ``` #include <linux/init.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/sched.h> #include <linux/mm.h> #include <linux/syscalls.h> #include <linux/uaccess.h> int segNum = 6; int *data[20]; char* segmentName[6] = {"BSS", "Text", "Data", "Heap", "Stack", "Shared library"}; void printVMA(struct task_struct *task){ struct mm_struct *mm = task->mm; struct vm_area_struct *vma; int count = 0, index = 0; printk("\nText Segment start = 0x%lx, end = 0x%lx\n" "\nData Segment start = 0x%lx, end = 0x%lx\n" "\nStack Segment start = 0x%lx\n" "\nHeap Segment start = 0x%lx, end = 0x%lx\n", mm->start_code, mm->end_code, mm->start_data, mm->end_data, mm->start_stack, mm->start_brk, mm->brk); for(; index < segNum; index++){ count = 0; for(vma = mm->mmap; vma; vma = vma->vm_next){ ++count; if(data[index] >= vma->vm_start && data[index] <= vma->vm_end){ printk("\n%s Segment is in vma %d\n", segmentName[index], count); printk("\nStarts at 0x%lx, Ends at 0x%lx\n", vma->vm_start, vma->vm_end); } } } } void convertToPhysical(struct task_struct *task){ pgd_t *pgd; p4d_t *p4d; pud_t *pud; pmd_t *pmd; pte_t *pte; unsigned long paddr=0, page_addr=0, page_offset=0; int index=0; for(; index<segNum; index++){ pgd = pgd_offset(task->mm, data[index]); if(pgd_none(*pgd)){ printk("not mapped in pgd\n"); return; } p4d = p4d_offset(pgd, data[index]); if(p4d_none(*p4d)){ printk("not mapped in p4d\n"); return; } pud = pud_offset(p4d, data[index]); if(pud_none(*pud)){ printk("not mapped in pud\n"); return; } pmd = pmd_offset(pud, data[index]); if(pmd_none(*pmd)){ printk("not mapped in pmd\n"); return; } pte = pte_offset_kernel(pmd, data[index]); if(pte_none(*pte)){ printk("not mapped in pte\n"); return; } page_addr = pte_val(*pte) & PAGE_MASK; page_offset = (unsigned long)data[index] & ~PAGE_MASK; paddr = page_addr | page_offset; printk("\nphysical address %s Segment is 0x%lx\n", segmentName[index], paddr); } } SYSCALL_DEFINE2(printSegment, int, pid, const int*, userData){ copy_from_user(data, userData, sizeof(data)); printk("\ntask id is %d\n", current->pid); convertToPhysical(current); printVMA(current); return 0; } ``` ### user space code ``` #include <stdio.h> #include <unistd.h> #include <sys/syscall.h> #include <linux/kernel.h> int initData = 1, cnt = 0; int notInitData; int *userData[20] = {0}; int main(){ int a = 1; int *p = malloc(sizeof(int)); printf("%d\n", initData); userData[cnt++] = &notInitData; userData[cnt++] = main; userData[cnt++] = &initData; userData[cnt++] = p; userData[cnt++] = &a; userData[cnt++] = printf; syscall(336, (int)syscall(SYS_gettid), userData); sleep(5); return 0; } ``` ### kernel 輸出結果 ``` [278649.368720] task id is 31499 [278649.368730] physical address BSS Segment is 0x800000021d832100 [278649.368736] physical address Text Segment is 0x206e6f77a [278649.368739] physical address Data Segment is 0x800000021d832010 [278649.368742] physical address Heap Segment is 0x800000025b9a9260 [278649.368745] physical address Stack Segment is 0x800000020cebe39c [278649.368747] physical address Shared library Segment is 0x110d3de40 [278649.368750] Text Segment start = 0x557d93c00000, end = 0x557d93c00ae0 Data Segment start = 0x557d93e00d98, end = 0x557d93e01014 Stack Segment start = 0x7ffef193f490 Heap Segment start = 0x557d94df7000, end = 0x557d94e18000 [278649.368758] BSS Segment is in vma 3 [278649.368761] Starts at 0x557d93e01000, Ends at 0x557d93e02000 [278649.368765] Text Segment is in vma 1 [278649.368767] Starts at 0x557d93c00000, Ends at 0x557d93c01000 [278649.368770] Data Segment is in vma 3 [278649.368772] Starts at 0x557d93e01000, Ends at 0x557d93e02000 [278649.368775] Heap Segment is in vma 4 [278649.368777] Starts at 0x557d94df7000, Ends at 0x557d94e18000 [278649.368780] Stack Segment is in vma 15 [278649.368783] Starts at 0x7ffef1920000, Ends at 0x7ffef1941000 [278649.368785] Shared library Segment is in vma 5 [278649.368788] Starts at 0x7f8ca4000000, Ends at 0x7f8ca41e7000 [278651.842351] task id is 31500 [278651.842361] physical address BSS Segment is 0x800000024909c100 [278651.842367] physical address Text Segment is 0x206e6f77a [278651.842370] physical address Data Segment is 0x800000024909c010 [278651.842373] physical address Heap Segment is 0x800000023360d260 [278651.842375] physical address Stack Segment is 0x8000000247e2f3dc [278651.842378] physical address Shared library Segment is 0x110d3de40 [278651.842381] Text Segment start = 0x55b30fc00000, end = 0x55b30fc00ae0 Data Segment start = 0x55b30fe00d98, end = 0x55b30fe01014 Stack Segment start = 0x7ffe2b4e84d0 Heap Segment start = 0x55b311557000, end = 0x55b311578000 [278651.842389] BSS Segment is in vma 3 [278651.842392] Starts at 0x55b30fe01000, Ends at 0x55b30fe02000 [278651.842395] Text Segment is in vma 1 [278651.842398] Starts at 0x55b30fc00000, Ends at 0x55b30fc01000 [278651.842401] Data Segment is in vma 3 [278651.842403] Starts at 0x55b30fe01000, Ends at 0x55b30fe02000 [278651.842405] Heap Segment is in vma 4 [278651.842408] Starts at 0x55b311557000, Ends at 0x55b311578000 [278651.842410] Stack Segment is in vma 15 [278651.842413] Starts at 0x7ffe2b4c9000, Ends at 0x7ffe2b4ea000 [278651.842416] Shared library Segment is in vma 5 [278651.842419] Starts at 0x7f3ba5800000, Ends at 0x7f3ba59e7000 ``` ### 結論 從上圖的輸出結果可觀察到在兩個 process 中 Text、Shared library 這兩個 segment 的 physical address 都相同,所以它們是共用的。 ### Reference [https://www.jianshu.com/p/0520d6b76318](https://www.jianshu.com/p/0520d6b76318) [https://kkc.github.io/2020/08/22/file-descriptor/](https://kkc.github.io/2020/08/22/file-descriptor/) [https://stackoverflow.com/questions/32170711/how-is-the-code-segment-shared-between-processes-in-linux](https://stackoverflow.com/questions/32170711/how-is-the-code-segment-shared-between-processes-in-linux)

    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