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
__malloc(size_t size,const char * location)25 static void *__malloc(size_t size, const char *location __attribute__((unused)))
26 {
27 return malloc(size);
28 }
29
__realloc(void * ptr,size_t size,const char * location)30 static void *__realloc(void *ptr, size_t size, const char *location __attribute__((unused)))
31 {
32 return realloc(ptr, size);
33 }
34
__zalloc(size_t size,const char * location)35 static void *__zalloc(size_t size, const char *location __attribute__((unused)))
36 {
37 return calloc(size, 1);
38 }
39
__free(void * p,const char * location)40 static inline void __free(void *p, const char *location __attribute__((unused)))
41 {
42 return free(p);
43 }
44
45 #include <skiboot.h>
46
47 /* We need mem_region to accept __location__ */
48 #define is_rodata(p) true
49 #include "../mem_region.c"
50
51 /* But we need device tree to make copies of names. */
52 #undef is_rodata
53 #define is_rodata(p) false
54
55 #include "../device.c"
56 #include <assert.h>
57 #include <stdio.h>
58
59 enum proc_chip_quirks proc_chip_quirks;
60
lock_caller(struct lock * l,const char * caller)61 void lock_caller(struct lock *l, const char *caller)
62 {
63 (void)caller;
64 l->lock_val++;
65 }
66
unlock(struct lock * l)67 void unlock(struct lock *l)
68 {
69 l->lock_val--;
70 }
71
lock_held_by_me(struct lock * l)72 bool lock_held_by_me(struct lock *l)
73 {
74 return l->lock_val;
75 }
76
77 #define TEST_HEAP_ORDER 12
78 #define TEST_HEAP_SIZE (1ULL << TEST_HEAP_ORDER)
79
add_mem_node(uint64_t start,uint64_t len)80 static void add_mem_node(uint64_t start, uint64_t len)
81 {
82 struct dt_node *mem;
83 u64 reg[2];
84 char *name;
85
86 name = (char*)malloc(sizeof("memory@") + STR_MAX_CHARS(reg[0]));
87 assert(name);
88
89 /* reg contains start and length */
90 reg[0] = cpu_to_be64(start);
91 reg[1] = cpu_to_be64(len);
92
93 sprintf(name, "memory@%llx", (long long)start);
94
95 mem = dt_new(dt_root, name);
96 dt_add_property_string(mem, "device_type", "memory");
97 dt_add_property(mem, "reg", reg, sizeof(reg));
98 free(name);
99 }
100
add_chip_dev_associativity(struct dt_node * dev)101 void add_chip_dev_associativity(struct dt_node *dev __attribute__((unused)))
102 {
103 }
104
main(void)105 int main(void)
106 {
107 uint64_t i;
108 struct mem_region *r, *other = NULL;
109 void *other_mem;
110 const char *last;
111
112 /* Use malloc for the heap, so valgrind can find issues. */
113 skiboot_heap.start = (unsigned long)malloc(TEST_HEAP_SIZE);
114 skiboot_heap.len = TEST_HEAP_SIZE;
115 skiboot_os_reserve.len = 0;
116
117 dt_root = dt_new_root("");
118 dt_add_property_cells(dt_root, "#address-cells", 2);
119 dt_add_property_cells(dt_root, "#size-cells", 2);
120
121 other_mem = malloc(1024*1024);
122 add_mem_node((unsigned long)other_mem, 1024*1024);
123
124 /* Now convert. */
125 mem_region_init();
126
127 /* Find our node to allocate from */
128 list_for_each(®ions, r, list) {
129 if (region_start(r) == other_mem)
130 other = r;
131 }
132 /* This could happen if skiboot addresses clashed with our alloc. */
133 assert(other);
134 assert(mem_check(other));
135
136 /* Allocate 1k from other region. */
137 lock(&other->free_list_lock);
138 mem_alloc(other, 1024, 1, "1k");
139 unlock(&other->free_list_lock);
140
141 mem_region_release_unused();
142
143 assert(mem_check(&skiboot_heap));
144
145 /* Now we expect it to be split. */
146 i = 0;
147 list_for_each(®ions, r, list) {
148 assert(mem_check(r));
149 i++;
150 if (r == &skiboot_os_reserve)
151 continue;
152 if (r == &skiboot_code_and_text)
153 continue;
154 if (r == &skiboot_heap)
155 continue;
156 if (r == &skiboot_after_heap)
157 continue;
158 if (r == &skiboot_cpu_stacks)
159 continue;
160 if (r == other) {
161 assert(r->type == REGION_MEMORY);
162 assert(r->len < 1024 * 1024);
163 } else {
164 assert(r->type == REGION_OS);
165 assert(r->start == other->start + other->len);
166 assert(r->start + r->len == other->start + 1024*1024);
167 }
168 }
169 assert(i == 7);
170
171 last = NULL;
172 list_for_each(®ions, r, list) {
173 if (last != r->name &&
174 strncmp(r->name, NODE_REGION_PREFIX,
175 strlen(NODE_REGION_PREFIX)) == 0) {
176 /* It's safe to cast away const as this is
177 * only going to happen in test code */
178 free((void*)r->name);
179 break;
180 }
181 last = r->name;
182 }
183
184 dt_free(dt_root);
185 free((void *)(long)skiboot_heap.start);
186 free(other_mem);
187 return 0;
188 }
189