Skip to main content

obkrnl/malloc/
vm.rs

1use crate::config::{PAGE_MASK, PAGE_SHIFT, PAGE_SIZE};
2use crate::context::{CpuLocal, current_thread, uma};
3use crate::uma::{Alloc, SlabFlags, UmaFlags, UmaZone};
4use crate::vm::{PageObj, Vm, kaddr_to_phys};
5use alloc::string::ToString;
6use alloc::sync::Arc;
7use alloc::vec::Vec;
8use core::alloc::Layout;
9use core::cell::RefCell;
10use core::num::NonZero;
11
12/// Kernel heap that allocate a memory from a virtual memory management system. This struct is a
13/// merge of `malloc_type` and `malloc_type_internal` structure.
14pub struct VmHeap {
15    vm: &'static Vm,
16    zones: [Vec<Arc<UmaZone>>; PAGE_SHIFT + 1], // kmemsize + kmemzones
17    stats: CpuLocal<RefCell<Stats>>,            // mti_stats
18}
19
20impl VmHeap {
21    const KMEM_ZSHIFT: usize = 4;
22    const KMEM_ZBASE: usize = 16;
23    const KMEM_ZMASK: usize = Self::KMEM_ZBASE - 1;
24    const KMEM_ZSIZE: usize = PAGE_SIZE.get() >> Self::KMEM_ZSHIFT;
25
26    /// See `kmeminit` on the Orbis for a reference.
27    ///
28    /// # Reference offsets
29    /// | Version | Offset |
30    /// |---------|--------|
31    /// |PS4 11.00|0x1A4B80|
32    pub fn new(vm: &'static Vm) -> Self {
33        // The possible of maximum alignment that Layout allowed is a bit before the most
34        // significant bit of isize (e.g. 0x4000000000000000 on 64 bit system). So we can use
35        // "size_of::<usize>() * 8 - 1" to get the size of array for all possible alignment.
36        let uma = uma().unwrap();
37        let zones = core::array::from_fn(|align| {
38            let mut zones = Vec::with_capacity(Self::KMEM_ZSIZE + 1);
39            let mut last = 0;
40            let align = align
41                .try_into()
42                .ok()
43                .and_then(|align| 1usize.checked_shl(align))
44                .unwrap();
45
46            for i in Self::KMEM_ZSHIFT.. {
47                // Stop if size larger than page size.
48                let size = NonZero::new(1usize << i).unwrap();
49
50                if size > PAGE_SIZE {
51                    break;
52                }
53
54                // Create zone.
55                let zone = Arc::new(uma.into_owned().create_zone(
56                    size.to_string(),
57                    size,
58                    Some(align - 1),
59                    None,
60                    UmaFlags::Malloc,
61                ));
62
63                while last <= size.get() {
64                    zones.push(zone.clone());
65                    last += Self::KMEM_ZBASE;
66                }
67            }
68
69            zones
70        });
71
72        Self {
73            vm,
74            zones,
75            stats: CpuLocal::new(|_| RefCell::default()),
76        }
77    }
78
79    /// Returns null on failure.
80    ///
81    /// See `malloc` on the Orbis for a reference.
82    ///
83    /// # Safety
84    /// `layout` must be nonzero.
85    ///
86    /// # Reference offsets
87    /// | Version | Offset |
88    /// |---------|--------|
89    /// |PS4 11.00|0x1A4220|
90    pub unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
91        // Our implementation imply M_WAITOK.
92        let td = current_thread();
93
94        if !td.can_sleep() {
95            panic!("heap allocation in a non-sleeping context is not supported");
96        }
97
98        // Determine how to allocate.
99        let lock = td.disable_vm_heap();
100        let size = layout.size();
101        let mem = if size <= PAGE_SIZE.get() {
102            // Get zone to allocate from.
103            let align = layout.align().trailing_zeros() as usize;
104            let size = if (size & Self::KMEM_ZMASK) != 0 {
105                // TODO: Refactor this for readability.
106                (size + Self::KMEM_ZBASE) & !Self::KMEM_ZMASK
107            } else {
108                size
109            };
110
111            // Allocate a memory from UMA zone.
112            let zone = &self.zones[align][size >> Self::KMEM_ZSHIFT];
113            let mem = zone.alloc(Alloc::Wait | Alloc::Zero);
114
115            // Update stats.
116            let stats = self.stats.lock();
117            let mut stats = stats.borrow_mut();
118            let size = if mem.is_null() { 0 } else { zone.size().get() };
119
120            if size != 0 {
121                stats.alloc_bytes = stats
122                    .alloc_bytes
123                    .checked_add(size.try_into().unwrap())
124                    .unwrap();
125                stats.alloc_count += 1;
126            }
127
128            // TODO: How to update mts_size here since our zone table also indexed by alignment?
129            mem
130        } else {
131            todo!()
132        };
133
134        drop(lock);
135
136        mem
137    }
138
139    /// See `free` on the Orbis for a reference.
140    ///
141    /// # Safety
142    /// `ptr` must be obtained with [Self::alloc()] and `layout` must be the same one that was
143    /// passed to that method.
144    ///
145    /// # Reference offsets
146    /// | Version | Offset |
147    /// |---------|--------|
148    /// |PS4 11.00|0x1A43E0|
149    pub unsafe fn dealloc(&self, ptr: *mut u8, _: Layout) {
150        let page = (ptr as usize) & !PAGE_MASK.get();
151        let page = unsafe { kaddr_to_phys(page) };
152        let page = self.vm.phys_to_page(page).unwrap(); // Orbis assume the pointer is not null.
153        let ps = page.state.lock();
154        let obj = ps.object.as_ref().unwrap(); // Orbis panic when this is null.
155        let slab = match obj {
156            PageObj::Slab(s) => unsafe { s.as_ref() },
157        };
158
159        if slab.flags().has_any(SlabFlags::Malloc) {
160            todo!()
161        } else {
162            todo!()
163        }
164    }
165}
166
167/// Implementation of `malloc_type_stats` structure.
168#[derive(Default)]
169struct Stats {
170    alloc_bytes: u64, // mts_memalloced
171    alloc_count: u64, // mts_numallocs
172}