From: Jacob Lifshay Date: Fri, 1 Dec 2023 09:17:05 +0000 (-0800) Subject: openpower/test/elf/simple_cases: add some simple ELF test cases X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=404f6e2ff5b321137448fac1d5642fc6d8e45ad2;p=openpower-isa.git openpower/test/elf/simple_cases: add some simple ELF test cases --- diff --git a/src/openpower/decoder/isa/test_caller_elf_simple.py b/src/openpower/decoder/isa/test_caller_elf_simple.py new file mode 100644 index 00000000..d8559f7d --- /dev/null +++ b/src/openpower/decoder/isa/test_caller_elf_simple.py @@ -0,0 +1,24 @@ +""" ELF simple tests +""" + +import unittest + +from openpower.test.elf.simple_cases import SimpleCases +from openpower.test.runner import TestRunnerBase + +# writing the test_caller invocation this way makes it work with pytest + + +class TestELFSimple(TestRunnerBase): + def __init__(self, test): + assert test == 'test' + super().__init__(SimpleCases().test_data, + fp=True, use_syscall_emu=True) + + def test(self): + # dummy function to make unittest try to test this class + pass + + +if __name__ == "__main__": + unittest.main() diff --git a/src/openpower/test/elf/simple_cases.py b/src/openpower/test/elf/simple_cases.py new file mode 100644 index 00000000..9e59bc82 --- /dev/null +++ b/src/openpower/test/elf/simple_cases.py @@ -0,0 +1,80 @@ +# SPDX-License-Identifier: LGPLv3+ +# Copyright (C) 2023 Jacob Lifshay +# Funded by NLnet http://nlnet.nl +""" simple ELF test cases + +related bugs: + +* https://bugs.libre-soc.org/show_bug.cgi?id=1169 +""" + +from openpower.test.common import TestAccumulatorBase, skip_case +from openpower.test.state import ExpectedState +from openpower.test.elf import compile_elf +from openpower.consts import MSR, DEFAULT_MSR + +SYSCALL_DEF = r""" +#include + +long syscall(long number, ...); + +asm(".globl syscall\n" + ".p2align 4\n" + ".type syscall,@function\n" + "syscall:\n" + "mr 0,3\n" + "mr 3,4\n" + "mr 4,5\n" + "mr 5,6\n" + "mr 6,7\n" + "mr 7,8\n" + "mr 8,9\n" + "sc\n" + "blr"); +""" + +# we have to specify *all* sprs that our binary might possibly need to +# read, because ISACaller is annoying like that... +# https://bugs.libre-soc.org/show_bug.cgi?id=1226#c2 +_INITIAL_SPRS = ('LR', 'CTR', 'TAR', 'SVSTATE', 'SRR0', 'SRR1', + 'SVSHAPE0', 'SVSHAPE1', 'SVSHAPE2', 'SVSHAPE3') + +DEFAULT_USER_MSR = DEFAULT_MSR | (1 << MSR.PR) + +class SimpleCases(TestAccumulatorBase): + def case_hello_world(self): + prog = compile_elf(SYSCALL_DEF + r""" +void _start() { + static const char msg[] = "Hello World!\n"; + syscall(SYS_write, 1, (const void *)msg, sizeof(msg) - 1); + syscall(SYS_exit_group, 0); +} +""") + self.add_case(prog, initial_sprs=dict.fromkeys(_INITIAL_SPRS, 0), + initial_msr=DEFAULT_USER_MSR) + + def case_hello_world_with_data_and_bss(self): + prog = compile_elf(SYSCALL_DEF + r""" +const char msg_in_ro_data[] = "World!\n"; +char msg_in_data[] = "Hello "; +char msg_in_bss[sizeof(msg_in_data)] = {}; + +void _start() { + for(int i = 0; i < sizeof(msg_in_data); i++) + msg_in_bss[i] = msg_in_data[i]; + syscall(SYS_write, 1, (const void *)msg_in_bss, sizeof(msg_in_data) - 1); + syscall(SYS_write, 1, (const void *)msg_in_ro_data, sizeof(msg_in_ro_data) - 1); + syscall(SYS_exit_group, 0); +} +""") + self.add_case(prog, initial_sprs=dict.fromkeys(_INITIAL_SPRS, 0), + initial_msr=DEFAULT_USER_MSR) + + def case_just_exit(self): + prog = compile_elf(SYSCALL_DEF + r""" +void _start() { + syscall(SYS_exit_group, 0); +} +""") + self.add_case(prog, initial_sprs=dict.fromkeys(_INITIAL_SPRS, 0), + initial_msr=DEFAULT_USER_MSR)