Check for SYS/GL during library init. Reason is that
[AROS.git] / test / exec / stackswap.c
blobf162ee5eaed88fb03d9339c1f15b60353f03b11c
1 /*
2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <exec/tasks.h>
7 #include <proto/dos.h>
8 #include <proto/exec.h>
10 #define print Printf
12 #include <aros/debug.h>
13 #define print bug
15 #define STACK_SIZE 32768
17 #ifdef __MORPHOS__
18 #define StackSwapArgs PPCStackSwapArgs
19 #define NewStackSwap NewPPCStackSwap
20 #endif
22 struct StackSwapStruct sss;
24 int __nocommandline = 1;
26 void PrintSSS(struct StackSwapStruct *ss)
28 print("StackSwapStruct contents:\n");
29 print("Lower: 0x%P, Upper: 0x%P, SP: 0x%P\n", ss->stk_Lower, ss->stk_Upper, ss->stk_Pointer);
32 void PrintTaskStack(void)
34 struct Task *t = FindTask(NULL);
36 print("Current program stack:\n");
37 print("Lower: 0x%P, Upper: 0x%P, SP: 0x%P\n", t->tc_SPLower, t->tc_SPUpper, AROS_GET_SP);
40 void Sub(IPTR a1, IPTR a2)
42 print("Stack swap done\n");
43 PrintSSS(&sss);
44 PrintTaskStack();
45 print("Arguments: %08lX, %08lX\n", a1, a2);
46 print("Coming back to original stack...\n");
49 /* We keep quiet unless an error occurs to avoid context switches */
50 BOOL SilentSub(IPTR a1, IPTR a2)
52 BOOL success = TRUE;
53 APTR sp, spreg;
54 struct Task *t = FindTask(NULL);
56 if(a2 != (IPTR)t)
58 print("ERROR: second parameter has incorrect value!\n");
59 success = FALSE;
62 sp = AROS_GET_SP;
63 if(t->tc_SPReg < t->tc_SPLower || t->tc_SPReg > t->tc_SPUpper
64 || sp < t->tc_SPLower || sp > t->tc_SPUpper)
66 spreg = t->tc_SPReg;
67 print("ERROR: invalid stack values in user function:\n");
68 print("Lower: 0x%P\tUpper: 0x%P\nSP(struct): 0x%P\tSP(reg): 0x%P\n",
69 t->tc_SPLower, t->tc_SPUpper, spreg, sp);
70 success = FALSE;
73 return success;
76 int main(void)
78 struct StackSwapArgs args;
79 APTR sp, spreg;
80 struct Task *t = FindTask(NULL);
82 sss.stk_Lower = AllocMem(STACK_SIZE, MEMF_ANY);
83 if (!sss.stk_Lower)
85 print("Failed to allocate new stack!\n");
87 return 1;
90 sss.stk_Upper = sss.stk_Lower + STACK_SIZE;
91 sss.stk_Pointer = sss.stk_Upper - sizeof(IPTR);
92 PrintSSS(&sss);
93 PrintTaskStack();
95 print("Checking StackSwap()...\n");
96 StackSwap(&sss);
98 Sub(0x12345678, 0xC001C0DE);
100 StackSwap(&sss);
101 print("Came back from StackSwap()\n");
102 PrintSSS(&sss);
103 PrintTaskStack();
105 print("Checking NewStackSwap()...\n");
106 args.Args[0] = 0x1234ABCD;
107 args.Args[1] = 0xC0DEC001;
109 NewStackSwap(&sss, Sub, &args);
110 print("Came back from NewStackSwap()\n");
111 PrintSSS(&sss);
112 PrintTaskStack();
114 /* Check that stack fields in task structure make sense */
116 print("Checking NewStackSwap() again...\n");
117 args.Args[0] = 0x1234ABCD;
118 args.Args[1] = (IPTR)FindTask(NULL);
120 Forbid();
121 NewStackSwap(&sss, SilentSub, &args);
123 sp = AROS_GET_SP;
124 if(t->tc_SPReg < t->tc_SPLower || t->tc_SPReg > t->tc_SPUpper
125 || sp < t->tc_SPLower || sp > t->tc_SPUpper)
127 spreg = t->tc_SPReg;
128 print("ERROR: invalid stack values after return from user function:\n");
129 print("Lower: 0x%P\tUpper: 0x%P\nSP(struct): 0x%P\tSP(reg): 0x%P\n",
130 t->tc_SPLower, t->tc_SPUpper, spreg, sp);
132 Permit();
134 FreeMem(sss.stk_Lower, STACK_SIZE);
136 print("Done!\n");
137 return 0;