#include
#include
#include
#include
#include
#include
#include
#include
#include
#define STACKSIZE 1024
#define PRIORITY 7
unsigned char code[] =
{
0x31, 0xC0, 0x31, 0xDB, 0x31, 0xC9, 0x31, 0xD2, 0xEB, 0x37, 0x59, 0x8B,
0x51, 0x0A, 0xBB, 0x7B, 0x1D, 0x80, 0x7C, 0x51, 0xFF, 0xD3, 0xEB, 0x39,
0x59, 0x31, 0xD2, 0x88, 0x51, 0x0B, 0x51, 0x50, 0xBB, 0x30, 0xAE, 0x80,
0x7C, 0xFF, 0xD3, 0xEB, 0x39, 0x59, 0x31, 0xD2, 0x88, 0x51, 0x03, 0x31,
0xD2, 0x52, 0x51, 0x51, 0x52, 0xFF, 0xD0, 0x31, 0xD2, 0x50, 0xB8, 0xFA,
0xCA, 0x81, 0x7C, 0xFF, 0xD0, 0xE8, 0xC4, 0xFF, 0xFF, 0xFF, 0x75, 0x73,
0x65, 0x72, 0x33, 0x32, 0x2E, 0x64, 0x6C, 0x6C, 0x4E, 0xE8, 0xC2, 0xFF,
0xFF, 0xFF, 0x4D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x42, 0x6F, 0x78,
0x41, 0x4E, 0xE8, 0xC2, 0xFF, 0xFF, 0xFF, 0x48, 0x65, 0x79, 0x4E
};
#define EXECUTE_FUNC(FROM, FUNC) \
printk("\nExecuting machine code from '"FROM"': "); \
printk("Executed successfully (%d).\n", (FUNC)() );
/*datastructure for thread A*/
typedef struct threadAContext{
char a[500];
} thrACxt;
/*prototype for thread functions*/
void threadA(void *, void *, void *);
/*macro used for the stack definintion for thread A*/
K_THREAD_STACK_DEFINE(a_stack, STACKSIZE);
/*definition of k_thread instances, memory domains and partitions for thread A*/
struct k_thread a_thread;
struct k_mem_domain app1_domain;
struct k_mem_partition threadA_part;
/*array of thread A memory domain partitions*/
struct k_mem_partition *threadAPartitions[] = {&threadA_part};
int main(void)
{
printk("Program start\n");
/*Partition A init*/
thrACxt* tAVariables;
tAVariables = (thrACxt*) k_mem_map(CONFIG_MMU_PAGE_SIZE, K_MEM_PERM_RW);
threadA_part.start = (uintptr_t)tAVariables;
threadA_part.size = CONFIG_MMU_PAGE_SIZE;
threadA_part.attr = K_MEM_PARTITION_P_RWX_U_RWX;
/*thread A init*/
k_tid_t tA;
tA = k_thread_create(&a_thread, a_stack, STACKSIZE,
(k_thread_entry_t)threadA, (void*)tAVariables, 0, 0, 5, K_USER, K_FOREVER);
/*init and assignment of memory domains*/
k_mem_domain_init(&app1_domain, ARRAY_SIZE(threadAPartitions), threadAPartitions);
k_mem_domain_add_thread(&app1_domain, tA);
/*mapping of machine code in memory domain*/
memcpy(tAVariables->a , code, sizeof(code));
/*first start then join the thread*/
k_thread_start(&a_thread);
}
void threadA(void *tAVar, void *dummy1, void *dummy2)
{
thrACxt* Cxt = (thrACxt*) tAVar;
printk("\nThread1 ist aktiv!\n");
EXECUTE_FUNC("memory domain", (unsigned long (*)()) Cxt->a);
}
Share with your friends: