source: rtems/cpukit/posix/src/pthreadcreate.c @ 51e008dd

5
Last change on this file since 51e008dd was 51e008dd, checked in by Sebastian Huber <sebastian.huber@…>, on 06/23/16 at 09:49:22

posix: Fix pthread_create()

Do not access data of the executing thread without proper locks.

  • Property mode set to 100644
File size: 7.3 KB
RevLine 
[65f6d3c]1/**
2 * @file
3 *
4 * @brief Function Starts a New Thread in The Calling Process
[5cb175bb]5 * @ingroup POSIXAPI
[65f6d3c]6 */
7
[03598b1]8/*
9 *  16.1.2 Thread Creation, P1003.1c/Draft 10, p. 144
[412dbff6]10 */
11
[68c156a]12/*
13 *  COPYRIGHT (c) 1989-2014.
[03598b1]14 *  On-Line Applications Research Corporation (OAR).
15 *
16 *  The license and distribution terms for this file may be
17 *  found in the file LICENSE in this distribution or at
[c499856]18 *  http://www.rtems.org/license/LICENSE.
[03598b1]19 */
20
[f42b726]21#if HAVE_CONFIG_H
22#include "config.h"
23#endif
24
[03598b1]25#include <pthread.h>
26#include <errno.h>
27
[97552c98]28#include <rtems/posix/priorityimpl.h>
[5618c37a]29#include <rtems/posix/pthreadimpl.h>
[51e008dd]30#include <rtems/score/assert.h>
[185e46f]31#include <rtems/score/cpusetimpl.h>
[5618c37a]32#include <rtems/score/threadimpl.h>
[5088d97]33#include <rtems/score/apimutex.h>
[218286bc]34#include <rtems/score/stackimpl.h>
[e045fb6c]35#include <rtems/score/schedulerimpl.h>
36
[ecf0f4c]37static inline size_t _POSIX_Threads_Ensure_minimum_stack (
38  size_t size
39)
40{
41  if ( size >= PTHREAD_MINIMUM_STACK_SIZE )
42    return size;
43  return PTHREAD_MINIMUM_STACK_SIZE;
44}
45
46
[03598b1]47int pthread_create(
48  pthread_t              *thread,
49  const pthread_attr_t   *attr,
50  void                 *(*start_routine)( void * ),
51  void                   *arg
52)
53{
[ccd5434]54  Thread_Entry_information entry = {
55    .adaptor = _Thread_Entry_adaptor_pointer,
56    .Kinds = {
57      .Pointer = {
58        .entry = start_routine,
59        .argument = arg
60      }
61    }
62  };
[03598b1]63  const pthread_attr_t               *the_attr;
[eec08ef]64  int                                 low_prio;
65  int                                 high_prio;
[1a4eac50]66  bool                                valid;
[eec08ef]67  Priority_Control                    core_low_prio;
68  Priority_Control                    core_high_prio;
[03598b1]69  Thread_CPU_budget_algorithms        budget_algorithm;
70  Thread_CPU_budget_algorithm_callout budget_callout;
[f8437c8]71  bool                                is_fp;
72  bool                                status;
[03598b1]73  Thread_Control                     *the_thread;
[c5831a3f]74  Thread_Control                     *executing;
[5a32c48]75  const Scheduler_Control            *scheduler;
[03598b1]76  POSIX_API_Control                  *api;
77  int                                 schedpolicy = SCHED_RR;
78  struct sched_param                  schedparam;
[ce19f1fa]79  Objects_Name                        name;
[eec08ef]80  int                                 error;
[e135271]81  ISR_lock_Context                    lock_context;
[03598b1]82
[e889a857]83  if ( !start_routine )
84    return EFAULT;
85
[03598b1]86  the_attr = (attr) ? attr : &_POSIX_Threads_Default_attributes;
87
88  if ( !the_attr->is_initialized )
89    return EINVAL;
90
91  /*
[ecf0f4c]92   *  Core Thread Initialize ensures we get the minimum amount of
[03598b1]93   *  stack space if it is allowed to allocate it itself.
[ecf0f4c]94   *
95   *  NOTE: If the user provides the stack we will let it drop below
96   *        twice the minimum.
[03598b1]97   */
[ecf0f4c]98  if ( the_attr->stackaddr && !_Stack_Is_enough(the_attr->stacksize) )
[03598b1]99    return EINVAL;
100
[2212a2ad]101  #if 0
102    int  cputime_clock_allowed;  /* see time.h */
103    rtems_set_errno_and_return_minus_one( ENOSYS );
104  #endif
[03598b1]105
[c5831a3f]106  executing = _Thread_Get_executing();
107
[03598b1]108  /*
109   *  P1003.1c/Draft 10, p. 121.
110   *
111   *  If inheritsched is set to PTHREAD_INHERIT_SCHED, then this thread
112   *  inherits scheduling attributes from the creating thread.   If it is
[874297f3]113   *  PTHREAD_EXPLICIT_SCHED, then scheduling parameters come from the
[03598b1]114   *  attributes structure.
115   */
116  switch ( the_attr->inheritsched ) {
117    case PTHREAD_INHERIT_SCHED:
[51e008dd]118      error = pthread_getschedparam(
119        pthread_self(),
120        &schedpolicy,
121        &schedparam
122      );
123      _Assert( error == 0 );
[874297f3]124      break;
[03598b1]125
126    case PTHREAD_EXPLICIT_SCHED:
127      schedpolicy = the_attr->schedpolicy;
128      schedparam  = the_attr->schedparam;
[874297f3]129      break;
[03598b1]130
131    default:
132      return EINVAL;
133  }
134
135  /*
[874297f3]136   *  Check the contentionscope since rtems only supports PROCESS wide
[03598b1]137   *  contention (i.e. no system wide contention).
138   */
139  if ( the_attr->contentionscope != PTHREAD_SCOPE_PROCESS )
140    return ENOTSUP;
141
[eec08ef]142  error = _POSIX_Thread_Translate_sched_param(
[2212a2ad]143    schedpolicy,
144    &schedparam,
145    &budget_algorithm,
146    &budget_callout
147  );
[eec08ef]148  if ( error != 0 ) {
149    return error;
150  }
151
152  if ( schedpolicy == SCHED_SPORADIC ) {
153    low_prio = schedparam.sched_ss_low_priority;
154    high_prio = schedparam.sched_priority;
155  } else {
156    low_prio = schedparam.sched_priority;
157    high_prio = low_prio;
158  }
159
[5a32c48]160  scheduler = _Scheduler_Get_own( executing );
161
[1a4eac50]162  core_low_prio = _POSIX_Priority_To_core( scheduler, low_prio, &valid );
163  if ( !valid ) {
[eec08ef]164    return EINVAL;
165  }
166
[1a4eac50]167  core_high_prio = _POSIX_Priority_To_core( scheduler, high_prio, &valid );
168  if ( !valid ) {
[eec08ef]169    return EINVAL;
170  }
171
[185e46f]172#if defined(RTEMS_SMP)
173#if __RTEMS_HAVE_SYS_CPUSET_H__
[e045fb6c]174  status = _CPU_set_Is_valid( the_attr->affinityset, the_attr->affinitysetsize );
[68c156a]175  if ( !status )
[185e46f]176    return EINVAL;
177#endif
178#endif
179
[03598b1]180  /*
[874297f3]181   *  Currently all POSIX threads are floating point if the hardware
[03598b1]182   *  supports it.
183   */
[335e5ca]184  is_fp = true;
[03598b1]185
186  /*
187   *  Allocate the thread control block.
188   *
189   *  NOTE:  Global threads are not currently supported.
190   */
191  the_thread = _POSIX_Threads_Allocate();
192  if ( !the_thread ) {
[23fec9f0]193    _Objects_Allocator_unlock();
[03598b1]194    return EAGAIN;
195  }
196
197  /*
198   *  Initialize the core thread for this task.
199   */
[ce19f1fa]200  name.name_p = NULL;   /* posix threads don't have a name by default */
[03598b1]201  status = _Thread_Initialize(
202    &_POSIX_Threads_Information,
203    the_thread,
[77ff5599]204    scheduler,
[03598b1]205    the_attr->stackaddr,
[ecf0f4c]206    _POSIX_Threads_Ensure_minimum_stack(the_attr->stacksize),
[03598b1]207    is_fp,
[eec08ef]208    core_high_prio,
[b1dbfd7]209    true,                 /* preemptible */
[03598b1]210    budget_algorithm,
211    budget_callout,
212    0,                    /* isr level */
[ce19f1fa]213    name                  /* posix threads don't have a name */
[03598b1]214  );
215  if ( !status ) {
216    _POSIX_Threads_Free( the_thread );
[23fec9f0]217    _Objects_Allocator_unlock();
[03598b1]218    return EAGAIN;
219  }
220
[54550e04]221  if ( the_attr->detachstate == PTHREAD_CREATE_DETACHED ) {
222    the_thread->Life.state |= THREAD_LIFE_DETACHED;
223  }
224
[da826560]225  the_thread->Life.state |= THREAD_LIFE_CHANGE_DEFERRED;
226
[68c156a]227#if defined(RTEMS_SMP) && __RTEMS_HAVE_SYS_CPUSET_H__
[e135271]228  _ISR_lock_ISR_disable( &lock_context );
[68c156a]229   status = _Scheduler_Set_affinity(
230     the_thread,
231     the_attr->affinitysetsize,
232     the_attr->affinityset
[e045fb6c]233   );
[e135271]234  _ISR_lock_ISR_enable( &lock_context );
[68c156a]235   if ( !status ) {
236     _POSIX_Threads_Free( the_thread );
237     _RTEMS_Unlock_allocator();
238     return EINVAL;
239   }
[185e46f]240#endif
241
[03598b1]242  /*
243   *  finish initializing the per API structure
244   */
245  api = the_thread->API_Extensions[ THREAD_API_POSIX ];
246
[185e46f]247  _POSIX_Threads_Copy_attributes( &api->Attributes, the_attr );
[eec08ef]248  api->Sporadic.low_priority = core_low_prio;
249  api->Sporadic.high_priority = core_high_prio;
[03598b1]250
[917884c]251  if ( schedpolicy == SCHED_SPORADIC ) {
252    _ISR_lock_ISR_disable( &lock_context );
253    _POSIX_Threads_Sporadic_timer_insert( the_thread, api );
254    _ISR_lock_ISR_enable( &lock_context );
255  }
256
[03598b1]257  /*
258   *  POSIX threads are allocated and started in one operation.
259   */
[33829ce]260  _ISR_lock_ISR_disable( &lock_context );
261  status = _Thread_Start( the_thread, &entry, &lock_context );
[03598b1]262
[2212a2ad]263  #if defined(RTEMS_DEBUG)
264    /*
265     *  _Thread_Start only fails if the thread was in the incorrect state
[1de949a8]266     *
[2212a2ad]267     *  NOTE: This can only happen if someone slips in and touches the
268     *        thread while we are creating it.
269     */
270    if ( !status ) {
271      _POSIX_Threads_Free( the_thread );
[23fec9f0]272      _Objects_Allocator_unlock();
[2212a2ad]273      return EINVAL;
274    }
275  #endif
276
[03598b1]277  /*
278   *  Return the id and indicate we successfully created the thread
279   */
280  *thread = the_thread->Object.id;
281
[23fec9f0]282  _Objects_Allocator_unlock();
[5088d97]283  return 0;
[03598b1]284}
Note: See TracBrowser for help on using the repository browser.