blob: 000310c3ab069d78f1bc92bec982279286332508 [file] [log] [blame]
Andrew Boie945af952017-08-22 13:15:23 -07001/*
2 * Copyright (c) 2017 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +02008#include <zephyr/kernel.h>
Andrew Boie945af952017-08-22 13:15:23 -07009#include <string.h>
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +020010#include <zephyr/sys/math_extras.h>
11#include <zephyr/sys/rb.h>
12#include <zephyr/kernel_structs.h>
13#include <zephyr/sys/sys_io.h>
Andrew Boie5cfa5dc2017-08-30 14:17:44 -070014#include <ksched.h>
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +020015#include <zephyr/syscall.h>
16#include <zephyr/syscall_handler.h>
17#include <zephyr/device.h>
18#include <zephyr/init.h>
Flavio Ceolin8a148172018-12-16 12:39:44 -080019#include <stdbool.h>
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +020020#include <zephyr/app_memory/app_memdomain.h>
21#include <zephyr/sys/libc-hooks.h>
22#include <zephyr/sys/mutex.h>
Andrew Boie800b35f2019-11-05 09:27:18 -080023#include <inttypes.h>
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +020024#include <zephyr/linker/linker-defs.h>
Andrew Boie17ce8222019-02-21 13:44:54 -080025
Andrew Boie77070602019-02-27 20:12:40 -080026#ifdef Z_LIBC_PARTITION_EXISTS
Andrew Boie17ce8222019-02-21 13:44:54 -080027K_APPMEM_PARTITION_DEFINE(z_libc_partition);
Andrew Boie77070602019-02-27 20:12:40 -080028#endif
Anas Nashif0a0c8c82018-09-17 06:58:09 -050029
Andrew Boiee686aef2019-02-27 14:41:45 -080030/* TODO: Find a better place to put this. Since we pull the entire
Anas Nashif6e27d6d2019-05-09 08:43:30 -040031 * lib..__modules__crypto__mbedtls.a globals into app shared memory
32 * section, we can't put this in zephyr_init.c of the mbedtls module.
Andrew Boiee686aef2019-02-27 14:41:45 -080033 */
34#ifdef CONFIG_MBEDTLS
35K_APPMEM_PARTITION_DEFINE(k_mbedtls_partition);
36#endif
37
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +020038#include <zephyr/logging/log.h>
Krzysztof Chruscinski3ed80832020-11-26 19:32:34 +010039LOG_MODULE_DECLARE(os, CONFIG_KERNEL_LOG_LEVEL);
Anas Nashif0a0c8c82018-09-17 06:58:09 -050040
Andy Ross8a3d57b2019-02-06 09:10:36 -080041/* The originally synchronization strategy made heavy use of recursive
42 * irq_locking, which ports poorly to spinlocks which are
43 * non-recursive. Rather than try to redesign as part of
44 * spinlockification, this uses multiple locks to preserve the
45 * original semantics exactly. The locks are named for the data they
46 * protect where possible, or just for the code that uses them where
47 * not.
48 */
49#ifdef CONFIG_DYNAMIC_OBJECTS
Flavio Ceolin2b1106a2023-07-14 12:24:33 -070050static struct k_spinlock lists_lock; /* kobj dlist */
Andy Ross8a3d57b2019-02-06 09:10:36 -080051static struct k_spinlock objfree_lock; /* k_object_free */
Flavio Ceolin3b7e0b62023-06-23 09:34:14 -070052
53#ifdef CONFIG_GEN_PRIV_STACKS
54/* On ARM MPU we may have two different alignment requirement
55 * when dynamically allocating thread stacks, one for the privileged
56 * stack and other for the user stack, so we need to account the
57 * worst alignment scenario and reserve space for that.
58 */
59#ifdef CONFIG_ARM_MPU
60#define STACK_ELEMENT_DATA_SIZE(size) \
61 (sizeof(struct z_stack_data) + CONFIG_PRIVILEGED_STACK_SIZE + \
62 Z_THREAD_STACK_OBJ_ALIGN(size) + Z_THREAD_STACK_SIZE_ADJUST(size))
63#else
64#define STACK_ELEMENT_DATA_SIZE(size) (sizeof(struct z_stack_data) + \
65 Z_THREAD_STACK_SIZE_ADJUST(size))
66#endif /* CONFIG_ARM_MPU */
67#else
68#define STACK_ELEMENT_DATA_SIZE(size) Z_THREAD_STACK_SIZE_ADJUST(size)
69#endif /* CONFIG_GEN_PRIV_STACKS */
70
Andy Ross8a3d57b2019-02-06 09:10:36 -080071#endif
72static struct k_spinlock obj_lock; /* kobj struct data */
Andy Ross8a3d57b2019-02-06 09:10:36 -080073
Andrew Boie25742192017-10-16 15:29:30 -070074#define MAX_THREAD_BITS (CONFIG_MAX_THREAD_BYTES * 8)
75
Daniel Leunge58b6542018-08-08 11:23:16 -070076#ifdef CONFIG_DYNAMIC_OBJECTS
Kumar Galaa1b77fd2020-05-27 11:26:57 -050077extern uint8_t _thread_idx_map[CONFIG_MAX_THREAD_BYTES];
Daniel Leunge58b6542018-08-08 11:23:16 -070078#endif
79
Andrew Boie2dc2ecf2020-03-11 07:13:07 -070080static void clear_perms_cb(struct z_object *ko, void *ctx_ptr);
Daniel Leunge58b6542018-08-08 11:23:16 -070081
Andrew Boie945af952017-08-22 13:15:23 -070082const char *otype_to_str(enum k_objects otype)
83{
Flavio Ceolin3259ac02018-09-11 13:14:21 -070084 const char *ret;
Andrew Boie945af952017-08-22 13:15:23 -070085 /* -fdata-sections doesn't work right except in very very recent
86 * GCC and these literal strings would appear in the binary even if
87 * otype_to_str was omitted by the linker
88 */
Andrew Boiecb1dd742019-10-01 10:28:32 -070089#ifdef CONFIG_LOG
Andrew Boie945af952017-08-22 13:15:23 -070090 switch (otype) {
Leandro Pereira39dc7d02018-04-05 13:59:33 -070091 /* otype-to-str.h is generated automatically during build by
92 * gen_kobject_list.py
93 */
Andrew Boiebe919d32020-05-29 17:49:02 -070094 case K_OBJ_ANY:
95 ret = "generic";
96 break;
Leandro Pereira39dc7d02018-04-05 13:59:33 -070097#include <otype-to-str.h>
Andrew Boie945af952017-08-22 13:15:23 -070098 default:
Flavio Ceolin3259ac02018-09-11 13:14:21 -070099 ret = "?";
100 break;
Andrew Boie945af952017-08-22 13:15:23 -0700101 }
102#else
103 ARG_UNUSED(otype);
Maksim Masalskid6c9d402021-05-24 16:30:32 +0800104 ret = NULL;
Andrew Boie945af952017-08-22 13:15:23 -0700105#endif
Flavio Ceolin3259ac02018-09-11 13:14:21 -0700106 return ret;
Andrew Boie945af952017-08-22 13:15:23 -0700107}
108
Andrew Boie47f8fd12017-10-05 11:11:02 -0700109struct perm_ctx {
110 int parent_id;
111 int child_id;
112 struct k_thread *parent;
113};
114
Andrew Boie28be7932020-03-11 10:56:19 -0700115#ifdef CONFIG_GEN_PRIV_STACKS
Anas Nashifefbadbb2022-07-11 10:53:29 -0400116/* See write_gperf_table() in scripts/build/gen_kobject_list.py. The privilege
Andrew Boie28be7932020-03-11 10:56:19 -0700117 * mode stacks are allocated as an array. The base of the array is
118 * aligned to Z_PRIVILEGE_STACK_ALIGN, and all members must be as well.
119 */
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500120uint8_t *z_priv_stack_find(k_thread_stack_t *stack)
Andrew Boie28be7932020-03-11 10:56:19 -0700121{
122 struct z_object *obj = z_object_find(stack);
123
124 __ASSERT(obj != NULL, "stack object not found");
125 __ASSERT(obj->type == K_OBJ_THREAD_STACK_ELEMENT,
126 "bad stack object");
127
128 return obj->data.stack_data->priv;
129}
130#endif /* CONFIG_GEN_PRIV_STACKS */
131
Andrew Boie31bdfc02017-11-08 16:38:03 -0800132#ifdef CONFIG_DYNAMIC_OBJECTS
Daniel Leungfe477ea2020-12-15 13:50:48 -0800133
134/*
135 * Note that dyn_obj->data is where the kernel object resides
136 * so it is the one that actually needs to be aligned.
137 * Due to the need to get the the fields inside struct dyn_obj
138 * from kernel object pointers (i.e. from data[]), the offset
139 * from data[] needs to be fixed at build time. Therefore,
140 * data[] is declared with __aligned(), such that when dyn_obj
141 * is allocated with alignment, data[] is also aligned.
142 * Due to this requirement, data[] needs to be aligned with
143 * the maximum alignment needed for all kernel objects
144 * (hence the following DYN_OBJ_DATA_ALIGN).
145 */
Peter Mitsis48f51642021-12-16 12:47:32 -0500146#ifdef ARCH_DYNAMIC_OBJ_K_THREAD_ALIGNMENT
147#define DYN_OBJ_DATA_ALIGN_K_THREAD (ARCH_DYNAMIC_OBJ_K_THREAD_ALIGNMENT)
Daniel Leungfe477ea2020-12-15 13:50:48 -0800148#else
149#define DYN_OBJ_DATA_ALIGN_K_THREAD (sizeof(void *))
150#endif
151
Flavio Ceolin3b7e0b62023-06-23 09:34:14 -0700152#ifdef CONFIG_DYNAMIC_THREAD_STACK_SIZE
153#ifndef CONFIG_MPU_STACK_GUARD
154#define DYN_OBJ_DATA_ALIGN_K_THREAD_STACK \
155 Z_THREAD_STACK_OBJ_ALIGN(CONFIG_PRIVILEGED_STACK_SIZE)
156#else
157#define DYN_OBJ_DATA_ALIGN_K_THREAD_STACK \
158 Z_THREAD_STACK_OBJ_ALIGN(CONFIG_DYNAMIC_THREAD_STACK_SIZE)
159#endif /* !CONFIG_MPU_STACK_GUARD */
160#else
161#define DYN_OBJ_DATA_ALIGN_K_THREAD_STACK \
162 Z_THREAD_STACK_OBJ_ALIGN(ARCH_STACK_PTR_ALIGN)
163#endif /* CONFIG_DYNAMIC_THREAD_STACK_SIZE */
164
Daniel Leungfe477ea2020-12-15 13:50:48 -0800165#define DYN_OBJ_DATA_ALIGN \
166 MAX(DYN_OBJ_DATA_ALIGN_K_THREAD, (sizeof(void *)))
167
Flavio Ceolin3b7e0b62023-06-23 09:34:14 -0700168struct dyn_obj {
Flavio Ceolincbbe6d22023-07-18 13:11:07 -0700169 struct z_object kobj;
170 sys_dnode_t dobj_list;
Flavio Ceolin3b7e0b62023-06-23 09:34:14 -0700171
172 /* The object itself */
173 void *data;
174};
175
Peter Bigot2fcf7622020-05-14 05:06:08 -0500176extern struct z_object *z_object_gperf_find(const void *obj);
Patrik Flykt4344e272019-03-08 14:19:05 -0700177extern void z_object_gperf_wordlist_foreach(_wordlist_cb_func_t func,
Andrew Boie31bdfc02017-11-08 16:38:03 -0800178 void *context);
179
Andrew Boie97bf0012018-04-24 17:01:37 -0700180/*
181 * Linked list of allocated kernel objects, for iteration over all allocated
182 * objects (and potentially deleting them during iteration).
183 */
184static sys_dlist_t obj_list = SYS_DLIST_STATIC_INIT(&obj_list);
185
186/*
Flavio Ceolin2b1106a2023-07-14 12:24:33 -0700187 * TODO: Write some hash table code that will replace obj_list.
Andrew Boie97bf0012018-04-24 17:01:37 -0700188 */
189
Andrew Boie31bdfc02017-11-08 16:38:03 -0800190static size_t obj_size_get(enum k_objects otype)
191{
Flavio Ceolin3259ac02018-09-11 13:14:21 -0700192 size_t ret;
193
Andrew Boie31bdfc02017-11-08 16:38:03 -0800194 switch (otype) {
Andrew Boie47fa8eb2018-05-16 10:11:17 -0700195#include <otype-to-size.h>
Andrew Boie31bdfc02017-11-08 16:38:03 -0800196 default:
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +0200197 ret = sizeof(const struct device);
Flavio Ceolin3259ac02018-09-11 13:14:21 -0700198 break;
Andrew Boie31bdfc02017-11-08 16:38:03 -0800199 }
Flavio Ceolin3259ac02018-09-11 13:14:21 -0700200
201 return ret;
Andrew Boie31bdfc02017-11-08 16:38:03 -0800202}
203
Daniel Leungfe477ea2020-12-15 13:50:48 -0800204static size_t obj_align_get(enum k_objects otype)
205{
206 size_t ret;
207
208 switch (otype) {
209 case K_OBJ_THREAD:
Peter Mitsis48f51642021-12-16 12:47:32 -0500210#ifdef ARCH_DYNAMIC_OBJ_K_THREAD_ALIGNMENT
211 ret = ARCH_DYNAMIC_OBJ_K_THREAD_ALIGNMENT;
Daniel Leungfe477ea2020-12-15 13:50:48 -0800212#else
Daniel Leungb6dd9602021-12-13 14:54:51 -0800213 ret = __alignof(struct dyn_obj);
Daniel Leungfe477ea2020-12-15 13:50:48 -0800214#endif
215 break;
216 default:
Daniel Leungb6dd9602021-12-13 14:54:51 -0800217 ret = __alignof(struct dyn_obj);
Daniel Leungfe477ea2020-12-15 13:50:48 -0800218 break;
219 }
220
221 return ret;
222}
223
Flavio Ceolincbbe6d22023-07-18 13:11:07 -0700224static struct dyn_obj *dyn_object_find(void *obj)
Andrew Boie31bdfc02017-11-08 16:38:03 -0800225{
Flavio Ceolincbbe6d22023-07-18 13:11:07 -0700226 struct dyn_obj *node;
Flavio Ceolin3b7e0b62023-06-23 09:34:14 -0700227 k_spinlock_key_t key;
Andrew Boie31bdfc02017-11-08 16:38:03 -0800228
229 /* For any dynamically allocated kernel object, the object
Naiyuan Tianbc3fda42021-08-23 23:32:58 +0800230 * pointer is just a member of the containing struct dyn_obj,
Andrew Boie31bdfc02017-11-08 16:38:03 -0800231 * so just a little arithmetic is necessary to locate the
232 * corresponding struct rbnode
233 */
Flavio Ceolin3b7e0b62023-06-23 09:34:14 -0700234 key = k_spin_lock(&lists_lock);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800235
Flavio Ceolin2b1106a2023-07-14 12:24:33 -0700236 SYS_DLIST_FOR_EACH_CONTAINER(&obj_list, node, dobj_list) {
237 if (node->kobj.name == obj) {
Flavio Ceolin3b7e0b62023-06-23 09:34:14 -0700238 goto end;
239 }
Andrew Boie31bdfc02017-11-08 16:38:03 -0800240 }
Flavio Ceolin3b7e0b62023-06-23 09:34:14 -0700241
242 /* No object found */
Flavio Ceolin2b1106a2023-07-14 12:24:33 -0700243 node = NULL;
Flavio Ceolin3b7e0b62023-06-23 09:34:14 -0700244
245 end:
Andy Ross8a3d57b2019-02-06 09:10:36 -0800246 k_spin_unlock(&lists_lock, key);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800247
Flavio Ceolin2b1106a2023-07-14 12:24:33 -0700248 return node;
Andrew Boie31bdfc02017-11-08 16:38:03 -0800249}
250
Daniel Leunge58b6542018-08-08 11:23:16 -0700251/**
252 * @internal
253 *
254 * @brief Allocate a new thread index for a new thread.
255 *
256 * This finds an unused thread index that can be assigned to a new
257 * thread. If too many threads have been allocated, the kernel will
258 * run out of indexes and this function will fail.
259 *
260 * Note that if an unused index is found, that index will be marked as
261 * used after return of this function.
262 *
263 * @param tidx The new thread index if successful
264 *
Flavio Ceolin8a148172018-12-16 12:39:44 -0800265 * @return true if successful, false if failed
Daniel Leunge58b6542018-08-08 11:23:16 -0700266 **/
Andrew Boie428afe52019-11-18 10:20:16 -0800267static bool thread_idx_alloc(uintptr_t *tidx)
Daniel Leunge58b6542018-08-08 11:23:16 -0700268{
269 int i;
270 int idx;
271 int base;
272
273 base = 0;
274 for (i = 0; i < CONFIG_MAX_THREAD_BYTES; i++) {
275 idx = find_lsb_set(_thread_idx_map[i]);
276
Flavio Ceolin76b35182018-12-16 12:48:29 -0800277 if (idx != 0) {
Daniel Leunge58b6542018-08-08 11:23:16 -0700278 *tidx = base + (idx - 1);
279
280 sys_bitfield_clear_bit((mem_addr_t)_thread_idx_map,
281 *tidx);
282
283 /* Clear permission from all objects */
Patrik Flykt4344e272019-03-08 14:19:05 -0700284 z_object_wordlist_foreach(clear_perms_cb,
Daniel Leunge58b6542018-08-08 11:23:16 -0700285 (void *)*tidx);
286
Flavio Ceolin8a148172018-12-16 12:39:44 -0800287 return true;
Daniel Leunge58b6542018-08-08 11:23:16 -0700288 }
289
290 base += 8;
291 }
292
Flavio Ceolin8a148172018-12-16 12:39:44 -0800293 return false;
Daniel Leunge58b6542018-08-08 11:23:16 -0700294}
295
296/**
297 * @internal
298 *
299 * @brief Free a thread index.
300 *
301 * This frees a thread index so it can be used by another
302 * thread.
303 *
304 * @param tidx The thread index to be freed
305 **/
Andrew Boie428afe52019-11-18 10:20:16 -0800306static void thread_idx_free(uintptr_t tidx)
Daniel Leunge58b6542018-08-08 11:23:16 -0700307{
308 /* To prevent leaked permission when index is recycled */
Patrik Flykt4344e272019-03-08 14:19:05 -0700309 z_object_wordlist_foreach(clear_perms_cb, (void *)tidx);
Daniel Leunge58b6542018-08-08 11:23:16 -0700310
311 sys_bitfield_set_bit((mem_addr_t)_thread_idx_map, tidx);
312}
313
Flavio Ceolin3b7e0b62023-06-23 09:34:14 -0700314static struct z_object *dynamic_object_create(enum k_objects otype, size_t align,
315 size_t size)
Andrew Boie31bdfc02017-11-08 16:38:03 -0800316{
Flavio Ceolincbbe6d22023-07-18 13:11:07 -0700317 struct dyn_obj *dyn;
318
319 dyn = z_thread_aligned_alloc(align, sizeof(struct dyn_obj));
320 if (dyn == NULL) {
321 return NULL;
322 }
Andrew Boie31bdfc02017-11-08 16:38:03 -0800323
Flavio Ceolin3b7e0b62023-06-23 09:34:14 -0700324 if (otype == K_OBJ_THREAD_STACK_ELEMENT) {
Flavio Ceolin3b7e0b62023-06-23 09:34:14 -0700325 size_t adjusted_size;
326
327 if (size == 0) {
Flavio Ceolincbbe6d22023-07-18 13:11:07 -0700328 k_free(dyn);
Flavio Ceolin3b7e0b62023-06-23 09:34:14 -0700329 return NULL;
330 }
331
332 adjusted_size = STACK_ELEMENT_DATA_SIZE(size);
Flavio Ceolincbbe6d22023-07-18 13:11:07 -0700333 dyn->data = z_thread_aligned_alloc(DYN_OBJ_DATA_ALIGN_K_THREAD_STACK,
Flavio Ceolin3b7e0b62023-06-23 09:34:14 -0700334 adjusted_size);
Flavio Ceolincbbe6d22023-07-18 13:11:07 -0700335 if (dyn->data == NULL) {
336 k_free(dyn);
Flavio Ceolin3b7e0b62023-06-23 09:34:14 -0700337 return NULL;
338 }
339
340#ifdef CONFIG_GEN_PRIV_STACKS
341 struct z_stack_data *stack_data = (struct z_stack_data *)
Flavio Ceolincbbe6d22023-07-18 13:11:07 -0700342 ((uint8_t *)dyn->data + adjusted_size - sizeof(*stack_data));
343 stack_data->priv = (uint8_t *)dyn->data;
Flavio Ceolin3b7e0b62023-06-23 09:34:14 -0700344 dyn->kobj.data.stack_data = stack_data;
345#ifdef CONFIG_ARM_MPU
346 dyn->kobj.name = (void *)ROUND_UP(
Flavio Ceolincbbe6d22023-07-18 13:11:07 -0700347 ((uint8_t *)dyn->data + CONFIG_PRIVILEGED_STACK_SIZE),
Flavio Ceolin3b7e0b62023-06-23 09:34:14 -0700348 Z_THREAD_STACK_OBJ_ALIGN(size));
349#else
Flavio Ceolincbbe6d22023-07-18 13:11:07 -0700350 dyn->kobj.name = dyn->data;
Flavio Ceolin3b7e0b62023-06-23 09:34:14 -0700351#endif
352#else
Flavio Ceolincbbe6d22023-07-18 13:11:07 -0700353 dyn->kobj.name = dyn->data;
Flavio Ceolin3b7e0b62023-06-23 09:34:14 -0700354#endif
355 } else {
Flavio Ceolincbbe6d22023-07-18 13:11:07 -0700356 dyn->data = z_thread_aligned_alloc(align, obj_size_get(otype) + size);
357 if (dyn->data == NULL) {
358 k_free(dyn->data);
Flavio Ceolin3b7e0b62023-06-23 09:34:14 -0700359 return NULL;
360 }
Flavio Ceolincbbe6d22023-07-18 13:11:07 -0700361 dyn->kobj.name = dyn->data;
Andrew Boie31bdfc02017-11-08 16:38:03 -0800362 }
363
Flavio Ceolin3b7e0b62023-06-23 09:34:14 -0700364 dyn->kobj.type = otype;
Spoorthy Priya Yerabolu9247e8b2020-08-25 03:11:16 -0700365 dyn->kobj.flags = 0;
366 (void)memset(dyn->kobj.perms, 0, CONFIG_MAX_THREAD_BYTES);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800367
Andy Ross8a3d57b2019-02-06 09:10:36 -0800368 k_spinlock_key_t key = k_spin_lock(&lists_lock);
369
Daniel Leungabfe0452021-04-27 11:49:30 -0700370 sys_dlist_append(&obj_list, &dyn->dobj_list);
Andy Ross8a3d57b2019-02-06 09:10:36 -0800371 k_spin_unlock(&lists_lock, key);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800372
Spoorthy Priya Yerabolu9247e8b2020-08-25 03:11:16 -0700373 return &dyn->kobj;
Andrew Boiebe919d32020-05-29 17:49:02 -0700374}
375
Flavio Ceolin3b7e0b62023-06-23 09:34:14 -0700376struct z_object *z_dynamic_object_aligned_create(size_t align, size_t size)
377{
378 struct z_object *obj = dynamic_object_create(K_OBJ_ANY, align, size);
379
380 if (obj == NULL) {
381 LOG_ERR("could not allocate kernel object, out of memory");
382 }
383
384 return obj;
385}
386
387static void *z_object_alloc(enum k_objects otype, size_t size)
Andrew Boiebe919d32020-05-29 17:49:02 -0700388{
389 struct z_object *zo;
Ioannis Glaropoulos8ada29e2020-06-11 09:53:01 +0200390 uintptr_t tidx = 0;
Andrew Boiebe919d32020-05-29 17:49:02 -0700391
392 if (otype <= K_OBJ_ANY || otype >= K_OBJ_LAST) {
393 LOG_ERR("bad object type %d requested", otype);
394 return NULL;
395 }
396
397 switch (otype) {
398 case K_OBJ_THREAD:
399 if (!thread_idx_alloc(&tidx)) {
400 LOG_ERR("out of free thread indexes");
401 return NULL;
402 }
403 break;
404 /* The following are currently not allowed at all */
405 case K_OBJ_FUTEX: /* Lives in user memory */
406 case K_OBJ_SYS_MUTEX: /* Lives in user memory */
Andrew Boiebe919d32020-05-29 17:49:02 -0700407 case K_OBJ_NET_SOCKET: /* Indeterminate size */
408 LOG_ERR("forbidden object type '%s' requested",
409 otype_to_str(otype));
410 return NULL;
411 default:
412 /* Remainder within bounds are permitted */
413 break;
414 }
415
Flavio Ceolin3b7e0b62023-06-23 09:34:14 -0700416 zo = dynamic_object_create(otype, obj_align_get(otype), size);
Andrew Boiebe919d32020-05-29 17:49:02 -0700417 if (zo == NULL) {
Nicolas Pitre962b3742022-03-13 18:28:59 -0400418 if (otype == K_OBJ_THREAD) {
419 thread_idx_free(tidx);
420 }
Andrew Boiebe919d32020-05-29 17:49:02 -0700421 return NULL;
422 }
Andrew Boiebe919d32020-05-29 17:49:02 -0700423
424 if (otype == K_OBJ_THREAD) {
425 zo->data.thread_id = tidx;
426 }
427
428 /* The allocating thread implicitly gets permission on kernel objects
429 * that it allocates
430 */
431 z_thread_perms_set(zo, _current);
432
433 /* Activates reference counting logic for automatic disposal when
434 * all permissions have been revoked
435 */
436 zo->flags |= K_OBJ_FLAG_ALLOC;
437
438 return zo->name;
Andrew Boie31bdfc02017-11-08 16:38:03 -0800439}
440
Flavio Ceolin67e66e42023-06-22 06:27:28 +0000441void *z_impl_k_object_alloc(enum k_objects otype)
442{
Flavio Ceolin3b7e0b62023-06-23 09:34:14 -0700443 return z_object_alloc(otype, 0);
Flavio Ceolin67e66e42023-06-22 06:27:28 +0000444}
445
446void *z_impl_k_object_alloc_size(enum k_objects otype, size_t size)
447{
448 return z_object_alloc(otype, size);
449}
450
Andrew Boie31bdfc02017-11-08 16:38:03 -0800451void k_object_free(void *obj)
452{
Flavio Ceolincbbe6d22023-07-18 13:11:07 -0700453 struct dyn_obj *dyn;
Andrew Boie31bdfc02017-11-08 16:38:03 -0800454
455 /* This function is intentionally not exposed to user mode.
456 * There's currently no robust way to track that an object isn't
457 * being used by some other thread
458 */
459
Andy Ross8a3d57b2019-02-06 09:10:36 -0800460 k_spinlock_key_t key = k_spin_lock(&objfree_lock);
461
Spoorthy Priya Yerabolu9247e8b2020-08-25 03:11:16 -0700462 dyn = dyn_object_find(obj);
463 if (dyn != NULL) {
Daniel Leungabfe0452021-04-27 11:49:30 -0700464 sys_dlist_remove(&dyn->dobj_list);
Daniel Leunge58b6542018-08-08 11:23:16 -0700465
Spoorthy Priya Yerabolu9247e8b2020-08-25 03:11:16 -0700466 if (dyn->kobj.type == K_OBJ_THREAD) {
467 thread_idx_free(dyn->kobj.data.thread_id);
Daniel Leunge58b6542018-08-08 11:23:16 -0700468 }
Andrew Boie31bdfc02017-11-08 16:38:03 -0800469 }
Andy Ross8a3d57b2019-02-06 09:10:36 -0800470 k_spin_unlock(&objfree_lock, key);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800471
Spoorthy Priya Yerabolu9247e8b2020-08-25 03:11:16 -0700472 if (dyn != NULL) {
Flavio Ceolined8355a2023-07-18 21:00:07 +0000473 k_free(dyn->data);
Spoorthy Priya Yerabolu9247e8b2020-08-25 03:11:16 -0700474 k_free(dyn);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800475 }
476}
477
Peter Bigot2fcf7622020-05-14 05:06:08 -0500478struct z_object *z_object_find(const void *obj)
Andrew Boie31bdfc02017-11-08 16:38:03 -0800479{
Andrew Boie2dc2ecf2020-03-11 07:13:07 -0700480 struct z_object *ret;
Andrew Boie31bdfc02017-11-08 16:38:03 -0800481
Patrik Flykt4344e272019-03-08 14:19:05 -0700482 ret = z_object_gperf_find(obj);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800483
Flavio Ceolin4218d5f2018-09-17 09:39:51 -0700484 if (ret == NULL) {
Flavio Ceolincbbe6d22023-07-18 13:11:07 -0700485 struct dyn_obj *dyn;
Andrew Boie31bdfc02017-11-08 16:38:03 -0800486
Peter Bigot2fcf7622020-05-14 05:06:08 -0500487 /* The cast to pointer-to-non-const violates MISRA
488 * 11.8 but is justified since we know dynamic objects
489 * were not declared with a const qualifier.
490 */
Flavio Ceolin3b7e0b62023-06-23 09:34:14 -0700491 dyn = dyn_object_find((void *)obj);
492 if (dyn != NULL) {
493 ret = &dyn->kobj;
Andrew Boie31bdfc02017-11-08 16:38:03 -0800494 }
495 }
496
497 return ret;
498}
499
Patrik Flykt4344e272019-03-08 14:19:05 -0700500void z_object_wordlist_foreach(_wordlist_cb_func_t func, void *context)
Andrew Boie31bdfc02017-11-08 16:38:03 -0800501{
Flavio Ceolincbbe6d22023-07-18 13:11:07 -0700502 struct dyn_obj *obj, *next;
Andrew Boie31bdfc02017-11-08 16:38:03 -0800503
Patrik Flykt4344e272019-03-08 14:19:05 -0700504 z_object_gperf_wordlist_foreach(func, context);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800505
Andy Ross8a3d57b2019-02-06 09:10:36 -0800506 k_spinlock_key_t key = k_spin_lock(&lists_lock);
507
Daniel Leungabfe0452021-04-27 11:49:30 -0700508 SYS_DLIST_FOR_EACH_CONTAINER_SAFE(&obj_list, obj, next, dobj_list) {
Andrew Boie97bf0012018-04-24 17:01:37 -0700509 func(&obj->kobj, context);
510 }
Andy Ross8a3d57b2019-02-06 09:10:36 -0800511 k_spin_unlock(&lists_lock, key);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800512}
513#endif /* CONFIG_DYNAMIC_OBJECTS */
514
Andrew Boief2734ab2020-03-11 06:37:42 -0700515static unsigned int thread_index_get(struct k_thread *thread)
Andrew Boie818a96d2017-11-03 09:00:35 -0700516{
Andrew Boie2dc2ecf2020-03-11 07:13:07 -0700517 struct z_object *ko;
Andrew Boie818a96d2017-11-03 09:00:35 -0700518
Anas Nashif9e3e7f62019-12-19 08:19:45 -0500519 ko = z_object_find(thread);
Andrew Boie818a96d2017-11-03 09:00:35 -0700520
Flavio Ceolin4218d5f2018-09-17 09:39:51 -0700521 if (ko == NULL) {
Andrew Boie818a96d2017-11-03 09:00:35 -0700522 return -1;
523 }
524
Andrew Boief2734ab2020-03-11 06:37:42 -0700525 return ko->data.thread_id;
Andrew Boie818a96d2017-11-03 09:00:35 -0700526}
527
Andrew Boie2dc2ecf2020-03-11 07:13:07 -0700528static void unref_check(struct z_object *ko, uintptr_t index)
Andrew Boie337e7432018-04-13 14:44:00 -0700529{
Andy Ross8a3d57b2019-02-06 09:10:36 -0800530 k_spinlock_key_t key = k_spin_lock(&obj_lock);
Andrew Boie7ecc3592019-01-31 12:09:06 -0800531
532 sys_bitfield_clear_bit((mem_addr_t)&ko->perms, index);
533
534#ifdef CONFIG_DYNAMIC_OBJECTS
Daniel Leungb6dd9602021-12-13 14:54:51 -0800535 if ((ko->flags & K_OBJ_FLAG_ALLOC) == 0U) {
536 /* skip unref check for static kernel object */
537 goto out;
538 }
539
Carles Cufi55350a92021-12-04 19:57:03 +0100540 void *vko = ko;
541
Flavio Ceolincbbe6d22023-07-18 13:11:07 -0700542 struct dyn_obj *dyn = CONTAINER_OF(vko, struct dyn_obj, kobj);
Andrew Boie7ecc3592019-01-31 12:09:06 -0800543
Daniel Leungb6dd9602021-12-13 14:54:51 -0800544 __ASSERT(IS_PTR_ALIGNED(dyn, struct dyn_obj), "unaligned z_object");
Andrew Boie7ecc3592019-01-31 12:09:06 -0800545
Andrew Boie337e7432018-04-13 14:44:00 -0700546 for (int i = 0; i < CONFIG_MAX_THREAD_BYTES; i++) {
Patrik Flykt24d71432019-03-26 19:57:45 -0600547 if (ko->perms[i] != 0U) {
Andrew Boie7ecc3592019-01-31 12:09:06 -0800548 goto out;
Andrew Boie337e7432018-04-13 14:44:00 -0700549 }
550 }
551
552 /* This object has no more references. Some objects may have
553 * dynamically allocated resources, require cleanup, or need to be
554 * marked as uninitailized when all references are gone. What
555 * specifically needs to happen depends on the object type.
556 */
557 switch (ko->type) {
Peter Mitsisf86027f2022-07-08 11:27:09 -0400558#ifdef CONFIG_PIPES
Andrew Boie44fe8122018-04-12 17:38:12 -0700559 case K_OBJ_PIPE:
560 k_pipe_cleanup((struct k_pipe *)ko->name);
561 break;
Peter Mitsisf86027f2022-07-08 11:27:09 -0400562#endif
Andrew Boie0fe789f2018-04-12 18:35:56 -0700563 case K_OBJ_MSGQ:
564 k_msgq_cleanup((struct k_msgq *)ko->name);
565 break;
Andrew Boief3bee952018-05-02 17:44:39 -0700566 case K_OBJ_STACK:
567 k_stack_cleanup((struct k_stack *)ko->name);
568 break;
Andrew Boie337e7432018-04-13 14:44:00 -0700569 default:
Flavio Ceolin3259ac02018-09-11 13:14:21 -0700570 /* Nothing to do */
Andrew Boie337e7432018-04-13 14:44:00 -0700571 break;
572 }
Andrew Boie97bf0012018-04-24 17:01:37 -0700573
Daniel Leungabfe0452021-04-27 11:49:30 -0700574 sys_dlist_remove(&dyn->dobj_list);
Flavio Ceolined8355a2023-07-18 21:00:07 +0000575 k_free(dyn->data);
Spoorthy Priya Yerabolu9247e8b2020-08-25 03:11:16 -0700576 k_free(dyn);
Andrew Boie7ecc3592019-01-31 12:09:06 -0800577out:
Andrew Boie97bf0012018-04-24 17:01:37 -0700578#endif
Andy Ross8a3d57b2019-02-06 09:10:36 -0800579 k_spin_unlock(&obj_lock, key);
Andrew Boie337e7432018-04-13 14:44:00 -0700580}
581
Andrew Boie2dc2ecf2020-03-11 07:13:07 -0700582static void wordlist_cb(struct z_object *ko, void *ctx_ptr)
Andrew Boie47f8fd12017-10-05 11:11:02 -0700583{
584 struct perm_ctx *ctx = (struct perm_ctx *)ctx_ptr;
585
586 if (sys_bitfield_test_bit((mem_addr_t)&ko->perms, ctx->parent_id) &&
587 (struct k_thread *)ko->name != ctx->parent) {
588 sys_bitfield_set_bit((mem_addr_t)&ko->perms, ctx->child_id);
589 }
590}
591
Patrik Flykt4344e272019-03-08 14:19:05 -0700592void z_thread_perms_inherit(struct k_thread *parent, struct k_thread *child)
Andrew Boie47f8fd12017-10-05 11:11:02 -0700593{
594 struct perm_ctx ctx = {
Andrew Boie818a96d2017-11-03 09:00:35 -0700595 thread_index_get(parent),
596 thread_index_get(child),
Andrew Boie47f8fd12017-10-05 11:11:02 -0700597 parent
598 };
599
Andrew Boie818a96d2017-11-03 09:00:35 -0700600 if ((ctx.parent_id != -1) && (ctx.child_id != -1)) {
Patrik Flykt4344e272019-03-08 14:19:05 -0700601 z_object_wordlist_foreach(wordlist_cb, &ctx);
Andrew Boie47f8fd12017-10-05 11:11:02 -0700602 }
603}
604
Andrew Boie2dc2ecf2020-03-11 07:13:07 -0700605void z_thread_perms_set(struct z_object *ko, struct k_thread *thread)
Andrew Boie945af952017-08-22 13:15:23 -0700606{
Andrew Boie818a96d2017-11-03 09:00:35 -0700607 int index = thread_index_get(thread);
608
609 if (index != -1) {
610 sys_bitfield_set_bit((mem_addr_t)&ko->perms, index);
Andrew Boie2acfcd62017-08-30 14:31:03 -0700611 }
Andrew Boie945af952017-08-22 13:15:23 -0700612}
613
Andrew Boie2dc2ecf2020-03-11 07:13:07 -0700614void z_thread_perms_clear(struct z_object *ko, struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700615{
Andrew Boie818a96d2017-11-03 09:00:35 -0700616 int index = thread_index_get(thread);
617
618 if (index != -1) {
Andy Ross8a3d57b2019-02-06 09:10:36 -0800619 sys_bitfield_clear_bit((mem_addr_t)&ko->perms, index);
Andrew Boie7ecc3592019-01-31 12:09:06 -0800620 unref_check(ko, index);
Andrew Boiea89bf012017-10-09 14:47:55 -0700621 }
622}
623
Andrew Boie2dc2ecf2020-03-11 07:13:07 -0700624static void clear_perms_cb(struct z_object *ko, void *ctx_ptr)
Andrew Boie04caa672017-10-13 13:57:07 -0700625{
Andrew Boie428afe52019-11-18 10:20:16 -0800626 uintptr_t id = (uintptr_t)ctx_ptr;
Andrew Boie04caa672017-10-13 13:57:07 -0700627
Andrew Boie7ecc3592019-01-31 12:09:06 -0800628 unref_check(ko, id);
Andrew Boie04caa672017-10-13 13:57:07 -0700629}
630
Patrik Flykt4344e272019-03-08 14:19:05 -0700631void z_thread_perms_all_clear(struct k_thread *thread)
Andrew Boie04caa672017-10-13 13:57:07 -0700632{
Andrew Boie428afe52019-11-18 10:20:16 -0800633 uintptr_t index = thread_index_get(thread);
Andrew Boie818a96d2017-11-03 09:00:35 -0700634
Carlo Caionef1612232020-10-12 12:10:45 +0200635 if ((int)index != -1) {
Patrik Flykt4344e272019-03-08 14:19:05 -0700636 z_object_wordlist_foreach(clear_perms_cb, (void *)index);
Andrew Boie04caa672017-10-13 13:57:07 -0700637 }
638}
639
Andrew Boie2dc2ecf2020-03-11 07:13:07 -0700640static int thread_perms_test(struct z_object *ko)
Andrew Boie945af952017-08-22 13:15:23 -0700641{
Andrew Boie818a96d2017-11-03 09:00:35 -0700642 int index;
643
Patrik Flykt24d71432019-03-26 19:57:45 -0600644 if ((ko->flags & K_OBJ_FLAG_PUBLIC) != 0U) {
Andrew Boie04caa672017-10-13 13:57:07 -0700645 return 1;
646 }
647
Andrew Boie818a96d2017-11-03 09:00:35 -0700648 index = thread_index_get(_current);
649 if (index != -1) {
650 return sys_bitfield_test_bit((mem_addr_t)&ko->perms, index);
Andrew Boie2acfcd62017-08-30 14:31:03 -0700651 }
652 return 0;
Andrew Boie945af952017-08-22 13:15:23 -0700653}
654
Andrew Boie2dc2ecf2020-03-11 07:13:07 -0700655static void dump_permission_error(struct z_object *ko)
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700656{
Andrew Boie818a96d2017-11-03 09:00:35 -0700657 int index = thread_index_get(_current);
Andrew Boie99b3f862019-09-30 14:25:23 -0700658 LOG_ERR("thread %p (%d) does not have permission on %s %p",
659 _current, index,
660 otype_to_str(ko->type), ko->name);
661 LOG_HEXDUMP_ERR(ko->perms, sizeof(ko->perms), "permission bitmap");
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700662}
Andrew Boie945af952017-08-22 13:15:23 -0700663
Peter Bigot2fcf7622020-05-14 05:06:08 -0500664void z_dump_object_error(int retval, const void *obj, struct z_object *ko,
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700665 enum k_objects otype)
666{
667 switch (retval) {
668 case -EBADF:
Andrew Boie99b3f862019-09-30 14:25:23 -0700669 LOG_ERR("%p is not a valid %s", obj, otype_to_str(otype));
Andrew Boiebe919d32020-05-29 17:49:02 -0700670 if (ko == NULL) {
671 LOG_ERR("address is not a known kernel object");
672 } else {
673 LOG_ERR("address is actually a %s",
674 otype_to_str(ko->type));
675 }
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700676 break;
677 case -EPERM:
678 dump_permission_error(ko);
679 break;
680 case -EINVAL:
Andrew Boie99b3f862019-09-30 14:25:23 -0700681 LOG_ERR("%p used before initialization", obj);
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700682 break;
Andrew Boiea2b40ec2017-10-15 14:22:08 -0700683 case -EADDRINUSE:
Andrew Boie99b3f862019-09-30 14:25:23 -0700684 LOG_ERR("%p %s in use", obj, otype_to_str(otype));
Flavio Ceolina3cea502018-09-10 22:54:55 -0700685 break;
686 default:
687 /* Not handled error */
688 break;
Andrew Boie945af952017-08-22 13:15:23 -0700689 }
Andrew Boie3b5ae802017-10-04 12:10:32 -0700690}
691
Peter Bigot2fcf7622020-05-14 05:06:08 -0500692void z_impl_k_object_access_grant(const void *object, struct k_thread *thread)
Andrew Boie3b5ae802017-10-04 12:10:32 -0700693{
Andrew Boie2dc2ecf2020-03-11 07:13:07 -0700694 struct z_object *ko = z_object_find(object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700695
Flavio Ceolin4218d5f2018-09-17 09:39:51 -0700696 if (ko != NULL) {
Patrik Flykt4344e272019-03-08 14:19:05 -0700697 z_thread_perms_set(ko, thread);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700698 }
699}
700
Peter Bigot2fcf7622020-05-14 05:06:08 -0500701void k_object_access_revoke(const void *object, struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700702{
Andrew Boie2dc2ecf2020-03-11 07:13:07 -0700703 struct z_object *ko = z_object_find(object);
Andrew Boiea89bf012017-10-09 14:47:55 -0700704
Flavio Ceolin4218d5f2018-09-17 09:39:51 -0700705 if (ko != NULL) {
Patrik Flykt4344e272019-03-08 14:19:05 -0700706 z_thread_perms_clear(ko, thread);
Andrew Boiea89bf012017-10-09 14:47:55 -0700707 }
708}
709
Peter Bigot2fcf7622020-05-14 05:06:08 -0500710void z_impl_k_object_release(const void *object)
Andrew Boiee9cfc542018-04-13 13:15:28 -0700711{
712 k_object_access_revoke(object, _current);
713}
714
Peter Bigot2fcf7622020-05-14 05:06:08 -0500715void k_object_access_all_grant(const void *object)
Andrew Boie3b5ae802017-10-04 12:10:32 -0700716{
Andrew Boie2dc2ecf2020-03-11 07:13:07 -0700717 struct z_object *ko = z_object_find(object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700718
Flavio Ceolin4218d5f2018-09-17 09:39:51 -0700719 if (ko != NULL) {
Andrew Boie04caa672017-10-13 13:57:07 -0700720 ko->flags |= K_OBJ_FLAG_PUBLIC;
Andrew Boie3b5ae802017-10-04 12:10:32 -0700721 }
Andrew Boie945af952017-08-22 13:15:23 -0700722}
723
Andrew Boie2dc2ecf2020-03-11 07:13:07 -0700724int z_object_validate(struct z_object *ko, enum k_objects otype,
Andrew Boiea2b40ec2017-10-15 14:22:08 -0700725 enum _obj_init_check init)
Andrew Boie945af952017-08-22 13:15:23 -0700726{
Flavio Ceolinea716bf2018-09-20 16:30:45 -0700727 if (unlikely((ko == NULL) ||
728 (otype != K_OBJ_ANY && ko->type != otype))) {
Andrew Boie945af952017-08-22 13:15:23 -0700729 return -EBADF;
730 }
731
Andrew Boie3a0f6842017-10-09 12:46:25 -0700732 /* Manipulation of any kernel objects by a user thread requires that
733 * thread be granted access first, even for uninitialized objects
Andrew Boie945af952017-08-22 13:15:23 -0700734 */
Flavio Ceolin2df02cc2019-03-14 14:32:45 -0700735 if (unlikely(thread_perms_test(ko) == 0)) {
Andrew Boie945af952017-08-22 13:15:23 -0700736 return -EPERM;
737 }
738
Andrew Boiea2b40ec2017-10-15 14:22:08 -0700739 /* Initialization state checks. _OBJ_INIT_ANY, we don't care */
740 if (likely(init == _OBJ_INIT_TRUE)) {
Naiyuan Tianbc3fda42021-08-23 23:32:58 +0800741 /* Object MUST be initialized */
Patrik Flykt21358ba2019-03-28 14:57:54 -0600742 if (unlikely((ko->flags & K_OBJ_FLAG_INITIALIZED) == 0U)) {
Andrew Boiea2b40ec2017-10-15 14:22:08 -0700743 return -EINVAL;
744 }
Maksim Masalski929956d2021-05-17 16:58:20 +0800745 } else if (init == _OBJ_INIT_FALSE) { /* _OBJ_INIT_FALSE case */
Andrew Boiea2b40ec2017-10-15 14:22:08 -0700746 /* Object MUST NOT be initialized */
Patrik Flykt21358ba2019-03-28 14:57:54 -0600747 if (unlikely((ko->flags & K_OBJ_FLAG_INITIALIZED) != 0U)) {
Andrew Boiea2b40ec2017-10-15 14:22:08 -0700748 return -EADDRINUSE;
749 }
Flavio Ceolin3e97acc2018-09-25 11:24:28 -0700750 } else {
751 /* _OBJ_INIT_ANY */
Andrew Boie945af952017-08-22 13:15:23 -0700752 }
753
754 return 0;
755}
756
Peter Bigot2fcf7622020-05-14 05:06:08 -0500757void z_object_init(const void *obj)
Andrew Boie945af952017-08-22 13:15:23 -0700758{
Andrew Boie2dc2ecf2020-03-11 07:13:07 -0700759 struct z_object *ko;
Andrew Boie945af952017-08-22 13:15:23 -0700760
761 /* By the time we get here, if the caller was from userspace, all the
Patrik Flykt4344e272019-03-08 14:19:05 -0700762 * necessary checks have been done in z_object_validate(), which takes
Andrew Boie945af952017-08-22 13:15:23 -0700763 * place before the object is initialized.
764 *
765 * This function runs after the object has been initialized and
766 * finalizes it
767 */
768
Patrik Flykt4344e272019-03-08 14:19:05 -0700769 ko = z_object_find(obj);
Flavio Ceolin4218d5f2018-09-17 09:39:51 -0700770 if (ko == NULL) {
Andrew Boie945af952017-08-22 13:15:23 -0700771 /* Supervisor threads can ignore rules about kernel objects
772 * and may declare them on stacks, etc. Such objects will never
773 * be usable from userspace, but we shouldn't explode.
774 */
775 return;
776 }
777
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700778 /* Allows non-initialization system calls to be made on this object */
Andrew Boie945af952017-08-22 13:15:23 -0700779 ko->flags |= K_OBJ_FLAG_INITIALIZED;
780}
781
Peter Bigot2fcf7622020-05-14 05:06:08 -0500782void z_object_recycle(const void *obj)
Andrew Boie83fda7c2018-07-31 14:39:11 -0700783{
Andrew Boie2dc2ecf2020-03-11 07:13:07 -0700784 struct z_object *ko = z_object_find(obj);
Andrew Boie83fda7c2018-07-31 14:39:11 -0700785
Flavio Ceolin4218d5f2018-09-17 09:39:51 -0700786 if (ko != NULL) {
Flavio Ceolinda49f2e2018-09-11 19:09:03 -0700787 (void)memset(ko->perms, 0, sizeof(ko->perms));
Patrik Flykt4344e272019-03-08 14:19:05 -0700788 z_thread_perms_set(ko, k_current_get());
Andrew Boie83fda7c2018-07-31 14:39:11 -0700789 ko->flags |= K_OBJ_FLAG_INITIALIZED;
790 }
791}
792
Peter Bigot2fcf7622020-05-14 05:06:08 -0500793void z_object_uninit(const void *obj)
Andrew Boie4a9a4242017-10-05 12:21:36 -0700794{
Andrew Boie2dc2ecf2020-03-11 07:13:07 -0700795 struct z_object *ko;
Andrew Boie4a9a4242017-10-05 12:21:36 -0700796
Patrik Flykt4344e272019-03-08 14:19:05 -0700797 /* See comments in z_object_init() */
798 ko = z_object_find(obj);
Flavio Ceolin4218d5f2018-09-17 09:39:51 -0700799 if (ko == NULL) {
Andrew Boie4a9a4242017-10-05 12:21:36 -0700800 return;
801 }
802
803 ko->flags &= ~K_OBJ_FLAG_INITIALIZED;
804}
805
Andrew Boiec8188f62018-06-22 14:31:51 -0700806/*
807 * Copy to/from helper functions used in syscall handlers
808 */
Andrew Boie526807c2019-03-28 15:17:31 -0700809void *z_user_alloc_from_copy(const void *src, size_t size)
Andrew Boiec8188f62018-06-22 14:31:51 -0700810{
811 void *dst = NULL;
Andrew Boiec8188f62018-06-22 14:31:51 -0700812
813 /* Does the caller in user mode have access to read this memory? */
814 if (Z_SYSCALL_MEMORY_READ(src, size)) {
815 goto out_err;
816 }
817
818 dst = z_thread_malloc(size);
Flavio Ceolin4218d5f2018-09-17 09:39:51 -0700819 if (dst == NULL) {
Andrew Boie99b3f862019-09-30 14:25:23 -0700820 LOG_ERR("out of thread resource pool memory (%zu)", size);
Andrew Boiec8188f62018-06-22 14:31:51 -0700821 goto out_err;
822 }
823
Flavio Ceolin66994232018-08-13 15:17:04 -0700824 (void)memcpy(dst, src, size);
Andrew Boiec8188f62018-06-22 14:31:51 -0700825out_err:
Andrew Boiec8188f62018-06-22 14:31:51 -0700826 return dst;
827}
828
Andrew Boie526807c2019-03-28 15:17:31 -0700829static int user_copy(void *dst, const void *src, size_t size, bool to_user)
Andrew Boiec8188f62018-06-22 14:31:51 -0700830{
831 int ret = EFAULT;
Andrew Boiec8188f62018-06-22 14:31:51 -0700832
833 /* Does the caller in user mode have access to this memory? */
834 if (to_user ? Z_SYSCALL_MEMORY_WRITE(dst, size) :
835 Z_SYSCALL_MEMORY_READ(src, size)) {
836 goto out_err;
837 }
838
Flavio Ceolin66994232018-08-13 15:17:04 -0700839 (void)memcpy(dst, src, size);
Andrew Boiec8188f62018-06-22 14:31:51 -0700840 ret = 0;
841out_err:
Andrew Boiec8188f62018-06-22 14:31:51 -0700842 return ret;
843}
844
Andrew Boie526807c2019-03-28 15:17:31 -0700845int z_user_from_copy(void *dst, const void *src, size_t size)
Andrew Boiec8188f62018-06-22 14:31:51 -0700846{
847 return user_copy(dst, src, size, false);
848}
849
Andrew Boie526807c2019-03-28 15:17:31 -0700850int z_user_to_copy(void *dst, const void *src, size_t size)
Andrew Boiec8188f62018-06-22 14:31:51 -0700851{
852 return user_copy(dst, src, size, true);
853}
854
Andrew Boie526807c2019-03-28 15:17:31 -0700855char *z_user_string_alloc_copy(const char *src, size_t maxlen)
Andrew Boiec8188f62018-06-22 14:31:51 -0700856{
Jakob Olesenc8708d92019-05-07 10:17:35 -0700857 size_t actual_len;
Flavio Ceolin0866d182018-08-14 17:57:08 -0700858 int err;
Andrew Boiec8188f62018-06-22 14:31:51 -0700859 char *ret = NULL;
860
Andrew Boiec8188f62018-06-22 14:31:51 -0700861 actual_len = z_user_string_nlen(src, maxlen, &err);
Flavio Ceolin76b35182018-12-16 12:48:29 -0800862 if (err != 0) {
Andrew Boiec8188f62018-06-22 14:31:51 -0700863 goto out;
864 }
865 if (actual_len == maxlen) {
866 /* Not NULL terminated */
Andrew Boie99b3f862019-09-30 14:25:23 -0700867 LOG_ERR("string too long %p (%zu)", src, actual_len);
Andrew Boiec8188f62018-06-22 14:31:51 -0700868 goto out;
869 }
Jakob Olesenc8708d92019-05-07 10:17:35 -0700870 if (size_add_overflow(actual_len, 1, &actual_len)) {
Andrew Boie99b3f862019-09-30 14:25:23 -0700871 LOG_ERR("overflow");
Andrew Boiec8188f62018-06-22 14:31:51 -0700872 goto out;
873 }
874
875 ret = z_user_alloc_from_copy(src, actual_len);
Andrew Boie09dc9292019-04-12 12:32:34 -0700876
877 /* Someone may have modified the source string during the above
878 * checks. Ensure what we actually copied is still terminated
879 * properly.
880 */
881 if (ret != NULL) {
Anas Nashifbbbc38b2021-03-29 10:03:49 -0400882 ret[actual_len - 1U] = '\0';
Andrew Boie09dc9292019-04-12 12:32:34 -0700883 }
Andrew Boiec8188f62018-06-22 14:31:51 -0700884out:
Andrew Boiec8188f62018-06-22 14:31:51 -0700885 return ret;
886}
887
Andrew Boie526807c2019-03-28 15:17:31 -0700888int z_user_string_copy(char *dst, const char *src, size_t maxlen)
Andrew Boiec8188f62018-06-22 14:31:51 -0700889{
Jakob Olesenc8708d92019-05-07 10:17:35 -0700890 size_t actual_len;
Flavio Ceolin0866d182018-08-14 17:57:08 -0700891 int ret, err;
Andrew Boiec8188f62018-06-22 14:31:51 -0700892
Andrew Boiec8188f62018-06-22 14:31:51 -0700893 actual_len = z_user_string_nlen(src, maxlen, &err);
Flavio Ceolin76b35182018-12-16 12:48:29 -0800894 if (err != 0) {
Andrew Boiec8188f62018-06-22 14:31:51 -0700895 ret = EFAULT;
896 goto out;
897 }
898 if (actual_len == maxlen) {
899 /* Not NULL terminated */
Andrew Boie99b3f862019-09-30 14:25:23 -0700900 LOG_ERR("string too long %p (%zu)", src, actual_len);
Andrew Boiec8188f62018-06-22 14:31:51 -0700901 ret = EINVAL;
902 goto out;
903 }
Jakob Olesenc8708d92019-05-07 10:17:35 -0700904 if (size_add_overflow(actual_len, 1, &actual_len)) {
Andrew Boie99b3f862019-09-30 14:25:23 -0700905 LOG_ERR("overflow");
Andrew Boiec8188f62018-06-22 14:31:51 -0700906 ret = EINVAL;
907 goto out;
908 }
909
910 ret = z_user_from_copy(dst, src, actual_len);
Andrew Boie09dc9292019-04-12 12:32:34 -0700911
912 /* See comment above in z_user_string_alloc_copy() */
913 dst[actual_len - 1] = '\0';
Andrew Boiec8188f62018-06-22 14:31:51 -0700914out:
Andrew Boiec8188f62018-06-22 14:31:51 -0700915 return ret;
916}
917
918/*
Andrew Boie4ce652e2019-02-22 16:08:44 -0800919 * Application memory region initialization
920 */
921
922extern char __app_shmem_regions_start[];
923extern char __app_shmem_regions_end[];
924
Gerard Marull-Paretasa5fd0d12022-10-19 09:33:44 +0200925static int app_shmem_bss_zero(void)
Andrew Boie4ce652e2019-02-22 16:08:44 -0800926{
927 struct z_app_region *region, *end;
928
Andrew Boiefb1c2942020-03-16 11:20:08 -0700929
Andrew Boie4ce652e2019-02-22 16:08:44 -0800930 end = (struct z_app_region *)&__app_shmem_regions_end;
931 region = (struct z_app_region *)&__app_shmem_regions_start;
932
933 for ( ; region < end; region++) {
Daniel Leung2117a2a2021-07-12 13:33:32 -0700934#if defined(CONFIG_DEMAND_PAGING) && !defined(CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT)
935 /* When BSS sections are not present at boot, we need to wait for
936 * paging mechanism to be initialized before we can zero out BSS.
937 */
938 extern bool z_sys_post_kernel;
939 bool do_clear = z_sys_post_kernel;
940
941 /* During pre-kernel init, z_sys_post_kernel == false, but
942 * with pinned rodata region, so clear. Otherwise skip.
943 * In post-kernel init, z_sys_post_kernel == true,
944 * skip those in pinned rodata region as they have already
945 * been cleared and possibly already in use. Otherwise clear.
946 */
947 if (((uint8_t *)region->bss_start >= (uint8_t *)_app_smem_pinned_start) &&
948 ((uint8_t *)region->bss_start < (uint8_t *)_app_smem_pinned_end)) {
949 do_clear = !do_clear;
950 }
951
952 if (do_clear)
953#endif /* CONFIG_DEMAND_PAGING && !CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT */
954 {
955 (void)memset(region->bss_start, 0, region->bss_size);
956 }
Andrew Boie4ce652e2019-02-22 16:08:44 -0800957 }
Andrew Boiefb1c2942020-03-16 11:20:08 -0700958
959 return 0;
Andrew Boie4ce652e2019-02-22 16:08:44 -0800960}
961
Jordan Yates6f41d522022-07-02 12:06:55 +1000962SYS_INIT_NAMED(app_shmem_bss_zero_pre, app_shmem_bss_zero,
963 PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
Andrew Boiefb1c2942020-03-16 11:20:08 -0700964
Daniel Leung2117a2a2021-07-12 13:33:32 -0700965#if defined(CONFIG_DEMAND_PAGING) && !defined(CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT)
966/* When BSS sections are not present at boot, we need to wait for
967 * paging mechanism to be initialized before we can zero out BSS.
968 */
Jordan Yates6f41d522022-07-02 12:06:55 +1000969SYS_INIT_NAMED(app_shmem_bss_zero_post, app_shmem_bss_zero,
970 POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
Daniel Leung2117a2a2021-07-12 13:33:32 -0700971#endif /* CONFIG_DEMAND_PAGING && !CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT */
972
Andrew Boie4ce652e2019-02-22 16:08:44 -0800973/*
Andrew Boiec8188f62018-06-22 14:31:51 -0700974 * Default handlers if otherwise unimplemented
975 */
976
Andrew Boie800b35f2019-11-05 09:27:18 -0800977static uintptr_t handler_bad_syscall(uintptr_t bad_id, uintptr_t arg2,
978 uintptr_t arg3, uintptr_t arg4,
979 uintptr_t arg5, uintptr_t arg6,
980 void *ssf)
Andrew Boief5649862017-09-08 12:10:12 -0700981{
Andrew Boie800b35f2019-11-05 09:27:18 -0800982 LOG_ERR("Bad system call id %" PRIuPTR " invoked", bad_id);
Andrew Boie64c81892020-05-28 16:24:09 -0700983 arch_syscall_oops(ssf);
Andrew Boie777336e2019-06-24 09:35:55 -0700984 CODE_UNREACHABLE; /* LCOV_EXCL_LINE */
Andrew Boief5649862017-09-08 12:10:12 -0700985}
986
Andrew Boie800b35f2019-11-05 09:27:18 -0800987static uintptr_t handler_no_syscall(uintptr_t arg1, uintptr_t arg2,
988 uintptr_t arg3, uintptr_t arg4,
989 uintptr_t arg5, uintptr_t arg6, void *ssf)
Andrew Boiefa94ee72017-09-28 16:54:35 -0700990{
Andrew Boie99b3f862019-09-30 14:25:23 -0700991 LOG_ERR("Unimplemented system call");
Andrew Boie64c81892020-05-28 16:24:09 -0700992 arch_syscall_oops(ssf);
Andrew Boie777336e2019-06-24 09:35:55 -0700993 CODE_UNREACHABLE; /* LCOV_EXCL_LINE */
Andrew Boiefa94ee72017-09-28 16:54:35 -0700994}
Andrew Boiefc273c02017-09-23 12:51:23 -0700995
Andrew Boiefa94ee72017-09-28 16:54:35 -0700996#include <syscall_dispatch.c>