Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newbie Question about userspace and how to use zephyr_library_app_memory correctly #83362

Open
TianlongLiang opened this issue Dec 24, 2024 · 2 comments

Comments

@TianlongLiang
Copy link

Thank you for taking the time to look at my question. I'm new to this project and still learning, so I truly appreciate any guidance or help you can provide. If there's something I missed or could improve in my approach, please let me know—I'd be happy to learn and adjust.

Let's say I have a simple project structure like this:

  • src
    • main.c
  • lib-test
    • test.c
    • CMakeLists.txt
  • CMakeLists.txt

In the root CMakeLists.txt I will have something like this:

cmake_minimum_required(VERSION 3.13.1)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})

project(app LANGUAGES C)

add_subdirectory(lib-test)

target_link_libraries(app PRIVATE test_lib)

target_sources(app PRIVATE src/main.c)

And the CMAkeLists.txt for lib-test will be like:

# Define a new Zephyr library
zephyr_library_named (test_lib)

# Add source files to the library
zephyr_library_sources (
  test.c
)

# Specify the memory partition where all globals in the library should be placed.
zephyr_library_app_memory (test_partition)

My plan is to write the test.c like normal C source file would, don't have any Zephyr macro for memory partition for global variables:

#include <stdio.h>

// Define a global integer variable
int a = 1;

// Function to print "Hello, World!" and the value of the global variable a
void print_hello_world(void) {
    printf("Hello, World!\n");
    printf("The value of the global variable a is: %d\n", a);
}

And I define the test_partition in main.c:

#include <stdio.h>
#include <zephyr/app_memory/app_memdomain.h>

#define CONFIG_MAIN_THREAD_STACK_SIZE 8192

#define MAIN_THREAD_STACK_SIZE (CONFIG_MAIN_THREAD_STACK_SIZE)
#define MAIN_THREAD_PRIORITY 5

K_THREAD_STACK_DEFINE(user_mode_thread_stack, MAIN_THREAD_STACK_SIZE);
static struct k_thread user_mode_thread;

K_APPMEM_PARTITION_DEFINE(test_partition);

struct k_mem_domain test_domain;

void print_hello_world(void);

void
test_main(void *arg1, void *arg2, void *arg3){
    print_hello_world();
}

bool
test_user_mode(void)
{
    struct k_mem_partition *domain_parts[] = { &test_partition};

    /* Initialize the memory domain with single partition */
    k_mem_domain_init(&wamr_domain, 1, domain_parts);

    k_tid_t tid =
        k_thread_create(&user_mode_thread, user_mode_thread_stack,
                        MAIN_THREAD_STACK_SIZE, test_main, NULL, NULL, NULL,
                        MAIN_THREAD_PRIORITY, K_USER, K_FOREVER);

    k_mem_domain_add_thread(&test_domain, tid);
    k_thread_start(&tid);

    return tid ? true : false;
}

main(void)
{
    iwasm_user_mode();
    printk("Hello, world from main thread\n");
}

I can get the project to compile and run on the qemu-x86_32 board, but the user mode thread didn't run correctly and hung. My log indicates the fail on spinlock assertion:

ASSERTION FAIL [z_spin_lock_valid(l)] @ WEST_TOPDIR/zephyr/include/zephyr/spinlock.h:136
        Invalid spinlock 0x4011d5e8

Thank you in advance for your support!

Copy link

Hi @TianlongLiang! We appreciate you submitting your first issue for our open-source project. 🌟

Even though I'm a bot, I can assure you that the whole community is genuinely grateful for your time and effort. 🤖💙

@TianlongLiang
Copy link
Author

My prj.conf is as follow, nothing fancy:

CONFIG_USERSPACE=y
CONFIG_PRINTK=y
CONFIG_LOG=y
CONFIG_LOG_BUFFER_SIZE=4096

CONFIG_ASSERT=y

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant