blob: cbec3245f47190925a985e1b19f0eb5fb079152f [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
Patrik Flykt4344e272019-03-08 14:19:05 -070019 ko = z_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 */
Daniel Leungd47b1c02023-09-27 13:06:16 -070024 ret = z_object_validate(ko, otype, init);
Flavio Ceolin76b35182018-12-16 12:48:29 -080025 if (ret != 0) {
Andrew Boiecb1dd742019-10-01 10:28:32 -070026#ifdef CONFIG_LOG
Daniel Leungd47b1c02023-09-27 13:06:16 -070027 z_dump_object_error(ret, obj, ko, otype);
Andrew Boie7e3d3d72017-10-10 09:31:32 -070028#endif
29 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 *
Patrik Flykt4344e272019-03-08 14:19:05 -070053 * To avoid double z_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
Andrew Boie8345e5e2018-05-04 15:57:57 -070061 Z_OOPS(Z_SYSCALL_OBJ_INIT(thread, K_OBJ_THREAD));
Andy Ross65649742019-08-06 13:34:31 -070062 ko = validate_any_object(object);
Anas Nashif684b8fc2023-09-27 10:41:51 +000063 Z_OOPS(K_SYSCALL_VERIFY_MSG(ko != NULL, "object %p access denied",
Andy Ross65649742019-08-06 13:34:31 -070064 object));
65 z_thread_perms_set(ko, thread);
Andrew Boie743e4682017-10-04 12:25:50 -070066}
Andy Ross65649742019-08-06 13:34:31 -070067#include <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
Andrew Boiea89bf012017-10-09 14:47:55 -070073 ko = validate_any_object((void *)object);
Anas Nashif684b8fc2023-09-27 10:41:51 +000074 Z_OOPS(K_SYSCALL_VERIFY_MSG(ko != NULL, "object %p access denied",
Andrew Boie8345e5e2018-05-04 15:57:57 -070075 (void *)object));
Patrik Flykt4344e272019-03-08 14:19:05 -070076 z_thread_perms_clear(ko, _current);
Andrew Boiea89bf012017-10-09 14:47:55 -070077}
Andy Ross65649742019-08-06 13:34:31 -070078#include <syscalls/k_object_release_mrsh.c>
Andrew Boie97bf0012018-04-24 17:01:37 -070079
Andy Ross65649742019-08-06 13:34:31 -070080static inline void *z_vrfy_k_object_alloc(enum k_objects otype)
Andrew Boie97bf0012018-04-24 17:01:37 -070081{
Andy Ross65649742019-08-06 13:34:31 -070082 return z_impl_k_object_alloc(otype);
Andrew Boie97bf0012018-04-24 17:01:37 -070083}
Andy Ross65649742019-08-06 13:34:31 -070084#include <syscalls/k_object_alloc_mrsh.c>
Flavio Ceolin67e66e42023-06-22 06:27:28 +000085
86static inline void *z_vrfy_k_object_alloc_size(enum k_objects otype, size_t size)
87{
88 return z_impl_k_object_alloc_size(otype, size);
89}
90#include <syscalls/k_object_alloc_size_mrsh.c>