Permalink
Browse files

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.
  • Loading branch information...
anp committed May 10, 2016
1 parent 75b0329 commit 0e93e5fb122101f8fb7da2bed40ef1b37727293e
Showing with 117 additions and 45 deletions.
  1. +1 −0 .gitignore
  2. +3 −0 Cargo.toml
  3. +0 −38 musl/src/mman/mmap.c
  4. +3 −2 src/lib.rs
  5. +52 −2 src/mmap.rs
  6. +1 −0 src/platform/linux-x86_64/c_types.rs
  7. +7 −1 src/platform/linux-x86_64/mman.rs
  8. +50 −2 src/syscall_mgt.rs
View
@@ -4,5 +4,6 @@ Cargo.lock
build
*.a
*.o
+*.lo
libc-test/src/common/options.h
rusl_failures
View
@@ -11,3 +11,6 @@ crate-type = ["staticlib", "dylib"]
lazy_static = { version = "0.2.1", features = ["spin_no_std"] }
rlibc = "1.0.0"
syscall = "0.2.1"
+
+[profile.release]
+debug = true
View
@@ -1,38 +0,0 @@
-#include <unistd.h>
-#include <sys/mman.h>
-#include <errno.h>
-#include <stdint.h>
-#include <limits.h>
-#include "syscall.h"
-#include "libc.h"
-
-static void dummy(void) { }
-weak_alias(dummy, __vm_wait);
-
-#define UNIT SYSCALL_MMAP2_UNIT
-#define OFF_MASK ((-0x2000ULL << (8*sizeof(long)-1)) | (UNIT-1))
-
-void *__mmap(void *start, size_t len, int prot, int flags, int fd, off_t off)
-{
- if (off & OFF_MASK) {
- errno = EINVAL;
- return MAP_FAILED;
- }
- if (len >= PTRDIFF_MAX) {
- errno = ENOMEM;
- return MAP_FAILED;
- }
- if (flags & MAP_FIXED) {
- __vm_wait();
- }
-#ifdef SYS_mmap2
- return (void *)syscall(SYS_mmap2, start, len, prot, flags, fd, off/UNIT);
-#else
- return (void *)syscall(SYS_mmap, start, len, prot, flags, fd, off);
-#endif
-}
-
-weak_alias(__mmap, mmap);
-
-/* this creates the mmap64 function */
-LFS64(mmap);
View
@@ -5,13 +5,14 @@
#[macro_use]
extern crate lazy_static;
-#[macro_use]
extern crate syscall;
+#[macro_use]
+pub mod syscall_mgt;
+
pub mod malloc;
pub mod mmap;
pub mod string;
-pub mod syscall_mgt;
pub mod thread;
pub mod time;
pub mod unistd;
View
@@ -1,7 +1,57 @@
-use core::mem::size_of;
-use core::usize;
+use core::isize;
use c_types::*;
+use errno::{set_errno, EINVAL, ENOMEM};
+use platform::mman::*;
+use thread::vmlock::__vm_wait;
+
+#[no_mangle]
+pub unsafe extern "C" fn __mmap(start: *mut c_void,
+ len: size_t,
+ prot: c_int,
+ flags: c_int,
+ fd: c_int,
+ off: off_t)
+ -> *mut c_void {
+
+ if (off & (PAGE_SIZE - 1)) != 0 {
+ set_errno(EINVAL);
+ return MAP_FAILED;
+ }
+
+ if len >= isize::MAX as usize {
+ set_errno(ENOMEM);
+ return MAP_FAILED;
+ }
+
+ if flags & MAP_FIXED != 0 {
+ __vm_wait();
+ }
+
+ syscall!(MMAP, start, len, prot, flags, fd, off) as *mut c_void
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn mmap(start: *mut c_void,
+ len: size_t,
+ prot: c_int,
+ flags: c_int,
+ fd: c_int,
+ off: off_t)
+ -> *mut c_void {
+ __mmap(start, len, prot, flags, fd, off)
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn mmap64(start: *mut c_void,
+ len: size_t,
+ prot: c_int,
+ flags: c_int,
+ fd: c_int,
+ off: off_t)
+ -> *mut c_void {
+ __mmap(start, len, prot, flags, fd, off)
+}
#[no_mangle]
pub unsafe extern "C" fn madvise(address: *mut c_void, len: usize, advice: c_int) -> c_int {
@@ -41,6 +41,7 @@ pub type ptrdiff_t = isize;
pub type intptr_t = isize;
pub type uintptr_t = usize;
pub type ssize_t = isize;
+pub type off_t = i64;
pub type pid_t = i32;
pub type uid_t = u32;
@@ -1 +1,7 @@
-pub const MAP_FAILED: usize = (!1i64) as usize;
+use c_types::*;
+
+pub const PAGE_SIZE: off_t = 4096;
+
+pub const MAP_FAILED: *mut c_void = 0xffffffffffffffff as *mut c_void;
+
+pub const MAP_FIXED: c_int = 0x10;
View
@@ -4,12 +4,60 @@ use c_types::*;
use errno::set_errno;
// from musl/src/internal/syscall_ret.c
-pub unsafe fn syscall_return(code: usize) -> isize {
+pub unsafe fn syscall_return(code: usize) -> usize {
let max_err: usize = transmute(-4096i64);
if code > max_err {
set_errno(-(code as c_int));
- -1
+ (-1isize) as usize
} else {
transmute(code)
}
}
+
+// from the syscall.rs crate, just added the return handling
+#[macro_export]
+macro_rules! syscall {
+ ($nr:ident)
+ => ( ::syscall_mgt::syscall_return(::syscall::syscall0(
+ ::syscall::nr::$nr)) );
+
+ ($nr:ident, $a1:expr)
+ => ( ::syscall_mgt::syscall_return(::syscall::syscall1(
+ ::syscall::nr::$nr,
+ $a1 as usize)) );
+
+ ($nr:ident, $a1:expr, $a2:expr)
+ => ( ::syscall_mgt::syscall_return(::syscall::syscall2(
+ ::syscall::nr::$nr,
+ $a1 as usize, $a2 as usize)) );
+
+ ($nr:ident, $a1:expr, $a2:expr, $a3:expr)
+ => ( ::syscall_mgt::syscall_return(::syscall::syscall3(
+ ::syscall::nr::$nr,
+ $a1 as usize, $a2 as usize, $a3 as usize)) );
+
+ ($nr:ident, $a1:expr, $a2:expr, $a3:expr, $a4:expr)
+ => ( ::syscall_mgt::syscall_return(::syscall::syscall4(
+ ::syscall::nr::$nr,
+ $a1 as usize, $a2 as usize, $a3 as usize,
+ $a4 as usize)) );
+
+ ($nr:ident, $a1:expr, $a2:expr, $a3:expr, $a4:expr, $a5:expr)
+ => ( ::syscall_mgt::syscall_return(::syscall::syscall5(
+ ::syscall::nr::$nr,
+ $a1 as usize, $a2 as usize, $a3 as usize,
+ $a4 as usize, $a5 as usize)) );
+
+ ($nr:ident, $a1:expr, $a2:expr, $a3:expr, $a4:expr, $a5:expr, $a6:expr)
+ => ( ::syscall_mgt::syscall_return(::syscall::syscall6(
+ ::syscall::nr::$nr,
+ $a1 as usize, $a2 as usize, $a3 as usize,
+ $a4 as usize, $a5 as usize, $a6 as usize)) );
+
+ ($nr:ident, $a1:expr, $a2:expr, $a3:expr, $a4:expr, $a5:expr, $a6:expr, $a7:expr)
+ => ( ::syscall_mgt::syscall_return(::syscall::syscall7(
+ ::syscall::nr::$nr,
+ $a1 as usize, $a2 as usize, $a3 as usize,
+ $a4 as usize, $a5 as usize, $a6 as usize,
+ $a7 as usize)) );
+}

0 comments on commit 0e93e5f

Please sign in to comment.