···11-#+title: Hello
22-#+author: Akshit Gaur
33-#+description: First Blog!
44-#+TAGS: emacs, org, astro
55-#+OPTIONS: toc:nil
66-* Hello
77-** Everyone
88-#+begin_src python
99- print("HELLO!")
1010- print("World!")
1111-#+end_src
1212-1313-:::caution
1414-Use caution while using python
1515-:::
+141
src/content/posts/xv6/index.md
···11+---
22+title: "xv6"
33+published: 2025-12-16
44+draft: false
55+description: 'Documenting the xv6 kernel'
66+tags: ["xv6","os"]
77+---
88+99+1010+1111+# Why??
1212+1313+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.
1414+1515+1616+# The xv6 Kernel
1717+1818+- Based on Unix kernel
1919+- Created by MIT for educational purposes
2020+- Implemented for RISC-V
2121+- Multicore
2222+- ~6000 lines of code (C and Assembly)
2323+2424+2525+## Features
2626+2727+- Processes
2828+- Virtual Address Spaces
2929+ - Page Tables
3030+- Files and Directories
3131+- Pipes
3232+- Multitasking
3333+ - Time-Slicing
3434+- 21 system calls
3535+3636+3737+## User Programs
3838+3939+- sh
4040+- cat
4141+- echo
4242+- grep
4343+- kill
4444+- ln
4545+- ls
4646+- mkdir
4747+- rm
4848+- wc
4949+5050+5151+## What is missing?
5252+5353+All the complexity of a "real" operating system is missing. For example, UserID, file protections, IPC, "mount"able filesystem, etc.
5454+5555+5656+## SMP: Shared Memory Multiprocesser
5757+5858+CPU = core = HART
5959+6060+Main memory (RAM) is shared and is of size 128 Mbytes
6161+6262+6363+## Supported Devices
6464+6565+- UART (Serial Comm. Tx <=> Rx)
6666+- Disk
6767+- Timer Interrupts
6868+- PLIC: Platform Level Interrupt Controller
6969+- CLINT: Core Local Interrupt Controller
7070+7171+7272+## Memory Management
7373+7474+- Page Size = 4096 bytes (`#define PGSIZE`)
7575+- Single Free List
7676+- No variable sized allocation
7777+- No "malloc"
7878+- Page Tables-
7979+ - Three Levels
8080+ - One table per process + One table for Kernel
8181+ - Pages marked - R/W/X/U/V
8282+8383+8484+## Scheduler
8585+8686+- Round Robin
8787+- Size of TimeSlice is fixed (1,000,000 cycles)
8888+- All cores share one "Ready Queue"
8989+- Next TimeSlice may be on a different core
9090+9191+9292+## Boot Sequence
9393+9494+- QEMU
9595+ - Loads kernel code at a fixed address (0x8000-0000)
9696+ - Starts all cores running
9797+- No bootloader/boot-block/BIOS
9898+9999+100100+## Locking
101101+102102+- Spin Locks
103103+- `sleep()` & `wakeup()`
104104+105105+106106+## "Param.h"
107107+108108+- Fixed Limits (like no. of processes, no. of open files, etc)
109109+- Several Arrays ("`kill(pid)`" => Linear search of Processes array)
110110+111111+112112+## User Address Space
113113+114114+
115115+116116+Arguments (argc, argv) will be placed on the stack before the program begins execution.
117117+118118+119119+## RISC-V Virtual Addresses
120120+121121+- Multiple Schemes are available
122122+ - Sv32 -> Used for 2-level Page Tables
123123+ - Sv39 -> For 3-levels
124124+ - Sv48 -> For 4-levels!
125125+- xv6 uses Sv39
126126+- VA Size-
127127+128128+ ```
129129+ 39 bits
130130+ 2^39 = 512 GB
131131+ = 0x80-0000-0000
132132+ ```
133133+- But xv6 uses only 38 bits for its VAs
134134+135135+ ```
136136+ 38 bits
137137+ 2^38 = 256 GB
138138+ = 0x40-0000-0000
139139+ ```
140140+- Therefore the allowed range is - 0…0x3F-FFFF-FFFF
141141+
+108
src/content/posts/xv6/index.org
···11+#+title: xv6
22+#+author: Akshit Gaur
33+#+description: Documenting the xv6 kernel
44+#+TAGS: xv6, os
55+#+OPTIONS: toc:nil
66+77+* Why??
88+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.
99+1010+* The xv6 Kernel
1111+- Based on Unix kernel
1212+- Created by MIT for educational purposes
1313+- Implemented for RISC-V
1414+- Multicore
1515+- ~6000 lines of code (C and Assembly)
1616+1717+** Features
1818+- Processes
1919+- Virtual Address Spaces
2020+ + Page Tables
2121+- Files and Directories
2222+- Pipes
2323+- Multitasking
2424+ + Time-Slicing
2525+- 21 system calls
2626+2727+** User Programs
2828+- sh
2929+- cat
3030+- echo
3131+- grep
3232+- kill
3333+- ln
3434+- ls
3535+- mkdir
3636+- rm
3737+- wc
3838+3939+** What is missing?
4040+All the complexity of a "real" operating system is missing. For example, UserID, file protections, IPC, "mount"able filesystem, etc.
4141+4242+** SMP: Shared Memory Multiprocesser
4343+CPU = core = HART
4444+4545+Main memory (RAM) is shared and is of size 128 Mbytes
4646+4747+** Supported Devices
4848+- UART (Serial Comm. Tx <=> Rx)
4949+- Disk
5050+- Timer Interrupts
5151+- PLIC: Platform Level Interrupt Controller
5252+- CLINT: Core Local Interrupt Controller
5353+5454+** Memory Management
5555+- Page Size = 4096 bytes (=#define PGSIZE=)
5656+- Single Free List
5757+- No variable sized allocation
5858+- No "malloc"
5959+- Page Tables-
6060+ + Three Levels
6161+ + One table per process + One table for Kernel
6262+ + Pages marked - R/W/X/U/V
6363+6464+** Scheduler
6565+- Round Robin
6666+- Size of TimeSlice is fixed (1,000,000 cycles)
6767+- All cores share one "Ready Queue"
6868+- Next TimeSlice may be on a different core
6969+7070+** Boot Sequence
7171+- QEMU
7272+ + Loads kernel code at a fixed address (0x8000-0000)
7373+ + Starts all cores running
7474+- No bootloader/boot-block/BIOS
7575+7676+** Locking
7777+- Spin Locks
7878+- =sleep()= & =wakeup()=
7979+8080+** "Param.h"
8181+- Fixed Limits (like no. of processes, no. of open files, etc)
8282+- Several Arrays ("=kill(pid)=" => Linear search of Processes array)
8383+8484+** User Address Space
8585+#+CAPTION: Sorry for the bad handwriting!
8686+[[./ua-space.jpeg]]
8787+8888+Arguments (argc, argv) will be placed on the stack before the program begins execution.
8989+9090+** RISC-V Virtual Addresses
9191+- Multiple Schemes are available
9292+ + Sv32 -> Used for 2-level Page Tables
9393+ + Sv39 -> For 3-levels
9494+ + Sv48 -> For 4-levels!
9595+- xv6 uses Sv39
9696+- VA Size-
9797+ #+begin_example
9898+ 39 bits
9999+ 2^39 = 512 GB
100100+ = 0x80-0000-0000
101101+ #+end_example
102102+- But xv6 uses only 38 bits for its VAs
103103+ #+begin_example
104104+ 38 bits
105105+ 2^38 = 256 GB
106106+ = 0x40-0000-0000
107107+ #+end_example
108108+- Therefore the allowed range is - 0...0x3F-FFFF-FFFF