Implemented memory map routines

This commit is contained in:
lucic71 2022-06-28 19:40:51 +03:00
parent a16f07de0d
commit 6d0a6b84cb
5 changed files with 114 additions and 12 deletions

View File

@ -10,35 +10,52 @@ SECTIONS {
. = 0x00100000;
/*
* Align all sections from all files to a 4KB boundary.
*
*/
/* Align all sections from all files to a 4KB boundary. */
_kernel_start = .;
_kernel_text_start = .;
.text ALIGN (0x1000) : {
*(.multiboot)
*(.text)
}
_kernel_text_end = .;
_kernel_rodata_start = .;
.rodata ALIGN (0x1000) : {
*(.rodata*)
}
_kernel_rodata_end = .;
_kernel_data_start = .;
.data ALIGN (0x1000) : {
*(.data)
}
/*
* Note:
* COMMON section keeps data objects that are not yet allocated,
* this is just a prelinking section, it will be transformed into bss
* and data after linking.
*
*/
_kernel_data_end = .;
_kernel_bss_start = .;
.bss ALIGN (0x1000) : {
*(COMMON)
*(.bss)
}
_kernel_bss_end = .;
_kernel_end = .;
/DISCARD/ : {
*(.comment)
*(.note.gnu.build-id)
}
}
/*
* Note:
* COMMON section keeps data objects that are not yet allocated,
* this is just a prelinking section, it will be transformed into bss
* and data after linking.
*
*/

View File

@ -6,7 +6,7 @@
/*
* print_memory_map
* Using the multiboot structure, print memory regions, size
* of physical memory and info about kernel sections.
* of physical memory.
*
* @param mb_info - Multiboot info
*
@ -14,4 +14,12 @@
void print_memory_map(multiboot_info_t *mb_info);
/*
* print_ksections
* Print the layout of the kernel in memory.
*
*/
void print_kernel_map(void);
#endif

View File

@ -24,6 +24,8 @@ void kmain(multiboot_info_t *mb_info, uint32_t mb_magic) {
panic("Not a multiboot bootloader!");
print_memory_map(mb_info);
puts("");
print_kernel_map();
serial_init(COM1);

30
kernel/pmm/src/kmap.c Normal file
View File

@ -0,0 +1,30 @@
#include "kernel/pmm.h"
#include <stdio.h>
#include <stdint.h>
extern uint8_t *_kernel_start;
extern uint8_t *_kernel_end;
extern uint8_t *_kernel_text_start;
extern uint8_t *_kernel_text_end;
extern uint8_t *_kernel_rodata_start;
extern uint8_t *_kernel_rodata_end;
extern uint8_t *_kernel_data_start;
extern uint8_t *_kernel_data_end;
extern uint8_t *_kernel_bss_start;
extern uint8_t *_kernel_bss_end;
void print_kernel_map(void) {
printf("Kernel Memory Map:\n");
printf("kernel: 0x%x - 0x%x\n", &_kernel_start, &_kernel_end);
printf("text: 0x%x - 0x%x\n", &_kernel_text_start, &_kernel_text_end);
printf("rodata: 0x%x - 0x%x\n", &_kernel_rodata_start, &_kernel_rodata_end);
printf("data: 0x%x - 0x%x\n", &_kernel_data_start, &_kernel_data_end);
printf("bss: 0x%x - 0x%x\n", &_kernel_bss_start, &_kernel_bss_end);
}

45
kernel/pmm/src/mmap.c Normal file
View File

@ -0,0 +1,45 @@
#include "kernel/pmm.h"
#include <stdio.h>
#include <stdint.h>
/* Defined in kernel/multiboot.h as macros. */
static char *mem_types[] = {
"(Available)",
"(Reserved)",
"(ACPI Reclaimable)",
"(ACPI NVS)",
"(BadRAM)"
};
/*
* print_memory_map:
* -----------------
*
* Extract mmap_addr from mb_info and iterate through all avaialble
* memory zones.
*
*/
void print_memory_map(multiboot_info_t *mb_info) {
printf("Physical Memory Map:\n");
multiboot_memory_map_t *mmap =
(multiboot_memory_map_t *) mb_info->mmap_addr;
multiboot_memory_map_t *mmap_end =
(multiboot_memory_map_t *) (mb_info->mmap_addr + mb_info->mmap_length);
for (int i = 0; mmap < mmap_end; mmap++, i++) {
printf("region: %d start: 0x%x length: 0x%x type: %d ", i,
(uint32_t) mmap->addr, (uint32_t) mmap->len, mmap->type);
printf("%s\n", mem_types[mmap->type - 1]);
}
}