1 | /*
|
---|
2 | * Copyright (c) 2016 Martin Decky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <arch/main.h>
|
---|
30 | #include <arch/arch.h>
|
---|
31 | #include <arch/asm.h>
|
---|
32 | #include <arch/ucb.h>
|
---|
33 | #include <arch/mm.h>
|
---|
34 | #include <arch/types.h>
|
---|
35 | #include <version.h>
|
---|
36 | #include <stddef.h>
|
---|
37 | #include <printf.h>
|
---|
38 | #include <macros.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <align.h>
|
---|
41 | #include <str.h>
|
---|
42 | #include <halt.h>
|
---|
43 | #include <payload.h>
|
---|
44 | #include <kernel.h>
|
---|
45 |
|
---|
46 | static bootinfo_t bootinfo;
|
---|
47 |
|
---|
48 | void bootstrap(void)
|
---|
49 | {
|
---|
50 | version_print();
|
---|
51 |
|
---|
52 | bootinfo.htif_frame = ((uintptr_t) &htif_page) >> PAGE_WIDTH;
|
---|
53 | bootinfo.pt_frame = ((uintptr_t) &pt_page) >> PAGE_WIDTH;
|
---|
54 |
|
---|
55 | bootinfo.ucbinfo.tohost =
|
---|
56 | (volatile uint64_t *) PA2KA((uintptr_t) &tohost);
|
---|
57 | bootinfo.ucbinfo.fromhost =
|
---|
58 | (volatile uint64_t *) PA2KA((uintptr_t) &fromhost);
|
---|
59 |
|
---|
60 | // FIXME TODO: read from device tree
|
---|
61 | bootinfo.physmem_start = PHYSMEM_START;
|
---|
62 | bootinfo.memmap.total = PHYSMEM_SIZE;
|
---|
63 | bootinfo.memmap.cnt = 1;
|
---|
64 | bootinfo.memmap.zones[0].start = (void *) PHYSMEM_START;
|
---|
65 | bootinfo.memmap.zones[0].size = PHYSMEM_SIZE;
|
---|
66 |
|
---|
67 | printf("\nMemory statistics (total %" PRIu64 " MB, starting at %p)\n\n",
|
---|
68 | bootinfo.memmap.total >> 20, (void *) bootinfo.physmem_start);
|
---|
69 | printf(" %p: boot info structure\n", &bootinfo);
|
---|
70 |
|
---|
71 | uint8_t *load_addr = (uint8_t *) BOOT_OFFSET;
|
---|
72 | uintptr_t kernel_addr = PA2KA(load_addr);
|
---|
73 |
|
---|
74 | printf(" %p: inflate area\n", load_addr);
|
---|
75 | printf(" %p: kernel entry point\n", (void *) kernel_addr);
|
---|
76 |
|
---|
77 | /* Find the end of the memory zone containing the load address. */
|
---|
78 | uint8_t *end = NULL;
|
---|
79 | for (size_t i = 0; i < bootinfo.memmap.cnt; i++) {
|
---|
80 | memzone_t z = bootinfo.memmap.zones[i];
|
---|
81 |
|
---|
82 | if (z.start <= (void *) load_addr &&
|
---|
83 | z.start + z.size > (void *) load_addr) {
|
---|
84 | end = z.start + z.size;
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | // TODO: Cache-coherence callback?
|
---|
89 | extract_payload(&bootinfo.taskmap, load_addr, end, kernel_addr, NULL);
|
---|
90 |
|
---|
91 | uintptr_t entry = check_kernel(load_addr);
|
---|
92 |
|
---|
93 | printf("Booting the kernel...\n");
|
---|
94 | jump_to_kernel(PA2KA(&bootinfo), entry);
|
---|
95 | }
|
---|