Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*	$NetBSD: vmem.c,v 1.7 2009/03/18 10:22:29 cegger Exp $	*/

/*-
 * Copyright (c) 1999 Shin Takemura.
 * All rights reserved.
 *
 * This software is part of the PocketBSD.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the PocketBSD project
 *	and its contributors.
 * 4. Neither the name of the project nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */
#include <pbsdboot.h>

struct addr_s {
	void *addr;
	int in_use;
};

struct page_header_s {
	unsigned long magic0;
	int pageno;
	unsigned long magic1;
};

struct map_s *map = NULL;
struct addr_s *phys_addrs = NULL;
unsigned char* heap = NULL;
int npages;
void *kernel_start;
void *kernel_end;

int
vmem_exec(void *entry, int argc, char *argv[], struct bootinfo *bi)
{
	int i;
	void *p;

	if (map == NULL) {
		debug_printf(TEXT("vmem is not initialized.\n"));
		msg_printf(MSG_ERROR, whoami, TEXT("vmem is not initialized.\n"));
		return (-1);
	}

	debug_printf(TEXT("entry point=0x%x\n"), entry);

	map->entry = entry;
	map->base = kernel_start;

	for (i = 0; i < argc; i++) {
		argv[i] = vtophysaddr(argv[i]);
	}
	map->arg0 = (void *)argc;
	map->arg1 = vtophysaddr((void *)argv);
	map->arg2 = vtophysaddr((void *)bi);
	map->arg3 = NULL;

	if (map->arg1 == NULL || map->arg2 == NULL) {
		debug_printf(TEXT("arg, vtophysaddr() failed\n"));
		msg_printf(MSG_ERROR, whoami,
			   TEXT("arg, vtophysaddr() failed\n"));
		return (-1);
	}

	for (i = 0; p = map->leaf[i / map->leafsize][i % map->leafsize]; i++)  {
		if ((p = vtophysaddr(p)) == NULL) {
			debug_printf(TEXT("vtophysaddr() failed, page %d (addr=0x%x) \n"),
				     i, map->leaf[i / map->leafsize][i % map->leafsize]);
			msg_printf(MSG_ERROR, whoami,
				   TEXT("vtophysaddr() failed, page %d (addr=0x%x) \n"),
				   i, map->leaf[i / map->leafsize][i % map->leafsize]);
			return (-1);
		}
		map->leaf[i / map->leafsize][i % map->leafsize] = p;
	}

	for (i = 0; i < map->nleaves; i++) {
		if ((p = vtophysaddr((void *)map->leaf[i])) == NULL) {
			debug_printf(TEXT("vtophysaddr() failed, leaf %d (addr=0x%x) \n"),
				     i, map->leaf[i / map->leafsize][i % map->leafsize]);
			msg_printf(MSG_ERROR, whoami,
				   TEXT("vtophysaddr() failed, leaf %d (addr=0x%x) \n"),
				   i, map->leaf[i / map->leafsize][i % map->leafsize]);
			return (-1);
		}
		map->leaf[i] = (void **)p;
	}

	debug_printf(TEXT("execute startprog()\n"));
	//return (-1);
	close_debug_log();
	return ((*system_info.si_boot)(vtophysaddr((void *)map)));
}

void *
vmem_alloc(void)
{
	int i, pagesize;
	struct page_header_s *page;

	pagesize = system_info.si_pagesize;
	for (i = 0; i < npages; i++) {
		page = (struct page_header_s*)&heap[pagesize * i];
		if (!phys_addrs[i].in_use &&
		    !(kernel_start <= phys_addrs[i].addr &&
		      phys_addrs[i].addr < kernel_end)) {
			phys_addrs[i].in_use = 1;
			return ((void *)page);
		}
	}
	return (NULL);
}

static void *
alloc_kpage(void *phys_addr)
{
	int i, pagesize;
	struct page_header_s *page;

	pagesize = system_info.si_pagesize;
	for (i = 0; i < npages; i++) {
		page = (struct page_header_s*)&heap[pagesize * i];
		if (phys_addrs[i].addr == phys_addr) {
			if (phys_addrs[i].in_use) {
				debug_printf(TEXT("page %d (phys addr=0x%x) is already in use\n"),
					     i, phys_addr);
				msg_printf(MSG_ERROR, whoami,
					   TEXT("page %d (phys addr=0x%x) is already in use\n"),
					   i, phys_addr);
				return (NULL);
			}
			phys_addrs[i].in_use = 1;
			return ((void *)page);
		}
	}
	return (vmem_alloc());
}

void *
vmem_get(void *phys_addr, int *length)
{
	int pagesize = system_info.si_pagesize;
	int pageno = (phys_addr - kernel_start) / pagesize;
	int offset = (phys_addr - kernel_start) % pagesize;

	if (map == NULL || pageno < 0 || npages <= pageno) {
		return (NULL);
	}
	if (length) {
		*length = pagesize - offset;
	}
	return (map->leaf[pageno / map->leafsize][pageno % map->leafsize] + offset);
}

void *
vtophysaddr(void *page)
{
	int pageno = (page - heap) / system_info.si_pagesize;
	int offset = (page - heap) % system_info.si_pagesize;

	if (map == NULL || pageno < 0 || npages <= pageno) {
		return (NULL);
	}
	return (phys_addrs[pageno].addr + offset);
}

int
vmem_init(void *start, void *end)
{
#define MEM_BLOCK_SIZE (1024*1024*4) /* must be greater than page size */
	int i, m, pageno;
	unsigned long magic0;
	unsigned long magic1;
	int nfounds;
	struct page_header_s *page;
	long size;
	int nleaves;
	int pagesize, memblocks;

	pagesize = system_info.si_pagesize;
	memblocks = (system_info.si_drammaxsize) / MEM_BLOCK_SIZE;

	/* align with page size */
	start = (void *)(((long)start / pagesize) * pagesize);
	end = (void *)((((long)end + pagesize - 1) / pagesize) * pagesize);

	kernel_start = start;
	kernel_end = end;
	size = end - start;

	/*
	 *  program image pages.
	 */
	npages = (size + pagesize - 1) / pagesize;

	/*
	 *  map leaf pages.
	 *  npages plus one for end mark.
	 */
	npages += (nleaves = ((npages * sizeof(void *) + pagesize) / pagesize));

	/*
	 *  map root page, startprg code page, argument page and bootinfo page.
	 */
	npages += 4;

	/*
	 *  allocate pages
	 */
	debug_printf(TEXT("allocate %d pages\n"), npages);
	heap = (unsigned char*)
		VirtualAlloc(0,
			     npages * pagesize,
			     MEM_COMMIT,
			     PAGE_READWRITE | PAGE_NOCACHE);
	if (heap == NULL) {
		debug_printf(TEXT("can't allocate heap\n"));
		msg_printf(MSG_ERROR, whoami, TEXT("can't allocate heap\n"));
		goto error_cleanup;
	}

	/*
	 *  allocate address table.
	 */
	phys_addrs = (struct addr_s *)
		VirtualAlloc(0,
			     npages * sizeof(struct addr_s),
			     MEM_COMMIT,
			     PAGE_READWRITE);
	if (phys_addrs == NULL) {
		debug_printf(TEXT("can't allocate address table\n"));
		msg_printf(MSG_ERROR, whoami, TEXT("can't allocate address table\n"));
		goto error_cleanup;
	}

	/*
	 *  set magic number for each page in buffer.
	 */
	magic0 = Random();
	magic1 = Random();
	debug_printf(TEXT("magic=%08x%08x\n"), magic0, magic1);

	for (i = 0; i < npages; i++) {
		page = (struct page_header_s*)&heap[pagesize * i];
		page->magic0 = magic0;
		page->pageno = i;
		page->magic1 = magic1;
		phys_addrs[i].addr = 0;
		phys_addrs[i].in_use = 0;
	}

	/*
	 *  Scan whole physical memory.
	 */
	nfounds = 0;
	for (m = 0; (m < memblocks) && (nfounds < npages); m++) {
		unsigned char* mem;
		/* Map physical memory block */
		mem = (unsigned char*)VirtualAlloc(0, MEM_BLOCK_SIZE, 
						   MEM_RESERVE, PAGE_NOACCESS);
		if(!VirtualCopy((LPVOID)mem, (LPVOID)
				((system_info.si_dramstart + MEM_BLOCK_SIZE * m) >> 8),
				MEM_BLOCK_SIZE,	
				PAGE_READWRITE | PAGE_NOCACHE | PAGE_PHYSICAL)) {
			VirtualFree(mem, 0, MEM_RELEASE);			
			continue;
		}
		/* Find preliminary allocated pages */
		for (i = 0; i < (int)(MEM_BLOCK_SIZE / pagesize); i++) {
			page = (struct page_header_s*)&mem[pagesize * i];
			if (page->magic0 == magic0 &&
			    page->magic1 == magic1) {
				pageno = page->pageno;
				if (0 <= pageno && pageno < npages &&
				    phys_addrs[pageno].addr == 0) {
					/* Set kernel virtual addr. XXX mips dependent */
					phys_addrs[pageno].addr = (unsigned char*)
						((0x80000000 |
						  system_info.si_dramstart) +
						 MEM_BLOCK_SIZE * m +
						 pagesize * i);
					page->magic0 = 0;
					page->magic1 = 0;
					if (npages <= ++nfounds) {
						break;
					}
				} else {
					debug_printf(TEXT("invalid page header\n"));
					msg_printf(MSG_ERROR, whoami, TEXT("invalid page header\n"));
					goto error_cleanup;
				}
			}
		}
		VirtualFree(mem, 0, MEM_RELEASE);
	}

	if (nfounds < npages) {
		debug_printf(TEXT("lost %d pages\n"), npages - nfounds);
		msg_printf(MSG_ERROR, whoami, 
			   TEXT("lost %d pages (allocated %d pages)\n"), 
			   npages - nfounds, npages);
		goto error_cleanup;
	}

	/*
	 *  allocate root page
	 */
	if ((map = (struct map_s*)vmem_alloc()) == NULL) {
		debug_printf(TEXT("can't allocate root page.\n"));
		msg_printf(MSG_ERROR, whoami, TEXT("can't allocate root page.\n"));
		goto error_cleanup;
	}
	map->nleaves = nleaves;
	map->leafsize = pagesize / sizeof(void *);
	map->pagesize = pagesize;

	/*
	 *  allocate leaf pages
	 */
	for (i = 0; i < nleaves; i++) {
		if ((map->leaf[i] = (void **)vmem_alloc()) == NULL) {
			debug_printf(TEXT("can't allocate leaf page.\n"));
			msg_printf(MSG_ERROR, whoami, TEXT("can't allocate leaf page.\n"));
			goto error_cleanup;
		}
	}

	/*
	 *  allocate kernel pages
	 */
	for (i = 0; start < kernel_end; start += pagesize, i++) {
		void **leaf = map->leaf[i / map->leafsize];
		if ((leaf[i % map->leafsize] = alloc_kpage(start)) == NULL) {
			debug_printf(TEXT("can't allocate page 0x%x.\n"), start);
			msg_printf(MSG_ERROR, whoami, TEXT("can't allocate page 0x%x.\n"), start);
			goto error_cleanup;
		}
	}
	map->leaf[i / map->leafsize][i % map->leafsize] = NULL; /* END MARK */

	return (0);

 error_cleanup:
	vmem_free();

	return (-1);
}

void
vmem_free(void)
{
	map = NULL;
	if (heap) {
		VirtualFree(heap, 0, MEM_RELEASE);
		heap = NULL;
	}
	if (phys_addrs) {
		VirtualFree(phys_addrs, 0, MEM_RELEASE);
		phys_addrs = NULL;
	}
}

void
vmem_dump_map(void)
{
	void *addr, page, paddr;

	if (map == NULL) {
		debug_printf(TEXT("no page map\n"));
		return;
	}

	for (addr = kernel_start; addr < kernel_end; addr += system_info.si_pagesize) {
		page = vmem_get(addr, NULL);
		paddr = vtophysaddr(page);
		debug_printf(TEXT("%08X: vaddr=%08X paddr=%08X %s\n"),
			     addr, page, paddr, addr == paddr ? TEXT("*") : TEXT("reloc"));
    
	}
}