1/*
2(C) Copyright IBM Corp. 2008
3
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met:
8
9* Redistributions of source code must retain the above copyright notice,
10this list of conditions and the following disclaimer.
11* Redistributions in binary form must reproduce the above copyright
12notice, this list of conditions and the following disclaimer in the
13documentation and/or other materials provided with the distribution.
14* Neither the name of IBM nor the names of its contributors may be
15used to endorse or promote products derived from this software without
16specific prior written permission.
17
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28POSSIBILITY OF SUCH DAMAGE.
29
30Author: Ken Werner <ken.werner@de.ibm.com>
31*/
32
33/* _mcount extracts the address of the function just entered and the address
34    of the caller of that function and then calls __mcount_internal. The
35    prologue calls mcount without saving any registers. The return address is
36    stored in $75. The _mcount function has to:
37     - create a new stack frame
38     - save registers $2 to $75 on the stack
39     - copy the two addresses ($0 and $75) into the argument registers $3 and $4
40     - call __mcount_internal
41     - restore registers
42     - return to $75  */
43
44/* The following two convenience macros assist in the coding of the
45   saving and restoring the register.
46
47   saveregs     first, last    Saves registers from first to the last.
48   restoreregs  first, last    Restores registers from last down to first.
49
50   Note:       first must be less than or equal to last.  */
51.macro  saveregs        first, last
52        stqd            $\first, \first*16($SP)
53.if     \last-\first
54        saveregs        "(\first+1)",\last
55.endif
56.endm
57
58.macro  restoreregs     first, last
59        lqd             $\last, \last*16($SP)
60.if     \last-\first
61        restoreregs     \first,"(\last-1)"
62.endif
63.endm
64
65/* _mcount needs to be resident since the overlay manager uses the scratch
66   registers too.  */
67.text
68  .align 3 /* 8 byte alignment.  */
69  .global _mcount
70  .type _mcount, @function
71
72_mcount:
73  stqd $lr, 16($sp)    /* Save link register in the callers stack frame.  */
74  stqd $lr, -1216($sp) /* Store back pointer.  */
75  il   $lr, -1216      /* Push a new stack frame.  */
76  a    $sp, $sp, $lr   /* Frame size: 16 * (74 + 2) = 1216.  */
77
78  /* Save registers $2 to $75 on the stack.  */
79  saveregs 2, 75
80
81  /* Bring the __mcount_internal arguments in place.  */
82  lqd $3, 1232($sp) /* frompc (the link register).  */
83  ori $4, $75, 0    /* selfpc (the gcc prologue puts "brsl $75, _mcount" in
84                       front of every function).  */
85  brsl  $lr, __mcount_internal
86
87  /* Restore register $2 to $75 from the stack.  */
88  restoreregs 2, 75
89
90  il   $lr, 1216
91  a    $sp, $sp, $lr   /* Pop the stack frame.  */
92  lqd  $lr, 16($sp)    /* Restore link register.  */
93  bi   $75             /* Branch to the called function.  */
94