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

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

Fix various bugs

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