2020-07-25 16:54:37 -05:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
*/
|
|
|
|
|
2020-07-22 19:35:23 -05:00
|
|
|
#include "cpu/arch_consts.h"
|
2020-07-22 19:26:55 -05:00
|
|
|
#include "cpu/halt.h"
|
|
|
|
#include "vga_err.h"
|
2020-07-22 19:35:23 -05:00
|
|
|
#include <grub/multiboot2.h>
|
2019-03-11 09:32:55 -05:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2020-07-25 16:54:37 -05:00
|
|
|
#define BMAP_LEN (NUM_FRAMES/8) //!< The size of the physical memory manager's bitmap
|
2020-07-22 19:26:55 -05:00
|
|
|
|
2020-07-25 16:54:37 -05:00
|
|
|
static char bmap[BMAP_LEN]; //!< Bitmap of allocated/non-present page frames
|
2019-03-11 09:32:55 -05:00
|
|
|
|
2020-07-25 16:54:37 -05:00
|
|
|
/**
|
|
|
|
* Get a bit in the bitmap
|
|
|
|
* \param index The bit to get
|
|
|
|
* \return the bit
|
|
|
|
*/
|
2019-03-11 09:32:55 -05:00
|
|
|
static char get_bmap_bit(int index) {
|
|
|
|
int byte=index/8;
|
|
|
|
int bit=index%8;
|
|
|
|
char entry=bmap[byte];
|
|
|
|
return (entry&(1<<bit))>0;
|
|
|
|
}
|
|
|
|
|
2020-07-25 16:54:37 -05:00
|
|
|
/**
|
|
|
|
* Set a bit in the heap bitmap
|
|
|
|
* \param index The bit to set
|
|
|
|
*/
|
2019-03-11 09:32:55 -05:00
|
|
|
static void set_bmap_bit(int index) {
|
|
|
|
int byte=index/8;
|
|
|
|
int bit=index%8;
|
|
|
|
bmap[byte]=bmap[byte]|(1<<bit);
|
|
|
|
}
|
|
|
|
|
2020-07-25 16:54:37 -05:00
|
|
|
/**
|
|
|
|
* Clear a bit in the heap bitmap
|
|
|
|
* \param index The bit to clear
|
|
|
|
*/
|
2019-03-11 09:32:55 -05:00
|
|
|
static void clear_bmap_bit(int index) {
|
|
|
|
int byte=index/8;
|
|
|
|
int bit=index%8;
|
|
|
|
bmap[byte]=bmap[byte]&(~(1<<bit));
|
|
|
|
}
|
|
|
|
|
2019-04-27 15:03:31 -05:00
|
|
|
void pmem_init(struct multiboot_boot_header_tag* tags) {
|
2020-07-22 19:26:55 -05:00
|
|
|
for (int i=0;i<BMAP_LEN;i++) {
|
2019-03-11 09:32:55 -05:00
|
|
|
bmap[i]=0xFF;
|
|
|
|
}
|
2019-04-27 15:03:31 -05:00
|
|
|
char found_mmap=0;
|
|
|
|
struct multiboot_tag* tag=(struct multiboot_tag*)(tags+1);
|
|
|
|
while (tag->type!=0) {
|
|
|
|
switch (tag->type) {
|
|
|
|
case MULTIBOOT_TAG_TYPE_MMAP: {
|
|
|
|
found_mmap=1;
|
|
|
|
struct multiboot_mmap_entry* orig_ptr=(struct multiboot_mmap_entry*)(((char*)tag)+16);
|
|
|
|
for (struct multiboot_mmap_entry* ptr=orig_ptr;(char*)ptr<((char*)orig_ptr)+tag->size;ptr++) {
|
|
|
|
if (ptr->type!=MULTIBOOT_MEMORY_AVAILABLE) continue;
|
2020-07-23 11:50:23 -05:00
|
|
|
size_t start=ptr->addr;
|
2019-04-27 15:03:31 -05:00
|
|
|
if (start<0x100000) continue;
|
2020-07-23 11:50:23 -05:00
|
|
|
size_t end=start+ptr->len-1;
|
2020-07-22 19:26:55 -05:00
|
|
|
if (start&(FRAME_SZ-1)) {
|
|
|
|
start+=FRAME_SZ;
|
2019-04-27 15:03:31 -05:00
|
|
|
}
|
2020-07-22 19:26:55 -05:00
|
|
|
start=start>>FRAME_NO_OFFSET;
|
|
|
|
end=end>>FRAME_NO_OFFSET;
|
2020-07-23 11:50:23 -05:00
|
|
|
for (size_t i=start;i<end;i++) {
|
2019-04-27 15:03:31 -05:00
|
|
|
clear_bmap_bit(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-03-11 09:32:55 -05:00
|
|
|
}
|
2019-04-27 15:03:31 -05:00
|
|
|
tag=(struct multiboot_tag*)((char*)tag+((tag->size+7)&0xFFFFFFF8));
|
|
|
|
}
|
|
|
|
if (!found_mmap) {
|
|
|
|
vga_write_string("[PANIC] No memory map supplied by bootloader!");
|
|
|
|
halt();
|
2019-03-11 09:32:55 -05:00
|
|
|
}
|
2020-07-23 11:50:23 -05:00
|
|
|
for (size_t i=0;i<NUM_KERN_FRAMES;i++) {
|
2019-03-11 09:32:55 -05:00
|
|
|
set_bmap_bit(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void* pmem_alloc(int num_pages) {
|
2020-07-23 11:50:23 -05:00
|
|
|
size_t bmap_index;
|
|
|
|
size_t remaining_blks;
|
|
|
|
for(size_t i=0;i<131072;i++) {
|
2019-07-01 15:30:00 -05:00
|
|
|
char got_0=0;
|
|
|
|
remaining_blks=num_pages;
|
2020-07-23 11:50:23 -05:00
|
|
|
size_t old_j;
|
|
|
|
for (size_t j=i*8;;j++) {
|
2019-07-01 15:30:00 -05:00
|
|
|
char bit=get_bmap_bit(j);
|
|
|
|
if (got_0) {
|
|
|
|
if (bit) {
|
|
|
|
if (remaining_blks==0) {
|
|
|
|
bmap_index=old_j;
|
2019-03-11 09:32:55 -05:00
|
|
|
break;
|
|
|
|
} else {
|
2019-07-01 15:30:00 -05:00
|
|
|
i+=j/8;
|
|
|
|
i--;
|
|
|
|
break;
|
2019-03-11 09:32:55 -05:00
|
|
|
}
|
|
|
|
} else {
|
2019-07-01 15:30:00 -05:00
|
|
|
remaining_blks--;
|
2019-03-11 09:32:55 -05:00
|
|
|
}
|
2019-07-01 15:30:00 -05:00
|
|
|
} else {
|
|
|
|
if (!bit) {
|
|
|
|
got_0=1;
|
|
|
|
old_j=j;
|
|
|
|
remaining_blks--;
|
2019-03-11 09:32:55 -05:00
|
|
|
}
|
|
|
|
}
|
2019-07-01 15:30:00 -05:00
|
|
|
if (remaining_blks==0) {
|
|
|
|
bmap_index=old_j;
|
|
|
|
break;
|
|
|
|
}
|
2019-03-11 09:32:55 -05:00
|
|
|
}
|
|
|
|
if (remaining_blks==0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (remaining_blks!=0) {
|
2021-02-28 13:46:33 -06:00
|
|
|
serial_printf("Out of memory! Halting!\n");
|
|
|
|
halt();
|
2019-03-11 09:32:55 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (int i=0;i<num_pages;i++) {
|
|
|
|
set_bmap_bit(bmap_index+i);
|
|
|
|
}
|
|
|
|
void* addr=(void*)(bmap_index<<12);
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
2020-07-27 19:02:09 -05:00
|
|
|
void pmem_free(void* start,int num_pages) {
|
|
|
|
int start_page=(size_t)start>>FRAME_NO_OFFSET;
|
2019-03-11 09:32:55 -05:00
|
|
|
for (int i=start_page;i<num_pages;i++) {
|
|
|
|
set_bmap_bit(i);
|
|
|
|
}
|
|
|
|
}
|