kernel: userspace: Add k_object_alloc_size

Add a new API to dynamically allocate kernel objects that allow
passing an arbitrary size. This new API allows to allocate dynamic
thread stack.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
This commit is contained in:
Flavio Ceolin 2023-06-22 06:27:28 +00:00 committed by Chris Friedt
parent d58c9a1ca4
commit 67e66e4807
3 changed files with 49 additions and 4 deletions

View file

@ -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)
{

View file

@ -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;

View file

@ -65,3 +65,9 @@ static inline void *z_vrfy_k_object_alloc(enum k_objects otype)
return z_impl_k_object_alloc(otype);
}
#include <syscalls/k_object_alloc_mrsh.c>
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 <syscalls/k_object_alloc_size_mrsh.c>