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

Last change on this file since 484 was 457, checked in by alain, 6 years ago

This version modifies the exec syscall and fixes a large number of small bugs.
The version number has been updated (0.1)

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