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
mod address;
mod block_cache_block;
mod block_cache_page;
mod elf_cache;
mod frame_allocator;
mod heap_allocator;
mod memory_set;
mod page_table;

use address::VPNRange;
pub use address::{PhysAddr, PhysPageNum, StepByOne, VirtAddr, VirtPageNum};
pub use elf_cache::push_elf_area;
pub use frame_allocator::{frame_alloc, frame_dealloc, unallocated_frames, FrameTracker};
pub use memory_set::remap_test;
pub use memory_set::{
    kernel_token, mmap, munmap, sbrk, MapFlags, MapPermission, MemorySet, KERNEL_SPACE,
};
use page_table::PTEFlags;
pub use page_table::{
    copy_from_user, copy_from_user_array, copy_to_user, copy_to_user_array, copy_to_user_string, translated_byte_buffer_append_to_existed_vec, translated_byte_buffer, translated_ref,
    translated_refmut, translated_str, PageTable, PageTableEntry, UserBuffer, UserBufferIterator,
};

pub fn init() {
    heap_allocator::init_heap();
    frame_allocator::init_frame_allocator();
    KERNEL_SPACE.lock().activate();
}

#[macro_export]
/// Convert user pointer trg to `Some(*trg)` or `None` if null.
macro_rules! move_ptr_to_opt {
    ($trg:ident) => {
        if $trg != null() {
            let t = *translated_ref(current_user_token(), $trg);
            Some(t)
        } else {
            None
        }
    };
    ($token:ident,$trg:ident) => {
        if $trg != null() {
            let t = *translated_ref($token, $trg);
            Some(t)
        } else {
            None
        }
    };
}

#[macro_export]
/// Convert user pointer `trg:*const T` to `Some(trg as & T)` or `None` if null.
macro_rules! ptr_to_opt_ref {
    ($trg:ident) => {
        if $trg != null() {
            Some(translated_ref(current_user_token(), $trg))
        } else {
            None
        }
    };
    ($token:ident,$trg:ident) => {
        if $trg != null() {
            Some(translated_ref($token, $trg))
        } else {
            None
        }
    };
}

#[macro_export]
/// Convert user pointer `trg:*mut T` to `Some(trg as &mut T)` or `None` if null.
macro_rules! ptr_to_opt_ref_mut {
    ($trg:ident) => {
        if $trg != null_mut() {
            Some(translated_refmut(current_user_token(), $trg))
        } else {
            None
        }
    };
    ($token:ident,$trg:ident) => {
        if $trg != null_mut() {
            Some(translated_refmut($token, $trg))
        } else {
            None
        }
    };
}