The Unix Heritage Society mailing list
 help / color / mirror / Atom feed
From: michael_davidson@pacbell.net (Michael Davidson)
Subject: [pups] V7 setjmp/longjmp
Date: Mon, 30 Dec 2002 18:23:14 -0800	[thread overview]
Message-ID: <3E10FF92.3080807@pacbell.net> (raw)
In-Reply-To: <ad59f854675cbdf4887859e421701e4f@coraid.com>

bwc at coraid.com wrote:

>Looking thru the code for setjmp/longjmp (/usr/src/libc/gen/setjmp.s),
>and have a question.  Longjmp seems to make an attempt at restoring
>register two through four, but setjmp doesn't save them.  Where do
>these register get saved from?
>
setjmp and longjmp are intimately familiar with the way in which
the standard stack frame for C functions is laid out

The stack frame itself is maintained by the functions csv and cret
which are defined in /usr/src/libc/crt/csv.s

The C compiler generates a call to csv at the start of each function:

    jsr    r5, csv

which has the net effect of saving the current value of r5 on the stack,
setting r5 to point to the saved r5, and saving r4, r3 and r2

and at the end of each function it generates:

    jmp    cret

which restores the values of r2 - r5 and returns to the function's caller.

Thus r5 is used as a frame pointer.

All that setjmp saves are the values of r5, sp and the return address.

When longjmp is called it walks back up the chain of frame pointers
pointed to by r5 until it gets to the frame immediately below the
one for the context it wants to return to. It then patches the return
address in that frame to be the return address saved by setjmp and
uses cret to restore everything (almost) as if setjmp had just returned.

Note that this means that r4, r3 and r2 get restored to the values
that they had at the time that the next stack frame was established
(ie when the "next" function down was called from the original context)
and that these are not necessarily the values that they had at the
moment when the original call to setjnp returned. Hence the various
caveats about not relying on the value of any register variables
after returning via longjmp.





  reply	other threads:[~2002-12-31  2:23 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-12-30 18:51 bwc
2002-12-31  2:23 ` Michael Davidson [this message]
2002-12-31 11:04 Wolfgang Helbig
2002-12-31 16:02 ` Michael Davidson
2003-03-23 12:32 Wolfgang Helbig
2003-03-25  5:14 Dennis Ritchie

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3E10FF92.3080807@pacbell.net \
    --to=michael_davidson@pacbell.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).