source: mainline/kernel/generic/src/mm/km.c@ 9306cd7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9306cd7 was 63e27ef, checked in by Jiri Svoboda <jiri@…>, 8 years ago

ASSERT → assert

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