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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//! Address space of user mode management of microkernel version of ArceOS.
#![no_std]

#[macro_use]
extern crate alloc;
#[macro_use]
extern crate axlog;

use core::cmp::Ordering;

use alloc::sync::Arc;
use alloc::vec::Vec;
use alloc::{collections::BTreeMap, string::String};
use axalloc::GlobalPage;
use axerrno::{ax_err, AxError, AxResult};
use axhal::{
    mem::{phys_to_virt, virt_to_phys},
    paging::{MappingFlags, PageTable},
};
use crate_interface::def_interface;
use memory_addr::{align_up, align_up_4k, PhysAddr, VirtAddr, PAGE_SIZE_4K};
use spinlock::SpinNoIrq;

/// Start of binary
pub const USER_START: usize = 0x0400_0000;
/// top of user stack
pub const USTACK_START: usize = 0x10_0000_0000 - USTACK_SIZE;
/// size of user stack
pub const USTACK_SIZE: usize = 4096 * 10;
/// address of trampoline page
pub const TRAMPOLINE_START: usize = 0xffff_ffc0_0000_0000;
/// start of default mmap areas
pub const MMAP_AREA_START: usize = 0x10_0000_0000;
/// end of default mmap areas
pub const MMAP_AREA_END: usize = 0x20_0000_0000;

pub(crate) struct MapSegment {
    start_vaddr: VirtAddr,
    size: usize,
    phy_mem: Vec<Arc<GlobalPage>>,
}

#[derive(Clone)]
pub(crate) struct HeapSegment {
    start_vaddr: VirtAddr,
    actual_size: usize,
}

/// mutable inner objects of address space
pub struct AddrSpaceInner {
    segments: alloc::vec::Vec<MapSegment>,
    page_table: PageTable,
    heap: Option<HeapSegment>,
    mmap_use: BTreeMap<VirtAddr, GlobalPage>,
}

impl Default for AddrSpaceInner {
    fn default() -> Self {
        Self::new()
    }
}

impl AddrSpaceInner {
    pub(crate) fn new() -> AddrSpaceInner {
        AddrSpaceInner {
            segments: vec![],
            page_table: PageTable::try_new().expect("Creating page table failed!"),
            heap: None,
            mmap_use: BTreeMap::new(),
        }
    }

    /// create a segment region
    pub fn add_region(
        &mut self,
        vaddr: VirtAddr,
        paddr: PhysAddr,
        phy_page: Arc<GlobalPage>,
        flags: MappingFlags,
        huge_page: bool,
    ) -> AxResult<()> {
        self.page_table
            .map_region(vaddr, paddr, phy_page.size(), flags, huge_page)
            .map_err(|_| AxError::BadAddress)?;
        self.segments.push(MapSegment {
            start_vaddr: vaddr,
            size: phy_page.size(),
            phy_mem: vec![phy_page],
        });
        Ok(())
    }

    /// create a segment region without memory allocation
    pub fn add_region_shadow(
        &mut self,
        vaddr: VirtAddr,
        paddr: PhysAddr,
        size: usize,
        flags: MappingFlags,
        huge_page: bool,
    ) -> AxResult<()> {
        self.page_table
            .map_region(vaddr, paddr, size, flags, huge_page)
            .map_err(|_| AxError::BadAddress)?;
        self.segments.push(MapSegment {
            start_vaddr: vaddr,
            size,
            phy_mem: vec![],
        });
        Ok(())
    }

    /// remove a segment
    pub fn remove_region(&mut self, vaddr: VirtAddr) -> AxResult<()> {
        if let Some((idx, _)) = self
            .segments
            .iter()
            .enumerate()
            .find(|(_, page)| page.start_vaddr == vaddr)
        {
            let item = self.segments.remove(idx);
            self.page_table
                .unmap_region(item.start_vaddr, item.size)
                .map_err(|_| AxError::BadAddress)?;
            Ok(())
        } else {
            ax_err!(BadAddress)
        }
    }

    /// get root address of pagetable
    pub fn page_table_addr(&self) -> PhysAddr {
        self.page_table.root_paddr()
    }

    pub(crate) fn init_heap(&mut self, vaddr: VirtAddr) {
        if self.heap.is_some() {
            return;
        }
        self.heap = Some(HeapSegment {
            start_vaddr: vaddr,
            actual_size: 0,
        });
        let page = GlobalPage::alloc_zero().expect("Alloc error!");
        self.page_table
            .map_region(
                vaddr,
                page.start_paddr(virt_to_phys),
                page.size(),
                MappingFlags::READ | MappingFlags::WRITE | MappingFlags::USER,
                false,
            )
            .expect("Mapping Segment Error");
        self.segments.push(MapSegment {
            start_vaddr: vaddr,
            size: page.size(),
            phy_mem: vec![page.into()],
        });
        info!("User heap inited @ {:x}", vaddr);
    }

    /// modify heap size
    pub fn sbrk(&mut self, size: isize) -> Option<usize> {
        if let Some(heap) = &mut self.heap {
            let old_brk: usize = (heap.start_vaddr + heap.actual_size).into();
            trace!("user sbrk: {} bytes", size);
            debug!("sbrk: {}, {}", size, old_brk);
            match size.cmp(&0) {
                Ordering::Equal => {
                    return Some(old_brk);
                }
                Ordering::Less => {
                    if (-size) as usize > heap.actual_size {
                        return None;
                    }
                    heap.actual_size -= -size as usize
                }
                Ordering::Greater => {
                    heap.actual_size += size as usize;
                    let heap_seg = self
                        .segments
                        .iter_mut()
                        .find(|x| x.start_vaddr == heap.start_vaddr)
                        .unwrap();
                    if heap.actual_size > heap_seg.size {
                        let delta = align_up_4k(heap.actual_size - heap_seg.size);
                        while heap.actual_size > heap_seg.size {
                            if let Ok(page) =
                                GlobalPage::alloc_contiguous(delta / PAGE_SIZE_4K, PAGE_SIZE_4K)
                            {
                                self.page_table
                                    .map_region(
                                        heap.start_vaddr + heap_seg.size,
                                        page.start_paddr(virt_to_phys),
                                        page.size(),
                                        MappingFlags::READ
                                            | MappingFlags::WRITE
                                            | MappingFlags::USER,
                                        false,
                                    )
                                    .expect("Mapping Error");
                                heap_seg.size += page.size();
                                heap_seg.phy_mem.push(page.into());
                            } else {
                                return None;
                            }
                        }
                    }
                }
            }
            Some(old_brk)
        } else {
            None
        }
    }

    /// a simple mmap-like page allocator, except that memory is alloced in pages
    /// @param addr: desired memory position
    /// @param len: desired pages
    /// @returns: starting addr of the maped pages
    pub fn mmap_page(
        &mut self,
        _addr: Option<VirtAddr>,
        len: usize,
        flags: MappingFlags,
    ) -> AxResult<VirtAddr> {
        let mut addr: VirtAddr = MMAP_AREA_START.into();
        let len = align_up_4k(len);
        let pages = len / PAGE_SIZE_4K;
        while addr + len < MMAP_AREA_END.into() {
            if let Some(offset) = (0..pages)
                .find(|offset| self.mmap_use.contains_key(&(addr + offset * PAGE_SIZE_4K)))
            {
                addr += (offset + 1) * PAGE_SIZE_4K;
            } else {
                // TODO: undo when error
                (0..pages).for_each(|offset| {
                    let phy_page = GlobalPage::alloc_zero().expect("Run out of memory!");
                    self.page_table
                        .map_region(
                            addr + offset * PAGE_SIZE_4K,
                            phy_page.start_paddr(virt_to_phys),
                            PAGE_SIZE_4K,
                            flags,
                            false,
                        )
                        .expect("Mapping error");
                    self.mmap_use.insert(addr + offset * PAGE_SIZE_4K, phy_page);
                });
                return Ok(addr);
            }
        }
        ax_err!(NoMemory)
    }

    /// unmap a page obtained from `mmap`
    pub fn munmap_page(&mut self, addr: VirtAddr, len: usize) -> AxResult<()> {
        let len = align_up_4k(len);
        trace!("unmap: [{:x?}, {:x?})", addr, addr + len);
        let pages = len / PAGE_SIZE_4K;
        if (0..pages).any(|offset| !self.mmap_use.contains_key(&(addr + offset * PAGE_SIZE_4K))) {
            return ax_err!(BadAddress);
        } else {
            (0..pages).for_each(|offset| {
                self.page_table.unmap(addr + offset * PAGE_SIZE_4K).unwrap();
                self.mmap_use.remove(&(addr + offset * PAGE_SIZE_4K));
            })
        }
        Ok(())
    }

    /// translate a slice to slices in kernel
    pub fn translate_buffer(
        &self,
        vaddr: VirtAddr,
        size: usize,
        _write: bool,
    ) -> AxResult<Vec<&'static mut [u8]>> {
        let mut read_size = 0usize;
        let mut vaddr = vaddr;
        let mut result: Vec<&'static mut [u8]> = vec![];
        while read_size < size {
            let (paddr, _flag, page_size) = self
                .page_table
                .query(vaddr)
                .map_err(|_| AxError::BadAddress)?;
            /*
                if !flag.contains(MappingFlags::USER) || (write && !flag.contains(MappingFlags::WRITE)) {
                panic!("Invalid vaddr with improper rights!");
            }
                 */
            let nxt_vaddr = align_up(vaddr.as_usize() + 1, page_size.into());
            let len = (nxt_vaddr - vaddr.as_usize()).min(size - read_size);
            let data =
                unsafe { core::slice::from_raw_parts_mut(phys_to_virt(paddr).as_mut_ptr(), len) };
            trace!("translating {:x} -> {:x}, len = {}", vaddr, paddr, len);
            vaddr += len;
            read_size += len;
            result.push(data);
        }
        Ok(result)
    }

    /// find a mapping of the virtual address
    pub fn query(&self, vaddr: VirtAddr) -> Option<PhysAddr> {
        self.page_table.query(vaddr).ok().map(|x| x.0)
    }
}

impl AddrSpaceInner {
    fn init_global(&mut self, data: &[u8]) -> AxResult<()> {
        let segments = elf_loader::SegmentEntry::new(data).ok_or(AxError::InvalidData)?;

        let mut data_end: VirtAddr = 0.into();

        for segment in &segments {
            let mut user_phy_page = GlobalPage::alloc_contiguous(
                align_up_4k(segment.size) / PAGE_SIZE_4K,
                PAGE_SIZE_4K,
            )
            .expect("Alloc page error!");
            // init
            user_phy_page.zero();

            // copy user content
            user_phy_page.as_slice_mut()[..segment.data.len()].copy_from_slice(segment.data);
            debug!(
                "{:x} {:x}",
                user_phy_page.as_slice()[0],
                user_phy_page.as_slice()[1]
            );

            self.add_region(
                segment.start_addr,
                user_phy_page.start_paddr(virt_to_phys),
                Arc::new(user_phy_page),
                segment.flags | MappingFlags::USER,
                false,
            )
            .map_err(|_| AxError::BadAddress)?;
            data_end = data_end.max(segment.start_addr + align_up_4k(segment.size))
        }

        self.init_heap(data_end);

        /*
        // stack allocation
        //assert!(USTACK_SIZE % PAGE_SIZE_4K == 0);
        #[cfg(not(feature = "multitask"))]
        {
            let user_stack_page =
                GlobalPage::alloc_contiguous(USTACK_SIZE / PAGE_SIZE_4K, PAGE_SIZE_4K)
                    .expect("Alloc page error!");
            debug!("{:?}", user_stack_page);

            self
                .add_region(
                    USTACK_START.into(),
                    user_stack_page.start_paddr(virt_to_phys),
                    Arc::new(user_stack_page),
                    MappingFlags::READ | MappingFlags::WRITE | MappingFlags::USER,
                    false,
                )
                .expect("Memory Error");
        }
        */
        extern "C" {
            fn strampoline();
        }
        self.add_region_shadow(
            TRAMPOLINE_START.into(),
            virt_to_phys((strampoline as usize).into()),
            PAGE_SIZE_4K,
            MappingFlags::READ | MappingFlags::EXECUTE,
            false,
        )
        .expect("Memory Error");

        Ok(())
    }
}

impl Clone for AddrSpace {
    fn clone(&self) -> Self {
        let inner = self.lock();
        let mut new_inner = AddrSpaceInner::new();
        let page_table = &mut new_inner.page_table;

        for segment in &inner.segments {
            let mut new_seg = MapSegment {
                start_vaddr: segment.start_vaddr,
                size: segment.size,
                phy_mem: Vec::new(),
            };
            let pages = segment.size / PAGE_SIZE_4K;

            /*
                if segment.phy_mem.is_empty() { // pages such as trampoline
                (0..pages).for_each(|page| {
                let vaddr = segment.start_vaddr + page * PAGE_SIZE_4K;
                let (paddr, flags, _) = inner.page_table.query(vaddr).unwrap();
                page_table.map_region(vaddr, paddr, PAGE_SIZE_4K, flags, false).unwrap();
            })
            } else {
                 */
            (0..pages).for_each(|page| {
                let vaddr = segment.start_vaddr + page * PAGE_SIZE_4K;
                let (paddr, flags, _) = inner.page_table.query(vaddr).unwrap();
                let mut user_phy_page = GlobalPage::alloc().unwrap();
                user_phy_page.as_slice_mut().copy_from_slice(unsafe {
                    core::slice::from_raw_parts(phys_to_virt(paddr).as_ptr(), PAGE_SIZE_4K)
                });
                page_table
                    .map_region(
                        vaddr,
                        user_phy_page.start_paddr(virt_to_phys),
                        PAGE_SIZE_4K,
                        flags,
                        false,
                    )
                    .unwrap();
                new_seg.phy_mem.push(user_phy_page.into());
            });
            //}
            new_inner.segments.push(new_seg)
        }
        new_inner.heap = inner.heap.clone();
        for (vaddr, page) in &inner.mmap_use {
            let mut user_phy_page = GlobalPage::alloc().unwrap();
            user_phy_page
                .as_slice_mut()
                .copy_from_slice(page.as_slice());
            let (_, flags, _) = inner.page_table.query(*vaddr).unwrap();

            page_table
                .map_region(
                    *vaddr,
                    user_phy_page.start_paddr(virt_to_phys),
                    PAGE_SIZE_4K,
                    flags,
                    false,
                )
                .unwrap();
            new_inner.mmap_use.insert(*vaddr, user_phy_page);
        }

        AddrSpace(SpinNoIrq::new(new_inner))
    }
}

/// A user address space
pub struct AddrSpace(SpinNoIrq<AddrSpaceInner>);

impl AddrSpace {
    /// init address space of a process
    pub fn init_global(user_elf: &[u8]) -> AxResult<AddrSpace> {
        let mut user_space = AddrSpaceInner::new();
        user_space.init_global(user_elf)?;

        Ok(AddrSpace(SpinNoIrq::new(user_space)))
    }

    /// init address space in current structure.
    pub fn init_exec(&self, user_elf: &[u8]) -> AxResult<()> {
        let mut new_space = AddrSpaceInner::new();
        new_space.init_global(user_elf)?;
        *self.0.lock() = new_space;
        Ok(())
    }
}

impl core::ops::Deref for AddrSpace {
    type Target = SpinNoIrq<AddrSpaceInner>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

/// interface to get current process's address space
#[def_interface]
pub trait CurrentAddrSpace {
    /// get reference of current address space
    fn current_addr_space() -> Arc<AddrSpace>;
}

fn current_addr_space() -> Arc<AddrSpace> {
    crate_interface::call_interface!(CurrentAddrSpace::current_addr_space)
}

/// alloc a user space
pub fn alloc_user_page(vaddr: VirtAddr, size: usize, flags: MappingFlags) -> Arc<GlobalPage> {
    let mut user_phy_page =
        GlobalPage::alloc_contiguous(align_up_4k(size) / PAGE_SIZE_4K, PAGE_SIZE_4K)
            .expect("Alloc page error!");
    // init
    user_phy_page.zero();
    let user_phy_page = Arc::new(user_phy_page);

    current_addr_space()
        .lock()
        .add_region(
            vaddr,
            user_phy_page.start_paddr(virt_to_phys),
            user_phy_page.clone(),
            flags,
            false,
        )
        .expect("Memory Error");

    user_phy_page
}

/// manage heap space of current process
pub fn global_sbrk(size: isize) -> Option<usize> {
    current_addr_space().lock().sbrk(size)
}

/// get root of page table of current process
pub fn get_satp() -> usize {
    current_addr_space().lock().page_table_addr().into()
}

/// mmap a page of current process
pub fn mmap_page(addr: Option<VirtAddr>, len: usize, flags: MappingFlags) -> AxResult<VirtAddr> {
    current_addr_space().lock().mmap_page(addr, len, flags)
}

/// unmap a page of current process
pub fn munmap_page(addr: VirtAddr, len: usize) -> AxResult<()> {
    current_addr_space().lock().munmap_page(addr, len)
}

/// translate a slice from current process
pub fn translate_buffer(vaddr: VirtAddr, size: usize, write: bool) -> Vec<&'static mut [u8]> {
    current_addr_space()
        .lock()
        .translate_buffer(vaddr, size, write)
        .unwrap()
}

/// copy a slice of current process into kernel address space
pub fn copy_slice_from_user(vaddr: VirtAddr, size: usize) -> Vec<u8> {
    let mut result = Vec::new();
    let buffers = translate_buffer(vaddr, size, false);
    for fragment in &buffers {
        result.extend_from_slice(fragment);
    }
    result
}

/// copy a `str` in current process into kernel address space
pub fn copy_str_from_user(vaddr: VirtAddr, size: usize) -> String {
    let result = copy_slice_from_user(vaddr, size);
    String::from_utf8(result).expect("Invalid string!")
}

/// translate a virtual address to phyical address of current process
pub fn translate_addr(vaddr: VirtAddr) -> Option<PhysAddr> {
    current_addr_space().lock().query(vaddr)
}

/// Copy a [u8] array `data' from current memory space into position `ptr' of the userspace `token'(not used)
// Copied from my code in rCore
pub fn copy_byte_buffer_to_user(_token: usize, ptr: *const u8, data: &[u8]) {
    let copy_len = data.len();
    let dst = translate_buffer((ptr as usize).into(), copy_len, false);
    let mut offset = 0;
    for dst_space in dst {
        let dst_len = dst_space.len();
        dst_space.copy_from_slice(&data[offset..offset + dst_len]);
        offset += dst_len;
    }
    assert_eq!(copy_len, offset);
}

/// Copy a `data' with type `T' from current memory space into position `ptr' of the userspace `token' (not used)
// Copied from my code in rCore
pub fn copy_data_to_user<T>(token: usize, ptr: *const u8, data: &T) {
    let data_ptr = data as *const T as *const u8;
    let data_buf = unsafe { core::slice::from_raw_parts(data_ptr, core::mem::size_of::<T>()) };
    copy_byte_buffer_to_user(token, ptr, data_buf);
}