source: trunk/kernel/syscalls/sys_thread_create.c @ 607

Last change on this file since 607 was 594, checked in by alain, 6 years ago

Fix various bugs in sys_stat() and sys_mmap() functions.
Improve debug in other functions.

File size: 7.1 KB
RevLine 
[1]1/*
2 * sys_thread_create.c - creates a new user thread
[289]3 *
[437]4 * Author     Alain Greiner (2016,2017,2018)
[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 <printk.h>
28#include <errno.h>
29#include <core.h>
30#include <cluster.h>
31#include <list.h>
32#include <xlist.h>
33#include <thread.h>
34#include <scheduler.h>
35#include <kmem.h>
36#include <process.h>
37#include <dqdt.h>
[23]38#include <rpc.h>
[1]39
[506]40#include <syscalls.h>
[1]41
[566]42/////////////////////////////////////////////////////////
43int sys_thread_create( trdid_t               * trdid_ptr,
44                       struct pthread_attr_s * user_attr,
45                       void                  * start_func,
46                       void                  * start_args )
[1]47{
[407]48        pthread_attr_t   kern_attr;        // copy of pthread attributes in kernel space
[1]49        thread_t       * parent;           // pointer on thread executing the pthread_create
[566]50        xptr_t           parent_xp;        // extended pointer on calling thread
51    cxy_t            child_cxy;        // created child thread cluster identifier
[289]52        thread_t       * child_ptr;        // pointer on created child thread
53        xptr_t           child_xp;         // extended pointer on created thread
54        trdid_t          trdid;            // created thread identifier
55        process_t      * process;          // pointer on local process descriptor
[440]56        vseg_t         * vseg;             // required for user space checking
[289]57        error_t          error;
[1]58
[407]59        // get parent thead pointer, extended pointer, and process
[1]60        parent     = CURRENT_THREAD;
[289]61        parent_xp  = XPTR( local_cxy , parent );
[1]62        process    = parent->process;
63
[594]64#if (DEBUG_SYS_THREAD_CREATE || CONFIG_INSTRUMENTATION_SYSCALLS)
65uint64_t     tm_start = hal_get_cycles();
66#endif
67
[438]68#if DEBUG_SYS_THREAD_CREATE
[437]69tm_start = hal_get_cycles();
[438]70if( DEBUG_SYS_THREAD_CREATE < tm_start )
[594]71printk("\n[%s] thread[%x,%x] enter / cycle %d\n",
72__FUNCTION__, process_pid, parent->trdid, (uint32_t)tm_start );
[437]73#endif
74
[440]75    // check trdid buffer in user space
[566]76    error = vmm_get_vseg( process , (intptr_t)trdid_ptr , &vseg );
[440]77
78    if ( error )
79    {
80
81#if DEBUG_SYSCALLS_ERROR
[594]82printk("\n[ERROR] in %s : thread[%x,%x] / trdid buffer %x unmapped %x\n",
83__FUNCTION__, process->pid, parent->trdid, (intptr_t)trdid_ptr );
[440]84vmm_display( process , false );
85#endif
86                parent->errno = EINVAL;
87                return -1;
88    }
89
90        // check user_attr buffer in user space & copy to kernel space
[407]91    if( user_attr != NULL )
92    {
[440]93            error = vmm_get_vseg( process , (intptr_t)user_attr , &vseg );
[23]94
[407]95            if( error )
96            {
[437]97
[438]98#if DEBUG_SYSCALLS_ERROR
[594]99printk("\n[ERROR] in %s : thread[%x,%x] / user_attr buffer unmapped %x\n",
100__FUNCTION__, process->pid, parent->trdid, (intptr_t)user_attr );
[440]101vmm_display( process , false );
[437]102#endif
[407]103                    parent->errno = EINVAL;
104                    return -1;
105            }
106       
107            hal_copy_from_uspace( &kern_attr , user_attr , sizeof(pthread_attr_t) );
108    }
[23]109
[289]110        // check start_func in user space
[440]111        error = vmm_get_vseg( process , (intptr_t)start_func , &vseg );
[23]112
[440]113    if( error )
114    {
[437]115
[438]116#if DEBUG_SYSCALLS_ERROR
[594]117printk("\n[ERROR] in %s : thread[%x,%x] / start_func unmapped %x\n",
118__FUNCTION__, process->pid, parent->trdid, (intptr_t)start_func );
[440]119vmm_display( process , false );
[437]120#endif
[440]121        parent->errno = EINVAL;
122            return -1;
[1]123        }
124
[506]125        // check start_args buffer in user space
126        if( start_args != NULL )
[440]127    {
[506]128        error = vmm_get_vseg( process , (intptr_t)start_args , &vseg );
[1]129
[440]130            if( error )
131            {
[437]132
[438]133#if DEBUG_SYSCALLS_ERROR
[594]134printk("\n[ERROR] in %s : thread[%x,%x] / start_args buffer unmapped %x\n",
135__FUNCTION__, process->pid, parent->trdid, (intptr_t)start_args );
[440]136vmm_display( process , false );
[437]137#endif
[440]138                    parent->errno = EINVAL;
139                    return -1;
140        }
[23]141        }
142
[566]143    // define attributes and child_cxy
[407]144    if( user_attr != NULL )                      // user defined attributes
145    {
[566]146            // check / get child_cxy
[407]147            if( kern_attr.attributes & PT_ATTR_CLUSTER_DEFINED )
148            {
149                    if( cluster_is_undefined( kern_attr.cxy ) )
150                    {
[437]151
[438]152#if DEBUG_SYSCALLS_ERROR
[594]153printk("\n[ERROR] in %s : thread[%x,%x] / illegal target cluster %x\n",
154__FUNCTION__, process->pid, parent->trdid, kern_attr.cxy );
[437]155#endif
[407]156                            parent->errno = EINVAL;
157                            return -1;
158            }
[566]159            child_cxy = kern_attr.cxy;
[289]160                }
[407]161        else
162        {
[566]163            child_cxy = dqdt_get_cluster_for_process();
[407]164        }
[289]165        }
[407]166        else                                        // set default attributes
[289]167        {
[407]168        kern_attr.attributes = PT_ATTR_DETACH | PT_ATTR_CLUSTER_DEFINED;
[566]169        child_cxy           = dqdt_get_cluster_for_process();
[289]170        }
[1]171
[289]172        // create the thread, using a RPC if required
[407]173        // this returns "error", "child_ptr", and "child_xp"
[1]174
[566]175        if( child_cxy == local_cxy )                         // target cluster is local
[289]176        {
177                // create thread in local cluster
178                error = thread_user_create( process->pid,
179                                            start_func,
[506]180                                            start_args,
[407]181                                            &kern_attr,
[289]182                                            &child_ptr );
[1]183
[289]184                child_xp = XPTR( local_cxy , child_ptr );
185        }
186        else                                                 // target cluster is remote
187        {
[566]188                rpc_thread_user_create_client( child_cxy,
[289]189                                               process->pid,
190                                               start_func,
[506]191                                               start_args,
[407]192                                               &kern_attr,
[289]193                                               &child_xp,
194                                               &error );
[23]195
[289]196                child_ptr = (thread_t *)GET_PTR( child_xp );
197        }
[1]198
[289]199        // check successful thread creation
200        if( error )
201        {
[437]202
[438]203#if DEBUG_SYSCALLS_ERROR
[594]204printk("\n[ERROR] in %s : thread[%x,%x] cannot create new thread\n",
205__FUNCTION__ , process->pid, parent->trdid );
[437]206#endif
[440]207                parent->errno = ENOMEM;
208                return -1;
[289]209        }
[1]210
[289]211        // returns trdid to user space
[566]212        trdid = hal_remote_l32( XPTR( child_cxy , &child_ptr->trdid ) );
213        hal_copy_to_uspace( trdid_ptr , &trdid , sizeof(pthread_t) );
[1]214
[407]215    // activate new thread
216        thread_unblock( child_xp , THREAD_BLOCKED_GLOBAL );
217
218    hal_fence();
219
[594]220#if (DEBUG_SYS_THREAD_CREATE || CONFIG_INSTRUMENTATION_SYSCALLS)
221uint64_t     tm_end = hal_get_cycles();
222#endif
223
[438]224#if DEBUG_SYS_THREAD_CREATE
225if( DEBUG_SYS_THREAD_CREATE < tm_end )
[594]226printk("\n[%s] thread[%x,%x] created thread %x / cycle %d\n",
227__FUNCTION__, process->pid, parent->trdid, child_ptr->trdid, (uint32_t)tm_end );
[437]228#endif
[1]229
[594]230#if CONFIG_INSTRUMENTATION_SYSCALLS
231hal_atomic_add( &syscalls_cumul_cost[SYS_THREAD_CREATE] , tm_end - tm_start );
232hal_atomic_add( &syscalls_occurences[SYS_THREAD_CREATE] , 1 );
233#endif
234
[1]235        return 0;
236
[407]237}  // end sys_thread_create()
238
Note: See TracBrowser for help on using the repository browser.