[1] | 1 | /* |
---|
[23] | 2 | * sys_mmap.c - map files, memory or devices into process virtual address space |
---|
[1] | 3 | * |
---|
[23] | 4 | * Authors Ghassan Almaless (2008,2009,2010,2011,2012) |
---|
| 5 | * Alain Greiner (2016,2017) |
---|
[1] | 6 | * |
---|
[23] | 7 | * Copyright (c) UPMC Sorbonne Universites |
---|
[1] | 8 | * |
---|
[23] | 9 | * This file is part of ALMOS-MKH. |
---|
| 10 | * |
---|
| 11 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
[1] | 12 | * under the terms of the GNU General Public License as published by |
---|
| 13 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 14 | * |
---|
[23] | 15 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
[1] | 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 18 | * General Public License for more details. |
---|
| 19 | * |
---|
| 20 | * You should have received a copy of the GNU General Public License |
---|
[23] | 21 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
[1] | 22 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 23 | */ |
---|
| 24 | |
---|
[23] | 25 | #include <hal_types.h> |
---|
[1] | 26 | #include <errno.h> |
---|
| 27 | #include <thread.h> |
---|
[23] | 28 | #include <printk.h> |
---|
[1] | 29 | #include <vfs.h> |
---|
| 30 | #include <process.h> |
---|
| 31 | #include <vmm.h> |
---|
| 32 | |
---|
| 33 | /////////////////////////////////// |
---|
[23] | 34 | int sys_mmap( mmap_attr_t * attr ) |
---|
[1] | 35 | { |
---|
[23] | 36 | printk("\n[WARNING] function %s not implemented\n", __FUNCTION__ ); |
---|
| 37 | return 0; |
---|
| 38 | /* |
---|
[1] | 39 | error_t err; |
---|
| 40 | uint_t count; |
---|
| 41 | struct thread_s *this; |
---|
| 42 | struct process_s *process; |
---|
| 43 | struct vfs_file_s *file; |
---|
| 44 | mmap_attr_t attr; |
---|
| 45 | size_t isize; |
---|
| 46 | int retval; |
---|
| 47 | |
---|
| 48 | this = current_thread; |
---|
| 49 | process = this->process; |
---|
| 50 | err = EINVAL; |
---|
| 51 | file = NULL; |
---|
| 52 | |
---|
[23] | 53 | if((err = cpu_copy_from_uspace(&attr, attr, sizeof(mmap_attr_t)))) |
---|
[1] | 54 | { |
---|
| 55 | printk(INFO, "%s: failed, copying from uspace @%x\n", |
---|
| 56 | __FUNCTION__, |
---|
[23] | 57 | attr); |
---|
[1] | 58 | |
---|
| 59 | this->info.errno = EFAULT; |
---|
| 60 | return (int)VM_FAILED; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | if((attr.flags & VM_REG_HEAP) || |
---|
| 64 | ((attr.flags & VM_REG_PVSH) == VM_REG_PVSH) || |
---|
| 65 | ((attr.flags & VM_REG_PVSH) == 0) || |
---|
| 66 | (attr.length == 0) || |
---|
| 67 | (attr.offset & PMM_PAGE_MASK) || |
---|
| 68 | ((attr.addr != NULL) && (((uint_t)attr.addr & PMM_PAGE_MASK) || |
---|
| 69 | (NOT_IN_USPACE(attr.length + (uint_t)attr.addr)) || |
---|
| 70 | (NOT_IN_USPACE((uint_t)attr.addr)) ))) |
---|
| 71 | { |
---|
| 72 | printk(INFO, "%s: failed, we don't like flags (%x), length (%d), or addr (%x)\n", |
---|
| 73 | __FUNCTION__, |
---|
| 74 | attr.flags, |
---|
| 75 | attr.length, |
---|
| 76 | attr.addr); |
---|
| 77 | |
---|
| 78 | this->info.errno = EINVAL; |
---|
| 79 | return (int)VM_FAILED; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | if(attr.flags & VM_REG_ANON) |
---|
| 83 | { |
---|
| 84 | attr.offset = 0; |
---|
| 85 | attr.addr = (attr.flags & VM_REG_FIXED) ? attr.addr : NULL; |
---|
| 86 | } |
---|
| 87 | else |
---|
| 88 | { |
---|
[23] | 89 | // FIXME: possible concurent delete of file from another bugy thread closing it |
---|
[1] | 90 | if((attr.fd >= CONFIG_TASK_FILE_MAX_NR) || (process_fd_lookup(process, attr.fd, &file))) |
---|
| 91 | { |
---|
| 92 | printk(INFO, "%s: failed, bad file descriptor (%d)\n", |
---|
| 93 | __FUNCTION__, |
---|
| 94 | attr.fd); |
---|
| 95 | |
---|
| 96 | this->info.errno = EBADFD; |
---|
| 97 | return (int)VM_FAILED; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | //atomic_add(&file->f_count, 1); |
---|
| 101 | vfs_file_up(file);//FIXME coalsce access to remote node info |
---|
| 102 | |
---|
| 103 | //FIXME: does we really to get the size... |
---|
| 104 | isize = vfs_inode_size_get_remote(file->f_inode.ptr, file->f_inode.cid); |
---|
| 105 | if((attr.offset + attr.length) > isize) |
---|
| 106 | { |
---|
| 107 | printk(INFO, "%s: failed, offset (%d) + len (%d) >= file's size (%d)\n", |
---|
| 108 | __FUNCTION__, |
---|
| 109 | attr.offset, |
---|
| 110 | attr.length, |
---|
| 111 | isize); |
---|
| 112 | |
---|
| 113 | this->info.errno = ERANGE; |
---|
| 114 | goto SYS_MMAP_FILE_ERR; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | if(((attr.prot & VM_REG_RD) && !(VFS_IS(file->f_flags, VFS_O_RDONLY))) || |
---|
| 118 | ((attr.prot & VM_REG_WR) && !(VFS_IS(file->f_flags, VFS_O_WRONLY))) || |
---|
| 119 | ((attr.prot & VM_REG_WR) && (VFS_IS(file->f_flags, VFS_O_APPEND))))// || |
---|
| 120 | //(!(attr.prot & VM_REG_RD) && (attr.flags & VM_REG_PRIVATE))) |
---|
| 121 | { |
---|
| 122 | printk(INFO, "%s: failed, EACCES prot (%x), f_flags (%x)\n", |
---|
| 123 | __FUNCTION__, |
---|
| 124 | attr.prot, |
---|
| 125 | file->f_flags); |
---|
| 126 | |
---|
| 127 | this->info.errno = EACCES; |
---|
| 128 | goto SYS_MMAP_FILE_ERR; |
---|
| 129 | } |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | retval = (int) vmm_mmap(process, |
---|
| 133 | file, |
---|
| 134 | attr.addr, |
---|
| 135 | attr.length, |
---|
| 136 | attr.prot, |
---|
| 137 | attr.flags, |
---|
| 138 | attr.offset); |
---|
| 139 | |
---|
| 140 | if((retval != (int)VM_FAILED) || (attr.flags & VM_REG_ANON)) |
---|
| 141 | return retval; |
---|
| 142 | |
---|
| 143 | SYS_MMAP_FILE_ERR: |
---|
| 144 | printk(INFO, "%s: Failed, Droping file count \n", |
---|
| 145 | __FUNCTION__); |
---|
| 146 | |
---|
| 147 | vfs_close( file , &count ); |
---|
| 148 | |
---|
| 149 | if(count == 1) process_fd_put( process , attr.fd ); |
---|
| 150 | |
---|
| 151 | return (int)VM_FAILED; |
---|
[23] | 152 | */ |
---|
| 153 | } // end sys_mmap() |
---|