How do I call an OS function "by hand"?
  • Moderator
    Kronos
    Posts: 2242 from 2003/2/24
    I am trying to setup a system that lets a LUA script call any given OS function (think about the LIBCALLS in AmigaBasic) and for this I would need to call the OS from C without involving any macros or compiler magic.

    This is what I thought to be the solution...

    (trying to open dos.libray)

    Code:


    TEXT DosName[] = "dos.library";
    REG_D0 = 0;
    REG_A0 = (ULONG) DosName;
    REG_A6 = SysBase;
    ULONG res = (*MyEmulHandle->EmulCallDirectOS)(-552);
    ULONG res1 = REG_A0;
    ULONG res2 = REG_D0;

    kprintf("aaa %x %x %x %x %xn",SysBase,res,res1,res2,DosName);


    ..but is obviously not.
  • »04.08.21 - 13:47
    Profile
  • Acolyte of the Butterfly
    Acolyte of the Butterfly
    Georg
    Posts: 106 from 2004/4/7
    library name goes into A1 ...
  • »04.08.21 - 14:19
    Profile
  • Moderator
    Kronos
    Posts: 2242 from 2003/2/24
    Quote:

    Georg wrote:
    library name goes into A1 ...



    *doh*

    I was almost certain it had to be something that simple/dumb.....

    Thanks.
  • »04.08.21 - 14:28
    Profile
  • MorphOS Developer
    Piru
    Posts: 576 from 2003/2/24
    From: finland, the l...
    Quote:

    Kronos wrote:
    Code:


    TEXT DosName[] = "dos.library";
    REG_D0 = 0;
    REG_A0 = (ULONG) DosName;
    REG_A6 = SysBase;
    ULONG res = (*MyEmulHandle->EmulCallDirectOS)(-552);
    ULONG res1 = REG_A0;
    ULONG res2 = REG_D0;

    kprintf("aaa %x %x %x %x %xn",SysBase,res,res1,res2,DosName);




    As noted before the library name needs to be in A1.

    The return value is in D0. Generally D1 does not contain any sensible value. 99% of the library functions return value only in D0 only. There are some really rare exceptions to this (utility.library UDivMod32 and SDivMod32 functions return 64-bit value in d0:d1 pair for example... but it makes little sense to call these functions directly I'd say).

    If you mean res2 as in "the last dos error", then you need to use dos.library/IoErr() to get that value.

    Note that when you set up REG_xx like that, since the registers might be overwritten unless if you're careful. One example of problematic code:

    Code:

    REG_D0 = 0x12345678;
    REG_D1 = Output();


    Here REG_D0 that is set before would be overwritten by the dos.library Output() call glue logic (and both REG_D0 and REG_D1 would result as current output file handle value).

    One solution is to use a variable like this:
    Code:

    BPTR outfh = Output();
    REG_D0 = 0x12345678;
    REG_D1 = outfh;


    Either way, unless if you really must, I'd try to remain using the MorphOS SDK provided ppcinline glue logic instead of custom code like this. However, it does work directly like this as well. The likelihood of silly mistakes is rather high, however. Of course it might be possible to generate the glue functions from the protos/fd files with some tool similar to cvinclude.pl.

    Finally, many functions will be rather dangerous to call. Imagine for example intuition/LockIBase or something that obtains a global system lock. Holding this lock will effectively lock all UI operations, and the system will appear "locked up". It's like weaponizing Lua. It's likely that you can't shoot yourself to the foot with Lua normally, but with this functionality you give ample arsenal for it. Maybe there just needs to be a warning to the user deciding to use this functionality: "be careful what you do with this thing"...

    [ Edited by Piru 04.08.2021 - 19:05 ]
  • »04.08.21 - 17:02
    Profile
  • Moderator
    Kronos
    Posts: 2242 from 2003/2/24
    @Piru

    The 3 "res" variants are only there because I was unsure if and where I would get the return value.

    This whole thing can for sure be cumbersome and dangerous, just like it was back in the day with the AmigaBasic version of the same idea.

    I don't expect anybody trying to write a file system in LUA but (which I guess would be hilarious) and that is nowhere near the aim of this.

    I do plan to extend MUI-wrapper and will add what I think are useful non MUI functions with direct LUA calls (mostly stuff like GFX) but there can always be the case where someone writes a tool/script that somehow need to list mounted devices, check wether there is a registered user (as provided by a keyfile) or something of that matter.
  • »04.08.21 - 19:15
    Profile