From mboxrd@z Thu Jan 1 00:00:00 1970 From: erik quanstrom Date: Mon, 7 Jan 2013 05:31:44 -0500 To: 9fans@9fans.net Message-ID: In-Reply-To: <1752877.EVzC7FFEmr@krypton> References: <1752877.EVzC7FFEmr@krypton> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit Subject: Re: [9fans] a jmp_buf in APE question Topicbox-Message-UUID: 0234f6a6-ead8-11e9-9d60-3106f5b1d025 On Mon Jan 7 00:47:14 EST 2013, staal1978@gmail.com wrote: > Hi all. Perhaps I have been looking at completely the wrong places and perhaps > I am just not grasping it at all. > > For a package that I want to build under APE, I need to put in the stack > pointer (sp) and the program counter (pc) part of jmp_buf specific for Plan9 > (since APE does not expose any stack-related functions that could have been > used) > > according to /sys/include/ape/setjmp.h, jmp_buf is an array of 10 int:s. > > I then went to check out /sys/src/ape/lib/ap/$objtype/setjmp.s > but I could not really understand anything in those files as you point out, the interface is opaque. but if you look under the covers, PC and SP are stored in first two "entries" of the jmp_buf blob. obviously PC and SP are not properly ints, but rather uintptrs, so the absolute offset will depend on the pointer size (0 and 4 for 386). a better definition of jmp_buf might be typedef uchar jmp_buf[4 * sizeof uintptr]; note, a structure will not do, since a structure would be passed in by value and therefore any values set would disappear on return from setjmp. for amd64 the setjmp code is (comments changed assuming that jmp_buf is an array of uintptrs) TEXT setjmp(SB), $0 /* RARG is set to &jmp_buf[0] */ MOVQ SP, 0(RARG) /* store sp in RARG offset 0; jmp_buf[0] = SP */ MOVQ 0(SP), BX /* return pc */ MOVQ BX, 8(RARG) /* stored at offset 8; jmp_buf[1] = PC */ MOVL $0, AX /* return 0 */ RET note that in this case the absolute offsets are 0 and 8, since pointers on amd64 take 8 bytes. > The second question would be, is the position of those two values in jmp_buf > architecture-specific so that I should make a note of that it is working on > i386 only? the structure within jmp_buf is entirely determined by the code in setjmp.s i don't think it would be wise to rely on the structure of jmp_buf. it's not exposing any structure, so it seems unwise to assume it. the typical way around this would be to create $objtype.s for each $objtype you wish to support and store the PC and SP as you like them. i think what you want is enum { Spidx, Pcidx, Nregsave, }; typedef uintptr myjmpbuf[Nregsave]; then it should be a simple matter of reappropriating the code from setjmp. - erik