source: mainline/kernel/arch/ia32/src/mm/frame.c

Last change on this file was bab75df6, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Let kernel code get printf via the standard stdio header. Clean up unused includes.

  • Property mode set to 100644
File size: 4.8 KB
RevLine 
[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
[c5429fe]29/** @addtogroup kernel_ia32_mm
[b45c443]30 * @{
31 */
32/** @file
[c5429fe]33 * @ingroup kernel_ia32_mm, kernel_amd64_mm
[b45c443]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>
[bab75df6]46#include <stdio.h>
[085d973]47
[49ec568]48#define PHYSMEM_LIMIT32 UINT64_C(0x100000000)
49
[80d301ab]50static void init_e820_memory(pfn_t minconf, bool low)
[085d973]51{
[b07c332]52 unsigned int i;
[a35b458]53
[085d973]54 for (i = 0; i < e820counter; i++) {
[9117ef9b]55 uint64_t base64 = e820table[i].base_address;
56 uint64_t size64 = e820table[i].size;
[a35b458]57
[9117ef9b]58#ifdef KARCH_ia32
59 /*
60 * Restrict the e820 table entries to 32-bits.
61 */
[49ec568]62 if (base64 >= PHYSMEM_LIMIT32)
[9117ef9b]63 continue;
[a35b458]64
[49ec568]65 if (base64 + size64 > PHYSMEM_LIMIT32)
66 size64 = PHYSMEM_LIMIT32 - base64;
[9117ef9b]67#endif
[a35b458]68
[9117ef9b]69 uintptr_t base = (uintptr_t) base64;
70 size_t size = (size_t) size64;
[a35b458]71
[ad12b5ea]72 if (!frame_adjust_zone_bounds(low, &base, &size))
73 continue;
[a35b458]74
[085d973]75 if (e820table[i].type == MEMMAP_MEMORY_AVAILABLE) {
[720db0c]76 /* To be safe, make the available zone possibly smaller */
77 uint64_t new_base = ALIGN_UP(base, FRAME_SIZE);
78 uint64_t new_size = ALIGN_DOWN(size - (new_base - base),
79 FRAME_SIZE);
[a35b458]80
[720db0c]81 size_t count = SIZE2FRAMES(new_size);
[80d301ab]82 pfn_t pfn = ADDR2PFN(new_base);
[e49e234]83 pfn_t conf;
[a35b458]84
[80d301ab]85 if (low) {
86 if ((minconf < pfn) || (minconf >= pfn + count))
87 conf = pfn;
88 else
89 conf = minconf;
[3772af6]90 zone_create(pfn, count, conf,
91 ZONE_AVAILABLE | ZONE_LOWMEM);
[80d301ab]92 } else {
93 conf = zone_external_conf_alloc(count);
[3772af6]94 if (conf != 0)
[7852625]95 zone_create(pfn, count, conf,
96 ZONE_AVAILABLE | ZONE_HIGHMEM);
[80d301ab]97 }
[142084b2]98 } else if ((e820table[i].type == MEMMAP_MEMORY_ACPI) ||
99 (e820table[i].type == MEMMAP_MEMORY_NVS)) {
100 /* To be safe, make the firmware zone possibly larger */
[720db0c]101 uint64_t new_base = ALIGN_DOWN(base, FRAME_SIZE);
102 uint64_t new_size = ALIGN_UP(size + (base - new_base),
103 FRAME_SIZE);
[a35b458]104
[720db0c]105 zone_create(ADDR2PFN(new_base), SIZE2FRAMES(new_size), 0,
[142084b2]106 ZONE_FIRMWARE);
107 } else {
108 /* To be safe, make the reserved zone possibly larger */
[720db0c]109 uint64_t new_base = ALIGN_DOWN(base, FRAME_SIZE);
110 uint64_t new_size = ALIGN_UP(size + (base - new_base),
111 FRAME_SIZE);
[a35b458]112
[720db0c]113 zone_create(ADDR2PFN(new_base), SIZE2FRAMES(new_size), 0,
[142084b2]114 ZONE_RESERVED);
[ae318d3]115 }
[085d973]116 }
117}
118
[a000878c]119static const char *e820names[] = {
[a7df23c]120 "invalid",
121 "available",
122 "reserved",
123 "acpi",
124 "nvs",
125 "unusable"
126};
[085d973]127
[b07c332]128void physmem_print(void)
[085d973]129{
[43b1e86]130 unsigned int i;
[33dac7d]131 printf("[base ] [size ] [name ]\n");
[a35b458]132
[085d973]133 for (i = 0; i < e820counter; i++) {
[33dac7d]134 const char *name;
[a35b458]135
[085d973]136 if (e820table[i].type <= MEMMAP_MEMORY_UNUSABLE)
137 name = e820names[e820table[i].type];
138 else
139 name = "invalid";
[a35b458]140
[1433ecda]141 printf("%#018" PRIx64 " %#018" PRIx64 " %s\n", e820table[i].base_address,
[ba4a63b8]142 e820table[i].size, name);
[0b5f9fa]143 }
[085d973]144}
145
[ddcc8a0]146void frame_low_arch_init(void)
[f761f1eb]147{
[b07c332]148 pfn_t minconf;
[a35b458]149
[f761f1eb]150 if (config.cpu_active == 1) {
[085d973]151 minconf = 1;
[a35b458]152
[085d973]153#ifdef CONFIG_SMP
[8a1afd2]154 size_t unmapped_size =
[bae43dc]155 (uintptr_t) unmapped_end - BOOT_OFFSET;
[8a1afd2]156
[085d973]157 minconf = max(minconf,
[8a1afd2]158 ADDR2PFN(AP_BOOT_OFFSET + unmapped_size));
[085d973]159#endif
[a35b458]160
[80d301ab]161 init_e820_memory(minconf, true);
[a35b458]162
[328f2934]163 /* Reserve frame 0 (BIOS data) */
[085d973]164 frame_mark_unavailable(0, 1);
[a35b458]165
[66def8d]166#ifdef CONFIG_SMP
167 /* Reserve AP real mode bootstrap memory */
[142084b2]168 frame_mark_unavailable(AP_BOOT_OFFSET >> FRAME_WIDTH,
[8a1afd2]169 unmapped_size >> FRAME_WIDTH);
[66def8d]170#endif
[f761f1eb]171 }
172}
[b45c443]173
[ddcc8a0]174void frame_high_arch_init(void)
175{
[80d301ab]176 if (config.cpu_active == 1)
177 init_e820_memory(0, false);
[ddcc8a0]178}
179
[06e1e95]180/** @}
[b45c443]181 */
Note: See TracBrowser for help on using the repository browser.