forked from nieklinnenbank/FreeNOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessShares.cpp
368 lines (318 loc) · 11 KB
/
ProcessShares.cpp
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* Copyright (C) 2015 Niek Linnenbank
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <FreeNOS/System.h>
#include <MemoryContext.h>
#include <MemoryChannel.h>
#include <List.h>
#include <ListIterator.h>
#include <SplitAllocator.h>
#include "ProcessEvent.h"
#include "ProcessManager.h"
ProcessShares::ProcessShares(ProcessID pid)
{
m_pid = pid;
m_memory = ZERO;
}
ProcessShares::~ProcessShares()
{
ProcessManager *procs = Kernel::instance()->getProcessManager();
List<ProcessID> pids;
// Make a list of unique process IDs which
// have a share with this Process
Size size = m_shares.size();
for (Size i = 0; i < size; i++)
{
MemoryShare *sh = m_shares.get(i);
if (sh)
{
if (!pids.contains(sh->pid))
pids.append(sh->pid);
releaseShare(sh, i);
}
}
// Raise process terminated events
for (ListIterator<ProcessID> i(pids); i.hasCurrent(); i++)
{
ProcessID pid = i.current();
Process *proc = procs->get(pid);
if (proc)
{
ProcessEvent event;
event.type = ProcessTerminated;
event.number = m_pid;
procs->raiseEvent(proc, &event);
}
}
}
const ProcessID ProcessShares::getProcessID() const
{
return m_pid;
}
MemoryContext * ProcessShares::getMemoryContext()
{
return m_memory;
}
ProcessShares::Result ProcessShares::setMemoryContext(MemoryContext *ctx)
{
m_memory = ctx;
return Success;
}
ProcessShares::Result ProcessShares::createShare(ProcessID pid,
Size coreId,
Size tagId,
Address virt,
Size size)
{
MemoryShare *share = ZERO;
MemoryContext::Result result = MemoryContext::Success;
if (size == 0 || size % PAGESIZE)
return InvalidArgument;
// This code currently does not support intra-core IPC
assert(coreId == coreInfo.coreId);
// Allocate MemoryShare objects
share = new MemoryShare;
if (!share)
{
ERROR("failed to allocate MemoryShare");
return OutOfMemory;
}
// Fill the share object
share->pid = pid;
share->coreId = coreId;
share->tagId = tagId;
share->range.virt = virt;
share->range.size = size;
// For the kernel channel, set to unattached to ensure releaseShare() always works
share->attached = !(pid == KERNEL_PID);
// Translate to physical address
if ((result = m_memory->lookup(share->range.virt, &share->range.phys)) != MemoryContext::Success)
{
ERROR("failed to translate share virtual address at " <<
(void *)share->range.virt << ": " << (int)result);
delete share;
return MemoryMapError;
}
assert(!(share->range.phys & ~PAGEMASK));
// Retrieve memory access permissions
if ((result = m_memory->access(share->range.virt, &share->range.access)) != MemoryContext::Success)
{
ERROR("failed to retrieve share access permissions for virtual address " <<
(void *)share->range.virt << ": " << (int)result);
delete share;
return MemoryMapError;
}
// insert into shares list
Size idx = 0;
m_shares.insert(idx, share);
return Success;
}
ProcessShares::Result ProcessShares::createShare(ProcessShares & instance,
ProcessShares::MemoryShare *share)
{
MemoryShare *localShare = ZERO;
MemoryShare *remoteShare = ZERO;
MemoryContext *localMem = m_memory;
MemoryContext *remoteMem = instance.getMemoryContext();
Arch::Cache cache;
Allocator::Range allocPhys, allocVirt;
Size idx = 0;
if (share->range.size == 0)
return InvalidArgument;
// Check if the share already exists
if (findShare(share->pid, share->coreId, share->tagId) != ZERO)
return AlreadyExists;
// Check if the remote share is still detaching
remoteShare = instance.findShare(m_pid, share->coreId, share->tagId);
if (remoteShare && !remoteShare->attached)
{
return DetachInProgress;
}
// Allocate local
localShare = new MemoryShare;
if (!localShare)
{
ERROR("failed to allocate MemoryShare for local process");
return OutOfMemory;
}
// Allocate remote
remoteShare = new MemoryShare;
if (!remoteShare)
{
ERROR("failed to allocate MemoryShare for remote process");
delete localShare;
return OutOfMemory;
}
// Allocate actual pages
allocPhys.address = 0;
allocPhys.size = share->range.size;
allocPhys.alignment = PAGESIZE;
if (Kernel::instance()->getAllocator()->allocate(allocPhys, allocVirt) != Allocator::Success)
{
ERROR("failed to allocate pages for MemoryShare");
return OutOfMemory;
}
// Zero out the pages
MemoryBlock::set((void *) allocVirt.address, 0, share->range.size);
for (Size i = 0; i < share->range.size; i+=PAGESIZE)
cache.cleanData(allocVirt.address + i);
// Fill the local share object
localShare->pid = instance.getProcessID();
localShare->coreId = Kernel::instance()->getCoreInfo()->coreId;
localShare->tagId = share->tagId;
localShare->range.phys = allocPhys.address;
localShare->range.size = share->range.size;
localShare->range.access = Memory::User | share->range.access;
localShare->attached = true;
// Map in the local process
if (localMem->findFree(localShare->range.size, MemoryMap::UserShare, &localShare->range.virt) != MemoryContext::Success ||
localMem->mapRangeContiguous(&localShare->range) != MemoryContext::Success)
{
ERROR("failed to map MemoryShare in local process");
delete localShare;
delete remoteShare;
return OutOfMemory;
}
// Fill the remote share object
remoteShare->pid = m_pid;
remoteShare->coreId = localShare->coreId;
remoteShare->tagId = localShare->tagId;
remoteShare->range.phys = localShare->range.phys;
remoteShare->range.size = localShare->range.size;
remoteShare->range.access = localShare->range.access;
remoteShare->attached = true;
// Map in the remote process
if (remoteMem->findFree(remoteShare->range.size, MemoryMap::UserShare, &remoteShare->range.virt) != MemoryContext::Success ||
remoteMem->mapRangeContiguous(&remoteShare->range) != MemoryContext::Success)
{
ERROR("failed to map MemoryShare in remote process");
delete localShare;
delete remoteShare;
return OutOfMemory;
}
// insert into shares list
m_shares.insert(idx, localShare);
instance.m_shares.insert(idx, remoteShare);
// raise event on the remote process
ProcessManager *procs = Kernel::instance()->getProcessManager();
Process *proc = procs->get(instance.getProcessID());
ProcessEvent event;
event.type = ShareCreated;
event.number = m_pid;
MemoryBlock::copy(&event.share, remoteShare, sizeof(*remoteShare));
procs->raiseEvent(proc, &event);
// Update parameter outputs
MemoryBlock::copy(share, localShare, sizeof(*share));
return Success;
}
ProcessShares::Result ProcessShares::removeShares(ProcessID pid)
{
const Size size = m_shares.size();
MemoryShare *s = 0;
for (Size i = 0; i < size; i++)
{
if ((s = m_shares.get(i)) != ZERO)
{
if (s->pid != pid)
continue;
releaseShare(s, i);
}
}
return Success;
}
ProcessShares::Result ProcessShares::releaseShare(MemoryShare *s, Size idx)
{
assert(s->coreId == coreInfo.coreId);
// Only release physical memory if both processes have detached.
// Note that in case all memory shares for a certain ProcessID have
// been detached but not yet released, and due to a very unlikely race
// condition (ProcessID reuse) a new memory share was just created (before old ones were released)
// the new memory share would also be detached here, resulting in a memory
// share with is detached in this process but attached and useless in the
// other process.
if (s->attached)
{
Process *proc = Kernel::instance()->getProcessManager()->get(s->pid);
if (proc)
{
ProcessShares & shares = proc->getShares();
const Size size = shares.m_shares.size();
// Mark all process shares detached in the other process
for (Size i = 0; i < size; i++)
{
MemoryShare *otherShare = shares.m_shares.get(i);
if (otherShare)
{
assert(otherShare->coreId == coreInfo.coreId);
if (otherShare->pid == m_pid && otherShare->coreId == s->coreId)
{
otherShare->attached = false;
}
}
}
}
}
else
{
// Only release physical memory pages if the other
// process already detached earlier
Allocator *alloc = Kernel::instance()->getAllocator();
for (Size i = 0; i < s->range.size; i += PAGESIZE)
alloc->release(s->range.phys + i);
}
// Unmap the share
m_memory->unmapRange(&s->range);
// Release the share object
delete s;
// Remove share from our administration
if (!m_shares.remove(idx))
{
return NotFound;
}
else
{
return Success;
}
}
ProcessShares::MemoryShare * ProcessShares::findShare(const ProcessID pid,
const Size coreId,
const Size tagId)
{
const Size size = m_shares.size();
for (Size i = 0; i < size; i++)
{
MemoryShare *s = m_shares.get(i);
if (s != ZERO)
{
assert(s->coreId == coreInfo.coreId);
if (s->pid == pid && s->coreId == coreId && s->tagId == tagId)
{
return s;
}
}
}
return ZERO;
}
ProcessShares::Result ProcessShares::readShare(MemoryShare *share)
{
const MemoryShare *s = findShare(share->pid, share->coreId, share->tagId);
if (s != ZERO)
{
MemoryBlock::copy(share, s, sizeof(MemoryShare));
return Success;
}
return NotFound;
}