static void *mremap_move(struct anon_header *hdr, size_t new_size) { void *new_base; char *p; size_t block_size, old_size; new_base = mmap_anon(new_size); if (new_base == MAP_FAILED) return MAP_FAILED; /* If we're moving large memory areas, it takes less memory to copy the memory pages in smaller blocks. */ old_size = hdr->size; block_size = 1024*1024; p = (char *) hdr + header_size + hdr->size; do { if (block_size > old_size) block_size = old_size; p -= block_size; old_size -= block_size; memcpy((char *) new_base + old_size, p, block_size); if (munmap((void *) p, block_size) < 0) i_panic("munmap() failed: %m"); } while (old_size != 0); if (munmap((void *) hdr, header_size) < 0) i_panic("munmap() failed: %m"); return new_base; }
HANDLE WINAPI CreateFileMappingA(HANDLE handle, LPSECURITY_ATTRIBUTES lpAttr, DWORD flProtect, DWORD dwMaxHigh, DWORD dwMaxLow, LPCSTR name) { int hFile = (int)handle; unsigned int len; LPVOID answer; int anon=0; int mmap_access=0; if(hFile<0) anon=1; if(!anon) { len=lseek(hFile, 0, SEEK_END); lseek(hFile, 0, SEEK_SET); } else len=dwMaxLow; if(flProtect & PAGE_READONLY) mmap_access |=PROT_READ; else mmap_access |=PROT_READ|PROT_WRITE; if(anon) answer=mmap_anon(NULL, len, mmap_access, MAP_PRIVATE, 0); else answer=mmap(NULL, len, mmap_access, MAP_PRIVATE, hFile, 0); if(answer!=(LPVOID)-1) { if(fm==0) { fm = malloc(sizeof(file_mapping)); fm->prev=NULL; } else { fm->next = malloc(sizeof(file_mapping)); fm->next->prev=fm; fm=fm->next; } fm->next=NULL; fm->handle=answer; if(name) { fm->name = malloc(strlen(name)+1); strcpy(fm->name, name); } else fm->name=NULL; fm->mapping_size=len; return (HANDLE)answer; } return (HANDLE)0; }
LPVOID WINAPI VirtualAlloc(LPVOID address, DWORD size, DWORD type, DWORD protection) { void* answer; long pgsz; //printf("VirtualAlloc(0x%08X, %u, 0x%08X, 0x%08X)\n", (unsigned)address, size, type, protection); if ((type&(MEM_RESERVE|MEM_COMMIT)) == 0) return NULL; if (type&MEM_RESERVE && (unsigned)address&0xffff) { size += (unsigned)address&0xffff; address = (unsigned)address&~0xffff; } pgsz = sysconf(_SC_PAGESIZE); if (type&MEM_COMMIT && (unsigned)address%pgsz) { size += (unsigned)address%pgsz; address -= (unsigned)address%pgsz; } if (type&MEM_RESERVE && size<0x10000) size = 0x10000; if (size%pgsz) size += pgsz - size%pgsz; if(address!=0) { //check whether we can allow to allocate this virt_alloc* str=vm; while(str) { if((unsigned)address>=(unsigned)str->address+str->mapping_size) { str=str->prev; continue; } if((unsigned)address+size<=(unsigned)str->address) { str=str->prev; continue; } if(str->state==0) { // FIXME if( ((unsigned)address >= (unsigned)str->address) && ((unsigned)address+size<=(unsigned)str->address+str->mapping_size) && (type & MEM_COMMIT)) { return address; //returning previously reserved memory } //printf(" VirtualAlloc(...) does not commit or not entirely within reserved, and\n"); } /*printf(" VirtualAlloc(...) (0x%08X, %u) overlaps with (0x%08X, %u, state=%d)\n", (unsigned)address, size, (unsigned)str->address, str->mapping_size, str->state);*/ return NULL; } } answer=mmap_anon(address, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, 0); // answer=FILE_dommap(-1, address, 0, size, 0, 0, // PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE); if (answer != (void *)-1 && address && answer != address) { /* It is dangerous to try mmap() with MAP_FIXED since it does not always detect conflicts or non-allocation and chaos ensues after a successful call but an overlapping or non-allocated region. */ munmap(answer, size); answer = (void *) -1; errno = EINVAL; //printf(" VirtualAlloc(...) cannot satisfy requested address but address=NULL would work.\n"); } if(answer==(void*)-1) { /*printf(" VirtualAlloc(...) mmap(0x%08X, %u, ...) failed with errno=%d (\"%s\")\n", (unsigned)address, size, errno, strerror(errno));*/ return NULL; } else { virt_alloc *new_vm = malloc(sizeof(virt_alloc)); new_vm->mapping_size=size; new_vm->address=(char*)answer; new_vm->prev=vm; if(type == MEM_RESERVE) new_vm->state=0; else new_vm->state=1; if(vm) vm->next=new_vm; vm=new_vm; vm->next=0; //if(va_size!=0) // printf("Multiple VirtualAlloc!\n"); //printf(" VirtualAlloc(...) provides (0x%08X, %u)\n", (unsigned)answer, size); return answer; } }
//#define MAP_PRIVATE //#define MAP_SHARED LPVOID FILE_dommap( int unix_handle, LPVOID start, DWORD size_high, DWORD size_low, DWORD offset_high, DWORD offset_low, int prot, int flags ) { int fd = -1; int pos; LPVOID ret; if (size_high || offset_high) printf("offsets larger than 4Gb not supported\n"); if (unix_handle == -1) { ret = mmap_anon( start, size_low, prot, flags, offset_low ); } else { fd = unix_handle; ret = mmap( start, size_low, prot, flags, fd, offset_low ); } if (ret != (LPVOID)-1) { // printf("address %08x\n", *(int*)ret); // printf("%x\n", ret); return ret; } // printf("mmap %d\n", errno); /* mmap() failed; if this is because the file offset is not */ /* page-aligned (EINVAL), or because the underlying filesystem */ /* does not support mmap() (ENOEXEC), we do it by hand. */ if (unix_handle == -1) return ret; if ((errno != ENOEXEC) && (errno != EINVAL)) return ret; if (prot & PROT_WRITE) { /* We cannot fake shared write mappings */ #ifdef MAP_SHARED if (flags & MAP_SHARED) return ret; #endif #ifdef MAP_PRIVATE if (!(flags & MAP_PRIVATE)) return ret; #endif } /* printf( "FILE_mmap: mmap failed (%d), faking it\n", errno );*/ /* Reserve the memory with an anonymous mmap */ ret = FILE_dommap( -1, start, size_high, size_low, 0, 0, PROT_READ | PROT_WRITE, flags ); if (ret == (LPVOID)-1) // { // perror( return ret; /* Now read in the file */ if ((pos = lseek( fd, offset_low, SEEK_SET )) == -1) { FILE_munmap( ret, size_high, size_low ); // printf("lseek\n"); return (LPVOID)-1; } read( fd, ret, size_low ); lseek( fd, pos, SEEK_SET ); /* Restore the file pointer */ mprotect( ret, size_low, prot ); /* Set the right protection */ // printf("address %08x\n", *(int*)ret); return ret; }