xref: /dragonfly/stand/boot/pc32/libi386/biossmap.c (revision 655933d6)
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  * $FreeBSD: src/sys/boot/i386/libi386/biossmap.c,v 1.2 2003/08/25 23:28:31 obrien Exp $
27  */
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 <machine/metadata.h>
36 #include <machine/pc/bios.h>
37 #include <machine/psl.h>
38 #include "bootstrap.h"
39 #include "libi386.h"
40 #include "btxv86.h"
41 
42 static struct bios_smap smap;
43 
44 static struct bios_smap *smapbase;
45 static int smaplen;
46 
47 void
48 bios_getsmap(void)
49 {
50 	int n;
51 
52 	n = 0;
53 	smaplen = 0;
54 	/* Count up segments in system memory map */
55 	v86.ebx = 0;
56 	do {
57 		v86.ctl = V86_FLAGS;
58 		v86.addr = 0x15;		/* int 0x15 function 0xe820*/
59 		v86.eax = 0xe820;
60 		v86.ecx = sizeof(struct bios_smap);
61 		v86.edx = SMAP_SIG;
62 		v86.es = VTOPSEG(&smap);
63 		v86.edi = VTOPOFF(&smap);
64 		v86int();
65 		if ((v86.efl & PSL_C) || (v86.eax != SMAP_SIG))
66 			break;
67 		n++;
68 	} while (v86.ebx != 0);
69 	if (n == 0)
70 		return;
71 	n += 10;	/* spare room */
72 	smapbase = malloc(n * sizeof(*smapbase));
73 
74 	/* Save system memory map */
75 	v86.ebx = 0;
76 	do {
77 		v86.ctl = V86_FLAGS;
78 		v86.addr = 0x15;		/* int 0x15 function 0xe820*/
79 		v86.eax = 0xe820;
80 		v86.ecx = sizeof(struct bios_smap);
81 		v86.edx = SMAP_SIG;
82 		v86.es = VTOPSEG(&smap);
83 		v86.edi = VTOPOFF(&smap);
84 		v86int();
85 
86 		/*
87 		 * Our heap is now in high memory and must be removed from
88 		 * the smap so the kernel does not blow away passed-in
89 		 * arguments, smap, kenv, etc.
90 		 *
91 		 * This wastes a little memory.
92 		 */
93 		if (smap.type == SMAP_TYPE_MEMORY &&
94 		    smap.base + smap.length > heapbase &&
95 		    smap.base < memtop) {
96 			if (smap.base <= heapbase) {
97 				if (heapbase - smap.base) {
98 					smapbase[smaplen] = smap;
99 					smapbase[smaplen].length =
100 						heapbase - smap.base;
101 					++smaplen;
102 				}
103 			}
104 			if (smap.base + smap.length >= memtop) {
105 				if (smap.base + smap.length - memtop) {
106 					smapbase[smaplen] = smap;
107 					smapbase[smaplen].base = memtop;
108 					smapbase[smaplen].length =
109 						smap.base + smap.length -
110 						memtop;
111 					++smaplen;
112 				}
113 			}
114 		} else {
115 			smapbase[smaplen] = smap;
116 			++smaplen;
117 		}
118 		if ((v86.efl & PSL_C) || (v86.eax != SMAP_SIG))
119 			break;
120 	} while (v86.ebx != 0 && smaplen < n);
121 }
122 void
123 bios_addsmapdata(struct preloaded_file *kfp)
124 {
125 	int len;
126 
127 	if (smapbase == NULL || smaplen == 0)
128 		return;
129 	len = smaplen * sizeof(*smapbase);
130 	file_addmetadata(kfp, MODINFOMD_SMAP, len, smapbase);
131 	/* Temporary compatibility with older development kernels */
132 	file_addmetadata(kfp, 0x0009, len, smapbase);
133 }
134