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

Last change on this file since 620 was 619, checked in by alain, 5 years ago

1) Fix a bug in KSH : after the "load" command,

the [ksh] prompt is now printed after completion
of the loaded application.

2) Fix a bug in vmm_handle_cow() : the copy-on-write

use now a hal_remote_memcpy() to replicate the page content.


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