1 /*
2  * Copyright (c) 2016-2020 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief Measure time
10  *
11  */
12 #include <zephyr/kernel.h>
13 #include <ksched.h>
14 #include <zephyr/internal/syscall_handler.h>
15 #include <zephyr/app_memory/app_memdomain.h>
16 
17 #include "footprint.h"
18 #include "userspace.h"
19 
20 #define STACK_SIZE	512
21 
22 K_SEM_DEFINE(test_sema, 1, 10);
23 
24 struct k_thread my_thread_user;
25 
z_impl_dummy_syscall(void)26 int z_impl_dummy_syscall(void)
27 {
28 	return 0;
29 }
30 
z_vrfy_dummy_syscall(void)31 static inline int z_vrfy_dummy_syscall(void)
32 {
33 	return 0;
34 }
35 #include <zephyr/syscalls/dummy_syscall_mrsh.c>
36 
z_impl_validation_overhead_syscall(void)37 int z_impl_validation_overhead_syscall(void)
38 {
39 	return 0;
40 }
41 
z_vrfy_validation_overhead_syscall(void)42 static inline int z_vrfy_validation_overhead_syscall(void)
43 {
44 	bool status_0 = K_SYSCALL_OBJ_INIT(&test_sema, K_OBJ_SEM);
45 
46 	bool status_1 = K_SYSCALL_OBJ(&test_sema, K_OBJ_SEM);
47 
48 	return status_0 || status_1;
49 }
50 #include <zephyr/syscalls/validation_overhead_syscall_mrsh.c>
51 
52 
test_drop_to_user_mode_1(void * p1,void * p2,void * p3)53 void test_drop_to_user_mode_1(void *p1, void *p2, void *p3)
54 {
55 	volatile uint32_t dummy = 100U;
56 
57 	dummy++;
58 }
59 
drop_to_user_mode_thread(void * p1,void * p2,void * p3)60 void drop_to_user_mode_thread(void *p1, void *p2, void *p3)
61 {
62 	k_thread_user_mode_enter(test_drop_to_user_mode_1, NULL, NULL, NULL);
63 }
64 
drop_to_user_mode(void)65 void drop_to_user_mode(void)
66 {
67 	k_thread_create(&my_thread_user, my_stack_area_0, STACK_SIZE,
68 			drop_to_user_mode_thread,
69 			NULL, NULL, NULL,
70 			-1, K_INHERIT_PERMS, K_NO_WAIT);
71 
72 	k_yield();
73 }
74 
user_thread_creation(void)75 void user_thread_creation(void)
76 {
77 	k_thread_create(&my_thread_user, my_stack_area, STACK_SIZE,
78 			test_drop_to_user_mode_1,
79 			NULL, NULL, NULL,
80 			0, K_INHERIT_PERMS | K_USER, K_FOREVER);
81 
82 	k_thread_abort(&my_thread_user);
83 }
84 
syscall_overhead_user_thread(void * p1,void * p2,void * p3)85 void syscall_overhead_user_thread(void *p1, void *p2, void *p3)
86 {
87 	int val = dummy_syscall();
88 
89 	val |= 0xFF;
90 }
91 
syscall_overhead(void)92 void syscall_overhead(void)
93 {
94 	k_thread_create(&my_thread_user, my_stack_area_0, STACK_SIZE,
95 			syscall_overhead_user_thread,
96 			NULL, NULL, NULL,
97 			-1, K_INHERIT_PERMS | K_USER, K_NO_WAIT);
98 }
99 
validation_overhead_user_thread(void * p1,void * p2,void * p3)100 void validation_overhead_user_thread(void *p1, void *p2, void *p3)
101 {
102 	/* get validation numbers */
103 	validation_overhead_syscall();
104 }
105 
validation_overhead(void)106 void validation_overhead(void)
107 {
108 	k_thread_access_grant(k_current_get(), &test_sema);
109 
110 	k_thread_create(&my_thread_user, my_stack_area, STACK_SIZE,
111 			validation_overhead_user_thread,
112 			NULL, NULL, NULL,
113 			-1 /*priority*/, K_INHERIT_PERMS | K_USER, K_NO_WAIT);
114 }
115 
run_userspace(void)116 void run_userspace(void)
117 {
118 	k_mem_domain_add_thread(&footprint_mem_domain, k_current_get());
119 
120 	drop_to_user_mode();
121 
122 	user_thread_creation();
123 
124 	syscall_overhead();
125 
126 	validation_overhead();
127 
128 }
129