Merge branch 'master' into net-dev (with code changes listed below besides normal...
[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 in the system
14 physaddr_t maxaddrpa;  // Maximum addressable physical address
15 void *SNT maxaddrpa_ptr;
16 size_t npages;         // Total number of physical memory pages
17 size_t naddrpages;        // Number of addressable physical memory pages
18
19 static size_t basemem;  // Amount of base memory (in bytes)
20 static size_t extmem;   // Amount of extended memory (in bytes)
21
22 void
23 multiboot_detect_memory(multiboot_info_t *mbi)
24 {
25         // Tells us how many kilobytes there are
26         basemem = ROUNDDOWN(mbi->mem_lower*1024, PGSIZE);
27         extmem = ROUNDDOWN(mbi->mem_upper*1024, PGSIZE);
28
29         // Calculate the maximum physical address based on whether
30         // or not there is any extended memory.  See comment in <inc/memlayout.h>
31         if (extmem)
32                 maxpa = EXTPHYSMEM + extmem;
33         else
34                 maxpa = basemem;
35
36         npages = maxpa / PGSIZE;
37
38         // IOAPIC - KERNBASE is the max amount of virtual addresses we can use
39         // for the physical memory mapping (aka - the KERNBASE mapping)
40         maxaddrpa = MIN(maxpa, IOAPIC_BASE - KERNBASE);
41         maxaddrpa_ptr = (void *SNT)maxaddrpa;
42
43         naddrpages = maxaddrpa / PGSIZE;
44
45         cprintf("Physical memory: %dK available, ", (int)(maxpa/1024));
46         cprintf("base = %dK, extended = %dK\n", (int)(basemem/1024), (int)(extmem/1024));
47         printk("Maximum directly addressable physical memory: %dK\n", (int)(maxaddrpa/1024));
48 }
49
50 void
51 multiboot_print_memory_map(multiboot_info_t *mbi) {
52         const char *NTS memory_type[] = {"", "FREE", "RESERVED", "UNDEFINED", "UNDEFINED4"};
53
54
55         if(CHECK_FLAG(mbi->flags, 6)) {
56                 memory_map_t *SNT mmap_b =
57                         (memory_map_t *SNT)(mbi->mmap_addr + KERNBASE);
58                 memory_map_t *SNT mmap_e =
59                         (memory_map_t *SNT)(mbi->mmap_addr + KERNBASE + mbi->mmap_length);
60                 memory_map_t *BND(mmap_b, mmap_e) mmap = TC(mmap_b);
61
62                 cprintf ("mmap_addr = 0x%x, mmap_length = 0x%x\n", (unsigned long)mbi->mmap_addr,
63                            (unsigned long)mbi->mmap_length);
64
65                 while(mmap < mmap_e) {
66                         cprintf ("base = 0x%08x%08x, length = 0x%08x%08x, type = %s\n",
67                                 (unsigned) mmap->base_addr_high,
68                                 (unsigned) mmap->base_addr_low,
69                                 (unsigned) mmap->length_high,
70                                 (unsigned) mmap->length_low,
71                                 (unsigned) memory_type[mmap->type]);
72                         mmap = (memory_map_t *BND(mmap_b,mmap_e))((char *BND(mmap_b,mmap_e))mmap + mmap->size + sizeof(mmap->size));
73                 }
74         }
75 }
76