3 #include <minix/sysutil.h>
5 #include <minix/timers.h>
6 #include <minix/endpoint.h>
13 #define SEF_SELF_NAME_MAXLEN 20
14 char sef_self_name
[SEF_SELF_NAME_MAXLEN
];
15 endpoint_t sef_self_endpoint
= NONE
;
16 endpoint_t sef_self_proc_nr
;
17 int sef_self_priv_flags
;
18 int sef_self_init_flags
;
19 int sef_self_receiving
;
20 int sef_controlled_crash
;
22 /* Extern variables. */
23 EXTERN
int sef_lu_state
;
24 EXTERN
int __sef_st_before_receive_enabled
;
25 EXTERN
__attribute__((weak
)) int __vm_init_fresh
;
28 #if SEF_INIT_DEBUG || SEF_LU_DEBUG || SEF_PING_DEBUG || SEF_SIGNAL_DEBUG
29 #define SEF_DEBUG_HEADER_MAXLEN 50
30 static int sef_debug_init
= 0;
31 static time_t sef_debug_boottime
= 0;
32 static u32_t sef_debug_system_hz
= 0;
33 static time_t sef_debug_time_sec
= 0;
34 static time_t sef_debug_time_us
= 0;
35 static char sef_debug_header_buff
[SEF_DEBUG_HEADER_MAXLEN
];
36 static void sef_debug_refresh_params(void);
37 char* sef_debug_header(void);
40 /* SEF Init prototypes. */
41 EXTERN
int do_sef_rs_init(endpoint_t old_endpoint
);
42 EXTERN
int do_sef_init_request(message
*m_ptr
);
44 /* SEF Ping prototypes. */
45 EXTERN
int do_sef_ping_request(message
*m_ptr
);
47 /* SEF Live update prototypes. */
48 EXTERN
void do_sef_lu_before_receive(void);
49 EXTERN
int do_sef_lu_request(message
*m_ptr
);
51 /* SEF Signal prototypes. */
52 EXTERN
int do_sef_signal_request(message
*m_ptr
);
54 /* State transfer prototypes. */
55 EXTERN
void do_sef_st_before_receive(void);
57 /* SEF GCOV prototypes. */
59 EXTERN
int do_sef_gcov_request(message
*m_ptr
);
62 /* SEF Fault Injection prototypes. */
63 EXTERN
int do_sef_fi_request(message
*m_ptr
);
65 /*===========================================================================*
67 *===========================================================================*/
70 /* SEF startup interface for system services. */
72 endpoint_t old_endpoint
;
75 int sys_upd_flags
= 0;
77 /* Get information about self. */
78 r
= sys_whoami(&sef_self_endpoint
, sef_self_name
, SEF_SELF_NAME_MAXLEN
,
79 &priv_flags
, &init_flags
);
81 panic("sef_startup: sys_whoami failed: %d\n", r
);
84 sef_self_proc_nr
= _ENDPOINT_P(sef_self_endpoint
);
85 sef_self_priv_flags
= priv_flags
;
86 sef_self_init_flags
= init_flags
;
87 sef_lu_state
= SEF_LU_STATE_NULL
;
88 sef_controlled_crash
= FALSE
;
90 if(init_flags
& SEF_LU_NOMMAP
) {
91 sys_upd_flags
|= SF_VM_NOMMAP
;
95 /* RS may wake up with the wrong endpoint, perfom the update in that case. */
96 if((sef_self_priv_flags
& ROOT_SYS_PROC
) && sef_self_endpoint
!= RS_PROC_NR
) {
97 r
= vm_update(RS_PROC_NR
, sef_self_endpoint
, sys_upd_flags
);
99 panic("unable to update RS from instance %d to %d: %d",
100 RS_PROC_NR
, sef_self_endpoint
, r
);
102 old_endpoint
= sef_self_endpoint
;
103 sef_self_endpoint
= RS_PROC_NR
;
105 #endif /* USE_LIVEUPDATE */
107 #if INTERCEPT_SEF_INIT_REQUESTS
108 /* Intercept SEF Init requests. */
109 if(sef_self_priv_flags
& ROOT_SYS_PROC
) {
110 /* RS initialization is special. */
111 if((r
= do_sef_rs_init(old_endpoint
)) != OK
) {
112 panic("RS unable to complete init: %d", r
);
115 else if(sef_self_endpoint
== VM_PROC_NR
&& __vm_init_fresh
) {
116 /* VM handles fresh initialization by RS later */
120 /* Wait for an initialization message from RS. We need this to learn the
121 * initialization type and parameters. When restarting after a crash, we
122 * may get some spurious IPC messages from RS (e.g. update request) that
123 * were originally meant to be delivered to the old instance. We discard
124 * these messages and block till a proper initialization request arrives.
127 r
= ipc_receive(RS_PROC_NR
, &m
, &status
);
129 panic("unable to ipc_receive from RS: %d", r
);
131 } while(!IS_SEF_INIT_REQUEST(&m
, status
));
133 /* Process initialization request for this system service. */
134 if((r
= do_sef_init_request(&m
)) != OK
) {
135 panic("unable to process init request: %d", r
);
140 /* (Re)initialize SEF variables. */
141 sef_self_priv_flags
= priv_flags
;
142 sef_self_init_flags
= init_flags
;
143 sef_lu_state
= SEF_LU_STATE_NULL
;
144 sef_controlled_crash
= FALSE
;
147 /*===========================================================================*
148 * sef_receive_status *
149 *===========================================================================*/
150 int sef_receive_status(endpoint_t src
, message
*m_ptr
, int *status_ptr
)
152 /* SEF receive() interface for system services. */
153 int r
, status
, m_type
;
155 sef_self_receiving
= TRUE
;
158 /* If the caller indicated that it no longer wants to receive a message,
161 if (!sef_self_receiving
)
164 #if INTERCEPT_SEF_LU_REQUESTS
165 /* Handle SEF Live update before receive events. */
166 if(sef_lu_state
!= SEF_LU_STATE_NULL
) {
167 do_sef_lu_before_receive();
170 /* Handle State transfer before receive events. */
171 if(__sef_st_before_receive_enabled
) {
172 do_sef_st_before_receive();
176 /* Receive and return in case of error. */
177 r
= ipc_receive(src
, m_ptr
, &status
);
178 if(status_ptr
) *status_ptr
= status
;
183 m_type
= m_ptr
->m_type
;
184 if (is_ipc_notify(status
)) {
185 switch (m_ptr
->m_source
) {
187 m_type
= SEF_SIGNAL_REQUEST_TYPE
;
190 m_type
= SEF_PING_REQUEST_TYPE
;
196 #if INTERCEPT_SEF_INIT_REQUESTS
197 case SEF_INIT_REQUEST_TYPE
:
198 /* Intercept SEF Init requests. */
199 if(IS_SEF_INIT_REQUEST(m_ptr
, status
)) {
200 /* Ignore spurious init requests. */
201 if (m_ptr
->m_rs_init
.type
!= SEF_INIT_FRESH
202 || sef_self_endpoint
!= VM_PROC_NR
)
208 #if INTERCEPT_SEF_PING_REQUESTS
209 case SEF_PING_REQUEST_TYPE
:
210 /* Intercept SEF Ping requests. */
211 if(IS_SEF_PING_REQUEST(m_ptr
, status
)) {
212 if(do_sef_ping_request(m_ptr
) == OK
) {
219 #if INTERCEPT_SEF_LU_REQUESTS
220 case SEF_LU_REQUEST_TYPE
:
221 /* Intercept SEF Live update requests. */
222 if(IS_SEF_LU_REQUEST(m_ptr
, status
)) {
223 if(do_sef_lu_request(m_ptr
) == OK
) {
230 #if INTERCEPT_SEF_SIGNAL_REQUESTS
231 case SEF_SIGNAL_REQUEST_TYPE
:
232 /* Intercept SEF Signal requests. */
233 if(IS_SEF_SIGNAL_REQUEST(m_ptr
, status
)) {
234 if(do_sef_signal_request(m_ptr
) == OK
) {
241 #if INTERCEPT_SEF_GCOV_REQUESTS && USE_COVERAGE
242 case SEF_GCOV_REQUEST_TYPE
:
243 /* Intercept GCOV data requests (sent by VFS in vfs/gcov.c). */
244 if(IS_SEF_GCOV_REQUEST(m_ptr
, status
)) {
245 if(do_sef_gcov_request(m_ptr
) == OK
) {
252 #if INTERCEPT_SEF_FI_REQUESTS
253 case SEF_FI_REQUEST_TYPE
:
254 /* Intercept SEF Fault Injection requests. */
255 if(IS_SEF_FI_REQUEST(m_ptr
, status
)) {
256 if(do_sef_fi_request(m_ptr
) == OK
) {
267 /* If we get this far, this is not a valid SEF request, return and
268 * let the caller deal with that.
276 /*===========================================================================*
278 *===========================================================================*/
279 endpoint_t
sef_self(void)
281 /* Return the process's own endpoint number. */
283 if (sef_self_endpoint
== NONE
)
284 panic("sef_self called before initialization");
286 return sef_self_endpoint
;
289 /*===========================================================================*
291 *===========================================================================*/
292 void sef_cancel(void)
294 /* Cancel receiving a message. This function be called from a callback invoked
295 * from within sef_receive_status(), which will then return an EINTR error
296 * code. In particular, this function can be used to exit from the main receive
297 * loop when a signal handler causes the process to want to shut down.
300 sef_self_receiving
= FALSE
;
303 /*===========================================================================*
305 *===========================================================================*/
306 int sef_getrndseed(void)
308 return (int)getticks();
311 /*===========================================================================*
313 *===========================================================================*/
314 void sef_exit(int status
)
316 /* System services use a special version of exit() that generates a
317 * self-termination signal.
320 /* Ask the kernel to exit. */
323 /* If everything else fails, hang. */
324 printf("Warning: system service %d couldn't exit\n", sef_self_endpoint
);
329 __weak_alias(_exit
, sef_exit
);
330 __weak_alias(__exit
, sef_exit
);
333 /*===========================================================================*
335 *===========================================================================*/
336 int sef_munmap(void *addrstart
, vir_bytes len
, int type
)
338 /* System services use a special version of munmap() to control implicit
339 * munmaps as startup and allow for asynchronous mnmap for VM.
343 m
.VMUM_ADDR
= addrstart
;
345 if(sef_self_endpoint
== VM_PROC_NR
) {
346 return asynsend3(SELF
, &m
, AMF_NOREPLY
);
348 return _syscall(VM_PROC_NR
, type
, &m
);
351 #if SEF_INIT_DEBUG || SEF_LU_DEBUG || SEF_PING_DEBUG || SEF_SIGNAL_DEBUG
352 /*===========================================================================*
353 * sef_debug_refresh_params *
354 *===========================================================================*/
355 static void sef_debug_refresh_params(void)
357 /* Refresh SEF debug params. */
360 /* Get boottime and system hz the first time. */
361 if(!sef_debug_init
) {
362 if (sys_times(NONE
, NULL
, NULL
, NULL
, &sef_debug_boottime
) != OK
)
364 else if (sys_getinfo(GET_HZ
, &sef_debug_system_hz
,
365 sizeof(sef_debug_system_hz
), 0, 0) != OK
)
373 if (sef_debug_init
< 1 || sys_times(NONE
, NULL
, NULL
, &uptime
, NULL
) != OK
) {
374 sef_debug_time_sec
= 0;
375 sef_debug_time_us
= 0;
378 /* Compute current time. */
379 sef_debug_time_sec
= (time_t) (sef_debug_boottime
380 + (uptime
/sef_debug_system_hz
));
381 sef_debug_time_us
= (uptime
%sef_debug_system_hz
)
382 * 1000000/sef_debug_system_hz
;
386 /*===========================================================================*
388 *===========================================================================*/
389 char* sef_debug_header(void)
391 /* Build and return a SEF debug header. */
392 sef_debug_refresh_params();
393 snprintf(sef_debug_header_buff
, sizeof(sef_debug_header_buff
),
394 "%s: time = %ds %06dus", sef_self_name
, (int) sef_debug_time_sec
,
395 (int) sef_debug_time_us
);
397 return sef_debug_header_buff
;
399 #endif /*SEF_INIT_DEBUG || SEF_LU_DEBUG || SEF_PING_DEBUG || SEF_SIGNAL_DEBUG*/