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

Creating a k_thread as runtime instantiated kernel object using k_malloc causes general protection fault #29629

Closed
maxbachmann opened this issue Oct 29, 2020 · 1 comment · Fixed by #30762
Assignees
Labels
bug The issue is a bug, or the PR is fixing a bug priority: medium Medium impact/importance bug

Comments

@maxbachmann
Copy link
Contributor

maxbachmann commented Oct 29, 2020

Describe the bug
When I try to create a k_thread using a runtime instantiated a kernel object in Kernel Mode using k_malloc Zephyr throws a General protection fault on thread creation. According to #29589 this should be possible. I tried the same using k_calloc, which causes the same error for me. Is there anything else that has to be done before calling k_thread_create?

I only tried this on the up squared board, since qemu is stuck while booting.

To Reproduce
Steps to reproduce the behavior:

  1. Download Error_Minimal.zip and unzip
  2. mkdir build; cd build
  3. cmake -DBOARD=up_squared ..
  4. make
  5. run on up squared board

Expected behavior
It should create the Thread without throwing a protection fault.

Logs and console output

*** Booting Zephyr OS build zephyr-v2.4.0-1054-g1ae2b7cad9db  ***
allocated thread object
[00:00:00.018,000] <err> os: General protection fault (code 0x0)
[00:00:00.018,000] <err> os: RAX: 0x0000000000000000 RBX: 0x0000000000108db8 RCX: 0x0000000000100000 RDX: 0x0000000000d12ff8
[00:00:00.018,000] <err> os: RSI: 0x0000000000d12000 RDI: 0x0000000000108db8 RBP: 0x0000000000108db8 RSP: 0x0000000000d17f28
[00:00:00.018,000] <err> os:  R8: 0x0000000000000000  R9: 0x0000000000000000 R10: 0x0000000000000000 R11: 0x000000000000000f
[00:00:00.018,000] <err> os: R12: 0x0000000000d13000 R13: 0x0000000000000000 R14: 0x0000000000000000 R15: 0x0000000000000000
[00:00:00.018,000] <err> os: RSP: 0x0000000000d17f28 RFLAGS: 0x0000000000010206 CS: 0x0018 CR3: 0x0000000001124000
[00:00:00.018,000] <err> os: RIP: 0x000000000000117f
[00:00:00.018,000] <err> os: >>> ZEPHYR FATAL ERROR 0: CPU exception on CPU 0
[00:00:00.018,000] <err> os: Current thread: 0x0000000000108900 (unknown)
[00:00:00.115,000] <err> os: Halting system

Environment (please complete the following information):

  • OS: Linux
  • Board: Up Squared
  • Toolchain Zephyr SDK
  • Commit SHA: 1ae2b7c

Additional context
Here is the minimal example, so it is not required to download the zip to check the code:

#include <zephyr.h>
#include <sys/printk.h>
#include <kernel.h>
#include <string.h>
#include <stdlib.h>

#define MY_STACK_SIZE 0x1000
#define MY_PRIORITY 5

K_THREAD_STACK_DEFINE(my_stack_area, MY_STACK_SIZE);

void my_entry_point(void* a, void* b, void* c) {
    int counter = 0;
    while (true) {
        printk("counter %i\n", counter);
        ++counter;
        k_msleep(250);
    }
}

void main(void)
{
    struct k_thread* my_thread = (struct k_thread*)k_malloc(sizeof(struct k_thread));

    if (NULL == my_thread){
        printk("failed allocating thread object\n");
        return;
    }

    printk("allocated thread object\n");

    k_thread_create(my_thread, my_stack_area,
                                 K_THREAD_STACK_SIZEOF(my_stack_area),
                                 my_entry_point,
                                 NULL, NULL, NULL,
                                 MY_PRIORITY,
                                 0,
                                 K_NO_WAIT);

    printk("created thread\n");
    k_sleep(K_FOREVER);
}
@maxbachmann maxbachmann added the bug The issue is a bug, or the PR is fixing a bug label Oct 29, 2020
@nashif nashif added the priority: medium Medium impact/importance bug label Oct 29, 2020
@dcpleung dcpleung added the Blocked Blocked by another PR or issue label Oct 30, 2020
@dcpleung
Copy link
Member

Blocked by #17893

@dcpleung dcpleung removed the Blocked Blocked by another PR or issue label Dec 16, 2020
dcpleung added a commit to dcpleung/zephyr that referenced this issue Jan 12, 2021
x86 and x86_64 require certain alignment in the k_thread struct
since the buffer to save/restore FPU/SSE registers requires
strict alignment.

Fixes zephyrproject-rtos#29589
Fixes zephyrproject-rtos#29629

Signed-off-by: Daniel Leung <[email protected]>
andrewboie pushed a commit that referenced this issue Jan 13, 2021
x86 and x86_64 require certain alignment in the k_thread struct
since the buffer to save/restore FPU/SSE registers requires
strict alignment.

Fixes #29589
Fixes #29629

Signed-off-by: Daniel Leung <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug The issue is a bug, or the PR is fixing a bug priority: medium Medium impact/importance bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants