source: mainline/kernel/generic/src/mm/km.c@ 492ddc9

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

Match km_page_free() size parameter in km_unmap_aligned() with that of
km_page_alloc() in km_map_aligned().

  • Property mode set to 100644
File size: 7.5 KB
Line 
1/*
2 * Copyright (c) 2011 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 genericmm
30 * @{
31 */
32
33/**
34 * @file
35 * @brief Kernel virtual memory setup.
36 */
37
38#include <mm/km.h>
39#include <arch/mm/km.h>
40#include <mm/page.h>
41#include <mm/frame.h>
42#include <mm/asid.h>
43#include <config.h>
44#include <typedefs.h>
45#include <lib/ra.h>
46#include <debug.h>
47#include <arch.h>
48#include <align.h>
49#include <macros.h>
50#include <bitops.h>
51
52static ra_arena_t *km_ni_arena;
53
54#define DEFERRED_PAGES_MAX (PAGE_SIZE / sizeof(uintptr_t))
55
56/** Number of freed pages in the deferred buffer. */
57static volatile unsigned deferred_pages;
58/** Buffer of deferred freed pages. */
59static uintptr_t deferred_page[DEFERRED_PAGES_MAX];
60
61/** Flush the buffer of deferred freed pages.
62 *
63 * @return Number of freed pages.
64 */
65static unsigned km_flush_deferred(void)
66{
67 unsigned i = 0;
68 ipl_t ipl;
69
70 ipl = tlb_shootdown_start(TLB_INVL_ASID, ASID_KERNEL, 0, 0);
71
72 for (i = 0; i < deferred_pages; i++) {
73 page_mapping_remove(AS_KERNEL, deferred_page[i]);
74 km_page_free(deferred_page[i], PAGE_SIZE);
75 }
76
77 tlb_invalidate_asid(ASID_KERNEL);
78
79 as_invalidate_translation_cache(AS_KERNEL, 0, -1);
80 tlb_shootdown_finalize(ipl);
81
82 return i;
83}
84
85/** Architecture dependent setup of identity-mapped kernel memory. */
86void km_identity_init(void)
87{
88 km_identity_arch_init();
89 config.identity_configured = true;
90}
91
92/** Architecture dependent setup of non-identity-mapped kernel memory. */
93void km_non_identity_init(void)
94{
95 km_ni_arena = ra_arena_create();
96 ASSERT(km_ni_arena != NULL);
97 km_non_identity_arch_init();
98 config.non_identity_configured = true;
99}
100
101bool km_is_non_identity(uintptr_t addr)
102{
103 return km_is_non_identity_arch(addr);
104}
105
106void km_non_identity_span_add(uintptr_t base, size_t size)
107{
108 bool span_added;
109
110 page_mapping_make_global(base, size);
111
112 span_added = ra_span_add(km_ni_arena, base, size);
113 ASSERT(span_added);
114}
115
116uintptr_t km_page_alloc(size_t size, size_t align)
117{
118 return ra_alloc(km_ni_arena, size, align);
119}
120
121void km_page_free(uintptr_t page, size_t size)
122{
123 ra_free(km_ni_arena, page, size);
124}
125
126static uintptr_t
127km_map_aligned(uintptr_t paddr, size_t size, unsigned int flags)
128{
129 uintptr_t vaddr;
130 size_t align;
131 uintptr_t offs;
132
133 ASSERT(ALIGN_DOWN(paddr, FRAME_SIZE) == paddr);
134 ASSERT(ALIGN_UP(size, FRAME_SIZE) == size);
135
136 /* Enforce natural or at least PAGE_SIZE alignment. */
137 align = ispwr2(size) ? size : (1U << (fnzb(size) + 1));
138 vaddr = km_page_alloc(size, max(PAGE_SIZE, align));
139
140 page_table_lock(AS_KERNEL, true);
141 for (offs = 0; offs < size; offs += PAGE_SIZE) {
142 page_mapping_insert(AS_KERNEL, vaddr + offs, paddr + offs,
143 flags);
144 }
145 page_table_unlock(AS_KERNEL, true);
146
147 return vaddr;
148}
149
150static void km_unmap_aligned(uintptr_t vaddr, size_t size)
151{
152 uintptr_t offs;
153 size_t align;
154 ipl_t ipl;
155
156 ASSERT(ALIGN_DOWN(vaddr, PAGE_SIZE) == vaddr);
157 ASSERT(ALIGN_UP(size, PAGE_SIZE) == size);
158
159 page_table_lock(AS_KERNEL, true);
160
161 ipl = tlb_shootdown_start(TLB_INVL_ASID, ASID_KERNEL, 0, 0);
162
163 for (offs = 0; offs < size; offs += PAGE_SIZE)
164 page_mapping_remove(AS_KERNEL, vaddr + offs);
165
166 tlb_invalidate_asid(ASID_KERNEL);
167
168 as_invalidate_translation_cache(AS_KERNEL, 0, -1);
169 tlb_shootdown_finalize(ipl);
170 page_table_unlock(AS_KERNEL, true);
171
172 /*
173 * Match the size parameter with that of km_page_alloc() in
174 * km_map_aligned().
175 */
176 align = ispwr2(size) ? size : (1U << (fnzb(size) + 1));
177 km_page_free(vaddr, max(PAGE_SIZE, align));
178}
179
180/** Map a piece of physical address space into the virtual address space.
181 *
182 * @param paddr Physical address to be mapped. May be unaligned.
183 * @param size Size of area starting at paddr to be mapped.
184 * @param flags Protection flags to be used for the mapping.
185 *
186 * @return New virtual address mapped to paddr.
187 */
188uintptr_t km_map(uintptr_t paddr, size_t size, unsigned int flags)
189{
190 uintptr_t page;
191 size_t offs;
192
193 offs = paddr - ALIGN_DOWN(paddr, FRAME_SIZE);
194 page = km_map_aligned(ALIGN_DOWN(paddr, FRAME_SIZE),
195 ALIGN_UP(size + offs, FRAME_SIZE), flags);
196
197 return page + offs;
198}
199
200/** Unmap a piece of virtual address space.
201 *
202 * @param vaddr Virtual address to be unmapped. May be unaligned, but
203 * it must a value previously returned by km_map().
204 * @param size Size of area starting at vaddr to be unmapped.
205 */
206void km_unmap(uintptr_t vaddr, size_t size)
207{
208 size_t offs;
209
210 offs = vaddr - ALIGN_DOWN(vaddr, PAGE_SIZE);
211 km_unmap_aligned(ALIGN_DOWN(vaddr, PAGE_SIZE),
212 ALIGN_UP(size + offs, PAGE_SIZE));
213}
214
215/** Unmap kernel non-identity page.
216 *
217 * @param[in] page Non-identity page to be unmapped.
218 */
219static void km_unmap_deferred(uintptr_t page)
220{
221 page_table_lock(AS_KERNEL, true);
222
223 if (deferred_pages == DEFERRED_PAGES_MAX) {
224 (void) km_flush_deferred();
225 deferred_pages = 0;
226 }
227
228 deferred_page[deferred_pages++] = page;
229
230 page_table_unlock(AS_KERNEL, true);
231}
232
233/** Create a temporary page.
234 *
235 * The page is mapped read/write to a newly allocated frame of physical memory.
236 * The page must be returned back to the system by a call to
237 * km_temporary_page_put().
238 *
239 * @param[inout] framep Pointer to a variable which will receive the physical
240 * address of the allocated frame.
241 * @param[in] flags Frame allocation flags. FRAME_NONE or FRAME_NO_RESERVE.
242 * @return Virtual address of the allocated frame.
243 */
244uintptr_t km_temporary_page_get(uintptr_t *framep, frame_flags_t flags)
245{
246 uintptr_t frame;
247 uintptr_t page;
248
249 ASSERT(THREAD);
250 ASSERT(framep);
251 ASSERT(!(flags & ~FRAME_NO_RESERVE));
252
253 /*
254 * Allocate a frame, preferably from high memory.
255 */
256 frame = (uintptr_t) frame_alloc(ONE_FRAME,
257 FRAME_HIGHMEM | FRAME_ATOMIC | flags);
258 if (frame) {
259 page = km_map(frame, PAGE_SIZE,
260 PAGE_READ | PAGE_WRITE | PAGE_CACHEABLE);
261 ASSERT(page); // FIXME
262 } else {
263 frame = (uintptr_t) frame_alloc_noreserve(ONE_FRAME,
264 FRAME_LOWMEM);
265 page = PA2KA(frame);
266 }
267
268 *framep = frame;
269 return page;
270}
271
272/** Destroy a temporary page.
273 *
274 * This function destroys a temporary page previously created by
275 * km_temporary_page_get(). The page destruction may be immediate or deferred.
276 * The frame mapped by the destroyed page is not freed.
277 *
278 * @param[in] page Temporary page to be destroyed.
279 */
280void km_temporary_page_put(uintptr_t page)
281{
282 ASSERT(THREAD);
283
284 if (km_is_non_identity(page))
285 km_unmap_deferred(page);
286}
287
288/** @}
289 */
290
Note: See TracBrowser for help on using the repository browser.