[f761f1eb] | 1 | /*
|
---|
[4bb31f7] | 2 | * Copyright (c) 2008 Jakub Jermar
|
---|
[f761f1eb] | 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 |
|
---|
[a000878c] | 29 | /** @addtogroup ia32mm
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * @ingroup ia32mm, amd64mm
|
---|
| 34 | */
|
---|
| 35 |
|
---|
[f761f1eb] | 36 | #include <mm/frame.h>
|
---|
| 37 | #include <arch/mm/frame.h>
|
---|
[20d50a1] | 38 | #include <mm/as.h>
|
---|
[f761f1eb] | 39 | #include <config.h>
|
---|
[f6297e0] | 40 | #include <arch/boot/boot.h>
|
---|
[5d721f0] | 41 | #include <arch/boot/memmap.h>
|
---|
[84dd253] | 42 | #include <panic.h>
|
---|
[328f2934] | 43 | #include <debug.h>
|
---|
[95498e5] | 44 | #include <align.h>
|
---|
[085d973] | 45 | #include <macros.h>
|
---|
| 46 | #include <print.h>
|
---|
| 47 |
|
---|
[dc0b964] | 48 | #define PHYSMEM_LIMIT32 UINT64_C(0x07c000000)
|
---|
| 49 | #define PHYSMEM_LIMIT64 UINT64_C(0x200000000)
|
---|
[cae5404] | 50 |
|
---|
[f6297e0] | 51 | size_t hardcoded_unmapped_ktext_size = 0;
|
---|
| 52 | size_t hardcoded_unmapped_kdata_size = 0;
|
---|
[f761f1eb] | 53 |
|
---|
[7f1c620] | 54 | uintptr_t last_frame = 0;
|
---|
[95498e5] | 55 |
|
---|
[085d973] | 56 | static void init_e820_memory(pfn_t minconf)
|
---|
| 57 | {
|
---|
[b07c332] | 58 | unsigned int i;
|
---|
[cae5404] | 59 |
|
---|
[085d973] | 60 | for (i = 0; i < e820counter; i++) {
|
---|
[e49e234] | 61 | uint64_t base = e820table[i].base_address;
|
---|
| 62 | uint64_t size = e820table[i].size;
|
---|
| 63 |
|
---|
| 64 | #ifdef __32_BITS__
|
---|
[cae5404] | 65 | /*
|
---|
| 66 | * XXX FIXME:
|
---|
| 67 | *
|
---|
[68667ce] | 68 | * Ignore zones which start above PHYSMEM_LIMIT32
|
---|
| 69 | * or clip zones which go beyond PHYSMEM_LIMIT32.
|
---|
[cae5404] | 70 | *
|
---|
[68667ce] | 71 | * The PHYSMEM_LIMIT32 (2 GB - 64 MB) is a rather
|
---|
[cae5404] | 72 | * arbitrary constant which allows to have at
|
---|
| 73 | * least 64 MB in the kernel address space to
|
---|
| 74 | * map hardware resources.
|
---|
| 75 | *
|
---|
| 76 | * The kernel uses fixed 1:1 identity mapping
|
---|
| 77 | * of the physical memory with 2:2 GB split.
|
---|
| 78 | * This is a severe limitation of the current
|
---|
| 79 | * kernel memory management.
|
---|
| 80 | *
|
---|
| 81 | */
|
---|
| 82 |
|
---|
[68667ce] | 83 | if (base > PHYSMEM_LIMIT32)
|
---|
[e49e234] | 84 | continue;
|
---|
| 85 |
|
---|
[68667ce] | 86 | if (base + size > PHYSMEM_LIMIT32)
|
---|
| 87 | size = PHYSMEM_LIMIT32 - base;
|
---|
| 88 | #endif
|
---|
| 89 |
|
---|
| 90 | #ifdef __64_BITS__
|
---|
| 91 | /*
|
---|
| 92 | * XXX FIXME:
|
---|
| 93 | *
|
---|
| 94 | * Ignore zones which start above PHYSMEM_LIMIT64
|
---|
| 95 | * or clip zones which go beyond PHYSMEM_LIMIT64.
|
---|
| 96 | *
|
---|
[64f6ef04] | 97 | * The PHYSMEM_LIMIT64 (8 GB) is the size of the
|
---|
| 98 | * fixed 1:1 identically mapped physical memory
|
---|
| 99 | * accessible during the bootstrap process.
|
---|
| 100 | * This is a severe limitation of the current
|
---|
| 101 | * kernel memory management.
|
---|
[68667ce] | 102 | *
|
---|
| 103 | */
|
---|
| 104 |
|
---|
| 105 | if (base > PHYSMEM_LIMIT64)
|
---|
| 106 | continue;
|
---|
| 107 |
|
---|
| 108 | if (base + size > PHYSMEM_LIMIT64)
|
---|
| 109 | size = PHYSMEM_LIMIT64 - base;
|
---|
[e49e234] | 110 | #endif
|
---|
[cae5404] | 111 |
|
---|
[085d973] | 112 | if (e820table[i].type == MEMMAP_MEMORY_AVAILABLE) {
|
---|
[720db0c] | 113 | /* To be safe, make the available zone possibly smaller */
|
---|
| 114 | uint64_t new_base = ALIGN_UP(base, FRAME_SIZE);
|
---|
| 115 | uint64_t new_size = ALIGN_DOWN(size - (new_base - base),
|
---|
| 116 | FRAME_SIZE);
|
---|
| 117 |
|
---|
| 118 | pfn_t pfn = ADDR2PFN(new_base);
|
---|
| 119 | size_t count = SIZE2FRAMES(new_size);
|
---|
[b07c332] | 120 |
|
---|
[e49e234] | 121 | pfn_t conf;
|
---|
| 122 | if ((minconf < pfn) || (minconf >= pfn + count))
|
---|
| 123 | conf = pfn;
|
---|
[085d973] | 124 | else
|
---|
| 125 | conf = minconf;
|
---|
[b07c332] | 126 |
|
---|
[e49e234] | 127 | zone_create(pfn, count, conf, ZONE_AVAILABLE);
|
---|
[b07c332] | 128 |
|
---|
[e49e234] | 129 | // XXX this has to be removed
|
---|
[720db0c] | 130 | if (last_frame < ALIGN_UP(new_base + new_size, FRAME_SIZE))
|
---|
| 131 | last_frame = ALIGN_UP(new_base + new_size, FRAME_SIZE);
|
---|
[142084b2] | 132 | } else if ((e820table[i].type == MEMMAP_MEMORY_ACPI) ||
|
---|
| 133 | (e820table[i].type == MEMMAP_MEMORY_NVS)) {
|
---|
| 134 | /* To be safe, make the firmware zone possibly larger */
|
---|
[720db0c] | 135 | uint64_t new_base = ALIGN_DOWN(base, FRAME_SIZE);
|
---|
| 136 | uint64_t new_size = ALIGN_UP(size + (base - new_base),
|
---|
| 137 | FRAME_SIZE);
|
---|
[e49e234] | 138 |
|
---|
[720db0c] | 139 | zone_create(ADDR2PFN(new_base), SIZE2FRAMES(new_size), 0,
|
---|
[142084b2] | 140 | ZONE_FIRMWARE);
|
---|
| 141 | } else {
|
---|
| 142 | /* To be safe, make the reserved zone possibly larger */
|
---|
[720db0c] | 143 | uint64_t new_base = ALIGN_DOWN(base, FRAME_SIZE);
|
---|
| 144 | uint64_t new_size = ALIGN_UP(size + (base - new_base),
|
---|
| 145 | FRAME_SIZE);
|
---|
[e49e234] | 146 |
|
---|
[720db0c] | 147 | zone_create(ADDR2PFN(new_base), SIZE2FRAMES(new_size), 0,
|
---|
[142084b2] | 148 | ZONE_RESERVED);
|
---|
[ae318d3] | 149 | }
|
---|
[085d973] | 150 | }
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[a000878c] | 153 | static const char *e820names[] = {
|
---|
[a7df23c] | 154 | "invalid",
|
---|
| 155 | "available",
|
---|
| 156 | "reserved",
|
---|
| 157 | "acpi",
|
---|
| 158 | "nvs",
|
---|
| 159 | "unusable"
|
---|
| 160 | };
|
---|
[085d973] | 161 |
|
---|
[b07c332] | 162 | void physmem_print(void)
|
---|
[085d973] | 163 | {
|
---|
[43b1e86] | 164 | unsigned int i;
|
---|
[33dac7d] | 165 | printf("[base ] [size ] [name ]\n");
|
---|
[43b1e86] | 166 |
|
---|
[085d973] | 167 | for (i = 0; i < e820counter; i++) {
|
---|
[33dac7d] | 168 | const char *name;
|
---|
| 169 |
|
---|
[085d973] | 170 | if (e820table[i].type <= MEMMAP_MEMORY_UNUSABLE)
|
---|
| 171 | name = e820names[e820table[i].type];
|
---|
| 172 | else
|
---|
| 173 | name = "invalid";
|
---|
[43b1e86] | 174 |
|
---|
[33dac7d] | 175 | printf("%#018" PRIx64 " %#018" PRIx64" %s\n", e820table[i].base_address,
|
---|
[ba4a63b8] | 176 | e820table[i].size, name);
|
---|
[0b5f9fa] | 177 | }
|
---|
[085d973] | 178 | }
|
---|
| 179 |
|
---|
| 180 |
|
---|
[f761f1eb] | 181 | void frame_arch_init(void)
|
---|
| 182 | {
|
---|
[b07c332] | 183 | pfn_t minconf;
|
---|
[4cc2ddd] | 184 |
|
---|
[f761f1eb] | 185 | if (config.cpu_active == 1) {
|
---|
[085d973] | 186 | minconf = 1;
|
---|
[ae318d3] | 187 |
|
---|
[085d973] | 188 | #ifdef CONFIG_SMP
|
---|
| 189 | minconf = max(minconf,
|
---|
[ba4a63b8] | 190 | ADDR2PFN(AP_BOOT_OFFSET + hardcoded_unmapped_ktext_size +
|
---|
| 191 | hardcoded_unmapped_kdata_size));
|
---|
[085d973] | 192 | #endif
|
---|
[33dac7d] | 193 |
|
---|
[085d973] | 194 | init_e820_memory(minconf);
|
---|
[ae318d3] | 195 |
|
---|
[328f2934] | 196 | /* Reserve frame 0 (BIOS data) */
|
---|
[085d973] | 197 | frame_mark_unavailable(0, 1);
|
---|
[328f2934] | 198 |
|
---|
[66def8d] | 199 | #ifdef CONFIG_SMP
|
---|
| 200 | /* Reserve AP real mode bootstrap memory */
|
---|
[142084b2] | 201 | frame_mark_unavailable(AP_BOOT_OFFSET >> FRAME_WIDTH,
|
---|
[ba4a63b8] | 202 | (hardcoded_unmapped_ktext_size +
|
---|
| 203 | hardcoded_unmapped_kdata_size) >> FRAME_WIDTH);
|
---|
[66def8d] | 204 | #endif
|
---|
[f761f1eb] | 205 | }
|
---|
| 206 | }
|
---|
[b45c443] | 207 |
|
---|
[06e1e95] | 208 | /** @}
|
---|
[b45c443] | 209 | */
|
---|