| /* For email below, drop spaces and <spam-buster> tag. |
| * MODIFIED: March 20, 2014 (jric<spam-buster> @ <spam-buster> chegg DOT com) |
| */ |
| #include "efence.h" |
| #include <fcntl.h> |
| #include <parlib/uthread.h> |
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <string.h> |
| #include <sys/mman.h> |
| #include <unistd.h> |
| |
| static caddr_t startAddr = (caddr_t)0; |
| |
| /* |
| * Create memory. |
| */ |
| void *Page_Create(size_t size) |
| { |
| caddr_t allocation; |
| |
| /* |
| * In this version, "startAddr" is a _hint_, not a demand. |
| * When the memory I map here is contiguous with other |
| * mappings, the allocator can coalesce the memory from two |
| * or more mappings into one large contiguous chunk, and thus |
| * might be able to find a fit that would not otherwise have |
| * been possible. I could _force_ it to be contiguous by using |
| * the MMAP_FIXED flag, but I don't want to stomp on memory mappings |
| * generated by other software, etc. |
| */ |
| allocation = (caddr_t)mmap(startAddr, size, PROT_READ | PROT_WRITE, |
| MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| |
| /* |
| * Set the "address hint" for the next mmap() so that it will abut |
| * the mapping we just created. |
| * |
| * HP/UX 9.01 has a kernel bug that makes mmap() fail sometimes |
| * when given a non-zero address hint, so we'll leave the hint set |
| * to zero on that system. HP recently told me this is now fixed. |
| * Someone please tell me when it is probable to assume that most |
| * of those systems that were running 9.01 have been upgraded. |
| */ |
| startAddr = allocation + size; |
| |
| if (allocation == (caddr_t)-1) |
| EF_Exit("mmap() failed: %r"); |
| |
| return (void *)allocation; |
| } |
| |
| static void mprotectFailed(void) |
| { |
| EF_Exit("mprotect() failed: %r"); |
| } |
| |
| void Page_AllowAccess(void *address, size_t size) |
| { |
| if (mprotect((caddr_t)address, size, PROT_READ | PROT_WRITE) < 0) |
| mprotectFailed(); |
| } |
| |
| void Page_DenyAccess(void *address, size_t size) |
| { |
| if (mprotect((caddr_t)address, size, PROT_NONE) < 0) |
| mprotectFailed(); |
| } |
| |
| void Page_Delete(void *address, size_t size) |
| { |
| Page_DenyAccess(address, size); |
| } |
| |
| size_t Page_Size(void) |
| { |
| return PGSIZE; |
| } |