/*-
* Copyright (c) 1989, 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software developed by the Computer Systems
* Engineering group at Lawrence Berkeley Laboratory under DARPA contract
* BG 91-66 and contributed to Berkeley.
*
* 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. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS 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.
*/
/* If one range contains the start of the other, it's a match. */
if (p1->pa >= p2->pa && p1->pa < p2->pa + p2->sz) {
return 0;
}
if (p2->pa >= p1->pa && p2->pa < p1->pa + p1->sz) {
return 0;
}
/* Otherwise sort by pa. */
if (p1->pa < p2->pa)
return -1;
else if (p1->pa > p2->pa)
return 1;
else
return 0;
}
/*
* Translate a physical address to a file-offset in the crash dump.
*/
off_t
_kvm_pa2off(kvm_t *kd, paddr_t pa)
{
cpu_kcore_hdr_t *cpu_kh;
phys_ram_seg_t *ramsegs;
off_t off;
int i;
if (map == NULL) {
map = calloc(sizeof *map, cpu_kh->nmemsegs);
off = 0;
for (i = 0; i < cpu_kh->nmemsegs; i++) {
map[i].pa = ramsegs[i].start;
map[i].sz = ramsegs[i].size;
map[i].off = off;
off += ramsegs[i].size;
}
#if 0
/* The array appears to be sorted already */
qsort(map, cpu_kh->nmemsegs, sizeof(*map), cmp_p2o);
#endif
}
key.pa = pa;
key.sz = 1;
key.off = -1;
val = bsearch(&key, map, cpu_kh->nmemsegs, sizeof (key), cmp_p2o);
if (val)
off = val->off + pa - val->pa;
else
off = 0;
return (kd->dump_off + off);
}
/*
* Machine-dependent initialization for ALL open kvm descriptors,
* not just those for a kernel crash dump. Some architectures
* have to deal with these NOT being constants! (i.e. m68k)
*/
int
_kvm_mdopen(kvm_t *kd)
{