1 /* Copyright 2013-2015 IBM Corp.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * 	http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <config.h>
18 
19 #define BITS_PER_LONG (sizeof(long) * 8)
20 
21 #include "dummy-cpu.h"
22 
23 #include <stdlib.h>
24 #include <string.h>
25 
26 /* Use these before we override definitions below. */
real_malloc(size_t size)27 static void *real_malloc(size_t size)
28 {
29 	return malloc(size);
30 }
31 
real_free(void * p)32 static void real_free(void *p)
33 {
34 	return free(p);
35 }
36 
37 #undef malloc
38 #undef free
39 
40 #include <skiboot.h>
41 
42 #define is_rodata(p) true
43 
44 #include "../mem_region.c"
45 #include "../malloc.c"
46 #include "../device.c"
47 
48 #include <assert.h>
49 #include <stdio.h>
50 
51 enum proc_chip_quirks proc_chip_quirks;
52 
lock_caller(struct lock * l,const char * caller)53 void lock_caller(struct lock *l, const char *caller)
54 {
55 	(void)caller;
56 	assert(!l->lock_val);
57 	l->lock_val++;
58 }
59 
unlock(struct lock * l)60 void unlock(struct lock *l)
61 {
62 	assert(l->lock_val);
63 	l->lock_val--;
64 }
65 
lock_held_by_me(struct lock * l)66 bool lock_held_by_me(struct lock *l)
67 {
68 	return l->lock_val;
69 }
70 
71 
72 #define TEST_HEAP_ORDER 12
73 #define TEST_HEAP_SIZE (1ULL << TEST_HEAP_ORDER)
74 
main(void)75 int main(void)
76 {
77 	struct mem_region *r;
78 	char *test_heap;
79 
80 	/* Use malloc for the heap, so valgrind can find issues. */
81 	test_heap = real_malloc(TEST_HEAP_SIZE);
82 	skiboot_heap.start = (unsigned long)test_heap;
83 	skiboot_heap.len = TEST_HEAP_SIZE;
84 
85 	lock(&mem_region_lock);
86 
87 	/* empty regions */
88 	r = mem_region_next(NULL);
89 	assert(!r);
90 
91 	r = new_region("test.1", 0x1000, 0x1000, NULL, REGION_RESERVED);
92 	assert(add_region(r));
93 	r = new_region("test.2", 0x2000, 0x1000, NULL, REGION_RESERVED);
94 	assert(add_region(r));
95 	mem_regions_finalised = true;
96 
97 	r = mem_region_next(NULL);
98 	assert(r);
99 	assert(r->start == 0x2000);
100 	assert(r->len == 0x1000);
101 	assert(r->type == REGION_RESERVED);
102 
103 	r = mem_region_next(r);
104 	assert(r);
105 	assert(r->start == 0x1000);
106 	assert(r->len == 0x1000);
107 	assert(r->type == REGION_RESERVED);
108 
109 	r = mem_region_next(r);
110 	assert(!r);
111 
112 	unlock(&mem_region_lock);
113 	real_free(test_heap);
114 
115 	return 0;
116 }
117