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-2021, Free Software Foundation, Inc.         --
11--                                                                          --
12-- GNAT 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.  GNAT 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 a AIX (Native THREADS) 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 Ada.Unchecked_Conversion;
42
43with Interfaces.C;
44with Interfaces.C.Extensions;
45
46with System.Parameters;
47
48package System.OS_Interface is
49   pragma Preelaborate;
50
51   pragma Linker_Options ("-pthread");
52   --  This implies -lpthreads + other things depending on the GCC
53   --  configuration, such as the selection of a proper libgcc variant
54   --  for table-based exception handling when it is available.
55
56   pragma Linker_Options ("-lc_r");
57
58   subtype int            is Interfaces.C.int;
59   subtype short          is Interfaces.C.short;
60   subtype long           is Interfaces.C.long;
61   subtype long_long      is Interfaces.C.Extensions.long_long;
62   subtype unsigned       is Interfaces.C.unsigned;
63   subtype unsigned_short is Interfaces.C.unsigned_short;
64   subtype unsigned_long  is Interfaces.C.unsigned_long;
65   subtype unsigned_char  is Interfaces.C.unsigned_char;
66   subtype plain_char     is Interfaces.C.plain_char;
67   subtype size_t         is Interfaces.C.size_t;
68
69   -----------
70   -- Errno --
71   -----------
72
73   function errno return int;
74   pragma Import (C, errno, "__get_errno");
75
76   EAGAIN    : constant := 11;
77   EINTR     : constant := 4;
78   EINVAL    : constant := 22;
79   ENOMEM    : constant := 12;
80   ETIMEDOUT : constant := 78;
81
82   -------------
83   -- Signals --
84   -------------
85
86   Max_Interrupt : constant := 63;
87   type Signal is new int range 0 .. Max_Interrupt;
88   for Signal'Size use int'Size;
89
90   SIGHUP      : constant := 1; --  hangup
91   SIGINT      : constant := 2; --  interrupt (rubout)
92   SIGQUIT     : constant := 3; --  quit (ASCD FS)
93   SIGILL      : constant := 4; --  illegal instruction (not reset)
94   SIGTRAP     : constant := 5; --  trace trap (not reset)
95   SIGIOT      : constant := 6; --  IOT instruction
96   SIGABRT     : constant := 6; --  used by abort, replace SIGIOT in the future
97   SIGEMT      : constant := 7; --  EMT instruction
98   SIGFPE      : constant := 8; --  floating point exception
99   SIGKILL     : constant := 9; --  kill (cannot be caught or ignored)
100   SIGBUS      : constant := 10; --  bus error
101   SIGSEGV     : constant := 11; --  segmentation violation
102   SIGSYS      : constant := 12; --  bad argument to system call
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   SIGUSR1     : constant := 30; --  user defined signal 1
107   SIGUSR2     : constant := 31; --  user defined signal 2
108   SIGCLD      : constant := 20; --  alias for SIGCHLD
109   SIGCHLD     : constant := 20; --  child status change
110   SIGPWR      : constant := 29; --  power-fail restart
111   SIGWINCH    : constant := 28; --  window size change
112   SIGURG      : constant := 16; --  urgent condition on IO channel
113   SIGPOLL     : constant := 23; --  pollable event occurred
114   SIGIO       : constant := 23; --  I/O possible (Solaris SIGPOLL alias)
115   SIGSTOP     : constant := 17; --  stop (cannot be caught or ignored)
116   SIGTSTP     : constant := 18; --  user stop requested from tty
117   SIGCONT     : constant := 19; --  stopped process has been continued
118   SIGTTIN     : constant := 21; --  background tty read attempted
119   SIGTTOU     : constant := 22; --  background tty write attempted
120   SIGVTALRM   : constant := 34; --  virtual timer expired
121   SIGPROF     : constant := 32; --  profiling timer expired
122   SIGXCPU     : constant := 24; --  CPU time limit exceeded
123   SIGXFSZ     : constant := 25; --  filesize limit exceeded
124   SIGWAITING  : constant := 39; --  m:n scheduling
125
126   --  The following signals are AIX specific
127
128   SIGMSG      : constant := 27; -- input data is in the ring buffer
129   SIGDANGER   : constant := 33; -- system crash imminent
130   SIGMIGRATE  : constant := 35; -- migrate process
131   SIGPRE      : constant := 36; -- programming exception
132   SIGVIRT     : constant := 37; -- AIX virtual time alarm
133   SIGALRM1    : constant := 38; -- m:n condition variables
134   SIGCPUFAIL  : constant := 59; -- Predictive De-configuration of Processors
135   SIGKAP      : constant := 60; -- keep alive poll from native keyboard
136   SIGGRANT    : constant := SIGKAP; -- monitor mode granted
137   SIGRETRACT  : constant := 61; -- monitor mode should be relinquished
138   SIGSOUND    : constant := 62; -- sound control has completed
139   SIGSAK      : constant := 63; -- secure attention key
140
141   SIGADAABORT : constant := SIGEMT;
142   --  Note: on other targets, we usually use SIGABRT, but on AIX, it appears
143   --  that SIGABRT can't be used in sigwait(), so we use SIGEMT.
144   --  SIGEMT is "Emulator Trap Instruction" from the PDP-11, and does not
145   --  have a standardized usage.
146
147   type Signal_Set is array (Natural range <>) of Signal;
148
149   Unmasked : constant Signal_Set :=
150                (SIGTRAP, SIGTTIN, SIGTTOU, SIGTSTP, SIGPROF);
151   Reserved : constant Signal_Set :=
152                (SIGABRT, SIGKILL, SIGSTOP, SIGALRM1, SIGWAITING, SIGCPUFAIL);
153
154   type sigset_t is private;
155
156   function sigaddset (set : access sigset_t; sig : Signal) return int;
157   pragma Import (C, sigaddset, "sigaddset");
158
159   function sigdelset (set : access sigset_t; sig : Signal) return int;
160   pragma Import (C, sigdelset, "sigdelset");
161
162   function sigfillset (set : access sigset_t) return int;
163   pragma Import (C, sigfillset, "sigfillset");
164
165   function sigismember (set : access sigset_t; sig : Signal) return int;
166   pragma Import (C, sigismember, "sigismember");
167
168   function sigemptyset (set : access sigset_t) return int;
169   pragma Import (C, sigemptyset, "sigemptyset");
170
171   type struct_sigaction is record
172      sa_handler : System.Address;
173      sa_mask    : sigset_t;
174      sa_flags   : int;
175   end record;
176   pragma Convention (C, struct_sigaction);
177   type struct_sigaction_ptr is access all struct_sigaction;
178
179   SA_SIGINFO : constant := 16#0100#;
180   SA_ONSTACK : constant := 16#0001#;
181
182   SIG_BLOCK   : constant := 0;
183   SIG_UNBLOCK : constant := 1;
184   SIG_SETMASK : constant := 2;
185
186   SIG_DFL : constant := 0;
187   SIG_IGN : constant := 1;
188
189   function sigaction
190     (sig  : Signal;
191      act  : struct_sigaction_ptr;
192      oact : struct_sigaction_ptr) return int;
193   pragma Import (C, sigaction, "sigaction");
194
195   ----------
196   -- Time --
197   ----------
198
199   Time_Slice_Supported : constant Boolean := True;
200   --  Indicates whether time slicing is supported
201
202   type timespec is private;
203
204   type clockid_t is new long_long;
205
206   function clock_gettime
207     (clock_id : clockid_t;
208      tp       : access timespec) return int;
209   pragma Import (C, clock_gettime, "clock_gettime");
210
211   function clock_getres
212     (clock_id : clockid_t;
213      res      : access timespec) return int;
214   pragma Import (C, clock_getres, "clock_getres");
215
216   function To_Duration (TS : timespec) return Duration;
217   pragma Inline (To_Duration);
218
219   function To_Timespec (D : Duration) return timespec;
220   pragma Inline (To_Timespec);
221
222   type struct_timezone is record
223      tz_minuteswest : int;
224      tz_dsttime     : int;
225   end record;
226   pragma Convention (C, struct_timezone);
227   type struct_timezone_ptr is access all struct_timezone;
228
229   -------------------------
230   -- Priority Scheduling --
231   -------------------------
232
233   SCHED_FIFO  : constant := 1;
234   SCHED_RR    : constant := 2;
235   SCHED_OTHER : constant := 0;
236
237   function To_Target_Priority
238     (Prio : System.Any_Priority) return Interfaces.C.int;
239   --  Maps System.Any_Priority to a POSIX priority
240
241   -------------
242   -- Process --
243   -------------
244
245   type pid_t is private;
246
247   function kill (pid : pid_t; sig : Signal) return int;
248   pragma Import (C, kill, "kill");
249
250   function getpid return pid_t;
251   pragma Import (C, getpid, "getpid");
252
253   ---------
254   -- LWP --
255   ---------
256
257   function lwp_self return System.Address;
258   pragma Import (C, lwp_self, "thread_self");
259
260   -------------
261   -- Threads --
262   -------------
263
264   type Thread_Body is access
265     function (arg : System.Address) return System.Address;
266   pragma Convention (C, Thread_Body);
267
268   function Thread_Body_Access is new
269     Ada.Unchecked_Conversion (System.Address, Thread_Body);
270
271   type pthread_t           is private;
272   subtype Thread_Id        is pthread_t;
273
274   type pthread_mutex_t     is limited private;
275   type pthread_cond_t      is limited private;
276   type pthread_attr_t      is limited private;
277   type pthread_mutexattr_t is limited private;
278   type pthread_condattr_t  is limited private;
279   type pthread_key_t       is private;
280
281   PTHREAD_CREATE_DETACHED : constant := 1;
282
283   PTHREAD_SCOPE_PROCESS : constant := 1;
284   PTHREAD_SCOPE_SYSTEM  : constant := 0;
285
286   --  Read/Write lock not supported on AIX. To add support both types
287   --  pthread_rwlock_t and pthread_rwlockattr_t must properly be defined
288   --  with the associated routines pthread_rwlock_[init/destroy] and
289   --  pthread_rwlock_[rdlock/wrlock/unlock].
290
291   subtype pthread_rwlock_t     is pthread_mutex_t;
292   subtype pthread_rwlockattr_t is pthread_mutexattr_t;
293
294   -----------
295   -- Stack --
296   -----------
297
298   type stack_t is record
299      ss_sp    : System.Address;
300      ss_size  : size_t;
301      ss_flags : int;
302   end record;
303   pragma Convention (C, stack_t);
304
305   function sigaltstack
306     (ss  : not null access stack_t;
307      oss : access stack_t) return int;
308   pragma Import (C, sigaltstack, "sigaltstack");
309
310   Alternate_Stack : aliased System.Address;
311   --  This is a dummy definition, never used (Alternate_Stack_Size is null)
312
313   Alternate_Stack_Size : constant := 0;
314   --  No alternate signal stack is used on this platform
315
316   Stack_Base_Available : constant Boolean := False;
317   --  Indicates whether the stack base is available on this target
318
319   function Get_Stack_Base (thread : pthread_t) return Address;
320   pragma Inline (Get_Stack_Base);
321   --  Returns the stack base of the specified thread. Only call this function
322   --  when Stack_Base_Available is True.
323
324   function Get_Page_Size return int;
325   pragma Import (C, Get_Page_Size, "getpagesize");
326   --  Returns the size of a page
327
328   PROT_NONE  : constant := 0;
329   PROT_READ  : constant := 1;
330   PROT_WRITE : constant := 2;
331   PROT_EXEC  : constant := 4;
332   PROT_ALL   : constant := PROT_READ + PROT_WRITE + PROT_EXEC;
333   PROT_ON    : constant := PROT_READ;
334   PROT_OFF   : constant := PROT_ALL;
335
336   function mprotect (addr : Address; len : size_t; prot : int) return int;
337   pragma Import (C, mprotect);
338
339   ---------------------------------------
340   -- Nonstandard Thread Initialization --
341   ---------------------------------------
342
343   --  Though not documented, pthread_init *must* be called before any other
344   --  pthread call.
345
346   procedure pthread_init;
347   pragma Import (C, pthread_init, "pthread_init");
348
349   -------------------------
350   -- POSIX.1c  Section 3 --
351   -------------------------
352
353   function sigwait
354     (set : access sigset_t;
355      sig : access Signal) return int;
356   pragma Import (C, sigwait, "sigwait");
357
358   function pthread_kill
359     (thread : pthread_t;
360      sig    : Signal) return int;
361   pragma Import (C, pthread_kill, "pthread_kill");
362
363   function pthread_sigmask
364     (how  : int;
365      set  : access sigset_t;
366      oset : access sigset_t) return int;
367   pragma Import (C, pthread_sigmask, "sigthreadmask");
368
369   --------------------------
370   -- POSIX.1c  Section 11 --
371   --------------------------
372
373   function pthread_mutexattr_init
374     (attr : access pthread_mutexattr_t) return int;
375   pragma Import (C, pthread_mutexattr_init, "pthread_mutexattr_init");
376
377   function pthread_mutexattr_destroy
378     (attr : access pthread_mutexattr_t) return int;
379   pragma Import (C, pthread_mutexattr_destroy, "pthread_mutexattr_destroy");
380
381   function pthread_mutex_init
382     (mutex : access pthread_mutex_t;
383      attr  : access pthread_mutexattr_t) return int;
384   pragma Import (C, pthread_mutex_init, "pthread_mutex_init");
385
386   function pthread_mutex_destroy (mutex : access pthread_mutex_t) return int;
387   pragma Import (C, pthread_mutex_destroy, "pthread_mutex_destroy");
388
389   function pthread_mutex_lock (mutex : access pthread_mutex_t) return int;
390   pragma Import (C, pthread_mutex_lock, "pthread_mutex_lock");
391
392   function pthread_mutex_unlock (mutex : access pthread_mutex_t) return int;
393   pragma Import (C, pthread_mutex_unlock, "pthread_mutex_unlock");
394
395   function pthread_condattr_init
396     (attr : access pthread_condattr_t) return int;
397   pragma Import (C, pthread_condattr_init, "pthread_condattr_init");
398
399   function pthread_condattr_destroy
400     (attr : access pthread_condattr_t) return int;
401   pragma Import (C, pthread_condattr_destroy, "pthread_condattr_destroy");
402
403   function pthread_cond_init
404     (cond : access pthread_cond_t;
405      attr : access pthread_condattr_t) return int;
406   pragma Import (C, pthread_cond_init, "pthread_cond_init");
407
408   function pthread_cond_destroy (cond : access pthread_cond_t) return int;
409   pragma Import (C, pthread_cond_destroy, "pthread_cond_destroy");
410
411   function pthread_cond_signal (cond : access pthread_cond_t) return int;
412   pragma Import (C, pthread_cond_signal, "pthread_cond_signal");
413
414   function pthread_cond_wait
415     (cond  : access pthread_cond_t;
416      mutex : access pthread_mutex_t) return int;
417   pragma Import (C, pthread_cond_wait, "pthread_cond_wait");
418
419   function pthread_cond_timedwait
420     (cond    : access pthread_cond_t;
421      mutex   : access pthread_mutex_t;
422      abstime : access timespec) return int;
423   pragma Import (C, pthread_cond_timedwait, "pthread_cond_timedwait");
424
425   --------------------------
426   -- POSIX.1c  Section 13 --
427   --------------------------
428
429   PTHREAD_PRIO_PROTECT : constant := 2;
430
431   function PTHREAD_PRIO_INHERIT return int;
432   --  Return value of C macro PTHREAD_PRIO_INHERIT. This function is needed
433   --  since the value is different between AIX versions.
434
435   function pthread_mutexattr_setprotocol
436     (attr     : access pthread_mutexattr_t;
437      protocol : int) return int;
438   pragma Import (C, pthread_mutexattr_setprotocol);
439
440   function pthread_mutexattr_setprioceiling
441     (attr        : access pthread_mutexattr_t;
442      prioceiling : int) return int;
443   pragma Import (C, pthread_mutexattr_setprioceiling);
444
445   type Array_5_Int is array (0 .. 5) of int;
446   type struct_sched_param is record
447      sched_priority : int;
448      sched_policy   : int;
449      sched_reserved : Array_5_Int;
450   end record;
451
452   function pthread_setschedparam
453     (thread : pthread_t;
454      policy : int;
455      param  : access struct_sched_param) return int;
456   pragma Import (C, pthread_setschedparam, "pthread_setschedparam");
457
458   function pthread_attr_setscope
459     (attr            : access pthread_attr_t;
460      contentionscope : int) return int;
461   pragma Import (C, pthread_attr_setscope, "pthread_attr_setscope");
462
463   function pthread_attr_setinheritsched
464     (attr            : access pthread_attr_t;
465      inheritsched : int) return int;
466   pragma Import (C, pthread_attr_setinheritsched);
467
468   function pthread_attr_setschedpolicy
469     (attr   : access pthread_attr_t;
470      policy : int) return int;
471   pragma Import (C, pthread_attr_setschedpolicy);
472
473   function pthread_attr_setschedparam
474     (attr        : access pthread_attr_t;
475      sched_param : int) return int;
476   pragma Import (C, pthread_attr_setschedparam);
477
478   function sched_yield return int;
479   --  AIX have a nonstandard sched_yield
480
481   --------------------------
482   -- P1003.1c  Section 16 --
483   --------------------------
484
485   function pthread_attr_init (attributes : access pthread_attr_t) return int;
486   pragma Import (C, pthread_attr_init, "pthread_attr_init");
487
488   function pthread_attr_destroy
489     (attributes : access pthread_attr_t) return int;
490   pragma Import (C, pthread_attr_destroy, "pthread_attr_destroy");
491
492   function pthread_attr_setdetachstate
493     (attr        : access pthread_attr_t;
494      detachstate : int) return int;
495   pragma Import (C, pthread_attr_setdetachstate);
496
497   function pthread_attr_setstacksize
498     (attr      : access pthread_attr_t;
499      stacksize : size_t) return int;
500   pragma Import (C, pthread_attr_setstacksize);
501
502   function pthread_create
503     (thread        : access pthread_t;
504      attributes    : access pthread_attr_t;
505      start_routine : Thread_Body;
506      arg           : System.Address)
507     return int;
508   pragma Import (C, pthread_create, "pthread_create");
509
510   procedure pthread_exit (status : System.Address);
511   pragma Import (C, pthread_exit, "pthread_exit");
512
513   function pthread_self return pthread_t;
514   pragma Import (C, pthread_self, "pthread_self");
515
516   --------------------------
517   -- POSIX.1c  Section 17 --
518   --------------------------
519
520   function pthread_setspecific
521     (key   : pthread_key_t;
522      value : System.Address) return int;
523   pragma Import (C, pthread_setspecific, "pthread_setspecific");
524
525   function pthread_getspecific (key : pthread_key_t) return System.Address;
526   pragma Import (C, pthread_getspecific, "pthread_getspecific");
527
528   type destructor_pointer is access procedure (arg : System.Address);
529   pragma Convention (C, destructor_pointer);
530
531   function pthread_key_create
532     (key        : access pthread_key_t;
533      destructor : destructor_pointer) return int;
534   pragma Import (C, pthread_key_create, "pthread_key_create");
535
536private
537   type sigset_t is record
538      losigs : unsigned_long;
539      hisigs : unsigned_long;
540   end record;
541   pragma Convention (C_Pass_By_Copy, sigset_t);
542
543   type pid_t is new int;
544
545   type time_t is range -2 ** (System.Parameters.time_t_bits - 1)
546     .. 2 ** (System.Parameters.time_t_bits - 1) - 1;
547
548   type timespec is record
549      tv_sec  : time_t;
550      tv_nsec : long;
551   end record;
552   pragma Convention (C, timespec);
553
554   type pthread_attr_t is new System.Address;
555   pragma Convention (C, pthread_attr_t);
556   --  typedef struct __pt_attr        *pthread_attr_t;
557
558   type pthread_condattr_t is new System.Address;
559   pragma Convention (C, pthread_condattr_t);
560   --  typedef struct __pt_attr        *pthread_condattr_t;
561
562   type pthread_mutexattr_t is new System.Address;
563   pragma Convention (C, pthread_mutexattr_t);
564   --  typedef struct __pt_attr        *pthread_mutexattr_t;
565
566   type pthread_t is new System.Address;
567   pragma Convention (C, pthread_t);
568   --  typedef void    *pthread_t;
569
570   type ptq_queue;
571   type ptq_queue_ptr is access all ptq_queue;
572
573   type ptq_queue is record
574      ptq_next : ptq_queue_ptr;
575      ptq_prev : ptq_queue_ptr;
576   end record;
577
578   type Array_3_Int is array (0 .. 3) of int;
579   type pthread_mutex_t is record
580        link        : ptq_queue;
581        ptmtx_lock  : int;
582        ptmtx_flags : long;
583        protocol    : int;
584        prioceiling : int;
585        ptmtx_owner : pthread_t;
586        mtx_id      : int;
587        attr        : pthread_attr_t;
588        mtx_kind    : int;
589        lock_cpt    : int;
590        reserved    : Array_3_Int;
591   end record;
592   pragma Convention (C, pthread_mutex_t);
593   type pthread_mutex_t_ptr is access pthread_mutex_t;
594
595   type pthread_cond_t is record
596      link         : ptq_queue;
597      ptcv_lock    : int;
598      ptcv_flags   : long;
599      ptcv_waiters : ptq_queue;
600      cv_id        : int;
601      attr         : pthread_attr_t;
602      mutex        : pthread_mutex_t_ptr;
603      cptwait      : int;
604      reserved     : int;
605   end record;
606   pragma Convention (C, pthread_cond_t);
607
608   type pthread_key_t is new unsigned;
609
610end System.OS_Interface;
611