1 /* $NetBSD: kloader.c,v 1.19 2009/03/18 10:22:39 cegger Exp $ */
4 * Copyright (c) 2001, 2002, 2004 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: kloader.c,v 1.19 2009/03/18 10:22:39 cegger Exp $");
32 #include "debug_kloader.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
38 #include <sys/vnode.h>
39 #include <sys/namei.h>
40 #include <sys/fcntl.h>
42 #include <sys/exec_elf.h>
44 #include <uvm/uvm_extern.h>
46 #include <machine/kloader.h>
48 #define PRINTF(fmt, args...) printf("kloader: " fmt, ##args)
51 int kloader_debug
= 1;
52 #define DPRINTF(fmt, args...) \
54 printf("%s: " fmt, __func__ , ##args)
55 #define _DPRINTF(fmt, args...) \
58 #define DPRINTFN(n, fmt, args...) \
59 if (kloader_debug > (n)) \
60 printf("%s: " fmt, __func__ , ##args)
61 #define _DPRINTFN(n, fmt, args...) \
62 if (kloader_debug > (n)) \
66 #define DPRINTF(fmt, args...) ((void)0)
67 #define _DPRINTF(fmt, args...) ((void)0)
68 #define DPRINTFN(n, fmt, args...) ((void)0)
69 #define _DPRINTFN(n, fmt, args...) ((void)0)
74 struct pglist pg_head
;
75 struct vm_page
*cur_pg
;
76 struct kloader_page_tag
*cur_tag
;
78 struct kloader_page_tag
*tagstart
;
79 struct kloader_bootinfo
*bootinfo
;
80 struct kloader_bootinfo
*rebootinfo
;
82 kloader_bootfunc_t
*loader
;
85 struct kloader_ops
*ops
;
88 #define BUCKET_SIZE (PAGE_SIZE - sizeof(struct kloader_page_tag))
89 #define KLOADER_LWP (&lwp0)
90 STATIC
struct kloader kloader
;
92 #define ROUND4(x) (((x) + 3) & ~3)
94 STATIC
int kloader_load(void);
96 STATIC
int kloader_alloc_memory(size_t);
97 STATIC
struct kloader_page_tag
*kloader_get_tag(vaddr_t
);
98 STATIC
void kloader_from_file(vaddr_t
, off_t
, size_t);
99 STATIC
void kloader_copy(vaddr_t
, const void *, size_t);
100 STATIC
void kloader_zero(vaddr_t
, size_t);
102 STATIC
void kloader_load_segment(Elf_Phdr
*);
104 STATIC
struct vnode
*kloader_open(const char *);
105 STATIC
void kloader_close(void);
106 STATIC
int kloader_read(size_t, size_t, void *);
109 STATIC
void kloader_pagetag_dump(void);
113 __kloader_reboot_setup(struct kloader_ops
*ops
, const char *filename
)
116 if (kloader
.bootinfo
== NULL
) {
117 PRINTF("No bootinfo.\n");
121 if (ops
== NULL
|| ops
->jump
== NULL
|| ops
->boot
== NULL
) {
122 PRINTF("No boot operations.\n");
127 if (kloader
.called
++ == 0) {
128 PRINTF("kernel file name: %s\n", filename
);
129 kloader
.vp
= kloader_open(filename
);
130 if (kloader
.vp
== NULL
)
133 if (kloader_load() == 0) {
134 kloader
.setuped
= TRUE
;
136 kloader_pagetag_dump();
141 /* Fatal case. reboot from DDB etc. */
151 if (kloader
.setuped
) {
152 PRINTF("Rebooting...\n");
153 (*kloader
.ops
->jump
)(kloader
.loader
, kloader
.loader_sp
,
154 kloader
.rebootinfo
, kloader
.tagstart
);
157 if (kloader
.ops
->reset
!= NULL
) {
158 PRINTF("Resetting...\n");
159 (*kloader
.ops
->reset
)();
161 while (/*CONSTCOND*/1)
180 struct kloader_bootinfo nbi
; /* new boot info */
181 char *oldbuf
, *newbuf
;
189 /* read kernel's ELF header */
190 kloader_read(0, sizeof(Elf_Ehdr
), &eh
);
192 if (eh
.e_ident
[EI_MAG0
] != ELFMAG0
||
193 eh
.e_ident
[EI_MAG1
] != ELFMAG1
||
194 eh
.e_ident
[EI_MAG2
] != ELFMAG2
||
195 eh
.e_ident
[EI_MAG3
] != ELFMAG3
) {
196 PRINTF("not an ELF file\n");
200 /* read program headers */
201 sz
= eh
.e_phentsize
* eh
.e_phnum
;
202 if ((ph
= malloc(sz
, M_TEMP
, M_NOWAIT
)) == NULL
) {
203 PRINTF("can't allocate program header table.\n");
206 if (kloader_read(eh
.e_phoff
, sz
, ph
) != 0) {
207 PRINTF("program header read error.\n");
211 /* read section headers */
212 sz
= eh
.e_shentsize
* eh
.e_shnum
;
213 if ((sh
= malloc(sz
, M_TEMP
, M_NOWAIT
)) == NULL
) {
214 PRINTF("can't allocate section header table.\n");
217 if (kloader_read(eh
.e_shoff
, eh
.e_shentsize
* eh
.e_shnum
, sh
) != 0) {
218 PRINTF("section header read error.\n");
222 /* read section names */
223 shstrsz
= ROUND4(sh
[eh
.e_shstrndx
].sh_size
);
224 shstrtab
= malloc(shstrsz
, M_TEMP
, M_NOWAIT
);
225 if (shstrtab
== NULL
) {
226 PRINTF("unable to allocate memory for .shstrtab\n");
229 DPRINTF("reading 0x%x bytes of .shstrtab at 0x%x\n",
230 sh
[eh
.e_shstrndx
].sh_size
, sh
[eh
.e_shstrndx
].sh_offset
);
231 kloader_read(sh
[eh
.e_shstrndx
].sh_offset
, sh
[eh
.e_shstrndx
].sh_size
,
234 /* save entry point, code to construct symbol table overwrites it */
239 * Calculate memory size
243 /* loadable segments */
244 for (i
= 0; i
< eh
.e_phnum
; i
++) {
245 if (ph
[i
].p_type
== PT_LOAD
) {
246 DPRINTF("segment %d size = file 0x%x memory 0x%x\n",
247 i
, ph
[i
].p_filesz
, ph
[i
].p_memsz
);
248 #ifdef KLOADER_ZERO_BSS
249 sz
+= round_page(ph
[i
].p_memsz
);
251 sz
+= round_page(ph
[i
].p_filesz
);
253 sz
+= PAGE_SIZE
; /* compensate for partial last tag */
257 if (sz
== 0) /* nothing to load? */
260 /* symbols/strings sections */
261 symndx
= strndx
= -1;
262 for (i
= 0; i
< eh
.e_shnum
; i
++) {
263 if (strcmp(shstrtab
+ sh
[i
].sh_name
, ".symtab") == 0)
265 else if (strcmp(shstrtab
+ sh
[i
].sh_name
, ".strtab") == 0)
267 else if (i
!= eh
.e_shstrndx
)
268 /* while here, mark all other sections as unused */
269 sh
[i
].sh_type
= SHT_NULL
;
272 if (symndx
< 0 || strndx
< 0) {
274 PRINTF("no .symtab section\n");
276 PRINTF("no .strtab section\n");
277 ksymsz
= SELFMAG
; /* just a bad magic */
279 ksymsz
= sizeof(Elf_Ehdr
)
280 + eh
.e_shentsize
* eh
.e_shnum
281 + shstrsz
/* rounded to 4 bytes */
283 + sh
[strndx
].sh_size
;
284 DPRINTF("ksyms size = 0x%zx\n", ksymsz
);
286 sz
+= ROUND4(ksymsz
);
288 /* boot info for the new kernel */
289 sz
+= sizeof(struct kloader_bootinfo
);
291 /* get memory for new kernel */
292 if (kloader_alloc_memory(sz
) != 0)
297 * Copy new kernel in.
299 kv
= 0; /* XXX: -Wuninitialized */
300 for (i
= 0, p
= ph
; i
< eh
.e_phnum
; i
++, p
++) {
301 if (p
->p_type
== PT_LOAD
) {
302 kloader_load_segment(p
);
303 kv
= p
->p_vaddr
+ ROUND4(p
->p_memsz
);
309 * Construct symbol table for ksyms.
311 if (symndx
< 0 || strndx
< 0) {
312 kloader_zero(kv
, SELFMAG
);
316 off_t symoff
, stroff
;
318 /* save offsets of .symtab and .strtab before we change them */
319 symoff
= sh
[symndx
].sh_offset
;
320 stroff
= sh
[strndx
].sh_offset
;
322 /* no loadable segments */
327 /* change offsets to reflect new layout */
328 eoff
= sizeof(Elf_Ehdr
);
331 eoff
+= eh
.e_shentsize
* eh
.e_shnum
;
332 sh
[eh
.e_shstrndx
].sh_offset
= eoff
;
335 sh
[symndx
].sh_offset
= eoff
;
337 eoff
+= sh
[symndx
].sh_size
;
338 sh
[strndx
].sh_offset
= eoff
;
340 /* local copies massaged, can serve them now */
341 DPRINTF("ksyms ELF header\n");
342 kloader_copy(kv
, &eh
, sizeof(Elf_Ehdr
));
343 kv
+= sizeof(Elf_Ehdr
);
345 DPRINTF("ksyms section headers\n");
346 kloader_copy(kv
, sh
, eh
.e_shentsize
* eh
.e_shnum
);
347 kv
+= eh
.e_shentsize
* eh
.e_shnum
;
349 DPRINTF("ksyms .shstrtab\n");
350 kloader_copy(kv
, shstrtab
, shstrsz
);
353 DPRINTF("ksyms .symtab\n");
354 kloader_from_file(kv
, symoff
, sh
[symndx
].sh_size
);
355 kv
+= sh
[symndx
].sh_size
;
357 DPRINTF("ksyms .strtab\n");
358 kloader_from_file(kv
, stroff
, ROUND4(sh
[strndx
].sh_size
));
359 kv
+= ROUND4(sh
[strndx
].sh_size
);
363 * Create boot info to pass to the new kernel.
364 * All pointers in it are *not* valid until the new kernel runs!
367 /* get a private copy of current bootinfo to vivisect */
368 memcpy(&nbi
, kloader
.bootinfo
,
369 sizeof(struct kloader_bootinfo
));
371 /* new kernel entry point */
374 /* where args currently are, see kloader_bootinfo_set() */
375 oldbuf
= &kloader
.bootinfo
->_argbuf
[0];
377 /* where args *will* be after boot code copied them */
378 newbuf
= (char *)(void *)kv
379 + offsetof(struct kloader_bootinfo
, _argbuf
);
381 DPRINTF("argv: old %p -> new %p\n", oldbuf
, newbuf
);
383 /* not a valid pointer in this kernel! */
384 nbi
.argv
= (void *)newbuf
;
386 /* local copy that we populate with new (not yet valid) pointers */
387 ap
= (char **)(void *)nbi
._argbuf
;
389 for (i
= 0; i
< kloader
.bootinfo
->argc
; ++i
) {
390 DPRINTFN(1, " [%d]: %p -> ", i
, kloader
.bootinfo
->argv
[i
]);
392 (kloader
.bootinfo
->argv
[i
] - oldbuf
);
393 _DPRINTFN(1, "%p\n", ap
[i
]);
396 /* arrange for the new bootinfo to get copied */
397 DPRINTF("bootinfo\n");
398 kloader_copy(kv
, &nbi
, sizeof(struct kloader_bootinfo
));
400 /* will be valid by the time the new kernel starts */
401 kloader
.rebootinfo
= (void *)kv
;
402 /* kv += sizeof(struct kloader_bootinfo); */
407 KDASSERT(kloader
.cur_pg
);
408 kloader
.loader
= (void *)PG_VADDR(kloader
.cur_pg
);
409 memcpy(kloader
.loader
, kloader
.ops
->boot
, PAGE_SIZE
);
411 /* loader stack starts at the bottom of that page */
412 kloader
.loader_sp
= (vaddr_t
)kloader
.loader
+ PAGE_SIZE
;
414 DPRINTF("[loader] addr=%p sp=%p [kernel] entry=%p\n",
415 kloader
.loader
, (void *)kloader
.loader_sp
, (void *)nbi
.entry
);
423 if (shstrtab
!= NULL
)
424 free(shstrtab
, M_TEMP
);
431 kloader_alloc_memory(size_t sz
)
433 extern paddr_t avail_start
, avail_end
;
436 n
= (sz
+ BUCKET_SIZE
- 1) / BUCKET_SIZE
/* kernel &co */
437 + 1; /* 2nd loader */
439 error
= uvm_pglistalloc(n
* PAGE_SIZE
, avail_start
, avail_end
,
440 PAGE_SIZE
, 0, &kloader
.pg_head
, n
, 0);
442 PRINTF("can't allocate memory.\n");
445 DPRINTF("allocated %d pages.\n", n
);
447 kloader
.cur_pg
= TAILQ_FIRST(&kloader
.pg_head
);
448 kloader
.tagstart
= (void *)PG_VADDR(kloader
.cur_pg
);
449 kloader
.cur_tag
= NULL
;
455 struct kloader_page_tag
*
456 kloader_get_tag(vaddr_t dst
)
460 struct kloader_page_tag
*tag
;
462 tag
= kloader
.cur_tag
;
463 if (tag
!= NULL
/* has tag */
464 && tag
->sz
< BUCKET_SIZE
/* that has free space */
465 && tag
->dst
+ tag
->sz
== dst
) /* and new data are contiguous */
467 DPRINTFN(1, "current tag %x/%x ok\n", tag
->dst
, tag
->sz
);
472 KDASSERT(pg
!= NULL
);
473 kloader
.cur_pg
= TAILQ_NEXT(pg
, pageq
.queue
);
479 * 2nd loader uses simple word-by-word copy, so destination
480 * address of a tag must be properly aligned.
482 KASSERT(ALIGNED_POINTER(dst
, register_t
));
484 tag
->src
= addr
+ sizeof(struct kloader_page_tag
);
487 tag
->next
= 0; /* Terminate. this member may overwrite after. */
489 kloader
.cur_tag
->next
= addr
;
490 kloader
.cur_tag
= tag
;
497 * Operations to populate kloader_page_tag's with data.
501 kloader_from_file(vaddr_t dst
, off_t ofs
, size_t sz
)
503 struct kloader_page_tag
*tag
;
507 tag
= kloader_get_tag(dst
);
508 KDASSERT(tag
!= NULL
);
509 freesz
= BUCKET_SIZE
- tag
->sz
;
513 DPRINTFN(1, "0x%08"PRIxVADDR
" + 0x%zx <- 0x%lx\n", dst
, freesz
,
515 kloader_read(ofs
, freesz
, (void *)(tag
->src
+ tag
->sz
));
526 kloader_copy(vaddr_t dst
, const void *src
, size_t sz
)
528 struct kloader_page_tag
*tag
;
532 tag
= kloader_get_tag(dst
);
533 KDASSERT(tag
!= NULL
);
534 freesz
= BUCKET_SIZE
- tag
->sz
;
538 DPRINTFN(1, "0x%08"PRIxVADDR
" + 0x%zx <- %p\n", dst
, freesz
, src
);
539 memcpy((void *)(tag
->src
+ tag
->sz
), src
, freesz
);
543 src
= (const char *)src
+ freesz
;
550 kloader_zero(vaddr_t dst
, size_t sz
)
552 struct kloader_page_tag
*tag
;
556 tag
= kloader_get_tag(dst
);
557 KDASSERT(tag
!= NULL
);
558 freesz
= BUCKET_SIZE
- tag
->sz
;
562 DPRINTFN(1, "0x%08"PRIxVADDR
" + 0x%zx\n", dst
, freesz
);
563 memset((void *)(tag
->src
+ tag
->sz
), 0, freesz
);
573 kloader_load_segment(Elf_Phdr
*p
)
576 DPRINTF("memory 0x%08x 0x%x <- file 0x%x 0x%x\n",
577 p
->p_vaddr
, p
->p_memsz
, p
->p_offset
, p
->p_filesz
);
579 kloader_from_file(p
->p_vaddr
, p
->p_offset
, p
->p_filesz
);
580 #ifdef KLOADER_ZERO_BSS
581 kloader_zero(p
->p_vaddr
+ p
->p_filesz
, p
->p_memsz
- p
->p_filesz
);
590 kloader_open(const char *filename
)
592 struct nameidata nid
;
595 NDINIT(&nid
, LOOKUP
, FOLLOW
, UIO_SYSSPACE
, filename
);
599 PRINTF("%s: namei failed, errno=%d\n", filename
, error
);
603 error
= vn_open(&nid
, FREAD
, 0);
605 PRINTF("%s: open failed, errno=%d\n", filename
, error
);
615 struct lwp
*l
= KLOADER_LWP
;
616 struct vnode
*vp
= kloader
.vp
;
619 vn_close(vp
, FREAD
, l
->l_cred
);
623 kloader_read(size_t ofs
, size_t size
, void *buf
)
625 struct lwp
*l
= KLOADER_LWP
;
626 struct vnode
*vp
= kloader
.vp
;
630 error
= vn_rdwr(UIO_READ
, vp
, buf
, size
, ofs
, UIO_SYSSPACE
,
631 IO_NODELOCKED
| IO_SYNC
, l
->l_cred
, &resid
, NULL
);
634 PRINTF("read error.\n");
644 kloader_bootinfo_set(struct kloader_bootinfo
*kbi
, int argc
, char *argv
[],
645 struct bootinfo
*bi
, int printok
)
647 char *p
, *pend
, *buf
;
650 kloader
.bootinfo
= kbi
;
653 memcpy(&kbi
->bootinfo
, bi
, sizeof(struct bootinfo
));
655 kbi
->argv
= (char **)buf
;
657 p
= &buf
[argc
* sizeof(char **)];
658 pend
= &buf
[KLOADER_KERNELARGS_MAX
- 1];
660 for (i
= 0; i
< argc
; i
++) {
662 int len
= strlen(q
) + 1;
663 if ((p
+ len
) > pend
) {
664 kloader
.bootinfo
= NULL
;
666 PRINTF("buffer insufficient.\n");
678 kloader_pagetag_dump(void)
680 struct kloader_page_tag
*tag
= kloader
.tagstart
;
681 struct kloader_page_tag
*p
, *op
;
689 PRINTF("[page tag chain]\n");
694 if ((uint32_t)p
& 3) {
695 printf("tag alignment error\n");
698 if ((p
->src
& 3) || (p
->dst
& 3)) {
699 printf("data alignement error.\n");
704 printf("[%2d] next 0x%08x src 0x%08x dst 0x%08x"
705 " sz 0x%x\n", i
, p
->next
, p
->src
, p
->dst
, p
->sz
);
711 } while ((p
= (struct kloader_page_tag
*)(p
->next
)) != 0);
714 printf("[%d(last)] next 0x%08x src 0x%08x dst 0x%08x sz 0x%x\n",
715 i
- 1, op
->next
, op
->src
, op
->dst
, op
->sz
);
718 #endif /* KLOADER_DEBUG */