[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 | |
---|
| 42 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 43 | // This function implements the pthread_create system call |
---|
| 44 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 45 | int sys_thread_create ( thread_t * new_thread, // [out] argument |
---|
| 46 | pthread_attr_t * user_attr, // [in] argument |
---|
| 47 | void * start_func, // [in] argument |
---|
[23] | 48 | void * start_arg ) // [in] argument |
---|
[1] | 49 | { |
---|
[23] | 50 | pthread_attr_t k_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 |
---|
| 57 | paddr_t paddr; // unused, required by vmm_v2p_translate() |
---|
| 58 | error_t error; |
---|
[1] | 59 | |
---|
| 60 | uint32_t tm_start; |
---|
| 61 | uint32_t tm_end; |
---|
| 62 | |
---|
[101] | 63 | tm_start = hal_get_cycles(); |
---|
[1] | 64 | |
---|
[289] | 65 | // get parent thead pointer, extended pointer, and process pointer |
---|
[1] | 66 | parent = CURRENT_THREAD; |
---|
[289] | 67 | parent_xp = XPTR( local_cxy , parent ); |
---|
[1] | 68 | process = parent->process; |
---|
| 69 | |
---|
[289] | 70 | // check user_attr in user space |
---|
| 71 | error = vmm_v2p_translate( false , user_attr , &paddr ); |
---|
[23] | 72 | |
---|
| 73 | if( error ) |
---|
[1] | 74 | { |
---|
[23] | 75 | printk("\n[ERROR] in %s : user_attr unmapped\n", __FUNCTION__ ); |
---|
| 76 | parent->errno = EINVAL; |
---|
[289] | 77 | return -1; |
---|
[1] | 78 | } |
---|
[23] | 79 | |
---|
[289] | 80 | // check start_func in user space |
---|
| 81 | error = vmm_v2p_translate( false , start_func , &paddr ); |
---|
[23] | 82 | |
---|
| 83 | if( error ) |
---|
[1] | 84 | { |
---|
[23] | 85 | printk("\n[ERROR] in %s : start_func unmapped\n", __FUNCTION__ ); |
---|
| 86 | parent->errno = EINVAL; |
---|
[289] | 87 | return -1; |
---|
[1] | 88 | } |
---|
| 89 | |
---|
[289] | 90 | // check start_arg in user space |
---|
| 91 | if( start_arg != NULL ) error = vmm_v2p_translate( false , start_arg , &paddr ); |
---|
[1] | 92 | |
---|
[23] | 93 | if( error ) |
---|
| 94 | { |
---|
| 95 | printk("\n[ERROR] in %s : start_arg unmapped\n", __FUNCTION__ ); |
---|
| 96 | parent->errno = EINVAL; |
---|
[289] | 97 | return -1; |
---|
[23] | 98 | } |
---|
| 99 | |
---|
[289] | 100 | // copy user_attr structure from user space to kernel space |
---|
[23] | 101 | hal_copy_from_uspace( &k_attr , user_attr , sizeof(pthread_attr_t) ); |
---|
| 102 | |
---|
[289] | 103 | // check/set "cxy" attribute |
---|
| 104 | if( k_attr.attributes & PT_ATTR_CLUSTER_DEFINED ) |
---|
| 105 | { |
---|
| 106 | if( cluster_is_undefined( k_attr.cxy ) ) |
---|
| 107 | { |
---|
| 108 | printk("\n[ERROR] in %s : illegal target cluster attribute = %x\n", |
---|
| 109 | __FUNCTION__ , k_attr.cxy ); |
---|
| 110 | parent->errno = EINVAL; |
---|
| 111 | return -1; |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | else |
---|
| 115 | { |
---|
| 116 | k_attr.cxy = dqdt_get_cluster_for_process(); |
---|
| 117 | } |
---|
[1] | 118 | |
---|
[289] | 119 | // create the thread, using a RPC if required |
---|
| 120 | // this returns "error", "child", and "child_xp" |
---|
[1] | 121 | |
---|
[289] | 122 | if( k_attr.cxy == local_cxy ) // target cluster is local |
---|
| 123 | { |
---|
| 124 | // create thread in local cluster |
---|
| 125 | error = thread_user_create( process->pid, |
---|
| 126 | start_func, |
---|
| 127 | start_arg, |
---|
| 128 | &k_attr, |
---|
| 129 | &child_ptr ); |
---|
[1] | 130 | |
---|
[289] | 131 | child_xp = XPTR( local_cxy , child_ptr ); |
---|
| 132 | } |
---|
| 133 | else // target cluster is remote |
---|
| 134 | { |
---|
| 135 | rpc_thread_user_create_client( k_attr.cxy, |
---|
| 136 | process->pid, |
---|
| 137 | start_func, |
---|
| 138 | start_arg, |
---|
| 139 | &k_attr, |
---|
| 140 | &child_xp, |
---|
| 141 | &error ); |
---|
[23] | 142 | |
---|
[289] | 143 | child_ptr = (thread_t *)GET_PTR( child_xp ); |
---|
| 144 | } |
---|
[1] | 145 | |
---|
[289] | 146 | // check successful thread creation |
---|
| 147 | if( error ) |
---|
| 148 | { |
---|
[1] | 149 | printk("\n[ERROR] in %s : cannot create thread\n", __FUNCTION__ ); |
---|
[289] | 150 | return ENOMEM; |
---|
| 151 | } |
---|
[1] | 152 | |
---|
[289] | 153 | // returns trdid to user space |
---|
| 154 | trdid = hal_remote_lw( XPTR( k_attr.cxy , &child_ptr->trdid ) ); |
---|
[1] | 155 | hal_copy_to_uspace( new_thread , &trdid , sizeof(pthread_t) ); |
---|
| 156 | |
---|
[289] | 157 | // register new-thread in parent-thread children list if required |
---|
| 158 | if( (k_attr.attributes & PT_ATTR_DETACH) == 0 ) |
---|
| 159 | thread_child_parent_link( parent_xp , child_xp ); |
---|
| 160 | |
---|
[101] | 161 | tm_end = hal_get_cycles(); |
---|
[1] | 162 | |
---|
| 163 | thread_dmsg("\n[INFO] %s created thread %x for process %x in cluster %x\n" |
---|
[289] | 164 | " start_cycle = %d / end_cycle = %d\n", |
---|
| 165 | trdid , process->pid , k_attr.cxy , tm_start , tm_end ); |
---|
[1] | 166 | return 0; |
---|
[289] | 167 | } |
---|
[1] | 168 | |
---|