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

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

First implementation of fork/exec.

File size: 5.8 KB
Line 
1/*
2 * sys_thread_create.c - creates a new user thread
3 *
4 * Author     Alain Greiner (2016,2017)
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        uint32_t         tm_start;
60        uint32_t         tm_end;
61
62        tm_start = hal_get_cycles();
63
64        // get parent thead pointer, extended pointer, and process
65        parent     = CURRENT_THREAD;
66        parent_xp  = XPTR( local_cxy , parent );
67        process    = parent->process;
68
69        // check user_attr in user space & copy to kernel space
70    if( user_attr != NULL )
71    {
72            error = vmm_v2p_translate( false , user_attr , &paddr );
73
74            if( error )
75            {
76                    printk("\n[ERROR] in %s : user_attr unmapped\n", __FUNCTION__ );
77                    parent->errno = EINVAL;
78                    return -1;
79            }
80       
81            hal_copy_from_uspace( &kern_attr , user_attr , sizeof(pthread_attr_t) );
82    }
83
84        // check start_func in user space
85        error = vmm_v2p_translate( false , start_func , &paddr );
86
87        if( error )
88        {
89                printk("\n[ERROR] in %s : start_func unmapped\n", __FUNCTION__ );
90                parent->errno = EINVAL;
91                return -1;
92        }
93
94        // check start_arg in user space
95        if( start_arg != NULL ) error = vmm_v2p_translate( false , start_arg , &paddr );
96
97        if( error )
98        {
99                printk("\n[ERROR] in %s : start_arg unmapped\n", __FUNCTION__ );
100                parent->errno = EINVAL;
101                return -1;
102        }
103
104        // check / define attributes an target_cxy
105    if( user_attr != NULL )                      // user defined attributes
106    {
107            // check / get target_cxy
108            if( kern_attr.attributes & PT_ATTR_CLUSTER_DEFINED )
109            {
110                    if( cluster_is_undefined( kern_attr.cxy ) )
111                    {
112                            printk("\n[ERROR] in %s : illegal target cluster = %x\n",
113                            __FUNCTION__ , kern_attr.cxy );
114                            parent->errno = EINVAL;
115                            return -1;
116            }
117            target_cxy = kern_attr.cxy;
118                }
119        else
120        {
121            target_cxy = dqdt_get_cluster_for_process();
122        }
123        }
124        else                                        // set default attributes
125        {
126        kern_attr.attributes = PT_ATTR_DETACH | PT_ATTR_CLUSTER_DEFINED;
127        target_cxy           = dqdt_get_cluster_for_process();
128        }
129
130        // create the thread, using a RPC if required
131        // this returns "error", "child_ptr", and "child_xp"
132
133        if( target_cxy == local_cxy )                         // target cluster is local
134        {
135                // create thread in local cluster
136                error = thread_user_create( process->pid,
137                                            start_func,
138                                            start_arg,
139                                            &kern_attr,
140                                            &child_ptr );
141
142                child_xp = XPTR( local_cxy , child_ptr );
143        }
144        else                                                 // target cluster is remote
145        {
146                rpc_thread_user_create_client( target_cxy,
147                                               process->pid,
148                                               start_func,
149                                               start_arg,
150                                               &kern_attr,
151                                               &child_xp,
152                                               &error );
153
154                child_ptr = (thread_t *)GET_PTR( child_xp );
155        }
156
157        // check successful thread creation
158        if( error )
159        {
160                printk("\n[ERROR] in %s : cannot create thread\n", __FUNCTION__ );
161                return ENOMEM;
162        }
163
164        // returns trdid to user space
165        trdid = hal_remote_lw( XPTR( target_cxy , &child_ptr->trdid ) );
166        hal_copy_to_uspace( trdid_ptr , &trdid , sizeof(pthread_t) );
167
168    // register child in parent if required
169    if( user_attr != NULL )
170    {
171            if( (kern_attr.attributes & PT_ATTR_DETACH) == 0 )
172                thread_child_parent_link( parent_xp , child_xp );
173    }
174
175    // activate new thread
176        thread_unblock( child_xp , THREAD_BLOCKED_GLOBAL );
177
178    hal_fence();
179
180        tm_end = hal_get_cycles();
181
182syscall_dmsg("\n[DBG] %s : core[%x,%d] created thread %x for process %x / cycle %d\n"
183"      cluster %x / cost = %d cycles\n",
184__FUNCTION__ , local_cxy , parent->core->lid , trdid , process->pid , tm_end ,
185target_cxy , tm_end - tm_start );
186
187        return 0;
188
189}  // end sys_thread_create()
190
Note: See TracBrowser for help on using the repository browser.