Bin
    • 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
    • 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
    • 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 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
  • 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
    # XV6 A simple, Unix-like Teaching Operating System Chapter 1 --- > [TOC] --- ## Chapter 1 Operating system organization --- ### Kernel organization ![](https://i.imgur.com/nRL83kx.png) 讀取資料必須透過 kernel --- ### Process overview ![](https://i.imgur.com/5A4kSMy.png) --- - Process是一個抽象的概念,他是 Unix 中的一個隔離單元,即 Process 互相為個體。可以一個 Process 有自己的抽象機器,代表在 Virtual Memory 裡。一個 Process 看上去是一個私有的,其他 Process 無法對他 R/W ,也因為一顆 CPU 同時只能執行一個 Process 的關係讓此 CPU 看上去僅執行此 Process 而已。 - 依照上圖,由於先前說過把 Process 視為一個機器,所以他有分 Kernel 和User。 - 每個 Process 都有自己的 Page Table --- :::info [reference][http://fred-zone.blogspot.tw/2011/12/linux-kernel-process.html] - Process - Process is a program in execution. => 程式執行檔的 binary code + 記錄資料的記憶體 - Process 分成內外兩個角度來探討: 1. 從 Process 內部,程式執行的觀點 2. 從 Process 外部,也就是作業系統的觀點 - 程式執行的觀點: - Process 就是一般的程式碼被放到記憶體執行。一支程式擁有著數個 segments(區段),並仰賴著這些 segment 來持續運作。大致上來說,每個 segment 有不同的用途: - Code Segment - 存放主要程式 - Data Segment - 存放已被初始化並賦予值的全域變數 - BSS Segment - 紀錄尚未被賦予值的全域變數 - Stack Segment(Stack/Heap) - 紀錄 Process 在執行時動態註冊的變數包括 function 中的 local variable - 作業系統的觀點 - 作業系統有記憶體管理機制,其建立虛擬記憶體空間,將程式對映進去該空間後,開始執行。作業系統管理著 Process 所擁有的 Segments,為每個 Segment 都配置了『虛擬記憶體區域(VMA, Virtual Memory Area)』。 ::: --- :::warning - 使用虛擬記憶體之優點 1. 程式不再受到實際記憶體可用空間限制,記憶體可用空間變大。 2. 有更多程式能同時運用記憶體,增加了CPU 使用率。 3. 載入程式或置換程式所須 I/O 次數減少,速度加快。 ::: --- ## 20170712 Meeting - 之後先報 - Open RISK 1200 - WISHBONE - 再回來報 XV-6 --- ### Process Code - Code: The First Address Space - 說明 xv6 如何為 Kernel 製造第一個 Process 1. 開機 2. 初始化自己 3. 從硬碟讀取 Boot Loader 到記憶體並執行(Appendix B 有說明細節) 4. Boot Loader 會把 xv6 Kernel 讀到 **實體記憶體** 0x100000。 :::warning 此時 Page Hardware 不啟用,因此 Kernel 不算完全啟動,同時記憶體位置是直接映射到實體位址的。 ::: 5. 為了允許內核的其餘部分運行,設置一個 Page Table,將從 0x80000000 開始的虛擬地址(稱為 KERNBASE) 映射到從 0x0 開始的物理地址。設置兩個虛擬地址映射到相同物理內存是 Page Table 的常用的用法。 ![](https://i.imgur.com/zNMxszZ.png) 6. Enabled Paging Hardware 7. 讓 Kernel 在高位址上面跑 1. 將 %esp 指向高位址的 Stack 記憶體 2. Jump 位於高位址的 Main --- - Code: Creating The First Process 1. 調用 Userinit 建立第一個 Process (只有第一個會調用) ```c= // Set up first user process. void userinit(void) { struct proc *p; extern uchar _binary_initcode_start[], _binary_initcode_size[]; p = allocproc(); initproc = p; // Initialize memory from initcode.S p->sz = PAGE; p->mem = kalloc(p->sz); memmove(p->mem, _binary_initcode_start, (int)_binary_initcode_size); memset(p->tf, 0, sizeof(*p->tf)); p->tf->cs = (SEG_UCODE << 3) | DPL_USER; p->tf->ds = (SEG_UDATA << 3) | DPL_USER; p->tf->es = p->tf->ds; p->tf->ss = p->tf->ds; p->tf->eflags = FL_IF; p->tf->esp = p->sz; p->tf->eip = 0; // beginning of initcode.S safestrcpy(p->name, "initcode", sizeof(p->name)); p->cwd = namei("/"); p->state = RUNNABLE; } ``` 2. Userinit 調用 Allocproc (每個都會掉用) 3. Allocproc 在 Process Table 中分配一個 Slot (Struct Proc) 並初始化 Process 與 Kernel Thread 有關的 4. Allocproc 掃描 Proc Table 找到 p->state 是 **UNUSED** 並把它標記為 **EMBRYO** 且給他一個 Pid 代表有人用 5. Allocproc 嘗試請求分配一個 Kernel Stack ,若失敗則把前面 p->state 復原 **UNUSED** ```c= // File:proc.c // Look in the process table for an UNUSED proc. // If found, change state to EMBRYO and return it. // Otherwise return 0. static struct proc* allocproc(void) { struct proc *p; acquire(&ptable.lock); for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){ if(p->state == UNUSED){ p->state = EMBRYO; p->pid = nextpid++; goto found; } } release(&ptable.lock); return 0; found: release(&ptable.lock); // Allocate kernel stack if necessary. if((p->kstack = kalloc(KSTACKSIZE)) == 0){ p->state = UNUSED; return 0; } p->tf = (struct trapframe*)(p->kstack + KSTACKSIZE) - 1; // Set up new context to start executing at forkret (see below). p->context = (struct context *)p->tf - 1; memset(p->context, 0, sizeof(*p->context)); p->context->eip = (uint)forkret; return p; } ``` ```c= // File:proc.c // A fork child's very first scheduling by scheduler() // will swtch here. "Return" to user space. void forkret(void) { // Still holding ptable.lock from scheduler. release(&ptable.lock); // Jump into assembly, never to return. forkret1(cp->tf); } ``` ![](https://i.imgur.com/H8sOroj.png) --- - Code: Running The First Process - 在 Call "Userinit" , "Mpmain" 之後,為了讓他跑起來要去呼叫 "Scheduler"。 1. 利用指令啟動此 Processor 的 Interrupts 2. 利用 For Loop 尋找即將起動的 Process ( 這時因為只有一個 Process 所以只會找到剛剛新建的那個 ) 3. 利用 Switchuvm 告訴硬件開始使用目標 Process 的 Page Table 4. 將 Process p->state 改成 RUNNABLE 5. 呼叫 "Swtch" 做 Context Switch - 主程式 ```c= // File:proc.c // PAGEBREAK: 42 // Per-CPU process scheduler. // Each CPU calls scheduler() after setting itself up. // Scheduler never returns. It loops, doing: // - choose a process to run // - swtch to start running that process // - eventually that process transfers control // via swtch back to the scheduler. void scheduler(void) { struct proc *p; for(;;){ // Enable interrupts on this processor, in lieu of saving intena. sti(); // Loop over process table looking for process to run. acquire(&ptable.lock); for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){ if(p->state != RUNNABLE) continue; // Switch to chosen process. It is the process's job // to release ptable.lock and then reacquire it // before jumping back to us. cp = p; usegment(); p->state = RUNNING; swtch(&c->context, &p->context); // Process is done running for now. // It should have changed its p->state before coming back. cp = 0; usegment(); } release(&ptable.lock); } } ``` - Usegment() ```c= // File:proc.c // Set up CPU's segment descriptors and task state for the current process. // If cp==0, set up for "idle" state for when scheduler() is running. void usegment(void) { pushcli(); c->ts.ss0 = SEG_KDATA << 3; if(cp) c->ts.esp0 = (uint)(cp->kstack + KSTACKSIZE); else c->ts.esp0 = 0xffffffff; if(cp){ c->gdt[SEG_UCODE] = SEG(STA_X|STA_R, (uint)cp->mem, cp->sz-1, DPL_USER); c->gdt[SEG_UDATA] = SEG(STA_W, (uint)cp->mem, cp->sz-1, DPL_USER); } else { c->gdt[SEG_UCODE] = SEG_NULL; c->gdt[SEG_UDATA] = SEG_NULL; } c->gdt[SEG_TSS] = SEG16(STS_T32A, (uint)&c->ts, sizeof(c->ts)-1, 0); c->gdt[SEG_TSS].s = 0; lgdt(c->gdt, sizeof(c->gdt)); ltr(SEG_TSS << 3); popcli(); } ``` - Swtch() ```c= # File:swtch.S # void swtch(struct context **old, struct context **new); # # Save current register context in old # and then load register context from new. .globl swtch swtch: movl 4(%esp), %eax movl 8(%esp), %edx # Save old callee-save registers pushl %ebp pushl %ebx pushl %esi pushl %edi # Switch stacks movl %esp, (%eax) movl (%edx), %esp # Load new callee-save registers popl %edi popl %esi popl %ebx popl %ebp ret ``` --- - The first system call: exec - 第二章後面 --- ### Real world - 大多數操作系統都採用 Process 概念,大多數 Process 看起來與 xv6 相似。 一個真正的操作系統將會找到具有明確的 Free Proc 結構還有 Free List,而不是在 allocproc 中的線性時間搜索; 為了簡單起見,xv6使用線性掃描(許多的第一個)。 - xv6 的地址空間佈局存在不能使用超過 2GB 物理 RAM 的缺陷。 可以解決這個問題,但最好的方案是切換到64位地址的機器。

    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