From:	MERC::"uunet!WKUVX1.BITNET!MacroMan"  4-APR-1992 14:22:17.65
To:	<raxco!galaxy.dnet!gleeve@UUNET.UU.NET>
CC:	
Subj:	RE: System service calls & access modes

In article <702334345.119861.MILLER@TGV.COM>, <MILLER@TGV.COM> writes:
> Ok, i'll give it a shot.
>
> Let's say I'm programming in C.  and I want to do something in kernel
> mode.   but for stylistic reasons, I don't want to create another routine.
> I want the code to appear inline.  I want to do something like this:

> How do you do it?
> Like this:

Very nice, but slightly flawed....

        $ TYPE BREAK_KMON.C
        int saved;
        main() {
                demo(42);
                return 1;
        }
        demo(param) {
                long s;
                s = kmon();
                save_it(param); /* just to force the compiler to put an
                                   actual reference to param without
                                   optimising things away on me */
                kmoff(s);
                printf("param = %d when in kernel mode\n", c);
        }
        save_it(x) {
                saved = x;
        }

        $ RUN BREAK_KMON
        param = 2146628900 when in kernel mode

Oops!  That one could bite in some really obscure ways.  It's easily
fixed though:

>         .TITLE  KMON     "Toggle kernel mode"
          .IDENT /1.1/
> [...]
> 20$:    .word   ^m<>
>         MOVL    4(AP),R0

          movl    8(R0),AP      ; keep AP pointing at the (user mode)
                                ; caller's arglist

>         MOVQ    20(R0),R2
>         MOVQ    28(R0),R4
>         MOVQ    36(R0),R6
>         MOVQ    44(R0),R8
>         MOVQ    52(R0),R10
>         JMP     @16(R0)
> [...]


Personally, I'd be inclined to store the s in "s = kmon();" in kmon's
module, preinitialised to 0.  That would allow run-time checking, in the
*unlikely* event of the routines being called out of sequence.  Also you
don't need to carry around this spare variable; just:

        kmon();
        <kernel code>
        kmoff();

Yes I know, picky picky.... 8-)


> I actually had comments explaining the whole thing, but I figured you
> guys would have more fun if I took them out.

We'd have stripped them out if you hadn't.  Comments can be misleading.
The only true documentation is the binary.... 8-)


Don Stokes, ZL2TNM (DS555)                       don@zl2tnm.gen.nz (home)
Network Manager, Computing Services Centre           don@vuw.ac.nz (work)
Victoria University of Wellington, New Zealand             +64-4-495-5052
