Also hier mal die Rückgabewerte der pmm_alloc() für die Taskstruktur (struct task* task = pmm_alloc()), bei Task1: 40000 und bei Task2: fffdf000
Da der 2. Wert sehr kaputt erscheint hier mal meine pmm.c:
// Source File for Headerdatei pmm.h
/*
* Der Einfachheit halber deklarieren wir die maximal benoetige Bitmapgroesse
* statisch (Wir brauchen 4 GB / 4 kB = 1M Bits; 1M Bits sind 1M/32 = 32k
* Eintraege fuer das Array)
*
* Willkuerliche Festlegung: 1 = Speicher frei, 0 = Speicher belegt
*/
#include "console.h"
#include <stdint.h>
#include <stddef.h>
//#include "stdint.h"
#include "pmm.h"
#define BITMAP_SIZE 32768
static unsigned long bitmap[BITMAP_SIZE];
extern const void kernel_start;
extern const void kernel_end;
unsigned int indexArrayValNotNull(unsigned long array[]);
unsigned int indexLowestBit(int x);
void pmm_mark_all(void);
void pmm_mark_used(void* addr)
{
uintptr_t page_nummer;
uintptr_t bitnummer;
uintptr_t array_index;
page_nummer = (uintptr_t) addr / 4096;
bitnummer = page_nummer % 32;
array_index = (page_nummer - bitnummer) / 32;
//Bit loeschen weil Speicher belegt: Bit = 0
// x &= FLAG
bitmap[array_index] &= bitnummer;
}
void pmm_free(void* addr)
{
uintptr_t page_nummer;
uintptr_t bitnummer;
uintptr_t array_index;
page_nummer = (uintptr_t) addr / 4096;
bitnummer = page_nummer % 32;
array_index = (page_nummer - bitnummer) / 32;
// Bit setzen weil Speicher frei: Bit = 1
// x |= FLAG
bitmap[array_index] |= bitnummer;
}
void* pmm_alloc(void)
{
// TODO: Exceptions einbauen
unsigned int index;
unsigned int bit;
unsigned int page_nummer;
unsigned int adresse;
index = indexArrayValNotNull(bitmap);
bit = indexLowestBit(bitmap[index]);
page_nummer = index * 32 + bit; // 32 pages pro eintrag in "bitmap"
adresse = page_nummer * 4096; // jede page ist 4096 groß
// Pmm mark used
pmm_mark_used((void*) adresse);
return (void*) adresse;
}
void pmm_init(struct multiboot_info *mb_info)
{
struct multiboot_mmap* mmap = mb_info->mbs_mmap_addr; // Pointer zur Memory Map wird in Pointer mmmap gelegt
struct multiboot_mmap* mmap_end = (void*) ((uintptr_t) mb_info->mbs_mmap_addr + mb_info->mbs_mmap_length);
// Alles als belegt markieren
pmm_mark_all();
while (mmap < mmap_end) {
if (mmap->type == 1) {
// Der Speicherbereich ist frei, entsprechend markieren
uintptr_t addr = mmap->base; // Groesse: 1byte = 8bit = 0x1000 = addre zeigt auf adresse *addr auf inhalt
uintptr_t end_addr = addr + mmap->length; // 8bit
while (addr < end_addr) {
pmm_free((void*) addr);
addr += 0x1000;
}
}
mmap++;
}
uintptr_t kernel_addr = (uintptr_t) &kernel_start;
uintptr_t kernel_end_addr = (uintptr_t) &kernel_end;
while (kernel_addr < kernel_end_addr) {
pmm_mark_used((void*) kernel_addr);
kernel_addr += 0x1000;
}
}
unsigned int indexArrayValNotNull(unsigned long array[]) {
int i;
for (i=0; i < sizeof(array); i++) { // Array Laenge ?!
if (array[i] != 0) { return i; }
}
/* Fehlercode wenn nur 0en */
return (unsigned) -1;
}
unsigned int indexLowestBit(int x) {
int i;
for (i=0; i < 32; i++) {
if ((x & (1 << i)) != 0) { return i; }
}
/* Fehlercode wenn kein Bit gesetzt */
return (unsigned) -1;
}
void pmm_mark_all(void) {
int i;
int x;
for(i=0; i < BITMAP_SIZE; i++) {
for(x=0; x < 32; x++) {
//Bit loeschen weil Speicher belegt: Bit = 0
// x &= FLAG
bitmap[i] &= x;
}
}
}
void pmm_check(void* addr) {
uintptr_t page_nummer;
uintptr_t bitnummer;
uintptr_t array_index;
page_nummer = (uintptr_t) addr / 4096;
bitnummer = page_nummer % 32;
array_index = (page_nummer - bitnummer) / 32;
if(bitmap[array_index] & (1 << bitnummer)) { kputs("-1-"); }
else { kputs("-0-"); }
}