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