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