Kernel: Fix 32 bit target

Rewrite some assembly and add some required casts
This commit is contained in:
Bananymous 2025-11-11 05:08:22 +02:00
parent 46f9a9053f
commit 09175d1799
7 changed files with 61 additions and 59 deletions

View File

@ -1,12 +1,6 @@
// arguments in EAX, EBX, ECX, EDX, ESI, EDI
.global asm_syscall_handler
asm_syscall_handler:
# save segment registers
pushw %ds
pushw %es
pushw %fs
pushw %gs
# save general purpose registers
pushl %ebx
pushl %ecx
@ -18,13 +12,12 @@ asm_syscall_handler:
# align stack
movl %esp, %ebp
subl $15, %esp
andl $0xFFFFFFF0, %esp
andl $-16, %esp
# push arguments
subl $4, %esp
pushl %ebp
addl $32, (%esp)
addl $24, (%esp)
pushl %edi
pushl %esi
pushl %edx
@ -44,6 +37,15 @@ asm_syscall_handler:
movl %ebp, %esp
# restore userspace segments
movw $(0x20 | 3), %bx
movw %bx, %ds
movw %bx, %es
movw $(0x30 | 3), %bx
movw %bx, %fs
movw $(0x38 | 3), %bx
movw %bx, %gs
# restore general purpose registers
popl %ebp
popl %esi
@ -52,12 +54,6 @@ asm_syscall_handler:
popl %ecx
popl %ebx
# restore segment registers
popw %gs
popw %fs
popw %es
popw %ds
iret
.global sys_fork_trampoline

View File

@ -31,8 +31,6 @@ start_kernel_thread:
subl $12, %esp
pushl %edi
call *%esi
addl $16, %esp
.global start_userspace_thread
start_userspace_thread:
@ -41,14 +39,11 @@ start_userspace_thread:
call get_thread_start_sp
movl %eax, %esp
# ds, es = user data
movw $(0x20 | 3), %bx
movw %bx, %ds
movw %bx, %es
# fs
movw $(0x30 | 3), %bx
movw %bx, %fs
# gs
movw $(0x38 | 3), %bx
movw %bx, %gs

View File

@ -1,12 +1,7 @@
.macro push_userspace
pushw %gs
pushw %fs
pushw %es
pushw %ds
pushal
.endm
.macro maybe_load_kernel_segments, n
cmpb $0x08, \n(%esp)
je 1f
.macro load_kernel_segments
movw $0x10, %ax
movw %ax, %ds
movw %ax, %es
@ -14,19 +9,26 @@
movw $0x28, %ax
movw %ax, %gs
1:
.endm
.macro pop_userspace
popal
popw %ds
popw %es
popw %fs
popw %gs
.macro maybe_load_userspace_segments, n
cmpb $0x08, \n(%esp)
je 1f
movw $(0x20 | 3), %bx
movw %bx, %ds
movw %bx, %es
movw $(0x30 | 3), %bx
movw %bx, %fs
movw $(0x38 | 3), %bx
movw %bx, %gs
1:
.endm
isr_stub:
push_userspace
load_kernel_segments
pushal
maybe_load_kernel_segments 44
cld
movl %cr0, %eax; pushl %eax
@ -34,33 +36,39 @@ isr_stub:
movl %cr3, %eax; pushl %eax
movl %cr4, %eax; pushl %eax
movl %esp, %eax // register ptr
leal 64(%esp), %ebx // interrupt stack ptr
movl 60(%esp), %ecx // error code
movl 56(%esp), %edx // isr number
movl 48(%esp), %edi // isr number
movl 52(%esp), %esi // error code
leal 56(%esp), %edx // interrupt stack ptr
movl %esp, %ecx // register ptr
# stack frame for stack trace
leal 56(%esp), %eax
pushl (%eax)
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
pushl %eax
pushl %ebx
pushl %ecx
pushl %edx
pushl %esi
pushl %edi
call cpp_isr_handler
movl %ebp, %esp
addl $16, %esp
addl $24, %esp
pop_userspace
maybe_load_userspace_segments 44
popal
addl $8, %esp
iret
irq_stub:
push_userspace
load_kernel_segments
pushal
maybe_load_kernel_segments 44
cld
movl 40(%esp), %edi # interrupt number
movl 32(%esp), %edi # interrupt number
movl %esp, %ebp
andl $-16, %esp
@ -71,7 +79,8 @@ irq_stub:
movl %ebp, %esp
pop_userspace
maybe_load_userspace_segments 44
popal
addl $8, %esp
iret
@ -99,8 +108,8 @@ asm_yield_handler:
.global asm_ipi_handler
asm_ipi_handler:
push_userspace
load_kernel_segments
pushal
maybe_load_kernel_segments 36
cld
movl %esp, %ebp
@ -110,13 +119,14 @@ asm_ipi_handler:
movl %ebp, %esp
pop_userspace
maybe_load_userspace_segments 36
popal
iret
.global asm_timer_handler
asm_timer_handler:
push_userspace
load_kernel_segments
pushal
maybe_load_kernel_segments 36
cld
movl %esp, %ebp
@ -126,7 +136,8 @@ asm_timer_handler:
movl %ebp, %esp
pop_userspace
maybe_load_userspace_segments 36
popal
iret
.macro isr n

View File

@ -856,7 +856,7 @@ acpi_release_global_lock:
};
// TODO: EC can also reside in memory space
auto crs_buffer = BAN::ConstByteSpan { crs.as.str_buf->bytes, crs.as.str_buf->size };
auto crs_buffer = BAN::ConstByteSpan { crs.as.str_buf->bytes, static_cast<size_t>(crs.as.str_buf->size) };
const auto data_port = TRY(extract_io_port(crs_buffer));
const auto command_port = TRY(extract_io_port(crs_buffer));

View File

@ -29,8 +29,8 @@ namespace Kernel
gdt->write_entry(0x20, 0x00000000, 0xFFFFF, 0xF2, data_flags); // user data
#if ARCH(i686)
gdt->write_entry(0x28, reinterpret_cast<uint32_t>(processor), sizeof(Processor), 0x92, 0x4); // processor data
gdt->write_entry(0x30, 0x00000000, 0x00000, 0x00, 0x0); // fsbase
gdt->write_entry(0x38, 0x00000000, 0x00000, 0x00, 0x0); // gsbase
gdt->write_entry(0x30, 0x00000000, 0x00000, 0xF2, data_flags); // fsbase
gdt->write_entry(0x38, 0x00000000, 0x00000, 0xF2, data_flags); // gsbase
#endif
gdt->write_tss();

View File

@ -321,7 +321,7 @@ namespace Kernel::Input
result.type = type;
BAN::Optional<ACPI::ResourceData> data;
ACPI::ResourceParser parser({ crs_obj->node.as.str_buf->bytes, crs_obj->node.as.str_buf->size });
ACPI::ResourceParser parser({ crs_obj->node.as.str_buf->bytes, static_cast<size_t>(crs_obj->node.as.str_buf->size) });
while ((data = parser.get_next()).has_value())
{
switch (data->type)

View File

@ -386,7 +386,7 @@ namespace Kernel
vaddr_t vaddr = userspace_stack_top() - needed_size;
const size_t page_count = BAN::Math::div_round_up(needed_size, PAGE_SIZE);
const size_t page_count = BAN::Math::div_round_up<size_t>(needed_size, PAGE_SIZE);
for (size_t i = 0; i < page_count; i++)
TRY(m_userspace_stack->allocate_page_for_demand_paging(vaddr + i * PAGE_SIZE));