use of setcontext()/getcontext()
Hello list,
I wonder if the above function set can be used to implement something
like what is used to work on other types of machines with setjmp()/longjmp(). I would like to continue a program at a certain point of execution with a saved environment. I achieved this by now by the following algorithm and want to ask if there is any better way.
static __cont_done = 0;
make_continuation {
__cont_done = 0
getcontext(ctx)
if (__cont_done) goto __continuation save_both_stacks() // register and normal stack in heap return
__continuation:
return
}
run_continuation {
restore_both_stacks() // register and normal stack from heap
__cont_done = 1
setcontext(ctx)
}I do not like the static __cont_done in the piece of code. But I do not know how to determine if the instruction behind the getcontext() call is due to a normal call to make_continuation() or due to the setcontext() of run_continuation(). The setjmp() does return something appropiate but I do not know how to do it with setcontext()/getcontext().
Thanks in advance,
[email protected]
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · stefan
Stefan> Hello list, I wonder if the above function set can be used Stefan> to implement something like what is used to work on other Stefan> types of machines with setjmp()/longjmp(). I would like to Stefan> continue a program at a certain point of execution with a Stefan> saved environment.
From this description, it sounds like you'd want to use swapcontext(). I don't quite understand your pseudo-code though (what's are save_both_stacks()/restore_both_stacks() supposed to do?).
I do agree though that it would be nice to have a portable and thread-safe way of determining whether or not a getcontext() is returning for the first time. On ia64, I made getcontext() return this information in register r9, but that's obviously not a portable solution.
--david
Quoted 9 lines · David Mosberger
Quoted 1 line
From this description, it sounds like you'd want to use
swapcontext().I don't quite understand your pseudo-code though (what's are save_both_stacks()/restore_both_stacks() supposed to do?).
I do agree though that it would be nice to have a portable and thread-safe way of determining whether or not a
getcontext()is returning for the first time. On ia64, I madegetcontext()return this information in register r9, but that's obviously not a portable solution.
Some more details about the pseudo code:
save_both_stacks:
ucontext_t ctx;
long size;
void *backing_store, *stack;
getcontext (&ctx);
/* save backing store */
size = ctx.uc_mcontext.sc_ar_bsp -
__libc_ia64_resgister_backing_store_base;
backing_store = malloc (size);
memcpy (backing_store, __libc_ia64_register_backing_store_base, size);
/* save stack */
size = top_of_stack - (long) &ctx;
stack = malloc (size);
memcpy (stack, &ctx, size);restore_both_stacks:
/* does the opposite; puts the save data back onto same addresses */
Does this explain it better ?
I guess I can't just read r9 for the above purpose ?
Cheers,
[email protected]
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · stefan
stefan> Some more details about the pseudo code:
stefan> [snip]
stefan> Does this explain it better ?
Yes, I understand now what you're trying to do.
stefan> I guess I can't just read r9 for the above purpose ?
I suppose you could. Since the solution will be platform-dependent no matter how you slice it, you might just as well rely on the ia64 linux specific behavior of getcontext(). Below is a code fragment that shows how swapcontext() can be implemented on ia64 linux. You can use the same approach for your case.
--david
struct rv
{
long retval;
long first_return;};
int
swapcontext (ucontext_t *oucp, const ucontext_t *ucp) {
struct rv rv = getcontext (oucp);
if (rv.first_return)
setcontext (ucp);
return 0;
}
Quoted 6 lines · David Mosberger
struct rv
{ long retval; long first_return;};
Quoted 8 lines
int
swapcontext (ucontext_t*oucp, constucontext_t*ucp) {
struct rv rv = getcontext (oucp);
if (rv.first_return)setcontext (ucp);return 0;
}
By including <sys/ucontext.h> and declaring getcontext() and setcontext() by hand it was possible to port the upcoming GNU Guile release 1.6 to ia64-linux. Thank you very much for your help !
Thanks,
[email protected]
Quoted 8 lines · David Mosberger
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · stefan
stefan> By including <
sys/ucontext.h> and declaringgetcontext()and stefan>setcontext()by hand it was possible to port the upcoming stefan> GNU Guile release 1.6 to ia64-linux. Thank you very much for stefan> your help !Excellent! Thanks for the good work!
One more question about this: The `ucontext_t' structure seemed to changed a bit over the weeks. Since when is it known to have the sc_ar_bsp member (glibc version) ? Also I would like to know if
extern unsigned long __libc_ia64_register_backing_store_base; or
extern void * __libc_ia64_register_backing_store_base;
is meant to be correct.
Thanks in advance,
[email protected]
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · stefan
Stefan> One more question about this: The `ucontext_t' structure Stefan> seemed to changed a bit over the weeks. Since when is it Stefan> known to have the sc_ar_bsp member (glibc version) ?
I thought that member was there pretty much "forever". However, I did add sc_rbs_base and sc_loadrs recently. This is really for sigcontext, but since ucontext_t is effectively an alias, it would be affected, too.
Stefan> Also I would like to know if
Stefan> extern unsigned long
Stefan> __libc_ia64_register_backing_store_base; or extern void * Stefan> __libc_ia64_register_backing_store_base;
Stefan> is meant to be correct.
Either one should work.
--david
Quoted 9 lines · David Mosberger
Stefan> Also I would like to know if
Stefan> extern unsigned long
Stefan>__libc_ia64_register_backing_store_base; or extern void * Stefan>__libc_ia64_register_backing_store_base;Stefan> is meant to be correct.
Either one should work.
I know that :-) But how is it initially declared ? I just need to decide which to chose.
Thanks in advance,
[email protected]
Quoted 10 lines · David Mosberger
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · stefan
Stefan> One more question about this: The `ucontext_t' structure Stefan> seemed to changed a bit over the weeks. Since when is it Stefan> known to have the
sc_ar_bspmember (glibc version) ?I thought that member was there pretty much "forever". However, I did add
sc_rbs_baseandsc_loadrsrecently. This is really for sigcontext, but sinceucontext_tis effectively an alias, it would be affected, too.
I found some ia64 machine which declares mcontext_t' in <sys/ucontext.h> in another way as sigcontext' in <bits/sigcontext.h>. That is dated on May 22nd this year. With members like ar_bsp_base' and ar_bspstore'... That is why I ask.
Thanks in advance,
[email protected]
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · stefan
Stefan> On Wed, 10 Oct 2001, David Mosberger wrote: Also I would Stefan> like to know if
Quoted 1 line
Quoted 1 line
Stefan> extern unsigned long
Stefan> __libc_ia64_register_backing_store_base; or extern void * Stefan> __libc_ia64_register_backing_store_base;
Quoted 1 line
Quoted 1 line
Stefan> is meant to be correct.
Quoted 1 line
Quoted 1 line
Either one should work.
Stefan> I know that :-) But how is it initially declared ? I just Stefan> need to decide which to chose.
Well, it's defined in an assembly file, so there is no initial declaration. I'd recommend to declare it as "unsigned long *". That way, it would be compatible with the rse.h helper functions (not that you'd need them... ;-).
--david
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · stefan
Stefan> I found some ia64 machine which declares mcontext_t' in Stefan> <sys/ucontext.h> in another way as sigcontext' in Stefan> <bits/sigcontext.h>. That is dated on May 22nd this Stefan> year. With members like ar_bsp_base' and ar_bspstore'... Stefan> That is why I ask.
Oh, right. I had forgotten about that. Any version of glibc that comes with setcontext()/getcontext() should have a correct ucontext.h, but we didn't implement those routines until quite late in the game. If I recall correctly, they were first supported by glibc-2.2.3, but there were some nasty bugs, so I think you pretty much have to use glibc-2.2.4 or newer.
--david