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__FBSDID("$FreeBSD$");
29
30/*
31 * Obtain memory configuration information from the BIOS
32 */
33#include <stand.h>
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/pc/bios.h>
40#include "bootstrap.h"
41#include "libi386.h"
42#include "btxv86.h"
43
44struct smap_buf {
45	struct bios_smap	smap;
46	uint32_t		xattr;	/* Extended attribute from ACPI 3.0 */
47	STAILQ_ENTRY(smap_buf)	bufs;
48};
49
50#define	SMAP_BUFSIZE		offsetof(struct smap_buf, bufs)
51
52static struct bios_smap		*smapbase;
53static uint32_t			*smapattr;
54static u_int			smaplen;
55
56void
57bios_getsmap(void)
58{
59	struct smap_buf		buf;
60	STAILQ_HEAD(smap_head, smap_buf) head =
61	    STAILQ_HEAD_INITIALIZER(head);
62	struct smap_buf		*cur, *next;
63	u_int			n, x;
64
65	STAILQ_INIT(&head);
66	n = 0;
67	x = 0;
68	v86.ebx = 0;
69	do {
70		v86.ctl = V86_FLAGS;
71		v86.addr = 0x15;
72		v86.eax = 0xe820;	/* int 0x15 function 0xe820 */
73		v86.ecx = SMAP_BUFSIZE;
74		v86.edx = SMAP_SIG;
75		v86.es = VTOPSEG(&buf);
76		v86.edi = VTOPOFF(&buf);
77		v86int();
78		if (V86_CY(v86.efl) || v86.eax != SMAP_SIG ||
79		    v86.ecx < sizeof(buf.smap) || v86.ecx > SMAP_BUFSIZE)
80			break;
81
82		next = malloc(sizeof(*next));
83		if (next == NULL)
84			break;
85		next->smap = buf.smap;
86		if (v86.ecx == SMAP_BUFSIZE) {
87			next->xattr = buf.xattr;
88			x++;
89		}
90		STAILQ_INSERT_TAIL(&head, next, bufs);
91		n++;
92	} while (v86.ebx != 0);
93	smaplen = n;
94
95	if (smaplen > 0) {
96		smapbase = malloc(smaplen * sizeof(*smapbase));
97		if (smapbase != NULL) {
98			n = 0;
99			STAILQ_FOREACH(cur, &head, bufs)
100				smapbase[n++] = cur->smap;
101		}
102		if (smaplen == x) {
103			smapattr = malloc(smaplen * sizeof(*smapattr));
104			if (smapattr != NULL) {
105				n = 0;
106				STAILQ_FOREACH(cur, &head, bufs)
107					smapattr[n++] = cur->xattr &
108					    SMAP_XATTR_MASK;
109			}
110		} else
111			smapattr = NULL;
112		cur = STAILQ_FIRST(&head);
113		while (cur != NULL) {
114			next = STAILQ_NEXT(cur, bufs);
115			free(cur);
116			cur = next;
117		}
118	}
119}
120
121void
122bios_addsmapdata(struct preloaded_file *kfp)
123{
124	size_t			size;
125
126	if (smapbase == NULL || smaplen == 0)
127		return;
128	size = smaplen * sizeof(*smapbase);
129	file_addmetadata(kfp, MODINFOMD_SMAP, size, smapbase);
130	if (smapattr != NULL) {
131		size = smaplen * sizeof(*smapattr);
132		file_addmetadata(kfp, MODINFOMD_SMAP_XATTR, size, smapattr);
133	}
134}
135
136COMMAND_SET(smap, "smap", "show BIOS SMAP", command_smap);
137
138static int
139command_smap(int argc, char *argv[])
140{
141	u_int			i;
142
143	if (smapbase == NULL || smaplen == 0)
144		return (CMD_ERROR);
145	if (smapattr != NULL)
146		for (i = 0; i < smaplen; i++)
147			printf("SMAP type=%02x base=%016llx len=%016llx attr=%02x\n",
148			    (unsigned int)smapbase[i].type,
149			    (unsigned long long)smapbase[i].base,
150			    (unsigned long long)smapbase[i].length,
151			    (unsigned int)smapattr[i]);
152	else
153		for (i = 0; i < smaplen; i++)
154			printf("SMAP type=%02x base=%016llx len=%016llx\n",
155			    (unsigned int)smapbase[i].type,
156			    (unsigned long long)smapbase[i].base,
157			    (unsigned long long)smapbase[i].length);
158	return (CMD_OK);
159}
160