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 
preface(void)36 void preface(void)
37 {
38 	zassert_true(expect_preface, "%s unexpectedly called", __func__);
39 	expect_preface = false;
40 	preface_called = true;
41 	expect_foo1 = true;
42 }
43 
foo1(uint32_t arg1,uint32_t arg2,uint32_t arg3,uint32_t arg4)44 uint32_t foo1(uint32_t arg1, uint32_t arg2, uint32_t arg3, uint32_t arg4)
45 {
46 	zassert_true(expect_foo1, "%s unexpectedly called", __func__);
47 	zassert_equal(arg1, foo1_arg1, "Was 0x%" PRIx32 ", expected 0x%" PRIx32, arg1, foo1_arg1);
48 	zassert_equal(arg2, foo1_arg2);
49 	zassert_equal(arg3, foo1_arg3);
50 	zassert_equal(arg4, foo1_arg4);
51 	expect_foo1 = false;
52 	foo1_called = true;
53 	expect_postface = true;
54 	return foo1_retval;
55 }
56 
postface(void)57 void postface(void)
58 {
59 	zassert_true(expect_postface, "%s unexpectedly called", __func__);
60 	expect_postface = false;
61 	postface_called = true;
62 }
63 
wrap_foo1(uint32_t arg1,uint32_t arg2,uint32_t arg3,uint32_t arg4)64 uint32_t __attribute__((naked)) wrap_foo1(uint32_t arg1, uint32_t arg2, uint32_t arg3,
65 					  uint32_t arg4)
66 {
67 	__TZ_WRAP_FUNC(preface, foo1, postface);
68 }
69 
ZTEST(tz_wrap_func,test_tz_wrap_func)70 ZTEST(tz_wrap_func, test_tz_wrap_func)
71 {
72 	reset_mocks();
73 	foo1_retval = 0x01234567;
74 	foo1_arg1 = 0x12345678;
75 	foo1_arg2 = 0x23456789;
76 	foo1_arg3 = 0x3456789a;
77 	foo1_arg4 = 0x456789ab;
78 
79 	uint32_t msp1, psp1;
80 	msp1 = __get_MSP();
81 	psp1 = __get_PSP();
82 
83 	zassert_equal(foo1_retval, wrap_foo1(foo1_arg1, foo1_arg2, foo1_arg3, foo1_arg4), NULL);
84 
85 	zassert_equal(msp1, __get_MSP());
86 	zassert_equal(psp1, __get_PSP());
87 
88 	zassert_true(preface_called);
89 	zassert_true(foo1_called);
90 	zassert_true(postface_called);
91 	zassert_false(expect_preface);
92 	zassert_false(expect_foo1);
93 	zassert_false(expect_postface);
94 }
95 
96 ZTEST_SUITE(tz_wrap_func, NULL, NULL, NULL, NULL, NULL);
97