Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 Intel Corporation |
| 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
| 6 | |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 7 | #include <zephyr/kernel.h> |
Anas Nashif | 4e39617 | 2023-09-26 22:46:01 +0000 | [diff] [blame] | 8 | #include <zephyr/internal/syscall_handler.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 9 | #include <zephyr/kernel_structs.h> |
Daniel Leung | d47b1c0 | 2023-09-27 13:06:16 -0700 | [diff] [blame] | 10 | #include <zephyr/toolchain.h> |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 11 | |
Anas Nashif | a6b4900 | 2023-09-26 21:37:25 +0000 | [diff] [blame] | 12 | static struct k_object *validate_kernel_object(const void *obj, |
Daniel Leung | d47b1c0 | 2023-09-27 13:06:16 -0700 | [diff] [blame] | 13 | enum k_objects otype, |
| 14 | enum _obj_init_check init) |
Andrew Boie | 7e3d3d7 | 2017-10-10 09:31:32 -0700 | [diff] [blame] | 15 | { |
Anas Nashif | a6b4900 | 2023-09-26 21:37:25 +0000 | [diff] [blame] | 16 | struct k_object *ko; |
Andrew Boie | 7e3d3d7 | 2017-10-10 09:31:32 -0700 | [diff] [blame] | 17 | int ret; |
| 18 | |
Anas Nashif | c25d080 | 2023-09-27 10:49:28 +0000 | [diff] [blame] | 19 | ko = k_object_find(obj); |
Andrew Boie | 7e3d3d7 | 2017-10-10 09:31:32 -0700 | [diff] [blame] | 20 | |
| 21 | /* This can be any kernel object and it doesn't have to be |
| 22 | * initialized |
| 23 | */ |
Anas Nashif | 21254b2 | 2023-09-27 10:50:26 +0000 | [diff] [blame] | 24 | ret = k_object_validate(ko, K_OBJ_ANY, _OBJ_INIT_ANY); |
Flavio Ceolin | 76b3518 | 2018-12-16 12:48:29 -0800 | [diff] [blame] | 25 | if (ret != 0) { |
Andrew Boie | cb1dd74 | 2019-10-01 10:28:32 -0700 | [diff] [blame] | 26 | #ifdef CONFIG_LOG |
Anas Nashif | 3ab3566 | 2023-09-27 10:51:23 +0000 | [diff] [blame] | 27 | k_object_dump_error(ret, obj, ko, otype); |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 28 | #endif /* CONFIG_LOG */ |
Andrew Boie | 7e3d3d7 | 2017-10-10 09:31:32 -0700 | [diff] [blame] | 29 | return NULL; |
| 30 | } |
| 31 | |
| 32 | return ko; |
| 33 | } |
| 34 | |
Anas Nashif | d178b6a | 2023-09-27 10:44:21 +0000 | [diff] [blame] | 35 | static ALWAYS_INLINE struct k_object *validate_any_object(const void *obj) |
Daniel Leung | d47b1c0 | 2023-09-27 13:06:16 -0700 | [diff] [blame] | 36 | { |
| 37 | return validate_kernel_object(obj, K_OBJ_ANY, _OBJ_INIT_ANY); |
| 38 | } |
| 39 | |
| 40 | bool k_object_is_valid(const void *obj, enum k_objects otype) |
| 41 | { |
Anas Nashif | d178b6a | 2023-09-27 10:44:21 +0000 | [diff] [blame] | 42 | struct k_object *ko; |
Daniel Leung | d47b1c0 | 2023-09-27 13:06:16 -0700 | [diff] [blame] | 43 | |
| 44 | ko = validate_kernel_object(obj, otype, _OBJ_INIT_TRUE); |
| 45 | |
| 46 | return (ko != NULL); |
| 47 | } |
| 48 | |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 49 | /* 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 Boie | 7e3d3d7 | 2017-10-10 09:31:32 -0700 | [diff] [blame] | 52 | * |
Anas Nashif | c25d080 | 2023-09-27 10:49:28 +0000 | [diff] [blame] | 53 | * To avoid double k_object_find() lookups, we don't call the implementation |
Andrew Boie | 7e3d3d7 | 2017-10-10 09:31:32 -0700 | [diff] [blame] | 54 | * function, but call a level deeper. |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 55 | */ |
Peter Bigot | 2fcf762 | 2020-05-14 05:06:08 -0500 | [diff] [blame] | 56 | static inline void z_vrfy_k_object_access_grant(const void *object, |
Andy Ross | 643701a | 2019-08-13 12:58:38 -0700 | [diff] [blame] | 57 | struct k_thread *thread) |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 58 | { |
Anas Nashif | a6b4900 | 2023-09-26 21:37:25 +0000 | [diff] [blame] | 59 | struct k_object *ko; |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 60 | |
Anas Nashif | a08bfeb | 2023-09-27 11:20:28 +0000 | [diff] [blame] | 61 | K_OOPS(K_SYSCALL_OBJ_INIT(thread, K_OBJ_THREAD)); |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 62 | ko = validate_any_object(object); |
Anas Nashif | a08bfeb | 2023-09-27 11:20:28 +0000 | [diff] [blame] | 63 | K_OOPS(K_SYSCALL_VERIFY_MSG(ko != NULL, "object %p access denied", |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 64 | object)); |
Anas Nashif | 993f903 | 2023-09-27 10:47:01 +0000 | [diff] [blame] | 65 | k_thread_perms_set(ko, thread); |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 66 | } |
Yong Cong Sin | bbe5e1e | 2024-01-24 17:35:04 +0800 | [diff] [blame] | 67 | #include <zephyr/syscalls/k_object_access_grant_mrsh.c> |
Andrew Boie | 743e468 | 2017-10-04 12:25:50 -0700 | [diff] [blame] | 68 | |
Peter Bigot | 2fcf762 | 2020-05-14 05:06:08 -0500 | [diff] [blame] | 69 | static inline void z_vrfy_k_object_release(const void *object) |
Andrew Boie | a89bf01 | 2017-10-09 14:47:55 -0700 | [diff] [blame] | 70 | { |
Anas Nashif | a6b4900 | 2023-09-26 21:37:25 +0000 | [diff] [blame] | 71 | struct k_object *ko; |
Andrew Boie | a89bf01 | 2017-10-09 14:47:55 -0700 | [diff] [blame] | 72 | |
Hess Nathan | e05c4a8 | 2024-05-07 13:22:50 +0200 | [diff] [blame] | 73 | ko = validate_any_object(object); |
| 74 | K_OOPS(K_SYSCALL_VERIFY_MSG(ko != NULL, "object %p access denied", object)); |
Nicolas Pitre | 46aa671 | 2025-01-07 12:00:43 -0500 | [diff] [blame] | 75 | k_thread_perms_clear(ko, _current); |
Andrew Boie | a89bf01 | 2017-10-09 14:47:55 -0700 | [diff] [blame] | 76 | } |
Yong Cong Sin | bbe5e1e | 2024-01-24 17:35:04 +0800 | [diff] [blame] | 77 | #include <zephyr/syscalls/k_object_release_mrsh.c> |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 78 | |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 79 | static inline void *z_vrfy_k_object_alloc(enum k_objects otype) |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 80 | { |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 81 | return z_impl_k_object_alloc(otype); |
Andrew Boie | 97bf001 | 2018-04-24 17:01:37 -0700 | [diff] [blame] | 82 | } |
Yong Cong Sin | bbe5e1e | 2024-01-24 17:35:04 +0800 | [diff] [blame] | 83 | #include <zephyr/syscalls/k_object_alloc_mrsh.c> |
Flavio Ceolin | 67e66e4 | 2023-06-22 06:27:28 +0000 | [diff] [blame] | 84 | |
| 85 | static 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 Sin | bbe5e1e | 2024-01-24 17:35:04 +0800 | [diff] [blame] | 89 | #include <zephyr/syscalls/k_object_alloc_size_mrsh.c> |