s-osinte-rtems.ad[bs]: Get_Page_Size cannot return 0.
[gcc.git] / gcc / ada / s-osinte-rtems.ads
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
5 -- S Y S T E M . O S _ I N T E R F A C E --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 1997-2009 Free Software Foundation, Inc. --
10 -- --
11 -- GNARL is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 -- The GNARL files that were developed for RTEMS are maintained by On-Line --
31 -- Applications Research Corporation (http://www.oarcorp.com) in coopera- --
32 -- tion with Ada Core Technologies Inc. and Florida State University. --
33 -- --
34 ------------------------------------------------------------------------------
35
36 -- This is the RTEMS version of this package.
37 --
38 -- RTEMS target names are of the form CPU-rtems.
39 -- This implementation is designed to work on ALL RTEMS targets.
40 -- The RTEMS implementation is primarily based upon the POSIX threads
41 -- API but there are also bindings to GNAT/RTEMS support routines
42 -- to insulate this code from C API specific details and, in some
43 -- cases, obtain target architecture and BSP specific information
44 -- that is unavailable at the time this package is built.
45
46 -- This package encapsulates all direct interfaces to OS services
47 -- that are needed by children of System.
48
49 -- PLEASE DO NOT add any with-clauses to this package
50 -- or remove the pragma Preelaborate.
51 -- It is designed to be a bottom-level (leaf) package.
52
53 with Interfaces.C;
54 package System.OS_Interface is
55 pragma Preelaborate;
56
57 -- This interface assumes that "unsigned" is a 32-bit entity. This
58 -- will correspond to RTEMS object ids.
59
60 subtype rtems_id is Interfaces.C.unsigned;
61
62 subtype int is Interfaces.C.int;
63 subtype short is Interfaces.C.short;
64 subtype long is Interfaces.C.long;
65 subtype unsigned is Interfaces.C.unsigned;
66 subtype unsigned_short is Interfaces.C.unsigned_short;
67 subtype unsigned_long is Interfaces.C.unsigned_long;
68 subtype unsigned_char is Interfaces.C.unsigned_char;
69 subtype plain_char is Interfaces.C.plain_char;
70 subtype size_t is Interfaces.C.size_t;
71
72 -----------
73 -- Errno --
74 -----------
75
76 function errno return int;
77 pragma Import (C, errno, "__get_errno");
78
79 EAGAIN : constant := 11;
80 EINTR : constant := 4;
81 EINVAL : constant := 22;
82 ENOMEM : constant := 12;
83 ETIMEDOUT : constant := 116;
84
85 -------------
86 -- Signals --
87 -------------
88
89 Num_HW_Interrupts : constant := 256;
90
91 Max_HW_Interrupt : constant := Num_HW_Interrupts - 1;
92 type HW_Interrupt is new int range 0 .. Max_HW_Interrupt;
93
94 Max_Interrupt : constant := Max_HW_Interrupt;
95
96 type Signal is new int range 0 .. Max_Interrupt;
97
98 SIGXCPU : constant := 0; -- XCPU
99 SIGHUP : constant := 1; -- hangup
100 SIGINT : constant := 2; -- interrupt (rubout)
101 SIGQUIT : constant := 3; -- quit (ASCD FS)
102 SIGILL : constant := 4; -- illegal instruction (not reset)
103 SIGTRAP : constant := 5; -- trace trap (not reset)
104 SIGIOT : constant := 6; -- IOT instruction
105 SIGABRT : constant := 6; -- used by abort, replace SIGIOT in the future
106 SIGEMT : constant := 7; -- EMT instruction
107 SIGFPE : constant := 8; -- floating point exception
108 SIGKILL : constant := 9; -- kill (cannot be caught or ignored)
109 SIGBUS : constant := 10; -- bus error
110 SIGSEGV : constant := 11; -- segmentation violation
111 SIGSYS : constant := 12; -- bad argument to system call
112 SIGPIPE : constant := 13; -- write on a pipe with no one to read it
113 SIGALRM : constant := 14; -- alarm clock
114 SIGTERM : constant := 15; -- software termination signal from kill
115 SIGUSR1 : constant := 16; -- user defined signal 1
116 SIGUSR2 : constant := 17; -- user defined signal 2
117
118 SIGADAABORT : constant := SIGABRT;
119
120 type Signal_Set is array (Natural range <>) of Signal;
121
122 Unmasked : constant Signal_Set := (SIGTRAP, SIGALRM, SIGEMT);
123 Reserved : constant Signal_Set := (1 .. 1 => SIGKILL);
124
125 type sigset_t is private;
126
127 function sigaddset (set : access sigset_t; sig : Signal) return int;
128 pragma Import (C, sigaddset, "sigaddset");
129
130 function sigdelset (set : access sigset_t; sig : Signal) return int;
131 pragma Import (C, sigdelset, "sigdelset");
132
133 function sigfillset (set : access sigset_t) return int;
134 pragma Import (C, sigfillset, "sigfillset");
135
136 function sigismember (set : access sigset_t; sig : Signal) return int;
137 pragma Import (C, sigismember, "sigismember");
138
139 function sigemptyset (set : access sigset_t) return int;
140 pragma Import (C, sigemptyset, "sigemptyset");
141
142 type struct_sigaction is record
143 sa_flags : int;
144 sa_mask : sigset_t;
145 sa_handler : System.Address;
146 end record;
147 pragma Convention (C, struct_sigaction);
148 type struct_sigaction_ptr is access all struct_sigaction;
149
150 SA_SIGINFO : constant := 16#02#;
151
152 SA_ONSTACK : constant := 16#00#;
153 -- SA_ONSTACK is not defined on RTEMS, but it is referred to in the POSIX
154 -- implementation of System.Interrupt_Management. Therefore we define a
155 -- dummy value of zero here so that setting this flag is a nop.
156
157 SIG_BLOCK : constant := 1;
158 SIG_UNBLOCK : constant := 2;
159 SIG_SETMASK : constant := 3;
160
161 SIG_DFL : constant := 0;
162 SIG_IGN : constant := 1;
163
164 function sigaction
165 (sig : Signal;
166 act : struct_sigaction_ptr;
167 oact : struct_sigaction_ptr) return int;
168 pragma Import (C, sigaction, "sigaction");
169
170 ----------
171 -- Time --
172 ----------
173
174 Time_Slice_Supported : constant Boolean := True;
175 -- Indicates whether time slicing is supported (i.e SCHED_RR is supported)
176
177 type timespec is private;
178
179 type clockid_t is private;
180
181 CLOCK_REALTIME : constant clockid_t;
182
183 function clock_gettime
184 (clock_id : clockid_t;
185 tp : access timespec) return int;
186 pragma Import (C, clock_gettime, "clock_gettime");
187
188 function To_Duration (TS : timespec) return Duration;
189 pragma Inline (To_Duration);
190
191 function To_Timespec (D : Duration) return timespec;
192 pragma Inline (To_Timespec);
193
194 type struct_timeval is private;
195
196 function To_Duration (TV : struct_timeval) return Duration;
197 pragma Inline (To_Duration);
198
199 function To_Timeval (D : Duration) return struct_timeval;
200 pragma Inline (To_Timeval);
201
202 -------------------------
203 -- Priority Scheduling --
204 -------------------------
205
206 SCHED_FIFO : constant := 1;
207 SCHED_RR : constant := 2;
208 SCHED_OTHER : constant := 0;
209
210 function To_Target_Priority
211 (Prio : System.Any_Priority) return Interfaces.C.int;
212 -- Maps System.Any_Priority to a POSIX priority
213
214 -------------
215 -- Process --
216 -------------
217
218 type pid_t is private;
219
220 function kill (pid : pid_t; sig : Signal) return int;
221 pragma Import (C, kill, "kill");
222
223 function getpid return pid_t;
224 pragma Import (C, getpid, "getpid");
225
226 ---------
227 -- LWP --
228 ---------
229
230 function lwp_self return System.Address;
231 -- lwp_self does not exist on this thread library, revert to pthread_self
232 -- which is the closest approximation (with getpid). This function is
233 -- needed to share 7staprop.adb across POSIX-like targets.
234 pragma Import (C, lwp_self, "pthread_self");
235
236 -------------
237 -- Threads --
238 -------------
239
240 type Thread_Body is access
241 function (arg : System.Address) return System.Address;
242 pragma Convention (C, Thread_Body);
243
244 type pthread_t is private;
245 subtype Thread_Id is pthread_t;
246
247 type pthread_mutex_t is limited private;
248 type pthread_cond_t is limited private;
249 type pthread_attr_t is limited private;
250 type pthread_mutexattr_t is limited private;
251 type pthread_condattr_t is limited private;
252 type pthread_key_t is private;
253
254 No_Key : constant pthread_key_t;
255
256 PTHREAD_CREATE_DETACHED : constant := 0;
257
258 PTHREAD_SCOPE_PROCESS : constant := 0;
259 PTHREAD_SCOPE_SYSTEM : constant := 1;
260
261 -----------
262 -- Stack --
263 -----------
264
265 type stack_t is record
266 ss_sp : System.Address;
267 ss_flags : int;
268 ss_size : size_t;
269 end record;
270 pragma Convention (C, stack_t);
271
272 function sigaltstack
273 (ss : not null access stack_t;
274 oss : access stack_t) return int;
275
276 Alternate_Stack : aliased System.Address;
277 -- This is a dummy definition, never used (Alternate_Stack_Size is null)
278
279 Alternate_Stack_Size : constant := 0;
280 -- No alternate signal stack is used on this platform
281
282 Stack_Base_Available : constant Boolean := False;
283 -- Indicates whether the stack base is available on this target.
284 -- This allows us to share s-osinte.adb between all the FSU/RTEMS
285 -- run time.
286 -- Note that this value can only be true if pthread_t has a complete
287 -- definition that corresponds exactly to the C header files.
288
289 function Get_Stack_Base (thread : pthread_t) return Address;
290 pragma Inline (Get_Stack_Base);
291 -- returns the stack base of the specified thread.
292 -- Only call this function when Stack_Base_Available is True.
293
294 -- These two functions are only needed to share s-taprop.adb with
295 -- FSU threads.
296
297 function Get_Page_Size return size_t;
298 function Get_Page_Size return Address;
299 pragma Import (C, Get_Page_Size, "getpagesize");
300 -- Returns the size of a page
301
302 PROT_ON : constant := 0;
303 PROT_OFF : constant := 0;
304
305 function mprotect (addr : Address; len : size_t; prot : int) return int;
306 pragma Import (C, mprotect);
307
308 -----------------------------------------
309 -- Nonstandard Thread Initialization --
310 -----------------------------------------
311
312 procedure pthread_init;
313 -- FSU_THREADS requires pthread_init, which is nonstandard
314 -- and this should be invoked during the elaboration of s-taprop.adb
315 --
316 -- RTEMS does not require this so we provide an empty Ada body.
317
318 -------------------------
319 -- POSIX.1c Section 3 --
320 -------------------------
321
322 function sigwait
323 (set : access sigset_t;
324 sig : access Signal) return int;
325 pragma Import (C, sigwait, "sigwait");
326
327 function pthread_kill
328 (thread : pthread_t;
329 sig : Signal) return int;
330 pragma Import (C, pthread_kill, "pthread_kill");
331
332 function pthread_sigmask
333 (how : int;
334 set : access sigset_t;
335 oset : access sigset_t) return int;
336 pragma Import (C, pthread_sigmask, "pthread_sigmask");
337
338 ----------------------------
339 -- POSIX.1c Section 11 --
340 ----------------------------
341
342 function pthread_mutexattr_init
343 (attr : access pthread_mutexattr_t) return int;
344 pragma Import (C, pthread_mutexattr_init, "pthread_mutexattr_init");
345
346 function pthread_mutexattr_destroy
347 (attr : access pthread_mutexattr_t) return int;
348 pragma Import (C, pthread_mutexattr_destroy, "pthread_mutexattr_destroy");
349
350 function pthread_mutex_init
351 (mutex : access pthread_mutex_t;
352 attr : access pthread_mutexattr_t) return int;
353 pragma Import (C, pthread_mutex_init, "pthread_mutex_init");
354
355 function pthread_mutex_destroy (mutex : access pthread_mutex_t) return int;
356 pragma Import (C, pthread_mutex_destroy, "pthread_mutex_destroy");
357
358 function pthread_mutex_lock (mutex : access pthread_mutex_t) return int;
359 pragma Import (C, pthread_mutex_lock, "pthread_mutex_lock");
360
361 function pthread_mutex_unlock (mutex : access pthread_mutex_t) return int;
362 pragma Import (C, pthread_mutex_unlock, "pthread_mutex_unlock");
363
364 function pthread_condattr_init
365 (attr : access pthread_condattr_t) return int;
366 pragma Import (C, pthread_condattr_init, "pthread_condattr_init");
367
368 function pthread_condattr_destroy
369 (attr : access pthread_condattr_t) return int;
370 pragma Import (C, pthread_condattr_destroy, "pthread_condattr_destroy");
371
372 function pthread_cond_init
373 (cond : access pthread_cond_t;
374 attr : access pthread_condattr_t) return int;
375 pragma Import (C, pthread_cond_init, "pthread_cond_init");
376
377 function pthread_cond_destroy (cond : access pthread_cond_t) return int;
378 pragma Import (C, pthread_cond_destroy, "pthread_cond_destroy");
379
380 function pthread_cond_signal (cond : access pthread_cond_t) return int;
381 pragma Import (C, pthread_cond_signal, "pthread_cond_signal");
382
383 function pthread_cond_wait
384 (cond : access pthread_cond_t;
385 mutex : access pthread_mutex_t) return int;
386 pragma Import (C, pthread_cond_wait, "pthread_cond_wait");
387
388 function pthread_cond_timedwait
389 (cond : access pthread_cond_t;
390 mutex : access pthread_mutex_t;
391 abstime : access timespec) return int;
392 pragma Import (C, pthread_cond_timedwait, "pthread_cond_timedwait");
393
394 Relative_Timed_Wait : constant Boolean := False;
395 -- pthread_cond_timedwait requires an absolute delay time
396
397 --------------------------
398 -- POSIX.1c Section 13 --
399 --------------------------
400
401 PTHREAD_PRIO_NONE : constant := 0;
402 PTHREAD_PRIO_PROTECT : constant := 2;
403 PTHREAD_PRIO_INHERIT : constant := 1;
404
405 function pthread_mutexattr_setprotocol
406 (attr : access pthread_mutexattr_t;
407 protocol : int) return int;
408 pragma Import (C, pthread_mutexattr_setprotocol);
409
410 function pthread_mutexattr_setprioceiling
411 (attr : access pthread_mutexattr_t;
412 prioceiling : int) return int;
413 pragma Import
414 (C, pthread_mutexattr_setprioceiling,
415 "pthread_mutexattr_setprioceiling");
416
417 type struct_sched_param is record
418 sched_priority : int;
419 ss_low_priority : int;
420 ss_replenish_period : timespec;
421 ss_initial_budget : timespec;
422 end record;
423 pragma Convention (C, struct_sched_param);
424
425 function pthread_setschedparam
426 (thread : pthread_t;
427 policy : int;
428 param : access struct_sched_param) return int;
429 pragma Import (C, pthread_setschedparam, "pthread_setschedparam");
430
431 function pthread_attr_setscope
432 (attr : access pthread_attr_t;
433 contentionscope : int) return int;
434 pragma Import (C, pthread_attr_setscope, "pthread_attr_setscope");
435
436 function pthread_attr_setinheritsched
437 (attr : access pthread_attr_t;
438 inheritsched : int) return int;
439 pragma Import (C, pthread_attr_setinheritsched);
440
441 function pthread_attr_setschedpolicy
442 (attr : access pthread_attr_t;
443 policy : int) return int;
444 pragma Import (C, pthread_attr_setschedpolicy);
445
446 function pthread_attr_setschedparam
447 (attr : access pthread_attr_t;
448 sched_param : int) return int;
449 pragma Import (C, pthread_attr_setschedparam);
450
451 function sched_yield return int;
452 pragma Import (C, sched_yield, "sched_yield");
453
454 ---------------------------
455 -- P1003.1c - Section 16 --
456 ---------------------------
457
458 function pthread_attr_init (attributes : access pthread_attr_t) return int;
459 pragma Import (C, pthread_attr_init, "pthread_attr_init");
460
461 function pthread_attr_destroy
462 (attributes : access pthread_attr_t) return int;
463 pragma Import (C, pthread_attr_destroy, "pthread_attr_destroy");
464
465 function pthread_attr_setdetachstate
466 (attr : access pthread_attr_t;
467 detachstate : int) return int;
468 pragma Import (C, pthread_attr_setdetachstate);
469
470 function pthread_attr_setstacksize
471 (attr : access pthread_attr_t;
472 stacksize : size_t) return int;
473 pragma Import (C, pthread_attr_setstacksize, "pthread_attr_setstacksize");
474
475 function pthread_create
476 (thread : access pthread_t;
477 attributes : access pthread_attr_t;
478 start_routine : Thread_Body;
479 arg : System.Address) return int;
480 pragma Import (C, pthread_create, "pthread_create");
481
482 procedure pthread_exit (status : System.Address);
483 pragma Import (C, pthread_exit, "pthread_exit");
484
485 function pthread_self return pthread_t;
486 pragma Import (C, pthread_self, "pthread_self");
487
488 --------------------------
489 -- POSIX.1c Section 17 --
490 --------------------------
491
492 function pthread_setspecific
493 (key : pthread_key_t;
494 value : System.Address) return int;
495 pragma Import (C, pthread_setspecific, "pthread_setspecific");
496
497 function pthread_getspecific (key : pthread_key_t) return System.Address;
498 pragma Import (C, pthread_getspecific, "pthread_getspecific");
499
500 type destructor_pointer is access procedure (arg : System.Address);
501 pragma Convention (C, destructor_pointer);
502
503 function pthread_key_create
504 (key : access pthread_key_t;
505 destructor : destructor_pointer) return int;
506 pragma Import (C, pthread_key_create, "pthread_key_create");
507
508 ------------------------------------------------------------
509 -- Binary Semaphore Wrapper to Support Interrupt Tasks --
510 ------------------------------------------------------------
511
512 type Binary_Semaphore_Id is new rtems_id;
513
514 function Binary_Semaphore_Create return Binary_Semaphore_Id;
515 pragma Import (
516 C,
517 Binary_Semaphore_Create,
518 "__gnat_binary_semaphore_create");
519
520 function Binary_Semaphore_Delete (ID : Binary_Semaphore_Id) return int;
521 pragma Import (
522 C,
523 Binary_Semaphore_Delete,
524 "__gnat_binary_semaphore_delete");
525
526 function Binary_Semaphore_Obtain (ID : Binary_Semaphore_Id) return int;
527 pragma Import (
528 C,
529 Binary_Semaphore_Obtain,
530 "__gnat_binary_semaphore_obtain");
531
532 function Binary_Semaphore_Release (ID : Binary_Semaphore_Id) return int;
533 pragma Import (
534 C,
535 Binary_Semaphore_Release,
536 "__gnat_binary_semaphore_release");
537
538 function Binary_Semaphore_Flush (ID : Binary_Semaphore_Id) return int;
539 pragma Import (
540 C,
541 Binary_Semaphore_Flush,
542 "__gnat_binary_semaphore_flush");
543
544 ------------------------------------------------------------
545 -- Hardware Interrupt Wrappers to Support Interrupt Tasks --
546 ------------------------------------------------------------
547
548 type Interrupt_Handler is access procedure (parameter : System.Address);
549 pragma Convention (C, Interrupt_Handler);
550 type Interrupt_Vector is new System.Address;
551
552 function Interrupt_Connect
553 (vector : Interrupt_Vector;
554 handler : Interrupt_Handler;
555 parameter : System.Address := System.Null_Address) return int;
556 pragma Import (C, Interrupt_Connect, "__gnat_interrupt_connect");
557 -- Use this to set up an user handler. The routine installs a
558 -- a user handler which is invoked after RTEMS has saved enough
559 -- context for a high-level language routine to be safely invoked.
560
561 function Interrupt_Vector_Get
562 (Vector : Interrupt_Vector) return Interrupt_Handler;
563 pragma Import (C, Interrupt_Vector_Get, "__gnat_interrupt_get");
564 -- Use this to get the existing handler for later restoral.
565
566 procedure Interrupt_Vector_Set
567 (Vector : Interrupt_Vector;
568 Handler : Interrupt_Handler);
569 pragma Import (C, Interrupt_Vector_Set, "__gnat_interrupt_set");
570 -- Use this to restore a handler obtained using Interrupt_Vector_Get.
571
572 function Interrupt_Number_To_Vector (intNum : int) return Interrupt_Vector;
573 -- Convert a logical interrupt number to the hardware interrupt vector
574 -- number used to connect the interrupt.
575 pragma Import (
576 C,
577 Interrupt_Number_To_Vector,
578 "__gnat_interrupt_number_to_vector"
579 );
580
581 private
582
583 type sigset_t is new int;
584
585 type pid_t is new int;
586
587 type time_t is new long;
588
589 type timespec is record
590 tv_sec : time_t;
591 tv_nsec : long;
592 end record;
593 pragma Convention (C, timespec);
594
595 type clockid_t is new rtems_id;
596 CLOCK_REALTIME : constant clockid_t := 1;
597
598 type struct_timeval is record
599 tv_sec : int;
600 tv_usec : int;
601 end record;
602 pragma Convention (C, struct_timeval);
603
604 type pthread_attr_t is record
605 is_initialized : int;
606 stackaddr : System.Address;
607 stacksize : int;
608 contentionscope : int;
609 inheritsched : int;
610 schedpolicy : int;
611 schedparam : struct_sched_param;
612 cputime_clocked_allowed : int;
613 detatchstate : int;
614 end record;
615 pragma Convention (C, pthread_attr_t);
616
617 type pthread_condattr_t is record
618 flags : int;
619 process_shared : int;
620 end record;
621 pragma Convention (C, pthread_condattr_t);
622
623 type pthread_mutexattr_t is record
624 is_initialized : int;
625 process_shared : int;
626 prio_ceiling : int;
627 protocol : int;
628 recursive : int;
629 end record;
630 pragma Convention (C, pthread_mutexattr_t);
631
632 type pthread_t is new rtems_id;
633
634 type pthread_mutex_t is new rtems_id;
635
636 type pthread_cond_t is new rtems_id;
637
638 type pthread_key_t is new rtems_id;
639
640 No_Key : constant pthread_key_t := 0;
641
642 end System.OS_Interface;