kloader.c revision 1.20 1 1.20 matt /* $NetBSD: kloader.c,v 1.20 2009/12/14 00:52:38 matt Exp $ */
2 1.1 uch
3 1.1 uch /*-
4 1.1 uch * Copyright (c) 2001, 2002, 2004 The NetBSD Foundation, Inc.
5 1.1 uch * All rights reserved.
6 1.1 uch *
7 1.1 uch * Redistribution and use in source and binary forms, with or without
8 1.1 uch * modification, are permitted provided that the following conditions
9 1.1 uch * are met:
10 1.1 uch * 1. Redistributions of source code must retain the above copyright
11 1.1 uch * notice, this list of conditions and the following disclaimer.
12 1.1 uch * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 uch * notice, this list of conditions and the following disclaimer in the
14 1.1 uch * documentation and/or other materials provided with the distribution.
15 1.1 uch *
16 1.1 uch * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 uch * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 uch * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 uch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 uch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 uch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 uch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 uch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 uch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 uch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 uch * POSSIBILITY OF SUCH DAMAGE.
27 1.1 uch */
28 1.1 uch
29 1.1 uch #include <sys/cdefs.h>
30 1.20 matt __KERNEL_RCSID(0, "$NetBSD: kloader.c,v 1.20 2009/12/14 00:52:38 matt Exp $");
31 1.1 uch
32 1.1 uch #include "debug_kloader.h"
33 1.1 uch
34 1.1 uch #include <sys/param.h>
35 1.1 uch #include <sys/systm.h>
36 1.1 uch #include <sys/malloc.h>
37 1.1 uch #include <sys/proc.h>
38 1.1 uch #include <sys/vnode.h>
39 1.1 uch #include <sys/namei.h>
40 1.1 uch #include <sys/fcntl.h>
41 1.1 uch #define ELFSIZE 32
42 1.1 uch #include <sys/exec_elf.h>
43 1.1 uch
44 1.1 uch #include <uvm/uvm_extern.h>
45 1.1 uch
46 1.1 uch #include <machine/kloader.h>
47 1.1 uch
48 1.1 uch #define PRINTF(fmt, args...) printf("kloader: " fmt, ##args)
49 1.1 uch
50 1.1 uch #ifdef KLOADER_DEBUG
51 1.1 uch int kloader_debug = 1;
52 1.1 uch #define DPRINTF(fmt, args...) \
53 1.1 uch if (kloader_debug) \
54 1.15 perry printf("%s: " fmt, __func__ , ##args)
55 1.1 uch #define _DPRINTF(fmt, args...) \
56 1.1 uch if (kloader_debug) \
57 1.1 uch printf(fmt, ##args)
58 1.1 uch #define DPRINTFN(n, fmt, args...) \
59 1.1 uch if (kloader_debug > (n)) \
60 1.15 perry printf("%s: " fmt, __func__ , ##args)
61 1.1 uch #define _DPRINTFN(n, fmt, args...) \
62 1.1 uch if (kloader_debug > (n)) \
63 1.1 uch printf(fmt, ##args)
64 1.1 uch #define STATIC
65 1.1 uch #else
66 1.1 uch #define DPRINTF(fmt, args...) ((void)0)
67 1.1 uch #define _DPRINTF(fmt, args...) ((void)0)
68 1.1 uch #define DPRINTFN(n, fmt, args...) ((void)0)
69 1.1 uch #define _DPRINTFN(n, fmt, args...) ((void)0)
70 1.1 uch #define STATIC static
71 1.1 uch #endif
72 1.1 uch
73 1.1 uch struct kloader {
74 1.1 uch struct pglist pg_head;
75 1.1 uch struct vm_page *cur_pg;
76 1.1 uch struct kloader_page_tag *cur_tag;
77 1.1 uch struct vnode *vp;
78 1.1 uch struct kloader_page_tag *tagstart;
79 1.1 uch struct kloader_bootinfo *bootinfo;
80 1.1 uch struct kloader_bootinfo *rebootinfo;
81 1.1 uch vaddr_t loader_sp;
82 1.1 uch kloader_bootfunc_t *loader;
83 1.1 uch int setuped;
84 1.1 uch int called;
85 1.1 uch struct kloader_ops *ops;
86 1.1 uch };
87 1.1 uch
88 1.1 uch #define BUCKET_SIZE (PAGE_SIZE - sizeof(struct kloader_page_tag))
89 1.7 christos #define KLOADER_LWP (&lwp0)
90 1.1 uch STATIC struct kloader kloader;
91 1.1 uch
92 1.1 uch #define ROUND4(x) (((x) + 3) & ~3)
93 1.1 uch
94 1.1 uch STATIC int kloader_load(void);
95 1.1 uch
96 1.1 uch STATIC int kloader_alloc_memory(size_t);
97 1.1 uch STATIC struct kloader_page_tag *kloader_get_tag(vaddr_t);
98 1.1 uch STATIC void kloader_from_file(vaddr_t, off_t, size_t);
99 1.1 uch STATIC void kloader_copy(vaddr_t, const void *, size_t);
100 1.1 uch STATIC void kloader_zero(vaddr_t, size_t);
101 1.1 uch
102 1.1 uch STATIC void kloader_load_segment(Elf_Phdr *);
103 1.1 uch
104 1.1 uch STATIC struct vnode *kloader_open(const char *);
105 1.1 uch STATIC void kloader_close(void);
106 1.1 uch STATIC int kloader_read(size_t, size_t, void *);
107 1.1 uch
108 1.1 uch #ifdef KLOADER_DEBUG
109 1.1 uch STATIC void kloader_pagetag_dump(void);
110 1.1 uch #endif
111 1.1 uch
112 1.1 uch void
113 1.1 uch __kloader_reboot_setup(struct kloader_ops *ops, const char *filename)
114 1.1 uch {
115 1.1 uch
116 1.1 uch if (kloader.bootinfo == NULL) {
117 1.1 uch PRINTF("No bootinfo.\n");
118 1.1 uch return;
119 1.1 uch }
120 1.1 uch
121 1.1 uch if (ops == NULL || ops->jump == NULL || ops->boot == NULL) {
122 1.1 uch PRINTF("No boot operations.\n");
123 1.1 uch return;
124 1.1 uch }
125 1.1 uch kloader.ops = ops;
126 1.1 uch
127 1.1 uch if (kloader.called++ == 0) {
128 1.1 uch PRINTF("kernel file name: %s\n", filename);
129 1.1 uch kloader.vp = kloader_open(filename);
130 1.1 uch if (kloader.vp == NULL)
131 1.1 uch return;
132 1.1 uch
133 1.1 uch if (kloader_load() == 0) {
134 1.1 uch kloader.setuped = TRUE;
135 1.1 uch #ifdef KLOADER_DEBUG
136 1.1 uch kloader_pagetag_dump();
137 1.1 uch #endif
138 1.1 uch }
139 1.1 uch kloader_close();
140 1.1 uch } else {
141 1.1 uch /* Fatal case. reboot from DDB etc. */
142 1.1 uch kloader_reboot();
143 1.1 uch }
144 1.1 uch }
145 1.1 uch
146 1.1 uch
147 1.1 uch void
148 1.19 cegger kloader_reboot(void)
149 1.1 uch {
150 1.1 uch
151 1.1 uch if (kloader.setuped) {
152 1.1 uch PRINTF("Rebooting...\n");
153 1.1 uch (*kloader.ops->jump)(kloader.loader, kloader.loader_sp,
154 1.1 uch kloader.rebootinfo, kloader.tagstart);
155 1.1 uch }
156 1.1 uch
157 1.1 uch if (kloader.ops->reset != NULL) {
158 1.4 peter PRINTF("Resetting...\n");
159 1.1 uch (*kloader.ops->reset)();
160 1.1 uch }
161 1.1 uch while (/*CONSTCOND*/1)
162 1.1 uch ;
163 1.1 uch /* NOTREACHED */
164 1.1 uch }
165 1.1 uch
166 1.1 uch
167 1.1 uch int
168 1.19 cegger kloader_load(void)
169 1.1 uch {
170 1.1 uch Elf_Ehdr eh;
171 1.1 uch Elf_Phdr *ph, *p;
172 1.1 uch Elf_Shdr *sh;
173 1.1 uch Elf_Addr entry;
174 1.1 uch vaddr_t kv;
175 1.1 uch size_t sz;
176 1.1 uch size_t shstrsz;
177 1.1 uch char *shstrtab;
178 1.1 uch int symndx, strndx;
179 1.1 uch size_t ksymsz;
180 1.1 uch struct kloader_bootinfo nbi; /* new boot info */
181 1.1 uch char *oldbuf, *newbuf;
182 1.1 uch char **ap;
183 1.1 uch int i;
184 1.1 uch
185 1.1 uch ph = NULL;
186 1.1 uch sh = NULL;
187 1.1 uch shstrtab = NULL;
188 1.1 uch
189 1.1 uch /* read kernel's ELF header */
190 1.1 uch kloader_read(0, sizeof(Elf_Ehdr), &eh);
191 1.1 uch
192 1.1 uch if (eh.e_ident[EI_MAG0] != ELFMAG0 ||
193 1.1 uch eh.e_ident[EI_MAG1] != ELFMAG1 ||
194 1.1 uch eh.e_ident[EI_MAG2] != ELFMAG2 ||
195 1.1 uch eh.e_ident[EI_MAG3] != ELFMAG3) {
196 1.1 uch PRINTF("not an ELF file\n");
197 1.1 uch goto err;
198 1.1 uch }
199 1.1 uch
200 1.1 uch /* read program headers */
201 1.1 uch sz = eh.e_phentsize * eh.e_phnum;
202 1.1 uch if ((ph = malloc(sz, M_TEMP, M_NOWAIT)) == NULL) {
203 1.1 uch PRINTF("can't allocate program header table.\n");
204 1.1 uch goto err;
205 1.1 uch }
206 1.1 uch if (kloader_read(eh.e_phoff, sz, ph) != 0) {
207 1.1 uch PRINTF("program header read error.\n");
208 1.1 uch goto err;
209 1.1 uch }
210 1.1 uch
211 1.1 uch /* read section headers */
212 1.1 uch sz = eh.e_shentsize * eh.e_shnum;
213 1.1 uch if ((sh = malloc(sz, M_TEMP, M_NOWAIT)) == NULL) {
214 1.1 uch PRINTF("can't allocate section header table.\n");
215 1.1 uch goto err;
216 1.1 uch }
217 1.1 uch if (kloader_read(eh.e_shoff, eh.e_shentsize * eh.e_shnum, sh) != 0) {
218 1.1 uch PRINTF("section header read error.\n");
219 1.1 uch goto err;
220 1.1 uch }
221 1.1 uch
222 1.1 uch /* read section names */
223 1.1 uch shstrsz = ROUND4(sh[eh.e_shstrndx].sh_size);
224 1.1 uch shstrtab = malloc(shstrsz, M_TEMP, M_NOWAIT);
225 1.1 uch if (shstrtab == NULL) {
226 1.1 uch PRINTF("unable to allocate memory for .shstrtab\n");
227 1.1 uch goto err;
228 1.1 uch }
229 1.1 uch DPRINTF("reading 0x%x bytes of .shstrtab at 0x%x\n",
230 1.1 uch sh[eh.e_shstrndx].sh_size, sh[eh.e_shstrndx].sh_offset);
231 1.1 uch kloader_read(sh[eh.e_shstrndx].sh_offset, sh[eh.e_shstrndx].sh_size,
232 1.1 uch shstrtab);
233 1.1 uch
234 1.1 uch /* save entry point, code to construct symbol table overwrites it */
235 1.1 uch entry = eh.e_entry;
236 1.1 uch
237 1.1 uch
238 1.1 uch /*
239 1.4 peter * Calculate memory size
240 1.1 uch */
241 1.1 uch sz = 0;
242 1.1 uch
243 1.1 uch /* loadable segments */
244 1.1 uch for (i = 0; i < eh.e_phnum; i++) {
245 1.1 uch if (ph[i].p_type == PT_LOAD) {
246 1.1 uch DPRINTF("segment %d size = file 0x%x memory 0x%x\n",
247 1.1 uch i, ph[i].p_filesz, ph[i].p_memsz);
248 1.1 uch #ifdef KLOADER_ZERO_BSS
249 1.1 uch sz += round_page(ph[i].p_memsz);
250 1.1 uch #else
251 1.1 uch sz += round_page(ph[i].p_filesz);
252 1.1 uch #endif
253 1.1 uch sz += PAGE_SIZE; /* compensate for partial last tag */
254 1.1 uch }
255 1.1 uch }
256 1.1 uch
257 1.1 uch if (sz == 0) /* nothing to load? */
258 1.1 uch goto err;
259 1.1 uch
260 1.1 uch /* symbols/strings sections */
261 1.1 uch symndx = strndx = -1;
262 1.1 uch for (i = 0; i < eh.e_shnum; i++) {
263 1.1 uch if (strcmp(shstrtab + sh[i].sh_name, ".symtab") == 0)
264 1.1 uch symndx = i;
265 1.1 uch else if (strcmp(shstrtab + sh[i].sh_name, ".strtab") == 0)
266 1.1 uch strndx = i;
267 1.1 uch else if (i != eh.e_shstrndx)
268 1.1 uch /* while here, mark all other sections as unused */
269 1.1 uch sh[i].sh_type = SHT_NULL;
270 1.1 uch }
271 1.1 uch
272 1.1 uch if (symndx < 0 || strndx < 0) {
273 1.1 uch if (symndx < 0)
274 1.1 uch PRINTF("no .symtab section\n");
275 1.1 uch if (strndx < 0)
276 1.1 uch PRINTF("no .strtab section\n");
277 1.1 uch ksymsz = SELFMAG; /* just a bad magic */
278 1.1 uch } else {
279 1.1 uch ksymsz = sizeof(Elf_Ehdr)
280 1.1 uch + eh.e_shentsize * eh.e_shnum
281 1.1 uch + shstrsz /* rounded to 4 bytes */
282 1.1 uch + sh[symndx].sh_size
283 1.1 uch + sh[strndx].sh_size;
284 1.6 peter DPRINTF("ksyms size = 0x%zx\n", ksymsz);
285 1.1 uch }
286 1.1 uch sz += ROUND4(ksymsz);
287 1.1 uch
288 1.1 uch /* boot info for the new kernel */
289 1.1 uch sz += sizeof(struct kloader_bootinfo);
290 1.1 uch
291 1.1 uch /* get memory for new kernel */
292 1.1 uch if (kloader_alloc_memory(sz) != 0)
293 1.1 uch goto err;
294 1.1 uch
295 1.1 uch
296 1.1 uch /*
297 1.1 uch * Copy new kernel in.
298 1.1 uch */
299 1.1 uch kv = 0; /* XXX: -Wuninitialized */
300 1.1 uch for (i = 0, p = ph; i < eh.e_phnum; i++, p++) {
301 1.1 uch if (p->p_type == PT_LOAD) {
302 1.1 uch kloader_load_segment(p);
303 1.1 uch kv = p->p_vaddr + ROUND4(p->p_memsz);
304 1.1 uch }
305 1.1 uch }
306 1.1 uch
307 1.1 uch
308 1.1 uch /*
309 1.1 uch * Construct symbol table for ksyms.
310 1.1 uch */
311 1.1 uch if (symndx < 0 || strndx < 0) {
312 1.1 uch kloader_zero(kv, SELFMAG);
313 1.1 uch kv += SELFMAG;
314 1.1 uch } else {
315 1.1 uch Elf_Off eoff;
316 1.1 uch off_t symoff, stroff;
317 1.1 uch
318 1.1 uch /* save offsets of .symtab and .strtab before we change them */
319 1.1 uch symoff = sh[symndx].sh_offset;
320 1.1 uch stroff = sh[strndx].sh_offset;
321 1.1 uch
322 1.1 uch /* no loadable segments */
323 1.1 uch eh.e_entry = 0;
324 1.1 uch eh.e_phnum = 0;
325 1.1 uch eh.e_phoff = 0;
326 1.1 uch
327 1.1 uch /* change offsets to reflect new layout */
328 1.1 uch eoff = sizeof(Elf_Ehdr);
329 1.1 uch eh.e_shoff = eoff;
330 1.1 uch
331 1.1 uch eoff += eh.e_shentsize * eh.e_shnum;
332 1.1 uch sh[eh.e_shstrndx].sh_offset = eoff;
333 1.1 uch
334 1.1 uch eoff += shstrsz;
335 1.1 uch sh[symndx].sh_offset = eoff;
336 1.1 uch
337 1.1 uch eoff += sh[symndx].sh_size;
338 1.1 uch sh[strndx].sh_offset = eoff;
339 1.1 uch
340 1.1 uch /* local copies massaged, can serve them now */
341 1.1 uch DPRINTF("ksyms ELF header\n");
342 1.1 uch kloader_copy(kv, &eh, sizeof(Elf_Ehdr));
343 1.1 uch kv += sizeof(Elf_Ehdr);
344 1.1 uch
345 1.1 uch DPRINTF("ksyms section headers\n");
346 1.1 uch kloader_copy(kv, sh, eh.e_shentsize * eh.e_shnum);
347 1.1 uch kv += eh.e_shentsize * eh.e_shnum;
348 1.1 uch
349 1.1 uch DPRINTF("ksyms .shstrtab\n");
350 1.1 uch kloader_copy(kv, shstrtab, shstrsz);
351 1.1 uch kv += shstrsz;
352 1.1 uch
353 1.1 uch DPRINTF("ksyms .symtab\n");
354 1.1 uch kloader_from_file(kv, symoff, sh[symndx].sh_size);
355 1.1 uch kv += sh[symndx].sh_size;
356 1.1 uch
357 1.1 uch DPRINTF("ksyms .strtab\n");
358 1.1 uch kloader_from_file(kv, stroff, ROUND4(sh[strndx].sh_size));
359 1.1 uch kv += ROUND4(sh[strndx].sh_size);
360 1.1 uch }
361 1.1 uch
362 1.1 uch /*
363 1.1 uch * Create boot info to pass to the new kernel.
364 1.1 uch * All pointers in it are *not* valid until the new kernel runs!
365 1.1 uch */
366 1.1 uch
367 1.1 uch /* get a private copy of current bootinfo to vivisect */
368 1.1 uch memcpy(&nbi, kloader.bootinfo,
369 1.1 uch sizeof(struct kloader_bootinfo));
370 1.1 uch
371 1.1 uch /* new kernel entry point */
372 1.1 uch nbi.entry = entry;
373 1.1 uch
374 1.1 uch /* where args currently are, see kloader_bootinfo_set() */
375 1.1 uch oldbuf = &kloader.bootinfo->_argbuf[0];
376 1.1 uch
377 1.1 uch /* where args *will* be after boot code copied them */
378 1.1 uch newbuf = (char *)(void *)kv
379 1.1 uch + offsetof(struct kloader_bootinfo, _argbuf);
380 1.1 uch
381 1.1 uch DPRINTF("argv: old %p -> new %p\n", oldbuf, newbuf);
382 1.1 uch
383 1.1 uch /* not a valid pointer in this kernel! */
384 1.1 uch nbi.argv = (void *)newbuf;
385 1.1 uch
386 1.1 uch /* local copy that we populate with new (not yet valid) pointers */
387 1.1 uch ap = (char **)(void *)nbi._argbuf;
388 1.1 uch
389 1.1 uch for (i = 0; i < kloader.bootinfo->argc; ++i) {
390 1.1 uch DPRINTFN(1, " [%d]: %p -> ", i, kloader.bootinfo->argv[i]);
391 1.1 uch ap[i] = newbuf +
392 1.1 uch (kloader.bootinfo->argv[i] - oldbuf);
393 1.1 uch _DPRINTFN(1, "%p\n", ap[i]);
394 1.1 uch }
395 1.1 uch
396 1.1 uch /* arrange for the new bootinfo to get copied */
397 1.1 uch DPRINTF("bootinfo\n");
398 1.1 uch kloader_copy(kv, &nbi, sizeof(struct kloader_bootinfo));
399 1.1 uch
400 1.1 uch /* will be valid by the time the new kernel starts */
401 1.1 uch kloader.rebootinfo = (void *)kv;
402 1.1 uch /* kv += sizeof(struct kloader_bootinfo); */
403 1.1 uch
404 1.1 uch /*
405 1.1 uch * Copy loader code
406 1.1 uch */
407 1.1 uch KDASSERT(kloader.cur_pg);
408 1.1 uch kloader.loader = (void *)PG_VADDR(kloader.cur_pg);
409 1.1 uch memcpy(kloader.loader, kloader.ops->boot, PAGE_SIZE);
410 1.1 uch
411 1.1 uch /* loader stack starts at the bottom of that page */
412 1.1 uch kloader.loader_sp = (vaddr_t)kloader.loader + PAGE_SIZE;
413 1.1 uch
414 1.1 uch DPRINTF("[loader] addr=%p sp=%p [kernel] entry=%p\n",
415 1.1 uch kloader.loader, (void *)kloader.loader_sp, (void *)nbi.entry);
416 1.1 uch
417 1.1 uch return (0);
418 1.1 uch err:
419 1.1 uch if (ph != NULL)
420 1.1 uch free(ph, M_TEMP);
421 1.1 uch if (sh != NULL)
422 1.1 uch free(sh, M_TEMP);
423 1.1 uch if (shstrtab != NULL)
424 1.1 uch free(shstrtab, M_TEMP);
425 1.1 uch
426 1.1 uch return 1;
427 1.1 uch }
428 1.1 uch
429 1.1 uch
430 1.1 uch int
431 1.1 uch kloader_alloc_memory(size_t sz)
432 1.1 uch {
433 1.1 uch extern paddr_t avail_start, avail_end;
434 1.1 uch int n, error;
435 1.1 uch
436 1.1 uch n = (sz + BUCKET_SIZE - 1) / BUCKET_SIZE /* kernel &co */
437 1.1 uch + 1; /* 2nd loader */
438 1.1 uch
439 1.1 uch error = uvm_pglistalloc(n * PAGE_SIZE, avail_start, avail_end,
440 1.1 uch PAGE_SIZE, 0, &kloader.pg_head, n, 0);
441 1.1 uch if (error) {
442 1.1 uch PRINTF("can't allocate memory.\n");
443 1.1 uch return (1);
444 1.1 uch }
445 1.1 uch DPRINTF("allocated %d pages.\n", n);
446 1.1 uch
447 1.1 uch kloader.cur_pg = TAILQ_FIRST(&kloader.pg_head);
448 1.1 uch kloader.tagstart = (void *)PG_VADDR(kloader.cur_pg);
449 1.1 uch kloader.cur_tag = NULL;
450 1.1 uch
451 1.1 uch return (0);
452 1.1 uch }
453 1.1 uch
454 1.1 uch
455 1.1 uch struct kloader_page_tag *
456 1.1 uch kloader_get_tag(vaddr_t dst)
457 1.1 uch {
458 1.1 uch struct vm_page *pg;
459 1.1 uch vaddr_t addr;
460 1.1 uch struct kloader_page_tag *tag;
461 1.1 uch
462 1.1 uch tag = kloader.cur_tag;
463 1.1 uch if (tag != NULL /* has tag */
464 1.1 uch && tag->sz < BUCKET_SIZE /* that has free space */
465 1.1 uch && tag->dst + tag->sz == dst) /* and new data are contiguous */
466 1.1 uch {
467 1.1 uch DPRINTFN(1, "current tag %x/%x ok\n", tag->dst, tag->sz);
468 1.1 uch return (tag);
469 1.1 uch }
470 1.1 uch
471 1.1 uch pg = kloader.cur_pg;
472 1.1 uch KDASSERT(pg != NULL);
473 1.18 ad kloader.cur_pg = TAILQ_NEXT(pg, pageq.queue);
474 1.1 uch
475 1.1 uch addr = PG_VADDR(pg);
476 1.1 uch tag = (void *)addr;
477 1.1 uch
478 1.1 uch /*
479 1.1 uch * 2nd loader uses simple word-by-word copy, so destination
480 1.1 uch * address of a tag must be properly aligned.
481 1.1 uch */
482 1.1 uch KASSERT(ALIGNED_POINTER(dst, register_t));
483 1.1 uch
484 1.1 uch tag->src = addr + sizeof(struct kloader_page_tag);
485 1.1 uch tag->dst = dst;
486 1.1 uch tag->sz = 0;
487 1.1 uch tag->next = 0; /* Terminate. this member may overwrite after. */
488 1.1 uch if (kloader.cur_tag)
489 1.1 uch kloader.cur_tag->next = addr;
490 1.1 uch kloader.cur_tag = tag;
491 1.1 uch
492 1.1 uch return (tag);
493 1.1 uch }
494 1.1 uch
495 1.1 uch
496 1.1 uch /*
497 1.1 uch * Operations to populate kloader_page_tag's with data.
498 1.1 uch */
499 1.1 uch
500 1.1 uch void
501 1.1 uch kloader_from_file(vaddr_t dst, off_t ofs, size_t sz)
502 1.1 uch {
503 1.1 uch struct kloader_page_tag *tag;
504 1.1 uch size_t freesz;
505 1.1 uch
506 1.1 uch while (sz > 0) {
507 1.1 uch tag = kloader_get_tag(dst);
508 1.1 uch KDASSERT(tag != NULL);
509 1.1 uch freesz = BUCKET_SIZE - tag->sz;
510 1.1 uch if (freesz > sz)
511 1.1 uch freesz = sz;
512 1.1 uch
513 1.20 matt DPRINTFN(1, "0x%08"PRIxVADDR" + 0x%zx <- 0x%lx\n", dst, freesz,
514 1.6 peter (unsigned long)ofs);
515 1.1 uch kloader_read(ofs, freesz, (void *)(tag->src + tag->sz));
516 1.1 uch
517 1.1 uch tag->sz += freesz;
518 1.1 uch sz -= freesz;
519 1.1 uch ofs += freesz;
520 1.1 uch dst += freesz;
521 1.1 uch }
522 1.1 uch }
523 1.1 uch
524 1.1 uch
525 1.1 uch void
526 1.1 uch kloader_copy(vaddr_t dst, const void *src, size_t sz)
527 1.1 uch {
528 1.1 uch struct kloader_page_tag *tag;
529 1.1 uch size_t freesz;
530 1.1 uch
531 1.1 uch while (sz > 0) {
532 1.1 uch tag = kloader_get_tag(dst);
533 1.1 uch KDASSERT(tag != NULL);
534 1.1 uch freesz = BUCKET_SIZE - tag->sz;
535 1.1 uch if (freesz > sz)
536 1.1 uch freesz = sz;
537 1.1 uch
538 1.20 matt DPRINTFN(1, "0x%08"PRIxVADDR" + 0x%zx <- %p\n", dst, freesz, src);
539 1.1 uch memcpy((void *)(tag->src + tag->sz), src, freesz);
540 1.1 uch
541 1.1 uch tag->sz += freesz;
542 1.1 uch sz -= freesz;
543 1.3 uwe src = (const char *)src + freesz;
544 1.1 uch dst += freesz;
545 1.1 uch }
546 1.1 uch }
547 1.1 uch
548 1.1 uch
549 1.1 uch void
550 1.1 uch kloader_zero(vaddr_t dst, size_t sz)
551 1.1 uch {
552 1.1 uch struct kloader_page_tag *tag;
553 1.1 uch size_t freesz;
554 1.1 uch
555 1.1 uch while (sz > 0) {
556 1.1 uch tag = kloader_get_tag(dst);
557 1.1 uch KDASSERT(tag != NULL);
558 1.1 uch freesz = BUCKET_SIZE - tag->sz;
559 1.1 uch if (freesz > sz)
560 1.1 uch freesz = sz;
561 1.1 uch
562 1.20 matt DPRINTFN(1, "0x%08"PRIxVADDR" + 0x%zx\n", dst, freesz);
563 1.1 uch memset((void *)(tag->src + tag->sz), 0, freesz);
564 1.1 uch
565 1.1 uch tag->sz += freesz;
566 1.1 uch sz -= freesz;
567 1.1 uch dst += freesz;
568 1.1 uch }
569 1.1 uch }
570 1.1 uch
571 1.1 uch
572 1.1 uch void
573 1.1 uch kloader_load_segment(Elf_Phdr *p)
574 1.1 uch {
575 1.1 uch
576 1.1 uch DPRINTF("memory 0x%08x 0x%x <- file 0x%x 0x%x\n",
577 1.1 uch p->p_vaddr, p->p_memsz, p->p_offset, p->p_filesz);
578 1.1 uch
579 1.1 uch kloader_from_file(p->p_vaddr, p->p_offset, p->p_filesz);
580 1.1 uch #ifdef KLOADER_ZERO_BSS
581 1.1 uch kloader_zero(p->p_vaddr + p->p_filesz, p->p_memsz - p->p_filesz);
582 1.1 uch #endif
583 1.1 uch }
584 1.1 uch
585 1.1 uch
586 1.1 uch /*
587 1.1 uch * file access
588 1.1 uch */
589 1.1 uch struct vnode *
590 1.1 uch kloader_open(const char *filename)
591 1.1 uch {
592 1.1 uch struct nameidata nid;
593 1.8 uwe int error;
594 1.1 uch
595 1.13 pooka NDINIT(&nid, LOOKUP, FOLLOW, UIO_SYSSPACE, filename);
596 1.1 uch
597 1.8 uwe error = namei(&nid);
598 1.8 uwe if (error != 0) {
599 1.8 uwe PRINTF("%s: namei failed, errno=%d\n", filename, error);
600 1.8 uwe return (NULL);
601 1.1 uch }
602 1.1 uch
603 1.8 uwe error = vn_open(&nid, FREAD, 0);
604 1.8 uwe if (error != 0) {
605 1.8 uwe PRINTF("%s: open failed, errno=%d\n", filename, error);
606 1.8 uwe return (NULL);
607 1.1 uch }
608 1.1 uch
609 1.1 uch return (nid.ni_vp);
610 1.1 uch }
611 1.1 uch
612 1.1 uch void
613 1.19 cegger kloader_close(void)
614 1.1 uch {
615 1.7 christos struct lwp *l = KLOADER_LWP;
616 1.1 uch struct vnode *vp = kloader.vp;
617 1.1 uch
618 1.1 uch VOP_UNLOCK(vp, 0);
619 1.16 ad vn_close(vp, FREAD, l->l_cred);
620 1.1 uch }
621 1.1 uch
622 1.1 uch int
623 1.1 uch kloader_read(size_t ofs, size_t size, void *buf)
624 1.1 uch {
625 1.7 christos struct lwp *l = KLOADER_LWP;
626 1.1 uch struct vnode *vp = kloader.vp;
627 1.1 uch size_t resid;
628 1.1 uch int error;
629 1.1 uch
630 1.1 uch error = vn_rdwr(UIO_READ, vp, buf, size, ofs, UIO_SYSSPACE,
631 1.11 ad IO_NODELOCKED | IO_SYNC, l->l_cred, &resid, NULL);
632 1.1 uch
633 1.1 uch if (error)
634 1.1 uch PRINTF("read error.\n");
635 1.1 uch
636 1.1 uch return (error);
637 1.1 uch }
638 1.1 uch
639 1.1 uch
640 1.1 uch /*
641 1.1 uch * bootinfo
642 1.1 uch */
643 1.1 uch void
644 1.1 uch kloader_bootinfo_set(struct kloader_bootinfo *kbi, int argc, char *argv[],
645 1.1 uch struct bootinfo *bi, int printok)
646 1.1 uch {
647 1.1 uch char *p, *pend, *buf;
648 1.1 uch int i;
649 1.1 uch
650 1.1 uch kloader.bootinfo = kbi;
651 1.1 uch buf = kbi->_argbuf;
652 1.1 uch if (bi != NULL)
653 1.1 uch memcpy(&kbi->bootinfo, bi, sizeof(struct bootinfo));
654 1.1 uch kbi->argc = argc;
655 1.1 uch kbi->argv = (char **)buf;
656 1.1 uch
657 1.1 uch p = &buf[argc * sizeof(char **)];
658 1.1 uch pend = &buf[KLOADER_KERNELARGS_MAX - 1];
659 1.1 uch
660 1.1 uch for (i = 0; i < argc; i++) {
661 1.1 uch char *q = argv[i];
662 1.1 uch int len = strlen(q) + 1;
663 1.1 uch if ((p + len) > pend) {
664 1.1 uch kloader.bootinfo = NULL;
665 1.1 uch if (printok)
666 1.1 uch PRINTF("buffer insufficient.\n");
667 1.1 uch return;
668 1.1 uch }
669 1.1 uch kbi->argv[i] = p;
670 1.1 uch memcpy(p, q, len);
671 1.1 uch p += len;
672 1.1 uch }
673 1.1 uch }
674 1.1 uch
675 1.1 uch
676 1.1 uch #ifdef KLOADER_DEBUG
677 1.1 uch void
678 1.19 cegger kloader_pagetag_dump(void)
679 1.1 uch {
680 1.1 uch struct kloader_page_tag *tag = kloader.tagstart;
681 1.1 uch struct kloader_page_tag *p, *op;
682 1.12 thorpej bool print;
683 1.1 uch int i, n;
684 1.1 uch
685 1.1 uch p = tag;
686 1.1 uch op = NULL;
687 1.1 uch i = 0, n = 15;
688 1.1 uch
689 1.1 uch PRINTF("[page tag chain]\n");
690 1.1 uch do {
691 1.1 uch print = FALSE;
692 1.1 uch if (i < n)
693 1.1 uch print = TRUE;
694 1.9 uwe if ((uint32_t)p & 3) {
695 1.1 uch printf("tag alignment error\n");
696 1.1 uch break;
697 1.1 uch }
698 1.1 uch if ((p->src & 3) || (p->dst & 3)) {
699 1.1 uch printf("data alignement error.\n");
700 1.1 uch print = TRUE;
701 1.1 uch }
702 1.1 uch
703 1.1 uch if (print) {
704 1.1 uch printf("[%2d] next 0x%08x src 0x%08x dst 0x%08x"
705 1.1 uch " sz 0x%x\n", i, p->next, p->src, p->dst, p->sz);
706 1.1 uch } else if (i == n) {
707 1.1 uch printf("[...]\n");
708 1.1 uch }
709 1.1 uch op = p;
710 1.1 uch i++;
711 1.1 uch } while ((p = (struct kloader_page_tag *)(p->next)) != 0);
712 1.1 uch
713 1.1 uch if (op != NULL)
714 1.1 uch printf("[%d(last)] next 0x%08x src 0x%08x dst 0x%08x sz 0x%x\n",
715 1.1 uch i - 1, op->next, op->src, op->dst, op->sz);
716 1.1 uch }
717 1.1 uch
718 1.1 uch #endif /* KLOADER_DEBUG */
719