Add qemu 2.4.0
[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
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <ipxe/init.h>
26 #include <ipxe/uaccess.h>
27 #include <ipxe/io.h>
28
29 /** @file
30  *
31  * Function trace recorder for crash and hang debugging
32  *
33  */
34
35 /** Constant for identifying valid trace buffers */
36 #define FNREC_MAGIC ( 'f' << 24 | 'n' << 16 | 'r' << 8 | 'e' )
37
38 /** Number of trace buffer entries */
39 #define FNREC_NUM_ENTRIES 4096
40
41 /** Trace buffer physical address
42  *
43  * Fixed at 17MB
44  */
45 #define FNREC_PHYS_ADDRESS ( 17 * 1024 * 1024 )
46
47 /** A trace buffer entry */
48 struct fnrec_entry {
49         /** Called function address */
50         void *called_fn;
51         /** Call site */
52         void *call_site;
53         /** Entry count */
54         uint16_t entry_count;
55         /** Exit count */
56         uint16_t exit_count;
57         /** Checksum */
58         unsigned long checksum;
59 };
60
61 /** A trace buffer */
62 struct fnrec_buffer {
63         /** Constant for identifying valid trace buffers */
64         uint32_t magic;
65
66         /** Next trace buffer entry to fill */
67         unsigned int idx;
68
69         /** Trace buffer */
70         struct fnrec_entry data[FNREC_NUM_ENTRIES]
71                 __attribute__ (( aligned ( 64 ) ));
72 };
73
74 /** The trace buffer */
75 static struct fnrec_buffer *fnrec_buffer;
76
77 /**
78  * Test whether the trace buffer is valid
79  *
80  * @ret is_valid        Buffer is valid
81  */
82 static int fnrec_is_valid ( void ) {
83         return ( fnrec_buffer && ( fnrec_buffer->magic == FNREC_MAGIC ) );
84 }
85
86 /**
87  * Invalidate the trace buffer
88  *
89  */
90 static void fnrec_invalidate ( void ) {
91         fnrec_buffer->magic = 0;
92 }
93
94 /**
95  * Reset the trace buffer and clear entries
96  */
97 static void fnrec_reset ( void ) {
98         memset ( fnrec_buffer, 0, sizeof ( *fnrec_buffer ) );
99         fnrec_buffer->magic = FNREC_MAGIC;
100 }
101
102 /**
103  * Append an entry to the trace buffer
104  *
105  * @v called_fn         Called function
106  * @v call_site         Call site
107  * @ret entry           Trace buffer entry
108  */
109 static struct fnrec_entry * fnrec_append ( void *called_fn, void *call_site ) {
110         struct fnrec_entry *entry;
111
112         /* Re-use existing entry, if possible */
113         entry = &fnrec_buffer->data[ fnrec_buffer->idx ];
114         if ( ( entry->called_fn == called_fn ) &&
115              ( entry->call_site == call_site ) &&
116              ( entry->entry_count >= entry->exit_count ) ) {
117                 return entry;
118         }
119
120         /* Otherwise, create a new entry */
121         fnrec_buffer->idx = ( ( fnrec_buffer->idx + 1 ) % FNREC_NUM_ENTRIES );
122         entry = &fnrec_buffer->data[ fnrec_buffer->idx ];
123         entry->called_fn = called_fn;
124         entry->call_site = call_site;
125         entry->entry_count = 0;
126         entry->exit_count = 0;
127         entry->checksum = ( ( ( unsigned long ) called_fn ) ^
128                             ( ( unsigned long ) call_site ) );
129         return entry;
130 }
131
132 /**
133  * Print the contents of the trace buffer in chronological order
134  */
135 static void fnrec_dump ( void ) {
136         struct fnrec_entry *entry;
137         unsigned int i;
138         unsigned int idx;
139         unsigned long checksum;
140
141         printf ( "fnrec buffer dump:\n" );
142         for ( i = 1 ; i <= FNREC_NUM_ENTRIES ; i++ ) {
143                 idx = ( ( fnrec_buffer->idx + i ) % FNREC_NUM_ENTRIES );
144                 entry = &fnrec_buffer->data[idx];
145                 if ( ( entry->entry_count == 0 ) && ( entry->exit_count == 0 ) )
146                         continue;
147                 checksum = ( ( ( ( unsigned long ) entry->called_fn ) ^
148                                ( ( unsigned long ) entry->call_site ) ) +
149                              entry->entry_count + entry->exit_count );
150                 printf ( "%p %p %d %d", entry->called_fn, entry->call_site,
151                          entry->entry_count, entry->exit_count );
152                 if ( entry->checksum != checksum ) {
153                         printf ( " (checksum wrong at phys %08lx)",
154                                  virt_to_phys ( entry ) );
155                 }
156                 printf ( "\n");
157         }
158 }
159
160 /**
161  * Function tracer initialisation function
162  */
163 static void fnrec_init ( void ) {
164
165         fnrec_buffer = phys_to_virt ( FNREC_PHYS_ADDRESS );
166         if ( fnrec_is_valid() ) {
167                 fnrec_invalidate();
168                 fnrec_dump();
169         } else {
170                 printf ( "fnrec buffer not found\n" );
171         }
172         fnrec_reset();
173 }
174
175 struct init_fn fnrec_init_fn __init_fn ( INIT_NORMAL ) = {
176         .initialise = fnrec_init,
177 };
178
179 /*
180  * These functions are called from every C function.  The compiler inserts
181  * these calls when -finstrument-functions is used.
182  */
183 void __cyg_profile_func_enter ( void *called_fn, void *call_site ) {
184         struct fnrec_entry *entry;
185
186         if ( fnrec_is_valid() ) {
187                 entry = fnrec_append ( called_fn, call_site );
188                 entry->entry_count++;
189                 entry->checksum++;
190                 mb();
191         }
192 }
193
194 void __cyg_profile_func_exit ( void *called_fn, void *call_site ) {
195         struct fnrec_entry *entry;
196
197         if ( fnrec_is_valid() ) {
198                 entry = fnrec_append ( called_fn, call_site );
199                 entry->exit_count++;
200                 entry->checksum++;
201                 mb();
202         }
203 }