]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/vinum/vinummemory.c
This commit was generated by cvs2svn to compensate for changes in r53574,
[FreeBSD/FreeBSD.git] / sys / dev / vinum / vinummemory.c
1 /*-
2  * Copyright (c) 1997, 1998
3  *      Nan Yang Computer Services Limited.  All rights reserved.
4  *
5  *  This software is distributed under the so-called ``Berkeley
6  *  License'':
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Nan Yang Computer
19  *      Services Limited.
20  * 4. Neither the name of the Company nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * This software is provided ``as is'', and any express or implied
25  * warranties, including, but not limited to, the implied warranties of
26  * merchantability and fitness for a particular purpose are disclaimed.
27  * In no event shall the company or contributors be liable for any
28  * direct, indirect, incidental, special, exemplary, or consequential
29  * damages (including, but not limited to, procurement of substitute
30  * goods or services; loss of use, data, or profits; or business
31  * interruption) however caused and on any theory of liability, whether
32  * in contract, strict liability, or tort (including negligence or
33  * otherwise) arising in any way out of the use of this software, even if
34  * advised of the possibility of such damage.
35  *
36  * $Id: vinummemory.c,v 1.22 1999/10/12 04:35:48 grog Exp grog $
37  * $FreeBSD$
38  */
39
40 #include <dev/vinum/vinumhdr.h>
41
42 #ifdef VINUMDEBUG
43 #undef longjmp                                              /* this was defined as LongJmp */
44 void longjmp(jmp_buf, int);                                 /* the kernel doesn't define this */
45
46 #include <dev/vinum/request.h>
47 extern struct rqinfo rqinfo[];
48 extern struct rqinfo *rqip;
49 int rqinfo_size = RQINFO_SIZE;                              /* for debugger */
50
51 #ifdef __i386__                                             /* check for validity */
52 void
53 LongJmp(jmp_buf buf, int retval)
54 {
55 /*
56    * longjmp is not documented, not even jmp_buf.
57    * This is what's in i386/i386/support.s:
58    * ENTRY(longjmp)
59    *    movl    4(%esp),%eax
60    *    movl    (%eax),%ebx                      restore ebx
61    *    movl    4(%eax),%esp                     restore esp
62    *    movl    8(%eax),%ebp                     restore ebp
63    *    movl    12(%eax),%esi                    restore esi
64    *    movl    16(%eax),%edi                    restore edi
65    *    movl    20(%eax),%edx                    get rta
66    *    movl    %edx,(%esp)                      put in return frame
67    *    xorl    %eax,%eax                        return(1);
68    *    incl    %eax
69    *    ret
70    *
71    * from which we deduce the structure of jmp_buf:
72  */
73     struct JmpBuf {
74         int jb_ebx;
75         int jb_esp;
76         int jb_ebp;
77         int jb_esi;
78         int jb_edi;
79         int jb_eip;
80     };
81
82     struct JmpBuf *jb = (struct JmpBuf *) buf;
83
84     if ((jb->jb_esp < 0xc0000000)
85         || (jb->jb_ebp < 0xc0000000)
86         || (jb->jb_eip < 0xc0000000))
87         panic("Invalid longjmp");
88     longjmp(buf, retval);
89 }
90
91 /* find the base name of a path name */
92 char *
93 basename(char *file)
94 {
95     char *f = rindex(file, '/');                            /* chop off dirname if present */
96
97     if (f == NULL)
98         return file;
99     else
100         return ++f;                                         /* skip the / */
101 }
102
103 #else
104 #define LongJmp longjmp                                     /* just use the kernel function */
105 #endif
106 #endif
107
108 void
109 expand_table(void **table, int oldsize, int newsize)
110 {
111     if (newsize > oldsize) {
112         int *temp;
113
114         temp = (int *) Malloc(newsize);                     /* allocate a new table */
115         CHECKALLOC(temp, "vinum: Can't expand table\n");
116         bzero((char *) temp, newsize);                      /* clean it all out */
117         if (*table != NULL) {                               /* already something there, */
118             bcopy((char *) *table, (char *) temp, oldsize); /* copy it to the old table */
119             Free(*table);
120         }
121         *table = temp;
122     }
123 }
124
125 #if VINUMDEBUG                                              /* XXX debug */
126 #define MALLOCENTRIES 16384
127 int malloccount = 0;
128 int highwater = 0;                                          /* highest index ever allocated */
129 struct mc malloced[MALLOCENTRIES];
130
131 #define FREECOUNT 64
132 int freecount = FREECOUNT;                                  /* for debugger */
133 int lastfree = 0;
134 struct mc freeinfo[FREECOUNT];
135
136 int total_malloced;
137 static int mallocseq = 0;
138
139 caddr_t
140 MMalloc(int size, char *file, int line)
141 {
142     int s;
143     caddr_t result;
144     int i;
145
146     if (malloccount >= MALLOCENTRIES) {                     /* too many */
147         log(LOG_ERR, "vinum: can't allocate table space to trace memory allocation");
148         return 0;                                           /* can't continue */
149     }
150     /* Wait for malloc if we can */
151     result = malloc(size, M_DEVBUF, intr_nesting_level == 0 ? M_WAITOK : M_NOWAIT);
152     if (result == NULL)
153         log(LOG_ERR, "vinum: can't allocate %d bytes from %s:%d\n", size, file, line);
154     else {
155         s = splhigh();
156         for (i = 0; i < malloccount; i++) {
157             if (((result + size) > malloced[i].address)
158                 && (result < malloced[i].address + malloced[i].size)) /* overlap */
159                 Debugger("Malloc overlap");
160         }
161         if (result) {
162             char *f = basename(file);
163
164             i = malloccount++;
165             total_malloced += size;
166             microtime(&malloced[i].time);
167             malloced[i].seq = mallocseq++;
168             malloced[i].size = size;
169             malloced[i].line = line;
170             malloced[i].address = result;
171             bcopy(f, malloced[lastfree].file, min(strlen(f), MCFILENAMELEN - 1));
172             malloced[lastfree].file[MCFILENAMELEN - 1] = '\0';
173         }
174         if (malloccount > highwater)
175             highwater = malloccount;
176         splx(s);
177     }
178     return result;
179 }
180
181 void
182 FFree(void *mem, char *file, int line)
183 {
184     int s;
185     int i;
186
187     s = splhigh();
188     for (i = 0; i < malloccount; i++) {
189         if ((caddr_t) mem == malloced[i].address) {         /* found it */
190             bzero(mem, malloced[i].size);                   /* XXX */
191             free(mem, M_DEVBUF);
192             malloccount--;
193             total_malloced -= malloced[i].size;
194             if (debug & DEBUG_MEMFREE) {                    /* keep track of recent frees */
195                 char *f = rindex(file, '/');                /* chop off dirname if present */
196
197                 if (f == NULL)
198                     f = file;
199                 else
200                     f++;                                    /* skip the / */
201
202                 microtime(&freeinfo[lastfree].time);
203                 freeinfo[lastfree].seq = malloced[i].seq;
204                 freeinfo[lastfree].size = malloced[i].size;
205                 freeinfo[lastfree].line = line;
206                 freeinfo[lastfree].address = mem;
207                 bcopy(f, freeinfo[lastfree].file, min(strlen(f), MCFILENAMELEN - 1));
208                 freeinfo[lastfree].file[MCFILENAMELEN - 1] = '\0';
209                 if (++lastfree == FREECOUNT)
210                     lastfree = 0;
211             }
212             if (i < malloccount)                            /* more coming after */
213                 bcopy(&malloced[i + 1], &malloced[i], (malloccount - i) * sizeof(struct mc));
214             splx(s);
215             return;
216         }
217     }
218     splx(s);
219     log(LOG_ERR, "Freeing unallocated data at 0x%08x from %s, line %d\n", (int) mem, file, line);
220     Debugger("Free");
221 }
222
223 void
224 vinum_meminfo(caddr_t data)
225 {
226     struct meminfo *m = (struct meminfo *) data;
227
228     m->mallocs = malloccount;
229     m->total_malloced = total_malloced;
230     m->malloced = malloced;
231     m->highwater = highwater;
232 }
233
234 int
235 vinum_mallocinfo(caddr_t data)
236 {
237     struct mc *m = (struct mc *) data;
238     unsigned int ent = m->seq;                              /* index of entry to return */
239
240     if (ent >= malloccount)
241         return ENOENT;
242     m->address = malloced[ent].address;
243     m->size = malloced[ent].size;
244     m->line = malloced[ent].line;
245     m->seq = malloced[ent].seq;
246     bcopy(malloced[ent].file, m->file, MCFILENAMELEN);
247     return 0;
248 }
249
250 /*
251  * return the nth request trace buffer entry.  This
252  * is indexed back from the current entry (which
253  * has index 0)
254  */
255 int
256 vinum_rqinfo(caddr_t data)
257 {
258     struct rqinfo *rq = (struct rqinfo *) data;
259     int ent = *(int *) data;                                /* 1st word is index */
260     int lastent = rqip - rqinfo;                            /* entry number of current entry */
261
262     if (ent >= RQINFO_SIZE)                                 /* out of the table */
263         return ENOENT;
264     if ((ent = lastent - ent - 1) < 0)
265         ent += RQINFO_SIZE;                                 /* roll over backwards */
266     bcopy(&rqinfo[ent], rq, sizeof(struct rqinfo));
267     return 0;
268 }
269 #endif