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

Last change on this file since 628 was 626, checked in by alain, 5 years ago

This version has been tested on the sort multithreaded application
for TSAR_IOB architectures ranging from 1 to 8 clusters.
It fixes three bigs bugs:
1) the dev_ioc device API has been modified: the dev_ioc_sync_read()
and dev_ioc_sync_write() function use now extended pointers on the
kernel buffer to access a mapper stored in any cluster.
2) the hal_uspace API has been modified: the hal_copy_to_uspace()
and hal_copy_from_uspace() functions use now a (cxy,ptr) couple
to identify the target buffer (equivalent to an extended pointer.
3) an implementation bug has been fixed in the assembly code contained
in the hal_copy_to_uspace() and hal_copy_from_uspace() functions.

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