6 ; Generic stack extension routines.
8 ; (C) Janne Jalkanen 1998
9 ; (C) Copyright 1998 Manuel Lemos.
10 ; (C) Copyright 1996-1997 Ian J. Einman.
11 ; (C) Copyright 1993-1996 Jaba Development.
12 ; (C) Copyright 1993-1996 Jan van den Baard.
13 ; All Rights Reserved.
16 ; Revision 42.0 2000/05/09 22:10:18 mlemos
17 ; Bumped to revision 42.0 before handing BGUI to AROS team
19 ; Revision 41.11 2000/05/09 20:21:53 mlemos
20 ; Bumped to revision 41.11
22 ; Revision 41.1 2000/05/09 19:55:11 mlemos
23 ; Merged with the branch Manuel_Lemos_fixes.
25 ; Revision 1.1.2.5 1998/10/12 01:31:17 mlemos
26 ; Fixed a remaining make-proto word in the previous log.
27 ; Changed the make-proto declaration for EnsureStack function.
29 ; Revision 1.1.2.4 1998/10/11 15:39:35 mlemos
30 ; Split make-proto words in the log comment to not confuse make-proto program.
31 ; Added make-proto declarations for EnsureStack and RevertStack to let them
32 ; be called from C code.
34 ; Revision 1.1.2.3 1998/10/01 23:03:12 mlemos
35 ; Replaced a C like comment that was confusing make-proto by a semi-colon.
36 ; Defined functions exported to C by make-proto as regargs.
38 ; Revision 1.1.2.2 1998/10/01 04:37:01 mlemos
39 ; Made the calls to exec.library be done by fetching the library base pointer
41 ; Increased the stack margin size to 2048 bytes.
42 ; Added code to pre-allocate and free the stack space for use by the
44 ; Removed the stack space copying code.
45 ; Fixed the bug of post incrementing the register a5 after restoring pushed
46 ; register from the previous stack space.
48 ; Revision 1.1.2.1 1998/09/12 02:21:22 mlemos
52 INCLUDE 'exec/exec_lib.i'
53 INCLUDE 'exec/tasks.i'
54 INCLUDE 'exec/memory.i'
56 XDEF _EnsureStack
,_RevertStack
59 STKMARGIN
EQU 2048 ; When should we start worrying about the stack?
60 STKSIZE
EQU 16384 ; Allocate this much stack space
61 CTLBLOCK
EQU 4*4 ; The control block, currently sizeof(StackSwapStruct)
64 move.l _SysBase
(a4
),a6
74 *******************************************************************************
76 * EnsureStack allocates a handle
, which must be returned back to RevertStack
78 * Registers
preserved: all
, except d0
and sp
83 ;makeproto ASM APTR EnsureStack(VOID);
86 movem.l d1
-d7
/a0
-a6
,-(sp) ; total of 14 regs = 56 bytes
88 ; Figure out our address
91 callsys FindTask
; a2=FindTask(NULL)
94 ; Calculate current stack left and check if it is within
97 move.l TC_SPLOWER
(a2
),d2
; d2 = oldlower
99 sub.l d2
,d3
; d3 = stkptr-oldlower
101 moveq #
0,d0
; d0 = NULL ; THis is the default return value
105 ; Check if this is input.device
107 cmpa.l _InputDevice
(a4
),a2
110 ; if current stack space is already the pre-allocated stack, allocate a stack extension
112 move.l _InputStack
(a4
),d0
118 ; Allocate the new stack and the control block (20 bytes extra)
121 move.l #STKSIZE
+CTLBLOCK
,d0
122 move.l #MEMF_CLEAR
+MEMF_PUBLIC
,d1
127 ; Allocation was successfull, so we'll fill out the necessary data
128 ; in the control block.
131 move.l d0
,a3
; a3 = control block address
133 move.l d0
,stk_Lower
(a3
) ; start of real stack (ctrl block not included)
136 move.l d0
,stk_Upper
(a3
) ; end of stack
138 move.l TC_SPUPPER
(a2
),d1
139 sub.l
sp,d1
; d1 = stacksize = tc_SPUpper - stkptr
141 sub.l d1
,d0
; d0 = stk_Upper - stackoffset
142 move.l d0
,stk_Pointer
(a3
)
144 ; Copy the return address.
147 move.l
56(a7
),(a1
) ; Copy return address to new stack
149 ; Finally, do StackSwap()
150 move.l
sp,a5
; Old stack pointer so that we can easily
151 ; restore registers on exit
155 move.l a3
,d0
; d0 = return value
157 movem.l
(a5
),d1
-d7
/a0
-a6
; Restore registers and return
165 movem.l
(sp)+,d1
-d7
/a0
-a6
168 *******************************************************************************
170 * This function returns the stack back to free memory pool.
171 * Safe to
call with NULL args
173 * Registers
destroyed: d0
/a0
/a1
/a7
175 ;makeproto ASM VOID RevertStack( REG(a0) APTR);
182 movem.l d1
-d7
/a2
-a6
,-(sp) ; 12 registers = 48 bytes
184 ; Revert to original stack
185 move.l d0
,a3
; a3 = control block
190 move.l stk_Pointer
(a3
),a5
; a5 = our allocated stack pointer (now old)
191 move.l
48(a5
),a0
; a0 = return address
193 lea 48+12(sp),sp ; forget all previous stuff
194 move.l a0
,-(sp) ; push real return address
196 move.l a3
,a1
; a1 = memory block area
198 cmp.l _InputStack
(a4
),a1
201 callsys FreeVec
; free memory in a1
203 movem.l
(a5
),d1
-d7
/a2
-a6
; Return real registers
207 ********************************************************************************
209 * This function allocates memory for the input.device stack extension
,
210 * which is allocated only during library initialization.
212 ;makeproto VOID InitInputStack(VOID);
220 ; Find input.device address
224 move.l d0
,_InputDevice
(a4
) ; Save to globals.
227 move.l #STKSIZE
+CTLBLOCK
,d0
228 move.l #MEMF_CLEAR
+MEMF_PUBLIC
,d1
231 move.l d0
,_InputStack
(a4
)
236 ;makeproto VOID FreeInputStack(VOID);
241 move.l _InputStack
(a4
),a1
242 callsys FreeVec
; NULL is safe, says autodoc
248 dc.b
"input.device",0