-/* END DEFUNCT */
-
-/* USV: No! need to obsolete map_fd()! mmap() already supports 64 bits */
-kern_return_t
-map_fd(struct map_fd_args *args)
-{
- int fd = args->fd;
- vm_offset_t offset = args->offset;
- vm_offset_t *va = args->va;
- boolean_t findspace = args->findspace;
- vm_size_t size = args->size;
- kern_return_t ret;
-
- AUDIT_MACH_SYSCALL_ENTER(AUE_MAPFD);
- AUDIT_ARG(addr, CAST_DOWN(user_addr_t, va));
- AUDIT_ARG(fd, fd);
-
- ret = map_fd_funneled( fd, (vm_object_offset_t)offset, va, findspace, size);
-
- AUDIT_MACH_SYSCALL_EXIT(ret);
- return ret;
-}
-
-kern_return_t
-map_fd_funneled(
- int fd,
- vm_object_offset_t offset,
- vm_offset_t *va,
- boolean_t findspace,
- vm_size_t size)
-{
- kern_return_t result;
- struct fileproc *fp;
- struct vnode *vp;
- void * pager;
- vm_offset_t map_addr=0;
- vm_size_t map_size;
- int err=0;
- vm_map_t my_map;
- struct proc *p =(struct proc *)current_proc();
- struct vnode_attr vattr;
- struct vfs_context context;
-
- /*
- * Find the inode; verify that it's a regular file.
- */
-
- err = fp_lookup(p, fd, &fp, 0);
- if (err)
- return(err);
-
- if (fp->f_fglob->fg_type != DTYPE_VNODE){
- err = KERN_INVALID_ARGUMENT;
- goto bad;
- }
-
- if (!(fp->f_fglob->fg_flag & FREAD)) {
- err = KERN_PROTECTION_FAILURE;
- goto bad;
- }
-
- vp = (struct vnode *)fp->f_fglob->fg_data;
- err = vnode_getwithref(vp);
- if(err != 0)
- goto bad;
-
- if (vp->v_type != VREG) {
- (void)vnode_put(vp);
- err = KERN_INVALID_ARGUMENT;
- goto bad;
- }
-
- AUDIT_ARG(vnpath, vp, ARG_VNODE1);
-
- /* conformance change - mmap needs to update access time for mapped
- * files
- */
- VATTR_INIT(&vattr);
- nanotime(&vattr.va_access_time);
- VATTR_SET_ACTIVE(&vattr, va_access_time);
- context.vc_proc = p;
- context.vc_ucred = kauth_cred_get();
- vnode_setattr(vp, &vattr, &context);
-
- if (offset & PAGE_MASK_64) {
- printf("map_fd: file offset not page aligned(%d : %s)\n",p->p_pid, p->p_comm);
- (void)vnode_put(vp);
- err = KERN_INVALID_ARGUMENT;
- goto bad;
- }
- map_size = round_page(size);
-
- /*
- * Allow user to map in a zero length file.
- */
- if (size == 0) {
- (void)vnode_put(vp);
- err = KERN_SUCCESS;
- goto bad;
- }
- /*
- * Map in the file.
- */
- UBCINFOCHECK("map_fd_funneled", vp);
- pager = (void *) ubc_getpager(vp);
- if (pager == NULL) {
- (void)vnode_put(vp);
- err = KERN_FAILURE;
- goto bad;
- }
-
-
- my_map = current_map();
-
- result = vm_map_64(
- my_map,
- &map_addr, map_size, (vm_offset_t)0,
- VM_FLAGS_ANYWHERE, pager, offset, TRUE,
- VM_PROT_DEFAULT, VM_PROT_ALL,
- VM_INHERIT_DEFAULT);
- if (result != KERN_SUCCESS) {
- (void)vnode_put(vp);
- err = result;
- goto bad;
- }
-
-
- if (!findspace) {
- vm_offset_t dst_addr;
- vm_map_copy_t tmp;
-
- if (copyin(CAST_USER_ADDR_T(va), &dst_addr, sizeof (dst_addr)) ||
- trunc_page_32(dst_addr) != dst_addr) {
- (void) vm_map_remove(
- my_map,
- map_addr, map_addr + map_size,
- VM_MAP_NO_FLAGS);
- (void)vnode_put(vp);
- err = KERN_INVALID_ADDRESS;
- goto bad;
- }
-
- result = vm_map_copyin(my_map, (vm_map_address_t)map_addr,
- (vm_map_size_t)map_size, TRUE, &tmp);
- if (result != KERN_SUCCESS) {
-
- (void) vm_map_remove(my_map, vm_map_trunc_page(map_addr),
- vm_map_round_page(map_addr + map_size),
- VM_MAP_NO_FLAGS);
- (void)vnode_put(vp);
- err = result;
- goto bad;
- }
-
- result = vm_map_copy_overwrite(my_map,
- (vm_map_address_t)dst_addr, tmp, FALSE);
- if (result != KERN_SUCCESS) {
- vm_map_copy_discard(tmp);
- (void)vnode_put(vp);
- err = result;
- goto bad;
- }
- } else {
- if (copyout(&map_addr, CAST_USER_ADDR_T(va), sizeof (map_addr))) {
- (void) vm_map_remove(my_map, vm_map_trunc_page(map_addr),
- vm_map_round_page(map_addr + map_size),
- VM_MAP_NO_FLAGS);
- (void)vnode_put(vp);
- err = KERN_INVALID_ADDRESS;
- goto bad;
- }
- }
-
- ubc_setthreadcred(vp, current_proc(), current_thread());
- (void)ubc_map(vp, (PROT_READ | PROT_WRITE | PROT_EXEC));
- (void)vnode_put(vp);
- err = 0;
-bad:
- fp_drop(p, fd, fp, 0);
- return (err);
-}
-