b66207ad17f40ef40b217c68bd9b701b49615f9e
[akaros.git] / kern / src / multiboot.c
1 #include <multiboot.h>
2 #include <arch/types.h>
3 #include <arch/mmu.h>
4 #include <arch/arch.h>
5 #include <ros/memlayout.h>
6 #include <stdio.h>
7
8 #ifdef __i386__
9 #include <arch/apic.h>
10 #endif
11
12 // These variables are set by i386_detect_memory()
13 physaddr_t maxpa;// Maximum physical address
14 physaddr_t maxaddrpa;    // Maximum directly addressable physical address
15 void *SNT maxaddrpa_ptr;
16 size_t npage;   // Amount of physical memory (in pages)
17 size_t naddrpage;       // Amount of addressable physical memory (in pages)
18 static size_t basemem;  // Amount of base memory (in bytes)
19 static size_t extmem;   // Amount of extended memory (in bytes)
20
21 void
22 multiboot_detect_memory(multiboot_info_t *mbi)
23 {
24         // Tells us how many kilobytes there are
25         basemem = ROUNDDOWN(mbi->mem_lower*1024, PGSIZE);
26         extmem = ROUNDDOWN(mbi->mem_upper*1024, PGSIZE);
27
28         // Calculate the maximum physical address based on whether
29         // or not there is any extended memory.  See comment in <inc/memlayout.h>
30         if (extmem)
31                 maxpa = EXTPHYSMEM + extmem;
32         else
33                 maxpa = basemem;
34
35         npage = maxpa / PGSIZE;
36
37         // IOAPIC - KERNBASE is the max amount of virtual addresses we can use
38         // for the physical memory mapping (aka - the KERNBASE mapping)
39         maxaddrpa = MIN(maxpa, IOAPIC_BASE - KERNBASE);
40         maxaddrpa_ptr = (void *SNT)maxaddrpa;
41
42         naddrpage = maxaddrpa / PGSIZE;
43
44         cprintf("Physical memory: %dK available, ", (int)(maxpa/1024));
45         cprintf("base = %dK, extended = %dK\n", (int)(basemem/1024), (int)(extmem/1024));
46         printk("Maximum directly addressable physical memory: %dK\n", (int)(maxaddrpa/1024));
47 }
48
49 void
50 multiboot_print_memory_map(multiboot_info_t *mbi) {
51         const char *NTS memory_type[] = {"", "FREE", "RESERVED", "UNDEFINED", "UNDEFINED4"};
52
53
54         if(CHECK_FLAG(mbi->flags, 6)) {
55                 memory_map_t *SNT mmap_b =
56                         (memory_map_t *SNT)(mbi->mmap_addr + KERNBASE);
57                 memory_map_t *SNT mmap_e =
58                         (memory_map_t *SNT)(mbi->mmap_addr + KERNBASE + mbi->mmap_length);
59                 memory_map_t *BND(mmap_b, mmap_e) mmap = TC(mmap_b);
60
61                 cprintf ("mmap_addr = 0x%x, mmap_length = 0x%x\n", (unsigned long)mbi->mmap_addr,
62                            (unsigned long)mbi->mmap_length);
63
64                 while(mmap < mmap_e) {
65                         cprintf ("base = 0x%08x%08x, length = 0x%08x%08x, type = %s\n",
66                                 (unsigned) mmap->base_addr_high,
67                                 (unsigned) mmap->base_addr_low,
68                                 (unsigned) mmap->length_high,
69                                 (unsigned) mmap->length_low,
70                                 (unsigned) memory_type[mmap->type]);
71                         mmap = (memory_map_t *BND(mmap_b,mmap_e))((char *BND(mmap_b,mmap_e))mmap + mmap->size + sizeof(mmap->size));
72                 }
73         }
74 }
75