forked from mit-pdos/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtrapasm.S
114 lines (103 loc) · 2.39 KB
/
trapasm.S
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# vectors.S sends all traps here.
.global alltraps
alltraps:
# Build trap frame.
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %r11
pushq %r10
pushq %r9
pushq %r8
pushq %rdi
pushq %rsi
pushq %rbp
pushq %rdx
pushq %rcx
pushq %rbx
pushq %rax
movq %rsp, %rdi # frame in arg1
callq trap
# Return falls through to trapret...
.global trapret
trapret:
popq %rax
popq %rbx
popq %rcx
popq %rdx
popq %rbp
popq %rsi
popq %rdi
popq %r8
popq %r9
popq %r10
popq %r11
popq %r12
popq %r13
popq %r14
popq %r15
addq $16, %rsp # discard trapnum and errorcode
iretq
#PAGEBREAK!
.global syscall_entry
syscall_entry:
# switch to kernel stack. With the syscall instruction,
# this is a kernel resposibility
# store %rsp on the top of proc->kstack,
movq %rax, %fs:(0) # save %rax above __thread vars
movq %fs:(-8), %rax # %fs:(-8) is proc (the last __thread)
movq 0x10(%rax), %rax # get proc->kstack (see struct proc)
addq $(4096-16), %rax # %rax points to tf->rsp
movq %rsp, (%rax) # save user rsp to tf->rsp
movq %rax, %rsp # switch to the kstack
movq %fs:(0), %rax # restore %rax
pushq %r11 # rflags
pushq $0 # cs is ignored
pushq %rcx # rip (next user insn)
pushq $0 # err
pushq $0 # trapno ignored
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %r11
pushq %r10
pushq %r9
pushq %r8
pushq %rdi
pushq %rsi
pushq %rbp
pushq %rdx
pushq %rcx
pushq %rbx
pushq %rax
movq %rsp, %rdi # frame in arg1
callq syscall
# Return falls through to syscall_trapret...
#PAGEBREAK!
.global syscall_trapret
syscall_trapret:
popq %rax
popq %rbx
popq %rcx
popq %rdx
popq %rbp
popq %rsi
popq %rdi
popq %r8
popq %r9
popq %r10
popq %r11
popq %r12
popq %r13
popq %r14
popq %r15
addq $40, %rsp # discard trapnum, errorcode, rip, cs and rflags
# to make sure we don't get any interrupts on the user stack while in
# supervisor mode. this is actually slightly unsafe still,
# since some interrupts are nonmaskable.
# See https://www.felixcloutier.com/x86/sysret
cli
movq (%rsp), %rsp # restore the user stack
sysretq