summaryrefslogtreecommitdiffstats
path: root/testsuites/smptests/smpfatal03/init.c
blob: 13a9db511a32d8a52006baba8943bb8568058ba0 (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
/*
 * Copyright (c) 2014 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

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

#include <rtems.h>
#include <rtems/counter.h>
#include <rtems/test.h>
#include <rtems/score/smpbarrier.h>
#include <rtems/score/smpimpl.h>
#include <rtems/score/threaddispatch.h>

#include <assert.h>
#include <stdlib.h>

const char rtems_test_name[] = "SMPFATAL 3";

#define CPU_COUNT 2

static uint32_t main_cpu;

static SMP_barrier_Control giant_barrier = SMP_BARRIER_CONTROL_INITIALIZER;

static SMP_barrier_Control fatal_barrier = SMP_BARRIER_CONTROL_INITIALIZER;

static void acquire_giant_and_fatal_task(rtems_task_argument arg)
{
  SMP_barrier_State state = SMP_BARRIER_STATE_INITIALIZER;
  int i;

  for (i = 0; i < 13; ++i) {
    _Giant_Acquire();
  }

  _SMP_barrier_Wait(&giant_barrier, &state, CPU_COUNT);

  /*
   * Now we have to wait some time so that the other thread can actually start
   * with the _Giant_Acquire() procedure.
   */
  rtems_counter_delay_nanoseconds(1000000);

  rtems_fatal(RTEMS_FATAL_SOURCE_APPLICATION, 0xdeadbeef);
}

static void wait_for_giant(void)
{
  SMP_barrier_State state = SMP_BARRIER_STATE_INITIALIZER;

  _SMP_barrier_Wait(&giant_barrier, &state, CPU_COUNT);

  _Giant_Acquire();
}

static void Init(rtems_task_argument arg)
{
  uint32_t self = rtems_smp_get_current_processor();
  uint32_t cpu_count = rtems_get_processor_count();

  rtems_test_begink();

  main_cpu = self;

  if (cpu_count >= CPU_COUNT) {
    rtems_status_code sc;
    rtems_id id;

    sc = rtems_task_create(
      rtems_build_name( 'W', 'A', 'I', 'T' ),
      1,
      RTEMS_MINIMUM_STACK_SIZE,
      RTEMS_DEFAULT_MODES,
      RTEMS_DEFAULT_ATTRIBUTES,
      &id
    );
    assert(sc == RTEMS_SUCCESSFUL);

    sc = rtems_task_start(id, acquire_giant_and_fatal_task, 0);
    assert(sc == RTEMS_SUCCESSFUL);

    wait_for_giant();
  } else {
    rtems_test_endk();
    exit(0);
  }
}

static void fatal_extension(
  rtems_fatal_source source,
  bool is_internal,
  rtems_fatal_code code
)
{
  if (
    source == RTEMS_FATAL_SOURCE_APPLICATION
      || source == RTEMS_FATAL_SOURCE_SMP
  ) {
    uint32_t self = rtems_smp_get_current_processor();
    SMP_barrier_State state = SMP_BARRIER_STATE_INITIALIZER;

    assert(!is_internal);

    if (self == main_cpu) {
      assert(source == RTEMS_FATAL_SOURCE_SMP);
      assert(code == SMP_FATAL_SHUTDOWN);
    } else {
      assert(source == RTEMS_FATAL_SOURCE_APPLICATION);
      assert(code == 0xdeadbeef);
    }

    _SMP_barrier_Wait(&fatal_barrier, &state, CPU_COUNT);

    if (self == 0) {
      rtems_test_endk();
    }
  }
}

#define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER

#define CONFIGURE_INITIAL_EXTENSIONS \
  { .fatal = fatal_extension }, \
  RTEMS_TEST_INITIAL_EXTENSION

#define CONFIGURE_SMP_APPLICATION

#define CONFIGURE_SMP_MAXIMUM_PROCESSORS CPU_COUNT

#define CONFIGURE_MAXIMUM_TASKS 2

#define CONFIGURE_RTEMS_INIT_TASKS_TABLE

#define CONFIGURE_INIT

#include <rtems/confdefs.h>