biosmemx.S revision 1.12 1 /* $NetBSD: biosmemx.S,v 1.12 2024/08/17 08:38:31 andvar Exp $ */
2
3 /*
4 * Copyright (c) 1997, 1999
5 * Matthias Drochner. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
28
29 #include <machine/asm.h>
30
31 .text
32
33 /*
34 * int getextmem2(int buffer[2])
35 *
36 * return: 0=OK, -1=error
37 * buffer[0]: extmem kBytes below 16M (max 15M/1024)
38 * buffer[1]: extmem above 16M, in 64k units
39 */
40 ENTRY(getextmem2)
41 pushl %ebp
42 movl %esp,%ebp
43 pushl %ebx
44 pushl %ecx
45 pushl %edx
46 push %esi
47 push %edi
48
49 call _C_LABEL(prot_to_real)
50 .code16
51
52 xorl %ebx,%ebx
53 movl $0xe801,%eax
54 int $0x15
55 pushf
56
57 movw %si,%ax
58 orw %si,%bx
59 jz 1f /* if zero use configured values */
60 movw %cx,%ax /* k below 16M (max 0x3c00 = 15MB) */
61 movw %dx,%bx /* 64k above 16M */
62 1:
63 popf
64 setc %bl
65
66 calll _C_LABEL(real_to_prot)
67 .code32
68
69 movl 8(%ebp),%edi
70 xorl %eax,%eax
71 movw %cx,%ax
72 stosl
73 movw %dx,%ax
74 stosl
75 movb %bl,%al
76 cbw
77
78 pop %edi
79 pop %esi
80 popl %edx
81 popl %ecx
82 popl %ebx
83 popl %ebp
84 ret
85
86 /*
87 * int getmementry(int *iterator, int buffer[6])
88 *
89 * return: 0=ok, else error
90 * buffer[0]: start of memory chunk
91 * buffer[2]: length (bytes)
92 * buffer[4]: type
93 * buffer[5]: ACPI 3.0 Extended Attributes bitfield (unused)
94 * Some buggy BIOSes may write to 24 bytes even if only 20 were requested.
95 * Therefore, the buffer is defined for 6 elements to avoid stack buffer
96 * overruns. See PR install/49470.
97 *
98 * More details can be found in the
99 * Advanced Configuration and Power Interface (ACPI) Specification, Release 6.5,
100 * 2022-08-29, UEFI Forum, Inc.,
101 * Sec. 15.1 `INT 15H E820H - Query System Address Map', pp. 756-757
102 * https://uefi.org/sites/default/files/resources/ACPI_Spec_6_5_Aug29.pdf#page=824
103 * https://uefi.org/specs/ACPI/6.5/15_System_Address_Map_Interfaces.html#int-15h-e820h-query-system-address-map
104 * as well as OSDev.org wiki page
105 * https://wiki.osdev.org/Detecting_Memory_(x86)#BIOS_Function:_INT_0x15,_EAX_=_0xE820.
106 */
107 ENTRY(getmementry)
108 pushl %ebp
109 movl %esp,%ebp
110 pushl %ebx
111 pushl %ecx
112 pushl %edx
113 push %esi
114 push %edi
115
116 movl 8(%ebp),%eax
117 movl 0(%eax),%ebx /* index */
118 movl $20,%ecx /* Buffer size */
119 movl $0x534d4150,%edx /* "SMAP" */
120 movl 12(%ebp),%edi /* buffer address */
121
122 call _C_LABEL(prot_to_real)
123 .code16
124
125 push %di
126 shrl $4,%edi
127 mov %ds,%ax
128 add %di,%ax
129 mov %ax,%es
130 pop %di
131 and $0xf,%di /* buffer address now in ES:DI */
132
133 movl $0xe820,%eax /* Some BIOS check EAX value */
134 int $0x15
135
136 setc %cl
137
138 calll _C_LABEL(real_to_prot)
139 .code32
140
141 movl 8(%ebp),%eax
142 movl %ebx,0(%eax) /* updated index */
143 xorl %eax,%eax
144 movb %cl,%al
145
146 pop %edi
147 pop %esi
148 popl %edx
149 popl %ecx
150 popl %ebx
151 popl %ebp
152 ret
153
154 /*
155 * int biosA20(void)
156 *
157 * return: 0=ok, else error
158 */
159 ENTRY(biosA20)
160 pushl %ebp
161 movl %esp,%ebp
162 pushl %ebx
163 pushl %ecx
164 pushl %edx
165 push %esi
166 push %edi
167
168 call _C_LABEL(prot_to_real)
169 .code16
170
171 movl $0x2401,%eax
172 int $0x15
173 setc %cl
174
175 calll _C_LABEL(real_to_prot)
176 .code32
177
178 movzbl %cl,%eax
179
180 pop %edi
181 pop %esi
182 popl %edx
183 popl %ecx
184 popl %ebx
185 popl %ebp
186 ret
187