1------------------------------------------------------------------------------
2--                                                                          --
3--                  GNAT 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) 1991-2017, Florida State University             --
10--          Copyright (C) 1995-2019, Free Software Foundation, Inc.         --
11--                                                                          --
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.                                     --
18--                                                                          --
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.               --
22--                                                                          --
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/>.                                          --
27--                                                                          --
28-- GNARL was developed by the GNARL team at Florida State University.       --
29-- Extensive contributions were provided by Ada Core Technologies, Inc.     --
30--                                                                          --
31------------------------------------------------------------------------------
32
33--  This is the VxWorks version of this package
34
35--  This package encapsulates all direct interfaces to OS services that are
36--  needed by the tasking run-time (libgnarl).
37
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.
40
41with Interfaces.C;
42with System.VxWorks;
43with System.VxWorks.Ext;
44with System.Multiprocessors;
45
46package System.OS_Interface is
47   pragma Preelaborate;
48
49   subtype int             is Interfaces.C.int;
50   subtype unsigned        is Interfaces.C.unsigned;
51   subtype short           is Short_Integer;
52   type unsigned_int       is mod 2 ** int'Size;
53   type long               is new Long_Integer;
54   type unsigned_long      is mod 2 ** long'Size;
55   type long_long          is new Long_Long_Integer;
56   type unsigned_long_long is mod 2 ** long_long'Size;
57   type size_t             is mod 2 ** Standard'Address_Size;
58
59   -----------
60   -- Errno --
61   -----------
62
63   function errno return int;
64   pragma Import (C, errno, "errnoGet");
65
66   EINTR     : constant := 4;
67   EAGAIN    : constant := 35;
68   ENOMEM    : constant := 12;
69   EINVAL    : constant := 22;
70   ETIMEDOUT : constant := 60;
71
72   FUNC_ERR  : constant := -1;
73
74   ----------------------------
75   -- Signals and interrupts --
76   ----------------------------
77
78   NSIG : constant := 64;
79   --  Number of signals on the target OS
80   type Signal is new int range 0 .. Interfaces.C."-" (NSIG, 1);
81
82   Max_HW_Interrupt : constant := System.VxWorks.Num_HW_Interrupts - 1;
83   type HW_Interrupt is new int range 0 .. Max_HW_Interrupt;
84
85   Max_Interrupt : constant := Max_HW_Interrupt;
86   subtype Interrupt_Range is Natural range 0 .. Max_HW_Interrupt;
87   --  For s-interr
88
89   --  Signals common to Vxworks 5.x and 6.x
90
91   SIGILL    : constant :=  4; --  illegal instruction (not reset when caught)
92   SIGABRT   : constant :=  6; --  used by abort, replace SIGIOT in the future
93   SIGFPE    : constant :=  8; --  floating point exception
94   SIGBUS    : constant := 10; --  bus error
95   SIGSEGV   : constant := 11; --  segmentation violation
96
97   --  Signals specific to VxWorks 6.x
98
99   SIGHUP    : constant :=  1; --  hangup
100   SIGINT    : constant :=  2; --  interrupt
101   SIGQUIT   : constant :=  3; --  quit
102   SIGTRAP   : constant :=  5; --  trace trap (not reset when caught)
103   SIGEMT    : constant :=  7; --  EMT instruction
104   SIGKILL   : constant :=  9; --  kill
105   SIGFMT    : constant := 12; --  STACK FORMAT ERROR (not posix)
106   SIGPIPE   : constant := 13; --  write on a pipe with no one to read it
107   SIGALRM   : constant := 14; --  alarm clock
108   SIGTERM   : constant := 15; --  software termination signal from kill
109   SIGCNCL   : constant := 16; --  pthreads cancellation signal
110   SIGSTOP   : constant := 17; --  sendable stop signal not from tty
111   SIGTSTP   : constant := 18; --  stop signal from tty
112   SIGCONT   : constant := 19; --  continue a stopped process
113   SIGCHLD   : constant := 20; --  to parent on child stop or exit
114   SIGTTIN   : constant := 21; --  to readers pgrp upon background tty read
115   SIGTTOU   : constant := 22; --  like TTIN for output
116
117   SIGRES1   : constant := 23; --  reserved signal number (Not POSIX)
118   SIGRES2   : constant := 24; --  reserved signal number (Not POSIX)
119   SIGRES3   : constant := 25; --  reserved signal number (Not POSIX)
120   SIGRES4   : constant := 26; --  reserved signal number (Not POSIX)
121   SIGRES5   : constant := 27; --  reserved signal number (Not POSIX)
122   SIGRES6   : constant := 28; --  reserved signal number (Not POSIX)
123   SIGRES7   : constant := 29; --  reserved signal number (Not POSIX)
124
125   SIGUSR1   : constant := 30; --  user defined signal 1
126   SIGUSR2   : constant := 31; --  user defined signal 2
127
128   SIGPOLL   : constant := 32; --  pollable event
129   SIGPROF   : constant := 33; --  profiling timer expired
130   SIGSYS    : constant := 34; --  bad system call
131   SIGURG    : constant := 35; --  high bandwidth data is available at socket
132   SIGVTALRM : constant := 36; --  virtual timer expired
133   SIGXCPU   : constant := 37; --  CPU time limit exceeded
134   SIGXFSZ   : constant := 38; --  file size time limit exceeded
135
136   SIGEVTS   : constant := 39; --  signal event thread send
137   SIGEVTD   : constant := 40; --  signal event thread delete
138
139   SIGRTMIN  : constant := 48; --  Realtime signal min
140   SIGRTMAX  : constant := 63; --  Realtime signal max
141
142   -----------------------------------
143   -- Signal processing definitions --
144   -----------------------------------
145
146   --  The how in sigprocmask()
147
148   SIG_BLOCK   : constant := 1;
149   SIG_UNBLOCK : constant := 2;
150   SIG_SETMASK : constant := 3;
151
152   --  The sa_flags in struct sigaction
153
154   SA_SIGINFO : constant := 16#0002#;
155   SA_ONSTACK : constant := 16#0004#;
156
157   SIG_DFL : constant := 0;
158   SIG_IGN : constant := 1;
159
160   type sigset_t is private;
161
162   type struct_sigaction is record
163      sa_handler : System.Address;
164      sa_mask    : sigset_t;
165      sa_flags   : int;
166   end record;
167   pragma Convention (C, struct_sigaction);
168   type struct_sigaction_ptr is access all struct_sigaction;
169
170   function sigaddset (set : access sigset_t; sig : Signal) return int;
171   pragma Import (C, sigaddset, "sigaddset");
172
173   function sigdelset (set : access sigset_t; sig : Signal) return int;
174   pragma Import (C, sigdelset, "sigdelset");
175
176   function sigfillset (set : access sigset_t) return int;
177   pragma Import (C, sigfillset, "sigfillset");
178
179   function sigismember (set : access sigset_t; sig : Signal) return int;
180   pragma Import (C, sigismember, "sigismember");
181
182   function sigemptyset (set : access sigset_t) return int;
183   pragma Import (C, sigemptyset, "sigemptyset");
184
185   function sigaction
186     (sig  : Signal;
187      act  : struct_sigaction_ptr;
188      oact : struct_sigaction_ptr) return int;
189   pragma Import (C, sigaction, "sigaction");
190
191   type isr_address is access procedure (sig : int);
192   pragma Convention (C, isr_address);
193
194   function c_signal (sig : Signal; handler : isr_address) return isr_address;
195   pragma Import (C, c_signal, "signal");
196
197   function pthread_sigmask
198     (how  : int;
199      set  : access sigset_t;
200      oset : access sigset_t) return int;
201   pragma Import (C, pthread_sigmask, "sigprocmask");
202
203   subtype t_id is System.VxWorks.Ext.t_id;
204   subtype Thread_Id is t_id;
205   --  Thread_Id and t_id are VxWorks identifiers for tasks. This value,
206   --  although represented as a Long_Integer, is in fact an address. With
207   --  some BSPs, this address can have a value sufficiently high that the
208   --  Thread_Id becomes negative: this should not be considered as an error.
209
210   function kill (pid : t_id; sig : Signal) return int;
211   pragma Inline (kill);
212
213   function getpid return t_id renames System.VxWorks.Ext.getpid;
214
215   function Task_Stop (tid : t_id) return int
216     renames System.VxWorks.Ext.Task_Stop;
217   --  If we are in the kernel space, stop the task whose t_id is given in
218   --  parameter in such a way that it can be examined by the debugger. This
219   --  typically maps to taskSuspend on VxWorks 5 and to taskStop on VxWorks 6.
220
221   function Task_Cont (tid : t_id) return int
222     renames System.VxWorks.Ext.Task_Cont;
223   --  If we are in the kernel space, continue the task whose t_id is given
224   --  in parameter if it has been stopped previously to be examined by the
225   --  debugger (e.g. by taskStop). It typically maps to taskResume on VxWorks
226   --  5 and to taskCont on VxWorks 6.
227
228   function Int_Lock return int renames System.VxWorks.Ext.Int_Lock;
229   --  If we are in the kernel space, lock interrupts. It typically maps to
230   --  intLock.
231
232   function Int_Unlock (Old : int) return int
233     renames System.VxWorks.Ext.Int_Unlock;
234   --  If we are in the kernel space, unlock interrupts. It typically maps to
235   --  intUnlock. The parameter Old is only used on PowerPC where it contains
236   --  the returned value from Int_Lock (the old MPSR).
237
238   ----------
239   -- Time --
240   ----------
241
242   type time_t is new unsigned_long;
243
244   type timespec is record
245      ts_sec  : time_t;
246      ts_nsec : long;
247   end record;
248   pragma Convention (C, timespec);
249
250   type clockid_t is new int;
251
252   function To_Duration (TS : timespec) return Duration;
253   pragma Inline (To_Duration);
254
255   function To_Timespec (D : Duration) return timespec;
256   pragma Inline (To_Timespec);
257   --  Convert a Duration value to a timespec value. Note that in VxWorks,
258   --  timespec is always non-negative (since time_t is defined above as
259   --  unsigned long). This means that there is a potential problem if a
260   --  negative argument is passed for D. However, in actual usage, the
261   --  value of the input argument D is always non-negative, so no problem
262   --  arises in practice.
263
264   function To_Clock_Ticks (D : Duration) return int;
265   --  Convert a duration value (in seconds) into clock ticks
266
267   function clock_gettime
268     (clock_id : clockid_t; tp : access timespec) return int;
269   pragma Import (C, clock_gettime, "clock_gettime");
270
271   ----------------------
272   -- Utility Routines --
273   ----------------------
274
275   function To_VxWorks_Priority (Priority : int) return int;
276   pragma Inline (To_VxWorks_Priority);
277   --  Convenience routine to convert between VxWorks priority and Ada priority
278
279   --------------------------
280   -- VxWorks specific API --
281   --------------------------
282
283   subtype STATUS is int;
284   --  Equivalent of the C type STATUS
285
286   OK    : constant STATUS := 0;
287   ERROR : constant STATUS := Interfaces.C.int (-1);
288
289   function taskIdVerify (tid : t_id) return STATUS;
290   pragma Import (C, taskIdVerify, "taskIdVerify");
291
292   function taskIdSelf return t_id;
293   pragma Import (C, taskIdSelf, "taskIdSelf");
294
295   function taskOptionsGet (tid : t_id; pOptions : access int) return int;
296   pragma Import (C, taskOptionsGet, "taskOptionsGet");
297
298   function taskSuspend (tid : t_id) return int;
299   pragma Import (C, taskSuspend, "taskSuspend");
300
301   function taskResume (tid : t_id) return int;
302   pragma Import (C, taskResume, "taskResume");
303
304   function taskIsSuspended (tid : t_id) return int;
305   pragma Import (C, taskIsSuspended, "taskIsSuspended");
306
307   function taskDelay (ticks : int) return int;
308   pragma Import (C, taskDelay, "taskDelay");
309
310   function sysClkRateGet return int;
311   pragma Import (C, sysClkRateGet, "sysClkRateGet");
312
313   --  VxWorks 5.x specific functions
314   --  Must not be called from run-time for versions that do not support
315   --  taskVarLib: eg VxWorks 6 RTPs
316
317   function taskVarAdd
318     (tid : t_id; pVar : access System.Address) return int;
319   pragma Import (C, taskVarAdd, "taskVarAdd");
320
321   function taskVarDelete
322     (tid : t_id; pVar : access System.Address) return int;
323   pragma Import (C, taskVarDelete, "taskVarDelete");
324
325   function taskVarSet
326     (tid   : t_id;
327      pVar  : access System.Address;
328      value : System.Address) return int;
329   pragma Import (C, taskVarSet, "taskVarSet");
330
331   function taskVarGet
332     (tid  : t_id;
333      pVar : access System.Address) return int;
334   pragma Import (C, taskVarGet, "taskVarGet");
335
336   --  VxWorks 6.x specific functions
337
338   --  Can only be called from the VxWorks 6 run-time libary that supports
339   --  tlsLib, and not by the VxWorks 6.6 SMP library
340
341   function tlsKeyCreate return int;
342   pragma Import (C, tlsKeyCreate, "tlsKeyCreate");
343
344   function tlsValueGet (key : int) return System.Address;
345   pragma Import (C, tlsValueGet, "tlsValueGet");
346
347   function tlsValueSet (key : int; value : System.Address) return STATUS;
348   pragma Import (C, tlsValueSet, "tlsValueSet");
349
350   --  Option flags for taskSpawn
351
352   VX_UNBREAKABLE    : constant := 16#0002#;
353   VX_FP_PRIVATE_ENV : constant := 16#0080#;
354   VX_NO_STACK_FILL  : constant := 16#0100#;
355
356   function taskSpawn
357     (name          : System.Address;  --  Pointer to task name
358      priority      : int;
359      options       : int;
360      stacksize     : size_t;
361      start_routine : System.Address;
362      arg1          : System.Address;
363      arg2          : int := 0;
364      arg3          : int := 0;
365      arg4          : int := 0;
366      arg5          : int := 0;
367      arg6          : int := 0;
368      arg7          : int := 0;
369      arg8          : int := 0;
370      arg9          : int := 0;
371      arg10         : int := 0) return t_id;
372   pragma Import (C, taskSpawn, "taskSpawn");
373
374   procedure taskDelete (tid : t_id);
375   pragma Import (C, taskDelete, "taskDelete");
376
377   function Set_Time_Slice (ticks : int) return int
378     renames System.VxWorks.Ext.Set_Time_Slice;
379   --  Calls kernelTimeSlice under VxWorks 5.x, VxWorks 653, or in VxWorks 6
380   --  kernel apps. Returns ERROR for RTPs, VxWorks 5 /CERT
381
382   function taskPriorityGet (tid : t_id; pPriority : access int) return int;
383   pragma Import (C, taskPriorityGet, "taskPriorityGet");
384
385   function taskPrioritySet (tid : t_id; newPriority : int) return int;
386   pragma Import (C, taskPrioritySet, "taskPrioritySet");
387
388   --  Semaphore creation flags
389
390   SEM_Q_FIFO         : constant := 0;
391   SEM_Q_PRIORITY     : constant := 1;
392   SEM_DELETE_SAFE    : constant := 4;  -- only valid for binary semaphore
393   SEM_INVERSION_SAFE : constant := 8;  -- only valid for binary semaphore
394
395   --  Semaphore initial state flags
396
397   SEM_EMPTY : constant := 0;
398   SEM_FULL  : constant := 1;
399
400   --  Semaphore take (semTake) time constants
401
402   WAIT_FOREVER : constant := -1;
403   NO_WAIT      : constant := 0;
404
405   --  Error codes (errno). The lower level 16 bits are the error code, with
406   --  the upper 16 bits representing the module number in which the error
407   --  occurred. By convention, the module number is 0 for UNIX errors. VxWorks
408   --  reserves module numbers 1-500, with the remaining module numbers being
409   --  available for user applications.
410
411   M_objLib                 : constant := 61 * 2**16;
412   --  semTake() failure with ticks = NO_WAIT
413   S_objLib_OBJ_UNAVAILABLE : constant := M_objLib + 2;
414   --  semTake() timeout with ticks > NO_WAIT
415   S_objLib_OBJ_TIMEOUT     : constant := M_objLib + 4;
416
417   subtype SEM_ID is System.VxWorks.Ext.SEM_ID;
418   --  typedef struct semaphore *SEM_ID;
419
420   --  We use two different kinds of VxWorks semaphores: mutex and binary
421   --  semaphores. A null ID is returned when a semaphore cannot be created.
422
423   function semBCreate (options : int; initial_state : int) return SEM_ID;
424   pragma Import (C, semBCreate, "semBCreate");
425   --  Create a binary semaphore. Return ID, or 0 if memory could not
426   --  be allocated.
427
428   function semMCreate (options : int) return SEM_ID;
429   pragma Import (C, semMCreate, "semMCreate");
430
431   function semDelete (Sem : SEM_ID) return int
432     renames System.VxWorks.Ext.semDelete;
433   --  Delete a semaphore
434
435   function semGive (Sem : SEM_ID) return int;
436   pragma Import (C, semGive, "semGive");
437
438   function semTake (Sem : SEM_ID; timeout : int) return int;
439   pragma Import (C, semTake, "semTake");
440   --  Attempt to take binary semaphore.  Error is returned if operation
441   --  times out
442
443   function semFlush (SemID : SEM_ID) return STATUS;
444   pragma Import (C, semFlush, "semFlush");
445   --  Release all threads blocked on the semaphore
446
447   ------------------------------------------------------------
448   --   Binary Semaphore Wrapper to Support interrupt Tasks  --
449   ------------------------------------------------------------
450
451   type Binary_Semaphore_Id is new Long_Integer;
452
453   function Binary_Semaphore_Create return Binary_Semaphore_Id;
454   pragma Inline (Binary_Semaphore_Create);
455
456   function Binary_Semaphore_Delete (ID : Binary_Semaphore_Id) return int;
457   pragma Inline (Binary_Semaphore_Delete);
458
459   function Binary_Semaphore_Obtain (ID : Binary_Semaphore_Id) return int;
460   pragma Inline (Binary_Semaphore_Obtain);
461
462   function Binary_Semaphore_Release (ID : Binary_Semaphore_Id) return int;
463   pragma Inline (Binary_Semaphore_Release);
464
465   function Binary_Semaphore_Flush (ID : Binary_Semaphore_Id) return int;
466   pragma Inline (Binary_Semaphore_Flush);
467
468   ------------------------------------------------------------
469   -- Hardware Interrupt Wrappers to Support Interrupt Tasks --
470   ------------------------------------------------------------
471
472   type Interrupt_Handler is access procedure (parameter : System.Address);
473   pragma Convention (C, Interrupt_Handler);
474
475   type Interrupt_Vector is new System.Address;
476
477   function Interrupt_Connect
478     (Vector    : Interrupt_Vector;
479      Handler   : Interrupt_Handler;
480      Parameter : System.Address := System.Null_Address) return int;
481   pragma Inline (Interrupt_Connect);
482   --  Use this to set up an user handler. The routine installs a user handler
483   --  which is invoked after the OS has saved enough context for a high-level
484   --  language routine to be safely invoked.
485
486   function Interrupt_Context return int;
487   pragma Inline (Interrupt_Context);
488   --  Return 1 if executing in an interrupt context; return 0 if executing in
489   --  a task context.
490
491   function Interrupt_Number_To_Vector (intNum : int) return Interrupt_Vector;
492   pragma Inline (Interrupt_Number_To_Vector);
493   --  Convert a logical interrupt number to the hardware interrupt vector
494   --  number used to connect the interrupt.
495
496   --------------------------------
497   -- Processor Affinity for SMP --
498   --------------------------------
499
500   function taskCpuAffinitySet (tid : t_id; CPU : int) return int
501     renames System.VxWorks.Ext.taskCpuAffinitySet;
502   --  For SMP run-times the affinity to CPU.
503   --  For uniprocessor systems return ERROR status.
504
505   function taskMaskAffinitySet (tid : t_id; CPU_Set : unsigned) return int
506     renames System.VxWorks.Ext.taskMaskAffinitySet;
507   --  For SMP run-times the affinity to CPU_Set.
508   --  For uniprocessor systems return ERROR status.
509
510   ---------------------
511   -- Multiprocessors --
512   ---------------------
513
514   function Current_CPU return Multiprocessors.CPU;
515   --  Return the id of the current CPU
516
517private
518   type pid_t is new int;
519
520   ERROR_PID : constant pid_t := -1;
521
522   type sigset_t is new System.VxWorks.Ext.sigset_t;
523end System.OS_Interface;
524