Musings from the mountains himwant.org

xv6 kernel documentation!

+250 -143
-90
.github/workflows/astro.yml
··· 1 - # Sample workflow for building and deploying an Astro site to GitHub Pages 2 - # 3 - # To get started with Astro see: https://docs.astro.build/en/getting-started/ 4 - # 5 - name: Deploy Astro site to Pages 6 - 7 - on: 8 - # Runs on pushes targeting the default branch 9 - push: 10 - branches: ["main"] 11 - 12 - # Allows you to run this workflow manually from the Actions tab 13 - workflow_dispatch: 14 - 15 - # Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages 16 - permissions: 17 - contents: read 18 - pages: write 19 - id-token: write 20 - 21 - # Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued. 22 - # However, do NOT cancel in-progress runs as we want to allow these production deployments to complete. 23 - concurrency: 24 - group: "pages" 25 - cancel-in-progress: false 26 - 27 - env: 28 - BUILD_PATH: "." # default value when not using subfolders 29 - # BUILD_PATH: subfolder 30 - 31 - jobs: 32 - build: 33 - name: Build 34 - runs-on: ubuntu-latest 35 - steps: 36 - - name: Checkout 37 - uses: actions/checkout@v4 38 - - name: Detect package manager 39 - id: detect-package-manager 40 - run: | 41 - if [ -f "${{ github.workspace }}/yarn.lock" ]; then 42 - echo "manager=yarn" >> $GITHUB_OUTPUT 43 - echo "command=install" >> $GITHUB_OUTPUT 44 - echo "runner=yarn" >> $GITHUB_OUTPUT 45 - echo "lockfile=yarn.lock" >> $GITHUB_OUTPUT 46 - exit 0 47 - elif [ -f "${{ github.workspace }}/package.json" ]; then 48 - echo "manager=npm" >> $GITHUB_OUTPUT 49 - echo "command=ci" >> $GITHUB_OUTPUT 50 - echo "runner=npx --no-install" >> $GITHUB_OUTPUT 51 - echo "lockfile=package-lock.json" >> $GITHUB_OUTPUT 52 - exit 0 53 - else 54 - echo "Unable to determine package manager" 55 - exit 1 56 - fi 57 - - name: Setup Node 58 - uses: actions/setup-node@v4 59 - with: 60 - node-version: "20" 61 - cache: ${{ steps.detect-package-manager.outputs.manager }} 62 - cache-dependency-path: ${{ env.BUILD_PATH }}/${{ steps.detect-package-manager.outputs.lockfile }} 63 - - name: Setup Pages 64 - id: pages 65 - uses: actions/configure-pages@v5 66 - - name: Install dependencies 67 - run: ${{ steps.detect-package-manager.outputs.manager }} ${{ steps.detect-package-manager.outputs.command }} 68 - working-directory: ${{ env.BUILD_PATH }} 69 - - name: Build with Astro 70 - run: | 71 - ${{ steps.detect-package-manager.outputs.runner }} astro build \ 72 - --site "${{ steps.pages.outputs.origin }}" \ 73 - --base "${{ steps.pages.outputs.base_path }}" 74 - working-directory: ${{ env.BUILD_PATH }} 75 - - name: Upload artifact 76 - uses: actions/upload-pages-artifact@v3 77 - with: 78 - path: ${{ env.BUILD_PATH }}/dist 79 - 80 - deploy: 81 - environment: 82 - name: github-pages 83 - url: ${{ steps.deployment.outputs.page_url }} 84 - needs: build 85 - runs-on: ubuntu-latest 86 - name: Deploy 87 - steps: 88 - - name: Deploy to GitHub Pages 89 - id: deployment 90 - uses: actions/deploy-pages@v4
-4
.vscode/extensions.json
··· 1 - { 2 - "recommendations": ["astro-build.astro-vscode"], 3 - "unwantedRecommendations": [] 4 - }
-11
.vscode/launch.json
··· 1 - { 2 - "version": "0.2.0", 3 - "configurations": [ 4 - { 5 - "command": "./node_modules/.bin/astro dev", 6 - "name": "Development server", 7 - "request": "launch", 8 - "type": "node-terminal" 9 - } 10 - ] 11 - }
+1 -1
src/content/home.md
··· 6 6 7 7 # Akshit Gaur 8 8 9 - > **High Altitude, Low Level.** 9 + > **High Altitude, Low Level** 10 10 11 11 Systems Engineer and CS undergrad at JNU 12 12
-22
src/content/posts/hello.md
··· 1 - --- 2 - title: "Hello" 3 - published: 2025-10-30 4 - draft: false 5 - --- 6 - 7 - 8 - 9 - # Hello 10 - 11 - 12 - ## Everyone 13 - 14 - ```python 15 - print("HELLO!") 16 - print("World!") 17 - ``` 18 - 19 - :::caution 20 - Use caution while using python 21 - ::: 22 -
-15
src/content/posts/hello.org
··· 1 - #+title: Hello 2 - #+author: Akshit Gaur 3 - #+description: First Blog! 4 - #+TAGS: emacs, org, astro 5 - #+OPTIONS: toc:nil 6 - * Hello 7 - ** Everyone 8 - #+begin_src python 9 - print("HELLO!") 10 - print("World!") 11 - #+end_src 12 - 13 - :::caution 14 - Use caution while using python 15 - :::
+141
src/content/posts/xv6/index.md
··· 1 + --- 2 + title: "xv6" 3 + published: 2025-12-16 4 + draft: false 5 + description: 'Documenting the xv6 kernel' 6 + tags: ["xv6","os"] 7 + --- 8 + 9 + 10 + 11 + # Why?? 12 + 13 + I am currently going through the book ***Operating Systems: Three Easy Pieces***, and the next step after completing the first piece, Virtualisation, is to go through the xv6 kernel. The xv6 kernel had two implementations, the x86 one and RISC-V one. As the x86 one is now unmaintained and on recommendation of a mentor, I am using [xv6-riscv](https://github.com/mit-pdos/xv6-riscv)! So without further ado, let's get started. 14 + 15 + 16 + # The xv6 Kernel 17 + 18 + - Based on Unix kernel 19 + - Created by MIT for educational purposes 20 + - Implemented for RISC-V 21 + - Multicore 22 + - ~6000 lines of code (C and Assembly) 23 + 24 + 25 + ## Features 26 + 27 + - Processes 28 + - Virtual Address Spaces 29 + - Page Tables 30 + - Files and Directories 31 + - Pipes 32 + - Multitasking 33 + - Time-Slicing 34 + - 21 system calls 35 + 36 + 37 + ## User Programs 38 + 39 + - sh 40 + - cat 41 + - echo 42 + - grep 43 + - kill 44 + - ln 45 + - ls 46 + - mkdir 47 + - rm 48 + - wc 49 + 50 + 51 + ## What is missing? 52 + 53 + All the complexity of a "real" operating system is missing. For example, UserID, file protections, IPC, "mount"able filesystem, etc. 54 + 55 + 56 + ## SMP: Shared Memory Multiprocesser 57 + 58 + CPU = core = HART 59 + 60 + Main memory (RAM) is shared and is of size 128 Mbytes 61 + 62 + 63 + ## Supported Devices 64 + 65 + - UART (Serial Comm. Tx <=> Rx) 66 + - Disk 67 + - Timer Interrupts 68 + - PLIC: Platform Level Interrupt Controller 69 + - CLINT: Core Local Interrupt Controller 70 + 71 + 72 + ## Memory Management 73 + 74 + - Page Size = 4096 bytes (`#define PGSIZE`) 75 + - Single Free List 76 + - No variable sized allocation 77 + - No "malloc" 78 + - Page Tables- 79 + - Three Levels 80 + - One table per process + One table for Kernel 81 + - Pages marked - R/W/X/U/V 82 + 83 + 84 + ## Scheduler 85 + 86 + - Round Robin 87 + - Size of TimeSlice is fixed (1,000,000 cycles) 88 + - All cores share one "Ready Queue" 89 + - Next TimeSlice may be on a different core 90 + 91 + 92 + ## Boot Sequence 93 + 94 + - QEMU 95 + - Loads kernel code at a fixed address (0x8000-0000) 96 + - Starts all cores running 97 + - No bootloader/boot-block/BIOS 98 + 99 + 100 + ## Locking 101 + 102 + - Spin Locks 103 + - `sleep()` & `wakeup()` 104 + 105 + 106 + ## "Param.h" 107 + 108 + - Fixed Limits (like no. of processes, no. of open files, etc) 109 + - Several Arrays ("`kill(pid)`" => Linear search of Processes array) 110 + 111 + 112 + ## User Address Space 113 + 114 + ![img](./ua-space.jpeg "Sorry for the bad handwriting!") 115 + 116 + Arguments (argc, argv) will be placed on the stack before the program begins execution. 117 + 118 + 119 + ## RISC-V Virtual Addresses 120 + 121 + - Multiple Schemes are available 122 + - Sv32 -> Used for 2-level Page Tables 123 + - Sv39 -> For 3-levels 124 + - Sv48 -> For 4-levels! 125 + - xv6 uses Sv39 126 + - VA Size- 127 + 128 + ``` 129 + 39 bits 130 + 2^39 = 512 GB 131 + = 0x80-0000-0000 132 + ``` 133 + - But xv6 uses only 38 bits for its VAs 134 + 135 + ``` 136 + 38 bits 137 + 2^38 = 256 GB 138 + = 0x40-0000-0000 139 + ``` 140 + - Therefore the allowed range is - 0&#x2026;0x3F-FFFF-FFFF 141 +
+108
src/content/posts/xv6/index.org
··· 1 + #+title: xv6 2 + #+author: Akshit Gaur 3 + #+description: Documenting the xv6 kernel 4 + #+TAGS: xv6, os 5 + #+OPTIONS: toc:nil 6 + 7 + * Why?? 8 + I am currently going through the book */Operating Systems: Three Easy Pieces/*, and the next step after completing the first piece, Virtualisation, is to go through the xv6 kernel. The xv6 kernel had two implementations, the x86 one and RISC-V one. As the x86 one is now unmaintained and on recommendation of a mentor, I am using [[https://github.com/mit-pdos/xv6-riscv][xv6-riscv]]! So without further ado, let's get started. 9 + 10 + * The xv6 Kernel 11 + - Based on Unix kernel 12 + - Created by MIT for educational purposes 13 + - Implemented for RISC-V 14 + - Multicore 15 + - ~6000 lines of code (C and Assembly) 16 + 17 + ** Features 18 + - Processes 19 + - Virtual Address Spaces 20 + + Page Tables 21 + - Files and Directories 22 + - Pipes 23 + - Multitasking 24 + + Time-Slicing 25 + - 21 system calls 26 + 27 + ** User Programs 28 + - sh 29 + - cat 30 + - echo 31 + - grep 32 + - kill 33 + - ln 34 + - ls 35 + - mkdir 36 + - rm 37 + - wc 38 + 39 + ** What is missing? 40 + All the complexity of a "real" operating system is missing. For example, UserID, file protections, IPC, "mount"able filesystem, etc. 41 + 42 + ** SMP: Shared Memory Multiprocesser 43 + CPU = core = HART 44 + 45 + Main memory (RAM) is shared and is of size 128 Mbytes 46 + 47 + ** Supported Devices 48 + - UART (Serial Comm. Tx <=> Rx) 49 + - Disk 50 + - Timer Interrupts 51 + - PLIC: Platform Level Interrupt Controller 52 + - CLINT: Core Local Interrupt Controller 53 + 54 + ** Memory Management 55 + - Page Size = 4096 bytes (=#define PGSIZE=) 56 + - Single Free List 57 + - No variable sized allocation 58 + - No "malloc" 59 + - Page Tables- 60 + + Three Levels 61 + + One table per process + One table for Kernel 62 + + Pages marked - R/W/X/U/V 63 + 64 + ** Scheduler 65 + - Round Robin 66 + - Size of TimeSlice is fixed (1,000,000 cycles) 67 + - All cores share one "Ready Queue" 68 + - Next TimeSlice may be on a different core 69 + 70 + ** Boot Sequence 71 + - QEMU 72 + + Loads kernel code at a fixed address (0x8000-0000) 73 + + Starts all cores running 74 + - No bootloader/boot-block/BIOS 75 + 76 + ** Locking 77 + - Spin Locks 78 + - =sleep()= & =wakeup()= 79 + 80 + ** "Param.h" 81 + - Fixed Limits (like no. of processes, no. of open files, etc) 82 + - Several Arrays ("=kill(pid)=" => Linear search of Processes array) 83 + 84 + ** User Address Space 85 + #+CAPTION: Sorry for the bad handwriting! 86 + [[./ua-space.jpeg]] 87 + 88 + Arguments (argc, argv) will be placed on the stack before the program begins execution. 89 + 90 + ** RISC-V Virtual Addresses 91 + - Multiple Schemes are available 92 + + Sv32 -> Used for 2-level Page Tables 93 + + Sv39 -> For 3-levels 94 + + Sv48 -> For 4-levels! 95 + - xv6 uses Sv39 96 + - VA Size- 97 + #+begin_example 98 + 39 bits 99 + 2^39 = 512 GB 100 + = 0x80-0000-0000 101 + #+end_example 102 + - But xv6 uses only 38 bits for its VAs 103 + #+begin_example 104 + 38 bits 105 + 2^38 = 256 GB 106 + = 0x40-0000-0000 107 + #+end_example 108 + - Therefore the allowed range is - 0...0x3F-FFFF-FFFF
src/content/posts/xv6/ua-space.jpeg

This is a binary file and will not be displayed.