File : s-osinte-pikeos4.ads
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) 2009-2016, AdaCore --
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 3, 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. --
17 -- --
18 -- --
19 -- --
20 -- --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 -- This is the Ravenscar version of this package for PikeOS
33
34 -- This package encapsulates all direct interfaces to OS services that are
35 -- needed by the tasking run-time (libgnarl).
36
37 pragma Restrictions (No_Elaboration_Code);
38
39 with System.Parameters;
40 with System.Multiprocessors;
41
42 package System.OS_Interface is
43 pragma Preelaborate;
44
45 -- PikeOS binding
46
47 -- The definitions are in p4.h
48
49 type P4_uint8_t is mod 2**8 with Convention => C;
50 type P4_uint32_t is mod 2**32 with Convention => C;
51 type P4_uint64_t is mod 2**64 with Convention => C;
52
53 subtype P4_uid_t is P4_uint32_t;
54 subtype P4_thr_t is P4_uint32_t;
55 subtype P4_time_t is P4_uint64_t;
56 subtype P4_prio_t is P4_uint32_t;
57 subtype P4_timeout_t is P4_uint64_t;
58 subtype P4_cpureg_t is P4_uint32_t;
59 subtype P4_hm_type_t is P4_uint32_t; -- An enum
60 subtype P4_intid_t is P4_uint32_t;
61
62 type P4_rulock_list_elem_t is record
63 Next : Address;
64 Prev : Address;
65 end record with Convention => C;
66
67 type P4_tls_area_t is record
68 Tls_Self : Address;
69 Uid : P4_uid_t;
70 Uprio : P4_uint8_t;
71 Kprio : P4_uint8_t;
72 Cpuid : P4_uint8_t;
73 Padding : P4_uint8_t;
74 Rulock_Trying : Address;
75 Rulock_List : P4_rulock_list_elem_t;
76 Rulock_Count : P4_uint32_t;
77 Error : P4_uint32_t;
78 Preempt_Handler : Address;
79 Preempt_Stack : Address;
80 Preempt_Regs : Address;
81 Preempt_Flags : P4_cpureg_t;
82 Sysemu_Handler : Address;
83 Sysemu_Stack : Address;
84 Sysemu_Regs : Address;
85 Sysemu_Flags : P4_cpureg_t;
86 Except_Handler : Address;
87 Except_Stack : Address;
88 Except_Regs : Address;
89 Except_Flags : P4_cpureg_t;
90 Error_Domain : P4_uint32_t;
91 Error_Type : P4_hm_type_t;
92 Error_Id : P4_uint32_t;
93 Error_Code : P4_uint32_t;
94 Error_Msg : Address;
95 Error_Msg_Size : P4_uint32_t;
96 end record with Convention => C;
97
98 -- Safety check (the value 104 is sizeof (P4_tls_area_t)
99 pragma Assert (P4_tls_area_t'Size = 104 * 8);
100
101 P4_TIMEOUT_ABSOLUTE : constant P4_timeout_t := 8 * 2 ** 60;
102 P4_TIMEOUT_INFINITE : constant P4_timeout_t := 16#0fffffff_ffffffff#;
103
104 subtype P4_e_t is Integer;
105
106 P4_E_OK : constant P4_e_t := 0;
107 P4_E_STATE : constant P4_e_t := 28;
108 P4_E_BADTIMEOUT : constant P4_e_t := 16;
109 P4_TIMEPART_KEEP : constant P4_uint32_t := 16#FFFF_FFFE#;
110 P4_PRIO_KEEP : constant P4_prio_t := 16#FFFF_FFFE#;
111
112 function p4_int_attach (intid : P4_intid_t) return P4_e_t;
113 pragma Import (C, p4_int_attach);
114
115 function p4_int_wait (timeout : P4_timeout_t; flags : P4_uint32_t)
116 return P4_e_t;
117 pragma Import (C, p4_int_wait);
118
119 ----------------
120 -- Interrupts --
121 ----------------
122
123 Max_Interrupt : constant := 16;
124 -- Maximum number of asynchronous interrupts.
125
126 subtype Interrupt_ID is P4_intid_t;
127 -- Interrupt identifiers
128
129 No_Interrupt : constant := 16#Ffff_Ffff#;
130 -- Special value indicating no interrupt (corresponds to P4_INT_DETACH)
131
132 type Interrupt_Handler is access procedure (Id : Interrupt_ID);
133
134 --------------------------
135 -- Interrupt processing --
136 --------------------------
137
138 function Current_Interrupt return Interrupt_ID;
139 -- Function that returns the hardware interrupt currently being handled (if
140 -- any). In case no hardware interrupt is being handled the returned value
141 -- is No_Interrupt.
142
143 procedure Set_Interrupt (Id : Interrupt_ID);
144 -- Indicate that the current thread handles interrupt Id. Set once only on
145 -- interrupt threads (see s-interr).
146
147 ----------
148 -- Time --
149 ----------
150
151 subtype Time is P4_time_t;
152 -- Representation of the time in the underlying tasking system
153
154 type Time_Span is range -2 ** 63 .. 2 ** 63 - 1;
155 for Time_Span'Size use 64;
156 -- Represents length of time intervals in the underlying tasking system
157
158 Ticks_Per_Second : constant Natural := 1_000_000_000;
159 -- Number of clock ticks per second (PikeOS resolution is 1ns)
160
161 function Clock return Time;
162 -- Get the number of ticks elapsed since startup
163
164 procedure Delay_Until (T : Time);
165 -- Suspend the calling task until the absolute time specified by T
166
167 -------------
168 -- Threads --
169 -------------
170
171 type Thread_Descriptor is private;
172 -- Type that contains information about a thread (registers, priority, etc)
173
174 type Thread_Id is access all Thread_Descriptor;
175 -- Identifiers for the underlying threads
176
177 Null_Thread_Id : constant Thread_Id := null;
178 -- Identifier for a non valid thread
179
180 procedure Initialize
181 (Environment_Thread : Thread_Id;
182 Main_Priority : System.Any_Priority);
183 -- Procedure for initializing the underlying tasking system
184
185 procedure Thread_Create
186 (Id : Thread_Id;
187 Code : System.Address;
188 Arg : System.Address;
189 Priority : System.Any_Priority;
190 Base_CPU : System.Multiprocessors.CPU_Range;
191 Stack_Address : System.Address;
192 Stack_Size : System.Parameters.Size_Type);
193 -- Create a new thread
194
195 function Thread_Self return Thread_Id;
196 -- Return the thread identifier for the calling task
197
198 function Lwp_Self return System.Address;
199 -- Return the LWP for the calling task
200
201 ----------
202 -- ATCB --
203 ----------
204
205 procedure Set_ATCB (ATCB : System.Address);
206 -- Associate the specified ATCB to the currently running thread
207
208 function Get_ATCB return System.Address;
209 -- Get the ATCB associated to the currently running thread
210
211 ----------------
212 -- Scheduling --
213 ----------------
214
215 procedure Set_Priority (Priority : System.Any_Priority);
216 -- Set the active priority of the executing thread to the given value
217
218 function Get_Priority (Id : Thread_Id) return System.Any_Priority;
219 -- Get the current base priority of a thread
220
221 procedure Sleep;
222 -- The calling thread is unconditionally suspended
223
224 procedure Wakeup (Id : Thread_Id);
225 -- The referred thread becomes ready (the thread must be suspended)
226
227 ---------------------
228 -- Multiprocessors --
229 ---------------------
230
231 function Current_CPU return Multiprocessors.CPU;
232 -- Return the id of the current CPU
233
234 function Get_Affinity (Id : Thread_Id) return Multiprocessors.CPU_Range;
235 -- Return CPU affinity of the given thread (maybe Not_A_Specific_CPU)
236
237 function Get_CPU (Id : Thread_Id) return Multiprocessors.CPU;
238 -- Return the CPU in charge of the given thread (always a valid CPU)
239
240 private
241 type Thread_Descriptor is record
242 Tls : P4_tls_area_t;
243 -- TLS area defined by pikeos-4
244
245 ATCB : System.Address;
246 -- Address of the Ada Task Control Block corresponding to the Ada task
247 -- that executes on this thread.
248
249 Num : P4_thr_t;
250 -- PikeOS thread identifier
251
252 Interrupt : Interrupt_ID;
253 -- Interrupt handled by the thread, or No_Interrupt if none.
254 end record;
255 end System.OS_Interface;