f58c4a8931a15c8908b8d2264608e1b8ace344d6
[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 .intel_syntax noprefix
16
17 .globl switch_threads
18 .func switch_threads
19 switch_threads:
20         # Save caller's register state.
21         #
22         # Note that the SVR4 ABI allows us to destroy eax, ecx, edx,
23         # but requires us to preserve ebx, ebp, esi, edi.  See
24         # [SysV-ABI-386] pages 3-11 and 3-12 for details.
25         #
26         # This stack frame must match the one set up by thread_create().
27         push ebx
28         push ebp
29         push esi
30         push edi
31
32         # Get offsetof (struct thread, stack).
33 .globl thread_stack_ofs
34         mov edx, thread_stack_ofs
35
36         # Save current stack pointer to old thread's stack, if any.
37         mov eax, SWITCH_CUR[esp]
38         test eax, eax
39         jz 1f
40         mov [eax + edx], esp
41 1:
42
43         # Restore stack pointer from new thread's stack.
44         mov ecx, SWITCH_NEXT[esp]
45         mov esp, [ecx + edx]
46
47         # Restore caller's register state.
48         pop edi
49         pop esi
50         pop ebp
51         pop ebx
52         ret
53 .endfunc
54
55 .globl switch_entry
56 .func switch_entry
57 switch_entry:
58         # Discard switch_threads() arguments.
59         add esp, 8
60
61         # Call schedule_tail(prev).
62         push eax
63 .globl schedule_tail
64         call schedule_tail
65         add esp, 4
66
67         # Start thread proper.
68         ret
69 .endfunc