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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
/* The kernel call implemented in this file:
* m_type: SYS_UPDATE
*
* The parameters for this kernel call are:
* m2_i1: SYS_UPD_SRC_ENDPT (source process endpoint)
* m2_i2: SYS_UPD_DST_ENDPT (destination process endpoint)
* m2_i3: SYS_UPD_FLAGS (update flags)
*/
#include "kernel/system.h"
#include <string.h>
#include <assert.h>
#if USE_UPDATE
#define DEBUG 0
#define proc_is_updatable(p) \
(RTS_ISSET(p, RTS_NO_PRIV) || RTS_ISSET(p, RTS_SIG_PENDING) \
|| (RTS_ISSET(p, RTS_RECEIVING) && !RTS_ISSET(p, RTS_SENDING)))
static int inherit_priv_irq(struct proc *src_rp, struct proc *dst_rp);
static int inherit_priv_io(struct proc *src_rp, struct proc *dst_rp);
static int inherit_priv_mem(struct proc *src_rp, struct proc *dst_rp);
static void abort_proc_ipc_send(struct proc *rp);
static void adjust_proc_slot(struct proc *rp, struct proc *from_rp);
static void adjust_priv_slot(struct priv *privp, struct priv
*from_privp);
static void adjust_asyn_table(struct priv *src_privp, struct priv *dst_privp);
static void swap_proc_slot_pointer(struct proc **rpp, struct proc
*src_rp, struct proc *dst_rp);
static void swap_memreq(struct proc *src_rp, struct proc *dst_rp);
/*===========================================================================*
* do_update *
*===========================================================================*/
int do_update(struct proc * caller, message * m_ptr)
{
/* Handle sys_update(). Update a process into another by swapping their process
* slots.
*/
endpoint_t src_e, dst_e;
int src_p, dst_p, flags;
struct proc *src_rp, *dst_rp;
struct priv *src_privp, *dst_privp;
struct proc orig_src_proc;
struct proc orig_dst_proc;
struct priv orig_src_priv;
struct priv orig_dst_priv;
int i, r;
/* Lookup slots for source and destination process. */
flags = m_ptr->SYS_UPD_FLAGS;
src_e = m_ptr->SYS_UPD_SRC_ENDPT;
if(!isokendpt(src_e, &src_p)) {
return EINVAL;
}
src_rp = proc_addr(src_p);
src_privp = priv(src_rp);
if(!(src_privp->s_flags & SYS_PROC)) {
return EPERM;
}
dst_e = m_ptr->SYS_UPD_DST_ENDPT;
if(!isokendpt(dst_e, &dst_p)) {
return EINVAL;
}
dst_rp = proc_addr(dst_p);
dst_privp = priv(dst_rp);
if(!(dst_privp->s_flags & SYS_PROC)) {
return EPERM;
}
assert(!proc_is_runnable(src_rp) && !proc_is_runnable(dst_rp));
/* Check if processes are updatable. */
if(!proc_is_updatable(src_rp) || !proc_is_updatable(dst_rp)) {
return EBUSY;
}
#if DEBUG
printf("do_update: updating %d (%s, %d, %d) into %d (%s, %d, %d)\n",
src_rp->p_endpoint, src_rp->p_name, src_rp->p_nr, priv(src_rp)->s_proc_nr,
dst_rp->p_endpoint, dst_rp->p_name, dst_rp->p_nr, priv(dst_rp)->s_proc_nr);
proc_stacktrace(src_rp);
proc_stacktrace(dst_rp);
printf("do_update: curr ptproc %d\n", get_cpulocal_var(ptproc)->p_endpoint);
printf("do_update: endpoint %d rts flags %x asyn tab %08x asyn endpoint %d grant tab %08x grant endpoint %d\n", src_rp->p_endpoint, src_rp->p_rts_flags, priv(src_rp)->s_asyntab, priv(src_rp)->s_asynendpoint, priv(src_rp)->s_grant_table, priv(src_rp)->s_grant_endpoint);
printf("do_update: endpoint %d rts flags %x asyn tab %08x asyn endpoint %d grant tab %08x grant endpoint %d\n", dst_rp->p_endpoint, dst_rp->p_rts_flags, priv(dst_rp)->s_asyntab, priv(dst_rp)->s_asynendpoint, priv(dst_rp)->s_grant_table, priv(dst_rp)->s_grant_endpoint);
#endif
/* Let destination inherit allowed IRQ, I/O ranges, and memory ranges. */
r = inherit_priv_irq(src_rp, dst_rp);
if(r != OK) {
return r;
}
r = inherit_priv_io(src_rp, dst_rp);
if(r != OK) {
return r;
}
r = inherit_priv_mem(src_rp, dst_rp);
if(r != OK) {
return r;
}
/* Let destination inherit the target mask from source. */
for (i=0; i < NR_SYS_PROCS; i++) {
if (get_sys_bit(priv(src_rp)->s_ipc_to, i)) {
set_sendto_bit(dst_rp, i);
}
}
/* Save existing data. */
orig_src_proc = *src_rp;
orig_src_priv = *(priv(src_rp));
orig_dst_proc = *dst_rp;
orig_dst_priv = *(priv(dst_rp));
/* Adjust asyn tables. */
adjust_asyn_table(priv(src_rp), priv(dst_rp));
adjust_asyn_table(priv(dst_rp), priv(src_rp));
/* Abort any pending send() on rollback. */
if(flags & SYS_UPD_ROLLBACK) {
abort_proc_ipc_send(src_rp);
}
/* Swap slots. */
*src_rp = orig_dst_proc;
*src_privp = orig_dst_priv;
*dst_rp = orig_src_proc;
*dst_privp = orig_src_priv;
/* Adjust process slots. */
adjust_proc_slot(src_rp, &orig_src_proc);
adjust_proc_slot(dst_rp, &orig_dst_proc);
/* Adjust privilege slots. */
adjust_priv_slot(priv(src_rp), &orig_src_priv);
adjust_priv_slot(priv(dst_rp), &orig_dst_priv);
/* Swap global process slot addresses. */
swap_proc_slot_pointer(get_cpulocal_var_ptr(ptproc), src_rp, dst_rp);
/* Swap VM request entries. */
swap_memreq(src_rp, dst_rp);
#if DEBUG
printf("do_update: updated %d (%s, %d, %d) into %d (%s, %d, %d)\n",
src_rp->p_endpoint, src_rp->p_name, src_rp->p_nr, priv(src_rp)->s_proc_nr,
dst_rp->p_endpoint, dst_rp->p_name, dst_rp->p_nr, priv(dst_rp)->s_proc_nr);
proc_stacktrace(src_rp);
proc_stacktrace(dst_rp);
printf("do_update: curr ptproc %d\n", get_cpulocal_var(ptproc)->p_endpoint);
printf("do_update: endpoint %d rts flags %x asyn tab %08x asyn endpoint %d grant tab %08x grant endpoint %d\n", src_rp->p_endpoint, src_rp->p_rts_flags, priv(src_rp)->s_asyntab, priv(src_rp)->s_asynendpoint, priv(src_rp)->s_grant_table, priv(src_rp)->s_grant_endpoint);
printf("do_update: endpoint %d rts flags %x asyn tab %08x asyn endpoint %d grant tab %08x grant endpoint %d\n", dst_rp->p_endpoint, dst_rp->p_rts_flags, priv(dst_rp)->s_asyntab, priv(dst_rp)->s_asynendpoint, priv(dst_rp)->s_grant_table, priv(dst_rp)->s_grant_endpoint);
#endif
#ifdef CONFIG_SMP
bits_fill(src_rp->p_stale_tlb, CONFIG_MAX_CPUS);
bits_fill(dst_rp->p_stale_tlb, CONFIG_MAX_CPUS);
#endif
return OK;
}
/*===========================================================================*
* inherit_priv_irq *
*===========================================================================*/
int inherit_priv_irq(struct proc *src_rp, struct proc *dst_rp)
{
int i, r;
for (i= 0; i<priv(src_rp)->s_nr_irq; i++) {
r = priv_add_irq(dst_rp, priv(src_rp)->s_irq_tab[i]);
if(r != OK) {
return r;
}
}
return OK;
}
/*===========================================================================*
* inherit_priv_io *
*===========================================================================*/
int inherit_priv_io(struct proc *src_rp, struct proc *dst_rp)
{
int i, r;
for (i= 0; i<priv(src_rp)->s_nr_io_range; i++) {
r = priv_add_io(dst_rp, &(priv(src_rp)->s_io_tab[i]));
if(r != OK) {
return r;
}
}
return OK;
}
/*===========================================================================*
* inherit_priv_mem *
*===========================================================================*/
int inherit_priv_mem(struct proc *src_rp, struct proc *dst_rp)
{
int i, r;
for (i= 0; i<priv(src_rp)->s_nr_mem_range; i++) {
r = priv_add_mem(dst_rp, &(priv(src_rp)->s_mem_tab[i]));
if(r != OK) {
return r;
}
}
return OK;
}
/*===========================================================================*
* abort_proc_ipc_send *
*===========================================================================*/
void abort_proc_ipc_send(struct proc *rp)
{
if(RTS_ISSET(rp, RTS_SENDING)) {
struct proc **xpp;
RTS_UNSET(rp, RTS_SENDING);
rp->p_misc_flags &= ~MF_SENDING_FROM_KERNEL;
xpp = &(proc_addr(_ENDPOINT_P(rp->p_sendto_e))->p_caller_q);
while (*xpp) {
if(*xpp == rp) {
*xpp = rp->p_q_link;
rp->p_q_link = NULL;
break;
}
xpp = &(*xpp)->p_q_link;
}
}
}
/*===========================================================================*
* adjust_proc_slot *
*===========================================================================*/
static void adjust_proc_slot(struct proc *rp, struct proc *from_rp)
{
/* Preserve endpoints, slot numbers, priv structure, and IPC. */
rp->p_endpoint = from_rp->p_endpoint;
rp->p_nr = from_rp->p_nr;
rp->p_priv = from_rp->p_priv;
priv(rp)->s_proc_nr = from_rp->p_nr;
rp->p_caller_q = from_rp->p_caller_q;
/* preserve scheduling */
rp->p_scheduler = from_rp->p_scheduler;
#ifdef CONFIG_SMP
rp->p_cpu = from_rp->p_cpu;
memcpy(rp->p_cpu_mask, from_rp->p_cpu_mask,
sizeof(bitchunk_t) * BITMAP_CHUNKS(CONFIG_MAX_CPUS));
#endif
}
/*===========================================================================*
* adjust_asyn_table *
*===========================================================================*/
static void adjust_asyn_table(struct priv *src_privp, struct priv *dst_privp)
{
/* Transfer the asyn table if source's table belongs to the destination. */
endpoint_t src_e = proc_addr(src_privp->s_proc_nr)->p_endpoint;
endpoint_t dst_e = proc_addr(dst_privp->s_proc_nr)->p_endpoint;
if(src_privp->s_asynsize > 0 && dst_privp->s_asynsize > 0 && src_privp->s_asynendpoint == dst_e) {
if(data_copy(src_e, src_privp->s_asyntab, dst_e, dst_privp->s_asyntab,
src_privp->s_asynsize*sizeof(asynmsg_t)) != OK) {
printf("Warning: unable to transfer asyn table from ep %d to ep %d\n",
src_e, dst_e);
}
else {
dst_privp->s_asynsize = src_privp->s_asynsize;
}
}
}
/*===========================================================================*
* adjust_priv_slot *
*===========================================================================*/
static void adjust_priv_slot(struct priv *privp, struct priv *from_privp)
{
/* Preserve privilege ids and non-privilege stuff in the priv structure. */
privp->s_id = from_privp->s_id;
privp->s_asyn_pending = from_privp->s_asyn_pending;
privp->s_notify_pending = from_privp->s_notify_pending;
privp->s_int_pending = from_privp->s_int_pending;
privp->s_sig_pending = from_privp->s_sig_pending;
privp->s_alarm_timer = from_privp->s_alarm_timer;
privp->s_diag_sig = from_privp->s_diag_sig;
}
/*===========================================================================*
* swap_proc_slot_pointer *
*===========================================================================*/
static void swap_proc_slot_pointer(struct proc **rpp, struct proc *src_rp,
struct proc *dst_rp)
{
if(*rpp == src_rp) {
*rpp = dst_rp;
}
else if(*rpp == dst_rp) {
*rpp = src_rp;
}
}
/*===========================================================================*
* swap_memreq *
*===========================================================================*/
static void swap_memreq(struct proc *src_rp, struct proc *dst_rp)
{
/* If either the source or the destination process is part of the VM request
* chain, but not both, then swap the process pointers in the chain.
*/
struct proc **rpp;
if (RTS_ISSET(src_rp, RTS_VMREQUEST) == RTS_ISSET(dst_rp, RTS_VMREQUEST))
return; /* nothing to do */
for (rpp = &vmrequest; *rpp != NULL;
rpp = &(*rpp)->p_vmrequest.nextrequestor) {
if (*rpp == src_rp) {
dst_rp->p_vmrequest.nextrequestor =
src_rp->p_vmrequest.nextrequestor;
*rpp = dst_rp;
break;
} else if (*rpp == dst_rp) {
src_rp->p_vmrequest.nextrequestor =
dst_rp->p_vmrequest.nextrequestor;
*rpp = src_rp;
break;
}
}
}
#endif /* USE_UPDATE */
|