2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
8 * This test relies on some particular internal functioning of kernel memory allocator!
9 * You'll have to change the code if you change something in kernel!
11 * Note also that this test requires working exec trap handling.
15 * We will not perform access tests because when we exit from trap handler,
16 * execution continues from the same location (re-triggering the trap again).
17 * Fixing this requires adjusting PC register in the context, which is not
18 * portable accross various CPUs and additionally will not currently work
19 * on all ports. We don't want to overcomplicate things so much.
21 #define NO_ACCESS_TESTS
23 #include <aros/kernel.h>
24 #include <exec/execbase.h>
25 #include <proto/exec.h>
26 #include <proto/kernel.h>
30 /* Include private kernel.resource stuff, for DumpState() */
32 #include "../../rom/kernel/mm_linear.h"
36 volatile static ULONG trap
;
38 static void TrapHandler(ULONG code
, void *ctx
)
43 static void TestRead(UBYTE
*location
)
45 #ifdef NO_ACCESS_TESTS
46 printf("Access tests disabled\n");
51 /* Reset trap indication */
54 printf("Test read from 0x%p... ", location
);
58 * We can't printf() from trap handler. Instead we just
59 * remember trap code and check it later here.
62 printf("Success, value is 0x%02X\n", val
);
64 printf("Hit trap 0x%08X\n", trap
);
70 * Print status of all pages in the specified MemHeader.
71 * '#' means allocated page, '.' means free page
73 static void DumpState(struct MemHeader
*mh
)
75 struct BlockHeader
*head
= (struct BlockHeader
*)mh
->mh_First
;
78 printf("Page map (%u total):\n", (unsigned)head
->size
);
80 for (p
= 0; p
< head
->size
; p
++)
81 printf(P_STATUS(head
->map
[p
]) ? "#" : ".");
88 #if defined(KrnStatMemory)
93 struct MemHeader
*TestArea
;
96 APTR region1
, region2
, region3
, region4
;
98 KernelBase
= OpenResource("kernel.resource");
101 printf("Failed to open kernel.resource!\n");
105 if (!KrnStatMemory(0, KMS_PageSize
, &page
, TAG_DONE
))
107 printf("MMU support is not implemented for this system!\n"
108 "kernel.resource memory allocator will not work!\n");
111 printf("System page size: %u (0x%08X)\n", (unsigned)page
, (unsigned)page
);
113 TestLength
= PAGES_NUM
* page
;
114 TestArea
= AllocMem(TestLength
, MEMF_ANY
);
115 printf("Allocated test region (%u bytes) at 0x%p\n", (unsigned)TestLength
, TestArea
);
118 printf("Failed to allocate test region!\n");
122 /* Install trap handler */
124 me
->tc_TrapCode
= TrapHandler
;
126 /* Compose a MemHeader */
127 TestArea
->mh_Node
.ln_Succ
= NULL
;
128 TestArea
->mh_Node
.ln_Type
= NT_MEMORY
;
129 TestArea
->mh_Node
.ln_Name
= "Kernel allocator test area";
130 TestArea
->mh_Node
.ln_Pri
= 127; /* This MemHeader must be the first in the list, otherwise KrnFreePages() will find a wrong one */
131 TestArea
->mh_Attributes
= MEMF_FAST
;
132 TestArea
->mh_Lower
= TestArea
;
133 TestArea
->mh_Upper
= TestArea
->mh_Lower
+ TestLength
- 1;
134 TestArea
->mh_First
= TestArea
->mh_Lower
+ MEMHEADER_TOTAL
;
135 TestArea
->mh_Free
= TestLength
- MEMHEADER_TOTAL
;
137 mc
= TestArea
->mh_First
;
139 mc
->mc_Bytes
= TestArea
->mh_Free
;
141 /* Give up the area to kernel allocator */
142 KrnInitMemory(TestArea
);
143 if (mc
->mc_Next
|| mc
->mc_Bytes
)
145 printf("KrnInitMemory() failed:\n"
147 " mc_Bytes is %lu\n",
148 mc
->mc_Next
, mc
->mc_Bytes
);
152 printf("Testing initial no-access protection...\n");
153 TestRead((UBYTE
*)TestArea
+ page
);
156 * Insert the area into system list.
157 * We do it manually because in future AddMemList() will call KrnInitMemory() itself.
160 Enqueue(&SysBase
->MemList
, &TestArea
->mh_Node
);
163 printf("Allocating region1 (two read-write pages)...\n");
164 region1
= KrnAllocPages(NULL
, 2 * page
, MEMF_FAST
);
165 printf("region1 at 0x%p\n", region1
);
168 printf("Freeing region1...\n");
169 KrnFreePages(region1
, 2 * page
);
173 printf("Allocating region1 (3 read-only pages)...\n");
174 region1
= KrnAllocPages(NULL
, 3 * page
, MEMF_FAST
);
175 printf("region1 at 0x%p\n", region1
);
178 printf("Allocating region2 (4 write-only ;-) pages)...\n");
179 region2
= KrnAllocPages(NULL
, 4 * page
, MEMF_FAST
);
180 printf("region2 at 0x%p\n", region2
);
183 printf("Attempting to allocate page with wrong flags...\n");
184 region3
= KrnAllocPages(NULL
, page
, MEMF_CHIP
|MEMF_FAST
);
185 printf("Region at 0x%p\n", region3
);
188 printf("WARNING!!! This should have been NULL!\n");
189 KrnFreePages(region3
, page
);
192 printf("Freeing region1...\n");
193 KrnFreePages(region1
, 3 * page
);
197 printf("Freeing region2...\n");
198 KrnFreePages(region2
, 4 * page
);
202 printf("Allocating region1 (one read-write page)...\n");
203 region1
= KrnAllocPages(NULL
, page
, MEMF_FAST
);
204 printf("region1 at 0x%p\n", region1
);
207 printf("Freeing region1...\n");
208 KrnFreePages(region1
, page
);
212 printf("Now we'll try to fragment the memory\n");
214 printf("Allocating region1 (2 pages)...\n");
215 region1
= KrnAllocPages(NULL
, 2 * page
, MEMF_FAST
);
216 printf("region1 at 0x%p\n", region1
);
219 printf("Allocating region2 (3 pages)...\n");
220 region2
= KrnAllocPages(NULL
, 3 * page
, MEMF_FAST
);
221 printf("region2 at 0x%p\n", region2
);
224 printf("Allocating region 3 (1 page)...\n");
225 region3
= KrnAllocPages(NULL
, page
, MEMF_FAST
);
226 printf("Region at 0x%p\n", region3
);
229 printf("Allocating region 4 (2 pages)...\n");
230 region4
= KrnAllocPages(NULL
, 2 * page
, MEMF_FAST
);
231 printf("region4 at 0x%p\n", region1
);
234 printf("Freeing region1...\n");
235 KrnFreePages(region1
, 2 * page
);
239 printf("Freeing region3...\n");
240 KrnFreePages(region3
, page
);
244 printf("Allocating region 3 (1 page) again...\n");
245 region3
= KrnAllocPages(NULL
, page
, MEMF_FAST
);
246 printf("Region at 0x%p\n", region3
);
249 printf("Freeing region2...\n");
250 KrnFreePages(region2
, 3 * page
);
254 printf("Freeing region3...\n");
255 KrnFreePages(region3
, page
);
259 printf("Freeing region4...\n");
260 KrnFreePages(region4
, 2 * page
);
265 if (TestArea
->mh_Node
.ln_Succ
)
268 Remove(&TestArea
->mh_Node
);
271 FreeMem(TestArea
, TestLength
);
274 printf("The test can't be built for this kernel.resource implementation\n");