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

Last change on this file since 558 was 506, checked in by viala@…, 6 years ago

[syscalls] add interface in implementation.

Add const where possible, fix protoypes to follow interface.

File size: 6.9 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 <spinlock.h>
38#include <dqdt.h>
39#include <rpc.h>
40
41#include <syscalls.h>
42
43///////////////////////////////////////////////////
44int sys_thread_create(
45  struct thread_s             * new_thread,
46  const struct pthread_attr_s * user_attr,
47  const void                  * start_func,
48  const void                  * start_args )
49{
50        pthread_attr_t   kern_attr;        // copy of pthread attributes in kernel space
51        thread_t       * parent;           // pointer on thread executing the pthread_create
52        xptr_t           parent_xp;        // extended pointer on created thread
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    cxy_t            target_cxy;       // target cluster identifier
59        error_t          error;
60
61        // get parent thead pointer, extended pointer, and process
62        parent     = CURRENT_THREAD;
63        parent_xp  = XPTR( local_cxy , parent );
64        process    = parent->process;
65
66#if DEBUG_SYS_THREAD_CREATE
67uint64_t tm_start;
68uint64_t tm_end;
69tm_start = hal_get_cycles();
70if( DEBUG_SYS_THREAD_CREATE < tm_start )
71printk("\n[DBG] %s : thread %x (cxy %x) enter / process %x / cycle %d\n",
72__FUNCTION__, parent, local_cxy, process->pid, (uint32_t)tm_start );
73#endif
74
75    // check trdid buffer in user space
76    error = vmm_get_vseg( process , (intptr_t)new_thread , &vseg );
77
78    if ( error )
79    {
80
81#if DEBUG_SYSCALLS_ERROR
82printk("\n[ERROR] in %s : trdid buffer unmapped %x / thread %x / process %x\n",
83__FUNCTION__ , (intptr_t)new_thread, parent->trdid, process->pid );
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 : user_attr buffer unmapped %x / thread %x / process %x\n",
100__FUNCTION__ , (intptr_t)user_attr , parent->trdid , process->pid );
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 : start_func unmapped %x / thread %x / process %x\n",
118__FUNCTION__ , (intptr_t)start_func , parent->trdid , process->pid );
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 : start_args buffer unmapped %x / thread %x / process %x\n",
135__FUNCTION__ , (intptr_t)start_args , parent->trdid , process->pid );
136vmm_display( process , false );
137#endif
138                    parent->errno = EINVAL;
139                    return -1;
140        }
141        }
142
143    // define attributes and target_cxy
144    if( user_attr != NULL )                      // user defined attributes
145    {
146            // check / get target_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 : illegal target cluster = %x / thread %x / process %x\n",
154__FUNCTION__ , kern_attr.cxy, parent->trdid, process->pid );
155#endif
156                            parent->errno = EINVAL;
157                            return -1;
158            }
159            target_cxy = kern_attr.cxy;
160                }
161        else
162        {
163            target_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        target_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( target_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( target_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 : cannot create new thread / thread %x / process %x\n",
205__FUNCTION__ , parent->trdid, process->pid );
206#endif
207                parent->errno = ENOMEM;
208                return -1;
209        }
210
211        // returns trdid to user space
212        trdid = hal_remote_lw( XPTR( target_cxy , &child_ptr->trdid ) );
213        hal_copy_to_uspace( new_thread , &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
221tm_end = hal_get_cycles();
222if( DEBUG_SYS_THREAD_CREATE < tm_end )
223printk("\n[DBG] %s : thread %x (cxy %x) created thread %x (cxy %x) / process %x / cycle %d\n",
224__FUNCTION__, parent, local_cxy, child_ptr, target_cxy, process->pid, (uint32_t)tm_end );
225#endif
226
227        return 0;
228
229}  // end sys_thread_create()
230
Note: See TracBrowser for help on using the repository browser.