Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / libs / reqtools / checkstack.asm
blobf440e0e6b3fecac56e06f7ec2dd975550df6972d
2 SECTION 'text',CODE
4 INCLUDE "exec/funcdef.i"
5 INCLUDE "exec/exec_lib.i"
6 INCLUDE "exec/tasks.i"
7 INCLUDE "dos/dosextens.i"
8 INCLUDE "libraries/reqtools.i"
9 INCLUDE "lvo/reqtools_lib.i"
11 * This function will make sure there are at least 4096 bytes free on
12 * the stack. If this is not the case it will allocate a new stack and
13 * use this for your function.
15 * Usage of CheckStackCallFunc():
16 * Push your real functions' address on the stack.
17 * Call CheckStackCallFunc()
18 * (note that you CANNOT pass arguments on the stack to your function
19 * because it is possible your function will run with a newly allocated
20 * _clear_ stack!)
22 ThisTask equ $114
24 XDEF _CheckStackCallFunc
26 _CheckStackCallFunc:
27 movem.l d0-d7/a0-a6,-(a7) ; push all regs
28 move.l $4.w,a6
29 move.l ThisTask(a6),a3
30 move.l #4096,d7
31 ; move.l pr_CLI(a3),d0
32 ; beq.b fromwb
33 ; lsl.l #2,d0
34 ; move.l d0,a0
35 ; move.l cli_DefaultStack(a0),d1
36 ; lsl.l #2,d1
37 ; bra.b dostack
38 ;fromwb:
40 cmp.l TC_SPUPPER(a3),a7
41 bcc.b swapstack
43 cmp.l TC_SPLOWER(a3),a7
44 bcs.b swapstack
46 move.l a7,d1
47 sub.l TC_SPLOWER(a3),d1
48 dostack:
49 cmp.l d7,d1
50 bcc.b okstack
52 swapstack:
53 moveq #StackSwapStruct_SIZEOF,d0
54 add.l d7,d0 ; room for stackswap struct
55 move.l #$10000,d1
56 jsr _LVOAllocVec(a6)
57 tst.l d0
58 beq.b nostack
60 move.l d0,a4
61 add.l d7,a4
63 move.l d0,stk_Lower(a4) ; lower bound
64 move.l a4,stk_Upper(a4) ; upper bound
66 move.l a4,a2
68 move.l d0,-(a2) ; push address of new stack on new stack
69 lea returnaddr2(PC),a0
70 move.l a0,-(a2) ; push returnaddr2 on new stack
72 move.l 60+4(a7),d0
73 move.l d0,-(a2) ; push function address on new stack
75 lea 60(a7),a1 ; copy over regs from old to new stack
76 moveq #14,d0
77 .copyregs:
78 move.l -(a1),-(a2)
79 dbra d0,.copyregs
81 move.l a2,stk_Pointer(a4) ; current stack pointer
82 move.l a4,a0
83 jsr _LVOStackSwap(a6)
85 movem.l (a7)+,d0-d7/a0-a6
86 rts ; call function, return to returnaddr2
88 returnaddr2:
89 move.l (a7)+,a4 ; get address of new stack
90 move.l d0,d7
91 move.l a4,a0
92 add.l #4096,a0
93 move.l $4.w,a6
94 jsr _LVOStackSwap(a6) ; swap back old stack
95 move.l a4,a1
96 jsr _LVOFreeVec(a6)
97 move.l d7,d0
98 addq.l #4,a7 ; purge old d0
99 movem.l (a7)+,d1-d7/a0-a6
100 bra.b endcheckstack
102 okstack:
103 movem.l (a7)+,d0-d7/a0-a6
104 pea endcheckstack(PC)
105 move.l 8(a7),-(a7) ; push function's address
106 rts ; call function
108 nostack:
109 movem.l (a7)+,d0-d7/a0-a6
110 moveq #0,d0
111 endcheckstack:
112 move.l (a7),4(a7)
113 addq.l #4,a7