booke_pmap.c revision 1.4 1 /*-
2 * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects
7 * Agency and which was developed by Matt Thomas of 3am Software Foundry.
8 *
9 * This material is based upon work supported by the Defense Advanced Research
10 * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under
11 * Contract No. N66001-09-C-2073.
12 * Approved for Public Release, Distribution Unlimited
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 #define __PMAP_PRIVATE
37
38 #include <sys/cdefs.h>
39
40 __KERNEL_RCSID(0, "$NetBSD: booke_pmap.c,v 1.4 2011/06/05 16:52:24 matt Exp $");
41
42 #include <sys/param.h>
43 #include <sys/kcore.h>
44 #include <sys/buf.h>
45
46 #include <uvm/uvm_extern.h>
47
48 #include <machine/pmap.h>
49
50 /*
51 * Initialize the kernel pmap.
52 */
53 #ifdef MULTIPROCESSOR
54 #define PMAP_SIZE offsetof(struct pmap, pm_pai[MAXCPUS])
55 #else
56 #define PMAP_SIZE sizeof(struct pmap)
57 #endif
58
59 CTASSERT(sizeof(struct pmap_segtab) == NBPG);
60
61 void
62 pmap_procwr(struct proc *p, vaddr_t va, size_t len)
63 {
64 struct pmap * const pmap = p->p_vmspace->vm_map.pmap;
65 vsize_t off = va & PAGE_SIZE;
66
67 kpreempt_disable();
68 for (const vaddr_t eva = va + len; va < eva; off = 0) {
69 const vaddr_t segeva = min(va + len, va - off + PAGE_SIZE);
70 pt_entry_t * const ptep = pmap_pte_lookup(pmap, va);
71 if (ptep == NULL) {
72 va = segeva;
73 continue;
74 }
75 pt_entry_t pt_entry = *ptep;
76 if (!pte_valid_p(pt_entry) || !pte_exec_p(pt_entry)) {
77 va = segeva;
78 continue;
79 }
80 kpreempt_enable();
81 dcache_wb(pte_to_paddr(pt_entry), segeva - va);
82 icache_inv(pte_to_paddr(pt_entry), segeva - va);
83 kpreempt_disable();
84 va = segeva;
85 }
86 kpreempt_enable();
87 }
88
89 void
90 pmap_md_page_syncicache(struct vm_page *pg, __cpuset_t onproc)
91 {
92 /*
93 * If onproc is empty, we could do a
94 * pmap_page_protect(pg, VM_PROT_NONE) and remove all
95 * mappings of the page and clear its execness. Then
96 * the next time page is faulted, it will get icache
97 * synched. But this is easier. :)
98 */
99 paddr_t pa = VM_PAGE_TO_PHYS(pg);
100 dcache_wb_page(pa);
101 icache_inv_page(pa);
102 }
103
104 vaddr_t
105 pmap_md_direct_map_paddr(paddr_t pa)
106 {
107 return (vaddr_t) pa;
108 }
109
110 bool
111 pmap_md_direct_mapped_vaddr_p(vaddr_t va)
112 {
113 return va < VM_MIN_KERNEL_ADDRESS || VM_MAX_KERNEL_ADDRESS <= va;
114 }
115
116 paddr_t
117 pmap_md_direct_mapped_vaddr_to_paddr(vaddr_t va)
118 {
119 return (paddr_t) va;
120 }
121
122 /*
123 * Bootstrap the system enough to run with virtual memory.
124 * firstaddr is the first unused kseg0 address (not page aligned).
125 */
126 void
127 pmap_bootstrap(vaddr_t startkernel, vaddr_t endkernel,
128 const phys_ram_seg_t *avail, size_t cnt)
129 {
130 for (size_t i = 0; i < cnt; i++) {
131 printf(" uvm_page_physload(%#lx,%#lx,%#lx,%#lx,%d)",
132 atop(avail[i].start),
133 atop(avail[i].start + avail[i].size) - 1,
134 atop(avail[i].start),
135 atop(avail[i].start + avail[i].size) - 1,
136 VM_FREELIST_DEFAULT);
137 uvm_page_physload(
138 atop(avail[i].start),
139 atop(avail[i].start + avail[i].size) - 1,
140 atop(avail[i].start),
141 atop(avail[i].start + avail[i].size) - 1,
142 VM_FREELIST_DEFAULT);
143 }
144
145 pmap_tlb_info_init(&pmap_tlb0_info); /* init the lock */
146
147 /*
148 * Compute the number of pages kmem_map will have.
149 */
150 kmeminit_nkmempages();
151
152 /*
153 * Figure out how many PTE's are necessary to map the kernel.
154 * We also reserve space for kmem_alloc_pageable() for vm_fork().
155 */
156
157 /* Get size of buffer cache and set an upper limit */
158 buf_setvalimit((VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) / 8);
159 vsize_t bufsz = buf_memcalc();
160 buf_setvalimit(bufsz);
161
162 vsize_t nsegtabs = pmap_round_seg(VM_PHYS_SIZE
163 + (ubc_nwins << ubc_winshift)
164 + bufsz
165 + 16 * NCARGS
166 + pager_map_size
167 + maxproc * USPACE
168 #ifdef SYSVSHM
169 + NBPG * shminfo.shmall
170 #endif
171 + NBPG * nkmempages);
172
173 /*
174 * Initialize `FYI' variables. Note we're relying on
175 * the fact that BSEARCH sorts the vm_physmem[] array
176 * for us. Must do this before uvm_pageboot_alloc()
177 * can be called.
178 */
179 pmap_limits.avail_start = vm_physmem[0].start << PGSHIFT;
180 pmap_limits.avail_end = vm_physmem[vm_nphysseg - 1].end << PGSHIFT;
181 const vsize_t max_nsegtabs =
182 (pmap_round_seg(VM_MAX_KERNEL_ADDRESS)
183 - pmap_trunc_seg(VM_MIN_KERNEL_ADDRESS)) / NBSEG;
184 if (nsegtabs >= max_nsegtabs) {
185 pmap_limits.virtual_end = VM_MAX_KERNEL_ADDRESS;
186 nsegtabs = max_nsegtabs;
187 } else {
188 pmap_limits.virtual_end = VM_MIN_KERNEL_ADDRESS
189 + nsegtabs * NBSEG;
190 }
191
192 pmap_pvlist_lock_init(curcpu()->ci_ci.dcache_line_size);
193
194 /*
195 * Now actually allocate the kernel PTE array (must be done
196 * after virtual_end is initialized).
197 */
198 vaddr_t segtabs =
199 uvm_pageboot_alloc(NBPG * nsegtabs + sizeof(struct pmap_segtab));
200
201 /*
202 * Initialize the kernel's two-level page level. This only wastes
203 * an extra page for the segment table and allows the user/kernel
204 * access to be common.
205 */
206 struct pmap_segtab * const stp = (void *)segtabs;
207 segtabs += round_page(sizeof(struct pmap_segtab));
208 pt_entry_t **ptp = &stp->seg_tab[VM_MIN_KERNEL_ADDRESS >> SEGSHIFT];
209 for (u_int i = 0; i < nsegtabs; i++, segtabs += NBPG) {
210 *ptp++ = (void *)segtabs;
211 }
212 pmap_kernel()->pm_segtab = stp;
213 curcpu()->ci_pmap_kern_segtab = stp;
214 printf(" kern_segtab=%p", stp);
215
216 #if 0
217 nsegtabs = (physmem + NPTEPG - 1) / NPTEPG;
218 segtabs = uvm_pageboot_alloc(NBPG * nsegtabs);
219 ptp = stp->seg_tab;
220 pt_entry_t pt_entry = PTE_M|PTE_xX|PTE_xR;
221 pt_entry_t *ptep = (void *)segtabs;
222 printf("%s: allocated %lu page table pages for mapping %u pages\n",
223 __func__, nsegtabs, physmem);
224 for (u_int i = 0; i < nsegtabs; i++, segtabs += NBPG, ptp++) {
225 *ptp = ptep;
226 for (u_int j = 0; j < NPTEPG; j++, ptep++) {
227 *ptep = pt_entry;
228 pt_entry += NBPG;
229 }
230 printf(" [%u]=%p (%#x)", i, *ptp, **ptp);
231 pt_entry |= PTE_xW;
232 pt_entry &= ~PTE_xX;
233 }
234
235 /*
236 * Now make everything before the kernel inaccessible.
237 */
238 for (u_int i = 0; i < startkernel / NBPG; i += NBPG) {
239 stp->seg_tab[i >> SEGSHIFT][(i & SEGOFSET) >> PAGE_SHIFT] = 0;
240 }
241 #endif
242
243 /*
244 * Initialize the pools.
245 */
246 pool_init(&pmap_pmap_pool, PMAP_SIZE, 0, 0, 0, "pmappl",
247 &pool_allocator_nointr, IPL_NONE);
248 pool_init(&pmap_pv_pool, sizeof(struct pv_entry), 0, 0, 0, "pvpl",
249 &pmap_pv_page_allocator, IPL_NONE);
250
251 tlb_set_asid(0);
252 }
253
254 struct vm_page *
255 pmap_md_alloc_poolpage(int flags)
256 {
257 /*
258 * Any managed page works for us.
259 */
260 return uvm_pagealloc(NULL, 0, NULL, flags);
261 }
262
263 void
264 pmap_zero_page(paddr_t pa)
265 {
266 // printf("%s(%#lx): calling dcache_zero_page(%#lx)\n", __func__, pa, pa);
267 dcache_zero_page(pa);
268 }
269
270 void
271 pmap_copy_page(paddr_t src, paddr_t dst)
272 {
273 const size_t line_size = curcpu()->ci_ci.dcache_line_size;
274 const paddr_t end = src + PAGE_SIZE;
275
276 while (src < end) {
277 __asm(
278 "dcbt %2,%1" "\n\t" /* touch next src cachline */
279 "dcba 0,%1" "\n\t" /* don't fetch dst cacheline */
280 :: "b"(src), "b"(dst), "b"(line_size));
281 for (u_int i = 0;
282 i < line_size;
283 src += 32, dst += 32, i += 32) {
284 __asm(
285 "lmw 24,0(%0)" "\n\t"
286 "stmw 24,0(%1)"
287 :: "b"(src), "b"(dst)
288 : "r24", "r25", "r26", "r27",
289 "r28", "r29", "r30", "r31");
290 }
291 }
292 }
293
294 void
295 pmap_md_init(void)
296 {
297
298 /* nothing for now */
299 }
300
301 bool
302 pmap_md_io_vaddr_p(vaddr_t va)
303 {
304 return va >= pmap_limits.avail_end
305 && !(VM_MIN_KERNEL_ADDRESS <= va && va < VM_MAX_KERNEL_ADDRESS);
306 }
307
308