~pmdj/ubuntu/trusty/qemu/2.9+applesmc+fadtv3

« back to all changes in this revision

Viewing changes to roms/skiboot/core/test/run-mem_region_release_unused.c

  • Committer: Phil Dennis-Jordan
  • Date: 2017-07-21 08:03:43 UTC
  • mfrom: (1.1.1)
  • Revision ID: phil@philjordan.eu-20170721080343-2yr2vdj7713czahv
New upstream release 2.9.0.

Show diffs side-by-side

added added

removed removed

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