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-2002 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 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,  59 Temple Place - Suite 330,  Boston, --
20-- MA 02111-1307, USA.                                                      --
21--                                                                          --
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.                                      --
28--                                                                          --
29-- GNARL was developed by the GNARL team at Florida State University.       --
30-- Extensive contributions were provided by Ada Core Technologies, Inc.     --
31--                                                                          --
32------------------------------------------------------------------------------
33
34--  This is the VxWorks version of this package.
35--
36--  VxWorks does not directly support the needed POSIX routines, but it
37--  does have other routines that make it possible to code equivalent
38--  POSIX compliant routines.  The approach taken is to provide an
39--  FSU threads compliant interface.
40
41--  This package encapsulates all direct interfaces to OS services
42--  that are needed by children of System.
43
44--  PLEASE DO NOT add any with-clauses to this package
45--  or remove the pragma Elaborate_Body.
46--  It is designed to be a bottom-level (leaf) package.
47
48with Interfaces.C;
49with System.VxWorks;
50
51package System.OS_Interface is
52   pragma Preelaborate;
53
54   subtype int         is Interfaces.C.int;
55   subtype short       is Short_Integer;
56   type long           is new Long_Integer;
57   type unsigned_long  is mod 2 ** long'Size;
58   type size_t         is mod 2 ** Standard'Address_Size;
59
60   -----------
61   -- Errno --
62   -----------
63
64   function errno return int;
65   pragma Import (C, errno, "errnoGet");
66
67   EINTR     : constant := 4;
68   EAGAIN    : constant := 35;
69   ENOMEM    : constant := 12;
70   EINVAL    : constant := 22;
71   ETIMEDOUT : constant := 60;
72
73   FUNC_ERR  : constant := -1;
74
75   ----------------------------
76   -- Signals and Interrupts --
77   ----------------------------
78
79   NSIG : constant := 32;
80   --  Number of signals on the target OS
81   type Signal is new int range 0 .. Interfaces.C."-" (NSIG, 1);
82
83   Max_HW_Interrupt : constant := System.VxWorks.Num_HW_Interrupts - 1;
84   type HW_Interrupt is new int range 0 .. Max_HW_Interrupt;
85
86   Max_Interrupt : constant := Max_HW_Interrupt;
87
88   SIGILL  : constant :=  4; --  illegal instruction (not reset)
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
93
94   -----------------------------------
95   -- Signal processing definitions --
96   -----------------------------------
97
98   --  The how in sigprocmask().
99   SIG_BLOCK   : constant := 1;
100   SIG_UNBLOCK : constant := 2;
101   SIG_SETMASK : constant := 3;
102
103   --  The sa_flags in struct sigaction.
104   SA_SIGINFO   : constant := 16#0002#;
105   SA_ONSTACK   : constant := 16#0004#;
106
107   SIG_DFL : constant := 0;
108   SIG_IGN : constant := 1;
109
110   type sigset_t is private;
111
112   type struct_sigaction is record
113      sa_handler : System.Address;
114      sa_mask    : sigset_t;
115      sa_flags   : int;
116   end record;
117   pragma Convention (C, struct_sigaction);
118   type struct_sigaction_ptr is access all struct_sigaction;
119
120   function sigaddset (set : access sigset_t; sig : Signal) return int;
121   pragma Import (C, sigaddset, "sigaddset");
122
123   function sigdelset (set : access sigset_t; sig : Signal) return int;
124   pragma Import (C, sigdelset, "sigdelset");
125
126   function sigfillset (set : access sigset_t) return int;
127   pragma Import (C, sigfillset, "sigfillset");
128
129   function sigismember (set : access sigset_t; sig : Signal) return int;
130   pragma Import (C, sigismember, "sigismember");
131
132   function sigemptyset (set : access sigset_t) return int;
133   pragma Import (C, sigemptyset, "sigemptyset");
134
135   function sigaction
136     (sig  : Signal;
137      act  : struct_sigaction_ptr;
138      oact : struct_sigaction_ptr) return int;
139   pragma Import (C, sigaction, "sigaction");
140
141   type isr_address is access procedure (sig : int);
142
143   function c_signal (sig : Signal; handler : isr_address) return isr_address;
144   pragma Import (C, c_signal, "signal");
145
146   function sigwait (set : access sigset_t; sig : access Signal) return int;
147   pragma Inline (sigwait);
148
149   type sigset_t_ptr is access all sigset_t;
150
151   function pthread_sigmask
152     (how  : int;
153      set  : sigset_t_ptr;
154      oset : sigset_t_ptr) return int;
155   pragma Import (C, pthread_sigmask, "sigprocmask");
156
157   type t_id is new long;
158   subtype Thread_Id is t_id;
159
160   function kill (pid : t_id; sig : Signal) return int;
161   pragma Import (C, kill, "kill");
162
163   --  VxWorks doesn't have getpid; taskIdSelf is the equivalent
164   --  routine.
165   function getpid return t_id;
166   pragma Import (C, getpid, "taskIdSelf");
167
168   ----------
169   -- Time --
170   ----------
171
172   type time_t is new unsigned_long;
173
174   type timespec is record
175      ts_sec  : time_t;
176      ts_nsec : long;
177   end record;
178   pragma Convention (C, timespec);
179
180   type clockid_t is private;
181
182   CLOCK_REALTIME : constant clockid_t;   --  System wide realtime clock
183
184   function To_Duration (TS : timespec) return Duration;
185   pragma Inline (To_Duration);
186
187   function To_Timespec (D : Duration) return timespec;
188   pragma Inline (To_Timespec);
189
190   function To_Clock_Ticks (D : Duration) return int;
191   --  Convert a duration value (in seconds) into clock ticks.
192
193   function clock_gettime
194     (clock_id : clockid_t; tp : access timespec) return int;
195   pragma Import (C, clock_gettime, "clock_gettime");
196
197   type ULONG is new unsigned_long;
198
199   procedure tickSet (ticks : ULONG);
200   pragma Import (C, tickSet, "tickSet");
201
202   function tickGet return ULONG;
203   pragma Import (C, tickGet, "tickGet");
204
205   -----------------------------------------------------
206   --  Convenience routine to convert between VxWorks --
207   --  priority and Ada priority.                     --
208   -----------------------------------------------------
209
210   function To_VxWorks_Priority (Priority : in int) return int;
211   pragma Inline (To_VxWorks_Priority);
212
213   --------------------------
214   -- VxWorks specific API --
215   --------------------------
216
217   subtype STATUS is int;
218   --  Equivalent of the C type STATUS
219
220   OK    : constant STATUS := 0;
221   ERROR : constant STATUS := Interfaces.C.int (-1);
222
223   function taskIdVerify (tid : t_id)  return STATUS;
224   pragma Import (C, taskIdVerify, "taskIdVerify");
225
226   function taskIdSelf return t_id;
227   pragma Import (C, taskIdSelf, "taskIdSelf");
228
229   function taskSuspend (tid : t_id) return int;
230   pragma Import (C, taskSuspend, "taskSuspend");
231
232   function taskResume (tid : t_id) return int;
233   pragma Import (C, taskResume, "taskResume");
234
235   function taskIsSuspended (tid : t_id) return int;
236   pragma Import (C, taskIsSuspended, "taskIsSuspended");
237
238   function taskVarAdd
239     (tid : t_id; pVar : access System.Address) return int;
240   pragma Import (C, taskVarAdd, "taskVarAdd");
241
242   function taskVarDelete
243     (tid : t_id; pVar : access System.Address) return int;
244   pragma Import (C, taskVarDelete, "taskVarDelete");
245
246   function taskVarSet
247     (tid   : t_id;
248      pVar  : access System.Address;
249      value : System.Address) return int;
250   pragma Import (C, taskVarSet, "taskVarSet");
251
252   function taskVarGet
253     (tid  : t_id;
254      pVar : access System.Address) return int;
255   pragma Import (C, taskVarGet, "taskVarGet");
256
257   function taskDelay (ticks : int) return int;
258   procedure taskDelay (ticks : int);
259   pragma Import (C, taskDelay, "taskDelay");
260
261   function sysClkRateGet return int;
262   pragma Import (C, sysClkRateGet, "sysClkRateGet");
263
264   --  Option flags for taskSpawn
265
266   VX_UNBREAKABLE    : constant := 16#0002#;
267   VX_FP_TASK        : constant := 16#0008#;
268   VX_FP_PRIVATE_ENV : constant := 16#0080#;
269   VX_NO_STACK_FILL  : constant := 16#0100#;
270
271   function taskSpawn
272     (name          : System.Address;  --  Pointer to task name
273      priority      : int;
274      options       : int;
275      stacksize     : size_t;
276      start_routine : System.Address;
277      arg1          : System.Address;
278      arg2          : int := 0;
279      arg3          : int := 0;
280      arg4          : int := 0;
281      arg5          : int := 0;
282      arg6          : int := 0;
283      arg7          : int := 0;
284      arg8          : int := 0;
285      arg9          : int := 0;
286      arg10         : int := 0) return t_id;
287   pragma Import (C, taskSpawn, "taskSpawn");
288
289   procedure taskDelete (tid : t_id);
290   pragma Import (C, taskDelete, "taskDelete");
291
292   function kernelTimeSlice (ticks : int) return int;
293   pragma Import (C, kernelTimeSlice, "kernelTimeSlice");
294
295   function taskPrioritySet
296     (tid : t_id; newPriority : int) return int;
297   pragma Import (C, taskPrioritySet, "taskPrioritySet");
298
299   --  Semaphore creation flags.
300
301   SEM_Q_FIFO         : constant := 0;
302   SEM_Q_PRIORITY     : constant := 1;
303   SEM_DELETE_SAFE    : constant := 4;  -- only valid for binary semaphore
304   SEM_INVERSION_SAFE : constant := 8;  -- only valid for binary semaphore
305
306   --  Semaphore initial state flags
307
308   SEM_EMPTY : constant := 0;
309   SEM_FULL  : constant := 1;
310
311   --  Semaphore take (semTake) time constants.
312
313   WAIT_FOREVER : constant := -1;
314   NO_WAIT      : constant := 0;
315
316   --  Error codes (errno).  The lower level 16 bits are the
317   --  error code, with the upper 16 bits representing the
318   --  module number in which the error occurred.  By convention,
319   --  the module number is 0 for UNIX errors.  VxWorks reserves
320   --  module numbers 1-500, with the remaining module numbers
321   --  being available for user applications.
322
323   M_objLib                 : constant := 61 * 2**16;
324   --  semTake() failure with ticks = NO_WAIT
325   S_objLib_OBJ_UNAVAILABLE : constant := M_objLib + 2;
326   --  semTake() timeout with ticks > NO_WAIT
327   S_objLib_OBJ_TIMEOUT     : constant := M_objLib + 4;
328
329   type SEM_ID is new System.Address;
330   --  typedef struct semaphore *SEM_ID;
331
332   --  We use two different kinds of VxWorks semaphores: mutex
333   --  and binary semaphores.  A null ID is returned when
334   --  a semaphore cannot be created.
335
336   function semBCreate (options : int; initial_state : int) return SEM_ID;
337   --  Create a binary semaphore. Return ID, or 0 if memory could not
338   --  be allocated.
339   pragma Import (C, semBCreate, "semBCreate");
340
341   function semMCreate (options : int) return SEM_ID;
342   pragma Import (C, semMCreate, "semMCreate");
343
344   function semDelete (Sem : SEM_ID) return int;
345   --  Delete a semaphore
346   pragma Import (C, semDelete, "semDelete");
347
348   function semGive (Sem : SEM_ID) return int;
349   pragma Import (C, semGive, "semGive");
350
351   function semTake (Sem : SEM_ID; timeout : int) return int;
352   --  Attempt to take binary semaphore.  Error is returned if operation
353   --  times out
354   pragma Import (C, semTake, "semTake");
355
356   function semFlush (SemID : SEM_ID) return STATUS;
357   --  Release all threads blocked on the semaphore
358   pragma Import (C, semFlush, "semFlush");
359
360   function taskLock return int;
361   pragma Import (C, taskLock, "taskLock");
362
363   function taskUnlock return int;
364   pragma Import (C, taskUnlock, "taskUnlock");
365
366private
367   type sigset_t is new long;
368
369   type pid_t is new int;
370
371   ERROR_PID : constant pid_t := -1;
372
373   type clockid_t is new int;
374   CLOCK_REALTIME : constant clockid_t := 0;
375
376end System.OS_Interface;
377