Skip to content

Commit

Permalink
添加用户堆架构, 增加系统信息获取
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyi1212 committed Nov 15, 2024
1 parent a48bd7e commit df788d0
Show file tree
Hide file tree
Showing 24 changed files with 538 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/boot/boot.asm
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ times 52 db 0 ; 显式初始化 52 个字节为零

section .text
global _start
extern kernel_main ; 内核主函数 main.c
extern kernel_head ; 内核预处理函数 src/core/mboot/kernel_head.c

_start:
mov esp,stack_top
push esp
push ebx
call kernel_main
call kernel_head
L1:
hlt
jmp L1
Expand Down
2 changes: 2 additions & 0 deletions src/core/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "krlibc.h"
#include "syscall.h"
#include "shell.h"
#include "cpuid.h"

extern void* program_break_end;

Expand Down Expand Up @@ -56,6 +57,7 @@ _Noreturn void kernel_main(multiboot_t *multiboot, uint32_t kernel_stack) {
printk("KernelArea: 0x00000000 - 0x%08x | GraphicsBuffer: 0x%08x \n",
program_break_end,
multiboot->framebuffer_addr);
init_cpuid();
klogf(true, "Memory manager initialize.\n");
acpi_install(); //ACPI初始化
init_timer(1); //RTC 时钟中断
Expand Down
15 changes: 15 additions & 0 deletions src/core/mboot/kernel_head.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "multiboot.h"
#include "ctypes.h"

extern void kernel_main(multiboot_t *multiboot, uint32_t kernel_stack);
extern void init_mmap(multiboot_t *multiboot);

/*
* 该mboot目录下的源文件与 multiboot 引导协议耦合度较高, 其主要功能对协议依赖性较大
* 故单独拆分出来, 方便移植
* kernel_head 由 boot.asm/_start 调用
*/
void kernel_head(multiboot_t *multiboot, uint32_t kernel_stack){
init_mmap(multiboot);
kernel_main(multiboot,kernel_stack);
}
32 changes: 32 additions & 0 deletions src/core/mboot/memory_map.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "multiboot.h"
#include "ctypes.h"
#include "klog.h"

uint32_t phy_mem_size;

void show_memory_map(multiboot_t *mboot) {
uint32_t mmap_addr = mboot->mmap_addr;
uint32_t mmap_length = mboot->mmap_length;

mmap_entry_t *mmap = (mmap_entry_t *) mmap_addr;
for (mmap = (mmap_entry_t *) mmap_addr; (uint32_t) mmap < mmap_addr + mmap_length; mmap++) {
if(mmap->type == MULTIBOOT_MEMORY_ACPI_RECLAIMABLE)
logkf("mmap: %08x ACPI\n",(uint32_t) mmap->base_addr_low);
else if(mmap->type == MULTIBOOT_MEMORY_BADRAM)
logkf("mmap: %08x BAD\n",(uint32_t) mmap->base_addr_low);
else if(mmap->type == MULTIBOOT_MEMORY_INFO)
logkf("mmap: %08x INFO\n",(uint32_t) mmap->base_addr_low);
else if(mmap->type == MULTIBOOT_MEMORY_AVAILABLE)
logkf("mmap: %08x AVAILABLE\n",(uint32_t) mmap->base_addr_low);
else if(mmap->type == MULTIBOOT_MEMORY_NVS)
logkf("mmap: %08x NVS\n",(uint32_t) mmap->base_addr_low);
else if(mmap->type == MULTIBOOT_MEMORY_RESERVED)
logkf("mmap: %08x RESERVED\n",(uint32_t) mmap->base_addr_low);
else logkf("mmap: %08x UNKNOWN\n",(uint32_t) mmap->base_addr_low);
}
}

void init_mmap(multiboot_t *multiboot){
phy_mem_size = (multiboot->mem_upper + multiboot->mem_lower) / 1024;
show_memory_map(multiboot);
}
8 changes: 7 additions & 1 deletion src/core/memory/free_page.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,10 @@ void setup_free_page(){
fifo8 = kmalloc(sizeof(struct FIFO8));
uint8_t *buf = kmalloc(sizeof(uint32_t) * MAX_FREE_QUEUE);
fifo8_init(fifo8,sizeof(uint32_t) * MAX_FREE_QUEUE,buf);
}
}

uint32_t kh_usage_memory_byte = 0;

uint32_t get_kernel_memory_usage(){
return kh_usage_memory_byte;
}
10 changes: 8 additions & 2 deletions src/core/memory/kmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ extern page_directory_t *kernel_directory; //page.c
void *program_break = (void*)0x3e0000;
void *program_break_end;

void *sbrk(int incr) { //内核堆扩容措施
#define getpagesize() PAGE_SIZE

static void *sbrk(int incr) { //内核堆扩容措施
if (program_break == 0) {
return (void *) -1;
}
Expand Down Expand Up @@ -74,6 +76,8 @@ static void morecore(int bucket);

static int findbucket(union overhead *freep, int srchlen);

extern uint32_t kh_usage_memory_byte; //free_page.c

void *kmalloc(size_t nbytes) {
register union overhead *op;
register int bucket;
Expand Down Expand Up @@ -124,6 +128,7 @@ void *kmalloc(size_t nbytes) {
nextf[bucket] = op->ov_next;
op->ov_magic = MAGIC;
op->ov_index = bucket;
kh_usage_memory_byte += bucket;
return ((char *) (op + 1));
}

Expand Down Expand Up @@ -183,6 +188,7 @@ void kfree(void *cp) {
#ifdef MSTATS
nmalloc[size]--;
#endif
kh_usage_memory_byte -= size;
}


Expand All @@ -201,7 +207,7 @@ size_t kmalloc_usable_size(void *cp) {
return op->ov_index;
}

int realloc_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */
static int realloc_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */

void *krealloc(void *cp, size_t nbytes) {
register u_long onb;
Expand Down
Loading

0 comments on commit df788d0

Please sign in to comment.