2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * Obtain memory configuration information from the BIOS
34 #include <sys/param.h>
35 #include <sys/linker.h>
36 #include <sys/queue.h>
37 #include <sys/stddef.h>
38 #include <machine/metadata.h>
39 #include <machine/psl.h>
40 #include <machine/pc/bios.h>
41 #include "bootstrap.h"
45 #define V86_CY(x) ((x) & PSL_C)
48 struct bios_smap smap;
49 uint32_t xattr; /* Extended attribute from ACPI 3.0 */
50 STAILQ_ENTRY(smap_buf) bufs;
53 #define SMAP_BUFSIZE offsetof(struct smap_buf, bufs)
55 static struct bios_smap *smapbase;
56 static uint32_t *smapattr;
63 STAILQ_HEAD(smap_head, smap_buf) head =
64 STAILQ_HEAD_INITIALIZER(head);
65 struct smap_buf *cur, *next;
75 v86.eax = 0xe820; /* int 0x15 function 0xe820 */
76 v86.ecx = SMAP_BUFSIZE;
78 v86.es = VTOPSEG(&buf);
79 v86.edi = VTOPOFF(&buf);
81 if (V86_CY(v86.efl) || v86.eax != SMAP_SIG ||
82 v86.ecx < sizeof(buf.smap) || v86.ecx > SMAP_BUFSIZE)
85 next = malloc(sizeof(*next));
88 next->smap = buf.smap;
89 if (v86.ecx == SMAP_BUFSIZE) {
90 next->xattr = buf.xattr;
93 STAILQ_INSERT_TAIL(&head, next, bufs);
95 } while (v86.ebx != 0);
99 smapbase = malloc(smaplen * sizeof(*smapbase));
100 if (smapbase != NULL) {
102 STAILQ_FOREACH(cur, &head, bufs)
103 smapbase[n++] = cur->smap;
106 smapattr = malloc(smaplen * sizeof(*smapattr));
107 if (smapattr != NULL) {
109 STAILQ_FOREACH(cur, &head, bufs)
110 smapattr[n++] = cur->xattr &
115 cur = STAILQ_FIRST(&head);
116 while (cur != NULL) {
117 next = STAILQ_NEXT(cur, bufs);
125 bios_addsmapdata(struct preloaded_file *kfp)
129 if (smapbase == NULL || smaplen == 0)
131 size = smaplen * sizeof(*smapbase);
132 file_addmetadata(kfp, MODINFOMD_SMAP, size, smapbase);
133 if (smapattr != NULL) {
134 size = smaplen * sizeof(*smapattr);
135 file_addmetadata(kfp, MODINFOMD_SMAP_XATTR, size, smapattr);
139 COMMAND_SET(smap, "smap", "show BIOS SMAP", command_smap);
142 command_smap(int argc, char *argv[])
146 if (smapbase == NULL || smaplen == 0)
148 if (smapattr != NULL)
149 for (i = 0; i < smaplen; i++)
150 printf("SMAP type=%02x base=%016llx len=%016llx attr=%02x\n",
151 (unsigned int)smapbase[i].type,
152 (unsigned long long)smapbase[i].base,
153 (unsigned long long)smapbase[i].length,
154 (unsigned int)smapattr[i]);
156 for (i = 0; i < smaplen; i++)
157 printf("SMAP type=%02x base=%016llx len=%016llx\n",
158 (unsigned int)smapbase[i].type,
159 (unsigned long long)smapbase[i].base,
160 (unsigned long long)smapbase[i].length);