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