Skip to content

Commit

Permalink
debug: add helper functions for cap type checking
Browse files Browse the repository at this point in the history
This reduces the usage of magic values to one file only.

Signed-off-by: Axel Heider <axelheider@gmx.de>
  • Loading branch information
axel-h authored and Oliver Scott committed Sep 24, 2020
1 parent 890f707 commit 1baf531
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

cmake_minimum_required(VERSION 3.7.2)

add_subdirectory(libsel4debug)
add_subdirectory(libsel4allocman)
add_subdirectory(libsel4vka)
add_subdirectory(libsel4utils)
Expand All @@ -20,7 +21,6 @@ add_subdirectory(libsel4platsupport)
add_subdirectory(libsel4serialserver)
add_subdirectory(libsel4simple)
add_subdirectory(libsel4simple-default)
add_subdirectory(libsel4debug)
add_subdirectory(libsel4test)
add_subdirectory(libsel4sync)
add_subdirectory(libsel4muslcsys)
Expand Down
20 changes: 20 additions & 0 deletions libsel4debug/include/sel4debug/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,25 @@ __attribute__((no_instrument_function));

void debug_cap_identify(seL4_CPtr cap);

static inline int debug_cap_is_valid(seL4_CPtr cap)
{
return (0 != seL4_DebugCapIdentify(cap));
}

static inline int debug_cap_is_endpoint(seL4_CPtr cap)
{
// there is kernel/generated/arch/object/structures_gen.h that defines
// cap_endpoint_cap = 4, but we can't include it here
return (4 == seL4_DebugCapIdentify(cap));
}

static inline int debug_cap_is_notification(seL4_CPtr cap)
{
// there is kernel/generated/arch/object/structures_gen.h that defines
// cap_notification_cap = 6, but we can't include it here
return (6 == seL4_DebugCapIdentify(cap));
}


void debug_print_bootinfo(seL4_BootInfo *info);

10 changes: 5 additions & 5 deletions libsel4sync/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ list(SORT deps)

add_library(sel4sync STATIC EXCLUDE_FROM_ALL ${deps})

target_include_directories(
sel4sync
PUBLIC
include
)
target_include_directories(sel4sync PUBLIC include)

target_link_libraries(
sel4sync
Expand All @@ -37,3 +33,7 @@ target_link_libraries(
sel4_
autoconf
)

if(KernelDebugBuild)
target_link_libraries(sel4sync PUBLIC sel4debug)
endif()
5 changes: 4 additions & 1 deletion libsel4sync/include/sync/bin_sem.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#include <autoconf.h>
#include <assert.h>
#include <sel4/sel4.h>
#ifdef CONFIG_DEBUG_BUILD
#include <sel4debug/debug.h>
#endif
#include <vka/vka.h>
#include <vka/object.h>
#include <stddef.h>
Expand Down Expand Up @@ -44,7 +47,7 @@ static inline int sync_bin_sem_init(sync_bin_sem_t *sem, seL4_CPtr notification,

#ifdef CONFIG_DEBUG_BUILD
/* Check the cap actually is a notification. */
assert(seL4_DebugCapIdentify(notification) == 6);
assert(debug_cap_is_notification(notification));
#endif

sem->notification.cptr = notification;
Expand Down
5 changes: 4 additions & 1 deletion libsel4sync/include/sync/condition_var.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

#include <autoconf.h>
#include <sel4/sel4.h>
#ifdef CONFIG_DEBUG_BUILD
#include <sel4debug/debug.h>
#endif
#include <vka/vka.h>
#include <vka/object.h>
#include <platsupport/sync/atomic.h>
Expand All @@ -38,7 +41,7 @@ static inline int sync_cv_init(sync_cv_t *cv, seL4_CPtr notification)

#ifdef CONFIG_DEBUG_BUILD
/* Check the cap actually is a notification. */
assert(seL4_DebugCapIdentify(notification) == 6);
assert(debug_cap_is_notification(notification));
#endif

cv->notification.cptr = notification;
Expand Down
7 changes: 5 additions & 2 deletions libsel4sync/include/sync/sem-bare.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@
#include <assert.h>
#include <limits.h>
#include <sel4/sel4.h>
#ifdef CONFIG_DEBUG_BUILD
#include <sel4debug/debug.h>
#endif
#include <stddef.h>
#include <platsupport/sync/atomic.h>

static inline int sync_sem_bare_wait(seL4_CPtr ep, volatile int *value)
{
#ifdef CONFIG_DEBUG_BUILD
/* Check the cap actually is an EP. */
assert(seL4_DebugCapIdentify(ep) == 4);
assert(debug_cap_is_endpoint(ep));
#endif
assert(value != NULL);
int oldval;
Expand Down Expand Up @@ -80,7 +83,7 @@ static inline int sync_sem_bare_post(seL4_CPtr ep, volatile int *value)
{
#ifdef CONFIG_DEBUG_BUILD
/* Check the cap actually is an EP. */
assert(seL4_DebugCapIdentify(ep) == 4);
assert(debug_cap_is_endpoint(ep));
#endif
assert(value != NULL);
/* We can do an "unsafe" increment here because we know the lock cannot be
Expand Down
5 changes: 4 additions & 1 deletion libsel4sync/include/sync/sem.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#include <autoconf.h>
#include <assert.h>
#include <sel4/sel4.h>
#ifdef CONFIG_DEBUG_BUILD
#include <sel4debug/debug.h>
#endif
#include <vka/vka.h>
#include <vka/object.h>
#include <stddef.h>
Expand All @@ -38,7 +41,7 @@ static inline int sync_sem_init(sync_sem_t *sem, seL4_CPtr ep, int value)
}
#ifdef CONFIG_DEBUG_BUILD
/* Check the cap actually is an EP. */
assert(seL4_DebugCapIdentify(ep) == 4);
assert(debug_cap_is_endpoint(ep));
#endif

sem->ep.cptr = ep;
Expand Down
5 changes: 4 additions & 1 deletion libsel4sync/src/recursive_mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include <limits.h>

#include <sel4/sel4.h>
#ifdef CONFIG_DEBUG_BUILD
#include <sel4debug/debug.h>
#endif

static void *thread_id(void)
{
Expand All @@ -31,7 +34,7 @@ int sync_recursive_mutex_init(sync_recursive_mutex_t *mutex, seL4_CPtr notificat
}
#ifdef CONFIG_DEBUG_BUILD
/* Check the cap actually is a notification. */
assert(seL4_DebugCapIdentify(notification) == 6);
assert(debug_cap_is_notification(notification));
#endif

mutex->notification.cptr = notification;
Expand Down
3 changes: 3 additions & 0 deletions libsel4vka/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,6 @@ target_link_libraries(
sel4vka_Config
sel4_autoconf
)
if(KernelDebugBuild)
target_link_libraries(sel4vka PUBLIC sel4debug)
endif()
5 changes: 4 additions & 1 deletion libsel4vka/include/vka/vka.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

#include <sel4/sel4.h>
#include <sel4/types.h>
#ifdef CONFIG_DEBUG_BUILD
#include <sel4debug/debug.h>
#endif
#include <assert.h>
#include <stdint.h>
#include <utils/util.h>
Expand Down Expand Up @@ -192,7 +195,7 @@ static inline int vka_cspace_alloc_path(vka_t *vka, cspacepath_t *res)
static inline void vka_cspace_free(vka_t *vka, seL4_CPtr slot)
{
#ifdef CONFIG_DEBUG_BUILD
if (seL4_DebugCapIdentify(slot) != 0) {
if (debug_cap_is_valid(slot)) {
ZF_LOGF("slot is not free: call vka_cnode_delete first");
/* this terminates the system */
}
Expand Down

0 comments on commit 1baf531

Please sign in to comment.