rv64i.cpp revision 11730:08ab68477ea0
1/*
2 * Copyright (c) 2016 The University of Virginia
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
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Alec Roelke
29 */
30
31#include <fcntl.h>
32#include <sys/stat.h>
33#include <sys/time.h>
34#include <sys/times.h>
35#include <sys/types.h>
36#include <unistd.h>
37
38#include <cstdint>
39#include <cstring>
40#include <iostream>
41#include <limits>
42
43#include "insttest.h"
44#include "rv64i.h"
45
46int main()
47{
48    using namespace std;
49    using namespace insttest;
50
51    // LUI
52    expect<int64_t>(4096, []{return I::lui(1);}, "lui");
53    expect<int64_t>(numeric_limits<int32_t>::min(),
54            []{return I::lui(0x80000);}, "lui, negative");
55
56    // AUIPC
57    expect<bool>(true, []{return I::auipc(3);}, "auipc");
58
59    // Jump (JAL, JALR)
60    expect<bool>(true, []{return I::jal();}, "jal");
61    expect<bool>(true, []{return I::jalr();}, "jalr");
62
63    // BEQ
64    expect<bool>(true, []{return I::beq(5, 5);}, "beq, equal");
65    expect<bool>(false, []{return I::beq(numeric_limits<int64_t>::max(),
66            numeric_limits<int64_t>::min());}, "beq, not equal");
67
68    // BNE
69    expect<bool>(false, []{return I::bne(5, 5);}, "bne, equal");
70    expect<bool>(true, []{return I::bne(numeric_limits<int64_t>::max(),
71            numeric_limits<int64_t>::min());}, "bne, not equal");
72
73    // BLT
74    expect<bool>(true, []{return I::blt(numeric_limits<int64_t>::min(),
75            numeric_limits<int64_t>::max());}, "blt, less");
76    expect<bool>(false, []{return I::blt(numeric_limits<int64_t>::min(),
77            numeric_limits<int64_t>::min());}, "blt, equal");
78    expect<bool>(false, []{return I::blt(numeric_limits<int64_t>::max(),
79            numeric_limits<int64_t>::min());}, "blt, greater");
80
81    // BGE
82    expect<bool>(false, []{return I::bge(numeric_limits<int64_t>::min(),
83            numeric_limits<int64_t>::max());}, "bge, less");
84    expect<bool>(true, []{return I::bge(numeric_limits<int64_t>::min(),
85            numeric_limits<int64_t>::min());}, "bge, equal");
86    expect<bool>(true, []{return I::bge(numeric_limits<int64_t>::max(),
87            numeric_limits<int64_t>::min());}, "bge, greater");
88
89    // BLTU
90    expect<bool>(true, []{return I::blt(numeric_limits<int64_t>::min(),
91            numeric_limits<int64_t>::max());}, "bltu, greater");
92    expect<bool>(false, []{return I::blt(numeric_limits<int64_t>::min(),
93            numeric_limits<int64_t>::min());}, "bltu, equal");
94    expect<bool>(false, []{return I::blt(numeric_limits<int64_t>::max(),
95            numeric_limits<int64_t>::min());}, "bltu, less");
96
97    // BGEU
98    expect<bool>(false, []{return I::bge(numeric_limits<int64_t>::min(),
99            numeric_limits<int64_t>::max());}, "bgeu, greater");
100    expect<bool>(true, []{return I::bge(numeric_limits<int64_t>::min(),
101            numeric_limits<int64_t>::min());}, "bgeu, equal");
102    expect<bool>(true, []{return I::bge(numeric_limits<int64_t>::max(),
103            numeric_limits<int64_t>::min());}, "bgeu, less");
104
105    // Load (LB, LH, LW, LBU, LHU)
106    expect<int64_t>(7, []{return I::load<int8_t, int64_t>(0x07);},
107            "lb, positive");
108    expect<int64_t>(numeric_limits<int8_t>::min(),
109            []{return I::load<int8_t, int64_t>(0x80);}, "lb, negative");
110    expect<int64_t>(1792, []{return I::load<int16_t, int64_t>(0x0700);},
111            "lh, positive");
112    expect<int64_t>(numeric_limits<int16_t>::min(),
113            []{return I::load<int16_t, int64_t>(0x8000);}, "lh, negative");
114    expect<int64_t>(458752, []{return I::load<int32_t, int64_t>(0x00070000);},
115            "lw, positive");
116    expect<int64_t>(numeric_limits<int32_t>::min(),
117            []{return I::load<int32_t, int64_t>(0x80000000);},
118            "lw, negative");
119    expect<uint64_t>(128, []{return I::load<uint8_t, uint64_t>(0x80);}, "lbu");
120    expect<uint64_t>(32768, []{return I::load<uint16_t, uint64_t>(0x8000);},
121            "lhu");
122
123    // Store (SB, SH, SW)
124    expect<uint8_t>(0xFF, []{return I::store<int8_t>(-1);}, "sb");
125    expect<uint16_t>(0xFFFF, []{return I::store<int16_t>(-1);}, "sh");
126    expect<uint32_t>(0xFFFFFFFF, []{return I::store<int32_t>(-1);}, "sw");
127
128    // ADDI
129    expect<int64_t>(1073742078, []{return I::addi(0x3FFFFFFF, 255);},
130            "addi");
131    expect<int64_t>(1, []{return I::addi(-1, 2);}, "addi, overflow");
132
133    // SLTI
134    expect<bool>(true, []{return I::slti(-1, 0);}, "slti, true");
135    expect<bool>(false, []{return I::slti(0, -1);}, "slti, false");
136
137    // SLTIU
138    expect<bool>(false, []{return I::sltiu(-1, 0);}, "sltiu, false");
139    expect<bool>(true, []{return I::sltiu(0, -1);}, "sltiu, true");
140
141    // XORI
142    expect<uint64_t>(0xFF, []{return I::xori(0xAA, 0x55);}, "xori (1)");
143    expect<uint64_t>(0, []{return I::xori(0xAA, 0xAA);}, "xori (0)");
144
145    // ORI
146    expect<uint64_t>(0xFF, []{return I::ori(0xAA, 0x55);}, "ori (1)");
147    expect<uint64_t>(0xAA, []{return I::ori(0xAA, 0xAA);}, "ori (A)");
148
149    // ANDI
150    expect<uint64_t>(0, []{return I::andi(-1, 0);}, "andi (0)");
151    expect<uint64_t>(0x1234567812345678ULL,
152            []{return I::andi(0x1234567812345678ULL, -1);}, "andi (1)");
153
154    // SLLI
155    expect<int64_t>(65280, []{return I::slli(255, 8);}, "slli, general");
156    expect<int64_t>(numeric_limits<int64_t>::min(),
157            []{return I::slli(255, 63);}, "slli, erase");
158
159    // SRLI
160    expect<int64_t>(255, []{return I::srli(65280, 8);}, "srli, general");
161    expect<int64_t>(0, []{return I::srli(255, 8);}, "srli, erase");
162    expect<int64_t>(1, []{return I::srli(numeric_limits<int64_t>::min(), 63);},
163            "srli, negative");
164
165    // SRAI
166    expect<int64_t>(255, []{return I::srai(65280, 8);}, "srai, general");
167    expect<int64_t>(0, []{return I::srai(255, 8);}, "srai, erase");
168    expect<int64_t>(-1,
169            []{return I::srai(numeric_limits<int64_t>::min(), 63);},
170            "srai, negative");
171
172    // ADD
173    expect<int64_t>(1073742078, []{return I::add(0x3FFFFFFF, 255);}, "add");
174    expect<int64_t>(-1,
175            []{return I::add(0x7FFFFFFFFFFFFFFFLL, 0x8000000000000000LL);},
176            "add, overflow");
177
178    // SUB
179    expect<int64_t>(65535, []{return I::sub(65536, 1);}, "sub");
180    expect<int64_t>(-1,
181            []{return I::sub(0x7FFFFFFFFFFFFFFFLL, 0x8000000000000000LL);},
182            "sub, \"overflow\"");
183
184    // SLL
185    expect<int64_t>(65280, []{return I::sll(255, 8);}, "sll, general");
186    expect<int64_t>(numeric_limits<int64_t>::min(),
187            []{return I::sll(255, 63);}, "sll, erase");
188
189    // SLT
190    expect<bool>(true, []{return I::slt(-1, 0);}, "slt, true");
191    expect<bool>(false, []{return I::slt(0, -1);}, "slt, false");
192
193    // SLTU
194    expect<bool>(false, []{return I::sltu(-1, 0);}, "sltu, false");
195    expect<bool>(true, []{return I::sltu(0, -1);}, "sltu, true");
196
197    // XOR
198    expect<uint64_t>(-1,
199            []{return I::xor_inst(0xAAAAAAAAAAAAAAAAULL,
200                    0x5555555555555555ULL);},
201            "xor (1)");
202    expect<uint64_t>(0,
203            []{return I::xor_inst(0xAAAAAAAAAAAAAAAAULL,
204                    0xAAAAAAAAAAAAAAAAULL);},
205            "xor (0)");
206
207    // SRL
208    expect<uint64_t>(255, []{return I::srl(65280, 8);}, "srl, general");
209    expect<uint64_t>(0, []{return I::srl(255, 8);}, "srl, erase");
210    expect<uint64_t>(1, []{return I::srl(numeric_limits<int64_t>::min(), 63);},
211            "srl, negative");
212
213    // SRA
214    expect<int64_t>(255, []{return I::sra(65280, 8);}, "sra, general");
215    expect<int64_t>(0, []{return I::sra(255, 8);}, "sra, erase");
216    expect<int64_t>(-1, []{return I::sra(numeric_limits<int64_t>::min(), 63);},
217            "sra, negative");
218
219    // OR
220    expect<uint64_t>(-1,
221            []{return I::or_inst(0xAAAAAAAAAAAAAAAAULL,
222                    0x5555555555555555ULL);},
223            "or (1)");
224    expect<uint64_t>(0xAAAAAAAAAAAAAAAAULL,
225            []{return I::or_inst(0xAAAAAAAAAAAAAAAAULL,
226                    0xAAAAAAAAAAAAAAAAULL);},
227            "or (A)");
228
229    // AND
230    expect<uint64_t>(0, []{return I::and_inst(-1, 0);}, "and (0)");
231    expect<uint64_t>(0x1234567812345678ULL,
232            []{return I::and_inst(0x1234567812345678ULL, -1);}, "and (-1)");
233
234    // FENCE/FENCE.I
235    asm volatile("fence" : : );
236    asm volatile("fence.i" : : );
237
238    // ECALL
239    char fname[] = "test.txt";
240    char teststr[] = "this is a test";
241    expect<bool>(true, [=]{
242            int fd = open(fname, O_CREAT | O_WRONLY | O_TRUNC, 0644);
243            if (fd < 0) {
244                return false;
245            }
246            size_t n = write(fd, teststr, sizeof(teststr));
247            cout << "Bytes written: " << n << endl;
248            return close(fd) >= 0 && n > 0;
249        }, "open, write");
250    expect<int>(0, [=]{return access(fname, F_OK);}, "access F_OK");
251    expect<int>(0, [=]{return access(fname, R_OK);}, "access R_OK");
252    expect<int>(0, [=]{return access(fname, W_OK);}, "access W_OK");
253    // gem5's implementation of access is incorrect; it should return
254    // -1 on failure, not -errno.  Account for this using an inequality.
255    expect<bool>(true, [=]{return access(fname, X_OK) != 0;}, "access X_OK");
256    expect<bool>(true, [=]{
257            struct stat stat_buf, fstat_buf;
258            int s = stat(fname, &stat_buf);
259            if (s < 0) {
260                return false;
261            } else {
262                cout << "stat:" << endl;
263                cout << "\tst_dev =\t" << stat_buf.st_dev << endl;
264                cout << "\tst_ino =\t" << stat_buf.st_ino << endl;
265                cout << "\tst_mode =\t" << stat_buf.st_mode << endl;
266                cout << "\tst_nlink =\t" << stat_buf.st_nlink << endl;
267                cout << "\tst_uid =\t" << stat_buf.st_uid << endl;
268                cout << "\tst_gid =\t" << stat_buf.st_gid << endl;
269                cout << "\tst_rdev =\t" << stat_buf.st_rdev << endl;
270                cout << "\tst_size =\t" << stat_buf.st_size << endl;
271                cout << "\tst_blksize =\t" << stat_buf.st_blksize << endl;
272                cout << "\tst_blocks =\t" << stat_buf.st_blocks << endl;
273            }
274            int fd = open(fname, O_RDONLY);
275            if (fd < 0) {
276                return false;
277            }
278            int f = fstat(fd, &fstat_buf);
279            if (f >= 0) {
280                cout << "fstat:" << endl;
281                cout << "\tst_dev =\t" << fstat_buf.st_dev << endl;
282                cout << "\tst_ino =\t" << fstat_buf.st_ino << endl;
283                cout << "\tst_mode =\t" << fstat_buf.st_mode << endl;
284                cout << "\tst_nlink =\t" << fstat_buf.st_nlink << endl;
285                cout << "\tst_uid =\t" << fstat_buf.st_uid << endl;
286                cout << "\tst_gid =\t" << fstat_buf.st_gid << endl;
287                cout << "\tst_rdev =\t" << fstat_buf.st_rdev << endl;
288                cout << "\tst_size =\t" << fstat_buf.st_size << endl;
289                cout << "\tst_blksize =\t" << fstat_buf.st_blksize << endl;
290                cout << "\tst_blocks =\t" << fstat_buf.st_blocks << endl;
291            }
292            return close(fd) >= 0 && f >= 0;
293        }, "open, stat");
294    expect<bool>(true, [=]{
295            int fd = open(fname, O_RDONLY);
296            if (fd < 0) {
297                return false;
298            }
299            char in[128];
300            size_t n = read(fd, in, sizeof(in));
301            cout << "Bytes read: " << n << endl;
302            cout << "String read: " << in << endl;
303            int cl = close(fd);
304            int un = unlink(fname);
305            return n > 0 && cl >= 0 && un >= 0 && strcmp(teststr, in) == 0;
306        }, "open, read, unlink");
307    expect<bool>(true, []{
308            struct tms buf;
309            clock_t t = times(&buf);
310            cout << "times:" << endl;
311            cout << "\ttms_utime =\t" << buf.tms_utime << endl;
312            cout << "\ttms_stime =\t" << buf.tms_stime << endl;
313            cout << "\ttms_cutime =\t" << buf.tms_cutime << endl;
314            cout << "\ttms_cstime =\t" << buf.tms_cstime << endl;
315            return t > 0;
316        }, "times");
317    expect<int>(0, []{
318            struct timeval time;
319            int res = gettimeofday(&time, nullptr);
320            cout << "timeval:" << endl;
321            cout << "\ttv_sec =\t" << time.tv_sec << endl;
322            cout << "\ttv_usec =\t" << time.tv_usec << endl;
323            return res;
324        }, "gettimeofday");
325
326    // EBREAK not tested because it only makes sense in FS mode or when
327    // using gdb
328
329    // ERET not tested because it only makes sense in FS mode and will cause
330    // a panic when used in SE mode
331
332    // CSRs (RDCYCLE, RDTIME, RDINSTRET)
333    expect<bool>(true, []{
334                uint64_t cycles = 0;
335                asm("rdcycle %0" : "=r" (cycles));
336                cout << "Cycles: " << cycles << endl;
337                return cycles > 0;
338            }, "rdcycle");
339    expect<bool>(true, []{
340                uint64_t time = 0;
341                asm("rdtime %0" : "=r" (time));
342                cout << "Time: " << time << endl;
343                return time > 0;
344            }, "rdtime");
345    expect<bool>(true, []{
346                uint64_t instret = 0;
347                asm("rdinstret %0" : "=r" (instret));
348                cout << "Instructions Retired: " << instret << endl;
349                return instret > 0;
350            }, "rdinstret");
351
352    // 64-bit memory (LWU, LD, SD)
353    expect<int64_t>(0xFFFFFFFF, []{return I::load<uint32_t, uint64_t>(-1);},
354            "lwu");
355    expect<int64_t>(30064771072,
356            []{return I::load<int64_t, int64_t>(30064771072);}, "ld");
357    expect<uint64_t>(-1, []{return I::store<int64_t>(-1);}, "sd");
358
359    // ADDIW
360    expect<int64_t>(268435710, []{return I::addiw(0x0FFFFFFF, 255);}, "addiw");
361    expect<int64_t>(-2147481602, []{return I::addiw(0x7FFFFFFF, 0x7FF);},
362            "addiw, overflow");
363    expect<int64_t>(0, []{return I::addiw(0x7FFFFFFFFFFFFFFFLL, 1);},
364            "addiw, truncate");
365
366    // SLLIW
367    expect<int64_t>(65280, []{return I::slliw(255, 8);}, "slliw, general");
368    expect<int64_t>(numeric_limits<int32_t>::min(),
369            []{return I::slliw(255, 31);}, "slliw, erase");
370    expect<int64_t>(numeric_limits<int32_t>::min(),
371            []{return I::slliw(0xFFFFFFFF00800000LL, 8);}, "slliw, truncate");
372
373    // SRLIW
374    expect<int64_t>(255, []{return I::srliw(65280, 8);}, "srliw, general");
375    expect<int64_t>(0, []{return I::srliw(255, 8);}, "srliw, erase");
376    expect<int64_t>(1,
377            []{return I::srliw(numeric_limits<int32_t>::min(), 31);},
378            "srliw, negative");
379    expect<int64_t>(1, []{return I::srliw(0xFFFFFFFF80000000LL, 31);},
380            "srliw, truncate");
381
382    // SRAIW
383    expect<int64_t>(255, []{return I::sraiw(65280, 8);}, "sraiw, general");
384    expect<int64_t>(0, []{return I::sraiw(255, 8);}, "sraiw, erase");
385    expect<int64_t>(-1,
386            []{return I::sraiw(numeric_limits<int32_t>::min(), 31);},
387            "sraiw, negative");
388    expect<int64_t>(-1, []{return I::sraiw(0x0000000180000000LL, 31);},
389            "sraiw, truncate");
390
391    // ADDW
392    expect<int64_t>(1073742078, []{return I::addw(0x3FFFFFFF, 255);}, "addw");
393    expect<int64_t>(-1, []{return I::addw(0x7FFFFFFF, 0x80000000);},
394            "addw, overflow");
395    expect<int64_t>(65536, []{return I::addw(0xFFFFFFFF0000FFFFLL, 1);},
396            "addw, truncate");
397
398    // SUBW
399    expect<int64_t>(65535, []{return I::subw(65536, 1);}, "subw");
400    expect<int64_t>(-1, []{return I::subw(0x7FFFFFFF, 0x80000000);},
401            "subw, \"overflow\"");
402    expect<int64_t>(0,
403            []{return I::subw(0xAAAAAAAAFFFFFFFFULL, 0x55555555FFFFFFFFULL);},
404            "subw, truncate");
405
406    // SLLW
407    expect<int64_t>(65280, []{return I::sllw(255, 8);}, "sllw, general");
408    expect<int64_t>(numeric_limits<int32_t>::min(),
409            []{return I::sllw(255, 31);}, "sllw, erase");
410    expect<int64_t>(numeric_limits<int32_t>::min(),
411            []{return I::sllw(0xFFFFFFFF00008000LL, 16);}, "sllw, truncate");
412
413    // SRLW
414    expect<uint64_t>(255, []{return I::srlw(65280, 8);}, "srlw, general");
415    expect<uint64_t>(0, []{return I::srlw(255, 8);}, "srlw, erase");
416    expect<uint64_t>(1,
417            []{return I::srlw(numeric_limits<int32_t>::min(), 31);},
418            "srlw, negative");
419    expect<uint64_t>(1, []{return I::srlw(0x0000000180000000LL, 31);},
420            "srlw, truncate");
421
422    // SRAW
423    expect<int64_t>(255, []{return I::sraw(65280, 8);}, "sraw, general");
424    expect<int64_t>(0, []{return I::sraw(255, 8);}, "sraw, erase");
425    expect<int64_t>(-1,
426            []{return I::sraw(numeric_limits<int32_t>::min(), 31);},
427            "sraw, negative");
428    expect<int64_t>(1, []{return I::sraw(0xFFFFFFFF40000000LL, 30);},
429            "sraw, truncate");
430
431    return 0;
432}
433