source: rtems/c/src/lib/libbsp/shared/bootcard.c @ dce79aee

4.104.115
Last change on this file since dce79aee was dce79aee, checked in by Joel Sherrill <joel.sherrill@…>, on 05/05/09 at 21:17:47

2009-05-05 Joel Sherrill <joel.sherrill@…>

  • bootcard.c, gdbstub/rtems-stub-glue.c: Add info to not enough memory message.
  • Property mode set to 100644
File size: 7.7 KB
Line 
1/*
2 *  This is the C entry point for ALL RTEMS BSPs.  It is invoked
3 *  from the assembly language initialization file usually called
4 *  start.S.  It provides the framework for the BSP initialization
5 *  sequence.  The basic flow of initialization is:
6 *
7 *  + start.S: basic CPU setup (stack, zero BSS)
8 *    + boot_card
9 *      + if defined(BSP_BOOTCARD_HANDLES_RAM_ALLOCATION)
10 *        - obtain information on BSP memory and allocate RTEMS Workspace
11 *      + bspstart.c: bsp_start - more advanced initialization
12 *      + rtems_initialize_data_structures
13 *      + if defined(BSP_BOOTCARD_HANDLES_RAM_ALLOCATION)
14 *        - Allocate memory to C Program Heap
15 *        - initialize C Library and C Program Heap
16 *      + bsp_pretasking_hook
17 *      + if defined(RTEMS_DEBUG)
18 *        - rtems_debug_enable( RTEMS_DEBUG_ALL_MASK );
19 *      + rtems_initialize_before_drivers
20 *      + bsp_predriver_hook
21 *      + rtems_initialize_device_drivers
22 *        - all device drivers
23 *      + bsp_postdriver_hook
24 *      + rtems_initialize_start_multitasking
25 *        - 1st task executes C++ global constructors
26 *          .... appplication runs ...
27 *          - exit
28 *     + back to here eventually
29 *     + bspclean.c: bsp_cleanup
30 *
31 *  This style of initialization ensures that the C++ global
32 *  constructors are executed after RTEMS is initialized.
33 *  Thanks to Chris Johns <cjohns@plessey.com.au> for the idea
34 *  to move C++ global constructors into the first task.
35 *
36 *  COPYRIGHT (c) 1989-2008.
37 *  On-Line Applications Research Corporation (OAR).
38 *
39 *  The license and distribution terms for this file may be
40 *  found in the file LICENSE in this distribution or at
41 *  http://www.rtems.com/license/LICENSE.
42 *
43 *  $Id$
44 */
45
46#include <rtems.h>
47
48#include <bsp/bootcard.h>
49
50/*
51 *  At most a single pointer to the cmdline for those target
52 *  short on memory and not supporting a command line.
53 */
54const char *bsp_boot_cmdline;
55
56/*
57 * Are we using a single heap for the RTEMS Workspace and C Program Heap?
58 */
59extern bool rtems_unified_work_area;
60
61/*
62 *  These are the prototypes and helper routines which are used
63 *  when the BSP lets the framework handle RAM allocation between
64 *  the RTEMS Workspace and C Program Heap.
65 */
66static rtems_status_code bootcard_bsp_libc_helper(
67  void    *work_area_start,
68  intptr_t work_area_size,
69  void    *heap_start,
70  intptr_t heap_size
71)
72{
73  intptr_t heap_size_default = 0;
74
75  if ( !rtems_unified_work_area &&
76       heap_start == BSP_BOOTCARD_HEAP_USES_WORK_AREA) {
77    /* Place the heap immediately following the work area */
78    heap_start = work_area_start + rtems_configuration_get_work_space_size();
79
80    /* Ensure proper alignement */
81    if ((uintptr_t) heap_start & (CPU_ALIGNMENT - 1)) {
82      heap_start = (void *) (((uintptr_t) heap_start + CPU_ALIGNMENT)
83        & ~(CPU_ALIGNMENT - 1));
84    }
85
86    /*
87     * For the default heap size use the free space from the end of the
88     * work space up to the end of the work area as heap.
89     */
90    heap_size_default = work_area_size - rtems_configuration_get_work_space_size();
91
92    /* Keep it as a multiple of 16 bytes */
93    heap_size_default &= ~((intptr_t) 0xf);
94
95    /* Use default heap size if requested */
96    if (heap_size == BSP_BOOTCARD_HEAP_SIZE_DEFAULT) {
97      heap_size = heap_size_default;
98    }
99             
100    /* Check heap size */
101    if (heap_size > heap_size_default) {
102      return RTEMS_INVALID_SIZE;
103    }
104  }
105
106  bsp_libc_init(heap_start, heap_size, 0);
107
108  return RTEMS_SUCCESSFUL;
109}
110
111/*
112 *  This is the initialization framework routine that weaves together
113 *  calls to RTEMS and the BSP in the proper sequence to initialize
114 *  the system while maximizing shared code and keeping BSP code in C
115 *  as much as possible.
116 */
117int boot_card(
118  const char *cmdline
119)
120{
121  rtems_interrupt_level  bsp_isr_level;
122  rtems_status_code      sc = RTEMS_SUCCESSFUL;
123  void                  *work_area_start = NULL;
124  intptr_t               work_area_size = 0;
125  void                  *heap_start = NULL;
126  intptr_t               heap_size = 0;
127
128  /*
129   * Special case for PowerPC: The interrupt disable mask is stored in SPRG0.
130   * It must be valid before we can use rtems_interrupt_disable().
131   */
132  #ifdef PPC_INTERRUPT_DISABLE_MASK_DEFAULT
133    ppc_interrupt_set_disable_mask( PPC_INTERRUPT_DISABLE_MASK_DEFAULT );
134  #endif /* PPC_INTERRUPT_DISABLE_MASK_DEFAULT */
135
136  /*
137   *  Make sure interrupts are disabled.
138   */
139  rtems_interrupt_disable( bsp_isr_level );
140
141  bsp_boot_cmdline = cmdline;
142
143  /*
144   * Invoke Board Support Package initialization routine written in C.
145   */
146  bsp_start();
147
148  /*
149   *  Find out where the block of memory the BSP will use for
150   *  the RTEMS Workspace and the C Program Heap is.
151   */
152  bsp_get_work_area(&work_area_start, (ssize_t*) &work_area_size,
153                    &heap_start, (ssize_t*) &heap_size);
154
155  if ( work_area_size <= Configuration.work_space_size ) {
156    printk(
157      "bootcard: Work space too big for work area! (%d > %d)\n",
158      Configuration.work_space_size,
159      work_area_size
160    );
161    bsp_cleanup();
162    return -1;
163  }
164
165  if ( rtems_unified_work_area ) {
166    Configuration.work_space_start = work_area_start;
167    Configuration.work_space_size  = work_area_size;
168  } else {
169    Configuration.work_space_start = (char *) work_area_start;
170  }
171
172  #if (BSP_DIRTY_MEMORY == 1)
173    memset( work_area_start, 0xCF,  work_area_size );
174  #endif
175
176  /*
177   *  Initialize RTEMS data structures
178   */
179  rtems_initialize_data_structures();
180
181  /*
182   *  Initialize the C library for those BSPs using the shared
183   *  framework.
184   */
185  sc = bootcard_bsp_libc_helper(
186    work_area_start,
187    work_area_size,
188    heap_start,
189    heap_size
190  );
191  if ( sc != RTEMS_SUCCESSFUL ) {
192    printk( "bootcard: Cannot initialize C library!\n");
193    bsp_cleanup();
194    return -1;
195  }
196
197  /*
198   *  All BSP to do any required initialization now that RTEMS
199   *  data structures are initialized.  In older BSPs or those
200   *  which do not use the shared framework, this is the typical
201   *  time when the C Library is initialized so malloc()
202   *  can be called by device drivers.  For BSPs using the shared
203   *  framework, this routine can be empty.
204   */
205  bsp_pretasking_hook();
206
207  /*
208   *  If debug is enabled, then enable all dynamic RTEMS debug
209   *  capabilities.
210   *
211   *  NOTE: Most debug features are conditionally compiled in
212   *        or enabled via configure time plugins.
213   */
214  #ifdef RTEMS_DEBUG
215    rtems_debug_enable( RTEMS_DEBUG_ALL_MASK );
216  #endif
217
218  /*
219   *  Let RTEMS perform initialization it requires before drivers
220   *  are allowed to be initialized.
221   */
222  rtems_initialize_before_drivers();
223
224  /*
225   *  Execute BSP specific pre-driver hook. Drivers haven't gotten
226   *  to initialize yet so this is a good chance to initialize
227   *  buses, spurious interrupt handlers, etc..
228   *
229   *  NOTE: Many BSPs do not require this handler and use the
230   *        shared stub.
231   */
232  bsp_predriver_hook();
233
234  /*
235   *  Initialize all device drivers.
236   */
237  rtems_initialize_device_drivers();
238
239  /*
240   *  Invoke the postdriver hook.  This normally opens /dev/console
241   *  for use as stdin, stdout, and stderr.
242   */
243  bsp_postdriver_hook();
244
245  /*
246   *  Complete initialization of RTEMS and switch to the first task.
247   *  Global C++ constructors will be executed in the context of that task.
248   */
249  rtems_initialize_start_multitasking();
250
251  /***************************************************************
252   ***************************************************************
253   *  APPLICATION RUNS HERE!!!  When it shuts down, we return!!! *
254   ***************************************************************
255   ***************************************************************
256   */
257
258  /*
259   *  Perform any BSP specific shutdown actions which are written in C.
260   */
261  bsp_cleanup();
262
263  /*
264   *  Now return to the start code.
265   */
266  return 0;
267}
Note: See TracBrowser for help on using the repository browser.