Skip to content

Commit

Permalink
kernel: use a union for kobject data values
Browse files Browse the repository at this point in the history
Rather than stuffing various values in a uintptr_t based on
type using casts, use a union for this instead.

No functional difference, but the semantics of the data member
are now much clearer to the casual observer since it is now
formally defined by this union.

Signed-off-by: Andrew Boie <[email protected]>
  • Loading branch information
Andrew Boie authored and andrewboie committed Mar 17, 2020
1 parent e13bb7e commit f2734ab
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 16 deletions.
5 changes: 2 additions & 3 deletions doc/reference/usermode/kernelobjects.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,8 @@ includes:
instance of :cpp:enum:`k_objects`.
* A set of flags for that object. This is currently used to track
initialization state and whether an object is public or not.
* An extra data field. This is currently used for thread stack objects
to denote how large the stack is, and for thread objects to indicate
the thread's index in kernel object permission bitfields.
* An extra data field. The semantics of this field vary by object type, see
the definition of :c:type:`union z_object_data`.

Dynamic objects allocated at runtime are tracked in a runtime red/black tree
which is used in parallel to the gperf table when validating object pointers.
Expand Down
21 changes: 20 additions & 1 deletion include/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ struct k_poll_signal;
struct k_mem_domain;
struct k_mem_partition;
struct k_futex;
struct z_futex_data;

/**
* @brief Kernel Object Types
Expand Down Expand Up @@ -165,14 +166,32 @@ enum k_objects {
*/

#ifdef CONFIG_USERSPACE
/* Object extra data. Only some objects use this, determined by object type */
union z_object_data {
/* Backing mutex for K_OBJ_SYS_MUTEX */
struct k_mutex *mutex;

/* Numerical thread ID for K_OBJ_THREAD */
unsigned int thread_id;

/* Stack buffer size for K_OBJ__THREAD_STACK_ELEMENT */
size_t stack_size;

/* Futex wait queue and spinlock for K_OBJ_FUTEX */
struct z_futex_data *futex_data;

/* All other objects */
int unused;
};

/* Table generated by gperf, these objects are retrieved via
* z_object_find() */
struct _k_object {
void *name;
u8_t perms[CONFIG_MAX_THREAD_BYTES];
u8_t type;
u8_t flags;
uintptr_t data;
union z_object_data data;
} __packed __aligned(4);

struct _k_object_assignment {
Expand Down
2 changes: 1 addition & 1 deletion kernel/futex.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static struct z_futex_data *k_futex_find_data(struct k_futex *futex)
return NULL;
}

return (struct z_futex_data *)obj->data;
return obj->data.futex_data;
}

int z_impl_k_futex_wake(struct k_futex *futex, bool wake_all)
Expand Down
6 changes: 3 additions & 3 deletions kernel/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -664,9 +664,9 @@ k_tid_t z_vrfy_k_thread_create(struct k_thread *new_thread,
/* Testing less-than-or-equal since additional room may have been
* allocated for alignment constraints
*/
Z_OOPS(Z_SYSCALL_VERIFY_MSG(total_size <= stack_object->data,
"stack size %zu is too big, max is %lu",
total_size, stack_object->data));
Z_OOPS(Z_SYSCALL_VERIFY_MSG(total_size <= stack_object->data.stack_size,
"stack size %zu is too big, max is %zu",
total_size, stack_object->data.stack_size));

/* User threads may only create other user threads and they can't
* be marked as essential
Expand Down
8 changes: 4 additions & 4 deletions kernel/userspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ void *z_impl_k_object_alloc(enum k_objects otype)
return NULL;
}

dyn_obj->kobj.data = tidx;
dyn_obj->kobj.data.thread_id = tidx;
}

/* The allocating thread implicitly gets permission on kernel objects
Expand Down Expand Up @@ -297,7 +297,7 @@ void k_object_free(void *obj)
sys_dlist_remove(&dyn_obj->obj_list);

if (dyn_obj->kobj.type == K_OBJ_THREAD) {
thread_idx_free(dyn_obj->kobj.data);
thread_idx_free(dyn_obj->kobj.data.thread_id);
}
}
k_spin_unlock(&objfree_lock, key);
Expand Down Expand Up @@ -340,7 +340,7 @@ void z_object_wordlist_foreach(_wordlist_cb_func_t func, void *context)
}
#endif /* CONFIG_DYNAMIC_OBJECTS */

static int thread_index_get(struct k_thread *thread)
static unsigned int thread_index_get(struct k_thread *thread)
{
struct _k_object *ko;

Expand All @@ -350,7 +350,7 @@ static int thread_index_get(struct k_thread *thread)
return -1;
}

return ko->data;
return ko->data.thread_id;
}

static void unref_check(struct _k_object *ko, uintptr_t index)
Expand Down
2 changes: 1 addition & 1 deletion lib/os/mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static struct k_mutex *get_k_mutex(struct sys_mutex *mutex)
return NULL;
}

return (struct k_mutex *)obj->data;
return obj->data.mutex;
}

static bool check_sys_mutex_addr(struct sys_mutex *addr)
Expand Down
4 changes: 2 additions & 2 deletions scripts/elf_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ def __init__(self, type_obj, addr):
self.data = thread_counter
thread_counter = thread_counter + 1
elif self.type_obj.name == "sys_mutex":
self.data = "(uintptr_t)(&kernel_mutexes[%d])" % sys_mutex_counter
self.data = "&kernel_mutexes[%d]" % sys_mutex_counter
sys_mutex_counter += 1
elif self.type_obj.name == "k_futex":
self.data = "(uintptr_t)(&futex_data[%d])" % futex_counter
self.data = "&futex_data[%d]" % futex_counter
futex_counter += 1
else:
self.data = 0
Expand Down
14 changes: 13 additions & 1 deletion scripts/gen_kobject_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@
#endif
"""

metadata_names = {
"K_OBJ_THREAD" : "thread_id",
"K_OBJ__THREAD_STACK_ELEMENT" : "stack_size",
"K_OBJ_SYS_MUTEX" : "mutex",
"K_OBJ_FUTEX" : "futex_data"
}

def write_gperf_table(fp, eh, objs, static_begin, static_end):
fp.write(header)
Expand Down Expand Up @@ -204,7 +210,13 @@ def write_gperf_table(fp, eh, objs, static_begin, static_end):
if is_driver:
flags += " | K_OBJ_FLAG_DRIVER"

fp.write("\", {}, %s, %s, %s\n" % (obj_type, flags, str(ko.data)))
if ko.type_name in metadata_names:
tname = metadata_names[ko.type_name]
else:
tname = "unused"

fp.write("\", {}, %s, %s, { .%s = %s }\n" % (obj_type, flags,
tname, str(ko.data)))

if obj_type == "K_OBJ_THREAD":
idx = math.floor(ko.data / 8)
Expand Down

0 comments on commit f2734ab

Please sign in to comment.