Skip to content

Commit 0e93e5f

Browse files
committed
Working mmap.
* Also replaced syscall! with in-tree version that calls syscall_return() on result. * Learned how to use and read strace and objdump today too.
1 parent 75b0329 commit 0e93e5f

File tree

8 files changed

+117
-45
lines changed

8 files changed

+117
-45
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ Cargo.lock
44
build
55
*.a
66
*.o
7+
*.lo
78
libc-test/src/common/options.h
89
rusl_failures

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ crate-type = ["staticlib", "dylib"]
1111
lazy_static = { version = "0.2.1", features = ["spin_no_std"] }
1212
rlibc = "1.0.0"
1313
syscall = "0.2.1"
14+
15+
[profile.release]
16+
debug = true

musl/src/mman/mmap.c

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55

66
#[macro_use]
77
extern crate lazy_static;
8-
#[macro_use]
98
extern crate syscall;
109

10+
#[macro_use]
11+
pub mod syscall_mgt;
12+
1113
pub mod malloc;
1214
pub mod mmap;
1315
pub mod string;
14-
pub mod syscall_mgt;
1516
pub mod thread;
1617
pub mod time;
1718
pub mod unistd;

src/mmap.rs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,57 @@
1-
use core::mem::size_of;
2-
use core::usize;
1+
use core::isize;
32

43
use c_types::*;
4+
use errno::{set_errno, EINVAL, ENOMEM};
5+
use platform::mman::*;
6+
use thread::vmlock::__vm_wait;
7+
8+
#[no_mangle]
9+
pub unsafe extern "C" fn __mmap(start: *mut c_void,
10+
len: size_t,
11+
prot: c_int,
12+
flags: c_int,
13+
fd: c_int,
14+
off: off_t)
15+
-> *mut c_void {
16+
17+
if (off & (PAGE_SIZE - 1)) != 0 {
18+
set_errno(EINVAL);
19+
return MAP_FAILED;
20+
}
21+
22+
if len >= isize::MAX as usize {
23+
set_errno(ENOMEM);
24+
return MAP_FAILED;
25+
}
26+
27+
if flags & MAP_FIXED != 0 {
28+
__vm_wait();
29+
}
30+
31+
syscall!(MMAP, start, len, prot, flags, fd, off) as *mut c_void
32+
}
33+
34+
#[no_mangle]
35+
pub unsafe extern "C" fn mmap(start: *mut c_void,
36+
len: size_t,
37+
prot: c_int,
38+
flags: c_int,
39+
fd: c_int,
40+
off: off_t)
41+
-> *mut c_void {
42+
__mmap(start, len, prot, flags, fd, off)
43+
}
44+
45+
#[no_mangle]
46+
pub unsafe extern "C" fn mmap64(start: *mut c_void,
47+
len: size_t,
48+
prot: c_int,
49+
flags: c_int,
50+
fd: c_int,
51+
off: off_t)
52+
-> *mut c_void {
53+
__mmap(start, len, prot, flags, fd, off)
54+
}
555

656
#[no_mangle]
757
pub unsafe extern "C" fn madvise(address: *mut c_void, len: usize, advice: c_int) -> c_int {

src/platform/linux-x86_64/c_types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub type ptrdiff_t = isize;
4141
pub type intptr_t = isize;
4242
pub type uintptr_t = usize;
4343
pub type ssize_t = isize;
44+
pub type off_t = i64;
4445

4546
pub type pid_t = i32;
4647
pub type uid_t = u32;

src/platform/linux-x86_64/mman.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
pub const MAP_FAILED: usize = (!1i64) as usize;
1+
use c_types::*;
2+
3+
pub const PAGE_SIZE: off_t = 4096;
4+
5+
pub const MAP_FAILED: *mut c_void = 0xffffffffffffffff as *mut c_void;
6+
7+
pub const MAP_FIXED: c_int = 0x10;

src/syscall_mgt.rs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,60 @@ use c_types::*;
44
use errno::set_errno;
55

66
// from musl/src/internal/syscall_ret.c
7-
pub unsafe fn syscall_return(code: usize) -> isize {
7+
pub unsafe fn syscall_return(code: usize) -> usize {
88
let max_err: usize = transmute(-4096i64);
99
if code > max_err {
1010
set_errno(-(code as c_int));
11-
-1
11+
(-1isize) as usize
1212
} else {
1313
transmute(code)
1414
}
1515
}
16+
17+
// from the syscall.rs crate, just added the return handling
18+
#[macro_export]
19+
macro_rules! syscall {
20+
($nr:ident)
21+
=> ( ::syscall_mgt::syscall_return(::syscall::syscall0(
22+
::syscall::nr::$nr)) );
23+
24+
($nr:ident, $a1:expr)
25+
=> ( ::syscall_mgt::syscall_return(::syscall::syscall1(
26+
::syscall::nr::$nr,
27+
$a1 as usize)) );
28+
29+
($nr:ident, $a1:expr, $a2:expr)
30+
=> ( ::syscall_mgt::syscall_return(::syscall::syscall2(
31+
::syscall::nr::$nr,
32+
$a1 as usize, $a2 as usize)) );
33+
34+
($nr:ident, $a1:expr, $a2:expr, $a3:expr)
35+
=> ( ::syscall_mgt::syscall_return(::syscall::syscall3(
36+
::syscall::nr::$nr,
37+
$a1 as usize, $a2 as usize, $a3 as usize)) );
38+
39+
($nr:ident, $a1:expr, $a2:expr, $a3:expr, $a4:expr)
40+
=> ( ::syscall_mgt::syscall_return(::syscall::syscall4(
41+
::syscall::nr::$nr,
42+
$a1 as usize, $a2 as usize, $a3 as usize,
43+
$a4 as usize)) );
44+
45+
($nr:ident, $a1:expr, $a2:expr, $a3:expr, $a4:expr, $a5:expr)
46+
=> ( ::syscall_mgt::syscall_return(::syscall::syscall5(
47+
::syscall::nr::$nr,
48+
$a1 as usize, $a2 as usize, $a3 as usize,
49+
$a4 as usize, $a5 as usize)) );
50+
51+
($nr:ident, $a1:expr, $a2:expr, $a3:expr, $a4:expr, $a5:expr, $a6:expr)
52+
=> ( ::syscall_mgt::syscall_return(::syscall::syscall6(
53+
::syscall::nr::$nr,
54+
$a1 as usize, $a2 as usize, $a3 as usize,
55+
$a4 as usize, $a5 as usize, $a6 as usize)) );
56+
57+
($nr:ident, $a1:expr, $a2:expr, $a3:expr, $a4:expr, $a5:expr, $a6:expr, $a7:expr)
58+
=> ( ::syscall_mgt::syscall_return(::syscall::syscall7(
59+
::syscall::nr::$nr,
60+
$a1 as usize, $a2 as usize, $a3 as usize,
61+
$a4 as usize, $a5 as usize, $a6 as usize,
62+
$a7 as usize)) );
63+
}

0 commit comments

Comments
 (0)