⚠️ VeridianOS Kernel Documentation - This is low-level kernel code. All functions are unsafe unless explicitly marked otherwise. no_std

enter_usermode

Function enter_usermode 

Source
pub unsafe fn enter_usermode(
    entry_point: u64,
    user_stack: u64,
    user_cs: u64,
    user_ss: u64,
) -> !
Expand description

Enter user mode for the first time via iretq.

The iretq instruction pops SS, RSP, RFLAGS, CS, RIP from the stack and transitions the CPU to the privilege level specified in the CS selector’s RPL field.

§Arguments

  • entry_point: User-space RIP (entry point of the user program)
  • user_stack: User-space RSP (top of user stack)
  • user_cs: User code segment selector with RPL=3 (0x33)
  • user_ss: User data segment selector with RPL=3 (0x2B)

§Safety

  • entry_point must be a valid user-space address with executable code mapped
  • user_stack must be a valid user-space stack address, 16-byte aligned
  • The correct page tables must be loaded in CR3 with USER-accessible mappings
  • Per-CPU data (kernel_rsp) must be set before calling this, otherwise the first syscall or interrupt will crash due to invalid kernel stack
  • The GDT must contain valid Ring 3 segments at the specified selectors