1/*
2 * Copyright (c) 2018, Cornell University
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or
6 * without modification, are permitted provided that the following
7 * conditions are met:
8 *
9 * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
16 *
17 * Neither the name of Cornell University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
22 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
23 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
26 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
29 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 *
35 * Authors: Tuan Ta
36 */
37
38//------------------------------------------------------------------------
39// This code tests amoswap.d instruction in multi-threading system.
40// All threads execute a critical section LOOP_COUNT times. A thread
41// gets into a critical section by acquiring a lock variable (i.e.,
42// shared_var) and checking return value.
43// 0 means the lock is not being locked. Each thread increments
44// a variable (i.e., var) inside the critical section and releases the
45// lock by swapping back 0 to the lock variable.
46// The master thread (i.e., thread 0) waits for all threads to complete
47// and compare the var's value to the expected result.
48//------------------------------------------------------------------------
49
50#include "riscv_test.h"
51#include "test_macros.h"
52#include "test_macros_mt.h"
53
54  RVTEST_RV64U
55  RVTEST_CODE_BEGIN
56
57#define LOOP_COUNT  1000
58#define RESULT      NUM_THREADS * LOOP_COUNT
59
60//------------------------------------------------------------------------
61// Master thread creates new threads, waits for all threads to complete,
62// deallocates threads and checks result
63//------------------------------------------------------------------------
64  call _create_threads
65  call _join
66  call _delete_threads
67  call _check
68
69  RVTEST_CODE_END
70
71//------------------------------------------------------------------------
72// mt_test function executed in child threads
73// A child thread signals its completion by atomicaly adding 1 to barrier
74//------------------------------------------------------------------------
75_mt_test:
76  li        t0, 1               // initialize the swap value (1-locked)
77  li        t1, LOOP_COUNT
78  la        t2, var             // load the var's address
79  la        a0, shared_var
80
811:
82  amoswap.d.aq  s2, t0, (a0)    // try to swap t0 with the lock
83  bnez          s2, 1b          // retry if the lock is being held
84
85  lw            t3, (t2)        // load the var's value
86  addi          t3, t3, 1       // add 1 to the value
87  sw            t3, (t2)        // store the new value to var
88
89  amoswap.d.rl  zero, zero, (a0)// release the lock by swapping back 0
90
91  addi          t1, t1, -1      // decrement the loop_count
92  bnez          t1, 1b          // repeat if not done yet
93
94  la            a0, barrier
95  amoadd.d      zero, t0, (a0)  // signal this thread's completion
96
97  RVTEST_CODE_END
98
99//------------------------------------------------------------------------
100// Master thread checks result
101//------------------------------------------------------------------------
102_check:
103  la        a0, var
104  li        a1, RESULT
105  ld        a0, (a0)
106  bne       a0, a1, _fail
107  li        a0, SUCCESS
108  ret
109
110_fail:
111  li        a0, FAILURE
112  ret
113
114  .data
115
116MT_DATA
117var: .dword   0
118