source: trunk/kernel/syscalls/sys_exec.c @ 635

Last change on this file since 635 was 635, checked in by alain, 15 months ago

This version is a major evolution: The physical memory allocators,
defined in the kmem.c, ppm.c, and kcm.c files have been modified
to support remote accesses. The RPCs that were previously user
to allocate physical memory in a remote cluster have been removed.
This has been done to cure a dead-lock in case of concurrent page-faults.

This version 2.2 has been tested on a (4 clusters / 2 cores per cluster)
TSAR architecture, for both the "sort" and the "fft" applications.

File size: 8.9 KB
Line 
1/*
2 * sys_exec.c - Kernel function implementing the "exec" system call.
3 *
4 * Authors   Alain Greiner (2016,2017,2017,2019)
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
24#include <kernel_config.h>
25#include <hal_kernel_types.h>
26#include <hal_uspace.h>
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
38#include <syscalls.h>
39
40////////////////////////////////////////////////i//////////////////////////////////////
41// This static function is called twice by the sys_exec() function :
42// - to register the main() arguments (args) in the 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 string pointers in user space,
45// and a set of strings in user space.
46// We allocate one physical page to store a kernel copy of the array of pointers,
47// we allocate one or several physical pages to store the strings themselve,
48// and register these buffers and the number of strings in the exec_info structure.
49// The max number of strings is 1024 (for both args and envs). The numbers of pages
50// to store the (args) and (envs) strings are configuration parameters.
51///////////////////////////////////////////////////////////////////////////////////////
52// @ exec_info   : pointer on the exec_info structure.
53// @ is_args     : true if called for (args) / false if called for (envs).
54// @ u_pointers  : array of pointers on the strings (in user space).
55// @ return 0 if success / non-zero if too many strings or no more memory.
56///////////////////////////////////////////////////////////////////////////////////////
57static error_t process_exec_get_strings( exec_info_t  * exec_info,
58                                         bool_t         is_args,
59                                         char        ** u_pointers )
60{
61    uint32_t     index;       // string index
62    uint32_t     found_null;  // NULL pointer found in array of pointers
63    uint32_t     length;      // string length
64    kmem_req_t   req;         // kmem request
65    uint32_t     order;       // ln2( number of pages to store strings )
66    char      ** k_pointers;  // base of kernel array of pointers
67    char       * k_buf_base;  // base address of the kernel strings buffer
68    char       * k_buf_ptr;   // pointer on first empty slot in kernel strings buffer
69
70    // compute ln2( number of pages for kernel strings buffer )
71    if( is_args ) order = bits_log2( CONFIG_VMM_ARGS_SIZE );
72    else          order = bits_log2( CONFIG_VMM_ENVS_SIZE );
73
74    // allocate one physical page for kernel array of pointers
75    req.type   = KMEM_PPM;
76    req.order  = 0;
77    req.flags  = AF_KERNEL | AF_ZERO;
78    k_pointers = kmem_alloc( &req );
79
80    if( k_pointers == NULL ) return ENOMEM;
81
82    // allocate several physical pages to store the strings themselve
83    req.type   = KMEM_PPM;
84    req.order  = order;
85    req.flags  = AF_KERNEL | AF_ZERO;
86    k_buf_base = kmem_alloc( &req );
87
88    if( k_buf_base == NULL ) return ENOMEM;
89
90    // copy the array of pointers to kernel buffer
91    hal_copy_from_uspace( local_cxy,
92                          k_pointers,
93                          u_pointers,
94                          CONFIG_PPM_PAGE_SIZE );
95
96    // scan kernel array of pointers to copy the strings
97    found_null = 0;
98    k_buf_ptr  = k_buf_base;
99    for( index = 0 ; index < 1024 ; index++ )
100    {
101        if( k_pointers[index] == NULL )
102        {
103            found_null = 1;
104            break;
105        }
106
107        // compute string length
108        length = hal_strlen_from_uspace( k_pointers[index] );
109
110        // copy the user string to kernel buffer
111        hal_copy_from_uspace( local_cxy,
112                              k_buf_ptr,
113                              k_pointers[index],
114                              length );
115
116        // update k_pointer[index] entry
117        k_pointers[index] = k_buf_ptr;
118
119        // increment pointer on kernel strings buffer
120        k_buf_ptr += (length + 1);
121    }
122
123    // update into exec_info structure
124    if( found_null && is_args )
125    {
126        exec_info->args_pointers  =  k_pointers;
127        exec_info->args_buf_base  =  k_buf_base;
128        exec_info->args_nr        =  index;
129    }
130    else if( found_null && !is_args )
131    {
132        exec_info->envs_pointers  =  k_pointers;
133        exec_info->envs_buf_base  =  k_buf_base;
134        exec_info->envs_buf_free  =  k_buf_ptr;
135        exec_info->envs_nr        =  index;
136    }
137    else
138    {
139        return EINVAL;
140    }
141
142    return 0;
143} // end process_exec_get_strings()
144
145/////////////////////////////////////////////////////////////////////////////////////////
146// Implementation note:
147// This function must be called by the main thread (thread 0 in owner cluster).
148// It build an exec_info_t structure containing all informations
149// required to initialize the new process descriptor and the associated thread.
150// It includes the new process main() arguments, the environment variables,
151// and the pathname to the new process .elf file.
152// It calls the process_exec_get_strings() functions to copy the main() arguments and
153// the environment variables from user buffers to the exec_info_t structure, allocate
154// and call the process_make_exec() function.
155// As it must destroy all process copies, and all other threads in all clusters,
156// the process_make_exec() function must be executed in the owner cluster.
157//
158// TODO : the args & envs arguments are not supported yet : both must be NULL  [AG]
159/////////////////////////////////////////////////////////////////////////////////////////
160int sys_exec( char  * pathname,       // .elf file pathname
161              char ** args,           // process arguments
162              char ** envs )          // environment variables
163{
164    exec_info_t   exec_info;          // structure to pass to process_make_exec()
165    error_t       error;
166
167    // get calling thread, process, & pid
168    thread_t    * this    = CURRENT_THREAD;
169    process_t   * process = this->process;
170    pid_t         pid     = process->pid;
171
172#if DEBUG_SYS_EXEC
173uint64_t     tm_start = hal_get_cycles();
174#endif
175
176    assert( (CXY_FROM_PID( pid ) == local_cxy) ,
177    "must be called in the owner cluster\n");
178
179    assert( (LTID_FROM_TRDID( this->trdid ) == 0) ,
180    "must be called by the main thread\n");
181
182    assert( (args == NULL) ,
183    "args not supported yet\n" );
184
185    assert( (envs == NULL) ,
186    "args not supported yet\n" );
187
188    // check pathname length
189    if( hal_strlen_from_uspace( pathname ) >= CONFIG_VFS_MAX_PATH_LENGTH )
190    {
191
192#if DEBUG_SYSCALLS_ERROR
193printk("\n[ERROR] in %s : thread[%x,%x] pathname too long\n",
194__FUNCTION__, pid, this->trdid );
195#endif
196        this->errno = ENFILE;
197        return -1;
198    }
199
200    // copy pathname in exec_info structure (kernel space)
201    hal_strcpy_from_uspace( exec_info.path , pathname , CONFIG_VFS_MAX_PATH_LENGTH );
202
203#if DEBUG_SYS_EXEC
204if( DEBUG_SYS_EXEC < tm_start )
205printk("\n[%s] thread[%x,%x] enter for path <%s> / cycle = %d\n",
206__FUNCTION__, pid, this->trdid, exec_info.path, (uint32_t)tm_start );
207#endif
208
209    // check and store args in exec_info structure if required
210    if( args != NULL )
211    {
212        if( process_exec_get_strings( &exec_info , true , args ) )
213        {
214
215#if DEBUG_SYSCALLS_ERROR
216printk("\n[ERROR] in %s : thread[%x,%x] cannot access args for <%s>\n",
217__FUNCTION__, pid, this->trdid, exec_info.path );
218#endif
219            this->errno = EINVAL;
220            return -1;
221        }
222    }
223
224    // check and store envs in exec_info structure if required
225    if( envs != NULL )
226    {
227        if( process_exec_get_strings( &exec_info , false , envs ) )
228        {
229
230#if DEBUG_SYSCALLS_ERROR
231printk("\n[ERROR] in %s : thread[%x,%x] cannot access envs for <%s>\n",
232__FUNCTION__ , pid, this->trdid, exec_info.path );
233#endif
234            this->errno = EINVAL;
235            return -1;
236        }
237    }
238
239    // call relevant kernel function
240    error = process_make_exec( &exec_info );
241
242    if( error )
243    {
244
245#if DEBUG_SYSCALLS_ERROR
246printk("\n[ERROR] in %s : thread[%x,%x] cannot create process for <%s>\n",
247__FUNCTION__, pid, this->trdid, exec_info.path );
248#endif
249        this->errno = error;
250        return -1;
251    }
252
253    assert( false , "we should never execute this code" );
254
255    return 0; 
256
257} // end sys_exec()
258
Note: See TracBrowser for help on using the repository browser.