]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/physmem.c
followup to r332730/r332752: set kdb_why to "trap" for fatal traps
[FreeBSD/FreeBSD.git] / sys / arm / arm / physmem.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2014 Ian Lepore <ian@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include "opt_ddb.h"
33
34 /*
35  * Routines for describing and initializing anything related to physical memory.
36  */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <vm/vm.h>
41 #include <machine/md_var.h>
42 #include <machine/physmem.h>
43
44 /*
45  * These structures are used internally to keep track of regions of physical
46  * ram, and regions within the physical ram that need to be excluded.  An
47  * exclusion region can be excluded from crash dumps, from the vm pool of pages
48  * that can be allocated, or both, depending on the exclusion flags associated
49  * with the region.
50  */
51 #define MAX_HWCNT       10
52 #define MAX_EXCNT       10
53
54 #define MAX_PHYS_ADDR   0xFFFFFFFFull
55
56 struct region {
57         vm_paddr_t      addr;
58         vm_size_t       size;
59         uint32_t        flags;
60 };
61
62 static struct region hwregions[MAX_HWCNT];
63 static struct region exregions[MAX_EXCNT];
64
65 static size_t hwcnt;
66 static size_t excnt;
67
68 /*
69  * These "avail lists" are globals used to communicate physical memory layout to
70  * other parts of the kernel.  Within the arrays, each value is the starting
71  * address of a contiguous area of physical address space.  The values at even
72  * indexes are areas that contain usable memory and the values at odd indexes
73  * are areas that aren't usable.  Each list is terminated by a pair of zero
74  * entries.
75  *
76  * dump_avail tells the dump code what regions to include in a crash dump, and
77  * phys_avail is the way we hand all the remaining physical ram we haven't used
78  * in early kernel init over to the vm system for allocation management.
79  *
80  * We size these arrays to hold twice as many available regions as we allow for
81  * hardware memory regions, to allow for the fact that exclusions can split a
82  * hardware region into two or more available regions.  In the real world there
83  * will typically be one or two hardware regions and two or three exclusions.
84  *
85  * Each available region in this list occupies two array slots (the start of the
86  * available region and the start of the unavailable region that follows it).
87  */
88 #define MAX_AVAIL_REGIONS       (MAX_HWCNT * 2)
89 #define MAX_AVAIL_ENTRIES       (MAX_AVAIL_REGIONS * 2)
90
91 vm_paddr_t phys_avail[MAX_AVAIL_ENTRIES + 2]; /* +2 to allow for a pair  */
92 vm_paddr_t dump_avail[MAX_AVAIL_ENTRIES + 2]; /* of zeroes to terminate. */
93
94 /*
95  * realmem is the total number of hardware pages, excluded or not.
96  * Maxmem is one greater than the last physical page number.
97  */
98 long realmem;
99 long Maxmem;
100
101 /* The address at which the kernel was loaded.  Set early in initarm(). */
102 vm_paddr_t arm_physmem_kernaddr;
103
104 /*
105  * Print the contents of the physical and excluded region tables using the
106  * provided printf-like output function (which will be either printf or
107  * db_printf).
108  */
109 static void
110 physmem_dump_tables(int (*prfunc)(const char *, ...))
111 {
112         int flags, i;
113         uintmax_t addr, size;
114         const unsigned int mbyte = 1024 * 1024;
115
116         prfunc("Physical memory chunk(s):\n");
117         for (i = 0; i < hwcnt; ++i) {
118                 addr = hwregions[i].addr;
119                 size = hwregions[i].size;
120                 prfunc("  0x%08jx - 0x%08jx, %5ju MB (%7ju pages)\n", addr,
121                     addr + size - 1, size / mbyte, size / PAGE_SIZE);
122         }
123
124         prfunc("Excluded memory regions:\n");
125         for (i = 0; i < excnt; ++i) {
126                 addr  = exregions[i].addr;
127                 size  = exregions[i].size;
128                 flags = exregions[i].flags;
129                 prfunc("  0x%08jx - 0x%08jx, %5ju MB (%7ju pages) %s %s\n",
130                     addr, addr + size - 1, size / mbyte, size / PAGE_SIZE,
131                     (flags & EXFLAG_NOALLOC) ? "NoAlloc" : "",
132                     (flags & EXFLAG_NODUMP)  ? "NoDump" : "");
133         }
134
135 #ifdef DEBUG
136         prfunc("Avail lists:\n");
137         for (i = 0; phys_avail[i] != 0; ++i) {
138                 prfunc("  phys_avail[%d] 0x%08x\n", i, phys_avail[i]);
139         }
140         for (i = 0; dump_avail[i] != 0; ++i) {
141                 prfunc("  dump_avail[%d] 0x%08x\n", i, dump_avail[i]);
142         }
143 #endif
144 }
145
146 /*
147  * Print the contents of the static mapping table.  Used for bootverbose.
148  */
149 void
150 arm_physmem_print_tables(void)
151 {
152
153         physmem_dump_tables(printf);
154 }
155
156 /*
157  * Walk the list of hardware regions, processing it against the list of
158  * exclusions that contain the given exflags, and generating an "avail list".
159  *
160  * Updates the value at *pavail with the sum of all pages in all hw regions.
161  *
162  * Returns the number of pages of non-excluded memory added to the avail list.
163  */
164 static size_t
165 regions_to_avail(vm_paddr_t *avail, uint32_t exflags, long *pavail)
166 {
167         size_t acnt, exi, hwi;
168         uint64_t end, start, xend, xstart;
169         long availmem;
170         const struct region *exp, *hwp;
171
172         realmem = 0;
173         availmem = 0;
174         acnt = 0;
175         for (hwi = 0, hwp = hwregions; hwi < hwcnt; ++hwi, ++hwp) {
176                 start = hwp->addr;
177                 end   = hwp->size + start;
178                 realmem += arm32_btop((vm_offset_t)(end - start));
179                 for (exi = 0, exp = exregions; exi < excnt; ++exi, ++exp) {
180                         /*
181                          * If the excluded region does not match given flags,
182                          * continue checking with the next excluded region.
183                          */
184                         if ((exp->flags & exflags) == 0)
185                                 continue;
186                         xstart = exp->addr;
187                         xend   = exp->size + xstart;
188                         /*
189                          * If the excluded region ends before this hw region,
190                          * continue checking with the next excluded region.
191                          */
192                         if (xend <= start)
193                                 continue;
194                         /*
195                          * If the excluded region begins after this hw region
196                          * we're done because both lists are sorted.
197                          */
198                         if (xstart >= end)
199                                 break;
200                         /*
201                          * If the excluded region completely covers this hw
202                          * region, shrink this hw region to zero size.
203                          */
204                         if ((start >= xstart) && (end <= xend)) {
205                                 start = xend;
206                                 end = xend;
207                                 break;
208                         }
209                         /*
210                          * If the excluded region falls wholly within this hw
211                          * region without abutting or overlapping the beginning
212                          * or end, create an available entry from the leading
213                          * fragment, then adjust the start of this hw region to
214                          * the end of the excluded region, and continue checking
215                          * the next excluded region because another exclusion
216                          * could affect the remainder of this hw region.
217                          */
218                         if ((xstart > start) && (xend < end)) {
219                                 avail[acnt++] = (vm_paddr_t)start;
220                                 avail[acnt++] = (vm_paddr_t)xstart;
221                                 availmem += 
222                                     arm32_btop((vm_offset_t)(xstart - start));
223                                 start = xend;
224                                 continue;
225                         }
226                         /*
227                          * We know the excluded region overlaps either the start
228                          * or end of this hardware region (but not both), trim
229                          * the excluded portion off the appropriate end.
230                          */
231                         if (xstart <= start)
232                                 start = xend;
233                         else
234                                 end = xstart;
235                 }
236                 /*
237                  * If the trimming actions above left a non-zero size, create an
238                  * available entry for it.
239                  */
240                 if (end > start) {
241                         avail[acnt++] = (vm_paddr_t)start;
242                         avail[acnt++] = (vm_paddr_t)end;
243                         availmem += arm32_btop((vm_offset_t)(end - start));
244                 }
245                 if (acnt >= MAX_AVAIL_ENTRIES)
246                         panic("Not enough space in the dump/phys_avail arrays");
247         }
248
249         if (pavail)
250                 *pavail = availmem;
251         return (acnt);
252 }
253
254 /*
255  * Insertion-sort a new entry into a regions list; sorted by start address.
256  */
257 static void
258 insert_region(struct region *regions, size_t rcnt, vm_paddr_t addr,
259     vm_size_t size, uint32_t flags)
260 {
261         size_t i;
262         struct region *ep, *rp;
263
264         ep = regions + rcnt;
265         for (i = 0, rp = regions; i < rcnt; ++i, ++rp) {
266                 if (addr < rp->addr) {
267                         bcopy(rp, rp + 1, (ep - rp) * sizeof(*rp));
268                         break;
269                 }
270         }
271         rp->addr  = addr;
272         rp->size  = size;
273         rp->flags = flags;
274 }
275
276 /*
277  * Add a hardware memory region.
278  */
279 void
280 arm_physmem_hardware_region(uint64_t pa, uint64_t sz)
281 {
282         vm_offset_t adj;
283
284         /*
285          * Filter out the page at PA 0x00000000.  The VM can't handle it, as
286          * pmap_extract() == 0 means failure.
287          */
288         if (pa == 0) {
289                 if (sz <= PAGE_SIZE)
290                         return;
291                 pa  = PAGE_SIZE;
292                 sz -= PAGE_SIZE;
293         } else if (pa > MAX_PHYS_ADDR) {
294                 /* This range is past usable memory, ignore it */
295                 return;
296         }
297
298         /*
299          * Also filter out the page at the end of the physical address space --
300          * if addr is non-zero and addr+size is zero we wrapped to the next byte
301          * beyond what vm_paddr_t can express.  That leads to a NULL pointer
302          * deref early in startup; work around it by leaving the last page out.
303          *
304          * XXX This just in:  subtract out a whole megabyte, not just 1 page.
305          * Reducing the size by anything less than 1MB results in the NULL
306          * pointer deref in _vm_map_lock_read().  Better to give up a megabyte
307          * than leave some folks with an unusable system while we investigate.
308          */
309         if ((pa + sz) > (MAX_PHYS_ADDR - 1024 * 1024)) {
310                 sz = MAX_PHYS_ADDR - pa + 1;
311                 if (sz <= 1024 * 1024)
312                         return;
313                 sz -= 1024 * 1024;
314         }
315
316         /*
317          * Round the starting address up to a page boundary, and truncate the
318          * ending page down to a page boundary.
319          */
320         adj = round_page(pa) - pa;
321         pa  = round_page(pa);
322         sz  = trunc_page(sz - adj);
323
324         if (sz > 0 && hwcnt < nitems(hwregions))
325                 insert_region(hwregions, hwcnt++, pa, sz, 0);
326 }
327
328 /*
329  * Add an exclusion region.
330  */
331 void arm_physmem_exclude_region(vm_paddr_t pa, vm_size_t sz, uint32_t exflags)
332 {
333         vm_offset_t adj;
334
335         /*
336          * Truncate the starting address down to a page boundary, and round the
337          * ending page up to a page boundary.
338          */
339         adj = pa - trunc_page(pa);
340         pa  = trunc_page(pa);
341         sz  = round_page(sz + adj);
342
343         if (excnt < nitems(exregions))
344                 insert_region(exregions, excnt++, pa, sz, exflags);
345 }
346
347 /*
348  * Process all the regions added earlier into the global avail lists.
349  *
350  * Updates the kernel global 'physmem' with the number of physical pages
351  * available for use (all pages not in any exclusion region).
352  *
353  * Updates the kernel global 'Maxmem' with the page number one greater then the
354  * last page of physical memory in the system.
355  */
356 void
357 arm_physmem_init_kernel_globals(void)
358 {
359         size_t nextidx;
360
361         regions_to_avail(dump_avail, EXFLAG_NODUMP, NULL);
362         nextidx = regions_to_avail(phys_avail, EXFLAG_NOALLOC, &physmem);
363         if (nextidx == 0)
364                 panic("No memory entries in phys_avail");
365         Maxmem = atop(phys_avail[nextidx - 1]);
366 }
367
368 #ifdef DDB
369 #include <ddb/ddb.h>
370
371 DB_SHOW_COMMAND(physmem, db_show_physmem)
372 {
373
374         physmem_dump_tables(db_printf);
375 }
376
377 #endif /* DDB */
378