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