These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / core / fnrec.c
1 /*
2  * Copyright (C) 2010 Stefan Hajnoczi <stefanha@gmail.com>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <ipxe/init.h>
30 #include <ipxe/uaccess.h>
31 #include <ipxe/io.h>
32
33 /** @file
34  *
35  * Function trace recorder for crash and hang debugging
36  *
37  */
38
39 /** Constant for identifying valid trace buffers */
40 #define FNREC_MAGIC ( 'f' << 24 | 'n' << 16 | 'r' << 8 | 'e' )
41
42 /** Number of trace buffer entries */
43 #define FNREC_NUM_ENTRIES 4096
44
45 /** Trace buffer physical address
46  *
47  * Fixed at 17MB
48  */
49 #define FNREC_PHYS_ADDRESS ( 17 * 1024 * 1024 )
50
51 /** A trace buffer entry */
52 struct fnrec_entry {
53         /** Called function address */
54         void *called_fn;
55         /** Call site */
56         void *call_site;
57         /** Entry count */
58         uint16_t entry_count;
59         /** Exit count */
60         uint16_t exit_count;
61         /** Checksum */
62         unsigned long checksum;
63 };
64
65 /** A trace buffer */
66 struct fnrec_buffer {
67         /** Constant for identifying valid trace buffers */
68         uint32_t magic;
69
70         /** Next trace buffer entry to fill */
71         unsigned int idx;
72
73         /** Trace buffer */
74         struct fnrec_entry data[FNREC_NUM_ENTRIES]
75                 __attribute__ (( aligned ( 64 ) ));
76 };
77
78 /** The trace buffer */
79 static struct fnrec_buffer *fnrec_buffer;
80
81 /**
82  * Test whether the trace buffer is valid
83  *
84  * @ret is_valid        Buffer is valid
85  */
86 static int fnrec_is_valid ( void ) {
87         return ( fnrec_buffer && ( fnrec_buffer->magic == FNREC_MAGIC ) );
88 }
89
90 /**
91  * Invalidate the trace buffer
92  *
93  */
94 static void fnrec_invalidate ( void ) {
95         fnrec_buffer->magic = 0;
96 }
97
98 /**
99  * Reset the trace buffer and clear entries
100  */
101 static void fnrec_reset ( void ) {
102         memset ( fnrec_buffer, 0, sizeof ( *fnrec_buffer ) );
103         fnrec_buffer->magic = FNREC_MAGIC;
104 }
105
106 /**
107  * Append an entry to the trace buffer
108  *
109  * @v called_fn         Called function
110  * @v call_site         Call site
111  * @ret entry           Trace buffer entry
112  */
113 static struct fnrec_entry * fnrec_append ( void *called_fn, void *call_site ) {
114         struct fnrec_entry *entry;
115
116         /* Re-use existing entry, if possible */
117         entry = &fnrec_buffer->data[ fnrec_buffer->idx ];
118         if ( ( entry->called_fn == called_fn ) &&
119              ( entry->call_site == call_site ) &&
120              ( entry->entry_count >= entry->exit_count ) ) {
121                 return entry;
122         }
123
124         /* Otherwise, create a new entry */
125         fnrec_buffer->idx = ( ( fnrec_buffer->idx + 1 ) % FNREC_NUM_ENTRIES );
126         entry = &fnrec_buffer->data[ fnrec_buffer->idx ];
127         entry->called_fn = called_fn;
128         entry->call_site = call_site;
129         entry->entry_count = 0;
130         entry->exit_count = 0;
131         entry->checksum = ( ( ( unsigned long ) called_fn ) ^
132                             ( ( unsigned long ) call_site ) );
133         return entry;
134 }
135
136 /**
137  * Print the contents of the trace buffer in chronological order
138  */
139 static void fnrec_dump ( void ) {
140         struct fnrec_entry *entry;
141         unsigned int i;
142         unsigned int idx;
143         unsigned long checksum;
144
145         printf ( "fnrec buffer dump:\n" );
146         for ( i = 1 ; i <= FNREC_NUM_ENTRIES ; i++ ) {
147                 idx = ( ( fnrec_buffer->idx + i ) % FNREC_NUM_ENTRIES );
148                 entry = &fnrec_buffer->data[idx];
149                 if ( ( entry->entry_count == 0 ) && ( entry->exit_count == 0 ) )
150                         continue;
151                 checksum = ( ( ( ( unsigned long ) entry->called_fn ) ^
152                                ( ( unsigned long ) entry->call_site ) ) +
153                              entry->entry_count + entry->exit_count );
154                 printf ( "%p %p %d %d", entry->called_fn, entry->call_site,
155                          entry->entry_count, entry->exit_count );
156                 if ( entry->checksum != checksum ) {
157                         printf ( " (checksum wrong at phys %08lx)",
158                                  virt_to_phys ( entry ) );
159                 }
160                 printf ( "\n");
161         }
162 }
163
164 /**
165  * Function tracer initialisation function
166  */
167 static void fnrec_init ( void ) {
168
169         fnrec_buffer = phys_to_virt ( FNREC_PHYS_ADDRESS );
170         if ( fnrec_is_valid() ) {
171                 fnrec_invalidate();
172                 fnrec_dump();
173         } else {
174                 printf ( "fnrec buffer not found\n" );
175         }
176         fnrec_reset();
177 }
178
179 struct init_fn fnrec_init_fn __init_fn ( INIT_NORMAL ) = {
180         .initialise = fnrec_init,
181 };
182
183 /*
184  * These functions are called from every C function.  The compiler inserts
185  * these calls when -finstrument-functions is used.
186  */
187 void __cyg_profile_func_enter ( void *called_fn, void *call_site ) {
188         struct fnrec_entry *entry;
189
190         if ( fnrec_is_valid() ) {
191                 entry = fnrec_append ( called_fn, call_site );
192                 entry->entry_count++;
193                 entry->checksum++;
194                 mb();
195         }
196 }
197
198 void __cyg_profile_func_exit ( void *called_fn, void *call_site ) {
199         struct fnrec_entry *entry;
200
201         if ( fnrec_is_valid() ) {
202                 entry = fnrec_append ( called_fn, call_site );
203                 entry->exit_count++;
204                 entry->checksum++;
205                 mb();
206         }
207 }