Writing an OS Kernel from Scratch | #345: USB Protocol — From HID to High-Speed Devices

dean
Writing an OS Kernel from Scratch | #345: USB Protocol — From HID to High-Speed Devices Abstract: USB is essentially the “universal serial bus” for all peripherals, but many only know “plug and play.” This article takes you from physical layer topology, transfer types, xHCI controller, to practical lsusb usage, thoroughly understanding USB’s design philosophy... » read more

Writing an OS Kernel from Scratch – Part 6: First Look at Multitasking — From Single-Core Loops to Process Switching

dean
Writing an OS Kernel from Scratch – Part 6: First Look at Multitasking — From Single-Core Loops to Process Switching “A kernel that can only do one thing? That’s a bare-metal program. A real OS must be able to ‘simultaneously’ do multiple things!” In the previous five posts, we completed kernel boot, entering protected mode,... » read more

Writing an OS Kernel from Scratch – Part 15: Slab Allocator — Efficiently Managing Kernel Small Objects

dean
Writing an OS Kernel from Scratch – Part 15: Slab Allocator — Efficiently Managing Kernel Small Objects “The Buddy system is great at allocating large memory blocks, but what about the kernel’s frequent 32-byte, 128-byte small object allocations? Today, we implement the Slab allocator, making kernel memory allocation lightning fast!” In the previous post, we... » read more

A05 — Use-After-Free: The Problem of Accessing Freed Memory

dean
A05 — Use-After-Free: The Problem of Accessing Freed Memory Key insight: Continuing to use a freed memory block is like continuing to build a house on land you’ve already sold — sooner or later someone will come to tear it down. What is Use-After-Free Use-After-Free (UAF) refers to accessing memory that has already been freed... » read more

Long Article 104: Memory Management – Virtual Part I: In-Depth Page Table Operations and Virtual Memory Areas (VMA)

dean
Long Article 104: Memory Management – Virtual Part I: In-Depth Page Table Operations and Virtual Memory Areas (VMA) Analysis “Virtual memory is the cornerstone of modern operating systems, and VMA is the ‘map’ of the process address space. This article dives deep into x86 page table structures and VMA design principles, comparing Linux implementation details... » read more

A01 — The Internals of malloc: How brk and mmap Work Together

dean
A01 — The Internals of malloc: How brk and mmap Work Together Key Insight: Every block of memory you allocate with malloc is backed by the system making allocation decisions on your behalf. malloc is Not a System Call malloc is a glibc (ptmalloc) library function, not a system call. Two paths for malloc: 1.... » read more

Long Article 106: Memory Management — Advanced: Deep Dive into Copy-on-Write, Swapping, and NUMA Optimization

dean
# Long Article 106: Memory Management — Advanced: Deep Dive into Copy-on-Write, Swapping, and NUMA Optimization “When physical memory is insufficient, how does the OS handle it gracefully? This article covers CoW, Swapping, and NUMA optimization.” Introduction Three advanced features for memory pressure: CoW: Share pages on fork, copy only on write Swapping: Move inactive... » read more

Writing an OS Kernel from Scratch | VFS — The Virtual File System: How Linux’s “Everything is a File” Works

dean
Writing an OS Kernel from Scratch | VFS — The Virtual File System: How Linux’s “Everything is a File” Works You use cat /proc/cpuinfo, ls /dev/null, echo "hello" > /tmp/a.txt — three different operations, all using the same open/read/write system calls. Why? The answer is VFS (Virtual File System Switch) — an abstraction layer that... » read more

K10 — NUMA Systems: Memory Nodes and Local Allocation

dean
K10 — NUMA Systems: Memory Nodes and Local Allocation Key Insight: On multi-socket servers, “local memory” and “remote memory” access latency can differ by up to 3x — NUMA is the source of this difference. The Concept of NUMA NUMA (Non-Uniform Memory Access) means each CPU core has local memory. Local access is fast, remote... » read more

K09 — Memory Leaks: Why malloc Without free Causes Unrecoverable Memory

dean
K09 — Memory Leaks: Why malloc Without free Causes Unrecoverable Memory Key Insight: After a program ends, the system reclaims its memory — but while running, leaked memory is truly leaked. What Is a Memory Leak A memory leak occurs when a program dynamically allocates memory but fails to release it, making that memory permanently... » read more

A10 — Memory Analysis Tools: malloc_stats, Valgrind Massif, pmap

dean
A10 — Memory Analysis Tools: malloc_stats, Valgrind Massif, pmap Key Insight: The first step to optimizing memory is knowing where it is being used. malloc_stats: glibc Allocation Statistics malloc_stats() 输出所有 arena 的统计信息: #include <malloc.h> malloc_stats(); 输出示例: Arena 0: system bytes = 1048576 memory bytes = 1048576 96 malloc-chunks in use (12345 bytes) Arena 1: system... » read more