1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
/*
Linux Real Mode Interface - A library of DPMI-like functions for Linux.
Copyright (C) 1998 by Josh Vanderhoof
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL JOSH VANDERHOOF BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include "libx86.h"
#define REAL_MEM_BASE ((void *)0x01000)
#define REAL_MEM_SIZE 0xa0000
#define REAL_MEM_BLOCKS 0x100
struct mem_block {
unsigned int size : 20;
unsigned int free : 1;
};
static struct {
int ready;
int count;
struct mem_block blocks[REAL_MEM_BLOCKS];
} mem_info = { 0 };
static int
read_file(char *name, void *p, size_t n)
{
int fd;
fd = open(name, O_RDONLY);
if (fd == -1) {
perror("open");
return 0;
}
if (read(fd, p, n) != n) {
perror("read");
close(fd);
return 0;
}
close(fd);
return 1;
}
static int
map_file(void *start, size_t length, int prot, int flags, char *name, long offset)
{
void *m;
int fd;
fd = open(name, (flags & MAP_SHARED) ? O_RDWR : O_RDONLY);
if (fd == -1) {
perror("open");
return 0;
}
m = mmap(start, length, prot, flags, fd, offset);
if (m == (void *)-1) {
perror("mmap");
close(fd);
return 0;
}
close(fd);
return 1;
}
static int
real_mem_init(void)
{
if (mem_info.ready)
return 1;
if (!map_file((void *)REAL_MEM_BASE, REAL_MEM_SIZE,
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_FIXED | MAP_PRIVATE, "/dev/zero", 0))
return 0;
mem_info.ready = 1;
mem_info.count = 1;
mem_info.blocks[0].size = REAL_MEM_SIZE;
mem_info.blocks[0].free = 1;
return 1;
}
static void
real_mem_deinit(void)
{
if (mem_info.ready) {
munmap((void *)REAL_MEM_BASE, REAL_MEM_SIZE);
mem_info.ready = 0;
}
}
static void
insert_block(int i)
{
memmove(
mem_info.blocks + i + 1,
mem_info.blocks + i,
(mem_info.count - i) * sizeof(struct mem_block));
mem_info.count++;
}
static void
delete_block(int i)
{
mem_info.count--;
memmove(
mem_info.blocks + i,
mem_info.blocks + i + 1,
(mem_info.count - i) * sizeof(struct mem_block));
}
void *
LRMI_alloc_real(int size)
{
int i;
char *r = (char *)REAL_MEM_BASE;
if (!mem_info.ready)
return NULL;
if (mem_info.count == REAL_MEM_BLOCKS)
return NULL;
size = (size + 15) & ~15;
for (i = 0; i < mem_info.count; i++) {
if (mem_info.blocks[i].free && size < mem_info.blocks[i].size) {
insert_block(i);
mem_info.blocks[i].size = size;
mem_info.blocks[i].free = 0;
mem_info.blocks[i + 1].size -= size;
return (void *)r;
}
r += mem_info.blocks[i].size;
}
return NULL;
}
void
LRMI_free_real(void *m)
{
int i;
char *r = (char *)REAL_MEM_BASE;
if (!mem_info.ready)
return;
i = 0;
while (m != (void *)r) {
r += mem_info.blocks[i].size;
i++;
if (i == mem_info.count)
return;
}
mem_info.blocks[i].free = 1;
if (i + 1 < mem_info.count && mem_info.blocks[i + 1].free) {
mem_info.blocks[i].size += mem_info.blocks[i + 1].size;
delete_block(i + 1);
}
if (i - 1 >= 0 && mem_info.blocks[i - 1].free) {
mem_info.blocks[i - 1].size += mem_info.blocks[i].size;
delete_block(i);
}
}
int LRMI_common_init(void)
{
void *m;
int fd_mem;
if (!real_mem_init())
return 0;
/*
Map the Interrupt Vectors (0x0 - 0x400) + BIOS data (0x400 - 0x502)
and the ROM (0xa0000 - 0x100000)
*/
if (!map_file((void *)0, 0x502,
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_FIXED | MAP_PRIVATE, "/dev/zero", 0)) {
real_mem_deinit();
return 0;
}
if (!read_file("/dev/mem", (void *)0, 0x502)) {
munmap((void *)0, 0x502);
real_mem_deinit();
return 0;
}
if (!map_file((void *)0xa0000, 0x100000 - 0xa0000,
PROT_READ | PROT_WRITE,
MAP_FIXED | MAP_SHARED, "/dev/mem", 0xa0000)) {
munmap((void *)0, 0x502);
real_mem_deinit();
return 0;
}
close(fd_mem);
return 1;
}
void
LRMI_set_exit_condition(xf86Int10InfoPtr pInt)
{
/*
* Here we set the exit condition: We return when we encounter
* 'hlt' (=0xf4), which we locate at address 0x600 in x86 memory.
*/
MEM_WB(pInt, 0x0600, 0xf4);
}
|