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

4.104.114.95
Last change on this file since cc54cc9 was cc54cc9, checked in by Thomas Doerfler <Thomas.Doerfler@…>, on 07/29/08 at 07:11:21

src/irq-legacy.c: Free allocated memory in hander remove
bootcard.c: Check if the heap fits into the work area

  • Property mode set to 100644
File size: 8.3 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 *  Since there is a forward reference
52 */
53char *rtems_progname;
54
55/*
56 *  These are the prototypes and helper routines which are used
57 *  when the BSP lets the framework handle RAM allocation between
58 *  the RTEMS Workspace and C Program Heap.
59 */
60#if defined(BSP_BOOTCARD_HANDLES_RAM_ALLOCATION)
61  static rtems_status_code bootcard_bsp_libc_helper(
62    void   *work_area_start,
63    size_t  work_area_size,
64    void   *heap_start,
65    size_t  heap_size
66  )
67  {
68    size_t heap_size_default = 0;
69
70    if (heap_start == BSP_BOOTCARD_HEAP_USES_WORK_AREA) {
71      /* Use the work area start as heap start */
72      heap_start = work_area_start;
73
74      /* Ensure proper alignement */
75      if ((uintptr_t) heap_start & (CPU_ALIGNMENT - 1)) {
76        heap_start = (void *) (((uintptr_t) heap_start + CPU_ALIGNMENT)
77          & ~(CPU_ALIGNMENT - 1));
78      }
79
80      /*
81       * For the default heap size use the free space from the start of the
82       * work area up to the work space start as heap area.
83       */
84      heap_size_default = (char *) Configuration.work_space_start
85        - (char *) work_area_start;
86
87      /* Keep it as a multiple of 16 bytes */
88      heap_size_default &= ~((size_t) 0xf);
89
90      /* Use default heap size if requested */
91      if (heap_size == BSP_BOOTCARD_HEAP_SIZE_DEFAULT) {
92        heap_size = heap_size_default;
93      }
94               
95      /* Check heap size */
96      if (heap_size > heap_size_default) {
97        return RTEMS_INVALID_SIZE;
98      }
99    }
100
101    bsp_libc_init( heap_start, (uint32_t) heap_size, 0);
102
103    return RTEMS_SUCCESSFUL;
104  }
105#endif
106
107/*
108 *  This is the initialization framework routine that weaves together
109 *  calls to RTEMS and the BSP in the proper sequence to initialize
110 *  the system while maximizing shared code and keeping BSP code in C
111 *  as much as possible.
112 */
113int boot_card(
114  int    argc,
115  char **argv,
116  char **envp
117)
118{
119  static char  *argv_pointer = NULL;
120  static char  *envp_pointer = NULL;
121  char **argv_p = &argv_pointer;
122  char **envp_p = &envp_pointer;
123  rtems_interrupt_level bsp_isr_level;
124  #if defined(BSP_BOOTCARD_HANDLES_RAM_ALLOCATION)
125    rtems_status_code sc = RTEMS_SUCCESSFUL;
126    void   *work_area_start = NULL;
127    size_t  work_area_size = 0;
128    void   *heap_start = NULL;
129    size_t  heap_size = 0;
130  #endif
131
132  /*
133   * Special case for PowerPC: The interrupt disable mask is stored in SPRG0.
134   * It must be valid before we can use rtems_interrupt_disable().
135   */
136  #ifdef PPC_INTERRUPT_DISABLE_MASK_DEFAULT
137    ppc_interrupt_set_disable_mask( PPC_INTERRUPT_DISABLE_MASK_DEFAULT );
138  #endif /* PPC_INTERRUPT_DISABLE_MASK_DEFAULT */
139
140  /*
141   *  Make sure interrupts are disabled.
142   */
143  rtems_interrupt_disable( bsp_isr_level );
144
145  /*
146   *  Set things up so we have real pointers for argv and envp.
147   *  If the BSP has passed us something useful, then pass it on.
148   *  Somehow we need to eventually make this available to
149   *  a real main() in user land. :)
150   */
151  if ( argv ) argv_p = argv;
152  if ( envp ) envp_p = envp;
153
154  /*
155   *  Set the program name in case some application cares.
156   */
157  if ((argc > 0) && argv && argv[0])
158    rtems_progname = argv[0];
159  else
160    rtems_progname = "RTEMS";
161
162  /*
163   *  Find out where the block of memory the BSP will use for
164   *  the RTEMS Workspace and the C Program Heap is.
165   */
166  #if defined(BSP_BOOTCARD_HANDLES_RAM_ALLOCATION)
167    {
168      void *work_space_start = NULL;
169   
170      bsp_get_work_area(
171        &work_area_start,
172        &work_area_size,
173        &heap_start,
174        &heap_size
175      );
176   
177      work_space_start = (char *) work_area_start + work_area_size
178        - rtems_configuration_get_work_space_size();
179   
180      if ((uintptr_t) work_space_start <= (uintptr_t) work_area_start) {
181        printk( "bootcard: Work space to big for work area!\n");
182        bsp_cleanup();
183        return -1;
184      }
185   
186      Configuration.work_space_start = work_space_start;
187   
188      #if (BSP_DIRTY_MEMORY == 1)
189        memset( work_area_start, 0xCF,  work_area_size);
190      #endif
191    }
192  #endif
193
194  /*
195   * Invoke Board Support Package initialization routine written in C.
196   */
197  bsp_start();
198
199  /*
200   *  Initialize RTEMS data structures
201   */
202  rtems_initialize_data_structures( &Configuration );
203
204  /*
205   *  Initialize the C library for those BSPs using the shared
206   *  framework.
207   */
208  #if defined(BSP_BOOTCARD_HANDLES_RAM_ALLOCATION)
209    sc = bootcard_bsp_libc_helper(
210      work_area_start,
211      work_area_size,
212      heap_start,
213      heap_size
214    );
215    if (sc != RTEMS_SUCCESSFUL) {
216      printk( "bootcard: Cannot initialize C library!\n");
217      bsp_cleanup();
218      return -1;
219    }
220  #endif
221
222  /*
223   *  All BSP to do any required initialization now that RTEMS
224   *  data structures are initialized.  In older BSPs or those
225   *  which do not use the shared framework, this is the typical
226   *  time when the C Library is initialized so malloc()
227   *  can be called by device drivers.  For BSPs using the shared
228   *  framework, this routine can be empty.
229   */
230  bsp_pretasking_hook();
231
232  /*
233   *  If debug is enabled, then enable all dynamic RTEMS debug
234   *  capabilities.
235   *
236   *  NOTE: Most debug features are conditionally compiled in
237   *        or enabled via configure time plugins.
238   */
239  #ifdef RTEMS_DEBUG
240    rtems_debug_enable( RTEMS_DEBUG_ALL_MASK );
241  #endif
242
243  /*
244   *  Let RTEMS perform initialization it requires before drivers
245   *  are allowed to be initialized.
246   */
247  rtems_initialize_before_drivers();
248
249  /*
250   *  Execute BSP specific pre-driver hook. Drivers haven't gotten
251   *  to initialize yet so this is a good chance to initialize
252   *  buses, spurious interrupt handlers, etc..
253   *
254   *  NOTE: Many BSPs do not require this handler and use the
255   *        shared stub.
256   */
257  bsp_predriver_hook();
258
259  /*
260   *  Initialize all device drivers.
261   */
262  rtems_initialize_device_drivers();
263
264  /*
265   *  Invoke the postdriver hook.  This normally opens /dev/console
266   *  for use as stdin, stdout, and stderr.
267   */
268  bsp_postdriver_hook();
269
270  /*
271   *  Complete initialization of RTEMS and switch to the first task.
272   *  Global C++ constructors will be executed in the context of that task.
273   */
274  rtems_initialize_start_multitasking();
275
276  /***************************************************************
277   ***************************************************************
278   *  APPLICATION RUNS HERE!!!  When it shuts down, we return!!! *
279   ***************************************************************
280   ***************************************************************
281   */
282
283  /*
284   *  Perform any BSP specific shutdown actions which are written in C.
285   */
286  bsp_cleanup();
287
288  /*
289   *  Now return to the start code.
290   */
291
292  return 0;
293}
Note: See TracBrowser for help on using the repository browser.