Added or completed: map(), unmap() pm_reserve(), pm_alloc(), pm_free(), pm_init(), get_paddr()

This commit is contained in:
notsomeidiot123 2024-10-30 13:39:33 -04:00
parent e7d272a8c7
commit cfdb33fc77
5 changed files with 97 additions and 7 deletions

1
.gitignore vendored
View File

@ -8,3 +8,4 @@ diskwrite
*.dump
intlog.*
*.img
.gitignore

View File

@ -1,12 +1,58 @@
#include <stdint.h>
#include "../shared/memory.h"
#include "../shared/kstdlib.h"
uint8_t pm_map[0x20000];
int pm_alloc(){
#define mmap_count 0x200000
uint8_t pm_map[mmap_count];
uint32_t total_memory;
uint32_t total_memory_usable;
uint32_t last_allocated;
uint32_t pm_alloc(){
for(uint32_t i = 0; i < mmap_count; i++){
for(int j = 0; j < 8; j++){
if(!(pm_map[i] & (1 << j))){
return (i << 3 | j) << 12;
}
}
}
}
void pm_free(uint32_t address){
pm_map[address >> 15] &= ~(1 << ((address>>12) & 7));
}
void pm_reserve(uint32_t address){
pm_map[address >> 15] |= 1 << ((address>>12) & 7);
}
int pm_init(kernel_info_t *kernel_info){
mmap_entry_t *mmap = (mmap_entry_t*)(kernel_info->mmap_ptr);
// printf("mmap count: %d", kernel_info->mmap_entry_count);
for(uint32_t i = 0; i < mmap_count; i++){
pm_map[i] = 0;
}
for(uint32_t i = 0; i < kernel_info->mmap_entry_count; i++){
for(uint32_t j = 0; j < (mmap[i].entry_length >> 12); j++){
// printf("j = %d\nIndex:%x\n", j, (mmap[i].entry_base >> 12) + (j & 3));
if(mmap[i].type != BIOS_MMAP_USABLE){
pm_map[(mmap[i].entry_base >> 12) + (j >> 3)] |= 1 << (j & 7);
}
else{
// uint8_t tmp = pm_map[(mmap[i].entry_base >> 12 ) + (j >> 3)];
// tmp &= ~(1 << (j & 3));
pm_map[(mmap[i].entry_base >> 12) + (j >> 3)] &= ~(1 << (j & 7));
}
// if(j % 8 == 7){
// printf("%d, %x, %d\n", j >> 3, pm_map[(mmap[i].entry_base >> 12 ) + (j >> 3)], mmap[i].type);
// }
}
}
int pm_init(kernel_info_t *kernel_info){
for(uint32_t i = 0; i < 512; i++){
pm_reserve(i * 4096);
}
}
void map(void *vaddr, void *paddr, uint32_t flags){
@ -15,6 +61,37 @@ void map(void *vaddr, void *paddr, uint32_t flags){
uint32_t *pd = (uint32_t*)0xfffff000;
if(!pd[pd_index]){
pd[pd_index] = pm_alloc();
pd[pd_index] = pm_alloc() | 1;
}
uint32_t *pt = (uint32_t *)(0xffc00000 + (pd_index * 0x400));
pt[pt_index] = (uint32_t)paddr | flags;
asm volatile("invlpg (%0) " : : "b"(vaddr) : "memory");
}
void unmap(void *vaddr){
uint32_t pd_index = (uint32_t)vaddr >> 22;
uint32_t pt_index = (uint32_t)vaddr >> 12 & 0x3ff;
uint32_t *pd = (uint32_t *)0xfffff000;
if(!pd[pd_index]){
return;
}
uint32_t *pt = (uint32_t *)(0xffc00000 + (pd_index * 0x400));
uint32_t paddr = pt[pt_index];
paddr ^= (paddr & 0xfff);
pm_free(paddr);
asm volatile("invlpg (%0) " : : "b"(vaddr) : "memory");
}
void map_4mb(void *vaddr, void *paddr, uint32_t flags){
//just wanted to get the prototype out
}
uint32_t get_paddr(void *addr){
uint32_t vaddr = (uint32_t)addr;
uint32_t pd_index = vaddr >> 22;
uint32_t pt_index = vaddr >> 12 & 0x3ff;
uint32_t *pd = (uint32_t *)0xfffff000;
if(!pd[pd_index]){
return 0;
}
uint32_t *pt = (uint32_t *)(0xffc00000 + (0x400 * pd_index));
return pt[pt_index];
}

View File

@ -57,6 +57,15 @@ void printf(char *string, ...){
itoa(num, numb, 10);
debug_puts(numb);
continue;
case 'x':
// debug_puts(":3");
string += 2;
int32_t hex = va_arg(vars, int32_t);
char hnumb[12];
itoa(hex, hnumb, 16);
strpad(hnumb, '0', 8);
debug_puts(hnumb);
continue;
}
}
outb(com_ports[0], *string);

View File

@ -1,6 +1,8 @@
#include "memory.h"
#pragma once
// #include "memory.h"
#include "../drivers/serial.h"
typedef struct kernel_info{
memory_map_entry *mmap_ptr;
void *mmap_ptr;
uint16_t mmap_entry_count;
uint8_t padding;
uint8_t pixel_depth;

View File

@ -1,5 +1,6 @@
#pragma once
#include <stdint.h>
#include "kstdlib.h"
#define PD_PRESENT 0x0001
#define PD_USER_RW 0x0002