How to handle Interrupt Stack in Userspace (RSP)

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
tsdnz
Member
Member
Posts: 333
Joined: Sun Jun 16, 2013 4:09 am

How to handle Interrupt Stack in Userspace (RSP)

Post by tsdnz »

Hi all,

When user code runs out of stack space the program is terminated.
Checking through my code I realised that an interrupt using the stack could throw the user's stack into an exception (stack exception, followed by user program termination), causing OS dramas.
I have programmed around it and was wondering if anyone has an elegant solution?

As an example it would be possible for a user to use all their stack space and then an ethernet interrupt is fired.

Ideas I have are:
1. Set a bool on interrupt call for CPU and test inside page exception, then allocate another page and set another bool to remove page after interrupt call.
2. Change RSP on interrupt call, storing RAX, RSP in specific memory locations per CPU and/or per interrupt.
3. Inside interrupt use syscall...

What ideas are out there?
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

Re: How to handle Interrupt Stack in Userspace (RSP)

Post by alexfru »

Interrupts must run on their own kernel stack. I don't recall the 64-bit details, but in 32-bit mode external interrupts cause a switch from user mode to kernel mode, which switches the stack to SS0:ESP0 from the current TSS. There must be a similar mechanism in 64-bit mode. Check the CPU documentation.
tsdnz
Member
Member
Posts: 333
Joined: Sun Jun 16, 2013 4:09 am

Re: How to handle Interrupt Stack in Userspace (RSP)

Post by tsdnz »

alexfru wrote:Interrupts must run on their own kernel stack. I don't recall the 64-bit details, but in 32-bit mode external interrupts cause a switch from user mode to kernel mode, which switches the stack to SS0:ESP0 from the current TSS. There must be a similar mechanism in 64-bit mode. Check the CPU documentation.
Yes thanks. I my ?WISE? decision, I am not using TSS, but I am currently looking to see if I can get it to fit my requirements.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: How to handle Interrupt Stack in Userspace (RSP)

Post by bluemoon »

For 64-bits x86_64 the cpu loads rsp from TSS.rsp0 or from IST depends on your IDT entry.

And no, you can't skip the TSS, to use user-mode you need to setup at least one TSS.
By "not using TSS" people indeed usually mean they do not switch task with TSS - but you still need to initialise it once.
tsdnz
Member
Member
Posts: 333
Joined: Sun Jun 16, 2013 4:09 am

Re: How to handle Interrupt Stack in Userspace (RSP)

Post by tsdnz »

bluemoon wrote:For 64-bits x86_64 the cpu loads rsp from TSS.rsp0 or from IST depends on your IDT entry.

And no, you can't skip the TSS, to use user-mode you need to setup at least one TSS.
By "not using TSS" people indeed usually mean they do not switch task with TSS - but you still need to initialise it once.
Yes that is what I thought, just trying to find where I set it up. LOL.
tsdnz
Member
Member
Posts: 333
Joined: Sun Jun 16, 2013 4:09 am

Re: How to handle Interrupt Stack in Userspace (RSP)

Post by tsdnz »

bluemoon wrote:For 64-bits x86_64 the cpu loads rsp from TSS.rsp0 or from IST depends on your IDT entry.

And no, you can't skip the TSS, to use user-mode you need to setup at least one TSS.
By "not using TSS" people indeed usually mean they do not switch task with TSS - but you still need to initialise it once.
Hi, found it. I am not using "User Mode", I remember my decision now.

All programs are running on CPL0.
I have not loaded TSS or IST.

It was good to see the assembly code again, with no comments? LOL
tsdnz
Member
Member
Posts: 333
Joined: Sun Jun 16, 2013 4:09 am

Re: How to handle Interrupt Stack in Userspace (RSP)

Post by tsdnz »

It looks like I am doing things against the "grain" so to speak, so I will have to accept my work-around code.
Post Reply