diff --git a/include/zephyr/sys/kobject.h b/include/zephyr/sys/kobject.h index fa63037542..64db4d781c 100644 --- a/include/zephyr/sys/kobject.h +++ b/include/zephyr/sys/kobject.h @@ -248,7 +248,8 @@ static inline void k_object_access_all_grant(const void *object) * state, with the calling thread being granted permission on it. The memory * for the object will be allocated out of the calling thread's resource pool. * - * Currently, allocation of thread stacks is not supported. + * @note Thread stack object has to use k_object_alloc_size() since stacks may + * have different sizes. * * @param otype Requested kernel object type * @return A pointer to the allocated kernel object, or NULL if memory wasn't @@ -256,6 +257,24 @@ static inline void k_object_access_all_grant(const void *object) */ __syscall void *k_object_alloc(enum k_objects otype); +/** + * Allocate a kernel object of a designated type and a given size + * + * This will instantiate at runtime a kernel object of the specified type, + * returning a pointer to it. The object will be returned in an uninitialized + * state, with the calling thread being granted permission on it. The memory + * for the object will be allocated out of the calling thread's resource pool. + * + * This function is specially helpful for thread stack objects because + * their sizes can vary. Other objects should probably look k_object_alloc(). + * + * @param otype Requested kernel object type + * @param size Requested kernel object size + * @return A pointer to the allocated kernel object, or NULL if memory wasn't + * available + */ +__syscall void *k_object_alloc_size(enum k_objects otype, size_t size); + /** * Allocate memory and install as a generic kernel object * @@ -322,6 +341,15 @@ static inline void *z_impl_k_object_alloc(enum k_objects otype) return NULL; } +static inline void *z_impl_k_object_alloc_size(enum k_objects otype, + size_t size) +{ + ARG_UNUSED(otype); + ARG_UNUSED(size); + + return NULL; +} + static inline struct z_object *z_dynamic_object_aligned_create(size_t align, size_t size) { diff --git a/kernel/userspace.c b/kernel/userspace.c index 9d8611a387..24a59b9401 100644 --- a/kernel/userspace.c +++ b/kernel/userspace.c @@ -328,7 +328,7 @@ struct z_object *z_dynamic_object_aligned_create(size_t align, size_t size) return &dyn->kobj; } -void *z_impl_k_object_alloc(enum k_objects otype) +static void *z_object_alloc(enum k_objects otype, size_t object_size) { struct z_object *zo; uintptr_t tidx = 0; @@ -348,7 +348,8 @@ void *z_impl_k_object_alloc(enum k_objects otype) /* The following are currently not allowed at all */ case K_OBJ_FUTEX: /* Lives in user memory */ case K_OBJ_SYS_MUTEX: /* Lives in user memory */ - case K_OBJ_THREAD_STACK_ELEMENT: /* No aligned allocator */ + case K_OBJ_THREAD_STACK_ELEMENT: + break; case K_OBJ_NET_SOCKET: /* Indeterminate size */ LOG_ERR("forbidden object type '%s' requested", otype_to_str(otype)); @@ -359,7 +360,7 @@ void *z_impl_k_object_alloc(enum k_objects otype) } zo = z_dynamic_object_aligned_create(obj_align_get(otype), - obj_size_get(otype)); + object_size); if (zo == NULL) { if (otype == K_OBJ_THREAD) { thread_idx_free(tidx); @@ -385,6 +386,16 @@ void *z_impl_k_object_alloc(enum k_objects otype) return zo->name; } +void *z_impl_k_object_alloc(enum k_objects otype) +{ + return z_object_alloc(otype, obj_size_get(otype)); +} + +void *z_impl_k_object_alloc_size(enum k_objects otype, size_t size) +{ + return z_object_alloc(otype, size); +} + void k_object_free(void *obj) { struct dyn_obj *dyn; diff --git a/kernel/userspace_handler.c b/kernel/userspace_handler.c index 6531e83791..80f9325b7d 100644 --- a/kernel/userspace_handler.c +++ b/kernel/userspace_handler.c @@ -65,3 +65,9 @@ static inline void *z_vrfy_k_object_alloc(enum k_objects otype) return z_impl_k_object_alloc(otype); } #include + +static inline void *z_vrfy_k_object_alloc_size(enum k_objects otype, size_t size) +{ + return z_impl_k_object_alloc_size(otype, size); +} +#include