mach_dep.c 11.4 KB
Newer Older
1
/*
eg's avatar
eg committed
2 3 4 5 6 7 8 9 10 11 12 13
 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
 * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
 *
 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
 * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
 *
 * Permission is hereby granted to use or copy this program
 * for any purpose,  provided the above notices are retained on all copies.
 * Permission to modify the code and to distribute modified code is granted,
 * provided the above notices are retained, and a notice that the code was
 * modified is included with the above copyright notice.
 */
14 15 16 17 18 19 20 21 22 23

#include "private/gc_priv.h"

#include <stdio.h>

#ifdef AMIGA
# ifndef __GNUC__
#   include <dos.h>
# else
#   include <machine/reg.h>
eg's avatar
eg committed
24
# endif
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#endif

#if defined(MACOS) && defined(__MWERKS__)

#if defined(POWERPC)

# define NONVOLATILE_GPR_COUNT 19
  struct ppc_registers {
        unsigned long gprs[NONVOLATILE_GPR_COUNT];      /* R13-R31 */
  };
  typedef struct ppc_registers ppc_registers;

# if defined(CPPCHECK)
    void getRegisters(ppc_registers* regs);
# else
    asm static void getRegisters(register ppc_registers* regs)
    {
        stmw    r13,regs->gprs                          /* save R13-R31 */
        blr
    }
eg's avatar
eg committed
45 46
# endif

47 48 49 50 51 52 53 54
  static void PushMacRegisters(void)
  {
        ppc_registers regs;
        int i;
        getRegisters(&regs);
        for (i = 0; i < NONVOLATILE_GPR_COUNT; i++)
                GC_push_one(regs.gprs[i]);
  }
eg's avatar
eg committed
55

56 57 58 59 60
#else /* M68K */

  asm static void PushMacRegisters(void)
  {
    sub.w   #4,sp                   /* reserve space for one parameter */
eg's avatar
eg committed
61
    move.l  a2,(sp)
62
    jsr         GC_push_one
eg's avatar
eg committed
63
    move.l  a3,(sp)
64
    jsr         GC_push_one
eg's avatar
eg committed
65
    move.l  a4,(sp)
66
    jsr         GC_push_one
eg's avatar
eg committed
67
#   if !__option(a6frames)
68 69 70
      /* <pcb> perhaps a6 should be pushed if stack frames are not being used */
        move.l  a6,(sp)
        jsr             GC_push_one
eg's avatar
eg committed
71
#   endif
72
        /* skip a5 (globals), a6 (frame pointer), and a7 (stack pointer) */
eg's avatar
eg committed
73
    move.l  d2,(sp)
74
    jsr         GC_push_one
eg's avatar
eg committed
75
    move.l  d3,(sp)
76
    jsr         GC_push_one
eg's avatar
eg committed
77
    move.l  d4,(sp)
78
    jsr         GC_push_one
eg's avatar
eg committed
79
    move.l  d5,(sp)
80
    jsr         GC_push_one
eg's avatar
eg committed
81
    move.l  d6,(sp)
82
    jsr         GC_push_one
eg's avatar
eg committed
83
    move.l  d7,(sp)
84 85
    jsr         GC_push_one
    add.w   #4,sp                   /* fix stack */
eg's avatar
eg committed
86
    rts
87
  }
eg's avatar
eg committed
88

89 90 91
#endif /* M68K */

#endif /* MACOS && __MWERKS__ */
eg's avatar
eg committed
92 93 94

# if defined(SPARC) || defined(IA64)
    /* Value returned from register flushing routine; either sp (SPARC) */
95 96
    /* or ar.bsp (IA64).                                                */
    GC_INNER ptr_t GC_save_regs_ret_val = NULL;
eg's avatar
eg committed
97 98 99
# endif

/* Routine to mark from registers that are preserved by the C compiler. */
100 101 102
/* This must be ported to every new architecture.  It is not optional,  */
/* and should not be used on platforms that are either UNIX-like, or    */
/* require thread support.                                              */
eg's avatar
eg committed
103

eg's avatar
eg committed
104 105 106
#undef HAVE_PUSH_REGS

#if defined(USE_ASM_PUSH_REGS)
107
# define HAVE_PUSH_REGS
eg's avatar
eg committed
108
#else  /* No asm implementation */
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

# ifdef STACK_NOT_SCANNED
    void GC_push_regs(void)
    {
      /* empty */
    }
#   define HAVE_PUSH_REGS

# elif defined(M68K) && defined(AMIGA)
    /* This function is not static because it could also be             */
    /* erroneously defined in .S file, so this error would be caught    */
    /* by the linker.                                                   */
    void GC_push_regs(void)
    {
         /*  AMIGA - could be replaced by generic code                  */
         /* a0, a1, d0 and d1 are caller save */

#       ifdef __GNUC__
          asm("subq.w &0x4,%sp");       /* allocate word on top of stack */

          asm("mov.l %a2,(%sp)"); asm("jsr _GC_push_one");
          asm("mov.l %a3,(%sp)"); asm("jsr _GC_push_one");
          asm("mov.l %a4,(%sp)"); asm("jsr _GC_push_one");
          asm("mov.l %a5,(%sp)"); asm("jsr _GC_push_one");
          asm("mov.l %a6,(%sp)"); asm("jsr _GC_push_one");
          /* Skip frame pointer and stack pointer */
          asm("mov.l %d2,(%sp)"); asm("jsr _GC_push_one");
          asm("mov.l %d3,(%sp)"); asm("jsr _GC_push_one");
          asm("mov.l %d4,(%sp)"); asm("jsr _GC_push_one");
          asm("mov.l %d5,(%sp)"); asm("jsr _GC_push_one");
          asm("mov.l %d6,(%sp)"); asm("jsr _GC_push_one");
          asm("mov.l %d7,(%sp)"); asm("jsr _GC_push_one");

          asm("addq.w &0x4,%sp");       /* put stack back where it was  */
#       else /* !__GNUC__ */
          GC_push_one(getreg(REG_A2));
          GC_push_one(getreg(REG_A3));
eg's avatar
eg committed
146
#         ifndef __SASC
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
            /* Can probably be changed to #if 0 -Kjetil M. (a4=globals) */
            GC_push_one(getreg(REG_A4));
#         endif
          GC_push_one(getreg(REG_A5));
          GC_push_one(getreg(REG_A6));
          /* Skip stack pointer */
          GC_push_one(getreg(REG_D2));
          GC_push_one(getreg(REG_D3));
          GC_push_one(getreg(REG_D4));
          GC_push_one(getreg(REG_D5));
          GC_push_one(getreg(REG_D6));
          GC_push_one(getreg(REG_D7));
#       endif /* !__GNUC__ */
    }
#   define HAVE_PUSH_REGS

# elif defined(MACOS)

#   if defined(M68K) && defined(THINK_C) && !defined(CPPCHECK)
#     define PushMacReg(reg) \
eg's avatar
eg committed
167 168
              move.l  reg,(sp) \
              jsr             GC_push_one
169 170 171 172
      void GC_push_regs(void)
      {
          asm {
              sub.w   #4,sp          ; reserve space for one parameter.
eg's avatar
eg committed
173 174 175 176 177 178 179 180 181 182
              PushMacReg(a2);
              PushMacReg(a3);
              PushMacReg(a4);
              ; skip a5 (globals), a6 (frame pointer), and a7 (stack pointer)
              PushMacReg(d2);
              PushMacReg(d3);
              PushMacReg(d4);
              PushMacReg(d5);
              PushMacReg(d6);
              PushMacReg(d7);
183 184 185 186 187 188 189 190 191 192 193 194 195 196
              add.w   #4,sp          ; fix stack.
          }
      }
#     define HAVE_PUSH_REGS
#     undef PushMacReg
#   elif defined(__MWERKS__)
      void GC_push_regs(void)
      {
          PushMacRegisters();
      }
#     define HAVE_PUSH_REGS
#   endif /* __MWERKS__ */
# endif /* MACOS */

eg's avatar
eg committed
197
#endif /* !USE_ASM_PUSH_REGS */
eg's avatar
eg committed
198

eg's avatar
eg committed
199 200
#if defined(HAVE_PUSH_REGS) && defined(THREADS)
# error GC_push_regs cannot be used with threads
201 202
 /* Would fail for GC_do_blocking.  There are probably other safety     */
 /* issues.                                                             */
eg's avatar
eg committed
203 204
# undef HAVE_PUSH_REGS
#endif
eg's avatar
eg committed
205

206 207
#if !defined(HAVE_PUSH_REGS) && defined(UNIX_LIKE)
# include <signal.h>
208 209 210 211 212 213 214 215 216 217 218 219
# ifndef NO_GETCONTEXT
#   if defined(DARWIN) \
       && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1060 /*MAC_OS_X_VERSION_10_6*/)
#     include <sys/ucontext.h>
#   else
#     include <ucontext.h>
#   endif /* !DARWIN */
#   ifdef GETCONTEXT_FPU_EXCMASK_BUG
#     include <fenv.h>
#   endif
# endif
#endif /* !HAVE_PUSH_REGS */
220

221 222 223 224 225 226
/* Ensure that either registers are pushed, or callee-save registers    */
/* are somewhere on the stack, and then call fn(arg, ctxt).             */
/* ctxt is either a pointer to a ucontext_t we generated, or NULL.      */
GC_ATTR_NO_SANITIZE_ADDR
GC_INNER void GC_with_callee_saves_pushed(void (*fn)(ptr_t, void *),
                                          volatile ptr_t arg)
eg's avatar
eg committed
227
{
228 229 230 231 232 233 234 235 236 237
  volatile int dummy;
  void * volatile context = 0;

# if defined(HAVE_PUSH_REGS)
    GC_push_regs();
# else
#   if defined(UNIX_LIKE) && !defined(NO_GETCONTEXT)
      /* Older versions of Darwin seem to lack getcontext().    */
      /* ARM and MIPS Linux often doesn't support a real        */
      /* getcontext().                                          */
eg's avatar
eg committed
238
      ucontext_t ctxt;
239 240 241 242 243 244 245 246 247 248 249 250 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
#     ifdef GETCONTEXT_FPU_EXCMASK_BUG
        /* Workaround a bug (clearing the FPU exception mask) in        */
        /* getcontext on Linux/x86_64.                                  */
#       ifdef X86_64
          /* We manipulate FPU control word here just not to force the  */
          /* client application to use -lm linker option.               */
          unsigned short old_fcw;

#         if defined(CPPCHECK)
            GC_noop1((word)&old_fcw);
#         endif
          __asm__ __volatile__ ("fstcw %0" : "=m" (*&old_fcw));
#       else
          int except_mask = fegetexcept();
#       endif
#     endif

      if (getcontext(&ctxt) < 0) {
        WARN("getcontext failed:"
             " using another register retrieval method...\n", 0);
        /* E.g., to workaround a bug in Docker ubuntu_32bit.    */
      } else {
        context = &ctxt;
      }
#     ifdef GETCONTEXT_FPU_EXCMASK_BUG
#       ifdef X86_64
          __asm__ __volatile__ ("fldcw %0" : : "m" (*&old_fcw));
          {
            unsigned mxcsr;
            /* And now correct the exception mask in SSE MXCSR. */
            __asm__ __volatile__ ("stmxcsr %0" : "=m" (*&mxcsr));
            mxcsr = (mxcsr & ~(FE_ALL_EXCEPT << 7)) |
                        ((old_fcw & FE_ALL_EXCEPT) << 7);
            __asm__ __volatile__ ("ldmxcsr %0" : : "m" (*&mxcsr));
          }
#       else /* !X86_64 */
          if (feenableexcept(except_mask) < 0)
            ABORT("feenableexcept failed");
#       endif
#     endif /* GETCONTEXT_FPU_EXCMASK_BUG */
eg's avatar
eg committed
279
#     if defined(SPARC) || defined(IA64)
280 281 282 283 284 285 286 287 288 289 290 291 292 293
        /* On a register window machine, we need to save register       */
        /* contents on the stack for this to work.  This may already be */
        /* subsumed by the getcontext() call.                           */
        GC_save_regs_ret_val = GC_save_regs_in_stack();
#     endif
      if (NULL == context) /* getcontext failed */
#   endif /* !NO_GETCONTEXT */
    {
#     if defined(HAVE_BUILTIN_UNWIND_INIT)
        /* This was suggested by Richard Henderson as the way to        */
        /* force callee-save registers and register windows onto        */
        /* the stack.                                                   */
        __builtin_unwind_init();
#     else
eg's avatar
eg committed
294 295 296
        /* Generic code                          */
        /* The idea is due to Parag Patel at HP. */
        /* We're not sure whether he would like  */
297
        /* to be acknowledged for it or not.     */
eg's avatar
eg committed
298
        jmp_buf regs;
299 300 301 302 303 304 305 306 307 308 309 310
        register word * i = (word *) &regs;
        register ptr_t lim = (ptr_t)(&regs) + (sizeof regs);

        /* Setjmp doesn't always clear all of the buffer.               */
        /* That tends to preserve garbage.  Clear it.                   */
        for (; (word)i < (word)lim; i++) {
            *i = 0;
        }
#       if defined(MSWIN32) || defined(MSWINCE) || defined(UTS4) \
           || defined(OS2) || defined(CX_UX) || defined(__CC_ARM) \
           || defined(LINUX) || defined(EWS4800) || defined(RTEMS)
          (void) setjmp(regs);
eg's avatar
eg committed
311 312
#       else
          (void) _setjmp(regs);
313 314 315
          /* We don't want to mess with signals. According to   */
          /* SUSV3, setjmp() may or may not save signal mask.   */
          /* _setjmp won't, but is less portable.               */
eg's avatar
eg committed
316
#       endif
317 318 319 320 321 322 323 324 325 326
#     endif /* !HAVE_BUILTIN_UNWIND_INIT */
    }
# endif /* !HAVE_PUSH_REGS */
  /* FIXME: context here is sometimes just zero.  At the moment the     */
  /* callees don't really need it.                                      */
  fn(arg, context);
  /* Strongly discourage the compiler from treating the above   */
  /* as a tail-call, since that would pop the register          */
  /* contents before we get a chance to look at them.           */
  GC_noop1((word)(&dummy));
eg's avatar
eg committed
327 328 329 330
}

#if defined(ASM_CLEAR_CODE)
# ifdef LINT
331 332 333 334
    ptr_t GC_clear_stack_inner(ptr_t arg, word limit)
    {
      return limit ? arg : 0; /* use both arguments */
    }
eg's avatar
eg committed
335
    /* The real version is in a .S file */
eg's avatar
eg committed
336
# endif
337
#endif /* ASM_CLEAR_CODE */