1 ------------------------------------------------------------------------------
3 -- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS --
5 -- S Y S T E M . O S _ I N T E R F A C E --
9 -- Copyright (C) 1997-2008 Free Software Foundation, Inc. --
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 2, or (at your option) any later ver- --
14 -- sion. GNARL 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNARL; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 -- The GNARL files that were developed for RTEMS are maintained by On-Line --
33 -- Applications Research Corporation (http://www.oarcorp.com) in coopera- --
34 -- tion with Ada Core Technologies Inc. and Florida State University. --
36 ------------------------------------------------------------------------------
38 -- This is the RTEMS version of this package.
40 -- RTEMS target names are of the form CPU-rtems.
41 -- This implementation is designed to work on ALL RTEMS targets.
42 -- The RTEMS implementation is primarily based upon the POSIX threads
43 -- API but there are also bindings to GNAT/RTEMS support routines
44 -- to insulate this code from C API specific details and, in some
45 -- cases, obtain target architecture and BSP specific information
46 -- that is unavailable at the time this package is built.
48 -- This package encapsulates all direct interfaces to OS services
49 -- that are needed by children of System.
51 -- PLEASE DO NOT add any with-clauses to this package
52 -- or remove the pragma Preelaborate.
53 -- It is designed to be a bottom-level (leaf) package.
56 package System
.OS_Interface
is
59 -- This interface assumes that "unsigned" is a 32-bit entity. This
60 -- will correspond to RTEMS object ids.
62 subtype rtems_id
is Interfaces
.C
.unsigned
;
64 subtype int
is Interfaces
.C
.int
;
65 subtype short
is Interfaces
.C
.short
;
66 subtype long
is Interfaces
.C
.long
;
67 subtype unsigned
is Interfaces
.C
.unsigned
;
68 subtype unsigned_short
is Interfaces
.C
.unsigned_short
;
69 subtype unsigned_long
is Interfaces
.C
.unsigned_long
;
70 subtype unsigned_char
is Interfaces
.C
.unsigned_char
;
71 subtype plain_char
is Interfaces
.C
.plain_char
;
72 subtype size_t
is Interfaces
.C
.size_t
;
78 function errno
return int
;
79 pragma Import
(C
, errno
, "__get_errno");
81 EAGAIN
: constant := 11;
82 EINTR
: constant := 4;
83 EINVAL
: constant := 22;
84 ENOMEM
: constant := 12;
85 ETIMEDOUT
: constant := 116;
91 Num_HW_Interrupts
: constant := 256;
93 Max_HW_Interrupt
: constant := Num_HW_Interrupts
- 1;
94 type HW_Interrupt
is new int
range 0 .. Max_HW_Interrupt
;
96 Max_Interrupt
: constant := Max_HW_Interrupt
;
98 type Signal
is new int
range 0 .. Max_Interrupt
;
100 SIGXCPU
: constant := 0; -- XCPU
101 SIGHUP
: constant := 1; -- hangup
102 SIGINT
: constant := 2; -- interrupt (rubout)
103 SIGQUIT
: constant := 3; -- quit (ASCD FS)
104 SIGILL
: constant := 4; -- illegal instruction (not reset)
105 SIGTRAP
: constant := 5; -- trace trap (not reset)
106 SIGIOT
: constant := 6; -- IOT instruction
107 SIGABRT
: constant := 6; -- used by abort, replace SIGIOT in the future
108 SIGEMT
: constant := 7; -- EMT instruction
109 SIGFPE
: constant := 8; -- floating point exception
110 SIGKILL
: constant := 9; -- kill (cannot be caught or ignored)
111 SIGBUS
: constant := 10; -- bus error
112 SIGSEGV
: constant := 11; -- segmentation violation
113 SIGSYS
: constant := 12; -- bad argument to system call
114 SIGPIPE
: constant := 13; -- write on a pipe with no one to read it
115 SIGALRM
: constant := 14; -- alarm clock
116 SIGTERM
: constant := 15; -- software termination signal from kill
117 SIGUSR1
: constant := 16; -- user defined signal 1
118 SIGUSR2
: constant := 17; -- user defined signal 2
120 SIGADAABORT
: constant := SIGABRT
;
122 type Signal_Set
is array (Natural range <>) of Signal
;
124 Unmasked
: constant Signal_Set
:= (SIGTRAP
, SIGALRM
, SIGEMT
);
125 Reserved
: constant Signal_Set
:= (1 .. 1 => SIGKILL
);
127 type sigset_t
is private;
129 function sigaddset
(set
: access sigset_t
; sig
: Signal
) return int
;
130 pragma Import
(C
, sigaddset
, "sigaddset");
132 function sigdelset
(set
: access sigset_t
; sig
: Signal
) return int
;
133 pragma Import
(C
, sigdelset
, "sigdelset");
135 function sigfillset
(set
: access sigset_t
) return int
;
136 pragma Import
(C
, sigfillset
, "sigfillset");
138 function sigismember
(set
: access sigset_t
; sig
: Signal
) return int
;
139 pragma Import
(C
, sigismember
, "sigismember");
141 function sigemptyset
(set
: access sigset_t
) return int
;
142 pragma Import
(C
, sigemptyset
, "sigemptyset");
144 type struct_sigaction
is record
147 sa_handler
: System
.Address
;
149 pragma Convention
(C
, struct_sigaction
);
150 type struct_sigaction_ptr
is access all struct_sigaction
;
152 SA_SIGINFO
: constant := 16#
02#
;
154 SA_ONSTACK
: constant := 16#
00#
;
155 -- SA_ONSTACK is not defined on RTEMS, but it is referred to in the POSIX
156 -- implementation of System.Interrupt_Management. Therefore we define a
157 -- dummy value of zero here so that setting this flag is a nop.
159 SIG_BLOCK
: constant := 1;
160 SIG_UNBLOCK
: constant := 2;
161 SIG_SETMASK
: constant := 3;
163 SIG_DFL
: constant := 0;
164 SIG_IGN
: constant := 1;
168 act
: struct_sigaction_ptr
;
169 oact
: struct_sigaction_ptr
) return int
;
170 pragma Import
(C
, sigaction
, "sigaction");
176 Time_Slice_Supported
: constant Boolean := True;
177 -- Indicates whether time slicing is supported (i.e SCHED_RR is supported)
179 type timespec
is private;
181 type clockid_t
is private;
183 CLOCK_REALTIME
: constant clockid_t
;
185 function clock_gettime
186 (clock_id
: clockid_t
;
187 tp
: access timespec
) return int
;
188 pragma Import
(C
, clock_gettime
, "clock_gettime");
190 function To_Duration
(TS
: timespec
) return Duration;
191 pragma Inline
(To_Duration
);
193 function To_Timespec
(D
: Duration) return timespec
;
194 pragma Inline
(To_Timespec
);
196 type struct_timeval
is private;
198 function To_Duration
(TV
: struct_timeval
) return Duration;
199 pragma Inline
(To_Duration
);
201 function To_Timeval
(D
: Duration) return struct_timeval
;
202 pragma Inline
(To_Timeval
);
204 -------------------------
205 -- Priority Scheduling --
206 -------------------------
208 SCHED_FIFO
: constant := 1;
209 SCHED_RR
: constant := 2;
210 SCHED_OTHER
: constant := 0;
212 function To_Target_Priority
213 (Prio
: System
.Any_Priority
) return Interfaces
.C
.int
;
214 -- Maps System.Any_Priority to a POSIX priority
220 type pid_t
is private;
222 function kill
(pid
: pid_t
; sig
: Signal
) return int
;
223 pragma Import
(C
, kill
, "kill");
225 function getpid
return pid_t
;
226 pragma Import
(C
, getpid
, "getpid");
232 function lwp_self
return System
.Address
;
233 -- lwp_self does not exist on this thread library, revert to pthread_self
234 -- which is the closest approximation (with getpid). This function is
235 -- needed to share 7staprop.adb across POSIX-like targets.
236 pragma Import
(C
, lwp_self
, "pthread_self");
242 type Thread_Body
is access
243 function (arg
: System
.Address
) return System
.Address
;
244 pragma Convention
(C
, Thread_Body
);
246 type pthread_t
is private;
247 subtype Thread_Id
is pthread_t
;
249 type pthread_mutex_t
is limited private;
250 type pthread_cond_t
is limited private;
251 type pthread_attr_t
is limited private;
252 type pthread_mutexattr_t
is limited private;
253 type pthread_condattr_t
is limited private;
254 type pthread_key_t
is private;
256 No_Key
: constant pthread_key_t
;
258 PTHREAD_CREATE_DETACHED
: constant := 0;
260 PTHREAD_SCOPE_PROCESS
: constant := 0;
261 PTHREAD_SCOPE_SYSTEM
: constant := 1;
267 type stack_t
is record
268 ss_sp
: System
.Address
;
272 pragma Convention
(C
, stack_t
);
275 (ss
: not null access stack_t
;
276 oss
: access stack_t
) return int
;
278 Alternate_Stack
: aliased System
.Address
;
279 -- This is a dummy definition, never used (Alternate_Stack_Size is null)
281 Alternate_Stack_Size
: constant := 0;
282 -- No alternate signal stack is used on this platform
284 Stack_Base_Available
: constant Boolean := False;
285 -- Indicates whether the stack base is available on this target.
286 -- This allows us to share s-osinte.adb between all the FSU/RTEMS
288 -- Note that this value can only be true if pthread_t has a complete
289 -- definition that corresponds exactly to the C header files.
291 function Get_Stack_Base
(thread
: pthread_t
) return Address
;
292 pragma Inline
(Get_Stack_Base
);
293 -- returns the stack base of the specified thread.
294 -- Only call this function when Stack_Base_Available is True.
296 -- These two functions are only needed to share s-taprop.adb with
299 function Get_Page_Size
return size_t
;
300 function Get_Page_Size
return Address
;
301 -- returns the size of a page, or 0 if this is not relevant on this
302 -- target (which is the case for RTEMS)
304 PROT_ON
: constant := 0;
305 PROT_OFF
: constant := 0;
307 function mprotect
(addr
: Address
; len
: size_t
; prot
: int
) return int
;
308 pragma Import
(C
, mprotect
);
310 -----------------------------------------
311 -- Nonstandard Thread Initialization --
312 -----------------------------------------
314 procedure pthread_init
;
315 -- FSU_THREADS requires pthread_init, which is nonstandard
316 -- and this should be invoked during the elaboration of s-taprop.adb
318 -- RTEMS does not require this so we provide an empty Ada body.
320 -------------------------
321 -- POSIX.1c Section 3 --
322 -------------------------
325 (set
: access sigset_t
;
326 sig
: access Signal
) return int
;
327 pragma Import
(C
, sigwait
, "sigwait");
329 function pthread_kill
331 sig
: Signal
) return int
;
332 pragma Import
(C
, pthread_kill
, "pthread_kill");
334 function pthread_sigmask
336 set
: access sigset_t
;
337 oset
: access sigset_t
) return int
;
338 pragma Import
(C
, pthread_sigmask
, "pthread_sigmask");
340 ----------------------------
341 -- POSIX.1c Section 11 --
342 ----------------------------
344 function pthread_mutexattr_init
345 (attr
: access pthread_mutexattr_t
) return int
;
346 pragma Import
(C
, pthread_mutexattr_init
, "pthread_mutexattr_init");
348 function pthread_mutexattr_destroy
349 (attr
: access pthread_mutexattr_t
) return int
;
350 pragma Import
(C
, pthread_mutexattr_destroy
, "pthread_mutexattr_destroy");
352 function pthread_mutex_init
353 (mutex
: access pthread_mutex_t
;
354 attr
: access pthread_mutexattr_t
) return int
;
355 pragma Import
(C
, pthread_mutex_init
, "pthread_mutex_init");
357 function pthread_mutex_destroy
(mutex
: access pthread_mutex_t
) return int
;
358 pragma Import
(C
, pthread_mutex_destroy
, "pthread_mutex_destroy");
360 function pthread_mutex_lock
(mutex
: access pthread_mutex_t
) return int
;
361 pragma Import
(C
, pthread_mutex_lock
, "pthread_mutex_lock");
363 function pthread_mutex_unlock
(mutex
: access pthread_mutex_t
) return int
;
364 pragma Import
(C
, pthread_mutex_unlock
, "pthread_mutex_unlock");
366 function pthread_condattr_init
367 (attr
: access pthread_condattr_t
) return int
;
368 pragma Import
(C
, pthread_condattr_init
, "pthread_condattr_init");
370 function pthread_condattr_destroy
371 (attr
: access pthread_condattr_t
) return int
;
372 pragma Import
(C
, pthread_condattr_destroy
, "pthread_condattr_destroy");
374 function pthread_cond_init
375 (cond
: access pthread_cond_t
;
376 attr
: access pthread_condattr_t
) return int
;
377 pragma Import
(C
, pthread_cond_init
, "pthread_cond_init");
379 function pthread_cond_destroy
(cond
: access pthread_cond_t
) return int
;
380 pragma Import
(C
, pthread_cond_destroy
, "pthread_cond_destroy");
382 function pthread_cond_signal
(cond
: access pthread_cond_t
) return int
;
383 pragma Import
(C
, pthread_cond_signal
, "pthread_cond_signal");
385 function pthread_cond_wait
386 (cond
: access pthread_cond_t
;
387 mutex
: access pthread_mutex_t
) return int
;
388 pragma Import
(C
, pthread_cond_wait
, "pthread_cond_wait");
390 function pthread_cond_timedwait
391 (cond
: access pthread_cond_t
;
392 mutex
: access pthread_mutex_t
;
393 abstime
: access timespec
) return int
;
394 pragma Import
(C
, pthread_cond_timedwait
, "pthread_cond_timedwait");
396 Relative_Timed_Wait
: constant Boolean := False;
397 -- pthread_cond_timedwait requires an absolute delay time
399 --------------------------
400 -- POSIX.1c Section 13 --
401 --------------------------
403 PTHREAD_PRIO_NONE
: constant := 0;
404 PTHREAD_PRIO_PROTECT
: constant := 2;
405 PTHREAD_PRIO_INHERIT
: constant := 1;
407 function pthread_mutexattr_setprotocol
408 (attr
: access pthread_mutexattr_t
;
409 protocol
: int
) return int
;
410 pragma Import
(C
, pthread_mutexattr_setprotocol
);
412 function pthread_mutexattr_setprioceiling
413 (attr
: access pthread_mutexattr_t
;
414 prioceiling
: int
) return int
;
416 (C
, pthread_mutexattr_setprioceiling
,
417 "pthread_mutexattr_setprioceiling");
419 type struct_sched_param
is record
420 sched_priority
: int
;
421 ss_low_priority
: int
;
422 ss_replenish_period
: timespec
;
423 ss_initial_budget
: timespec
;
425 pragma Convention
(C
, struct_sched_param
);
427 function pthread_setschedparam
430 param
: access struct_sched_param
) return int
;
431 pragma Import
(C
, pthread_setschedparam
, "pthread_setschedparam");
433 function pthread_attr_setscope
434 (attr
: access pthread_attr_t
;
435 contentionscope
: int
) return int
;
436 pragma Import
(C
, pthread_attr_setscope
, "pthread_attr_setscope");
438 function pthread_attr_setinheritsched
439 (attr
: access pthread_attr_t
;
440 inheritsched
: int
) return int
;
441 pragma Import
(C
, pthread_attr_setinheritsched
);
443 function pthread_attr_setschedpolicy
444 (attr
: access pthread_attr_t
;
445 policy
: int
) return int
;
446 pragma Import
(C
, pthread_attr_setschedpolicy
);
448 function pthread_attr_setschedparam
449 (attr
: access pthread_attr_t
;
450 sched_param
: int
) return int
;
451 pragma Import
(C
, pthread_attr_setschedparam
);
453 function sched_yield
return int
;
454 pragma Import
(C
, sched_yield
, "sched_yield");
456 ---------------------------
457 -- P1003.1c - Section 16 --
458 ---------------------------
460 function pthread_attr_init
(attributes
: access pthread_attr_t
) return int
;
461 pragma Import
(C
, pthread_attr_init
, "pthread_attr_init");
463 function pthread_attr_destroy
464 (attributes
: access pthread_attr_t
) return int
;
465 pragma Import
(C
, pthread_attr_destroy
, "pthread_attr_destroy");
467 function pthread_attr_setdetachstate
468 (attr
: access pthread_attr_t
;
469 detachstate
: int
) return int
;
470 pragma Import
(C
, pthread_attr_setdetachstate
);
472 function pthread_attr_setstacksize
473 (attr
: access pthread_attr_t
;
474 stacksize
: size_t
) return int
;
475 pragma Import
(C
, pthread_attr_setstacksize
, "pthread_attr_setstacksize");
477 function pthread_create
478 (thread
: access pthread_t
;
479 attributes
: access pthread_attr_t
;
480 start_routine
: Thread_Body
;
481 arg
: System
.Address
) return int
;
482 pragma Import
(C
, pthread_create
, "pthread_create");
484 procedure pthread_exit
(status
: System
.Address
);
485 pragma Import
(C
, pthread_exit
, "pthread_exit");
487 function pthread_self
return pthread_t
;
488 pragma Import
(C
, pthread_self
, "pthread_self");
490 --------------------------
491 -- POSIX.1c Section 17 --
492 --------------------------
494 function pthread_setspecific
495 (key
: pthread_key_t
;
496 value
: System
.Address
) return int
;
497 pragma Import
(C
, pthread_setspecific
, "pthread_setspecific");
499 function pthread_getspecific
(key
: pthread_key_t
) return System
.Address
;
500 pragma Import
(C
, pthread_getspecific
, "pthread_getspecific");
502 type destructor_pointer
is access procedure (arg
: System
.Address
);
503 pragma Convention
(C
, destructor_pointer
);
505 function pthread_key_create
506 (key
: access pthread_key_t
;
507 destructor
: destructor_pointer
) return int
;
508 pragma Import
(C
, pthread_key_create
, "pthread_key_create");
510 ------------------------------------------------------------
511 -- Binary Semaphore Wrapper to Support Interrupt Tasks --
512 ------------------------------------------------------------
514 type Binary_Semaphore_Id
is new rtems_id
;
516 function Binary_Semaphore_Create
return Binary_Semaphore_Id
;
519 Binary_Semaphore_Create
,
520 "__gnat_binary_semaphore_create");
522 function Binary_Semaphore_Delete
(ID
: Binary_Semaphore_Id
) return int
;
525 Binary_Semaphore_Delete
,
526 "__gnat_binary_semaphore_delete");
528 function Binary_Semaphore_Obtain
(ID
: Binary_Semaphore_Id
) return int
;
531 Binary_Semaphore_Obtain
,
532 "__gnat_binary_semaphore_obtain");
534 function Binary_Semaphore_Release
(ID
: Binary_Semaphore_Id
) return int
;
537 Binary_Semaphore_Release
,
538 "__gnat_binary_semaphore_release");
540 function Binary_Semaphore_Flush
(ID
: Binary_Semaphore_Id
) return int
;
543 Binary_Semaphore_Flush
,
544 "__gnat_binary_semaphore_flush");
546 ------------------------------------------------------------
547 -- Hardware Interrupt Wrappers to Support Interrupt Tasks --
548 ------------------------------------------------------------
550 type Interrupt_Handler
is access procedure (parameter
: System
.Address
);
551 pragma Convention
(C
, Interrupt_Handler
);
552 type Interrupt_Vector
is new System
.Address
;
554 function Interrupt_Connect
555 (vector
: Interrupt_Vector
;
556 handler
: Interrupt_Handler
;
557 parameter
: System
.Address
:= System
.Null_Address
) return int
;
558 pragma Import
(C
, Interrupt_Connect
, "__gnat_interrupt_connect");
559 -- Use this to set up an user handler. The routine installs a
560 -- a user handler which is invoked after RTEMS has saved enough
561 -- context for a high-level language routine to be safely invoked.
563 function Interrupt_Vector_Get
564 (Vector
: Interrupt_Vector
) return Interrupt_Handler
;
565 pragma Import
(C
, Interrupt_Vector_Get
, "__gnat_interrupt_get");
566 -- Use this to get the existing handler for later restoral.
568 procedure Interrupt_Vector_Set
569 (Vector
: Interrupt_Vector
;
570 Handler
: Interrupt_Handler
);
571 pragma Import
(C
, Interrupt_Vector_Set
, "__gnat_interrupt_set");
572 -- Use this to restore a handler obtained using Interrupt_Vector_Get.
574 function Interrupt_Number_To_Vector
(intNum
: int
) return Interrupt_Vector
;
575 -- Convert a logical interrupt number to the hardware interrupt vector
576 -- number used to connect the interrupt.
579 Interrupt_Number_To_Vector
,
580 "__gnat_interrupt_number_to_vector"
585 type sigset_t
is new int
;
587 type pid_t
is new int
;
589 type time_t
is new long
;
591 type timespec
is record
595 pragma Convention
(C
, timespec
);
597 type clockid_t
is new rtems_id
;
598 CLOCK_REALTIME
: constant clockid_t
:= 1;
600 type struct_timeval
is record
604 pragma Convention
(C
, struct_timeval
);
606 type pthread_attr_t
is record
607 is_initialized
: int
;
608 stackaddr
: System
.Address
;
610 contentionscope
: int
;
613 schedparam
: struct_sched_param
;
614 cputime_clocked_allowed
: int
;
617 pragma Convention
(C
, pthread_attr_t
);
619 type pthread_condattr_t
is record
621 process_shared
: int
;
623 pragma Convention
(C
, pthread_condattr_t
);
625 type pthread_mutexattr_t
is record
626 is_initialized
: int
;
627 process_shared
: int
;
632 pragma Convention
(C
, pthread_mutexattr_t
);
634 type pthread_t
is new rtems_id
;
636 type pthread_mutex_t
is new rtems_id
;
638 type pthread_cond_t
is new rtems_id
;
640 type pthread_key_t
is new rtems_id
;
642 No_Key
: constant pthread_key_t
:= 0;
644 end System
.OS_Interface
;