]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/boot/i386/libi386/biosmem.c
MFC Loader Fixes 2017q2: r316437,r316577,r316578,r316585,r316590,r316612,
[FreeBSD/FreeBSD.git] / sys / boot / i386 / libi386 / biosmem.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
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 <machine/pc/bios.h>
35 #include "bootstrap.h"
36 #include "libi386.h"
37 #include "btxv86.h"
38 #include "smbios.h"
39
40 vm_offset_t     memtop, memtop_copyin, high_heap_base;
41 uint32_t        bios_basemem, bios_extmem, high_heap_size;
42
43 static struct bios_smap_xattr smap;
44
45 /*
46  * Used to track which method was used to set BIOS memory
47  * regions.
48  */
49 static uint8_t b_bios_probed;
50 #define B_BASEMEM_E820  0x1
51 #define B_BASEMEM_12    0x2
52 #define B_EXTMEM_E820   0x4
53 #define B_EXTMEM_E801   0x8
54 #define B_EXTMEM_8800   0x10
55
56 /*
57  * The minimum amount of memory to reserve in bios_extmem for the heap.
58  */
59 #define HEAP_MIN        (64 * 1024 * 1024)
60
61 /*
62  * Products in this list need quirks to detect
63  * memory correctly. You need both maker and product as
64  * reported by smbios.
65  */
66 #define BQ_DISTRUST_E820_EXTMEM 0x1     /* e820 might not return useful
67                                            extended memory */
68 struct bios_getmem_quirks {
69     const char* bios_vendor;
70     const char* maker;
71     const char* product;
72     int         quirk;
73 };
74
75 static struct bios_getmem_quirks quirks[] = {
76     {"coreboot", "Acer", "Peppy", BQ_DISTRUST_E820_EXTMEM},
77     {NULL, NULL, NULL, 0}
78 };
79
80 static int
81 bios_getquirks(void)
82 {
83     int i;
84
85     for (i=0; quirks[i].quirk != 0; ++i)
86         if (smbios_match(quirks[i].bios_vendor, quirks[i].maker,
87             quirks[i].product))
88             return (quirks[i].quirk);
89
90     return (0);
91 }
92
93 void
94 bios_getmem(void)
95 {
96     uint64_t size;
97
98     /* Parse system memory map */
99     v86.ebx = 0;
100     do {
101         v86.ctl = V86_FLAGS;
102         v86.addr = 0x15;                /* int 0x15 function 0xe820*/
103         v86.eax = 0xe820;
104         v86.ecx = sizeof(struct bios_smap_xattr);
105         v86.edx = SMAP_SIG;
106         v86.es = VTOPSEG(&smap);
107         v86.edi = VTOPOFF(&smap);
108         v86int();
109         if ((V86_CY(v86.efl)) || (v86.eax != SMAP_SIG))
110             break;
111         /* look for a low-memory segment that's large enough */
112         if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0) &&
113             (smap.length >= (512 * 1024))) {
114             bios_basemem = smap.length;
115             b_bios_probed |= B_BASEMEM_E820;
116         }
117
118         /* look for the first segment in 'extended' memory */
119         if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0x100000) &&
120             !(bios_getquirks() & BQ_DISTRUST_E820_EXTMEM)) {
121             bios_extmem = smap.length;
122             b_bios_probed |= B_EXTMEM_E820;
123         }
124
125         /*
126          * Look for the largest segment in 'extended' memory beyond
127          * 1MB but below 4GB.
128          */
129         if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base > 0x100000) &&
130             (smap.base < 0x100000000ull)) {
131             size = smap.length;
132
133             /*
134              * If this segment crosses the 4GB boundary, truncate it.
135              */
136             if (smap.base + size > 0x100000000ull)
137                 size = 0x100000000ull - smap.base;
138
139             if (size > high_heap_size) {
140                 high_heap_size = size;
141                 high_heap_base = smap.base;
142             }
143         }
144     } while (v86.ebx != 0);
145
146     /* Fall back to the old compatibility function for base memory */
147     if (bios_basemem == 0) {
148         v86.ctl = 0;
149         v86.addr = 0x12;                /* int 0x12 */
150         v86int();
151         
152         bios_basemem = (v86.eax & 0xffff) * 1024;
153         b_bios_probed |= B_BASEMEM_12;
154     }
155
156     /* Fall back through several compatibility functions for extended memory */
157     if (bios_extmem == 0) {
158         v86.ctl = V86_FLAGS;
159         v86.addr = 0x15;                /* int 0x15 function 0xe801*/
160         v86.eax = 0xe801;
161         v86int();
162         if (!(V86_CY(v86.efl))) {
163             /*
164              * Clear high_heap; it may end up overlapping
165              * with the segment we're determining here.
166              * Let the default "steal stuff from top of
167              * bios_extmem" code below pick up on it.
168              */
169             high_heap_size = 0;
170             high_heap_base = 0;
171
172             /*
173              * %cx is the number of 1KiB blocks between 1..16MiB.
174              * It can only be up to 0x3c00; if it's smaller then
175              * there's a PC AT memory hole so we can't treat
176              * it as contiguous.
177              */
178             bios_extmem = (v86.ecx & 0xffff) * 1024;
179             if (bios_extmem == (1024 * 0x3c00))
180                 bios_extmem += (v86.edx & 0xffff) * 64 * 1024;
181
182             /* truncate bios_extmem */
183             if (bios_extmem > 0x3ff00000)
184                 bios_extmem = 0x3ff00000;
185
186             b_bios_probed |= B_EXTMEM_E801;
187         }
188     }
189     if (bios_extmem == 0) {
190         v86.ctl = 0;
191         v86.addr = 0x15;                /* int 0x15 function 0x88*/
192         v86.eax = 0x8800;
193         v86int();
194         bios_extmem = (v86.eax & 0xffff) * 1024;
195         b_bios_probed |= B_EXTMEM_8800;
196     }
197
198     /* Set memtop to actual top of memory */
199     memtop = memtop_copyin = 0x100000 + bios_extmem;
200
201     /*
202      * If we have extended memory and did not find a suitable heap
203      * region in the SMAP, use the last HEAP_MIN of 'extended' memory as a
204      * high heap candidate.
205      */
206     if (bios_extmem >= HEAP_MIN && high_heap_size < HEAP_MIN) {
207         high_heap_size = HEAP_MIN;
208         high_heap_base = memtop - HEAP_MIN;
209     }
210 }
211
212 static int
213 command_biosmem(int argc, char *argv[])
214 {
215         int bq = bios_getquirks();
216
217         printf("bios_basemem: 0x%llx\n", (unsigned long long) bios_basemem);
218         printf("bios_extmem: 0x%llx\n", (unsigned long long) bios_extmem);
219         printf("memtop: 0x%llx\n", (unsigned long long) memtop);
220         printf("high_heap_base: 0x%llx\n", (unsigned long long) high_heap_base);
221         printf("high_heap_size: 0x%llx\n", (unsigned long long) high_heap_size);
222         printf("bios_quirks: 0x%02x", bq);
223         if (bq & BQ_DISTRUST_E820_EXTMEM)
224                 printf(" BQ_DISTRUST_E820_EXTMEM");
225         printf("\n");
226         printf("b_bios_probed: 0x%02x", (int) b_bios_probed);
227         if (b_bios_probed & B_BASEMEM_E820)
228                 printf(" B_BASEMEM_E820");
229         if (b_bios_probed & B_BASEMEM_12)
230                 printf(" B_BASEMEM_12");
231         if (b_bios_probed & B_EXTMEM_E820)
232                 printf(" B_EXTMEM_E820");
233         if (b_bios_probed & B_EXTMEM_E801)
234                 printf(" B_EXTMEM_E801");
235         if (b_bios_probed & B_EXTMEM_8800)
236                 printf(" B_EXTMEM_8800");
237         printf("\n");
238
239         return (CMD_OK);
240 }
241
242 COMMAND_SET(biosmem, "biosmem", "show BIOS memory setup", command_biosmem);