1 /*
2  * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3  * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
4  *
5  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
6  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
7  *
8  * Permission is hereby granted to use or copy this program
9  * for any purpose,  provided the above notices are retained on all copies.
10  * Permission to modify the code and to distribute modified code is granted,
11  * provided the above notices are retained, and a notice that the code was
12  * modified is included with the above copyright notice.
13  */
14 /* Boehm, November 17, 1995 12:13 pm PST */
15 # include "gc_priv.h"
16 # include <stdio.h>
17 # include <setjmp.h>
18 # if defined(OS2) || defined(CX_UX)
19 #   define _setjmp(b) setjmp(b)
20 #   define _longjmp(b,v) longjmp(b,v)
21 # endif
22 # ifdef AMIGA
23 #   include <dos.h>
24 # endif
25 
26 #if defined(__MWERKS__) && !defined(POWERPC)
27 
PushMacRegisters()28 asm static void PushMacRegisters()
29 {
30     sub.w   #4,sp                   // reserve space for one parameter.
31     move.l  a2,(sp)
32     jsr		GC_push_one
33     move.l  a3,(sp)
34     jsr		GC_push_one
35     move.l  a4,(sp)
36     jsr		GC_push_one
37 #   if !__option(a6frames)
38 	// <pcb> perhaps a6 should be pushed if stack frames are not being used.
39   	move.l	a6,(sp)
40   	jsr		GC_push_one
41 #   endif
42 	// skip a5 (globals), a6 (frame pointer), and a7 (stack pointer)
43     move.l  d2,(sp)
44     jsr		GC_push_one
45     move.l  d3,(sp)
46     jsr		GC_push_one
47     move.l  d4,(sp)
48     jsr		GC_push_one
49     move.l  d5,(sp)
50     jsr		GC_push_one
51     move.l  d6,(sp)
52     jsr		GC_push_one
53     move.l  d7,(sp)
54     jsr		GC_push_one
55     add.w   #4,sp                   // fix stack.
56     rts
57 }
58 
59 #endif /* __MWERKS__ */
60 
61 /* Routine to mark from registers that are preserved by the C compiler. */
62 /* This must be ported to every new architecture.  There is a generic   */
63 /* version at the end, that is likely, but not guaranteed to work       */
64 /* on your architecture.  Run the test_setjmp program to see whether    */
65 /* there is any chance it will work.                                    */
66 
GC_push_regs()67 void GC_push_regs()
68 {
69 #       ifdef RT
70 	  register long TMP_SP; /* must be bound to r11 */
71 #       endif
72 #       ifdef VAX
73 	  /* VAX - generic code below does not work under 4.2 */
74 	  /* r1 through r5 are caller save, and therefore     */
75 	  /* on the stack or dead.                            */
76 	  asm("pushl r11");     asm("calls $1,_GC_push_one");
77 	  asm("pushl r10"); 	asm("calls $1,_GC_push_one");
78 	  asm("pushl r9");	asm("calls $1,_GC_push_one");
79 	  asm("pushl r8");	asm("calls $1,_GC_push_one");
80 	  asm("pushl r7");	asm("calls $1,_GC_push_one");
81 	  asm("pushl r6");	asm("calls $1,_GC_push_one");
82 #       endif
83 #       if defined(M68K) && (defined(SUNOS4) || defined(NEXT))
84 	/*  M68K SUNOS - could be replaced by generic code */
85 	  /* a0, a1 and d1 are caller save          */
86 	  /*  and therefore are on stack or dead.   */
87 
88 	  asm("subqw #0x4,sp");		/* allocate word on top of stack */
89 
90 	  asm("movl a2,sp@");	asm("jbsr _GC_push_one");
91 	  asm("movl a3,sp@");	asm("jbsr _GC_push_one");
92 	  asm("movl a4,sp@");	asm("jbsr _GC_push_one");
93 	  asm("movl a5,sp@");	asm("jbsr _GC_push_one");
94 	  /* Skip frame pointer and stack pointer */
95 	  asm("movl d1,sp@");	asm("jbsr _GC_push_one");
96 	  asm("movl d2,sp@");	asm("jbsr _GC_push_one");
97 	  asm("movl d3,sp@");	asm("jbsr _GC_push_one");
98 	  asm("movl d4,sp@");	asm("jbsr _GC_push_one");
99 	  asm("movl d5,sp@");	asm("jbsr _GC_push_one");
100 	  asm("movl d6,sp@");	asm("jbsr _GC_push_one");
101 	  asm("movl d7,sp@");	asm("jbsr _GC_push_one");
102 
103 	  asm("addqw #0x4,sp");		/* put stack back where it was	*/
104 #       endif
105 
106 #       if defined(M68K) && defined(HP)
107 	/*  M68K HP - could be replaced by generic code */
108 	  /* a0, a1 and d1 are caller save.  */
109 
110 	  asm("subq.w &0x4,%sp");	/* allocate word on top of stack */
111 
112 	  asm("mov.l %a2,(%sp)"); asm("jsr _GC_push_one");
113 	  asm("mov.l %a3,(%sp)"); asm("jsr _GC_push_one");
114 	  asm("mov.l %a4,(%sp)"); asm("jsr _GC_push_one");
115 	  asm("mov.l %a5,(%sp)"); asm("jsr _GC_push_one");
116 	  /* Skip frame pointer and stack pointer */
117 	  asm("mov.l %d1,(%sp)"); asm("jsr _GC_push_one");
118 	  asm("mov.l %d2,(%sp)"); asm("jsr _GC_push_one");
119 	  asm("mov.l %d3,(%sp)"); asm("jsr _GC_push_one");
120 	  asm("mov.l %d4,(%sp)"); asm("jsr _GC_push_one");
121 	  asm("mov.l %d5,(%sp)"); asm("jsr _GC_push_one");
122 	  asm("mov.l %d6,(%sp)"); asm("jsr _GC_push_one");
123 	  asm("mov.l %d7,(%sp)"); asm("jsr _GC_push_one");
124 
125 	  asm("addq.w &0x4,%sp");	/* put stack back where it was	*/
126 #       endif /* M68K HP */
127 
128 #       ifdef AMIGA
129 	/*  AMIGA - could be replaced by generic code 			*/
130 	  /* a0, a1, d0 and d1 are caller save */
131 	  GC_push_one(getreg(REG_A2));
132 	  GC_push_one(getreg(REG_A3));
133 	  GC_push_one(getreg(REG_A4));
134 	  GC_push_one(getreg(REG_A5));
135 	  GC_push_one(getreg(REG_A6));
136 	  /* Skip stack pointer */
137 	  GC_push_one(getreg(REG_D2));
138 	  GC_push_one(getreg(REG_D3));
139 	  GC_push_one(getreg(REG_D4));
140 	  GC_push_one(getreg(REG_D5));
141 	  GC_push_one(getreg(REG_D6));
142 	  GC_push_one(getreg(REG_D7));
143 #       endif
144 
145 #	if defined(M68K) && defined(MACOS)
146 #	if defined(THINK_C)
147 #         define PushMacReg(reg) \
148               move.l  reg,(sp) \
149               jsr             GC_push_one
150 	  asm {
151               sub.w   #4,sp                   ; reserve space for one parameter.
152               PushMacReg(a2);
153               PushMacReg(a3);
154               PushMacReg(a4);
155               ; skip a5 (globals), a6 (frame pointer), and a7 (stack pointer)
156               PushMacReg(d2);
157               PushMacReg(d3);
158               PushMacReg(d4);
159               PushMacReg(d5);
160               PushMacReg(d6);
161               PushMacReg(d7);
162               add.w   #4,sp                   ; fix stack.
163 	  }
164 #	  undef PushMacReg
165 #	endif /* THINK_C */
166 #	if defined(__MWERKS__)
167 	  PushMacRegisters();
168 #	endif	/* __MWERKS__ */
169 #   endif	/* MACOS */
170 
171 #       if defined(I386) &&!defined(OS2) &&!defined(SVR4) &&!defined(MSWIN32) \
172 	&& !defined(SCO) && !(defined(LINUX) && defined(__ELF__)) \
173 	&& !defined(DOS4GW)
174 	/* I386 code, generic code does not appear to work */
175 	/* It does appear to work under OS2, and asms dont */
176 	/* This is used for some 38g UNIX variants and for CYGWIN32 */
177 	  asm("pushl %eax");  asm("call _GC_push_one"); asm("addl $4,%esp");
178 	  asm("pushl %ecx");  asm("call _GC_push_one"); asm("addl $4,%esp");
179 	  asm("pushl %edx");  asm("call _GC_push_one"); asm("addl $4,%esp");
180 	  asm("pushl %ebp");  asm("call _GC_push_one"); asm("addl $4,%esp");
181 	  asm("pushl %esi");  asm("call _GC_push_one"); asm("addl $4,%esp");
182 	  asm("pushl %edi");  asm("call _GC_push_one"); asm("addl $4,%esp");
183 	  asm("pushl %ebx");  asm("call _GC_push_one"); asm("addl $4,%esp");
184 #       endif
185 
186 #	if defined(I386) && defined(LINUX) && defined(__ELF__)
187 	/* This is modified for Linux with ELF (Note: _ELF_ only) */
188 	  asm("pushl %eax");  asm("call GC_push_one"); asm("addl $4,%esp");
189 	  asm("pushl %ecx");  asm("call GC_push_one"); asm("addl $4,%esp");
190 	  asm("pushl %edx");  asm("call GC_push_one"); asm("addl $4,%esp");
191 	  asm("pushl %ebp");  asm("call GC_push_one"); asm("addl $4,%esp");
192 	  asm("pushl %esi");  asm("call GC_push_one"); asm("addl $4,%esp");
193 	  asm("pushl %edi");  asm("call GC_push_one"); asm("addl $4,%esp");
194 	  asm("pushl %ebx");  asm("call GC_push_one"); asm("addl $4,%esp");
195 #	endif
196 
197 #       if defined(I386) && defined(MSWIN32) && !defined(USE_GENERIC)
198 	/* I386 code, Microsoft variant		*/
199 	  __asm  push eax
200 	  __asm  call GC_push_one
201 	  __asm  add esp,4
202 	  __asm  push ebx
203 	  __asm  call GC_push_one
204 	  __asm  add esp,4
205 	  __asm  push ecx
206 	  __asm  call GC_push_one
207 	  __asm  add esp,4
208 	  __asm  push edx
209 	  __asm  call GC_push_one
210 	  __asm  add esp,4
211 	  __asm  push ebp
212 	  __asm  call GC_push_one
213 	  __asm  add esp,4
214 	  __asm  push esi
215 	  __asm  call GC_push_one
216 	  __asm  add esp,4
217 	  __asm  push edi
218 	  __asm  call GC_push_one
219 	  __asm  add esp,4
220 #       endif
221 
222 #       if defined(I386) && (defined(SVR4) || defined(SCO))
223 	/* I386 code, SVR4 variant, generic code does not appear to work */
224 	  asm("pushl %eax");  asm("call GC_push_one"); asm("addl $4,%esp");
225 	  asm("pushl %ebx");  asm("call GC_push_one"); asm("addl $4,%esp");
226 	  asm("pushl %ecx");  asm("call GC_push_one"); asm("addl $4,%esp");
227 	  asm("pushl %edx");  asm("call GC_push_one"); asm("addl $4,%esp");
228 	  asm("pushl %ebp");  asm("call GC_push_one"); asm("addl $4,%esp");
229 	  asm("pushl %esi");  asm("call GC_push_one"); asm("addl $4,%esp");
230 	  asm("pushl %edi");  asm("call GC_push_one"); asm("addl $4,%esp");
231 #       endif
232 
233 #       ifdef NS32K
234 	  asm ("movd r3, tos"); asm ("bsr ?_GC_push_one"); asm ("adjspb $-4");
235 	  asm ("movd r4, tos"); asm ("bsr ?_GC_push_one"); asm ("adjspb $-4");
236 	  asm ("movd r5, tos"); asm ("bsr ?_GC_push_one"); asm ("adjspb $-4");
237 	  asm ("movd r6, tos"); asm ("bsr ?_GC_push_one"); asm ("adjspb $-4");
238 	  asm ("movd r7, tos"); asm ("bsr ?_GC_push_one"); asm ("adjspb $-4");
239 #       endif
240 
241 #       ifdef SPARC
242 	  {
243 	      word GC_save_regs_in_stack();
244 
245 	      /* generic code will not work */
246 	      (void)GC_save_regs_in_stack();
247 	  }
248 #       endif
249 
250 #	ifdef RT
251 	    GC_push_one(TMP_SP);    /* GC_push_one from r11 */
252 
253 	    asm("cas r11, r6, r0"); GC_push_one(TMP_SP);	/* r6 */
254 	    asm("cas r11, r7, r0"); GC_push_one(TMP_SP);	/* through */
255 	    asm("cas r11, r8, r0"); GC_push_one(TMP_SP);	/* r10 */
256 	    asm("cas r11, r9, r0"); GC_push_one(TMP_SP);
257 	    asm("cas r11, r10, r0"); GC_push_one(TMP_SP);
258 
259 	    asm("cas r11, r12, r0"); GC_push_one(TMP_SP); /* r12 */
260 	    asm("cas r11, r13, r0"); GC_push_one(TMP_SP); /* through */
261 	    asm("cas r11, r14, r0"); GC_push_one(TMP_SP); /* r15 */
262 	    asm("cas r11, r15, r0"); GC_push_one(TMP_SP);
263 #       endif
264 
265 #       if defined(M68K) && defined(SYSV)
266   	/*  Once again similar to SUN and HP, though setjmp appears to work.
267   		--Parag
268   	 */
269 #        ifdef __GNUC__
270   	  asm("subqw #0x4,%sp");	/* allocate word on top of stack */
271 
272   	  asm("movl %a2,%sp@");	asm("jbsr GC_push_one");
273   	  asm("movl %a3,%sp@");	asm("jbsr GC_push_one");
274   	  asm("movl %a4,%sp@");	asm("jbsr GC_push_one");
275   	  asm("movl %a5,%sp@");	asm("jbsr GC_push_one");
276   	  /* Skip frame pointer and stack pointer */
277   	  asm("movl %d1,%sp@");	asm("jbsr GC_push_one");
278   	  asm("movl %d2,%sp@");	asm("jbsr GC_push_one");
279   	  asm("movl %d3,%sp@");	asm("jbsr GC_push_one");
280   	  asm("movl %d4,%sp@");	asm("jbsr GC_push_one");
281   	  asm("movl %d5,%sp@");	asm("jbsr GC_push_one");
282   	  asm("movl %d6,%sp@");	asm("jbsr GC_push_one");
283   	  asm("movl %d7,%sp@");	asm("jbsr GC_push_one");
284 
285   	  asm("addqw #0x4,%sp");	/* put stack back where it was	*/
286 #        else /* !__GNUC__*/
287   	  asm("subq.w &0x4,%sp");	/* allocate word on top of stack */
288 
289   	  asm("mov.l %a2,(%sp)"); asm("jsr GC_push_one");
290   	  asm("mov.l %a3,(%sp)"); asm("jsr GC_push_one");
291   	  asm("mov.l %a4,(%sp)"); asm("jsr GC_push_one");
292   	  asm("mov.l %a5,(%sp)"); asm("jsr GC_push_one");
293   	  /* Skip frame pointer and stack pointer */
294   	  asm("mov.l %d1,(%sp)"); asm("jsr GC_push_one");
295   	  asm("mov.l %d2,(%sp)"); asm("jsr GC_push_one");
296   	  asm("mov.l %d3,(%sp)"); asm("jsr GC_push_one");
297   	  asm("mov.l %d4,(%sp)"); asm("jsr GC_push_one");
298   	  asm("mov.l %d5,(%sp)"); asm("jsr GC_push_one");
299    	  asm("mov.l %d6,(%sp)"); asm("jsr GC_push_one");
300   	  asm("mov.l %d7,(%sp)"); asm("jsr GC_push_one");
301 
302   	  asm("addq.w &0x4,%sp");	/* put stack back where it was	*/
303 #        endif /* !__GNUC__ */
304 #       endif /* M68K/SYSV */
305 
306 
307 #     if defined(HP_PA) || defined(M88K) || defined(POWERPC) || (defined(I386) && (defined(OS2) || defined(USE_GENERIC))) || defined(UTS4)
308 	/* Generic code                          */
309 	/* The idea is due to Parag Patel at HP. */
310 	/* We're not sure whether he would like  */
311 	/* to be he acknowledged for it or not.  */
312 	{
313 	    static jmp_buf regs;
314 	    register word * i = (word *) regs;
315 	    register ptr_t lim = (ptr_t)(regs) + (sizeof regs);
316 
317 	    /* Setjmp on Sun 3s doesn't clear all of the buffer.  */
318 	    /* That tends to preserve garbage.  Clear it.         */
319 		for (; (char *)i < lim; i++) {
320 		    *i = 0;
321 		}
322 #	    if defined(POWERPC) || defined(MSWIN32) || defined(UTS4)
323 		(void) setjmp(regs);
324 #	    else
325 	        (void) _setjmp(regs);
326 #	    endif
327 	    GC_push_all_stack((ptr_t)regs, lim);
328 	}
329 #     endif
330 
331       /* other machines... */
332 #       if !(defined M68K) && !(defined VAX) && !(defined RT)
333 #	if !(defined SPARC) && !(defined I386) && !(defined NS32K)
334 #	if !defined(HP_PA) && !defined(M88K) && !defined(POWERPC)
335 #	if !defined(UTS4)
336 	    --> bad news <--
337 # 	endif
338 #       endif
339 #       endif
340 #       endif
341 }
342 
343 /* On register window machines, we need a way to force registers into 	*/
344 /* the stack.	Return sp.						*/
345 # ifdef SPARC
346     asm("	.seg 	\"text\"");
347 #   ifdef SVR4
348       asm("	.globl	GC_save_regs_in_stack");
349       asm("GC_save_regs_in_stack:");
350       asm("	.type GC_save_regs_in_stack,#function");
351 #   else
352       asm("	.globl	_GC_save_regs_in_stack");
353       asm("_GC_save_regs_in_stack:");
354 #   endif
355     asm("	ta	0x3   ! ST_FLUSH_WINDOWS");
356     asm("	mov	%sp,%o0");
357     asm("	retl");
358     asm("	nop");
359 #   ifdef SVR4
360       asm("	.GC_save_regs_in_stack_end:");
361       asm("	.size GC_save_regs_in_stack,.GC_save_regs_in_stack_end-GC_save_regs_in_stack");
362 #   endif
363 #   ifdef LINT
GC_save_regs_in_stack()364 	word GC_save_regs_in_stack() { return(0 /* sp really */);}
365 #   endif
366 # endif
367 
368 
369 /* GC_clear_stack_inner(arg, limit) clears stack area up to limit and	*/
370 /* returns arg.  Stack clearing is crucial on SPARC, so we supply	*/
371 /* an assembly version that's more careful.  Assumes limit is hotter	*/
372 /* than sp, and limit is 8 byte aligned.				*/
373 #if defined(ASM_CLEAR_CODE) && !defined(THREADS)
374 #ifndef SPARC
375 	--> fix it
376 #endif
377 # ifdef SUNOS4
378     asm(".globl _GC_clear_stack_inner");
379     asm("_GC_clear_stack_inner:");
380 # else
381     asm(".globl GC_clear_stack_inner");
382     asm("GC_clear_stack_inner:");
383     asm(".type GC_save_regs_in_stack,#function");
384 # endif
385   asm("mov %sp,%o2");		/* Save sp	*/
386   asm("add %sp,-8,%o3");	/* p = sp-8	*/
387   asm("clr %g1");		/* [g0,g1] = 0	*/
388   asm("add %o1,-0x60,%sp");	/* Move sp out of the way,	*/
389   				/* so that traps still work.	*/
390   				/* Includes some extra words	*/
391   				/* so we can be sloppy below.	*/
392   asm("loop:");
393   asm("std %g0,[%o3]");		/* *(long long *)p = 0	*/
394   asm("cmp %o3,%o1");
395   asm("bgu loop	");		/* if (p > limit) goto loop	*/
396     asm("add %o3,-8,%o3");	/* p -= 8 (delay slot) */
397   asm("retl");
398     asm("mov %o2,%sp");		/* Restore sp., delay slot	*/
399   /* First argument = %o0 = return value */
400 #   ifdef SVR4
401       asm("	.GC_clear_stack_inner_end:");
402       asm("	.size GC_clear_stack_inner,.GC_clear_stack_inner_end-GC_clear_stack_inner");
403 #   endif
404 
405 # ifdef LINT
406     /*ARGSUSED*/
GC_clear_stack_inner(arg,limit)407     ptr_t GC_clear_stack_inner(arg, limit)
408     ptr_t arg; word limit;
409     { return(arg); }
410 # endif
411 #endif
412