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

« back to all changes in this revision

Viewing changes to roms/skiboot/core/test/run-msg.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
#include <inttypes.h>
 
17
#include <stdbool.h>
 
18
#include <stddef.h>
 
19
#include <assert.h>
 
20
#include <errno.h>
 
21
#include <stdlib.h>
 
22
 
 
23
static bool zalloc_should_fail = false;
 
24
static int zalloc_should_fail_after = 0;
 
25
 
 
26
static void *zalloc(size_t size)
 
27
{
 
28
        if (zalloc_should_fail && zalloc_should_fail_after == 0) {
 
29
                errno = ENOMEM;
 
30
                return NULL;
 
31
        }
 
32
        if (zalloc_should_fail_after > 0)
 
33
                zalloc_should_fail_after--;
 
34
 
 
35
        return calloc(size, 1);
 
36
}
 
37
 
 
38
#include "../opal-msg.c"
 
39
#include <skiboot.h>
 
40
 
 
41
void lock(struct lock *l)
 
42
{
 
43
        assert(!l->lock_val);
 
44
        l->lock_val = 1;
 
45
}
 
46
 
 
47
void unlock(struct lock *l)
 
48
{
 
49
        assert(l->lock_val);
 
50
        l->lock_val = 0;
 
51
}
 
52
 
 
53
void opal_update_pending_evt(uint64_t evt_mask, uint64_t evt_values)
 
54
{
 
55
        (void)evt_mask;
 
56
        (void)evt_values;
 
57
}
 
58
 
 
59
static long magic = 8097883813087437089UL;
 
60
static void callback(void *data)
 
61
{
 
62
        assert(*(uint64_t *)data == magic);
 
63
}
 
64
 
 
65
static size_t list_count(struct list_head *list)
 
66
{
 
67
        size_t count = 0;
 
68
        struct opal_msg_entry *dummy;
 
69
 
 
70
        list_for_each(list, dummy, link)
 
71
                count++;
 
72
        return count;
 
73
}
 
74
 
 
75
int main(void)
 
76
{
 
77
        struct opal_msg_entry* entry;
 
78
        int free_size = OPAL_MAX_MSGS;
 
79
        int nfree = free_size;
 
80
        int npending = 0;
 
81
        int r;
 
82
        static struct opal_msg m;
 
83
        uint64_t *m_ptr = (uint64_t *)&m;
 
84
 
 
85
        zalloc_should_fail = true;
 
86
        zalloc_should_fail_after = 3;
 
87
        opal_init_msg();
 
88
 
 
89
        zalloc_should_fail = false;
 
90
        opal_init_msg();
 
91
 
 
92
        assert(list_count(&msg_pending_list) == npending);
 
93
        assert(list_count(&msg_free_list) == nfree);
 
94
 
 
95
        /* Callback. */
 
96
        r = opal_queue_msg(0, &magic, callback, (u64)0, (u64)1, (u64)2);
 
97
        assert(r == 0);
 
98
 
 
99
        assert(list_count(&msg_pending_list) == ++npending);
 
100
        assert(list_count(&msg_free_list) == --nfree);
 
101
 
 
102
        r = opal_get_msg(m_ptr, sizeof(m));
 
103
        assert(r == 0);
 
104
 
 
105
        assert(m.params[0] == 0);
 
106
        assert(m.params[1] == 1);
 
107
        assert(m.params[2] == 2);
 
108
 
 
109
        assert(list_count(&msg_pending_list) == --npending);
 
110
        assert(list_count(&msg_free_list) == ++nfree);
 
111
 
 
112
        /* No params. */
 
113
        r = opal_queue_msg(0, NULL, NULL);
 
114
        assert(r == 0);
 
115
 
 
116
        assert(list_count(&msg_pending_list) == ++npending);
 
117
        assert(list_count(&msg_free_list) == --nfree);
 
118
 
 
119
        r = opal_get_msg(m_ptr, sizeof(m));
 
120
        assert(r == 0);
 
121
 
 
122
        assert(list_count(&msg_pending_list) == --npending);
 
123
        assert(list_count(&msg_free_list) == ++nfree);
 
124
 
 
125
        /* > 8 params (ARRAY_SIZE(entry->msg.params) */
 
126
        r = opal_queue_msg(0, NULL, NULL, 0, 1, 2, 3, 4, 5, 6, 7, 0xBADDA7A);
 
127
        assert(r == 0);
 
128
 
 
129
        assert(list_count(&msg_pending_list) == ++npending);
 
130
        assert(list_count(&msg_free_list) == --nfree);
 
131
 
 
132
        r = opal_get_msg(m_ptr, sizeof(m));
 
133
        assert(r == 0);
 
134
 
 
135
        assert(list_count(&msg_pending_list) == --npending);
 
136
        assert(list_count(&msg_free_list) == ++nfree);
 
137
 
 
138
        assert(m.params[0] == 0);
 
139
        assert(m.params[1] == 1);
 
140
        assert(m.params[2] == 2);
 
141
        assert(m.params[3] == 3);
 
142
        assert(m.params[4] == 4);
 
143
        assert(m.params[5] == 5);
 
144
        assert(m.params[6] == 6);
 
145
        assert(m.params[7] == 7);
 
146
 
 
147
        /* 8 params (ARRAY_SIZE(entry->msg.params) */
 
148
        r = opal_queue_msg(0, NULL, NULL, 0, 10, 20, 30, 40, 50, 60, 70);
 
149
        assert(r == 0);
 
150
 
 
151
        assert(list_count(&msg_pending_list) == ++npending);
 
152
        assert(list_count(&msg_free_list) == --nfree);
 
153
 
 
154
        r = opal_get_msg(m_ptr, sizeof(m));
 
155
        assert(r == 0);
 
156
 
 
157
        assert(list_count(&msg_pending_list) == --npending);
 
158
        assert(list_count(&msg_free_list) == ++nfree);
 
159
 
 
160
        assert(m.params[0] == 0);
 
161
        assert(m.params[1] == 10);
 
162
        assert(m.params[2] == 20);
 
163
        assert(m.params[3] == 30);
 
164
        assert(m.params[4] == 40);
 
165
        assert(m.params[5] == 50);
 
166
        assert(m.params[6] == 60);
 
167
        assert(m.params[7] == 70);
 
168
 
 
169
        /* Full list (no free nodes in pending). */
 
170
        while (nfree > 0) {
 
171
                r = opal_queue_msg(OPAL_MSG_ASYNC_COMP, NULL, NULL);
 
172
                assert(r == 0);
 
173
                assert(list_count(&msg_pending_list) == ++npending);
 
174
                assert(list_count(&msg_free_list) == --nfree);
 
175
        }
 
176
        assert(list_count(&msg_free_list) == 0);
 
177
        assert(nfree == 0);
 
178
        assert(npending == OPAL_MAX_MSGS);
 
179
 
 
180
        r = opal_queue_msg(OPAL_MSG_ASYNC_COMP, NULL, NULL);
 
181
        assert(r == 0);
 
182
 
 
183
        assert(list_count(&msg_pending_list) == OPAL_MAX_MSGS+1);
 
184
        assert(list_count(&msg_pending_list) == ++npending);
 
185
        assert(list_count(&msg_free_list) == nfree);
 
186
 
 
187
        /* Make zalloc fail to test error handling. */
 
188
        zalloc_should_fail = true;
 
189
        r = opal_queue_msg(OPAL_MSG_ASYNC_COMP, NULL, NULL);
 
190
        assert(r == OPAL_RESOURCE);
 
191
 
 
192
        assert(list_count(&msg_pending_list) == OPAL_MAX_MSGS+1);
 
193
        assert(list_count(&msg_pending_list) == npending);
 
194
        assert(list_count(&msg_free_list) == nfree);
 
195
 
 
196
        /* Empty list (no nodes). */
 
197
        while(!list_empty(&msg_pending_list)) {
 
198
                r = opal_get_msg(m_ptr, sizeof(m));
 
199
                assert(r == 0);
 
200
                npending--;
 
201
                nfree++;
 
202
        }
 
203
        assert(list_count(&msg_pending_list) == npending);
 
204
        assert(list_count(&msg_free_list) == nfree);
 
205
        assert(npending == 0);
 
206
        assert(nfree == OPAL_MAX_MSGS+1);
 
207
 
 
208
        r = opal_queue_msg(OPAL_MSG_ASYNC_COMP, NULL, NULL);
 
209
        assert(r == 0);
 
210
 
 
211
        assert(list_count(&msg_pending_list) == ++npending);
 
212
        assert(list_count(&msg_free_list) == --nfree);
 
213
 
 
214
        /* Request invalid size. */
 
215
        r = opal_get_msg(m_ptr, sizeof(m) - 1);
 
216
        assert(r == OPAL_PARAMETER);
 
217
 
 
218
        /* Pass null buffer. */
 
219
        r = opal_get_msg(NULL, sizeof(m));
 
220
        assert(r == OPAL_PARAMETER);
 
221
 
 
222
        /* Get msg when none are pending. */
 
223
        r = opal_get_msg(m_ptr, sizeof(m));
 
224
        assert(r == 0);
 
225
 
 
226
        r = opal_get_msg(m_ptr, sizeof(m));
 
227
        assert(r == OPAL_RESOURCE);
 
228
 
 
229
#define test_queue_num(type, val) \
 
230
        r = opal_queue_msg(0, NULL, NULL, \
 
231
                (type)val, (type)val, (type)val, (type)val, \
 
232
                (type)val, (type)val, (type)val, (type)val); \
 
233
        assert(r == 0); \
 
234
        opal_get_msg(m_ptr, sizeof(m)); \
 
235
        assert(r == OPAL_SUCCESS); \
 
236
        assert(m.params[0] == (type)val); \
 
237
        assert(m.params[1] == (type)val); \
 
238
        assert(m.params[2] == (type)val); \
 
239
        assert(m.params[3] == (type)val); \
 
240
        assert(m.params[4] == (type)val); \
 
241
        assert(m.params[5] == (type)val); \
 
242
        assert(m.params[6] == (type)val); \
 
243
        assert(m.params[7] == (type)val)
 
244
 
 
245
        /* Test types of various widths */
 
246
        test_queue_num(u64, -1);
 
247
        test_queue_num(s64, -1);
 
248
        test_queue_num(u32, -1);
 
249
        test_queue_num(s32, -1);
 
250
        test_queue_num(u16, -1);
 
251
        test_queue_num(s16, -1);
 
252
        test_queue_num(u8, -1);
 
253
        test_queue_num(s8, -1);
 
254
 
 
255
        /* Clean up the list to keep valgrind happy. */
 
256
        while(!list_empty(&msg_free_list)) {
 
257
                entry = list_pop(&msg_free_list, struct opal_msg_entry, link);
 
258
                assert(entry);
 
259
                free(entry);
 
260
        }
 
261
 
 
262
        while(!list_empty(&msg_pending_list)) {
 
263
                entry = list_pop(&msg_pending_list, struct opal_msg_entry, link);
 
264
                assert(entry);
 
265
                free(entry);
 
266
        }
 
267
 
 
268
        return 0;
 
269
}