374da9e4a11cfd64a79816c3d358a177b224ff09
[pintos-anon] / src / threads / switch.S
1 #include "threads/switch.h"
2
3 #### struct thread *switch_threads (struct thread *cur, struct thread *next);
4 ####
5 #### Switches from CUR, which must be the running thread, to NEXT,
6 #### which must also be running switch_threads(), returning CUR in
7 #### NEXT's context.
8 ####
9 #### This function works by assuming that the thread we're switching
10 #### into is also running switch_threads().  Thus, all it has to do is
11 #### preserve a few registers on the stack, then switch stacks and
12 #### restore the registers.  As part of switching stacks we record the
13 #### current stack pointer in CUR's thread structure.
14
15 .globl switch_threads 
16 switch_threads:
17         # Save caller's register state.
18         #
19         # Note that the SVR4 ABI allows us to destroy %eax, %ecx, %edx,
20         # but requires us to preserve %ebx, %ebp, %esi, %edi.  See
21         # [SysV-ABI-386] pages 3-11 and 3-12 for details.
22         #
23         # This stack frame must match the one set up by thread_create().
24         pushl %ebx
25         pushl %ebp
26         pushl %esi
27         pushl %edi
28
29         # Get offsetof (struct thread, stack).
30 .globl thread_stack_ofs
31         mov thread_stack_ofs, %edx
32
33         # Save current stack pointer to old thread's stack, if any.
34         movl SWITCH_CUR(%esp), %eax
35         test %eax, %eax
36         jz 1f
37         movl %esp, (%eax,%edx,1)
38 1:
39
40         # Restore stack pointer from new thread's stack.
41         movl SWITCH_NEXT(%esp), %ecx
42         movl (%ecx,%edx,1), %esp
43
44         # Restore caller's register state.
45         popl %edi
46         popl %esi
47         popl %ebp
48         popl %ebx
49         ret
50
51 .globl switch_entry
52 switch_entry:
53         # Discard switch_threads() arguments.
54         addl $8, %esp
55
56         # Call schedule_tail(prev).
57         pushl %eax
58 .globl schedule_tail
59         call schedule_tail
60         addl $4, %esp
61
62         # Start thread proper.
63         ret