1
votes

I have read about how Linux uses hard IRQ stack and soft IRQ stack per CPU in case the exception stack determined to be only 4KB in compilation time of the kernel.

Now I know that in the case of 8KB kernel mode stack, when the CPU looks in the IDT for interrupt handler and find out there is a need for a change of privilege he get the address of the kernel mode stack from the TSS segment of the process. Also in the case of 4KB kernel mode stack the cpu gets the address of the exception stack from the TSS segment.

What I am not clear about is how the CPU gets the address of the hard_irq stack or soft_irq stack in case of handling an interrupt.

Can someone explain it to me?

1

1 Answers

0
votes

In the function do_IRQ() that gets called right after saving the cpu registers on the stack, there is a check whether the stack the current interrupt handler executes on is the hard IRQ stack. It does that by the following code(taken from https://elixir.bootlin.com/linux/latest/source/arch/powerpc/kernel/irq.c#L659)

void *cursp, *irqsp, *sirqsp;
cursp = (void *)(current_stack_pointer() & ~(THREAD_SIZE - 1));
irqsp = hardirq_ctx[raw_smp_processor_id()];
sirqsp = softirq_ctx[raw_smp_processor_id()];

/* Already there ? */
if (unlikely(cursp == irqsp || cursp == sirqsp)) {
    __do_irq(regs);
    set_irq_regs(old_regs);
    return;
}

If it is not executing in the hard IRQ stack already than it is executing in the exception stack because this is the stack that the TSS segment contains(to be able to switch to kernel mode). In that case the function switch to that stack explicitly.

Note:

It is possible for the handler the start is execution in the hard IRQ stack if the interrupt occurred during the handling of another interrupt and thus there was not any change to another stack because the CPU was already executing in kernel mode.