boot/libi386: Use SMAP_TYPE_MEMORY constant.
[dragonfly.git] / sys / boot / pc32 / libi386 / biossmap.c
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 #define SMAPSIG 0x534D4150
43
44 struct smap {
45         u_int64_t       base;
46         u_int64_t       length;
47         u_int32_t       type;
48 } __packed;
49
50 static struct smap smap;
51
52 static struct smap *smapbase;
53 static int smaplen;
54
55 void
56 bios_getsmap(void)
57 {
58         int n;
59
60         n = 0;
61         smaplen = 0;
62         /* Count up segments in system memory map */
63         v86.ebx = 0;
64         do {
65                 v86.ctl = V86_FLAGS;
66                 v86.addr = 0x15;                /* int 0x15 function 0xe820*/
67                 v86.eax = 0xe820;
68                 v86.ecx = sizeof(struct smap);
69                 v86.edx = SMAPSIG;
70                 v86.es = VTOPSEG(&smap);
71                 v86.edi = VTOPOFF(&smap);
72                 v86int();
73                 if ((v86.efl & PSL_C) || (v86.eax != SMAPSIG))
74                         break;
75                 n++;
76         } while (v86.ebx != 0);
77         if (n == 0)
78                 return;
79         n += 10;        /* spare room */
80         smapbase = malloc(n * sizeof(*smapbase));
81
82         /* Save system memory map */
83         v86.ebx = 0;
84         do {
85                 v86.ctl = V86_FLAGS;
86                 v86.addr = 0x15;                /* int 0x15 function 0xe820*/
87                 v86.eax = 0xe820;
88                 v86.ecx = sizeof(struct smap);
89                 v86.edx = SMAPSIG;
90                 v86.es = VTOPSEG(&smap);
91                 v86.edi = VTOPOFF(&smap);
92                 v86int();
93
94                 /*
95                  * Our heap is now in high memory and must be removed from
96                  * the smap so the kernel does not blow away passed-in
97                  * arguments, smap, kenv, etc.
98                  *
99                  * This wastes a little memory.
100                  */
101                 if (smap.type == SMAP_TYPE_MEMORY &&
102                     smap.base + smap.length > heapbase &&
103                     smap.base < memtop) {
104                         if (smap.base <= heapbase) {
105                                 if (heapbase - smap.base) {
106                                         smapbase[smaplen] = smap;
107                                         smapbase[smaplen].length =
108                                                 heapbase - smap.base;
109                                         ++smaplen;
110                                 }
111                         }
112                         if (smap.base + smap.length >= memtop) {
113                                 if (smap.base + smap.length - memtop) {
114                                         smapbase[smaplen] = smap;
115                                         smapbase[smaplen].base = memtop;
116                                         smapbase[smaplen].length =
117                                                 smap.base + smap.length -
118                                                 memtop;
119                                         ++smaplen;
120                                 }
121                         }
122                 } else {
123                         smapbase[smaplen] = smap;
124                         ++smaplen;
125                 }
126                 if ((v86.efl & PSL_C) || (v86.eax != SMAPSIG))
127                         break;
128         } while (v86.ebx != 0 && smaplen < n);
129 }
130 void
131 bios_addsmapdata(struct preloaded_file *kfp)
132 {
133         int len;
134
135         if (smapbase == NULL || smaplen == 0)
136                 return;
137         len = smaplen * sizeof(*smapbase);
138         file_addmetadata(kfp, MODINFOMD_SMAP, len, smapbase);
139         /* Temporary compatibility with older development kernels */
140         file_addmetadata(kfp, 0x0009, len, smapbase);
141 }