lrsc_d.S revision 12771
113531Sjairo.balart@metempsy.com/*
214227Sgiacomo.travaglini@arm.com * Copyright (c) 2018, Cornell University
314227Sgiacomo.travaglini@arm.com * All rights reserved.
414227Sgiacomo.travaglini@arm.com *
514227Sgiacomo.travaglini@arm.com * Redistribution and use in source and binary forms, with or
614227Sgiacomo.travaglini@arm.com * without modification, are permitted provided that the following
714227Sgiacomo.travaglini@arm.com * conditions are met:
814227Sgiacomo.travaglini@arm.com *
914227Sgiacomo.travaglini@arm.com * Redistributions of source code must retain the above copyright
1014227Sgiacomo.travaglini@arm.com * notice, this list of conditions and the following disclaimer.
1114227Sgiacomo.travaglini@arm.com *
1214227Sgiacomo.travaglini@arm.com * Redistributions in binary form must reproduce the above
1314227Sgiacomo.travaglini@arm.com * copyright notice, this list of conditions and the following
1413531Sjairo.balart@metempsy.com * disclaimer in the documentation and/or other materials provided
1513531Sjairo.balart@metempsy.com * with the distribution.
1613531Sjairo.balart@metempsy.com *
1713531Sjairo.balart@metempsy.com * Neither the name of Cornell University nor the names of its
1813531Sjairo.balart@metempsy.com * contributors may be used to endorse or promote products derived
1913531Sjairo.balart@metempsy.com * from this software without specific prior written permission.
2013531Sjairo.balart@metempsy.com *
2113531Sjairo.balart@metempsy.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
2213531Sjairo.balart@metempsy.com * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
2313531Sjairo.balart@metempsy.com * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
2413531Sjairo.balart@metempsy.com * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2513531Sjairo.balart@metempsy.com * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
2613531Sjairo.balart@metempsy.com * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2713531Sjairo.balart@metempsy.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2813531Sjairo.balart@metempsy.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
2913531Sjairo.balart@metempsy.com * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
3013531Sjairo.balart@metempsy.com * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3113531Sjairo.balart@metempsy.com * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
3213531Sjairo.balart@metempsy.com * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3313531Sjairo.balart@metempsy.com * POSSIBILITY OF SUCH DAMAGE.
3413531Sjairo.balart@metempsy.com *
3513531Sjairo.balart@metempsy.com * Authors: Tuan Ta
3613531Sjairo.balart@metempsy.com */
3713531Sjairo.balart@metempsy.com
3813531Sjairo.balart@metempsy.com//------------------------------------------------------------------------
3913531Sjairo.balart@metempsy.com// This code tests lr.d and sc.d instructions in multi-threading system.
4013531Sjairo.balart@metempsy.com// All threads execute a critical section LOOP_COUNT times. A thread
4113531Sjairo.balart@metempsy.com// gets into a critical section by acquiring a lock variable (i.e.,
4213531Sjairo.balart@metempsy.com// shared_var) and checking return value.
4313531Sjairo.balart@metempsy.com// 0 means the lock is not being locked. Each thread increments
4413531Sjairo.balart@metempsy.com// a variable (i.e., var) inside the critical section and releases the
4513531Sjairo.balart@metempsy.com// lock by swapping back 0 to the lock variable.
4613531Sjairo.balart@metempsy.com// The master thread (i.e., thread 0) waits for all threads to complete
4713531Sjairo.balart@metempsy.com// and compare the var's value to the expected result.
4813531Sjairo.balart@metempsy.com//------------------------------------------------------------------------
4913531Sjairo.balart@metempsy.com
5013531Sjairo.balart@metempsy.com#include "riscv_test.h"
5113926Sgiacomo.travaglini@arm.com#include "test_macros.h"
5213926Sgiacomo.travaglini@arm.com#include "test_macros_mt.h"
5313926Sgiacomo.travaglini@arm.com
5413531Sjairo.balart@metempsy.com  RVTEST_RV64U
5513531Sjairo.balart@metempsy.com  RVTEST_CODE_BEGIN
5613531Sjairo.balart@metempsy.com
5713531Sjairo.balart@metempsy.com#define LOOP_COUNT  1000
5813531Sjairo.balart@metempsy.com#define RESULT      NUM_THREADS * LOOP_COUNT
5913531Sjairo.balart@metempsy.com
6013531Sjairo.balart@metempsy.com//------------------------------------------------------------------------
6113531Sjairo.balart@metempsy.com// Master thread creates new threads, waits for all threads to complete,
6213531Sjairo.balart@metempsy.com// deallocates threads and checks result
6313531Sjairo.balart@metempsy.com//------------------------------------------------------------------------
6413531Sjairo.balart@metempsy.com  call _create_threads
6513531Sjairo.balart@metempsy.com  call _join
6613531Sjairo.balart@metempsy.com  call _delete_threads
6713531Sjairo.balart@metempsy.com  call _check
6813531Sjairo.balart@metempsy.com
6913531Sjairo.balart@metempsy.com  RVTEST_CODE_END
7013531Sjairo.balart@metempsy.com
7113531Sjairo.balart@metempsy.com//------------------------------------------------------------------------
7213531Sjairo.balart@metempsy.com// mt_test function executed in child threads
7313531Sjairo.balart@metempsy.com// A child thread signals its completion by atomicaly adding 1 to barrier
7413531Sjairo.balart@metempsy.com//------------------------------------------------------------------------
7513531Sjairo.balart@metempsy.com_mt_test:
7613531Sjairo.balart@metempsy.com  li        t0, 1               // initialize the swap value (1-locked)
7713531Sjairo.balart@metempsy.com  li        t1, LOOP_COUNT
7813531Sjairo.balart@metempsy.com  la        t2, var             // load the var's address
7913531Sjairo.balart@metempsy.com  la        a0, shared_var
8013531Sjairo.balart@metempsy.com
8113531Sjairo.balart@metempsy.com1:
8213826Sgiacomo.travaglini@arm.com  lr.d.aq       s2, (a0)        // load and reserve a0
8313826Sgiacomo.travaglini@arm.com  bnez          s2, 1b          // retry lr if the lock is being held
8413826Sgiacomo.travaglini@arm.com  sc.d.rl       s2, t0, (a0)    // try to lock a0
8513826Sgiacomo.travaglini@arm.com  bnez          s2, 1b          // retry if sc failed
8613826Sgiacomo.travaglini@arm.com
8713826Sgiacomo.travaglini@arm.com  lw            t3, (t2)        // load the var's value
8813531Sjairo.balart@metempsy.com  addi          t3, t3, 1       // add 1 to the value
8913760Sjairo.balart@metempsy.com  sw            t3, (t2)        // store the new value to var
9013531Sjairo.balart@metempsy.com
9113531Sjairo.balart@metempsy.com  sd            zero, (a0)      // release the lock by storing 0 to a0
9213531Sjairo.balart@metempsy.com
9313531Sjairo.balart@metempsy.com  addi          t1, t1, -1      // decrement the loop_count
9413531Sjairo.balart@metempsy.com  bnez          t1, 1b          // repeat if not done yet
9513531Sjairo.balart@metempsy.com
9613531Sjairo.balart@metempsy.com  la            a0, barrier
9713531Sjairo.balart@metempsy.com  amoadd.d      zero, t0, (a0)  // signal this thread's completion
9813531Sjairo.balart@metempsy.com
9913531Sjairo.balart@metempsy.com  RVTEST_CODE_END
10013531Sjairo.balart@metempsy.com
10113531Sjairo.balart@metempsy.com//------------------------------------------------------------------------
10213531Sjairo.balart@metempsy.com// Master thread checks result
10313760Sjairo.balart@metempsy.com//------------------------------------------------------------------------
10413531Sjairo.balart@metempsy.com_check:
10513531Sjairo.balart@metempsy.com  la        a0, var
10613531Sjairo.balart@metempsy.com  li        a1, RESULT
10713531Sjairo.balart@metempsy.com  ld        a0, (a0)
10813531Sjairo.balart@metempsy.com  bne       a0, a1, _fail
10913531Sjairo.balart@metempsy.com  li        a0, SUCCESS
11013531Sjairo.balart@metempsy.com  ret
11113531Sjairo.balart@metempsy.com
11213531Sjairo.balart@metempsy.com_fail:
11313531Sjairo.balart@metempsy.com  li        a0, FAILURE
11413531Sjairo.balart@metempsy.com  ret
11513531Sjairo.balart@metempsy.com
11613580Sgabeblack@google.com  .data
11713531Sjairo.balart@metempsy.com
11813531Sjairo.balart@metempsy.comMT_DATA
11913580Sgabeblack@google.comvar: .dword   0
12013531Sjairo.balart@metempsy.com