Home | History | Annotate | Line # | Download | only in hpcboot
load.cpp revision 1.13
      1  1.13     uwe /*	$NetBSD: load.cpp,v 1.13 2006/02/20 03:09:05 uwe Exp $	*/
      2   1.1     uch 
      3   1.1     uch /*-
      4   1.1     uch  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5   1.1     uch  * All rights reserved.
      6   1.1     uch  *
      7   1.1     uch  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1     uch  * by UCHIYAMA Yasushi.
      9   1.1     uch  *
     10   1.1     uch  * Redistribution and use in source and binary forms, with or without
     11   1.1     uch  * modification, are permitted provided that the following conditions
     12   1.1     uch  * are met:
     13   1.1     uch  * 1. Redistributions of source code must retain the above copyright
     14   1.1     uch  *    notice, this list of conditions and the following disclaimer.
     15   1.1     uch  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1     uch  *    notice, this list of conditions and the following disclaimer in the
     17   1.1     uch  *    documentation and/or other materials provided with the distribution.
     18   1.1     uch  * 3. All advertising materials mentioning features or use of this software
     19   1.1     uch  *    must display the following acknowledgement:
     20   1.1     uch  *        This product includes software developed by the NetBSD
     21   1.1     uch  *        Foundation, Inc. and its contributors.
     22   1.1     uch  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1     uch  *    contributors may be used to endorse or promote products derived
     24   1.1     uch  *    from this software without specific prior written permission.
     25   1.1     uch  *
     26   1.1     uch  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1     uch  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1     uch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1     uch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1     uch  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1     uch  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1     uch  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1     uch  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1     uch  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1     uch  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1     uch  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1     uch  */
     38   1.1     uch 
     39   1.1     uch #include <load.h>
     40   1.1     uch #include <exec_coff.h>
     41   1.1     uch #undef DPRINTF // trash coff_machdep.h 's define.
     42   1.1     uch 
     43   1.1     uch #include <console.h>
     44   1.1     uch #include <memory.h>
     45   1.1     uch #include <file.h>
     46   1.1     uch 
     47   1.1     uch #include <exec_elf.h>
     48   1.1     uch 
     49   1.1     uch Loader::Loader(Console *&cons, MemoryManager *&mem)
     50   1.1     uch 	: _mem(mem), _cons(cons)
     51   1.1     uch {
     52   1.1     uch 	_file = 0;
     53   1.1     uch 	_page_tag_start = 0;
     54   1.1     uch }
     55   1.1     uch 
     56   1.1     uch LoaderOps
     57   1.1     uch Loader::objectFormat(File &file)
     58   1.1     uch {
     59   1.1     uch 	union {
     60   1.1     uch 		Elf_Ehdr elf;
     61   1.1     uch 		coff_exechdr coff;
     62   1.1     uch 	} header;
     63   1.1     uch 	file.read(reinterpret_cast<void *>(&header), sizeof(header), 0);
     64   1.1     uch 
     65   1.1     uch 	if (header.elf.e_ident[EI_MAG0] == ELFMAG0 &&
     66   1.1     uch 	    header.elf.e_ident[EI_MAG1] == ELFMAG1 &&
     67   1.1     uch 	    header.elf.e_ident[EI_MAG2] == ELFMAG2 &&
     68   1.1     uch 	    header.elf.e_ident[EI_MAG3] == ELFMAG3)
     69   1.1     uch 		return LOADER_ELF;
     70   1.1     uch #ifdef COFF_BADMAG
     71   1.1     uch 	if (!COFF_BADMAG(&header.coff.f))
     72   1.1     uch 		return LOADER_COFF;
     73   1.1     uch #endif // COFF_BADMAG
     74   1.1     uch 
     75   1.1     uch 	return LOADER_UNKNOWN;
     76   1.1     uch }
     77   1.1     uch 
     78   1.5     uch BOOL
     79   1.1     uch Loader::loadExtData(void)
     80   1.1     uch {
     81   1.1     uch 	size_t sz;
     82   1.1     uch 	vaddr_t kv;
     83   1.1     uch 
     84   1.6     uwe 	sz = _file->realsize();
     85   1.1     uch 	kv = ROUND(_kernend, static_cast <vsize_t>(KERNEL_PAGE_SIZE));
     86   1.1     uch 
     87  1.11     uwe 	DPRINTF((TEXT("[file system image]\n")));
     88   1.1     uch 	_load_segment(kv, sz, 0, sz);
     89   1.5     uch 
     90   1.5     uch 	return _load_success();
     91   1.1     uch }
     92   1.1     uch 
     93   1.1     uch void
     94   1.1     uch Loader::loadEnd(void)
     95   1.7     uch {
     96   1.1     uch 	/* tag chain end */
     97   1.1     uch 	_load_segment_end();
     98   1.1     uch }
     99   1.1     uch 
    100   1.1     uch void
    101   1.1     uch Loader::tagDump(int n)
    102   1.1     uch {
    103   1.1     uch #ifdef PAGE_LINK_DUMP
    104   1.1     uch 	struct PageTag *p, *op;
    105   1.1     uch 	int i = 0;
    106   1.7     uch 
    107   1.1     uch 	DPRINTF((TEXT("page tag start physical address: 0x%08x\n"),
    108   1.4     uch 	    _page_tag_start));
    109   1.1     uch 	p = reinterpret_cast <struct PageTag *>(_page_tag_start);
    110   1.1     uch 	do  {
    111   1.1     uch 		if (i < n || p->src == ~0)
    112   1.1     uch 			DPRINTF((TEXT("[%d] next 0x%08x src 0x%08x dst 0x%08x sz 0x%x\n"),
    113   1.4     uch 			    i, p->next, p->src, p->dst, p->sz));
    114   1.1     uch 		else if (i == n)
    115   1.1     uch 			DPRINTF((TEXT("[...]\n")));
    116   1.1     uch 		op = p;
    117   1.1     uch 		i++;
    118   1.1     uch 	} while ((p = reinterpret_cast <struct PageTag *>(p->next)) != ~0);
    119   1.7     uch 
    120   1.1     uch 	DPRINTF((TEXT("[%d(last)] next 0x%08x src 0x%08x dst 0x%08x sz 0x%x\n"),
    121   1.4     uch 	    i - 1, op->next, op->src, op->dst, op->sz));
    122   1.1     uch #endif // PAGE_LINK_DUMP
    123   1.1     uch }
    124   1.1     uch 
    125   1.1     uch paddr_t
    126   1.1     uch Loader::tagStart(void)
    127   1.7     uch {
    128   1.1     uch 	return _page_tag_start;
    129   1.1     uch }
    130   1.1     uch 
    131   1.1     uch void
    132   1.1     uch Loader::_load_segment_start(void)
    133   1.1     uch {
    134   1.1     uch 	vaddr_t v;
    135   1.1     uch 	paddr_t p;
    136   1.1     uch 
    137   1.5     uch 	_error = FALSE;
    138   1.1     uch 	_nload_link = _n0clr_link = 0;
    139   1.1     uch 	_tpsz = _mem->getTaggedPageSize();
    140   1.1     uch 
    141   1.1     uch 	// start of chain.
    142   1.5     uch 	if (!_mem->getTaggedPage(v, p, &_pvec_clr, _pvec_clr_paddr))
    143   1.5     uch 		_error = TRUE;
    144   1.1     uch #ifdef PAGE_LINK_DUMP
    145  1.10     uwe 	_page_tag_start = (uint32_t)_pvec_clr;
    146   1.1     uch #else
    147   1.1     uch 	_page_tag_start = _pvec_clr_paddr;
    148   1.1     uch #endif
    149   1.1     uch 	_pvec_prev = _pvec_clr++;
    150   1.1     uch 	_pvec_clr_paddr += sizeof(struct PageTag);
    151   1.1     uch }
    152   1.1     uch 
    153   1.1     uch void
    154   1.1     uch Loader::_load_segment_end(void)
    155   1.1     uch {
    156   1.1     uch 	_opvec_prev->next = ~0; // terminate
    157  1.13     uwe 	DPRINTF((TEXT("load link %d, zero clear link %d\n"),
    158   1.4     uch 	    _nload_link, _n0clr_link));
    159   1.1     uch }
    160   1.1     uch 
    161   1.1     uch void
    162   1.1     uch Loader::_load_segment(vaddr_t kv, vsize_t memsz, off_t fileofs, size_t filesz)
    163   1.1     uch {
    164   1.1     uch 	int j, n;
    165   1.1     uch 	vaddr_t kv_start = kv;
    166   1.1     uch 
    167   1.1     uch 	DPRINTF((TEXT("\t->load 0x%08x+0x%08x=0x%08x ofs=0x%08x+0x%x\n"),
    168   1.4     uch 	    kv, memsz, kv + memsz, fileofs, filesz));
    169   1.3     uch 	_kernend = kv + memsz;
    170   1.3     uch 
    171   1.1     uch 	if (filesz) {
    172   1.1     uch 		n = filesz / _tpsz;
    173   1.1     uch 		for (j = 0; j < n; j++) {
    174   1.1     uch 			_opvec_prev = _pvec_prev;
    175   1.1     uch 			_pvec_prev = _load_page(kv, fileofs,
    176   1.4     uch 			    _tpsz, _pvec_prev);
    177   1.1     uch 			kv += _tpsz;
    178   1.1     uch 			fileofs += _tpsz;
    179   1.1     uch 			++_nload_link;
    180   1.1     uch 		}
    181   1.1     uch 		size_t rest = filesz % _tpsz;
    182   1.1     uch 		if (rest) {
    183   1.1     uch 			_opvec_prev = _pvec_prev;
    184   1.1     uch 			_pvec_prev = _load_page(kv, fileofs, rest, _pvec_prev);
    185   1.1     uch 			++_nload_link;
    186   1.1     uch 		}
    187   1.1     uch 	}
    188   1.1     uch 
    189   1.1     uch 	// zero clear tag
    190   1.1     uch 	if (filesz < memsz) {
    191   1.1     uch 		_pvec_prev->src = ~0;
    192   1.1     uch 		_pvec_prev->dst = kv_start + filesz;
    193   1.1     uch 		_pvec_prev->sz = memsz - filesz;
    194   1.1     uch #ifdef PAGE_LINK_DUMP
    195  1.10     uwe 		_pvec_prev->next = (uint32_t)_pvec_clr;
    196   1.1     uch #else
    197   1.1     uch 		_pvec_prev->next = ptokv(_pvec_clr_paddr);
    198   1.1     uch #endif
    199   1.9     uwe 		DPRINTF((TEXT("\t->zero 0x%08x+0x%08x=0x%08x\n"),
    200   1.4     uch 		    _pvec_prev->dst, _pvec_prev->sz,
    201   1.4     uch 		    _pvec_prev->dst + _pvec_prev->sz));
    202   1.1     uch 		_opvec_prev = _pvec_prev;
    203   1.1     uch 		_pvec_prev = _pvec_clr++;
    204   1.1     uch 		_pvec_clr_paddr += sizeof(struct PageTag);
    205   1.1     uch 		++_n0clr_link;
    206   1.1     uch 	}
    207   1.2  toshii }
    208   1.2  toshii 
    209   1.2  toshii void
    210   1.2  toshii Loader::_load_memory(vaddr_t kv, vsize_t memsz, void *data)
    211   1.2  toshii {
    212   1.2  toshii 	struct PageTag *pvec;
    213   1.2  toshii 	paddr_t p, pvec_paddr;
    214  1.10     uwe 	vaddr_t v;
    215  1.12     uwe 	vaddr_t dst;
    216  1.12     uwe 	vsize_t remsz;
    217   1.2  toshii 
    218   1.2  toshii 	DPRINTF((TEXT("\t->load 0x%08x+0x%08x=0x%08x\n"),
    219   1.4     uch 	    kv, memsz, kv + memsz));
    220   1.2  toshii 
    221  1.12     uwe 	dst = kv;
    222  1.12     uwe 	remsz = memsz;
    223  1.12     uwe 	while (remsz > 0) {
    224  1.12     uwe 		_opvec_prev = _pvec_prev;
    225  1.12     uwe 		if (!_mem->getTaggedPage(v, p, &pvec, pvec_paddr))
    226  1.12     uwe 			_error = TRUE;
    227  1.12     uwe 
    228  1.12     uwe 		vsize_t tocopy = (remsz < _tpsz) ? remsz : _tpsz;
    229  1.12     uwe 		memcpy((void *)v, data, tocopy);
    230  1.12     uwe 		_pvec_prev->src = ptokv(p);
    231  1.12     uwe 		_pvec_prev->dst = dst;
    232  1.12     uwe 		_pvec_prev->sz = tocopy;
    233   1.2  toshii #ifdef PAGE_LINK_DUMP
    234  1.12     uwe 		_pvec_prev->next = (uint32_t)pvec;
    235   1.2  toshii #else
    236  1.12     uwe 		_pvec_prev->next = ptokv(pvec_paddr);
    237   1.2  toshii #endif
    238  1.12     uwe 		data = (char *)data + tocopy;
    239  1.12     uwe 		dst += tocopy;
    240  1.12     uwe 		remsz -= tocopy;
    241  1.12     uwe 
    242  1.12     uwe 		_pvec_prev = pvec;
    243  1.12     uwe 	}
    244   1.2  toshii 
    245   1.2  toshii 	_kernend = kv + memsz;
    246   1.2  toshii 	++_nload_link;
    247   1.1     uch }
    248   1.1     uch 
    249   1.1     uch struct PageTag *
    250   1.1     uch Loader::_load_page(vaddr_t kv, off_t ofs, size_t sz, struct PageTag *prev)
    251   1.1     uch {
    252   1.1     uch 	struct PageTag *pvec;
    253   1.1     uch 	paddr_t p, pvec_paddr;
    254   1.1     uch 	vaddr_t v;
    255   1.1     uch 
    256   1.5     uch 	if (!_mem->getTaggedPage(v, p, &pvec, pvec_paddr))
    257   1.5     uch 		_error = TRUE;
    258   1.1     uch 	_file->read((void *)v, sz, ofs);
    259   1.1     uch 	prev->src = ptokv(p);
    260   1.1     uch 	prev->dst = kv;
    261   1.1     uch 	prev->sz = sz;
    262   1.1     uch #ifdef PAGE_LINK_DUMP
    263  1.10     uwe 	prev->next = (uint32_t)pvec;
    264   1.1     uch #else
    265   1.1     uch 	prev->next = ptokv(pvec_paddr);
    266   1.1     uch #endif
    267   1.1     uch 
    268   1.1     uch 	return pvec;
    269   1.1     uch }
    270