9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Russ Cox <russcox@gmail.com>
To: Fans of the OS Plan 9 from Bell Labs <9fans@cse.psu.edu>
Subject: Re: [9fans] Questions about libthread and setjmp
Date: Mon, 21 Feb 2005 10:41:06 -0500	[thread overview]
Message-ID: <ee9e417a05022107416d03ae9c@mail.gmail.com> (raw)
In-Reply-To: <afb1cac24a379c0512ee89a9058fcc50@plan9.escet.urjc.es>

>         t->sched[JMPBUFSP] = (ulong)tos - 8;            /* old PC and new PC */

Taking this as a concrete example, the thread library is
trying to make it look like launcher386(f, arg) called setjmp
at the very beginning of its execution.  If it had, the stack
would look like:

    arg
    f
    pc of caller of launcher386 ("old pc")
    pc of caller of setjmp (== launcher386, "new pc")

and the saved SP is the one pointing at the bottom of this stack.

When longjmp comes along, it restores SP to the same place,
writes the jmp_buf PC over the slot marked "pc of caller of setjmp"
(in normal setjmp/longjmp use that memory word is no longer valid)
and executes a return instruction, ending up at the beginning of
launcher386 with f, arg as the arguments on the stack.

Normal setjmp-produced stacks look a little different, because
there is a pointer to the jump buffer, and perhaps then a stack frame
full of local variables, between the two pcs.  But here we are not
returning to a function that has called setjmp.  We are "returning"
to the beginning of a function, so we want the stack to look like
it does on entry, not like it does in the middle.

Russ


      reply	other threads:[~2005-02-21 15:41 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-02-21 12:15 Gorka Guardiola
2005-02-21 15:41 ` Russ Cox [this message]

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=ee9e417a05022107416d03ae9c@mail.gmail.com \
    --to=russcox@gmail.com \
    --cc=9fans@cse.psu.edu \
    /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).