| 1 | /*
|
|---|
| 2 | * Copyright (c) 2005 Martin Decky
|
|---|
| 3 | * Copyright (c) 2006 Jakub Jermar
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - 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.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | #include "main.h"
|
|---|
| 31 | #include <printf.h>
|
|---|
| 32 | #include "asm.h"
|
|---|
| 33 | #include "_components.h"
|
|---|
| 34 | #include <balloc.h>
|
|---|
| 35 | #include <ofw.h>
|
|---|
| 36 | #include <ofw_tree.h>
|
|---|
| 37 | #include "ofwarch.h"
|
|---|
| 38 | #include <align.h>
|
|---|
| 39 | #include <macros.h>
|
|---|
| 40 | #include <string.h>
|
|---|
| 41 | #include <memstr.h>
|
|---|
| 42 |
|
|---|
| 43 | static bootinfo_t bootinfo;
|
|---|
| 44 | static component_t components[COMPONENTS];
|
|---|
| 45 | static char *release = STRING(RELEASE);
|
|---|
| 46 |
|
|---|
| 47 | #ifdef REVISION
|
|---|
| 48 | static char *revision = ", revision " STRING(REVISION);
|
|---|
| 49 | #else
|
|---|
| 50 | static char *revision = "";
|
|---|
| 51 | #endif
|
|---|
| 52 |
|
|---|
| 53 | #ifdef TIMESTAMP
|
|---|
| 54 | static char *timestamp = "\nBuilt on " STRING(TIMESTAMP);
|
|---|
| 55 | #else
|
|---|
| 56 | static char *timestamp = "";
|
|---|
| 57 | #endif
|
|---|
| 58 |
|
|---|
| 59 | /** UltraSPARC subarchitecture - 1 for US, 3 for US3 */
|
|---|
| 60 | static uint8_t subarchitecture;
|
|---|
| 61 |
|
|---|
| 62 | /**
|
|---|
| 63 | * mask of the MID field inside the ICBUS_CONFIG register shifted by
|
|---|
| 64 | * MID_SHIFT bits to the right
|
|---|
| 65 | */
|
|---|
| 66 | static uint16_t mid_mask;
|
|---|
| 67 |
|
|---|
| 68 | /** Print version information. */
|
|---|
| 69 | static void version_print(void)
|
|---|
| 70 | {
|
|---|
| 71 | printf("HelenOS SPARC64 Bootloader\nRelease %s%s%s\n"
|
|---|
| 72 | "Copyright (c) 2006 HelenOS project\n",
|
|---|
| 73 | release, revision, timestamp);
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | /* the lowest ID (read from the VER register) of some US3 CPU model */
|
|---|
| 77 | #define FIRST_US3_CPU 0x14
|
|---|
| 78 |
|
|---|
| 79 | /* the greatest ID (read from the VER register) of some US3 CPU model */
|
|---|
| 80 | #define LAST_US3_CPU 0x19
|
|---|
| 81 |
|
|---|
| 82 | /* UltraSPARC IIIi processor implementation code */
|
|---|
| 83 | #define US_IIIi_CODE 0x15
|
|---|
| 84 |
|
|---|
| 85 | /**
|
|---|
| 86 | * Sets the global variables "subarchitecture" and "mid_mask" to
|
|---|
| 87 | * correct values.
|
|---|
| 88 | */
|
|---|
| 89 | static void detect_subarchitecture(void)
|
|---|
| 90 | {
|
|---|
| 91 | uint64_t v;
|
|---|
| 92 | asm volatile (
|
|---|
| 93 | "rdpr %%ver, %0\n"
|
|---|
| 94 | : "=r" (v)
|
|---|
| 95 | );
|
|---|
| 96 |
|
|---|
| 97 | v = (v << 16) >> 48;
|
|---|
| 98 | if ((v >= FIRST_US3_CPU) && (v <= LAST_US3_CPU)) {
|
|---|
| 99 | subarchitecture = SUBARCH_US3;
|
|---|
| 100 | if (v == US_IIIi_CODE)
|
|---|
| 101 | mid_mask = (1 << 5) - 1;
|
|---|
| 102 | else
|
|---|
| 103 | mid_mask = (1 << 10) - 1;
|
|---|
| 104 | } else if (v < FIRST_US3_CPU) {
|
|---|
| 105 | subarchitecture = SUBARCH_US;
|
|---|
| 106 | mid_mask = (1 << 5) - 1;
|
|---|
| 107 | } else
|
|---|
| 108 | printf("\nThis CPU is not supported by HelenOS.");
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | void bootstrap(void)
|
|---|
| 112 | {
|
|---|
| 113 | void *base = (void *) KERNEL_VIRTUAL_ADDRESS;
|
|---|
| 114 | void *balloc_base;
|
|---|
| 115 | unsigned int top = 0;
|
|---|
| 116 | unsigned int i;
|
|---|
| 117 | unsigned int j;
|
|---|
| 118 |
|
|---|
| 119 | version_print();
|
|---|
| 120 |
|
|---|
| 121 | detect_subarchitecture();
|
|---|
| 122 | init_components(components);
|
|---|
| 123 |
|
|---|
| 124 | if (!ofw_get_physmem_start(&bootinfo.physmem_start)) {
|
|---|
| 125 | printf("Error: unable to get start of physical memory.\n");
|
|---|
| 126 | halt();
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | if (!ofw_memmap(&bootinfo.memmap)) {
|
|---|
| 130 | printf("Error: unable to get memory map, halting.\n");
|
|---|
| 131 | halt();
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | if (bootinfo.memmap.total == 0) {
|
|---|
| 135 | printf("Error: no memory detected, halting.\n");
|
|---|
| 136 | halt();
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | /*
|
|---|
| 140 | * SILO for some reason adds 0x400000 and subtracts
|
|---|
| 141 | * bootinfo.physmem_start to/from silo_ramdisk_image.
|
|---|
| 142 | * We just need plain physical address so we fix it up.
|
|---|
| 143 | */
|
|---|
| 144 | if (silo_ramdisk_image) {
|
|---|
| 145 | silo_ramdisk_image += bootinfo.physmem_start;
|
|---|
| 146 | silo_ramdisk_image -= 0x400000;
|
|---|
| 147 |
|
|---|
| 148 | /* Install 1:1 mapping for the RAM disk. */
|
|---|
| 149 | if (ofw_map((void *) ((uintptr_t) silo_ramdisk_image),
|
|---|
| 150 | (void *) ((uintptr_t) silo_ramdisk_image),
|
|---|
| 151 | silo_ramdisk_size, -1) != 0) {
|
|---|
| 152 | printf("Failed to map RAM disk.\n");
|
|---|
| 153 | halt();
|
|---|
| 154 | }
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | printf("\nMemory statistics (total %d MB, starting at %P)\n",
|
|---|
| 158 | bootinfo.memmap.total >> 20, bootinfo.physmem_start);
|
|---|
| 159 | printf(" %P: kernel entry point\n", KERNEL_VIRTUAL_ADDRESS);
|
|---|
| 160 | printf(" %P: boot info structure\n", &bootinfo);
|
|---|
| 161 |
|
|---|
| 162 | /*
|
|---|
| 163 | * Figure out destination address for each component.
|
|---|
| 164 | * In this phase, we don't copy the components yet because we want to
|
|---|
| 165 | * to be careful not to overwrite anything, especially the components
|
|---|
| 166 | * which haven't been copied yet.
|
|---|
| 167 | */
|
|---|
| 168 | bootinfo.taskmap.count = 0;
|
|---|
| 169 | for (i = 0; i < COMPONENTS; i++) {
|
|---|
| 170 | printf(" %P: %s image (size %d bytes)\n", components[i].start,
|
|---|
| 171 | components[i].name, components[i].size);
|
|---|
| 172 | top = ALIGN_UP(top, PAGE_SIZE);
|
|---|
| 173 | if (i > 0) {
|
|---|
| 174 | if (bootinfo.taskmap.count == TASKMAP_MAX_RECORDS) {
|
|---|
| 175 | printf("Skipping superfluous components.\n");
|
|---|
| 176 | break;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | bootinfo.taskmap.tasks[bootinfo.taskmap.count].addr =
|
|---|
| 180 | base + top;
|
|---|
| 181 | bootinfo.taskmap.tasks[bootinfo.taskmap.count].size =
|
|---|
| 182 | components[i].size;
|
|---|
| 183 | strncpy(bootinfo.taskmap.tasks[
|
|---|
| 184 | bootinfo.taskmap.count].name, components[i].name,
|
|---|
| 185 | BOOTINFO_TASK_NAME_BUFLEN);
|
|---|
| 186 | bootinfo.taskmap.count++;
|
|---|
| 187 | }
|
|---|
| 188 | top += components[i].size;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | printf("\n");
|
|---|
| 192 |
|
|---|
| 193 | /* Do not consider RAM disk */
|
|---|
| 194 | j = bootinfo.taskmap.count - 1;
|
|---|
| 195 |
|
|---|
| 196 | if (silo_ramdisk_image) {
|
|---|
| 197 | /* Treat the RAM disk as the last bootinfo task. */
|
|---|
| 198 | if (bootinfo.taskmap.count == TASKMAP_MAX_RECORDS) {
|
|---|
| 199 | printf("Skipping RAM disk.\n");
|
|---|
| 200 | goto skip_ramdisk;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | top = ALIGN_UP(top, PAGE_SIZE);
|
|---|
| 204 | bootinfo.taskmap.tasks[bootinfo.taskmap.count].addr =
|
|---|
| 205 | base + top;
|
|---|
| 206 | bootinfo.taskmap.tasks[bootinfo.taskmap.count].size =
|
|---|
| 207 | silo_ramdisk_size;
|
|---|
| 208 | bootinfo.taskmap.count++;
|
|---|
| 209 | printf("Copying RAM disk...");
|
|---|
| 210 |
|
|---|
| 211 | /*
|
|---|
| 212 | * Claim and map the whole ramdisk as it may exceed the area
|
|---|
| 213 | * given to us by SILO.
|
|---|
| 214 | */
|
|---|
| 215 | (void) ofw_claim_phys(base + top, silo_ramdisk_size);
|
|---|
| 216 | (void) ofw_map(bootinfo.physmem_start + base + top, base + top,
|
|---|
| 217 | silo_ramdisk_size, -1);
|
|---|
| 218 | memmove(base + top, (void *) ((uintptr_t) silo_ramdisk_image),
|
|---|
| 219 | silo_ramdisk_size);
|
|---|
| 220 |
|
|---|
| 221 | printf("done.\n");
|
|---|
| 222 | top += silo_ramdisk_size;
|
|---|
| 223 | }
|
|---|
| 224 | skip_ramdisk:
|
|---|
| 225 |
|
|---|
| 226 | /*
|
|---|
| 227 | * Now we can proceed to copy the components. We do it in reverse order
|
|---|
| 228 | * so that we don't overwrite anything even if the components overlap
|
|---|
| 229 | * with base.
|
|---|
| 230 | */
|
|---|
| 231 | printf("Copying tasks...");
|
|---|
| 232 | for (i = COMPONENTS - 1; i > 0; i--, j--) {
|
|---|
| 233 | printf("%s ", components[i].name);
|
|---|
| 234 |
|
|---|
| 235 | /*
|
|---|
| 236 | * At this point, we claim the physical memory that we are
|
|---|
| 237 | * going to use. We should be safe in case of the virtual
|
|---|
| 238 | * address space because the OpenFirmware, according to its
|
|---|
| 239 | * SPARC binding, should restrict its use of virtual memory
|
|---|
| 240 | * to addresses from [0xffd00000; 0xffefffff] and
|
|---|
| 241 | * [0xfe000000; 0xfeffffff].
|
|---|
| 242 | *
|
|---|
| 243 | * XXX We don't map this piece of memory. We simply rely on
|
|---|
| 244 | * SILO to have it done for us already in this case.
|
|---|
| 245 | */
|
|---|
| 246 | (void) ofw_claim_phys(bootinfo.physmem_start +
|
|---|
| 247 | bootinfo.taskmap.tasks[j].addr,
|
|---|
| 248 | ALIGN_UP(components[i].size, PAGE_SIZE));
|
|---|
| 249 |
|
|---|
| 250 | memcpy((void *) bootinfo.taskmap.tasks[j].addr,
|
|---|
| 251 | components[i].start, components[i].size);
|
|---|
| 252 |
|
|---|
| 253 | }
|
|---|
| 254 | printf(".\n");
|
|---|
| 255 |
|
|---|
| 256 | printf("Copying kernel...");
|
|---|
| 257 | (void) ofw_claim_phys(bootinfo.physmem_start + base,
|
|---|
| 258 | ALIGN_UP(components[0].size, PAGE_SIZE));
|
|---|
| 259 | memcpy(base, components[0].start, components[0].size);
|
|---|
| 260 | printf("done.\n");
|
|---|
| 261 |
|
|---|
| 262 | /*
|
|---|
| 263 | * Claim and map the physical memory for the boot allocator.
|
|---|
| 264 | * Initialize the boot allocator.
|
|---|
| 265 | */
|
|---|
| 266 | balloc_base = base + ALIGN_UP(top, PAGE_SIZE);
|
|---|
| 267 | (void) ofw_claim_phys(bootinfo.physmem_start + balloc_base,
|
|---|
| 268 | BALLOC_MAX_SIZE);
|
|---|
| 269 | (void) ofw_map(bootinfo.physmem_start + balloc_base, balloc_base,
|
|---|
| 270 | BALLOC_MAX_SIZE, -1);
|
|---|
| 271 | balloc_init(&bootinfo.ballocs, (uintptr_t) balloc_base,
|
|---|
| 272 | (uintptr_t) balloc_base);
|
|---|
| 273 |
|
|---|
| 274 | printf("Setting up screens...");
|
|---|
| 275 | ofw_setup_screens();
|
|---|
| 276 | printf("done.\n");
|
|---|
| 277 |
|
|---|
| 278 | printf("Canonizing OpenFirmware device tree...");
|
|---|
| 279 | bootinfo.ofw_root = ofw_tree_build();
|
|---|
| 280 | printf("done.\n");
|
|---|
| 281 |
|
|---|
| 282 | #ifdef CONFIG_AP
|
|---|
| 283 | printf("Checking for secondary processors...");
|
|---|
| 284 | if (!ofw_cpu(mid_mask, bootinfo.physmem_start))
|
|---|
| 285 | printf("Error: unable to get CPU properties\n");
|
|---|
| 286 | printf("done.\n");
|
|---|
| 287 | #endif
|
|---|
| 288 |
|
|---|
| 289 | printf("Booting the kernel...\n");
|
|---|
| 290 | jump_to_kernel((void *) KERNEL_VIRTUAL_ADDRESS,
|
|---|
| 291 | bootinfo.physmem_start | BSP_PROCESSOR, &bootinfo,
|
|---|
| 292 | sizeof(bootinfo), subarchitecture);
|
|---|
| 293 | }
|
|---|