blob: ab6e4f0623c7f288e70f373b34f57427f61797a5 [file] [log] [blame]
Andrew Boie743e4682017-10-04 12:25:50 -07001/*
2 * Copyright (c) 2017 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +02007#include <zephyr/kernel.h>
Anas Nashif4e396172023-09-26 22:46:01 +00008#include <zephyr/internal/syscall_handler.h>
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +02009#include <zephyr/kernel_structs.h>
Daniel Leungd47b1c02023-09-27 13:06:16 -070010#include <zephyr/toolchain.h>
Andrew Boie743e4682017-10-04 12:25:50 -070011
Anas Nashifa6b49002023-09-26 21:37:25 +000012static struct k_object *validate_kernel_object(const void *obj,
Daniel Leungd47b1c02023-09-27 13:06:16 -070013 enum k_objects otype,
14 enum _obj_init_check init)
Andrew Boie7e3d3d72017-10-10 09:31:32 -070015{
Anas Nashifa6b49002023-09-26 21:37:25 +000016 struct k_object *ko;
Andrew Boie7e3d3d72017-10-10 09:31:32 -070017 int ret;
18
Anas Nashifc25d0802023-09-27 10:49:28 +000019 ko = k_object_find(obj);
Andrew Boie7e3d3d72017-10-10 09:31:32 -070020
21 /* This can be any kernel object and it doesn't have to be
22 * initialized
23 */
Anas Nashif21254b22023-09-27 10:50:26 +000024 ret = k_object_validate(ko, K_OBJ_ANY, _OBJ_INIT_ANY);
Flavio Ceolin76b35182018-12-16 12:48:29 -080025 if (ret != 0) {
Andrew Boiecb1dd742019-10-01 10:28:32 -070026#ifdef CONFIG_LOG
Anas Nashif3ab35662023-09-27 10:51:23 +000027 k_object_dump_error(ret, obj, ko, otype);
Simon Heinbcd1d192024-03-08 12:00:10 +010028#endif /* CONFIG_LOG */
Andrew Boie7e3d3d72017-10-10 09:31:32 -070029 return NULL;
30 }
31
32 return ko;
33}
34
Anas Nashifd178b6a2023-09-27 10:44:21 +000035static ALWAYS_INLINE struct k_object *validate_any_object(const void *obj)
Daniel Leungd47b1c02023-09-27 13:06:16 -070036{
37 return validate_kernel_object(obj, K_OBJ_ANY, _OBJ_INIT_ANY);
38}
39
40bool k_object_is_valid(const void *obj, enum k_objects otype)
41{
Anas Nashifd178b6a2023-09-27 10:44:21 +000042 struct k_object *ko;
Daniel Leungd47b1c02023-09-27 13:06:16 -070043
44 ko = validate_kernel_object(obj, otype, _OBJ_INIT_TRUE);
45
46 return (ko != NULL);
47}
48
Andrew Boie743e4682017-10-04 12:25:50 -070049/* Normally these would be included in userspace.c, but the way
50 * syscall_dispatch.c declares weak handlers results in build errors if these
51 * are located in userspace.c. Just put in a separate file.
Andrew Boie7e3d3d72017-10-10 09:31:32 -070052 *
Anas Nashifc25d0802023-09-27 10:49:28 +000053 * To avoid double k_object_find() lookups, we don't call the implementation
Andrew Boie7e3d3d72017-10-10 09:31:32 -070054 * function, but call a level deeper.
Andrew Boie743e4682017-10-04 12:25:50 -070055 */
Peter Bigot2fcf7622020-05-14 05:06:08 -050056static inline void z_vrfy_k_object_access_grant(const void *object,
Andy Ross643701a2019-08-13 12:58:38 -070057 struct k_thread *thread)
Andrew Boie743e4682017-10-04 12:25:50 -070058{
Anas Nashifa6b49002023-09-26 21:37:25 +000059 struct k_object *ko;
Andrew Boie743e4682017-10-04 12:25:50 -070060
Anas Nashifa08bfeb2023-09-27 11:20:28 +000061 K_OOPS(K_SYSCALL_OBJ_INIT(thread, K_OBJ_THREAD));
Andy Ross65649742019-08-06 13:34:31 -070062 ko = validate_any_object(object);
Anas Nashifa08bfeb2023-09-27 11:20:28 +000063 K_OOPS(K_SYSCALL_VERIFY_MSG(ko != NULL, "object %p access denied",
Andy Ross65649742019-08-06 13:34:31 -070064 object));
Anas Nashif993f9032023-09-27 10:47:01 +000065 k_thread_perms_set(ko, thread);
Andrew Boie743e4682017-10-04 12:25:50 -070066}
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +080067#include <zephyr/syscalls/k_object_access_grant_mrsh.c>
Andrew Boie743e4682017-10-04 12:25:50 -070068
Peter Bigot2fcf7622020-05-14 05:06:08 -050069static inline void z_vrfy_k_object_release(const void *object)
Andrew Boiea89bf012017-10-09 14:47:55 -070070{
Anas Nashifa6b49002023-09-26 21:37:25 +000071 struct k_object *ko;
Andrew Boiea89bf012017-10-09 14:47:55 -070072
Hess Nathane05c4a82024-05-07 13:22:50 +020073 ko = validate_any_object(object);
74 K_OOPS(K_SYSCALL_VERIFY_MSG(ko != NULL, "object %p access denied", object));
Nicolas Pitre46aa6712025-01-07 12:00:43 -050075 k_thread_perms_clear(ko, _current);
Andrew Boiea89bf012017-10-09 14:47:55 -070076}
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +080077#include <zephyr/syscalls/k_object_release_mrsh.c>
Andrew Boie97bf0012018-04-24 17:01:37 -070078
Andy Ross65649742019-08-06 13:34:31 -070079static inline void *z_vrfy_k_object_alloc(enum k_objects otype)
Andrew Boie97bf0012018-04-24 17:01:37 -070080{
Andy Ross65649742019-08-06 13:34:31 -070081 return z_impl_k_object_alloc(otype);
Andrew Boie97bf0012018-04-24 17:01:37 -070082}
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +080083#include <zephyr/syscalls/k_object_alloc_mrsh.c>
Flavio Ceolin67e66e42023-06-22 06:27:28 +000084
85static inline void *z_vrfy_k_object_alloc_size(enum k_objects otype, size_t size)
86{
87 return z_impl_k_object_alloc_size(otype, size);
88}
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +080089#include <zephyr/syscalls/k_object_alloc_size_mrsh.c>