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 |
|
---|
49 | static ra_arena_t *km_ni_arena;
|
---|
50 |
|
---|
51 | #define DEFERRED_PAGES_MAX (PAGE_SIZE / sizeof(uintptr_t))
|
---|
52 |
|
---|
53 | /** Number of freed pages in the deferred buffer. */
|
---|
54 | static volatile unsigned deferred_pages;
|
---|
55 | /** Buffer of deferred freed pages. */
|
---|
56 | static uintptr_t deferred_page[DEFERRED_PAGES_MAX];
|
---|
57 |
|
---|
58 | /** Flush the buffer of deferred freed pages.
|
---|
59 | *
|
---|
60 | * @return Number of freed pages.
|
---|
61 | */
|
---|
62 | static unsigned km_flush_deferred(void)
|
---|
63 | {
|
---|
64 | unsigned i = 0;
|
---|
65 | ipl_t ipl;
|
---|
66 |
|
---|
67 | ipl = tlb_shootdown_start(TLB_INVL_ASID, ASID_KERNEL, 0, 0);
|
---|
68 |
|
---|
69 | for (i = 0; i < deferred_pages; i++) {
|
---|
70 | page_mapping_remove(AS_KERNEL, deferred_page[i]);
|
---|
71 | km_page_free(deferred_page[i], PAGE_SIZE);
|
---|
72 | }
|
---|
73 |
|
---|
74 | tlb_invalidate_asid(ASID_KERNEL);
|
---|
75 |
|
---|
76 | as_invalidate_translation_cache(AS_KERNEL, 0, -1);
|
---|
77 | tlb_shootdown_finalize(ipl);
|
---|
78 |
|
---|
79 | return i;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /** Architecture dependent setup of identity-mapped kernel memory. */
|
---|
83 | void km_identity_init(void)
|
---|
84 | {
|
---|
85 | km_identity_arch_init();
|
---|
86 | config.identity_configured = true;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /** Architecture dependent setup of non-identity-mapped kernel memory. */
|
---|
90 | void km_non_identity_init(void)
|
---|
91 | {
|
---|
92 | km_ni_arena = ra_arena_create();
|
---|
93 | ASSERT(km_ni_arena != NULL);
|
---|
94 | km_non_identity_arch_init();
|
---|
95 | config.non_identity_configured = true;
|
---|
96 | }
|
---|
97 |
|
---|
98 | bool km_is_non_identity(uintptr_t addr)
|
---|
99 | {
|
---|
100 | return km_is_non_identity_arch(addr);
|
---|
101 | }
|
---|
102 |
|
---|
103 | void km_non_identity_span_add(uintptr_t base, size_t size)
|
---|
104 | {
|
---|
105 | bool span_added;
|
---|
106 |
|
---|
107 | page_mapping_make_global(base, size);
|
---|
108 |
|
---|
109 | span_added = ra_span_add(km_ni_arena, base, size);
|
---|
110 | ASSERT(span_added);
|
---|
111 | }
|
---|
112 |
|
---|
113 | uintptr_t km_page_alloc(size_t size, size_t align)
|
---|
114 | {
|
---|
115 | return ra_alloc(km_ni_arena, size, align);
|
---|
116 | }
|
---|
117 |
|
---|
118 | void km_page_free(uintptr_t page, size_t size)
|
---|
119 | {
|
---|
120 | ra_free(km_ni_arena, page, size);
|
---|
121 | }
|
---|
122 |
|
---|
123 | /** Unmap kernen non-identity page.
|
---|
124 | *
|
---|
125 | * @param[in] page Non-identity page to be unmapped.
|
---|
126 | */
|
---|
127 | static void km_unmap_deferred(uintptr_t page)
|
---|
128 | {
|
---|
129 | page_table_lock(AS_KERNEL, true);
|
---|
130 |
|
---|
131 | if (deferred_pages == DEFERRED_PAGES_MAX) {
|
---|
132 | (void) km_flush_deferred();
|
---|
133 | deferred_pages = 0;
|
---|
134 | }
|
---|
135 |
|
---|
136 | deferred_page[deferred_pages++] = page;
|
---|
137 |
|
---|
138 | page_table_unlock(AS_KERNEL, true);
|
---|
139 | }
|
---|
140 |
|
---|
141 | /** Create a temporary page.
|
---|
142 | *
|
---|
143 | * The page is mapped read/write to a newly allocated frame of physical memory.
|
---|
144 | * The page must be returned back to the system by a call to
|
---|
145 | * km_temporary_page_put().
|
---|
146 | *
|
---|
147 | * @param[inout] framep Pointer to a variable which will receive the physical
|
---|
148 | * address of the allocated frame.
|
---|
149 | * @param[in] flags Frame allocation flags. FRAME_NONE or FRAME_NO_RESERVE.
|
---|
150 | * @return Virtual address of the allocated frame.
|
---|
151 | */
|
---|
152 | uintptr_t km_temporary_page_get(uintptr_t *framep, frame_flags_t flags)
|
---|
153 | {
|
---|
154 | uintptr_t frame;
|
---|
155 | uintptr_t page;
|
---|
156 |
|
---|
157 | ASSERT(THREAD);
|
---|
158 | ASSERT(framep);
|
---|
159 | ASSERT(!(flags & ~FRAME_NO_RESERVE));
|
---|
160 |
|
---|
161 | /*
|
---|
162 | * Allocate a frame, preferably from high memory.
|
---|
163 | */
|
---|
164 | frame = (uintptr_t) frame_alloc(ONE_FRAME,
|
---|
165 | FRAME_HIGHMEM | FRAME_ATOMIC | flags);
|
---|
166 | if (frame) {
|
---|
167 | page = km_page_alloc(PAGE_SIZE, PAGE_SIZE);
|
---|
168 | ASSERT(page); // FIXME
|
---|
169 | page_table_lock(AS_KERNEL, true);
|
---|
170 | page_mapping_insert(AS_KERNEL, page, frame,
|
---|
171 | PAGE_CACHEABLE | PAGE_READ | PAGE_WRITE);
|
---|
172 | page_table_unlock(AS_KERNEL, true);
|
---|
173 | } else {
|
---|
174 | frame = (uintptr_t) frame_alloc_noreserve(ONE_FRAME,
|
---|
175 | FRAME_LOWMEM);
|
---|
176 | page = PA2KA(frame);
|
---|
177 | }
|
---|
178 |
|
---|
179 | *framep = frame;
|
---|
180 | return page;
|
---|
181 | }
|
---|
182 |
|
---|
183 | /** Destroy a temporary page.
|
---|
184 | *
|
---|
185 | * This function destroys a temporary page previously created by
|
---|
186 | * km_temporary_page_get(). The page destruction may be immediate or deferred.
|
---|
187 | * The frame mapped by the destroyed page is not freed.
|
---|
188 | *
|
---|
189 | * @param[in] page Temporary page to be destroyed.
|
---|
190 | */
|
---|
191 | void km_temporary_page_put(uintptr_t page)
|
---|
192 | {
|
---|
193 | ASSERT(THREAD);
|
---|
194 |
|
---|
195 | if (km_is_non_identity(page))
|
---|
196 | km_unmap_deferred(page);
|
---|
197 | }
|
---|
198 |
|
---|
199 | /** @}
|
---|
200 | */
|
---|
201 |
|
---|