source: rtems/c/src/lib/libbsp/unix/posix/startup/bspstart.c @ 421dfef6

4.104.114.84.95
Last change on this file since 421dfef6 was 421dfef6, checked in by Joel Sherrill <joel.sherrill@…>, on 01/30/98 at 20:59:22

Corrected Linux port for glibc2

  • Property mode set to 100644
File size: 7.9 KB
Line 
1/*  bsp_start()
2 *
3 *  This routine starts the application.  It includes application,
4 *  board, and monitor specific initialization and configuration.
5 *  The generic CPU dependent initialization has been performed
6 *  before this routine is invoked.
7 *
8 *  Called by RTEMS::RTEMS constructor in startup-ctor.cc
9 *
10 *  INPUT:  NONE
11 *
12 *  OUTPUT: NONE
13 *
14 *  COPYRIGHT (c) 1989-1997.
15 *  On-Line Applications Research Corporation (OAR).
16 *  Copyright assigned to U.S. Government, 1994.
17 *
18 *  The license and distribution terms for this file may be
19 *  found in the file LICENSE in this distribution or at
20 *  http://www.OARcorp.com/rtems/license.html.
21 *
22 *  $Id$
23 */
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <fcntl.h>
28
29/* for sbrk prototype in linux */
30#if defined(__linux__)
31#define __USE_MISC
32#endif
33#include <unistd.h>
34
35#include <bsp.h>
36#include <libcsupport.h>
37
38#include <rtems/libio.h>
39
40#ifdef STACK_CHECKER_ON
41#include <stackchk.h>
42#endif
43
44extern rtems_configuration_table  Configuration;
45
46/*
47 * A copy of the configuration table from the application
48 * with some changes applied to it.
49 */
50
51rtems_configuration_table    BSP_Configuration;
52rtems_multiprocessing_table  BSP_Multiprocessing;
53rtems_cpu_table              Cpu_table;
54rtems_unsigned32             bsp_isr_level;
55rtems_unsigned32             Heap_size;
56int                          rtems_argc;
57char                       **rtems_argv;
58
59/*
60 * May be overridden by RTEMS_WORKSPACE_SIZE and RTEMS_HEAPSPACE_SIZE
61 * environment variables; see below.
62 */
63
64#define DEFAULT_WORKSPACE_SIZE (WORKSPACE_MB * (1024 * 1024))
65#define DEFAULT_HEAPSPACE_SIZE (HEAPSPACE_MB * (1024 * 1024))
66
67/*
68 * Amount to increment itimer by each pass
69 * It is a variable instead of a #define to allow the 'looptest'
70 * script to bump it without recompiling rtems
71 */
72
73rtems_unsigned32 CPU_CLICKS_PER_TICK;
74
75/*
76 *  Function:   bsp_libc_init
77 *  Created:    94/12/6
78 *
79 *  Description:
80 *      Initialize whatever libc we are using
81 *      called from bsp_postdriver_hook
82 *
83 *
84 *  Parameters:
85 *      none
86 *
87 *  Returns:
88 *      none.
89 *
90 *  Side Effects:
91 *
92 *
93 *  Notes:
94 *
95 *  Deficiencies/ToDo:
96 *
97 *
98 */
99
100void
101bsp_libc_init(void)
102{
103    void *heap_start;
104
105    if (getenv("RTEMS_HEAPSPACE_SIZE"))
106        Heap_size = strtol(getenv("RTEMS_HEAPSPACE_SIZE"), 0, 0);
107    else
108        Heap_size = DEFAULT_HEAPSPACE_SIZE;
109 
110    heap_start = 0;
111
112    RTEMS_Malloc_Initialize((void *)heap_start, Heap_size, 1024 * 1024);
113
114    /*
115     *  Init the RTEMS libio facility to provide UNIX-like system
116     *  calls for use by newlib (ie: provide __rtems_open, __rtems_close, etc)
117     *  Uses malloc() to get area for the iops, so must be after malloc init
118     */
119
120    rtems_libio_init();
121
122    libc_init(1);
123}
124
125
126/*
127 *  Function:   bsp_pretasking_hook
128 *  Created:    95/03/10
129 *
130 *  Description:
131 *      BSP pretasking hook.  Called just before drivers are initialized.
132 *      Used to setup libc and install any BSP extensions.
133 *
134 *  Parameters:
135 *      none
136 *
137 *  Returns:
138 *      nada
139 *
140 *  Side Effects:
141 *      installs a few extensions
142 *
143 *  Notes:
144 *      Must not use libc (to do io) from here, since drivers are
145 *      not yet initialized.
146 *
147 *  Deficiencies/ToDo:
148 *
149 *
150 */
151
152void
153bsp_pretasking_hook(void)
154{
155    bsp_libc_init();
156
157#ifdef STACK_CHECKER_ON
158    /*
159     *  Initialize the stack bounds checker
160     *  We can either turn it on here or from the app.
161     */
162
163    Stack_check_Initialize();
164#endif
165
166#ifdef RTEMS_DEBUG
167    rtems_debug_enable( RTEMS_DEBUG_ALL_MASK );
168#endif
169
170    /*
171     * Dump malloc stats on exit...
172     */
173#if defined(RTEMS_DEBUG)
174  atexit(malloc_dump);
175#endif
176}
177
178/*
179 * After drivers are setup, register some "filenames"
180 * and open stdin, stdout, stderr files
181 *
182 * Newlib will automatically associate the files with these
183 * (it hardcodes the numbers)
184 */
185 
186void
187bsp_postdriver_hook(void)
188{
189#if 0
190  int stdin_fd, stdout_fd, stderr_fd;
191  int error_code;
192 
193  error_code = 'S' << 24 | 'T' << 16;
194 
195  if ((stdin_fd = __rtems_open("/dev/console", O_RDONLY, 0)) == -1)
196    rtems_fatal_error_occurred( error_code | 'D' << 8 | '0' );
197 
198  if ((stdout_fd = __rtems_open("/dev/console", O_WRONLY, 0)) == -1)
199    rtems_fatal_error_occurred( error_code | 'D' << 8 | '1' );
200 
201  if ((stderr_fd = __rtems_open("/dev/console", O_WRONLY, 0)) == -1)
202    rtems_fatal_error_occurred( error_code | 'D' << 8 | '2' );
203 
204  if ((stdin_fd != 0) || (stdout_fd != 1) || (stderr_fd != 2))
205    rtems_fatal_error_occurred( error_code | 'I' << 8 | 'O' );
206#endif
207}
208
209/*
210 *  Function:   bsp_start
211 *  Created:    94/12/6
212 *
213 *  Description:
214 *      called by crt0 as our "main" equivalent
215 *
216 *
217 *
218 *  Parameters:
219 *
220 *
221 *  Returns:
222 *
223 *
224 *  Side Effects:
225 *
226 *
227 *  Notes:
228 *
229 *
230 *  Deficiencies/ToDo:
231 *
232 *
233 */
234
235void
236bsp_start(void)
237{
238    unsigned32 workspace_ptr;
239
240    /*
241     *  Copy the table
242     */
243
244    BSP_Configuration = Configuration;
245 
246    /*
247     *  If the node number is -1 then the application better provide
248     *  it through environment variables RTEMS_NODE.
249     *  Ditto for RTEMS_MAXIMUM_NODES
250     */
251
252    if (BSP_Configuration.User_multiprocessing_table) {
253        char *p;
254 
255        /* make a copy for possible editing */
256        BSP_Multiprocessing = *BSP_Configuration.User_multiprocessing_table;
257        BSP_Configuration.User_multiprocessing_table = &BSP_Multiprocessing;
258 
259        if (BSP_Multiprocessing.node == -1)
260        {
261            p = getenv("RTEMS_NODE");
262            BSP_Multiprocessing.node = p ? atoi(p) : 1;
263        }
264 
265        /* If needed provide maximum_nodes also */
266        if (BSP_Multiprocessing.maximum_nodes == -1)
267        {
268            p = getenv("RTEMS_MAXIMUM_NODES");
269            BSP_Multiprocessing.maximum_nodes = p ? atoi(p) : 1;
270        }
271    }
272
273    /*
274     * Set cpu_number to accurately reflect our cpu number
275     */
276
277    if (BSP_Configuration.User_multiprocessing_table)
278        cpu_number = BSP_Configuration.User_multiprocessing_table->node - 1;
279    else
280        cpu_number = 0;
281
282    if (getenv("RTEMS_WORKSPACE_SIZE"))
283        BSP_Configuration.work_space_size =
284           strtol(getenv("RTEMS_WORKSPACE_SIZE"), 0, 0);
285    else
286        BSP_Configuration.work_space_size = DEFAULT_WORKSPACE_SIZE;
287 
288    /*
289     * Allocate workspace memory, ensuring it is properly aligned
290     */
291 
292    workspace_ptr =
293      (unsigned32) sbrk(BSP_Configuration.work_space_size + CPU_ALIGNMENT);
294    workspace_ptr += CPU_ALIGNMENT - 1;
295    workspace_ptr &= ~(CPU_ALIGNMENT - 1);
296
297    BSP_Configuration.work_space_start = (void *) workspace_ptr;
298 
299    /*
300     * Set up our hooks
301     * Make sure libc_init is done before drivers init'd so that
302     * they can use atexit()
303     */
304
305    Cpu_table.pretasking_hook = bsp_pretasking_hook;  /* init libc, etc. */
306
307    Cpu_table.predriver_hook = NULL;
308
309    Cpu_table.postdriver_hook = bsp_postdriver_hook;
310
311    Cpu_table.idle_task = NULL;  /* do not override system IDLE task */
312
313    /*
314     *  Don't zero out the workspace since it is in the BSS under UNIX.
315     */
316
317    Cpu_table.do_zero_of_workspace = FALSE;
318
319    /*
320     * XXX; interrupt stack not currently used, so this doesn't matter
321     */
322
323    Cpu_table.interrupt_stack_size = (12 * 1024);
324
325    Cpu_table.extra_mpci_receive_server_stack = 0;
326
327    /*
328     * Add 1 region for RTEMS Malloc
329     */
330
331    BSP_Configuration.RTEMS_api_configuration->maximum_regions++;
332
333#ifdef RTEMS_NEWLIB
334    /*
335     * Add 1 extension for newlib libc
336     */
337
338    BSP_Configuration.maximum_extensions++;
339#endif
340
341#ifdef STACK_CHECKER_ON
342  /*
343   * Add 1 extension for stack checker
344   */
345
346    BSP_Configuration.maximum_extensions++;
347#endif
348
349  /*
350   * Tell libio how many fd's we want and allow it to tweak config
351   */
352
353  rtems_libio_config(&BSP_Configuration, BSP_LIBIO_MAX_FDS);
354
355  /*
356   * Add 1 extension for MPCI_fatal
357   */
358
359  if (BSP_Configuration.User_multiprocessing_table)
360      BSP_Configuration.maximum_extensions++;
361
362  CPU_CLICKS_PER_TICK = 1;
363
364  /*
365   *  Start most of RTEMS
366   *  main() will start the rest
367   */
368
369  bsp_isr_level = rtems_initialize_executive_early(
370    &BSP_Configuration,
371    &Cpu_table
372  );
373}
Note: See TracBrowser for help on using the repository browser.