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