booke_pmap.c revision 1.3.2.2 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.2 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.2 2011/03/05 20:51:33 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.2 rmind pmap_md_page_syncicache(struct vm_page *pg)
91 1.3.2.2 rmind {
92 1.3.2.2 rmind paddr_t pa = VM_PAGE_TO_PHYS(pg);
93 1.3.2.2 rmind dcache_wb_page(pa);
94 1.3.2.2 rmind icache_inv_page(pa);
95 1.3.2.2 rmind }
96 1.3.2.2 rmind
97 1.3.2.2 rmind vaddr_t
98 1.3.2.2 rmind pmap_md_direct_map_paddr(paddr_t pa)
99 1.3.2.2 rmind {
100 1.3.2.2 rmind return (vaddr_t) pa;
101 1.3.2.2 rmind }
102 1.3.2.2 rmind
103 1.3.2.2 rmind bool
104 1.3.2.2 rmind pmap_md_direct_mapped_vaddr_p(vaddr_t va)
105 1.3.2.2 rmind {
106 1.3.2.2 rmind return va < VM_MIN_KERNEL_ADDRESS || VM_MAX_KERNEL_ADDRESS <= va;
107 1.3.2.2 rmind }
108 1.3.2.2 rmind
109 1.3.2.2 rmind paddr_t
110 1.3.2.2 rmind pmap_md_direct_mapped_vaddr_to_paddr(vaddr_t va)
111 1.3.2.2 rmind {
112 1.3.2.2 rmind return (paddr_t) va;
113 1.3.2.2 rmind }
114 1.3.2.2 rmind
115 1.3.2.2 rmind /*
116 1.3.2.2 rmind * Bootstrap the system enough to run with virtual memory.
117 1.3.2.2 rmind * firstaddr is the first unused kseg0 address (not page aligned).
118 1.3.2.2 rmind */
119 1.3.2.2 rmind void
120 1.3.2.2 rmind pmap_bootstrap(vaddr_t startkernel, vaddr_t endkernel,
121 1.3.2.2 rmind const phys_ram_seg_t *avail, size_t cnt)
122 1.3.2.2 rmind {
123 1.3.2.2 rmind for (size_t i = 0; i < cnt; i++) {
124 1.3.2.2 rmind printf(" uvm_page_physload(%#lx,%#lx,%#lx,%#lx,%d)",
125 1.3.2.2 rmind atop(avail[i].start),
126 1.3.2.2 rmind atop(avail[i].start + avail[i].size) - 1,
127 1.3.2.2 rmind atop(avail[i].start),
128 1.3.2.2 rmind atop(avail[i].start + avail[i].size) - 1,
129 1.3.2.2 rmind VM_FREELIST_DEFAULT);
130 1.3.2.2 rmind uvm_page_physload(
131 1.3.2.2 rmind atop(avail[i].start),
132 1.3.2.2 rmind atop(avail[i].start + avail[i].size) - 1,
133 1.3.2.2 rmind atop(avail[i].start),
134 1.3.2.2 rmind atop(avail[i].start + avail[i].size) - 1,
135 1.3.2.2 rmind VM_FREELIST_DEFAULT);
136 1.3.2.2 rmind }
137 1.3.2.2 rmind
138 1.3.2.2 rmind pmap_tlb_info_init(&pmap_tlb0_info); /* init the lock */
139 1.3.2.2 rmind
140 1.3.2.2 rmind /*
141 1.3.2.2 rmind * Compute the number of pages kmem_map will have.
142 1.3.2.2 rmind */
143 1.3.2.2 rmind kmeminit_nkmempages();
144 1.3.2.2 rmind
145 1.3.2.2 rmind /*
146 1.3.2.2 rmind * Figure out how many PTE's are necessary to map the kernel.
147 1.3.2.2 rmind * We also reserve space for kmem_alloc_pageable() for vm_fork().
148 1.3.2.2 rmind */
149 1.3.2.2 rmind
150 1.3.2.2 rmind /* Get size of buffer cache and set an upper limit */
151 1.3.2.2 rmind buf_setvalimit((VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) / 8);
152 1.3.2.2 rmind vsize_t bufsz = buf_memcalc();
153 1.3.2.2 rmind buf_setvalimit(bufsz);
154 1.3.2.2 rmind
155 1.3.2.2 rmind vsize_t nsegtabs = pmap_round_seg(VM_PHYS_SIZE
156 1.3.2.2 rmind + (ubc_nwins << ubc_winshift)
157 1.3.2.2 rmind + bufsz
158 1.3.2.2 rmind + 16 * NCARGS
159 1.3.2.2 rmind + pager_map_size
160 1.3.2.2 rmind + maxproc * USPACE
161 1.3.2.2 rmind #ifdef SYSVSHM
162 1.3.2.2 rmind + NBPG * shminfo.shmall
163 1.3.2.2 rmind #endif
164 1.3.2.2 rmind + NBPG * nkmempages);
165 1.3.2.2 rmind
166 1.3.2.2 rmind /*
167 1.3.2.2 rmind * Initialize `FYI' variables. Note we're relying on
168 1.3.2.2 rmind * the fact that BSEARCH sorts the vm_physmem[] array
169 1.3.2.2 rmind * for us. Must do this before uvm_pageboot_alloc()
170 1.3.2.2 rmind * can be called.
171 1.3.2.2 rmind */
172 1.3.2.2 rmind pmap_limits.avail_start = vm_physmem[0].start << PGSHIFT;
173 1.3.2.2 rmind pmap_limits.avail_end = vm_physmem[vm_nphysseg - 1].end << PGSHIFT;
174 1.3.2.2 rmind const vsize_t max_nsegtabs =
175 1.3.2.2 rmind (pmap_round_seg(VM_MAX_KERNEL_ADDRESS)
176 1.3.2.2 rmind - pmap_trunc_seg(VM_MIN_KERNEL_ADDRESS)) / NBSEG;
177 1.3.2.2 rmind if (nsegtabs >= max_nsegtabs) {
178 1.3.2.2 rmind pmap_limits.virtual_end = VM_MAX_KERNEL_ADDRESS;
179 1.3.2.2 rmind nsegtabs = max_nsegtabs;
180 1.3.2.2 rmind } else {
181 1.3.2.2 rmind pmap_limits.virtual_end = VM_MIN_KERNEL_ADDRESS
182 1.3.2.2 rmind + nsegtabs * NBSEG;
183 1.3.2.2 rmind }
184 1.3.2.2 rmind
185 1.3.2.2 rmind pmap_pvlist_lock_init(curcpu()->ci_ci.dcache_line_size);
186 1.3.2.2 rmind
187 1.3.2.2 rmind /*
188 1.3.2.2 rmind * Now actually allocate the kernel PTE array (must be done
189 1.3.2.2 rmind * after virtual_end is initialized).
190 1.3.2.2 rmind */
191 1.3.2.2 rmind vaddr_t segtabs =
192 1.3.2.2 rmind uvm_pageboot_alloc(NBPG * nsegtabs + sizeof(struct pmap_segtab));
193 1.3.2.2 rmind
194 1.3.2.2 rmind /*
195 1.3.2.2 rmind * Initialize the kernel's two-level page level. This only wastes
196 1.3.2.2 rmind * an extra page for the segment table and allows the user/kernel
197 1.3.2.2 rmind * access to be common.
198 1.3.2.2 rmind */
199 1.3.2.2 rmind struct pmap_segtab * const stp = (void *)segtabs;
200 1.3.2.2 rmind segtabs += round_page(sizeof(struct pmap_segtab));
201 1.3.2.2 rmind pt_entry_t **ptp = &stp->seg_tab[VM_MIN_KERNEL_ADDRESS >> SEGSHIFT];
202 1.3.2.2 rmind for (u_int i = 0; i < nsegtabs; i++, segtabs += NBPG) {
203 1.3.2.2 rmind *ptp++ = (void *)segtabs;
204 1.3.2.2 rmind }
205 1.3.2.2 rmind pmap_kernel()->pm_segtab = stp;
206 1.3.2.2 rmind curcpu()->ci_pmap_kern_segtab = stp;
207 1.3.2.2 rmind printf(" kern_segtab=%p", stp);
208 1.3.2.2 rmind
209 1.3.2.2 rmind #if 0
210 1.3.2.2 rmind nsegtabs = (physmem + NPTEPG - 1) / NPTEPG;
211 1.3.2.2 rmind segtabs = uvm_pageboot_alloc(NBPG * nsegtabs);
212 1.3.2.2 rmind ptp = stp->seg_tab;
213 1.3.2.2 rmind pt_entry_t pt_entry = PTE_M|PTE_xX|PTE_xR;
214 1.3.2.2 rmind pt_entry_t *ptep = (void *)segtabs;
215 1.3.2.2 rmind printf("%s: allocated %lu page table pages for mapping %u pages\n",
216 1.3.2.2 rmind __func__, nsegtabs, physmem);
217 1.3.2.2 rmind for (u_int i = 0; i < nsegtabs; i++, segtabs += NBPG, ptp++) {
218 1.3.2.2 rmind *ptp = ptep;
219 1.3.2.2 rmind for (u_int j = 0; j < NPTEPG; j++, ptep++) {
220 1.3.2.2 rmind *ptep = pt_entry;
221 1.3.2.2 rmind pt_entry += NBPG;
222 1.3.2.2 rmind }
223 1.3.2.2 rmind printf(" [%u]=%p (%#x)", i, *ptp, **ptp);
224 1.3.2.2 rmind pt_entry |= PTE_xW;
225 1.3.2.2 rmind pt_entry &= ~PTE_xX;
226 1.3.2.2 rmind }
227 1.3.2.2 rmind
228 1.3.2.2 rmind /*
229 1.3.2.2 rmind * Now make everything before the kernel inaccessible.
230 1.3.2.2 rmind */
231 1.3.2.2 rmind for (u_int i = 0; i < startkernel / NBPG; i += NBPG) {
232 1.3.2.2 rmind stp->seg_tab[i >> SEGSHIFT][(i & SEGOFSET) >> PAGE_SHIFT] = 0;
233 1.3.2.2 rmind }
234 1.3.2.2 rmind #endif
235 1.3.2.2 rmind
236 1.3.2.2 rmind /*
237 1.3.2.2 rmind * Initialize the pools.
238 1.3.2.2 rmind */
239 1.3.2.2 rmind pool_init(&pmap_pmap_pool, PMAP_SIZE, 0, 0, 0, "pmappl",
240 1.3.2.2 rmind &pool_allocator_nointr, IPL_NONE);
241 1.3.2.2 rmind pool_init(&pmap_pv_pool, sizeof(struct pv_entry), 0, 0, 0, "pvpl",
242 1.3.2.2 rmind &pmap_pv_page_allocator, IPL_NONE);
243 1.3.2.2 rmind
244 1.3.2.2 rmind tlb_set_asid(0);
245 1.3.2.2 rmind }
246 1.3.2.2 rmind
247 1.3.2.2 rmind struct vm_page *
248 1.3.2.2 rmind pmap_md_alloc_poolpage(int flags)
249 1.3.2.2 rmind {
250 1.3.2.2 rmind /*
251 1.3.2.2 rmind * Any managed page works for us.
252 1.3.2.2 rmind */
253 1.3.2.2 rmind return uvm_pagealloc(NULL, 0, NULL, flags);
254 1.3.2.2 rmind }
255 1.3.2.2 rmind
256 1.3.2.2 rmind void
257 1.3.2.2 rmind pmap_zero_page(paddr_t pa)
258 1.3.2.2 rmind {
259 1.3.2.2 rmind // printf("%s(%#lx): calling dcache_zero_page(%#lx)\n", __func__, pa, pa);
260 1.3.2.2 rmind dcache_zero_page(pa);
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_copy_page(paddr_t src, paddr_t dst)
265 1.3.2.2 rmind {
266 1.3.2.2 rmind const size_t line_size = curcpu()->ci_ci.dcache_line_size;
267 1.3.2.2 rmind const paddr_t end = src + PAGE_SIZE;
268 1.3.2.2 rmind
269 1.3.2.2 rmind while (src < end) {
270 1.3.2.2 rmind __asm(
271 1.3.2.2 rmind "dcbt %2,%1" "\n\t" /* touch next src cachline */
272 1.3.2.2 rmind "dcba 0,%1" "\n\t" /* don't fetch dst cacheline */
273 1.3.2.2 rmind :: "b"(src), "b"(dst), "b"(line_size));
274 1.3.2.2 rmind for (u_int i = 0;
275 1.3.2.2 rmind i < line_size;
276 1.3.2.2 rmind src += 32, dst += 32, i += 32) {
277 1.3.2.2 rmind __asm(
278 1.3.2.2 rmind "lmw 24,0(%0)" "\n\t"
279 1.3.2.2 rmind "stmw 24,0(%1)"
280 1.3.2.2 rmind :: "b"(src), "b"(dst)
281 1.3.2.2 rmind : "r24", "r25", "r26", "r27",
282 1.3.2.2 rmind "r28", "r29", "r30", "r31");
283 1.3.2.2 rmind }
284 1.3.2.2 rmind }
285 1.3.2.2 rmind }
286 1.3.2.2 rmind
287 1.3.2.2 rmind void
288 1.3.2.2 rmind pmap_md_init(void)
289 1.3.2.2 rmind {
290 1.3.2.2 rmind
291 1.3.2.2 rmind /* nothing for now */
292 1.3.2.2 rmind }
293 1.3.2.2 rmind
294 1.3.2.2 rmind bool
295 1.3.2.2 rmind pmap_md_io_vaddr_p(vaddr_t va)
296 1.3.2.2 rmind {
297 1.3.2.2 rmind return va >= pmap_limits.avail_end
298 1.3.2.2 rmind && !(VM_MIN_KERNEL_ADDRESS <= va && va < VM_MAX_KERNEL_ADDRESS);
299 1.3.2.2 rmind }
300 1.3.2.2 rmind
301