From 5141044c0620a2e9b9bd4d0aa60ac2de7049764e Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Wed, 22 Apr 2020 17:31:37 +0200 Subject: [PATCH] use popal/pushal for context switch Should take fewer clock nd save esp --- core/cpu_context.c | 5 +++-- core/cpu_context_switch.S | 42 +++++++++++---------------------------- 2 files changed, 15 insertions(+), 32 deletions(-) diff --git a/core/cpu_context.c b/core/cpu_context.c index dcba68b..c4f376c 100644 --- a/core/cpu_context.c +++ b/core/cpu_context.c @@ -36,12 +36,13 @@ struct cpu_state { thread, even for a user thread */ uint16_t alignment_padding; /* unused */ uint32_t eax; - uint32_t ebx; uint32_t ecx; uint32_t edx; + uint32_t ebx; + uint32_t ebp; + uint32_t esp; uint32_t esi; uint32_t edi; - uint32_t ebp; /* MUST NEVER CHANGE (dependent on the IA32 iret instruction) */ uint32_t error_code; diff --git a/core/cpu_context_switch.S b/core/cpu_context_switch.S index ccfa146..aa7e5f5 100644 --- a/core/cpu_context_switch.S +++ b/core/cpu_context_switch.S @@ -6,20 +6,14 @@ .globl cpu_context_switch .type cpu_context_switch, @function cpu_context_switch: - // arg2= to_context -- esp+64 - // arg1= from_context -- esp+60 - // caller ip -- esp+56 - pushf // (eflags) esp+52 - pushl %cs // (cs) esp+48 - pushl $resume_pc // (ip) esp+44 - pushl $0 // (error code) esp+40 - pushl %ebp // esp+36 - pushl %edi // esp+32 - pushl %esi // esp+28 - pushl %edx // esp+24 - pushl %ecx // esp+20 - pushl %ebx // esp+16 - pushl %eax // esp+12 + // arg2= to_context -- esp+68 + // arg1= from_context -- esp+64 + // caller ip -- esp+60 + pushf // (eflags) esp+56 + pushl %cs // (cs) esp+52 + pushl $resume_pc // (ip) esp+48 + pushl $0 // (error code) esp+44 + pushal // (general reg) esp+12+8*4 subl $2, %esp // (alignment) esp+10 pushw %ss // esp+8 pushw %ds // esp+6 @@ -32,11 +26,11 @@ cpu_context_switch: */ /* Store the address of the saved context */ - movl 60(%esp), %ebx + movl 64(%esp), %ebx movl %esp, (%ebx) /* This is the proper context switch ! We change the stack here */ - movl 64(%esp), %esp + movl 68(%esp), %esp /* Restore the CPU context */ popw %gs @@ -45,13 +39,7 @@ cpu_context_switch: popw %ds popw %ss addl $2,%esp - popl %eax - popl %ebx - popl %ecx - popl %edx - popl %esi - popl %edi - popl %ebp + popal addl $4, %esp /* Ignore "error code" */ /* This restores the eflags, the cs and the eip registers */ @@ -94,13 +82,7 @@ cpu_context_exit_to: popw %ds popw %ss addl $2,%esp - popl %eax - popl %ebx - popl %ecx - popl %edx - popl %esi - popl %edi - popl %ebp + popal addl $4, %esp /* Ignore "error code" */ /* This restores the eflags, the cs and the eip registers */