load.cpp revision 1.3 1 1.3 uch /* $NetBSD: load.cpp,v 1.3 2001/03/22 18:23:08 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.1 uch void
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.1 uch sz = _file->size();
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.1 uch }
90 1.1 uch
91 1.1 uch void
92 1.1 uch Loader::loadEnd(void)
93 1.1 uch {
94 1.1 uch /* tag chain end */
95 1.1 uch _load_segment_end();
96 1.1 uch }
97 1.1 uch
98 1.1 uch void
99 1.1 uch Loader::tagDump(int n)
100 1.1 uch {
101 1.1 uch #ifdef PAGE_LINK_DUMP
102 1.1 uch struct PageTag *p, *op;
103 1.1 uch int i = 0;
104 1.1 uch
105 1.1 uch DPRINTF((TEXT("page tag start physical address: 0x%08x\n"),
106 1.1 uch _page_tag_start));
107 1.1 uch p = reinterpret_cast <struct PageTag *>(_page_tag_start);
108 1.1 uch do {
109 1.1 uch if (i < n || p->src == ~0)
110 1.1 uch DPRINTF((TEXT("[%d] next 0x%08x src 0x%08x dst 0x%08x sz 0x%x\n"),
111 1.1 uch i, p->next, p->src, p->dst, p->sz));
112 1.1 uch else if (i == n)
113 1.1 uch DPRINTF((TEXT("[...]\n")));
114 1.1 uch op = p;
115 1.1 uch i++;
116 1.1 uch } while ((p = reinterpret_cast <struct PageTag *>(p->next)) != ~0);
117 1.1 uch
118 1.1 uch DPRINTF((TEXT("[%d(last)] next 0x%08x src 0x%08x dst 0x%08x sz 0x%x\n"),
119 1.1 uch i - 1, op->next, op->src, op->dst, op->sz));
120 1.1 uch #endif // PAGE_LINK_DUMP
121 1.1 uch }
122 1.1 uch
123 1.1 uch paddr_t
124 1.1 uch Loader::tagStart(void)
125 1.1 uch {
126 1.1 uch return _page_tag_start;
127 1.1 uch }
128 1.1 uch
129 1.1 uch void
130 1.1 uch Loader::_load_segment_start(void)
131 1.1 uch {
132 1.1 uch vaddr_t v;
133 1.1 uch paddr_t p;
134 1.1 uch
135 1.1 uch _nload_link = _n0clr_link = 0;
136 1.1 uch _tpsz = _mem->getTaggedPageSize();
137 1.1 uch
138 1.1 uch // start of chain.
139 1.1 uch _mem->getTaggedPage(v, p, &_pvec_clr, _pvec_clr_paddr);
140 1.1 uch #ifdef PAGE_LINK_DUMP
141 1.1 uch _page_tag_start =(u_int32_t)_pvec_clr;
142 1.1 uch #else
143 1.1 uch _page_tag_start = _pvec_clr_paddr;
144 1.1 uch #endif
145 1.1 uch _pvec_prev = _pvec_clr++;
146 1.1 uch _pvec_clr_paddr += sizeof(struct PageTag);
147 1.1 uch }
148 1.1 uch
149 1.1 uch void
150 1.1 uch Loader::_load_segment_end(void)
151 1.1 uch {
152 1.1 uch _opvec_prev->next = ~0; // terminate
153 1.1 uch DPRINTF((TEXT("load link %d zero clear link %d.\n"),
154 1.1 uch _nload_link, _n0clr_link));
155 1.1 uch }
156 1.1 uch
157 1.1 uch void
158 1.1 uch Loader::_load_segment(vaddr_t kv, vsize_t memsz, off_t fileofs, size_t filesz)
159 1.1 uch {
160 1.1 uch int j, n;
161 1.1 uch vaddr_t kv_start = kv;
162 1.1 uch
163 1.1 uch DPRINTF((TEXT("\t->load 0x%08x+0x%08x=0x%08x ofs=0x%08x+0x%x\n"),
164 1.1 uch kv, memsz, kv + memsz, fileofs, filesz));
165 1.3 uch _kernend = kv + memsz;
166 1.3 uch
167 1.1 uch if (filesz) {
168 1.1 uch n = filesz / _tpsz;
169 1.1 uch for (j = 0; j < n; j++) {
170 1.1 uch _opvec_prev = _pvec_prev;
171 1.1 uch _pvec_prev = _load_page(kv, fileofs,
172 1.1 uch _tpsz, _pvec_prev);
173 1.1 uch kv += _tpsz;
174 1.1 uch fileofs += _tpsz;
175 1.1 uch ++_nload_link;
176 1.1 uch }
177 1.1 uch size_t rest = filesz % _tpsz;
178 1.1 uch if (rest) {
179 1.1 uch _opvec_prev = _pvec_prev;
180 1.1 uch _pvec_prev = _load_page(kv, fileofs, rest, _pvec_prev);
181 1.1 uch ++_nload_link;
182 1.1 uch }
183 1.1 uch }
184 1.1 uch
185 1.1 uch // zero clear tag
186 1.1 uch if (filesz < memsz) {
187 1.1 uch _pvec_prev->src = ~0;
188 1.1 uch _pvec_prev->dst = kv_start + filesz;
189 1.1 uch _pvec_prev->sz = memsz - filesz;
190 1.1 uch #ifdef PAGE_LINK_DUMP
191 1.1 uch _pvec_prev->next =(u_int32_t)_pvec_clr;
192 1.1 uch #else
193 1.1 uch _pvec_prev->next = ptokv(_pvec_clr_paddr);
194 1.1 uch #endif
195 1.1 uch DPRINTF((TEXT("[zero clear] ->0x%08x+0x%08x=0x%08x\n"),
196 1.1 uch _pvec_prev->dst, _pvec_prev->sz,
197 1.1 uch _pvec_prev->dst + _pvec_prev->sz));
198 1.1 uch _opvec_prev = _pvec_prev;
199 1.1 uch _pvec_prev = _pvec_clr++;
200 1.1 uch _pvec_clr_paddr += sizeof(struct PageTag);
201 1.1 uch ++_n0clr_link;
202 1.1 uch }
203 1.2 toshii }
204 1.2 toshii
205 1.2 toshii void
206 1.2 toshii Loader::_load_memory(vaddr_t kv, vsize_t memsz, void *data)
207 1.2 toshii {
208 1.2 toshii struct PageTag *pvec;
209 1.2 toshii vaddr_t kv_start = kv, v;
210 1.2 toshii paddr_t p, pvec_paddr;
211 1.2 toshii
212 1.2 toshii DPRINTF((TEXT("\t->load 0x%08x+0x%08x=0x%08x\n"),
213 1.2 toshii kv, memsz, kv + memsz));
214 1.2 toshii if (memsz > _tpsz) {
215 1.2 toshii /* XXX failure */
216 1.2 toshii return;
217 1.2 toshii }
218 1.2 toshii
219 1.2 toshii _opvec_prev = _pvec_prev;
220 1.2 toshii _mem->getTaggedPage(v, p, &pvec, pvec_paddr);
221 1.2 toshii memcpy((void *)v, data, memsz);
222 1.2 toshii _pvec_prev->src = ptokv(p);
223 1.2 toshii _pvec_prev->dst = kv;
224 1.2 toshii _pvec_prev->sz = memsz;
225 1.2 toshii #ifdef PAGE_LINK_DUMP
226 1.2 toshii _pvec_prev->next =(u_int32_t)pvec;
227 1.2 toshii #else
228 1.2 toshii _pvec_prev->next = ptokv(pvec_paddr);
229 1.2 toshii #endif
230 1.2 toshii _pvec_prev = pvec;
231 1.2 toshii
232 1.2 toshii _kernend = kv + memsz;
233 1.2 toshii ++_nload_link;
234 1.1 uch }
235 1.1 uch
236 1.1 uch struct PageTag *
237 1.1 uch Loader::_load_page(vaddr_t kv, off_t ofs, size_t sz, struct PageTag *prev)
238 1.1 uch {
239 1.1 uch struct PageTag *pvec;
240 1.1 uch paddr_t p, pvec_paddr;
241 1.1 uch vaddr_t v;
242 1.1 uch
243 1.1 uch _mem->getTaggedPage(v, p, &pvec, pvec_paddr);
244 1.1 uch _file->read((void *)v, sz, ofs);
245 1.1 uch prev->src = ptokv(p);
246 1.1 uch prev->dst = kv;
247 1.1 uch prev->sz = sz;
248 1.1 uch #ifdef PAGE_LINK_DUMP
249 1.1 uch prev->next =(u_int32_t)pvec;
250 1.1 uch #else
251 1.1 uch prev->next = ptokv(pvec_paddr);
252 1.1 uch #endif
253 1.1 uch
254 1.1 uch return pvec;
255 1.1 uch }
256