Skip to contentSkip to conversation

getcontext() correctly implemented ?

Linux-ia64 stefan 4 participants 7 messages Sep 20, 2001 – Sep 21, 2001

lore
Share & tools
lore

stefan

author
#0 lore raw

Hello list,

in order to save the full register set of the ia64 I want to use getcontext(&ctx) instead of setjmp(jmpbuf) and check the registers in ctx.uc_mcontext for references in a garbage collected system.

My question is: Is getcontext() correctly implemented in glibc(). Which version do I need and can I use it for the described purpose at all ?

Thanks in advance,
[email protected]

#1 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · stefan

Stefan> Hello list, in order to save the full register set of the Stefan> ia64 I want to use getcontext(&ctx) instead of Stefan> setjmp(jmpbuf) and check the registers in ctx.uc_mcontext Stefan> for references in a garbage collected system.

The *context() routines are designed to support synchronous context switches only (see glibc FAQ). Thus, they basically save the "preserved" state only.

Stefan> My question is: Is getcontext() correctly implemented in Stefan> glibc(). Which version do I need and can I use it for the Stefan> described purpose at all ?

It's implemented to meet the requirements of SuS. In that sense, it should be correct (you'll probably need glibc 2.2.4 as there were some silly bugs in earlier versions).

To get the full machine context, in a signal handler, you'd have to either (a) use stack unwinding to recover the preserved state at the point of the signal or (b) use a combination of sigcontext and setjmp() or getcontext(). Can say a little more about what you're trying to do? That would make it easier to give a specific example.

To accomplish (a), we really need a user-level unwinder. It's been on the todo list for some time, but nobody got around to implement it, so far.

	--david
author
#2 lore raw
In reply to stefan
Quoted 9 lines · David Mosberger

To get the full machine context, in a signal handler, you'd have to either (a) use stack unwinding to recover the preserved state at the point of the signal or (b) use a combination of sigcontext and setjmp() or getcontext(). Can say a little more about what you're trying to do? That would make it easier to give a specific example.

To accomplish (a), we really need a user-level unwinder. It's been on the todo list for some time, but nobody got around to implement it, so far.

The garbage collector of GNU Guile does not need the machine context in a signal handler. It just looks for heap references within the programs stack. The top of the stack is saved when starting the program, the bottom is gained from the current stack position when garbage collecting. More references might be within the machines registers. That is why it uses setjmp() in order to get these into a defined buffer. On a ia64 Linux setjump() is not said to save *all* registers. That is why I thought the getcontext() function could help here. Is that true ? I already tried it with glibc 2.2.3 without success. Still missed some references...

Thanks in advance,
[email protected]

#3 lore raw
In reply to stefan
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line · stefan

Stefan> The garbage collector of GNU Guile does not need the machine Stefan> context in a signal handler. It just looks for heap Stefan> references within the programs stack. The top of the stack Stefan> is saved when starting the program, the bottom is gained Stefan> from the current stack position when garbage Stefan> collecting. More references might be within the machines Stefan> registers.

You didn't mention the register backing store. Be sure to scan that one too! For the main thread, libc exports a global variable which will help you find the beginning of the backing store. The variable is called "__libc_ia64_register_backing_store_base". I'm not sure what pthreads does (Hans can you help?). The top of the register backing store can be obtained from the "sc_bsp" member in the ucontext (uc->uc_mcontext.sc_bsp).

Stefan> That is why it uses setjmp() in order to get these into a Stefan> defined buffer. On a ia64 Linux setjump() is not said to Stefan> save *all* registers. That is why I thought the getcontext() Stefan> function could help here. Is that true ? I already tried it Stefan> with glibc 2.2.3 without success. Still missed some Stefan> references...

Do you need *all* registers or *all live* registers? If it's the latter and if you're willing to make a function call to obtain this state, then getcontext() should do all you need: the scratch state gets killed by the call to getcontext() and hence the "preserved" registers that it saves is really all you need.

For your particular case, setjmp() would be an option, too. The major difference between setjmp() and getcontext() is that the latter flushes the register stack, whereas the former does not (but you could do this on your own later on). Overall, I'd still recommend using getcontext() though. It's a little safer for two reasons:

(a) We go try hard to avoid changing the layout of "struct sigcontext" (and ucontext_t by implication). For the jump-buffer, we really only try to avoid changing its size; the internal structure could easily change.

(b) getcontext() is defined to be used for synchronous context switches, so you can be certain that it saves all "preserved" state.

Hope this helps.

	--david
#4 lore raw
In reply to stefan
Quoted 12 lines · stefan

The garbage collector of GNU Guile does not need the machine context in a signal handler. It just looks for heap references within the programs stack. The top of the stack is saved when starting the program, the bottom is gained from the current stack position when garbage collecting. More references might be within the machines registers. That is why it uses setjmp() in order to get these into a defined buffer. On a ia64 Linux setjump() is not said to save *all* registers. That is why I thought the getcontext() function could help here. Is that true ? I already tried it with glibc 2.2.3 without success. Still missed some references...

I can't help thinking you'd be best looking at Hans Boehm's garbage collector and the way it accesses registers on IA-64. boehm-gc is in a subdir of the gcc soures.

Andrew.

author
#5 lore raw
In reply to stefan
Quoted 7 lines · David Mosberger

You didn't mention the register backing store. Be sure to scan that one too! For the main thread, libc exports a global variable which will help you find the beginning of the backing store. The variable is called "__libc_ia64_register_backing_store_base". I'm not sure what pthreads does (Hans can you help?). The top of the register backing store can be obtained from the "sc_bsp" member in the ucontext (uc->uc_mcontext.sc_bsp).

Wow ! This fixed this specific problem. With a little change:
uc->uc_mcontext.sc_bsp is uc->uc_mcontext.sc_ar_bsp.

Thanks a lot,
[email protected]

#6 lore raw
In reply to stefan

In the threads case, I intercept thread creation, and thus capture the backing store pointer on thread startup. For the main thread, I use the variable if its available (glibc 2.2.4+ ?), and a rather brittle heuristic (which happens to work on current kernels) if it's not.

Hans

Quoted 31 lines

-----Original Message-----
From: stefan [mailto:[email protected]]
Sent: Friday, September 21, 2001 7:12 AM
To: David Mosberger
Cc: [email protected]
Subject: Re: [Linux-ia64] getcontext() correctly implemented ?

Quoted 6 lines · David Mosberger

You didn't mention the register backing store. Be sure to scan that one too! For the main thread, libc exports a global variable which will help you find the beginning of the backing store. The variable is called "__libc_ia64_register_backing_store_base". I'm not sure what pthreads does (Hans can you help?). The top of the register backing store can be obtained from the "sc_bsp" member in

the ucontext

Quoted 1 line

(uc->uc_mcontext.sc_bsp).

Wow ! This fixed this specific problem. With a little change:
uc->uc_mcontext.sc_bsp is uc->uc_mcontext.sc_ar_bsp.

Thanks a lot,
[email protected]

_______________________________________________
Linux-IA64 mailing list
[email protected]
http://lists.linuxia64.org/lists/listinfo/linux-ia64