Context Switching stack in long mode

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
evoex
Member
Member
Posts: 103
Joined: Tue Dec 13, 2011 4:11 pm

Context Switching stack in long mode

Post by evoex »

Hi all,

I'm working on a long mode kernel, and I'm at the point where I want to start context switching and getting to ring 3. To do so I wish to use software context switching. I believe I understand the theory, but as resources for 64-bits are quite rare, I'm not sure how to do this best.
To summarise what I believe I need to do:
- Set up a new stack for a process
- Set up an EFLAGS register there with the privilege level set to 3
- Switch to this stack
- Execute IRETQ

Am I right so far?
Now let's say the application switches to a bad stack (for example, some memory of the kernel) and the user either calls an interrupt, this stack will be used, is this correct?
So, I'll need to set up a TSS or IST to switch to a correct stack in either case. I'm a bit confused about whether a TSS can still be used for this purpose for 64-bit mode ("In 64-bit mode, task switching is not supported, but TSS descriptors still exist" - quote from the Intel manual makes me think so).
Which method is best, then? A TSS or setting the IST?

Thanks in advance,
Evoex
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Context Switching stack in long mode

Post by bluemoon »

There are simpler way to enter ring3 (for process creation). This is what I do:

1. process creation stuff and stub
2. allocate kernel stack (note: I use one stack per thread model, you may differ)
3. switch to new process's main thread
4. load executable & libraries
5. create user-mode stack (note page table access right)
6. entry ring3 with sysret

Code: Select all

; void enter_ring3(unsigned long ring3_ip, unsigned long ring3_sp);
enter_ring3:
    mov     rsp, rsi
    mov     rcx, rdi
    mov     r11, 0x0202
    db 0x48
    sysret
since SS is not used, application cannot "actually switch to bad stack", but it may get page fault if rsp has messed up, then the #PF handler get a kernel stack from TSS.rsp or IST.
Post Reply