[1] | 1 | /* |
---|
| 2 | * sys_thread_create.c - creates a new user thread |
---|
[289] | 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 | |
---|
[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 | |
---|
| 59 | uint32_t tm_start; |
---|
| 60 | uint32_t tm_end; |
---|
| 61 | |
---|
[101] | 62 | tm_start = hal_get_cycles(); |
---|
[1] | 63 | |
---|
[407] | 64 | // get parent thead pointer, extended pointer, and process |
---|
[1] | 65 | parent = CURRENT_THREAD; |
---|
[289] | 66 | parent_xp = XPTR( local_cxy , parent ); |
---|
[1] | 67 | process = parent->process; |
---|
| 68 | |
---|
[407] | 69 | // check user_attr in user space & copy to kernel space |
---|
| 70 | if( user_attr != NULL ) |
---|
| 71 | { |
---|
| 72 | error = vmm_v2p_translate( false , user_attr , &paddr ); |
---|
[23] | 73 | |
---|
[407] | 74 | if( error ) |
---|
| 75 | { |
---|
| 76 | printk("\n[ERROR] in %s : user_attr unmapped\n", __FUNCTION__ ); |
---|
| 77 | parent->errno = EINVAL; |
---|
| 78 | return -1; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | hal_copy_from_uspace( &kern_attr , user_attr , sizeof(pthread_attr_t) ); |
---|
| 82 | } |
---|
[23] | 83 | |
---|
[289] | 84 | // check start_func in user space |
---|
| 85 | error = vmm_v2p_translate( false , start_func , &paddr ); |
---|
[23] | 86 | |
---|
| 87 | if( error ) |
---|
[1] | 88 | { |
---|
[23] | 89 | printk("\n[ERROR] in %s : start_func unmapped\n", __FUNCTION__ ); |
---|
| 90 | parent->errno = EINVAL; |
---|
[289] | 91 | return -1; |
---|
[1] | 92 | } |
---|
| 93 | |
---|
[289] | 94 | // check start_arg in user space |
---|
| 95 | if( start_arg != NULL ) error = vmm_v2p_translate( false , start_arg , &paddr ); |
---|
[1] | 96 | |
---|
[23] | 97 | if( error ) |
---|
| 98 | { |
---|
| 99 | printk("\n[ERROR] in %s : start_arg unmapped\n", __FUNCTION__ ); |
---|
| 100 | parent->errno = EINVAL; |
---|
[289] | 101 | return -1; |
---|
[23] | 102 | } |
---|
| 103 | |
---|
[407] | 104 | // check / define attributes an target_cxy |
---|
| 105 | if( user_attr != NULL ) // user defined attributes |
---|
| 106 | { |
---|
| 107 | // check / get target_cxy |
---|
| 108 | if( kern_attr.attributes & PT_ATTR_CLUSTER_DEFINED ) |
---|
| 109 | { |
---|
| 110 | if( cluster_is_undefined( kern_attr.cxy ) ) |
---|
| 111 | { |
---|
| 112 | printk("\n[ERROR] in %s : illegal target cluster = %x\n", |
---|
| 113 | __FUNCTION__ , kern_attr.cxy ); |
---|
| 114 | parent->errno = EINVAL; |
---|
| 115 | return -1; |
---|
| 116 | } |
---|
| 117 | target_cxy = kern_attr.cxy; |
---|
[289] | 118 | } |
---|
[407] | 119 | else |
---|
| 120 | { |
---|
| 121 | target_cxy = dqdt_get_cluster_for_process(); |
---|
| 122 | } |
---|
[289] | 123 | } |
---|
[407] | 124 | else // set default attributes |
---|
[289] | 125 | { |
---|
[407] | 126 | kern_attr.attributes = PT_ATTR_DETACH | PT_ATTR_CLUSTER_DEFINED; |
---|
| 127 | target_cxy = dqdt_get_cluster_for_process(); |
---|
[289] | 128 | } |
---|
[1] | 129 | |
---|
[289] | 130 | // create the thread, using a RPC if required |
---|
[407] | 131 | // this returns "error", "child_ptr", and "child_xp" |
---|
[1] | 132 | |
---|
[407] | 133 | if( target_cxy == local_cxy ) // target cluster is local |
---|
[289] | 134 | { |
---|
| 135 | // create thread in local cluster |
---|
| 136 | error = thread_user_create( process->pid, |
---|
| 137 | start_func, |
---|
| 138 | start_arg, |
---|
[407] | 139 | &kern_attr, |
---|
[289] | 140 | &child_ptr ); |
---|
[1] | 141 | |
---|
[289] | 142 | child_xp = XPTR( local_cxy , child_ptr ); |
---|
| 143 | } |
---|
| 144 | else // target cluster is remote |
---|
| 145 | { |
---|
[407] | 146 | rpc_thread_user_create_client( target_cxy, |
---|
[289] | 147 | process->pid, |
---|
| 148 | start_func, |
---|
| 149 | start_arg, |
---|
[407] | 150 | &kern_attr, |
---|
[289] | 151 | &child_xp, |
---|
| 152 | &error ); |
---|
[23] | 153 | |
---|
[289] | 154 | child_ptr = (thread_t *)GET_PTR( child_xp ); |
---|
| 155 | } |
---|
[1] | 156 | |
---|
[289] | 157 | // check successful thread creation |
---|
| 158 | if( error ) |
---|
| 159 | { |
---|
[1] | 160 | printk("\n[ERROR] in %s : cannot create thread\n", __FUNCTION__ ); |
---|
[289] | 161 | return ENOMEM; |
---|
| 162 | } |
---|
[1] | 163 | |
---|
[289] | 164 | // returns trdid to user space |
---|
[407] | 165 | trdid = hal_remote_lw( XPTR( target_cxy , &child_ptr->trdid ) ); |
---|
| 166 | hal_copy_to_uspace( trdid_ptr , &trdid , sizeof(pthread_t) ); |
---|
[1] | 167 | |
---|
[407] | 168 | // register child in parent if required |
---|
| 169 | if( user_attr != NULL ) |
---|
| 170 | { |
---|
| 171 | if( (kern_attr.attributes & PT_ATTR_DETACH) == 0 ) |
---|
| 172 | thread_child_parent_link( parent_xp , child_xp ); |
---|
| 173 | } |
---|
[289] | 174 | |
---|
[407] | 175 | // activate new thread |
---|
| 176 | thread_unblock( child_xp , THREAD_BLOCKED_GLOBAL ); |
---|
| 177 | |
---|
| 178 | hal_fence(); |
---|
| 179 | |
---|
[101] | 180 | tm_end = hal_get_cycles(); |
---|
[1] | 181 | |
---|
[407] | 182 | syscall_dmsg("\n[DBG] %s : core[%x,%d] created thread %x for process %x / cycle %d\n" |
---|
| 183 | " cluster %x / cost = %d cycles\n", |
---|
| 184 | __FUNCTION__ , local_cxy , parent->core->lid , trdid , process->pid , tm_end , |
---|
| 185 | target_cxy , tm_end - tm_start ); |
---|
| 186 | |
---|
[1] | 187 | return 0; |
---|
| 188 | |
---|
[407] | 189 | } // end sys_thread_create() |
---|
| 190 | |
---|