[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 | |
---|
[611] | 121 | /////////////////////////////////////////////////////////// MAP_FILE |
---|
| 122 | if( (map_anon == false) && (map_remote == false) ) |
---|
[407] | 123 | { |
---|
[594] | 124 | |
---|
| 125 | #if (DEBUG_SYS_MMAP & 1) |
---|
| 126 | if ( DEBUG_SYS_MMAP < tm_start ) |
---|
| 127 | printk("\n[%s] thread[%x,%x] map file : fdid %d / offset %d / %d bytes\n", |
---|
| 128 | __FUNCTION__, process->pid, this->trdid, fdid, offset, length ); |
---|
| 129 | #endif |
---|
| 130 | |
---|
[407] | 131 | // FIXME: handle concurent delete of file by another thread closing it |
---|
| 132 | |
---|
| 133 | if( fdid >= CONFIG_PROCESS_FILE_MAX_NR ) |
---|
[1] | 134 | { |
---|
[435] | 135 | |
---|
[438] | 136 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 137 | printk("\n[ERROR] in %s : thread[%x,%x] / bad file descriptor %d\n", |
---|
| 138 | __FUNCTION__ , process->pid , this->trdid , fdid ); |
---|
[435] | 139 | #endif |
---|
[407] | 140 | this->errno = EBADFD; |
---|
| 141 | return -1; |
---|
| 142 | } |
---|
[1] | 143 | |
---|
[407] | 144 | // get extended pointer on file descriptor |
---|
| 145 | xptr_t file_xp = process_fd_get_xptr( process , fdid ); |
---|
| 146 | |
---|
| 147 | if( file_xp == XPTR_NULL ) |
---|
| 148 | { |
---|
[435] | 149 | |
---|
[438] | 150 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 151 | printk("\n[ERROR] in %s : thread[%x,%x] / file descriptor %d not found\n", |
---|
| 152 | __FUNCTION__ , this->trdid , process->pid , fdid ); |
---|
[435] | 153 | #endif |
---|
[407] | 154 | this->errno = EBADFD; |
---|
| 155 | return -1; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | // get file cluster and local pointer |
---|
| 159 | cxy_t file_cxy = GET_CXY( file_xp ); |
---|
| 160 | vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp ); |
---|
| 161 | |
---|
[594] | 162 | #if (DEBUG_SYS_MMAP & 1) |
---|
| 163 | if ( DEBUG_SYS_MMAP < tm_start ) |
---|
| 164 | printk("\n[%s] thread[%x,%x] get file pointer %x in cluster %x\n", |
---|
| 165 | __FUNCTION__, process->pid, this->trdid, file_ptr, file_cxy ); |
---|
| 166 | #endif |
---|
| 167 | |
---|
| 168 | // get inode pointer & mapper pointer |
---|
[407] | 169 | vfs_inode_t * inode_ptr = hal_remote_lpt(XPTR(file_cxy , &file_ptr->inode )); |
---|
| 170 | mapper_t * mapper_ptr = hal_remote_lpt(XPTR(file_cxy , &file_ptr->mapper)); |
---|
| 171 | |
---|
| 172 | // get file size |
---|
[566] | 173 | uint32_t size = hal_remote_l32( XPTR( file_cxy , &inode_ptr->size ) ); |
---|
[407] | 174 | |
---|
[594] | 175 | #if (DEBUG_SYS_MMAP & 1) |
---|
| 176 | if ( DEBUG_SYS_MMAP < tm_start ) |
---|
| 177 | printk("\n[%s] thread[%x,%x] get file size : %d bytes\n", |
---|
| 178 | __FUNCTION__, process->pid, this->trdid, size ); |
---|
| 179 | #endif |
---|
| 180 | |
---|
[407] | 181 | // chek offset and length arguments |
---|
| 182 | if( (offset + length) > size) |
---|
[1] | 183 | { |
---|
[435] | 184 | |
---|
[438] | 185 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 186 | printk("\n[ERROR] in %s: thread[%x,%x] / offset(%d) + len(%d) >= file's size(%d)\n", |
---|
| 187 | __FUNCTION__, process->pid, this->trdid, k_attr.offset, k_attr.length, size ); |
---|
[435] | 188 | #endif |
---|
[407] | 189 | this->errno = ERANGE; |
---|
| 190 | return -1; |
---|
[1] | 191 | } |
---|
| 192 | |
---|
[594] | 193 | /* TODO |
---|
| 194 | // chek access rigths |
---|
| 195 | uint32_t file_attr = hal_remote_l32(XPTR(file_cxy , &file_ptr->attr )); |
---|
| 196 | bool_t prot_read = ( (k_attr.prot & PROT_READ ) != 0 ); |
---|
| 197 | bool_t prot_write = ( (k_attr.prot & PROT_WRITE) != 0 ); |
---|
| 198 | |
---|
[407] | 199 | // check access rights |
---|
| 200 | if( (prot_read && !(file_attr & FD_ATTR_READ_ENABLE)) || |
---|
| 201 | (prot_write && !(file_attr & FD_ATTR_WRITE_ENABLE)) ) |
---|
[1] | 202 | { |
---|
[435] | 203 | |
---|
[438] | 204 | #if DEBUG_SYSCALLS_ERROR |
---|
[440] | 205 | printk("\n[ERROR] in %s: prot = %x / file_attr = %x / thread %x , process %x\n", |
---|
| 206 | __FUNCTION__ , k_attr.prot , file_attr , this->trdid , process->pid ); |
---|
[435] | 207 | #endif |
---|
[407] | 208 | this->errno = EACCES; |
---|
| 209 | return -1; |
---|
[1] | 210 | } |
---|
[594] | 211 | */ |
---|
[1] | 212 | |
---|
[407] | 213 | // increment file refcount |
---|
| 214 | vfs_file_count_up( file_xp ); |
---|
[1] | 215 | |
---|
[407] | 216 | mapper_xp = XPTR( file_cxy , mapper_ptr ); |
---|
| 217 | vseg_type = VSEG_TYPE_FILE; |
---|
| 218 | vseg_cxy = file_cxy; |
---|
| 219 | } |
---|
[611] | 220 | ///////////////////////////////////////////////////////// MAP_ANON |
---|
| 221 | else if ( map_anon ) |
---|
[407] | 222 | { |
---|
| 223 | mapper_xp = XPTR_NULL; |
---|
[594] | 224 | vseg_type = VSEG_TYPE_ANON; |
---|
| 225 | vseg_cxy = local_cxy; |
---|
[1] | 226 | |
---|
[594] | 227 | #if (DEBUG_SYS_MMAP & 1) |
---|
| 228 | if ( DEBUG_SYS_MMAP < tm_start ) |
---|
| 229 | printk("\n[%s] thread[%x,%x] map anon / %d bytes / cluster %x\n", |
---|
| 230 | __FUNCTION__, process->pid, this->trdid, length, vseg_cxy ); |
---|
| 231 | #endif |
---|
| 232 | |
---|
| 233 | } |
---|
[611] | 234 | /////////////////////////////////////////////////////// MAP_REMOTE |
---|
| 235 | else |
---|
[594] | 236 | { |
---|
| 237 | mapper_xp = XPTR_NULL; |
---|
| 238 | vseg_type = VSEG_TYPE_REMOTE; |
---|
| 239 | vseg_cxy = k_attr.fdid; |
---|
| 240 | |
---|
| 241 | #if (DEBUG_SYS_MMAP & 1) |
---|
| 242 | if ( DEBUG_SYS_MMAP < tm_start ) |
---|
| 243 | printk("\n[%s] thread[%x,%x] map remote / %d bytes / cluster %x\n", |
---|
| 244 | __FUNCTION__, process->pid, this->trdid, length, vseg_cxy ); |
---|
| 245 | #endif |
---|
| 246 | |
---|
| 247 | if( cluster_is_undefined( vseg_cxy ) ) |
---|
[407] | 248 | { |
---|
[435] | 249 | |
---|
[438] | 250 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 251 | printk("\n[ERROR] in %s : thread[%x,%x] / illegal cxy %x for REMOTE\n", |
---|
| 252 | __FUNCTION__, this->trdid , process->pid, vseg_cxy ); |
---|
[435] | 253 | #endif |
---|
[594] | 254 | this->errno = EINVAL; |
---|
| 255 | return -1; |
---|
[407] | 256 | } |
---|
| 257 | } |
---|
[1] | 258 | |
---|
[435] | 259 | // enable IRQs |
---|
| 260 | hal_enable_irq( &save_sr ); |
---|
| 261 | |
---|
[407] | 262 | // get reference process cluster and local pointer |
---|
| 263 | xptr_t ref_xp = process->ref_xp; |
---|
| 264 | cxy_t ref_cxy = GET_CXY( ref_xp ); |
---|
[594] | 265 | process_t * ref_ptr = GET_PTR( ref_xp ); |
---|
[407] | 266 | |
---|
| 267 | // create the vseg in reference cluster |
---|
| 268 | if( local_cxy == ref_cxy ) |
---|
| 269 | { |
---|
| 270 | vseg = vmm_create_vseg( process, |
---|
| 271 | vseg_type, |
---|
[594] | 272 | 0, // vseg base (unused for mmap) |
---|
| 273 | length, // vseg size |
---|
| 274 | offset, // file offset |
---|
| 275 | 0, // file_size (unused for mmap) |
---|
[407] | 276 | mapper_xp, |
---|
| 277 | vseg_cxy ); |
---|
| 278 | } |
---|
| 279 | else |
---|
| 280 | { |
---|
| 281 | rpc_vmm_create_vseg_client( ref_cxy, |
---|
| 282 | ref_ptr, |
---|
| 283 | vseg_type, |
---|
[594] | 284 | 0, // vseg base (unused for mmap) |
---|
| 285 | length, // vseg size |
---|
| 286 | offset, // file offset |
---|
| 287 | 0, // file size (unused for mmap) |
---|
[407] | 288 | mapper_xp, |
---|
| 289 | vseg_cxy, |
---|
| 290 | &vseg ); |
---|
| 291 | } |
---|
| 292 | |
---|
[435] | 293 | // restore IRQs |
---|
| 294 | hal_restore_irq( save_sr ); |
---|
| 295 | |
---|
[407] | 296 | if( vseg == NULL ) |
---|
| 297 | { |
---|
[435] | 298 | |
---|
[438] | 299 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 300 | printk("\n[ERROR] in %s : thread[%x,%x] / cannot create vseg\n", |
---|
| 301 | __FUNCTION__, process->pid, this->trdid ); |
---|
[435] | 302 | #endif |
---|
[407] | 303 | this->errno = ENOMEM; |
---|
| 304 | return -1; |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | // copy vseg base address to user space |
---|
| 308 | hal_copy_to_uspace( &attr->addr , &vseg->min , sizeof(intptr_t) ); |
---|
| 309 | |
---|
[435] | 310 | hal_fence(); |
---|
[407] | 311 | |
---|
[594] | 312 | #if (DEBUG_SYS_MMAP || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
| 313 | uint64_t tm_end = hal_get_cycles(); |
---|
| 314 | #endif |
---|
| 315 | |
---|
[438] | 316 | #if DEBUG_SYS_MMAP |
---|
| 317 | if ( DEBUG_SYS_MMAP < tm_start ) |
---|
[594] | 318 | printk("\n[%s] thread[%x,%x] exit / %s / cxy %x / base %x / size %d / cycle %d\n", |
---|
| 319 | __FUNCTION__, process->pid, this->trdid, |
---|
| 320 | vseg_type_str(vseg->type), vseg->cxy, vseg->min, length, (uint32_t)tm_end ); |
---|
[435] | 321 | #endif |
---|
[407] | 322 | |
---|
| 323 | return 0; |
---|
| 324 | |
---|
[23] | 325 | } // end sys_mmap() |
---|
[407] | 326 | |
---|