-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_jmp.c
More file actions
150 lines (123 loc) · 2.97 KB
/
Copy pathdemo_jmp.c
File metadata and controls
150 lines (123 loc) · 2.97 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* sigsetjmp/siglongjmp demo program.
* Hebrew University OS course.
* Author: OS, os@cs.huji.ac.il
*/
#include <stdio.h>
#include <setjmp.h>
#include <signal.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdbool.h>
#ifdef __x86_64__
/* code for 64 bit Intel arch */
typedef unsigned long address_t;
#define JB_SP 6
#define JB_PC 7
/* A translation is required when using an address of a variable.
Use this as a black box in your code. */
address_t translate_address(address_t addr)
{
address_t ret;
asm volatile("xor %%fs:0x30,%0\n"
"rol $0x11,%0\n"
: "=g" (ret)
: "0" (addr));
return ret;
}
#else
/* code for 32 bit Intel arch */
typedef unsigned int address_t;
#define JB_SP 4
#define JB_PC 5
/* A translation is required when using an address of a variable.
Use this as a black box in your code. */
address_t translate_address(address_t addr)
{
address_t ret;
asm volatile("xor %%gs:0x18,%0\n"
"rol $0x9,%0\n"
: "=g" (ret)
: "0" (addr));
return ret;
}
#endif
#define SECOND 1000000
#define STACK_SIZE 4096
typedef void (*thread_entry_point)(void);
char stack0[STACK_SIZE];
char stack1[STACK_SIZE];
sigjmp_buf env[2];
int current_thread = -1;
void jump_to_thread(int tid)
{
current_thread = tid;
siglongjmp(env[tid], 1);
}
/**
* @brief Saves the current thread state, and jumps to the other thread.
*/
void yield(void)
{
int ret_val = sigsetjmp(env[current_thread], 1);
printf("yield: ret_val=%d\n", ret_val);
bool did_just_save_bookmark = ret_val == 0;
// bool did_jump_from_another_thread = ret_val != 0;
if (did_just_save_bookmark)
{
jump_to_thread(1 - current_thread);
}
}
void thread0(void)
{
int i = 0;
while (1)
{
++i;
printf("in thread0 (%d)\n", i);
if (i % 3 == 0)
{
printf("thread0: yielding\n");
yield();
}
usleep(SECOND);
}
}
void thread1(void)
{
int i = 0;
while (1)
{
++i;
printf("in thread1 (%d)\n", i);
if (i % 5 == 0)
{
printf("thread1: yielding\n");
yield();
}
usleep(SECOND);
}
}
void setup_thread(int tid, char * thread_entry_point entry_point)
{
// initializes env[tid] to use the right stack, and to run from the function 'entry_point', when we'll use
// siglongjmp to jump into the thread.
address_t sp = (address_t) stack + STACK_SIZE - sizeof(address_t);
address_t pc = (address_t) entry_point;
sigsetjmp(env[tid], 1);
(env[tid]->__jmpbuf)[JB_SP] = translate_address(sp);
(env[tid]->__jmpbuf)[JB_PC] = translate_address(pc);
sigemptyset(&env[tid]->__saved_mask);
}
void setup(void)
{
// sets up the threads, so they'll have the wanted initial SP and PC.
setup_thread(0, stack0, thread0);
setup_thread(1, stack1, thread1);
}
int main(void)
{
setup();
jump_to_thread(0);
return 0;
}