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

Last change on this file since 51 was 23, checked in by alain, 7 years ago

Introduce syscalls.

File size: 5.6 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//////////////////////////////////////////////////////////////////////////////////////////
43// This function implements the pthread_create system call
44//////////////////////////////////////////////////////////////////////////////////////////
45int sys_thread_create ( thread_t       * new_thread,    // [out] argument
46                        pthread_attr_t * user_attr,     // [in] argument
47                        void           * start_func,    // [in] argument
48                        void           * start_arg )    // [in] argument
49{
50        pthread_attr_t   k_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    paddr_t          paddr;            // unused, required by vmm_v2p_translate()
58    error_t          error;
59
60        uint32_t         tm_start;
61        uint32_t         tm_end;
62
63        tm_start = hal_time_stamp();
64
65    // get parent thead pointer, extended pointer, and process pointer
66        parent     = CURRENT_THREAD;
67    parent_xp  = XPTR( local_cxy , parent );   
68        process    = parent->process;
69
70    // check user_attr in user space
71    error = vmm_v2p_translate( false , user_attr , &paddr );
72
73        if( error )
74        {
75                printk("\n[ERROR] in %s : user_attr unmapped\n", __FUNCTION__ );
76                parent->errno = EINVAL;
77        return -1;
78        }
79
80    // check start_func in user space
81    error = vmm_v2p_translate( false , start_func , &paddr );
82
83        if( error )
84        {
85                printk("\n[ERROR] in %s : start_func unmapped\n", __FUNCTION__ );
86                parent->errno = EINVAL;
87        return -1;
88        }
89
90    // check start_arg in user space
91    if( start_arg != NULL ) error = vmm_v2p_translate( false , start_arg , &paddr );
92
93        if( error )
94        {
95                printk("\n[ERROR] in %s : start_arg unmapped\n", __FUNCTION__ );
96                parent->errno = EINVAL;
97        return -1;
98        }
99
100    // copy user_attr structure from user space to kernel space
101        hal_copy_from_uspace( &k_attr , user_attr , sizeof(pthread_attr_t) );
102
103    // check/set "cxy" attribute
104        if( k_attr.attributes & PT_ATTR_CLUSTER_DEFINED ) 
105    {
106        if( cluster_is_undefined( k_attr.cxy ) )
107        {
108                    printk("\n[ERROR] in %s : illegal target cluster attribute = %x\n",
109                   __FUNCTION__ , k_attr.cxy );
110                    parent->errno = EINVAL;
111            return -1;
112        } 
113    }
114    else
115    {
116        k_attr.cxy = dqdt_get_cluster_for_process();
117    }
118
119    // create the thread, using a RPC if required
120    // this returns "error", "child", and "child_xp"
121 
122    if( k_attr.cxy == local_cxy )                         // target cluster is local
123    {
124
125        // create thread in local cluster
126        error = thread_user_create( process->pid,
127                                    start_func,
128                                    start_arg,
129                                    &k_attr,
130                                    &child_ptr );
131
132        child_xp = XPTR( local_cxy , child_ptr );
133    }
134    else                                                 // target cluster is remote
135    {
136        rpc_thread_user_create_client( k_attr.cxy,
137                                       process->pid,
138                                       start_func,
139                                       start_arg,
140                                       &k_attr, 
141                                       &child_xp, 
142                                       &error );
143
144        child_ptr = (thread_t *)GET_PTR( child_xp );
145    }
146
147    // check successful thread creation
148    if( error ) 
149    {
150                printk("\n[ERROR] in %s : cannot create thread\n", __FUNCTION__ );
151        return ENOMEM;
152    }
153
154    // returns trdid to user space
155    trdid = hal_remote_lw( XPTR( k_attr.cxy , &child_ptr->trdid ) );   
156        hal_copy_to_uspace( new_thread , &trdid , sizeof(pthread_t) );
157   
158    // register new-thread in parent-thread children list if required
159    if( (k_attr.attributes & PT_ATTR_DETACH) == 0 ) 
160        thread_child_parent_link( parent_xp , child_xp );
161
162        tm_end = hal_time_stamp();
163
164        thread_dmsg("\n[INFO] %s created thread %x for process %x in cluster %x\n"
165                "  start_cycle = %d / end_cycle = %d\n",
166                   trdid , process->pid , k_attr.cxy , tm_start , tm_end );
167        return 0;
168
169}  // end sys_thread_create()
170
171
Note: See TracBrowser for help on using the repository browser.