[1] | 1 | /* |
---|
[23] | 2 | * sys_exec.c - Kernel function implementing the "exec" system call. |
---|
[302] | 3 | * |
---|
[670] | 4 | * Authors Alain Greiner (2016,2017,2017,2019,2020) |
---|
[1] | 5 | * |
---|
| 6 | * Copyright (c) UPMC Sorbonne Universites |
---|
| 7 | * |
---|
| 8 | * This file is part of ALMOS-MKH. |
---|
| 9 | * |
---|
| 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
| 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 | * |
---|
| 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
| 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 |
---|
| 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
| 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 22 | */ |
---|
| 23 | |
---|
[14] | 24 | #include <kernel_config.h> |
---|
[457] | 25 | #include <hal_kernel_types.h> |
---|
[23] | 26 | #include <hal_uspace.h> |
---|
[1] | 27 | #include <errno.h> |
---|
| 28 | #include <printk.h> |
---|
| 29 | #include <core.h> |
---|
| 30 | #include <vfs.h> |
---|
| 31 | #include <cluster.h> |
---|
| 32 | #include <process.h> |
---|
| 33 | #include <thread.h> |
---|
| 34 | #include <vmm.h> |
---|
| 35 | #include <ppm.h> |
---|
| 36 | #include <rpc.h> |
---|
| 37 | |
---|
[506] | 38 | #include <syscalls.h> |
---|
[1] | 39 | |
---|
[683] | 40 | ////////////////////////////////////////////////i//////////////////////////////////////// |
---|
| 41 | // This static function is called twice by the sys_exec() function : |
---|
| 42 | // - to register the main() arguments (args) in the process <exec_info> structure. |
---|
| 43 | // - to register the environment variables (envs) in the <exec_info> structure. |
---|
| 44 | // In both cases the input is an array of NULL terminated string pointers in user space, |
---|
| 45 | // identified by the <u_pointers> argument. The strings can be dispatched anywhere in |
---|
| 46 | // the calling user process space. The max number of envs, and the max number of args |
---|
| 47 | // are defined by the CONFIG_PROCESS_ARGS_NR and CONFIG_PROCESS_ENVS_MAX_NR parameters. |
---|
| 48 | ////////////////////////////////////////////////i//////////////////////////////////////// |
---|
| 49 | // Implementation Note: |
---|
| 50 | // Both the array of pointers and the strings themselve are stored in kernel space in one |
---|
| 51 | // single, dynamically allocated, kernel buffer containing an integer number of pages, |
---|
| 52 | // defined by the CONFIG_VMM_ENVS_SIZE and CONFIG_VMM_STACK_SIZE parameters. |
---|
| 53 | // These two kernel buffers contains : |
---|
| 54 | // - in the first bytes a fixed size kernel array of kernel pointers on the strings. |
---|
| 55 | // - in the following bytes the strings themselves. |
---|
| 56 | // The exec_info_t structure is defined in the <process.h> file. |
---|
| 57 | ////////////////////////////////////////////////i//////////////////////////////////////// |
---|
| 58 | // @ is_args : [in] true if called for (args) / false if called for (envs). |
---|
| 59 | // @ u_pointers : [in] array of pointers on the strings (in user space). |
---|
| 60 | // @ exec_info : [inout] pointer on the exec_info structure. |
---|
| 61 | // @ return 0 if success / non-zero if too many strings or no memory. |
---|
| 62 | ////////////////////////////////////////////////i//////////////////////////////////////// |
---|
| 63 | static error_t exec_get_strings( bool_t is_args, |
---|
| 64 | char ** u_pointers, |
---|
| 65 | exec_info_t * exec_info ) |
---|
| 66 | { |
---|
| 67 | uint32_t index; // slot index in pointers array |
---|
| 68 | uint32_t length; // string length (in bytes) |
---|
| 69 | uint32_t pointers_bytes; // number of bytes to store pointers |
---|
| 70 | uint32_t max_index; // max size of pointers array |
---|
| 71 | char ** k_pointers; // base of kernel array of pointers |
---|
| 72 | char * k_buf_ptr; // pointer on first empty slot in strings buffer |
---|
| 73 | uint32_t k_buf_space; // number of bytes available in string buffer |
---|
| 74 | char * k_buf; // kernel buffer for both pointers & strings |
---|
| 75 | |
---|
| 76 | #if DEBUG_SYS_EXEC |
---|
| 77 | thread_t * this = CURRENT_THREAD; |
---|
| 78 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
| 79 | #endif |
---|
| 80 | |
---|
| 81 | // Allocate one block of physical memory for both the pointers and the strings |
---|
| 82 | |
---|
| 83 | if( is_args ) |
---|
| 84 | { |
---|
| 85 | k_buf = kmem_alloc( bits_log2(CONFIG_VMM_ARGS_SIZE << CONFIG_PPM_PAGE_ORDER), AF_ZERO ); |
---|
| 86 | |
---|
| 87 | pointers_bytes = (CONFIG_PROCESS_ARGS_MAX_NR + 1) * sizeof(char *); |
---|
| 88 | k_pointers = (char **)k_buf; |
---|
| 89 | k_buf_ptr = k_buf + pointers_bytes; |
---|
| 90 | k_buf_space = (CONFIG_VMM_ARGS_SIZE << CONFIG_PPM_PAGE_ORDER) - pointers_bytes; |
---|
| 91 | max_index = CONFIG_PROCESS_ARGS_MAX_NR + 1; |
---|
| 92 | |
---|
| 93 | #if DEBUG_SYS_EXEC |
---|
| 94 | if( DEBUG_SYS_EXEC < cycle ) |
---|
| 95 | printk("\n[%s] thread[%x,%x] for args / u_buf %x / k_buf %x\n", |
---|
| 96 | __FUNCTION__, this->process->pid, this->trdid, u_pointers, k_buf ); |
---|
| 97 | #endif |
---|
| 98 | |
---|
| 99 | } |
---|
| 100 | else // envs |
---|
| 101 | { |
---|
| 102 | k_buf = kmem_alloc( bits_log2(CONFIG_VMM_ENVS_SIZE << CONFIG_PPM_PAGE_ORDER), AF_ZERO ); |
---|
| 103 | |
---|
| 104 | pointers_bytes = (CONFIG_PROCESS_ENVS_MAX_NR + 1) * sizeof(char *); |
---|
| 105 | k_pointers = (char **)k_buf; |
---|
| 106 | k_buf_ptr = k_buf + pointers_bytes; |
---|
| 107 | k_buf_space = (CONFIG_VMM_ENVS_SIZE << CONFIG_PPM_PAGE_ORDER) - pointers_bytes; |
---|
| 108 | max_index = CONFIG_PROCESS_ENVS_MAX_NR + 1; |
---|
| 109 | |
---|
| 110 | #if DEBUG_SYS_EXEC |
---|
| 111 | if( DEBUG_SYS_EXEC < cycle ) |
---|
| 112 | printk("\n[%s] thread[%x,%x] for envs / u_buf %x / k_buf %x\n", |
---|
| 113 | __FUNCTION__, this->process->pid, this->trdid, u_pointers, k_buf ); |
---|
| 114 | #endif |
---|
| 115 | |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | // copy the user array of pointers to kernel buffer |
---|
| 119 | hal_copy_from_uspace( XPTR( local_cxy , k_pointers ), |
---|
| 120 | u_pointers, |
---|
| 121 | pointers_bytes ); |
---|
| 122 | |
---|
| 123 | // WARNING : the pointers copied in the k_pointers[] array are user pointers, |
---|
| 124 | // after the loop below, the k_pointers[] array contains kernel pointers. |
---|
| 125 | |
---|
| 126 | #if DEBUG_SYS_EXEC |
---|
| 127 | if( DEBUG_SYS_EXEC < cycle ) |
---|
| 128 | printk("\n[%s] thread[%x,%x] moved u_ptr array of pointers to k_ptr array\n", |
---|
| 129 | __FUNCTION__, this->process->pid, this->trdid ); |
---|
| 130 | #endif |
---|
| 131 | |
---|
| 132 | // scan kernel array of pointers to copy strings to kernel buffer |
---|
| 133 | for( index = 0 ; index < max_index ; index++ ) |
---|
| 134 | { |
---|
| 135 | // exit loop if (k_pointers[index] == NUll) |
---|
| 136 | if( k_pointers[index] == NULL ) break; |
---|
| 137 | |
---|
| 138 | // compute string length (without the NUL character) |
---|
| 139 | length = hal_strlen_from_uspace( k_pointers[index] ); |
---|
| 140 | |
---|
| 141 | // return error if overflow in kernel buffer |
---|
| 142 | if( length > k_buf_space ) return -1; |
---|
| 143 | |
---|
| 144 | // copy the string itself to kernel buffer |
---|
| 145 | hal_copy_from_uspace( XPTR( local_cxy , k_buf_ptr ), |
---|
| 146 | k_pointers[index], |
---|
| 147 | length + 1 ); |
---|
| 148 | |
---|
| 149 | #if DEBUG_SYS_EXEC |
---|
| 150 | if( DEBUG_SYS_EXEC < cycle ) |
---|
| 151 | printk("\n[%s] thread[%x,%x] copied string[%d] <%s> to kernel buffer / length %d\n", |
---|
| 152 | __FUNCTION__, this->process->pid, this->trdid, index, k_buf_ptr, length ); |
---|
| 153 | #endif |
---|
| 154 | |
---|
| 155 | // replace the user pointer by a kernel pointer in the k_pointer[] array |
---|
| 156 | k_pointers[index] = k_buf_ptr; |
---|
| 157 | |
---|
| 158 | // increment loop variables |
---|
| 159 | k_buf_ptr += (length + 1); |
---|
| 160 | k_buf_space -= (length + 1); |
---|
| 161 | |
---|
| 162 | #if DEBUG_SYS_EXEC |
---|
| 163 | if( DEBUG_SYS_EXEC < cycle ) |
---|
| 164 | { |
---|
| 165 | if( k_pointers[0] != NULL ) |
---|
| 166 | printk("\n[%s] thread[%x,%x] : &arg0 = %x / arg0 = <%s>\n", |
---|
| 167 | __FUNCTION__, this->process->pid, this->trdid, k_pointers[0], k_pointers[0] ); |
---|
| 168 | else |
---|
| 169 | printk("\n[%s] thread[%x,%x] : unexpected NULL value for &arg0\n", |
---|
| 170 | __FUNCTION__, this->process->pid, this->trdid ); |
---|
| 171 | } |
---|
| 172 | #endif |
---|
| 173 | |
---|
| 174 | } // end loop on index |
---|
| 175 | |
---|
| 176 | // update into exec_info structure |
---|
| 177 | if( is_args ) |
---|
| 178 | { |
---|
| 179 | exec_info->args_pointers = k_pointers; |
---|
| 180 | exec_info->args_nr = index; |
---|
| 181 | } |
---|
| 182 | else |
---|
| 183 | { |
---|
| 184 | exec_info->envs_pointers = k_pointers; |
---|
| 185 | exec_info->envs_buf_free = k_buf_ptr; |
---|
| 186 | exec_info->envs_nr = index; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | #if DEBUG_SYS_EXEC |
---|
| 190 | if( DEBUG_SYS_EXEC < cycle ) |
---|
| 191 | printk("\n[%s] thread[%x,%x] copied %d strings to kernel buffer\n", |
---|
| 192 | __FUNCTION__, this->process->pid, this->trdid, index ); |
---|
| 193 | #endif |
---|
| 194 | |
---|
| 195 | return 0; |
---|
| 196 | |
---|
| 197 | } // end exec_get_strings() |
---|
| 198 | |
---|
| 199 | |
---|
[670] | 200 | /////////////////////////////// |
---|
[683] | 201 | int sys_exec( char * pathname, // .elf file pathname in user space |
---|
| 202 | char ** user_args, // pointer on array of process arguments in user space |
---|
| 203 | char ** user_envs ) // pointer on array of env variables in user space |
---|
[1] | 204 | { |
---|
[407] | 205 | error_t error; |
---|
[670] | 206 | vseg_t * vseg; |
---|
[1] | 207 | |
---|
[433] | 208 | // get calling thread, process, & pid |
---|
[421] | 209 | thread_t * this = CURRENT_THREAD; |
---|
| 210 | process_t * process = this->process; |
---|
| 211 | pid_t pid = process->pid; |
---|
[670] | 212 | trdid_t trdid = this->trdid; |
---|
[23] | 213 | |
---|
[670] | 214 | assert( __FUNCTION__, (CXY_FROM_PID( pid ) == local_cxy) , |
---|
| 215 | "must be called in the owner cluster\n"); |
---|
| 216 | |
---|
| 217 | assert( __FUNCTION__, (LTID_FROM_TRDID( trdid ) == 0) , |
---|
| 218 | "must be called by the main thread\n"); |
---|
| 219 | |
---|
| 220 | assert( __FUNCTION__, (user_envs == NULL) , |
---|
| 221 | "environment variables not supported yet\n" ); |
---|
| 222 | |
---|
| 223 | #if DEBUG_SYS_EXEC || DEBUG_SYSCALLS_ERROR |
---|
[566] | 224 | uint64_t tm_start = hal_get_cycles(); |
---|
| 225 | #endif |
---|
| 226 | |
---|
[670] | 227 | // check "pathname" mapped in user space |
---|
| 228 | if( vmm_get_vseg( process , (intptr_t)pathname , &vseg ) ) |
---|
| 229 | { |
---|
[23] | 230 | |
---|
[670] | 231 | #if DEBUG_SYSCALLS_ERROR |
---|
| 232 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
| 233 | printk("\n[ERROR] in %s : thread[%x,%] / pathname pointer %x unmapped\n", |
---|
| 234 | __FUNCTION__, pid, trdid, pathname ); |
---|
| 235 | #endif |
---|
| 236 | this->errno = EINVAL; |
---|
| 237 | return -1; |
---|
| 238 | } |
---|
[433] | 239 | |
---|
[670] | 240 | // check "pathname" length |
---|
[407] | 241 | if( hal_strlen_from_uspace( pathname ) >= CONFIG_VFS_MAX_PATH_LENGTH ) |
---|
[23] | 242 | { |
---|
[433] | 243 | |
---|
[438] | 244 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 245 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
| 246 | printk("\n[ERROR] in %s : thread[%x,%x] / pathname too long\n", |
---|
| 247 | __FUNCTION__, pid, trdid ); |
---|
[433] | 248 | #endif |
---|
[407] | 249 | this->errno = ENFILE; |
---|
[23] | 250 | return -1; |
---|
| 251 | } |
---|
| 252 | |
---|
[670] | 253 | // check "args" mapped in user space if non NULL |
---|
| 254 | if( (user_args != NULL) && (vmm_get_vseg( process , (intptr_t)user_args , &vseg )) ) |
---|
| 255 | { |
---|
[408] | 256 | |
---|
[670] | 257 | #if DEBUG_SYSCALLS_ERROR |
---|
[683] | 258 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
[670] | 259 | printk("\n[ERROR] in %s for thread[%x,%] : user_args pointer %x unmapped\n", |
---|
| 260 | __FUNCTION__, pid, trdid, user_args ); |
---|
| 261 | #endif |
---|
| 262 | this->errno = EINVAL; |
---|
| 263 | return -1; |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | // check "envs" mapped in user space if not NULL |
---|
| 267 | if( (user_envs != NULL) && (vmm_get_vseg( process , (intptr_t)user_envs , &vseg )) ) |
---|
| 268 | { |
---|
| 269 | |
---|
| 270 | #if DEBUG_SYSCALLS_ERROR |
---|
| 271 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
| 272 | printk("\n[ERROR] in %s : thread[%x,%] / user_envs pointer %x unmapped\n", |
---|
| 273 | __FUNCTION__, pid, trdid, user_envs ); |
---|
| 274 | #endif |
---|
| 275 | this->errno = EINVAL; |
---|
| 276 | return -1; |
---|
| 277 | } |
---|
| 278 | |
---|
[683] | 279 | // 1. copy "pathname" in kernel exec_info structure |
---|
| 280 | hal_strcpy_from_uspace( XPTR( local_cxy , &process->exec_info.path[0] ), |
---|
| 281 | pathname, |
---|
| 282 | CONFIG_VFS_MAX_PATH_LENGTH ); |
---|
| 283 | |
---|
[438] | 284 | #if DEBUG_SYS_EXEC |
---|
[670] | 285 | if( DEBUG_SYS_EXEC < (uint32_t)tm_start ) |
---|
| 286 | printk("\n[%s] thread[%x,%x] enter / path <%s> / args %x / envs %x / cycle %d\n", |
---|
[683] | 287 | __FUNCTION__, pid, trdid, &process->exec_info.path[0], |
---|
| 288 | user_args, user_envs, (uint32_t)tm_start ); |
---|
[433] | 289 | #endif |
---|
[23] | 290 | |
---|
[670] | 291 | // 2. copy "arguments" pointers & strings in process exec_info if required |
---|
| 292 | if( user_args != NULL ) |
---|
[1] | 293 | { |
---|
[683] | 294 | if( exec_get_strings( true , user_args , &process->exec_info ) ) |
---|
[407] | 295 | { |
---|
[433] | 296 | |
---|
[438] | 297 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 298 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
[683] | 299 | printk("\n[ERROR] in %s : thread[%x,%] cannot get arguments for <%s>\n", |
---|
[670] | 300 | __FUNCTION__, pid, trdid, pathname ); |
---|
[433] | 301 | #endif |
---|
[435] | 302 | this->errno = EINVAL; |
---|
[407] | 303 | return -1; |
---|
| 304 | } |
---|
[670] | 305 | |
---|
| 306 | #if DEBUG_SYS_EXEC |
---|
| 307 | if( DEBUG_SYS_EXEC < (uint32_t)tm_start ) |
---|
[683] | 308 | printk("\n[%s] thread[%x,%x] set arguments in exec_info / arg[0] = <%s>\n", |
---|
[670] | 309 | __FUNCTION__, pid, trdid, process->exec_info.args_pointers[0] ); |
---|
| 310 | #endif |
---|
| 311 | |
---|
[23] | 312 | } |
---|
[1] | 313 | |
---|
[670] | 314 | // 3. copy "environment" pointers & strings in process exec_info if required |
---|
| 315 | if( user_envs != NULL ) |
---|
[23] | 316 | { |
---|
[683] | 317 | if( exec_get_strings( false , user_envs , &process->exec_info ) ) |
---|
[407] | 318 | { |
---|
[433] | 319 | |
---|
[509] | 320 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 321 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
[683] | 322 | printk("\n[ERROR] in %s : thread[%x,%] cannot get env variables for <%s>\n", |
---|
[670] | 323 | __FUNCTION__, pid, trdid, pathname ); |
---|
[433] | 324 | #endif |
---|
[435] | 325 | this->errno = EINVAL; |
---|
[407] | 326 | return -1; |
---|
| 327 | } |
---|
[670] | 328 | |
---|
| 329 | #if DEBUG_SYS_EXEC |
---|
| 330 | if( DEBUG_SYS_EXEC < (uint32_t)tm_start ) |
---|
[683] | 331 | printk("\n[%s] thread[%x,%x] set envs in exec_info / env[0] = <%s>\n", |
---|
[670] | 332 | __FUNCTION__, pid, trdid, process->exec_info.envs_pointers[0] ); |
---|
| 333 | #endif |
---|
| 334 | |
---|
[1] | 335 | } |
---|
[23] | 336 | |
---|
[670] | 337 | // call relevant kernel function (no return if success) |
---|
| 338 | error = process_make_exec(); |
---|
[408] | 339 | |
---|
[433] | 340 | if( error ) |
---|
[1] | 341 | { |
---|
[302] | 342 | |
---|
[438] | 343 | #if DEBUG_SYSCALLS_ERROR |
---|
[670] | 344 | if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) |
---|
| 345 | printk("\n[ERROR] in %s : thread[%x,%x] cannot create process <%s>\n", |
---|
| 346 | __FUNCTION__, pid, trdid, process->exec_info.path ); |
---|
[433] | 347 | #endif |
---|
[23] | 348 | this->errno = error; |
---|
| 349 | return -1; |
---|
[1] | 350 | } |
---|
| 351 | |
---|
[433] | 352 | return 0; |
---|
| 353 | |
---|
[1] | 354 | } // end sys_exec() |
---|
| 355 | |
---|