[1] | 1 | /* |
---|
| 2 | * sys_thread_create.c - creates a new user thread |
---|
[289] | 3 | * |
---|
[437] | 4 | * Author Alain Greiner (2016,2017,2018) |
---|
[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> |
---|
[457] | 25 | #include <hal_kernel_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 | |
---|
[506] | 41 | #include <syscalls.h> |
---|
[1] | 42 | |
---|
[407] | 43 | /////////////////////////////////////////////////// |
---|
[506] | 44 | int 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 ) |
---|
[1] | 49 | { |
---|
[407] | 50 | pthread_attr_t kern_attr; // copy of pthread attributes in kernel space |
---|
[1] | 51 | thread_t * parent; // pointer on thread executing the pthread_create |
---|
[289] | 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 |
---|
[440] | 57 | vseg_t * vseg; // required for user space checking |
---|
[407] | 58 | cxy_t target_cxy; // target cluster identifier |
---|
[289] | 59 | error_t error; |
---|
[1] | 60 | |
---|
[407] | 61 | // get parent thead pointer, extended pointer, and process |
---|
[1] | 62 | parent = CURRENT_THREAD; |
---|
[289] | 63 | parent_xp = XPTR( local_cxy , parent ); |
---|
[1] | 64 | process = parent->process; |
---|
| 65 | |
---|
[438] | 66 | #if DEBUG_SYS_THREAD_CREATE |
---|
[437] | 67 | uint64_t tm_start; |
---|
| 68 | uint64_t tm_end; |
---|
| 69 | tm_start = hal_get_cycles(); |
---|
[438] | 70 | if( DEBUG_SYS_THREAD_CREATE < tm_start ) |
---|
[440] | 71 | printk("\n[DBG] %s : thread %x (cxy %x) enter / process %x / cycle %d\n", |
---|
| 72 | __FUNCTION__, parent, local_cxy, process->pid, (uint32_t)tm_start ); |
---|
[437] | 73 | #endif |
---|
| 74 | |
---|
[440] | 75 | // check trdid buffer in user space |
---|
[506] | 76 | error = vmm_get_vseg( process , (intptr_t)new_thread , &vseg ); |
---|
[440] | 77 | |
---|
| 78 | if ( error ) |
---|
| 79 | { |
---|
| 80 | |
---|
| 81 | #if DEBUG_SYSCALLS_ERROR |
---|
| 82 | printk("\n[ERROR] in %s : trdid buffer unmapped %x / thread %x / process %x\n", |
---|
[506] | 83 | __FUNCTION__ , (intptr_t)new_thread, parent->trdid, process->pid ); |
---|
[440] | 84 | vmm_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 |
---|
[407] | 91 | if( user_attr != NULL ) |
---|
| 92 | { |
---|
[440] | 93 | error = vmm_get_vseg( process , (intptr_t)user_attr , &vseg ); |
---|
[23] | 94 | |
---|
[407] | 95 | if( error ) |
---|
| 96 | { |
---|
[437] | 97 | |
---|
[438] | 98 | #if DEBUG_SYSCALLS_ERROR |
---|
[440] | 99 | printk("\n[ERROR] in %s : user_attr buffer unmapped %x / thread %x / process %x\n", |
---|
| 100 | __FUNCTION__ , (intptr_t)user_attr , parent->trdid , process->pid ); |
---|
| 101 | vmm_display( process , false ); |
---|
[437] | 102 | #endif |
---|
[407] | 103 | parent->errno = EINVAL; |
---|
| 104 | return -1; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | hal_copy_from_uspace( &kern_attr , user_attr , sizeof(pthread_attr_t) ); |
---|
| 108 | } |
---|
[23] | 109 | |
---|
[289] | 110 | // check start_func in user space |
---|
[440] | 111 | error = vmm_get_vseg( process , (intptr_t)start_func , &vseg ); |
---|
[23] | 112 | |
---|
[440] | 113 | if( error ) |
---|
| 114 | { |
---|
[437] | 115 | |
---|
[438] | 116 | #if DEBUG_SYSCALLS_ERROR |
---|
[440] | 117 | printk("\n[ERROR] in %s : start_func unmapped %x / thread %x / process %x\n", |
---|
| 118 | __FUNCTION__ , (intptr_t)start_func , parent->trdid , process->pid ); |
---|
| 119 | vmm_display( process , false ); |
---|
[437] | 120 | #endif |
---|
[440] | 121 | parent->errno = EINVAL; |
---|
| 122 | return -1; |
---|
[1] | 123 | } |
---|
| 124 | |
---|
[506] | 125 | // check start_args buffer in user space |
---|
| 126 | if( start_args != NULL ) |
---|
[440] | 127 | { |
---|
[506] | 128 | error = vmm_get_vseg( process , (intptr_t)start_args , &vseg ); |
---|
[1] | 129 | |
---|
[440] | 130 | if( error ) |
---|
| 131 | { |
---|
[437] | 132 | |
---|
[438] | 133 | #if DEBUG_SYSCALLS_ERROR |
---|
[506] | 134 | printk("\n[ERROR] in %s : start_args buffer unmapped %x / thread %x / process %x\n", |
---|
| 135 | __FUNCTION__ , (intptr_t)start_args , parent->trdid , process->pid ); |
---|
[440] | 136 | vmm_display( process , false ); |
---|
[437] | 137 | #endif |
---|
[440] | 138 | parent->errno = EINVAL; |
---|
| 139 | return -1; |
---|
| 140 | } |
---|
[23] | 141 | } |
---|
| 142 | |
---|
[440] | 143 | // define attributes and target_cxy |
---|
[407] | 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 | { |
---|
[437] | 151 | |
---|
[438] | 152 | #if DEBUG_SYSCALLS_ERROR |
---|
[440] | 153 | printk("\n[ERROR] in %s : illegal target cluster = %x / thread %x / process %x\n", |
---|
| 154 | __FUNCTION__ , kern_attr.cxy, parent->trdid, process->pid ); |
---|
[437] | 155 | #endif |
---|
[407] | 156 | parent->errno = EINVAL; |
---|
| 157 | return -1; |
---|
| 158 | } |
---|
| 159 | target_cxy = kern_attr.cxy; |
---|
[289] | 160 | } |
---|
[407] | 161 | else |
---|
| 162 | { |
---|
| 163 | target_cxy = dqdt_get_cluster_for_process(); |
---|
| 164 | } |
---|
[289] | 165 | } |
---|
[407] | 166 | else // set default attributes |
---|
[289] | 167 | { |
---|
[407] | 168 | kern_attr.attributes = PT_ATTR_DETACH | PT_ATTR_CLUSTER_DEFINED; |
---|
| 169 | target_cxy = dqdt_get_cluster_for_process(); |
---|
[289] | 170 | } |
---|
[1] | 171 | |
---|
[289] | 172 | // create the thread, using a RPC if required |
---|
[407] | 173 | // this returns "error", "child_ptr", and "child_xp" |
---|
[1] | 174 | |
---|
[407] | 175 | if( target_cxy == local_cxy ) // target cluster is local |
---|
[289] | 176 | { |
---|
| 177 | // create thread in local cluster |
---|
| 178 | error = thread_user_create( process->pid, |
---|
| 179 | start_func, |
---|
[506] | 180 | start_args, |
---|
[407] | 181 | &kern_attr, |
---|
[289] | 182 | &child_ptr ); |
---|
[1] | 183 | |
---|
[289] | 184 | child_xp = XPTR( local_cxy , child_ptr ); |
---|
| 185 | } |
---|
| 186 | else // target cluster is remote |
---|
| 187 | { |
---|
[407] | 188 | rpc_thread_user_create_client( target_cxy, |
---|
[289] | 189 | process->pid, |
---|
| 190 | start_func, |
---|
[506] | 191 | start_args, |
---|
[407] | 192 | &kern_attr, |
---|
[289] | 193 | &child_xp, |
---|
| 194 | &error ); |
---|
[23] | 195 | |
---|
[289] | 196 | child_ptr = (thread_t *)GET_PTR( child_xp ); |
---|
| 197 | } |
---|
[1] | 198 | |
---|
[289] | 199 | // check successful thread creation |
---|
| 200 | if( error ) |
---|
| 201 | { |
---|
[437] | 202 | |
---|
[438] | 203 | #if DEBUG_SYSCALLS_ERROR |
---|
[440] | 204 | printk("\n[ERROR] in %s : cannot create new thread / thread %x / process %x\n", |
---|
| 205 | __FUNCTION__ , parent->trdid, process->pid ); |
---|
[437] | 206 | #endif |
---|
[440] | 207 | parent->errno = ENOMEM; |
---|
| 208 | return -1; |
---|
[289] | 209 | } |
---|
[1] | 210 | |
---|
[289] | 211 | // returns trdid to user space |
---|
[407] | 212 | trdid = hal_remote_lw( XPTR( target_cxy , &child_ptr->trdid ) ); |
---|
[506] | 213 | hal_copy_to_uspace( new_thread , &trdid , sizeof(pthread_t) ); |
---|
[1] | 214 | |
---|
[407] | 215 | // activate new thread |
---|
| 216 | thread_unblock( child_xp , THREAD_BLOCKED_GLOBAL ); |
---|
| 217 | |
---|
| 218 | hal_fence(); |
---|
| 219 | |
---|
[438] | 220 | #if DEBUG_SYS_THREAD_CREATE |
---|
[437] | 221 | tm_end = hal_get_cycles(); |
---|
[438] | 222 | if( DEBUG_SYS_THREAD_CREATE < tm_end ) |
---|
[440] | 223 | printk("\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 ); |
---|
[437] | 225 | #endif |
---|
[1] | 226 | |
---|
| 227 | return 0; |
---|
| 228 | |
---|
[407] | 229 | } // end sys_thread_create() |
---|
| 230 | |
---|