unixint.d 77.4 KB
Newer Older
1
2
3
4
5
6
7
8
/* -*- mode: c -*- */
/*
    unixint.c -- Unix interrupt interface.
*/
/*
    Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya.
    Copyright (c) 1990, Giuseppe Attardi.
    Copyright (c) 2001, Juan Jose Garcia Ripoll.
9
    Copyrignt (c) 2010-2019, Jean-Claude Beaudoin. (Completely rewritten 2010)
10
11
12
13
14
15
16
17
18

    MKCL is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 3 of the License, or (at your option) any later version.

    See file '../../Copyright' for full details.
*/
#include <mkcl/mkcl.h>
19
#include <mkcl/internal.h>
20
21
22
23
24
25
26
27
#include <mkcl/mkcl-gc.h>

#include <errno.h>
#include <string.h>
#include <stdio.h>

#include <signal.h>

28
29
#if MKCL_WINDOWS
# include <malloc.h> /* for _resetstkoflw(). */
30
# ifndef __MINGW64_VERSION_MAJOR
31
int _resetstkoflw(void); /* since MinGW does not provide it in malloc.h like MS says and MinGW64 does. */
32
# endif
33
34
35
# include <windows.h>
#endif

36
#if MKCL_UNIX
37
38
# include <sys/types.h>
# include <sys/wait.h>
39
40
# include <dlfcn.h>
# include <ucontext.h>
41
# include <sched.h>
42
43
44
45
46
#endif

#include <mkcl/mkcl-fenv.h>
#include <mkcl/mkcl-inl.h>

47
#if MKCL_UNIX
48
49
50
51
typedef void (*mkcl_sighandler_t)(int, siginfo_t *, void *);
#endif


52
#if MKCL_PTHREADS
53
# if MKCL_GC_7_2d
54
55
#  define MK_GC_SIG_SUSPEND MK_GC_suspend_signal()
#  define MK_GC_SIG_THR_RESTART MK_GC_thread_restart_signal()
56
57
58
# else
#  define MK_GC_SIG_SUSPEND MK_GC_get_suspend_signal()
#  define MK_GC_SIG_THR_RESTART MK_GC_get_thr_restart_signal()
59
60
# endif

Jean-Claude Beaudoin's avatar
Typos.    
Jean-Claude Beaudoin committed
61
62
# if 0
#  define DEBUG_SIGNALS
63
static int mkcl_lose_on_rogue_signal = 1;
Jean-Claude Beaudoin's avatar
Typos.    
Jean-Claude Beaudoin committed
64
# else
65
static int mkcl_lose_on_rogue_signal = 0;
Jean-Claude Beaudoin's avatar
Typos.    
Jean-Claude Beaudoin committed
66
# endif
67
68
69
70
71
72
73
74
75
76

static int interrupt_sig = 0;
static int resume_sig = 0;
static int wake_up_sig = 0;
static pid_t mkcl_pid;
#endif /* __linux */

/******************************* ------- ******************************/


77
#if MKCL_UNIX
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
static char * ltoad(long val, char * str)
{
  int i = 0, j = 0;
  long quotient = (val > 0) ? val : -val;
  char buf[24];
  
  if ( quotient == 0 )
    buf[i++] = '0';
  else
    do {
      char remainder = quotient % 10;
      quotient = quotient / 10;

      buf[i++] = remainder + '0';
    } while ( quotient != 0 );
  if ( val < 0 ) 
    buf[i++] = '-';

  while ( i > 0 )
    str[j++] = buf[--i];
  str[j] = '\0';
  return(str);
}
101
#endif /* MKCL_UNIX */
102
103
104
105
106

/******************************* ------- ******************************/

/* New fully POSIX compliant signal processing. JCB */

107
#if MKCL_UNIX
108
109
110
111

static int stderr_fd;

static
112
void sig_print(const char * const msg)
113
114
115
116
117
118
119
{
  /* This call may fail and it is just too bad. We do not want to do anything about it. */
  ssize_t count = write(stderr_fd, msg, strlen(msg));
}

#define signal_name(sig) #sig 

120
# if __linux
121
static const char * const signal_names[MKCL_BASE_SIGMAX + 1] =
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  {
    "",
    signal_name(SIGHUP),
    signal_name(SIGINT),
    signal_name(SIGQUIT),
    signal_name(SIGILL),
    signal_name(SIGTRAP),
    signal_name(SIGABRT),
    signal_name(SIGBUS),
    signal_name(SIGFPE),
    signal_name(SIGKILL),
    signal_name(SIGUSR1),
    signal_name(SIGSEGV),
    signal_name(SIGUSR2),
    signal_name(SIGPIPE),
    signal_name(SIGALRM),
    signal_name(SIGTERM),
    signal_name(SIGSTKFLT),
    signal_name(SIGCHLD),
    signal_name(SIGCONT),
    signal_name(SIGSTOP),
    signal_name(SIGTSTP),
    signal_name(SIGTTIN),
    signal_name(SIGTTOU),
    signal_name(SIGURG),
    signal_name(SIGXCPU),
    signal_name(SIGXFSZ),
    signal_name(SIGVTALRM),
    signal_name(SIGPROF),
    signal_name(SIGWINCH),
    signal_name(SIGIO),
    signal_name(SIGPWR),
154
    signal_name(SIGSYS),
155
  };
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# elif __FreeBSD__
static const char * const signal_names[MKCL_BASE_SIGMAX + 1] =
  {
    "",
    signal_name(SIGHUP),
    signal_name(SIGINT),
    signal_name(SIGQUIT),
    signal_name(SIGILL),
    signal_name(SIGTRAP),
    signal_name(SIGABRT),
    signal_name(SIGEMT),
    signal_name(SIGFPE),
    signal_name(SIGKILL),
    signal_name(SIGBUS),
    signal_name(SIGSEGV),
    signal_name(SIGSYS),
    signal_name(SIGPIPE),
    signal_name(SIGALRM),
    signal_name(SIGTERM),
    signal_name(SIGURG),
    signal_name(SIGSTOP),
    signal_name(SIGTSTP),
    signal_name(SIGCONT),
    signal_name(SIGCHLD),
    signal_name(SIGTTIN),
    signal_name(SIGTTOU),
    signal_name(SIGIO),
    signal_name(SIGXCPU),
    signal_name(SIGXFSZ),
    signal_name(SIGVTALRM),
    signal_name(SIGPROF),
    signal_name(SIGWINCH),
    signal_name(SIGINFO),
    signal_name(SIGUSR1),
    signal_name(SIGUSR2),
  };
# endif
193
194
195



196
struct mkcl_signal_control mkcl_signals[MKCL_SIGMAX + 1] = { { FALSE } };
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211

volatile int mkcl_terminal_signal_number = -1;

static
void sig_perror(char * msg)
{
  int my_errno = errno;
  char errno_str[24];

  sig_print(msg);
  sig_print(" errno = ");
  sig_print(ltoad(my_errno, errno_str));
  sig_print("\n");
}

212
static void bark_about_signal(siginfo_t *info, const char * const msg)
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
{
  char buf[24];

  sig_print(msg);
  sig_print(" from pid = ");
  sig_print(ltoad(info->si_pid, buf));
  sig_print(" and uid = ");
  sig_print(ltoad(info->si_uid, buf));
  sig_print(", si_signo = ");
  sig_print(ltoad(info->si_signo, buf));
  sig_print(", si_code = ");
  sig_print(ltoad(info->si_code, buf));
  sig_print(".\n");
  return;
}

Jean-Claude Beaudoin's avatar
Jean-Claude Beaudoin committed
229
#ifdef DEBUG_SIGNALS
230
231
232
233
234
235
static void sig_print_sigmask(sigset_t * set)
{
    int i;
    char sig_num[24];

    sig_print("Blocked: ");
236
    for (i = 1; i <= MKCL_SIGMAX; i++) {
237
238
239
240
241
242
243
244
245
246
247
248
249
      if (sigismember(set, i))
	{ sig_print(ltoad(i, sig_num)); sig_print(" "); }
    }
    sig_print("\n");
}

static void show_sigmask(void)
{
  sigset_t mask;

  pthread_sigmask(SIG_SETMASK, NULL, &mask);
  sig_print_sigmask(&mask);
}
Jean-Claude Beaudoin's avatar
Jean-Claude Beaudoin committed
250
#endif /* def DEBUG_SIGNALS */
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293

static
void posix_signal(MKCL, int sig, void (*handler)(int, siginfo_t *, void *))
{
  struct sigaction new_action;

  /* As a matter of style we should identify values SIG_DFL and SIG_IGN
     for handler and give them special treatment. */
  
  new_action.sa_sigaction = handler;
  sigemptyset(&new_action.sa_mask);

  /* We do not want our interrupt signal handler to interrupt in the 
     middle of its business another signal handler. Such nesting
     of signal handlers one on top of another would be a serious
     source of headaches.  JCB */
  if (sigaddset(&new_action.sa_mask, interrupt_sig))
    mkcl_FElibc_error(env, "posix_signal failed on sigaddset.", 0);

#if 1
  new_action.sa_flags = SA_SIGINFO;
#else
  new_action.sa_flags = SA_SIGINFO | SA_ONSTACK; /* Let's try the signal alternate stack. JCB */ /* Unusable with Boehm's GC */
#endif
  
  if (sigaction(sig, &new_action, &(mkcl_signals[sig].old_action)))
    mkcl_FElibc_error(env, "posix_signal failed on sigaction.", 0);

  mkcl_signals[sig].installed = TRUE;

#ifdef DEBUG_SIGNALS
  fprintf(stderr, "\nInstalled handler for signal %d.\n", sig); fflush(stderr);
  if (mkcl_signals[sig].old_action.sa_handler != SIG_DFL)
    {
      fprintf(stderr, "\nOld signal handler for signal %d is not default: %p\n",
	      sig, mkcl_signals[sig].old_action.sa_handler);
      fflush(stderr);
    }
#endif
}

static pid_t mkcl_debugged_by_process_id = 0; /* 0 is never a valid process id. */

294
295
296
297
298
#ifndef SI_TKILL
# ifdef SI_LWP
#  define SI_TKILL SI_LWP
# endif
#endif
299
300
301
302
303
304
305
306
307
308

void mkcl_resume_signal_handler(int sig, siginfo_t * info, void * aux)
{
  mkcl_env env = mkcl_interrupted_thread_env;

#ifdef DEBUG_SIGNALS
  sig_print("\nInside mkcl_resume_signal_handler.\n");
  show_sigmask();
#endif

309
  if ( (info->si_code != SI_TKILL && info->si_code != SI_USER)
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
       || (info->si_pid != mkcl_pid && info->si_pid != mkcl_debugged_by_process_id))
    { /* This is a rogue signal! */
      bark_about_signal(info, "\nMKCL: received an invalid resume signal");
      return;
    }

  env->own_thread->thread.resume_handler_ran = TRUE;

#if 0 /* moved to interrupt_signal_handler() */
  if (sem_post(mkcl_interrupted_thread_resumed))
    sig_perror("\nmkcl_resume_signal_handler failed on sem_post.");
#endif
}

static void mkcl_fix_sigmask(int sig)
{
  sigset_t interrupt_sigmask;

#ifdef DEBUG_SIGNALS
  sig_print("\nFixing sigmask!\n");
#endif

  sigemptyset(&interrupt_sigmask);
  if (sigaddset(&interrupt_sigmask, interrupt_sig))
    sig_perror("\nmkcl_fix_sigmask failed on sigaddset.");

  if ( sig != 0 )
    if (sigaddset(&interrupt_sigmask, sig))
      sig_perror("\nmkcl_fix_sigmask failed on sigaddset.");

  /* Strickly speaking this one is not POSIX async-signal-safe
     de jure but seems to be de facto on Linux (glibc 2.5)
     and Solaris (10 and some earlier). On Solaris the purist
     could use thr_sigsetmask() instead.
     The alternative would be to plug every call to mkcl_frs_pop()
     which is a quite annoying overhead. JCB */
  pthread_sigmask(SIG_UNBLOCK, &interrupt_sigmask, NULL);
}

void mkcl_interrupt_signal_handler(int sig, siginfo_t *info, void *aux)
{
  mkcl_env env = mkcl_interrupted_thread_env;

#ifdef DEBUG_SIGNALS
  sig_print("\nInside mkcl_interrupt_signal_handler.\n");
  show_sigmask();
#endif

358
  if ( (info->si_code != SI_TKILL && info->si_code != SI_USER)
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
       || (info->si_pid != mkcl_pid && info->si_pid != mkcl_debugged_by_process_id))
    { /* This is a rogue signal! */
      bark_about_signal(info, "\nMKCL: received an invalid interrupt signal");
      return;
    }

  if ( env == NULL /* The thread died unexpectedly! */
       || (env->own_thread->thread.status == mkcl_thread_done) /* The thread is dying on us! */
       || (env->disable_interrupts > 1) /* being interrupted already! */
       || (env->disable_interrupts && !mkcl_interrupt_forcefully) )
    {
      mkcl_interrupt_refused = TRUE;
      if (sem_post(mkcl_interrupted_thread_suspended))
	sig_perror("\nmkcl_interrupt_signal_handler failed on sem_post.");

#ifdef DEBUG_SIGNALS
      sig_print("\nmkcl_interrupt_signal_handler refused interrupt.\n");
      show_sigmask();
#endif
      return;
    }
  else
    if (sem_post(mkcl_interrupted_thread_suspended))
      sig_perror("\nmkcl_interrupt_signal_handler failed on sem_post.");

  {
    sigset_t suspend_sigmask;
   
    sigfillset(&suspend_sigmask);
    if (sigdelset(&suspend_sigmask, resume_sig))
      sig_perror("\nmkcl_interrupt_signal_handler failed on sigdelset.");
    if (sigdelset(&suspend_sigmask, MK_GC_SIG_SUSPEND)) /* Because of Boehm's GC */
      sig_perror("\nmkcl_interrupt_signal_handler failed on sigdelset.");

#ifdef DEBUG_SIGNALS
    /* We want to be able to get a core dump if need be. */
    if (sigdelset(&suspend_sigmask, SIGQUIT))
      sig_perror("\nmkcl_interrupt_signal_handler failed on sigdelset.");

    sig_print("\nAbout to sigsuspend on this mask:");
    sig_print_sigmask(&suspend_sigmask);
#endif

    do {
      sigsuspend(&suspend_sigmask);
      if ( errno != EINTR )
	sig_perror("\nmkcl_interrupt_signal_handler failed on sigsuspend.");
    } while ( env->own_thread->thread.resume_handler_ran != TRUE );

    /* Now that we have seen it let's clear it right away. */
    env->own_thread->thread.resume_handler_ran = FALSE;

    if (sem_post(mkcl_interrupted_thread_resumed))
      sig_perror("\nmkcl_resume_signal_handler failed on sem_post.");
  }

  if (env->nlj_fr)
    {/* resume unwinding initiated from the interrupt function. */

      mkcl_fix_sigmask(0);

#ifdef DEBUG_SIGNALS
      sig_print("\nResuming unwinding in interrupted thread.\n");
      show_sigmask();
#endif

      mkcl_unwind(env, env->nlj_fr);
    }
#ifdef DEBUG_SIGNALS
  else
    {
      sig_print("\nResuming interrupted thread normally.\n");
      show_sigmask();
    }
#endif
}

void mkcl_generic_signal_handler(int sig, siginfo_t *info, void *aux)
{
#ifdef DEBUG_SIGNALS
  sig_print("\nInside mkcl_generic_signal_handler for signal ");
  {
    char buf[24];
    sig_print(ltoad(sig, buf));
    sig_print(".\n");
  }
  show_sigmask();
#endif

  if (sem_post(mkcl_signals[sig].sem))
    sig_perror("\nmkcl_generic_signal_handler failed on sem_post.");
}

void mkcl_terminal_signal_handler(int sig, siginfo_t *info, void *aux)
{
#ifdef DEBUG_SIGNALS
  sig_print("\nInside mkcl_terminal_signal_handler for signal ");
  {
    char buf[24];
    sig_print(ltoad(sig, buf));
    sig_print(".\n");
  }
  show_sigmask();
#endif

  mkcl_terminal_signal_number = sig;

  if (sem_post(mkcl_signals[0].sem))
    sig_perror("\nmkcl_terminal_signal_handler failed on sem_post.");
}

void mkcl_wake_up_signal_handler(int sig, siginfo_t *info, void *aux)
{
#ifdef DEBUG_SIGNALS
  sig_print("\nInside mkcl_wake_up_signal_handler for signal ");
  {
    char buf[24];
    sig_print(ltoad(sig, buf));
    sig_print(".\n");
  }
  /* show_sigmask(); */
#endif
  struct sigaction * old = &(mkcl_signals[sig].old_action);
    
  /* SIGCONT resumes the process by default. */
  if (sig == SIGCONT && old->sa_handler != SIG_IGN)
    {
#ifdef DEBUG_SIGNALS
      sig_print("\nSIGCONT handler chaining.");
#endif
      if (old->sa_flags & SA_SIGINFO )
	(old->sa_sigaction)(sig, info, aux);
      else
	(old->sa_handler)(sig);
    }
}

static void install_wake_up_signal_handler(MKCL, int sig)
{
  posix_signal(env, sig, mkcl_wake_up_signal_handler);
}

static void install_resume_signal_handler(MKCL, int sig)
{
  posix_signal(env, sig, mkcl_resume_signal_handler);
}

static void install_interrupt_signal_handler(MKCL, int sig)
{
  posix_signal(env, sig, mkcl_interrupt_signal_handler);
}

static void 
512
install_lisp_signal_handler(MKCL, int signum, mkcl_object func_designator)
513
514
{
  /* Create the signal servicing thread */
515
  char sig_thread_name[128] = { 0 };
516

517
  if (signum <= MKCL_BASE_SIGMAX)
518
    snprintf(sig_thread_name, sizeof(sig_thread_name), "%s handling daemon", signal_names[signum]);
519
  else
520
    snprintf(sig_thread_name, sizeof(sig_thread_name), "SIG%d handling daemon", signum);
521

522

523
  mkcl_create_signal_servicing_thread(env, sig_thread_name, signum, func_designator);
524
525
526
527
528
529
530
531
532
533
534
535
}

static void 
install_lisp_terminal_signal_handler(MKCL)
{
  /* Create the signal servicing thread */
  char * sig_thread_name = "Terminal signal handling daemon";

  mkcl_create_signal_servicing_thread(env, sig_thread_name, 0, @'si::terminal-signal-handler');
}

/* End of new fully POSIX compliant signal code */
536
#endif /* MKCL_UNIX */
537
538
539
540

@(defun si::setup-for-gdb (&o pid)
@
  {
541
#if MKCL_UNIX
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
    if (mkcl_Null(pid))
      { @(return MKCL_MAKE_FIXNUM(mkcl_debugged_by_process_id = getppid())); }
    else
      { @(return MKCL_MAKE_FIXNUM(mkcl_debugged_by_process_id = mkcl_safe_fixnum_to_word(env, pid))); }
#else
    @(return MKCL_MAKE_FIXNUM(0));
#endif
  }
@)



/******************************* ------- ******************************/


557
#if MKCL_UNIX    /* Posix (linux) synchronous signal handlers.*/
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588

static void
maybe_lose(char * msg)
{
#ifdef DEBUG_SIGNALS
  if ( mkcl_lose_on_rogue_signal )
    {
      fprintf(stderr, "\n%s\n", msg); fflush(stderr);
      abort();
    }
  else
#endif
    { sig_print("\n"); sig_print(msg); sig_print("\n"); }
}

void mkcl_sigfpe_handler(int sig, siginfo_t *info, void *aux)
{
#ifdef DEBUG_SIGNALS
  {
    char buf[24];
    sig_print("\nInside mkcl_sigfpe_handler, sig = ");
    sig_print(ltoad(sig, buf));
    sig_print(".\n");
  }
#endif

  const mkcl_env env = MKCL_ENV();

  if ( env == NULL )
    maybe_lose("MKCL: mkcl_sigfpe_handler called outside a lisp thread!");
  else if (!mkcl_get_option(MKCL_OPT_BOOTED)) {
589
    psiginfo(info, "In mkcl_sigfpe_handler, Received this");
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
    mkcl_lose(env, "In mkcl_sigfpe_handler. Got signal before environment was installed on our thread.");
  }
  else
    {
      mkcl_object condition = @'arithmetic-error';

      if (info) {
	char * siginfo_str;
	switch (info->si_code)
	  {
	  case FPE_INTDIV:
	    condition = @'division-by-zero';
	    siginfo_str = "FPE_INTDIV";
	    break;
	  case FPE_FLTDIV:
	    condition = @'division-by-zero';
	    siginfo_str = "FPE_FLTDIV";
	    break;
	  case FPE_FLTOVF:
	    condition = @'floating-point-overflow';
	    siginfo_str = "FPE_FLTOVF";
	    break;
	  case FPE_FLTUND:
	    condition = @'floating-point-underflow';
	    siginfo_str = "FPE_FLTUND";
	    break;
	  case FPE_FLTRES:
	    condition = @'floating-point-inexact';
	    siginfo_str = "FPE_FLTRES";
	    break;
	  case FPE_FLTINV:
	    condition = @'floating-point-invalid-operation';
	    siginfo_str = "FPE_FLTINV";
	    break;

	  case FPE_FLTSUB: /* Subscript out of range.  */
	    siginfo_str = "FPE_FLTSUB";
	    break;
	  case FPE_INTOVF: /* Integer overflow.  */
	    siginfo_str = "FPE_INTOVF";
	    /* What do we do with these? JCB */
	    break;
	  default:
	    sig_print("\nMKCL: received an unknown SIGFPE signal! Ignoring it.\n");
	    return;
	  }
      }
      MKCL_UNWIND_PROTECT_BEGIN(env) {
	mk_cl_error(env, 1, condition);
      } MKCL_UNWIND_PROTECT_EXIT {
	mkcl_reactivate_fpe_set(env);
	mkcl_fix_sigmask(sig);
      } MKCL_UNWIND_PROTECT_END;
    }

#ifdef DEBUG_SIGNALS
  sig_print("\nLeaving mkcl_sigfpe_handler.\n");
#endif
}

Jean-Claude Beaudoin's avatar
Jean-Claude Beaudoin committed
650
#if 0 /* unused? */
651
652
653
654
655
656
657
658
659
660
661
662
static void
unblock_signal(int signal)
{
  struct sigaction oact;
  sigset_t unblock_mask, current_mask;
  sigaction(signal, NULL, &oact);
  unblock_mask = oact.sa_mask;

  sigaddset(&unblock_mask, signal);
  pthread_sigmask(SIG_UNBLOCK, &unblock_mask, NULL);
  pthread_sigmask(SIG_SETMASK, NULL, &current_mask);
}
Jean-Claude Beaudoin's avatar
Jean-Claude Beaudoin committed
663
#endif
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682

void mkcl_synchronous_signal_handler(int sig, siginfo_t *info, void *aux)
{
#ifdef DEBUG_SIGNALS
  {
    char buf[24];
    sig_print("\nInside mkcl_synchronous_signal_handler, sig = ");
    sig_print(ltoad(sig, buf));
    sig_print(".\n");
  }
#endif

  const mkcl_env env = MKCL_ENV();

  if ( env == NULL )
    maybe_lose("MKCL: mkcl_synchronous_signal_handler called outside a lisp thread!");
  else
    {
      if (!mkcl_get_option(MKCL_OPT_BOOTED)) {
683
	psiginfo(info, "In mkcl_synchronous_signal_handler, Received this");
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
	mkcl_lose(env, "In mkcl_synchronous_signal_handler. "
		  "Got signal before environment was installed"
		  " on our thread.");
      }

      mkcl_fix_sigmask(sig);
      mkcl_FEerror(env, "Synchronous signal ~D caught.", 1, MKCL_MAKE_FIXNUM(sig));
    }
#ifdef DEBUG_SIGNALS
  sig_print("\nLeaving mkcl_synchronous_signal_handler.\n");
#endif
}

#define MKCL_SIGSEGV_MAXIMUM_NESTING_DEPTH 5

void mkcl_sigsegv_handler(int sig, siginfo_t *info, void *aux)
{
  ucontext_t * ctx = aux;
#if defined(DEBUG_SIGNALS)
  {
    char buf[24];
    sig_print("\nInside mkcl_sigsegv_handler, sig = ");
    sig_print(ltoad(sig, buf));
    sig_print(".\n");
  }
#endif

  const mkcl_env env = MKCL_ENV();

  if ( env == NULL )
    {
      struct sigaction * old = &(mkcl_signals[sig].old_action);
      maybe_lose("MKCL: mkcl_sigsegv_handler called outside a lisp thread!");

      if (old->sa_handler != SIG_IGN && old->sa_handler != SIG_DFL)
719
720
721
722
723
724
	{
	  if (old->sa_flags & SA_SIGINFO )
	    (old->sa_sigaction)(sig, info, aux);
	  else
	    (old->sa_handler)(sig);
	}
725
726
      
    }
727
728
729
730
731
732
733
  else if (env->disable_interrupts >= 2)
    { /* The risk of reentering a locked region in the GC is just too high. */
      maybe_lose("MKCL: mkcl_sigsegv_handler called inside uninterruptable foreign code. Cannot re-enter lisp!");

      (void) sigaction(sig, &(mkcl_signals[sig].old_action), NULL);
      (void) kill(getpid(), sig); /* resending since we do not know what to do else! */
    }
734
735
736
  else
    {
      if (!mkcl_get_option(MKCL_OPT_BOOTED)) {
737
	psiginfo(info, "In mkcl_sigsegv_handler, Received this");
738
739
740
741
742
743
744
745
746
747
748
	mkcl_lose(env, "In mkcl_sigsegv_handler. "
		  "Got signal before environment was installed"
		  " on our thread.");
      }

      {
#if 0  /* This code assumes the use of sigaltstack() and sigaltstack() is incompatible with Boehm's GC */
	uintptr_t fault_address = (uintptr_t) info->si_addr;
#ifdef __x86_64
	mkcl_index fault_BP = ctx->uc_mcontext.gregs[REG_RBP]; /* REG_RSP == 10 on x86_64.*/
	mkcl_index fault_SP = ctx->uc_mcontext.gregs[REG_RSP]; /* REG_RSP == 15 on x86_64.*/
749
#elif __i386
750
751
	mkcl_index fault_BP = ctx->uc_mcontext.gregs[REG_EBP]; /* REG_EBP == 6 on x86.*/
	mkcl_index fault_SP = ctx->uc_mcontext.gregs[REG_ESP]; /* REG_ESP == 7 on x86.*/
752
753
#else
# error  Unknown processor architecture
754
755
756
757
758
759
760
761
762
763
764
765
#endif
	/* The following test assumes a stack that grows downward. */
	if ((fault_BP >= fault_address) && (fault_address >= (fault_SP - mkcl_core.pagesize)))
	  {
	    mkcl_fix_sigmask(sig);
	    mk_cl_error(env, 5, @'mkcl::stack-overflow',
			@':size', mkcl_make_unsigned_integer(env, env->cs_size),
			@':type', @'si::call-stack');
	  }
	else
#endif
	  {
766
	    char address_cstr[24] = { 0 };
767
768
769
770
	    mkcl_object address_str;

	    switch (info->si_code)
	      {
771
772
	      case SI_USER: /* Software generated (by kill or raise). */
		if (info->si_pid != mkcl_debugged_by_process_id) goto error; else goto normal;
773
774
	      case SEGV_MAPERR: /* Address not mapped to object.  */
	      case SEGV_ACCERR: /* Invalid permissions for mapped object.  */
775
              normal:
776
		snprintf(address_cstr, sizeof(address_cstr), "%p", info->si_addr);
777
778
779
		break;
	      error:
	      default:
780
#ifdef DEBUG_SIGNALS
781
782
783
784
785
786
787
788
		sig_print("\nMKCL: received an invalid SIGSEGV signal from pid = ");
		sig_print(ltoad(info->si_pid, address_cstr));
		sig_print(" and uid = ");
		sig_print(ltoad(info->si_uid, address_cstr));
		sig_print(", si_code = ");
		sig_print(ltoad(info->si_code, address_cstr));
		if (info->si_code == SI_KERNEL) sig_print(" (SI_KERNEL)");
		sig_print(".\n");
789
#endif
790
		snprintf(address_cstr, sizeof(address_cstr), "invalid address");
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
		break;
	      }
	    address_str =  mkcl_make_base_string_copy(env, address_cstr);
	    mkcl_fix_sigmask(sig);

	    mk_cl_error(env, 3, @'mkcl::segmentation-violation', @':address', address_str);
	  }
      }
    }

#ifdef DEBUG_SIGNALS
  sig_print("\nLeaving mkcl_sigsegv_handler.\n");
#endif
}


void mkcl_sigbus_handler(int sig, siginfo_t *info, void *aux)
{
#ifdef DEBUG_SIGNALS
  {
    char buf[24];
    sig_print("\nInside mkcl_sigbus_handler, sig = ");
    sig_print(ltoad(sig, buf));
    sig_print(".\n");
  }
#endif

  const mkcl_env env = MKCL_ENV();

  if ( env == NULL )
    maybe_lose("MKCL: mkcl_sigbus_handler called outside a lisp thread!");
  else
    {
      if (!mkcl_get_option(MKCL_OPT_BOOTED)) {
825
	psiginfo(info, "In mkcl_sigbus_handler, Received this");
826
827
828
829
830
831
	mkcl_lose(env, "In mkcl_sigbus_handler. "
		  "Got signal before environment was installed"
		  " on our thread.");
      }

      {
832
	char address_cstr[24] = { 0 };
833
834
835
836
837
838
839
	mkcl_object address_str;
	    
	switch (info->si_code)
	  {
	  case BUS_ADRALN: /* Invalid address alignment.  */
	  case BUS_ADRERR: /* Non-existant physical address.  */
	  case BUS_OBJERR: /* Object specific hardware error.  */
840
	    snprintf(address_cstr, sizeof(address_cstr), "%p", info->si_addr);
841
842
	    break;
	  default:
843
#ifdef DEBUG_SIGNALS
844
845
846
847
848
849
850
	    sig_print("\nMKCL: received an invalid SIGBUS signal from pid = ");
	    sig_print(ltoad(info->si_pid, address_cstr));
	    sig_print(" and uid = ");
	    sig_print(ltoad(info->si_uid, address_cstr));
	    sig_print(", si_code = ");
	    sig_print(ltoad(info->si_code, address_cstr));
	    sig_print(".\n");
851
#endif
852
	    snprintf(address_cstr, sizeof(address_cstr), "invalid address");
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
	    break;
	  }
	address_str = mkcl_make_base_string_copy(env, address_cstr);
	mkcl_fix_sigmask(sig);
	mk_cl_error(env, 3, @'mkcl::segmentation-violation', @':address', address_str);
      }
    }

#ifdef DEBUG_SIGNALS
  sig_print("\nLeaving mkcl_sigbus_handler.\n");
#endif
}

void mkcl_sigchld_handler(int sig, siginfo_t *info, void *aux)
{
#ifdef DEBUG_SIGNALS
  {
    char buf[24];
    sig_print("\nInside mkcl_sigchld_handler, sig = ");
    sig_print(ltoad(sig, buf));
    sig_print(".\n");
  }
#endif

  pid_t dead_child_pid = info->si_pid;
  mkcl_object detached_children = mkcl_core.detached_children;
  long i = 0;

  mkcl_loop_for_on_unsafe(detached_children) {
    mkcl_object child_pid = MKCL_CONS_CAR(detached_children);
    if (MKCL_FIXNUMP(child_pid) && (mkcl_fixnum_to_word(child_pid) == dead_child_pid)) /* Was it ours? */
      {
	int status;

	while ((waitpid(dead_child_pid, &status, 0) < 0) && (errno == EINTR)); /* To prevent a zombie. */

	/* This splicing works because of the sentinel put at the end of the detached_children list. */
	MKCL_RPLACA(detached_children, MKCL_CONS_CAR(MKCL_CONS_CDR(detached_children)));
	MKCL_RPLACD(detached_children, MKCL_CONS_CDR(MKCL_CONS_CDR(detached_children)));

#ifdef DEBUG_SIGNALS
	{
	  char buf[24];
	  sig_print("\n");
	  sig_print(ltoad(i, buf));
	  sig_print(" nth, It was a detached one of ours! Leaving mkcl_sigchld_handler.\n");
	}
#endif
	return;
      }
    else if (MKCL_BIGNUMP(child_pid) && mpz_cmp_si(child_pid->big.big_num, dead_child_pid)) /* Was it ours? */
      {
	int status;

	while ((waitpid(dead_child_pid, &status, 0) < 0) && (errno == EINTR)); /* To prevent a zombie. */

	/* This splicing works because of the sentinel put at the end of the detached_children list. */
	MKCL_RPLACA(detached_children, MKCL_CONS_CAR(MKCL_CONS_CDR(detached_children)));
	MKCL_RPLACD(detached_children, MKCL_CONS_CDR(MKCL_CONS_CDR(detached_children)));

#ifdef DEBUG_SIGNALS
	sig_print("\nIt was a detached one (big) of ours! Leaving mkcl_sigchld_handler.\n");
#endif
	return;
      }
    i++;
  } mkcl_end_loop_for_on;

  mkcl_object children = mkcl_core.children;
  i = 0;
  mkcl_loop_for_on_unsafe(children) {
    mkcl_object child_pid = MKCL_CONS_CAR(children);
    if (MKCL_FIXNUMP(child_pid) && (mkcl_fixnum_to_word(child_pid) == dead_child_pid)) /* Was it ours? */
      {
#ifdef DEBUG_SIGNALS
	char buf[24];
	sig_print("\n");
	sig_print(ltoad(i, buf));
	sig_print(" nth, It was one of ours! Leaving mkcl_sigchld_handler.\n");
#endif
	return;
      }
    else if (MKCL_BIGNUMP(child_pid) && mpz_cmp_si(child_pid->big.big_num, dead_child_pid)) /* Was it ours? */
      {
#ifdef DEBUG_SIGNALS
	sig_print("\nIt was one (big) of ours! Leaving mkcl_sigchld_handler.\n");
#endif
	return;
      }
    /* else nice screw-up! */
    i++;
  } mkcl_end_loop_for_on;
  
  /* pass it on to the previous signal handler */
  {
    struct sigaction * old = &(mkcl_signals[sig].old_action);
    
    if (old->sa_handler == SIG_IGN)
      {
	int status;

	while (waitpid(dead_child_pid, &status, 0) < 0) /* To prevent a potential zombie. */
	  if (errno != EINTR) {
	    break;
	  }
#ifdef DEBUG_SIGNALS
	sig_print("\nIgnoring SIGCHLD signal.");
#endif
      }
    else if (old->sa_handler != SIG_DFL) /* SIGCHLD is ignored (with zombies preserved) by default. */
      {
#ifdef DEBUG_SIGNALS
	sig_print("\nSIGCHLD handler chaining.");
#endif
	if (old->sa_flags & SA_SIGINFO )
	  (old->sa_sigaction)(sig, info, aux);
	else
	  (old->sa_handler)(sig);
      }
  }

#ifdef DEBUG_SIGNALS
  sig_print("\nLeaving mkcl_sigchld_handler.\n");
#endif
}

void mkcl_sigpipe_handler(int sig, siginfo_t *info, void *aux)
{
  struct sigaction * old = &(mkcl_signals[sig].old_action);
    
  /* SIGPIPE is terminal by default and we cannot have that. */
  if (old->sa_handler != SIG_DFL && old->sa_handler != SIG_IGN)
    {
#ifdef DEBUG_SIGNALS
      sig_print("\nSIGPIPE handler chaining.");
#endif
      if (old->sa_flags & SA_SIGINFO )
	(old->sa_sigaction)(sig, info, aux);
      else
	(old->sa_handler)(sig);
    }
}

#endif /* __linux */

998
#if MKCL_WINDOWS
999

1000
1001
static const char * const signal_names[MKCL_BASE_SIGMAX + 1] = { NULL };

1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
static volatile BOOL console_ctrl_event = FALSE;

BOOL mkcl_saw_console_ctrl_event(void)
{
  BOOL saw_it = console_ctrl_event;
  
  console_ctrl_event = FALSE;
  return saw_it;
}


static const mkcl_base_string_object(console_ctrl_event_handler_name_obj, "Console control event handler");
static const mkcl_object console_ctrl_event_handler_name = (mkcl_object) &console_ctrl_event_handler_name_obj;

static void handle_console_ctrl_event(mkcl_object lisp_handler, int signo)
{
  char stack_mark = 0;
  const mkcl_object name = console_ctrl_event_handler_name;
  const mkcl_env env = mkcl_import_current_thread(name, mk_cl_Cnil, NULL, NULL);

  if ( env == NULL )
    {
      printf("\nMKCL: Unable to create console control event thread!\n");
      fflush(NULL);
    }
  else
    {
      mkcl_object thread = env->own_thread;

      /* mk_mt_thread_detach(env, thread); */ /* probably much too strong. */

      MKCL_CATCH_ALL_BEGIN(env) {
	MKCL_SETUP_CALL_STACK_ROOT_GUARD(env);

1036
	mkcl_setup_thread_lisp_context(env, &stack_mark);
1037
1038
1039
1040
1041
1042

	mkcl_register_thread_as_active(env, thread);

	mkcl_enable_interrupts(env);
	mkcl_funcall1(env, lisp_handler, MKCL_MAKE_FIXNUM(signo));
	mkcl_disable_interrupts(env);
1043
	mkcl_cleanup_thread_lisp_context(env);
1044
        MKCL_UNSET_CALL_STACK_ROOT_GUARD(env);
1045
      } MKCL_CATCH_ALL_IF_CAUGHT {
1046
        MKCL_UNSET_CALL_STACK_ROOT_GUARD(env);
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
      } MKCL_CATCH_ALL_END;
      thread->thread.status = mkcl_thread_done;

      mkcl_release_current_thread(env);
    }
}

static LONG handle_fpe(EXCEPTION_POINTERS* ep)
{
  const mkcl_env env = MKCL_ENV();
  mkcl_object condition = @'arithmetic-error';
  char * cond_str;

  switch (ep->ExceptionRecord->ExceptionCode)
    {
      /* Catch all arithmetic exceptions */
    case EXCEPTION_FLT_DIVIDE_BY_ZERO:
      condition = @'division-by-zero';
      cond_str = "EXCEPTION_FLT_DIVIDE_BY_ZERO";
      break;
    case EXCEPTION_FLT_OVERFLOW:
      condition = @'floating-point-overflow';
      cond_str = "EXCEPTION_FLT_OVERFLOW";
      break;
    case EXCEPTION_FLT_UNDERFLOW:
      condition = @'floating-point-underflow';
      cond_str = "EXCEPTION_FLT_UNDERFLOW";
      break;
    case EXCEPTION_FLT_INVALID_OPERATION:
      condition = @'floating-point-invalid-operation';
      cond_str = "EXCEPTION_FLT_INVALID_OPERATION";
      break;
    case EXCEPTION_FLT_INEXACT_RESULT:
      condition = @'floating-point-inexact';
      cond_str = "EXCEPTION_FLT_INEXACT_RESULT";
      break;
    case EXCEPTION_FLT_DENORMAL_OPERAND:
      cond_str = "EXCEPTION_FLT_DENORMAL_OPERAND";
      break;
    case EXCEPTION_INT_DIVIDE_BY_ZERO:
      cond_str = "EXCEPTION_INT_DIVIDE_BY_ZERO";
      break;
    case EXCEPTION_INT_OVERFLOW:
      cond_str = "EXCEPTION_INT_OVERFLOW";
      break;
    case EXCEPTION_FLT_STACK_CHECK:
       /* Not really handled! JCB */
      cond_str = "EXCEPTION_FLT_STACK_CHECK";
      break;
    }

  unsigned int old_fp_status = _clearfp();

  mk_cl_error(env, 1, condition); 

  return EXCEPTION_CONTINUE_SEARCH;
}

static LONG handle_access_violation(EXCEPTION_POINTERS* ep)
{
  const mkcl_env env = MKCL_ENV();
1108
  char address_cstr[24] = { 0 };
1109
1110
  mkcl_object address_str;

1111
  snprintf(address_cstr, sizeof(address_cstr), "%p", (void *) ep->ExceptionRecord->ExceptionInformation[1]);
1112
1113
1114
1115
1116
1117
1118
  address_str =  mkcl_make_base_string_copy(env, address_cstr);

  mk_cl_error(env, 3, @'mkcl::segmentation-violation', @':address', address_str);

  return EXCEPTION_CONTINUE_SEARCH;
}

1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
static LONG handle_stack_overflow(EXCEPTION_POINTERS* ep)
{
  const mkcl_env env = MKCL_ENV();
  int ok = _resetstkoflw();

#if 0
  printf("\nMKCL: Received EXCEPTION_STACK_OVERFLOW!\n");
  printf("env->disable_interrupts = %d\n", (env)->disable_interrupts);
  if (env->disable_interrupts)
    {
      printf("env->interrupt_disabler_file = %s\n", (env)->interrupt_disabler_file);
      printf("env->interrupt_disabler_lineno = %llu\n", (env)->interrupt_disabler_lineno);
    }
  printf("ExceptionRecord: %p\n", ep->ExceptionRecord->ExceptionRecord);
  printf("ExceptionAddress: %p\n", ep->ExceptionRecord->ExceptionAddress);
  printf("NumberParameters: %lu\n", ep->ExceptionRecord->NumberParameters);
  printf("ExceptionInformation[0]: %p\n", ep->ExceptionRecord->ExceptionInformation[0]);
  printf("ExceptionInformation[1]: %p\n", ep->ExceptionRecord->ExceptionInformation[1]);
  printf("thread name: %s\n", env->own_thread->thread.name->base_string.self);
  printf("tid = %d, Stack size = %lu, stack base = %p, stack top = %p.\n",
	 (env->own_thread ? env->own_thread->thread.tid : 0),
	 env->cs_size, env->cs_org, env->cs_org + env->cs_size);
  fflush(NULL);
#endif

  if (ok)
    {
#if 0
      printf("_resetstkoflw() said OK! Try to call the debugger...");
      fflush(NULL);
#endif
      env->cs_overflowing = TRUE;
      mk_cl_error(env, 5, @'mkcl::stack-overflow',
		  @':size', mkcl_make_unsigned_integer(env, env->cs_size),
		  @':type', @'si::call-stack');
    }
  else
    {
#if 0
      printf("_resetstkoflw() failed! All that is left to do is to sleep until we die...");
      fflush(NULL);
      Sleep(1000000);
#endif
    }
  return EXCEPTION_CONTINUE_SEARCH;
}


1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
static LONG unhandled_exception(EXCEPTION_POINTERS* ep)
{
  printf("\nMKCL: Received an exception we cannot handle!\n");
  fflush(NULL);
  return EXCEPTION_CONTINUE_SEARCH;
}

static LONG handle_illegal_instruction(EXCEPTION_POINTERS* ep)
{
  printf("\nhandle_illegal_instruction is not implemented yet!\n");
  fflush(NULL);
  return EXCEPTION_CONTINUE_SEARCH;
}

static LONG WINAPI W32_exception_filter(EXCEPTION_POINTERS* ep)
{
  LONG excpt_result;

  excpt_result = EXCEPTION_CONTINUE_EXECUTION;
  switch (ep->ExceptionRecord->ExceptionCode)
    {
      /* Catch all arithmetic exceptions */
    case EXCEPTION_INT_DIVIDE_BY_ZERO:
    case EXCEPTION_INT_OVERFLOW:
    case EXCEPTION_FLT_DIVIDE_BY_ZERO:
    case EXCEPTION_FLT_OVERFLOW:
    case EXCEPTION_FLT_UNDERFLOW:
    case EXCEPTION_FLT_INEXACT_RESULT:
    case EXCEPTION_FLT_DENORMAL_OPERAND:
    case EXCEPTION_FLT_INVALID_OPERATION:
    case EXCEPTION_FLT_STACK_CHECK:
      excpt_result = handle_fpe(ep);
      break;
      /* Catch segmentation fault */
    case EXCEPTION_ACCESS_VIOLATION:
      excpt_result = handle_access_violation(ep);
      break;
      /* Catch illegal instruction */
    case EXCEPTION_ILLEGAL_INSTRUCTION:
      excpt_result = handle_illegal_instruction(ep);
      break;
    case EXCEPTION_PRIV_INSTRUCTION:
1209
1210
1211
1212
#if 0
      printf("\nMKCL: Received EXCEPTION_PRIV_INSTRUCTION!\n");
      fflush(NULL);
#endif
1213
1214
1215
1216
1217
      excpt_result = unhandled_exception(ep);
      break;

      /* These are equivalent to a SIGBUS */
    case EXCEPTION_DATATYPE_MISALIGNMENT:
1218
1219
1220
1221
#if 0
      printf("\nMKCL: Received EXCEPTION_DATATYPE_MISALIGNMENT!\n");
      fflush(NULL);
#endif
1222
1223
1224
      excpt_result = unhandled_exception(ep);
      break;
    case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
1225
1226
1227
1228
#if 0
      printf("\nMKCL: Received EXCEPTION_ARRAY_BOUNDS_EXCEEDED!\n");
      fflush(NULL);
#endif
1229
1230
1231
      excpt_result = unhandled_exception(ep);
      break;
    case EXCEPTION_IN_PAGE_ERROR:
1232
1233
1234
1235
#if 0
      printf("\nMKCL: Received EXCEPTION_IN_PAGE_ERROR!\n");
      fflush(NULL);
#endif
1236
1237
1238
      excpt_result = unhandled_exception(ep);
      break;
    case EXCEPTION_STACK_OVERFLOW:
1239
      excpt_result = handle_stack_overflow(ep);
1240
1241
1242
1243
      break;

      /* Do not catch anything else */
    default:
1244
1245
      printf("\nMKCL: Received an unknown exception, (ExceptionCode = %lu)!\n", ep->ExceptionRecord->ExceptionCode);
      fflush(NULL);
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
      excpt_result = EXCEPTION_CONTINUE_SEARCH;
      break;
    }

  return excpt_result;
}

#ifdef __MINGW64__
typedef EXCEPTION_DISPOSITION
(* W64UnhandledExceptionFilter)(struct _EXCEPTION_RECORD *, void *,
				struct _CONTEXT *, void *);

static int SetMingW64UnhandledExceptionFilter(W64UnhandledExceptionFilter filter);
/* void register_UEF_WrapperCallbacks(void); */

static EXCEPTION_DISPOSITION
W64_exception_filter (struct _EXCEPTION_RECORD* ExceptionRecord,
		      void *EstablisherFrame  __attribute__ ((unused)),
		      struct _CONTEXT* ContextRecord,
		      void *DispatcherContext __attribute__ ((unused)))
{
  EXCEPTION_POINTERS ep;
  LONG excpt_result;

  ep.ExceptionRecord = ExceptionRecord;
  ep.ContextRecord = ContextRecord;

  excpt_result = W32_exception_filter(&ep);

  if (excpt_result == EXCEPTION_CONTINUE_EXECUTION)
    return ExceptionContinueExecution;
  else
    return ExceptionContinueSearch;
}
#endif /* __MINGW64__ */


static BOOL WINAPI W32_console_ctrl_handler(DWORD type)
{
  console_ctrl_event = TRUE;
  switch (type)
    {
      /* Catch CTRL-C */
    case CTRL_C_EVENT:
      handle_console_ctrl_event(@'si::sigint-handler', SIGINT);
      return TRUE;
1292
    case CTRL_BREAK_EVENT: /* equivalent to SIGTERM or SIGHUP? */
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
      handle_console_ctrl_event(@'si::sigint-handler', SIGBREAK);
      return TRUE;
    case CTRL_CLOSE_EVENT:
      handle_console_ctrl_event(@'si::sighup-handler', SIGTERM);
      return TRUE;
    case CTRL_LOGOFF_EVENT:
      handle_console_ctrl_event(@'si::sighup-handler', SIGTERM);
      return TRUE;
    case CTRL_SHUTDOWN_EVENT:
      handle_console_ctrl_event(@'si::terminal-signal-handler', SIGTERM);
      return TRUE;
    }
  return FALSE;
}

#endif  /* MKCL_WINDOWS */

1310
#if MKCL_WINDOWS
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
int mkcl_feenableexcept(int excepts)
{
  unsigned int cw = _controlfp(0,0);

  cw &= ~(excepts);

  int new_cw = _controlfp(cw,_MCW_EM);

  return _MCW_EM & ~new_cw;
}

int mkcl_fedisableexcept(int excepts)
{
  int cw = _controlfp(0,0);

  cw |= (excepts);

  int new_cw = _controlfp(cw,_MCW_EM);

  return _MCW_EM & ~new_cw;
}

int mkcl_feholdexcept(int * fenv)
{
  int cw = _controlfp(0,0);

  *fenv = cw;

  int new_cw = _controlfp(0xffffffff, _MCW_EM); 

  return _MCW_EM & ~new_cw;
}

int mkcl_fegetexcept(void)
{
  int cw = _controlfp(0,0);

  int excepts = _MCW_EM & ~ cw;

  return excepts;
}

int mkcl_fesetenv(int * fenv)
{
  int new_cw = _controlfp(*fenv, _MCW_EM);

  return 0;
}

int mkcl_fetestexcept(int excepts)
{
  int sw = _statusfp();

  excepts &= sw;

  return excepts;
}

int mkcl_feclearexcept(int excepts)
{
  _clearfp();
  return 0;
}
#endif /* MKCL_WINDOWS */


mkcl_object
mk_si_initial_floating_point_exception_set(MKCL)
{
  mkcl_object fpe_set = mk_cl_Cnil;

  mkcl_call_stack_check(env);
  fpe_set = MKCL_CONS(env, @'division-by-zero', fpe_set);
  fpe_set = MKCL_CONS(env, @'floating-point-overflow', fpe_set);
  fpe_set = MKCL_CONS(env, @'floating-point-underflow', fpe_set);
  fpe_set = MKCL_CONS(env, @'floating-point-invalid-operation', fpe_set);
#if 0
  fpe_set = MKCL_CONS(env, @'floating-point-inexact', fpe_set);
#endif
  
  @(return fpe_set);
}

static int default_fpe_mask(MKCL)
{
  mkcl_object default_set = MKCL_SYM_VAL(env, @'si::*default-floating-point-exception-set*');
  mkcl_object exception;
  int mask = 0;

  mkcl_loop_for_in(env, default_set) {
    exception = MKCL_CONS_CAR(default_set);

    if (exception == @'division-by-zero')
      mask |= FE_DIVBYZERO;
    else if (exception == @'floating-point-overflow')
      mask |= FE_OVERFLOW;
    else if (exception == @'floating-point-underflow')
      mask |= FE_UNDERFLOW;
    else if (exception == @'floating-point-invalid-operation')
      mask |= FE_INVALID;
    else if (exception == @'floating-point-inexact')
      mask |= FE_INEXACT;
    else
      mkcl_FEerror(env, "Unknown floating-point exception: ~S.", 1, exception);
  } mkcl_end_loop_for_in;
    
  return mask;
}


mkcl_object
mk_si_disable_fpe(MKCL, mkcl_object exception)
{
  int bits = 0;

  mkcl_call_stack_check(env);
  if (exception == @'division-by-zero')
    bits = FE_DIVBYZERO;
  else if (exception == @'floating-point-overflow')
    bits = FE_OVERFLOW;
  else if (exception == @'floating-point-underflow')
    bits = FE_UNDERFLOW;
  else if (exception == @'floating-point-invalid-operation')
    bits = FE_INVALID;
  else if (exception == @'floating-point-inexact')
    bits = FE_INEXACT;
  else if (exception == @':default')
    bits = default_fpe_mask(env);
  else if (exception == mk_cl_Ct)
    bits = FE_ALL_EXCEPT;
  else if (mkcl_Null(exception))
    bits = 0;
  else
    mkcl_FEerror(env, "Unknown floating-point exception: ~S.", 1, exception);

  fedisableexcept(bits);

  env->fpe_control_bits &= ~bits;

  @(return mk_cl_Cnil);
}

mkcl_object
mk_si_enable_fpe(MKCL, mkcl_object exception)
{
  int bits = 0;

  mkcl_call_stack_check(env);
  if (exception == @'division-by-zero')
    bits = FE_DIVBYZERO;
  else if (exception == @'floating-point-overflow')
    bits = FE_OVERFLOW;
  else if (exception == @'floating-point-underflow')
    bits = FE_UNDERFLOW;
  else if (exception == @'floating-point-invalid-operation')
    bits = FE_INVALID;
  else if (exception == @'floating-point-inexact')
    bits = FE_INEXACT;
  else if (exception == @':default')
    bits = default_fpe_mask(env);
  else if (exception == mk_cl_Ct)
    bits = FE_ALL_EXCEPT;
  else if (mkcl_Null(exception))
    bits = 0;
  else
    mkcl_FEerror(env, "Unknown floating-point exception: ~S.", 1, exception);

  feclearexcept(bits); /* We clear them because we have no idea where they came from. */
  feenableexcept(bits);
  
  env->fpe_control_bits |= bits;

  @(return mk_cl_Cnil);
}

void mkcl_reactivate_fpe_set(MKCL)
{
  feenableexcept(env->fpe_control_bits);
}

mkcl_object
mk_si_all_enabled_fpe(MKCL)
{
  mkcl_call_stack_check(env);
  mkcl_object fpe_set = mk_cl_Cnil;
  int enabled_except = fegetexcept();

  if (enabled_except & FE_DIVBYZERO)
    fpe_set = MKCL_CONS(env, @'division-by-zero', fpe_set);
  if (enabled_except & FE_OVERFLOW)
    fpe_set = MKCL_CONS(env, @'floating-point-overflow', fpe_set);
  if (enabled_except & FE_UNDERFLOW)
    fpe_set = MKCL_CONS(env, @'floating-point-underflow', fpe_set);
  if (enabled_except & FE_INVALID)
    fpe_set = MKCL_CONS(env, @'floating-point-invalid-operation', fpe_set);
  if (enabled_except & FE_INEXACT)
    fpe_set = MKCL_CONS(env, @'floating-point-inexact', fpe_set);

  @(return fpe_set);
}

mkcl_object
mk_si_fpe_enabled_p(MKCL, mkcl_object exception)
{
  mkcl_call_stack_check(env);
  int bits = 0;
  int enabled_except = fegetexcept();

  if (exception == @'division-by-zero')
    bits = FE_DIVBYZERO;
  else if (exception == @'floating-point-overflow')
    bits = FE_OVERFLOW;
  else if (exception == @'floating-point-underflow')
    bits = FE_UNDERFLOW;
  else if (exception == @'floating-point-invalid-operation')
    bits = FE_INVALID;
  else if (exception == @'floating-point-inexact')
    bits = FE_INEXACT;
  else
    mkcl_FEerror(env, "Unknown floating-point exception: ~S.", 1, exception);

  @(return ((enabled_except & bits) ? mk_cl_Ct : mk_cl_Cnil));
}

mkcl_object
mk_si_all_raised_fpe(MKCL)
{
  mkcl_call_stack_check(env);
  int raised_except = fetestexcept(FE_ALL_EXCEPT);
  mkcl_object fpe_set = mk_cl_Cnil;

  if (raised_except & FE_DIVBYZERO)
    fpe_set = MKCL_CONS(env, @'division-by-zero', fpe_set);
  if (raised_except & FE_OVERFLOW)
    fpe_set = MKCL_CONS(env, @'floating-point-overflow', fpe_set);
  if (raised_except & FE_UNDERFLOW)
    fpe_set = MKCL_CONS(env, @'floating-point-underflow', fpe_set);
  if (raised_except & FE_INVALID)
    fpe_set = MKCL_CONS(env, @'floating-point-invalid-operation', fpe_set);
  if (raised_except & FE_INEXACT)
    fpe_set = MKCL_CONS(env, @'floating-point-inexact', fpe_set);

  @(return fpe_set);
}

mkcl_object
mk_si_fpe_raised_p(MKCL, mkcl_object exception)
{
  mkcl_call_stack_check(env);
  int raised_except = fetestexcept(FE_ALL_EXCEPT);
  int bits = 0;

  if (exception == @'division-by-zero')
    bits = FE_DIVBYZERO;
  else if (exception == @'floating-point-overflow')
    bits = FE_OVERFLOW;
  else if (exception == @'floating-point-underflow')
    bits = FE_UNDERFLOW;
  else if (exception == @'floating-point-invalid-operation')
    bits = FE_INVALID;
  else if (exception == @'floating-point-inexact')
    bits = FE_INEXACT;
  else
    mkcl_FEerror(env, "Unknown floating-point exception: ~S.", 1, exception);

  @(return ((raised_except & bits) ? mk_cl_Ct : mk_cl_Cnil));
}

mkcl_object
mk_si_raise_fpe(MKCL, mkcl_object exception)
{
  int bits = 0;

  mkcl_call_stack_check(env);
  if (exception == @'division-by-zero')
    bits = FE_DIVBYZERO;
  else if (exception == @'floating-point-overflow')
    bits = FE_OVERFLOW;
  else if (exception == @'floating-point-underflow')
    bits = FE_UNDERFLOW;
  else if (exception == @'floating-point-invalid-operation')
    bits = FE_INVALID;
  else if (exception == @'floating-point-inexact')
    bits = FE_INEXACT;
  else
    mkcl_FEerror(env, "Unknown floating-point exception: ~S.", 1, exception);

  feraiseexcept(bits);

  @(return mk_cl_Cnil);
}

void mkcl_clear_fpe(MKCL, int except)
{
  feclearexcept(FE_ALL_EXCEPT & except);
}

mkcl_object
mk_si_clear_fpe(MKCL, mkcl_object exception)
{
  int bits = 0;

  mkcl_call_stack_check(env);
  if (exception == @'division-by-zero')
    bits = FE_DIVBYZERO;
  else if (exception == @'floating-point-overflow')
    bits = FE_OVERFLOW;
  else if (exception == @'floating-point-underflow')
    bits = FE_UNDERFLOW;
  else if (exception == @'floating-point-invalid-operation')
    bits = FE_INVALID;
  else if (exception == @'floating-point-inexact')
    bits = FE_INEXACT;
  else if (exception == @':default')
    bits = default_fpe_mask(env);
  else if (exception == mk_cl_Ct)
    bits = FE_ALL_EXCEPT;
  else if (mkcl_Null(exception))
    bits = 0;
  else
    mkcl_FEerror(env, "Unknown floating-point exception: ~S.", 1, exception);

  feclearexcept(bits);

  @(return mk_cl_Cnil);
}

mkcl_object
mk_si_clear_all_fpe(MKCL)
{
  mkcl_call_stack_check(env);
  feclearexcept(FE_ALL_EXCEPT);
  @(return mk_cl_Cnil);
}

1646
#if MKCL_WINDOWS
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
static VOID CALLBACK dummy_apc_func(ULONG_PTR dwParam)
{
  /* This function is deliberately empty. */
}
#endif

mkcl_object
mk_mt_try_to_wake_up_thread(MKCL, mkcl_object thread)
{
  int rc, i;
  mkcl_object sleeping_on;

  mkcl_call_stack_check(env);
  if (mkcl_type_of(thread) != mkcl_t_thread)
    mkcl_FEwrong_type_argument(env, @'mt::thread', thread);

  if (thread->thread.status != mkcl_thread_active)
    { @(return mk_cl_Cnil); } /* There is no point in trying to wake up something that cannot be made to run. */

  sleeping_on = thread->thread.env->sleeping_on;
  if (mkcl_Null(sleeping_on))
    {
#if 0
      fprintf(stderr, "\n;; MKCL: Tried to wake up [%s] on NIL!\n", thread->thread.name->base_string.self);
      fflush(stderr);
#endif
      @(return mk_cl_Cnil);
    }
  else if (sleeping_on == @':io')
    {
#if 0
      fprintf(stderr, "\n;; MKCL: Tried to wake up [%s] on I/O!\n", thread->thread.name->base_string.self);
      fflush(stderr);
#endif
1681
#if MKCL_WINDOWS
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
      if (thread->thread.thread)
	{
	  DWORD ok;
	  
	  MKCL_LIBC_NO_INTR(env, ok = QueueUserAPC(dummy_apc_func, thread->thread.thread, (ULONG_PTR) NULL));
	  if (!ok)
	    {
	      if (ERROR_INVALID_HANDLE == GetLastError())
		thread->thread.status = mkcl_thread_done; /* Let's prononce it dead. */
	      
	      @(return mk_cl_Cnil);
	    }
	}
      else
	{ @(return mk_cl_Cnil); }
#else
      for (i = 0; i < 3; i++) /* We knock 3 times because our wake-up call may be received before the sleep. */
	{
	  MKCL_LIBC_NO_INTR(env, (rc = pthread_kill(thread->thread.thread, wake_up_sig)));
	  if (rc)
	    {
	      if (rc == ESRCH)
		thread->thread.status = mkcl_thread_done; /* Let's prononce it dead. */

	      @(return mk_cl_Cnil); /* We muffle any error because this was just a try. */
	    }
1708
	  sched_yield();
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
	}
#endif
      @(return mk_cl_Ct);
    }
  else
    {
      mkcl_type sleep_type = mkcl_type_of(sleeping_on);

      switch (sleep_type)
	{
	case mkcl_t_condition_variable:
	  /* We deliberately shoot too wide but this should be harmless. */
#if 0
	  fprintf(stderr, "\n;; MKCL: Tried to wake up [%s] on a condition variable!\n", thread->thread.name->base_string.self);
	  fflush(stderr);
#endif
	  mk_mt_condition_broadcast(env, sleeping_on);
	  @(return mk_cl_Ct);
	  break;
	case mkcl_t_lock:
	case mkcl_t_rwlock: /* We're out of luck on these, thanks to POSIX. */
#if 0
	  fprintf(stderr, "\n;; MKCL: Tried to wake up [%s] on a lock!\n", thread->thread.name->base_string.self);
	  fflush(stderr);
#endif
	  @(return mk_cl_Cnil);
	  break;
	case mkcl_t_semaphore:
	  /* Messing with a semaphore count is a dangerous propostion at best. Caveat emptor. */
	  /* Sleeper must be able to handle graciously this kind of forced wake up. */
#if 0
	  fprintf(stderr, "\n;; MKCL: Tried to wake up [%s] on a semaphore!\n", thread->thread.name->base_string.self);
	  fflush(stderr);
#endif
	  mkcl_funcall1(env, @+'mt::semaphore-signal', sleeping_on);
	  @(return mk_cl_Ct);
	  break;
	case mkcl_t_cons: /* a hack. just in case. JCB */
	  {
	    void (*f)(void *) = (void (*)(void *)) MKCL_CONS_CAR(sleeping_on);
	    void * data = MKCL_CONS_CDR(sleeping_on);

#if 0
	    fprintf(stderr, "\n;; MKCL: Tried to wake up [%s] on a cons!\n", thread->thread.name->base_string.self);
	    fflush(stderr);
#endif
	    f(data);
	    @(return mk_cl_Ct);
	  }
	  break;
	case mkcl_t_cfun: case mkcl_t_cclosure:
	case mkcl_t_bytecode: case mkcl_t_bclosure:
#if 0
	  fprintf(stderr, "\n;; MKCL: Tried to wake up [%s] on a lisp function!\n", thread->thread.name->base_string.self);
	  fflush(stderr);
#endif
	  mkcl_funcall0(env, sleeping_on);
	  @(return mk_cl_Ct);
	  break;
	default:
#if 0
	  fprintf(stderr, "\n;; MKCL: Tried to wake up [%s] on an UNKNOWN objet!\n", thread->thread.name->base_string.self);
	  fflush(stderr);
#endif
	  @(return mk_cl_Cnil);
	}
    }
}


1779
#if MKCL_UNIX
1780

1781
static void * default_signal_servicing_loop(void * arg)
1782
1783
1784
1785
1786
1787
1788
{
  pthread_detach(pthread_self());
  for (;;) /* sleep forever! */
    pause();
  return NULL;
}

1789
static mkcl_os_thread_t default_signal_servicing_thread;
1790
1791
1792
1793
1794
1795
1796
1797
1798


struct mkcl_signal_disposition
{
  mkcl_sighandler_t c_handler; /* NULL or an apropriate C function pointer. */
  mkcl_object lisp_handler; /* nil means C handler decides everything.
                               T means a lisp handler specific to the C handler will be called.
			       A symbol names the lisp handler function to be called. 
			    */
1799
1800
1801
1802
1803
};


# if __linux
static const struct mkcl_signal_disposition c_signal_disposition[MKCL_SIGMAX + 1] = {
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
  /* Linux signal ordering */
  /*  0 SIG0 */      { NULL, mk_cl_Cnil }, /* does not exist. */
  /*  1 SIGHUP */    { mkcl_generic_signal_handler, @'si::sighup-handler' },
  /*  2 SIGINT */    { mkcl_generic_signal_handler,  @'si::sigint-handler' },
  /*  3 SIGQUIT */   { mkcl_terminal_signal_handler, mk_cl_Ct },
  /*  4 SIGILL */    { mkcl_synchronous_signal_handler, mk_cl_Cnil },
  /*  5 SIGTRAP */   { mkcl_terminal_signal_handler, mk_cl_Ct },
  /*  6 SIGABRT */   { mkcl_terminal_signal_handler, mk_cl_Ct },
  /*  7 SIGBUS */    { mkcl_sigbus_handler, mk_cl_Cnil },
  /*  8 SIGFPE */    { mkcl_sigfpe_handler, mk_cl_Cnil },
  /*  9 SIGKILL */   { NULL, mk_cl_Cnil }, /* cannot be handled! */
  /* 10 SIGUSR1 */   { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* 11 SIGSEGV */   { mkcl_sigsegv_handler, mk_cl_Cnil },
  /* 12 SIGUSR2 */   { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* 13 SIGPIPE */   { mkcl_sigpipe_handler, mk_cl_Cnil },
  /* 14 SIGALRM */   { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* 15 SIGTERM */   { mkcl_generic_signal_handler, @'si::sigterm-handler' },
  /* 16 SIGSTKFLT */ { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* 17 SIGCHLD */   { mkcl_sigchld_handler, mk_cl_Cnil },
  /* 18 SIGCONT */   { NULL, mk_cl_Cnil }, /* continue by default */
  /* 19 SIGSTOP */   { NULL, mk_cl_Cnil }, /* stop by default */
  /* 20 SIGTSTP */   { NULL, mk_cl_Cnil }, /* stop by default */
  /* 21 SIGTTIN */   { NULL, mk_cl_Cnil }, /* stop by default */
  /* 22 SIGTTOU */   { NULL, mk_cl_Cnil }, /* stop by default */
  /* 23 SIGURG */    { NULL, mk_cl_Cnil }, /* ignored by default */
  /* 24 SIGXCPU */   { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* 25 SIGXFSZ */   { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* 26 SIGVTALRM */ { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* 27 SIGPROF */   { NULL, mk_cl_Cnil }, /* used by gprof profiling. */
  /* 28 SIGWINCH */  { NULL, mk_cl_Cnil }, /* ignored by default */
  /* 29 SIGIO */     { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* 30 SIGPWR */    { mkcl_terminal_signal_handler, mk_cl_Ct },
1836
  /* 31 SIGSYS */    { mkcl_terminal_signal_handler, mk_cl_Ct },
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
  /* SIG32 */        { NULL, mk_cl_Cnil }, /* reserved by linux */
  /* SIG33 */        { NULL, mk_cl_Cnil }, /* reserved by linux */
  /* SIG34 */        { mkcl_terminal_signal_handler, mk_cl_Ct }, /* LinuxThreads: reserved */
  /* SIG35 */        { mkcl_terminal_signal_handler, mk_cl_Ct }, /* (default) resume */
  /* SIG36 */        { mkcl_terminal_signal_handler, mk_cl_Ct }, /* (default) interrupt */
  /* SIG37 */        { mkcl_terminal_signal_handler, mk_cl_Ct }, /* (default) wake-up */
  /* SIG38 */        { mkcl_terminal_signal_handler, mk_cl_Ct }, /* used by Boehm's GC */
  /* SIG39 */        { mkcl_terminal_signal_handler, mk_cl_Ct }, /* used by Boehm's GC */
  /* SIG40 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG41 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG42 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG43 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG44 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG45 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG46 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG47 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG48 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG49 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG50 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG51 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG52 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG53 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG54 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG55 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG56 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG57 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG58 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG59 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG60 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG61 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG62 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG63 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG64 */        { mkcl_terminal_signal_handler, mk_cl_Ct }
};

1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
# elif __FreeBSD__

static const struct mkcl_signal_disposition c_signal_disposition[MKCL_SIGMAX + 1] = {
  /* FreeBSD signal ordering */
  /*  0 SIG0 */      { NULL, mk_cl_Cnil }, /* does not exist. */
  /*  1 SIGHUP */    { mkcl_generic_signal_handler, @'si::sighup-handler' },
  /*  2 SIGINT */    { mkcl_generic_signal_handler,  @'si::sigint-handler' },
  /*  3 SIGQUIT */   { mkcl_terminal_signal_handler, mk_cl_Ct },
  /*  4 SIGILL */    { mkcl_synchronous_signal_handler, mk_cl_Cnil },
  /*  5 SIGTRAP */   { mkcl_terminal_signal_handler, mk_cl_Ct },
  /*  6 SIGABRT */   { mkcl_terminal_signal_handler, mk_cl_Ct },
  /*  7 SIGEMT */    { mkcl_terminal_signal_handler, mk_cl_Ct },
  /*  8 SIGFPE */    { mkcl_sigfpe_handler, mk_cl_Cnil },
  /*  9 SIGKILL */   { NULL, mk_cl_Cnil }, /* cannot be handled! */
  /* 10 SIGBUS */    { mkcl_sigbus_handler, mk_cl_Cnil },
  /* 11 SIGSEGV */   { mkcl_sigsegv_handler, mk_cl_Cnil },
  /* 12 SIGSYS */    { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* 13 SIGPIPE */   { mkcl_sigpipe_handler, mk_cl_Cnil },
  /* 14 SIGALRM */   { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* 15 SIGTERM */   { mkcl_generic_signal_handler, @'si::sigterm-handler' },
  /* 16 SIGURG */    { NULL, mk_cl_Cnil }, /* ignored by default */
  /* 17 SIGSTOP */   { NULL, mk_cl_Cnil }, /* cannot be redefined! */
  /* 18 SIGTSTP */   { NULL, mk_cl_Cnil }, /* stop by default */
  /* 19 SIGCONT */   { NULL, mk_cl_Cnil }, /* continue by default */
  /* 20 SIGCHLD */   { mkcl_sigchld_handler, mk_cl_Cnil },
  /* 21 SIGTTIN */   { NULL, mk_cl_Cnil }, /* stop by default */
  /* 22 SIGTTOU */   { NULL, mk_cl_Cnil }, /* stop by default */
  /* 23 SIGIO */     { NULL, mk_cl_Cnil }, /* ignored by default */
  /* 24 SIGXCPU */   { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* 25 SIGXFSZ */   { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* 26 SIGVTALRM */ { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* 27 SIGPROF */   { mkcl_terminal_signal_handler, mk_cl_Ct }, /* used by gprof profiling. */
  /* 28 SIGWINCH */  { NULL, mk_cl_Cnil }, /* ignored by default */
  /* 29 SIGINFO */   { NULL, mk_cl_Cnil }, /* ignored by default */
  /* 30 SIGUSR1 */   { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* 31 SIGUSR2 */   { mkcl_terminal_signal_handler, mk_cl_Ct },

  /* 32 SIGTHR */    { NULL, mk_cl_Cnil }, /* reserved by FreeBSD, terminal? */
  /* 33 SIGLIBRT */  { NULL, mk_cl_Cnil }, /* reserved by FreeBSD, terminal? */

  /* SIG34 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG35 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG36 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG37 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG38 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG39 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG40 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG41 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG42 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG43 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG44 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG45 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG46 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG47 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG48 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG49 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG50 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG51 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG52 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG53 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG54 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG55 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG56 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG57 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG58 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG59 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG60 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG61 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG62 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG63 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG64 */        { mkcl_terminal_signal_handler, mk_cl_Ct },

  /* SIG65 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG66 */        { mkcl_terminal_signal_handler, mk_cl_Ct }, /* (default) resume */
  /* SIG67 */        { mkcl_terminal_signal_handler, mk_cl_Ct }, /* (default) interrupt */
  /* SIG68 */        { mkcl_terminal_signal_handler, mk_cl_Ct }, /* (default) wake-up */
  /* SIG69 */        { mkcl_terminal_signal_handler, mk_cl_Ct }, /* used by Boehm's GC */
  /* SIG70 */        { mkcl_terminal_signal_handler, mk_cl_Ct }, /* used by Boehm's GC */
  /* SIG71 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG72 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG73 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG74 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG75 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG76 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG77 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG78 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG79 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG80 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG81 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG82 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG83 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG84 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG85 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG86 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG87 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG88 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG89 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG90 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG91 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG92 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG93 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG94 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG95 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG96 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG97 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG98 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG99 */        { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG100 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG101 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG102 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG103 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG104 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG105 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG106 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG107 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG108 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG109 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG110 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG111 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG112 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG113 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG114 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG115 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG116 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG117 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG118 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG119 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG120 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG121 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG122 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG123 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG124 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG125 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG126 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG127 */       { mkcl_terminal_signal_handler, mk_cl_Ct },
  /* SIG128 */       { mkcl_terminal_signal_handler, mk_cl_Ct }
};
# endif /* __FreeBSD__ */

#endif /* MKCL_UNIX */
2012
2013
2014

void mkcl_init_early_unixint(MKCL)
{
2015
#if MKCL_PTHREADS
2016
2017
2018
2019
2020
2021
2022
  int i;
  stderr_fd = fileno(stderr);

  mkcl_pid = getpid();

  /* Create a forever sleeping thread to ensure async-signals handlers will always find
     at least one thread stack to run on. */
2023
  if (pthread_create(&default_signal_servicing_thread, NULL, default_signal_servicing_loop, NULL))
2024
2025
    mkcl_lose(env, "mkcl_init_unixint failed on pthread_create.");

Jean-Claude Beaudoin's avatar
Jean-Claude Beaudoin committed
2026
2027
# if __ANDROID__ && (__arm__ || __i386__)
  /* Signal mask support for realtime signals is broken in Android 32bits. */
2028
2029
2030
2031
2032
2033
2034
2035
#  define DEFAULT_THREAD_RESUME_SIGNAL SIGWINCH
#  define DEFAULT_THREAD_INTERRUPT_SIGNAL SIGSTKFLT
#  define DEFAULT_THREAD_WAKE_UP_SIGNAL SIGXFSZ
# else
#  define DEFAULT_THREAD_RESUME_SIGNAL SIGRTMIN + 1
#  define DEFAULT_THREAD_INTERRUPT_SIGNAL SIGRTMIN + 2
#  define DEFAULT_THREAD_WAKE_UP_SIGNAL SIGRTMIN + 3
# endif
2036

2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066

  wake_up_sig = mkcl_get_option(MKCL_OPT_THREAD_WAKE_UP_SIGNAL);
  if (wake_up_sig == 0) {
    wake_up_sig = DEFAULT_THREAD_WAKE_UP_SIGNAL;
    mkcl_set_option(MKCL_OPT_THREAD_WAKE_UP_SIGNAL, wake_up_sig);
  }
  resume_sig = mkcl_get_option(MKCL_OPT_THREAD_RESUME_SIGNAL);
  if (resume_sig == 0) {
    resume_sig = DEFAULT_THREAD_RESUME_SIGNAL;
    mkcl_set_option(MKCL_OPT_THREAD_RESUME_SIGNAL, resume_sig);
  }
  interrupt_sig = mkcl_get_option(MKCL_OPT_THREAD_INTERRUPT_SIGNAL);
  if (interrupt_sig == 0) {
    interrupt_sig = DEFAULT_THREAD_INTERRUPT_SIGNAL;
    mkcl_set_option(MKCL_OPT_THREAD_INTERRUPT_SIGNAL, interrupt_sig);
  }

  if (mkcl_get_option(MKCL_OPT_CHAIN_SIGSEGV)) {
    mkcl_signals[SIGSEGV].chainable = TRUE;
  }
  if (mkcl_get_option(MKCL_OPT_CHAIN_SIGBUS)) {
    mkcl_signals[SIGBUS].chainable = TRUE;
  }
  if (mkcl_get_option(MKCL_OPT_CHAIN_SIGINT)) {
    mkcl_signals[SIGINT].chainable = TRUE;
  }
  if (mkcl_get_option(MKCL_OPT_CHAIN_SIGFPE)) {
    mkcl_signals[SIGFPE].chainable = TRUE;
  }

2067
  for (i = 0; i <= MKCL_SIGMAX; i++)
2068
2069
2070
2071
2072
    {
      mkcl_signals[i].sem = &mkcl_signals[i].sem_obj;
      if (sem_init(mkcl_signals[i].sem, 0, 0))
	mkcl_C_lose(env, "mkcl_init_early_unixint failed on sem_init.");
    }
2073
2074
#elif MKCL_WINDOWS
#endif /* MKCL_UNIX */
2075
2076
2077
2078
}

void mkcl_init_late_unixint(MKCL)
{
2079
#if MKCL_UNIX
2080
#if MKCL_GC_7_2d
2081
2082
  int gc_thread_suspend_sig = MK_GC_suspend_signal();
  int gc_thread_restart_sig = MK_GC_thread_restart_signal();
2083
2084
2085
2086
#else
  int gc_thread_suspend_sig = MK_GC_get_suspend_signal();
  int gc_thread_restart_sig = MK_GC_get_thr_restart_signal();
#endif
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
  int i;

  install_lisp_terminal_signal_handler(env);

  install_wake_up_signal_handler(env, wake_up_sig);
#if 0
  c_signal_disposition[wake_up_sig].c_handler = NULL;
  c_signal_disposition[wake_up_sig].lisp_handler = mk_cl_Cnil;
#endif

  install_resume_signal_handler(env, resume_sig);
#if 0
  c_signal_disposition[resume_sig].c_handler = NULL;
  c_signal_disposition[resume_sig].lisp_handler = mk_cl_Cnil;
#endif

  install_interrupt_signal_handler(env, interrupt_sig);
#if 0
  c_signal_disposition[interrupt_sig].c_handler = NULL;
  c_signal_disposition[interrupt_sig].lisp_handler = mk_cl_Cnil;
#endif