Mercurial > selfmade-os
view mem.c @ 1:42ba76f77035 default tip
the kernel with output and mem alloc
author | meillo@marmaro.de |
---|---|
date | Sun, 01 Nov 2009 23:50:51 +0100 |
parents | |
children |
line wrap: on
line source
#include "mem.h" #include "io.h" void initmem(unsigned int memstart) { unsigned int* p; int step = 2*1024*1024; unsigned int* zero = 0x0; minmem = (unsigned char*)memstart; /* disable cache */ __asm__ __volatile__ ( "movl %cr0, %eax\n\t" "movl $0x60000000, %ebx\n\t" "orl %ebx, %eax\n\t" "movl %eax, %cr0" ); /* test mem size */ p = (unsigned int*) ((unsigned int)minmem/sizeof(char)); do { p += step; *p = 0xf0f0f0f0; } while (*p == 0xf0f0f0f0); maxmem = (unsigned char*)p; /* test mem mirroring */ p = 0; *p = 0; p++; while (p < (unsigned int*)minmem) { p = (unsigned int*) ((unsigned int)p * 2); } while (p < (unsigned int*)maxmem) { *p = 0xf0f0f0f0; if (*zero == 0xf0f0f0f0) { maxmem = (unsigned char*)p; break; } p = (unsigned int*) ((unsigned int)p * 2); } /* enable cache again */ __asm__ __volatile__ ( "movl %cr0, %eax\n\t" "movl $0x60000000, %ebx \n\t" "negl %ebx\n\t" "andl %ebx, %eax\n\t" "movl %eax, %cr0" ); /* min, max */ printf("mem: start=%p end=%p size=%p\n", minmem, maxmem, (void*)(maxmem-minmem)); /* init heap */ newp = minmem; } void* malloc(unsigned int size) { if (newp + size > maxmem) { printf("Fatal error: memory exhausted!\n"); while (1) { } } newp += size; return newp - size; }