mirror of
https://github.com/ebrandi/FDD-book.git
synced 2026-12-06 00:23:30 +00:00
Fixed markdown issue that was generating error when being converted to pdf file. Thank you Isabelle Gonçalves for catching that bug!
This commit is contained in:
parent
87cfb19c2a
commit
8388fe50dd
1 changed files with 32 additions and 8 deletions
|
|
@ -107,6 +107,7 @@ if (file_size > 1000000) {
|
||||||
This works fine until you encounter a file larger than 2GB on a 32-bit system, where `int` is typically 32 bits and can only hold values up to about 2.1 billion. Suddenly, a 3GB file appears to have a negative size due to integer overflow.
|
This works fine until you encounter a file larger than 2GB on a 32-bit system, where `int` is typically 32 bits and can only hold values up to about 2.1 billion. Suddenly, a 3GB file appears to have a negative size due to integer overflow.
|
||||||
|
|
||||||
In the kernel, this kind of problem is amplified because:
|
In the kernel, this kind of problem is amplified because:
|
||||||
|
|
||||||
- Your code must work across different architectures (32-bit, 64-bit)
|
- Your code must work across different architectures (32-bit, 64-bit)
|
||||||
- Data corruption can affect the entire system
|
- Data corruption can affect the entire system
|
||||||
- Performance-critical code paths can't afford to check for overflow at runtime
|
- Performance-critical code paths can't afford to check for overflow at runtime
|
||||||
|
|
@ -397,6 +398,7 @@ uintptr_t addr = (uintptr_t)pointer;
|
||||||
### Summary
|
### Summary
|
||||||
|
|
||||||
Kernel-specific data types aren't just about being precise, they're about writing code that:
|
Kernel-specific data types aren't just about being precise, they're about writing code that:
|
||||||
|
|
||||||
- Works correctly across different architectures
|
- Works correctly across different architectures
|
||||||
- Clearly expresses its intentions
|
- Clearly expresses its intentions
|
||||||
- Avoids subtle bugs that can crash the system
|
- Avoids subtle bugs that can crash the system
|
||||||
|
|
@ -860,6 +862,7 @@ Compiling and loading the kernel will print your message, proving a safe bounded
|
||||||
### Summary
|
### Summary
|
||||||
|
|
||||||
Kernel memory management requires discipline and understanding:
|
Kernel memory management requires discipline and understanding:
|
||||||
|
|
||||||
- Use appropriate allocation flags (`M_WAITOK` vs `M_NOWAIT`)
|
- Use appropriate allocation flags (`M_WAITOK` vs `M_NOWAIT`)
|
||||||
- Always specify a memory type for tracking
|
- Always specify a memory type for tracking
|
||||||
- Check return values, even with `M_WAITOK`
|
- Check return values, even with `M_WAITOK`
|
||||||
|
|
@ -1036,11 +1039,13 @@ strcpy(buffer, user_input); /* Dangerous! */
|
||||||
```
|
```
|
||||||
|
|
||||||
This code is problematic because:
|
This code is problematic because:
|
||||||
|
|
||||||
- `strcpy()` doesn't check buffer boundaries
|
- `strcpy()` doesn't check buffer boundaries
|
||||||
- If `user_input` is longer than 255 characters, memory corruption occurs
|
- If `user_input` is longer than 255 characters, memory corruption occurs
|
||||||
- In the kernel, this could overwrite critical data structures
|
- In the kernel, this could overwrite critical data structures
|
||||||
|
|
||||||
The kernel needs functions that:
|
The kernel needs functions that:
|
||||||
|
|
||||||
- Always respect buffer boundaries
|
- Always respect buffer boundaries
|
||||||
- Handle partially-filled buffers gracefully
|
- Handle partially-filled buffers gracefully
|
||||||
- Work efficiently with both kernel and user data
|
- Work efficiently with both kernel and user data
|
||||||
|
|
@ -1424,6 +1429,7 @@ size_t total_len = len1 + len2;
|
||||||
### Summary
|
### Summary
|
||||||
|
|
||||||
Kernel string handling requires constant vigilance:
|
Kernel string handling requires constant vigilance:
|
||||||
|
|
||||||
- Use safe functions that respect buffer boundaries
|
- Use safe functions that respect buffer boundaries
|
||||||
- Always validate lengths and check for truncation
|
- Always validate lengths and check for truncation
|
||||||
- Handle user data with `copyin()`/`copyout()`
|
- Handle user data with `copyin()`/`copyout()`
|
||||||
|
|
@ -1912,6 +1918,7 @@ MODULE_VERSION(function_demo, 1);
|
||||||
### Summary
|
### Summary
|
||||||
|
|
||||||
Kernel function design is about predictability and safety:
|
Kernel function design is about predictability and safety:
|
||||||
|
|
||||||
- Follow consistent naming and parameter ordering conventions
|
- Follow consistent naming and parameter ordering conventions
|
||||||
- Use the standard error return pattern (0 for success)
|
- Use the standard error return pattern (0 for success)
|
||||||
- Validate parameters and handle all error conditions
|
- Validate parameters and handle all error conditions
|
||||||
|
|
@ -2043,12 +2050,14 @@ namei(struct nameidata *ndp)
|
||||||
Understanding when your code can and cannot **sleep** (voluntarily give up the CPU) is critical for kernel programming.
|
Understanding when your code can and cannot **sleep** (voluntarily give up the CPU) is critical for kernel programming.
|
||||||
|
|
||||||
**Atomic context** (cannot sleep):
|
**Atomic context** (cannot sleep):
|
||||||
|
|
||||||
- Interrupt handlers
|
- Interrupt handlers
|
||||||
- Code holding spinlocks
|
- Code holding spinlocks
|
||||||
- Code in critical sections
|
- Code in critical sections
|
||||||
- Some callback functions
|
- Some callback functions
|
||||||
|
|
||||||
**Preemptible context** (can sleep):
|
**Preemptible context** (can sleep):
|
||||||
|
|
||||||
- System call handlers
|
- System call handlers
|
||||||
- Kernel threads
|
- Kernel threads
|
||||||
- Most driver probe/attach functions
|
- Most driver probe/attach functions
|
||||||
|
|
@ -2368,6 +2377,7 @@ MODULE_VERSION(restrictions_demo, 1);
|
||||||
### Summary
|
### Summary
|
||||||
|
|
||||||
Kernel programming restrictions exist for good reasons:
|
Kernel programming restrictions exist for good reasons:
|
||||||
|
|
||||||
- No floating-point prevents corruption of user process state
|
- No floating-point prevents corruption of user process state
|
||||||
- Limited stack size forces efficient algorithms and prevents overflow
|
- Limited stack size forces efficient algorithms and prevents overflow
|
||||||
- Sleep restrictions ensure system responsiveness and prevent deadlocks
|
- Sleep restrictions ensure system responsiveness and prevent deadlocks
|
||||||
|
|
@ -2394,6 +2404,7 @@ void increment_counter(void)
|
||||||
```
|
```
|
||||||
|
|
||||||
On a multiprocessor system, `global_counter++` actually involves multiple steps:
|
On a multiprocessor system, `global_counter++` actually involves multiple steps:
|
||||||
|
|
||||||
1. Load the current value from memory
|
1. Load the current value from memory
|
||||||
2. Increment the value in a register
|
2. Increment the value in a register
|
||||||
3. Store the new value back to memory
|
3. Store the new value back to memory
|
||||||
|
|
@ -2601,12 +2612,14 @@ vm_page_wire(vm_page_t m)
|
||||||
### When to Use Inline Functions
|
### When to Use Inline Functions
|
||||||
|
|
||||||
**Use inline for**:
|
**Use inline for**:
|
||||||
|
|
||||||
- Small, frequently called functions (< 10 lines typically)
|
- Small, frequently called functions (< 10 lines typically)
|
||||||
- Functions in critical performance paths
|
- Functions in critical performance paths
|
||||||
- Simple accessor functions
|
- Simple accessor functions
|
||||||
- Functions that wrap complex macros to add type safety
|
- Functions that wrap complex macros to add type safety
|
||||||
|
|
||||||
**Don't inline**:
|
**Don't inline**:
|
||||||
|
|
||||||
- Large functions (increases code size)
|
- Large functions (increases code size)
|
||||||
- Functions with complex control flow
|
- Functions with complex control flow
|
||||||
- Functions that are rarely called
|
- Functions that are rarely called
|
||||||
|
|
@ -2783,6 +2796,7 @@ MODULE_VERSION(atomic_demo, 1);
|
||||||
### Summary
|
### Summary
|
||||||
|
|
||||||
Atomic operations and inline functions are essential tools for high-performance, correct kernel programming:
|
Atomic operations and inline functions are essential tools for high-performance, correct kernel programming:
|
||||||
|
|
||||||
- Atomic operations ensure data consistency in multiprocessor systems
|
- Atomic operations ensure data consistency in multiprocessor systems
|
||||||
- Memory barriers control operation ordering when needed
|
- Memory barriers control operation ordering when needed
|
||||||
- Compare-and-swap enables sophisticated lock-free algorithms
|
- Compare-and-swap enables sophisticated lock-free algorithms
|
||||||
|
|
@ -3403,6 +3417,7 @@ MODULE_VERSION(style_demo, 1);
|
||||||
### Summary
|
### Summary
|
||||||
|
|
||||||
FreeBSD's coding idioms aren't arbitrary rules; they're distilled wisdom from decades of kernel development. Following these patterns makes your code:
|
FreeBSD's coding idioms aren't arbitrary rules; they're distilled wisdom from decades of kernel development. Following these patterns makes your code:
|
||||||
|
|
||||||
- Easier for other developers to read and understand
|
- Easier for other developers to read and understand
|
||||||
- Less likely to contain subtle bugs
|
- Less likely to contain subtle bugs
|
||||||
- More consistent with the existing kernel codebase
|
- More consistent with the existing kernel codebase
|
||||||
|
|
@ -3499,6 +3514,7 @@ cleanup:
|
||||||
### Input Validation: Trust No One
|
### Input Validation: Trust No One
|
||||||
|
|
||||||
Never trust data that comes from outside your immediate control. This includes:
|
Never trust data that comes from outside your immediate control. This includes:
|
||||||
|
|
||||||
- User-space programs (via system calls)
|
- User-space programs (via system calls)
|
||||||
- Hardware devices (via device registers)
|
- Hardware devices (via device registers)
|
||||||
- Network packets
|
- Network packets
|
||||||
|
|
@ -5235,6 +5251,7 @@ These aren't just academic examples; this is production code that handles millio
|
||||||
### Summary
|
### Summary
|
||||||
|
|
||||||
Real FreeBSD kernel code demonstrates how all the concepts we've covered work together:
|
Real FreeBSD kernel code demonstrates how all the concepts we've covered work together:
|
||||||
|
|
||||||
- Kernel-specific data types provide portability and clarity
|
- Kernel-specific data types provide portability and clarity
|
||||||
- Defensive programming prevents subtle bugs
|
- Defensive programming prevents subtle bugs
|
||||||
- Consistent error handling makes systems reliable
|
- Consistent error handling makes systems reliable
|
||||||
|
|
@ -5471,6 +5488,7 @@ Memory Lab: Module unloaded safely
|
||||||
```
|
```
|
||||||
|
|
||||||
**Key Learning Points**:
|
**Key Learning Points**:
|
||||||
|
|
||||||
- Kernel C requires explicit memory type definitions (`MALLOC_DEFINE`)
|
- Kernel C requires explicit memory type definitions (`MALLOC_DEFINE`)
|
||||||
- Every `malloc()` must be paired with exactly one `free()`
|
- Every `malloc()` must be paired with exactly one `free()`
|
||||||
- Module unload handlers must clean up ALL allocated resources
|
- Module unload handlers must clean up ALL allocated resources
|
||||||
|
|
@ -5737,6 +5755,7 @@ Echo: 'Hello from user space!' (received 23 bytes at ticks 45678)
|
||||||
```
|
```
|
||||||
|
|
||||||
**Key Learning Points**:
|
**Key Learning Points**:
|
||||||
|
|
||||||
- Kernel C cannot directly access user space pointers
|
- Kernel C cannot directly access user space pointers
|
||||||
- `uiomove()` safely transfers data across the user-kernel boundary
|
- `uiomove()` safely transfers data across the user-kernel boundary
|
||||||
- Always validate buffer sizes and handle partial transfers
|
- Always validate buffer sizes and handle partial transfers
|
||||||
|
|
@ -6038,6 +6057,7 @@ Log Lab: ========================================
|
||||||
```
|
```
|
||||||
|
|
||||||
**Key Learning Points**:
|
**Key Learning Points**:
|
||||||
|
|
||||||
- Different printf() variants serve different purposes in kernel code
|
- Different printf() variants serve different purposes in kernel code
|
||||||
- Device context provides better diagnostics than generic messages
|
- Device context provides better diagnostics than generic messages
|
||||||
- Timer callbacks require careful consideration of logging frequency
|
- Timer callbacks require careful consideration of logging frequency
|
||||||
|
|
@ -6519,6 +6539,7 @@ Error Lab: Final statistics:
|
||||||
```
|
```
|
||||||
|
|
||||||
**Key Learning Points**:
|
**Key Learning Points**:
|
||||||
|
|
||||||
- Always use standard errno.h error codes for consistent behavior
|
- Always use standard errno.h error codes for consistent behavior
|
||||||
- Every resource allocation needs a corresponding cleanup path
|
- Every resource allocation needs a corresponding cleanup path
|
||||||
- Error injection helps test failure paths that are hard to trigger naturally
|
- Error injection helps test failure paths that are hard to trigger naturally
|
||||||
|
|
@ -6557,6 +6578,7 @@ This is the **mindset shift** that makes someone a kernel programmer. You now th
|
||||||
**Next Steps**:
|
**Next Steps**:
|
||||||
|
|
||||||
The patterns you've learned in these labs appear everywhere in the FreeBSD kernel:
|
The patterns you've learned in these labs appear everywhere in the FreeBSD kernel:
|
||||||
|
|
||||||
- Device drivers use these same memory management patterns
|
- Device drivers use these same memory management patterns
|
||||||
- Network protocols use these same error handling strategies
|
- Network protocols use these same error handling strategies
|
||||||
- File systems use these same user-kernel communication techniques
|
- File systems use these same user-kernel communication techniques
|
||||||
|
|
@ -6644,8 +6666,8 @@ Take your time. Each challenge can be completed with the same lab environment yo
|
||||||
|
|
||||||
### Challenge 1: Trace the Data Type Origins
|
### Challenge 1: Trace the Data Type Origins
|
||||||
Open `/usr/src/sys/sys/types.h` and locate at least **five typedefs** that appear in this chapter
|
Open `/usr/src/sys/sys/types.h` and locate at least **five typedefs** that appear in this chapter
|
||||||
(e.g., `vm_offset_t`, `bus_size_t`, `sbintime_t`).
|
(e.g., `vm_offset_t`, `bus_size_t`, `sbintime_t`). For each:
|
||||||
For each:
|
|
||||||
- Identify what underlying C type it maps to on your architecture.
|
- Identify what underlying C type it maps to on your architecture.
|
||||||
- Explain in a comment *why* the kernel uses a typedef instead of a primitive type.
|
- Explain in a comment *why* the kernel uses a typedef instead of a primitive type.
|
||||||
|
|
||||||
|
|
@ -6653,12 +6675,13 @@ Goal: see how portability and readability are built into FreeBSD's type system.
|
||||||
|
|
||||||
### Challenge 2: Memory Allocation Scenarios
|
### Challenge 2: Memory Allocation Scenarios
|
||||||
Create a short kernel module that allocates memory three different ways:
|
Create a short kernel module that allocates memory three different ways:
|
||||||
|
|
||||||
1. `malloc()` with `M_WAITOK`
|
1. `malloc()` with `M_WAITOK`
|
||||||
2. `malloc()` with `M_NOWAIT`
|
2. `malloc()` with `M_NOWAIT`
|
||||||
3. A UMA zone allocation (`uma_zalloc()`)
|
3. A UMA zone allocation (`uma_zalloc()`)
|
||||||
|
|
||||||
Log the pointer addresses and note what happens if you try to load the module when memory pressure is high.
|
Log the pointer addresses and note what happens if you try to load the module when memory pressure is high. Then answer in comments:
|
||||||
Then answer in comments:
|
|
||||||
- Why is `M_WAITOK` unsafe in interrupt context?
|
- Why is `M_WAITOK` unsafe in interrupt context?
|
||||||
- What would be the correct pattern for emergency allocations?
|
- What would be the correct pattern for emergency allocations?
|
||||||
|
|
||||||
|
|
@ -6690,10 +6713,11 @@ Goal: learn when to use **assertions vs. recoverable errors**.
|
||||||
### What You'll Gain
|
### What You'll Gain
|
||||||
|
|
||||||
By completing these challenges, you'll reinforce:
|
By completing these challenges, you'll reinforce:
|
||||||
- Precision with kernel data types
|
|
||||||
- Conscious memory-allocation decisions
|
- Precision with kernel data types
|
||||||
- Structured error handling and cleanup
|
- Conscious memory-allocation decisions
|
||||||
- Respect for stack limits and context safety
|
- Structured error handling and cleanup
|
||||||
|
- Respect for stack limits and context safety
|
||||||
- The discipline that distinguishes **user-space coding** from **kernel engineering**
|
- The discipline that distinguishes **user-space coding** from **kernel engineering**
|
||||||
|
|
||||||
You're now ready to approach Chapter 6, where we start assembling these pieces into the real structure of a FreeBSD driver.
|
You're now ready to approach Chapter 6, where we start assembling these pieces into the real structure of a FreeBSD driver.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue