OpenCores
URL https://opencores.org/ocsvn/ao486/ao486/trunk

Subversion Repositories ao486

[/] [ao486/] [trunk/] [ao486_tool/] [src/] [ao486/] [module/] [memory/] [test/] [TestTLBCheckAccess.java] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 alfik
/*
2
 * Copyright (c) 2014, Aleksander Osman
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * * Redistributions of source code must retain the above copyright notice, this
9
 *   list of conditions and the following disclaimer.
10
 *
11
 * * Redistributions in binary form must reproduce the above copyright notice,
12
 *   this list of conditions and the following disclaimer in the documentation
13
 *   and/or other materials provided with the distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
 */
26
 
27
package ao486.module.memory.test;
28
 
29
import ao486.module.memory.AvalonListener;
30
import ao486.module.memory.CheckListener;
31
import ao486.module.memory.GlobalListener;
32
import ao486.module.memory.Input;
33
import ao486.module.memory.Listener;
34
import ao486.module.memory.Output;
35
import ao486.module.memory.ReadListener;
36
import ao486.module.memory.Test;
37
import ao486.module.memory.WriteListener;
38
import java.util.HashSet;
39
import java.util.LinkedList;
40
import java.util.Random;
41
 
42
class TestTLBCheckAccessState {
43
 
44
    TestTLBCheckAccessState(Random random) {
45
        this.random = random;
46
 
47
        do {
48
            cr3_base = random.nextLong();
49
            cr3_base &= 0xFFFFF000L;
50
        } while(Test.address_not_in_cache(cr3_base) || Test.address_not_in_cache(cr3_base + 0x1000L));
51
 
52
        cr3_pwt = true;
53
        cr3_pcd = true;
54
 
55
        linear_set = new HashSet<>();
56
    }
57
 
58
    boolean overlap(long a1, long a2, long b1, long b2) {
59
        if(b2 <= a2 && b2 >= a1) return true;
60
        if(b1 <= a2 && b1 >= a1) return true;
61
        return false;
62
    }
63
 
64
    void prepare(AvalonListener avalon) {
65
 
66
        //pde
67
        do {
68
            pde = random.nextLong();
69
            pde &= 0xFFFFF000L;
70
        } while(Test.address_not_in_cache(pde) || Test.address_not_in_cache(pde + 0x1000L) || overlap(cr3_base, cr3_base+ 0x1000L, pde, pde + 0x1000L));
71
 
72
        pde_pwt     = true;
73
        pde_pcd     = true;
74
        pde_present = random.nextInt(3) == 0;
75
        pde_rw      = random.nextBoolean();
76
        pde_su      = random.nextBoolean();
77
        pde_accessed= random.nextBoolean();
78
 
79
        //pte
80
        do {
81
            pte = random.nextLong();
82
            pte &= 0xFFFFF000L;
83
        } while(Test.address_not_in_cache(pte) || Test.address_not_in_cache(pte + 0x1000L) ||
84
                overlap(cr3_base, cr3_base+ 0x1000L, pte, pte + 0x1000L) || overlap(pde, pde+ 0x1000L, pte, pte + 0x1000L));
85
 
86
        pte_pwt     = true;
87
        pte_pcd     = true;
88
        pte_present = random.nextInt(3) == 0;
89
        pte_rw      = random.nextBoolean();
90
        pte_su      = random.nextBoolean();
91
        pte_accessed= random.nextBoolean();
92
        pte_dirty   = random.nextBoolean();
93
 
94
        //linear
95
        do {
96
            linear = random.nextLong();
97
            linear &= 0xFFFFFFFFL;
98
        } while(linear_set.contains(linear & 0xFFFFF000L));
99
 
100
        linear_set.add(linear & 0xFFFFF000L);
101
 
102
        linear_pde = (linear >> 22) & 0x3FFL;
103
        linear_pte = (linear >> 12) & 0x3FFL;
104
 
105
        length = (int)(16 - (linear & 0xFL));
106
        if(length > 8) length = 8;
107
        length = 1 + random.nextInt(length);
108
 
109
        physical = pte | (linear & 0xFFFL);
110
 
111
        //----------
112
 
113
        long pde_value = pde;
114
        if(pde_present) pde_value |= 0x1L;
115
        if(pde_rw)      pde_value |= 0x2L;
116
        if(pde_su)      pde_value |= 0x4L;
117
        if(pde_pwt)     pde_value |= 0x8L;
118
        if(pde_pcd)     pde_value |= 0x10L;
119
        if(pde_accessed)pde_value |= 0x20L;
120
 
121
        avalon.set_memory(cr3_base | (linear_pde << 2), pde_value, 4);
122
 
123
        long pte_value = pte;
124
        if(pte_present) pte_value |= 0x1L;
125
        if(pte_rw)      pte_value |= 0x2L;
126
        if(pte_su)      pte_value |= 0x4L;
127
        if(pte_pwt)     pte_value |= 0x8L;
128
        if(pte_pcd)     pte_value |= 0x10L;
129
        if(pte_accessed)pte_value |= 0x20L;
130
        if(pte_dirty)   pte_value |= 0x40L;
131
 
132
        avalon.set_memory(pde | (linear_pte << 2), pte_value, 4);
133
    }
134
 
135
    boolean should_page_fault(boolean is_write, boolean cr0_wp) {
136
 
137
        boolean tlb_rw = pde_rw && pte_rw;
138
        boolean tlb_su = pde_su && pte_su;
139
 
140
        if(pde_present == false) return true;
141
        if(pte_present == false) return true;
142
 
143
        if(cr0_wp && tlb_rw == false && is_write) return true;
144
 
145
        return false;
146
    }
147
 
148
    Random random;
149
 
150
    long cr3_base;
151
    boolean cr3_pwt;
152
    boolean cr3_pcd;
153
 
154
    long pde;
155
    boolean pde_pwt;
156
    boolean pde_pcd;
157
    boolean pde_present;
158
    boolean pde_rw;
159
    boolean pde_su;
160
    boolean pde_accessed;
161
 
162
    long pte;
163
    boolean pte_pwt;
164
    boolean pte_pcd;
165
    boolean pte_present;
166
    boolean pte_rw;
167
    boolean pte_su;
168
    boolean pte_accessed;
169
    boolean pte_dirty;
170
 
171
    long linear;
172
    long linear_pde;
173
    long linear_pte;
174
 
175
    long physical;
176
 
177
    int length;
178
 
179
    HashSet<Long> linear_set;
180
}
181
 
182
public class TestTLBCheckAccess extends Test implements Listener, ReadListener.ReadInterface, WriteListener.WriteInterface, CheckListener.CheckInterface {
183
 
184
    void go(long seed) throws Exception {
185
        random = new Random(seed);
186
 
187
        avalon = new AvalonListener(random, this);
188
        check  = new CheckListener();
189
        global = new GlobalListener();
190
 
191
        finish_cycle = 0;
192
        counter = 1;
193
 
194
        state = new TestTLBCheckAccessState(random);
195
 
196
        global.set(true, random.nextBoolean(), false, true, true, false, state.cr3_base, state.cr3_pcd, state.cr3_pwt, true, false);
197
 
198
//System.out.printf("addr: %x\n", pde | (linear_pde << 2));
199
 
200
        state.prepare(avalon);
201
 
202
        is_write = random.nextBoolean();
203
 
204
        check.check(4, state.linear, is_write, this);
205
 
206
        //----------
207
 
208
        LinkedList<Listener> listeners = new LinkedList<>();
209
        listeners.add(avalon);
210
        listeners.add(check);
211
        listeners.add(global);
212
        listeners.add(this);
213
 
214
        run_simulation(listeners);
215
    }
216
    @Override
217
    public void read(int cycle, long data) throws Exception {
218
        //System.out.printf("Read value: %x from linear/physical %x/%x size %d\n", data, state.linear, state.physical, state.length);
219
    }
220
 
221
    @Override
222
    public void read_page_fault(int cycle, long cr2, long error_code) throws Exception {
223
        throw new Exception(String.format("read_page_fault: cr2: %x, error_code: %x", cr2, error_code));
224
    }
225
    @Override
226
    public void read_ac_fault(int cycle) throws Exception {
227
        throw new Exception("read_ac_fault.");
228
    }
229
 
230
    @Override
231
    public void written(int cycle) throws Exception {
232
    }
233
    @Override
234
    public void write_page_fault(int cycle, long cr2, long error_code) throws Exception {
235
        throw new Exception(String.format("write_page_fault: cr2: %x, error_code: %x", cr2, error_code));
236
    }
237
    @Override
238
    public void write_ac_fault(int cycle) throws Exception {
239
        throw new Exception("write_ac_fault.");
240
    }
241
 
242
    @Override
243
    public void checked(int cycle) throws Exception {
244
 
245
        boolean should_pf = state.should_page_fault(is_write, global.cr0_wp);
246
        if(should_pf) throw new Exception("checked() but should page fault.");
247
 
248
        checked_counter++;
249
 
250
        counter++;
251
 
252
        if(counter >= 1000) {
253
            System.out.println("checked_counter: " + checked_counter);
254
 
255
            finish_cycle = cycle+1;
256
            return;
257
        }
258
 
259
        state.prepare(avalon);
260
 
261
        is_write = random.nextBoolean();
262
 
263
        check.check(cycle+1, state.linear, is_write, this);
264
    }
265
    @Override
266
    public void check_page_fault(int cycle, long cr2, long error_code) throws Exception {
267
        //System.out.println("check_page_fault.");
268
 
269
        boolean should_pf = state.should_page_fault(is_write, global.cr0_wp);
270
        if(should_pf == false) throw new Exception("check_page_fault() but should not page fault.");
271
 
272
        counter++;
273
 
274
        if(counter >= 1000) {
275
            System.out.println("checked_counter: " + checked_counter);
276
 
277
            finish_cycle = cycle+1;
278
            return;
279
        }
280
 
281
        finish_reset_cycle = cycle+1;
282
        global.set_reset(false, false, true, false);
283
 
284
        state.prepare(avalon);
285
 
286
        is_write = random.nextBoolean();
287
 
288
        check.check(cycle+3, state.linear, is_write, this);
289
    }
290
 
291
    @Override
292
    public void set_input(int cycle, Input input) throws Exception {
293
        if(cycle >= finish_cycle && finish_cycle > 0) {
294
            input.finished = true;
295
        }
296
 
297
        if(cycle >= finish_reset_cycle && finish_reset_cycle > 0) {
298
            global.set_reset(false, false, false, false);
299
            finish_reset_cycle = 0;
300
        }
301
    }
302
 
303
    @Override
304
    public void get_output(int cycle, Output output) throws Exception {
305
    }
306
 
307
    @Override
308
    public void avalon_read(int cycle, long read_address) throws Exception {
309
    }
310
 
311
    int counter;
312
    Random random;
313
 
314
    int finish_cycle;
315
 
316
    TestTLBCheckAccessState state;
317
 
318
    int finish_reset_cycle;
319
    boolean is_write;
320
 
321
    int checked_counter;
322
 
323
    AvalonListener avalon;
324
    CheckListener  check;
325
    GlobalListener global;
326
 
327
    //-------------
328
 
329
    public static void main(String args[]) throws Exception {
330
        TestTLBCheckAccess test1 = new TestTLBCheckAccess();
331
 
332
        for(int i=0; i<1; i++) {
333
            test1.go(i);
334
 
335
            System.out.println("Run " + i + " complete.");
336
        }
337
 
338
        System.out.println("[Main] thread end.");
339
    }
340
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.