1998-12-28 04:56:24 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1997, 1998
|
|
|
|
* Nan Yang Computer Services Limited. All rights reserved.
|
|
|
|
*
|
|
|
|
* This software is distributed under the so-called ``Berkeley
|
|
|
|
* License'':
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by Nan Yang Computer
|
|
|
|
* Services Limited.
|
|
|
|
* 4. Neither the name of the Company nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
1999-10-13 03:18:53 +00:00
|
|
|
*
|
1998-12-28 04:56:24 +00:00
|
|
|
* This software is provided ``as is'', and any express or implied
|
|
|
|
* warranties, including, but not limited to, the implied warranties of
|
|
|
|
* merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
* In no event shall the company or contributors be liable for any
|
|
|
|
* direct, indirect, incidental, special, exemplary, or consequential
|
|
|
|
* damages (including, but not limited to, procurement of substitute
|
|
|
|
* goods or services; loss of use, data, or profits; or business
|
|
|
|
* interruption) however caused and on any theory of liability, whether
|
|
|
|
* in contract, strict liability, or tort (including negligence or
|
|
|
|
* otherwise) arising in any way out of the use of this software, even if
|
|
|
|
* advised of the possibility of such damage.
|
|
|
|
*
|
2001-05-23 23:24:05 +00:00
|
|
|
* $Id: vinummemory.c,v 1.28 2001/05/23 23:04:06 grog Exp grog $
|
1999-08-28 01:08:13 +00:00
|
|
|
* $FreeBSD$
|
1998-12-28 04:56:24 +00:00
|
|
|
*/
|
|
|
|
|
1998-12-28 16:28:24 +00:00
|
|
|
#include <dev/vinum/vinumhdr.h>
|
1998-12-28 04:56:24 +00:00
|
|
|
|
1998-12-28 16:28:24 +00:00
|
|
|
#ifdef VINUMDEBUG
|
1999-03-28 08:54:03 +00:00
|
|
|
#undef longjmp /* this was defined as LongJmp */
|
|
|
|
|
1998-12-28 16:28:24 +00:00
|
|
|
#include <dev/vinum/request.h>
|
1998-12-28 04:56:24 +00:00
|
|
|
extern struct rqinfo rqinfo[];
|
|
|
|
extern struct rqinfo *rqip;
|
1999-10-13 03:18:53 +00:00
|
|
|
int rqinfo_size = RQINFO_SIZE; /* for debugger */
|
1998-12-28 04:56:24 +00:00
|
|
|
|
1999-03-28 08:54:03 +00:00
|
|
|
#ifdef __i386__ /* check for validity */
|
1999-10-13 03:18:53 +00:00
|
|
|
void
|
1999-03-28 08:54:03 +00:00
|
|
|
LongJmp(jmp_buf buf, int retval)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* longjmp is not documented, not even jmp_buf.
|
|
|
|
* This is what's in i386/i386/support.s:
|
|
|
|
* ENTRY(longjmp)
|
|
|
|
* movl 4(%esp),%eax
|
1999-10-13 03:18:53 +00:00
|
|
|
* movl (%eax),%ebx restore ebx
|
|
|
|
* movl 4(%eax),%esp restore esp
|
|
|
|
* movl 8(%eax),%ebp restore ebp
|
|
|
|
* movl 12(%eax),%esi restore esi
|
|
|
|
* movl 16(%eax),%edi restore edi
|
|
|
|
* movl 20(%eax),%edx get rta
|
|
|
|
* movl %edx,(%esp) put in return frame
|
|
|
|
* xorl %eax,%eax return(1);
|
1999-03-28 08:54:03 +00:00
|
|
|
* incl %eax
|
|
|
|
* ret
|
|
|
|
*
|
|
|
|
* from which we deduce the structure of jmp_buf:
|
|
|
|
*/
|
|
|
|
struct JmpBuf {
|
|
|
|
int jb_ebx;
|
|
|
|
int jb_esp;
|
|
|
|
int jb_ebp;
|
|
|
|
int jb_esi;
|
|
|
|
int jb_edi;
|
|
|
|
int jb_eip;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct JmpBuf *jb = (struct JmpBuf *) buf;
|
|
|
|
|
1999-04-09 01:17:50 +00:00
|
|
|
if ((jb->jb_esp < 0xc0000000)
|
|
|
|
|| (jb->jb_ebp < 0xc0000000)
|
|
|
|
|| (jb->jb_eip < 0xc0000000))
|
1999-03-28 08:54:03 +00:00
|
|
|
panic("Invalid longjmp");
|
|
|
|
longjmp(buf, retval);
|
|
|
|
}
|
1999-04-10 08:11:44 +00:00
|
|
|
|
2000-01-05 06:08:23 +00:00
|
|
|
#else
|
|
|
|
#define LongJmp longjmp /* just use the kernel function */
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
1999-04-10 08:11:44 +00:00
|
|
|
/* find the base name of a path name */
|
|
|
|
char *
|
|
|
|
basename(char *file)
|
|
|
|
{
|
|
|
|
char *f = rindex(file, '/'); /* chop off dirname if present */
|
|
|
|
|
|
|
|
if (f == NULL)
|
|
|
|
return file;
|
|
|
|
else
|
|
|
|
return ++f; /* skip the / */
|
|
|
|
}
|
|
|
|
|
1999-10-13 03:18:53 +00:00
|
|
|
void
|
1998-12-28 04:56:24 +00:00
|
|
|
expand_table(void **table, int oldsize, int newsize)
|
|
|
|
{
|
|
|
|
if (newsize > oldsize) {
|
|
|
|
int *temp;
|
2000-05-04 07:38:47 +00:00
|
|
|
int s;
|
1998-12-28 04:56:24 +00:00
|
|
|
|
2000-05-04 07:38:47 +00:00
|
|
|
s = splhigh();
|
1998-12-28 04:56:24 +00:00
|
|
|
temp = (int *) Malloc(newsize); /* allocate a new table */
|
|
|
|
CHECKALLOC(temp, "vinum: Can't expand table\n");
|
1999-03-02 06:55:44 +00:00
|
|
|
bzero((char *) temp, newsize); /* clean it all out */
|
1998-12-28 04:56:24 +00:00
|
|
|
if (*table != NULL) { /* already something there, */
|
|
|
|
bcopy((char *) *table, (char *) temp, oldsize); /* copy it to the old table */
|
|
|
|
Free(*table);
|
|
|
|
}
|
|
|
|
*table = temp;
|
2000-05-04 07:38:47 +00:00
|
|
|
splx(s);
|
1998-12-28 04:56:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-05-23 23:24:05 +00:00
|
|
|
#ifdef VINUMDEBUG /* XXX debug */
|
1998-12-28 04:56:24 +00:00
|
|
|
#define MALLOCENTRIES 16384
|
|
|
|
int malloccount = 0;
|
|
|
|
int highwater = 0; /* highest index ever allocated */
|
1999-03-31 08:43:12 +00:00
|
|
|
struct mc malloced[MALLOCENTRIES];
|
1998-12-28 04:56:24 +00:00
|
|
|
|
1999-03-13 07:37:44 +00:00
|
|
|
#define FREECOUNT 64
|
1999-10-13 03:18:53 +00:00
|
|
|
int freecount = FREECOUNT; /* for debugger */
|
1999-03-13 07:37:44 +00:00
|
|
|
int lastfree = 0;
|
|
|
|
struct mc freeinfo[FREECOUNT];
|
|
|
|
|
1999-03-31 08:43:12 +00:00
|
|
|
int total_malloced;
|
1999-03-13 07:37:44 +00:00
|
|
|
static int mallocseq = 0;
|
1998-12-28 04:56:24 +00:00
|
|
|
|
1999-10-13 03:18:53 +00:00
|
|
|
caddr_t
|
1998-12-28 04:56:24 +00:00
|
|
|
MMalloc(int size, char *file, int line)
|
|
|
|
{
|
1999-10-13 03:18:53 +00:00
|
|
|
int s;
|
1998-12-28 04:56:24 +00:00
|
|
|
caddr_t result;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (malloccount >= MALLOCENTRIES) { /* too many */
|
1999-03-02 06:55:44 +00:00
|
|
|
log(LOG_ERR, "vinum: can't allocate table space to trace memory allocation");
|
1998-12-28 04:56:24 +00:00
|
|
|
return 0; /* can't continue */
|
|
|
|
}
|
1999-10-13 03:18:53 +00:00
|
|
|
/* Wait for malloc if we can */
|
2001-05-22 02:35:19 +00:00
|
|
|
result = malloc(size,
|
|
|
|
M_DEVBUF,
|
2001-09-12 08:38:13 +00:00
|
|
|
curthread->td_intr_nesting_level == 0 ? M_WAITOK : M_NOWAIT);
|
1998-12-28 04:56:24 +00:00
|
|
|
if (result == NULL)
|
1999-03-02 06:55:44 +00:00
|
|
|
log(LOG_ERR, "vinum: can't allocate %d bytes from %s:%d\n", size, file, line);
|
1998-12-28 04:56:24 +00:00
|
|
|
else {
|
|
|
|
s = splhigh();
|
|
|
|
for (i = 0; i < malloccount; i++) {
|
|
|
|
if (((result + size) > malloced[i].address)
|
|
|
|
&& (result < malloced[i].address + malloced[i].size)) /* overlap */
|
|
|
|
Debugger("Malloc overlap");
|
|
|
|
}
|
|
|
|
if (result) {
|
1999-04-10 08:11:44 +00:00
|
|
|
char *f = basename(file);
|
1998-12-28 04:56:24 +00:00
|
|
|
|
|
|
|
i = malloccount++;
|
|
|
|
total_malloced += size;
|
1999-03-19 07:14:18 +00:00
|
|
|
microtime(&malloced[i].time);
|
1999-03-13 07:37:44 +00:00
|
|
|
malloced[i].seq = mallocseq++;
|
1998-12-28 04:56:24 +00:00
|
|
|
malloced[i].size = size;
|
|
|
|
malloced[i].line = line;
|
1999-03-13 07:37:44 +00:00
|
|
|
malloced[i].address = result;
|
2000-02-29 06:13:24 +00:00
|
|
|
bcopy(f, malloced[i].file, min(strlen(f), MCFILENAMELEN - 1));
|
|
|
|
malloced[i].file[MCFILENAMELEN - 1] = '\0';
|
1998-12-28 04:56:24 +00:00
|
|
|
}
|
|
|
|
if (malloccount > highwater)
|
|
|
|
highwater = malloccount;
|
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1999-10-13 03:18:53 +00:00
|
|
|
void
|
1998-12-28 04:56:24 +00:00
|
|
|
FFree(void *mem, char *file, int line)
|
|
|
|
{
|
|
|
|
int s;
|
1999-10-13 03:18:53 +00:00
|
|
|
int i;
|
1998-12-28 04:56:24 +00:00
|
|
|
|
|
|
|
s = splhigh();
|
|
|
|
for (i = 0; i < malloccount; i++) {
|
|
|
|
if ((caddr_t) mem == malloced[i].address) { /* found it */
|
|
|
|
bzero(mem, malloced[i].size); /* XXX */
|
|
|
|
free(mem, M_DEVBUF);
|
|
|
|
malloccount--;
|
|
|
|
total_malloced -= malloced[i].size;
|
1999-03-13 07:37:44 +00:00
|
|
|
if (debug & DEBUG_MEMFREE) { /* keep track of recent frees */
|
|
|
|
char *f = rindex(file, '/'); /* chop off dirname if present */
|
|
|
|
|
|
|
|
if (f == NULL)
|
|
|
|
f = file;
|
|
|
|
else
|
|
|
|
f++; /* skip the / */
|
|
|
|
|
1999-03-19 07:14:18 +00:00
|
|
|
microtime(&freeinfo[lastfree].time);
|
1999-03-13 07:37:44 +00:00
|
|
|
freeinfo[lastfree].seq = malloced[i].seq;
|
|
|
|
freeinfo[lastfree].size = malloced[i].size;
|
|
|
|
freeinfo[lastfree].line = line;
|
|
|
|
freeinfo[lastfree].address = mem;
|
1999-10-13 03:18:53 +00:00
|
|
|
bcopy(f, freeinfo[lastfree].file, min(strlen(f), MCFILENAMELEN - 1));
|
|
|
|
freeinfo[lastfree].file[MCFILENAMELEN - 1] = '\0';
|
1999-03-13 07:37:44 +00:00
|
|
|
if (++lastfree == FREECOUNT)
|
|
|
|
lastfree = 0;
|
|
|
|
}
|
1998-12-28 04:56:24 +00:00
|
|
|
if (i < malloccount) /* more coming after */
|
|
|
|
bcopy(&malloced[i + 1], &malloced[i], (malloccount - i) * sizeof(struct mc));
|
|
|
|
splx(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
splx(s);
|
2000-01-05 06:08:23 +00:00
|
|
|
log(LOG_ERR,
|
|
|
|
"Freeing unallocated data at 0x%p from %s, line %d\n",
|
|
|
|
mem,
|
|
|
|
file,
|
|
|
|
line);
|
1998-12-28 04:56:24 +00:00
|
|
|
Debugger("Free");
|
|
|
|
}
|
|
|
|
|
1999-10-13 03:18:53 +00:00
|
|
|
void
|
1998-12-28 04:56:24 +00:00
|
|
|
vinum_meminfo(caddr_t data)
|
|
|
|
{
|
|
|
|
struct meminfo *m = (struct meminfo *) data;
|
|
|
|
|
|
|
|
m->mallocs = malloccount;
|
|
|
|
m->total_malloced = total_malloced;
|
|
|
|
m->malloced = malloced;
|
|
|
|
m->highwater = highwater;
|
|
|
|
}
|
|
|
|
|
1999-10-13 03:18:53 +00:00
|
|
|
int
|
1998-12-28 04:56:24 +00:00
|
|
|
vinum_mallocinfo(caddr_t data)
|
|
|
|
{
|
|
|
|
struct mc *m = (struct mc *) data;
|
1999-05-02 07:50:38 +00:00
|
|
|
unsigned int ent = m->seq; /* index of entry to return */
|
1998-12-28 04:56:24 +00:00
|
|
|
|
|
|
|
if (ent >= malloccount)
|
|
|
|
return ENOENT;
|
|
|
|
m->address = malloced[ent].address;
|
|
|
|
m->size = malloced[ent].size;
|
|
|
|
m->line = malloced[ent].line;
|
|
|
|
m->seq = malloced[ent].seq;
|
1999-10-13 03:18:53 +00:00
|
|
|
bcopy(malloced[ent].file, m->file, MCFILENAMELEN);
|
1998-12-28 04:56:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-01-29 01:17:54 +00:00
|
|
|
/*
|
|
|
|
* return the nth request trace buffer entry. This
|
1998-12-28 04:56:24 +00:00
|
|
|
* is indexed back from the current entry (which
|
1999-10-13 03:18:53 +00:00
|
|
|
* has index 0)
|
1999-01-29 01:17:54 +00:00
|
|
|
*/
|
1999-10-13 03:18:53 +00:00
|
|
|
int
|
1998-12-28 04:56:24 +00:00
|
|
|
vinum_rqinfo(caddr_t data)
|
|
|
|
{
|
|
|
|
struct rqinfo *rq = (struct rqinfo *) data;
|
|
|
|
int ent = *(int *) data; /* 1st word is index */
|
|
|
|
int lastent = rqip - rqinfo; /* entry number of current entry */
|
|
|
|
|
|
|
|
if (ent >= RQINFO_SIZE) /* out of the table */
|
|
|
|
return ENOENT;
|
|
|
|
if ((ent = lastent - ent - 1) < 0)
|
|
|
|
ent += RQINFO_SIZE; /* roll over backwards */
|
|
|
|
bcopy(&rqinfo[ent], rq, sizeof(struct rqinfo));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|