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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 720db0c was 720db0c, checked in by Martin Decky <martin@…>, 15 years ago

fix erroneous resizing of physical zones (ticket #240)

  • Property mode set to 100644
File size: 5.1 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
47#include <print.h>
48
49#define PHYSMEM_LIMIT 0x7C000000ull
50
51size_t hardcoded_unmapped_ktext_size = 0;
52size_t hardcoded_unmapped_kdata_size = 0;
53
54uintptr_t last_frame = 0;
55
56static void init_e820_memory(pfn_t minconf)
57{
58 unsigned int i;
59
60 for (i = 0; i < e820counter; i++) {
61 uint64_t base = e820table[i].base_address;
62 uint64_t size = e820table[i].size;
63
64#ifdef __32_BITS__
65 /*
66 * XXX FIXME:
67 *
68 * Ignore zones which start above PHYSMEM_LIMIT
69 * or clip zones which go beyond PHYSMEM_LIMIT.
70 *
71 * The PHYSMEM_LIMIT (2 GB - 64 MB) is a rather
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
83 if (base > PHYSMEM_LIMIT)
84 continue;
85
86 if (base + size > PHYSMEM_LIMIT)
87 size = PHYSMEM_LIMIT - base;
88#endif
89
90 if (e820table[i].type == MEMMAP_MEMORY_AVAILABLE) {
91 /* To be safe, make the available zone possibly smaller */
92 uint64_t new_base = ALIGN_UP(base, FRAME_SIZE);
93 uint64_t new_size = ALIGN_DOWN(size - (new_base - base),
94 FRAME_SIZE);
95
96 pfn_t pfn = ADDR2PFN(new_base);
97 size_t count = SIZE2FRAMES(new_size);
98
99 pfn_t conf;
100 if ((minconf < pfn) || (minconf >= pfn + count))
101 conf = pfn;
102 else
103 conf = minconf;
104
105 zone_create(pfn, count, conf, ZONE_AVAILABLE);
106
107 // XXX this has to be removed
108 if (last_frame < ALIGN_UP(new_base + new_size, FRAME_SIZE))
109 last_frame = ALIGN_UP(new_base + new_size, FRAME_SIZE);
110 }
111
112 if (e820table[i].type == MEMMAP_MEMORY_RESERVED) {
113 /* To be safe, make the reserved zone possibly larger */
114 uint64_t new_base = ALIGN_DOWN(base, FRAME_SIZE);
115 uint64_t new_size = ALIGN_UP(size + (base - new_base),
116 FRAME_SIZE);
117
118 zone_create(ADDR2PFN(new_base), SIZE2FRAMES(new_size), 0,
119 ZONE_RESERVED);
120 }
121
122 if (e820table[i].type == MEMMAP_MEMORY_ACPI) {
123 /* To be safe, make the firmware zone possibly larger */
124 uint64_t new_base = ALIGN_DOWN(base, FRAME_SIZE);
125 uint64_t new_size = ALIGN_UP(size + (base - new_base),
126 FRAME_SIZE);
127
128 zone_create(ADDR2PFN(new_base), SIZE2FRAMES(new_size), 0,
129 ZONE_FIRMWARE);
130 }
131 }
132}
133
134static const char *e820names[] = {
135 "invalid",
136 "available",
137 "reserved",
138 "acpi",
139 "nvs",
140 "unusable"
141};
142
143void physmem_print(void)
144{
145 unsigned int i;
146 const char *name;
147
148 printf("[base ] [size ] [name\n");
149
150 for (i = 0; i < e820counter; i++) {
151 if (e820table[i].type <= MEMMAP_MEMORY_UNUSABLE)
152 name = e820names[e820table[i].type];
153 else
154 name = "invalid";
155
156 printf("%#18llx %#18llx %s\n", e820table[i].base_address,
157 e820table[i].size, name);
158 }
159}
160
161
162void frame_arch_init(void)
163{
164 pfn_t minconf;
165
166 if (config.cpu_active == 1) {
167 minconf = 1;
168
169#ifdef CONFIG_SMP
170 minconf = max(minconf,
171 ADDR2PFN(AP_BOOT_OFFSET + hardcoded_unmapped_ktext_size +
172 hardcoded_unmapped_kdata_size));
173#endif
174
175 init_e820_memory(minconf);
176
177 /* Reserve frame 0 (BIOS data) */
178 frame_mark_unavailable(0, 1);
179
180#ifdef CONFIG_SMP
181 /* Reserve AP real mode bootstrap memory */
182 frame_mark_unavailable(AP_BOOT_OFFSET >> FRAME_WIDTH,
183 (hardcoded_unmapped_ktext_size +
184 hardcoded_unmapped_kdata_size) >> FRAME_WIDTH);
185#endif
186 }
187}
188
189/** @}
190 */
Note: See TracBrowser for help on using the repository browser.