Zephyr Project API 3.7.0
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
main.c File Reference
#include <zephyr/kernel.h>
#include <zephyr/ztest.h>
#include <zephyr/kernel_structs.h>
#include <string.h>
#include <stdlib.h>
#include <zephyr/app_memory/app_memdomain.h>
#include <zephyr/sys/util.h>
#include <zephyr/sys/barrier.h>
#include <zephyr/debug/stack.h>
#include <zephyr/internal/syscall_handler.h>
#include "test_syscall.h"
#include <zephyr/sys/libc-hooks.h>
#include <zephyr/linker/linker-defs.h>
#include <zephyr/syscalls/check_syscall_context_mrsh.c>

Macros

#define INFO(fmt, ...)   printk(fmt, ##__VA_ARGS__)
 
#define PIPE_LEN   1
 
#define BYTES_TO_READ_WRITE   1
 
#define STACKSIZE   (256 + CONFIG_TEST_EXTRA_STACK_SIZE)
 
#define test_oops(provided, expected)
 
#define KERNEL_ONLY_THREAD_STACK_SIZE   (ROUND_UP(1024, CONFIG_MMU_PAGE_SIZE))
 

Functions

 K_SEM_DEFINE (test_revoke_sem, 0, 1)
 
 K_APPMEM_PARTITION_DEFINE (default_part)
 
 K_APP_BMEM (default_part) volatile
 
static void set_fault (unsigned int reason)
 
void k_sys_fatal_error_handler (unsigned int reason, const struct arch_esf *pEsf)
 Fatal error policy handler.
 
 ZTEST_USER (userspace, test_is_usermode)
 Test to check if the thread is in user mode.
 
 ZTEST_USER (userspace, test_write_control)
 Test to write to a control register.
 
 ZTEST_USER (userspace, test_disable_mmu_mpu)
 Test to disable memory protection.
 
 ZTEST_USER (userspace, test_read_kernram)
 Test to read from kernel RAM.
 
 ZTEST_USER (userspace, test_write_kernram)
 Test to write to kernel RAM.
 
 ZTEST_USER (userspace, test_write_kernro)
 Test to write kernel RO.
 
 ZTEST_USER (userspace, test_write_kerntext)
 Test to write to kernel text section.
 
 ZTEST_USER (userspace, test_read_kernel_data)
 Test to read from kernel data section.
 
 ZTEST_USER (userspace, test_write_kernel_data)
 Test to write to kernel data section.
 
 K_APP_DMEM (default_part) volatile
 
 ZTEST_USER (userspace, test_write_priv_stack)
 Test to write to privilege stack.
 
 ZTEST_USER (userspace, test_pass_noperms_object)
 Test to pass object to a system call without permissions.
 
void thread_body (void *p1, void *p2, void *p3)
 
 ZTEST_USER (userspace, test_start_kernel_thread)
 Test to start kernel thread from usermode.
 
static void uthread_read_body (void *p1, void *p2, void *p3)
 
static void uthread_write_body (void *p1, void *p2, void *p3)
 
 ZTEST_USER (userspace, test_read_other_stack)
 Test to read from another thread's stack.
 
 ZTEST_USER (userspace, test_write_other_stack)
 Test to write to other thread's stack.
 
 ZTEST_USER (userspace, test_revoke_noperms_object)
 Test to revoke access to kobject without permission.
 
 ZTEST_USER (userspace, test_access_after_revoke)
 Test to access object after revoking access.
 
static void umode_enter_func (void *p1, void *p2, void *p3)
 
 ZTEST (userspace, test_user_mode_enter)
 Test to check supervisor thread enter one-way to usermode.
 
 K_PIPE_DEFINE (kpipe, 1, 1)
 
 ZTEST_USER (userspace, test_read_kobject_user_pipe)
 Test to read from kobject using pipe.
 
static void user_half (void *arg1, void *arg2, void *arg3)
 
static void spawn_user (volatile bool *to_modify)
 
static void drop_user (volatile bool *to_modify)
 
 ZTEST (userspace_domain, test_1st_init_and_access_other_memdomain)
 Test creation of new memory domains.
 
 ZTEST (userspace_domain, test_domain_add_thread_drop_to_user)
 Show that changing between memory domains and dropping to user mode works as expected.
 
 ZTEST (userspace_domain, test_domain_add_part_drop_to_user)
 
 ZTEST (userspace_domain, test_domain_remove_part_drop_to_user)
 Show that self-removing a partition from a domain we are a member of, and then dropping to user mode faults as expected.
 
 ZTEST (userspace_domain_ctx, test_domain_add_thread_context_switch)
 Show that changing between memory domains and then switching to another thread in the same domain works as expected.
 
 ZTEST (userspace_domain_ctx, test_domain_add_part_context_switch)
 
 ZTEST (userspace_domain_ctx, test_domain_remove_part_context_switch)
 Show that self-removing a partition from a domain we are a member of, and then switching to another user thread in the same domain faults as expected.
 
 ZTEST_USER (userspace, test_unimplemented_syscall)
 Test unimplemented system call.
 
 ZTEST_USER (userspace, test_bad_syscall)
 Test bad syscall handler.
 
 ZTEST (userspace, test_object_recycle)
 Test recycle object.
 
 ZTEST_USER (userspace, test_oops_panic)
 
 ZTEST_USER (userspace, test_oops_oops)
 
 ZTEST_USER (userspace, test_oops_exception)
 
 ZTEST_USER (userspace, test_oops_maxint)
 
 ZTEST_USER (userspace, test_oops_stackcheck)
 
 ZTEST_USER (userspace, test_syscall_context)
 
 ZTEST (userspace, test_tls_leakage)
 
 ZTEST (userspace, test_tls_pointer)
 
void kernel_only_thread_entry (void *p1, void *p2, void *p3)
 
static K_KERNEL_THREAD_DEFINE (kernel_only_thread,(ROUND_UP(1024, CONFIG_MMU_PAGE_SIZE)), kernel_only_thread_entry, NULL, NULL, NULL, 0, 0, 0)
 
 ZTEST (userspace, test_kernel_only_thread)
 
void * userspace_setup (void)
 
 ZTEST_SUITE (userspace, NULL, userspace_setup, NULL, NULL, NULL)
 
 ZTEST_SUITE (userspace_domain, NULL, NULL, NULL, NULL, NULL)
 
 ZTEST_SUITE (userspace_domain_ctx, NULL, NULL, NULL, NULL, NULL)
 

Variables

struct k_mem_domain alternate_domain
 
static ZTEST_BMEM volatile bool expect_fault
 
static ZTEST_BMEM volatile unsigned int expected_reason
 
static int kernel_data
 
static struct k_sem ksem
 
k_thread_stack_t ztest_thread_stack []
 
static struct k_sem recycle_sem
 

Macro Definition Documentation

◆ BYTES_TO_READ_WRITE

#define BYTES_TO_READ_WRITE   1

◆ INFO

#define INFO (   fmt,
  ... 
)    printk(fmt, ##__VA_ARGS__)

◆ KERNEL_ONLY_THREAD_STACK_SIZE

#define KERNEL_ONLY_THREAD_STACK_SIZE   (ROUND_UP(1024, CONFIG_MMU_PAGE_SIZE))

◆ PIPE_LEN

#define PIPE_LEN   1

◆ STACKSIZE

#define STACKSIZE   (256 + CONFIG_TEST_EXTRA_STACK_SIZE)

◆ test_oops

#define test_oops (   provided,
  expected 
)
Value:
do { \
expect_fault = true; \
z_except_reason(provided); \
} while (false)
static ZTEST_BMEM volatile bool expect_fault
Definition main.c:53
static ZTEST_BMEM volatile unsigned int expected_reason
Definition main.c:54
static ZTEST_BMEM struct thread_data expected

Function Documentation

◆ drop_user()

static void drop_user ( volatile bool to_modify)
static

◆ K_APP_BMEM()

K_APP_BMEM ( default_part  ) volatile

◆ K_APP_DMEM()

K_APP_DMEM ( default_part  ) volatile

◆ K_APPMEM_PARTITION_DEFINE()

K_APPMEM_PARTITION_DEFINE ( default_part  )

◆ K_KERNEL_THREAD_DEFINE()

static K_KERNEL_THREAD_DEFINE ( kernel_only_thread  ,
(ROUND_UP(1024, CONFIG_MMU_PAGE_SIZE))  ,
kernel_only_thread_entry  ,
NULL  ,
NULL  ,
NULL  ,
,
,
 
)
static

◆ K_PIPE_DEFINE()

K_PIPE_DEFINE ( kpipe  ,
,
 
)

◆ K_SEM_DEFINE()

K_SEM_DEFINE ( test_revoke_sem  ,
,
 
)

◆ kernel_only_thread_entry()

void kernel_only_thread_entry ( void *  p1,
void *  p2,
void *  p3 
)

◆ set_fault()

static void set_fault ( unsigned int  reason)
static

◆ spawn_user()

static void spawn_user ( volatile bool to_modify)
static

◆ thread_body()

void thread_body ( void *  p1,
void *  p2,
void *  p3 
)

◆ umode_enter_func()

static void umode_enter_func ( void *  p1,
void *  p2,
void *  p3 
)
static

◆ user_half()

static void user_half ( void *  arg1,
void *  arg2,
void *  arg3 
)
static

◆ userspace_setup()

void * userspace_setup ( void  )

◆ uthread_read_body()

static void uthread_read_body ( void *  p1,
void *  p2,
void *  p3 
)
static

◆ uthread_write_body()

static void uthread_write_body ( void *  p1,
void *  p2,
void *  p3 
)
static

◆ ZTEST() [1/12]

ZTEST ( userspace  ,
test_kernel_only_thread   
)

◆ ZTEST() [2/12]

ZTEST ( userspace  ,
test_object_recycle   
)

Test recycle object.

Test recycle valid/invalid kernel object, see if perms_count changes as expected.

See also
k_object_recycle(), k_object_find()

◆ ZTEST() [3/12]

ZTEST ( userspace  ,
test_tls_leakage   
)

◆ ZTEST() [4/12]

ZTEST ( userspace  ,
test_tls_pointer   
)

◆ ZTEST() [5/12]

ZTEST ( userspace  ,
test_user_mode_enter   
)

Test to check supervisor thread enter one-way to usermode.

A thread running in supervisor mode must have one-way operation ability to drop privileges to user mode.

◆ ZTEST() [6/12]

ZTEST ( userspace_domain  ,
test_1st_init_and_access_other_memdomain   
)

Test creation of new memory domains.

We initialize a new memory domain and show that its partition configuration is correct. This new domain has "alt_part" in it, but not "default_part". We then try to modify data in "default_part" and show it produces an exception since that partition is not in the new domain.

This caught a bug once where an MMU system copied page tables for the new domain and accidentally copied memory partition permissions from the source page tables, allowing the write to "default_part" to work.

◆ ZTEST() [7/12]

ZTEST ( userspace_domain  ,
test_domain_add_part_drop_to_user   
)

◆ ZTEST() [8/12]

ZTEST ( userspace_domain  ,
test_domain_add_thread_drop_to_user   
)

Show that changing between memory domains and dropping to user mode works as expected.

◆ ZTEST() [9/12]

ZTEST ( userspace_domain  ,
test_domain_remove_part_drop_to_user   
)

Show that self-removing a partition from a domain we are a member of, and then dropping to user mode faults as expected.

◆ ZTEST() [10/12]

ZTEST ( userspace_domain_ctx  ,
test_domain_add_part_context_switch   
)

◆ ZTEST() [11/12]

ZTEST ( userspace_domain_ctx  ,
test_domain_add_thread_context_switch   
)

Show that changing between memory domains and then switching to another thread in the same domain works as expected.

◆ ZTEST() [12/12]

ZTEST ( userspace_domain_ctx  ,
test_domain_remove_part_context_switch   
)

Show that self-removing a partition from a domain we are a member of, and then switching to another user thread in the same domain faults as expected.

◆ ZTEST_SUITE() [1/3]

ZTEST_SUITE ( userspace  ,
NULL  ,
userspace_setup  ,
NULL  ,
NULL  ,
NULL   
)

◆ ZTEST_SUITE() [2/3]

ZTEST_SUITE ( userspace_domain  ,
NULL  ,
NULL  ,
NULL  ,
NULL  ,
NULL   
)

◆ ZTEST_SUITE() [3/3]

ZTEST_SUITE ( userspace_domain_ctx  ,
NULL  ,
NULL  ,
NULL  ,
NULL  ,
NULL   
)

◆ ZTEST_USER() [1/25]

ZTEST_USER ( userspace  ,
test_access_after_revoke   
)

Test to access object after revoking access.

◆ ZTEST_USER() [2/25]

ZTEST_USER ( userspace  ,
test_bad_syscall   
)

Test bad syscall handler.

When a system call handler decides to terminate the calling thread, the kernel will produce error which indicates the context, where the faulting system call was made from user code.

◆ ZTEST_USER() [3/25]

ZTEST_USER ( userspace  ,
test_disable_mmu_mpu   
)

Test to disable memory protection.

◆ ZTEST_USER() [4/25]

ZTEST_USER ( userspace  ,
test_is_usermode   
)

Test to check if the thread is in user mode.

◆ ZTEST_USER() [5/25]

ZTEST_USER ( userspace  ,
test_oops_exception   
)

◆ ZTEST_USER() [6/25]

ZTEST_USER ( userspace  ,
test_oops_maxint   
)

◆ ZTEST_USER() [7/25]

ZTEST_USER ( userspace  ,
test_oops_oops   
)

◆ ZTEST_USER() [8/25]

ZTEST_USER ( userspace  ,
test_oops_panic   
)

◆ ZTEST_USER() [9/25]

ZTEST_USER ( userspace  ,
test_oops_stackcheck   
)

◆ ZTEST_USER() [10/25]

ZTEST_USER ( userspace  ,
test_pass_noperms_object   
)

Test to pass object to a system call without permissions.

◆ ZTEST_USER() [11/25]

ZTEST_USER ( userspace  ,
test_read_kernel_data   
)

Test to read from kernel data section.

◆ ZTEST_USER() [12/25]

ZTEST_USER ( userspace  ,
test_read_kernram   
)

Test to read from kernel RAM.

◆ ZTEST_USER() [13/25]

ZTEST_USER ( userspace  ,
test_read_kobject_user_pipe   
)

Test to read from kobject using pipe.

◆ ZTEST_USER() [14/25]

ZTEST_USER ( userspace  ,
test_read_other_stack   
)

Test to read from another thread's stack.

◆ ZTEST_USER() [15/25]

ZTEST_USER ( userspace  ,
test_revoke_noperms_object   
)

Test to revoke access to kobject without permission.

User thread can only revoke their own access to an object. In that test user thread to revokes access to unathorized object, as a result the system will assert.

◆ ZTEST_USER() [16/25]

ZTEST_USER ( userspace  ,
test_start_kernel_thread   
)

Test to start kernel thread from usermode.

◆ ZTEST_USER() [17/25]

ZTEST_USER ( userspace  ,
test_syscall_context   
)

◆ ZTEST_USER() [18/25]

ZTEST_USER ( userspace  ,
test_unimplemented_syscall   
)

Test unimplemented system call.

Created a syscall with name missing_syscall() without a verification function. The kernel shall safety handle invocations of unimplemented system calls.

◆ ZTEST_USER() [19/25]

ZTEST_USER ( userspace  ,
test_write_control   
)

Test to write to a control register.

◆ ZTEST_USER() [20/25]

ZTEST_USER ( userspace  ,
test_write_kernel_data   
)

Test to write to kernel data section.

◆ ZTEST_USER() [21/25]

ZTEST_USER ( userspace  ,
test_write_kernram   
)

Test to write to kernel RAM.

◆ ZTEST_USER() [22/25]

ZTEST_USER ( userspace  ,
test_write_kernro   
)

Test to write kernel RO.

◆ ZTEST_USER() [23/25]

ZTEST_USER ( userspace  ,
test_write_kerntext   
)

Test to write to kernel text section.

◆ ZTEST_USER() [24/25]

ZTEST_USER ( userspace  ,
test_write_other_stack   
)

Test to write to other thread's stack.

◆ ZTEST_USER() [25/25]

ZTEST_USER ( userspace  ,
test_write_priv_stack   
)

Test to write to privilege stack.

Variable Documentation

◆ alternate_domain

struct k_mem_domain alternate_domain

◆ expect_fault

ZTEST_BMEM volatile bool expect_fault
static

◆ expected_reason

ZTEST_BMEM volatile unsigned int expected_reason
static

◆ kernel_data

int kernel_data
static

◆ ksem

struct k_sem ksem
static

◆ recycle_sem

struct k_sem recycle_sem
static

◆ ztest_thread_stack

k_thread_stack_t ztest_thread_stack[]
extern