| 1 | /*
|
|---|
| 2 | * Copyright (c) 2008 Jakub Jermar
|
|---|
| 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 | /** @addtogroup ia32mm
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @ingroup ia32mm, amd64mm
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <mm/frame.h>
|
|---|
| 37 | #include <arch/mm/frame.h>
|
|---|
| 38 | #include <mm/as.h>
|
|---|
| 39 | #include <config.h>
|
|---|
| 40 | #include <arch/boot/boot.h>
|
|---|
| 41 | #include <arch/boot/memmap.h>
|
|---|
| 42 | #include <panic.h>
|
|---|
| 43 | #include <debug.h>
|
|---|
| 44 | #include <align.h>
|
|---|
| 45 | #include <macros.h>
|
|---|
| 46 | #include <print.h>
|
|---|
| 47 |
|
|---|
| 48 | size_t hardcoded_unmapped_ktext_size = 0;
|
|---|
| 49 | size_t hardcoded_unmapped_kdata_size = 0;
|
|---|
| 50 |
|
|---|
| 51 | static void init_e820_memory(pfn_t minconf, bool low)
|
|---|
| 52 | {
|
|---|
| 53 | unsigned int i;
|
|---|
| 54 |
|
|---|
| 55 | for (i = 0; i < e820counter; i++) {
|
|---|
| 56 | uintptr_t base = (uintptr_t) e820table[i].base_address;
|
|---|
| 57 | size_t size = (size_t) e820table[i].size;
|
|---|
| 58 |
|
|---|
| 59 | if (!frame_adjust_zone_bounds(low, &base, &size))
|
|---|
| 60 | continue;
|
|---|
| 61 |
|
|---|
| 62 | if (e820table[i].type == MEMMAP_MEMORY_AVAILABLE) {
|
|---|
| 63 | /* To be safe, make the available zone possibly smaller */
|
|---|
| 64 | uint64_t new_base = ALIGN_UP(base, FRAME_SIZE);
|
|---|
| 65 | uint64_t new_size = ALIGN_DOWN(size - (new_base - base),
|
|---|
| 66 | FRAME_SIZE);
|
|---|
| 67 |
|
|---|
| 68 | size_t count = SIZE2FRAMES(new_size);
|
|---|
| 69 | pfn_t pfn = ADDR2PFN(new_base);
|
|---|
| 70 | pfn_t conf;
|
|---|
| 71 |
|
|---|
| 72 | if (low) {
|
|---|
| 73 | if ((minconf < pfn) || (minconf >= pfn + count))
|
|---|
| 74 | conf = pfn;
|
|---|
| 75 | else
|
|---|
| 76 | conf = minconf;
|
|---|
| 77 | zone_create(pfn, count, conf,
|
|---|
| 78 | ZONE_AVAILABLE | ZONE_LOWMEM);
|
|---|
| 79 | } else {
|
|---|
| 80 | conf = zone_external_conf_alloc(count);
|
|---|
| 81 | zone_create(pfn, count, conf,
|
|---|
| 82 | ZONE_AVAILABLE | ZONE_HIGHMEM);
|
|---|
| 83 | }
|
|---|
| 84 | } else if ((e820table[i].type == MEMMAP_MEMORY_ACPI) ||
|
|---|
| 85 | (e820table[i].type == MEMMAP_MEMORY_NVS)) {
|
|---|
| 86 | /* To be safe, make the firmware zone possibly larger */
|
|---|
| 87 | uint64_t new_base = ALIGN_DOWN(base, FRAME_SIZE);
|
|---|
| 88 | uint64_t new_size = ALIGN_UP(size + (base - new_base),
|
|---|
| 89 | FRAME_SIZE);
|
|---|
| 90 |
|
|---|
| 91 | zone_create(ADDR2PFN(new_base), SIZE2FRAMES(new_size), 0,
|
|---|
| 92 | ZONE_FIRMWARE);
|
|---|
| 93 | } else {
|
|---|
| 94 | /* To be safe, make the reserved zone possibly larger */
|
|---|
| 95 | uint64_t new_base = ALIGN_DOWN(base, FRAME_SIZE);
|
|---|
| 96 | uint64_t new_size = ALIGN_UP(size + (base - new_base),
|
|---|
| 97 | FRAME_SIZE);
|
|---|
| 98 |
|
|---|
| 99 | zone_create(ADDR2PFN(new_base), SIZE2FRAMES(new_size), 0,
|
|---|
| 100 | ZONE_RESERVED);
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | static const char *e820names[] = {
|
|---|
| 106 | "invalid",
|
|---|
| 107 | "available",
|
|---|
| 108 | "reserved",
|
|---|
| 109 | "acpi",
|
|---|
| 110 | "nvs",
|
|---|
| 111 | "unusable"
|
|---|
| 112 | };
|
|---|
| 113 |
|
|---|
| 114 | void physmem_print(void)
|
|---|
| 115 | {
|
|---|
| 116 | unsigned int i;
|
|---|
| 117 | printf("[base ] [size ] [name ]\n");
|
|---|
| 118 |
|
|---|
| 119 | for (i = 0; i < e820counter; i++) {
|
|---|
| 120 | const char *name;
|
|---|
| 121 |
|
|---|
| 122 | if (e820table[i].type <= MEMMAP_MEMORY_UNUSABLE)
|
|---|
| 123 | name = e820names[e820table[i].type];
|
|---|
| 124 | else
|
|---|
| 125 | name = "invalid";
|
|---|
| 126 |
|
|---|
| 127 | printf("%#018" PRIx64 " %#018" PRIx64" %s\n", e820table[i].base_address,
|
|---|
| 128 | e820table[i].size, name);
|
|---|
| 129 | }
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 | void frame_low_arch_init(void)
|
|---|
| 134 | {
|
|---|
| 135 | pfn_t minconf;
|
|---|
| 136 |
|
|---|
| 137 | if (config.cpu_active == 1) {
|
|---|
| 138 | minconf = 1;
|
|---|
| 139 |
|
|---|
| 140 | #ifdef CONFIG_SMP
|
|---|
| 141 | minconf = max(minconf,
|
|---|
| 142 | ADDR2PFN(AP_BOOT_OFFSET + hardcoded_unmapped_ktext_size +
|
|---|
| 143 | hardcoded_unmapped_kdata_size));
|
|---|
| 144 | #endif
|
|---|
| 145 |
|
|---|
| 146 | init_e820_memory(minconf, true);
|
|---|
| 147 |
|
|---|
| 148 | /* Reserve frame 0 (BIOS data) */
|
|---|
| 149 | frame_mark_unavailable(0, 1);
|
|---|
| 150 |
|
|---|
| 151 | #ifdef CONFIG_SMP
|
|---|
| 152 | /* Reserve AP real mode bootstrap memory */
|
|---|
| 153 | frame_mark_unavailable(AP_BOOT_OFFSET >> FRAME_WIDTH,
|
|---|
| 154 | (hardcoded_unmapped_ktext_size +
|
|---|
| 155 | hardcoded_unmapped_kdata_size) >> FRAME_WIDTH);
|
|---|
| 156 | #endif
|
|---|
| 157 | }
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | void frame_high_arch_init(void)
|
|---|
| 161 | {
|
|---|
| 162 | if (config.cpu_active == 1)
|
|---|
| 163 | init_e820_memory(0, false);
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | /** @}
|
|---|
| 167 | */
|
|---|