1 /*
2  * Copyright (c) 2020 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 #include <cortex_m/tz_ns.h>
9 #include <cmsis_core.h>
10 
11 static bool expect_preface;
12 static bool expect_postface;
13 static bool expect_foo1;
14 static bool preface_called;
15 static bool postface_called;
16 static bool foo1_called;
17 static uint32_t foo1_retval;
18 static uint32_t foo1_arg1;
19 static uint32_t foo1_arg2;
20 static uint32_t foo1_arg3;
21 static uint32_t foo1_arg4;
22 
reset_mocks(void)23 void reset_mocks(void)
24 {
25 	expect_preface = true;
26 	foo1_called = false;
27 	preface_called = false;
28 	postface_called = false;
29 	foo1_retval = 0;
30 	foo1_arg1 = 0;
31 	foo1_arg2 = 0;
32 	foo1_arg3 = 0;
33 	foo1_arg4 = 0;
34 }
35 
36 
preface(void)37 void preface(void)
38 {
39 	zassert_true(expect_preface, "%s unexpectedly called", __func__);
40 	expect_preface = false;
41 	preface_called = true;
42 	expect_foo1 = true;
43 }
44 
45 
foo1(uint32_t arg1,uint32_t arg2,uint32_t arg3,uint32_t arg4)46 uint32_t foo1(uint32_t arg1, uint32_t arg2, uint32_t arg3, uint32_t arg4)
47 {
48 	zassert_true(expect_foo1, "%s unexpectedly called", __func__);
49 	zassert_equal(arg1, foo1_arg1, "Was 0x%"PRIx32", expected 0x%"PRIx32,
50 		arg1, foo1_arg1);
51 	zassert_equal(arg2, foo1_arg2);
52 	zassert_equal(arg3, foo1_arg3);
53 	zassert_equal(arg4, foo1_arg4);
54 	expect_foo1 = false;
55 	foo1_called = true;
56 	expect_postface = true;
57 	return foo1_retval;
58 }
59 
60 
postface(void)61 void postface(void)
62 {
63 	zassert_true(expect_postface, "%s unexpectedly called", __func__);
64 	expect_postface = false;
65 	postface_called = true;
66 }
67 
68 
wrap_foo1(uint32_t arg1,uint32_t arg2,uint32_t arg3,uint32_t arg4)69 uint32_t __attribute__((naked)) wrap_foo1(uint32_t arg1, uint32_t arg2,
70 				uint32_t arg3, uint32_t arg4)
71 {
72 	__TZ_WRAP_FUNC(preface, foo1, postface);
73 }
74 
75 
ZTEST(tz_wrap_func,test_tz_wrap_func)76 ZTEST(tz_wrap_func, test_tz_wrap_func)
77 {
78 	reset_mocks();
79 	foo1_retval = 0x01234567;
80 	foo1_arg1 = 0x12345678;
81 	foo1_arg2 = 0x23456789;
82 	foo1_arg3 = 0x3456789a;
83 	foo1_arg4 = 0x456789ab;
84 
85 	uint32_t msp1, psp1;
86 	msp1 = __get_MSP();
87 	psp1 = __get_PSP();
88 
89 	zassert_equal(foo1_retval,
90 		wrap_foo1(foo1_arg1, foo1_arg2, foo1_arg3, foo1_arg4), NULL);
91 
92 	zassert_equal(msp1, __get_MSP());
93 	zassert_equal(psp1, __get_PSP());
94 
95 	zassert_true(preface_called);
96 	zassert_true(foo1_called);
97 	zassert_true(postface_called);
98 	zassert_false(expect_preface);
99 	zassert_false(expect_foo1);
100 	zassert_false(expect_postface);
101 }
102 
103 ZTEST_SUITE(tz_wrap_func, NULL, NULL, NULL, NULL, NULL);
104