[637df35] | 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 | * |
---|
[03f2154e] | 14 | * COPYRIGHT (c) 1989-1997. |
---|
[637df35] | 15 | * On-Line Applications Research Corporation (OAR). |
---|
[03f2154e] | 16 | * Copyright assigned to U.S. Government, 1994. |
---|
[637df35] | 17 | * |
---|
[98e4ebf5] | 18 | * The license and distribution terms for this file may be |
---|
| 19 | * found in the file LICENSE in this distribution or at |
---|
[03f2154e] | 20 | * http://www.OARcorp.com/rtems/license.html. |
---|
[637df35] | 21 | * |
---|
| 22 | * $Id$ |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | #include <stdio.h> |
---|
| 26 | #include <stdlib.h> |
---|
| 27 | #include <fcntl.h> |
---|
[ce41c5b] | 28 | |
---|
| 29 | /* for sbrk prototype in linux */ |
---|
| 30 | #if defined(linux) |
---|
| 31 | #define __USE_MISC |
---|
| 32 | #endif |
---|
[637df35] | 33 | #include <unistd.h> |
---|
| 34 | |
---|
| 35 | #include <bsp.h> |
---|
| 36 | #include <libcsupport.h> |
---|
| 37 | |
---|
[3a4ae6c] | 38 | #include <rtems/libio.h> |
---|
| 39 | |
---|
[637df35] | 40 | #ifdef STACK_CHECKER_ON |
---|
| 41 | #include <stackchk.h> |
---|
| 42 | #endif |
---|
| 43 | |
---|
| 44 | extern 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 | |
---|
| 51 | rtems_configuration_table BSP_Configuration; |
---|
| 52 | rtems_multiprocessing_table BSP_Multiprocessing; |
---|
| 53 | rtems_cpu_table Cpu_table; |
---|
| 54 | rtems_unsigned32 bsp_isr_level; |
---|
| 55 | rtems_unsigned32 Heap_size; |
---|
| 56 | int rtems_argc; |
---|
| 57 | char **rtems_argv; |
---|
| 58 | |
---|
[c1403ef1] | 59 | /* |
---|
| 60 | * May be overridden by RTEMS_WORKSPACE_SIZE and RTEMS_HEAPSPACE_SIZE |
---|
| 61 | * environment variables; see below. |
---|
| 62 | */ |
---|
[637df35] | 63 | |
---|
[c1403ef1] | 64 | #define DEFAULT_WORKSPACE_SIZE (WORKSPACE_MB * (1024 * 1024)) |
---|
| 65 | #define DEFAULT_HEAPSPACE_SIZE (HEAPSPACE_MB * (1024 * 1024)) |
---|
[637df35] | 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 | |
---|
| 73 | rtems_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 | |
---|
| 100 | void |
---|
| 101 | bsp_libc_init(void) |
---|
| 102 | { |
---|
| 103 | void *heap_start; |
---|
| 104 | |
---|
[c1403ef1] | 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 | |
---|
[637df35] | 110 | heap_start = 0; |
---|
| 111 | |
---|
| 112 | RTEMS_Malloc_Initialize((void *)heap_start, Heap_size, 1024 * 1024); |
---|
| 113 | |
---|
[3a4ae6c] | 114 | /* |
---|
| 115 | * Init the RTEMS libio facility to provide UNIX-like system |
---|
[634e746] | 116 | * calls for use by newlib (ie: provide __rtems_open, __rtems_close, etc) |
---|
[3a4ae6c] | 117 | * Uses malloc() to get area for the iops, so must be after malloc init |
---|
| 118 | */ |
---|
| 119 | |
---|
| 120 | rtems_libio_init(); |
---|
| 121 | |
---|
[637df35] | 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 | |
---|
| 152 | void |
---|
| 153 | bsp_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 |
---|
[1f94ed6b] | 169 | |
---|
| 170 | /* |
---|
| 171 | * Dump malloc stats on exit... |
---|
| 172 | */ |
---|
| 173 | #if defined(RTEMS_DEBUG) |
---|
| 174 | atexit(malloc_dump); |
---|
| 175 | #endif |
---|
[637df35] | 176 | } |
---|
| 177 | |
---|
[3a4ae6c] | 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 | |
---|
| 186 | void |
---|
| 187 | bsp_postdriver_hook(void) |
---|
| 188 | { |
---|
| 189 | #if 0 |
---|
| 190 | int stdin_fd, stdout_fd, stderr_fd; |
---|
[9700578] | 191 | int error_code; |
---|
| 192 | |
---|
| 193 | error_code = 'S' << 24 | 'T' << 16; |
---|
[3a4ae6c] | 194 | |
---|
[634e746] | 195 | if ((stdin_fd = __rtems_open("/dev/console", O_RDONLY, 0)) == -1) |
---|
[9700578] | 196 | rtems_fatal_error_occurred( error_code | 'D' << 8 | '0' ); |
---|
[3a4ae6c] | 197 | |
---|
[634e746] | 198 | if ((stdout_fd = __rtems_open("/dev/console", O_WRONLY, 0)) == -1) |
---|
[9700578] | 199 | rtems_fatal_error_occurred( error_code | 'D' << 8 | '1' ); |
---|
[3a4ae6c] | 200 | |
---|
[634e746] | 201 | if ((stderr_fd = __rtems_open("/dev/console", O_WRONLY, 0)) == -1) |
---|
[9700578] | 202 | rtems_fatal_error_occurred( error_code | 'D' << 8 | '2' ); |
---|
[3a4ae6c] | 203 | |
---|
| 204 | if ((stdin_fd != 0) || (stdout_fd != 1) || (stderr_fd != 2)) |
---|
[9700578] | 205 | rtems_fatal_error_occurred( error_code | 'I' << 8 | 'O' ); |
---|
[3a4ae6c] | 206 | #endif |
---|
| 207 | } |
---|
| 208 | |
---|
[637df35] | 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 | |
---|
| 235 | void |
---|
| 236 | bsp_start(void) |
---|
| 237 | { |
---|
[c1403ef1] | 238 | unsigned32 workspace_ptr; |
---|
[637df35] | 239 | |
---|
| 240 | /* |
---|
| 241 | * Copy the table |
---|
| 242 | */ |
---|
| 243 | |
---|
| 244 | BSP_Configuration = Configuration; |
---|
| 245 | |
---|
| 246 | /* |
---|
[c1403ef1] | 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 |
---|
[637df35] | 250 | */ |
---|
| 251 | |
---|
| 252 | if (BSP_Configuration.User_multiprocessing_table) { |
---|
[c1403ef1] | 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; |
---|
[637df35] | 263 | } |
---|
[c1403ef1] | 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; |
---|
[637df35] | 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 | |
---|
[c1403ef1] | 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); |
---|
[637df35] | 296 | |
---|
[c1403ef1] | 297 | BSP_Configuration.work_space_start = (void *) workspace_ptr; |
---|
| 298 | |
---|
[637df35] | 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 | |
---|
[3a4ae6c] | 309 | Cpu_table.postdriver_hook = bsp_postdriver_hook; |
---|
[637df35] | 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 | |
---|
[8cbbe312] | 325 | Cpu_table.extra_mpci_receive_server_stack = 0; |
---|
[637df35] | 326 | |
---|
| 327 | /* |
---|
| 328 | * Add 1 region for RTEMS Malloc |
---|
| 329 | */ |
---|
| 330 | |
---|
[e6d4b1d] | 331 | BSP_Configuration.RTEMS_api_configuration->maximum_regions++; |
---|
[637df35] | 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 | |
---|
[3a4ae6c] | 349 | /* |
---|
| 350 | * Tell libio how many fd's we want and allow it to tweak config |
---|
| 351 | */ |
---|
[637df35] | 352 | |
---|
[3a4ae6c] | 353 | rtems_libio_config(&BSP_Configuration, BSP_LIBIO_MAX_FDS); |
---|
[637df35] | 354 | |
---|
[3a4ae6c] | 355 | /* |
---|
| 356 | * Add 1 extension for MPCI_fatal |
---|
| 357 | */ |
---|
[637df35] | 358 | |
---|
[3a4ae6c] | 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 | */ |
---|
[637df35] | 368 | |
---|
[3a4ae6c] | 369 | bsp_isr_level = rtems_initialize_executive_early( |
---|
| 370 | &BSP_Configuration, |
---|
| 371 | &Cpu_table |
---|
| 372 | ); |
---|
[637df35] | 373 | } |
---|