1 /*
2  * Copyright (c) 2017 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <kernel.h>
8 #include <syscall_handler.h>
9 #include <kernel_structs.h>
10 
validate_any_object(const void * obj)11 static struct z_object *validate_any_object(const void *obj)
12 {
13 	struct z_object *ko;
14 	int ret;
15 
16 	ko = z_object_find(obj);
17 
18 	/* This can be any kernel object and it doesn't have to be
19 	 * initialized
20 	 */
21 	ret = z_object_validate(ko, K_OBJ_ANY, _OBJ_INIT_ANY);
22 	if (ret != 0) {
23 #ifdef CONFIG_LOG
24 		z_dump_object_error(ret, obj, ko, K_OBJ_ANY);
25 #endif
26 		return NULL;
27 	}
28 
29 	return ko;
30 }
31 
32 /* Normally these would be included in userspace.c, but the way
33  * syscall_dispatch.c declares weak handlers results in build errors if these
34  * are located in userspace.c. Just put in a separate file.
35  *
36  * To avoid double z_object_find() lookups, we don't call the implementation
37  * function, but call a level deeper.
38  */
z_vrfy_k_object_access_grant(const void * object,struct k_thread * thread)39 static inline void z_vrfy_k_object_access_grant(const void *object,
40 						struct k_thread *thread)
41 {
42 	struct z_object *ko;
43 
44 	Z_OOPS(Z_SYSCALL_OBJ_INIT(thread, K_OBJ_THREAD));
45 	ko = validate_any_object(object);
46 	Z_OOPS(Z_SYSCALL_VERIFY_MSG(ko != NULL, "object %p access denied",
47 				    object));
48 	z_thread_perms_set(ko, thread);
49 }
50 #include <syscalls/k_object_access_grant_mrsh.c>
51 
z_vrfy_k_object_release(const void * object)52 static inline void z_vrfy_k_object_release(const void *object)
53 {
54 	struct z_object *ko;
55 
56 	ko = validate_any_object((void *)object);
57 	Z_OOPS(Z_SYSCALL_VERIFY_MSG(ko != NULL, "object %p access denied",
58 				    (void *)object));
59 	z_thread_perms_clear(ko, _current);
60 }
61 #include <syscalls/k_object_release_mrsh.c>
62 
z_vrfy_k_object_alloc(enum k_objects otype)63 static inline void *z_vrfy_k_object_alloc(enum k_objects otype)
64 {
65 	return z_impl_k_object_alloc(otype);
66 }
67 #include <syscalls/k_object_alloc_mrsh.c>
68