[1] | 1 | /* |
---|
[23] | 2 | * sys_mmap.c - map files, memory or devices into process virtual address space |
---|
[1] | 3 | * |
---|
[625] | 4 | * Authors Alain Greiner (2016,2017,2018,2019) |
---|
[1] | 5 | * |
---|
[23] | 6 | * Copyright (c) UPMC Sorbonne Universites |
---|
[1] | 7 | * |
---|
[23] | 8 | * This file is part of ALMOS-MKH. |
---|
| 9 | * |
---|
| 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
[1] | 11 | * under the terms of the GNU General Public License as published by |
---|
| 12 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 13 | * |
---|
[23] | 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
[1] | 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 17 | * General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
[23] | 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
[1] | 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 22 | */ |
---|
| 23 | |
---|
[457] | 24 | #include <hal_kernel_types.h> |
---|
[407] | 25 | #include <hal_uspace.h> |
---|
[625] | 26 | #include <hal_vmm.h> |
---|
[435] | 27 | #include <hal_irqmask.h> |
---|
[407] | 28 | #include <shared_syscalls.h> |
---|
[1] | 29 | #include <errno.h> |
---|
| 30 | #include <thread.h> |
---|
[23] | 31 | #include <printk.h> |
---|
[407] | 32 | #include <mapper.h> |
---|
[1] | 33 | #include <vfs.h> |
---|
| 34 | #include <process.h> |
---|
| 35 | #include <vmm.h> |
---|
| 36 | |
---|
[506] | 37 | #include <syscalls.h> |
---|
| 38 | |
---|
[407] | 39 | ////////////////////////////////// |
---|
[23] | 40 | int sys_mmap( mmap_attr_t * attr ) |
---|
[1] | 41 | { |
---|
[407] | 42 | vseg_t * vseg; |
---|
| 43 | cxy_t vseg_cxy; |
---|
| 44 | vseg_type_t vseg_type; |
---|
| 45 | mmap_attr_t k_attr; // attributes copy in kernel space |
---|
| 46 | xptr_t mapper_xp; |
---|
| 47 | error_t error; |
---|
[435] | 48 | reg_t save_sr; // required to enable IRQs |
---|
[1] | 49 | |
---|
[407] | 50 | thread_t * this = CURRENT_THREAD; |
---|
| 51 | process_t * process = this->process; |
---|
| 52 | |
---|
[594] | 53 | #if (DEBUG_SYS_MMAP || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
| 54 | uint64_t tm_start = hal_get_cycles(); |
---|
| 55 | #endif |
---|
| 56 | |
---|
[438] | 57 | #if DEBUG_SYS_MMAP |
---|
[623] | 58 | if( DEBUG_SYS_MMAP < tm_start ) |
---|
[594] | 59 | printk("\n[%s] thread[%x,%x] enter / cycle %d\n", |
---|
| 60 | __FUNCTION__, process->pid, this->trdid, (uint32_t)tm_start ); |
---|
[435] | 61 | #endif |
---|
| 62 | |
---|
[594] | 63 | // check user buffer (containing attributes) is mapped |
---|
[440] | 64 | error = vmm_get_vseg( process , (intptr_t)attr , &vseg ); |
---|
[407] | 65 | |
---|
[594] | 66 | if( error ) |
---|
[407] | 67 | { |
---|
[435] | 68 | |
---|
[438] | 69 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 70 | printk("\n[ERROR] in %s : thread[%x,%x] / mmap attributes unmapped %x\n", |
---|
| 71 | __FUNCTION__ , process->pid, this->trdid, (intptr_t)attr ); |
---|
[624] | 72 | hal_vmm_display( process , false ); |
---|
[435] | 73 | #endif |
---|
[407] | 74 | this->errno = EINVAL; |
---|
| 75 | return -1; |
---|
| 76 | } |
---|
| 77 | |
---|
[594] | 78 | // copy attributes from user space to kernel space |
---|
[626] | 79 | hal_copy_from_uspace( local_cxy, |
---|
| 80 | &k_attr, |
---|
| 81 | attr, |
---|
| 82 | sizeof(mmap_attr_t) ); |
---|
[407] | 83 | |
---|
[594] | 84 | // get addr, fdid, offset, and length attributes |
---|
| 85 | uint32_t fdid = k_attr.fdid; |
---|
| 86 | uint32_t offset = k_attr.offset; |
---|
| 87 | uint32_t length = k_attr.length; |
---|
[407] | 88 | |
---|
| 89 | // get flags |
---|
| 90 | bool_t map_fixed = ( (k_attr.flags & MAP_FIXED) != 0 ); |
---|
| 91 | bool_t map_anon = ( (k_attr.flags & MAP_ANON) != 0 ); |
---|
| 92 | bool_t map_remote = ( (k_attr.flags & MAP_REMOTE) != 0 ); |
---|
| 93 | bool_t map_shared = ( (k_attr.flags & MAP_SHARED) != 0 ); |
---|
| 94 | bool_t map_private = ( (k_attr.flags & MAP_PRIVATE) != 0 ); |
---|
| 95 | |
---|
| 96 | // MAP_FIXED not supported |
---|
| 97 | if( map_fixed ) |
---|
| 98 | { |
---|
[435] | 99 | |
---|
[438] | 100 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 101 | printk("\n[ERROR] in %s : thread[%x,%x] / MAP_FIXED not supported\n", |
---|
| 102 | __FUNCTION__ , process->pid, this->trdid ); |
---|
[435] | 103 | #endif |
---|
[407] | 104 | this->errno = EINVAL; |
---|
| 105 | return -1; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | if( map_shared == map_private ) |
---|
| 109 | { |
---|
[435] | 110 | |
---|
[438] | 111 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 112 | printk("\n[ERROR] in %s : thread[%x,%x] / MAP_SHARED == MAP_PRIVATE\n", |
---|
| 113 | __FUNCTION__ , process->pid, this->trdid ); |
---|
[435] | 114 | #endif |
---|
[407] | 115 | this->errno = EINVAL; |
---|
| 116 | return -1; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | // FIXME handle Copy_On_Write for MAP_PRIVATE... |
---|
| 120 | |
---|
| 121 | // test mmap type : can be FILE / ANON / REMOTE |
---|
| 122 | |
---|
[611] | 123 | /////////////////////////////////////////////////////////// MAP_FILE |
---|
| 124 | if( (map_anon == false) && (map_remote == false) ) |
---|
[407] | 125 | { |
---|
[594] | 126 | |
---|
| 127 | #if (DEBUG_SYS_MMAP & 1) |
---|
| 128 | if ( DEBUG_SYS_MMAP < tm_start ) |
---|
| 129 | printk("\n[%s] thread[%x,%x] map file : fdid %d / offset %d / %d bytes\n", |
---|
| 130 | __FUNCTION__, process->pid, this->trdid, fdid, offset, length ); |
---|
| 131 | #endif |
---|
| 132 | |
---|
[407] | 133 | // FIXME: handle concurent delete of file by another thread closing it |
---|
| 134 | |
---|
| 135 | if( fdid >= CONFIG_PROCESS_FILE_MAX_NR ) |
---|
[1] | 136 | { |
---|
[435] | 137 | |
---|
[438] | 138 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 139 | printk("\n[ERROR] in %s : thread[%x,%x] / bad file descriptor %d\n", |
---|
| 140 | __FUNCTION__ , process->pid , this->trdid , fdid ); |
---|
[435] | 141 | #endif |
---|
[407] | 142 | this->errno = EBADFD; |
---|
| 143 | return -1; |
---|
| 144 | } |
---|
[1] | 145 | |
---|
[407] | 146 | // get extended pointer on file descriptor |
---|
| 147 | xptr_t file_xp = process_fd_get_xptr( process , fdid ); |
---|
| 148 | |
---|
| 149 | if( file_xp == XPTR_NULL ) |
---|
| 150 | { |
---|
[435] | 151 | |
---|
[438] | 152 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 153 | printk("\n[ERROR] in %s : thread[%x,%x] / file descriptor %d not found\n", |
---|
| 154 | __FUNCTION__ , this->trdid , process->pid , fdid ); |
---|
[435] | 155 | #endif |
---|
[407] | 156 | this->errno = EBADFD; |
---|
| 157 | return -1; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | // get file cluster and local pointer |
---|
| 161 | cxy_t file_cxy = GET_CXY( file_xp ); |
---|
| 162 | vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp ); |
---|
| 163 | |
---|
[594] | 164 | #if (DEBUG_SYS_MMAP & 1) |
---|
| 165 | if ( DEBUG_SYS_MMAP < tm_start ) |
---|
| 166 | printk("\n[%s] thread[%x,%x] get file pointer %x in cluster %x\n", |
---|
| 167 | __FUNCTION__, process->pid, this->trdid, file_ptr, file_cxy ); |
---|
| 168 | #endif |
---|
| 169 | |
---|
| 170 | // get inode pointer & mapper pointer |
---|
[407] | 171 | vfs_inode_t * inode_ptr = hal_remote_lpt(XPTR(file_cxy , &file_ptr->inode )); |
---|
| 172 | mapper_t * mapper_ptr = hal_remote_lpt(XPTR(file_cxy , &file_ptr->mapper)); |
---|
| 173 | |
---|
| 174 | // get file size |
---|
[566] | 175 | uint32_t size = hal_remote_l32( XPTR( file_cxy , &inode_ptr->size ) ); |
---|
[407] | 176 | |
---|
[594] | 177 | #if (DEBUG_SYS_MMAP & 1) |
---|
| 178 | if ( DEBUG_SYS_MMAP < tm_start ) |
---|
| 179 | printk("\n[%s] thread[%x,%x] get file size : %d bytes\n", |
---|
| 180 | __FUNCTION__, process->pid, this->trdid, size ); |
---|
| 181 | #endif |
---|
| 182 | |
---|
[407] | 183 | // chek offset and length arguments |
---|
| 184 | if( (offset + length) > size) |
---|
[1] | 185 | { |
---|
[435] | 186 | |
---|
[438] | 187 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 188 | printk("\n[ERROR] in %s: thread[%x,%x] / offset(%d) + len(%d) >= file's size(%d)\n", |
---|
| 189 | __FUNCTION__, process->pid, this->trdid, k_attr.offset, k_attr.length, size ); |
---|
[435] | 190 | #endif |
---|
[407] | 191 | this->errno = ERANGE; |
---|
| 192 | return -1; |
---|
[1] | 193 | } |
---|
| 194 | |
---|
[594] | 195 | /* TODO |
---|
| 196 | // chek access rigths |
---|
| 197 | uint32_t file_attr = hal_remote_l32(XPTR(file_cxy , &file_ptr->attr )); |
---|
| 198 | bool_t prot_read = ( (k_attr.prot & PROT_READ ) != 0 ); |
---|
| 199 | bool_t prot_write = ( (k_attr.prot & PROT_WRITE) != 0 ); |
---|
| 200 | |
---|
[407] | 201 | // check access rights |
---|
| 202 | if( (prot_read && !(file_attr & FD_ATTR_READ_ENABLE)) || |
---|
| 203 | (prot_write && !(file_attr & FD_ATTR_WRITE_ENABLE)) ) |
---|
[1] | 204 | { |
---|
[435] | 205 | |
---|
[438] | 206 | #if DEBUG_SYSCALLS_ERROR |
---|
[440] | 207 | printk("\n[ERROR] in %s: prot = %x / file_attr = %x / thread %x , process %x\n", |
---|
| 208 | __FUNCTION__ , k_attr.prot , file_attr , this->trdid , process->pid ); |
---|
[435] | 209 | #endif |
---|
[407] | 210 | this->errno = EACCES; |
---|
| 211 | return -1; |
---|
[1] | 212 | } |
---|
[594] | 213 | */ |
---|
[1] | 214 | |
---|
[407] | 215 | // increment file refcount |
---|
| 216 | vfs_file_count_up( file_xp ); |
---|
[1] | 217 | |
---|
[407] | 218 | mapper_xp = XPTR( file_cxy , mapper_ptr ); |
---|
| 219 | vseg_type = VSEG_TYPE_FILE; |
---|
| 220 | vseg_cxy = file_cxy; |
---|
| 221 | } |
---|
[611] | 222 | ///////////////////////////////////////////////////////// MAP_ANON |
---|
| 223 | else if ( map_anon ) |
---|
[407] | 224 | { |
---|
| 225 | mapper_xp = XPTR_NULL; |
---|
[594] | 226 | vseg_type = VSEG_TYPE_ANON; |
---|
| 227 | vseg_cxy = local_cxy; |
---|
[1] | 228 | |
---|
[594] | 229 | #if (DEBUG_SYS_MMAP & 1) |
---|
| 230 | if ( DEBUG_SYS_MMAP < tm_start ) |
---|
| 231 | printk("\n[%s] thread[%x,%x] map anon / %d bytes / cluster %x\n", |
---|
| 232 | __FUNCTION__, process->pid, this->trdid, length, vseg_cxy ); |
---|
| 233 | #endif |
---|
| 234 | |
---|
| 235 | } |
---|
[611] | 236 | /////////////////////////////////////////////////////// MAP_REMOTE |
---|
| 237 | else |
---|
[594] | 238 | { |
---|
| 239 | mapper_xp = XPTR_NULL; |
---|
| 240 | vseg_type = VSEG_TYPE_REMOTE; |
---|
| 241 | vseg_cxy = k_attr.fdid; |
---|
| 242 | |
---|
| 243 | #if (DEBUG_SYS_MMAP & 1) |
---|
| 244 | if ( DEBUG_SYS_MMAP < tm_start ) |
---|
| 245 | printk("\n[%s] thread[%x,%x] map remote / %d bytes / cluster %x\n", |
---|
| 246 | __FUNCTION__, process->pid, this->trdid, length, vseg_cxy ); |
---|
| 247 | #endif |
---|
| 248 | |
---|
| 249 | if( cluster_is_undefined( vseg_cxy ) ) |
---|
[407] | 250 | { |
---|
[435] | 251 | |
---|
[438] | 252 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 253 | printk("\n[ERROR] in %s : thread[%x,%x] / illegal cxy %x for REMOTE\n", |
---|
| 254 | __FUNCTION__, this->trdid , process->pid, vseg_cxy ); |
---|
[435] | 255 | #endif |
---|
[594] | 256 | this->errno = EINVAL; |
---|
| 257 | return -1; |
---|
[407] | 258 | } |
---|
| 259 | } |
---|
[1] | 260 | |
---|
[435] | 261 | // enable IRQs |
---|
| 262 | hal_enable_irq( &save_sr ); |
---|
| 263 | |
---|
[407] | 264 | // get reference process cluster and local pointer |
---|
| 265 | xptr_t ref_xp = process->ref_xp; |
---|
| 266 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
[594] | 267 | process_t * ref_ptr = GET_PTR( ref_xp ); |
---|
[407] | 268 | |
---|
| 269 | // create the vseg in reference cluster |
---|
| 270 | if( local_cxy == ref_cxy ) |
---|
| 271 | { |
---|
| 272 | vseg = vmm_create_vseg( process, |
---|
| 273 | vseg_type, |
---|
[594] | 274 | 0, // vseg base (unused for mmap) |
---|
| 275 | length, // vseg size |
---|
| 276 | offset, // file offset |
---|
| 277 | 0, // file_size (unused for mmap) |
---|
[407] | 278 | mapper_xp, |
---|
| 279 | vseg_cxy ); |
---|
| 280 | } |
---|
| 281 | else |
---|
| 282 | { |
---|
| 283 | rpc_vmm_create_vseg_client( ref_cxy, |
---|
| 284 | ref_ptr, |
---|
| 285 | vseg_type, |
---|
[594] | 286 | 0, // vseg base (unused for mmap) |
---|
| 287 | length, // vseg size |
---|
| 288 | offset, // file offset |
---|
| 289 | 0, // file size (unused for mmap) |
---|
[407] | 290 | mapper_xp, |
---|
| 291 | vseg_cxy, |
---|
| 292 | &vseg ); |
---|
| 293 | } |
---|
| 294 | |
---|
[435] | 295 | // restore IRQs |
---|
| 296 | hal_restore_irq( save_sr ); |
---|
| 297 | |
---|
[407] | 298 | if( vseg == NULL ) |
---|
| 299 | { |
---|
[435] | 300 | |
---|
[438] | 301 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 302 | printk("\n[ERROR] in %s : thread[%x,%x] / cannot create vseg\n", |
---|
| 303 | __FUNCTION__, process->pid, this->trdid ); |
---|
[435] | 304 | #endif |
---|
[407] | 305 | this->errno = ENOMEM; |
---|
| 306 | return -1; |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | // copy vseg base address to user space |
---|
[626] | 310 | hal_copy_to_uspace( local_cxy, |
---|
| 311 | &vseg->min, |
---|
| 312 | &attr->addr, |
---|
| 313 | sizeof(intptr_t) ); |
---|
[435] | 314 | hal_fence(); |
---|
[407] | 315 | |
---|
[594] | 316 | #if (DEBUG_SYS_MMAP || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
| 317 | uint64_t tm_end = hal_get_cycles(); |
---|
| 318 | #endif |
---|
| 319 | |
---|
[623] | 320 | #if CONFIG_INSTRUMENTATION_SYSCALLS |
---|
| 321 | hal_atomic_add( &syscalls_cumul_cost[SYS_MMAP] , tm_end - tm_start ); |
---|
| 322 | hal_atomic_add( &syscalls_occurences[SYS_MMAP] , 1 ); |
---|
| 323 | #endif |
---|
| 324 | |
---|
[438] | 325 | #if DEBUG_SYS_MMAP |
---|
[623] | 326 | if ( DEBUG_SYS_MMAP < tm_end ) |
---|
[594] | 327 | printk("\n[%s] thread[%x,%x] exit / %s / cxy %x / base %x / size %d / cycle %d\n", |
---|
| 328 | __FUNCTION__, process->pid, this->trdid, |
---|
| 329 | vseg_type_str(vseg->type), vseg->cxy, vseg->min, length, (uint32_t)tm_end ); |
---|
[435] | 330 | #endif |
---|
[407] | 331 | |
---|
| 332 | return 0; |
---|
| 333 | |
---|
[23] | 334 | } // end sys_mmap() |
---|
[407] | 335 | |
---|