ahrightos-kernel 0.1.0

RISC-V 64 Rust teaching kernel for AhRightOS.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
 * ============================================
 * RISC-V 地址空间抽象
 * ============================================
 * 功能:管理独立的虚拟地址空间
 *
 * 教学说明:
 * - 每个进程都有独立的地址空间
 * - 地址空间包含页表和内存区域
 * - 可以激活(切换到)某个地址空间
 * ============================================
 */

use super::paging::{map_page, unmap_page};
use super::{PageTable, PageTableFlags, PhysAddr, SimpleFrameAllocator, VirtAddr, PAGE_SIZE};
use alloc::vec::Vec;
use core::ops::Range;

/// 内存区域类型
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MemoryAreaType {
    Code,   // 代码段(R-X)
    Data,   // 数据段(RW-)
    Heap,   // 堆(RW-)
    Stack,  // 栈(RW-)
    Shared, // 共享内存(RW-)
    Kernel, // 内核内存(RWX)
}

impl MemoryAreaType {
    /// 获取该区域类型的默认标志位
    pub fn default_flags(&self) -> usize {
        use PageTableFlags as PTF;

        match self {
            MemoryAreaType::Code => {
                // 代码段:可读、可执行
                PTF::Valid as usize | PTF::Read as usize | PTF::Execute as usize
            }
            MemoryAreaType::Data | MemoryAreaType::Heap | MemoryAreaType::Stack => {
                // 数据段、堆、栈:可读、可写
                PTF::Valid as usize | PTF::Read as usize | PTF::Write as usize
            }
            MemoryAreaType::Shared => {
                // 共享内存:可读、可写
                PTF::Valid as usize | PTF::Read as usize | PTF::Write as usize
            }
            MemoryAreaType::Kernel => {
                // 内核内存:可读、可写、可执行
                PTF::Valid as usize
                    | PTF::Read as usize
                    | PTF::Write as usize
                    | PTF::Execute as usize
            }
        }
    }
}

/// 内存区域
///
/// # 教学说明
/// 内存区域是地址空间的一部分,具有连续的虚拟地址和相同的权限
#[derive(Debug, Clone)]
pub struct MemoryArea {
    pub range: Range<VirtAddr>,
    pub area_type: MemoryAreaType,
    pub flags: usize,
}

impl MemoryArea {
    /// 创建新的内存区域
    pub fn new(start: VirtAddr, end: VirtAddr, area_type: MemoryAreaType) -> Self {
        MemoryArea {
            range: start..end,
            area_type,
            flags: area_type.default_flags(),
        }
    }

    /// 获取区域大小
    pub fn size(&self) -> usize {
        self.range.end.as_usize() - self.range.start.as_usize()
    }

    /// 获取页数
    pub fn page_count(&self) -> usize {
        self.size().div_ceil(PAGE_SIZE)
    }
}

/// 地址空间
///
/// # 教学说明
/// 地址空间代表一个独立的虚拟内存空间,包含:
/// - 一个根页表
/// - 多个内存区域
/// - 可以激活(切换satp寄存器)
pub struct AddressSpace {
    page_table: *mut PageTable,
    page_table_paddr: PhysAddr,
    areas: Vec<MemoryArea>,
}

/// Deep-copy user-space mappings from `src_satp` into a new page table.
///
/// Kernel leaf mappings (no U flag) keep pointing at the same physical frames,
/// but every page-table level is private to the new address space.  User leaf
/// pages (U flag set) are allocated new physical frames and their data copied.
/// Returns the new satp value (Sv39 mode).
pub fn deep_copy_user_address_space(
    src_satp: usize,
    allocator: &mut SimpleFrameAllocator,
) -> Result<usize, &'static str> {
    let src_root_ppn = src_satp & 0x0000_0FFF_FFFF_FFFF;
    let src_root_paddr = src_root_ppn << 12;

    let new_root_frame = allocator.allocate().ok_or("OOM for root page table")?;
    let new_root_paddr = new_root_frame.start_address().as_usize();

    unsafe {
        let new_root = new_root_paddr as *mut PageTable;
        (*new_root).zero();

        clone_page_table_level(allocator, src_root_paddr, new_root_paddr, 2, true)?;
    }

    Ok((8usize << 60) | (new_root_paddr >> 12))
}

/// Recursively copy a Sv39 page-table subtree without sharing intermediate
/// page-table pages.
///
/// Non-user leaves are kernel/MMIO mappings: they are copied as PTE leaves so
/// both address spaces reference the same physical kernel device/memory pages,
/// but the intermediate page tables are newly allocated.  User leaves are
/// either skipped (`copy_user_pages = false`) or deep-copied into fresh frames.
unsafe fn clone_page_table_level(
    allocator: &mut SimpleFrameAllocator,
    src_table_paddr: usize,
    dst_table_paddr: usize,
    level: usize,
    copy_user_pages: bool,
) -> Result<(), &'static str> {
    let src = src_table_paddr as *const PageTable;
    let dst = dst_table_paddr as *mut PageTable;

    for idx in 0..512 {
        let entry = (*src).entries[idx];
        if !entry.is_valid() {
            continue;
        }

        let flags = entry.flags();
        let has_user = (flags & (PageTableFlags::User as usize)) != 0;

        if entry.is_leaf() {
            if !has_user {
                // Kernel leaf or MMIO mapping: share the physical mapping but
                // keep all page-table pages private.
                (*dst).entries[idx] = entry;
            } else if copy_user_pages {
                if level != 0 {
                    return Err("Unsupported user huge page");
                }
                let page_paddr = entry.ppn() << 12;
                let new_frame = allocator.allocate().ok_or("OOM for user page copy")?;
                let new_paddr = new_frame.start_address().as_usize();
                copy_4k_page(page_paddr, new_paddr);
                (*dst).entries[idx].set(new_paddr >> 12, flags);
            }
            // Otherwise this is a stale/user leaf while cloning kernel
            // mappings for a fresh exec address space: intentionally skip it.
        } else {
            if level == 0 {
                return Err("Malformed page table: non-leaf at level 0");
            }
            // Non-leaf: allocate new intermediate page table and recurse
            let src_child_paddr = entry.ppn() << 12;
            let new_child_frame = allocator.allocate().ok_or("OOM for intermediate PT")?;
            let new_child_paddr = new_child_frame.start_address().as_usize();
            let child = new_child_paddr as *mut PageTable;
            (*child).zero();
            (*dst).entries[idx].set(new_child_paddr >> 12, flags);
            clone_page_table_level(
                allocator,
                src_child_paddr,
                new_child_paddr,
                level - 1,
                copy_user_pages,
            )?;
        }
    }
    Ok(())
}

unsafe fn copy_4k_page(src_paddr: usize, dst_paddr: usize) {
    let page_size_words = PAGE_SIZE / core::mem::size_of::<u64>();
    for off in 0..page_size_words {
        let src_word = *((src_paddr + off * core::mem::size_of::<u64>()) as *const u64);
        *((dst_paddr + off * core::mem::size_of::<u64>()) as *mut u64) = src_word;
    }
}

impl AddressSpace {
    /// 创建新的地址空间
    ///
    /// # 教学说明
    /// 1. 分配一个物理帧作为根页表
    /// 2. 清空页表
    /// 3. 初始化空的内存区域列表
    pub fn new(allocator: &mut SimpleFrameAllocator) -> Result<Self, &'static str> {
        // 分配根页表
        let frame = allocator.allocate().ok_or("Out of memory")?;
        let page_table_paddr = frame.start_address();
        let page_table_ptr = page_table_paddr.as_usize() as *mut PageTable;

        // 清空页表
        unsafe {
            (*page_table_ptr).zero();
        }

        crate::serial_println!(
            "[ADDRESS_SPACE] Created new address space, page table at {:#x}",
            page_table_paddr.as_usize()
        );

        Ok(AddressSpace {
            page_table: page_table_ptr,
            page_table_paddr,
            areas: Vec::new(),
        })
    }

    /// Copy kernel/MMIO mappings from an existing kernel root into this address
    /// space without sharing intermediate page-table pages.
    ///
    /// This is intentionally a private page-table clone, not a shallow root-PTE
    /// copy.  AhRightOS maps UART at a low virtual address that shares VPN[2] with
    /// early user programs; sharing that subtree lets later user mappings write
    /// back into the boot kernel root and breaks repeated exec/ELF loads.
    pub fn copy_kernel_mappings_from(
        &mut self,
        kernel_root_paddr: PhysAddr,
        allocator: &mut SimpleFrameAllocator,
    ) -> Result<(), &'static str> {
        unsafe {
            clone_page_table_level(
                allocator,
                kernel_root_paddr.as_usize(),
                self.page_table_paddr.as_usize(),
                2,
                false,
            )
        }
    }

    /// 映射内存区域
    ///
    /// # 参数
    /// - `start`: 起始虚拟地址
    /// - `size`: 区域大小
    /// - `area_type`: 区域类型
    /// - `allocator`: 帧分配器
    ///
    /// # 教学说明
    /// 1. 创建内存区域描述
    /// 2. 分配物理帧
    /// 3. 建立虚拟地址到物理地址的映射
    pub fn map_region(
        &mut self,
        start: VirtAddr,
        size: usize,
        area_type: MemoryAreaType,
        allocator: &mut SimpleFrameAllocator,
    ) -> Result<(), &'static str> {
        let end = VirtAddr::new(start.as_usize() + size);
        let area = MemoryArea::new(start, end, area_type);

        crate::serial_println!(
            "[ADDRESS_SPACE] Mapping region: {:#x} - {:#x} ({:?})",
            start.as_usize(),
            end.as_usize(),
            area_type
        );

        // 分配并映射每个页面
        let page_count = area.page_count();

        for i in 0..page_count {
            let vaddr = VirtAddr::new(start.as_usize() + i * PAGE_SIZE);

            // 分配物理帧
            let frame = allocator.allocate().ok_or("Out of memory")?;
            let paddr = frame.start_address();

            // 建立映射
            unsafe {
                map_page(&mut *self.page_table, vaddr, paddr, area.flags, allocator)?;
            }
        }

        self.areas.push(area);
        Ok(())
    }

    /// 映射内存区域(恒等映射)
    ///
    /// # 教学说明
    /// 恒等映射:虚拟地址 == 物理地址
    /// 主要用于内核代码段,方便直接访问物理内存
    pub fn map_region_identity(
        &mut self,
        start: PhysAddr,
        size: usize,
        area_type: MemoryAreaType,
        allocator: &mut SimpleFrameAllocator,
    ) -> Result<(), &'static str> {
        let vstart = VirtAddr::new(start.as_usize());
        let end = VirtAddr::new(start.as_usize() + size);
        let area = MemoryArea::new(vstart, end, area_type);

        crate::serial_println!(
            "[ADDRESS_SPACE] Identity mapping region: {:#x} - {:#x} ({:?})",
            start.as_usize(),
            start.as_usize() + size,
            area_type
        );

        // 映射每个页面(恒等映射)
        let page_count = area.page_count();

        for i in 0..page_count {
            let addr = start.as_usize() + i * PAGE_SIZE;
            let vaddr = VirtAddr::new(addr);
            let paddr = PhysAddr::new(addr);

            // 建立恒等映射
            unsafe {
                map_page(&mut *self.page_table, vaddr, paddr, area.flags, allocator)?;
            }
        }

        self.areas.push(area);
        Ok(())
    }

    /// 取消映射内存区域
    pub fn unmap_region(&mut self, start: VirtAddr, size: usize) -> Result<(), &'static str> {
        let page_count = size.div_ceil(PAGE_SIZE);

        for i in 0..page_count {
            let vaddr = VirtAddr::new(start.as_usize() + i * PAGE_SIZE);
            unsafe {
                unmap_page(&mut *self.page_table, vaddr)?;
            }
        }

        // 从区域列表中移除
        self.areas
            .retain(|area| area.range.start.as_usize() != start.as_usize());

        Ok(())
    }

    /// 激活此地址空间(写入 satp)
    ///
    /// # 教学说明
    /// 1. 计算页表的物理页号(PPN)
    /// 2. 设置 satp 寄存器(Sv39 模式)
    /// 3. 刷新 TLB
    pub fn activate(&self) {
        use riscv::register::satp;

        let ppn = self.page_table_paddr.as_usize() >> 12;

        crate::serial_println!("[ADDRESS_SPACE] Activating address space, PPN: {:#x}", ppn);

        unsafe {
            // Sv39 模式,ASID = 0
            satp::set(satp::Mode::Sv39, 0, ppn);

            // 刷新整个 TLB
            core::arch::asm!("sfence.vma");
        }

        crate::serial_println!("[ADDRESS_SPACE] Address space activated");
    }

    /// 获取页表的物理地址
    pub fn page_table_paddr(&self) -> PhysAddr {
        self.page_table_paddr
    }

    /// Get mutable pointer to the root page table
    pub fn page_table_ptr(&self) -> *mut PageTable {
        self.page_table
    }

    /// 获取内存区域列表
    pub fn areas(&self) -> &[MemoryArea] {
        &self.areas
    }

    /// 可视化显示地址空间布局(教学特色)
    pub fn print_layout(&self) {
        crate::serial_println!("\n╔════════════════════════════════════════╗");
        crate::serial_println!("║      地址空间布局                      ║");
        crate::serial_println!("╠════════════════════════════════════════╣");
        crate::serial_println!(
            "║ 页表物理地址: {:#018x}      ║",
            self.page_table_paddr.as_usize()
        );
        crate::serial_println!("╠════════════════════════════════════════╣");
        crate::serial_println!("║ 内存区域:                              ║");

        for (i, area) in self.areas.iter().enumerate() {
            crate::serial_println!(
                "║ [{:2}] {:8?} {:#010x} - {:#010x} ║",
                i,
                area.area_type,
                area.range.start.as_usize(),
                area.range.end.as_usize()
            );
        }

        crate::serial_println!("╚════════════════════════════════════════╝\n");
    }
}

// 由于我们存储的是原始指针,需要手动实现 Send
unsafe impl Send for AddressSpace {}
unsafe impl Sync for AddressSpace {}

/// 创建内核地址空间
///
/// # 功能
/// - 映射内核代码和数据段(恒等映射)
/// - 映射物理内存
/// - 映射 MMIO 设备
///
/// # 教学说明
/// 内核地址空间使用恒等映射,这样内核代码可以直接访问物理地址
pub fn create_kernel_address_space(
    allocator: &mut SimpleFrameAllocator,
) -> Result<AddressSpace, &'static str> {
    let mut addr_space = AddressSpace::new(allocator)?;

    crate::serial_println!("\n[KERNEL] Creating kernel address space...");

    // 1. Map a minimal kernel region first for safety, then expand
    const KERNEL_START: usize = 0x8000_0000;
    const KERNEL_MAP_SIZE: usize = 8 * 1024 * 1024; // 8 MB — room for kernel + fork allocations

    crate::serial_println!("[KERNEL] step1");
    addr_space.map_region_identity(
        PhysAddr::new(KERNEL_START),
        KERNEL_MAP_SIZE,
        MemoryAreaType::Kernel,
        allocator,
    )?;
    crate::serial_println!(
        "[KERNEL] Kernel region mapped OK ({} MB)",
        KERNEL_MAP_SIZE / 1024 / 1024
    );

    // 2. 映射 UART(0x10000000)
    const UART_BASE: usize = 0x1000_0000;

    crate::serial_println!("[KERNEL] Mapping UART: {:#x}", UART_BASE);

    addr_space.map_region_identity(
        PhysAddr::new(UART_BASE),
        PAGE_SIZE,
        MemoryAreaType::Data,
        allocator,
    )?;

    crate::serial_println!("[KERNEL] Kernel address space created successfully\n");

    Ok(addr_space)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::memory::paging::{map_page, walk_page_table};

    #[test_case]
    fn deep_copy_user_address_space_copies_user_pages_independently() {
        let mut allocator = SimpleFrameAllocator::new(0x8060_0000, 0x8070_0000);
        let root_frame = allocator.allocate().expect("root frame");
        let root_paddr = root_frame.start_address();
        let root = unsafe { &mut *(root_paddr.as_usize() as *mut PageTable) };
        root.zero();

        let user_vaddr = VirtAddr::new(0x4000);
        let user_frame = allocator.allocate().expect("user page");
        unsafe {
            let page = user_frame.start_address().as_usize() as *mut u8;
            *page = 0x5a;
            *page.add(1) = 0xa5;
        }

        map_page(
            root,
            user_vaddr,
            user_frame.start_address(),
            PageTableFlags::Read as usize
                | PageTableFlags::Write as usize
                | PageTableFlags::User as usize,
            &mut allocator,
        )
        .expect("map user page");

        let kernel_vaddr = VirtAddr::new(0x8000_0000);
        let kernel_frame = allocator.allocate().expect("kernel page");
        map_page(
            root,
            kernel_vaddr,
            kernel_frame.start_address(),
            PageTableFlags::Read as usize | PageTableFlags::Write as usize,
            &mut allocator,
        )
        .expect("map kernel page");

        let src_satp = (8usize << 60) | (root_paddr.as_usize() >> 12);
        let child_satp =
            deep_copy_user_address_space(src_satp, &mut allocator).expect("copy address space");
        let child_root_paddr = PhysAddr::new((child_satp & 0x0000_0FFF_FFFF_FFFF) << 12);

        let parent_user_paddr =
            walk_page_table(root_paddr, user_vaddr).expect("parent user mapping");
        let child_user_paddr =
            walk_page_table(child_root_paddr, user_vaddr).expect("child user mapping");
        assert_ne!(
            parent_user_paddr.as_usize() & !(PAGE_SIZE - 1),
            child_user_paddr.as_usize() & !(PAGE_SIZE - 1)
        );

        unsafe {
            let parent_page = parent_user_paddr.as_usize() as *mut u8;
            let child_page = child_user_paddr.as_usize() as *mut u8;
            assert_eq!(*child_page, 0x5a);
            assert_eq!(*child_page.add(1), 0xa5);
            *child_page = 0x11;
            assert_eq!(*parent_page, 0x5a);
        }

        let child_kernel_paddr =
            walk_page_table(child_root_paddr, kernel_vaddr).expect("child kernel mapping");
        assert_eq!(child_kernel_paddr, kernel_frame.start_address());
    }
}