source: rtems/c/src/lib/libbsp/i386/pc386/startup/bspstart.c @ fb61b0fe

4.104.114.84.95
Last change on this file since fb61b0fe was fb61b0fe, checked in by Joel Sherrill <joel.sherrill@…>, on 09/07/07 at 23:32:58

2007-09-07 Joel Sherrill <joel.sherrill@…>

  • startup/bspstart.c: If we cannot find enough memory to run the program, generate a fatal error and print a message.
  • Property mode set to 100644
File size: 8.8 KB
Line 
1/*-------------------------------------------------------------------------+
2| This file contains the PC386 BSP startup package. It includes application,
3| board, and monitor specific initialization and configuration. The generic CPU
4| dependent initialization has been performed before this routine is invoked.
5+--------------------------------------------------------------------------+
6| (C) Copyright 1997 -
7| - NavIST Group - Real-Time Distributed Systems and Industrial Automation
8|
9| http://pandora.ist.utl.pt
10|
11| Instituto Superior Tecnico * Lisboa * PORTUGAL
12+--------------------------------------------------------------------------+
13| Disclaimer:
14|
15| This file is provided "AS IS" without warranty of any kind, either
16| expressed or implied.
17+--------------------------------------------------------------------------+
18| This code is based on:
19|   bspstart.c,v 1.8 1996/05/28 13:12:40 joel Exp - go32 BSP
20| With the following copyright notice:
21| **************************************************************************
22| *  COPYRIGHT (c) 1989-1999.
23| *  On-Line Applications Research Corporation (OAR).
24| *
25| *  The license and distribution terms for this file may be
26| *  found in found in the file LICENSE in this distribution or at
27| *  http://www.rtems.com/license/LICENSE.
28| **************************************************************************
29|
30|  $Id$
31+--------------------------------------------------------------------------*/
32
33#include <bsp.h>
34#include <rtems/libio.h>
35#include <rtems/libcsupport.h>
36#include <rtems/pci.h>
37#include <libcpu/cpuModel.h>
38
39/*-------------------------------------------------------------------------+
40| Global Variables
41+--------------------------------------------------------------------------*/
42extern uint32_t         _end;         /* End of BSS. Defined in 'linkcmds'. */
43
44/* rudimentary multiboot info */
45struct multiboot_info {
46        uint32_t        flags;          /* start.S only raises flags for items actually saved; this allows us to check for the size of the data structure */
47        uint32_t        mem_lower;      /* avail kB in lower memory */
48        uint32_t        mem_upper;      /* avail kB in lower memory */
49        /* ... (unimplemented) */
50};
51
52extern struct multiboot_info _boot_multiboot_info;
53/*
54 * Size of heap if it is 0 it will be dynamically defined by memory size,
55 * otherwise the value should be changed by binary patch
56 */
57uint32_t         _heap_size = 0;
58
59/* Alternative way to hardcode the board's memory size [rather than heap size].
60 * Can easily be overridden by application.
61 */
62extern uint32_t bsp_mem_size
63  __attribute__ ((weak, alias("bsp_mem_size_default")));
64uint32_t bsp_mem_size_default = 0;
65
66/* Size of stack used during initialization. Defined in 'start.s'.  */
67extern uint32_t         _stack_size;
68
69uint32_t         rtemsFreeMemStart;
70                         /* Address of start of free memory - should be updated
71                            after creating new partitions or regions.         */
72
73/* The original BSP configuration table from the application and our copy of it
74   with some changes. */
75
76extern rtems_configuration_table  Configuration;
77       rtems_configuration_table  BSP_Configuration;
78
79rtems_cpu_table Cpu_table;                     /* CPU configuration table.    */
80char            *rtems_progname;               /* Program name - from main(). */
81
82/*-------------------------------------------------------------------------+
83| External Prototypes
84+--------------------------------------------------------------------------*/
85extern void rtems_irq_mngt_init(void);
86void bsp_libc_init( void *, uint32_t, int );
87void bsp_postdriver_hook(void);
88
89/*-------------------------------------------------------------------------+
90|         Function: bsp_pretasking_hook
91|      Description: BSP pretasking hook.  Called just before drivers are
92|                   initialized. Used to setup libc and install any BSP
93|                   extensions. NOTE: Must not use libc (to do io) from here,
94|                   since drivers are not yet initialized.
95| Global Variables: None.
96|        Arguments: None.
97|          Returns: Nothing.
98+--------------------------------------------------------------------------*/
99void bsp_pretasking_hook(void)
100{
101  uint32_t topAddr, val;
102  int      i, lowest;
103
104  if (rtemsFreeMemStart & (CPU_ALIGNMENT - 1))  /* not aligned => align it */
105    rtemsFreeMemStart = (rtemsFreeMemStart+CPU_ALIGNMENT) & ~(CPU_ALIGNMENT-1);
106
107  /* find the lowest 1M boundary to probe */
108  lowest = ((rtemsFreeMemStart + (1<<20)) >> 20) + 1;
109  if ( lowest  < 2 )
110      lowest = 2;
111
112  /* The memory detection algorithm is very crude; try
113   * to use multiboot info, if possible (set from start.S)
114   */
115  if ( bsp_mem_size == 0 &&
116       (_boot_multiboot_info.flags & 1) &&
117       _boot_multiboot_info.mem_upper ) {
118    bsp_mem_size = _boot_multiboot_info.mem_upper * 1024;
119  }
120
121  if ( _heap_size == 0 ) {
122    if ( bsp_mem_size == 0 ) {
123        /*
124         * We have to dynamically size memory. Memory size can be anything
125         * between no less than 2M and 2048M.
126         * let us first write
127         */
128        for (i=2048; i>=lowest; i--) {
129          topAddr = i*1024*1024 - 4;
130          *(volatile uint32_t*)topAddr = topAddr;
131        }
132
133        for(i=lowest; i<=2048; i++) {
134          topAddr = i*1024*1024 - 4;
135          val =  *(uint32_t*)topAddr;
136          if (val != topAddr) {
137            break;
138          }
139        }
140     
141        topAddr = (i-1)*1024*1024 - 4;
142      } else {
143        topAddr = bsp_mem_size;
144      }
145
146    if ( rtemsFreeMemStart > topAddr ) {
147      printk( "Out of memory -- unable to initialize BSP\n" );
148      rtems_fatal_error_occurred( 0x85858585 );
149    }
150 
151    _heap_size = topAddr - rtemsFreeMemStart;
152  }
153
154  bsp_libc_init((void *)rtemsFreeMemStart, _heap_size, 0);
155  rtemsFreeMemStart += _heap_size;           /* HEAP_SIZE  in KBytes */
156
157#ifdef RTEMS_DEBUG
158  rtems_debug_enable(RTEMS_DEBUG_ALL_MASK);
159#endif /* RTEMS_DEBUG */
160
161} /* bsp_pretasking_hook */
162
163/*-------------------------------------------------------------------------+
164|         Function: bsp_start
165|      Description: Called before main is invoked.
166| Global Variables: None.
167|        Arguments: None.
168|          Returns: Nothing.
169+--------------------------------------------------------------------------*/
170void bsp_start_default( void )
171{
172  int pci_init_retval;
173  void Calibrate_loop_1ms(void);
174
175  /*
176   * Calibrate variable for 1ms-loop (see timer.c)
177   */
178  Calibrate_loop_1ms();
179
180  /* set the value of start of free memory. */
181  rtemsFreeMemStart = (uint32_t)&_end + _stack_size;
182
183  /* If we don't have command line arguments set default program name. */
184
185  Cpu_table.pretasking_hook         = bsp_pretasking_hook; /* init libc, etc. */
186  Cpu_table.predriver_hook          = NULL;                /* use system's    */
187  Cpu_table.postdriver_hook         = bsp_postdriver_hook;
188  Cpu_table.idle_task               = NULL;
189                                          /* do not override system IDLE task */
190  Cpu_table.interrupt_table_segment = get_ds();
191  Cpu_table.interrupt_table_offset  = (void *)Interrupt_descriptor_table;
192  Cpu_table.interrupt_stack_size    = CONFIGURE_INTERRUPT_STACK_MEMORY;
193  Cpu_table.extra_mpci_receive_server_stack = 0;
194
195  /* Place RTEMS workspace at beginning of free memory. */
196
197  if (rtemsFreeMemStart & (CPU_ALIGNMENT - 1))  /* not aligned => align it */
198    rtemsFreeMemStart = (rtemsFreeMemStart+CPU_ALIGNMENT) & ~(CPU_ALIGNMENT-1);
199
200  BSP_Configuration.work_space_start = (void *)rtemsFreeMemStart;
201  rtemsFreeMemStart += BSP_Configuration.work_space_size;
202
203  /*
204   * Init rtems interrupt management
205   */
206  rtems_irq_mngt_init();
207  /*
208   * Init rtems exceptions management
209   */
210  rtems_exception_init_mngt();
211
212  /*
213   * init PCI Bios interface...
214   */
215  pci_init_retval = pci_initialize();
216  if (pci_init_retval != PCIB_ERR_SUCCESS) {
217      printk("PCI bus: could not initialize PCI BIOS interface\n");
218  }
219
220  /*
221   *  The following information is very useful when debugging.
222   */
223
224#if 0
225  printk( "work_space_size = 0x%x\n", BSP_Configuration.work_space_size );
226  printk( "maximum_extensions = 0x%x\n", BSP_Configuration.maximum_extensions );
227  printk( "microseconds_per_tick = 0x%x\n",
228     BSP_Configuration.microseconds_per_tick );
229  printk( "ticks_per_timeslice = 0x%x\n",
230     BSP_Configuration.ticks_per_timeslice );
231  printk( "number_of_device_drivers = 0x%x\n",
232     BSP_Configuration.number_of_device_drivers );
233  printk( "Device_driver_table = 0x%x\n",
234     BSP_Configuration.Device_driver_table );
235
236  printk( "_heap_size = 0x%x\n", _heap_size );
237  printk( "_stack_size = 0x%x\n", _stack_size );
238  printk( "rtemsFreeMemStart = 0x%x\n", rtemsFreeMemStart );
239  printk( "work_space_start = 0x%x\n", BSP_Configuration.work_space_start );
240  printk( "work_space_size = 0x%x\n", BSP_Configuration.work_space_size );
241#endif
242} /* bsp_start */
243
244/*
245 *  By making this a weak alias for bsp_start_default, a brave soul
246 *  can override the actual bsp_start routine used.
247 */
248
249void bsp_start (void) __attribute__ ((weak, alias("bsp_start_default")));
Note: See TracBrowser for help on using the repository browser.