-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathcontext.c
180 lines (141 loc) · 3.77 KB
/
context.c
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "context.h"
#include "function.h"
#include "env.h"
#include "vm.h"
#include "types.h"
#include "queue.h"
/* create a new context */
int context_create(context_t **cont)
{
*cont = memory_alloc(sizeof(context_t));
if (NULL == *cont) {
ERROR(-ERROR_MEM);
}
(*cont)->funcs.next = NULL;
(*cont)->funcs.prev = NULL;
(*cont)->anonym.next = NULL;
(*cont)->anonym.prev = NULL;
spin_lock_init(&(*cont)->lock);
return 0;
}
/* lock a context */
unsigned long context_lock(context_t *cont)
{
unsigned long flags;
spin_lock_irqsave(&cont->lock, flags);
return flags;
}
/* unlock a context */
void context_unlock(context_t *cont, unsigned long flags)
{
spin_unlock_irqrestore(&cont->lock, flags);
}
/* add a function to a context */
int context_add_function(context_t *cont, function_t *func)
{
function_t *check_func = NULL;
unsigned long flags;
int err = 0;
flags = context_lock(cont);
if (func->name != NULL) {
/* this is a function with a name */
check_func = LIST_TO_STRUCT(function_t, list, cont->funcs.next);
/* check that we don't have this function's name already */
while (check_func != NULL) {
if (!string_compare(check_func->name, func->name)) {
ERROR_CLEAN(-ERROR_FEXIST);
}
check_func = LIST_TO_STRUCT(function_t, list, check_func->list.next);
}
/* add the function to the funcs list */
func->list.next = cont->funcs.next;
func->list.prev = &cont->funcs;
if (cont->funcs.next != NULL) {
cont->funcs.next->prev = &func->list;
}
cont->funcs.next = &func->list;
} else {
/* this is a anonymous function */
/* add the function to the anonym list */
func->list.next = cont->anonym.next;
func->list.prev = &cont->anonym;
if (cont->anonym.next != NULL) {
cont->anonym.next->prev = &func->list;
}
cont->anonym.next = &func->list;
}
func->cont = cont;
clean:
context_unlock(cont, flags);
return err;
}
/* remove and delete a function from the context */
void context_free_function(function_t *func)
{
context_t *cont = func->cont;
unsigned long flags;
flags = context_lock(cont);
if (NULL == func->list.prev && NULL == func->list.next) {
/* This is a race condition! The function was already removed from the context... */
goto clean;
}
func->list.prev->next = func->list.next;
if (func->list.next != NULL) {
func->list.next->prev = func->list.prev;
}
func->list.prev = NULL;
func->list.next = NULL;
queue_kill(&func->to_user);
queue_kill(&func->to_kernel);
function_put(func);
clean:
context_unlock(cont, flags);
}
/* delete a context */
void context_free(context_t *cont)
{
/* we don't need to lock because this is called only when we free the context anyway */
while (cont->funcs.next != NULL) {
context_free_function(LIST_TO_STRUCT(function_t, list, cont->funcs.next));
}
while (cont->anonym.next != NULL) {
context_free_function(LIST_TO_STRUCT(function_t, list, cont->anonym.next));
}
memory_free(cont);
}
/* find a function by name */
void *context_find_function(context_t *cont, byte *name)
{
function_t *func;
unsigned long flags;
flags = context_lock(cont);
func = LIST_TO_STRUCT(function_t, list, cont->funcs.next);
while (func != NULL) {
if (!string_compare(func->name, (char *)name)) {
function_get(func);
goto clean;
}
func = LIST_TO_STRUCT(function_t, list, func->list.next);
}
clean:
context_unlock(cont, flags);
return func;
}
/* find an anonymous function by address */
void *context_find_anonymous(context_t *cont, byte *ptr)
{
unsigned long flags;
function_t *func;
flags = context_lock(cont);
func = LIST_TO_STRUCT(function_t, list, cont->anonym.next);
while (func != NULL) {
if (func->func_code == ptr) {
function_get(func);
goto clean;
}
func = LIST_TO_STRUCT(function_t, list, func->list.next);
}
clean:
context_unlock(cont, flags);
return func;
}