blob: 7919a0748224b2913a1639112610a527627c85d2 [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
7#include <kernel.h>
8#include <syscall_handler.h>
Andrew Boiee9cfc542018-04-13 13:15:28 -07009#include <kernel_structs.h>
Andrew Boie743e4682017-10-04 12:25:50 -070010
Andrew Boie7e3d3d72017-10-10 09:31:32 -070011static struct _k_object *validate_any_object(void *obj)
12{
13 struct _k_object *ko;
14 int ret;
15
Patrik Flykt4344e272019-03-08 14:19:05 -070016 ko = z_object_find(obj);
Andrew Boie7e3d3d72017-10-10 09:31:32 -070017
18 /* This can be any kernel object and it doesn't have to be
19 * initialized
20 */
Patrik Flykt4344e272019-03-08 14:19:05 -070021 ret = z_object_validate(ko, K_OBJ_ANY, _OBJ_INIT_ANY);
Flavio Ceolin76b35182018-12-16 12:48:29 -080022 if (ret != 0) {
Andrew Boie7e3d3d72017-10-10 09:31:32 -070023#ifdef CONFIG_PRINTK
Patrik Flykt4344e272019-03-08 14:19:05 -070024 z_dump_object_error(ret, obj, ko, K_OBJ_ANY);
Andrew Boie7e3d3d72017-10-10 09:31:32 -070025#endif
26 return NULL;
27 }
28
29 return ko;
30}
31
Andrew Boie743e4682017-10-04 12:25:50 -070032/* 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.
Andrew Boie7e3d3d72017-10-10 09:31:32 -070035 *
Patrik Flykt4344e272019-03-08 14:19:05 -070036 * To avoid double z_object_find() lookups, we don't call the implementation
Andrew Boie7e3d3d72017-10-10 09:31:32 -070037 * function, but call a level deeper.
Andrew Boie743e4682017-10-04 12:25:50 -070038 */
Andrew Boie8345e5e2018-05-04 15:57:57 -070039Z_SYSCALL_HANDLER(k_object_access_grant, object, thread)
Andrew Boie743e4682017-10-04 12:25:50 -070040{
Andrew Boie7e3d3d72017-10-10 09:31:32 -070041 struct _k_object *ko;
Andrew Boie743e4682017-10-04 12:25:50 -070042
Andrew Boie8345e5e2018-05-04 15:57:57 -070043 Z_OOPS(Z_SYSCALL_OBJ_INIT(thread, K_OBJ_THREAD));
Andrew Boie7e3d3d72017-10-10 09:31:32 -070044 ko = validate_any_object((void *)object);
Flavio Ceolin92ea2f92018-09-20 16:14:57 -070045 Z_OOPS(Z_SYSCALL_VERIFY_MSG(ko != NULL, "object %p access denied",
Andrew Boie8345e5e2018-05-04 15:57:57 -070046 (void *)object));
Patrik Flykt4344e272019-03-08 14:19:05 -070047 z_thread_perms_set(ko, (struct k_thread *)thread);
Andrew Boie7e3d3d72017-10-10 09:31:32 -070048
Andrew Boie743e4682017-10-04 12:25:50 -070049 return 0;
50}
51
Andrew Boie8345e5e2018-05-04 15:57:57 -070052Z_SYSCALL_HANDLER(k_object_release, object)
Andrew Boiea89bf012017-10-09 14:47:55 -070053{
54 struct _k_object *ko;
55
Andrew Boiea89bf012017-10-09 14:47:55 -070056 ko = validate_any_object((void *)object);
Flavio Ceolin92ea2f92018-09-20 16:14:57 -070057 Z_OOPS(Z_SYSCALL_VERIFY_MSG(ko != NULL, "object %p access denied",
Andrew Boie8345e5e2018-05-04 15:57:57 -070058 (void *)object));
Patrik Flykt4344e272019-03-08 14:19:05 -070059 z_thread_perms_clear(ko, _current);
Andrew Boiea89bf012017-10-09 14:47:55 -070060
61 return 0;
62}
Andrew Boie97bf0012018-04-24 17:01:37 -070063
Andrew Boie8345e5e2018-05-04 15:57:57 -070064Z_SYSCALL_HANDLER(k_object_alloc, otype)
Andrew Boie97bf0012018-04-24 17:01:37 -070065{
Andrew Boie8345e5e2018-05-04 15:57:57 -070066 Z_OOPS(Z_SYSCALL_VERIFY_MSG(otype > K_OBJ_ANY && otype < K_OBJ_LAST &&
67 otype != K_OBJ__THREAD_STACK_ELEMENT,
68 "bad object type %d requested", otype));
Andrew Boie97bf0012018-04-24 17:01:37 -070069
Patrik Flykt4344e272019-03-08 14:19:05 -070070 return (u32_t)z_impl_k_object_alloc(otype);
Andrew Boie97bf0012018-04-24 17:01:37 -070071}