selfmade-os

diff mem.c @ 1:42ba76f77035

the kernel with output and mem alloc
author meillo@marmaro.de
date Sun, 01 Nov 2009 23:50:51 +0100
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mem.c	Sun Nov 01 23:50:51 2009 +0100
     1.3 @@ -0,0 +1,74 @@
     1.4 +#include "mem.h"
     1.5 +#include "io.h"
     1.6 +
     1.7 +
     1.8 +void
     1.9 +initmem(unsigned int memstart)
    1.10 +{
    1.11 +	unsigned int* p;
    1.12 +	int step = 2*1024*1024;
    1.13 +	unsigned int* zero = 0x0;
    1.14 +
    1.15 +
    1.16 +	minmem = (unsigned char*)memstart;
    1.17 +
    1.18 +	/* disable cache */
    1.19 +	__asm__ __volatile__ (
    1.20 +		"movl %cr0, %eax\n\t"
    1.21 +		"movl $0x60000000, %ebx\n\t"
    1.22 +		"orl %ebx, %eax\n\t"
    1.23 +		"movl %eax, %cr0"
    1.24 +	);
    1.25 +
    1.26 +	/* test mem size */
    1.27 +	p = (unsigned int*) ((unsigned int)minmem/sizeof(char));
    1.28 +	do {
    1.29 +		p += step;
    1.30 +		*p = 0xf0f0f0f0;
    1.31 +	} while (*p == 0xf0f0f0f0);
    1.32 +	maxmem = (unsigned char*)p;
    1.33 +
    1.34 +	/* test mem mirroring */
    1.35 +	p = 0;
    1.36 +	*p = 0;
    1.37 +	p++;
    1.38 +	while (p < (unsigned int*)minmem) {
    1.39 +		p = (unsigned int*) ((unsigned int)p * 2);
    1.40 +	}
    1.41 +	while (p < (unsigned int*)maxmem) {
    1.42 +		*p = 0xf0f0f0f0;
    1.43 +		if (*zero == 0xf0f0f0f0) {
    1.44 +			maxmem = (unsigned char*)p;
    1.45 +			break;
    1.46 +		}
    1.47 +		p = (unsigned int*) ((unsigned int)p * 2);
    1.48 +	}
    1.49 +
    1.50 +	/* enable cache again */
    1.51 +	__asm__ __volatile__ (
    1.52 +		"movl %cr0, %eax\n\t"
    1.53 +		"movl $0x60000000, %ebx \n\t"
    1.54 +		"negl %ebx\n\t"
    1.55 +		"andl %ebx, %eax\n\t"
    1.56 +		"movl %eax, %cr0"
    1.57 +	);
    1.58 +
    1.59 +	/* min, max */
    1.60 +	printf("mem: start=%p end=%p size=%p\n", minmem, maxmem, (void*)(maxmem-minmem));
    1.61 +
    1.62 +	/* init heap */
    1.63 +	newp = minmem;
    1.64 +}
    1.65 +
    1.66 +
    1.67 +void*
    1.68 +malloc(unsigned int size)
    1.69 +{
    1.70 +	if (newp + size > maxmem) {
    1.71 +		printf("Fatal error: memory exhausted!\n");
    1.72 +		while (1) {
    1.73 +		}
    1.74 +	}
    1.75 +	newp += size;
    1.76 +	return newp - size;
    1.77 +}