112771Sqtt2@cornell.edu/* 212771Sqtt2@cornell.edu * Copyright (c) 2018, Cornell University 312771Sqtt2@cornell.edu * All rights reserved. 412771Sqtt2@cornell.edu * 512771Sqtt2@cornell.edu * Redistribution and use in source and binary forms, with or 612771Sqtt2@cornell.edu * without modification, are permitted provided that the following 712771Sqtt2@cornell.edu * conditions are met: 812771Sqtt2@cornell.edu * 912771Sqtt2@cornell.edu * Redistributions of source code must retain the above copyright 1012771Sqtt2@cornell.edu * notice, this list of conditions and the following disclaimer. 1112771Sqtt2@cornell.edu * 1212771Sqtt2@cornell.edu * Redistributions in binary form must reproduce the above 1312771Sqtt2@cornell.edu * copyright notice, this list of conditions and the following 1412771Sqtt2@cornell.edu * disclaimer in the documentation and/or other materials provided 1512771Sqtt2@cornell.edu * with the distribution. 1612771Sqtt2@cornell.edu * 1712771Sqtt2@cornell.edu * Neither the name of Cornell University nor the names of its 1812771Sqtt2@cornell.edu * contributors may be used to endorse or promote products derived 1912771Sqtt2@cornell.edu * from this software without specific prior written permission. 2012771Sqtt2@cornell.edu * 2112771Sqtt2@cornell.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 2212771Sqtt2@cornell.edu * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 2312771Sqtt2@cornell.edu * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 2412771Sqtt2@cornell.edu * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 2512771Sqtt2@cornell.edu * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 2612771Sqtt2@cornell.edu * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2712771Sqtt2@cornell.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2812771Sqtt2@cornell.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 2912771Sqtt2@cornell.edu * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 3012771Sqtt2@cornell.edu * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3112771Sqtt2@cornell.edu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 3212771Sqtt2@cornell.edu * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3312771Sqtt2@cornell.edu * POSSIBILITY OF SUCH DAMAGE. 3412771Sqtt2@cornell.edu * 3512771Sqtt2@cornell.edu * Authors: Tuan Ta 3612771Sqtt2@cornell.edu */ 3712771Sqtt2@cornell.edu 3812771Sqtt2@cornell.edu//------------------------------------------------------------------------ 3912771Sqtt2@cornell.edu// sysfutex1_d tests basic functionalities of futex system call: 4012771Sqtt2@cornell.edu// - make some threads wait on a variable 4112771Sqtt2@cornell.edu// - wake up all threads waiting on a variable 4212771Sqtt2@cornell.edu//------------------------------------------------------------------------ 4312771Sqtt2@cornell.edu 4412771Sqtt2@cornell.edu#include "riscv_test.h" 4512771Sqtt2@cornell.edu#include "test_macros.h" 4612771Sqtt2@cornell.edu#include "test_macros_mt_ecall.h" 4712771Sqtt2@cornell.edu 4812771Sqtt2@cornell.edu RVTEST_RV64U 4912771Sqtt2@cornell.edu RVTEST_CODE_BEGIN 5012771Sqtt2@cornell.edu 5112771Sqtt2@cornell.edu#define MAX_NUM_THREADS 20 5212771Sqtt2@cornell.edu 5312771Sqtt2@cornell.edu//------------------------------------------------------------------------ 5412771Sqtt2@cornell.edu// Master thread creates new threads, call _master function, waits for all 5512771Sqtt2@cornell.edu// threads to complete, deallocates threads and checks result 5612771Sqtt2@cornell.edu//------------------------------------------------------------------------ 5712771Sqtt2@cornell.edu li a0, MAX_NUM_THREADS 5812771Sqtt2@cornell.edu call _create_threads 5912771Sqtt2@cornell.edu 6012771Sqtt2@cornell.edu la t6, n_worker_threads 6112771Sqtt2@cornell.edu ld a0, (t6) 6212771Sqtt2@cornell.edu beqz a0, _fail // exit if there's no worker thread 6312771Sqtt2@cornell.edu 6412771Sqtt2@cornell.edu call _master_work 6512771Sqtt2@cornell.edu 6612771Sqtt2@cornell.edu la t6, n_worker_threads 6712771Sqtt2@cornell.edu ld a0, (t6) 6812771Sqtt2@cornell.edu call _join 6912771Sqtt2@cornell.edu 7012771Sqtt2@cornell.edu la t6, n_worker_threads 7112771Sqtt2@cornell.edu ld a0, (t6) 7212771Sqtt2@cornell.edu call _check 7312771Sqtt2@cornell.edu 7412771Sqtt2@cornell.edu la t6, n_worker_threads 7512771Sqtt2@cornell.edu ld a0, (t6) 7612771Sqtt2@cornell.edu call _delete_threads 7712771Sqtt2@cornell.edu 7812771Sqtt2@cornell.edu li a0, SUCCESS 7912771Sqtt2@cornell.edu 8012771Sqtt2@cornell.edu RVTEST_CODE_END 8112771Sqtt2@cornell.edu 8212771Sqtt2@cornell.edu//------------------------------------------------------------------------ 8312771Sqtt2@cornell.edu// master_work function executed by the parent/master thread 8412771Sqtt2@cornell.edu// 8512771Sqtt2@cornell.edu// - wake up all threads waiting on futex_X 8612771Sqtt2@cornell.edu//------------------------------------------------------------------------ 8712771Sqtt2@cornell.edu_master_work: 8812771Sqtt2@cornell.edu mv s0, ra // save return address 8912771Sqtt2@cornell.edu li t0, 0 // number of threads that have been waken 9012771Sqtt2@cornell.edu la t1, n_worker_threads 9112771Sqtt2@cornell.edu ld t1, (t1) 9212771Sqtt2@cornell.edu 9312771Sqtt2@cornell.edu1: 9412771Sqtt2@cornell.edu // futex(futex_X, FUTEX_WAKE_PRIVATE, n_worker_threads) 9512771Sqtt2@cornell.edu la a0, futex_X 9612771Sqtt2@cornell.edu li a1, FUTEX_WAKE_PRIVATE 9712771Sqtt2@cornell.edu li a2, 1 // wake up at most 1 thread 9812771Sqtt2@cornell.edu li a7, SYSCALL_FUTEX 9912771Sqtt2@cornell.edu ecall 10012771Sqtt2@cornell.edu 10112771Sqtt2@cornell.edu add t0, t0, a0 // track the number of waken threads so far 10212771Sqtt2@cornell.edu 10312771Sqtt2@cornell.edu // keep waking up until all threads are waken up 10412771Sqtt2@cornell.edu blt t0, t1, 1b 10512771Sqtt2@cornell.edu 10612771Sqtt2@cornell.edu // restore return address and return 10712771Sqtt2@cornell.edu mv ra, s0 10812771Sqtt2@cornell.edu ret 10912771Sqtt2@cornell.edu 11012771Sqtt2@cornell.edu//------------------------------------------------------------------------ 11112771Sqtt2@cornell.edu// mt_test function executed by child threads 11212771Sqtt2@cornell.edu// 11312771Sqtt2@cornell.edu// Wait on futex_X 11412771Sqtt2@cornell.edu//------------------------------------------------------------------------ 11512771Sqtt2@cornell.edu_mt_test: 11612771Sqtt2@cornell.edu // futex(futex_X, FUTEX_WAIT_PRIVATE, 1) 11712771Sqtt2@cornell.edu la a0, futex_X 11812771Sqtt2@cornell.edu li a1, FUTEX_WAIT_PRIVATE 11912771Sqtt2@cornell.edu li a2, 0 // expected val of futex_X 12012771Sqtt2@cornell.edu li a7, SYSCALL_FUTEX 12112771Sqtt2@cornell.edu ecall 12212771Sqtt2@cornell.edu 12312771Sqtt2@cornell.edu RVTEST_CODE_END 12412771Sqtt2@cornell.edu 12512771Sqtt2@cornell.edu//------------------------------------------------------------------------ 12612771Sqtt2@cornell.edu// _check: 12712771Sqtt2@cornell.edu// Each thread should do LOOP_COUNT iterations 12812771Sqtt2@cornell.edu//------------------------------------------------------------------------ 12912771Sqtt2@cornell.edu 13012771Sqtt2@cornell.edu_check: 13112771Sqtt2@cornell.edu ret 13212771Sqtt2@cornell.edu 13312771Sqtt2@cornell.edu_fail: 13412771Sqtt2@cornell.edu li a0, FAILURE 13512771Sqtt2@cornell.edu RVTEST_CODE_END 13612771Sqtt2@cornell.edu 13712771Sqtt2@cornell.edu .data 13812771Sqtt2@cornell.edu 13912771Sqtt2@cornell.edufutex_X: .dword 0 14012771Sqtt2@cornell.edufutex_Y: .dword 0 14112771Sqtt2@cornell.edu 14212771Sqtt2@cornell.educount_master: .dword 0 14312771Sqtt2@cornell.educount_child: .dword 0 14412771Sqtt2@cornell.edu 14512771Sqtt2@cornell.eduMT_DATA 146