summaryrefslogtreecommitdiffstats
path: root/cpukit/score/src/smplock.c
blob: 9ef4e070423d47004a8f21e21e3c4a8c5cfa8bff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 *  COPYRIGHT (c) 1989-2011.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <rtems/system.h>
#include <rtems/score/smplock.h>
#include <rtems/score/smp.h>
#include <rtems/score/isr.h>

/*
 * Some debug stuff that is being left in, but disabled.  This will keep 
 * a log of lock/unlock sequences that can be printed out when the 
 * lockcount appears to have gotten off track.
 */
/* #define SMPLOCK_DEBUG */
#if defined (SMPLOCK_DEBUG)
  #include <rtems/score/thread.h>
  #include <rtems/bspIo.h>
  #include <rtems/score/percpu.h>
  #if (0)
    #define  ENABLE_ONESHOT_DEBUG_LOGGING
  #else
    #define ENABLE_LOOPED_DEBUG_LOGGING
  #endif
  #define ENABLE_DEBUG_LOGGING 
#endif

/*
 * Prototypes and structures used in the debug lock/unlock error log.
 */
#if defined(ENABLE_DEBUG_LOGGING)
  typedef struct {
    char      action;
    char      lock;
    char      cpu;
    char      count;
    uint32_t  nest_level;
    void      *ret1;
    void      *ret2;
    void      *ret3;
    void      *ret4;
  } debug_spinlog_t;

  extern void start(void);
  extern void _fini(void);

  #define DEBUG_SPINLOG_MAX 1024
  debug_spinlog_t DEBUG_SPINLOG[DEBUG_SPINLOG_MAX];
  int DEBUG_SPINLOG_INDEX = 0;

  static void debug_logit(
    char                             act,
    SMP_lock_spinlock_nested_Control *lock
  );
  static void debug_dump_log(void);
#else
  #define debug_logit( _act, _lock )
  #define debug_dump_log()
#endif

/*
 * SMP spinlock simple methods
 */
void _SMP_lock_spinlock_simple_Initialize(
  SMP_lock_spinlock_simple_Control *lock
)
{
  *lock = 0;
}

ISR_Level _SMP_lock_spinlock_simple_Obtain(
  SMP_lock_spinlock_simple_Control *lock
)
{
   ISR_Level  level = 0;
   uint32_t   value = 1;
   uint32_t   previous;

   /* Note: Disable provides an implicit memory barrier. */
  _ISR_Disable_on_this_core( level );
   do {
     RTEMS_COMPILER_MEMORY_BARRIER();
     SMP_CPU_SWAP( lock, value, previous );
     RTEMS_COMPILER_MEMORY_BARRIER();
   } while (previous == 1);

  return level;
}

void _SMP_lock_spinlock_simple_Release(
  SMP_lock_spinlock_simple_Control *lock,
  ISR_Level                        level
)
{
   *lock = 0;
   _ISR_Enable_on_this_core( level );
}

/*
 * SMP spinlock nested methods.
 */
void _SMP_lock_spinlock_nested_Initialize(
  SMP_lock_spinlock_nested_Control *lock
)
{
  lock->lock = 0;
  lock->count = 0;
  lock->cpu_id = -1;
}

void _SMP_lock_spinlock_nested_Release(
  SMP_lock_spinlock_nested_Control *lock,
  ISR_Level                        level
)
{
  #if defined (RTEMS_DEBUG) || defined(SMPLOCK_DEBUG)
    if ( lock->count == 0 ) {
      printk(
        "\ncpu %d lock %d Releasing spinlock when count is already "
        "zero (%p from %p,%p)?!?!\n",
        bsp_smp_processor_id(),  
        lock->cpu_id,
        lock,
        __builtin_return_address(0),
        __builtin_return_address(1)
      );
      debug_dump_log();
      return;
    }
  #endif

  /* assume we actually have it */
  if (lock->count == 1) {
    lock->cpu_id = -1;
    debug_logit( 'U', lock );
    lock->count  = 0;
    RTEMS_COMPILER_MEMORY_BARRIER();
    lock->lock = 0;
  } else {
    debug_logit( 'u', lock );
    lock->count--;
  }

  _ISR_Enable_on_this_core( level ); 
}

ISR_Level _SMP_lock_spinlock_nested_Obtain(
  SMP_lock_spinlock_nested_Control *lock
)
{
  ISR_Level  level = 0;
  uint32_t   value = 1;
  uint32_t   previous;
  int        cpu_id;

  /* Note: Disable provides an implicit memory barrier. */
  _ISR_Disable_on_this_core( level ); 

  cpu_id = bsp_smp_processor_id();

  /*
   *  Attempt to obtain the lock.  If we do not get it immediately, then
   *  do a single "monitor" iteration.  This should allow the loop to back
   *  off the bus a bit and allow the other core to finish sooner.
   */
  while (1) {
    RTEMS_COMPILER_MEMORY_BARRIER();
    SMP_CPU_SWAP( &lock->lock, value, previous );
    RTEMS_COMPILER_MEMORY_BARRIER();
    if ( previous == 0 ) {
      /* was not locked */
      break;
    }

    /* Deal with nested calls from one cpu */
    if (cpu_id == lock->cpu_id) {
      lock->count++;
      debug_logit( 'l', lock );
      return level;
    }
  }

  lock->cpu_id = cpu_id;
  lock->count = 1;
  debug_logit( 'L', lock );

  return level;
}

/*
 * Debug log for debugging nested lock/unlock problems.
 */
#if defined(ENABLE_DEBUG_LOGGING)
  static void debug_logit(
    char                             act,
    SMP_lock_spinlock_nested_Control *lock
  )
  { 
    debug_debug_spinlog_t *sp;
    if ( DEBUG_SPINLOG_INDEX == DEBUG_SPINLOG_MAX )
      #if defined (ENABLE_LOOPED_DEBUG_LOGGING)
        DEBUG_SPINLOG_INDEX = 0;
      #else
        return;
      #endif

    sp = &DEBUG_SPINLOG[ DEBUG_SPINLOG_INDEX++ ];
    sp->action = act;

    #if 0
      if ( lock == &_ISR_SMP_Lock )
        sp->lock = 'I';
      else 
    #endif
    if ( lock == &_Thread_Dispatch_disable_level_lock )
      sp->lock   = 'D';
    sp->cpu    = bsp_smp_processor_id() + '0';
    sp->count  = lock->count;
    #if 0
      if ( sp->lock == 'I' ) {
        if ( _Thread_Dispatch_smp_spin_lock.id == 0 )
          printk( "not nested %p from %p %p %p %p\n", sp,
          __builtin_return_address(0), __builtin_return_address(1),
          __builtin_return_address(2), __builtin_return_address(3) 
        );
      }
    #endif
    sp->nest_level =  _ISR_Nest_level; 
    sp->ret1 = 0;
    sp->ret2 = 0;
    sp->ret3 = 0;
    sp->ret4 = 0;
    sp->ret1 = __builtin_return_address(0);
    if ( sp->ret1 >= start && sp->ret1 <= _fini ) {
      sp->ret2   = __builtin_return_address(1);
      if ( sp->ret2 >= start && sp->ret2 <= _fini ) {
        sp->ret3   = __builtin_return_address(2);
        if ( sp->ret3 >= start && sp->ret3 <= _fini ) {
          sp->ret4   = __builtin_return_address(3);
        }
      }
    }
  }
  
  static void debug_dump_log(void)
  { 
    debug_debug_spinlog_t *sp;
    int       index;
    bool      done =false;
    
    #if defined (ENABLE_ONESHOT_DEBUG_LOGGING)
      index = 0;
    #else
      if (DEBUG_SPINLOG_INDEX >= DEBUG_SPINLOG_MAX)
        index = 0;
      else
        index = DEBUG_SPINLOG_INDEX;
    #endif


    do {
      sp = &DEBUG_SPINLOG[ index ];
      printk("%d) act %c lock %c cpu %c count=%d nest %d (%p, %p, %p, %p)\n",
        index, sp->action, sp->lock, sp->cpu, sp->count, sp->nest_level, 
        sp->ret1, sp->ret2, sp->ret3, sp->ret4
      );

      index++;
      if (index == DEBUG_SPINLOG_INDEX)
        break;
      if (index >= DEBUG_SPINLOG_MAX)
        index = 0;
    } while (1);
  }
#endif