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
|
.globl switch
switch:
# Load task page table
mov (%rdi), %rax
mov %rax, %cr3
# ds
mov 96(%rdi), %rax
mov %ax, %ds
# es
mov 104(%rdi), %rax
mov %ax, %es
# fs
mov 112(%rdi), %rax
mov %ax, %fs
# gs
mov 120(%rdi), %rax
mov %ax, %gs
# First load the task stack
mov 64(%rdi), %rsp
# Prepare for iret
push 56(%rdi) # ss
push 64(%rdi) # rsp
push 128(%rdi) # Flags
pop %rax
bts $9, %rax # Enable interrupt
not %rax
bts $14, %rax # NT flag
not %rax
push %rax # Apply flags changes
push 40(%rdi) # cs
push 48(%rdi) # rip
# Prepare to restore general registers
push 8(%rdi) # rax
push 16(%rdi) # rbx
push 24(%rdi) # rcx
push 32(%rdi) # rdx
push 72(%rdi) # rbp
push 80(%rdi) # rsi
push 88(%rdi) # rdi
push 144(%rdi) # r8
push 152(%rdi) # r9
push 160(%rdi) # r10
push 168(%rdi) # r11
push 176(%rdi) # r12
push 184(%rdi) # r13
push 192(%rdi) # r14
push 200(%rdi) # r15
# Restore general registers
pop %r15
pop %r14
pop %r13
pop %r12
pop %r11
pop %r10
pop %r9
pop %r8
pop %rdi
pop %rsi
pop %rbp
pop %rdx
pop %rcx
pop %rbx
pop %rax
# Perform task switching
iretq
|