xref: /illumos-gate/usr/src/boot/i386/libi386/biossmap.c (revision ddb365bfc9e868ad24ccdcb0dc91af18b10df082)
1 /*
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 
29 /*
30  * Obtain memory configuration information from the BIOS
31  */
32 #include <stand.h>
33 #include <sys/param.h>
34 #include <sys/linker.h>
35 #include <sys/queue.h>
36 #include <sys/stddef.h>
37 #include <machine/metadata.h>
38 #include <machine/pc/bios.h>
39 #include "bootstrap.h"
40 #include "libi386.h"
41 #include "btxv86.h"
42 
43 struct smap_buf {
44 	struct bios_smap	smap;
45 	uint32_t		xattr;	/* Extended attribute from ACPI 3.0 */
46 	STAILQ_ENTRY(smap_buf)	bufs;
47 };
48 
49 #define	SMAP_BUFSIZE		offsetof(struct smap_buf, bufs)
50 
51 static struct bios_smap		*smapbase;
52 static uint32_t			*smapattr;
53 static u_int			smaplen;
54 
55 void
56 bios_getsmap(void)
57 {
58 	struct smap_buf		buf;
59 	STAILQ_HEAD(smap_head, smap_buf) head =
60 	    STAILQ_HEAD_INITIALIZER(head);
61 	struct smap_buf		*cur, *next;
62 	u_int			n, x;
63 
64 	STAILQ_INIT(&head);
65 	n = 0;
66 	x = 0;
67 	v86.ebx = 0;
68 	do {
69 		v86.ctl = V86_FLAGS;
70 		v86.addr = 0x15;
71 		v86.eax = 0xe820;	/* int 0x15 function 0xe820 */
72 		v86.ecx = SMAP_BUFSIZE;
73 		v86.edx = SMAP_SIG;
74 		v86.es = VTOPSEG(&buf);
75 		v86.edi = VTOPOFF(&buf);
76 		v86int();
77 		if (V86_CY(v86.efl) || v86.eax != SMAP_SIG ||
78 		    v86.ecx < sizeof(buf.smap) || v86.ecx > SMAP_BUFSIZE)
79 			break;
80 
81 		next = malloc(sizeof(*next));
82 		if (next == NULL)
83 			break;
84 		next->smap = buf.smap;
85 		if (v86.ecx == SMAP_BUFSIZE) {
86 			next->xattr = buf.xattr;
87 			x++;
88 		}
89 		STAILQ_INSERT_TAIL(&head, next, bufs);
90 		n++;
91 	} while (v86.ebx != 0);
92 	smaplen = n;
93 
94 	if (smaplen > 0) {
95 		smapbase = malloc(smaplen * sizeof(*smapbase));
96 		if (smapbase != NULL) {
97 			n = 0;
98 			STAILQ_FOREACH(cur, &head, bufs)
99 				smapbase[n++] = cur->smap;
100 		}
101 		if (smaplen == x) {
102 			smapattr = malloc(smaplen * sizeof(*smapattr));
103 			if (smapattr != NULL) {
104 				n = 0;
105 				STAILQ_FOREACH(cur, &head, bufs)
106 					smapattr[n++] = cur->xattr &
107 					    SMAP_XATTR_MASK;
108 			}
109 		} else
110 			smapattr = NULL;
111 		cur = STAILQ_FIRST(&head);
112 		while (cur != NULL) {
113 			next = STAILQ_NEXT(cur, bufs);
114 			free(cur);
115 			cur = next;
116 		}
117 	}
118 }
119 
120 void
121 bios_addsmapdata(struct preloaded_file *kfp)
122 {
123 	size_t			size;
124 
125 	if (smapbase == NULL || smaplen == 0)
126 		return;
127 	size = smaplen * sizeof(*smapbase);
128 	file_addmetadata(kfp, MODINFOMD_SMAP, size, smapbase);
129 	if (smapattr != NULL) {
130 		size = smaplen * sizeof(*smapattr);
131 		file_addmetadata(kfp, MODINFOMD_SMAP_XATTR, size, smapattr);
132 	}
133 }
134 
135 COMMAND_SET(smap, "smap", "show BIOS SMAP", command_smap);
136 
137 static int
138 command_smap(int argc __unused, char *argv[] __unused)
139 {
140 	u_int			i;
141 
142 	if (smapbase == NULL || smaplen == 0)
143 		return (CMD_ERROR);
144 	if (smapattr != NULL)
145 		for (i = 0; i < smaplen; i++)
146 			printf("SMAP type=%02x base=%016llx len=%016llx attr=%02x\n",
147 			    (unsigned int)smapbase[i].type,
148 			    (unsigned long long)smapbase[i].base,
149 			    (unsigned long long)smapbase[i].length,
150 			    (unsigned int)smapattr[i]);
151 	else
152 		for (i = 0; i < smaplen; i++)
153 			printf("SMAP type=%02x base=%016llx len=%016llx\n",
154 			    (unsigned int)smapbase[i].type,
155 			    (unsigned long long)smapbase[i].base,
156 			    (unsigned long long)smapbase[i].length);
157 	return (CMD_OK);
158 }
159