f70467cce97574d0557cec7d2ff46933763afb99
[openpower-isa.git] / src / openpower / test / elf / simple_cases.py
1 # SPDX-License-Identifier: LGPLv3+
2 # Copyright (C) 2023 Jacob Lifshay <programmerjake@gmail.com>
3 # Funded by NLnet http://nlnet.nl
4 """ simple ELF test cases
5
6 related bugs:
7
8 * https://bugs.libre-soc.org/show_bug.cgi?id=1169
9 """
10
11 from openpower.test.common import TestAccumulatorBase, skip_case
12 from openpower.test.state import ExpectedState
13 from openpower.test.elf import compile_elf
14 from openpower.consts import MSR, DEFAULT_MSR
15 from copy import deepcopy
16
17 SYSCALL_DEF = r"""
18 #include <sys/syscall.h>
19
20 long syscall(long number, ...);
21
22 asm(".globl syscall\n"
23 ".p2align 4\n"
24 ".type syscall,@function\n"
25 "syscall:\n"
26 "mr 0,3\n"
27 "mr 3,4\n"
28 "mr 4,5\n"
29 "mr 5,6\n"
30 "mr 6,7\n"
31 "mr 7,8\n"
32 "mr 8,9\n"
33 "sc\n"
34 "blr");
35 """
36
37 hello_world = r"""
38 void _start() {
39 static const char msg[] = "Hello World!\n";
40 syscall(SYS_write, 1, (const void *)msg, sizeof(msg) - 1);
41 syscall(SYS_exit_group, 0);
42 }
43 """
44
45 hello_word_data_bss = r"""
46 const char msg_in_ro_data[] = "World!\n";
47 char msg_in_data[] = "Hello ";
48 char msg_in_bss[sizeof(msg_in_data)] = {};
49
50 void _start() {
51 for(int i = 0; i < sizeof(msg_in_data); i++)
52 msg_in_bss[i] = msg_in_data[i];
53 syscall(SYS_write, 1, (const void *)msg_in_bss, sizeof(msg_in_data) - 1);
54 syscall(SYS_write, 1, (const void *)msg_in_ro_data, sizeof(msg_in_ro_data) - 1);
55 syscall(SYS_exit_group, 0);
56 }
57 """)
58
59 just_exit = r"""
60 void _start() {
61 syscall(SYS_exit_group, 0);
62 """
63
64 # we have to specify *all* sprs that our binary might possibly need to
65 # read, because ISACaller is annoying like that...
66 # https://bugs.libre-soc.org/show_bug.cgi?id=1226#c2
67 INITIAL_SPRS = ('LR', 'CTR', 'TAR', 'SVSTATE', 'SRR0', 'SRR1',
68 'SVSHAPE0', 'SVSHAPE1', 'SVSHAPE2', 'SVSHAPE3')
69 initial_sprs = dict.fromkeys(INITIAL_SPRS, 0)
70
71 DEFAULT_USER_MSR = DEFAULT_MSR | (1 << MSR.PR) # needs problem state
72
73 class SimpleCases(TestAccumulatorBase):
74 def case_hello_world(self):
75 prog = compile_elf(SYSCALL_DEF + hello_world)
76 self.add_case(prog, initial_sprs=deepcopy(initial_sprs),
77 initial_msr=DEFAULT_USER_MSR)
78
79 def case_hello_world_with_data_and_bss(self):
80 prog = compile_elf(SYSCALL_DEF + hello_word_data_bss)
81 self.add_case(prog, initial_sprs=deepcopy(initial_sprs),
82 initial_msr=DEFAULT_USER_MSR)
83
84 def case_just_exit(self):
85 prog = compile_elf(SYSCALL_DEF + just_exit)
86 self.add_case(prog, initial_sprs=deepcopy(initial_sprs),
87 initial_msr=DEFAULT_USER_MSR)