1 ------------------------------------------------------------------------------
3 -- GNAT 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) 1991-1994, Florida State University --
10 -- Copyright (C) 1995-2010, Free Software Foundation, Inc. --
12 -- GNARL is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 3, or (at your option) any later ver- --
15 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. --
19 -- As a special exception under Section 7 of GPL version 3, you are granted --
20 -- additional permissions described in the GCC Runtime Library Exception, --
21 -- version 3.1, as published by the Free Software Foundation. --
23 -- You should have received a copy of the GNU General Public License and --
24 -- a copy of the GCC Runtime Library Exception along with this program; --
25 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
26 -- <http://www.gnu.org/licenses/>. --
28 -- GNARL was developed by the GNARL team at Florida State University. --
29 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
31 ------------------------------------------------------------------------------
33 -- This is the VxWorks version of this package
35 -- This package encapsulates all direct interfaces to OS services
36 -- that are needed by the tasking run-time (libgnarl).
38 -- PLEASE DO NOT add any with-clauses to this package or remove the pragma
39 -- Preelaborate. This package is designed to be a bottom-level (leaf) package.
43 with System.VxWorks.Ext;
44 with System.Multiprocessors;
46 package System.OS_Interface is
49 subtype int is Interfaces.C.int;
50 subtype short is Short_Integer;
51 type unsigned_int is mod 2 ** int'Size;
52 type long is new Long_Integer;
53 type unsigned_long is mod 2 ** long'Size;
54 type long_long is new Long_Long_Integer;
55 type unsigned_long_long is mod 2 ** long_long'Size;
56 type size_t is mod 2 ** Standard'Address_Size;
62 function errno return int;
63 pragma Import (C, errno, "errnoGet");
65 EINTR : constant := 4;
66 EAGAIN : constant := 35;
67 ENOMEM : constant := 12;
68 EINVAL : constant := 22;
69 ETIMEDOUT : constant := 60;
71 FUNC_ERR : constant := -1;
73 ----------------------------
74 -- Signals and interrupts --
75 ----------------------------
77 NSIG : constant := 64;
78 -- Number of signals on the target OS
79 type Signal is new int range 0 .. Interfaces.C."-" (NSIG, 1);
81 Max_HW_Interrupt : constant := System.VxWorks.Num_HW_Interrupts - 1;
82 type HW_Interrupt is new int range 0 .. Max_HW_Interrupt;
84 Max_Interrupt : constant := Max_HW_Interrupt;
86 -- Signals common to Vxworks 5.x and 6.x
88 SIGILL : constant := 4; -- illegal instruction (not reset when caught)
89 SIGABRT : constant := 6; -- used by abort, replace SIGIOT in the future
90 SIGFPE : constant := 8; -- floating point exception
91 SIGBUS : constant := 10; -- bus error
92 SIGSEGV : constant := 11; -- segmentation violation
94 -- Signals specific to VxWorks 6.x
96 SIGHUP : constant := 1; -- hangup
97 SIGINT : constant := 2; -- interrupt
98 SIGQUIT : constant := 3; -- quit
99 SIGTRAP : constant := 5; -- trace trap (not reset when caught)
100 SIGEMT : constant := 7; -- EMT instruction
101 SIGKILL : constant := 9; -- kill
102 SIGFMT : constant := 12; -- STACK FORMAT ERROR (not posix)
103 SIGPIPE : constant := 13; -- write on a pipe with no one to read it
104 SIGALRM : constant := 14; -- alarm clock
105 SIGTERM : constant := 15; -- software termination signal from kill
106 SIGCNCL : constant := 16; -- pthreads cancellation signal
107 SIGSTOP : constant := 17; -- sendable stop signal not from tty
108 SIGTSTP : constant := 18; -- stop signal from tty
109 SIGCONT : constant := 19; -- continue a stopped process
110 SIGCHLD : constant := 20; -- to parent on child stop or exit
111 SIGTTIN : constant := 21; -- to readers pgrp upon background tty read
112 SIGTTOU : constant := 22; -- like TTIN for output
114 SIGRES1 : constant := 23; -- reserved signal number (Not POSIX)
115 SIGRES2 : constant := 24; -- reserved signal number (Not POSIX)
116 SIGRES3 : constant := 25; -- reserved signal number (Not POSIX)
117 SIGRES4 : constant := 26; -- reserved signal number (Not POSIX)
118 SIGRES5 : constant := 27; -- reserved signal number (Not POSIX)
119 SIGRES6 : constant := 28; -- reserved signal number (Not POSIX)
120 SIGRES7 : constant := 29; -- reserved signal number (Not POSIX)
122 SIGUSR1 : constant := 30; -- user defined signal 1
123 SIGUSR2 : constant := 31; -- user defined signal 2
125 SIGPOLL : constant := 32; -- pollable event
126 SIGPROF : constant := 33; -- profiling timer expired
127 SIGSYS : constant := 34; -- bad system call
128 SIGURG : constant := 35; -- high bandwidth data is available at socket
129 SIGVTALRM : constant := 36; -- virtual timer expired
130 SIGXCPU : constant := 37; -- CPU time limit exceeded
131 SIGXFSZ : constant := 38; -- file size time limit exceeded
133 SIGEVTS : constant := 39; -- signal event thread send
134 SIGEVTD : constant := 40; -- signal event thread delete
136 SIGRTMIN : constant := 48; -- Realtime signal min
137 SIGRTMAX : constant := 63; -- Realtime signal max
139 -----------------------------------
140 -- Signal processing definitions --
141 -----------------------------------
143 -- The how in sigprocmask()
145 SIG_BLOCK : constant := 1;
146 SIG_UNBLOCK : constant := 2;
147 SIG_SETMASK : constant := 3;
149 -- The sa_flags in struct sigaction
151 SA_SIGINFO : constant := 16#0002#;
152 SA_ONSTACK : constant := 16#0004#;
154 SIG_DFL : constant := 0;
155 SIG_IGN : constant := 1;
157 type sigset_t is private;
159 type struct_sigaction is record
160 sa_handler : System.Address;
164 pragma Convention (C, struct_sigaction);
165 type struct_sigaction_ptr is access all struct_sigaction;
167 function sigaddset (set : access sigset_t; sig : Signal) return int;
168 pragma Import (C, sigaddset, "sigaddset");
170 function sigdelset (set : access sigset_t; sig : Signal) return int;
171 pragma Import (C, sigdelset, "sigdelset");
173 function sigfillset (set : access sigset_t) return int;
174 pragma Import (C, sigfillset, "sigfillset");
176 function sigismember (set : access sigset_t; sig : Signal) return int;
177 pragma Import (C, sigismember, "sigismember");
179 function sigemptyset (set : access sigset_t) return int;
180 pragma Import (C, sigemptyset, "sigemptyset");
184 act : struct_sigaction_ptr;
185 oact : struct_sigaction_ptr) return int;
186 pragma Import (C, sigaction, "sigaction");
188 type isr_address is access procedure (sig : int);
189 pragma Convention (C, isr_address);
191 function c_signal (sig : Signal; handler : isr_address) return isr_address;
192 pragma Import (C, c_signal, "signal");
194 function sigwait (set : access sigset_t; sig : access Signal) return int;
195 pragma Inline (sigwait);
197 function pthread_sigmask
199 set : access sigset_t;
200 oset : access sigset_t) return int;
201 pragma Import (C, pthread_sigmask, "sigprocmask");
203 subtype t_id is System.VxWorks.Ext.t_id;
204 subtype Thread_Id is t_id;
206 function kill (pid : t_id; sig : Signal) return int;
207 pragma Inline (kill);
209 function getpid return t_id renames System.VxWorks.Ext.getpid;
211 function Task_Stop (tid : t_id) return int
212 renames System.VxWorks.Ext.Task_Stop;
213 -- If we are in the kernel space, stop the task whose t_id is
214 -- given in parameter in such a way that it can be examined by the
215 -- debugger. This typically maps to taskSuspend on VxWorks 5 and
216 -- to taskStop on VxWorks 6.
218 function Task_Cont (tid : t_id) return int
219 renames System.VxWorks.Ext.Task_Cont;
220 -- If we are in the kernel space, continue the task whose t_id is
221 -- given in parameter if it has been stopped previously to be examined
222 -- by the debugger (e.g. by taskStop). It typically maps to taskResume
223 -- on VxWorks 5 and to taskCont on VxWorks 6.
225 function Int_Lock return int renames System.VxWorks.Ext.Int_Lock;
226 -- If we are in the kernel space, lock interrupts. It typically maps to
229 function Int_Unlock return int renames System.VxWorks.Ext.Int_Unlock;
230 -- If we are in the kernel space, unlock interrupts. It typically maps to
237 type time_t is new unsigned_long;
239 type timespec is record
243 pragma Convention (C, timespec);
245 type clockid_t is private;
247 CLOCK_REALTIME : constant clockid_t; -- System wide realtime clock
249 function To_Duration (TS : timespec) return Duration;
250 pragma Inline (To_Duration);
252 function To_Timespec (D : Duration) return timespec;
253 pragma Inline (To_Timespec);
255 function To_Clock_Ticks (D : Duration) return int;
256 -- Convert a duration value (in seconds) into clock ticks
258 function clock_gettime
259 (clock_id : clockid_t; tp : access timespec) return int;
260 pragma Import (C, clock_gettime, "clock_gettime");
262 ----------------------
263 -- Utility Routines --
264 ----------------------
266 function To_VxWorks_Priority (Priority : int) return int;
267 pragma Inline (To_VxWorks_Priority);
268 -- Convenience routine to convert between VxWorks priority and Ada priority
270 --------------------------
271 -- VxWorks specific API --
272 --------------------------
274 subtype STATUS is int;
275 -- Equivalent of the C type STATUS
277 OK : constant STATUS := 0;
278 ERROR : constant STATUS := Interfaces.C.int (-1);
280 function taskIdVerify (tid : t_id) return STATUS;
281 pragma Import (C, taskIdVerify, "taskIdVerify");
283 function taskIdSelf return t_id;
284 pragma Import (C, taskIdSelf, "taskIdSelf");
286 function taskOptionsGet (tid : t_id; pOptions : access int) return int;
287 pragma Import (C, taskOptionsGet, "taskOptionsGet");
289 function taskSuspend (tid : t_id) return int;
290 pragma Import (C, taskSuspend, "taskSuspend");
292 function taskResume (tid : t_id) return int;
293 pragma Import (C, taskResume, "taskResume");
295 function taskIsSuspended (tid : t_id) return int;
296 pragma Import (C, taskIsSuspended, "taskIsSuspended");
298 function taskDelay (ticks : int) return int;
299 procedure taskDelay (ticks : int);
300 pragma Import (C, taskDelay, "taskDelay");
302 function sysClkRateGet return int;
303 pragma Import (C, sysClkRateGet, "sysClkRateGet");
305 -- VxWorks 5.x specific functions
306 -- Must not be called from run-time for versions that do not support
307 -- taskVarLib: eg VxWorks 6 RTPs
310 (tid : t_id; pVar : access System.Address) return int;
311 pragma Import (C, taskVarAdd, "taskVarAdd");
313 function taskVarDelete
314 (tid : t_id; pVar : access System.Address) return int;
315 pragma Import (C, taskVarDelete, "taskVarDelete");
319 pVar : access System.Address;
320 value : System.Address) return int;
321 pragma Import (C, taskVarSet, "taskVarSet");
325 pVar : access System.Address) return int;
326 pragma Import (C, taskVarGet, "taskVarGet");
328 -- VxWorks 6.x specific functions
329 -- Can only be called from the VxWorks 6 run-time libary that supports
330 -- tlsLib, and not by the VxWorks 6.6 SMP library
332 function tlsKeyCreate return int;
333 pragma Import (C, tlsKeyCreate, "tlsKeyCreate");
335 function tlsValueGet (key : int) return System.Address;
336 pragma Import (C, tlsValueGet, "tlsValueGet");
338 function tlsValueSet (key : int; value : System.Address) return STATUS;
339 pragma Import (C, tlsValueSet, "tlsValueSet");
341 -- Option flags for taskSpawn
343 VX_UNBREAKABLE : constant := 16#0002#;
344 VX_FP_PRIVATE_ENV : constant := 16#0080#;
345 VX_NO_STACK_FILL : constant := 16#0100#;
348 (name : System.Address; -- Pointer to task name
352 start_routine : System.Address;
353 arg1 : System.Address;
362 arg10 : int := 0) return t_id;
363 pragma Import (C, taskSpawn, "taskSpawn");
365 procedure taskDelete (tid : t_id);
366 pragma Import (C, taskDelete, "taskDelete");
368 function Set_Time_Slice (ticks : int) return int
369 renames System.VxWorks.Ext.Set_Time_Slice;
370 -- Calls kernelTimeSlice under VxWorks 5.x, VxWorks 653, or in VxWorks 6
371 -- kernel apps. Returns ERROR for RTPs, VxWorks 5 /CERT
373 function taskPriorityGet (tid : t_id; pPriority : access int) return int;
374 pragma Import (C, taskPriorityGet, "taskPriorityGet");
376 function taskPrioritySet (tid : t_id; newPriority : int) return int;
377 pragma Import (C, taskPrioritySet, "taskPrioritySet");
379 -- Semaphore creation flags
381 SEM_Q_FIFO : constant := 0;
382 SEM_Q_PRIORITY : constant := 1;
383 SEM_DELETE_SAFE : constant := 4; -- only valid for binary semaphore
384 SEM_INVERSION_SAFE : constant := 8; -- only valid for binary semaphore
386 -- Semaphore initial state flags
388 SEM_EMPTY : constant := 0;
389 SEM_FULL : constant := 1;
391 -- Semaphore take (semTake) time constants
393 WAIT_FOREVER : constant := -1;
394 NO_WAIT : constant := 0;
396 -- Error codes (errno). The lower level 16 bits are the error code, with
397 -- the upper 16 bits representing the module number in which the error
398 -- occurred. By convention, the module number is 0 for UNIX errors. VxWorks
399 -- reserves module numbers 1-500, with the remaining module numbers being
400 -- available for user applications.
402 M_objLib : constant := 61 * 2**16;
403 -- semTake() failure with ticks = NO_WAIT
404 S_objLib_OBJ_UNAVAILABLE : constant := M_objLib + 2;
405 -- semTake() timeout with ticks > NO_WAIT
406 S_objLib_OBJ_TIMEOUT : constant := M_objLib + 4;
408 subtype SEM_ID is System.VxWorks.Ext.SEM_ID;
409 -- typedef struct semaphore *SEM_ID;
411 -- We use two different kinds of VxWorks semaphores: mutex and binary
412 -- semaphores. A null ID is returned when a semaphore cannot be created.
414 function semBCreate (options : int; initial_state : int) return SEM_ID;
415 pragma Import (C, semBCreate, "semBCreate");
416 -- Create a binary semaphore. Return ID, or 0 if memory could not
419 function semMCreate (options : int) return SEM_ID;
420 pragma Import (C, semMCreate, "semMCreate");
422 function semDelete (Sem : SEM_ID) return int
423 renames System.VxWorks.Ext.semDelete;
424 -- Delete a semaphore
426 function semGive (Sem : SEM_ID) return int;
427 pragma Import (C, semGive, "semGive");
429 function semTake (Sem : SEM_ID; timeout : int) return int;
430 pragma Import (C, semTake, "semTake");
431 -- Attempt to take binary semaphore. Error is returned if operation
434 function semFlush (SemID : SEM_ID) return STATUS;
435 pragma Import (C, semFlush, "semFlush");
436 -- Release all threads blocked on the semaphore
438 ------------------------------------------------------------
439 -- Binary Semaphore Wrapper to Support interrupt Tasks --
440 ------------------------------------------------------------
442 type Binary_Semaphore_Id is new Long_Integer;
444 function Binary_Semaphore_Create return Binary_Semaphore_Id;
445 pragma Inline (Binary_Semaphore_Create);
447 function Binary_Semaphore_Delete (ID : Binary_Semaphore_Id) return int;
448 pragma Inline (Binary_Semaphore_Delete);
450 function Binary_Semaphore_Obtain (ID : Binary_Semaphore_Id) return int;
451 pragma Inline (Binary_Semaphore_Obtain);
453 function Binary_Semaphore_Release (ID : Binary_Semaphore_Id) return int;
454 pragma Inline (Binary_Semaphore_Release);
456 function Binary_Semaphore_Flush (ID : Binary_Semaphore_Id) return int;
457 pragma Inline (Binary_Semaphore_Flush);
459 ------------------------------------------------------------
460 -- Hardware Interrupt Wrappers to Support Interrupt Tasks --
461 ------------------------------------------------------------
463 type Interrupt_Handler is access procedure (parameter : System.Address);
464 pragma Convention (C, Interrupt_Handler);
466 type Interrupt_Vector is new System.Address;
468 function Interrupt_Connect
469 (Vector : Interrupt_Vector;
470 Handler : Interrupt_Handler;
471 Parameter : System.Address := System.Null_Address) return int;
472 pragma Inline (Interrupt_Connect);
473 -- Use this to set up an user handler. The routine installs a user
474 -- handler which is invoked after the OS has saved enough context for a
475 -- high-level language routine to be safely invoked.
477 function Interrupt_Context return int;
478 pragma Inline (Interrupt_Context);
479 -- Return 1 if executing in an interrupt context; return 0 if executing in
482 function Interrupt_Number_To_Vector (intNum : int) return Interrupt_Vector;
483 pragma Inline (Interrupt_Number_To_Vector);
484 -- Convert a logical interrupt number to the hardware interrupt vector
485 -- number used to connect the interrupt.
487 --------------------------------
488 -- Processor Affinity for SMP --
489 --------------------------------
491 function taskCpuAffinitySet (tid : t_id; CPU : int) return int
492 renames System.VxWorks.Ext.taskCpuAffinitySet;
493 -- For SMP run-times the affinity to CPU.
494 -- For uniprocessor systems return ERROR status.
496 ---------------------
497 -- Multiprocessors --
498 ---------------------
500 function Current_CPU return Multiprocessors.CPU;
501 -- Return the id of the current CPU
504 type pid_t is new int;
506 ERROR_PID : constant pid_t := -1;
508 type clockid_t is new int;
509 CLOCK_REALTIME : constant clockid_t := 0;
511 type sigset_t is new System.VxWorks.Ext.sigset_t;
512 end System.OS_Interface;