aboutsummaryrefslogtreecommitdiff
path: root/tests/tcg/i386/system/memory.c
blob: a7a0a8e9784c89a9a33f6b33eba4087a3cb3d2d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * Memory Test
 *
 * This is intended to test the softmmu code and ensure we properly
 * behave across normal and unaligned accesses across several pages.
 * We are not replicating memory tests for stuck bits and other
 * hardware level failures but looking for issues with different size
 * accesses when:

 *
 */

#include <inttypes.h>
#include <minilib.h>

#define TEST_SIZE (4096 * 4)  /* 4 pages */

static uint8_t test_data[TEST_SIZE];

static void pdot(int count)
{
    if (count % 128 == 0) {
        ml_printf(".");
    }
}


/*
 * Fill the data with ascending value bytes. As x86 is a LE machine we
 * write in ascending order and then read and high byte should either
 * be zero or higher than the lower bytes.
 */

static void init_test_data_u8(void)
{
    uint8_t count = 0, *ptr = &test_data[0];
    int i;

    ml_printf("Filling test area with u8:");
    for (i = 0; i < TEST_SIZE; i++) {
        *ptr++ = count++;
        pdot(i);
    }
    ml_printf("done\n");
}

static void init_test_data_u16(int offset)
{
    uint8_t count = 0;
    uint16_t word, *ptr = (uint16_t *) &test_data[0];
    const int max = (TEST_SIZE - offset) / sizeof(word);
    int i;

    ml_printf("Filling test area with u16 (offset %d):", offset);

    /* Leading zeros */
    for (i = 0; i < offset; i++) {
        *ptr = 0;
    }

    ptr = (uint16_t *) &test_data[offset];
    for (i = 0; i < max; i++) {
        uint8_t high, low;
        low = count++;
        high = count++;
        word = (high << 8) | low;
        *ptr++ = word;
        pdot(i);
    }
    ml_printf("done\n");
}

static void init_test_data_u32(int offset)
{
    uint8_t count = 0;
    uint32_t word, *ptr = (uint32_t *) &test_data[0];
    const int max = (TEST_SIZE - offset) / sizeof(word);
    int i;

    ml_printf("Filling test area with u32 (offset %d):", offset);

    /* Leading zeros */
    for (i = 0; i < offset; i++) {
        *ptr = 0;
    }

    ptr = (uint32_t *) &test_data[offset];
    for (i = 0; i < max; i++) {
        uint8_t b1, b2, b3, b4;
        b4 = count++;
        b3 = count++;
        b2 = count++;
        b1 = count++;
        word = (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
        *ptr++ = word;
        pdot(i);
    }
    ml_printf("done\n");
}


static int read_test_data_u16(int offset)
{
    uint16_t word, *ptr = (uint16_t *)&test_data[offset];
    int i;
    const int max = (TEST_SIZE - offset) / sizeof(word);

    ml_printf("Reading u16 from %#lx (offset %d):", ptr, offset);

    for (i = 0; i < max; i++) {
        uint8_t high, low;
        word = *ptr++;
        high = (word >> 8) & 0xff;
        low = word & 0xff;
        if (high < low && high != 0) {
            ml_printf("Error %d < %d\n", high, low);
            return 1;
        } else {
            pdot(i);
        }

    }
    ml_printf("done\n");
    return 0;
}

static int read_test_data_u32(int offset)
{
    uint32_t word, *ptr = (uint32_t *)&test_data[offset];
    int i;
    const int max = (TEST_SIZE - offset) / sizeof(word);

    ml_printf("Reading u32 from %#lx (offset %d):", ptr, offset);

    for (i = 0; i < max; i++) {
        uint8_t b1, b2, b3, b4;
        word = *ptr++;

        b1 = word >> 24 & 0xff;
        b2 = word >> 16 & 0xff;
        b3 = word >> 8 & 0xff;
        b4 = word & 0xff;

        if ((b1 < b2 && b1 != 0) ||
            (b2 < b3 && b2 != 0) ||
            (b3 < b4 && b3 != 0)) {
            ml_printf("Error %d, %d, %d, %d", b1, b2, b3, b4);
            return 2;
        } else {
            pdot(i);
        }
    }
    ml_printf("done\n");
    return 0;
}

static int read_test_data_u64(int offset)
{
    uint64_t word, *ptr = (uint64_t *)&test_data[offset];
    int i;
    const int max = (TEST_SIZE - offset) / sizeof(word);

    ml_printf("Reading u64 from %#lx (offset %d):", ptr, offset);

    for (i = 0; i < max; i++) {
        uint8_t b1, b2, b3, b4, b5, b6, b7, b8;
        word = *ptr++;

        b1 = ((uint64_t) (word >> 56)) & 0xff;
        b2 = ((uint64_t) (word >> 48)) & 0xff;
        b3 = ((uint64_t) (word >> 40)) & 0xff;
        b4 = (word >> 32) & 0xff;
        b5 = (word >> 24) & 0xff;
        b6 = (word >> 16) & 0xff;
        b7 = (word >> 8)  & 0xff;
        b8 = (word >> 0)  & 0xff;

        if ((b1 < b2 && b1 != 0) ||
            (b2 < b3 && b2 != 0) ||
            (b3 < b4 && b3 != 0) ||
            (b4 < b5 && b4 != 0) ||
            (b5 < b6 && b5 != 0) ||
            (b6 < b7 && b6 != 0) ||
            (b7 < b8 && b7 != 0)) {
            ml_printf("Error %d, %d, %d, %d, %d, %d, %d, %d",
                      b1, b2, b3, b4, b5, b6, b7, b8);
            return 2;
        } else {
            pdot(i);
        }
    }
    ml_printf("done\n");
    return 0;
}

/* Read the test data and verify at various offsets */
int do_reads(void)
{
    int r = 0;
    int off = 0;

    while (r == 0 && off < 8) {
        r = read_test_data_u16(off);
        r |= read_test_data_u32(off);
        r |= read_test_data_u64(off);
        off++;
    }

    return r;
}

int main(void)
{
    int i, r = 0;


    init_test_data_u8();
    r = do_reads();
    if (r) {
        return r;
    }

    for (i = 0; i < 8; i++) {
        init_test_data_u16(i);

        r = do_reads();
        if (r) {
            return r;
        }
    }

    for (i = 0; i < 8; i++) {
        init_test_data_u32(i);

        r = do_reads();
        if (r) {
            return r;
        }
    }

    ml_printf("Test complete: %s\n", r == 0 ? "PASSED" : "FAILED");
    return r;
}