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