[1] | 1 | /* |
---|
| 2 | * sys_thread_create.c - creates a new user thread |
---|
[289] | 3 | * |
---|
[624] | 4 | * Author Alain Greiner (2016,2017,2018,2019) |
---|
[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> |
---|
[624] | 27 | #include <hal_vmm.h> |
---|
[1] | 28 | #include <printk.h> |
---|
| 29 | #include <errno.h> |
---|
| 30 | #include <core.h> |
---|
| 31 | #include <cluster.h> |
---|
| 32 | #include <list.h> |
---|
| 33 | #include <xlist.h> |
---|
| 34 | #include <thread.h> |
---|
| 35 | #include <scheduler.h> |
---|
| 36 | #include <kmem.h> |
---|
| 37 | #include <process.h> |
---|
| 38 | #include <dqdt.h> |
---|
[23] | 39 | #include <rpc.h> |
---|
[1] | 40 | |
---|
[506] | 41 | #include <syscalls.h> |
---|
[1] | 42 | |
---|
[566] | 43 | ///////////////////////////////////////////////////////// |
---|
| 44 | int sys_thread_create( trdid_t * trdid_ptr, |
---|
| 45 | struct pthread_attr_s * user_attr, |
---|
| 46 | void * start_func, |
---|
| 47 | void * start_args ) |
---|
[1] | 48 | { |
---|
[407] | 49 | pthread_attr_t kern_attr; // copy of pthread attributes in kernel space |
---|
[1] | 50 | thread_t * parent; // pointer on thread executing the pthread_create |
---|
[566] | 51 | xptr_t parent_xp; // extended pointer on calling thread |
---|
| 52 | cxy_t child_cxy; // created child thread cluster identifier |
---|
[289] | 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 |
---|
[289] | 58 | error_t error; |
---|
[1] | 59 | |
---|
[407] | 60 | // get parent thead pointer, extended pointer, and process |
---|
[1] | 61 | parent = CURRENT_THREAD; |
---|
[289] | 62 | parent_xp = XPTR( local_cxy , parent ); |
---|
[1] | 63 | process = parent->process; |
---|
| 64 | |
---|
[594] | 65 | #if (DEBUG_SYS_THREAD_CREATE || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
| 66 | uint64_t tm_start = hal_get_cycles(); |
---|
| 67 | #endif |
---|
| 68 | |
---|
[438] | 69 | #if DEBUG_SYS_THREAD_CREATE |
---|
[437] | 70 | tm_start = hal_get_cycles(); |
---|
[438] | 71 | if( DEBUG_SYS_THREAD_CREATE < tm_start ) |
---|
[594] | 72 | printk("\n[%s] thread[%x,%x] enter / cycle %d\n", |
---|
[619] | 73 | __FUNCTION__, process->pid, parent->trdid, (uint32_t)tm_start ); |
---|
[437] | 74 | #endif |
---|
| 75 | |
---|
[440] | 76 | // check trdid buffer in user space |
---|
[566] | 77 | error = vmm_get_vseg( process , (intptr_t)trdid_ptr , &vseg ); |
---|
[440] | 78 | |
---|
| 79 | if ( error ) |
---|
| 80 | { |
---|
| 81 | |
---|
| 82 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 83 | printk("\n[ERROR] in %s : thread[%x,%x] / trdid buffer %x unmapped %x\n", |
---|
| 84 | __FUNCTION__, process->pid, parent->trdid, (intptr_t)trdid_ptr ); |
---|
[624] | 85 | hal_vmm_display( process , false ); |
---|
[440] | 86 | #endif |
---|
| 87 | parent->errno = EINVAL; |
---|
| 88 | return -1; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | // check user_attr buffer in user space & copy to kernel space |
---|
[407] | 92 | if( user_attr != NULL ) |
---|
| 93 | { |
---|
[440] | 94 | error = vmm_get_vseg( process , (intptr_t)user_attr , &vseg ); |
---|
[23] | 95 | |
---|
[407] | 96 | if( error ) |
---|
| 97 | { |
---|
[437] | 98 | |
---|
[438] | 99 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 100 | printk("\n[ERROR] in %s : thread[%x,%x] / user_attr buffer unmapped %x\n", |
---|
| 101 | __FUNCTION__, process->pid, parent->trdid, (intptr_t)user_attr ); |
---|
[624] | 102 | hal_vmm_display( process , false ); |
---|
[437] | 103 | #endif |
---|
[407] | 104 | parent->errno = EINVAL; |
---|
| 105 | return -1; |
---|
| 106 | } |
---|
| 107 | |
---|
[626] | 108 | hal_copy_from_uspace( local_cxy, |
---|
| 109 | &kern_attr, |
---|
| 110 | user_attr, |
---|
| 111 | sizeof(pthread_attr_t) ); |
---|
[407] | 112 | } |
---|
[23] | 113 | |
---|
[289] | 114 | // check start_func in user space |
---|
[440] | 115 | error = vmm_get_vseg( process , (intptr_t)start_func , &vseg ); |
---|
[23] | 116 | |
---|
[440] | 117 | if( error ) |
---|
| 118 | { |
---|
[437] | 119 | |
---|
[438] | 120 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 121 | printk("\n[ERROR] in %s : thread[%x,%x] / start_func unmapped %x\n", |
---|
| 122 | __FUNCTION__, process->pid, parent->trdid, (intptr_t)start_func ); |
---|
[624] | 123 | hal_vmm_display( process , false ); |
---|
[437] | 124 | #endif |
---|
[440] | 125 | parent->errno = EINVAL; |
---|
| 126 | return -1; |
---|
[1] | 127 | } |
---|
| 128 | |
---|
[506] | 129 | // check start_args buffer in user space |
---|
| 130 | if( start_args != NULL ) |
---|
[440] | 131 | { |
---|
[506] | 132 | error = vmm_get_vseg( process , (intptr_t)start_args , &vseg ); |
---|
[1] | 133 | |
---|
[440] | 134 | if( error ) |
---|
| 135 | { |
---|
[437] | 136 | |
---|
[438] | 137 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 138 | printk("\n[ERROR] in %s : thread[%x,%x] / start_args buffer unmapped %x\n", |
---|
| 139 | __FUNCTION__, process->pid, parent->trdid, (intptr_t)start_args ); |
---|
[624] | 140 | hal_vmm_display( process , false ); |
---|
[437] | 141 | #endif |
---|
[440] | 142 | parent->errno = EINVAL; |
---|
| 143 | return -1; |
---|
| 144 | } |
---|
[23] | 145 | } |
---|
| 146 | |
---|
[566] | 147 | // define attributes and child_cxy |
---|
[407] | 148 | if( user_attr != NULL ) // user defined attributes |
---|
| 149 | { |
---|
[566] | 150 | // check / get child_cxy |
---|
[407] | 151 | if( kern_attr.attributes & PT_ATTR_CLUSTER_DEFINED ) |
---|
| 152 | { |
---|
| 153 | if( cluster_is_undefined( kern_attr.cxy ) ) |
---|
| 154 | { |
---|
[437] | 155 | |
---|
[438] | 156 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 157 | printk("\n[ERROR] in %s : thread[%x,%x] / illegal target cluster %x\n", |
---|
| 158 | __FUNCTION__, process->pid, parent->trdid, kern_attr.cxy ); |
---|
[437] | 159 | #endif |
---|
[407] | 160 | parent->errno = EINVAL; |
---|
| 161 | return -1; |
---|
| 162 | } |
---|
[566] | 163 | child_cxy = kern_attr.cxy; |
---|
[289] | 164 | } |
---|
[407] | 165 | else |
---|
| 166 | { |
---|
[566] | 167 | child_cxy = dqdt_get_cluster_for_process(); |
---|
[407] | 168 | } |
---|
[289] | 169 | } |
---|
[407] | 170 | else // set default attributes |
---|
[289] | 171 | { |
---|
[407] | 172 | kern_attr.attributes = PT_ATTR_DETACH | PT_ATTR_CLUSTER_DEFINED; |
---|
[566] | 173 | child_cxy = dqdt_get_cluster_for_process(); |
---|
[289] | 174 | } |
---|
[1] | 175 | |
---|
[289] | 176 | // create the thread, using a RPC if required |
---|
[407] | 177 | // this returns "error", "child_ptr", and "child_xp" |
---|
[1] | 178 | |
---|
[566] | 179 | if( child_cxy == local_cxy ) // target cluster is local |
---|
[289] | 180 | { |
---|
| 181 | // create thread in local cluster |
---|
| 182 | error = thread_user_create( process->pid, |
---|
| 183 | start_func, |
---|
[506] | 184 | start_args, |
---|
[407] | 185 | &kern_attr, |
---|
[289] | 186 | &child_ptr ); |
---|
[1] | 187 | |
---|
[289] | 188 | child_xp = XPTR( local_cxy , child_ptr ); |
---|
| 189 | } |
---|
| 190 | else // target cluster is remote |
---|
| 191 | { |
---|
[566] | 192 | rpc_thread_user_create_client( child_cxy, |
---|
[289] | 193 | process->pid, |
---|
| 194 | start_func, |
---|
[506] | 195 | start_args, |
---|
[407] | 196 | &kern_attr, |
---|
[289] | 197 | &child_xp, |
---|
| 198 | &error ); |
---|
[23] | 199 | |
---|
[289] | 200 | child_ptr = (thread_t *)GET_PTR( child_xp ); |
---|
| 201 | } |
---|
[1] | 202 | |
---|
[289] | 203 | // check successful thread creation |
---|
| 204 | if( error ) |
---|
| 205 | { |
---|
[437] | 206 | |
---|
[438] | 207 | #if DEBUG_SYSCALLS_ERROR |
---|
[594] | 208 | printk("\n[ERROR] in %s : thread[%x,%x] cannot create new thread\n", |
---|
| 209 | __FUNCTION__ , process->pid, parent->trdid ); |
---|
[437] | 210 | #endif |
---|
[440] | 211 | parent->errno = ENOMEM; |
---|
| 212 | return -1; |
---|
[289] | 213 | } |
---|
[1] | 214 | |
---|
[289] | 215 | // returns trdid to user space |
---|
[566] | 216 | trdid = hal_remote_l32( XPTR( child_cxy , &child_ptr->trdid ) ); |
---|
[626] | 217 | hal_copy_to_uspace( local_cxy, |
---|
| 218 | &trdid, |
---|
| 219 | trdid_ptr, |
---|
| 220 | sizeof(pthread_t) ); |
---|
[1] | 221 | |
---|
[407] | 222 | // activate new thread |
---|
| 223 | thread_unblock( child_xp , THREAD_BLOCKED_GLOBAL ); |
---|
| 224 | |
---|
| 225 | hal_fence(); |
---|
| 226 | |
---|
[594] | 227 | #if (DEBUG_SYS_THREAD_CREATE || CONFIG_INSTRUMENTATION_SYSCALLS) |
---|
| 228 | uint64_t tm_end = hal_get_cycles(); |
---|
| 229 | #endif |
---|
| 230 | |
---|
[438] | 231 | #if DEBUG_SYS_THREAD_CREATE |
---|
| 232 | if( DEBUG_SYS_THREAD_CREATE < tm_end ) |
---|
[594] | 233 | printk("\n[%s] thread[%x,%x] created thread %x / cycle %d\n", |
---|
| 234 | __FUNCTION__, process->pid, parent->trdid, child_ptr->trdid, (uint32_t)tm_end ); |
---|
[437] | 235 | #endif |
---|
[1] | 236 | |
---|
[594] | 237 | #if CONFIG_INSTRUMENTATION_SYSCALLS |
---|
| 238 | hal_atomic_add( &syscalls_cumul_cost[SYS_THREAD_CREATE] , tm_end - tm_start ); |
---|
| 239 | hal_atomic_add( &syscalls_occurences[SYS_THREAD_CREATE] , 1 ); |
---|
| 240 | #endif |
---|
| 241 | |
---|
[1] | 242 | return 0; |
---|
| 243 | |
---|
[407] | 244 | } // end sys_thread_create() |
---|
| 245 | |
---|