[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; |
---|
[637] | 43 | cxy_t vseg_cxy; // target cluster for the vseg |
---|
| 44 | vseg_type_t vseg_type; // vseg type |
---|
[407] | 45 | mmap_attr_t k_attr; // attributes copy in kernel space |
---|
| 46 | xptr_t mapper_xp; |
---|
[435] | 47 | reg_t save_sr; // required to enable IRQs |
---|
[1] | 48 | |
---|
[407] | 49 | thread_t * this = CURRENT_THREAD; |
---|
| 50 | process_t * process = this->process; |
---|
| 51 | |
---|
[594] | 52 | #if (DEBUG_SYS_MMAP || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
| 53 | uint64_t tm_start = hal_get_cycles(); |
---|
| 54 | #endif |
---|
| 55 | |
---|
[438] | 56 | #if DEBUG_SYS_MMAP |
---|
[623] | 57 | if( DEBUG_SYS_MMAP < tm_start ) |
---|
[594] | 58 | printk("\n[%s] thread[%x,%x] enter / cycle %d\n", |
---|
| 59 | __FUNCTION__, process->pid, this->trdid, (uint32_t)tm_start ); |
---|
[435] | 60 | #endif |
---|
| 61 | |
---|
[594] | 62 | // check user buffer (containing attributes) is mapped |
---|
[637] | 63 | if( vmm_get_vseg( process , (intptr_t)attr , &vseg ) ) |
---|
[407] | 64 | { |
---|
[435] | 65 | |
---|
[438] | 66 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 67 | printk("\n[ERROR] in %s : thread[%x,%x] / mmap attributes unmapped %x\n", |
---|
| 68 | __FUNCTION__ , process->pid, this->trdid, (intptr_t)attr ); |
---|
[435] | 69 | #endif |
---|
[407] | 70 | this->errno = EINVAL; |
---|
| 71 | return -1; |
---|
| 72 | } |
---|
| 73 | |
---|
[594] | 74 | // copy attributes from user space to kernel space |
---|
[637] | 75 | hal_copy_from_uspace( XPTR( local_cxy , &k_attr ), |
---|
[626] | 76 | attr, |
---|
| 77 | sizeof(mmap_attr_t) ); |
---|
[407] | 78 | |
---|
[594] | 79 | // get addr, fdid, offset, and length attributes |
---|
| 80 | uint32_t fdid = k_attr.fdid; |
---|
| 81 | uint32_t offset = k_attr.offset; |
---|
| 82 | uint32_t length = k_attr.length; |
---|
[407] | 83 | |
---|
| 84 | // get flags |
---|
| 85 | bool_t map_fixed = ( (k_attr.flags & MAP_FIXED) != 0 ); |
---|
| 86 | bool_t map_anon = ( (k_attr.flags & MAP_ANON) != 0 ); |
---|
| 87 | bool_t map_remote = ( (k_attr.flags & MAP_REMOTE) != 0 ); |
---|
| 88 | bool_t map_shared = ( (k_attr.flags & MAP_SHARED) != 0 ); |
---|
| 89 | bool_t map_private = ( (k_attr.flags & MAP_PRIVATE) != 0 ); |
---|
| 90 | |
---|
| 91 | // MAP_FIXED not supported |
---|
| 92 | if( map_fixed ) |
---|
| 93 | { |
---|
[435] | 94 | |
---|
[438] | 95 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 96 | printk("\n[ERROR] in %s : thread[%x,%x] / MAP_FIXED not supported\n", |
---|
| 97 | __FUNCTION__ , process->pid, this->trdid ); |
---|
[435] | 98 | #endif |
---|
[407] | 99 | this->errno = EINVAL; |
---|
| 100 | return -1; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | if( map_shared == map_private ) |
---|
| 104 | { |
---|
[435] | 105 | |
---|
[438] | 106 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 107 | printk("\n[ERROR] in %s : thread[%x,%x] / MAP_SHARED == MAP_PRIVATE\n", |
---|
| 108 | __FUNCTION__ , process->pid, this->trdid ); |
---|
[435] | 109 | #endif |
---|
[407] | 110 | this->errno = EINVAL; |
---|
| 111 | return -1; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | // FIXME handle Copy_On_Write for MAP_PRIVATE... |
---|
| 115 | |
---|
| 116 | // test mmap type : can be FILE / ANON / REMOTE |
---|
[637] | 117 | // to define vseg_type & vseg_cxy |
---|
[407] | 118 | |
---|
[611] | 119 | /////////////////////////////////////////////////////////// MAP_FILE |
---|
| 120 | if( (map_anon == false) && (map_remote == false) ) |
---|
[407] | 121 | { |
---|
[594] | 122 | |
---|
| 123 | #if (DEBUG_SYS_MMAP & 1) |
---|
| 124 | if ( DEBUG_SYS_MMAP < tm_start ) |
---|
[637] | 125 | printk("\n[%s] thread[%x,%x] type file : fdid %d / offset %x / %x bytes\n", |
---|
[594] | 126 | __FUNCTION__, process->pid, this->trdid, fdid, offset, length ); |
---|
| 127 | #endif |
---|
| 128 | |
---|
[637] | 129 | // FIXME: handle concurent delete of file by another thread |
---|
[407] | 130 | |
---|
| 131 | if( fdid >= CONFIG_PROCESS_FILE_MAX_NR ) |
---|
[1] | 132 | { |
---|
[435] | 133 | |
---|
[438] | 134 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 135 | printk("\n[ERROR] in %s : thread[%x,%x] / bad file descriptor %d\n", |
---|
| 136 | __FUNCTION__ , process->pid , this->trdid , fdid ); |
---|
[435] | 137 | #endif |
---|
[407] | 138 | this->errno = EBADFD; |
---|
| 139 | return -1; |
---|
| 140 | } |
---|
[1] | 141 | |
---|
[407] | 142 | // get extended pointer on file descriptor |
---|
| 143 | xptr_t file_xp = process_fd_get_xptr( process , fdid ); |
---|
| 144 | |
---|
| 145 | if( file_xp == XPTR_NULL ) |
---|
| 146 | { |
---|
[435] | 147 | |
---|
[438] | 148 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 149 | printk("\n[ERROR] in %s : thread[%x,%x] / file descriptor %d not found\n", |
---|
| 150 | __FUNCTION__ , this->trdid , process->pid , fdid ); |
---|
[435] | 151 | #endif |
---|
[407] | 152 | this->errno = EBADFD; |
---|
| 153 | return -1; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | // get file cluster and local pointer |
---|
| 157 | cxy_t file_cxy = GET_CXY( file_xp ); |
---|
| 158 | vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp ); |
---|
| 159 | |
---|
[594] | 160 | #if (DEBUG_SYS_MMAP & 1) |
---|
| 161 | if ( DEBUG_SYS_MMAP < tm_start ) |
---|
| 162 | printk("\n[%s] thread[%x,%x] get file pointer %x in cluster %x\n", |
---|
| 163 | __FUNCTION__, process->pid, this->trdid, file_ptr, file_cxy ); |
---|
| 164 | #endif |
---|
| 165 | |
---|
| 166 | // get inode pointer & mapper pointer |
---|
[407] | 167 | vfs_inode_t * inode_ptr = hal_remote_lpt(XPTR(file_cxy , &file_ptr->inode )); |
---|
| 168 | mapper_t * mapper_ptr = hal_remote_lpt(XPTR(file_cxy , &file_ptr->mapper)); |
---|
| 169 | |
---|
| 170 | // get file size |
---|
[566] | 171 | uint32_t size = hal_remote_l32( XPTR( file_cxy , &inode_ptr->size ) ); |
---|
[407] | 172 | |
---|
[594] | 173 | #if (DEBUG_SYS_MMAP & 1) |
---|
| 174 | if ( DEBUG_SYS_MMAP < tm_start ) |
---|
| 175 | printk("\n[%s] thread[%x,%x] get file size : %d bytes\n", |
---|
| 176 | __FUNCTION__, process->pid, this->trdid, size ); |
---|
| 177 | #endif |
---|
| 178 | |
---|
[407] | 179 | // chek offset and length arguments |
---|
| 180 | if( (offset + length) > size) |
---|
[1] | 181 | { |
---|
[435] | 182 | |
---|
[438] | 183 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 184 | printk("\n[ERROR] in %s: thread[%x,%x] / offset(%d) + len(%d) >= file's size(%d)\n", |
---|
| 185 | __FUNCTION__, process->pid, this->trdid, k_attr.offset, k_attr.length, size ); |
---|
[435] | 186 | #endif |
---|
[407] | 187 | this->errno = ERANGE; |
---|
| 188 | return -1; |
---|
[1] | 189 | } |
---|
| 190 | |
---|
[594] | 191 | /* TODO |
---|
| 192 | // chek access rigths |
---|
| 193 | uint32_t file_attr = hal_remote_l32(XPTR(file_cxy , &file_ptr->attr )); |
---|
| 194 | bool_t prot_read = ( (k_attr.prot & PROT_READ ) != 0 ); |
---|
| 195 | bool_t prot_write = ( (k_attr.prot & PROT_WRITE) != 0 ); |
---|
| 196 | |
---|
[407] | 197 | // check access rights |
---|
| 198 | if( (prot_read && !(file_attr & FD_ATTR_READ_ENABLE)) || |
---|
| 199 | (prot_write && !(file_attr & FD_ATTR_WRITE_ENABLE)) ) |
---|
[1] | 200 | { |
---|
[435] | 201 | |
---|
[438] | 202 | #if DEBUG_SYSCALLS_ERROR |
---|
[440] | 203 | printk("\n[ERROR] in %s: prot = %x / file_attr = %x / thread %x , process %x\n", |
---|
| 204 | __FUNCTION__ , k_attr.prot , file_attr , this->trdid , process->pid ); |
---|
[435] | 205 | #endif |
---|
[407] | 206 | this->errno = EACCES; |
---|
| 207 | return -1; |
---|
[1] | 208 | } |
---|
[594] | 209 | */ |
---|
[1] | 210 | |
---|
[407] | 211 | // increment file refcount |
---|
| 212 | vfs_file_count_up( file_xp ); |
---|
[1] | 213 | |
---|
[407] | 214 | mapper_xp = XPTR( file_cxy , mapper_ptr ); |
---|
| 215 | vseg_type = VSEG_TYPE_FILE; |
---|
| 216 | vseg_cxy = file_cxy; |
---|
| 217 | } |
---|
[611] | 218 | ///////////////////////////////////////////////////////// MAP_ANON |
---|
| 219 | else if ( map_anon ) |
---|
[407] | 220 | { |
---|
| 221 | mapper_xp = XPTR_NULL; |
---|
[594] | 222 | vseg_type = VSEG_TYPE_ANON; |
---|
| 223 | vseg_cxy = local_cxy; |
---|
[1] | 224 | |
---|
[594] | 225 | #if (DEBUG_SYS_MMAP & 1) |
---|
| 226 | if ( DEBUG_SYS_MMAP < tm_start ) |
---|
[637] | 227 | printk("\n[%s] thread[%x,%x] type anon / %x bytes / cluster %x\n", |
---|
[594] | 228 | __FUNCTION__, process->pid, this->trdid, length, vseg_cxy ); |
---|
| 229 | #endif |
---|
| 230 | |
---|
| 231 | } |
---|
[611] | 232 | /////////////////////////////////////////////////////// MAP_REMOTE |
---|
| 233 | else |
---|
[594] | 234 | { |
---|
| 235 | mapper_xp = XPTR_NULL; |
---|
| 236 | vseg_type = VSEG_TYPE_REMOTE; |
---|
| 237 | vseg_cxy = k_attr.fdid; |
---|
| 238 | |
---|
| 239 | #if (DEBUG_SYS_MMAP & 1) |
---|
| 240 | if ( DEBUG_SYS_MMAP < tm_start ) |
---|
[637] | 241 | printk("\n[%s] thread[%x,%x] type remote / %x bytes / target cluster %x\n", |
---|
[594] | 242 | __FUNCTION__, process->pid, this->trdid, length, vseg_cxy ); |
---|
| 243 | #endif |
---|
| 244 | |
---|
[637] | 245 | if( cluster_is_active( vseg_cxy ) == false ) |
---|
[407] | 246 | { |
---|
[435] | 247 | |
---|
[438] | 248 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 249 | printk("\n[ERROR] in %s : thread[%x,%x] / illegal cxy %x for REMOTE\n", |
---|
| 250 | __FUNCTION__, this->trdid , process->pid, vseg_cxy ); |
---|
[435] | 251 | #endif |
---|
[594] | 252 | this->errno = EINVAL; |
---|
| 253 | return -1; |
---|
[407] | 254 | } |
---|
| 255 | } |
---|
[1] | 256 | |
---|
[435] | 257 | // enable IRQs |
---|
| 258 | hal_enable_irq( &save_sr ); |
---|
| 259 | |
---|
[407] | 260 | // get reference process cluster and local pointer |
---|
| 261 | xptr_t ref_xp = process->ref_xp; |
---|
| 262 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
[594] | 263 | process_t * ref_ptr = GET_PTR( ref_xp ); |
---|
[407] | 264 | |
---|
[637] | 265 | // register vseg in reference VSL |
---|
[407] | 266 | if( local_cxy == ref_cxy ) |
---|
| 267 | { |
---|
| 268 | vseg = vmm_create_vseg( process, |
---|
| 269 | vseg_type, |
---|
[594] | 270 | 0, // vseg base (unused for mmap) |
---|
| 271 | length, // vseg size |
---|
| 272 | offset, // file offset |
---|
| 273 | 0, // file_size (unused for mmap) |
---|
[407] | 274 | mapper_xp, |
---|
| 275 | vseg_cxy ); |
---|
| 276 | } |
---|
| 277 | else |
---|
| 278 | { |
---|
| 279 | rpc_vmm_create_vseg_client( ref_cxy, |
---|
| 280 | ref_ptr, |
---|
| 281 | vseg_type, |
---|
[594] | 282 | 0, // vseg base (unused for mmap) |
---|
| 283 | length, // vseg size |
---|
| 284 | offset, // file offset |
---|
| 285 | 0, // file size (unused for mmap) |
---|
[407] | 286 | mapper_xp, |
---|
| 287 | vseg_cxy, |
---|
| 288 | &vseg ); |
---|
| 289 | } |
---|
| 290 | |
---|
[435] | 291 | // restore IRQs |
---|
| 292 | hal_restore_irq( save_sr ); |
---|
| 293 | |
---|
[407] | 294 | if( vseg == NULL ) |
---|
| 295 | { |
---|
[435] | 296 | |
---|
[438] | 297 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 298 | printk("\n[ERROR] in %s : thread[%x,%x] / cannot create vseg\n", |
---|
| 299 | __FUNCTION__, process->pid, this->trdid ); |
---|
[435] | 300 | #endif |
---|
[407] | 301 | this->errno = ENOMEM; |
---|
| 302 | return -1; |
---|
| 303 | } |
---|
| 304 | |
---|
[637] | 305 | // copy vseg base address to user space mmap_attr_t |
---|
| 306 | hal_copy_to_uspace( &attr->addr, |
---|
| 307 | XPTR( ref_cxy , &vseg->min ), |
---|
[626] | 308 | sizeof(intptr_t) ); |
---|
[435] | 309 | hal_fence(); |
---|
[407] | 310 | |
---|
[594] | 311 | #if (DEBUG_SYS_MMAP || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
| 312 | uint64_t tm_end = hal_get_cycles(); |
---|
| 313 | #endif |
---|
| 314 | |
---|
[623] | 315 | #if CONFIG_INSTRUMENTATION_SYSCALLS |
---|
| 316 | hal_atomic_add( &syscalls_cumul_cost[SYS_MMAP] , tm_end - tm_start ); |
---|
| 317 | hal_atomic_add( &syscalls_occurences[SYS_MMAP] , 1 ); |
---|
| 318 | #endif |
---|
| 319 | |
---|
[438] | 320 | #if DEBUG_SYS_MMAP |
---|
[623] | 321 | if ( DEBUG_SYS_MMAP < tm_end ) |
---|
[637] | 322 | printk("\n[%s] thread[%x,%x] exit / %s / cxy %x / base %x / size %x / cycle %d\n", |
---|
[594] | 323 | __FUNCTION__, process->pid, this->trdid, |
---|
| 324 | vseg_type_str(vseg->type), vseg->cxy, vseg->min, length, (uint32_t)tm_end ); |
---|
[435] | 325 | #endif |
---|
[407] | 326 | |
---|
| 327 | return 0; |
---|
| 328 | |
---|
[23] | 329 | } // end sys_mmap() |
---|
[407] | 330 | |
---|