1 | /* |
---|
2 | * COPYRIGHT (c) 1989-2007. |
---|
3 | * On-Line Applications Research Corporation (OAR). |
---|
4 | * |
---|
5 | * The license and distribution terms for this file may be found in |
---|
6 | * the file LICENSE in this distribution or at |
---|
7 | * http://www.rtems.com/license/LICENSE. |
---|
8 | * |
---|
9 | * $Id$ |
---|
10 | */ |
---|
11 | |
---|
12 | #include <bsp.h> |
---|
13 | |
---|
14 | #include <assert.h> |
---|
15 | #include <pthread.h> |
---|
16 | #include <stdlib.h> |
---|
17 | |
---|
18 | #if defined(MAIN_USE_NETWORKING) |
---|
19 | #include <rtems/rtems_bsdnet.h> |
---|
20 | #include "networkconfig.h" |
---|
21 | #endif |
---|
22 | #if defined(ENABLE_UNTAR_ROOT_FILESYSTEM) |
---|
23 | #include <rtems/untar.h> |
---|
24 | /* |
---|
25 | * The tarfile image is built automatically externally. |
---|
26 | */ |
---|
27 | #include "FilesystemImage.h" |
---|
28 | #endif |
---|
29 | |
---|
30 | /* |
---|
31 | * Using GNAT's Distributed Systems Annex support requires |
---|
32 | * each node in the system to have different pid's. In a |
---|
33 | * single CPU RTEMS system, it is always one. This lets the |
---|
34 | * user override the RTEMS default. |
---|
35 | */ |
---|
36 | #ifdef GNAT_PID |
---|
37 | #include <unistd.h> |
---|
38 | |
---|
39 | pid_t getpid() |
---|
40 | { |
---|
41 | return GNAT_PID; |
---|
42 | } |
---|
43 | #endif |
---|
44 | |
---|
45 | /* |
---|
46 | * Set up first argument |
---|
47 | */ |
---|
48 | static int argc = 1; |
---|
49 | static char arg0[20] = "rtems"; |
---|
50 | static char *argv[20] = { arg0 }; |
---|
51 | |
---|
52 | #if defined(MAIN_USE_REQUIRES_COMMAND_LINE) |
---|
53 | |
---|
54 | #define COMMAND_LINE_MAXIMUM 200 |
---|
55 | |
---|
56 | #include <stdio.h> |
---|
57 | #include <ctype.h> |
---|
58 | |
---|
59 | void parse_arguments( |
---|
60 | char *buffer, |
---|
61 | size_t maximum_length |
---|
62 | ) |
---|
63 | { |
---|
64 | char *cp; |
---|
65 | char *linebuf = buffer; |
---|
66 | size_t length; |
---|
67 | |
---|
68 | for (;;) { |
---|
69 | |
---|
70 | #if defined(MAIN_COMMAND_LINE) |
---|
71 | strncpy (linebuf, MAIN_COMMAND_LINE, maximum_length); |
---|
72 | #else |
---|
73 | /* |
---|
74 | * Read a line |
---|
75 | */ |
---|
76 | printf (">>> %s ", argv[0]); |
---|
77 | fflush (stdout); |
---|
78 | fgets (linebuf, maximum_length, stdin); |
---|
79 | |
---|
80 | length = strnlen( linebuf, maximum_length ); |
---|
81 | if ( linebuf[length - 1] == '\n' || linebuf[length - 1] == '\r' ) { |
---|
82 | linebuf[length - 1] = '\0'; |
---|
83 | } |
---|
84 | #endif |
---|
85 | |
---|
86 | /* |
---|
87 | * Break line into arguments |
---|
88 | */ |
---|
89 | cp = linebuf; |
---|
90 | for (;;) { |
---|
91 | while (isspace (*cp)) |
---|
92 | *cp++ = '\0'; |
---|
93 | if (*cp == '\0') |
---|
94 | break; |
---|
95 | if (argc >= ((sizeof argv / sizeof argv[0]) - 1)) { |
---|
96 | printf ("Too many arguments.\n"); |
---|
97 | argc = 0; |
---|
98 | break; |
---|
99 | } |
---|
100 | argv[argc++] = cp; |
---|
101 | while (!isspace (*cp)) { |
---|
102 | if (*cp == '\0') |
---|
103 | break; |
---|
104 | cp++; |
---|
105 | } |
---|
106 | } |
---|
107 | if (argc > 1) { |
---|
108 | argv[argc] = NULL; |
---|
109 | break; |
---|
110 | } |
---|
111 | printf ("You must give some arguments!\n"); |
---|
112 | } |
---|
113 | |
---|
114 | #if defined(DEBUG_COMMAND_LINE_ARGUMENTS) |
---|
115 | { |
---|
116 | int i; |
---|
117 | for (i=0; i<argc ; i++ ) { |
---|
118 | printf( "argv[%d] = ***%s***\n", i, argv[i] ); |
---|
119 | } |
---|
120 | printf( "\n" ); |
---|
121 | } |
---|
122 | #endif |
---|
123 | } |
---|
124 | #endif |
---|
125 | |
---|
126 | /* |
---|
127 | * By having the POSIX_Init thread create a second thread just |
---|
128 | * to invoke gnat_main, we can override all default attributes |
---|
129 | * of the "Ada environment task". Otherwise, we would be |
---|
130 | * stuck with the defaults set by RTEMS. |
---|
131 | */ |
---|
132 | |
---|
133 | void *start_gnat_main( void * argument ) |
---|
134 | { |
---|
135 | extern int gnat_main ( int argc, char **argv, char **envp ); |
---|
136 | |
---|
137 | #if defined(MAIN_USE_REQUIRES_COMMAND_LINE) |
---|
138 | /* |
---|
139 | * This is scoped to match the Ada program. |
---|
140 | */ |
---|
141 | char command_line[ COMMAND_LINE_MAXIMUM ]; |
---|
142 | |
---|
143 | parse_arguments( command_line, COMMAND_LINE_MAXIMUM ); |
---|
144 | #endif |
---|
145 | |
---|
146 | (void) gnat_main ( argc, argv, 0 ); |
---|
147 | |
---|
148 | exit( 0 ); |
---|
149 | |
---|
150 | return 0; |
---|
151 | } |
---|
152 | |
---|
153 | #ifndef GNAT_MAIN_STACKSPACE |
---|
154 | #define GNAT_MAIN_STACKSPACE 0 |
---|
155 | #endif |
---|
156 | |
---|
157 | void *POSIX_Init( void *argument ) |
---|
158 | { |
---|
159 | pthread_t thread_id; |
---|
160 | pthread_attr_t attr; |
---|
161 | size_t stacksize; |
---|
162 | int status; |
---|
163 | extern size_t _ada_pthread_minimum_stack_size(); |
---|
164 | |
---|
165 | #if defined(ENABLE_UNTAR_ROOT_FILESYSTEM) |
---|
166 | printk("Loading filesystem image\n"); |
---|
167 | status = Untar_FromMemory( (char *)FilesystemImage, FilesystemImage_size ); |
---|
168 | #endif |
---|
169 | |
---|
170 | #if defined(MAIN_USE_NETWORKING) |
---|
171 | printk("Initializing Network\n"); |
---|
172 | rtems_bsdnet_initialize_network (); |
---|
173 | rtems_bsdnet_show_inet_routes (); |
---|
174 | #endif |
---|
175 | |
---|
176 | #if defined(MAIN_CALL_C_INITIALIZE_APPLICATION) |
---|
177 | { |
---|
178 | extern void initialize_application(); |
---|
179 | printk("Invoking C Application Initialization\n"); |
---|
180 | initialize_application(); |
---|
181 | } |
---|
182 | #endif |
---|
183 | |
---|
184 | /* |
---|
185 | * Now create the thread that will be the GNAT Ada main. |
---|
186 | */ |
---|
187 | status = pthread_attr_init( &attr ); |
---|
188 | assert( !status ); |
---|
189 | |
---|
190 | stacksize = GNAT_MAIN_STACKSPACE * 1024; |
---|
191 | if ( stacksize < _ada_pthread_minimum_stack_size() ) |
---|
192 | stacksize = _ada_pthread_minimum_stack_size(); |
---|
193 | |
---|
194 | status = pthread_attr_setstacksize( &attr, stacksize ); |
---|
195 | assert( !status ); |
---|
196 | |
---|
197 | attr.schedpolicy = SCHED_RR; |
---|
198 | attr.schedparam.sched_priority = 122; |
---|
199 | status = pthread_create( &thread_id, &attr, start_gnat_main, NULL ); |
---|
200 | assert( !status ); |
---|
201 | |
---|
202 | pthread_exit( 0 ); |
---|
203 | return 0; |
---|
204 | } |
---|
205 | |
---|
206 | /* configuration information */ |
---|
207 | |
---|
208 | #if defined(HAS_EXTRA_CONFIGURATION) |
---|
209 | #include "config.h" |
---|
210 | #endif |
---|
211 | |
---|
212 | /* Standard output and a clock tick so time passes */ |
---|
213 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
214 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
215 | |
---|
216 | /* We need to be able to create sockets */ |
---|
217 | #define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 20 |
---|
218 | |
---|
219 | /* We need the full IMFS to pass the full ACATS */ |
---|
220 | #define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM |
---|
221 | |
---|
222 | /* This is overkill but is definitely enough to run the network stack */ |
---|
223 | #define CONFIGURE_MAXIMUM_TASKS 20 |
---|
224 | #define CONFIGURE_MAXIMUM_SEMAPHORES 20 |
---|
225 | |
---|
226 | /* We want a clock tick every millisecond */ |
---|
227 | #define CONFIGURE_MICROSECONDS_PER_TICK RTEMS_MILLISECONDS_TO_MICROSECONDS(1) |
---|
228 | |
---|
229 | /* The initialization task is a POSIX Initialization thread with default |
---|
230 | * attributes. |
---|
231 | */ |
---|
232 | #define CONFIGURE_POSIX_INIT_THREAD_TABLE |
---|
233 | |
---|
234 | /* We are using GNAT/RTEMS with a maximum of 20 Ada tasks and no fake Ada |
---|
235 | * tasks. |
---|
236 | * NOTE: A fake Ada task is a task created outside the Ada run-time that |
---|
237 | * calls into Ada. |
---|
238 | */ |
---|
239 | #define CONFIGURE_GNAT_RTEMS |
---|
240 | #define CONFIGURE_MAXIMUM_ADA_TASKS 20 |
---|
241 | |
---|
242 | #if !defined(CONFIGURE_MAXIMUM_FAKE_ADA_TASKS) |
---|
243 | #define CONFIGURE_MAXIMUM_FAKE_ADA_TASKS 0 |
---|
244 | #endif |
---|
245 | |
---|
246 | #if !defined(ADA_APPLICATION_NEEDS_EXTRA_MEMORY) |
---|
247 | #define ADA_APPLICATION_NEEDS_EXTRA_MEMORY 0 |
---|
248 | #endif |
---|
249 | |
---|
250 | /* Account for any extra task stack size */ |
---|
251 | #define CONFIGURE_MEMORY_OVERHEAD \ |
---|
252 | (ADA_APPLICATION_NEEDS_EXTRA_MEMORY + GNAT_MAIN_STACKSPACE) |
---|
253 | |
---|
254 | /* Make sure the C Program Heap and Workspace are zeroed for GNAT */ |
---|
255 | #define CONFIGURE_ZERO_WORKSPACE_AUTOMATICALLY TRUE |
---|
256 | |
---|
257 | #define CONFIGURE_INIT |
---|
258 | |
---|
259 | void _flush_cache() {} |
---|
260 | #include <rtems/confdefs.h> |
---|