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

Last change on this file since 635 was 635, checked in by alain, 15 months ago

This version is a major evolution: The physical memory allocators,
defined in the kmem.c, ppm.c, and kcm.c files have been modified
to support remote accesses. The RPCs that were previously user
to allocate physical memory in a remote cluster have been removed.
This has been done to cure a dead-lock in case of concurrent page-faults.

This version 2.2 has been tested on a (4 clusters / 2 cores per cluster)
TSAR architecture, for both the "sort" and the "fft" applications.

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