Skip to content

Trap inline asm is broken if frame pointer isn't used #8

@fifteenhex

Description

@fifteenhex

I'm working to fix this so this is just a placeholder with the info.

I noticed while refactoring the build to use meson that the scsi code stopped working.
I finally worked out what it is:

If I modify one of the functions to push more arguments on the stack:

static inline OSErr SCSIRead(const void *tibPtr)
{
	register OSErr ret asm("%%d0");

	asm volatile("clr.w -(%%sp)\n"
				 "move.l %1, -(%%sp)\n"
				 
				 "move.l %1, -(%%sp)\n"
				 "move.l %1, -(%%sp)\n"
				 "move.l %1, -(%%sp)\n"
				 "move.l %1, -(%%sp)\n"
				 SCSIDispatch(_SCSIRead)
				 : "=d" (ret) : "g" (tibPtr) : UNPRESERVED_REGS );

	return ret;
}

We can see with a frame pointer the generated asm looks like:

0001070c <SCSIRead>:
   1070c:       2f0e            movel %fp,%sp@-
   1070e:       2c4f            moveal %sp,%fp
   10710:       2f0a            movel %a2,%sp@-
   10712:       2f02            movel %d2,%sp@-
   10714:       4267            clrw %sp@-
   10716:       2f2e 0008       movel %fp@(8),%sp@-
   1071a:       2f2e 0008       movel %fp@(8),%sp@-
   1071e:       2f2e 0008       movel %fp@(8),%sp@-
   10722:       2f2e 0008       movel %fp@(8),%sp@-
   10726:       2f2e 0008       movel %fp@(8),%sp@-
   1072a:       3f3c 0005       movew #5,%sp@-
   1072e:       a815            .short 0xa815
   10730:       301f            movew %sp@+,%d0
   10732:       48c0            extl %d0
   10734:       241f            movel %sp@+,%d2
   10736:       245f            moveal %sp@+,%a2
   10738:       4e5e            unlk %fp
   1073a:       4e75            rts

Everything is fine because the generated asm is pushing tibPtr on the stack using the frame pointer with offset.

But if we disable the frame pointer we get:

    c03e:       4267            clrw %sp@-
    c040:       2f2f 002c       movel %sp@(44),%sp@-
    c044:       2f2f 002c       movel %sp@(44),%sp@-
    c048:       2f2f 002c       movel %sp@(44),%sp@-
    c04c:       2f2f 002c       movel %sp@(44),%sp@-
    c050:       2f2f 002c       movel %sp@(44),%sp@-
    c054:       3f3c 0005       movew #5,%sp@-
    c058:       a815            .short 0xa815

This is obviously no good because we are pushing relative to the stack pointer with the same offset but the stack pointer is changing so the offset should also be changing.

The best fix as far as I know is to change the constraint the stacked arguments for the trap to a register so that code is generated to put them into registers first and then the inline asm can put them on the stack without using the stack pointer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions