/*===========================================================================* * update_refcount_and_destroy * *===========================================================================*/ void update_refcount_and_destroy(void) { int i, j; for (i = 0, j = 0; i < shm_list_nr; i++) { u8_t rc; rc = vm_getrefcount(SELF_E, (void *) shm_list[i].page); if (rc == (u8_t) -1) { printf("IPC: can't find physical region.\n"); continue; } shm_list[i].shmid_ds.shm_nattch = rc - 1; if (shm_list[i].shmid_ds.shm_nattch || !(shm_list[i].shmid_ds.shm_perm.mode & SHM_DEST)) { if (i != j) shm_list[j] = shm_list[i]; j++; } else { int size = shm_list[i].shmid_ds.shm_segsz; if (size % I386_PAGE_SIZE) size += I386_PAGE_SIZE - size % I386_PAGE_SIZE; minix_munmap((void *)shm_list[i].page, size); } } shm_list_nr = j; }
void minix_munmap_t(void *a, int len) { vir_bytes av = (vir_bytes) a; assert(a); assert(a != MAP_FAILED); assert(len > 0); assert(!(len % PAGE_SIZE)); assert(!(av % PAGE_SIZE)); if(minix_munmap(a, len) < 0) panic("libminixfs cache: munmap failed"); }
void basic_regression(void) { void *block; #define BLOCKSIZE (PAGE_SIZE*10) block = minix_mmap(0, BLOCKSIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); if(block == MAP_FAILED) { e(1); exit(1); } memset(block, 0, BLOCKSIZE); /* shrink from bottom */ minix_munmap(block, PAGE_SIZE); }
int readblock(int b, int blocksize, u32_t seed, char *data) { u64_t offset; int fd; char *mmapdata; int pread_first = random() % 2; get_fd_offset(b, blocksize, &offset, &fd); if(pread_first) { if(pread(fd, data, blocksize, offset) < blocksize) { perror("pread"); return -1; } } if((mmapdata = minix_mmap(NULL, blocksize, PROT_READ, MAP_PRIVATE | MAP_FILE, fd, offset)) == MAP_FAILED) { perror("mmap"); return -1; } if(!pread_first) { if(pread(fd, data, blocksize, offset) < blocksize) { perror("pread"); return -1; } } if(memcmp(mmapdata, data, blocksize)) { fprintf(stderr, "readblock: mmap, pread mismatch\n"); return -1; } if(minix_munmap(mmapdata, blocksize) < 0) { perror("munmap"); return -1; } return blocksize; }
/*===========================================================================* * m_block_close * *===========================================================================*/ static int m_block_close(dev_t minor) { /* Close a memory block device. */ if (m_block_part(minor) == NULL) return(ENXIO); if(openct[minor] < 1) { printf("MEMORY: closing unopened device %d\n", minor); return(EINVAL); } openct[minor]--; #if 0 /* Special case: free initial ramdisk after it's been unmounted once. */ if(minor == IMGRD_DEV && openct[minor] == 0 && m_vaddrs[IMGRD_DEV]) { vir_bytes vaddr, vlen; vaddr = m_vaddrs[IMGRD_DEV]; vlen = imgrd_size; /* Align `inwards' so as to not unmap more than the initial * ramdisk image. */ if(vaddr % PAGE_SIZE) { vir_bytes o = PAGE_SIZE - (vaddr % PAGE_SIZE); vlen -= o; vaddr += o; } if(vlen % PAGE_SIZE) { vlen -= vlen % PAGE_SIZE; } minix_munmap((void *) vaddr, vlen); m_geom[IMGRD_DEV].dv_base= cvul64(0); m_geom[IMGRD_DEV].dv_size= cvul64(0); m_vaddrs[IMGRD_DEV] = 0; } #endif return(OK); }
int free_contig(void *addr, size_t len) { return minix_munmap(addr, len); }
/*===========================================================================* * m_block_ioctl * *===========================================================================*/ static int m_block_ioctl(dev_t minor, unsigned int request, endpoint_t endpt, cp_grant_id_t grant) { /* I/O controls for the block devices of the memory driver. Currently there is * one I/O control specific to the memory driver: * - MIOCRAMSIZE: to set the size of the RAM disk. */ struct device *dv; u32_t ramdev_size; int s; void *mem; if (request != MIOCRAMSIZE) return EINVAL; /* Someone wants to create a new RAM disk with the given size. * A ramdisk can be created only once, and only on RAM disk device. */ if ((dv = m_block_part(minor)) == NULL) return ENXIO; if((minor < RAM_DEV_FIRST || minor > RAM_DEV_LAST) && minor != RAM_DEV_OLD) { printf("MEM: MIOCRAMSIZE: %d not a ramdisk\n", minor); return EINVAL; } /* Get request structure */ s= sys_safecopyfrom(endpt, grant, 0, (vir_bytes)&ramdev_size, sizeof(ramdev_size)); if (s != OK) return s; if(m_vaddrs[minor] && !cmp64(dv->dv_size, cvul64(ramdev_size))) { return(OK); } /* openct is 1 for the ioctl(). */ if(openct[minor] != 1) { printf("MEM: MIOCRAMSIZE: %d in use (count %d)\n", minor, openct[minor]); return(EBUSY); } if(m_vaddrs[minor]) { u32_t size; if(ex64hi(dv->dv_size)) { panic("huge old ramdisk"); } size = ex64lo(dv->dv_size); minix_munmap((void *) m_vaddrs[minor], size); m_vaddrs[minor] = (vir_bytes) NULL; } #if DEBUG printf("MEM:%d: allocating ramdisk of size 0x%x\n", minor, ramdev_size); #endif /* Try to allocate a piece of memory for the RAM disk. */ if((mem = minix_mmap(NULL, ramdev_size, PROT_READ|PROT_WRITE, MAP_PREALLOC|MAP_ANON, -1, 0)) == MAP_FAILED) { printf("MEM: failed to get memory for ramdisk\n"); return(ENOMEM); } m_vaddrs[minor] = (vir_bytes) mem; dv->dv_size = cvul64(ramdev_size); return(OK); }