[1] | 1 | /* |
---|
| 2 | * sys_thread_create.c - creates a new user thread |
---|
| 3 | * |
---|
[23] | 4 | * Author Alain Greiner (2016,2017) |
---|
[1] | 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 | |
---|
[14] | 24 | #include <kernel_config.h> |
---|
[1] | 25 | #include <hal_types.h> |
---|
[23] | 26 | #include <hal_uspace.h> |
---|
[1] | 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> |
---|
[23] | 39 | #include <rpc.h> |
---|
[1] | 40 | |
---|
| 41 | |
---|
| 42 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 43 | // This function implements the pthread_create system call |
---|
| 44 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 45 | int sys_thread_create ( thread_t * new_thread, // [out] argument |
---|
| 46 | pthread_attr_t * user_attr, // [in] argument |
---|
| 47 | void * start_func, // [in] argument |
---|
[23] | 48 | void * start_arg ) // [in] argument |
---|
[1] | 49 | { |
---|
[23] | 50 | pthread_attr_t k_attr; // copy of pthread attributes in kernel space |
---|
[1] | 51 | thread_t * parent; // pointer on thread executing the pthread_create |
---|
[23] | 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 |
---|
[1] | 55 | trdid_t trdid; // created thread identifier |
---|
| 56 | process_t * process; // pointer on local process descriptor |
---|
[23] | 57 | paddr_t paddr; // unused, required by vmm_v2p_translate() |
---|
[1] | 58 | error_t error; |
---|
| 59 | |
---|
| 60 | uint32_t tm_start; |
---|
| 61 | uint32_t tm_end; |
---|
| 62 | |
---|
[101] | 63 | tm_start = hal_get_cycles(); |
---|
[1] | 64 | |
---|
| 65 | // get parent thead pointer, extended pointer, and process pointer |
---|
| 66 | parent = CURRENT_THREAD; |
---|
[23] | 67 | parent_xp = XPTR( local_cxy , parent ); |
---|
[1] | 68 | process = parent->process; |
---|
| 69 | |
---|
[23] | 70 | // check user_attr in user space |
---|
| 71 | error = vmm_v2p_translate( false , user_attr , &paddr ); |
---|
| 72 | |
---|
| 73 | if( error ) |
---|
[1] | 74 | { |
---|
[23] | 75 | printk("\n[ERROR] in %s : user_attr unmapped\n", __FUNCTION__ ); |
---|
| 76 | parent->errno = EINVAL; |
---|
| 77 | return -1; |
---|
[1] | 78 | } |
---|
[23] | 79 | |
---|
| 80 | // check start_func in user space |
---|
| 81 | error = vmm_v2p_translate( false , start_func , &paddr ); |
---|
| 82 | |
---|
| 83 | if( error ) |
---|
[1] | 84 | { |
---|
[23] | 85 | printk("\n[ERROR] in %s : start_func unmapped\n", __FUNCTION__ ); |
---|
| 86 | parent->errno = EINVAL; |
---|
| 87 | return -1; |
---|
[1] | 88 | } |
---|
| 89 | |
---|
[23] | 90 | // check start_arg in user space |
---|
| 91 | if( start_arg != NULL ) error = vmm_v2p_translate( false , start_arg , &paddr ); |
---|
[1] | 92 | |
---|
[23] | 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 | |
---|
[1] | 103 | // check/set "cxy" attribute |
---|
[23] | 104 | if( k_attr.attributes & PT_ATTR_CLUSTER_DEFINED ) |
---|
[1] | 105 | { |
---|
[23] | 106 | if( cluster_is_undefined( k_attr.cxy ) ) |
---|
[1] | 107 | { |
---|
[23] | 108 | printk("\n[ERROR] in %s : illegal target cluster attribute = %x\n", |
---|
| 109 | __FUNCTION__ , k_attr.cxy ); |
---|
| 110 | parent->errno = EINVAL; |
---|
| 111 | return -1; |
---|
[1] | 112 | } |
---|
| 113 | } |
---|
| 114 | else |
---|
| 115 | { |
---|
[23] | 116 | k_attr.cxy = dqdt_get_cluster_for_process(); |
---|
[1] | 117 | } |
---|
| 118 | |
---|
| 119 | // create the thread, using a RPC if required |
---|
[23] | 120 | // this returns "error", "child", and "child_xp" |
---|
| 121 | |
---|
| 122 | if( k_attr.cxy == local_cxy ) // target cluster is local |
---|
[1] | 123 | { |
---|
| 124 | |
---|
| 125 | // create thread in local cluster |
---|
[23] | 126 | error = thread_user_create( process->pid, |
---|
| 127 | start_func, |
---|
| 128 | start_arg, |
---|
| 129 | &k_attr, |
---|
| 130 | &child_ptr ); |
---|
[1] | 131 | |
---|
[23] | 132 | child_xp = XPTR( local_cxy , child_ptr ); |
---|
[1] | 133 | } |
---|
| 134 | else // target cluster is remote |
---|
| 135 | { |
---|
[23] | 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 ); |
---|
[1] | 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 |
---|
[23] | 155 | trdid = hal_remote_lw( XPTR( k_attr.cxy , &child_ptr->trdid ) ); |
---|
[1] | 156 | hal_copy_to_uspace( new_thread , &trdid , sizeof(pthread_t) ); |
---|
| 157 | |
---|
| 158 | // register new-thread in parent-thread children list if required |
---|
[23] | 159 | if( (k_attr.attributes & PT_ATTR_DETACH) == 0 ) |
---|
| 160 | thread_child_parent_link( parent_xp , child_xp ); |
---|
[1] | 161 | |
---|
[101] | 162 | tm_end = hal_get_cycles(); |
---|
[1] | 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", |
---|
[23] | 166 | trdid , process->pid , k_attr.cxy , tm_start , tm_end ); |
---|
[1] | 167 | return 0; |
---|
| 168 | |
---|
| 169 | } // end sys_thread_create() |
---|
| 170 | |
---|
| 171 | |
---|