x86: implement user mode on 64-bit

- In early boot, enable the syscall instruction and set up
  necessary MSRs
- Add a hook to update page tables on context switch
- Properly initialize thread based on whether it will
  start in user or supervisor mode
- Add landing function for system calls to execute the
  desired handler
- Implement arch_user_string_nlen()
- Implement logic for dropping a thread down to user mode
- Reserve per-CPU storage space for user and privilege
  elevation stack pointers, necessary for handling syscalls
  when no free registers are available
- Proper handling of gs register considerations when
  transitioning privilege levels

Kernel page table isolation (KPTI) is not yet implemented.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
This commit is contained in:
Andrew Boie 2019-12-18 14:30:41 -08:00 committed by Anas Nashif
commit 3d80208025
10 changed files with 406 additions and 15 deletions

View file

@ -15,27 +15,36 @@ void arch_new_thread(struct k_thread *thread, k_thread_stack_t *stack,
void *parameter1, void *parameter2, void *parameter3,
int priority, unsigned int options)
{
#if defined(CONFIG_X86_USERSPACE) || defined(CONFIG_X86_STACK_PROTECTION)
struct z_x86_thread_stack_header *header =
(struct z_x86_thread_stack_header *)stack;
#endif
void *switch_entry;
Z_ASSERT_VALID_PRIO(priority, entry);
z_new_thread_init(thread, Z_THREAD_STACK_BUFFER(stack),
stack_size, priority, options);
#if CONFIG_X86_STACK_PROTECTION
struct z_x86_thread_stack_header *header =
(struct z_x86_thread_stack_header *)stack;
/* 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
#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
thread->callee_saved.rsp = (long) Z_THREAD_STACK_BUFFER(stack);
thread->callee_saved.rsp += (stack_size - 8); /* fake RIP for ABI */
thread->callee_saved.rip = (long) z_thread_entry;
thread->callee_saved.rip = (long) switch_entry;
thread->callee_saved.rflags = EFLAGS_INITIAL;
/* Parameters to entry point, which is populated in
* thread->callee_saved.rip
*/
thread->arch.rdi = (long) entry;
thread->arch.rsi = (long) parameter1;
thread->arch.rdx = (long) parameter2;