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

Last change on this file since 451 was 441, checked in by alain, 7 years ago

Fix a bug in rpc_vmm_get_pte_client() function (bad RPC index).

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