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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 10a5479d was 7943c43, checked in by Jakub Jermar <jakub@…>, 13 years ago

Merge mainline changes.

  • Property mode set to 100644
File size: 4.9 KB
Line 
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
48size_t hardcoded_unmapped_ktext_size = 0;
49size_t hardcoded_unmapped_kdata_size = 0;
50
51static void init_e820_memory(pfn_t minconf, bool low)
52{
53 unsigned int i;
54
55 for (i = 0; i < e820counter; i++) {
56 uint64_t base64 = e820table[i].base_address;
57 uint64_t size64 = e820table[i].size;
58
59#ifdef KARCH_ia32
60 /*
61 * Restrict the e820 table entries to 32-bits.
62 */
63 if (base64 >= 0x100000000ULL)
64 continue;
65 if (base64 + size64 > 0x100000000ULL)
66 size64 -= base64 + size64 - 0x100000000ULL;
67#endif
68
69 uintptr_t base = (uintptr_t) base64;
70 size_t size = (size_t) size64;
71
72 if (!frame_adjust_zone_bounds(low, &base, &size))
73 continue;
74
75 if (e820table[i].type == MEMMAP_MEMORY_AVAILABLE) {
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);
80
81 size_t count = SIZE2FRAMES(new_size);
82 pfn_t pfn = ADDR2PFN(new_base);
83 pfn_t conf;
84
85 if (low) {
86 if ((minconf < pfn) || (minconf >= pfn + count))
87 conf = pfn;
88 else
89 conf = minconf;
90 zone_create(pfn, count, conf,
91 ZONE_AVAILABLE | ZONE_LOWMEM);
92 } else {
93 conf = zone_external_conf_alloc(count);
94 if (conf != 0)
95 zone_create(pfn, count, conf,
96 ZONE_AVAILABLE | ZONE_HIGHMEM);
97 }
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 */
101 uint64_t new_base = ALIGN_DOWN(base, FRAME_SIZE);
102 uint64_t new_size = ALIGN_UP(size + (base - new_base),
103 FRAME_SIZE);
104
105 zone_create(ADDR2PFN(new_base), SIZE2FRAMES(new_size), 0,
106 ZONE_FIRMWARE);
107 } else {
108 /* To be safe, make the reserved zone possibly larger */
109 uint64_t new_base = ALIGN_DOWN(base, FRAME_SIZE);
110 uint64_t new_size = ALIGN_UP(size + (base - new_base),
111 FRAME_SIZE);
112
113 zone_create(ADDR2PFN(new_base), SIZE2FRAMES(new_size), 0,
114 ZONE_RESERVED);
115 }
116 }
117}
118
119static const char *e820names[] = {
120 "invalid",
121 "available",
122 "reserved",
123 "acpi",
124 "nvs",
125 "unusable"
126};
127
128void physmem_print(void)
129{
130 unsigned int i;
131 printf("[base ] [size ] [name ]\n");
132
133 for (i = 0; i < e820counter; i++) {
134 const char *name;
135
136 if (e820table[i].type <= MEMMAP_MEMORY_UNUSABLE)
137 name = e820names[e820table[i].type];
138 else
139 name = "invalid";
140
141 printf("%#018" PRIx64 " %#018" PRIx64" %s\n", e820table[i].base_address,
142 e820table[i].size, name);
143 }
144}
145
146
147void frame_low_arch_init(void)
148{
149 pfn_t minconf;
150
151 if (config.cpu_active == 1) {
152 minconf = 1;
153
154#ifdef CONFIG_SMP
155 minconf = max(minconf,
156 ADDR2PFN(AP_BOOT_OFFSET + hardcoded_unmapped_ktext_size +
157 hardcoded_unmapped_kdata_size));
158#endif
159
160 init_e820_memory(minconf, true);
161
162 /* Reserve frame 0 (BIOS data) */
163 frame_mark_unavailable(0, 1);
164
165#ifdef CONFIG_SMP
166 /* Reserve AP real mode bootstrap memory */
167 frame_mark_unavailable(AP_BOOT_OFFSET >> FRAME_WIDTH,
168 (hardcoded_unmapped_ktext_size +
169 hardcoded_unmapped_kdata_size) >> FRAME_WIDTH);
170#endif
171 }
172}
173
174void frame_high_arch_init(void)
175{
176 if (config.cpu_active == 1)
177 init_e820_memory(0, false);
178}
179
180/** @}
181 */
Note: See TracBrowser for help on using the repository browser.