From 1ee37cb671169e138013029c724e3b0731c1a39e Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 22 Jul 2024 00:10:42 +0300 Subject: [PATCH] Kernel: Make sure stack is aligned on interrupts for i686 target I just realized that only x86_64 aligns stack pointer when interrupt is triggered. --- kernel/arch/i686/Syscall.S | 13 ++++++++++--- kernel/arch/i686/interrupts.S | 25 ++++++++++++++++++++----- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/kernel/arch/i686/Syscall.S b/kernel/arch/i686/Syscall.S index 92753b8173..4edf07cca6 100644 --- a/kernel/arch/i686/Syscall.S +++ b/kernel/arch/i686/Syscall.S @@ -15,8 +15,14 @@ asm_syscall_handler: pushl %esi pushl %ebp - # align stack and push arguments - pushl %esp + # align stack + movl %esp, %ebp + subl $15, %esp + andl $0xFFFFFFF0, %esp + + # push arguments + subl $4, %esp + pushl %ebp addl $32, (%esp) pushl %edi pushl %esi @@ -34,7 +40,8 @@ asm_syscall_handler: movw %ax, %gs call cpp_syscall_handler - addl $28, %esp + + movl %ebp, %esp # restore general purpose registers popl %ebp diff --git a/kernel/arch/i686/interrupts.S b/kernel/arch/i686/interrupts.S index 3a7a8e54bb..f93d190595 100644 --- a/kernel/arch/i686/interrupts.S +++ b/kernel/arch/i686/interrupts.S @@ -38,13 +38,18 @@ isr_stub: movl 60(%esp), %ecx // error code movl 56(%esp), %edx // isr number - subl $12, %esp + movl %esp, %ebp + subl $15, %esp + andl $0xFFFFFFF0, %esp + pushl %eax pushl %ebx pushl %ecx pushl %edx call cpp_isr_handler - addl $44, %esp + + movl %ebp, %esp + addl $16, %esp pop_userspace addl $8, %esp @@ -56,10 +61,15 @@ irq_stub: movl 40(%esp), %eax # interrupt number + movl %esp, %ebp + subl $15, %esp + andl $0xFFFFFFF0, %esp + subl $12, %esp pushl %eax call cpp_irq_handler - addl $16, %esp + + movl %ebp, %esp pop_userspace addl $8, %esp @@ -73,11 +83,16 @@ asm_yield_handler: movl %esp, %eax # interrupt registers ptr leal 32(%esp), %ebx # interrupt stack ptr - subl $4, %esp + movl %esp, %ebp + subl $15, %esp + andl $0xFFFFFFF0, %esp + + subl $8, %esp pushl %eax pushl %ebx call cpp_yield_handler - addl $12, %esp + + movl %ebp, %esp popal iret