load.cpp revision 1.7 1 1.7 uch /* $NetBSD: load.cpp,v 1.7 2004/08/06 18:33:09 uch 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.1 uch DPRINTF((TEXT("[file system image]")));
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.1 uch _page_tag_start =(u_int32_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.1 uch 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.1 uch _pvec_prev->next =(u_int32_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.7 uch DPRINTF((TEXT("[zero clear] ->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 vaddr_t kv_start = kv, v;
214 1.2 toshii paddr_t p, pvec_paddr;
215 1.2 toshii
216 1.2 toshii DPRINTF((TEXT("\t->load 0x%08x+0x%08x=0x%08x\n"),
217 1.4 uch kv, memsz, kv + memsz));
218 1.2 toshii if (memsz > _tpsz) {
219 1.2 toshii /* XXX failure */
220 1.2 toshii return;
221 1.2 toshii }
222 1.2 toshii
223 1.2 toshii _opvec_prev = _pvec_prev;
224 1.5 uch if (!_mem->getTaggedPage(v, p, &pvec, pvec_paddr))
225 1.5 uch _error = TRUE;
226 1.2 toshii memcpy((void *)v, data, memsz);
227 1.2 toshii _pvec_prev->src = ptokv(p);
228 1.2 toshii _pvec_prev->dst = kv;
229 1.2 toshii _pvec_prev->sz = memsz;
230 1.2 toshii #ifdef PAGE_LINK_DUMP
231 1.2 toshii _pvec_prev->next =(u_int32_t)pvec;
232 1.2 toshii #else
233 1.2 toshii _pvec_prev->next = ptokv(pvec_paddr);
234 1.2 toshii #endif
235 1.2 toshii _pvec_prev = pvec;
236 1.2 toshii
237 1.2 toshii _kernend = kv + memsz;
238 1.2 toshii ++_nload_link;
239 1.1 uch }
240 1.1 uch
241 1.1 uch struct PageTag *
242 1.1 uch Loader::_load_page(vaddr_t kv, off_t ofs, size_t sz, struct PageTag *prev)
243 1.1 uch {
244 1.1 uch struct PageTag *pvec;
245 1.1 uch paddr_t p, pvec_paddr;
246 1.1 uch vaddr_t v;
247 1.1 uch
248 1.5 uch if (!_mem->getTaggedPage(v, p, &pvec, pvec_paddr))
249 1.5 uch _error = TRUE;
250 1.1 uch _file->read((void *)v, sz, ofs);
251 1.1 uch prev->src = ptokv(p);
252 1.1 uch prev->dst = kv;
253 1.1 uch prev->sz = sz;
254 1.1 uch #ifdef PAGE_LINK_DUMP
255 1.1 uch prev->next =(u_int32_t)pvec;
256 1.1 uch #else
257 1.1 uch prev->next = ptokv(pvec_paddr);
258 1.1 uch #endif
259 1.1 uch
260 1.1 uch return pvec;
261 1.1 uch }
262