37
|
1 #include "kernel.h"
|
|
2
|
|
3 multiboot_info_t *multiboot_info = 0; // Set by startup code.
|
|
4 uint64_t available_memory = 0;
|
|
5 uint64_t ramdisk_location = 0;
|
|
6
|
|
7 /* Retrieve memory information from multiboot header */
|
|
8 void read_multiboot_info()
|
|
9 {
|
|
10 if ((multiboot_info->flags & (1 << 6)) == (1 << 6))
|
|
11 {
|
|
12 multiboot_memory_map_t *mmap;
|
|
13 for (mmap = (multiboot_memory_map_t*)(uint64_t)multiboot_info->mmap_addr;
|
|
14 (uint64_t)mmap < multiboot_info->mmap_addr + multiboot_info->mmap_length;
|
|
15 mmap = (multiboot_memory_map_t*) ( (uint64_t)mmap + mmap->size + sizeof(mmap->size)) )
|
|
16 {
|
|
17 /*
|
|
18 printf("size: %d, start: 0x%x, length=0x%x, type=%d\n", mmap->size,
|
|
19 mmap->base, mmap->length, mmap->type);
|
|
20 */
|
|
21
|
|
22 if ( (mmap->type == 1) && (mmap->base == 0x100000) )
|
|
23 {
|
|
24 available_memory = mmap->length;
|
|
25 }
|
|
26 }
|
|
27 }
|
|
28
|
|
29 if (available_memory == 0)
|
|
30 {
|
|
31 panic("Found no usable memory in grub's memory map\n");
|
|
32 }
|
|
33
|
|
34 if ((multiboot_info->flags & (1 << 3)) == (1 << 3))
|
|
35 {
|
|
36 uint64_t i;
|
|
37 multiboot_module_t *mod = (multiboot_module_t*)(uint64_t)multiboot_info->mods_addr;
|
|
38 for (i=0; i<multiboot_info->mods_count; i++)
|
|
39 {
|
|
40 // TODO: determine better way of finding the initrd module.
|
|
41 if (i == 0)
|
|
42 {
|
|
43 ramdisk_location = mod->mod_start;
|
|
44 }
|
|
45
|
|
46 // Make sure that placement malloc does not overwrite the ramdisk.
|
|
47 uint64_t new_placement = (uint64_t)mod->mod_end;
|
|
48 if (new_placement > placement_address)
|
|
49 {
|
|
50 if ((new_placement & 0xFFF) != 0)
|
|
51 {
|
|
52 new_placement = (new_placement & (~0xFFF)) + 0x1000; // Align to 4 KiB page.
|
|
53 }
|
|
54 placement_address = new_placement;
|
|
55 }
|
|
56 mod++; // Go to next module.
|
|
57 }
|
|
58 }
|
|
59 }
|
|
60
|