2019-07-04 13:49:06 -07:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Intel Corporation
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2019-07-04 20:17:14 -07:00
|
|
|
#include <kernel.h>
|
|
|
|
#include <ksched.h>
|
|
|
|
#include <kernel_structs.h>
|
|
|
|
#include <kernel_internal.h>
|
2019-12-18 23:57:25 -08:00
|
|
|
#include <offsets_short.h>
|
2019-07-04 20:17:14 -07:00
|
|
|
|
2019-07-24 11:40:24 -07:00
|
|
|
extern void x86_sse_init(struct k_thread *); /* in locore.S */
|
|
|
|
|
2019-11-07 12:43:29 -08:00
|
|
|
void arch_new_thread(struct k_thread *thread, k_thread_stack_t *stack,
|
|
|
|
size_t stack_size, k_thread_entry_t entry,
|
2020-04-23 11:17:14 -07:00
|
|
|
void *parameter1, void *parameter2, void *parameter3)
|
2019-07-04 13:49:06 -07:00
|
|
|
{
|
2019-12-18 14:30:41 -08:00
|
|
|
void *switch_entry;
|
2019-11-05 14:00:30 -08:00
|
|
|
|
2020-04-19 14:28:15 -07:00
|
|
|
z_new_thread_init(thread, Z_THREAD_STACK_BUFFER(stack), stack_size);
|
2019-07-04 20:17:14 -07:00
|
|
|
|
2019-11-05 14:00:30 -08:00
|
|
|
#if CONFIG_X86_STACK_PROTECTION
|
2019-12-18 14:30:41 -08:00
|
|
|
struct z_x86_thread_stack_header *header =
|
|
|
|
(struct z_x86_thread_stack_header *)stack;
|
|
|
|
|
2019-11-05 14:00:30 -08:00
|
|
|
/* Set guard area to read-only to catch stack overflows */
|
|
|
|
z_x86_mmu_set_flags(&z_x86_kernel_ptables, &header->guard_page,
|
|
|
|
MMU_PAGE_SIZE, MMU_ENTRY_READ, Z_X86_MMU_RW,
|
|
|
|
true);
|
|
|
|
#endif
|
2019-12-18 14:30:41 -08:00
|
|
|
#ifdef CONFIG_USERSPACE
|
|
|
|
switch_entry = z_x86_userspace_prepare_thread(thread);
|
|
|
|
thread->arch.cs = X86_KERNEL_CS;
|
|
|
|
thread->arch.ss = X86_KERNEL_DS;
|
|
|
|
#else
|
|
|
|
switch_entry = z_thread_entry;
|
|
|
|
#endif
|
2019-07-04 20:17:14 -07:00
|
|
|
thread->callee_saved.rsp = (long) Z_THREAD_STACK_BUFFER(stack);
|
|
|
|
thread->callee_saved.rsp += (stack_size - 8); /* fake RIP for ABI */
|
2019-12-18 14:30:41 -08:00
|
|
|
thread->callee_saved.rip = (long) switch_entry;
|
2019-07-04 20:17:14 -07:00
|
|
|
thread->callee_saved.rflags = EFLAGS_INITIAL;
|
|
|
|
|
2019-12-18 14:30:41 -08:00
|
|
|
/* Parameters to entry point, which is populated in
|
|
|
|
* thread->callee_saved.rip
|
|
|
|
*/
|
2019-07-04 20:17:14 -07:00
|
|
|
thread->arch.rdi = (long) entry;
|
|
|
|
thread->arch.rsi = (long) parameter1;
|
|
|
|
thread->arch.rdx = (long) parameter2;
|
|
|
|
thread->arch.rcx = (long) parameter3;
|
2019-07-24 11:40:24 -07:00
|
|
|
|
|
|
|
x86_sse_init(thread);
|
2019-09-18 17:55:25 -04:00
|
|
|
|
|
|
|
thread->arch.flags = X86_THREAD_FLAG_ALL;
|
2019-09-23 13:57:12 -04:00
|
|
|
thread->switch_handle = thread;
|
2019-07-04 13:49:06 -07:00
|
|
|
}
|