Mercurial > selfmade-os
comparison 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 |
comparison
equal
deleted
inserted
replaced
0:99db6262c157 | 1:42ba76f77035 |
---|---|
1 #include "mem.h" | |
2 #include "io.h" | |
3 | |
4 | |
5 void | |
6 initmem(unsigned int memstart) | |
7 { | |
8 unsigned int* p; | |
9 int step = 2*1024*1024; | |
10 unsigned int* zero = 0x0; | |
11 | |
12 | |
13 minmem = (unsigned char*)memstart; | |
14 | |
15 /* disable cache */ | |
16 __asm__ __volatile__ ( | |
17 "movl %cr0, %eax\n\t" | |
18 "movl $0x60000000, %ebx\n\t" | |
19 "orl %ebx, %eax\n\t" | |
20 "movl %eax, %cr0" | |
21 ); | |
22 | |
23 /* test mem size */ | |
24 p = (unsigned int*) ((unsigned int)minmem/sizeof(char)); | |
25 do { | |
26 p += step; | |
27 *p = 0xf0f0f0f0; | |
28 } while (*p == 0xf0f0f0f0); | |
29 maxmem = (unsigned char*)p; | |
30 | |
31 /* test mem mirroring */ | |
32 p = 0; | |
33 *p = 0; | |
34 p++; | |
35 while (p < (unsigned int*)minmem) { | |
36 p = (unsigned int*) ((unsigned int)p * 2); | |
37 } | |
38 while (p < (unsigned int*)maxmem) { | |
39 *p = 0xf0f0f0f0; | |
40 if (*zero == 0xf0f0f0f0) { | |
41 maxmem = (unsigned char*)p; | |
42 break; | |
43 } | |
44 p = (unsigned int*) ((unsigned int)p * 2); | |
45 } | |
46 | |
47 /* enable cache again */ | |
48 __asm__ __volatile__ ( | |
49 "movl %cr0, %eax\n\t" | |
50 "movl $0x60000000, %ebx \n\t" | |
51 "negl %ebx\n\t" | |
52 "andl %ebx, %eax\n\t" | |
53 "movl %eax, %cr0" | |
54 ); | |
55 | |
56 /* min, max */ | |
57 printf("mem: start=%p end=%p size=%p\n", minmem, maxmem, (void*)(maxmem-minmem)); | |
58 | |
59 /* init heap */ | |
60 newp = minmem; | |
61 } | |
62 | |
63 | |
64 void* | |
65 malloc(unsigned int size) | |
66 { | |
67 if (newp + size > maxmem) { | |
68 printf("Fatal error: memory exhausted!\n"); | |
69 while (1) { | |
70 } | |
71 } | |
72 newp += size; | |
73 return newp - size; | |
74 } |