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