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

Last change on this file since 634 was 633, checked in by alain, 5 years ago

cosmetic

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