-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathmm.rs
More file actions
457 lines (407 loc) · 14.7 KB
/
mm.rs
File metadata and controls
457 lines (407 loc) · 14.7 KB
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
//! User address space management.
use alloc::{borrow::ToOwned, string::String, vec, vec::Vec};
use core::{
ffi::CStr,
hint::unlikely,
iter,
mem::MaybeUninit,
sync::atomic::{AtomicBool, Ordering},
};
use axerrno::{AxError, AxResult};
use axfs::{CachedFile, FS_CONTEXT, FileBackend};
use axfs_ng_vfs::Location;
use axhal::{
asm::user_copy,
mem::virt_to_phys,
paging::{MappingFlags, PageSize},
};
use axmm::{AddrSpace, backend::Backend};
use axsync::Mutex;
use extern_trait::extern_trait;
use kernel_elf_parser::{AuxEntry, ELFHeaders, ELFHeadersBuilder, ELFParser, app_stack_region};
use kernel_guard::IrqSave;
use memory_addr::{MemoryAddr, PAGE_SIZE_4K, VirtAddr};
use ouroboros::self_referencing;
use starry_vm::{VmError, VmIo, VmResult};
use uluru::LRUCache;
use crate::config::{USER_SPACE_BASE, USER_SPACE_SIZE};
/// Creates a new empty user address space.
pub fn new_user_aspace_empty() -> AxResult<AddrSpace> {
AddrSpace::new_empty(
VirtAddr::from_usize(crate::config::USER_SPACE_BASE),
crate::config::USER_SPACE_SIZE,
)
}
/// If the target architecture requires it, the kernel portion of the address
/// space will be copied to the user address space.
pub fn copy_from_kernel(_aspace: &mut AddrSpace) -> AxResult {
#[cfg(not(any(target_arch = "aarch64", target_arch = "loongarch64")))]
{
// ARMv8 (aarch64) and LoongArch64 use separate page tables for user space
// (aarch64: TTBR0_EL1, LoongArch64: PGDL), so there is no need to copy the
// kernel portion to the user page table.
_aspace.copy_mappings_from(&axmm::kernel_aspace().lock())?;
}
Ok(())
}
/// Map the signal trampoline to the user address space.
pub fn map_trampoline(aspace: &mut AddrSpace) -> AxResult {
let signal_trampoline_paddr =
virt_to_phys(starry_signal::arch::signal_trampoline_address().into());
aspace.map_linear(
crate::config::SIGNAL_TRAMPOLINE.into(),
signal_trampoline_paddr,
PAGE_SIZE_4K,
MappingFlags::READ | MappingFlags::EXECUTE | MappingFlags::USER,
)?;
Ok(())
}
fn mapping_flags(flags: xmas_elf::program::Flags) -> MappingFlags {
let mut mapping_flags = MappingFlags::USER;
if flags.is_read() {
mapping_flags |= MappingFlags::READ;
}
if flags.is_write() {
mapping_flags |= MappingFlags::WRITE;
}
if flags.is_execute() {
mapping_flags |= MappingFlags::EXECUTE;
}
mapping_flags
}
/// Map the elf file to the user address space.
///
/// # Arguments
/// - `uspace`: The address space of the user app.
/// - `elf`: The elf file.
///
/// # Returns
/// - The entry point of the user app.
fn map_elf<'a>(
uspace: &mut AddrSpace,
base: usize,
entry: &'a ElfCacheEntry,
) -> AxResult<ELFParser<'a>> {
let elf_parser = ELFParser::new(entry.borrow_elf(), base).map_err(|_| AxError::InvalidData)?;
let cache = entry.borrow_cache();
for ph in elf_parser
.headers()
.ph
.iter()
.filter(|ph| ph.get_type() == Ok(xmas_elf::program::Type::Load))
{
let vaddr = ph.virtual_addr as usize + elf_parser.base();
debug!(
"Mapping ELF segment: [{:#x?}, {:#x?}) flags: {}",
vaddr,
vaddr + ph.mem_size as usize,
ph.flags
);
let seg_pad = vaddr.align_offset_4k();
assert_eq!(seg_pad, ph.offset as usize % PAGE_SIZE_4K);
let seg_align_size =
(ph.mem_size as usize + seg_pad + PAGE_SIZE_4K - 1) & !(PAGE_SIZE_4K - 1);
let seg_start = VirtAddr::from_usize(vaddr);
// Note that `offset` might not be aligned to 4K here, and it's
// backend's responsibility to properly handle it.
let backend = Backend::new_cow(
seg_start,
PageSize::Size4K,
FileBackend::Cached(cache.clone()),
ph.offset,
Some(ph.offset + ph.file_size),
);
uspace.map(
seg_start.align_down_4k(),
seg_align_size,
mapping_flags(ph.flags),
false,
backend,
)?;
// TDOO: flush the I-cache
}
Ok(elf_parser)
}
fn map_elf_error(err: &'static str) -> AxError {
debug!("Failed to parse ELF file: {err}");
AxError::InvalidExecutable
}
#[self_referencing]
struct ElfCacheEntry {
cache: CachedFile,
data: Vec<u8>,
#[borrows(data)]
#[covariant]
elf: ELFHeaders<'this>,
}
impl ElfCacheEntry {
fn load(loc: Location) -> AxResult<Result<Self, Vec<u8>>> {
let cache = CachedFile::get_or_create(loc);
let mut data = vec![0; 4096];
let read = cache.read_at(&mut data.as_mut_slice(), 0)?;
data.truncate(read);
match ElfCacheEntry::try_new_or_recover::<AxError>(cache.clone(), data, |data| {
let builder = ELFHeadersBuilder::new(data).map_err(map_elf_error)?;
let range = builder.ph_range();
if range.end as usize <= data.len() {
builder.build(&data[range.start as usize..range.end as usize])
} else {
let mut buf = vec![0; (range.end - range.start) as usize];
cache.read_at(&mut buf.as_mut_slice(), range.start)?;
builder.build(&buf)
}
.map_err(map_elf_error)
}) {
Ok(e) => Ok(Ok(e)),
Err((_, heads)) => Ok(Err(heads.data)),
}
}
}
struct ElfLoader(LRUCache<ElfCacheEntry, 32>);
type LoadResult = Result<(VirtAddr, Vec<AuxEntry>), Vec<u8>>;
impl ElfLoader {
const fn new() -> Self {
Self(LRUCache::new())
}
fn load(&mut self, uspace: &mut AddrSpace, path: &str) -> AxResult<LoadResult> {
let loc = FS_CONTEXT.lock().resolve(path)?;
if !self.0.touch(|e| e.borrow_cache().location().ptr_eq(&loc)) {
match ElfCacheEntry::load(loc)? {
Ok(e) => {
self.0.insert(e);
}
Err(data) => {
return Ok(Err(data));
}
}
}
uspace.clear();
map_trampoline(uspace)?;
let entry = self.0.front().unwrap();
let ldso = if let Some(header) = entry
.borrow_elf()
.ph
.iter()
.find(|ph| ph.get_type() == Ok(xmas_elf::program::Type::Interp))
{
let cache = entry.borrow_cache();
let mut data = vec![0; header.file_size as usize];
let read = cache.read_at(&mut data.as_mut_slice(), header.offset)?;
assert_eq!(data.len(), read);
let ldso = CStr::from_bytes_with_nul(&data)
.ok()
.and_then(|cstr| cstr.to_str().ok())
.ok_or(AxError::InvalidInput)?;
debug!("Loading dynamic linker: {ldso}");
Some(ldso.to_owned())
} else {
None
};
let (elf, ldso) = if let Some(ldso) = ldso {
let loc = FS_CONTEXT.lock().resolve(ldso)?;
if !self.0.touch(|e| e.borrow_cache().location().ptr_eq(&loc)) {
let e = ElfCacheEntry::load(loc)?.map_err(|_| AxError::InvalidInput)?;
self.0.insert(e);
}
let mut iter = self.0.iter();
let ldso = iter.next().unwrap();
let elf = iter.next().unwrap();
(elf, Some(ldso))
} else {
(entry, None)
};
let elf = map_elf(uspace, crate::config::USER_SPACE_BASE, elf)?;
let ldso = ldso
.map(|elf| map_elf(uspace, crate::config::USER_INTERP_BASE, elf))
.transpose()?;
let entry = VirtAddr::from_usize(
ldso.as_ref()
.map_or_else(|| elf.entry(), |ldso| ldso.entry()),
);
let mut auxv = elf
.aux_vector(PAGE_SIZE_4K, ldso.map(|elf| elf.base()))
.collect::<Vec<_>>();
{
let uspace = core::cell::RefCell::new(&mut *uspace);
starry_vdso::vdso::load_vdso_data(
&mut auxv,
|map_user_start, vdso_paddr_page, vdso_size| {
uspace
.borrow_mut()
.map_linear(
map_user_start.into(),
vdso_paddr_page,
vdso_size,
MappingFlags::READ | MappingFlags::EXECUTE | MappingFlags::USER,
)
.map_err(|_| AxError::InvalidExecutable)
},
|vvar_user_addr, vvar_paddr| {
uspace
.borrow_mut()
.map_linear(
vvar_user_addr.into(),
vvar_paddr.into(),
starry_vdso::config::VVAR_PAGES * PAGE_SIZE_4K,
MappingFlags::READ | MappingFlags::USER,
)
.map_err(|_| AxError::InvalidExecutable)
},
|seg_user_start, seg_paddr, seg_align_size, ph| {
let mut flags = MappingFlags::USER;
if ph.flags.is_read() {
flags |= MappingFlags::READ;
}
if ph.flags.is_write() {
flags |= MappingFlags::WRITE;
}
if ph.flags.is_execute() {
flags |= MappingFlags::EXECUTE;
}
uspace
.borrow_mut()
.map_linear(seg_user_start.into(), seg_paddr, seg_align_size, flags)
.map_err(|_| AxError::InvalidExecutable)
},
)?;
}
Ok(Ok((entry, auxv)))
}
}
static ELF_LOADER: Mutex<ElfLoader> = Mutex::new(ElfLoader::new());
/// Clear the ELF cache.
///
/// Useful for removing noises during memory leak detect.
pub fn clear_elf_cache() {
ELF_LOADER.lock().0.clear();
}
/// Load the user app to the user address space.
///
/// # Arguments
/// - `uspace`: The address space of the user app.
/// - `args`: The arguments of the user app. The first argument is the path of
/// the user app.
/// - `envs`: The environment variables of the user app.
///
/// # Returns
/// - The entry point of the user app.
/// - The stack pointer of the user app.
pub fn load_user_app(
uspace: &mut AddrSpace,
path: Option<&str>,
args: &[String],
envs: &[String],
) -> AxResult<(VirtAddr, VirtAddr, Vec<AuxEntry>)> {
let path = path
.or_else(|| args.first().map(String::as_str))
.ok_or(AxError::InvalidInput)?;
// FIXME: impl `/proc/self/exe` to let busybox retry running
if path.ends_with(".sh") {
let new_args: Vec<String> = iter::once("/bin/sh".to_owned())
.chain(args.iter().cloned())
.collect();
return load_user_app(uspace, None, &new_args, envs);
}
let (entry, auxv) = match { ELF_LOADER.lock().load(uspace, path)? } {
Ok((entry, auxv)) => (entry, auxv),
Err(data) => {
if data.starts_with(b"#!") {
let head = &data[2..data.len().min(256)];
let pos = head.iter().position(|c| *c == b'\n').unwrap_or(head.len());
let line = core::str::from_utf8(&head[..pos]).map_err(|_| AxError::InvalidInput)?;
let new_args: Vec<String> = line
.trim()
.splitn(2, |c: char| c.is_ascii_whitespace())
.map(|s| s.trim_ascii().to_owned())
.chain(iter::once(path.to_owned()))
.chain(args.iter().skip(1).cloned())
.collect();
return load_user_app(uspace, None, &new_args, envs);
}
return Err(AxError::InvalidExecutable);
}
};
let ustack_top = VirtAddr::from_usize(crate::config::USER_STACK_TOP);
let ustack_size = crate::config::USER_STACK_SIZE;
let ustack_start = ustack_top - ustack_size;
debug!("Mapping user stack: {ustack_start:#x?} -> {ustack_top:#x?}");
uspace.map(
ustack_start,
ustack_size,
MappingFlags::READ | MappingFlags::WRITE | MappingFlags::USER,
false,
Backend::new_alloc(ustack_start, PageSize::Size4K),
)?;
let stack_data = app_stack_region(args, envs, &auxv, ustack_top.into());
let user_sp = ustack_top - stack_data.len();
let user_sp_aligned = user_sp.align_down_4k();
uspace.populate_area(
user_sp_aligned,
(ustack_top - user_sp_aligned).align_up_4k(),
MappingFlags::READ | MappingFlags::WRITE,
)?;
uspace.write(user_sp, stack_data.as_slice())?;
let heap_start = VirtAddr::from_usize(crate::config::USER_HEAP_BASE);
let heap_size = crate::config::USER_HEAP_SIZE;
uspace.map(
heap_start,
heap_size,
MappingFlags::READ | MappingFlags::WRITE | MappingFlags::USER,
true,
Backend::new_alloc(heap_start, PageSize::Size4K),
)?;
Ok((entry, user_sp, auxv))
}
static ACCESSING_USER_MEM: AtomicBool = AtomicBool::new(false);
/// Enables scoped access into user memory, allowing page faults to occur inside
/// kernel.
pub fn access_user_memory<R>(f: impl FnOnce() -> R) -> R {
ACCESSING_USER_MEM.store(true, Ordering::Release);
let result = f();
ACCESSING_USER_MEM.store(false, Ordering::Release);
result
}
/// Check if the current thread is accessing user memory.
pub fn is_accessing_user_memory() -> bool {
ACCESSING_USER_MEM.load(Ordering::Acquire)
}
#[allow(dead_code)]
struct Vm(IrqSave);
/// Briefly checks if the given memory region is valid user memory.
pub fn check_access(start: usize, len: usize) -> VmResult {
const USER_SPACE_END: usize = USER_SPACE_BASE + USER_SPACE_SIZE;
let ok = (USER_SPACE_BASE..USER_SPACE_END).contains(&start) && (USER_SPACE_END - start) >= len;
if unlikely(!ok) {
Err(VmError::AccessDenied)
} else {
Ok(())
}
}
#[extern_trait]
unsafe impl VmIo for Vm {
fn new() -> Self {
Self(IrqSave::new())
}
fn read(&mut self, start: usize, buf: &mut [MaybeUninit<u8>]) -> VmResult {
check_access(start, buf.len())?;
let failed_at = access_user_memory(|| unsafe {
user_copy(buf.as_mut_ptr() as *mut _, start as _, buf.len())
});
if unlikely(failed_at != 0) {
Err(VmError::AccessDenied)
} else {
Ok(())
}
}
fn write(&mut self, start: usize, buf: &[u8]) -> VmResult {
check_access(start, buf.len())?;
let failed_at = access_user_memory(|| unsafe {
user_copy(start as _, buf.as_ptr() as *const _, buf.len())
});
if unlikely(failed_at != 0) {
Err(VmError::AccessDenied)
} else {
Ok(())
}
}
}