WarpUp (27/55)

From:Frank Wille
Date:16 Jul 2000 at 18:21:15
Subject:Re: Run68k yet a trouble

Almos Rajnai wrote:

> Since my last letter I could not solve the trouble with Run68k
> function of WarpOS. It always calls some impossible addresses instead
> of mine. Here is my source:
>
> ------------test68k.asm---------------
> [...]
> backcall:
> moveq.l #36,d0
> rts
>
> _PowerPCBase: dc.l 0
> dosbase: dc.l 0
> num: dc.l 0
>
> format: dc.l "%lx\n",0
> powerpcname: dc.b "powerpc.library",0
> dosname: dc.b "dos.library",0

The 68k-part is ok, although I would place the variables you
want to access from the PPC-side, like _PowerPCBase, into the
TOC section:

section ".tocd",data

dcb.b 32,0 ; buffer against cache problems
_PowerPCBase:
dc.l 0

By doing so you can access _PowerPCBase via A4 on the 68k-side
and directly via R2 (rtoc) on the PPC-side.

> -------------testppc.asm-------------------------
> [...]
> ppcroutine:
> .set PP_CODE,0
> .set PP_OFFSET,4
> .set PP_FLAGS,8
> .set PP_STACKPTR,12
> .set PP_STACKSIZE,16
> .set PP_REGS,20
> .set PP_FREGS,20+15*4
> .set PP_SIZE,20+15*4+8*8
>
> subi r1,r1,PP_SIZE
> stw _d0,PP_REGS(r1)
> stw _d1,PP_REGS+1*4(r1)
> stw _d2,PP_REGS+2*4(r1)
> stw _d3,PP_REGS+3*4(r1)
> stw _d4,PP_REGS+4*4(r1)
> stw _d5,PP_REGS+5*4(r1)
> stw _d6,PP_REGS+6*4(r1)
> stw _d7,PP_REGS+7*4(r1)
> stw _a0,PP_REGS+8*4(r1)
> stw _a1,PP_REGS+9*4(r1)
> stw _a2,PP_REGS+10*4(r1)
> stw _a3,PP_REGS+11*4(r1)
> stw _a4,PP_REGS+12*4(r1)
> stw _a5,PP_REGS+13*4(r1)
> stw _a6,PP_REGS+14*4(r1)
>
> lwz _d0,@_backcall(rtoc)
> stw _d0,PP_CODE(r1)
> li _d0,0
> stw _d0,PP_OFFSET(r1)
> stw _d0,PP_FLAGS(r1)
> stw _d0,PP_STACKPTR(r1)
> stw _d0,PP_STACKSIZE(r1)
> mr r4,r1
>
> .global @__PowerPCBase
>
> lwz r3,@__PowerPCBase(rtoc)
> lwz r3,0(r3)
> lwz r5,-300+2(r3) #Run68K
> mtlr r5
> blrl

This is very dangerous. You have built your PPCArgs structure at
the bottom of the current stack frame. But when you call a sub-
routine, it expects a valid PowerOpen stack frame at this location.
So when Run68k() saves CR, LR or whatever in this stack frame, it
will destroy your PPCArgs structure.

> lwz _d0,PP_REGS(r1)
> addi r1,r1,PP_SIZE
>
> blr

Finally this BLR may be the cause of all your troubles. You didn't
neither save nor restore the LR register. But you destroyed it
yourself by doing "mtlr r5", some lines before.

This function will never return.

> So, what is wrong with it? Calling requires an exact PowerOpen
> stackframe?

Exactly. I would strongly recommend building a valid PowerOpen
stack frame, when calling powerpc.library functions.

> I use pasm from VBCC package and phxass.

This is definitely not the problem. :)

BTW, how do you link the M68k and PPC objects? I recommend
linking with vlibwos:x.o, to insert some 32-bytes buffers
between the sections.



_ Frank Wille (frank@phoenix.owl.de)
_ // http://home.owl.de/~frank/
\X/ Phx @ #AmigaGer