Subject: asm.h changes to support user-mode profiling -- need code review
To: None <port-mips@netbsd.org>
From: Ethan Solomita <ethan@geocast.com>
List: port-mips
Date: 04/28/2000 15:44:22
	A big problem to getting user applications to compile with -pg was that
the _mcount call code that was being added to .S files was bad. Getting
it right has been tough, because of the need to keep $gp and $t9
correct, especially with routines that call .cpload themselves.

	Here is my change to sys/arch/mips/include/asm.h's #define
_KERN_MCOUNT:

#define _KERN_MCOUNT            \
        .set    noat;           \
        .cpload t9;             \
        subu    sp,sp,4;        \
        .cprestore 0;           \
        move    $1,$31;         \
        subu    sp,sp,8;        \
        jal     _mcount;        \
        nop;                    \
        addu    sp,sp,4;        \
        la      t9,1f;          \
        .set at;                \
1:

	The original was:

#define _KERN_MCOUNT            \
        .set    noat;           \
        move    $1,$31;         \
        jal     _mcount;        \
        subu    sp,sp,8;        \
        .set at

	_KERN_MCOUNT is part of the #define for LEAF(), so it will come before
any contents of the .S. Any such function which calls .cpload expects
$t9 to point to its first instruction, hence my la t9,1f;. The nop is
needed because, if jal _mcount is PIC code, then there isn't a bdslot,
otherwise there is. This was the only thing I could figure out that
seemed safe.

	Here's the result of _KERN_MCOUNT as it appears in libc's strcmp:

0x434f40 <strcmp>:      lui     $gp,0xfbd
0x434f44 <strcmp+4>:    addiu   $gp,$gp,20848
0x434f48 <strcmp+8>:    addu    $gp,$gp,$t9
0x434f4c <strcmp+12>:   addiu   $sp,$sp,-4
0x434f50 <strcmp+16>:   sw      $gp,0($sp)
0x434f54 <strcmp+20>:   move    $at,$ra
0x434f58 <strcmp+24>:   addiu   $sp,$sp,-8
0x434f5c <strcmp+28>:   lw      $t9,-32668($gp)
0x434f60 <strcmp+32>:   nop
0x434f64 <strcmp+36>:   jalr    $t9
0x434f68 <strcmp+40>:   nop
0x434f6c <strcmp+44>:   lw      $gp,0($sp)
0x434f70 <strcmp+48>:   nop
0x434f74 <strcmp+52>:   addiu   $sp,$sp,4
0x434f78 <strcmp+56>:   lw      $t9,-32724($gp)
0x434f7c <strcmp+60>:   nop
0x434f80 <strcmp+64>:   addiu   $t9,$t9,20356

	With this change to asm.h, programs seem to run correctly. I'm still
not getting a file containing the profiling data. I guess that's the
next step.
	-- Ethan