2 Copyright © 1995-2008, The AROS Development Team. All rights reserved.
5 Desc: RunProcess() - Run a process from an entry point with args
8 #include <aros/asmcall.h> /* LONG_FUNC */
10 # include <dos/dosextens.h>
11 # include <proto/exec.h>
12 # include <aros/debug.h>
14 # include <exec/types.h>
15 # include <exec/tasks.h>
16 # define D(x) /* eps */
22 struct ExecBase
* dl_SysBase
;
25 extern void StackSwap (struct StackSwapStruct
*, struct ExecBase
*);
27 #define StackSwap(s) StackSwap(s, SysBase)
28 #define AROS_SLIB_ENTRY(a,b) a
35 /**************************************************************************
38 LONG
AROS_SLIB_ENTRY(RunProcess
,Dos
) (
41 struct Process
* proc
,
42 struct StackSwapStruct
* sss
,
46 struct DosLibrary
* DOSBase
)
49 Sets the stack as specified and calls the routine with the given
53 proc - Process context
55 argptr - Pointer to argument string
56 argsize - Size of the argument string
57 entry - The entry point of the function
58 DOSBase - Pointer to dos.library structure
61 The return value of (*entry)();
73 **************************************************************************/
83 sp
= (APTR
*)(sss
->stk_Upper
);
84 oldSP
= (APTR
*)&DOSBase
;
85 oldReturnAddr
= proc
->pr_ReturnAddr
;
87 /* Compute argsize automatically */
90 argsize
= strlen(argptr
);
93 /* Copy stack + locals + regs + everything */
94 while ( oldSP
!= (APTR
*)&ret
)
99 sss
->stk_Pointer
= sp
;
101 D(bug("In RunProcess() entry=%lx, *entry=%lx\n", (IPTR
)entry
, (IPTR
)*entry
));
104 /* Call the function with the new stack */
106 We have to set the pr_ReturnAddr pointer to the correct value
107 before we call the entry() otherwise some startup code will
110 This can be done rather more easily on the m68k than elsewhere.
113 #error You need to write the AROS_UFC3R macro for your CPU
116 /* The AROS_UFC3R() macro doesn't work on my system (gcc 2.95.1, Linux 2.3.50)
117 * this is the workaround I'm currently using:
120 // *retptr = entry(argptr,argsize,SysBase);
122 *retptr
= AROS_UFC3R(ULONG
, entry
,
123 AROS_UFCA(CONST_STRPTR
, argptr
, A0
),
124 AROS_UFCA(ULONG
, argsize
, D0
),
125 AROS_UFCA(struct ExecBase
*, SysBase
, A6
),
126 &proc
->pr_ReturnAddr
, (sss
->stk_Upper
- (ULONG
)sss
->stk_Lower
)
131 proc
->pr_ReturnAddr
= oldReturnAddr
;
140 ULONG teststack
[4096];
142 int DemoProc (const char * argstr
, int argsize
, struct ExecBase
* SysBase
)
144 printf ("arg=\"%s\" (len=%d\n", argstr
, argsize
);
149 int main (int argc
, char ** argv
)
153 struct StackSwapStruct sss
;
154 struct DosLibrary DosBase
;
156 sss
.stk_Lower
= teststack
;
157 sss
.stk_Upper
= &teststack
[sizeof(teststack
) / sizeof(teststack
[0])];
158 sss
.stk_Pointer
= sss
.stk_Upper
;
160 DosBase
.dl_SysBase
= (struct ExecBase
*)0x0bad0bad;
162 printf ("Stack=%p\n", &ret
);
164 argstr
= "Hello world.";
166 len
= strlen (argstr
);
168 ret
= RunProcess (NULL
,
176 printf ("Stack=%p\n", &ret
);
177 printf ("RunProcess=%d\n",ret
);
181 printf("Test ok.\n");
185 printf("Test failed.\n");