vmem.c revision 1.2 1 1.2 uch /* $NetBSD: vmem.c,v 1.2 1999/09/22 12:49:50 uch Exp $ */
2 1.1 takemura
3 1.1 takemura /*-
4 1.1 takemura * Copyright (c) 1999 Shin Takemura.
5 1.1 takemura * All rights reserved.
6 1.1 takemura *
7 1.1 takemura * This software is part of the PocketBSD.
8 1.1 takemura *
9 1.1 takemura * Redistribution and use in source and binary forms, with or without
10 1.1 takemura * modification, are permitted provided that the following conditions
11 1.1 takemura * are met:
12 1.1 takemura * 1. Redistributions of source code must retain the above copyright
13 1.1 takemura * notice, this list of conditions and the following disclaimer.
14 1.1 takemura * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 takemura * notice, this list of conditions and the following disclaimer in the
16 1.1 takemura * documentation and/or other materials provided with the distribution.
17 1.1 takemura * 3. All advertising materials mentioning features or use of this software
18 1.1 takemura * must display the following acknowledgement:
19 1.1 takemura * This product includes software developed by the PocketBSD project
20 1.1 takemura * and its contributors.
21 1.1 takemura * 4. Neither the name of the project nor the names of its contributors
22 1.1 takemura * may be used to endorse or promote products derived from this software
23 1.1 takemura * without specific prior written permission.
24 1.1 takemura *
25 1.1 takemura * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 1.1 takemura * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.1 takemura * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.1 takemura * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 1.1 takemura * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 1.1 takemura * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 1.1 takemura * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.1 takemura * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.1 takemura * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.1 takemura * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1 takemura * SUCH DAMAGE.
36 1.1 takemura *
37 1.1 takemura */
38 1.1 takemura #include <pbsdboot.h>
39 1.1 takemura
40 1.1 takemura #define MAX_MEMORY (1024*1024*32) /* 32 MB */
41 1.1 takemura #define MEM_BLOCKS 8
42 1.1 takemura #define MEM_BLOCK_SIZE (1024*1024*4)
43 1.1 takemura
44 1.1 takemura struct addr_s {
45 1.2 uch caddr_t addr;
46 1.2 uch int in_use;
47 1.1 takemura };
48 1.1 takemura
49 1.1 takemura struct page_header_s {
50 1.2 uch unsigned long magic0;
51 1.2 uch int pageno;
52 1.2 uch unsigned long magic1;
53 1.1 takemura };
54 1.1 takemura
55 1.1 takemura struct map_s *map = NULL;
56 1.1 takemura struct addr_s *phys_addrs = NULL;
57 1.1 takemura unsigned char* heap = NULL;
58 1.1 takemura int npages;
59 1.1 takemura caddr_t kernel_start;
60 1.1 takemura caddr_t kernel_end;
61 1.1 takemura
62 1.1 takemura int
63 1.1 takemura vmem_exec(caddr_t entry, int argc, char *argv[], struct bootinfo *bi)
64 1.1 takemura {
65 1.2 uch int i;
66 1.2 uch caddr_t p;
67 1.1 takemura
68 1.2 uch if (map == NULL) {
69 1.2 uch debug_printf(TEXT("vmem is not initialized.\n"));
70 1.2 uch msg_printf(MSG_ERROR, whoami, TEXT("vmem is not initialized.\n"));
71 1.2 uch return (-1);
72 1.2 uch }
73 1.2 uch
74 1.2 uch debug_printf(TEXT("entry point=0x%x\n"), entry);
75 1.2 uch
76 1.2 uch map->entry = entry;
77 1.2 uch map->base = kernel_start;
78 1.2 uch
79 1.2 uch for (i = 0; i < argc; i++) {
80 1.2 uch argv[i] = vtophysaddr(argv[i]);
81 1.2 uch }
82 1.2 uch map->arg0 = (caddr_t)argc;
83 1.2 uch map->arg1 = vtophysaddr((caddr_t)argv);
84 1.2 uch map->arg2 = vtophysaddr((caddr_t)bi);
85 1.2 uch map->arg3 = NULL;
86 1.2 uch
87 1.2 uch if (map->arg1 == NULL || map->arg2 == NULL) {
88 1.2 uch debug_printf(TEXT("arg, vtophysaddr() failed\n"));
89 1.2 uch msg_printf(MSG_ERROR, whoami,
90 1.2 uch TEXT("arg, vtophysaddr() failed\n"));
91 1.2 uch return (-1);
92 1.2 uch }
93 1.2 uch
94 1.2 uch for (i = 0; p = map->leaf[i / map->leafsize][i % map->leafsize]; i++) {
95 1.2 uch if ((p = vtophysaddr(p)) == NULL) {
96 1.2 uch debug_printf(TEXT("vtophysaddr() failed, page %d (addr=0x%x) \n"),
97 1.2 uch i, map->leaf[i / map->leafsize][i % map->leafsize]);
98 1.2 uch msg_printf(MSG_ERROR, whoami,
99 1.2 uch TEXT("vtophysaddr() failed, page %d (addr=0x%x) \n"),
100 1.2 uch i, map->leaf[i / map->leafsize][i % map->leafsize]);
101 1.2 uch return (-1);
102 1.2 uch }
103 1.2 uch map->leaf[i / map->leafsize][i % map->leafsize] = p;
104 1.2 uch }
105 1.2 uch
106 1.2 uch for (i = 0; i < map->nleaves; i++) {
107 1.2 uch if ((p = vtophysaddr((caddr_t)map->leaf[i])) == NULL) {
108 1.2 uch debug_printf(TEXT("vtophysaddr() failed, leaf %d (addr=0x%x) \n"),
109 1.2 uch i, map->leaf[i / map->leafsize][i % map->leafsize]);
110 1.2 uch msg_printf(MSG_ERROR, whoami,
111 1.2 uch TEXT("vtophysaddr() failed, leaf %d (addr=0x%x) \n"),
112 1.2 uch i, map->leaf[i / map->leafsize][i % map->leafsize]);
113 1.2 uch return (-1);
114 1.2 uch }
115 1.2 uch map->leaf[i] = (caddr_t*)p;
116 1.2 uch }
117 1.2 uch
118 1.2 uch debug_printf(TEXT("execute startprog()\n"));
119 1.2 uch //return (-1);
120 1.2 uch return (startprog(vtophysaddr((caddr_t)map)));
121 1.1 takemura }
122 1.1 takemura
123 1.1 takemura DWORD
124 1.1 takemura getpagesize()
125 1.1 takemura {
126 1.2 uch static int init = 0;
127 1.2 uch static SYSTEM_INFO info;
128 1.1 takemura
129 1.2 uch if (!init) {
130 1.2 uch GetSystemInfo(&info);
131 1.2 uch init = 1;
132 1.2 uch }
133 1.1 takemura
134 1.2 uch return (info.dwPageSize);
135 1.1 takemura }
136 1.1 takemura
137 1.1 takemura caddr_t
138 1.1 takemura vmem_alloc()
139 1.1 takemura {
140 1.2 uch int i;
141 1.2 uch struct page_header_s *page;
142 1.2 uch for (i = 0; i < npages; i++) {
143 1.2 uch page = (struct page_header_s*)&heap[getpagesize() * i];
144 1.2 uch if (!phys_addrs[i].in_use &&
145 1.2 uch !(kernel_start <= phys_addrs[i].addr &&
146 1.2 uch phys_addrs[i].addr < kernel_end)) {
147 1.2 uch phys_addrs[i].in_use = 1;
148 1.2 uch return ((caddr_t)page);
149 1.2 uch }
150 1.2 uch }
151 1.2 uch return (NULL);
152 1.1 takemura }
153 1.1 takemura
154 1.1 takemura static caddr_t
155 1.1 takemura alloc_kpage(caddr_t phys_addr)
156 1.1 takemura {
157 1.2 uch int i;
158 1.2 uch struct page_header_s *page;
159 1.2 uch for (i = 0; i < npages; i++) {
160 1.2 uch page = (struct page_header_s*)&heap[getpagesize() * i];
161 1.2 uch if (phys_addrs[i].addr == phys_addr) {
162 1.2 uch if (phys_addrs[i].in_use) {
163 1.2 uch debug_printf(TEXT("page %d (phys addr=0x%x) is already in use\n"),
164 1.2 uch i, phys_addr);
165 1.2 uch msg_printf(MSG_ERROR, whoami,
166 1.2 uch TEXT("page %d (phys addr=0x%x) is already in use\n"),
167 1.2 uch i, phys_addr);
168 1.2 uch return (NULL);
169 1.2 uch }
170 1.2 uch phys_addrs[i].in_use = 1;
171 1.2 uch return ((caddr_t)page);
172 1.2 uch }
173 1.2 uch }
174 1.2 uch return (vmem_alloc());
175 1.1 takemura }
176 1.1 takemura
177 1.1 takemura caddr_t
178 1.1 takemura vmem_get(caddr_t phys_addr, int *length)
179 1.1 takemura {
180 1.2 uch int pageno = (phys_addr - kernel_start) / getpagesize();
181 1.2 uch int offset = (phys_addr - kernel_start) % getpagesize();
182 1.1 takemura
183 1.2 uch if (map == NULL || pageno < 0 || npages <= pageno) {
184 1.2 uch return (NULL);
185 1.2 uch }
186 1.2 uch if (length) {
187 1.2 uch *length = getpagesize() - offset;
188 1.2 uch }
189 1.2 uch return (map->leaf[pageno / map->leafsize][pageno % map->leafsize] + offset);
190 1.1 takemura }
191 1.1 takemura
192 1.1 takemura caddr_t
193 1.1 takemura vtophysaddr(caddr_t page)
194 1.1 takemura {
195 1.2 uch int pageno = (page - heap) / getpagesize();
196 1.2 uch int offset = (page - heap) % getpagesize();
197 1.1 takemura
198 1.2 uch if (map == NULL || pageno < 0 || npages <= pageno) {
199 1.2 uch return (NULL);
200 1.2 uch }
201 1.2 uch return (phys_addrs[pageno].addr + offset);
202 1.1 takemura }
203 1.1 takemura
204 1.1 takemura int
205 1.1 takemura vmem_init(caddr_t start, caddr_t end)
206 1.1 takemura {
207 1.2 uch int i, N, pageno;
208 1.2 uch unsigned long magic0;
209 1.2 uch unsigned long magic1;
210 1.2 uch int nfounds;
211 1.2 uch struct page_header_s *page;
212 1.2 uch long size;
213 1.2 uch int nleaves;
214 1.2 uch
215 1.2 uch /* align with page size */
216 1.2 uch start = (caddr_t)(((long)start / getpagesize()) * getpagesize());
217 1.2 uch end = (caddr_t)((((long)end + getpagesize() - 1) / getpagesize()) * getpagesize());
218 1.2 uch
219 1.2 uch kernel_start = start;
220 1.2 uch kernel_end = end;
221 1.2 uch size = end - start;
222 1.2 uch
223 1.2 uch /*
224 1.2 uch * program image pages.
225 1.2 uch */
226 1.2 uch npages = (size + getpagesize() - 1) / getpagesize();
227 1.2 uch
228 1.2 uch /*
229 1.2 uch * map leaf pages.
230 1.2 uch * npages plus one for end mark.
231 1.2 uch */
232 1.2 uch npages += (nleaves = ((npages * sizeof(caddr_t) + getpagesize()) / getpagesize()));
233 1.2 uch
234 1.2 uch /*
235 1.2 uch * map root page, startprg code page, argument page and bootinfo page.
236 1.2 uch */
237 1.2 uch npages += 4;
238 1.2 uch
239 1.2 uch /*
240 1.2 uch * allocate pages
241 1.2 uch */
242 1.2 uch debug_printf(TEXT("allocate %d pages\n"), npages);
243 1.2 uch heap = (unsigned char*)
244 1.2 uch VirtualAlloc(0,
245 1.2 uch npages * getpagesize(),
246 1.2 uch MEM_COMMIT,
247 1.2 uch PAGE_READWRITE | PAGE_NOCACHE);
248 1.2 uch if (heap == NULL) {
249 1.2 uch debug_printf(TEXT("can't allocate heap\n"));
250 1.2 uch msg_printf(MSG_ERROR, whoami, TEXT("can't allocate heap\n"));
251 1.2 uch goto error_cleanup;
252 1.2 uch }
253 1.2 uch
254 1.2 uch /*
255 1.2 uch * allocate address table.
256 1.2 uch */
257 1.2 uch phys_addrs = (struct addr_s *)
258 1.2 uch VirtualAlloc(0,
259 1.2 uch npages * sizeof(struct addr_s),
260 1.2 uch MEM_COMMIT,
261 1.2 uch PAGE_READWRITE);
262 1.2 uch if (phys_addrs == NULL) {
263 1.2 uch debug_printf(TEXT("can't allocate address table\n"));
264 1.2 uch msg_printf(MSG_ERROR, whoami, TEXT("can't allocate address table\n"));
265 1.2 uch goto error_cleanup;
266 1.2 uch }
267 1.2 uch
268 1.2 uch /*
269 1.2 uch * set magic number for each page in buffer.
270 1.2 uch */
271 1.2 uch magic0 = Random();
272 1.2 uch magic1 = Random();
273 1.2 uch debug_printf(TEXT("magic=%08x%08x\n"), magic0, magic1);
274 1.2 uch
275 1.2 uch for (i = 0; i < npages; i++) {
276 1.2 uch page = (struct page_header_s*)&heap[getpagesize() * i];
277 1.2 uch page->magic0 = magic0;
278 1.2 uch page->pageno = i;
279 1.2 uch page->magic1 = magic1;
280 1.2 uch phys_addrs[i].addr = 0;
281 1.2 uch phys_addrs[i].in_use = 0;
282 1.2 uch }
283 1.2 uch
284 1.2 uch /*
285 1.2 uch * Scan whole physical memory.
286 1.2 uch */
287 1.2 uch nfounds = 0;
288 1.2 uch for (N = 0; N < MEM_BLOCKS && nfounds < npages; N++) {
289 1.2 uch unsigned char* mem;
290 1.2 uch int res;
291 1.2 uch mem = (unsigned char*)
292 1.2 uch VirtualAlloc(0,
293 1.2 uch MEM_BLOCK_SIZE,
294 1.2 uch MEM_RESERVE,
295 1.2 uch PAGE_NOACCESS);
296 1.2 uch res = VirtualCopy((LPVOID)mem,
297 1.2 uch //(LPVOID)((0xa0000000 + MEM_BLOCK_SIZE * N) >> 8),
298 1.2 uch (LPVOID)((0x80000000 + MEM_BLOCK_SIZE * N) >> 8),
299 1.2 uch MEM_BLOCK_SIZE,
300 1.2 uch PAGE_READWRITE | PAGE_NOCACHE | PAGE_PHYSICAL);
301 1.2 uch
302 1.2 uch for (i = 0; i < (int)(MEM_BLOCK_SIZE/getpagesize()); i++) {
303 1.2 uch page = (struct page_header_s*)&mem[getpagesize() * i];
304 1.2 uch if (page->magic0 == magic0 &&
305 1.2 uch page->magic1 == magic1) {
306 1.2 uch pageno = page->pageno;
307 1.2 uch if (0 <= pageno && pageno < npages &&
308 1.2 uch phys_addrs[pageno].addr == 0) {
309 1.2 uch phys_addrs[pageno].addr =
310 1.2 uch (unsigned char*)(0x80000000 + MEM_BLOCK_SIZE * N +
311 1.2 uch getpagesize() * i);
312 1.2 uch page->magic0 = 0;
313 1.2 uch page->magic1 = 0;
314 1.2 uch if (npages <= ++nfounds) {
315 1.2 uch break;
316 1.2 uch }
317 1.2 uch } else {
318 1.2 uch debug_printf(TEXT("invalid page header\n"));
319 1.2 uch msg_printf(MSG_ERROR, whoami, TEXT("invalid page header\n"));
320 1.2 uch goto error_cleanup;
321 1.2 uch }
322 1.2 uch }
323 1.2 uch }
324 1.2 uch VirtualFree(mem, 0, MEM_RELEASE);
325 1.2 uch }
326 1.2 uch
327 1.2 uch if (nfounds < npages) {
328 1.2 uch debug_printf(TEXT("lost %d pages\n"), npages - nfounds);
329 1.2 uch msg_printf(MSG_ERROR, whoami, TEXT("lost %d pages\n"), npages - nfounds);
330 1.2 uch goto error_cleanup;
331 1.2 uch }
332 1.1 takemura
333 1.2 uch /*
334 1.2 uch * allocate root page
335 1.2 uch */
336 1.2 uch if ((map = (struct map_s*)vmem_alloc()) == NULL) {
337 1.2 uch debug_printf(TEXT("can't allocate root page.\n"));
338 1.2 uch msg_printf(MSG_ERROR, whoami, TEXT("can't allocate root page.\n"));
339 1.2 uch goto error_cleanup;
340 1.2 uch }
341 1.2 uch map->nleaves = nleaves;
342 1.2 uch map->leafsize = getpagesize() / sizeof(caddr_t);
343 1.2 uch map->pagesize = getpagesize();
344 1.2 uch
345 1.2 uch /*
346 1.2 uch * allocate leaf pages
347 1.2 uch */
348 1.2 uch for (i = 0; i < nleaves; i++) {
349 1.2 uch if ((map->leaf[i] = (caddr_t*)vmem_alloc()) == NULL) {
350 1.2 uch debug_printf(TEXT("can't allocate leaf page.\n"));
351 1.2 uch msg_printf(MSG_ERROR, whoami, TEXT("can't allocate leaf page.\n"));
352 1.2 uch goto error_cleanup;
353 1.2 uch }
354 1.2 uch }
355 1.2 uch
356 1.2 uch /*
357 1.2 uch * allocate kernel pages
358 1.2 uch */
359 1.2 uch for (i = 0; start < kernel_end; start += getpagesize(), i++) {
360 1.2 uch caddr_t *leaf = map->leaf[i / map->leafsize];
361 1.2 uch if ((leaf[i % map->leafsize] = alloc_kpage(start)) == NULL) {
362 1.2 uch debug_printf(TEXT("can't allocate page 0x%x.\n"), start);
363 1.2 uch msg_printf(MSG_ERROR, whoami, TEXT("can't allocate page 0x%x.\n"), start);
364 1.2 uch goto error_cleanup;
365 1.2 uch }
366 1.2 uch }
367 1.2 uch map->leaf[i / map->leafsize][i % map->leafsize] = NULL; /* END MARK */
368 1.2 uch
369 1.2 uch return (0);
370 1.1 takemura
371 1.1 takemura error_cleanup:
372 1.2 uch vmem_free();
373 1.1 takemura
374 1.2 uch return (-1);
375 1.1 takemura }
376 1.1 takemura
377 1.1 takemura void
378 1.1 takemura vmem_free()
379 1.1 takemura {
380 1.1 takemura map = NULL;
381 1.1 takemura if (heap) {
382 1.1 takemura VirtualFree(heap, 0, MEM_RELEASE);
383 1.1 takemura heap = NULL;
384 1.1 takemura }
385 1.1 takemura if (phys_addrs) {
386 1.1 takemura VirtualFree(phys_addrs, 0, MEM_RELEASE);
387 1.1 takemura phys_addrs = NULL;
388 1.1 takemura }
389 1.1 takemura }
390 1.1 takemura
391 1.1 takemura void
392 1.1 takemura vmem_dump_map()
393 1.1 takemura {
394 1.2 uch caddr_t addr, page, paddr;
395 1.2 uch
396 1.2 uch if (map == NULL) {
397 1.2 uch debug_printf(TEXT("no page map\n"));
398 1.2 uch return;
399 1.2 uch }
400 1.1 takemura
401 1.2 uch for (addr = kernel_start; addr < kernel_end; addr += getpagesize()) {
402 1.2 uch page = vmem_get(addr, NULL);
403 1.2 uch paddr = vtophysaddr(page);
404 1.2 uch debug_printf(TEXT("%08X: vaddr=%08X paddr=%08X %s\n"),
405 1.2 uch addr, page, paddr, addr == paddr ? TEXT("*") : TEXT("reloc"));
406 1.1 takemura
407 1.2 uch }
408 1.1 takemura }
409