1 /* Copyright 2013-2014 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
25 /* Use these before we undefine them below. */
real_malloc(size_t size)26 static inline void *real_malloc(size_t size)
27 {
28 return malloc(size);
29 }
30
real_free(void * p)31 static inline void real_free(void *p)
32 {
33 return free(p);
34 }
35
36 #include "../malloc.c"
37
38 #include <skiboot.h>
39 /* We need mem_region to accept __location__ */
40 #define is_rodata(p) true
41 #include "../mem_region.c"
42
43 /* But we need device tree to make copies of names. */
44 #undef is_rodata
45 #define is_rodata(p) false
46
skiboot_strdup(const char * str)47 static inline char *skiboot_strdup(const char *str)
48 {
49 char *ret = __malloc(strlen(str) + 1, "");
50 return memcpy(ret, str, strlen(str) + 1);
51 }
52 #undef strdup
53 #define strdup skiboot_strdup
54
55 #include "../device.c"
56
57 #include <skiboot.h>
58
59 #include <assert.h>
60 #include <stdio.h>
61
62 enum proc_chip_quirks proc_chip_quirks;
63
lock_caller(struct lock * l,const char * caller)64 void lock_caller(struct lock *l, const char *caller)
65 {
66 (void)caller;
67 assert(!l->lock_val);
68 l->lock_val = 1;
69 }
70
unlock(struct lock * l)71 void unlock(struct lock *l)
72 {
73 assert(l->lock_val);
74 l->lock_val = 0;
75 }
76
lock_held_by_me(struct lock * l)77 bool lock_held_by_me(struct lock *l)
78 {
79 return l->lock_val;
80 }
81
82 /* We actually need a lot of room for the bitmaps! */
83 #define TEST_HEAP_ORDER 27
84 #define TEST_HEAP_SIZE (1ULL << TEST_HEAP_ORDER)
85
add_mem_node(uint64_t start,uint64_t len)86 static void add_mem_node(uint64_t start, uint64_t len)
87 {
88 struct dt_node *mem;
89 u64 reg[2];
90 char *name= (char*)malloc(sizeof("memory@") + STR_MAX_CHARS(reg[0]));
91
92 assert(name);
93
94 /* reg contains start and length */
95 reg[0] = cpu_to_be64(start);
96 reg[1] = cpu_to_be64(len);
97
98 sprintf(name, "memory@%llx", (unsigned long long)start);
99
100 mem = dt_new(dt_root, name);
101 assert(mem);
102 dt_add_property_string(mem, "device_type", "memory");
103 dt_add_property(mem, "reg", reg, sizeof(reg));
104 free(name);
105 }
106
add_chip_dev_associativity(struct dt_node * dev)107 void add_chip_dev_associativity(struct dt_node *dev __attribute__((unused)))
108 {
109 }
110
main(void)111 int main(void)
112 {
113 uint64_t end;
114 int builtins;
115 struct mem_region *r;
116 char *heap = real_malloc(TEST_HEAP_SIZE);
117
118 /* Use malloc for the heap, so valgrind can find issues. */
119 skiboot_heap.start = (unsigned long)heap;
120 skiboot_heap.len = TEST_HEAP_SIZE;
121 skiboot_os_reserve.len = 16384;
122
123 dt_root = dt_new_root("");
124 dt_add_property_cells(dt_root, "#address-cells", 2);
125 dt_add_property_cells(dt_root, "#size-cells", 2);
126
127 /* Make sure we overlap the heap, at least. */
128 add_mem_node(0, (uint64_t)(heap + 0x100000000ULL));
129 add_mem_node((uint64_t)heap+0x100000000ULL , 0x100000000ULL);
130 end = (uint64_t)(heap+ 0x100000000ULL + 0x100000000ULL);
131
132 /* Now convert. */
133 mem_region_init();
134 mem_dump_allocs();
135 assert(mem_check(&skiboot_heap));
136
137 builtins = 0;
138 list_for_each(®ions, r, list) {
139 /* Regions must not overlap. */
140 struct mem_region *r2, *pre = NULL, *post = NULL;
141 list_for_each(®ions, r2, list) {
142 if (r == r2)
143 continue;
144 assert(!overlaps(r, r2));
145 }
146
147 /* But should have exact neighbours. */
148 list_for_each(®ions, r2, list) {
149 if (r == r2)
150 continue;
151 if (r2->start == r->start + r->len)
152 post = r2;
153 if (r2->start + r2->len == r->start)
154 pre = r2;
155 }
156 assert(r->start == 0 || pre);
157 assert(r->start + r->len == end || post);
158
159 if (r == &skiboot_code_and_text ||
160 r == &skiboot_heap ||
161 r == &skiboot_after_heap ||
162 r == &skiboot_cpu_stacks ||
163 r == &skiboot_os_reserve)
164 builtins++;
165 else
166 assert(r->type == REGION_MEMORY);
167 assert(mem_check(r));
168 }
169 assert(builtins == 5);
170
171 dt_free(dt_root);
172
173 while ((r = list_pop(®ions, struct mem_region, list)) != NULL) {
174 if (r != &skiboot_code_and_text &&
175 r != &skiboot_heap &&
176 r != &skiboot_after_heap &&
177 r != &skiboot_os_reserve &&
178 r != &skiboot_cpu_stacks) {
179 free(r);
180 }
181 assert(mem_check(&skiboot_heap));
182 }
183 assert(skiboot_heap.free_list_lock.lock_val == 0);
184 real_free(heap);
185 return 0;
186 }
187