199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Obtain memory configuration information from the BIOS
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stand.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/param.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/linker.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/queue.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/stddef.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <machine/metadata.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <machine/pc/bios.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "bootstrap.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "libi386.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "btxv86.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct smap_buf {
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct bios_smap smap;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t xattr; /* Extended attribute from ACPI 3.0 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_ENTRY(smap_buf) bufs;
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define SMAP_BUFSIZE offsetof(struct smap_buf, bufs)
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic struct bios_smap *smapbase;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic uint32_t *smapattr;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic u_int smaplen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soomebios_getsmap(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct smap_buf buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_HEAD(smap_head, smap_buf) head =
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_HEAD_INITIALIZER(head);
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct smap_buf *cur, *next;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int n, x;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_INIT(&head);
199767f8919635c4928607450d9e0abb932109ceToomas Soome n = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome x = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.ebx = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome do {
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.ctl = V86_FLAGS;
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.addr = 0x15;
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.eax = 0xe820; /* int 0x15 function 0xe820 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.ecx = SMAP_BUFSIZE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.edx = SMAP_SIG;
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.es = VTOPSEG(&buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.edi = VTOPOFF(&buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86int();
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (V86_CY(v86.efl) || v86.eax != SMAP_SIG ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.ecx < sizeof(buf.smap) || v86.ecx > SMAP_BUFSIZE)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome next = malloc(sizeof(*next));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (next == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome next->smap = buf.smap;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (v86.ecx == SMAP_BUFSIZE) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome next->xattr = buf.xattr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome x++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_INSERT_TAIL(&head, next, bufs);
199767f8919635c4928607450d9e0abb932109ceToomas Soome n++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } while (v86.ebx != 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome smaplen = n;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (smaplen > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome smapbase = malloc(smaplen * sizeof(*smapbase));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (smapbase != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_FOREACH(cur, &head, bufs)
199767f8919635c4928607450d9e0abb932109ceToomas Soome smapbase[n++] = cur->smap;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (smaplen == x) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome smapattr = malloc(smaplen * sizeof(*smapattr));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (smapattr != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome STAILQ_FOREACH(cur, &head, bufs)
199767f8919635c4928607450d9e0abb932109ceToomas Soome smapattr[n++] = cur->xattr &
199767f8919635c4928607450d9e0abb932109ceToomas Soome SMAP_XATTR_MASK;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else
199767f8919635c4928607450d9e0abb932109ceToomas Soome smapattr = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome cur = STAILQ_FIRST(&head);
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (cur != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome next = STAILQ_NEXT(cur, bufs);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(cur);
199767f8919635c4928607450d9e0abb932109ceToomas Soome cur = next;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soomebios_addsmapdata(struct preloaded_file *kfp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (smapbase == NULL || smaplen == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size = smaplen * sizeof(*smapbase);
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(kfp, MODINFOMD_SMAP, size, smapbase);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (smapattr != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome size = smaplen * sizeof(*smapattr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome file_addmetadata(kfp, MODINFOMD_SMAP_XATTR, size, smapattr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(smap, "smap", "show BIOS SMAP", command_smap);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomecommand_smap(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (smapbase == NULL || smaplen == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (smapattr != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < smaplen; i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("SMAP type=%02x base=%016llx len=%016llx attr=%02x\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome (unsigned int)smapbase[i].type,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (unsigned long long)smapbase[i].base,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (unsigned long long)smapbase[i].length,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (unsigned int)smapattr[i]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < smaplen; i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("SMAP type=%02x base=%016llx len=%016llx\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome (unsigned int)smapbase[i].type,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (unsigned long long)smapbase[i].base,
199767f8919635c4928607450d9e0abb932109ceToomas Soome (unsigned long long)smapbase[i].length);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}