[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> |
---|
[407] | 26 | #include <hal_uspace.h> |
---|
| 27 | #include <shared_syscalls.h> |
---|
[1] | 28 | #include <errno.h> |
---|
| 29 | #include <thread.h> |
---|
[23] | 30 | #include <printk.h> |
---|
[407] | 31 | #include <mapper.h> |
---|
[1] | 32 | #include <vfs.h> |
---|
| 33 | #include <process.h> |
---|
| 34 | #include <vmm.h> |
---|
| 35 | |
---|
[407] | 36 | ////////////////////////////////// |
---|
[23] | 37 | int sys_mmap( mmap_attr_t * attr ) |
---|
[1] | 38 | { |
---|
[407] | 39 | vseg_t * vseg; |
---|
| 40 | cxy_t vseg_cxy; |
---|
| 41 | vseg_type_t vseg_type; |
---|
| 42 | mmap_attr_t k_attr; // attributes copy in kernel space |
---|
| 43 | xptr_t mapper_xp; |
---|
| 44 | error_t error; |
---|
| 45 | paddr_t paddr; // unused, but required for user space checking |
---|
[1] | 46 | |
---|
[407] | 47 | uint64_t tm_start; |
---|
| 48 | uint64_t tm_end; |
---|
[1] | 49 | |
---|
[407] | 50 | tm_start = hal_get_cycles(); |
---|
[1] | 51 | |
---|
[407] | 52 | thread_t * this = CURRENT_THREAD; |
---|
| 53 | process_t * process = this->process; |
---|
| 54 | |
---|
| 55 | // check arguments in user space |
---|
| 56 | error = vmm_v2p_translate( false , attr , &paddr ); |
---|
| 57 | |
---|
| 58 | if ( error ) |
---|
| 59 | { |
---|
| 60 | printk("\n[ERROR] in %s : arguments not in used space = %x\n", |
---|
| 61 | __FUNCTION__ , (intptr_t)attr ); |
---|
| 62 | this->errno = EINVAL; |
---|
| 63 | return -1; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | // copy arguments from uspace |
---|
| 67 | hal_copy_from_uspace( &k_attr , attr , sizeof(mmap_attr_t) ); |
---|
| 68 | |
---|
| 69 | // get fdid, offset, and length arguments |
---|
| 70 | uint32_t fdid = k_attr.fdid; |
---|
| 71 | uint32_t offset = k_attr.offset; |
---|
| 72 | uint32_t length = k_attr.length; |
---|
| 73 | |
---|
| 74 | // get flags |
---|
| 75 | bool_t map_fixed = ( (k_attr.flags & MAP_FIXED) != 0 ); |
---|
| 76 | bool_t map_anon = ( (k_attr.flags & MAP_ANON) != 0 ); |
---|
| 77 | bool_t map_remote = ( (k_attr.flags & MAP_REMOTE) != 0 ); |
---|
| 78 | bool_t map_shared = ( (k_attr.flags & MAP_SHARED) != 0 ); |
---|
| 79 | bool_t map_private = ( (k_attr.flags & MAP_PRIVATE) != 0 ); |
---|
| 80 | |
---|
| 81 | // MAP_FIXED not supported |
---|
| 82 | if( map_fixed ) |
---|
| 83 | { |
---|
| 84 | printk("\n[ERROR] in %s : MAP_FIXED not supported\n", __FUNCTION__ ); |
---|
| 85 | this->errno = EINVAL; |
---|
| 86 | return -1; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | if( map_shared == map_private ) |
---|
| 90 | { |
---|
| 91 | printk("\n[ERROR] in %s : MAP_SHARED xor MAP_PRIVATE\n", __FUNCTION__ ); |
---|
| 92 | this->errno = EINVAL; |
---|
| 93 | return -1; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | // FIXME handle Copy_On_Write for MAP_PRIVATE... |
---|
| 97 | |
---|
| 98 | // get access rigths |
---|
| 99 | bool_t prot_read = ( (k_attr.prot & PROT_READ ) != 0 ); |
---|
| 100 | bool_t prot_write = ( (k_attr.prot & PROT_WRITE) != 0 ); |
---|
| 101 | |
---|
| 102 | // test mmap type : can be FILE / ANON / REMOTE |
---|
| 103 | |
---|
| 104 | if( (map_anon == false) && (map_remote == false) ) // FILE |
---|
| 105 | { |
---|
| 106 | // FIXME: handle concurent delete of file by another thread closing it |
---|
| 107 | |
---|
| 108 | if( fdid >= CONFIG_PROCESS_FILE_MAX_NR ) |
---|
[1] | 109 | { |
---|
[407] | 110 | printk("\n[ERROR] in %s: bad file descriptor = %d\n", __FUNCTION__ , fdid ); |
---|
| 111 | this->errno = EBADFD; |
---|
| 112 | return -1; |
---|
| 113 | } |
---|
[1] | 114 | |
---|
[407] | 115 | // get extended pointer on file descriptor |
---|
| 116 | xptr_t file_xp = process_fd_get_xptr( process , fdid ); |
---|
| 117 | |
---|
| 118 | if( file_xp == XPTR_NULL ) |
---|
| 119 | { |
---|
| 120 | printk("\n[ERROR] in %s: file %d not found\n", __FUNCTION__ , fdid ); |
---|
| 121 | this->errno = EBADFD; |
---|
| 122 | return -1; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | // get file cluster and local pointer |
---|
| 126 | cxy_t file_cxy = GET_CXY( file_xp ); |
---|
| 127 | vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp ); |
---|
| 128 | |
---|
| 129 | // get inode pointer, mapper pointer and file attributes |
---|
| 130 | vfs_inode_t * inode_ptr = hal_remote_lpt(XPTR(file_cxy , &file_ptr->inode )); |
---|
| 131 | uint32_t file_attr = hal_remote_lw (XPTR(file_cxy , &file_ptr->attr )); |
---|
| 132 | mapper_t * mapper_ptr = hal_remote_lpt(XPTR(file_cxy , &file_ptr->mapper)); |
---|
| 133 | |
---|
| 134 | // get file size |
---|
| 135 | uint32_t size = hal_remote_lw( XPTR( file_cxy , &inode_ptr->size ) ); |
---|
| 136 | |
---|
| 137 | // chek offset and length arguments |
---|
| 138 | if( (offset + length) > size) |
---|
[1] | 139 | { |
---|
[407] | 140 | printk("\n[ERROR] in %s: offset (%d) + len (%d) >= file's size (%d)\n", |
---|
| 141 | __FUNCTION__, k_attr.offset, k_attr.length, size ); |
---|
| 142 | this->errno = ERANGE; |
---|
| 143 | return -1; |
---|
[1] | 144 | } |
---|
| 145 | |
---|
[407] | 146 | // check access rights |
---|
| 147 | if( (prot_read && !(file_attr & FD_ATTR_READ_ENABLE)) || |
---|
| 148 | (prot_write && !(file_attr & FD_ATTR_WRITE_ENABLE)) ) |
---|
[1] | 149 | { |
---|
[407] | 150 | printk("\n[ERROR] in %s: prot = %x / file_attr = %x)\n", |
---|
| 151 | __FUNCTION__ , k_attr.prot , file_attr ); |
---|
| 152 | this->errno = EACCES; |
---|
| 153 | return -1; |
---|
[1] | 154 | } |
---|
| 155 | |
---|
[407] | 156 | // increment file refcount |
---|
| 157 | vfs_file_count_up( file_xp ); |
---|
[1] | 158 | |
---|
[407] | 159 | mapper_xp = XPTR( file_cxy , mapper_ptr ); |
---|
| 160 | vseg_type = VSEG_TYPE_FILE; |
---|
| 161 | vseg_cxy = file_cxy; |
---|
| 162 | } |
---|
| 163 | else // ANON or REMOTE |
---|
| 164 | { |
---|
| 165 | // no mapper for ANON or REMOTE |
---|
| 166 | mapper_xp = XPTR_NULL; |
---|
[1] | 167 | |
---|
[407] | 168 | if( map_anon ) |
---|
| 169 | { |
---|
| 170 | vseg_type = VSEG_TYPE_ANON; |
---|
| 171 | vseg_cxy = local_cxy; |
---|
| 172 | } |
---|
| 173 | else |
---|
| 174 | { |
---|
| 175 | vseg_type = VSEG_TYPE_REMOTE; |
---|
| 176 | vseg_cxy = k_attr.fdid; |
---|
| 177 | |
---|
| 178 | if( cluster_is_undefined( vseg_cxy ) ) |
---|
| 179 | { |
---|
| 180 | printk("\n[ERROR] in %s : illegal cxy for MAP_REMOTE\n", __FUNCTION__ ); |
---|
| 181 | this->errno = EINVAL; |
---|
| 182 | return -1; |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | } |
---|
[1] | 186 | |
---|
[407] | 187 | // get reference process cluster and local pointer |
---|
| 188 | xptr_t ref_xp = process->ref_xp; |
---|
| 189 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
| 190 | process_t * ref_ptr = (process_t *)GET_PTR( ref_xp ); |
---|
| 191 | |
---|
| 192 | // create the vseg in reference cluster |
---|
| 193 | if( local_cxy == ref_cxy ) |
---|
| 194 | { |
---|
| 195 | vseg = vmm_create_vseg( process, |
---|
| 196 | vseg_type, |
---|
| 197 | 0, // base address unused for mmap() |
---|
| 198 | length, |
---|
| 199 | offset, |
---|
| 200 | 0, // file_size unused for mmap() |
---|
| 201 | mapper_xp, |
---|
| 202 | vseg_cxy ); |
---|
| 203 | } |
---|
| 204 | else |
---|
| 205 | { |
---|
| 206 | rpc_vmm_create_vseg_client( ref_cxy, |
---|
| 207 | ref_ptr, |
---|
| 208 | vseg_type, |
---|
| 209 | 0, // base address unused for mmap() |
---|
| 210 | length, |
---|
| 211 | offset, |
---|
| 212 | 0, // file size unused for mmap() |
---|
| 213 | mapper_xp, |
---|
| 214 | vseg_cxy, |
---|
| 215 | &vseg ); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | if( vseg == NULL ) |
---|
| 219 | { |
---|
| 220 | printk("\n[ERROR] in %s : cannot create vseg\n", __FUNCTION__ ); |
---|
| 221 | this->errno = ENOMEM; |
---|
| 222 | return -1; |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | // copy vseg base address to user space |
---|
| 226 | hal_copy_to_uspace( &attr->addr , &vseg->min , sizeof(intptr_t) ); |
---|
| 227 | |
---|
| 228 | tm_end = hal_get_cycles(); |
---|
| 229 | |
---|
| 230 | syscall_dmsg("\n[DBG] %s : core[%x,%d] created vseg %s in cluster %x / cycle %d\n" |
---|
| 231 | " base = %x / length = %x / cost = %d\n", |
---|
| 232 | __FUNCTION__, local_cxy , this->core->lid , vseg_type_str(vseg->type) , |
---|
| 233 | vseg->cxy , (uint32_t)tm_start , vseg->min , length , (uint32_t)(tm_end - tm_start) ); |
---|
| 234 | |
---|
| 235 | return 0; |
---|
| 236 | |
---|
[23] | 237 | } // end sys_mmap() |
---|
[407] | 238 | |
---|