From: Ben Pfaff Date: Tue, 29 Mar 2005 07:41:20 +0000 (+0000) Subject: Update mmap() tests. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=90e779441b8ed82f464f35624ad9a79d93651d9b;p=pintos-anon Update mmap() tests. Change MAPID_ERROR to MAP_FAILED to be closer to POSIX. --- diff --git a/grading/vm/child-mm-wrt.c b/grading/vm/child-mm-wrt.c index 28a52c6..b0794ec 100644 --- a/grading/vm/child-mm-wrt.c +++ b/grading/vm/child-mm-wrt.c @@ -30,7 +30,7 @@ main (void) return 1; } - if (!mmap (fd, ACTUAL, strlen (sample))) + if (mmap (fd, ACTUAL) == MAP_FAILED) { printf ("(child-mm-wrt) mmap() failed\n"); return 1; diff --git a/grading/vm/mmap-close.c b/grading/vm/mmap-close.c index 901b34d..335f244 100644 --- a/grading/vm/mmap-close.c +++ b/grading/vm/mmap-close.c @@ -14,6 +14,7 @@ int main (void) { int fd; + mapid_t map; printf ("(mmap-close) begin\n"); @@ -24,7 +25,8 @@ main (void) return 1; } - if (!mmap (fd, ACTUAL, strlen (sample))) + map = mmap (fd, ACTUAL); + if (map == MAP_FAILED) { printf ("(mmap-close) mmap() failed\n"); return 1; @@ -38,7 +40,7 @@ main (void) return 1; } - munmap (ACTUAL, strlen (sample)); + munmap (map); /* Done. */ printf ("(mmap-close) end\n"); diff --git a/grading/vm/mmap-overlap.c b/grading/vm/mmap-overlap.c index 6153793..d2104b8 100644 --- a/grading/vm/mmap-overlap.c +++ b/grading/vm/mmap-overlap.c @@ -30,7 +30,7 @@ main (void) printf ("(mmap-overlap) open() failed\n"); return 1; } - if (!mmap (fd[i], start, 8192)) + if (mmap (fd[i], start) == MAP_FAILED) { if (i == 1) return 0; diff --git a/grading/vm/mmap-read.c b/grading/vm/mmap-read.c index b763ddd..cb25ba1 100644 --- a/grading/vm/mmap-read.c +++ b/grading/vm/mmap-read.c @@ -13,6 +13,7 @@ int main (void) { int fd; + mapid_t map; printf ("(mmap-read) begin\n"); @@ -23,7 +24,8 @@ main (void) return 1; } - if (!mmap (fd, ACTUAL, strlen (sample))) + map = mmap (fd, ACTUAL); + if (map == MAP_FAILED) { printf ("(mmap-read) mmap() failed\n"); return 1; @@ -35,7 +37,7 @@ main (void) return 1; } - if (!munmap (ACTUAL, strlen (sample))) + if (!munmap (map)) { printf ("(mmap-read) munmap() failed\n"); return 1; diff --git a/grading/vm/mmap-shuffle.c b/grading/vm/mmap-shuffle.c index ae16f4a..15a37f4 100644 --- a/grading/vm/mmap-shuffle.c +++ b/grading/vm/mmap-shuffle.c @@ -72,7 +72,7 @@ main (void) return 1; } - if (!mmap (fd, buf, SIZE)) + if (mmap (fd, buf) == MAP_FAILED) { printf ("(mmap-shuffle) mmap() failed\n"); return 1; diff --git a/grading/vm/mmap-twice.c b/grading/vm/mmap-twice.c index aba5ef2..a2b5a10 100644 --- a/grading/vm/mmap-twice.c +++ b/grading/vm/mmap-twice.c @@ -24,7 +24,7 @@ main (void) printf ("(mmap-twice) open() #%zu failed\n", i); return 1; } - if (!mmap (fd[i], actual[i], 8192)) + if (mmap (fd[i], actual[i]) == MAP_FAILED) { printf ("(mmap-twice) mmap() #%zu failed\n", i); return 1; diff --git a/grading/vm/mmap-unmap.c b/grading/vm/mmap-unmap.c index 2ab3eea..327158b 100644 --- a/grading/vm/mmap-unmap.c +++ b/grading/vm/mmap-unmap.c @@ -13,6 +13,7 @@ int main (void) { int fd; + mapid_t map; printf ("(mmap-unmap) begin\n"); @@ -23,13 +24,14 @@ main (void) return 1; } - if (!mmap (fd, ACTUAL, strlen (sample))) + map = mmap (fd, ACTUAL); + if (map == MAP_FAILED) { printf ("(mmap-unmap) mmap() failed\n"); return 1; } - munmap (ACTUAL, strlen (sample)); + munmap (map); printf ("(mmap-unmap) FAIL: unmapped memory is readable (%d)\n", *(int *) ACTUAL); diff --git a/grading/vm/mmap-write.c b/grading/vm/mmap-write.c index 183c822..2a9cda4 100644 --- a/grading/vm/mmap-write.c +++ b/grading/vm/mmap-write.c @@ -13,6 +13,7 @@ int main (void) { int fd; + mapid_t map; char buf[1024]; printf ("(mmap-write) begin\n"); @@ -31,13 +32,14 @@ main (void) return 1; } - if (!mmap (fd, ACTUAL, strlen (sample))) + map = mmap (fd, ACTUAL); + if (map == MAP_FAILED) { printf ("(mmap-write) mmap() failed\n"); return 1; } memcpy (ACTUAL, sample, strlen (sample)); - munmap (ACTUAL, strlen (sample)); + munmap (map); /* Read back via read(). */ read (fd, buf, strlen (sample)); diff --git a/grading/vm/posix-compat.c b/grading/vm/posix-compat.c index dc5b5ab..a4aae6b 100644 --- a/grading/vm/posix-compat.c +++ b/grading/vm/posix-compat.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -100,10 +101,9 @@ pintos_open (const char *file) int pintos_filesize (int fd) { - off_t old = lseek (fd, 0, SEEK_CUR); - off_t size = lseek (fd, 0, SEEK_END); - lseek (fd, old, SEEK_SET); - return size; + struct stat s; + fstat (fd, &s); + return s.st_size; } int @@ -137,17 +137,24 @@ pintos_close (int fd) close (fd); } -bool -pintos_mmap (int fd, void *addr, unsigned length) +mapid_t +pintos_mmap (int fd, void *addr) { - return mmap (addr, length, PROT_READ | PROT_WRITE, - MAP_SHARED | MAP_FIXED, fd, 0) == addr; + size_t page_size = getpagesize (); + int length = pintos_filesize (fd); + uintptr_t ptr = (uintptr_t) mmap (addr, length, PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_FIXED, fd, 0); + return ptr | DIV_ROUND_UP (size, page_size); } bool pintos_munmap (void *addr, unsigned length) { - return munmap (addr, length) == 0; + size_t page_size = getpagesize (); + uintptr_t page_mask = page_size - 1; + void *base = (void *) ((uintptr_t) addr & ~page_mask); + size_t length = ((uintptr_t) addr & page_mask) * page_size; + return munmap (base, length); } bool diff --git a/src/lib/user/syscall.h b/src/lib/user/syscall.h index f629466..1472525 100644 --- a/src/lib/user/syscall.h +++ b/src/lib/user/syscall.h @@ -8,7 +8,7 @@ typedef int pid_t; #define PID_ERROR ((pid_t) -1) typedef int mapid_t; -#define MAPID_ERROR ((mapid_t) -1) +#define MAP_FAILED ((mapid_t) -1) void halt (void) NO_RETURN; void exit (int status) NO_RETURN;